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