gas/
[binutils-gdb.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
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 #define min(a, b) ((a) < (b) ? (a) : (b))
29
30 #include "as.h"
31 #include "safe-ctype.h"
32 #include "obstack.h"
33
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #endif
37 #ifndef CHAR_BIT
38 #define CHAR_BIT 8
39 #endif
40
41 static void floating_constant (expressionS * expressionP);
42 static valueT generic_bignum_to_int32 (void);
43 #ifdef BFD64
44 static valueT generic_bignum_to_int64 (void);
45 #endif
46 static void integer_constant (int radix, expressionS * expressionP);
47 static void mri_char_constant (expressionS *);
48 static void current_location (expressionS *);
49 static void clean_up_expression (expressionS * expressionP);
50 static segT operand (expressionS *, enum expr_mode);
51 static operatorT operator (int *);
52
53 extern const char EXP_CHARS[], FLT_CHARS[];
54
55 /* We keep a mapping of expression symbols to file positions, so that
56 we can provide better error messages. */
57
58 struct expr_symbol_line {
59 struct expr_symbol_line *next;
60 symbolS *sym;
61 char *file;
62 unsigned int line;
63 };
64
65 static struct expr_symbol_line *expr_symbol_lines;
66 \f
67 /* Build a dummy symbol to hold a complex expression. This is how we
68 build expressions up out of other expressions. The symbol is put
69 into the fake section expr_section. */
70
71 symbolS *
72 make_expr_symbol (expressionS *expressionP)
73 {
74 expressionS zero;
75 symbolS *symbolP;
76 struct expr_symbol_line *n;
77
78 if (expressionP->X_op == O_symbol
79 && expressionP->X_add_number == 0)
80 return expressionP->X_add_symbol;
81
82 if (expressionP->X_op == O_big)
83 {
84 /* This won't work, because the actual value is stored in
85 generic_floating_point_number or generic_bignum, and we are
86 going to lose it if we haven't already. */
87 if (expressionP->X_add_number > 0)
88 as_bad (_("bignum invalid"));
89 else
90 as_bad (_("floating point number invalid"));
91 zero.X_op = O_constant;
92 zero.X_add_number = 0;
93 zero.X_unsigned = 0;
94 clean_up_expression (&zero);
95 expressionP = &zero;
96 }
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_LABEL_NAME,
103 (expressionP->X_op == O_constant
104 ? absolute_section
105 : expressionP->X_op == O_register
106 ? reg_section
107 : expr_section),
108 0, &zero_address_frag);
109 symbol_set_value_expression (symbolP, expressionP);
110
111 if (expressionP->X_op == O_constant)
112 resolve_symbol_value (symbolP);
113
114 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
115 n->sym = symbolP;
116 as_where (&n->file, &n->line);
117 n->next = expr_symbol_lines;
118 expr_symbol_lines = n;
119
120 return symbolP;
121 }
122
123 /* Return the file and line number for an expr symbol. Return
124 non-zero if something was found, 0 if no information is known for
125 the symbol. */
126
127 int
128 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
129 {
130 register struct expr_symbol_line *l;
131
132 for (l = expr_symbol_lines; l != NULL; l = l->next)
133 {
134 if (l->sym == sym)
135 {
136 *pfile = l->file;
137 *pline = l->line;
138 return 1;
139 }
140 }
141
142 return 0;
143 }
144 \f
145 /* Utilities for building expressions.
146 Since complex expressions are recorded as symbols for use in other
147 expressions these return a symbolS * and not an expressionS *.
148 These explicitly do not take an "add_number" argument. */
149 /* ??? For completeness' sake one might want expr_build_symbol.
150 It would just return its argument. */
151
152 /* Build an expression for an unsigned constant.
153 The corresponding one for signed constants is missing because
154 there's currently no need for it. One could add an unsigned_p flag
155 but that seems more clumsy. */
156
157 symbolS *
158 expr_build_uconstant (offsetT value)
159 {
160 expressionS e;
161
162 e.X_op = O_constant;
163 e.X_add_number = value;
164 e.X_unsigned = 1;
165 return make_expr_symbol (&e);
166 }
167
168 /* Build an expression for the current location ('.'). */
169
170 symbolS *
171 expr_build_dot (void)
172 {
173 expressionS e;
174
175 current_location (&e);
176 return make_expr_symbol (&e);
177 }
178 \f
179 /* Build any floating-point literal here.
180 Also build any bignum literal here. */
181
182 /* Seems atof_machine can backscan through generic_bignum and hit whatever
183 happens to be loaded before it in memory. And its way too complicated
184 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
185 and never write into the early words, thus they'll always be zero.
186 I hate Dean's floating-point code. Bleh. */
187 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
188
189 FLONUM_TYPE generic_floating_point_number = {
190 &generic_bignum[6], /* low. (JF: Was 0) */
191 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
192 0, /* leader. */
193 0, /* exponent. */
194 0 /* sign. */
195 };
196
197 \f
198 static void
199 floating_constant (expressionS *expressionP)
200 {
201 /* input_line_pointer -> floating-point constant. */
202 int error_code;
203
204 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
205 &generic_floating_point_number);
206
207 if (error_code)
208 {
209 if (error_code == ERROR_EXPONENT_OVERFLOW)
210 {
211 as_bad (_("bad floating-point constant: exponent overflow"));
212 }
213 else
214 {
215 as_bad (_("bad floating-point constant: unknown error code=%d"),
216 error_code);
217 }
218 }
219 expressionP->X_op = O_big;
220 /* input_line_pointer -> just after constant, which may point to
221 whitespace. */
222 expressionP->X_add_number = -1;
223 }
224
225 static valueT
226 generic_bignum_to_int32 (void)
227 {
228 valueT number =
229 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
230 | (generic_bignum[0] & LITTLENUM_MASK);
231 number &= 0xffffffff;
232 return number;
233 }
234
235 #ifdef BFD64
236 static valueT
237 generic_bignum_to_int64 (void)
238 {
239 valueT number =
240 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
241 << LITTLENUM_NUMBER_OF_BITS)
242 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
243 << LITTLENUM_NUMBER_OF_BITS)
244 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
245 << LITTLENUM_NUMBER_OF_BITS)
246 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
247 return number;
248 }
249 #endif
250
251 static void
252 integer_constant (int radix, expressionS *expressionP)
253 {
254 char *start; /* Start of number. */
255 char *suffix = NULL;
256 char c;
257 valueT number; /* Offset or (absolute) value. */
258 short int digit; /* Value of next digit in current radix. */
259 short int maxdig = 0; /* Highest permitted digit value. */
260 int too_many_digits = 0; /* If we see >= this number of. */
261 char *name; /* Points to name of symbol. */
262 symbolS *symbolP; /* Points to symbol. */
263
264 int small; /* True if fits in 32 bits. */
265
266 /* May be bignum, or may fit in 32 bits. */
267 /* Most numbers fit into 32 bits, and we want this case to be fast.
268 so we pretend it will fit into 32 bits. If, after making up a 32
269 bit number, we realise that we have scanned more digits than
270 comfortably fit into 32 bits, we re-scan the digits coding them
271 into a bignum. For decimal and octal numbers we are
272 conservative: Some numbers may be assumed bignums when in fact
273 they do fit into 32 bits. Numbers of any radix can have excess
274 leading zeros: We strive to recognise this and cast them back
275 into 32 bits. We must check that the bignum really is more than
276 32 bits, and change it back to a 32-bit number if it fits. The
277 number we are looking for is expected to be positive, but if it
278 fits into 32 bits as an unsigned number, we let it be a 32-bit
279 number. The cavalier approach is for speed in ordinary cases. */
280 /* This has been extended for 64 bits. We blindly assume that if
281 you're compiling in 64-bit mode, the target is a 64-bit machine.
282 This should be cleaned up. */
283
284 #ifdef BFD64
285 #define valuesize 64
286 #else /* includes non-bfd case, mostly */
287 #define valuesize 32
288 #endif
289
290 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
291 {
292 int flt = 0;
293
294 /* In MRI mode, the number may have a suffix indicating the
295 radix. For that matter, it might actually be a floating
296 point constant. */
297 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
298 {
299 if (*suffix == 'e' || *suffix == 'E')
300 flt = 1;
301 }
302
303 if (suffix == input_line_pointer)
304 {
305 radix = 10;
306 suffix = NULL;
307 }
308 else
309 {
310 c = *--suffix;
311 c = TOUPPER (c);
312 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
313 we distinguish between 'B' and 'b'. This is the case for
314 Z80. */
315 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
316 radix = 2;
317 else if (c == 'D')
318 radix = 10;
319 else if (c == 'O' || c == 'Q')
320 radix = 8;
321 else if (c == 'H')
322 radix = 16;
323 else if (suffix[1] == '.' || c == 'E' || flt)
324 {
325 floating_constant (expressionP);
326 return;
327 }
328 else
329 {
330 radix = 10;
331 suffix = NULL;
332 }
333 }
334 }
335
336 switch (radix)
337 {
338 case 2:
339 maxdig = 2;
340 too_many_digits = valuesize + 1;
341 break;
342 case 8:
343 maxdig = radix = 8;
344 too_many_digits = (valuesize + 2) / 3 + 1;
345 break;
346 case 16:
347 maxdig = radix = 16;
348 too_many_digits = (valuesize + 3) / 4 + 1;
349 break;
350 case 10:
351 maxdig = radix = 10;
352 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
353 }
354 #undef valuesize
355 start = input_line_pointer;
356 c = *input_line_pointer++;
357 for (number = 0;
358 (digit = hex_value (c)) < maxdig;
359 c = *input_line_pointer++)
360 {
361 number = number * radix + digit;
362 }
363 /* c contains character after number. */
364 /* input_line_pointer->char after c. */
365 small = (input_line_pointer - start - 1) < too_many_digits;
366
367 if (radix == 16 && c == '_')
368 {
369 /* This is literal of the form 0x333_0_12345678_1.
370 This example is equivalent to 0x00000333000000001234567800000001. */
371
372 int num_little_digits = 0;
373 int i;
374 input_line_pointer = start; /* -> 1st digit. */
375
376 know (LITTLENUM_NUMBER_OF_BITS == 16);
377
378 for (c = '_'; c == '_'; num_little_digits += 2)
379 {
380
381 /* Convert one 64-bit word. */
382 int ndigit = 0;
383 number = 0;
384 for (c = *input_line_pointer++;
385 (digit = hex_value (c)) < maxdig;
386 c = *(input_line_pointer++))
387 {
388 number = number * radix + digit;
389 ndigit++;
390 }
391
392 /* Check for 8 digit per word max. */
393 if (ndigit > 8)
394 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
395
396 /* Add this chunk to the bignum.
397 Shift things down 2 little digits. */
398 know (LITTLENUM_NUMBER_OF_BITS == 16);
399 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
400 i >= 2;
401 i--)
402 generic_bignum[i] = generic_bignum[i - 2];
403
404 /* Add the new digits as the least significant new ones. */
405 generic_bignum[0] = number & 0xffffffff;
406 generic_bignum[1] = number >> 16;
407 }
408
409 /* Again, c is char after number, input_line_pointer->after c. */
410
411 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
412 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
413
414 assert (num_little_digits >= 4);
415
416 if (num_little_digits != 8)
417 as_bad (_("a bignum with underscores must have exactly 4 words"));
418
419 /* We might have some leading zeros. These can be trimmed to give
420 us a change to fit this constant into a small number. */
421 while (generic_bignum[num_little_digits - 1] == 0
422 && num_little_digits > 1)
423 num_little_digits--;
424
425 if (num_little_digits <= 2)
426 {
427 /* will fit into 32 bits. */
428 number = generic_bignum_to_int32 ();
429 small = 1;
430 }
431 #ifdef BFD64
432 else if (num_little_digits <= 4)
433 {
434 /* Will fit into 64 bits. */
435 number = generic_bignum_to_int64 ();
436 small = 1;
437 }
438 #endif
439 else
440 {
441 small = 0;
442
443 /* Number of littlenums in the bignum. */
444 number = num_little_digits;
445 }
446 }
447 else if (!small)
448 {
449 /* We saw a lot of digits. manufacture a bignum the hard way. */
450 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
451 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
452 long carry;
453
454 leader = generic_bignum;
455 generic_bignum[0] = 0;
456 generic_bignum[1] = 0;
457 generic_bignum[2] = 0;
458 generic_bignum[3] = 0;
459 input_line_pointer = start; /* -> 1st digit. */
460 c = *input_line_pointer++;
461 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
462 {
463 for (pointer = generic_bignum; pointer <= leader; pointer++)
464 {
465 long work;
466
467 work = carry + radix * *pointer;
468 *pointer = work & LITTLENUM_MASK;
469 carry = work >> LITTLENUM_NUMBER_OF_BITS;
470 }
471 if (carry)
472 {
473 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
474 {
475 /* Room to grow a longer bignum. */
476 *++leader = carry;
477 }
478 }
479 }
480 /* Again, c is char after number. */
481 /* input_line_pointer -> after c. */
482 know (LITTLENUM_NUMBER_OF_BITS == 16);
483 if (leader < generic_bignum + 2)
484 {
485 /* Will fit into 32 bits. */
486 number = generic_bignum_to_int32 ();
487 small = 1;
488 }
489 #ifdef BFD64
490 else if (leader < generic_bignum + 4)
491 {
492 /* Will fit into 64 bits. */
493 number = generic_bignum_to_int64 ();
494 small = 1;
495 }
496 #endif
497 else
498 {
499 /* Number of littlenums in the bignum. */
500 number = leader - generic_bignum + 1;
501 }
502 }
503
504 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
505 && suffix != NULL
506 && input_line_pointer - 1 == suffix)
507 c = *input_line_pointer++;
508
509 if (small)
510 {
511 /* Here with number, in correct radix. c is the next char.
512 Note that unlike un*x, we allow "011f" "0x9f" to both mean
513 the same as the (conventional) "9f".
514 This is simply easier than checking for strict canonical
515 form. Syntax sux! */
516
517 if (LOCAL_LABELS_FB && c == 'b')
518 {
519 /* Backward ref to local label.
520 Because it is backward, expect it to be defined. */
521 /* Construct a local label. */
522 name = fb_label_name ((int) number, 0);
523
524 /* Seen before, or symbol is defined: OK. */
525 symbolP = symbol_find (name);
526 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
527 {
528 /* Local labels are never absolute. Don't waste time
529 checking absoluteness. */
530 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
531
532 expressionP->X_op = O_symbol;
533 expressionP->X_add_symbol = symbolP;
534 }
535 else
536 {
537 /* Either not seen or not defined. */
538 /* @@ Should print out the original string instead of
539 the parsed number. */
540 as_bad (_("backward ref to unknown label \"%d:\""),
541 (int) number);
542 expressionP->X_op = O_constant;
543 }
544
545 expressionP->X_add_number = 0;
546 } /* case 'b' */
547 else if (LOCAL_LABELS_FB && c == 'f')
548 {
549 /* Forward reference. Expect symbol to be undefined or
550 unknown. undefined: seen it before. unknown: never seen
551 it before.
552
553 Construct a local label name, then an undefined symbol.
554 Don't create a xseg frag for it: caller may do that.
555 Just return it as never seen before. */
556 name = fb_label_name ((int) number, 1);
557 symbolP = symbol_find_or_make (name);
558 /* We have no need to check symbol properties. */
559 #ifndef many_segments
560 /* Since "know" puts its arg into a "string", we
561 can't have newlines in the argument. */
562 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
563 #endif
564 expressionP->X_op = O_symbol;
565 expressionP->X_add_symbol = symbolP;
566 expressionP->X_add_number = 0;
567 } /* case 'f' */
568 else if (LOCAL_LABELS_DOLLAR && c == '$')
569 {
570 /* If the dollar label is *currently* defined, then this is just
571 another reference to it. If it is not *currently* defined,
572 then this is a fresh instantiation of that number, so create
573 it. */
574
575 if (dollar_label_defined ((long) number))
576 {
577 name = dollar_label_name ((long) number, 0);
578 symbolP = symbol_find (name);
579 know (symbolP != NULL);
580 }
581 else
582 {
583 name = dollar_label_name ((long) number, 1);
584 symbolP = symbol_find_or_make (name);
585 }
586
587 expressionP->X_op = O_symbol;
588 expressionP->X_add_symbol = symbolP;
589 expressionP->X_add_number = 0;
590 } /* case '$' */
591 else
592 {
593 expressionP->X_op = O_constant;
594 expressionP->X_add_number = number;
595 input_line_pointer--; /* Restore following character. */
596 } /* Really just a number. */
597 }
598 else
599 {
600 /* Not a small number. */
601 expressionP->X_op = O_big;
602 expressionP->X_add_number = number; /* Number of littlenums. */
603 input_line_pointer--; /* -> char following number. */
604 }
605 }
606
607 /* Parse an MRI multi character constant. */
608
609 static void
610 mri_char_constant (expressionS *expressionP)
611 {
612 int i;
613
614 if (*input_line_pointer == '\''
615 && input_line_pointer[1] != '\'')
616 {
617 expressionP->X_op = O_constant;
618 expressionP->X_add_number = 0;
619 return;
620 }
621
622 /* In order to get the correct byte ordering, we must build the
623 number in reverse. */
624 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
625 {
626 int j;
627
628 generic_bignum[i] = 0;
629 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
630 {
631 if (*input_line_pointer == '\'')
632 {
633 if (input_line_pointer[1] != '\'')
634 break;
635 ++input_line_pointer;
636 }
637 generic_bignum[i] <<= 8;
638 generic_bignum[i] += *input_line_pointer;
639 ++input_line_pointer;
640 }
641
642 if (i < SIZE_OF_LARGE_NUMBER - 1)
643 {
644 /* If there is more than one littlenum, left justify the
645 last one to make it match the earlier ones. If there is
646 only one, we can just use the value directly. */
647 for (; j < CHARS_PER_LITTLENUM; j++)
648 generic_bignum[i] <<= 8;
649 }
650
651 if (*input_line_pointer == '\''
652 && input_line_pointer[1] != '\'')
653 break;
654 }
655
656 if (i < 0)
657 {
658 as_bad (_("character constant too large"));
659 i = 0;
660 }
661
662 if (i > 0)
663 {
664 int c;
665 int j;
666
667 c = SIZE_OF_LARGE_NUMBER - i;
668 for (j = 0; j < c; j++)
669 generic_bignum[j] = generic_bignum[i + j];
670 i = c;
671 }
672
673 know (LITTLENUM_NUMBER_OF_BITS == 16);
674 if (i > 2)
675 {
676 expressionP->X_op = O_big;
677 expressionP->X_add_number = i;
678 }
679 else
680 {
681 expressionP->X_op = O_constant;
682 if (i < 2)
683 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
684 else
685 expressionP->X_add_number =
686 (((generic_bignum[1] & LITTLENUM_MASK)
687 << LITTLENUM_NUMBER_OF_BITS)
688 | (generic_bignum[0] & LITTLENUM_MASK));
689 }
690
691 /* Skip the final closing quote. */
692 ++input_line_pointer;
693 }
694
695 /* Return an expression representing the current location. This
696 handles the magic symbol `.'. */
697
698 static void
699 current_location (expressionS *expressionp)
700 {
701 if (now_seg == absolute_section)
702 {
703 expressionp->X_op = O_constant;
704 expressionp->X_add_number = abs_section_offset;
705 }
706 else
707 {
708 expressionp->X_op = O_symbol;
709 expressionp->X_add_symbol = symbol_temp_new_now ();
710 expressionp->X_add_number = 0;
711 }
712 }
713
714 /* In: Input_line_pointer points to 1st char of operand, which may
715 be a space.
716
717 Out: An expressionS.
718 The operand may have been empty: in this case X_op == O_absent.
719 Input_line_pointer->(next non-blank) char after operand. */
720
721 static segT
722 operand (expressionS *expressionP, enum expr_mode mode)
723 {
724 char c;
725 symbolS *symbolP; /* Points to symbol. */
726 char *name; /* Points to name of symbol. */
727 segT segment;
728
729 /* All integers are regarded as unsigned unless they are negated.
730 This is because the only thing which cares whether a number is
731 unsigned is the code in emit_expr which extends constants into
732 bignums. It should only sign extend negative numbers, so that
733 something like ``.quad 0x80000000'' is not sign extended even
734 though it appears negative if valueT is 32 bits. */
735 expressionP->X_unsigned = 1;
736
737 /* Digits, assume it is a bignum. */
738
739 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
740 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
741
742 if (is_end_of_line[(unsigned char) c])
743 goto eol;
744
745 switch (c)
746 {
747 case '1':
748 case '2':
749 case '3':
750 case '4':
751 case '5':
752 case '6':
753 case '7':
754 case '8':
755 case '9':
756 input_line_pointer--;
757
758 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
759 ? 0 : 10,
760 expressionP);
761 break;
762
763 #ifdef LITERAL_PREFIXDOLLAR_HEX
764 case '$':
765 /* $L is the start of a local label, not a hex constant. */
766 if (* input_line_pointer == 'L')
767 goto isname;
768 integer_constant (16, expressionP);
769 break;
770 #endif
771
772 #ifdef LITERAL_PREFIXPERCENT_BIN
773 case '%':
774 integer_constant (2, expressionP);
775 break;
776 #endif
777
778 case '0':
779 /* Non-decimal radix. */
780
781 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
782 {
783 char *s;
784
785 /* Check for a hex or float constant. */
786 for (s = input_line_pointer; hex_p (*s); s++)
787 ;
788 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
789 {
790 --input_line_pointer;
791 integer_constant (0, expressionP);
792 break;
793 }
794 }
795 c = *input_line_pointer;
796 switch (c)
797 {
798 case 'o':
799 case 'O':
800 case 'q':
801 case 'Q':
802 case '8':
803 case '9':
804 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
805 {
806 integer_constant (0, expressionP);
807 break;
808 }
809 /* Fall through. */
810 default:
811 default_case:
812 if (c && strchr (FLT_CHARS, c))
813 {
814 input_line_pointer++;
815 floating_constant (expressionP);
816 expressionP->X_add_number = - TOLOWER (c);
817 }
818 else
819 {
820 /* The string was only zero. */
821 expressionP->X_op = O_constant;
822 expressionP->X_add_number = 0;
823 }
824
825 break;
826
827 case 'x':
828 case 'X':
829 if (flag_m68k_mri)
830 goto default_case;
831 input_line_pointer++;
832 integer_constant (16, expressionP);
833 break;
834
835 case 'b':
836 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
837 {
838 /* This code used to check for '+' and '-' here, and, in
839 some conditions, fall through to call
840 integer_constant. However, that didn't make sense,
841 as integer_constant only accepts digits. */
842 /* Some of our code elsewhere does permit digits greater
843 than the expected base; for consistency, do the same
844 here. */
845 if (input_line_pointer[1] < '0'
846 || input_line_pointer[1] > '9')
847 {
848 /* Parse this as a back reference to label 0. */
849 input_line_pointer--;
850 integer_constant (10, expressionP);
851 break;
852 }
853 /* Otherwise, parse this as a binary number. */
854 }
855 /* Fall through. */
856 case 'B':
857 input_line_pointer++;
858 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
859 goto default_case;
860 integer_constant (2, expressionP);
861 break;
862
863 case '0':
864 case '1':
865 case '2':
866 case '3':
867 case '4':
868 case '5':
869 case '6':
870 case '7':
871 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
872 ? 0 : 8,
873 expressionP);
874 break;
875
876 case 'f':
877 if (LOCAL_LABELS_FB)
878 {
879 /* If it says "0f" and it could possibly be a floating point
880 number, make it one. Otherwise, make it a local label,
881 and try to deal with parsing the rest later. */
882 if (!input_line_pointer[1]
883 || (is_end_of_line[0xff & input_line_pointer[1]])
884 || strchr (FLT_CHARS, 'f') == NULL)
885 goto is_0f_label;
886 {
887 char *cp = input_line_pointer + 1;
888 int r = atof_generic (&cp, ".", EXP_CHARS,
889 &generic_floating_point_number);
890 switch (r)
891 {
892 case 0:
893 case ERROR_EXPONENT_OVERFLOW:
894 if (*cp == 'f' || *cp == 'b')
895 /* Looks like a difference expression. */
896 goto is_0f_label;
897 else if (cp == input_line_pointer + 1)
898 /* No characters has been accepted -- looks like
899 end of operand. */
900 goto is_0f_label;
901 else
902 goto is_0f_float;
903 default:
904 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
905 r);
906 }
907 }
908
909 /* Okay, now we've sorted it out. We resume at one of these
910 two labels, depending on what we've decided we're probably
911 looking at. */
912 is_0f_label:
913 input_line_pointer--;
914 integer_constant (10, expressionP);
915 break;
916
917 is_0f_float:
918 /* Fall through. */
919 ;
920 }
921
922 case 'd':
923 case 'D':
924 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
925 {
926 integer_constant (0, expressionP);
927 break;
928 }
929 /* Fall through. */
930 case 'F':
931 case 'r':
932 case 'e':
933 case 'E':
934 case 'g':
935 case 'G':
936 input_line_pointer++;
937 floating_constant (expressionP);
938 expressionP->X_add_number = - TOLOWER (c);
939 break;
940
941 case '$':
942 if (LOCAL_LABELS_DOLLAR)
943 {
944 integer_constant (10, expressionP);
945 break;
946 }
947 else
948 goto default_case;
949 }
950
951 break;
952
953 case '(':
954 #ifndef NEED_INDEX_OPERATOR
955 case '[':
956 #endif
957 /* Didn't begin with digit & not a name. */
958 if (mode != expr_defer)
959 segment = expression (expressionP);
960 else
961 segment = deferred_expression (expressionP);
962 /* expression () will pass trailing whitespace. */
963 if ((c == '(' && *input_line_pointer != ')')
964 || (c == '[' && *input_line_pointer != ']'))
965 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
966 else
967 input_line_pointer++;
968 SKIP_WHITESPACE ();
969 /* Here with input_line_pointer -> char after "(...)". */
970 return segment;
971
972 #ifdef TC_M68K
973 case 'E':
974 if (! flag_m68k_mri || *input_line_pointer != '\'')
975 goto de_fault;
976 as_bad (_("EBCDIC constants are not supported"));
977 /* Fall through. */
978 case 'A':
979 if (! flag_m68k_mri || *input_line_pointer != '\'')
980 goto de_fault;
981 ++input_line_pointer;
982 /* Fall through. */
983 #endif
984 case '\'':
985 if (! flag_m68k_mri)
986 {
987 /* Warning: to conform to other people's assemblers NO
988 ESCAPEMENT is permitted for a single quote. The next
989 character, parity errors and all, is taken as the value
990 of the operand. VERY KINKY. */
991 expressionP->X_op = O_constant;
992 expressionP->X_add_number = *input_line_pointer++;
993 break;
994 }
995
996 mri_char_constant (expressionP);
997 break;
998
999 #ifdef TC_M68K
1000 case '"':
1001 /* Double quote is the bitwise not operator in MRI mode. */
1002 if (! flag_m68k_mri)
1003 goto de_fault;
1004 /* Fall through. */
1005 #endif
1006 case '~':
1007 /* '~' is permitted to start a label on the Delta. */
1008 if (is_name_beginner (c))
1009 goto isname;
1010 case '!':
1011 case '-':
1012 case '+':
1013 {
1014 operand (expressionP, mode);
1015 if (expressionP->X_op == O_constant)
1016 {
1017 /* input_line_pointer -> char after operand. */
1018 if (c == '-')
1019 {
1020 expressionP->X_add_number = - expressionP->X_add_number;
1021 /* Notice: '-' may overflow: no warning is given.
1022 This is compatible with other people's
1023 assemblers. Sigh. */
1024 expressionP->X_unsigned = 0;
1025 }
1026 else if (c == '~' || c == '"')
1027 expressionP->X_add_number = ~ expressionP->X_add_number;
1028 else if (c == '!')
1029 expressionP->X_add_number = ! expressionP->X_add_number;
1030 }
1031 else if (expressionP->X_op == O_big
1032 && expressionP->X_add_number <= 0
1033 && c == '-'
1034 && (generic_floating_point_number.sign == '+'
1035 || generic_floating_point_number.sign == 'P'))
1036 {
1037 /* Negative flonum (eg, -1.000e0). */
1038 if (generic_floating_point_number.sign == '+')
1039 generic_floating_point_number.sign = '-';
1040 else
1041 generic_floating_point_number.sign = 'N';
1042 }
1043 else if (expressionP->X_op == O_big
1044 && expressionP->X_add_number > 0)
1045 {
1046 int i;
1047
1048 if (c == '~' || c == '-')
1049 {
1050 for (i = 0; i < expressionP->X_add_number; ++i)
1051 generic_bignum[i] = ~generic_bignum[i];
1052 if (c == '-')
1053 for (i = 0; i < expressionP->X_add_number; ++i)
1054 {
1055 generic_bignum[i] += 1;
1056 if (generic_bignum[i])
1057 break;
1058 }
1059 }
1060 else if (c == '!')
1061 {
1062 int nonzero = 0;
1063 for (i = 0; i < expressionP->X_add_number; ++i)
1064 {
1065 if (generic_bignum[i])
1066 nonzero = 1;
1067 generic_bignum[i] = 0;
1068 }
1069 generic_bignum[0] = nonzero;
1070 }
1071 }
1072 else if (expressionP->X_op != O_illegal
1073 && expressionP->X_op != O_absent)
1074 {
1075 if (c != '+')
1076 {
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;
1085 }
1086 }
1087 else
1088 as_warn (_("Unary operator %c ignored because bad operand follows"),
1089 c);
1090 }
1091 break;
1092
1093 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1094 case '$':
1095 /* '$' is the program counter when in MRI mode, or when
1096 DOLLAR_DOT is defined. */
1097 #ifndef DOLLAR_DOT
1098 if (! flag_m68k_mri)
1099 goto de_fault;
1100 #endif
1101 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1102 {
1103 /* In MRI mode and on Z80, '$' is also used as the prefix
1104 for a hexadecimal constant. */
1105 integer_constant (16, expressionP);
1106 break;
1107 }
1108
1109 if (is_part_of_name (*input_line_pointer))
1110 goto isname;
1111
1112 current_location (expressionP);
1113 break;
1114 #endif
1115
1116 case '.':
1117 if (!is_part_of_name (*input_line_pointer))
1118 {
1119 current_location (expressionP);
1120 break;
1121 }
1122 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1123 && ! is_part_of_name (input_line_pointer[8]))
1124 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1125 && ! is_part_of_name (input_line_pointer[7])))
1126 {
1127 int start;
1128
1129 start = (input_line_pointer[1] == 't'
1130 || input_line_pointer[1] == 'T');
1131 input_line_pointer += start ? 8 : 7;
1132 SKIP_WHITESPACE ();
1133 if (*input_line_pointer != '(')
1134 as_bad (_("syntax error in .startof. or .sizeof."));
1135 else
1136 {
1137 char *buf;
1138
1139 ++input_line_pointer;
1140 SKIP_WHITESPACE ();
1141 name = input_line_pointer;
1142 c = get_symbol_end ();
1143
1144 buf = (char *) xmalloc (strlen (name) + 10);
1145 if (start)
1146 sprintf (buf, ".startof.%s", name);
1147 else
1148 sprintf (buf, ".sizeof.%s", name);
1149 symbolP = symbol_make (buf);
1150 free (buf);
1151
1152 expressionP->X_op = O_symbol;
1153 expressionP->X_add_symbol = symbolP;
1154 expressionP->X_add_number = 0;
1155
1156 *input_line_pointer = c;
1157 SKIP_WHITESPACE ();
1158 if (*input_line_pointer != ')')
1159 as_bad (_("syntax error in .startof. or .sizeof."));
1160 else
1161 ++input_line_pointer;
1162 }
1163 break;
1164 }
1165 else
1166 {
1167 goto isname;
1168 }
1169
1170 case ',':
1171 eol:
1172 /* Can't imagine any other kind of operand. */
1173 expressionP->X_op = O_absent;
1174 input_line_pointer--;
1175 break;
1176
1177 #ifdef TC_M68K
1178 case '%':
1179 if (! flag_m68k_mri)
1180 goto de_fault;
1181 integer_constant (2, expressionP);
1182 break;
1183
1184 case '@':
1185 if (! flag_m68k_mri)
1186 goto de_fault;
1187 integer_constant (8, expressionP);
1188 break;
1189
1190 case ':':
1191 if (! flag_m68k_mri)
1192 goto de_fault;
1193
1194 /* In MRI mode, this is a floating point constant represented
1195 using hexadecimal digits. */
1196
1197 ++input_line_pointer;
1198 integer_constant (16, expressionP);
1199 break;
1200
1201 case '*':
1202 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1203 goto de_fault;
1204
1205 current_location (expressionP);
1206 break;
1207 #endif
1208
1209 default:
1210 #ifdef TC_M68K
1211 de_fault:
1212 #endif
1213 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1214 {
1215 /* Identifier begins here.
1216 This is kludged for speed, so code is repeated. */
1217 isname:
1218 name = --input_line_pointer;
1219 c = get_symbol_end ();
1220
1221 #ifdef md_parse_name
1222 /* This is a hook for the backend to parse certain names
1223 specially in certain contexts. If a name always has a
1224 specific value, it can often be handled by simply
1225 entering it in the symbol table. */
1226 if (md_parse_name (name, expressionP, mode, &c))
1227 {
1228 *input_line_pointer = c;
1229 break;
1230 }
1231 #endif
1232
1233 #ifdef TC_I960
1234 /* The MRI i960 assembler permits
1235 lda sizeof code,g13
1236 FIXME: This should use md_parse_name. */
1237 if (flag_mri
1238 && (strcasecmp (name, "sizeof") == 0
1239 || strcasecmp (name, "startof") == 0))
1240 {
1241 int start;
1242 char *buf;
1243
1244 start = (name[1] == 't'
1245 || name[1] == 'T');
1246
1247 *input_line_pointer = c;
1248 SKIP_WHITESPACE ();
1249
1250 name = input_line_pointer;
1251 c = get_symbol_end ();
1252
1253 buf = (char *) xmalloc (strlen (name) + 10);
1254 if (start)
1255 sprintf (buf, ".startof.%s", name);
1256 else
1257 sprintf (buf, ".sizeof.%s", name);
1258 symbolP = symbol_make (buf);
1259 free (buf);
1260
1261 expressionP->X_op = O_symbol;
1262 expressionP->X_add_symbol = symbolP;
1263 expressionP->X_add_number = 0;
1264
1265 *input_line_pointer = c;
1266 SKIP_WHITESPACE ();
1267
1268 break;
1269 }
1270 #endif
1271
1272 symbolP = symbol_find_or_make (name);
1273
1274 /* If we have an absolute symbol or a reg, then we know its
1275 value now. */
1276 segment = S_GET_SEGMENT (symbolP);
1277 if (mode != expr_defer && segment == absolute_section)
1278 {
1279 expressionP->X_op = O_constant;
1280 expressionP->X_add_number = S_GET_VALUE (symbolP);
1281 }
1282 else if (mode != expr_defer && segment == reg_section)
1283 {
1284 expressionP->X_op = O_register;
1285 expressionP->X_add_number = S_GET_VALUE (symbolP);
1286 }
1287 else
1288 {
1289 expressionP->X_op = O_symbol;
1290 expressionP->X_add_symbol = symbolP;
1291 expressionP->X_add_number = 0;
1292 }
1293 *input_line_pointer = c;
1294 }
1295 else
1296 {
1297 /* Let the target try to parse it. Success is indicated by changing
1298 the X_op field to something other than O_absent and pointing
1299 input_line_pointer past the expression. If it can't parse the
1300 expression, X_op and input_line_pointer should be unchanged. */
1301 expressionP->X_op = O_absent;
1302 --input_line_pointer;
1303 md_operand (expressionP);
1304 if (expressionP->X_op == O_absent)
1305 {
1306 ++input_line_pointer;
1307 as_bad (_("bad expression"));
1308 expressionP->X_op = O_constant;
1309 expressionP->X_add_number = 0;
1310 }
1311 }
1312 break;
1313 }
1314
1315 /* It is more 'efficient' to clean up the expressionS when they are
1316 created. Doing it here saves lines of code. */
1317 clean_up_expression (expressionP);
1318 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1319 know (*input_line_pointer != ' ');
1320
1321 /* The PA port needs this information. */
1322 if (expressionP->X_add_symbol)
1323 symbol_mark_used (expressionP->X_add_symbol);
1324
1325 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1326 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1327
1328 switch (expressionP->X_op)
1329 {
1330 default:
1331 return absolute_section;
1332 case O_symbol:
1333 return S_GET_SEGMENT (expressionP->X_add_symbol);
1334 case O_register:
1335 return reg_section;
1336 }
1337 }
1338 \f
1339 /* Internal. Simplify a struct expression for use by expr (). */
1340
1341 /* In: address of an expressionS.
1342 The X_op field of the expressionS may only take certain values.
1343 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1344
1345 Out: expressionS may have been modified:
1346 Unused fields zeroed to help expr (). */
1347
1348 static void
1349 clean_up_expression (expressionS *expressionP)
1350 {
1351 switch (expressionP->X_op)
1352 {
1353 case O_illegal:
1354 case O_absent:
1355 expressionP->X_add_number = 0;
1356 /* Fall through. */
1357 case O_big:
1358 case O_constant:
1359 case O_register:
1360 expressionP->X_add_symbol = NULL;
1361 /* Fall through. */
1362 case O_symbol:
1363 case O_uminus:
1364 case O_bit_not:
1365 expressionP->X_op_symbol = NULL;
1366 break;
1367 default:
1368 break;
1369 }
1370 }
1371 \f
1372 /* Expression parser. */
1373
1374 /* We allow an empty expression, and just assume (absolute,0) silently.
1375 Unary operators and parenthetical expressions are treated as operands.
1376 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1377
1378 We used to do an aho/ullman shift-reduce parser, but the logic got so
1379 warped that I flushed it and wrote a recursive-descent parser instead.
1380 Now things are stable, would anybody like to write a fast parser?
1381 Most expressions are either register (which does not even reach here)
1382 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1383 So I guess it doesn't really matter how inefficient more complex expressions
1384 are parsed.
1385
1386 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1387 Also, we have consumed any leading or trailing spaces (operand does that)
1388 and done all intervening operators.
1389
1390 This returns the segment of the result, which will be
1391 absolute_section or the segment of a symbol. */
1392
1393 #undef __
1394 #define __ O_illegal
1395 #ifndef O_SINGLE_EQ
1396 #define O_SINGLE_EQ O_illegal
1397 #endif
1398
1399 /* Maps ASCII -> operators. */
1400 static const operatorT op_encoding[256] = {
1401 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1402 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1403
1404 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1405 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1406 __, __, __, __, __, __, __, __,
1407 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1408 __, __, __, __, __, __, __, __,
1409 __, __, __, __, __, __, __, __,
1410 __, __, __, __, __, __, __, __,
1411 __, __, __,
1412 #ifdef NEED_INDEX_OPERATOR
1413 O_index,
1414 #else
1415 __,
1416 #endif
1417 __, __, O_bit_exclusive_or, __,
1418 __, __, __, __, __, __, __, __,
1419 __, __, __, __, __, __, __, __,
1420 __, __, __, __, __, __, __, __,
1421 __, __, __, __, O_bit_inclusive_or, __, __, __,
1422
1423 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1424 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1425 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1426 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1427 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1428 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1429 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1430 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1431 };
1432
1433 /* Rank Examples
1434 0 operand, (expression)
1435 1 ||
1436 2 &&
1437 3 == <> < <= >= >
1438 4 + -
1439 5 used for * / % in MRI mode
1440 6 & ^ ! |
1441 7 * / % << >>
1442 8 unary - unary ~
1443 */
1444 static operator_rankT op_rank[] = {
1445 0, /* O_illegal */
1446 0, /* O_absent */
1447 0, /* O_constant */
1448 0, /* O_symbol */
1449 0, /* O_symbol_rva */
1450 0, /* O_register */
1451 0, /* O_big */
1452 9, /* O_uminus */
1453 9, /* O_bit_not */
1454 9, /* O_logical_not */
1455 8, /* O_multiply */
1456 8, /* O_divide */
1457 8, /* O_modulus */
1458 8, /* O_left_shift */
1459 8, /* O_right_shift */
1460 7, /* O_bit_inclusive_or */
1461 7, /* O_bit_or_not */
1462 7, /* O_bit_exclusive_or */
1463 7, /* O_bit_and */
1464 5, /* O_add */
1465 5, /* O_subtract */
1466 4, /* O_eq */
1467 4, /* O_ne */
1468 4, /* O_lt */
1469 4, /* O_le */
1470 4, /* O_ge */
1471 4, /* O_gt */
1472 3, /* O_logical_and */
1473 2, /* O_logical_or */
1474 1, /* O_index */
1475 0, /* O_md1 */
1476 0, /* O_md2 */
1477 0, /* O_md3 */
1478 0, /* O_md4 */
1479 0, /* O_md5 */
1480 0, /* O_md6 */
1481 0, /* O_md7 */
1482 0, /* O_md8 */
1483 0, /* O_md9 */
1484 0, /* O_md10 */
1485 0, /* O_md11 */
1486 0, /* O_md12 */
1487 0, /* O_md13 */
1488 0, /* O_md14 */
1489 0, /* O_md15 */
1490 0, /* O_md16 */
1491 };
1492
1493 /* Unfortunately, in MRI mode for the m68k, multiplication and
1494 division have lower precedence than the bit wise operators. This
1495 function sets the operator precedences correctly for the current
1496 mode. Also, MRI uses a different bit_not operator, and this fixes
1497 that as well. */
1498
1499 #define STANDARD_MUL_PRECEDENCE 8
1500 #define MRI_MUL_PRECEDENCE 6
1501
1502 void
1503 expr_set_precedence (void)
1504 {
1505 if (flag_m68k_mri)
1506 {
1507 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1508 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1509 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1510 }
1511 else
1512 {
1513 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1514 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1515 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1516 }
1517 }
1518
1519 /* Initialize the expression parser. */
1520
1521 void
1522 expr_begin (void)
1523 {
1524 expr_set_precedence ();
1525
1526 /* Verify that X_op field is wide enough. */
1527 {
1528 expressionS e;
1529 e.X_op = O_max;
1530 assert (e.X_op == O_max);
1531 }
1532 }
1533 \f
1534 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1535 sets NUM_CHARS to the number of characters in the operator.
1536 Does not advance INPUT_LINE_POINTER. */
1537
1538 static inline operatorT
1539 operator (int *num_chars)
1540 {
1541 int c;
1542 operatorT ret;
1543
1544 c = *input_line_pointer & 0xff;
1545 *num_chars = 1;
1546
1547 if (is_end_of_line[c])
1548 return O_illegal;
1549
1550 switch (c)
1551 {
1552 default:
1553 return op_encoding[c];
1554
1555 case '+':
1556 case '-':
1557 return op_encoding[c];
1558
1559 case '<':
1560 switch (input_line_pointer[1])
1561 {
1562 default:
1563 return op_encoding[c];
1564 case '<':
1565 ret = O_left_shift;
1566 break;
1567 case '>':
1568 ret = O_ne;
1569 break;
1570 case '=':
1571 ret = O_le;
1572 break;
1573 }
1574 *num_chars = 2;
1575 return ret;
1576
1577 case '=':
1578 if (input_line_pointer[1] != '=')
1579 return op_encoding[c];
1580
1581 *num_chars = 2;
1582 return O_eq;
1583
1584 case '>':
1585 switch (input_line_pointer[1])
1586 {
1587 default:
1588 return op_encoding[c];
1589 case '>':
1590 ret = O_right_shift;
1591 break;
1592 case '=':
1593 ret = O_ge;
1594 break;
1595 }
1596 *num_chars = 2;
1597 return ret;
1598
1599 case '!':
1600 switch (input_line_pointer[1])
1601 {
1602 case '!':
1603 /* We accept !! as equivalent to ^ for MRI compatibility. */
1604 *num_chars = 2;
1605 return O_bit_exclusive_or;
1606 case '=':
1607 /* We accept != as equivalent to <>. */
1608 *num_chars = 2;
1609 return O_ne;
1610 default:
1611 if (flag_m68k_mri)
1612 return O_bit_inclusive_or;
1613 return op_encoding[c];
1614 }
1615
1616 case '|':
1617 if (input_line_pointer[1] != '|')
1618 return op_encoding[c];
1619
1620 *num_chars = 2;
1621 return O_logical_or;
1622
1623 case '&':
1624 if (input_line_pointer[1] != '&')
1625 return op_encoding[c];
1626
1627 *num_chars = 2;
1628 return O_logical_and;
1629 }
1630
1631 /* NOTREACHED */
1632 }
1633
1634 /* Parse an expression. */
1635
1636 segT
1637 expr (int rankarg, /* Larger # is higher rank. */
1638 expressionS *resultP, /* Deliver result here. */
1639 enum expr_mode mode /* Controls behavior. */)
1640 {
1641 operator_rankT rank = (operator_rankT) rankarg;
1642 segT retval;
1643 expressionS right;
1644 operatorT op_left;
1645 operatorT op_right;
1646 int op_chars;
1647
1648 know (rankarg >= 0);
1649
1650 /* Save the value of dot for the fixup code. */
1651 if (rank == 0)
1652 dot_value = frag_now_fix ();
1653
1654 retval = operand (resultP, mode);
1655
1656 /* operand () gobbles spaces. */
1657 know (*input_line_pointer != ' ');
1658
1659 op_left = operator (&op_chars);
1660 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1661 {
1662 segT rightseg;
1663 bfd_vma frag_off;
1664
1665 input_line_pointer += op_chars; /* -> after operator. */
1666
1667 rightseg = expr (op_rank[(int) op_left], &right, mode);
1668 if (right.X_op == O_absent)
1669 {
1670 as_warn (_("missing operand; zero assumed"));
1671 right.X_op = O_constant;
1672 right.X_add_number = 0;
1673 right.X_add_symbol = NULL;
1674 right.X_op_symbol = NULL;
1675 }
1676
1677 know (*input_line_pointer != ' ');
1678
1679 if (op_left == O_index)
1680 {
1681 if (*input_line_pointer != ']')
1682 as_bad ("missing right bracket");
1683 else
1684 {
1685 ++input_line_pointer;
1686 SKIP_WHITESPACE ();
1687 }
1688 }
1689
1690 op_right = operator (&op_chars);
1691
1692 know (op_right == O_illegal
1693 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1694 know ((int) op_left >= (int) O_multiply
1695 && (int) op_left <= (int) O_index);
1696
1697 /* input_line_pointer->after right-hand quantity. */
1698 /* left-hand quantity in resultP. */
1699 /* right-hand quantity in right. */
1700 /* operator in op_left. */
1701
1702 if (resultP->X_op == O_big)
1703 {
1704 if (resultP->X_add_number > 0)
1705 as_warn (_("left operand is a bignum; integer 0 assumed"));
1706 else
1707 as_warn (_("left operand is a float; integer 0 assumed"));
1708 resultP->X_op = O_constant;
1709 resultP->X_add_number = 0;
1710 resultP->X_add_symbol = NULL;
1711 resultP->X_op_symbol = NULL;
1712 }
1713 if (right.X_op == O_big)
1714 {
1715 if (right.X_add_number > 0)
1716 as_warn (_("right operand is a bignum; integer 0 assumed"));
1717 else
1718 as_warn (_("right operand is a float; integer 0 assumed"));
1719 right.X_op = O_constant;
1720 right.X_add_number = 0;
1721 right.X_add_symbol = NULL;
1722 right.X_op_symbol = NULL;
1723 }
1724
1725 /* Optimize common cases. */
1726 #ifdef md_optimize_expr
1727 if (md_optimize_expr (resultP, op_left, &right))
1728 {
1729 /* Skip. */
1730 ;
1731 }
1732 else
1733 #endif
1734 #ifndef md_register_arithmetic
1735 # define md_register_arithmetic 1
1736 #endif
1737 if (op_left == O_add && right.X_op == O_constant
1738 && (md_register_arithmetic || resultP->X_op != O_register))
1739 {
1740 /* X + constant. */
1741 resultP->X_add_number += right.X_add_number;
1742 }
1743 /* This case comes up in PIC code. */
1744 else if (op_left == O_subtract
1745 && right.X_op == O_symbol
1746 && resultP->X_op == O_symbol
1747 && retval == rightseg
1748 #ifdef md_allow_local_subtract
1749 && md_allow_local_subtract (resultP, & right, rightseg)
1750 #endif
1751 && (SEG_NORMAL (rightseg)
1752 || right.X_add_symbol == resultP->X_add_symbol)
1753 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1754 symbol_get_frag (right.X_add_symbol),
1755 &frag_off))
1756 {
1757 resultP->X_add_number -= right.X_add_number;
1758 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1759 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1760 - S_GET_VALUE (right.X_add_symbol));
1761 resultP->X_op = O_constant;
1762 resultP->X_add_symbol = 0;
1763 }
1764 else if (op_left == O_subtract && right.X_op == O_constant
1765 && (md_register_arithmetic || resultP->X_op != O_register))
1766 {
1767 /* X - constant. */
1768 resultP->X_add_number -= right.X_add_number;
1769 }
1770 else if (op_left == O_add && resultP->X_op == O_constant
1771 && (md_register_arithmetic || right.X_op != O_register))
1772 {
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;
1779 }
1780 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1781 {
1782 /* Constant OP constant. */
1783 offsetT v = right.X_add_number;
1784 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1785 {
1786 as_warn (_("division by zero"));
1787 v = 1;
1788 }
1789 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1790 && (op_left == O_left_shift || op_left == O_right_shift))
1791 {
1792 as_warn_value_out_of_range (_("shift count"), v, 0,
1793 sizeof(valueT) * CHAR_BIT - 1,
1794 NULL, 0);
1795 resultP->X_add_number = v = 0;
1796 }
1797 switch (op_left)
1798 {
1799 default: abort ();
1800 case O_multiply: resultP->X_add_number *= v; break;
1801 case O_divide: resultP->X_add_number /= v; break;
1802 case O_modulus: resultP->X_add_number %= v; break;
1803 case O_left_shift: resultP->X_add_number <<= v; break;
1804 case O_right_shift:
1805 /* We always use unsigned shifts, to avoid relying on
1806 characteristics of the compiler used to compile gas. */
1807 resultP->X_add_number =
1808 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1809 break;
1810 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1811 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1812 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1813 case O_bit_and: resultP->X_add_number &= v; break;
1814 /* Constant + constant (O_add) is handled by the
1815 previous if statement for constant + X, so is omitted
1816 here. */
1817 case O_subtract: resultP->X_add_number -= v; break;
1818 case O_eq:
1819 resultP->X_add_number =
1820 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1821 break;
1822 case O_ne:
1823 resultP->X_add_number =
1824 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1825 break;
1826 case O_lt:
1827 resultP->X_add_number =
1828 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1829 break;
1830 case O_le:
1831 resultP->X_add_number =
1832 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1833 break;
1834 case O_ge:
1835 resultP->X_add_number =
1836 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1837 break;
1838 case O_gt:
1839 resultP->X_add_number =
1840 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1841 break;
1842 case O_logical_and:
1843 resultP->X_add_number = resultP->X_add_number && v;
1844 break;
1845 case O_logical_or:
1846 resultP->X_add_number = resultP->X_add_number || v;
1847 break;
1848 }
1849 }
1850 else if (resultP->X_op == O_symbol
1851 && right.X_op == O_symbol
1852 && (op_left == O_add
1853 || op_left == O_subtract
1854 || (resultP->X_add_number == 0
1855 && right.X_add_number == 0)))
1856 {
1857 /* Symbol OP symbol. */
1858 resultP->X_op = op_left;
1859 resultP->X_op_symbol = right.X_add_symbol;
1860 if (op_left == O_add)
1861 resultP->X_add_number += right.X_add_number;
1862 else if (op_left == O_subtract)
1863 {
1864 resultP->X_add_number -= right.X_add_number;
1865 if (retval == rightseg && SEG_NORMAL (retval))
1866 {
1867 retval = absolute_section;
1868 rightseg = absolute_section;
1869 }
1870 }
1871 }
1872 else
1873 {
1874 /* The general case. */
1875 resultP->X_add_symbol = make_expr_symbol (resultP);
1876 resultP->X_op_symbol = make_expr_symbol (&right);
1877 resultP->X_op = op_left;
1878 resultP->X_add_number = 0;
1879 resultP->X_unsigned = 1;
1880 }
1881
1882 if (retval != rightseg)
1883 {
1884 if (! SEG_NORMAL (retval))
1885 {
1886 if (retval != undefined_section || SEG_NORMAL (rightseg))
1887 retval = rightseg;
1888 }
1889 else if (SEG_NORMAL (rightseg)
1890 #ifdef DIFF_EXPR_OK
1891 && op_left != O_subtract
1892 #endif
1893 )
1894 as_bad (_("operation combines symbols in different segments"));
1895 }
1896
1897 op_left = op_right;
1898 } /* While next operator is >= this rank. */
1899
1900 /* The PA port needs this information. */
1901 if (resultP->X_add_symbol)
1902 symbol_mark_used (resultP->X_add_symbol);
1903
1904 if (rank == 0 && mode == expr_evaluate)
1905 resolve_expression (resultP);
1906
1907 return resultP->X_op == O_constant ? absolute_section : retval;
1908 }
1909
1910 /* Resolve an expression without changing any symbols/sub-expressions
1911 used. */
1912
1913 int
1914 resolve_expression (expressionS *expressionP)
1915 {
1916 /* Help out with CSE. */
1917 valueT final_val = expressionP->X_add_number;
1918 symbolS *add_symbol = expressionP->X_add_symbol;
1919 symbolS *op_symbol = expressionP->X_op_symbol;
1920 operatorT op = expressionP->X_op;
1921 valueT left, right;
1922 segT seg_left, seg_right;
1923 fragS *frag_left, *frag_right;
1924 bfd_vma frag_off;
1925
1926 switch (op)
1927 {
1928 default:
1929 return 0;
1930
1931 case O_constant:
1932 case O_register:
1933 left = 0;
1934 break;
1935
1936 case O_symbol:
1937 case O_symbol_rva:
1938 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1939 return 0;
1940
1941 break;
1942
1943 case O_uminus:
1944 case O_bit_not:
1945 case O_logical_not:
1946 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1947 return 0;
1948
1949 if (seg_left != absolute_section)
1950 return 0;
1951
1952 if (op == O_logical_not)
1953 left = !left;
1954 else if (op == O_uminus)
1955 left = -left;
1956 else
1957 left = ~left;
1958 op = O_constant;
1959 break;
1960
1961 case O_multiply:
1962 case O_divide:
1963 case O_modulus:
1964 case O_left_shift:
1965 case O_right_shift:
1966 case O_bit_inclusive_or:
1967 case O_bit_or_not:
1968 case O_bit_exclusive_or:
1969 case O_bit_and:
1970 case O_add:
1971 case O_subtract:
1972 case O_eq:
1973 case O_ne:
1974 case O_lt:
1975 case O_le:
1976 case O_ge:
1977 case O_gt:
1978 case O_logical_and:
1979 case O_logical_or:
1980 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
1981 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
1982 return 0;
1983
1984 /* Simplify addition or subtraction of a constant by folding the
1985 constant into X_add_number. */
1986 if (op == O_add)
1987 {
1988 if (seg_right == absolute_section)
1989 {
1990 final_val += right;
1991 op = O_symbol;
1992 break;
1993 }
1994 else if (seg_left == absolute_section)
1995 {
1996 final_val += left;
1997 left = right;
1998 seg_left = seg_right;
1999 add_symbol = op_symbol;
2000 op = O_symbol;
2001 break;
2002 }
2003 }
2004 else if (op == O_subtract)
2005 {
2006 if (seg_right == absolute_section)
2007 {
2008 final_val -= right;
2009 op = O_symbol;
2010 break;
2011 }
2012 }
2013
2014 /* Equality and non-equality tests are permitted on anything.
2015 Subtraction, and other comparison operators are permitted if
2016 both operands are in the same section.
2017 Shifts by constant zero are permitted on anything.
2018 Multiplies, bit-ors, and bit-ands with constant zero are
2019 permitted on anything.
2020 Multiplies and divides by constant one are permitted on
2021 anything.
2022 Binary operations with both operands being the same register
2023 or undefined symbol are permitted if the result doesn't depend
2024 on the input value.
2025 Otherwise, both operands must be absolute. We already handled
2026 the case of addition or subtraction of a constant above. */
2027 frag_off = 0;
2028 if (!(seg_left == absolute_section
2029 && seg_right == absolute_section)
2030 && !(op == O_eq || op == O_ne)
2031 && !((op == O_subtract
2032 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2033 && seg_left == seg_right
2034 && (finalize_syms
2035 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2036 && (seg_left != reg_section || left == right)
2037 && (seg_left != undefined_section || add_symbol == op_symbol)))
2038 {
2039 if ((seg_left == absolute_section && left == 0)
2040 || (seg_right == absolute_section && right == 0))
2041 {
2042 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2043 {
2044 if (seg_right != absolute_section || right != 0)
2045 {
2046 seg_left = seg_right;
2047 left = right;
2048 add_symbol = op_symbol;
2049 }
2050 op = O_symbol;
2051 break;
2052 }
2053 else if (op == O_left_shift || op == O_right_shift)
2054 {
2055 if (seg_left != absolute_section || left != 0)
2056 {
2057 op = O_symbol;
2058 break;
2059 }
2060 }
2061 else if (op != O_multiply
2062 && op != O_bit_or_not && op != O_bit_and)
2063 return 0;
2064 }
2065 else if (op == O_multiply
2066 && seg_left == absolute_section && left == 1)
2067 {
2068 seg_left = seg_right;
2069 left = right;
2070 add_symbol = op_symbol;
2071 op = O_symbol;
2072 break;
2073 }
2074 else if ((op == O_multiply || op == O_divide)
2075 && seg_right == absolute_section && right == 1)
2076 {
2077 op = O_symbol;
2078 break;
2079 }
2080 else if (left != right
2081 || ((seg_left != reg_section || seg_right != reg_section)
2082 && (seg_left != undefined_section
2083 || seg_right != undefined_section
2084 || add_symbol != op_symbol)))
2085 return 0;
2086 else if (op == O_bit_and || op == O_bit_inclusive_or)
2087 {
2088 op = O_symbol;
2089 break;
2090 }
2091 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2092 return 0;
2093 }
2094
2095 right += frag_off / OCTETS_PER_BYTE;
2096 switch (op)
2097 {
2098 case O_add: left += right; break;
2099 case O_subtract: left -= right; break;
2100 case O_multiply: left *= right; break;
2101 case O_divide:
2102 if (right == 0)
2103 return 0;
2104 left = (offsetT) left / (offsetT) right;
2105 break;
2106 case O_modulus:
2107 if (right == 0)
2108 return 0;
2109 left = (offsetT) left % (offsetT) right;
2110 break;
2111 case O_left_shift: left <<= right; break;
2112 case O_right_shift: left >>= right; break;
2113 case O_bit_inclusive_or: left |= right; break;
2114 case O_bit_or_not: left |= ~right; break;
2115 case O_bit_exclusive_or: left ^= right; break;
2116 case O_bit_and: left &= right; break;
2117 case O_eq:
2118 case O_ne:
2119 left = (left == right
2120 && seg_left == seg_right
2121 && (finalize_syms || frag_left == frag_right)
2122 && (seg_left != undefined_section
2123 || add_symbol == op_symbol)
2124 ? ~ (valueT) 0 : 0);
2125 if (op == O_ne)
2126 left = ~left;
2127 break;
2128 case O_lt:
2129 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2130 break;
2131 case O_le:
2132 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2133 break;
2134 case O_ge:
2135 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2136 break;
2137 case O_gt:
2138 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2139 break;
2140 case O_logical_and: left = left && right; break;
2141 case O_logical_or: left = left || right; break;
2142 default: abort ();
2143 }
2144
2145 op = O_constant;
2146 break;
2147 }
2148
2149 if (op == O_symbol)
2150 {
2151 if (seg_left == absolute_section)
2152 op = O_constant;
2153 else if (seg_left == reg_section && final_val == 0)
2154 op = O_register;
2155 else if (add_symbol != expressionP->X_add_symbol)
2156 final_val += left;
2157 expressionP->X_add_symbol = add_symbol;
2158 }
2159 expressionP->X_op = op;
2160
2161 if (op == O_constant || op == O_register)
2162 final_val += left;
2163 expressionP->X_add_number = final_val;
2164
2165 return 1;
2166 }
2167 \f
2168 /* This lives here because it belongs equally in expr.c & read.c.
2169 expr.c is just a branch office read.c anyway, and putting it
2170 here lessens the crowd at read.c.
2171
2172 Assume input_line_pointer is at start of symbol name.
2173 Advance input_line_pointer past symbol name.
2174 Turn that character into a '\0', returning its former value.
2175 This allows a string compare (RMS wants symbol names to be strings)
2176 of the symbol name.
2177 There will always be a char following symbol name, because all good
2178 lines end in end-of-line. */
2179
2180 char
2181 get_symbol_end (void)
2182 {
2183 char c;
2184
2185 /* We accept \001 in a name in case this is being called with a
2186 constructed string. */
2187 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2188 {
2189 while (is_part_of_name (c = *input_line_pointer++)
2190 || c == '\001')
2191 ;
2192 if (is_name_ender (c))
2193 c = *input_line_pointer++;
2194 }
2195 *--input_line_pointer = 0;
2196 return (c);
2197 }
2198
2199 unsigned int
2200 get_single_number (void)
2201 {
2202 expressionS exp;
2203 operand (&exp, expr_normal);
2204 return exp.X_add_number;
2205 }