1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2023 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
= notes_alloc (sizeof (*n
));
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
133 static symbolS
**seen
[2];
134 static unsigned int nr_seen
[2];
137 symbol_lookup_or_make (const char *name
, bool start
)
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
153 : strcasecmp (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 expressionP
->X_op
= O_symbol
;
577 expressionP
->X_add_symbol
= symbolP
;
581 /* Either not seen or not defined. */
582 /* @@ Should print out the original string instead of
583 the parsed number. */
584 as_bad (_("backward ref to unknown label \"%d:\""),
586 expressionP
->X_op
= O_constant
;
589 expressionP
->X_add_number
= 0;
591 else if (LOCAL_LABELS_FB
&& c
== 'f')
593 /* Forward reference. Expect symbol to be undefined or
594 unknown. undefined: seen it before. unknown: never seen
597 Construct a local label name, then an undefined symbol.
598 Don't create a xseg frag for it: caller may do that.
599 Just return it as never seen before. */
600 name
= fb_label_name (number
, 1);
601 symbolP
= symbol_find_or_make (name
);
602 /* We have no need to check symbol properties. */
603 expressionP
->X_op
= O_symbol
;
604 expressionP
->X_add_symbol
= symbolP
;
605 expressionP
->X_add_number
= 0;
607 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
609 /* If the dollar label is *currently* defined, then this is just
610 another reference to it. If it is not *currently* defined,
611 then this is a fresh instantiation of that number, so create
614 if (dollar_label_defined (number
))
616 name
= dollar_label_name (number
, 0);
617 symbolP
= symbol_find (name
);
618 know (symbolP
!= NULL
);
622 name
= dollar_label_name (number
, 1);
623 symbolP
= symbol_find_or_make (name
);
626 expressionP
->X_op
= O_symbol
;
627 expressionP
->X_add_symbol
= symbolP
;
628 expressionP
->X_add_number
= 0;
632 expressionP
->X_op
= O_constant
;
633 expressionP
->X_add_number
= number
;
634 input_line_pointer
--; /* Restore following character. */
635 } /* Really just a number. */
639 /* Not a small number. */
640 expressionP
->X_op
= O_big
;
641 expressionP
->X_add_number
= number
; /* Number of littlenums. */
642 input_line_pointer
--; /* -> char following number. */
646 /* Parse an MRI multi character constant. */
649 mri_char_constant (expressionS
*expressionP
)
653 if (*input_line_pointer
== '\''
654 && input_line_pointer
[1] != '\'')
656 expressionP
->X_op
= O_constant
;
657 expressionP
->X_add_number
= 0;
661 /* In order to get the correct byte ordering, we must build the
662 number in reverse. */
663 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
667 generic_bignum
[i
] = 0;
668 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
670 if (*input_line_pointer
== '\'')
672 if (input_line_pointer
[1] != '\'')
674 ++input_line_pointer
;
676 generic_bignum
[i
] <<= 8;
677 generic_bignum
[i
] += *input_line_pointer
;
678 ++input_line_pointer
;
681 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
683 /* If there is more than one littlenum, left justify the
684 last one to make it match the earlier ones. If there is
685 only one, we can just use the value directly. */
686 for (; j
< CHARS_PER_LITTLENUM
; j
++)
687 generic_bignum
[i
] <<= 8;
690 if (*input_line_pointer
== '\''
691 && input_line_pointer
[1] != '\'')
697 as_bad (_("character constant too large"));
706 c
= SIZE_OF_LARGE_NUMBER
- i
;
707 for (j
= 0; j
< c
; j
++)
708 generic_bignum
[j
] = generic_bignum
[i
+ j
];
712 know (LITTLENUM_NUMBER_OF_BITS
== 16);
715 expressionP
->X_op
= O_big
;
716 expressionP
->X_add_number
= i
;
720 expressionP
->X_op
= O_constant
;
722 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
724 expressionP
->X_add_number
=
725 (((generic_bignum
[1] & LITTLENUM_MASK
)
726 << LITTLENUM_NUMBER_OF_BITS
)
727 | (generic_bignum
[0] & LITTLENUM_MASK
));
730 /* Skip the final closing quote. */
731 ++input_line_pointer
;
734 /* Return an expression representing the current location. This
735 handles the magic symbol `.'. */
738 current_location (expressionS
*expressionp
)
740 if (now_seg
== absolute_section
)
742 expressionp
->X_op
= O_constant
;
743 expressionp
->X_add_number
= abs_section_offset
;
747 expressionp
->X_op
= O_symbol
;
748 expressionp
->X_add_symbol
= &dot_symbol
;
749 expressionp
->X_add_number
= 0;
753 /* In: Input_line_pointer points to 1st char of operand, which may
757 The operand may have been empty: in this case X_op == O_absent.
758 Input_line_pointer->(next non-blank) char after operand. */
761 operand (expressionS
*expressionP
, enum expr_mode mode
)
764 symbolS
*symbolP
; /* Points to symbol. */
765 char *name
; /* Points to name of symbol. */
768 /* All integers are regarded as unsigned unless they are negated.
769 This is because the only thing which cares whether a number is
770 unsigned is the code in emit_expr which extends constants into
771 bignums. It should only sign extend negative numbers, so that
772 something like ``.quad 0x80000000'' is not sign extended even
773 though it appears negative if valueT is 32 bits. */
774 expressionP
->X_unsigned
= 1;
775 expressionP
->X_extrabit
= 0;
777 /* Digits, assume it is a bignum. */
779 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
780 c
= *input_line_pointer
++; /* input_line_pointer -> past char in c. */
782 if (is_end_of_line
[(unsigned char) c
])
796 input_line_pointer
--;
798 integer_constant ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
803 #ifdef LITERAL_PREFIXPERCENT_BIN
805 integer_constant (2, expressionP
);
810 /* Non-decimal radix. */
812 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
816 /* Check for a hex or float constant. */
817 for (s
= input_line_pointer
; hex_p (*s
); s
++)
819 if (*s
== 'h' || *s
== 'H' || *input_line_pointer
== '.')
821 --input_line_pointer
;
822 integer_constant (0, expressionP
);
826 c
= *input_line_pointer
;
835 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
837 integer_constant (0, expressionP
);
843 if (c
&& strchr (FLT_CHARS
, c
))
845 input_line_pointer
++;
846 floating_constant (expressionP
);
847 expressionP
->X_add_number
= - TOLOWER (c
);
851 /* The string was only zero. */
852 expressionP
->X_op
= O_constant
;
853 expressionP
->X_add_number
= 0;
862 input_line_pointer
++;
863 integer_constant (16, expressionP
);
867 if (LOCAL_LABELS_FB
&& !flag_m68k_mri
868 && input_line_pointer
[1] != '0'
869 && input_line_pointer
[1] != '1')
871 /* Parse this as a back reference to label 0. */
872 input_line_pointer
--;
873 integer_constant (10, expressionP
);
876 /* Otherwise, parse this as a binary number. */
879 if (input_line_pointer
[1] == '0'
880 || input_line_pointer
[1] == '1')
882 input_line_pointer
++;
883 integer_constant (2, expressionP
);
886 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
887 input_line_pointer
++;
898 integer_constant ((flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
908 /* If it says "0f" and it could possibly be a floating point
909 number, make it one. Otherwise, make it a local label,
910 and try to deal with parsing the rest later. */
911 if (!is_end_of_line
[(unsigned char) input_line_pointer
[1]]
912 && strchr (FLT_CHARS
, 'f') != NULL
)
914 char *cp
= input_line_pointer
+ 1;
916 atof_generic (&cp
, ".", EXP_CHARS
,
917 &generic_floating_point_number
);
919 /* Was nothing parsed, or does it look like an
921 is_label
= (cp
== input_line_pointer
+ 1
922 || (cp
== input_line_pointer
+ 2
923 && (cp
[-1] == '-' || cp
[-1] == '+'))
929 input_line_pointer
--;
930 integer_constant (10, expressionP
);
938 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
940 integer_constant (0, expressionP
);
950 input_line_pointer
++;
951 floating_constant (expressionP
);
952 expressionP
->X_add_number
= - TOLOWER (c
);
956 if (LOCAL_LABELS_DOLLAR
)
958 integer_constant (10, expressionP
);
967 #ifndef NEED_INDEX_OPERATOR
969 # ifdef md_need_index_operator
970 if (md_need_index_operator())
976 /* Didn't begin with digit & not a name. */
977 segment
= expr (0, expressionP
, mode
);
978 /* expression () will pass trailing whitespace. */
979 if ((c
== '(' && *input_line_pointer
!= ')')
980 || (c
== '[' && *input_line_pointer
!= ']'))
982 if (* input_line_pointer
)
983 as_bad (_("found '%c', expected: '%c'"),
984 * input_line_pointer
, c
== '(' ? ')' : ']');
986 as_bad (_("missing '%c'"), c
== '(' ? ')' : ']');
989 input_line_pointer
++;
990 SKIP_ALL_WHITESPACE ();
991 /* Here with input_line_pointer -> char after "(...)". */
996 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
998 as_bad (_("EBCDIC constants are not supported"));
1001 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1003 ++input_line_pointer
;
1007 if (! flag_m68k_mri
)
1009 /* Warning: to conform to other people's assemblers NO
1010 ESCAPEMENT is permitted for a single quote. The next
1011 character, parity errors and all, is taken as the value
1012 of the operand. VERY KINKY. */
1013 expressionP
->X_op
= O_constant
;
1014 expressionP
->X_add_number
= *input_line_pointer
++;
1018 mri_char_constant (expressionP
);
1023 /* Double quote is the bitwise not operator in MRI mode. */
1024 if (! flag_m68k_mri
)
1029 /* '~' is permitted to start a label on the Delta. */
1030 if (is_name_beginner (c
))
1040 operand (expressionP
, mode
);
1041 if (expressionP
->X_op
== O_constant
)
1043 /* input_line_pointer -> char after operand. */
1046 expressionP
->X_add_number
1047 = - (addressT
) expressionP
->X_add_number
;
1048 /* Notice: '-' may overflow: no warning is given.
1049 This is compatible with other people's
1050 assemblers. Sigh. */
1051 expressionP
->X_unsigned
= 0;
1052 if (expressionP
->X_add_number
)
1053 expressionP
->X_extrabit
^= 1;
1055 else if (c
== '~' || c
== '"')
1057 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1058 expressionP
->X_extrabit
^= 1;
1062 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1063 expressionP
->X_unsigned
= 1;
1064 expressionP
->X_extrabit
= 0;
1067 else if (expressionP
->X_op
== O_big
1068 && expressionP
->X_add_number
<= 0
1070 && (generic_floating_point_number
.sign
== '+'
1071 || generic_floating_point_number
.sign
== 'P'))
1073 /* Negative flonum (eg, -1.000e0). */
1074 if (generic_floating_point_number
.sign
== '+')
1075 generic_floating_point_number
.sign
= '-';
1077 generic_floating_point_number
.sign
= 'N';
1079 else if (expressionP
->X_op
== O_big
1080 && expressionP
->X_add_number
> 0)
1084 if (c
== '~' || c
== '-')
1086 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1087 generic_bignum
[i
] = ~generic_bignum
[i
];
1089 /* Extend the bignum to at least the size of .octa. */
1090 if (expressionP
->X_add_number
< SIZE_OF_LARGE_NUMBER
)
1092 expressionP
->X_add_number
= SIZE_OF_LARGE_NUMBER
;
1093 for (; i
< expressionP
->X_add_number
; ++i
)
1094 generic_bignum
[i
] = ~(LITTLENUM_TYPE
) 0;
1098 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1100 generic_bignum
[i
] += 1;
1101 if (generic_bignum
[i
])
1107 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1108 if (generic_bignum
[i
] != 0)
1110 expressionP
->X_add_number
= i
>= expressionP
->X_add_number
;
1111 expressionP
->X_op
= O_constant
;
1112 expressionP
->X_unsigned
= 1;
1113 expressionP
->X_extrabit
= 0;
1116 else if (expressionP
->X_op
!= O_illegal
1117 && expressionP
->X_op
!= O_absent
)
1121 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1123 expressionP
->X_op
= O_uminus
;
1124 else if (c
== '~' || c
== '"')
1125 expressionP
->X_op
= O_bit_not
;
1127 expressionP
->X_op
= O_logical_not
;
1128 expressionP
->X_add_number
= 0;
1132 as_warn (_("Unary operator %c ignored because bad operand follows"),
1137 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1139 if (literal_prefix_dollar_hex
)
1141 /* $L is the start of a local label, not a hex constant. */
1142 if (* input_line_pointer
== 'L')
1144 integer_constant (16, expressionP
);
1153 /* '$' is the program counter when in MRI mode, or when
1154 DOLLAR_DOT is defined. */
1156 if (! flag_m68k_mri
)
1159 if (DOLLAR_AMBIGU
&& hex_p (*input_line_pointer
))
1161 /* In MRI mode and on Z80, '$' is also used as the prefix
1162 for a hexadecimal constant. */
1163 integer_constant (16, expressionP
);
1167 if (is_part_of_name (*input_line_pointer
))
1170 current_location (expressionP
);
1175 if (!is_part_of_name (*input_line_pointer
))
1177 current_location (expressionP
);
1180 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1181 && ! is_part_of_name (input_line_pointer
[8]))
1182 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1183 && ! is_part_of_name (input_line_pointer
[7])))
1187 start
= (input_line_pointer
[1] == 't'
1188 || input_line_pointer
[1] == 'T');
1189 input_line_pointer
+= start
? 8 : 7;
1192 /* Cover for the as_bad () invocations below. */
1193 expressionP
->X_op
= O_absent
;
1195 if (*input_line_pointer
!= '(')
1196 as_bad (_("syntax error in .startof. or .sizeof."));
1199 ++input_line_pointer
;
1201 c
= get_symbol_name (& name
);
1204 as_bad (_("expected symbol name"));
1205 (void) restore_line_pointer (c
);
1207 ++input_line_pointer
;
1211 expressionP
->X_op
= O_symbol
;
1212 expressionP
->X_add_symbol
= symbol_lookup_or_make (name
, start
);
1213 expressionP
->X_add_number
= 0;
1215 *input_line_pointer
= c
;
1216 SKIP_WHITESPACE_AFTER_NAME ();
1217 if (*input_line_pointer
!= ')')
1218 as_bad (_("syntax error in .startof. or .sizeof."));
1220 ++input_line_pointer
;
1231 /* Can't imagine any other kind of operand. */
1232 expressionP
->X_op
= O_absent
;
1233 input_line_pointer
--;
1238 if (! flag_m68k_mri
)
1240 integer_constant (2, expressionP
);
1244 if (! flag_m68k_mri
)
1246 integer_constant (8, expressionP
);
1250 if (! flag_m68k_mri
)
1253 /* In MRI mode, this is a floating point constant represented
1254 using hexadecimal digits. */
1256 ++input_line_pointer
;
1257 integer_constant (16, expressionP
);
1261 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1264 current_location (expressionP
);
1269 #if defined(md_need_index_operator) || defined(TC_M68K)
1272 if (is_name_beginner (c
) || c
== '"') /* Here if did not begin with a digit. */
1274 /* Identifier begins here.
1275 This is kludged for speed, so code is repeated. */
1277 -- input_line_pointer
;
1278 c
= get_symbol_name (&name
);
1282 operatorT op
= md_operator (name
, 1, &c
);
1287 restore_line_pointer (c
);
1291 restore_line_pointer (c
);
1295 restore_line_pointer (c
);
1299 as_bad (_("invalid use of operator \"%s\""), name
);
1305 if (op
!= O_absent
&& op
!= O_illegal
)
1307 restore_line_pointer (c
);
1308 expr (9, expressionP
, mode
);
1309 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1310 expressionP
->X_op_symbol
= NULL
;
1311 expressionP
->X_add_number
= 0;
1312 expressionP
->X_op
= op
;
1318 #ifdef md_parse_name
1319 /* This is a hook for the backend to parse certain names
1320 specially in certain contexts. If a name always has a
1321 specific value, it can often be handled by simply
1322 entering it in the symbol table. */
1323 if (md_parse_name (name
, expressionP
, mode
, &c
))
1325 restore_line_pointer (c
);
1330 symbolP
= symbol_find_or_make (name
);
1332 /* If we have an absolute symbol or a reg, then we know its
1334 segment
= S_GET_SEGMENT (symbolP
);
1335 if (mode
!= expr_defer
1336 && segment
== absolute_section
1337 && !S_FORCE_RELOC (symbolP
, 0))
1339 expressionP
->X_op
= O_constant
;
1340 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1342 else if (mode
!= expr_defer
&& segment
== reg_section
)
1344 expressionP
->X_op
= O_register
;
1345 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1349 expressionP
->X_op
= O_symbol
;
1350 expressionP
->X_add_symbol
= symbolP
;
1351 expressionP
->X_add_number
= 0;
1354 restore_line_pointer (c
);
1358 /* Let the target try to parse it. Success is indicated by changing
1359 the X_op field to something other than O_absent and pointing
1360 input_line_pointer past the expression. If it can't parse the
1361 expression, X_op and input_line_pointer should be unchanged. */
1362 expressionP
->X_op
= O_absent
;
1363 --input_line_pointer
;
1364 md_operand (expressionP
);
1365 if (expressionP
->X_op
== O_absent
)
1367 ++input_line_pointer
;
1368 as_bad (_("bad expression"));
1369 expressionP
->X_op
= O_constant
;
1370 expressionP
->X_add_number
= 0;
1376 /* It is more 'efficient' to clean up the expressionS when they are
1377 created. Doing it here saves lines of code. */
1378 clean_up_expression (expressionP
);
1379 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1380 know (*input_line_pointer
!= ' ');
1382 /* The PA port needs this information. */
1383 if (expressionP
->X_add_symbol
)
1384 symbol_mark_used (expressionP
->X_add_symbol
);
1386 if (mode
!= expr_defer
)
1388 expressionP
->X_add_symbol
1389 = symbol_clone_if_forward_ref (expressionP
->X_add_symbol
);
1390 expressionP
->X_op_symbol
1391 = symbol_clone_if_forward_ref (expressionP
->X_op_symbol
);
1394 switch (expressionP
->X_op
)
1397 return absolute_section
;
1399 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1405 /* Internal. Simplify a struct expression for use by expr (). */
1407 /* In: address of an expressionS.
1408 The X_op field of the expressionS may only take certain values.
1409 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1411 Out: expressionS may have been modified:
1412 Unused fields zeroed to help expr (). */
1415 clean_up_expression (expressionS
*expressionP
)
1417 switch (expressionP
->X_op
)
1421 expressionP
->X_add_number
= 0;
1426 expressionP
->X_add_symbol
= NULL
;
1431 expressionP
->X_op_symbol
= NULL
;
1438 /* Expression parser. */
1440 /* We allow an empty expression, and just assume (absolute,0) silently.
1441 Unary operators and parenthetical expressions are treated as operands.
1442 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1444 We used to do an aho/ullman shift-reduce parser, but the logic got so
1445 warped that I flushed it and wrote a recursive-descent parser instead.
1446 Now things are stable, would anybody like to write a fast parser?
1447 Most expressions are either register (which does not even reach here)
1448 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1449 So I guess it doesn't really matter how inefficient more complex expressions
1452 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1453 Also, we have consumed any leading or trailing spaces (operand does that)
1454 and done all intervening operators.
1456 This returns the segment of the result, which will be
1457 absolute_section or the segment of a symbol. */
1460 #define __ O_illegal
1462 #define O_SINGLE_EQ O_illegal
1465 /* Maps ASCII -> operators. */
1466 static const operatorT op_encoding
[256] = {
1467 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1468 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1470 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1471 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1472 __
, __
, __
, __
, __
, __
, __
, __
,
1473 __
, __
, __
, __
, O_lt
, O_SINGLE_EQ
, O_gt
, __
,
1474 __
, __
, __
, __
, __
, __
, __
, __
,
1475 __
, __
, __
, __
, __
, __
, __
, __
,
1476 __
, __
, __
, __
, __
, __
, __
, __
,
1478 #ifdef NEED_INDEX_OPERATOR
1483 __
, __
, O_bit_exclusive_or
, __
,
1484 __
, __
, __
, __
, __
, __
, __
, __
,
1485 __
, __
, __
, __
, __
, __
, __
, __
,
1486 __
, __
, __
, __
, __
, __
, __
, __
,
1487 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1489 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1490 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1491 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1492 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1493 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1494 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1495 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1496 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1500 0 operand, (expression)
1505 5 used for * / % in MRI mode
1510 static operator_rankT op_rank
[O_max
] = {
1515 0, /* O_symbol_rva */
1521 9, /* O_logical_not */
1525 8, /* O_left_shift */
1526 8, /* O_right_shift */
1527 7, /* O_bit_inclusive_or */
1528 7, /* O_bit_or_not */
1529 7, /* O_bit_exclusive_or */
1539 3, /* O_logical_and */
1540 2, /* O_logical_or */
1544 /* Unfortunately, in MRI mode for the m68k, multiplication and
1545 division have lower precedence than the bit wise operators. This
1546 function sets the operator precedences correctly for the current
1547 mode. Also, MRI uses a different bit_not operator, and this fixes
1550 #define STANDARD_MUL_PRECEDENCE 8
1551 #define MRI_MUL_PRECEDENCE 6
1554 expr_set_precedence (void)
1558 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1559 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1560 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1564 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1565 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1566 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1571 expr_set_rank (operatorT op
, operator_rankT rank
)
1573 gas_assert (op
>= O_md1
&& op
< ARRAY_SIZE (op_rank
));
1577 /* Initialize the expression parser. */
1582 expr_set_precedence ();
1584 /* Verify that X_op field is wide enough. */
1588 gas_assert (e
.X_op
== O_max
);
1591 memset (seen
, 0, sizeof seen
);
1592 memset (nr_seen
, 0, sizeof nr_seen
);
1593 expr_symbol_lines
= NULL
;
1599 for (size_t i
= 0; i
< ARRAY_SIZE (seen
); i
++)
1603 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1604 sets NUM_CHARS to the number of characters in the operator.
1605 Does not advance INPUT_LINE_POINTER. */
1607 static inline operatorT
1608 operatorf (int *num_chars
)
1613 c
= *input_line_pointer
& 0xff;
1616 if (is_end_of_line
[c
])
1620 if (is_name_beginner (c
))
1623 char ec
= get_symbol_name (& name
);
1625 ret
= md_operator (name
, 2, &ec
);
1629 *input_line_pointer
= ec
;
1630 input_line_pointer
= name
;
1635 as_bad (_("invalid use of operator \"%s\""), name
);
1639 *input_line_pointer
= ec
;
1640 *num_chars
= input_line_pointer
- name
;
1641 input_line_pointer
= name
;
1650 ret
= op_encoding
[c
];
1652 if (ret
== O_illegal
)
1654 char *start
= input_line_pointer
;
1656 ret
= md_operator (NULL
, 2, NULL
);
1657 if (ret
!= O_illegal
)
1658 *num_chars
= input_line_pointer
- start
;
1659 input_line_pointer
= start
;
1666 return op_encoding
[c
];
1669 switch (input_line_pointer
[1])
1672 return op_encoding
[c
];
1687 if (input_line_pointer
[1] != '=')
1688 return op_encoding
[c
];
1694 switch (input_line_pointer
[1])
1697 return op_encoding
[c
];
1699 ret
= O_right_shift
;
1709 switch (input_line_pointer
[1])
1712 /* We accept !! as equivalent to ^ for MRI compatibility. */
1714 return O_bit_exclusive_or
;
1716 /* We accept != as equivalent to <>. */
1721 return O_bit_inclusive_or
;
1722 return op_encoding
[c
];
1726 if (input_line_pointer
[1] != '|')
1727 return op_encoding
[c
];
1730 return O_logical_or
;
1733 if (input_line_pointer
[1] != '&')
1734 return op_encoding
[c
];
1737 return O_logical_and
;
1743 /* Implement "word-size + 1 bit" addition for
1744 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1745 is used so that the full range of unsigned word values and the full range of
1746 signed word values can be represented in an O_constant expression, which is
1747 useful e.g. for .sleb128 directives. */
1750 add_to_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1752 valueT ures
= resultP
->X_add_number
;
1753 valueT uamount
= amount
;
1755 resultP
->X_add_number
+= uamount
;
1757 resultP
->X_extrabit
^= rhs_highbit
;
1759 if (ures
+ uamount
< ures
)
1760 resultP
->X_extrabit
^= 1;
1763 /* Similarly, for subtraction. */
1766 subtract_from_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1768 valueT ures
= resultP
->X_add_number
;
1769 valueT uamount
= amount
;
1771 resultP
->X_add_number
-= uamount
;
1773 resultP
->X_extrabit
^= rhs_highbit
;
1776 resultP
->X_extrabit
^= 1;
1779 /* Parse an expression. */
1782 expr (int rankarg
, /* Larger # is higher rank. */
1783 expressionS
*resultP
, /* Deliver result here. */
1784 enum expr_mode mode
/* Controls behavior. */)
1786 operator_rankT rank
= (operator_rankT
) rankarg
;
1793 know (rankarg
>= 0);
1795 /* Save the value of dot for the fixup code. */
1798 dot_value
= frag_now_fix ();
1799 dot_frag
= frag_now
;
1802 retval
= operand (resultP
, mode
);
1804 /* operand () gobbles spaces. */
1805 know (*input_line_pointer
!= ' ');
1807 op_left
= operatorf (&op_chars
);
1808 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1813 input_line_pointer
+= op_chars
; /* -> after operator. */
1816 rightseg
= expr (op_rank
[(int) op_left
], &right
, mode
);
1817 if (right
.X_op
== O_absent
)
1819 as_warn (_("missing operand; zero assumed"));
1820 right
.X_op
= O_constant
;
1821 right
.X_add_number
= 0;
1822 right
.X_add_symbol
= NULL
;
1823 right
.X_op_symbol
= NULL
;
1826 know (*input_line_pointer
!= ' ');
1828 if (op_left
== O_index
)
1830 if (*input_line_pointer
!= ']')
1831 as_bad ("missing right bracket");
1834 ++input_line_pointer
;
1839 op_right
= operatorf (&op_chars
);
1841 know (op_right
== O_illegal
|| op_left
== O_index
1842 || op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1843 know ((int) op_left
>= (int) O_multiply
);
1845 know ((int) op_left
<= (int) O_index
);
1847 know ((int) op_left
< (int) O_max
);
1850 /* input_line_pointer->after right-hand quantity. */
1851 /* left-hand quantity in resultP. */
1852 /* right-hand quantity in right. */
1853 /* operator in op_left. */
1855 if (resultP
->X_op
== O_big
)
1857 if (resultP
->X_add_number
> 0)
1858 as_warn (_("left operand is a bignum; integer 0 assumed"));
1860 as_warn (_("left operand is a float; integer 0 assumed"));
1861 resultP
->X_op
= O_constant
;
1862 resultP
->X_add_number
= 0;
1863 resultP
->X_add_symbol
= NULL
;
1864 resultP
->X_op_symbol
= NULL
;
1866 if (right
.X_op
== O_big
)
1868 if (right
.X_add_number
> 0)
1869 as_warn (_("right operand is a bignum; integer 0 assumed"));
1871 as_warn (_("right operand is a float; integer 0 assumed"));
1872 right
.X_op
= O_constant
;
1873 right
.X_add_number
= 0;
1874 right
.X_add_symbol
= NULL
;
1875 right
.X_op_symbol
= NULL
;
1878 if (mode
== expr_defer
1879 && ((resultP
->X_add_symbol
!= NULL
1880 && S_IS_FORWARD_REF (resultP
->X_add_symbol
))
1881 || (right
.X_add_symbol
!= NULL
1882 && S_IS_FORWARD_REF (right
.X_add_symbol
))))
1885 /* Optimize common cases. */
1886 #ifdef md_optimize_expr
1887 if (md_optimize_expr (resultP
, op_left
, &right
))
1894 #ifndef md_register_arithmetic
1895 # define md_register_arithmetic 1
1897 if (op_left
== O_add
&& right
.X_op
== O_constant
1898 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1901 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1903 /* This case comes up in PIC code. */
1904 else if (op_left
== O_subtract
1905 && right
.X_op
== O_symbol
1906 && resultP
->X_op
== O_symbol
1907 && retval
== rightseg
1908 #ifdef md_allow_local_subtract
1909 && md_allow_local_subtract (resultP
, & right
, rightseg
)
1911 && ((SEG_NORMAL (rightseg
)
1912 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
1913 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
1914 || right
.X_add_symbol
== resultP
->X_add_symbol
)
1915 && frag_offset_fixed_p (symbol_get_frag (resultP
->X_add_symbol
),
1916 symbol_get_frag (right
.X_add_symbol
),
1919 offsetT symval_diff
= S_GET_VALUE (resultP
->X_add_symbol
)
1920 - S_GET_VALUE (right
.X_add_symbol
);
1921 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1922 subtract_from_result (resultP
, frag_off
/ OCTETS_PER_BYTE
, 0);
1923 add_to_result (resultP
, symval_diff
, symval_diff
< 0);
1924 resultP
->X_op
= O_constant
;
1925 resultP
->X_add_symbol
= 0;
1927 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
1928 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1931 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1933 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
1934 && (md_register_arithmetic
|| right
.X_op
!= O_register
))
1937 resultP
->X_op
= right
.X_op
;
1938 resultP
->X_add_symbol
= right
.X_add_symbol
;
1939 resultP
->X_op_symbol
= right
.X_op_symbol
;
1940 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1943 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1945 /* Constant OP constant. */
1946 offsetT v
= right
.X_add_number
;
1947 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1949 as_warn (_("division by zero"));
1952 if ((valueT
) v
>= sizeof(valueT
) * CHAR_BIT
1953 && (op_left
== O_left_shift
|| op_left
== O_right_shift
))
1955 as_warn_value_out_of_range (_("shift count"), v
, 0,
1956 sizeof(valueT
) * CHAR_BIT
- 1,
1958 resultP
->X_add_number
= v
= 0;
1962 default: goto general
;
1964 /* Do the multiply as unsigned to silence ubsan. The
1965 result is of course the same when we throw away high
1966 bits of the result. */
1967 resultP
->X_add_number
*= (valueT
) v
;
1969 case O_divide
: resultP
->X_add_number
/= v
; break;
1970 case O_modulus
: resultP
->X_add_number
%= v
; break;
1972 /* We always use unsigned shifts. According to the ISO
1973 C standard, left shift of a signed type having a
1974 negative value is undefined behaviour, and right
1975 shift of a signed type having negative value is
1976 implementation defined. Left shift of a signed type
1977 when the result overflows is also undefined
1978 behaviour. So don't trigger ubsan warnings or rely
1979 on characteristics of the compiler. */
1980 resultP
->X_add_number
1981 = (valueT
) resultP
->X_add_number
<< (valueT
) v
;
1984 resultP
->X_add_number
1985 = (valueT
) resultP
->X_add_number
>> (valueT
) v
;
1987 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1988 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1989 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1990 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1991 /* Constant + constant (O_add) is handled by the
1992 previous if statement for constant + X, so is omitted
1995 subtract_from_result (resultP
, v
, 0);
1998 resultP
->X_add_number
=
1999 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
2002 resultP
->X_add_number
=
2003 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
2006 resultP
->X_add_number
=
2007 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
2010 resultP
->X_add_number
=
2011 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
2014 resultP
->X_add_number
=
2015 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
2018 resultP
->X_add_number
=
2019 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
2022 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
2025 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
2029 else if (resultP
->X_op
== O_symbol
2030 && right
.X_op
== O_symbol
2031 && (op_left
== O_add
2032 || op_left
== O_subtract
2033 || (resultP
->X_add_number
== 0
2034 && right
.X_add_number
== 0)))
2036 /* Symbol OP symbol. */
2037 resultP
->X_op
= op_left
;
2038 resultP
->X_op_symbol
= right
.X_add_symbol
;
2039 if (op_left
== O_add
)
2040 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
2041 else if (op_left
== O_subtract
)
2043 subtract_from_result (resultP
, right
.X_add_number
,
2045 if (retval
== rightseg
2046 && SEG_NORMAL (retval
)
2047 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
2048 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
2050 retval
= absolute_section
;
2051 rightseg
= absolute_section
;
2058 /* The general case. */
2059 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
2060 resultP
->X_op_symbol
= make_expr_symbol (&right
);
2061 resultP
->X_op
= op_left
;
2062 resultP
->X_add_number
= 0;
2063 resultP
->X_unsigned
= 1;
2064 resultP
->X_extrabit
= 0;
2067 if (retval
!= rightseg
)
2069 if (retval
== undefined_section
)
2071 else if (rightseg
== undefined_section
)
2073 else if (retval
== expr_section
)
2075 else if (rightseg
== expr_section
)
2077 else if (retval
== reg_section
)
2079 else if (rightseg
== reg_section
)
2081 else if (rightseg
== absolute_section
)
2083 else if (retval
== absolute_section
)
2086 else if (op_left
== O_subtract
)
2090 as_bad (_("operation combines symbols in different segments"));
2094 } /* While next operator is >= this rank. */
2096 /* The PA port needs this information. */
2097 if (resultP
->X_add_symbol
)
2098 symbol_mark_used (resultP
->X_add_symbol
);
2100 if (rank
== 0 && mode
== expr_evaluate
)
2101 resolve_expression (resultP
);
2103 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
2106 /* Resolve an expression without changing any symbols/sub-expressions
2110 resolve_expression (expressionS
*expressionP
)
2112 /* Help out with CSE. */
2113 valueT final_val
= expressionP
->X_add_number
;
2114 symbolS
*add_symbol
= expressionP
->X_add_symbol
;
2115 symbolS
*orig_add_symbol
= add_symbol
;
2116 symbolS
*op_symbol
= expressionP
->X_op_symbol
;
2117 operatorT op
= expressionP
->X_op
;
2119 segT seg_left
, seg_right
;
2120 fragS
*frag_left
, *frag_right
;
2135 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2143 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2146 if (seg_left
!= absolute_section
)
2149 if (op
== O_logical_not
)
2151 else if (op
== O_uminus
)
2163 case O_bit_inclusive_or
:
2165 case O_bit_exclusive_or
:
2177 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
)
2178 || !snapshot_symbol (&op_symbol
, &right
, &seg_right
, &frag_right
))
2181 /* Simplify addition or subtraction of a constant by folding the
2182 constant into X_add_number. */
2185 if (seg_right
== absolute_section
)
2191 else if (seg_left
== absolute_section
)
2195 seg_left
= seg_right
;
2196 add_symbol
= op_symbol
;
2197 orig_add_symbol
= expressionP
->X_op_symbol
;
2202 else if (op
== O_subtract
)
2204 if (seg_right
== absolute_section
)
2212 /* Equality and non-equality tests are permitted on anything.
2213 Subtraction, and other comparison operators are permitted if
2214 both operands are in the same section.
2215 Shifts by constant zero are permitted on anything.
2216 Multiplies, bit-ors, and bit-ands with constant zero are
2217 permitted on anything.
2218 Multiplies and divides by constant one are permitted on
2220 Binary operations with both operands being the same register
2221 or undefined symbol are permitted if the result doesn't depend
2223 Otherwise, both operands must be absolute. We already handled
2224 the case of addition or subtraction of a constant above. */
2226 if (!(seg_left
== absolute_section
2227 && seg_right
== absolute_section
)
2228 && !(op
== O_eq
|| op
== O_ne
)
2229 && !((op
== O_subtract
2230 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
2231 && seg_left
== seg_right
2233 || frag_offset_fixed_p (frag_left
, frag_right
, &frag_off
)
2235 && frag_gtoffset_p (left
, frag_left
,
2236 right
, frag_right
, &frag_off
)))
2237 && (seg_left
!= reg_section
|| left
== right
)
2238 && (seg_left
!= undefined_section
|| add_symbol
== op_symbol
)))
2240 if ((seg_left
== absolute_section
&& left
== 0)
2241 || (seg_right
== absolute_section
&& right
== 0))
2243 if (op
== O_bit_exclusive_or
|| op
== O_bit_inclusive_or
)
2245 if (!(seg_right
== absolute_section
&& right
== 0))
2247 seg_left
= seg_right
;
2249 add_symbol
= op_symbol
;
2250 orig_add_symbol
= expressionP
->X_op_symbol
;
2255 else if (op
== O_left_shift
|| op
== O_right_shift
)
2257 if (!(seg_left
== absolute_section
&& left
== 0))
2263 else if (op
!= O_multiply
2264 && op
!= O_bit_or_not
&& op
!= O_bit_and
)
2267 else if (op
== O_multiply
2268 && seg_left
== absolute_section
&& left
== 1)
2270 seg_left
= seg_right
;
2272 add_symbol
= op_symbol
;
2273 orig_add_symbol
= expressionP
->X_op_symbol
;
2277 else if ((op
== O_multiply
|| op
== O_divide
)
2278 && seg_right
== absolute_section
&& right
== 1)
2283 else if (!(left
== right
2284 && ((seg_left
== reg_section
&& seg_right
== reg_section
)
2285 || (seg_left
== undefined_section
2286 && seg_right
== undefined_section
2287 && add_symbol
== op_symbol
))))
2289 else if (op
== O_bit_and
|| op
== O_bit_inclusive_or
)
2294 else if (op
!= O_bit_exclusive_or
&& op
!= O_bit_or_not
)
2298 right
+= frag_off
/ OCTETS_PER_BYTE
;
2301 case O_add
: left
+= right
; break;
2302 case O_subtract
: left
-= right
; break;
2303 case O_multiply
: left
*= right
; break;
2307 left
= (offsetT
) left
/ (offsetT
) right
;
2312 left
= (offsetT
) left
% (offsetT
) right
;
2315 if (right
>= sizeof (left
) * CHAR_BIT
)
2321 if (right
>= sizeof (left
) * CHAR_BIT
)
2326 case O_bit_inclusive_or
: left
|= right
; break;
2327 case O_bit_or_not
: left
|= ~right
; break;
2328 case O_bit_exclusive_or
: left
^= right
; break;
2329 case O_bit_and
: left
&= right
; break;
2332 left
= (left
== right
2333 && seg_left
== seg_right
2334 && (finalize_syms
|| frag_left
== frag_right
)
2335 && (seg_left
!= undefined_section
2336 || add_symbol
== op_symbol
)
2337 ? ~ (valueT
) 0 : 0);
2342 left
= (offsetT
) left
< (offsetT
) right
? ~ (valueT
) 0 : 0;
2345 left
= (offsetT
) left
<= (offsetT
) right
? ~ (valueT
) 0 : 0;
2348 left
= (offsetT
) left
>= (offsetT
) right
? ~ (valueT
) 0 : 0;
2351 left
= (offsetT
) left
> (offsetT
) right
? ~ (valueT
) 0 : 0;
2353 case O_logical_and
: left
= left
&& right
; break;
2354 case O_logical_or
: left
= left
|| right
; break;
2364 if (seg_left
== absolute_section
)
2366 else if (seg_left
== reg_section
&& final_val
== 0)
2368 else if (!symbol_same_p (add_symbol
, orig_add_symbol
))
2370 expressionP
->X_add_symbol
= add_symbol
;
2372 expressionP
->X_op
= op
;
2374 if (op
== O_constant
|| op
== O_register
)
2376 expressionP
->X_add_number
= final_val
;
2381 /* This lives here because it belongs equally in expr.c & read.c.
2382 expr.c is just a branch office read.c anyway, and putting it
2383 here lessens the crowd at read.c.
2385 Assume input_line_pointer is at start of symbol name, or the
2386 start of a double quote enclosed symbol name.
2387 Advance input_line_pointer past symbol name.
2388 Turn that character into a '\0', returning its former value,
2389 which may be the closing double quote.
2390 This allows a string compare (RMS wants symbol names to be strings)
2392 There will always be a char following symbol name, because all good
2393 lines end in end-of-line. */
2396 get_symbol_name (char ** ilp_return
)
2400 * ilp_return
= input_line_pointer
;
2401 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2402 constructed string. */
2403 if (is_name_beginner (c
= *input_line_pointer
++)
2404 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2406 while (is_part_of_name (c
= *input_line_pointer
++)
2407 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2409 if (is_name_ender (c
))
2410 c
= *input_line_pointer
++;
2414 char *dst
= input_line_pointer
;
2416 * ilp_return
= input_line_pointer
;
2419 c
= *input_line_pointer
++;
2423 as_warn (_("missing closing '\"'"));
2429 char *ilp_save
= input_line_pointer
;
2432 if (*input_line_pointer
== '"')
2434 ++input_line_pointer
;
2437 input_line_pointer
= ilp_save
;
2442 switch (*input_line_pointer
)
2446 c
= *input_line_pointer
++;
2451 as_warn (_("'\\%c' in quoted symbol name; "
2452 "behavior may change in the future"),
2453 *input_line_pointer
);
2461 *--input_line_pointer
= 0;
2465 /* Replace the NUL character pointed to by input_line_pointer
2466 with C. If C is \" then advance past it. Return the character
2467 now pointed to by input_line_pointer. */
2470 restore_line_pointer (char c
)
2472 * input_line_pointer
= c
;
2474 c
= * ++ input_line_pointer
;
2479 get_single_number (void)
2482 operand (&exp
, expr_normal
);
2483 return exp
.X_add_number
;