1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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 2, or (at your option)
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.
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
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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.
31 #include "libiberty.h"
34 static void floating_constant PARAMS ((expressionS * expressionP));
35 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
36 static void mri_char_constant PARAMS ((expressionS *));
37 static void current_location PARAMS ((expressionS *));
38 static void clean_up_expression PARAMS ((expressionS * expressionP));
40 extern const char EXP_CHARS[], FLT_CHARS[];
42 /* Build a dummy symbol to hold a complex expression. This is how we
43 build expressions up out of other expressions. The symbol is put
44 into the fake section expr_section. */
47 make_expr_symbol (expressionP)
48 expressionS *expressionP;
53 if (expressionP->X_op == O_symbol
54 && expressionP->X_add_number == 0)
55 return expressionP->X_add_symbol;
57 /* FIXME: This should be something which decode_local_label_name
59 fake = FAKE_LABEL_NAME;
61 /* Putting constant symbols in absolute_section rather than
62 expr_section is convenient for the old a.out code, for which
63 S_GET_SEGMENT does not always retrieve the value put in by
65 symbolP = symbol_create (fake,
66 (expressionP->X_op == O_constant
69 0, &zero_address_frag);
70 symbolP->sy_value = *expressionP;
72 if (expressionP->X_op == O_constant)
73 resolve_symbol_value (symbolP);
79 * Build any floating-point literal here.
80 * Also build any bignum literal here.
83 /* Seems atof_machine can backscan through generic_bignum and hit whatever
84 happens to be loaded before it in memory. And its way too complicated
85 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
86 and never write into the early words, thus they'll always be zero.
87 I hate Dean's floating-point code. Bleh. */
88 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
89 FLONUM_TYPE generic_floating_point_number =
91 &generic_bignum[6], /* low (JF: Was 0) */
92 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */
97 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
98 int generic_floating_point_magic;
101 floating_constant (expressionP)
102 expressionS *expressionP;
104 /* input_line_pointer->*/
105 /* floating-point constant. */
108 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
109 &generic_floating_point_number);
113 if (error_code == ERROR_EXPONENT_OVERFLOW)
115 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
119 as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
122 expressionP->X_op = O_big;
123 /* input_line_pointer->just after constant, */
124 /* which may point to whitespace. */
125 expressionP->X_add_number = -1;
129 integer_constant (radix, expressionP)
131 expressionS *expressionP;
133 char *start; /* start of number. */
136 valueT number; /* offset or (absolute) value */
137 short int digit; /* value of next digit in current radix */
138 short int maxdig = 0;/* highest permitted digit value. */
139 int too_many_digits = 0; /* if we see >= this number of */
140 char *name; /* points to name of symbol */
141 symbolS *symbolP; /* points to symbol */
143 int small; /* true if fits in 32 bits. */
145 /* May be bignum, or may fit in 32 bits. */
146 /* Most numbers fit into 32 bits, and we want this case to be fast.
147 so we pretend it will fit into 32 bits. If, after making up a 32
148 bit number, we realise that we have scanned more digits than
149 comfortably fit into 32 bits, we re-scan the digits coding them
150 into a bignum. For decimal and octal numbers we are
151 conservative: Some numbers may be assumed bignums when in fact
152 they do fit into 32 bits. Numbers of any radix can have excess
153 leading zeros: We strive to recognise this and cast them back
154 into 32 bits. We must check that the bignum really is more than
155 32 bits, and change it back to a 32-bit number if it fits. The
156 number we are looking for is expected to be positive, but if it
157 fits into 32 bits as an unsigned number, we let it be a 32-bit
158 number. The cavalier approach is for speed in ordinary cases. */
159 /* This has been extended for 64 bits. We blindly assume that if
160 you're compiling in 64-bit mode, the target is a 64-bit machine.
161 This should be cleaned up. */
165 #else /* includes non-bfd case, mostly */
169 if (flag_mri && radix == 0)
173 /* In MRI mode, the number may have a suffix indicating the
174 radix. For that matter, it might actually be a floating
176 for (suffix = input_line_pointer; isalnum (*suffix); suffix++)
178 if (*suffix == 'e' || *suffix == 'E')
182 if (suffix == input_line_pointer)
196 else if (c == 'O' || c == 'Q')
200 else if (suffix[1] == '.' || c == 'E' || flt)
202 floating_constant (expressionP);
217 too_many_digits = valuesize + 1;
221 too_many_digits = (valuesize + 2) / 3 + 1;
225 too_many_digits = (valuesize + 3) / 4 + 1;
229 too_many_digits = (valuesize + 12) / 4; /* very rough */
232 start = input_line_pointer;
233 c = *input_line_pointer++;
235 (digit = hex_value (c)) < maxdig;
236 c = *input_line_pointer++)
238 number = number * radix + digit;
240 /* c contains character after number. */
241 /* input_line_pointer->char after c. */
242 small = (input_line_pointer - start - 1) < too_many_digits;
246 * we saw a lot of digits. manufacture a bignum the hard way.
248 LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */
249 LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */
252 leader = generic_bignum;
253 generic_bignum[0] = 0;
254 generic_bignum[1] = 0;
255 input_line_pointer = start; /*->1st digit. */
256 c = *input_line_pointer++;
258 (carry = hex_value (c)) < maxdig;
259 c = *input_line_pointer++)
261 for (pointer = generic_bignum;
267 work = carry + radix * *pointer;
268 *pointer = work & LITTLENUM_MASK;
269 carry = work >> LITTLENUM_NUMBER_OF_BITS;
273 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
275 /* room to grow a longer bignum. */
280 /* again, c is char after number, */
281 /* input_line_pointer->after c. */
282 know (LITTLENUM_NUMBER_OF_BITS == 16);
283 if (leader < generic_bignum + 2)
285 /* will fit into 32 bits. */
287 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
288 | (generic_bignum[0] & LITTLENUM_MASK);
293 number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
297 if (flag_mri && suffix != NULL && input_line_pointer - 1 == suffix)
298 c = *input_line_pointer++;
303 * here with number, in correct radix. c is the next char.
304 * note that unlike un*x, we allow "011f" "0x9f" to
305 * both mean the same as the (conventional) "9f". this is simply easier
306 * than checking for strict canonical form. syntax sux!
309 if (LOCAL_LABELS_FB && c == 'b')
312 * backward ref to local label.
313 * because it is backward, expect it to be defined.
315 /* Construct a local label. */
316 name = fb_label_name ((int) number, 0);
318 /* seen before, or symbol is defined: ok */
319 symbolP = symbol_find (name);
320 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
322 /* local labels are never absolute. don't waste time
323 checking absoluteness. */
324 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
326 expressionP->X_op = O_symbol;
327 expressionP->X_add_symbol = symbolP;
331 /* either not seen or not defined. */
332 /* @@ Should print out the original string instead of
333 the parsed number. */
334 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
336 expressionP->X_op = O_constant;
339 expressionP->X_add_number = 0;
341 else if (LOCAL_LABELS_FB && c == 'f')
344 * forward reference. expect symbol to be undefined or
345 * unknown. undefined: seen it before. unknown: never seen
347 * construct a local label name, then an undefined symbol.
348 * don't create a xseg frag for it: caller may do that.
349 * just return it as never seen before.
351 name = fb_label_name ((int) number, 1);
352 symbolP = symbol_find_or_make (name);
353 /* we have no need to check symbol properties. */
354 #ifndef many_segments
355 /* since "know" puts its arg into a "string", we
356 can't have newlines in the argument. */
357 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
359 expressionP->X_op = O_symbol;
360 expressionP->X_add_symbol = symbolP;
361 expressionP->X_add_number = 0;
363 else if (LOCAL_LABELS_DOLLAR && c == '$')
365 /* If the dollar label is *currently* defined, then this is just
366 another reference to it. If it is not *currently* defined,
367 then this is a fresh instantiation of that number, so create
370 if (dollar_label_defined ((long) number))
372 name = dollar_label_name ((long) number, 0);
373 symbolP = symbol_find (name);
374 know (symbolP != NULL);
378 name = dollar_label_name ((long) number, 1);
379 symbolP = symbol_find_or_make (name);
382 expressionP->X_op = O_symbol;
383 expressionP->X_add_symbol = symbolP;
384 expressionP->X_add_number = 0;
388 expressionP->X_op = O_constant;
389 #ifdef TARGET_WORD_SIZE
390 /* Sign extend NUMBER. */
391 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
393 expressionP->X_add_number = number;
394 input_line_pointer--; /* restore following character. */
395 } /* really just a number */
399 /* not a small number */
400 expressionP->X_op = O_big;
401 expressionP->X_add_number = number; /* number of littlenums */
402 input_line_pointer--; /*->char following number. */
406 /* Parse an MRI multi character constant. */
409 mri_char_constant (expressionP)
410 expressionS *expressionP;
414 if (*input_line_pointer == '\''
415 && input_line_pointer[1] != '\'')
417 expressionP->X_op = O_constant;
418 expressionP->X_add_number = 0;
422 /* In order to get the correct byte ordering, we must build the
423 number in reverse. */
424 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
428 generic_bignum[i] = 0;
429 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
431 if (*input_line_pointer == '\'')
433 if (input_line_pointer[1] != '\'')
435 ++input_line_pointer;
437 generic_bignum[i] <<= 8;
438 generic_bignum[i] += *input_line_pointer;
439 ++input_line_pointer;
442 if (i < SIZE_OF_LARGE_NUMBER - 1)
444 /* If there is more than one littlenum, left justify the
445 last one to make it match the earlier ones. If there is
446 only one, we can just use the value directly. */
447 for (; j < CHARS_PER_LITTLENUM; j++)
448 generic_bignum[i] <<= 8;
451 if (*input_line_pointer == '\''
452 && input_line_pointer[1] != '\'')
458 as_bad ("Character constant too large");
467 c = SIZE_OF_LARGE_NUMBER - i;
468 for (j = 0; j < c; j++)
469 generic_bignum[j] = generic_bignum[i + j];
473 know (LITTLENUM_NUMBER_OF_BITS == 16);
476 expressionP->X_op = O_big;
477 expressionP->X_add_number = i;
481 expressionP->X_op = O_constant;
483 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
485 expressionP->X_add_number =
486 (((generic_bignum[1] & LITTLENUM_MASK)
487 << LITTLENUM_NUMBER_OF_BITS)
488 | (generic_bignum[0] & LITTLENUM_MASK));
491 /* Skip the final closing quote. */
492 ++input_line_pointer;
495 /* Return an expression representing the current location. This
496 handles the magic symbol `.'. */
499 current_location (expressionp)
500 expressionS *expressionp;
502 if (now_seg == absolute_section)
504 expressionp->X_op = O_constant;
505 expressionp->X_add_number = abs_section_offset;
511 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
512 (valueT) frag_now_fix (),
514 expressionp->X_op = O_symbol;
515 expressionp->X_add_symbol = symbolp;
516 expressionp->X_add_number = 0;
521 * Summary of operand().
523 * in: Input_line_pointer points to 1st char of operand, which may
526 * out: A expressionS.
527 * The operand may have been empty: in this case X_op == O_absent.
528 * Input_line_pointer->(next non-blank) char after operand.
532 operand (expressionP)
533 expressionS *expressionP;
536 symbolS *symbolP; /* points to symbol */
537 char *name; /* points to name of symbol */
540 /* All integers are regarded as unsigned unless they are negated.
541 This is because the only thing which cares whether a number is
542 unsigned is the code in emit_expr which extends constants into
543 bignums. It should only sign extend negative numbers, so that
544 something like ``.quad 0x80000000'' is not sign extended even
545 though it appears negative if valueT is 32 bits. */
546 expressionP->X_unsigned = 1;
548 /* digits, assume it is a bignum. */
550 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
551 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
564 input_line_pointer--;
566 integer_constant (flag_mri ? 0 : 10, expressionP);
570 /* non-decimal radix */
576 /* Check for a hex constant. */
577 for (s = input_line_pointer; hex_p (*s); s++)
579 if (*s == 'h' || *s == 'H')
581 --input_line_pointer;
582 integer_constant (0, expressionP);
587 c = *input_line_pointer;
594 integer_constant (0, expressionP);
600 if (c && strchr (FLT_CHARS, c))
602 input_line_pointer++;
603 floating_constant (expressionP);
604 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
608 /* The string was only zero */
609 expressionP->X_op = O_constant;
610 expressionP->X_add_number = 0;
619 input_line_pointer++;
620 integer_constant (16, expressionP);
626 switch (input_line_pointer[1])
630 /* If unambiguously a difference expression, treat
631 it as one by indicating a label; otherwise, it's
632 always a binary number. */
634 char *cp = input_line_pointer + 1;
635 while (strchr ("0123456789", *++cp))
637 if (*cp == 'b' || *cp == 'f')
642 /* Some of our code elsewhere does permit digits
643 greater than the expected base; for consistency,
645 case '2': case '3': case '4': case '5':
646 case '6': case '7': case '8': case '9':
654 input_line_pointer--;
655 integer_constant (10, expressionP);
661 input_line_pointer++;
664 integer_constant (2, expressionP);
675 integer_constant (flag_mri ? 0 : 8, expressionP);
681 /* If it says "0f" and it could possibly be a floating point
682 number, make it one. Otherwise, make it a local label,
683 and try to deal with parsing the rest later. */
684 if (!input_line_pointer[1]
685 || (is_end_of_line[0xff & input_line_pointer[1]]))
688 char *cp = input_line_pointer + 1;
689 int r = atof_generic (&cp, ".", EXP_CHARS,
690 &generic_floating_point_number);
694 case ERROR_EXPONENT_OVERFLOW:
695 if (*cp == 'f' || *cp == 'b')
696 /* looks like a difference expression */
701 as_fatal ("expr.c(operand): bad atof_generic return val %d",
706 /* Okay, now we've sorted it out. We resume at one of these
707 two labels, depending on what we've decided we're probably
710 input_line_pointer--;
711 integer_constant (10, expressionP);
727 input_line_pointer++;
728 floating_constant (expressionP);
729 expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
733 if (LOCAL_LABELS_DOLLAR)
735 integer_constant (10, expressionP);
746 /* didn't begin with digit & not a name */
747 segment = expression (expressionP);
748 /* Expression() will pass trailing whitespace */
749 if ((c == '(' && *input_line_pointer++ != ')')
750 || (c == '[' && *input_line_pointer++ != ']'))
752 as_bad ("Missing ')' assumed");
753 input_line_pointer--;
755 /* here with input_line_pointer->char after "(...)" */
759 if (! flag_mri || *input_line_pointer != '\'')
761 as_bad ("EBCDIC constants are not supported");
764 if (! flag_mri || *input_line_pointer != '\'')
766 ++input_line_pointer;
771 /* Warning: to conform to other people's assemblers NO
772 ESCAPEMENT is permitted for a single quote. The next
773 character, parity errors and all, is taken as the value
774 of the operand. VERY KINKY. */
775 expressionP->X_op = O_constant;
776 expressionP->X_add_number = *input_line_pointer++;
780 mri_char_constant (expressionP);
784 (void) operand (expressionP);
788 /* Double quote is the logical not operator in MRI mode. */
795 operand (expressionP);
796 if (expressionP->X_op == O_constant)
798 /* input_line_pointer -> char after operand */
801 expressionP->X_add_number = - expressionP->X_add_number;
802 /* Notice: '-' may overflow: no warning is given. This is
803 compatible with other people's assemblers. Sigh. */
804 expressionP->X_unsigned = 0;
807 expressionP->X_add_number = ~ expressionP->X_add_number;
809 else if (expressionP->X_op != O_illegal
810 && expressionP->X_op != O_absent)
812 expressionP->X_add_symbol = make_expr_symbol (expressionP);
814 expressionP->X_op = O_uminus;
816 expressionP->X_op = O_bit_not;
817 expressionP->X_add_number = 0;
820 as_warn ("Unary operator %c ignored because bad operand follows",
826 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
832 if (flag_mri && hex_p (*input_line_pointer))
834 /* In MRI mode, $ is also used as the prefix for a
835 hexadecimal constant. */
836 integer_constant (16, expressionP);
840 if (is_part_of_name (*input_line_pointer))
843 current_location (expressionP);
847 if (!is_part_of_name (*input_line_pointer))
849 current_location (expressionP);
852 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
853 && ! is_part_of_name (input_line_pointer[8]))
854 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
855 && ! is_part_of_name (input_line_pointer[7])))
859 start = (input_line_pointer[1] == 't'
860 || input_line_pointer[1] == 'T');
861 input_line_pointer += start ? 8 : 7;
863 if (*input_line_pointer != '(')
864 as_bad ("syntax error in .startof. or .sizeof.");
869 ++input_line_pointer;
871 name = input_line_pointer;
872 c = get_symbol_end ();
874 buf = (char *) xmalloc (strlen (name) + 10);
876 sprintf (buf, ".startof.%s", name);
878 sprintf (buf, ".sizeof.%s", name);
879 symbolP = symbol_make (buf);
882 expressionP->X_op = O_symbol;
883 expressionP->X_add_symbol = symbolP;
884 expressionP->X_add_number = 0;
886 *input_line_pointer = c;
888 if (*input_line_pointer != ')')
889 as_bad ("syntax error in .startof. or .sizeof.");
891 ++input_line_pointer;
903 /* can't imagine any other kind of operand */
904 expressionP->X_op = O_absent;
905 input_line_pointer--;
911 integer_constant (2, expressionP);
917 integer_constant (8, expressionP);
924 /* In MRI mode, this is a floating point constant represented
925 using hexadecimal digits. */
927 ++input_line_pointer;
928 integer_constant (16, expressionP);
932 if (! flag_mri || is_part_of_name (*input_line_pointer))
935 current_location (expressionP);
940 if (is_end_of_line[(unsigned char) c])
942 if (is_name_beginner (c)) /* here if did not begin with a digit */
945 * Identifier begins here.
946 * This is kludged for speed, so code is repeated.
949 name = --input_line_pointer;
950 c = get_symbol_end ();
951 symbolP = symbol_find_or_make (name);
953 /* If we have an absolute symbol or a reg, then we know its
955 segment = S_GET_SEGMENT (symbolP);
956 if (segment == absolute_section)
958 expressionP->X_op = O_constant;
959 expressionP->X_add_number = S_GET_VALUE (symbolP);
961 else if (segment == reg_section)
963 expressionP->X_op = O_register;
964 expressionP->X_add_number = S_GET_VALUE (symbolP);
968 expressionP->X_op = O_symbol;
969 expressionP->X_add_symbol = symbolP;
970 expressionP->X_add_number = 0;
972 *input_line_pointer = c;
976 /* Let the target try to parse it. Success is indicated by changing
977 the X_op field to something other than O_absent and pointing
978 input_line_pointer passed the expression. If it can't parse the
979 expression, X_op and input_line_pointer should be unchanged. */
980 expressionP->X_op = O_absent;
981 --input_line_pointer;
982 md_operand (expressionP);
983 if (expressionP->X_op == O_absent)
985 ++input_line_pointer;
986 as_bad ("Bad expression");
987 expressionP->X_op = O_constant;
988 expressionP->X_add_number = 0;
995 * It is more 'efficient' to clean up the expressionS when they are created.
996 * Doing it here saves lines of code.
998 clean_up_expression (expressionP);
999 SKIP_WHITESPACE (); /*->1st char after operand. */
1000 know (*input_line_pointer != ' ');
1002 /* The PA port needs this information. */
1003 if (expressionP->X_add_symbol)
1004 expressionP->X_add_symbol->sy_used = 1;
1006 switch (expressionP->X_op)
1009 return absolute_section;
1011 return S_GET_SEGMENT (expressionP->X_add_symbol);
1017 /* Internal. Simplify a struct expression for use by expr() */
1020 * In: address of a expressionS.
1021 * The X_op field of the expressionS may only take certain values.
1022 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1023 * Out: expressionS may have been modified:
1024 * 'foo-foo' symbol references cancelled to 0,
1025 * which changes X_op from O_subtract to O_constant.
1026 * Unused fields zeroed to help expr().
1030 clean_up_expression (expressionP)
1031 expressionS *expressionP;
1033 switch (expressionP->X_op)
1037 expressionP->X_add_number = 0;
1042 expressionP->X_add_symbol = NULL;
1047 expressionP->X_op_symbol = NULL;
1050 if (expressionP->X_op_symbol == expressionP->X_add_symbol
1051 || ((expressionP->X_op_symbol->sy_frag
1052 == expressionP->X_add_symbol->sy_frag)
1053 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1054 && (S_GET_VALUE (expressionP->X_op_symbol)
1055 == S_GET_VALUE (expressionP->X_add_symbol))))
1057 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1058 - S_GET_VALUE (expressionP->X_op_symbol));
1060 expressionP->X_op = O_constant;
1061 expressionP->X_add_symbol = NULL;
1062 expressionP->X_op_symbol = NULL;
1063 expressionP->X_add_number += diff;
1071 /* Expression parser. */
1074 * We allow an empty expression, and just assume (absolute,0) silently.
1075 * Unary operators and parenthetical expressions are treated as operands.
1076 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1078 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1079 * warped that I flushed it and wrote a recursive-descent parser instead.
1080 * Now things are stable, would anybody like to write a fast parser?
1081 * Most expressions are either register (which does not even reach here)
1082 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1083 * So I guess it doesn't really matter how inefficient more complex expressions
1086 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1087 * Also, we have consumed any leading or trailing spaces (operand does that)
1088 * and done all intervening operators.
1090 * This returns the segment of the result, which will be
1091 * absolute_section or the segment of a symbol.
1095 #define __ O_illegal
1097 static operatorT op_encoding[256] =
1098 { /* maps ASCII->operators */
1100 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1101 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1103 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1104 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1105 __, __, __, __, __, __, __, __,
1106 __, __, __, __, O_lt, __, O_gt, __,
1107 __, __, __, __, __, __, __, __,
1108 __, __, __, __, __, __, __, __,
1109 __, __, __, __, __, __, __, __,
1110 __, __, __, __, __, __, O_bit_exclusive_or, __,
1111 __, __, __, __, __, __, __, __,
1112 __, __, __, __, __, __, __, __,
1113 __, __, __, __, __, __, __, __,
1114 __, __, __, __, O_bit_inclusive_or, __, __, __,
1116 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1117 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1118 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1119 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1120 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1121 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1122 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1123 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1129 * 0 operand, (expression)
1132 * 3 used for * / % in MRI mode
1137 static operator_rankT op_rank[] =
1150 5, /* O_left_shift */
1151 5, /* O_right_shift */
1152 4, /* O_bit_inclusive_or */
1153 4, /* O_bit_or_not */
1154 4, /* O_bit_exclusive_or */
1166 /* Initialize the expression parser. */
1171 /* In MRI mode, multiplication and division have lower precedence
1172 than the bit wise operators. */
1175 op_rank[O_multiply] = 3;
1176 op_rank[O_divide] = 3;
1177 op_rank[O_modulus] = 3;
1178 op_encoding['"'] = O_bit_not;
1182 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1183 Advance INPUT_LINE_POINTER to the last character in the operator
1184 (i.e., don't change it for a single character operator). */
1186 static inline operatorT
1192 c = *input_line_pointer;
1197 return op_encoding[c];
1200 switch (input_line_pointer[1])
1203 return op_encoding[c];
1214 ++input_line_pointer;
1218 switch (input_line_pointer[1])
1221 return op_encoding[c];
1223 ret = O_right_shift;
1229 ++input_line_pointer;
1233 /* We accept !! as equivalent to ^ for MRI compatibility. */
1234 if (input_line_pointer[1] != '!')
1237 return O_bit_inclusive_or;
1238 return op_encoding[c];
1240 ++input_line_pointer;
1241 return O_bit_exclusive_or;
1247 /* Parse an expression. */
1250 expr (rank, resultP)
1251 operator_rankT rank; /* Larger # is higher rank. */
1252 expressionS *resultP; /* Deliver result here. */
1261 retval = operand (resultP);
1263 know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */
1265 op_left = operator ();
1266 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1270 input_line_pointer++; /*->after 1st character of operator. */
1272 rightseg = expr (op_rank[(int) op_left], &right);
1273 if (right.X_op == O_absent)
1275 as_warn ("missing operand; zero assumed");
1276 right.X_op = O_constant;
1277 right.X_add_number = 0;
1278 right.X_add_symbol = NULL;
1279 right.X_op_symbol = NULL;
1282 know (*input_line_pointer != ' ');
1284 if (retval == undefined_section)
1286 if (SEG_NORMAL (rightseg))
1289 else if (! SEG_NORMAL (retval))
1291 else if (SEG_NORMAL (rightseg)
1292 && retval != rightseg
1294 && op_left != O_subtract
1297 as_bad ("operation combines symbols in different segments");
1299 op_right = operator ();
1301 know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1302 know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_gt);
1304 /* input_line_pointer->after right-hand quantity. */
1305 /* left-hand quantity in resultP */
1306 /* right-hand quantity in right. */
1307 /* operator in op_left. */
1309 if (resultP->X_op == O_big)
1311 as_warn ("left operand is a %s; integer 0 assumed",
1312 resultP->X_add_number > 0 ? "bignum" : "float");
1313 resultP->X_op = O_constant;
1314 resultP->X_add_number = 0;
1315 resultP->X_add_symbol = NULL;
1316 resultP->X_op_symbol = NULL;
1318 if (right.X_op == O_big)
1320 as_warn ("right operand is a %s; integer 0 assumed",
1321 right.X_add_number > 0 ? "bignum" : "float");
1322 right.X_op = O_constant;
1323 right.X_add_number = 0;
1324 right.X_add_symbol = NULL;
1325 right.X_op_symbol = NULL;
1328 /* Optimize common cases. */
1329 if (op_left == O_add && right.X_op == O_constant)
1332 resultP->X_add_number += right.X_add_number;
1334 /* This case comes up in PIC code. */
1335 else if (op_left == O_subtract
1336 && right.X_op == O_symbol
1337 && resultP->X_op == O_symbol
1338 && (right.X_add_symbol->sy_frag
1339 == resultP->X_add_symbol->sy_frag)
1340 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1343 resultP->X_add_number += right.X_add_number;
1344 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1345 - S_GET_VALUE (right.X_add_symbol));
1346 resultP->X_op = O_constant;
1347 resultP->X_add_symbol = 0;
1349 else if (op_left == O_subtract && right.X_op == O_constant)
1352 resultP->X_add_number -= right.X_add_number;
1354 else if (op_left == O_add && resultP->X_op == O_constant)
1357 resultP->X_op = right.X_op;
1358 resultP->X_add_symbol = right.X_add_symbol;
1359 resultP->X_op_symbol = right.X_op_symbol;
1360 resultP->X_add_number += right.X_add_number;
1363 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1365 /* Constant OP constant. */
1366 offsetT v = right.X_add_number;
1367 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1369 as_warn ("division by zero");
1375 case O_multiply: resultP->X_add_number *= v; break;
1376 case O_divide: resultP->X_add_number /= v; break;
1377 case O_modulus: resultP->X_add_number %= v; break;
1378 case O_left_shift: resultP->X_add_number <<= v; break;
1379 case O_right_shift: resultP->X_add_number >>= v; break;
1380 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1381 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1382 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1383 case O_bit_and: resultP->X_add_number &= v; break;
1384 case O_add: resultP->X_add_number += v; break;
1385 case O_subtract: resultP->X_add_number -= v; break;
1387 resultP->X_add_number =
1388 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1391 resultP->X_add_number =
1392 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1395 resultP->X_add_number =
1396 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
1399 resultP->X_add_number =
1400 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1403 resultP->X_add_number =
1404 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1407 resultP->X_add_number =
1408 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
1412 else if (resultP->X_op == O_symbol
1413 && right.X_op == O_symbol
1414 && (op_left == O_add
1415 || op_left == O_subtract
1416 || (resultP->X_add_number == 0
1417 && right.X_add_number == 0)))
1419 /* Symbol OP symbol. */
1420 resultP->X_op = op_left;
1421 resultP->X_op_symbol = right.X_add_symbol;
1422 if (op_left == O_add)
1423 resultP->X_add_number += right.X_add_number;
1424 else if (op_left == O_subtract)
1425 resultP->X_add_number -= right.X_add_number;
1429 /* The general case. */
1430 resultP->X_add_symbol = make_expr_symbol (resultP);
1431 resultP->X_op_symbol = make_expr_symbol (&right);
1432 resultP->X_op = op_left;
1433 resultP->X_add_number = 0;
1434 resultP->X_unsigned = 1;
1438 } /* While next operator is >= this rank. */
1440 /* The PA port needs this information. */
1441 if (resultP->X_add_symbol)
1442 resultP->X_add_symbol->sy_used = 1;
1444 return resultP->X_op == O_constant ? absolute_section : retval;
1450 * This lives here because it belongs equally in expr.c & read.c.
1451 * Expr.c is just a branch office read.c anyway, and putting it
1452 * here lessens the crowd at read.c.
1454 * Assume input_line_pointer is at start of symbol name.
1455 * Advance input_line_pointer past symbol name.
1456 * Turn that character into a '\0', returning its former value.
1457 * This allows a string compare (RMS wants symbol names to be strings)
1458 * of the symbol name.
1459 * There will always be a char following symbol name, because all good
1460 * lines end in end-of-line.
1467 /* We accept \001 in a name in case this is being called with a
1468 constructed string. */
1469 while (is_part_of_name (c = *input_line_pointer++)
1472 *--input_line_pointer = 0;
1478 get_single_number ()
1482 return exp.X_add_number;