* config/tc-mips.h (mips_flush_pending_output): Delete.
[binutils.git] / gas / expr.c
blobfb29fddabe6ac42f375010f9fb15c44c43a7b8de
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
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 2, 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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 #include <string.h>
29 #define min(a, b) ((a) < (b) ? (a) : (b))
31 #include "as.h"
32 #include "safe-ctype.h"
33 #include "obstack.h"
35 static void floating_constant (expressionS * expressionP);
36 static valueT generic_bignum_to_int32 (void);
37 #ifdef BFD64
38 static valueT generic_bignum_to_int64 (void);
39 #endif
40 static void integer_constant (int radix, expressionS * expressionP);
41 static void mri_char_constant (expressionS *);
42 static void current_location (expressionS *);
43 static void clean_up_expression (expressionS * expressionP);
44 static segT operand (expressionS *);
45 static operatorT operator (int *);
47 extern const char EXP_CHARS[], FLT_CHARS[];
49 /* We keep a mapping of expression symbols to file positions, so that
50 we can provide better error messages. */
52 struct expr_symbol_line {
53 struct expr_symbol_line *next;
54 symbolS *sym;
55 char *file;
56 unsigned int line;
59 static struct expr_symbol_line *expr_symbol_lines;
61 /* Build a dummy symbol to hold a complex expression. This is how we
62 build expressions up out of other expressions. The symbol is put
63 into the fake section expr_section. */
65 symbolS *
66 make_expr_symbol (expressionS *expressionP)
68 expressionS zero;
69 symbolS *symbolP;
70 struct expr_symbol_line *n;
72 if (expressionP->X_op == O_symbol
73 && expressionP->X_add_number == 0)
74 return expressionP->X_add_symbol;
76 if (expressionP->X_op == O_big)
78 /* This won't work, because the actual value is stored in
79 generic_floating_point_number or generic_bignum, and we are
80 going to lose it if we haven't already. */
81 if (expressionP->X_add_number > 0)
82 as_bad (_("bignum invalid"));
83 else
84 as_bad (_("floating point number invalid"));
85 zero.X_op = O_constant;
86 zero.X_add_number = 0;
87 zero.X_unsigned = 0;
88 clean_up_expression (&zero);
89 expressionP = &zero;
92 /* Putting constant symbols in absolute_section rather than
93 expr_section is convenient for the old a.out code, for which
94 S_GET_SEGMENT does not always retrieve the value put in by
95 S_SET_SEGMENT. */
96 symbolP = symbol_create (FAKE_LABEL_NAME,
97 (expressionP->X_op == O_constant
98 ? absolute_section
99 : expr_section),
100 0, &zero_address_frag);
101 symbol_set_value_expression (symbolP, expressionP);
103 if (expressionP->X_op == O_constant)
104 resolve_symbol_value (symbolP);
106 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
107 n->sym = symbolP;
108 as_where (&n->file, &n->line);
109 n->next = expr_symbol_lines;
110 expr_symbol_lines = n;
112 return symbolP;
115 /* Return the file and line number for an expr symbol. Return
116 non-zero if something was found, 0 if no information is known for
117 the symbol. */
120 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
122 register struct expr_symbol_line *l;
124 for (l = expr_symbol_lines; l != NULL; l = l->next)
126 if (l->sym == sym)
128 *pfile = l->file;
129 *pline = l->line;
130 return 1;
134 return 0;
137 /* Utilities for building expressions.
138 Since complex expressions are recorded as symbols for use in other
139 expressions these return a symbolS * and not an expressionS *.
140 These explicitly do not take an "add_number" argument. */
141 /* ??? For completeness' sake one might want expr_build_symbol.
142 It would just return its argument. */
144 /* Build an expression for an unsigned constant.
145 The corresponding one for signed constants is missing because
146 there's currently no need for it. One could add an unsigned_p flag
147 but that seems more clumsy. */
149 symbolS *
150 expr_build_uconstant (offsetT value)
152 expressionS e;
154 e.X_op = O_constant;
155 e.X_add_number = value;
156 e.X_unsigned = 1;
157 return make_expr_symbol (&e);
160 /* Build an expression for OP s1. */
162 symbolS *
163 expr_build_unary (operatorT op, symbolS *s1)
165 expressionS e;
167 e.X_op = op;
168 e.X_add_symbol = s1;
169 e.X_add_number = 0;
170 return make_expr_symbol (&e);
173 /* Build an expression for s1 OP s2. */
175 symbolS *
176 expr_build_binary (operatorT op, symbolS *s1, symbolS *s2)
178 expressionS e;
180 e.X_op = op;
181 e.X_add_symbol = s1;
182 e.X_op_symbol = s2;
183 e.X_add_number = 0;
184 return make_expr_symbol (&e);
187 /* Build an expression for the current location ('.'). */
189 symbolS *
190 expr_build_dot (void)
192 expressionS e;
194 current_location (&e);
195 return make_expr_symbol (&e);
198 /* Build any floating-point literal here.
199 Also build any bignum literal here. */
201 /* Seems atof_machine can backscan through generic_bignum and hit whatever
202 happens to be loaded before it in memory. And its way too complicated
203 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
204 and never write into the early words, thus they'll always be zero.
205 I hate Dean's floating-point code. Bleh. */
206 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
208 FLONUM_TYPE generic_floating_point_number = {
209 &generic_bignum[6], /* low. (JF: Was 0) */
210 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
211 0, /* leader. */
212 0, /* exponent. */
213 0 /* sign. */
216 /* If nonzero, we've been asked to assemble nan, +inf or -inf. */
217 int generic_floating_point_magic;
219 static void
220 floating_constant (expressionS *expressionP)
222 /* input_line_pointer -> floating-point constant. */
223 int error_code;
225 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
226 &generic_floating_point_number);
228 if (error_code)
230 if (error_code == ERROR_EXPONENT_OVERFLOW)
232 as_bad (_("bad floating-point constant: exponent overflow"));
234 else
236 as_bad (_("bad floating-point constant: unknown error code=%d"),
237 error_code);
240 expressionP->X_op = O_big;
241 /* input_line_pointer -> just after constant, which may point to
242 whitespace. */
243 expressionP->X_add_number = -1;
246 static valueT
247 generic_bignum_to_int32 (void)
249 valueT number =
250 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
251 | (generic_bignum[0] & LITTLENUM_MASK);
252 number &= 0xffffffff;
253 return number;
256 #ifdef BFD64
257 static valueT
258 generic_bignum_to_int64 (void)
260 valueT number =
261 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
262 << LITTLENUM_NUMBER_OF_BITS)
263 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
264 << LITTLENUM_NUMBER_OF_BITS)
265 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
266 << LITTLENUM_NUMBER_OF_BITS)
267 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
268 return number;
270 #endif
272 static void
273 integer_constant (int radix, expressionS *expressionP)
275 char *start; /* Start of number. */
276 char *suffix = NULL;
277 char c;
278 valueT number; /* Offset or (absolute) value. */
279 short int digit; /* Value of next digit in current radix. */
280 short int maxdig = 0; /* Highest permitted digit value. */
281 int too_many_digits = 0; /* If we see >= this number of. */
282 char *name; /* Points to name of symbol. */
283 symbolS *symbolP; /* Points to symbol. */
285 int small; /* True if fits in 32 bits. */
287 /* May be bignum, or may fit in 32 bits. */
288 /* Most numbers fit into 32 bits, and we want this case to be fast.
289 so we pretend it will fit into 32 bits. If, after making up a 32
290 bit number, we realise that we have scanned more digits than
291 comfortably fit into 32 bits, we re-scan the digits coding them
292 into a bignum. For decimal and octal numbers we are
293 conservative: Some numbers may be assumed bignums when in fact
294 they do fit into 32 bits. Numbers of any radix can have excess
295 leading zeros: We strive to recognise this and cast them back
296 into 32 bits. We must check that the bignum really is more than
297 32 bits, and change it back to a 32-bit number if it fits. The
298 number we are looking for is expected to be positive, but if it
299 fits into 32 bits as an unsigned number, we let it be a 32-bit
300 number. The cavalier approach is for speed in ordinary cases. */
301 /* This has been extended for 64 bits. We blindly assume that if
302 you're compiling in 64-bit mode, the target is a 64-bit machine.
303 This should be cleaned up. */
305 #ifdef BFD64
306 #define valuesize 64
307 #else /* includes non-bfd case, mostly */
308 #define valuesize 32
309 #endif
311 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
313 int flt = 0;
315 /* In MRI mode, the number may have a suffix indicating the
316 radix. For that matter, it might actually be a floating
317 point constant. */
318 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
320 if (*suffix == 'e' || *suffix == 'E')
321 flt = 1;
324 if (suffix == input_line_pointer)
326 radix = 10;
327 suffix = NULL;
329 else
331 c = *--suffix;
332 c = TOUPPER (c);
333 if (c == 'B')
334 radix = 2;
335 else if (c == 'D')
336 radix = 10;
337 else if (c == 'O' || c == 'Q')
338 radix = 8;
339 else if (c == 'H')
340 radix = 16;
341 else if (suffix[1] == '.' || c == 'E' || flt)
343 floating_constant (expressionP);
344 return;
346 else
348 radix = 10;
349 suffix = NULL;
354 switch (radix)
356 case 2:
357 maxdig = 2;
358 too_many_digits = valuesize + 1;
359 break;
360 case 8:
361 maxdig = radix = 8;
362 too_many_digits = (valuesize + 2) / 3 + 1;
363 break;
364 case 16:
365 maxdig = radix = 16;
366 too_many_digits = (valuesize + 3) / 4 + 1;
367 break;
368 case 10:
369 maxdig = radix = 10;
370 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
372 #undef valuesize
373 start = input_line_pointer;
374 c = *input_line_pointer++;
375 for (number = 0;
376 (digit = hex_value (c)) < maxdig;
377 c = *input_line_pointer++)
379 number = number * radix + digit;
381 /* c contains character after number. */
382 /* input_line_pointer->char after c. */
383 small = (input_line_pointer - start - 1) < too_many_digits;
385 if (radix == 16 && c == '_')
387 /* This is literal of the form 0x333_0_12345678_1.
388 This example is equivalent to 0x00000333000000001234567800000001. */
390 int num_little_digits = 0;
391 int i;
392 input_line_pointer = start; /* -> 1st digit. */
394 know (LITTLENUM_NUMBER_OF_BITS == 16);
396 for (c = '_'; c == '_'; num_little_digits += 2)
399 /* Convert one 64-bit word. */
400 int ndigit = 0;
401 number = 0;
402 for (c = *input_line_pointer++;
403 (digit = hex_value (c)) < maxdig;
404 c = *(input_line_pointer++))
406 number = number * radix + digit;
407 ndigit++;
410 /* Check for 8 digit per word max. */
411 if (ndigit > 8)
412 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
414 /* Add this chunk to the bignum.
415 Shift things down 2 little digits. */
416 know (LITTLENUM_NUMBER_OF_BITS == 16);
417 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
418 i >= 2;
419 i--)
420 generic_bignum[i] = generic_bignum[i - 2];
422 /* Add the new digits as the least significant new ones. */
423 generic_bignum[0] = number & 0xffffffff;
424 generic_bignum[1] = number >> 16;
427 /* Again, c is char after number, input_line_pointer->after c. */
429 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
430 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
432 assert (num_little_digits >= 4);
434 if (num_little_digits != 8)
435 as_bad (_("a bignum with underscores must have exactly 4 words"));
437 /* We might have some leading zeros. These can be trimmed to give
438 us a change to fit this constant into a small number. */
439 while (generic_bignum[num_little_digits - 1] == 0
440 && num_little_digits > 1)
441 num_little_digits--;
443 if (num_little_digits <= 2)
445 /* will fit into 32 bits. */
446 number = generic_bignum_to_int32 ();
447 small = 1;
449 #ifdef BFD64
450 else if (num_little_digits <= 4)
452 /* Will fit into 64 bits. */
453 number = generic_bignum_to_int64 ();
454 small = 1;
456 #endif
457 else
459 small = 0;
461 /* Number of littlenums in the bignum. */
462 number = num_little_digits;
465 else if (!small)
467 /* We saw a lot of digits. manufacture a bignum the hard way. */
468 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
469 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
470 long carry;
472 leader = generic_bignum;
473 generic_bignum[0] = 0;
474 generic_bignum[1] = 0;
475 generic_bignum[2] = 0;
476 generic_bignum[3] = 0;
477 input_line_pointer = start; /* -> 1st digit. */
478 c = *input_line_pointer++;
479 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
481 for (pointer = generic_bignum; pointer <= leader; pointer++)
483 long work;
485 work = carry + radix * *pointer;
486 *pointer = work & LITTLENUM_MASK;
487 carry = work >> LITTLENUM_NUMBER_OF_BITS;
489 if (carry)
491 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
493 /* Room to grow a longer bignum. */
494 *++leader = carry;
498 /* Again, c is char after number. */
499 /* input_line_pointer -> after c. */
500 know (LITTLENUM_NUMBER_OF_BITS == 16);
501 if (leader < generic_bignum + 2)
503 /* Will fit into 32 bits. */
504 number = generic_bignum_to_int32 ();
505 small = 1;
507 #ifdef BFD64
508 else if (leader < generic_bignum + 4)
510 /* Will fit into 64 bits. */
511 number = generic_bignum_to_int64 ();
512 small = 1;
514 #endif
515 else
517 /* Number of littlenums in the bignum. */
518 number = leader - generic_bignum + 1;
522 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
523 && suffix != NULL
524 && input_line_pointer - 1 == suffix)
525 c = *input_line_pointer++;
527 if (small)
529 /* Here with number, in correct radix. c is the next char.
530 Note that unlike un*x, we allow "011f" "0x9f" to both mean
531 the same as the (conventional) "9f".
532 This is simply easier than checking for strict canonical
533 form. Syntax sux! */
535 if (LOCAL_LABELS_FB && c == 'b')
537 /* Backward ref to local label.
538 Because it is backward, expect it to be defined. */
539 /* Construct a local label. */
540 name = fb_label_name ((int) number, 0);
542 /* Seen before, or symbol is defined: OK. */
543 symbolP = symbol_find (name);
544 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
546 /* Local labels are never absolute. Don't waste time
547 checking absoluteness. */
548 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
550 expressionP->X_op = O_symbol;
551 expressionP->X_add_symbol = symbolP;
553 else
555 /* Either not seen or not defined. */
556 /* @@ Should print out the original string instead of
557 the parsed number. */
558 as_bad (_("backward ref to unknown label \"%d:\""),
559 (int) number);
560 expressionP->X_op = O_constant;
563 expressionP->X_add_number = 0;
564 } /* case 'b' */
565 else if (LOCAL_LABELS_FB && c == 'f')
567 /* Forward reference. Expect symbol to be undefined or
568 unknown. undefined: seen it before. unknown: never seen
569 it before.
571 Construct a local label name, then an undefined symbol.
572 Don't create a xseg frag for it: caller may do that.
573 Just return it as never seen before. */
574 name = fb_label_name ((int) number, 1);
575 symbolP = symbol_find_or_make (name);
576 /* We have no need to check symbol properties. */
577 #ifndef many_segments
578 /* Since "know" puts its arg into a "string", we
579 can't have newlines in the argument. */
580 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
581 #endif
582 expressionP->X_op = O_symbol;
583 expressionP->X_add_symbol = symbolP;
584 expressionP->X_add_number = 0;
585 } /* case 'f' */
586 else if (LOCAL_LABELS_DOLLAR && c == '$')
588 /* If the dollar label is *currently* defined, then this is just
589 another reference to it. If it is not *currently* defined,
590 then this is a fresh instantiation of that number, so create
591 it. */
593 if (dollar_label_defined ((long) number))
595 name = dollar_label_name ((long) number, 0);
596 symbolP = symbol_find (name);
597 know (symbolP != NULL);
599 else
601 name = dollar_label_name ((long) number, 1);
602 symbolP = symbol_find_or_make (name);
605 expressionP->X_op = O_symbol;
606 expressionP->X_add_symbol = symbolP;
607 expressionP->X_add_number = 0;
608 } /* case '$' */
609 else
611 expressionP->X_op = O_constant;
612 expressionP->X_add_number = number;
613 input_line_pointer--; /* Restore following character. */
614 } /* Really just a number. */
616 else
618 /* Not a small number. */
619 expressionP->X_op = O_big;
620 expressionP->X_add_number = number; /* Number of littlenums. */
621 input_line_pointer--; /* -> char following number. */
625 /* Parse an MRI multi character constant. */
627 static void
628 mri_char_constant (expressionS *expressionP)
630 int i;
632 if (*input_line_pointer == '\''
633 && input_line_pointer[1] != '\'')
635 expressionP->X_op = O_constant;
636 expressionP->X_add_number = 0;
637 return;
640 /* In order to get the correct byte ordering, we must build the
641 number in reverse. */
642 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
644 int j;
646 generic_bignum[i] = 0;
647 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
649 if (*input_line_pointer == '\'')
651 if (input_line_pointer[1] != '\'')
652 break;
653 ++input_line_pointer;
655 generic_bignum[i] <<= 8;
656 generic_bignum[i] += *input_line_pointer;
657 ++input_line_pointer;
660 if (i < SIZE_OF_LARGE_NUMBER - 1)
662 /* If there is more than one littlenum, left justify the
663 last one to make it match the earlier ones. If there is
664 only one, we can just use the value directly. */
665 for (; j < CHARS_PER_LITTLENUM; j++)
666 generic_bignum[i] <<= 8;
669 if (*input_line_pointer == '\''
670 && input_line_pointer[1] != '\'')
671 break;
674 if (i < 0)
676 as_bad (_("character constant too large"));
677 i = 0;
680 if (i > 0)
682 int c;
683 int j;
685 c = SIZE_OF_LARGE_NUMBER - i;
686 for (j = 0; j < c; j++)
687 generic_bignum[j] = generic_bignum[i + j];
688 i = c;
691 know (LITTLENUM_NUMBER_OF_BITS == 16);
692 if (i > 2)
694 expressionP->X_op = O_big;
695 expressionP->X_add_number = i;
697 else
699 expressionP->X_op = O_constant;
700 if (i < 2)
701 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
702 else
703 expressionP->X_add_number =
704 (((generic_bignum[1] & LITTLENUM_MASK)
705 << LITTLENUM_NUMBER_OF_BITS)
706 | (generic_bignum[0] & LITTLENUM_MASK));
709 /* Skip the final closing quote. */
710 ++input_line_pointer;
713 /* Return an expression representing the current location. This
714 handles the magic symbol `.'. */
716 static void
717 current_location (expressionS *expressionp)
719 if (now_seg == absolute_section)
721 expressionp->X_op = O_constant;
722 expressionp->X_add_number = abs_section_offset;
724 else
726 expressionp->X_op = O_symbol;
727 expressionp->X_add_symbol = symbol_temp_new_now ();
728 expressionp->X_add_number = 0;
732 /* In: Input_line_pointer points to 1st char of operand, which may
733 be a space.
735 Out: An expressionS.
736 The operand may have been empty: in this case X_op == O_absent.
737 Input_line_pointer->(next non-blank) char after operand. */
739 static segT
740 operand (expressionS *expressionP)
742 char c;
743 symbolS *symbolP; /* Points to symbol. */
744 char *name; /* Points to name of symbol. */
745 segT segment;
747 /* All integers are regarded as unsigned unless they are negated.
748 This is because the only thing which cares whether a number is
749 unsigned is the code in emit_expr which extends constants into
750 bignums. It should only sign extend negative numbers, so that
751 something like ``.quad 0x80000000'' is not sign extended even
752 though it appears negative if valueT is 32 bits. */
753 expressionP->X_unsigned = 1;
755 /* Digits, assume it is a bignum. */
757 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
758 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
760 if (is_end_of_line[(unsigned char) c])
761 goto eol;
763 switch (c)
765 case '1':
766 case '2':
767 case '3':
768 case '4':
769 case '5':
770 case '6':
771 case '7':
772 case '8':
773 case '9':
774 input_line_pointer--;
776 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
777 ? 0 : 10,
778 expressionP);
779 break;
781 #ifdef LITERAL_PREFIXDOLLAR_HEX
782 case '$':
783 /* $L is the start of a local label, not a hex constant. */
784 if (* input_line_pointer == 'L')
785 goto isname;
786 integer_constant (16, expressionP);
787 break;
788 #endif
790 #ifdef LITERAL_PREFIXPERCENT_BIN
791 case '%':
792 integer_constant (2, expressionP);
793 break;
794 #endif
796 case '0':
797 /* Non-decimal radix. */
799 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
801 char *s;
803 /* Check for a hex or float constant. */
804 for (s = input_line_pointer; hex_p (*s); s++)
806 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
808 --input_line_pointer;
809 integer_constant (0, expressionP);
810 break;
813 c = *input_line_pointer;
814 switch (c)
816 case 'o':
817 case 'O':
818 case 'q':
819 case 'Q':
820 case '8':
821 case '9':
822 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
824 integer_constant (0, expressionP);
825 break;
827 /* Fall through. */
828 default:
829 default_case:
830 if (c && strchr (FLT_CHARS, c))
832 input_line_pointer++;
833 floating_constant (expressionP);
834 expressionP->X_add_number = - TOLOWER (c);
836 else
838 /* The string was only zero. */
839 expressionP->X_op = O_constant;
840 expressionP->X_add_number = 0;
843 break;
845 case 'x':
846 case 'X':
847 if (flag_m68k_mri)
848 goto default_case;
849 input_line_pointer++;
850 integer_constant (16, expressionP);
851 break;
853 case 'b':
854 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
856 /* This code used to check for '+' and '-' here, and, in
857 some conditions, fall through to call
858 integer_constant. However, that didn't make sense,
859 as integer_constant only accepts digits. */
860 /* Some of our code elsewhere does permit digits greater
861 than the expected base; for consistency, do the same
862 here. */
863 if (input_line_pointer[1] < '0'
864 || input_line_pointer[1] > '9')
866 /* Parse this as a back reference to label 0. */
867 input_line_pointer--;
868 integer_constant (10, expressionP);
869 break;
871 /* Otherwise, parse this as a binary number. */
873 /* Fall through. */
874 case 'B':
875 input_line_pointer++;
876 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
877 goto default_case;
878 integer_constant (2, expressionP);
879 break;
881 case '0':
882 case '1':
883 case '2':
884 case '3':
885 case '4':
886 case '5':
887 case '6':
888 case '7':
889 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
890 ? 0 : 8,
891 expressionP);
892 break;
894 case 'f':
895 if (LOCAL_LABELS_FB)
897 /* If it says "0f" and it could possibly be a floating point
898 number, make it one. Otherwise, make it a local label,
899 and try to deal with parsing the rest later. */
900 if (!input_line_pointer[1]
901 || (is_end_of_line[0xff & input_line_pointer[1]])
902 || strchr (FLT_CHARS, 'f') == NULL)
903 goto is_0f_label;
905 char *cp = input_line_pointer + 1;
906 int r = atof_generic (&cp, ".", EXP_CHARS,
907 &generic_floating_point_number);
908 switch (r)
910 case 0:
911 case ERROR_EXPONENT_OVERFLOW:
912 if (*cp == 'f' || *cp == 'b')
913 /* Looks like a difference expression. */
914 goto is_0f_label;
915 else if (cp == input_line_pointer + 1)
916 /* No characters has been accepted -- looks like
917 end of operand. */
918 goto is_0f_label;
919 else
920 goto is_0f_float;
921 default:
922 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
927 /* Okay, now we've sorted it out. We resume at one of these
928 two labels, depending on what we've decided we're probably
929 looking at. */
930 is_0f_label:
931 input_line_pointer--;
932 integer_constant (10, expressionP);
933 break;
935 is_0f_float:
936 /* Fall through. */
940 case 'd':
941 case 'D':
942 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
944 integer_constant (0, expressionP);
945 break;
947 /* Fall through. */
948 case 'F':
949 case 'r':
950 case 'e':
951 case 'E':
952 case 'g':
953 case 'G':
954 input_line_pointer++;
955 floating_constant (expressionP);
956 expressionP->X_add_number = - TOLOWER (c);
957 break;
959 case '$':
960 if (LOCAL_LABELS_DOLLAR)
962 integer_constant (10, expressionP);
963 break;
965 else
966 goto default_case;
969 break;
971 case '(':
972 #ifndef NEED_INDEX_OPERATOR
973 case '[':
974 #endif
975 /* Didn't begin with digit & not a name. */
976 segment = expression (expressionP);
977 /* expression () will pass trailing whitespace. */
978 if ((c == '(' && *input_line_pointer != ')')
979 || (c == '[' && *input_line_pointer != ']'))
980 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
981 else
982 input_line_pointer++;
983 SKIP_WHITESPACE ();
984 /* Here with input_line_pointer -> char after "(...)". */
985 return segment;
987 #ifdef TC_M68K
988 case 'E':
989 if (! flag_m68k_mri || *input_line_pointer != '\'')
990 goto de_fault;
991 as_bad (_("EBCDIC constants are not supported"));
992 /* Fall through. */
993 case 'A':
994 if (! flag_m68k_mri || *input_line_pointer != '\'')
995 goto de_fault;
996 ++input_line_pointer;
997 /* Fall through. */
998 #endif
999 case '\'':
1000 if (! flag_m68k_mri)
1002 /* Warning: to conform to other people's assemblers NO
1003 ESCAPEMENT is permitted for a single quote. The next
1004 character, parity errors and all, is taken as the value
1005 of the operand. VERY KINKY. */
1006 expressionP->X_op = O_constant;
1007 expressionP->X_add_number = *input_line_pointer++;
1008 break;
1011 mri_char_constant (expressionP);
1012 break;
1014 case '+':
1015 /* Do not accept ++e as +(+e).
1016 Disabled, since the preprocessor removes whitespace. */
1017 if (0 && *input_line_pointer == '+')
1018 goto target_op;
1019 (void) operand (expressionP);
1020 break;
1022 #ifdef TC_M68K
1023 case '"':
1024 /* Double quote is the bitwise not operator in MRI mode. */
1025 if (! flag_m68k_mri)
1026 goto de_fault;
1027 /* Fall through. */
1028 #endif
1029 case '~':
1030 /* '~' is permitted to start a label on the Delta. */
1031 if (is_name_beginner (c))
1032 goto isname;
1033 case '!':
1034 case '-':
1036 /* Do not accept --e as -(-e)
1037 Disabled, since the preprocessor removes whitespace. */
1038 if (0 && c == '-' && *input_line_pointer == '-')
1039 goto target_op;
1041 operand (expressionP);
1042 if (expressionP->X_op == O_constant)
1044 /* input_line_pointer -> char after operand. */
1045 if (c == '-')
1047 expressionP->X_add_number = - expressionP->X_add_number;
1048 /* Notice: '-' may overflow: no warning is given.
1049 This is compatible with other people's
1050 assemblers. Sigh. */
1051 expressionP->X_unsigned = 0;
1053 else if (c == '~' || c == '"')
1054 expressionP->X_add_number = ~ expressionP->X_add_number;
1055 else
1056 expressionP->X_add_number = ! expressionP->X_add_number;
1058 else if (expressionP->X_op == O_big
1059 && expressionP->X_add_number <= 0
1060 && c == '-'
1061 && (generic_floating_point_number.sign == '+'
1062 || generic_floating_point_number.sign == 'P'))
1064 /* Negative flonum (eg, -1.000e0). */
1065 if (generic_floating_point_number.sign == '+')
1066 generic_floating_point_number.sign = '-';
1067 else
1068 generic_floating_point_number.sign = 'N';
1070 else if (expressionP->X_op == O_big
1071 && expressionP->X_add_number > 0)
1073 int i;
1075 if (c == '~' || c == '-')
1077 for (i = 0; i < expressionP->X_add_number; ++i)
1078 generic_bignum[i] = ~generic_bignum[i];
1079 if (c == '-')
1080 for (i = 0; i < expressionP->X_add_number; ++i)
1082 generic_bignum[i] += 1;
1083 if (generic_bignum[i])
1084 break;
1087 else if (c == '!')
1089 int nonzero = 0;
1090 for (i = 0; i < expressionP->X_add_number; ++i)
1092 if (generic_bignum[i])
1093 nonzero = 1;
1094 generic_bignum[i] = 0;
1096 generic_bignum[0] = nonzero;
1099 else if (expressionP->X_op != O_illegal
1100 && expressionP->X_op != O_absent)
1102 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1103 if (c == '-')
1104 expressionP->X_op = O_uminus;
1105 else if (c == '~' || c == '"')
1106 expressionP->X_op = O_bit_not;
1107 else
1108 expressionP->X_op = O_logical_not;
1109 expressionP->X_add_number = 0;
1111 else
1112 as_warn (_("Unary operator %c ignored because bad operand follows"),
1115 break;
1117 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1118 case '$':
1119 /* '$' is the program counter when in MRI mode, or when
1120 DOLLAR_DOT is defined. */
1121 #ifndef DOLLAR_DOT
1122 if (! flag_m68k_mri)
1123 goto de_fault;
1124 #endif
1125 if (flag_m68k_mri && hex_p (*input_line_pointer))
1127 /* In MRI mode, '$' is also used as the prefix for a
1128 hexadecimal constant. */
1129 integer_constant (16, expressionP);
1130 break;
1133 if (is_part_of_name (*input_line_pointer))
1134 goto isname;
1136 current_location (expressionP);
1137 break;
1138 #endif
1140 case '.':
1141 if (!is_part_of_name (*input_line_pointer))
1143 current_location (expressionP);
1144 break;
1146 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1147 && ! is_part_of_name (input_line_pointer[8]))
1148 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1149 && ! is_part_of_name (input_line_pointer[7])))
1151 int start;
1153 start = (input_line_pointer[1] == 't'
1154 || input_line_pointer[1] == 'T');
1155 input_line_pointer += start ? 8 : 7;
1156 SKIP_WHITESPACE ();
1157 if (*input_line_pointer != '(')
1158 as_bad (_("syntax error in .startof. or .sizeof."));
1159 else
1161 char *buf;
1163 ++input_line_pointer;
1164 SKIP_WHITESPACE ();
1165 name = input_line_pointer;
1166 c = get_symbol_end ();
1168 buf = (char *) xmalloc (strlen (name) + 10);
1169 if (start)
1170 sprintf (buf, ".startof.%s", name);
1171 else
1172 sprintf (buf, ".sizeof.%s", name);
1173 symbolP = symbol_make (buf);
1174 free (buf);
1176 expressionP->X_op = O_symbol;
1177 expressionP->X_add_symbol = symbolP;
1178 expressionP->X_add_number = 0;
1180 *input_line_pointer = c;
1181 SKIP_WHITESPACE ();
1182 if (*input_line_pointer != ')')
1183 as_bad (_("syntax error in .startof. or .sizeof."));
1184 else
1185 ++input_line_pointer;
1187 break;
1189 else
1191 goto isname;
1194 case ',':
1195 eol:
1196 /* Can't imagine any other kind of operand. */
1197 expressionP->X_op = O_absent;
1198 input_line_pointer--;
1199 break;
1201 #ifdef TC_M68K
1202 case '%':
1203 if (! flag_m68k_mri)
1204 goto de_fault;
1205 integer_constant (2, expressionP);
1206 break;
1208 case '@':
1209 if (! flag_m68k_mri)
1210 goto de_fault;
1211 integer_constant (8, expressionP);
1212 break;
1214 case ':':
1215 if (! flag_m68k_mri)
1216 goto de_fault;
1218 /* In MRI mode, this is a floating point constant represented
1219 using hexadecimal digits. */
1221 ++input_line_pointer;
1222 integer_constant (16, expressionP);
1223 break;
1225 case '*':
1226 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1227 goto de_fault;
1229 current_location (expressionP);
1230 break;
1231 #endif
1233 default:
1234 #ifdef TC_M68K
1235 de_fault:
1236 #endif
1237 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1239 /* Identifier begins here.
1240 This is kludged for speed, so code is repeated. */
1241 isname:
1242 name = --input_line_pointer;
1243 c = get_symbol_end ();
1245 #ifdef md_parse_name
1246 /* This is a hook for the backend to parse certain names
1247 specially in certain contexts. If a name always has a
1248 specific value, it can often be handled by simply
1249 entering it in the symbol table. */
1250 if (md_parse_name (name, expressionP, &c))
1252 *input_line_pointer = c;
1253 break;
1255 #endif
1257 #ifdef TC_I960
1258 /* The MRI i960 assembler permits
1259 lda sizeof code,g13
1260 FIXME: This should use md_parse_name. */
1261 if (flag_mri
1262 && (strcasecmp (name, "sizeof") == 0
1263 || strcasecmp (name, "startof") == 0))
1265 int start;
1266 char *buf;
1268 start = (name[1] == 't'
1269 || name[1] == 'T');
1271 *input_line_pointer = c;
1272 SKIP_WHITESPACE ();
1274 name = input_line_pointer;
1275 c = get_symbol_end ();
1277 buf = (char *) xmalloc (strlen (name) + 10);
1278 if (start)
1279 sprintf (buf, ".startof.%s", name);
1280 else
1281 sprintf (buf, ".sizeof.%s", name);
1282 symbolP = symbol_make (buf);
1283 free (buf);
1285 expressionP->X_op = O_symbol;
1286 expressionP->X_add_symbol = symbolP;
1287 expressionP->X_add_number = 0;
1289 *input_line_pointer = c;
1290 SKIP_WHITESPACE ();
1292 break;
1294 #endif
1296 symbolP = symbol_find_or_make (name);
1298 /* If we have an absolute symbol or a reg, then we know its
1299 value now. */
1300 segment = S_GET_SEGMENT (symbolP);
1301 if (segment == absolute_section)
1303 expressionP->X_op = O_constant;
1304 expressionP->X_add_number = S_GET_VALUE (symbolP);
1306 else if (segment == reg_section)
1308 expressionP->X_op = O_register;
1309 expressionP->X_add_number = S_GET_VALUE (symbolP);
1311 else
1313 expressionP->X_op = O_symbol;
1314 expressionP->X_add_symbol = symbolP;
1315 expressionP->X_add_number = 0;
1317 *input_line_pointer = c;
1319 else
1321 target_op:
1322 /* Let the target try to parse it. Success is indicated by changing
1323 the X_op field to something other than O_absent and pointing
1324 input_line_pointer past the expression. If it can't parse the
1325 expression, X_op and input_line_pointer should be unchanged. */
1326 expressionP->X_op = O_absent;
1327 --input_line_pointer;
1328 md_operand (expressionP);
1329 if (expressionP->X_op == O_absent)
1331 ++input_line_pointer;
1332 as_bad (_("bad expression"));
1333 expressionP->X_op = O_constant;
1334 expressionP->X_add_number = 0;
1337 break;
1340 /* It is more 'efficient' to clean up the expressionS when they are
1341 created. Doing it here saves lines of code. */
1342 clean_up_expression (expressionP);
1343 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1344 know (*input_line_pointer != ' ');
1346 /* The PA port needs this information. */
1347 if (expressionP->X_add_symbol)
1348 symbol_mark_used (expressionP->X_add_symbol);
1350 switch (expressionP->X_op)
1352 default:
1353 return absolute_section;
1354 case O_symbol:
1355 return S_GET_SEGMENT (expressionP->X_add_symbol);
1356 case O_register:
1357 return reg_section;
1361 /* Internal. Simplify a struct expression for use by expr (). */
1363 /* In: address of an expressionS.
1364 The X_op field of the expressionS may only take certain values.
1365 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1367 Out: expressionS may have been modified:
1368 Unused fields zeroed to help expr (). */
1370 static void
1371 clean_up_expression (expressionS *expressionP)
1373 switch (expressionP->X_op)
1375 case O_illegal:
1376 case O_absent:
1377 expressionP->X_add_number = 0;
1378 /* Fall through. */
1379 case O_big:
1380 case O_constant:
1381 case O_register:
1382 expressionP->X_add_symbol = NULL;
1383 /* Fall through. */
1384 case O_symbol:
1385 case O_uminus:
1386 case O_bit_not:
1387 expressionP->X_op_symbol = NULL;
1388 break;
1389 default:
1390 break;
1394 /* Expression parser. */
1396 /* We allow an empty expression, and just assume (absolute,0) silently.
1397 Unary operators and parenthetical expressions are treated as operands.
1398 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1400 We used to do an aho/ullman shift-reduce parser, but the logic got so
1401 warped that I flushed it and wrote a recursive-descent parser instead.
1402 Now things are stable, would anybody like to write a fast parser?
1403 Most expressions are either register (which does not even reach here)
1404 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1405 So I guess it doesn't really matter how inefficient more complex expressions
1406 are parsed.
1408 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1409 Also, we have consumed any leading or trailing spaces (operand does that)
1410 and done all intervening operators.
1412 This returns the segment of the result, which will be
1413 absolute_section or the segment of a symbol. */
1415 #undef __
1416 #define __ O_illegal
1418 /* Maps ASCII -> operators. */
1419 static const operatorT op_encoding[256] = {
1420 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1421 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1423 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1424 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1425 __, __, __, __, __, __, __, __,
1426 __, __, __, __, O_lt, __, O_gt, __,
1427 __, __, __, __, __, __, __, __,
1428 __, __, __, __, __, __, __, __,
1429 __, __, __, __, __, __, __, __,
1430 __, __, __,
1431 #ifdef NEED_INDEX_OPERATOR
1432 O_index,
1433 #else
1435 #endif
1436 __, __, O_bit_exclusive_or, __,
1437 __, __, __, __, __, __, __, __,
1438 __, __, __, __, __, __, __, __,
1439 __, __, __, __, __, __, __, __,
1440 __, __, __, __, O_bit_inclusive_or, __, __, __,
1442 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1443 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1444 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1445 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1446 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1447 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1448 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1449 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1452 /* Rank Examples
1453 0 operand, (expression)
1454 1 ||
1455 2 &&
1456 3 == <> < <= >= >
1457 4 + -
1458 5 used for * / % in MRI mode
1459 6 & ^ ! |
1460 7 * / % << >>
1461 8 unary - unary ~
1463 static operator_rankT op_rank[] = {
1464 0, /* O_illegal */
1465 0, /* O_absent */
1466 0, /* O_constant */
1467 0, /* O_symbol */
1468 0, /* O_symbol_rva */
1469 0, /* O_register */
1470 0, /* O_big */
1471 9, /* O_uminus */
1472 9, /* O_bit_not */
1473 9, /* O_logical_not */
1474 8, /* O_multiply */
1475 8, /* O_divide */
1476 8, /* O_modulus */
1477 8, /* O_left_shift */
1478 8, /* O_right_shift */
1479 7, /* O_bit_inclusive_or */
1480 7, /* O_bit_or_not */
1481 7, /* O_bit_exclusive_or */
1482 7, /* O_bit_and */
1483 5, /* O_add */
1484 5, /* O_subtract */
1485 4, /* O_eq */
1486 4, /* O_ne */
1487 4, /* O_lt */
1488 4, /* O_le */
1489 4, /* O_ge */
1490 4, /* O_gt */
1491 3, /* O_logical_and */
1492 2, /* O_logical_or */
1493 1, /* O_index */
1494 0, /* O_md1 */
1495 0, /* O_md2 */
1496 0, /* O_md3 */
1497 0, /* O_md4 */
1498 0, /* O_md5 */
1499 0, /* O_md6 */
1500 0, /* O_md7 */
1501 0, /* O_md8 */
1502 0, /* O_md9 */
1503 0, /* O_md10 */
1504 0, /* O_md11 */
1505 0, /* O_md12 */
1506 0, /* O_md13 */
1507 0, /* O_md14 */
1508 0, /* O_md15 */
1509 0, /* O_md16 */
1512 /* Unfortunately, in MRI mode for the m68k, multiplication and
1513 division have lower precedence than the bit wise operators. This
1514 function sets the operator precedences correctly for the current
1515 mode. Also, MRI uses a different bit_not operator, and this fixes
1516 that as well. */
1518 #define STANDARD_MUL_PRECEDENCE 8
1519 #define MRI_MUL_PRECEDENCE 6
1521 void
1522 expr_set_precedence (void)
1524 if (flag_m68k_mri)
1526 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1527 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1528 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1530 else
1532 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1533 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1534 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1538 /* Initialize the expression parser. */
1540 void
1541 expr_begin (void)
1543 expr_set_precedence ();
1545 /* Verify that X_op field is wide enough. */
1547 expressionS e;
1548 e.X_op = O_max;
1549 assert (e.X_op == O_max);
1553 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1554 sets NUM_CHARS to the number of characters in the operator.
1555 Does not advance INPUT_LINE_POINTER. */
1557 static inline operatorT
1558 operator (int *num_chars)
1560 int c;
1561 operatorT ret;
1563 c = *input_line_pointer & 0xff;
1564 *num_chars = 1;
1566 if (is_end_of_line[c])
1567 return O_illegal;
1569 switch (c)
1571 default:
1572 return op_encoding[c];
1574 case '+':
1575 case '-':
1576 /* Do not allow a++b and a--b to be a + (+b) and a - (-b)
1577 Disabled, since the preprocessor removes whitespace. */
1578 if (1 || input_line_pointer[1] != c)
1579 return op_encoding[c];
1580 return O_illegal;
1582 case '<':
1583 switch (input_line_pointer[1])
1585 default:
1586 return op_encoding[c];
1587 case '<':
1588 ret = O_left_shift;
1589 break;
1590 case '>':
1591 ret = O_ne;
1592 break;
1593 case '=':
1594 ret = O_le;
1595 break;
1597 *num_chars = 2;
1598 return ret;
1600 case '=':
1601 if (input_line_pointer[1] != '=')
1602 return op_encoding[c];
1604 *num_chars = 2;
1605 return O_eq;
1607 case '>':
1608 switch (input_line_pointer[1])
1610 default:
1611 return op_encoding[c];
1612 case '>':
1613 ret = O_right_shift;
1614 break;
1615 case '=':
1616 ret = O_ge;
1617 break;
1619 *num_chars = 2;
1620 return ret;
1622 case '!':
1623 /* We accept !! as equivalent to ^ for MRI compatibility. */
1624 if (input_line_pointer[1] != '!')
1626 if (flag_m68k_mri)
1627 return O_bit_inclusive_or;
1628 return op_encoding[c];
1630 *num_chars = 2;
1631 return O_bit_exclusive_or;
1633 case '|':
1634 if (input_line_pointer[1] != '|')
1635 return op_encoding[c];
1637 *num_chars = 2;
1638 return O_logical_or;
1640 case '&':
1641 if (input_line_pointer[1] != '&')
1642 return op_encoding[c];
1644 *num_chars = 2;
1645 return O_logical_and;
1648 /* NOTREACHED */
1651 /* Parse an expression. */
1653 segT
1654 expr (int rankarg, /* Larger # is higher rank. */
1655 expressionS *resultP /* Deliver result here. */)
1657 operator_rankT rank = (operator_rankT) rankarg;
1658 segT retval;
1659 expressionS right;
1660 operatorT op_left;
1661 operatorT op_right;
1662 int op_chars;
1664 know (rank >= 0);
1666 /* Save the value of dot for the fixup code. */
1667 if (rank == 0)
1668 dot_value = frag_now_fix ();
1670 retval = operand (resultP);
1672 /* operand () gobbles spaces. */
1673 know (*input_line_pointer != ' ');
1675 op_left = operator (&op_chars);
1676 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1678 segT rightseg;
1680 input_line_pointer += op_chars; /* -> after operator. */
1682 rightseg = expr (op_rank[(int) op_left], &right);
1683 if (right.X_op == O_absent)
1685 as_warn (_("missing operand; zero assumed"));
1686 right.X_op = O_constant;
1687 right.X_add_number = 0;
1688 right.X_add_symbol = NULL;
1689 right.X_op_symbol = NULL;
1692 know (*input_line_pointer != ' ');
1694 if (op_left == O_index)
1696 if (*input_line_pointer != ']')
1697 as_bad ("missing right bracket");
1698 else
1700 ++input_line_pointer;
1701 SKIP_WHITESPACE ();
1705 op_right = operator (&op_chars);
1707 know (op_right == O_illegal
1708 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1709 know ((int) op_left >= (int) O_multiply
1710 && (int) op_left <= (int) O_index);
1712 /* input_line_pointer->after right-hand quantity. */
1713 /* left-hand quantity in resultP. */
1714 /* right-hand quantity in right. */
1715 /* operator in op_left. */
1717 if (resultP->X_op == O_big)
1719 if (resultP->X_add_number > 0)
1720 as_warn (_("left operand is a bignum; integer 0 assumed"));
1721 else
1722 as_warn (_("left operand is a float; integer 0 assumed"));
1723 resultP->X_op = O_constant;
1724 resultP->X_add_number = 0;
1725 resultP->X_add_symbol = NULL;
1726 resultP->X_op_symbol = NULL;
1728 if (right.X_op == O_big)
1730 if (right.X_add_number > 0)
1731 as_warn (_("right operand is a bignum; integer 0 assumed"));
1732 else
1733 as_warn (_("right operand is a float; integer 0 assumed"));
1734 right.X_op = O_constant;
1735 right.X_add_number = 0;
1736 right.X_add_symbol = NULL;
1737 right.X_op_symbol = NULL;
1740 /* Optimize common cases. */
1741 #ifdef md_optimize_expr
1742 if (md_optimize_expr (resultP, op_left, &right))
1744 /* Skip. */
1747 else
1748 #endif
1749 if (op_left == O_add && right.X_op == O_constant)
1751 /* X + constant. */
1752 resultP->X_add_number += right.X_add_number;
1754 /* This case comes up in PIC code. */
1755 else if (op_left == O_subtract
1756 && right.X_op == O_symbol
1757 && resultP->X_op == O_symbol
1758 && (symbol_get_frag (right.X_add_symbol)
1759 == symbol_get_frag (resultP->X_add_symbol))
1760 && (SEG_NORMAL (rightseg)
1761 || right.X_add_symbol == resultP->X_add_symbol))
1763 resultP->X_add_number -= right.X_add_number;
1764 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1765 - S_GET_VALUE (right.X_add_symbol));
1766 resultP->X_op = O_constant;
1767 resultP->X_add_symbol = 0;
1769 else if (op_left == O_subtract && right.X_op == O_constant)
1771 /* X - constant. */
1772 resultP->X_add_number -= right.X_add_number;
1774 else if (op_left == O_add && resultP->X_op == O_constant)
1776 /* Constant + X. */
1777 resultP->X_op = right.X_op;
1778 resultP->X_add_symbol = right.X_add_symbol;
1779 resultP->X_op_symbol = right.X_op_symbol;
1780 resultP->X_add_number += right.X_add_number;
1781 retval = rightseg;
1783 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1785 /* Constant OP constant. */
1786 offsetT v = right.X_add_number;
1787 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1789 as_warn (_("division by zero"));
1790 v = 1;
1792 switch (op_left)
1794 default: abort ();
1795 case O_multiply: resultP->X_add_number *= v; break;
1796 case O_divide: resultP->X_add_number /= v; break;
1797 case O_modulus: resultP->X_add_number %= v; break;
1798 case O_left_shift: resultP->X_add_number <<= v; break;
1799 case O_right_shift:
1800 /* We always use unsigned shifts, to avoid relying on
1801 characteristics of the compiler used to compile gas. */
1802 resultP->X_add_number =
1803 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1804 break;
1805 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1806 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1807 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1808 case O_bit_and: resultP->X_add_number &= v; break;
1809 case O_add: resultP->X_add_number += v; break;
1810 case O_subtract: resultP->X_add_number -= v; break;
1811 case O_eq:
1812 resultP->X_add_number =
1813 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1814 break;
1815 case O_ne:
1816 resultP->X_add_number =
1817 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1818 break;
1819 case O_lt:
1820 resultP->X_add_number =
1821 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1822 break;
1823 case O_le:
1824 resultP->X_add_number =
1825 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1826 break;
1827 case O_ge:
1828 resultP->X_add_number =
1829 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1830 break;
1831 case O_gt:
1832 resultP->X_add_number =
1833 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1834 break;
1835 case O_logical_and:
1836 resultP->X_add_number = resultP->X_add_number && v;
1837 break;
1838 case O_logical_or:
1839 resultP->X_add_number = resultP->X_add_number || v;
1840 break;
1843 else if (resultP->X_op == O_symbol
1844 && right.X_op == O_symbol
1845 && (op_left == O_add
1846 || op_left == O_subtract
1847 || (resultP->X_add_number == 0
1848 && right.X_add_number == 0)))
1850 /* Symbol OP symbol. */
1851 resultP->X_op = op_left;
1852 resultP->X_op_symbol = right.X_add_symbol;
1853 if (op_left == O_add)
1854 resultP->X_add_number += right.X_add_number;
1855 else if (op_left == O_subtract)
1857 resultP->X_add_number -= right.X_add_number;
1858 if (retval == rightseg && SEG_NORMAL (retval))
1860 retval = absolute_section;
1861 rightseg = absolute_section;
1865 else
1867 /* The general case. */
1868 resultP->X_add_symbol = make_expr_symbol (resultP);
1869 resultP->X_op_symbol = make_expr_symbol (&right);
1870 resultP->X_op = op_left;
1871 resultP->X_add_number = 0;
1872 resultP->X_unsigned = 1;
1875 if (retval != rightseg)
1877 if (! SEG_NORMAL (retval))
1879 if (retval != undefined_section || SEG_NORMAL (rightseg))
1880 retval = rightseg;
1882 else if (SEG_NORMAL (rightseg)
1883 #ifdef DIFF_EXPR_OK
1884 && op_left != O_subtract
1885 #endif
1887 as_bad (_("operation combines symbols in different segments"));
1890 op_left = op_right;
1891 } /* While next operator is >= this rank. */
1893 /* The PA port needs this information. */
1894 if (resultP->X_add_symbol)
1895 symbol_mark_used (resultP->X_add_symbol);
1897 return resultP->X_op == O_constant ? absolute_section : retval;
1900 /* This lives here because it belongs equally in expr.c & read.c.
1901 expr.c is just a branch office read.c anyway, and putting it
1902 here lessens the crowd at read.c.
1904 Assume input_line_pointer is at start of symbol name.
1905 Advance input_line_pointer past symbol name.
1906 Turn that character into a '\0', returning its former value.
1907 This allows a string compare (RMS wants symbol names to be strings)
1908 of the symbol name.
1909 There will always be a char following symbol name, because all good
1910 lines end in end-of-line. */
1912 char
1913 get_symbol_end (void)
1915 char c;
1917 /* We accept \001 in a name in case this is being called with a
1918 constructed string. */
1919 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1921 while (is_part_of_name (c = *input_line_pointer++)
1922 || c == '\001')
1924 if (is_name_ender (c))
1925 c = *input_line_pointer++;
1927 *--input_line_pointer = 0;
1928 return (c);
1931 unsigned int
1932 get_single_number (void)
1934 expressionS exp;
1935 operand (&exp);
1936 return exp.X_add_number;