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