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