1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2024 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 bool u_seen
= !tc_allow_U_suffix
;
542 /* PR 19910: Look for, and ignore, a U suffix to the number. */
543 if (!u_seen
&& (c
== 'U' || c
== 'u'))
545 c
= *input_line_pointer
++;
549 #ifndef tc_allow_L_suffix
550 #define tc_allow_L_suffix 1
552 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
553 if (tc_allow_L_suffix
&& (c
== 'L' || c
== 'l'))
555 c
= * input_line_pointer
++;
556 if (c
== 'L' || c
== 'l')
557 c
= *input_line_pointer
++;
558 if (!u_seen
&& (c
== 'U' || c
== 'u'))
559 c
= *input_line_pointer
++;
564 /* Here with number, in correct radix. c is the next char.
565 Note that unlike un*x, we allow "011f" "0x9f" to both mean
566 the same as the (conventional) "9f".
567 This is simply easier than checking for strict canonical
570 if (LOCAL_LABELS_FB
&& c
== 'b')
572 /* Backward ref to local label.
573 Because it is backward, expect it to be defined. */
574 /* Construct a local label. */
575 name
= fb_label_name (number
, 0);
577 /* Seen before, or symbol is defined: OK. */
578 symbolP
= symbol_find (name
);
579 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
581 expressionP
->X_op
= O_symbol
;
582 expressionP
->X_add_symbol
= symbolP
;
586 /* Either not seen or not defined. */
587 /* @@ Should print out the original string instead of
588 the parsed number. */
589 as_bad (_("backward ref to unknown label \"%d:\""),
591 expressionP
->X_op
= O_constant
;
594 expressionP
->X_add_number
= 0;
596 else if (LOCAL_LABELS_FB
&& c
== 'f')
598 /* Forward reference. Expect symbol to be undefined or
599 unknown. undefined: seen it before. unknown: never seen
602 Construct a local label name, then an undefined symbol.
603 Don't create a xseg frag for it: caller may do that.
604 Just return it as never seen before. */
605 name
= fb_label_name (number
, 1);
606 symbolP
= symbol_find_or_make (name
);
607 /* We have no need to check symbol properties. */
608 expressionP
->X_op
= O_symbol
;
609 expressionP
->X_add_symbol
= symbolP
;
610 expressionP
->X_add_number
= 0;
612 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
614 /* If the dollar label is *currently* defined, then this is just
615 another reference to it. If it is not *currently* defined,
616 then this is a fresh instantiation of that number, so create
619 if (dollar_label_defined (number
))
621 name
= dollar_label_name (number
, 0);
622 symbolP
= symbol_find (name
);
623 know (symbolP
!= NULL
);
627 name
= dollar_label_name (number
, 1);
628 symbolP
= symbol_find_or_make (name
);
631 expressionP
->X_op
= O_symbol
;
632 expressionP
->X_add_symbol
= symbolP
;
633 expressionP
->X_add_number
= 0;
637 expressionP
->X_op
= O_constant
;
638 expressionP
->X_add_number
= number
;
639 input_line_pointer
--; /* Restore following character. */
640 } /* Really just a number. */
644 /* Not a small number. */
645 expressionP
->X_op
= O_big
;
646 expressionP
->X_add_number
= number
; /* Number of littlenums. */
647 input_line_pointer
--; /* -> char following number. */
651 /* Parse an MRI multi character constant. */
654 mri_char_constant (expressionS
*expressionP
)
658 if (*input_line_pointer
== '\''
659 && input_line_pointer
[1] != '\'')
661 expressionP
->X_op
= O_constant
;
662 expressionP
->X_add_number
= 0;
666 /* In order to get the correct byte ordering, we must build the
667 number in reverse. */
668 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
672 generic_bignum
[i
] = 0;
673 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
675 if (*input_line_pointer
== '\'')
677 if (input_line_pointer
[1] != '\'')
679 ++input_line_pointer
;
681 generic_bignum
[i
] <<= 8;
682 generic_bignum
[i
] += *input_line_pointer
;
683 ++input_line_pointer
;
686 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
688 /* If there is more than one littlenum, left justify the
689 last one to make it match the earlier ones. If there is
690 only one, we can just use the value directly. */
691 for (; j
< CHARS_PER_LITTLENUM
; j
++)
692 generic_bignum
[i
] <<= 8;
695 if (*input_line_pointer
== '\''
696 && input_line_pointer
[1] != '\'')
702 as_bad (_("character constant too large"));
711 c
= SIZE_OF_LARGE_NUMBER
- i
;
712 for (j
= 0; j
< c
; j
++)
713 generic_bignum
[j
] = generic_bignum
[i
+ j
];
717 know (LITTLENUM_NUMBER_OF_BITS
== 16);
720 expressionP
->X_op
= O_big
;
721 expressionP
->X_add_number
= i
;
725 expressionP
->X_op
= O_constant
;
727 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
729 expressionP
->X_add_number
=
730 (((generic_bignum
[1] & LITTLENUM_MASK
)
731 << LITTLENUM_NUMBER_OF_BITS
)
732 | (generic_bignum
[0] & LITTLENUM_MASK
));
735 /* Skip the final closing quote. */
736 ++input_line_pointer
;
739 /* Return an expression representing the current location. This
740 handles the magic symbol `.'. */
743 current_location (expressionS
*expressionp
)
745 if (now_seg
== absolute_section
)
747 expressionp
->X_op
= O_constant
;
748 expressionp
->X_add_number
= abs_section_offset
;
752 expressionp
->X_op
= O_symbol
;
753 expressionp
->X_add_symbol
= &dot_symbol
;
754 expressionp
->X_add_number
= 0;
758 #ifndef md_register_arithmetic
759 # define md_register_arithmetic 1
762 /* In: Input_line_pointer points to 1st char of operand, which may
766 The operand may have been empty: in this case X_op == O_absent.
767 Input_line_pointer->(next non-blank) char after operand. */
770 operand (expressionS
*expressionP
, enum expr_mode mode
)
773 symbolS
*symbolP
; /* Points to symbol. */
774 char *name
; /* Points to name of symbol. */
776 operatorT op
= O_absent
; /* For unary operators. */
778 /* All integers are regarded as unsigned unless they are negated.
779 This is because the only thing which cares whether a number is
780 unsigned is the code in emit_expr which extends constants into
781 bignums. It should only sign extend negative numbers, so that
782 something like ``.quad 0x80000000'' is not sign extended even
783 though it appears negative if valueT is 32 bits. */
784 expressionP
->X_unsigned
= 1;
785 expressionP
->X_extrabit
= 0;
787 /* Digits, assume it is a bignum. */
789 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
790 c
= *input_line_pointer
++; /* input_line_pointer -> past char in c. */
792 if (is_end_of_line
[(unsigned char) c
])
806 input_line_pointer
--;
808 integer_constant ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
813 #ifdef LITERAL_PREFIXPERCENT_BIN
815 integer_constant (2, expressionP
);
820 /* Non-decimal radix. */
822 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
826 /* Check for a hex or float constant. */
827 for (s
= input_line_pointer
; hex_p (*s
); s
++)
829 if (*s
== 'h' || *s
== 'H' || *input_line_pointer
== '.')
831 --input_line_pointer
;
832 integer_constant (0, expressionP
);
836 c
= *input_line_pointer
;
845 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
847 integer_constant (0, expressionP
);
853 if (c
&& strchr (FLT_CHARS
, c
))
855 input_line_pointer
++;
856 floating_constant (expressionP
);
857 expressionP
->X_add_number
= - TOLOWER (c
);
861 /* The string was only zero. */
862 expressionP
->X_op
= O_constant
;
863 expressionP
->X_add_number
= 0;
872 input_line_pointer
++;
873 integer_constant (16, expressionP
);
877 if (LOCAL_LABELS_FB
&& !flag_m68k_mri
878 && input_line_pointer
[1] != '0'
879 && input_line_pointer
[1] != '1')
881 /* Parse this as a back reference to label 0. */
882 input_line_pointer
--;
883 integer_constant (10, expressionP
);
886 /* Otherwise, parse this as a binary number. */
889 if (input_line_pointer
[1] == '0'
890 || input_line_pointer
[1] == '1')
892 input_line_pointer
++;
893 integer_constant (2, expressionP
);
896 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
897 input_line_pointer
++;
902 /* Accept an L suffix to the zero. */
903 if (tc_allow_L_suffix
)
909 /* Accept a U suffix to the zero. */
910 if (!tc_allow_U_suffix
)
922 integer_constant ((flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
932 /* If it says "0f" and it could possibly be a floating point
933 number, make it one. Otherwise, make it a local label,
934 and try to deal with parsing the rest later. */
935 if (!is_end_of_line
[(unsigned char) input_line_pointer
[1]]
936 && strchr (FLT_CHARS
, 'f') != NULL
)
938 char *cp
= input_line_pointer
+ 1;
940 atof_generic (&cp
, ".", EXP_CHARS
,
941 &generic_floating_point_number
);
943 /* Was nothing parsed, or does it look like an
945 is_label
= (cp
== input_line_pointer
+ 1
946 || (cp
== input_line_pointer
+ 2
947 && (cp
[-1] == '-' || cp
[-1] == '+'))
953 input_line_pointer
--;
954 integer_constant (10, expressionP
);
962 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
964 integer_constant (0, expressionP
);
974 input_line_pointer
++;
975 floating_constant (expressionP
);
976 expressionP
->X_add_number
= - TOLOWER (c
);
980 if (LOCAL_LABELS_DOLLAR
)
982 integer_constant (10, expressionP
);
991 #ifndef NEED_INDEX_OPERATOR
993 # ifdef md_need_index_operator
994 if (md_need_index_operator())
1000 /* Didn't begin with digit & not a name. */
1001 segment
= expr (0, expressionP
, mode
);
1002 /* expression () will pass trailing whitespace. */
1003 if ((c
== '(' && *input_line_pointer
!= ')')
1004 || (c
== '[' && *input_line_pointer
!= ']'))
1006 if (* input_line_pointer
)
1007 as_bad (_("found '%c', expected: '%c'"),
1008 * input_line_pointer
, c
== '(' ? ')' : ']');
1010 as_bad (_("missing '%c'"), c
== '(' ? ')' : ']');
1013 input_line_pointer
++;
1014 SKIP_ALL_WHITESPACE ();
1015 /* Here with input_line_pointer -> char after "(...)". */
1020 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1022 as_bad (_("EBCDIC constants are not supported"));
1025 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1027 ++input_line_pointer
;
1031 if (! flag_m68k_mri
)
1033 /* Warning: to conform to other people's assemblers NO
1034 ESCAPEMENT is permitted for a single quote. The next
1035 character, parity errors and all, is taken as the value
1036 of the operand. VERY KINKY. */
1037 expressionP
->X_op
= O_constant
;
1038 expressionP
->X_add_number
= *input_line_pointer
++;
1042 mri_char_constant (expressionP
);
1047 /* Double quote is the bitwise not operator in MRI mode. */
1048 if (! flag_m68k_mri
)
1053 /* '~' is permitted to start a label on the Delta. */
1054 if (is_name_beginner (c
))
1069 operand (expressionP
, mode
);
1071 #ifdef md_optimize_expr
1072 if (md_optimize_expr (NULL
, op
, expressionP
))
1079 if (expressionP
->X_op
== O_constant
)
1081 /* input_line_pointer -> char after operand. */
1084 expressionP
->X_add_number
1085 = - (addressT
) expressionP
->X_add_number
;
1086 /* Notice: '-' may overflow: no warning is given.
1087 This is compatible with other people's
1088 assemblers. Sigh. */
1089 expressionP
->X_unsigned
= 0;
1090 if (expressionP
->X_add_number
)
1091 expressionP
->X_extrabit
^= 1;
1093 else if (op
== O_bit_not
)
1095 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1096 expressionP
->X_extrabit
^= 1;
1097 expressionP
->X_unsigned
= 0;
1099 else if (op
== O_logical_not
)
1101 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1102 expressionP
->X_unsigned
= 1;
1103 expressionP
->X_extrabit
= 0;
1106 else if (expressionP
->X_op
== O_big
1107 && expressionP
->X_add_number
<= 0
1109 && (generic_floating_point_number
.sign
== '+'
1110 || generic_floating_point_number
.sign
== 'P'))
1112 /* Negative flonum (eg, -1.000e0). */
1113 if (generic_floating_point_number
.sign
== '+')
1114 generic_floating_point_number
.sign
= '-';
1116 generic_floating_point_number
.sign
= 'N';
1118 else if (expressionP
->X_op
== O_big
1119 && expressionP
->X_add_number
> 0)
1123 if (op
== O_uminus
|| op
== O_bit_not
)
1125 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1126 generic_bignum
[i
] = ~generic_bignum
[i
];
1128 /* Extend the bignum to at least the size of .octa. */
1129 if (expressionP
->X_add_number
< SIZE_OF_LARGE_NUMBER
)
1131 expressionP
->X_add_number
= SIZE_OF_LARGE_NUMBER
;
1132 for (; i
< expressionP
->X_add_number
; ++i
)
1133 generic_bignum
[i
] = ~(LITTLENUM_TYPE
) 0;
1137 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1139 generic_bignum
[i
] += 1;
1140 if (generic_bignum
[i
])
1144 else if (op
== O_logical_not
)
1146 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1147 if (generic_bignum
[i
] != 0)
1149 expressionP
->X_add_number
= i
>= expressionP
->X_add_number
;
1150 expressionP
->X_op
= O_constant
;
1151 expressionP
->X_unsigned
= 1;
1152 expressionP
->X_extrabit
= 0;
1155 else if (expressionP
->X_op
!= O_illegal
1156 && expressionP
->X_op
!= O_absent
)
1160 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1161 expressionP
->X_op
= op
;
1162 expressionP
->X_add_number
= 0;
1164 else if (!md_register_arithmetic
&& expressionP
->X_op
== O_register
)
1166 /* Convert to binary '+'. */
1167 expressionP
->X_op_symbol
= make_expr_symbol (expressionP
);
1168 expressionP
->X_add_symbol
= make_expr_symbol (&zero
);
1169 expressionP
->X_add_number
= 0;
1170 expressionP
->X_op
= O_add
;
1174 as_warn (_("Unary operator %c ignored because bad operand follows"),
1179 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1181 if (literal_prefix_dollar_hex
)
1183 /* $L is the start of a local label, not a hex constant. */
1184 if (* input_line_pointer
== 'L')
1186 integer_constant (16, expressionP
);
1195 /* '$' is the program counter when in MRI mode, or when
1196 DOLLAR_DOT is defined. */
1198 if (! flag_m68k_mri
)
1201 if (DOLLAR_AMBIGU
&& hex_p (*input_line_pointer
))
1203 /* In MRI mode and on Z80, '$' is also used as the prefix
1204 for a hexadecimal constant. */
1205 integer_constant (16, expressionP
);
1209 if (is_part_of_name (*input_line_pointer
))
1212 current_location (expressionP
);
1217 if (!is_part_of_name (*input_line_pointer
))
1219 current_location (expressionP
);
1222 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1223 && ! is_part_of_name (input_line_pointer
[8]))
1224 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1225 && ! is_part_of_name (input_line_pointer
[7])))
1229 start
= (input_line_pointer
[1] == 't'
1230 || input_line_pointer
[1] == 'T');
1231 input_line_pointer
+= start
? 8 : 7;
1234 /* Cover for the as_bad () invocations below. */
1235 expressionP
->X_op
= O_absent
;
1237 if (*input_line_pointer
!= '(')
1238 as_bad (_("syntax error in .startof. or .sizeof."));
1241 ++input_line_pointer
;
1243 c
= get_symbol_name (& name
);
1246 as_bad (_("expected symbol name"));
1247 (void) restore_line_pointer (c
);
1249 ++input_line_pointer
;
1253 expressionP
->X_op
= O_symbol
;
1254 expressionP
->X_add_symbol
= symbol_lookup_or_make (name
, start
);
1255 expressionP
->X_add_number
= 0;
1257 *input_line_pointer
= c
;
1258 SKIP_WHITESPACE_AFTER_NAME ();
1259 if (*input_line_pointer
!= ')')
1260 as_bad (_("syntax error in .startof. or .sizeof."));
1262 ++input_line_pointer
;
1273 /* Can't imagine any other kind of operand. */
1274 expressionP
->X_op
= O_absent
;
1275 input_line_pointer
--;
1280 if (! flag_m68k_mri
)
1282 integer_constant (2, expressionP
);
1286 if (! flag_m68k_mri
)
1288 integer_constant (8, expressionP
);
1292 if (! flag_m68k_mri
)
1295 /* In MRI mode, this is a floating point constant represented
1296 using hexadecimal digits. */
1298 ++input_line_pointer
;
1299 integer_constant (16, expressionP
);
1303 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1306 current_location (expressionP
);
1311 #if defined(md_need_index_operator) || defined(TC_M68K)
1314 if (is_name_beginner (c
) || c
== '"') /* Here if did not begin with a digit. */
1316 /* Identifier begins here.
1317 This is kludged for speed, so code is repeated. */
1319 -- input_line_pointer
;
1320 c
= get_symbol_name (&name
);
1324 op
= md_operator (name
, 1, &c
);
1328 restore_line_pointer (c
);
1332 restore_line_pointer (c
);
1336 restore_line_pointer (c
);
1340 as_bad (_("invalid use of operator \"%s\""), name
);
1346 if (op
!= O_absent
&& op
!= O_illegal
)
1348 restore_line_pointer (c
);
1349 expr (9, expressionP
, mode
);
1350 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1351 expressionP
->X_op_symbol
= NULL
;
1352 expressionP
->X_add_number
= 0;
1353 expressionP
->X_op
= op
;
1359 #ifdef md_parse_name
1360 /* This is a hook for the backend to parse certain names
1361 specially in certain contexts. If a name always has a
1362 specific value, it can often be handled by simply
1363 entering it in the symbol table. */
1364 if (md_parse_name (name
, expressionP
, mode
, &c
))
1366 restore_line_pointer (c
);
1371 symbolP
= symbol_find_or_make (name
);
1373 /* If we have an absolute symbol or a reg, then we know its
1375 segment
= S_GET_SEGMENT (symbolP
);
1376 if (mode
!= expr_defer
1377 && segment
== absolute_section
1378 && !S_FORCE_RELOC (symbolP
, 0))
1380 expressionP
->X_op
= O_constant
;
1381 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1383 else if (mode
!= expr_defer
&& segment
== reg_section
)
1385 expressionP
->X_op
= O_register
;
1386 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1390 expressionP
->X_op
= O_symbol
;
1391 expressionP
->X_add_symbol
= symbolP
;
1392 expressionP
->X_add_number
= 0;
1395 restore_line_pointer (c
);
1399 /* Let the target try to parse it. Success is indicated by changing
1400 the X_op field to something other than O_absent and pointing
1401 input_line_pointer past the expression. If it can't parse the
1402 expression, X_op and input_line_pointer should be unchanged. */
1403 expressionP
->X_op
= O_absent
;
1404 --input_line_pointer
;
1405 md_operand (expressionP
);
1406 if (expressionP
->X_op
== O_absent
)
1408 ++input_line_pointer
;
1409 as_bad (_("bad expression"));
1410 expressionP
->X_op
= O_constant
;
1411 expressionP
->X_add_number
= 0;
1417 /* It is more 'efficient' to clean up the expressionS when they are
1418 created. Doing it here saves lines of code. */
1419 clean_up_expression (expressionP
);
1420 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1421 know (*input_line_pointer
!= ' ');
1423 /* The PA port needs this information. */
1424 if (expressionP
->X_add_symbol
)
1425 symbol_mark_used (expressionP
->X_add_symbol
);
1427 if (mode
!= expr_defer
)
1429 expressionP
->X_add_symbol
1430 = symbol_clone_if_forward_ref (expressionP
->X_add_symbol
);
1431 expressionP
->X_op_symbol
1432 = symbol_clone_if_forward_ref (expressionP
->X_op_symbol
);
1435 switch (expressionP
->X_op
)
1438 return absolute_section
;
1440 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1446 /* Internal. Simplify a struct expression for use by expr (). */
1448 /* In: address of an expressionS.
1449 The X_op field of the expressionS may only take certain values.
1450 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1452 Out: expressionS may have been modified:
1453 Unused fields zeroed to help expr (). */
1456 clean_up_expression (expressionS
*expressionP
)
1458 switch (expressionP
->X_op
)
1462 expressionP
->X_add_number
= 0;
1467 expressionP
->X_add_symbol
= NULL
;
1472 expressionP
->X_op_symbol
= NULL
;
1479 /* Expression parser. */
1481 /* We allow an empty expression, and just assume (absolute,0) silently.
1482 Unary operators and parenthetical expressions are treated as operands.
1483 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1485 We used to do an aho/ullman shift-reduce parser, but the logic got so
1486 warped that I flushed it and wrote a recursive-descent parser instead.
1487 Now things are stable, would anybody like to write a fast parser?
1488 Most expressions are either register (which does not even reach here)
1489 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1490 So I guess it doesn't really matter how inefficient more complex expressions
1493 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1494 Also, we have consumed any leading or trailing spaces (operand does that)
1495 and done all intervening operators.
1497 This returns the segment of the result, which will be
1498 absolute_section or the segment of a symbol. */
1501 #define __ O_illegal
1503 #define O_SINGLE_EQ O_illegal
1506 /* Maps ASCII -> operators. */
1507 static const operatorT op_encoding
[256] = {
1508 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1509 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1511 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1512 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1513 __
, __
, __
, __
, __
, __
, __
, __
,
1514 __
, __
, __
, __
, O_lt
, O_SINGLE_EQ
, O_gt
, __
,
1515 __
, __
, __
, __
, __
, __
, __
, __
,
1516 __
, __
, __
, __
, __
, __
, __
, __
,
1517 __
, __
, __
, __
, __
, __
, __
, __
,
1519 #ifdef NEED_INDEX_OPERATOR
1524 __
, __
, O_bit_exclusive_or
, __
,
1525 __
, __
, __
, __
, __
, __
, __
, __
,
1526 __
, __
, __
, __
, __
, __
, __
, __
,
1527 __
, __
, __
, __
, __
, __
, __
, __
,
1528 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1530 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1531 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1532 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1533 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1534 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1535 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1536 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1537 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1541 0 operand, (expression)
1546 5 used for * / % in MRI mode
1551 static operator_rankT op_rank
[O_max
] = {
1556 0, /* O_symbol_rva */
1562 9, /* O_logical_not */
1566 8, /* O_left_shift */
1567 8, /* O_right_shift */
1568 7, /* O_bit_inclusive_or */
1569 7, /* O_bit_or_not */
1570 7, /* O_bit_exclusive_or */
1580 3, /* O_logical_and */
1581 2, /* O_logical_or */
1585 /* Unfortunately, in MRI mode for the m68k, multiplication and
1586 division have lower precedence than the bit wise operators. This
1587 function sets the operator precedences correctly for the current
1588 mode. Also, MRI uses a different bit_not operator, and this fixes
1591 #define STANDARD_MUL_PRECEDENCE 8
1592 #define MRI_MUL_PRECEDENCE 6
1595 expr_set_precedence (void)
1599 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1600 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1601 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1605 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1606 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1607 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1612 expr_set_rank (operatorT op
, operator_rankT rank
)
1614 gas_assert (op
>= O_md1
&& op
< ARRAY_SIZE (op_rank
));
1618 /* Initialize the expression parser. */
1623 expr_set_precedence ();
1625 /* Verify that X_op field is wide enough. */
1629 gas_assert (e
.X_op
== O_max
);
1632 memset (seen
, 0, sizeof seen
);
1633 memset (nr_seen
, 0, sizeof nr_seen
);
1634 expr_symbol_lines
= NULL
;
1640 for (size_t i
= 0; i
< ARRAY_SIZE (seen
); i
++)
1644 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1645 sets NUM_CHARS to the number of characters in the operator.
1646 Does not advance INPUT_LINE_POINTER. */
1648 static inline operatorT
1649 operatorf (int *num_chars
)
1654 c
= *input_line_pointer
& 0xff;
1657 if (is_end_of_line
[c
])
1661 if (is_name_beginner (c
))
1664 char ec
= get_symbol_name (& name
);
1666 ret
= md_operator (name
, 2, &ec
);
1670 *input_line_pointer
= ec
;
1671 input_line_pointer
= name
;
1676 as_bad (_("invalid use of operator \"%s\""), name
);
1680 *input_line_pointer
= ec
;
1681 *num_chars
= input_line_pointer
- name
;
1682 input_line_pointer
= name
;
1691 ret
= op_encoding
[c
];
1693 if (ret
== O_illegal
)
1695 char *start
= input_line_pointer
;
1697 ret
= md_operator (NULL
, 2, NULL
);
1698 if (ret
!= O_illegal
)
1699 *num_chars
= input_line_pointer
- start
;
1700 input_line_pointer
= start
;
1707 return op_encoding
[c
];
1710 switch (input_line_pointer
[1])
1713 return op_encoding
[c
];
1728 if (input_line_pointer
[1] != '=')
1729 return op_encoding
[c
];
1735 switch (input_line_pointer
[1])
1738 return op_encoding
[c
];
1740 ret
= O_right_shift
;
1750 switch (input_line_pointer
[1])
1753 /* We accept !! as equivalent to ^ for MRI compatibility. */
1755 return O_bit_exclusive_or
;
1757 /* We accept != as equivalent to <>. */
1762 return O_bit_inclusive_or
;
1763 return op_encoding
[c
];
1767 if (input_line_pointer
[1] != '|')
1768 return op_encoding
[c
];
1771 return O_logical_or
;
1774 if (input_line_pointer
[1] != '&')
1775 return op_encoding
[c
];
1778 return O_logical_and
;
1784 /* Implement "word-size + 1 bit" addition for
1785 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1786 is used so that the full range of unsigned word values and the full range of
1787 signed word values can be represented in an O_constant expression, which is
1788 useful e.g. for .sleb128 directives. */
1791 add_to_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1793 valueT ures
= resultP
->X_add_number
;
1794 valueT uamount
= amount
;
1796 resultP
->X_add_number
+= uamount
;
1798 resultP
->X_extrabit
^= rhs_highbit
;
1800 if (ures
+ uamount
< ures
)
1801 resultP
->X_extrabit
^= 1;
1804 /* Similarly, for subtraction. */
1807 subtract_from_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1809 valueT ures
= resultP
->X_add_number
;
1810 valueT uamount
= amount
;
1812 resultP
->X_add_number
-= uamount
;
1814 resultP
->X_extrabit
^= rhs_highbit
;
1817 resultP
->X_extrabit
^= 1;
1820 /* Parse an expression. */
1823 expr (int rankarg
, /* Larger # is higher rank. */
1824 expressionS
*resultP
, /* Deliver result here. */
1825 enum expr_mode mode
/* Controls behavior. */)
1827 operator_rankT rank
= (operator_rankT
) rankarg
;
1834 know (rankarg
>= 0);
1836 /* Save the value of dot for the fixup code. */
1839 dot_value
= frag_now_fix ();
1840 dot_frag
= frag_now
;
1843 retval
= operand (resultP
, mode
);
1845 /* operand () gobbles spaces. */
1846 know (*input_line_pointer
!= ' ');
1848 op_left
= operatorf (&op_chars
);
1849 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1855 input_line_pointer
+= op_chars
; /* -> after operator. */
1858 rightseg
= expr (op_rank
[(int) op_left
], &right
, mode
);
1859 if (right
.X_op
== O_absent
)
1861 as_warn (_("missing operand; zero assumed"));
1862 right
.X_op
= O_constant
;
1863 right
.X_add_number
= 0;
1864 right
.X_add_symbol
= NULL
;
1865 right
.X_op_symbol
= NULL
;
1868 know (*input_line_pointer
!= ' ');
1870 if (op_left
== O_index
)
1872 if (*input_line_pointer
!= ']')
1873 as_bad ("missing right bracket");
1876 ++input_line_pointer
;
1881 op_right
= operatorf (&op_chars
);
1883 know (op_right
== O_illegal
|| op_left
== O_index
1884 || op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1885 know ((int) op_left
>= (int) O_multiply
);
1887 know ((int) op_left
<= (int) O_index
);
1889 know ((int) op_left
< (int) O_max
);
1892 /* input_line_pointer->after right-hand quantity. */
1893 /* left-hand quantity in resultP. */
1894 /* right-hand quantity in right. */
1895 /* operator in op_left. */
1897 if (resultP
->X_op
== O_big
)
1899 if (resultP
->X_add_number
> 0)
1900 as_warn (_("left operand is a bignum; integer 0 assumed"));
1902 as_warn (_("left operand is a float; integer 0 assumed"));
1903 resultP
->X_op
= O_constant
;
1904 resultP
->X_add_number
= 0;
1905 resultP
->X_add_symbol
= NULL
;
1906 resultP
->X_op_symbol
= NULL
;
1908 if (right
.X_op
== O_big
)
1910 if (right
.X_add_number
> 0)
1911 as_warn (_("right operand is a bignum; integer 0 assumed"));
1913 as_warn (_("right operand is a float; integer 0 assumed"));
1914 right
.X_op
= O_constant
;
1915 right
.X_add_number
= 0;
1916 right
.X_add_symbol
= NULL
;
1917 right
.X_op_symbol
= NULL
;
1920 is_unsigned
= resultP
->X_unsigned
&& right
.X_unsigned
;
1922 if (mode
== expr_defer
1923 && ((resultP
->X_add_symbol
!= NULL
1924 && S_IS_FORWARD_REF (resultP
->X_add_symbol
))
1925 || (right
.X_add_symbol
!= NULL
1926 && S_IS_FORWARD_REF (right
.X_add_symbol
))))
1929 /* Optimize common cases. */
1930 #ifdef md_optimize_expr
1931 if (md_optimize_expr (resultP
, op_left
, &right
))
1934 is_unsigned
= resultP
->X_unsigned
;
1938 if (op_left
== O_add
&& right
.X_op
== O_constant
1939 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1942 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1944 /* This case comes up in PIC code. */
1945 else if (op_left
== O_subtract
1946 && right
.X_op
== O_symbol
1947 && resultP
->X_op
== O_symbol
1948 && retval
== rightseg
1949 #ifdef md_allow_local_subtract
1950 && md_allow_local_subtract (resultP
, & right
, rightseg
)
1952 && ((SEG_NORMAL (rightseg
)
1953 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
1954 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
1955 || right
.X_add_symbol
== resultP
->X_add_symbol
)
1956 && frag_offset_fixed_p (symbol_get_frag (resultP
->X_add_symbol
),
1957 symbol_get_frag (right
.X_add_symbol
),
1960 offsetT symval_diff
= S_GET_VALUE (resultP
->X_add_symbol
)
1961 - S_GET_VALUE (right
.X_add_symbol
);
1962 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1963 subtract_from_result (resultP
, frag_off
/ OCTETS_PER_BYTE
, 0);
1964 add_to_result (resultP
, symval_diff
, symval_diff
< 0);
1965 resultP
->X_op
= O_constant
;
1966 resultP
->X_add_symbol
= 0;
1967 is_unsigned
= false;
1969 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
1970 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1973 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1974 is_unsigned
= false;
1976 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
1977 && (md_register_arithmetic
|| right
.X_op
!= O_register
))
1980 resultP
->X_op
= right
.X_op
;
1981 resultP
->X_add_symbol
= right
.X_add_symbol
;
1982 resultP
->X_op_symbol
= right
.X_op_symbol
;
1983 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1986 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1988 /* Constant OP constant. */
1989 offsetT v
= right
.X_add_number
;
1990 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1992 as_warn (_("division by zero"));
1997 default: goto general
;
1999 /* Do the multiply as unsigned to silence ubsan. The
2000 result is of course the same when we throw away high
2001 bits of the result. */
2002 resultP
->X_add_number
*= (valueT
) v
;
2004 case O_divide
: resultP
->X_add_number
/= v
; break;
2005 case O_modulus
: resultP
->X_add_number
%= v
; break;
2008 /* We always use unsigned shifts. According to the ISO
2009 C standard, left shift of a signed type having a
2010 negative value is undefined behaviour, and right
2011 shift of a signed type having negative value is
2012 implementation defined. Left shift of a signed type
2013 when the result overflows is also undefined
2014 behaviour. So don't trigger ubsan warnings or rely
2015 on characteristics of the compiler. */
2016 if ((valueT
) v
>= sizeof (valueT
) * CHAR_BIT
)
2018 as_warn_value_out_of_range (_("shift count"), v
, 0,
2019 sizeof (valueT
) * CHAR_BIT
- 1,
2021 resultP
->X_add_number
= 0;
2023 else if (op_left
== O_left_shift
)
2024 resultP
->X_add_number
2025 = (valueT
) resultP
->X_add_number
<< (valueT
) v
;
2027 resultP
->X_add_number
2028 = (valueT
) resultP
->X_add_number
>> (valueT
) v
;
2029 is_unsigned
= resultP
->X_unsigned
;
2031 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
2032 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
2033 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
2034 case O_bit_and
: resultP
->X_add_number
&= v
; break;
2035 /* Constant + constant (O_add) is handled by the
2036 previous if statement for constant + X, so is omitted
2039 subtract_from_result (resultP
, v
, 0);
2040 is_unsigned
= false;
2043 resultP
->X_add_number
=
2044 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
2045 is_unsigned
= false;
2048 resultP
->X_add_number
=
2049 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
2050 is_unsigned
= false;
2053 resultP
->X_add_number
=
2054 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
2055 is_unsigned
= false;
2058 resultP
->X_add_number
=
2059 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
2060 is_unsigned
= false;
2063 resultP
->X_add_number
=
2064 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
2065 is_unsigned
= false;
2068 resultP
->X_add_number
=
2069 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
2070 is_unsigned
= false;
2073 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
2077 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
2082 else if (resultP
->X_op
== O_symbol
2083 && right
.X_op
== O_symbol
2084 && (op_left
== O_add
2085 || op_left
== O_subtract
2086 || (resultP
->X_add_number
== 0
2087 && right
.X_add_number
== 0)))
2089 /* Symbol OP symbol. */
2090 resultP
->X_op
= op_left
;
2091 resultP
->X_op_symbol
= right
.X_add_symbol
;
2092 if (op_left
== O_add
)
2093 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
2094 else if (op_left
== O_subtract
)
2096 subtract_from_result (resultP
, right
.X_add_number
,
2098 if (retval
== rightseg
2099 && SEG_NORMAL (retval
)
2100 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
2101 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
2103 retval
= absolute_section
;
2104 rightseg
= absolute_section
;
2111 /* The general case. */
2112 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
2113 resultP
->X_op_symbol
= make_expr_symbol (&right
);
2114 resultP
->X_op
= op_left
;
2115 resultP
->X_add_number
= 0;
2116 resultP
->X_extrabit
= 0;
2119 resultP
->X_unsigned
= is_unsigned
;
2121 if (retval
!= rightseg
)
2123 if (retval
== undefined_section
)
2125 else if (rightseg
== undefined_section
)
2127 else if (retval
== expr_section
)
2129 else if (rightseg
== expr_section
)
2131 else if (retval
== reg_section
)
2133 else if (rightseg
== reg_section
)
2135 else if (rightseg
== absolute_section
)
2137 else if (retval
== absolute_section
)
2140 else if (op_left
== O_subtract
)
2144 as_bad (_("operation combines symbols in different segments"));
2148 } /* While next operator is >= this rank. */
2150 /* The PA port needs this information. */
2151 if (resultP
->X_add_symbol
)
2152 symbol_mark_used (resultP
->X_add_symbol
);
2154 if (rank
== 0 && mode
== expr_evaluate
)
2155 resolve_expression (resultP
);
2157 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
2160 /* Resolve an expression without changing any symbols/sub-expressions
2164 resolve_expression (expressionS
*expressionP
)
2166 /* Help out with CSE. */
2167 valueT final_val
= expressionP
->X_add_number
;
2168 symbolS
*add_symbol
= expressionP
->X_add_symbol
;
2169 symbolS
*orig_add_symbol
= add_symbol
;
2170 symbolS
*op_symbol
= expressionP
->X_op_symbol
;
2171 operatorT op
= expressionP
->X_op
;
2173 segT seg_left
, seg_right
;
2174 fragS
*frag_left
, *frag_right
;
2189 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2197 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2200 if (seg_left
!= absolute_section
)
2203 if (op
== O_logical_not
)
2205 else if (op
== O_uminus
)
2217 case O_bit_inclusive_or
:
2219 case O_bit_exclusive_or
:
2231 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
)
2232 || !snapshot_symbol (&op_symbol
, &right
, &seg_right
, &frag_right
))
2235 /* Simplify addition or subtraction of a constant by folding the
2236 constant into X_add_number. */
2239 if (seg_right
== absolute_section
)
2245 else if (seg_left
== absolute_section
)
2249 seg_left
= seg_right
;
2250 add_symbol
= op_symbol
;
2251 orig_add_symbol
= expressionP
->X_op_symbol
;
2256 else if (op
== O_subtract
)
2258 if (seg_right
== absolute_section
)
2266 /* Equality and non-equality tests are permitted on anything.
2267 Subtraction, and other comparison operators are permitted if
2268 both operands are in the same section.
2269 Shifts by constant zero are permitted on anything.
2270 Multiplies, bit-ors, and bit-ands with constant zero are
2271 permitted on anything.
2272 Multiplies and divides by constant one are permitted on
2274 Binary operations with both operands being the same register
2275 or undefined symbol are permitted if the result doesn't depend
2277 Otherwise, both operands must be absolute. We already handled
2278 the case of addition or subtraction of a constant above. */
2280 if (!(seg_left
== absolute_section
2281 && seg_right
== absolute_section
)
2282 && !(op
== O_eq
|| op
== O_ne
)
2283 && !((op
== O_subtract
2284 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
2285 && seg_left
== seg_right
2287 || frag_offset_fixed_p (frag_left
, frag_right
, &frag_off
)
2289 && frag_gtoffset_p (left
, frag_left
,
2290 right
, frag_right
, &frag_off
)))
2291 && (seg_left
!= reg_section
|| left
== right
)
2292 && (seg_left
!= undefined_section
|| add_symbol
== op_symbol
)))
2294 if ((seg_left
== absolute_section
&& left
== 0)
2295 || (seg_right
== absolute_section
&& right
== 0))
2297 if (op
== O_bit_exclusive_or
|| op
== O_bit_inclusive_or
)
2299 if (!(seg_right
== absolute_section
&& right
== 0))
2301 seg_left
= seg_right
;
2303 add_symbol
= op_symbol
;
2304 orig_add_symbol
= expressionP
->X_op_symbol
;
2309 else if (op
== O_left_shift
|| op
== O_right_shift
)
2311 if (!(seg_left
== absolute_section
&& left
== 0))
2317 else if (op
!= O_multiply
2318 && op
!= O_bit_or_not
&& op
!= O_bit_and
)
2321 else if (op
== O_multiply
2322 && seg_left
== absolute_section
&& left
== 1)
2324 seg_left
= seg_right
;
2326 add_symbol
= op_symbol
;
2327 orig_add_symbol
= expressionP
->X_op_symbol
;
2331 else if ((op
== O_multiply
|| op
== O_divide
)
2332 && seg_right
== absolute_section
&& right
== 1)
2337 else if (!(left
== right
2338 && ((seg_left
== reg_section
&& seg_right
== reg_section
)
2339 || (seg_left
== undefined_section
2340 && seg_right
== undefined_section
2341 && add_symbol
== op_symbol
))))
2343 else if (op
== O_bit_and
|| op
== O_bit_inclusive_or
)
2348 else if (op
!= O_bit_exclusive_or
&& op
!= O_bit_or_not
)
2352 right
+= frag_off
/ OCTETS_PER_BYTE
;
2355 case O_add
: left
+= right
; break;
2356 case O_subtract
: left
-= right
; break;
2357 case O_multiply
: left
*= right
; break;
2361 left
= (offsetT
) left
/ (offsetT
) right
;
2366 left
= (offsetT
) left
% (offsetT
) right
;
2369 if (right
>= sizeof (left
) * CHAR_BIT
)
2375 if (right
>= sizeof (left
) * CHAR_BIT
)
2380 case O_bit_inclusive_or
: left
|= right
; break;
2381 case O_bit_or_not
: left
|= ~right
; break;
2382 case O_bit_exclusive_or
: left
^= right
; break;
2383 case O_bit_and
: left
&= right
; break;
2386 left
= (left
== right
2387 && seg_left
== seg_right
2388 && (finalize_syms
|| frag_left
== frag_right
)
2389 && (seg_left
!= undefined_section
2390 || add_symbol
== op_symbol
)
2391 ? ~ (valueT
) 0 : 0);
2396 left
= (offsetT
) left
< (offsetT
) right
? ~ (valueT
) 0 : 0;
2399 left
= (offsetT
) left
<= (offsetT
) right
? ~ (valueT
) 0 : 0;
2402 left
= (offsetT
) left
>= (offsetT
) right
? ~ (valueT
) 0 : 0;
2405 left
= (offsetT
) left
> (offsetT
) right
? ~ (valueT
) 0 : 0;
2407 case O_logical_and
: left
= left
&& right
; break;
2408 case O_logical_or
: left
= left
|| right
; break;
2418 if (seg_left
== absolute_section
)
2420 else if (seg_left
== reg_section
&& final_val
== 0)
2422 else if (!symbol_same_p (add_symbol
, orig_add_symbol
))
2424 expressionP
->X_add_symbol
= add_symbol
;
2426 expressionP
->X_op
= op
;
2428 if (op
== O_constant
|| op
== O_register
)
2430 expressionP
->X_add_number
= final_val
;
2435 /* "Look through" register equates. */
2436 void resolve_register (expressionS
*expP
)
2440 const expressionS
*e
= expP
;
2442 if (expP
->X_op
!= O_symbol
)
2447 sym
= e
->X_add_symbol
;
2448 acc
+= e
->X_add_number
;
2449 e
= symbol_get_value_expression (sym
);
2451 while (symbol_equated_p (sym
));
2453 if (e
->X_op
== O_register
)
2456 expP
->X_add_number
+= acc
;
2460 /* This lives here because it belongs equally in expr.c & read.c.
2461 expr.c is just a branch office read.c anyway, and putting it
2462 here lessens the crowd at read.c.
2464 Assume input_line_pointer is at start of symbol name, or the
2465 start of a double quote enclosed symbol name. Advance
2466 input_line_pointer past symbol name. Turn that character into a '\0',
2467 returning its former value, which may be the closing double quote.
2469 This allows a string compare (RMS wants symbol names to be strings)
2472 NOTE: The input buffer is further altered when adjacent strings are
2473 concatenated by the function. Callers caring about the original buffer
2474 contents will need to make a copy before calling here.
2476 There will always be a char following symbol name, because all good
2477 lines end in end-of-line. */
2480 get_symbol_name (char ** ilp_return
)
2484 * ilp_return
= input_line_pointer
;
2485 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2486 constructed string. */
2487 if (is_name_beginner (c
= *input_line_pointer
++)
2488 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2490 while (is_part_of_name (c
= *input_line_pointer
++)
2491 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2493 if (is_name_ender (c
))
2494 c
= *input_line_pointer
++;
2498 char *dst
= input_line_pointer
;
2500 * ilp_return
= input_line_pointer
;
2503 c
= *input_line_pointer
++;
2507 as_warn (_("missing closing '\"'"));
2513 char *ilp_save
= input_line_pointer
;
2516 if (*input_line_pointer
== '"')
2518 ++input_line_pointer
;
2521 input_line_pointer
= ilp_save
;
2526 switch (*input_line_pointer
)
2530 c
= *input_line_pointer
++;
2535 as_warn (_("'\\%c' in quoted symbol name; "
2536 "behavior may change in the future"),
2537 *input_line_pointer
);
2545 *--input_line_pointer
= 0;
2549 /* Replace the NUL character pointed to by input_line_pointer
2550 with C. If C is \" then advance past it. Return the character
2551 now pointed to by input_line_pointer. */
2554 restore_line_pointer (char c
)
2556 * input_line_pointer
= c
;
2558 c
= * ++ input_line_pointer
;
2563 get_single_number (void)
2566 operand (&exp
, expr_normal
);
2567 return exp
.X_add_number
;