1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This is really a branch office of as-read.c. I split it out to clearly
23 distinguish the world of expressions from the world of statements.
24 (It also gives smaller files to re-compile.)
25 Here, "operand"s are of expressions, not instructions. */
29 #define min(a, b) ((a) < (b) ? (a) : (b))
34 static void floating_constant PARAMS ((expressionS * expressionP));
35 static valueT generic_bignum_to_int32 PARAMS ((void));
37 static valueT generic_bignum_to_int64 PARAMS ((void));
39 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
40 static void mri_char_constant PARAMS ((expressionS *));
41 static void current_location PARAMS ((expressionS *));
42 static void clean_up_expression PARAMS ((expressionS * expressionP));
43 static segT operand PARAMS ((expressionS *));
44 static operatorT operator PARAMS ((void));
46 extern const char EXP_CHARS[], FLT_CHARS[];
48 /* We keep a mapping of expression symbols to file positions, so that
49 we can provide better error messages. */
51 struct expr_symbol_line
53 struct expr_symbol_line *next;
59 static struct expr_symbol_line *expr_symbol_lines;
61 /* Build a dummy symbol to hold a complex expression. This is how we
62 build expressions up out of other expressions. The symbol is put
63 into the fake section expr_section. */
66 make_expr_symbol (expressionP)
67 expressionS *expressionP;
72 struct expr_symbol_line *n;
74 if (expressionP->X_op == O_symbol
75 && expressionP->X_add_number == 0)
76 return expressionP->X_add_symbol;
78 if (expressionP->X_op == O_big)
80 /* This won't work, because the actual value is stored in
81 generic_floating_point_number or generic_bignum, and we are
82 going to lose it if we haven't already. */
83 if (expressionP->X_add_number > 0)
84 as_bad (_("bignum invalid; zero assumed"));
86 as_bad (_("floating point number invalid; zero assumed"));
87 zero.X_op = O_constant;
88 zero.X_add_number = 0;
90 clean_up_expression (&zero);
94 fake = FAKE_LABEL_NAME;
96 /* Putting constant symbols in absolute_section rather than
97 expr_section is convenient for the old a.out code, for which
98 S_GET_SEGMENT does not always retrieve the value put in by
100 symbolP = symbol_create (fake,
101 (expressionP->X_op == O_constant
104 0, &zero_address_frag);
105 symbol_set_value_expression (symbolP, expressionP);
107 if (expressionP->X_op == O_constant)
108 resolve_symbol_value (symbolP, 1);
110 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
112 as_where (&n->file, &n->line);
113 n->next = expr_symbol_lines;
114 expr_symbol_lines = n;
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
124 expr_symbol_where (sym, pfile, pline)
129 register struct expr_symbol_line *l;
131 for (l = expr_symbol_lines; l != NULL; l = l->next)
144 /* Utilities for building expressions.
145 Since complex expressions are recorded as symbols for use in other
146 expressions these return a symbolS * and not an expressionS *.
147 These explicitly do not take an "add_number" argument. */
148 /* ??? For completeness' sake one might want expr_build_symbol.
149 It would just return its argument. */
151 /* Build an expression for an unsigned constant.
152 The corresponding one for signed constants is missing because
153 there's currently no need for it. One could add an unsigned_p flag
154 but that seems more clumsy. */
157 expr_build_uconstant (value)
163 e.X_add_number = value;
165 return make_expr_symbol (&e);
168 /* Build an expression for OP s1. */
171 expr_build_unary (op, s1)
180 return make_expr_symbol (&e);
183 /* Build an expression for s1 OP s2. */
186 expr_build_binary (op, s1, s2)
197 return make_expr_symbol (&e);
200 /* Build an expression for the current location ('.'). */
207 current_location (&e);
208 return make_expr_symbol (&e);
211 /* Build any floating-point literal here.
212 Also build any bignum literal here. */
214 /* Seems atof_machine can backscan through generic_bignum and hit whatever
215 happens to be loaded before it in memory. And its way too complicated
216 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
217 and never write into the early words, thus they'll always be zero.
218 I hate Dean's floating-point code. Bleh. */
219 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
220 FLONUM_TYPE generic_floating_point_number =
222 &generic_bignum[6], /* low. (JF: Was 0) */
223 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
229 /* If nonzero, we've been asked to assemble nan, +inf or -inf. */
230 int generic_floating_point_magic;
233 floating_constant (expressionP)
234 expressionS *expressionP;
236 /* input_line_pointer -> floating-point constant. */
239 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
240 &generic_floating_point_number);
244 if (error_code == ERROR_EXPONENT_OVERFLOW)
246 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
250 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code);
253 expressionP->X_op = O_big;
254 /* input_line_pointer -> just after constant, which may point to
256 expressionP->X_add_number = -1;
260 generic_bignum_to_int32 ()
263 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
264 | (generic_bignum[0] & LITTLENUM_MASK);
265 number &= 0xffffffff;
271 generic_bignum_to_int64 ()
274 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
275 << LITTLENUM_NUMBER_OF_BITS)
276 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
277 << LITTLENUM_NUMBER_OF_BITS)
278 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
279 << LITTLENUM_NUMBER_OF_BITS)
280 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
286 integer_constant (radix, expressionP)
288 expressionS *expressionP;
290 char *start; /* Start of number. */
293 valueT number; /* Offset or (absolute) value. */
294 short int digit; /* Value of next digit in current radix. */
295 short int maxdig = 0; /* Highest permitted digit value. */
296 int too_many_digits = 0; /* If we see >= this number of. */
297 char *name; /* Points to name of symbol. */
298 symbolS *symbolP; /* Points to symbol. */
300 int small; /* True if fits in 32 bits. */
302 /* May be bignum, or may fit in 32 bits. */
303 /* Most numbers fit into 32 bits, and we want this case to be fast.
304 so we pretend it will fit into 32 bits. If, after making up a 32
305 bit number, we realise that we have scanned more digits than
306 comfortably fit into 32 bits, we re-scan the digits coding them
307 into a bignum. For decimal and octal numbers we are
308 conservative: Some numbers may be assumed bignums when in fact
309 they do fit into 32 bits. Numbers of any radix can have excess
310 leading zeros: We strive to recognise this and cast them back
311 into 32 bits. We must check that the bignum really is more than
312 32 bits, and change it back to a 32-bit number if it fits. The
313 number we are looking for is expected to be positive, but if it
314 fits into 32 bits as an unsigned number, we let it be a 32-bit
315 number. The cavalier approach is for speed in ordinary cases. */
316 /* This has been extended for 64 bits. We blindly assume that if
317 you're compiling in 64-bit mode, the target is a 64-bit machine.
318 This should be cleaned up. */
322 #else /* includes non-bfd case, mostly */
326 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
330 /* In MRI mode, the number may have a suffix indicating the
331 radix. For that matter, it might actually be a floating
333 for (suffix = input_line_pointer;
334 isalnum ((unsigned char) *suffix);
337 if (*suffix == 'e' || *suffix == 'E')
341 if (suffix == input_line_pointer)
349 if (islower ((unsigned char) c))
355 else if (c == 'O' || c == 'Q')
359 else if (suffix[1] == '.' || c == 'E' || flt)
361 floating_constant (expressionP);
376 too_many_digits = valuesize + 1;
380 too_many_digits = (valuesize + 2) / 3 + 1;
384 too_many_digits = (valuesize + 3) / 4 + 1;
388 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
391 start = input_line_pointer;
392 c = *input_line_pointer++;
394 (digit = hex_value (c)) < maxdig;
395 c = *input_line_pointer++)
397 number = number * radix + digit;
399 /* c contains character after number. */
400 /* input_line_pointer->char after c. */
401 small = (input_line_pointer - start - 1) < too_many_digits;
403 if (radix == 16 && c == '_')
405 /* This is literal of the form 0x333_0_12345678_1.
406 This example is equivalent to 0x00000333000000001234567800000001. */
408 int num_little_digits = 0;
410 input_line_pointer = start; /* -> 1st digit. */
412 know (LITTLENUM_NUMBER_OF_BITS == 16);
414 for (c = '_'; c == '_'; num_little_digits += 2)
417 /* Convert one 64-bit word. */
420 for (c = *input_line_pointer++;
421 (digit = hex_value (c)) < maxdig;
422 c = *(input_line_pointer++))
424 number = number * radix + digit;
428 /* Check for 8 digit per word max. */
430 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
432 /* Add this chunk to the bignum.
433 Shift things down 2 little digits. */
434 know (LITTLENUM_NUMBER_OF_BITS == 16);
435 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
438 generic_bignum[i] = generic_bignum[i - 2];
440 /* Add the new digits as the least significant new ones. */
441 generic_bignum[0] = number & 0xffffffff;
442 generic_bignum[1] = number >> 16;
445 /* Again, c is char after number, input_line_pointer->after c. */
447 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
448 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
450 assert (num_little_digits >= 4);
452 if (num_little_digits != 8)
453 as_bad (_("A bignum with underscores must have exactly 4 words."));
455 /* We might have some leading zeros. These can be trimmed to give
456 us a change to fit this constant into a small number. */
457 while (generic_bignum[num_little_digits - 1] == 0
458 && num_little_digits > 1)
461 if (num_little_digits <= 2)
463 /* will fit into 32 bits. */
464 number = generic_bignum_to_int32 ();
468 else if (num_little_digits <= 4)
470 /* Will fit into 64 bits. */
471 number = generic_bignum_to_int64 ();
479 /* Number of littlenums in the bignum. */
480 number = num_little_digits;
485 /* We saw a lot of digits. manufacture a bignum the hard way. */
486 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
487 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
490 leader = generic_bignum;
491 generic_bignum[0] = 0;
492 generic_bignum[1] = 0;
493 generic_bignum[2] = 0;
494 generic_bignum[3] = 0;
495 input_line_pointer = start; /* -> 1st digit. */
496 c = *input_line_pointer++;
497 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
499 for (pointer = generic_bignum; pointer <= leader; pointer++)
503 work = carry + radix * *pointer;
504 *pointer = work & LITTLENUM_MASK;
505 carry = work >> LITTLENUM_NUMBER_OF_BITS;
509 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
511 /* Room to grow a longer bignum. */
516 /* Again, c is char after number. */
517 /* input_line_pointer -> after c. */
518 know (LITTLENUM_NUMBER_OF_BITS == 16);
519 if (leader < generic_bignum + 2)
521 /* Will fit into 32 bits. */
522 number = generic_bignum_to_int32 ();
526 else if (leader < generic_bignum + 4)
528 /* Will fit into 64 bits. */
529 number = generic_bignum_to_int64 ();
535 /* Number of littlenums in the bignum. */
536 number = leader - generic_bignum + 1;
540 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
542 && input_line_pointer - 1 == suffix)
543 c = *input_line_pointer++;
547 /* Here with number, in correct radix. c is the next char.
548 Note that unlike un*x, we allow "011f" "0x9f" to both mean
549 the same as the (conventional) "9f".
550 This is simply easier than checking for strict canonical
553 if (LOCAL_LABELS_FB && c == 'b')
555 /* Backward ref to local label.
556 Because it is backward, expect it to be defined. */
557 /* Construct a local label. */
558 name = fb_label_name ((int) number, 0);
560 /* Seen before, or symbol is defined: OK. */
561 symbolP = symbol_find (name);
562 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
564 /* Local labels are never absolute. Don't waste time
565 checking absoluteness. */
566 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
568 expressionP->X_op = O_symbol;
569 expressionP->X_add_symbol = symbolP;
573 /* Either not seen or not defined. */
574 /* @@ Should print out the original string instead of
575 the parsed number. */
576 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
578 expressionP->X_op = O_constant;
581 expressionP->X_add_number = 0;
583 else if (LOCAL_LABELS_FB && c == 'f')
585 /* Forward reference. Expect symbol to be undefined or
586 unknown. undefined: seen it before. unknown: never seen
589 Construct a local label name, then an undefined symbol.
590 Don't create a xseg frag for it: caller may do that.
591 Just return it as never seen before. */
592 name = fb_label_name ((int) number, 1);
593 symbolP = symbol_find_or_make (name);
594 /* We have no need to check symbol properties. */
595 #ifndef many_segments
596 /* Since "know" puts its arg into a "string", we
597 can't have newlines in the argument. */
598 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
600 expressionP->X_op = O_symbol;
601 expressionP->X_add_symbol = symbolP;
602 expressionP->X_add_number = 0;
604 else if (LOCAL_LABELS_DOLLAR && c == '$')
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
611 if (dollar_label_defined ((long) number))
613 name = dollar_label_name ((long) number, 0);
614 symbolP = symbol_find (name);
615 know (symbolP != NULL);
619 name = dollar_label_name ((long) number, 1);
620 symbolP = symbol_find_or_make (name);
623 expressionP->X_op = O_symbol;
624 expressionP->X_add_symbol = symbolP;
625 expressionP->X_add_number = 0;
629 expressionP->X_op = O_constant;
630 #ifdef TARGET_WORD_SIZE
631 /* Sign extend NUMBER. */
632 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
634 expressionP->X_add_number = number;
635 input_line_pointer--; /* Restore following character. */
636 } /* Really just a number. */
640 /* Not a small number. */
641 expressionP->X_op = O_big;
642 expressionP->X_add_number = number; /* Number of littlenums. */
643 input_line_pointer--; /* -> char following number. */
647 /* Parse an MRI multi character constant. */
650 mri_char_constant (expressionP)
651 expressionS *expressionP;
655 if (*input_line_pointer == '\''
656 && input_line_pointer[1] != '\'')
658 expressionP->X_op = O_constant;
659 expressionP->X_add_number = 0;
663 /* In order to get the correct byte ordering, we must build the
664 number in reverse. */
665 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
669 generic_bignum[i] = 0;
670 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
672 if (*input_line_pointer == '\'')
674 if (input_line_pointer[1] != '\'')
676 ++input_line_pointer;
678 generic_bignum[i] <<= 8;
679 generic_bignum[i] += *input_line_pointer;
680 ++input_line_pointer;
683 if (i < SIZE_OF_LARGE_NUMBER - 1)
685 /* If there is more than one littlenum, left justify the
686 last one to make it match the earlier ones. If there is
687 only one, we can just use the value directly. */
688 for (; j < CHARS_PER_LITTLENUM; j++)
689 generic_bignum[i] <<= 8;
692 if (*input_line_pointer == '\''
693 && input_line_pointer[1] != '\'')
699 as_bad (_("Character constant too large"));
708 c = SIZE_OF_LARGE_NUMBER - i;
709 for (j = 0; j < c; j++)
710 generic_bignum[j] = generic_bignum[i + j];
714 know (LITTLENUM_NUMBER_OF_BITS == 16);
717 expressionP->X_op = O_big;
718 expressionP->X_add_number = i;
722 expressionP->X_op = O_constant;
724 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
726 expressionP->X_add_number =
727 (((generic_bignum[1] & LITTLENUM_MASK)
728 << LITTLENUM_NUMBER_OF_BITS)
729 | (generic_bignum[0] & LITTLENUM_MASK));
732 /* Skip the final closing quote. */
733 ++input_line_pointer;
736 /* Return an expression representing the current location. This
737 handles the magic symbol `.'. */
740 current_location (expressionp)
741 expressionS *expressionp;
743 if (now_seg == absolute_section)
745 expressionp->X_op = O_constant;
746 expressionp->X_add_number = abs_section_offset;
752 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
753 (valueT) frag_now_fix (),
755 expressionp->X_op = O_symbol;
756 expressionp->X_add_symbol = symbolp;
757 expressionp->X_add_number = 0;
761 /* In: Input_line_pointer points to 1st char of operand, which may
765 The operand may have been empty: in this case X_op == O_absent.
766 Input_line_pointer->(next non-blank) char after operand. */
769 operand (expressionP)
770 expressionS *expressionP;
773 symbolS *symbolP; /* Points to symbol. */
774 char *name; /* Points to name of symbol. */
777 /* All integers are regarded as unsigned unless they are negated.
778 This is because the only thing which cares whether a number is
779 unsigned is the code in emit_expr which extends constants into
780 bignums. It should only sign extend negative numbers, so that
781 something like ``.quad 0x80000000'' is not sign extended even
782 though it appears negative if valueT is 32 bits. */
783 expressionP->X_unsigned = 1;
785 /* Digits, assume it is a bignum. */
787 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
788 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
790 if (is_end_of_line[(unsigned char) c])
804 input_line_pointer--;
806 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
811 #ifdef LITERAL_PREFIXDOLLAR_HEX
813 integer_constant (16, expressionP);
817 #ifdef LITERAL_PREFIXPERCENT_BIN
819 integer_constant (2, expressionP);
824 /* Non-decimal radix. */
826 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
830 /* Check for a hex constant. */
831 for (s = input_line_pointer; hex_p (*s); s++)
833 if (*s == 'h' || *s == 'H')
835 --input_line_pointer;
836 integer_constant (0, expressionP);
840 c = *input_line_pointer;
849 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
851 integer_constant (0, expressionP);
857 if (c && strchr (FLT_CHARS, c))
859 input_line_pointer++;
860 floating_constant (expressionP);
861 expressionP->X_add_number =
862 - (isupper ((unsigned char) c) ? tolower (c) : c);
866 /* The string was only zero. */
867 expressionP->X_op = O_constant;
868 expressionP->X_add_number = 0;
877 input_line_pointer++;
878 integer_constant (16, expressionP);
882 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
884 /* This code used to check for '+' and '-' here, and, in
885 some conditions, fall through to call
886 integer_constant. However, that didn't make sense,
887 as integer_constant only accepts digits. */
888 /* Some of our code elsewhere does permit digits greater
889 than the expected base; for consistency, do the same
891 if (input_line_pointer[1] < '0'
892 || input_line_pointer[1] > '9')
894 /* Parse this as a back reference to label 0. */
895 input_line_pointer--;
896 integer_constant (10, expressionP);
899 /* Otherwise, parse this as a binary number. */
903 input_line_pointer++;
904 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
906 integer_constant (2, expressionP);
917 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
925 /* If it says "0f" and it could possibly be a floating point
926 number, make it one. Otherwise, make it a local label,
927 and try to deal with parsing the rest later. */
928 if (!input_line_pointer[1]
929 || (is_end_of_line[0xff & input_line_pointer[1]])
930 || strchr (FLT_CHARS, 'f') == NULL)
933 char *cp = input_line_pointer + 1;
934 int r = atof_generic (&cp, ".", EXP_CHARS,
935 &generic_floating_point_number);
939 case ERROR_EXPONENT_OVERFLOW:
940 if (*cp == 'f' || *cp == 'b')
941 /* Looks like a difference expression. */
943 else if (cp == input_line_pointer + 1)
944 /* No characters has been accepted -- looks like
950 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
955 /* Okay, now we've sorted it out. We resume at one of these
956 two labels, depending on what we've decided we're probably
959 input_line_pointer--;
960 integer_constant (10, expressionP);
970 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
972 integer_constant (0, expressionP);
982 input_line_pointer++;
983 floating_constant (expressionP);
984 expressionP->X_add_number =
985 - (isupper ((unsigned char) c) ? tolower (c) : c);
989 if (LOCAL_LABELS_DOLLAR)
991 integer_constant (10, expressionP);
1001 #ifndef NEED_INDEX_OPERATOR
1004 /* Didn't begin with digit & not a name. */
1005 segment = expression (expressionP);
1006 /* expression () will pass trailing whitespace. */
1007 if ((c == '(' && *input_line_pointer != ')')
1008 || (c == '[' && *input_line_pointer != ']'))
1010 #ifdef RELAX_PAREN_GROUPING
1013 as_bad (_("Missing '%c' assumed"), c == '(' ? ')' : ']');
1016 input_line_pointer++;
1018 /* Here with input_line_pointer -> char after "(...)". */
1023 if (! flag_m68k_mri || *input_line_pointer != '\'')
1025 as_bad (_("EBCDIC constants are not supported"));
1028 if (! flag_m68k_mri || *input_line_pointer != '\'')
1030 ++input_line_pointer;
1034 if (! flag_m68k_mri)
1036 /* Warning: to conform to other people's assemblers NO
1037 ESCAPEMENT is permitted for a single quote. The next
1038 character, parity errors and all, is taken as the value
1039 of the operand. VERY KINKY. */
1040 expressionP->X_op = O_constant;
1041 expressionP->X_add_number = *input_line_pointer++;
1045 mri_char_constant (expressionP);
1049 (void) operand (expressionP);
1054 /* Double quote is the bitwise not operator in MRI mode. */
1055 if (! flag_m68k_mri)
1060 /* '~' is permitted to start a label on the Delta. */
1061 if (is_name_beginner (c))
1066 operand (expressionP);
1067 if (expressionP->X_op == O_constant)
1069 /* input_line_pointer -> char after operand. */
1072 expressionP->X_add_number = - expressionP->X_add_number;
1073 /* Notice: '-' may overflow: no warning is given.
1074 This is compatible with other people's
1075 assemblers. Sigh. */
1076 expressionP->X_unsigned = 0;
1078 else if (c == '~' || c == '"')
1079 expressionP->X_add_number = ~ expressionP->X_add_number;
1081 expressionP->X_add_number = ! expressionP->X_add_number;
1083 else if (expressionP->X_op != O_illegal
1084 && expressionP->X_op != O_absent)
1086 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1088 expressionP->X_op = O_uminus;
1089 else if (c == '~' || c == '"')
1090 expressionP->X_op = O_bit_not;
1092 expressionP->X_op = O_logical_not;
1093 expressionP->X_add_number = 0;
1096 as_warn (_("Unary operator %c ignored because bad operand follows"),
1101 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1103 /* '$' is the program counter when in MRI mode, or when
1104 DOLLAR_DOT is defined. */
1106 if (! flag_m68k_mri)
1109 if (flag_m68k_mri && hex_p (*input_line_pointer))
1111 /* In MRI mode, '$' is also used as the prefix for a
1112 hexadecimal constant. */
1113 integer_constant (16, expressionP);
1117 if (is_part_of_name (*input_line_pointer))
1120 current_location (expressionP);
1125 if (!is_part_of_name (*input_line_pointer))
1127 current_location (expressionP);
1130 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1131 && ! is_part_of_name (input_line_pointer[8]))
1132 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1133 && ! is_part_of_name (input_line_pointer[7])))
1137 start = (input_line_pointer[1] == 't'
1138 || input_line_pointer[1] == 'T');
1139 input_line_pointer += start ? 8 : 7;
1141 if (*input_line_pointer != '(')
1142 as_bad (_("syntax error in .startof. or .sizeof."));
1147 ++input_line_pointer;
1149 name = input_line_pointer;
1150 c = get_symbol_end ();
1152 buf = (char *) xmalloc (strlen (name) + 10);
1154 sprintf (buf, ".startof.%s", name);
1156 sprintf (buf, ".sizeof.%s", name);
1157 symbolP = symbol_make (buf);
1160 expressionP->X_op = O_symbol;
1161 expressionP->X_add_symbol = symbolP;
1162 expressionP->X_add_number = 0;
1164 *input_line_pointer = c;
1166 if (*input_line_pointer != ')')
1167 as_bad (_("syntax error in .startof. or .sizeof."));
1169 ++input_line_pointer;
1180 /* Can't imagine any other kind of operand. */
1181 expressionP->X_op = O_absent;
1182 input_line_pointer--;
1187 if (! flag_m68k_mri)
1189 integer_constant (2, expressionP);
1193 if (! flag_m68k_mri)
1195 integer_constant (8, expressionP);
1199 if (! flag_m68k_mri)
1202 /* In MRI mode, this is a floating point constant represented
1203 using hexadecimal digits. */
1205 ++input_line_pointer;
1206 integer_constant (16, expressionP);
1210 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1213 current_location (expressionP);
1221 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
1223 /* Identifier begins here.
1224 This is kludged for speed, so code is repeated. */
1226 name = --input_line_pointer;
1227 c = get_symbol_end ();
1229 #ifdef md_parse_name
1230 /* This is a hook for the backend to parse certain names
1231 specially in certain contexts. If a name always has a
1232 specific value, it can often be handled by simply
1233 entering it in the symbol table. */
1234 if (md_parse_name (name, expressionP))
1236 *input_line_pointer = c;
1242 /* The MRI i960 assembler permits
1244 FIXME: This should use md_parse_name. */
1246 && (strcasecmp (name, "sizeof") == 0
1247 || strcasecmp (name, "startof") == 0))
1252 start = (name[1] == 't'
1255 *input_line_pointer = c;
1258 name = input_line_pointer;
1259 c = get_symbol_end ();
1261 buf = (char *) xmalloc (strlen (name) + 10);
1263 sprintf (buf, ".startof.%s", name);
1265 sprintf (buf, ".sizeof.%s", name);
1266 symbolP = symbol_make (buf);
1269 expressionP->X_op = O_symbol;
1270 expressionP->X_add_symbol = symbolP;
1271 expressionP->X_add_number = 0;
1273 *input_line_pointer = c;
1280 symbolP = symbol_find_or_make (name);
1282 /* If we have an absolute symbol or a reg, then we know its
1284 segment = S_GET_SEGMENT (symbolP);
1285 if (segment == absolute_section)
1287 expressionP->X_op = O_constant;
1288 expressionP->X_add_number = S_GET_VALUE (symbolP);
1290 else if (segment == reg_section)
1292 expressionP->X_op = O_register;
1293 expressionP->X_add_number = S_GET_VALUE (symbolP);
1297 expressionP->X_op = O_symbol;
1298 expressionP->X_add_symbol = symbolP;
1299 expressionP->X_add_number = 0;
1301 *input_line_pointer = c;
1305 /* Let the target try to parse it. Success is indicated by changing
1306 the X_op field to something other than O_absent and pointing
1307 input_line_pointer passed the expression. If it can't parse the
1308 expression, X_op and input_line_pointer should be unchanged. */
1309 expressionP->X_op = O_absent;
1310 --input_line_pointer;
1311 md_operand (expressionP);
1312 if (expressionP->X_op == O_absent)
1314 ++input_line_pointer;
1315 as_bad (_("Bad expression"));
1316 expressionP->X_op = O_constant;
1317 expressionP->X_add_number = 0;
1323 /* It is more 'efficient' to clean up the expressionS when they are
1324 created. Doing it here saves lines of code. */
1325 clean_up_expression (expressionP);
1326 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1327 know (*input_line_pointer != ' ');
1329 /* The PA port needs this information. */
1330 if (expressionP->X_add_symbol)
1331 symbol_mark_used (expressionP->X_add_symbol);
1333 switch (expressionP->X_op)
1336 return absolute_section;
1338 return S_GET_SEGMENT (expressionP->X_add_symbol);
1344 /* Internal. Simplify a struct expression for use by expr (). */
1346 /* In: address of a expressionS.
1347 The X_op field of the expressionS may only take certain values.
1348 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1350 Out: expressionS may have been modified:
1351 'foo-foo' symbol references cancelled to 0, which changes X_op
1352 from O_subtract to O_constant.
1353 Unused fields zeroed to help expr (). */
1356 clean_up_expression (expressionP)
1357 expressionS *expressionP;
1359 switch (expressionP->X_op)
1363 expressionP->X_add_number = 0;
1368 expressionP->X_add_symbol = NULL;
1373 expressionP->X_op_symbol = NULL;
1376 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1377 || ((symbol_get_frag (expressionP->X_op_symbol)
1378 == symbol_get_frag (expressionP->X_add_symbol))
1379 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1380 && (S_GET_VALUE (expressionP->X_op_symbol)
1381 == S_GET_VALUE (expressionP->X_add_symbol))))
1383 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1384 - S_GET_VALUE (expressionP->X_op_symbol));
1386 expressionP->X_op = O_constant;
1387 expressionP->X_add_symbol = NULL;
1388 expressionP->X_op_symbol = NULL;
1389 expressionP->X_add_number += diff;
1397 /* Expression parser. */
1399 /* We allow an empty expression, and just assume (absolute,0) silently.
1400 Unary operators and parenthetical expressions are treated as operands.
1401 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1403 We used to do a aho/ullman shift-reduce parser, but the logic got so
1404 warped that I flushed it and wrote a recursive-descent parser instead.
1405 Now things are stable, would anybody like to write a fast parser?
1406 Most expressions are either register (which does not even reach here)
1407 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1408 So I guess it doesn't really matter how inefficient more complex expressions
1411 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1412 Also, we have consumed any leading or trailing spaces (operand does that)
1413 and done all intervening operators.
1415 This returns the segment of the result, which will be
1416 absolute_section or the segment of a symbol. */
1419 #define __ O_illegal
1421 static const operatorT op_encoding[256] =
1422 { /* Maps ASCII -> operators. */
1424 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1425 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1427 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1428 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1429 __, __, __, __, __, __, __, __,
1430 __, __, __, __, O_lt, __, O_gt, __,
1431 __, __, __, __, __, __, __, __,
1432 __, __, __, __, __, __, __, __,
1433 __, __, __, __, __, __, __, __,
1435 #ifdef NEED_INDEX_OPERATOR
1440 __, __, O_bit_exclusive_or, __,
1441 __, __, __, __, __, __, __, __,
1442 __, __, __, __, __, __, __, __,
1443 __, __, __, __, __, __, __, __,
1444 __, __, __, __, O_bit_inclusive_or, __, __, __,
1446 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1447 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1448 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1449 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1450 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1451 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1452 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1453 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1457 0 operand, (expression)
1462 5 used for * / % in MRI mode
1467 static operator_rankT op_rank[] =
1473 0, /* O_symbol_rva */
1478 9, /* O_logical_not */
1482 8, /* O_left_shift */
1483 8, /* O_right_shift */
1484 7, /* O_bit_inclusive_or */
1485 7, /* O_bit_or_not */
1486 7, /* O_bit_exclusive_or */
1496 3, /* O_logical_and */
1497 2, /* O_logical_or */
1517 /* Unfortunately, in MRI mode for the m68k, multiplication and
1518 division have lower precedence than the bit wise operators. This
1519 function sets the operator precedences correctly for the current
1520 mode. Also, MRI uses a different bit_not operator, and this fixes
1523 #define STANDARD_MUL_PRECEDENCE (7)
1524 #define MRI_MUL_PRECEDENCE (5)
1527 expr_set_precedence ()
1531 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1532 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1533 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1537 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1538 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1539 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1543 /* Initialize the expression parser. */
1548 expr_set_precedence ();
1550 /* Verify that X_op field is wide enough. */
1554 assert (e.X_op == O_max);
1558 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1559 Advance INPUT_LINE_POINTER to the last character in the operator
1560 (i.e., don't change it for a single character operator). */
1562 static inline operatorT
1568 c = *input_line_pointer & 0xff;
1570 if (is_end_of_line[c])
1576 return op_encoding[c];
1579 switch (input_line_pointer[1])
1582 return op_encoding[c];
1593 ++input_line_pointer;
1597 if (input_line_pointer[1] != '=')
1598 return op_encoding[c];
1600 ++input_line_pointer;
1604 switch (input_line_pointer[1])
1607 return op_encoding[c];
1609 ret = O_right_shift;
1615 ++input_line_pointer;
1619 /* We accept !! as equivalent to ^ for MRI compatibility. */
1620 if (input_line_pointer[1] != '!')
1623 return O_bit_inclusive_or;
1624 return op_encoding[c];
1626 ++input_line_pointer;
1627 return O_bit_exclusive_or;
1630 if (input_line_pointer[1] != '|')
1631 return op_encoding[c];
1633 ++input_line_pointer;
1634 return O_logical_or;
1637 if (input_line_pointer[1] != '&')
1638 return op_encoding[c];
1640 ++input_line_pointer;
1641 return O_logical_and;
1647 /* Parse an expression. */
1650 expr (rankarg, resultP)
1651 int rankarg; /* Larger # is higher rank. */
1652 expressionS *resultP; /* Deliver result here. */
1654 operator_rankT rank = (operator_rankT) rankarg;
1662 retval = operand (resultP);
1664 /* operand () gobbles spaces. */
1665 know (*input_line_pointer != ' ');
1667 op_left = operator ();
1668 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1672 input_line_pointer++; /* -> after 1st character of operator. */
1674 rightseg = expr (op_rank[(int) op_left], &right);
1675 if (right.X_op == O_absent)
1677 as_warn (_("missing operand; zero assumed"));
1678 right.X_op = O_constant;
1679 right.X_add_number = 0;
1680 right.X_add_symbol = NULL;
1681 right.X_op_symbol = NULL;
1684 know (*input_line_pointer != ' ');
1686 if (op_left == O_index)
1688 if (*input_line_pointer != ']')
1689 as_bad ("missing right bracket");
1692 ++input_line_pointer;
1697 if (retval == undefined_section)
1699 if (SEG_NORMAL (rightseg))
1702 else if (! SEG_NORMAL (retval))
1704 else if (SEG_NORMAL (rightseg)
1705 && retval != rightseg
1707 && op_left != O_subtract
1710 as_bad (_("operation combines symbols in different segments"));
1712 op_right = operator ();
1714 know (op_right == O_illegal
1715 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1716 know ((int) op_left >= (int) O_multiply
1717 && (int) op_left <= (int) O_logical_or);
1719 /* input_line_pointer->after right-hand quantity. */
1720 /* left-hand quantity in resultP. */
1721 /* right-hand quantity in right. */
1722 /* operator in op_left. */
1724 if (resultP->X_op == O_big)
1726 if (resultP->X_add_number > 0)
1727 as_warn (_("left operand is a bignum; integer 0 assumed"));
1729 as_warn (_("left operand is a float; integer 0 assumed"));
1730 resultP->X_op = O_constant;
1731 resultP->X_add_number = 0;
1732 resultP->X_add_symbol = NULL;
1733 resultP->X_op_symbol = NULL;
1735 if (right.X_op == O_big)
1737 if (right.X_add_number > 0)
1738 as_warn (_("right operand is a bignum; integer 0 assumed"));
1740 as_warn (_("right operand is a float; integer 0 assumed"));
1741 right.X_op = O_constant;
1742 right.X_add_number = 0;
1743 right.X_add_symbol = NULL;
1744 right.X_op_symbol = NULL;
1747 /* Optimize common cases. */
1748 #ifdef md_optimize_expr
1749 if (md_optimize_expr (resultP, op_left, &right))
1756 if (op_left == O_add && right.X_op == O_constant)
1759 resultP->X_add_number += right.X_add_number;
1761 /* This case comes up in PIC code. */
1762 else if (op_left == O_subtract
1763 && right.X_op == O_symbol
1764 && resultP->X_op == O_symbol
1765 && (symbol_get_frag (right.X_add_symbol)
1766 == symbol_get_frag (resultP->X_add_symbol))
1767 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1770 resultP->X_add_number -= right.X_add_number;
1771 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1772 - S_GET_VALUE (right.X_add_symbol));
1773 resultP->X_op = O_constant;
1774 resultP->X_add_symbol = 0;
1776 else if (op_left == O_subtract && right.X_op == O_constant)
1779 resultP->X_add_number -= right.X_add_number;
1781 else if (op_left == O_add && resultP->X_op == O_constant)
1784 resultP->X_op = right.X_op;
1785 resultP->X_add_symbol = right.X_add_symbol;
1786 resultP->X_op_symbol = right.X_op_symbol;
1787 resultP->X_add_number += right.X_add_number;
1790 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1792 /* Constant OP constant. */
1793 offsetT v = right.X_add_number;
1794 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1796 as_warn (_("division by zero"));
1802 case O_multiply: resultP->X_add_number *= v; break;
1803 case O_divide: resultP->X_add_number /= v; break;
1804 case O_modulus: resultP->X_add_number %= v; break;
1805 case O_left_shift: resultP->X_add_number <<= v; break;
1807 /* We always use unsigned shifts, to avoid relying on
1808 characteristics of the compiler used to compile gas. */
1809 resultP->X_add_number =
1810 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1812 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1813 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1814 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1815 case O_bit_and: resultP->X_add_number &= v; break;
1816 case O_add: resultP->X_add_number += v; break;
1817 case O_subtract: resultP->X_add_number -= v; break;
1819 resultP->X_add_number =
1820 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1823 resultP->X_add_number =
1824 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1827 resultP->X_add_number =
1828 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1831 resultP->X_add_number =
1832 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1835 resultP->X_add_number =
1836 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1839 resultP->X_add_number =
1840 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1843 resultP->X_add_number = resultP->X_add_number && v;
1846 resultP->X_add_number = resultP->X_add_number || v;
1850 else if (resultP->X_op == O_symbol
1851 && right.X_op == O_symbol
1852 && (op_left == O_add
1853 || op_left == O_subtract
1854 || (resultP->X_add_number == 0
1855 && right.X_add_number == 0)))
1857 /* Symbol OP symbol. */
1858 resultP->X_op = op_left;
1859 resultP->X_op_symbol = right.X_add_symbol;
1860 if (op_left == O_add)
1861 resultP->X_add_number += right.X_add_number;
1862 else if (op_left == O_subtract)
1863 resultP->X_add_number -= right.X_add_number;
1867 /* The general case. */
1868 resultP->X_add_symbol = make_expr_symbol (resultP);
1869 resultP->X_op_symbol = make_expr_symbol (&right);
1870 resultP->X_op = op_left;
1871 resultP->X_add_number = 0;
1872 resultP->X_unsigned = 1;
1876 } /* While next operator is >= this rank. */
1878 /* The PA port needs this information. */
1879 if (resultP->X_add_symbol)
1880 symbol_mark_used (resultP->X_add_symbol);
1882 return resultP->X_op == O_constant ? absolute_section : retval;
1885 /* This lives here because it belongs equally in expr.c & read.c.
1886 expr.c is just a branch office read.c anyway, and putting it
1887 here lessens the crowd at read.c.
1889 Assume input_line_pointer is at start of symbol name.
1890 Advance input_line_pointer past symbol name.
1891 Turn that character into a '\0', returning its former value.
1892 This allows a string compare (RMS wants symbol names to be strings)
1894 There will always be a char following symbol name, because all good
1895 lines end in end-of-line. */
1902 /* We accept \001 in a name in case this is being called with a
1903 constructed string. */
1904 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1906 while (is_part_of_name (c = *input_line_pointer++)
1909 if (is_name_ender (c))
1910 c = *input_line_pointer++;
1912 *--input_line_pointer = 0;
1917 get_single_number ()
1921 return exp.X_add_number;