1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 /* This is really a branch office of as-read.c. I split it out to clearly
22 distinguish the world of expressions from the world of statements.
23 (It also gives smaller files to re-compile.)
24 Here, "operand"s are of expressions, not instructions. */
26 #define min(a, b) ((a) < (b) ? (a) : (b))
29 #include "safe-ctype.h"
36 bool literal_prefix_dollar_hex
= false;
38 static void clean_up_expression (expressionS
* expressionP
);
40 /* We keep a mapping of expression symbols to file positions, so that
41 we can provide better error messages. */
43 struct expr_symbol_line
{
44 struct expr_symbol_line
*next
;
50 static struct expr_symbol_line
*expr_symbol_lines
;
52 /* Build a dummy symbol to hold a complex expression. This is how we
53 build expressions up out of other expressions. The symbol is put
54 into the fake section expr_section. */
57 make_expr_symbol (expressionS
*expressionP
)
61 struct expr_symbol_line
*n
;
63 if (expressionP
->X_op
== O_symbol
64 && expressionP
->X_add_number
== 0)
65 return expressionP
->X_add_symbol
;
67 if (expressionP
->X_op
== O_big
)
69 /* This won't work, because the actual value is stored in
70 generic_floating_point_number or generic_bignum, and we are
71 going to lose it if we haven't already. */
72 if (expressionP
->X_add_number
> 0)
73 as_bad (_("bignum invalid"));
75 as_bad (_("floating point number invalid"));
76 zero
.X_op
= O_constant
;
77 zero
.X_add_number
= 0;
80 clean_up_expression (&zero
);
84 /* Putting constant symbols in absolute_section rather than
85 expr_section is convenient for the old a.out code, for which
86 S_GET_SEGMENT does not always retrieve the value put in by
88 symbolP
= symbol_create (FAKE_LABEL_NAME
,
89 (expressionP
->X_op
== O_constant
91 : expressionP
->X_op
== O_register
94 &zero_address_frag
, 0);
95 symbol_set_value_expression (symbolP
, expressionP
);
97 if (expressionP
->X_op
== O_constant
)
98 resolve_symbol_value (symbolP
);
100 n
= XNEW (struct expr_symbol_line
);
102 n
->file
= as_where (&n
->line
);
103 n
->next
= expr_symbol_lines
;
104 expr_symbol_lines
= n
;
109 /* Return the file and line number for an expr symbol. Return
110 non-zero if something was found, 0 if no information is known for
114 expr_symbol_where (symbolS
*sym
, const char **pfile
, unsigned int *pline
)
116 struct expr_symbol_line
*l
;
118 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
131 /* Utilities for building expressions.
132 Since complex expressions are recorded as symbols for use in other
133 expressions these return a symbolS * and not an expressionS *.
134 These explicitly do not take an "add_number" argument. */
135 /* ??? For completeness' sake one might want expr_build_symbol.
136 It would just return its argument. */
138 /* Build an expression for an unsigned constant.
139 The corresponding one for signed constants is missing because
140 there's currently no need for it. One could add an unsigned_p flag
141 but that seems more clumsy. */
144 expr_build_uconstant (offsetT value
)
149 e
.X_add_number
= value
;
152 return make_expr_symbol (&e
);
155 /* Build an expression for the current location ('.'). */
158 expr_build_dot (void)
162 current_location (&e
);
163 return symbol_clone_if_forward_ref (make_expr_symbol (&e
));
166 /* Build any floating-point literal here.
167 Also build any bignum literal here. */
169 /* Seems atof_machine can backscan through generic_bignum and hit whatever
170 happens to be loaded before it in memory. And its way too complicated
171 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
172 and never write into the early words, thus they'll always be zero.
173 I hate Dean's floating-point code. Bleh. */
174 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
176 FLONUM_TYPE generic_floating_point_number
= {
177 &generic_bignum
[6], /* low. (JF: Was 0) */
178 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high. JF: (added +6) */
186 floating_constant (expressionS
*expressionP
)
188 /* input_line_pointer -> floating-point constant. */
191 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
192 &generic_floating_point_number
);
196 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
198 as_bad (_("bad floating-point constant: exponent overflow"));
202 as_bad (_("bad floating-point constant: unknown error code=%d"),
206 expressionP
->X_op
= O_big
;
207 /* input_line_pointer -> just after constant, which may point to
209 expressionP
->X_add_number
= -1;
213 generic_bignum_to_int32 (void)
215 return ((((uint32_t) generic_bignum
[1] & LITTLENUM_MASK
)
216 << LITTLENUM_NUMBER_OF_BITS
)
217 | ((uint32_t) generic_bignum
[0] & LITTLENUM_MASK
));
221 generic_bignum_to_int64 (void)
223 return ((((((((uint64_t) generic_bignum
[3] & LITTLENUM_MASK
)
224 << LITTLENUM_NUMBER_OF_BITS
)
225 | ((uint64_t) generic_bignum
[2] & LITTLENUM_MASK
))
226 << LITTLENUM_NUMBER_OF_BITS
)
227 | ((uint64_t) generic_bignum
[1] & LITTLENUM_MASK
))
228 << LITTLENUM_NUMBER_OF_BITS
)
229 | ((uint64_t) generic_bignum
[0] & LITTLENUM_MASK
));
233 integer_constant (int radix
, expressionS
*expressionP
)
235 char *start
; /* Start of number. */
238 valueT number
; /* Offset or (absolute) value. */
239 short int digit
; /* Value of next digit in current radix. */
240 short int maxdig
= 0; /* Highest permitted digit value. */
241 int too_many_digits
= 0; /* If we see >= this number of. */
242 char *name
; /* Points to name of symbol. */
243 symbolS
*symbolP
; /* Points to symbol. */
245 int small
; /* True if fits in 32 bits. */
247 /* May be bignum, or may fit in 32 bits. */
248 /* Most numbers fit into 32 bits, and we want this case to be fast.
249 so we pretend it will fit into 32 bits. If, after making up a 32
250 bit number, we realise that we have scanned more digits than
251 comfortably fit into 32 bits, we re-scan the digits coding them
252 into a bignum. For decimal and octal numbers we are
253 conservative: Some numbers may be assumed bignums when in fact
254 they do fit into 32 bits. Numbers of any radix can have excess
255 leading zeros: We strive to recognise this and cast them back
256 into 32 bits. We must check that the bignum really is more than
257 32 bits, and change it back to a 32-bit number if it fits. The
258 number we are looking for is expected to be positive, but if it
259 fits into 32 bits as an unsigned number, we let it be a 32-bit
260 number. The cavalier approach is for speed in ordinary cases. */
261 /* This has been extended for 64 bits. We blindly assume that if
262 you're compiling in 64-bit mode, the target is a 64-bit machine.
263 This should be cleaned up. */
267 #else /* includes non-bfd case, mostly */
271 if (is_end_of_line
[(unsigned char) *input_line_pointer
])
273 expressionP
->X_op
= O_absent
;
277 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
) && radix
== 0)
281 /* In MRI mode, the number may have a suffix indicating the
282 radix. For that matter, it might actually be a floating
284 for (suffix
= input_line_pointer
; ISALNUM (*suffix
); suffix
++)
286 if (*suffix
== 'e' || *suffix
== 'E')
290 if (suffix
== input_line_pointer
)
299 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
300 we distinguish between 'B' and 'b'. This is the case for
302 if ((NUMBERS_WITH_SUFFIX
&& LOCAL_LABELS_FB
? *suffix
: c
) == 'B')
306 else if (c
== 'O' || c
== 'Q')
310 else if (suffix
[1] == '.' || c
== 'E' || flt
)
312 floating_constant (expressionP
);
327 too_many_digits
= valuesize
+ 1;
331 too_many_digits
= (valuesize
+ 2) / 3 + 1;
335 too_many_digits
= (valuesize
+ 3) / 4 + 1;
339 too_many_digits
= (valuesize
+ 11) / 4; /* Very rough. */
342 start
= input_line_pointer
;
343 c
= *input_line_pointer
++;
345 (digit
= hex_value (c
)) < maxdig
;
346 c
= *input_line_pointer
++)
348 number
= number
* radix
+ digit
;
350 /* c contains character after number. */
351 /* input_line_pointer->char after c. */
352 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
354 if (radix
== 16 && c
== '_')
356 /* This is literal of the form 0x333_0_12345678_1.
357 This example is equivalent to 0x00000333000000001234567800000001. */
359 int num_little_digits
= 0;
361 input_line_pointer
= start
; /* -> 1st digit. */
363 know (LITTLENUM_NUMBER_OF_BITS
== 16);
365 for (c
= '_'; c
== '_'; num_little_digits
+= 2)
368 /* Convert one 64-bit word. */
371 for (c
= *input_line_pointer
++;
372 (digit
= hex_value (c
)) < maxdig
;
373 c
= *(input_line_pointer
++))
375 number
= number
* radix
+ digit
;
379 /* Check for 8 digit per word max. */
381 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
383 /* Add this chunk to the bignum.
384 Shift things down 2 little digits. */
385 know (LITTLENUM_NUMBER_OF_BITS
== 16);
386 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1);
389 generic_bignum
[i
] = generic_bignum
[i
- 2];
391 /* Add the new digits as the least significant new ones. */
392 generic_bignum
[0] = number
& 0xffffffff;
393 generic_bignum
[1] = number
>> 16;
396 /* Again, c is char after number, input_line_pointer->after c. */
398 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
399 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
401 gas_assert (num_little_digits
>= 4);
403 if (num_little_digits
!= 8)
404 as_bad (_("a bignum with underscores must have exactly 4 words"));
406 /* We might have some leading zeros. These can be trimmed to give
407 us a change to fit this constant into a small number. */
408 while (generic_bignum
[num_little_digits
- 1] == 0
409 && num_little_digits
> 1)
412 if (num_little_digits
<= 2)
414 /* will fit into 32 bits. */
415 number
= generic_bignum_to_int32 ();
419 else if (num_little_digits
<= 4)
421 /* Will fit into 64 bits. */
422 number
= generic_bignum_to_int64 ();
430 /* Number of littlenums in the bignum. */
431 number
= num_little_digits
;
436 /* We saw a lot of digits. manufacture a bignum the hard way. */
437 LITTLENUM_TYPE
*leader
; /* -> high order littlenum of the bignum. */
438 LITTLENUM_TYPE
*pointer
; /* -> littlenum we are frobbing now. */
441 leader
= generic_bignum
;
442 generic_bignum
[0] = 0;
443 generic_bignum
[1] = 0;
444 generic_bignum
[2] = 0;
445 generic_bignum
[3] = 0;
446 input_line_pointer
= start
; /* -> 1st digit. */
447 c
= *input_line_pointer
++;
448 for (; (carry
= hex_value (c
)) < maxdig
; c
= *input_line_pointer
++)
450 for (pointer
= generic_bignum
; pointer
<= leader
; pointer
++)
454 work
= carry
+ radix
* *pointer
;
455 *pointer
= work
& LITTLENUM_MASK
;
456 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
460 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
462 /* Room to grow a longer bignum. */
467 /* Again, c is char after number. */
468 /* input_line_pointer -> after c. */
469 know (LITTLENUM_NUMBER_OF_BITS
== 16);
470 if (leader
< generic_bignum
+ 2)
472 /* Will fit into 32 bits. */
473 number
= generic_bignum_to_int32 ();
477 else if (leader
< generic_bignum
+ 4)
479 /* Will fit into 64 bits. */
480 number
= generic_bignum_to_int64 ();
486 /* Number of littlenums in the bignum. */
487 number
= leader
- generic_bignum
+ 1;
491 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
493 && input_line_pointer
- 1 == suffix
)
494 c
= *input_line_pointer
++;
496 #ifndef tc_allow_U_suffix
497 #define tc_allow_U_suffix 1
499 /* PR 19910: Look for, and ignore, a U suffix to the number. */
500 if (tc_allow_U_suffix
&& (c
== 'U' || c
== 'u'))
501 c
= * input_line_pointer
++;
503 #ifndef tc_allow_L_suffix
504 #define tc_allow_L_suffix 1
506 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
507 if (tc_allow_L_suffix
)
508 while (c
== 'L' || c
== 'l')
509 c
= * input_line_pointer
++;
513 /* Here with number, in correct radix. c is the next char.
514 Note that unlike un*x, we allow "011f" "0x9f" to both mean
515 the same as the (conventional) "9f".
516 This is simply easier than checking for strict canonical
519 if (LOCAL_LABELS_FB
&& c
== 'b')
521 /* Backward ref to local label.
522 Because it is backward, expect it to be defined. */
523 /* Construct a local label. */
524 name
= fb_label_name ((int) number
, 0);
526 /* Seen before, or symbol is defined: OK. */
527 symbolP
= symbol_find (name
);
528 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
530 /* Local labels are never absolute. Don't waste time
531 checking absoluteness. */
532 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
534 expressionP
->X_op
= O_symbol
;
535 expressionP
->X_add_symbol
= symbolP
;
539 /* Either not seen or not defined. */
540 /* @@ Should print out the original string instead of
541 the parsed number. */
542 as_bad (_("backward ref to unknown label \"%d:\""),
544 expressionP
->X_op
= O_constant
;
547 expressionP
->X_add_number
= 0;
549 else if (LOCAL_LABELS_FB
&& c
== 'f')
551 /* Forward reference. Expect symbol to be undefined or
552 unknown. undefined: seen it before. unknown: never seen
555 Construct a local label name, then an undefined symbol.
556 Don't create a xseg frag for it: caller may do that.
557 Just return it as never seen before. */
558 name
= fb_label_name ((int) number
, 1);
559 symbolP
= symbol_find_or_make (name
);
560 /* We have no need to check symbol properties. */
561 #ifndef many_segments
562 /* Since "know" puts its arg into a "string", we
563 can't have newlines in the argument. */
564 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
566 expressionP
->X_op
= O_symbol
;
567 expressionP
->X_add_symbol
= symbolP
;
568 expressionP
->X_add_number
= 0;
570 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
572 /* If the dollar label is *currently* defined, then this is just
573 another reference to it. If it is not *currently* defined,
574 then this is a fresh instantiation of that number, so create
577 if (dollar_label_defined ((long) number
))
579 name
= dollar_label_name ((long) number
, 0);
580 symbolP
= symbol_find (name
);
581 know (symbolP
!= NULL
);
585 name
= dollar_label_name ((long) number
, 1);
586 symbolP
= symbol_find_or_make (name
);
589 expressionP
->X_op
= O_symbol
;
590 expressionP
->X_add_symbol
= symbolP
;
591 expressionP
->X_add_number
= 0;
595 expressionP
->X_op
= O_constant
;
596 expressionP
->X_add_number
= number
;
597 input_line_pointer
--; /* Restore following character. */
598 } /* Really just a number. */
602 /* Not a small number. */
603 expressionP
->X_op
= O_big
;
604 expressionP
->X_add_number
= number
; /* Number of littlenums. */
605 input_line_pointer
--; /* -> char following number. */
609 /* Parse an MRI multi character constant. */
612 mri_char_constant (expressionS
*expressionP
)
616 if (*input_line_pointer
== '\''
617 && input_line_pointer
[1] != '\'')
619 expressionP
->X_op
= O_constant
;
620 expressionP
->X_add_number
= 0;
624 /* In order to get the correct byte ordering, we must build the
625 number in reverse. */
626 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
630 generic_bignum
[i
] = 0;
631 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
633 if (*input_line_pointer
== '\'')
635 if (input_line_pointer
[1] != '\'')
637 ++input_line_pointer
;
639 generic_bignum
[i
] <<= 8;
640 generic_bignum
[i
] += *input_line_pointer
;
641 ++input_line_pointer
;
644 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
646 /* If there is more than one littlenum, left justify the
647 last one to make it match the earlier ones. If there is
648 only one, we can just use the value directly. */
649 for (; j
< CHARS_PER_LITTLENUM
; j
++)
650 generic_bignum
[i
] <<= 8;
653 if (*input_line_pointer
== '\''
654 && input_line_pointer
[1] != '\'')
660 as_bad (_("character constant too large"));
669 c
= SIZE_OF_LARGE_NUMBER
- i
;
670 for (j
= 0; j
< c
; j
++)
671 generic_bignum
[j
] = generic_bignum
[i
+ j
];
675 know (LITTLENUM_NUMBER_OF_BITS
== 16);
678 expressionP
->X_op
= O_big
;
679 expressionP
->X_add_number
= i
;
683 expressionP
->X_op
= O_constant
;
685 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
687 expressionP
->X_add_number
=
688 (((generic_bignum
[1] & LITTLENUM_MASK
)
689 << LITTLENUM_NUMBER_OF_BITS
)
690 | (generic_bignum
[0] & LITTLENUM_MASK
));
693 /* Skip the final closing quote. */
694 ++input_line_pointer
;
697 /* Return an expression representing the current location. This
698 handles the magic symbol `.'. */
701 current_location (expressionS
*expressionp
)
703 if (now_seg
== absolute_section
)
705 expressionp
->X_op
= O_constant
;
706 expressionp
->X_add_number
= abs_section_offset
;
710 expressionp
->X_op
= O_symbol
;
711 expressionp
->X_add_symbol
= &dot_symbol
;
712 expressionp
->X_add_number
= 0;
716 /* In: Input_line_pointer points to 1st char of operand, which may
720 The operand may have been empty: in this case X_op == O_absent.
721 Input_line_pointer->(next non-blank) char after operand. */
724 operand (expressionS
*expressionP
, enum expr_mode mode
)
727 symbolS
*symbolP
; /* Points to symbol. */
728 char *name
; /* Points to name of symbol. */
731 /* All integers are regarded as unsigned unless they are negated.
732 This is because the only thing which cares whether a number is
733 unsigned is the code in emit_expr which extends constants into
734 bignums. It should only sign extend negative numbers, so that
735 something like ``.quad 0x80000000'' is not sign extended even
736 though it appears negative if valueT is 32 bits. */
737 expressionP
->X_unsigned
= 1;
738 expressionP
->X_extrabit
= 0;
740 /* Digits, assume it is a bignum. */
742 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
743 c
= *input_line_pointer
++; /* input_line_pointer -> past char in c. */
745 if (is_end_of_line
[(unsigned char) c
])
759 input_line_pointer
--;
761 integer_constant ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
766 #ifdef LITERAL_PREFIXPERCENT_BIN
768 integer_constant (2, expressionP
);
773 /* Non-decimal radix. */
775 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
779 /* Check for a hex or float constant. */
780 for (s
= input_line_pointer
; hex_p (*s
); s
++)
782 if (*s
== 'h' || *s
== 'H' || *input_line_pointer
== '.')
784 --input_line_pointer
;
785 integer_constant (0, expressionP
);
789 c
= *input_line_pointer
;
798 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
800 integer_constant (0, expressionP
);
806 if (c
&& strchr (FLT_CHARS
, c
))
808 input_line_pointer
++;
809 floating_constant (expressionP
);
810 expressionP
->X_add_number
= - TOLOWER (c
);
814 /* The string was only zero. */
815 expressionP
->X_op
= O_constant
;
816 expressionP
->X_add_number
= 0;
825 input_line_pointer
++;
826 integer_constant (16, expressionP
);
830 if (LOCAL_LABELS_FB
&& !flag_m68k_mri
831 && input_line_pointer
[1] != '0'
832 && input_line_pointer
[1] != '1')
834 /* Parse this as a back reference to label 0. */
835 input_line_pointer
--;
836 integer_constant (10, expressionP
);
839 /* Otherwise, parse this as a binary number. */
842 if (input_line_pointer
[1] == '0'
843 || input_line_pointer
[1] == '1')
845 input_line_pointer
++;
846 integer_constant (2, expressionP
);
849 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
850 input_line_pointer
++;
861 integer_constant ((flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
871 /* If it says "0f" and it could possibly be a floating point
872 number, make it one. Otherwise, make it a local label,
873 and try to deal with parsing the rest later. */
874 if (!is_end_of_line
[(unsigned char) input_line_pointer
[1]]
875 && strchr (FLT_CHARS
, 'f') != NULL
)
877 char *cp
= input_line_pointer
+ 1;
879 atof_generic (&cp
, ".", EXP_CHARS
,
880 &generic_floating_point_number
);
882 /* Was nothing parsed, or does it look like an
884 is_label
= (cp
== input_line_pointer
+ 1
885 || (cp
== input_line_pointer
+ 2
886 && (cp
[-1] == '-' || cp
[-1] == '+'))
892 input_line_pointer
--;
893 integer_constant (10, expressionP
);
901 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
903 integer_constant (0, expressionP
);
913 input_line_pointer
++;
914 floating_constant (expressionP
);
915 expressionP
->X_add_number
= - TOLOWER (c
);
919 if (LOCAL_LABELS_DOLLAR
)
921 integer_constant (10, expressionP
);
930 #ifndef NEED_INDEX_OPERATOR
932 # ifdef md_need_index_operator
933 if (md_need_index_operator())
939 /* Didn't begin with digit & not a name. */
940 segment
= expr (0, expressionP
, mode
);
941 /* expression () will pass trailing whitespace. */
942 if ((c
== '(' && *input_line_pointer
!= ')')
943 || (c
== '[' && *input_line_pointer
!= ']'))
945 if (* input_line_pointer
)
946 as_bad (_("found '%c', expected: '%c'"),
947 * input_line_pointer
, c
== '(' ? ')' : ']');
949 as_bad (_("missing '%c'"), c
== '(' ? ')' : ']');
952 input_line_pointer
++;
954 /* Here with input_line_pointer -> char after "(...)". */
959 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
961 as_bad (_("EBCDIC constants are not supported"));
964 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
966 ++input_line_pointer
;
972 /* Warning: to conform to other people's assemblers NO
973 ESCAPEMENT is permitted for a single quote. The next
974 character, parity errors and all, is taken as the value
975 of the operand. VERY KINKY. */
976 expressionP
->X_op
= O_constant
;
977 expressionP
->X_add_number
= *input_line_pointer
++;
981 mri_char_constant (expressionP
);
986 /* Double quote is the bitwise not operator in MRI mode. */
992 /* '~' is permitted to start a label on the Delta. */
993 if (is_name_beginner (c
))
1003 operand (expressionP
, mode
);
1004 if (expressionP
->X_op
== O_constant
)
1006 /* input_line_pointer -> char after operand. */
1009 expressionP
->X_add_number
1010 = - (addressT
) expressionP
->X_add_number
;
1011 /* Notice: '-' may overflow: no warning is given.
1012 This is compatible with other people's
1013 assemblers. Sigh. */
1014 expressionP
->X_unsigned
= 0;
1015 if (expressionP
->X_add_number
)
1016 expressionP
->X_extrabit
^= 1;
1018 else if (c
== '~' || c
== '"')
1020 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1021 expressionP
->X_extrabit
^= 1;
1025 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1026 expressionP
->X_unsigned
= 1;
1027 expressionP
->X_extrabit
= 0;
1030 else if (expressionP
->X_op
== O_big
1031 && expressionP
->X_add_number
<= 0
1033 && (generic_floating_point_number
.sign
== '+'
1034 || generic_floating_point_number
.sign
== 'P'))
1036 /* Negative flonum (eg, -1.000e0). */
1037 if (generic_floating_point_number
.sign
== '+')
1038 generic_floating_point_number
.sign
= '-';
1040 generic_floating_point_number
.sign
= 'N';
1042 else if (expressionP
->X_op
== O_big
1043 && expressionP
->X_add_number
> 0)
1047 if (c
== '~' || c
== '-')
1049 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1050 generic_bignum
[i
] = ~generic_bignum
[i
];
1052 /* Extend the bignum to at least the size of .octa. */
1053 if (expressionP
->X_add_number
< SIZE_OF_LARGE_NUMBER
)
1055 expressionP
->X_add_number
= SIZE_OF_LARGE_NUMBER
;
1056 for (; i
< expressionP
->X_add_number
; ++i
)
1057 generic_bignum
[i
] = ~(LITTLENUM_TYPE
) 0;
1061 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1063 generic_bignum
[i
] += 1;
1064 if (generic_bignum
[i
])
1070 for (i
= 0; i
< expressionP
->X_add_number
; ++i
)
1071 if (generic_bignum
[i
] != 0)
1073 expressionP
->X_add_number
= i
>= expressionP
->X_add_number
;
1074 expressionP
->X_op
= O_constant
;
1075 expressionP
->X_unsigned
= 1;
1076 expressionP
->X_extrabit
= 0;
1079 else if (expressionP
->X_op
!= O_illegal
1080 && expressionP
->X_op
!= O_absent
)
1084 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1086 expressionP
->X_op
= O_uminus
;
1087 else if (c
== '~' || c
== '"')
1088 expressionP
->X_op
= O_bit_not
;
1090 expressionP
->X_op
= O_logical_not
;
1091 expressionP
->X_add_number
= 0;
1095 as_warn (_("Unary operator %c ignored because bad operand follows"),
1100 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1102 if (literal_prefix_dollar_hex
)
1104 /* $L is the start of a local label, not a hex constant. */
1105 if (* input_line_pointer
== 'L')
1107 integer_constant (16, expressionP
);
1116 /* '$' is the program counter when in MRI mode, or when
1117 DOLLAR_DOT is defined. */
1119 if (! flag_m68k_mri
)
1122 if (DOLLAR_AMBIGU
&& hex_p (*input_line_pointer
))
1124 /* In MRI mode and on Z80, '$' is also used as the prefix
1125 for a hexadecimal constant. */
1126 integer_constant (16, expressionP
);
1130 if (is_part_of_name (*input_line_pointer
))
1133 current_location (expressionP
);
1138 if (!is_part_of_name (*input_line_pointer
))
1140 current_location (expressionP
);
1143 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1144 && ! is_part_of_name (input_line_pointer
[8]))
1145 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1146 && ! is_part_of_name (input_line_pointer
[7])))
1150 start
= (input_line_pointer
[1] == 't'
1151 || input_line_pointer
[1] == 'T');
1152 input_line_pointer
+= start
? 8 : 7;
1155 /* Cover for the as_bad () invocations below. */
1156 expressionP
->X_op
= O_absent
;
1158 if (*input_line_pointer
!= '(')
1159 as_bad (_("syntax error in .startof. or .sizeof."));
1164 ++input_line_pointer
;
1166 c
= get_symbol_name (& name
);
1169 as_bad (_("expected symbol name"));
1170 (void) restore_line_pointer (c
);
1172 ignore_rest_of_line ();
1174 ++input_line_pointer
;
1178 buf
= concat (start
? ".startof." : ".sizeof.", name
,
1180 symbolP
= symbol_make (buf
);
1183 expressionP
->X_op
= O_symbol
;
1184 expressionP
->X_add_symbol
= symbolP
;
1185 expressionP
->X_add_number
= 0;
1187 *input_line_pointer
= c
;
1188 SKIP_WHITESPACE_AFTER_NAME ();
1189 if (*input_line_pointer
!= ')')
1190 as_bad (_("syntax error in .startof. or .sizeof."));
1192 ++input_line_pointer
;
1203 /* Can't imagine any other kind of operand. */
1204 expressionP
->X_op
= O_absent
;
1205 input_line_pointer
--;
1210 if (! flag_m68k_mri
)
1212 integer_constant (2, expressionP
);
1216 if (! flag_m68k_mri
)
1218 integer_constant (8, expressionP
);
1222 if (! flag_m68k_mri
)
1225 /* In MRI mode, this is a floating point constant represented
1226 using hexadecimal digits. */
1228 ++input_line_pointer
;
1229 integer_constant (16, expressionP
);
1233 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1236 current_location (expressionP
);
1241 #if defined(md_need_index_operator) || defined(TC_M68K)
1244 if (is_name_beginner (c
) || c
== '"') /* Here if did not begin with a digit. */
1246 /* Identifier begins here.
1247 This is kludged for speed, so code is repeated. */
1249 -- input_line_pointer
;
1250 c
= get_symbol_name (&name
);
1254 operatorT op
= md_operator (name
, 1, &c
);
1259 restore_line_pointer (c
);
1263 restore_line_pointer (c
);
1267 restore_line_pointer (c
);
1271 as_bad (_("invalid use of operator \"%s\""), name
);
1277 if (op
!= O_absent
&& op
!= O_illegal
)
1279 restore_line_pointer (c
);
1280 expr (9, expressionP
, mode
);
1281 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1282 expressionP
->X_op_symbol
= NULL
;
1283 expressionP
->X_add_number
= 0;
1284 expressionP
->X_op
= op
;
1290 #ifdef md_parse_name
1291 /* This is a hook for the backend to parse certain names
1292 specially in certain contexts. If a name always has a
1293 specific value, it can often be handled by simply
1294 entering it in the symbol table. */
1295 if (md_parse_name (name
, expressionP
, mode
, &c
))
1297 restore_line_pointer (c
);
1302 symbolP
= symbol_find_or_make (name
);
1304 /* If we have an absolute symbol or a reg, then we know its
1306 segment
= S_GET_SEGMENT (symbolP
);
1307 if (mode
!= expr_defer
1308 && segment
== absolute_section
1309 && !S_FORCE_RELOC (symbolP
, 0))
1311 expressionP
->X_op
= O_constant
;
1312 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1314 else if (mode
!= expr_defer
&& segment
== reg_section
)
1316 expressionP
->X_op
= O_register
;
1317 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1321 expressionP
->X_op
= O_symbol
;
1322 expressionP
->X_add_symbol
= symbolP
;
1323 expressionP
->X_add_number
= 0;
1326 restore_line_pointer (c
);
1330 /* Let the target try to parse it. Success is indicated by changing
1331 the X_op field to something other than O_absent and pointing
1332 input_line_pointer past the expression. If it can't parse the
1333 expression, X_op and input_line_pointer should be unchanged. */
1334 expressionP
->X_op
= O_absent
;
1335 --input_line_pointer
;
1336 md_operand (expressionP
);
1337 if (expressionP
->X_op
== O_absent
)
1339 ++input_line_pointer
;
1340 as_bad (_("bad expression"));
1341 expressionP
->X_op
= O_constant
;
1342 expressionP
->X_add_number
= 0;
1348 /* It is more 'efficient' to clean up the expressionS when they are
1349 created. Doing it here saves lines of code. */
1350 clean_up_expression (expressionP
);
1351 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1352 know (*input_line_pointer
!= ' ');
1354 /* The PA port needs this information. */
1355 if (expressionP
->X_add_symbol
)
1356 symbol_mark_used (expressionP
->X_add_symbol
);
1358 if (mode
!= expr_defer
)
1360 expressionP
->X_add_symbol
1361 = symbol_clone_if_forward_ref (expressionP
->X_add_symbol
);
1362 expressionP
->X_op_symbol
1363 = symbol_clone_if_forward_ref (expressionP
->X_op_symbol
);
1366 switch (expressionP
->X_op
)
1369 return absolute_section
;
1371 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1377 /* Internal. Simplify a struct expression for use by expr (). */
1379 /* In: address of an expressionS.
1380 The X_op field of the expressionS may only take certain values.
1381 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1383 Out: expressionS may have been modified:
1384 Unused fields zeroed to help expr (). */
1387 clean_up_expression (expressionS
*expressionP
)
1389 switch (expressionP
->X_op
)
1393 expressionP
->X_add_number
= 0;
1398 expressionP
->X_add_symbol
= NULL
;
1403 expressionP
->X_op_symbol
= NULL
;
1410 /* Expression parser. */
1412 /* We allow an empty expression, and just assume (absolute,0) silently.
1413 Unary operators and parenthetical expressions are treated as operands.
1414 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1416 We used to do an aho/ullman shift-reduce parser, but the logic got so
1417 warped that I flushed it and wrote a recursive-descent parser instead.
1418 Now things are stable, would anybody like to write a fast parser?
1419 Most expressions are either register (which does not even reach here)
1420 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1421 So I guess it doesn't really matter how inefficient more complex expressions
1424 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1425 Also, we have consumed any leading or trailing spaces (operand does that)
1426 and done all intervening operators.
1428 This returns the segment of the result, which will be
1429 absolute_section or the segment of a symbol. */
1432 #define __ O_illegal
1434 #define O_SINGLE_EQ O_illegal
1437 /* Maps ASCII -> operators. */
1438 static const operatorT op_encoding
[256] = {
1439 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1440 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1442 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1443 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1444 __
, __
, __
, __
, __
, __
, __
, __
,
1445 __
, __
, __
, __
, O_lt
, O_SINGLE_EQ
, O_gt
, __
,
1446 __
, __
, __
, __
, __
, __
, __
, __
,
1447 __
, __
, __
, __
, __
, __
, __
, __
,
1448 __
, __
, __
, __
, __
, __
, __
, __
,
1450 #ifdef NEED_INDEX_OPERATOR
1455 __
, __
, O_bit_exclusive_or
, __
,
1456 __
, __
, __
, __
, __
, __
, __
, __
,
1457 __
, __
, __
, __
, __
, __
, __
, __
,
1458 __
, __
, __
, __
, __
, __
, __
, __
,
1459 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1461 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1462 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1463 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1464 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1465 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1466 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1467 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1468 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1472 0 operand, (expression)
1477 5 used for * / % in MRI mode
1482 static operator_rankT op_rank
[O_max
] = {
1487 0, /* O_symbol_rva */
1492 9, /* O_logical_not */
1496 8, /* O_left_shift */
1497 8, /* O_right_shift */
1498 7, /* O_bit_inclusive_or */
1499 7, /* O_bit_or_not */
1500 7, /* O_bit_exclusive_or */
1510 3, /* O_logical_and */
1511 2, /* O_logical_or */
1515 /* Unfortunately, in MRI mode for the m68k, multiplication and
1516 division have lower precedence than the bit wise operators. This
1517 function sets the operator precedences correctly for the current
1518 mode. Also, MRI uses a different bit_not operator, and this fixes
1521 #define STANDARD_MUL_PRECEDENCE 8
1522 #define MRI_MUL_PRECEDENCE 6
1525 expr_set_precedence (void)
1529 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1530 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1531 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1535 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1536 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1537 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1542 expr_set_rank (operatorT op
, operator_rankT rank
)
1544 gas_assert (op
>= O_md1
&& op
< ARRAY_SIZE (op_rank
));
1548 /* Initialize the expression parser. */
1553 expr_set_precedence ();
1555 /* Verify that X_op field is wide enough. */
1559 gas_assert (e
.X_op
== O_max
);
1563 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1564 sets NUM_CHARS to the number of characters in the operator.
1565 Does not advance INPUT_LINE_POINTER. */
1567 static inline operatorT
1568 operatorf (int *num_chars
)
1573 c
= *input_line_pointer
& 0xff;
1576 if (is_end_of_line
[c
])
1580 if (is_name_beginner (c
))
1583 char ec
= get_symbol_name (& name
);
1585 ret
= md_operator (name
, 2, &ec
);
1589 *input_line_pointer
= ec
;
1590 input_line_pointer
= name
;
1595 as_bad (_("invalid use of operator \"%s\""), name
);
1599 *input_line_pointer
= ec
;
1600 *num_chars
= input_line_pointer
- name
;
1601 input_line_pointer
= name
;
1610 ret
= op_encoding
[c
];
1612 if (ret
== O_illegal
)
1614 char *start
= input_line_pointer
;
1616 ret
= md_operator (NULL
, 2, NULL
);
1617 if (ret
!= O_illegal
)
1618 *num_chars
= input_line_pointer
- start
;
1619 input_line_pointer
= start
;
1626 return op_encoding
[c
];
1629 switch (input_line_pointer
[1])
1632 return op_encoding
[c
];
1647 if (input_line_pointer
[1] != '=')
1648 return op_encoding
[c
];
1654 switch (input_line_pointer
[1])
1657 return op_encoding
[c
];
1659 ret
= O_right_shift
;
1669 switch (input_line_pointer
[1])
1672 /* We accept !! as equivalent to ^ for MRI compatibility. */
1674 return O_bit_exclusive_or
;
1676 /* We accept != as equivalent to <>. */
1681 return O_bit_inclusive_or
;
1682 return op_encoding
[c
];
1686 if (input_line_pointer
[1] != '|')
1687 return op_encoding
[c
];
1690 return O_logical_or
;
1693 if (input_line_pointer
[1] != '&')
1694 return op_encoding
[c
];
1697 return O_logical_and
;
1703 /* Implement "word-size + 1 bit" addition for
1704 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1705 is used so that the full range of unsigned word values and the full range of
1706 signed word values can be represented in an O_constant expression, which is
1707 useful e.g. for .sleb128 directives. */
1710 add_to_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1712 valueT ures
= resultP
->X_add_number
;
1713 valueT uamount
= amount
;
1715 resultP
->X_add_number
+= uamount
;
1717 resultP
->X_extrabit
^= rhs_highbit
;
1719 if (ures
+ uamount
< ures
)
1720 resultP
->X_extrabit
^= 1;
1723 /* Similarly, for subtraction. */
1726 subtract_from_result (expressionS
*resultP
, offsetT amount
, int rhs_highbit
)
1728 valueT ures
= resultP
->X_add_number
;
1729 valueT uamount
= amount
;
1731 resultP
->X_add_number
-= uamount
;
1733 resultP
->X_extrabit
^= rhs_highbit
;
1736 resultP
->X_extrabit
^= 1;
1739 /* Parse an expression. */
1742 expr (int rankarg
, /* Larger # is higher rank. */
1743 expressionS
*resultP
, /* Deliver result here. */
1744 enum expr_mode mode
/* Controls behavior. */)
1746 operator_rankT rank
= (operator_rankT
) rankarg
;
1753 know (rankarg
>= 0);
1755 /* Save the value of dot for the fixup code. */
1758 dot_value
= frag_now_fix ();
1759 dot_frag
= frag_now
;
1762 retval
= operand (resultP
, mode
);
1764 /* operand () gobbles spaces. */
1765 know (*input_line_pointer
!= ' ');
1767 op_left
= operatorf (&op_chars
);
1768 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1773 input_line_pointer
+= op_chars
; /* -> after operator. */
1776 rightseg
= expr (op_rank
[(int) op_left
], &right
, mode
);
1777 if (right
.X_op
== O_absent
)
1779 as_warn (_("missing operand; zero assumed"));
1780 right
.X_op
= O_constant
;
1781 right
.X_add_number
= 0;
1782 right
.X_add_symbol
= NULL
;
1783 right
.X_op_symbol
= NULL
;
1786 know (*input_line_pointer
!= ' ');
1788 if (op_left
== O_index
)
1790 if (*input_line_pointer
!= ']')
1791 as_bad ("missing right bracket");
1794 ++input_line_pointer
;
1799 op_right
= operatorf (&op_chars
);
1801 know (op_right
== O_illegal
|| op_left
== O_index
1802 || op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1803 know ((int) op_left
>= (int) O_multiply
);
1805 know ((int) op_left
<= (int) O_index
);
1807 know ((int) op_left
< (int) O_max
);
1810 /* input_line_pointer->after right-hand quantity. */
1811 /* left-hand quantity in resultP. */
1812 /* right-hand quantity in right. */
1813 /* operator in op_left. */
1815 if (resultP
->X_op
== O_big
)
1817 if (resultP
->X_add_number
> 0)
1818 as_warn (_("left operand is a bignum; integer 0 assumed"));
1820 as_warn (_("left operand is a float; integer 0 assumed"));
1821 resultP
->X_op
= O_constant
;
1822 resultP
->X_add_number
= 0;
1823 resultP
->X_add_symbol
= NULL
;
1824 resultP
->X_op_symbol
= NULL
;
1826 if (right
.X_op
== O_big
)
1828 if (right
.X_add_number
> 0)
1829 as_warn (_("right operand is a bignum; integer 0 assumed"));
1831 as_warn (_("right operand is a float; integer 0 assumed"));
1832 right
.X_op
= O_constant
;
1833 right
.X_add_number
= 0;
1834 right
.X_add_symbol
= NULL
;
1835 right
.X_op_symbol
= NULL
;
1838 if (mode
== expr_defer
1839 && ((resultP
->X_add_symbol
!= NULL
1840 && S_IS_FORWARD_REF (resultP
->X_add_symbol
))
1841 || (right
.X_add_symbol
!= NULL
1842 && S_IS_FORWARD_REF (right
.X_add_symbol
))))
1845 /* Optimize common cases. */
1846 #ifdef md_optimize_expr
1847 if (md_optimize_expr (resultP
, op_left
, &right
))
1854 #ifndef md_register_arithmetic
1855 # define md_register_arithmetic 1
1857 if (op_left
== O_add
&& right
.X_op
== O_constant
1858 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1861 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1863 /* This case comes up in PIC code. */
1864 else if (op_left
== O_subtract
1865 && right
.X_op
== O_symbol
1866 && resultP
->X_op
== O_symbol
1867 && retval
== rightseg
1868 #ifdef md_allow_local_subtract
1869 && md_allow_local_subtract (resultP
, & right
, rightseg
)
1871 && ((SEG_NORMAL (rightseg
)
1872 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
1873 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
1874 || right
.X_add_symbol
== resultP
->X_add_symbol
)
1875 && frag_offset_fixed_p (symbol_get_frag (resultP
->X_add_symbol
),
1876 symbol_get_frag (right
.X_add_symbol
),
1879 offsetT symval_diff
= S_GET_VALUE (resultP
->X_add_symbol
)
1880 - S_GET_VALUE (right
.X_add_symbol
);
1881 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1882 subtract_from_result (resultP
, frag_off
/ OCTETS_PER_BYTE
, 0);
1883 add_to_result (resultP
, symval_diff
, symval_diff
< 0);
1884 resultP
->X_op
= O_constant
;
1885 resultP
->X_add_symbol
= 0;
1887 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
1888 && (md_register_arithmetic
|| resultP
->X_op
!= O_register
))
1891 subtract_from_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1893 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
1894 && (md_register_arithmetic
|| right
.X_op
!= O_register
))
1897 resultP
->X_op
= right
.X_op
;
1898 resultP
->X_add_symbol
= right
.X_add_symbol
;
1899 resultP
->X_op_symbol
= right
.X_op_symbol
;
1900 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1903 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1905 /* Constant OP constant. */
1906 offsetT v
= right
.X_add_number
;
1907 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1909 as_warn (_("division by zero"));
1912 if ((valueT
) v
>= sizeof(valueT
) * CHAR_BIT
1913 && (op_left
== O_left_shift
|| op_left
== O_right_shift
))
1915 as_warn_value_out_of_range (_("shift count"), v
, 0,
1916 sizeof(valueT
) * CHAR_BIT
- 1,
1918 resultP
->X_add_number
= v
= 0;
1922 default: goto general
;
1923 case O_multiply
: resultP
->X_add_number
*= v
; break;
1924 case O_divide
: resultP
->X_add_number
/= v
; break;
1925 case O_modulus
: resultP
->X_add_number
%= v
; break;
1927 /* We always use unsigned shifts. According to the ISO
1928 C standard, left shift of a signed type having a
1929 negative value is undefined behaviour, and right
1930 shift of a signed type having negative value is
1931 implementation defined. Left shift of a signed type
1932 when the result overflows is also undefined
1933 behaviour. So don't trigger ubsan warnings or rely
1934 on characteristics of the compiler. */
1935 resultP
->X_add_number
1936 = (valueT
) resultP
->X_add_number
<< (valueT
) v
;
1939 resultP
->X_add_number
1940 = (valueT
) resultP
->X_add_number
>> (valueT
) v
;
1942 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1943 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1944 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1945 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1946 /* Constant + constant (O_add) is handled by the
1947 previous if statement for constant + X, so is omitted
1950 subtract_from_result (resultP
, v
, 0);
1953 resultP
->X_add_number
=
1954 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1957 resultP
->X_add_number
=
1958 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1961 resultP
->X_add_number
=
1962 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1965 resultP
->X_add_number
=
1966 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1969 resultP
->X_add_number
=
1970 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1973 resultP
->X_add_number
=
1974 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1977 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1980 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1984 else if (resultP
->X_op
== O_symbol
1985 && right
.X_op
== O_symbol
1986 && (op_left
== O_add
1987 || op_left
== O_subtract
1988 || (resultP
->X_add_number
== 0
1989 && right
.X_add_number
== 0)))
1991 /* Symbol OP symbol. */
1992 resultP
->X_op
= op_left
;
1993 resultP
->X_op_symbol
= right
.X_add_symbol
;
1994 if (op_left
== O_add
)
1995 add_to_result (resultP
, right
.X_add_number
, right
.X_extrabit
);
1996 else if (op_left
== O_subtract
)
1998 subtract_from_result (resultP
, right
.X_add_number
,
2000 if (retval
== rightseg
2001 && SEG_NORMAL (retval
)
2002 && !S_FORCE_RELOC (resultP
->X_add_symbol
, 0)
2003 && !S_FORCE_RELOC (right
.X_add_symbol
, 0))
2005 retval
= absolute_section
;
2006 rightseg
= absolute_section
;
2013 /* The general case. */
2014 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
2015 resultP
->X_op_symbol
= make_expr_symbol (&right
);
2016 resultP
->X_op
= op_left
;
2017 resultP
->X_add_number
= 0;
2018 resultP
->X_unsigned
= 1;
2019 resultP
->X_extrabit
= 0;
2022 if (retval
!= rightseg
)
2024 if (retval
== undefined_section
)
2026 else if (rightseg
== undefined_section
)
2028 else if (retval
== expr_section
)
2030 else if (rightseg
== expr_section
)
2032 else if (retval
== reg_section
)
2034 else if (rightseg
== reg_section
)
2036 else if (rightseg
== absolute_section
)
2038 else if (retval
== absolute_section
)
2041 else if (op_left
== O_subtract
)
2045 as_bad (_("operation combines symbols in different segments"));
2049 } /* While next operator is >= this rank. */
2051 /* The PA port needs this information. */
2052 if (resultP
->X_add_symbol
)
2053 symbol_mark_used (resultP
->X_add_symbol
);
2055 if (rank
== 0 && mode
== expr_evaluate
)
2056 resolve_expression (resultP
);
2058 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
2061 /* Resolve an expression without changing any symbols/sub-expressions
2065 resolve_expression (expressionS
*expressionP
)
2067 /* Help out with CSE. */
2068 valueT final_val
= expressionP
->X_add_number
;
2069 symbolS
*add_symbol
= expressionP
->X_add_symbol
;
2070 symbolS
*orig_add_symbol
= add_symbol
;
2071 symbolS
*op_symbol
= expressionP
->X_op_symbol
;
2072 operatorT op
= expressionP
->X_op
;
2074 segT seg_left
, seg_right
;
2075 fragS
*frag_left
, *frag_right
;
2090 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2098 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
))
2101 if (seg_left
!= absolute_section
)
2104 if (op
== O_logical_not
)
2106 else if (op
== O_uminus
)
2118 case O_bit_inclusive_or
:
2120 case O_bit_exclusive_or
:
2132 if (!snapshot_symbol (&add_symbol
, &left
, &seg_left
, &frag_left
)
2133 || !snapshot_symbol (&op_symbol
, &right
, &seg_right
, &frag_right
))
2136 /* Simplify addition or subtraction of a constant by folding the
2137 constant into X_add_number. */
2140 if (seg_right
== absolute_section
)
2146 else if (seg_left
== absolute_section
)
2150 seg_left
= seg_right
;
2151 add_symbol
= op_symbol
;
2152 orig_add_symbol
= expressionP
->X_op_symbol
;
2157 else if (op
== O_subtract
)
2159 if (seg_right
== absolute_section
)
2167 /* Equality and non-equality tests are permitted on anything.
2168 Subtraction, and other comparison operators are permitted if
2169 both operands are in the same section.
2170 Shifts by constant zero are permitted on anything.
2171 Multiplies, bit-ors, and bit-ands with constant zero are
2172 permitted on anything.
2173 Multiplies and divides by constant one are permitted on
2175 Binary operations with both operands being the same register
2176 or undefined symbol are permitted if the result doesn't depend
2178 Otherwise, both operands must be absolute. We already handled
2179 the case of addition or subtraction of a constant above. */
2181 if (!(seg_left
== absolute_section
2182 && seg_right
== absolute_section
)
2183 && !(op
== O_eq
|| op
== O_ne
)
2184 && !((op
== O_subtract
2185 || op
== O_lt
|| op
== O_le
|| op
== O_ge
|| op
== O_gt
)
2186 && seg_left
== seg_right
2188 || frag_offset_fixed_p (frag_left
, frag_right
, &frag_off
)
2190 && frag_gtoffset_p (left
, frag_left
,
2191 right
, frag_right
, &frag_off
)))
2192 && (seg_left
!= reg_section
|| left
== right
)
2193 && (seg_left
!= undefined_section
|| add_symbol
== op_symbol
)))
2195 if ((seg_left
== absolute_section
&& left
== 0)
2196 || (seg_right
== absolute_section
&& right
== 0))
2198 if (op
== O_bit_exclusive_or
|| op
== O_bit_inclusive_or
)
2200 if (!(seg_right
== absolute_section
&& right
== 0))
2202 seg_left
= seg_right
;
2204 add_symbol
= op_symbol
;
2205 orig_add_symbol
= expressionP
->X_op_symbol
;
2210 else if (op
== O_left_shift
|| op
== O_right_shift
)
2212 if (!(seg_left
== absolute_section
&& left
== 0))
2218 else if (op
!= O_multiply
2219 && op
!= O_bit_or_not
&& op
!= O_bit_and
)
2222 else if (op
== O_multiply
2223 && seg_left
== absolute_section
&& left
== 1)
2225 seg_left
= seg_right
;
2227 add_symbol
= op_symbol
;
2228 orig_add_symbol
= expressionP
->X_op_symbol
;
2232 else if ((op
== O_multiply
|| op
== O_divide
)
2233 && seg_right
== absolute_section
&& right
== 1)
2238 else if (!(left
== right
2239 && ((seg_left
== reg_section
&& seg_right
== reg_section
)
2240 || (seg_left
== undefined_section
2241 && seg_right
== undefined_section
2242 && add_symbol
== op_symbol
))))
2244 else if (op
== O_bit_and
|| op
== O_bit_inclusive_or
)
2249 else if (op
!= O_bit_exclusive_or
&& op
!= O_bit_or_not
)
2253 right
+= frag_off
/ OCTETS_PER_BYTE
;
2256 case O_add
: left
+= right
; break;
2257 case O_subtract
: left
-= right
; break;
2258 case O_multiply
: left
*= right
; break;
2262 left
= (offsetT
) left
/ (offsetT
) right
;
2267 left
= (offsetT
) left
% (offsetT
) right
;
2269 case O_left_shift
: left
<<= right
; break;
2270 case O_right_shift
: left
>>= right
; break;
2271 case O_bit_inclusive_or
: left
|= right
; break;
2272 case O_bit_or_not
: left
|= ~right
; break;
2273 case O_bit_exclusive_or
: left
^= right
; break;
2274 case O_bit_and
: left
&= right
; break;
2277 left
= (left
== right
2278 && seg_left
== seg_right
2279 && (finalize_syms
|| frag_left
== frag_right
)
2280 && (seg_left
!= undefined_section
2281 || add_symbol
== op_symbol
)
2282 ? ~ (valueT
) 0 : 0);
2287 left
= (offsetT
) left
< (offsetT
) right
? ~ (valueT
) 0 : 0;
2290 left
= (offsetT
) left
<= (offsetT
) right
? ~ (valueT
) 0 : 0;
2293 left
= (offsetT
) left
>= (offsetT
) right
? ~ (valueT
) 0 : 0;
2296 left
= (offsetT
) left
> (offsetT
) right
? ~ (valueT
) 0 : 0;
2298 case O_logical_and
: left
= left
&& right
; break;
2299 case O_logical_or
: left
= left
|| right
; break;
2309 if (seg_left
== absolute_section
)
2311 else if (seg_left
== reg_section
&& final_val
== 0)
2313 else if (!symbol_same_p (add_symbol
, orig_add_symbol
))
2315 expressionP
->X_add_symbol
= add_symbol
;
2317 expressionP
->X_op
= op
;
2319 if (op
== O_constant
|| op
== O_register
)
2321 expressionP
->X_add_number
= final_val
;
2326 /* This lives here because it belongs equally in expr.c & read.c.
2327 expr.c is just a branch office read.c anyway, and putting it
2328 here lessens the crowd at read.c.
2330 Assume input_line_pointer is at start of symbol name, or the
2331 start of a double quote enclosed symbol name.
2332 Advance input_line_pointer past symbol name.
2333 Turn that character into a '\0', returning its former value,
2334 which may be the closing double quote.
2335 This allows a string compare (RMS wants symbol names to be strings)
2337 There will always be a char following symbol name, because all good
2338 lines end in end-of-line. */
2341 get_symbol_name (char ** ilp_return
)
2345 * ilp_return
= input_line_pointer
;
2346 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2347 constructed string. */
2348 if (is_name_beginner (c
= *input_line_pointer
++)
2349 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2351 while (is_part_of_name (c
= *input_line_pointer
++)
2352 || (input_from_string
&& c
== FAKE_LABEL_CHAR
))
2354 if (is_name_ender (c
))
2355 c
= *input_line_pointer
++;
2359 bool backslash_seen
;
2361 * ilp_return
= input_line_pointer
;
2364 backslash_seen
= c
== '\\';
2365 c
= * input_line_pointer
++;
2367 while (c
!= 0 && (c
!= '"' || backslash_seen
));
2370 as_warn (_("missing closing '\"'"));
2372 *--input_line_pointer
= 0;
2376 /* Replace the NUL character pointed to by input_line_pointer
2377 with C. If C is \" then advance past it. Return the character
2378 now pointed to by input_line_pointer. */
2381 restore_line_pointer (char c
)
2383 * input_line_pointer
= c
;
2385 c
= * ++ input_line_pointer
;
2390 get_single_number (void)
2393 operand (&exp
, expr_normal
);
2394 return exp
.X_add_number
;