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