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