* gas/testsuite/all/gas.exp: No longer expect failures for
[deliverable/binutils-gdb.git] / gas / expr.c
CommitLineData
fecd2382 1/* expr.c -operands, expressions-
c246596a 2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
30d0557c 3 Free Software Foundation, Inc.
2ed83a59 4
a39116f1 5 This file is part of GAS, the GNU Assembler.
2ed83a59 6
a39116f1
RP
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
2ed83a59 11
a39116f1
RP
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
2ed83a59 16
a39116f1 17 You should have received a copy of the GNU General Public License
30d0557c
ILT
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
fecd2382
RP
21
22/*
23 * This is really a branch office of as-read.c. I split it out to clearly
24 * distinguish the world of expressions from the world of statements.
25 * (It also gives smaller files to re-compile.)
26 * Here, "operand"s are of expressions, not instructions.
27 */
28
29#include <ctype.h>
30#include <string.h>
e5d62150 31#define min(a, b) ((a) < (b) ? (a) : (b))
fecd2382
RP
32
33#include "as.h"
fecd2382
RP
34#include "obstack.h"
35
d4c8cbd8
JL
36static void floating_constant PARAMS ((expressionS * expressionP));
37static void integer_constant PARAMS ((int radix, expressionS * expressionP));
219deb70 38static void mri_char_constant PARAMS ((expressionS *));
52e1cf9d 39static void current_location PARAMS ((expressionS *));
f2f7d044 40static void clean_up_expression PARAMS ((expressionS * expressionP));
b463948b
JL
41static segT operand PARAMS ((expressionS *));
42static operatorT operator PARAMS ((void));
5ac34ac3 43
f2f7d044 44extern const char EXP_CHARS[], FLT_CHARS[];
30d0557c
ILT
45
46/* We keep a mapping of expression symbols to file positions, so that
47 we can provide better error messages. */
48
49struct expr_symbol_line
50{
51 struct expr_symbol_line *next;
52 symbolS *sym;
53 char *file;
54 unsigned int line;
55};
56
57static struct expr_symbol_line *expr_symbol_lines;
5ac34ac3
ILT
58\f
59/* Build a dummy symbol to hold a complex expression. This is how we
60 build expressions up out of other expressions. The symbol is put
61 into the fake section expr_section. */
62
3a762a0b 63symbolS *
5ac34ac3
ILT
64make_expr_symbol (expressionP)
65 expressionS *expressionP;
66{
67 const char *fake;
68 symbolS *symbolP;
30d0557c 69 struct expr_symbol_line *n;
fecd2382 70
d90f530b
KR
71 if (expressionP->X_op == O_symbol
72 && expressionP->X_add_number == 0)
73 return expressionP->X_add_symbol;
74
d4c8cbd8
JL
75 fake = FAKE_LABEL_NAME;
76
5ac34ac3
ILT
77 /* Putting constant symbols in absolute_section rather than
78 expr_section is convenient for the old a.out code, for which
79 S_GET_SEGMENT does not always retrieve the value put in by
80 S_SET_SEGMENT. */
3a762a0b
KR
81 symbolP = symbol_create (fake,
82 (expressionP->X_op == O_constant
83 ? absolute_section
84 : expr_section),
85 0, &zero_address_frag);
5ac34ac3 86 symbolP->sy_value = *expressionP;
d90f530b
KR
87
88 if (expressionP->X_op == O_constant)
b463948b 89 resolve_symbol_value (symbolP, 1);
d90f530b 90
30d0557c
ILT
91 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
92 n->sym = symbolP;
93 as_where (&n->file, &n->line);
94 n->next = expr_symbol_lines;
95 expr_symbol_lines = n;
96
5ac34ac3
ILT
97 return symbolP;
98}
30d0557c
ILT
99
100/* Return the file and line number for an expr symbol. Return
101 non-zero if something was found, 0 if no information is known for
102 the symbol. */
103
104int
105expr_symbol_where (sym, pfile, pline)
106 symbolS *sym;
107 char **pfile;
108 unsigned int *pline;
109{
110 register struct expr_symbol_line *l;
111
112 for (l = expr_symbol_lines; l != NULL; l = l->next)
113 {
114 if (l->sym == sym)
115 {
116 *pfile = l->file;
117 *pline = l->line;
118 return 1;
119 }
120 }
121
122 return 0;
123}
5ac34ac3 124\f
e5d62150
DE
125/* Utilities for building expressions.
126 Since complex expressions are recorded as symbols for use in other
127 expressions these return a symbolS * and not an expressionS *.
128 These explicitly do not take an "add_number" argument. */
129/* ??? For completeness' sake one might want expr_build_symbol.
130 It would just return its argument. */
131
132/* Build an expression for an unsigned constant.
133 The corresponding one for signed constants is missing because
134 there's currently no need for it. One could add an unsigned_p flag
135 but that seems more clumsy. */
136
137symbolS *
138expr_build_uconstant (value)
139 offsetT value;
140{
141 expressionS e;
142
143 e.X_op = O_constant;
144 e.X_add_number = value;
145 e.X_unsigned = 1;
146 return make_expr_symbol (&e);
147}
148
149/* Build an expression for OP s1. */
150
151symbolS *
152expr_build_unary (op, s1)
153 operatorT op;
154 symbolS *s1;
155{
156 expressionS e;
157
158 e.X_op = op;
159 e.X_add_symbol = s1;
160 e.X_add_number = 0;
161 return make_expr_symbol (&e);
162}
163
164/* Build an expression for s1 OP s2. */
165
166symbolS *
167expr_build_binary (op, s1, s2)
168 operatorT op;
169 symbolS *s1;
170 symbolS *s2;
171{
172 expressionS e;
173
174 e.X_op = op;
175 e.X_add_symbol = s1;
176 e.X_op_symbol = s2;
177 e.X_add_number = 0;
178 return make_expr_symbol (&e);
179}
180\f
fecd2382
RP
181/*
182 * Build any floating-point literal here.
183 * Also build any bignum literal here.
184 */
185
fecd2382
RP
186/* Seems atof_machine can backscan through generic_bignum and hit whatever
187 happens to be loaded before it in memory. And its way too complicated
188 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
189 and never write into the early words, thus they'll always be zero.
f2f7d044 190 I hate Dean's floating-point code. Bleh. */
2ed83a59
KR
191LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
192FLONUM_TYPE generic_floating_point_number =
fecd2382 193{
2ed83a59 194 &generic_bignum[6], /* low (JF: Was 0) */
351878df 195 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
2ed83a59
KR
196 0, /* leader */
197 0, /* exponent */
198 0 /* sign */
199};
fecd2382
RP
200/* If nonzero, we've been asked to assemble nan, +inf or -inf */
201int generic_floating_point_magic;
202\f
d4c8cbd8 203static void
2ed83a59
KR
204floating_constant (expressionP)
205 expressionS *expressionP;
c593cf41
SC
206{
207 /* input_line_pointer->*/
208 /* floating-point constant. */
209 int error_code;
210
351878df
KR
211 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
212 &generic_floating_point_number);
c593cf41
SC
213
214 if (error_code)
c593cf41 215 {
2ed83a59
KR
216 if (error_code == ERROR_EXPONENT_OVERFLOW)
217 {
218 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
219 }
220 else
221 {
222 as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
223 }
c593cf41 224 }
5ac34ac3 225 expressionP->X_op = O_big;
c593cf41
SC
226 /* input_line_pointer->just after constant, */
227 /* which may point to whitespace. */
2ed83a59 228 expressionP->X_add_number = -1;
c593cf41
SC
229}
230
e5d62150
DE
231static valueT
232generic_bignum_to_int32 ()
233{
234 valueT number =
235 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
236 | (generic_bignum[0] & LITTLENUM_MASK);
237 number &= 0xffffffff;
238 return number;
239}
240
241#ifdef BFD64
242static valueT
243generic_bignum_to_int64 ()
244{
245 valueT number =
246 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
247 << LITTLENUM_NUMBER_OF_BITS)
248 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
249 << LITTLENUM_NUMBER_OF_BITS)
250 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
251 << LITTLENUM_NUMBER_OF_BITS)
252 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
253 return number;
254}
255#endif
256
d4c8cbd8 257static void
2ed83a59
KR
258integer_constant (radix, expressionP)
259 int radix;
260 expressionS *expressionP;
c593cf41 261{
7691379e 262 char *start; /* start of number. */
219deb70 263 char *suffix = NULL;
c593cf41 264 char c;
dae92eab
KR
265 valueT number; /* offset or (absolute) value */
266 short int digit; /* value of next digit in current radix */
267 short int maxdig = 0;/* highest permitted digit value. */
268 int too_many_digits = 0; /* if we see >= this number of */
269 char *name; /* points to name of symbol */
270 symbolS *symbolP; /* points to symbol */
2ed83a59
KR
271
272 int small; /* true if fits in 32 bits. */
2ed83a59 273
dae92eab
KR
274 /* May be bignum, or may fit in 32 bits. */
275 /* Most numbers fit into 32 bits, and we want this case to be fast.
276 so we pretend it will fit into 32 bits. If, after making up a 32
277 bit number, we realise that we have scanned more digits than
278 comfortably fit into 32 bits, we re-scan the digits coding them
279 into a bignum. For decimal and octal numbers we are
280 conservative: Some numbers may be assumed bignums when in fact
281 they do fit into 32 bits. Numbers of any radix can have excess
282 leading zeros: We strive to recognise this and cast them back
283 into 32 bits. We must check that the bignum really is more than
284 32 bits, and change it back to a 32-bit number if it fits. The
285 number we are looking for is expected to be positive, but if it
286 fits into 32 bits as an unsigned number, we let it be a 32-bit
287 number. The cavalier approach is for speed in ordinary cases. */
58d4951d
ILT
288 /* This has been extended for 64 bits. We blindly assume that if
289 you're compiling in 64-bit mode, the target is a 64-bit machine.
290 This should be cleaned up. */
291
292#ifdef BFD64
293#define valuesize 64
294#else /* includes non-bfd case, mostly */
295#define valuesize 32
296#endif
2ed83a59 297
30d0557c 298 if (flag_m68k_mri && radix == 0)
219deb70
ILT
299 {
300 int flt = 0;
301
302 /* In MRI mode, the number may have a suffix indicating the
303 radix. For that matter, it might actually be a floating
304 point constant. */
c246596a
ILT
305 for (suffix = input_line_pointer;
306 isalnum ((unsigned char) *suffix);
307 suffix++)
219deb70
ILT
308 {
309 if (*suffix == 'e' || *suffix == 'E')
310 flt = 1;
311 }
312
313 if (suffix == input_line_pointer)
314 {
315 radix = 10;
316 suffix = NULL;
317 }
318 else
319 {
320 c = *--suffix;
c246596a 321 if (islower ((unsigned char) c))
219deb70
ILT
322 c = toupper (c);
323 if (c == 'B')
324 radix = 2;
325 else if (c == 'D')
326 radix = 10;
327 else if (c == 'O' || c == 'Q')
328 radix = 8;
329 else if (c == 'H')
330 radix = 16;
331 else if (suffix[1] == '.' || c == 'E' || flt)
332 {
333 floating_constant (expressionP);
334 return;
335 }
336 else
337 {
338 radix = 10;
339 suffix = NULL;
340 }
341 }
342 }
343
2ed83a59 344 switch (radix)
f8701a3f 345 {
f8701a3f
SC
346 case 2:
347 maxdig = 2;
58d4951d 348 too_many_digits = valuesize + 1;
f8701a3f
SC
349 break;
350 case 8:
351 maxdig = radix = 8;
7691379e 352 too_many_digits = (valuesize + 2) / 3 + 1;
f8701a3f
SC
353 break;
354 case 16:
f8701a3f 355 maxdig = radix = 16;
7691379e 356 too_many_digits = (valuesize + 3) / 4 + 1;
f8701a3f
SC
357 break;
358 case 10:
359 maxdig = radix = 10;
58d4951d 360 too_many_digits = (valuesize + 12) / 4; /* very rough */
f8701a3f 361 }
58d4951d 362#undef valuesize
7691379e
KR
363 start = input_line_pointer;
364 c = *input_line_pointer++;
58d4951d 365 for (number = 0;
3a762a0b 366 (digit = hex_value (c)) < maxdig;
58d4951d 367 c = *input_line_pointer++)
f8701a3f
SC
368 {
369 number = number * radix + digit;
370 }
c593cf41
SC
371 /* c contains character after number. */
372 /* input_line_pointer->char after c. */
7691379e 373 small = (input_line_pointer - start - 1) < too_many_digits;
e5d62150
DE
374
375 if (radix == 16 && c == '_')
376 {
377 /* This is literal of the form 0x333_0_12345678_1.
378 This example is equivalent to 0x00000333000000001234567800000001. */
379
380 int num_little_digits = 0;
381 int i;
382 input_line_pointer = start; /*->1st digit. */
383
384 know (LITTLENUM_NUMBER_OF_BITS == 16);
385
386 for (c = '_'; c == '_'; num_little_digits+=2)
387 {
388
389 /* Convert one 64-bit word. */
390 int ndigit = 0;
391 number = 0;
392 for (c = *input_line_pointer++;
393 (digit = hex_value (c)) < maxdig;
394 c = *(input_line_pointer++))
395 {
396 number = number * radix + digit;
397 ndigit++;
398 }
399
400 /* Check for 8 digit per word max. */
401 if (ndigit > 8)
402 as_bad ("An bignum with underscores may not have more than 8 hex digits in any word.");
403
404 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
405 know (LITTLENUM_NUMBER_OF_BITS == 16);
406 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1); i >= 2; i--)
407 generic_bignum[i] = generic_bignum[i-2];
408
409 /* Add the new digits as the least significant new ones. */
410 generic_bignum[0] = number & 0xffffffff;
411 generic_bignum[1] = number >> 16;
412 }
413
414 /* Again, c is char after number, input_line_pointer->after c. */
415
416 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
417 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
418
419 assert (num_little_digits >= 4);
420
421 if (num_little_digits != 8)
422 as_bad ("A bignum with underscores must have exactly 4 words.");
423
424 /* We might have some leading zeros. These can be trimmed to give
425 * us a change to fit this constant into a small number.
426 */
427 while (generic_bignum[num_little_digits-1] == 0 && num_little_digits > 1)
428 num_little_digits--;
429
430 if (num_little_digits <= 2)
431 {
432 /* will fit into 32 bits. */
433 number = generic_bignum_to_int32 ();
434 small = 1;
435 }
436#ifdef BFD64
437 else if (num_little_digits <= 4)
438 {
439 /* Will fit into 64 bits. */
440 number = generic_bignum_to_int64 ();
441 small = 1;
442 }
443#endif
444 else
445 {
446 small = 0;
447 number = num_little_digits; /* number of littlenums in the bignum. */
448 }
449 }
450 else if (!small)
c593cf41 451 {
f8701a3f
SC
452 /*
453 * we saw a lot of digits. manufacture a bignum the hard way.
454 */
2ed83a59
KR
455 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
456 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
f8701a3f 457 long carry;
2ed83a59 458
f8701a3f 459 leader = generic_bignum;
2ed83a59
KR
460 generic_bignum[0] = 0;
461 generic_bignum[1] = 0;
b463948b
JL
462 generic_bignum[2] = 0;
463 generic_bignum[3] = 0;
7691379e 464 input_line_pointer = start; /*->1st digit. */
f8701a3f 465 c = *input_line_pointer++;
58d4951d 466 for (;
3a762a0b 467 (carry = hex_value (c)) < maxdig;
58d4951d 468 c = *input_line_pointer++)
f8701a3f
SC
469 {
470 for (pointer = generic_bignum;
471 pointer <= leader;
472 pointer++)
473 {
474 long work;
2ed83a59
KR
475
476 work = carry + radix * *pointer;
f8701a3f
SC
477 *pointer = work & LITTLENUM_MASK;
478 carry = work >> LITTLENUM_NUMBER_OF_BITS;
479 }
480 if (carry)
481 {
482 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
351878df
KR
483 {
484 /* room to grow a longer bignum. */
f8701a3f
SC
485 *++leader = carry;
486 }
487 }
488 }
489 /* again, c is char after number, */
490 /* input_line_pointer->after c. */
2ed83a59 491 know (LITTLENUM_NUMBER_OF_BITS == 16);
7691379e 492 if (leader < generic_bignum + 2)
351878df
KR
493 {
494 /* will fit into 32 bits. */
e5d62150 495 number = generic_bignum_to_int32 ();
f8701a3f
SC
496 small = 1;
497 }
b463948b
JL
498#ifdef BFD64
499 else if (leader < generic_bignum + 4)
500 {
501 /* Will fit into 64 bits. */
e5d62150 502 number = generic_bignum_to_int64 ();
b463948b
JL
503 small = 1;
504 }
505#endif
f8701a3f
SC
506 else
507 {
2ed83a59 508 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
c593cf41 509 }
c593cf41 510 }
219deb70 511
30d0557c 512 if (flag_m68k_mri && suffix != NULL && input_line_pointer - 1 == suffix)
219deb70
ILT
513 c = *input_line_pointer++;
514
2ed83a59
KR
515 if (small)
516 {
f8701a3f 517 /*
2ed83a59
KR
518 * here with number, in correct radix. c is the next char.
519 * note that unlike un*x, we allow "011f" "0x9f" to
520 * both mean the same as the (conventional) "9f". this is simply easier
521 * than checking for strict canonical form. syntax sux!
f8701a3f 522 */
2ed83a59 523
219deb70 524 if (LOCAL_LABELS_FB && c == 'b')
2ed83a59 525 {
219deb70
ILT
526 /*
527 * backward ref to local label.
528 * because it is backward, expect it to be defined.
529 */
530 /* Construct a local label. */
531 name = fb_label_name ((int) number, 0);
2ed83a59 532
219deb70
ILT
533 /* seen before, or symbol is defined: ok */
534 symbolP = symbol_find (name);
535 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
536 {
537 /* local labels are never absolute. don't waste time
538 checking absoluteness. */
539 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
2ed83a59 540
219deb70
ILT
541 expressionP->X_op = O_symbol;
542 expressionP->X_add_symbol = symbolP;
543 }
544 else
545 {
546 /* either not seen or not defined. */
547 /* @@ Should print out the original string instead of
548 the parsed number. */
549 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
550 (int) number);
551 expressionP->X_op = O_constant;
552 }
2ed83a59 553
219deb70
ILT
554 expressionP->X_add_number = 0;
555 } /* case 'b' */
556 else if (LOCAL_LABELS_FB && c == 'f')
557 {
558 /*
559 * forward reference. expect symbol to be undefined or
560 * unknown. undefined: seen it before. unknown: never seen
561 * it before.
562 * construct a local label name, then an undefined symbol.
563 * don't create a xseg frag for it: caller may do that.
564 * just return it as never seen before.
565 */
566 name = fb_label_name ((int) number, 1);
567 symbolP = symbol_find_or_make (name);
568 /* we have no need to check symbol properties. */
c593cf41 569#ifndef many_segments
219deb70
ILT
570 /* since "know" puts its arg into a "string", we
571 can't have newlines in the argument. */
572 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
c593cf41 573#endif
219deb70
ILT
574 expressionP->X_op = O_symbol;
575 expressionP->X_add_symbol = symbolP;
576 expressionP->X_add_number = 0;
577 } /* case 'f' */
578 else if (LOCAL_LABELS_DOLLAR && c == '$')
579 {
580 /* If the dollar label is *currently* defined, then this is just
581 another reference to it. If it is not *currently* defined,
582 then this is a fresh instantiation of that number, so create
583 it. */
2ed83a59 584
219deb70
ILT
585 if (dollar_label_defined ((long) number))
586 {
587 name = dollar_label_name ((long) number, 0);
588 symbolP = symbol_find (name);
589 know (symbolP != NULL);
590 }
591 else
592 {
593 name = dollar_label_name ((long) number, 1);
594 symbolP = symbol_find_or_make (name);
595 }
2ed83a59 596
219deb70
ILT
597 expressionP->X_op = O_symbol;
598 expressionP->X_add_symbol = symbolP;
599 expressionP->X_add_number = 0;
600 } /* case '$' */
601 else
602 {
603 expressionP->X_op = O_constant;
604#ifdef TARGET_WORD_SIZE
605 /* Sign extend NUMBER. */
606 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
607#endif
608 expressionP->X_add_number = number;
609 input_line_pointer--; /* restore following character. */
610 } /* really just a number */
611 }
612 else
613 {
614 /* not a small number */
615 expressionP->X_op = O_big;
616 expressionP->X_add_number = number; /* number of littlenums */
617 input_line_pointer--; /*->char following number. */
618 }
619}
2ed83a59 620
219deb70 621/* Parse an MRI multi character constant. */
f8701a3f 622
219deb70
ILT
623static void
624mri_char_constant (expressionP)
625 expressionS *expressionP;
626{
627 int i;
2ed83a59 628
219deb70
ILT
629 if (*input_line_pointer == '\''
630 && input_line_pointer[1] != '\'')
631 {
632 expressionP->X_op = O_constant;
633 expressionP->X_add_number = 0;
634 return;
635 }
2ed83a59 636
219deb70
ILT
637 /* In order to get the correct byte ordering, we must build the
638 number in reverse. */
639 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
640 {
641 int j;
2ed83a59 642
219deb70
ILT
643 generic_bignum[i] = 0;
644 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
645 {
646 if (*input_line_pointer == '\'')
647 {
648 if (input_line_pointer[1] != '\'')
649 break;
650 ++input_line_pointer;
651 }
652 generic_bignum[i] <<= 8;
653 generic_bignum[i] += *input_line_pointer;
654 ++input_line_pointer;
655 }
2ed83a59 656
219deb70
ILT
657 if (i < SIZE_OF_LARGE_NUMBER - 1)
658 {
659 /* If there is more than one littlenum, left justify the
660 last one to make it match the earlier ones. If there is
661 only one, we can just use the value directly. */
662 for (; j < CHARS_PER_LITTLENUM; j++)
663 generic_bignum[i] <<= 8;
664 }
2ed83a59 665
219deb70
ILT
666 if (*input_line_pointer == '\''
667 && input_line_pointer[1] != '\'')
668 break;
669 }
2ed83a59 670
219deb70
ILT
671 if (i < 0)
672 {
673 as_bad ("Character constant too large");
674 i = 0;
675 }
2ed83a59 676
219deb70
ILT
677 if (i > 0)
678 {
679 int c;
680 int j;
2ed83a59 681
219deb70
ILT
682 c = SIZE_OF_LARGE_NUMBER - i;
683 for (j = 0; j < c; j++)
684 generic_bignum[j] = generic_bignum[i + j];
685 i = c;
2ed83a59 686 }
219deb70
ILT
687
688 know (LITTLENUM_NUMBER_OF_BITS == 16);
689 if (i > 2)
dae92eab 690 {
5ac34ac3 691 expressionP->X_op = O_big;
219deb70
ILT
692 expressionP->X_add_number = i;
693 }
694 else
695 {
696 expressionP->X_op = O_constant;
697 if (i < 2)
698 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
699 else
700 expressionP->X_add_number =
701 (((generic_bignum[1] & LITTLENUM_MASK)
702 << LITTLENUM_NUMBER_OF_BITS)
703 | (generic_bignum[0] & LITTLENUM_MASK));
dae92eab 704 }
c593cf41 705
219deb70
ILT
706 /* Skip the final closing quote. */
707 ++input_line_pointer;
708}
c593cf41 709
52e1cf9d
ILT
710/* Return an expression representing the current location. This
711 handles the magic symbol `.'. */
712
713static void
714current_location (expressionp)
715 expressionS *expressionp;
716{
717 if (now_seg == absolute_section)
718 {
719 expressionp->X_op = O_constant;
720 expressionp->X_add_number = abs_section_offset;
721 }
722 else
723 {
724 symbolS *symbolp;
725
726 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
727 (valueT) frag_now_fix (),
728 frag_now);
729 expressionp->X_op = O_symbol;
730 expressionp->X_add_symbol = symbolp;
731 expressionp->X_add_number = 0;
732 }
733}
734
fecd2382
RP
735/*
736 * Summary of operand().
737 *
738 * in: Input_line_pointer points to 1st char of operand, which may
739 * be a space.
740 *
5ac34ac3
ILT
741 * out: A expressionS.
742 * The operand may have been empty: in this case X_op == O_absent.
fecd2382 743 * Input_line_pointer->(next non-blank) char after operand.
fecd2382 744 */
c593cf41 745
fecd2382 746static segT
c593cf41 747operand (expressionP)
dae92eab 748 expressionS *expressionP;
fecd2382 749{
dae92eab
KR
750 char c;
751 symbolS *symbolP; /* points to symbol */
752 char *name; /* points to name of symbol */
58d4951d 753 segT segment;
c593cf41 754
d4c8cbd8
JL
755 /* All integers are regarded as unsigned unless they are negated.
756 This is because the only thing which cares whether a number is
757 unsigned is the code in emit_expr which extends constants into
758 bignums. It should only sign extend negative numbers, so that
759 something like ``.quad 0x80000000'' is not sign extended even
760 though it appears negative if valueT is 32 bits. */
761 expressionP->X_unsigned = 1;
762
c593cf41
SC
763 /* digits, assume it is a bignum. */
764
2ed83a59
KR
765 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
766 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
c593cf41
SC
767
768 switch (c)
fecd2382 769 {
c593cf41
SC
770 case '1':
771 case '2':
772 case '3':
773 case '4':
774 case '5':
775 case '6':
776 case '7':
2ed83a59
KR
777 case '8':
778 case '9':
779 input_line_pointer--;
780
30d0557c 781 integer_constant (flag_m68k_mri ? 0 : 10, expressionP);
c593cf41
SC
782 break;
783
2ed83a59
KR
784 case '0':
785 /* non-decimal radix */
786
30d0557c 787 if (flag_m68k_mri)
219deb70
ILT
788 {
789 char *s;
790
791 /* Check for a hex constant. */
792 for (s = input_line_pointer; hex_p (*s); s++)
793 ;
794 if (*s == 'h' || *s == 'H')
795 {
796 --input_line_pointer;
797 integer_constant (0, expressionP);
798 break;
799 }
800 }
801
2ed83a59
KR
802 c = *input_line_pointer;
803 switch (c)
804 {
30d0557c
ILT
805 case 'o':
806 case 'O':
807 case 'q':
808 case 'Q':
3dce804d
ILT
809 case '8':
810 case '9':
30d0557c 811 if (flag_m68k_mri)
3dce804d
ILT
812 {
813 integer_constant (0, expressionP);
814 break;
815 }
816 /* Fall through. */
2ed83a59 817 default:
219deb70 818 default_case:
2ed83a59
KR
819 if (c && strchr (FLT_CHARS, c))
820 {
821 input_line_pointer++;
822 floating_constant (expressionP);
c246596a
ILT
823 expressionP->X_add_number =
824 - (isupper ((unsigned char) c) ? tolower (c) : c);
2ed83a59
KR
825 }
826 else
827 {
828 /* The string was only zero */
5ac34ac3 829 expressionP->X_op = O_constant;
2ed83a59 830 expressionP->X_add_number = 0;
2ed83a59
KR
831 }
832
833 break;
834
835 case 'x':
836 case 'X':
30d0557c 837 if (flag_m68k_mri)
219deb70 838 goto default_case;
2ed83a59
KR
839 input_line_pointer++;
840 integer_constant (16, expressionP);
841 break;
842
843 case 'b':
30d0557c 844 if (LOCAL_LABELS_FB && ! flag_m68k_mri)
2ed83a59 845 {
30d0557c
ILT
846 /* This code used to check for '+' and '-' here, and, in
847 some conditions, fall through to call
848 integer_constant. However, that didn't make sense,
849 as integer_constant only accepts digits. */
850 /* Some of our code elsewhere does permit digits greater
851 than the expected base; for consistency, do the same
852 here. */
853 if (input_line_pointer[1] < '0'
854 || input_line_pointer[1] > '9')
219deb70 855 {
30d0557c
ILT
856 /* Parse this as a back reference to label 0. */
857 input_line_pointer--;
858 integer_constant (10, expressionP);
859 break;
219deb70 860 }
30d0557c 861 /* Otherwise, parse this as a binary number. */
2ed83a59 862 }
30d0557c 863 /* Fall through. */
2ed83a59
KR
864 case 'B':
865 input_line_pointer++;
30d0557c 866 if (flag_m68k_mri)
219deb70 867 goto default_case;
2ed83a59
KR
868 integer_constant (2, expressionP);
869 break;
870
871 case '0':
872 case '1':
873 case '2':
874 case '3':
875 case '4':
876 case '5':
877 case '6':
878 case '7':
30d0557c 879 integer_constant (flag_m68k_mri ? 0 : 8, expressionP);
2ed83a59
KR
880 break;
881
882 case 'f':
219deb70
ILT
883 if (LOCAL_LABELS_FB)
884 {
885 /* If it says "0f" and it could possibly be a floating point
886 number, make it one. Otherwise, make it a local label,
887 and try to deal with parsing the rest later. */
888 if (!input_line_pointer[1]
889 || (is_end_of_line[0xff & input_line_pointer[1]]))
890 goto is_0f_label;
d90f530b 891 {
219deb70
ILT
892 char *cp = input_line_pointer + 1;
893 int r = atof_generic (&cp, ".", EXP_CHARS,
894 &generic_floating_point_number);
895 switch (r)
896 {
897 case 0:
898 case ERROR_EXPONENT_OVERFLOW:
899 if (*cp == 'f' || *cp == 'b')
900 /* looks like a difference expression */
901 goto is_0f_label;
902 else
903 goto is_0f_float;
904 default:
905 as_fatal ("expr.c(operand): bad atof_generic return val %d",
906 r);
907 }
d90f530b 908 }
d90f530b 909
219deb70
ILT
910 /* Okay, now we've sorted it out. We resume at one of these
911 two labels, depending on what we've decided we're probably
912 looking at. */
913 is_0f_label:
914 input_line_pointer--;
915 integer_constant (10, expressionP);
916 break;
917
918 is_0f_float:
919 /* fall through */
920 ;
921 }
2ed83a59
KR
922
923 case 'd':
924 case 'D':
30d0557c
ILT
925 if (flag_m68k_mri)
926 {
927 integer_constant (0, expressionP);
928 break;
929 }
930 /* Fall through. */
2ed83a59
KR
931 case 'F':
932 case 'r':
933 case 'e':
934 case 'E':
935 case 'g':
936 case 'G':
2ed83a59
KR
937 input_line_pointer++;
938 floating_constant (expressionP);
c246596a
ILT
939 expressionP->X_add_number =
940 - (isupper ((unsigned char) c) ? tolower (c) : c);
2ed83a59
KR
941 break;
942
2ed83a59 943 case '$':
219deb70
ILT
944 if (LOCAL_LABELS_DOLLAR)
945 {
946 integer_constant (10, expressionP);
947 break;
948 }
949 else
950 goto default_case;
2ed83a59
KR
951 }
952
c593cf41 953 break;
5ac34ac3 954
2ed83a59 955 case '(':
d90f530b 956 case '[':
2ed83a59 957 /* didn't begin with digit & not a name */
58d4951d 958 segment = expression (expressionP);
5ac34ac3 959 /* Expression() will pass trailing whitespace */
219deb70
ILT
960 if ((c == '(' && *input_line_pointer++ != ')')
961 || (c == '[' && *input_line_pointer++ != ']'))
5ac34ac3
ILT
962 {
963 as_bad ("Missing ')' assumed");
964 input_line_pointer--;
965 }
30d0557c 966 SKIP_WHITESPACE ();
5ac34ac3 967 /* here with input_line_pointer->char after "(...)" */
58d4951d 968 return segment;
c593cf41 969
219deb70 970 case 'E':
30d0557c 971 if (! flag_m68k_mri || *input_line_pointer != '\'')
219deb70
ILT
972 goto de_fault;
973 as_bad ("EBCDIC constants are not supported");
974 /* Fall through. */
975 case 'A':
30d0557c 976 if (! flag_m68k_mri || *input_line_pointer != '\'')
219deb70
ILT
977 goto de_fault;
978 ++input_line_pointer;
979 /* Fall through. */
2ed83a59 980 case '\'':
30d0557c 981 if (! flag_m68k_mri)
219deb70
ILT
982 {
983 /* Warning: to conform to other people's assemblers NO
984 ESCAPEMENT is permitted for a single quote. The next
985 character, parity errors and all, is taken as the value
986 of the operand. VERY KINKY. */
987 expressionP->X_op = O_constant;
988 expressionP->X_add_number = *input_line_pointer++;
989 break;
990 }
991
992 mri_char_constant (expressionP);
2ed83a59
KR
993 break;
994
49864cfa 995 case '+':
58d4951d 996 (void) operand (expressionP);
49864cfa
KR
997 break;
998
219deb70 999 case '"':
30d0557c
ILT
1000 /* Double quote is the bitwise not operator in MRI mode. */
1001 if (! flag_m68k_mri)
219deb70
ILT
1002 goto de_fault;
1003 /* Fall through. */
2ed83a59 1004 case '~':
30d0557c
ILT
1005 /* ~ is permitted to start a label on the Delta. */
1006 if (is_name_beginner (c))
1007 goto isname;
1008 case '!':
2ed83a59 1009 case '-':
2ed83a59 1010 {
5ac34ac3
ILT
1011 operand (expressionP);
1012 if (expressionP->X_op == O_constant)
2ed83a59 1013 {
2ed83a59
KR
1014 /* input_line_pointer -> char after operand */
1015 if (c == '-')
1016 {
5ac34ac3 1017 expressionP->X_add_number = - expressionP->X_add_number;
d841bc49
KR
1018 /* Notice: '-' may overflow: no warning is given. This is
1019 compatible with other people's assemblers. Sigh. */
d4c8cbd8 1020 expressionP->X_unsigned = 0;
2ed83a59 1021 }
30d0557c 1022 else if (c == '~' || c == '"')
5ac34ac3 1023 expressionP->X_add_number = ~ expressionP->X_add_number;
30d0557c
ILT
1024 else
1025 expressionP->X_add_number = ! expressionP->X_add_number;
f2f7d044 1026 }
5ac34ac3
ILT
1027 else if (expressionP->X_op != O_illegal
1028 && expressionP->X_op != O_absent)
f2f7d044 1029 {
5ac34ac3 1030 expressionP->X_add_symbol = make_expr_symbol (expressionP);
2ed83a59 1031 if (c == '-')
5ac34ac3 1032 expressionP->X_op = O_uminus;
30d0557c 1033 else if (c == '~' || c == '"')
5ac34ac3 1034 expressionP->X_op = O_bit_not;
30d0557c
ILT
1035 else
1036 expressionP->X_op = O_logical_not;
5ac34ac3 1037 expressionP->X_add_number = 0;
c593cf41 1038 }
f2f7d044 1039 else
5ac34ac3
ILT
1040 as_warn ("Unary operator %c ignored because bad operand follows",
1041 c);
c593cf41 1042 }
2ed83a59
KR
1043 break;
1044
d90f530b 1045 case '$':
219deb70
ILT
1046 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1047 is defined. */
1048#ifndef DOLLAR_DOT
30d0557c 1049 if (! flag_m68k_mri)
219deb70 1050 goto de_fault;
d90f530b 1051#endif
30d0557c 1052 if (flag_m68k_mri && hex_p (*input_line_pointer))
219deb70
ILT
1053 {
1054 /* In MRI mode, $ is also used as the prefix for a
1055 hexadecimal constant. */
1056 integer_constant (16, expressionP);
1057 break;
1058 }
52e1cf9d
ILT
1059
1060 if (is_part_of_name (*input_line_pointer))
1061 goto isname;
1062
1063 current_location (expressionP);
1064 break;
1065
219deb70 1066 case '.':
2ed83a59
KR
1067 if (!is_part_of_name (*input_line_pointer))
1068 {
52e1cf9d 1069 current_location (expressionP);
2ed83a59 1070 break;
2ed83a59 1071 }
3dce804d
ILT
1072 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1073 && ! is_part_of_name (input_line_pointer[8]))
1074 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1075 && ! is_part_of_name (input_line_pointer[7])))
1076 {
1077 int start;
1078
1079 start = (input_line_pointer[1] == 't'
1080 || input_line_pointer[1] == 'T');
1081 input_line_pointer += start ? 8 : 7;
1082 SKIP_WHITESPACE ();
1083 if (*input_line_pointer != '(')
1084 as_bad ("syntax error in .startof. or .sizeof.");
1085 else
1086 {
1087 char *buf;
1088
1089 ++input_line_pointer;
1090 SKIP_WHITESPACE ();
1091 name = input_line_pointer;
1092 c = get_symbol_end ();
1093
1094 buf = (char *) xmalloc (strlen (name) + 10);
1095 if (start)
1096 sprintf (buf, ".startof.%s", name);
1097 else
1098 sprintf (buf, ".sizeof.%s", name);
1099 symbolP = symbol_make (buf);
1100 free (buf);
1101
1102 expressionP->X_op = O_symbol;
1103 expressionP->X_add_symbol = symbolP;
1104 expressionP->X_add_number = 0;
1105
1106 *input_line_pointer = c;
1107 SKIP_WHITESPACE ();
1108 if (*input_line_pointer != ')')
1109 as_bad ("syntax error in .startof. or .sizeof.");
1110 else
1111 ++input_line_pointer;
1112 }
1113 break;
1114 }
2ed83a59
KR
1115 else
1116 {
1117 goto isname;
2ed83a59
KR
1118 }
1119 case ',':
1120 case '\n':
f2f7d044 1121 case '\0':
0bd77bc4 1122 eol:
2ed83a59 1123 /* can't imagine any other kind of operand */
5ac34ac3 1124 expressionP->X_op = O_absent;
2ed83a59 1125 input_line_pointer--;
219deb70
ILT
1126 break;
1127
1128 case '%':
30d0557c 1129 if (! flag_m68k_mri)
219deb70
ILT
1130 goto de_fault;
1131 integer_constant (2, expressionP);
1132 break;
1133
1134 case '@':
30d0557c 1135 if (! flag_m68k_mri)
219deb70
ILT
1136 goto de_fault;
1137 integer_constant (8, expressionP);
1138 break;
1139
1140 case ':':
30d0557c 1141 if (! flag_m68k_mri)
219deb70
ILT
1142 goto de_fault;
1143
1144 /* In MRI mode, this is a floating point constant represented
1145 using hexadecimal digits. */
1146
1147 ++input_line_pointer;
1148 integer_constant (16, expressionP);
2ed83a59 1149 break;
0bd77bc4 1150
52e1cf9d 1151 case '*':
30d0557c 1152 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
52e1cf9d
ILT
1153 goto de_fault;
1154
1155 current_location (expressionP);
1156 break;
1157
2ed83a59 1158 default:
219deb70 1159 de_fault:
58d4951d 1160 if (is_end_of_line[(unsigned char) c])
0bd77bc4 1161 goto eol;
2ed83a59
KR
1162 if (is_name_beginner (c)) /* here if did not begin with a digit */
1163 {
1164 /*
d841bc49
KR
1165 * Identifier begins here.
1166 * This is kludged for speed, so code is repeated.
1167 */
2ed83a59
KR
1168 isname:
1169 name = --input_line_pointer;
1170 c = get_symbol_end ();
30d0557c
ILT
1171
1172#ifdef md_parse_name
1173 /* This is a hook for the backend to parse certain names
1174 specially in certain contexts. If a name always has a
1175 specific value, it can often be handled by simply
1176 entering it in the symbol table. */
1177 if (md_parse_name (name, expressionP))
1178 {
1179 *input_line_pointer = c;
1180 break;
1181 }
1182#endif
1183
1184#ifdef TC_I960
1185 /* The MRI i960 assembler permits
1186 lda sizeof code,g13
1187 FIXME: This should use md_parse_name. */
1188 if (flag_mri
1189 && (strcasecmp (name, "sizeof") == 0
1190 || strcasecmp (name, "startof") == 0))
1191 {
1192 int start;
1193 char *buf;
1194
1195 start = (name[1] == 't'
1196 || name[1] == 'T');
1197
1198 *input_line_pointer = c;
1199 SKIP_WHITESPACE ();
1200
1201 name = input_line_pointer;
1202 c = get_symbol_end ();
1203
1204 buf = (char *) xmalloc (strlen (name) + 10);
1205 if (start)
1206 sprintf (buf, ".startof.%s", name);
1207 else
1208 sprintf (buf, ".sizeof.%s", name);
1209 symbolP = symbol_make (buf);
1210 free (buf);
1211
1212 expressionP->X_op = O_symbol;
1213 expressionP->X_add_symbol = symbolP;
1214 expressionP->X_add_number = 0;
1215
1216 *input_line_pointer = c;
1217 SKIP_WHITESPACE ();
1218
1219 break;
1220 }
1221#endif
1222
2ed83a59 1223 symbolP = symbol_find_or_make (name);
5ac34ac3
ILT
1224
1225 /* If we have an absolute symbol or a reg, then we know its
1226 value now. */
58d4951d
ILT
1227 segment = S_GET_SEGMENT (symbolP);
1228 if (segment == absolute_section)
5ac34ac3
ILT
1229 {
1230 expressionP->X_op = O_constant;
1231 expressionP->X_add_number = S_GET_VALUE (symbolP);
1232 }
58d4951d 1233 else if (segment == reg_section)
5ac34ac3
ILT
1234 {
1235 expressionP->X_op = O_register;
1236 expressionP->X_add_number = S_GET_VALUE (symbolP);
1237 }
f2f7d044 1238 else
2ed83a59 1239 {
5ac34ac3 1240 expressionP->X_op = O_symbol;
2ed83a59 1241 expressionP->X_add_symbol = symbolP;
5ac34ac3 1242 expressionP->X_add_number = 0;
2ed83a59
KR
1243 }
1244 *input_line_pointer = c;
2ed83a59
KR
1245 }
1246 else
1247 {
219deb70
ILT
1248 /* Let the target try to parse it. Success is indicated by changing
1249 the X_op field to something other than O_absent and pointing
1250 input_line_pointer passed the expression. If it can't parse the
1251 expression, X_op and input_line_pointer should be unchanged. */
1252 expressionP->X_op = O_absent;
1253 --input_line_pointer;
1254 md_operand (expressionP);
1255 if (expressionP->X_op == O_absent)
1256 {
1257 ++input_line_pointer;
1258 as_bad ("Bad expression");
1259 expressionP->X_op = O_constant;
1260 expressionP->X_add_number = 0;
1261 }
2ed83a59 1262 }
219deb70 1263 break;
c593cf41 1264 }
c593cf41 1265
c593cf41
SC
1266 /*
1267 * It is more 'efficient' to clean up the expressionS when they are created.
1268 * Doing it here saves lines of code.
1269 */
1270 clean_up_expression (expressionP);
2ed83a59
KR
1271 SKIP_WHITESPACE (); /*->1st char after operand. */
1272 know (*input_line_pointer != ' ');
58d4951d 1273
009dc5e1
JL
1274 /* The PA port needs this information. */
1275 if (expressionP->X_add_symbol)
1276 expressionP->X_add_symbol->sy_used = 1;
1277
58d4951d
ILT
1278 switch (expressionP->X_op)
1279 {
1280 default:
1281 return absolute_section;
1282 case O_symbol:
1283 return S_GET_SEGMENT (expressionP->X_add_symbol);
1284 case O_register:
1285 return reg_section;
1286 }
2ed83a59 1287} /* operand() */
fecd2382
RP
1288\f
1289/* Internal. Simplify a struct expression for use by expr() */
1290
1291/*
1292 * In: address of a expressionS.
5ac34ac3 1293 * The X_op field of the expressionS may only take certain values.
fecd2382
RP
1294 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1295 * Out: expressionS may have been modified:
1296 * 'foo-foo' symbol references cancelled to 0,
5ac34ac3 1297 * which changes X_op from O_subtract to O_constant.
fecd2382
RP
1298 * Unused fields zeroed to help expr().
1299 */
1300
1301static void
c593cf41 1302clean_up_expression (expressionP)
dae92eab 1303 expressionS *expressionP;
fecd2382 1304{
5ac34ac3 1305 switch (expressionP->X_op)
2ed83a59 1306 {
5ac34ac3
ILT
1307 case O_illegal:
1308 case O_absent:
2ed83a59 1309 expressionP->X_add_number = 0;
5ac34ac3
ILT
1310 /* Fall through. */
1311 case O_big:
1312 case O_constant:
1313 case O_register:
2ed83a59 1314 expressionP->X_add_symbol = NULL;
5ac34ac3
ILT
1315 /* Fall through. */
1316 case O_symbol:
1317 case O_uminus:
1318 case O_bit_not:
1319 expressionP->X_op_symbol = NULL;
1320 break;
1321 case O_subtract:
1322 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1323 || ((expressionP->X_op_symbol->sy_frag
1324 == expressionP->X_add_symbol->sy_frag)
ffffc8fb 1325 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
5ac34ac3 1326 && (S_GET_VALUE (expressionP->X_op_symbol)
49864cfa 1327 == S_GET_VALUE (expressionP->X_add_symbol))))
2ed83a59 1328 {
3a762a0b
KR
1329 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1330 - S_GET_VALUE (expressionP->X_op_symbol));
d90f530b 1331
5ac34ac3 1332 expressionP->X_op = O_constant;
2ed83a59 1333 expressionP->X_add_symbol = NULL;
5ac34ac3 1334 expressionP->X_op_symbol = NULL;
d90f530b 1335 expressionP->X_add_number += diff;
fecd2382 1336 }
5ac34ac3
ILT
1337 break;
1338 default:
1339 break;
fecd2382 1340 }
f2f7d044 1341}
fecd2382
RP
1342\f
1343/* Expression parser. */
1344
1345/*
1346 * We allow an empty expression, and just assume (absolute,0) silently.
1347 * Unary operators and parenthetical expressions are treated as operands.
1348 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1349 *
1350 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1351 * warped that I flushed it and wrote a recursive-descent parser instead.
1352 * Now things are stable, would anybody like to write a fast parser?
1353 * Most expressions are either register (which does not even reach here)
1354 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1355 * So I guess it doesn't really matter how inefficient more complex expressions
1356 * are parsed.
1357 *
1358 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1359 * Also, we have consumed any leading or trailing spaces (operand does that)
1360 * and done all intervening operators.
5ac34ac3
ILT
1361 *
1362 * This returns the segment of the result, which will be
1363 * absolute_section or the segment of a symbol.
fecd2382
RP
1364 */
1365
49864cfa 1366#undef __
fecd2382
RP
1367#define __ O_illegal
1368
3dce804d 1369static operatorT op_encoding[256] =
2ed83a59
KR
1370{ /* maps ASCII->operators */
1371
1372 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1373 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1374
3dce804d 1375 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
2ed83a59
KR
1376 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1377 __, __, __, __, __, __, __, __,
219deb70 1378 __, __, __, __, O_lt, __, O_gt, __,
2ed83a59
KR
1379 __, __, __, __, __, __, __, __,
1380 __, __, __, __, __, __, __, __,
1381 __, __, __, __, __, __, __, __,
1382 __, __, __, __, __, __, O_bit_exclusive_or, __,
1383 __, __, __, __, __, __, __, __,
1384 __, __, __, __, __, __, __, __,
1385 __, __, __, __, __, __, __, __,
1386 __, __, __, __, O_bit_inclusive_or, __, __, __,
1387
1388 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1389 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1390 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1391 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1392 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1393 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1394 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1395 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1396};
fecd2382
RP
1397
1398
1399/*
1400 * Rank Examples
1401 * 0 operand, (expression)
30d0557c
ILT
1402 * 1 ||
1403 * 2 &&
1404 * 3 = <> < <= >= >
1405 * 4 + -
1406 * 5 used for * / % in MRI mode
1407 * 6 & ^ ! |
1408 * 7 * / % << >>
1409 * 8 unary - unary ~
fecd2382 1410 */
219deb70 1411static operator_rankT op_rank[] =
5ac34ac3
ILT
1412{
1413 0, /* O_illegal */
1414 0, /* O_absent */
1415 0, /* O_constant */
1416 0, /* O_symbol */
30d0557c 1417 0, /* O_symbol_rva */
5ac34ac3
ILT
1418 0, /* O_register */
1419 0, /* O_bit */
30d0557c
ILT
1420 8, /* O_uminus */
1421 8, /* O_bit_not */
1422 8, /* O_logical_not */
1423 7, /* O_multiply */
1424 7, /* O_divide */
1425 7, /* O_modulus */
1426 7, /* O_left_shift */
1427 7, /* O_right_shift */
1428 6, /* O_bit_inclusive_or */
1429 6, /* O_bit_or_not */
1430 6, /* O_bit_exclusive_or */
1431 6, /* O_bit_and */
1432 4, /* O_add */
1433 4, /* O_subtract */
1434 3, /* O_eq */
1435 3, /* O_ne */
1436 3, /* O_lt */
1437 3, /* O_le */
1438 3, /* O_ge */
1439 3, /* O_gt */
1440 2, /* O_logical_and */
1441 1 /* O_logical_or */
5ac34ac3 1442};
219deb70
ILT
1443
1444/* Initialize the expression parser. */
1445
1446void
1447expr_begin ()
1448{
30d0557c
ILT
1449 /* In MRI mode for the m68k, multiplication and division have lower
1450 precedence than the bit wise operators. */
1451 if (flag_m68k_mri)
219deb70 1452 {
30d0557c
ILT
1453 op_rank[O_multiply] = 5;
1454 op_rank[O_divide] = 5;
1455 op_rank[O_modulus] = 5;
3dce804d 1456 op_encoding['"'] = O_bit_not;
219deb70 1457 }
30d0557c
ILT
1458
1459 /* Verify that X_op field is wide enough. */
1460 {
1461 expressionS e;
1462 e.X_op = O_max;
1463 assert (e.X_op == O_max);
1464 }
219deb70 1465}
fecd2382 1466\f
219deb70
ILT
1467/* Return the encoding for the operator at INPUT_LINE_POINTER.
1468 Advance INPUT_LINE_POINTER to the last character in the operator
1469 (i.e., don't change it for a single character operator). */
1470
1471static inline operatorT
1472operator ()
1473{
1474 int c;
1475 operatorT ret;
1476
1477 c = *input_line_pointer;
1478
1479 switch (c)
1480 {
1481 default:
1482 return op_encoding[c];
1483
1484 case '<':
1485 switch (input_line_pointer[1])
1486 {
1487 default:
1488 return op_encoding[c];
1489 case '<':
1490 ret = O_left_shift;
1491 break;
1492 case '>':
1493 ret = O_ne;
1494 break;
1495 case '=':
1496 ret = O_le;
1497 break;
1498 }
1499 ++input_line_pointer;
1500 return ret;
1501
b463948b
JL
1502 case '=':
1503 if (input_line_pointer[1] != '=')
1504 return op_encoding[c];
1505
1506 ++input_line_pointer;
1507 return O_eq;
1508
219deb70
ILT
1509 case '>':
1510 switch (input_line_pointer[1])
1511 {
1512 default:
1513 return op_encoding[c];
1514 case '>':
1515 ret = O_right_shift;
1516 break;
1517 case '=':
1518 ret = O_ge;
1519 break;
1520 }
1521 ++input_line_pointer;
1522 return ret;
1523
1524 case '!':
1525 /* We accept !! as equivalent to ^ for MRI compatibility. */
1526 if (input_line_pointer[1] != '!')
1527 {
30d0557c 1528 if (flag_m68k_mri)
219deb70
ILT
1529 return O_bit_inclusive_or;
1530 return op_encoding[c];
1531 }
1532 ++input_line_pointer;
1533 return O_bit_exclusive_or;
30d0557c
ILT
1534
1535 case '|':
1536 if (input_line_pointer[1] != '|')
1537 return op_encoding[c];
1538
1539 ++input_line_pointer;
1540 return O_logical_or;
1541
1542 case '&':
1543 if (input_line_pointer[1] != '&')
1544 return op_encoding[c];
1545
1546 ++input_line_pointer;
1547 return O_logical_and;
219deb70
ILT
1548 }
1549
1550 /*NOTREACHED*/
e5d62150 1551}
219deb70
ILT
1552
1553/* Parse an expression. */
1554
5ac34ac3 1555segT
2ed83a59 1556expr (rank, resultP)
dae92eab
KR
1557 operator_rankT rank; /* Larger # is higher rank. */
1558 expressionS *resultP; /* Deliver result here. */
fecd2382 1559{
5ac34ac3 1560 segT retval;
2ed83a59 1561 expressionS right;
dae92eab 1562 operatorT op_left;
dae92eab 1563 operatorT op_right;
c593cf41 1564
2ed83a59 1565 know (rank >= 0);
5ac34ac3
ILT
1566
1567 retval = operand (resultP);
1568
2ed83a59 1569 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
5ac34ac3 1570
219deb70 1571 op_left = operator ();
2ed83a59 1572 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
fecd2382 1573 {
5ac34ac3
ILT
1574 segT rightseg;
1575
2ed83a59 1576 input_line_pointer++; /*->after 1st character of operator. */
5ac34ac3
ILT
1577
1578 rightseg = expr (op_rank[(int) op_left], &right);
1579 if (right.X_op == O_absent)
fecd2382 1580 {
5ac34ac3
ILT
1581 as_warn ("missing operand; zero assumed");
1582 right.X_op = O_constant;
1583 right.X_add_number = 0;
d4c8cbd8
JL
1584 right.X_add_symbol = NULL;
1585 right.X_op_symbol = NULL;
fecd2382 1586 }
5ac34ac3 1587
2ed83a59 1588 know (*input_line_pointer != ' ');
5ac34ac3 1589
58d4951d
ILT
1590 if (retval == undefined_section)
1591 {
1592 if (SEG_NORMAL (rightseg))
1593 retval = rightseg;
1594 }
1595 else if (! SEG_NORMAL (retval))
5ac34ac3
ILT
1596 retval = rightseg;
1597 else if (SEG_NORMAL (rightseg)
d4c8cbd8
JL
1598 && retval != rightseg
1599#ifdef DIFF_EXPR_OK
1600 && op_left != O_subtract
1601#endif
1602 )
5ac34ac3
ILT
1603 as_bad ("operation combines symbols in different segments");
1604
219deb70 1605 op_right = operator ();
5ac34ac3
ILT
1606
1607 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
30d0557c
ILT
1608 know ((int) op_left >= (int) O_multiply
1609 && (int) op_left <= (int) O_logical_or);
5ac34ac3 1610
c593cf41
SC
1611 /* input_line_pointer->after right-hand quantity. */
1612 /* left-hand quantity in resultP */
1613 /* right-hand quantity in right. */
1614 /* operator in op_left. */
5ac34ac3
ILT
1615
1616 if (resultP->X_op == O_big)
fecd2382 1617 {
219deb70
ILT
1618 as_warn ("left operand is a %s; integer 0 assumed",
1619 resultP->X_add_number > 0 ? "bignum" : "float");
5ac34ac3
ILT
1620 resultP->X_op = O_constant;
1621 resultP->X_add_number = 0;
1622 resultP->X_add_symbol = NULL;
1623 resultP->X_op_symbol = NULL;
fecd2382 1624 }
5ac34ac3 1625 if (right.X_op == O_big)
fecd2382 1626 {
219deb70
ILT
1627 as_warn ("right operand is a %s; integer 0 assumed",
1628 right.X_add_number > 0 ? "bignum" : "float");
5ac34ac3
ILT
1629 right.X_op = O_constant;
1630 right.X_add_number = 0;
1631 right.X_add_symbol = NULL;
1632 right.X_op_symbol = NULL;
1633 }
1634
1635 /* Optimize common cases. */
219deb70 1636 if (op_left == O_add && right.X_op == O_constant)
5ac34ac3
ILT
1637 {
1638 /* X + constant. */
1639 resultP->X_add_number += right.X_add_number;
1640 }
d90f530b
KR
1641 /* This case comes up in PIC code. */
1642 else if (op_left == O_subtract
1643 && right.X_op == O_symbol
1644 && resultP->X_op == O_symbol
1645 && (right.X_add_symbol->sy_frag
fcacfef6
JL
1646 == resultP->X_add_symbol->sy_frag)
1647 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1648
d90f530b 1649 {
b463948b 1650 resultP->X_add_number -= right.X_add_number;
d90f530b
KR
1651 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1652 - S_GET_VALUE (right.X_add_symbol));
1653 resultP->X_op = O_constant;
1654 resultP->X_add_symbol = 0;
1655 }
5ac34ac3
ILT
1656 else if (op_left == O_subtract && right.X_op == O_constant)
1657 {
1658 /* X - constant. */
1659 resultP->X_add_number -= right.X_add_number;
1660 }
1661 else if (op_left == O_add && resultP->X_op == O_constant)
1662 {
1663 /* Constant + X. */
1664 resultP->X_op = right.X_op;
1665 resultP->X_add_symbol = right.X_add_symbol;
1666 resultP->X_op_symbol = right.X_op_symbol;
1667 resultP->X_add_number += right.X_add_number;
1668 retval = rightseg;
1669 }
1670 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1671 {
1672 /* Constant OP constant. */
1673 offsetT v = right.X_add_number;
1674 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
fecd2382 1675 {
5ac34ac3
ILT
1676 as_warn ("division by zero");
1677 v = 1;
fecd2382 1678 }
5ac34ac3 1679 switch (op_left)
fecd2382 1680 {
219deb70 1681 default: abort ();
5ac34ac3
ILT
1682 case O_multiply: resultP->X_add_number *= v; break;
1683 case O_divide: resultP->X_add_number /= v; break;
1684 case O_modulus: resultP->X_add_number %= v; break;
1685 case O_left_shift: resultP->X_add_number <<= v; break;
30d0557c
ILT
1686 case O_right_shift:
1687 /* We always use unsigned shifts, to avoid relying on
1688 characteristics of the compiler used to compile gas. */
1689 resultP->X_add_number =
1690 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1691 break;
5ac34ac3
ILT
1692 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1693 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1694 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1695 case O_bit_and: resultP->X_add_number &= v; break;
1696 case O_add: resultP->X_add_number += v; break;
1697 case O_subtract: resultP->X_add_number -= v; break;
219deb70
ILT
1698 case O_eq:
1699 resultP->X_add_number =
1700 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1701 break;
1702 case O_ne:
1703 resultP->X_add_number =
1704 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1705 break;
1706 case O_lt:
1707 resultP->X_add_number =
1708 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1709 break;
1710 case O_le:
1711 resultP->X_add_number =
1712 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1713 break;
1714 case O_ge:
1715 resultP->X_add_number =
1716 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1717 break;
1718 case O_gt:
1719 resultP->X_add_number =
1720 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1721 break;
30d0557c
ILT
1722 case O_logical_and:
1723 resultP->X_add_number = resultP->X_add_number && v;
1724 break;
1725 case O_logical_or:
1726 resultP->X_add_number = resultP->X_add_number || v;
1727 break;
fecd2382 1728 }
5ac34ac3
ILT
1729 }
1730 else if (resultP->X_op == O_symbol
1731 && right.X_op == O_symbol
1732 && (op_left == O_add
1733 || op_left == O_subtract
1734 || (resultP->X_add_number == 0
1735 && right.X_add_number == 0)))
1736 {
1737 /* Symbol OP symbol. */
1738 resultP->X_op = op_left;
1739 resultP->X_op_symbol = right.X_add_symbol;
c593cf41 1740 if (op_left == O_add)
5ac34ac3
ILT
1741 resultP->X_add_number += right.X_add_number;
1742 else if (op_left == O_subtract)
1743 resultP->X_add_number -= right.X_add_number;
1744 }
1745 else
1746 {
1747 /* The general case. */
1748 resultP->X_add_symbol = make_expr_symbol (resultP);
1749 resultP->X_op_symbol = make_expr_symbol (&right);
1750 resultP->X_op = op_left;
1751 resultP->X_add_number = 0;
d4c8cbd8 1752 resultP->X_unsigned = 1;
5ac34ac3 1753 }
351878df 1754
2ed83a59 1755 op_left = op_right;
fecd2382 1756 } /* While next operator is >= this rank. */
5ac34ac3 1757
009dc5e1
JL
1758 /* The PA port needs this information. */
1759 if (resultP->X_add_symbol)
1760 resultP->X_add_symbol->sy_used = 1;
1761
5ac34ac3 1762 return resultP->X_op == O_constant ? absolute_section : retval;
fecd2382
RP
1763}
1764\f
1765/*
1766 * get_symbol_end()
1767 *
1768 * This lives here because it belongs equally in expr.c & read.c.
1769 * Expr.c is just a branch office read.c anyway, and putting it
1770 * here lessens the crowd at read.c.
1771 *
1772 * Assume input_line_pointer is at start of symbol name.
1773 * Advance input_line_pointer past symbol name.
1774 * Turn that character into a '\0', returning its former value.
1775 * This allows a string compare (RMS wants symbol names to be strings)
1776 * of the symbol name.
1777 * There will always be a char following symbol name, because all good
1778 * lines end in end-of-line.
1779 */
1780char
2ed83a59 1781get_symbol_end ()
fecd2382 1782{
dae92eab 1783 char c;
2ed83a59 1784
3dce804d
ILT
1785 /* We accept \001 in a name in case this is being called with a
1786 constructed string. */
30d0557c
ILT
1787 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1788 while (is_part_of_name (c = *input_line_pointer++)
1789 || c == '\001')
1790 ;
2ed83a59
KR
1791 *--input_line_pointer = 0;
1792 return (c);
fecd2382
RP
1793}
1794
a39116f1 1795
351878df 1796unsigned int
2ed83a59 1797get_single_number ()
a39116f1 1798{
2ed83a59
KR
1799 expressionS exp;
1800 operand (&exp);
1801 return exp.X_add_number;
1802
a39116f1 1803}
2ed83a59 1804
8b228fe9 1805/* end of expr.c */
This page took 0.315968 seconds and 4 git commands to generate.