move valueT typedef to as.h from struc-symbol.h
[deliverable/binutils-gdb.git] / gas / expr.c
CommitLineData
fecd2382 1/* expr.c -operands, expressions-
f2f7d044 2 Copyright (C) 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
2ed83a59 3
a39116f1 4 This file is part of GAS, the GNU Assembler.
2ed83a59 5
a39116f1
RP
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
2ed83a59 10
a39116f1
RP
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
2ed83a59 15
a39116f1
RP
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
fecd2382
RP
19
20/*
21 * This is really a branch office of as-read.c. I split it out to clearly
22 * distinguish the world of expressions from the world of statements.
23 * (It also gives smaller files to re-compile.)
24 * Here, "operand"s are of expressions, not instructions.
25 */
26
27#include <ctype.h>
28#include <string.h>
29
30#include "as.h"
31
32#include "obstack.h"
33
f2f7d044
ILT
34static void clean_up_expression PARAMS ((expressionS * expressionP));
35extern const char EXP_CHARS[], FLT_CHARS[];
fecd2382 36
fecd2382
RP
37/*
38 * Build any floating-point literal here.
39 * Also build any bignum literal here.
40 */
41
fecd2382
RP
42/* Seems atof_machine can backscan through generic_bignum and hit whatever
43 happens to be loaded before it in memory. And its way too complicated
44 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
45 and never write into the early words, thus they'll always be zero.
f2f7d044 46 I hate Dean's floating-point code. Bleh. */
2ed83a59
KR
47LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
48FLONUM_TYPE generic_floating_point_number =
fecd2382 49{
2ed83a59
KR
50 &generic_bignum[6], /* low (JF: Was 0) */
51 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
52 0, /* leader */
53 0, /* exponent */
54 0 /* sign */
55};
fecd2382
RP
56/* If nonzero, we've been asked to assemble nan, +inf or -inf */
57int generic_floating_point_magic;
58\f
2ed83a59
KR
59floating_constant (expressionP)
60 expressionS *expressionP;
c593cf41
SC
61{
62 /* input_line_pointer->*/
63 /* floating-point constant. */
64 int error_code;
65
66 error_code = atof_generic
2ed83a59
KR
67 (&input_line_pointer, ".", EXP_CHARS,
68 &generic_floating_point_number);
c593cf41
SC
69
70 if (error_code)
c593cf41 71 {
2ed83a59
KR
72 if (error_code == ERROR_EXPONENT_OVERFLOW)
73 {
74 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
75 }
76 else
77 {
78 as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
79 }
c593cf41 80 }
f2f7d044 81 expressionP->X_seg = big_section;
c593cf41
SC
82 /* input_line_pointer->just after constant, */
83 /* which may point to whitespace. */
2ed83a59 84 expressionP->X_add_number = -1;
c593cf41
SC
85}
86
87
88
2ed83a59
KR
89integer_constant (radix, expressionP)
90 int radix;
91 expressionS *expressionP;
c593cf41 92{
2ed83a59 93 register char *digit_2; /*->2nd digit of number. */
c593cf41 94 char c;
2ed83a59
KR
95
96 register valueT number; /* offset or (absolute) value */
97 register short int digit; /* value of next digit in current radix */
98 register short int maxdig = 0;/* highest permitted digit value. */
99 register int too_many_digits = 0; /* if we see >= this number of */
100 register char *name; /* points to name of symbol */
101 register symbolS *symbolP; /* points to symbol */
102
103 int small; /* true if fits in 32 bits. */
f2f7d044 104 extern const char hex_value[]; /* in hex_value.c */
2ed83a59 105
c593cf41
SC
106 /* may be bignum, or may fit in 32 bits. */
107 /*
108 * most numbers fit into 32 bits, and we want this case to be fast.
109 * so we pretend it will fit into 32 bits. if, after making up a 32
110 * bit number, we realise that we have scanned more digits than
111 * comfortably fit into 32 bits, we re-scan the digits coding
112 * them into a bignum. for decimal and octal numbers we are conservative: some
113 * numbers may be assumed bignums when in fact they do fit into 32 bits.
114 * numbers of any radix can have excess leading zeros: we strive
115 * to recognise this and cast them back into 32 bits.
116 * we must check that the bignum really is more than 32
117 * bits, and change it back to a 32-bit number if it fits.
118 * the number we are looking for is expected to be positive, but
119 * if it fits into 32 bits as an unsigned number, we let it be a 32-bit
120 * number. the cavalier approach is for speed in ordinary cases.
121 */
2ed83a59
KR
122
123 switch (radix)
f8701a3f 124 {
2ed83a59 125
f8701a3f
SC
126 case 2:
127 maxdig = 2;
128 too_many_digits = 33;
129 break;
130 case 8:
131 maxdig = radix = 8;
132 too_many_digits = 11;
133 break;
134 case 16:
2ed83a59
KR
135
136
f8701a3f
SC
137 maxdig = radix = 16;
138 too_many_digits = 9;
139 break;
140 case 10:
141 maxdig = radix = 10;
142 too_many_digits = 11;
143 }
c593cf41
SC
144 c = *input_line_pointer;
145 input_line_pointer++;
146 digit_2 = input_line_pointer;
2ed83a59 147 for (number = 0; (digit = hex_value[c]) < maxdig; c = *input_line_pointer++)
f8701a3f
SC
148 {
149 number = number * radix + digit;
150 }
c593cf41
SC
151 /* c contains character after number. */
152 /* input_line_pointer->char after c. */
153 small = input_line_pointer - digit_2 < too_many_digits;
2ed83a59 154 if (!small)
c593cf41 155 {
f8701a3f
SC
156 /*
157 * we saw a lot of digits. manufacture a bignum the hard way.
158 */
2ed83a59
KR
159 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
160 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
f8701a3f 161 long carry;
2ed83a59 162
f8701a3f 163 leader = generic_bignum;
2ed83a59
KR
164 generic_bignum[0] = 0;
165 generic_bignum[1] = 0;
f8701a3f 166 /* we could just use digit_2, but lets be mnemonic. */
2ed83a59 167 input_line_pointer = --digit_2; /*->1st digit. */
f8701a3f 168 c = *input_line_pointer++;
2ed83a59 169 for (; (carry = hex_value[c]) < maxdig; c = *input_line_pointer++)
f8701a3f
SC
170 {
171 for (pointer = generic_bignum;
172 pointer <= leader;
173 pointer++)
174 {
175 long work;
2ed83a59
KR
176
177 work = carry + radix * *pointer;
f8701a3f
SC
178 *pointer = work & LITTLENUM_MASK;
179 carry = work >> LITTLENUM_NUMBER_OF_BITS;
180 }
181 if (carry)
182 {
183 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
2ed83a59 184 { /* room to grow a longer bignum. */
f8701a3f
SC
185 *++leader = carry;
186 }
187 }
188 }
189 /* again, c is char after number, */
190 /* input_line_pointer->after c. */
2ed83a59
KR
191 know (sizeof (int) * 8 == 32);
192 know (LITTLENUM_NUMBER_OF_BITS == 16);
f8701a3f
SC
193 /* hence the constant "2" in the next line. */
194 if (leader < generic_bignum + 2)
2ed83a59 195 { /* will fit into 32 bits. */
f8701a3f 196 number =
2ed83a59
KR
197 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
198 | (generic_bignum[0] & LITTLENUM_MASK);
f8701a3f
SC
199 small = 1;
200 }
201 else
202 {
2ed83a59 203 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
c593cf41 204 }
c593cf41 205 }
2ed83a59
KR
206 if (small)
207 {
f8701a3f 208 /*
2ed83a59
KR
209 * here with number, in correct radix. c is the next char.
210 * note that unlike un*x, we allow "011f" "0x9f" to
211 * both mean the same as the (conventional) "9f". this is simply easier
212 * than checking for strict canonical form. syntax sux!
f8701a3f 213 */
2ed83a59
KR
214
215 switch (c)
216 {
217
218#ifdef LOCAL_LABELS_FB
219 case 'b':
220 {
221 /*
222 * backward ref to local label.
223 * because it is backward, expect it to be defined.
224 */
225 /* Construct a local label. */
226 name = fb_label_name ((int) number, 0);
227
228 /* seen before, or symbol is defined: ok */
229 symbolP = symbol_find (name);
230 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
231 {
232
233 /* local labels are never absolute. don't waste time
234 checking absoluteness. */
235 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
236
237 expressionP->X_add_symbol = symbolP;
238 expressionP->X_seg = S_GET_SEGMENT (symbolP);
239
240 }
241 else
242 { /* either not seen or not defined. */
243 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.", number);
f2f7d044 244 expressionP->X_seg = absolute_section;
2ed83a59
KR
245 }
246
247 expressionP->X_add_number = 0;
248 break;
249 } /* case 'b' */
250
251 case 'f':
252 {
253 /*
254 * forward reference. expect symbol to be undefined or
255 * unknown. undefined: seen it before. unknown: never seen
256 * it before.
257 * construct a local label name, then an undefined symbol.
258 * don't create a xseg frag for it: caller may do that.
259 * just return it as never seen before.
260 */
261 name = fb_label_name ((int) number, 1);
262 symbolP = symbol_find_or_make (name);
263 /* we have no need to check symbol properties. */
c593cf41 264#ifndef many_segments
2ed83a59
KR
265 /* since "know" puts its arg into a "string", we
266 can't have newlines in the argument. */
f2f7d044 267 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
c593cf41 268#endif
2ed83a59 269 expressionP->X_add_symbol = symbolP;
f2f7d044 270 expressionP->X_seg = undefined_section;
2ed83a59
KR
271 expressionP->X_subtract_symbol = NULL;
272 expressionP->X_add_number = 0;
273
274 break;
275 } /* case 'f' */
276
f8701a3f 277#endif /* LOCAL_LABELS_FB */
2ed83a59 278
f8701a3f 279#ifdef LOCAL_LABELS_DOLLAR
f8701a3f 280
2ed83a59
KR
281 case '$':
282 {
283
284 /* If the dollar label is *currently* defined, then this is just
285 another reference to it. If it is not *currently* defined,
286 then this is a fresh instantiation of that number, so create
287 it. */
288
289 if (dollar_label_defined (number))
290 {
291 name = dollar_label_name (number, 0);
292 symbolP = symbol_find (name);
293 know (symbolP != NULL);
294 }
295 else
296 {
297 name = dollar_label_name (number, 1);
298 symbolP = symbol_find_or_make (name);
299 }
300
301 expressionP->X_add_symbol = symbolP;
302 expressionP->X_add_number = 0;
303 expressionP->X_seg = S_GET_SEGMENT (symbolP);
304
305 break;
306 } /* case '$' */
307
f8701a3f 308#endif /* LOCAL_LABELS_DOLLAR */
2ed83a59
KR
309
310 default:
311 {
312 expressionP->X_add_number = number;
f2f7d044 313 expressionP->X_seg = absolute_section;
2ed83a59
KR
314 input_line_pointer--; /* restore following character. */
315 break;
316 } /* really just a number */
317
318 } /* switch on char following the number */
319
320
321 }
322 else
323 { /* not a small number */
c593cf41 324 expressionP->X_add_number = number;
f2f7d044 325 expressionP->X_seg = big_section;
2ed83a59
KR
326 input_line_pointer--; /*->char following number. */
327 } /* if (small) */
328} /* integer_constant() */
c593cf41
SC
329
330
fecd2382
RP
331/*
332 * Summary of operand().
333 *
334 * in: Input_line_pointer points to 1st char of operand, which may
335 * be a space.
336 *
337 * out: A expressionS. X_seg determines how to understand the rest of the
338 * expressionS.
339 * The operand may have been empty: in this case X_seg == SEG_ABSENT.
340 * Input_line_pointer->(next non-blank) char after operand.
341 *
342 */
343\f
c593cf41
SC
344
345
fecd2382 346static segT
c593cf41 347operand (expressionP)
2ed83a59 348 register expressionS *expressionP;
fecd2382 349{
c593cf41 350 register char c;
2ed83a59
KR
351 register symbolS *symbolP; /* points to symbol */
352 register char *name; /* points to name of symbol */
c593cf41
SC
353 /* invented for humans only, hope */
354 /* optimising compiler flushes it! */
2ed83a59 355 register short int radix; /* 2, 8, 10 or 16, 0 when floating */
c593cf41
SC
356 /* 0 means we saw start of a floating- */
357 /* point constant. */
358
359 /* digits, assume it is a bignum. */
360
2ed83a59
KR
361 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
362 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
c593cf41
SC
363
364 switch (c)
fecd2382 365 {
2ed83a59
KR
366#ifdef MRI
367 case '%':
368 integer_constant (2, expressionP);
c593cf41 369 break;
2ed83a59
KR
370 case '@':
371 integer_constant (8, expressionP);
c593cf41 372 break;
2ed83a59
KR
373 case '$':
374 integer_constant (16, expressionP);
c593cf41 375 break;
2ed83a59 376#endif
c593cf41
SC
377 case '1':
378 case '2':
379 case '3':
380 case '4':
381 case '5':
382 case '6':
383 case '7':
2ed83a59
KR
384 case '8':
385 case '9':
386 input_line_pointer--;
387
388 integer_constant (10, expressionP);
c593cf41
SC
389 break;
390
2ed83a59
KR
391 case '0':
392 /* non-decimal radix */
393
394
395 c = *input_line_pointer;
396 switch (c)
397 {
398
399 default:
400 if (c && strchr (FLT_CHARS, c))
401 {
402 input_line_pointer++;
403 floating_constant (expressionP);
404 }
405 else
406 {
407 /* The string was only zero */
408 expressionP->X_add_symbol = 0;
409 expressionP->X_add_number = 0;
f2f7d044 410 expressionP->X_seg = absolute_section;
2ed83a59
KR
411 }
412
413 break;
414
415 case 'x':
416 case 'X':
417 input_line_pointer++;
418 integer_constant (16, expressionP);
419 break;
420
421 case 'b':
422#ifdef LOCAL_LABELS_FB
423 if (!*input_line_pointer
424 || (!strchr ("+-.0123456789", *input_line_pointer)
425 && !strchr (EXP_CHARS, *input_line_pointer)))
426 {
427 input_line_pointer--;
428 integer_constant (10, expressionP);
429 break;
430 }
431#endif
432 case 'B':
433 input_line_pointer++;
434 integer_constant (2, expressionP);
435 break;
436
437 case '0':
438 case '1':
439 case '2':
440 case '3':
441 case '4':
442 case '5':
443 case '6':
444 case '7':
445 integer_constant (8, expressionP);
446 break;
447
448 case 'f':
449#ifdef LOCAL_LABELS_FB
450 /* if it says '0f' and the line ends or it doesn't look like
d841bc49 451 a floating point #, its a local label ref. dtrt */
2ed83a59
KR
452 /* likewise for the b's. xoxorich. */
453 if (c == 'f'
454 && (!*input_line_pointer ||
455 (!strchr ("+-.0123456789", *input_line_pointer) &&
456 !strchr (EXP_CHARS, *input_line_pointer))))
457 {
458 input_line_pointer -= 1;
459 integer_constant (10, expressionP);
460 break;
461 }
462#endif
463
464 case 'd':
465 case 'D':
466 case 'F':
467 case 'r':
468 case 'e':
469 case 'E':
470 case 'g':
471 case 'G':
472
473 input_line_pointer++;
474 floating_constant (expressionP);
f2f7d044 475 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
2ed83a59
KR
476 break;
477
478#ifdef LOCAL_LABELS_DOLLAR
479 case '$':
480 integer_constant (10, expressionP);
481 break;
482#endif
483 }
484
c593cf41 485 break;
2ed83a59
KR
486 case '(':
487 /* didn't begin with digit & not a name */
488 {
489 (void) expression (expressionP);
490 /* Expression() will pass trailing whitespace */
491 if (*input_line_pointer++ != ')')
492 {
493 as_bad ("Missing ')' assumed");
494 input_line_pointer--;
495 }
496 /* here with input_line_pointer->char after "(...)" */
497 }
498 return expressionP->X_seg;
c593cf41
SC
499
500
2ed83a59 501 case '\'':
d841bc49
KR
502 /* Warning: to conform to other people's assemblers NO ESCAPEMENT is
503 permitted for a single quote. The next character, parity errors and
504 all, is taken as the value of the operand. VERY KINKY. */
2ed83a59 505 expressionP->X_add_number = *input_line_pointer++;
f2f7d044 506 expressionP->X_seg = absolute_section;
2ed83a59
KR
507 break;
508
49864cfa
KR
509 case '+':
510 operand (expressionP);
511 break;
512
2ed83a59
KR
513 case '~':
514 case '-':
2ed83a59
KR
515 {
516 /* unary operator: hope for SEG_ABSOLUTE */
f2f7d044
ILT
517 segT opseg = operand (expressionP);
518 if (opseg == absolute_section)
2ed83a59 519 {
2ed83a59
KR
520 /* input_line_pointer -> char after operand */
521 if (c == '-')
522 {
523 expressionP->X_add_number = -expressionP->X_add_number;
d841bc49
KR
524 /* Notice: '-' may overflow: no warning is given. This is
525 compatible with other people's assemblers. Sigh. */
2ed83a59
KR
526 }
527 else
528 {
529 expressionP->X_add_number = ~expressionP->X_add_number;
530 }
f2f7d044
ILT
531 }
532 else if (opseg == text_section
533 || opseg == data_section
534 || opseg == bss_section
535 || opseg == pass1_section
536 || opseg == undefined_section)
537 {
2ed83a59 538 if (c == '-')
f2f7d044 539 {
2ed83a59
KR
540 expressionP->X_subtract_symbol = expressionP->X_add_symbol;
541 expressionP->X_add_symbol = 0;
f2f7d044 542 expressionP->X_seg = diff_section;
2ed83a59 543 }
f2f7d044
ILT
544 else
545 as_warn ("Unary operator %c ignored because bad operand follows",
546 c);
c593cf41 547 }
f2f7d044
ILT
548 else
549 as_warn ("Unary operator %c ignored because bad operand follows", c);
c593cf41 550 }
2ed83a59
KR
551 break;
552
553 case '.':
554 if (!is_part_of_name (*input_line_pointer))
555 {
85825401 556 char *fake;
2ed83a59
KR
557 extern struct obstack frags;
558
85825401
ILT
559 /* JF: '.' is pseudo symbol with value of current location
560 in current segment. */
561#ifdef DOT_LABEL_PREFIX
562 fake = ".L0\001";
563#else
564 fake = "L0\001";
565#endif
566 symbolP = symbol_new (fake,
2ed83a59
KR
567 now_seg,
568 (valueT) (obstack_next_free (&frags) - frag_now->fr_literal),
569 frag_now);
c593cf41 570
2ed83a59
KR
571 expressionP->X_add_number = 0;
572 expressionP->X_add_symbol = symbolP;
573 expressionP->X_seg = now_seg;
574 break;
575
576 }
577 else
578 {
579 goto isname;
580
581
582 }
583 case ',':
584 case '\n':
f2f7d044 585 case '\0':
0bd77bc4 586 eol:
2ed83a59 587 /* can't imagine any other kind of operand */
f2f7d044 588 expressionP->X_seg = absent_section;
2ed83a59
KR
589 input_line_pointer--;
590 md_operand (expressionP);
591 break;
0bd77bc4 592
2ed83a59 593 default:
0bd77bc4
KR
594 if (is_end_of_line[c])
595 goto eol;
2ed83a59
KR
596 if (is_name_beginner (c)) /* here if did not begin with a digit */
597 {
598 /*
d841bc49
KR
599 * Identifier begins here.
600 * This is kludged for speed, so code is repeated.
601 */
2ed83a59
KR
602 isname:
603 name = --input_line_pointer;
604 c = get_symbol_end ();
605 symbolP = symbol_find_or_make (name);
d841bc49
KR
606 /* If we have an absolute symbol or a reg, then we know its value
607 now. */
2ed83a59 608 expressionP->X_seg = S_GET_SEGMENT (symbolP);
f2f7d044
ILT
609 if (expressionP->X_seg == absolute_section
610 || expressionP->X_seg == reg_section)
611 expressionP->X_add_number = S_GET_VALUE (symbolP);
612 else
2ed83a59 613 {
2ed83a59
KR
614 expressionP->X_add_number = 0;
615 expressionP->X_add_symbol = symbolP;
616 }
617 *input_line_pointer = c;
618 expressionP->X_subtract_symbol = NULL;
619 }
620 else
621 {
622 as_bad ("Bad expression");
623 expressionP->X_add_number = 0;
f2f7d044 624 expressionP->X_seg = absolute_section;
2ed83a59 625 }
c593cf41 626 }
c593cf41 627
c593cf41
SC
628 /*
629 * It is more 'efficient' to clean up the expressionS when they are created.
630 * Doing it here saves lines of code.
631 */
632 clean_up_expression (expressionP);
2ed83a59
KR
633 SKIP_WHITESPACE (); /*->1st char after operand. */
634 know (*input_line_pointer != ' ');
c593cf41 635 return (expressionP->X_seg);
2ed83a59 636} /* operand() */
fecd2382 637\f
2ed83a59 638
fecd2382
RP
639/* Internal. Simplify a struct expression for use by expr() */
640
641/*
642 * In: address of a expressionS.
643 * The X_seg field of the expressionS may only take certain values.
644 * Now, we permit SEG_PASS1 to make code smaller & faster.
645 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
646 * Out: expressionS may have been modified:
647 * 'foo-foo' symbol references cancelled to 0,
648 * which changes X_seg from SEG_DIFFERENCE to SEG_ABSOLUTE;
649 * Unused fields zeroed to help expr().
650 */
651
652static void
c593cf41 653clean_up_expression (expressionP)
2ed83a59 654 register expressionS *expressionP;
fecd2382 655{
f2f7d044
ILT
656 segT s = expressionP->X_seg;
657 if (s == absent_section
658 || s == pass1_section)
2ed83a59 659 {
2ed83a59
KR
660 expressionP->X_add_symbol = NULL;
661 expressionP->X_subtract_symbol = NULL;
662 expressionP->X_add_number = 0;
f2f7d044
ILT
663 }
664 else if (s == big_section
665 || s == absolute_section)
666 {
2ed83a59
KR
667 expressionP->X_subtract_symbol = NULL;
668 expressionP->X_add_symbol = NULL;
f2f7d044
ILT
669 }
670 else if (s == undefined_section)
671 expressionP->X_subtract_symbol = NULL;
672 else if (s == diff_section)
673 {
2ed83a59 674 /*
f2f7d044
ILT
675 * It does not hurt to 'cancel' NULL==NULL
676 * when comparing symbols for 'eq'ness.
677 * It is faster to re-cancel them to NULL
678 * than to check for this special case.
679 */
2ed83a59
KR
680 if (expressionP->X_subtract_symbol == expressionP->X_add_symbol
681 || (expressionP->X_subtract_symbol
682 && expressionP->X_add_symbol
49864cfa
KR
683 && (expressionP->X_subtract_symbol->sy_frag
684 == expressionP->X_add_symbol->sy_frag)
685 && (S_GET_VALUE (expressionP->X_subtract_symbol)
686 == S_GET_VALUE (expressionP->X_add_symbol))))
2ed83a59
KR
687 {
688 expressionP->X_subtract_symbol = NULL;
689 expressionP->X_add_symbol = NULL;
f2f7d044 690 expressionP->X_seg = absolute_section;
a39116f1 691 }
f2f7d044
ILT
692 }
693 else if (s == reg_section)
694 {
2ed83a59
KR
695 expressionP->X_add_symbol = NULL;
696 expressionP->X_subtract_symbol = NULL;
f2f7d044
ILT
697 }
698 else
699 {
2ed83a59
KR
700 if (SEG_NORMAL (expressionP->X_seg))
701 {
702 expressionP->X_subtract_symbol = NULL;
a39116f1 703 }
2ed83a59
KR
704 else
705 {
706 BAD_CASE (expressionP->X_seg);
a39116f1 707 }
2ed83a59 708 }
f2f7d044 709}
fecd2382
RP
710\f
711/*
712 * expr_part ()
713 *
714 * Internal. Made a function because this code is used in 2 places.
715 * Generate error or correct X_?????_symbol of expressionS.
716 */
717
718/*
719 * symbol_1 += symbol_2 ... well ... sort of.
720 */
721
722static segT
c593cf41 723expr_part (symbol_1_PP, symbol_2_P)
2ed83a59
KR
724 symbolS **symbol_1_PP;
725 symbolS *symbol_2_P;
fecd2382 726{
2ed83a59 727 segT return_value;
a39116f1 728#ifndef MANY_SEGMENTS
49864cfa
KR
729#ifndef OBJ_ECOFF
730 int test = ((*symbol_1_PP) == NULL
731 || (S_GET_SEGMENT (*symbol_1_PP) == text_section)
732 || (S_GET_SEGMENT (*symbol_1_PP) == data_section)
733 || (S_GET_SEGMENT (*symbol_1_PP) == bss_section)
734 || (!S_IS_DEFINED (*symbol_1_PP)));
735 assert (test);
736 test = (symbol_2_P == NULL
737 || (S_GET_SEGMENT (symbol_2_P) == text_section)
738 || (S_GET_SEGMENT (symbol_2_P) == data_section)
739 || (S_GET_SEGMENT (symbol_2_P) == bss_section)
f2f7d044 740 || (!S_IS_DEFINED (symbol_2_P)));
49864cfa
KR
741 assert (test);
742#endif
a39116f1 743#endif
2ed83a59 744 if (*symbol_1_PP)
fecd2382 745 {
2ed83a59 746 if (!S_IS_DEFINED (*symbol_1_PP))
fecd2382 747 {
c593cf41 748 if (symbol_2_P)
fecd2382 749 {
f2f7d044 750 return_value = pass1_section;
2ed83a59 751 *symbol_1_PP = NULL;
fecd2382 752 }
c593cf41 753 else
fecd2382 754 {
2ed83a59 755 know (!S_IS_DEFINED (*symbol_1_PP));
f2f7d044 756 return_value = undefined_section;
fecd2382
RP
757 }
758 }
c593cf41 759 else
fecd2382 760 {
c593cf41 761 if (symbol_2_P)
fecd2382 762 {
2ed83a59 763 if (!S_IS_DEFINED (symbol_2_P))
fecd2382 764 {
2ed83a59 765 *symbol_1_PP = NULL;
f2f7d044 766 return_value = pass1_section;
fecd2382 767 }
c593cf41 768 else
fecd2382 769 {
c593cf41 770 /* {seg1} - {seg2} */
2ed83a59
KR
771 as_bad ("Expression too complex, 2 symbolS forgotten: \"%s\" \"%s\"",
772 S_GET_NAME (*symbol_1_PP), S_GET_NAME (symbol_2_P));
773 *symbol_1_PP = NULL;
f2f7d044 774 return_value = absolute_section;
fecd2382
RP
775 }
776 }
c593cf41 777 else
fecd2382 778 {
2ed83a59 779 return_value = S_GET_SEGMENT (*symbol_1_PP);
fecd2382
RP
780 }
781 }
782 }
c593cf41 783 else
fecd2382 784 { /* (* symbol_1_PP) == NULL */
c593cf41 785 if (symbol_2_P)
fecd2382 786 {
2ed83a59
KR
787 *symbol_1_PP = symbol_2_P;
788 return_value = S_GET_SEGMENT (symbol_2_P);
fecd2382 789 }
c593cf41 790 else
fecd2382 791 {
2ed83a59 792 *symbol_1_PP = NULL;
f2f7d044 793 return_value = absolute_section;
fecd2382
RP
794 }
795 }
a39116f1 796#ifndef MANY_SEGMENTS
49864cfa
KR
797#ifndef OBJ_ECOFF
798 test = (return_value == absolute_section
799 || return_value == text_section
800 || return_value == data_section
801 || return_value == bss_section
802 || return_value == undefined_section
f2f7d044 803 || return_value == pass1_section);
49864cfa
KR
804 assert (test);
805#endif
a39116f1 806#endif
f2f7d044
ILT
807 know ((*symbol_1_PP) == NULL
808 || (S_GET_SEGMENT (*symbol_1_PP) == return_value));
c593cf41 809 return (return_value);
f2f7d044 810}
fecd2382
RP
811\f
812/* Expression parser. */
813
814/*
815 * We allow an empty expression, and just assume (absolute,0) silently.
816 * Unary operators and parenthetical expressions are treated as operands.
817 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
818 *
819 * We used to do a aho/ullman shift-reduce parser, but the logic got so
820 * warped that I flushed it and wrote a recursive-descent parser instead.
821 * Now things are stable, would anybody like to write a fast parser?
822 * Most expressions are either register (which does not even reach here)
823 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
824 * So I guess it doesn't really matter how inefficient more complex expressions
825 * are parsed.
826 *
827 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
828 * Also, we have consumed any leading or trailing spaces (operand does that)
829 * and done all intervening operators.
830 */
831
832typedef enum
833{
2ed83a59
KR
834 O_illegal, /* (0) what we get for illegal op */
835
836 O_multiply, /* (1) * */
837 O_divide, /* (2) / */
838 O_modulus, /* (3) % */
839 O_left_shift, /* (4) < */
840 O_right_shift, /* (5) > */
841 O_bit_inclusive_or, /* (6) | */
842 O_bit_or_not, /* (7) ! */
843 O_bit_exclusive_or, /* (8) ^ */
844 O_bit_and, /* (9) & */
845 O_add, /* (10) + */
846 O_subtract /* (11) - */
847}
848
fecd2382
RP
849operatorT;
850
49864cfa 851#undef __
fecd2382
RP
852#define __ O_illegal
853
2ed83a59
KR
854static const operatorT op_encoding[256] =
855{ /* maps ASCII->operators */
856
857 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
858 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
859
860 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
861 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
862 __, __, __, __, __, __, __, __,
863 __, __, __, __, O_left_shift, __, O_right_shift, __,
864 __, __, __, __, __, __, __, __,
865 __, __, __, __, __, __, __, __,
866 __, __, __, __, __, __, __, __,
867 __, __, __, __, __, __, O_bit_exclusive_or, __,
868 __, __, __, __, __, __, __, __,
869 __, __, __, __, __, __, __, __,
870 __, __, __, __, __, __, __, __,
871 __, __, __, __, O_bit_inclusive_or, __, __, __,
872
873 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
874 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
875 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
876 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
877 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
878 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
879 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
880 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
881};
fecd2382
RP
882
883
884/*
885 * Rank Examples
886 * 0 operand, (expression)
887 * 1 + -
888 * 2 & ^ ! |
889 * 3 * / % << >>
890 */
891static const operator_rankT
2ed83a59
KR
892 op_rank[] =
893{0, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1};
fecd2382
RP
894\f
895/* Return resultP->X_seg. */
2ed83a59
KR
896segT
897expr (rank, resultP)
898 register operator_rankT rank; /* Larger # is higher rank. */
899 register expressionS *resultP; /* Deliver result here. */
fecd2382 900{
2ed83a59
KR
901 expressionS right;
902 register operatorT op_left;
903 register char c_left; /* 1st operator character. */
904 register operatorT op_right;
c593cf41
SC
905 register char c_right;
906
2ed83a59
KR
907 know (rank >= 0);
908 (void) operand (resultP);
909 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
910 c_left = *input_line_pointer; /* Potential operator character. */
911 op_left = op_encoding[c_left];
912 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
fecd2382 913 {
2ed83a59
KR
914 input_line_pointer++; /*->after 1st character of operator. */
915 /* Operators "<<" and ">>" have 2 characters. */
916 if (*input_line_pointer == c_left && (c_left == '<' || c_left == '>'))
fecd2382 917 {
2ed83a59 918 input_line_pointer++;
fecd2382 919 } /*->after operator. */
f2f7d044 920 if (absent_section == expr (op_rank[(int) op_left], &right))
fecd2382 921 {
2ed83a59
KR
922 as_warn ("Missing operand value assumed absolute 0.");
923 resultP->X_add_number = 0;
924 resultP->X_subtract_symbol = NULL;
925 resultP->X_add_symbol = NULL;
f2f7d044 926 resultP->X_seg = absolute_section;
fecd2382 927 }
2ed83a59
KR
928 know (*input_line_pointer != ' ');
929 c_right = *input_line_pointer;
930 op_right = op_encoding[c_right];
931 if (*input_line_pointer == c_right && (c_right == '<' || c_right == '>'))
fecd2382 932 {
2ed83a59 933 input_line_pointer++;
fecd2382 934 } /*->after operator. */
2ed83a59 935 know ((int) op_right == 0 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
c593cf41
SC
936 /* input_line_pointer->after right-hand quantity. */
937 /* left-hand quantity in resultP */
938 /* right-hand quantity in right. */
939 /* operator in op_left. */
f2f7d044 940 if (resultP->X_seg == pass1_section || right.X_seg == pass1_section)
fecd2382 941 {
f2f7d044 942 resultP->X_seg = pass1_section;
fecd2382 943 }
c593cf41 944 else
fecd2382 945 {
f2f7d044 946 if (resultP->X_seg == big_section)
fecd2382 947 {
2ed83a59
KR
948 as_warn ("Left operand of %c is a %s. Integer 0 assumed.",
949 c_left, resultP->X_add_number > 0 ? "bignum" : "float");
f2f7d044 950 resultP->X_seg = absolute_section;
c593cf41
SC
951 resultP->X_add_symbol = 0;
952 resultP->X_subtract_symbol = 0;
953 resultP->X_add_number = 0;
fecd2382 954 }
f2f7d044 955 if (right.X_seg == big_section)
fecd2382 956 {
2ed83a59
KR
957 as_warn ("Right operand of %c is a %s. Integer 0 assumed.",
958 c_left, right.X_add_number > 0 ? "bignum" : "float");
f2f7d044 959 right.X_seg = absolute_section;
2ed83a59
KR
960 right.X_add_symbol = 0;
961 right.X_subtract_symbol = 0;
962 right.X_add_number = 0;
fecd2382 963 }
c593cf41 964 if (op_left == O_subtract)
fecd2382 965 {
c593cf41
SC
966 /*
967 * Convert - into + by exchanging symbolS and negating number.
968 * I know -infinity can't be negated in 2's complement:
969 * but then it can't be subtracted either. This trick
970 * does not cause any further inaccuracy.
971 */
972
2ed83a59 973 register symbolS *symbolP;
c593cf41 974
2ed83a59
KR
975 right.X_add_number = -right.X_add_number;
976 symbolP = right.X_add_symbol;
977 right.X_add_symbol = right.X_subtract_symbol;
978 right.X_subtract_symbol = symbolP;
c593cf41 979 if (symbolP)
fecd2382 980 {
f2f7d044 981 right.X_seg = diff_section;
fecd2382 982 }
c593cf41 983 op_left = O_add;
fecd2382 984 }
c593cf41
SC
985\f
986 if (op_left == O_add)
fecd2382 987 {
2ed83a59
KR
988 segT seg1;
989 segT seg2;
a39116f1 990#ifndef MANY_SEGMENTS
49864cfa 991#ifndef OBJ_ECOFF
f2f7d044 992 know (resultP->X_seg == data_section || resultP->X_seg == text_section || resultP->X_seg == bss_section || resultP->X_seg == undefined_section || resultP->X_seg == diff_section || resultP->X_seg == absolute_section || resultP->X_seg == pass1_section || resultP->X_seg == reg_section);
2ed83a59 993
f2f7d044 994 know (right.X_seg == data_section || right.X_seg == text_section || right.X_seg == bss_section || right.X_seg == undefined_section || right.X_seg == diff_section || right.X_seg == absolute_section || right.X_seg == pass1_section);
49864cfa 995#endif
a39116f1 996#endif
2ed83a59 997 clean_up_expression (&right);
c593cf41
SC
998 clean_up_expression (resultP);
999
2ed83a59
KR
1000 seg1 = expr_part (&resultP->X_add_symbol, right.X_add_symbol);
1001 seg2 = expr_part (&resultP->X_subtract_symbol, right.X_subtract_symbol);
f2f7d044 1002 if (seg1 == pass1_section || seg2 == pass1_section)
2ed83a59
KR
1003 {
1004 need_pass_2 = 1;
f2f7d044 1005 resultP->X_seg = pass1_section;
2ed83a59 1006 }
f2f7d044 1007 else if (seg2 == absolute_section)
2ed83a59 1008 resultP->X_seg = seg1;
f2f7d044
ILT
1009 else if (seg1 != undefined_section
1010 && seg1 != absolute_section
1011 && seg2 != undefined_section
2ed83a59
KR
1012 && seg1 != seg2)
1013 {
f2f7d044 1014 know (seg2 != absolute_section);
2ed83a59 1015 know (resultP->X_subtract_symbol);
a39116f1 1016#ifndef MANY_SEGMENTS
49864cfa 1017#ifndef OBJ_ECOFF
f2f7d044
ILT
1018 know (seg1 == text_section || seg1 == data_section || seg1 == bss_section);
1019 know (seg2 == text_section || seg2 == data_section || seg2 == bss_section);
49864cfa 1020#endif
a39116f1 1021#endif
2ed83a59
KR
1022 know (resultP->X_add_symbol);
1023 know (resultP->X_subtract_symbol);
1024 as_bad ("Expression too complex: forgetting %s - %s",
1025 S_GET_NAME (resultP->X_add_symbol),
1026 S_GET_NAME (resultP->X_subtract_symbol));
f2f7d044 1027 resultP->X_seg = absolute_section;
2ed83a59
KR
1028 /* Clean_up_expression() will do the rest. */
1029 }
1030 else
f2f7d044 1031 resultP->X_seg = diff_section;
c593cf41 1032
2ed83a59 1033 resultP->X_add_number += right.X_add_number;
c593cf41 1034 clean_up_expression (resultP);
2ed83a59 1035 }
c593cf41 1036 else
fecd2382 1037 { /* Not +. */
f2f7d044 1038 if (resultP->X_seg == undefined_section || right.X_seg == undefined_section)
fecd2382 1039 {
f2f7d044 1040 resultP->X_seg = pass1_section;
c593cf41 1041 need_pass_2 = 1;
fecd2382 1042 }
c593cf41 1043 else
fecd2382 1044 {
c593cf41
SC
1045 resultP->X_subtract_symbol = NULL;
1046 resultP->X_add_symbol = NULL;
f2f7d044
ILT
1047 /* Will be absolute_section. */
1048 if (resultP->X_seg != absolute_section || right.X_seg != absolute_section)
fecd2382 1049 {
2ed83a59 1050 as_bad ("Relocation error. Absolute 0 assumed.");
f2f7d044 1051 resultP->X_seg = absolute_section;
c593cf41 1052 resultP->X_add_number = 0;
fecd2382 1053 }
c593cf41 1054 else
fecd2382 1055 {
c593cf41 1056 switch (op_left)
fecd2382
RP
1057 {
1058 case O_bit_inclusive_or:
2ed83a59 1059 resultP->X_add_number |= right.X_add_number;
c593cf41
SC
1060 break;
1061
fecd2382 1062 case O_modulus:
2ed83a59 1063 if (right.X_add_number)
fecd2382 1064 {
2ed83a59 1065 resultP->X_add_number %= right.X_add_number;
fecd2382 1066 }
c593cf41 1067 else
fecd2382 1068 {
2ed83a59 1069 as_warn ("Division by 0. 0 assumed.");
c593cf41 1070 resultP->X_add_number = 0;
fecd2382 1071 }
c593cf41
SC
1072 break;
1073
fecd2382 1074 case O_bit_and:
2ed83a59 1075 resultP->X_add_number &= right.X_add_number;
c593cf41
SC
1076 break;
1077
fecd2382 1078 case O_multiply:
2ed83a59 1079 resultP->X_add_number *= right.X_add_number;
c593cf41
SC
1080 break;
1081
fecd2382 1082 case O_divide:
2ed83a59 1083 if (right.X_add_number)
fecd2382 1084 {
2ed83a59 1085 resultP->X_add_number /= right.X_add_number;
fecd2382 1086 }
c593cf41 1087 else
fecd2382 1088 {
2ed83a59
KR
1089 as_warn ("Division by 0. 0 assumed.");
1090 resultP->X_add_number = 0;
fecd2382 1091 }
2ed83a59
KR
1092 break;
1093
fecd2382 1094 case O_left_shift:
2ed83a59
KR
1095 resultP->X_add_number <<= right.X_add_number;
1096 break;
1097
fecd2382 1098 case O_right_shift:
2ed83a59
KR
1099 resultP->X_add_number >>= right.X_add_number;
1100 break;
1101
fecd2382 1102 case O_bit_exclusive_or:
2ed83a59
KR
1103 resultP->X_add_number ^= right.X_add_number;
1104 break;
1105
fecd2382 1106 case O_bit_or_not:
2ed83a59
KR
1107 resultP->X_add_number |= ~right.X_add_number;
1108 break;
1109
fecd2382 1110 default:
2ed83a59
KR
1111 BAD_CASE (op_left);
1112 break;
1113 } /* switch(operator) */
fecd2382
RP
1114 }
1115 } /* If we have to force need_pass_2. */
1116 } /* If operator was +. */
1117 } /* If we didn't set need_pass_2. */
2ed83a59 1118 op_left = op_right;
fecd2382 1119 } /* While next operator is >= this rank. */
2ed83a59 1120 return (resultP->X_seg);
fecd2382
RP
1121}
1122\f
1123/*
1124 * get_symbol_end()
1125 *
1126 * This lives here because it belongs equally in expr.c & read.c.
1127 * Expr.c is just a branch office read.c anyway, and putting it
1128 * here lessens the crowd at read.c.
1129 *
1130 * Assume input_line_pointer is at start of symbol name.
1131 * Advance input_line_pointer past symbol name.
1132 * Turn that character into a '\0', returning its former value.
1133 * This allows a string compare (RMS wants symbol names to be strings)
1134 * of the symbol name.
1135 * There will always be a char following symbol name, because all good
1136 * lines end in end-of-line.
1137 */
1138char
2ed83a59 1139get_symbol_end ()
fecd2382 1140{
2ed83a59
KR
1141 register char c;
1142
1143 while (is_part_of_name (c = *input_line_pointer++))
1144 ;
1145 *--input_line_pointer = 0;
1146 return (c);
fecd2382
RP
1147}
1148
a39116f1 1149
2ed83a59
KR
1150unsigned int
1151get_single_number ()
a39116f1 1152{
2ed83a59
KR
1153 expressionS exp;
1154 operand (&exp);
1155 return exp.X_add_number;
1156
a39116f1 1157}
2ed83a59 1158
8b228fe9 1159/* end of expr.c */
This page took 0.147358 seconds and 4 git commands to generate.