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