* Makefile.def: configure-target-boehm-gc depends on
[binutils.git] / gas / expr.c
blob285b4381da24445242c9c5fbe4542c2172fb65c6
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 : expressionP->X_op == O_register
99 ? reg_section
100 : expr_section),
101 0, &zero_address_frag);
102 symbol_set_value_expression (symbolP, expressionP);
104 if (expressionP->X_op == O_constant)
105 resolve_symbol_value (symbolP);
107 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
108 n->sym = symbolP;
109 as_where (&n->file, &n->line);
110 n->next = expr_symbol_lines;
111 expr_symbol_lines = n;
113 return symbolP;
116 /* Return the file and line number for an expr symbol. Return
117 non-zero if something was found, 0 if no information is known for
118 the symbol. */
121 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
123 register struct expr_symbol_line *l;
125 for (l = expr_symbol_lines; l != NULL; l = l->next)
127 if (l->sym == sym)
129 *pfile = l->file;
130 *pline = l->line;
131 return 1;
135 return 0;
138 /* Utilities for building expressions.
139 Since complex expressions are recorded as symbols for use in other
140 expressions these return a symbolS * and not an expressionS *.
141 These explicitly do not take an "add_number" argument. */
142 /* ??? For completeness' sake one might want expr_build_symbol.
143 It would just return its argument. */
145 /* Build an expression for an unsigned constant.
146 The corresponding one for signed constants is missing because
147 there's currently no need for it. One could add an unsigned_p flag
148 but that seems more clumsy. */
150 symbolS *
151 expr_build_uconstant (offsetT value)
153 expressionS e;
155 e.X_op = O_constant;
156 e.X_add_number = value;
157 e.X_unsigned = 1;
158 return make_expr_symbol (&e);
161 /* Build an expression for the current location ('.'). */
163 symbolS *
164 expr_build_dot (void)
166 expressionS e;
168 current_location (&e);
169 return make_expr_symbol (&e);
172 /* Build any floating-point literal here.
173 Also build any bignum literal here. */
175 /* Seems atof_machine can backscan through generic_bignum and hit whatever
176 happens to be loaded before it in memory. And its way too complicated
177 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
178 and never write into the early words, thus they'll always be zero.
179 I hate Dean's floating-point code. Bleh. */
180 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
182 FLONUM_TYPE generic_floating_point_number = {
183 &generic_bignum[6], /* low. (JF: Was 0) */
184 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
185 0, /* leader. */
186 0, /* exponent. */
187 0 /* sign. */
191 static void
192 floating_constant (expressionS *expressionP)
194 /* input_line_pointer -> floating-point constant. */
195 int error_code;
197 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
198 &generic_floating_point_number);
200 if (error_code)
202 if (error_code == ERROR_EXPONENT_OVERFLOW)
204 as_bad (_("bad floating-point constant: exponent overflow"));
206 else
208 as_bad (_("bad floating-point constant: unknown error code=%d"),
209 error_code);
212 expressionP->X_op = O_big;
213 /* input_line_pointer -> just after constant, which may point to
214 whitespace. */
215 expressionP->X_add_number = -1;
218 static valueT
219 generic_bignum_to_int32 (void)
221 valueT number =
222 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
223 | (generic_bignum[0] & LITTLENUM_MASK);
224 number &= 0xffffffff;
225 return number;
228 #ifdef BFD64
229 static valueT
230 generic_bignum_to_int64 (void)
232 valueT number =
233 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
234 << LITTLENUM_NUMBER_OF_BITS)
235 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
236 << LITTLENUM_NUMBER_OF_BITS)
237 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
238 << LITTLENUM_NUMBER_OF_BITS)
239 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
240 return number;
242 #endif
244 static void
245 integer_constant (int radix, expressionS *expressionP)
247 char *start; /* Start of number. */
248 char *suffix = NULL;
249 char c;
250 valueT number; /* Offset or (absolute) value. */
251 short int digit; /* Value of next digit in current radix. */
252 short int maxdig = 0; /* Highest permitted digit value. */
253 int too_many_digits = 0; /* If we see >= this number of. */
254 char *name; /* Points to name of symbol. */
255 symbolS *symbolP; /* Points to symbol. */
257 int small; /* True if fits in 32 bits. */
259 /* May be bignum, or may fit in 32 bits. */
260 /* Most numbers fit into 32 bits, and we want this case to be fast.
261 so we pretend it will fit into 32 bits. If, after making up a 32
262 bit number, we realise that we have scanned more digits than
263 comfortably fit into 32 bits, we re-scan the digits coding them
264 into a bignum. For decimal and octal numbers we are
265 conservative: Some numbers may be assumed bignums when in fact
266 they do fit into 32 bits. Numbers of any radix can have excess
267 leading zeros: We strive to recognise this and cast them back
268 into 32 bits. We must check that the bignum really is more than
269 32 bits, and change it back to a 32-bit number if it fits. The
270 number we are looking for is expected to be positive, but if it
271 fits into 32 bits as an unsigned number, we let it be a 32-bit
272 number. The cavalier approach is for speed in ordinary cases. */
273 /* This has been extended for 64 bits. We blindly assume that if
274 you're compiling in 64-bit mode, the target is a 64-bit machine.
275 This should be cleaned up. */
277 #ifdef BFD64
278 #define valuesize 64
279 #else /* includes non-bfd case, mostly */
280 #define valuesize 32
281 #endif
283 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
285 int flt = 0;
287 /* In MRI mode, the number may have a suffix indicating the
288 radix. For that matter, it might actually be a floating
289 point constant. */
290 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
292 if (*suffix == 'e' || *suffix == 'E')
293 flt = 1;
296 if (suffix == input_line_pointer)
298 radix = 10;
299 suffix = NULL;
301 else
303 c = *--suffix;
304 c = TOUPPER (c);
305 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
306 we distinguish between 'B' and 'b'. This is the case for
307 Z80. */
308 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
309 radix = 2;
310 else if (c == 'D')
311 radix = 10;
312 else if (c == 'O' || c == 'Q')
313 radix = 8;
314 else if (c == 'H')
315 radix = 16;
316 else if (suffix[1] == '.' || c == 'E' || flt)
318 floating_constant (expressionP);
319 return;
321 else
323 radix = 10;
324 suffix = NULL;
329 switch (radix)
331 case 2:
332 maxdig = 2;
333 too_many_digits = valuesize + 1;
334 break;
335 case 8:
336 maxdig = radix = 8;
337 too_many_digits = (valuesize + 2) / 3 + 1;
338 break;
339 case 16:
340 maxdig = radix = 16;
341 too_many_digits = (valuesize + 3) / 4 + 1;
342 break;
343 case 10:
344 maxdig = radix = 10;
345 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
347 #undef valuesize
348 start = input_line_pointer;
349 c = *input_line_pointer++;
350 for (number = 0;
351 (digit = hex_value (c)) < maxdig;
352 c = *input_line_pointer++)
354 number = number * radix + digit;
356 /* c contains character after number. */
357 /* input_line_pointer->char after c. */
358 small = (input_line_pointer - start - 1) < too_many_digits;
360 if (radix == 16 && c == '_')
362 /* This is literal of the form 0x333_0_12345678_1.
363 This example is equivalent to 0x00000333000000001234567800000001. */
365 int num_little_digits = 0;
366 int i;
367 input_line_pointer = start; /* -> 1st digit. */
369 know (LITTLENUM_NUMBER_OF_BITS == 16);
371 for (c = '_'; c == '_'; num_little_digits += 2)
374 /* Convert one 64-bit word. */
375 int ndigit = 0;
376 number = 0;
377 for (c = *input_line_pointer++;
378 (digit = hex_value (c)) < maxdig;
379 c = *(input_line_pointer++))
381 number = number * radix + digit;
382 ndigit++;
385 /* Check for 8 digit per word max. */
386 if (ndigit > 8)
387 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
389 /* Add this chunk to the bignum.
390 Shift things down 2 little digits. */
391 know (LITTLENUM_NUMBER_OF_BITS == 16);
392 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
393 i >= 2;
394 i--)
395 generic_bignum[i] = generic_bignum[i - 2];
397 /* Add the new digits as the least significant new ones. */
398 generic_bignum[0] = number & 0xffffffff;
399 generic_bignum[1] = number >> 16;
402 /* Again, c is char after number, input_line_pointer->after c. */
404 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
405 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
407 assert (num_little_digits >= 4);
409 if (num_little_digits != 8)
410 as_bad (_("a bignum with underscores must have exactly 4 words"));
412 /* We might have some leading zeros. These can be trimmed to give
413 us a change to fit this constant into a small number. */
414 while (generic_bignum[num_little_digits - 1] == 0
415 && num_little_digits > 1)
416 num_little_digits--;
418 if (num_little_digits <= 2)
420 /* will fit into 32 bits. */
421 number = generic_bignum_to_int32 ();
422 small = 1;
424 #ifdef BFD64
425 else if (num_little_digits <= 4)
427 /* Will fit into 64 bits. */
428 number = generic_bignum_to_int64 ();
429 small = 1;
431 #endif
432 else
434 small = 0;
436 /* Number of littlenums in the bignum. */
437 number = num_little_digits;
440 else if (!small)
442 /* We saw a lot of digits. manufacture a bignum the hard way. */
443 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
444 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
445 long carry;
447 leader = generic_bignum;
448 generic_bignum[0] = 0;
449 generic_bignum[1] = 0;
450 generic_bignum[2] = 0;
451 generic_bignum[3] = 0;
452 input_line_pointer = start; /* -> 1st digit. */
453 c = *input_line_pointer++;
454 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
456 for (pointer = generic_bignum; pointer <= leader; pointer++)
458 long work;
460 work = carry + radix * *pointer;
461 *pointer = work & LITTLENUM_MASK;
462 carry = work >> LITTLENUM_NUMBER_OF_BITS;
464 if (carry)
466 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
468 /* Room to grow a longer bignum. */
469 *++leader = carry;
473 /* Again, c is char after number. */
474 /* input_line_pointer -> after c. */
475 know (LITTLENUM_NUMBER_OF_BITS == 16);
476 if (leader < generic_bignum + 2)
478 /* Will fit into 32 bits. */
479 number = generic_bignum_to_int32 ();
480 small = 1;
482 #ifdef BFD64
483 else if (leader < generic_bignum + 4)
485 /* Will fit into 64 bits. */
486 number = generic_bignum_to_int64 ();
487 small = 1;
489 #endif
490 else
492 /* Number of littlenums in the bignum. */
493 number = leader - generic_bignum + 1;
497 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
498 && suffix != NULL
499 && input_line_pointer - 1 == suffix)
500 c = *input_line_pointer++;
502 if (small)
504 /* Here with number, in correct radix. c is the next char.
505 Note that unlike un*x, we allow "011f" "0x9f" to both mean
506 the same as the (conventional) "9f".
507 This is simply easier than checking for strict canonical
508 form. Syntax sux! */
510 if (LOCAL_LABELS_FB && c == 'b')
512 /* Backward ref to local label.
513 Because it is backward, expect it to be defined. */
514 /* Construct a local label. */
515 name = fb_label_name ((int) number, 0);
517 /* Seen before, or symbol is defined: OK. */
518 symbolP = symbol_find (name);
519 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
521 /* Local labels are never absolute. Don't waste time
522 checking absoluteness. */
523 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
525 expressionP->X_op = O_symbol;
526 expressionP->X_add_symbol = symbolP;
528 else
530 /* Either not seen or not defined. */
531 /* @@ Should print out the original string instead of
532 the parsed number. */
533 as_bad (_("backward ref to unknown label \"%d:\""),
534 (int) number);
535 expressionP->X_op = O_constant;
538 expressionP->X_add_number = 0;
539 } /* case 'b' */
540 else if (LOCAL_LABELS_FB && c == 'f')
542 /* Forward reference. Expect symbol to be undefined or
543 unknown. undefined: seen it before. unknown: never seen
544 it before.
546 Construct a local label name, then an undefined symbol.
547 Don't create a xseg frag for it: caller may do that.
548 Just return it as never seen before. */
549 name = fb_label_name ((int) number, 1);
550 symbolP = symbol_find_or_make (name);
551 /* We have no need to check symbol properties. */
552 #ifndef many_segments
553 /* Since "know" puts its arg into a "string", we
554 can't have newlines in the argument. */
555 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
556 #endif
557 expressionP->X_op = O_symbol;
558 expressionP->X_add_symbol = symbolP;
559 expressionP->X_add_number = 0;
560 } /* case 'f' */
561 else if (LOCAL_LABELS_DOLLAR && c == '$')
563 /* If the dollar label is *currently* defined, then this is just
564 another reference to it. If it is not *currently* defined,
565 then this is a fresh instantiation of that number, so create
566 it. */
568 if (dollar_label_defined ((long) number))
570 name = dollar_label_name ((long) number, 0);
571 symbolP = symbol_find (name);
572 know (symbolP != NULL);
574 else
576 name = dollar_label_name ((long) number, 1);
577 symbolP = symbol_find_or_make (name);
580 expressionP->X_op = O_symbol;
581 expressionP->X_add_symbol = symbolP;
582 expressionP->X_add_number = 0;
583 } /* case '$' */
584 else
586 expressionP->X_op = O_constant;
587 expressionP->X_add_number = number;
588 input_line_pointer--; /* Restore following character. */
589 } /* Really just a number. */
591 else
593 /* Not a small number. */
594 expressionP->X_op = O_big;
595 expressionP->X_add_number = number; /* Number of littlenums. */
596 input_line_pointer--; /* -> char following number. */
600 /* Parse an MRI multi character constant. */
602 static void
603 mri_char_constant (expressionS *expressionP)
605 int i;
607 if (*input_line_pointer == '\''
608 && input_line_pointer[1] != '\'')
610 expressionP->X_op = O_constant;
611 expressionP->X_add_number = 0;
612 return;
615 /* In order to get the correct byte ordering, we must build the
616 number in reverse. */
617 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
619 int j;
621 generic_bignum[i] = 0;
622 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
624 if (*input_line_pointer == '\'')
626 if (input_line_pointer[1] != '\'')
627 break;
628 ++input_line_pointer;
630 generic_bignum[i] <<= 8;
631 generic_bignum[i] += *input_line_pointer;
632 ++input_line_pointer;
635 if (i < SIZE_OF_LARGE_NUMBER - 1)
637 /* If there is more than one littlenum, left justify the
638 last one to make it match the earlier ones. If there is
639 only one, we can just use the value directly. */
640 for (; j < CHARS_PER_LITTLENUM; j++)
641 generic_bignum[i] <<= 8;
644 if (*input_line_pointer == '\''
645 && input_line_pointer[1] != '\'')
646 break;
649 if (i < 0)
651 as_bad (_("character constant too large"));
652 i = 0;
655 if (i > 0)
657 int c;
658 int j;
660 c = SIZE_OF_LARGE_NUMBER - i;
661 for (j = 0; j < c; j++)
662 generic_bignum[j] = generic_bignum[i + j];
663 i = c;
666 know (LITTLENUM_NUMBER_OF_BITS == 16);
667 if (i > 2)
669 expressionP->X_op = O_big;
670 expressionP->X_add_number = i;
672 else
674 expressionP->X_op = O_constant;
675 if (i < 2)
676 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
677 else
678 expressionP->X_add_number =
679 (((generic_bignum[1] & LITTLENUM_MASK)
680 << LITTLENUM_NUMBER_OF_BITS)
681 | (generic_bignum[0] & LITTLENUM_MASK));
684 /* Skip the final closing quote. */
685 ++input_line_pointer;
688 /* Return an expression representing the current location. This
689 handles the magic symbol `.'. */
691 static void
692 current_location (expressionS *expressionp)
694 if (now_seg == absolute_section)
696 expressionp->X_op = O_constant;
697 expressionp->X_add_number = abs_section_offset;
699 else
701 expressionp->X_op = O_symbol;
702 expressionp->X_add_symbol = symbol_temp_new_now ();
703 expressionp->X_add_number = 0;
707 /* In: Input_line_pointer points to 1st char of operand, which may
708 be a space.
710 Out: An expressionS.
711 The operand may have been empty: in this case X_op == O_absent.
712 Input_line_pointer->(next non-blank) char after operand. */
714 static segT
715 operand (expressionS *expressionP, enum expr_mode mode)
717 char c;
718 symbolS *symbolP; /* Points to symbol. */
719 char *name; /* Points to name of symbol. */
720 segT segment;
722 /* All integers are regarded as unsigned unless they are negated.
723 This is because the only thing which cares whether a number is
724 unsigned is the code in emit_expr which extends constants into
725 bignums. It should only sign extend negative numbers, so that
726 something like ``.quad 0x80000000'' is not sign extended even
727 though it appears negative if valueT is 32 bits. */
728 expressionP->X_unsigned = 1;
730 /* Digits, assume it is a bignum. */
732 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
733 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
735 if (is_end_of_line[(unsigned char) c])
736 goto eol;
738 switch (c)
740 case '1':
741 case '2':
742 case '3':
743 case '4':
744 case '5':
745 case '6':
746 case '7':
747 case '8':
748 case '9':
749 input_line_pointer--;
751 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
752 ? 0 : 10,
753 expressionP);
754 break;
756 #ifdef LITERAL_PREFIXDOLLAR_HEX
757 case '$':
758 /* $L is the start of a local label, not a hex constant. */
759 if (* input_line_pointer == 'L')
760 goto isname;
761 integer_constant (16, expressionP);
762 break;
763 #endif
765 #ifdef LITERAL_PREFIXPERCENT_BIN
766 case '%':
767 integer_constant (2, expressionP);
768 break;
769 #endif
771 case '0':
772 /* Non-decimal radix. */
774 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
776 char *s;
778 /* Check for a hex or float constant. */
779 for (s = input_line_pointer; hex_p (*s); s++)
781 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
783 --input_line_pointer;
784 integer_constant (0, expressionP);
785 break;
788 c = *input_line_pointer;
789 switch (c)
791 case 'o':
792 case 'O':
793 case 'q':
794 case 'Q':
795 case '8':
796 case '9':
797 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
799 integer_constant (0, expressionP);
800 break;
802 /* Fall through. */
803 default:
804 default_case:
805 if (c && strchr (FLT_CHARS, c))
807 input_line_pointer++;
808 floating_constant (expressionP);
809 expressionP->X_add_number = - TOLOWER (c);
811 else
813 /* The string was only zero. */
814 expressionP->X_op = O_constant;
815 expressionP->X_add_number = 0;
818 break;
820 case 'x':
821 case 'X':
822 if (flag_m68k_mri)
823 goto default_case;
824 input_line_pointer++;
825 integer_constant (16, expressionP);
826 break;
828 case 'b':
829 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
831 /* This code used to check for '+' and '-' here, and, in
832 some conditions, fall through to call
833 integer_constant. However, that didn't make sense,
834 as integer_constant only accepts digits. */
835 /* Some of our code elsewhere does permit digits greater
836 than the expected base; for consistency, do the same
837 here. */
838 if (input_line_pointer[1] < '0'
839 || input_line_pointer[1] > '9')
841 /* Parse this as a back reference to label 0. */
842 input_line_pointer--;
843 integer_constant (10, expressionP);
844 break;
846 /* Otherwise, parse this as a binary number. */
848 /* Fall through. */
849 case 'B':
850 input_line_pointer++;
851 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
852 goto default_case;
853 integer_constant (2, expressionP);
854 break;
856 case '0':
857 case '1':
858 case '2':
859 case '3':
860 case '4':
861 case '5':
862 case '6':
863 case '7':
864 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
865 ? 0 : 8,
866 expressionP);
867 break;
869 case 'f':
870 if (LOCAL_LABELS_FB)
872 /* If it says "0f" and it could possibly be a floating point
873 number, make it one. Otherwise, make it a local label,
874 and try to deal with parsing the rest later. */
875 if (!input_line_pointer[1]
876 || (is_end_of_line[0xff & input_line_pointer[1]])
877 || strchr (FLT_CHARS, 'f') == NULL)
878 goto is_0f_label;
880 char *cp = input_line_pointer + 1;
881 int r = atof_generic (&cp, ".", EXP_CHARS,
882 &generic_floating_point_number);
883 switch (r)
885 case 0:
886 case ERROR_EXPONENT_OVERFLOW:
887 if (*cp == 'f' || *cp == 'b')
888 /* Looks like a difference expression. */
889 goto is_0f_label;
890 else if (cp == input_line_pointer + 1)
891 /* No characters has been accepted -- looks like
892 end of operand. */
893 goto is_0f_label;
894 else
895 goto is_0f_float;
896 default:
897 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
902 /* Okay, now we've sorted it out. We resume at one of these
903 two labels, depending on what we've decided we're probably
904 looking at. */
905 is_0f_label:
906 input_line_pointer--;
907 integer_constant (10, expressionP);
908 break;
910 is_0f_float:
911 /* Fall through. */
915 case 'd':
916 case 'D':
917 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
919 integer_constant (0, expressionP);
920 break;
922 /* Fall through. */
923 case 'F':
924 case 'r':
925 case 'e':
926 case 'E':
927 case 'g':
928 case 'G':
929 input_line_pointer++;
930 floating_constant (expressionP);
931 expressionP->X_add_number = - TOLOWER (c);
932 break;
934 case '$':
935 if (LOCAL_LABELS_DOLLAR)
937 integer_constant (10, expressionP);
938 break;
940 else
941 goto default_case;
944 break;
946 case '(':
947 #ifndef NEED_INDEX_OPERATOR
948 case '[':
949 #endif
950 /* Didn't begin with digit & not a name. */
951 if (mode != expr_defer)
952 segment = expression (expressionP);
953 else
954 segment = deferred_expression (expressionP);
955 /* expression () will pass trailing whitespace. */
956 if ((c == '(' && *input_line_pointer != ')')
957 || (c == '[' && *input_line_pointer != ']'))
958 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
959 else
960 input_line_pointer++;
961 SKIP_WHITESPACE ();
962 /* Here with input_line_pointer -> char after "(...)". */
963 return segment;
965 #ifdef TC_M68K
966 case 'E':
967 if (! flag_m68k_mri || *input_line_pointer != '\'')
968 goto de_fault;
969 as_bad (_("EBCDIC constants are not supported"));
970 /* Fall through. */
971 case 'A':
972 if (! flag_m68k_mri || *input_line_pointer != '\'')
973 goto de_fault;
974 ++input_line_pointer;
975 /* Fall through. */
976 #endif
977 case '\'':
978 if (! flag_m68k_mri)
980 /* Warning: to conform to other people's assemblers NO
981 ESCAPEMENT is permitted for a single quote. The next
982 character, parity errors and all, is taken as the value
983 of the operand. VERY KINKY. */
984 expressionP->X_op = O_constant;
985 expressionP->X_add_number = *input_line_pointer++;
986 break;
989 mri_char_constant (expressionP);
990 break;
992 #ifdef TC_M68K
993 case '"':
994 /* Double quote is the bitwise not operator in MRI mode. */
995 if (! flag_m68k_mri)
996 goto de_fault;
997 /* Fall through. */
998 #endif
999 case '~':
1000 /* '~' is permitted to start a label on the Delta. */
1001 if (is_name_beginner (c))
1002 goto isname;
1003 case '!':
1004 case '-':
1005 case '+':
1007 operand (expressionP, mode);
1008 if (expressionP->X_op == O_constant)
1010 /* input_line_pointer -> char after operand. */
1011 if (c == '-')
1013 expressionP->X_add_number = - expressionP->X_add_number;
1014 /* Notice: '-' may overflow: no warning is given.
1015 This is compatible with other people's
1016 assemblers. Sigh. */
1017 expressionP->X_unsigned = 0;
1019 else if (c == '~' || c == '"')
1020 expressionP->X_add_number = ~ expressionP->X_add_number;
1021 else if (c == '!')
1022 expressionP->X_add_number = ! expressionP->X_add_number;
1024 else if (expressionP->X_op == O_big
1025 && expressionP->X_add_number <= 0
1026 && c == '-'
1027 && (generic_floating_point_number.sign == '+'
1028 || generic_floating_point_number.sign == 'P'))
1030 /* Negative flonum (eg, -1.000e0). */
1031 if (generic_floating_point_number.sign == '+')
1032 generic_floating_point_number.sign = '-';
1033 else
1034 generic_floating_point_number.sign = 'N';
1036 else if (expressionP->X_op == O_big
1037 && expressionP->X_add_number > 0)
1039 int i;
1041 if (c == '~' || c == '-')
1043 for (i = 0; i < expressionP->X_add_number; ++i)
1044 generic_bignum[i] = ~generic_bignum[i];
1045 if (c == '-')
1046 for (i = 0; i < expressionP->X_add_number; ++i)
1048 generic_bignum[i] += 1;
1049 if (generic_bignum[i])
1050 break;
1053 else if (c == '!')
1055 int nonzero = 0;
1056 for (i = 0; i < expressionP->X_add_number; ++i)
1058 if (generic_bignum[i])
1059 nonzero = 1;
1060 generic_bignum[i] = 0;
1062 generic_bignum[0] = nonzero;
1065 else if (expressionP->X_op != O_illegal
1066 && expressionP->X_op != O_absent)
1068 if (c != '+')
1070 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1071 if (c == '-')
1072 expressionP->X_op = O_uminus;
1073 else if (c == '~' || c == '"')
1074 expressionP->X_op = O_bit_not;
1075 else
1076 expressionP->X_op = O_logical_not;
1077 expressionP->X_add_number = 0;
1080 else
1081 as_warn (_("Unary operator %c ignored because bad operand follows"),
1084 break;
1086 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1087 case '$':
1088 /* '$' is the program counter when in MRI mode, or when
1089 DOLLAR_DOT is defined. */
1090 #ifndef DOLLAR_DOT
1091 if (! flag_m68k_mri)
1092 goto de_fault;
1093 #endif
1094 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1096 /* In MRI mode and on Z80, '$' is also used as the prefix
1097 for a hexadecimal constant. */
1098 integer_constant (16, expressionP);
1099 break;
1102 if (is_part_of_name (*input_line_pointer))
1103 goto isname;
1105 current_location (expressionP);
1106 break;
1107 #endif
1109 case '.':
1110 if (!is_part_of_name (*input_line_pointer))
1112 current_location (expressionP);
1113 break;
1115 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1116 && ! is_part_of_name (input_line_pointer[8]))
1117 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1118 && ! is_part_of_name (input_line_pointer[7])))
1120 int start;
1122 start = (input_line_pointer[1] == 't'
1123 || input_line_pointer[1] == 'T');
1124 input_line_pointer += start ? 8 : 7;
1125 SKIP_WHITESPACE ();
1126 if (*input_line_pointer != '(')
1127 as_bad (_("syntax error in .startof. or .sizeof."));
1128 else
1130 char *buf;
1132 ++input_line_pointer;
1133 SKIP_WHITESPACE ();
1134 name = input_line_pointer;
1135 c = get_symbol_end ();
1137 buf = (char *) xmalloc (strlen (name) + 10);
1138 if (start)
1139 sprintf (buf, ".startof.%s", name);
1140 else
1141 sprintf (buf, ".sizeof.%s", name);
1142 symbolP = symbol_make (buf);
1143 free (buf);
1145 expressionP->X_op = O_symbol;
1146 expressionP->X_add_symbol = symbolP;
1147 expressionP->X_add_number = 0;
1149 *input_line_pointer = c;
1150 SKIP_WHITESPACE ();
1151 if (*input_line_pointer != ')')
1152 as_bad (_("syntax error in .startof. or .sizeof."));
1153 else
1154 ++input_line_pointer;
1156 break;
1158 else
1160 goto isname;
1163 case ',':
1164 eol:
1165 /* Can't imagine any other kind of operand. */
1166 expressionP->X_op = O_absent;
1167 input_line_pointer--;
1168 break;
1170 #ifdef TC_M68K
1171 case '%':
1172 if (! flag_m68k_mri)
1173 goto de_fault;
1174 integer_constant (2, expressionP);
1175 break;
1177 case '@':
1178 if (! flag_m68k_mri)
1179 goto de_fault;
1180 integer_constant (8, expressionP);
1181 break;
1183 case ':':
1184 if (! flag_m68k_mri)
1185 goto de_fault;
1187 /* In MRI mode, this is a floating point constant represented
1188 using hexadecimal digits. */
1190 ++input_line_pointer;
1191 integer_constant (16, expressionP);
1192 break;
1194 case '*':
1195 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1196 goto de_fault;
1198 current_location (expressionP);
1199 break;
1200 #endif
1202 default:
1203 #ifdef TC_M68K
1204 de_fault:
1205 #endif
1206 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1208 /* Identifier begins here.
1209 This is kludged for speed, so code is repeated. */
1210 isname:
1211 name = --input_line_pointer;
1212 c = get_symbol_end ();
1214 #ifdef md_parse_name
1215 /* This is a hook for the backend to parse certain names
1216 specially in certain contexts. If a name always has a
1217 specific value, it can often be handled by simply
1218 entering it in the symbol table. */
1219 if (md_parse_name (name, expressionP, mode, &c))
1221 *input_line_pointer = c;
1222 break;
1224 #endif
1226 #ifdef TC_I960
1227 /* The MRI i960 assembler permits
1228 lda sizeof code,g13
1229 FIXME: This should use md_parse_name. */
1230 if (flag_mri
1231 && (strcasecmp (name, "sizeof") == 0
1232 || strcasecmp (name, "startof") == 0))
1234 int start;
1235 char *buf;
1237 start = (name[1] == 't'
1238 || name[1] == 'T');
1240 *input_line_pointer = c;
1241 SKIP_WHITESPACE ();
1243 name = input_line_pointer;
1244 c = get_symbol_end ();
1246 buf = (char *) xmalloc (strlen (name) + 10);
1247 if (start)
1248 sprintf (buf, ".startof.%s", name);
1249 else
1250 sprintf (buf, ".sizeof.%s", name);
1251 symbolP = symbol_make (buf);
1252 free (buf);
1254 expressionP->X_op = O_symbol;
1255 expressionP->X_add_symbol = symbolP;
1256 expressionP->X_add_number = 0;
1258 *input_line_pointer = c;
1259 SKIP_WHITESPACE ();
1261 break;
1263 #endif
1265 symbolP = symbol_find_or_make (name);
1267 /* If we have an absolute symbol or a reg, then we know its
1268 value now. */
1269 segment = S_GET_SEGMENT (symbolP);
1270 if (mode != expr_defer && segment == absolute_section)
1272 expressionP->X_op = O_constant;
1273 expressionP->X_add_number = S_GET_VALUE (symbolP);
1275 else if (mode != expr_defer && segment == reg_section)
1277 expressionP->X_op = O_register;
1278 expressionP->X_add_number = S_GET_VALUE (symbolP);
1280 else
1282 expressionP->X_op = O_symbol;
1283 expressionP->X_add_symbol = symbolP;
1284 expressionP->X_add_number = 0;
1286 *input_line_pointer = c;
1288 else
1290 /* Let the target try to parse it. Success is indicated by changing
1291 the X_op field to something other than O_absent and pointing
1292 input_line_pointer past the expression. If it can't parse the
1293 expression, X_op and input_line_pointer should be unchanged. */
1294 expressionP->X_op = O_absent;
1295 --input_line_pointer;
1296 md_operand (expressionP);
1297 if (expressionP->X_op == O_absent)
1299 ++input_line_pointer;
1300 as_bad (_("bad expression"));
1301 expressionP->X_op = O_constant;
1302 expressionP->X_add_number = 0;
1305 break;
1308 /* It is more 'efficient' to clean up the expressionS when they are
1309 created. Doing it here saves lines of code. */
1310 clean_up_expression (expressionP);
1311 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1312 know (*input_line_pointer != ' ');
1314 /* The PA port needs this information. */
1315 if (expressionP->X_add_symbol)
1316 symbol_mark_used (expressionP->X_add_symbol);
1318 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1319 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1321 switch (expressionP->X_op)
1323 default:
1324 return absolute_section;
1325 case O_symbol:
1326 return S_GET_SEGMENT (expressionP->X_add_symbol);
1327 case O_register:
1328 return reg_section;
1332 /* Internal. Simplify a struct expression for use by expr (). */
1334 /* In: address of an expressionS.
1335 The X_op field of the expressionS may only take certain values.
1336 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1338 Out: expressionS may have been modified:
1339 Unused fields zeroed to help expr (). */
1341 static void
1342 clean_up_expression (expressionS *expressionP)
1344 switch (expressionP->X_op)
1346 case O_illegal:
1347 case O_absent:
1348 expressionP->X_add_number = 0;
1349 /* Fall through. */
1350 case O_big:
1351 case O_constant:
1352 case O_register:
1353 expressionP->X_add_symbol = NULL;
1354 /* Fall through. */
1355 case O_symbol:
1356 case O_uminus:
1357 case O_bit_not:
1358 expressionP->X_op_symbol = NULL;
1359 break;
1360 default:
1361 break;
1365 /* Expression parser. */
1367 /* We allow an empty expression, and just assume (absolute,0) silently.
1368 Unary operators and parenthetical expressions are treated as operands.
1369 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1371 We used to do an aho/ullman shift-reduce parser, but the logic got so
1372 warped that I flushed it and wrote a recursive-descent parser instead.
1373 Now things are stable, would anybody like to write a fast parser?
1374 Most expressions are either register (which does not even reach here)
1375 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1376 So I guess it doesn't really matter how inefficient more complex expressions
1377 are parsed.
1379 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1380 Also, we have consumed any leading or trailing spaces (operand does that)
1381 and done all intervening operators.
1383 This returns the segment of the result, which will be
1384 absolute_section or the segment of a symbol. */
1386 #undef __
1387 #define __ O_illegal
1388 #ifndef O_SINGLE_EQ
1389 #define O_SINGLE_EQ O_illegal
1390 #endif
1392 /* Maps ASCII -> operators. */
1393 static const operatorT op_encoding[256] = {
1394 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1395 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1397 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1398 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1399 __, __, __, __, __, __, __, __,
1400 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1401 __, __, __, __, __, __, __, __,
1402 __, __, __, __, __, __, __, __,
1403 __, __, __, __, __, __, __, __,
1404 __, __, __,
1405 #ifdef NEED_INDEX_OPERATOR
1406 O_index,
1407 #else
1409 #endif
1410 __, __, O_bit_exclusive_or, __,
1411 __, __, __, __, __, __, __, __,
1412 __, __, __, __, __, __, __, __,
1413 __, __, __, __, __, __, __, __,
1414 __, __, __, __, O_bit_inclusive_or, __, __, __,
1416 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1417 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1418 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1419 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1420 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1421 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1422 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1423 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1426 /* Rank Examples
1427 0 operand, (expression)
1428 1 ||
1429 2 &&
1430 3 == <> < <= >= >
1431 4 + -
1432 5 used for * / % in MRI mode
1433 6 & ^ ! |
1434 7 * / % << >>
1435 8 unary - unary ~
1437 static operator_rankT op_rank[] = {
1438 0, /* O_illegal */
1439 0, /* O_absent */
1440 0, /* O_constant */
1441 0, /* O_symbol */
1442 0, /* O_symbol_rva */
1443 0, /* O_register */
1444 0, /* O_big */
1445 9, /* O_uminus */
1446 9, /* O_bit_not */
1447 9, /* O_logical_not */
1448 8, /* O_multiply */
1449 8, /* O_divide */
1450 8, /* O_modulus */
1451 8, /* O_left_shift */
1452 8, /* O_right_shift */
1453 7, /* O_bit_inclusive_or */
1454 7, /* O_bit_or_not */
1455 7, /* O_bit_exclusive_or */
1456 7, /* O_bit_and */
1457 5, /* O_add */
1458 5, /* O_subtract */
1459 4, /* O_eq */
1460 4, /* O_ne */
1461 4, /* O_lt */
1462 4, /* O_le */
1463 4, /* O_ge */
1464 4, /* O_gt */
1465 3, /* O_logical_and */
1466 2, /* O_logical_or */
1467 1, /* O_index */
1468 0, /* O_md1 */
1469 0, /* O_md2 */
1470 0, /* O_md3 */
1471 0, /* O_md4 */
1472 0, /* O_md5 */
1473 0, /* O_md6 */
1474 0, /* O_md7 */
1475 0, /* O_md8 */
1476 0, /* O_md9 */
1477 0, /* O_md10 */
1478 0, /* O_md11 */
1479 0, /* O_md12 */
1480 0, /* O_md13 */
1481 0, /* O_md14 */
1482 0, /* O_md15 */
1483 0, /* O_md16 */
1486 /* Unfortunately, in MRI mode for the m68k, multiplication and
1487 division have lower precedence than the bit wise operators. This
1488 function sets the operator precedences correctly for the current
1489 mode. Also, MRI uses a different bit_not operator, and this fixes
1490 that as well. */
1492 #define STANDARD_MUL_PRECEDENCE 8
1493 #define MRI_MUL_PRECEDENCE 6
1495 void
1496 expr_set_precedence (void)
1498 if (flag_m68k_mri)
1500 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1501 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1502 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1504 else
1506 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1507 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1508 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1512 /* Initialize the expression parser. */
1514 void
1515 expr_begin (void)
1517 expr_set_precedence ();
1519 /* Verify that X_op field is wide enough. */
1521 expressionS e;
1522 e.X_op = O_max;
1523 assert (e.X_op == O_max);
1527 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1528 sets NUM_CHARS to the number of characters in the operator.
1529 Does not advance INPUT_LINE_POINTER. */
1531 static inline operatorT
1532 operator (int *num_chars)
1534 int c;
1535 operatorT ret;
1537 c = *input_line_pointer & 0xff;
1538 *num_chars = 1;
1540 if (is_end_of_line[c])
1541 return O_illegal;
1543 switch (c)
1545 default:
1546 return op_encoding[c];
1548 case '+':
1549 case '-':
1550 return op_encoding[c];
1552 case '<':
1553 switch (input_line_pointer[1])
1555 default:
1556 return op_encoding[c];
1557 case '<':
1558 ret = O_left_shift;
1559 break;
1560 case '>':
1561 ret = O_ne;
1562 break;
1563 case '=':
1564 ret = O_le;
1565 break;
1567 *num_chars = 2;
1568 return ret;
1570 case '=':
1571 if (input_line_pointer[1] != '=')
1572 return op_encoding[c];
1574 *num_chars = 2;
1575 return O_eq;
1577 case '>':
1578 switch (input_line_pointer[1])
1580 default:
1581 return op_encoding[c];
1582 case '>':
1583 ret = O_right_shift;
1584 break;
1585 case '=':
1586 ret = O_ge;
1587 break;
1589 *num_chars = 2;
1590 return ret;
1592 case '!':
1593 switch (input_line_pointer[1])
1595 case '!':
1596 /* We accept !! as equivalent to ^ for MRI compatibility. */
1597 *num_chars = 2;
1598 return O_bit_exclusive_or;
1599 case '=':
1600 /* We accept != as equivalent to <>. */
1601 *num_chars = 2;
1602 return O_ne;
1603 default:
1604 if (flag_m68k_mri)
1605 return O_bit_inclusive_or;
1606 return op_encoding[c];
1609 case '|':
1610 if (input_line_pointer[1] != '|')
1611 return op_encoding[c];
1613 *num_chars = 2;
1614 return O_logical_or;
1616 case '&':
1617 if (input_line_pointer[1] != '&')
1618 return op_encoding[c];
1620 *num_chars = 2;
1621 return O_logical_and;
1624 /* NOTREACHED */
1627 /* Parse an expression. */
1629 segT
1630 expr (int rankarg, /* Larger # is higher rank. */
1631 expressionS *resultP, /* Deliver result here. */
1632 enum expr_mode mode /* Controls behavior. */)
1634 operator_rankT rank = (operator_rankT) rankarg;
1635 segT retval;
1636 expressionS right;
1637 operatorT op_left;
1638 operatorT op_right;
1639 int op_chars;
1641 know (rankarg >= 0);
1643 /* Save the value of dot for the fixup code. */
1644 if (rank == 0)
1645 dot_value = frag_now_fix ();
1647 retval = operand (resultP, mode);
1649 /* operand () gobbles spaces. */
1650 know (*input_line_pointer != ' ');
1652 op_left = operator (&op_chars);
1653 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1655 segT rightseg;
1656 bfd_vma frag_off;
1658 input_line_pointer += op_chars; /* -> after operator. */
1660 rightseg = expr (op_rank[(int) op_left], &right, mode);
1661 if (right.X_op == O_absent)
1663 as_warn (_("missing operand; zero assumed"));
1664 right.X_op = O_constant;
1665 right.X_add_number = 0;
1666 right.X_add_symbol = NULL;
1667 right.X_op_symbol = NULL;
1670 know (*input_line_pointer != ' ');
1672 if (op_left == O_index)
1674 if (*input_line_pointer != ']')
1675 as_bad ("missing right bracket");
1676 else
1678 ++input_line_pointer;
1679 SKIP_WHITESPACE ();
1683 op_right = operator (&op_chars);
1685 know (op_right == O_illegal
1686 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1687 know ((int) op_left >= (int) O_multiply
1688 && (int) op_left <= (int) O_index);
1690 /* input_line_pointer->after right-hand quantity. */
1691 /* left-hand quantity in resultP. */
1692 /* right-hand quantity in right. */
1693 /* operator in op_left. */
1695 if (resultP->X_op == O_big)
1697 if (resultP->X_add_number > 0)
1698 as_warn (_("left operand is a bignum; integer 0 assumed"));
1699 else
1700 as_warn (_("left operand is a float; integer 0 assumed"));
1701 resultP->X_op = O_constant;
1702 resultP->X_add_number = 0;
1703 resultP->X_add_symbol = NULL;
1704 resultP->X_op_symbol = NULL;
1706 if (right.X_op == O_big)
1708 if (right.X_add_number > 0)
1709 as_warn (_("right operand is a bignum; integer 0 assumed"));
1710 else
1711 as_warn (_("right operand is a float; integer 0 assumed"));
1712 right.X_op = O_constant;
1713 right.X_add_number = 0;
1714 right.X_add_symbol = NULL;
1715 right.X_op_symbol = NULL;
1718 /* Optimize common cases. */
1719 #ifdef md_optimize_expr
1720 if (md_optimize_expr (resultP, op_left, &right))
1722 /* Skip. */
1725 else
1726 #endif
1727 #ifndef md_register_arithmetic
1728 # define md_register_arithmetic 1
1729 #endif
1730 if (op_left == O_add && right.X_op == O_constant
1731 && (md_register_arithmetic || resultP->X_op != O_register))
1733 /* X + constant. */
1734 resultP->X_add_number += right.X_add_number;
1736 /* This case comes up in PIC code. */
1737 else if (op_left == O_subtract
1738 && right.X_op == O_symbol
1739 && resultP->X_op == O_symbol
1740 && retval == rightseg
1741 #ifdef md_allow_local_subtract
1742 && md_allow_local_subtract (resultP, & right, rightseg)
1743 #endif
1744 && (SEG_NORMAL (rightseg)
1745 || right.X_add_symbol == resultP->X_add_symbol)
1746 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1747 symbol_get_frag (right.X_add_symbol),
1748 &frag_off))
1750 resultP->X_add_number -= right.X_add_number;
1751 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1752 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1753 - S_GET_VALUE (right.X_add_symbol));
1754 resultP->X_op = O_constant;
1755 resultP->X_add_symbol = 0;
1757 else if (op_left == O_subtract && right.X_op == O_constant
1758 && (md_register_arithmetic || resultP->X_op != O_register))
1760 /* X - constant. */
1761 resultP->X_add_number -= right.X_add_number;
1763 else if (op_left == O_add && resultP->X_op == O_constant
1764 && (md_register_arithmetic || right.X_op != O_register))
1766 /* Constant + X. */
1767 resultP->X_op = right.X_op;
1768 resultP->X_add_symbol = right.X_add_symbol;
1769 resultP->X_op_symbol = right.X_op_symbol;
1770 resultP->X_add_number += right.X_add_number;
1771 retval = rightseg;
1773 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1775 /* Constant OP constant. */
1776 offsetT v = right.X_add_number;
1777 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1779 as_warn (_("division by zero"));
1780 v = 1;
1782 switch (op_left)
1784 default: abort ();
1785 case O_multiply: resultP->X_add_number *= v; break;
1786 case O_divide: resultP->X_add_number /= v; break;
1787 case O_modulus: resultP->X_add_number %= v; break;
1788 case O_left_shift: resultP->X_add_number <<= v; break;
1789 case O_right_shift:
1790 /* We always use unsigned shifts, to avoid relying on
1791 characteristics of the compiler used to compile gas. */
1792 resultP->X_add_number =
1793 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1794 break;
1795 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1796 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1797 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1798 case O_bit_and: resultP->X_add_number &= v; break;
1799 /* Constant + constant (O_add) is handled by the
1800 previous if statement for constant + X, so is omitted
1801 here. */
1802 case O_subtract: resultP->X_add_number -= v; break;
1803 case O_eq:
1804 resultP->X_add_number =
1805 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1806 break;
1807 case O_ne:
1808 resultP->X_add_number =
1809 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1810 break;
1811 case O_lt:
1812 resultP->X_add_number =
1813 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1814 break;
1815 case O_le:
1816 resultP->X_add_number =
1817 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1818 break;
1819 case O_ge:
1820 resultP->X_add_number =
1821 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1822 break;
1823 case O_gt:
1824 resultP->X_add_number =
1825 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1826 break;
1827 case O_logical_and:
1828 resultP->X_add_number = resultP->X_add_number && v;
1829 break;
1830 case O_logical_or:
1831 resultP->X_add_number = resultP->X_add_number || v;
1832 break;
1835 else if (resultP->X_op == O_symbol
1836 && right.X_op == O_symbol
1837 && (op_left == O_add
1838 || op_left == O_subtract
1839 || (resultP->X_add_number == 0
1840 && right.X_add_number == 0)))
1842 /* Symbol OP symbol. */
1843 resultP->X_op = op_left;
1844 resultP->X_op_symbol = right.X_add_symbol;
1845 if (op_left == O_add)
1846 resultP->X_add_number += right.X_add_number;
1847 else if (op_left == O_subtract)
1849 resultP->X_add_number -= right.X_add_number;
1850 if (retval == rightseg && SEG_NORMAL (retval))
1852 retval = absolute_section;
1853 rightseg = absolute_section;
1857 else
1859 /* The general case. */
1860 resultP->X_add_symbol = make_expr_symbol (resultP);
1861 resultP->X_op_symbol = make_expr_symbol (&right);
1862 resultP->X_op = op_left;
1863 resultP->X_add_number = 0;
1864 resultP->X_unsigned = 1;
1867 if (retval != rightseg)
1869 if (! SEG_NORMAL (retval))
1871 if (retval != undefined_section || SEG_NORMAL (rightseg))
1872 retval = rightseg;
1874 else if (SEG_NORMAL (rightseg)
1875 #ifdef DIFF_EXPR_OK
1876 && op_left != O_subtract
1877 #endif
1879 as_bad (_("operation combines symbols in different segments"));
1882 op_left = op_right;
1883 } /* While next operator is >= this rank. */
1885 /* The PA port needs this information. */
1886 if (resultP->X_add_symbol)
1887 symbol_mark_used (resultP->X_add_symbol);
1889 if (rank == 0 && mode == expr_evaluate)
1890 resolve_expression (resultP);
1892 return resultP->X_op == O_constant ? absolute_section : retval;
1895 /* Resolve an expression without changing any symbols/sub-expressions
1896 used. */
1899 resolve_expression (expressionS *expressionP)
1901 /* Help out with CSE. */
1902 valueT final_val = expressionP->X_add_number;
1903 symbolS *add_symbol = expressionP->X_add_symbol;
1904 symbolS *op_symbol = expressionP->X_op_symbol;
1905 operatorT op = expressionP->X_op;
1906 valueT left, right;
1907 segT seg_left, seg_right;
1908 fragS *frag_left, *frag_right;
1909 bfd_vma frag_off;
1911 switch (op)
1913 default:
1914 return 0;
1916 case O_constant:
1917 case O_register:
1918 left = 0;
1919 break;
1921 case O_symbol:
1922 case O_symbol_rva:
1923 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1924 return 0;
1926 break;
1928 case O_uminus:
1929 case O_bit_not:
1930 case O_logical_not:
1931 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1932 return 0;
1934 if (seg_left != absolute_section)
1935 return 0;
1937 if (op == O_logical_not)
1938 left = !left;
1939 else if (op == O_uminus)
1940 left = -left;
1941 else
1942 left = ~left;
1943 op = O_constant;
1944 break;
1946 case O_multiply:
1947 case O_divide:
1948 case O_modulus:
1949 case O_left_shift:
1950 case O_right_shift:
1951 case O_bit_inclusive_or:
1952 case O_bit_or_not:
1953 case O_bit_exclusive_or:
1954 case O_bit_and:
1955 case O_add:
1956 case O_subtract:
1957 case O_eq:
1958 case O_ne:
1959 case O_lt:
1960 case O_le:
1961 case O_ge:
1962 case O_gt:
1963 case O_logical_and:
1964 case O_logical_or:
1965 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
1966 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
1967 return 0;
1969 /* Simplify addition or subtraction of a constant by folding the
1970 constant into X_add_number. */
1971 if (op == O_add)
1973 if (seg_right == absolute_section)
1975 final_val += right;
1976 op = O_symbol;
1977 break;
1979 else if (seg_left == absolute_section)
1981 final_val += left;
1982 left = right;
1983 seg_left = seg_right;
1984 add_symbol = op_symbol;
1985 op = O_symbol;
1986 break;
1989 else if (op == O_subtract)
1991 if (seg_right == absolute_section)
1993 final_val -= right;
1994 op = O_symbol;
1995 break;
1999 /* Equality and non-equality tests are permitted on anything.
2000 Subtraction, and other comparison operators are permitted if
2001 both operands are in the same section.
2002 Shifts by constant zero are permitted on anything.
2003 Multiplies, bit-ors, and bit-ands with constant zero are
2004 permitted on anything.
2005 Multiplies and divides by constant one are permitted on
2006 anything.
2007 Binary operations with both operands being the same register
2008 or undefined symbol are permitted if the result doesn't depend
2009 on the input value.
2010 Otherwise, both operands must be absolute. We already handled
2011 the case of addition or subtraction of a constant above. */
2012 frag_off = 0;
2013 if (!(seg_left == absolute_section
2014 && seg_right == absolute_section)
2015 && !(op == O_eq || op == O_ne)
2016 && !((op == O_subtract
2017 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2018 && seg_left == seg_right
2019 && (finalize_syms
2020 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2021 && (seg_left != reg_section || left == right)
2022 && (seg_left != undefined_section || add_symbol == op_symbol)))
2024 if ((seg_left == absolute_section && left == 0)
2025 || (seg_right == absolute_section && right == 0))
2027 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2029 if (seg_right != absolute_section || right != 0)
2031 seg_left = seg_right;
2032 left = right;
2033 add_symbol = op_symbol;
2035 op = O_symbol;
2036 break;
2038 else if (op == O_left_shift || op == O_right_shift)
2040 if (seg_left != absolute_section || left != 0)
2042 op = O_symbol;
2043 break;
2046 else if (op != O_multiply
2047 && op != O_bit_or_not && op != O_bit_and)
2048 return 0;
2050 else if (op == O_multiply
2051 && seg_left == absolute_section && left == 1)
2053 seg_left = seg_right;
2054 left = right;
2055 add_symbol = op_symbol;
2056 op = O_symbol;
2057 break;
2059 else if ((op == O_multiply || op == O_divide)
2060 && seg_right == absolute_section && right == 1)
2062 op = O_symbol;
2063 break;
2065 else if (left != right
2066 || ((seg_left != reg_section || seg_right != reg_section)
2067 && (seg_left != undefined_section
2068 || seg_right != undefined_section
2069 || add_symbol != op_symbol)))
2070 return 0;
2071 else if (op == O_bit_and || op == O_bit_inclusive_or)
2073 op = O_symbol;
2074 break;
2076 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2077 return 0;
2080 right += frag_off / OCTETS_PER_BYTE;
2081 switch (op)
2083 case O_add: left += right; break;
2084 case O_subtract: left -= right; break;
2085 case O_multiply: left *= right; break;
2086 case O_divide:
2087 if (right == 0)
2088 return 0;
2089 left = (offsetT) left / (offsetT) right;
2090 break;
2091 case O_modulus:
2092 if (right == 0)
2093 return 0;
2094 left = (offsetT) left % (offsetT) right;
2095 break;
2096 case O_left_shift: left <<= right; break;
2097 case O_right_shift: left >>= right; break;
2098 case O_bit_inclusive_or: left |= right; break;
2099 case O_bit_or_not: left |= ~right; break;
2100 case O_bit_exclusive_or: left ^= right; break;
2101 case O_bit_and: left &= right; break;
2102 case O_eq:
2103 case O_ne:
2104 left = (left == right
2105 && seg_left == seg_right
2106 && (finalize_syms || frag_left == frag_right)
2107 && (seg_left != undefined_section
2108 || add_symbol == op_symbol)
2109 ? ~ (valueT) 0 : 0);
2110 if (op == O_ne)
2111 left = ~left;
2112 break;
2113 case O_lt:
2114 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2115 break;
2116 case O_le:
2117 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2118 break;
2119 case O_ge:
2120 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2121 break;
2122 case O_gt:
2123 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2124 break;
2125 case O_logical_and: left = left && right; break;
2126 case O_logical_or: left = left || right; break;
2127 default: abort ();
2130 op = O_constant;
2131 break;
2134 if (op == O_symbol)
2136 if (seg_left == absolute_section)
2137 op = O_constant;
2138 else if (seg_left == reg_section && final_val == 0)
2139 op = O_register;
2140 else if (add_symbol != expressionP->X_add_symbol)
2141 final_val += left;
2142 expressionP->X_add_symbol = add_symbol;
2144 expressionP->X_op = op;
2146 if (op == O_constant || op == O_register)
2147 final_val += left;
2148 expressionP->X_add_number = final_val;
2150 return 1;
2153 /* This lives here because it belongs equally in expr.c & read.c.
2154 expr.c is just a branch office read.c anyway, and putting it
2155 here lessens the crowd at read.c.
2157 Assume input_line_pointer is at start of symbol name.
2158 Advance input_line_pointer past symbol name.
2159 Turn that character into a '\0', returning its former value.
2160 This allows a string compare (RMS wants symbol names to be strings)
2161 of the symbol name.
2162 There will always be a char following symbol name, because all good
2163 lines end in end-of-line. */
2165 char
2166 get_symbol_end (void)
2168 char c;
2170 /* We accept \001 in a name in case this is being called with a
2171 constructed string. */
2172 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2174 while (is_part_of_name (c = *input_line_pointer++)
2175 || c == '\001')
2177 if (is_name_ender (c))
2178 c = *input_line_pointer++;
2180 *--input_line_pointer = 0;
2181 return (c);
2184 unsigned int
2185 get_single_number (void)
2187 expressionS exp;
2188 operand (&exp, expr_normal);
2189 return exp.X_add_number;