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 clean_up_expression
PARAMS ((expressionS
* expressionP
));
39 extern const char EXP_CHARS
[], FLT_CHARS
[];
41 /* Build a dummy symbol to hold a complex expression. This is how we
42 build expressions up out of other expressions. The symbol is put
43 into the fake section expr_section. */
46 make_expr_symbol (expressionP
)
47 expressionS
*expressionP
;
52 if (expressionP
->X_op
== O_symbol
53 && expressionP
->X_add_number
== 0)
54 return expressionP
->X_add_symbol
;
56 /* FIXME: This should be something which decode_local_label_name
58 fake
= FAKE_LABEL_NAME
;
60 /* Putting constant symbols in absolute_section rather than
61 expr_section is convenient for the old a.out code, for which
62 S_GET_SEGMENT does not always retrieve the value put in by
64 symbolP
= symbol_create (fake
,
65 (expressionP
->X_op
== O_constant
68 0, &zero_address_frag
);
69 symbolP
->sy_value
= *expressionP
;
71 if (expressionP
->X_op
== O_constant
)
72 resolve_symbol_value (symbolP
);
78 * Build any floating-point literal here.
79 * Also build any bignum literal here.
82 /* Seems atof_machine can backscan through generic_bignum and hit whatever
83 happens to be loaded before it in memory. And its way too complicated
84 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
85 and never write into the early words, thus they'll always be zero.
86 I hate Dean's floating-point code. Bleh. */
87 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
88 FLONUM_TYPE generic_floating_point_number
=
90 &generic_bignum
[6], /* low (JF: Was 0) */
91 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high JF: (added +6) */
96 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
97 int generic_floating_point_magic
;
100 floating_constant (expressionP
)
101 expressionS
*expressionP
;
103 /* input_line_pointer->*/
104 /* floating-point constant. */
107 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
108 &generic_floating_point_number
);
112 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
114 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
118 as_bad ("bad floating-point constant: unknown error code=%d.", error_code
);
121 expressionP
->X_op
= O_big
;
122 /* input_line_pointer->just after constant, */
123 /* which may point to whitespace. */
124 expressionP
->X_add_number
= -1;
128 integer_constant (radix
, expressionP
)
130 expressionS
*expressionP
;
132 char *start
; /* start of number. */
135 valueT number
; /* offset or (absolute) value */
136 short int digit
; /* value of next digit in current radix */
137 short int maxdig
= 0;/* highest permitted digit value. */
138 int too_many_digits
= 0; /* if we see >= this number of */
139 char *name
; /* points to name of symbol */
140 symbolS
*symbolP
; /* points to symbol */
142 int small
; /* true if fits in 32 bits. */
144 /* May be bignum, or may fit in 32 bits. */
145 /* Most numbers fit into 32 bits, and we want this case to be fast.
146 so we pretend it will fit into 32 bits. If, after making up a 32
147 bit number, we realise that we have scanned more digits than
148 comfortably fit into 32 bits, we re-scan the digits coding them
149 into a bignum. For decimal and octal numbers we are
150 conservative: Some numbers may be assumed bignums when in fact
151 they do fit into 32 bits. Numbers of any radix can have excess
152 leading zeros: We strive to recognise this and cast them back
153 into 32 bits. We must check that the bignum really is more than
154 32 bits, and change it back to a 32-bit number if it fits. The
155 number we are looking for is expected to be positive, but if it
156 fits into 32 bits as an unsigned number, we let it be a 32-bit
157 number. The cavalier approach is for speed in ordinary cases. */
158 /* This has been extended for 64 bits. We blindly assume that if
159 you're compiling in 64-bit mode, the target is a 64-bit machine.
160 This should be cleaned up. */
164 #else /* includes non-bfd case, mostly */
168 if (flag_mri
&& radix
== 0)
172 /* In MRI mode, the number may have a suffix indicating the
173 radix. For that matter, it might actually be a floating
175 for (suffix
= input_line_pointer
; isalnum (*suffix
); suffix
++)
177 if (*suffix
== 'e' || *suffix
== 'E')
181 if (suffix
== input_line_pointer
)
195 else if (c
== 'O' || c
== 'Q')
199 else if (suffix
[1] == '.' || c
== 'E' || flt
)
201 floating_constant (expressionP
);
216 too_many_digits
= valuesize
+ 1;
220 too_many_digits
= (valuesize
+ 2) / 3 + 1;
224 too_many_digits
= (valuesize
+ 3) / 4 + 1;
228 too_many_digits
= (valuesize
+ 12) / 4; /* very rough */
231 start
= input_line_pointer
;
232 c
= *input_line_pointer
++;
234 (digit
= hex_value (c
)) < maxdig
;
235 c
= *input_line_pointer
++)
237 number
= number
* radix
+ digit
;
239 /* c contains character after number. */
240 /* input_line_pointer->char after c. */
241 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
245 * we saw a lot of digits. manufacture a bignum the hard way.
247 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
248 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
251 leader
= generic_bignum
;
252 generic_bignum
[0] = 0;
253 generic_bignum
[1] = 0;
254 input_line_pointer
= start
; /*->1st digit. */
255 c
= *input_line_pointer
++;
257 (carry
= hex_value (c
)) < maxdig
;
258 c
= *input_line_pointer
++)
260 for (pointer
= generic_bignum
;
266 work
= carry
+ radix
* *pointer
;
267 *pointer
= work
& LITTLENUM_MASK
;
268 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
272 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
274 /* room to grow a longer bignum. */
279 /* again, c is char after number, */
280 /* input_line_pointer->after c. */
281 know (LITTLENUM_NUMBER_OF_BITS
== 16);
282 if (leader
< generic_bignum
+ 2)
284 /* will fit into 32 bits. */
286 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
287 | (generic_bignum
[0] & LITTLENUM_MASK
);
292 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
296 if (flag_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
297 c
= *input_line_pointer
++;
302 * here with number, in correct radix. c is the next char.
303 * note that unlike un*x, we allow "011f" "0x9f" to
304 * both mean the same as the (conventional) "9f". this is simply easier
305 * than checking for strict canonical form. syntax sux!
308 if (LOCAL_LABELS_FB
&& c
== 'b')
311 * backward ref to local label.
312 * because it is backward, expect it to be defined.
314 /* Construct a local label. */
315 name
= fb_label_name ((int) number
, 0);
317 /* seen before, or symbol is defined: ok */
318 symbolP
= symbol_find (name
);
319 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
321 /* local labels are never absolute. don't waste time
322 checking absoluteness. */
323 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
325 expressionP
->X_op
= O_symbol
;
326 expressionP
->X_add_symbol
= symbolP
;
330 /* either not seen or not defined. */
331 /* @@ Should print out the original string instead of
332 the parsed number. */
333 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
335 expressionP
->X_op
= O_constant
;
338 expressionP
->X_add_number
= 0;
340 else if (LOCAL_LABELS_FB
&& c
== 'f')
343 * forward reference. expect symbol to be undefined or
344 * unknown. undefined: seen it before. unknown: never seen
346 * construct a local label name, then an undefined symbol.
347 * don't create a xseg frag for it: caller may do that.
348 * just return it as never seen before.
350 name
= fb_label_name ((int) number
, 1);
351 symbolP
= symbol_find_or_make (name
);
352 /* we have no need to check symbol properties. */
353 #ifndef many_segments
354 /* since "know" puts its arg into a "string", we
355 can't have newlines in the argument. */
356 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
358 expressionP
->X_op
= O_symbol
;
359 expressionP
->X_add_symbol
= symbolP
;
360 expressionP
->X_add_number
= 0;
362 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
364 /* If the dollar label is *currently* defined, then this is just
365 another reference to it. If it is not *currently* defined,
366 then this is a fresh instantiation of that number, so create
369 if (dollar_label_defined ((long) number
))
371 name
= dollar_label_name ((long) number
, 0);
372 symbolP
= symbol_find (name
);
373 know (symbolP
!= NULL
);
377 name
= dollar_label_name ((long) number
, 1);
378 symbolP
= symbol_find_or_make (name
);
381 expressionP
->X_op
= O_symbol
;
382 expressionP
->X_add_symbol
= symbolP
;
383 expressionP
->X_add_number
= 0;
387 expressionP
->X_op
= O_constant
;
388 #ifdef TARGET_WORD_SIZE
389 /* Sign extend NUMBER. */
390 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
392 expressionP
->X_add_number
= number
;
393 input_line_pointer
--; /* restore following character. */
394 } /* really just a number */
398 /* not a small number */
399 expressionP
->X_op
= O_big
;
400 expressionP
->X_add_number
= number
; /* number of littlenums */
401 input_line_pointer
--; /*->char following number. */
405 /* Parse an MRI multi character constant. */
408 mri_char_constant (expressionP
)
409 expressionS
*expressionP
;
413 if (*input_line_pointer
== '\''
414 && input_line_pointer
[1] != '\'')
416 expressionP
->X_op
= O_constant
;
417 expressionP
->X_add_number
= 0;
421 /* In order to get the correct byte ordering, we must build the
422 number in reverse. */
423 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
427 generic_bignum
[i
] = 0;
428 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
430 if (*input_line_pointer
== '\'')
432 if (input_line_pointer
[1] != '\'')
434 ++input_line_pointer
;
436 generic_bignum
[i
] <<= 8;
437 generic_bignum
[i
] += *input_line_pointer
;
438 ++input_line_pointer
;
441 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
443 /* If there is more than one littlenum, left justify the
444 last one to make it match the earlier ones. If there is
445 only one, we can just use the value directly. */
446 for (; j
< CHARS_PER_LITTLENUM
; j
++)
447 generic_bignum
[i
] <<= 8;
450 if (*input_line_pointer
== '\''
451 && input_line_pointer
[1] != '\'')
457 as_bad ("Character constant too large");
466 c
= SIZE_OF_LARGE_NUMBER
- i
;
467 for (j
= 0; j
< c
; j
++)
468 generic_bignum
[j
] = generic_bignum
[i
+ j
];
472 know (LITTLENUM_NUMBER_OF_BITS
== 16);
475 expressionP
->X_op
= O_big
;
476 expressionP
->X_add_number
= i
;
480 expressionP
->X_op
= O_constant
;
482 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
484 expressionP
->X_add_number
=
485 (((generic_bignum
[1] & LITTLENUM_MASK
)
486 << LITTLENUM_NUMBER_OF_BITS
)
487 | (generic_bignum
[0] & LITTLENUM_MASK
));
490 /* Skip the final closing quote. */
491 ++input_line_pointer
;
495 * Summary of operand().
497 * in: Input_line_pointer points to 1st char of operand, which may
500 * out: A expressionS.
501 * The operand may have been empty: in this case X_op == O_absent.
502 * Input_line_pointer->(next non-blank) char after operand.
506 operand (expressionP
)
507 expressionS
*expressionP
;
510 symbolS
*symbolP
; /* points to symbol */
511 char *name
; /* points to name of symbol */
514 /* All integers are regarded as unsigned unless they are negated.
515 This is because the only thing which cares whether a number is
516 unsigned is the code in emit_expr which extends constants into
517 bignums. It should only sign extend negative numbers, so that
518 something like ``.quad 0x80000000'' is not sign extended even
519 though it appears negative if valueT is 32 bits. */
520 expressionP
->X_unsigned
= 1;
522 /* digits, assume it is a bignum. */
524 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
525 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
538 input_line_pointer
--;
540 integer_constant (flag_mri
? 0 : 10, expressionP
);
544 /* non-decimal radix */
550 /* Check for a hex constant. */
551 for (s
= input_line_pointer
; hex_p (*s
); s
++)
553 if (*s
== 'h' || *s
== 'H')
555 --input_line_pointer
;
556 integer_constant (0, expressionP
);
561 c
= *input_line_pointer
;
566 if (c
&& strchr (FLT_CHARS
, c
))
568 input_line_pointer
++;
569 floating_constant (expressionP
);
570 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
574 /* The string was only zero */
575 expressionP
->X_op
= O_constant
;
576 expressionP
->X_add_number
= 0;
585 input_line_pointer
++;
586 integer_constant (16, expressionP
);
592 switch (input_line_pointer
[1])
596 /* If unambiguously a difference expression, treat
597 it as one by indicating a label; otherwise, it's
598 always a binary number. */
600 char *cp
= input_line_pointer
+ 1;
601 while (strchr ("0123456789", *++cp
))
603 if (*cp
== 'b' || *cp
== 'f')
608 /* Some of our code elsewhere does permit digits
609 greater than the expected base; for consistency,
611 case '2': case '3': case '4': case '5':
612 case '6': case '7': case '8': case '9':
620 input_line_pointer
--;
621 integer_constant (10, expressionP
);
627 input_line_pointer
++;
630 integer_constant (2, expressionP
);
641 integer_constant (flag_mri
? 0 : 8, expressionP
);
647 /* If it says "0f" and it could possibly be a floating point
648 number, make it one. Otherwise, make it a local label,
649 and try to deal with parsing the rest later. */
650 if (!input_line_pointer
[1]
651 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
654 char *cp
= input_line_pointer
+ 1;
655 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
656 &generic_floating_point_number
);
660 case ERROR_EXPONENT_OVERFLOW
:
661 if (*cp
== 'f' || *cp
== 'b')
662 /* looks like a difference expression */
667 as_fatal ("expr.c(operand): bad atof_generic return val %d",
672 /* Okay, now we've sorted it out. We resume at one of these
673 two labels, depending on what we've decided we're probably
676 input_line_pointer
--;
677 integer_constant (10, expressionP
);
693 input_line_pointer
++;
694 floating_constant (expressionP
);
695 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
699 if (LOCAL_LABELS_DOLLAR
)
701 integer_constant (10, expressionP
);
712 /* didn't begin with digit & not a name */
713 segment
= expression (expressionP
);
714 /* Expression() will pass trailing whitespace */
715 if ((c
== '(' && *input_line_pointer
++ != ')')
716 || (c
== '[' && *input_line_pointer
++ != ']'))
718 as_bad ("Missing ')' assumed");
719 input_line_pointer
--;
721 /* here with input_line_pointer->char after "(...)" */
725 if (! flag_mri
|| *input_line_pointer
!= '\'')
727 as_bad ("EBCDIC constants are not supported");
730 if (! flag_mri
|| *input_line_pointer
!= '\'')
732 ++input_line_pointer
;
737 /* Warning: to conform to other people's assemblers NO
738 ESCAPEMENT is permitted for a single quote. The next
739 character, parity errors and all, is taken as the value
740 of the operand. VERY KINKY. */
741 expressionP
->X_op
= O_constant
;
742 expressionP
->X_add_number
= *input_line_pointer
++;
746 mri_char_constant (expressionP
);
750 (void) operand (expressionP
);
754 /* Double quote is the logical not operator in MRI mode. */
761 operand (expressionP
);
762 if (expressionP
->X_op
== O_constant
)
764 /* input_line_pointer -> char after operand */
767 expressionP
->X_add_number
= - expressionP
->X_add_number
;
768 /* Notice: '-' may overflow: no warning is given. This is
769 compatible with other people's assemblers. Sigh. */
770 expressionP
->X_unsigned
= 0;
773 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
775 else if (expressionP
->X_op
!= O_illegal
776 && expressionP
->X_op
!= O_absent
)
778 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
780 expressionP
->X_op
= O_uminus
;
782 expressionP
->X_op
= O_bit_not
;
783 expressionP
->X_add_number
= 0;
786 as_warn ("Unary operator %c ignored because bad operand follows",
792 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
798 if (flag_mri
&& hex_p (*input_line_pointer
))
800 /* In MRI mode, $ is also used as the prefix for a
801 hexadecimal constant. */
802 integer_constant (16, expressionP
);
807 if (!is_part_of_name (*input_line_pointer
))
811 /* JF: '.' is pseudo symbol with value of current location
812 in current segment. */
813 fake
= FAKE_LABEL_NAME
;
814 symbolP
= symbol_new (fake
,
816 (valueT
) frag_now_fix (),
819 expressionP
->X_op
= O_symbol
;
820 expressionP
->X_add_symbol
= symbolP
;
821 expressionP
->X_add_number
= 0;
832 /* can't imagine any other kind of operand */
833 expressionP
->X_op
= O_absent
;
834 input_line_pointer
--;
840 integer_constant (2, expressionP
);
846 integer_constant (8, expressionP
);
853 /* In MRI mode, this is a floating point constant represented
854 using hexadecimal digits. */
856 ++input_line_pointer
;
857 integer_constant (16, expressionP
);
862 if (is_end_of_line
[(unsigned char) c
])
864 if (is_name_beginner (c
)) /* here if did not begin with a digit */
867 * Identifier begins here.
868 * This is kludged for speed, so code is repeated.
871 name
= --input_line_pointer
;
872 c
= get_symbol_end ();
873 symbolP
= symbol_find_or_make (name
);
875 /* If we have an absolute symbol or a reg, then we know its
877 segment
= S_GET_SEGMENT (symbolP
);
878 if (segment
== absolute_section
)
880 expressionP
->X_op
= O_constant
;
881 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
883 else if (segment
== reg_section
)
885 expressionP
->X_op
= O_register
;
886 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
890 expressionP
->X_op
= O_symbol
;
891 expressionP
->X_add_symbol
= symbolP
;
892 expressionP
->X_add_number
= 0;
894 *input_line_pointer
= c
;
898 /* Let the target try to parse it. Success is indicated by changing
899 the X_op field to something other than O_absent and pointing
900 input_line_pointer passed the expression. If it can't parse the
901 expression, X_op and input_line_pointer should be unchanged. */
902 expressionP
->X_op
= O_absent
;
903 --input_line_pointer
;
904 md_operand (expressionP
);
905 if (expressionP
->X_op
== O_absent
)
907 ++input_line_pointer
;
908 as_bad ("Bad expression");
909 expressionP
->X_op
= O_constant
;
910 expressionP
->X_add_number
= 0;
917 * It is more 'efficient' to clean up the expressionS when they are created.
918 * Doing it here saves lines of code.
920 clean_up_expression (expressionP
);
921 SKIP_WHITESPACE (); /*->1st char after operand. */
922 know (*input_line_pointer
!= ' ');
924 /* The PA port needs this information. */
925 if (expressionP
->X_add_symbol
)
926 expressionP
->X_add_symbol
->sy_used
= 1;
928 switch (expressionP
->X_op
)
931 return absolute_section
;
933 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
939 /* Internal. Simplify a struct expression for use by expr() */
942 * In: address of a expressionS.
943 * The X_op field of the expressionS may only take certain values.
944 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
945 * Out: expressionS may have been modified:
946 * 'foo-foo' symbol references cancelled to 0,
947 * which changes X_op from O_subtract to O_constant.
948 * Unused fields zeroed to help expr().
952 clean_up_expression (expressionP
)
953 expressionS
*expressionP
;
955 switch (expressionP
->X_op
)
959 expressionP
->X_add_number
= 0;
964 expressionP
->X_add_symbol
= NULL
;
969 expressionP
->X_op_symbol
= NULL
;
972 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
973 || ((expressionP
->X_op_symbol
->sy_frag
974 == expressionP
->X_add_symbol
->sy_frag
)
975 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
976 && (S_GET_VALUE (expressionP
->X_op_symbol
)
977 == S_GET_VALUE (expressionP
->X_add_symbol
))))
979 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
980 - S_GET_VALUE (expressionP
->X_op_symbol
));
982 expressionP
->X_op
= O_constant
;
983 expressionP
->X_add_symbol
= NULL
;
984 expressionP
->X_op_symbol
= NULL
;
985 expressionP
->X_add_number
+= diff
;
993 /* Expression parser. */
996 * We allow an empty expression, and just assume (absolute,0) silently.
997 * Unary operators and parenthetical expressions are treated as operands.
998 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1000 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1001 * warped that I flushed it and wrote a recursive-descent parser instead.
1002 * Now things are stable, would anybody like to write a fast parser?
1003 * Most expressions are either register (which does not even reach here)
1004 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1005 * So I guess it doesn't really matter how inefficient more complex expressions
1008 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1009 * Also, we have consumed any leading or trailing spaces (operand does that)
1010 * and done all intervening operators.
1012 * This returns the segment of the result, which will be
1013 * absolute_section or the segment of a symbol.
1017 #define __ O_illegal
1019 static const operatorT op_encoding
[256] =
1020 { /* maps ASCII->operators */
1022 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1023 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1025 __
, O_bit_or_not
, O_bit_not
, __
, __
, O_modulus
, O_bit_and
, __
,
1026 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1027 __
, __
, __
, __
, __
, __
, __
, __
,
1028 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1029 __
, __
, __
, __
, __
, __
, __
, __
,
1030 __
, __
, __
, __
, __
, __
, __
, __
,
1031 __
, __
, __
, __
, __
, __
, __
, __
,
1032 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1033 __
, __
, __
, __
, __
, __
, __
, __
,
1034 __
, __
, __
, __
, __
, __
, __
, __
,
1035 __
, __
, __
, __
, __
, __
, __
, __
,
1036 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1038 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1039 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1040 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1041 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1042 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1043 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1044 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1045 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1051 * 0 operand, (expression)
1054 * 3 used for * / % in MRI mode
1059 static operator_rankT op_rank
[] =
1072 5, /* O_left_shift */
1073 5, /* O_right_shift */
1074 4, /* O_bit_inclusive_or */
1075 4, /* O_bit_or_not */
1076 4, /* O_bit_exclusive_or */
1088 /* Initialize the expression parser. */
1093 /* In MRI mode, multiplication and division have lower precedence
1094 than the bit wise operators. */
1097 op_rank
[O_multiply
] = 3;
1098 op_rank
[O_divide
] = 3;
1099 op_rank
[O_modulus
] = 3;
1103 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1104 Advance INPUT_LINE_POINTER to the last character in the operator
1105 (i.e., don't change it for a single character operator). */
1107 static inline operatorT
1113 c
= *input_line_pointer
;
1118 return op_encoding
[c
];
1121 switch (input_line_pointer
[1])
1124 return op_encoding
[c
];
1135 ++input_line_pointer
;
1139 switch (input_line_pointer
[1])
1142 return op_encoding
[c
];
1144 ret
= O_right_shift
;
1150 ++input_line_pointer
;
1154 /* We accept !! as equivalent to ^ for MRI compatibility. */
1155 if (input_line_pointer
[1] != '!')
1158 return O_bit_inclusive_or
;
1159 return op_encoding
[c
];
1161 ++input_line_pointer
;
1162 return O_bit_exclusive_or
;
1168 /* Parse an expression. */
1171 expr (rank
, resultP
)
1172 operator_rankT rank
; /* Larger # is higher rank. */
1173 expressionS
*resultP
; /* Deliver result here. */
1182 retval
= operand (resultP
);
1184 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1186 op_left
= operator ();
1187 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1191 input_line_pointer
++; /*->after 1st character of operator. */
1193 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1194 if (right
.X_op
== O_absent
)
1196 as_warn ("missing operand; zero assumed");
1197 right
.X_op
= O_constant
;
1198 right
.X_add_number
= 0;
1199 right
.X_add_symbol
= NULL
;
1200 right
.X_op_symbol
= NULL
;
1203 know (*input_line_pointer
!= ' ');
1205 if (retval
== undefined_section
)
1207 if (SEG_NORMAL (rightseg
))
1210 else if (! SEG_NORMAL (retval
))
1212 else if (SEG_NORMAL (rightseg
)
1213 && retval
!= rightseg
1215 && op_left
!= O_subtract
1218 as_bad ("operation combines symbols in different segments");
1220 op_right
= operator ();
1222 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1223 know ((int) op_left
>= (int) O_multiply
&& (int) op_left
<= (int) O_subtract
);
1225 /* input_line_pointer->after right-hand quantity. */
1226 /* left-hand quantity in resultP */
1227 /* right-hand quantity in right. */
1228 /* operator in op_left. */
1230 if (resultP
->X_op
== O_big
)
1232 as_warn ("left operand is a %s; integer 0 assumed",
1233 resultP
->X_add_number
> 0 ? "bignum" : "float");
1234 resultP
->X_op
= O_constant
;
1235 resultP
->X_add_number
= 0;
1236 resultP
->X_add_symbol
= NULL
;
1237 resultP
->X_op_symbol
= NULL
;
1239 if (right
.X_op
== O_big
)
1241 as_warn ("right operand is a %s; integer 0 assumed",
1242 right
.X_add_number
> 0 ? "bignum" : "float");
1243 right
.X_op
= O_constant
;
1244 right
.X_add_number
= 0;
1245 right
.X_add_symbol
= NULL
;
1246 right
.X_op_symbol
= NULL
;
1249 /* Optimize common cases. */
1250 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1253 resultP
->X_add_number
+= right
.X_add_number
;
1255 /* This case comes up in PIC code. */
1256 else if (op_left
== O_subtract
1257 && right
.X_op
== O_symbol
1258 && resultP
->X_op
== O_symbol
1259 && (right
.X_add_symbol
->sy_frag
1260 == resultP
->X_add_symbol
->sy_frag
)
1261 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1264 resultP
->X_add_number
+= right
.X_add_number
;
1265 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1266 - S_GET_VALUE (right
.X_add_symbol
));
1267 resultP
->X_op
= O_constant
;
1268 resultP
->X_add_symbol
= 0;
1270 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1273 resultP
->X_add_number
-= right
.X_add_number
;
1275 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1278 resultP
->X_op
= right
.X_op
;
1279 resultP
->X_add_symbol
= right
.X_add_symbol
;
1280 resultP
->X_op_symbol
= right
.X_op_symbol
;
1281 resultP
->X_add_number
+= right
.X_add_number
;
1284 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1286 /* Constant OP constant. */
1287 offsetT v
= right
.X_add_number
;
1288 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1290 as_warn ("division by zero");
1296 case O_multiply
: resultP
->X_add_number
*= v
; break;
1297 case O_divide
: resultP
->X_add_number
/= v
; break;
1298 case O_modulus
: resultP
->X_add_number
%= v
; break;
1299 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1300 case O_right_shift
: resultP
->X_add_number
>>= v
; break;
1301 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1302 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1303 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1304 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1305 case O_add
: resultP
->X_add_number
+= v
; break;
1306 case O_subtract
: resultP
->X_add_number
-= v
; break;
1308 resultP
->X_add_number
=
1309 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1312 resultP
->X_add_number
=
1313 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1316 resultP
->X_add_number
=
1317 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1320 resultP
->X_add_number
=
1321 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1324 resultP
->X_add_number
=
1325 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1328 resultP
->X_add_number
=
1329 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1333 else if (resultP
->X_op
== O_symbol
1334 && right
.X_op
== O_symbol
1335 && (op_left
== O_add
1336 || op_left
== O_subtract
1337 || (resultP
->X_add_number
== 0
1338 && right
.X_add_number
== 0)))
1340 /* Symbol OP symbol. */
1341 resultP
->X_op
= op_left
;
1342 resultP
->X_op_symbol
= right
.X_add_symbol
;
1343 if (op_left
== O_add
)
1344 resultP
->X_add_number
+= right
.X_add_number
;
1345 else if (op_left
== O_subtract
)
1346 resultP
->X_add_number
-= right
.X_add_number
;
1350 /* The general case. */
1351 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1352 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1353 resultP
->X_op
= op_left
;
1354 resultP
->X_add_number
= 0;
1355 resultP
->X_unsigned
= 1;
1359 } /* While next operator is >= this rank. */
1361 /* The PA port needs this information. */
1362 if (resultP
->X_add_symbol
)
1363 resultP
->X_add_symbol
->sy_used
= 1;
1365 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1371 * This lives here because it belongs equally in expr.c & read.c.
1372 * Expr.c is just a branch office read.c anyway, and putting it
1373 * here lessens the crowd at read.c.
1375 * Assume input_line_pointer is at start of symbol name.
1376 * Advance input_line_pointer past symbol name.
1377 * Turn that character into a '\0', returning its former value.
1378 * This allows a string compare (RMS wants symbol names to be strings)
1379 * of the symbol name.
1380 * There will always be a char following symbol name, because all good
1381 * lines end in end-of-line.
1388 while (is_part_of_name (c
= *input_line_pointer
++))
1390 *--input_line_pointer
= 0;
1396 get_single_number ()
1400 return exp
.X_add_number
;