]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/expr.c
Change version number to 2.41.50 and regenerate files
[thirdparty/binutils-gdb.git] / gas / expr.c
CommitLineData
252b5132 1/* expr.c -operands, expressions-
d87bef3a 2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
252b5132
RH
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
929b12bc
KH
21/* This is really a branch office of as-read.c. I split it out to clearly
22 distinguish the world of expressions from the world of statements.
23 (It also gives smaller files to re-compile.)
24 Here, "operand"s are of expressions, not instructions. */
252b5132 25
252b5132
RH
26#define min(a, b) ((a) < (b) ? (a) : (b))
27
28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30
d85733c8 31#include <limits.h>
d85733c8
JB
32#ifndef CHAR_BIT
33#define CHAR_BIT 8
34#endif
35
5b7c81bd 36bool literal_prefix_dollar_hex = false;
e2d15955 37
dd625418 38static void clean_up_expression (expressionS * expressionP);
252b5132 39
252b5132
RH
40/* We keep a mapping of expression symbols to file positions, so that
41 we can provide better error messages. */
42
e6c774b4 43struct expr_symbol_line {
252b5132
RH
44 struct expr_symbol_line *next;
45 symbolS *sym;
3b4dbbbf 46 const char *file;
252b5132
RH
47 unsigned int line;
48};
49
50static struct expr_symbol_line *expr_symbol_lines;
529b6c24
JB
51
52static const expressionS zero = { .X_op = O_constant };
252b5132
RH
53\f
54/* Build a dummy symbol to hold a complex expression. This is how we
55 build expressions up out of other expressions. The symbol is put
56 into the fake section expr_section. */
57
58symbolS *
529b6c24 59make_expr_symbol (const expressionS *expressionP)
252b5132 60{
252b5132
RH
61 symbolS *symbolP;
62 struct expr_symbol_line *n;
63
64 if (expressionP->X_op == O_symbol
65 && expressionP->X_add_number == 0)
66 return expressionP->X_add_symbol;
67
68 if (expressionP->X_op == O_big)
69 {
70 /* This won't work, because the actual value is stored in
02000999
AM
71 generic_floating_point_number or generic_bignum, and we are
72 going to lose it if we haven't already. */
252b5132 73 if (expressionP->X_add_number > 0)
0e389e77 74 as_bad (_("bignum invalid"));
252b5132 75 else
0e389e77 76 as_bad (_("floating point number invalid"));
252b5132
RH
77 expressionP = &zero;
78 }
79
252b5132
RH
80 /* Putting constant symbols in absolute_section rather than
81 expr_section is convenient for the old a.out code, for which
82 S_GET_SEGMENT does not always retrieve the value put in by
83 S_SET_SEGMENT. */
756d1d01 84 symbolP = symbol_create (FAKE_LABEL_NAME,
252b5132
RH
85 (expressionP->X_op == O_constant
86 ? absolute_section
5a918ce7
JB
87 : expressionP->X_op == O_register
88 ? reg_section
89 : expr_section),
e01e1cee 90 &zero_address_frag, 0);
49309057 91 symbol_set_value_expression (symbolP, expressionP);
252b5132
RH
92
93 if (expressionP->X_op == O_constant)
6386f3a7 94 resolve_symbol_value (symbolP);
252b5132 95
951e757d 96 n = notes_alloc (sizeof (*n));
252b5132 97 n->sym = symbolP;
3b4dbbbf 98 n->file = as_where (&n->line);
252b5132
RH
99 n->next = expr_symbol_lines;
100 expr_symbol_lines = n;
101
102 return symbolP;
103}
104
105/* Return the file and line number for an expr symbol. Return
106 non-zero if something was found, 0 if no information is known for
107 the symbol. */
108
109int
3b4dbbbf 110expr_symbol_where (symbolS *sym, const char **pfile, unsigned int *pline)
252b5132 111{
ed9e98c2 112 struct expr_symbol_line *l;
252b5132
RH
113
114 for (l = expr_symbol_lines; l != NULL; l = l->next)
115 {
116 if (l->sym == sym)
117 {
118 *pfile = l->file;
119 *pline = l->line;
120 return 1;
121 }
122 }
123
124 return 0;
125}
162c6aef
JB
126
127/* Look up a previously used .startof. / .sizeof. symbol, or make a fresh
128 one. */
6eb099ae
AM
129static symbolS **seen[2];
130static unsigned int nr_seen[2];
162c6aef
JB
131
132static symbolS *
133symbol_lookup_or_make (const char *name, bool start)
134{
162c6aef
JB
135 char *buf = concat (start ? ".startof." : ".sizeof.", name, NULL);
136 symbolS *symbolP;
137 unsigned int i;
138
139 for (i = 0; i < nr_seen[start]; ++i)
140 {
141 symbolP = seen[start][i];
142
143 if (! symbolP)
144 break;
145
146 name = S_GET_NAME (symbolP);
147 if ((symbols_case_sensitive
12ef6830
JB
148 ? strcmp (buf, name)
149 : strcasecmp (buf, name)) == 0)
162c6aef
JB
150 {
151 free (buf);
152 return symbolP;
153 }
154 }
155
156 symbolP = symbol_make (buf);
157 free (buf);
158
159 if (i >= nr_seen[start])
160 {
161 unsigned int nr = (i + 1) * 2;
162
163 seen[start] = XRESIZEVEC (symbolS *, seen[start], nr);
164 nr_seen[start] = nr;
165 memset (&seen[start][i + 1], 0, (nr - i - 1) * sizeof(seen[0][0]));
166 }
167
168 seen[start][i] = symbolP;
169
170 return symbolP;
171}
252b5132
RH
172\f
173/* Utilities for building expressions.
174 Since complex expressions are recorded as symbols for use in other
175 expressions these return a symbolS * and not an expressionS *.
176 These explicitly do not take an "add_number" argument. */
177/* ??? For completeness' sake one might want expr_build_symbol.
178 It would just return its argument. */
179
180/* Build an expression for an unsigned constant.
181 The corresponding one for signed constants is missing because
182 there's currently no need for it. One could add an unsigned_p flag
183 but that seems more clumsy. */
184
185symbolS *
dd625418 186expr_build_uconstant (offsetT value)
252b5132
RH
187{
188 expressionS e;
189
190 e.X_op = O_constant;
191 e.X_add_number = value;
192 e.X_unsigned = 1;
956a6ba3 193 e.X_extrabit = 0;
252b5132
RH
194 return make_expr_symbol (&e);
195}
196
252b5132
RH
197/* Build an expression for the current location ('.'). */
198
199symbolS *
dd625418 200expr_build_dot (void)
252b5132
RH
201{
202 expressionS e;
203
204 current_location (&e);
e66a3432 205 return symbol_clone_if_forward_ref (make_expr_symbol (&e));
252b5132
RH
206}
207\f
929b12bc
KH
208/* Build any floating-point literal here.
209 Also build any bignum literal here. */
252b5132
RH
210
211/* Seems atof_machine can backscan through generic_bignum and hit whatever
212 happens to be loaded before it in memory. And its way too complicated
213 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
214 and never write into the early words, thus they'll always be zero.
215 I hate Dean's floating-point code. Bleh. */
216LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
e6c774b4
KH
217
218FLONUM_TYPE generic_floating_point_number = {
bc4466dc
KH
219 &generic_bignum[6], /* low. (JF: Was 0) */
220 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
221 0, /* leader. */
222 0, /* exponent. */
223 0 /* sign. */
252b5132 224};
929b12bc 225
252b5132
RH
226\f
227static void
dd625418 228floating_constant (expressionS *expressionP)
252b5132 229{
929b12bc 230 /* input_line_pointer -> floating-point constant. */
252b5132
RH
231 int error_code;
232
233 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
234 &generic_floating_point_number);
235
236 if (error_code)
237 {
238 if (error_code == ERROR_EXPONENT_OVERFLOW)
239 {
0e389e77 240 as_bad (_("bad floating-point constant: exponent overflow"));
252b5132
RH
241 }
242 else
243 {
0e389e77
AM
244 as_bad (_("bad floating-point constant: unknown error code=%d"),
245 error_code);
252b5132
RH
246 }
247 }
248 expressionP->X_op = O_big;
929b12bc
KH
249 /* input_line_pointer -> just after constant, which may point to
250 whitespace. */
252b5132
RH
251 expressionP->X_add_number = -1;
252}
253
32d71569 254uint32_t
dd625418 255generic_bignum_to_int32 (void)
252b5132 256{
32d71569
AM
257 return ((((uint32_t) generic_bignum[1] & LITTLENUM_MASK)
258 << LITTLENUM_NUMBER_OF_BITS)
259 | ((uint32_t) generic_bignum[0] & LITTLENUM_MASK));
252b5132
RH
260}
261
32d71569 262uint64_t
dd625418 263generic_bignum_to_int64 (void)
252b5132 264{
32d71569
AM
265 return ((((((((uint64_t) generic_bignum[3] & LITTLENUM_MASK)
266 << LITTLENUM_NUMBER_OF_BITS)
267 | ((uint64_t) generic_bignum[2] & LITTLENUM_MASK))
268 << LITTLENUM_NUMBER_OF_BITS)
269 | ((uint64_t) generic_bignum[1] & LITTLENUM_MASK))
270 << LITTLENUM_NUMBER_OF_BITS)
271 | ((uint64_t) generic_bignum[0] & LITTLENUM_MASK));
252b5132 272}
252b5132
RH
273
274static void
dd625418 275integer_constant (int radix, expressionS *expressionP)
252b5132 276{
929b12bc 277 char *start; /* Start of number. */
252b5132
RH
278 char *suffix = NULL;
279 char c;
929b12bc
KH
280 valueT number; /* Offset or (absolute) value. */
281 short int digit; /* Value of next digit in current radix. */
282 short int maxdig = 0; /* Highest permitted digit value. */
283 int too_many_digits = 0; /* If we see >= this number of. */
284 char *name; /* Points to name of symbol. */
285 symbolS *symbolP; /* Points to symbol. */
252b5132 286
929b12bc 287 int small; /* True if fits in 32 bits. */
252b5132 288
929b12bc 289 /* May be bignum, or may fit in 32 bits. */
252b5132
RH
290 /* Most numbers fit into 32 bits, and we want this case to be fast.
291 so we pretend it will fit into 32 bits. If, after making up a 32
292 bit number, we realise that we have scanned more digits than
293 comfortably fit into 32 bits, we re-scan the digits coding them
294 into a bignum. For decimal and octal numbers we are
295 conservative: Some numbers may be assumed bignums when in fact
296 they do fit into 32 bits. Numbers of any radix can have excess
297 leading zeros: We strive to recognise this and cast them back
298 into 32 bits. We must check that the bignum really is more than
299 32 bits, and change it back to a 32-bit number if it fits. The
300 number we are looking for is expected to be positive, but if it
301 fits into 32 bits as an unsigned number, we let it be a 32-bit
929b12bc 302 number. The cavalier approach is for speed in ordinary cases. */
252b5132
RH
303 /* This has been extended for 64 bits. We blindly assume that if
304 you're compiling in 64-bit mode, the target is a 64-bit machine.
305 This should be cleaned up. */
306
307#ifdef BFD64
308#define valuesize 64
309#else /* includes non-bfd case, mostly */
310#define valuesize 32
311#endif
312
c14c7a8a
AM
313 if (is_end_of_line[(unsigned char) *input_line_pointer])
314 {
315 expressionP->X_op = O_absent;
316 return;
317 }
318
f805106c 319 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
252b5132
RH
320 {
321 int flt = 0;
322
323 /* In MRI mode, the number may have a suffix indicating the
02000999
AM
324 radix. For that matter, it might actually be a floating
325 point constant. */
3882b010 326 for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
252b5132
RH
327 {
328 if (*suffix == 'e' || *suffix == 'E')
329 flt = 1;
330 }
331
332 if (suffix == input_line_pointer)
333 {
334 radix = 10;
335 suffix = NULL;
336 }
337 else
338 {
339 c = *--suffix;
3882b010 340 c = TOUPPER (c);
fee9cbc7
AM
341 /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
342 we distinguish between 'B' and 'b'. This is the case for
343 Z80. */
344 if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
252b5132
RH
345 radix = 2;
346 else if (c == 'D')
347 radix = 10;
348 else if (c == 'O' || c == 'Q')
349 radix = 8;
350 else if (c == 'H')
351 radix = 16;
352 else if (suffix[1] == '.' || c == 'E' || flt)
353 {
354 floating_constant (expressionP);
355 return;
356 }
357 else
358 {
359 radix = 10;
360 suffix = NULL;
361 }
362 }
363 }
364
365 switch (radix)
366 {
367 case 2:
368 maxdig = 2;
369 too_many_digits = valuesize + 1;
370 break;
371 case 8:
372 maxdig = radix = 8;
373 too_many_digits = (valuesize + 2) / 3 + 1;
374 break;
375 case 16:
376 maxdig = radix = 16;
377 too_many_digits = (valuesize + 3) / 4 + 1;
378 break;
379 case 10:
380 maxdig = radix = 10;
929b12bc 381 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
252b5132
RH
382 }
383#undef valuesize
384 start = input_line_pointer;
385 c = *input_line_pointer++;
386 for (number = 0;
387 (digit = hex_value (c)) < maxdig;
388 c = *input_line_pointer++)
389 {
390 number = number * radix + digit;
391 }
929b12bc
KH
392 /* c contains character after number. */
393 /* input_line_pointer->char after c. */
252b5132
RH
394 small = (input_line_pointer - start - 1) < too_many_digits;
395
929b12bc 396 if (radix == 16 && c == '_')
252b5132
RH
397 {
398 /* This is literal of the form 0x333_0_12345678_1.
02000999 399 This example is equivalent to 0x00000333000000001234567800000001. */
252b5132
RH
400
401 int num_little_digits = 0;
402 int i;
929b12bc 403 input_line_pointer = start; /* -> 1st digit. */
252b5132
RH
404
405 know (LITTLENUM_NUMBER_OF_BITS == 16);
406
929b12bc 407 for (c = '_'; c == '_'; num_little_digits += 2)
252b5132
RH
408 {
409
929b12bc
KH
410 /* Convert one 64-bit word. */
411 int ndigit = 0;
252b5132
RH
412 number = 0;
413 for (c = *input_line_pointer++;
414 (digit = hex_value (c)) < maxdig;
415 c = *(input_line_pointer++))
416 {
417 number = number * radix + digit;
418 ndigit++;
419 }
420
421 /* Check for 8 digit per word max. */
929b12bc 422 if (ndigit > 8)
0e389e77 423 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
252b5132 424
929b12bc
KH
425 /* Add this chunk to the bignum.
426 Shift things down 2 little digits. */
252b5132 427 know (LITTLENUM_NUMBER_OF_BITS == 16);
929b12bc
KH
428 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
429 i >= 2;
430 i--)
431 generic_bignum[i] = generic_bignum[i - 2];
252b5132 432
929b12bc 433 /* Add the new digits as the least significant new ones. */
252b5132
RH
434 generic_bignum[0] = number & 0xffffffff;
435 generic_bignum[1] = number >> 16;
436 }
437
929b12bc 438 /* Again, c is char after number, input_line_pointer->after c. */
252b5132
RH
439
440 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
441 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
442
9c2799c2 443 gas_assert (num_little_digits >= 4);
252b5132
RH
444
445 if (num_little_digits != 8)
0e389e77 446 as_bad (_("a bignum with underscores must have exactly 4 words"));
252b5132
RH
447
448 /* We might have some leading zeros. These can be trimmed to give
929b12bc
KH
449 us a change to fit this constant into a small number. */
450 while (generic_bignum[num_little_digits - 1] == 0
451 && num_little_digits > 1)
252b5132 452 num_little_digits--;
929b12bc 453
252b5132
RH
454 if (num_little_digits <= 2)
455 {
929b12bc 456 /* will fit into 32 bits. */
252b5132
RH
457 number = generic_bignum_to_int32 ();
458 small = 1;
459 }
460#ifdef BFD64
461 else if (num_little_digits <= 4)
462 {
463 /* Will fit into 64 bits. */
464 number = generic_bignum_to_int64 ();
465 small = 1;
466 }
467#endif
468 else
469 {
470 small = 0;
929b12bc
KH
471
472 /* Number of littlenums in the bignum. */
473 number = num_little_digits;
252b5132
RH
474 }
475 }
476 else if (!small)
477 {
929b12bc
KH
478 /* We saw a lot of digits. manufacture a bignum the hard way. */
479 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
480 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
252b5132
RH
481 long carry;
482
483 leader = generic_bignum;
484 generic_bignum[0] = 0;
485 generic_bignum[1] = 0;
486 generic_bignum[2] = 0;
487 generic_bignum[3] = 0;
929b12bc 488 input_line_pointer = start; /* -> 1st digit. */
252b5132 489 c = *input_line_pointer++;
929b12bc 490 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
252b5132 491 {
929b12bc 492 for (pointer = generic_bignum; pointer <= leader; pointer++)
252b5132
RH
493 {
494 long work;
495
496 work = carry + radix * *pointer;
497 *pointer = work & LITTLENUM_MASK;
498 carry = work >> LITTLENUM_NUMBER_OF_BITS;
499 }
500 if (carry)
501 {
502 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
503 {
929b12bc 504 /* Room to grow a longer bignum. */
252b5132
RH
505 *++leader = carry;
506 }
507 }
508 }
929b12bc
KH
509 /* Again, c is char after number. */
510 /* input_line_pointer -> after c. */
252b5132
RH
511 know (LITTLENUM_NUMBER_OF_BITS == 16);
512 if (leader < generic_bignum + 2)
513 {
929b12bc 514 /* Will fit into 32 bits. */
252b5132
RH
515 number = generic_bignum_to_int32 ();
516 small = 1;
517 }
518#ifdef BFD64
519 else if (leader < generic_bignum + 4)
520 {
521 /* Will fit into 64 bits. */
522 number = generic_bignum_to_int64 ();
523 small = 1;
524 }
525#endif
526 else
527 {
929b12bc
KH
528 /* Number of littlenums in the bignum. */
529 number = leader - generic_bignum + 1;
252b5132
RH
530 }
531 }
532
60bcf0fa
NC
533 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
534 && suffix != NULL
f805106c 535 && input_line_pointer - 1 == suffix)
252b5132
RH
536 c = *input_line_pointer++;
537
e140100a
NC
538#ifndef tc_allow_U_suffix
539#define tc_allow_U_suffix 1
540#endif
541 /* PR 19910: Look for, and ignore, a U suffix to the number. */
542 if (tc_allow_U_suffix && (c == 'U' || c == 'u'))
543 c = * input_line_pointer++;
544
86b80085
NC
545#ifndef tc_allow_L_suffix
546#define tc_allow_L_suffix 1
547#endif
548 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
549 if (tc_allow_L_suffix)
550 while (c == 'L' || c == 'l')
551 c = * input_line_pointer++;
552
252b5132
RH
553 if (small)
554 {
929b12bc
KH
555 /* Here with number, in correct radix. c is the next char.
556 Note that unlike un*x, we allow "011f" "0x9f" to both mean
557 the same as the (conventional) "9f".
558 This is simply easier than checking for strict canonical
559 form. Syntax sux! */
252b5132
RH
560
561 if (LOCAL_LABELS_FB && c == 'b')
562 {
929b12bc
KH
563 /* Backward ref to local label.
564 Because it is backward, expect it to be defined. */
252b5132 565 /* Construct a local label. */
19f7966e 566 name = fb_label_name (number, 0);
252b5132 567
929b12bc 568 /* Seen before, or symbol is defined: OK. */
252b5132
RH
569 symbolP = symbol_find (name);
570 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
571 {
252b5132
RH
572 expressionP->X_op = O_symbol;
573 expressionP->X_add_symbol = symbolP;
574 }
575 else
576 {
929b12bc 577 /* Either not seen or not defined. */
252b5132
RH
578 /* @@ Should print out the original string instead of
579 the parsed number. */
0e389e77 580 as_bad (_("backward ref to unknown label \"%d:\""),
252b5132
RH
581 (int) number);
582 expressionP->X_op = O_constant;
583 }
584
585 expressionP->X_add_number = 0;
586 } /* case 'b' */
587 else if (LOCAL_LABELS_FB && c == 'f')
588 {
929b12bc
KH
589 /* Forward reference. Expect symbol to be undefined or
590 unknown. undefined: seen it before. unknown: never seen
591 it before.
592
593 Construct a local label name, then an undefined symbol.
594 Don't create a xseg frag for it: caller may do that.
595 Just return it as never seen before. */
19f7966e 596 name = fb_label_name (number, 1);
252b5132 597 symbolP = symbol_find_or_make (name);
929b12bc 598 /* We have no need to check symbol properties. */
252b5132
RH
599 expressionP->X_op = O_symbol;
600 expressionP->X_add_symbol = symbolP;
601 expressionP->X_add_number = 0;
602 } /* case 'f' */
603 else if (LOCAL_LABELS_DOLLAR && c == '$')
604 {
605 /* If the dollar label is *currently* defined, then this is just
606 another reference to it. If it is not *currently* defined,
607 then this is a fresh instantiation of that number, so create
608 it. */
609
19f7966e 610 if (dollar_label_defined (number))
252b5132 611 {
19f7966e 612 name = dollar_label_name (number, 0);
252b5132
RH
613 symbolP = symbol_find (name);
614 know (symbolP != NULL);
615 }
616 else
617 {
19f7966e 618 name = dollar_label_name (number, 1);
252b5132
RH
619 symbolP = symbol_find_or_make (name);
620 }
621
622 expressionP->X_op = O_symbol;
623 expressionP->X_add_symbol = symbolP;
624 expressionP->X_add_number = 0;
625 } /* case '$' */
626 else
627 {
628 expressionP->X_op = O_constant;
252b5132 629 expressionP->X_add_number = number;
bc4466dc 630 input_line_pointer--; /* Restore following character. */
929b12bc 631 } /* Really just a number. */
252b5132
RH
632 }
633 else
634 {
bc4466dc 635 /* Not a small number. */
252b5132 636 expressionP->X_op = O_big;
929b12bc
KH
637 expressionP->X_add_number = number; /* Number of littlenums. */
638 input_line_pointer--; /* -> char following number. */
252b5132
RH
639 }
640}
641
642/* Parse an MRI multi character constant. */
643
644static void
dd625418 645mri_char_constant (expressionS *expressionP)
252b5132
RH
646{
647 int i;
648
649 if (*input_line_pointer == '\''
650 && input_line_pointer[1] != '\'')
651 {
652 expressionP->X_op = O_constant;
653 expressionP->X_add_number = 0;
654 return;
655 }
656
657 /* In order to get the correct byte ordering, we must build the
658 number in reverse. */
659 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
660 {
661 int j;
662
663 generic_bignum[i] = 0;
664 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
665 {
666 if (*input_line_pointer == '\'')
667 {
668 if (input_line_pointer[1] != '\'')
669 break;
670 ++input_line_pointer;
671 }
672 generic_bignum[i] <<= 8;
673 generic_bignum[i] += *input_line_pointer;
674 ++input_line_pointer;
675 }
676
677 if (i < SIZE_OF_LARGE_NUMBER - 1)
678 {
679 /* If there is more than one littlenum, left justify the
02000999
AM
680 last one to make it match the earlier ones. If there is
681 only one, we can just use the value directly. */
252b5132
RH
682 for (; j < CHARS_PER_LITTLENUM; j++)
683 generic_bignum[i] <<= 8;
684 }
685
686 if (*input_line_pointer == '\''
687 && input_line_pointer[1] != '\'')
688 break;
689 }
690
691 if (i < 0)
692 {
0e389e77 693 as_bad (_("character constant too large"));
252b5132
RH
694 i = 0;
695 }
696
697 if (i > 0)
698 {
699 int c;
700 int j;
701
702 c = SIZE_OF_LARGE_NUMBER - i;
703 for (j = 0; j < c; j++)
704 generic_bignum[j] = generic_bignum[i + j];
705 i = c;
706 }
707
708 know (LITTLENUM_NUMBER_OF_BITS == 16);
709 if (i > 2)
710 {
711 expressionP->X_op = O_big;
712 expressionP->X_add_number = i;
713 }
714 else
715 {
716 expressionP->X_op = O_constant;
717 if (i < 2)
718 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
719 else
720 expressionP->X_add_number =
721 (((generic_bignum[1] & LITTLENUM_MASK)
722 << LITTLENUM_NUMBER_OF_BITS)
723 | (generic_bignum[0] & LITTLENUM_MASK));
724 }
725
726 /* Skip the final closing quote. */
727 ++input_line_pointer;
728}
729
730/* Return an expression representing the current location. This
731 handles the magic symbol `.'. */
732
b7adb16d 733void
dd625418 734current_location (expressionS *expressionp)
252b5132
RH
735{
736 if (now_seg == absolute_section)
737 {
738 expressionp->X_op = O_constant;
739 expressionp->X_add_number = abs_section_offset;
740 }
741 else
742 {
252b5132 743 expressionp->X_op = O_symbol;
4a826962 744 expressionp->X_add_symbol = &dot_symbol;
252b5132
RH
745 expressionp->X_add_number = 0;
746 }
747}
748
529b6c24
JB
749#ifndef md_register_arithmetic
750# define md_register_arithmetic 1
751#endif
752
929b12bc
KH
753/* In: Input_line_pointer points to 1st char of operand, which may
754 be a space.
755
3b37fd66 756 Out: An expressionS.
929b12bc
KH
757 The operand may have been empty: in this case X_op == O_absent.
758 Input_line_pointer->(next non-blank) char after operand. */
252b5132
RH
759
760static segT
9497f5ac 761operand (expressionS *expressionP, enum expr_mode mode)
252b5132
RH
762{
763 char c;
929b12bc
KH
764 symbolS *symbolP; /* Points to symbol. */
765 char *name; /* Points to name of symbol. */
252b5132 766 segT segment;
6f5ee7a3 767 operatorT op = O_absent; /* For unary operators. */
252b5132
RH
768
769 /* All integers are regarded as unsigned unless they are negated.
770 This is because the only thing which cares whether a number is
771 unsigned is the code in emit_expr which extends constants into
772 bignums. It should only sign extend negative numbers, so that
773 something like ``.quad 0x80000000'' is not sign extended even
774 though it appears negative if valueT is 32 bits. */
775 expressionP->X_unsigned = 1;
956a6ba3 776 expressionP->X_extrabit = 0;
252b5132 777
929b12bc 778 /* Digits, assume it is a bignum. */
252b5132 779
929b12bc 780 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
bc4466dc 781 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
252b5132 782
b75c0c92
AM
783 if (is_end_of_line[(unsigned char) c])
784 goto eol;
785
252b5132
RH
786 switch (c)
787 {
788 case '1':
789 case '2':
790 case '3':
791 case '4':
792 case '5':
793 case '6':
794 case '7':
795 case '8':
796 case '9':
797 input_line_pointer--;
798
60bcf0fa 799 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
929b12bc 800 ? 0 : 10,
411863a4 801 expressionP);
252b5132
RH
802 break;
803
977e771a
FCE
804#ifdef LITERAL_PREFIXPERCENT_BIN
805 case '%':
806 integer_constant (2, expressionP);
807 break;
808#endif
809
252b5132 810 case '0':
929b12bc 811 /* Non-decimal radix. */
252b5132 812
f805106c 813 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
814 {
815 char *s;
816
02000999 817 /* Check for a hex or float constant. */
252b5132
RH
818 for (s = input_line_pointer; hex_p (*s); s++)
819 ;
02000999 820 if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
252b5132
RH
821 {
822 --input_line_pointer;
823 integer_constant (0, expressionP);
824 break;
825 }
929b12bc 826 }
252b5132
RH
827 c = *input_line_pointer;
828 switch (c)
829 {
830 case 'o':
831 case 'O':
832 case 'q':
833 case 'Q':
834 case '8':
835 case '9':
f805106c 836 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
837 {
838 integer_constant (0, expressionP);
839 break;
840 }
841 /* Fall through. */
842 default:
843 default_case:
844 if (c && strchr (FLT_CHARS, c))
845 {
846 input_line_pointer++;
847 floating_constant (expressionP);
3882b010 848 expressionP->X_add_number = - TOLOWER (c);
252b5132
RH
849 }
850 else
851 {
929b12bc 852 /* The string was only zero. */
252b5132
RH
853 expressionP->X_op = O_constant;
854 expressionP->X_add_number = 0;
855 }
856
857 break;
858
859 case 'x':
860 case 'X':
ab266a97 861 if (flag_m68k_mri)
252b5132
RH
862 goto default_case;
863 input_line_pointer++;
864 integer_constant (16, expressionP);
865 break;
866
867 case 'b':
c14c7a8a
AM
868 if (LOCAL_LABELS_FB && !flag_m68k_mri
869 && input_line_pointer[1] != '0'
870 && input_line_pointer[1] != '1')
252b5132 871 {
c14c7a8a
AM
872 /* Parse this as a back reference to label 0. */
873 input_line_pointer--;
874 integer_constant (10, expressionP);
875 break;
252b5132 876 }
c14c7a8a 877 /* Otherwise, parse this as a binary number. */
252b5132
RH
878 /* Fall through. */
879 case 'B':
c14c7a8a
AM
880 if (input_line_pointer[1] == '0'
881 || input_line_pointer[1] == '1')
882 {
883 input_line_pointer++;
884 integer_constant (2, expressionP);
885 break;
886 }
6dc19fc4 887 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
c14c7a8a
AM
888 input_line_pointer++;
889 goto default_case;
252b5132
RH
890
891 case '0':
892 case '1':
893 case '2':
894 case '3':
895 case '4':
896 case '5':
897 case '6':
898 case '7':
6dc19fc4 899 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
929b12bc
KH
900 ? 0 : 8,
901 expressionP);
252b5132
RH
902 break;
903
904 case 'f':
905 if (LOCAL_LABELS_FB)
906 {
19c2883a
AM
907 int is_label = 1;
908
252b5132
RH
909 /* If it says "0f" and it could possibly be a floating point
910 number, make it one. Otherwise, make it a local label,
911 and try to deal with parsing the rest later. */
19c2883a
AM
912 if (!is_end_of_line[(unsigned char) input_line_pointer[1]]
913 && strchr (FLT_CHARS, 'f') != NULL)
914 {
915 char *cp = input_line_pointer + 1;
916
917 atof_generic (&cp, ".", EXP_CHARS,
918 &generic_floating_point_number);
919
920 /* Was nothing parsed, or does it look like an
921 expression? */
922 is_label = (cp == input_line_pointer + 1
923 || (cp == input_line_pointer + 2
924 && (cp[-1] == '-' || cp[-1] == '+'))
925 || *cp == 'f'
926 || *cp == 'b');
927 }
928 if (is_label)
929 {
930 input_line_pointer--;
931 integer_constant (10, expressionP);
932 break;
933 }
252b5132 934 }
19c2883a 935 /* Fall through. */
252b5132
RH
936
937 case 'd':
938 case 'D':
6dc19fc4 939 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
940 {
941 integer_constant (0, expressionP);
942 break;
943 }
944 /* Fall through. */
945 case 'F':
946 case 'r':
947 case 'e':
948 case 'E':
949 case 'g':
950 case 'G':
951 input_line_pointer++;
952 floating_constant (expressionP);
3882b010 953 expressionP->X_add_number = - TOLOWER (c);
252b5132
RH
954 break;
955
956 case '$':
957 if (LOCAL_LABELS_DOLLAR)
958 {
959 integer_constant (10, expressionP);
960 break;
961 }
962 else
963 goto default_case;
964 }
965
966 break;
967
b585bc2c 968#ifndef NEED_INDEX_OPERATOR
252b5132 969 case '[':
fcaed75e
JB
970# ifdef md_need_index_operator
971 if (md_need_index_operator())
972 goto de_fault;
973# endif
b585bc2c 974#endif
fcddde94 975 /* Fall through. */
fcaed75e 976 case '(':
929b12bc 977 /* Didn't begin with digit & not a name. */
259af69e 978 segment = expr (0, expressionP, mode);
929b12bc 979 /* expression () will pass trailing whitespace. */
f7c88872
AM
980 if ((c == '(' && *input_line_pointer != ')')
981 || (c == '[' && *input_line_pointer != ']'))
e140100a
NC
982 {
983 if (* input_line_pointer)
984 as_bad (_("found '%c', expected: '%c'"),
985 * input_line_pointer, c == '(' ? ')' : ']');
986 else
987 as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
988 }
f7c88872 989 else
929b12bc 990 input_line_pointer++;
4fd74b5f 991 SKIP_ALL_WHITESPACE ();
929b12bc 992 /* Here with input_line_pointer -> char after "(...)". */
252b5132
RH
993 return segment;
994
abd63a32 995#ifdef TC_M68K
252b5132
RH
996 case 'E':
997 if (! flag_m68k_mri || *input_line_pointer != '\'')
998 goto de_fault;
999 as_bad (_("EBCDIC constants are not supported"));
1000 /* Fall through. */
1001 case 'A':
1002 if (! flag_m68k_mri || *input_line_pointer != '\'')
1003 goto de_fault;
1004 ++input_line_pointer;
abd63a32 1005#endif
fcddde94 1006 /* Fall through. */
252b5132
RH
1007 case '\'':
1008 if (! flag_m68k_mri)
1009 {
1010 /* Warning: to conform to other people's assemblers NO
929b12bc 1011 ESCAPEMENT is permitted for a single quote. The next
252b5132 1012 character, parity errors and all, is taken as the value
929b12bc 1013 of the operand. VERY KINKY. */
252b5132
RH
1014 expressionP->X_op = O_constant;
1015 expressionP->X_add_number = *input_line_pointer++;
1016 break;
1017 }
1018
1019 mri_char_constant (expressionP);
1020 break;
1021
abd63a32 1022#ifdef TC_M68K
252b5132
RH
1023 case '"':
1024 /* Double quote is the bitwise not operator in MRI mode. */
1025 if (! flag_m68k_mri)
1026 goto de_fault;
abd63a32 1027#endif
fcddde94 1028 /* Fall through. */
252b5132 1029 case '~':
929b12bc 1030 /* '~' is permitted to start a label on the Delta. */
252b5132
RH
1031 if (is_name_beginner (c))
1032 goto isname;
6f5ee7a3
JB
1033 op = O_bit_not;
1034 goto unary;
1035
252b5132 1036 case '!':
6f5ee7a3
JB
1037 op = O_logical_not;
1038 goto unary;
1039
252b5132 1040 case '-':
6f5ee7a3
JB
1041 op = O_uminus;
1042 /* Fall through. */
43c3ab55 1043 case '+':
252b5132 1044 {
fcaed75e 1045 unary:
9497f5ac 1046 operand (expressionP, mode);
6f5ee7a3
JB
1047
1048#ifdef md_optimize_expr
1049 if (md_optimize_expr (NULL, op, expressionP))
1050 {
1051 /* Skip. */
1052 ;
1053 }
1054 else
1055#endif
252b5132
RH
1056 if (expressionP->X_op == O_constant)
1057 {
929b12bc 1058 /* input_line_pointer -> char after operand. */
6f5ee7a3 1059 if (op == O_uminus)
252b5132 1060 {
65879393
AM
1061 expressionP->X_add_number
1062 = - (addressT) expressionP->X_add_number;
929b12bc
KH
1063 /* Notice: '-' may overflow: no warning is given.
1064 This is compatible with other people's
1065 assemblers. Sigh. */
252b5132 1066 expressionP->X_unsigned = 0;
956a6ba3
JB
1067 if (expressionP->X_add_number)
1068 expressionP->X_extrabit ^= 1;
252b5132 1069 }
6f5ee7a3 1070 else if (op == O_bit_not)
32d71569
AM
1071 {
1072 expressionP->X_add_number = ~ expressionP->X_add_number;
1073 expressionP->X_extrabit ^= 1;
762acf21 1074 expressionP->X_unsigned = 0;
32d71569 1075 }
6f5ee7a3 1076 else if (op == O_logical_not)
32d71569
AM
1077 {
1078 expressionP->X_add_number = ! expressionP->X_add_number;
1079 expressionP->X_unsigned = 1;
1080 expressionP->X_extrabit = 0;
1081 }
252b5132 1082 }
02000999
AM
1083 else if (expressionP->X_op == O_big
1084 && expressionP->X_add_number <= 0
6f5ee7a3 1085 && op == O_uminus
02000999
AM
1086 && (generic_floating_point_number.sign == '+'
1087 || generic_floating_point_number.sign == 'P'))
1088 {
1089 /* Negative flonum (eg, -1.000e0). */
1090 if (generic_floating_point_number.sign == '+')
1091 generic_floating_point_number.sign = '-';
1092 else
1093 generic_floating_point_number.sign = 'N';
1094 }
b2221023
MM
1095 else if (expressionP->X_op == O_big
1096 && expressionP->X_add_number > 0)
1097 {
1098 int i;
1099
6f5ee7a3 1100 if (op == O_uminus || op == O_bit_not)
b2221023
MM
1101 {
1102 for (i = 0; i < expressionP->X_add_number; ++i)
1103 generic_bignum[i] = ~generic_bignum[i];
93d90f46
AM
1104
1105 /* Extend the bignum to at least the size of .octa. */
1106 if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1107 {
1108 expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1109 for (; i < expressionP->X_add_number; ++i)
1110 generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1111 }
1112
6f5ee7a3 1113 if (op == O_uminus)
b2221023
MM
1114 for (i = 0; i < expressionP->X_add_number; ++i)
1115 {
1116 generic_bignum[i] += 1;
1117 if (generic_bignum[i])
1118 break;
1119 }
1120 }
6f5ee7a3 1121 else if (op == O_logical_not)
b2221023 1122 {
b2221023 1123 for (i = 0; i < expressionP->X_add_number; ++i)
93d90f46
AM
1124 if (generic_bignum[i] != 0)
1125 break;
1126 expressionP->X_add_number = i >= expressionP->X_add_number;
1127 expressionP->X_op = O_constant;
1128 expressionP->X_unsigned = 1;
956a6ba3 1129 expressionP->X_extrabit = 0;
b2221023
MM
1130 }
1131 }
252b5132
RH
1132 else if (expressionP->X_op != O_illegal
1133 && expressionP->X_op != O_absent)
1134 {
6f5ee7a3 1135 if (op != O_absent)
43c3ab55
JB
1136 {
1137 expressionP->X_add_symbol = make_expr_symbol (expressionP);
6f5ee7a3 1138 expressionP->X_op = op;
43c3ab55
JB
1139 expressionP->X_add_number = 0;
1140 }
529b6c24
JB
1141 else if (!md_register_arithmetic && expressionP->X_op == O_register)
1142 {
1143 /* Convert to binary '+'. */
1144 expressionP->X_op_symbol = make_expr_symbol (expressionP);
1145 expressionP->X_add_symbol = make_expr_symbol (&zero);
1146 expressionP->X_add_number = 0;
1147 expressionP->X_op = O_add;
1148 }
252b5132
RH
1149 }
1150 else
1151 as_warn (_("Unary operator %c ignored because bad operand follows"),
1152 c);
1153 }
1154 break;
1155
e2d15955
JD
1156#if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1157 case '$':
1158 if (literal_prefix_dollar_hex)
1159 {
1160 /* $L is the start of a local label, not a hex constant. */
1161 if (* input_line_pointer == 'L')
1162 goto isname;
1163 integer_constant (16, expressionP);
1164 }
1165 else
1166 {
1167 goto isname;
1168 }
1169 break;
1170#else
252b5132 1171 case '$':
929b12bc 1172 /* '$' is the program counter when in MRI mode, or when
02000999 1173 DOLLAR_DOT is defined. */
252b5132
RH
1174#ifndef DOLLAR_DOT
1175 if (! flag_m68k_mri)
1176 goto de_fault;
1177#endif
4fa6945e 1178 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
252b5132 1179 {
4fa6945e
NC
1180 /* In MRI mode and on Z80, '$' is also used as the prefix
1181 for a hexadecimal constant. */
252b5132
RH
1182 integer_constant (16, expressionP);
1183 break;
1184 }
1185
1186 if (is_part_of_name (*input_line_pointer))
1187 goto isname;
1188
1189 current_location (expressionP);
1190 break;
abd63a32 1191#endif
252b5132
RH
1192
1193 case '.':
1194 if (!is_part_of_name (*input_line_pointer))
1195 {
1196 current_location (expressionP);
1197 break;
1198 }
1199 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1200 && ! is_part_of_name (input_line_pointer[8]))
1201 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1202 && ! is_part_of_name (input_line_pointer[7])))
1203 {
1204 int start;
1205
1206 start = (input_line_pointer[1] == 't'
1207 || input_line_pointer[1] == 'T');
1208 input_line_pointer += start ? 8 : 7;
1209 SKIP_WHITESPACE ();
b0c53498
JB
1210
1211 /* Cover for the as_bad () invocations below. */
1212 expressionP->X_op = O_absent;
1213
252b5132
RH
1214 if (*input_line_pointer != '(')
1215 as_bad (_("syntax error in .startof. or .sizeof."));
1216 else
1217 {
252b5132
RH
1218 ++input_line_pointer;
1219 SKIP_WHITESPACE ();
d02603dc 1220 c = get_symbol_name (& name);
b0c53498
JB
1221 if (! *name)
1222 {
1223 as_bad (_("expected symbol name"));
1224 (void) restore_line_pointer (c);
0d1064fa 1225 if (c == ')')
b0c53498
JB
1226 ++input_line_pointer;
1227 break;
1228 }
252b5132 1229
252b5132 1230 expressionP->X_op = O_symbol;
162c6aef 1231 expressionP->X_add_symbol = symbol_lookup_or_make (name, start);
252b5132
RH
1232 expressionP->X_add_number = 0;
1233
1234 *input_line_pointer = c;
d02603dc 1235 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
1236 if (*input_line_pointer != ')')
1237 as_bad (_("syntax error in .startof. or .sizeof."));
1238 else
1239 ++input_line_pointer;
1240 }
1241 break;
1242 }
1243 else
1244 {
1245 goto isname;
1246 }
b75c0c92 1247
252b5132 1248 case ',':
252b5132 1249 eol:
929b12bc 1250 /* Can't imagine any other kind of operand. */
252b5132
RH
1251 expressionP->X_op = O_absent;
1252 input_line_pointer--;
1253 break;
1254
abd63a32 1255#ifdef TC_M68K
252b5132
RH
1256 case '%':
1257 if (! flag_m68k_mri)
1258 goto de_fault;
1259 integer_constant (2, expressionP);
1260 break;
1261
1262 case '@':
1263 if (! flag_m68k_mri)
1264 goto de_fault;
1265 integer_constant (8, expressionP);
1266 break;
1267
1268 case ':':
1269 if (! flag_m68k_mri)
1270 goto de_fault;
1271
1272 /* In MRI mode, this is a floating point constant represented
02000999 1273 using hexadecimal digits. */
252b5132
RH
1274
1275 ++input_line_pointer;
1276 integer_constant (16, expressionP);
1277 break;
1278
1279 case '*':
1280 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1281 goto de_fault;
1282
1283 current_location (expressionP);
1284 break;
abd63a32 1285#endif
252b5132
RH
1286
1287 default:
fcaed75e 1288#if defined(md_need_index_operator) || defined(TC_M68K)
252b5132 1289 de_fault:
abd63a32 1290#endif
d02603dc 1291 if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
252b5132 1292 {
929b12bc
KH
1293 /* Identifier begins here.
1294 This is kludged for speed, so code is repeated. */
252b5132 1295 isname:
d02603dc
NC
1296 -- input_line_pointer;
1297 c = get_symbol_name (&name);
252b5132 1298
fcaed75e
JB
1299#ifdef md_operator
1300 {
6f5ee7a3 1301 op = md_operator (name, 1, &c);
1e9cc1c2 1302 switch (op)
fcaed75e
JB
1303 {
1304 case O_uminus:
d02603dc 1305 restore_line_pointer (c);
fcaed75e
JB
1306 c = '-';
1307 goto unary;
1308 case O_bit_not:
d02603dc 1309 restore_line_pointer (c);
fcaed75e
JB
1310 c = '~';
1311 goto unary;
1312 case O_logical_not:
d02603dc 1313 restore_line_pointer (c);
fcaed75e
JB
1314 c = '!';
1315 goto unary;
1316 case O_illegal:
1317 as_bad (_("invalid use of operator \"%s\""), name);
1318 break;
1319 default:
1320 break;
1321 }
d02603dc 1322
1e9cc1c2 1323 if (op != O_absent && op != O_illegal)
fcaed75e 1324 {
d02603dc 1325 restore_line_pointer (c);
fcaed75e
JB
1326 expr (9, expressionP, mode);
1327 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1328 expressionP->X_op_symbol = NULL;
1329 expressionP->X_add_number = 0;
1e9cc1c2 1330 expressionP->X_op = op;
fcaed75e
JB
1331 break;
1332 }
1333 }
1334#endif
1335
252b5132
RH
1336#ifdef md_parse_name
1337 /* This is a hook for the backend to parse certain names
02000999
AM
1338 specially in certain contexts. If a name always has a
1339 specific value, it can often be handled by simply
1340 entering it in the symbol table. */
9497f5ac 1341 if (md_parse_name (name, expressionP, mode, &c))
252b5132 1342 {
d02603dc 1343 restore_line_pointer (c);
252b5132
RH
1344 break;
1345 }
1346#endif
1347
252b5132
RH
1348 symbolP = symbol_find_or_make (name);
1349
1350 /* If we have an absolute symbol or a reg, then we know its
1351 value now. */
1352 segment = S_GET_SEGMENT (symbolP);
c969da64
RS
1353 if (mode != expr_defer
1354 && segment == absolute_section
1355 && !S_FORCE_RELOC (symbolP, 0))
252b5132
RH
1356 {
1357 expressionP->X_op = O_constant;
1358 expressionP->X_add_number = S_GET_VALUE (symbolP);
1359 }
9497f5ac 1360 else if (mode != expr_defer && segment == reg_section)
252b5132
RH
1361 {
1362 expressionP->X_op = O_register;
1363 expressionP->X_add_number = S_GET_VALUE (symbolP);
1364 }
1365 else
1366 {
1367 expressionP->X_op = O_symbol;
1368 expressionP->X_add_symbol = symbolP;
1369 expressionP->X_add_number = 0;
1370 }
d02603dc
NC
1371
1372 restore_line_pointer (c);
252b5132
RH
1373 }
1374 else
1375 {
1376 /* Let the target try to parse it. Success is indicated by changing
1377 the X_op field to something other than O_absent and pointing
927781e2 1378 input_line_pointer past the expression. If it can't parse the
252b5132
RH
1379 expression, X_op and input_line_pointer should be unchanged. */
1380 expressionP->X_op = O_absent;
1381 --input_line_pointer;
1382 md_operand (expressionP);
1383 if (expressionP->X_op == O_absent)
1384 {
1385 ++input_line_pointer;
0e389e77 1386 as_bad (_("bad expression"));
252b5132
RH
1387 expressionP->X_op = O_constant;
1388 expressionP->X_add_number = 0;
1389 }
1390 }
1391 break;
1392 }
1393
929b12bc
KH
1394 /* It is more 'efficient' to clean up the expressionS when they are
1395 created. Doing it here saves lines of code. */
252b5132 1396 clean_up_expression (expressionP);
14c1428b 1397 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
252b5132
RH
1398 know (*input_line_pointer != ' ');
1399
1400 /* The PA port needs this information. */
1401 if (expressionP->X_add_symbol)
49309057 1402 symbol_mark_used (expressionP->X_add_symbol);
252b5132 1403
3df4e177
MR
1404 if (mode != expr_defer)
1405 {
1406 expressionP->X_add_symbol
1407 = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1408 expressionP->X_op_symbol
1409 = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1410 }
9497f5ac 1411
252b5132
RH
1412 switch (expressionP->X_op)
1413 {
1414 default:
1415 return absolute_section;
1416 case O_symbol:
1417 return S_GET_SEGMENT (expressionP->X_add_symbol);
1418 case O_register:
1419 return reg_section;
1420 }
929b12bc 1421}
252b5132 1422\f
929b12bc
KH
1423/* Internal. Simplify a struct expression for use by expr (). */
1424
3b37fd66 1425/* In: address of an expressionS.
929b12bc
KH
1426 The X_op field of the expressionS may only take certain values.
1427 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1428
1429 Out: expressionS may have been modified:
929b12bc 1430 Unused fields zeroed to help expr (). */
252b5132
RH
1431
1432static void
dd625418 1433clean_up_expression (expressionS *expressionP)
252b5132
RH
1434{
1435 switch (expressionP->X_op)
1436 {
1437 case O_illegal:
1438 case O_absent:
1439 expressionP->X_add_number = 0;
1440 /* Fall through. */
1441 case O_big:
1442 case O_constant:
1443 case O_register:
1444 expressionP->X_add_symbol = NULL;
1445 /* Fall through. */
1446 case O_symbol:
1447 case O_uminus:
1448 case O_bit_not:
1449 expressionP->X_op_symbol = NULL;
1450 break;
252b5132
RH
1451 default:
1452 break;
1453 }
1454}
1455\f
929b12bc
KH
1456/* Expression parser. */
1457
1458/* We allow an empty expression, and just assume (absolute,0) silently.
1459 Unary operators and parenthetical expressions are treated as operands.
1460 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1461
3b37fd66 1462 We used to do an aho/ullman shift-reduce parser, but the logic got so
929b12bc
KH
1463 warped that I flushed it and wrote a recursive-descent parser instead.
1464 Now things are stable, would anybody like to write a fast parser?
1465 Most expressions are either register (which does not even reach here)
1466 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1467 So I guess it doesn't really matter how inefficient more complex expressions
1468 are parsed.
1469
1470 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1471 Also, we have consumed any leading or trailing spaces (operand does that)
1472 and done all intervening operators.
1473
1474 This returns the segment of the result, which will be
1475 absolute_section or the segment of a symbol. */
252b5132
RH
1476
1477#undef __
1478#define __ O_illegal
fb7ccfc1
AM
1479#ifndef O_SINGLE_EQ
1480#define O_SINGLE_EQ O_illegal
1481#endif
252b5132 1482
b041f888
KH
1483/* Maps ASCII -> operators. */
1484static const operatorT op_encoding[256] = {
252b5132
RH
1485 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1486 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1487
1488 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1489 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1490 __, __, __, __, __, __, __, __,
fb7ccfc1 1491 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
252b5132
RH
1492 __, __, __, __, __, __, __, __,
1493 __, __, __, __, __, __, __, __,
1494 __, __, __, __, __, __, __, __,
b585bc2c
RH
1495 __, __, __,
1496#ifdef NEED_INDEX_OPERATOR
1497 O_index,
1498#else
1499 __,
1500#endif
1501 __, __, O_bit_exclusive_or, __,
252b5132
RH
1502 __, __, __, __, __, __, __, __,
1503 __, __, __, __, __, __, __, __,
1504 __, __, __, __, __, __, __, __,
1505 __, __, __, __, O_bit_inclusive_or, __, __, __,
1506
1507 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1508 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1509 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1510 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1511 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1512 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1513 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1514 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1515};
1516
929b12bc
KH
1517/* Rank Examples
1518 0 operand, (expression)
1519 1 ||
1520 2 &&
07a7a145 1521 3 == <> < <= >= >
929b12bc
KH
1522 4 + -
1523 5 used for * / % in MRI mode
1524 6 & ^ ! |
1525 7 * / % << >>
1526 8 unary - unary ~
1527*/
1035ad42 1528static operator_rankT op_rank[O_max] = {
252b5132
RH
1529 0, /* O_illegal */
1530 0, /* O_absent */
1531 0, /* O_constant */
1532 0, /* O_symbol */
1533 0, /* O_symbol_rva */
145667f8 1534 0, /* O_secidx */
252b5132 1535 0, /* O_register */
16887944 1536 0, /* O_big */
b585bc2c
RH
1537 9, /* O_uminus */
1538 9, /* O_bit_not */
1539 9, /* O_logical_not */
1540 8, /* O_multiply */
1541 8, /* O_divide */
1542 8, /* O_modulus */
1543 8, /* O_left_shift */
1544 8, /* O_right_shift */
1545 7, /* O_bit_inclusive_or */
1546 7, /* O_bit_or_not */
1547 7, /* O_bit_exclusive_or */
1548 7, /* O_bit_and */
1549 5, /* O_add */
1550 5, /* O_subtract */
1551 4, /* O_eq */
1552 4, /* O_ne */
1553 4, /* O_lt */
1554 4, /* O_le */
1555 4, /* O_ge */
1556 4, /* O_gt */
1557 3, /* O_logical_and */
1558 2, /* O_logical_or */
1559 1, /* O_index */
252b5132
RH
1560};
1561
1562/* Unfortunately, in MRI mode for the m68k, multiplication and
1563 division have lower precedence than the bit wise operators. This
1564 function sets the operator precedences correctly for the current
1565 mode. Also, MRI uses a different bit_not operator, and this fixes
1566 that as well. */
1567
16887944
AM
1568#define STANDARD_MUL_PRECEDENCE 8
1569#define MRI_MUL_PRECEDENCE 6
252b5132
RH
1570
1571void
dd625418 1572expr_set_precedence (void)
252b5132
RH
1573{
1574 if (flag_m68k_mri)
1575 {
1576 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1577 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1578 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1579 }
1580 else
1581 {
1582 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1583 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1584 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1585 }
1586}
1587
fcaed75e 1588void
1e9cc1c2 1589expr_set_rank (operatorT op, operator_rankT rank)
fcaed75e 1590{
1e9cc1c2
NC
1591 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1592 op_rank[op] = rank;
fcaed75e
JB
1593}
1594
252b5132
RH
1595/* Initialize the expression parser. */
1596
1597void
dd625418 1598expr_begin (void)
252b5132
RH
1599{
1600 expr_set_precedence ();
1601
1602 /* Verify that X_op field is wide enough. */
1603 {
1604 expressionS e;
1605 e.X_op = O_max;
9c2799c2 1606 gas_assert (e.X_op == O_max);
252b5132 1607 }
6eb099ae
AM
1608
1609 memset (seen, 0, sizeof seen);
1610 memset (nr_seen, 0, sizeof nr_seen);
1611 expr_symbol_lines = NULL;
1612}
1613
1614void
1615expr_end (void)
1616{
1617 for (size_t i = 0; i < ARRAY_SIZE (seen); i++)
1618 free (seen[i]);
252b5132
RH
1619}
1620\f
6fad6acb
AM
1621/* Return the encoding for the operator at INPUT_LINE_POINTER, and
1622 sets NUM_CHARS to the number of characters in the operator.
1623 Does not advance INPUT_LINE_POINTER. */
252b5132
RH
1624
1625static inline operatorT
1e9cc1c2 1626operatorf (int *num_chars)
252b5132
RH
1627{
1628 int c;
1629 operatorT ret;
1630
1631 c = *input_line_pointer & 0xff;
6fad6acb 1632 *num_chars = 1;
252b5132 1633
b75c0c92
AM
1634 if (is_end_of_line[c])
1635 return O_illegal;
1636
fcaed75e
JB
1637#ifdef md_operator
1638 if (is_name_beginner (c))
1639 {
d02603dc
NC
1640 char *name;
1641 char ec = get_symbol_name (& name);
fcaed75e 1642
91d6fa6a 1643 ret = md_operator (name, 2, &ec);
fcaed75e
JB
1644 switch (ret)
1645 {
1646 case O_absent:
91d6fa6a 1647 *input_line_pointer = ec;
fcaed75e
JB
1648 input_line_pointer = name;
1649 break;
1650 case O_uminus:
1651 case O_bit_not:
1652 case O_logical_not:
1653 as_bad (_("invalid use of operator \"%s\""), name);
1654 ret = O_illegal;
1655 /* FALLTHROUGH */
1656 default:
91d6fa6a 1657 *input_line_pointer = ec;
fcaed75e
JB
1658 *num_chars = input_line_pointer - name;
1659 input_line_pointer = name;
1660 return ret;
1661 }
1662 }
1663#endif
1664
252b5132
RH
1665 switch (c)
1666 {
1667 default:
fcaed75e
JB
1668 ret = op_encoding[c];
1669#ifdef md_operator
1670 if (ret == O_illegal)
1671 {
1672 char *start = input_line_pointer;
1673
1674 ret = md_operator (NULL, 2, NULL);
1675 if (ret != O_illegal)
1676 *num_chars = input_line_pointer - start;
1677 input_line_pointer = start;
1678 }
1679#endif
1680 return ret;
252b5132 1681
3e4caed2
NS
1682 case '+':
1683 case '-':
8c9f705e 1684 return op_encoding[c];
3e4caed2 1685
252b5132
RH
1686 case '<':
1687 switch (input_line_pointer[1])
1688 {
1689 default:
1690 return op_encoding[c];
1691 case '<':
1692 ret = O_left_shift;
1693 break;
1694 case '>':
1695 ret = O_ne;
1696 break;
1697 case '=':
1698 ret = O_le;
1699 break;
1700 }
6fad6acb 1701 *num_chars = 2;
252b5132
RH
1702 return ret;
1703
1704 case '=':
1705 if (input_line_pointer[1] != '=')
1706 return op_encoding[c];
1707
6fad6acb 1708 *num_chars = 2;
252b5132
RH
1709 return O_eq;
1710
1711 case '>':
1712 switch (input_line_pointer[1])
1713 {
1714 default:
1715 return op_encoding[c];
1716 case '>':
1717 ret = O_right_shift;
1718 break;
1719 case '=':
1720 ret = O_ge;
1721 break;
1722 }
6fad6acb 1723 *num_chars = 2;
252b5132
RH
1724 return ret;
1725
1726 case '!':
723a8472 1727 switch (input_line_pointer[1])
252b5132 1728 {
723a8472
NC
1729 case '!':
1730 /* We accept !! as equivalent to ^ for MRI compatibility. */
1731 *num_chars = 2;
1732 return O_bit_exclusive_or;
1733 case '=':
1734 /* We accept != as equivalent to <>. */
1735 *num_chars = 2;
1736 return O_ne;
1737 default:
252b5132
RH
1738 if (flag_m68k_mri)
1739 return O_bit_inclusive_or;
1740 return op_encoding[c];
1741 }
252b5132
RH
1742
1743 case '|':
1744 if (input_line_pointer[1] != '|')
1745 return op_encoding[c];
1746
6fad6acb 1747 *num_chars = 2;
252b5132
RH
1748 return O_logical_or;
1749
1750 case '&':
1751 if (input_line_pointer[1] != '&')
1752 return op_encoding[c];
1753
6fad6acb 1754 *num_chars = 2;
252b5132
RH
1755 return O_logical_and;
1756 }
1757
929b12bc 1758 /* NOTREACHED */
252b5132
RH
1759}
1760
956a6ba3
JB
1761/* Implement "word-size + 1 bit" addition for
1762 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1763 is used so that the full range of unsigned word values and the full range of
1764 signed word values can be represented in an O_constant expression, which is
1765 useful e.g. for .sleb128 directives. */
1766
4455e9ad 1767void
956a6ba3
JB
1768add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1769{
1770 valueT ures = resultP->X_add_number;
1771 valueT uamount = amount;
1772
c77a6ef6 1773 resultP->X_add_number += uamount;
956a6ba3
JB
1774
1775 resultP->X_extrabit ^= rhs_highbit;
1776
1777 if (ures + uamount < ures)
1778 resultP->X_extrabit ^= 1;
1779}
1780
1781/* Similarly, for subtraction. */
1782
4455e9ad 1783void
956a6ba3
JB
1784subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1785{
1786 valueT ures = resultP->X_add_number;
1787 valueT uamount = amount;
1788
c77a6ef6 1789 resultP->X_add_number -= uamount;
956a6ba3
JB
1790
1791 resultP->X_extrabit ^= rhs_highbit;
1792
1793 if (ures < uamount)
1794 resultP->X_extrabit ^= 1;
1795}
1796
252b5132
RH
1797/* Parse an expression. */
1798
1799segT
dd625418 1800expr (int rankarg, /* Larger # is higher rank. */
9497f5ac
NC
1801 expressionS *resultP, /* Deliver result here. */
1802 enum expr_mode mode /* Controls behavior. */)
252b5132 1803{
0561a208 1804 operator_rankT rank = (operator_rankT) rankarg;
252b5132
RH
1805 segT retval;
1806 expressionS right;
1807 operatorT op_left;
1808 operatorT op_right;
6fad6acb 1809 int op_chars;
252b5132 1810
db557034 1811 know (rankarg >= 0);
252b5132 1812
26346241
AM
1813 /* Save the value of dot for the fixup code. */
1814 if (rank == 0)
8e723a10
CLT
1815 {
1816 dot_value = frag_now_fix ();
1817 dot_frag = frag_now;
1818 }
26346241 1819
9497f5ac 1820 retval = operand (resultP, mode);
252b5132 1821
929b12bc
KH
1822 /* operand () gobbles spaces. */
1823 know (*input_line_pointer != ' ');
252b5132 1824
1e9cc1c2 1825 op_left = operatorf (&op_chars);
252b5132
RH
1826 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1827 {
1828 segT rightseg;
762acf21 1829 bool is_unsigned;
08ea7020 1830 offsetT frag_off;
252b5132 1831
6fad6acb 1832 input_line_pointer += op_chars; /* -> after operator. */
252b5132 1833
34a7d6c6 1834 right.X_md = 0;
9497f5ac 1835 rightseg = expr (op_rank[(int) op_left], &right, mode);
252b5132
RH
1836 if (right.X_op == O_absent)
1837 {
1838 as_warn (_("missing operand; zero assumed"));
1839 right.X_op = O_constant;
1840 right.X_add_number = 0;
1841 right.X_add_symbol = NULL;
1842 right.X_op_symbol = NULL;
1843 }
1844
1845 know (*input_line_pointer != ' ');
1846
b585bc2c
RH
1847 if (op_left == O_index)
1848 {
1849 if (*input_line_pointer != ']')
1850 as_bad ("missing right bracket");
1851 else
1852 {
1853 ++input_line_pointer;
1854 SKIP_WHITESPACE ();
1855 }
1856 }
1857
1e9cc1c2 1858 op_right = operatorf (&op_chars);
252b5132 1859
fcaed75e 1860 know (op_right == O_illegal || op_left == O_index
929b12bc 1861 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
fcaed75e
JB
1862 know ((int) op_left >= (int) O_multiply);
1863#ifndef md_operator
1864 know ((int) op_left <= (int) O_index);
1865#else
1866 know ((int) op_left < (int) O_max);
1867#endif
252b5132 1868
929b12bc
KH
1869 /* input_line_pointer->after right-hand quantity. */
1870 /* left-hand quantity in resultP. */
1871 /* right-hand quantity in right. */
1872 /* operator in op_left. */
252b5132
RH
1873
1874 if (resultP->X_op == O_big)
1875 {
1876 if (resultP->X_add_number > 0)
1877 as_warn (_("left operand is a bignum; integer 0 assumed"));
1878 else
1879 as_warn (_("left operand is a float; integer 0 assumed"));
1880 resultP->X_op = O_constant;
1881 resultP->X_add_number = 0;
1882 resultP->X_add_symbol = NULL;
1883 resultP->X_op_symbol = NULL;
1884 }
1885 if (right.X_op == O_big)
1886 {
1887 if (right.X_add_number > 0)
1888 as_warn (_("right operand is a bignum; integer 0 assumed"));
1889 else
1890 as_warn (_("right operand is a float; integer 0 assumed"));
1891 right.X_op = O_constant;
1892 right.X_add_number = 0;
1893 right.X_add_symbol = NULL;
1894 right.X_op_symbol = NULL;
1895 }
1896
762acf21
JB
1897 is_unsigned = resultP->X_unsigned && right.X_unsigned;
1898
e4c2619a
AM
1899 if (mode == expr_defer
1900 && ((resultP->X_add_symbol != NULL
1901 && S_IS_FORWARD_REF (resultP->X_add_symbol))
1902 || (right.X_add_symbol != NULL
1903 && S_IS_FORWARD_REF (right.X_add_symbol))))
1904 goto general;
1905
252b5132 1906 /* Optimize common cases. */
800eeca4
JW
1907#ifdef md_optimize_expr
1908 if (md_optimize_expr (resultP, op_left, &right))
1909 {
929b12bc 1910 /* Skip. */
762acf21 1911 is_unsigned = resultP->X_unsigned;
800eeca4
JW
1912 }
1913 else
5a918ce7
JB
1914#endif
1915 if (op_left == O_add && right.X_op == O_constant
1916 && (md_register_arithmetic || resultP->X_op != O_register))
252b5132
RH
1917 {
1918 /* X + constant. */
956a6ba3 1919 add_to_result (resultP, right.X_add_number, right.X_extrabit);
252b5132
RH
1920 }
1921 /* This case comes up in PIC code. */
1922 else if (op_left == O_subtract
1923 && right.X_op == O_symbol
1924 && resultP->X_op == O_symbol
99630778 1925 && retval == rightseg
bfff1642
NC
1926#ifdef md_allow_local_subtract
1927 && md_allow_local_subtract (resultP, & right, rightseg)
1928#endif
c969da64
RS
1929 && ((SEG_NORMAL (rightseg)
1930 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1931 && !S_FORCE_RELOC (right.X_add_symbol, 0))
99630778
AM
1932 || right.X_add_symbol == resultP->X_add_symbol)
1933 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1934 symbol_get_frag (right.X_add_symbol),
1935 &frag_off))
252b5132 1936 {
956a6ba3
JB
1937 offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1938 - S_GET_VALUE (right.X_add_symbol);
1939 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1940 subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1941 add_to_result (resultP, symval_diff, symval_diff < 0);
252b5132
RH
1942 resultP->X_op = O_constant;
1943 resultP->X_add_symbol = 0;
762acf21 1944 is_unsigned = false;
252b5132 1945 }
5a918ce7
JB
1946 else if (op_left == O_subtract && right.X_op == O_constant
1947 && (md_register_arithmetic || resultP->X_op != O_register))
252b5132
RH
1948 {
1949 /* X - constant. */
956a6ba3 1950 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
762acf21 1951 is_unsigned = false;
252b5132 1952 }
5a918ce7
JB
1953 else if (op_left == O_add && resultP->X_op == O_constant
1954 && (md_register_arithmetic || right.X_op != O_register))
252b5132
RH
1955 {
1956 /* Constant + X. */
1957 resultP->X_op = right.X_op;
1958 resultP->X_add_symbol = right.X_add_symbol;
1959 resultP->X_op_symbol = right.X_op_symbol;
956a6ba3 1960 add_to_result (resultP, right.X_add_number, right.X_extrabit);
252b5132
RH
1961 retval = rightseg;
1962 }
1963 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1964 {
1965 /* Constant OP constant. */
1966 offsetT v = right.X_add_number;
1967 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1968 {
1969 as_warn (_("division by zero"));
1970 v = 1;
1971 }
1972 switch (op_left)
1973 {
fcaed75e 1974 default: goto general;
487b0ff0
AM
1975 case O_multiply:
1976 /* Do the multiply as unsigned to silence ubsan. The
1977 result is of course the same when we throw away high
1978 bits of the result. */
1979 resultP->X_add_number *= (valueT) v;
1980 break;
252b5132
RH
1981 case O_divide: resultP->X_add_number /= v; break;
1982 case O_modulus: resultP->X_add_number %= v; break;
b2f386b9 1983 case O_left_shift:
2b6132c3 1984 case O_right_shift:
b2f386b9
AM
1985 /* We always use unsigned shifts. According to the ISO
1986 C standard, left shift of a signed type having a
1987 negative value is undefined behaviour, and right
1988 shift of a signed type having negative value is
1989 implementation defined. Left shift of a signed type
1990 when the result overflows is also undefined
1991 behaviour. So don't trigger ubsan warnings or rely
1992 on characteristics of the compiler. */
2b6132c3
JB
1993 if ((valueT) v >= sizeof (valueT) * CHAR_BIT)
1994 {
1995 as_warn_value_out_of_range (_("shift count"), v, 0,
1996 sizeof (valueT) * CHAR_BIT - 1,
1997 NULL, 0);
1998 resultP->X_add_number = 0;
1999 }
2000 else if (op_left == O_left_shift)
2001 resultP->X_add_number
2002 = (valueT) resultP->X_add_number << (valueT) v;
2003 else
2004 resultP->X_add_number
2005 = (valueT) resultP->X_add_number >> (valueT) v;
762acf21 2006 is_unsigned = resultP->X_unsigned;
252b5132
RH
2007 break;
2008 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
2009 case O_bit_or_not: resultP->X_add_number |= ~v; break;
2010 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
2011 case O_bit_and: resultP->X_add_number &= v; break;
7b383517
BE
2012 /* Constant + constant (O_add) is handled by the
2013 previous if statement for constant + X, so is omitted
2014 here. */
956a6ba3
JB
2015 case O_subtract:
2016 subtract_from_result (resultP, v, 0);
762acf21 2017 is_unsigned = false;
956a6ba3 2018 break;
252b5132
RH
2019 case O_eq:
2020 resultP->X_add_number =
958b5f01 2021 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
762acf21 2022 is_unsigned = false;
252b5132
RH
2023 break;
2024 case O_ne:
2025 resultP->X_add_number =
958b5f01 2026 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
762acf21 2027 is_unsigned = false;
252b5132
RH
2028 break;
2029 case O_lt:
2030 resultP->X_add_number =
958b5f01 2031 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
762acf21 2032 is_unsigned = false;
252b5132
RH
2033 break;
2034 case O_le:
2035 resultP->X_add_number =
958b5f01 2036 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
762acf21 2037 is_unsigned = false;
252b5132
RH
2038 break;
2039 case O_ge:
2040 resultP->X_add_number =
958b5f01 2041 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
762acf21 2042 is_unsigned = false;
252b5132
RH
2043 break;
2044 case O_gt:
2045 resultP->X_add_number =
958b5f01 2046 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
762acf21 2047 is_unsigned = false;
252b5132
RH
2048 break;
2049 case O_logical_and:
2050 resultP->X_add_number = resultP->X_add_number && v;
762acf21 2051 is_unsigned = true;
252b5132
RH
2052 break;
2053 case O_logical_or:
2054 resultP->X_add_number = resultP->X_add_number || v;
762acf21 2055 is_unsigned = true;
252b5132
RH
2056 break;
2057 }
2058 }
2059 else if (resultP->X_op == O_symbol
2060 && right.X_op == O_symbol
2061 && (op_left == O_add
2062 || op_left == O_subtract
2063 || (resultP->X_add_number == 0
2064 && right.X_add_number == 0)))
2065 {
2066 /* Symbol OP symbol. */
2067 resultP->X_op = op_left;
2068 resultP->X_op_symbol = right.X_add_symbol;
2069 if (op_left == O_add)
956a6ba3 2070 add_to_result (resultP, right.X_add_number, right.X_extrabit);
252b5132 2071 else if (op_left == O_subtract)
e0890092 2072 {
956a6ba3
JB
2073 subtract_from_result (resultP, right.X_add_number,
2074 right.X_extrabit);
c969da64
RS
2075 if (retval == rightseg
2076 && SEG_NORMAL (retval)
2077 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2078 && !S_FORCE_RELOC (right.X_add_symbol, 0))
e0890092
AM
2079 {
2080 retval = absolute_section;
2081 rightseg = absolute_section;
2082 }
2083 }
252b5132
RH
2084 }
2085 else
2086 {
fcaed75e 2087 general:
252b5132
RH
2088 /* The general case. */
2089 resultP->X_add_symbol = make_expr_symbol (resultP);
2090 resultP->X_op_symbol = make_expr_symbol (&right);
2091 resultP->X_op = op_left;
2092 resultP->X_add_number = 0;
956a6ba3 2093 resultP->X_extrabit = 0;
252b5132
RH
2094 }
2095
762acf21
JB
2096 resultP->X_unsigned = is_unsigned;
2097
e0890092
AM
2098 if (retval != rightseg)
2099 {
1e0f6894
AM
2100 if (retval == undefined_section)
2101 ;
2102 else if (rightseg == undefined_section)
2103 retval = rightseg;
2104 else if (retval == expr_section)
2105 ;
2106 else if (rightseg == expr_section)
2107 retval = rightseg;
2108 else if (retval == reg_section)
2109 ;
2110 else if (rightseg == reg_section)
2111 retval = rightseg;
2112 else if (rightseg == absolute_section)
2113 ;
2114 else if (retval == absolute_section)
2115 retval = rightseg;
e0890092 2116#ifdef DIFF_EXPR_OK
1e0f6894
AM
2117 else if (op_left == O_subtract)
2118 ;
e0890092 2119#endif
1e0f6894 2120 else
e0890092
AM
2121 as_bad (_("operation combines symbols in different segments"));
2122 }
2123
252b5132 2124 op_left = op_right;
929b12bc 2125 } /* While next operator is >= this rank. */
252b5132
RH
2126
2127 /* The PA port needs this information. */
2128 if (resultP->X_add_symbol)
49309057 2129 symbol_mark_used (resultP->X_add_symbol);
252b5132 2130
9497f5ac
NC
2131 if (rank == 0 && mode == expr_evaluate)
2132 resolve_expression (resultP);
2133
252b5132
RH
2134 return resultP->X_op == O_constant ? absolute_section : retval;
2135}
9497f5ac
NC
2136
2137/* Resolve an expression without changing any symbols/sub-expressions
2138 used. */
2139
2140int
2141resolve_expression (expressionS *expressionP)
2142{
2143 /* Help out with CSE. */
2144 valueT final_val = expressionP->X_add_number;
2145 symbolS *add_symbol = expressionP->X_add_symbol;
14610ad1 2146 symbolS *orig_add_symbol = add_symbol;
9497f5ac
NC
2147 symbolS *op_symbol = expressionP->X_op_symbol;
2148 operatorT op = expressionP->X_op;
2149 valueT left, right;
2150 segT seg_left, seg_right;
2151 fragS *frag_left, *frag_right;
08ea7020 2152 offsetT frag_off;
9497f5ac
NC
2153
2154 switch (op)
2155 {
2156 default:
2157 return 0;
2158
2159 case O_constant:
2160 case O_register:
2161 left = 0;
2162 break;
2163
2164 case O_symbol:
2165 case O_symbol_rva:
2e1e12b1 2166 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
9497f5ac
NC
2167 return 0;
2168
2169 break;
2170
2171 case O_uminus:
2172 case O_bit_not:
2173 case O_logical_not:
2e1e12b1 2174 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
9497f5ac
NC
2175 return 0;
2176
2177 if (seg_left != absolute_section)
2178 return 0;
2179
2180 if (op == O_logical_not)
2181 left = !left;
2182 else if (op == O_uminus)
2183 left = -left;
2184 else
2185 left = ~left;
2186 op = O_constant;
2187 break;
2188
2189 case O_multiply:
2190 case O_divide:
2191 case O_modulus:
2192 case O_left_shift:
2193 case O_right_shift:
2194 case O_bit_inclusive_or:
2195 case O_bit_or_not:
2196 case O_bit_exclusive_or:
2197 case O_bit_and:
2198 case O_add:
2199 case O_subtract:
2200 case O_eq:
2201 case O_ne:
2202 case O_lt:
2203 case O_le:
2204 case O_ge:
2205 case O_gt:
2206 case O_logical_and:
2207 case O_logical_or:
2e1e12b1
JB
2208 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2209 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
9497f5ac
NC
2210 return 0;
2211
2212 /* Simplify addition or subtraction of a constant by folding the
2213 constant into X_add_number. */
2214 if (op == O_add)
2215 {
2216 if (seg_right == absolute_section)
2217 {
2218 final_val += right;
2219 op = O_symbol;
2220 break;
2221 }
2222 else if (seg_left == absolute_section)
2223 {
2224 final_val += left;
2225 left = right;
2226 seg_left = seg_right;
2e1e12b1 2227 add_symbol = op_symbol;
14610ad1 2228 orig_add_symbol = expressionP->X_op_symbol;
9497f5ac
NC
2229 op = O_symbol;
2230 break;
2231 }
2232 }
2233 else if (op == O_subtract)
2234 {
2235 if (seg_right == absolute_section)
2236 {
2237 final_val -= right;
2238 op = O_symbol;
2239 break;
2240 }
2241 }
2242
2243 /* Equality and non-equality tests are permitted on anything.
2244 Subtraction, and other comparison operators are permitted if
2e1e12b1
JB
2245 both operands are in the same section.
2246 Shifts by constant zero are permitted on anything.
2247 Multiplies, bit-ors, and bit-ands with constant zero are
2248 permitted on anything.
2249 Multiplies and divides by constant one are permitted on
2250 anything.
2251 Binary operations with both operands being the same register
2252 or undefined symbol are permitted if the result doesn't depend
2253 on the input value.
2254 Otherwise, both operands must be absolute. We already handled
2255 the case of addition or subtraction of a constant above. */
99630778 2256 frag_off = 0;
9497f5ac
NC
2257 if (!(seg_left == absolute_section
2258 && seg_right == absolute_section)
2259 && !(op == O_eq || op == O_ne)
2260 && !((op == O_subtract
2261 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2262 && seg_left == seg_right
99630778 2263 && (finalize_syms
38c3873e
AO
2264 || frag_offset_fixed_p (frag_left, frag_right, &frag_off)
2265 || (op == O_gt
2266 && frag_gtoffset_p (left, frag_left,
2267 right, frag_right, &frag_off)))
2e1e12b1
JB
2268 && (seg_left != reg_section || left == right)
2269 && (seg_left != undefined_section || add_symbol == op_symbol)))
2270 {
2271 if ((seg_left == absolute_section && left == 0)
2272 || (seg_right == absolute_section && right == 0))
2273 {
2274 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2275 {
14610ad1 2276 if (!(seg_right == absolute_section && right == 0))
2e1e12b1
JB
2277 {
2278 seg_left = seg_right;
2279 left = right;
2280 add_symbol = op_symbol;
14610ad1 2281 orig_add_symbol = expressionP->X_op_symbol;
2e1e12b1
JB
2282 }
2283 op = O_symbol;
2284 break;
2285 }
2286 else if (op == O_left_shift || op == O_right_shift)
2287 {
14610ad1 2288 if (!(seg_left == absolute_section && left == 0))
2e1e12b1
JB
2289 {
2290 op = O_symbol;
2291 break;
2292 }
2293 }
2294 else if (op != O_multiply
2295 && op != O_bit_or_not && op != O_bit_and)
2296 return 0;
2297 }
2298 else if (op == O_multiply
2299 && seg_left == absolute_section && left == 1)
2300 {
2301 seg_left = seg_right;
2302 left = right;
2303 add_symbol = op_symbol;
14610ad1 2304 orig_add_symbol = expressionP->X_op_symbol;
2e1e12b1
JB
2305 op = O_symbol;
2306 break;
2307 }
2308 else if ((op == O_multiply || op == O_divide)
2309 && seg_right == absolute_section && right == 1)
2310 {
2311 op = O_symbol;
2312 break;
2313 }
14610ad1
AM
2314 else if (!(left == right
2315 && ((seg_left == reg_section && seg_right == reg_section)
2316 || (seg_left == undefined_section
2317 && seg_right == undefined_section
2318 && add_symbol == op_symbol))))
2e1e12b1
JB
2319 return 0;
2320 else if (op == O_bit_and || op == O_bit_inclusive_or)
2321 {
2322 op = O_symbol;
2323 break;
2324 }
2325 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2326 return 0;
2327 }
9497f5ac 2328
99630778 2329 right += frag_off / OCTETS_PER_BYTE;
9497f5ac
NC
2330 switch (op)
2331 {
2332 case O_add: left += right; break;
2333 case O_subtract: left -= right; break;
2334 case O_multiply: left *= right; break;
2335 case O_divide:
2336 if (right == 0)
2337 return 0;
2338 left = (offsetT) left / (offsetT) right;
2339 break;
2340 case O_modulus:
2341 if (right == 0)
2342 return 0;
2343 left = (offsetT) left % (offsetT) right;
2344 break;
44a178cc
AM
2345 case O_left_shift:
2346 if (right >= sizeof (left) * CHAR_BIT)
2347 left = 0;
2348 else
2349 left <<= right;
2350 break;
2351 case O_right_shift:
2352 if (right >= sizeof (left) * CHAR_BIT)
2353 left = 0;
2354 else
2355 left >>= right;
2356 break;
9497f5ac
NC
2357 case O_bit_inclusive_or: left |= right; break;
2358 case O_bit_or_not: left |= ~right; break;
2359 case O_bit_exclusive_or: left ^= right; break;
2360 case O_bit_and: left &= right; break;
2361 case O_eq:
2362 case O_ne:
2363 left = (left == right
2364 && seg_left == seg_right
2365 && (finalize_syms || frag_left == frag_right)
2e1e12b1 2366 && (seg_left != undefined_section
9497f5ac
NC
2367 || add_symbol == op_symbol)
2368 ? ~ (valueT) 0 : 0);
2369 if (op == O_ne)
2370 left = ~left;
2371 break;
2372 case O_lt:
2373 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2374 break;
2375 case O_le:
2376 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2377 break;
2378 case O_ge:
2379 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2380 break;
2381 case O_gt:
2382 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2383 break;
2384 case O_logical_and: left = left && right; break;
2385 case O_logical_or: left = left || right; break;
2386 default: abort ();
2387 }
2388
2389 op = O_constant;
2390 break;
2391 }
2392
2393 if (op == O_symbol)
2394 {
2395 if (seg_left == absolute_section)
2396 op = O_constant;
2397 else if (seg_left == reg_section && final_val == 0)
2398 op = O_register;
087d837e 2399 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2e1e12b1
JB
2400 final_val += left;
2401 expressionP->X_add_symbol = add_symbol;
9497f5ac
NC
2402 }
2403 expressionP->X_op = op;
2404
2405 if (op == O_constant || op == O_register)
2406 final_val += left;
2407 expressionP->X_add_number = final_val;
2408
2409 return 1;
2410}
d50c498a
JB
2411
2412/* "Look through" register equates. */
2413void resolve_register (expressionS *expP)
2414{
2415 symbolS *sym;
2416 offsetT acc = 0;
2417 const expressionS *e = expP;
2418
2419 if (expP->X_op != O_symbol)
2420 return;
2421
2422 do
2423 {
2424 sym = e->X_add_symbol;
2425 acc += e->X_add_number;
2426 e = symbol_get_value_expression (sym);
2427 }
2428 while (symbol_equated_p (sym));
2429
2430 if (e->X_op == O_register)
2431 {
2432 *expP = *e;
2433 expP->X_add_number += acc;
2434 }
2435}
252b5132 2436\f
929b12bc
KH
2437/* This lives here because it belongs equally in expr.c & read.c.
2438 expr.c is just a branch office read.c anyway, and putting it
2439 here lessens the crowd at read.c.
2440
d02603dc 2441 Assume input_line_pointer is at start of symbol name, or the
ac164fa8
JB
2442 start of a double quote enclosed symbol name. Advance
2443 input_line_pointer past symbol name. Turn that character into a '\0',
2444 returning its former value, which may be the closing double quote.
2445
929b12bc 2446 This allows a string compare (RMS wants symbol names to be strings)
ac164fa8
JB
2447 of the symbol name.
2448
2449 NOTE: The input buffer is further altered when adjacent strings are
2450 concatenated by the function. Callers caring about the original buffer
2451 contents will need to make a copy before calling here.
2452
929b12bc
KH
2453 There will always be a char following symbol name, because all good
2454 lines end in end-of-line. */
2455
252b5132 2456char
d02603dc 2457get_symbol_name (char ** ilp_return)
252b5132
RH
2458{
2459 char c;
2460
d02603dc 2461 * ilp_return = input_line_pointer;
2469b3c5 2462 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
252b5132 2463 constructed string. */
2469b3c5
JW
2464 if (is_name_beginner (c = *input_line_pointer++)
2465 || (input_from_string && c == FAKE_LABEL_CHAR))
58b5739a
RH
2466 {
2467 while (is_part_of_name (c = *input_line_pointer++)
2469b3c5 2468 || (input_from_string && c == FAKE_LABEL_CHAR))
58b5739a
RH
2469 ;
2470 if (is_name_ender (c))
2471 c = *input_line_pointer++;
2472 }
d02603dc
NC
2473 else if (c == '"')
2474 {
5ed4d49d 2475 char *dst = input_line_pointer;
d02603dc
NC
2476
2477 * ilp_return = input_line_pointer;
5ed4d49d 2478 for (;;)
d02603dc 2479 {
5ed4d49d
JB
2480 c = *input_line_pointer++;
2481
2482 if (c == 0)
2483 {
2484 as_warn (_("missing closing '\"'"));
2485 break;
2486 }
d02603dc 2487
5ed4d49d
JB
2488 if (c == '"')
2489 {
2490 char *ilp_save = input_line_pointer;
2491
2492 SKIP_WHITESPACE ();
2493 if (*input_line_pointer == '"')
2494 {
2495 ++input_line_pointer;
2496 continue;
2497 }
2498 input_line_pointer = ilp_save;
2499 break;
2500 }
2501
2502 if (c == '\\')
2503 switch (*input_line_pointer)
2504 {
2505 case '"':
2506 case '\\':
2507 c = *input_line_pointer++;
2508 break;
2509
2510 default:
2511 if (c != 0)
2512 as_warn (_("'\\%c' in quoted symbol name; "
2513 "behavior may change in the future"),
2514 *input_line_pointer);
2515 break;
2516 }
2517
2518 *dst++ = c;
2519 }
2520 *dst = 0;
d02603dc 2521 }
252b5132 2522 *--input_line_pointer = 0;
d02603dc
NC
2523 return c;
2524}
2525
2526/* Replace the NUL character pointed to by input_line_pointer
2527 with C. If C is \" then advance past it. Return the character
2528 now pointed to by input_line_pointer. */
2529
2530char
2531restore_line_pointer (char c)
2532{
2533 * input_line_pointer = c;
2534 if (c == '"')
2535 c = * ++ input_line_pointer;
2536 return c;
252b5132
RH
2537}
2538
252b5132 2539unsigned int
dd625418 2540get_single_number (void)
252b5132
RH
2541{
2542 expressionS exp;
9497f5ac 2543 operand (&exp, expr_normal);
252b5132 2544 return exp.X_add_number;
252b5132 2545}