* expr.c (operand): Handle 08 and 09 in MRI mode.
[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 case '8':
565 case '9':
566 if (flag_mri)
567 {
568 integer_constant (0, expressionP);
569 break;
570 }
571 /* Fall through. */
572 default:
573 default_case:
574 if (c && strchr (FLT_CHARS, c))
575 {
576 input_line_pointer++;
577 floating_constant (expressionP);
578 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
579 }
580 else
581 {
582 /* The string was only zero */
583 expressionP->X_op = O_constant;
584 expressionP->X_add_number = 0;
585 }
586
587 break;
588
589 case 'x':
590 case 'X':
591 if (flag_mri)
592 goto default_case;
593 input_line_pointer++;
594 integer_constant (16, expressionP);
595 break;
596
597 case 'b':
598 if (LOCAL_LABELS_FB)
599 {
600 switch (input_line_pointer[1])
601 {
602 case '+':
603 case '-':
604 /* If unambiguously a difference expression, treat
605 it as one by indicating a label; otherwise, it's
606 always a binary number. */
607 {
608 char *cp = input_line_pointer + 1;
609 while (strchr ("0123456789", *++cp))
610 ;
611 if (*cp == 'b' || *cp == 'f')
612 goto is_0b_label;
613 }
614 goto is_0b_binary;
615 case '0': case '1':
616 /* Some of our code elsewhere does permit digits
617 greater than the expected base; for consistency,
618 do the same here. */
619 case '2': case '3': case '4': case '5':
620 case '6': case '7': case '8': case '9':
621 goto is_0b_binary;
622 case 0:
623 goto is_0b_label;
624 default:
625 goto is_0b_label;
626 }
627 is_0b_label:
628 input_line_pointer--;
629 integer_constant (10, expressionP);
630 break;
631 is_0b_binary:
632 ;
633 }
634 case 'B':
635 input_line_pointer++;
636 if (flag_mri)
637 goto default_case;
638 integer_constant (2, expressionP);
639 break;
640
641 case '0':
642 case '1':
643 case '2':
644 case '3':
645 case '4':
646 case '5':
647 case '6':
648 case '7':
649 integer_constant (flag_mri ? 0 : 8, expressionP);
650 break;
651
652 case 'f':
653 if (LOCAL_LABELS_FB)
654 {
655 /* If it says "0f" and it could possibly be a floating point
656 number, make it one. Otherwise, make it a local label,
657 and try to deal with parsing the rest later. */
658 if (!input_line_pointer[1]
659 || (is_end_of_line[0xff & input_line_pointer[1]]))
660 goto is_0f_label;
661 {
662 char *cp = input_line_pointer + 1;
663 int r = atof_generic (&cp, ".", EXP_CHARS,
664 &generic_floating_point_number);
665 switch (r)
666 {
667 case 0:
668 case ERROR_EXPONENT_OVERFLOW:
669 if (*cp == 'f' || *cp == 'b')
670 /* looks like a difference expression */
671 goto is_0f_label;
672 else
673 goto is_0f_float;
674 default:
675 as_fatal ("expr.c(operand): bad atof_generic return val %d",
676 r);
677 }
678 }
679
680 /* Okay, now we've sorted it out. We resume at one of these
681 two labels, depending on what we've decided we're probably
682 looking at. */
683 is_0f_label:
684 input_line_pointer--;
685 integer_constant (10, expressionP);
686 break;
687
688 is_0f_float:
689 /* fall through */
690 ;
691 }
692
693 case 'd':
694 case 'D':
695 case 'F':
696 case 'r':
697 case 'e':
698 case 'E':
699 case 'g':
700 case 'G':
701 input_line_pointer++;
702 floating_constant (expressionP);
703 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
704 break;
705
706 case '$':
707 if (LOCAL_LABELS_DOLLAR)
708 {
709 integer_constant (10, expressionP);
710 break;
711 }
712 else
713 goto default_case;
714 }
715
716 break;
717
718 case '(':
719 case '[':
720 /* didn't begin with digit & not a name */
721 segment = expression (expressionP);
722 /* Expression() will pass trailing whitespace */
723 if ((c == '(' && *input_line_pointer++ != ')')
724 || (c == '[' && *input_line_pointer++ != ']'))
725 {
726 as_bad ("Missing ')' assumed");
727 input_line_pointer--;
728 }
729 /* here with input_line_pointer->char after "(...)" */
730 return segment;
731
732 case 'E':
733 if (! flag_mri || *input_line_pointer != '\'')
734 goto de_fault;
735 as_bad ("EBCDIC constants are not supported");
736 /* Fall through. */
737 case 'A':
738 if (! flag_mri || *input_line_pointer != '\'')
739 goto de_fault;
740 ++input_line_pointer;
741 /* Fall through. */
742 case '\'':
743 if (! flag_mri)
744 {
745 /* Warning: to conform to other people's assemblers NO
746 ESCAPEMENT is permitted for a single quote. The next
747 character, parity errors and all, is taken as the value
748 of the operand. VERY KINKY. */
749 expressionP->X_op = O_constant;
750 expressionP->X_add_number = *input_line_pointer++;
751 break;
752 }
753
754 mri_char_constant (expressionP);
755 break;
756
757 case '+':
758 (void) operand (expressionP);
759 break;
760
761 case '"':
762 /* Double quote is the logical not operator in MRI mode. */
763 if (! flag_mri)
764 goto de_fault;
765 /* Fall through. */
766 case '~':
767 case '-':
768 {
769 operand (expressionP);
770 if (expressionP->X_op == O_constant)
771 {
772 /* input_line_pointer -> char after operand */
773 if (c == '-')
774 {
775 expressionP->X_add_number = - expressionP->X_add_number;
776 /* Notice: '-' may overflow: no warning is given. This is
777 compatible with other people's assemblers. Sigh. */
778 expressionP->X_unsigned = 0;
779 }
780 else
781 expressionP->X_add_number = ~ expressionP->X_add_number;
782 }
783 else if (expressionP->X_op != O_illegal
784 && expressionP->X_op != O_absent)
785 {
786 expressionP->X_add_symbol = make_expr_symbol (expressionP);
787 if (c == '-')
788 expressionP->X_op = O_uminus;
789 else
790 expressionP->X_op = O_bit_not;
791 expressionP->X_add_number = 0;
792 }
793 else
794 as_warn ("Unary operator %c ignored because bad operand follows",
795 c);
796 }
797 break;
798
799 case '$':
800 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
801 is defined. */
802 #ifndef DOLLAR_DOT
803 if (! flag_mri)
804 goto de_fault;
805 #endif
806 if (flag_mri && hex_p (*input_line_pointer))
807 {
808 /* In MRI mode, $ is also used as the prefix for a
809 hexadecimal constant. */
810 integer_constant (16, expressionP);
811 break;
812 }
813 /* Fall through. */
814 case '.':
815 if (!is_part_of_name (*input_line_pointer))
816 {
817 const char *fake;
818
819 /* JF: '.' is pseudo symbol with value of current location
820 in current segment. */
821 fake = FAKE_LABEL_NAME;
822 symbolP = symbol_new (fake,
823 now_seg,
824 (valueT) frag_now_fix (),
825 frag_now);
826
827 expressionP->X_op = O_symbol;
828 expressionP->X_add_symbol = symbolP;
829 expressionP->X_add_number = 0;
830 break;
831 }
832 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
833 && ! is_part_of_name (input_line_pointer[8]))
834 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
835 && ! is_part_of_name (input_line_pointer[7])))
836 {
837 int start;
838
839 start = (input_line_pointer[1] == 't'
840 || input_line_pointer[1] == 'T');
841 input_line_pointer += start ? 8 : 7;
842 SKIP_WHITESPACE ();
843 if (*input_line_pointer != '(')
844 as_bad ("syntax error in .startof. or .sizeof.");
845 else
846 {
847 char *buf;
848
849 ++input_line_pointer;
850 SKIP_WHITESPACE ();
851 name = input_line_pointer;
852 c = get_symbol_end ();
853
854 buf = (char *) xmalloc (strlen (name) + 10);
855 if (start)
856 sprintf (buf, ".startof.%s", name);
857 else
858 sprintf (buf, ".sizeof.%s", name);
859 symbolP = symbol_make (buf);
860 free (buf);
861
862 expressionP->X_op = O_symbol;
863 expressionP->X_add_symbol = symbolP;
864 expressionP->X_add_number = 0;
865
866 *input_line_pointer = c;
867 SKIP_WHITESPACE ();
868 if (*input_line_pointer != ')')
869 as_bad ("syntax error in .startof. or .sizeof.");
870 else
871 ++input_line_pointer;
872 }
873 break;
874 }
875 else
876 {
877 goto isname;
878 }
879 case ',':
880 case '\n':
881 case '\0':
882 eol:
883 /* can't imagine any other kind of operand */
884 expressionP->X_op = O_absent;
885 input_line_pointer--;
886 break;
887
888 case '%':
889 if (! flag_mri)
890 goto de_fault;
891 integer_constant (2, expressionP);
892 break;
893
894 case '@':
895 if (! flag_mri)
896 goto de_fault;
897 integer_constant (8, expressionP);
898 break;
899
900 case ':':
901 if (! flag_mri)
902 goto de_fault;
903
904 /* In MRI mode, this is a floating point constant represented
905 using hexadecimal digits. */
906
907 ++input_line_pointer;
908 integer_constant (16, expressionP);
909 break;
910
911 default:
912 de_fault:
913 if (is_end_of_line[(unsigned char) c])
914 goto eol;
915 if (is_name_beginner (c)) /* here if did not begin with a digit */
916 {
917 /*
918 * Identifier begins here.
919 * This is kludged for speed, so code is repeated.
920 */
921 isname:
922 name = --input_line_pointer;
923 c = get_symbol_end ();
924 symbolP = symbol_find_or_make (name);
925
926 /* If we have an absolute symbol or a reg, then we know its
927 value now. */
928 segment = S_GET_SEGMENT (symbolP);
929 if (segment == absolute_section)
930 {
931 expressionP->X_op = O_constant;
932 expressionP->X_add_number = S_GET_VALUE (symbolP);
933 }
934 else if (segment == reg_section)
935 {
936 expressionP->X_op = O_register;
937 expressionP->X_add_number = S_GET_VALUE (symbolP);
938 }
939 else
940 {
941 expressionP->X_op = O_symbol;
942 expressionP->X_add_symbol = symbolP;
943 expressionP->X_add_number = 0;
944 }
945 *input_line_pointer = c;
946 }
947 else
948 {
949 /* Let the target try to parse it. Success is indicated by changing
950 the X_op field to something other than O_absent and pointing
951 input_line_pointer passed the expression. If it can't parse the
952 expression, X_op and input_line_pointer should be unchanged. */
953 expressionP->X_op = O_absent;
954 --input_line_pointer;
955 md_operand (expressionP);
956 if (expressionP->X_op == O_absent)
957 {
958 ++input_line_pointer;
959 as_bad ("Bad expression");
960 expressionP->X_op = O_constant;
961 expressionP->X_add_number = 0;
962 }
963 }
964 break;
965 }
966
967 /*
968 * It is more 'efficient' to clean up the expressionS when they are created.
969 * Doing it here saves lines of code.
970 */
971 clean_up_expression (expressionP);
972 SKIP_WHITESPACE (); /*->1st char after operand. */
973 know (*input_line_pointer != ' ');
974
975 /* The PA port needs this information. */
976 if (expressionP->X_add_symbol)
977 expressionP->X_add_symbol->sy_used = 1;
978
979 switch (expressionP->X_op)
980 {
981 default:
982 return absolute_section;
983 case O_symbol:
984 return S_GET_SEGMENT (expressionP->X_add_symbol);
985 case O_register:
986 return reg_section;
987 }
988 } /* operand() */
989 \f
990 /* Internal. Simplify a struct expression for use by expr() */
991
992 /*
993 * In: address of a expressionS.
994 * The X_op field of the expressionS may only take certain values.
995 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
996 * Out: expressionS may have been modified:
997 * 'foo-foo' symbol references cancelled to 0,
998 * which changes X_op from O_subtract to O_constant.
999 * Unused fields zeroed to help expr().
1000 */
1001
1002 static void
1003 clean_up_expression (expressionP)
1004 expressionS *expressionP;
1005 {
1006 switch (expressionP->X_op)
1007 {
1008 case O_illegal:
1009 case O_absent:
1010 expressionP->X_add_number = 0;
1011 /* Fall through. */
1012 case O_big:
1013 case O_constant:
1014 case O_register:
1015 expressionP->X_add_symbol = NULL;
1016 /* Fall through. */
1017 case O_symbol:
1018 case O_uminus:
1019 case O_bit_not:
1020 expressionP->X_op_symbol = NULL;
1021 break;
1022 case O_subtract:
1023 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1024 || ((expressionP->X_op_symbol->sy_frag
1025 == expressionP->X_add_symbol->sy_frag)
1026 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1027 && (S_GET_VALUE (expressionP->X_op_symbol)
1028 == S_GET_VALUE (expressionP->X_add_symbol))))
1029 {
1030 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1031 - S_GET_VALUE (expressionP->X_op_symbol));
1032
1033 expressionP->X_op = O_constant;
1034 expressionP->X_add_symbol = NULL;
1035 expressionP->X_op_symbol = NULL;
1036 expressionP->X_add_number += diff;
1037 }
1038 break;
1039 default:
1040 break;
1041 }
1042 }
1043 \f
1044 /* Expression parser. */
1045
1046 /*
1047 * We allow an empty expression, and just assume (absolute,0) silently.
1048 * Unary operators and parenthetical expressions are treated as operands.
1049 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1050 *
1051 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1052 * warped that I flushed it and wrote a recursive-descent parser instead.
1053 * Now things are stable, would anybody like to write a fast parser?
1054 * Most expressions are either register (which does not even reach here)
1055 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1056 * So I guess it doesn't really matter how inefficient more complex expressions
1057 * are parsed.
1058 *
1059 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1060 * Also, we have consumed any leading or trailing spaces (operand does that)
1061 * and done all intervening operators.
1062 *
1063 * This returns the segment of the result, which will be
1064 * absolute_section or the segment of a symbol.
1065 */
1066
1067 #undef __
1068 #define __ O_illegal
1069
1070 static operatorT op_encoding[256] =
1071 { /* maps ASCII->operators */
1072
1073 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1074 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1075
1076 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1077 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1078 __, __, __, __, __, __, __, __,
1079 __, __, __, __, O_lt, __, O_gt, __,
1080 __, __, __, __, __, __, __, __,
1081 __, __, __, __, __, __, __, __,
1082 __, __, __, __, __, __, __, __,
1083 __, __, __, __, __, __, O_bit_exclusive_or, __,
1084 __, __, __, __, __, __, __, __,
1085 __, __, __, __, __, __, __, __,
1086 __, __, __, __, __, __, __, __,
1087 __, __, __, __, O_bit_inclusive_or, __, __, __,
1088
1089 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1090 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1091 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1092 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1093 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1094 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1095 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1096 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1097 };
1098
1099
1100 /*
1101 * Rank Examples
1102 * 0 operand, (expression)
1103 * 1 = <> < <= >= >
1104 * 2 + -
1105 * 3 used for * / % in MRI mode
1106 * 4 & ^ ! |
1107 * 5 * / % << >>
1108 * 6 unary - unary ~
1109 */
1110 static operator_rankT op_rank[] =
1111 {
1112 0, /* O_illegal */
1113 0, /* O_absent */
1114 0, /* O_constant */
1115 0, /* O_symbol */
1116 0, /* O_register */
1117 0, /* O_bit */
1118 6, /* O_uminus */
1119 6, /* O_bit_not */
1120 5, /* O_multiply */
1121 5, /* O_divide */
1122 5, /* O_modulus */
1123 5, /* O_left_shift */
1124 5, /* O_right_shift */
1125 4, /* O_bit_inclusive_or */
1126 4, /* O_bit_or_not */
1127 4, /* O_bit_exclusive_or */
1128 4, /* O_bit_and */
1129 2, /* O_add */
1130 2, /* O_subtract */
1131 1, /* O_eq */
1132 1, /* O_ne */
1133 1, /* O_lt */
1134 1, /* O_le */
1135 1, /* O_ge */
1136 1 /* O_gt */
1137 };
1138
1139 /* Initialize the expression parser. */
1140
1141 void
1142 expr_begin ()
1143 {
1144 /* In MRI mode, multiplication and division have lower precedence
1145 than the bit wise operators. */
1146 if (flag_mri)
1147 {
1148 op_rank[O_multiply] = 3;
1149 op_rank[O_divide] = 3;
1150 op_rank[O_modulus] = 3;
1151 op_encoding['"'] = O_bit_not;
1152 }
1153 }
1154 \f
1155 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1156 Advance INPUT_LINE_POINTER to the last character in the operator
1157 (i.e., don't change it for a single character operator). */
1158
1159 static inline operatorT
1160 operator ()
1161 {
1162 int c;
1163 operatorT ret;
1164
1165 c = *input_line_pointer;
1166
1167 switch (c)
1168 {
1169 default:
1170 return op_encoding[c];
1171
1172 case '<':
1173 switch (input_line_pointer[1])
1174 {
1175 default:
1176 return op_encoding[c];
1177 case '<':
1178 ret = O_left_shift;
1179 break;
1180 case '>':
1181 ret = O_ne;
1182 break;
1183 case '=':
1184 ret = O_le;
1185 break;
1186 }
1187 ++input_line_pointer;
1188 return ret;
1189
1190 case '>':
1191 switch (input_line_pointer[1])
1192 {
1193 default:
1194 return op_encoding[c];
1195 case '>':
1196 ret = O_right_shift;
1197 break;
1198 case '=':
1199 ret = O_ge;
1200 break;
1201 }
1202 ++input_line_pointer;
1203 return ret;
1204
1205 case '!':
1206 /* We accept !! as equivalent to ^ for MRI compatibility. */
1207 if (input_line_pointer[1] != '!')
1208 {
1209 if (flag_mri)
1210 return O_bit_inclusive_or;
1211 return op_encoding[c];
1212 }
1213 ++input_line_pointer;
1214 return O_bit_exclusive_or;
1215 }
1216
1217 /*NOTREACHED*/
1218 }
1219
1220 /* Parse an expression. */
1221
1222 segT
1223 expr (rank, resultP)
1224 operator_rankT rank; /* Larger # is higher rank. */
1225 expressionS *resultP; /* Deliver result here. */
1226 {
1227 segT retval;
1228 expressionS right;
1229 operatorT op_left;
1230 operatorT op_right;
1231
1232 know (rank >= 0);
1233
1234 retval = operand (resultP);
1235
1236 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
1237
1238 op_left = operator ();
1239 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1240 {
1241 segT rightseg;
1242
1243 input_line_pointer++; /*->after 1st character of operator. */
1244
1245 rightseg = expr (op_rank[(int) op_left], &right);
1246 if (right.X_op == O_absent)
1247 {
1248 as_warn ("missing operand; zero assumed");
1249 right.X_op = O_constant;
1250 right.X_add_number = 0;
1251 right.X_add_symbol = NULL;
1252 right.X_op_symbol = NULL;
1253 }
1254
1255 know (*input_line_pointer != ' ');
1256
1257 if (retval == undefined_section)
1258 {
1259 if (SEG_NORMAL (rightseg))
1260 retval = rightseg;
1261 }
1262 else if (! SEG_NORMAL (retval))
1263 retval = rightseg;
1264 else if (SEG_NORMAL (rightseg)
1265 && retval != rightseg
1266 #ifdef DIFF_EXPR_OK
1267 && op_left != O_subtract
1268 #endif
1269 )
1270 as_bad ("operation combines symbols in different segments");
1271
1272 op_right = operator ();
1273
1274 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1275 know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_subtract);
1276
1277 /* input_line_pointer->after right-hand quantity. */
1278 /* left-hand quantity in resultP */
1279 /* right-hand quantity in right. */
1280 /* operator in op_left. */
1281
1282 if (resultP->X_op == O_big)
1283 {
1284 as_warn ("left operand is a %s; integer 0 assumed",
1285 resultP->X_add_number > 0 ? "bignum" : "float");
1286 resultP->X_op = O_constant;
1287 resultP->X_add_number = 0;
1288 resultP->X_add_symbol = NULL;
1289 resultP->X_op_symbol = NULL;
1290 }
1291 if (right.X_op == O_big)
1292 {
1293 as_warn ("right operand is a %s; integer 0 assumed",
1294 right.X_add_number > 0 ? "bignum" : "float");
1295 right.X_op = O_constant;
1296 right.X_add_number = 0;
1297 right.X_add_symbol = NULL;
1298 right.X_op_symbol = NULL;
1299 }
1300
1301 /* Optimize common cases. */
1302 if (op_left == O_add && right.X_op == O_constant)
1303 {
1304 /* X + constant. */
1305 resultP->X_add_number += right.X_add_number;
1306 }
1307 /* This case comes up in PIC code. */
1308 else if (op_left == O_subtract
1309 && right.X_op == O_symbol
1310 && resultP->X_op == O_symbol
1311 && (right.X_add_symbol->sy_frag
1312 == resultP->X_add_symbol->sy_frag)
1313 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1314
1315 {
1316 resultP->X_add_number += right.X_add_number;
1317 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1318 - S_GET_VALUE (right.X_add_symbol));
1319 resultP->X_op = O_constant;
1320 resultP->X_add_symbol = 0;
1321 }
1322 else if (op_left == O_subtract && right.X_op == O_constant)
1323 {
1324 /* X - constant. */
1325 resultP->X_add_number -= right.X_add_number;
1326 }
1327 else if (op_left == O_add && resultP->X_op == O_constant)
1328 {
1329 /* Constant + X. */
1330 resultP->X_op = right.X_op;
1331 resultP->X_add_symbol = right.X_add_symbol;
1332 resultP->X_op_symbol = right.X_op_symbol;
1333 resultP->X_add_number += right.X_add_number;
1334 retval = rightseg;
1335 }
1336 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1337 {
1338 /* Constant OP constant. */
1339 offsetT v = right.X_add_number;
1340 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1341 {
1342 as_warn ("division by zero");
1343 v = 1;
1344 }
1345 switch (op_left)
1346 {
1347 default: abort ();
1348 case O_multiply: resultP->X_add_number *= v; break;
1349 case O_divide: resultP->X_add_number /= v; break;
1350 case O_modulus: resultP->X_add_number %= v; break;
1351 case O_left_shift: resultP->X_add_number <<= v; break;
1352 case O_right_shift: resultP->X_add_number >>= v; break;
1353 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1354 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1355 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1356 case O_bit_and: resultP->X_add_number &= v; break;
1357 case O_add: resultP->X_add_number += v; break;
1358 case O_subtract: resultP->X_add_number -= v; break;
1359 case O_eq:
1360 resultP->X_add_number =
1361 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1362 break;
1363 case O_ne:
1364 resultP->X_add_number =
1365 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1366 break;
1367 case O_lt:
1368 resultP->X_add_number =
1369 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1370 break;
1371 case O_le:
1372 resultP->X_add_number =
1373 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1374 break;
1375 case O_ge:
1376 resultP->X_add_number =
1377 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1378 break;
1379 case O_gt:
1380 resultP->X_add_number =
1381 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1382 break;
1383 }
1384 }
1385 else if (resultP->X_op == O_symbol
1386 && right.X_op == O_symbol
1387 && (op_left == O_add
1388 || op_left == O_subtract
1389 || (resultP->X_add_number == 0
1390 && right.X_add_number == 0)))
1391 {
1392 /* Symbol OP symbol. */
1393 resultP->X_op = op_left;
1394 resultP->X_op_symbol = right.X_add_symbol;
1395 if (op_left == O_add)
1396 resultP->X_add_number += right.X_add_number;
1397 else if (op_left == O_subtract)
1398 resultP->X_add_number -= right.X_add_number;
1399 }
1400 else
1401 {
1402 /* The general case. */
1403 resultP->X_add_symbol = make_expr_symbol (resultP);
1404 resultP->X_op_symbol = make_expr_symbol (&right);
1405 resultP->X_op = op_left;
1406 resultP->X_add_number = 0;
1407 resultP->X_unsigned = 1;
1408 }
1409
1410 op_left = op_right;
1411 } /* While next operator is >= this rank. */
1412
1413 /* The PA port needs this information. */
1414 if (resultP->X_add_symbol)
1415 resultP->X_add_symbol->sy_used = 1;
1416
1417 return resultP->X_op == O_constant ? absolute_section : retval;
1418 }
1419 \f
1420 /*
1421 * get_symbol_end()
1422 *
1423 * This lives here because it belongs equally in expr.c & read.c.
1424 * Expr.c is just a branch office read.c anyway, and putting it
1425 * here lessens the crowd at read.c.
1426 *
1427 * Assume input_line_pointer is at start of symbol name.
1428 * Advance input_line_pointer past symbol name.
1429 * Turn that character into a '\0', returning its former value.
1430 * This allows a string compare (RMS wants symbol names to be strings)
1431 * of the symbol name.
1432 * There will always be a char following symbol name, because all good
1433 * lines end in end-of-line.
1434 */
1435 char
1436 get_symbol_end ()
1437 {
1438 char c;
1439
1440 /* We accept \001 in a name in case this is being called with a
1441 constructed string. */
1442 while (is_part_of_name (c = *input_line_pointer++)
1443 || c == '\001')
1444 ;
1445 *--input_line_pointer = 0;
1446 return (c);
1447 }
1448
1449
1450 unsigned int
1451 get_single_number ()
1452 {
1453 expressionS exp;
1454 operand (&exp);
1455 return exp.X_add_number;
1456
1457 }
1458
1459 /* end of expr.c */