* config/obj-bout.h (S_GET_VALUE): Removed unnecessary cast.
[deliverable/binutils-gdb.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21 * This is really a branch office of as-read.c. I split it out to clearly
22 * distinguish the world of expressions from the world of statements.
23 * (It also gives smaller files to re-compile.)
24 * Here, "operand"s are of expressions, not instructions.
25 */
26
27 #include <ctype.h>
28 #include <string.h>
29
30 #include "as.h"
31
32 #include "obstack.h"
33
34 static void clean_up_expression PARAMS ((expressionS * expressionP));
35 extern const char EXP_CHARS[], FLT_CHARS[];
36
37 /*
38 * Build any floating-point literal here.
39 * Also build any bignum literal here.
40 */
41
42 /* Seems atof_machine can backscan through generic_bignum and hit whatever
43 happens to be loaded before it in memory. And its way too complicated
44 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
45 and never write into the early words, thus they'll always be zero.
46 I hate Dean's floating-point code. Bleh. */
47 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
48 FLONUM_TYPE generic_floating_point_number =
49 {
50 &generic_bignum[6], /* low (JF: Was 0) */
51 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
52 0, /* leader */
53 0, /* exponent */
54 0 /* sign */
55 };
56 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
57 int generic_floating_point_magic;
58 \f
59 floating_constant (expressionP)
60 expressionS *expressionP;
61 {
62 /* input_line_pointer->*/
63 /* floating-point constant. */
64 int error_code;
65
66 error_code = atof_generic
67 (&input_line_pointer, ".", EXP_CHARS,
68 &generic_floating_point_number);
69
70 if (error_code)
71 {
72 if (error_code == ERROR_EXPONENT_OVERFLOW)
73 {
74 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
75 }
76 else
77 {
78 as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
79 }
80 }
81 expressionP->X_seg = big_section;
82 /* input_line_pointer->just after constant, */
83 /* which may point to whitespace. */
84 expressionP->X_add_number = -1;
85 }
86
87
88
89 integer_constant (radix, expressionP)
90 int radix;
91 expressionS *expressionP;
92 {
93 register char *digit_2; /*->2nd digit of number. */
94 char c;
95
96 register valueT number; /* offset or (absolute) value */
97 register short int digit; /* value of next digit in current radix */
98 register short int maxdig = 0;/* highest permitted digit value. */
99 register int too_many_digits = 0; /* if we see >= this number of */
100 register char *name; /* points to name of symbol */
101 register symbolS *symbolP; /* points to symbol */
102
103 int small; /* true if fits in 32 bits. */
104 extern const char hex_value[]; /* in hex_value.c */
105
106 /* may be bignum, or may fit in 32 bits. */
107 /*
108 * most numbers fit into 32 bits, and we want this case to be fast.
109 * so we pretend it will fit into 32 bits. if, after making up a 32
110 * bit number, we realise that we have scanned more digits than
111 * comfortably fit into 32 bits, we re-scan the digits coding
112 * them into a bignum. for decimal and octal numbers we are conservative: some
113 * numbers may be assumed bignums when in fact they do fit into 32 bits.
114 * numbers of any radix can have excess leading zeros: we strive
115 * to recognise this and cast them back into 32 bits.
116 * we must check that the bignum really is more than 32
117 * bits, and change it back to a 32-bit number if it fits.
118 * the number we are looking for is expected to be positive, but
119 * if it fits into 32 bits as an unsigned number, we let it be a 32-bit
120 * number. the cavalier approach is for speed in ordinary cases.
121 */
122
123 switch (radix)
124 {
125
126 case 2:
127 maxdig = 2;
128 too_many_digits = 33;
129 break;
130 case 8:
131 maxdig = radix = 8;
132 too_many_digits = 11;
133 break;
134 case 16:
135
136
137 maxdig = radix = 16;
138 too_many_digits = 9;
139 break;
140 case 10:
141 maxdig = radix = 10;
142 too_many_digits = 11;
143 }
144 c = *input_line_pointer;
145 input_line_pointer++;
146 digit_2 = input_line_pointer;
147 for (number = 0; (digit = hex_value[c]) < maxdig; c = *input_line_pointer++)
148 {
149 number = number * radix + digit;
150 }
151 /* c contains character after number. */
152 /* input_line_pointer->char after c. */
153 small = input_line_pointer - digit_2 < too_many_digits;
154 if (!small)
155 {
156 /*
157 * we saw a lot of digits. manufacture a bignum the hard way.
158 */
159 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
160 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
161 long carry;
162
163 leader = generic_bignum;
164 generic_bignum[0] = 0;
165 generic_bignum[1] = 0;
166 /* we could just use digit_2, but lets be mnemonic. */
167 input_line_pointer = --digit_2; /*->1st digit. */
168 c = *input_line_pointer++;
169 for (; (carry = hex_value[c]) < maxdig; c = *input_line_pointer++)
170 {
171 for (pointer = generic_bignum;
172 pointer <= leader;
173 pointer++)
174 {
175 long work;
176
177 work = carry + radix * *pointer;
178 *pointer = work & LITTLENUM_MASK;
179 carry = work >> LITTLENUM_NUMBER_OF_BITS;
180 }
181 if (carry)
182 {
183 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
184 { /* room to grow a longer bignum. */
185 *++leader = carry;
186 }
187 }
188 }
189 /* again, c is char after number, */
190 /* input_line_pointer->after c. */
191 know (sizeof (int) * 8 == 32);
192 know (LITTLENUM_NUMBER_OF_BITS == 16);
193 /* hence the constant "2" in the next line. */
194 if (leader < generic_bignum + 2)
195 { /* will fit into 32 bits. */
196 number =
197 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
198 | (generic_bignum[0] & LITTLENUM_MASK);
199 small = 1;
200 }
201 else
202 {
203 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
204 }
205 }
206 if (small)
207 {
208 /*
209 * here with number, in correct radix. c is the next char.
210 * note that unlike un*x, we allow "011f" "0x9f" to
211 * both mean the same as the (conventional) "9f". this is simply easier
212 * than checking for strict canonical form. syntax sux!
213 */
214
215 switch (c)
216 {
217
218 #ifdef LOCAL_LABELS_FB
219 case 'b':
220 {
221 /*
222 * backward ref to local label.
223 * because it is backward, expect it to be defined.
224 */
225 /* Construct a local label. */
226 name = fb_label_name ((int) number, 0);
227
228 /* seen before, or symbol is defined: ok */
229 symbolP = symbol_find (name);
230 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
231 {
232
233 /* local labels are never absolute. don't waste time
234 checking absoluteness. */
235 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
236
237 expressionP->X_add_symbol = symbolP;
238 expressionP->X_seg = S_GET_SEGMENT (symbolP);
239
240 }
241 else
242 { /* either not seen or not defined. */
243 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.", number);
244 expressionP->X_seg = absolute_section;
245 }
246
247 expressionP->X_add_number = 0;
248 break;
249 } /* case 'b' */
250
251 case 'f':
252 {
253 /*
254 * forward reference. expect symbol to be undefined or
255 * unknown. undefined: seen it before. unknown: never seen
256 * it before.
257 * construct a local label name, then an undefined symbol.
258 * don't create a xseg frag for it: caller may do that.
259 * just return it as never seen before.
260 */
261 name = fb_label_name ((int) number, 1);
262 symbolP = symbol_find_or_make (name);
263 /* we have no need to check symbol properties. */
264 #ifndef many_segments
265 /* since "know" puts its arg into a "string", we
266 can't have newlines in the argument. */
267 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
268 #endif
269 expressionP->X_add_symbol = symbolP;
270 expressionP->X_seg = undefined_section;
271 expressionP->X_subtract_symbol = NULL;
272 expressionP->X_add_number = 0;
273
274 break;
275 } /* case 'f' */
276
277 #endif /* LOCAL_LABELS_FB */
278
279 #ifdef LOCAL_LABELS_DOLLAR
280
281 case '$':
282 {
283
284 /* If the dollar label is *currently* defined, then this is just
285 another reference to it. If it is not *currently* defined,
286 then this is a fresh instantiation of that number, so create
287 it. */
288
289 if (dollar_label_defined (number))
290 {
291 name = dollar_label_name (number, 0);
292 symbolP = symbol_find (name);
293 know (symbolP != NULL);
294 }
295 else
296 {
297 name = dollar_label_name (number, 1);
298 symbolP = symbol_find_or_make (name);
299 }
300
301 expressionP->X_add_symbol = symbolP;
302 expressionP->X_add_number = 0;
303 expressionP->X_seg = S_GET_SEGMENT (symbolP);
304
305 break;
306 } /* case '$' */
307
308 #endif /* LOCAL_LABELS_DOLLAR */
309
310 default:
311 {
312 expressionP->X_add_number = number;
313 expressionP->X_seg = absolute_section;
314 input_line_pointer--; /* restore following character. */
315 break;
316 } /* really just a number */
317
318 } /* switch on char following the number */
319
320
321 }
322 else
323 { /* not a small number */
324 expressionP->X_add_number = number;
325 expressionP->X_seg = big_section;
326 input_line_pointer--; /*->char following number. */
327 } /* if (small) */
328 } /* integer_constant() */
329
330
331 /*
332 * Summary of operand().
333 *
334 * in: Input_line_pointer points to 1st char of operand, which may
335 * be a space.
336 *
337 * out: A expressionS. X_seg determines how to understand the rest of the
338 * expressionS.
339 * The operand may have been empty: in this case X_seg == SEG_ABSENT.
340 * Input_line_pointer->(next non-blank) char after operand.
341 *
342 */
343 \f
344
345
346 static segT
347 operand (expressionP)
348 register expressionS *expressionP;
349 {
350 register char c;
351 register symbolS *symbolP; /* points to symbol */
352 register char *name; /* points to name of symbol */
353 /* invented for humans only, hope */
354 /* optimising compiler flushes it! */
355 register short int radix; /* 2, 8, 10 or 16, 0 when floating */
356 /* 0 means we saw start of a floating- */
357 /* point constant. */
358
359 /* digits, assume it is a bignum. */
360
361 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
362 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
363
364 switch (c)
365 {
366 #ifdef MRI
367 case '%':
368 integer_constant (2, expressionP);
369 break;
370 case '@':
371 integer_constant (8, expressionP);
372 break;
373 case '$':
374 integer_constant (16, expressionP);
375 break;
376 #endif
377 case '1':
378 case '2':
379 case '3':
380 case '4':
381 case '5':
382 case '6':
383 case '7':
384 case '8':
385 case '9':
386 input_line_pointer--;
387
388 integer_constant (10, expressionP);
389 break;
390
391 case '0':
392 /* non-decimal radix */
393
394
395 c = *input_line_pointer;
396 switch (c)
397 {
398
399 default:
400 if (c && strchr (FLT_CHARS, c))
401 {
402 input_line_pointer++;
403 floating_constant (expressionP);
404 }
405 else
406 {
407 /* The string was only zero */
408 expressionP->X_add_symbol = 0;
409 expressionP->X_add_number = 0;
410 expressionP->X_seg = absolute_section;
411 }
412
413 break;
414
415 case 'x':
416 case 'X':
417 input_line_pointer++;
418 integer_constant (16, expressionP);
419 break;
420
421 case 'b':
422 #ifdef LOCAL_LABELS_FB
423 if (!*input_line_pointer
424 || (!strchr ("+-.0123456789", *input_line_pointer)
425 && !strchr (EXP_CHARS, *input_line_pointer)))
426 {
427 input_line_pointer--;
428 integer_constant (10, expressionP);
429 break;
430 }
431 #endif
432 case 'B':
433 input_line_pointer++;
434 integer_constant (2, expressionP);
435 break;
436
437 case '0':
438 case '1':
439 case '2':
440 case '3':
441 case '4':
442 case '5':
443 case '6':
444 case '7':
445 integer_constant (8, expressionP);
446 break;
447
448 case 'f':
449 #ifdef LOCAL_LABELS_FB
450 /* if it says '0f' and the line ends or it doesn't look like
451 a floating point #, its a local label ref. dtrt */
452 /* likewise for the b's. xoxorich. */
453 if (c == 'f'
454 && (!*input_line_pointer ||
455 (!strchr ("+-.0123456789", *input_line_pointer) &&
456 !strchr (EXP_CHARS, *input_line_pointer))))
457 {
458 input_line_pointer -= 1;
459 integer_constant (10, expressionP);
460 break;
461 }
462 #endif
463
464 case 'd':
465 case 'D':
466 case 'F':
467 case 'r':
468 case 'e':
469 case 'E':
470 case 'g':
471 case 'G':
472
473 input_line_pointer++;
474 floating_constant (expressionP);
475 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
476 break;
477
478 #ifdef LOCAL_LABELS_DOLLAR
479 case '$':
480 integer_constant (10, expressionP);
481 break;
482 #endif
483 }
484
485 break;
486 case '(':
487 /* didn't begin with digit & not a name */
488 {
489 (void) expression (expressionP);
490 /* Expression() will pass trailing whitespace */
491 if (*input_line_pointer++ != ')')
492 {
493 as_bad ("Missing ')' assumed");
494 input_line_pointer--;
495 }
496 /* here with input_line_pointer->char after "(...)" */
497 }
498 return expressionP->X_seg;
499
500
501 case '\'':
502 /* Warning: to conform to other people's assemblers NO ESCAPEMENT is
503 permitted for a single quote. The next character, parity errors and
504 all, is taken as the value of the operand. VERY KINKY. */
505 expressionP->X_add_number = *input_line_pointer++;
506 expressionP->X_seg = absolute_section;
507 break;
508
509 case '~':
510 case '-':
511 case '+':
512 {
513 /* unary operator: hope for SEG_ABSOLUTE */
514 segT opseg = operand (expressionP);
515 if (opseg == absolute_section)
516 {
517 /* input_line_pointer -> char after operand */
518 if (c == '-')
519 {
520 expressionP->X_add_number = -expressionP->X_add_number;
521 /* Notice: '-' may overflow: no warning is given. This is
522 compatible with other people's assemblers. Sigh. */
523 }
524 else
525 {
526 expressionP->X_add_number = ~expressionP->X_add_number;
527 }
528 }
529 else if (opseg == text_section
530 || opseg == data_section
531 || opseg == bss_section
532 || opseg == pass1_section
533 || opseg == undefined_section)
534 {
535 if (c == '-')
536 {
537 expressionP->X_subtract_symbol = expressionP->X_add_symbol;
538 expressionP->X_add_symbol = 0;
539 expressionP->X_seg = diff_section;
540 }
541 else
542 as_warn ("Unary operator %c ignored because bad operand follows",
543 c);
544 }
545 else
546 as_warn ("Unary operator %c ignored because bad operand follows", c);
547 }
548 break;
549
550 case '.':
551 if (!is_part_of_name (*input_line_pointer))
552 {
553 char *fake;
554 extern struct obstack frags;
555
556 /* JF: '.' is pseudo symbol with value of current location
557 in current segment. */
558 #ifdef DOT_LABEL_PREFIX
559 fake = ".L0\001";
560 #else
561 fake = "L0\001";
562 #endif
563 symbolP = symbol_new (fake,
564 now_seg,
565 (valueT) (obstack_next_free (&frags) - frag_now->fr_literal),
566 frag_now);
567
568 expressionP->X_add_number = 0;
569 expressionP->X_add_symbol = symbolP;
570 expressionP->X_seg = now_seg;
571 break;
572
573 }
574 else
575 {
576 goto isname;
577
578
579 }
580 case ',':
581 case '\n':
582 case '\0':
583 eol:
584 /* can't imagine any other kind of operand */
585 expressionP->X_seg = absent_section;
586 input_line_pointer--;
587 md_operand (expressionP);
588 break;
589
590 default:
591 if (is_end_of_line[c])
592 goto eol;
593 if (is_name_beginner (c)) /* here if did not begin with a digit */
594 {
595 /*
596 * Identifier begins here.
597 * This is kludged for speed, so code is repeated.
598 */
599 isname:
600 name = --input_line_pointer;
601 c = get_symbol_end ();
602 symbolP = symbol_find_or_make (name);
603 /* If we have an absolute symbol or a reg, then we know its value
604 now. */
605 expressionP->X_seg = S_GET_SEGMENT (symbolP);
606 if (expressionP->X_seg == absolute_section
607 || expressionP->X_seg == reg_section)
608 expressionP->X_add_number = S_GET_VALUE (symbolP);
609 else
610 {
611 expressionP->X_add_number = 0;
612 expressionP->X_add_symbol = symbolP;
613 }
614 *input_line_pointer = c;
615 expressionP->X_subtract_symbol = NULL;
616 }
617 else
618 {
619 as_bad ("Bad expression");
620 expressionP->X_add_number = 0;
621 expressionP->X_seg = absolute_section;
622 }
623 }
624
625 /*
626 * It is more 'efficient' to clean up the expressionS when they are created.
627 * Doing it here saves lines of code.
628 */
629 clean_up_expression (expressionP);
630 SKIP_WHITESPACE (); /*->1st char after operand. */
631 know (*input_line_pointer != ' ');
632 return (expressionP->X_seg);
633 } /* operand() */
634 \f
635
636 /* Internal. Simplify a struct expression for use by expr() */
637
638 /*
639 * In: address of a expressionS.
640 * The X_seg field of the expressionS may only take certain values.
641 * Now, we permit SEG_PASS1 to make code smaller & faster.
642 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
643 * Out: expressionS may have been modified:
644 * 'foo-foo' symbol references cancelled to 0,
645 * which changes X_seg from SEG_DIFFERENCE to SEG_ABSOLUTE;
646 * Unused fields zeroed to help expr().
647 */
648
649 static void
650 clean_up_expression (expressionP)
651 register expressionS *expressionP;
652 {
653 segT s = expressionP->X_seg;
654 if (s == absent_section
655 || s == pass1_section)
656 {
657 expressionP->X_add_symbol = NULL;
658 expressionP->X_subtract_symbol = NULL;
659 expressionP->X_add_number = 0;
660 }
661 else if (s == big_section
662 || s == absolute_section)
663 {
664 expressionP->X_subtract_symbol = NULL;
665 expressionP->X_add_symbol = NULL;
666 }
667 else if (s == undefined_section)
668 expressionP->X_subtract_symbol = NULL;
669 else if (s == diff_section)
670 {
671 /*
672 * It does not hurt to 'cancel' NULL==NULL
673 * when comparing symbols for 'eq'ness.
674 * It is faster to re-cancel them to NULL
675 * than to check for this special case.
676 */
677 if (expressionP->X_subtract_symbol == expressionP->X_add_symbol
678 || (expressionP->X_subtract_symbol
679 && expressionP->X_add_symbol
680 && expressionP->X_subtract_symbol->sy_frag == expressionP->X_add_symbol->sy_frag
681 && S_GET_VALUE (expressionP->X_subtract_symbol) == S_GET_VALUE (expressionP->X_add_symbol)))
682 {
683 expressionP->X_subtract_symbol = NULL;
684 expressionP->X_add_symbol = NULL;
685 expressionP->X_seg = absolute_section;
686 }
687 }
688 else if (s == reg_section)
689 {
690 expressionP->X_add_symbol = NULL;
691 expressionP->X_subtract_symbol = NULL;
692 }
693 else
694 {
695 if (SEG_NORMAL (expressionP->X_seg))
696 {
697 expressionP->X_subtract_symbol = NULL;
698 }
699 else
700 {
701 BAD_CASE (expressionP->X_seg);
702 }
703 }
704 }
705 \f
706 /*
707 * expr_part ()
708 *
709 * Internal. Made a function because this code is used in 2 places.
710 * Generate error or correct X_?????_symbol of expressionS.
711 */
712
713 /*
714 * symbol_1 += symbol_2 ... well ... sort of.
715 */
716
717 static segT
718 expr_part (symbol_1_PP, symbol_2_P)
719 symbolS **symbol_1_PP;
720 symbolS *symbol_2_P;
721 {
722 segT return_value;
723 #ifndef MANY_SEGMENTS
724 assert ((*symbol_1_PP) == NULL \
725 || (S_GET_SEGMENT (*symbol_1_PP) == text_section) \
726 || (S_GET_SEGMENT (*symbol_1_PP) == data_section) \
727 || (S_GET_SEGMENT (*symbol_1_PP) == bss_section) \
728 || (!S_IS_DEFINED (*symbol_1_PP)));
729 assert (symbol_2_P == NULL \
730 || (S_GET_SEGMENT (symbol_2_P) == text_section) \
731 || (S_GET_SEGMENT (symbol_2_P) == data_section) \
732 || (S_GET_SEGMENT (symbol_2_P) == bss_section) \
733 || (!S_IS_DEFINED (symbol_2_P)));
734 #endif
735 if (*symbol_1_PP)
736 {
737 if (!S_IS_DEFINED (*symbol_1_PP))
738 {
739 if (symbol_2_P)
740 {
741 return_value = pass1_section;
742 *symbol_1_PP = NULL;
743 }
744 else
745 {
746 know (!S_IS_DEFINED (*symbol_1_PP));
747 return_value = undefined_section;
748 }
749 }
750 else
751 {
752 if (symbol_2_P)
753 {
754 if (!S_IS_DEFINED (symbol_2_P))
755 {
756 *symbol_1_PP = NULL;
757 return_value = pass1_section;
758 }
759 else
760 {
761 /* {seg1} - {seg2} */
762 as_bad ("Expression too complex, 2 symbolS forgotten: \"%s\" \"%s\"",
763 S_GET_NAME (*symbol_1_PP), S_GET_NAME (symbol_2_P));
764 *symbol_1_PP = NULL;
765 return_value = absolute_section;
766 }
767 }
768 else
769 {
770 return_value = S_GET_SEGMENT (*symbol_1_PP);
771 }
772 }
773 }
774 else
775 { /* (* symbol_1_PP) == NULL */
776 if (symbol_2_P)
777 {
778 *symbol_1_PP = symbol_2_P;
779 return_value = S_GET_SEGMENT (symbol_2_P);
780 }
781 else
782 {
783 *symbol_1_PP = NULL;
784 return_value = absolute_section;
785 }
786 }
787 #ifndef MANY_SEGMENTS
788 assert (return_value == absolute_section \
789 || return_value == text_section \
790 || return_value == data_section \
791 || return_value == bss_section \
792 || return_value == undefined_section \
793 || return_value == pass1_section);
794 #endif
795 know ((*symbol_1_PP) == NULL
796 || (S_GET_SEGMENT (*symbol_1_PP) == return_value));
797 return (return_value);
798 }
799 \f
800 /* Expression parser. */
801
802 /*
803 * We allow an empty expression, and just assume (absolute,0) silently.
804 * Unary operators and parenthetical expressions are treated as operands.
805 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
806 *
807 * We used to do a aho/ullman shift-reduce parser, but the logic got so
808 * warped that I flushed it and wrote a recursive-descent parser instead.
809 * Now things are stable, would anybody like to write a fast parser?
810 * Most expressions are either register (which does not even reach here)
811 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
812 * So I guess it doesn't really matter how inefficient more complex expressions
813 * are parsed.
814 *
815 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
816 * Also, we have consumed any leading or trailing spaces (operand does that)
817 * and done all intervening operators.
818 */
819
820 typedef enum
821 {
822 O_illegal, /* (0) what we get for illegal op */
823
824 O_multiply, /* (1) * */
825 O_divide, /* (2) / */
826 O_modulus, /* (3) % */
827 O_left_shift, /* (4) < */
828 O_right_shift, /* (5) > */
829 O_bit_inclusive_or, /* (6) | */
830 O_bit_or_not, /* (7) ! */
831 O_bit_exclusive_or, /* (8) ^ */
832 O_bit_and, /* (9) & */
833 O_add, /* (10) + */
834 O_subtract /* (11) - */
835 }
836
837 operatorT;
838
839 #define __ O_illegal
840
841 static const operatorT op_encoding[256] =
842 { /* maps ASCII->operators */
843
844 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
845 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
846
847 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
848 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
849 __, __, __, __, __, __, __, __,
850 __, __, __, __, O_left_shift, __, O_right_shift, __,
851 __, __, __, __, __, __, __, __,
852 __, __, __, __, __, __, __, __,
853 __, __, __, __, __, __, __, __,
854 __, __, __, __, __, __, O_bit_exclusive_or, __,
855 __, __, __, __, __, __, __, __,
856 __, __, __, __, __, __, __, __,
857 __, __, __, __, __, __, __, __,
858 __, __, __, __, O_bit_inclusive_or, __, __, __,
859
860 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
861 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
862 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
863 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
864 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
865 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
866 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
867 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
868 };
869
870
871 /*
872 * Rank Examples
873 * 0 operand, (expression)
874 * 1 + -
875 * 2 & ^ ! |
876 * 3 * / % << >>
877 */
878 static const operator_rankT
879 op_rank[] =
880 {0, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1};
881 \f
882 /* Return resultP->X_seg. */
883 segT
884 expr (rank, resultP)
885 register operator_rankT rank; /* Larger # is higher rank. */
886 register expressionS *resultP; /* Deliver result here. */
887 {
888 expressionS right;
889 register operatorT op_left;
890 register char c_left; /* 1st operator character. */
891 register operatorT op_right;
892 register char c_right;
893
894 know (rank >= 0);
895 (void) operand (resultP);
896 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
897 c_left = *input_line_pointer; /* Potential operator character. */
898 op_left = op_encoding[c_left];
899 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
900 {
901 input_line_pointer++; /*->after 1st character of operator. */
902 /* Operators "<<" and ">>" have 2 characters. */
903 if (*input_line_pointer == c_left && (c_left == '<' || c_left == '>'))
904 {
905 input_line_pointer++;
906 } /*->after operator. */
907 if (absent_section == expr (op_rank[(int) op_left], &right))
908 {
909 as_warn ("Missing operand value assumed absolute 0.");
910 resultP->X_add_number = 0;
911 resultP->X_subtract_symbol = NULL;
912 resultP->X_add_symbol = NULL;
913 resultP->X_seg = absolute_section;
914 }
915 know (*input_line_pointer != ' ');
916 c_right = *input_line_pointer;
917 op_right = op_encoding[c_right];
918 if (*input_line_pointer == c_right && (c_right == '<' || c_right == '>'))
919 {
920 input_line_pointer++;
921 } /*->after operator. */
922 know ((int) op_right == 0 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
923 /* input_line_pointer->after right-hand quantity. */
924 /* left-hand quantity in resultP */
925 /* right-hand quantity in right. */
926 /* operator in op_left. */
927 if (resultP->X_seg == pass1_section || right.X_seg == pass1_section)
928 {
929 resultP->X_seg = pass1_section;
930 }
931 else
932 {
933 if (resultP->X_seg == big_section)
934 {
935 as_warn ("Left operand of %c is a %s. Integer 0 assumed.",
936 c_left, resultP->X_add_number > 0 ? "bignum" : "float");
937 resultP->X_seg = absolute_section;
938 resultP->X_add_symbol = 0;
939 resultP->X_subtract_symbol = 0;
940 resultP->X_add_number = 0;
941 }
942 if (right.X_seg == big_section)
943 {
944 as_warn ("Right operand of %c is a %s. Integer 0 assumed.",
945 c_left, right.X_add_number > 0 ? "bignum" : "float");
946 right.X_seg = absolute_section;
947 right.X_add_symbol = 0;
948 right.X_subtract_symbol = 0;
949 right.X_add_number = 0;
950 }
951 if (op_left == O_subtract)
952 {
953 /*
954 * Convert - into + by exchanging symbolS and negating number.
955 * I know -infinity can't be negated in 2's complement:
956 * but then it can't be subtracted either. This trick
957 * does not cause any further inaccuracy.
958 */
959
960 register symbolS *symbolP;
961
962 right.X_add_number = -right.X_add_number;
963 symbolP = right.X_add_symbol;
964 right.X_add_symbol = right.X_subtract_symbol;
965 right.X_subtract_symbol = symbolP;
966 if (symbolP)
967 {
968 right.X_seg = diff_section;
969 }
970 op_left = O_add;
971 }
972 \f
973 if (op_left == O_add)
974 {
975 segT seg1;
976 segT seg2;
977 #ifndef MANY_SEGMENTS
978
979 know (resultP->X_seg == data_section || resultP->X_seg == text_section || resultP->X_seg == bss_section || resultP->X_seg == undefined_section || resultP->X_seg == diff_section || resultP->X_seg == absolute_section || resultP->X_seg == pass1_section || resultP->X_seg == reg_section);
980
981 know (right.X_seg == data_section || right.X_seg == text_section || right.X_seg == bss_section || right.X_seg == undefined_section || right.X_seg == diff_section || right.X_seg == absolute_section || right.X_seg == pass1_section);
982 #endif
983 clean_up_expression (&right);
984 clean_up_expression (resultP);
985
986 seg1 = expr_part (&resultP->X_add_symbol, right.X_add_symbol);
987 seg2 = expr_part (&resultP->X_subtract_symbol, right.X_subtract_symbol);
988 if (seg1 == pass1_section || seg2 == pass1_section)
989 {
990 need_pass_2 = 1;
991 resultP->X_seg = pass1_section;
992 }
993 else if (seg2 == absolute_section)
994 resultP->X_seg = seg1;
995 else if (seg1 != undefined_section
996 && seg1 != absolute_section
997 && seg2 != undefined_section
998 && seg1 != seg2)
999 {
1000 know (seg2 != absolute_section);
1001 know (resultP->X_subtract_symbol);
1002 #ifndef MANY_SEGMENTS
1003 know (seg1 == text_section || seg1 == data_section || seg1 == bss_section);
1004 know (seg2 == text_section || seg2 == data_section || seg2 == bss_section);
1005 #endif
1006 know (resultP->X_add_symbol);
1007 know (resultP->X_subtract_symbol);
1008 as_bad ("Expression too complex: forgetting %s - %s",
1009 S_GET_NAME (resultP->X_add_symbol),
1010 S_GET_NAME (resultP->X_subtract_symbol));
1011 resultP->X_seg = absolute_section;
1012 /* Clean_up_expression() will do the rest. */
1013 }
1014 else
1015 resultP->X_seg = diff_section;
1016
1017 resultP->X_add_number += right.X_add_number;
1018 clean_up_expression (resultP);
1019 }
1020 else
1021 { /* Not +. */
1022 if (resultP->X_seg == undefined_section || right.X_seg == undefined_section)
1023 {
1024 resultP->X_seg = pass1_section;
1025 need_pass_2 = 1;
1026 }
1027 else
1028 {
1029 resultP->X_subtract_symbol = NULL;
1030 resultP->X_add_symbol = NULL;
1031 /* Will be absolute_section. */
1032 if (resultP->X_seg != absolute_section || right.X_seg != absolute_section)
1033 {
1034 as_bad ("Relocation error. Absolute 0 assumed.");
1035 resultP->X_seg = absolute_section;
1036 resultP->X_add_number = 0;
1037 }
1038 else
1039 {
1040 switch (op_left)
1041 {
1042 case O_bit_inclusive_or:
1043 resultP->X_add_number |= right.X_add_number;
1044 break;
1045
1046 case O_modulus:
1047 if (right.X_add_number)
1048 {
1049 resultP->X_add_number %= right.X_add_number;
1050 }
1051 else
1052 {
1053 as_warn ("Division by 0. 0 assumed.");
1054 resultP->X_add_number = 0;
1055 }
1056 break;
1057
1058 case O_bit_and:
1059 resultP->X_add_number &= right.X_add_number;
1060 break;
1061
1062 case O_multiply:
1063 resultP->X_add_number *= right.X_add_number;
1064 break;
1065
1066 case O_divide:
1067 if (right.X_add_number)
1068 {
1069 resultP->X_add_number /= right.X_add_number;
1070 }
1071 else
1072 {
1073 as_warn ("Division by 0. 0 assumed.");
1074 resultP->X_add_number = 0;
1075 }
1076 break;
1077
1078 case O_left_shift:
1079 resultP->X_add_number <<= right.X_add_number;
1080 break;
1081
1082 case O_right_shift:
1083 resultP->X_add_number >>= right.X_add_number;
1084 break;
1085
1086 case O_bit_exclusive_or:
1087 resultP->X_add_number ^= right.X_add_number;
1088 break;
1089
1090 case O_bit_or_not:
1091 resultP->X_add_number |= ~right.X_add_number;
1092 break;
1093
1094 default:
1095 BAD_CASE (op_left);
1096 break;
1097 } /* switch(operator) */
1098 }
1099 } /* If we have to force need_pass_2. */
1100 } /* If operator was +. */
1101 } /* If we didn't set need_pass_2. */
1102 op_left = op_right;
1103 } /* While next operator is >= this rank. */
1104 return (resultP->X_seg);
1105 }
1106 \f
1107 /*
1108 * get_symbol_end()
1109 *
1110 * This lives here because it belongs equally in expr.c & read.c.
1111 * Expr.c is just a branch office read.c anyway, and putting it
1112 * here lessens the crowd at read.c.
1113 *
1114 * Assume input_line_pointer is at start of symbol name.
1115 * Advance input_line_pointer past symbol name.
1116 * Turn that character into a '\0', returning its former value.
1117 * This allows a string compare (RMS wants symbol names to be strings)
1118 * of the symbol name.
1119 * There will always be a char following symbol name, because all good
1120 * lines end in end-of-line.
1121 */
1122 char
1123 get_symbol_end ()
1124 {
1125 register char c;
1126
1127 while (is_part_of_name (c = *input_line_pointer++))
1128 ;
1129 *--input_line_pointer = 0;
1130 return (c);
1131 }
1132
1133
1134 unsigned int
1135 get_single_number ()
1136 {
1137 expressionS exp;
1138 operand (&exp);
1139 return exp.X_add_number;
1140
1141 }
1142
1143 /* end of expr.c */
This page took 0.051634 seconds and 4 git commands to generate.