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