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