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