aarch64: Add the SME2 FMLA and FMLS instructions
[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 /* PR 19910: Look for, and ignore, a U suffix to the number. */
542 if (tc_allow_U_suffix && (c == 'U' || c == 'u'))
543 c = * input_line_pointer++;
544
545 #ifndef tc_allow_L_suffix
546 #define tc_allow_L_suffix 1
547 #endif
548 /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */
549 if (tc_allow_L_suffix)
550 while (c == 'L' || c == 'l')
551 c = * input_line_pointer++;
552
553 if (small)
554 {
555 /* Here with number, in correct radix. c is the next char.
556 Note that unlike un*x, we allow "011f" "0x9f" to both mean
557 the same as the (conventional) "9f".
558 This is simply easier than checking for strict canonical
559 form. Syntax sux! */
560
561 if (LOCAL_LABELS_FB && c == 'b')
562 {
563 /* Backward ref to local label.
564 Because it is backward, expect it to be defined. */
565 /* Construct a local label. */
566 name = fb_label_name (number, 0);
567
568 /* Seen before, or symbol is defined: OK. */
569 symbolP = symbol_find (name);
570 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
571 {
572 expressionP->X_op = O_symbol;
573 expressionP->X_add_symbol = symbolP;
574 }
575 else
576 {
577 /* Either not seen or not defined. */
578 /* @@ Should print out the original string instead of
579 the parsed number. */
580 as_bad (_("backward ref to unknown label \"%d:\""),
581 (int) number);
582 expressionP->X_op = O_constant;
583 }
584
585 expressionP->X_add_number = 0;
586 } /* case 'b' */
587 else if (LOCAL_LABELS_FB && c == 'f')
588 {
589 /* Forward reference. Expect symbol to be undefined or
590 unknown. undefined: seen it before. unknown: never seen
591 it before.
592
593 Construct a local label name, then an undefined symbol.
594 Don't create a xseg frag for it: caller may do that.
595 Just return it as never seen before. */
596 name = fb_label_name (number, 1);
597 symbolP = symbol_find_or_make (name);
598 /* We have no need to check symbol properties. */
599 expressionP->X_op = O_symbol;
600 expressionP->X_add_symbol = symbolP;
601 expressionP->X_add_number = 0;
602 } /* case 'f' */
603 else if (LOCAL_LABELS_DOLLAR && c == '$')
604 {
605 /* If the dollar label is *currently* defined, then this is just
606 another reference to it. If it is not *currently* defined,
607 then this is a fresh instantiation of that number, so create
608 it. */
609
610 if (dollar_label_defined (number))
611 {
612 name = dollar_label_name (number, 0);
613 symbolP = symbol_find (name);
614 know (symbolP != NULL);
615 }
616 else
617 {
618 name = dollar_label_name (number, 1);
619 symbolP = symbol_find_or_make (name);
620 }
621
622 expressionP->X_op = O_symbol;
623 expressionP->X_add_symbol = symbolP;
624 expressionP->X_add_number = 0;
625 } /* case '$' */
626 else
627 {
628 expressionP->X_op = O_constant;
629 expressionP->X_add_number = number;
630 input_line_pointer--; /* Restore following character. */
631 } /* Really just a number. */
632 }
633 else
634 {
635 /* Not a small number. */
636 expressionP->X_op = O_big;
637 expressionP->X_add_number = number; /* Number of littlenums. */
638 input_line_pointer--; /* -> char following number. */
639 }
640 }
641
642 /* Parse an MRI multi character constant. */
643
644 static void
645 mri_char_constant (expressionS *expressionP)
646 {
647 int i;
648
649 if (*input_line_pointer == '\''
650 && input_line_pointer[1] != '\'')
651 {
652 expressionP->X_op = O_constant;
653 expressionP->X_add_number = 0;
654 return;
655 }
656
657 /* In order to get the correct byte ordering, we must build the
658 number in reverse. */
659 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
660 {
661 int j;
662
663 generic_bignum[i] = 0;
664 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
665 {
666 if (*input_line_pointer == '\'')
667 {
668 if (input_line_pointer[1] != '\'')
669 break;
670 ++input_line_pointer;
671 }
672 generic_bignum[i] <<= 8;
673 generic_bignum[i] += *input_line_pointer;
674 ++input_line_pointer;
675 }
676
677 if (i < SIZE_OF_LARGE_NUMBER - 1)
678 {
679 /* If there is more than one littlenum, left justify the
680 last one to make it match the earlier ones. If there is
681 only one, we can just use the value directly. */
682 for (; j < CHARS_PER_LITTLENUM; j++)
683 generic_bignum[i] <<= 8;
684 }
685
686 if (*input_line_pointer == '\''
687 && input_line_pointer[1] != '\'')
688 break;
689 }
690
691 if (i < 0)
692 {
693 as_bad (_("character constant too large"));
694 i = 0;
695 }
696
697 if (i > 0)
698 {
699 int c;
700 int j;
701
702 c = SIZE_OF_LARGE_NUMBER - i;
703 for (j = 0; j < c; j++)
704 generic_bignum[j] = generic_bignum[i + j];
705 i = c;
706 }
707
708 know (LITTLENUM_NUMBER_OF_BITS == 16);
709 if (i > 2)
710 {
711 expressionP->X_op = O_big;
712 expressionP->X_add_number = i;
713 }
714 else
715 {
716 expressionP->X_op = O_constant;
717 if (i < 2)
718 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
719 else
720 expressionP->X_add_number =
721 (((generic_bignum[1] & LITTLENUM_MASK)
722 << LITTLENUM_NUMBER_OF_BITS)
723 | (generic_bignum[0] & LITTLENUM_MASK));
724 }
725
726 /* Skip the final closing quote. */
727 ++input_line_pointer;
728 }
729
730 /* Return an expression representing the current location. This
731 handles the magic symbol `.'. */
732
733 void
734 current_location (expressionS *expressionp)
735 {
736 if (now_seg == absolute_section)
737 {
738 expressionp->X_op = O_constant;
739 expressionp->X_add_number = abs_section_offset;
740 }
741 else
742 {
743 expressionp->X_op = O_symbol;
744 expressionp->X_add_symbol = &dot_symbol;
745 expressionp->X_add_number = 0;
746 }
747 }
748
749 #ifndef md_register_arithmetic
750 # define md_register_arithmetic 1
751 #endif
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 else if (!md_register_arithmetic && expressionP->X_op == O_register)
1131 {
1132 /* Convert to binary '+'. */
1133 expressionP->X_op_symbol = make_expr_symbol (expressionP);
1134 expressionP->X_add_symbol = make_expr_symbol (&zero);
1135 expressionP->X_add_number = 0;
1136 expressionP->X_op = O_add;
1137 }
1138 }
1139 else
1140 as_warn (_("Unary operator %c ignored because bad operand follows"),
1141 c);
1142 }
1143 break;
1144
1145 #if !defined (DOLLAR_DOT) && !defined (TC_M68K)
1146 case '$':
1147 if (literal_prefix_dollar_hex)
1148 {
1149 /* $L is the start of a local label, not a hex constant. */
1150 if (* input_line_pointer == 'L')
1151 goto isname;
1152 integer_constant (16, expressionP);
1153 }
1154 else
1155 {
1156 goto isname;
1157 }
1158 break;
1159 #else
1160 case '$':
1161 /* '$' is the program counter when in MRI mode, or when
1162 DOLLAR_DOT is defined. */
1163 #ifndef DOLLAR_DOT
1164 if (! flag_m68k_mri)
1165 goto de_fault;
1166 #endif
1167 if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1168 {
1169 /* In MRI mode and on Z80, '$' is also used as the prefix
1170 for a hexadecimal constant. */
1171 integer_constant (16, expressionP);
1172 break;
1173 }
1174
1175 if (is_part_of_name (*input_line_pointer))
1176 goto isname;
1177
1178 current_location (expressionP);
1179 break;
1180 #endif
1181
1182 case '.':
1183 if (!is_part_of_name (*input_line_pointer))
1184 {
1185 current_location (expressionP);
1186 break;
1187 }
1188 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1189 && ! is_part_of_name (input_line_pointer[8]))
1190 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1191 && ! is_part_of_name (input_line_pointer[7])))
1192 {
1193 int start;
1194
1195 start = (input_line_pointer[1] == 't'
1196 || input_line_pointer[1] == 'T');
1197 input_line_pointer += start ? 8 : 7;
1198 SKIP_WHITESPACE ();
1199
1200 /* Cover for the as_bad () invocations below. */
1201 expressionP->X_op = O_absent;
1202
1203 if (*input_line_pointer != '(')
1204 as_bad (_("syntax error in .startof. or .sizeof."));
1205 else
1206 {
1207 ++input_line_pointer;
1208 SKIP_WHITESPACE ();
1209 c = get_symbol_name (& name);
1210 if (! *name)
1211 {
1212 as_bad (_("expected symbol name"));
1213 (void) restore_line_pointer (c);
1214 if (c == ')')
1215 ++input_line_pointer;
1216 break;
1217 }
1218
1219 expressionP->X_op = O_symbol;
1220 expressionP->X_add_symbol = symbol_lookup_or_make (name, start);
1221 expressionP->X_add_number = 0;
1222
1223 *input_line_pointer = c;
1224 SKIP_WHITESPACE_AFTER_NAME ();
1225 if (*input_line_pointer != ')')
1226 as_bad (_("syntax error in .startof. or .sizeof."));
1227 else
1228 ++input_line_pointer;
1229 }
1230 break;
1231 }
1232 else
1233 {
1234 goto isname;
1235 }
1236
1237 case ',':
1238 eol:
1239 /* Can't imagine any other kind of operand. */
1240 expressionP->X_op = O_absent;
1241 input_line_pointer--;
1242 break;
1243
1244 #ifdef TC_M68K
1245 case '%':
1246 if (! flag_m68k_mri)
1247 goto de_fault;
1248 integer_constant (2, expressionP);
1249 break;
1250
1251 case '@':
1252 if (! flag_m68k_mri)
1253 goto de_fault;
1254 integer_constant (8, expressionP);
1255 break;
1256
1257 case ':':
1258 if (! flag_m68k_mri)
1259 goto de_fault;
1260
1261 /* In MRI mode, this is a floating point constant represented
1262 using hexadecimal digits. */
1263
1264 ++input_line_pointer;
1265 integer_constant (16, expressionP);
1266 break;
1267
1268 case '*':
1269 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1270 goto de_fault;
1271
1272 current_location (expressionP);
1273 break;
1274 #endif
1275
1276 default:
1277 #if defined(md_need_index_operator) || defined(TC_M68K)
1278 de_fault:
1279 #endif
1280 if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
1281 {
1282 /* Identifier begins here.
1283 This is kludged for speed, so code is repeated. */
1284 isname:
1285 -- input_line_pointer;
1286 c = get_symbol_name (&name);
1287
1288 #ifdef md_operator
1289 {
1290 operatorT op = md_operator (name, 1, &c);
1291
1292 switch (op)
1293 {
1294 case O_uminus:
1295 restore_line_pointer (c);
1296 c = '-';
1297 goto unary;
1298 case O_bit_not:
1299 restore_line_pointer (c);
1300 c = '~';
1301 goto unary;
1302 case O_logical_not:
1303 restore_line_pointer (c);
1304 c = '!';
1305 goto unary;
1306 case O_illegal:
1307 as_bad (_("invalid use of operator \"%s\""), name);
1308 break;
1309 default:
1310 break;
1311 }
1312
1313 if (op != O_absent && op != O_illegal)
1314 {
1315 restore_line_pointer (c);
1316 expr (9, expressionP, mode);
1317 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1318 expressionP->X_op_symbol = NULL;
1319 expressionP->X_add_number = 0;
1320 expressionP->X_op = op;
1321 break;
1322 }
1323 }
1324 #endif
1325
1326 #ifdef md_parse_name
1327 /* This is a hook for the backend to parse certain names
1328 specially in certain contexts. If a name always has a
1329 specific value, it can often be handled by simply
1330 entering it in the symbol table. */
1331 if (md_parse_name (name, expressionP, mode, &c))
1332 {
1333 restore_line_pointer (c);
1334 break;
1335 }
1336 #endif
1337
1338 symbolP = symbol_find_or_make (name);
1339
1340 /* If we have an absolute symbol or a reg, then we know its
1341 value now. */
1342 segment = S_GET_SEGMENT (symbolP);
1343 if (mode != expr_defer
1344 && segment == absolute_section
1345 && !S_FORCE_RELOC (symbolP, 0))
1346 {
1347 expressionP->X_op = O_constant;
1348 expressionP->X_add_number = S_GET_VALUE (symbolP);
1349 }
1350 else if (mode != expr_defer && segment == reg_section)
1351 {
1352 expressionP->X_op = O_register;
1353 expressionP->X_add_number = S_GET_VALUE (symbolP);
1354 }
1355 else
1356 {
1357 expressionP->X_op = O_symbol;
1358 expressionP->X_add_symbol = symbolP;
1359 expressionP->X_add_number = 0;
1360 }
1361
1362 restore_line_pointer (c);
1363 }
1364 else
1365 {
1366 /* Let the target try to parse it. Success is indicated by changing
1367 the X_op field to something other than O_absent and pointing
1368 input_line_pointer past the expression. If it can't parse the
1369 expression, X_op and input_line_pointer should be unchanged. */
1370 expressionP->X_op = O_absent;
1371 --input_line_pointer;
1372 md_operand (expressionP);
1373 if (expressionP->X_op == O_absent)
1374 {
1375 ++input_line_pointer;
1376 as_bad (_("bad expression"));
1377 expressionP->X_op = O_constant;
1378 expressionP->X_add_number = 0;
1379 }
1380 }
1381 break;
1382 }
1383
1384 /* It is more 'efficient' to clean up the expressionS when they are
1385 created. Doing it here saves lines of code. */
1386 clean_up_expression (expressionP);
1387 SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */
1388 know (*input_line_pointer != ' ');
1389
1390 /* The PA port needs this information. */
1391 if (expressionP->X_add_symbol)
1392 symbol_mark_used (expressionP->X_add_symbol);
1393
1394 if (mode != expr_defer)
1395 {
1396 expressionP->X_add_symbol
1397 = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1398 expressionP->X_op_symbol
1399 = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1400 }
1401
1402 switch (expressionP->X_op)
1403 {
1404 default:
1405 return absolute_section;
1406 case O_symbol:
1407 return S_GET_SEGMENT (expressionP->X_add_symbol);
1408 case O_register:
1409 return reg_section;
1410 }
1411 }
1412 \f
1413 /* Internal. Simplify a struct expression for use by expr (). */
1414
1415 /* In: address of an expressionS.
1416 The X_op field of the expressionS may only take certain values.
1417 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1418
1419 Out: expressionS may have been modified:
1420 Unused fields zeroed to help expr (). */
1421
1422 static void
1423 clean_up_expression (expressionS *expressionP)
1424 {
1425 switch (expressionP->X_op)
1426 {
1427 case O_illegal:
1428 case O_absent:
1429 expressionP->X_add_number = 0;
1430 /* Fall through. */
1431 case O_big:
1432 case O_constant:
1433 case O_register:
1434 expressionP->X_add_symbol = NULL;
1435 /* Fall through. */
1436 case O_symbol:
1437 case O_uminus:
1438 case O_bit_not:
1439 expressionP->X_op_symbol = NULL;
1440 break;
1441 default:
1442 break;
1443 }
1444 }
1445 \f
1446 /* Expression parser. */
1447
1448 /* We allow an empty expression, and just assume (absolute,0) silently.
1449 Unary operators and parenthetical expressions are treated as operands.
1450 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1451
1452 We used to do an aho/ullman shift-reduce parser, but the logic got so
1453 warped that I flushed it and wrote a recursive-descent parser instead.
1454 Now things are stable, would anybody like to write a fast parser?
1455 Most expressions are either register (which does not even reach here)
1456 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1457 So I guess it doesn't really matter how inefficient more complex expressions
1458 are parsed.
1459
1460 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1461 Also, we have consumed any leading or trailing spaces (operand does that)
1462 and done all intervening operators.
1463
1464 This returns the segment of the result, which will be
1465 absolute_section or the segment of a symbol. */
1466
1467 #undef __
1468 #define __ O_illegal
1469 #ifndef O_SINGLE_EQ
1470 #define O_SINGLE_EQ O_illegal
1471 #endif
1472
1473 /* Maps ASCII -> operators. */
1474 static const operatorT op_encoding[256] = {
1475 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1476 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1477
1478 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1479 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1480 __, __, __, __, __, __, __, __,
1481 __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1482 __, __, __, __, __, __, __, __,
1483 __, __, __, __, __, __, __, __,
1484 __, __, __, __, __, __, __, __,
1485 __, __, __,
1486 #ifdef NEED_INDEX_OPERATOR
1487 O_index,
1488 #else
1489 __,
1490 #endif
1491 __, __, O_bit_exclusive_or, __,
1492 __, __, __, __, __, __, __, __,
1493 __, __, __, __, __, __, __, __,
1494 __, __, __, __, __, __, __, __,
1495 __, __, __, __, O_bit_inclusive_or, __, __, __,
1496
1497 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1498 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1499 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1500 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1501 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1502 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1503 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1504 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1505 };
1506
1507 /* Rank Examples
1508 0 operand, (expression)
1509 1 ||
1510 2 &&
1511 3 == <> < <= >= >
1512 4 + -
1513 5 used for * / % in MRI mode
1514 6 & ^ ! |
1515 7 * / % << >>
1516 8 unary - unary ~
1517 */
1518 static operator_rankT op_rank[O_max] = {
1519 0, /* O_illegal */
1520 0, /* O_absent */
1521 0, /* O_constant */
1522 0, /* O_symbol */
1523 0, /* O_symbol_rva */
1524 0, /* O_secidx */
1525 0, /* O_register */
1526 0, /* O_big */
1527 9, /* O_uminus */
1528 9, /* O_bit_not */
1529 9, /* O_logical_not */
1530 8, /* O_multiply */
1531 8, /* O_divide */
1532 8, /* O_modulus */
1533 8, /* O_left_shift */
1534 8, /* O_right_shift */
1535 7, /* O_bit_inclusive_or */
1536 7, /* O_bit_or_not */
1537 7, /* O_bit_exclusive_or */
1538 7, /* O_bit_and */
1539 5, /* O_add */
1540 5, /* O_subtract */
1541 4, /* O_eq */
1542 4, /* O_ne */
1543 4, /* O_lt */
1544 4, /* O_le */
1545 4, /* O_ge */
1546 4, /* O_gt */
1547 3, /* O_logical_and */
1548 2, /* O_logical_or */
1549 1, /* O_index */
1550 };
1551
1552 /* Unfortunately, in MRI mode for the m68k, multiplication and
1553 division have lower precedence than the bit wise operators. This
1554 function sets the operator precedences correctly for the current
1555 mode. Also, MRI uses a different bit_not operator, and this fixes
1556 that as well. */
1557
1558 #define STANDARD_MUL_PRECEDENCE 8
1559 #define MRI_MUL_PRECEDENCE 6
1560
1561 void
1562 expr_set_precedence (void)
1563 {
1564 if (flag_m68k_mri)
1565 {
1566 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1567 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1568 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1569 }
1570 else
1571 {
1572 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1573 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1574 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1575 }
1576 }
1577
1578 void
1579 expr_set_rank (operatorT op, operator_rankT rank)
1580 {
1581 gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1582 op_rank[op] = rank;
1583 }
1584
1585 /* Initialize the expression parser. */
1586
1587 void
1588 expr_begin (void)
1589 {
1590 expr_set_precedence ();
1591
1592 /* Verify that X_op field is wide enough. */
1593 {
1594 expressionS e;
1595 e.X_op = O_max;
1596 gas_assert (e.X_op == O_max);
1597 }
1598
1599 memset (seen, 0, sizeof seen);
1600 memset (nr_seen, 0, sizeof nr_seen);
1601 expr_symbol_lines = NULL;
1602 }
1603
1604 void
1605 expr_end (void)
1606 {
1607 for (size_t i = 0; i < ARRAY_SIZE (seen); i++)
1608 free (seen[i]);
1609 }
1610 \f
1611 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1612 sets NUM_CHARS to the number of characters in the operator.
1613 Does not advance INPUT_LINE_POINTER. */
1614
1615 static inline operatorT
1616 operatorf (int *num_chars)
1617 {
1618 int c;
1619 operatorT ret;
1620
1621 c = *input_line_pointer & 0xff;
1622 *num_chars = 1;
1623
1624 if (is_end_of_line[c])
1625 return O_illegal;
1626
1627 #ifdef md_operator
1628 if (is_name_beginner (c))
1629 {
1630 char *name;
1631 char ec = get_symbol_name (& name);
1632
1633 ret = md_operator (name, 2, &ec);
1634 switch (ret)
1635 {
1636 case O_absent:
1637 *input_line_pointer = ec;
1638 input_line_pointer = name;
1639 break;
1640 case O_uminus:
1641 case O_bit_not:
1642 case O_logical_not:
1643 as_bad (_("invalid use of operator \"%s\""), name);
1644 ret = O_illegal;
1645 /* FALLTHROUGH */
1646 default:
1647 *input_line_pointer = ec;
1648 *num_chars = input_line_pointer - name;
1649 input_line_pointer = name;
1650 return ret;
1651 }
1652 }
1653 #endif
1654
1655 switch (c)
1656 {
1657 default:
1658 ret = op_encoding[c];
1659 #ifdef md_operator
1660 if (ret == O_illegal)
1661 {
1662 char *start = input_line_pointer;
1663
1664 ret = md_operator (NULL, 2, NULL);
1665 if (ret != O_illegal)
1666 *num_chars = input_line_pointer - start;
1667 input_line_pointer = start;
1668 }
1669 #endif
1670 return ret;
1671
1672 case '+':
1673 case '-':
1674 return op_encoding[c];
1675
1676 case '<':
1677 switch (input_line_pointer[1])
1678 {
1679 default:
1680 return op_encoding[c];
1681 case '<':
1682 ret = O_left_shift;
1683 break;
1684 case '>':
1685 ret = O_ne;
1686 break;
1687 case '=':
1688 ret = O_le;
1689 break;
1690 }
1691 *num_chars = 2;
1692 return ret;
1693
1694 case '=':
1695 if (input_line_pointer[1] != '=')
1696 return op_encoding[c];
1697
1698 *num_chars = 2;
1699 return O_eq;
1700
1701 case '>':
1702 switch (input_line_pointer[1])
1703 {
1704 default:
1705 return op_encoding[c];
1706 case '>':
1707 ret = O_right_shift;
1708 break;
1709 case '=':
1710 ret = O_ge;
1711 break;
1712 }
1713 *num_chars = 2;
1714 return ret;
1715
1716 case '!':
1717 switch (input_line_pointer[1])
1718 {
1719 case '!':
1720 /* We accept !! as equivalent to ^ for MRI compatibility. */
1721 *num_chars = 2;
1722 return O_bit_exclusive_or;
1723 case '=':
1724 /* We accept != as equivalent to <>. */
1725 *num_chars = 2;
1726 return O_ne;
1727 default:
1728 if (flag_m68k_mri)
1729 return O_bit_inclusive_or;
1730 return op_encoding[c];
1731 }
1732
1733 case '|':
1734 if (input_line_pointer[1] != '|')
1735 return op_encoding[c];
1736
1737 *num_chars = 2;
1738 return O_logical_or;
1739
1740 case '&':
1741 if (input_line_pointer[1] != '&')
1742 return op_encoding[c];
1743
1744 *num_chars = 2;
1745 return O_logical_and;
1746 }
1747
1748 /* NOTREACHED */
1749 }
1750
1751 /* Implement "word-size + 1 bit" addition for
1752 {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
1753 is used so that the full range of unsigned word values and the full range of
1754 signed word values can be represented in an O_constant expression, which is
1755 useful e.g. for .sleb128 directives. */
1756
1757 void
1758 add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1759 {
1760 valueT ures = resultP->X_add_number;
1761 valueT uamount = amount;
1762
1763 resultP->X_add_number += uamount;
1764
1765 resultP->X_extrabit ^= rhs_highbit;
1766
1767 if (ures + uamount < ures)
1768 resultP->X_extrabit ^= 1;
1769 }
1770
1771 /* Similarly, for subtraction. */
1772
1773 void
1774 subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1775 {
1776 valueT ures = resultP->X_add_number;
1777 valueT uamount = amount;
1778
1779 resultP->X_add_number -= uamount;
1780
1781 resultP->X_extrabit ^= rhs_highbit;
1782
1783 if (ures < uamount)
1784 resultP->X_extrabit ^= 1;
1785 }
1786
1787 /* Parse an expression. */
1788
1789 segT
1790 expr (int rankarg, /* Larger # is higher rank. */
1791 expressionS *resultP, /* Deliver result here. */
1792 enum expr_mode mode /* Controls behavior. */)
1793 {
1794 operator_rankT rank = (operator_rankT) rankarg;
1795 segT retval;
1796 expressionS right;
1797 operatorT op_left;
1798 operatorT op_right;
1799 int op_chars;
1800
1801 know (rankarg >= 0);
1802
1803 /* Save the value of dot for the fixup code. */
1804 if (rank == 0)
1805 {
1806 dot_value = frag_now_fix ();
1807 dot_frag = frag_now;
1808 }
1809
1810 retval = operand (resultP, mode);
1811
1812 /* operand () gobbles spaces. */
1813 know (*input_line_pointer != ' ');
1814
1815 op_left = operatorf (&op_chars);
1816 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1817 {
1818 segT rightseg;
1819 offsetT frag_off;
1820
1821 input_line_pointer += op_chars; /* -> after operator. */
1822
1823 right.X_md = 0;
1824 rightseg = expr (op_rank[(int) op_left], &right, mode);
1825 if (right.X_op == O_absent)
1826 {
1827 as_warn (_("missing operand; zero assumed"));
1828 right.X_op = O_constant;
1829 right.X_add_number = 0;
1830 right.X_add_symbol = NULL;
1831 right.X_op_symbol = NULL;
1832 }
1833
1834 know (*input_line_pointer != ' ');
1835
1836 if (op_left == O_index)
1837 {
1838 if (*input_line_pointer != ']')
1839 as_bad ("missing right bracket");
1840 else
1841 {
1842 ++input_line_pointer;
1843 SKIP_WHITESPACE ();
1844 }
1845 }
1846
1847 op_right = operatorf (&op_chars);
1848
1849 know (op_right == O_illegal || op_left == O_index
1850 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1851 know ((int) op_left >= (int) O_multiply);
1852 #ifndef md_operator
1853 know ((int) op_left <= (int) O_index);
1854 #else
1855 know ((int) op_left < (int) O_max);
1856 #endif
1857
1858 /* input_line_pointer->after right-hand quantity. */
1859 /* left-hand quantity in resultP. */
1860 /* right-hand quantity in right. */
1861 /* operator in op_left. */
1862
1863 if (resultP->X_op == O_big)
1864 {
1865 if (resultP->X_add_number > 0)
1866 as_warn (_("left operand is a bignum; integer 0 assumed"));
1867 else
1868 as_warn (_("left operand is a float; integer 0 assumed"));
1869 resultP->X_op = O_constant;
1870 resultP->X_add_number = 0;
1871 resultP->X_add_symbol = NULL;
1872 resultP->X_op_symbol = NULL;
1873 }
1874 if (right.X_op == O_big)
1875 {
1876 if (right.X_add_number > 0)
1877 as_warn (_("right operand is a bignum; integer 0 assumed"));
1878 else
1879 as_warn (_("right operand is a float; integer 0 assumed"));
1880 right.X_op = O_constant;
1881 right.X_add_number = 0;
1882 right.X_add_symbol = NULL;
1883 right.X_op_symbol = NULL;
1884 }
1885
1886 if (mode == expr_defer
1887 && ((resultP->X_add_symbol != NULL
1888 && S_IS_FORWARD_REF (resultP->X_add_symbol))
1889 || (right.X_add_symbol != NULL
1890 && S_IS_FORWARD_REF (right.X_add_symbol))))
1891 goto general;
1892
1893 /* Optimize common cases. */
1894 #ifdef md_optimize_expr
1895 if (md_optimize_expr (resultP, op_left, &right))
1896 {
1897 /* Skip. */
1898 ;
1899 }
1900 else
1901 #endif
1902 if (op_left == O_add && right.X_op == O_constant
1903 && (md_register_arithmetic || resultP->X_op != O_register))
1904 {
1905 /* X + constant. */
1906 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1907 }
1908 /* This case comes up in PIC code. */
1909 else if (op_left == O_subtract
1910 && right.X_op == O_symbol
1911 && resultP->X_op == O_symbol
1912 && retval == rightseg
1913 #ifdef md_allow_local_subtract
1914 && md_allow_local_subtract (resultP, & right, rightseg)
1915 #endif
1916 && ((SEG_NORMAL (rightseg)
1917 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1918 && !S_FORCE_RELOC (right.X_add_symbol, 0))
1919 || right.X_add_symbol == resultP->X_add_symbol)
1920 && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1921 symbol_get_frag (right.X_add_symbol),
1922 &frag_off))
1923 {
1924 offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1925 - S_GET_VALUE (right.X_add_symbol);
1926 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1927 subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1928 add_to_result (resultP, symval_diff, symval_diff < 0);
1929 resultP->X_op = O_constant;
1930 resultP->X_add_symbol = 0;
1931 }
1932 else if (op_left == O_subtract && right.X_op == O_constant
1933 && (md_register_arithmetic || resultP->X_op != O_register))
1934 {
1935 /* X - constant. */
1936 subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1937 }
1938 else if (op_left == O_add && resultP->X_op == O_constant
1939 && (md_register_arithmetic || right.X_op != O_register))
1940 {
1941 /* Constant + X. */
1942 resultP->X_op = right.X_op;
1943 resultP->X_add_symbol = right.X_add_symbol;
1944 resultP->X_op_symbol = right.X_op_symbol;
1945 add_to_result (resultP, right.X_add_number, right.X_extrabit);
1946 retval = rightseg;
1947 }
1948 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1949 {
1950 /* Constant OP constant. */
1951 offsetT v = right.X_add_number;
1952 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1953 {
1954 as_warn (_("division by zero"));
1955 v = 1;
1956 }
1957 if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1958 && (op_left == O_left_shift || op_left == O_right_shift))
1959 {
1960 as_warn_value_out_of_range (_("shift count"), v, 0,
1961 sizeof(valueT) * CHAR_BIT - 1,
1962 NULL, 0);
1963 resultP->X_add_number = v = 0;
1964 }
1965 switch (op_left)
1966 {
1967 default: goto general;
1968 case O_multiply:
1969 /* Do the multiply as unsigned to silence ubsan. The
1970 result is of course the same when we throw away high
1971 bits of the result. */
1972 resultP->X_add_number *= (valueT) v;
1973 break;
1974 case O_divide: resultP->X_add_number /= v; break;
1975 case O_modulus: resultP->X_add_number %= v; break;
1976 case O_left_shift:
1977 /* We always use unsigned shifts. According to the ISO
1978 C standard, left shift of a signed type having a
1979 negative value is undefined behaviour, and right
1980 shift of a signed type having negative value is
1981 implementation defined. Left shift of a signed type
1982 when the result overflows is also undefined
1983 behaviour. So don't trigger ubsan warnings or rely
1984 on characteristics of the compiler. */
1985 resultP->X_add_number
1986 = (valueT) resultP->X_add_number << (valueT) v;
1987 break;
1988 case O_right_shift:
1989 resultP->X_add_number
1990 = (valueT) resultP->X_add_number >> (valueT) v;
1991 break;
1992 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1993 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1994 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1995 case O_bit_and: resultP->X_add_number &= v; break;
1996 /* Constant + constant (O_add) is handled by the
1997 previous if statement for constant + X, so is omitted
1998 here. */
1999 case O_subtract:
2000 subtract_from_result (resultP, v, 0);
2001 break;
2002 case O_eq:
2003 resultP->X_add_number =
2004 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
2005 break;
2006 case O_ne:
2007 resultP->X_add_number =
2008 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
2009 break;
2010 case O_lt:
2011 resultP->X_add_number =
2012 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
2013 break;
2014 case O_le:
2015 resultP->X_add_number =
2016 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
2017 break;
2018 case O_ge:
2019 resultP->X_add_number =
2020 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
2021 break;
2022 case O_gt:
2023 resultP->X_add_number =
2024 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
2025 break;
2026 case O_logical_and:
2027 resultP->X_add_number = resultP->X_add_number && v;
2028 break;
2029 case O_logical_or:
2030 resultP->X_add_number = resultP->X_add_number || v;
2031 break;
2032 }
2033 }
2034 else if (resultP->X_op == O_symbol
2035 && right.X_op == O_symbol
2036 && (op_left == O_add
2037 || op_left == O_subtract
2038 || (resultP->X_add_number == 0
2039 && right.X_add_number == 0)))
2040 {
2041 /* Symbol OP symbol. */
2042 resultP->X_op = op_left;
2043 resultP->X_op_symbol = right.X_add_symbol;
2044 if (op_left == O_add)
2045 add_to_result (resultP, right.X_add_number, right.X_extrabit);
2046 else if (op_left == O_subtract)
2047 {
2048 subtract_from_result (resultP, right.X_add_number,
2049 right.X_extrabit);
2050 if (retval == rightseg
2051 && SEG_NORMAL (retval)
2052 && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2053 && !S_FORCE_RELOC (right.X_add_symbol, 0))
2054 {
2055 retval = absolute_section;
2056 rightseg = absolute_section;
2057 }
2058 }
2059 }
2060 else
2061 {
2062 general:
2063 /* The general case. */
2064 resultP->X_add_symbol = make_expr_symbol (resultP);
2065 resultP->X_op_symbol = make_expr_symbol (&right);
2066 resultP->X_op = op_left;
2067 resultP->X_add_number = 0;
2068 resultP->X_unsigned = 1;
2069 resultP->X_extrabit = 0;
2070 }
2071
2072 if (retval != rightseg)
2073 {
2074 if (retval == undefined_section)
2075 ;
2076 else if (rightseg == undefined_section)
2077 retval = rightseg;
2078 else if (retval == expr_section)
2079 ;
2080 else if (rightseg == expr_section)
2081 retval = rightseg;
2082 else if (retval == reg_section)
2083 ;
2084 else if (rightseg == reg_section)
2085 retval = rightseg;
2086 else if (rightseg == absolute_section)
2087 ;
2088 else if (retval == absolute_section)
2089 retval = rightseg;
2090 #ifdef DIFF_EXPR_OK
2091 else if (op_left == O_subtract)
2092 ;
2093 #endif
2094 else
2095 as_bad (_("operation combines symbols in different segments"));
2096 }
2097
2098 op_left = op_right;
2099 } /* While next operator is >= this rank. */
2100
2101 /* The PA port needs this information. */
2102 if (resultP->X_add_symbol)
2103 symbol_mark_used (resultP->X_add_symbol);
2104
2105 if (rank == 0 && mode == expr_evaluate)
2106 resolve_expression (resultP);
2107
2108 return resultP->X_op == O_constant ? absolute_section : retval;
2109 }
2110
2111 /* Resolve an expression without changing any symbols/sub-expressions
2112 used. */
2113
2114 int
2115 resolve_expression (expressionS *expressionP)
2116 {
2117 /* Help out with CSE. */
2118 valueT final_val = expressionP->X_add_number;
2119 symbolS *add_symbol = expressionP->X_add_symbol;
2120 symbolS *orig_add_symbol = add_symbol;
2121 symbolS *op_symbol = expressionP->X_op_symbol;
2122 operatorT op = expressionP->X_op;
2123 valueT left, right;
2124 segT seg_left, seg_right;
2125 fragS *frag_left, *frag_right;
2126 offsetT frag_off;
2127
2128 switch (op)
2129 {
2130 default:
2131 return 0;
2132
2133 case O_constant:
2134 case O_register:
2135 left = 0;
2136 break;
2137
2138 case O_symbol:
2139 case O_symbol_rva:
2140 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2141 return 0;
2142
2143 break;
2144
2145 case O_uminus:
2146 case O_bit_not:
2147 case O_logical_not:
2148 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2149 return 0;
2150
2151 if (seg_left != absolute_section)
2152 return 0;
2153
2154 if (op == O_logical_not)
2155 left = !left;
2156 else if (op == O_uminus)
2157 left = -left;
2158 else
2159 left = ~left;
2160 op = O_constant;
2161 break;
2162
2163 case O_multiply:
2164 case O_divide:
2165 case O_modulus:
2166 case O_left_shift:
2167 case O_right_shift:
2168 case O_bit_inclusive_or:
2169 case O_bit_or_not:
2170 case O_bit_exclusive_or:
2171 case O_bit_and:
2172 case O_add:
2173 case O_subtract:
2174 case O_eq:
2175 case O_ne:
2176 case O_lt:
2177 case O_le:
2178 case O_ge:
2179 case O_gt:
2180 case O_logical_and:
2181 case O_logical_or:
2182 if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2183 || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2184 return 0;
2185
2186 /* Simplify addition or subtraction of a constant by folding the
2187 constant into X_add_number. */
2188 if (op == O_add)
2189 {
2190 if (seg_right == absolute_section)
2191 {
2192 final_val += right;
2193 op = O_symbol;
2194 break;
2195 }
2196 else if (seg_left == absolute_section)
2197 {
2198 final_val += left;
2199 left = right;
2200 seg_left = seg_right;
2201 add_symbol = op_symbol;
2202 orig_add_symbol = expressionP->X_op_symbol;
2203 op = O_symbol;
2204 break;
2205 }
2206 }
2207 else if (op == O_subtract)
2208 {
2209 if (seg_right == absolute_section)
2210 {
2211 final_val -= right;
2212 op = O_symbol;
2213 break;
2214 }
2215 }
2216
2217 /* Equality and non-equality tests are permitted on anything.
2218 Subtraction, and other comparison operators are permitted if
2219 both operands are in the same section.
2220 Shifts by constant zero are permitted on anything.
2221 Multiplies, bit-ors, and bit-ands with constant zero are
2222 permitted on anything.
2223 Multiplies and divides by constant one are permitted on
2224 anything.
2225 Binary operations with both operands being the same register
2226 or undefined symbol are permitted if the result doesn't depend
2227 on the input value.
2228 Otherwise, both operands must be absolute. We already handled
2229 the case of addition or subtraction of a constant above. */
2230 frag_off = 0;
2231 if (!(seg_left == absolute_section
2232 && seg_right == absolute_section)
2233 && !(op == O_eq || op == O_ne)
2234 && !((op == O_subtract
2235 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2236 && seg_left == seg_right
2237 && (finalize_syms
2238 || frag_offset_fixed_p (frag_left, frag_right, &frag_off)
2239 || (op == O_gt
2240 && frag_gtoffset_p (left, frag_left,
2241 right, frag_right, &frag_off)))
2242 && (seg_left != reg_section || left == right)
2243 && (seg_left != undefined_section || add_symbol == op_symbol)))
2244 {
2245 if ((seg_left == absolute_section && left == 0)
2246 || (seg_right == absolute_section && right == 0))
2247 {
2248 if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2249 {
2250 if (!(seg_right == absolute_section && right == 0))
2251 {
2252 seg_left = seg_right;
2253 left = right;
2254 add_symbol = op_symbol;
2255 orig_add_symbol = expressionP->X_op_symbol;
2256 }
2257 op = O_symbol;
2258 break;
2259 }
2260 else if (op == O_left_shift || op == O_right_shift)
2261 {
2262 if (!(seg_left == absolute_section && left == 0))
2263 {
2264 op = O_symbol;
2265 break;
2266 }
2267 }
2268 else if (op != O_multiply
2269 && op != O_bit_or_not && op != O_bit_and)
2270 return 0;
2271 }
2272 else if (op == O_multiply
2273 && seg_left == absolute_section && left == 1)
2274 {
2275 seg_left = seg_right;
2276 left = right;
2277 add_symbol = op_symbol;
2278 orig_add_symbol = expressionP->X_op_symbol;
2279 op = O_symbol;
2280 break;
2281 }
2282 else if ((op == O_multiply || op == O_divide)
2283 && seg_right == absolute_section && right == 1)
2284 {
2285 op = O_symbol;
2286 break;
2287 }
2288 else if (!(left == right
2289 && ((seg_left == reg_section && seg_right == reg_section)
2290 || (seg_left == undefined_section
2291 && seg_right == undefined_section
2292 && add_symbol == op_symbol))))
2293 return 0;
2294 else if (op == O_bit_and || op == O_bit_inclusive_or)
2295 {
2296 op = O_symbol;
2297 break;
2298 }
2299 else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2300 return 0;
2301 }
2302
2303 right += frag_off / OCTETS_PER_BYTE;
2304 switch (op)
2305 {
2306 case O_add: left += right; break;
2307 case O_subtract: left -= right; break;
2308 case O_multiply: left *= right; break;
2309 case O_divide:
2310 if (right == 0)
2311 return 0;
2312 left = (offsetT) left / (offsetT) right;
2313 break;
2314 case O_modulus:
2315 if (right == 0)
2316 return 0;
2317 left = (offsetT) left % (offsetT) right;
2318 break;
2319 case O_left_shift:
2320 if (right >= sizeof (left) * CHAR_BIT)
2321 left = 0;
2322 else
2323 left <<= right;
2324 break;
2325 case O_right_shift:
2326 if (right >= sizeof (left) * CHAR_BIT)
2327 left = 0;
2328 else
2329 left >>= right;
2330 break;
2331 case O_bit_inclusive_or: left |= right; break;
2332 case O_bit_or_not: left |= ~right; break;
2333 case O_bit_exclusive_or: left ^= right; break;
2334 case O_bit_and: left &= right; break;
2335 case O_eq:
2336 case O_ne:
2337 left = (left == right
2338 && seg_left == seg_right
2339 && (finalize_syms || frag_left == frag_right)
2340 && (seg_left != undefined_section
2341 || add_symbol == op_symbol)
2342 ? ~ (valueT) 0 : 0);
2343 if (op == O_ne)
2344 left = ~left;
2345 break;
2346 case O_lt:
2347 left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
2348 break;
2349 case O_le:
2350 left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2351 break;
2352 case O_ge:
2353 left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2354 break;
2355 case O_gt:
2356 left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
2357 break;
2358 case O_logical_and: left = left && right; break;
2359 case O_logical_or: left = left || right; break;
2360 default: abort ();
2361 }
2362
2363 op = O_constant;
2364 break;
2365 }
2366
2367 if (op == O_symbol)
2368 {
2369 if (seg_left == absolute_section)
2370 op = O_constant;
2371 else if (seg_left == reg_section && final_val == 0)
2372 op = O_register;
2373 else if (!symbol_same_p (add_symbol, orig_add_symbol))
2374 final_val += left;
2375 expressionP->X_add_symbol = add_symbol;
2376 }
2377 expressionP->X_op = op;
2378
2379 if (op == O_constant || op == O_register)
2380 final_val += left;
2381 expressionP->X_add_number = final_val;
2382
2383 return 1;
2384 }
2385 \f
2386 /* This lives here because it belongs equally in expr.c & read.c.
2387 expr.c is just a branch office read.c anyway, and putting it
2388 here lessens the crowd at read.c.
2389
2390 Assume input_line_pointer is at start of symbol name, or the
2391 start of a double quote enclosed symbol name.
2392 Advance input_line_pointer past symbol name.
2393 Turn that character into a '\0', returning its former value,
2394 which may be the closing double quote.
2395 This allows a string compare (RMS wants symbol names to be strings)
2396 of the symbol name.
2397 There will always be a char following symbol name, because all good
2398 lines end in end-of-line. */
2399
2400 char
2401 get_symbol_name (char ** ilp_return)
2402 {
2403 char c;
2404
2405 * ilp_return = input_line_pointer;
2406 /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a
2407 constructed string. */
2408 if (is_name_beginner (c = *input_line_pointer++)
2409 || (input_from_string && c == FAKE_LABEL_CHAR))
2410 {
2411 while (is_part_of_name (c = *input_line_pointer++)
2412 || (input_from_string && c == FAKE_LABEL_CHAR))
2413 ;
2414 if (is_name_ender (c))
2415 c = *input_line_pointer++;
2416 }
2417 else if (c == '"')
2418 {
2419 char *dst = input_line_pointer;
2420
2421 * ilp_return = input_line_pointer;
2422 for (;;)
2423 {
2424 c = *input_line_pointer++;
2425
2426 if (c == 0)
2427 {
2428 as_warn (_("missing closing '\"'"));
2429 break;
2430 }
2431
2432 if (c == '"')
2433 {
2434 char *ilp_save = input_line_pointer;
2435
2436 SKIP_WHITESPACE ();
2437 if (*input_line_pointer == '"')
2438 {
2439 ++input_line_pointer;
2440 continue;
2441 }
2442 input_line_pointer = ilp_save;
2443 break;
2444 }
2445
2446 if (c == '\\')
2447 switch (*input_line_pointer)
2448 {
2449 case '"':
2450 case '\\':
2451 c = *input_line_pointer++;
2452 break;
2453
2454 default:
2455 if (c != 0)
2456 as_warn (_("'\\%c' in quoted symbol name; "
2457 "behavior may change in the future"),
2458 *input_line_pointer);
2459 break;
2460 }
2461
2462 *dst++ = c;
2463 }
2464 *dst = 0;
2465 }
2466 *--input_line_pointer = 0;
2467 return c;
2468 }
2469
2470 /* Replace the NUL character pointed to by input_line_pointer
2471 with C. If C is \" then advance past it. Return the character
2472 now pointed to by input_line_pointer. */
2473
2474 char
2475 restore_line_pointer (char c)
2476 {
2477 * input_line_pointer = c;
2478 if (c == '"')
2479 c = * ++ input_line_pointer;
2480 return c;
2481 }
2482
2483 unsigned int
2484 get_single_number (void)
2485 {
2486 expressionS exp;
2487 operand (&exp, expr_normal);
2488 return exp.X_add_number;
2489 }