2000-02-27 Thomas de Lellis <tdel@windriver.com>
[binutils.git] / gas / expr.c
blobcd94e8ebec5588e4b2c8b58b402f8417608859d9
1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 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.
29 #include <ctype.h>
30 #include <string.h>
31 #define min(a, b) ((a) < (b) ? (a) : (b))
33 #include "as.h"
34 #include "obstack.h"
36 static void floating_constant PARAMS ((expressionS * expressionP));
37 static valueT generic_bignum_to_int32 PARAMS ((void));
38 #ifdef BFD64
39 static valueT generic_bignum_to_int64 PARAMS ((void));
40 #endif
41 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
42 static void mri_char_constant PARAMS ((expressionS *));
43 static void current_location PARAMS ((expressionS *));
44 static void clean_up_expression PARAMS ((expressionS * expressionP));
45 static segT operand PARAMS ((expressionS *));
46 static operatorT operator PARAMS ((void));
48 extern const char EXP_CHARS[], FLT_CHARS[];
50 /* We keep a mapping of expression symbols to file positions, so that
51 we can provide better error messages. */
53 struct expr_symbol_line
55 struct expr_symbol_line *next;
56 symbolS *sym;
57 char *file;
58 unsigned int line;
61 static struct expr_symbol_line *expr_symbol_lines;
63 /* Build a dummy symbol to hold a complex expression. This is how we
64 build expressions up out of other expressions. The symbol is put
65 into the fake section expr_section. */
67 symbolS *
68 make_expr_symbol (expressionP)
69 expressionS *expressionP;
71 expressionS zero;
72 const char *fake;
73 symbolS *symbolP;
74 struct expr_symbol_line *n;
76 if (expressionP->X_op == O_symbol
77 && expressionP->X_add_number == 0)
78 return expressionP->X_add_symbol;
80 if (expressionP->X_op == O_big)
82 /* This won't work, because the actual value is stored in
83 generic_floating_point_number or generic_bignum, and we are
84 going to lose it if we haven't already. */
85 if (expressionP->X_add_number > 0)
86 as_bad (_("bignum invalid; zero assumed"));
87 else
88 as_bad (_("floating point number invalid; zero assumed"));
89 zero.X_op = O_constant;
90 zero.X_add_number = 0;
91 zero.X_unsigned = 0;
92 clean_up_expression (&zero);
93 expressionP = &zero;
96 fake = FAKE_LABEL_NAME;
98 /* Putting constant symbols in absolute_section rather than
99 expr_section is convenient for the old a.out code, for which
100 S_GET_SEGMENT does not always retrieve the value put in by
101 S_SET_SEGMENT. */
102 symbolP = symbol_create (fake,
103 (expressionP->X_op == O_constant
104 ? absolute_section
105 : expr_section),
106 0, &zero_address_frag);
107 symbol_set_value_expression (symbolP, expressionP);
109 if (expressionP->X_op == O_constant)
110 resolve_symbol_value (symbolP, 1);
112 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
113 n->sym = symbolP;
114 as_where (&n->file, &n->line);
115 n->next = expr_symbol_lines;
116 expr_symbol_lines = n;
118 return symbolP;
121 /* Return the file and line number for an expr symbol. Return
122 non-zero if something was found, 0 if no information is known for
123 the symbol. */
126 expr_symbol_where (sym, pfile, pline)
127 symbolS *sym;
128 char **pfile;
129 unsigned int *pline;
131 register struct expr_symbol_line *l;
133 for (l = expr_symbol_lines; l != NULL; l = l->next)
135 if (l->sym == sym)
137 *pfile = l->file;
138 *pline = l->line;
139 return 1;
143 return 0;
146 /* Utilities for building expressions.
147 Since complex expressions are recorded as symbols for use in other
148 expressions these return a symbolS * and not an expressionS *.
149 These explicitly do not take an "add_number" argument. */
150 /* ??? For completeness' sake one might want expr_build_symbol.
151 It would just return its argument. */
153 /* Build an expression for an unsigned constant.
154 The corresponding one for signed constants is missing because
155 there's currently no need for it. One could add an unsigned_p flag
156 but that seems more clumsy. */
158 symbolS *
159 expr_build_uconstant (value)
160 offsetT value;
162 expressionS e;
164 e.X_op = O_constant;
165 e.X_add_number = value;
166 e.X_unsigned = 1;
167 return make_expr_symbol (&e);
170 /* Build an expression for OP s1. */
172 symbolS *
173 expr_build_unary (op, s1)
174 operatorT op;
175 symbolS *s1;
177 expressionS e;
179 e.X_op = op;
180 e.X_add_symbol = s1;
181 e.X_add_number = 0;
182 return make_expr_symbol (&e);
185 /* Build an expression for s1 OP s2. */
187 symbolS *
188 expr_build_binary (op, s1, s2)
189 operatorT op;
190 symbolS *s1;
191 symbolS *s2;
193 expressionS e;
195 e.X_op = op;
196 e.X_add_symbol = s1;
197 e.X_op_symbol = s2;
198 e.X_add_number = 0;
199 return make_expr_symbol (&e);
202 /* Build an expression for the current location ('.'). */
204 symbolS *
205 expr_build_dot ()
207 expressionS e;
209 current_location (&e);
210 return make_expr_symbol (&e);
214 * Build any floating-point literal here.
215 * Also build any bignum literal here.
218 /* Seems atof_machine can backscan through generic_bignum and hit whatever
219 happens to be loaded before it in memory. And its way too complicated
220 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
221 and never write into the early words, thus they'll always be zero.
222 I hate Dean's floating-point code. Bleh. */
223 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
224 FLONUM_TYPE generic_floating_point_number =
226 &generic_bignum[6], /* low (JF: Was 0) */
227 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
228 0, /* leader */
229 0, /* exponent */
230 0 /* sign */
232 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
233 int generic_floating_point_magic;
235 static void
236 floating_constant (expressionP)
237 expressionS *expressionP;
239 /* input_line_pointer->*/
240 /* floating-point constant. */
241 int error_code;
243 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
244 &generic_floating_point_number);
246 if (error_code)
248 if (error_code == ERROR_EXPONENT_OVERFLOW)
250 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
252 else
254 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code);
257 expressionP->X_op = O_big;
258 /* input_line_pointer->just after constant, */
259 /* which may point to whitespace. */
260 expressionP->X_add_number = -1;
263 static valueT
264 generic_bignum_to_int32 ()
266 valueT number =
267 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
268 | (generic_bignum[0] & LITTLENUM_MASK);
269 number &= 0xffffffff;
270 return number;
273 #ifdef BFD64
274 static valueT
275 generic_bignum_to_int64 ()
277 valueT number =
278 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
279 << LITTLENUM_NUMBER_OF_BITS)
280 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
281 << LITTLENUM_NUMBER_OF_BITS)
282 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
283 << LITTLENUM_NUMBER_OF_BITS)
284 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
285 return number;
287 #endif
289 static void
290 integer_constant (radix, expressionP)
291 int radix;
292 expressionS *expressionP;
294 char *start; /* start of number. */
295 char *suffix = NULL;
296 char c;
297 valueT number; /* offset or (absolute) value */
298 short int digit; /* value of next digit in current radix */
299 short int maxdig = 0;/* highest permitted digit value. */
300 int too_many_digits = 0; /* if we see >= this number of */
301 char *name; /* points to name of symbol */
302 symbolS *symbolP; /* points to symbol */
304 int small; /* true if fits in 32 bits. */
306 /* May be bignum, or may fit in 32 bits. */
307 /* Most numbers fit into 32 bits, and we want this case to be fast.
308 so we pretend it will fit into 32 bits. If, after making up a 32
309 bit number, we realise that we have scanned more digits than
310 comfortably fit into 32 bits, we re-scan the digits coding them
311 into a bignum. For decimal and octal numbers we are
312 conservative: Some numbers may be assumed bignums when in fact
313 they do fit into 32 bits. Numbers of any radix can have excess
314 leading zeros: We strive to recognise this and cast them back
315 into 32 bits. We must check that the bignum really is more than
316 32 bits, and change it back to a 32-bit number if it fits. The
317 number we are looking for is expected to be positive, but if it
318 fits into 32 bits as an unsigned number, we let it be a 32-bit
319 number. The cavalier approach is for speed in ordinary cases. */
320 /* This has been extended for 64 bits. We blindly assume that if
321 you're compiling in 64-bit mode, the target is a 64-bit machine.
322 This should be cleaned up. */
324 #ifdef BFD64
325 #define valuesize 64
326 #else /* includes non-bfd case, mostly */
327 #define valuesize 32
328 #endif
330 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
332 int flt = 0;
334 /* In MRI mode, the number may have a suffix indicating the
335 radix. For that matter, it might actually be a floating
336 point constant. */
337 for (suffix = input_line_pointer;
338 isalnum ((unsigned char) *suffix);
339 suffix++)
341 if (*suffix == 'e' || *suffix == 'E')
342 flt = 1;
345 if (suffix == input_line_pointer)
347 radix = 10;
348 suffix = NULL;
350 else
352 c = *--suffix;
353 if (islower ((unsigned char) c))
354 c = toupper (c);
355 if (c == 'B')
356 radix = 2;
357 else if (c == 'D')
358 radix = 10;
359 else if (c == 'O' || c == 'Q')
360 radix = 8;
361 else if (c == 'H')
362 radix = 16;
363 else if (suffix[1] == '.' || c == 'E' || flt)
365 floating_constant (expressionP);
366 return;
368 else
370 radix = 10;
371 suffix = NULL;
376 switch (radix)
378 case 2:
379 maxdig = 2;
380 too_many_digits = valuesize + 1;
381 break;
382 case 8:
383 maxdig = radix = 8;
384 too_many_digits = (valuesize + 2) / 3 + 1;
385 break;
386 case 16:
387 maxdig = radix = 16;
388 too_many_digits = (valuesize + 3) / 4 + 1;
389 break;
390 case 10:
391 maxdig = radix = 10;
392 too_many_digits = (valuesize + 11) / 4; /* very rough */
394 #undef valuesize
395 start = input_line_pointer;
396 c = *input_line_pointer++;
397 for (number = 0;
398 (digit = hex_value (c)) < maxdig;
399 c = *input_line_pointer++)
401 number = number * radix + digit;
403 /* c contains character after number. */
404 /* input_line_pointer->char after c. */
405 small = (input_line_pointer - start - 1) < too_many_digits;
407 if (radix == 16 && c == '_')
409 /* This is literal of the form 0x333_0_12345678_1.
410 This example is equivalent to 0x00000333000000001234567800000001. */
412 int num_little_digits = 0;
413 int i;
414 input_line_pointer = start; /*->1st digit. */
416 know (LITTLENUM_NUMBER_OF_BITS == 16);
418 for (c = '_'; c == '_'; num_little_digits+=2)
421 /* Convert one 64-bit word. */
422 int ndigit = 0;
423 number = 0;
424 for (c = *input_line_pointer++;
425 (digit = hex_value (c)) < maxdig;
426 c = *(input_line_pointer++))
428 number = number * radix + digit;
429 ndigit++;
432 /* Check for 8 digit per word max. */
433 if (ndigit > 8)
434 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
436 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
437 know (LITTLENUM_NUMBER_OF_BITS == 16);
438 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1); i >= 2; i--)
439 generic_bignum[i] = generic_bignum[i-2];
441 /* Add the new digits as the least significant new ones. */
442 generic_bignum[0] = number & 0xffffffff;
443 generic_bignum[1] = number >> 16;
446 /* Again, c is char after number, input_line_pointer->after c. */
448 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
449 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
451 assert (num_little_digits >= 4);
453 if (num_little_digits != 8)
454 as_bad (_("A bignum with underscores must have exactly 4 words."));
456 /* We might have some leading zeros. These can be trimmed to give
457 * us a change to fit this constant into a small number.
459 while (generic_bignum[num_little_digits-1] == 0 && num_little_digits > 1)
460 num_little_digits--;
462 if (num_little_digits <= 2)
464 /* will fit into 32 bits. */
465 number = generic_bignum_to_int32 ();
466 small = 1;
468 #ifdef BFD64
469 else if (num_little_digits <= 4)
471 /* Will fit into 64 bits. */
472 number = generic_bignum_to_int64 ();
473 small = 1;
475 #endif
476 else
478 small = 0;
479 number = num_little_digits; /* number of littlenums in the bignum. */
482 else if (!small)
485 * we saw a lot of digits. manufacture a bignum the hard way.
487 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
488 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
489 long carry;
491 leader = generic_bignum;
492 generic_bignum[0] = 0;
493 generic_bignum[1] = 0;
494 generic_bignum[2] = 0;
495 generic_bignum[3] = 0;
496 input_line_pointer = start; /*->1st digit. */
497 c = *input_line_pointer++;
498 for (;
499 (carry = hex_value (c)) < maxdig;
500 c = *input_line_pointer++)
502 for (pointer = generic_bignum;
503 pointer <= leader;
504 pointer++)
506 long work;
508 work = carry + radix * *pointer;
509 *pointer = work & LITTLENUM_MASK;
510 carry = work >> LITTLENUM_NUMBER_OF_BITS;
512 if (carry)
514 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
516 /* room to grow a longer bignum. */
517 *++leader = carry;
521 /* again, c is char after number, */
522 /* input_line_pointer->after c. */
523 know (LITTLENUM_NUMBER_OF_BITS == 16);
524 if (leader < generic_bignum + 2)
526 /* will fit into 32 bits. */
527 number = generic_bignum_to_int32 ();
528 small = 1;
530 #ifdef BFD64
531 else if (leader < generic_bignum + 4)
533 /* Will fit into 64 bits. */
534 number = generic_bignum_to_int64 ();
535 small = 1;
537 #endif
538 else
540 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
544 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
545 && suffix != NULL
546 && input_line_pointer - 1 == suffix)
547 c = *input_line_pointer++;
549 if (small)
552 * here with number, in correct radix. c is the next char.
553 * note that unlike un*x, we allow "011f" "0x9f" to
554 * both mean the same as the (conventional) "9f". this is simply easier
555 * than checking for strict canonical form. syntax sux!
558 if (LOCAL_LABELS_FB && c == 'b')
561 * backward ref to local label.
562 * because it is backward, expect it to be defined.
564 /* Construct a local label. */
565 name = fb_label_name ((int) number, 0);
567 /* seen before, or symbol is defined: ok */
568 symbolP = symbol_find (name);
569 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
571 /* local labels are never absolute. don't waste time
572 checking absoluteness. */
573 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
575 expressionP->X_op = O_symbol;
576 expressionP->X_add_symbol = symbolP;
578 else
580 /* either not seen or not defined. */
581 /* @@ Should print out the original string instead of
582 the parsed number. */
583 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
584 (int) number);
585 expressionP->X_op = O_constant;
588 expressionP->X_add_number = 0;
589 } /* case 'b' */
590 else if (LOCAL_LABELS_FB && c == 'f')
593 * forward reference. expect symbol to be undefined or
594 * unknown. undefined: seen it before. unknown: never seen
595 * it before.
596 * construct a local label name, then an undefined symbol.
597 * don't create a xseg frag for it: caller may do that.
598 * just return it as never seen before.
600 name = fb_label_name ((int) number, 1);
601 symbolP = symbol_find_or_make (name);
602 /* we have no need to check symbol properties. */
603 #ifndef many_segments
604 /* since "know" puts its arg into a "string", we
605 can't have newlines in the argument. */
606 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
607 #endif
608 expressionP->X_op = O_symbol;
609 expressionP->X_add_symbol = symbolP;
610 expressionP->X_add_number = 0;
611 } /* case 'f' */
612 else if (LOCAL_LABELS_DOLLAR && c == '$')
614 /* If the dollar label is *currently* defined, then this is just
615 another reference to it. If it is not *currently* defined,
616 then this is a fresh instantiation of that number, so create
617 it. */
619 if (dollar_label_defined ((long) number))
621 name = dollar_label_name ((long) number, 0);
622 symbolP = symbol_find (name);
623 know (symbolP != NULL);
625 else
627 name = dollar_label_name ((long) number, 1);
628 symbolP = symbol_find_or_make (name);
631 expressionP->X_op = O_symbol;
632 expressionP->X_add_symbol = symbolP;
633 expressionP->X_add_number = 0;
634 } /* case '$' */
635 else
637 expressionP->X_op = O_constant;
638 #ifdef TARGET_WORD_SIZE
639 /* Sign extend NUMBER. */
640 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
641 #endif
642 expressionP->X_add_number = number;
643 input_line_pointer--; /* restore following character. */
644 } /* really just a number */
646 else
648 /* not a small number */
649 expressionP->X_op = O_big;
650 expressionP->X_add_number = number; /* number of littlenums */
651 input_line_pointer--; /*->char following number. */
655 /* Parse an MRI multi character constant. */
657 static void
658 mri_char_constant (expressionP)
659 expressionS *expressionP;
661 int i;
663 if (*input_line_pointer == '\''
664 && input_line_pointer[1] != '\'')
666 expressionP->X_op = O_constant;
667 expressionP->X_add_number = 0;
668 return;
671 /* In order to get the correct byte ordering, we must build the
672 number in reverse. */
673 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
675 int j;
677 generic_bignum[i] = 0;
678 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
680 if (*input_line_pointer == '\'')
682 if (input_line_pointer[1] != '\'')
683 break;
684 ++input_line_pointer;
686 generic_bignum[i] <<= 8;
687 generic_bignum[i] += *input_line_pointer;
688 ++input_line_pointer;
691 if (i < SIZE_OF_LARGE_NUMBER - 1)
693 /* If there is more than one littlenum, left justify the
694 last one to make it match the earlier ones. If there is
695 only one, we can just use the value directly. */
696 for (; j < CHARS_PER_LITTLENUM; j++)
697 generic_bignum[i] <<= 8;
700 if (*input_line_pointer == '\''
701 && input_line_pointer[1] != '\'')
702 break;
705 if (i < 0)
707 as_bad (_("Character constant too large"));
708 i = 0;
711 if (i > 0)
713 int c;
714 int j;
716 c = SIZE_OF_LARGE_NUMBER - i;
717 for (j = 0; j < c; j++)
718 generic_bignum[j] = generic_bignum[i + j];
719 i = c;
722 know (LITTLENUM_NUMBER_OF_BITS == 16);
723 if (i > 2)
725 expressionP->X_op = O_big;
726 expressionP->X_add_number = i;
728 else
730 expressionP->X_op = O_constant;
731 if (i < 2)
732 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
733 else
734 expressionP->X_add_number =
735 (((generic_bignum[1] & LITTLENUM_MASK)
736 << LITTLENUM_NUMBER_OF_BITS)
737 | (generic_bignum[0] & LITTLENUM_MASK));
740 /* Skip the final closing quote. */
741 ++input_line_pointer;
744 /* Return an expression representing the current location. This
745 handles the magic symbol `.'. */
747 static void
748 current_location (expressionp)
749 expressionS *expressionp;
751 if (now_seg == absolute_section)
753 expressionp->X_op = O_constant;
754 expressionp->X_add_number = abs_section_offset;
756 else
758 symbolS *symbolp;
760 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
761 (valueT) frag_now_fix (),
762 frag_now);
763 expressionp->X_op = O_symbol;
764 expressionp->X_add_symbol = symbolp;
765 expressionp->X_add_number = 0;
770 * Summary of operand().
772 * in: Input_line_pointer points to 1st char of operand, which may
773 * be a space.
775 * out: A expressionS.
776 * The operand may have been empty: in this case X_op == O_absent.
777 * Input_line_pointer->(next non-blank) char after operand.
780 static segT
781 operand (expressionP)
782 expressionS *expressionP;
784 char c;
785 symbolS *symbolP; /* points to symbol */
786 char *name; /* points to name of symbol */
787 segT segment;
789 /* All integers are regarded as unsigned unless they are negated.
790 This is because the only thing which cares whether a number is
791 unsigned is the code in emit_expr which extends constants into
792 bignums. It should only sign extend negative numbers, so that
793 something like ``.quad 0x80000000'' is not sign extended even
794 though it appears negative if valueT is 32 bits. */
795 expressionP->X_unsigned = 1;
797 /* digits, assume it is a bignum. */
799 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
800 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
802 switch (c)
804 case '1':
805 case '2':
806 case '3':
807 case '4':
808 case '5':
809 case '6':
810 case '7':
811 case '8':
812 case '9':
813 input_line_pointer--;
815 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
816 ? 0 : 10,
817 expressionP);
818 break;
820 case '0':
821 /* non-decimal radix */
823 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
825 char *s;
827 /* Check for a hex constant. */
828 for (s = input_line_pointer; hex_p (*s); s++)
830 if (*s == 'h' || *s == 'H')
832 --input_line_pointer;
833 integer_constant (0, expressionP);
834 break;
837 c = *input_line_pointer;
838 switch (c)
840 case 'o':
841 case 'O':
842 case 'q':
843 case 'Q':
844 case '8':
845 case '9':
846 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
848 integer_constant (0, expressionP);
849 break;
851 /* Fall through. */
852 default:
853 default_case:
854 if (c && strchr (FLT_CHARS, c))
856 input_line_pointer++;
857 floating_constant (expressionP);
858 expressionP->X_add_number =
859 - (isupper ((unsigned char) c) ? tolower (c) : c);
861 else
863 /* The string was only zero */
864 expressionP->X_op = O_constant;
865 expressionP->X_add_number = 0;
868 break;
870 case 'x':
871 case 'X':
872 if (flag_m68k_mri)
873 goto default_case;
874 input_line_pointer++;
875 integer_constant (16, expressionP);
876 break;
878 case 'b':
879 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
881 /* This code used to check for '+' and '-' here, and, in
882 some conditions, fall through to call
883 integer_constant. However, that didn't make sense,
884 as integer_constant only accepts digits. */
885 /* Some of our code elsewhere does permit digits greater
886 than the expected base; for consistency, do the same
887 here. */
888 if (input_line_pointer[1] < '0'
889 || input_line_pointer[1] > '9')
891 /* Parse this as a back reference to label 0. */
892 input_line_pointer--;
893 integer_constant (10, expressionP);
894 break;
896 /* Otherwise, parse this as a binary number. */
898 /* Fall through. */
899 case 'B':
900 input_line_pointer++;
901 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
902 goto default_case;
903 integer_constant (2, expressionP);
904 break;
906 case '0':
907 case '1':
908 case '2':
909 case '3':
910 case '4':
911 case '5':
912 case '6':
913 case '7':
914 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
915 ? 0 : 8,
916 expressionP);
917 break;
919 case 'f':
920 if (LOCAL_LABELS_FB)
922 /* If it says "0f" and it could possibly be a floating point
923 number, make it one. Otherwise, make it a local label,
924 and try to deal with parsing the rest later. */
925 if (!input_line_pointer[1]
926 || (is_end_of_line[0xff & input_line_pointer[1]]))
927 goto is_0f_label;
929 char *cp = input_line_pointer + 1;
930 int r = atof_generic (&cp, ".", EXP_CHARS,
931 &generic_floating_point_number);
932 switch (r)
934 case 0:
935 case ERROR_EXPONENT_OVERFLOW:
936 if (*cp == 'f' || *cp == 'b')
937 /* looks like a difference expression */
938 goto is_0f_label;
939 else if (cp == input_line_pointer + 1)
940 /* No characters has been accepted -- looks like
941 end of operand. */
942 goto is_0f_label;
943 else
944 goto is_0f_float;
945 default:
946 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
951 /* Okay, now we've sorted it out. We resume at one of these
952 two labels, depending on what we've decided we're probably
953 looking at. */
954 is_0f_label:
955 input_line_pointer--;
956 integer_constant (10, expressionP);
957 break;
959 is_0f_float:
960 /* fall through */
964 case 'd':
965 case 'D':
966 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
968 integer_constant (0, expressionP);
969 break;
971 /* Fall through. */
972 case 'F':
973 case 'r':
974 case 'e':
975 case 'E':
976 case 'g':
977 case 'G':
978 input_line_pointer++;
979 floating_constant (expressionP);
980 expressionP->X_add_number =
981 - (isupper ((unsigned char) c) ? tolower (c) : c);
982 break;
984 case '$':
985 if (LOCAL_LABELS_DOLLAR)
987 integer_constant (10, expressionP);
988 break;
990 else
991 goto default_case;
994 break;
996 case '(':
997 #ifndef NEED_INDEX_OPERATOR
998 case '[':
999 #endif
1000 /* didn't begin with digit & not a name */
1001 segment = expression (expressionP);
1002 /* Expression() will pass trailing whitespace */
1003 if ((c == '(' && *input_line_pointer++ != ')')
1004 || (c == '[' && *input_line_pointer++ != ']'))
1006 as_bad (_("Missing ')' assumed"));
1007 input_line_pointer--;
1009 SKIP_WHITESPACE ();
1010 /* here with input_line_pointer->char after "(...)" */
1011 return segment;
1013 #ifdef TC_M68K
1014 case 'E':
1015 if (! flag_m68k_mri || *input_line_pointer != '\'')
1016 goto de_fault;
1017 as_bad (_("EBCDIC constants are not supported"));
1018 /* Fall through. */
1019 case 'A':
1020 if (! flag_m68k_mri || *input_line_pointer != '\'')
1021 goto de_fault;
1022 ++input_line_pointer;
1023 /* Fall through. */
1024 #endif
1025 case '\'':
1026 if (! flag_m68k_mri)
1028 /* Warning: to conform to other people's assemblers NO
1029 ESCAPEMENT is permitted for a single quote. The next
1030 character, parity errors and all, is taken as the value
1031 of the operand. VERY KINKY. */
1032 expressionP->X_op = O_constant;
1033 expressionP->X_add_number = *input_line_pointer++;
1034 break;
1037 mri_char_constant (expressionP);
1038 break;
1040 case '+':
1041 (void) operand (expressionP);
1042 break;
1044 #ifdef TC_M68K
1045 case '"':
1046 /* Double quote is the bitwise not operator in MRI mode. */
1047 if (! flag_m68k_mri)
1048 goto de_fault;
1049 /* Fall through. */
1050 #endif
1051 case '~':
1052 /* ~ is permitted to start a label on the Delta. */
1053 if (is_name_beginner (c))
1054 goto isname;
1055 case '!':
1056 case '-':
1058 operand (expressionP);
1059 if (expressionP->X_op == O_constant)
1061 /* input_line_pointer -> char after operand */
1062 if (c == '-')
1064 expressionP->X_add_number = - expressionP->X_add_number;
1065 /* Notice: '-' may overflow: no warning is given. This is
1066 compatible with other people's assemblers. Sigh. */
1067 expressionP->X_unsigned = 0;
1069 else if (c == '~' || c == '"')
1070 expressionP->X_add_number = ~ expressionP->X_add_number;
1071 else
1072 expressionP->X_add_number = ! expressionP->X_add_number;
1074 else if (expressionP->X_op != O_illegal
1075 && expressionP->X_op != O_absent)
1077 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1078 if (c == '-')
1079 expressionP->X_op = O_uminus;
1080 else if (c == '~' || c == '"')
1081 expressionP->X_op = O_bit_not;
1082 else
1083 expressionP->X_op = O_logical_not;
1084 expressionP->X_add_number = 0;
1086 else
1087 as_warn (_("Unary operator %c ignored because bad operand follows"),
1090 break;
1092 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1093 case '$':
1094 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1095 is defined. */
1096 #ifndef DOLLAR_DOT
1097 if (! flag_m68k_mri)
1098 goto de_fault;
1099 #endif
1100 if (flag_m68k_mri && hex_p (*input_line_pointer))
1102 /* In MRI mode, $ is also used as the prefix for a
1103 hexadecimal constant. */
1104 integer_constant (16, expressionP);
1105 break;
1108 if (is_part_of_name (*input_line_pointer))
1109 goto isname;
1111 current_location (expressionP);
1112 break;
1113 #endif
1115 case '.':
1116 if (!is_part_of_name (*input_line_pointer))
1118 current_location (expressionP);
1119 break;
1121 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1122 && ! is_part_of_name (input_line_pointer[8]))
1123 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1124 && ! is_part_of_name (input_line_pointer[7])))
1126 int start;
1128 start = (input_line_pointer[1] == 't'
1129 || input_line_pointer[1] == 'T');
1130 input_line_pointer += start ? 8 : 7;
1131 SKIP_WHITESPACE ();
1132 if (*input_line_pointer != '(')
1133 as_bad (_("syntax error in .startof. or .sizeof."));
1134 else
1136 char *buf;
1138 ++input_line_pointer;
1139 SKIP_WHITESPACE ();
1140 name = input_line_pointer;
1141 c = get_symbol_end ();
1143 buf = (char *) xmalloc (strlen (name) + 10);
1144 if (start)
1145 sprintf (buf, ".startof.%s", name);
1146 else
1147 sprintf (buf, ".sizeof.%s", name);
1148 symbolP = symbol_make (buf);
1149 free (buf);
1151 expressionP->X_op = O_symbol;
1152 expressionP->X_add_symbol = symbolP;
1153 expressionP->X_add_number = 0;
1155 *input_line_pointer = c;
1156 SKIP_WHITESPACE ();
1157 if (*input_line_pointer != ')')
1158 as_bad (_("syntax error in .startof. or .sizeof."));
1159 else
1160 ++input_line_pointer;
1162 break;
1164 else
1166 goto isname;
1168 case ',':
1169 case '\n':
1170 case '\0':
1171 eol:
1172 /* can't imagine any other kind of operand */
1173 expressionP->X_op = O_absent;
1174 input_line_pointer--;
1175 break;
1177 #ifdef TC_M68K
1178 case '%':
1179 if (! flag_m68k_mri)
1180 goto de_fault;
1181 integer_constant (2, expressionP);
1182 break;
1184 case '@':
1185 if (! flag_m68k_mri)
1186 goto de_fault;
1187 integer_constant (8, expressionP);
1188 break;
1190 case ':':
1191 if (! flag_m68k_mri)
1192 goto de_fault;
1194 /* In MRI mode, this is a floating point constant represented
1195 using hexadecimal digits. */
1197 ++input_line_pointer;
1198 integer_constant (16, expressionP);
1199 break;
1201 case '*':
1202 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1203 goto de_fault;
1205 current_location (expressionP);
1206 break;
1207 #endif
1209 default:
1210 #ifdef TC_M68K
1211 de_fault:
1212 #endif
1213 if (is_end_of_line[(unsigned char) c])
1214 goto eol;
1215 if (is_name_beginner (c)) /* here if did not begin with a digit */
1218 * Identifier begins here.
1219 * This is kludged for speed, so code is repeated.
1221 isname:
1222 name = --input_line_pointer;
1223 c = get_symbol_end ();
1225 #ifdef md_parse_name
1226 /* This is a hook for the backend to parse certain names
1227 specially in certain contexts. If a name always has a
1228 specific value, it can often be handled by simply
1229 entering it in the symbol table. */
1230 if (md_parse_name (name, expressionP))
1232 *input_line_pointer = c;
1233 break;
1235 #endif
1237 #ifdef TC_I960
1238 /* The MRI i960 assembler permits
1239 lda sizeof code,g13
1240 FIXME: This should use md_parse_name. */
1241 if (flag_mri
1242 && (strcasecmp (name, "sizeof") == 0
1243 || strcasecmp (name, "startof") == 0))
1245 int start;
1246 char *buf;
1248 start = (name[1] == 't'
1249 || name[1] == 'T');
1251 *input_line_pointer = c;
1252 SKIP_WHITESPACE ();
1254 name = input_line_pointer;
1255 c = get_symbol_end ();
1257 buf = (char *) xmalloc (strlen (name) + 10);
1258 if (start)
1259 sprintf (buf, ".startof.%s", name);
1260 else
1261 sprintf (buf, ".sizeof.%s", name);
1262 symbolP = symbol_make (buf);
1263 free (buf);
1265 expressionP->X_op = O_symbol;
1266 expressionP->X_add_symbol = symbolP;
1267 expressionP->X_add_number = 0;
1269 *input_line_pointer = c;
1270 SKIP_WHITESPACE ();
1272 break;
1274 #endif
1276 symbolP = symbol_find_or_make (name);
1278 /* If we have an absolute symbol or a reg, then we know its
1279 value now. */
1280 segment = S_GET_SEGMENT (symbolP);
1281 if (segment == absolute_section)
1283 expressionP->X_op = O_constant;
1284 expressionP->X_add_number = S_GET_VALUE (symbolP);
1286 else if (segment == reg_section)
1288 expressionP->X_op = O_register;
1289 expressionP->X_add_number = S_GET_VALUE (symbolP);
1291 else
1293 expressionP->X_op = O_symbol;
1294 expressionP->X_add_symbol = symbolP;
1295 expressionP->X_add_number = 0;
1297 *input_line_pointer = c;
1299 else
1301 /* Let the target try to parse it. Success is indicated by changing
1302 the X_op field to something other than O_absent and pointing
1303 input_line_pointer passed the expression. If it can't parse the
1304 expression, X_op and input_line_pointer should be unchanged. */
1305 expressionP->X_op = O_absent;
1306 --input_line_pointer;
1307 md_operand (expressionP);
1308 if (expressionP->X_op == O_absent)
1310 ++input_line_pointer;
1311 as_bad (_("Bad expression"));
1312 expressionP->X_op = O_constant;
1313 expressionP->X_add_number = 0;
1316 break;
1320 * It is more 'efficient' to clean up the expressionS when they are created.
1321 * Doing it here saves lines of code.
1323 clean_up_expression (expressionP);
1324 SKIP_WHITESPACE (); /*->1st char after operand. */
1325 know (*input_line_pointer != ' ');
1327 /* The PA port needs this information. */
1328 if (expressionP->X_add_symbol)
1329 symbol_mark_used (expressionP->X_add_symbol);
1331 switch (expressionP->X_op)
1333 default:
1334 return absolute_section;
1335 case O_symbol:
1336 return S_GET_SEGMENT (expressionP->X_add_symbol);
1337 case O_register:
1338 return reg_section;
1340 } /* operand() */
1342 /* Internal. Simplify a struct expression for use by expr() */
1345 * In: address of a expressionS.
1346 * The X_op field of the expressionS may only take certain values.
1347 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1348 * Out: expressionS may have been modified:
1349 * 'foo-foo' symbol references cancelled to 0,
1350 * which changes X_op from O_subtract to O_constant.
1351 * Unused fields zeroed to help expr().
1354 static void
1355 clean_up_expression (expressionP)
1356 expressionS *expressionP;
1358 switch (expressionP->X_op)
1360 case O_illegal:
1361 case O_absent:
1362 expressionP->X_add_number = 0;
1363 /* Fall through. */
1364 case O_big:
1365 case O_constant:
1366 case O_register:
1367 expressionP->X_add_symbol = NULL;
1368 /* Fall through. */
1369 case O_symbol:
1370 case O_uminus:
1371 case O_bit_not:
1372 expressionP->X_op_symbol = NULL;
1373 break;
1374 case O_subtract:
1375 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1376 || ((symbol_get_frag (expressionP->X_op_symbol)
1377 == symbol_get_frag (expressionP->X_add_symbol))
1378 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1379 && (S_GET_VALUE (expressionP->X_op_symbol)
1380 == S_GET_VALUE (expressionP->X_add_symbol))))
1382 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1383 - S_GET_VALUE (expressionP->X_op_symbol));
1385 expressionP->X_op = O_constant;
1386 expressionP->X_add_symbol = NULL;
1387 expressionP->X_op_symbol = NULL;
1388 expressionP->X_add_number += diff;
1390 break;
1391 default:
1392 break;
1396 /* Expression parser. */
1399 * We allow an empty expression, and just assume (absolute,0) silently.
1400 * Unary operators and parenthetical expressions are treated as operands.
1401 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1403 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1404 * warped that I flushed it and wrote a recursive-descent parser instead.
1405 * Now things are stable, would anybody like to write a fast parser?
1406 * Most expressions are either register (which does not even reach here)
1407 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1408 * So I guess it doesn't really matter how inefficient more complex expressions
1409 * are parsed.
1411 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1412 * Also, we have consumed any leading or trailing spaces (operand does that)
1413 * and done all intervening operators.
1415 * This returns the segment of the result, which will be
1416 * absolute_section or the segment of a symbol.
1419 #undef __
1420 #define __ O_illegal
1422 static const operatorT op_encoding[256] =
1423 { /* maps ASCII->operators */
1425 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1426 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1428 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1429 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1430 __, __, __, __, __, __, __, __,
1431 __, __, __, __, O_lt, __, O_gt, __,
1432 __, __, __, __, __, __, __, __,
1433 __, __, __, __, __, __, __, __,
1434 __, __, __, __, __, __, __, __,
1435 __, __, __,
1436 #ifdef NEED_INDEX_OPERATOR
1437 O_index,
1438 #else
1440 #endif
1441 __, __, O_bit_exclusive_or, __,
1442 __, __, __, __, __, __, __, __,
1443 __, __, __, __, __, __, __, __,
1444 __, __, __, __, __, __, __, __,
1445 __, __, __, __, O_bit_inclusive_or, __, __, __,
1447 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1448 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1449 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1450 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1451 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1452 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1453 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1454 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1459 * Rank Examples
1460 * 0 operand, (expression)
1461 * 1 ||
1462 * 2 &&
1463 * 3 = <> < <= >= >
1464 * 4 + -
1465 * 5 used for * / % in MRI mode
1466 * 6 & ^ ! |
1467 * 7 * / % << >>
1468 * 8 unary - unary ~
1470 static operator_rankT op_rank[] =
1472 0, /* O_illegal */
1473 0, /* O_absent */
1474 0, /* O_constant */
1475 0, /* O_symbol */
1476 0, /* O_symbol_rva */
1477 0, /* O_register */
1478 0, /* O_bit */
1479 9, /* O_uminus */
1480 9, /* O_bit_not */
1481 9, /* O_logical_not */
1482 8, /* O_multiply */
1483 8, /* O_divide */
1484 8, /* O_modulus */
1485 8, /* O_left_shift */
1486 8, /* O_right_shift */
1487 7, /* O_bit_inclusive_or */
1488 7, /* O_bit_or_not */
1489 7, /* O_bit_exclusive_or */
1490 7, /* O_bit_and */
1491 5, /* O_add */
1492 5, /* O_subtract */
1493 4, /* O_eq */
1494 4, /* O_ne */
1495 4, /* O_lt */
1496 4, /* O_le */
1497 4, /* O_ge */
1498 4, /* O_gt */
1499 3, /* O_logical_and */
1500 2, /* O_logical_or */
1501 1, /* O_index */
1502 0, /* O_md1 */
1503 0, /* O_md2 */
1504 0, /* O_md3 */
1505 0, /* O_md4 */
1506 0, /* O_md5 */
1507 0, /* O_md6 */
1508 0, /* O_md7 */
1509 0, /* O_md8 */
1510 0, /* O_md9 */
1511 0, /* O_md10 */
1512 0, /* O_md11 */
1513 0, /* O_md12 */
1514 0, /* O_md13 */
1515 0, /* O_md14 */
1516 0, /* O_md15 */
1517 0, /* O_md16 */
1520 /* Unfortunately, in MRI mode for the m68k, multiplication and
1521 division have lower precedence than the bit wise operators. This
1522 function sets the operator precedences correctly for the current
1523 mode. Also, MRI uses a different bit_not operator, and this fixes
1524 that as well. */
1526 #define STANDARD_MUL_PRECEDENCE (7)
1527 #define MRI_MUL_PRECEDENCE (5)
1529 void
1530 expr_set_precedence ()
1532 if (flag_m68k_mri)
1534 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1535 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1536 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1538 else
1540 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1541 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1542 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1546 /* Initialize the expression parser. */
1548 void
1549 expr_begin ()
1551 expr_set_precedence ();
1553 /* Verify that X_op field is wide enough. */
1555 expressionS e;
1556 e.X_op = O_max;
1557 assert (e.X_op == O_max);
1561 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1562 Advance INPUT_LINE_POINTER to the last character in the operator
1563 (i.e., don't change it for a single character operator). */
1565 static inline operatorT
1566 operator ()
1568 int c;
1569 operatorT ret;
1571 c = *input_line_pointer & 0xff;
1573 switch (c)
1575 default:
1576 return op_encoding[c];
1578 case '<':
1579 switch (input_line_pointer[1])
1581 default:
1582 return op_encoding[c];
1583 case '<':
1584 ret = O_left_shift;
1585 break;
1586 case '>':
1587 ret = O_ne;
1588 break;
1589 case '=':
1590 ret = O_le;
1591 break;
1593 ++input_line_pointer;
1594 return ret;
1596 case '=':
1597 if (input_line_pointer[1] != '=')
1598 return op_encoding[c];
1600 ++input_line_pointer;
1601 return O_eq;
1603 case '>':
1604 switch (input_line_pointer[1])
1606 default:
1607 return op_encoding[c];
1608 case '>':
1609 ret = O_right_shift;
1610 break;
1611 case '=':
1612 ret = O_ge;
1613 break;
1615 ++input_line_pointer;
1616 return ret;
1618 case '!':
1619 /* We accept !! as equivalent to ^ for MRI compatibility. */
1620 if (input_line_pointer[1] != '!')
1622 if (flag_m68k_mri)
1623 return O_bit_inclusive_or;
1624 return op_encoding[c];
1626 ++input_line_pointer;
1627 return O_bit_exclusive_or;
1629 case '|':
1630 if (input_line_pointer[1] != '|')
1631 return op_encoding[c];
1633 ++input_line_pointer;
1634 return O_logical_or;
1636 case '&':
1637 if (input_line_pointer[1] != '&')
1638 return op_encoding[c];
1640 ++input_line_pointer;
1641 return O_logical_and;
1644 /*NOTREACHED*/
1647 /* Parse an expression. */
1649 segT
1650 expr (rankarg, resultP)
1651 int rankarg; /* Larger # is higher rank. */
1652 expressionS *resultP; /* Deliver result here. */
1654 operator_rankT rank = (operator_rankT) rankarg;
1655 segT retval;
1656 expressionS right;
1657 operatorT op_left;
1658 operatorT op_right;
1660 know (rank >= 0);
1662 retval = operand (resultP);
1664 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
1666 op_left = operator ();
1667 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1669 segT rightseg;
1671 input_line_pointer++; /*->after 1st character of operator. */
1673 rightseg = expr (op_rank[(int) op_left], &right);
1674 if (right.X_op == O_absent)
1676 as_warn (_("missing operand; zero assumed"));
1677 right.X_op = O_constant;
1678 right.X_add_number = 0;
1679 right.X_add_symbol = NULL;
1680 right.X_op_symbol = NULL;
1683 know (*input_line_pointer != ' ');
1685 if (op_left == O_index)
1687 if (*input_line_pointer != ']')
1688 as_bad ("missing right bracket");
1689 else
1691 ++input_line_pointer;
1692 SKIP_WHITESPACE ();
1696 if (retval == undefined_section)
1698 if (SEG_NORMAL (rightseg))
1699 retval = rightseg;
1701 else if (! SEG_NORMAL (retval))
1702 retval = rightseg;
1703 else if (SEG_NORMAL (rightseg)
1704 && retval != rightseg
1705 #ifdef DIFF_EXPR_OK
1706 && op_left != O_subtract
1707 #endif
1709 as_bad (_("operation combines symbols in different segments"));
1711 op_right = operator ();
1713 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1714 know ((int) op_left >= (int) O_multiply
1715 && (int) op_left <= (int) O_logical_or);
1717 /* input_line_pointer->after right-hand quantity. */
1718 /* left-hand quantity in resultP */
1719 /* right-hand quantity in right. */
1720 /* operator in op_left. */
1722 if (resultP->X_op == O_big)
1724 if (resultP->X_add_number > 0)
1725 as_warn (_("left operand is a bignum; integer 0 assumed"));
1726 else
1727 as_warn (_("left operand is a float; integer 0 assumed"));
1728 resultP->X_op = O_constant;
1729 resultP->X_add_number = 0;
1730 resultP->X_add_symbol = NULL;
1731 resultP->X_op_symbol = NULL;
1733 if (right.X_op == O_big)
1735 if (right.X_add_number > 0)
1736 as_warn (_("right operand is a bignum; integer 0 assumed"));
1737 else
1738 as_warn (_("right operand is a float; integer 0 assumed"));
1739 right.X_op = O_constant;
1740 right.X_add_number = 0;
1741 right.X_add_symbol = NULL;
1742 right.X_op_symbol = NULL;
1745 /* Optimize common cases. */
1746 if (op_left == O_add && right.X_op == O_constant)
1748 /* X + constant. */
1749 resultP->X_add_number += right.X_add_number;
1751 /* This case comes up in PIC code. */
1752 else if (op_left == O_subtract
1753 && right.X_op == O_symbol
1754 && resultP->X_op == O_symbol
1755 && (symbol_get_frag (right.X_add_symbol)
1756 == symbol_get_frag (resultP->X_add_symbol))
1757 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1760 resultP->X_add_number -= right.X_add_number;
1761 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1762 - S_GET_VALUE (right.X_add_symbol));
1763 resultP->X_op = O_constant;
1764 resultP->X_add_symbol = 0;
1766 else if (op_left == O_subtract && right.X_op == O_constant)
1768 /* X - constant. */
1769 resultP->X_add_number -= right.X_add_number;
1771 else if (op_left == O_add && resultP->X_op == O_constant)
1773 /* Constant + X. */
1774 resultP->X_op = right.X_op;
1775 resultP->X_add_symbol = right.X_add_symbol;
1776 resultP->X_op_symbol = right.X_op_symbol;
1777 resultP->X_add_number += right.X_add_number;
1778 retval = rightseg;
1780 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1782 /* Constant OP constant. */
1783 offsetT v = right.X_add_number;
1784 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1786 as_warn (_("division by zero"));
1787 v = 1;
1789 switch (op_left)
1791 default: abort ();
1792 case O_multiply: resultP->X_add_number *= v; break;
1793 case O_divide: resultP->X_add_number /= v; break;
1794 case O_modulus: resultP->X_add_number %= v; break;
1795 case O_left_shift: resultP->X_add_number <<= v; break;
1796 case O_right_shift:
1797 /* We always use unsigned shifts, to avoid relying on
1798 characteristics of the compiler used to compile gas. */
1799 resultP->X_add_number =
1800 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1801 break;
1802 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1803 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1804 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1805 case O_bit_and: resultP->X_add_number &= v; break;
1806 case O_add: resultP->X_add_number += v; break;
1807 case O_subtract: resultP->X_add_number -= v; break;
1808 case O_eq:
1809 resultP->X_add_number =
1810 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1811 break;
1812 case O_ne:
1813 resultP->X_add_number =
1814 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1815 break;
1816 case O_lt:
1817 resultP->X_add_number =
1818 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1819 break;
1820 case O_le:
1821 resultP->X_add_number =
1822 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1823 break;
1824 case O_ge:
1825 resultP->X_add_number =
1826 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1827 break;
1828 case O_gt:
1829 resultP->X_add_number =
1830 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1831 break;
1832 case O_logical_and:
1833 resultP->X_add_number = resultP->X_add_number && v;
1834 break;
1835 case O_logical_or:
1836 resultP->X_add_number = resultP->X_add_number || v;
1837 break;
1840 else if (resultP->X_op == O_symbol
1841 && right.X_op == O_symbol
1842 && (op_left == O_add
1843 || op_left == O_subtract
1844 || (resultP->X_add_number == 0
1845 && right.X_add_number == 0)))
1847 /* Symbol OP symbol. */
1848 resultP->X_op = op_left;
1849 resultP->X_op_symbol = right.X_add_symbol;
1850 if (op_left == O_add)
1851 resultP->X_add_number += right.X_add_number;
1852 else if (op_left == O_subtract)
1853 resultP->X_add_number -= right.X_add_number;
1855 else
1857 /* The general case. */
1858 resultP->X_add_symbol = make_expr_symbol (resultP);
1859 resultP->X_op_symbol = make_expr_symbol (&right);
1860 resultP->X_op = op_left;
1861 resultP->X_add_number = 0;
1862 resultP->X_unsigned = 1;
1865 op_left = op_right;
1866 } /* While next operator is >= this rank. */
1868 /* The PA port needs this information. */
1869 if (resultP->X_add_symbol)
1870 symbol_mark_used (resultP->X_add_symbol);
1872 return resultP->X_op == O_constant ? absolute_section : retval;
1876 * get_symbol_end()
1878 * This lives here because it belongs equally in expr.c & read.c.
1879 * Expr.c is just a branch office read.c anyway, and putting it
1880 * here lessens the crowd at read.c.
1882 * Assume input_line_pointer is at start of symbol name.
1883 * Advance input_line_pointer past symbol name.
1884 * Turn that character into a '\0', returning its former value.
1885 * This allows a string compare (RMS wants symbol names to be strings)
1886 * of the symbol name.
1887 * There will always be a char following symbol name, because all good
1888 * lines end in end-of-line.
1890 char
1891 get_symbol_end ()
1893 char c;
1895 /* We accept \001 in a name in case this is being called with a
1896 constructed string. */
1897 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1899 while (is_part_of_name (c = *input_line_pointer++)
1900 || c == '\001')
1902 if (is_name_ender (c))
1903 c = *input_line_pointer++;
1905 *--input_line_pointer = 0;
1906 return (c);
1910 unsigned int
1911 get_single_number ()
1913 expressionS exp;
1914 operand (&exp);
1915 return exp.X_add_number;
1919 /* end of expr.c */