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 static const expressionS zero
= { .X_op
= O_constant
};
54 /* Build a dummy symbol to hold a complex expression. This is how we
55 build expressions up out of other expressions. The symbol is put
56 into the fake section expr_section. */
59 make_expr_symbol (const expressionS
*expressionP
)
62 struct expr_symbol_line
*n
;
64 if (expressionP
->X_op
== O_symbol
65 && expressionP
->X_add_number
== 0)
66 return expressionP
->X_add_symbol
;
68 if (expressionP
->X_op
== O_big
)
70 /* This won't work, because the actual value is stored in
71 generic_floating_point_number or generic_bignum, and we are
72 going to lose it if we haven't already. */
73 if (expressionP
->X_add_number
> 0)
74 as_bad (_("bignum invalid"));
76 as_bad (_("floating point number invalid"));
80 /* Putting constant symbols in absolute_section rather than
81 expr_section is convenient for the old a.out code, for which
82 S_GET_SEGMENT does not always retrieve the value put in by
84 symbolP
= symbol_create (FAKE_LABEL_NAME
,
85 (expressionP
->X_op
== O_constant
87 : expressionP
->X_op
== O_register
90 &zero_address_frag
, 0);
91 symbol_set_value_expression (symbolP
, expressionP
);
93 if (expressionP
->X_op
== O_constant
)
94 resolve_symbol_value (symbolP
);
96 n
= notes_alloc (sizeof (*n
));
98 n
->file
= as_where (&n
->line
);
99 n
->next
= expr_symbol_lines
;
100 expr_symbol_lines
= n
;
105 /* Return the file and line number for an expr symbol. Return
106 non-zero if something was found, 0 if no information is known for
110 expr_symbol_where (symbolS
*sym
, const char **pfile
, unsigned int *pline
)
112 struct expr_symbol_line
*l
;
114 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
127 /* Look up a previously used .startof. / .sizeof. symbol, or make a fresh
129 static symbolS
**seen
[2];
130 static unsigned int nr_seen
[2];
133 symbol_lookup_or_make (const char *name
, bool start
)
135 char *buf
= concat (start
? ".startof." : ".sizeof.", name
, NULL
);
139 for (i
= 0; i
< nr_seen
[start
]; ++i
)
141 symbolP
= seen
[start
][i
];
146 name
= S_GET_NAME (symbolP
);
147 if ((symbols_case_sensitive
149 : strcasecmp (buf
, name
)) == 0)
156 symbolP
= symbol_make (buf
);
159 if (i
>= nr_seen
[start
])
161 unsigned int nr
= (i
+ 1) * 2;
163 seen
[start
] = XRESIZEVEC (symbolS
*, seen
[start
], nr
);
165 memset (&seen
[start
][i
+ 1], 0, (nr
- i
- 1) * sizeof(seen
[0][0]));
168 seen
[start
][i
] = symbolP
;
173 /* Utilities for building expressions.
174 Since complex expressions are recorded as symbols for use in other
175 expressions these return a symbolS * and not an expressionS *.
176 These explicitly do not take an "add_number" argument. */
177 /* ??? For completeness' sake one might want expr_build_symbol.
178 It would just return its argument. */
180 /* Build an expression for an unsigned constant.
181 The corresponding one for signed constants is missing because
182 there's currently no need for it. One could add an unsigned_p flag
183 but that seems more clumsy. */
186 expr_build_uconstant (offsetT value
)
191 e
.X_add_number
= value
;
194 return make_expr_symbol (&e
);
197 /* Build an expression for the current location ('.'). */
200 expr_build_dot (void)
204 current_location (&e
);
205 return symbol_clone_if_forward_ref (make_expr_symbol (&e
));
208 /* Build any floating-point literal here.
209 Also build any bignum literal here. */
211 /* Seems atof_machine can backscan through generic_bignum and hit whatever
212 happens to be loaded before it in memory. And its way too complicated
213 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
214 and never write into the early words, thus they'll always be zero.
215 I hate Dean's floating-point code. Bleh. */
216 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
218 FLONUM_TYPE generic_floating_point_number
= {
219 &generic_bignum
[6], /* low. (JF: Was 0) */
220 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high. JF: (added +6) */
228 floating_constant (expressionS
*expressionP
)
230 /* input_line_pointer -> floating-point constant. */
233 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
234 &generic_floating_point_number
);
238 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
240 as_bad (_("bad floating-point constant: exponent overflow"));
244 as_bad (_("bad floating-point constant: unknown error code=%d"),
248 expressionP
->X_op
= O_big
;
249 /* input_line_pointer -> just after constant, which may point to
251 expressionP
->X_add_number
= -1;
255 generic_bignum_to_int32 (void)
257 return ((((uint32_t) generic_bignum
[1] & LITTLENUM_MASK
)
258 << LITTLENUM_NUMBER_OF_BITS
)
259 | ((uint32_t) generic_bignum
[0] & LITTLENUM_MASK
));
263 generic_bignum_to_int64 (void)
265 return ((((((((uint64_t) generic_bignum
[3] & LITTLENUM_MASK
)
266 << LITTLENUM_NUMBER_OF_BITS
)
267 | ((uint64_t) generic_bignum
[2] & LITTLENUM_MASK
))
268 << LITTLENUM_NUMBER_OF_BITS
)
269 | ((uint64_t) generic_bignum
[1] & LITTLENUM_MASK
))
270 << LITTLENUM_NUMBER_OF_BITS
)
271 | ((uint64_t) generic_bignum
[0] & LITTLENUM_MASK
));
275 integer_constant (int radix
, expressionS
*expressionP
)
277 char *start
; /* Start of number. */
280 valueT number
; /* Offset or (absolute) value. */
281 short int digit
; /* Value of next digit in current radix. */
282 short int maxdig
= 0; /* Highest permitted digit value. */
283 int too_many_digits
= 0; /* If we see >= this number of. */
284 char *name
; /* Points to name of symbol. */
285 symbolS
*symbolP
; /* Points to symbol. */
287 int small
; /* True if fits in 32 bits. */
289 /* May be bignum, or may fit in 32 bits. */
290 /* Most numbers fit into 32 bits, and we want this case to be fast.
291 so we pretend it will fit into 32 bits. If, after making up a 32
292 bit number, we realise that we have scanned more digits than
293 comfortably fit into 32 bits, we re-scan the digits coding them
294 into a bignum. For decimal and octal numbers we are
295 conservative: Some numbers may be assumed bignums when in fact
296 they do fit into 32 bits. Numbers of any radix can have excess
297 leading zeros: We strive to recognise this and cast them back
298 into 32 bits. We must check that the bignum really is more than
299 32 bits, and change it back to a 32-bit number if it fits. The
300 number we are looking for is expected to be positive, but if it
301 fits into 32 bits as an unsigned number, we let it be a 32-bit
302 number. The cavalier approach is for speed in ordinary cases. */
303 /* This has been extended for 64 bits. We blindly assume that if
304 you're compiling in 64-bit mode, the target is a 64-bit machine.
305 This should be cleaned up. */
309 #else /* includes non-bfd case, mostly */
313 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
315 expressionP
->X_op
= O_absent
;
319 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
) && radix
== 0)
323 /* In MRI mode, the number may have a suffix indicating the
324 radix. For that matter, it might actually be a floating
326 for (suffix
= input_line_pointer
; ISALNUM (*suffix
); suffix
++)
328 if (*suffix
== 'e' || *suffix
== 'E')
332 if (suffix
== input_line_pointer
)
341 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
342 we distinguish between 'B' and 'b'. This is the case for
344 if ((NUMBERS_WITH_SUFFIX
&& LOCAL_LABELS_FB
? *suffix
: c
) == 'B')
348 else if (c
== 'O' || c
== 'Q')
352 else if (suffix
[1] == '.' || c
== 'E' || flt
)
354 floating_constant (expressionP
);
369 too_many_digits
= valuesize
+ 1;
373 too_many_digits
= (valuesize
+ 2) / 3 + 1;
377 too_many_digits
= (valuesize
+ 3) / 4 + 1;
381 too_many_digits
= (valuesize
+ 11) / 4; /* Very rough. */
384 start
= input_line_pointer
;
385 c
= *input_line_pointer
++;
387 (digit
= hex_value (c
)) < maxdig
;
388 c
= *input_line_pointer
++)
390 number
= number
* radix
+ digit
;
392 /* c contains character after number. */
393 /* input_line_pointer->char after c. */
394 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
396 if (radix
== 16 && c
== '_')
398 /* This is literal of the form 0x333_0_12345678_1.
399 This example is equivalent to 0x00000333000000001234567800000001. */
401 int num_little_digits
= 0;
403 input_line_pointer
= start
; /* -> 1st digit. */
405 know (LITTLENUM_NUMBER_OF_BITS
== 16);
407 for (c
= '_'; c
== '_'; num_little_digits
+= 2)
410 /* Convert one 64-bit word. */
413 for (c
= *input_line_pointer
++;
414 (digit
= hex_value (c
)) < maxdig
;
415 c
= *(input_line_pointer
++))
417 number
= number
* radix
+ digit
;
421 /* Check for 8 digit per word max. */
423 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
425 /* Add this chunk to the bignum.
426 Shift things down 2 little digits. */
427 know (LITTLENUM_NUMBER_OF_BITS
== 16);
428 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1);
431 generic_bignum
[i
] = generic_bignum
[i
- 2];
433 /* Add the new digits as the least significant new ones. */
434 generic_bignum
[0] = number
& 0xffffffff;
435 generic_bignum
[1] = number
>> 16;
438 /* Again, c is char after number, input_line_pointer->after c. */
440 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
441 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
443 gas_assert (num_little_digits
>= 4);
445 if (num_little_digits
!= 8)
446 as_bad (_("a bignum with underscores must have exactly 4 words"));
448 /* We might have some leading zeros. These can be trimmed to give
449 us a change to fit this constant into a small number. */
450 while (generic_bignum
[num_little_digits
- 1] == 0
451 && num_little_digits
> 1)
454 if (num_little_digits
<= 2)
456 /* will fit into 32 bits. */
457 number
= generic_bignum_to_int32 ();
461 else if (num_little_digits
<= 4)
463 /* Will fit into 64 bits. */
464 number
= generic_bignum_to_int64 ();
472 /* Number of littlenums in the bignum. */
473 number
= num_little_digits
;
478 /* We saw a lot of digits. manufacture a bignum the hard way. */
479 LITTLENUM_TYPE
*leader
; /* -> high order littlenum of the bignum. */
480 LITTLENUM_TYPE
*pointer
; /* -> littlenum we are frobbing now. */
483 leader
= generic_bignum
;
484 generic_bignum
[0] = 0;
485 generic_bignum
[1] = 0;
486 generic_bignum
[2] = 0;
487 generic_bignum
[3] = 0;
488 input_line_pointer
= start
; /* -> 1st digit. */
489 c
= *input_line_pointer
++;
490 for (; (carry
= hex_value (c
)) < maxdig
; c
= *input_line_pointer
++)
492 for (pointer
= generic_bignum
; pointer
<= leader
; pointer
++)
496 work
= carry
+ radix
* *pointer
;
497 *pointer
= work
& LITTLENUM_MASK
;
498 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
502 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
504 /* Room to grow a longer bignum. */
509 /* Again, c is char after number. */
510 /* input_line_pointer -> after c. */
511 know (LITTLENUM_NUMBER_OF_BITS
== 16);
512 if (leader
< generic_bignum
+ 2)
514 /* Will fit into 32 bits. */
515 number
= generic_bignum_to_int32 ();
519 else if (leader
< generic_bignum
+ 4)
521 /* Will fit into 64 bits. */
522 number
= generic_bignum_to_int64 ();
528 /* Number of littlenums in the bignum. */
529 number
= leader
- generic_bignum
+ 1;
533 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
535 && input_line_pointer
- 1 == suffix
)
536 c
= *input_line_pointer
++;
538 #ifndef tc_allow_U_suffix
539 #define tc_allow_U_suffix 1
541 /* PR 19910: Look for, and ignore, a U suffix to the number. */
542 if (tc_allow_U_suffix
&& (c
== 'U' || c
== 'u'))
543 c
= * input_line_pointer
++;
545 #ifndef tc_allow_L_suffix
546 #define tc_allow_L_suffix 1
548 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
549 if (tc_allow_L_suffix
)
550 while (c
== 'L' || c
== 'l')
551 c
= * input_line_pointer
++;
555 /* Here with number, in correct radix. c is the next char.
556 Note that unlike un*x, we allow "011f" "0x9f" to both mean
557 the same as the (conventional) "9f".
558 This is simply easier than checking for strict canonical
561 if (LOCAL_LABELS_FB
&& c
== 'b')
563 /* Backward ref to local label.
564 Because it is backward, expect it to be defined. */
565 /* Construct a local label. */
566 name
= fb_label_name (number
, 0);
568 /* Seen before, or symbol is defined: OK. */
569 symbolP
= symbol_find (name
);
570 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
572 expressionP
->X_op
= O_symbol
;
573 expressionP
->X_add_symbol
= symbolP
;
577 /* Either not seen or not defined. */
578 /* @@ Should print out the original string instead of
579 the parsed number. */
580 as_bad (_("backward ref to unknown label \"%d:\""),
582 expressionP
->X_op
= O_constant
;
585 expressionP
->X_add_number
= 0;
587 else if (LOCAL_LABELS_FB
&& c
== 'f')
589 /* Forward reference. Expect symbol to be undefined or
590 unknown. undefined: seen it before. unknown: never seen
593 Construct a local label name, then an undefined symbol.
594 Don't create a xseg frag for it: caller may do that.
595 Just return it as never seen before. */
596 name
= fb_label_name (number
, 1);
597 symbolP
= symbol_find_or_make (name
);
598 /* We have no need to check symbol properties. */
599 expressionP
->X_op
= O_symbol
;
600 expressionP
->X_add_symbol
= symbolP
;
601 expressionP
->X_add_number
= 0;
603 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
605 /* If the dollar label is *currently* defined, then this is just
606 another reference to it. If it is not *currently* defined,
607 then this is a fresh instantiation of that number, so create
610 if (dollar_label_defined (number
))
612 name
= dollar_label_name (number
, 0);
613 symbolP
= symbol_find (name
);
614 know (symbolP
!= NULL
);
618 name
= dollar_label_name (number
, 1);
619 symbolP
= symbol_find_or_make (name
);
622 expressionP
->X_op
= O_symbol
;
623 expressionP
->X_add_symbol
= symbolP
;
624 expressionP
->X_add_number
= 0;
628 expressionP
->X_op
= O_constant
;
629 expressionP
->X_add_number
= number
;
630 input_line_pointer
--; /* Restore following character. */
631 } /* Really just a number. */
635 /* Not a small number. */
636 expressionP
->X_op
= O_big
;
637 expressionP
->X_add_number
= number
; /* Number of littlenums. */
638 input_line_pointer
--; /* -> char following number. */
642 /* Parse an MRI multi character constant. */
645 mri_char_constant (expressionS
*expressionP
)
649 if (*input_line_pointer
== '\''
650 && input_line_pointer
[1] != '\'')
652 expressionP
->X_op
= O_constant
;
653 expressionP
->X_add_number
= 0;
657 /* In order to get the correct byte ordering, we must build the
658 number in reverse. */
659 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
663 generic_bignum
[i
] = 0;
664 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
666 if (*input_line_pointer
== '\'')
668 if (input_line_pointer
[1] != '\'')
670 ++input_line_pointer
;
672 generic_bignum
[i
] <<= 8;
673 generic_bignum
[i
] += *input_line_pointer
;
674 ++input_line_pointer
;
677 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
679 /* If there is more than one littlenum, left justify the
680 last one to make it match the earlier ones. If there is
681 only one, we can just use the value directly. */
682 for (; j
< CHARS_PER_LITTLENUM
; j
++)
683 generic_bignum
[i
] <<= 8;
686 if (*input_line_pointer
== '\''
687 && input_line_pointer
[1] != '\'')
693 as_bad (_("character constant too large"));
702 c
= SIZE_OF_LARGE_NUMBER
- i
;
703 for (j
= 0; j
< c
; j
++)
704 generic_bignum
[j
] = generic_bignum
[i
+ j
];
708 know (LITTLENUM_NUMBER_OF_BITS
== 16);
711 expressionP
->X_op
= O_big
;
712 expressionP
->X_add_number
= i
;
716 expressionP
->X_op
= O_constant
;
718 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
720 expressionP
->X_add_number
=
721 (((generic_bignum
[1] & LITTLENUM_MASK
)
722 << LITTLENUM_NUMBER_OF_BITS
)
723 | (generic_bignum
[0] & LITTLENUM_MASK
));
726 /* Skip the final closing quote. */
727 ++input_line_pointer
;
730 /* Return an expression representing the current location. This
731 handles the magic symbol `.'. */
734 current_location (expressionS
*expressionp
)
736 if (now_seg
== absolute_section
)
738 expressionp
->X_op
= O_constant
;
739 expressionp
->X_add_number
= abs_section_offset
;
743 expressionp
->X_op
= O_symbol
;
744 expressionp
->X_add_symbol
= &dot_symbol
;
745 expressionp
->X_add_number
= 0;
749 #ifndef md_register_arithmetic
750 # define md_register_arithmetic 1
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;
1059 expressionP
->X_unsigned
= 0;
1063 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1064 expressionP
->X_unsigned
= 1;
1065 expressionP
->X_extrabit
= 0;
1068 else if (expressionP
->X_op
== O_big
1069 && expressionP
->X_add_number
<= 0
1071 && (generic_floating_point_number
.sign
== '+'
1072 || generic_floating_point_number
.sign
== 'P'))
1074 /* Negative flonum (eg, -1.000e0). */
1075 if (generic_floating_point_number
.sign
== '+')
1076 generic_floating_point_number
.sign
= '-';
1078 generic_floating_point_number
.sign
= 'N';
1080 else if (expressionP
->X_op
== O_big
1081 && expressionP
->X_add_number
> 0)
1085 if (c
== '~' || c
== '-')
1087 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1088 generic_bignum
[i
] = ~generic_bignum
[i
];
1090 /* Extend the bignum to at least the size of .octa. */
1091 if (expressionP
->X_add_number
< SIZE_OF_LARGE_NUMBER
)
1093 expressionP
->X_add_number
= SIZE_OF_LARGE_NUMBER
;
1094 for (; i
< expressionP
->X_add_number
; ++i
)
1095 generic_bignum
[i
] = ~(LITTLENUM_TYPE
) 0;
1099 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1101 generic_bignum
[i
] += 1;
1102 if (generic_bignum
[i
])
1108 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1109 if (generic_bignum
[i
] != 0)
1111 expressionP
->X_add_number
= i
>= expressionP
->X_add_number
;
1112 expressionP
->X_op
= O_constant
;
1113 expressionP
->X_unsigned
= 1;
1114 expressionP
->X_extrabit
= 0;
1117 else if (expressionP
->X_op
!= O_illegal
1118 && expressionP
->X_op
!= O_absent
)
1122 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1124 expressionP
->X_op
= O_uminus
;
1125 else if (c
== '~' || c
== '"')
1126 expressionP
->X_op
= O_bit_not
;
1128 expressionP
->X_op
= O_logical_not
;
1129 expressionP
->X_add_number
= 0;
1131 else if (!md_register_arithmetic
&& expressionP
->X_op
== O_register
)
1133 /* Convert to binary '+'. */
1134 expressionP
->X_op_symbol
= make_expr_symbol (expressionP
);
1135 expressionP
->X_add_symbol
= make_expr_symbol (&zero
);
1136 expressionP
->X_add_number
= 0;
1137 expressionP
->X_op
= O_add
;
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
);
1600 memset (seen
, 0, sizeof seen
);
1601 memset (nr_seen
, 0, sizeof nr_seen
);
1602 expr_symbol_lines
= NULL
;
1608 for (size_t i
= 0; i
< ARRAY_SIZE (seen
); i
++)
1612 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1613 sets NUM_CHARS to the number of characters in the operator.
1614 Does not advance INPUT_LINE_POINTER. */
1616 static inline operatorT
1617 operatorf (int *num_chars
)
1622 c
= *input_line_pointer
& 0xff;
1625 if (is_end_of_line
[c
])
1629 if (is_name_beginner (c
))
1632 char ec
= get_symbol_name (& name
);
1634 ret
= md_operator (name
, 2, &ec
);
1638 *input_line_pointer
= ec
;
1639 input_line_pointer
= name
;
1644 as_bad (_("invalid use of operator \"%s\""), name
);
1648 *input_line_pointer
= ec
;
1649 *num_chars
= input_line_pointer
- name
;
1650 input_line_pointer
= name
;
1659 ret
= op_encoding
[c
];
1661 if (ret
== O_illegal
)
1663 char *start
= input_line_pointer
;
1665 ret
= md_operator (NULL
, 2, NULL
);
1666 if (ret
!= O_illegal
)
1667 *num_chars
= input_line_pointer
- start
;
1668 input_line_pointer
= start
;
1675 return op_encoding
[c
];
1678 switch (input_line_pointer
[1])
1681 return op_encoding
[c
];
1696 if (input_line_pointer
[1] != '=')
1697 return op_encoding
[c
];
1703 switch (input_line_pointer
[1])
1706 return op_encoding
[c
];
1708 ret
= O_right_shift
;
1718 switch (input_line_pointer
[1])
1721 /* We accept !! as equivalent to ^ for MRI compatibility. */
1723 return O_bit_exclusive_or
;
1725 /* We accept != as equivalent to <>. */
1730 return O_bit_inclusive_or
;
1731 return op_encoding
[c
];
1735 if (input_line_pointer
[1] != '|')
1736 return op_encoding
[c
];
1739 return O_logical_or
;
1742 if (input_line_pointer
[1] != '&')
1743 return op_encoding
[c
];
1746 return O_logical_and
;
1752 /* Implement "word-size + 1 bit" addition for
1753 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1754 is used so that the full range of unsigned word values and the full range of
1755 signed word values can be represented in an O_constant expression, which is
1756 useful e.g. for .sleb128 directives. */
1759 add_to_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1761 valueT ures
= resultP
->X_add_number
;
1762 valueT uamount
= amount
;
1764 resultP
->X_add_number
+= uamount
;
1766 resultP
->X_extrabit
^= rhs_highbit
;
1768 if (ures
+ uamount
< ures
)
1769 resultP
->X_extrabit
^= 1;
1772 /* Similarly, for subtraction. */
1775 subtract_from_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1777 valueT ures
= resultP
->X_add_number
;
1778 valueT uamount
= amount
;
1780 resultP
->X_add_number
-= uamount
;
1782 resultP
->X_extrabit
^= rhs_highbit
;
1785 resultP
->X_extrabit
^= 1;
1788 /* Parse an expression. */
1791 expr (int rankarg
, /* Larger # is higher rank. */
1792 expressionS
*resultP
, /* Deliver result here. */
1793 enum expr_mode mode
/* Controls behavior. */)
1795 operator_rankT rank
= (operator_rankT
) rankarg
;
1802 know (rankarg
>= 0);
1804 /* Save the value of dot for the fixup code. */
1807 dot_value
= frag_now_fix ();
1808 dot_frag
= frag_now
;
1811 retval
= operand (resultP
, mode
);
1813 /* operand () gobbles spaces. */
1814 know (*input_line_pointer
!= ' ');
1816 op_left
= operatorf (&op_chars
);
1817 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1823 input_line_pointer
+= op_chars
; /* -> after operator. */
1826 rightseg
= expr (op_rank
[(int) op_left
], &right
, mode
);
1827 if (right
.X_op
== O_absent
)
1829 as_warn (_("missing operand; zero assumed"));
1830 right
.X_op
= O_constant
;
1831 right
.X_add_number
= 0;
1832 right
.X_add_symbol
= NULL
;
1833 right
.X_op_symbol
= NULL
;
1836 know (*input_line_pointer
!= ' ');
1838 if (op_left
== O_index
)
1840 if (*input_line_pointer
!= ']')
1841 as_bad ("missing right bracket");
1844 ++input_line_pointer
;
1849 op_right
= operatorf (&op_chars
);
1851 know (op_right
== O_illegal
|| op_left
== O_index
1852 || op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1853 know ((int) op_left
>= (int) O_multiply
);
1855 know ((int) op_left
<= (int) O_index
);
1857 know ((int) op_left
< (int) O_max
);
1860 /* input_line_pointer->after right-hand quantity. */
1861 /* left-hand quantity in resultP. */
1862 /* right-hand quantity in right. */
1863 /* operator in op_left. */
1865 if (resultP
->X_op
== O_big
)
1867 if (resultP
->X_add_number
> 0)
1868 as_warn (_("left operand is a bignum; integer 0 assumed"));
1870 as_warn (_("left operand is a float; integer 0 assumed"));
1871 resultP
->X_op
= O_constant
;
1872 resultP
->X_add_number
= 0;
1873 resultP
->X_add_symbol
= NULL
;
1874 resultP
->X_op_symbol
= NULL
;
1876 if (right
.X_op
== O_big
)
1878 if (right
.X_add_number
> 0)
1879 as_warn (_("right operand is a bignum; integer 0 assumed"));
1881 as_warn (_("right operand is a float; integer 0 assumed"));
1882 right
.X_op
= O_constant
;
1883 right
.X_add_number
= 0;
1884 right
.X_add_symbol
= NULL
;
1885 right
.X_op_symbol
= NULL
;
1888 is_unsigned
= resultP
->X_unsigned
&& right
.X_unsigned
;
1890 if (mode
== expr_defer
1891 && ((resultP
->X_add_symbol
!= NULL
1892 && S_IS_FORWARD_REF (resultP
->X_add_symbol
))
1893 || (right
.X_add_symbol
!= NULL
1894 && S_IS_FORWARD_REF (right
.X_add_symbol
))))
1897 /* Optimize common cases. */
1898 #ifdef md_optimize_expr
1899 if (md_optimize_expr (resultP
, op_left
, &right
))
1902 is_unsigned
= resultP
->X_unsigned
;
1906 if (op_left
== O_add
&& right
.X_op
== O_constant
1907 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1910 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1912 /* This case comes up in PIC code. */
1913 else if (op_left
== O_subtract
1914 && right
.X_op
== O_symbol
1915 && resultP
->X_op
== O_symbol
1916 && retval
== rightseg
1917 #ifdef md_allow_local_subtract
1918 && md_allow_local_subtract (resultP
, & right
, rightseg
)
1920 && ((SEG_NORMAL (rightseg
)
1921 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
1922 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
1923 || right
.X_add_symbol
== resultP
->X_add_symbol
)
1924 && frag_offset_fixed_p (symbol_get_frag (resultP
->X_add_symbol
),
1925 symbol_get_frag (right
.X_add_symbol
),
1928 offsetT symval_diff
= S_GET_VALUE (resultP
->X_add_symbol
)
1929 - S_GET_VALUE (right
.X_add_symbol
);
1930 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1931 subtract_from_result (resultP
, frag_off
/ OCTETS_PER_BYTE
, 0);
1932 add_to_result (resultP
, symval_diff
, symval_diff
< 0);
1933 resultP
->X_op
= O_constant
;
1934 resultP
->X_add_symbol
= 0;
1935 is_unsigned
= false;
1937 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
1938 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1941 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1942 is_unsigned
= false;
1944 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
1945 && (md_register_arithmetic
|| right
.X_op
!= O_register
))
1948 resultP
->X_op
= right
.X_op
;
1949 resultP
->X_add_symbol
= right
.X_add_symbol
;
1950 resultP
->X_op_symbol
= right
.X_op_symbol
;
1951 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1954 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1956 /* Constant OP constant. */
1957 offsetT v
= right
.X_add_number
;
1958 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1960 as_warn (_("division by zero"));
1965 default: goto general
;
1967 /* Do the multiply as unsigned to silence ubsan. The
1968 result is of course the same when we throw away high
1969 bits of the result. */
1970 resultP
->X_add_number
*= (valueT
) v
;
1972 case O_divide
: resultP
->X_add_number
/= v
; break;
1973 case O_modulus
: resultP
->X_add_number
%= v
; break;
1976 /* We always use unsigned shifts. According to the ISO
1977 C standard, left shift of a signed type having a
1978 negative value is undefined behaviour, and right
1979 shift of a signed type having negative value is
1980 implementation defined. Left shift of a signed type
1981 when the result overflows is also undefined
1982 behaviour. So don't trigger ubsan warnings or rely
1983 on characteristics of the compiler. */
1984 if ((valueT
) v
>= sizeof (valueT
) * CHAR_BIT
)
1986 as_warn_value_out_of_range (_("shift count"), v
, 0,
1987 sizeof (valueT
) * CHAR_BIT
- 1,
1989 resultP
->X_add_number
= 0;
1991 else if (op_left
== O_left_shift
)
1992 resultP
->X_add_number
1993 = (valueT
) resultP
->X_add_number
<< (valueT
) v
;
1995 resultP
->X_add_number
1996 = (valueT
) resultP
->X_add_number
>> (valueT
) v
;
1997 is_unsigned
= resultP
->X_unsigned
;
1999 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
2000 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
2001 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
2002 case O_bit_and
: resultP
->X_add_number
&= v
; break;
2003 /* Constant + constant (O_add) is handled by the
2004 previous if statement for constant + X, so is omitted
2007 subtract_from_result (resultP
, v
, 0);
2008 is_unsigned
= false;
2011 resultP
->X_add_number
=
2012 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
2013 is_unsigned
= false;
2016 resultP
->X_add_number
=
2017 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
2018 is_unsigned
= false;
2021 resultP
->X_add_number
=
2022 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
2023 is_unsigned
= false;
2026 resultP
->X_add_number
=
2027 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
2028 is_unsigned
= false;
2031 resultP
->X_add_number
=
2032 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
2033 is_unsigned
= false;
2036 resultP
->X_add_number
=
2037 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
2038 is_unsigned
= false;
2041 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
2045 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
2050 else if (resultP
->X_op
== O_symbol
2051 && right
.X_op
== O_symbol
2052 && (op_left
== O_add
2053 || op_left
== O_subtract
2054 || (resultP
->X_add_number
== 0
2055 && right
.X_add_number
== 0)))
2057 /* Symbol OP symbol. */
2058 resultP
->X_op
= op_left
;
2059 resultP
->X_op_symbol
= right
.X_add_symbol
;
2060 if (op_left
== O_add
)
2061 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
2062 else if (op_left
== O_subtract
)
2064 subtract_from_result (resultP
, right
.X_add_number
,
2066 if (retval
== rightseg
2067 && SEG_NORMAL (retval
)
2068 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
2069 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
2071 retval
= absolute_section
;
2072 rightseg
= absolute_section
;
2079 /* The general case. */
2080 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
2081 resultP
->X_op_symbol
= make_expr_symbol (&right
);
2082 resultP
->X_op
= op_left
;
2083 resultP
->X_add_number
= 0;
2084 resultP
->X_extrabit
= 0;
2087 resultP
->X_unsigned
= is_unsigned
;
2089 if (retval
!= rightseg
)
2091 if (retval
== undefined_section
)
2093 else if (rightseg
== undefined_section
)
2095 else if (retval
== expr_section
)
2097 else if (rightseg
== expr_section
)
2099 else if (retval
== reg_section
)
2101 else if (rightseg
== reg_section
)
2103 else if (rightseg
== absolute_section
)
2105 else if (retval
== absolute_section
)
2108 else if (op_left
== O_subtract
)
2112 as_bad (_("operation combines symbols in different segments"));
2116 } /* While next operator is >= this rank. */
2118 /* The PA port needs this information. */
2119 if (resultP
->X_add_symbol
)
2120 symbol_mark_used (resultP
->X_add_symbol
);
2122 if (rank
== 0 && mode
== expr_evaluate
)
2123 resolve_expression (resultP
);
2125 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
2128 /* Resolve an expression without changing any symbols/sub-expressions
2132 resolve_expression (expressionS
*expressionP
)
2134 /* Help out with CSE. */
2135 valueT final_val
= expressionP
->X_add_number
;
2136 symbolS
*add_symbol
= expressionP
->X_add_symbol
;
2137 symbolS
*orig_add_symbol
= add_symbol
;
2138 symbolS
*op_symbol
= expressionP
->X_op_symbol
;
2139 operatorT op
= expressionP
->X_op
;
2141 segT seg_left
, seg_right
;
2142 fragS
*frag_left
, *frag_right
;
2157 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2165 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2168 if (seg_left
!= absolute_section
)
2171 if (op
== O_logical_not
)
2173 else if (op
== O_uminus
)
2185 case O_bit_inclusive_or
:
2187 case O_bit_exclusive_or
:
2199 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
)
2200 || !snapshot_symbol (&op_symbol
, &right
, &seg_right
, &frag_right
))
2203 /* Simplify addition or subtraction of a constant by folding the
2204 constant into X_add_number. */
2207 if (seg_right
== absolute_section
)
2213 else if (seg_left
== absolute_section
)
2217 seg_left
= seg_right
;
2218 add_symbol
= op_symbol
;
2219 orig_add_symbol
= expressionP
->X_op_symbol
;
2224 else if (op
== O_subtract
)
2226 if (seg_right
== absolute_section
)
2234 /* Equality and non-equality tests are permitted on anything.
2235 Subtraction, and other comparison operators are permitted if
2236 both operands are in the same section.
2237 Shifts by constant zero are permitted on anything.
2238 Multiplies, bit-ors, and bit-ands with constant zero are
2239 permitted on anything.
2240 Multiplies and divides by constant one are permitted on
2242 Binary operations with both operands being the same register
2243 or undefined symbol are permitted if the result doesn't depend
2245 Otherwise, both operands must be absolute. We already handled
2246 the case of addition or subtraction of a constant above. */
2248 if (!(seg_left
== absolute_section
2249 && seg_right
== absolute_section
)
2250 && !(op
== O_eq
|| op
== O_ne
)
2251 && !((op
== O_subtract
2252 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
2253 && seg_left
== seg_right
2255 || frag_offset_fixed_p (frag_left
, frag_right
, &frag_off
)
2257 && frag_gtoffset_p (left
, frag_left
,
2258 right
, frag_right
, &frag_off
)))
2259 && (seg_left
!= reg_section
|| left
== right
)
2260 && (seg_left
!= undefined_section
|| add_symbol
== op_symbol
)))
2262 if ((seg_left
== absolute_section
&& left
== 0)
2263 || (seg_right
== absolute_section
&& right
== 0))
2265 if (op
== O_bit_exclusive_or
|| op
== O_bit_inclusive_or
)
2267 if (!(seg_right
== absolute_section
&& right
== 0))
2269 seg_left
= seg_right
;
2271 add_symbol
= op_symbol
;
2272 orig_add_symbol
= expressionP
->X_op_symbol
;
2277 else if (op
== O_left_shift
|| op
== O_right_shift
)
2279 if (!(seg_left
== absolute_section
&& left
== 0))
2285 else if (op
!= O_multiply
2286 && op
!= O_bit_or_not
&& op
!= O_bit_and
)
2289 else if (op
== O_multiply
2290 && seg_left
== absolute_section
&& left
== 1)
2292 seg_left
= seg_right
;
2294 add_symbol
= op_symbol
;
2295 orig_add_symbol
= expressionP
->X_op_symbol
;
2299 else if ((op
== O_multiply
|| op
== O_divide
)
2300 && seg_right
== absolute_section
&& right
== 1)
2305 else if (!(left
== right
2306 && ((seg_left
== reg_section
&& seg_right
== reg_section
)
2307 || (seg_left
== undefined_section
2308 && seg_right
== undefined_section
2309 && add_symbol
== op_symbol
))))
2311 else if (op
== O_bit_and
|| op
== O_bit_inclusive_or
)
2316 else if (op
!= O_bit_exclusive_or
&& op
!= O_bit_or_not
)
2320 right
+= frag_off
/ OCTETS_PER_BYTE
;
2323 case O_add
: left
+= right
; break;
2324 case O_subtract
: left
-= right
; break;
2325 case O_multiply
: left
*= right
; break;
2329 left
= (offsetT
) left
/ (offsetT
) right
;
2334 left
= (offsetT
) left
% (offsetT
) right
;
2337 if (right
>= sizeof (left
) * CHAR_BIT
)
2343 if (right
>= sizeof (left
) * CHAR_BIT
)
2348 case O_bit_inclusive_or
: left
|= right
; break;
2349 case O_bit_or_not
: left
|= ~right
; break;
2350 case O_bit_exclusive_or
: left
^= right
; break;
2351 case O_bit_and
: left
&= right
; break;
2354 left
= (left
== right
2355 && seg_left
== seg_right
2356 && (finalize_syms
|| frag_left
== frag_right
)
2357 && (seg_left
!= undefined_section
2358 || add_symbol
== op_symbol
)
2359 ? ~ (valueT
) 0 : 0);
2364 left
= (offsetT
) left
< (offsetT
) right
? ~ (valueT
) 0 : 0;
2367 left
= (offsetT
) left
<= (offsetT
) right
? ~ (valueT
) 0 : 0;
2370 left
= (offsetT
) left
>= (offsetT
) right
? ~ (valueT
) 0 : 0;
2373 left
= (offsetT
) left
> (offsetT
) right
? ~ (valueT
) 0 : 0;
2375 case O_logical_and
: left
= left
&& right
; break;
2376 case O_logical_or
: left
= left
|| right
; break;
2386 if (seg_left
== absolute_section
)
2388 else if (seg_left
== reg_section
&& final_val
== 0)
2390 else if (!symbol_same_p (add_symbol
, orig_add_symbol
))
2392 expressionP
->X_add_symbol
= add_symbol
;
2394 expressionP
->X_op
= op
;
2396 if (op
== O_constant
|| op
== O_register
)
2398 expressionP
->X_add_number
= final_val
;
2403 /* "Look through" register equates. */
2404 void resolve_register (expressionS
*expP
)
2408 const expressionS
*e
= expP
;
2410 if (expP
->X_op
!= O_symbol
)
2415 sym
= e
->X_add_symbol
;
2416 acc
+= e
->X_add_number
;
2417 e
= symbol_get_value_expression (sym
);
2419 while (symbol_equated_p (sym
));
2421 if (e
->X_op
== O_register
)
2424 expP
->X_add_number
+= acc
;
2428 /* This lives here because it belongs equally in expr.c & read.c.
2429 expr.c is just a branch office read.c anyway, and putting it
2430 here lessens the crowd at read.c.
2432 Assume input_line_pointer is at start of symbol name, or the
2433 start of a double quote enclosed symbol name. Advance
2434 input_line_pointer past symbol name. Turn that character into a '\0',
2435 returning its former value, which may be the closing double quote.
2437 This allows a string compare (RMS wants symbol names to be strings)
2440 NOTE: The input buffer is further altered when adjacent strings are
2441 concatenated by the function. Callers caring about the original buffer
2442 contents will need to make a copy before calling here.
2444 There will always be a char following symbol name, because all good
2445 lines end in end-of-line. */
2448 get_symbol_name (char ** ilp_return
)
2452 * ilp_return
= input_line_pointer
;
2453 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2454 constructed string. */
2455 if (is_name_beginner (c
= *input_line_pointer
++)
2456 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2458 while (is_part_of_name (c
= *input_line_pointer
++)
2459 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2461 if (is_name_ender (c
))
2462 c
= *input_line_pointer
++;
2466 char *dst
= input_line_pointer
;
2468 * ilp_return
= input_line_pointer
;
2471 c
= *input_line_pointer
++;
2475 as_warn (_("missing closing '\"'"));
2481 char *ilp_save
= input_line_pointer
;
2484 if (*input_line_pointer
== '"')
2486 ++input_line_pointer
;
2489 input_line_pointer
= ilp_save
;
2494 switch (*input_line_pointer
)
2498 c
= *input_line_pointer
++;
2503 as_warn (_("'\\%c' in quoted symbol name; "
2504 "behavior may change in the future"),
2505 *input_line_pointer
);
2513 *--input_line_pointer
= 0;
2517 /* Replace the NUL character pointed to by input_line_pointer
2518 with C. If C is \" then advance past it. Return the character
2519 now pointed to by input_line_pointer. */
2522 restore_line_pointer (char c
)
2524 * input_line_pointer
= c
;
2526 c
= * ++ input_line_pointer
;
2531 get_single_number (void)
2534 operand (&exp
, expr_normal
);
2535 return exp
.X_add_number
;