Automatic date update in version.in
[binutils-gdb.git] / gas / expr.c
blob03caa91f4d18a8f25361d8d36b4b975cc0c83840
1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 /* This is really a branch office of as-read.c. I split it out to clearly
22 distinguish the world of expressions from the world of statements.
23 (It also gives smaller files to re-compile.)
24 Here, "operand"s are of expressions, not instructions. */
26 #define min(a, b) ((a) < (b) ? (a) : (b))
28 #include "as.h"
29 #include "safe-ctype.h"
31 #include <limits.h>
32 #ifndef CHAR_BIT
33 #define CHAR_BIT 8
34 #endif
36 bool literal_prefix_dollar_hex = false;
38 static void clean_up_expression (expressionS * expressionP);
40 /* We keep a mapping of expression symbols to file positions, so that
41 we can provide better error messages. */
43 struct expr_symbol_line {
44 struct expr_symbol_line *next;
45 symbolS *sym;
46 const char *file;
47 unsigned int line;
50 static struct expr_symbol_line *expr_symbol_lines;
52 /* Build a dummy symbol to hold a complex expression. This is how we
53 build expressions up out of other expressions. The symbol is put
54 into the fake section expr_section. */
56 symbolS *
57 make_expr_symbol (expressionS *expressionP)
59 expressionS zero;
60 symbolS *symbolP;
61 struct expr_symbol_line *n;
63 if (expressionP->X_op == O_symbol
64 && expressionP->X_add_number == 0)
65 return expressionP->X_add_symbol;
67 if (expressionP->X_op == O_big)
69 /* This won't work, because the actual value is stored in
70 generic_floating_point_number or generic_bignum, and we are
71 going to lose it if we haven't already. */
72 if (expressionP->X_add_number > 0)
73 as_bad (_("bignum invalid"));
74 else
75 as_bad (_("floating point number invalid"));
76 zero.X_op = O_constant;
77 zero.X_add_number = 0;
78 zero.X_unsigned = 0;
79 zero.X_extrabit = 0;
80 clean_up_expression (&zero);
81 expressionP = &zero;
84 /* Putting constant symbols in absolute_section rather than
85 expr_section is convenient for the old a.out code, for which
86 S_GET_SEGMENT does not always retrieve the value put in by
87 S_SET_SEGMENT. */
88 symbolP = symbol_create (FAKE_LABEL_NAME,
89 (expressionP->X_op == O_constant
90 ? absolute_section
91 : expressionP->X_op == O_register
92 ? reg_section
93 : expr_section),
94 &zero_address_frag, 0);
95 symbol_set_value_expression (symbolP, expressionP);
97 if (expressionP->X_op == O_constant)
98 resolve_symbol_value (symbolP);
100 n = XNEW (struct expr_symbol_line);
101 n->sym = symbolP;
102 n->file = as_where (&n->line);
103 n->next = expr_symbol_lines;
104 expr_symbol_lines = n;
106 return symbolP;
109 /* Return the file and line number for an expr symbol. Return
110 non-zero if something was found, 0 if no information is known for
111 the symbol. */
114 expr_symbol_where (symbolS *sym, const char **pfile, unsigned int *pline)
116 struct expr_symbol_line *l;
118 for (l = expr_symbol_lines; l != NULL; l = l->next)
120 if (l->sym == sym)
122 *pfile = l->file;
123 *pline = l->line;
124 return 1;
128 return 0;
131 /* Look up a previously used .startof. / .sizeof. symbol, or make a fresh
132 one. */
134 static symbolS *
135 symbol_lookup_or_make (const char *name, bool start)
137 static symbolS **seen[2];
138 static unsigned int nr_seen[2];
139 char *buf = concat (start ? ".startof." : ".sizeof.", name, NULL);
140 symbolS *symbolP;
141 unsigned int i;
143 for (i = 0; i < nr_seen[start]; ++i)
145 symbolP = seen[start][i];
147 if (! symbolP)
148 break;
150 name = S_GET_NAME (symbolP);
151 if ((symbols_case_sensitive
152 ? strcasecmp (buf, name)
153 : strcmp (buf, name)) == 0)
155 free (buf);
156 return symbolP;
160 symbolP = symbol_make (buf);
161 free (buf);
163 if (i >= nr_seen[start])
165 unsigned int nr = (i + 1) * 2;
167 seen[start] = XRESIZEVEC (symbolS *, seen[start], nr);
168 nr_seen[start] = nr;
169 memset (&seen[start][i + 1], 0, (nr - i - 1) * sizeof(seen[0][0]));
172 seen[start][i] = symbolP;
174 return symbolP;
177 /* Utilities for building expressions.
178 Since complex expressions are recorded as symbols for use in other
179 expressions these return a symbolS * and not an expressionS *.
180 These explicitly do not take an "add_number" argument. */
181 /* ??? For completeness' sake one might want expr_build_symbol.
182 It would just return its argument. */
184 /* Build an expression for an unsigned constant.
185 The corresponding one for signed constants is missing because
186 there's currently no need for it. One could add an unsigned_p flag
187 but that seems more clumsy. */
189 symbolS *
190 expr_build_uconstant (offsetT value)
192 expressionS e;
194 e.X_op = O_constant;
195 e.X_add_number = value;
196 e.X_unsigned = 1;
197 e.X_extrabit = 0;
198 return make_expr_symbol (&e);
201 /* Build an expression for the current location ('.'). */
203 symbolS *
204 expr_build_dot (void)
206 expressionS e;
208 current_location (&e);
209 return symbol_clone_if_forward_ref (make_expr_symbol (&e));
212 /* Build any floating-point literal here.
213 Also build any bignum literal here. */
215 /* Seems atof_machine can backscan through generic_bignum and hit whatever
216 happens to be loaded before it in memory. And its way too complicated
217 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
218 and never write into the early words, thus they'll always be zero.
219 I hate Dean's floating-point code. Bleh. */
220 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
222 FLONUM_TYPE generic_floating_point_number = {
223 &generic_bignum[6], /* low. (JF: Was 0) */
224 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
225 0, /* leader. */
226 0, /* exponent. */
227 0 /* sign. */
231 static void
232 floating_constant (expressionS *expressionP)
234 /* input_line_pointer -> floating-point constant. */
235 int error_code;
237 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
238 &generic_floating_point_number);
240 if (error_code)
242 if (error_code == ERROR_EXPONENT_OVERFLOW)
244 as_bad (_("bad floating-point constant: exponent overflow"));
246 else
248 as_bad (_("bad floating-point constant: unknown error code=%d"),
249 error_code);
252 expressionP->X_op = O_big;
253 /* input_line_pointer -> just after constant, which may point to
254 whitespace. */
255 expressionP->X_add_number = -1;
258 uint32_t
259 generic_bignum_to_int32 (void)
261 return ((((uint32_t) generic_bignum[1] & LITTLENUM_MASK)
262 << LITTLENUM_NUMBER_OF_BITS)
263 | ((uint32_t) generic_bignum[0] & LITTLENUM_MASK));
266 uint64_t
267 generic_bignum_to_int64 (void)
269 return ((((((((uint64_t) generic_bignum[3] & LITTLENUM_MASK)
270 << LITTLENUM_NUMBER_OF_BITS)
271 | ((uint64_t) generic_bignum[2] & LITTLENUM_MASK))
272 << LITTLENUM_NUMBER_OF_BITS)
273 | ((uint64_t) generic_bignum[1] & LITTLENUM_MASK))
274 << LITTLENUM_NUMBER_OF_BITS)
275 | ((uint64_t) generic_bignum[0] & LITTLENUM_MASK));
278 static void
279 integer_constant (int radix, expressionS *expressionP)
281 char *start; /* Start of number. */
282 char *suffix = NULL;
283 char c;
284 valueT number; /* Offset or (absolute) value. */
285 short int digit; /* Value of next digit in current radix. */
286 short int maxdig = 0; /* Highest permitted digit value. */
287 int too_many_digits = 0; /* If we see >= this number of. */
288 char *name; /* Points to name of symbol. */
289 symbolS *symbolP; /* Points to symbol. */
291 int small; /* True if fits in 32 bits. */
293 /* May be bignum, or may fit in 32 bits. */
294 /* Most numbers fit into 32 bits, and we want this case to be fast.
295 so we pretend it will fit into 32 bits. If, after making up a 32
296 bit number, we realise that we have scanned more digits than
297 comfortably fit into 32 bits, we re-scan the digits coding them
298 into a bignum. For decimal and octal numbers we are
299 conservative: Some numbers may be assumed bignums when in fact
300 they do fit into 32 bits. Numbers of any radix can have excess
301 leading zeros: We strive to recognise this and cast them back
302 into 32 bits. We must check that the bignum really is more than
303 32 bits, and change it back to a 32-bit number if it fits. The
304 number we are looking for is expected to be positive, but if it
305 fits into 32 bits as an unsigned number, we let it be a 32-bit
306 number. The cavalier approach is for speed in ordinary cases. */
307 /* This has been extended for 64 bits. We blindly assume that if
308 you're compiling in 64-bit mode, the target is a 64-bit machine.
309 This should be cleaned up. */
311 #ifdef BFD64
312 #define valuesize 64
313 #else /* includes non-bfd case, mostly */
314 #define valuesize 32
315 #endif
317 if (is_end_of_line[(unsigned char) *input_line_pointer])
319 expressionP->X_op = O_absent;
320 return;
323 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
325 int flt = 0;
327 /* In MRI mode, the number may have a suffix indicating the
328 radix. For that matter, it might actually be a floating
329 point constant. */
330 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
332 if (*suffix == 'e' || *suffix == 'E')
333 flt = 1;
336 if (suffix == input_line_pointer)
338 radix = 10;
339 suffix = NULL;
341 else
343 c = *--suffix;
344 c = TOUPPER (c);
345 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
346 we distinguish between 'B' and 'b'. This is the case for
347 Z80. */
348 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
349 radix = 2;
350 else if (c == 'D')
351 radix = 10;
352 else if (c == 'O' || c == 'Q')
353 radix = 8;
354 else if (c == 'H')
355 radix = 16;
356 else if (suffix[1] == '.' || c == 'E' || flt)
358 floating_constant (expressionP);
359 return;
361 else
363 radix = 10;
364 suffix = NULL;
369 switch (radix)
371 case 2:
372 maxdig = 2;
373 too_many_digits = valuesize + 1;
374 break;
375 case 8:
376 maxdig = radix = 8;
377 too_many_digits = (valuesize + 2) / 3 + 1;
378 break;
379 case 16:
380 maxdig = radix = 16;
381 too_many_digits = (valuesize + 3) / 4 + 1;
382 break;
383 case 10:
384 maxdig = radix = 10;
385 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
387 #undef valuesize
388 start = input_line_pointer;
389 c = *input_line_pointer++;
390 for (number = 0;
391 (digit = hex_value (c)) < maxdig;
392 c = *input_line_pointer++)
394 number = number * radix + digit;
396 /* c contains character after number. */
397 /* input_line_pointer->char after c. */
398 small = (input_line_pointer - start - 1) < too_many_digits;
400 if (radix == 16 && c == '_')
402 /* This is literal of the form 0x333_0_12345678_1.
403 This example is equivalent to 0x00000333000000001234567800000001. */
405 int num_little_digits = 0;
406 int i;
407 input_line_pointer = start; /* -> 1st digit. */
409 know (LITTLENUM_NUMBER_OF_BITS == 16);
411 for (c = '_'; c == '_'; num_little_digits += 2)
414 /* Convert one 64-bit word. */
415 int ndigit = 0;
416 number = 0;
417 for (c = *input_line_pointer++;
418 (digit = hex_value (c)) < maxdig;
419 c = *(input_line_pointer++))
421 number = number * radix + digit;
422 ndigit++;
425 /* Check for 8 digit per word max. */
426 if (ndigit > 8)
427 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
429 /* Add this chunk to the bignum.
430 Shift things down 2 little digits. */
431 know (LITTLENUM_NUMBER_OF_BITS == 16);
432 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
433 i >= 2;
434 i--)
435 generic_bignum[i] = generic_bignum[i - 2];
437 /* Add the new digits as the least significant new ones. */
438 generic_bignum[0] = number & 0xffffffff;
439 generic_bignum[1] = number >> 16;
442 /* Again, c is char after number, input_line_pointer->after c. */
444 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
445 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
447 gas_assert (num_little_digits >= 4);
449 if (num_little_digits != 8)
450 as_bad (_("a bignum with underscores must have exactly 4 words"));
452 /* We might have some leading zeros. These can be trimmed to give
453 us a change to fit this constant into a small number. */
454 while (generic_bignum[num_little_digits - 1] == 0
455 && num_little_digits > 1)
456 num_little_digits--;
458 if (num_little_digits <= 2)
460 /* will fit into 32 bits. */
461 number = generic_bignum_to_int32 ();
462 small = 1;
464 #ifdef BFD64
465 else if (num_little_digits <= 4)
467 /* Will fit into 64 bits. */
468 number = generic_bignum_to_int64 ();
469 small = 1;
471 #endif
472 else
474 small = 0;
476 /* Number of littlenums in the bignum. */
477 number = num_little_digits;
480 else if (!small)
482 /* We saw a lot of digits. manufacture a bignum the hard way. */
483 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
484 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
485 long carry;
487 leader = generic_bignum;
488 generic_bignum[0] = 0;
489 generic_bignum[1] = 0;
490 generic_bignum[2] = 0;
491 generic_bignum[3] = 0;
492 input_line_pointer = start; /* -> 1st digit. */
493 c = *input_line_pointer++;
494 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
496 for (pointer = generic_bignum; pointer <= leader; pointer++)
498 long work;
500 work = carry + radix * *pointer;
501 *pointer = work & LITTLENUM_MASK;
502 carry = work >> LITTLENUM_NUMBER_OF_BITS;
504 if (carry)
506 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
508 /* Room to grow a longer bignum. */
509 *++leader = carry;
513 /* Again, c is char after number. */
514 /* input_line_pointer -> after c. */
515 know (LITTLENUM_NUMBER_OF_BITS == 16);
516 if (leader < generic_bignum + 2)
518 /* Will fit into 32 bits. */
519 number = generic_bignum_to_int32 ();
520 small = 1;
522 #ifdef BFD64
523 else if (leader < generic_bignum + 4)
525 /* Will fit into 64 bits. */
526 number = generic_bignum_to_int64 ();
527 small = 1;
529 #endif
530 else
532 /* Number of littlenums in the bignum. */
533 number = leader - generic_bignum + 1;
537 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
538 && suffix != NULL
539 && input_line_pointer - 1 == suffix)
540 c = *input_line_pointer++;
542 #ifndef tc_allow_U_suffix
543 #define tc_allow_U_suffix 1
544 #endif
545 /* PR 19910: Look for, and ignore, a U suffix to the number. */
546 if (tc_allow_U_suffix && (c == 'U' || c == 'u'))
547 c = * input_line_pointer++;
549 #ifndef tc_allow_L_suffix
550 #define tc_allow_L_suffix 1
551 #endif
552 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
553 if (tc_allow_L_suffix)
554 while (c == 'L' || c == 'l')
555 c = * input_line_pointer++;
557 if (small)
559 /* Here with number, in correct radix. c is the next char.
560 Note that unlike un*x, we allow "011f" "0x9f" to both mean
561 the same as the (conventional) "9f".
562 This is simply easier than checking for strict canonical
563 form. Syntax sux! */
565 if (LOCAL_LABELS_FB && c == 'b')
567 /* Backward ref to local label.
568 Because it is backward, expect it to be defined. */
569 /* Construct a local label. */
570 name = fb_label_name ((int) number, 0);
572 /* Seen before, or symbol is defined: OK. */
573 symbolP = symbol_find (name);
574 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
576 /* Local labels are never absolute. Don't waste time
577 checking absoluteness. */
578 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
580 expressionP->X_op = O_symbol;
581 expressionP->X_add_symbol = symbolP;
583 else
585 /* Either not seen or not defined. */
586 /* @@ Should print out the original string instead of
587 the parsed number. */
588 as_bad (_("backward ref to unknown label \"%d:\""),
589 (int) number);
590 expressionP->X_op = O_constant;
593 expressionP->X_add_number = 0;
594 } /* case 'b' */
595 else if (LOCAL_LABELS_FB && c == 'f')
597 /* Forward reference. Expect symbol to be undefined or
598 unknown. undefined: seen it before. unknown: never seen
599 it before.
601 Construct a local label name, then an undefined symbol.
602 Don't create a xseg frag for it: caller may do that.
603 Just return it as never seen before. */
604 name = fb_label_name ((int) number, 1);
605 symbolP = symbol_find_or_make (name);
606 /* We have no need to check symbol properties. */
607 #ifndef many_segments
608 /* Since "know" puts its arg into a "string", we
609 can't have newlines in the argument. */
610 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
611 #endif
612 expressionP->X_op = O_symbol;
613 expressionP->X_add_symbol = symbolP;
614 expressionP->X_add_number = 0;
615 } /* case 'f' */
616 else if (LOCAL_LABELS_DOLLAR && c == '$')
618 /* If the dollar label is *currently* defined, then this is just
619 another reference to it. If it is not *currently* defined,
620 then this is a fresh instantiation of that number, so create
621 it. */
623 if (dollar_label_defined ((long) number))
625 name = dollar_label_name ((long) number, 0);
626 symbolP = symbol_find (name);
627 know (symbolP != NULL);
629 else
631 name = dollar_label_name ((long) number, 1);
632 symbolP = symbol_find_or_make (name);
635 expressionP->X_op = O_symbol;
636 expressionP->X_add_symbol = symbolP;
637 expressionP->X_add_number = 0;
638 } /* case '$' */
639 else
641 expressionP->X_op = O_constant;
642 expressionP->X_add_number = number;
643 input_line_pointer--; /* Restore following character. */
644 } /* Really just a number. */
646 else
648 /* Not a small number. */
649 expressionP->X_op = O_big;
650 expressionP->X_add_number = number; /* Number of littlenums. */
651 input_line_pointer--; /* -> char following number. */
655 /* Parse an MRI multi character constant. */
657 static void
658 mri_char_constant (expressionS *expressionP)
660 int i;
662 if (*input_line_pointer == '\''
663 && input_line_pointer[1] != '\'')
665 expressionP->X_op = O_constant;
666 expressionP->X_add_number = 0;
667 return;
670 /* In order to get the correct byte ordering, we must build the
671 number in reverse. */
672 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
674 int j;
676 generic_bignum[i] = 0;
677 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
679 if (*input_line_pointer == '\'')
681 if (input_line_pointer[1] != '\'')
682 break;
683 ++input_line_pointer;
685 generic_bignum[i] <<= 8;
686 generic_bignum[i] += *input_line_pointer;
687 ++input_line_pointer;
690 if (i < SIZE_OF_LARGE_NUMBER - 1)
692 /* If there is more than one littlenum, left justify the
693 last one to make it match the earlier ones. If there is
694 only one, we can just use the value directly. */
695 for (; j < CHARS_PER_LITTLENUM; j++)
696 generic_bignum[i] <<= 8;
699 if (*input_line_pointer == '\''
700 && input_line_pointer[1] != '\'')
701 break;
704 if (i < 0)
706 as_bad (_("character constant too large"));
707 i = 0;
710 if (i > 0)
712 int c;
713 int j;
715 c = SIZE_OF_LARGE_NUMBER - i;
716 for (j = 0; j < c; j++)
717 generic_bignum[j] = generic_bignum[i + j];
718 i = c;
721 know (LITTLENUM_NUMBER_OF_BITS == 16);
722 if (i > 2)
724 expressionP->X_op = O_big;
725 expressionP->X_add_number = i;
727 else
729 expressionP->X_op = O_constant;
730 if (i < 2)
731 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
732 else
733 expressionP->X_add_number =
734 (((generic_bignum[1] & LITTLENUM_MASK)
735 << LITTLENUM_NUMBER_OF_BITS)
736 | (generic_bignum[0] & LITTLENUM_MASK));
739 /* Skip the final closing quote. */
740 ++input_line_pointer;
743 /* Return an expression representing the current location. This
744 handles the magic symbol `.'. */
746 void
747 current_location (expressionS *expressionp)
749 if (now_seg == absolute_section)
751 expressionp->X_op = O_constant;
752 expressionp->X_add_number = abs_section_offset;
754 else
756 expressionp->X_op = O_symbol;
757 expressionp->X_add_symbol = &dot_symbol;
758 expressionp->X_add_number = 0;
762 /* In: Input_line_pointer points to 1st char of operand, which may
763 be a space.
765 Out: An expressionS.
766 The operand may have been empty: in this case X_op == O_absent.
767 Input_line_pointer->(next non-blank) char after operand. */
769 static segT
770 operand (expressionS *expressionP, enum expr_mode mode)
772 char c;
773 symbolS *symbolP; /* Points to symbol. */
774 char *name; /* Points to name of symbol. */
775 segT segment;
777 /* All integers are regarded as unsigned unless they are negated.
778 This is because the only thing which cares whether a number is
779 unsigned is the code in emit_expr which extends constants into
780 bignums. It should only sign extend negative numbers, so that
781 something like ``.quad 0x80000000'' is not sign extended even
782 though it appears negative if valueT is 32 bits. */
783 expressionP->X_unsigned = 1;
784 expressionP->X_extrabit = 0;
786 /* Digits, assume it is a bignum. */
788 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
789 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
791 if (is_end_of_line[(unsigned char) c])
792 goto eol;
794 switch (c)
796 case '1':
797 case '2':
798 case '3':
799 case '4':
800 case '5':
801 case '6':
802 case '7':
803 case '8':
804 case '9':
805 input_line_pointer--;
807 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
808 ? 0 : 10,
809 expressionP);
810 break;
812 #ifdef LITERAL_PREFIXPERCENT_BIN
813 case '%':
814 integer_constant (2, expressionP);
815 break;
816 #endif
818 case '0':
819 /* Non-decimal radix. */
821 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
823 char *s;
825 /* Check for a hex or float constant. */
826 for (s = input_line_pointer; hex_p (*s); s++)
828 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
830 --input_line_pointer;
831 integer_constant (0, expressionP);
832 break;
835 c = *input_line_pointer;
836 switch (c)
838 case 'o':
839 case 'O':
840 case 'q':
841 case 'Q':
842 case '8':
843 case '9':
844 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
846 integer_constant (0, expressionP);
847 break;
849 /* Fall through. */
850 default:
851 default_case:
852 if (c && strchr (FLT_CHARS, c))
854 input_line_pointer++;
855 floating_constant (expressionP);
856 expressionP->X_add_number = - TOLOWER (c);
858 else
860 /* The string was only zero. */
861 expressionP->X_op = O_constant;
862 expressionP->X_add_number = 0;
865 break;
867 case 'x':
868 case 'X':
869 if (flag_m68k_mri)
870 goto default_case;
871 input_line_pointer++;
872 integer_constant (16, expressionP);
873 break;
875 case 'b':
876 if (LOCAL_LABELS_FB && !flag_m68k_mri
877 && input_line_pointer[1] != '0'
878 && input_line_pointer[1] != '1')
880 /* Parse this as a back reference to label 0. */
881 input_line_pointer--;
882 integer_constant (10, expressionP);
883 break;
885 /* Otherwise, parse this as a binary number. */
886 /* Fall through. */
887 case 'B':
888 if (input_line_pointer[1] == '0'
889 || input_line_pointer[1] == '1')
891 input_line_pointer++;
892 integer_constant (2, expressionP);
893 break;
895 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
896 input_line_pointer++;
897 goto default_case;
899 case '0':
900 case '1':
901 case '2':
902 case '3':
903 case '4':
904 case '5':
905 case '6':
906 case '7':
907 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
908 ? 0 : 8,
909 expressionP);
910 break;
912 case 'f':
913 if (LOCAL_LABELS_FB)
915 int is_label = 1;
917 /* If it says "0f" and it could possibly be a floating point
918 number, make it one. Otherwise, make it a local label,
919 and try to deal with parsing the rest later. */
920 if (!is_end_of_line[(unsigned char) input_line_pointer[1]]
921 && strchr (FLT_CHARS, 'f') != NULL)
923 char *cp = input_line_pointer + 1;
925 atof_generic (&cp, ".", EXP_CHARS,
926 &generic_floating_point_number);
928 /* Was nothing parsed, or does it look like an
929 expression? */
930 is_label = (cp == input_line_pointer + 1
931 || (cp == input_line_pointer + 2
932 && (cp[-1] == '-' || cp[-1] == '+'))
933 || *cp == 'f'
934 || *cp == 'b');
936 if (is_label)
938 input_line_pointer--;
939 integer_constant (10, expressionP);
940 break;
943 /* Fall through. */
945 case 'd':
946 case 'D':
947 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
949 integer_constant (0, expressionP);
950 break;
952 /* Fall through. */
953 case 'F':
954 case 'r':
955 case 'e':
956 case 'E':
957 case 'g':
958 case 'G':
959 input_line_pointer++;
960 floating_constant (expressionP);
961 expressionP->X_add_number = - TOLOWER (c);
962 break;
964 case '$':
965 if (LOCAL_LABELS_DOLLAR)
967 integer_constant (10, expressionP);
968 break;
970 else
971 goto default_case;
974 break;
976 #ifndef NEED_INDEX_OPERATOR
977 case '[':
978 # ifdef md_need_index_operator
979 if (md_need_index_operator())
980 goto de_fault;
981 # endif
982 #endif
983 /* Fall through. */
984 case '(':
985 /* Didn't begin with digit & not a name. */
986 segment = expr (0, expressionP, mode);
987 /* expression () will pass trailing whitespace. */
988 if ((c == '(' && *input_line_pointer != ')')
989 || (c == '[' && *input_line_pointer != ']'))
991 if (* input_line_pointer)
992 as_bad (_("found '%c', expected: '%c'"),
993 * input_line_pointer, c == '(' ? ')' : ']');
994 else
995 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
997 else
998 input_line_pointer++;
999 SKIP_WHITESPACE ();
1000 /* Here with input_line_pointer -> char after "(...)". */
1001 return segment;
1003 #ifdef TC_M68K
1004 case 'E':
1005 if (! flag_m68k_mri || *input_line_pointer != '\'')
1006 goto de_fault;
1007 as_bad (_("EBCDIC constants are not supported"));
1008 /* Fall through. */
1009 case 'A':
1010 if (! flag_m68k_mri || *input_line_pointer != '\'')
1011 goto de_fault;
1012 ++input_line_pointer;
1013 #endif
1014 /* Fall through. */
1015 case '\'':
1016 if (! flag_m68k_mri)
1018 /* Warning: to conform to other people's assemblers NO
1019 ESCAPEMENT is permitted for a single quote. The next
1020 character, parity errors and all, is taken as the value
1021 of the operand. VERY KINKY. */
1022 expressionP->X_op = O_constant;
1023 expressionP->X_add_number = *input_line_pointer++;
1024 break;
1027 mri_char_constant (expressionP);
1028 break;
1030 #ifdef TC_M68K
1031 case '"':
1032 /* Double quote is the bitwise not operator in MRI mode. */
1033 if (! flag_m68k_mri)
1034 goto de_fault;
1035 #endif
1036 /* Fall through. */
1037 case '~':
1038 /* '~' is permitted to start a label on the Delta. */
1039 if (is_name_beginner (c))
1040 goto isname;
1041 /* Fall through. */
1042 case '!':
1043 case '-':
1044 case '+':
1046 #ifdef md_operator
1047 unary:
1048 #endif
1049 operand (expressionP, mode);
1050 if (expressionP->X_op == O_constant)
1052 /* input_line_pointer -> char after operand. */
1053 if (c == '-')
1055 expressionP->X_add_number
1056 = - (addressT) expressionP->X_add_number;
1057 /* Notice: '-' may overflow: no warning is given.
1058 This is compatible with other people's
1059 assemblers. Sigh. */
1060 expressionP->X_unsigned = 0;
1061 if (expressionP->X_add_number)
1062 expressionP->X_extrabit ^= 1;
1064 else if (c == '~' || c == '"')
1066 expressionP->X_add_number = ~ expressionP->X_add_number;
1067 expressionP->X_extrabit ^= 1;
1069 else if (c == '!')
1071 expressionP->X_add_number = ! expressionP->X_add_number;
1072 expressionP->X_unsigned = 1;
1073 expressionP->X_extrabit = 0;
1076 else if (expressionP->X_op == O_big
1077 && expressionP->X_add_number <= 0
1078 && c == '-'
1079 && (generic_floating_point_number.sign == '+'
1080 || generic_floating_point_number.sign == 'P'))
1082 /* Negative flonum (eg, -1.000e0). */
1083 if (generic_floating_point_number.sign == '+')
1084 generic_floating_point_number.sign = '-';
1085 else
1086 generic_floating_point_number.sign = 'N';
1088 else if (expressionP->X_op == O_big
1089 && expressionP->X_add_number > 0)
1091 int i;
1093 if (c == '~' || c == '-')
1095 for (i = 0; i < expressionP->X_add_number; ++i)
1096 generic_bignum[i] = ~generic_bignum[i];
1098 /* Extend the bignum to at least the size of .octa. */
1099 if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1101 expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1102 for (; i < expressionP->X_add_number; ++i)
1103 generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1106 if (c == '-')
1107 for (i = 0; i < expressionP->X_add_number; ++i)
1109 generic_bignum[i] += 1;
1110 if (generic_bignum[i])
1111 break;
1114 else if (c == '!')
1116 for (i = 0; i < expressionP->X_add_number; ++i)
1117 if (generic_bignum[i] != 0)
1118 break;
1119 expressionP->X_add_number = i >= expressionP->X_add_number;
1120 expressionP->X_op = O_constant;
1121 expressionP->X_unsigned = 1;
1122 expressionP->X_extrabit = 0;
1125 else if (expressionP->X_op != O_illegal
1126 && expressionP->X_op != O_absent)
1128 if (c != '+')
1130 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1131 if (c == '-')
1132 expressionP->X_op = O_uminus;
1133 else if (c == '~' || c == '"')
1134 expressionP->X_op = O_bit_not;
1135 else
1136 expressionP->X_op = O_logical_not;
1137 expressionP->X_add_number = 0;
1140 else
1141 as_warn (_("Unary operator %c ignored because bad operand follows"),
1144 break;
1146 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1147 case '$':
1148 if (literal_prefix_dollar_hex)
1150 /* $L is the start of a local label, not a hex constant. */
1151 if (* input_line_pointer == 'L')
1152 goto isname;
1153 integer_constant (16, expressionP);
1155 else
1157 goto isname;
1159 break;
1160 #else
1161 case '$':
1162 /* '$' is the program counter when in MRI mode, or when
1163 DOLLAR_DOT is defined. */
1164 #ifndef DOLLAR_DOT
1165 if (! flag_m68k_mri)
1166 goto de_fault;
1167 #endif
1168 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1170 /* In MRI mode and on Z80, '$' is also used as the prefix
1171 for a hexadecimal constant. */
1172 integer_constant (16, expressionP);
1173 break;
1176 if (is_part_of_name (*input_line_pointer))
1177 goto isname;
1179 current_location (expressionP);
1180 break;
1181 #endif
1183 case '.':
1184 if (!is_part_of_name (*input_line_pointer))
1186 current_location (expressionP);
1187 break;
1189 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1190 && ! is_part_of_name (input_line_pointer[8]))
1191 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1192 && ! is_part_of_name (input_line_pointer[7])))
1194 int start;
1196 start = (input_line_pointer[1] == 't'
1197 || input_line_pointer[1] == 'T');
1198 input_line_pointer += start ? 8 : 7;
1199 SKIP_WHITESPACE ();
1201 /* Cover for the as_bad () invocations below. */
1202 expressionP->X_op = O_absent;
1204 if (*input_line_pointer != '(')
1205 as_bad (_("syntax error in .startof. or .sizeof."));
1206 else
1208 ++input_line_pointer;
1209 SKIP_WHITESPACE ();
1210 c = get_symbol_name (& name);
1211 if (! *name)
1213 as_bad (_("expected symbol name"));
1214 (void) restore_line_pointer (c);
1215 if (c != ')')
1216 ignore_rest_of_line ();
1217 else
1218 ++input_line_pointer;
1219 break;
1222 expressionP->X_op = O_symbol;
1223 expressionP->X_add_symbol = symbol_lookup_or_make (name, start);
1224 expressionP->X_add_number = 0;
1226 *input_line_pointer = c;
1227 SKIP_WHITESPACE_AFTER_NAME ();
1228 if (*input_line_pointer != ')')
1229 as_bad (_("syntax error in .startof. or .sizeof."));
1230 else
1231 ++input_line_pointer;
1233 break;
1235 else
1237 goto isname;
1240 case ',':
1241 eol:
1242 /* Can't imagine any other kind of operand. */
1243 expressionP->X_op = O_absent;
1244 input_line_pointer--;
1245 break;
1247 #ifdef TC_M68K
1248 case '%':
1249 if (! flag_m68k_mri)
1250 goto de_fault;
1251 integer_constant (2, expressionP);
1252 break;
1254 case '@':
1255 if (! flag_m68k_mri)
1256 goto de_fault;
1257 integer_constant (8, expressionP);
1258 break;
1260 case ':':
1261 if (! flag_m68k_mri)
1262 goto de_fault;
1264 /* In MRI mode, this is a floating point constant represented
1265 using hexadecimal digits. */
1267 ++input_line_pointer;
1268 integer_constant (16, expressionP);
1269 break;
1271 case '*':
1272 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1273 goto de_fault;
1275 current_location (expressionP);
1276 break;
1277 #endif
1279 default:
1280 #if defined(md_need_index_operator) || defined(TC_M68K)
1281 de_fault:
1282 #endif
1283 if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
1285 /* Identifier begins here.
1286 This is kludged for speed, so code is repeated. */
1287 isname:
1288 -- input_line_pointer;
1289 c = get_symbol_name (&name);
1291 #ifdef md_operator
1293 operatorT op = md_operator (name, 1, &c);
1295 switch (op)
1297 case O_uminus:
1298 restore_line_pointer (c);
1299 c = '-';
1300 goto unary;
1301 case O_bit_not:
1302 restore_line_pointer (c);
1303 c = '~';
1304 goto unary;
1305 case O_logical_not:
1306 restore_line_pointer (c);
1307 c = '!';
1308 goto unary;
1309 case O_illegal:
1310 as_bad (_("invalid use of operator \"%s\""), name);
1311 break;
1312 default:
1313 break;
1316 if (op != O_absent && op != O_illegal)
1318 restore_line_pointer (c);
1319 expr (9, expressionP, mode);
1320 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1321 expressionP->X_op_symbol = NULL;
1322 expressionP->X_add_number = 0;
1323 expressionP->X_op = op;
1324 break;
1327 #endif
1329 #ifdef md_parse_name
1330 /* This is a hook for the backend to parse certain names
1331 specially in certain contexts. If a name always has a
1332 specific value, it can often be handled by simply
1333 entering it in the symbol table. */
1334 if (md_parse_name (name, expressionP, mode, &c))
1336 restore_line_pointer (c);
1337 break;
1339 #endif
1341 symbolP = symbol_find_or_make (name);
1343 /* If we have an absolute symbol or a reg, then we know its
1344 value now. */
1345 segment = S_GET_SEGMENT (symbolP);
1346 if (mode != expr_defer
1347 && segment == absolute_section
1348 && !S_FORCE_RELOC (symbolP, 0))
1350 expressionP->X_op = O_constant;
1351 expressionP->X_add_number = S_GET_VALUE (symbolP);
1353 else if (mode != expr_defer && segment == reg_section)
1355 expressionP->X_op = O_register;
1356 expressionP->X_add_number = S_GET_VALUE (symbolP);
1358 else
1360 expressionP->X_op = O_symbol;
1361 expressionP->X_add_symbol = symbolP;
1362 expressionP->X_add_number = 0;
1365 restore_line_pointer (c);
1367 else
1369 /* Let the target try to parse it. Success is indicated by changing
1370 the X_op field to something other than O_absent and pointing
1371 input_line_pointer past the expression. If it can't parse the
1372 expression, X_op and input_line_pointer should be unchanged. */
1373 expressionP->X_op = O_absent;
1374 --input_line_pointer;
1375 md_operand (expressionP);
1376 if (expressionP->X_op == O_absent)
1378 ++input_line_pointer;
1379 as_bad (_("bad expression"));
1380 expressionP->X_op = O_constant;
1381 expressionP->X_add_number = 0;
1384 break;
1387 /* It is more 'efficient' to clean up the expressionS when they are
1388 created. Doing it here saves lines of code. */
1389 clean_up_expression (expressionP);
1390 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1391 know (*input_line_pointer != ' ');
1393 /* The PA port needs this information. */
1394 if (expressionP->X_add_symbol)
1395 symbol_mark_used (expressionP->X_add_symbol);
1397 if (mode != expr_defer)
1399 expressionP->X_add_symbol
1400 = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1401 expressionP->X_op_symbol
1402 = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1405 switch (expressionP->X_op)
1407 default:
1408 return absolute_section;
1409 case O_symbol:
1410 return S_GET_SEGMENT (expressionP->X_add_symbol);
1411 case O_register:
1412 return reg_section;
1416 /* Internal. Simplify a struct expression for use by expr (). */
1418 /* In: address of an expressionS.
1419 The X_op field of the expressionS may only take certain values.
1420 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1422 Out: expressionS may have been modified:
1423 Unused fields zeroed to help expr (). */
1425 static void
1426 clean_up_expression (expressionS *expressionP)
1428 switch (expressionP->X_op)
1430 case O_illegal:
1431 case O_absent:
1432 expressionP->X_add_number = 0;
1433 /* Fall through. */
1434 case O_big:
1435 case O_constant:
1436 case O_register:
1437 expressionP->X_add_symbol = NULL;
1438 /* Fall through. */
1439 case O_symbol:
1440 case O_uminus:
1441 case O_bit_not:
1442 expressionP->X_op_symbol = NULL;
1443 break;
1444 default:
1445 break;
1449 /* Expression parser. */
1451 /* We allow an empty expression, and just assume (absolute,0) silently.
1452 Unary operators and parenthetical expressions are treated as operands.
1453 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1455 We used to do an aho/ullman shift-reduce parser, but the logic got so
1456 warped that I flushed it and wrote a recursive-descent parser instead.
1457 Now things are stable, would anybody like to write a fast parser?
1458 Most expressions are either register (which does not even reach here)
1459 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1460 So I guess it doesn't really matter how inefficient more complex expressions
1461 are parsed.
1463 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1464 Also, we have consumed any leading or trailing spaces (operand does that)
1465 and done all intervening operators.
1467 This returns the segment of the result, which will be
1468 absolute_section or the segment of a symbol. */
1470 #undef __
1471 #define __ O_illegal
1472 #ifndef O_SINGLE_EQ
1473 #define O_SINGLE_EQ O_illegal
1474 #endif
1476 /* Maps ASCII -> operators. */
1477 static const operatorT op_encoding[256] = {
1478 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1479 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1481 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1482 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1483 __, __, __, __, __, __, __, __,
1484 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1485 __, __, __, __, __, __, __, __,
1486 __, __, __, __, __, __, __, __,
1487 __, __, __, __, __, __, __, __,
1488 __, __, __,
1489 #ifdef NEED_INDEX_OPERATOR
1490 O_index,
1491 #else
1493 #endif
1494 __, __, O_bit_exclusive_or, __,
1495 __, __, __, __, __, __, __, __,
1496 __, __, __, __, __, __, __, __,
1497 __, __, __, __, __, __, __, __,
1498 __, __, __, __, O_bit_inclusive_or, __, __, __,
1500 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1501 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1502 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1503 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1504 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1505 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1506 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1507 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1510 /* Rank Examples
1511 0 operand, (expression)
1512 1 ||
1513 2 &&
1514 3 == <> < <= >= >
1515 4 + -
1516 5 used for * / % in MRI mode
1517 6 & ^ ! |
1518 7 * / % << >>
1519 8 unary - unary ~
1521 static operator_rankT op_rank[O_max] = {
1522 0, /* O_illegal */
1523 0, /* O_absent */
1524 0, /* O_constant */
1525 0, /* O_symbol */
1526 0, /* O_symbol_rva */
1527 0, /* O_register */
1528 0, /* O_big */
1529 9, /* O_uminus */
1530 9, /* O_bit_not */
1531 9, /* O_logical_not */
1532 8, /* O_multiply */
1533 8, /* O_divide */
1534 8, /* O_modulus */
1535 8, /* O_left_shift */
1536 8, /* O_right_shift */
1537 7, /* O_bit_inclusive_or */
1538 7, /* O_bit_or_not */
1539 7, /* O_bit_exclusive_or */
1540 7, /* O_bit_and */
1541 5, /* O_add */
1542 5, /* O_subtract */
1543 4, /* O_eq */
1544 4, /* O_ne */
1545 4, /* O_lt */
1546 4, /* O_le */
1547 4, /* O_ge */
1548 4, /* O_gt */
1549 3, /* O_logical_and */
1550 2, /* O_logical_or */
1551 1, /* O_index */
1554 /* Unfortunately, in MRI mode for the m68k, multiplication and
1555 division have lower precedence than the bit wise operators. This
1556 function sets the operator precedences correctly for the current
1557 mode. Also, MRI uses a different bit_not operator, and this fixes
1558 that as well. */
1560 #define STANDARD_MUL_PRECEDENCE 8
1561 #define MRI_MUL_PRECEDENCE 6
1563 void
1564 expr_set_precedence (void)
1566 if (flag_m68k_mri)
1568 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1569 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1570 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1572 else
1574 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1575 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1576 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1580 void
1581 expr_set_rank (operatorT op, operator_rankT rank)
1583 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1584 op_rank[op] = rank;
1587 /* Initialize the expression parser. */
1589 void
1590 expr_begin (void)
1592 expr_set_precedence ();
1594 /* Verify that X_op field is wide enough. */
1596 expressionS e;
1597 e.X_op = O_max;
1598 gas_assert (e.X_op == O_max);
1602 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1603 sets NUM_CHARS to the number of characters in the operator.
1604 Does not advance INPUT_LINE_POINTER. */
1606 static inline operatorT
1607 operatorf (int *num_chars)
1609 int c;
1610 operatorT ret;
1612 c = *input_line_pointer & 0xff;
1613 *num_chars = 1;
1615 if (is_end_of_line[c])
1616 return O_illegal;
1618 #ifdef md_operator
1619 if (is_name_beginner (c))
1621 char *name;
1622 char ec = get_symbol_name (& name);
1624 ret = md_operator (name, 2, &ec);
1625 switch (ret)
1627 case O_absent:
1628 *input_line_pointer = ec;
1629 input_line_pointer = name;
1630 break;
1631 case O_uminus:
1632 case O_bit_not:
1633 case O_logical_not:
1634 as_bad (_("invalid use of operator \"%s\""), name);
1635 ret = O_illegal;
1636 /* FALLTHROUGH */
1637 default:
1638 *input_line_pointer = ec;
1639 *num_chars = input_line_pointer - name;
1640 input_line_pointer = name;
1641 return ret;
1644 #endif
1646 switch (c)
1648 default:
1649 ret = op_encoding[c];
1650 #ifdef md_operator
1651 if (ret == O_illegal)
1653 char *start = input_line_pointer;
1655 ret = md_operator (NULL, 2, NULL);
1656 if (ret != O_illegal)
1657 *num_chars = input_line_pointer - start;
1658 input_line_pointer = start;
1660 #endif
1661 return ret;
1663 case '+':
1664 case '-':
1665 return op_encoding[c];
1667 case '<':
1668 switch (input_line_pointer[1])
1670 default:
1671 return op_encoding[c];
1672 case '<':
1673 ret = O_left_shift;
1674 break;
1675 case '>':
1676 ret = O_ne;
1677 break;
1678 case '=':
1679 ret = O_le;
1680 break;
1682 *num_chars = 2;
1683 return ret;
1685 case '=':
1686 if (input_line_pointer[1] != '=')
1687 return op_encoding[c];
1689 *num_chars = 2;
1690 return O_eq;
1692 case '>':
1693 switch (input_line_pointer[1])
1695 default:
1696 return op_encoding[c];
1697 case '>':
1698 ret = O_right_shift;
1699 break;
1700 case '=':
1701 ret = O_ge;
1702 break;
1704 *num_chars = 2;
1705 return ret;
1707 case '!':
1708 switch (input_line_pointer[1])
1710 case '!':
1711 /* We accept !! as equivalent to ^ for MRI compatibility. */
1712 *num_chars = 2;
1713 return O_bit_exclusive_or;
1714 case '=':
1715 /* We accept != as equivalent to <>. */
1716 *num_chars = 2;
1717 return O_ne;
1718 default:
1719 if (flag_m68k_mri)
1720 return O_bit_inclusive_or;
1721 return op_encoding[c];
1724 case '|':
1725 if (input_line_pointer[1] != '|')
1726 return op_encoding[c];
1728 *num_chars = 2;
1729 return O_logical_or;
1731 case '&':
1732 if (input_line_pointer[1] != '&')
1733 return op_encoding[c];
1735 *num_chars = 2;
1736 return O_logical_and;
1739 /* NOTREACHED */
1742 /* Implement "word-size + 1 bit" addition for
1743 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1744 is used so that the full range of unsigned word values and the full range of
1745 signed word values can be represented in an O_constant expression, which is
1746 useful e.g. for .sleb128 directives. */
1748 void
1749 add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1751 valueT ures = resultP->X_add_number;
1752 valueT uamount = amount;
1754 resultP->X_add_number += uamount;
1756 resultP->X_extrabit ^= rhs_highbit;
1758 if (ures + uamount < ures)
1759 resultP->X_extrabit ^= 1;
1762 /* Similarly, for subtraction. */
1764 void
1765 subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1767 valueT ures = resultP->X_add_number;
1768 valueT uamount = amount;
1770 resultP->X_add_number -= uamount;
1772 resultP->X_extrabit ^= rhs_highbit;
1774 if (ures < uamount)
1775 resultP->X_extrabit ^= 1;
1778 /* Parse an expression. */
1780 segT
1781 expr (int rankarg, /* Larger # is higher rank. */
1782 expressionS *resultP, /* Deliver result here. */
1783 enum expr_mode mode /* Controls behavior. */)
1785 operator_rankT rank = (operator_rankT) rankarg;
1786 segT retval;
1787 expressionS right;
1788 operatorT op_left;
1789 operatorT op_right;
1790 int op_chars;
1792 know (rankarg >= 0);
1794 /* Save the value of dot for the fixup code. */
1795 if (rank == 0)
1797 dot_value = frag_now_fix ();
1798 dot_frag = frag_now;
1801 retval = operand (resultP, mode);
1803 /* operand () gobbles spaces. */
1804 know (*input_line_pointer != ' ');
1806 op_left = operatorf (&op_chars);
1807 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1809 segT rightseg;
1810 offsetT frag_off;
1812 input_line_pointer += op_chars; /* -> after operator. */
1814 right.X_md = 0;
1815 rightseg = expr (op_rank[(int) op_left], &right, mode);
1816 if (right.X_op == O_absent)
1818 as_warn (_("missing operand; zero assumed"));
1819 right.X_op = O_constant;
1820 right.X_add_number = 0;
1821 right.X_add_symbol = NULL;
1822 right.X_op_symbol = NULL;
1825 know (*input_line_pointer != ' ');
1827 if (op_left == O_index)
1829 if (*input_line_pointer != ']')
1830 as_bad ("missing right bracket");
1831 else
1833 ++input_line_pointer;
1834 SKIP_WHITESPACE ();
1838 op_right = operatorf (&op_chars);
1840 know (op_right == O_illegal || op_left == O_index
1841 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1842 know ((int) op_left >= (int) O_multiply);
1843 #ifndef md_operator
1844 know ((int) op_left <= (int) O_index);
1845 #else
1846 know ((int) op_left < (int) O_max);
1847 #endif
1849 /* input_line_pointer->after right-hand quantity. */
1850 /* left-hand quantity in resultP. */
1851 /* right-hand quantity in right. */
1852 /* operator in op_left. */
1854 if (resultP->X_op == O_big)
1856 if (resultP->X_add_number > 0)
1857 as_warn (_("left operand is a bignum; integer 0 assumed"));
1858 else
1859 as_warn (_("left operand is a float; integer 0 assumed"));
1860 resultP->X_op = O_constant;
1861 resultP->X_add_number = 0;
1862 resultP->X_add_symbol = NULL;
1863 resultP->X_op_symbol = NULL;
1865 if (right.X_op == O_big)
1867 if (right.X_add_number > 0)
1868 as_warn (_("right operand is a bignum; integer 0 assumed"));
1869 else
1870 as_warn (_("right operand is a float; integer 0 assumed"));
1871 right.X_op = O_constant;
1872 right.X_add_number = 0;
1873 right.X_add_symbol = NULL;
1874 right.X_op_symbol = NULL;
1877 if (mode == expr_defer
1878 && ((resultP->X_add_symbol != NULL
1879 && S_IS_FORWARD_REF (resultP->X_add_symbol))
1880 || (right.X_add_symbol != NULL
1881 && S_IS_FORWARD_REF (right.X_add_symbol))))
1882 goto general;
1884 /* Optimize common cases. */
1885 #ifdef md_optimize_expr
1886 if (md_optimize_expr (resultP, op_left, &right))
1888 /* Skip. */
1891 else
1892 #endif
1893 #ifndef md_register_arithmetic
1894 # define md_register_arithmetic 1
1895 #endif
1896 if (op_left == O_add && right.X_op == O_constant
1897 && (md_register_arithmetic || resultP->X_op != O_register))
1899 /* X + constant. */
1900 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1902 /* This case comes up in PIC code. */
1903 else if (op_left == O_subtract
1904 && right.X_op == O_symbol
1905 && resultP->X_op == O_symbol
1906 && retval == rightseg
1907 #ifdef md_allow_local_subtract
1908 && md_allow_local_subtract (resultP, & right, rightseg)
1909 #endif
1910 && ((SEG_NORMAL (rightseg)
1911 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1912 && !S_FORCE_RELOC (right.X_add_symbol, 0))
1913 || right.X_add_symbol == resultP->X_add_symbol)
1914 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1915 symbol_get_frag (right.X_add_symbol),
1916 &frag_off))
1918 offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1919 - S_GET_VALUE (right.X_add_symbol);
1920 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1921 subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1922 add_to_result (resultP, symval_diff, symval_diff < 0);
1923 resultP->X_op = O_constant;
1924 resultP->X_add_symbol = 0;
1926 else if (op_left == O_subtract && right.X_op == O_constant
1927 && (md_register_arithmetic || resultP->X_op != O_register))
1929 /* X - constant. */
1930 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1932 else if (op_left == O_add && resultP->X_op == O_constant
1933 && (md_register_arithmetic || right.X_op != O_register))
1935 /* Constant + X. */
1936 resultP->X_op = right.X_op;
1937 resultP->X_add_symbol = right.X_add_symbol;
1938 resultP->X_op_symbol = right.X_op_symbol;
1939 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1940 retval = rightseg;
1942 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1944 /* Constant OP constant. */
1945 offsetT v = right.X_add_number;
1946 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1948 as_warn (_("division by zero"));
1949 v = 1;
1951 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1952 && (op_left == O_left_shift || op_left == O_right_shift))
1954 as_warn_value_out_of_range (_("shift count"), v, 0,
1955 sizeof(valueT) * CHAR_BIT - 1,
1956 NULL, 0);
1957 resultP->X_add_number = v = 0;
1959 switch (op_left)
1961 default: goto general;
1962 case O_multiply: resultP->X_add_number *= v; break;
1963 case O_divide: resultP->X_add_number /= v; break;
1964 case O_modulus: resultP->X_add_number %= v; break;
1965 case O_left_shift:
1966 /* We always use unsigned shifts. According to the ISO
1967 C standard, left shift of a signed type having a
1968 negative value is undefined behaviour, and right
1969 shift of a signed type having negative value is
1970 implementation defined. Left shift of a signed type
1971 when the result overflows is also undefined
1972 behaviour. So don't trigger ubsan warnings or rely
1973 on characteristics of the compiler. */
1974 resultP->X_add_number
1975 = (valueT) resultP->X_add_number << (valueT) v;
1976 break;
1977 case O_right_shift:
1978 resultP->X_add_number
1979 = (valueT) resultP->X_add_number >> (valueT) v;
1980 break;
1981 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1982 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1983 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1984 case O_bit_and: resultP->X_add_number &= v; break;
1985 /* Constant + constant (O_add) is handled by the
1986 previous if statement for constant + X, so is omitted
1987 here. */
1988 case O_subtract:
1989 subtract_from_result (resultP, v, 0);
1990 break;
1991 case O_eq:
1992 resultP->X_add_number =
1993 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1994 break;
1995 case O_ne:
1996 resultP->X_add_number =
1997 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1998 break;
1999 case O_lt:
2000 resultP->X_add_number =
2001 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
2002 break;
2003 case O_le:
2004 resultP->X_add_number =
2005 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
2006 break;
2007 case O_ge:
2008 resultP->X_add_number =
2009 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
2010 break;
2011 case O_gt:
2012 resultP->X_add_number =
2013 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
2014 break;
2015 case O_logical_and:
2016 resultP->X_add_number = resultP->X_add_number && v;
2017 break;
2018 case O_logical_or:
2019 resultP->X_add_number = resultP->X_add_number || v;
2020 break;
2023 else if (resultP->X_op == O_symbol
2024 && right.X_op == O_symbol
2025 && (op_left == O_add
2026 || op_left == O_subtract
2027 || (resultP->X_add_number == 0
2028 && right.X_add_number == 0)))
2030 /* Symbol OP symbol. */
2031 resultP->X_op = op_left;
2032 resultP->X_op_symbol = right.X_add_symbol;
2033 if (op_left == O_add)
2034 add_to_result (resultP, right.X_add_number, right.X_extrabit);
2035 else if (op_left == O_subtract)
2037 subtract_from_result (resultP, right.X_add_number,
2038 right.X_extrabit);
2039 if (retval == rightseg
2040 && SEG_NORMAL (retval)
2041 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2042 && !S_FORCE_RELOC (right.X_add_symbol, 0))
2044 retval = absolute_section;
2045 rightseg = absolute_section;
2049 else
2051 general:
2052 /* The general case. */
2053 resultP->X_add_symbol = make_expr_symbol (resultP);
2054 resultP->X_op_symbol = make_expr_symbol (&right);
2055 resultP->X_op = op_left;
2056 resultP->X_add_number = 0;
2057 resultP->X_unsigned = 1;
2058 resultP->X_extrabit = 0;
2061 if (retval != rightseg)
2063 if (retval == undefined_section)
2065 else if (rightseg == undefined_section)
2066 retval = rightseg;
2067 else if (retval == expr_section)
2069 else if (rightseg == expr_section)
2070 retval = rightseg;
2071 else if (retval == reg_section)
2073 else if (rightseg == reg_section)
2074 retval = rightseg;
2075 else if (rightseg == absolute_section)
2077 else if (retval == absolute_section)
2078 retval = rightseg;
2079 #ifdef DIFF_EXPR_OK
2080 else if (op_left == O_subtract)
2082 #endif
2083 else
2084 as_bad (_("operation combines symbols in different segments"));
2087 op_left = op_right;
2088 } /* While next operator is >= this rank. */
2090 /* The PA port needs this information. */
2091 if (resultP->X_add_symbol)
2092 symbol_mark_used (resultP->X_add_symbol);
2094 if (rank == 0 && mode == expr_evaluate)
2095 resolve_expression (resultP);
2097 return resultP->X_op == O_constant ? absolute_section : retval;
2100 /* Resolve an expression without changing any symbols/sub-expressions
2101 used. */
2104 resolve_expression (expressionS *expressionP)
2106 /* Help out with CSE. */
2107 valueT final_val = expressionP->X_add_number;
2108 symbolS *add_symbol = expressionP->X_add_symbol;
2109 symbolS *orig_add_symbol = add_symbol;
2110 symbolS *op_symbol = expressionP->X_op_symbol;
2111 operatorT op = expressionP->X_op;
2112 valueT left, right;
2113 segT seg_left, seg_right;
2114 fragS *frag_left, *frag_right;
2115 offsetT frag_off;
2117 switch (op)
2119 default:
2120 return 0;
2122 case O_constant:
2123 case O_register:
2124 left = 0;
2125 break;
2127 case O_symbol:
2128 case O_symbol_rva:
2129 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2130 return 0;
2132 break;
2134 case O_uminus:
2135 case O_bit_not:
2136 case O_logical_not:
2137 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2138 return 0;
2140 if (seg_left != absolute_section)
2141 return 0;
2143 if (op == O_logical_not)
2144 left = !left;
2145 else if (op == O_uminus)
2146 left = -left;
2147 else
2148 left = ~left;
2149 op = O_constant;
2150 break;
2152 case O_multiply:
2153 case O_divide:
2154 case O_modulus:
2155 case O_left_shift:
2156 case O_right_shift:
2157 case O_bit_inclusive_or:
2158 case O_bit_or_not:
2159 case O_bit_exclusive_or:
2160 case O_bit_and:
2161 case O_add:
2162 case O_subtract:
2163 case O_eq:
2164 case O_ne:
2165 case O_lt:
2166 case O_le:
2167 case O_ge:
2168 case O_gt:
2169 case O_logical_and:
2170 case O_logical_or:
2171 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2172 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2173 return 0;
2175 /* Simplify addition or subtraction of a constant by folding the
2176 constant into X_add_number. */
2177 if (op == O_add)
2179 if (seg_right == absolute_section)
2181 final_val += right;
2182 op = O_symbol;
2183 break;
2185 else if (seg_left == absolute_section)
2187 final_val += left;
2188 left = right;
2189 seg_left = seg_right;
2190 add_symbol = op_symbol;
2191 orig_add_symbol = expressionP->X_op_symbol;
2192 op = O_symbol;
2193 break;
2196 else if (op == O_subtract)
2198 if (seg_right == absolute_section)
2200 final_val -= right;
2201 op = O_symbol;
2202 break;
2206 /* Equality and non-equality tests are permitted on anything.
2207 Subtraction, and other comparison operators are permitted if
2208 both operands are in the same section.
2209 Shifts by constant zero are permitted on anything.
2210 Multiplies, bit-ors, and bit-ands with constant zero are
2211 permitted on anything.
2212 Multiplies and divides by constant one are permitted on
2213 anything.
2214 Binary operations with both operands being the same register
2215 or undefined symbol are permitted if the result doesn't depend
2216 on the input value.
2217 Otherwise, both operands must be absolute. We already handled
2218 the case of addition or subtraction of a constant above. */
2219 frag_off = 0;
2220 if (!(seg_left == absolute_section
2221 && seg_right == absolute_section)
2222 && !(op == O_eq || op == O_ne)
2223 && !((op == O_subtract
2224 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2225 && seg_left == seg_right
2226 && (finalize_syms
2227 || frag_offset_fixed_p (frag_left, frag_right, &frag_off)
2228 || (op == O_gt
2229 && frag_gtoffset_p (left, frag_left,
2230 right, frag_right, &frag_off)))
2231 && (seg_left != reg_section || left == right)
2232 && (seg_left != undefined_section || add_symbol == op_symbol)))
2234 if ((seg_left == absolute_section && left == 0)
2235 || (seg_right == absolute_section && right == 0))
2237 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2239 if (!(seg_right == absolute_section && right == 0))
2241 seg_left = seg_right;
2242 left = right;
2243 add_symbol = op_symbol;
2244 orig_add_symbol = expressionP->X_op_symbol;
2246 op = O_symbol;
2247 break;
2249 else if (op == O_left_shift || op == O_right_shift)
2251 if (!(seg_left == absolute_section && left == 0))
2253 op = O_symbol;
2254 break;
2257 else if (op != O_multiply
2258 && op != O_bit_or_not && op != O_bit_and)
2259 return 0;
2261 else if (op == O_multiply
2262 && seg_left == absolute_section && left == 1)
2264 seg_left = seg_right;
2265 left = right;
2266 add_symbol = op_symbol;
2267 orig_add_symbol = expressionP->X_op_symbol;
2268 op = O_symbol;
2269 break;
2271 else if ((op == O_multiply || op == O_divide)
2272 && seg_right == absolute_section && right == 1)
2274 op = O_symbol;
2275 break;
2277 else if (!(left == right
2278 && ((seg_left == reg_section && seg_right == reg_section)
2279 || (seg_left == undefined_section
2280 && seg_right == undefined_section
2281 && add_symbol == op_symbol))))
2282 return 0;
2283 else if (op == O_bit_and || op == O_bit_inclusive_or)
2285 op = O_symbol;
2286 break;
2288 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2289 return 0;
2292 right += frag_off / OCTETS_PER_BYTE;
2293 switch (op)
2295 case O_add: left += right; break;
2296 case O_subtract: left -= right; break;
2297 case O_multiply: left *= right; break;
2298 case O_divide:
2299 if (right == 0)
2300 return 0;
2301 left = (offsetT) left / (offsetT) right;
2302 break;
2303 case O_modulus:
2304 if (right == 0)
2305 return 0;
2306 left = (offsetT) left % (offsetT) right;
2307 break;
2308 case O_left_shift: left <<= right; break;
2309 case O_right_shift: left >>= right; break;
2310 case O_bit_inclusive_or: left |= right; break;
2311 case O_bit_or_not: left |= ~right; break;
2312 case O_bit_exclusive_or: left ^= right; break;
2313 case O_bit_and: left &= right; break;
2314 case O_eq:
2315 case O_ne:
2316 left = (left == right
2317 && seg_left == seg_right
2318 && (finalize_syms || frag_left == frag_right)
2319 && (seg_left != undefined_section
2320 || add_symbol == op_symbol)
2321 ? ~ (valueT) 0 : 0);
2322 if (op == O_ne)
2323 left = ~left;
2324 break;
2325 case O_lt:
2326 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2327 break;
2328 case O_le:
2329 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2330 break;
2331 case O_ge:
2332 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2333 break;
2334 case O_gt:
2335 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2336 break;
2337 case O_logical_and: left = left && right; break;
2338 case O_logical_or: left = left || right; break;
2339 default: abort ();
2342 op = O_constant;
2343 break;
2346 if (op == O_symbol)
2348 if (seg_left == absolute_section)
2349 op = O_constant;
2350 else if (seg_left == reg_section && final_val == 0)
2351 op = O_register;
2352 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2353 final_val += left;
2354 expressionP->X_add_symbol = add_symbol;
2356 expressionP->X_op = op;
2358 if (op == O_constant || op == O_register)
2359 final_val += left;
2360 expressionP->X_add_number = final_val;
2362 return 1;
2365 /* This lives here because it belongs equally in expr.c & read.c.
2366 expr.c is just a branch office read.c anyway, and putting it
2367 here lessens the crowd at read.c.
2369 Assume input_line_pointer is at start of symbol name, or the
2370 start of a double quote enclosed symbol name.
2371 Advance input_line_pointer past symbol name.
2372 Turn that character into a '\0', returning its former value,
2373 which may be the closing double quote.
2374 This allows a string compare (RMS wants symbol names to be strings)
2375 of the symbol name.
2376 There will always be a char following symbol name, because all good
2377 lines end in end-of-line. */
2379 char
2380 get_symbol_name (char ** ilp_return)
2382 char c;
2384 * ilp_return = input_line_pointer;
2385 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2386 constructed string. */
2387 if (is_name_beginner (c = *input_line_pointer++)
2388 || (input_from_string && c == FAKE_LABEL_CHAR))
2390 while (is_part_of_name (c = *input_line_pointer++)
2391 || (input_from_string && c == FAKE_LABEL_CHAR))
2393 if (is_name_ender (c))
2394 c = *input_line_pointer++;
2396 else if (c == '"')
2398 bool backslash_seen;
2400 * ilp_return = input_line_pointer;
2403 backslash_seen = c == '\\';
2404 c = * input_line_pointer ++;
2406 while (c != 0 && (c != '"' || backslash_seen));
2408 if (c == 0)
2409 as_warn (_("missing closing '\"'"));
2411 *--input_line_pointer = 0;
2412 return c;
2415 /* Replace the NUL character pointed to by input_line_pointer
2416 with C. If C is \" then advance past it. Return the character
2417 now pointed to by input_line_pointer. */
2419 char
2420 restore_line_pointer (char c)
2422 * input_line_pointer = c;
2423 if (c == '"')
2424 c = * ++ input_line_pointer;
2425 return c;
2428 unsigned int
2429 get_single_number (void)
2431 expressionS exp;
2432 operand (&exp, expr_normal);
2433 return exp.X_add_number;