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