remove some duplicate #include's.
[deliverable/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
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 \f
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 *
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
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 \f
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 *
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 *
162 expr_build_dot (void)
163 {
164 expressionS e;
165
166 current_location (&e);
167 return make_expr_symbol (&e);
168 }
169 \f
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 \f
189 static void
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
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
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
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
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
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
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 if (c == '-')
1044 for (i = 0; i < expressionP->X_add_number; ++i)
1045 {
1046 generic_bignum[i] += 1;
1047 if (generic_bignum[i])
1048 break;
1049 }
1050 }
1051 else if (c == '!')
1052 {
1053 int nonzero = 0;
1054 for (i = 0; i < expressionP->X_add_number; ++i)
1055 {
1056 if (generic_bignum[i])
1057 nonzero = 1;
1058 generic_bignum[i] = 0;
1059 }
1060 generic_bignum[0] = nonzero;
1061 }
1062 }
1063 else if (expressionP->X_op != O_illegal
1064 && expressionP->X_op != O_absent)
1065 {
1066 if (c != '+')
1067 {
1068 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1069 if (c == '-')
1070 expressionP->X_op = O_uminus;
1071 else if (c == '~' || c == '"')
1072 expressionP->X_op = O_bit_not;
1073 else
1074 expressionP->X_op = O_logical_not;
1075 expressionP->X_add_number = 0;
1076 }
1077 }
1078 else
1079 as_warn (_("Unary operator %c ignored because bad operand follows"),
1080 c);
1081 }
1082 break;
1083
1084 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1085 case '$':
1086 /* '$' is the program counter when in MRI mode, or when
1087 DOLLAR_DOT is defined. */
1088 #ifndef DOLLAR_DOT
1089 if (! flag_m68k_mri)
1090 goto de_fault;
1091 #endif
1092 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1093 {
1094 /* In MRI mode and on Z80, '$' is also used as the prefix
1095 for a hexadecimal constant. */
1096 integer_constant (16, expressionP);
1097 break;
1098 }
1099
1100 if (is_part_of_name (*input_line_pointer))
1101 goto isname;
1102
1103 current_location (expressionP);
1104 break;
1105 #endif
1106
1107 case '.':
1108 if (!is_part_of_name (*input_line_pointer))
1109 {
1110 current_location (expressionP);
1111 break;
1112 }
1113 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1114 && ! is_part_of_name (input_line_pointer[8]))
1115 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1116 && ! is_part_of_name (input_line_pointer[7])))
1117 {
1118 int start;
1119
1120 start = (input_line_pointer[1] == 't'
1121 || input_line_pointer[1] == 'T');
1122 input_line_pointer += start ? 8 : 7;
1123 SKIP_WHITESPACE ();
1124 if (*input_line_pointer != '(')
1125 as_bad (_("syntax error in .startof. or .sizeof."));
1126 else
1127 {
1128 char *buf;
1129
1130 ++input_line_pointer;
1131 SKIP_WHITESPACE ();
1132 name = input_line_pointer;
1133 c = get_symbol_end ();
1134
1135 buf = (char *) xmalloc (strlen (name) + 10);
1136 if (start)
1137 sprintf (buf, ".startof.%s", name);
1138 else
1139 sprintf (buf, ".sizeof.%s", name);
1140 symbolP = symbol_make (buf);
1141 free (buf);
1142
1143 expressionP->X_op = O_symbol;
1144 expressionP->X_add_symbol = symbolP;
1145 expressionP->X_add_number = 0;
1146
1147 *input_line_pointer = c;
1148 SKIP_WHITESPACE ();
1149 if (*input_line_pointer != ')')
1150 as_bad (_("syntax error in .startof. or .sizeof."));
1151 else
1152 ++input_line_pointer;
1153 }
1154 break;
1155 }
1156 else
1157 {
1158 goto isname;
1159 }
1160
1161 case ',':
1162 eol:
1163 /* Can't imagine any other kind of operand. */
1164 expressionP->X_op = O_absent;
1165 input_line_pointer--;
1166 break;
1167
1168 #ifdef TC_M68K
1169 case '%':
1170 if (! flag_m68k_mri)
1171 goto de_fault;
1172 integer_constant (2, expressionP);
1173 break;
1174
1175 case '@':
1176 if (! flag_m68k_mri)
1177 goto de_fault;
1178 integer_constant (8, expressionP);
1179 break;
1180
1181 case ':':
1182 if (! flag_m68k_mri)
1183 goto de_fault;
1184
1185 /* In MRI mode, this is a floating point constant represented
1186 using hexadecimal digits. */
1187
1188 ++input_line_pointer;
1189 integer_constant (16, expressionP);
1190 break;
1191
1192 case '*':
1193 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1194 goto de_fault;
1195
1196 current_location (expressionP);
1197 break;
1198 #endif
1199
1200 default:
1201 #ifdef TC_M68K
1202 de_fault:
1203 #endif
1204 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1205 {
1206 /* Identifier begins here.
1207 This is kludged for speed, so code is repeated. */
1208 isname:
1209 name = --input_line_pointer;
1210 c = get_symbol_end ();
1211
1212 #ifdef md_parse_name
1213 /* This is a hook for the backend to parse certain names
1214 specially in certain contexts. If a name always has a
1215 specific value, it can often be handled by simply
1216 entering it in the symbol table. */
1217 if (md_parse_name (name, expressionP, mode, &c))
1218 {
1219 *input_line_pointer = c;
1220 break;
1221 }
1222 #endif
1223
1224 #ifdef TC_I960
1225 /* The MRI i960 assembler permits
1226 lda sizeof code,g13
1227 FIXME: This should use md_parse_name. */
1228 if (flag_mri
1229 && (strcasecmp (name, "sizeof") == 0
1230 || strcasecmp (name, "startof") == 0))
1231 {
1232 int start;
1233 char *buf;
1234
1235 start = (name[1] == 't'
1236 || name[1] == 'T');
1237
1238 *input_line_pointer = c;
1239 SKIP_WHITESPACE ();
1240
1241 name = input_line_pointer;
1242 c = get_symbol_end ();
1243
1244 buf = (char *) xmalloc (strlen (name) + 10);
1245 if (start)
1246 sprintf (buf, ".startof.%s", name);
1247 else
1248 sprintf (buf, ".sizeof.%s", name);
1249 symbolP = symbol_make (buf);
1250 free (buf);
1251
1252 expressionP->X_op = O_symbol;
1253 expressionP->X_add_symbol = symbolP;
1254 expressionP->X_add_number = 0;
1255
1256 *input_line_pointer = c;
1257 SKIP_WHITESPACE ();
1258
1259 break;
1260 }
1261 #endif
1262
1263 symbolP = symbol_find_or_make (name);
1264
1265 /* If we have an absolute symbol or a reg, then we know its
1266 value now. */
1267 segment = S_GET_SEGMENT (symbolP);
1268 if (mode != expr_defer && segment == absolute_section)
1269 {
1270 expressionP->X_op = O_constant;
1271 expressionP->X_add_number = S_GET_VALUE (symbolP);
1272 }
1273 else if (mode != expr_defer && segment == reg_section)
1274 {
1275 expressionP->X_op = O_register;
1276 expressionP->X_add_number = S_GET_VALUE (symbolP);
1277 }
1278 else
1279 {
1280 expressionP->X_op = O_symbol;
1281 expressionP->X_add_symbol = symbolP;
1282 expressionP->X_add_number = 0;
1283 }
1284 *input_line_pointer = c;
1285 }
1286 else
1287 {
1288 /* Let the target try to parse it. Success is indicated by changing
1289 the X_op field to something other than O_absent and pointing
1290 input_line_pointer past the expression. If it can't parse the
1291 expression, X_op and input_line_pointer should be unchanged. */
1292 expressionP->X_op = O_absent;
1293 --input_line_pointer;
1294 md_operand (expressionP);
1295 if (expressionP->X_op == O_absent)
1296 {
1297 ++input_line_pointer;
1298 as_bad (_("bad expression"));
1299 expressionP->X_op = O_constant;
1300 expressionP->X_add_number = 0;
1301 }
1302 }
1303 break;
1304 }
1305
1306 /* It is more 'efficient' to clean up the expressionS when they are
1307 created. Doing it here saves lines of code. */
1308 clean_up_expression (expressionP);
1309 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1310 know (*input_line_pointer != ' ');
1311
1312 /* The PA port needs this information. */
1313 if (expressionP->X_add_symbol)
1314 symbol_mark_used (expressionP->X_add_symbol);
1315
1316 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1317 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1318
1319 switch (expressionP->X_op)
1320 {
1321 default:
1322 return absolute_section;
1323 case O_symbol:
1324 return S_GET_SEGMENT (expressionP->X_add_symbol);
1325 case O_register:
1326 return reg_section;
1327 }
1328 }
1329 \f
1330 /* Internal. Simplify a struct expression for use by expr (). */
1331
1332 /* In: address of an expressionS.
1333 The X_op field of the expressionS may only take certain values.
1334 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1335
1336 Out: expressionS may have been modified:
1337 Unused fields zeroed to help expr (). */
1338
1339 static void
1340 clean_up_expression (expressionS *expressionP)
1341 {
1342 switch (expressionP->X_op)
1343 {
1344 case O_illegal:
1345 case O_absent:
1346 expressionP->X_add_number = 0;
1347 /* Fall through. */
1348 case O_big:
1349 case O_constant:
1350 case O_register:
1351 expressionP->X_add_symbol = NULL;
1352 /* Fall through. */
1353 case O_symbol:
1354 case O_uminus:
1355 case O_bit_not:
1356 expressionP->X_op_symbol = NULL;
1357 break;
1358 default:
1359 break;
1360 }
1361 }
1362 \f
1363 /* Expression parser. */
1364
1365 /* We allow an empty expression, and just assume (absolute,0) silently.
1366 Unary operators and parenthetical expressions are treated as operands.
1367 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1368
1369 We used to do an aho/ullman shift-reduce parser, but the logic got so
1370 warped that I flushed it and wrote a recursive-descent parser instead.
1371 Now things are stable, would anybody like to write a fast parser?
1372 Most expressions are either register (which does not even reach here)
1373 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1374 So I guess it doesn't really matter how inefficient more complex expressions
1375 are parsed.
1376
1377 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1378 Also, we have consumed any leading or trailing spaces (operand does that)
1379 and done all intervening operators.
1380
1381 This returns the segment of the result, which will be
1382 absolute_section or the segment of a symbol. */
1383
1384 #undef __
1385 #define __ O_illegal
1386 #ifndef O_SINGLE_EQ
1387 #define O_SINGLE_EQ O_illegal
1388 #endif
1389
1390 /* Maps ASCII -> operators. */
1391 static const operatorT op_encoding[256] = {
1392 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1393 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1394
1395 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1396 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1397 __, __, __, __, __, __, __, __,
1398 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1399 __, __, __, __, __, __, __, __,
1400 __, __, __, __, __, __, __, __,
1401 __, __, __, __, __, __, __, __,
1402 __, __, __,
1403 #ifdef NEED_INDEX_OPERATOR
1404 O_index,
1405 #else
1406 __,
1407 #endif
1408 __, __, O_bit_exclusive_or, __,
1409 __, __, __, __, __, __, __, __,
1410 __, __, __, __, __, __, __, __,
1411 __, __, __, __, __, __, __, __,
1412 __, __, __, __, O_bit_inclusive_or, __, __, __,
1413
1414 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1415 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1416 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1417 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1418 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1419 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1420 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1421 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1422 };
1423
1424 /* Rank Examples
1425 0 operand, (expression)
1426 1 ||
1427 2 &&
1428 3 == <> < <= >= >
1429 4 + -
1430 5 used for * / % in MRI mode
1431 6 & ^ ! |
1432 7 * / % << >>
1433 8 unary - unary ~
1434 */
1435 static operator_rankT op_rank[] = {
1436 0, /* O_illegal */
1437 0, /* O_absent */
1438 0, /* O_constant */
1439 0, /* O_symbol */
1440 0, /* O_symbol_rva */
1441 0, /* O_register */
1442 0, /* O_big */
1443 9, /* O_uminus */
1444 9, /* O_bit_not */
1445 9, /* O_logical_not */
1446 8, /* O_multiply */
1447 8, /* O_divide */
1448 8, /* O_modulus */
1449 8, /* O_left_shift */
1450 8, /* O_right_shift */
1451 7, /* O_bit_inclusive_or */
1452 7, /* O_bit_or_not */
1453 7, /* O_bit_exclusive_or */
1454 7, /* O_bit_and */
1455 5, /* O_add */
1456 5, /* O_subtract */
1457 4, /* O_eq */
1458 4, /* O_ne */
1459 4, /* O_lt */
1460 4, /* O_le */
1461 4, /* O_ge */
1462 4, /* O_gt */
1463 3, /* O_logical_and */
1464 2, /* O_logical_or */
1465 1, /* O_index */
1466 0, /* O_md1 */
1467 0, /* O_md2 */
1468 0, /* O_md3 */
1469 0, /* O_md4 */
1470 0, /* O_md5 */
1471 0, /* O_md6 */
1472 0, /* O_md7 */
1473 0, /* O_md8 */
1474 0, /* O_md9 */
1475 0, /* O_md10 */
1476 0, /* O_md11 */
1477 0, /* O_md12 */
1478 0, /* O_md13 */
1479 0, /* O_md14 */
1480 0, /* O_md15 */
1481 0, /* O_md16 */
1482 };
1483
1484 /* Unfortunately, in MRI mode for the m68k, multiplication and
1485 division have lower precedence than the bit wise operators. This
1486 function sets the operator precedences correctly for the current
1487 mode. Also, MRI uses a different bit_not operator, and this fixes
1488 that as well. */
1489
1490 #define STANDARD_MUL_PRECEDENCE 8
1491 #define MRI_MUL_PRECEDENCE 6
1492
1493 void
1494 expr_set_precedence (void)
1495 {
1496 if (flag_m68k_mri)
1497 {
1498 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1499 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1500 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1501 }
1502 else
1503 {
1504 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1505 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1506 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1507 }
1508 }
1509
1510 /* Initialize the expression parser. */
1511
1512 void
1513 expr_begin (void)
1514 {
1515 expr_set_precedence ();
1516
1517 /* Verify that X_op field is wide enough. */
1518 {
1519 expressionS e;
1520 e.X_op = O_max;
1521 assert (e.X_op == O_max);
1522 }
1523 }
1524 \f
1525 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1526 sets NUM_CHARS to the number of characters in the operator.
1527 Does not advance INPUT_LINE_POINTER. */
1528
1529 static inline operatorT
1530 operator (int *num_chars)
1531 {
1532 int c;
1533 operatorT ret;
1534
1535 c = *input_line_pointer & 0xff;
1536 *num_chars = 1;
1537
1538 if (is_end_of_line[c])
1539 return O_illegal;
1540
1541 switch (c)
1542 {
1543 default:
1544 return op_encoding[c];
1545
1546 case '+':
1547 case '-':
1548 return op_encoding[c];
1549
1550 case '<':
1551 switch (input_line_pointer[1])
1552 {
1553 default:
1554 return op_encoding[c];
1555 case '<':
1556 ret = O_left_shift;
1557 break;
1558 case '>':
1559 ret = O_ne;
1560 break;
1561 case '=':
1562 ret = O_le;
1563 break;
1564 }
1565 *num_chars = 2;
1566 return ret;
1567
1568 case '=':
1569 if (input_line_pointer[1] != '=')
1570 return op_encoding[c];
1571
1572 *num_chars = 2;
1573 return O_eq;
1574
1575 case '>':
1576 switch (input_line_pointer[1])
1577 {
1578 default:
1579 return op_encoding[c];
1580 case '>':
1581 ret = O_right_shift;
1582 break;
1583 case '=':
1584 ret = O_ge;
1585 break;
1586 }
1587 *num_chars = 2;
1588 return ret;
1589
1590 case '!':
1591 switch (input_line_pointer[1])
1592 {
1593 case '!':
1594 /* We accept !! as equivalent to ^ for MRI compatibility. */
1595 *num_chars = 2;
1596 return O_bit_exclusive_or;
1597 case '=':
1598 /* We accept != as equivalent to <>. */
1599 *num_chars = 2;
1600 return O_ne;
1601 default:
1602 if (flag_m68k_mri)
1603 return O_bit_inclusive_or;
1604 return op_encoding[c];
1605 }
1606
1607 case '|':
1608 if (input_line_pointer[1] != '|')
1609 return op_encoding[c];
1610
1611 *num_chars = 2;
1612 return O_logical_or;
1613
1614 case '&':
1615 if (input_line_pointer[1] != '&')
1616 return op_encoding[c];
1617
1618 *num_chars = 2;
1619 return O_logical_and;
1620 }
1621
1622 /* NOTREACHED */
1623 }
1624
1625 /* Parse an expression. */
1626
1627 segT
1628 expr (int rankarg, /* Larger # is higher rank. */
1629 expressionS *resultP, /* Deliver result here. */
1630 enum expr_mode mode /* Controls behavior. */)
1631 {
1632 operator_rankT rank = (operator_rankT) rankarg;
1633 segT retval;
1634 expressionS right;
1635 operatorT op_left;
1636 operatorT op_right;
1637 int op_chars;
1638
1639 know (rank >= 0);
1640
1641 /* Save the value of dot for the fixup code. */
1642 if (rank == 0)
1643 dot_value = frag_now_fix ();
1644
1645 retval = operand (resultP, mode);
1646
1647 /* operand () gobbles spaces. */
1648 know (*input_line_pointer != ' ');
1649
1650 op_left = operator (&op_chars);
1651 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1652 {
1653 segT rightseg;
1654 bfd_vma frag_off;
1655
1656 input_line_pointer += op_chars; /* -> after operator. */
1657
1658 rightseg = expr (op_rank[(int) op_left], &right, mode);
1659 if (right.X_op == O_absent)
1660 {
1661 as_warn (_("missing operand; zero assumed"));
1662 right.X_op = O_constant;
1663 right.X_add_number = 0;
1664 right.X_add_symbol = NULL;
1665 right.X_op_symbol = NULL;
1666 }
1667
1668 know (*input_line_pointer != ' ');
1669
1670 if (op_left == O_index)
1671 {
1672 if (*input_line_pointer != ']')
1673 as_bad ("missing right bracket");
1674 else
1675 {
1676 ++input_line_pointer;
1677 SKIP_WHITESPACE ();
1678 }
1679 }
1680
1681 op_right = operator (&op_chars);
1682
1683 know (op_right == O_illegal
1684 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1685 know ((int) op_left >= (int) O_multiply
1686 && (int) op_left <= (int) O_index);
1687
1688 /* input_line_pointer->after right-hand quantity. */
1689 /* left-hand quantity in resultP. */
1690 /* right-hand quantity in right. */
1691 /* operator in op_left. */
1692
1693 if (resultP->X_op == O_big)
1694 {
1695 if (resultP->X_add_number > 0)
1696 as_warn (_("left operand is a bignum; integer 0 assumed"));
1697 else
1698 as_warn (_("left operand is a float; integer 0 assumed"));
1699 resultP->X_op = O_constant;
1700 resultP->X_add_number = 0;
1701 resultP->X_add_symbol = NULL;
1702 resultP->X_op_symbol = NULL;
1703 }
1704 if (right.X_op == O_big)
1705 {
1706 if (right.X_add_number > 0)
1707 as_warn (_("right operand is a bignum; integer 0 assumed"));
1708 else
1709 as_warn (_("right operand is a float; integer 0 assumed"));
1710 right.X_op = O_constant;
1711 right.X_add_number = 0;
1712 right.X_add_symbol = NULL;
1713 right.X_op_symbol = NULL;
1714 }
1715
1716 /* Optimize common cases. */
1717 #ifdef md_optimize_expr
1718 if (md_optimize_expr (resultP, op_left, &right))
1719 {
1720 /* Skip. */
1721 ;
1722 }
1723 else
1724 #endif
1725 if (op_left == O_add && right.X_op == O_constant)
1726 {
1727 /* X + constant. */
1728 resultP->X_add_number += right.X_add_number;
1729 }
1730 /* This case comes up in PIC code. */
1731 else if (op_left == O_subtract
1732 && right.X_op == O_symbol
1733 && resultP->X_op == O_symbol
1734 && retval == rightseg
1735 && (SEG_NORMAL (rightseg)
1736 || right.X_add_symbol == resultP->X_add_symbol)
1737 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1738 symbol_get_frag (right.X_add_symbol),
1739 &frag_off))
1740 {
1741 resultP->X_add_number -= right.X_add_number;
1742 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1743 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1744 - S_GET_VALUE (right.X_add_symbol));
1745 resultP->X_op = O_constant;
1746 resultP->X_add_symbol = 0;
1747 }
1748 else if (op_left == O_subtract && right.X_op == O_constant)
1749 {
1750 /* X - constant. */
1751 resultP->X_add_number -= right.X_add_number;
1752 }
1753 else if (op_left == O_add && resultP->X_op == O_constant)
1754 {
1755 /* Constant + X. */
1756 resultP->X_op = right.X_op;
1757 resultP->X_add_symbol = right.X_add_symbol;
1758 resultP->X_op_symbol = right.X_op_symbol;
1759 resultP->X_add_number += right.X_add_number;
1760 retval = rightseg;
1761 }
1762 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1763 {
1764 /* Constant OP constant. */
1765 offsetT v = right.X_add_number;
1766 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1767 {
1768 as_warn (_("division by zero"));
1769 v = 1;
1770 }
1771 switch (op_left)
1772 {
1773 default: abort ();
1774 case O_multiply: resultP->X_add_number *= v; break;
1775 case O_divide: resultP->X_add_number /= v; break;
1776 case O_modulus: resultP->X_add_number %= v; break;
1777 case O_left_shift: resultP->X_add_number <<= v; break;
1778 case O_right_shift:
1779 /* We always use unsigned shifts, to avoid relying on
1780 characteristics of the compiler used to compile gas. */
1781 resultP->X_add_number =
1782 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1783 break;
1784 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1785 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1786 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1787 case O_bit_and: resultP->X_add_number &= v; break;
1788 case O_add: resultP->X_add_number += v; break;
1789 case O_subtract: resultP->X_add_number -= v; break;
1790 case O_eq:
1791 resultP->X_add_number =
1792 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1793 break;
1794 case O_ne:
1795 resultP->X_add_number =
1796 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1797 break;
1798 case O_lt:
1799 resultP->X_add_number =
1800 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1801 break;
1802 case O_le:
1803 resultP->X_add_number =
1804 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1805 break;
1806 case O_ge:
1807 resultP->X_add_number =
1808 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1809 break;
1810 case O_gt:
1811 resultP->X_add_number =
1812 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1813 break;
1814 case O_logical_and:
1815 resultP->X_add_number = resultP->X_add_number && v;
1816 break;
1817 case O_logical_or:
1818 resultP->X_add_number = resultP->X_add_number || v;
1819 break;
1820 }
1821 }
1822 else if (resultP->X_op == O_symbol
1823 && right.X_op == O_symbol
1824 && (op_left == O_add
1825 || op_left == O_subtract
1826 || (resultP->X_add_number == 0
1827 && right.X_add_number == 0)))
1828 {
1829 /* Symbol OP symbol. */
1830 resultP->X_op = op_left;
1831 resultP->X_op_symbol = right.X_add_symbol;
1832 if (op_left == O_add)
1833 resultP->X_add_number += right.X_add_number;
1834 else if (op_left == O_subtract)
1835 {
1836 resultP->X_add_number -= right.X_add_number;
1837 if (retval == rightseg && SEG_NORMAL (retval))
1838 {
1839 retval = absolute_section;
1840 rightseg = absolute_section;
1841 }
1842 }
1843 }
1844 else
1845 {
1846 /* The general case. */
1847 resultP->X_add_symbol = make_expr_symbol (resultP);
1848 resultP->X_op_symbol = make_expr_symbol (&right);
1849 resultP->X_op = op_left;
1850 resultP->X_add_number = 0;
1851 resultP->X_unsigned = 1;
1852 }
1853
1854 if (retval != rightseg)
1855 {
1856 if (! SEG_NORMAL (retval))
1857 {
1858 if (retval != undefined_section || SEG_NORMAL (rightseg))
1859 retval = rightseg;
1860 }
1861 else if (SEG_NORMAL (rightseg)
1862 #ifdef DIFF_EXPR_OK
1863 && op_left != O_subtract
1864 #endif
1865 )
1866 as_bad (_("operation combines symbols in different segments"));
1867 }
1868
1869 op_left = op_right;
1870 } /* While next operator is >= this rank. */
1871
1872 /* The PA port needs this information. */
1873 if (resultP->X_add_symbol)
1874 symbol_mark_used (resultP->X_add_symbol);
1875
1876 if (rank == 0 && mode == expr_evaluate)
1877 resolve_expression (resultP);
1878
1879 return resultP->X_op == O_constant ? absolute_section : retval;
1880 }
1881
1882 /* Resolve an expression without changing any symbols/sub-expressions
1883 used. */
1884
1885 int
1886 resolve_expression (expressionS *expressionP)
1887 {
1888 /* Help out with CSE. */
1889 valueT final_val = expressionP->X_add_number;
1890 symbolS *add_symbol = expressionP->X_add_symbol;
1891 symbolS *op_symbol = expressionP->X_op_symbol;
1892 operatorT op = expressionP->X_op;
1893 valueT left, right;
1894 segT seg_left, seg_right;
1895 fragS *frag_left, *frag_right;
1896 bfd_vma frag_off;
1897
1898 switch (op)
1899 {
1900 default:
1901 return 0;
1902
1903 case O_constant:
1904 case O_register:
1905 left = 0;
1906 break;
1907
1908 case O_symbol:
1909 case O_symbol_rva:
1910 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1911 return 0;
1912
1913 break;
1914
1915 case O_uminus:
1916 case O_bit_not:
1917 case O_logical_not:
1918 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
1919 return 0;
1920
1921 if (seg_left != absolute_section)
1922 return 0;
1923
1924 if (op == O_logical_not)
1925 left = !left;
1926 else if (op == O_uminus)
1927 left = -left;
1928 else
1929 left = ~left;
1930 op = O_constant;
1931 break;
1932
1933 case O_multiply:
1934 case O_divide:
1935 case O_modulus:
1936 case O_left_shift:
1937 case O_right_shift:
1938 case O_bit_inclusive_or:
1939 case O_bit_or_not:
1940 case O_bit_exclusive_or:
1941 case O_bit_and:
1942 case O_add:
1943 case O_subtract:
1944 case O_eq:
1945 case O_ne:
1946 case O_lt:
1947 case O_le:
1948 case O_ge:
1949 case O_gt:
1950 case O_logical_and:
1951 case O_logical_or:
1952 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
1953 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
1954 return 0;
1955
1956 /* Simplify addition or subtraction of a constant by folding the
1957 constant into X_add_number. */
1958 if (op == O_add)
1959 {
1960 if (seg_right == absolute_section)
1961 {
1962 final_val += right;
1963 op = O_symbol;
1964 break;
1965 }
1966 else if (seg_left == absolute_section)
1967 {
1968 final_val += left;
1969 left = right;
1970 seg_left = seg_right;
1971 add_symbol = op_symbol;
1972 op = O_symbol;
1973 break;
1974 }
1975 }
1976 else if (op == O_subtract)
1977 {
1978 if (seg_right == absolute_section)
1979 {
1980 final_val -= right;
1981 op = O_symbol;
1982 break;
1983 }
1984 }
1985
1986 /* Equality and non-equality tests are permitted on anything.
1987 Subtraction, and other comparison operators are permitted if
1988 both operands are in the same section.
1989 Shifts by constant zero are permitted on anything.
1990 Multiplies, bit-ors, and bit-ands with constant zero are
1991 permitted on anything.
1992 Multiplies and divides by constant one are permitted on
1993 anything.
1994 Binary operations with both operands being the same register
1995 or undefined symbol are permitted if the result doesn't depend
1996 on the input value.
1997 Otherwise, both operands must be absolute. We already handled
1998 the case of addition or subtraction of a constant above. */
1999 frag_off = 0;
2000 if (!(seg_left == absolute_section
2001 && seg_right == absolute_section)
2002 && !(op == O_eq || op == O_ne)
2003 && !((op == O_subtract
2004 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2005 && seg_left == seg_right
2006 && (finalize_syms
2007 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2008 && (seg_left != reg_section || left == right)
2009 && (seg_left != undefined_section || add_symbol == op_symbol)))
2010 {
2011 if ((seg_left == absolute_section && left == 0)
2012 || (seg_right == absolute_section && right == 0))
2013 {
2014 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2015 {
2016 if (seg_right != absolute_section || right != 0)
2017 {
2018 seg_left = seg_right;
2019 left = right;
2020 add_symbol = op_symbol;
2021 }
2022 op = O_symbol;
2023 break;
2024 }
2025 else if (op == O_left_shift || op == O_right_shift)
2026 {
2027 if (seg_left != absolute_section || left != 0)
2028 {
2029 op = O_symbol;
2030 break;
2031 }
2032 }
2033 else if (op != O_multiply
2034 && op != O_bit_or_not && op != O_bit_and)
2035 return 0;
2036 }
2037 else if (op == O_multiply
2038 && seg_left == absolute_section && left == 1)
2039 {
2040 seg_left = seg_right;
2041 left = right;
2042 add_symbol = op_symbol;
2043 op = O_symbol;
2044 break;
2045 }
2046 else if ((op == O_multiply || op == O_divide)
2047 && seg_right == absolute_section && right == 1)
2048 {
2049 op = O_symbol;
2050 break;
2051 }
2052 else if (left != right
2053 || ((seg_left != reg_section || seg_right != reg_section)
2054 && (seg_left != undefined_section
2055 || seg_right != undefined_section
2056 || add_symbol != op_symbol)))
2057 return 0;
2058 else if (op == O_bit_and || op == O_bit_inclusive_or)
2059 {
2060 op = O_symbol;
2061 break;
2062 }
2063 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2064 return 0;
2065 }
2066
2067 right += frag_off / OCTETS_PER_BYTE;
2068 switch (op)
2069 {
2070 case O_add: left += right; break;
2071 case O_subtract: left -= right; break;
2072 case O_multiply: left *= right; break;
2073 case O_divide:
2074 if (right == 0)
2075 return 0;
2076 left = (offsetT) left / (offsetT) right;
2077 break;
2078 case O_modulus:
2079 if (right == 0)
2080 return 0;
2081 left = (offsetT) left % (offsetT) right;
2082 break;
2083 case O_left_shift: left <<= right; break;
2084 case O_right_shift: left >>= right; break;
2085 case O_bit_inclusive_or: left |= right; break;
2086 case O_bit_or_not: left |= ~right; break;
2087 case O_bit_exclusive_or: left ^= right; break;
2088 case O_bit_and: left &= right; break;
2089 case O_eq:
2090 case O_ne:
2091 left = (left == right
2092 && seg_left == seg_right
2093 && (finalize_syms || frag_left == frag_right)
2094 && (seg_left != undefined_section
2095 || add_symbol == op_symbol)
2096 ? ~ (valueT) 0 : 0);
2097 if (op == O_ne)
2098 left = ~left;
2099 break;
2100 case O_lt:
2101 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2102 break;
2103 case O_le:
2104 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2105 break;
2106 case O_ge:
2107 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2108 break;
2109 case O_gt:
2110 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2111 break;
2112 case O_logical_and: left = left && right; break;
2113 case O_logical_or: left = left || right; break;
2114 default: abort ();
2115 }
2116
2117 op = O_constant;
2118 break;
2119 }
2120
2121 if (op == O_symbol)
2122 {
2123 if (seg_left == absolute_section)
2124 op = O_constant;
2125 else if (seg_left == reg_section && final_val == 0)
2126 op = O_register;
2127 else if (add_symbol != expressionP->X_add_symbol)
2128 final_val += left;
2129 expressionP->X_add_symbol = add_symbol;
2130 }
2131 expressionP->X_op = op;
2132
2133 if (op == O_constant || op == O_register)
2134 final_val += left;
2135 expressionP->X_add_number = final_val;
2136
2137 return 1;
2138 }
2139 \f
2140 /* This lives here because it belongs equally in expr.c & read.c.
2141 expr.c is just a branch office read.c anyway, and putting it
2142 here lessens the crowd at read.c.
2143
2144 Assume input_line_pointer is at start of symbol name.
2145 Advance input_line_pointer past symbol name.
2146 Turn that character into a '\0', returning its former value.
2147 This allows a string compare (RMS wants symbol names to be strings)
2148 of the symbol name.
2149 There will always be a char following symbol name, because all good
2150 lines end in end-of-line. */
2151
2152 char
2153 get_symbol_end (void)
2154 {
2155 char c;
2156
2157 /* We accept \001 in a name in case this is being called with a
2158 constructed string. */
2159 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2160 {
2161 while (is_part_of_name (c = *input_line_pointer++)
2162 || c == '\001')
2163 ;
2164 if (is_name_ender (c))
2165 c = *input_line_pointer++;
2166 }
2167 *--input_line_pointer = 0;
2168 return (c);
2169 }
2170
2171 unsigned int
2172 get_single_number (void)
2173 {
2174 expressionS exp;
2175 operand (&exp, expr_normal);
2176 return exp.X_add_number;
2177 }
This page took 0.071267 seconds and 5 git commands to generate.