* elf32-ppc.c (ppc_elf_adjust_dynamic_symbol): Don't eliminate
[binutils.git] / gas / expr.c
blobb88328502eebdddbd2250ebdb38c628dcbebb024
1 /* expr.c -operands, expressions-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This is really a branch office of as-read.c. I split it out to clearly
24 distinguish the world of expressions from the world of statements.
25 (It also gives smaller files to re-compile.)
26 Here, "operand"s are of expressions, not instructions. */
28 #define min(a, b) ((a) < (b) ? (a) : (b))
30 #include "as.h"
31 #include "safe-ctype.h"
32 #include "obstack.h"
34 static void floating_constant (expressionS * expressionP);
35 static valueT generic_bignum_to_int32 (void);
36 #ifdef BFD64
37 static valueT generic_bignum_to_int64 (void);
38 #endif
39 static void integer_constant (int radix, expressionS * expressionP);
40 static void mri_char_constant (expressionS *);
41 static void current_location (expressionS *);
42 static void clean_up_expression (expressionS * expressionP);
43 static segT operand (expressionS *, enum expr_mode);
44 static operatorT operator (int *);
46 extern const char EXP_CHARS[], FLT_CHARS[];
48 /* We keep a mapping of expression symbols to file positions, so that
49 we can provide better error messages. */
51 struct expr_symbol_line {
52 struct expr_symbol_line *next;
53 symbolS *sym;
54 char *file;
55 unsigned int line;
58 static struct expr_symbol_line *expr_symbol_lines;
60 /* Build a dummy symbol to hold a complex expression. This is how we
61 build expressions up out of other expressions. The symbol is put
62 into the fake section expr_section. */
64 symbolS *
65 make_expr_symbol (expressionS *expressionP)
67 expressionS zero;
68 symbolS *symbolP;
69 struct expr_symbol_line *n;
71 if (expressionP->X_op == O_symbol
72 && expressionP->X_add_number == 0)
73 return expressionP->X_add_symbol;
75 if (expressionP->X_op == O_big)
77 /* This won't work, because the actual value is stored in
78 generic_floating_point_number or generic_bignum, and we are
79 going to lose it if we haven't already. */
80 if (expressionP->X_add_number > 0)
81 as_bad (_("bignum invalid"));
82 else
83 as_bad (_("floating point number invalid"));
84 zero.X_op = O_constant;
85 zero.X_add_number = 0;
86 zero.X_unsigned = 0;
87 clean_up_expression (&zero);
88 expressionP = &zero;
91 /* Putting constant symbols in absolute_section rather than
92 expr_section is convenient for the old a.out code, for which
93 S_GET_SEGMENT does not always retrieve the value put in by
94 S_SET_SEGMENT. */
95 symbolP = symbol_create (FAKE_LABEL_NAME,
96 (expressionP->X_op == O_constant
97 ? absolute_section
98 : expr_section),
99 0, &zero_address_frag);
100 symbol_set_value_expression (symbolP, expressionP);
102 if (expressionP->X_op == O_constant)
103 resolve_symbol_value (symbolP);
105 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
106 n->sym = symbolP;
107 as_where (&n->file, &n->line);
108 n->next = expr_symbol_lines;
109 expr_symbol_lines = n;
111 return symbolP;
114 /* Return the file and line number for an expr symbol. Return
115 non-zero if something was found, 0 if no information is known for
116 the symbol. */
119 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
121 register struct expr_symbol_line *l;
123 for (l = expr_symbol_lines; l != NULL; l = l->next)
125 if (l->sym == sym)
127 *pfile = l->file;
128 *pline = l->line;
129 return 1;
133 return 0;
136 /* Utilities for building expressions.
137 Since complex expressions are recorded as symbols for use in other
138 expressions these return a symbolS * and not an expressionS *.
139 These explicitly do not take an "add_number" argument. */
140 /* ??? For completeness' sake one might want expr_build_symbol.
141 It would just return its argument. */
143 /* Build an expression for an unsigned constant.
144 The corresponding one for signed constants is missing because
145 there's currently no need for it. One could add an unsigned_p flag
146 but that seems more clumsy. */
148 symbolS *
149 expr_build_uconstant (offsetT value)
151 expressionS e;
153 e.X_op = O_constant;
154 e.X_add_number = value;
155 e.X_unsigned = 1;
156 return make_expr_symbol (&e);
159 /* Build an expression for the current location ('.'). */
161 symbolS *
162 expr_build_dot (void)
164 expressionS e;
166 current_location (&e);
167 return make_expr_symbol (&e);
170 /* Build any floating-point literal here.
171 Also build any bignum literal here. */
173 /* Seems atof_machine can backscan through generic_bignum and hit whatever
174 happens to be loaded before it in memory. And its way too complicated
175 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
176 and never write into the early words, thus they'll always be zero.
177 I hate Dean's floating-point code. Bleh. */
178 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
180 FLONUM_TYPE generic_floating_point_number = {
181 &generic_bignum[6], /* low. (JF: Was 0) */
182 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
183 0, /* leader. */
184 0, /* exponent. */
185 0 /* sign. */
189 static void
190 floating_constant (expressionS *expressionP)
192 /* input_line_pointer -> floating-point constant. */
193 int error_code;
195 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
196 &generic_floating_point_number);
198 if (error_code)
200 if (error_code == ERROR_EXPONENT_OVERFLOW)
202 as_bad (_("bad floating-point constant: exponent overflow"));
204 else
206 as_bad (_("bad floating-point constant: unknown error code=%d"),
207 error_code);
210 expressionP->X_op = O_big;
211 /* input_line_pointer -> just after constant, which may point to
212 whitespace. */
213 expressionP->X_add_number = -1;
216 static valueT
217 generic_bignum_to_int32 (void)
219 valueT number =
220 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
221 | (generic_bignum[0] & LITTLENUM_MASK);
222 number &= 0xffffffff;
223 return number;
226 #ifdef BFD64
227 static valueT
228 generic_bignum_to_int64 (void)
230 valueT number =
231 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
232 << LITTLENUM_NUMBER_OF_BITS)
233 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
234 << LITTLENUM_NUMBER_OF_BITS)
235 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
236 << LITTLENUM_NUMBER_OF_BITS)
237 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
238 return number;
240 #endif
242 static void
243 integer_constant (int radix, expressionS *expressionP)
245 char *start; /* Start of number. */
246 char *suffix = NULL;
247 char c;
248 valueT number; /* Offset or (absolute) value. */
249 short int digit; /* Value of next digit in current radix. */
250 short int maxdig = 0; /* Highest permitted digit value. */
251 int too_many_digits = 0; /* If we see >= this number of. */
252 char *name; /* Points to name of symbol. */
253 symbolS *symbolP; /* Points to symbol. */
255 int small; /* True if fits in 32 bits. */
257 /* May be bignum, or may fit in 32 bits. */
258 /* Most numbers fit into 32 bits, and we want this case to be fast.
259 so we pretend it will fit into 32 bits. If, after making up a 32
260 bit number, we realise that we have scanned more digits than
261 comfortably fit into 32 bits, we re-scan the digits coding them
262 into a bignum. For decimal and octal numbers we are
263 conservative: Some numbers may be assumed bignums when in fact
264 they do fit into 32 bits. Numbers of any radix can have excess
265 leading zeros: We strive to recognise this and cast them back
266 into 32 bits. We must check that the bignum really is more than
267 32 bits, and change it back to a 32-bit number if it fits. The
268 number we are looking for is expected to be positive, but if it
269 fits into 32 bits as an unsigned number, we let it be a 32-bit
270 number. The cavalier approach is for speed in ordinary cases. */
271 /* This has been extended for 64 bits. We blindly assume that if
272 you're compiling in 64-bit mode, the target is a 64-bit machine.
273 This should be cleaned up. */
275 #ifdef BFD64
276 #define valuesize 64
277 #else /* includes non-bfd case, mostly */
278 #define valuesize 32
279 #endif
281 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
283 int flt = 0;
285 /* In MRI mode, the number may have a suffix indicating the
286 radix. For that matter, it might actually be a floating
287 point constant. */
288 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
290 if (*suffix == 'e' || *suffix == 'E')
291 flt = 1;
294 if (suffix == input_line_pointer)
296 radix = 10;
297 suffix = NULL;
299 else
301 c = *--suffix;
302 c = TOUPPER (c);
303 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
304 we distinguish between 'B' and 'b'. This is the case for
305 Z80. */
306 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
307 radix = 2;
308 else if (c == 'D')
309 radix = 10;
310 else if (c == 'O' || c == 'Q')
311 radix = 8;
312 else if (c == 'H')
313 radix = 16;
314 else if (suffix[1] == '.' || c == 'E' || flt)
316 floating_constant (expressionP);
317 return;
319 else
321 radix = 10;
322 suffix = NULL;
327 switch (radix)
329 case 2:
330 maxdig = 2;
331 too_many_digits = valuesize + 1;
332 break;
333 case 8:
334 maxdig = radix = 8;
335 too_many_digits = (valuesize + 2) / 3 + 1;
336 break;
337 case 16:
338 maxdig = radix = 16;
339 too_many_digits = (valuesize + 3) / 4 + 1;
340 break;
341 case 10:
342 maxdig = radix = 10;
343 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
345 #undef valuesize
346 start = input_line_pointer;
347 c = *input_line_pointer++;
348 for (number = 0;
349 (digit = hex_value (c)) < maxdig;
350 c = *input_line_pointer++)
352 number = number * radix + digit;
354 /* c contains character after number. */
355 /* input_line_pointer->char after c. */
356 small = (input_line_pointer - start - 1) < too_many_digits;
358 if (radix == 16 && c == '_')
360 /* This is literal of the form 0x333_0_12345678_1.
361 This example is equivalent to 0x00000333000000001234567800000001. */
363 int num_little_digits = 0;
364 int i;
365 input_line_pointer = start; /* -> 1st digit. */
367 know (LITTLENUM_NUMBER_OF_BITS == 16);
369 for (c = '_'; c == '_'; num_little_digits += 2)
372 /* Convert one 64-bit word. */
373 int ndigit = 0;
374 number = 0;
375 for (c = *input_line_pointer++;
376 (digit = hex_value (c)) < maxdig;
377 c = *(input_line_pointer++))
379 number = number * radix + digit;
380 ndigit++;
383 /* Check for 8 digit per word max. */
384 if (ndigit > 8)
385 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
387 /* Add this chunk to the bignum.
388 Shift things down 2 little digits. */
389 know (LITTLENUM_NUMBER_OF_BITS == 16);
390 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
391 i >= 2;
392 i--)
393 generic_bignum[i] = generic_bignum[i - 2];
395 /* Add the new digits as the least significant new ones. */
396 generic_bignum[0] = number & 0xffffffff;
397 generic_bignum[1] = number >> 16;
400 /* Again, c is char after number, input_line_pointer->after c. */
402 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
403 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
405 assert (num_little_digits >= 4);
407 if (num_little_digits != 8)
408 as_bad (_("a bignum with underscores must have exactly 4 words"));
410 /* We might have some leading zeros. These can be trimmed to give
411 us a change to fit this constant into a small number. */
412 while (generic_bignum[num_little_digits - 1] == 0
413 && num_little_digits > 1)
414 num_little_digits--;
416 if (num_little_digits <= 2)
418 /* will fit into 32 bits. */
419 number = generic_bignum_to_int32 ();
420 small = 1;
422 #ifdef BFD64
423 else if (num_little_digits <= 4)
425 /* Will fit into 64 bits. */
426 number = generic_bignum_to_int64 ();
427 small = 1;
429 #endif
430 else
432 small = 0;
434 /* Number of littlenums in the bignum. */
435 number = num_little_digits;
438 else if (!small)
440 /* We saw a lot of digits. manufacture a bignum the hard way. */
441 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
442 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
443 long carry;
445 leader = generic_bignum;
446 generic_bignum[0] = 0;
447 generic_bignum[1] = 0;
448 generic_bignum[2] = 0;
449 generic_bignum[3] = 0;
450 input_line_pointer = start; /* -> 1st digit. */
451 c = *input_line_pointer++;
452 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
454 for (pointer = generic_bignum; pointer <= leader; pointer++)
456 long work;
458 work = carry + radix * *pointer;
459 *pointer = work & LITTLENUM_MASK;
460 carry = work >> LITTLENUM_NUMBER_OF_BITS;
462 if (carry)
464 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
466 /* Room to grow a longer bignum. */
467 *++leader = carry;
471 /* Again, c is char after number. */
472 /* input_line_pointer -> after c. */
473 know (LITTLENUM_NUMBER_OF_BITS == 16);
474 if (leader < generic_bignum + 2)
476 /* Will fit into 32 bits. */
477 number = generic_bignum_to_int32 ();
478 small = 1;
480 #ifdef BFD64
481 else if (leader < generic_bignum + 4)
483 /* Will fit into 64 bits. */
484 number = generic_bignum_to_int64 ();
485 small = 1;
487 #endif
488 else
490 /* Number of littlenums in the bignum. */
491 number = leader - generic_bignum + 1;
495 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
496 && suffix != NULL
497 && input_line_pointer - 1 == suffix)
498 c = *input_line_pointer++;
500 if (small)
502 /* Here with number, in correct radix. c is the next char.
503 Note that unlike un*x, we allow "011f" "0x9f" to both mean
504 the same as the (conventional) "9f".
505 This is simply easier than checking for strict canonical
506 form. Syntax sux! */
508 if (LOCAL_LABELS_FB && c == 'b')
510 /* Backward ref to local label.
511 Because it is backward, expect it to be defined. */
512 /* Construct a local label. */
513 name = fb_label_name ((int) number, 0);
515 /* Seen before, or symbol is defined: OK. */
516 symbolP = symbol_find (name);
517 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
519 /* Local labels are never absolute. Don't waste time
520 checking absoluteness. */
521 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
523 expressionP->X_op = O_symbol;
524 expressionP->X_add_symbol = symbolP;
526 else
528 /* Either not seen or not defined. */
529 /* @@ Should print out the original string instead of
530 the parsed number. */
531 as_bad (_("backward ref to unknown label \"%d:\""),
532 (int) number);
533 expressionP->X_op = O_constant;
536 expressionP->X_add_number = 0;
537 } /* case 'b' */
538 else if (LOCAL_LABELS_FB && c == 'f')
540 /* Forward reference. Expect symbol to be undefined or
541 unknown. undefined: seen it before. unknown: never seen
542 it before.
544 Construct a local label name, then an undefined symbol.
545 Don't create a xseg frag for it: caller may do that.
546 Just return it as never seen before. */
547 name = fb_label_name ((int) number, 1);
548 symbolP = symbol_find_or_make (name);
549 /* We have no need to check symbol properties. */
550 #ifndef many_segments
551 /* Since "know" puts its arg into a "string", we
552 can't have newlines in the argument. */
553 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
554 #endif
555 expressionP->X_op = O_symbol;
556 expressionP->X_add_symbol = symbolP;
557 expressionP->X_add_number = 0;
558 } /* case 'f' */
559 else if (LOCAL_LABELS_DOLLAR && c == '$')
561 /* If the dollar label is *currently* defined, then this is just
562 another reference to it. If it is not *currently* defined,
563 then this is a fresh instantiation of that number, so create
564 it. */
566 if (dollar_label_defined ((long) number))
568 name = dollar_label_name ((long) number, 0);
569 symbolP = symbol_find (name);
570 know (symbolP != NULL);
572 else
574 name = dollar_label_name ((long) number, 1);
575 symbolP = symbol_find_or_make (name);
578 expressionP->X_op = O_symbol;
579 expressionP->X_add_symbol = symbolP;
580 expressionP->X_add_number = 0;
581 } /* case '$' */
582 else
584 expressionP->X_op = O_constant;
585 expressionP->X_add_number = number;
586 input_line_pointer--; /* Restore following character. */
587 } /* Really just a number. */
589 else
591 /* Not a small number. */
592 expressionP->X_op = O_big;
593 expressionP->X_add_number = number; /* Number of littlenums. */
594 input_line_pointer--; /* -> char following number. */
598 /* Parse an MRI multi character constant. */
600 static void
601 mri_char_constant (expressionS *expressionP)
603 int i;
605 if (*input_line_pointer == '\''
606 && input_line_pointer[1] != '\'')
608 expressionP->X_op = O_constant;
609 expressionP->X_add_number = 0;
610 return;
613 /* In order to get the correct byte ordering, we must build the
614 number in reverse. */
615 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
617 int j;
619 generic_bignum[i] = 0;
620 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
622 if (*input_line_pointer == '\'')
624 if (input_line_pointer[1] != '\'')
625 break;
626 ++input_line_pointer;
628 generic_bignum[i] <<= 8;
629 generic_bignum[i] += *input_line_pointer;
630 ++input_line_pointer;
633 if (i < SIZE_OF_LARGE_NUMBER - 1)
635 /* If there is more than one littlenum, left justify the
636 last one to make it match the earlier ones. If there is
637 only one, we can just use the value directly. */
638 for (; j < CHARS_PER_LITTLENUM; j++)
639 generic_bignum[i] <<= 8;
642 if (*input_line_pointer == '\''
643 && input_line_pointer[1] != '\'')
644 break;
647 if (i < 0)
649 as_bad (_("character constant too large"));
650 i = 0;
653 if (i > 0)
655 int c;
656 int j;
658 c = SIZE_OF_LARGE_NUMBER - i;
659 for (j = 0; j < c; j++)
660 generic_bignum[j] = generic_bignum[i + j];
661 i = c;
664 know (LITTLENUM_NUMBER_OF_BITS == 16);
665 if (i > 2)
667 expressionP->X_op = O_big;
668 expressionP->X_add_number = i;
670 else
672 expressionP->X_op = O_constant;
673 if (i < 2)
674 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
675 else
676 expressionP->X_add_number =
677 (((generic_bignum[1] & LITTLENUM_MASK)
678 << LITTLENUM_NUMBER_OF_BITS)
679 | (generic_bignum[0] & LITTLENUM_MASK));
682 /* Skip the final closing quote. */
683 ++input_line_pointer;
686 /* Return an expression representing the current location. This
687 handles the magic symbol `.'. */
689 static void
690 current_location (expressionS *expressionp)
692 if (now_seg == absolute_section)
694 expressionp->X_op = O_constant;
695 expressionp->X_add_number = abs_section_offset;
697 else
699 expressionp->X_op = O_symbol;
700 expressionp->X_add_symbol = symbol_temp_new_now ();
701 expressionp->X_add_number = 0;
705 /* In: Input_line_pointer points to 1st char of operand, which may
706 be a space.
708 Out: An expressionS.
709 The operand may have been empty: in this case X_op == O_absent.
710 Input_line_pointer->(next non-blank) char after operand. */
712 static segT
713 operand (expressionS *expressionP, enum expr_mode mode)
715 char c;
716 symbolS *symbolP; /* Points to symbol. */
717 char *name; /* Points to name of symbol. */
718 segT segment;
720 /* All integers are regarded as unsigned unless they are negated.
721 This is because the only thing which cares whether a number is
722 unsigned is the code in emit_expr which extends constants into
723 bignums. It should only sign extend negative numbers, so that
724 something like ``.quad 0x80000000'' is not sign extended even
725 though it appears negative if valueT is 32 bits. */
726 expressionP->X_unsigned = 1;
728 /* Digits, assume it is a bignum. */
730 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
731 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
733 if (is_end_of_line[(unsigned char) c])
734 goto eol;
736 switch (c)
738 case '1':
739 case '2':
740 case '3':
741 case '4':
742 case '5':
743 case '6':
744 case '7':
745 case '8':
746 case '9':
747 input_line_pointer--;
749 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
750 ? 0 : 10,
751 expressionP);
752 break;
754 #ifdef LITERAL_PREFIXDOLLAR_HEX
755 case '$':
756 /* $L is the start of a local label, not a hex constant. */
757 if (* input_line_pointer == 'L')
758 goto isname;
759 integer_constant (16, expressionP);
760 break;
761 #endif
763 #ifdef LITERAL_PREFIXPERCENT_BIN
764 case '%':
765 integer_constant (2, expressionP);
766 break;
767 #endif
769 case '0':
770 /* Non-decimal radix. */
772 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
774 char *s;
776 /* Check for a hex or float constant. */
777 for (s = input_line_pointer; hex_p (*s); s++)
779 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
781 --input_line_pointer;
782 integer_constant (0, expressionP);
783 break;
786 c = *input_line_pointer;
787 switch (c)
789 case 'o':
790 case 'O':
791 case 'q':
792 case 'Q':
793 case '8':
794 case '9':
795 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
797 integer_constant (0, expressionP);
798 break;
800 /* Fall through. */
801 default:
802 default_case:
803 if (c && strchr (FLT_CHARS, c))
805 input_line_pointer++;
806 floating_constant (expressionP);
807 expressionP->X_add_number = - TOLOWER (c);
809 else
811 /* The string was only zero. */
812 expressionP->X_op = O_constant;
813 expressionP->X_add_number = 0;
816 break;
818 case 'x':
819 case 'X':
820 if (flag_m68k_mri)
821 goto default_case;
822 input_line_pointer++;
823 integer_constant (16, expressionP);
824 break;
826 case 'b':
827 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
829 /* This code used to check for '+' and '-' here, and, in
830 some conditions, fall through to call
831 integer_constant. However, that didn't make sense,
832 as integer_constant only accepts digits. */
833 /* Some of our code elsewhere does permit digits greater
834 than the expected base; for consistency, do the same
835 here. */
836 if (input_line_pointer[1] < '0'
837 || input_line_pointer[1] > '9')
839 /* Parse this as a back reference to label 0. */
840 input_line_pointer--;
841 integer_constant (10, expressionP);
842 break;
844 /* Otherwise, parse this as a binary number. */
846 /* Fall through. */
847 case 'B':
848 input_line_pointer++;
849 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
850 goto default_case;
851 integer_constant (2, expressionP);
852 break;
854 case '0':
855 case '1':
856 case '2':
857 case '3':
858 case '4':
859 case '5':
860 case '6':
861 case '7':
862 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
863 ? 0 : 8,
864 expressionP);
865 break;
867 case 'f':
868 if (LOCAL_LABELS_FB)
870 /* If it says "0f" and it could possibly be a floating point
871 number, make it one. Otherwise, make it a local label,
872 and try to deal with parsing the rest later. */
873 if (!input_line_pointer[1]
874 || (is_end_of_line[0xff & input_line_pointer[1]])
875 || strchr (FLT_CHARS, 'f') == NULL)
876 goto is_0f_label;
878 char *cp = input_line_pointer + 1;
879 int r = atof_generic (&cp, ".", EXP_CHARS,
880 &generic_floating_point_number);
881 switch (r)
883 case 0:
884 case ERROR_EXPONENT_OVERFLOW:
885 if (*cp == 'f' || *cp == 'b')
886 /* Looks like a difference expression. */
887 goto is_0f_label;
888 else if (cp == input_line_pointer + 1)
889 /* No characters has been accepted -- looks like
890 end of operand. */
891 goto is_0f_label;
892 else
893 goto is_0f_float;
894 default:
895 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
900 /* Okay, now we've sorted it out. We resume at one of these
901 two labels, depending on what we've decided we're probably
902 looking at. */
903 is_0f_label:
904 input_line_pointer--;
905 integer_constant (10, expressionP);
906 break;
908 is_0f_float:
909 /* Fall through. */
913 case 'd':
914 case 'D':
915 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
917 integer_constant (0, expressionP);
918 break;
920 /* Fall through. */
921 case 'F':
922 case 'r':
923 case 'e':
924 case 'E':
925 case 'g':
926 case 'G':
927 input_line_pointer++;
928 floating_constant (expressionP);
929 expressionP->X_add_number = - TOLOWER (c);
930 break;
932 case '$':
933 if (LOCAL_LABELS_DOLLAR)
935 integer_constant (10, expressionP);
936 break;
938 else
939 goto default_case;
942 break;
944 case '(':
945 #ifndef NEED_INDEX_OPERATOR
946 case '[':
947 #endif
948 /* Didn't begin with digit & not a name. */
949 if (mode != expr_defer)
950 segment = expression (expressionP);
951 else
952 segment = deferred_expression (expressionP);
953 /* expression () will pass trailing whitespace. */
954 if ((c == '(' && *input_line_pointer != ')')
955 || (c == '[' && *input_line_pointer != ']'))
956 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
957 else
958 input_line_pointer++;
959 SKIP_WHITESPACE ();
960 /* Here with input_line_pointer -> char after "(...)". */
961 return segment;
963 #ifdef TC_M68K
964 case 'E':
965 if (! flag_m68k_mri || *input_line_pointer != '\'')
966 goto de_fault;
967 as_bad (_("EBCDIC constants are not supported"));
968 /* Fall through. */
969 case 'A':
970 if (! flag_m68k_mri || *input_line_pointer != '\'')
971 goto de_fault;
972 ++input_line_pointer;
973 /* Fall through. */
974 #endif
975 case '\'':
976 if (! flag_m68k_mri)
978 /* Warning: to conform to other people's assemblers NO
979 ESCAPEMENT is permitted for a single quote. The next
980 character, parity errors and all, is taken as the value
981 of the operand. VERY KINKY. */
982 expressionP->X_op = O_constant;
983 expressionP->X_add_number = *input_line_pointer++;
984 break;
987 mri_char_constant (expressionP);
988 break;
990 #ifdef TC_M68K
991 case '"':
992 /* Double quote is the bitwise not operator in MRI mode. */
993 if (! flag_m68k_mri)
994 goto de_fault;
995 /* Fall through. */
996 #endif
997 case '~':
998 /* '~' is permitted to start a label on the Delta. */
999 if (is_name_beginner (c))
1000 goto isname;
1001 case '!':
1002 case '-':
1003 case '+':
1005 operand (expressionP, mode);
1006 if (expressionP->X_op == O_constant)
1008 /* input_line_pointer -> char after operand. */
1009 if (c == '-')
1011 expressionP->X_add_number = - expressionP->X_add_number;
1012 /* Notice: '-' may overflow: no warning is given.
1013 This is compatible with other people's
1014 assemblers. Sigh. */
1015 expressionP->X_unsigned = 0;
1017 else if (c == '~' || c == '"')
1018 expressionP->X_add_number = ~ expressionP->X_add_number;
1019 else if (c == '!')
1020 expressionP->X_add_number = ! expressionP->X_add_number;
1022 else if (expressionP->X_op == O_big
1023 && expressionP->X_add_number <= 0
1024 && c == '-'
1025 && (generic_floating_point_number.sign == '+'
1026 || generic_floating_point_number.sign == 'P'))
1028 /* Negative flonum (eg, -1.000e0). */
1029 if (generic_floating_point_number.sign == '+')
1030 generic_floating_point_number.sign = '-';
1031 else
1032 generic_floating_point_number.sign = 'N';
1034 else if (expressionP->X_op == O_big
1035 && expressionP->X_add_number > 0)
1037 int i;
1039 if (c == '~' || c == '-')
1041 for (i = 0; i < expressionP->X_add_number; ++i)
1042 generic_bignum[i] = ~generic_bignum[i];
1043 if (c == '-')
1044 for (i = 0; i < expressionP->X_add_number; ++i)
1046 generic_bignum[i] += 1;
1047 if (generic_bignum[i])
1048 break;
1051 else if (c == '!')
1053 int nonzero = 0;
1054 for (i = 0; i < expressionP->X_add_number; ++i)
1056 if (generic_bignum[i])
1057 nonzero = 1;
1058 generic_bignum[i] = 0;
1060 generic_bignum[0] = nonzero;
1063 else if (expressionP->X_op != O_illegal
1064 && expressionP->X_op != O_absent)
1066 if (c != '+')
1068 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1069 if (c == '-')
1070 expressionP->X_op = O_uminus;
1071 else if (c == '~' || c == '"')
1072 expressionP->X_op = O_bit_not;
1073 else
1074 expressionP->X_op = O_logical_not;
1075 expressionP->X_add_number = 0;
1078 else
1079 as_warn (_("Unary operator %c ignored because bad operand follows"),
1082 break;
1084 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1085 case '$':
1086 /* '$' is the program counter when in MRI mode, or when
1087 DOLLAR_DOT is defined. */
1088 #ifndef DOLLAR_DOT
1089 if (! flag_m68k_mri)
1090 goto de_fault;
1091 #endif
1092 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1094 /* In MRI mode and on Z80, '$' is also used as the prefix
1095 for a hexadecimal constant. */
1096 integer_constant (16, expressionP);
1097 break;
1100 if (is_part_of_name (*input_line_pointer))
1101 goto isname;
1103 current_location (expressionP);
1104 break;
1105 #endif
1107 case '.':
1108 if (!is_part_of_name (*input_line_pointer))
1110 current_location (expressionP);
1111 break;
1113 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1114 && ! is_part_of_name (input_line_pointer[8]))
1115 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1116 && ! is_part_of_name (input_line_pointer[7])))
1118 int start;
1120 start = (input_line_pointer[1] == 't'
1121 || input_line_pointer[1] == 'T');
1122 input_line_pointer += start ? 8 : 7;
1123 SKIP_WHITESPACE ();
1124 if (*input_line_pointer != '(')
1125 as_bad (_("syntax error in .startof. or .sizeof."));
1126 else
1128 char *buf;
1130 ++input_line_pointer;
1131 SKIP_WHITESPACE ();
1132 name = input_line_pointer;
1133 c = get_symbol_end ();
1135 buf = (char *) xmalloc (strlen (name) + 10);
1136 if (start)
1137 sprintf (buf, ".startof.%s", name);
1138 else
1139 sprintf (buf, ".sizeof.%s", name);
1140 symbolP = symbol_make (buf);
1141 free (buf);
1143 expressionP->X_op = O_symbol;
1144 expressionP->X_add_symbol = symbolP;
1145 expressionP->X_add_number = 0;
1147 *input_line_pointer = c;
1148 SKIP_WHITESPACE ();
1149 if (*input_line_pointer != ')')
1150 as_bad (_("syntax error in .startof. or .sizeof."));
1151 else
1152 ++input_line_pointer;
1154 break;
1156 else
1158 goto isname;
1161 case ',':
1162 eol:
1163 /* Can't imagine any other kind of operand. */
1164 expressionP->X_op = O_absent;
1165 input_line_pointer--;
1166 break;
1168 #ifdef TC_M68K
1169 case '%':
1170 if (! flag_m68k_mri)
1171 goto de_fault;
1172 integer_constant (2, expressionP);
1173 break;
1175 case '@':
1176 if (! flag_m68k_mri)
1177 goto de_fault;
1178 integer_constant (8, expressionP);
1179 break;
1181 case ':':
1182 if (! flag_m68k_mri)
1183 goto de_fault;
1185 /* In MRI mode, this is a floating point constant represented
1186 using hexadecimal digits. */
1188 ++input_line_pointer;
1189 integer_constant (16, expressionP);
1190 break;
1192 case '*':
1193 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1194 goto de_fault;
1196 current_location (expressionP);
1197 break;
1198 #endif
1200 default:
1201 #ifdef TC_M68K
1202 de_fault:
1203 #endif
1204 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1206 /* Identifier begins here.
1207 This is kludged for speed, so code is repeated. */
1208 isname:
1209 name = --input_line_pointer;
1210 c = get_symbol_end ();
1212 #ifdef md_parse_name
1213 /* This is a hook for the backend to parse certain names
1214 specially in certain contexts. If a name always has a
1215 specific value, it can often be handled by simply
1216 entering it in the symbol table. */
1217 if (md_parse_name (name, expressionP, mode, &c))
1219 *input_line_pointer = c;
1220 break;
1222 #endif
1224 #ifdef TC_I960
1225 /* The MRI i960 assembler permits
1226 lda sizeof code,g13
1227 FIXME: This should use md_parse_name. */
1228 if (flag_mri
1229 && (strcasecmp (name, "sizeof") == 0
1230 || strcasecmp (name, "startof") == 0))
1232 int start;
1233 char *buf;
1235 start = (name[1] == 't'
1236 || name[1] == 'T');
1238 *input_line_pointer = c;
1239 SKIP_WHITESPACE ();
1241 name = input_line_pointer;
1242 c = get_symbol_end ();
1244 buf = (char *) xmalloc (strlen (name) + 10);
1245 if (start)
1246 sprintf (buf, ".startof.%s", name);
1247 else
1248 sprintf (buf, ".sizeof.%s", name);
1249 symbolP = symbol_make (buf);
1250 free (buf);
1252 expressionP->X_op = O_symbol;
1253 expressionP->X_add_symbol = symbolP;
1254 expressionP->X_add_number = 0;
1256 *input_line_pointer = c;
1257 SKIP_WHITESPACE ();
1259 break;
1261 #endif
1263 symbolP = symbol_find_or_make (name);
1265 /* If we have an absolute symbol or a reg, then we know its
1266 value now. */
1267 segment = S_GET_SEGMENT (symbolP);
1268 if (mode != expr_defer && segment == absolute_section)
1270 expressionP->X_op = O_constant;
1271 expressionP->X_add_number = S_GET_VALUE (symbolP);
1273 else if (mode != expr_defer && segment == reg_section)
1275 expressionP->X_op = O_register;
1276 expressionP->X_add_number = S_GET_VALUE (symbolP);
1278 else
1280 expressionP->X_op = O_symbol;
1281 expressionP->X_add_symbol = symbolP;
1282 expressionP->X_add_number = 0;
1284 *input_line_pointer = c;
1286 else
1288 /* Let the target try to parse it. Success is indicated by changing
1289 the X_op field to something other than O_absent and pointing
1290 input_line_pointer past the expression. If it can't parse the
1291 expression, X_op and input_line_pointer should be unchanged. */
1292 expressionP->X_op = O_absent;
1293 --input_line_pointer;
1294 md_operand (expressionP);
1295 if (expressionP->X_op == O_absent)
1297 ++input_line_pointer;
1298 as_bad (_("bad expression"));
1299 expressionP->X_op = O_constant;
1300 expressionP->X_add_number = 0;
1303 break;
1306 /* It is more 'efficient' to clean up the expressionS when they are
1307 created. Doing it here saves lines of code. */
1308 clean_up_expression (expressionP);
1309 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1310 know (*input_line_pointer != ' ');
1312 /* The PA port needs this information. */
1313 if (expressionP->X_add_symbol)
1314 symbol_mark_used (expressionP->X_add_symbol);
1316 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1317 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1319 switch (expressionP->X_op)
1321 default:
1322 return absolute_section;
1323 case O_symbol:
1324 return S_GET_SEGMENT (expressionP->X_add_symbol);
1325 case O_register:
1326 return reg_section;
1330 /* Internal. Simplify a struct expression for use by expr (). */
1332 /* In: address of an expressionS.
1333 The X_op field of the expressionS may only take certain values.
1334 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1336 Out: expressionS may have been modified:
1337 Unused fields zeroed to help expr (). */
1339 static void
1340 clean_up_expression (expressionS *expressionP)
1342 switch (expressionP->X_op)
1344 case O_illegal:
1345 case O_absent:
1346 expressionP->X_add_number = 0;
1347 /* Fall through. */
1348 case O_big:
1349 case O_constant:
1350 case O_register:
1351 expressionP->X_add_symbol = NULL;
1352 /* Fall through. */
1353 case O_symbol:
1354 case O_uminus:
1355 case O_bit_not:
1356 expressionP->X_op_symbol = NULL;
1357 break;
1358 default:
1359 break;
1363 /* Expression parser. */
1365 /* We allow an empty expression, and just assume (absolute,0) silently.
1366 Unary operators and parenthetical expressions are treated as operands.
1367 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1369 We used to do an aho/ullman shift-reduce parser, but the logic got so
1370 warped that I flushed it and wrote a recursive-descent parser instead.
1371 Now things are stable, would anybody like to write a fast parser?
1372 Most expressions are either register (which does not even reach here)
1373 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1374 So I guess it doesn't really matter how inefficient more complex expressions
1375 are parsed.
1377 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1378 Also, we have consumed any leading or trailing spaces (operand does that)
1379 and done all intervening operators.
1381 This returns the segment of the result, which will be
1382 absolute_section or the segment of a symbol. */
1384 #undef __
1385 #define __ O_illegal
1386 #ifndef O_SINGLE_EQ
1387 #define O_SINGLE_EQ O_illegal
1388 #endif
1390 /* Maps ASCII -> operators. */
1391 static const operatorT op_encoding[256] = {
1392 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1393 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1395 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1396 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1397 __, __, __, __, __, __, __, __,
1398 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1399 __, __, __, __, __, __, __, __,
1400 __, __, __, __, __, __, __, __,
1401 __, __, __, __, __, __, __, __,
1402 __, __, __,
1403 #ifdef NEED_INDEX_OPERATOR
1404 O_index,
1405 #else
1407 #endif
1408 __, __, O_bit_exclusive_or, __,
1409 __, __, __, __, __, __, __, __,
1410 __, __, __, __, __, __, __, __,
1411 __, __, __, __, __, __, __, __,
1412 __, __, __, __, O_bit_inclusive_or, __, __, __,
1414 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1415 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1416 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1417 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1418 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1419 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1420 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1421 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1424 /* Rank Examples
1425 0 operand, (expression)
1426 1 ||
1427 2 &&
1428 3 == <> < <= >= >
1429 4 + -
1430 5 used for * / % in MRI mode
1431 6 & ^ ! |
1432 7 * / % << >>
1433 8 unary - unary ~
1435 static operator_rankT op_rank[] = {
1436 0, /* O_illegal */
1437 0, /* O_absent */
1438 0, /* O_constant */
1439 0, /* O_symbol */
1440 0, /* O_symbol_rva */
1441 0, /* O_register */
1442 0, /* O_big */
1443 9, /* O_uminus */
1444 9, /* O_bit_not */
1445 9, /* O_logical_not */
1446 8, /* O_multiply */
1447 8, /* O_divide */
1448 8, /* O_modulus */
1449 8, /* O_left_shift */
1450 8, /* O_right_shift */
1451 7, /* O_bit_inclusive_or */
1452 7, /* O_bit_or_not */
1453 7, /* O_bit_exclusive_or */
1454 7, /* O_bit_and */
1455 5, /* O_add */
1456 5, /* O_subtract */
1457 4, /* O_eq */
1458 4, /* O_ne */
1459 4, /* O_lt */
1460 4, /* O_le */
1461 4, /* O_ge */
1462 4, /* O_gt */
1463 3, /* O_logical_and */
1464 2, /* O_logical_or */
1465 1, /* O_index */
1466 0, /* O_md1 */
1467 0, /* O_md2 */
1468 0, /* O_md3 */
1469 0, /* O_md4 */
1470 0, /* O_md5 */
1471 0, /* O_md6 */
1472 0, /* O_md7 */
1473 0, /* O_md8 */
1474 0, /* O_md9 */
1475 0, /* O_md10 */
1476 0, /* O_md11 */
1477 0, /* O_md12 */
1478 0, /* O_md13 */
1479 0, /* O_md14 */
1480 0, /* O_md15 */
1481 0, /* O_md16 */
1484 /* Unfortunately, in MRI mode for the m68k, multiplication and
1485 division have lower precedence than the bit wise operators. This
1486 function sets the operator precedences correctly for the current
1487 mode. Also, MRI uses a different bit_not operator, and this fixes
1488 that as well. */
1490 #define STANDARD_MUL_PRECEDENCE 8
1491 #define MRI_MUL_PRECEDENCE 6
1493 void
1494 expr_set_precedence (void)
1496 if (flag_m68k_mri)
1498 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1499 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1500 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1502 else
1504 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1505 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1506 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1510 /* Initialize the expression parser. */
1512 void
1513 expr_begin (void)
1515 expr_set_precedence ();
1517 /* Verify that X_op field is wide enough. */
1519 expressionS e;
1520 e.X_op = O_max;
1521 assert (e.X_op == O_max);
1525 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1526 sets NUM_CHARS to the number of characters in the operator.
1527 Does not advance INPUT_LINE_POINTER. */
1529 static inline operatorT
1530 operator (int *num_chars)
1532 int c;
1533 operatorT ret;
1535 c = *input_line_pointer & 0xff;
1536 *num_chars = 1;
1538 if (is_end_of_line[c])
1539 return O_illegal;
1541 switch (c)
1543 default:
1544 return op_encoding[c];
1546 case '+':
1547 case '-':
1548 return op_encoding[c];
1550 case '<':
1551 switch (input_line_pointer[1])
1553 default:
1554 return op_encoding[c];
1555 case '<':
1556 ret = O_left_shift;
1557 break;
1558 case '>':
1559 ret = O_ne;
1560 break;
1561 case '=':
1562 ret = O_le;
1563 break;
1565 *num_chars = 2;
1566 return ret;
1568 case '=':
1569 if (input_line_pointer[1] != '=')
1570 return op_encoding[c];
1572 *num_chars = 2;
1573 return O_eq;
1575 case '>':
1576 switch (input_line_pointer[1])
1578 default:
1579 return op_encoding[c];
1580 case '>':
1581 ret = O_right_shift;
1582 break;
1583 case '=':
1584 ret = O_ge;
1585 break;
1587 *num_chars = 2;
1588 return ret;
1590 case '!':
1591 switch (input_line_pointer[1])
1593 case '!':
1594 /* We accept !! as equivalent to ^ for MRI compatibility. */
1595 *num_chars = 2;
1596 return O_bit_exclusive_or;
1597 case '=':
1598 /* We accept != as equivalent to <>. */
1599 *num_chars = 2;
1600 return O_ne;
1601 default:
1602 if (flag_m68k_mri)
1603 return O_bit_inclusive_or;
1604 return op_encoding[c];
1607 case '|':
1608 if (input_line_pointer[1] != '|')
1609 return op_encoding[c];
1611 *num_chars = 2;
1612 return O_logical_or;
1614 case '&':
1615 if (input_line_pointer[1] != '&')
1616 return op_encoding[c];
1618 *num_chars = 2;
1619 return O_logical_and;
1622 /* NOTREACHED */
1625 /* Parse an expression. */
1627 segT
1628 expr (int rankarg, /* Larger # is higher rank. */
1629 expressionS *resultP, /* Deliver result here. */
1630 enum expr_mode mode /* Controls behavior. */)
1632 operator_rankT rank = (operator_rankT) rankarg;
1633 segT retval;
1634 expressionS right;
1635 operatorT op_left;
1636 operatorT op_right;
1637 int op_chars;
1639 know (rankarg >= 0);
1641 /* Save the value of dot for the fixup code. */
1642 if (rank == 0)
1643 dot_value = frag_now_fix ();
1645 retval = operand (resultP, mode);
1647 /* operand () gobbles spaces. */
1648 know (*input_line_pointer != ' ');
1650 op_left = operator (&op_chars);
1651 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1653 segT rightseg;
1654 bfd_vma frag_off;
1656 input_line_pointer += op_chars; /* -> after operator. */
1658 rightseg = expr (op_rank[(int) op_left], &right, mode);
1659 if (right.X_op == O_absent)
1661 as_warn (_("missing operand; zero assumed"));
1662 right.X_op = O_constant;
1663 right.X_add_number = 0;
1664 right.X_add_symbol = NULL;
1665 right.X_op_symbol = NULL;
1668 know (*input_line_pointer != ' ');
1670 if (op_left == O_index)
1672 if (*input_line_pointer != ']')
1673 as_bad ("missing right bracket");
1674 else
1676 ++input_line_pointer;
1677 SKIP_WHITESPACE ();
1681 op_right = operator (&op_chars);
1683 know (op_right == O_illegal
1684 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1685 know ((int) op_left >= (int) O_multiply
1686 && (int) op_left <= (int) O_index);
1688 /* input_line_pointer->after right-hand quantity. */
1689 /* left-hand quantity in resultP. */
1690 /* right-hand quantity in right. */
1691 /* operator in op_left. */
1693 if (resultP->X_op == O_big)
1695 if (resultP->X_add_number > 0)
1696 as_warn (_("left operand is a bignum; integer 0 assumed"));
1697 else
1698 as_warn (_("left operand is a float; integer 0 assumed"));
1699 resultP->X_op = O_constant;
1700 resultP->X_add_number = 0;
1701 resultP->X_add_symbol = NULL;
1702 resultP->X_op_symbol = NULL;
1704 if (right.X_op == O_big)
1706 if (right.X_add_number > 0)
1707 as_warn (_("right operand is a bignum; integer 0 assumed"));
1708 else
1709 as_warn (_("right operand is a float; integer 0 assumed"));
1710 right.X_op = O_constant;
1711 right.X_add_number = 0;
1712 right.X_add_symbol = NULL;
1713 right.X_op_symbol = NULL;
1716 /* Optimize common cases. */
1717 #ifdef md_optimize_expr
1718 if (md_optimize_expr (resultP, op_left, &right))
1720 /* Skip. */
1723 else
1724 #endif
1725 if (op_left == O_add && right.X_op == O_constant)
1727 /* X + constant. */
1728 resultP->X_add_number += right.X_add_number;
1730 /* This case comes up in PIC code. */
1731 else if (op_left == O_subtract
1732 && right.X_op == O_symbol
1733 && resultP->X_op == O_symbol
1734 && retval == rightseg
1735 && (SEG_NORMAL (rightseg)
1736 || right.X_add_symbol == resultP->X_add_symbol)
1737 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1738 symbol_get_frag (right.X_add_symbol),
1739 &frag_off))
1741 resultP->X_add_number -= right.X_add_number;
1742 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1743 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1744 - S_GET_VALUE (right.X_add_symbol));
1745 resultP->X_op = O_constant;
1746 resultP->X_add_symbol = 0;
1748 else if (op_left == O_subtract && right.X_op == O_constant)
1750 /* X - constant. */
1751 resultP->X_add_number -= right.X_add_number;
1753 else if (op_left == O_add && resultP->X_op == O_constant)
1755 /* Constant + X. */
1756 resultP->X_op = right.X_op;
1757 resultP->X_add_symbol = right.X_add_symbol;
1758 resultP->X_op_symbol = right.X_op_symbol;
1759 resultP->X_add_number += right.X_add_number;
1760 retval = rightseg;
1762 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1764 /* Constant OP constant. */
1765 offsetT v = right.X_add_number;
1766 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1768 as_warn (_("division by zero"));
1769 v = 1;
1771 switch (op_left)
1773 default: abort ();
1774 case O_multiply: resultP->X_add_number *= v; break;
1775 case O_divide: resultP->X_add_number /= v; break;
1776 case O_modulus: resultP->X_add_number %= v; break;
1777 case O_left_shift: resultP->X_add_number <<= v; break;
1778 case O_right_shift:
1779 /* We always use unsigned shifts, to avoid relying on
1780 characteristics of the compiler used to compile gas. */
1781 resultP->X_add_number =
1782 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1783 break;
1784 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1785 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1786 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1787 case O_bit_and: resultP->X_add_number &= v; break;
1788 /* Constant + constant (O_add) is handled by the
1789 previous if statement for constant + X, so is omitted
1790 here. */
1791 case O_subtract: resultP->X_add_number -= v; break;
1792 case O_eq:
1793 resultP->X_add_number =
1794 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1795 break;
1796 case O_ne:
1797 resultP->X_add_number =
1798 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1799 break;
1800 case O_lt:
1801 resultP->X_add_number =
1802 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1803 break;
1804 case O_le:
1805 resultP->X_add_number =
1806 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1807 break;
1808 case O_ge:
1809 resultP->X_add_number =
1810 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1811 break;
1812 case O_gt:
1813 resultP->X_add_number =
1814 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1815 break;
1816 case O_logical_and:
1817 resultP->X_add_number = resultP->X_add_number && v;
1818 break;
1819 case O_logical_or:
1820 resultP->X_add_number = resultP->X_add_number || v;
1821 break;
1824 else if (resultP->X_op == O_symbol
1825 && right.X_op == O_symbol
1826 && (op_left == O_add
1827 || op_left == O_subtract
1828 || (resultP->X_add_number == 0
1829 && right.X_add_number == 0)))
1831 /* Symbol OP symbol. */
1832 resultP->X_op = op_left;
1833 resultP->X_op_symbol = right.X_add_symbol;
1834 if (op_left == O_add)
1835 resultP->X_add_number += right.X_add_number;
1836 else if (op_left == O_subtract)
1838 resultP->X_add_number -= right.X_add_number;
1839 if (retval == rightseg && SEG_NORMAL (retval))
1841 retval = absolute_section;
1842 rightseg = absolute_section;
1846 else
1848 /* The general case. */
1849 resultP->X_add_symbol = make_expr_symbol (resultP);
1850 resultP->X_op_symbol = make_expr_symbol (&right);
1851 resultP->X_op = op_left;
1852 resultP->X_add_number = 0;
1853 resultP->X_unsigned = 1;
1856 if (retval != rightseg)
1858 if (! SEG_NORMAL (retval))
1860 if (retval != undefined_section || SEG_NORMAL (rightseg))
1861 retval = rightseg;
1863 else if (SEG_NORMAL (rightseg)
1864 #ifdef DIFF_EXPR_OK
1865 && op_left != O_subtract
1866 #endif
1868 as_bad (_("operation combines symbols in different segments"));
1871 op_left = op_right;
1872 } /* While next operator is >= this rank. */
1874 /* The PA port needs this information. */
1875 if (resultP->X_add_symbol)
1876 symbol_mark_used (resultP->X_add_symbol);
1878 if (rank == 0 && mode == expr_evaluate)
1879 resolve_expression (resultP);
1881 return resultP->X_op == O_constant ? absolute_section : retval;
1884 /* Resolve an expression without changing any symbols/sub-expressions
1885 used. */
1888 resolve_expression (expressionS *expressionP)
1890 /* Help out with CSE. */
1891 valueT final_val = expressionP->X_add_number;
1892 symbolS *add_symbol = expressionP->X_add_symbol;
1893 symbolS *op_symbol = expressionP->X_op_symbol;
1894 operatorT op = expressionP->X_op;
1895 valueT left, right;
1896 segT seg_left, seg_right;
1897 fragS *frag_left, *frag_right;
1898 bfd_vma frag_off;
1900 switch (op)
1902 default:
1903 return 0;
1905 case O_constant:
1906 case O_register:
1907 left = 0;
1908 break;
1910 case O_symbol:
1911 case O_symbol_rva:
1912 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1913 return 0;
1915 break;
1917 case O_uminus:
1918 case O_bit_not:
1919 case O_logical_not:
1920 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1921 return 0;
1923 if (seg_left != absolute_section)
1924 return 0;
1926 if (op == O_logical_not)
1927 left = !left;
1928 else if (op == O_uminus)
1929 left = -left;
1930 else
1931 left = ~left;
1932 op = O_constant;
1933 break;
1935 case O_multiply:
1936 case O_divide:
1937 case O_modulus:
1938 case O_left_shift:
1939 case O_right_shift:
1940 case O_bit_inclusive_or:
1941 case O_bit_or_not:
1942 case O_bit_exclusive_or:
1943 case O_bit_and:
1944 case O_add:
1945 case O_subtract:
1946 case O_eq:
1947 case O_ne:
1948 case O_lt:
1949 case O_le:
1950 case O_ge:
1951 case O_gt:
1952 case O_logical_and:
1953 case O_logical_or:
1954 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
1955 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
1956 return 0;
1958 /* Simplify addition or subtraction of a constant by folding the
1959 constant into X_add_number. */
1960 if (op == O_add)
1962 if (seg_right == absolute_section)
1964 final_val += right;
1965 op = O_symbol;
1966 break;
1968 else if (seg_left == absolute_section)
1970 final_val += left;
1971 left = right;
1972 seg_left = seg_right;
1973 add_symbol = op_symbol;
1974 op = O_symbol;
1975 break;
1978 else if (op == O_subtract)
1980 if (seg_right == absolute_section)
1982 final_val -= right;
1983 op = O_symbol;
1984 break;
1988 /* Equality and non-equality tests are permitted on anything.
1989 Subtraction, and other comparison operators are permitted if
1990 both operands are in the same section.
1991 Shifts by constant zero are permitted on anything.
1992 Multiplies, bit-ors, and bit-ands with constant zero are
1993 permitted on anything.
1994 Multiplies and divides by constant one are permitted on
1995 anything.
1996 Binary operations with both operands being the same register
1997 or undefined symbol are permitted if the result doesn't depend
1998 on the input value.
1999 Otherwise, both operands must be absolute. We already handled
2000 the case of addition or subtraction of a constant above. */
2001 frag_off = 0;
2002 if (!(seg_left == absolute_section
2003 && seg_right == absolute_section)
2004 && !(op == O_eq || op == O_ne)
2005 && !((op == O_subtract
2006 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2007 && seg_left == seg_right
2008 && (finalize_syms
2009 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2010 && (seg_left != reg_section || left == right)
2011 && (seg_left != undefined_section || add_symbol == op_symbol)))
2013 if ((seg_left == absolute_section && left == 0)
2014 || (seg_right == absolute_section && right == 0))
2016 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2018 if (seg_right != absolute_section || right != 0)
2020 seg_left = seg_right;
2021 left = right;
2022 add_symbol = op_symbol;
2024 op = O_symbol;
2025 break;
2027 else if (op == O_left_shift || op == O_right_shift)
2029 if (seg_left != absolute_section || left != 0)
2031 op = O_symbol;
2032 break;
2035 else if (op != O_multiply
2036 && op != O_bit_or_not && op != O_bit_and)
2037 return 0;
2039 else if (op == O_multiply
2040 && seg_left == absolute_section && left == 1)
2042 seg_left = seg_right;
2043 left = right;
2044 add_symbol = op_symbol;
2045 op = O_symbol;
2046 break;
2048 else if ((op == O_multiply || op == O_divide)
2049 && seg_right == absolute_section && right == 1)
2051 op = O_symbol;
2052 break;
2054 else if (left != right
2055 || ((seg_left != reg_section || seg_right != reg_section)
2056 && (seg_left != undefined_section
2057 || seg_right != undefined_section
2058 || add_symbol != op_symbol)))
2059 return 0;
2060 else if (op == O_bit_and || op == O_bit_inclusive_or)
2062 op = O_symbol;
2063 break;
2065 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2066 return 0;
2069 right += frag_off / OCTETS_PER_BYTE;
2070 switch (op)
2072 case O_add: left += right; break;
2073 case O_subtract: left -= right; break;
2074 case O_multiply: left *= right; break;
2075 case O_divide:
2076 if (right == 0)
2077 return 0;
2078 left = (offsetT) left / (offsetT) right;
2079 break;
2080 case O_modulus:
2081 if (right == 0)
2082 return 0;
2083 left = (offsetT) left % (offsetT) right;
2084 break;
2085 case O_left_shift: left <<= right; break;
2086 case O_right_shift: left >>= right; break;
2087 case O_bit_inclusive_or: left |= right; break;
2088 case O_bit_or_not: left |= ~right; break;
2089 case O_bit_exclusive_or: left ^= right; break;
2090 case O_bit_and: left &= right; break;
2091 case O_eq:
2092 case O_ne:
2093 left = (left == right
2094 && seg_left == seg_right
2095 && (finalize_syms || frag_left == frag_right)
2096 && (seg_left != undefined_section
2097 || add_symbol == op_symbol)
2098 ? ~ (valueT) 0 : 0);
2099 if (op == O_ne)
2100 left = ~left;
2101 break;
2102 case O_lt:
2103 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2104 break;
2105 case O_le:
2106 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2107 break;
2108 case O_ge:
2109 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2110 break;
2111 case O_gt:
2112 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2113 break;
2114 case O_logical_and: left = left && right; break;
2115 case O_logical_or: left = left || right; break;
2116 default: abort ();
2119 op = O_constant;
2120 break;
2123 if (op == O_symbol)
2125 if (seg_left == absolute_section)
2126 op = O_constant;
2127 else if (seg_left == reg_section && final_val == 0)
2128 op = O_register;
2129 else if (add_symbol != expressionP->X_add_symbol)
2130 final_val += left;
2131 expressionP->X_add_symbol = add_symbol;
2133 expressionP->X_op = op;
2135 if (op == O_constant || op == O_register)
2136 final_val += left;
2137 expressionP->X_add_number = final_val;
2139 return 1;
2142 /* This lives here because it belongs equally in expr.c & read.c.
2143 expr.c is just a branch office read.c anyway, and putting it
2144 here lessens the crowd at read.c.
2146 Assume input_line_pointer is at start of symbol name.
2147 Advance input_line_pointer past symbol name.
2148 Turn that character into a '\0', returning its former value.
2149 This allows a string compare (RMS wants symbol names to be strings)
2150 of the symbol name.
2151 There will always be a char following symbol name, because all good
2152 lines end in end-of-line. */
2154 char
2155 get_symbol_end (void)
2157 char c;
2159 /* We accept \001 in a name in case this is being called with a
2160 constructed string. */
2161 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2163 while (is_part_of_name (c = *input_line_pointer++)
2164 || c == '\001')
2166 if (is_name_ender (c))
2167 c = *input_line_pointer++;
2169 *--input_line_pointer = 0;
2170 return (c);
2173 unsigned int
2174 get_single_number (void)
2176 expressionS exp;
2177 operand (&exp, expr_normal);
2178 return exp.X_add_number;