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