* hppa-tdep.c (frame_saved_pc): Don't try to dig a return pointer
[deliverable/binutils-gdb.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #include "libiberty.h"
32 #include "obstack.h"
33
34 static void floating_constant PARAMS ((expressionS * expressionP));
35 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
36 static void mri_char_constant PARAMS ((expressionS *));
37 static void clean_up_expression PARAMS ((expressionS * expressionP));
38
39 extern const char EXP_CHARS[], FLT_CHARS[];
40 \f
41 /* Build a dummy symbol to hold a complex expression. This is how we
42 build expressions up out of other expressions. The symbol is put
43 into the fake section expr_section. */
44
45 symbolS *
46 make_expr_symbol (expressionP)
47 expressionS *expressionP;
48 {
49 const char *fake;
50 symbolS *symbolP;
51
52 if (expressionP->X_op == O_symbol
53 && expressionP->X_add_number == 0)
54 return expressionP->X_add_symbol;
55
56 /* FIXME: This should be something which decode_local_label_name
57 will handle. */
58 fake = FAKE_LABEL_NAME;
59
60 /* Putting constant symbols in absolute_section rather than
61 expr_section is convenient for the old a.out code, for which
62 S_GET_SEGMENT does not always retrieve the value put in by
63 S_SET_SEGMENT. */
64 symbolP = symbol_create (fake,
65 (expressionP->X_op == O_constant
66 ? absolute_section
67 : expr_section),
68 0, &zero_address_frag);
69 symbolP->sy_value = *expressionP;
70
71 if (expressionP->X_op == O_constant)
72 resolve_symbol_value (symbolP);
73
74 return symbolP;
75 }
76 \f
77 /*
78 * Build any floating-point literal here.
79 * Also build any bignum literal here.
80 */
81
82 /* Seems atof_machine can backscan through generic_bignum and hit whatever
83 happens to be loaded before it in memory. And its way too complicated
84 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
85 and never write into the early words, thus they'll always be zero.
86 I hate Dean's floating-point code. Bleh. */
87 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
88 FLONUM_TYPE generic_floating_point_number =
89 {
90 &generic_bignum[6], /* low (JF: Was 0) */
91 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
92 0, /* leader */
93 0, /* exponent */
94 0 /* sign */
95 };
96 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
97 int generic_floating_point_magic;
98 \f
99 static void
100 floating_constant (expressionP)
101 expressionS *expressionP;
102 {
103 /* input_line_pointer->*/
104 /* floating-point constant. */
105 int error_code;
106
107 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
108 &generic_floating_point_number);
109
110 if (error_code)
111 {
112 if (error_code == ERROR_EXPONENT_OVERFLOW)
113 {
114 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
115 }
116 else
117 {
118 as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
119 }
120 }
121 expressionP->X_op = O_big;
122 /* input_line_pointer->just after constant, */
123 /* which may point to whitespace. */
124 expressionP->X_add_number = -1;
125 }
126
127 static void
128 integer_constant (radix, expressionP)
129 int radix;
130 expressionS *expressionP;
131 {
132 char *start; /* start of number. */
133 char *suffix = NULL;
134 char c;
135 valueT number; /* offset or (absolute) value */
136 short int digit; /* value of next digit in current radix */
137 short int maxdig = 0;/* highest permitted digit value. */
138 int too_many_digits = 0; /* if we see >= this number of */
139 char *name; /* points to name of symbol */
140 symbolS *symbolP; /* points to symbol */
141
142 int small; /* true if fits in 32 bits. */
143
144 /* May be bignum, or may fit in 32 bits. */
145 /* Most numbers fit into 32 bits, and we want this case to be fast.
146 so we pretend it will fit into 32 bits. If, after making up a 32
147 bit number, we realise that we have scanned more digits than
148 comfortably fit into 32 bits, we re-scan the digits coding them
149 into a bignum. For decimal and octal numbers we are
150 conservative: Some numbers may be assumed bignums when in fact
151 they do fit into 32 bits. Numbers of any radix can have excess
152 leading zeros: We strive to recognise this and cast them back
153 into 32 bits. We must check that the bignum really is more than
154 32 bits, and change it back to a 32-bit number if it fits. The
155 number we are looking for is expected to be positive, but if it
156 fits into 32 bits as an unsigned number, we let it be a 32-bit
157 number. The cavalier approach is for speed in ordinary cases. */
158 /* This has been extended for 64 bits. We blindly assume that if
159 you're compiling in 64-bit mode, the target is a 64-bit machine.
160 This should be cleaned up. */
161
162 #ifdef BFD64
163 #define valuesize 64
164 #else /* includes non-bfd case, mostly */
165 #define valuesize 32
166 #endif
167
168 if (flag_mri && radix == 0)
169 {
170 int flt = 0;
171
172 /* In MRI mode, the number may have a suffix indicating the
173 radix. For that matter, it might actually be a floating
174 point constant. */
175 for (suffix = input_line_pointer; isalnum (*suffix); suffix++)
176 {
177 if (*suffix == 'e' || *suffix == 'E')
178 flt = 1;
179 }
180
181 if (suffix == input_line_pointer)
182 {
183 radix = 10;
184 suffix = NULL;
185 }
186 else
187 {
188 c = *--suffix;
189 if (islower (c))
190 c = toupper (c);
191 if (c == 'B')
192 radix = 2;
193 else if (c == 'D')
194 radix = 10;
195 else if (c == 'O' || c == 'Q')
196 radix = 8;
197 else if (c == 'H')
198 radix = 16;
199 else if (suffix[1] == '.' || c == 'E' || flt)
200 {
201 floating_constant (expressionP);
202 return;
203 }
204 else
205 {
206 radix = 10;
207 suffix = NULL;
208 }
209 }
210 }
211
212 switch (radix)
213 {
214 case 2:
215 maxdig = 2;
216 too_many_digits = valuesize + 1;
217 break;
218 case 8:
219 maxdig = radix = 8;
220 too_many_digits = (valuesize + 2) / 3 + 1;
221 break;
222 case 16:
223 maxdig = radix = 16;
224 too_many_digits = (valuesize + 3) / 4 + 1;
225 break;
226 case 10:
227 maxdig = radix = 10;
228 too_many_digits = (valuesize + 12) / 4; /* very rough */
229 }
230 #undef valuesize
231 start = input_line_pointer;
232 c = *input_line_pointer++;
233 for (number = 0;
234 (digit = hex_value (c)) < maxdig;
235 c = *input_line_pointer++)
236 {
237 number = number * radix + digit;
238 }
239 /* c contains character after number. */
240 /* input_line_pointer->char after c. */
241 small = (input_line_pointer - start - 1) < too_many_digits;
242 if (!small)
243 {
244 /*
245 * we saw a lot of digits. manufacture a bignum the hard way.
246 */
247 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
248 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
249 long carry;
250
251 leader = generic_bignum;
252 generic_bignum[0] = 0;
253 generic_bignum[1] = 0;
254 input_line_pointer = start; /*->1st digit. */
255 c = *input_line_pointer++;
256 for (;
257 (carry = hex_value (c)) < maxdig;
258 c = *input_line_pointer++)
259 {
260 for (pointer = generic_bignum;
261 pointer <= leader;
262 pointer++)
263 {
264 long work;
265
266 work = carry + radix * *pointer;
267 *pointer = work & LITTLENUM_MASK;
268 carry = work >> LITTLENUM_NUMBER_OF_BITS;
269 }
270 if (carry)
271 {
272 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
273 {
274 /* room to grow a longer bignum. */
275 *++leader = carry;
276 }
277 }
278 }
279 /* again, c is char after number, */
280 /* input_line_pointer->after c. */
281 know (LITTLENUM_NUMBER_OF_BITS == 16);
282 if (leader < generic_bignum + 2)
283 {
284 /* will fit into 32 bits. */
285 number =
286 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
287 | (generic_bignum[0] & LITTLENUM_MASK);
288 small = 1;
289 }
290 else
291 {
292 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
293 }
294 }
295
296 if (flag_mri && suffix != NULL && input_line_pointer - 1 == suffix)
297 c = *input_line_pointer++;
298
299 if (small)
300 {
301 /*
302 * here with number, in correct radix. c is the next char.
303 * note that unlike un*x, we allow "011f" "0x9f" to
304 * both mean the same as the (conventional) "9f". this is simply easier
305 * than checking for strict canonical form. syntax sux!
306 */
307
308 if (LOCAL_LABELS_FB && c == 'b')
309 {
310 /*
311 * backward ref to local label.
312 * because it is backward, expect it to be defined.
313 */
314 /* Construct a local label. */
315 name = fb_label_name ((int) number, 0);
316
317 /* seen before, or symbol is defined: ok */
318 symbolP = symbol_find (name);
319 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
320 {
321 /* local labels are never absolute. don't waste time
322 checking absoluteness. */
323 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
324
325 expressionP->X_op = O_symbol;
326 expressionP->X_add_symbol = symbolP;
327 }
328 else
329 {
330 /* either not seen or not defined. */
331 /* @@ Should print out the original string instead of
332 the parsed number. */
333 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
334 (int) number);
335 expressionP->X_op = O_constant;
336 }
337
338 expressionP->X_add_number = 0;
339 } /* case 'b' */
340 else if (LOCAL_LABELS_FB && c == 'f')
341 {
342 /*
343 * forward reference. expect symbol to be undefined or
344 * unknown. undefined: seen it before. unknown: never seen
345 * it before.
346 * construct a local label name, then an undefined symbol.
347 * don't create a xseg frag for it: caller may do that.
348 * just return it as never seen before.
349 */
350 name = fb_label_name ((int) number, 1);
351 symbolP = symbol_find_or_make (name);
352 /* we have no need to check symbol properties. */
353 #ifndef many_segments
354 /* since "know" puts its arg into a "string", we
355 can't have newlines in the argument. */
356 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
357 #endif
358 expressionP->X_op = O_symbol;
359 expressionP->X_add_symbol = symbolP;
360 expressionP->X_add_number = 0;
361 } /* case 'f' */
362 else if (LOCAL_LABELS_DOLLAR && c == '$')
363 {
364 /* If the dollar label is *currently* defined, then this is just
365 another reference to it. If it is not *currently* defined,
366 then this is a fresh instantiation of that number, so create
367 it. */
368
369 if (dollar_label_defined ((long) number))
370 {
371 name = dollar_label_name ((long) number, 0);
372 symbolP = symbol_find (name);
373 know (symbolP != NULL);
374 }
375 else
376 {
377 name = dollar_label_name ((long) number, 1);
378 symbolP = symbol_find_or_make (name);
379 }
380
381 expressionP->X_op = O_symbol;
382 expressionP->X_add_symbol = symbolP;
383 expressionP->X_add_number = 0;
384 } /* case '$' */
385 else
386 {
387 expressionP->X_op = O_constant;
388 #ifdef TARGET_WORD_SIZE
389 /* Sign extend NUMBER. */
390 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
391 #endif
392 expressionP->X_add_number = number;
393 input_line_pointer--; /* restore following character. */
394 } /* really just a number */
395 }
396 else
397 {
398 /* not a small number */
399 expressionP->X_op = O_big;
400 expressionP->X_add_number = number; /* number of littlenums */
401 input_line_pointer--; /*->char following number. */
402 }
403 }
404
405 /* Parse an MRI multi character constant. */
406
407 static void
408 mri_char_constant (expressionP)
409 expressionS *expressionP;
410 {
411 int i;
412
413 if (*input_line_pointer == '\''
414 && input_line_pointer[1] != '\'')
415 {
416 expressionP->X_op = O_constant;
417 expressionP->X_add_number = 0;
418 return;
419 }
420
421 /* In order to get the correct byte ordering, we must build the
422 number in reverse. */
423 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
424 {
425 int j;
426
427 generic_bignum[i] = 0;
428 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
429 {
430 if (*input_line_pointer == '\'')
431 {
432 if (input_line_pointer[1] != '\'')
433 break;
434 ++input_line_pointer;
435 }
436 generic_bignum[i] <<= 8;
437 generic_bignum[i] += *input_line_pointer;
438 ++input_line_pointer;
439 }
440
441 if (i < SIZE_OF_LARGE_NUMBER - 1)
442 {
443 /* If there is more than one littlenum, left justify the
444 last one to make it match the earlier ones. If there is
445 only one, we can just use the value directly. */
446 for (; j < CHARS_PER_LITTLENUM; j++)
447 generic_bignum[i] <<= 8;
448 }
449
450 if (*input_line_pointer == '\''
451 && input_line_pointer[1] != '\'')
452 break;
453 }
454
455 if (i < 0)
456 {
457 as_bad ("Character constant too large");
458 i = 0;
459 }
460
461 if (i > 0)
462 {
463 int c;
464 int j;
465
466 c = SIZE_OF_LARGE_NUMBER - i;
467 for (j = 0; j < c; j++)
468 generic_bignum[j] = generic_bignum[i + j];
469 i = c;
470 }
471
472 know (LITTLENUM_NUMBER_OF_BITS == 16);
473 if (i > 2)
474 {
475 expressionP->X_op = O_big;
476 expressionP->X_add_number = i;
477 }
478 else
479 {
480 expressionP->X_op = O_constant;
481 if (i < 2)
482 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
483 else
484 expressionP->X_add_number =
485 (((generic_bignum[1] & LITTLENUM_MASK)
486 << LITTLENUM_NUMBER_OF_BITS)
487 | (generic_bignum[0] & LITTLENUM_MASK));
488 }
489
490 /* Skip the final closing quote. */
491 ++input_line_pointer;
492 }
493
494 /*
495 * Summary of operand().
496 *
497 * in: Input_line_pointer points to 1st char of operand, which may
498 * be a space.
499 *
500 * out: A expressionS.
501 * The operand may have been empty: in this case X_op == O_absent.
502 * Input_line_pointer->(next non-blank) char after operand.
503 */
504
505 static segT
506 operand (expressionP)
507 expressionS *expressionP;
508 {
509 char c;
510 symbolS *symbolP; /* points to symbol */
511 char *name; /* points to name of symbol */
512 segT segment;
513
514 /* All integers are regarded as unsigned unless they are negated.
515 This is because the only thing which cares whether a number is
516 unsigned is the code in emit_expr which extends constants into
517 bignums. It should only sign extend negative numbers, so that
518 something like ``.quad 0x80000000'' is not sign extended even
519 though it appears negative if valueT is 32 bits. */
520 expressionP->X_unsigned = 1;
521
522 /* digits, assume it is a bignum. */
523
524 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
525 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
526
527 switch (c)
528 {
529 case '1':
530 case '2':
531 case '3':
532 case '4':
533 case '5':
534 case '6':
535 case '7':
536 case '8':
537 case '9':
538 input_line_pointer--;
539
540 integer_constant (flag_mri ? 0 : 10, expressionP);
541 break;
542
543 case '0':
544 /* non-decimal radix */
545
546 if (flag_mri)
547 {
548 char *s;
549
550 /* Check for a hex constant. */
551 for (s = input_line_pointer; hex_p (*s); s++)
552 ;
553 if (*s == 'h' || *s == 'H')
554 {
555 --input_line_pointer;
556 integer_constant (0, expressionP);
557 break;
558 }
559 }
560
561 c = *input_line_pointer;
562 switch (c)
563 {
564 default:
565 default_case:
566 if (c && strchr (FLT_CHARS, c))
567 {
568 input_line_pointer++;
569 floating_constant (expressionP);
570 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
571 }
572 else
573 {
574 /* The string was only zero */
575 expressionP->X_op = O_constant;
576 expressionP->X_add_number = 0;
577 }
578
579 break;
580
581 case 'x':
582 case 'X':
583 if (flag_mri)
584 goto default_case;
585 input_line_pointer++;
586 integer_constant (16, expressionP);
587 break;
588
589 case 'b':
590 if (LOCAL_LABELS_FB)
591 {
592 switch (input_line_pointer[1])
593 {
594 case '+':
595 case '-':
596 /* If unambiguously a difference expression, treat
597 it as one by indicating a label; otherwise, it's
598 always a binary number. */
599 {
600 char *cp = input_line_pointer + 1;
601 while (strchr ("0123456789", *++cp))
602 ;
603 if (*cp == 'b' || *cp == 'f')
604 goto is_0b_label;
605 }
606 goto is_0b_binary;
607 case '0': case '1':
608 /* Some of our code elsewhere does permit digits
609 greater than the expected base; for consistency,
610 do the same here. */
611 case '2': case '3': case '4': case '5':
612 case '6': case '7': case '8': case '9':
613 goto is_0b_binary;
614 case 0:
615 goto is_0b_label;
616 default:
617 goto is_0b_label;
618 }
619 is_0b_label:
620 input_line_pointer--;
621 integer_constant (10, expressionP);
622 break;
623 is_0b_binary:
624 ;
625 }
626 case 'B':
627 input_line_pointer++;
628 if (flag_mri)
629 goto default_case;
630 integer_constant (2, expressionP);
631 break;
632
633 case '0':
634 case '1':
635 case '2':
636 case '3':
637 case '4':
638 case '5':
639 case '6':
640 case '7':
641 integer_constant (flag_mri ? 0 : 8, expressionP);
642 break;
643
644 case 'f':
645 if (LOCAL_LABELS_FB)
646 {
647 /* If it says "0f" and it could possibly be a floating point
648 number, make it one. Otherwise, make it a local label,
649 and try to deal with parsing the rest later. */
650 if (!input_line_pointer[1]
651 || (is_end_of_line[0xff & input_line_pointer[1]]))
652 goto is_0f_label;
653 {
654 char *cp = input_line_pointer + 1;
655 int r = atof_generic (&cp, ".", EXP_CHARS,
656 &generic_floating_point_number);
657 switch (r)
658 {
659 case 0:
660 case ERROR_EXPONENT_OVERFLOW:
661 if (*cp == 'f' || *cp == 'b')
662 /* looks like a difference expression */
663 goto is_0f_label;
664 else
665 goto is_0f_float;
666 default:
667 as_fatal ("expr.c(operand): bad atof_generic return val %d",
668 r);
669 }
670 }
671
672 /* Okay, now we've sorted it out. We resume at one of these
673 two labels, depending on what we've decided we're probably
674 looking at. */
675 is_0f_label:
676 input_line_pointer--;
677 integer_constant (10, expressionP);
678 break;
679
680 is_0f_float:
681 /* fall through */
682 ;
683 }
684
685 case 'd':
686 case 'D':
687 case 'F':
688 case 'r':
689 case 'e':
690 case 'E':
691 case 'g':
692 case 'G':
693 input_line_pointer++;
694 floating_constant (expressionP);
695 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
696 break;
697
698 case '$':
699 if (LOCAL_LABELS_DOLLAR)
700 {
701 integer_constant (10, expressionP);
702 break;
703 }
704 else
705 goto default_case;
706 }
707
708 break;
709
710 case '(':
711 case '[':
712 /* didn't begin with digit & not a name */
713 segment = expression (expressionP);
714 /* Expression() will pass trailing whitespace */
715 if ((c == '(' && *input_line_pointer++ != ')')
716 || (c == '[' && *input_line_pointer++ != ']'))
717 {
718 as_bad ("Missing ')' assumed");
719 input_line_pointer--;
720 }
721 /* here with input_line_pointer->char after "(...)" */
722 return segment;
723
724 case 'E':
725 if (! flag_mri || *input_line_pointer != '\'')
726 goto de_fault;
727 as_bad ("EBCDIC constants are not supported");
728 /* Fall through. */
729 case 'A':
730 if (! flag_mri || *input_line_pointer != '\'')
731 goto de_fault;
732 ++input_line_pointer;
733 /* Fall through. */
734 case '\'':
735 if (! flag_mri)
736 {
737 /* Warning: to conform to other people's assemblers NO
738 ESCAPEMENT is permitted for a single quote. The next
739 character, parity errors and all, is taken as the value
740 of the operand. VERY KINKY. */
741 expressionP->X_op = O_constant;
742 expressionP->X_add_number = *input_line_pointer++;
743 break;
744 }
745
746 mri_char_constant (expressionP);
747 break;
748
749 case '+':
750 (void) operand (expressionP);
751 break;
752
753 case '"':
754 /* Double quote is the logical not operator in MRI mode. */
755 if (! flag_mri)
756 goto de_fault;
757 /* Fall through. */
758 case '~':
759 case '-':
760 {
761 operand (expressionP);
762 if (expressionP->X_op == O_constant)
763 {
764 /* input_line_pointer -> char after operand */
765 if (c == '-')
766 {
767 expressionP->X_add_number = - expressionP->X_add_number;
768 /* Notice: '-' may overflow: no warning is given. This is
769 compatible with other people's assemblers. Sigh. */
770 expressionP->X_unsigned = 0;
771 }
772 else
773 expressionP->X_add_number = ~ expressionP->X_add_number;
774 }
775 else if (expressionP->X_op != O_illegal
776 && expressionP->X_op != O_absent)
777 {
778 expressionP->X_add_symbol = make_expr_symbol (expressionP);
779 if (c == '-')
780 expressionP->X_op = O_uminus;
781 else
782 expressionP->X_op = O_bit_not;
783 expressionP->X_add_number = 0;
784 }
785 else
786 as_warn ("Unary operator %c ignored because bad operand follows",
787 c);
788 }
789 break;
790
791 case '$':
792 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
793 is defined. */
794 #ifndef DOLLAR_DOT
795 if (! flag_mri)
796 goto de_fault;
797 #endif
798 if (flag_mri && hex_p (*input_line_pointer))
799 {
800 /* In MRI mode, $ is also used as the prefix for a
801 hexadecimal constant. */
802 integer_constant (16, expressionP);
803 break;
804 }
805 /* Fall through. */
806 case '.':
807 if (!is_part_of_name (*input_line_pointer))
808 {
809 const char *fake;
810
811 /* JF: '.' is pseudo symbol with value of current location
812 in current segment. */
813 fake = FAKE_LABEL_NAME;
814 symbolP = symbol_new (fake,
815 now_seg,
816 (valueT) frag_now_fix (),
817 frag_now);
818
819 expressionP->X_op = O_symbol;
820 expressionP->X_add_symbol = symbolP;
821 expressionP->X_add_number = 0;
822 break;
823 }
824 else
825 {
826 goto isname;
827 }
828 case ',':
829 case '\n':
830 case '\0':
831 eol:
832 /* can't imagine any other kind of operand */
833 expressionP->X_op = O_absent;
834 input_line_pointer--;
835 break;
836
837 case '%':
838 if (! flag_mri)
839 goto de_fault;
840 integer_constant (2, expressionP);
841 break;
842
843 case '@':
844 if (! flag_mri)
845 goto de_fault;
846 integer_constant (8, expressionP);
847 break;
848
849 case ':':
850 if (! flag_mri)
851 goto de_fault;
852
853 /* In MRI mode, this is a floating point constant represented
854 using hexadecimal digits. */
855
856 ++input_line_pointer;
857 integer_constant (16, expressionP);
858 break;
859
860 default:
861 de_fault:
862 if (is_end_of_line[(unsigned char) c])
863 goto eol;
864 if (is_name_beginner (c)) /* here if did not begin with a digit */
865 {
866 /*
867 * Identifier begins here.
868 * This is kludged for speed, so code is repeated.
869 */
870 isname:
871 name = --input_line_pointer;
872 c = get_symbol_end ();
873 symbolP = symbol_find_or_make (name);
874
875 /* If we have an absolute symbol or a reg, then we know its
876 value now. */
877 segment = S_GET_SEGMENT (symbolP);
878 if (segment == absolute_section)
879 {
880 expressionP->X_op = O_constant;
881 expressionP->X_add_number = S_GET_VALUE (symbolP);
882 }
883 else if (segment == reg_section)
884 {
885 expressionP->X_op = O_register;
886 expressionP->X_add_number = S_GET_VALUE (symbolP);
887 }
888 else
889 {
890 expressionP->X_op = O_symbol;
891 expressionP->X_add_symbol = symbolP;
892 expressionP->X_add_number = 0;
893 }
894 *input_line_pointer = c;
895 }
896 else
897 {
898 /* Let the target try to parse it. Success is indicated by changing
899 the X_op field to something other than O_absent and pointing
900 input_line_pointer passed the expression. If it can't parse the
901 expression, X_op and input_line_pointer should be unchanged. */
902 expressionP->X_op = O_absent;
903 --input_line_pointer;
904 md_operand (expressionP);
905 if (expressionP->X_op == O_absent)
906 {
907 ++input_line_pointer;
908 as_bad ("Bad expression");
909 expressionP->X_op = O_constant;
910 expressionP->X_add_number = 0;
911 }
912 }
913 break;
914 }
915
916 /*
917 * It is more 'efficient' to clean up the expressionS when they are created.
918 * Doing it here saves lines of code.
919 */
920 clean_up_expression (expressionP);
921 SKIP_WHITESPACE (); /*->1st char after operand. */
922 know (*input_line_pointer != ' ');
923
924 /* The PA port needs this information. */
925 if (expressionP->X_add_symbol)
926 expressionP->X_add_symbol->sy_used = 1;
927
928 switch (expressionP->X_op)
929 {
930 default:
931 return absolute_section;
932 case O_symbol:
933 return S_GET_SEGMENT (expressionP->X_add_symbol);
934 case O_register:
935 return reg_section;
936 }
937 } /* operand() */
938 \f
939 /* Internal. Simplify a struct expression for use by expr() */
940
941 /*
942 * In: address of a expressionS.
943 * The X_op field of the expressionS may only take certain values.
944 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
945 * Out: expressionS may have been modified:
946 * 'foo-foo' symbol references cancelled to 0,
947 * which changes X_op from O_subtract to O_constant.
948 * Unused fields zeroed to help expr().
949 */
950
951 static void
952 clean_up_expression (expressionP)
953 expressionS *expressionP;
954 {
955 switch (expressionP->X_op)
956 {
957 case O_illegal:
958 case O_absent:
959 expressionP->X_add_number = 0;
960 /* Fall through. */
961 case O_big:
962 case O_constant:
963 case O_register:
964 expressionP->X_add_symbol = NULL;
965 /* Fall through. */
966 case O_symbol:
967 case O_uminus:
968 case O_bit_not:
969 expressionP->X_op_symbol = NULL;
970 break;
971 case O_subtract:
972 if (expressionP->X_op_symbol == expressionP->X_add_symbol
973 || ((expressionP->X_op_symbol->sy_frag
974 == expressionP->X_add_symbol->sy_frag)
975 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
976 && (S_GET_VALUE (expressionP->X_op_symbol)
977 == S_GET_VALUE (expressionP->X_add_symbol))))
978 {
979 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
980 - S_GET_VALUE (expressionP->X_op_symbol));
981
982 expressionP->X_op = O_constant;
983 expressionP->X_add_symbol = NULL;
984 expressionP->X_op_symbol = NULL;
985 expressionP->X_add_number += diff;
986 }
987 break;
988 default:
989 break;
990 }
991 }
992 \f
993 /* Expression parser. */
994
995 /*
996 * We allow an empty expression, and just assume (absolute,0) silently.
997 * Unary operators and parenthetical expressions are treated as operands.
998 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
999 *
1000 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1001 * warped that I flushed it and wrote a recursive-descent parser instead.
1002 * Now things are stable, would anybody like to write a fast parser?
1003 * Most expressions are either register (which does not even reach here)
1004 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1005 * So I guess it doesn't really matter how inefficient more complex expressions
1006 * are parsed.
1007 *
1008 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1009 * Also, we have consumed any leading or trailing spaces (operand does that)
1010 * and done all intervening operators.
1011 *
1012 * This returns the segment of the result, which will be
1013 * absolute_section or the segment of a symbol.
1014 */
1015
1016 #undef __
1017 #define __ O_illegal
1018
1019 static const operatorT op_encoding[256] =
1020 { /* maps ASCII->operators */
1021
1022 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1023 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1024
1025 __, O_bit_or_not, O_bit_not, __, __, O_modulus, O_bit_and, __,
1026 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1027 __, __, __, __, __, __, __, __,
1028 __, __, __, __, O_lt, __, O_gt, __,
1029 __, __, __, __, __, __, __, __,
1030 __, __, __, __, __, __, __, __,
1031 __, __, __, __, __, __, __, __,
1032 __, __, __, __, __, __, O_bit_exclusive_or, __,
1033 __, __, __, __, __, __, __, __,
1034 __, __, __, __, __, __, __, __,
1035 __, __, __, __, __, __, __, __,
1036 __, __, __, __, O_bit_inclusive_or, __, __, __,
1037
1038 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1039 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1040 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1041 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1042 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1043 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1044 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1045 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1046 };
1047
1048
1049 /*
1050 * Rank Examples
1051 * 0 operand, (expression)
1052 * 1 = <> < <= >= >
1053 * 2 + -
1054 * 3 used for * / % in MRI mode
1055 * 4 & ^ ! |
1056 * 5 * / % << >>
1057 * 6 unary - unary ~
1058 */
1059 static operator_rankT op_rank[] =
1060 {
1061 0, /* O_illegal */
1062 0, /* O_absent */
1063 0, /* O_constant */
1064 0, /* O_symbol */
1065 0, /* O_register */
1066 0, /* O_bit */
1067 6, /* O_uminus */
1068 6, /* O_bit_not */
1069 5, /* O_multiply */
1070 5, /* O_divide */
1071 5, /* O_modulus */
1072 5, /* O_left_shift */
1073 5, /* O_right_shift */
1074 4, /* O_bit_inclusive_or */
1075 4, /* O_bit_or_not */
1076 4, /* O_bit_exclusive_or */
1077 4, /* O_bit_and */
1078 2, /* O_add */
1079 2, /* O_subtract */
1080 1, /* O_eq */
1081 1, /* O_ne */
1082 1, /* O_lt */
1083 1, /* O_le */
1084 1, /* O_ge */
1085 1 /* O_gt */
1086 };
1087
1088 /* Initialize the expression parser. */
1089
1090 void
1091 expr_begin ()
1092 {
1093 /* In MRI mode, multiplication and division have lower precedence
1094 than the bit wise operators. */
1095 if (flag_mri)
1096 {
1097 op_rank[O_multiply] = 3;
1098 op_rank[O_divide] = 3;
1099 op_rank[O_modulus] = 3;
1100 }
1101 }
1102 \f
1103 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1104 Advance INPUT_LINE_POINTER to the last character in the operator
1105 (i.e., don't change it for a single character operator). */
1106
1107 static inline operatorT
1108 operator ()
1109 {
1110 int c;
1111 operatorT ret;
1112
1113 c = *input_line_pointer;
1114
1115 switch (c)
1116 {
1117 default:
1118 return op_encoding[c];
1119
1120 case '<':
1121 switch (input_line_pointer[1])
1122 {
1123 default:
1124 return op_encoding[c];
1125 case '<':
1126 ret = O_left_shift;
1127 break;
1128 case '>':
1129 ret = O_ne;
1130 break;
1131 case '=':
1132 ret = O_le;
1133 break;
1134 }
1135 ++input_line_pointer;
1136 return ret;
1137
1138 case '>':
1139 switch (input_line_pointer[1])
1140 {
1141 default:
1142 return op_encoding[c];
1143 case '>':
1144 ret = O_right_shift;
1145 break;
1146 case '=':
1147 ret = O_ge;
1148 break;
1149 }
1150 ++input_line_pointer;
1151 return ret;
1152
1153 case '!':
1154 /* We accept !! as equivalent to ^ for MRI compatibility. */
1155 if (input_line_pointer[1] != '!')
1156 {
1157 if (flag_mri)
1158 return O_bit_inclusive_or;
1159 return op_encoding[c];
1160 }
1161 ++input_line_pointer;
1162 return O_bit_exclusive_or;
1163 }
1164
1165 /*NOTREACHED*/
1166 }
1167
1168 /* Parse an expression. */
1169
1170 segT
1171 expr (rank, resultP)
1172 operator_rankT rank; /* Larger # is higher rank. */
1173 expressionS *resultP; /* Deliver result here. */
1174 {
1175 segT retval;
1176 expressionS right;
1177 operatorT op_left;
1178 operatorT op_right;
1179
1180 know (rank >= 0);
1181
1182 retval = operand (resultP);
1183
1184 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
1185
1186 op_left = operator ();
1187 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1188 {
1189 segT rightseg;
1190
1191 input_line_pointer++; /*->after 1st character of operator. */
1192
1193 rightseg = expr (op_rank[(int) op_left], &right);
1194 if (right.X_op == O_absent)
1195 {
1196 as_warn ("missing operand; zero assumed");
1197 right.X_op = O_constant;
1198 right.X_add_number = 0;
1199 right.X_add_symbol = NULL;
1200 right.X_op_symbol = NULL;
1201 }
1202
1203 know (*input_line_pointer != ' ');
1204
1205 if (retval == undefined_section)
1206 {
1207 if (SEG_NORMAL (rightseg))
1208 retval = rightseg;
1209 }
1210 else if (! SEG_NORMAL (retval))
1211 retval = rightseg;
1212 else if (SEG_NORMAL (rightseg)
1213 && retval != rightseg
1214 #ifdef DIFF_EXPR_OK
1215 && op_left != O_subtract
1216 #endif
1217 )
1218 as_bad ("operation combines symbols in different segments");
1219
1220 op_right = operator ();
1221
1222 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1223 know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_subtract);
1224
1225 /* input_line_pointer->after right-hand quantity. */
1226 /* left-hand quantity in resultP */
1227 /* right-hand quantity in right. */
1228 /* operator in op_left. */
1229
1230 if (resultP->X_op == O_big)
1231 {
1232 as_warn ("left operand is a %s; integer 0 assumed",
1233 resultP->X_add_number > 0 ? "bignum" : "float");
1234 resultP->X_op = O_constant;
1235 resultP->X_add_number = 0;
1236 resultP->X_add_symbol = NULL;
1237 resultP->X_op_symbol = NULL;
1238 }
1239 if (right.X_op == O_big)
1240 {
1241 as_warn ("right operand is a %s; integer 0 assumed",
1242 right.X_add_number > 0 ? "bignum" : "float");
1243 right.X_op = O_constant;
1244 right.X_add_number = 0;
1245 right.X_add_symbol = NULL;
1246 right.X_op_symbol = NULL;
1247 }
1248
1249 /* Optimize common cases. */
1250 if (op_left == O_add && right.X_op == O_constant)
1251 {
1252 /* X + constant. */
1253 resultP->X_add_number += right.X_add_number;
1254 }
1255 /* This case comes up in PIC code. */
1256 else if (op_left == O_subtract
1257 && right.X_op == O_symbol
1258 && resultP->X_op == O_symbol
1259 && (right.X_add_symbol->sy_frag
1260 == resultP->X_add_symbol->sy_frag)
1261 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1262
1263 {
1264 resultP->X_add_number += right.X_add_number;
1265 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1266 - S_GET_VALUE (right.X_add_symbol));
1267 resultP->X_op = O_constant;
1268 resultP->X_add_symbol = 0;
1269 }
1270 else if (op_left == O_subtract && right.X_op == O_constant)
1271 {
1272 /* X - constant. */
1273 resultP->X_add_number -= right.X_add_number;
1274 }
1275 else if (op_left == O_add && resultP->X_op == O_constant)
1276 {
1277 /* Constant + X. */
1278 resultP->X_op = right.X_op;
1279 resultP->X_add_symbol = right.X_add_symbol;
1280 resultP->X_op_symbol = right.X_op_symbol;
1281 resultP->X_add_number += right.X_add_number;
1282 retval = rightseg;
1283 }
1284 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1285 {
1286 /* Constant OP constant. */
1287 offsetT v = right.X_add_number;
1288 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1289 {
1290 as_warn ("division by zero");
1291 v = 1;
1292 }
1293 switch (op_left)
1294 {
1295 default: abort ();
1296 case O_multiply: resultP->X_add_number *= v; break;
1297 case O_divide: resultP->X_add_number /= v; break;
1298 case O_modulus: resultP->X_add_number %= v; break;
1299 case O_left_shift: resultP->X_add_number <<= v; break;
1300 case O_right_shift: resultP->X_add_number >>= v; break;
1301 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1302 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1303 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1304 case O_bit_and: resultP->X_add_number &= v; break;
1305 case O_add: resultP->X_add_number += v; break;
1306 case O_subtract: resultP->X_add_number -= v; break;
1307 case O_eq:
1308 resultP->X_add_number =
1309 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1310 break;
1311 case O_ne:
1312 resultP->X_add_number =
1313 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1314 break;
1315 case O_lt:
1316 resultP->X_add_number =
1317 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1318 break;
1319 case O_le:
1320 resultP->X_add_number =
1321 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1322 break;
1323 case O_ge:
1324 resultP->X_add_number =
1325 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1326 break;
1327 case O_gt:
1328 resultP->X_add_number =
1329 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1330 break;
1331 }
1332 }
1333 else if (resultP->X_op == O_symbol
1334 && right.X_op == O_symbol
1335 && (op_left == O_add
1336 || op_left == O_subtract
1337 || (resultP->X_add_number == 0
1338 && right.X_add_number == 0)))
1339 {
1340 /* Symbol OP symbol. */
1341 resultP->X_op = op_left;
1342 resultP->X_op_symbol = right.X_add_symbol;
1343 if (op_left == O_add)
1344 resultP->X_add_number += right.X_add_number;
1345 else if (op_left == O_subtract)
1346 resultP->X_add_number -= right.X_add_number;
1347 }
1348 else
1349 {
1350 /* The general case. */
1351 resultP->X_add_symbol = make_expr_symbol (resultP);
1352 resultP->X_op_symbol = make_expr_symbol (&right);
1353 resultP->X_op = op_left;
1354 resultP->X_add_number = 0;
1355 resultP->X_unsigned = 1;
1356 }
1357
1358 op_left = op_right;
1359 } /* While next operator is >= this rank. */
1360
1361 /* The PA port needs this information. */
1362 if (resultP->X_add_symbol)
1363 resultP->X_add_symbol->sy_used = 1;
1364
1365 return resultP->X_op == O_constant ? absolute_section : retval;
1366 }
1367 \f
1368 /*
1369 * get_symbol_end()
1370 *
1371 * This lives here because it belongs equally in expr.c & read.c.
1372 * Expr.c is just a branch office read.c anyway, and putting it
1373 * here lessens the crowd at read.c.
1374 *
1375 * Assume input_line_pointer is at start of symbol name.
1376 * Advance input_line_pointer past symbol name.
1377 * Turn that character into a '\0', returning its former value.
1378 * This allows a string compare (RMS wants symbol names to be strings)
1379 * of the symbol name.
1380 * There will always be a char following symbol name, because all good
1381 * lines end in end-of-line.
1382 */
1383 char
1384 get_symbol_end ()
1385 {
1386 char c;
1387
1388 while (is_part_of_name (c = *input_line_pointer++))
1389 ;
1390 *--input_line_pointer = 0;
1391 return (c);
1392 }
1393
1394
1395 unsigned int
1396 get_single_number ()
1397 {
1398 expressionS exp;
1399 operand (&exp);
1400 return exp.X_add_number;
1401
1402 }
1403
1404 /* end of expr.c */
This page took 0.057873 seconds and 4 git commands to generate.