1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2022 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)
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
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))
29 #include "safe-ctype.h"
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;
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. */
57 make_expr_symbol (expressionS *expressionP)
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"));
75 as_bad (_("floating point number invalid"));
76 zero.X_op = O_constant;
77 zero.X_add_number = 0;
80 clean_up_expression (&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
88 symbolP = symbol_create (FAKE_LABEL_NAME,
89 (expressionP->X_op == O_constant
91 : expressionP->X_op == O_register
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);
102 n->file = as_where (&n->line);
103 n->next = expr_symbol_lines;
104 expr_symbol_lines = n;
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
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)
131 /* Look up a previously used .startof. / .sizeof. symbol, or make a fresh
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);
143 for (i = 0; i < nr_seen[start]; ++i)
145 symbolP = seen[start][i];
150 name = S_GET_NAME (symbolP);
151 if ((symbols_case_sensitive
152 ? strcasecmp (buf, name)
153 : strcmp (buf, name)) == 0)
160 symbolP = symbol_make (buf);
163 if (i >= nr_seen[start])
165 unsigned int nr = (i + 1) * 2;
167 seen[start] = XRESIZEVEC (symbolS *, seen[start], nr);
169 memset (&seen[start][i + 1], 0, (nr - i - 1) * sizeof(seen[0][0]));
172 seen[start][i] = 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. */
190 expr_build_uconstant (offsetT value)
195 e.X_add_number = value;
198 return make_expr_symbol (&e);
201 /* Build an expression for the current location ('.'). */
204 expr_build_dot (void)
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) */
232 floating_constant (expressionS *expressionP)
234 /* input_line_pointer -> floating-point constant. */
237 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
238 &generic_floating_point_number);
242 if (error_code == ERROR_EXPONENT_OVERFLOW)
244 as_bad (_("bad floating-point constant: exponent overflow"));
248 as_bad (_("bad floating-point constant: unknown error code=%d"),
252 expressionP->X_op = O_big;
253 /* input_line_pointer -> just after constant, which may point to
255 expressionP->X_add_number = -1;
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));
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));
279 integer_constant (int radix, expressionS *expressionP)
281 char *start; /* Start of number. */
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. */
313 #else /* includes non-bfd case, mostly */
317 if (is_end_of_line[(unsigned char) *input_line_pointer])
319 expressionP->X_op = O_absent;
323 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
327 /* In MRI mode, the number may have a suffix indicating the
328 radix. For that matter, it might actually be a floating
330 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
332 if (*suffix == 'e' || *suffix == 'E')
336 if (suffix == input_line_pointer)
345 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
346 we distinguish between 'B' and 'b'. This is the case for
348 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
352 else if (c == 'O' || c == 'Q')
356 else if (suffix[1] == '.' || c == 'E' || flt)
358 floating_constant (expressionP);
373 too_many_digits = valuesize + 1;
377 too_many_digits = (valuesize + 2) / 3 + 1;
381 too_many_digits = (valuesize + 3) / 4 + 1;
385 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
388 start = input_line_pointer;
389 c = *input_line_pointer++;
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;
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. */
417 for (c = *input_line_pointer++;
418 (digit = hex_value (c)) < maxdig;
419 c = *(input_line_pointer++))
421 number = number * radix + digit;
425 /* Check for 8 digit per word max. */
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);
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)
458 if (num_little_digits <= 2)
460 /* will fit into 32 bits. */
461 number = generic_bignum_to_int32 ();
465 else if (num_little_digits <= 4)
467 /* Will fit into 64 bits. */
468 number = generic_bignum_to_int64 ();
476 /* Number of littlenums in the bignum. */
477 number = num_little_digits;
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. */
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++)
500 work = carry + radix * *pointer;
501 *pointer = work & LITTLENUM_MASK;
502 carry = work >> LITTLENUM_NUMBER_OF_BITS;
506 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
508 /* Room to grow a longer bignum. */
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 ();
523 else if (leader < generic_bignum + 4)
525 /* Will fit into 64 bits. */
526 number = generic_bignum_to_int64 ();
532 /* Number of littlenums in the bignum. */
533 number = leader - generic_bignum + 1;
537 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
539 && input_line_pointer - 1 == suffix)
540 c = *input_line_pointer++;
542 #ifndef tc_allow_U_suffix
543 #define tc_allow_U_suffix 1
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
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++;
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
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 (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;
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:\""),
590 expressionP->X_op = O_constant;
593 expressionP->X_add_number = 0;
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
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 (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);
612 expressionP->X_op = O_symbol;
613 expressionP->X_add_symbol = symbolP;
614 expressionP->X_add_number = 0;
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
623 if (dollar_label_defined (number))
625 name = dollar_label_name (number, 0);
626 symbolP = symbol_find (name);
627 know (symbolP != NULL);
631 name = dollar_label_name (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;
641 expressionP->X_op = O_constant;
642 expressionP->X_add_number = number;
643 input_line_pointer--; /* Restore following character. */
644 } /* Really just a number. */
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. */
658 mri_char_constant (expressionS *expressionP)
662 if (*input_line_pointer == '\''
663 && input_line_pointer[1] != '\'')
665 expressionP->X_op = O_constant;
666 expressionP->X_add_number = 0;
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--)
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] != '\'')
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] != '\'')
706 as_bad (_("character constant too large"));
715 c = SIZE_OF_LARGE_NUMBER - i;
716 for (j = 0; j < c; j++)
717 generic_bignum[j] = generic_bignum[i + j];
721 know (LITTLENUM_NUMBER_OF_BITS == 16);
724 expressionP->X_op = O_big;
725 expressionP->X_add_number = i;
729 expressionP->X_op = O_constant;
731 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
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 `.'. */
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;
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
766 The operand may have been empty: in this case X_op == O_absent.
767 Input_line_pointer->(next non-blank) char after operand. */
770 operand (expressionS *expressionP, enum expr_mode mode)
773 symbolS *symbolP; /* Points to symbol. */
774 char *name; /* Points to name of symbol. */
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])
805 input_line_pointer--;
807 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
812 #ifdef LITERAL_PREFIXPERCENT_BIN
814 integer_constant (2, expressionP);
819 /* Non-decimal radix. */
821 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
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);
835 c = *input_line_pointer;
844 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
846 integer_constant (0, expressionP);
852 if (c && strchr (FLT_CHARS, c))
854 input_line_pointer++;
855 floating_constant (expressionP);
856 expressionP->X_add_number = - TOLOWER (c);
860 /* The string was only zero. */
861 expressionP->X_op = O_constant;
862 expressionP->X_add_number = 0;
871 input_line_pointer++;
872 integer_constant (16, expressionP);
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);
885 /* Otherwise, parse this as a binary number. */
888 if (input_line_pointer[1] == '0'
889 || input_line_pointer[1] == '1')
891 input_line_pointer++;
892 integer_constant (2, expressionP);
895 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
896 input_line_pointer++;
907 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
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
930 is_label = (cp == input_line_pointer + 1
931 || (cp == input_line_pointer + 2
932 && (cp[-1] == '-' || cp[-1] == '+'))
938 input_line_pointer--;
939 integer_constant (10, expressionP);
947 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
949 integer_constant (0, expressionP);
959 input_line_pointer++;
960 floating_constant (expressionP);
961 expressionP->X_add_number = - TOLOWER (c);
965 if (LOCAL_LABELS_DOLLAR)
967 integer_constant (10, expressionP);
976 #ifndef NEED_INDEX_OPERATOR
978 # ifdef md_need_index_operator
979 if (md_need_index_operator())
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 == '(' ? ')' : ']');
995 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
998 input_line_pointer++;
1000 /* Here with input_line_pointer -> char after "(...)". */
1005 if (! flag_m68k_mri || *input_line_pointer != '\'')
1007 as_bad (_("EBCDIC constants are not supported"));
1010 if (! flag_m68k_mri || *input_line_pointer != '\'')
1012 ++input_line_pointer;
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++;
1027 mri_char_constant (expressionP);
1032 /* Double quote is the bitwise not operator in MRI mode. */
1033 if (! flag_m68k_mri)
1038 /* '~' is permitted to start a label on the Delta. */
1039 if (is_name_beginner (c))
1049 operand (expressionP, mode);
1050 if (expressionP->X_op == O_constant)
1052 /* input_line_pointer -> char after operand. */
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;
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
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 = '-';
1086 generic_floating_point_number.sign = 'N';
1088 else if (expressionP->X_op == O_big
1089 && expressionP->X_add_number > 0)
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;
1107 for (i = 0; i < expressionP->X_add_number; ++i)
1109 generic_bignum[i] += 1;
1110 if (generic_bignum[i])
1116 for (i = 0; i < expressionP->X_add_number; ++i)
1117 if (generic_bignum[i] != 0)
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)
1130 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1132 expressionP->X_op = O_uminus;
1133 else if (c == '~' || c == '"')
1134 expressionP->X_op = O_bit_not;
1136 expressionP->X_op = O_logical_not;
1137 expressionP->X_add_number = 0;
1141 as_warn (_("Unary operator %c ignored because bad operand follows"),
1146 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
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')
1153 integer_constant (16, expressionP);
1162 /* '$' is the program counter when in MRI mode, or when
1163 DOLLAR_DOT is defined. */
1165 if (! flag_m68k_mri)
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);
1176 if (is_part_of_name (*input_line_pointer))
1179 current_location (expressionP);
1184 if (!is_part_of_name (*input_line_pointer))
1186 current_location (expressionP);
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])))
1196 start = (input_line_pointer[1] == 't'
1197 || input_line_pointer[1] == 'T');
1198 input_line_pointer += start ? 8 : 7;
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."));
1208 ++input_line_pointer;
1210 c = get_symbol_name (& name);
1213 as_bad (_("expected symbol name"));
1214 (void) restore_line_pointer (c);
1216 ++input_line_pointer;
1220 expressionP->X_op = O_symbol;
1221 expressionP->X_add_symbol = symbol_lookup_or_make (name, start);
1222 expressionP->X_add_number = 0;
1224 *input_line_pointer = c;
1225 SKIP_WHITESPACE_AFTER_NAME ();
1226 if (*input_line_pointer != ')')
1227 as_bad (_("syntax error in .startof. or .sizeof."));
1229 ++input_line_pointer;
1240 /* Can't imagine any other kind of operand. */
1241 expressionP->X_op = O_absent;
1242 input_line_pointer--;
1247 if (! flag_m68k_mri)
1249 integer_constant (2, expressionP);
1253 if (! flag_m68k_mri)
1255 integer_constant (8, expressionP);
1259 if (! flag_m68k_mri)
1262 /* In MRI mode, this is a floating point constant represented
1263 using hexadecimal digits. */
1265 ++input_line_pointer;
1266 integer_constant (16, expressionP);
1270 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1273 current_location (expressionP);
1278 #if defined(md_need_index_operator) || defined(TC_M68K)
1281 if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
1283 /* Identifier begins here.
1284 This is kludged for speed, so code is repeated. */
1286 -- input_line_pointer;
1287 c = get_symbol_name (&name);
1291 operatorT op = md_operator (name, 1, &c);
1296 restore_line_pointer (c);
1300 restore_line_pointer (c);
1304 restore_line_pointer (c);
1308 as_bad (_("invalid use of operator \"%s\""), name);
1314 if (op != O_absent && op != O_illegal)
1316 restore_line_pointer (c);
1317 expr (9, expressionP, mode);
1318 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1319 expressionP->X_op_symbol = NULL;
1320 expressionP->X_add_number = 0;
1321 expressionP->X_op = op;
1327 #ifdef md_parse_name
1328 /* This is a hook for the backend to parse certain names
1329 specially in certain contexts. If a name always has a
1330 specific value, it can often be handled by simply
1331 entering it in the symbol table. */
1332 if (md_parse_name (name, expressionP, mode, &c))
1334 restore_line_pointer (c);
1339 symbolP = symbol_find_or_make (name);
1341 /* If we have an absolute symbol or a reg, then we know its
1343 segment = S_GET_SEGMENT (symbolP);
1344 if (mode != expr_defer
1345 && segment == absolute_section
1346 && !S_FORCE_RELOC (symbolP, 0))
1348 expressionP->X_op = O_constant;
1349 expressionP->X_add_number = S_GET_VALUE (symbolP);
1351 else if (mode != expr_defer && segment == reg_section)
1353 expressionP->X_op = O_register;
1354 expressionP->X_add_number = S_GET_VALUE (symbolP);
1358 expressionP->X_op = O_symbol;
1359 expressionP->X_add_symbol = symbolP;
1360 expressionP->X_add_number = 0;
1363 restore_line_pointer (c);
1367 /* Let the target try to parse it. Success is indicated by changing
1368 the X_op field to something other than O_absent and pointing
1369 input_line_pointer past the expression. If it can't parse the
1370 expression, X_op and input_line_pointer should be unchanged. */
1371 expressionP->X_op = O_absent;
1372 --input_line_pointer;
1373 md_operand (expressionP);
1374 if (expressionP->X_op == O_absent)
1376 ++input_line_pointer;
1377 as_bad (_("bad expression"));
1378 expressionP->X_op = O_constant;
1379 expressionP->X_add_number = 0;
1385 /* It is more 'efficient' to clean up the expressionS when they are
1386 created. Doing it here saves lines of code. */
1387 clean_up_expression (expressionP);
1388 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1389 know (*input_line_pointer != ' ');
1391 /* The PA port needs this information. */
1392 if (expressionP->X_add_symbol)
1393 symbol_mark_used (expressionP->X_add_symbol);
1395 if (mode != expr_defer)
1397 expressionP->X_add_symbol
1398 = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1399 expressionP->X_op_symbol
1400 = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1403 switch (expressionP->X_op)
1406 return absolute_section;
1408 return S_GET_SEGMENT (expressionP->X_add_symbol);
1414 /* Internal. Simplify a struct expression for use by expr (). */
1416 /* In: address of an expressionS.
1417 The X_op field of the expressionS may only take certain values.
1418 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1420 Out: expressionS may have been modified:
1421 Unused fields zeroed to help expr (). */
1424 clean_up_expression (expressionS *expressionP)
1426 switch (expressionP->X_op)
1430 expressionP->X_add_number = 0;
1435 expressionP->X_add_symbol = NULL;
1440 expressionP->X_op_symbol = NULL;
1447 /* Expression parser. */
1449 /* We allow an empty expression, and just assume (absolute,0) silently.
1450 Unary operators and parenthetical expressions are treated as operands.
1451 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1453 We used to do an aho/ullman shift-reduce parser, but the logic got so
1454 warped that I flushed it and wrote a recursive-descent parser instead.
1455 Now things are stable, would anybody like to write a fast parser?
1456 Most expressions are either register (which does not even reach here)
1457 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1458 So I guess it doesn't really matter how inefficient more complex expressions
1461 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1462 Also, we have consumed any leading or trailing spaces (operand does that)
1463 and done all intervening operators.
1465 This returns the segment of the result, which will be
1466 absolute_section or the segment of a symbol. */
1469 #define __ O_illegal
1471 #define O_SINGLE_EQ O_illegal
1474 /* Maps ASCII -> operators. */
1475 static const operatorT op_encoding[256] = {
1476 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1477 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1479 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1480 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1481 __, __, __, __, __, __, __, __,
1482 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1483 __, __, __, __, __, __, __, __,
1484 __, __, __, __, __, __, __, __,
1485 __, __, __, __, __, __, __, __,
1487 #ifdef NEED_INDEX_OPERATOR
1492 __, __, O_bit_exclusive_or, __,
1493 __, __, __, __, __, __, __, __,
1494 __, __, __, __, __, __, __, __,
1495 __, __, __, __, __, __, __, __,
1496 __, __, __, __, O_bit_inclusive_or, __, __, __,
1498 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1499 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1500 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1501 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1502 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1503 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1504 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1505 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1509 0 operand, (expression)
1514 5 used for * / % in MRI mode
1519 static operator_rankT op_rank[O_max] = {
1524 0, /* O_symbol_rva */
1530 9, /* O_logical_not */
1534 8, /* O_left_shift */
1535 8, /* O_right_shift */
1536 7, /* O_bit_inclusive_or */
1537 7, /* O_bit_or_not */
1538 7, /* O_bit_exclusive_or */
1548 3, /* O_logical_and */
1549 2, /* O_logical_or */
1553 /* Unfortunately, in MRI mode for the m68k, multiplication and
1554 division have lower precedence than the bit wise operators. This
1555 function sets the operator precedences correctly for the current
1556 mode. Also, MRI uses a different bit_not operator, and this fixes
1559 #define STANDARD_MUL_PRECEDENCE 8
1560 #define MRI_MUL_PRECEDENCE 6
1563 expr_set_precedence (void)
1567 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1568 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1569 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1573 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1574 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1575 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1580 expr_set_rank (operatorT op, operator_rankT rank)
1582 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1586 /* Initialize the expression parser. */
1591 expr_set_precedence ();
1593 /* Verify that X_op field is wide enough. */
1597 gas_assert (e.X_op == O_max);
1601 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1602 sets NUM_CHARS to the number of characters in the operator.
1603 Does not advance INPUT_LINE_POINTER. */
1605 static inline operatorT
1606 operatorf (int *num_chars)
1611 c = *input_line_pointer & 0xff;
1614 if (is_end_of_line[c])
1618 if (is_name_beginner (c))
1621 char ec = get_symbol_name (& name);
1623 ret = md_operator (name, 2, &ec);
1627 *input_line_pointer = ec;
1628 input_line_pointer = name;
1633 as_bad (_("invalid use of operator \"%s\""), name);
1637 *input_line_pointer = ec;
1638 *num_chars = input_line_pointer - name;
1639 input_line_pointer = name;
1648 ret = op_encoding[c];
1650 if (ret == O_illegal)
1652 char *start = input_line_pointer;
1654 ret = md_operator (NULL, 2, NULL);
1655 if (ret != O_illegal)
1656 *num_chars = input_line_pointer - start;
1657 input_line_pointer = start;
1664 return op_encoding[c];
1667 switch (input_line_pointer[1])
1670 return op_encoding[c];
1685 if (input_line_pointer[1] != '=')
1686 return op_encoding[c];
1692 switch (input_line_pointer[1])
1695 return op_encoding[c];
1697 ret = O_right_shift;
1707 switch (input_line_pointer[1])
1710 /* We accept !! as equivalent to ^ for MRI compatibility. */
1712 return O_bit_exclusive_or;
1714 /* We accept != as equivalent to <>. */
1719 return O_bit_inclusive_or;
1720 return op_encoding[c];
1724 if (input_line_pointer[1] != '|')
1725 return op_encoding[c];
1728 return O_logical_or;
1731 if (input_line_pointer[1] != '&')
1732 return op_encoding[c];
1735 return O_logical_and;
1741 /* Implement "word-size + 1 bit" addition for
1742 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1743 is used so that the full range of unsigned word values and the full range of
1744 signed word values can be represented in an O_constant expression, which is
1745 useful e.g. for .sleb128 directives. */
1748 add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1750 valueT ures = resultP->X_add_number;
1751 valueT uamount = amount;
1753 resultP->X_add_number += uamount;
1755 resultP->X_extrabit ^= rhs_highbit;
1757 if (ures + uamount < ures)
1758 resultP->X_extrabit ^= 1;
1761 /* Similarly, for subtraction. */
1764 subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1766 valueT ures = resultP->X_add_number;
1767 valueT uamount = amount;
1769 resultP->X_add_number -= uamount;
1771 resultP->X_extrabit ^= rhs_highbit;
1774 resultP->X_extrabit ^= 1;
1777 /* Parse an expression. */
1780 expr (int rankarg, /* Larger # is higher rank. */
1781 expressionS *resultP, /* Deliver result here. */
1782 enum expr_mode mode /* Controls behavior. */)
1784 operator_rankT rank = (operator_rankT) rankarg;
1791 know (rankarg >= 0);
1793 /* Save the value of dot for the fixup code. */
1796 dot_value = frag_now_fix ();
1797 dot_frag = frag_now;
1800 retval = operand (resultP, mode);
1802 /* operand () gobbles spaces. */
1803 know (*input_line_pointer != ' ');
1805 op_left = operatorf (&op_chars);
1806 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1811 input_line_pointer += op_chars; /* -> after operator. */
1814 rightseg = expr (op_rank[(int) op_left], &right, mode);
1815 if (right.X_op == O_absent)
1817 as_warn (_("missing operand; zero assumed"));
1818 right.X_op = O_constant;
1819 right.X_add_number = 0;
1820 right.X_add_symbol = NULL;
1821 right.X_op_symbol = NULL;
1824 know (*input_line_pointer != ' ');
1826 if (op_left == O_index)
1828 if (*input_line_pointer != ']')
1829 as_bad ("missing right bracket");
1832 ++input_line_pointer;
1837 op_right = operatorf (&op_chars);
1839 know (op_right == O_illegal || op_left == O_index
1840 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1841 know ((int) op_left >= (int) O_multiply);
1843 know ((int) op_left <= (int) O_index);
1845 know ((int) op_left < (int) O_max);
1848 /* input_line_pointer->after right-hand quantity. */
1849 /* left-hand quantity in resultP. */
1850 /* right-hand quantity in right. */
1851 /* operator in op_left. */
1853 if (resultP->X_op == O_big)
1855 if (resultP->X_add_number > 0)
1856 as_warn (_("left operand is a bignum; integer 0 assumed"));
1858 as_warn (_("left operand is a float; integer 0 assumed"));
1859 resultP->X_op = O_constant;
1860 resultP->X_add_number = 0;
1861 resultP->X_add_symbol = NULL;
1862 resultP->X_op_symbol = NULL;
1864 if (right.X_op == O_big)
1866 if (right.X_add_number > 0)
1867 as_warn (_("right operand is a bignum; integer 0 assumed"));
1869 as_warn (_("right operand is a float; integer 0 assumed"));
1870 right.X_op = O_constant;
1871 right.X_add_number = 0;
1872 right.X_add_symbol = NULL;
1873 right.X_op_symbol = NULL;
1876 if (mode == expr_defer
1877 && ((resultP->X_add_symbol != NULL
1878 && S_IS_FORWARD_REF (resultP->X_add_symbol))
1879 || (right.X_add_symbol != NULL
1880 && S_IS_FORWARD_REF (right.X_add_symbol))))
1883 /* Optimize common cases. */
1884 #ifdef md_optimize_expr
1885 if (md_optimize_expr (resultP, op_left, &right))
1892 #ifndef md_register_arithmetic
1893 # define md_register_arithmetic 1
1895 if (op_left == O_add && right.X_op == O_constant
1896 && (md_register_arithmetic || resultP->X_op != O_register))
1899 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1901 /* This case comes up in PIC code. */
1902 else if (op_left == O_subtract
1903 && right.X_op == O_symbol
1904 && resultP->X_op == O_symbol
1905 && retval == rightseg
1906 #ifdef md_allow_local_subtract
1907 && md_allow_local_subtract (resultP, & right, rightseg)
1909 && ((SEG_NORMAL (rightseg)
1910 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1911 && !S_FORCE_RELOC (right.X_add_symbol, 0))
1912 || right.X_add_symbol == resultP->X_add_symbol)
1913 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1914 symbol_get_frag (right.X_add_symbol),
1917 offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1918 - S_GET_VALUE (right.X_add_symbol);
1919 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1920 subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1921 add_to_result (resultP, symval_diff, symval_diff < 0);
1922 resultP->X_op = O_constant;
1923 resultP->X_add_symbol = 0;
1925 else if (op_left == O_subtract && right.X_op == O_constant
1926 && (md_register_arithmetic || resultP->X_op != O_register))
1929 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1931 else if (op_left == O_add && resultP->X_op == O_constant
1932 && (md_register_arithmetic || right.X_op != O_register))
1935 resultP->X_op = right.X_op;
1936 resultP->X_add_symbol = right.X_add_symbol;
1937 resultP->X_op_symbol = right.X_op_symbol;
1938 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1941 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1943 /* Constant OP constant. */
1944 offsetT v = right.X_add_number;
1945 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1947 as_warn (_("division by zero"));
1950 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1951 && (op_left == O_left_shift || op_left == O_right_shift))
1953 as_warn_value_out_of_range (_("shift count"), v, 0,
1954 sizeof(valueT) * CHAR_BIT - 1,
1956 resultP->X_add_number = v = 0;
1960 default: goto general;
1962 /* Do the multiply as unsigned to silence ubsan. The
1963 result is of course the same when we throw away high
1964 bits of the result. */
1965 resultP->X_add_number *= (valueT) v;
1967 case O_divide: resultP->X_add_number /= v; break;
1968 case O_modulus: resultP->X_add_number %= v; break;
1970 /* We always use unsigned shifts. According to the ISO
1971 C standard, left shift of a signed type having a
1972 negative value is undefined behaviour, and right
1973 shift of a signed type having negative value is
1974 implementation defined. Left shift of a signed type
1975 when the result overflows is also undefined
1976 behaviour. So don't trigger ubsan warnings or rely
1977 on characteristics of the compiler. */
1978 resultP->X_add_number
1979 = (valueT) resultP->X_add_number << (valueT) v;
1982 resultP->X_add_number
1983 = (valueT) resultP->X_add_number >> (valueT) v;
1985 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1986 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1987 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1988 case O_bit_and: resultP->X_add_number &= v; break;
1989 /* Constant + constant (O_add) is handled by the
1990 previous if statement for constant + X, so is omitted
1993 subtract_from_result (resultP, v, 0);
1996 resultP->X_add_number =
1997 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
2000 resultP->X_add_number =
2001 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
2004 resultP->X_add_number =
2005 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
2008 resultP->X_add_number =
2009 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
2012 resultP->X_add_number =
2013 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
2016 resultP->X_add_number =
2017 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
2020 resultP->X_add_number = resultP->X_add_number && v;
2023 resultP->X_add_number = resultP->X_add_number || v;
2027 else if (resultP->X_op == O_symbol
2028 && right.X_op == O_symbol
2029 && (op_left == O_add
2030 || op_left == O_subtract
2031 || (resultP->X_add_number == 0
2032 && right.X_add_number == 0)))
2034 /* Symbol OP symbol. */
2035 resultP->X_op = op_left;
2036 resultP->X_op_symbol = right.X_add_symbol;
2037 if (op_left == O_add)
2038 add_to_result (resultP, right.X_add_number, right.X_extrabit);
2039 else if (op_left == O_subtract)
2041 subtract_from_result (resultP, right.X_add_number,
2043 if (retval == rightseg
2044 && SEG_NORMAL (retval)
2045 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2046 && !S_FORCE_RELOC (right.X_add_symbol, 0))
2048 retval = absolute_section;
2049 rightseg = absolute_section;
2056 /* The general case. */
2057 resultP->X_add_symbol = make_expr_symbol (resultP);
2058 resultP->X_op_symbol = make_expr_symbol (&right);
2059 resultP->X_op = op_left;
2060 resultP->X_add_number = 0;
2061 resultP->X_unsigned = 1;
2062 resultP->X_extrabit = 0;
2065 if (retval != rightseg)
2067 if (retval == undefined_section)
2069 else if (rightseg == undefined_section)
2071 else if (retval == expr_section)
2073 else if (rightseg == expr_section)
2075 else if (retval == reg_section)
2077 else if (rightseg == reg_section)
2079 else if (rightseg == absolute_section)
2081 else if (retval == absolute_section)
2084 else if (op_left == O_subtract)
2088 as_bad (_("operation combines symbols in different segments"));
2092 } /* While next operator is >= this rank. */
2094 /* The PA port needs this information. */
2095 if (resultP->X_add_symbol)
2096 symbol_mark_used (resultP->X_add_symbol);
2098 if (rank == 0 && mode == expr_evaluate)
2099 resolve_expression (resultP);
2101 return resultP->X_op == O_constant ? absolute_section : retval;
2104 /* Resolve an expression without changing any symbols/sub-expressions
2108 resolve_expression (expressionS *expressionP)
2110 /* Help out with CSE. */
2111 valueT final_val = expressionP->X_add_number;
2112 symbolS *add_symbol = expressionP->X_add_symbol;
2113 symbolS *orig_add_symbol = add_symbol;
2114 symbolS *op_symbol = expressionP->X_op_symbol;
2115 operatorT op = expressionP->X_op;
2117 segT seg_left, seg_right;
2118 fragS *frag_left, *frag_right;
2133 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2141 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2144 if (seg_left != absolute_section)
2147 if (op == O_logical_not)
2149 else if (op == O_uminus)
2161 case O_bit_inclusive_or:
2163 case O_bit_exclusive_or:
2175 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2176 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2179 /* Simplify addition or subtraction of a constant by folding the
2180 constant into X_add_number. */
2183 if (seg_right == absolute_section)
2189 else if (seg_left == absolute_section)
2193 seg_left = seg_right;
2194 add_symbol = op_symbol;
2195 orig_add_symbol = expressionP->X_op_symbol;
2200 else if (op == O_subtract)
2202 if (seg_right == absolute_section)
2210 /* Equality and non-equality tests are permitted on anything.
2211 Subtraction, and other comparison operators are permitted if
2212 both operands are in the same section.
2213 Shifts by constant zero are permitted on anything.
2214 Multiplies, bit-ors, and bit-ands with constant zero are
2215 permitted on anything.
2216 Multiplies and divides by constant one are permitted on
2218 Binary operations with both operands being the same register
2219 or undefined symbol are permitted if the result doesn't depend
2221 Otherwise, both operands must be absolute. We already handled
2222 the case of addition or subtraction of a constant above. */
2224 if (!(seg_left == absolute_section
2225 && seg_right == absolute_section)
2226 && !(op == O_eq || op == O_ne)
2227 && !((op == O_subtract
2228 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2229 && seg_left == seg_right
2231 || frag_offset_fixed_p (frag_left, frag_right, &frag_off)
2233 && frag_gtoffset_p (left, frag_left,
2234 right, frag_right, &frag_off)))
2235 && (seg_left != reg_section || left == right)
2236 && (seg_left != undefined_section || add_symbol == op_symbol)))
2238 if ((seg_left == absolute_section && left == 0)
2239 || (seg_right == absolute_section && right == 0))
2241 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2243 if (!(seg_right == absolute_section && right == 0))
2245 seg_left = seg_right;
2247 add_symbol = op_symbol;
2248 orig_add_symbol = expressionP->X_op_symbol;
2253 else if (op == O_left_shift || op == O_right_shift)
2255 if (!(seg_left == absolute_section && left == 0))
2261 else if (op != O_multiply
2262 && op != O_bit_or_not && op != O_bit_and)
2265 else if (op == O_multiply
2266 && seg_left == absolute_section && left == 1)
2268 seg_left = seg_right;
2270 add_symbol = op_symbol;
2271 orig_add_symbol = expressionP->X_op_symbol;
2275 else if ((op == O_multiply || op == O_divide)
2276 && seg_right == absolute_section && right == 1)
2281 else if (!(left == right
2282 && ((seg_left == reg_section && seg_right == reg_section)
2283 || (seg_left == undefined_section
2284 && seg_right == undefined_section
2285 && add_symbol == op_symbol))))
2287 else if (op == O_bit_and || op == O_bit_inclusive_or)
2292 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2296 right += frag_off / OCTETS_PER_BYTE;
2299 case O_add: left += right; break;
2300 case O_subtract: left -= right; break;
2301 case O_multiply: left *= right; break;
2305 left = (offsetT) left / (offsetT) right;
2310 left = (offsetT) left % (offsetT) right;
2312 case O_left_shift: left <<= right; break;
2313 case O_right_shift: left >>= right; break;
2314 case O_bit_inclusive_or: left |= right; break;
2315 case O_bit_or_not: left |= ~right; break;
2316 case O_bit_exclusive_or: left ^= right; break;
2317 case O_bit_and: left &= right; break;
2320 left = (left == right
2321 && seg_left == seg_right
2322 && (finalize_syms || frag_left == frag_right)
2323 && (seg_left != undefined_section
2324 || add_symbol == op_symbol)
2325 ? ~ (valueT) 0 : 0);
2330 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2333 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2336 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2339 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2341 case O_logical_and: left = left && right; break;
2342 case O_logical_or: left = left || right; break;
2352 if (seg_left == absolute_section)
2354 else if (seg_left == reg_section && final_val == 0)
2356 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2358 expressionP->X_add_symbol = add_symbol;
2360 expressionP->X_op = op;
2362 if (op == O_constant || op == O_register)
2364 expressionP->X_add_number = final_val;
2369 /* This lives here because it belongs equally in expr.c & read.c.
2370 expr.c is just a branch office read.c anyway, and putting it
2371 here lessens the crowd at read.c.
2373 Assume input_line_pointer is at start of symbol name, or the
2374 start of a double quote enclosed symbol name.
2375 Advance input_line_pointer past symbol name.
2376 Turn that character into a '\0', returning its former value,
2377 which may be the closing double quote.
2378 This allows a string compare (RMS wants symbol names to be strings)
2380 There will always be a char following symbol name, because all good
2381 lines end in end-of-line. */
2384 get_symbol_name (char ** ilp_return)
2388 * ilp_return = input_line_pointer;
2389 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2390 constructed string. */
2391 if (is_name_beginner (c = *input_line_pointer++)
2392 || (input_from_string && c == FAKE_LABEL_CHAR))
2394 while (is_part_of_name (c = *input_line_pointer++)
2395 || (input_from_string && c == FAKE_LABEL_CHAR))
2397 if (is_name_ender (c))
2398 c = *input_line_pointer++;
2402 char *dst = input_line_pointer;
2404 * ilp_return = input_line_pointer;
2407 c = *input_line_pointer++;
2411 as_warn (_("missing closing '\"'"));
2417 char *ilp_save = input_line_pointer;
2420 if (*input_line_pointer == '"')
2422 ++input_line_pointer;
2425 input_line_pointer = ilp_save;
2430 switch (*input_line_pointer)
2434 c = *input_line_pointer++;
2439 as_warn (_("'\\%c' in quoted symbol name; "
2440 "behavior may change in the future"),
2441 *input_line_pointer);
2449 *--input_line_pointer = 0;
2453 /* Replace the NUL character pointed to by input_line_pointer
2454 with C. If C is \" then advance past it. Return the character
2455 now pointed to by input_line_pointer. */
2458 restore_line_pointer (char c)
2460 * input_line_pointer = c;
2462 c = * ++ input_line_pointer;
2467 get_single_number (void)
2470 operand (&exp, expr_normal);
2471 return exp.X_add_number;