Extend objdump's --show-all-symbols option so that it also shows the extra symbols...
[binutils-gdb.git] / gas / expr.c
blob03f5f8b4085ea25cbae0e37ded233a06b416253d
1 /* expr.c -operands, expressions-
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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 #include <limits.h>
32 #ifndef CHAR_BIT
33 #define CHAR_BIT 8
34 #endif
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;
45 symbolS *sym;
46 const char *file;
47 unsigned int line;
50 static struct expr_symbol_line *expr_symbol_lines;
52 static const expressionS zero = { .X_op = O_constant };
54 /* Build a dummy symbol to hold a complex expression. This is how we
55 build expressions up out of other expressions. The symbol is put
56 into the fake section expr_section. */
58 symbolS *
59 make_expr_symbol (const expressionS *expressionP)
61 symbolS *symbolP;
62 struct expr_symbol_line *n;
64 if (expressionP->X_op == O_symbol
65 && expressionP->X_add_number == 0)
66 return expressionP->X_add_symbol;
68 if (expressionP->X_op == O_big)
70 /* This won't work, because the actual value is stored in
71 generic_floating_point_number or generic_bignum, and we are
72 going to lose it if we haven't already. */
73 if (expressionP->X_add_number > 0)
74 as_bad (_("bignum invalid"));
75 else
76 as_bad (_("floating point number invalid"));
77 expressionP = &zero;
80 /* Putting constant symbols in absolute_section rather than
81 expr_section is convenient for the old a.out code, for which
82 S_GET_SEGMENT does not always retrieve the value put in by
83 S_SET_SEGMENT. */
84 symbolP = symbol_create (FAKE_LABEL_NAME,
85 (expressionP->X_op == O_constant
86 ? absolute_section
87 : expressionP->X_op == O_register
88 ? reg_section
89 : expr_section),
90 &zero_address_frag, 0);
91 symbol_set_value_expression (symbolP, expressionP);
93 if (expressionP->X_op == O_constant)
94 resolve_symbol_value (symbolP);
96 n = notes_alloc (sizeof (*n));
97 n->sym = symbolP;
98 n->file = as_where (&n->line);
99 n->next = expr_symbol_lines;
100 expr_symbol_lines = n;
102 return symbolP;
105 /* Return the file and line number for an expr symbol. Return
106 non-zero if something was found, 0 if no information is known for
107 the symbol. */
110 expr_symbol_where (symbolS *sym, const char **pfile, unsigned int *pline)
112 struct expr_symbol_line *l;
114 for (l = expr_symbol_lines; l != NULL; l = l->next)
116 if (l->sym == sym)
118 *pfile = l->file;
119 *pline = l->line;
120 return 1;
124 return 0;
127 /* Look up a previously used .startof. / .sizeof. symbol, or make a fresh
128 one. */
129 static symbolS **seen[2];
130 static unsigned int nr_seen[2];
132 static symbolS *
133 symbol_lookup_or_make (const char *name, bool start)
135 char *buf = concat (start ? ".startof." : ".sizeof.", name, NULL);
136 symbolS *symbolP;
137 unsigned int i;
139 for (i = 0; i < nr_seen[start]; ++i)
141 symbolP = seen[start][i];
143 if (! symbolP)
144 break;
146 name = S_GET_NAME (symbolP);
147 if ((symbols_case_sensitive
148 ? strcmp (buf, name)
149 : strcasecmp (buf, name)) == 0)
151 free (buf);
152 return symbolP;
156 symbolP = symbol_make (buf);
157 free (buf);
159 if (i >= nr_seen[start])
161 unsigned int nr = (i + 1) * 2;
163 seen[start] = XRESIZEVEC (symbolS *, seen[start], nr);
164 nr_seen[start] = nr;
165 memset (&seen[start][i + 1], 0, (nr - i - 1) * sizeof(seen[0][0]));
168 seen[start][i] = symbolP;
170 return symbolP;
173 /* Utilities for building expressions.
174 Since complex expressions are recorded as symbols for use in other
175 expressions these return a symbolS * and not an expressionS *.
176 These explicitly do not take an "add_number" argument. */
177 /* ??? For completeness' sake one might want expr_build_symbol.
178 It would just return its argument. */
180 /* Build an expression for an unsigned constant.
181 The corresponding one for signed constants is missing because
182 there's currently no need for it. One could add an unsigned_p flag
183 but that seems more clumsy. */
185 symbolS *
186 expr_build_uconstant (offsetT value)
188 expressionS e;
190 e.X_op = O_constant;
191 e.X_add_number = value;
192 e.X_unsigned = 1;
193 e.X_extrabit = 0;
194 return make_expr_symbol (&e);
197 /* Build an expression for the current location ('.'). */
199 symbolS *
200 expr_build_dot (void)
202 expressionS e;
204 current_location (&e);
205 return symbol_clone_if_forward_ref (make_expr_symbol (&e));
208 /* Build any floating-point literal here.
209 Also build any bignum literal here. */
211 /* Seems atof_machine can backscan through generic_bignum and hit whatever
212 happens to be loaded before it in memory. And its way too complicated
213 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
214 and never write into the early words, thus they'll always be zero.
215 I hate Dean's floating-point code. Bleh. */
216 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
218 FLONUM_TYPE generic_floating_point_number = {
219 &generic_bignum[6], /* low. (JF: Was 0) */
220 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
221 0, /* leader. */
222 0, /* exponent. */
223 0 /* sign. */
227 static void
228 floating_constant (expressionS *expressionP)
230 /* input_line_pointer -> floating-point constant. */
231 int error_code;
233 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
234 &generic_floating_point_number);
236 if (error_code)
238 if (error_code == ERROR_EXPONENT_OVERFLOW)
240 as_bad (_("bad floating-point constant: exponent overflow"));
242 else
244 as_bad (_("bad floating-point constant: unknown error code=%d"),
245 error_code);
248 expressionP->X_op = O_big;
249 /* input_line_pointer -> just after constant, which may point to
250 whitespace. */
251 expressionP->X_add_number = -1;
254 uint32_t
255 generic_bignum_to_int32 (void)
257 return ((((uint32_t) generic_bignum[1] & LITTLENUM_MASK)
258 << LITTLENUM_NUMBER_OF_BITS)
259 | ((uint32_t) generic_bignum[0] & LITTLENUM_MASK));
262 uint64_t
263 generic_bignum_to_int64 (void)
265 return ((((((((uint64_t) generic_bignum[3] & LITTLENUM_MASK)
266 << LITTLENUM_NUMBER_OF_BITS)
267 | ((uint64_t) generic_bignum[2] & LITTLENUM_MASK))
268 << LITTLENUM_NUMBER_OF_BITS)
269 | ((uint64_t) generic_bignum[1] & LITTLENUM_MASK))
270 << LITTLENUM_NUMBER_OF_BITS)
271 | ((uint64_t) generic_bignum[0] & LITTLENUM_MASK));
274 static void
275 integer_constant (int radix, expressionS *expressionP)
277 char *start; /* Start of number. */
278 char *suffix = NULL;
279 char c;
280 valueT number; /* Offset or (absolute) value. */
281 short int digit; /* Value of next digit in current radix. */
282 int too_many_digits = 0; /* If we see >= this number of. */
283 char *name; /* Points to name of symbol. */
284 symbolS *symbolP; /* Points to symbol. */
286 bool small; /* True if fits in 32 bits (64 bits with BFD64). */
288 /* May be bignum, or may fit in 32 bits. */
289 /* Most numbers fit into 32 bits, and we want this case to be fast.
290 so we pretend it will fit into 32 bits. If, after making up a 32
291 bit number, we realise that we have scanned more digits than
292 comfortably fit into 32 bits, we re-scan the digits coding them
293 into a bignum. For decimal and octal numbers we are
294 conservative: Some numbers may be assumed bignums when in fact
295 they do fit into 32 bits. Numbers of any radix can have excess
296 leading zeros: We strive to recognise this and cast them back
297 into 32 bits. We must check that the bignum really is more than
298 32 bits, and change it back to a 32-bit number if it fits. The
299 number we are looking for is expected to be positive, but if it
300 fits into 32 bits as an unsigned number, we let it be a 32-bit
301 number. The cavalier approach is for speed in ordinary cases. */
302 /* This has been extended for 64 bits. We blindly assume that if
303 you're compiling in 64-bit mode, the target is a 64-bit machine.
304 This should be cleaned up. */
306 #ifdef BFD64
307 #define valuesize 64
308 #else /* includes non-bfd case, mostly */
309 #define valuesize 32
310 #endif
312 if (is_end_of_line[(unsigned char) *input_line_pointer])
314 expressionP->X_op = O_absent;
315 return;
318 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
320 int flt = 0;
322 /* In MRI mode, the number may have a suffix indicating the
323 radix. For that matter, it might actually be a floating
324 point constant. */
325 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
327 if (*suffix == 'e' || *suffix == 'E')
328 flt = 1;
331 if (suffix == input_line_pointer)
333 radix = 10;
334 suffix = NULL;
336 else
338 c = *--suffix;
339 c = TOUPPER (c);
340 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
341 we distinguish between 'B' and 'b'. This is the case for
342 Z80. */
343 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
344 radix = 2;
345 else if (c == 'D')
346 radix = 10;
347 else if (c == 'O' || c == 'Q')
348 radix = 8;
349 else if (c == 'H')
350 radix = 16;
351 else if (suffix[1] == '.' || c == 'E' || flt)
353 floating_constant (expressionP);
354 return;
356 else
358 radix = 10;
359 suffix = NULL;
364 switch (radix)
366 case 2:
367 too_many_digits = valuesize + 1;
368 break;
369 case 8:
370 too_many_digits = (valuesize + 2) / 3 + 1;
371 break;
372 case 16:
373 too_many_digits = (valuesize + 3) / 4 + 1;
374 break;
375 case 10:
376 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
377 break;
379 #undef valuesize
380 start = input_line_pointer;
381 c = *input_line_pointer++;
382 for (number = 0;
383 (digit = hex_value (c)) < radix;
384 c = *input_line_pointer++)
386 number = number * radix + digit;
388 /* c contains character after number. */
389 /* input_line_pointer->char after c. */
390 small = (input_line_pointer - start - 1) < too_many_digits;
392 if (radix == 16 && c == '_')
394 /* This is literal of the form 0x333_0_12345678_1.
395 This example is equivalent to 0x00000333000000001234567800000001. */
397 int num_little_digits = 0;
398 int i;
399 input_line_pointer = start; /* -> 1st digit. */
401 know (LITTLENUM_NUMBER_OF_BITS == 16);
403 for (c = '_'; c == '_'; num_little_digits += 2)
406 /* Convert one 64-bit word. */
407 int ndigit = 0;
408 number = 0;
409 for (c = *input_line_pointer++;
410 (digit = hex_value (c)) < radix;
411 c = *(input_line_pointer++))
413 number = number * radix + digit;
414 ndigit++;
417 /* Check for 8 digit per word max. */
418 if (ndigit > 8)
419 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
421 /* Add this chunk to the bignum.
422 Shift things down 2 little digits. */
423 know (LITTLENUM_NUMBER_OF_BITS == 16);
424 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
425 i >= 2;
426 i--)
427 generic_bignum[i] = generic_bignum[i - 2];
429 /* Add the new digits as the least significant new ones. */
430 generic_bignum[0] = number & 0xffffffff;
431 generic_bignum[1] = number >> 16;
434 /* Again, c is char after number, input_line_pointer->after c. */
436 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
437 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
439 gas_assert (num_little_digits >= 4);
441 if (num_little_digits != 8)
442 as_bad (_("a bignum with underscores must have exactly 4 words"));
444 /* We might have some leading zeros. These can be trimmed to give
445 us a change to fit this constant into a small number. */
446 while (generic_bignum[num_little_digits - 1] == 0
447 && num_little_digits > 1)
448 num_little_digits--;
450 if (num_little_digits <= 2)
452 /* will fit into 32 bits. */
453 number = generic_bignum_to_int32 ();
454 small = 1;
456 #ifdef BFD64
457 else if (num_little_digits <= 4)
459 /* Will fit into 64 bits. */
460 number = generic_bignum_to_int64 ();
461 small = 1;
463 #endif
464 else
466 small = 0;
468 /* Number of littlenums in the bignum. */
469 number = num_little_digits;
472 else if (!small)
474 /* We saw a lot of digits. manufacture a bignum the hard way. */
475 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
476 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
477 long carry;
479 leader = generic_bignum;
480 generic_bignum[0] = 0;
481 generic_bignum[1] = 0;
482 generic_bignum[2] = 0;
483 generic_bignum[3] = 0;
484 input_line_pointer = start; /* -> 1st digit. */
485 c = *input_line_pointer++;
486 for (; (carry = hex_value (c)) < radix; c = *input_line_pointer++)
488 for (pointer = generic_bignum; pointer <= leader; pointer++)
490 long work;
492 work = carry + radix * *pointer;
493 *pointer = work & LITTLENUM_MASK;
494 carry = work >> LITTLENUM_NUMBER_OF_BITS;
496 if (carry)
498 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
500 /* Room to grow a longer bignum. */
501 *++leader = carry;
505 /* Again, c is char after number. */
506 /* input_line_pointer -> after c. */
507 know (LITTLENUM_NUMBER_OF_BITS == 16);
508 if (leader < generic_bignum + 2)
510 /* Will fit into 32 bits. */
511 number = generic_bignum_to_int32 ();
512 small = 1;
514 #ifdef BFD64
515 else if (leader < generic_bignum + 4)
517 /* Will fit into 64 bits. */
518 number = generic_bignum_to_int64 ();
519 small = 1;
521 #endif
522 else
524 /* Number of littlenums in the bignum. */
525 number = leader - generic_bignum + 1;
529 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
530 && suffix != NULL
531 && input_line_pointer - 1 == suffix)
532 c = *input_line_pointer++;
534 #ifndef tc_allow_U_suffix
535 #define tc_allow_U_suffix 1
536 #endif
537 bool u_seen = !tc_allow_U_suffix;
538 /* PR 19910: Look for, and ignore, a U suffix to the number. */
539 if (!u_seen && (c == 'U' || c == 'u'))
541 c = *input_line_pointer++;
542 u_seen = true;
545 #ifndef tc_allow_L_suffix
546 #define tc_allow_L_suffix 1
547 #endif
548 bool l_seen = !tc_allow_L_suffix;
549 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
550 if (tc_allow_L_suffix && (c == 'L' || c == 'l'))
552 c = * input_line_pointer++;
553 l_seen = true;
554 if (c == 'L' || c == 'l')
555 c = *input_line_pointer++;
556 if (!u_seen && (c == 'U' || c == 'u'))
557 c = *input_line_pointer++;
560 if (small)
562 /* Here with number, in correct radix. c is the next char. */
563 bool maybe_label = suffix == NULL
564 && (!tc_allow_U_suffix || !u_seen)
565 && (!tc_allow_L_suffix || !l_seen)
566 && (radix == 10 ||
567 (radix == 8 && input_line_pointer == start + 1));
569 if (LOCAL_LABELS_FB && c == 'b' && maybe_label)
571 /* Backward ref to local label.
572 Because it is backward, expect it to be defined. */
573 /* Construct a local label. */
574 name = fb_label_name (number, 0);
576 /* Seen before, or symbol is defined: OK. */
577 symbolP = symbol_find (name);
578 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
580 expressionP->X_op = O_symbol;
581 expressionP->X_add_symbol = symbolP;
583 else
585 /* Either not seen or not defined. */
586 /* @@ Should print out the original string instead of
587 the parsed number. */
588 as_bad (_("backward ref to unknown label \"%d:\""),
589 (int) number);
590 expressionP->X_op = O_constant;
593 expressionP->X_add_number = 0;
594 } /* case 'b' */
595 else if (LOCAL_LABELS_FB && c == 'f' && maybe_label)
597 /* Forward reference. Expect symbol to be undefined or
598 unknown. undefined: seen it before. unknown: never seen
599 it before.
601 Construct a local label name, then an undefined symbol.
602 Don't create a xseg frag for it: caller may do that.
603 Just return it as never seen before. */
604 name = fb_label_name (number, 1);
605 symbolP = symbol_find_or_make (name);
606 /* We have no need to check symbol properties. */
607 expressionP->X_op = O_symbol;
608 expressionP->X_add_symbol = symbolP;
609 expressionP->X_add_number = 0;
610 } /* case 'f' */
611 else if (LOCAL_LABELS_DOLLAR && c == '$' && maybe_label)
613 /* If the dollar label is *currently* defined, then this is just
614 another reference to it. If it is not *currently* defined,
615 then this is a fresh instantiation of that number, so create
616 it. */
618 if (dollar_label_defined (number))
620 name = dollar_label_name (number, 0);
621 symbolP = symbol_find (name);
622 know (symbolP != NULL);
624 else
626 name = dollar_label_name (number, 1);
627 symbolP = symbol_find_or_make (name);
630 expressionP->X_op = O_symbol;
631 expressionP->X_add_symbol = symbolP;
632 expressionP->X_add_number = 0;
633 } /* case '$' */
634 else
636 expressionP->X_op = O_constant;
637 expressionP->X_add_number = number;
638 input_line_pointer--; /* Restore following character. */
639 } /* Really just a number. */
641 else
643 /* Not a small number. */
644 expressionP->X_op = O_big;
645 expressionP->X_add_number = number; /* Number of littlenums. */
646 input_line_pointer--; /* -> char following number. */
650 /* Parse an MRI multi character constant. */
652 static void
653 mri_char_constant (expressionS *expressionP)
655 int i;
657 if (*input_line_pointer == '\''
658 && input_line_pointer[1] != '\'')
660 expressionP->X_op = O_constant;
661 expressionP->X_add_number = 0;
662 return;
665 /* In order to get the correct byte ordering, we must build the
666 number in reverse. */
667 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
669 int j;
671 generic_bignum[i] = 0;
672 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
674 if (*input_line_pointer == '\'')
676 if (input_line_pointer[1] != '\'')
677 break;
678 ++input_line_pointer;
680 generic_bignum[i] <<= 8;
681 generic_bignum[i] += *input_line_pointer;
682 ++input_line_pointer;
685 if (i < SIZE_OF_LARGE_NUMBER - 1)
687 /* If there is more than one littlenum, left justify the
688 last one to make it match the earlier ones. If there is
689 only one, we can just use the value directly. */
690 for (; j < CHARS_PER_LITTLENUM; j++)
691 generic_bignum[i] <<= 8;
694 if (*input_line_pointer == '\''
695 && input_line_pointer[1] != '\'')
696 break;
699 if (i < 0)
701 as_bad (_("character constant too large"));
702 i = 0;
705 if (i > 0)
707 int c;
708 int j;
710 c = SIZE_OF_LARGE_NUMBER - i;
711 for (j = 0; j < c; j++)
712 generic_bignum[j] = generic_bignum[i + j];
713 i = c;
716 know (LITTLENUM_NUMBER_OF_BITS == 16);
717 if (i > 2)
719 expressionP->X_op = O_big;
720 expressionP->X_add_number = i;
722 else
724 expressionP->X_op = O_constant;
725 if (i < 2)
726 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
727 else
728 expressionP->X_add_number =
729 (((generic_bignum[1] & LITTLENUM_MASK)
730 << LITTLENUM_NUMBER_OF_BITS)
731 | (generic_bignum[0] & LITTLENUM_MASK));
734 /* Skip the final closing quote. */
735 ++input_line_pointer;
738 /* Return an expression representing the current location. This
739 handles the magic symbol `.'. */
741 void
742 current_location (expressionS *expressionp)
744 if (now_seg == absolute_section)
746 expressionp->X_op = O_constant;
747 expressionp->X_add_number = abs_section_offset;
749 else
751 expressionp->X_op = O_symbol;
752 expressionp->X_add_symbol = &dot_symbol;
753 expressionp->X_add_number = 0;
757 #ifndef md_register_arithmetic
758 # define md_register_arithmetic 1
759 #endif
761 /* In: Input_line_pointer points to 1st char of operand, which may
762 be a space.
764 Out: An expressionS.
765 The operand may have been empty: in this case X_op == O_absent.
766 Input_line_pointer->(next non-blank) char after operand. */
768 static segT
769 operand (expressionS *expressionP, enum expr_mode mode)
771 char c;
772 symbolS *symbolP; /* Points to symbol. */
773 char *name; /* Points to name of symbol. */
774 segT segment;
775 operatorT op = O_absent; /* For unary operators. */
777 /* All integers are regarded as unsigned unless they are negated.
778 This is because the only thing which cares whether a number is
779 unsigned is the code in emit_expr which extends constants into
780 bignums. It should only sign extend negative numbers, so that
781 something like ``.quad 0x80000000'' is not sign extended even
782 though it appears negative if valueT is 32 bits. */
783 expressionP->X_unsigned = 1;
784 expressionP->X_extrabit = 0;
786 /* Digits, assume it is a bignum. */
788 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
789 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
791 if (is_end_of_line[(unsigned char) c])
792 goto eol;
794 switch (c)
796 case '1':
797 case '2':
798 case '3':
799 case '4':
800 case '5':
801 case '6':
802 case '7':
803 case '8':
804 case '9':
805 input_line_pointer--;
807 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
808 ? 0 : 10,
809 expressionP);
810 break;
812 #ifdef LITERAL_PREFIXPERCENT_BIN
813 case '%':
814 integer_constant (2, expressionP);
815 break;
816 #endif
818 case '0':
819 /* Non-decimal radix. */
821 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
823 char *s;
825 /* Check for a hex or float constant. */
826 for (s = input_line_pointer; hex_p (*s); s++)
828 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
830 --input_line_pointer;
831 integer_constant (0, expressionP);
832 break;
835 c = *input_line_pointer;
836 switch (c)
838 case 'o':
839 case 'O':
840 case 'q':
841 case 'Q':
842 case '8':
843 case '9':
844 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
846 integer_constant (0, expressionP);
847 break;
849 /* Fall through. */
850 default:
851 default_case:
852 if (c && strchr (FLT_CHARS, c))
854 input_line_pointer++;
855 floating_constant (expressionP);
856 expressionP->X_add_number = - TOLOWER (c);
858 else
860 /* The string was only zero. */
861 expressionP->X_op = O_constant;
862 expressionP->X_add_number = 0;
865 break;
867 case 'x':
868 case 'X':
869 if (flag_m68k_mri)
870 goto default_case;
871 input_line_pointer++;
872 integer_constant (16, expressionP);
873 break;
875 case 'b':
876 if (LOCAL_LABELS_FB && !flag_m68k_mri
877 && input_line_pointer[1] != '0'
878 && input_line_pointer[1] != '1')
880 /* Parse this as a back reference to label 0. */
881 input_line_pointer--;
882 integer_constant (10, expressionP);
883 break;
885 /* Otherwise, parse this as a binary number. */
886 /* Fall through. */
887 case 'B':
888 if (input_line_pointer[1] == '0'
889 || input_line_pointer[1] == '1')
891 input_line_pointer++;
892 integer_constant (2, expressionP);
893 break;
895 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
896 input_line_pointer++;
897 goto default_case;
899 case 'l':
900 case 'L':
901 /* Accept an L suffix to the zero. */
902 if (tc_allow_L_suffix)
903 goto numeric;
904 goto default_case;
906 case 'u':
907 case 'U':
908 /* Accept a U suffix to the zero. */
909 if (!tc_allow_U_suffix)
910 goto default_case;
911 /* Fall through. */
912 case '0':
913 case '1':
914 case '2':
915 case '3':
916 case '4':
917 case '5':
918 case '6':
919 case '7':
920 numeric:
921 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
922 ? 0 : 8,
923 expressionP);
924 break;
926 case 'f':
927 if (LOCAL_LABELS_FB)
929 int is_label = 1;
931 /* If it says "0f" and it could possibly be a floating point
932 number, make it one. Otherwise, make it a local label,
933 and try to deal with parsing the rest later. */
934 if (!is_end_of_line[(unsigned char) input_line_pointer[1]]
935 && strchr (FLT_CHARS, 'f') != NULL)
937 char *cp = input_line_pointer + 1;
939 atof_generic (&cp, ".", EXP_CHARS,
940 &generic_floating_point_number);
942 /* Was nothing parsed, or does it look like an
943 expression? */
944 is_label = (cp == input_line_pointer + 1
945 || (cp == input_line_pointer + 2
946 && (cp[-1] == '-' || cp[-1] == '+'))
947 || *cp == 'f'
948 || *cp == 'b');
950 if (is_label)
952 input_line_pointer--;
953 integer_constant (10, expressionP);
954 break;
957 /* Fall through. */
959 case 'd':
960 case 'D':
961 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
963 integer_constant (0, expressionP);
964 break;
966 /* Fall through. */
967 case 'F':
968 case 'r':
969 case 'e':
970 case 'E':
971 case 'g':
972 case 'G':
973 input_line_pointer++;
974 floating_constant (expressionP);
975 expressionP->X_add_number = - TOLOWER (c);
976 break;
978 case '$':
979 if (LOCAL_LABELS_DOLLAR)
981 integer_constant (10, expressionP);
982 break;
984 else
985 goto default_case;
988 break;
990 #ifndef NEED_INDEX_OPERATOR
991 case '[':
992 # ifdef md_need_index_operator
993 if (md_need_index_operator())
994 goto de_fault;
995 # endif
996 #endif
997 /* Fall through. */
998 case '(':
999 /* Didn't begin with digit & not a name. */
1000 segment = expr (0, expressionP, mode);
1001 /* expression () will pass trailing whitespace. */
1002 if ((c == '(' && *input_line_pointer != ')')
1003 || (c == '[' && *input_line_pointer != ']'))
1005 if (* input_line_pointer)
1006 as_bad (_("found '%c', expected: '%c'"),
1007 * input_line_pointer, c == '(' ? ')' : ']');
1008 else
1009 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
1011 else
1012 input_line_pointer++;
1013 SKIP_ALL_WHITESPACE ();
1014 /* Here with input_line_pointer -> char after "(...)". */
1015 return segment;
1017 #ifdef TC_M68K
1018 case 'E':
1019 if (! flag_m68k_mri || *input_line_pointer != '\'')
1020 goto de_fault;
1021 as_bad (_("EBCDIC constants are not supported"));
1022 /* Fall through. */
1023 case 'A':
1024 if (! flag_m68k_mri || *input_line_pointer != '\'')
1025 goto de_fault;
1026 ++input_line_pointer;
1027 #endif
1028 /* Fall through. */
1029 case '\'':
1030 if (! flag_m68k_mri)
1032 /* Warning: to conform to other people's assemblers NO
1033 ESCAPEMENT is permitted for a single quote. The next
1034 character, parity errors and all, is taken as the value
1035 of the operand. VERY KINKY. */
1036 expressionP->X_op = O_constant;
1037 expressionP->X_add_number = *input_line_pointer++;
1038 break;
1041 mri_char_constant (expressionP);
1042 break;
1044 #ifdef TC_M68K
1045 case '"':
1046 /* Double quote is the bitwise not operator in MRI mode. */
1047 if (! flag_m68k_mri)
1048 goto de_fault;
1049 #endif
1050 /* Fall through. */
1051 case '~':
1052 /* '~' is permitted to start a label on the Delta. */
1053 if (is_name_beginner (c))
1054 goto isname;
1055 op = O_bit_not;
1056 goto unary;
1058 case '!':
1059 op = O_logical_not;
1060 goto unary;
1062 case '-':
1063 op = O_uminus;
1064 /* Fall through. */
1065 case '+':
1067 unary:
1068 operand (expressionP, mode);
1070 #ifdef md_optimize_expr
1071 if (md_optimize_expr (NULL, op, expressionP))
1073 /* Skip. */
1076 else
1077 #endif
1078 if (expressionP->X_op == O_constant)
1080 /* input_line_pointer -> char after operand. */
1081 if (op == O_uminus)
1083 expressionP->X_add_number
1084 = - (addressT) expressionP->X_add_number;
1085 /* Notice: '-' may overflow: no warning is given.
1086 This is compatible with other people's
1087 assemblers. Sigh. */
1088 expressionP->X_unsigned = 0;
1089 if (expressionP->X_add_number)
1090 expressionP->X_extrabit ^= 1;
1092 else if (op == O_bit_not)
1094 expressionP->X_add_number = ~ expressionP->X_add_number;
1095 expressionP->X_extrabit ^= 1;
1096 expressionP->X_unsigned = 0;
1098 else if (op == O_logical_not)
1100 expressionP->X_add_number = ! expressionP->X_add_number;
1101 expressionP->X_unsigned = 1;
1102 expressionP->X_extrabit = 0;
1105 else if (expressionP->X_op == O_big
1106 && expressionP->X_add_number <= 0
1107 && op == O_uminus
1108 && (generic_floating_point_number.sign == '+'
1109 || generic_floating_point_number.sign == 'P'))
1111 /* Negative flonum (eg, -1.000e0). */
1112 if (generic_floating_point_number.sign == '+')
1113 generic_floating_point_number.sign = '-';
1114 else
1115 generic_floating_point_number.sign = 'N';
1117 else if (expressionP->X_op == O_big
1118 && expressionP->X_add_number > 0)
1120 int i;
1122 if (op == O_uminus || op == O_bit_not)
1124 for (i = 0; i < expressionP->X_add_number; ++i)
1125 generic_bignum[i] = ~generic_bignum[i];
1127 /* Extend the bignum to at least the size of .octa. */
1128 if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1130 expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1131 for (; i < expressionP->X_add_number; ++i)
1132 generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1135 if (op == O_uminus)
1136 for (i = 0; i < expressionP->X_add_number; ++i)
1138 generic_bignum[i] += 1;
1139 if (generic_bignum[i])
1140 break;
1143 else if (op == O_logical_not)
1145 for (i = 0; i < expressionP->X_add_number; ++i)
1146 if (generic_bignum[i] != 0)
1147 break;
1148 expressionP->X_add_number = i >= expressionP->X_add_number;
1149 expressionP->X_op = O_constant;
1150 expressionP->X_unsigned = 1;
1151 expressionP->X_extrabit = 0;
1154 else if (expressionP->X_op != O_illegal
1155 && expressionP->X_op != O_absent)
1157 if (op != O_absent)
1159 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1160 expressionP->X_op = op;
1161 expressionP->X_add_number = 0;
1163 else if (!md_register_arithmetic && expressionP->X_op == O_register)
1165 /* Convert to binary '+'. */
1166 expressionP->X_op_symbol = make_expr_symbol (expressionP);
1167 expressionP->X_add_symbol = make_expr_symbol (&zero);
1168 expressionP->X_add_number = 0;
1169 expressionP->X_op = O_add;
1172 else
1173 as_warn (_("Unary operator %c ignored because bad operand follows"),
1176 break;
1178 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1179 case '$':
1180 if (literal_prefix_dollar_hex)
1182 /* $L is the start of a local label, not a hex constant. */
1183 if (* input_line_pointer == 'L')
1184 goto isname;
1185 integer_constant (16, expressionP);
1187 else
1189 goto isname;
1191 break;
1192 #else
1193 case '$':
1194 /* '$' is the program counter when in MRI mode, or when
1195 DOLLAR_DOT is defined. */
1196 #ifndef DOLLAR_DOT
1197 if (! flag_m68k_mri)
1198 goto de_fault;
1199 #endif
1200 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1202 /* In MRI mode and on Z80, '$' is also used as the prefix
1203 for a hexadecimal constant. */
1204 integer_constant (16, expressionP);
1205 break;
1208 if (is_part_of_name (*input_line_pointer))
1209 goto isname;
1211 current_location (expressionP);
1212 break;
1213 #endif
1215 case '.':
1216 if (!is_part_of_name (*input_line_pointer))
1218 current_location (expressionP);
1219 break;
1221 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1222 && ! is_part_of_name (input_line_pointer[8]))
1223 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1224 && ! is_part_of_name (input_line_pointer[7])))
1226 int start;
1228 start = (input_line_pointer[1] == 't'
1229 || input_line_pointer[1] == 'T');
1230 input_line_pointer += start ? 8 : 7;
1231 SKIP_WHITESPACE ();
1233 /* Cover for the as_bad () invocations below. */
1234 expressionP->X_op = O_absent;
1236 if (*input_line_pointer != '(')
1237 as_bad (_("syntax error in .startof. or .sizeof."));
1238 else
1240 ++input_line_pointer;
1241 SKIP_WHITESPACE ();
1242 c = get_symbol_name (& name);
1243 if (! *name)
1245 as_bad (_("expected symbol name"));
1246 (void) restore_line_pointer (c);
1247 if (c == ')')
1248 ++input_line_pointer;
1249 break;
1252 expressionP->X_op = O_symbol;
1253 expressionP->X_add_symbol = symbol_lookup_or_make (name, start);
1254 expressionP->X_add_number = 0;
1256 *input_line_pointer = c;
1257 SKIP_WHITESPACE_AFTER_NAME ();
1258 if (*input_line_pointer != ')')
1259 as_bad (_("syntax error in .startof. or .sizeof."));
1260 else
1261 ++input_line_pointer;
1263 break;
1265 else
1267 goto isname;
1270 case ',':
1271 eol:
1272 /* Can't imagine any other kind of operand. */
1273 expressionP->X_op = O_absent;
1274 input_line_pointer--;
1275 break;
1277 #ifdef TC_M68K
1278 case '%':
1279 if (! flag_m68k_mri)
1280 goto de_fault;
1281 integer_constant (2, expressionP);
1282 break;
1284 case '@':
1285 if (! flag_m68k_mri)
1286 goto de_fault;
1287 integer_constant (8, expressionP);
1288 break;
1290 case ':':
1291 if (! flag_m68k_mri)
1292 goto de_fault;
1294 /* In MRI mode, this is a floating point constant represented
1295 using hexadecimal digits. */
1297 ++input_line_pointer;
1298 integer_constant (16, expressionP);
1299 break;
1301 case '*':
1302 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1303 goto de_fault;
1305 current_location (expressionP);
1306 break;
1307 #endif
1309 default:
1310 #if defined(md_need_index_operator) || defined(TC_M68K)
1311 de_fault:
1312 #endif
1313 if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
1315 /* Identifier begins here.
1316 This is kludged for speed, so code is repeated. */
1317 isname:
1318 -- input_line_pointer;
1319 c = get_symbol_name (&name);
1321 #ifdef md_operator
1323 op = md_operator (name, 1, &c);
1324 switch (op)
1326 case O_uminus:
1327 restore_line_pointer (c);
1328 c = '-';
1329 goto unary;
1330 case O_bit_not:
1331 restore_line_pointer (c);
1332 c = '~';
1333 goto unary;
1334 case O_logical_not:
1335 restore_line_pointer (c);
1336 c = '!';
1337 goto unary;
1338 case O_illegal:
1339 as_bad (_("invalid use of operator \"%s\""), name);
1340 break;
1341 default:
1342 break;
1345 if (op != O_absent && op != O_illegal)
1347 restore_line_pointer (c);
1348 expr (9, expressionP, mode);
1349 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1350 expressionP->X_op_symbol = NULL;
1351 expressionP->X_add_number = 0;
1352 expressionP->X_op = op;
1353 break;
1356 #endif
1358 #ifdef md_parse_name
1359 /* This is a hook for the backend to parse certain names
1360 specially in certain contexts. If a name always has a
1361 specific value, it can often be handled by simply
1362 entering it in the symbol table. */
1363 if (md_parse_name (name, expressionP, mode, &c))
1365 restore_line_pointer (c);
1366 break;
1368 #endif
1370 symbolP = symbol_find_or_make (name);
1372 /* If we have an absolute symbol or a reg, then we know its
1373 value now. */
1374 segment = S_GET_SEGMENT (symbolP);
1375 if (mode != expr_defer
1376 && segment == absolute_section
1377 && !S_FORCE_RELOC (symbolP, 0))
1379 expressionP->X_op = O_constant;
1380 expressionP->X_add_number = S_GET_VALUE (symbolP);
1382 else if (mode != expr_defer && segment == reg_section)
1384 expressionP->X_op = O_register;
1385 expressionP->X_add_number = S_GET_VALUE (symbolP);
1387 else
1389 expressionP->X_op = O_symbol;
1390 expressionP->X_add_symbol = symbolP;
1391 expressionP->X_add_number = 0;
1394 restore_line_pointer (c);
1396 else
1398 /* Let the target try to parse it. Success is indicated by changing
1399 the X_op field to something other than O_absent and pointing
1400 input_line_pointer past the expression. If it can't parse the
1401 expression, X_op and input_line_pointer should be unchanged. */
1402 expressionP->X_op = O_absent;
1403 --input_line_pointer;
1404 md_operand (expressionP);
1405 if (expressionP->X_op == O_absent)
1407 ++input_line_pointer;
1408 as_bad (_("bad expression"));
1409 expressionP->X_op = O_constant;
1410 expressionP->X_add_number = 0;
1413 break;
1416 /* It is more 'efficient' to clean up the expressionS when they are
1417 created. Doing it here saves lines of code. */
1418 clean_up_expression (expressionP);
1419 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1420 know (*input_line_pointer != ' ');
1422 /* The PA port needs this information. */
1423 if (expressionP->X_add_symbol)
1424 symbol_mark_used (expressionP->X_add_symbol);
1426 if (mode != expr_defer)
1428 expressionP->X_add_symbol
1429 = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1430 expressionP->X_op_symbol
1431 = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1434 switch (expressionP->X_op)
1436 default:
1437 return absolute_section;
1438 case O_symbol:
1439 return S_GET_SEGMENT (expressionP->X_add_symbol);
1440 case O_register:
1441 return reg_section;
1445 /* Internal. Simplify a struct expression for use by expr (). */
1447 /* In: address of an expressionS.
1448 The X_op field of the expressionS may only take certain values.
1449 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1451 Out: expressionS may have been modified:
1452 Unused fields zeroed to help expr (). */
1454 static void
1455 clean_up_expression (expressionS *expressionP)
1457 switch (expressionP->X_op)
1459 case O_illegal:
1460 case O_absent:
1461 expressionP->X_add_number = 0;
1462 /* Fall through. */
1463 case O_big:
1464 case O_constant:
1465 case O_register:
1466 expressionP->X_add_symbol = NULL;
1467 /* Fall through. */
1468 case O_symbol:
1469 case O_uminus:
1470 case O_bit_not:
1471 expressionP->X_op_symbol = NULL;
1472 break;
1473 default:
1474 break;
1478 /* Expression parser. */
1480 /* We allow an empty expression, and just assume (absolute,0) silently.
1481 Unary operators and parenthetical expressions are treated as operands.
1482 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1484 We used to do an aho/ullman shift-reduce parser, but the logic got so
1485 warped that I flushed it and wrote a recursive-descent parser instead.
1486 Now things are stable, would anybody like to write a fast parser?
1487 Most expressions are either register (which does not even reach here)
1488 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1489 So I guess it doesn't really matter how inefficient more complex expressions
1490 are parsed.
1492 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1493 Also, we have consumed any leading or trailing spaces (operand does that)
1494 and done all intervening operators.
1496 This returns the segment of the result, which will be
1497 absolute_section or the segment of a symbol. */
1499 #undef __
1500 #define __ O_illegal
1501 #ifndef O_SINGLE_EQ
1502 #define O_SINGLE_EQ O_illegal
1503 #endif
1505 /* Maps ASCII -> operators. */
1506 static const operatorT op_encoding[256] = {
1507 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1508 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1510 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1511 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1512 __, __, __, __, __, __, __, __,
1513 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1514 __, __, __, __, __, __, __, __,
1515 __, __, __, __, __, __, __, __,
1516 __, __, __, __, __, __, __, __,
1517 __, __, __,
1518 #ifdef NEED_INDEX_OPERATOR
1519 O_index,
1520 #else
1522 #endif
1523 __, __, O_bit_exclusive_or, __,
1524 __, __, __, __, __, __, __, __,
1525 __, __, __, __, __, __, __, __,
1526 __, __, __, __, __, __, __, __,
1527 __, __, __, __, O_bit_inclusive_or, __, __, __,
1529 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1530 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1531 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1532 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1533 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1534 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1535 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1536 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1539 /* Rank Examples
1540 0 operand, (expression)
1541 1 ||
1542 2 &&
1543 3 == <> < <= >= >
1544 4 + -
1545 5 used for * / % in MRI mode
1546 6 & ^ ! |
1547 7 * / % << >>
1548 8 unary - unary ~
1550 static operator_rankT op_rank[O_max] = {
1551 0, /* O_illegal */
1552 0, /* O_absent */
1553 0, /* O_constant */
1554 0, /* O_symbol */
1555 0, /* O_symbol_rva */
1556 0, /* O_secidx */
1557 0, /* O_register */
1558 0, /* O_big */
1559 9, /* O_uminus */
1560 9, /* O_bit_not */
1561 9, /* O_logical_not */
1562 8, /* O_multiply */
1563 8, /* O_divide */
1564 8, /* O_modulus */
1565 8, /* O_left_shift */
1566 8, /* O_right_shift */
1567 7, /* O_bit_inclusive_or */
1568 7, /* O_bit_or_not */
1569 7, /* O_bit_exclusive_or */
1570 7, /* O_bit_and */
1571 5, /* O_add */
1572 5, /* O_subtract */
1573 4, /* O_eq */
1574 4, /* O_ne */
1575 4, /* O_lt */
1576 4, /* O_le */
1577 4, /* O_ge */
1578 4, /* O_gt */
1579 3, /* O_logical_and */
1580 2, /* O_logical_or */
1581 1, /* O_index */
1584 /* Unfortunately, in MRI mode for the m68k, multiplication and
1585 division have lower precedence than the bit wise operators. This
1586 function sets the operator precedences correctly for the current
1587 mode. Also, MRI uses a different bit_not operator, and this fixes
1588 that as well. */
1590 #define STANDARD_MUL_PRECEDENCE 8
1591 #define MRI_MUL_PRECEDENCE 6
1593 void
1594 expr_set_precedence (void)
1596 if (flag_m68k_mri)
1598 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1599 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1600 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1602 else
1604 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1605 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1606 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1610 void
1611 expr_set_rank (operatorT op, operator_rankT rank)
1613 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1614 op_rank[op] = rank;
1617 /* Initialize the expression parser. */
1619 void
1620 expr_begin (void)
1622 expr_set_precedence ();
1624 /* Verify that X_op field is wide enough. */
1626 expressionS e;
1627 e.X_op = O_max;
1628 gas_assert (e.X_op == O_max);
1631 memset (seen, 0, sizeof seen);
1632 memset (nr_seen, 0, sizeof nr_seen);
1633 expr_symbol_lines = NULL;
1636 void
1637 expr_end (void)
1639 for (size_t i = 0; i < ARRAY_SIZE (seen); i++)
1640 free (seen[i]);
1643 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1644 sets NUM_CHARS to the number of characters in the operator.
1645 Does not advance INPUT_LINE_POINTER. */
1647 static inline operatorT
1648 operatorf (int *num_chars)
1650 int c;
1651 operatorT ret;
1653 c = *input_line_pointer & 0xff;
1654 *num_chars = 1;
1656 if (is_end_of_line[c])
1657 return O_illegal;
1659 #ifdef md_operator
1660 if (is_name_beginner (c))
1662 char *name;
1663 char ec = get_symbol_name (& name);
1665 ret = md_operator (name, 2, &ec);
1666 switch (ret)
1668 case O_absent:
1669 *input_line_pointer = ec;
1670 input_line_pointer = name;
1671 break;
1672 case O_uminus:
1673 case O_bit_not:
1674 case O_logical_not:
1675 as_bad (_("invalid use of operator \"%s\""), name);
1676 ret = O_illegal;
1677 /* FALLTHROUGH */
1678 default:
1679 *input_line_pointer = ec;
1680 *num_chars = input_line_pointer - name;
1681 input_line_pointer = name;
1682 return ret;
1685 #endif
1687 switch (c)
1689 default:
1690 ret = op_encoding[c];
1691 #ifdef md_operator
1692 if (ret == O_illegal)
1694 char *start = input_line_pointer;
1696 ret = md_operator (NULL, 2, NULL);
1697 if (ret != O_illegal)
1698 *num_chars = input_line_pointer - start;
1699 input_line_pointer = start;
1701 #endif
1702 return ret;
1704 case '+':
1705 case '-':
1706 return op_encoding[c];
1708 case '<':
1709 switch (input_line_pointer[1])
1711 default:
1712 return op_encoding[c];
1713 case '<':
1714 ret = O_left_shift;
1715 break;
1716 case '>':
1717 ret = O_ne;
1718 break;
1719 case '=':
1720 ret = O_le;
1721 break;
1723 *num_chars = 2;
1724 return ret;
1726 case '=':
1727 if (input_line_pointer[1] != '=')
1728 return op_encoding[c];
1730 *num_chars = 2;
1731 return O_eq;
1733 case '>':
1734 switch (input_line_pointer[1])
1736 default:
1737 return op_encoding[c];
1738 case '>':
1739 ret = O_right_shift;
1740 break;
1741 case '=':
1742 ret = O_ge;
1743 break;
1745 *num_chars = 2;
1746 return ret;
1748 case '!':
1749 switch (input_line_pointer[1])
1751 case '!':
1752 /* We accept !! as equivalent to ^ for MRI compatibility. */
1753 *num_chars = 2;
1754 return O_bit_exclusive_or;
1755 case '=':
1756 /* We accept != as equivalent to <>. */
1757 *num_chars = 2;
1758 return O_ne;
1759 default:
1760 if (flag_m68k_mri)
1761 return O_bit_inclusive_or;
1762 return op_encoding[c];
1765 case '|':
1766 if (input_line_pointer[1] != '|')
1767 return op_encoding[c];
1769 *num_chars = 2;
1770 return O_logical_or;
1772 case '&':
1773 if (input_line_pointer[1] != '&')
1774 return op_encoding[c];
1776 *num_chars = 2;
1777 return O_logical_and;
1780 /* NOTREACHED */
1783 /* Implement "word-size + 1 bit" addition for
1784 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1785 is used so that the full range of unsigned word values and the full range of
1786 signed word values can be represented in an O_constant expression, which is
1787 useful e.g. for .sleb128 directives. */
1789 void
1790 add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1792 valueT ures = resultP->X_add_number;
1793 valueT uamount = amount;
1795 resultP->X_add_number += uamount;
1797 resultP->X_extrabit ^= rhs_highbit;
1799 if (ures + uamount < ures)
1800 resultP->X_extrabit ^= 1;
1803 /* Similarly, for subtraction. */
1805 void
1806 subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1808 valueT ures = resultP->X_add_number;
1809 valueT uamount = amount;
1811 resultP->X_add_number -= uamount;
1813 resultP->X_extrabit ^= rhs_highbit;
1815 if (ures < uamount)
1816 resultP->X_extrabit ^= 1;
1819 /* Parse an expression. */
1821 segT
1822 expr (int rankarg, /* Larger # is higher rank. */
1823 expressionS *resultP, /* Deliver result here. */
1824 enum expr_mode mode /* Controls behavior. */)
1826 operator_rankT rank = (operator_rankT) rankarg;
1827 segT retval;
1828 expressionS right;
1829 operatorT op_left;
1830 operatorT op_right;
1831 int op_chars;
1833 know (rankarg >= 0);
1835 /* Save the value of dot for the fixup code. */
1836 if (rank == 0)
1838 dot_value = frag_now_fix ();
1839 dot_frag = frag_now;
1842 retval = operand (resultP, mode);
1844 /* operand () gobbles spaces. */
1845 know (*input_line_pointer != ' ');
1847 op_left = operatorf (&op_chars);
1848 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1850 segT rightseg;
1851 bool is_unsigned;
1852 offsetT frag_off;
1854 input_line_pointer += op_chars; /* -> after operator. */
1856 right.X_md = 0;
1857 rightseg = expr (op_rank[(int) op_left], &right, mode);
1858 if (right.X_op == O_absent)
1860 as_warn (_("missing operand; zero assumed"));
1861 right.X_op = O_constant;
1862 right.X_add_number = 0;
1863 right.X_add_symbol = NULL;
1864 right.X_op_symbol = NULL;
1867 know (*input_line_pointer != ' ');
1869 if (op_left == O_index)
1871 if (*input_line_pointer != ']')
1872 as_bad ("missing right bracket");
1873 else
1875 ++input_line_pointer;
1876 SKIP_WHITESPACE ();
1880 op_right = operatorf (&op_chars);
1882 know (op_right == O_illegal || op_left == O_index
1883 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1884 know ((int) op_left >= (int) O_multiply);
1885 #ifndef md_operator
1886 know ((int) op_left <= (int) O_index);
1887 #else
1888 know ((int) op_left < (int) O_max);
1889 #endif
1891 /* input_line_pointer->after right-hand quantity. */
1892 /* left-hand quantity in resultP. */
1893 /* right-hand quantity in right. */
1894 /* operator in op_left. */
1896 if (resultP->X_op == O_big)
1898 if (resultP->X_add_number > 0)
1899 as_warn (_("left operand is a bignum; integer 0 assumed"));
1900 else
1901 as_warn (_("left operand is a float; integer 0 assumed"));
1902 resultP->X_op = O_constant;
1903 resultP->X_add_number = 0;
1904 resultP->X_add_symbol = NULL;
1905 resultP->X_op_symbol = NULL;
1907 if (right.X_op == O_big)
1909 if (right.X_add_number > 0)
1910 as_warn (_("right operand is a bignum; integer 0 assumed"));
1911 else
1912 as_warn (_("right operand is a float; integer 0 assumed"));
1913 right.X_op = O_constant;
1914 right.X_add_number = 0;
1915 right.X_add_symbol = NULL;
1916 right.X_op_symbol = NULL;
1919 is_unsigned = resultP->X_unsigned && right.X_unsigned;
1921 if (mode == expr_defer
1922 && ((resultP->X_add_symbol != NULL
1923 && S_IS_FORWARD_REF (resultP->X_add_symbol))
1924 || (right.X_add_symbol != NULL
1925 && S_IS_FORWARD_REF (right.X_add_symbol))))
1926 goto general;
1928 /* Optimize common cases. */
1929 #ifdef md_optimize_expr
1930 if (md_optimize_expr (resultP, op_left, &right))
1932 /* Skip. */
1933 is_unsigned = resultP->X_unsigned;
1935 else
1936 #endif
1937 if (op_left == O_add && right.X_op == O_constant
1938 && (md_register_arithmetic || resultP->X_op != O_register))
1940 /* X + constant. */
1941 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1943 /* This case comes up in PIC code. */
1944 else if (op_left == O_subtract
1945 && right.X_op == O_symbol
1946 && resultP->X_op == O_symbol
1947 && retval == rightseg
1948 #ifdef md_allow_local_subtract
1949 && md_allow_local_subtract (resultP, & right, rightseg)
1950 #endif
1951 && ((SEG_NORMAL (rightseg)
1952 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1953 && !S_FORCE_RELOC (right.X_add_symbol, 0))
1954 || right.X_add_symbol == resultP->X_add_symbol)
1955 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1956 symbol_get_frag (right.X_add_symbol),
1957 &frag_off))
1959 offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1960 - S_GET_VALUE (right.X_add_symbol);
1961 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1962 subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1963 add_to_result (resultP, symval_diff, symval_diff < 0);
1964 resultP->X_op = O_constant;
1965 resultP->X_add_symbol = 0;
1966 is_unsigned = false;
1968 else if (op_left == O_subtract && right.X_op == O_constant
1969 && (md_register_arithmetic || resultP->X_op != O_register))
1971 /* X - constant. */
1972 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1973 is_unsigned = false;
1975 else if (op_left == O_add && resultP->X_op == O_constant
1976 && (md_register_arithmetic || right.X_op != O_register))
1978 /* Constant + X. */
1979 resultP->X_op = right.X_op;
1980 resultP->X_add_symbol = right.X_add_symbol;
1981 resultP->X_op_symbol = right.X_op_symbol;
1982 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1983 retval = rightseg;
1985 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1987 /* Constant OP constant. */
1988 offsetT v = right.X_add_number;
1989 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1991 as_warn (_("division by zero"));
1992 v = 1;
1994 switch (op_left)
1996 default: goto general;
1997 case O_multiply:
1998 /* Do the multiply as unsigned to silence ubsan. The
1999 result is of course the same when we throw away high
2000 bits of the result. */
2001 resultP->X_add_number *= (valueT) v;
2002 break;
2003 case O_divide: resultP->X_add_number /= v; break;
2004 case O_modulus: resultP->X_add_number %= v; break;
2005 case O_left_shift:
2006 case O_right_shift:
2007 /* We always use unsigned shifts. According to the ISO
2008 C standard, left shift of a signed type having a
2009 negative value is undefined behaviour, and right
2010 shift of a signed type having negative value is
2011 implementation defined. Left shift of a signed type
2012 when the result overflows is also undefined
2013 behaviour. So don't trigger ubsan warnings or rely
2014 on characteristics of the compiler. */
2015 if ((valueT) v >= sizeof (valueT) * CHAR_BIT)
2017 as_warn_value_out_of_range (_("shift count"), v, 0,
2018 sizeof (valueT) * CHAR_BIT - 1,
2019 NULL, 0);
2020 resultP->X_add_number = 0;
2022 else if (op_left == O_left_shift)
2023 resultP->X_add_number
2024 = (valueT) resultP->X_add_number << (valueT) v;
2025 else
2026 resultP->X_add_number
2027 = (valueT) resultP->X_add_number >> (valueT) v;
2028 is_unsigned = resultP->X_unsigned;
2029 break;
2030 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
2031 case O_bit_or_not: resultP->X_add_number |= ~v; break;
2032 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
2033 case O_bit_and: resultP->X_add_number &= v; break;
2034 /* Constant + constant (O_add) is handled by the
2035 previous if statement for constant + X, so is omitted
2036 here. */
2037 case O_subtract:
2038 subtract_from_result (resultP, v, 0);
2039 is_unsigned = false;
2040 break;
2041 case O_eq:
2042 resultP->X_add_number =
2043 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
2044 is_unsigned = false;
2045 break;
2046 case O_ne:
2047 resultP->X_add_number =
2048 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
2049 is_unsigned = false;
2050 break;
2051 case O_lt:
2052 resultP->X_add_number =
2053 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
2054 is_unsigned = false;
2055 break;
2056 case O_le:
2057 resultP->X_add_number =
2058 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
2059 is_unsigned = false;
2060 break;
2061 case O_ge:
2062 resultP->X_add_number =
2063 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
2064 is_unsigned = false;
2065 break;
2066 case O_gt:
2067 resultP->X_add_number =
2068 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
2069 is_unsigned = false;
2070 break;
2071 case O_logical_and:
2072 resultP->X_add_number = resultP->X_add_number && v;
2073 is_unsigned = true;
2074 break;
2075 case O_logical_or:
2076 resultP->X_add_number = resultP->X_add_number || v;
2077 is_unsigned = true;
2078 break;
2081 else if (resultP->X_op == O_symbol
2082 && right.X_op == O_symbol
2083 && (op_left == O_add
2084 || op_left == O_subtract
2085 || (resultP->X_add_number == 0
2086 && right.X_add_number == 0)))
2088 /* Symbol OP symbol. */
2089 resultP->X_op = op_left;
2090 resultP->X_op_symbol = right.X_add_symbol;
2091 if (op_left == O_add)
2092 add_to_result (resultP, right.X_add_number, right.X_extrabit);
2093 else if (op_left == O_subtract)
2095 subtract_from_result (resultP, right.X_add_number,
2096 right.X_extrabit);
2097 if (retval == rightseg
2098 && SEG_NORMAL (retval)
2099 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2100 && !S_FORCE_RELOC (right.X_add_symbol, 0))
2102 retval = absolute_section;
2103 rightseg = absolute_section;
2107 else
2109 general:
2110 /* The general case. */
2111 resultP->X_add_symbol = make_expr_symbol (resultP);
2112 resultP->X_op_symbol = make_expr_symbol (&right);
2113 resultP->X_op = op_left;
2114 resultP->X_add_number = 0;
2115 resultP->X_extrabit = 0;
2118 resultP->X_unsigned = is_unsigned;
2120 if (retval != rightseg)
2122 if (retval == undefined_section)
2124 else if (rightseg == undefined_section)
2125 retval = rightseg;
2126 else if (retval == expr_section)
2128 else if (rightseg == expr_section)
2129 retval = rightseg;
2130 else if (retval == reg_section)
2132 else if (rightseg == reg_section)
2133 retval = rightseg;
2134 else if (rightseg == absolute_section)
2136 else if (retval == absolute_section)
2137 retval = rightseg;
2138 #ifdef DIFF_EXPR_OK
2139 else if (op_left == O_subtract)
2141 #endif
2142 else
2143 as_bad (_("operation combines symbols in different segments"));
2146 op_left = op_right;
2147 } /* While next operator is >= this rank. */
2149 /* The PA port needs this information. */
2150 if (resultP->X_add_symbol)
2151 symbol_mark_used (resultP->X_add_symbol);
2153 if (rank == 0 && mode == expr_evaluate)
2154 resolve_expression (resultP);
2156 return resultP->X_op == O_constant ? absolute_section : retval;
2159 /* Resolve an expression without changing any symbols/sub-expressions
2160 used. */
2163 resolve_expression (expressionS *expressionP)
2165 /* Help out with CSE. */
2166 valueT final_val = expressionP->X_add_number;
2167 symbolS *add_symbol = expressionP->X_add_symbol;
2168 symbolS *orig_add_symbol = add_symbol;
2169 symbolS *op_symbol = expressionP->X_op_symbol;
2170 operatorT op = expressionP->X_op;
2171 valueT left, right;
2172 segT seg_left, seg_right;
2173 fragS *frag_left, *frag_right;
2174 offsetT frag_off;
2176 switch (op)
2178 default:
2179 return 0;
2181 case O_constant:
2182 case O_register:
2183 left = 0;
2184 break;
2186 case O_symbol:
2187 case O_symbol_rva:
2188 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2189 return 0;
2191 break;
2193 case O_uminus:
2194 case O_bit_not:
2195 case O_logical_not:
2196 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2197 return 0;
2199 if (seg_left != absolute_section)
2200 return 0;
2202 if (op == O_logical_not)
2203 left = !left;
2204 else if (op == O_uminus)
2205 left = -left;
2206 else
2207 left = ~left;
2208 op = O_constant;
2209 break;
2211 case O_multiply:
2212 case O_divide:
2213 case O_modulus:
2214 case O_left_shift:
2215 case O_right_shift:
2216 case O_bit_inclusive_or:
2217 case O_bit_or_not:
2218 case O_bit_exclusive_or:
2219 case O_bit_and:
2220 case O_add:
2221 case O_subtract:
2222 case O_eq:
2223 case O_ne:
2224 case O_lt:
2225 case O_le:
2226 case O_ge:
2227 case O_gt:
2228 case O_logical_and:
2229 case O_logical_or:
2230 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2231 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2232 return 0;
2234 /* Simplify addition or subtraction of a constant by folding the
2235 constant into X_add_number. */
2236 if (op == O_add)
2238 if (seg_right == absolute_section)
2240 final_val += right;
2241 op = O_symbol;
2242 break;
2244 else if (seg_left == absolute_section)
2246 final_val += left;
2247 left = right;
2248 seg_left = seg_right;
2249 add_symbol = op_symbol;
2250 orig_add_symbol = expressionP->X_op_symbol;
2251 op = O_symbol;
2252 break;
2255 else if (op == O_subtract)
2257 if (seg_right == absolute_section)
2259 final_val -= right;
2260 op = O_symbol;
2261 break;
2265 /* Equality and non-equality tests are permitted on anything.
2266 Subtraction, and other comparison operators are permitted if
2267 both operands are in the same section.
2268 Shifts by constant zero are permitted on anything.
2269 Multiplies, bit-ors, and bit-ands with constant zero are
2270 permitted on anything.
2271 Multiplies and divides by constant one are permitted on
2272 anything.
2273 Binary operations with both operands being the same register
2274 or undefined symbol are permitted if the result doesn't depend
2275 on the input value.
2276 Otherwise, both operands must be absolute. We already handled
2277 the case of addition or subtraction of a constant above. */
2278 frag_off = 0;
2279 if (!(seg_left == absolute_section
2280 && seg_right == absolute_section)
2281 && !(op == O_eq || op == O_ne)
2282 && !((op == O_subtract
2283 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2284 && seg_left == seg_right
2285 && (finalize_syms
2286 || frag_offset_fixed_p (frag_left, frag_right, &frag_off)
2287 || (op == O_gt
2288 && frag_gtoffset_p (left, frag_left,
2289 right, frag_right, &frag_off)))
2290 && (seg_left != reg_section || left == right)
2291 && (seg_left != undefined_section || add_symbol == op_symbol)))
2293 if ((seg_left == absolute_section && left == 0)
2294 || (seg_right == absolute_section && right == 0))
2296 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2298 if (!(seg_right == absolute_section && right == 0))
2300 seg_left = seg_right;
2301 left = right;
2302 add_symbol = op_symbol;
2303 orig_add_symbol = expressionP->X_op_symbol;
2305 op = O_symbol;
2306 break;
2308 else if (op == O_left_shift || op == O_right_shift)
2310 if (!(seg_left == absolute_section && left == 0))
2312 op = O_symbol;
2313 break;
2316 else if (op != O_multiply
2317 && op != O_bit_or_not && op != O_bit_and)
2318 return 0;
2320 else if (op == O_multiply
2321 && seg_left == absolute_section && left == 1)
2323 seg_left = seg_right;
2324 left = right;
2325 add_symbol = op_symbol;
2326 orig_add_symbol = expressionP->X_op_symbol;
2327 op = O_symbol;
2328 break;
2330 else if ((op == O_multiply || op == O_divide)
2331 && seg_right == absolute_section && right == 1)
2333 op = O_symbol;
2334 break;
2336 else if (!(left == right
2337 && ((seg_left == reg_section && seg_right == reg_section)
2338 || (seg_left == undefined_section
2339 && seg_right == undefined_section
2340 && add_symbol == op_symbol))))
2341 return 0;
2342 else if (op == O_bit_and || op == O_bit_inclusive_or)
2344 op = O_symbol;
2345 break;
2347 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2348 return 0;
2351 right += frag_off / OCTETS_PER_BYTE;
2352 switch (op)
2354 case O_add: left += right; break;
2355 case O_subtract: left -= right; break;
2356 case O_multiply: left *= right; break;
2357 case O_divide:
2358 if (right == 0)
2359 return 0;
2360 left = (offsetT) left / (offsetT) right;
2361 break;
2362 case O_modulus:
2363 if (right == 0)
2364 return 0;
2365 left = (offsetT) left % (offsetT) right;
2366 break;
2367 case O_left_shift:
2368 if (right >= sizeof (left) * CHAR_BIT)
2369 left = 0;
2370 else
2371 left <<= right;
2372 break;
2373 case O_right_shift:
2374 if (right >= sizeof (left) * CHAR_BIT)
2375 left = 0;
2376 else
2377 left >>= right;
2378 break;
2379 case O_bit_inclusive_or: left |= right; break;
2380 case O_bit_or_not: left |= ~right; break;
2381 case O_bit_exclusive_or: left ^= right; break;
2382 case O_bit_and: left &= right; break;
2383 case O_eq:
2384 case O_ne:
2385 left = (left == right
2386 && seg_left == seg_right
2387 && (finalize_syms || frag_left == frag_right)
2388 && (seg_left != undefined_section
2389 || add_symbol == op_symbol)
2390 ? ~ (valueT) 0 : 0);
2391 if (op == O_ne)
2392 left = ~left;
2393 break;
2394 case O_lt:
2395 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2396 break;
2397 case O_le:
2398 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2399 break;
2400 case O_ge:
2401 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2402 break;
2403 case O_gt:
2404 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2405 break;
2406 case O_logical_and: left = left && right; break;
2407 case O_logical_or: left = left || right; break;
2408 default: abort ();
2411 op = O_constant;
2412 break;
2415 if (op == O_symbol)
2417 if (seg_left == absolute_section)
2418 op = O_constant;
2419 else if (seg_left == reg_section && final_val == 0)
2420 op = O_register;
2421 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2422 final_val += left;
2423 expressionP->X_add_symbol = add_symbol;
2425 expressionP->X_op = op;
2427 if (op == O_constant || op == O_register)
2428 final_val += left;
2429 expressionP->X_add_number = final_val;
2431 return 1;
2434 /* "Look through" register equates. */
2435 void resolve_register (expressionS *expP)
2437 symbolS *sym;
2438 offsetT acc = 0;
2439 const expressionS *e = expP;
2441 if (expP->X_op != O_symbol)
2442 return;
2446 sym = e->X_add_symbol;
2447 acc += e->X_add_number;
2448 e = symbol_get_value_expression (sym);
2450 while (symbol_equated_p (sym));
2452 if (e->X_op == O_register)
2454 *expP = *e;
2455 expP->X_add_number += acc;
2459 /* This lives here because it belongs equally in expr.c & read.c.
2460 expr.c is just a branch office read.c anyway, and putting it
2461 here lessens the crowd at read.c.
2463 Assume input_line_pointer is at start of symbol name, or the
2464 start of a double quote enclosed symbol name. Advance
2465 input_line_pointer past symbol name. Turn that character into a '\0',
2466 returning its former value, which may be the closing double quote.
2468 This allows a string compare (RMS wants symbol names to be strings)
2469 of the symbol name.
2471 NOTE: The input buffer is further altered when adjacent strings are
2472 concatenated by the function. Callers caring about the original buffer
2473 contents will need to make a copy before calling here.
2475 There will always be a char following symbol name, because all good
2476 lines end in end-of-line. */
2478 char
2479 get_symbol_name (char ** ilp_return)
2481 char c;
2483 * ilp_return = input_line_pointer;
2484 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2485 constructed string. */
2486 if (is_name_beginner (c = *input_line_pointer++)
2487 || (input_from_string && c == FAKE_LABEL_CHAR))
2489 while (is_part_of_name (c = *input_line_pointer++)
2490 || (input_from_string && c == FAKE_LABEL_CHAR))
2492 if (is_name_ender (c))
2493 c = *input_line_pointer++;
2495 else if (c == '"')
2497 char *dst = input_line_pointer;
2499 * ilp_return = input_line_pointer;
2500 for (;;)
2502 c = *input_line_pointer++;
2504 if (c == 0)
2506 as_warn (_("missing closing '\"'"));
2507 break;
2510 if (c == '"')
2512 char *ilp_save = input_line_pointer;
2514 SKIP_WHITESPACE ();
2515 if (*input_line_pointer == '"')
2517 ++input_line_pointer;
2518 continue;
2520 input_line_pointer = ilp_save;
2521 break;
2524 if (c == '\\')
2525 switch (*input_line_pointer)
2527 case '"':
2528 case '\\':
2529 c = *input_line_pointer++;
2530 break;
2532 default:
2533 if (c != 0)
2534 as_warn (_("'\\%c' in quoted symbol name; "
2535 "behavior may change in the future"),
2536 *input_line_pointer);
2537 break;
2540 *dst++ = c;
2542 *dst = 0;
2544 *--input_line_pointer = 0;
2545 return c;
2548 /* Replace the NUL character pointed to by input_line_pointer
2549 with C. If C is \" then advance past it. Return the character
2550 now pointed to by input_line_pointer. */
2552 char
2553 restore_line_pointer (char c)
2555 * input_line_pointer = c;
2556 if (c == '"')
2557 c = * ++ input_line_pointer;
2558 return c;
2561 unsigned int
2562 get_single_number (void)
2564 expressionS exp;
2565 operand (&exp, expr_normal);
2566 return exp.X_add_number;