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