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