* expr.c (expr): Move code setting "retval" to the end of the loop,
[deliverable/binutils-gdb.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
23 /* This is really a branch office of as-read.c. I split it out to clearly
24 distinguish the world of expressions from the world of statements.
25 (It also gives smaller files to re-compile.)
26 Here, "operand"s are of expressions, not instructions. */
27
28 #include <ctype.h>
29 #include <string.h>
30 #define min(a, b) ((a) < (b) ? (a) : (b))
31
32 #include "as.h"
33 #include "obstack.h"
34
35 static void floating_constant PARAMS ((expressionS * expressionP));
36 static valueT generic_bignum_to_int32 PARAMS ((void));
37 #ifdef BFD64
38 static valueT generic_bignum_to_int64 PARAMS ((void));
39 #endif
40 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
41 static void mri_char_constant PARAMS ((expressionS *));
42 static void current_location PARAMS ((expressionS *));
43 static void clean_up_expression PARAMS ((expressionS * expressionP));
44 static segT operand PARAMS ((expressionS *));
45 static operatorT operator PARAMS ((int *));
46
47 extern const char EXP_CHARS[], FLT_CHARS[];
48
49 /* We keep a mapping of expression symbols to file positions, so that
50 we can provide better error messages. */
51
52 struct expr_symbol_line {
53 struct expr_symbol_line *next;
54 symbolS *sym;
55 char *file;
56 unsigned int line;
57 };
58
59 static struct expr_symbol_line *expr_symbol_lines;
60 \f
61 /* Build a dummy symbol to hold a complex expression. This is how we
62 build expressions up out of other expressions. The symbol is put
63 into the fake section expr_section. */
64
65 symbolS *
66 make_expr_symbol (expressionP)
67 expressionS *expressionP;
68 {
69 expressionS zero;
70 const char *fake;
71 symbolS *symbolP;
72 struct expr_symbol_line *n;
73
74 if (expressionP->X_op == O_symbol
75 && expressionP->X_add_number == 0)
76 return expressionP->X_add_symbol;
77
78 if (expressionP->X_op == O_big)
79 {
80 /* This won't work, because the actual value is stored in
81 generic_floating_point_number or generic_bignum, and we are
82 going to lose it if we haven't already. */
83 if (expressionP->X_add_number > 0)
84 as_bad (_("bignum invalid"));
85 else
86 as_bad (_("floating point number invalid"));
87 zero.X_op = O_constant;
88 zero.X_add_number = 0;
89 zero.X_unsigned = 0;
90 clean_up_expression (&zero);
91 expressionP = &zero;
92 }
93
94 fake = FAKE_LABEL_NAME;
95
96 /* Putting constant symbols in absolute_section rather than
97 expr_section is convenient for the old a.out code, for which
98 S_GET_SEGMENT does not always retrieve the value put in by
99 S_SET_SEGMENT. */
100 symbolP = symbol_create (fake,
101 (expressionP->X_op == O_constant
102 ? absolute_section
103 : expr_section),
104 0, &zero_address_frag);
105 symbol_set_value_expression (symbolP, expressionP);
106
107 if (expressionP->X_op == O_constant)
108 resolve_symbol_value (symbolP);
109
110 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
111 n->sym = symbolP;
112 as_where (&n->file, &n->line);
113 n->next = expr_symbol_lines;
114 expr_symbol_lines = n;
115
116 return symbolP;
117 }
118
119 /* Return the file and line number for an expr symbol. Return
120 non-zero if something was found, 0 if no information is known for
121 the symbol. */
122
123 int
124 expr_symbol_where (sym, pfile, pline)
125 symbolS *sym;
126 char **pfile;
127 unsigned int *pline;
128 {
129 register struct expr_symbol_line *l;
130
131 for (l = expr_symbol_lines; l != NULL; l = l->next)
132 {
133 if (l->sym == sym)
134 {
135 *pfile = l->file;
136 *pline = l->line;
137 return 1;
138 }
139 }
140
141 return 0;
142 }
143 \f
144 /* Utilities for building expressions.
145 Since complex expressions are recorded as symbols for use in other
146 expressions these return a symbolS * and not an expressionS *.
147 These explicitly do not take an "add_number" argument. */
148 /* ??? For completeness' sake one might want expr_build_symbol.
149 It would just return its argument. */
150
151 /* Build an expression for an unsigned constant.
152 The corresponding one for signed constants is missing because
153 there's currently no need for it. One could add an unsigned_p flag
154 but that seems more clumsy. */
155
156 symbolS *
157 expr_build_uconstant (value)
158 offsetT value;
159 {
160 expressionS e;
161
162 e.X_op = O_constant;
163 e.X_add_number = value;
164 e.X_unsigned = 1;
165 return make_expr_symbol (&e);
166 }
167
168 /* Build an expression for OP s1. */
169
170 symbolS *
171 expr_build_unary (op, s1)
172 operatorT op;
173 symbolS *s1;
174 {
175 expressionS e;
176
177 e.X_op = op;
178 e.X_add_symbol = s1;
179 e.X_add_number = 0;
180 return make_expr_symbol (&e);
181 }
182
183 /* Build an expression for s1 OP s2. */
184
185 symbolS *
186 expr_build_binary (op, s1, s2)
187 operatorT op;
188 symbolS *s1;
189 symbolS *s2;
190 {
191 expressionS e;
192
193 e.X_op = op;
194 e.X_add_symbol = s1;
195 e.X_op_symbol = s2;
196 e.X_add_number = 0;
197 return make_expr_symbol (&e);
198 }
199
200 /* Build an expression for the current location ('.'). */
201
202 symbolS *
203 expr_build_dot ()
204 {
205 expressionS e;
206
207 current_location (&e);
208 return make_expr_symbol (&e);
209 }
210 \f
211 /* Build any floating-point literal here.
212 Also build any bignum literal here. */
213
214 /* Seems atof_machine can backscan through generic_bignum and hit whatever
215 happens to be loaded before it in memory. And its way too complicated
216 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
217 and never write into the early words, thus they'll always be zero.
218 I hate Dean's floating-point code. Bleh. */
219 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
220
221 FLONUM_TYPE generic_floating_point_number = {
222 &generic_bignum[6], /* low. (JF: Was 0) */
223 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
224 0, /* leader. */
225 0, /* exponent. */
226 0 /* sign. */
227 };
228
229 /* If nonzero, we've been asked to assemble nan, +inf or -inf. */
230 int generic_floating_point_magic;
231 \f
232 static void
233 floating_constant (expressionP)
234 expressionS *expressionP;
235 {
236 /* input_line_pointer -> floating-point constant. */
237 int error_code;
238
239 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
240 &generic_floating_point_number);
241
242 if (error_code)
243 {
244 if (error_code == ERROR_EXPONENT_OVERFLOW)
245 {
246 as_bad (_("bad floating-point constant: exponent overflow"));
247 }
248 else
249 {
250 as_bad (_("bad floating-point constant: unknown error code=%d"),
251 error_code);
252 }
253 }
254 expressionP->X_op = O_big;
255 /* input_line_pointer -> just after constant, which may point to
256 whitespace. */
257 expressionP->X_add_number = -1;
258 }
259
260 static valueT
261 generic_bignum_to_int32 ()
262 {
263 valueT number =
264 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
265 | (generic_bignum[0] & LITTLENUM_MASK);
266 number &= 0xffffffff;
267 return number;
268 }
269
270 #ifdef BFD64
271 static valueT
272 generic_bignum_to_int64 ()
273 {
274 valueT number =
275 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
276 << LITTLENUM_NUMBER_OF_BITS)
277 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
278 << LITTLENUM_NUMBER_OF_BITS)
279 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
280 << LITTLENUM_NUMBER_OF_BITS)
281 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
282 return number;
283 }
284 #endif
285
286 static void
287 integer_constant (radix, expressionP)
288 int radix;
289 expressionS *expressionP;
290 {
291 char *start; /* Start of number. */
292 char *suffix = NULL;
293 char c;
294 valueT number; /* Offset or (absolute) value. */
295 short int digit; /* Value of next digit in current radix. */
296 short int maxdig = 0; /* Highest permitted digit value. */
297 int too_many_digits = 0; /* If we see >= this number of. */
298 char *name; /* Points to name of symbol. */
299 symbolS *symbolP; /* Points to symbol. */
300
301 int small; /* True if fits in 32 bits. */
302
303 /* May be bignum, or may fit in 32 bits. */
304 /* Most numbers fit into 32 bits, and we want this case to be fast.
305 so we pretend it will fit into 32 bits. If, after making up a 32
306 bit number, we realise that we have scanned more digits than
307 comfortably fit into 32 bits, we re-scan the digits coding them
308 into a bignum. For decimal and octal numbers we are
309 conservative: Some numbers may be assumed bignums when in fact
310 they do fit into 32 bits. Numbers of any radix can have excess
311 leading zeros: We strive to recognise this and cast them back
312 into 32 bits. We must check that the bignum really is more than
313 32 bits, and change it back to a 32-bit number if it fits. The
314 number we are looking for is expected to be positive, but if it
315 fits into 32 bits as an unsigned number, we let it be a 32-bit
316 number. The cavalier approach is for speed in ordinary cases. */
317 /* This has been extended for 64 bits. We blindly assume that if
318 you're compiling in 64-bit mode, the target is a 64-bit machine.
319 This should be cleaned up. */
320
321 #ifdef BFD64
322 #define valuesize 64
323 #else /* includes non-bfd case, mostly */
324 #define valuesize 32
325 #endif
326
327 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
328 {
329 int flt = 0;
330
331 /* In MRI mode, the number may have a suffix indicating the
332 radix. For that matter, it might actually be a floating
333 point constant. */
334 for (suffix = input_line_pointer;
335 isalnum ((unsigned char) *suffix);
336 suffix++)
337 {
338 if (*suffix == 'e' || *suffix == 'E')
339 flt = 1;
340 }
341
342 if (suffix == input_line_pointer)
343 {
344 radix = 10;
345 suffix = NULL;
346 }
347 else
348 {
349 c = *--suffix;
350 if (islower ((unsigned char) c))
351 c = toupper (c);
352 if (c == 'B')
353 radix = 2;
354 else if (c == 'D')
355 radix = 10;
356 else if (c == 'O' || c == 'Q')
357 radix = 8;
358 else if (c == 'H')
359 radix = 16;
360 else if (suffix[1] == '.' || c == 'E' || flt)
361 {
362 floating_constant (expressionP);
363 return;
364 }
365 else
366 {
367 radix = 10;
368 suffix = NULL;
369 }
370 }
371 }
372
373 switch (radix)
374 {
375 case 2:
376 maxdig = 2;
377 too_many_digits = valuesize + 1;
378 break;
379 case 8:
380 maxdig = radix = 8;
381 too_many_digits = (valuesize + 2) / 3 + 1;
382 break;
383 case 16:
384 maxdig = radix = 16;
385 too_many_digits = (valuesize + 3) / 4 + 1;
386 break;
387 case 10:
388 maxdig = radix = 10;
389 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
390 }
391 #undef valuesize
392 start = input_line_pointer;
393 c = *input_line_pointer++;
394 for (number = 0;
395 (digit = hex_value (c)) < maxdig;
396 c = *input_line_pointer++)
397 {
398 number = number * radix + digit;
399 }
400 /* c contains character after number. */
401 /* input_line_pointer->char after c. */
402 small = (input_line_pointer - start - 1) < too_many_digits;
403
404 if (radix == 16 && c == '_')
405 {
406 /* This is literal of the form 0x333_0_12345678_1.
407 This example is equivalent to 0x00000333000000001234567800000001. */
408
409 int num_little_digits = 0;
410 int i;
411 input_line_pointer = start; /* -> 1st digit. */
412
413 know (LITTLENUM_NUMBER_OF_BITS == 16);
414
415 for (c = '_'; c == '_'; num_little_digits += 2)
416 {
417
418 /* Convert one 64-bit word. */
419 int ndigit = 0;
420 number = 0;
421 for (c = *input_line_pointer++;
422 (digit = hex_value (c)) < maxdig;
423 c = *(input_line_pointer++))
424 {
425 number = number * radix + digit;
426 ndigit++;
427 }
428
429 /* Check for 8 digit per word max. */
430 if (ndigit > 8)
431 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
432
433 /* Add this chunk to the bignum.
434 Shift things down 2 little digits. */
435 know (LITTLENUM_NUMBER_OF_BITS == 16);
436 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
437 i >= 2;
438 i--)
439 generic_bignum[i] = generic_bignum[i - 2];
440
441 /* Add the new digits as the least significant new ones. */
442 generic_bignum[0] = number & 0xffffffff;
443 generic_bignum[1] = number >> 16;
444 }
445
446 /* Again, c is char after number, input_line_pointer->after c. */
447
448 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
449 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
450
451 assert (num_little_digits >= 4);
452
453 if (num_little_digits != 8)
454 as_bad (_("a bignum with underscores must have exactly 4 words"));
455
456 /* We might have some leading zeros. These can be trimmed to give
457 us a change to fit this constant into a small number. */
458 while (generic_bignum[num_little_digits - 1] == 0
459 && num_little_digits > 1)
460 num_little_digits--;
461
462 if (num_little_digits <= 2)
463 {
464 /* will fit into 32 bits. */
465 number = generic_bignum_to_int32 ();
466 small = 1;
467 }
468 #ifdef BFD64
469 else if (num_little_digits <= 4)
470 {
471 /* Will fit into 64 bits. */
472 number = generic_bignum_to_int64 ();
473 small = 1;
474 }
475 #endif
476 else
477 {
478 small = 0;
479
480 /* Number of littlenums in the bignum. */
481 number = num_little_digits;
482 }
483 }
484 else if (!small)
485 {
486 /* We saw a lot of digits. manufacture a bignum the hard way. */
487 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
488 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
489 long carry;
490
491 leader = generic_bignum;
492 generic_bignum[0] = 0;
493 generic_bignum[1] = 0;
494 generic_bignum[2] = 0;
495 generic_bignum[3] = 0;
496 input_line_pointer = start; /* -> 1st digit. */
497 c = *input_line_pointer++;
498 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
499 {
500 for (pointer = generic_bignum; pointer <= leader; pointer++)
501 {
502 long work;
503
504 work = carry + radix * *pointer;
505 *pointer = work & LITTLENUM_MASK;
506 carry = work >> LITTLENUM_NUMBER_OF_BITS;
507 }
508 if (carry)
509 {
510 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
511 {
512 /* Room to grow a longer bignum. */
513 *++leader = carry;
514 }
515 }
516 }
517 /* Again, c is char after number. */
518 /* input_line_pointer -> after c. */
519 know (LITTLENUM_NUMBER_OF_BITS == 16);
520 if (leader < generic_bignum + 2)
521 {
522 /* Will fit into 32 bits. */
523 number = generic_bignum_to_int32 ();
524 small = 1;
525 }
526 #ifdef BFD64
527 else if (leader < generic_bignum + 4)
528 {
529 /* Will fit into 64 bits. */
530 number = generic_bignum_to_int64 ();
531 small = 1;
532 }
533 #endif
534 else
535 {
536 /* Number of littlenums in the bignum. */
537 number = leader - generic_bignum + 1;
538 }
539 }
540
541 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
542 && suffix != NULL
543 && input_line_pointer - 1 == suffix)
544 c = *input_line_pointer++;
545
546 if (small)
547 {
548 /* Here with number, in correct radix. c is the next char.
549 Note that unlike un*x, we allow "011f" "0x9f" to both mean
550 the same as the (conventional) "9f".
551 This is simply easier than checking for strict canonical
552 form. Syntax sux! */
553
554 if (LOCAL_LABELS_FB && c == 'b')
555 {
556 /* Backward ref to local label.
557 Because it is backward, expect it to be defined. */
558 /* Construct a local label. */
559 name = fb_label_name ((int) number, 0);
560
561 /* Seen before, or symbol is defined: OK. */
562 symbolP = symbol_find (name);
563 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
564 {
565 /* Local labels are never absolute. Don't waste time
566 checking absoluteness. */
567 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
568
569 expressionP->X_op = O_symbol;
570 expressionP->X_add_symbol = symbolP;
571 }
572 else
573 {
574 /* Either not seen or not defined. */
575 /* @@ Should print out the original string instead of
576 the parsed number. */
577 as_bad (_("backward ref to unknown label \"%d:\""),
578 (int) number);
579 expressionP->X_op = O_constant;
580 }
581
582 expressionP->X_add_number = 0;
583 } /* case 'b' */
584 else if (LOCAL_LABELS_FB && c == 'f')
585 {
586 /* Forward reference. Expect symbol to be undefined or
587 unknown. undefined: seen it before. unknown: never seen
588 it before.
589
590 Construct a local label name, then an undefined symbol.
591 Don't create a xseg frag for it: caller may do that.
592 Just return it as never seen before. */
593 name = fb_label_name ((int) number, 1);
594 symbolP = symbol_find_or_make (name);
595 /* We have no need to check symbol properties. */
596 #ifndef many_segments
597 /* Since "know" puts its arg into a "string", we
598 can't have newlines in the argument. */
599 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
600 #endif
601 expressionP->X_op = O_symbol;
602 expressionP->X_add_symbol = symbolP;
603 expressionP->X_add_number = 0;
604 } /* case 'f' */
605 else if (LOCAL_LABELS_DOLLAR && c == '$')
606 {
607 /* If the dollar label is *currently* defined, then this is just
608 another reference to it. If it is not *currently* defined,
609 then this is a fresh instantiation of that number, so create
610 it. */
611
612 if (dollar_label_defined ((long) number))
613 {
614 name = dollar_label_name ((long) number, 0);
615 symbolP = symbol_find (name);
616 know (symbolP != NULL);
617 }
618 else
619 {
620 name = dollar_label_name ((long) number, 1);
621 symbolP = symbol_find_or_make (name);
622 }
623
624 expressionP->X_op = O_symbol;
625 expressionP->X_add_symbol = symbolP;
626 expressionP->X_add_number = 0;
627 } /* case '$' */
628 else
629 {
630 expressionP->X_op = O_constant;
631 #ifdef TARGET_WORD_SIZE
632 /* Sign extend NUMBER. */
633 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
634 #endif
635 expressionP->X_add_number = number;
636 input_line_pointer--; /* Restore following character. */
637 } /* Really just a number. */
638 }
639 else
640 {
641 /* Not a small number. */
642 expressionP->X_op = O_big;
643 expressionP->X_add_number = number; /* Number of littlenums. */
644 input_line_pointer--; /* -> char following number. */
645 }
646 }
647
648 /* Parse an MRI multi character constant. */
649
650 static void
651 mri_char_constant (expressionP)
652 expressionS *expressionP;
653 {
654 int i;
655
656 if (*input_line_pointer == '\''
657 && input_line_pointer[1] != '\'')
658 {
659 expressionP->X_op = O_constant;
660 expressionP->X_add_number = 0;
661 return;
662 }
663
664 /* In order to get the correct byte ordering, we must build the
665 number in reverse. */
666 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
667 {
668 int j;
669
670 generic_bignum[i] = 0;
671 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
672 {
673 if (*input_line_pointer == '\'')
674 {
675 if (input_line_pointer[1] != '\'')
676 break;
677 ++input_line_pointer;
678 }
679 generic_bignum[i] <<= 8;
680 generic_bignum[i] += *input_line_pointer;
681 ++input_line_pointer;
682 }
683
684 if (i < SIZE_OF_LARGE_NUMBER - 1)
685 {
686 /* If there is more than one littlenum, left justify the
687 last one to make it match the earlier ones. If there is
688 only one, we can just use the value directly. */
689 for (; j < CHARS_PER_LITTLENUM; j++)
690 generic_bignum[i] <<= 8;
691 }
692
693 if (*input_line_pointer == '\''
694 && input_line_pointer[1] != '\'')
695 break;
696 }
697
698 if (i < 0)
699 {
700 as_bad (_("character constant too large"));
701 i = 0;
702 }
703
704 if (i > 0)
705 {
706 int c;
707 int j;
708
709 c = SIZE_OF_LARGE_NUMBER - i;
710 for (j = 0; j < c; j++)
711 generic_bignum[j] = generic_bignum[i + j];
712 i = c;
713 }
714
715 know (LITTLENUM_NUMBER_OF_BITS == 16);
716 if (i > 2)
717 {
718 expressionP->X_op = O_big;
719 expressionP->X_add_number = i;
720 }
721 else
722 {
723 expressionP->X_op = O_constant;
724 if (i < 2)
725 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
726 else
727 expressionP->X_add_number =
728 (((generic_bignum[1] & LITTLENUM_MASK)
729 << LITTLENUM_NUMBER_OF_BITS)
730 | (generic_bignum[0] & LITTLENUM_MASK));
731 }
732
733 /* Skip the final closing quote. */
734 ++input_line_pointer;
735 }
736
737 /* Return an expression representing the current location. This
738 handles the magic symbol `.'. */
739
740 static void
741 current_location (expressionp)
742 expressionS *expressionp;
743 {
744 if (now_seg == absolute_section)
745 {
746 expressionp->X_op = O_constant;
747 expressionp->X_add_number = abs_section_offset;
748 }
749 else
750 {
751 symbolS *symbolp;
752
753 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
754 (valueT) frag_now_fix (),
755 frag_now);
756 expressionp->X_op = O_symbol;
757 expressionp->X_add_symbol = symbolp;
758 expressionp->X_add_number = 0;
759 }
760 }
761
762 /* In: Input_line_pointer points to 1st char of operand, which may
763 be a space.
764
765 Out: A expressionS.
766 The operand may have been empty: in this case X_op == O_absent.
767 Input_line_pointer->(next non-blank) char after operand. */
768
769 static segT
770 operand (expressionP)
771 expressionS *expressionP;
772 {
773 char c;
774 symbolS *symbolP; /* Points to symbol. */
775 char *name; /* Points to name of symbol. */
776 segT segment;
777
778 /* All integers are regarded as unsigned unless they are negated.
779 This is because the only thing which cares whether a number is
780 unsigned is the code in emit_expr which extends constants into
781 bignums. It should only sign extend negative numbers, so that
782 something like ``.quad 0x80000000'' is not sign extended even
783 though it appears negative if valueT is 32 bits. */
784 expressionP->X_unsigned = 1;
785
786 /* Digits, assume it is a bignum. */
787
788 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
789 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
790
791 if (is_end_of_line[(unsigned char) c])
792 goto eol;
793
794 switch (c)
795 {
796 case '1':
797 case '2':
798 case '3':
799 case '4':
800 case '5':
801 case '6':
802 case '7':
803 case '8':
804 case '9':
805 input_line_pointer--;
806
807 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
808 ? 0 : 10,
809 expressionP);
810 break;
811
812 #ifdef LITERAL_PREFIXDOLLAR_HEX
813 case '$':
814 /* $L is the start of a local label, not a hex constant. */
815 if (* input_line_pointer == 'L')
816 goto isname;
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'"), 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, &c))
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 \f
1348 /* Internal. Simplify a struct expression for use by expr (). */
1349
1350 /* In: address of a expressionS.
1351 The X_op field of the expressionS may only take certain values.
1352 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1353
1354 Out: expressionS may have been modified:
1355 'foo-foo' symbol references cancelled to 0, which changes X_op
1356 from O_subtract to O_constant.
1357 Unused fields zeroed to help expr (). */
1358
1359 static void
1360 clean_up_expression (expressionP)
1361 expressionS *expressionP;
1362 {
1363 switch (expressionP->X_op)
1364 {
1365 case O_illegal:
1366 case O_absent:
1367 expressionP->X_add_number = 0;
1368 /* Fall through. */
1369 case O_big:
1370 case O_constant:
1371 case O_register:
1372 expressionP->X_add_symbol = NULL;
1373 /* Fall through. */
1374 case O_symbol:
1375 case O_uminus:
1376 case O_bit_not:
1377 expressionP->X_op_symbol = NULL;
1378 break;
1379 case O_subtract:
1380 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1381 || ((symbol_get_frag (expressionP->X_op_symbol)
1382 == symbol_get_frag (expressionP->X_add_symbol))
1383 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1384 && (S_GET_VALUE (expressionP->X_op_symbol)
1385 == S_GET_VALUE (expressionP->X_add_symbol))))
1386 {
1387 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1388 - S_GET_VALUE (expressionP->X_op_symbol));
1389
1390 expressionP->X_op = O_constant;
1391 expressionP->X_add_symbol = NULL;
1392 expressionP->X_op_symbol = NULL;
1393 expressionP->X_add_number += diff;
1394 }
1395 break;
1396 default:
1397 break;
1398 }
1399 }
1400 \f
1401 /* Expression parser. */
1402
1403 /* We allow an empty expression, and just assume (absolute,0) silently.
1404 Unary operators and parenthetical expressions are treated as operands.
1405 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1406
1407 We used to do a aho/ullman shift-reduce parser, but the logic got so
1408 warped that I flushed it and wrote a recursive-descent parser instead.
1409 Now things are stable, would anybody like to write a fast parser?
1410 Most expressions are either register (which does not even reach here)
1411 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1412 So I guess it doesn't really matter how inefficient more complex expressions
1413 are parsed.
1414
1415 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1416 Also, we have consumed any leading or trailing spaces (operand does that)
1417 and done all intervening operators.
1418
1419 This returns the segment of the result, which will be
1420 absolute_section or the segment of a symbol. */
1421
1422 #undef __
1423 #define __ O_illegal
1424
1425 /* Maps ASCII -> operators. */
1426 static const operatorT op_encoding[256] = {
1427 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1428 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1429
1430 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1431 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1432 __, __, __, __, __, __, __, __,
1433 __, __, __, __, O_lt, __, O_gt, __,
1434 __, __, __, __, __, __, __, __,
1435 __, __, __, __, __, __, __, __,
1436 __, __, __, __, __, __, __, __,
1437 __, __, __,
1438 #ifdef NEED_INDEX_OPERATOR
1439 O_index,
1440 #else
1441 __,
1442 #endif
1443 __, __, O_bit_exclusive_or, __,
1444 __, __, __, __, __, __, __, __,
1445 __, __, __, __, __, __, __, __,
1446 __, __, __, __, __, __, __, __,
1447 __, __, __, __, O_bit_inclusive_or, __, __, __,
1448
1449 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1450 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1451 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1452 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1453 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1454 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1455 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1456 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1457 };
1458
1459 /* Rank Examples
1460 0 operand, (expression)
1461 1 ||
1462 2 &&
1463 3 == <> < <= >= >
1464 4 + -
1465 5 used for * / % in MRI mode
1466 6 & ^ ! |
1467 7 * / % << >>
1468 8 unary - unary ~
1469 */
1470 static operator_rankT op_rank[] = {
1471 0, /* O_illegal */
1472 0, /* O_absent */
1473 0, /* O_constant */
1474 0, /* O_symbol */
1475 0, /* O_symbol_rva */
1476 0, /* O_register */
1477 0, /* O_big */
1478 9, /* O_uminus */
1479 9, /* O_bit_not */
1480 9, /* O_logical_not */
1481 8, /* O_multiply */
1482 8, /* O_divide */
1483 8, /* O_modulus */
1484 8, /* O_left_shift */
1485 8, /* O_right_shift */
1486 7, /* O_bit_inclusive_or */
1487 7, /* O_bit_or_not */
1488 7, /* O_bit_exclusive_or */
1489 7, /* O_bit_and */
1490 5, /* O_add */
1491 5, /* O_subtract */
1492 4, /* O_eq */
1493 4, /* O_ne */
1494 4, /* O_lt */
1495 4, /* O_le */
1496 4, /* O_ge */
1497 4, /* O_gt */
1498 3, /* O_logical_and */
1499 2, /* O_logical_or */
1500 1, /* O_index */
1501 0, /* O_md1 */
1502 0, /* O_md2 */
1503 0, /* O_md3 */
1504 0, /* O_md4 */
1505 0, /* O_md5 */
1506 0, /* O_md6 */
1507 0, /* O_md7 */
1508 0, /* O_md8 */
1509 0, /* O_md9 */
1510 0, /* O_md10 */
1511 0, /* O_md11 */
1512 0, /* O_md12 */
1513 0, /* O_md13 */
1514 0, /* O_md14 */
1515 0, /* O_md15 */
1516 0, /* O_md16 */
1517 };
1518
1519 /* Unfortunately, in MRI mode for the m68k, multiplication and
1520 division have lower precedence than the bit wise operators. This
1521 function sets the operator precedences correctly for the current
1522 mode. Also, MRI uses a different bit_not operator, and this fixes
1523 that as well. */
1524
1525 #define STANDARD_MUL_PRECEDENCE 8
1526 #define MRI_MUL_PRECEDENCE 6
1527
1528 void
1529 expr_set_precedence ()
1530 {
1531 if (flag_m68k_mri)
1532 {
1533 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1534 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1535 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1536 }
1537 else
1538 {
1539 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1540 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1541 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1542 }
1543 }
1544
1545 /* Initialize the expression parser. */
1546
1547 void
1548 expr_begin ()
1549 {
1550 expr_set_precedence ();
1551
1552 /* Verify that X_op field is wide enough. */
1553 {
1554 expressionS e;
1555 e.X_op = O_max;
1556 assert (e.X_op == O_max);
1557 }
1558 }
1559 \f
1560 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1561 sets NUM_CHARS to the number of characters in the operator.
1562 Does not advance INPUT_LINE_POINTER. */
1563
1564 static inline operatorT
1565 operator (num_chars)
1566 int *num_chars;
1567 {
1568 int c;
1569 operatorT ret;
1570
1571 c = *input_line_pointer & 0xff;
1572 *num_chars = 1;
1573
1574 if (is_end_of_line[c])
1575 return O_illegal;
1576
1577 switch (c)
1578 {
1579 default:
1580 return op_encoding[c];
1581
1582 case '<':
1583 switch (input_line_pointer[1])
1584 {
1585 default:
1586 return op_encoding[c];
1587 case '<':
1588 ret = O_left_shift;
1589 break;
1590 case '>':
1591 ret = O_ne;
1592 break;
1593 case '=':
1594 ret = O_le;
1595 break;
1596 }
1597 *num_chars = 2;
1598 return ret;
1599
1600 case '=':
1601 if (input_line_pointer[1] != '=')
1602 return op_encoding[c];
1603
1604 *num_chars = 2;
1605 return O_eq;
1606
1607 case '>':
1608 switch (input_line_pointer[1])
1609 {
1610 default:
1611 return op_encoding[c];
1612 case '>':
1613 ret = O_right_shift;
1614 break;
1615 case '=':
1616 ret = O_ge;
1617 break;
1618 }
1619 *num_chars = 2;
1620 return ret;
1621
1622 case '!':
1623 /* We accept !! as equivalent to ^ for MRI compatibility. */
1624 if (input_line_pointer[1] != '!')
1625 {
1626 if (flag_m68k_mri)
1627 return O_bit_inclusive_or;
1628 return op_encoding[c];
1629 }
1630 *num_chars = 2;
1631 return O_bit_exclusive_or;
1632
1633 case '|':
1634 if (input_line_pointer[1] != '|')
1635 return op_encoding[c];
1636
1637 *num_chars = 2;
1638 return O_logical_or;
1639
1640 case '&':
1641 if (input_line_pointer[1] != '&')
1642 return op_encoding[c];
1643
1644 *num_chars = 2;
1645 return O_logical_and;
1646 }
1647
1648 /* NOTREACHED */
1649 }
1650
1651 /* Parse an expression. */
1652
1653 segT
1654 expr (rankarg, resultP)
1655 int rankarg; /* Larger # is higher rank. */
1656 expressionS *resultP; /* Deliver result here. */
1657 {
1658 operator_rankT rank = (operator_rankT) rankarg;
1659 segT retval;
1660 expressionS right;
1661 operatorT op_left;
1662 operatorT op_right;
1663 int op_chars;
1664
1665 know (rank >= 0);
1666
1667 retval = operand (resultP);
1668
1669 /* operand () gobbles spaces. */
1670 know (*input_line_pointer != ' ');
1671
1672 op_left = operator (&op_chars);
1673 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1674 {
1675 segT rightseg;
1676
1677 input_line_pointer += op_chars; /* -> after operator. */
1678
1679 rightseg = expr (op_rank[(int) op_left], &right);
1680 if (right.X_op == O_absent)
1681 {
1682 as_warn (_("missing operand; zero assumed"));
1683 right.X_op = O_constant;
1684 right.X_add_number = 0;
1685 right.X_add_symbol = NULL;
1686 right.X_op_symbol = NULL;
1687 }
1688
1689 know (*input_line_pointer != ' ');
1690
1691 if (op_left == O_index)
1692 {
1693 if (*input_line_pointer != ']')
1694 as_bad ("missing right bracket");
1695 else
1696 {
1697 ++input_line_pointer;
1698 SKIP_WHITESPACE ();
1699 }
1700 }
1701
1702 op_right = operator (&op_chars);
1703
1704 know (op_right == O_illegal
1705 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1706 know ((int) op_left >= (int) O_multiply
1707 && (int) op_left <= (int) O_logical_or);
1708
1709 /* input_line_pointer->after right-hand quantity. */
1710 /* left-hand quantity in resultP. */
1711 /* right-hand quantity in right. */
1712 /* operator in op_left. */
1713
1714 if (resultP->X_op == O_big)
1715 {
1716 if (resultP->X_add_number > 0)
1717 as_warn (_("left operand is a bignum; integer 0 assumed"));
1718 else
1719 as_warn (_("left operand is a float; integer 0 assumed"));
1720 resultP->X_op = O_constant;
1721 resultP->X_add_number = 0;
1722 resultP->X_add_symbol = NULL;
1723 resultP->X_op_symbol = NULL;
1724 }
1725 if (right.X_op == O_big)
1726 {
1727 if (right.X_add_number > 0)
1728 as_warn (_("right operand is a bignum; integer 0 assumed"));
1729 else
1730 as_warn (_("right operand is a float; integer 0 assumed"));
1731 right.X_op = O_constant;
1732 right.X_add_number = 0;
1733 right.X_add_symbol = NULL;
1734 right.X_op_symbol = NULL;
1735 }
1736
1737 /* Optimize common cases. */
1738 #ifdef md_optimize_expr
1739 if (md_optimize_expr (resultP, op_left, &right))
1740 {
1741 /* Skip. */
1742 ;
1743 }
1744 else
1745 #endif
1746 if (op_left == O_add && right.X_op == O_constant)
1747 {
1748 /* X + constant. */
1749 resultP->X_add_number += right.X_add_number;
1750 }
1751 /* This case comes up in PIC code. */
1752 else if (op_left == O_subtract
1753 && right.X_op == O_symbol
1754 && resultP->X_op == O_symbol
1755 && (symbol_get_frag (right.X_add_symbol)
1756 == symbol_get_frag (resultP->X_add_symbol))
1757 && SEG_NORMAL (rightseg))
1758 {
1759 resultP->X_add_number -= right.X_add_number;
1760 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1761 - S_GET_VALUE (right.X_add_symbol));
1762 resultP->X_op = O_constant;
1763 resultP->X_add_symbol = 0;
1764 }
1765 else if (op_left == O_subtract && right.X_op == O_constant)
1766 {
1767 /* X - constant. */
1768 resultP->X_add_number -= right.X_add_number;
1769 }
1770 else if (op_left == O_add && resultP->X_op == O_constant)
1771 {
1772 /* Constant + X. */
1773 resultP->X_op = right.X_op;
1774 resultP->X_add_symbol = right.X_add_symbol;
1775 resultP->X_op_symbol = right.X_op_symbol;
1776 resultP->X_add_number += right.X_add_number;
1777 retval = rightseg;
1778 }
1779 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1780 {
1781 /* Constant OP constant. */
1782 offsetT v = right.X_add_number;
1783 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1784 {
1785 as_warn (_("division by zero"));
1786 v = 1;
1787 }
1788 switch (op_left)
1789 {
1790 default: abort ();
1791 case O_multiply: resultP->X_add_number *= v; break;
1792 case O_divide: resultP->X_add_number /= v; break;
1793 case O_modulus: resultP->X_add_number %= v; break;
1794 case O_left_shift: resultP->X_add_number <<= v; break;
1795 case O_right_shift:
1796 /* We always use unsigned shifts, to avoid relying on
1797 characteristics of the compiler used to compile gas. */
1798 resultP->X_add_number =
1799 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1800 break;
1801 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1802 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1803 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1804 case O_bit_and: resultP->X_add_number &= v; break;
1805 case O_add: resultP->X_add_number += v; break;
1806 case O_subtract: resultP->X_add_number -= v; break;
1807 case O_eq:
1808 resultP->X_add_number =
1809 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1810 break;
1811 case O_ne:
1812 resultP->X_add_number =
1813 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1814 break;
1815 case O_lt:
1816 resultP->X_add_number =
1817 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1818 break;
1819 case O_le:
1820 resultP->X_add_number =
1821 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1822 break;
1823 case O_ge:
1824 resultP->X_add_number =
1825 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1826 break;
1827 case O_gt:
1828 resultP->X_add_number =
1829 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1830 break;
1831 case O_logical_and:
1832 resultP->X_add_number = resultP->X_add_number && v;
1833 break;
1834 case O_logical_or:
1835 resultP->X_add_number = resultP->X_add_number || v;
1836 break;
1837 }
1838 }
1839 else if (resultP->X_op == O_symbol
1840 && right.X_op == O_symbol
1841 && (op_left == O_add
1842 || op_left == O_subtract
1843 || (resultP->X_add_number == 0
1844 && right.X_add_number == 0)))
1845 {
1846 /* Symbol OP symbol. */
1847 resultP->X_op = op_left;
1848 resultP->X_op_symbol = right.X_add_symbol;
1849 if (op_left == O_add)
1850 resultP->X_add_number += right.X_add_number;
1851 else if (op_left == O_subtract)
1852 {
1853 resultP->X_add_number -= right.X_add_number;
1854 if (retval == rightseg && SEG_NORMAL (retval))
1855 {
1856 retval = absolute_section;
1857 rightseg = absolute_section;
1858 }
1859 }
1860 }
1861 else
1862 {
1863 /* The general case. */
1864 resultP->X_add_symbol = make_expr_symbol (resultP);
1865 resultP->X_op_symbol = make_expr_symbol (&right);
1866 resultP->X_op = op_left;
1867 resultP->X_add_number = 0;
1868 resultP->X_unsigned = 1;
1869 }
1870
1871 if (retval != rightseg)
1872 {
1873 if (! SEG_NORMAL (retval))
1874 {
1875 if (retval != undefined_section || SEG_NORMAL (rightseg))
1876 retval = rightseg;
1877 }
1878 else if (SEG_NORMAL (rightseg)
1879 #ifdef DIFF_EXPR_OK
1880 && op_left != O_subtract
1881 #endif
1882 )
1883 as_bad (_("operation combines symbols in different segments"));
1884 }
1885
1886 op_left = op_right;
1887 } /* While next operator is >= this rank. */
1888
1889 /* The PA port needs this information. */
1890 if (resultP->X_add_symbol)
1891 symbol_mark_used (resultP->X_add_symbol);
1892
1893 return resultP->X_op == O_constant ? absolute_section : retval;
1894 }
1895 \f
1896 /* This lives here because it belongs equally in expr.c & read.c.
1897 expr.c is just a branch office read.c anyway, and putting it
1898 here lessens the crowd at read.c.
1899
1900 Assume input_line_pointer is at start of symbol name.
1901 Advance input_line_pointer past symbol name.
1902 Turn that character into a '\0', returning its former value.
1903 This allows a string compare (RMS wants symbol names to be strings)
1904 of the symbol name.
1905 There will always be a char following symbol name, because all good
1906 lines end in end-of-line. */
1907
1908 char
1909 get_symbol_end ()
1910 {
1911 char c;
1912
1913 /* We accept \001 in a name in case this is being called with a
1914 constructed string. */
1915 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1916 {
1917 while (is_part_of_name (c = *input_line_pointer++)
1918 || c == '\001')
1919 ;
1920 if (is_name_ender (c))
1921 c = *input_line_pointer++;
1922 }
1923 *--input_line_pointer = 0;
1924 return (c);
1925 }
1926
1927 unsigned int
1928 get_single_number ()
1929 {
1930 expressionS exp;
1931 operand (&exp);
1932 return exp.X_add_number;
1933 }
This page took 0.139083 seconds and 5 git commands to generate.