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;
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;
1130 else if (!md_register_arithmetic
&& expressionP
->X_op
== O_register
)
1132 /* Convert to binary '+'. */
1133 expressionP
->X_op_symbol
= make_expr_symbol (expressionP
);
1134 expressionP
->X_add_symbol
= make_expr_symbol (&zero
);
1135 expressionP
->X_add_number
= 0;
1136 expressionP
->X_op
= O_add
;
1140 as_warn (_("Unary operator %c ignored because bad operand follows"),
1145 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1147 if (literal_prefix_dollar_hex
)
1149 /* $L is the start of a local label, not a hex constant. */
1150 if (* input_line_pointer
== 'L')
1152 integer_constant (16, expressionP
);
1161 /* '$' is the program counter when in MRI mode, or when
1162 DOLLAR_DOT is defined. */
1164 if (! flag_m68k_mri
)
1167 if (DOLLAR_AMBIGU
&& hex_p (*input_line_pointer
))
1169 /* In MRI mode and on Z80, '$' is also used as the prefix
1170 for a hexadecimal constant. */
1171 integer_constant (16, expressionP
);
1175 if (is_part_of_name (*input_line_pointer
))
1178 current_location (expressionP
);
1183 if (!is_part_of_name (*input_line_pointer
))
1185 current_location (expressionP
);
1188 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1189 && ! is_part_of_name (input_line_pointer
[8]))
1190 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1191 && ! is_part_of_name (input_line_pointer
[7])))
1195 start
= (input_line_pointer
[1] == 't'
1196 || input_line_pointer
[1] == 'T');
1197 input_line_pointer
+= start
? 8 : 7;
1200 /* Cover for the as_bad () invocations below. */
1201 expressionP
->X_op
= O_absent
;
1203 if (*input_line_pointer
!= '(')
1204 as_bad (_("syntax error in .startof. or .sizeof."));
1207 ++input_line_pointer
;
1209 c
= get_symbol_name (& name
);
1212 as_bad (_("expected symbol name"));
1213 (void) restore_line_pointer (c
);
1215 ++input_line_pointer
;
1219 expressionP
->X_op
= O_symbol
;
1220 expressionP
->X_add_symbol
= symbol_lookup_or_make (name
, start
);
1221 expressionP
->X_add_number
= 0;
1223 *input_line_pointer
= c
;
1224 SKIP_WHITESPACE_AFTER_NAME ();
1225 if (*input_line_pointer
!= ')')
1226 as_bad (_("syntax error in .startof. or .sizeof."));
1228 ++input_line_pointer
;
1239 /* Can't imagine any other kind of operand. */
1240 expressionP
->X_op
= O_absent
;
1241 input_line_pointer
--;
1246 if (! flag_m68k_mri
)
1248 integer_constant (2, expressionP
);
1252 if (! flag_m68k_mri
)
1254 integer_constant (8, expressionP
);
1258 if (! flag_m68k_mri
)
1261 /* In MRI mode, this is a floating point constant represented
1262 using hexadecimal digits. */
1264 ++input_line_pointer
;
1265 integer_constant (16, expressionP
);
1269 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1272 current_location (expressionP
);
1277 #if defined(md_need_index_operator) || defined(TC_M68K)
1280 if (is_name_beginner (c
) || c
== '"') /* Here if did not begin with a digit. */
1282 /* Identifier begins here.
1283 This is kludged for speed, so code is repeated. */
1285 -- input_line_pointer
;
1286 c
= get_symbol_name (&name
);
1290 operatorT op
= md_operator (name
, 1, &c
);
1295 restore_line_pointer (c
);
1299 restore_line_pointer (c
);
1303 restore_line_pointer (c
);
1307 as_bad (_("invalid use of operator \"%s\""), name
);
1313 if (op
!= O_absent
&& op
!= O_illegal
)
1315 restore_line_pointer (c
);
1316 expr (9, expressionP
, mode
);
1317 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1318 expressionP
->X_op_symbol
= NULL
;
1319 expressionP
->X_add_number
= 0;
1320 expressionP
->X_op
= op
;
1326 #ifdef md_parse_name
1327 /* This is a hook for the backend to parse certain names
1328 specially in certain contexts. If a name always has a
1329 specific value, it can often be handled by simply
1330 entering it in the symbol table. */
1331 if (md_parse_name (name
, expressionP
, mode
, &c
))
1333 restore_line_pointer (c
);
1338 symbolP
= symbol_find_or_make (name
);
1340 /* If we have an absolute symbol or a reg, then we know its
1342 segment
= S_GET_SEGMENT (symbolP
);
1343 if (mode
!= expr_defer
1344 && segment
== absolute_section
1345 && !S_FORCE_RELOC (symbolP
, 0))
1347 expressionP
->X_op
= O_constant
;
1348 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1350 else if (mode
!= expr_defer
&& segment
== reg_section
)
1352 expressionP
->X_op
= O_register
;
1353 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1357 expressionP
->X_op
= O_symbol
;
1358 expressionP
->X_add_symbol
= symbolP
;
1359 expressionP
->X_add_number
= 0;
1362 restore_line_pointer (c
);
1366 /* Let the target try to parse it. Success is indicated by changing
1367 the X_op field to something other than O_absent and pointing
1368 input_line_pointer past the expression. If it can't parse the
1369 expression, X_op and input_line_pointer should be unchanged. */
1370 expressionP
->X_op
= O_absent
;
1371 --input_line_pointer
;
1372 md_operand (expressionP
);
1373 if (expressionP
->X_op
== O_absent
)
1375 ++input_line_pointer
;
1376 as_bad (_("bad expression"));
1377 expressionP
->X_op
= O_constant
;
1378 expressionP
->X_add_number
= 0;
1384 /* It is more 'efficient' to clean up the expressionS when they are
1385 created. Doing it here saves lines of code. */
1386 clean_up_expression (expressionP
);
1387 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1388 know (*input_line_pointer
!= ' ');
1390 /* The PA port needs this information. */
1391 if (expressionP
->X_add_symbol
)
1392 symbol_mark_used (expressionP
->X_add_symbol
);
1394 if (mode
!= expr_defer
)
1396 expressionP
->X_add_symbol
1397 = symbol_clone_if_forward_ref (expressionP
->X_add_symbol
);
1398 expressionP
->X_op_symbol
1399 = symbol_clone_if_forward_ref (expressionP
->X_op_symbol
);
1402 switch (expressionP
->X_op
)
1405 return absolute_section
;
1407 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1413 /* Internal. Simplify a struct expression for use by expr (). */
1415 /* In: address of an expressionS.
1416 The X_op field of the expressionS may only take certain values.
1417 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1419 Out: expressionS may have been modified:
1420 Unused fields zeroed to help expr (). */
1423 clean_up_expression (expressionS
*expressionP
)
1425 switch (expressionP
->X_op
)
1429 expressionP
->X_add_number
= 0;
1434 expressionP
->X_add_symbol
= NULL
;
1439 expressionP
->X_op_symbol
= NULL
;
1446 /* Expression parser. */
1448 /* We allow an empty expression, and just assume (absolute,0) silently.
1449 Unary operators and parenthetical expressions are treated as operands.
1450 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1452 We used to do an aho/ullman shift-reduce parser, but the logic got so
1453 warped that I flushed it and wrote a recursive-descent parser instead.
1454 Now things are stable, would anybody like to write a fast parser?
1455 Most expressions are either register (which does not even reach here)
1456 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1457 So I guess it doesn't really matter how inefficient more complex expressions
1460 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1461 Also, we have consumed any leading or trailing spaces (operand does that)
1462 and done all intervening operators.
1464 This returns the segment of the result, which will be
1465 absolute_section or the segment of a symbol. */
1468 #define __ O_illegal
1470 #define O_SINGLE_EQ O_illegal
1473 /* Maps ASCII -> operators. */
1474 static const operatorT op_encoding
[256] = {
1475 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1476 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1478 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1479 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1480 __
, __
, __
, __
, __
, __
, __
, __
,
1481 __
, __
, __
, __
, O_lt
, O_SINGLE_EQ
, O_gt
, __
,
1482 __
, __
, __
, __
, __
, __
, __
, __
,
1483 __
, __
, __
, __
, __
, __
, __
, __
,
1484 __
, __
, __
, __
, __
, __
, __
, __
,
1486 #ifdef NEED_INDEX_OPERATOR
1491 __
, __
, O_bit_exclusive_or
, __
,
1492 __
, __
, __
, __
, __
, __
, __
, __
,
1493 __
, __
, __
, __
, __
, __
, __
, __
,
1494 __
, __
, __
, __
, __
, __
, __
, __
,
1495 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1497 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1498 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1499 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1500 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1501 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1502 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1503 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1504 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1508 0 operand, (expression)
1513 5 used for * / % in MRI mode
1518 static operator_rankT op_rank
[O_max
] = {
1523 0, /* O_symbol_rva */
1529 9, /* O_logical_not */
1533 8, /* O_left_shift */
1534 8, /* O_right_shift */
1535 7, /* O_bit_inclusive_or */
1536 7, /* O_bit_or_not */
1537 7, /* O_bit_exclusive_or */
1547 3, /* O_logical_and */
1548 2, /* O_logical_or */
1552 /* Unfortunately, in MRI mode for the m68k, multiplication and
1553 division have lower precedence than the bit wise operators. This
1554 function sets the operator precedences correctly for the current
1555 mode. Also, MRI uses a different bit_not operator, and this fixes
1558 #define STANDARD_MUL_PRECEDENCE 8
1559 #define MRI_MUL_PRECEDENCE 6
1562 expr_set_precedence (void)
1566 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1567 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1568 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1572 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1573 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1574 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1579 expr_set_rank (operatorT op
, operator_rankT rank
)
1581 gas_assert (op
>= O_md1
&& op
< ARRAY_SIZE (op_rank
));
1585 /* Initialize the expression parser. */
1590 expr_set_precedence ();
1592 /* Verify that X_op field is wide enough. */
1596 gas_assert (e
.X_op
== O_max
);
1599 memset (seen
, 0, sizeof seen
);
1600 memset (nr_seen
, 0, sizeof nr_seen
);
1601 expr_symbol_lines
= NULL
;
1607 for (size_t i
= 0; i
< ARRAY_SIZE (seen
); i
++)
1611 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1612 sets NUM_CHARS to the number of characters in the operator.
1613 Does not advance INPUT_LINE_POINTER. */
1615 static inline operatorT
1616 operatorf (int *num_chars
)
1621 c
= *input_line_pointer
& 0xff;
1624 if (is_end_of_line
[c
])
1628 if (is_name_beginner (c
))
1631 char ec
= get_symbol_name (& name
);
1633 ret
= md_operator (name
, 2, &ec
);
1637 *input_line_pointer
= ec
;
1638 input_line_pointer
= name
;
1643 as_bad (_("invalid use of operator \"%s\""), name
);
1647 *input_line_pointer
= ec
;
1648 *num_chars
= input_line_pointer
- name
;
1649 input_line_pointer
= name
;
1658 ret
= op_encoding
[c
];
1660 if (ret
== O_illegal
)
1662 char *start
= input_line_pointer
;
1664 ret
= md_operator (NULL
, 2, NULL
);
1665 if (ret
!= O_illegal
)
1666 *num_chars
= input_line_pointer
- start
;
1667 input_line_pointer
= start
;
1674 return op_encoding
[c
];
1677 switch (input_line_pointer
[1])
1680 return op_encoding
[c
];
1695 if (input_line_pointer
[1] != '=')
1696 return op_encoding
[c
];
1702 switch (input_line_pointer
[1])
1705 return op_encoding
[c
];
1707 ret
= O_right_shift
;
1717 switch (input_line_pointer
[1])
1720 /* We accept !! as equivalent to ^ for MRI compatibility. */
1722 return O_bit_exclusive_or
;
1724 /* We accept != as equivalent to <>. */
1729 return O_bit_inclusive_or
;
1730 return op_encoding
[c
];
1734 if (input_line_pointer
[1] != '|')
1735 return op_encoding
[c
];
1738 return O_logical_or
;
1741 if (input_line_pointer
[1] != '&')
1742 return op_encoding
[c
];
1745 return O_logical_and
;
1751 /* Implement "word-size + 1 bit" addition for
1752 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1753 is used so that the full range of unsigned word values and the full range of
1754 signed word values can be represented in an O_constant expression, which is
1755 useful e.g. for .sleb128 directives. */
1758 add_to_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1760 valueT ures
= resultP
->X_add_number
;
1761 valueT uamount
= amount
;
1763 resultP
->X_add_number
+= uamount
;
1765 resultP
->X_extrabit
^= rhs_highbit
;
1767 if (ures
+ uamount
< ures
)
1768 resultP
->X_extrabit
^= 1;
1771 /* Similarly, for subtraction. */
1774 subtract_from_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1776 valueT ures
= resultP
->X_add_number
;
1777 valueT uamount
= amount
;
1779 resultP
->X_add_number
-= uamount
;
1781 resultP
->X_extrabit
^= rhs_highbit
;
1784 resultP
->X_extrabit
^= 1;
1787 /* Parse an expression. */
1790 expr (int rankarg
, /* Larger # is higher rank. */
1791 expressionS
*resultP
, /* Deliver result here. */
1792 enum expr_mode mode
/* Controls behavior. */)
1794 operator_rankT rank
= (operator_rankT
) rankarg
;
1801 know (rankarg
>= 0);
1803 /* Save the value of dot for the fixup code. */
1806 dot_value
= frag_now_fix ();
1807 dot_frag
= frag_now
;
1810 retval
= operand (resultP
, mode
);
1812 /* operand () gobbles spaces. */
1813 know (*input_line_pointer
!= ' ');
1815 op_left
= operatorf (&op_chars
);
1816 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1821 input_line_pointer
+= op_chars
; /* -> after operator. */
1824 rightseg
= expr (op_rank
[(int) op_left
], &right
, mode
);
1825 if (right
.X_op
== O_absent
)
1827 as_warn (_("missing operand; zero assumed"));
1828 right
.X_op
= O_constant
;
1829 right
.X_add_number
= 0;
1830 right
.X_add_symbol
= NULL
;
1831 right
.X_op_symbol
= NULL
;
1834 know (*input_line_pointer
!= ' ');
1836 if (op_left
== O_index
)
1838 if (*input_line_pointer
!= ']')
1839 as_bad ("missing right bracket");
1842 ++input_line_pointer
;
1847 op_right
= operatorf (&op_chars
);
1849 know (op_right
== O_illegal
|| op_left
== O_index
1850 || op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1851 know ((int) op_left
>= (int) O_multiply
);
1853 know ((int) op_left
<= (int) O_index
);
1855 know ((int) op_left
< (int) O_max
);
1858 /* input_line_pointer->after right-hand quantity. */
1859 /* left-hand quantity in resultP. */
1860 /* right-hand quantity in right. */
1861 /* operator in op_left. */
1863 if (resultP
->X_op
== O_big
)
1865 if (resultP
->X_add_number
> 0)
1866 as_warn (_("left operand is a bignum; integer 0 assumed"));
1868 as_warn (_("left operand is a float; integer 0 assumed"));
1869 resultP
->X_op
= O_constant
;
1870 resultP
->X_add_number
= 0;
1871 resultP
->X_add_symbol
= NULL
;
1872 resultP
->X_op_symbol
= NULL
;
1874 if (right
.X_op
== O_big
)
1876 if (right
.X_add_number
> 0)
1877 as_warn (_("right operand is a bignum; integer 0 assumed"));
1879 as_warn (_("right operand is a float; integer 0 assumed"));
1880 right
.X_op
= O_constant
;
1881 right
.X_add_number
= 0;
1882 right
.X_add_symbol
= NULL
;
1883 right
.X_op_symbol
= NULL
;
1886 if (mode
== expr_defer
1887 && ((resultP
->X_add_symbol
!= NULL
1888 && S_IS_FORWARD_REF (resultP
->X_add_symbol
))
1889 || (right
.X_add_symbol
!= NULL
1890 && S_IS_FORWARD_REF (right
.X_add_symbol
))))
1893 /* Optimize common cases. */
1894 #ifdef md_optimize_expr
1895 if (md_optimize_expr (resultP
, op_left
, &right
))
1902 if (op_left
== O_add
&& right
.X_op
== O_constant
1903 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1906 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1908 /* This case comes up in PIC code. */
1909 else if (op_left
== O_subtract
1910 && right
.X_op
== O_symbol
1911 && resultP
->X_op
== O_symbol
1912 && retval
== rightseg
1913 #ifdef md_allow_local_subtract
1914 && md_allow_local_subtract (resultP
, & right
, rightseg
)
1916 && ((SEG_NORMAL (rightseg
)
1917 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
1918 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
1919 || right
.X_add_symbol
== resultP
->X_add_symbol
)
1920 && frag_offset_fixed_p (symbol_get_frag (resultP
->X_add_symbol
),
1921 symbol_get_frag (right
.X_add_symbol
),
1924 offsetT symval_diff
= S_GET_VALUE (resultP
->X_add_symbol
)
1925 - S_GET_VALUE (right
.X_add_symbol
);
1926 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1927 subtract_from_result (resultP
, frag_off
/ OCTETS_PER_BYTE
, 0);
1928 add_to_result (resultP
, symval_diff
, symval_diff
< 0);
1929 resultP
->X_op
= O_constant
;
1930 resultP
->X_add_symbol
= 0;
1932 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
1933 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1936 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1938 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
1939 && (md_register_arithmetic
|| right
.X_op
!= O_register
))
1942 resultP
->X_op
= right
.X_op
;
1943 resultP
->X_add_symbol
= right
.X_add_symbol
;
1944 resultP
->X_op_symbol
= right
.X_op_symbol
;
1945 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1948 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1950 /* Constant OP constant. */
1951 offsetT v
= right
.X_add_number
;
1952 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1954 as_warn (_("division by zero"));
1957 if ((valueT
) v
>= sizeof(valueT
) * CHAR_BIT
1958 && (op_left
== O_left_shift
|| op_left
== O_right_shift
))
1960 as_warn_value_out_of_range (_("shift count"), v
, 0,
1961 sizeof(valueT
) * CHAR_BIT
- 1,
1963 resultP
->X_add_number
= v
= 0;
1967 default: goto general
;
1969 /* Do the multiply as unsigned to silence ubsan. The
1970 result is of course the same when we throw away high
1971 bits of the result. */
1972 resultP
->X_add_number
*= (valueT
) v
;
1974 case O_divide
: resultP
->X_add_number
/= v
; break;
1975 case O_modulus
: resultP
->X_add_number
%= v
; break;
1977 /* We always use unsigned shifts. According to the ISO
1978 C standard, left shift of a signed type having a
1979 negative value is undefined behaviour, and right
1980 shift of a signed type having negative value is
1981 implementation defined. Left shift of a signed type
1982 when the result overflows is also undefined
1983 behaviour. So don't trigger ubsan warnings or rely
1984 on characteristics of the compiler. */
1985 resultP
->X_add_number
1986 = (valueT
) resultP
->X_add_number
<< (valueT
) v
;
1989 resultP
->X_add_number
1990 = (valueT
) resultP
->X_add_number
>> (valueT
) v
;
1992 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1993 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1994 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1995 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1996 /* Constant + constant (O_add) is handled by the
1997 previous if statement for constant + X, so is omitted
2000 subtract_from_result (resultP
, v
, 0);
2003 resultP
->X_add_number
=
2004 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
2007 resultP
->X_add_number
=
2008 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
2011 resultP
->X_add_number
=
2012 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
2015 resultP
->X_add_number
=
2016 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
2019 resultP
->X_add_number
=
2020 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
2023 resultP
->X_add_number
=
2024 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
2027 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
2030 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
2034 else if (resultP
->X_op
== O_symbol
2035 && right
.X_op
== O_symbol
2036 && (op_left
== O_add
2037 || op_left
== O_subtract
2038 || (resultP
->X_add_number
== 0
2039 && right
.X_add_number
== 0)))
2041 /* Symbol OP symbol. */
2042 resultP
->X_op
= op_left
;
2043 resultP
->X_op_symbol
= right
.X_add_symbol
;
2044 if (op_left
== O_add
)
2045 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
2046 else if (op_left
== O_subtract
)
2048 subtract_from_result (resultP
, right
.X_add_number
,
2050 if (retval
== rightseg
2051 && SEG_NORMAL (retval
)
2052 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
2053 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
2055 retval
= absolute_section
;
2056 rightseg
= absolute_section
;
2063 /* The general case. */
2064 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
2065 resultP
->X_op_symbol
= make_expr_symbol (&right
);
2066 resultP
->X_op
= op_left
;
2067 resultP
->X_add_number
= 0;
2068 resultP
->X_unsigned
= 1;
2069 resultP
->X_extrabit
= 0;
2072 if (retval
!= rightseg
)
2074 if (retval
== undefined_section
)
2076 else if (rightseg
== undefined_section
)
2078 else if (retval
== expr_section
)
2080 else if (rightseg
== expr_section
)
2082 else if (retval
== reg_section
)
2084 else if (rightseg
== reg_section
)
2086 else if (rightseg
== absolute_section
)
2088 else if (retval
== absolute_section
)
2091 else if (op_left
== O_subtract
)
2095 as_bad (_("operation combines symbols in different segments"));
2099 } /* While next operator is >= this rank. */
2101 /* The PA port needs this information. */
2102 if (resultP
->X_add_symbol
)
2103 symbol_mark_used (resultP
->X_add_symbol
);
2105 if (rank
== 0 && mode
== expr_evaluate
)
2106 resolve_expression (resultP
);
2108 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
2111 /* Resolve an expression without changing any symbols/sub-expressions
2115 resolve_expression (expressionS
*expressionP
)
2117 /* Help out with CSE. */
2118 valueT final_val
= expressionP
->X_add_number
;
2119 symbolS
*add_symbol
= expressionP
->X_add_symbol
;
2120 symbolS
*orig_add_symbol
= add_symbol
;
2121 symbolS
*op_symbol
= expressionP
->X_op_symbol
;
2122 operatorT op
= expressionP
->X_op
;
2124 segT seg_left
, seg_right
;
2125 fragS
*frag_left
, *frag_right
;
2140 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2148 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2151 if (seg_left
!= absolute_section
)
2154 if (op
== O_logical_not
)
2156 else if (op
== O_uminus
)
2168 case O_bit_inclusive_or
:
2170 case O_bit_exclusive_or
:
2182 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
)
2183 || !snapshot_symbol (&op_symbol
, &right
, &seg_right
, &frag_right
))
2186 /* Simplify addition or subtraction of a constant by folding the
2187 constant into X_add_number. */
2190 if (seg_right
== absolute_section
)
2196 else if (seg_left
== absolute_section
)
2200 seg_left
= seg_right
;
2201 add_symbol
= op_symbol
;
2202 orig_add_symbol
= expressionP
->X_op_symbol
;
2207 else if (op
== O_subtract
)
2209 if (seg_right
== absolute_section
)
2217 /* Equality and non-equality tests are permitted on anything.
2218 Subtraction, and other comparison operators are permitted if
2219 both operands are in the same section.
2220 Shifts by constant zero are permitted on anything.
2221 Multiplies, bit-ors, and bit-ands with constant zero are
2222 permitted on anything.
2223 Multiplies and divides by constant one are permitted on
2225 Binary operations with both operands being the same register
2226 or undefined symbol are permitted if the result doesn't depend
2228 Otherwise, both operands must be absolute. We already handled
2229 the case of addition or subtraction of a constant above. */
2231 if (!(seg_left
== absolute_section
2232 && seg_right
== absolute_section
)
2233 && !(op
== O_eq
|| op
== O_ne
)
2234 && !((op
== O_subtract
2235 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
2236 && seg_left
== seg_right
2238 || frag_offset_fixed_p (frag_left
, frag_right
, &frag_off
)
2240 && frag_gtoffset_p (left
, frag_left
,
2241 right
, frag_right
, &frag_off
)))
2242 && (seg_left
!= reg_section
|| left
== right
)
2243 && (seg_left
!= undefined_section
|| add_symbol
== op_symbol
)))
2245 if ((seg_left
== absolute_section
&& left
== 0)
2246 || (seg_right
== absolute_section
&& right
== 0))
2248 if (op
== O_bit_exclusive_or
|| op
== O_bit_inclusive_or
)
2250 if (!(seg_right
== absolute_section
&& right
== 0))
2252 seg_left
= seg_right
;
2254 add_symbol
= op_symbol
;
2255 orig_add_symbol
= expressionP
->X_op_symbol
;
2260 else if (op
== O_left_shift
|| op
== O_right_shift
)
2262 if (!(seg_left
== absolute_section
&& left
== 0))
2268 else if (op
!= O_multiply
2269 && op
!= O_bit_or_not
&& op
!= O_bit_and
)
2272 else if (op
== O_multiply
2273 && seg_left
== absolute_section
&& left
== 1)
2275 seg_left
= seg_right
;
2277 add_symbol
= op_symbol
;
2278 orig_add_symbol
= expressionP
->X_op_symbol
;
2282 else if ((op
== O_multiply
|| op
== O_divide
)
2283 && seg_right
== absolute_section
&& right
== 1)
2288 else if (!(left
== right
2289 && ((seg_left
== reg_section
&& seg_right
== reg_section
)
2290 || (seg_left
== undefined_section
2291 && seg_right
== undefined_section
2292 && add_symbol
== op_symbol
))))
2294 else if (op
== O_bit_and
|| op
== O_bit_inclusive_or
)
2299 else if (op
!= O_bit_exclusive_or
&& op
!= O_bit_or_not
)
2303 right
+= frag_off
/ OCTETS_PER_BYTE
;
2306 case O_add
: left
+= right
; break;
2307 case O_subtract
: left
-= right
; break;
2308 case O_multiply
: left
*= right
; break;
2312 left
= (offsetT
) left
/ (offsetT
) right
;
2317 left
= (offsetT
) left
% (offsetT
) right
;
2320 if (right
>= sizeof (left
) * CHAR_BIT
)
2326 if (right
>= sizeof (left
) * CHAR_BIT
)
2331 case O_bit_inclusive_or
: left
|= right
; break;
2332 case O_bit_or_not
: left
|= ~right
; break;
2333 case O_bit_exclusive_or
: left
^= right
; break;
2334 case O_bit_and
: left
&= right
; break;
2337 left
= (left
== right
2338 && seg_left
== seg_right
2339 && (finalize_syms
|| frag_left
== frag_right
)
2340 && (seg_left
!= undefined_section
2341 || add_symbol
== op_symbol
)
2342 ? ~ (valueT
) 0 : 0);
2347 left
= (offsetT
) left
< (offsetT
) right
? ~ (valueT
) 0 : 0;
2350 left
= (offsetT
) left
<= (offsetT
) right
? ~ (valueT
) 0 : 0;
2353 left
= (offsetT
) left
>= (offsetT
) right
? ~ (valueT
) 0 : 0;
2356 left
= (offsetT
) left
> (offsetT
) right
? ~ (valueT
) 0 : 0;
2358 case O_logical_and
: left
= left
&& right
; break;
2359 case O_logical_or
: left
= left
|| right
; break;
2369 if (seg_left
== absolute_section
)
2371 else if (seg_left
== reg_section
&& final_val
== 0)
2373 else if (!symbol_same_p (add_symbol
, orig_add_symbol
))
2375 expressionP
->X_add_symbol
= add_symbol
;
2377 expressionP
->X_op
= op
;
2379 if (op
== O_constant
|| op
== O_register
)
2381 expressionP
->X_add_number
= final_val
;
2386 /* This lives here because it belongs equally in expr.c & read.c.
2387 expr.c is just a branch office read.c anyway, and putting it
2388 here lessens the crowd at read.c.
2390 Assume input_line_pointer is at start of symbol name, or the
2391 start of a double quote enclosed symbol name.
2392 Advance input_line_pointer past symbol name.
2393 Turn that character into a '\0', returning its former value,
2394 which may be the closing double quote.
2395 This allows a string compare (RMS wants symbol names to be strings)
2397 There will always be a char following symbol name, because all good
2398 lines end in end-of-line. */
2401 get_symbol_name (char ** ilp_return
)
2405 * ilp_return
= input_line_pointer
;
2406 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2407 constructed string. */
2408 if (is_name_beginner (c
= *input_line_pointer
++)
2409 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2411 while (is_part_of_name (c
= *input_line_pointer
++)
2412 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2414 if (is_name_ender (c
))
2415 c
= *input_line_pointer
++;
2419 char *dst
= input_line_pointer
;
2421 * ilp_return
= input_line_pointer
;
2424 c
= *input_line_pointer
++;
2428 as_warn (_("missing closing '\"'"));
2434 char *ilp_save
= input_line_pointer
;
2437 if (*input_line_pointer
== '"')
2439 ++input_line_pointer
;
2442 input_line_pointer
= ilp_save
;
2447 switch (*input_line_pointer
)
2451 c
= *input_line_pointer
++;
2456 as_warn (_("'\\%c' in quoted symbol name; "
2457 "behavior may change in the future"),
2458 *input_line_pointer
);
2466 *--input_line_pointer
= 0;
2470 /* Replace the NUL character pointed to by input_line_pointer
2471 with C. If C is \" then advance past it. Return the character
2472 now pointed to by input_line_pointer. */
2475 restore_line_pointer (char c
)
2477 * input_line_pointer
= c
;
2479 c
= * ++ input_line_pointer
;
2484 get_single_number (void)
2487 operand (&exp
, expr_normal
);
2488 return exp
.X_add_number
;