Use .2byte/.4byte instead of .short/.long.
[binutils.git] / gas / expr.c
bloba6910bf91ea529defe49fe23e5a98b43001f5bd6
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, 2009
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 clean_up_expression (expressionS * expressionP);
49 static segT operand (expressionS *, enum expr_mode);
50 static operatorT operatorf (int *);
52 extern const char EXP_CHARS[], FLT_CHARS[];
54 /* We keep a mapping of expression symbols to file positions, so that
55 we can provide better error messages. */
57 struct expr_symbol_line {
58 struct expr_symbol_line *next;
59 symbolS *sym;
60 char *file;
61 unsigned int line;
64 static struct expr_symbol_line *expr_symbol_lines;
66 /* Build a dummy symbol to hold a complex expression. This is how we
67 build expressions up out of other expressions. The symbol is put
68 into the fake section expr_section. */
70 symbolS *
71 make_expr_symbol (expressionS *expressionP)
73 expressionS zero;
74 symbolS *symbolP;
75 struct expr_symbol_line *n;
77 if (expressionP->X_op == O_symbol
78 && expressionP->X_add_number == 0)
79 return expressionP->X_add_symbol;
81 if (expressionP->X_op == O_big)
83 /* This won't work, because the actual value is stored in
84 generic_floating_point_number or generic_bignum, and we are
85 going to lose it if we haven't already. */
86 if (expressionP->X_add_number > 0)
87 as_bad (_("bignum invalid"));
88 else
89 as_bad (_("floating point number invalid"));
90 zero.X_op = O_constant;
91 zero.X_add_number = 0;
92 zero.X_unsigned = 0;
93 clean_up_expression (&zero);
94 expressionP = &zero;
97 /* Putting constant symbols in absolute_section rather than
98 expr_section is convenient for the old a.out code, for which
99 S_GET_SEGMENT does not always retrieve the value put in by
100 S_SET_SEGMENT. */
101 symbolP = symbol_create (FAKE_LABEL_NAME,
102 (expressionP->X_op == O_constant
103 ? absolute_section
104 : expressionP->X_op == O_register
105 ? reg_section
106 : expr_section),
107 0, &zero_address_frag);
108 symbol_set_value_expression (symbolP, expressionP);
110 if (expressionP->X_op == O_constant)
111 resolve_symbol_value (symbolP);
113 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
114 n->sym = symbolP;
115 as_where (&n->file, &n->line);
116 n->next = expr_symbol_lines;
117 expr_symbol_lines = n;
119 return symbolP;
122 /* Return the file and line number for an expr symbol. Return
123 non-zero if something was found, 0 if no information is known for
124 the symbol. */
127 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
129 register struct expr_symbol_line *l;
131 for (l = expr_symbol_lines; l != NULL; l = l->next)
133 if (l->sym == sym)
135 *pfile = l->file;
136 *pline = l->line;
137 return 1;
141 return 0;
144 /* Utilities for building expressions.
145 Since complex expressions are recorded as symbols for use in other
146 expressions these return a symbolS * and not an expressionS *.
147 These explicitly do not take an "add_number" argument. */
148 /* ??? For completeness' sake one might want expr_build_symbol.
149 It would just return its argument. */
151 /* Build an expression for an unsigned constant.
152 The corresponding one for signed constants is missing because
153 there's currently no need for it. One could add an unsigned_p flag
154 but that seems more clumsy. */
156 symbolS *
157 expr_build_uconstant (offsetT value)
159 expressionS e;
161 e.X_op = O_constant;
162 e.X_add_number = value;
163 e.X_unsigned = 1;
164 return make_expr_symbol (&e);
167 /* Build an expression for the current location ('.'). */
169 symbolS *
170 expr_build_dot (void)
172 expressionS e;
174 current_location (&e);
175 return make_expr_symbol (&e);
178 /* Build any floating-point literal here.
179 Also build any bignum literal here. */
181 /* Seems atof_machine can backscan through generic_bignum and hit whatever
182 happens to be loaded before it in memory. And its way too complicated
183 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
184 and never write into the early words, thus they'll always be zero.
185 I hate Dean's floating-point code. Bleh. */
186 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
188 FLONUM_TYPE generic_floating_point_number = {
189 &generic_bignum[6], /* low. (JF: Was 0) */
190 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
191 0, /* leader. */
192 0, /* exponent. */
193 0 /* sign. */
197 static void
198 floating_constant (expressionS *expressionP)
200 /* input_line_pointer -> floating-point constant. */
201 int error_code;
203 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
204 &generic_floating_point_number);
206 if (error_code)
208 if (error_code == ERROR_EXPONENT_OVERFLOW)
210 as_bad (_("bad floating-point constant: exponent overflow"));
212 else
214 as_bad (_("bad floating-point constant: unknown error code=%d"),
215 error_code);
218 expressionP->X_op = O_big;
219 /* input_line_pointer -> just after constant, which may point to
220 whitespace. */
221 expressionP->X_add_number = -1;
224 static valueT
225 generic_bignum_to_int32 (void)
227 valueT number =
228 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
229 | (generic_bignum[0] & LITTLENUM_MASK);
230 number &= 0xffffffff;
231 return number;
234 #ifdef BFD64
235 static valueT
236 generic_bignum_to_int64 (void)
238 valueT number =
239 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
240 << LITTLENUM_NUMBER_OF_BITS)
241 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
242 << LITTLENUM_NUMBER_OF_BITS)
243 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
244 << LITTLENUM_NUMBER_OF_BITS)
245 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
246 return number;
248 #endif
250 static void
251 integer_constant (int radix, expressionS *expressionP)
253 char *start; /* Start of number. */
254 char *suffix = NULL;
255 char c;
256 valueT number; /* Offset or (absolute) value. */
257 short int digit; /* Value of next digit in current radix. */
258 short int maxdig = 0; /* Highest permitted digit value. */
259 int too_many_digits = 0; /* If we see >= this number of. */
260 char *name; /* Points to name of symbol. */
261 symbolS *symbolP; /* Points to symbol. */
263 int small; /* True if fits in 32 bits. */
265 /* May be bignum, or may fit in 32 bits. */
266 /* Most numbers fit into 32 bits, and we want this case to be fast.
267 so we pretend it will fit into 32 bits. If, after making up a 32
268 bit number, we realise that we have scanned more digits than
269 comfortably fit into 32 bits, we re-scan the digits coding them
270 into a bignum. For decimal and octal numbers we are
271 conservative: Some numbers may be assumed bignums when in fact
272 they do fit into 32 bits. Numbers of any radix can have excess
273 leading zeros: We strive to recognise this and cast them back
274 into 32 bits. We must check that the bignum really is more than
275 32 bits, and change it back to a 32-bit number if it fits. The
276 number we are looking for is expected to be positive, but if it
277 fits into 32 bits as an unsigned number, we let it be a 32-bit
278 number. The cavalier approach is for speed in ordinary cases. */
279 /* This has been extended for 64 bits. We blindly assume that if
280 you're compiling in 64-bit mode, the target is a 64-bit machine.
281 This should be cleaned up. */
283 #ifdef BFD64
284 #define valuesize 64
285 #else /* includes non-bfd case, mostly */
286 #define valuesize 32
287 #endif
289 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
291 int flt = 0;
293 /* In MRI mode, the number may have a suffix indicating the
294 radix. For that matter, it might actually be a floating
295 point constant. */
296 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
298 if (*suffix == 'e' || *suffix == 'E')
299 flt = 1;
302 if (suffix == input_line_pointer)
304 radix = 10;
305 suffix = NULL;
307 else
309 c = *--suffix;
310 c = TOUPPER (c);
311 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
312 we distinguish between 'B' and 'b'. This is the case for
313 Z80. */
314 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
315 radix = 2;
316 else if (c == 'D')
317 radix = 10;
318 else if (c == 'O' || c == 'Q')
319 radix = 8;
320 else if (c == 'H')
321 radix = 16;
322 else if (suffix[1] == '.' || c == 'E' || flt)
324 floating_constant (expressionP);
325 return;
327 else
329 radix = 10;
330 suffix = NULL;
335 switch (radix)
337 case 2:
338 maxdig = 2;
339 too_many_digits = valuesize + 1;
340 break;
341 case 8:
342 maxdig = radix = 8;
343 too_many_digits = (valuesize + 2) / 3 + 1;
344 break;
345 case 16:
346 maxdig = radix = 16;
347 too_many_digits = (valuesize + 3) / 4 + 1;
348 break;
349 case 10:
350 maxdig = radix = 10;
351 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
353 #undef valuesize
354 start = input_line_pointer;
355 c = *input_line_pointer++;
356 for (number = 0;
357 (digit = hex_value (c)) < maxdig;
358 c = *input_line_pointer++)
360 number = number * radix + digit;
362 /* c contains character after number. */
363 /* input_line_pointer->char after c. */
364 small = (input_line_pointer - start - 1) < too_many_digits;
366 if (radix == 16 && c == '_')
368 /* This is literal of the form 0x333_0_12345678_1.
369 This example is equivalent to 0x00000333000000001234567800000001. */
371 int num_little_digits = 0;
372 int i;
373 input_line_pointer = start; /* -> 1st digit. */
375 know (LITTLENUM_NUMBER_OF_BITS == 16);
377 for (c = '_'; c == '_'; num_little_digits += 2)
380 /* Convert one 64-bit word. */
381 int ndigit = 0;
382 number = 0;
383 for (c = *input_line_pointer++;
384 (digit = hex_value (c)) < maxdig;
385 c = *(input_line_pointer++))
387 number = number * radix + digit;
388 ndigit++;
391 /* Check for 8 digit per word max. */
392 if (ndigit > 8)
393 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
395 /* Add this chunk to the bignum.
396 Shift things down 2 little digits. */
397 know (LITTLENUM_NUMBER_OF_BITS == 16);
398 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
399 i >= 2;
400 i--)
401 generic_bignum[i] = generic_bignum[i - 2];
403 /* Add the new digits as the least significant new ones. */
404 generic_bignum[0] = number & 0xffffffff;
405 generic_bignum[1] = number >> 16;
408 /* Again, c is char after number, input_line_pointer->after c. */
410 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
411 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
413 gas_assert (num_little_digits >= 4);
415 if (num_little_digits != 8)
416 as_bad (_("a bignum with underscores must have exactly 4 words"));
418 /* We might have some leading zeros. These can be trimmed to give
419 us a change to fit this constant into a small number. */
420 while (generic_bignum[num_little_digits - 1] == 0
421 && num_little_digits > 1)
422 num_little_digits--;
424 if (num_little_digits <= 2)
426 /* will fit into 32 bits. */
427 number = generic_bignum_to_int32 ();
428 small = 1;
430 #ifdef BFD64
431 else if (num_little_digits <= 4)
433 /* Will fit into 64 bits. */
434 number = generic_bignum_to_int64 ();
435 small = 1;
437 #endif
438 else
440 small = 0;
442 /* Number of littlenums in the bignum. */
443 number = num_little_digits;
446 else if (!small)
448 /* We saw a lot of digits. manufacture a bignum the hard way. */
449 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
450 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
451 long carry;
453 leader = generic_bignum;
454 generic_bignum[0] = 0;
455 generic_bignum[1] = 0;
456 generic_bignum[2] = 0;
457 generic_bignum[3] = 0;
458 input_line_pointer = start; /* -> 1st digit. */
459 c = *input_line_pointer++;
460 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
462 for (pointer = generic_bignum; pointer <= leader; pointer++)
464 long work;
466 work = carry + radix * *pointer;
467 *pointer = work & LITTLENUM_MASK;
468 carry = work >> LITTLENUM_NUMBER_OF_BITS;
470 if (carry)
472 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
474 /* Room to grow a longer bignum. */
475 *++leader = carry;
479 /* Again, c is char after number. */
480 /* input_line_pointer -> after c. */
481 know (LITTLENUM_NUMBER_OF_BITS == 16);
482 if (leader < generic_bignum + 2)
484 /* Will fit into 32 bits. */
485 number = generic_bignum_to_int32 ();
486 small = 1;
488 #ifdef BFD64
489 else if (leader < generic_bignum + 4)
491 /* Will fit into 64 bits. */
492 number = generic_bignum_to_int64 ();
493 small = 1;
495 #endif
496 else
498 /* Number of littlenums in the bignum. */
499 number = leader - generic_bignum + 1;
503 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
504 && suffix != NULL
505 && input_line_pointer - 1 == suffix)
506 c = *input_line_pointer++;
508 if (small)
510 /* Here with number, in correct radix. c is the next char.
511 Note that unlike un*x, we allow "011f" "0x9f" to both mean
512 the same as the (conventional) "9f".
513 This is simply easier than checking for strict canonical
514 form. Syntax sux! */
516 if (LOCAL_LABELS_FB && c == 'b')
518 /* Backward ref to local label.
519 Because it is backward, expect it to be defined. */
520 /* Construct a local label. */
521 name = fb_label_name ((int) number, 0);
523 /* Seen before, or symbol is defined: OK. */
524 symbolP = symbol_find (name);
525 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
527 /* Local labels are never absolute. Don't waste time
528 checking absoluteness. */
529 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
531 expressionP->X_op = O_symbol;
532 expressionP->X_add_symbol = symbolP;
534 else
536 /* Either not seen or not defined. */
537 /* @@ Should print out the original string instead of
538 the parsed number. */
539 as_bad (_("backward ref to unknown label \"%d:\""),
540 (int) number);
541 expressionP->X_op = O_constant;
544 expressionP->X_add_number = 0;
545 } /* case 'b' */
546 else if (LOCAL_LABELS_FB && c == 'f')
548 /* Forward reference. Expect symbol to be undefined or
549 unknown. undefined: seen it before. unknown: never seen
550 it before.
552 Construct a local label name, then an undefined symbol.
553 Don't create a xseg frag for it: caller may do that.
554 Just return it as never seen before. */
555 name = fb_label_name ((int) number, 1);
556 symbolP = symbol_find_or_make (name);
557 /* We have no need to check symbol properties. */
558 #ifndef many_segments
559 /* Since "know" puts its arg into a "string", we
560 can't have newlines in the argument. */
561 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
562 #endif
563 expressionP->X_op = O_symbol;
564 expressionP->X_add_symbol = symbolP;
565 expressionP->X_add_number = 0;
566 } /* case 'f' */
567 else if (LOCAL_LABELS_DOLLAR && c == '$')
569 /* If the dollar label is *currently* defined, then this is just
570 another reference to it. If it is not *currently* defined,
571 then this is a fresh instantiation of that number, so create
572 it. */
574 if (dollar_label_defined ((long) number))
576 name = dollar_label_name ((long) number, 0);
577 symbolP = symbol_find (name);
578 know (symbolP != NULL);
580 else
582 name = dollar_label_name ((long) number, 1);
583 symbolP = symbol_find_or_make (name);
586 expressionP->X_op = O_symbol;
587 expressionP->X_add_symbol = symbolP;
588 expressionP->X_add_number = 0;
589 } /* case '$' */
590 else
592 expressionP->X_op = O_constant;
593 expressionP->X_add_number = number;
594 input_line_pointer--; /* Restore following character. */
595 } /* Really just a number. */
597 else
599 /* Not a small number. */
600 expressionP->X_op = O_big;
601 expressionP->X_add_number = number; /* Number of littlenums. */
602 input_line_pointer--; /* -> char following number. */
606 /* Parse an MRI multi character constant. */
608 static void
609 mri_char_constant (expressionS *expressionP)
611 int i;
613 if (*input_line_pointer == '\''
614 && input_line_pointer[1] != '\'')
616 expressionP->X_op = O_constant;
617 expressionP->X_add_number = 0;
618 return;
621 /* In order to get the correct byte ordering, we must build the
622 number in reverse. */
623 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
625 int j;
627 generic_bignum[i] = 0;
628 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
630 if (*input_line_pointer == '\'')
632 if (input_line_pointer[1] != '\'')
633 break;
634 ++input_line_pointer;
636 generic_bignum[i] <<= 8;
637 generic_bignum[i] += *input_line_pointer;
638 ++input_line_pointer;
641 if (i < SIZE_OF_LARGE_NUMBER - 1)
643 /* If there is more than one littlenum, left justify the
644 last one to make it match the earlier ones. If there is
645 only one, we can just use the value directly. */
646 for (; j < CHARS_PER_LITTLENUM; j++)
647 generic_bignum[i] <<= 8;
650 if (*input_line_pointer == '\''
651 && input_line_pointer[1] != '\'')
652 break;
655 if (i < 0)
657 as_bad (_("character constant too large"));
658 i = 0;
661 if (i > 0)
663 int c;
664 int j;
666 c = SIZE_OF_LARGE_NUMBER - i;
667 for (j = 0; j < c; j++)
668 generic_bignum[j] = generic_bignum[i + j];
669 i = c;
672 know (LITTLENUM_NUMBER_OF_BITS == 16);
673 if (i > 2)
675 expressionP->X_op = O_big;
676 expressionP->X_add_number = i;
678 else
680 expressionP->X_op = O_constant;
681 if (i < 2)
682 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
683 else
684 expressionP->X_add_number =
685 (((generic_bignum[1] & LITTLENUM_MASK)
686 << LITTLENUM_NUMBER_OF_BITS)
687 | (generic_bignum[0] & LITTLENUM_MASK));
690 /* Skip the final closing quote. */
691 ++input_line_pointer;
694 /* Return an expression representing the current location. This
695 handles the magic symbol `.'. */
697 void
698 current_location (expressionS *expressionp)
700 if (now_seg == absolute_section)
702 expressionp->X_op = O_constant;
703 expressionp->X_add_number = abs_section_offset;
705 else
707 expressionp->X_op = O_symbol;
708 expressionp->X_add_symbol = symbol_temp_new_now ();
709 expressionp->X_add_number = 0;
713 /* In: Input_line_pointer points to 1st char of operand, which may
714 be a space.
716 Out: An expressionS.
717 The operand may have been empty: in this case X_op == O_absent.
718 Input_line_pointer->(next non-blank) char after operand. */
720 static segT
721 operand (expressionS *expressionP, enum expr_mode mode)
723 char c;
724 symbolS *symbolP; /* Points to symbol. */
725 char *name; /* Points to name of symbol. */
726 segT segment;
728 /* All integers are regarded as unsigned unless they are negated.
729 This is because the only thing which cares whether a number is
730 unsigned is the code in emit_expr which extends constants into
731 bignums. It should only sign extend negative numbers, so that
732 something like ``.quad 0x80000000'' is not sign extended even
733 though it appears negative if valueT is 32 bits. */
734 expressionP->X_unsigned = 1;
736 /* Digits, assume it is a bignum. */
738 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
739 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
741 if (is_end_of_line[(unsigned char) c])
742 goto eol;
744 switch (c)
746 case '1':
747 case '2':
748 case '3':
749 case '4':
750 case '5':
751 case '6':
752 case '7':
753 case '8':
754 case '9':
755 input_line_pointer--;
757 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
758 ? 0 : 10,
759 expressionP);
760 break;
762 #ifdef LITERAL_PREFIXDOLLAR_HEX
763 case '$':
764 /* $L is the start of a local label, not a hex constant. */
765 if (* input_line_pointer == 'L')
766 goto isname;
767 integer_constant (16, expressionP);
768 break;
769 #endif
771 #ifdef LITERAL_PREFIXPERCENT_BIN
772 case '%':
773 integer_constant (2, expressionP);
774 break;
775 #endif
777 case '0':
778 /* Non-decimal radix. */
780 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
782 char *s;
784 /* Check for a hex or float constant. */
785 for (s = input_line_pointer; hex_p (*s); s++)
787 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
789 --input_line_pointer;
790 integer_constant (0, expressionP);
791 break;
794 c = *input_line_pointer;
795 switch (c)
797 case 'o':
798 case 'O':
799 case 'q':
800 case 'Q':
801 case '8':
802 case '9':
803 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
805 integer_constant (0, expressionP);
806 break;
808 /* Fall through. */
809 default:
810 default_case:
811 if (c && strchr (FLT_CHARS, c))
813 input_line_pointer++;
814 floating_constant (expressionP);
815 expressionP->X_add_number = - TOLOWER (c);
817 else
819 /* The string was only zero. */
820 expressionP->X_op = O_constant;
821 expressionP->X_add_number = 0;
824 break;
826 case 'x':
827 case 'X':
828 if (flag_m68k_mri)
829 goto default_case;
830 input_line_pointer++;
831 integer_constant (16, expressionP);
832 break;
834 case 'b':
835 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
837 /* This code used to check for '+' and '-' here, and, in
838 some conditions, fall through to call
839 integer_constant. However, that didn't make sense,
840 as integer_constant only accepts digits. */
841 /* Some of our code elsewhere does permit digits greater
842 than the expected base; for consistency, do the same
843 here. */
844 if (input_line_pointer[1] < '0'
845 || input_line_pointer[1] > '9')
847 /* Parse this as a back reference to label 0. */
848 input_line_pointer--;
849 integer_constant (10, expressionP);
850 break;
852 /* Otherwise, parse this as a binary number. */
854 /* Fall through. */
855 case 'B':
856 input_line_pointer++;
857 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
858 goto default_case;
859 integer_constant (2, expressionP);
860 break;
862 case '0':
863 case '1':
864 case '2':
865 case '3':
866 case '4':
867 case '5':
868 case '6':
869 case '7':
870 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
871 ? 0 : 8,
872 expressionP);
873 break;
875 case 'f':
876 if (LOCAL_LABELS_FB)
878 /* If it says "0f" and it could possibly be a floating point
879 number, make it one. Otherwise, make it a local label,
880 and try to deal with parsing the rest later. */
881 if (!input_line_pointer[1]
882 || (is_end_of_line[0xff & input_line_pointer[1]])
883 || strchr (FLT_CHARS, 'f') == NULL)
884 goto is_0f_label;
886 char *cp = input_line_pointer + 1;
887 int r = atof_generic (&cp, ".", EXP_CHARS,
888 &generic_floating_point_number);
889 switch (r)
891 case 0:
892 case ERROR_EXPONENT_OVERFLOW:
893 if (*cp == 'f' || *cp == 'b')
894 /* Looks like a difference expression. */
895 goto is_0f_label;
896 else if (cp == input_line_pointer + 1)
897 /* No characters has been accepted -- looks like
898 end of operand. */
899 goto is_0f_label;
900 else
901 goto is_0f_float;
902 default:
903 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
908 /* Okay, now we've sorted it out. We resume at one of these
909 two labels, depending on what we've decided we're probably
910 looking at. */
911 is_0f_label:
912 input_line_pointer--;
913 integer_constant (10, expressionP);
914 break;
916 is_0f_float:
917 /* Fall through. */
921 case 'd':
922 case 'D':
923 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
925 integer_constant (0, expressionP);
926 break;
928 /* Fall through. */
929 case 'F':
930 case 'r':
931 case 'e':
932 case 'E':
933 case 'g':
934 case 'G':
935 input_line_pointer++;
936 floating_constant (expressionP);
937 expressionP->X_add_number = - TOLOWER (c);
938 break;
940 case '$':
941 if (LOCAL_LABELS_DOLLAR)
943 integer_constant (10, expressionP);
944 break;
946 else
947 goto default_case;
950 break;
952 #ifndef NEED_INDEX_OPERATOR
953 case '[':
954 # ifdef md_need_index_operator
955 if (md_need_index_operator())
956 goto de_fault;
957 # endif
958 /* FALLTHROUGH */
959 #endif
960 case '(':
961 /* Didn't begin with digit & not a name. */
962 if (mode != expr_defer)
963 segment = expression (expressionP);
964 else
965 segment = deferred_expression (expressionP);
966 /* expression () will pass trailing whitespace. */
967 if ((c == '(' && *input_line_pointer != ')')
968 || (c == '[' && *input_line_pointer != ']'))
969 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
970 else
971 input_line_pointer++;
972 SKIP_WHITESPACE ();
973 /* Here with input_line_pointer -> char after "(...)". */
974 return segment;
976 #ifdef TC_M68K
977 case 'E':
978 if (! flag_m68k_mri || *input_line_pointer != '\'')
979 goto de_fault;
980 as_bad (_("EBCDIC constants are not supported"));
981 /* Fall through. */
982 case 'A':
983 if (! flag_m68k_mri || *input_line_pointer != '\'')
984 goto de_fault;
985 ++input_line_pointer;
986 /* Fall through. */
987 #endif
988 case '\'':
989 if (! flag_m68k_mri)
991 /* Warning: to conform to other people's assemblers NO
992 ESCAPEMENT is permitted for a single quote. The next
993 character, parity errors and all, is taken as the value
994 of the operand. VERY KINKY. */
995 expressionP->X_op = O_constant;
996 expressionP->X_add_number = *input_line_pointer++;
997 break;
1000 mri_char_constant (expressionP);
1001 break;
1003 #ifdef TC_M68K
1004 case '"':
1005 /* Double quote is the bitwise not operator in MRI mode. */
1006 if (! flag_m68k_mri)
1007 goto de_fault;
1008 /* Fall through. */
1009 #endif
1010 case '~':
1011 /* '~' is permitted to start a label on the Delta. */
1012 if (is_name_beginner (c))
1013 goto isname;
1014 case '!':
1015 case '-':
1016 case '+':
1018 #ifdef md_operator
1019 unary:
1020 #endif
1021 operand (expressionP, mode);
1022 if (expressionP->X_op == O_constant)
1024 /* input_line_pointer -> char after operand. */
1025 if (c == '-')
1027 expressionP->X_add_number = - expressionP->X_add_number;
1028 /* Notice: '-' may overflow: no warning is given.
1029 This is compatible with other people's
1030 assemblers. Sigh. */
1031 expressionP->X_unsigned = 0;
1033 else if (c == '~' || c == '"')
1034 expressionP->X_add_number = ~ expressionP->X_add_number;
1035 else if (c == '!')
1036 expressionP->X_add_number = ! expressionP->X_add_number;
1038 else if (expressionP->X_op == O_big
1039 && expressionP->X_add_number <= 0
1040 && c == '-'
1041 && (generic_floating_point_number.sign == '+'
1042 || generic_floating_point_number.sign == 'P'))
1044 /* Negative flonum (eg, -1.000e0). */
1045 if (generic_floating_point_number.sign == '+')
1046 generic_floating_point_number.sign = '-';
1047 else
1048 generic_floating_point_number.sign = 'N';
1050 else if (expressionP->X_op == O_big
1051 && expressionP->X_add_number > 0)
1053 int i;
1055 if (c == '~' || c == '-')
1057 for (i = 0; i < expressionP->X_add_number; ++i)
1058 generic_bignum[i] = ~generic_bignum[i];
1059 if (c == '-')
1060 for (i = 0; i < expressionP->X_add_number; ++i)
1062 generic_bignum[i] += 1;
1063 if (generic_bignum[i])
1064 break;
1067 else if (c == '!')
1069 int nonzero = 0;
1070 for (i = 0; i < expressionP->X_add_number; ++i)
1072 if (generic_bignum[i])
1073 nonzero = 1;
1074 generic_bignum[i] = 0;
1076 generic_bignum[0] = nonzero;
1079 else if (expressionP->X_op != O_illegal
1080 && expressionP->X_op != O_absent)
1082 if (c != '+')
1084 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1085 if (c == '-')
1086 expressionP->X_op = O_uminus;
1087 else if (c == '~' || c == '"')
1088 expressionP->X_op = O_bit_not;
1089 else
1090 expressionP->X_op = O_logical_not;
1091 expressionP->X_add_number = 0;
1094 else
1095 as_warn (_("Unary operator %c ignored because bad operand follows"),
1098 break;
1100 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1101 case '$':
1102 /* '$' is the program counter when in MRI mode, or when
1103 DOLLAR_DOT is defined. */
1104 #ifndef DOLLAR_DOT
1105 if (! flag_m68k_mri)
1106 goto de_fault;
1107 #endif
1108 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1110 /* In MRI mode and on Z80, '$' is also used as the prefix
1111 for a hexadecimal constant. */
1112 integer_constant (16, expressionP);
1113 break;
1116 if (is_part_of_name (*input_line_pointer))
1117 goto isname;
1119 current_location (expressionP);
1120 break;
1121 #endif
1123 case '.':
1124 if (!is_part_of_name (*input_line_pointer))
1126 current_location (expressionP);
1127 break;
1129 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1130 && ! is_part_of_name (input_line_pointer[8]))
1131 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1132 && ! is_part_of_name (input_line_pointer[7])))
1134 int start;
1136 start = (input_line_pointer[1] == 't'
1137 || input_line_pointer[1] == 'T');
1138 input_line_pointer += start ? 8 : 7;
1139 SKIP_WHITESPACE ();
1140 if (*input_line_pointer != '(')
1141 as_bad (_("syntax error in .startof. or .sizeof."));
1142 else
1144 char *buf;
1146 ++input_line_pointer;
1147 SKIP_WHITESPACE ();
1148 name = input_line_pointer;
1149 c = get_symbol_end ();
1151 buf = (char *) xmalloc (strlen (name) + 10);
1152 if (start)
1153 sprintf (buf, ".startof.%s", name);
1154 else
1155 sprintf (buf, ".sizeof.%s", name);
1156 symbolP = symbol_make (buf);
1157 free (buf);
1159 expressionP->X_op = O_symbol;
1160 expressionP->X_add_symbol = symbolP;
1161 expressionP->X_add_number = 0;
1163 *input_line_pointer = c;
1164 SKIP_WHITESPACE ();
1165 if (*input_line_pointer != ')')
1166 as_bad (_("syntax error in .startof. or .sizeof."));
1167 else
1168 ++input_line_pointer;
1170 break;
1172 else
1174 goto isname;
1177 case ',':
1178 eol:
1179 /* Can't imagine any other kind of operand. */
1180 expressionP->X_op = O_absent;
1181 input_line_pointer--;
1182 break;
1184 #ifdef TC_M68K
1185 case '%':
1186 if (! flag_m68k_mri)
1187 goto de_fault;
1188 integer_constant (2, expressionP);
1189 break;
1191 case '@':
1192 if (! flag_m68k_mri)
1193 goto de_fault;
1194 integer_constant (8, expressionP);
1195 break;
1197 case ':':
1198 if (! flag_m68k_mri)
1199 goto de_fault;
1201 /* In MRI mode, this is a floating point constant represented
1202 using hexadecimal digits. */
1204 ++input_line_pointer;
1205 integer_constant (16, expressionP);
1206 break;
1208 case '*':
1209 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1210 goto de_fault;
1212 current_location (expressionP);
1213 break;
1214 #endif
1216 default:
1217 #if defined(md_need_index_operator) || defined(TC_M68K)
1218 de_fault:
1219 #endif
1220 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1222 /* Identifier begins here.
1223 This is kludged for speed, so code is repeated. */
1224 isname:
1225 name = --input_line_pointer;
1226 c = get_symbol_end ();
1228 #ifdef md_operator
1230 operatorT op = md_operator (name, 1, &c);
1232 switch (op)
1234 case O_uminus:
1235 *input_line_pointer = c;
1236 c = '-';
1237 goto unary;
1238 case O_bit_not:
1239 *input_line_pointer = c;
1240 c = '~';
1241 goto unary;
1242 case O_logical_not:
1243 *input_line_pointer = c;
1244 c = '!';
1245 goto unary;
1246 case O_illegal:
1247 as_bad (_("invalid use of operator \"%s\""), name);
1248 break;
1249 default:
1250 break;
1252 if (op != O_absent && op != O_illegal)
1254 *input_line_pointer = c;
1255 expr (9, expressionP, mode);
1256 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1257 expressionP->X_op_symbol = NULL;
1258 expressionP->X_add_number = 0;
1259 expressionP->X_op = op;
1260 break;
1263 #endif
1265 #ifdef md_parse_name
1266 /* This is a hook for the backend to parse certain names
1267 specially in certain contexts. If a name always has a
1268 specific value, it can often be handled by simply
1269 entering it in the symbol table. */
1270 if (md_parse_name (name, expressionP, mode, &c))
1272 *input_line_pointer = c;
1273 break;
1275 #endif
1277 #ifdef TC_I960
1278 /* The MRI i960 assembler permits
1279 lda sizeof code,g13
1280 FIXME: This should use md_parse_name. */
1281 if (flag_mri
1282 && (strcasecmp (name, "sizeof") == 0
1283 || strcasecmp (name, "startof") == 0))
1285 int start;
1286 char *buf;
1288 start = (name[1] == 't'
1289 || name[1] == 'T');
1291 *input_line_pointer = c;
1292 SKIP_WHITESPACE ();
1294 name = input_line_pointer;
1295 c = get_symbol_end ();
1297 buf = (char *) xmalloc (strlen (name) + 10);
1298 if (start)
1299 sprintf (buf, ".startof.%s", name);
1300 else
1301 sprintf (buf, ".sizeof.%s", name);
1302 symbolP = symbol_make (buf);
1303 free (buf);
1305 expressionP->X_op = O_symbol;
1306 expressionP->X_add_symbol = symbolP;
1307 expressionP->X_add_number = 0;
1309 *input_line_pointer = c;
1310 SKIP_WHITESPACE ();
1312 break;
1314 #endif
1316 symbolP = symbol_find_or_make (name);
1318 /* If we have an absolute symbol or a reg, then we know its
1319 value now. */
1320 segment = S_GET_SEGMENT (symbolP);
1321 if (mode != expr_defer && segment == absolute_section)
1323 expressionP->X_op = O_constant;
1324 expressionP->X_add_number = S_GET_VALUE (symbolP);
1326 else if (mode != expr_defer && segment == reg_section)
1328 expressionP->X_op = O_register;
1329 expressionP->X_add_number = S_GET_VALUE (symbolP);
1331 else
1333 expressionP->X_op = O_symbol;
1334 expressionP->X_add_symbol = symbolP;
1335 expressionP->X_add_number = 0;
1337 *input_line_pointer = c;
1339 else
1341 /* Let the target try to parse it. Success is indicated by changing
1342 the X_op field to something other than O_absent and pointing
1343 input_line_pointer past the expression. If it can't parse the
1344 expression, X_op and input_line_pointer should be unchanged. */
1345 expressionP->X_op = O_absent;
1346 --input_line_pointer;
1347 md_operand (expressionP);
1348 if (expressionP->X_op == O_absent)
1350 ++input_line_pointer;
1351 as_bad (_("bad expression"));
1352 expressionP->X_op = O_constant;
1353 expressionP->X_add_number = 0;
1356 break;
1359 /* It is more 'efficient' to clean up the expressionS when they are
1360 created. Doing it here saves lines of code. */
1361 clean_up_expression (expressionP);
1362 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1363 know (*input_line_pointer != ' ');
1365 /* The PA port needs this information. */
1366 if (expressionP->X_add_symbol)
1367 symbol_mark_used (expressionP->X_add_symbol);
1369 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1370 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1372 switch (expressionP->X_op)
1374 default:
1375 return absolute_section;
1376 case O_symbol:
1377 return S_GET_SEGMENT (expressionP->X_add_symbol);
1378 case O_register:
1379 return reg_section;
1383 /* Internal. Simplify a struct expression for use by expr (). */
1385 /* In: address of an expressionS.
1386 The X_op field of the expressionS may only take certain values.
1387 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1389 Out: expressionS may have been modified:
1390 Unused fields zeroed to help expr (). */
1392 static void
1393 clean_up_expression (expressionS *expressionP)
1395 switch (expressionP->X_op)
1397 case O_illegal:
1398 case O_absent:
1399 expressionP->X_add_number = 0;
1400 /* Fall through. */
1401 case O_big:
1402 case O_constant:
1403 case O_register:
1404 expressionP->X_add_symbol = NULL;
1405 /* Fall through. */
1406 case O_symbol:
1407 case O_uminus:
1408 case O_bit_not:
1409 expressionP->X_op_symbol = NULL;
1410 break;
1411 default:
1412 break;
1416 /* Expression parser. */
1418 /* We allow an empty expression, and just assume (absolute,0) silently.
1419 Unary operators and parenthetical expressions are treated as operands.
1420 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1422 We used to do an aho/ullman shift-reduce parser, but the logic got so
1423 warped that I flushed it and wrote a recursive-descent parser instead.
1424 Now things are stable, would anybody like to write a fast parser?
1425 Most expressions are either register (which does not even reach here)
1426 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1427 So I guess it doesn't really matter how inefficient more complex expressions
1428 are parsed.
1430 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1431 Also, we have consumed any leading or trailing spaces (operand does that)
1432 and done all intervening operators.
1434 This returns the segment of the result, which will be
1435 absolute_section or the segment of a symbol. */
1437 #undef __
1438 #define __ O_illegal
1439 #ifndef O_SINGLE_EQ
1440 #define O_SINGLE_EQ O_illegal
1441 #endif
1443 /* Maps ASCII -> operators. */
1444 static const operatorT op_encoding[256] = {
1445 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1446 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1448 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1449 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1450 __, __, __, __, __, __, __, __,
1451 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1452 __, __, __, __, __, __, __, __,
1453 __, __, __, __, __, __, __, __,
1454 __, __, __, __, __, __, __, __,
1455 __, __, __,
1456 #ifdef NEED_INDEX_OPERATOR
1457 O_index,
1458 #else
1460 #endif
1461 __, __, O_bit_exclusive_or, __,
1462 __, __, __, __, __, __, __, __,
1463 __, __, __, __, __, __, __, __,
1464 __, __, __, __, __, __, __, __,
1465 __, __, __, __, O_bit_inclusive_or, __, __, __,
1467 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1468 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1469 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1470 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1471 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1472 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1473 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1474 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1477 /* Rank Examples
1478 0 operand, (expression)
1479 1 ||
1480 2 &&
1481 3 == <> < <= >= >
1482 4 + -
1483 5 used for * / % in MRI mode
1484 6 & ^ ! |
1485 7 * / % << >>
1486 8 unary - unary ~
1488 static operator_rankT op_rank[O_max] = {
1489 0, /* O_illegal */
1490 0, /* O_absent */
1491 0, /* O_constant */
1492 0, /* O_symbol */
1493 0, /* O_symbol_rva */
1494 0, /* O_register */
1495 0, /* O_big */
1496 9, /* O_uminus */
1497 9, /* O_bit_not */
1498 9, /* O_logical_not */
1499 8, /* O_multiply */
1500 8, /* O_divide */
1501 8, /* O_modulus */
1502 8, /* O_left_shift */
1503 8, /* O_right_shift */
1504 7, /* O_bit_inclusive_or */
1505 7, /* O_bit_or_not */
1506 7, /* O_bit_exclusive_or */
1507 7, /* O_bit_and */
1508 5, /* O_add */
1509 5, /* O_subtract */
1510 4, /* O_eq */
1511 4, /* O_ne */
1512 4, /* O_lt */
1513 4, /* O_le */
1514 4, /* O_ge */
1515 4, /* O_gt */
1516 3, /* O_logical_and */
1517 2, /* O_logical_or */
1518 1, /* O_index */
1521 /* Unfortunately, in MRI mode for the m68k, multiplication and
1522 division have lower precedence than the bit wise operators. This
1523 function sets the operator precedences correctly for the current
1524 mode. Also, MRI uses a different bit_not operator, and this fixes
1525 that as well. */
1527 #define STANDARD_MUL_PRECEDENCE 8
1528 #define MRI_MUL_PRECEDENCE 6
1530 void
1531 expr_set_precedence (void)
1533 if (flag_m68k_mri)
1535 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1536 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1537 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1539 else
1541 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1542 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1543 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1547 void
1548 expr_set_rank (operatorT op, operator_rankT rank)
1550 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1551 op_rank[op] = rank;
1554 /* Initialize the expression parser. */
1556 void
1557 expr_begin (void)
1559 expr_set_precedence ();
1561 /* Verify that X_op field is wide enough. */
1563 expressionS e;
1564 e.X_op = O_max;
1565 gas_assert (e.X_op == O_max);
1569 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1570 sets NUM_CHARS to the number of characters in the operator.
1571 Does not advance INPUT_LINE_POINTER. */
1573 static inline operatorT
1574 operatorf (int *num_chars)
1576 int c;
1577 operatorT ret;
1579 c = *input_line_pointer & 0xff;
1580 *num_chars = 1;
1582 if (is_end_of_line[c])
1583 return O_illegal;
1585 #ifdef md_operator
1586 if (is_name_beginner (c))
1588 char *name = input_line_pointer;
1589 char ec = get_symbol_end ();
1591 ret = md_operator (name, 2, &ec);
1592 switch (ret)
1594 case O_absent:
1595 *input_line_pointer = ec;
1596 input_line_pointer = name;
1597 break;
1598 case O_uminus:
1599 case O_bit_not:
1600 case O_logical_not:
1601 as_bad (_("invalid use of operator \"%s\""), name);
1602 ret = O_illegal;
1603 /* FALLTHROUGH */
1604 default:
1605 *input_line_pointer = ec;
1606 *num_chars = input_line_pointer - name;
1607 input_line_pointer = name;
1608 return ret;
1611 #endif
1613 switch (c)
1615 default:
1616 ret = op_encoding[c];
1617 #ifdef md_operator
1618 if (ret == O_illegal)
1620 char *start = input_line_pointer;
1622 ret = md_operator (NULL, 2, NULL);
1623 if (ret != O_illegal)
1624 *num_chars = input_line_pointer - start;
1625 input_line_pointer = start;
1627 #endif
1628 return ret;
1630 case '+':
1631 case '-':
1632 return op_encoding[c];
1634 case '<':
1635 switch (input_line_pointer[1])
1637 default:
1638 return op_encoding[c];
1639 case '<':
1640 ret = O_left_shift;
1641 break;
1642 case '>':
1643 ret = O_ne;
1644 break;
1645 case '=':
1646 ret = O_le;
1647 break;
1649 *num_chars = 2;
1650 return ret;
1652 case '=':
1653 if (input_line_pointer[1] != '=')
1654 return op_encoding[c];
1656 *num_chars = 2;
1657 return O_eq;
1659 case '>':
1660 switch (input_line_pointer[1])
1662 default:
1663 return op_encoding[c];
1664 case '>':
1665 ret = O_right_shift;
1666 break;
1667 case '=':
1668 ret = O_ge;
1669 break;
1671 *num_chars = 2;
1672 return ret;
1674 case '!':
1675 switch (input_line_pointer[1])
1677 case '!':
1678 /* We accept !! as equivalent to ^ for MRI compatibility. */
1679 *num_chars = 2;
1680 return O_bit_exclusive_or;
1681 case '=':
1682 /* We accept != as equivalent to <>. */
1683 *num_chars = 2;
1684 return O_ne;
1685 default:
1686 if (flag_m68k_mri)
1687 return O_bit_inclusive_or;
1688 return op_encoding[c];
1691 case '|':
1692 if (input_line_pointer[1] != '|')
1693 return op_encoding[c];
1695 *num_chars = 2;
1696 return O_logical_or;
1698 case '&':
1699 if (input_line_pointer[1] != '&')
1700 return op_encoding[c];
1702 *num_chars = 2;
1703 return O_logical_and;
1706 /* NOTREACHED */
1709 /* Parse an expression. */
1711 segT
1712 expr (int rankarg, /* Larger # is higher rank. */
1713 expressionS *resultP, /* Deliver result here. */
1714 enum expr_mode mode /* Controls behavior. */)
1716 operator_rankT rank = (operator_rankT) rankarg;
1717 segT retval;
1718 expressionS right;
1719 operatorT op_left;
1720 operatorT op_right;
1721 int op_chars;
1723 know (rankarg >= 0);
1725 /* Save the value of dot for the fixup code. */
1726 if (rank == 0)
1727 dot_value = frag_now_fix ();
1729 retval = operand (resultP, mode);
1731 /* operand () gobbles spaces. */
1732 know (*input_line_pointer != ' ');
1734 op_left = operatorf (&op_chars);
1735 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1737 segT rightseg;
1738 bfd_vma frag_off;
1740 input_line_pointer += op_chars; /* -> after operator. */
1742 right.X_md = 0;
1743 rightseg = expr (op_rank[(int) op_left], &right, mode);
1744 if (right.X_op == O_absent)
1746 as_warn (_("missing operand; zero assumed"));
1747 right.X_op = O_constant;
1748 right.X_add_number = 0;
1749 right.X_add_symbol = NULL;
1750 right.X_op_symbol = NULL;
1753 know (*input_line_pointer != ' ');
1755 if (op_left == O_index)
1757 if (*input_line_pointer != ']')
1758 as_bad ("missing right bracket");
1759 else
1761 ++input_line_pointer;
1762 SKIP_WHITESPACE ();
1766 op_right = operatorf (&op_chars);
1768 know (op_right == O_illegal || op_left == O_index
1769 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1770 know ((int) op_left >= (int) O_multiply);
1771 #ifndef md_operator
1772 know ((int) op_left <= (int) O_index);
1773 #else
1774 know ((int) op_left < (int) O_max);
1775 #endif
1777 /* input_line_pointer->after right-hand quantity. */
1778 /* left-hand quantity in resultP. */
1779 /* right-hand quantity in right. */
1780 /* operator in op_left. */
1782 if (resultP->X_op == O_big)
1784 if (resultP->X_add_number > 0)
1785 as_warn (_("left operand is a bignum; integer 0 assumed"));
1786 else
1787 as_warn (_("left operand is a float; integer 0 assumed"));
1788 resultP->X_op = O_constant;
1789 resultP->X_add_number = 0;
1790 resultP->X_add_symbol = NULL;
1791 resultP->X_op_symbol = NULL;
1793 if (right.X_op == O_big)
1795 if (right.X_add_number > 0)
1796 as_warn (_("right operand is a bignum; integer 0 assumed"));
1797 else
1798 as_warn (_("right operand is a float; integer 0 assumed"));
1799 right.X_op = O_constant;
1800 right.X_add_number = 0;
1801 right.X_add_symbol = NULL;
1802 right.X_op_symbol = NULL;
1805 /* Optimize common cases. */
1806 #ifdef md_optimize_expr
1807 if (md_optimize_expr (resultP, op_left, &right))
1809 /* Skip. */
1812 else
1813 #endif
1814 #ifndef md_register_arithmetic
1815 # define md_register_arithmetic 1
1816 #endif
1817 if (op_left == O_add && right.X_op == O_constant
1818 && (md_register_arithmetic || resultP->X_op != O_register))
1820 /* X + constant. */
1821 resultP->X_add_number += right.X_add_number;
1823 /* This case comes up in PIC code. */
1824 else if (op_left == O_subtract
1825 && right.X_op == O_symbol
1826 && resultP->X_op == O_symbol
1827 && retval == rightseg
1828 #ifdef md_allow_local_subtract
1829 && md_allow_local_subtract (resultP, & right, rightseg)
1830 #endif
1831 && (SEG_NORMAL (rightseg)
1832 || right.X_add_symbol == resultP->X_add_symbol)
1833 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1834 symbol_get_frag (right.X_add_symbol),
1835 &frag_off))
1837 resultP->X_add_number -= right.X_add_number;
1838 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1839 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1840 - S_GET_VALUE (right.X_add_symbol));
1841 resultP->X_op = O_constant;
1842 resultP->X_add_symbol = 0;
1844 else if (op_left == O_subtract && right.X_op == O_constant
1845 && (md_register_arithmetic || resultP->X_op != O_register))
1847 /* X - constant. */
1848 resultP->X_add_number -= right.X_add_number;
1850 else if (op_left == O_add && resultP->X_op == O_constant
1851 && (md_register_arithmetic || right.X_op != O_register))
1853 /* Constant + X. */
1854 resultP->X_op = right.X_op;
1855 resultP->X_add_symbol = right.X_add_symbol;
1856 resultP->X_op_symbol = right.X_op_symbol;
1857 resultP->X_add_number += right.X_add_number;
1858 retval = rightseg;
1860 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1862 /* Constant OP constant. */
1863 offsetT v = right.X_add_number;
1864 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1866 as_warn (_("division by zero"));
1867 v = 1;
1869 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1870 && (op_left == O_left_shift || op_left == O_right_shift))
1872 as_warn_value_out_of_range (_("shift count"), v, 0,
1873 sizeof(valueT) * CHAR_BIT - 1,
1874 NULL, 0);
1875 resultP->X_add_number = v = 0;
1877 switch (op_left)
1879 default: goto general;
1880 case O_multiply: resultP->X_add_number *= v; break;
1881 case O_divide: resultP->X_add_number /= v; break;
1882 case O_modulus: resultP->X_add_number %= v; break;
1883 case O_left_shift: resultP->X_add_number <<= v; break;
1884 case O_right_shift:
1885 /* We always use unsigned shifts, to avoid relying on
1886 characteristics of the compiler used to compile gas. */
1887 resultP->X_add_number =
1888 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1889 break;
1890 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1891 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1892 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1893 case O_bit_and: resultP->X_add_number &= v; break;
1894 /* Constant + constant (O_add) is handled by the
1895 previous if statement for constant + X, so is omitted
1896 here. */
1897 case O_subtract: resultP->X_add_number -= v; break;
1898 case O_eq:
1899 resultP->X_add_number =
1900 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1901 break;
1902 case O_ne:
1903 resultP->X_add_number =
1904 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1905 break;
1906 case O_lt:
1907 resultP->X_add_number =
1908 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1909 break;
1910 case O_le:
1911 resultP->X_add_number =
1912 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1913 break;
1914 case O_ge:
1915 resultP->X_add_number =
1916 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1917 break;
1918 case O_gt:
1919 resultP->X_add_number =
1920 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1921 break;
1922 case O_logical_and:
1923 resultP->X_add_number = resultP->X_add_number && v;
1924 break;
1925 case O_logical_or:
1926 resultP->X_add_number = resultP->X_add_number || v;
1927 break;
1930 else if (resultP->X_op == O_symbol
1931 && right.X_op == O_symbol
1932 && (op_left == O_add
1933 || op_left == O_subtract
1934 || (resultP->X_add_number == 0
1935 && right.X_add_number == 0)))
1937 /* Symbol OP symbol. */
1938 resultP->X_op = op_left;
1939 resultP->X_op_symbol = right.X_add_symbol;
1940 if (op_left == O_add)
1941 resultP->X_add_number += right.X_add_number;
1942 else if (op_left == O_subtract)
1944 resultP->X_add_number -= right.X_add_number;
1945 if (retval == rightseg && SEG_NORMAL (retval))
1947 retval = absolute_section;
1948 rightseg = absolute_section;
1952 else
1954 general:
1955 /* The general case. */
1956 resultP->X_add_symbol = make_expr_symbol (resultP);
1957 resultP->X_op_symbol = make_expr_symbol (&right);
1958 resultP->X_op = op_left;
1959 resultP->X_add_number = 0;
1960 resultP->X_unsigned = 1;
1963 if (retval != rightseg)
1965 if (! SEG_NORMAL (retval))
1967 if (retval != undefined_section || SEG_NORMAL (rightseg))
1968 retval = rightseg;
1970 else if (SEG_NORMAL (rightseg)
1971 #ifdef DIFF_EXPR_OK
1972 && op_left != O_subtract
1973 #endif
1975 as_bad (_("operation combines symbols in different segments"));
1978 op_left = op_right;
1979 } /* While next operator is >= this rank. */
1981 /* The PA port needs this information. */
1982 if (resultP->X_add_symbol)
1983 symbol_mark_used (resultP->X_add_symbol);
1985 if (rank == 0 && mode == expr_evaluate)
1986 resolve_expression (resultP);
1988 return resultP->X_op == O_constant ? absolute_section : retval;
1991 /* Resolve an expression without changing any symbols/sub-expressions
1992 used. */
1995 resolve_expression (expressionS *expressionP)
1997 /* Help out with CSE. */
1998 valueT final_val = expressionP->X_add_number;
1999 symbolS *add_symbol = expressionP->X_add_symbol;
2000 symbolS *orig_add_symbol = add_symbol;
2001 symbolS *op_symbol = expressionP->X_op_symbol;
2002 operatorT op = expressionP->X_op;
2003 valueT left, right;
2004 segT seg_left, seg_right;
2005 fragS *frag_left, *frag_right;
2006 bfd_vma frag_off;
2008 switch (op)
2010 default:
2011 return 0;
2013 case O_constant:
2014 case O_register:
2015 left = 0;
2016 break;
2018 case O_symbol:
2019 case O_symbol_rva:
2020 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2021 return 0;
2023 break;
2025 case O_uminus:
2026 case O_bit_not:
2027 case O_logical_not:
2028 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2029 return 0;
2031 if (seg_left != absolute_section)
2032 return 0;
2034 if (op == O_logical_not)
2035 left = !left;
2036 else if (op == O_uminus)
2037 left = -left;
2038 else
2039 left = ~left;
2040 op = O_constant;
2041 break;
2043 case O_multiply:
2044 case O_divide:
2045 case O_modulus:
2046 case O_left_shift:
2047 case O_right_shift:
2048 case O_bit_inclusive_or:
2049 case O_bit_or_not:
2050 case O_bit_exclusive_or:
2051 case O_bit_and:
2052 case O_add:
2053 case O_subtract:
2054 case O_eq:
2055 case O_ne:
2056 case O_lt:
2057 case O_le:
2058 case O_ge:
2059 case O_gt:
2060 case O_logical_and:
2061 case O_logical_or:
2062 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2063 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2064 return 0;
2066 /* Simplify addition or subtraction of a constant by folding the
2067 constant into X_add_number. */
2068 if (op == O_add)
2070 if (seg_right == absolute_section)
2072 final_val += right;
2073 op = O_symbol;
2074 break;
2076 else if (seg_left == absolute_section)
2078 final_val += left;
2079 left = right;
2080 seg_left = seg_right;
2081 add_symbol = op_symbol;
2082 orig_add_symbol = expressionP->X_op_symbol;
2083 op = O_symbol;
2084 break;
2087 else if (op == O_subtract)
2089 if (seg_right == absolute_section)
2091 final_val -= right;
2092 op = O_symbol;
2093 break;
2097 /* Equality and non-equality tests are permitted on anything.
2098 Subtraction, and other comparison operators are permitted if
2099 both operands are in the same section.
2100 Shifts by constant zero are permitted on anything.
2101 Multiplies, bit-ors, and bit-ands with constant zero are
2102 permitted on anything.
2103 Multiplies and divides by constant one are permitted on
2104 anything.
2105 Binary operations with both operands being the same register
2106 or undefined symbol are permitted if the result doesn't depend
2107 on the input value.
2108 Otherwise, both operands must be absolute. We already handled
2109 the case of addition or subtraction of a constant above. */
2110 frag_off = 0;
2111 if (!(seg_left == absolute_section
2112 && seg_right == absolute_section)
2113 && !(op == O_eq || op == O_ne)
2114 && !((op == O_subtract
2115 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2116 && seg_left == seg_right
2117 && (finalize_syms
2118 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2119 && (seg_left != reg_section || left == right)
2120 && (seg_left != undefined_section || add_symbol == op_symbol)))
2122 if ((seg_left == absolute_section && left == 0)
2123 || (seg_right == absolute_section && right == 0))
2125 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2127 if (!(seg_right == absolute_section && right == 0))
2129 seg_left = seg_right;
2130 left = right;
2131 add_symbol = op_symbol;
2132 orig_add_symbol = expressionP->X_op_symbol;
2134 op = O_symbol;
2135 break;
2137 else if (op == O_left_shift || op == O_right_shift)
2139 if (!(seg_left == absolute_section && left == 0))
2141 op = O_symbol;
2142 break;
2145 else if (op != O_multiply
2146 && op != O_bit_or_not && op != O_bit_and)
2147 return 0;
2149 else if (op == O_multiply
2150 && seg_left == absolute_section && left == 1)
2152 seg_left = seg_right;
2153 left = right;
2154 add_symbol = op_symbol;
2155 orig_add_symbol = expressionP->X_op_symbol;
2156 op = O_symbol;
2157 break;
2159 else if ((op == O_multiply || op == O_divide)
2160 && seg_right == absolute_section && right == 1)
2162 op = O_symbol;
2163 break;
2165 else if (!(left == right
2166 && ((seg_left == reg_section && seg_right == reg_section)
2167 || (seg_left == undefined_section
2168 && seg_right == undefined_section
2169 && add_symbol == op_symbol))))
2170 return 0;
2171 else if (op == O_bit_and || op == O_bit_inclusive_or)
2173 op = O_symbol;
2174 break;
2176 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2177 return 0;
2180 right += frag_off / OCTETS_PER_BYTE;
2181 switch (op)
2183 case O_add: left += right; break;
2184 case O_subtract: left -= right; break;
2185 case O_multiply: left *= right; break;
2186 case O_divide:
2187 if (right == 0)
2188 return 0;
2189 left = (offsetT) left / (offsetT) right;
2190 break;
2191 case O_modulus:
2192 if (right == 0)
2193 return 0;
2194 left = (offsetT) left % (offsetT) right;
2195 break;
2196 case O_left_shift: left <<= right; break;
2197 case O_right_shift: left >>= right; break;
2198 case O_bit_inclusive_or: left |= right; break;
2199 case O_bit_or_not: left |= ~right; break;
2200 case O_bit_exclusive_or: left ^= right; break;
2201 case O_bit_and: left &= right; break;
2202 case O_eq:
2203 case O_ne:
2204 left = (left == right
2205 && seg_left == seg_right
2206 && (finalize_syms || frag_left == frag_right)
2207 && (seg_left != undefined_section
2208 || add_symbol == op_symbol)
2209 ? ~ (valueT) 0 : 0);
2210 if (op == O_ne)
2211 left = ~left;
2212 break;
2213 case O_lt:
2214 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2215 break;
2216 case O_le:
2217 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2218 break;
2219 case O_ge:
2220 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2221 break;
2222 case O_gt:
2223 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2224 break;
2225 case O_logical_and: left = left && right; break;
2226 case O_logical_or: left = left || right; break;
2227 default: abort ();
2230 op = O_constant;
2231 break;
2234 if (op == O_symbol)
2236 if (seg_left == absolute_section)
2237 op = O_constant;
2238 else if (seg_left == reg_section && final_val == 0)
2239 op = O_register;
2240 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2241 final_val += left;
2242 expressionP->X_add_symbol = add_symbol;
2244 expressionP->X_op = op;
2246 if (op == O_constant || op == O_register)
2247 final_val += left;
2248 expressionP->X_add_number = final_val;
2250 return 1;
2253 /* This lives here because it belongs equally in expr.c & read.c.
2254 expr.c is just a branch office read.c anyway, and putting it
2255 here lessens the crowd at read.c.
2257 Assume input_line_pointer is at start of symbol name.
2258 Advance input_line_pointer past symbol name.
2259 Turn that character into a '\0', returning its former value.
2260 This allows a string compare (RMS wants symbol names to be strings)
2261 of the symbol name.
2262 There will always be a char following symbol name, because all good
2263 lines end in end-of-line. */
2265 char
2266 get_symbol_end (void)
2268 char c;
2270 /* We accept \001 in a name in case this is being called with a
2271 constructed string. */
2272 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2274 while (is_part_of_name (c = *input_line_pointer++)
2275 || c == '\001')
2277 if (is_name_ender (c))
2278 c = *input_line_pointer++;
2280 *--input_line_pointer = 0;
2281 return (c);
2284 unsigned int
2285 get_single_number (void)
2287 expressionS exp;
2288 operand (&exp, expr_normal);
2289 return exp.X_add_number;