[PATCH 16/57][Arm][GAS] Add support for MVE instructions: vdup, vddup, vdwdup, vidup...
[binutils-gdb.git] / gas / expr.c
blob3efde88cc0a2c1034022f14454c3fb36c147432c
1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2019 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)
9 any later version.
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
19 02110-1301, USA. */
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))
28 #include "as.h"
29 #include "safe-ctype.h"
31 #ifdef HAVE_LIMITS_H
32 #include <limits.h>
33 #endif
34 #ifndef CHAR_BIT
35 #define CHAR_BIT 8
36 #endif
38 static void floating_constant (expressionS * expressionP);
39 static valueT generic_bignum_to_int32 (void);
40 #ifdef BFD64
41 static valueT generic_bignum_to_int64 (void);
42 #endif
43 static void integer_constant (int radix, expressionS * expressionP);
44 static void mri_char_constant (expressionS *);
45 static void clean_up_expression (expressionS * expressionP);
46 static segT operand (expressionS *, enum expr_mode);
47 static operatorT operatorf (int *);
49 /* We keep a mapping of expression symbols to file positions, so that
50 we can provide better error messages. */
52 struct expr_symbol_line {
53 struct expr_symbol_line *next;
54 symbolS *sym;
55 const char *file;
56 unsigned int line;
59 static struct expr_symbol_line *expr_symbol_lines;
61 /* Build a dummy symbol to hold a complex expression. This is how we
62 build expressions up out of other expressions. The symbol is put
63 into the fake section expr_section. */
65 symbolS *
66 make_expr_symbol (expressionS *expressionP)
68 expressionS zero;
69 symbolS *symbolP;
70 struct expr_symbol_line *n;
72 if (expressionP->X_op == O_symbol
73 && expressionP->X_add_number == 0)
74 return expressionP->X_add_symbol;
76 if (expressionP->X_op == O_big)
78 /* This won't work, because the actual value is stored in
79 generic_floating_point_number or generic_bignum, and we are
80 going to lose it if we haven't already. */
81 if (expressionP->X_add_number > 0)
82 as_bad (_("bignum invalid"));
83 else
84 as_bad (_("floating point number invalid"));
85 zero.X_op = O_constant;
86 zero.X_add_number = 0;
87 zero.X_unsigned = 0;
88 zero.X_extrabit = 0;
89 clean_up_expression (&zero);
90 expressionP = &zero;
93 /* Putting constant symbols in absolute_section rather than
94 expr_section is convenient for the old a.out code, for which
95 S_GET_SEGMENT does not always retrieve the value put in by
96 S_SET_SEGMENT. */
97 symbolP = symbol_create (FAKE_LABEL_NAME,
98 (expressionP->X_op == O_constant
99 ? absolute_section
100 : expressionP->X_op == O_register
101 ? reg_section
102 : expr_section),
103 0, &zero_address_frag);
104 symbol_set_value_expression (symbolP, expressionP);
106 if (expressionP->X_op == O_constant)
107 resolve_symbol_value (symbolP);
109 n = XNEW (struct expr_symbol_line);
110 n->sym = symbolP;
111 n->file = as_where (&n->line);
112 n->next = expr_symbol_lines;
113 expr_symbol_lines = n;
115 return symbolP;
118 /* Return the file and line number for an expr symbol. Return
119 non-zero if something was found, 0 if no information is known for
120 the symbol. */
123 expr_symbol_where (symbolS *sym, const char **pfile, unsigned int *pline)
125 struct expr_symbol_line *l;
127 for (l = expr_symbol_lines; l != NULL; l = l->next)
129 if (l->sym == sym)
131 *pfile = l->file;
132 *pline = l->line;
133 return 1;
137 return 0;
140 /* Utilities for building expressions.
141 Since complex expressions are recorded as symbols for use in other
142 expressions these return a symbolS * and not an expressionS *.
143 These explicitly do not take an "add_number" argument. */
144 /* ??? For completeness' sake one might want expr_build_symbol.
145 It would just return its argument. */
147 /* Build an expression for an unsigned constant.
148 The corresponding one for signed constants is missing because
149 there's currently no need for it. One could add an unsigned_p flag
150 but that seems more clumsy. */
152 symbolS *
153 expr_build_uconstant (offsetT value)
155 expressionS e;
157 e.X_op = O_constant;
158 e.X_add_number = value;
159 e.X_unsigned = 1;
160 e.X_extrabit = 0;
161 return make_expr_symbol (&e);
164 /* Build an expression for the current location ('.'). */
166 symbolS *
167 expr_build_dot (void)
169 expressionS e;
171 current_location (&e);
172 return symbol_clone_if_forward_ref (make_expr_symbol (&e));
175 /* Build any floating-point literal here.
176 Also build any bignum literal here. */
178 /* Seems atof_machine can backscan through generic_bignum and hit whatever
179 happens to be loaded before it in memory. And its way too complicated
180 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
181 and never write into the early words, thus they'll always be zero.
182 I hate Dean's floating-point code. Bleh. */
183 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
185 FLONUM_TYPE generic_floating_point_number = {
186 &generic_bignum[6], /* low. (JF: Was 0) */
187 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
188 0, /* leader. */
189 0, /* exponent. */
190 0 /* sign. */
194 static void
195 floating_constant (expressionS *expressionP)
197 /* input_line_pointer -> floating-point constant. */
198 int error_code;
200 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
201 &generic_floating_point_number);
203 if (error_code)
205 if (error_code == ERROR_EXPONENT_OVERFLOW)
207 as_bad (_("bad floating-point constant: exponent overflow"));
209 else
211 as_bad (_("bad floating-point constant: unknown error code=%d"),
212 error_code);
215 expressionP->X_op = O_big;
216 /* input_line_pointer -> just after constant, which may point to
217 whitespace. */
218 expressionP->X_add_number = -1;
221 static valueT
222 generic_bignum_to_int32 (void)
224 valueT number =
225 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
226 | (generic_bignum[0] & LITTLENUM_MASK);
227 number &= 0xffffffff;
228 return number;
231 #ifdef BFD64
232 static valueT
233 generic_bignum_to_int64 (void)
235 valueT number =
236 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
237 << LITTLENUM_NUMBER_OF_BITS)
238 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
239 << LITTLENUM_NUMBER_OF_BITS)
240 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
241 << LITTLENUM_NUMBER_OF_BITS)
242 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
243 return number;
245 #endif
247 static void
248 integer_constant (int radix, expressionS *expressionP)
250 char *start; /* Start of number. */
251 char *suffix = NULL;
252 char c;
253 valueT number; /* Offset or (absolute) value. */
254 short int digit; /* Value of next digit in current radix. */
255 short int maxdig = 0; /* Highest permitted digit value. */
256 int too_many_digits = 0; /* If we see >= this number of. */
257 char *name; /* Points to name of symbol. */
258 symbolS *symbolP; /* Points to symbol. */
260 int small; /* True if fits in 32 bits. */
262 /* May be bignum, or may fit in 32 bits. */
263 /* Most numbers fit into 32 bits, and we want this case to be fast.
264 so we pretend it will fit into 32 bits. If, after making up a 32
265 bit number, we realise that we have scanned more digits than
266 comfortably fit into 32 bits, we re-scan the digits coding them
267 into a bignum. For decimal and octal numbers we are
268 conservative: Some numbers may be assumed bignums when in fact
269 they do fit into 32 bits. Numbers of any radix can have excess
270 leading zeros: We strive to recognise this and cast them back
271 into 32 bits. We must check that the bignum really is more than
272 32 bits, and change it back to a 32-bit number if it fits. The
273 number we are looking for is expected to be positive, but if it
274 fits into 32 bits as an unsigned number, we let it be a 32-bit
275 number. The cavalier approach is for speed in ordinary cases. */
276 /* This has been extended for 64 bits. We blindly assume that if
277 you're compiling in 64-bit mode, the target is a 64-bit machine.
278 This should be cleaned up. */
280 #ifdef BFD64
281 #define valuesize 64
282 #else /* includes non-bfd case, mostly */
283 #define valuesize 32
284 #endif
286 if (is_end_of_line[(unsigned char) *input_line_pointer])
288 expressionP->X_op = O_absent;
289 return;
292 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
294 int flt = 0;
296 /* In MRI mode, the number may have a suffix indicating the
297 radix. For that matter, it might actually be a floating
298 point constant. */
299 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
301 if (*suffix == 'e' || *suffix == 'E')
302 flt = 1;
305 if (suffix == input_line_pointer)
307 radix = 10;
308 suffix = NULL;
310 else
312 c = *--suffix;
313 c = TOUPPER (c);
314 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
315 we distinguish between 'B' and 'b'. This is the case for
316 Z80. */
317 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
318 radix = 2;
319 else if (c == 'D')
320 radix = 10;
321 else if (c == 'O' || c == 'Q')
322 radix = 8;
323 else if (c == 'H')
324 radix = 16;
325 else if (suffix[1] == '.' || c == 'E' || flt)
327 floating_constant (expressionP);
328 return;
330 else
332 radix = 10;
333 suffix = NULL;
338 switch (radix)
340 case 2:
341 maxdig = 2;
342 too_many_digits = valuesize + 1;
343 break;
344 case 8:
345 maxdig = radix = 8;
346 too_many_digits = (valuesize + 2) / 3 + 1;
347 break;
348 case 16:
349 maxdig = radix = 16;
350 too_many_digits = (valuesize + 3) / 4 + 1;
351 break;
352 case 10:
353 maxdig = radix = 10;
354 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
356 #undef valuesize
357 start = input_line_pointer;
358 c = *input_line_pointer++;
359 for (number = 0;
360 (digit = hex_value (c)) < maxdig;
361 c = *input_line_pointer++)
363 number = number * radix + digit;
365 /* c contains character after number. */
366 /* input_line_pointer->char after c. */
367 small = (input_line_pointer - start - 1) < too_many_digits;
369 if (radix == 16 && c == '_')
371 /* This is literal of the form 0x333_0_12345678_1.
372 This example is equivalent to 0x00000333000000001234567800000001. */
374 int num_little_digits = 0;
375 int i;
376 input_line_pointer = start; /* -> 1st digit. */
378 know (LITTLENUM_NUMBER_OF_BITS == 16);
380 for (c = '_'; c == '_'; num_little_digits += 2)
383 /* Convert one 64-bit word. */
384 int ndigit = 0;
385 number = 0;
386 for (c = *input_line_pointer++;
387 (digit = hex_value (c)) < maxdig;
388 c = *(input_line_pointer++))
390 number = number * radix + digit;
391 ndigit++;
394 /* Check for 8 digit per word max. */
395 if (ndigit > 8)
396 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
398 /* Add this chunk to the bignum.
399 Shift things down 2 little digits. */
400 know (LITTLENUM_NUMBER_OF_BITS == 16);
401 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
402 i >= 2;
403 i--)
404 generic_bignum[i] = generic_bignum[i - 2];
406 /* Add the new digits as the least significant new ones. */
407 generic_bignum[0] = number & 0xffffffff;
408 generic_bignum[1] = number >> 16;
411 /* Again, c is char after number, input_line_pointer->after c. */
413 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
414 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
416 gas_assert (num_little_digits >= 4);
418 if (num_little_digits != 8)
419 as_bad (_("a bignum with underscores must have exactly 4 words"));
421 /* We might have some leading zeros. These can be trimmed to give
422 us a change to fit this constant into a small number. */
423 while (generic_bignum[num_little_digits - 1] == 0
424 && num_little_digits > 1)
425 num_little_digits--;
427 if (num_little_digits <= 2)
429 /* will fit into 32 bits. */
430 number = generic_bignum_to_int32 ();
431 small = 1;
433 #ifdef BFD64
434 else if (num_little_digits <= 4)
436 /* Will fit into 64 bits. */
437 number = generic_bignum_to_int64 ();
438 small = 1;
440 #endif
441 else
443 small = 0;
445 /* Number of littlenums in the bignum. */
446 number = num_little_digits;
449 else if (!small)
451 /* We saw a lot of digits. manufacture a bignum the hard way. */
452 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
453 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
454 long carry;
456 leader = generic_bignum;
457 generic_bignum[0] = 0;
458 generic_bignum[1] = 0;
459 generic_bignum[2] = 0;
460 generic_bignum[3] = 0;
461 input_line_pointer = start; /* -> 1st digit. */
462 c = *input_line_pointer++;
463 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
465 for (pointer = generic_bignum; pointer <= leader; pointer++)
467 long work;
469 work = carry + radix * *pointer;
470 *pointer = work & LITTLENUM_MASK;
471 carry = work >> LITTLENUM_NUMBER_OF_BITS;
473 if (carry)
475 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
477 /* Room to grow a longer bignum. */
478 *++leader = carry;
482 /* Again, c is char after number. */
483 /* input_line_pointer -> after c. */
484 know (LITTLENUM_NUMBER_OF_BITS == 16);
485 if (leader < generic_bignum + 2)
487 /* Will fit into 32 bits. */
488 number = generic_bignum_to_int32 ();
489 small = 1;
491 #ifdef BFD64
492 else if (leader < generic_bignum + 4)
494 /* Will fit into 64 bits. */
495 number = generic_bignum_to_int64 ();
496 small = 1;
498 #endif
499 else
501 /* Number of littlenums in the bignum. */
502 number = leader - generic_bignum + 1;
506 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
507 && suffix != NULL
508 && input_line_pointer - 1 == suffix)
509 c = *input_line_pointer++;
511 #ifndef tc_allow_U_suffix
512 #define tc_allow_U_suffix 1
513 #endif
514 /* PR 19910: Look for, and ignore, a U suffix to the number. */
515 if (tc_allow_U_suffix && (c == 'U' || c == 'u'))
516 c = * input_line_pointer++;
518 #ifndef tc_allow_L_suffix
519 #define tc_allow_L_suffix 1
520 #endif
521 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
522 if (tc_allow_L_suffix)
523 while (c == 'L' || c == 'l')
524 c = * input_line_pointer++;
526 if (small)
528 /* Here with number, in correct radix. c is the next char.
529 Note that unlike un*x, we allow "011f" "0x9f" to both mean
530 the same as the (conventional) "9f".
531 This is simply easier than checking for strict canonical
532 form. Syntax sux! */
534 if (LOCAL_LABELS_FB && c == 'b')
536 /* Backward ref to local label.
537 Because it is backward, expect it to be defined. */
538 /* Construct a local label. */
539 name = fb_label_name ((int) number, 0);
541 /* Seen before, or symbol is defined: OK. */
542 symbolP = symbol_find (name);
543 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
545 /* Local labels are never absolute. Don't waste time
546 checking absoluteness. */
547 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
549 expressionP->X_op = O_symbol;
550 expressionP->X_add_symbol = symbolP;
552 else
554 /* Either not seen or not defined. */
555 /* @@ Should print out the original string instead of
556 the parsed number. */
557 as_bad (_("backward ref to unknown label \"%d:\""),
558 (int) number);
559 expressionP->X_op = O_constant;
562 expressionP->X_add_number = 0;
563 } /* case 'b' */
564 else if (LOCAL_LABELS_FB && c == 'f')
566 /* Forward reference. Expect symbol to be undefined or
567 unknown. undefined: seen it before. unknown: never seen
568 it before.
570 Construct a local label name, then an undefined symbol.
571 Don't create a xseg frag for it: caller may do that.
572 Just return it as never seen before. */
573 name = fb_label_name ((int) number, 1);
574 symbolP = symbol_find_or_make (name);
575 /* We have no need to check symbol properties. */
576 #ifndef many_segments
577 /* Since "know" puts its arg into a "string", we
578 can't have newlines in the argument. */
579 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
580 #endif
581 expressionP->X_op = O_symbol;
582 expressionP->X_add_symbol = symbolP;
583 expressionP->X_add_number = 0;
584 } /* case 'f' */
585 else if (LOCAL_LABELS_DOLLAR && c == '$')
587 /* If the dollar label is *currently* defined, then this is just
588 another reference to it. If it is not *currently* defined,
589 then this is a fresh instantiation of that number, so create
590 it. */
592 if (dollar_label_defined ((long) number))
594 name = dollar_label_name ((long) number, 0);
595 symbolP = symbol_find (name);
596 know (symbolP != NULL);
598 else
600 name = dollar_label_name ((long) number, 1);
601 symbolP = symbol_find_or_make (name);
604 expressionP->X_op = O_symbol;
605 expressionP->X_add_symbol = symbolP;
606 expressionP->X_add_number = 0;
607 } /* case '$' */
608 else
610 expressionP->X_op = O_constant;
611 expressionP->X_add_number = number;
612 input_line_pointer--; /* Restore following character. */
613 } /* Really just a number. */
615 else
617 /* Not a small number. */
618 expressionP->X_op = O_big;
619 expressionP->X_add_number = number; /* Number of littlenums. */
620 input_line_pointer--; /* -> char following number. */
624 /* Parse an MRI multi character constant. */
626 static void
627 mri_char_constant (expressionS *expressionP)
629 int i;
631 if (*input_line_pointer == '\''
632 && input_line_pointer[1] != '\'')
634 expressionP->X_op = O_constant;
635 expressionP->X_add_number = 0;
636 return;
639 /* In order to get the correct byte ordering, we must build the
640 number in reverse. */
641 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
643 int j;
645 generic_bignum[i] = 0;
646 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
648 if (*input_line_pointer == '\'')
650 if (input_line_pointer[1] != '\'')
651 break;
652 ++input_line_pointer;
654 generic_bignum[i] <<= 8;
655 generic_bignum[i] += *input_line_pointer;
656 ++input_line_pointer;
659 if (i < SIZE_OF_LARGE_NUMBER - 1)
661 /* If there is more than one littlenum, left justify the
662 last one to make it match the earlier ones. If there is
663 only one, we can just use the value directly. */
664 for (; j < CHARS_PER_LITTLENUM; j++)
665 generic_bignum[i] <<= 8;
668 if (*input_line_pointer == '\''
669 && input_line_pointer[1] != '\'')
670 break;
673 if (i < 0)
675 as_bad (_("character constant too large"));
676 i = 0;
679 if (i > 0)
681 int c;
682 int j;
684 c = SIZE_OF_LARGE_NUMBER - i;
685 for (j = 0; j < c; j++)
686 generic_bignum[j] = generic_bignum[i + j];
687 i = c;
690 know (LITTLENUM_NUMBER_OF_BITS == 16);
691 if (i > 2)
693 expressionP->X_op = O_big;
694 expressionP->X_add_number = i;
696 else
698 expressionP->X_op = O_constant;
699 if (i < 2)
700 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
701 else
702 expressionP->X_add_number =
703 (((generic_bignum[1] & LITTLENUM_MASK)
704 << LITTLENUM_NUMBER_OF_BITS)
705 | (generic_bignum[0] & LITTLENUM_MASK));
708 /* Skip the final closing quote. */
709 ++input_line_pointer;
712 /* Return an expression representing the current location. This
713 handles the magic symbol `.'. */
715 void
716 current_location (expressionS *expressionp)
718 if (now_seg == absolute_section)
720 expressionp->X_op = O_constant;
721 expressionp->X_add_number = abs_section_offset;
723 else
725 expressionp->X_op = O_symbol;
726 expressionp->X_add_symbol = &dot_symbol;
727 expressionp->X_add_number = 0;
731 /* In: Input_line_pointer points to 1st char of operand, which may
732 be a space.
734 Out: An expressionS.
735 The operand may have been empty: in this case X_op == O_absent.
736 Input_line_pointer->(next non-blank) char after operand. */
738 static segT
739 operand (expressionS *expressionP, enum expr_mode mode)
741 char c;
742 symbolS *symbolP; /* Points to symbol. */
743 char *name; /* Points to name of symbol. */
744 segT segment;
746 /* All integers are regarded as unsigned unless they are negated.
747 This is because the only thing which cares whether a number is
748 unsigned is the code in emit_expr which extends constants into
749 bignums. It should only sign extend negative numbers, so that
750 something like ``.quad 0x80000000'' is not sign extended even
751 though it appears negative if valueT is 32 bits. */
752 expressionP->X_unsigned = 1;
753 expressionP->X_extrabit = 0;
755 /* Digits, assume it is a bignum. */
757 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
758 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
760 if (is_end_of_line[(unsigned char) c])
761 goto eol;
763 switch (c)
765 case '1':
766 case '2':
767 case '3':
768 case '4':
769 case '5':
770 case '6':
771 case '7':
772 case '8':
773 case '9':
774 input_line_pointer--;
776 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
777 ? 0 : 10,
778 expressionP);
779 break;
781 #ifdef LITERAL_PREFIXDOLLAR_HEX
782 case '$':
783 /* $L is the start of a local label, not a hex constant. */
784 if (* input_line_pointer == 'L')
785 goto isname;
786 integer_constant (16, expressionP);
787 break;
788 #endif
790 #ifdef LITERAL_PREFIXPERCENT_BIN
791 case '%':
792 integer_constant (2, expressionP);
793 break;
794 #endif
796 case '0':
797 /* Non-decimal radix. */
799 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
801 char *s;
803 /* Check for a hex or float constant. */
804 for (s = input_line_pointer; hex_p (*s); s++)
806 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
808 --input_line_pointer;
809 integer_constant (0, expressionP);
810 break;
813 c = *input_line_pointer;
814 switch (c)
816 case 'o':
817 case 'O':
818 case 'q':
819 case 'Q':
820 case '8':
821 case '9':
822 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
824 integer_constant (0, expressionP);
825 break;
827 /* Fall through. */
828 default:
829 default_case:
830 if (c && strchr (FLT_CHARS, c))
832 input_line_pointer++;
833 floating_constant (expressionP);
834 expressionP->X_add_number = - TOLOWER (c);
836 else
838 /* The string was only zero. */
839 expressionP->X_op = O_constant;
840 expressionP->X_add_number = 0;
843 break;
845 case 'x':
846 case 'X':
847 if (flag_m68k_mri)
848 goto default_case;
849 input_line_pointer++;
850 integer_constant (16, expressionP);
851 break;
853 case 'b':
854 if (LOCAL_LABELS_FB && !flag_m68k_mri
855 && input_line_pointer[1] != '0'
856 && input_line_pointer[1] != '1')
858 /* Parse this as a back reference to label 0. */
859 input_line_pointer--;
860 integer_constant (10, expressionP);
861 break;
863 /* Otherwise, parse this as a binary number. */
864 /* Fall through. */
865 case 'B':
866 if (input_line_pointer[1] == '0'
867 || input_line_pointer[1] == '1')
869 input_line_pointer++;
870 integer_constant (2, expressionP);
871 break;
873 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
874 input_line_pointer++;
875 goto default_case;
877 case '0':
878 case '1':
879 case '2':
880 case '3':
881 case '4':
882 case '5':
883 case '6':
884 case '7':
885 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
886 ? 0 : 8,
887 expressionP);
888 break;
890 case 'f':
891 if (LOCAL_LABELS_FB)
893 int is_label = 1;
895 /* If it says "0f" and it could possibly be a floating point
896 number, make it one. Otherwise, make it a local label,
897 and try to deal with parsing the rest later. */
898 if (!is_end_of_line[(unsigned char) input_line_pointer[1]]
899 && strchr (FLT_CHARS, 'f') != NULL)
901 char *cp = input_line_pointer + 1;
903 atof_generic (&cp, ".", EXP_CHARS,
904 &generic_floating_point_number);
906 /* Was nothing parsed, or does it look like an
907 expression? */
908 is_label = (cp == input_line_pointer + 1
909 || (cp == input_line_pointer + 2
910 && (cp[-1] == '-' || cp[-1] == '+'))
911 || *cp == 'f'
912 || *cp == 'b');
914 if (is_label)
916 input_line_pointer--;
917 integer_constant (10, expressionP);
918 break;
921 /* Fall through. */
923 case 'd':
924 case 'D':
925 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
927 integer_constant (0, expressionP);
928 break;
930 /* Fall through. */
931 case 'F':
932 case 'r':
933 case 'e':
934 case 'E':
935 case 'g':
936 case 'G':
937 input_line_pointer++;
938 floating_constant (expressionP);
939 expressionP->X_add_number = - TOLOWER (c);
940 break;
942 case '$':
943 if (LOCAL_LABELS_DOLLAR)
945 integer_constant (10, expressionP);
946 break;
948 else
949 goto default_case;
952 break;
954 #ifndef NEED_INDEX_OPERATOR
955 case '[':
956 # ifdef md_need_index_operator
957 if (md_need_index_operator())
958 goto de_fault;
959 # endif
960 #endif
961 /* Fall through. */
962 case '(':
963 /* Didn't begin with digit & not a name. */
964 segment = expr (0, expressionP, mode);
965 /* expression () will pass trailing whitespace. */
966 if ((c == '(' && *input_line_pointer != ')')
967 || (c == '[' && *input_line_pointer != ']'))
969 if (* input_line_pointer)
970 as_bad (_("found '%c', expected: '%c'"),
971 * input_line_pointer, c == '(' ? ')' : ']');
972 else
973 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
975 else
976 input_line_pointer++;
977 SKIP_WHITESPACE ();
978 /* Here with input_line_pointer -> char after "(...)". */
979 return segment;
981 #ifdef TC_M68K
982 case 'E':
983 if (! flag_m68k_mri || *input_line_pointer != '\'')
984 goto de_fault;
985 as_bad (_("EBCDIC constants are not supported"));
986 /* Fall through. */
987 case 'A':
988 if (! flag_m68k_mri || *input_line_pointer != '\'')
989 goto de_fault;
990 ++input_line_pointer;
991 #endif
992 /* Fall through. */
993 case '\'':
994 if (! flag_m68k_mri)
996 /* Warning: to conform to other people's assemblers NO
997 ESCAPEMENT is permitted for a single quote. The next
998 character, parity errors and all, is taken as the value
999 of the operand. VERY KINKY. */
1000 expressionP->X_op = O_constant;
1001 expressionP->X_add_number = *input_line_pointer++;
1002 break;
1005 mri_char_constant (expressionP);
1006 break;
1008 #ifdef TC_M68K
1009 case '"':
1010 /* Double quote is the bitwise not operator in MRI mode. */
1011 if (! flag_m68k_mri)
1012 goto de_fault;
1013 #endif
1014 /* Fall through. */
1015 case '~':
1016 /* '~' is permitted to start a label on the Delta. */
1017 if (is_name_beginner (c))
1018 goto isname;
1019 /* Fall through. */
1020 case '!':
1021 case '-':
1022 case '+':
1024 #ifdef md_operator
1025 unary:
1026 #endif
1027 operand (expressionP, mode);
1028 if (expressionP->X_op == O_constant)
1030 /* input_line_pointer -> char after operand. */
1031 if (c == '-')
1033 expressionP->X_add_number
1034 = - (addressT) expressionP->X_add_number;
1035 /* Notice: '-' may overflow: no warning is given.
1036 This is compatible with other people's
1037 assemblers. Sigh. */
1038 expressionP->X_unsigned = 0;
1039 if (expressionP->X_add_number)
1040 expressionP->X_extrabit ^= 1;
1042 else if (c == '~' || c == '"')
1043 expressionP->X_add_number = ~ expressionP->X_add_number;
1044 else if (c == '!')
1045 expressionP->X_add_number = ! expressionP->X_add_number;
1047 else if (expressionP->X_op == O_big
1048 && expressionP->X_add_number <= 0
1049 && c == '-'
1050 && (generic_floating_point_number.sign == '+'
1051 || generic_floating_point_number.sign == 'P'))
1053 /* Negative flonum (eg, -1.000e0). */
1054 if (generic_floating_point_number.sign == '+')
1055 generic_floating_point_number.sign = '-';
1056 else
1057 generic_floating_point_number.sign = 'N';
1059 else if (expressionP->X_op == O_big
1060 && expressionP->X_add_number > 0)
1062 int i;
1064 if (c == '~' || c == '-')
1066 for (i = 0; i < expressionP->X_add_number; ++i)
1067 generic_bignum[i] = ~generic_bignum[i];
1069 /* Extend the bignum to at least the size of .octa. */
1070 if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1072 expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1073 for (; i < expressionP->X_add_number; ++i)
1074 generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1077 if (c == '-')
1078 for (i = 0; i < expressionP->X_add_number; ++i)
1080 generic_bignum[i] += 1;
1081 if (generic_bignum[i])
1082 break;
1085 else if (c == '!')
1087 for (i = 0; i < expressionP->X_add_number; ++i)
1088 if (generic_bignum[i] != 0)
1089 break;
1090 expressionP->X_add_number = i >= expressionP->X_add_number;
1091 expressionP->X_op = O_constant;
1092 expressionP->X_unsigned = 1;
1093 expressionP->X_extrabit = 0;
1096 else if (expressionP->X_op != O_illegal
1097 && expressionP->X_op != O_absent)
1099 if (c != '+')
1101 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1102 if (c == '-')
1103 expressionP->X_op = O_uminus;
1104 else if (c == '~' || c == '"')
1105 expressionP->X_op = O_bit_not;
1106 else
1107 expressionP->X_op = O_logical_not;
1108 expressionP->X_add_number = 0;
1111 else
1112 as_warn (_("Unary operator %c ignored because bad operand follows"),
1115 break;
1117 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1118 case '$':
1119 /* '$' is the program counter when in MRI mode, or when
1120 DOLLAR_DOT is defined. */
1121 #ifndef DOLLAR_DOT
1122 if (! flag_m68k_mri)
1123 goto de_fault;
1124 #endif
1125 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1127 /* In MRI mode and on Z80, '$' is also used as the prefix
1128 for a hexadecimal constant. */
1129 integer_constant (16, expressionP);
1130 break;
1133 if (is_part_of_name (*input_line_pointer))
1134 goto isname;
1136 current_location (expressionP);
1137 break;
1138 #endif
1140 case '.':
1141 if (!is_part_of_name (*input_line_pointer))
1143 current_location (expressionP);
1144 break;
1146 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1147 && ! is_part_of_name (input_line_pointer[8]))
1148 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1149 && ! is_part_of_name (input_line_pointer[7])))
1151 int start;
1153 start = (input_line_pointer[1] == 't'
1154 || input_line_pointer[1] == 'T');
1155 input_line_pointer += start ? 8 : 7;
1156 SKIP_WHITESPACE ();
1158 /* Cover for the as_bad () invocations below. */
1159 expressionP->X_op = O_absent;
1161 if (*input_line_pointer != '(')
1162 as_bad (_("syntax error in .startof. or .sizeof."));
1163 else
1165 char *buf;
1167 ++input_line_pointer;
1168 SKIP_WHITESPACE ();
1169 c = get_symbol_name (& name);
1170 if (! *name)
1172 as_bad (_("expected symbol name"));
1173 (void) restore_line_pointer (c);
1174 if (c != ')')
1175 ignore_rest_of_line ();
1176 else
1177 ++input_line_pointer;
1178 break;
1181 buf = concat (start ? ".startof." : ".sizeof.", name,
1182 (char *) NULL);
1183 symbolP = symbol_make (buf);
1184 free (buf);
1186 expressionP->X_op = O_symbol;
1187 expressionP->X_add_symbol = symbolP;
1188 expressionP->X_add_number = 0;
1190 *input_line_pointer = c;
1191 SKIP_WHITESPACE_AFTER_NAME ();
1192 if (*input_line_pointer != ')')
1193 as_bad (_("syntax error in .startof. or .sizeof."));
1194 else
1195 ++input_line_pointer;
1197 break;
1199 else
1201 goto isname;
1204 case ',':
1205 eol:
1206 /* Can't imagine any other kind of operand. */
1207 expressionP->X_op = O_absent;
1208 input_line_pointer--;
1209 break;
1211 #ifdef TC_M68K
1212 case '%':
1213 if (! flag_m68k_mri)
1214 goto de_fault;
1215 integer_constant (2, expressionP);
1216 break;
1218 case '@':
1219 if (! flag_m68k_mri)
1220 goto de_fault;
1221 integer_constant (8, expressionP);
1222 break;
1224 case ':':
1225 if (! flag_m68k_mri)
1226 goto de_fault;
1228 /* In MRI mode, this is a floating point constant represented
1229 using hexadecimal digits. */
1231 ++input_line_pointer;
1232 integer_constant (16, expressionP);
1233 break;
1235 case '*':
1236 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1237 goto de_fault;
1239 current_location (expressionP);
1240 break;
1241 #endif
1243 default:
1244 #if defined(md_need_index_operator) || defined(TC_M68K)
1245 de_fault:
1246 #endif
1247 if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
1249 /* Identifier begins here.
1250 This is kludged for speed, so code is repeated. */
1251 isname:
1252 -- input_line_pointer;
1253 c = get_symbol_name (&name);
1255 #ifdef md_operator
1257 operatorT op = md_operator (name, 1, &c);
1259 switch (op)
1261 case O_uminus:
1262 restore_line_pointer (c);
1263 c = '-';
1264 goto unary;
1265 case O_bit_not:
1266 restore_line_pointer (c);
1267 c = '~';
1268 goto unary;
1269 case O_logical_not:
1270 restore_line_pointer (c);
1271 c = '!';
1272 goto unary;
1273 case O_illegal:
1274 as_bad (_("invalid use of operator \"%s\""), name);
1275 break;
1276 default:
1277 break;
1280 if (op != O_absent && op != O_illegal)
1282 restore_line_pointer (c);
1283 expr (9, expressionP, mode);
1284 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1285 expressionP->X_op_symbol = NULL;
1286 expressionP->X_add_number = 0;
1287 expressionP->X_op = op;
1288 break;
1291 #endif
1293 #ifdef md_parse_name
1294 /* This is a hook for the backend to parse certain names
1295 specially in certain contexts. If a name always has a
1296 specific value, it can often be handled by simply
1297 entering it in the symbol table. */
1298 if (md_parse_name (name, expressionP, mode, &c))
1300 restore_line_pointer (c);
1301 break;
1303 #endif
1305 symbolP = symbol_find_or_make (name);
1307 /* If we have an absolute symbol or a reg, then we know its
1308 value now. */
1309 segment = S_GET_SEGMENT (symbolP);
1310 if (mode != expr_defer
1311 && segment == absolute_section
1312 && !S_FORCE_RELOC (symbolP, 0))
1314 expressionP->X_op = O_constant;
1315 expressionP->X_add_number = S_GET_VALUE (symbolP);
1317 else if (mode != expr_defer && segment == reg_section)
1319 expressionP->X_op = O_register;
1320 expressionP->X_add_number = S_GET_VALUE (symbolP);
1322 else
1324 expressionP->X_op = O_symbol;
1325 expressionP->X_add_symbol = symbolP;
1326 expressionP->X_add_number = 0;
1329 restore_line_pointer (c);
1331 else
1333 /* Let the target try to parse it. Success is indicated by changing
1334 the X_op field to something other than O_absent and pointing
1335 input_line_pointer past the expression. If it can't parse the
1336 expression, X_op and input_line_pointer should be unchanged. */
1337 expressionP->X_op = O_absent;
1338 --input_line_pointer;
1339 md_operand (expressionP);
1340 if (expressionP->X_op == O_absent)
1342 ++input_line_pointer;
1343 as_bad (_("bad expression"));
1344 expressionP->X_op = O_constant;
1345 expressionP->X_add_number = 0;
1348 break;
1351 /* It is more 'efficient' to clean up the expressionS when they are
1352 created. Doing it here saves lines of code. */
1353 clean_up_expression (expressionP);
1354 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1355 know (*input_line_pointer != ' ');
1357 /* The PA port needs this information. */
1358 if (expressionP->X_add_symbol)
1359 symbol_mark_used (expressionP->X_add_symbol);
1361 if (mode != expr_defer)
1363 expressionP->X_add_symbol
1364 = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1365 expressionP->X_op_symbol
1366 = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1369 switch (expressionP->X_op)
1371 default:
1372 return absolute_section;
1373 case O_symbol:
1374 return S_GET_SEGMENT (expressionP->X_add_symbol);
1375 case O_register:
1376 return reg_section;
1380 /* Internal. Simplify a struct expression for use by expr (). */
1382 /* In: address of an expressionS.
1383 The X_op field of the expressionS may only take certain values.
1384 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1386 Out: expressionS may have been modified:
1387 Unused fields zeroed to help expr (). */
1389 static void
1390 clean_up_expression (expressionS *expressionP)
1392 switch (expressionP->X_op)
1394 case O_illegal:
1395 case O_absent:
1396 expressionP->X_add_number = 0;
1397 /* Fall through. */
1398 case O_big:
1399 case O_constant:
1400 case O_register:
1401 expressionP->X_add_symbol = NULL;
1402 /* Fall through. */
1403 case O_symbol:
1404 case O_uminus:
1405 case O_bit_not:
1406 expressionP->X_op_symbol = NULL;
1407 break;
1408 default:
1409 break;
1413 /* Expression parser. */
1415 /* We allow an empty expression, and just assume (absolute,0) silently.
1416 Unary operators and parenthetical expressions are treated as operands.
1417 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1419 We used to do an aho/ullman shift-reduce parser, but the logic got so
1420 warped that I flushed it and wrote a recursive-descent parser instead.
1421 Now things are stable, would anybody like to write a fast parser?
1422 Most expressions are either register (which does not even reach here)
1423 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1424 So I guess it doesn't really matter how inefficient more complex expressions
1425 are parsed.
1427 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1428 Also, we have consumed any leading or trailing spaces (operand does that)
1429 and done all intervening operators.
1431 This returns the segment of the result, which will be
1432 absolute_section or the segment of a symbol. */
1434 #undef __
1435 #define __ O_illegal
1436 #ifndef O_SINGLE_EQ
1437 #define O_SINGLE_EQ O_illegal
1438 #endif
1440 /* Maps ASCII -> operators. */
1441 static const operatorT op_encoding[256] = {
1442 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1443 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1445 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1446 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1447 __, __, __, __, __, __, __, __,
1448 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1449 __, __, __, __, __, __, __, __,
1450 __, __, __, __, __, __, __, __,
1451 __, __, __, __, __, __, __, __,
1452 __, __, __,
1453 #ifdef NEED_INDEX_OPERATOR
1454 O_index,
1455 #else
1457 #endif
1458 __, __, O_bit_exclusive_or, __,
1459 __, __, __, __, __, __, __, __,
1460 __, __, __, __, __, __, __, __,
1461 __, __, __, __, __, __, __, __,
1462 __, __, __, __, O_bit_inclusive_or, __, __, __,
1464 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1465 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1466 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1467 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1468 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1469 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1470 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1471 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1474 /* Rank Examples
1475 0 operand, (expression)
1476 1 ||
1477 2 &&
1478 3 == <> < <= >= >
1479 4 + -
1480 5 used for * / % in MRI mode
1481 6 & ^ ! |
1482 7 * / % << >>
1483 8 unary - unary ~
1485 static operator_rankT op_rank[O_max] = {
1486 0, /* O_illegal */
1487 0, /* O_absent */
1488 0, /* O_constant */
1489 0, /* O_symbol */
1490 0, /* O_symbol_rva */
1491 0, /* O_register */
1492 0, /* O_big */
1493 9, /* O_uminus */
1494 9, /* O_bit_not */
1495 9, /* O_logical_not */
1496 8, /* O_multiply */
1497 8, /* O_divide */
1498 8, /* O_modulus */
1499 8, /* O_left_shift */
1500 8, /* O_right_shift */
1501 7, /* O_bit_inclusive_or */
1502 7, /* O_bit_or_not */
1503 7, /* O_bit_exclusive_or */
1504 7, /* O_bit_and */
1505 5, /* O_add */
1506 5, /* O_subtract */
1507 4, /* O_eq */
1508 4, /* O_ne */
1509 4, /* O_lt */
1510 4, /* O_le */
1511 4, /* O_ge */
1512 4, /* O_gt */
1513 3, /* O_logical_and */
1514 2, /* O_logical_or */
1515 1, /* O_index */
1518 /* Unfortunately, in MRI mode for the m68k, multiplication and
1519 division have lower precedence than the bit wise operators. This
1520 function sets the operator precedences correctly for the current
1521 mode. Also, MRI uses a different bit_not operator, and this fixes
1522 that as well. */
1524 #define STANDARD_MUL_PRECEDENCE 8
1525 #define MRI_MUL_PRECEDENCE 6
1527 void
1528 expr_set_precedence (void)
1530 if (flag_m68k_mri)
1532 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1533 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1534 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1536 else
1538 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1539 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1540 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1544 void
1545 expr_set_rank (operatorT op, operator_rankT rank)
1547 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1548 op_rank[op] = rank;
1551 /* Initialize the expression parser. */
1553 void
1554 expr_begin (void)
1556 expr_set_precedence ();
1558 /* Verify that X_op field is wide enough. */
1560 expressionS e;
1561 e.X_op = O_max;
1562 gas_assert (e.X_op == O_max);
1566 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1567 sets NUM_CHARS to the number of characters in the operator.
1568 Does not advance INPUT_LINE_POINTER. */
1570 static inline operatorT
1571 operatorf (int *num_chars)
1573 int c;
1574 operatorT ret;
1576 c = *input_line_pointer & 0xff;
1577 *num_chars = 1;
1579 if (is_end_of_line[c])
1580 return O_illegal;
1582 #ifdef md_operator
1583 if (is_name_beginner (c))
1585 char *name;
1586 char ec = get_symbol_name (& name);
1588 ret = md_operator (name, 2, &ec);
1589 switch (ret)
1591 case O_absent:
1592 *input_line_pointer = ec;
1593 input_line_pointer = name;
1594 break;
1595 case O_uminus:
1596 case O_bit_not:
1597 case O_logical_not:
1598 as_bad (_("invalid use of operator \"%s\""), name);
1599 ret = O_illegal;
1600 /* FALLTHROUGH */
1601 default:
1602 *input_line_pointer = ec;
1603 *num_chars = input_line_pointer - name;
1604 input_line_pointer = name;
1605 return ret;
1608 #endif
1610 switch (c)
1612 default:
1613 ret = op_encoding[c];
1614 #ifdef md_operator
1615 if (ret == O_illegal)
1617 char *start = input_line_pointer;
1619 ret = md_operator (NULL, 2, NULL);
1620 if (ret != O_illegal)
1621 *num_chars = input_line_pointer - start;
1622 input_line_pointer = start;
1624 #endif
1625 return ret;
1627 case '+':
1628 case '-':
1629 return op_encoding[c];
1631 case '<':
1632 switch (input_line_pointer[1])
1634 default:
1635 return op_encoding[c];
1636 case '<':
1637 ret = O_left_shift;
1638 break;
1639 case '>':
1640 ret = O_ne;
1641 break;
1642 case '=':
1643 ret = O_le;
1644 break;
1646 *num_chars = 2;
1647 return ret;
1649 case '=':
1650 if (input_line_pointer[1] != '=')
1651 return op_encoding[c];
1653 *num_chars = 2;
1654 return O_eq;
1656 case '>':
1657 switch (input_line_pointer[1])
1659 default:
1660 return op_encoding[c];
1661 case '>':
1662 ret = O_right_shift;
1663 break;
1664 case '=':
1665 ret = O_ge;
1666 break;
1668 *num_chars = 2;
1669 return ret;
1671 case '!':
1672 switch (input_line_pointer[1])
1674 case '!':
1675 /* We accept !! as equivalent to ^ for MRI compatibility. */
1676 *num_chars = 2;
1677 return O_bit_exclusive_or;
1678 case '=':
1679 /* We accept != as equivalent to <>. */
1680 *num_chars = 2;
1681 return O_ne;
1682 default:
1683 if (flag_m68k_mri)
1684 return O_bit_inclusive_or;
1685 return op_encoding[c];
1688 case '|':
1689 if (input_line_pointer[1] != '|')
1690 return op_encoding[c];
1692 *num_chars = 2;
1693 return O_logical_or;
1695 case '&':
1696 if (input_line_pointer[1] != '&')
1697 return op_encoding[c];
1699 *num_chars = 2;
1700 return O_logical_and;
1703 /* NOTREACHED */
1706 /* Implement "word-size + 1 bit" addition for
1707 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1708 is used so that the full range of unsigned word values and the full range of
1709 signed word values can be represented in an O_constant expression, which is
1710 useful e.g. for .sleb128 directives. */
1712 void
1713 add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1715 valueT ures = resultP->X_add_number;
1716 valueT uamount = amount;
1718 resultP->X_add_number += amount;
1720 resultP->X_extrabit ^= rhs_highbit;
1722 if (ures + uamount < ures)
1723 resultP->X_extrabit ^= 1;
1726 /* Similarly, for subtraction. */
1728 void
1729 subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1731 valueT ures = resultP->X_add_number;
1732 valueT uamount = amount;
1734 resultP->X_add_number -= amount;
1736 resultP->X_extrabit ^= rhs_highbit;
1738 if (ures < uamount)
1739 resultP->X_extrabit ^= 1;
1742 /* Parse an expression. */
1744 segT
1745 expr (int rankarg, /* Larger # is higher rank. */
1746 expressionS *resultP, /* Deliver result here. */
1747 enum expr_mode mode /* Controls behavior. */)
1749 operator_rankT rank = (operator_rankT) rankarg;
1750 segT retval;
1751 expressionS right;
1752 operatorT op_left;
1753 operatorT op_right;
1754 int op_chars;
1756 know (rankarg >= 0);
1758 /* Save the value of dot for the fixup code. */
1759 if (rank == 0)
1761 dot_value = frag_now_fix ();
1762 dot_frag = frag_now;
1765 retval = operand (resultP, mode);
1767 /* operand () gobbles spaces. */
1768 know (*input_line_pointer != ' ');
1770 op_left = operatorf (&op_chars);
1771 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1773 segT rightseg;
1774 offsetT frag_off;
1776 input_line_pointer += op_chars; /* -> after operator. */
1778 right.X_md = 0;
1779 rightseg = expr (op_rank[(int) op_left], &right, mode);
1780 if (right.X_op == O_absent)
1782 as_warn (_("missing operand; zero assumed"));
1783 right.X_op = O_constant;
1784 right.X_add_number = 0;
1785 right.X_add_symbol = NULL;
1786 right.X_op_symbol = NULL;
1789 know (*input_line_pointer != ' ');
1791 if (op_left == O_index)
1793 if (*input_line_pointer != ']')
1794 as_bad ("missing right bracket");
1795 else
1797 ++input_line_pointer;
1798 SKIP_WHITESPACE ();
1802 op_right = operatorf (&op_chars);
1804 know (op_right == O_illegal || op_left == O_index
1805 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1806 know ((int) op_left >= (int) O_multiply);
1807 #ifndef md_operator
1808 know ((int) op_left <= (int) O_index);
1809 #else
1810 know ((int) op_left < (int) O_max);
1811 #endif
1813 /* input_line_pointer->after right-hand quantity. */
1814 /* left-hand quantity in resultP. */
1815 /* right-hand quantity in right. */
1816 /* operator in op_left. */
1818 if (resultP->X_op == O_big)
1820 if (resultP->X_add_number > 0)
1821 as_warn (_("left operand is a bignum; integer 0 assumed"));
1822 else
1823 as_warn (_("left operand is a float; integer 0 assumed"));
1824 resultP->X_op = O_constant;
1825 resultP->X_add_number = 0;
1826 resultP->X_add_symbol = NULL;
1827 resultP->X_op_symbol = NULL;
1829 if (right.X_op == O_big)
1831 if (right.X_add_number > 0)
1832 as_warn (_("right operand is a bignum; integer 0 assumed"));
1833 else
1834 as_warn (_("right operand is a float; integer 0 assumed"));
1835 right.X_op = O_constant;
1836 right.X_add_number = 0;
1837 right.X_add_symbol = NULL;
1838 right.X_op_symbol = NULL;
1841 if (mode == expr_defer
1842 && ((resultP->X_add_symbol != NULL
1843 && S_IS_FORWARD_REF (resultP->X_add_symbol))
1844 || (right.X_add_symbol != NULL
1845 && S_IS_FORWARD_REF (right.X_add_symbol))))
1846 goto general;
1848 /* Optimize common cases. */
1849 #ifdef md_optimize_expr
1850 if (md_optimize_expr (resultP, op_left, &right))
1852 /* Skip. */
1855 else
1856 #endif
1857 #ifndef md_register_arithmetic
1858 # define md_register_arithmetic 1
1859 #endif
1860 if (op_left == O_add && right.X_op == O_constant
1861 && (md_register_arithmetic || resultP->X_op != O_register))
1863 /* X + constant. */
1864 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1866 /* This case comes up in PIC code. */
1867 else if (op_left == O_subtract
1868 && right.X_op == O_symbol
1869 && resultP->X_op == O_symbol
1870 && retval == rightseg
1871 #ifdef md_allow_local_subtract
1872 && md_allow_local_subtract (resultP, & right, rightseg)
1873 #endif
1874 && ((SEG_NORMAL (rightseg)
1875 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1876 && !S_FORCE_RELOC (right.X_add_symbol, 0))
1877 || right.X_add_symbol == resultP->X_add_symbol)
1878 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1879 symbol_get_frag (right.X_add_symbol),
1880 &frag_off))
1882 offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1883 - S_GET_VALUE (right.X_add_symbol);
1884 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1885 subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1886 add_to_result (resultP, symval_diff, symval_diff < 0);
1887 resultP->X_op = O_constant;
1888 resultP->X_add_symbol = 0;
1890 else if (op_left == O_subtract && right.X_op == O_constant
1891 && (md_register_arithmetic || resultP->X_op != O_register))
1893 /* X - constant. */
1894 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1896 else if (op_left == O_add && resultP->X_op == O_constant
1897 && (md_register_arithmetic || right.X_op != O_register))
1899 /* Constant + X. */
1900 resultP->X_op = right.X_op;
1901 resultP->X_add_symbol = right.X_add_symbol;
1902 resultP->X_op_symbol = right.X_op_symbol;
1903 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1904 retval = rightseg;
1906 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1908 /* Constant OP constant. */
1909 offsetT v = right.X_add_number;
1910 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1912 as_warn (_("division by zero"));
1913 v = 1;
1915 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1916 && (op_left == O_left_shift || op_left == O_right_shift))
1918 as_warn_value_out_of_range (_("shift count"), v, 0,
1919 sizeof(valueT) * CHAR_BIT - 1,
1920 NULL, 0);
1921 resultP->X_add_number = v = 0;
1923 switch (op_left)
1925 default: goto general;
1926 case O_multiply: resultP->X_add_number *= v; break;
1927 case O_divide: resultP->X_add_number /= v; break;
1928 case O_modulus: resultP->X_add_number %= v; break;
1929 case O_left_shift: resultP->X_add_number <<= v; break;
1930 case O_right_shift:
1931 /* We always use unsigned shifts, to avoid relying on
1932 characteristics of the compiler used to compile gas. */
1933 resultP->X_add_number =
1934 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1935 break;
1936 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1937 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1938 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1939 case O_bit_and: resultP->X_add_number &= v; break;
1940 /* Constant + constant (O_add) is handled by the
1941 previous if statement for constant + X, so is omitted
1942 here. */
1943 case O_subtract:
1944 subtract_from_result (resultP, v, 0);
1945 break;
1946 case O_eq:
1947 resultP->X_add_number =
1948 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1949 break;
1950 case O_ne:
1951 resultP->X_add_number =
1952 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1953 break;
1954 case O_lt:
1955 resultP->X_add_number =
1956 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1957 break;
1958 case O_le:
1959 resultP->X_add_number =
1960 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1961 break;
1962 case O_ge:
1963 resultP->X_add_number =
1964 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1965 break;
1966 case O_gt:
1967 resultP->X_add_number =
1968 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1969 break;
1970 case O_logical_and:
1971 resultP->X_add_number = resultP->X_add_number && v;
1972 break;
1973 case O_logical_or:
1974 resultP->X_add_number = resultP->X_add_number || v;
1975 break;
1978 else if (resultP->X_op == O_symbol
1979 && right.X_op == O_symbol
1980 && (op_left == O_add
1981 || op_left == O_subtract
1982 || (resultP->X_add_number == 0
1983 && right.X_add_number == 0)))
1985 /* Symbol OP symbol. */
1986 resultP->X_op = op_left;
1987 resultP->X_op_symbol = right.X_add_symbol;
1988 if (op_left == O_add)
1989 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1990 else if (op_left == O_subtract)
1992 subtract_from_result (resultP, right.X_add_number,
1993 right.X_extrabit);
1994 if (retval == rightseg
1995 && SEG_NORMAL (retval)
1996 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1997 && !S_FORCE_RELOC (right.X_add_symbol, 0))
1999 retval = absolute_section;
2000 rightseg = absolute_section;
2004 else
2006 general:
2007 /* The general case. */
2008 resultP->X_add_symbol = make_expr_symbol (resultP);
2009 resultP->X_op_symbol = make_expr_symbol (&right);
2010 resultP->X_op = op_left;
2011 resultP->X_add_number = 0;
2012 resultP->X_unsigned = 1;
2013 resultP->X_extrabit = 0;
2016 if (retval != rightseg)
2018 if (retval == undefined_section)
2020 else if (rightseg == undefined_section)
2021 retval = rightseg;
2022 else if (retval == expr_section)
2024 else if (rightseg == expr_section)
2025 retval = rightseg;
2026 else if (retval == reg_section)
2028 else if (rightseg == reg_section)
2029 retval = rightseg;
2030 else if (rightseg == absolute_section)
2032 else if (retval == absolute_section)
2033 retval = rightseg;
2034 #ifdef DIFF_EXPR_OK
2035 else if (op_left == O_subtract)
2037 #endif
2038 else
2039 as_bad (_("operation combines symbols in different segments"));
2042 op_left = op_right;
2043 } /* While next operator is >= this rank. */
2045 /* The PA port needs this information. */
2046 if (resultP->X_add_symbol)
2047 symbol_mark_used (resultP->X_add_symbol);
2049 if (rank == 0 && mode == expr_evaluate)
2050 resolve_expression (resultP);
2052 return resultP->X_op == O_constant ? absolute_section : retval;
2055 /* Resolve an expression without changing any symbols/sub-expressions
2056 used. */
2059 resolve_expression (expressionS *expressionP)
2061 /* Help out with CSE. */
2062 valueT final_val = expressionP->X_add_number;
2063 symbolS *add_symbol = expressionP->X_add_symbol;
2064 symbolS *orig_add_symbol = add_symbol;
2065 symbolS *op_symbol = expressionP->X_op_symbol;
2066 operatorT op = expressionP->X_op;
2067 valueT left, right;
2068 segT seg_left, seg_right;
2069 fragS *frag_left, *frag_right;
2070 offsetT frag_off;
2072 switch (op)
2074 default:
2075 return 0;
2077 case O_constant:
2078 case O_register:
2079 left = 0;
2080 break;
2082 case O_symbol:
2083 case O_symbol_rva:
2084 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2085 return 0;
2087 break;
2089 case O_uminus:
2090 case O_bit_not:
2091 case O_logical_not:
2092 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2093 return 0;
2095 if (seg_left != absolute_section)
2096 return 0;
2098 if (op == O_logical_not)
2099 left = !left;
2100 else if (op == O_uminus)
2101 left = -left;
2102 else
2103 left = ~left;
2104 op = O_constant;
2105 break;
2107 case O_multiply:
2108 case O_divide:
2109 case O_modulus:
2110 case O_left_shift:
2111 case O_right_shift:
2112 case O_bit_inclusive_or:
2113 case O_bit_or_not:
2114 case O_bit_exclusive_or:
2115 case O_bit_and:
2116 case O_add:
2117 case O_subtract:
2118 case O_eq:
2119 case O_ne:
2120 case O_lt:
2121 case O_le:
2122 case O_ge:
2123 case O_gt:
2124 case O_logical_and:
2125 case O_logical_or:
2126 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2127 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2128 return 0;
2130 /* Simplify addition or subtraction of a constant by folding the
2131 constant into X_add_number. */
2132 if (op == O_add)
2134 if (seg_right == absolute_section)
2136 final_val += right;
2137 op = O_symbol;
2138 break;
2140 else if (seg_left == absolute_section)
2142 final_val += left;
2143 left = right;
2144 seg_left = seg_right;
2145 add_symbol = op_symbol;
2146 orig_add_symbol = expressionP->X_op_symbol;
2147 op = O_symbol;
2148 break;
2151 else if (op == O_subtract)
2153 if (seg_right == absolute_section)
2155 final_val -= right;
2156 op = O_symbol;
2157 break;
2161 /* Equality and non-equality tests are permitted on anything.
2162 Subtraction, and other comparison operators are permitted if
2163 both operands are in the same section.
2164 Shifts by constant zero are permitted on anything.
2165 Multiplies, bit-ors, and bit-ands with constant zero are
2166 permitted on anything.
2167 Multiplies and divides by constant one are permitted on
2168 anything.
2169 Binary operations with both operands being the same register
2170 or undefined symbol are permitted if the result doesn't depend
2171 on the input value.
2172 Otherwise, both operands must be absolute. We already handled
2173 the case of addition or subtraction of a constant above. */
2174 frag_off = 0;
2175 if (!(seg_left == absolute_section
2176 && seg_right == absolute_section)
2177 && !(op == O_eq || op == O_ne)
2178 && !((op == O_subtract
2179 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2180 && seg_left == seg_right
2181 && (finalize_syms
2182 || frag_offset_fixed_p (frag_left, frag_right, &frag_off)
2183 || (op == O_gt
2184 && frag_gtoffset_p (left, frag_left,
2185 right, frag_right, &frag_off)))
2186 && (seg_left != reg_section || left == right)
2187 && (seg_left != undefined_section || add_symbol == op_symbol)))
2189 if ((seg_left == absolute_section && left == 0)
2190 || (seg_right == absolute_section && right == 0))
2192 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2194 if (!(seg_right == absolute_section && right == 0))
2196 seg_left = seg_right;
2197 left = right;
2198 add_symbol = op_symbol;
2199 orig_add_symbol = expressionP->X_op_symbol;
2201 op = O_symbol;
2202 break;
2204 else if (op == O_left_shift || op == O_right_shift)
2206 if (!(seg_left == absolute_section && left == 0))
2208 op = O_symbol;
2209 break;
2212 else if (op != O_multiply
2213 && op != O_bit_or_not && op != O_bit_and)
2214 return 0;
2216 else if (op == O_multiply
2217 && seg_left == absolute_section && left == 1)
2219 seg_left = seg_right;
2220 left = right;
2221 add_symbol = op_symbol;
2222 orig_add_symbol = expressionP->X_op_symbol;
2223 op = O_symbol;
2224 break;
2226 else if ((op == O_multiply || op == O_divide)
2227 && seg_right == absolute_section && right == 1)
2229 op = O_symbol;
2230 break;
2232 else if (!(left == right
2233 && ((seg_left == reg_section && seg_right == reg_section)
2234 || (seg_left == undefined_section
2235 && seg_right == undefined_section
2236 && add_symbol == op_symbol))))
2237 return 0;
2238 else if (op == O_bit_and || op == O_bit_inclusive_or)
2240 op = O_symbol;
2241 break;
2243 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2244 return 0;
2247 right += frag_off / OCTETS_PER_BYTE;
2248 switch (op)
2250 case O_add: left += right; break;
2251 case O_subtract: left -= right; break;
2252 case O_multiply: left *= right; break;
2253 case O_divide:
2254 if (right == 0)
2255 return 0;
2256 left = (offsetT) left / (offsetT) right;
2257 break;
2258 case O_modulus:
2259 if (right == 0)
2260 return 0;
2261 left = (offsetT) left % (offsetT) right;
2262 break;
2263 case O_left_shift: left <<= right; break;
2264 case O_right_shift: left >>= right; break;
2265 case O_bit_inclusive_or: left |= right; break;
2266 case O_bit_or_not: left |= ~right; break;
2267 case O_bit_exclusive_or: left ^= right; break;
2268 case O_bit_and: left &= right; break;
2269 case O_eq:
2270 case O_ne:
2271 left = (left == right
2272 && seg_left == seg_right
2273 && (finalize_syms || frag_left == frag_right)
2274 && (seg_left != undefined_section
2275 || add_symbol == op_symbol)
2276 ? ~ (valueT) 0 : 0);
2277 if (op == O_ne)
2278 left = ~left;
2279 break;
2280 case O_lt:
2281 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2282 break;
2283 case O_le:
2284 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2285 break;
2286 case O_ge:
2287 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2288 break;
2289 case O_gt:
2290 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2291 break;
2292 case O_logical_and: left = left && right; break;
2293 case O_logical_or: left = left || right; break;
2294 default: abort ();
2297 op = O_constant;
2298 break;
2301 if (op == O_symbol)
2303 if (seg_left == absolute_section)
2304 op = O_constant;
2305 else if (seg_left == reg_section && final_val == 0)
2306 op = O_register;
2307 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2308 final_val += left;
2309 expressionP->X_add_symbol = add_symbol;
2311 expressionP->X_op = op;
2313 if (op == O_constant || op == O_register)
2314 final_val += left;
2315 expressionP->X_add_number = final_val;
2317 return 1;
2320 /* This lives here because it belongs equally in expr.c & read.c.
2321 expr.c is just a branch office read.c anyway, and putting it
2322 here lessens the crowd at read.c.
2324 Assume input_line_pointer is at start of symbol name, or the
2325 start of a double quote enclosed symbol name.
2326 Advance input_line_pointer past symbol name.
2327 Turn that character into a '\0', returning its former value,
2328 which may be the closing double quote.
2329 This allows a string compare (RMS wants symbol names to be strings)
2330 of the symbol name.
2331 There will always be a char following symbol name, because all good
2332 lines end in end-of-line. */
2334 char
2335 get_symbol_name (char ** ilp_return)
2337 char c;
2339 * ilp_return = input_line_pointer;
2340 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2341 constructed string. */
2342 if (is_name_beginner (c = *input_line_pointer++)
2343 || (input_from_string && c == FAKE_LABEL_CHAR))
2345 while (is_part_of_name (c = *input_line_pointer++)
2346 || (input_from_string && c == FAKE_LABEL_CHAR))
2348 if (is_name_ender (c))
2349 c = *input_line_pointer++;
2351 else if (c == '"')
2353 bfd_boolean backslash_seen;
2355 * ilp_return = input_line_pointer;
2358 backslash_seen = c == '\\';
2359 c = * input_line_pointer ++;
2361 while (c != 0 && (c != '"' || backslash_seen));
2363 if (c == 0)
2364 as_warn (_("missing closing '\"'"));
2366 *--input_line_pointer = 0;
2367 return c;
2370 /* Replace the NUL character pointed to by input_line_pointer
2371 with C. If C is \" then advance past it. Return the character
2372 now pointed to by input_line_pointer. */
2374 char
2375 restore_line_pointer (char c)
2377 * input_line_pointer = c;
2378 if (c == '"')
2379 c = * ++ input_line_pointer;
2380 return c;
2383 unsigned int
2384 get_single_number (void)
2386 expressionS exp;
2387 operand (&exp, expr_normal);
2388 return exp.X_add_number;