2009-06-15 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gas / expr.c
bloba75be2c1ea850ccef73954866ec1a5ef6a88ad08
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 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #endif
37 #ifndef CHAR_BIT
38 #define CHAR_BIT 8
39 #endif
41 static void floating_constant (expressionS * expressionP);
42 static valueT generic_bignum_to_int32 (void);
43 #ifdef BFD64
44 static valueT generic_bignum_to_int64 (void);
45 #endif
46 static void integer_constant (int radix, expressionS * expressionP);
47 static void mri_char_constant (expressionS *);
48 static void current_location (expressionS *);
49 static void clean_up_expression (expressionS * expressionP);
50 static segT operand (expressionS *, enum expr_mode);
51 static operatorT operator (int *);
53 extern const char EXP_CHARS[], FLT_CHARS[];
55 /* We keep a mapping of expression symbols to file positions, so that
56 we can provide better error messages. */
58 struct expr_symbol_line {
59 struct expr_symbol_line *next;
60 symbolS *sym;
61 char *file;
62 unsigned int line;
65 static struct expr_symbol_line *expr_symbol_lines;
67 /* Build a dummy symbol to hold a complex expression. This is how we
68 build expressions up out of other expressions. The symbol is put
69 into the fake section expr_section. */
71 symbolS *
72 make_expr_symbol (expressionS *expressionP)
74 expressionS zero;
75 symbolS *symbolP;
76 struct expr_symbol_line *n;
78 if (expressionP->X_op == O_symbol
79 && expressionP->X_add_number == 0)
80 return expressionP->X_add_symbol;
82 if (expressionP->X_op == O_big)
84 /* This won't work, because the actual value is stored in
85 generic_floating_point_number or generic_bignum, and we are
86 going to lose it if we haven't already. */
87 if (expressionP->X_add_number > 0)
88 as_bad (_("bignum invalid"));
89 else
90 as_bad (_("floating point number invalid"));
91 zero.X_op = O_constant;
92 zero.X_add_number = 0;
93 zero.X_unsigned = 0;
94 clean_up_expression (&zero);
95 expressionP = &zero;
98 /* Putting constant symbols in absolute_section rather than
99 expr_section is convenient for the old a.out code, for which
100 S_GET_SEGMENT does not always retrieve the value put in by
101 S_SET_SEGMENT. */
102 symbolP = symbol_create (FAKE_LABEL_NAME,
103 (expressionP->X_op == O_constant
104 ? absolute_section
105 : expressionP->X_op == O_register
106 ? reg_section
107 : expr_section),
108 0, &zero_address_frag);
109 symbol_set_value_expression (symbolP, expressionP);
111 if (expressionP->X_op == O_constant)
112 resolve_symbol_value (symbolP);
114 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
115 n->sym = symbolP;
116 as_where (&n->file, &n->line);
117 n->next = expr_symbol_lines;
118 expr_symbol_lines = n;
120 return symbolP;
123 /* Return the file and line number for an expr symbol. Return
124 non-zero if something was found, 0 if no information is known for
125 the symbol. */
128 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
130 register struct expr_symbol_line *l;
132 for (l = expr_symbol_lines; l != NULL; l = l->next)
134 if (l->sym == sym)
136 *pfile = l->file;
137 *pline = l->line;
138 return 1;
142 return 0;
145 /* Utilities for building expressions.
146 Since complex expressions are recorded as symbols for use in other
147 expressions these return a symbolS * and not an expressionS *.
148 These explicitly do not take an "add_number" argument. */
149 /* ??? For completeness' sake one might want expr_build_symbol.
150 It would just return its argument. */
152 /* Build an expression for an unsigned constant.
153 The corresponding one for signed constants is missing because
154 there's currently no need for it. One could add an unsigned_p flag
155 but that seems more clumsy. */
157 symbolS *
158 expr_build_uconstant (offsetT value)
160 expressionS e;
162 e.X_op = O_constant;
163 e.X_add_number = value;
164 e.X_unsigned = 1;
165 return make_expr_symbol (&e);
168 /* Build an expression for the current location ('.'). */
170 symbolS *
171 expr_build_dot (void)
173 expressionS e;
175 current_location (&e);
176 return make_expr_symbol (&e);
179 /* Build any floating-point literal here.
180 Also build any bignum literal here. */
182 /* Seems atof_machine can backscan through generic_bignum and hit whatever
183 happens to be loaded before it in memory. And its way too complicated
184 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
185 and never write into the early words, thus they'll always be zero.
186 I hate Dean's floating-point code. Bleh. */
187 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
189 FLONUM_TYPE generic_floating_point_number = {
190 &generic_bignum[6], /* low. (JF: Was 0) */
191 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
192 0, /* leader. */
193 0, /* exponent. */
194 0 /* sign. */
198 static void
199 floating_constant (expressionS *expressionP)
201 /* input_line_pointer -> floating-point constant. */
202 int error_code;
204 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
205 &generic_floating_point_number);
207 if (error_code)
209 if (error_code == ERROR_EXPONENT_OVERFLOW)
211 as_bad (_("bad floating-point constant: exponent overflow"));
213 else
215 as_bad (_("bad floating-point constant: unknown error code=%d"),
216 error_code);
219 expressionP->X_op = O_big;
220 /* input_line_pointer -> just after constant, which may point to
221 whitespace. */
222 expressionP->X_add_number = -1;
225 static valueT
226 generic_bignum_to_int32 (void)
228 valueT number =
229 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
230 | (generic_bignum[0] & LITTLENUM_MASK);
231 number &= 0xffffffff;
232 return number;
235 #ifdef BFD64
236 static valueT
237 generic_bignum_to_int64 (void)
239 valueT number =
240 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
241 << LITTLENUM_NUMBER_OF_BITS)
242 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
243 << LITTLENUM_NUMBER_OF_BITS)
244 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
245 << LITTLENUM_NUMBER_OF_BITS)
246 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
247 return number;
249 #endif
251 static void
252 integer_constant (int radix, expressionS *expressionP)
254 char *start; /* Start of number. */
255 char *suffix = NULL;
256 char c;
257 valueT number; /* Offset or (absolute) value. */
258 short int digit; /* Value of next digit in current radix. */
259 short int maxdig = 0; /* Highest permitted digit value. */
260 int too_many_digits = 0; /* If we see >= this number of. */
261 char *name; /* Points to name of symbol. */
262 symbolS *symbolP; /* Points to symbol. */
264 int small; /* True if fits in 32 bits. */
266 /* May be bignum, or may fit in 32 bits. */
267 /* Most numbers fit into 32 bits, and we want this case to be fast.
268 so we pretend it will fit into 32 bits. If, after making up a 32
269 bit number, we realise that we have scanned more digits than
270 comfortably fit into 32 bits, we re-scan the digits coding them
271 into a bignum. For decimal and octal numbers we are
272 conservative: Some numbers may be assumed bignums when in fact
273 they do fit into 32 bits. Numbers of any radix can have excess
274 leading zeros: We strive to recognise this and cast them back
275 into 32 bits. We must check that the bignum really is more than
276 32 bits, and change it back to a 32-bit number if it fits. The
277 number we are looking for is expected to be positive, but if it
278 fits into 32 bits as an unsigned number, we let it be a 32-bit
279 number. The cavalier approach is for speed in ordinary cases. */
280 /* This has been extended for 64 bits. We blindly assume that if
281 you're compiling in 64-bit mode, the target is a 64-bit machine.
282 This should be cleaned up. */
284 #ifdef BFD64
285 #define valuesize 64
286 #else /* includes non-bfd case, mostly */
287 #define valuesize 32
288 #endif
290 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
292 int flt = 0;
294 /* In MRI mode, the number may have a suffix indicating the
295 radix. For that matter, it might actually be a floating
296 point constant. */
297 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
299 if (*suffix == 'e' || *suffix == 'E')
300 flt = 1;
303 if (suffix == input_line_pointer)
305 radix = 10;
306 suffix = NULL;
308 else
310 c = *--suffix;
311 c = TOUPPER (c);
312 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
313 we distinguish between 'B' and 'b'. This is the case for
314 Z80. */
315 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
316 radix = 2;
317 else if (c == 'D')
318 radix = 10;
319 else if (c == 'O' || c == 'Q')
320 radix = 8;
321 else if (c == 'H')
322 radix = 16;
323 else if (suffix[1] == '.' || c == 'E' || flt)
325 floating_constant (expressionP);
326 return;
328 else
330 radix = 10;
331 suffix = NULL;
336 switch (radix)
338 case 2:
339 maxdig = 2;
340 too_many_digits = valuesize + 1;
341 break;
342 case 8:
343 maxdig = radix = 8;
344 too_many_digits = (valuesize + 2) / 3 + 1;
345 break;
346 case 16:
347 maxdig = radix = 16;
348 too_many_digits = (valuesize + 3) / 4 + 1;
349 break;
350 case 10:
351 maxdig = radix = 10;
352 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
354 #undef valuesize
355 start = input_line_pointer;
356 c = *input_line_pointer++;
357 for (number = 0;
358 (digit = hex_value (c)) < maxdig;
359 c = *input_line_pointer++)
361 number = number * radix + digit;
363 /* c contains character after number. */
364 /* input_line_pointer->char after c. */
365 small = (input_line_pointer - start - 1) < too_many_digits;
367 if (radix == 16 && c == '_')
369 /* This is literal of the form 0x333_0_12345678_1.
370 This example is equivalent to 0x00000333000000001234567800000001. */
372 int num_little_digits = 0;
373 int i;
374 input_line_pointer = start; /* -> 1st digit. */
376 know (LITTLENUM_NUMBER_OF_BITS == 16);
378 for (c = '_'; c == '_'; num_little_digits += 2)
381 /* Convert one 64-bit word. */
382 int ndigit = 0;
383 number = 0;
384 for (c = *input_line_pointer++;
385 (digit = hex_value (c)) < maxdig;
386 c = *(input_line_pointer++))
388 number = number * radix + digit;
389 ndigit++;
392 /* Check for 8 digit per word max. */
393 if (ndigit > 8)
394 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
396 /* Add this chunk to the bignum.
397 Shift things down 2 little digits. */
398 know (LITTLENUM_NUMBER_OF_BITS == 16);
399 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
400 i >= 2;
401 i--)
402 generic_bignum[i] = generic_bignum[i - 2];
404 /* Add the new digits as the least significant new ones. */
405 generic_bignum[0] = number & 0xffffffff;
406 generic_bignum[1] = number >> 16;
409 /* Again, c is char after number, input_line_pointer->after c. */
411 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
412 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
414 assert (num_little_digits >= 4);
416 if (num_little_digits != 8)
417 as_bad (_("a bignum with underscores must have exactly 4 words"));
419 /* We might have some leading zeros. These can be trimmed to give
420 us a change to fit this constant into a small number. */
421 while (generic_bignum[num_little_digits - 1] == 0
422 && num_little_digits > 1)
423 num_little_digits--;
425 if (num_little_digits <= 2)
427 /* will fit into 32 bits. */
428 number = generic_bignum_to_int32 ();
429 small = 1;
431 #ifdef BFD64
432 else if (num_little_digits <= 4)
434 /* Will fit into 64 bits. */
435 number = generic_bignum_to_int64 ();
436 small = 1;
438 #endif
439 else
441 small = 0;
443 /* Number of littlenums in the bignum. */
444 number = num_little_digits;
447 else if (!small)
449 /* We saw a lot of digits. manufacture a bignum the hard way. */
450 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
451 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
452 long carry;
454 leader = generic_bignum;
455 generic_bignum[0] = 0;
456 generic_bignum[1] = 0;
457 generic_bignum[2] = 0;
458 generic_bignum[3] = 0;
459 input_line_pointer = start; /* -> 1st digit. */
460 c = *input_line_pointer++;
461 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
463 for (pointer = generic_bignum; pointer <= leader; pointer++)
465 long work;
467 work = carry + radix * *pointer;
468 *pointer = work & LITTLENUM_MASK;
469 carry = work >> LITTLENUM_NUMBER_OF_BITS;
471 if (carry)
473 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
475 /* Room to grow a longer bignum. */
476 *++leader = carry;
480 /* Again, c is char after number. */
481 /* input_line_pointer -> after c. */
482 know (LITTLENUM_NUMBER_OF_BITS == 16);
483 if (leader < generic_bignum + 2)
485 /* Will fit into 32 bits. */
486 number = generic_bignum_to_int32 ();
487 small = 1;
489 #ifdef BFD64
490 else if (leader < generic_bignum + 4)
492 /* Will fit into 64 bits. */
493 number = generic_bignum_to_int64 ();
494 small = 1;
496 #endif
497 else
499 /* Number of littlenums in the bignum. */
500 number = leader - generic_bignum + 1;
504 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
505 && suffix != NULL
506 && input_line_pointer - 1 == suffix)
507 c = *input_line_pointer++;
509 if (small)
511 /* Here with number, in correct radix. c is the next char.
512 Note that unlike un*x, we allow "011f" "0x9f" to both mean
513 the same as the (conventional) "9f".
514 This is simply easier than checking for strict canonical
515 form. Syntax sux! */
517 if (LOCAL_LABELS_FB && c == 'b')
519 /* Backward ref to local label.
520 Because it is backward, expect it to be defined. */
521 /* Construct a local label. */
522 name = fb_label_name ((int) number, 0);
524 /* Seen before, or symbol is defined: OK. */
525 symbolP = symbol_find (name);
526 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
528 /* Local labels are never absolute. Don't waste time
529 checking absoluteness. */
530 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
532 expressionP->X_op = O_symbol;
533 expressionP->X_add_symbol = symbolP;
535 else
537 /* Either not seen or not defined. */
538 /* @@ Should print out the original string instead of
539 the parsed number. */
540 as_bad (_("backward ref to unknown label \"%d:\""),
541 (int) number);
542 expressionP->X_op = O_constant;
545 expressionP->X_add_number = 0;
546 } /* case 'b' */
547 else if (LOCAL_LABELS_FB && c == 'f')
549 /* Forward reference. Expect symbol to be undefined or
550 unknown. undefined: seen it before. unknown: never seen
551 it before.
553 Construct a local label name, then an undefined symbol.
554 Don't create a xseg frag for it: caller may do that.
555 Just return it as never seen before. */
556 name = fb_label_name ((int) number, 1);
557 symbolP = symbol_find_or_make (name);
558 /* We have no need to check symbol properties. */
559 #ifndef many_segments
560 /* Since "know" puts its arg into a "string", we
561 can't have newlines in the argument. */
562 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
563 #endif
564 expressionP->X_op = O_symbol;
565 expressionP->X_add_symbol = symbolP;
566 expressionP->X_add_number = 0;
567 } /* case 'f' */
568 else if (LOCAL_LABELS_DOLLAR && c == '$')
570 /* If the dollar label is *currently* defined, then this is just
571 another reference to it. If it is not *currently* defined,
572 then this is a fresh instantiation of that number, so create
573 it. */
575 if (dollar_label_defined ((long) number))
577 name = dollar_label_name ((long) number, 0);
578 symbolP = symbol_find (name);
579 know (symbolP != NULL);
581 else
583 name = dollar_label_name ((long) number, 1);
584 symbolP = symbol_find_or_make (name);
587 expressionP->X_op = O_symbol;
588 expressionP->X_add_symbol = symbolP;
589 expressionP->X_add_number = 0;
590 } /* case '$' */
591 else
593 expressionP->X_op = O_constant;
594 expressionP->X_add_number = number;
595 input_line_pointer--; /* Restore following character. */
596 } /* Really just a number. */
598 else
600 /* Not a small number. */
601 expressionP->X_op = O_big;
602 expressionP->X_add_number = number; /* Number of littlenums. */
603 input_line_pointer--; /* -> char following number. */
607 /* Parse an MRI multi character constant. */
609 static void
610 mri_char_constant (expressionS *expressionP)
612 int i;
614 if (*input_line_pointer == '\''
615 && input_line_pointer[1] != '\'')
617 expressionP->X_op = O_constant;
618 expressionP->X_add_number = 0;
619 return;
622 /* In order to get the correct byte ordering, we must build the
623 number in reverse. */
624 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
626 int j;
628 generic_bignum[i] = 0;
629 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
631 if (*input_line_pointer == '\'')
633 if (input_line_pointer[1] != '\'')
634 break;
635 ++input_line_pointer;
637 generic_bignum[i] <<= 8;
638 generic_bignum[i] += *input_line_pointer;
639 ++input_line_pointer;
642 if (i < SIZE_OF_LARGE_NUMBER - 1)
644 /* If there is more than one littlenum, left justify the
645 last one to make it match the earlier ones. If there is
646 only one, we can just use the value directly. */
647 for (; j < CHARS_PER_LITTLENUM; j++)
648 generic_bignum[i] <<= 8;
651 if (*input_line_pointer == '\''
652 && input_line_pointer[1] != '\'')
653 break;
656 if (i < 0)
658 as_bad (_("character constant too large"));
659 i = 0;
662 if (i > 0)
664 int c;
665 int j;
667 c = SIZE_OF_LARGE_NUMBER - i;
668 for (j = 0; j < c; j++)
669 generic_bignum[j] = generic_bignum[i + j];
670 i = c;
673 know (LITTLENUM_NUMBER_OF_BITS == 16);
674 if (i > 2)
676 expressionP->X_op = O_big;
677 expressionP->X_add_number = i;
679 else
681 expressionP->X_op = O_constant;
682 if (i < 2)
683 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
684 else
685 expressionP->X_add_number =
686 (((generic_bignum[1] & LITTLENUM_MASK)
687 << LITTLENUM_NUMBER_OF_BITS)
688 | (generic_bignum[0] & LITTLENUM_MASK));
691 /* Skip the final closing quote. */
692 ++input_line_pointer;
695 /* Return an expression representing the current location. This
696 handles the magic symbol `.'. */
698 static void
699 current_location (expressionS *expressionp)
701 if (now_seg == absolute_section)
703 expressionp->X_op = O_constant;
704 expressionp->X_add_number = abs_section_offset;
706 else
708 expressionp->X_op = O_symbol;
709 expressionp->X_add_symbol = symbol_temp_new_now ();
710 expressionp->X_add_number = 0;
714 /* In: Input_line_pointer points to 1st char of operand, which may
715 be a space.
717 Out: An expressionS.
718 The operand may have been empty: in this case X_op == O_absent.
719 Input_line_pointer->(next non-blank) char after operand. */
721 static segT
722 operand (expressionS *expressionP, enum expr_mode mode)
724 char c;
725 symbolS *symbolP; /* Points to symbol. */
726 char *name; /* Points to name of symbol. */
727 segT segment;
729 /* All integers are regarded as unsigned unless they are negated.
730 This is because the only thing which cares whether a number is
731 unsigned is the code in emit_expr which extends constants into
732 bignums. It should only sign extend negative numbers, so that
733 something like ``.quad 0x80000000'' is not sign extended even
734 though it appears negative if valueT is 32 bits. */
735 expressionP->X_unsigned = 1;
737 /* Digits, assume it is a bignum. */
739 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
740 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
742 if (is_end_of_line[(unsigned char) c])
743 goto eol;
745 switch (c)
747 case '1':
748 case '2':
749 case '3':
750 case '4':
751 case '5':
752 case '6':
753 case '7':
754 case '8':
755 case '9':
756 input_line_pointer--;
758 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
759 ? 0 : 10,
760 expressionP);
761 break;
763 #ifdef LITERAL_PREFIXDOLLAR_HEX
764 case '$':
765 /* $L is the start of a local label, not a hex constant. */
766 if (* input_line_pointer == 'L')
767 goto isname;
768 integer_constant (16, expressionP);
769 break;
770 #endif
772 #ifdef LITERAL_PREFIXPERCENT_BIN
773 case '%':
774 integer_constant (2, expressionP);
775 break;
776 #endif
778 case '0':
779 /* Non-decimal radix. */
781 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
783 char *s;
785 /* Check for a hex or float constant. */
786 for (s = input_line_pointer; hex_p (*s); s++)
788 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
790 --input_line_pointer;
791 integer_constant (0, expressionP);
792 break;
795 c = *input_line_pointer;
796 switch (c)
798 case 'o':
799 case 'O':
800 case 'q':
801 case 'Q':
802 case '8':
803 case '9':
804 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
806 integer_constant (0, expressionP);
807 break;
809 /* Fall through. */
810 default:
811 default_case:
812 if (c && strchr (FLT_CHARS, c))
814 input_line_pointer++;
815 floating_constant (expressionP);
816 expressionP->X_add_number = - TOLOWER (c);
818 else
820 /* The string was only zero. */
821 expressionP->X_op = O_constant;
822 expressionP->X_add_number = 0;
825 break;
827 case 'x':
828 case 'X':
829 if (flag_m68k_mri)
830 goto default_case;
831 input_line_pointer++;
832 integer_constant (16, expressionP);
833 break;
835 case 'b':
836 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
838 /* This code used to check for '+' and '-' here, and, in
839 some conditions, fall through to call
840 integer_constant. However, that didn't make sense,
841 as integer_constant only accepts digits. */
842 /* Some of our code elsewhere does permit digits greater
843 than the expected base; for consistency, do the same
844 here. */
845 if (input_line_pointer[1] < '0'
846 || input_line_pointer[1] > '9')
848 /* Parse this as a back reference to label 0. */
849 input_line_pointer--;
850 integer_constant (10, expressionP);
851 break;
853 /* Otherwise, parse this as a binary number. */
855 /* Fall through. */
856 case 'B':
857 input_line_pointer++;
858 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
859 goto default_case;
860 integer_constant (2, expressionP);
861 break;
863 case '0':
864 case '1':
865 case '2':
866 case '3':
867 case '4':
868 case '5':
869 case '6':
870 case '7':
871 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
872 ? 0 : 8,
873 expressionP);
874 break;
876 case 'f':
877 if (LOCAL_LABELS_FB)
879 /* If it says "0f" and it could possibly be a floating point
880 number, make it one. Otherwise, make it a local label,
881 and try to deal with parsing the rest later. */
882 if (!input_line_pointer[1]
883 || (is_end_of_line[0xff & input_line_pointer[1]])
884 || strchr (FLT_CHARS, 'f') == NULL)
885 goto is_0f_label;
887 char *cp = input_line_pointer + 1;
888 int r = atof_generic (&cp, ".", EXP_CHARS,
889 &generic_floating_point_number);
890 switch (r)
892 case 0:
893 case ERROR_EXPONENT_OVERFLOW:
894 if (*cp == 'f' || *cp == 'b')
895 /* Looks like a difference expression. */
896 goto is_0f_label;
897 else if (cp == input_line_pointer + 1)
898 /* No characters has been accepted -- looks like
899 end of operand. */
900 goto is_0f_label;
901 else
902 goto is_0f_float;
903 default:
904 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
909 /* Okay, now we've sorted it out. We resume at one of these
910 two labels, depending on what we've decided we're probably
911 looking at. */
912 is_0f_label:
913 input_line_pointer--;
914 integer_constant (10, expressionP);
915 break;
917 is_0f_float:
918 /* Fall through. */
922 case 'd':
923 case 'D':
924 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
926 integer_constant (0, expressionP);
927 break;
929 /* Fall through. */
930 case 'F':
931 case 'r':
932 case 'e':
933 case 'E':
934 case 'g':
935 case 'G':
936 input_line_pointer++;
937 floating_constant (expressionP);
938 expressionP->X_add_number = - TOLOWER (c);
939 break;
941 case '$':
942 if (LOCAL_LABELS_DOLLAR)
944 integer_constant (10, expressionP);
945 break;
947 else
948 goto default_case;
951 break;
953 #ifndef NEED_INDEX_OPERATOR
954 case '[':
955 # ifdef md_need_index_operator
956 if (md_need_index_operator())
957 goto de_fault;
958 # endif
959 /* FALLTHROUGH */
960 #endif
961 case '(':
962 /* Didn't begin with digit & not a name. */
963 if (mode != expr_defer)
964 segment = expression (expressionP);
965 else
966 segment = deferred_expression (expressionP);
967 /* expression () will pass trailing whitespace. */
968 if ((c == '(' && *input_line_pointer != ')')
969 || (c == '[' && *input_line_pointer != ']'))
970 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
971 else
972 input_line_pointer++;
973 SKIP_WHITESPACE ();
974 /* Here with input_line_pointer -> char after "(...)". */
975 return segment;
977 #ifdef TC_M68K
978 case 'E':
979 if (! flag_m68k_mri || *input_line_pointer != '\'')
980 goto de_fault;
981 as_bad (_("EBCDIC constants are not supported"));
982 /* Fall through. */
983 case 'A':
984 if (! flag_m68k_mri || *input_line_pointer != '\'')
985 goto de_fault;
986 ++input_line_pointer;
987 /* Fall through. */
988 #endif
989 case '\'':
990 if (! flag_m68k_mri)
992 /* Warning: to conform to other people's assemblers NO
993 ESCAPEMENT is permitted for a single quote. The next
994 character, parity errors and all, is taken as the value
995 of the operand. VERY KINKY. */
996 expressionP->X_op = O_constant;
997 expressionP->X_add_number = *input_line_pointer++;
998 break;
1001 mri_char_constant (expressionP);
1002 break;
1004 #ifdef TC_M68K
1005 case '"':
1006 /* Double quote is the bitwise not operator in MRI mode. */
1007 if (! flag_m68k_mri)
1008 goto de_fault;
1009 /* Fall through. */
1010 #endif
1011 case '~':
1012 /* '~' is permitted to start a label on the Delta. */
1013 if (is_name_beginner (c))
1014 goto isname;
1015 case '!':
1016 case '-':
1017 case '+':
1019 #ifdef md_operator
1020 unary:
1021 #endif
1022 operand (expressionP, mode);
1023 if (expressionP->X_op == O_constant)
1025 /* input_line_pointer -> char after operand. */
1026 if (c == '-')
1028 expressionP->X_add_number = - expressionP->X_add_number;
1029 /* Notice: '-' may overflow: no warning is given.
1030 This is compatible with other people's
1031 assemblers. Sigh. */
1032 expressionP->X_unsigned = 0;
1034 else if (c == '~' || c == '"')
1035 expressionP->X_add_number = ~ expressionP->X_add_number;
1036 else if (c == '!')
1037 expressionP->X_add_number = ! expressionP->X_add_number;
1039 else if (expressionP->X_op == O_big
1040 && expressionP->X_add_number <= 0
1041 && c == '-'
1042 && (generic_floating_point_number.sign == '+'
1043 || generic_floating_point_number.sign == 'P'))
1045 /* Negative flonum (eg, -1.000e0). */
1046 if (generic_floating_point_number.sign == '+')
1047 generic_floating_point_number.sign = '-';
1048 else
1049 generic_floating_point_number.sign = 'N';
1051 else if (expressionP->X_op == O_big
1052 && expressionP->X_add_number > 0)
1054 int i;
1056 if (c == '~' || c == '-')
1058 for (i = 0; i < expressionP->X_add_number; ++i)
1059 generic_bignum[i] = ~generic_bignum[i];
1060 if (c == '-')
1061 for (i = 0; i < expressionP->X_add_number; ++i)
1063 generic_bignum[i] += 1;
1064 if (generic_bignum[i])
1065 break;
1068 else if (c == '!')
1070 int nonzero = 0;
1071 for (i = 0; i < expressionP->X_add_number; ++i)
1073 if (generic_bignum[i])
1074 nonzero = 1;
1075 generic_bignum[i] = 0;
1077 generic_bignum[0] = nonzero;
1080 else if (expressionP->X_op != O_illegal
1081 && expressionP->X_op != O_absent)
1083 if (c != '+')
1085 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1086 if (c == '-')
1087 expressionP->X_op = O_uminus;
1088 else if (c == '~' || c == '"')
1089 expressionP->X_op = O_bit_not;
1090 else
1091 expressionP->X_op = O_logical_not;
1092 expressionP->X_add_number = 0;
1095 else
1096 as_warn (_("Unary operator %c ignored because bad operand follows"),
1099 break;
1101 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1102 case '$':
1103 /* '$' is the program counter when in MRI mode, or when
1104 DOLLAR_DOT is defined. */
1105 #ifndef DOLLAR_DOT
1106 if (! flag_m68k_mri)
1107 goto de_fault;
1108 #endif
1109 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1111 /* In MRI mode and on Z80, '$' is also used as the prefix
1112 for a hexadecimal constant. */
1113 integer_constant (16, expressionP);
1114 break;
1117 if (is_part_of_name (*input_line_pointer))
1118 goto isname;
1120 current_location (expressionP);
1121 break;
1122 #endif
1124 case '.':
1125 if (!is_part_of_name (*input_line_pointer))
1127 current_location (expressionP);
1128 break;
1130 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1131 && ! is_part_of_name (input_line_pointer[8]))
1132 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1133 && ! is_part_of_name (input_line_pointer[7])))
1135 int start;
1137 start = (input_line_pointer[1] == 't'
1138 || input_line_pointer[1] == 'T');
1139 input_line_pointer += start ? 8 : 7;
1140 SKIP_WHITESPACE ();
1141 if (*input_line_pointer != '(')
1142 as_bad (_("syntax error in .startof. or .sizeof."));
1143 else
1145 char *buf;
1147 ++input_line_pointer;
1148 SKIP_WHITESPACE ();
1149 name = input_line_pointer;
1150 c = get_symbol_end ();
1152 buf = (char *) xmalloc (strlen (name) + 10);
1153 if (start)
1154 sprintf (buf, ".startof.%s", name);
1155 else
1156 sprintf (buf, ".sizeof.%s", name);
1157 symbolP = symbol_make (buf);
1158 free (buf);
1160 expressionP->X_op = O_symbol;
1161 expressionP->X_add_symbol = symbolP;
1162 expressionP->X_add_number = 0;
1164 *input_line_pointer = c;
1165 SKIP_WHITESPACE ();
1166 if (*input_line_pointer != ')')
1167 as_bad (_("syntax error in .startof. or .sizeof."));
1168 else
1169 ++input_line_pointer;
1171 break;
1173 else
1175 goto isname;
1178 case ',':
1179 eol:
1180 /* Can't imagine any other kind of operand. */
1181 expressionP->X_op = O_absent;
1182 input_line_pointer--;
1183 break;
1185 #ifdef TC_M68K
1186 case '%':
1187 if (! flag_m68k_mri)
1188 goto de_fault;
1189 integer_constant (2, expressionP);
1190 break;
1192 case '@':
1193 if (! flag_m68k_mri)
1194 goto de_fault;
1195 integer_constant (8, expressionP);
1196 break;
1198 case ':':
1199 if (! flag_m68k_mri)
1200 goto de_fault;
1202 /* In MRI mode, this is a floating point constant represented
1203 using hexadecimal digits. */
1205 ++input_line_pointer;
1206 integer_constant (16, expressionP);
1207 break;
1209 case '*':
1210 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1211 goto de_fault;
1213 current_location (expressionP);
1214 break;
1215 #endif
1217 default:
1218 #if defined(md_need_index_operator) || defined(TC_M68K)
1219 de_fault:
1220 #endif
1221 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1223 /* Identifier begins here.
1224 This is kludged for speed, so code is repeated. */
1225 isname:
1226 name = --input_line_pointer;
1227 c = get_symbol_end ();
1229 #ifdef md_operator
1231 operatorT operator = md_operator (name, 1, &c);
1233 switch (operator)
1235 case O_uminus:
1236 *input_line_pointer = c;
1237 c = '-';
1238 goto unary;
1239 case O_bit_not:
1240 *input_line_pointer = c;
1241 c = '~';
1242 goto unary;
1243 case O_logical_not:
1244 *input_line_pointer = c;
1245 c = '!';
1246 goto unary;
1247 case O_illegal:
1248 as_bad (_("invalid use of operator \"%s\""), name);
1249 break;
1250 default:
1251 break;
1253 if (operator != O_absent && operator != O_illegal)
1255 *input_line_pointer = c;
1256 expr (9, expressionP, mode);
1257 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1258 expressionP->X_op_symbol = NULL;
1259 expressionP->X_add_number = 0;
1260 expressionP->X_op = operator;
1261 break;
1264 #endif
1266 #ifdef md_parse_name
1267 /* This is a hook for the backend to parse certain names
1268 specially in certain contexts. If a name always has a
1269 specific value, it can often be handled by simply
1270 entering it in the symbol table. */
1271 if (md_parse_name (name, expressionP, mode, &c))
1273 *input_line_pointer = c;
1274 break;
1276 #endif
1278 #ifdef TC_I960
1279 /* The MRI i960 assembler permits
1280 lda sizeof code,g13
1281 FIXME: This should use md_parse_name. */
1282 if (flag_mri
1283 && (strcasecmp (name, "sizeof") == 0
1284 || strcasecmp (name, "startof") == 0))
1286 int start;
1287 char *buf;
1289 start = (name[1] == 't'
1290 || name[1] == 'T');
1292 *input_line_pointer = c;
1293 SKIP_WHITESPACE ();
1295 name = input_line_pointer;
1296 c = get_symbol_end ();
1298 buf = (char *) xmalloc (strlen (name) + 10);
1299 if (start)
1300 sprintf (buf, ".startof.%s", name);
1301 else
1302 sprintf (buf, ".sizeof.%s", name);
1303 symbolP = symbol_make (buf);
1304 free (buf);
1306 expressionP->X_op = O_symbol;
1307 expressionP->X_add_symbol = symbolP;
1308 expressionP->X_add_number = 0;
1310 *input_line_pointer = c;
1311 SKIP_WHITESPACE ();
1313 break;
1315 #endif
1317 symbolP = symbol_find_or_make (name);
1319 /* If we have an absolute symbol or a reg, then we know its
1320 value now. */
1321 segment = S_GET_SEGMENT (symbolP);
1322 if (mode != expr_defer && segment == absolute_section)
1324 expressionP->X_op = O_constant;
1325 expressionP->X_add_number = S_GET_VALUE (symbolP);
1327 else if (mode != expr_defer && segment == reg_section)
1329 expressionP->X_op = O_register;
1330 expressionP->X_add_number = S_GET_VALUE (symbolP);
1332 else
1334 expressionP->X_op = O_symbol;
1335 expressionP->X_add_symbol = symbolP;
1336 expressionP->X_add_number = 0;
1338 *input_line_pointer = c;
1340 else
1342 /* Let the target try to parse it. Success is indicated by changing
1343 the X_op field to something other than O_absent and pointing
1344 input_line_pointer past the expression. If it can't parse the
1345 expression, X_op and input_line_pointer should be unchanged. */
1346 expressionP->X_op = O_absent;
1347 --input_line_pointer;
1348 md_operand (expressionP);
1349 if (expressionP->X_op == O_absent)
1351 ++input_line_pointer;
1352 as_bad (_("bad expression"));
1353 expressionP->X_op = O_constant;
1354 expressionP->X_add_number = 0;
1357 break;
1360 /* It is more 'efficient' to clean up the expressionS when they are
1361 created. Doing it here saves lines of code. */
1362 clean_up_expression (expressionP);
1363 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1364 know (*input_line_pointer != ' ');
1366 /* The PA port needs this information. */
1367 if (expressionP->X_add_symbol)
1368 symbol_mark_used (expressionP->X_add_symbol);
1370 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1371 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1373 switch (expressionP->X_op)
1375 default:
1376 return absolute_section;
1377 case O_symbol:
1378 return S_GET_SEGMENT (expressionP->X_add_symbol);
1379 case O_register:
1380 return reg_section;
1384 /* Internal. Simplify a struct expression for use by expr (). */
1386 /* In: address of an expressionS.
1387 The X_op field of the expressionS may only take certain values.
1388 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1390 Out: expressionS may have been modified:
1391 Unused fields zeroed to help expr (). */
1393 static void
1394 clean_up_expression (expressionS *expressionP)
1396 switch (expressionP->X_op)
1398 case O_illegal:
1399 case O_absent:
1400 expressionP->X_add_number = 0;
1401 /* Fall through. */
1402 case O_big:
1403 case O_constant:
1404 case O_register:
1405 expressionP->X_add_symbol = NULL;
1406 /* Fall through. */
1407 case O_symbol:
1408 case O_uminus:
1409 case O_bit_not:
1410 expressionP->X_op_symbol = NULL;
1411 break;
1412 default:
1413 break;
1417 /* Expression parser. */
1419 /* We allow an empty expression, and just assume (absolute,0) silently.
1420 Unary operators and parenthetical expressions are treated as operands.
1421 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1423 We used to do an aho/ullman shift-reduce parser, but the logic got so
1424 warped that I flushed it and wrote a recursive-descent parser instead.
1425 Now things are stable, would anybody like to write a fast parser?
1426 Most expressions are either register (which does not even reach here)
1427 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1428 So I guess it doesn't really matter how inefficient more complex expressions
1429 are parsed.
1431 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1432 Also, we have consumed any leading or trailing spaces (operand does that)
1433 and done all intervening operators.
1435 This returns the segment of the result, which will be
1436 absolute_section or the segment of a symbol. */
1438 #undef __
1439 #define __ O_illegal
1440 #ifndef O_SINGLE_EQ
1441 #define O_SINGLE_EQ O_illegal
1442 #endif
1444 /* Maps ASCII -> operators. */
1445 static const operatorT op_encoding[256] = {
1446 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1447 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1449 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1450 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1451 __, __, __, __, __, __, __, __,
1452 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1453 __, __, __, __, __, __, __, __,
1454 __, __, __, __, __, __, __, __,
1455 __, __, __, __, __, __, __, __,
1456 __, __, __,
1457 #ifdef NEED_INDEX_OPERATOR
1458 O_index,
1459 #else
1461 #endif
1462 __, __, O_bit_exclusive_or, __,
1463 __, __, __, __, __, __, __, __,
1464 __, __, __, __, __, __, __, __,
1465 __, __, __, __, __, __, __, __,
1466 __, __, __, __, O_bit_inclusive_or, __, __, __,
1468 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1469 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1470 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1471 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1472 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1473 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1474 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1475 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1478 /* Rank Examples
1479 0 operand, (expression)
1480 1 ||
1481 2 &&
1482 3 == <> < <= >= >
1483 4 + -
1484 5 used for * / % in MRI mode
1485 6 & ^ ! |
1486 7 * / % << >>
1487 8 unary - unary ~
1489 static operator_rankT op_rank[] = {
1490 0, /* O_illegal */
1491 0, /* O_absent */
1492 0, /* O_constant */
1493 0, /* O_symbol */
1494 0, /* O_symbol_rva */
1495 0, /* O_register */
1496 0, /* O_big */
1497 9, /* O_uminus */
1498 9, /* O_bit_not */
1499 9, /* O_logical_not */
1500 8, /* O_multiply */
1501 8, /* O_divide */
1502 8, /* O_modulus */
1503 8, /* O_left_shift */
1504 8, /* O_right_shift */
1505 7, /* O_bit_inclusive_or */
1506 7, /* O_bit_or_not */
1507 7, /* O_bit_exclusive_or */
1508 7, /* O_bit_and */
1509 5, /* O_add */
1510 5, /* O_subtract */
1511 4, /* O_eq */
1512 4, /* O_ne */
1513 4, /* O_lt */
1514 4, /* O_le */
1515 4, /* O_ge */
1516 4, /* O_gt */
1517 3, /* O_logical_and */
1518 2, /* O_logical_or */
1519 1, /* O_index */
1520 0, /* O_md1 */
1521 0, /* O_md2 */
1522 0, /* O_md3 */
1523 0, /* O_md4 */
1524 0, /* O_md5 */
1525 0, /* O_md6 */
1526 0, /* O_md7 */
1527 0, /* O_md8 */
1528 0, /* O_md9 */
1529 0, /* O_md10 */
1530 0, /* O_md11 */
1531 0, /* O_md12 */
1532 0, /* O_md13 */
1533 0, /* O_md14 */
1534 0, /* O_md15 */
1535 0, /* O_md16 */
1538 /* Unfortunately, in MRI mode for the m68k, multiplication and
1539 division have lower precedence than the bit wise operators. This
1540 function sets the operator precedences correctly for the current
1541 mode. Also, MRI uses a different bit_not operator, and this fixes
1542 that as well. */
1544 #define STANDARD_MUL_PRECEDENCE 8
1545 #define MRI_MUL_PRECEDENCE 6
1547 void
1548 expr_set_precedence (void)
1550 if (flag_m68k_mri)
1552 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1553 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1554 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1556 else
1558 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1559 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1560 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1564 void
1565 expr_set_rank (operatorT operator, operator_rankT rank)
1567 assert (operator >= O_md1 && operator < ARRAY_SIZE (op_rank));
1568 op_rank[operator] = rank;
1571 /* Initialize the expression parser. */
1573 void
1574 expr_begin (void)
1576 expr_set_precedence ();
1578 /* Verify that X_op field is wide enough. */
1580 expressionS e;
1581 e.X_op = O_max;
1582 assert (e.X_op == O_max);
1586 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1587 sets NUM_CHARS to the number of characters in the operator.
1588 Does not advance INPUT_LINE_POINTER. */
1590 static inline operatorT
1591 operator (int *num_chars)
1593 int c;
1594 operatorT ret;
1596 c = *input_line_pointer & 0xff;
1597 *num_chars = 1;
1599 if (is_end_of_line[c])
1600 return O_illegal;
1602 #ifdef md_operator
1603 if (is_name_beginner (c))
1605 char *name = input_line_pointer;
1606 char c = get_symbol_end ();
1608 ret = md_operator (name, 2, &c);
1609 switch (ret)
1611 case O_absent:
1612 *input_line_pointer = c;
1613 input_line_pointer = name;
1614 break;
1615 case O_uminus:
1616 case O_bit_not:
1617 case O_logical_not:
1618 as_bad (_("invalid use of operator \"%s\""), name);
1619 ret = O_illegal;
1620 /* FALLTHROUGH */
1621 default:
1622 *input_line_pointer = c;
1623 *num_chars = input_line_pointer - name;
1624 input_line_pointer = name;
1625 return ret;
1628 #endif
1630 switch (c)
1632 default:
1633 ret = op_encoding[c];
1634 #ifdef md_operator
1635 if (ret == O_illegal)
1637 char *start = input_line_pointer;
1639 ret = md_operator (NULL, 2, NULL);
1640 if (ret != O_illegal)
1641 *num_chars = input_line_pointer - start;
1642 input_line_pointer = start;
1644 #endif
1645 return ret;
1647 case '+':
1648 case '-':
1649 return op_encoding[c];
1651 case '<':
1652 switch (input_line_pointer[1])
1654 default:
1655 return op_encoding[c];
1656 case '<':
1657 ret = O_left_shift;
1658 break;
1659 case '>':
1660 ret = O_ne;
1661 break;
1662 case '=':
1663 ret = O_le;
1664 break;
1666 *num_chars = 2;
1667 return ret;
1669 case '=':
1670 if (input_line_pointer[1] != '=')
1671 return op_encoding[c];
1673 *num_chars = 2;
1674 return O_eq;
1676 case '>':
1677 switch (input_line_pointer[1])
1679 default:
1680 return op_encoding[c];
1681 case '>':
1682 ret = O_right_shift;
1683 break;
1684 case '=':
1685 ret = O_ge;
1686 break;
1688 *num_chars = 2;
1689 return ret;
1691 case '!':
1692 switch (input_line_pointer[1])
1694 case '!':
1695 /* We accept !! as equivalent to ^ for MRI compatibility. */
1696 *num_chars = 2;
1697 return O_bit_exclusive_or;
1698 case '=':
1699 /* We accept != as equivalent to <>. */
1700 *num_chars = 2;
1701 return O_ne;
1702 default:
1703 if (flag_m68k_mri)
1704 return O_bit_inclusive_or;
1705 return op_encoding[c];
1708 case '|':
1709 if (input_line_pointer[1] != '|')
1710 return op_encoding[c];
1712 *num_chars = 2;
1713 return O_logical_or;
1715 case '&':
1716 if (input_line_pointer[1] != '&')
1717 return op_encoding[c];
1719 *num_chars = 2;
1720 return O_logical_and;
1723 /* NOTREACHED */
1726 /* Parse an expression. */
1728 segT
1729 expr (int rankarg, /* Larger # is higher rank. */
1730 expressionS *resultP, /* Deliver result here. */
1731 enum expr_mode mode /* Controls behavior. */)
1733 operator_rankT rank = (operator_rankT) rankarg;
1734 segT retval;
1735 expressionS right;
1736 operatorT op_left;
1737 operatorT op_right;
1738 int op_chars;
1740 know (rankarg >= 0);
1742 /* Save the value of dot for the fixup code. */
1743 if (rank == 0)
1744 dot_value = frag_now_fix ();
1746 retval = operand (resultP, mode);
1748 /* operand () gobbles spaces. */
1749 know (*input_line_pointer != ' ');
1751 op_left = operator (&op_chars);
1752 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1754 segT rightseg;
1755 bfd_vma frag_off;
1757 input_line_pointer += op_chars; /* -> after operator. */
1759 rightseg = expr (op_rank[(int) op_left], &right, mode);
1760 if (right.X_op == O_absent)
1762 as_warn (_("missing operand; zero assumed"));
1763 right.X_op = O_constant;
1764 right.X_add_number = 0;
1765 right.X_add_symbol = NULL;
1766 right.X_op_symbol = NULL;
1769 know (*input_line_pointer != ' ');
1771 if (op_left == O_index)
1773 if (*input_line_pointer != ']')
1774 as_bad ("missing right bracket");
1775 else
1777 ++input_line_pointer;
1778 SKIP_WHITESPACE ();
1782 op_right = operator (&op_chars);
1784 know (op_right == O_illegal || op_left == O_index
1785 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1786 know ((int) op_left >= (int) O_multiply);
1787 #ifndef md_operator
1788 know ((int) op_left <= (int) O_index);
1789 #else
1790 know ((int) op_left < (int) O_max);
1791 #endif
1793 /* input_line_pointer->after right-hand quantity. */
1794 /* left-hand quantity in resultP. */
1795 /* right-hand quantity in right. */
1796 /* operator in op_left. */
1798 if (resultP->X_op == O_big)
1800 if (resultP->X_add_number > 0)
1801 as_warn (_("left operand is a bignum; integer 0 assumed"));
1802 else
1803 as_warn (_("left operand is a float; integer 0 assumed"));
1804 resultP->X_op = O_constant;
1805 resultP->X_add_number = 0;
1806 resultP->X_add_symbol = NULL;
1807 resultP->X_op_symbol = NULL;
1809 if (right.X_op == O_big)
1811 if (right.X_add_number > 0)
1812 as_warn (_("right operand is a bignum; integer 0 assumed"));
1813 else
1814 as_warn (_("right operand is a float; integer 0 assumed"));
1815 right.X_op = O_constant;
1816 right.X_add_number = 0;
1817 right.X_add_symbol = NULL;
1818 right.X_op_symbol = NULL;
1821 /* Optimize common cases. */
1822 #ifdef md_optimize_expr
1823 if (md_optimize_expr (resultP, op_left, &right))
1825 /* Skip. */
1828 else
1829 #endif
1830 #ifndef md_register_arithmetic
1831 # define md_register_arithmetic 1
1832 #endif
1833 if (op_left == O_add && right.X_op == O_constant
1834 && (md_register_arithmetic || resultP->X_op != O_register))
1836 /* X + constant. */
1837 resultP->X_add_number += right.X_add_number;
1839 /* This case comes up in PIC code. */
1840 else if (op_left == O_subtract
1841 && right.X_op == O_symbol
1842 && resultP->X_op == O_symbol
1843 && retval == rightseg
1844 #ifdef md_allow_local_subtract
1845 && md_allow_local_subtract (resultP, & right, rightseg)
1846 #endif
1847 && (SEG_NORMAL (rightseg)
1848 || right.X_add_symbol == resultP->X_add_symbol)
1849 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1850 symbol_get_frag (right.X_add_symbol),
1851 &frag_off))
1853 resultP->X_add_number -= right.X_add_number;
1854 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1855 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1856 - S_GET_VALUE (right.X_add_symbol));
1857 resultP->X_op = O_constant;
1858 resultP->X_add_symbol = 0;
1860 else if (op_left == O_subtract && right.X_op == O_constant
1861 && (md_register_arithmetic || resultP->X_op != O_register))
1863 /* X - constant. */
1864 resultP->X_add_number -= right.X_add_number;
1866 else if (op_left == O_add && resultP->X_op == O_constant
1867 && (md_register_arithmetic || right.X_op != O_register))
1869 /* Constant + X. */
1870 resultP->X_op = right.X_op;
1871 resultP->X_add_symbol = right.X_add_symbol;
1872 resultP->X_op_symbol = right.X_op_symbol;
1873 resultP->X_add_number += right.X_add_number;
1874 retval = rightseg;
1876 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1878 /* Constant OP constant. */
1879 offsetT v = right.X_add_number;
1880 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1882 as_warn (_("division by zero"));
1883 v = 1;
1885 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1886 && (op_left == O_left_shift || op_left == O_right_shift))
1888 as_warn_value_out_of_range (_("shift count"), v, 0,
1889 sizeof(valueT) * CHAR_BIT - 1,
1890 NULL, 0);
1891 resultP->X_add_number = v = 0;
1893 switch (op_left)
1895 default: goto general;
1896 case O_multiply: resultP->X_add_number *= v; break;
1897 case O_divide: resultP->X_add_number /= v; break;
1898 case O_modulus: resultP->X_add_number %= v; break;
1899 case O_left_shift: resultP->X_add_number <<= v; break;
1900 case O_right_shift:
1901 /* We always use unsigned shifts, to avoid relying on
1902 characteristics of the compiler used to compile gas. */
1903 resultP->X_add_number =
1904 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1905 break;
1906 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1907 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1908 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1909 case O_bit_and: resultP->X_add_number &= v; break;
1910 /* Constant + constant (O_add) is handled by the
1911 previous if statement for constant + X, so is omitted
1912 here. */
1913 case O_subtract: resultP->X_add_number -= v; break;
1914 case O_eq:
1915 resultP->X_add_number =
1916 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1917 break;
1918 case O_ne:
1919 resultP->X_add_number =
1920 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1921 break;
1922 case O_lt:
1923 resultP->X_add_number =
1924 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1925 break;
1926 case O_le:
1927 resultP->X_add_number =
1928 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1929 break;
1930 case O_ge:
1931 resultP->X_add_number =
1932 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1933 break;
1934 case O_gt:
1935 resultP->X_add_number =
1936 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1937 break;
1938 case O_logical_and:
1939 resultP->X_add_number = resultP->X_add_number && v;
1940 break;
1941 case O_logical_or:
1942 resultP->X_add_number = resultP->X_add_number || v;
1943 break;
1946 else if (resultP->X_op == O_symbol
1947 && right.X_op == O_symbol
1948 && (op_left == O_add
1949 || op_left == O_subtract
1950 || (resultP->X_add_number == 0
1951 && right.X_add_number == 0)))
1953 /* Symbol OP symbol. */
1954 resultP->X_op = op_left;
1955 resultP->X_op_symbol = right.X_add_symbol;
1956 if (op_left == O_add)
1957 resultP->X_add_number += right.X_add_number;
1958 else if (op_left == O_subtract)
1960 resultP->X_add_number -= right.X_add_number;
1961 if (retval == rightseg && SEG_NORMAL (retval))
1963 retval = absolute_section;
1964 rightseg = absolute_section;
1968 else
1970 general:
1971 /* The general case. */
1972 resultP->X_add_symbol = make_expr_symbol (resultP);
1973 resultP->X_op_symbol = make_expr_symbol (&right);
1974 resultP->X_op = op_left;
1975 resultP->X_add_number = 0;
1976 resultP->X_unsigned = 1;
1979 if (retval != rightseg)
1981 if (! SEG_NORMAL (retval))
1983 if (retval != undefined_section || SEG_NORMAL (rightseg))
1984 retval = rightseg;
1986 else if (SEG_NORMAL (rightseg)
1987 #ifdef DIFF_EXPR_OK
1988 && op_left != O_subtract
1989 #endif
1991 as_bad (_("operation combines symbols in different segments"));
1994 op_left = op_right;
1995 } /* While next operator is >= this rank. */
1997 /* The PA port needs this information. */
1998 if (resultP->X_add_symbol)
1999 symbol_mark_used (resultP->X_add_symbol);
2001 if (rank == 0 && mode == expr_evaluate)
2002 resolve_expression (resultP);
2004 return resultP->X_op == O_constant ? absolute_section : retval;
2007 /* Resolve an expression without changing any symbols/sub-expressions
2008 used. */
2011 resolve_expression (expressionS *expressionP)
2013 /* Help out with CSE. */
2014 valueT final_val = expressionP->X_add_number;
2015 symbolS *add_symbol = expressionP->X_add_symbol;
2016 symbolS *op_symbol = expressionP->X_op_symbol;
2017 operatorT op = expressionP->X_op;
2018 valueT left, right;
2019 segT seg_left, seg_right;
2020 fragS *frag_left, *frag_right;
2021 bfd_vma frag_off;
2023 switch (op)
2025 default:
2026 return 0;
2028 case O_constant:
2029 case O_register:
2030 left = 0;
2031 break;
2033 case O_symbol:
2034 case O_symbol_rva:
2035 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2036 return 0;
2038 break;
2040 case O_uminus:
2041 case O_bit_not:
2042 case O_logical_not:
2043 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2044 return 0;
2046 if (seg_left != absolute_section)
2047 return 0;
2049 if (op == O_logical_not)
2050 left = !left;
2051 else if (op == O_uminus)
2052 left = -left;
2053 else
2054 left = ~left;
2055 op = O_constant;
2056 break;
2058 case O_multiply:
2059 case O_divide:
2060 case O_modulus:
2061 case O_left_shift:
2062 case O_right_shift:
2063 case O_bit_inclusive_or:
2064 case O_bit_or_not:
2065 case O_bit_exclusive_or:
2066 case O_bit_and:
2067 case O_add:
2068 case O_subtract:
2069 case O_eq:
2070 case O_ne:
2071 case O_lt:
2072 case O_le:
2073 case O_ge:
2074 case O_gt:
2075 case O_logical_and:
2076 case O_logical_or:
2077 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2078 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2079 return 0;
2081 /* Simplify addition or subtraction of a constant by folding the
2082 constant into X_add_number. */
2083 if (op == O_add)
2085 if (seg_right == absolute_section)
2087 final_val += right;
2088 op = O_symbol;
2089 break;
2091 else if (seg_left == absolute_section)
2093 final_val += left;
2094 left = right;
2095 seg_left = seg_right;
2096 add_symbol = op_symbol;
2097 op = O_symbol;
2098 break;
2101 else if (op == O_subtract)
2103 if (seg_right == absolute_section)
2105 final_val -= right;
2106 op = O_symbol;
2107 break;
2111 /* Equality and non-equality tests are permitted on anything.
2112 Subtraction, and other comparison operators are permitted if
2113 both operands are in the same section.
2114 Shifts by constant zero are permitted on anything.
2115 Multiplies, bit-ors, and bit-ands with constant zero are
2116 permitted on anything.
2117 Multiplies and divides by constant one are permitted on
2118 anything.
2119 Binary operations with both operands being the same register
2120 or undefined symbol are permitted if the result doesn't depend
2121 on the input value.
2122 Otherwise, both operands must be absolute. We already handled
2123 the case of addition or subtraction of a constant above. */
2124 frag_off = 0;
2125 if (!(seg_left == absolute_section
2126 && seg_right == absolute_section)
2127 && !(op == O_eq || op == O_ne)
2128 && !((op == O_subtract
2129 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2130 && seg_left == seg_right
2131 && (finalize_syms
2132 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2133 && (seg_left != reg_section || left == right)
2134 && (seg_left != undefined_section || add_symbol == op_symbol)))
2136 if ((seg_left == absolute_section && left == 0)
2137 || (seg_right == absolute_section && right == 0))
2139 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2141 if (seg_right != absolute_section || right != 0)
2143 seg_left = seg_right;
2144 left = right;
2145 add_symbol = op_symbol;
2147 op = O_symbol;
2148 break;
2150 else if (op == O_left_shift || op == O_right_shift)
2152 if (seg_left != absolute_section || left != 0)
2154 op = O_symbol;
2155 break;
2158 else if (op != O_multiply
2159 && op != O_bit_or_not && op != O_bit_and)
2160 return 0;
2162 else if (op == O_multiply
2163 && seg_left == absolute_section && left == 1)
2165 seg_left = seg_right;
2166 left = right;
2167 add_symbol = op_symbol;
2168 op = O_symbol;
2169 break;
2171 else if ((op == O_multiply || op == O_divide)
2172 && seg_right == absolute_section && right == 1)
2174 op = O_symbol;
2175 break;
2177 else if (left != right
2178 || ((seg_left != reg_section || seg_right != reg_section)
2179 && (seg_left != undefined_section
2180 || seg_right != undefined_section
2181 || add_symbol != op_symbol)))
2182 return 0;
2183 else if (op == O_bit_and || op == O_bit_inclusive_or)
2185 op = O_symbol;
2186 break;
2188 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2189 return 0;
2192 right += frag_off / OCTETS_PER_BYTE;
2193 switch (op)
2195 case O_add: left += right; break;
2196 case O_subtract: left -= right; break;
2197 case O_multiply: left *= right; break;
2198 case O_divide:
2199 if (right == 0)
2200 return 0;
2201 left = (offsetT) left / (offsetT) right;
2202 break;
2203 case O_modulus:
2204 if (right == 0)
2205 return 0;
2206 left = (offsetT) left % (offsetT) right;
2207 break;
2208 case O_left_shift: left <<= right; break;
2209 case O_right_shift: left >>= right; break;
2210 case O_bit_inclusive_or: left |= right; break;
2211 case O_bit_or_not: left |= ~right; break;
2212 case O_bit_exclusive_or: left ^= right; break;
2213 case O_bit_and: left &= right; break;
2214 case O_eq:
2215 case O_ne:
2216 left = (left == right
2217 && seg_left == seg_right
2218 && (finalize_syms || frag_left == frag_right)
2219 && (seg_left != undefined_section
2220 || add_symbol == op_symbol)
2221 ? ~ (valueT) 0 : 0);
2222 if (op == O_ne)
2223 left = ~left;
2224 break;
2225 case O_lt:
2226 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2227 break;
2228 case O_le:
2229 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2230 break;
2231 case O_ge:
2232 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2233 break;
2234 case O_gt:
2235 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2236 break;
2237 case O_logical_and: left = left && right; break;
2238 case O_logical_or: left = left || right; break;
2239 default: abort ();
2242 op = O_constant;
2243 break;
2246 if (op == O_symbol)
2248 if (seg_left == absolute_section)
2249 op = O_constant;
2250 else if (seg_left == reg_section && final_val == 0)
2251 op = O_register;
2252 else if (add_symbol != expressionP->X_add_symbol)
2253 final_val += left;
2254 expressionP->X_add_symbol = add_symbol;
2256 expressionP->X_op = op;
2258 if (op == O_constant || op == O_register)
2259 final_val += left;
2260 expressionP->X_add_number = final_val;
2262 return 1;
2265 /* This lives here because it belongs equally in expr.c & read.c.
2266 expr.c is just a branch office read.c anyway, and putting it
2267 here lessens the crowd at read.c.
2269 Assume input_line_pointer is at start of symbol name.
2270 Advance input_line_pointer past symbol name.
2271 Turn that character into a '\0', returning its former value.
2272 This allows a string compare (RMS wants symbol names to be strings)
2273 of the symbol name.
2274 There will always be a char following symbol name, because all good
2275 lines end in end-of-line. */
2277 char
2278 get_symbol_end (void)
2280 char c;
2282 /* We accept \001 in a name in case this is being called with a
2283 constructed string. */
2284 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2286 while (is_part_of_name (c = *input_line_pointer++)
2287 || c == '\001')
2289 if (is_name_ender (c))
2290 c = *input_line_pointer++;
2292 *--input_line_pointer = 0;
2293 return (c);
2296 unsigned int
2297 get_single_number (void)
2299 expressionS exp;
2300 operand (&exp, expr_normal);
2301 return exp.X_add_number;