1 /* expr.c -operands, expressions-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* This is really a branch office of as-read.c. I split it out to clearly
24 distinguish the world of expressions from the world of statements.
25 (It also gives smaller files to re-compile.)
26 Here, "operand"s are of expressions, not instructions. */
29 #define min(a, b) ((a) < (b) ? (a) : (b))
32 #include "safe-ctype.h"
35 static void floating_constant
PARAMS ((expressionS
* expressionP
));
36 static valueT generic_bignum_to_int32
PARAMS ((void));
38 static valueT generic_bignum_to_int64
PARAMS ((void));
40 static void integer_constant
PARAMS ((int radix
, expressionS
* expressionP
));
41 static void mri_char_constant
PARAMS ((expressionS
*));
42 static void current_location
PARAMS ((expressionS
*));
43 static void clean_up_expression
PARAMS ((expressionS
* expressionP
));
44 static segT operand
PARAMS ((expressionS
*));
45 static operatorT
operator PARAMS ((int *));
47 extern const char EXP_CHARS
[], FLT_CHARS
[];
49 /* We keep a mapping of expression symbols to file positions, so that
50 we can provide better error messages. */
52 struct expr_symbol_line
{
53 struct expr_symbol_line
*next
;
59 static struct expr_symbol_line
*expr_symbol_lines
;
61 /* Build a dummy symbol to hold a complex expression. This is how we
62 build expressions up out of other expressions. The symbol is put
63 into the fake section expr_section. */
66 make_expr_symbol (expressionP
)
67 expressionS
*expressionP
;
72 struct expr_symbol_line
*n
;
74 if (expressionP
->X_op
== O_symbol
75 && expressionP
->X_add_number
== 0)
76 return expressionP
->X_add_symbol
;
78 if (expressionP
->X_op
== O_big
)
80 /* This won't work, because the actual value is stored in
81 generic_floating_point_number or generic_bignum, and we are
82 going to lose it if we haven't already. */
83 if (expressionP
->X_add_number
> 0)
84 as_bad (_("bignum invalid"));
86 as_bad (_("floating point number invalid"));
87 zero
.X_op
= O_constant
;
88 zero
.X_add_number
= 0;
90 clean_up_expression (&zero
);
94 fake
= FAKE_LABEL_NAME
;
96 /* Putting constant symbols in absolute_section rather than
97 expr_section is convenient for the old a.out code, for which
98 S_GET_SEGMENT does not always retrieve the value put in by
100 symbolP
= symbol_create (fake
,
101 (expressionP
->X_op
== O_constant
104 0, &zero_address_frag
);
105 symbol_set_value_expression (symbolP
, expressionP
);
107 if (expressionP
->X_op
== O_constant
)
108 resolve_symbol_value (symbolP
);
110 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
112 as_where (&n
->file
, &n
->line
);
113 n
->next
= expr_symbol_lines
;
114 expr_symbol_lines
= n
;
119 /* Return the file and line number for an expr symbol. Return
120 non-zero if something was found, 0 if no information is known for
124 expr_symbol_where (sym
, pfile
, pline
)
129 register struct expr_symbol_line
*l
;
131 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
144 /* Utilities for building expressions.
145 Since complex expressions are recorded as symbols for use in other
146 expressions these return a symbolS * and not an expressionS *.
147 These explicitly do not take an "add_number" argument. */
148 /* ??? For completeness' sake one might want expr_build_symbol.
149 It would just return its argument. */
151 /* Build an expression for an unsigned constant.
152 The corresponding one for signed constants is missing because
153 there's currently no need for it. One could add an unsigned_p flag
154 but that seems more clumsy. */
157 expr_build_uconstant (value
)
163 e
.X_add_number
= value
;
165 return make_expr_symbol (&e
);
168 /* Build an expression for OP s1. */
171 expr_build_unary (op
, s1
)
180 return make_expr_symbol (&e
);
183 /* Build an expression for s1 OP s2. */
186 expr_build_binary (op
, s1
, s2
)
197 return make_expr_symbol (&e
);
200 /* Build an expression for the current location ('.'). */
207 current_location (&e
);
208 return make_expr_symbol (&e
);
211 /* Build any floating-point literal here.
212 Also build any bignum literal here. */
214 /* Seems atof_machine can backscan through generic_bignum and hit whatever
215 happens to be loaded before it in memory. And its way too complicated
216 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
217 and never write into the early words, thus they'll always be zero.
218 I hate Dean's floating-point code. Bleh. */
219 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
221 FLONUM_TYPE generic_floating_point_number
= {
222 &generic_bignum
[6], /* low. (JF: Was 0) */
223 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high. JF: (added +6) */
229 /* If nonzero, we've been asked to assemble nan, +inf or -inf. */
230 int generic_floating_point_magic
;
233 floating_constant (expressionP
)
234 expressionS
*expressionP
;
236 /* input_line_pointer -> floating-point constant. */
239 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
240 &generic_floating_point_number
);
244 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
246 as_bad (_("bad floating-point constant: exponent overflow"));
250 as_bad (_("bad floating-point constant: unknown error code=%d"),
254 expressionP
->X_op
= O_big
;
255 /* input_line_pointer -> just after constant, which may point to
257 expressionP
->X_add_number
= -1;
261 generic_bignum_to_int32 ()
264 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
265 | (generic_bignum
[0] & LITTLENUM_MASK
);
266 number
&= 0xffffffff;
272 generic_bignum_to_int64 ()
275 ((((((((valueT
) generic_bignum
[3] & LITTLENUM_MASK
)
276 << LITTLENUM_NUMBER_OF_BITS
)
277 | ((valueT
) generic_bignum
[2] & LITTLENUM_MASK
))
278 << LITTLENUM_NUMBER_OF_BITS
)
279 | ((valueT
) generic_bignum
[1] & LITTLENUM_MASK
))
280 << LITTLENUM_NUMBER_OF_BITS
)
281 | ((valueT
) generic_bignum
[0] & LITTLENUM_MASK
));
287 integer_constant (radix
, expressionP
)
289 expressionS
*expressionP
;
291 char *start
; /* Start of number. */
294 valueT number
; /* Offset or (absolute) value. */
295 short int digit
; /* Value of next digit in current radix. */
296 short int maxdig
= 0; /* Highest permitted digit value. */
297 int too_many_digits
= 0; /* If we see >= this number of. */
298 char *name
; /* Points to name of symbol. */
299 symbolS
*symbolP
; /* Points to symbol. */
301 int small
; /* True if fits in 32 bits. */
303 /* May be bignum, or may fit in 32 bits. */
304 /* Most numbers fit into 32 bits, and we want this case to be fast.
305 so we pretend it will fit into 32 bits. If, after making up a 32
306 bit number, we realise that we have scanned more digits than
307 comfortably fit into 32 bits, we re-scan the digits coding them
308 into a bignum. For decimal and octal numbers we are
309 conservative: Some numbers may be assumed bignums when in fact
310 they do fit into 32 bits. Numbers of any radix can have excess
311 leading zeros: We strive to recognise this and cast them back
312 into 32 bits. We must check that the bignum really is more than
313 32 bits, and change it back to a 32-bit number if it fits. The
314 number we are looking for is expected to be positive, but if it
315 fits into 32 bits as an unsigned number, we let it be a 32-bit
316 number. The cavalier approach is for speed in ordinary cases. */
317 /* This has been extended for 64 bits. We blindly assume that if
318 you're compiling in 64-bit mode, the target is a 64-bit machine.
319 This should be cleaned up. */
323 #else /* includes non-bfd case, mostly */
327 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
) && radix
== 0)
331 /* In MRI mode, the number may have a suffix indicating the
332 radix. For that matter, it might actually be a floating
334 for (suffix
= input_line_pointer
; ISALNUM (*suffix
); suffix
++)
336 if (*suffix
== 'e' || *suffix
== 'E')
340 if (suffix
== input_line_pointer
)
353 else if (c
== 'O' || c
== 'Q')
357 else if (suffix
[1] == '.' || c
== 'E' || flt
)
359 floating_constant (expressionP
);
374 too_many_digits
= valuesize
+ 1;
378 too_many_digits
= (valuesize
+ 2) / 3 + 1;
382 too_many_digits
= (valuesize
+ 3) / 4 + 1;
386 too_many_digits
= (valuesize
+ 11) / 4; /* Very rough. */
389 start
= input_line_pointer
;
390 c
= *input_line_pointer
++;
392 (digit
= hex_value (c
)) < maxdig
;
393 c
= *input_line_pointer
++)
395 number
= number
* radix
+ digit
;
397 /* c contains character after number. */
398 /* input_line_pointer->char after c. */
399 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
401 if (radix
== 16 && c
== '_')
403 /* This is literal of the form 0x333_0_12345678_1.
404 This example is equivalent to 0x00000333000000001234567800000001. */
406 int num_little_digits
= 0;
408 input_line_pointer
= start
; /* -> 1st digit. */
410 know (LITTLENUM_NUMBER_OF_BITS
== 16);
412 for (c
= '_'; c
== '_'; num_little_digits
+= 2)
415 /* Convert one 64-bit word. */
418 for (c
= *input_line_pointer
++;
419 (digit
= hex_value (c
)) < maxdig
;
420 c
= *(input_line_pointer
++))
422 number
= number
* radix
+ digit
;
426 /* Check for 8 digit per word max. */
428 as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
430 /* Add this chunk to the bignum.
431 Shift things down 2 little digits. */
432 know (LITTLENUM_NUMBER_OF_BITS
== 16);
433 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1);
436 generic_bignum
[i
] = generic_bignum
[i
- 2];
438 /* Add the new digits as the least significant new ones. */
439 generic_bignum
[0] = number
& 0xffffffff;
440 generic_bignum
[1] = number
>> 16;
443 /* Again, c is char after number, input_line_pointer->after c. */
445 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
446 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
448 assert (num_little_digits
>= 4);
450 if (num_little_digits
!= 8)
451 as_bad (_("a bignum with underscores must have exactly 4 words"));
453 /* We might have some leading zeros. These can be trimmed to give
454 us a change to fit this constant into a small number. */
455 while (generic_bignum
[num_little_digits
- 1] == 0
456 && num_little_digits
> 1)
459 if (num_little_digits
<= 2)
461 /* will fit into 32 bits. */
462 number
= generic_bignum_to_int32 ();
466 else if (num_little_digits
<= 4)
468 /* Will fit into 64 bits. */
469 number
= generic_bignum_to_int64 ();
477 /* Number of littlenums in the bignum. */
478 number
= num_little_digits
;
483 /* We saw a lot of digits. manufacture a bignum the hard way. */
484 LITTLENUM_TYPE
*leader
; /* -> high order littlenum of the bignum. */
485 LITTLENUM_TYPE
*pointer
; /* -> littlenum we are frobbing now. */
488 leader
= generic_bignum
;
489 generic_bignum
[0] = 0;
490 generic_bignum
[1] = 0;
491 generic_bignum
[2] = 0;
492 generic_bignum
[3] = 0;
493 input_line_pointer
= start
; /* -> 1st digit. */
494 c
= *input_line_pointer
++;
495 for (; (carry
= hex_value (c
)) < maxdig
; c
= *input_line_pointer
++)
497 for (pointer
= generic_bignum
; pointer
<= leader
; pointer
++)
501 work
= carry
+ radix
* *pointer
;
502 *pointer
= work
& LITTLENUM_MASK
;
503 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
507 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
509 /* Room to grow a longer bignum. */
514 /* Again, c is char after number. */
515 /* input_line_pointer -> after c. */
516 know (LITTLENUM_NUMBER_OF_BITS
== 16);
517 if (leader
< generic_bignum
+ 2)
519 /* Will fit into 32 bits. */
520 number
= generic_bignum_to_int32 ();
524 else if (leader
< generic_bignum
+ 4)
526 /* Will fit into 64 bits. */
527 number
= generic_bignum_to_int64 ();
533 /* Number of littlenums in the bignum. */
534 number
= leader
- generic_bignum
+ 1;
538 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
540 && input_line_pointer
- 1 == suffix
)
541 c
= *input_line_pointer
++;
545 /* Here with number, in correct radix. c is the next char.
546 Note that unlike un*x, we allow "011f" "0x9f" to both mean
547 the same as the (conventional) "9f".
548 This is simply easier than checking for strict canonical
551 if (LOCAL_LABELS_FB
&& c
== 'b')
553 /* Backward ref to local label.
554 Because it is backward, expect it to be defined. */
555 /* Construct a local label. */
556 name
= fb_label_name ((int) number
, 0);
558 /* Seen before, or symbol is defined: OK. */
559 symbolP
= symbol_find (name
);
560 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
562 /* Local labels are never absolute. Don't waste time
563 checking absoluteness. */
564 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
566 expressionP
->X_op
= O_symbol
;
567 expressionP
->X_add_symbol
= symbolP
;
571 /* Either not seen or not defined. */
572 /* @@ Should print out the original string instead of
573 the parsed number. */
574 as_bad (_("backward ref to unknown label \"%d:\""),
576 expressionP
->X_op
= O_constant
;
579 expressionP
->X_add_number
= 0;
581 else if (LOCAL_LABELS_FB
&& c
== 'f')
583 /* Forward reference. Expect symbol to be undefined or
584 unknown. undefined: seen it before. unknown: never seen
587 Construct a local label name, then an undefined symbol.
588 Don't create a xseg frag for it: caller may do that.
589 Just return it as never seen before. */
590 name
= fb_label_name ((int) number
, 1);
591 symbolP
= symbol_find_or_make (name
);
592 /* We have no need to check symbol properties. */
593 #ifndef many_segments
594 /* Since "know" puts its arg into a "string", we
595 can't have newlines in the argument. */
596 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
598 expressionP
->X_op
= O_symbol
;
599 expressionP
->X_add_symbol
= symbolP
;
600 expressionP
->X_add_number
= 0;
602 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
604 /* If the dollar label is *currently* defined, then this is just
605 another reference to it. If it is not *currently* defined,
606 then this is a fresh instantiation of that number, so create
609 if (dollar_label_defined ((long) number
))
611 name
= dollar_label_name ((long) number
, 0);
612 symbolP
= symbol_find (name
);
613 know (symbolP
!= NULL
);
617 name
= dollar_label_name ((long) number
, 1);
618 symbolP
= symbol_find_or_make (name
);
621 expressionP
->X_op
= O_symbol
;
622 expressionP
->X_add_symbol
= symbolP
;
623 expressionP
->X_add_number
= 0;
627 expressionP
->X_op
= O_constant
;
628 #ifdef TARGET_WORD_SIZE
629 /* Sign extend NUMBER. */
630 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
632 expressionP
->X_add_number
= number
;
633 input_line_pointer
--; /* Restore following character. */
634 } /* Really just a number. */
638 /* Not a small number. */
639 expressionP
->X_op
= O_big
;
640 expressionP
->X_add_number
= number
; /* Number of littlenums. */
641 input_line_pointer
--; /* -> char following number. */
645 /* Parse an MRI multi character constant. */
648 mri_char_constant (expressionP
)
649 expressionS
*expressionP
;
653 if (*input_line_pointer
== '\''
654 && input_line_pointer
[1] != '\'')
656 expressionP
->X_op
= O_constant
;
657 expressionP
->X_add_number
= 0;
661 /* In order to get the correct byte ordering, we must build the
662 number in reverse. */
663 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
667 generic_bignum
[i
] = 0;
668 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
670 if (*input_line_pointer
== '\'')
672 if (input_line_pointer
[1] != '\'')
674 ++input_line_pointer
;
676 generic_bignum
[i
] <<= 8;
677 generic_bignum
[i
] += *input_line_pointer
;
678 ++input_line_pointer
;
681 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
683 /* If there is more than one littlenum, left justify the
684 last one to make it match the earlier ones. If there is
685 only one, we can just use the value directly. */
686 for (; j
< CHARS_PER_LITTLENUM
; j
++)
687 generic_bignum
[i
] <<= 8;
690 if (*input_line_pointer
== '\''
691 && input_line_pointer
[1] != '\'')
697 as_bad (_("character constant too large"));
706 c
= SIZE_OF_LARGE_NUMBER
- i
;
707 for (j
= 0; j
< c
; j
++)
708 generic_bignum
[j
] = generic_bignum
[i
+ j
];
712 know (LITTLENUM_NUMBER_OF_BITS
== 16);
715 expressionP
->X_op
= O_big
;
716 expressionP
->X_add_number
= i
;
720 expressionP
->X_op
= O_constant
;
722 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
724 expressionP
->X_add_number
=
725 (((generic_bignum
[1] & LITTLENUM_MASK
)
726 << LITTLENUM_NUMBER_OF_BITS
)
727 | (generic_bignum
[0] & LITTLENUM_MASK
));
730 /* Skip the final closing quote. */
731 ++input_line_pointer
;
734 /* Return an expression representing the current location. This
735 handles the magic symbol `.'. */
738 current_location (expressionp
)
739 expressionS
*expressionp
;
741 if (now_seg
== absolute_section
)
743 expressionp
->X_op
= O_constant
;
744 expressionp
->X_add_number
= abs_section_offset
;
750 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
751 (valueT
) frag_now_fix (),
753 expressionp
->X_op
= O_symbol
;
754 expressionp
->X_add_symbol
= symbolp
;
755 expressionp
->X_add_number
= 0;
759 /* In: Input_line_pointer points to 1st char of operand, which may
763 The operand may have been empty: in this case X_op == O_absent.
764 Input_line_pointer->(next non-blank) char after operand. */
767 operand (expressionP
)
768 expressionS
*expressionP
;
771 symbolS
*symbolP
; /* Points to symbol. */
772 char *name
; /* Points to name of symbol. */
775 /* All integers are regarded as unsigned unless they are negated.
776 This is because the only thing which cares whether a number is
777 unsigned is the code in emit_expr which extends constants into
778 bignums. It should only sign extend negative numbers, so that
779 something like ``.quad 0x80000000'' is not sign extended even
780 though it appears negative if valueT is 32 bits. */
781 expressionP
->X_unsigned
= 1;
783 /* Digits, assume it is a bignum. */
785 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
786 c
= *input_line_pointer
++; /* input_line_pointer -> past char in c. */
788 if (is_end_of_line
[(unsigned char) c
])
802 input_line_pointer
--;
804 integer_constant ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
809 #ifdef LITERAL_PREFIXDOLLAR_HEX
811 /* $L is the start of a local label, not a hex constant. */
812 if (* input_line_pointer
== 'L')
814 integer_constant (16, expressionP
);
818 #ifdef LITERAL_PREFIXPERCENT_BIN
820 integer_constant (2, expressionP
);
825 /* Non-decimal radix. */
827 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
831 /* Check for a hex or float constant. */
832 for (s
= input_line_pointer
; hex_p (*s
); s
++)
834 if (*s
== 'h' || *s
== 'H' || *input_line_pointer
== '.')
836 --input_line_pointer
;
837 integer_constant (0, expressionP
);
841 c
= *input_line_pointer
;
850 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
852 integer_constant (0, expressionP
);
858 if (c
&& strchr (FLT_CHARS
, c
))
860 input_line_pointer
++;
861 floating_constant (expressionP
);
862 expressionP
->X_add_number
= - TOLOWER (c
);
866 /* The string was only zero. */
867 expressionP
->X_op
= O_constant
;
868 expressionP
->X_add_number
= 0;
877 input_line_pointer
++;
878 integer_constant (16, expressionP
);
882 if (LOCAL_LABELS_FB
&& ! (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
))
884 /* This code used to check for '+' and '-' here, and, in
885 some conditions, fall through to call
886 integer_constant. However, that didn't make sense,
887 as integer_constant only accepts digits. */
888 /* Some of our code elsewhere does permit digits greater
889 than the expected base; for consistency, do the same
891 if (input_line_pointer
[1] < '0'
892 || input_line_pointer
[1] > '9')
894 /* Parse this as a back reference to label 0. */
895 input_line_pointer
--;
896 integer_constant (10, expressionP
);
899 /* Otherwise, parse this as a binary number. */
903 input_line_pointer
++;
904 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
906 integer_constant (2, expressionP
);
917 integer_constant ((flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
925 /* If it says "0f" and it could possibly be a floating point
926 number, make it one. Otherwise, make it a local label,
927 and try to deal with parsing the rest later. */
928 if (!input_line_pointer
[1]
929 || (is_end_of_line
[0xff & input_line_pointer
[1]])
930 || strchr (FLT_CHARS
, 'f') == NULL
)
933 char *cp
= input_line_pointer
+ 1;
934 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
935 &generic_floating_point_number
);
939 case ERROR_EXPONENT_OVERFLOW
:
940 if (*cp
== 'f' || *cp
== 'b')
941 /* Looks like a difference expression. */
943 else if (cp
== input_line_pointer
+ 1)
944 /* No characters has been accepted -- looks like
950 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
955 /* Okay, now we've sorted it out. We resume at one of these
956 two labels, depending on what we've decided we're probably
959 input_line_pointer
--;
960 integer_constant (10, expressionP
);
970 if (flag_m68k_mri
|| NUMBERS_WITH_SUFFIX
)
972 integer_constant (0, expressionP
);
982 input_line_pointer
++;
983 floating_constant (expressionP
);
984 expressionP
->X_add_number
= - TOLOWER (c
);
988 if (LOCAL_LABELS_DOLLAR
)
990 integer_constant (10, expressionP
);
1000 #ifndef NEED_INDEX_OPERATOR
1003 /* Didn't begin with digit & not a name. */
1004 segment
= expression (expressionP
);
1005 /* expression () will pass trailing whitespace. */
1006 if ((c
== '(' && *input_line_pointer
!= ')')
1007 || (c
== '[' && *input_line_pointer
!= ']'))
1009 #ifdef RELAX_PAREN_GROUPING
1012 as_bad (_("missing '%c'"), c
== '(' ? ')' : ']');
1015 input_line_pointer
++;
1017 /* Here with input_line_pointer -> char after "(...)". */
1022 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1024 as_bad (_("EBCDIC constants are not supported"));
1027 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1029 ++input_line_pointer
;
1033 if (! flag_m68k_mri
)
1035 /* Warning: to conform to other people's assemblers NO
1036 ESCAPEMENT is permitted for a single quote. The next
1037 character, parity errors and all, is taken as the value
1038 of the operand. VERY KINKY. */
1039 expressionP
->X_op
= O_constant
;
1040 expressionP
->X_add_number
= *input_line_pointer
++;
1044 mri_char_constant (expressionP
);
1048 (void) operand (expressionP
);
1053 /* Double quote is the bitwise not operator in MRI mode. */
1054 if (! flag_m68k_mri
)
1059 /* '~' is permitted to start a label on the Delta. */
1060 if (is_name_beginner (c
))
1065 operand (expressionP
);
1066 if (expressionP
->X_op
== O_constant
)
1068 /* input_line_pointer -> char after operand. */
1071 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1072 /* Notice: '-' may overflow: no warning is given.
1073 This is compatible with other people's
1074 assemblers. Sigh. */
1075 expressionP
->X_unsigned
= 0;
1077 else if (c
== '~' || c
== '"')
1078 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1080 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1082 else if (expressionP
->X_op
== O_big
1083 && expressionP
->X_add_number
<= 0
1085 && (generic_floating_point_number
.sign
== '+'
1086 || generic_floating_point_number
.sign
== 'P'))
1088 /* Negative flonum (eg, -1.000e0). */
1089 if (generic_floating_point_number
.sign
== '+')
1090 generic_floating_point_number
.sign
= '-';
1092 generic_floating_point_number
.sign
= 'N';
1094 else if (expressionP
->X_op
!= O_illegal
1095 && expressionP
->X_op
!= O_absent
)
1097 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1099 expressionP
->X_op
= O_uminus
;
1100 else if (c
== '~' || c
== '"')
1101 expressionP
->X_op
= O_bit_not
;
1103 expressionP
->X_op
= O_logical_not
;
1104 expressionP
->X_add_number
= 0;
1107 as_warn (_("Unary operator %c ignored because bad operand follows"),
1112 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1114 /* '$' is the program counter when in MRI mode, or when
1115 DOLLAR_DOT is defined. */
1117 if (! flag_m68k_mri
)
1120 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
1122 /* In MRI mode, '$' is also used as the prefix for a
1123 hexadecimal constant. */
1124 integer_constant (16, expressionP
);
1128 if (is_part_of_name (*input_line_pointer
))
1131 current_location (expressionP
);
1136 if (!is_part_of_name (*input_line_pointer
))
1138 current_location (expressionP
);
1141 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1142 && ! is_part_of_name (input_line_pointer
[8]))
1143 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1144 && ! is_part_of_name (input_line_pointer
[7])))
1148 start
= (input_line_pointer
[1] == 't'
1149 || input_line_pointer
[1] == 'T');
1150 input_line_pointer
+= start
? 8 : 7;
1152 if (*input_line_pointer
!= '(')
1153 as_bad (_("syntax error in .startof. or .sizeof."));
1158 ++input_line_pointer
;
1160 name
= input_line_pointer
;
1161 c
= get_symbol_end ();
1163 buf
= (char *) xmalloc (strlen (name
) + 10);
1165 sprintf (buf
, ".startof.%s", name
);
1167 sprintf (buf
, ".sizeof.%s", name
);
1168 symbolP
= symbol_make (buf
);
1171 expressionP
->X_op
= O_symbol
;
1172 expressionP
->X_add_symbol
= symbolP
;
1173 expressionP
->X_add_number
= 0;
1175 *input_line_pointer
= c
;
1177 if (*input_line_pointer
!= ')')
1178 as_bad (_("syntax error in .startof. or .sizeof."));
1180 ++input_line_pointer
;
1191 /* Can't imagine any other kind of operand. */
1192 expressionP
->X_op
= O_absent
;
1193 input_line_pointer
--;
1198 if (! flag_m68k_mri
)
1200 integer_constant (2, expressionP
);
1204 if (! flag_m68k_mri
)
1206 integer_constant (8, expressionP
);
1210 if (! flag_m68k_mri
)
1213 /* In MRI mode, this is a floating point constant represented
1214 using hexadecimal digits. */
1216 ++input_line_pointer
;
1217 integer_constant (16, expressionP
);
1221 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1224 current_location (expressionP
);
1232 if (is_name_beginner (c
)) /* Here if did not begin with a digit. */
1234 /* Identifier begins here.
1235 This is kludged for speed, so code is repeated. */
1237 name
= --input_line_pointer
;
1238 c
= get_symbol_end ();
1240 #ifdef md_parse_name
1241 /* This is a hook for the backend to parse certain names
1242 specially in certain contexts. If a name always has a
1243 specific value, it can often be handled by simply
1244 entering it in the symbol table. */
1245 if (md_parse_name (name
, expressionP
, &c
))
1247 *input_line_pointer
= c
;
1253 /* The MRI i960 assembler permits
1255 FIXME: This should use md_parse_name. */
1257 && (strcasecmp (name
, "sizeof") == 0
1258 || strcasecmp (name
, "startof") == 0))
1263 start
= (name
[1] == 't'
1266 *input_line_pointer
= c
;
1269 name
= input_line_pointer
;
1270 c
= get_symbol_end ();
1272 buf
= (char *) xmalloc (strlen (name
) + 10);
1274 sprintf (buf
, ".startof.%s", name
);
1276 sprintf (buf
, ".sizeof.%s", name
);
1277 symbolP
= symbol_make (buf
);
1280 expressionP
->X_op
= O_symbol
;
1281 expressionP
->X_add_symbol
= symbolP
;
1282 expressionP
->X_add_number
= 0;
1284 *input_line_pointer
= c
;
1291 symbolP
= symbol_find_or_make (name
);
1293 /* If we have an absolute symbol or a reg, then we know its
1295 segment
= S_GET_SEGMENT (symbolP
);
1296 if (segment
== absolute_section
)
1298 expressionP
->X_op
= O_constant
;
1299 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1301 else if (segment
== reg_section
)
1303 expressionP
->X_op
= O_register
;
1304 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1308 expressionP
->X_op
= O_symbol
;
1309 expressionP
->X_add_symbol
= symbolP
;
1310 expressionP
->X_add_number
= 0;
1312 *input_line_pointer
= c
;
1316 /* Let the target try to parse it. Success is indicated by changing
1317 the X_op field to something other than O_absent and pointing
1318 input_line_pointer past the expression. If it can't parse the
1319 expression, X_op and input_line_pointer should be unchanged. */
1320 expressionP
->X_op
= O_absent
;
1321 --input_line_pointer
;
1322 md_operand (expressionP
);
1323 if (expressionP
->X_op
== O_absent
)
1325 ++input_line_pointer
;
1326 as_bad (_("bad expression"));
1327 expressionP
->X_op
= O_constant
;
1328 expressionP
->X_add_number
= 0;
1334 /* It is more 'efficient' to clean up the expressionS when they are
1335 created. Doing it here saves lines of code. */
1336 clean_up_expression (expressionP
);
1337 SKIP_WHITESPACE (); /* -> 1st char after operand. */
1338 know (*input_line_pointer
!= ' ');
1340 /* The PA port needs this information. */
1341 if (expressionP
->X_add_symbol
)
1342 symbol_mark_used (expressionP
->X_add_symbol
);
1344 switch (expressionP
->X_op
)
1347 return absolute_section
;
1349 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1355 /* Internal. Simplify a struct expression for use by expr (). */
1357 /* In: address of an expressionS.
1358 The X_op field of the expressionS may only take certain values.
1359 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1361 Out: expressionS may have been modified:
1362 Unused fields zeroed to help expr (). */
1365 clean_up_expression (expressionP
)
1366 expressionS
*expressionP
;
1368 switch (expressionP
->X_op
)
1372 expressionP
->X_add_number
= 0;
1377 expressionP
->X_add_symbol
= NULL
;
1382 expressionP
->X_op_symbol
= NULL
;
1389 /* Expression parser. */
1391 /* We allow an empty expression, and just assume (absolute,0) silently.
1392 Unary operators and parenthetical expressions are treated as operands.
1393 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1395 We used to do an aho/ullman shift-reduce parser, but the logic got so
1396 warped that I flushed it and wrote a recursive-descent parser instead.
1397 Now things are stable, would anybody like to write a fast parser?
1398 Most expressions are either register (which does not even reach here)
1399 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1400 So I guess it doesn't really matter how inefficient more complex expressions
1403 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1404 Also, we have consumed any leading or trailing spaces (operand does that)
1405 and done all intervening operators.
1407 This returns the segment of the result, which will be
1408 absolute_section or the segment of a symbol. */
1411 #define __ O_illegal
1413 /* Maps ASCII -> operators. */
1414 static const operatorT op_encoding
[256] = {
1415 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1416 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1418 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1419 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1420 __
, __
, __
, __
, __
, __
, __
, __
,
1421 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1422 __
, __
, __
, __
, __
, __
, __
, __
,
1423 __
, __
, __
, __
, __
, __
, __
, __
,
1424 __
, __
, __
, __
, __
, __
, __
, __
,
1426 #ifdef NEED_INDEX_OPERATOR
1431 __
, __
, O_bit_exclusive_or
, __
,
1432 __
, __
, __
, __
, __
, __
, __
, __
,
1433 __
, __
, __
, __
, __
, __
, __
, __
,
1434 __
, __
, __
, __
, __
, __
, __
, __
,
1435 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1437 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1438 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1439 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1440 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1441 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1442 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1443 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1444 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1448 0 operand, (expression)
1453 5 used for * / % in MRI mode
1458 static operator_rankT op_rank
[] = {
1463 0, /* O_symbol_rva */
1468 9, /* O_logical_not */
1472 8, /* O_left_shift */
1473 8, /* O_right_shift */
1474 7, /* O_bit_inclusive_or */
1475 7, /* O_bit_or_not */
1476 7, /* O_bit_exclusive_or */
1486 3, /* O_logical_and */
1487 2, /* O_logical_or */
1507 /* Unfortunately, in MRI mode for the m68k, multiplication and
1508 division have lower precedence than the bit wise operators. This
1509 function sets the operator precedences correctly for the current
1510 mode. Also, MRI uses a different bit_not operator, and this fixes
1513 #define STANDARD_MUL_PRECEDENCE 8
1514 #define MRI_MUL_PRECEDENCE 6
1517 expr_set_precedence ()
1521 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1522 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1523 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1527 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1528 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1529 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1533 /* Initialize the expression parser. */
1538 expr_set_precedence ();
1540 /* Verify that X_op field is wide enough. */
1544 assert (e
.X_op
== O_max
);
1548 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1549 sets NUM_CHARS to the number of characters in the operator.
1550 Does not advance INPUT_LINE_POINTER. */
1552 static inline operatorT
1553 operator (num_chars
)
1559 c
= *input_line_pointer
& 0xff;
1562 if (is_end_of_line
[c
])
1568 return op_encoding
[c
];
1571 switch (input_line_pointer
[1])
1574 return op_encoding
[c
];
1589 if (input_line_pointer
[1] != '=')
1590 return op_encoding
[c
];
1596 switch (input_line_pointer
[1])
1599 return op_encoding
[c
];
1601 ret
= O_right_shift
;
1611 /* We accept !! as equivalent to ^ for MRI compatibility. */
1612 if (input_line_pointer
[1] != '!')
1615 return O_bit_inclusive_or
;
1616 return op_encoding
[c
];
1619 return O_bit_exclusive_or
;
1622 if (input_line_pointer
[1] != '|')
1623 return op_encoding
[c
];
1626 return O_logical_or
;
1629 if (input_line_pointer
[1] != '&')
1630 return op_encoding
[c
];
1633 return O_logical_and
;
1639 /* Parse an expression. */
1642 expr (rankarg
, resultP
)
1643 int rankarg
; /* Larger # is higher rank. */
1644 expressionS
*resultP
; /* Deliver result here. */
1646 operator_rankT rank
= (operator_rankT
) rankarg
;
1655 /* Save the value of dot for the fixup code. */
1657 dot_value
= frag_now_fix ();
1659 retval
= operand (resultP
);
1661 /* operand () gobbles spaces. */
1662 know (*input_line_pointer
!= ' ');
1664 op_left
= operator (&op_chars
);
1665 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1669 input_line_pointer
+= op_chars
; /* -> after operator. */
1671 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1672 if (right
.X_op
== O_absent
)
1674 as_warn (_("missing operand; zero assumed"));
1675 right
.X_op
= O_constant
;
1676 right
.X_add_number
= 0;
1677 right
.X_add_symbol
= NULL
;
1678 right
.X_op_symbol
= NULL
;
1681 know (*input_line_pointer
!= ' ');
1683 if (op_left
== O_index
)
1685 if (*input_line_pointer
!= ']')
1686 as_bad ("missing right bracket");
1689 ++input_line_pointer
;
1694 op_right
= operator (&op_chars
);
1696 know (op_right
== O_illegal
1697 || op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1698 know ((int) op_left
>= (int) O_multiply
1699 && (int) op_left
<= (int) O_logical_or
);
1701 /* input_line_pointer->after right-hand quantity. */
1702 /* left-hand quantity in resultP. */
1703 /* right-hand quantity in right. */
1704 /* operator in op_left. */
1706 if (resultP
->X_op
== O_big
)
1708 if (resultP
->X_add_number
> 0)
1709 as_warn (_("left operand is a bignum; integer 0 assumed"));
1711 as_warn (_("left operand is a float; integer 0 assumed"));
1712 resultP
->X_op
= O_constant
;
1713 resultP
->X_add_number
= 0;
1714 resultP
->X_add_symbol
= NULL
;
1715 resultP
->X_op_symbol
= NULL
;
1717 if (right
.X_op
== O_big
)
1719 if (right
.X_add_number
> 0)
1720 as_warn (_("right operand is a bignum; integer 0 assumed"));
1722 as_warn (_("right operand is a float; integer 0 assumed"));
1723 right
.X_op
= O_constant
;
1724 right
.X_add_number
= 0;
1725 right
.X_add_symbol
= NULL
;
1726 right
.X_op_symbol
= NULL
;
1729 /* Optimize common cases. */
1730 #ifdef md_optimize_expr
1731 if (md_optimize_expr (resultP
, op_left
, &right
))
1738 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1741 resultP
->X_add_number
+= right
.X_add_number
;
1743 /* This case comes up in PIC code. */
1744 else if (op_left
== O_subtract
1745 && right
.X_op
== O_symbol
1746 && resultP
->X_op
== O_symbol
1747 && (symbol_get_frag (right
.X_add_symbol
)
1748 == symbol_get_frag (resultP
->X_add_symbol
))
1749 && (SEG_NORMAL (rightseg
)
1750 || right
.X_add_symbol
== resultP
->X_add_symbol
))
1752 resultP
->X_add_number
-= right
.X_add_number
;
1753 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1754 - S_GET_VALUE (right
.X_add_symbol
));
1755 resultP
->X_op
= O_constant
;
1756 resultP
->X_add_symbol
= 0;
1758 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1761 resultP
->X_add_number
-= right
.X_add_number
;
1763 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1766 resultP
->X_op
= right
.X_op
;
1767 resultP
->X_add_symbol
= right
.X_add_symbol
;
1768 resultP
->X_op_symbol
= right
.X_op_symbol
;
1769 resultP
->X_add_number
+= right
.X_add_number
;
1772 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1774 /* Constant OP constant. */
1775 offsetT v
= right
.X_add_number
;
1776 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1778 as_warn (_("division by zero"));
1784 case O_multiply
: resultP
->X_add_number
*= v
; break;
1785 case O_divide
: resultP
->X_add_number
/= v
; break;
1786 case O_modulus
: resultP
->X_add_number
%= v
; break;
1787 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1789 /* We always use unsigned shifts, to avoid relying on
1790 characteristics of the compiler used to compile gas. */
1791 resultP
->X_add_number
=
1792 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1794 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1795 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1796 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1797 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1798 case O_add
: resultP
->X_add_number
+= v
; break;
1799 case O_subtract
: resultP
->X_add_number
-= v
; break;
1801 resultP
->X_add_number
=
1802 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1805 resultP
->X_add_number
=
1806 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1809 resultP
->X_add_number
=
1810 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1813 resultP
->X_add_number
=
1814 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1817 resultP
->X_add_number
=
1818 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1821 resultP
->X_add_number
=
1822 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1825 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1828 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1832 else if (resultP
->X_op
== O_symbol
1833 && right
.X_op
== O_symbol
1834 && (op_left
== O_add
1835 || op_left
== O_subtract
1836 || (resultP
->X_add_number
== 0
1837 && right
.X_add_number
== 0)))
1839 /* Symbol OP symbol. */
1840 resultP
->X_op
= op_left
;
1841 resultP
->X_op_symbol
= right
.X_add_symbol
;
1842 if (op_left
== O_add
)
1843 resultP
->X_add_number
+= right
.X_add_number
;
1844 else if (op_left
== O_subtract
)
1846 resultP
->X_add_number
-= right
.X_add_number
;
1847 if (retval
== rightseg
&& SEG_NORMAL (retval
))
1849 retval
= absolute_section
;
1850 rightseg
= absolute_section
;
1856 /* The general case. */
1857 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1858 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1859 resultP
->X_op
= op_left
;
1860 resultP
->X_add_number
= 0;
1861 resultP
->X_unsigned
= 1;
1864 if (retval
!= rightseg
)
1866 if (! SEG_NORMAL (retval
))
1868 if (retval
!= undefined_section
|| SEG_NORMAL (rightseg
))
1871 else if (SEG_NORMAL (rightseg
)
1873 && op_left
!= O_subtract
1876 as_bad (_("operation combines symbols in different segments"));
1880 } /* While next operator is >= this rank. */
1882 /* The PA port needs this information. */
1883 if (resultP
->X_add_symbol
)
1884 symbol_mark_used (resultP
->X_add_symbol
);
1886 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1889 /* This lives here because it belongs equally in expr.c & read.c.
1890 expr.c is just a branch office read.c anyway, and putting it
1891 here lessens the crowd at read.c.
1893 Assume input_line_pointer is at start of symbol name.
1894 Advance input_line_pointer past symbol name.
1895 Turn that character into a '\0', returning its former value.
1896 This allows a string compare (RMS wants symbol names to be strings)
1898 There will always be a char following symbol name, because all good
1899 lines end in end-of-line. */
1906 /* We accept \001 in a name in case this is being called with a
1907 constructed string. */
1908 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1910 while (is_part_of_name (c
= *input_line_pointer
++)
1913 if (is_name_ender (c
))
1914 c
= *input_line_pointer
++;
1916 *--input_line_pointer
= 0;
1921 get_single_number ()
1925 return exp
.X_add_number
;