]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/expr.c
* testsuite/regress-demangle (failed test): Show result and
[thirdparty/binutils-gdb.git] / gas / expr.c
CommitLineData
252b5132 1/* expr.c -operands, expressions-
abd63a32 2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
252b5132
RH
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
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.
11
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.
16
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. */
21
22/*
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.
27 */
28
29#include <ctype.h>
30#include <string.h>
31#define min(a, b) ((a) < (b) ? (a) : (b))
32
33#include "as.h"
34#include "obstack.h"
35
36static void floating_constant PARAMS ((expressionS * expressionP));
6d4d30bb
AM
37static valueT generic_bignum_to_int32 PARAMS ((void));
38#ifdef BFD64
39static valueT generic_bignum_to_int64 PARAMS ((void));
40#endif
252b5132
RH
41static void integer_constant PARAMS ((int radix, expressionS * expressionP));
42static void mri_char_constant PARAMS ((expressionS *));
43static void current_location PARAMS ((expressionS *));
44static void clean_up_expression PARAMS ((expressionS * expressionP));
45static segT operand PARAMS ((expressionS *));
46static operatorT operator PARAMS ((void));
47
48extern const char EXP_CHARS[], FLT_CHARS[];
49
50/* We keep a mapping of expression symbols to file positions, so that
51 we can provide better error messages. */
52
53struct expr_symbol_line
54{
55 struct expr_symbol_line *next;
56 symbolS *sym;
57 char *file;
58 unsigned int line;
59};
60
61static struct expr_symbol_line *expr_symbol_lines;
62\f
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. */
66
67symbolS *
68make_expr_symbol (expressionP)
69 expressionS *expressionP;
70{
71 expressionS zero;
72 const char *fake;
73 symbolS *symbolP;
74 struct expr_symbol_line *n;
75
76 if (expressionP->X_op == O_symbol
77 && expressionP->X_add_number == 0)
78 return expressionP->X_add_symbol;
79
80 if (expressionP->X_op == O_big)
81 {
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;
94 }
95
96 fake = FAKE_LABEL_NAME;
97
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);
49309057 107 symbol_set_value_expression (symbolP, expressionP);
252b5132
RH
108
109 if (expressionP->X_op == O_constant)
110 resolve_symbol_value (symbolP, 1);
111
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;
117
118 return symbolP;
119}
120
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. */
124
125int
126expr_symbol_where (sym, pfile, pline)
127 symbolS *sym;
128 char **pfile;
129 unsigned int *pline;
130{
131 register struct expr_symbol_line *l;
132
133 for (l = expr_symbol_lines; l != NULL; l = l->next)
134 {
135 if (l->sym == sym)
136 {
137 *pfile = l->file;
138 *pline = l->line;
139 return 1;
140 }
141 }
142
143 return 0;
144}
145\f
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. */
152
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. */
157
158symbolS *
159expr_build_uconstant (value)
160 offsetT value;
161{
162 expressionS e;
163
164 e.X_op = O_constant;
165 e.X_add_number = value;
166 e.X_unsigned = 1;
167 return make_expr_symbol (&e);
168}
169
170/* Build an expression for OP s1. */
171
172symbolS *
173expr_build_unary (op, s1)
174 operatorT op;
175 symbolS *s1;
176{
177 expressionS e;
178
179 e.X_op = op;
180 e.X_add_symbol = s1;
181 e.X_add_number = 0;
182 return make_expr_symbol (&e);
183}
184
185/* Build an expression for s1 OP s2. */
186
187symbolS *
188expr_build_binary (op, s1, s2)
189 operatorT op;
190 symbolS *s1;
191 symbolS *s2;
192{
193 expressionS e;
194
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);
200}
201
202/* Build an expression for the current location ('.'). */
203
204symbolS *
205expr_build_dot ()
206{
207 expressionS e;
208
209 current_location (&e);
210 return make_expr_symbol (&e);
211}
212\f
213/*
214 * Build any floating-point literal here.
215 * Also build any bignum literal here.
216 */
217
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. */
223LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
224FLONUM_TYPE generic_floating_point_number =
225{
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 */
231};
232/* If nonzero, we've been asked to assemble nan, +inf or -inf */
233int generic_floating_point_magic;
234\f
235static void
236floating_constant (expressionP)
237 expressionS *expressionP;
238{
239 /* input_line_pointer->*/
240 /* floating-point constant. */
241 int error_code;
242
243 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
244 &generic_floating_point_number);
245
246 if (error_code)
247 {
248 if (error_code == ERROR_EXPONENT_OVERFLOW)
249 {
250 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
251 }
252 else
253 {
254 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code);
255 }
256 }
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;
261}
262
263static valueT
264generic_bignum_to_int32 ()
265{
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;
271}
272
273#ifdef BFD64
274static valueT
275generic_bignum_to_int64 ()
276{
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;
286}
287#endif
288
289static void
290integer_constant (radix, expressionP)
291 int radix;
292 expressionS *expressionP;
293{
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 */
303
304 int small; /* true if fits in 32 bits. */
305
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. */
323
324#ifdef BFD64
325#define valuesize 64
326#else /* includes non-bfd case, mostly */
327#define valuesize 32
328#endif
329
f805106c 330 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
252b5132
RH
331 {
332 int flt = 0;
333
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++)
340 {
341 if (*suffix == 'e' || *suffix == 'E')
342 flt = 1;
343 }
344
345 if (suffix == input_line_pointer)
346 {
347 radix = 10;
348 suffix = NULL;
349 }
350 else
351 {
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)
364 {
365 floating_constant (expressionP);
366 return;
367 }
368 else
369 {
370 radix = 10;
371 suffix = NULL;
372 }
373 }
374 }
375
376 switch (radix)
377 {
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;
fa16d387 392 too_many_digits = (valuesize + 11) / 4; /* very rough */
252b5132
RH
393 }
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++)
400 {
401 number = number * radix + digit;
402 }
403 /* c contains character after number. */
404 /* input_line_pointer->char after c. */
405 small = (input_line_pointer - start - 1) < too_many_digits;
406
407 if (radix == 16 && c == '_')
408 {
409 /* This is literal of the form 0x333_0_12345678_1.
410 This example is equivalent to 0x00000333000000001234567800000001. */
411
412 int num_little_digits = 0;
413 int i;
414 input_line_pointer = start; /*->1st digit. */
415
416 know (LITTLENUM_NUMBER_OF_BITS == 16);
417
418 for (c = '_'; c == '_'; num_little_digits+=2)
419 {
420
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++))
427 {
428 number = number * radix + digit;
429 ndigit++;
430 }
431
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."));
435
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];
440
441 /* Add the new digits as the least significant new ones. */
442 generic_bignum[0] = number & 0xffffffff;
443 generic_bignum[1] = number >> 16;
444 }
445
446 /* Again, c is char after number, input_line_pointer->after c. */
447
448 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
449 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
450
451 assert (num_little_digits >= 4);
452
453 if (num_little_digits != 8)
454 as_bad (_("A bignum with underscores must have exactly 4 words."));
455
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.
458 */
459 while (generic_bignum[num_little_digits-1] == 0 && num_little_digits > 1)
460 num_little_digits--;
461
462 if (num_little_digits <= 2)
463 {
464 /* will fit into 32 bits. */
465 number = generic_bignum_to_int32 ();
466 small = 1;
467 }
468#ifdef BFD64
469 else if (num_little_digits <= 4)
470 {
471 /* Will fit into 64 bits. */
472 number = generic_bignum_to_int64 ();
473 small = 1;
474 }
475#endif
476 else
477 {
478 small = 0;
479 number = num_little_digits; /* number of littlenums in the bignum. */
480 }
481 }
482 else if (!small)
483 {
484 /*
485 * we saw a lot of digits. manufacture a bignum the hard way.
486 */
487 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
488 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
489 long carry;
490
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++)
501 {
502 for (pointer = generic_bignum;
503 pointer <= leader;
504 pointer++)
505 {
506 long work;
507
508 work = carry + radix * *pointer;
509 *pointer = work & LITTLENUM_MASK;
510 carry = work >> LITTLENUM_NUMBER_OF_BITS;
511 }
512 if (carry)
513 {
514 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
515 {
516 /* room to grow a longer bignum. */
517 *++leader = carry;
518 }
519 }
520 }
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)
525 {
526 /* will fit into 32 bits. */
527 number = generic_bignum_to_int32 ();
528 small = 1;
529 }
530#ifdef BFD64
531 else if (leader < generic_bignum + 4)
532 {
533 /* Will fit into 64 bits. */
534 number = generic_bignum_to_int64 ();
535 small = 1;
536 }
537#endif
538 else
539 {
540 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
541 }
542 }
543
60bcf0fa
NC
544 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
545 && suffix != NULL
f805106c 546 && input_line_pointer - 1 == suffix)
252b5132
RH
547 c = *input_line_pointer++;
548
549 if (small)
550 {
551 /*
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!
556 */
557
558 if (LOCAL_LABELS_FB && c == 'b')
559 {
560 /*
561 * backward ref to local label.
562 * because it is backward, expect it to be defined.
563 */
564 /* Construct a local label. */
565 name = fb_label_name ((int) number, 0);
566
567 /* seen before, or symbol is defined: ok */
568 symbolP = symbol_find (name);
569 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
570 {
571 /* local labels are never absolute. don't waste time
572 checking absoluteness. */
573 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
574
575 expressionP->X_op = O_symbol;
576 expressionP->X_add_symbol = symbolP;
577 }
578 else
579 {
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;
586 }
587
588 expressionP->X_add_number = 0;
589 } /* case 'b' */
590 else if (LOCAL_LABELS_FB && c == 'f')
591 {
592 /*
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.
599 */
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 == '$')
613 {
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. */
618
619 if (dollar_label_defined ((long) number))
620 {
621 name = dollar_label_name ((long) number, 0);
622 symbolP = symbol_find (name);
623 know (symbolP != NULL);
624 }
625 else
626 {
627 name = dollar_label_name ((long) number, 1);
628 symbolP = symbol_find_or_make (name);
629 }
630
631 expressionP->X_op = O_symbol;
632 expressionP->X_add_symbol = symbolP;
633 expressionP->X_add_number = 0;
634 } /* case '$' */
635 else
636 {
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 */
645 }
646 else
647 {
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. */
652 }
653}
654
655/* Parse an MRI multi character constant. */
656
657static void
658mri_char_constant (expressionP)
659 expressionS *expressionP;
660{
661 int i;
662
663 if (*input_line_pointer == '\''
664 && input_line_pointer[1] != '\'')
665 {
666 expressionP->X_op = O_constant;
667 expressionP->X_add_number = 0;
668 return;
669 }
670
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--)
674 {
675 int j;
676
677 generic_bignum[i] = 0;
678 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
679 {
680 if (*input_line_pointer == '\'')
681 {
682 if (input_line_pointer[1] != '\'')
683 break;
684 ++input_line_pointer;
685 }
686 generic_bignum[i] <<= 8;
687 generic_bignum[i] += *input_line_pointer;
688 ++input_line_pointer;
689 }
690
691 if (i < SIZE_OF_LARGE_NUMBER - 1)
692 {
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;
698 }
699
700 if (*input_line_pointer == '\''
701 && input_line_pointer[1] != '\'')
702 break;
703 }
704
705 if (i < 0)
706 {
707 as_bad (_("Character constant too large"));
708 i = 0;
709 }
710
711 if (i > 0)
712 {
713 int c;
714 int j;
715
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;
720 }
721
722 know (LITTLENUM_NUMBER_OF_BITS == 16);
723 if (i > 2)
724 {
725 expressionP->X_op = O_big;
726 expressionP->X_add_number = i;
727 }
728 else
729 {
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));
738 }
739
740 /* Skip the final closing quote. */
741 ++input_line_pointer;
742}
743
744/* Return an expression representing the current location. This
745 handles the magic symbol `.'. */
746
747static void
748current_location (expressionp)
749 expressionS *expressionp;
750{
751 if (now_seg == absolute_section)
752 {
753 expressionp->X_op = O_constant;
754 expressionp->X_add_number = abs_section_offset;
755 }
756 else
757 {
758 symbolS *symbolp;
759
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;
766 }
767}
768
769/*
770 * Summary of operand().
771 *
772 * in: Input_line_pointer points to 1st char of operand, which may
773 * be a space.
774 *
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.
778 */
779
780static segT
781operand (expressionP)
782 expressionS *expressionP;
783{
784 char c;
785 symbolS *symbolP; /* points to symbol */
786 char *name; /* points to name of symbol */
787 segT segment;
788
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;
796
797 /* digits, assume it is a bignum. */
798
799 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
800 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
801
b75c0c92
AM
802 if (is_end_of_line[(unsigned char) c])
803 goto eol;
804
252b5132
RH
805 switch (c)
806 {
807 case '1':
808 case '2':
809 case '3':
810 case '4':
811 case '5':
812 case '6':
813 case '7':
814 case '8':
815 case '9':
816 input_line_pointer--;
817
60bcf0fa 818 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
f805106c
TW
819 ? 0 : 10,
820 expressionP);
252b5132
RH
821 break;
822
ed6d6fd3
FCE
823#ifdef LITERAL_PREFIXDOLLAR_HEX
824 case '$':
825 integer_constant (16, expressionP);
826 break;
827#endif
828
252b5132
RH
829 case '0':
830 /* non-decimal radix */
831
f805106c 832 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
833 {
834 char *s;
835
836 /* Check for a hex constant. */
837 for (s = input_line_pointer; hex_p (*s); s++)
838 ;
839 if (*s == 'h' || *s == 'H')
840 {
841 --input_line_pointer;
842 integer_constant (0, expressionP);
843 break;
844 }
f805106c 845 }
252b5132
RH
846 c = *input_line_pointer;
847 switch (c)
848 {
849 case 'o':
850 case 'O':
851 case 'q':
852 case 'Q':
853 case '8':
854 case '9':
f805106c 855 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
856 {
857 integer_constant (0, expressionP);
858 break;
859 }
860 /* Fall through. */
861 default:
862 default_case:
863 if (c && strchr (FLT_CHARS, c))
864 {
865 input_line_pointer++;
866 floating_constant (expressionP);
867 expressionP->X_add_number =
868 - (isupper ((unsigned char) c) ? tolower (c) : c);
869 }
870 else
871 {
872 /* The string was only zero */
873 expressionP->X_op = O_constant;
874 expressionP->X_add_number = 0;
875 }
876
877 break;
878
879 case 'x':
880 case 'X':
ab266a97 881 if (flag_m68k_mri)
252b5132
RH
882 goto default_case;
883 input_line_pointer++;
884 integer_constant (16, expressionP);
885 break;
886
887 case 'b':
6dc19fc4 888 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
252b5132
RH
889 {
890 /* This code used to check for '+' and '-' here, and, in
891 some conditions, fall through to call
892 integer_constant. However, that didn't make sense,
893 as integer_constant only accepts digits. */
894 /* Some of our code elsewhere does permit digits greater
895 than the expected base; for consistency, do the same
896 here. */
897 if (input_line_pointer[1] < '0'
898 || input_line_pointer[1] > '9')
899 {
900 /* Parse this as a back reference to label 0. */
901 input_line_pointer--;
902 integer_constant (10, expressionP);
903 break;
904 }
905 /* Otherwise, parse this as a binary number. */
906 }
907 /* Fall through. */
908 case 'B':
909 input_line_pointer++;
6dc19fc4 910 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
911 goto default_case;
912 integer_constant (2, expressionP);
913 break;
914
915 case '0':
916 case '1':
917 case '2':
918 case '3':
919 case '4':
920 case '5':
921 case '6':
922 case '7':
6dc19fc4
TW
923 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
924 ? 0 : 8,
925 expressionP);
252b5132
RH
926 break;
927
928 case 'f':
929 if (LOCAL_LABELS_FB)
930 {
931 /* If it says "0f" and it could possibly be a floating point
932 number, make it one. Otherwise, make it a local label,
933 and try to deal with parsing the rest later. */
934 if (!input_line_pointer[1]
271bb601
HPN
935 || (is_end_of_line[0xff & input_line_pointer[1]])
936 || strchr (FLT_CHARS, 'f') == NULL)
252b5132
RH
937 goto is_0f_label;
938 {
939 char *cp = input_line_pointer + 1;
940 int r = atof_generic (&cp, ".", EXP_CHARS,
941 &generic_floating_point_number);
942 switch (r)
943 {
944 case 0:
945 case ERROR_EXPONENT_OVERFLOW:
946 if (*cp == 'f' || *cp == 'b')
947 /* looks like a difference expression */
948 goto is_0f_label;
949 else if (cp == input_line_pointer + 1)
950 /* No characters has been accepted -- looks like
951 end of operand. */
952 goto is_0f_label;
953 else
954 goto is_0f_float;
955 default:
956 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
957 r);
958 }
959 }
960
961 /* Okay, now we've sorted it out. We resume at one of these
962 two labels, depending on what we've decided we're probably
963 looking at. */
964 is_0f_label:
965 input_line_pointer--;
966 integer_constant (10, expressionP);
967 break;
968
969 is_0f_float:
970 /* fall through */
971 ;
972 }
973
974 case 'd':
975 case 'D':
6dc19fc4 976 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
977 {
978 integer_constant (0, expressionP);
979 break;
980 }
981 /* Fall through. */
982 case 'F':
983 case 'r':
984 case 'e':
985 case 'E':
986 case 'g':
987 case 'G':
988 input_line_pointer++;
989 floating_constant (expressionP);
990 expressionP->X_add_number =
991 - (isupper ((unsigned char) c) ? tolower (c) : c);
992 break;
993
994 case '$':
995 if (LOCAL_LABELS_DOLLAR)
996 {
997 integer_constant (10, expressionP);
998 break;
999 }
1000 else
1001 goto default_case;
1002 }
1003
1004 break;
1005
1006 case '(':
b585bc2c 1007#ifndef NEED_INDEX_OPERATOR
252b5132 1008 case '[':
b585bc2c 1009#endif
252b5132
RH
1010 /* didn't begin with digit & not a name */
1011 segment = expression (expressionP);
1012 /* Expression() will pass trailing whitespace */
f7c88872
AM
1013 if ((c == '(' && *input_line_pointer != ')')
1014 || (c == '[' && *input_line_pointer != ']'))
252b5132 1015 {
f7c88872
AM
1016#ifdef RELAX_PAREN_GROUPING
1017 if (c != '(')
1018#endif
1019 as_bad (_("Missing '%c' assumed"), c == '(' ? ')' : ']');
252b5132 1020 }
f7c88872
AM
1021 else
1022 input_line_pointer++;
252b5132
RH
1023 SKIP_WHITESPACE ();
1024 /* here with input_line_pointer->char after "(...)" */
1025 return segment;
1026
abd63a32 1027#ifdef TC_M68K
252b5132
RH
1028 case 'E':
1029 if (! flag_m68k_mri || *input_line_pointer != '\'')
1030 goto de_fault;
1031 as_bad (_("EBCDIC constants are not supported"));
1032 /* Fall through. */
1033 case 'A':
1034 if (! flag_m68k_mri || *input_line_pointer != '\'')
1035 goto de_fault;
1036 ++input_line_pointer;
1037 /* Fall through. */
abd63a32 1038#endif
252b5132
RH
1039 case '\'':
1040 if (! flag_m68k_mri)
1041 {
1042 /* Warning: to conform to other people's assemblers NO
1043 ESCAPEMENT is permitted for a single quote. The next
1044 character, parity errors and all, is taken as the value
1045 of the operand. VERY KINKY. */
1046 expressionP->X_op = O_constant;
1047 expressionP->X_add_number = *input_line_pointer++;
1048 break;
1049 }
1050
1051 mri_char_constant (expressionP);
1052 break;
1053
1054 case '+':
1055 (void) operand (expressionP);
1056 break;
1057
abd63a32 1058#ifdef TC_M68K
252b5132
RH
1059 case '"':
1060 /* Double quote is the bitwise not operator in MRI mode. */
1061 if (! flag_m68k_mri)
1062 goto de_fault;
1063 /* Fall through. */
abd63a32 1064#endif
252b5132
RH
1065 case '~':
1066 /* ~ is permitted to start a label on the Delta. */
1067 if (is_name_beginner (c))
1068 goto isname;
1069 case '!':
1070 case '-':
1071 {
1072 operand (expressionP);
1073 if (expressionP->X_op == O_constant)
1074 {
1075 /* input_line_pointer -> char after operand */
1076 if (c == '-')
1077 {
1078 expressionP->X_add_number = - expressionP->X_add_number;
1079 /* Notice: '-' may overflow: no warning is given. This is
1080 compatible with other people's assemblers. Sigh. */
1081 expressionP->X_unsigned = 0;
1082 }
1083 else if (c == '~' || c == '"')
1084 expressionP->X_add_number = ~ expressionP->X_add_number;
1085 else
1086 expressionP->X_add_number = ! expressionP->X_add_number;
1087 }
1088 else if (expressionP->X_op != O_illegal
1089 && expressionP->X_op != O_absent)
1090 {
1091 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1092 if (c == '-')
1093 expressionP->X_op = O_uminus;
1094 else if (c == '~' || c == '"')
1095 expressionP->X_op = O_bit_not;
1096 else
1097 expressionP->X_op = O_logical_not;
1098 expressionP->X_add_number = 0;
1099 }
1100 else
1101 as_warn (_("Unary operator %c ignored because bad operand follows"),
1102 c);
1103 }
1104 break;
1105
abd63a32 1106#if defined (DOLLAR_DOT) || defined (TC_M68K)
252b5132
RH
1107 case '$':
1108 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1109 is defined. */
1110#ifndef DOLLAR_DOT
1111 if (! flag_m68k_mri)
1112 goto de_fault;
1113#endif
1114 if (flag_m68k_mri && hex_p (*input_line_pointer))
1115 {
1116 /* In MRI mode, $ is also used as the prefix for a
1117 hexadecimal constant. */
1118 integer_constant (16, expressionP);
1119 break;
1120 }
1121
1122 if (is_part_of_name (*input_line_pointer))
1123 goto isname;
1124
1125 current_location (expressionP);
1126 break;
abd63a32 1127#endif
252b5132
RH
1128
1129 case '.':
1130 if (!is_part_of_name (*input_line_pointer))
1131 {
1132 current_location (expressionP);
1133 break;
1134 }
1135 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1136 && ! is_part_of_name (input_line_pointer[8]))
1137 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1138 && ! is_part_of_name (input_line_pointer[7])))
1139 {
1140 int start;
1141
1142 start = (input_line_pointer[1] == 't'
1143 || input_line_pointer[1] == 'T');
1144 input_line_pointer += start ? 8 : 7;
1145 SKIP_WHITESPACE ();
1146 if (*input_line_pointer != '(')
1147 as_bad (_("syntax error in .startof. or .sizeof."));
1148 else
1149 {
1150 char *buf;
1151
1152 ++input_line_pointer;
1153 SKIP_WHITESPACE ();
1154 name = input_line_pointer;
1155 c = get_symbol_end ();
1156
1157 buf = (char *) xmalloc (strlen (name) + 10);
1158 if (start)
1159 sprintf (buf, ".startof.%s", name);
1160 else
1161 sprintf (buf, ".sizeof.%s", name);
1162 symbolP = symbol_make (buf);
1163 free (buf);
1164
1165 expressionP->X_op = O_symbol;
1166 expressionP->X_add_symbol = symbolP;
1167 expressionP->X_add_number = 0;
1168
1169 *input_line_pointer = c;
1170 SKIP_WHITESPACE ();
1171 if (*input_line_pointer != ')')
1172 as_bad (_("syntax error in .startof. or .sizeof."));
1173 else
1174 ++input_line_pointer;
1175 }
1176 break;
1177 }
1178 else
1179 {
1180 goto isname;
1181 }
b75c0c92 1182
252b5132 1183 case ',':
252b5132
RH
1184 eol:
1185 /* can't imagine any other kind of operand */
1186 expressionP->X_op = O_absent;
1187 input_line_pointer--;
1188 break;
1189
abd63a32 1190#ifdef TC_M68K
252b5132
RH
1191 case '%':
1192 if (! flag_m68k_mri)
1193 goto de_fault;
1194 integer_constant (2, expressionP);
1195 break;
1196
1197 case '@':
1198 if (! flag_m68k_mri)
1199 goto de_fault;
1200 integer_constant (8, expressionP);
1201 break;
1202
1203 case ':':
1204 if (! flag_m68k_mri)
1205 goto de_fault;
1206
1207 /* In MRI mode, this is a floating point constant represented
1208 using hexadecimal digits. */
1209
1210 ++input_line_pointer;
1211 integer_constant (16, expressionP);
1212 break;
1213
1214 case '*':
1215 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1216 goto de_fault;
1217
1218 current_location (expressionP);
1219 break;
abd63a32 1220#endif
252b5132
RH
1221
1222 default:
abd63a32 1223#ifdef TC_M68K
252b5132 1224 de_fault:
abd63a32 1225#endif
252b5132
RH
1226 if (is_name_beginner (c)) /* here if did not begin with a digit */
1227 {
1228 /*
1229 * Identifier begins here.
1230 * This is kludged for speed, so code is repeated.
1231 */
1232 isname:
1233 name = --input_line_pointer;
1234 c = get_symbol_end ();
1235
1236#ifdef md_parse_name
1237 /* This is a hook for the backend to parse certain names
1238 specially in certain contexts. If a name always has a
1239 specific value, it can often be handled by simply
1240 entering it in the symbol table. */
1241 if (md_parse_name (name, expressionP))
1242 {
1243 *input_line_pointer = c;
1244 break;
1245 }
1246#endif
1247
1248#ifdef TC_I960
1249 /* The MRI i960 assembler permits
1250 lda sizeof code,g13
1251 FIXME: This should use md_parse_name. */
1252 if (flag_mri
1253 && (strcasecmp (name, "sizeof") == 0
1254 || strcasecmp (name, "startof") == 0))
1255 {
1256 int start;
1257 char *buf;
1258
1259 start = (name[1] == 't'
1260 || name[1] == 'T');
1261
1262 *input_line_pointer = c;
1263 SKIP_WHITESPACE ();
1264
1265 name = input_line_pointer;
1266 c = get_symbol_end ();
1267
1268 buf = (char *) xmalloc (strlen (name) + 10);
1269 if (start)
1270 sprintf (buf, ".startof.%s", name);
1271 else
1272 sprintf (buf, ".sizeof.%s", name);
1273 symbolP = symbol_make (buf);
1274 free (buf);
1275
1276 expressionP->X_op = O_symbol;
1277 expressionP->X_add_symbol = symbolP;
1278 expressionP->X_add_number = 0;
1279
1280 *input_line_pointer = c;
1281 SKIP_WHITESPACE ();
1282
1283 break;
1284 }
1285#endif
1286
1287 symbolP = symbol_find_or_make (name);
1288
1289 /* If we have an absolute symbol or a reg, then we know its
1290 value now. */
1291 segment = S_GET_SEGMENT (symbolP);
1292 if (segment == absolute_section)
1293 {
1294 expressionP->X_op = O_constant;
1295 expressionP->X_add_number = S_GET_VALUE (symbolP);
1296 }
1297 else if (segment == reg_section)
1298 {
1299 expressionP->X_op = O_register;
1300 expressionP->X_add_number = S_GET_VALUE (symbolP);
1301 }
1302 else
1303 {
1304 expressionP->X_op = O_symbol;
1305 expressionP->X_add_symbol = symbolP;
1306 expressionP->X_add_number = 0;
1307 }
1308 *input_line_pointer = c;
1309 }
1310 else
1311 {
1312 /* Let the target try to parse it. Success is indicated by changing
1313 the X_op field to something other than O_absent and pointing
1314 input_line_pointer passed the expression. If it can't parse the
1315 expression, X_op and input_line_pointer should be unchanged. */
1316 expressionP->X_op = O_absent;
1317 --input_line_pointer;
1318 md_operand (expressionP);
1319 if (expressionP->X_op == O_absent)
1320 {
1321 ++input_line_pointer;
1322 as_bad (_("Bad expression"));
1323 expressionP->X_op = O_constant;
1324 expressionP->X_add_number = 0;
1325 }
1326 }
1327 break;
1328 }
1329
1330 /*
1331 * It is more 'efficient' to clean up the expressionS when they are created.
1332 * Doing it here saves lines of code.
1333 */
1334 clean_up_expression (expressionP);
1335 SKIP_WHITESPACE (); /*->1st char after operand. */
1336 know (*input_line_pointer != ' ');
1337
1338 /* The PA port needs this information. */
1339 if (expressionP->X_add_symbol)
49309057 1340 symbol_mark_used (expressionP->X_add_symbol);
252b5132
RH
1341
1342 switch (expressionP->X_op)
1343 {
1344 default:
1345 return absolute_section;
1346 case O_symbol:
1347 return S_GET_SEGMENT (expressionP->X_add_symbol);
1348 case O_register:
1349 return reg_section;
1350 }
1351} /* operand() */
1352\f
1353/* Internal. Simplify a struct expression for use by expr() */
1354
1355/*
1356 * In: address of a expressionS.
1357 * The X_op field of the expressionS may only take certain values.
1358 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1359 * Out: expressionS may have been modified:
1360 * 'foo-foo' symbol references cancelled to 0,
1361 * which changes X_op from O_subtract to O_constant.
1362 * Unused fields zeroed to help expr().
1363 */
1364
1365static void
1366clean_up_expression (expressionP)
1367 expressionS *expressionP;
1368{
1369 switch (expressionP->X_op)
1370 {
1371 case O_illegal:
1372 case O_absent:
1373 expressionP->X_add_number = 0;
1374 /* Fall through. */
1375 case O_big:
1376 case O_constant:
1377 case O_register:
1378 expressionP->X_add_symbol = NULL;
1379 /* Fall through. */
1380 case O_symbol:
1381 case O_uminus:
1382 case O_bit_not:
1383 expressionP->X_op_symbol = NULL;
1384 break;
1385 case O_subtract:
1386 if (expressionP->X_op_symbol == expressionP->X_add_symbol
49309057
ILT
1387 || ((symbol_get_frag (expressionP->X_op_symbol)
1388 == symbol_get_frag (expressionP->X_add_symbol))
252b5132
RH
1389 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1390 && (S_GET_VALUE (expressionP->X_op_symbol)
1391 == S_GET_VALUE (expressionP->X_add_symbol))))
1392 {
1393 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1394 - S_GET_VALUE (expressionP->X_op_symbol));
1395
1396 expressionP->X_op = O_constant;
1397 expressionP->X_add_symbol = NULL;
1398 expressionP->X_op_symbol = NULL;
1399 expressionP->X_add_number += diff;
1400 }
1401 break;
1402 default:
1403 break;
1404 }
1405}
1406\f
1407/* Expression parser. */
1408
1409/*
1410 * We allow an empty expression, and just assume (absolute,0) silently.
1411 * Unary operators and parenthetical expressions are treated as operands.
1412 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1413 *
1414 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1415 * warped that I flushed it and wrote a recursive-descent parser instead.
1416 * Now things are stable, would anybody like to write a fast parser?
1417 * Most expressions are either register (which does not even reach here)
1418 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1419 * So I guess it doesn't really matter how inefficient more complex expressions
1420 * are parsed.
1421 *
1422 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1423 * Also, we have consumed any leading or trailing spaces (operand does that)
1424 * and done all intervening operators.
1425 *
1426 * This returns the segment of the result, which will be
1427 * absolute_section or the segment of a symbol.
1428 */
1429
1430#undef __
1431#define __ O_illegal
1432
1433static const operatorT op_encoding[256] =
1434{ /* maps ASCII->operators */
1435
1436 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1437 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1438
1439 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1440 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1441 __, __, __, __, __, __, __, __,
1442 __, __, __, __, O_lt, __, O_gt, __,
1443 __, __, __, __, __, __, __, __,
1444 __, __, __, __, __, __, __, __,
1445 __, __, __, __, __, __, __, __,
b585bc2c
RH
1446 __, __, __,
1447#ifdef NEED_INDEX_OPERATOR
1448 O_index,
1449#else
1450 __,
1451#endif
1452 __, __, O_bit_exclusive_or, __,
252b5132
RH
1453 __, __, __, __, __, __, __, __,
1454 __, __, __, __, __, __, __, __,
1455 __, __, __, __, __, __, __, __,
1456 __, __, __, __, O_bit_inclusive_or, __, __, __,
1457
1458 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1459 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1460 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1461 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1462 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1463 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1464 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1465 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1466};
1467
1468
1469/*
1470 * Rank Examples
1471 * 0 operand, (expression)
1472 * 1 ||
1473 * 2 &&
1474 * 3 = <> < <= >= >
1475 * 4 + -
1476 * 5 used for * / % in MRI mode
1477 * 6 & ^ ! |
1478 * 7 * / % << >>
1479 * 8 unary - unary ~
1480 */
1481static operator_rankT op_rank[] =
1482{
1483 0, /* O_illegal */
1484 0, /* O_absent */
1485 0, /* O_constant */
1486 0, /* O_symbol */
1487 0, /* O_symbol_rva */
1488 0, /* O_register */
1489 0, /* O_bit */
b585bc2c
RH
1490 9, /* O_uminus */
1491 9, /* O_bit_not */
1492 9, /* O_logical_not */
1493 8, /* O_multiply */
1494 8, /* O_divide */
1495 8, /* O_modulus */
1496 8, /* O_left_shift */
1497 8, /* O_right_shift */
1498 7, /* O_bit_inclusive_or */
1499 7, /* O_bit_or_not */
1500 7, /* O_bit_exclusive_or */
1501 7, /* O_bit_and */
1502 5, /* O_add */
1503 5, /* O_subtract */
1504 4, /* O_eq */
1505 4, /* O_ne */
1506 4, /* O_lt */
1507 4, /* O_le */
1508 4, /* O_ge */
1509 4, /* O_gt */
1510 3, /* O_logical_and */
1511 2, /* O_logical_or */
1512 1, /* O_index */
446a06c9
MM
1513 0, /* O_md1 */
1514 0, /* O_md2 */
1515 0, /* O_md3 */
1516 0, /* O_md4 */
1517 0, /* O_md5 */
1518 0, /* O_md6 */
1519 0, /* O_md7 */
1520 0, /* O_md8 */
dd33dc0f
MM
1521 0, /* O_md9 */
1522 0, /* O_md10 */
1523 0, /* O_md11 */
1524 0, /* O_md12 */
1525 0, /* O_md13 */
1526 0, /* O_md14 */
1527 0, /* O_md15 */
1528 0, /* O_md16 */
252b5132
RH
1529};
1530
1531/* Unfortunately, in MRI mode for the m68k, multiplication and
1532 division have lower precedence than the bit wise operators. This
1533 function sets the operator precedences correctly for the current
1534 mode. Also, MRI uses a different bit_not operator, and this fixes
1535 that as well. */
1536
1537#define STANDARD_MUL_PRECEDENCE (7)
1538#define MRI_MUL_PRECEDENCE (5)
1539
1540void
1541expr_set_precedence ()
1542{
1543 if (flag_m68k_mri)
1544 {
1545 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1546 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1547 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1548 }
1549 else
1550 {
1551 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1552 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1553 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1554 }
1555}
1556
1557/* Initialize the expression parser. */
1558
1559void
1560expr_begin ()
1561{
1562 expr_set_precedence ();
1563
1564 /* Verify that X_op field is wide enough. */
1565 {
1566 expressionS e;
1567 e.X_op = O_max;
1568 assert (e.X_op == O_max);
1569 }
1570}
1571\f
1572/* Return the encoding for the operator at INPUT_LINE_POINTER.
1573 Advance INPUT_LINE_POINTER to the last character in the operator
1574 (i.e., don't change it for a single character operator). */
1575
1576static inline operatorT
1577operator ()
1578{
1579 int c;
1580 operatorT ret;
1581
1582 c = *input_line_pointer & 0xff;
1583
b75c0c92
AM
1584 if (is_end_of_line[c])
1585 return O_illegal;
1586
252b5132
RH
1587 switch (c)
1588 {
1589 default:
1590 return op_encoding[c];
1591
1592 case '<':
1593 switch (input_line_pointer[1])
1594 {
1595 default:
1596 return op_encoding[c];
1597 case '<':
1598 ret = O_left_shift;
1599 break;
1600 case '>':
1601 ret = O_ne;
1602 break;
1603 case '=':
1604 ret = O_le;
1605 break;
1606 }
1607 ++input_line_pointer;
1608 return ret;
1609
1610 case '=':
1611 if (input_line_pointer[1] != '=')
1612 return op_encoding[c];
1613
1614 ++input_line_pointer;
1615 return O_eq;
1616
1617 case '>':
1618 switch (input_line_pointer[1])
1619 {
1620 default:
1621 return op_encoding[c];
1622 case '>':
1623 ret = O_right_shift;
1624 break;
1625 case '=':
1626 ret = O_ge;
1627 break;
1628 }
1629 ++input_line_pointer;
1630 return ret;
1631
1632 case '!':
1633 /* We accept !! as equivalent to ^ for MRI compatibility. */
1634 if (input_line_pointer[1] != '!')
1635 {
1636 if (flag_m68k_mri)
1637 return O_bit_inclusive_or;
1638 return op_encoding[c];
1639 }
1640 ++input_line_pointer;
1641 return O_bit_exclusive_or;
1642
1643 case '|':
1644 if (input_line_pointer[1] != '|')
1645 return op_encoding[c];
1646
1647 ++input_line_pointer;
1648 return O_logical_or;
1649
1650 case '&':
1651 if (input_line_pointer[1] != '&')
1652 return op_encoding[c];
1653
1654 ++input_line_pointer;
1655 return O_logical_and;
1656 }
1657
1658 /*NOTREACHED*/
1659}
1660
1661/* Parse an expression. */
1662
1663segT
0561a208
ILT
1664expr (rankarg, resultP)
1665 int rankarg; /* Larger # is higher rank. */
252b5132
RH
1666 expressionS *resultP; /* Deliver result here. */
1667{
0561a208 1668 operator_rankT rank = (operator_rankT) rankarg;
252b5132
RH
1669 segT retval;
1670 expressionS right;
1671 operatorT op_left;
1672 operatorT op_right;
1673
1674 know (rank >= 0);
1675
1676 retval = operand (resultP);
1677
1678 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
1679
1680 op_left = operator ();
1681 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1682 {
1683 segT rightseg;
1684
1685 input_line_pointer++; /*->after 1st character of operator. */
1686
1687 rightseg = expr (op_rank[(int) op_left], &right);
1688 if (right.X_op == O_absent)
1689 {
1690 as_warn (_("missing operand; zero assumed"));
1691 right.X_op = O_constant;
1692 right.X_add_number = 0;
1693 right.X_add_symbol = NULL;
1694 right.X_op_symbol = NULL;
1695 }
1696
1697 know (*input_line_pointer != ' ');
1698
b585bc2c
RH
1699 if (op_left == O_index)
1700 {
1701 if (*input_line_pointer != ']')
1702 as_bad ("missing right bracket");
1703 else
1704 {
1705 ++input_line_pointer;
1706 SKIP_WHITESPACE ();
1707 }
1708 }
1709
252b5132
RH
1710 if (retval == undefined_section)
1711 {
1712 if (SEG_NORMAL (rightseg))
1713 retval = rightseg;
1714 }
1715 else if (! SEG_NORMAL (retval))
1716 retval = rightseg;
1717 else if (SEG_NORMAL (rightseg)
1718 && retval != rightseg
1719#ifdef DIFF_EXPR_OK
1720 && op_left != O_subtract
1721#endif
1722 )
1723 as_bad (_("operation combines symbols in different segments"));
1724
1725 op_right = operator ();
1726
1727 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1728 know ((int) op_left >= (int) O_multiply
1729 && (int) op_left <= (int) O_logical_or);
1730
1731 /* input_line_pointer->after right-hand quantity. */
1732 /* left-hand quantity in resultP */
1733 /* right-hand quantity in right. */
1734 /* operator in op_left. */
1735
1736 if (resultP->X_op == O_big)
1737 {
1738 if (resultP->X_add_number > 0)
1739 as_warn (_("left operand is a bignum; integer 0 assumed"));
1740 else
1741 as_warn (_("left operand is a float; integer 0 assumed"));
1742 resultP->X_op = O_constant;
1743 resultP->X_add_number = 0;
1744 resultP->X_add_symbol = NULL;
1745 resultP->X_op_symbol = NULL;
1746 }
1747 if (right.X_op == O_big)
1748 {
1749 if (right.X_add_number > 0)
1750 as_warn (_("right operand is a bignum; integer 0 assumed"));
1751 else
1752 as_warn (_("right operand is a float; integer 0 assumed"));
1753 right.X_op = O_constant;
1754 right.X_add_number = 0;
1755 right.X_add_symbol = NULL;
1756 right.X_op_symbol = NULL;
1757 }
1758
1759 /* Optimize common cases. */
800eeca4
JW
1760#ifdef md_optimize_expr
1761 if (md_optimize_expr (resultP, op_left, &right))
1762 {
1763 /* skip */;
1764 }
1765 else
1766#endif
252b5132
RH
1767 if (op_left == O_add && right.X_op == O_constant)
1768 {
1769 /* X + constant. */
1770 resultP->X_add_number += right.X_add_number;
1771 }
1772 /* This case comes up in PIC code. */
1773 else if (op_left == O_subtract
1774 && right.X_op == O_symbol
1775 && resultP->X_op == O_symbol
49309057
ILT
1776 && (symbol_get_frag (right.X_add_symbol)
1777 == symbol_get_frag (resultP->X_add_symbol))
252b5132
RH
1778 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1779
1780 {
1781 resultP->X_add_number -= right.X_add_number;
1782 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1783 - S_GET_VALUE (right.X_add_symbol));
1784 resultP->X_op = O_constant;
1785 resultP->X_add_symbol = 0;
1786 }
1787 else if (op_left == O_subtract && right.X_op == O_constant)
1788 {
1789 /* X - constant. */
1790 resultP->X_add_number -= right.X_add_number;
1791 }
1792 else if (op_left == O_add && resultP->X_op == O_constant)
1793 {
1794 /* Constant + X. */
1795 resultP->X_op = right.X_op;
1796 resultP->X_add_symbol = right.X_add_symbol;
1797 resultP->X_op_symbol = right.X_op_symbol;
1798 resultP->X_add_number += right.X_add_number;
1799 retval = rightseg;
1800 }
1801 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1802 {
1803 /* Constant OP constant. */
1804 offsetT v = right.X_add_number;
1805 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1806 {
1807 as_warn (_("division by zero"));
1808 v = 1;
1809 }
1810 switch (op_left)
1811 {
1812 default: abort ();
1813 case O_multiply: resultP->X_add_number *= v; break;
1814 case O_divide: resultP->X_add_number /= v; break;
1815 case O_modulus: resultP->X_add_number %= v; break;
1816 case O_left_shift: resultP->X_add_number <<= v; break;
1817 case O_right_shift:
1818 /* We always use unsigned shifts, to avoid relying on
1819 characteristics of the compiler used to compile gas. */
1820 resultP->X_add_number =
1821 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1822 break;
1823 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1824 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1825 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1826 case O_bit_and: resultP->X_add_number &= v; break;
1827 case O_add: resultP->X_add_number += v; break;
1828 case O_subtract: resultP->X_add_number -= v; break;
1829 case O_eq:
1830 resultP->X_add_number =
1831 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1832 break;
1833 case O_ne:
1834 resultP->X_add_number =
1835 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1836 break;
1837 case O_lt:
1838 resultP->X_add_number =
1839 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1840 break;
1841 case O_le:
1842 resultP->X_add_number =
1843 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1844 break;
1845 case O_ge:
1846 resultP->X_add_number =
1847 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1848 break;
1849 case O_gt:
1850 resultP->X_add_number =
1851 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1852 break;
1853 case O_logical_and:
1854 resultP->X_add_number = resultP->X_add_number && v;
1855 break;
1856 case O_logical_or:
1857 resultP->X_add_number = resultP->X_add_number || v;
1858 break;
1859 }
1860 }
1861 else if (resultP->X_op == O_symbol
1862 && right.X_op == O_symbol
1863 && (op_left == O_add
1864 || op_left == O_subtract
1865 || (resultP->X_add_number == 0
1866 && right.X_add_number == 0)))
1867 {
1868 /* Symbol OP symbol. */
1869 resultP->X_op = op_left;
1870 resultP->X_op_symbol = right.X_add_symbol;
1871 if (op_left == O_add)
1872 resultP->X_add_number += right.X_add_number;
1873 else if (op_left == O_subtract)
1874 resultP->X_add_number -= right.X_add_number;
1875 }
1876 else
1877 {
1878 /* The general case. */
1879 resultP->X_add_symbol = make_expr_symbol (resultP);
1880 resultP->X_op_symbol = make_expr_symbol (&right);
1881 resultP->X_op = op_left;
1882 resultP->X_add_number = 0;
1883 resultP->X_unsigned = 1;
1884 }
1885
1886 op_left = op_right;
1887 } /* While next operator is >= this rank. */
1888
1889 /* The PA port needs this information. */
1890 if (resultP->X_add_symbol)
49309057 1891 symbol_mark_used (resultP->X_add_symbol);
252b5132
RH
1892
1893 return resultP->X_op == O_constant ? absolute_section : retval;
1894}
1895\f
1896/*
1897 * get_symbol_end()
1898 *
1899 * This lives here because it belongs equally in expr.c & read.c.
1900 * Expr.c is just a branch office read.c anyway, and putting it
1901 * here lessens the crowd at read.c.
1902 *
1903 * Assume input_line_pointer is at start of symbol name.
1904 * Advance input_line_pointer past symbol name.
1905 * Turn that character into a '\0', returning its former value.
1906 * This allows a string compare (RMS wants symbol names to be strings)
1907 * of the symbol name.
1908 * There will always be a char following symbol name, because all good
1909 * lines end in end-of-line.
1910 */
1911char
1912get_symbol_end ()
1913{
1914 char c;
1915
1916 /* We accept \001 in a name in case this is being called with a
1917 constructed string. */
1918 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
58b5739a
RH
1919 {
1920 while (is_part_of_name (c = *input_line_pointer++)
1921 || c == '\001')
1922 ;
1923 if (is_name_ender (c))
1924 c = *input_line_pointer++;
1925 }
252b5132
RH
1926 *--input_line_pointer = 0;
1927 return (c);
1928}
1929
1930
1931unsigned int
1932get_single_number ()
1933{
1934 expressionS exp;
1935 operand (&exp);
1936 return exp.X_add_number;
1937
1938}
1939
1940/* end of expr.c */