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