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
;
568 integer_constant (0, expressionP
);
574 if (c
&& strchr (FLT_CHARS
, c
))
576 input_line_pointer
++;
577 floating_constant (expressionP
);
578 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
582 /* The string was only zero */
583 expressionP
->X_op
= O_constant
;
584 expressionP
->X_add_number
= 0;
593 input_line_pointer
++;
594 integer_constant (16, expressionP
);
600 switch (input_line_pointer
[1])
604 /* If unambiguously a difference expression, treat
605 it as one by indicating a label; otherwise, it's
606 always a binary number. */
608 char *cp
= input_line_pointer
+ 1;
609 while (strchr ("0123456789", *++cp
))
611 if (*cp
== 'b' || *cp
== 'f')
616 /* Some of our code elsewhere does permit digits
617 greater than the expected base; for consistency,
619 case '2': case '3': case '4': case '5':
620 case '6': case '7': case '8': case '9':
628 input_line_pointer
--;
629 integer_constant (10, expressionP
);
635 input_line_pointer
++;
638 integer_constant (2, expressionP
);
649 integer_constant (flag_mri
? 0 : 8, expressionP
);
655 /* If it says "0f" and it could possibly be a floating point
656 number, make it one. Otherwise, make it a local label,
657 and try to deal with parsing the rest later. */
658 if (!input_line_pointer
[1]
659 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
662 char *cp
= input_line_pointer
+ 1;
663 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
664 &generic_floating_point_number
);
668 case ERROR_EXPONENT_OVERFLOW
:
669 if (*cp
== 'f' || *cp
== 'b')
670 /* looks like a difference expression */
675 as_fatal ("expr.c(operand): bad atof_generic return val %d",
680 /* Okay, now we've sorted it out. We resume at one of these
681 two labels, depending on what we've decided we're probably
684 input_line_pointer
--;
685 integer_constant (10, expressionP
);
701 input_line_pointer
++;
702 floating_constant (expressionP
);
703 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
707 if (LOCAL_LABELS_DOLLAR
)
709 integer_constant (10, expressionP
);
720 /* didn't begin with digit & not a name */
721 segment
= expression (expressionP
);
722 /* Expression() will pass trailing whitespace */
723 if ((c
== '(' && *input_line_pointer
++ != ')')
724 || (c
== '[' && *input_line_pointer
++ != ']'))
726 as_bad ("Missing ')' assumed");
727 input_line_pointer
--;
729 /* here with input_line_pointer->char after "(...)" */
733 if (! flag_mri
|| *input_line_pointer
!= '\'')
735 as_bad ("EBCDIC constants are not supported");
738 if (! flag_mri
|| *input_line_pointer
!= '\'')
740 ++input_line_pointer
;
745 /* Warning: to conform to other people's assemblers NO
746 ESCAPEMENT is permitted for a single quote. The next
747 character, parity errors and all, is taken as the value
748 of the operand. VERY KINKY. */
749 expressionP
->X_op
= O_constant
;
750 expressionP
->X_add_number
= *input_line_pointer
++;
754 mri_char_constant (expressionP
);
758 (void) operand (expressionP
);
762 /* Double quote is the logical not operator in MRI mode. */
769 operand (expressionP
);
770 if (expressionP
->X_op
== O_constant
)
772 /* input_line_pointer -> char after operand */
775 expressionP
->X_add_number
= - expressionP
->X_add_number
;
776 /* Notice: '-' may overflow: no warning is given. This is
777 compatible with other people's assemblers. Sigh. */
778 expressionP
->X_unsigned
= 0;
781 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
783 else if (expressionP
->X_op
!= O_illegal
784 && expressionP
->X_op
!= O_absent
)
786 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
788 expressionP
->X_op
= O_uminus
;
790 expressionP
->X_op
= O_bit_not
;
791 expressionP
->X_add_number
= 0;
794 as_warn ("Unary operator %c ignored because bad operand follows",
800 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
806 if (flag_mri
&& hex_p (*input_line_pointer
))
808 /* In MRI mode, $ is also used as the prefix for a
809 hexadecimal constant. */
810 integer_constant (16, expressionP
);
815 if (!is_part_of_name (*input_line_pointer
))
819 /* JF: '.' is pseudo symbol with value of current location
820 in current segment. */
821 fake
= FAKE_LABEL_NAME
;
822 symbolP
= symbol_new (fake
,
824 (valueT
) frag_now_fix (),
827 expressionP
->X_op
= O_symbol
;
828 expressionP
->X_add_symbol
= symbolP
;
829 expressionP
->X_add_number
= 0;
832 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
833 && ! is_part_of_name (input_line_pointer
[8]))
834 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
835 && ! is_part_of_name (input_line_pointer
[7])))
839 start
= (input_line_pointer
[1] == 't'
840 || input_line_pointer
[1] == 'T');
841 input_line_pointer
+= start
? 8 : 7;
843 if (*input_line_pointer
!= '(')
844 as_bad ("syntax error in .startof. or .sizeof.");
849 ++input_line_pointer
;
851 name
= input_line_pointer
;
852 c
= get_symbol_end ();
854 buf
= (char *) xmalloc (strlen (name
) + 10);
856 sprintf (buf
, ".startof.%s", name
);
858 sprintf (buf
, ".sizeof.%s", name
);
859 symbolP
= symbol_make (buf
);
862 expressionP
->X_op
= O_symbol
;
863 expressionP
->X_add_symbol
= symbolP
;
864 expressionP
->X_add_number
= 0;
866 *input_line_pointer
= c
;
868 if (*input_line_pointer
!= ')')
869 as_bad ("syntax error in .startof. or .sizeof.");
871 ++input_line_pointer
;
883 /* can't imagine any other kind of operand */
884 expressionP
->X_op
= O_absent
;
885 input_line_pointer
--;
891 integer_constant (2, expressionP
);
897 integer_constant (8, expressionP
);
904 /* In MRI mode, this is a floating point constant represented
905 using hexadecimal digits. */
907 ++input_line_pointer
;
908 integer_constant (16, expressionP
);
913 if (is_end_of_line
[(unsigned char) c
])
915 if (is_name_beginner (c
)) /* here if did not begin with a digit */
918 * Identifier begins here.
919 * This is kludged for speed, so code is repeated.
922 name
= --input_line_pointer
;
923 c
= get_symbol_end ();
924 symbolP
= symbol_find_or_make (name
);
926 /* If we have an absolute symbol or a reg, then we know its
928 segment
= S_GET_SEGMENT (symbolP
);
929 if (segment
== absolute_section
)
931 expressionP
->X_op
= O_constant
;
932 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
934 else if (segment
== reg_section
)
936 expressionP
->X_op
= O_register
;
937 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
941 expressionP
->X_op
= O_symbol
;
942 expressionP
->X_add_symbol
= symbolP
;
943 expressionP
->X_add_number
= 0;
945 *input_line_pointer
= c
;
949 /* Let the target try to parse it. Success is indicated by changing
950 the X_op field to something other than O_absent and pointing
951 input_line_pointer passed the expression. If it can't parse the
952 expression, X_op and input_line_pointer should be unchanged. */
953 expressionP
->X_op
= O_absent
;
954 --input_line_pointer
;
955 md_operand (expressionP
);
956 if (expressionP
->X_op
== O_absent
)
958 ++input_line_pointer
;
959 as_bad ("Bad expression");
960 expressionP
->X_op
= O_constant
;
961 expressionP
->X_add_number
= 0;
968 * It is more 'efficient' to clean up the expressionS when they are created.
969 * Doing it here saves lines of code.
971 clean_up_expression (expressionP
);
972 SKIP_WHITESPACE (); /*->1st char after operand. */
973 know (*input_line_pointer
!= ' ');
975 /* The PA port needs this information. */
976 if (expressionP
->X_add_symbol
)
977 expressionP
->X_add_symbol
->sy_used
= 1;
979 switch (expressionP
->X_op
)
982 return absolute_section
;
984 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
990 /* Internal. Simplify a struct expression for use by expr() */
993 * In: address of a expressionS.
994 * The X_op field of the expressionS may only take certain values.
995 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
996 * Out: expressionS may have been modified:
997 * 'foo-foo' symbol references cancelled to 0,
998 * which changes X_op from O_subtract to O_constant.
999 * Unused fields zeroed to help expr().
1003 clean_up_expression (expressionP
)
1004 expressionS
*expressionP
;
1006 switch (expressionP
->X_op
)
1010 expressionP
->X_add_number
= 0;
1015 expressionP
->X_add_symbol
= NULL
;
1020 expressionP
->X_op_symbol
= NULL
;
1023 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1024 || ((expressionP
->X_op_symbol
->sy_frag
1025 == expressionP
->X_add_symbol
->sy_frag
)
1026 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1027 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1028 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1030 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1031 - S_GET_VALUE (expressionP
->X_op_symbol
));
1033 expressionP
->X_op
= O_constant
;
1034 expressionP
->X_add_symbol
= NULL
;
1035 expressionP
->X_op_symbol
= NULL
;
1036 expressionP
->X_add_number
+= diff
;
1044 /* Expression parser. */
1047 * We allow an empty expression, and just assume (absolute,0) silently.
1048 * Unary operators and parenthetical expressions are treated as operands.
1049 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1051 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1052 * warped that I flushed it and wrote a recursive-descent parser instead.
1053 * Now things are stable, would anybody like to write a fast parser?
1054 * Most expressions are either register (which does not even reach here)
1055 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1056 * So I guess it doesn't really matter how inefficient more complex expressions
1059 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1060 * Also, we have consumed any leading or trailing spaces (operand does that)
1061 * and done all intervening operators.
1063 * This returns the segment of the result, which will be
1064 * absolute_section or the segment of a symbol.
1068 #define __ O_illegal
1070 static operatorT op_encoding
[256] =
1071 { /* maps ASCII->operators */
1073 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1074 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1076 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1077 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1078 __
, __
, __
, __
, __
, __
, __
, __
,
1079 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1080 __
, __
, __
, __
, __
, __
, __
, __
,
1081 __
, __
, __
, __
, __
, __
, __
, __
,
1082 __
, __
, __
, __
, __
, __
, __
, __
,
1083 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1084 __
, __
, __
, __
, __
, __
, __
, __
,
1085 __
, __
, __
, __
, __
, __
, __
, __
,
1086 __
, __
, __
, __
, __
, __
, __
, __
,
1087 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1089 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1090 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1091 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1092 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1093 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1094 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1095 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1096 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1102 * 0 operand, (expression)
1105 * 3 used for * / % in MRI mode
1110 static operator_rankT op_rank
[] =
1123 5, /* O_left_shift */
1124 5, /* O_right_shift */
1125 4, /* O_bit_inclusive_or */
1126 4, /* O_bit_or_not */
1127 4, /* O_bit_exclusive_or */
1139 /* Initialize the expression parser. */
1144 /* In MRI mode, multiplication and division have lower precedence
1145 than the bit wise operators. */
1148 op_rank
[O_multiply
] = 3;
1149 op_rank
[O_divide
] = 3;
1150 op_rank
[O_modulus
] = 3;
1151 op_encoding
['"'] = O_bit_not
;
1155 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1156 Advance INPUT_LINE_POINTER to the last character in the operator
1157 (i.e., don't change it for a single character operator). */
1159 static inline operatorT
1165 c
= *input_line_pointer
;
1170 return op_encoding
[c
];
1173 switch (input_line_pointer
[1])
1176 return op_encoding
[c
];
1187 ++input_line_pointer
;
1191 switch (input_line_pointer
[1])
1194 return op_encoding
[c
];
1196 ret
= O_right_shift
;
1202 ++input_line_pointer
;
1206 /* We accept !! as equivalent to ^ for MRI compatibility. */
1207 if (input_line_pointer
[1] != '!')
1210 return O_bit_inclusive_or
;
1211 return op_encoding
[c
];
1213 ++input_line_pointer
;
1214 return O_bit_exclusive_or
;
1220 /* Parse an expression. */
1223 expr (rank
, resultP
)
1224 operator_rankT rank
; /* Larger # is higher rank. */
1225 expressionS
*resultP
; /* Deliver result here. */
1234 retval
= operand (resultP
);
1236 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1238 op_left
= operator ();
1239 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1243 input_line_pointer
++; /*->after 1st character of operator. */
1245 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1246 if (right
.X_op
== O_absent
)
1248 as_warn ("missing operand; zero assumed");
1249 right
.X_op
= O_constant
;
1250 right
.X_add_number
= 0;
1251 right
.X_add_symbol
= NULL
;
1252 right
.X_op_symbol
= NULL
;
1255 know (*input_line_pointer
!= ' ');
1257 if (retval
== undefined_section
)
1259 if (SEG_NORMAL (rightseg
))
1262 else if (! SEG_NORMAL (retval
))
1264 else if (SEG_NORMAL (rightseg
)
1265 && retval
!= rightseg
1267 && op_left
!= O_subtract
1270 as_bad ("operation combines symbols in different segments");
1272 op_right
= operator ();
1274 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1275 know ((int) op_left
>= (int) O_multiply
&& (int) op_left
<= (int) O_subtract
);
1277 /* input_line_pointer->after right-hand quantity. */
1278 /* left-hand quantity in resultP */
1279 /* right-hand quantity in right. */
1280 /* operator in op_left. */
1282 if (resultP
->X_op
== O_big
)
1284 as_warn ("left operand is a %s; integer 0 assumed",
1285 resultP
->X_add_number
> 0 ? "bignum" : "float");
1286 resultP
->X_op
= O_constant
;
1287 resultP
->X_add_number
= 0;
1288 resultP
->X_add_symbol
= NULL
;
1289 resultP
->X_op_symbol
= NULL
;
1291 if (right
.X_op
== O_big
)
1293 as_warn ("right operand is a %s; integer 0 assumed",
1294 right
.X_add_number
> 0 ? "bignum" : "float");
1295 right
.X_op
= O_constant
;
1296 right
.X_add_number
= 0;
1297 right
.X_add_symbol
= NULL
;
1298 right
.X_op_symbol
= NULL
;
1301 /* Optimize common cases. */
1302 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1305 resultP
->X_add_number
+= right
.X_add_number
;
1307 /* This case comes up in PIC code. */
1308 else if (op_left
== O_subtract
1309 && right
.X_op
== O_symbol
1310 && resultP
->X_op
== O_symbol
1311 && (right
.X_add_symbol
->sy_frag
1312 == resultP
->X_add_symbol
->sy_frag
)
1313 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1316 resultP
->X_add_number
+= right
.X_add_number
;
1317 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1318 - S_GET_VALUE (right
.X_add_symbol
));
1319 resultP
->X_op
= O_constant
;
1320 resultP
->X_add_symbol
= 0;
1322 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1325 resultP
->X_add_number
-= right
.X_add_number
;
1327 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1330 resultP
->X_op
= right
.X_op
;
1331 resultP
->X_add_symbol
= right
.X_add_symbol
;
1332 resultP
->X_op_symbol
= right
.X_op_symbol
;
1333 resultP
->X_add_number
+= right
.X_add_number
;
1336 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1338 /* Constant OP constant. */
1339 offsetT v
= right
.X_add_number
;
1340 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1342 as_warn ("division by zero");
1348 case O_multiply
: resultP
->X_add_number
*= v
; break;
1349 case O_divide
: resultP
->X_add_number
/= v
; break;
1350 case O_modulus
: resultP
->X_add_number
%= v
; break;
1351 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1352 case O_right_shift
: resultP
->X_add_number
>>= v
; break;
1353 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1354 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1355 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1356 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1357 case O_add
: resultP
->X_add_number
+= v
; break;
1358 case O_subtract
: resultP
->X_add_number
-= v
; break;
1360 resultP
->X_add_number
=
1361 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1364 resultP
->X_add_number
=
1365 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1368 resultP
->X_add_number
=
1369 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1372 resultP
->X_add_number
=
1373 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1376 resultP
->X_add_number
=
1377 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1380 resultP
->X_add_number
=
1381 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1385 else if (resultP
->X_op
== O_symbol
1386 && right
.X_op
== O_symbol
1387 && (op_left
== O_add
1388 || op_left
== O_subtract
1389 || (resultP
->X_add_number
== 0
1390 && right
.X_add_number
== 0)))
1392 /* Symbol OP symbol. */
1393 resultP
->X_op
= op_left
;
1394 resultP
->X_op_symbol
= right
.X_add_symbol
;
1395 if (op_left
== O_add
)
1396 resultP
->X_add_number
+= right
.X_add_number
;
1397 else if (op_left
== O_subtract
)
1398 resultP
->X_add_number
-= right
.X_add_number
;
1402 /* The general case. */
1403 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1404 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1405 resultP
->X_op
= op_left
;
1406 resultP
->X_add_number
= 0;
1407 resultP
->X_unsigned
= 1;
1411 } /* While next operator is >= this rank. */
1413 /* The PA port needs this information. */
1414 if (resultP
->X_add_symbol
)
1415 resultP
->X_add_symbol
->sy_used
= 1;
1417 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1423 * This lives here because it belongs equally in expr.c & read.c.
1424 * Expr.c is just a branch office read.c anyway, and putting it
1425 * here lessens the crowd at read.c.
1427 * Assume input_line_pointer is at start of symbol name.
1428 * Advance input_line_pointer past symbol name.
1429 * Turn that character into a '\0', returning its former value.
1430 * This allows a string compare (RMS wants symbol names to be strings)
1431 * of the symbol name.
1432 * There will always be a char following symbol name, because all good
1433 * lines end in end-of-line.
1440 /* We accept \001 in a name in case this is being called with a
1441 constructed string. */
1442 while (is_part_of_name (c
= *input_line_pointer
++)
1445 *--input_line_pointer
= 0;
1451 get_single_number ()
1455 return exp
.X_add_number
;