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