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