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