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