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