1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
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
23 * This is really a branch office of as-read.c. I split it out to clearly
24 * distinguish the world of expressions from the world of statements.
25 * (It also gives smaller files to re-compile.)
26 * Here, "operand"s are of expressions, not instructions.
31 #define min(a, b) ((a) < (b) ? (a) : (b))
36 static void floating_constant PARAMS ((expressionS * expressionP));
37 static valueT generic_bignum_to_int32 PARAMS ((void));
39 static valueT generic_bignum_to_int64 PARAMS ((void));
41 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
42 static void mri_char_constant PARAMS ((expressionS *));
43 static void current_location PARAMS ((expressionS *));
44 static void clean_up_expression PARAMS ((expressionS * expressionP));
45 static segT operand PARAMS ((expressionS *));
46 static operatorT operator PARAMS ((void));
48 extern const char EXP_CHARS[], FLT_CHARS[];
50 /* We keep a mapping of expression symbols to file positions, so that
51 we can provide better error messages. */
53 struct expr_symbol_line
55 struct expr_symbol_line *next;
61 static struct expr_symbol_line *expr_symbol_lines;
63 /* Build a dummy symbol to hold a complex expression. This is how we
64 build expressions up out of other expressions. The symbol is put
65 into the fake section expr_section. */
68 make_expr_symbol (expressionP)
69 expressionS *expressionP;
74 struct expr_symbol_line *n;
76 if (expressionP->X_op == O_symbol
77 && expressionP->X_add_number == 0)
78 return expressionP->X_add_symbol;
80 if (expressionP->X_op == O_big)
82 /* This won't work, because the actual value is stored in
83 generic_floating_point_number or generic_bignum, and we are
84 going to lose it if we haven't already. */
85 if (expressionP->X_add_number > 0)
86 as_bad (_("bignum invalid; zero assumed"));
88 as_bad (_("floating point number invalid; zero assumed"));
89 zero.X_op = O_constant;
90 zero.X_add_number = 0;
92 clean_up_expression (&zero);
96 fake = FAKE_LABEL_NAME;
98 /* Putting constant symbols in absolute_section rather than
99 expr_section is convenient for the old a.out code, for which
100 S_GET_SEGMENT does not always retrieve the value put in by
102 symbolP = symbol_create (fake,
103 (expressionP->X_op == O_constant
106 0, &zero_address_frag);
107 symbolP->sy_value = *expressionP;
109 if (expressionP->X_op == O_constant)
110 resolve_symbol_value (symbolP, 1);
112 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
114 as_where (&n->file, &n->line);
115 n->next = expr_symbol_lines;
116 expr_symbol_lines = n;
121 /* Return the file and line number for an expr symbol. Return
122 non-zero if something was found, 0 if no information is known for
126 expr_symbol_where (sym, pfile, pline)
131 register struct expr_symbol_line *l;
133 for (l = expr_symbol_lines; l != NULL; l = l->next)
146 /* Utilities for building expressions.
147 Since complex expressions are recorded as symbols for use in other
148 expressions these return a symbolS * and not an expressionS *.
149 These explicitly do not take an "add_number" argument. */
150 /* ??? For completeness' sake one might want expr_build_symbol.
151 It would just return its argument. */
153 /* Build an expression for an unsigned constant.
154 The corresponding one for signed constants is missing because
155 there's currently no need for it. One could add an unsigned_p flag
156 but that seems more clumsy. */
159 expr_build_uconstant (value)
165 e.X_add_number = value;
167 return make_expr_symbol (&e);
170 /* Build an expression for OP s1. */
173 expr_build_unary (op, s1)
182 return make_expr_symbol (&e);
185 /* Build an expression for s1 OP s2. */
188 expr_build_binary (op, s1, s2)
199 return make_expr_symbol (&e);
202 /* Build an expression for the current location ('.'). */
209 current_location (&e);
210 return make_expr_symbol (&e);
214 * Build any floating-point literal here.
215 * Also build any bignum literal here.
218 /* Seems atof_machine can backscan through generic_bignum and hit whatever
219 happens to be loaded before it in memory. And its way too complicated
220 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
221 and never write into the early words, thus they'll always be zero.
222 I hate Dean's floating-point code. Bleh. */
223 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
224 FLONUM_TYPE generic_floating_point_number =
226 &generic_bignum[6], /* low (JF: Was 0) */
227 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
232 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
233 int generic_floating_point_magic;
236 floating_constant (expressionP)
237 expressionS *expressionP;
239 /* input_line_pointer->*/
240 /* floating-point constant. */
243 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
244 &generic_floating_point_number);
248 if (error_code == ERROR_EXPONENT_OVERFLOW)
250 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
254 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code);
257 expressionP->X_op = O_big;
258 /* input_line_pointer->just after constant, */
259 /* which may point to whitespace. */
260 expressionP->X_add_number = -1;
264 generic_bignum_to_int32 ()
267 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
268 | (generic_bignum[0] & LITTLENUM_MASK);
269 number &= 0xffffffff;
275 generic_bignum_to_int64 ()
278 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
279 << LITTLENUM_NUMBER_OF_BITS)
280 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
281 << LITTLENUM_NUMBER_OF_BITS)
282 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
283 << LITTLENUM_NUMBER_OF_BITS)
284 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
290 integer_constant (radix, expressionP)
292 expressionS *expressionP;
294 char *start; /* start of number. */
297 valueT number; /* offset or (absolute) value */
298 short int digit; /* value of next digit in current radix */
299 short int maxdig = 0;/* highest permitted digit value. */
300 int too_many_digits = 0; /* if we see >= this number of */
301 char *name; /* points to name of symbol */
302 symbolS *symbolP; /* points to symbol */
304 int small; /* true if fits in 32 bits. */
306 /* May be bignum, or may fit in 32 bits. */
307 /* Most numbers fit into 32 bits, and we want this case to be fast.
308 so we pretend it will fit into 32 bits. If, after making up a 32
309 bit number, we realise that we have scanned more digits than
310 comfortably fit into 32 bits, we re-scan the digits coding them
311 into a bignum. For decimal and octal numbers we are
312 conservative: Some numbers may be assumed bignums when in fact
313 they do fit into 32 bits. Numbers of any radix can have excess
314 leading zeros: We strive to recognise this and cast them back
315 into 32 bits. We must check that the bignum really is more than
316 32 bits, and change it back to a 32-bit number if it fits. The
317 number we are looking for is expected to be positive, but if it
318 fits into 32 bits as an unsigned number, we let it be a 32-bit
319 number. The cavalier approach is for speed in ordinary cases. */
320 /* This has been extended for 64 bits. We blindly assume that if
321 you're compiling in 64-bit mode, the target is a 64-bit machine.
322 This should be cleaned up. */
326 #else /* includes non-bfd case, mostly */
330 if (flag_m68k_mri && radix == 0)
334 /* In MRI mode, the number may have a suffix indicating the
335 radix. For that matter, it might actually be a floating
337 for (suffix = input_line_pointer;
338 isalnum ((unsigned char) *suffix);
341 if (*suffix == 'e' || *suffix == 'E')
345 if (suffix == input_line_pointer)
353 if (islower ((unsigned char) c))
359 else if (c == 'O' || c == 'Q')
363 else if (suffix[1] == '.' || c == 'E' || flt)
365 floating_constant (expressionP);
380 too_many_digits = valuesize + 1;
384 too_many_digits = (valuesize + 2) / 3 + 1;
388 too_many_digits = (valuesize + 3) / 4 + 1;
392 too_many_digits = (valuesize + 12) / 4; /* very rough */
395 start = input_line_pointer;
396 c = *input_line_pointer++;
398 (digit = hex_value (c)) < maxdig;
399 c = *input_line_pointer++)
401 number = number * radix + digit;
403 /* c contains character after number. */
404 /* input_line_pointer->char after c. */
405 small = (input_line_pointer - start - 1) < too_many_digits;
407 if (radix == 16 && c == '_')
409 /* This is literal of the form 0x333_0_12345678_1.
410 This example is equivalent to 0x00000333000000001234567800000001. */
412 int num_little_digits = 0;
414 input_line_pointer = start; /*->1st digit. */
416 know (LITTLENUM_NUMBER_OF_BITS == 16);
418 for (c = '_'; c == '_'; num_little_digits+=2)
421 /* Convert one 64-bit word. */
424 for (c = *input_line_pointer++;
425 (digit = hex_value (c)) < maxdig;
426 c = *(input_line_pointer++))
428 number = number * radix + digit;
432 /* Check for 8 digit per word max. */
434 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
436 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
437 know (LITTLENUM_NUMBER_OF_BITS == 16);
438 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1); i >= 2; i--)
439 generic_bignum[i] = generic_bignum[i-2];
441 /* Add the new digits as the least significant new ones. */
442 generic_bignum[0] = number & 0xffffffff;
443 generic_bignum[1] = number >> 16;
446 /* Again, c is char after number, input_line_pointer->after c. */
448 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
449 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
451 assert (num_little_digits >= 4);
453 if (num_little_digits != 8)
454 as_bad (_("A bignum with underscores must have exactly 4 words."));
456 /* We might have some leading zeros. These can be trimmed to give
457 * us a change to fit this constant into a small number.
459 while (generic_bignum[num_little_digits-1] == 0 && num_little_digits > 1)
462 if (num_little_digits <= 2)
464 /* will fit into 32 bits. */
465 number = generic_bignum_to_int32 ();
469 else if (num_little_digits <= 4)
471 /* Will fit into 64 bits. */
472 number = generic_bignum_to_int64 ();
479 number = num_little_digits; /* number of littlenums in the bignum. */
485 * we saw a lot of digits. manufacture a bignum the hard way.
487 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
488 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
491 leader = generic_bignum;
492 generic_bignum[0] = 0;
493 generic_bignum[1] = 0;
494 generic_bignum[2] = 0;
495 generic_bignum[3] = 0;
496 input_line_pointer = start; /*->1st digit. */
497 c = *input_line_pointer++;
499 (carry = hex_value (c)) < maxdig;
500 c = *input_line_pointer++)
502 for (pointer = generic_bignum;
508 work = carry + radix * *pointer;
509 *pointer = work & LITTLENUM_MASK;
510 carry = work >> LITTLENUM_NUMBER_OF_BITS;
514 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
516 /* room to grow a longer bignum. */
521 /* again, c is char after number, */
522 /* input_line_pointer->after c. */
523 know (LITTLENUM_NUMBER_OF_BITS == 16);
524 if (leader < generic_bignum + 2)
526 /* will fit into 32 bits. */
527 number = generic_bignum_to_int32 ();
531 else if (leader < generic_bignum + 4)
533 /* Will fit into 64 bits. */
534 number = generic_bignum_to_int64 ();
540 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
544 if (flag_m68k_mri && suffix != NULL && input_line_pointer - 1 == suffix)
545 c = *input_line_pointer++;
550 * here with number, in correct radix. c is the next char.
551 * note that unlike un*x, we allow "011f" "0x9f" to
552 * both mean the same as the (conventional) "9f". this is simply easier
553 * than checking for strict canonical form. syntax sux!
556 if (LOCAL_LABELS_FB && c == 'b')
559 * backward ref to local label.
560 * because it is backward, expect it to be defined.
562 /* Construct a local label. */
563 name = fb_label_name ((int) number, 0);
565 /* seen before, or symbol is defined: ok */
566 symbolP = symbol_find (name);
567 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
569 /* local labels are never absolute. don't waste time
570 checking absoluteness. */
571 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
573 expressionP->X_op = O_symbol;
574 expressionP->X_add_symbol = symbolP;
578 /* either not seen or not defined. */
579 /* @@ Should print out the original string instead of
580 the parsed number. */
581 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
583 expressionP->X_op = O_constant;
586 expressionP->X_add_number = 0;
588 else if (LOCAL_LABELS_FB && c == 'f')
591 * forward reference. expect symbol to be undefined or
592 * unknown. undefined: seen it before. unknown: never seen
594 * construct a local label name, then an undefined symbol.
595 * don't create a xseg frag for it: caller may do that.
596 * just return it as never seen before.
598 name = fb_label_name ((int) number, 1);
599 symbolP = symbol_find_or_make (name);
600 /* we have no need to check symbol properties. */
601 #ifndef many_segments
602 /* since "know" puts its arg into a "string", we
603 can't have newlines in the argument. */
604 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
606 expressionP->X_op = O_symbol;
607 expressionP->X_add_symbol = symbolP;
608 expressionP->X_add_number = 0;
610 else if (LOCAL_LABELS_DOLLAR && c == '$')
612 /* If the dollar label is *currently* defined, then this is just
613 another reference to it. If it is not *currently* defined,
614 then this is a fresh instantiation of that number, so create
617 if (dollar_label_defined ((long) number))
619 name = dollar_label_name ((long) number, 0);
620 symbolP = symbol_find (name);
621 know (symbolP != NULL);
625 name = dollar_label_name ((long) number, 1);
626 symbolP = symbol_find_or_make (name);
629 expressionP->X_op = O_symbol;
630 expressionP->X_add_symbol = symbolP;
631 expressionP->X_add_number = 0;
635 expressionP->X_op = O_constant;
636 #ifdef TARGET_WORD_SIZE
637 /* Sign extend NUMBER. */
638 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
640 expressionP->X_add_number = number;
641 input_line_pointer--; /* restore following character. */
642 } /* really just a number */
646 /* not a small number */
647 expressionP->X_op = O_big;
648 expressionP->X_add_number = number; /* number of littlenums */
649 input_line_pointer--; /*->char following number. */
653 /* Parse an MRI multi character constant. */
656 mri_char_constant (expressionP)
657 expressionS *expressionP;
661 if (*input_line_pointer == '\''
662 && input_line_pointer[1] != '\'')
664 expressionP->X_op = O_constant;
665 expressionP->X_add_number = 0;
669 /* In order to get the correct byte ordering, we must build the
670 number in reverse. */
671 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
675 generic_bignum[i] = 0;
676 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
678 if (*input_line_pointer == '\'')
680 if (input_line_pointer[1] != '\'')
682 ++input_line_pointer;
684 generic_bignum[i] <<= 8;
685 generic_bignum[i] += *input_line_pointer;
686 ++input_line_pointer;
689 if (i < SIZE_OF_LARGE_NUMBER - 1)
691 /* If there is more than one littlenum, left justify the
692 last one to make it match the earlier ones. If there is
693 only one, we can just use the value directly. */
694 for (; j < CHARS_PER_LITTLENUM; j++)
695 generic_bignum[i] <<= 8;
698 if (*input_line_pointer == '\''
699 && input_line_pointer[1] != '\'')
705 as_bad (_("Character constant too large"));
714 c = SIZE_OF_LARGE_NUMBER - i;
715 for (j = 0; j < c; j++)
716 generic_bignum[j] = generic_bignum[i + j];
720 know (LITTLENUM_NUMBER_OF_BITS == 16);
723 expressionP->X_op = O_big;
724 expressionP->X_add_number = i;
728 expressionP->X_op = O_constant;
730 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
732 expressionP->X_add_number =
733 (((generic_bignum[1] & LITTLENUM_MASK)
734 << LITTLENUM_NUMBER_OF_BITS)
735 | (generic_bignum[0] & LITTLENUM_MASK));
738 /* Skip the final closing quote. */
739 ++input_line_pointer;
742 /* Return an expression representing the current location. This
743 handles the magic symbol `.'. */
746 current_location (expressionp)
747 expressionS *expressionp;
749 if (now_seg == absolute_section)
751 expressionp->X_op = O_constant;
752 expressionp->X_add_number = abs_section_offset;
758 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
759 (valueT) frag_now_fix (),
761 expressionp->X_op = O_symbol;
762 expressionp->X_add_symbol = symbolp;
763 expressionp->X_add_number = 0;
768 * Summary of operand().
770 * in: Input_line_pointer points to 1st char of operand, which may
773 * out: A expressionS.
774 * The operand may have been empty: in this case X_op == O_absent.
775 * Input_line_pointer->(next non-blank) char after operand.
779 operand (expressionP)
780 expressionS *expressionP;
783 symbolS *symbolP; /* points to symbol */
784 char *name; /* points to name of symbol */
787 /* All integers are regarded as unsigned unless they are negated.
788 This is because the only thing which cares whether a number is
789 unsigned is the code in emit_expr which extends constants into
790 bignums. It should only sign extend negative numbers, so that
791 something like ``.quad 0x80000000'' is not sign extended even
792 though it appears negative if valueT is 32 bits. */
793 expressionP->X_unsigned = 1;
795 /* digits, assume it is a bignum. */
797 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
798 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
811 input_line_pointer--;
813 integer_constant (flag_m68k_mri ? 0 : 10, expressionP);
817 /* non-decimal radix */
823 /* Check for a hex constant. */
824 for (s = input_line_pointer; hex_p (*s); s++)
826 if (*s == 'h' || *s == 'H')
828 --input_line_pointer;
829 integer_constant (0, expressionP);
834 c = *input_line_pointer;
845 integer_constant (0, expressionP);
851 if (c && strchr (FLT_CHARS, c))
853 input_line_pointer++;
854 floating_constant (expressionP);
855 expressionP->X_add_number =
856 - (isupper ((unsigned char) c) ? tolower (c) : c);
860 /* The string was only zero */
861 expressionP->X_op = O_constant;
862 expressionP->X_add_number = 0;
871 input_line_pointer++;
872 integer_constant (16, expressionP);
876 if (LOCAL_LABELS_FB && ! flag_m68k_mri)
878 /* This code used to check for '+' and '-' here, and, in
879 some conditions, fall through to call
880 integer_constant. However, that didn't make sense,
881 as integer_constant only accepts digits. */
882 /* Some of our code elsewhere does permit digits greater
883 than the expected base; for consistency, do the same
885 if (input_line_pointer[1] < '0'
886 || input_line_pointer[1] > '9')
888 /* Parse this as a back reference to label 0. */
889 input_line_pointer--;
890 integer_constant (10, expressionP);
893 /* Otherwise, parse this as a binary number. */
897 input_line_pointer++;
900 integer_constant (2, expressionP);
911 integer_constant (flag_m68k_mri ? 0 : 8, expressionP);
917 /* If it says "0f" and it could possibly be a floating point
918 number, make it one. Otherwise, make it a local label,
919 and try to deal with parsing the rest later. */
920 if (!input_line_pointer[1]
921 || (is_end_of_line[0xff & input_line_pointer[1]]))
924 char *cp = input_line_pointer + 1;
925 int r = atof_generic (&cp, ".", EXP_CHARS,
926 &generic_floating_point_number);
930 case ERROR_EXPONENT_OVERFLOW:
931 if (*cp == 'f' || *cp == 'b')
932 /* looks like a difference expression */
934 else if (cp == input_line_pointer + 1)
935 /* No characters has been accepted -- looks like
941 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
946 /* Okay, now we've sorted it out. We resume at one of these
947 two labels, depending on what we've decided we're probably
950 input_line_pointer--;
951 integer_constant (10, expressionP);
963 integer_constant (0, expressionP);
973 input_line_pointer++;
974 floating_constant (expressionP);
975 expressionP->X_add_number =
976 - (isupper ((unsigned char) c) ? tolower (c) : c);
980 if (LOCAL_LABELS_DOLLAR)
982 integer_constant (10, expressionP);
993 /* didn't begin with digit & not a name */
994 segment = expression (expressionP);
995 /* Expression() will pass trailing whitespace */
996 if ((c == '(' && *input_line_pointer++ != ')')
997 || (c == '[' && *input_line_pointer++ != ']'))
999 as_bad (_("Missing ')' assumed"));
1000 input_line_pointer--;
1003 /* here with input_line_pointer->char after "(...)" */
1007 if (! flag_m68k_mri || *input_line_pointer != '\'')
1009 as_bad (_("EBCDIC constants are not supported"));
1012 if (! flag_m68k_mri || *input_line_pointer != '\'')
1014 ++input_line_pointer;
1017 if (! flag_m68k_mri)
1019 /* Warning: to conform to other people's assemblers NO
1020 ESCAPEMENT is permitted for a single quote. The next
1021 character, parity errors and all, is taken as the value
1022 of the operand. VERY KINKY. */
1023 expressionP->X_op = O_constant;
1024 expressionP->X_add_number = *input_line_pointer++;
1028 mri_char_constant (expressionP);
1032 (void) operand (expressionP);
1036 /* Double quote is the bitwise not operator in MRI mode. */
1037 if (! flag_m68k_mri)
1041 /* ~ is permitted to start a label on the Delta. */
1042 if (is_name_beginner (c))
1047 operand (expressionP);
1048 if (expressionP->X_op == O_constant)
1050 /* input_line_pointer -> char after operand */
1053 expressionP->X_add_number = - expressionP->X_add_number;
1054 /* Notice: '-' may overflow: no warning is given. This is
1055 compatible with other people's assemblers. Sigh. */
1056 expressionP->X_unsigned = 0;
1058 else if (c == '~' || c == '"')
1059 expressionP->X_add_number = ~ expressionP->X_add_number;
1061 expressionP->X_add_number = ! expressionP->X_add_number;
1063 else if (expressionP->X_op != O_illegal
1064 && expressionP->X_op != O_absent)
1066 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1068 expressionP->X_op = O_uminus;
1069 else if (c == '~' || c == '"')
1070 expressionP->X_op = O_bit_not;
1072 expressionP->X_op = O_logical_not;
1073 expressionP->X_add_number = 0;
1076 as_warn (_("Unary operator %c ignored because bad operand follows"),
1082 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1085 if (! flag_m68k_mri)
1088 if (flag_m68k_mri && hex_p (*input_line_pointer))
1090 /* In MRI mode, $ is also used as the prefix for a
1091 hexadecimal constant. */
1092 integer_constant (16, expressionP);
1096 if (is_part_of_name (*input_line_pointer))
1099 current_location (expressionP);
1103 if (!is_part_of_name (*input_line_pointer))
1105 current_location (expressionP);
1108 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1109 && ! is_part_of_name (input_line_pointer[8]))
1110 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1111 && ! is_part_of_name (input_line_pointer[7])))
1115 start = (input_line_pointer[1] == 't'
1116 || input_line_pointer[1] == 'T');
1117 input_line_pointer += start ? 8 : 7;
1119 if (*input_line_pointer != '(')
1120 as_bad (_("syntax error in .startof. or .sizeof."));
1125 ++input_line_pointer;
1127 name = input_line_pointer;
1128 c = get_symbol_end ();
1130 buf = (char *) xmalloc (strlen (name) + 10);
1132 sprintf (buf, ".startof.%s", name);
1134 sprintf (buf, ".sizeof.%s", name);
1135 symbolP = symbol_make (buf);
1138 expressionP->X_op = O_symbol;
1139 expressionP->X_add_symbol = symbolP;
1140 expressionP->X_add_number = 0;
1142 *input_line_pointer = c;
1144 if (*input_line_pointer != ')')
1145 as_bad (_("syntax error in .startof. or .sizeof."));
1147 ++input_line_pointer;
1159 /* can't imagine any other kind of operand */
1160 expressionP->X_op = O_absent;
1161 input_line_pointer--;
1165 if (! flag_m68k_mri)
1167 integer_constant (2, expressionP);
1171 if (! flag_m68k_mri)
1173 integer_constant (8, expressionP);
1177 if (! flag_m68k_mri)
1180 /* In MRI mode, this is a floating point constant represented
1181 using hexadecimal digits. */
1183 ++input_line_pointer;
1184 integer_constant (16, expressionP);
1188 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1191 current_location (expressionP);
1196 if (is_end_of_line[(unsigned char) c])
1198 if (is_name_beginner (c)) /* here if did not begin with a digit */
1201 * Identifier begins here.
1202 * This is kludged for speed, so code is repeated.
1205 name = --input_line_pointer;
1206 c = get_symbol_end ();
1208 #ifdef md_parse_name
1209 /* This is a hook for the backend to parse certain names
1210 specially in certain contexts. If a name always has a
1211 specific value, it can often be handled by simply
1212 entering it in the symbol table. */
1213 if (md_parse_name (name, expressionP))
1215 *input_line_pointer = c;
1221 /* The MRI i960 assembler permits
1223 FIXME: This should use md_parse_name. */
1225 && (strcasecmp (name, "sizeof") == 0
1226 || strcasecmp (name, "startof") == 0))
1231 start = (name[1] == 't'
1234 *input_line_pointer = c;
1237 name = input_line_pointer;
1238 c = get_symbol_end ();
1240 buf = (char *) xmalloc (strlen (name) + 10);
1242 sprintf (buf, ".startof.%s", name);
1244 sprintf (buf, ".sizeof.%s", name);
1245 symbolP = symbol_make (buf);
1248 expressionP->X_op = O_symbol;
1249 expressionP->X_add_symbol = symbolP;
1250 expressionP->X_add_number = 0;
1252 *input_line_pointer = c;
1259 symbolP = symbol_find_or_make (name);
1261 /* If we have an absolute symbol or a reg, then we know its
1263 segment = S_GET_SEGMENT (symbolP);
1264 if (segment == absolute_section)
1266 expressionP->X_op = O_constant;
1267 expressionP->X_add_number = S_GET_VALUE (symbolP);
1269 else if (segment == reg_section)
1271 expressionP->X_op = O_register;
1272 expressionP->X_add_number = S_GET_VALUE (symbolP);
1276 expressionP->X_op = O_symbol;
1277 expressionP->X_add_symbol = symbolP;
1278 expressionP->X_add_number = 0;
1280 *input_line_pointer = c;
1284 /* Let the target try to parse it. Success is indicated by changing
1285 the X_op field to something other than O_absent and pointing
1286 input_line_pointer passed the expression. If it can't parse the
1287 expression, X_op and input_line_pointer should be unchanged. */
1288 expressionP->X_op = O_absent;
1289 --input_line_pointer;
1290 md_operand (expressionP);
1291 if (expressionP->X_op == O_absent)
1293 ++input_line_pointer;
1294 as_bad (_("Bad expression"));
1295 expressionP->X_op = O_constant;
1296 expressionP->X_add_number = 0;
1303 * It is more 'efficient' to clean up the expressionS when they are created.
1304 * Doing it here saves lines of code.
1306 clean_up_expression (expressionP);
1307 SKIP_WHITESPACE (); /*->1st char after operand. */
1308 know (*input_line_pointer != ' ');
1310 /* The PA port needs this information. */
1311 if (expressionP->X_add_symbol)
1312 expressionP->X_add_symbol->sy_used = 1;
1314 switch (expressionP->X_op)
1317 return absolute_section;
1319 return S_GET_SEGMENT (expressionP->X_add_symbol);
1325 /* Internal. Simplify a struct expression for use by expr() */
1328 * In: address of a expressionS.
1329 * The X_op field of the expressionS may only take certain values.
1330 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1331 * Out: expressionS may have been modified:
1332 * 'foo-foo' symbol references cancelled to 0,
1333 * which changes X_op from O_subtract to O_constant.
1334 * Unused fields zeroed to help expr().
1338 clean_up_expression (expressionP)
1339 expressionS *expressionP;
1341 switch (expressionP->X_op)
1345 expressionP->X_add_number = 0;
1350 expressionP->X_add_symbol = NULL;
1355 expressionP->X_op_symbol = NULL;
1358 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1359 || ((expressionP->X_op_symbol->sy_frag
1360 == expressionP->X_add_symbol->sy_frag)
1361 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1362 && (S_GET_VALUE (expressionP->X_op_symbol)
1363 == S_GET_VALUE (expressionP->X_add_symbol))))
1365 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1366 - S_GET_VALUE (expressionP->X_op_symbol));
1368 expressionP->X_op = O_constant;
1369 expressionP->X_add_symbol = NULL;
1370 expressionP->X_op_symbol = NULL;
1371 expressionP->X_add_number += diff;
1379 /* Expression parser. */
1382 * We allow an empty expression, and just assume (absolute,0) silently.
1383 * Unary operators and parenthetical expressions are treated as operands.
1384 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1386 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1387 * warped that I flushed it and wrote a recursive-descent parser instead.
1388 * Now things are stable, would anybody like to write a fast parser?
1389 * Most expressions are either register (which does not even reach here)
1390 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1391 * So I guess it doesn't really matter how inefficient more complex expressions
1394 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1395 * Also, we have consumed any leading or trailing spaces (operand does that)
1396 * and done all intervening operators.
1398 * This returns the segment of the result, which will be
1399 * absolute_section or the segment of a symbol.
1403 #define __ O_illegal
1405 static const operatorT op_encoding[256] =
1406 { /* maps ASCII->operators */
1408 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1409 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1411 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1412 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1413 __, __, __, __, __, __, __, __,
1414 __, __, __, __, O_lt, __, O_gt, __,
1415 __, __, __, __, __, __, __, __,
1416 __, __, __, __, __, __, __, __,
1417 __, __, __, __, __, __, __, __,
1418 __, __, __, __, __, __, O_bit_exclusive_or, __,
1419 __, __, __, __, __, __, __, __,
1420 __, __, __, __, __, __, __, __,
1421 __, __, __, __, __, __, __, __,
1422 __, __, __, __, O_bit_inclusive_or, __, __, __,
1424 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1425 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1426 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1427 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1428 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1429 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1430 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1431 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1437 * 0 operand, (expression)
1442 * 5 used for * / % in MRI mode
1447 static operator_rankT op_rank[] =
1453 0, /* O_symbol_rva */
1458 8, /* O_logical_not */
1462 7, /* O_left_shift */
1463 7, /* O_right_shift */
1464 6, /* O_bit_inclusive_or */
1465 6, /* O_bit_or_not */
1466 6, /* O_bit_exclusive_or */
1476 2, /* O_logical_and */
1477 1 /* O_logical_or */
1480 /* Unfortunately, in MRI mode for the m68k, multiplication and
1481 division have lower precedence than the bit wise operators. This
1482 function sets the operator precedences correctly for the current
1483 mode. Also, MRI uses a different bit_not operator, and this fixes
1486 #define STANDARD_MUL_PRECEDENCE (7)
1487 #define MRI_MUL_PRECEDENCE (5)
1490 expr_set_precedence ()
1494 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1495 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1496 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1500 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1501 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1502 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1506 /* Initialize the expression parser. */
1511 expr_set_precedence ();
1513 /* Verify that X_op field is wide enough. */
1517 assert (e.X_op == O_max);
1521 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1522 Advance INPUT_LINE_POINTER to the last character in the operator
1523 (i.e., don't change it for a single character operator). */
1525 static inline operatorT
1531 c = *input_line_pointer & 0xff;
1536 return op_encoding[c];
1539 switch (input_line_pointer[1])
1542 return op_encoding[c];
1553 ++input_line_pointer;
1557 if (input_line_pointer[1] != '=')
1558 return op_encoding[c];
1560 ++input_line_pointer;
1564 switch (input_line_pointer[1])
1567 return op_encoding[c];
1569 ret = O_right_shift;
1575 ++input_line_pointer;
1579 /* We accept !! as equivalent to ^ for MRI compatibility. */
1580 if (input_line_pointer[1] != '!')
1583 return O_bit_inclusive_or;
1584 return op_encoding[c];
1586 ++input_line_pointer;
1587 return O_bit_exclusive_or;
1590 if (input_line_pointer[1] != '|')
1591 return op_encoding[c];
1593 ++input_line_pointer;
1594 return O_logical_or;
1597 if (input_line_pointer[1] != '&')
1598 return op_encoding[c];
1600 ++input_line_pointer;
1601 return O_logical_and;
1607 /* Parse an expression. */
1610 expr (rank, resultP)
1611 operator_rankT rank; /* Larger # is higher rank. */
1612 expressionS *resultP; /* Deliver result here. */
1621 retval = operand (resultP);
1623 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
1625 op_left = operator ();
1626 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1630 input_line_pointer++; /*->after 1st character of operator. */
1632 rightseg = expr (op_rank[(int) op_left], &right);
1633 if (right.X_op == O_absent)
1635 as_warn (_("missing operand; zero assumed"));
1636 right.X_op = O_constant;
1637 right.X_add_number = 0;
1638 right.X_add_symbol = NULL;
1639 right.X_op_symbol = NULL;
1642 know (*input_line_pointer != ' ');
1644 if (retval == undefined_section)
1646 if (SEG_NORMAL (rightseg))
1649 else if (! SEG_NORMAL (retval))
1651 else if (SEG_NORMAL (rightseg)
1652 && retval != rightseg
1654 && op_left != O_subtract
1657 as_bad (_("operation combines symbols in different segments"));
1659 op_right = operator ();
1661 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1662 know ((int) op_left >= (int) O_multiply
1663 && (int) op_left <= (int) O_logical_or);
1665 /* input_line_pointer->after right-hand quantity. */
1666 /* left-hand quantity in resultP */
1667 /* right-hand quantity in right. */
1668 /* operator in op_left. */
1670 if (resultP->X_op == O_big)
1672 if (resultP->X_add_number > 0)
1673 as_warn (_("left operand is a bignum; integer 0 assumed"));
1675 as_warn (_("left operand is a float; integer 0 assumed"));
1676 resultP->X_op = O_constant;
1677 resultP->X_add_number = 0;
1678 resultP->X_add_symbol = NULL;
1679 resultP->X_op_symbol = NULL;
1681 if (right.X_op == O_big)
1683 if (right.X_add_number > 0)
1684 as_warn (_("right operand is a bignum; integer 0 assumed"));
1686 as_warn (_("right operand is a float; integer 0 assumed"));
1687 right.X_op = O_constant;
1688 right.X_add_number = 0;
1689 right.X_add_symbol = NULL;
1690 right.X_op_symbol = NULL;
1693 /* Optimize common cases. */
1694 if (op_left == O_add && right.X_op == O_constant)
1697 resultP->X_add_number += right.X_add_number;
1699 /* This case comes up in PIC code. */
1700 else if (op_left == O_subtract
1701 && right.X_op == O_symbol
1702 && resultP->X_op == O_symbol
1703 && (right.X_add_symbol->sy_frag
1704 == resultP->X_add_symbol->sy_frag)
1705 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1708 resultP->X_add_number -= right.X_add_number;
1709 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1710 - S_GET_VALUE (right.X_add_symbol));
1711 resultP->X_op = O_constant;
1712 resultP->X_add_symbol = 0;
1714 else if (op_left == O_subtract && right.X_op == O_constant)
1717 resultP->X_add_number -= right.X_add_number;
1719 else if (op_left == O_add && resultP->X_op == O_constant)
1722 resultP->X_op = right.X_op;
1723 resultP->X_add_symbol = right.X_add_symbol;
1724 resultP->X_op_symbol = right.X_op_symbol;
1725 resultP->X_add_number += right.X_add_number;
1728 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1730 /* Constant OP constant. */
1731 offsetT v = right.X_add_number;
1732 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1734 as_warn (_("division by zero"));
1740 case O_multiply: resultP->X_add_number *= v; break;
1741 case O_divide: resultP->X_add_number /= v; break;
1742 case O_modulus: resultP->X_add_number %= v; break;
1743 case O_left_shift: resultP->X_add_number <<= v; break;
1745 /* We always use unsigned shifts, to avoid relying on
1746 characteristics of the compiler used to compile gas. */
1747 resultP->X_add_number =
1748 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1750 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1751 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1752 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1753 case O_bit_and: resultP->X_add_number &= v; break;
1754 case O_add: resultP->X_add_number += v; break;
1755 case O_subtract: resultP->X_add_number -= v; break;
1757 resultP->X_add_number =
1758 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1761 resultP->X_add_number =
1762 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1765 resultP->X_add_number =
1766 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1769 resultP->X_add_number =
1770 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1773 resultP->X_add_number =
1774 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1777 resultP->X_add_number =
1778 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1781 resultP->X_add_number = resultP->X_add_number && v;
1784 resultP->X_add_number = resultP->X_add_number || v;
1788 else if (resultP->X_op == O_symbol
1789 && right.X_op == O_symbol
1790 && (op_left == O_add
1791 || op_left == O_subtract
1792 || (resultP->X_add_number == 0
1793 && right.X_add_number == 0)))
1795 /* Symbol OP symbol. */
1796 resultP->X_op = op_left;
1797 resultP->X_op_symbol = right.X_add_symbol;
1798 if (op_left == O_add)
1799 resultP->X_add_number += right.X_add_number;
1800 else if (op_left == O_subtract)
1801 resultP->X_add_number -= right.X_add_number;
1805 /* The general case. */
1806 resultP->X_add_symbol = make_expr_symbol (resultP);
1807 resultP->X_op_symbol = make_expr_symbol (&right);
1808 resultP->X_op = op_left;
1809 resultP->X_add_number = 0;
1810 resultP->X_unsigned = 1;
1814 } /* While next operator is >= this rank. */
1816 /* The PA port needs this information. */
1817 if (resultP->X_add_symbol)
1818 resultP->X_add_symbol->sy_used = 1;
1820 return resultP->X_op == O_constant ? absolute_section : retval;
1826 * This lives here because it belongs equally in expr.c & read.c.
1827 * Expr.c is just a branch office read.c anyway, and putting it
1828 * here lessens the crowd at read.c.
1830 * Assume input_line_pointer is at start of symbol name.
1831 * Advance input_line_pointer past symbol name.
1832 * Turn that character into a '\0', returning its former value.
1833 * This allows a string compare (RMS wants symbol names to be strings)
1834 * of the symbol name.
1835 * There will always be a char following symbol name, because all good
1836 * lines end in end-of-line.
1843 /* We accept \001 in a name in case this is being called with a
1844 constructed string. */
1845 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1846 while (is_part_of_name (c = *input_line_pointer++)
1849 *--input_line_pointer = 0;
1855 get_single_number ()
1859 return exp.X_add_number;