source: trunk/src/binutils/gas/expr.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

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