* expr.c (operand): Remove `if (0 && ..)' statement and
[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,
99630778 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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);
fee9cbc7
AM
304 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
305 we distinguish between 'B' and 'b'. This is the case for
306 Z80. */
307 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
252b5132
RH
308 radix = 2;
309 else if (c == 'D')
310 radix = 10;
311 else if (c == 'O' || c == 'Q')
312 radix = 8;
313 else if (c == 'H')
314 radix = 16;
315 else if (suffix[1] == '.' || c == 'E' || flt)
316 {
317 floating_constant (expressionP);
318 return;
319 }
320 else
321 {
322 radix = 10;
323 suffix = NULL;
324 }
325 }
326 }
327
328 switch (radix)
329 {
330 case 2:
331 maxdig = 2;
332 too_many_digits = valuesize + 1;
333 break;
334 case 8:
335 maxdig = radix = 8;
336 too_many_digits = (valuesize + 2) / 3 + 1;
337 break;
338 case 16:
339 maxdig = radix = 16;
340 too_many_digits = (valuesize + 3) / 4 + 1;
341 break;
342 case 10:
343 maxdig = radix = 10;
929b12bc 344 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
252b5132
RH
345 }
346#undef valuesize
347 start = input_line_pointer;
348 c = *input_line_pointer++;
349 for (number = 0;
350 (digit = hex_value (c)) < maxdig;
351 c = *input_line_pointer++)
352 {
353 number = number * radix + digit;
354 }
929b12bc
KH
355 /* c contains character after number. */
356 /* input_line_pointer->char after c. */
252b5132
RH
357 small = (input_line_pointer - start - 1) < too_many_digits;
358
929b12bc 359 if (radix == 16 && c == '_')
252b5132
RH
360 {
361 /* This is literal of the form 0x333_0_12345678_1.
02000999 362 This example is equivalent to 0x00000333000000001234567800000001. */
252b5132
RH
363
364 int num_little_digits = 0;
365 int i;
929b12bc 366 input_line_pointer = start; /* -> 1st digit. */
252b5132
RH
367
368 know (LITTLENUM_NUMBER_OF_BITS == 16);
369
929b12bc 370 for (c = '_'; c == '_'; num_little_digits += 2)
252b5132
RH
371 {
372
929b12bc
KH
373 /* Convert one 64-bit word. */
374 int ndigit = 0;
252b5132
RH
375 number = 0;
376 for (c = *input_line_pointer++;
377 (digit = hex_value (c)) < maxdig;
378 c = *(input_line_pointer++))
379 {
380 number = number * radix + digit;
381 ndigit++;
382 }
383
384 /* Check for 8 digit per word max. */
929b12bc 385 if (ndigit > 8)
0e389e77 386 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
252b5132 387
929b12bc
KH
388 /* Add this chunk to the bignum.
389 Shift things down 2 little digits. */
252b5132 390 know (LITTLENUM_NUMBER_OF_BITS == 16);
929b12bc
KH
391 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
392 i >= 2;
393 i--)
394 generic_bignum[i] = generic_bignum[i - 2];
252b5132 395
929b12bc 396 /* Add the new digits as the least significant new ones. */
252b5132
RH
397 generic_bignum[0] = number & 0xffffffff;
398 generic_bignum[1] = number >> 16;
399 }
400
929b12bc 401 /* Again, c is char after number, input_line_pointer->after c. */
252b5132
RH
402
403 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
404 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
405
406 assert (num_little_digits >= 4);
407
408 if (num_little_digits != 8)
0e389e77 409 as_bad (_("a bignum with underscores must have exactly 4 words"));
252b5132
RH
410
411 /* We might have some leading zeros. These can be trimmed to give
929b12bc
KH
412 us a change to fit this constant into a small number. */
413 while (generic_bignum[num_little_digits - 1] == 0
414 && num_little_digits > 1)
252b5132 415 num_little_digits--;
929b12bc 416
252b5132
RH
417 if (num_little_digits <= 2)
418 {
929b12bc 419 /* will fit into 32 bits. */
252b5132
RH
420 number = generic_bignum_to_int32 ();
421 small = 1;
422 }
423#ifdef BFD64
424 else if (num_little_digits <= 4)
425 {
426 /* Will fit into 64 bits. */
427 number = generic_bignum_to_int64 ();
428 small = 1;
429 }
430#endif
431 else
432 {
433 small = 0;
929b12bc
KH
434
435 /* Number of littlenums in the bignum. */
436 number = num_little_digits;
252b5132
RH
437 }
438 }
439 else if (!small)
440 {
929b12bc
KH
441 /* We saw a lot of digits. manufacture a bignum the hard way. */
442 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
443 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
252b5132
RH
444 long carry;
445
446 leader = generic_bignum;
447 generic_bignum[0] = 0;
448 generic_bignum[1] = 0;
449 generic_bignum[2] = 0;
450 generic_bignum[3] = 0;
929b12bc 451 input_line_pointer = start; /* -> 1st digit. */
252b5132 452 c = *input_line_pointer++;
929b12bc 453 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
252b5132 454 {
929b12bc 455 for (pointer = generic_bignum; pointer <= leader; pointer++)
252b5132
RH
456 {
457 long work;
458
459 work = carry + radix * *pointer;
460 *pointer = work & LITTLENUM_MASK;
461 carry = work >> LITTLENUM_NUMBER_OF_BITS;
462 }
463 if (carry)
464 {
465 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
466 {
929b12bc 467 /* Room to grow a longer bignum. */
252b5132
RH
468 *++leader = carry;
469 }
470 }
471 }
929b12bc
KH
472 /* Again, c is char after number. */
473 /* input_line_pointer -> after c. */
252b5132
RH
474 know (LITTLENUM_NUMBER_OF_BITS == 16);
475 if (leader < generic_bignum + 2)
476 {
929b12bc 477 /* Will fit into 32 bits. */
252b5132
RH
478 number = generic_bignum_to_int32 ();
479 small = 1;
480 }
481#ifdef BFD64
482 else if (leader < generic_bignum + 4)
483 {
484 /* Will fit into 64 bits. */
485 number = generic_bignum_to_int64 ();
486 small = 1;
487 }
488#endif
489 else
490 {
929b12bc
KH
491 /* Number of littlenums in the bignum. */
492 number = leader - generic_bignum + 1;
252b5132
RH
493 }
494 }
495
60bcf0fa
NC
496 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
497 && suffix != NULL
f805106c 498 && input_line_pointer - 1 == suffix)
252b5132
RH
499 c = *input_line_pointer++;
500
501 if (small)
502 {
929b12bc
KH
503 /* Here with number, in correct radix. c is the next char.
504 Note that unlike un*x, we allow "011f" "0x9f" to both mean
505 the same as the (conventional) "9f".
506 This is simply easier than checking for strict canonical
507 form. Syntax sux! */
252b5132
RH
508
509 if (LOCAL_LABELS_FB && c == 'b')
510 {
929b12bc
KH
511 /* Backward ref to local label.
512 Because it is backward, expect it to be defined. */
252b5132
RH
513 /* Construct a local label. */
514 name = fb_label_name ((int) number, 0);
515
929b12bc 516 /* Seen before, or symbol is defined: OK. */
252b5132
RH
517 symbolP = symbol_find (name);
518 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
519 {
929b12bc
KH
520 /* Local labels are never absolute. Don't waste time
521 checking absoluteness. */
252b5132
RH
522 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
523
524 expressionP->X_op = O_symbol;
525 expressionP->X_add_symbol = symbolP;
526 }
527 else
528 {
929b12bc 529 /* Either not seen or not defined. */
252b5132
RH
530 /* @@ Should print out the original string instead of
531 the parsed number. */
0e389e77 532 as_bad (_("backward ref to unknown label \"%d:\""),
252b5132
RH
533 (int) number);
534 expressionP->X_op = O_constant;
535 }
536
537 expressionP->X_add_number = 0;
538 } /* case 'b' */
539 else if (LOCAL_LABELS_FB && c == 'f')
540 {
929b12bc
KH
541 /* Forward reference. Expect symbol to be undefined or
542 unknown. undefined: seen it before. unknown: never seen
543 it before.
544
545 Construct a local label name, then an undefined symbol.
546 Don't create a xseg frag for it: caller may do that.
547 Just return it as never seen before. */
252b5132
RH
548 name = fb_label_name ((int) number, 1);
549 symbolP = symbol_find_or_make (name);
929b12bc 550 /* We have no need to check symbol properties. */
252b5132 551#ifndef many_segments
929b12bc 552 /* Since "know" puts its arg into a "string", we
252b5132
RH
553 can't have newlines in the argument. */
554 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
555#endif
556 expressionP->X_op = O_symbol;
557 expressionP->X_add_symbol = symbolP;
558 expressionP->X_add_number = 0;
559 } /* case 'f' */
560 else if (LOCAL_LABELS_DOLLAR && c == '$')
561 {
562 /* If the dollar label is *currently* defined, then this is just
563 another reference to it. If it is not *currently* defined,
564 then this is a fresh instantiation of that number, so create
565 it. */
566
567 if (dollar_label_defined ((long) number))
568 {
569 name = dollar_label_name ((long) number, 0);
570 symbolP = symbol_find (name);
571 know (symbolP != NULL);
572 }
573 else
574 {
575 name = dollar_label_name ((long) number, 1);
576 symbolP = symbol_find_or_make (name);
577 }
578
579 expressionP->X_op = O_symbol;
580 expressionP->X_add_symbol = symbolP;
581 expressionP->X_add_number = 0;
582 } /* case '$' */
583 else
584 {
585 expressionP->X_op = O_constant;
252b5132 586 expressionP->X_add_number = number;
bc4466dc 587 input_line_pointer--; /* Restore following character. */
929b12bc 588 } /* Really just a number. */
252b5132
RH
589 }
590 else
591 {
bc4466dc 592 /* Not a small number. */
252b5132 593 expressionP->X_op = O_big;
929b12bc
KH
594 expressionP->X_add_number = number; /* Number of littlenums. */
595 input_line_pointer--; /* -> char following number. */
252b5132
RH
596 }
597}
598
599/* Parse an MRI multi character constant. */
600
601static void
dd625418 602mri_char_constant (expressionS *expressionP)
252b5132
RH
603{
604 int i;
605
606 if (*input_line_pointer == '\''
607 && input_line_pointer[1] != '\'')
608 {
609 expressionP->X_op = O_constant;
610 expressionP->X_add_number = 0;
611 return;
612 }
613
614 /* In order to get the correct byte ordering, we must build the
615 number in reverse. */
616 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
617 {
618 int j;
619
620 generic_bignum[i] = 0;
621 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
622 {
623 if (*input_line_pointer == '\'')
624 {
625 if (input_line_pointer[1] != '\'')
626 break;
627 ++input_line_pointer;
628 }
629 generic_bignum[i] <<= 8;
630 generic_bignum[i] += *input_line_pointer;
631 ++input_line_pointer;
632 }
633
634 if (i < SIZE_OF_LARGE_NUMBER - 1)
635 {
636 /* If there is more than one littlenum, left justify the
02000999
AM
637 last one to make it match the earlier ones. If there is
638 only one, we can just use the value directly. */
252b5132
RH
639 for (; j < CHARS_PER_LITTLENUM; j++)
640 generic_bignum[i] <<= 8;
641 }
642
643 if (*input_line_pointer == '\''
644 && input_line_pointer[1] != '\'')
645 break;
646 }
647
648 if (i < 0)
649 {
0e389e77 650 as_bad (_("character constant too large"));
252b5132
RH
651 i = 0;
652 }
653
654 if (i > 0)
655 {
656 int c;
657 int j;
658
659 c = SIZE_OF_LARGE_NUMBER - i;
660 for (j = 0; j < c; j++)
661 generic_bignum[j] = generic_bignum[i + j];
662 i = c;
663 }
664
665 know (LITTLENUM_NUMBER_OF_BITS == 16);
666 if (i > 2)
667 {
668 expressionP->X_op = O_big;
669 expressionP->X_add_number = i;
670 }
671 else
672 {
673 expressionP->X_op = O_constant;
674 if (i < 2)
675 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
676 else
677 expressionP->X_add_number =
678 (((generic_bignum[1] & LITTLENUM_MASK)
679 << LITTLENUM_NUMBER_OF_BITS)
680 | (generic_bignum[0] & LITTLENUM_MASK));
681 }
682
683 /* Skip the final closing quote. */
684 ++input_line_pointer;
685}
686
687/* Return an expression representing the current location. This
688 handles the magic symbol `.'. */
689
690static void
dd625418 691current_location (expressionS *expressionp)
252b5132
RH
692{
693 if (now_seg == absolute_section)
694 {
695 expressionp->X_op = O_constant;
696 expressionp->X_add_number = abs_section_offset;
697 }
698 else
699 {
252b5132 700 expressionp->X_op = O_symbol;
756d1d01 701 expressionp->X_add_symbol = symbol_temp_new_now ();
252b5132
RH
702 expressionp->X_add_number = 0;
703 }
704}
705
929b12bc
KH
706/* In: Input_line_pointer points to 1st char of operand, which may
707 be a space.
708
3b37fd66 709 Out: An expressionS.
929b12bc
KH
710 The operand may have been empty: in this case X_op == O_absent.
711 Input_line_pointer->(next non-blank) char after operand. */
252b5132
RH
712
713static segT
9497f5ac 714operand (expressionS *expressionP, enum expr_mode mode)
252b5132
RH
715{
716 char c;
929b12bc
KH
717 symbolS *symbolP; /* Points to symbol. */
718 char *name; /* Points to name of symbol. */
252b5132
RH
719 segT segment;
720
721 /* All integers are regarded as unsigned unless they are negated.
722 This is because the only thing which cares whether a number is
723 unsigned is the code in emit_expr which extends constants into
724 bignums. It should only sign extend negative numbers, so that
725 something like ``.quad 0x80000000'' is not sign extended even
726 though it appears negative if valueT is 32 bits. */
727 expressionP->X_unsigned = 1;
728
929b12bc 729 /* Digits, assume it is a bignum. */
252b5132 730
929b12bc 731 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
bc4466dc 732 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
252b5132 733
b75c0c92
AM
734 if (is_end_of_line[(unsigned char) c])
735 goto eol;
736
252b5132
RH
737 switch (c)
738 {
739 case '1':
740 case '2':
741 case '3':
742 case '4':
743 case '5':
744 case '6':
745 case '7':
746 case '8':
747 case '9':
748 input_line_pointer--;
749
60bcf0fa 750 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
929b12bc 751 ? 0 : 10,
411863a4 752 expressionP);
252b5132
RH
753 break;
754
ed6d6fd3
FCE
755#ifdef LITERAL_PREFIXDOLLAR_HEX
756 case '$':
c68012fb
JJ
757 /* $L is the start of a local label, not a hex constant. */
758 if (* input_line_pointer == 'L')
759 goto isname;
ed6d6fd3
FCE
760 integer_constant (16, expressionP);
761 break;
762#endif
763
977e771a
FCE
764#ifdef LITERAL_PREFIXPERCENT_BIN
765 case '%':
766 integer_constant (2, expressionP);
767 break;
768#endif
769
252b5132 770 case '0':
929b12bc 771 /* Non-decimal radix. */
252b5132 772
f805106c 773 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
774 {
775 char *s;
776
02000999 777 /* Check for a hex or float constant. */
252b5132
RH
778 for (s = input_line_pointer; hex_p (*s); s++)
779 ;
02000999 780 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
252b5132
RH
781 {
782 --input_line_pointer;
783 integer_constant (0, expressionP);
784 break;
785 }
929b12bc 786 }
252b5132
RH
787 c = *input_line_pointer;
788 switch (c)
789 {
790 case 'o':
791 case 'O':
792 case 'q':
793 case 'Q':
794 case '8':
795 case '9':
f805106c 796 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
797 {
798 integer_constant (0, expressionP);
799 break;
800 }
801 /* Fall through. */
802 default:
803 default_case:
804 if (c && strchr (FLT_CHARS, c))
805 {
806 input_line_pointer++;
807 floating_constant (expressionP);
3882b010 808 expressionP->X_add_number = - TOLOWER (c);
252b5132
RH
809 }
810 else
811 {
929b12bc 812 /* The string was only zero. */
252b5132
RH
813 expressionP->X_op = O_constant;
814 expressionP->X_add_number = 0;
815 }
816
817 break;
818
819 case 'x':
820 case 'X':
ab266a97 821 if (flag_m68k_mri)
252b5132
RH
822 goto default_case;
823 input_line_pointer++;
824 integer_constant (16, expressionP);
825 break;
826
827 case 'b':
6dc19fc4 828 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
252b5132
RH
829 {
830 /* This code used to check for '+' and '-' here, and, in
831 some conditions, fall through to call
832 integer_constant. However, that didn't make sense,
833 as integer_constant only accepts digits. */
834 /* Some of our code elsewhere does permit digits greater
835 than the expected base; for consistency, do the same
836 here. */
837 if (input_line_pointer[1] < '0'
838 || input_line_pointer[1] > '9')
839 {
840 /* Parse this as a back reference to label 0. */
841 input_line_pointer--;
842 integer_constant (10, expressionP);
843 break;
844 }
845 /* Otherwise, parse this as a binary number. */
846 }
847 /* Fall through. */
848 case 'B':
849 input_line_pointer++;
6dc19fc4 850 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
851 goto default_case;
852 integer_constant (2, expressionP);
853 break;
854
855 case '0':
856 case '1':
857 case '2':
858 case '3':
859 case '4':
860 case '5':
861 case '6':
862 case '7':
6dc19fc4 863 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
929b12bc
KH
864 ? 0 : 8,
865 expressionP);
252b5132
RH
866 break;
867
868 case 'f':
869 if (LOCAL_LABELS_FB)
870 {
871 /* If it says "0f" and it could possibly be a floating point
872 number, make it one. Otherwise, make it a local label,
873 and try to deal with parsing the rest later. */
874 if (!input_line_pointer[1]
271bb601
HPN
875 || (is_end_of_line[0xff & input_line_pointer[1]])
876 || strchr (FLT_CHARS, 'f') == NULL)
252b5132
RH
877 goto is_0f_label;
878 {
879 char *cp = input_line_pointer + 1;
880 int r = atof_generic (&cp, ".", EXP_CHARS,
881 &generic_floating_point_number);
882 switch (r)
883 {
884 case 0:
885 case ERROR_EXPONENT_OVERFLOW:
886 if (*cp == 'f' || *cp == 'b')
929b12bc 887 /* Looks like a difference expression. */
252b5132
RH
888 goto is_0f_label;
889 else if (cp == input_line_pointer + 1)
890 /* No characters has been accepted -- looks like
929b12bc 891 end of operand. */
252b5132
RH
892 goto is_0f_label;
893 else
894 goto is_0f_float;
895 default:
896 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
897 r);
898 }
899 }
900
901 /* Okay, now we've sorted it out. We resume at one of these
902 two labels, depending on what we've decided we're probably
903 looking at. */
904 is_0f_label:
905 input_line_pointer--;
906 integer_constant (10, expressionP);
907 break;
908
909 is_0f_float:
929b12bc 910 /* Fall through. */
252b5132
RH
911 ;
912 }
913
914 case 'd':
915 case 'D':
6dc19fc4 916 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
917 {
918 integer_constant (0, expressionP);
919 break;
920 }
921 /* Fall through. */
922 case 'F':
923 case 'r':
924 case 'e':
925 case 'E':
926 case 'g':
927 case 'G':
928 input_line_pointer++;
929 floating_constant (expressionP);
3882b010 930 expressionP->X_add_number = - TOLOWER (c);
252b5132
RH
931 break;
932
933 case '$':
934 if (LOCAL_LABELS_DOLLAR)
935 {
936 integer_constant (10, expressionP);
937 break;
938 }
939 else
940 goto default_case;
941 }
942
943 break;
944
945 case '(':
b585bc2c 946#ifndef NEED_INDEX_OPERATOR
252b5132 947 case '[':
b585bc2c 948#endif
929b12bc 949 /* Didn't begin with digit & not a name. */
9497f5ac
NC
950 if (mode != expr_defer)
951 segment = expression (expressionP);
952 else
953 segment = deferred_expression (expressionP);
929b12bc 954 /* expression () will pass trailing whitespace. */
f7c88872
AM
955 if ((c == '(' && *input_line_pointer != ')')
956 || (c == '[' && *input_line_pointer != ']'))
fbacee5b 957 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
f7c88872 958 else
929b12bc 959 input_line_pointer++;
252b5132 960 SKIP_WHITESPACE ();
929b12bc 961 /* Here with input_line_pointer -> char after "(...)". */
252b5132
RH
962 return segment;
963
abd63a32 964#ifdef TC_M68K
252b5132
RH
965 case 'E':
966 if (! flag_m68k_mri || *input_line_pointer != '\'')
967 goto de_fault;
968 as_bad (_("EBCDIC constants are not supported"));
969 /* Fall through. */
970 case 'A':
971 if (! flag_m68k_mri || *input_line_pointer != '\'')
972 goto de_fault;
973 ++input_line_pointer;
974 /* Fall through. */
abd63a32 975#endif
252b5132
RH
976 case '\'':
977 if (! flag_m68k_mri)
978 {
979 /* Warning: to conform to other people's assemblers NO
929b12bc 980 ESCAPEMENT is permitted for a single quote. The next
252b5132 981 character, parity errors and all, is taken as the value
929b12bc 982 of the operand. VERY KINKY. */
252b5132
RH
983 expressionP->X_op = O_constant;
984 expressionP->X_add_number = *input_line_pointer++;
985 break;
986 }
987
988 mri_char_constant (expressionP);
989 break;
990
abd63a32 991#ifdef TC_M68K
252b5132
RH
992 case '"':
993 /* Double quote is the bitwise not operator in MRI mode. */
994 if (! flag_m68k_mri)
995 goto de_fault;
996 /* Fall through. */
abd63a32 997#endif
252b5132 998 case '~':
929b12bc 999 /* '~' is permitted to start a label on the Delta. */
252b5132
RH
1000 if (is_name_beginner (c))
1001 goto isname;
1002 case '!':
1003 case '-':
43c3ab55 1004 case '+':
252b5132 1005 {
9497f5ac 1006 operand (expressionP, mode);
252b5132
RH
1007 if (expressionP->X_op == O_constant)
1008 {
929b12bc 1009 /* input_line_pointer -> char after operand. */
252b5132
RH
1010 if (c == '-')
1011 {
958b5f01 1012 expressionP->X_add_number = - expressionP->X_add_number;
929b12bc
KH
1013 /* Notice: '-' may overflow: no warning is given.
1014 This is compatible with other people's
1015 assemblers. Sigh. */
252b5132
RH
1016 expressionP->X_unsigned = 0;
1017 }
1018 else if (c == '~' || c == '"')
1019 expressionP->X_add_number = ~ expressionP->X_add_number;
43c3ab55 1020 else if (c == '!')
252b5132
RH
1021 expressionP->X_add_number = ! expressionP->X_add_number;
1022 }
02000999
AM
1023 else if (expressionP->X_op == O_big
1024 && expressionP->X_add_number <= 0
1025 && c == '-'
1026 && (generic_floating_point_number.sign == '+'
1027 || generic_floating_point_number.sign == 'P'))
1028 {
1029 /* Negative flonum (eg, -1.000e0). */
1030 if (generic_floating_point_number.sign == '+')
1031 generic_floating_point_number.sign = '-';
1032 else
1033 generic_floating_point_number.sign = 'N';
1034 }
b2221023
MM
1035 else if (expressionP->X_op == O_big
1036 && expressionP->X_add_number > 0)
1037 {
1038 int i;
1039
1040 if (c == '~' || c == '-')
1041 {
1042 for (i = 0; i < expressionP->X_add_number; ++i)
1043 generic_bignum[i] = ~generic_bignum[i];
1044 if (c == '-')
1045 for (i = 0; i < expressionP->X_add_number; ++i)
1046 {
1047 generic_bignum[i] += 1;
1048 if (generic_bignum[i])
1049 break;
1050 }
1051 }
1052 else if (c == '!')
1053 {
1054 int nonzero = 0;
1055 for (i = 0; i < expressionP->X_add_number; ++i)
1056 {
1057 if (generic_bignum[i])
1058 nonzero = 1;
1059 generic_bignum[i] = 0;
1060 }
1061 generic_bignum[0] = nonzero;
1062 }
1063 }
252b5132
RH
1064 else if (expressionP->X_op != O_illegal
1065 && expressionP->X_op != O_absent)
1066 {
43c3ab55
JB
1067 if (c != '+')
1068 {
1069 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1070 if (c == '-')
1071 expressionP->X_op = O_uminus;
1072 else if (c == '~' || c == '"')
1073 expressionP->X_op = O_bit_not;
1074 else
1075 expressionP->X_op = O_logical_not;
1076 expressionP->X_add_number = 0;
1077 }
252b5132
RH
1078 }
1079 else
1080 as_warn (_("Unary operator %c ignored because bad operand follows"),
1081 c);
1082 }
1083 break;
1084
abd63a32 1085#if defined (DOLLAR_DOT) || defined (TC_M68K)
252b5132 1086 case '$':
929b12bc 1087 /* '$' is the program counter when in MRI mode, or when
02000999 1088 DOLLAR_DOT is defined. */
252b5132
RH
1089#ifndef DOLLAR_DOT
1090 if (! flag_m68k_mri)
1091 goto de_fault;
1092#endif
4fa6945e 1093 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
252b5132 1094 {
4fa6945e
NC
1095 /* In MRI mode and on Z80, '$' is also used as the prefix
1096 for a hexadecimal constant. */
252b5132
RH
1097 integer_constant (16, expressionP);
1098 break;
1099 }
1100
1101 if (is_part_of_name (*input_line_pointer))
1102 goto isname;
1103
1104 current_location (expressionP);
1105 break;
abd63a32 1106#endif
252b5132
RH
1107
1108 case '.':
1109 if (!is_part_of_name (*input_line_pointer))
1110 {
1111 current_location (expressionP);
1112 break;
1113 }
1114 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1115 && ! is_part_of_name (input_line_pointer[8]))
1116 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1117 && ! is_part_of_name (input_line_pointer[7])))
1118 {
1119 int start;
1120
1121 start = (input_line_pointer[1] == 't'
1122 || input_line_pointer[1] == 'T');
1123 input_line_pointer += start ? 8 : 7;
1124 SKIP_WHITESPACE ();
1125 if (*input_line_pointer != '(')
1126 as_bad (_("syntax error in .startof. or .sizeof."));
1127 else
1128 {
1129 char *buf;
1130
1131 ++input_line_pointer;
1132 SKIP_WHITESPACE ();
1133 name = input_line_pointer;
1134 c = get_symbol_end ();
1135
1136 buf = (char *) xmalloc (strlen (name) + 10);
1137 if (start)
1138 sprintf (buf, ".startof.%s", name);
1139 else
1140 sprintf (buf, ".sizeof.%s", name);
1141 symbolP = symbol_make (buf);
1142 free (buf);
1143
1144 expressionP->X_op = O_symbol;
1145 expressionP->X_add_symbol = symbolP;
1146 expressionP->X_add_number = 0;
1147
1148 *input_line_pointer = c;
1149 SKIP_WHITESPACE ();
1150 if (*input_line_pointer != ')')
1151 as_bad (_("syntax error in .startof. or .sizeof."));
1152 else
1153 ++input_line_pointer;
1154 }
1155 break;
1156 }
1157 else
1158 {
1159 goto isname;
1160 }
b75c0c92 1161
252b5132 1162 case ',':
252b5132 1163 eol:
929b12bc 1164 /* Can't imagine any other kind of operand. */
252b5132
RH
1165 expressionP->X_op = O_absent;
1166 input_line_pointer--;
1167 break;
1168
abd63a32 1169#ifdef TC_M68K
252b5132
RH
1170 case '%':
1171 if (! flag_m68k_mri)
1172 goto de_fault;
1173 integer_constant (2, expressionP);
1174 break;
1175
1176 case '@':
1177 if (! flag_m68k_mri)
1178 goto de_fault;
1179 integer_constant (8, expressionP);
1180 break;
1181
1182 case ':':
1183 if (! flag_m68k_mri)
1184 goto de_fault;
1185
1186 /* In MRI mode, this is a floating point constant represented
02000999 1187 using hexadecimal digits. */
252b5132
RH
1188
1189 ++input_line_pointer;
1190 integer_constant (16, expressionP);
1191 break;
1192
1193 case '*':
1194 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1195 goto de_fault;
1196
1197 current_location (expressionP);
1198 break;
abd63a32 1199#endif
252b5132
RH
1200
1201 default:
abd63a32 1202#ifdef TC_M68K
252b5132 1203 de_fault:
abd63a32 1204#endif
929b12bc 1205 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
252b5132 1206 {
929b12bc
KH
1207 /* Identifier begins here.
1208 This is kludged for speed, so code is repeated. */
252b5132
RH
1209 isname:
1210 name = --input_line_pointer;
1211 c = get_symbol_end ();
1212
1213#ifdef md_parse_name
1214 /* This is a hook for the backend to parse certain names
02000999
AM
1215 specially in certain contexts. If a name always has a
1216 specific value, it can often be handled by simply
1217 entering it in the symbol table. */
9497f5ac 1218 if (md_parse_name (name, expressionP, mode, &c))
252b5132
RH
1219 {
1220 *input_line_pointer = c;
1221 break;
1222 }
1223#endif
1224
1225#ifdef TC_I960
1226 /* The MRI i960 assembler permits
1227 lda sizeof code,g13
1228 FIXME: This should use md_parse_name. */
1229 if (flag_mri
1230 && (strcasecmp (name, "sizeof") == 0
1231 || strcasecmp (name, "startof") == 0))
1232 {
1233 int start;
1234 char *buf;
1235
1236 start = (name[1] == 't'
1237 || name[1] == 'T');
1238
1239 *input_line_pointer = c;
1240 SKIP_WHITESPACE ();
1241
1242 name = input_line_pointer;
1243 c = get_symbol_end ();
1244
1245 buf = (char *) xmalloc (strlen (name) + 10);
1246 if (start)
1247 sprintf (buf, ".startof.%s", name);
1248 else
1249 sprintf (buf, ".sizeof.%s", name);
1250 symbolP = symbol_make (buf);
1251 free (buf);
1252
1253 expressionP->X_op = O_symbol;
1254 expressionP->X_add_symbol = symbolP;
1255 expressionP->X_add_number = 0;
1256
1257 *input_line_pointer = c;
1258 SKIP_WHITESPACE ();
1259
1260 break;
929b12bc 1261 }
252b5132
RH
1262#endif
1263
1264 symbolP = symbol_find_or_make (name);
1265
1266 /* If we have an absolute symbol or a reg, then we know its
1267 value now. */
1268 segment = S_GET_SEGMENT (symbolP);
9497f5ac 1269 if (mode != expr_defer && segment == absolute_section)
252b5132
RH
1270 {
1271 expressionP->X_op = O_constant;
1272 expressionP->X_add_number = S_GET_VALUE (symbolP);
1273 }
9497f5ac 1274 else if (mode != expr_defer && segment == reg_section)
252b5132
RH
1275 {
1276 expressionP->X_op = O_register;
1277 expressionP->X_add_number = S_GET_VALUE (symbolP);
1278 }
1279 else
1280 {
1281 expressionP->X_op = O_symbol;
1282 expressionP->X_add_symbol = symbolP;
1283 expressionP->X_add_number = 0;
1284 }
1285 *input_line_pointer = c;
1286 }
1287 else
1288 {
1289 /* Let the target try to parse it. Success is indicated by changing
1290 the X_op field to something other than O_absent and pointing
927781e2 1291 input_line_pointer past the expression. If it can't parse the
252b5132
RH
1292 expression, X_op and input_line_pointer should be unchanged. */
1293 expressionP->X_op = O_absent;
1294 --input_line_pointer;
1295 md_operand (expressionP);
1296 if (expressionP->X_op == O_absent)
1297 {
1298 ++input_line_pointer;
0e389e77 1299 as_bad (_("bad expression"));
252b5132
RH
1300 expressionP->X_op = O_constant;
1301 expressionP->X_add_number = 0;
1302 }
1303 }
1304 break;
1305 }
1306
929b12bc
KH
1307 /* It is more 'efficient' to clean up the expressionS when they are
1308 created. Doing it here saves lines of code. */
252b5132 1309 clean_up_expression (expressionP);
929b12bc 1310 SKIP_WHITESPACE (); /* -> 1st char after operand. */
252b5132
RH
1311 know (*input_line_pointer != ' ');
1312
1313 /* The PA port needs this information. */
1314 if (expressionP->X_add_symbol)
49309057 1315 symbol_mark_used (expressionP->X_add_symbol);
252b5132 1316
9497f5ac
NC
1317 expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1318 expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1319
252b5132
RH
1320 switch (expressionP->X_op)
1321 {
1322 default:
1323 return absolute_section;
1324 case O_symbol:
1325 return S_GET_SEGMENT (expressionP->X_add_symbol);
1326 case O_register:
1327 return reg_section;
1328 }
929b12bc 1329}
252b5132 1330\f
929b12bc
KH
1331/* Internal. Simplify a struct expression for use by expr (). */
1332
3b37fd66 1333/* In: address of an expressionS.
929b12bc
KH
1334 The X_op field of the expressionS may only take certain values.
1335 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1336
1337 Out: expressionS may have been modified:
929b12bc 1338 Unused fields zeroed to help expr (). */
252b5132
RH
1339
1340static void
dd625418 1341clean_up_expression (expressionS *expressionP)
252b5132
RH
1342{
1343 switch (expressionP->X_op)
1344 {
1345 case O_illegal:
1346 case O_absent:
1347 expressionP->X_add_number = 0;
1348 /* Fall through. */
1349 case O_big:
1350 case O_constant:
1351 case O_register:
1352 expressionP->X_add_symbol = NULL;
1353 /* Fall through. */
1354 case O_symbol:
1355 case O_uminus:
1356 case O_bit_not:
1357 expressionP->X_op_symbol = NULL;
1358 break;
252b5132
RH
1359 default:
1360 break;
1361 }
1362}
1363\f
929b12bc
KH
1364/* Expression parser. */
1365
1366/* We allow an empty expression, and just assume (absolute,0) silently.
1367 Unary operators and parenthetical expressions are treated as operands.
1368 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1369
3b37fd66 1370 We used to do an aho/ullman shift-reduce parser, but the logic got so
929b12bc
KH
1371 warped that I flushed it and wrote a recursive-descent parser instead.
1372 Now things are stable, would anybody like to write a fast parser?
1373 Most expressions are either register (which does not even reach here)
1374 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1375 So I guess it doesn't really matter how inefficient more complex expressions
1376 are parsed.
1377
1378 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1379 Also, we have consumed any leading or trailing spaces (operand does that)
1380 and done all intervening operators.
1381
1382 This returns the segment of the result, which will be
1383 absolute_section or the segment of a symbol. */
252b5132
RH
1384
1385#undef __
1386#define __ O_illegal
fb7ccfc1
AM
1387#ifndef O_SINGLE_EQ
1388#define O_SINGLE_EQ O_illegal
1389#endif
252b5132 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 __, __, __, __, __, __, __, __,
fb7ccfc1 1399 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
252b5132
RH
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 '-':
8c9f705e 1549 return op_encoding[c];
3e4caed2 1550
252b5132
RH
1551 case '<':
1552 switch (input_line_pointer[1])
1553 {
1554 default:
1555 return op_encoding[c];
1556 case '<':
1557 ret = O_left_shift;
1558 break;
1559 case '>':
1560 ret = O_ne;
1561 break;
1562 case '=':
1563 ret = O_le;
1564 break;
1565 }
6fad6acb 1566 *num_chars = 2;
252b5132
RH
1567 return ret;
1568
1569 case '=':
1570 if (input_line_pointer[1] != '=')
1571 return op_encoding[c];
1572
6fad6acb 1573 *num_chars = 2;
252b5132
RH
1574 return O_eq;
1575
1576 case '>':
1577 switch (input_line_pointer[1])
1578 {
1579 default:
1580 return op_encoding[c];
1581 case '>':
1582 ret = O_right_shift;
1583 break;
1584 case '=':
1585 ret = O_ge;
1586 break;
1587 }
6fad6acb 1588 *num_chars = 2;
252b5132
RH
1589 return ret;
1590
1591 case '!':
723a8472 1592 switch (input_line_pointer[1])
252b5132 1593 {
723a8472
NC
1594 case '!':
1595 /* We accept !! as equivalent to ^ for MRI compatibility. */
1596 *num_chars = 2;
1597 return O_bit_exclusive_or;
1598 case '=':
1599 /* We accept != as equivalent to <>. */
1600 *num_chars = 2;
1601 return O_ne;
1602 default:
252b5132
RH
1603 if (flag_m68k_mri)
1604 return O_bit_inclusive_or;
1605 return op_encoding[c];
1606 }
252b5132
RH
1607
1608 case '|':
1609 if (input_line_pointer[1] != '|')
1610 return op_encoding[c];
1611
6fad6acb 1612 *num_chars = 2;
252b5132
RH
1613 return O_logical_or;
1614
1615 case '&':
1616 if (input_line_pointer[1] != '&')
1617 return op_encoding[c];
1618
6fad6acb 1619 *num_chars = 2;
252b5132
RH
1620 return O_logical_and;
1621 }
1622
929b12bc 1623 /* NOTREACHED */
252b5132
RH
1624}
1625
1626/* Parse an expression. */
1627
1628segT
dd625418 1629expr (int rankarg, /* Larger # is higher rank. */
9497f5ac
NC
1630 expressionS *resultP, /* Deliver result here. */
1631 enum expr_mode mode /* Controls behavior. */)
252b5132 1632{
0561a208 1633 operator_rankT rank = (operator_rankT) rankarg;
252b5132
RH
1634 segT retval;
1635 expressionS right;
1636 operatorT op_left;
1637 operatorT op_right;
6fad6acb 1638 int op_chars;
252b5132
RH
1639
1640 know (rank >= 0);
1641
26346241
AM
1642 /* Save the value of dot for the fixup code. */
1643 if (rank == 0)
1644 dot_value = frag_now_fix ();
1645
9497f5ac 1646 retval = operand (resultP, mode);
252b5132 1647
929b12bc
KH
1648 /* operand () gobbles spaces. */
1649 know (*input_line_pointer != ' ');
252b5132 1650
6fad6acb 1651 op_left = operator (&op_chars);
252b5132
RH
1652 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1653 {
1654 segT rightseg;
99630778 1655 bfd_vma frag_off;
252b5132 1656
6fad6acb 1657 input_line_pointer += op_chars; /* -> after operator. */
252b5132 1658
9497f5ac 1659 rightseg = expr (op_rank[(int) op_left], &right, mode);
252b5132
RH
1660 if (right.X_op == O_absent)
1661 {
1662 as_warn (_("missing operand; zero assumed"));
1663 right.X_op = O_constant;
1664 right.X_add_number = 0;
1665 right.X_add_symbol = NULL;
1666 right.X_op_symbol = NULL;
1667 }
1668
1669 know (*input_line_pointer != ' ');
1670
b585bc2c
RH
1671 if (op_left == O_index)
1672 {
1673 if (*input_line_pointer != ']')
1674 as_bad ("missing right bracket");
1675 else
1676 {
1677 ++input_line_pointer;
1678 SKIP_WHITESPACE ();
1679 }
1680 }
1681
6fad6acb 1682 op_right = operator (&op_chars);
252b5132 1683
929b12bc
KH
1684 know (op_right == O_illegal
1685 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
252b5132 1686 know ((int) op_left >= (int) O_multiply
c9dea48b 1687 && (int) op_left <= (int) O_index);
252b5132 1688
929b12bc
KH
1689 /* input_line_pointer->after right-hand quantity. */
1690 /* left-hand quantity in resultP. */
1691 /* right-hand quantity in right. */
1692 /* operator in op_left. */
252b5132
RH
1693
1694 if (resultP->X_op == O_big)
1695 {
1696 if (resultP->X_add_number > 0)
1697 as_warn (_("left operand is a bignum; integer 0 assumed"));
1698 else
1699 as_warn (_("left operand is a float; integer 0 assumed"));
1700 resultP->X_op = O_constant;
1701 resultP->X_add_number = 0;
1702 resultP->X_add_symbol = NULL;
1703 resultP->X_op_symbol = NULL;
1704 }
1705 if (right.X_op == O_big)
1706 {
1707 if (right.X_add_number > 0)
1708 as_warn (_("right operand is a bignum; integer 0 assumed"));
1709 else
1710 as_warn (_("right operand is a float; integer 0 assumed"));
1711 right.X_op = O_constant;
1712 right.X_add_number = 0;
1713 right.X_add_symbol = NULL;
1714 right.X_op_symbol = NULL;
1715 }
1716
1717 /* Optimize common cases. */
800eeca4
JW
1718#ifdef md_optimize_expr
1719 if (md_optimize_expr (resultP, op_left, &right))
1720 {
929b12bc
KH
1721 /* Skip. */
1722 ;
800eeca4
JW
1723 }
1724 else
1725#endif
252b5132
RH
1726 if (op_left == O_add && right.X_op == O_constant)
1727 {
1728 /* X + constant. */
1729 resultP->X_add_number += right.X_add_number;
1730 }
1731 /* This case comes up in PIC code. */
1732 else if (op_left == O_subtract
1733 && right.X_op == O_symbol
1734 && resultP->X_op == O_symbol
99630778 1735 && retval == rightseg
f6cf69d7 1736 && (SEG_NORMAL (rightseg)
99630778
AM
1737 || right.X_add_symbol == resultP->X_add_symbol)
1738 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1739 symbol_get_frag (right.X_add_symbol),
1740 &frag_off))
252b5132
RH
1741 {
1742 resultP->X_add_number -= right.X_add_number;
99630778 1743 resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
252b5132
RH
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;
99630778 1897 bfd_vma frag_off;
9497f5ac
NC
1898
1899 switch (op)
1900 {
1901 default:
1902 return 0;
1903
1904 case O_constant:
1905 case O_register:
1906 left = 0;
1907 break;
1908
1909 case O_symbol:
1910 case O_symbol_rva:
2e1e12b1 1911 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
9497f5ac
NC
1912 return 0;
1913
1914 break;
1915
1916 case O_uminus:
1917 case O_bit_not:
1918 case O_logical_not:
2e1e12b1 1919 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
9497f5ac
NC
1920 return 0;
1921
1922 if (seg_left != absolute_section)
1923 return 0;
1924
1925 if (op == O_logical_not)
1926 left = !left;
1927 else if (op == O_uminus)
1928 left = -left;
1929 else
1930 left = ~left;
1931 op = O_constant;
1932 break;
1933
1934 case O_multiply:
1935 case O_divide:
1936 case O_modulus:
1937 case O_left_shift:
1938 case O_right_shift:
1939 case O_bit_inclusive_or:
1940 case O_bit_or_not:
1941 case O_bit_exclusive_or:
1942 case O_bit_and:
1943 case O_add:
1944 case O_subtract:
1945 case O_eq:
1946 case O_ne:
1947 case O_lt:
1948 case O_le:
1949 case O_ge:
1950 case O_gt:
1951 case O_logical_and:
1952 case O_logical_or:
2e1e12b1
JB
1953 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
1954 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
9497f5ac
NC
1955 return 0;
1956
1957 /* Simplify addition or subtraction of a constant by folding the
1958 constant into X_add_number. */
1959 if (op == O_add)
1960 {
1961 if (seg_right == absolute_section)
1962 {
1963 final_val += right;
1964 op = O_symbol;
1965 break;
1966 }
1967 else if (seg_left == absolute_section)
1968 {
1969 final_val += left;
1970 left = right;
1971 seg_left = seg_right;
2e1e12b1 1972 add_symbol = op_symbol;
9497f5ac
NC
1973 op = O_symbol;
1974 break;
1975 }
1976 }
1977 else if (op == O_subtract)
1978 {
1979 if (seg_right == absolute_section)
1980 {
1981 final_val -= right;
1982 op = O_symbol;
1983 break;
1984 }
1985 }
1986
1987 /* Equality and non-equality tests are permitted on anything.
1988 Subtraction, and other comparison operators are permitted if
2e1e12b1
JB
1989 both operands are in the same section.
1990 Shifts by constant zero are permitted on anything.
1991 Multiplies, bit-ors, and bit-ands with constant zero are
1992 permitted on anything.
1993 Multiplies and divides by constant one are permitted on
1994 anything.
1995 Binary operations with both operands being the same register
1996 or undefined symbol are permitted if the result doesn't depend
1997 on the input value.
1998 Otherwise, both operands must be absolute. We already handled
1999 the case of addition or subtraction of a constant above. */
99630778 2000 frag_off = 0;
9497f5ac
NC
2001 if (!(seg_left == absolute_section
2002 && seg_right == absolute_section)
2003 && !(op == O_eq || op == O_ne)
2004 && !((op == O_subtract
2005 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2006 && seg_left == seg_right
99630778
AM
2007 && (finalize_syms
2008 || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2e1e12b1
JB
2009 && (seg_left != reg_section || left == right)
2010 && (seg_left != undefined_section || add_symbol == op_symbol)))
2011 {
2012 if ((seg_left == absolute_section && left == 0)
2013 || (seg_right == absolute_section && right == 0))
2014 {
2015 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2016 {
2017 if (seg_right != absolute_section || right != 0)
2018 {
2019 seg_left = seg_right;
2020 left = right;
2021 add_symbol = op_symbol;
2022 }
2023 op = O_symbol;
2024 break;
2025 }
2026 else if (op == O_left_shift || op == O_right_shift)
2027 {
2028 if (seg_left != absolute_section || left != 0)
2029 {
2030 op = O_symbol;
2031 break;
2032 }
2033 }
2034 else if (op != O_multiply
2035 && op != O_bit_or_not && op != O_bit_and)
2036 return 0;
2037 }
2038 else if (op == O_multiply
2039 && seg_left == absolute_section && left == 1)
2040 {
2041 seg_left = seg_right;
2042 left = right;
2043 add_symbol = op_symbol;
2044 op = O_symbol;
2045 break;
2046 }
2047 else if ((op == O_multiply || op == O_divide)
2048 && seg_right == absolute_section && right == 1)
2049 {
2050 op = O_symbol;
2051 break;
2052 }
2053 else if (left != right
2054 || ((seg_left != reg_section || seg_right != reg_section)
2055 && (seg_left != undefined_section
2056 || seg_right != undefined_section
2057 || add_symbol != op_symbol)))
2058 return 0;
2059 else if (op == O_bit_and || op == O_bit_inclusive_or)
2060 {
2061 op = O_symbol;
2062 break;
2063 }
2064 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2065 return 0;
2066 }
9497f5ac 2067
99630778 2068 right += frag_off / OCTETS_PER_BYTE;
9497f5ac
NC
2069 switch (op)
2070 {
2071 case O_add: left += right; break;
2072 case O_subtract: left -= right; break;
2073 case O_multiply: left *= right; break;
2074 case O_divide:
2075 if (right == 0)
2076 return 0;
2077 left = (offsetT) left / (offsetT) right;
2078 break;
2079 case O_modulus:
2080 if (right == 0)
2081 return 0;
2082 left = (offsetT) left % (offsetT) right;
2083 break;
2084 case O_left_shift: left <<= right; break;
2085 case O_right_shift: left >>= right; break;
2086 case O_bit_inclusive_or: left |= right; break;
2087 case O_bit_or_not: left |= ~right; break;
2088 case O_bit_exclusive_or: left ^= right; break;
2089 case O_bit_and: left &= right; break;
2090 case O_eq:
2091 case O_ne:
2092 left = (left == right
2093 && seg_left == seg_right
2094 && (finalize_syms || frag_left == frag_right)
2e1e12b1 2095 && (seg_left != undefined_section
9497f5ac
NC
2096 || add_symbol == op_symbol)
2097 ? ~ (valueT) 0 : 0);
2098 if (op == O_ne)
2099 left = ~left;
2100 break;
2101 case O_lt:
2102 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2103 break;
2104 case O_le:
2105 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2106 break;
2107 case O_ge:
2108 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2109 break;
2110 case O_gt:
2111 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2112 break;
2113 case O_logical_and: left = left && right; break;
2114 case O_logical_or: left = left || right; break;
2115 default: abort ();
2116 }
2117
2118 op = O_constant;
2119 break;
2120 }
2121
2122 if (op == O_symbol)
2123 {
2124 if (seg_left == absolute_section)
2125 op = O_constant;
2126 else if (seg_left == reg_section && final_val == 0)
2127 op = O_register;
2e1e12b1
JB
2128 else if (add_symbol != expressionP->X_add_symbol)
2129 final_val += left;
2130 expressionP->X_add_symbol = add_symbol;
9497f5ac
NC
2131 }
2132 expressionP->X_op = op;
2133
2134 if (op == O_constant || op == O_register)
2135 final_val += left;
2136 expressionP->X_add_number = final_val;
2137
2138 return 1;
2139}
252b5132 2140\f
929b12bc
KH
2141/* This lives here because it belongs equally in expr.c & read.c.
2142 expr.c is just a branch office read.c anyway, and putting it
2143 here lessens the crowd at read.c.
2144
2145 Assume input_line_pointer is at start of symbol name.
2146 Advance input_line_pointer past symbol name.
2147 Turn that character into a '\0', returning its former value.
2148 This allows a string compare (RMS wants symbol names to be strings)
2149 of the symbol name.
2150 There will always be a char following symbol name, because all good
2151 lines end in end-of-line. */
2152
252b5132 2153char
dd625418 2154get_symbol_end (void)
252b5132
RH
2155{
2156 char c;
2157
2158 /* We accept \001 in a name in case this is being called with a
2159 constructed string. */
2160 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
58b5739a
RH
2161 {
2162 while (is_part_of_name (c = *input_line_pointer++)
2163 || c == '\001')
2164 ;
2165 if (is_name_ender (c))
2166 c = *input_line_pointer++;
2167 }
252b5132
RH
2168 *--input_line_pointer = 0;
2169 return (c);
2170}
2171
252b5132 2172unsigned int
dd625418 2173get_single_number (void)
252b5132
RH
2174{
2175 expressionS exp;
9497f5ac 2176 operand (&exp, expr_normal);
252b5132 2177 return exp.X_add_number;
252b5132 2178}
This page took 0.443806 seconds and 4 git commands to generate.