1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 * This is really a branch office of as-read.c. I split it out to clearly
24 * distinguish the world of expressions from the world of statements.
25 * (It also gives smaller files to re-compile.)
26 * Here, "operand"s are of expressions, not instructions.
31 #define min(a, b) ((a) < (b) ? (a) : (b))
36 static void floating_constant
PARAMS ((expressionS
* expressionP
));
37 static void integer_constant
PARAMS ((int radix
, expressionS
* expressionP
));
38 static void mri_char_constant
PARAMS ((expressionS
*));
39 static void current_location
PARAMS ((expressionS
*));
40 static void clean_up_expression
PARAMS ((expressionS
* expressionP
));
41 static segT operand
PARAMS ((expressionS
*));
42 static operatorT
operator PARAMS ((void));
44 extern const char EXP_CHARS
[], FLT_CHARS
[];
46 /* We keep a mapping of expression symbols to file positions, so that
47 we can provide better error messages. */
49 struct expr_symbol_line
51 struct expr_symbol_line
*next
;
57 static struct expr_symbol_line
*expr_symbol_lines
;
59 /* Build a dummy symbol to hold a complex expression. This is how we
60 build expressions up out of other expressions. The symbol is put
61 into the fake section expr_section. */
64 make_expr_symbol (expressionP
)
65 expressionS
*expressionP
;
69 struct expr_symbol_line
*n
;
71 if (expressionP
->X_op
== O_symbol
72 && expressionP
->X_add_number
== 0)
73 return expressionP
->X_add_symbol
;
75 fake
= FAKE_LABEL_NAME
;
77 /* Putting constant symbols in absolute_section rather than
78 expr_section is convenient for the old a.out code, for which
79 S_GET_SEGMENT does not always retrieve the value put in by
81 symbolP
= symbol_create (fake
,
82 (expressionP
->X_op
== O_constant
85 0, &zero_address_frag
);
86 symbolP
->sy_value
= *expressionP
;
88 if (expressionP
->X_op
== O_constant
)
89 resolve_symbol_value (symbolP
, 1);
91 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
93 as_where (&n
->file
, &n
->line
);
94 n
->next
= expr_symbol_lines
;
95 expr_symbol_lines
= n
;
100 /* Return the file and line number for an expr symbol. Return
101 non-zero if something was found, 0 if no information is known for
105 expr_symbol_where (sym
, pfile
, pline
)
110 register struct expr_symbol_line
*l
;
112 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
125 /* Utilities for building expressions.
126 Since complex expressions are recorded as symbols for use in other
127 expressions these return a symbolS * and not an expressionS *.
128 These explicitly do not take an "add_number" argument. */
129 /* ??? For completeness' sake one might want expr_build_symbol.
130 It would just return its argument. */
132 /* Build an expression for an unsigned constant.
133 The corresponding one for signed constants is missing because
134 there's currently no need for it. One could add an unsigned_p flag
135 but that seems more clumsy. */
138 expr_build_uconstant (value
)
144 e
.X_add_number
= value
;
146 return make_expr_symbol (&e
);
149 /* Build an expression for OP s1. */
152 expr_build_unary (op
, s1
)
161 return make_expr_symbol (&e
);
164 /* Build an expression for s1 OP s2. */
167 expr_build_binary (op
, s1
, s2
)
178 return make_expr_symbol (&e
);
181 /* Build an expression for the current location ('.'). */
188 current_location (&e
);
189 return make_expr_symbol (&e
);
193 * Build any floating-point literal here.
194 * Also build any bignum literal here.
197 /* Seems atof_machine can backscan through generic_bignum and hit whatever
198 happens to be loaded before it in memory. And its way too complicated
199 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
200 and never write into the early words, thus they'll always be zero.
201 I hate Dean's floating-point code. Bleh. */
202 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
203 FLONUM_TYPE generic_floating_point_number
=
205 &generic_bignum
[6], /* low (JF: Was 0) */
206 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high JF: (added +6) */
211 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
212 int generic_floating_point_magic
;
215 floating_constant (expressionP
)
216 expressionS
*expressionP
;
218 /* input_line_pointer->*/
219 /* floating-point constant. */
222 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
223 &generic_floating_point_number
);
227 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
229 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
233 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code
);
236 expressionP
->X_op
= O_big
;
237 /* input_line_pointer->just after constant, */
238 /* which may point to whitespace. */
239 expressionP
->X_add_number
= -1;
243 generic_bignum_to_int32 ()
246 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
247 | (generic_bignum
[0] & LITTLENUM_MASK
);
248 number
&= 0xffffffff;
254 generic_bignum_to_int64 ()
257 ((((((((valueT
) generic_bignum
[3] & LITTLENUM_MASK
)
258 << LITTLENUM_NUMBER_OF_BITS
)
259 | ((valueT
) generic_bignum
[2] & LITTLENUM_MASK
))
260 << LITTLENUM_NUMBER_OF_BITS
)
261 | ((valueT
) generic_bignum
[1] & LITTLENUM_MASK
))
262 << LITTLENUM_NUMBER_OF_BITS
)
263 | ((valueT
) generic_bignum
[0] & LITTLENUM_MASK
));
269 integer_constant (radix
, expressionP
)
271 expressionS
*expressionP
;
273 char *start
; /* start of number. */
276 valueT number
; /* offset or (absolute) value */
277 short int digit
; /* value of next digit in current radix */
278 short int maxdig
= 0;/* highest permitted digit value. */
279 int too_many_digits
= 0; /* if we see >= this number of */
280 char *name
; /* points to name of symbol */
281 symbolS
*symbolP
; /* points to symbol */
283 int small
; /* true if fits in 32 bits. */
285 /* May be bignum, or may fit in 32 bits. */
286 /* Most numbers fit into 32 bits, and we want this case to be fast.
287 so we pretend it will fit into 32 bits. If, after making up a 32
288 bit number, we realise that we have scanned more digits than
289 comfortably fit into 32 bits, we re-scan the digits coding them
290 into a bignum. For decimal and octal numbers we are
291 conservative: Some numbers may be assumed bignums when in fact
292 they do fit into 32 bits. Numbers of any radix can have excess
293 leading zeros: We strive to recognise this and cast them back
294 into 32 bits. We must check that the bignum really is more than
295 32 bits, and change it back to a 32-bit number if it fits. The
296 number we are looking for is expected to be positive, but if it
297 fits into 32 bits as an unsigned number, we let it be a 32-bit
298 number. The cavalier approach is for speed in ordinary cases. */
299 /* This has been extended for 64 bits. We blindly assume that if
300 you're compiling in 64-bit mode, the target is a 64-bit machine.
301 This should be cleaned up. */
305 #else /* includes non-bfd case, mostly */
309 if (flag_m68k_mri
&& radix
== 0)
313 /* In MRI mode, the number may have a suffix indicating the
314 radix. For that matter, it might actually be a floating
316 for (suffix
= input_line_pointer
;
317 isalnum ((unsigned char) *suffix
);
320 if (*suffix
== 'e' || *suffix
== 'E')
324 if (suffix
== input_line_pointer
)
332 if (islower ((unsigned char) c
))
338 else if (c
== 'O' || c
== 'Q')
342 else if (suffix
[1] == '.' || c
== 'E' || flt
)
344 floating_constant (expressionP
);
359 too_many_digits
= valuesize
+ 1;
363 too_many_digits
= (valuesize
+ 2) / 3 + 1;
367 too_many_digits
= (valuesize
+ 3) / 4 + 1;
371 too_many_digits
= (valuesize
+ 12) / 4; /* very rough */
374 start
= input_line_pointer
;
375 c
= *input_line_pointer
++;
377 (digit
= hex_value (c
)) < maxdig
;
378 c
= *input_line_pointer
++)
380 number
= number
* radix
+ digit
;
382 /* c contains character after number. */
383 /* input_line_pointer->char after c. */
384 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
386 if (radix
== 16 && c
== '_')
388 /* This is literal of the form 0x333_0_12345678_1.
389 This example is equivalent to 0x00000333000000001234567800000001. */
391 int num_little_digits
= 0;
393 input_line_pointer
= start
; /*->1st digit. */
395 know (LITTLENUM_NUMBER_OF_BITS
== 16);
397 for (c
= '_'; c
== '_'; num_little_digits
+=2)
400 /* Convert one 64-bit word. */
403 for (c
= *input_line_pointer
++;
404 (digit
= hex_value (c
)) < maxdig
;
405 c
= *(input_line_pointer
++))
407 number
= number
* radix
+ digit
;
411 /* Check for 8 digit per word max. */
413 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
415 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
416 know (LITTLENUM_NUMBER_OF_BITS
== 16);
417 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1); i
>= 2; i
--)
418 generic_bignum
[i
] = generic_bignum
[i
-2];
420 /* Add the new digits as the least significant new ones. */
421 generic_bignum
[0] = number
& 0xffffffff;
422 generic_bignum
[1] = number
>> 16;
425 /* Again, c is char after number, input_line_pointer->after c. */
427 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
428 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
430 assert (num_little_digits
>= 4);
432 if (num_little_digits
!= 8)
433 as_bad (_("A bignum with underscores must have exactly 4 words."));
435 /* We might have some leading zeros. These can be trimmed to give
436 * us a change to fit this constant into a small number.
438 while (generic_bignum
[num_little_digits
-1] == 0 && num_little_digits
> 1)
441 if (num_little_digits
<= 2)
443 /* will fit into 32 bits. */
444 number
= generic_bignum_to_int32 ();
448 else if (num_little_digits
<= 4)
450 /* Will fit into 64 bits. */
451 number
= generic_bignum_to_int64 ();
458 number
= num_little_digits
; /* number of littlenums in the bignum. */
464 * we saw a lot of digits. manufacture a bignum the hard way.
466 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
467 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
470 leader
= generic_bignum
;
471 generic_bignum
[0] = 0;
472 generic_bignum
[1] = 0;
473 generic_bignum
[2] = 0;
474 generic_bignum
[3] = 0;
475 input_line_pointer
= start
; /*->1st digit. */
476 c
= *input_line_pointer
++;
478 (carry
= hex_value (c
)) < maxdig
;
479 c
= *input_line_pointer
++)
481 for (pointer
= generic_bignum
;
487 work
= carry
+ radix
* *pointer
;
488 *pointer
= work
& LITTLENUM_MASK
;
489 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
493 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
495 /* room to grow a longer bignum. */
500 /* again, c is char after number, */
501 /* input_line_pointer->after c. */
502 know (LITTLENUM_NUMBER_OF_BITS
== 16);
503 if (leader
< generic_bignum
+ 2)
505 /* will fit into 32 bits. */
506 number
= generic_bignum_to_int32 ();
510 else if (leader
< generic_bignum
+ 4)
512 /* Will fit into 64 bits. */
513 number
= generic_bignum_to_int64 ();
519 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
523 if (flag_m68k_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
524 c
= *input_line_pointer
++;
529 * here with number, in correct radix. c is the next char.
530 * note that unlike un*x, we allow "011f" "0x9f" to
531 * both mean the same as the (conventional) "9f". this is simply easier
532 * than checking for strict canonical form. syntax sux!
535 if (LOCAL_LABELS_FB
&& c
== 'b')
538 * backward ref to local label.
539 * because it is backward, expect it to be defined.
541 /* Construct a local label. */
542 name
= fb_label_name ((int) number
, 0);
544 /* seen before, or symbol is defined: ok */
545 symbolP
= symbol_find (name
);
546 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
548 /* local labels are never absolute. don't waste time
549 checking absoluteness. */
550 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
552 expressionP
->X_op
= O_symbol
;
553 expressionP
->X_add_symbol
= symbolP
;
557 /* either not seen or not defined. */
558 /* @@ Should print out the original string instead of
559 the parsed number. */
560 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
562 expressionP
->X_op
= O_constant
;
565 expressionP
->X_add_number
= 0;
567 else if (LOCAL_LABELS_FB
&& c
== 'f')
570 * forward reference. expect symbol to be undefined or
571 * unknown. undefined: seen it before. unknown: never seen
573 * construct a local label name, then an undefined symbol.
574 * don't create a xseg frag for it: caller may do that.
575 * just return it as never seen before.
577 name
= fb_label_name ((int) number
, 1);
578 symbolP
= symbol_find_or_make (name
);
579 /* we have no need to check symbol properties. */
580 #ifndef many_segments
581 /* since "know" puts its arg into a "string", we
582 can't have newlines in the argument. */
583 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
585 expressionP
->X_op
= O_symbol
;
586 expressionP
->X_add_symbol
= symbolP
;
587 expressionP
->X_add_number
= 0;
589 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
591 /* If the dollar label is *currently* defined, then this is just
592 another reference to it. If it is not *currently* defined,
593 then this is a fresh instantiation of that number, so create
596 if (dollar_label_defined ((long) number
))
598 name
= dollar_label_name ((long) number
, 0);
599 symbolP
= symbol_find (name
);
600 know (symbolP
!= NULL
);
604 name
= dollar_label_name ((long) number
, 1);
605 symbolP
= symbol_find_or_make (name
);
608 expressionP
->X_op
= O_symbol
;
609 expressionP
->X_add_symbol
= symbolP
;
610 expressionP
->X_add_number
= 0;
614 expressionP
->X_op
= O_constant
;
615 #ifdef TARGET_WORD_SIZE
616 /* Sign extend NUMBER. */
617 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
619 expressionP
->X_add_number
= number
;
620 input_line_pointer
--; /* restore following character. */
621 } /* really just a number */
625 /* not a small number */
626 expressionP
->X_op
= O_big
;
627 expressionP
->X_add_number
= number
; /* number of littlenums */
628 input_line_pointer
--; /*->char following number. */
632 /* Parse an MRI multi character constant. */
635 mri_char_constant (expressionP
)
636 expressionS
*expressionP
;
640 if (*input_line_pointer
== '\''
641 && input_line_pointer
[1] != '\'')
643 expressionP
->X_op
= O_constant
;
644 expressionP
->X_add_number
= 0;
648 /* In order to get the correct byte ordering, we must build the
649 number in reverse. */
650 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
654 generic_bignum
[i
] = 0;
655 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
657 if (*input_line_pointer
== '\'')
659 if (input_line_pointer
[1] != '\'')
661 ++input_line_pointer
;
663 generic_bignum
[i
] <<= 8;
664 generic_bignum
[i
] += *input_line_pointer
;
665 ++input_line_pointer
;
668 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
670 /* If there is more than one littlenum, left justify the
671 last one to make it match the earlier ones. If there is
672 only one, we can just use the value directly. */
673 for (; j
< CHARS_PER_LITTLENUM
; j
++)
674 generic_bignum
[i
] <<= 8;
677 if (*input_line_pointer
== '\''
678 && input_line_pointer
[1] != '\'')
684 as_bad (_("Character constant too large"));
693 c
= SIZE_OF_LARGE_NUMBER
- i
;
694 for (j
= 0; j
< c
; j
++)
695 generic_bignum
[j
] = generic_bignum
[i
+ j
];
699 know (LITTLENUM_NUMBER_OF_BITS
== 16);
702 expressionP
->X_op
= O_big
;
703 expressionP
->X_add_number
= i
;
707 expressionP
->X_op
= O_constant
;
709 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
711 expressionP
->X_add_number
=
712 (((generic_bignum
[1] & LITTLENUM_MASK
)
713 << LITTLENUM_NUMBER_OF_BITS
)
714 | (generic_bignum
[0] & LITTLENUM_MASK
));
717 /* Skip the final closing quote. */
718 ++input_line_pointer
;
721 /* Return an expression representing the current location. This
722 handles the magic symbol `.'. */
725 current_location (expressionp
)
726 expressionS
*expressionp
;
728 if (now_seg
== absolute_section
)
730 expressionp
->X_op
= O_constant
;
731 expressionp
->X_add_number
= abs_section_offset
;
737 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
738 (valueT
) frag_now_fix (),
740 expressionp
->X_op
= O_symbol
;
741 expressionp
->X_add_symbol
= symbolp
;
742 expressionp
->X_add_number
= 0;
747 * Summary of operand().
749 * in: Input_line_pointer points to 1st char of operand, which may
752 * out: A expressionS.
753 * The operand may have been empty: in this case X_op == O_absent.
754 * Input_line_pointer->(next non-blank) char after operand.
758 operand (expressionP
)
759 expressionS
*expressionP
;
762 symbolS
*symbolP
; /* points to symbol */
763 char *name
; /* points to name of symbol */
766 /* All integers are regarded as unsigned unless they are negated.
767 This is because the only thing which cares whether a number is
768 unsigned is the code in emit_expr which extends constants into
769 bignums. It should only sign extend negative numbers, so that
770 something like ``.quad 0x80000000'' is not sign extended even
771 though it appears negative if valueT is 32 bits. */
772 expressionP
->X_unsigned
= 1;
774 /* digits, assume it is a bignum. */
776 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
777 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
790 input_line_pointer
--;
792 integer_constant (flag_m68k_mri
? 0 : 10, expressionP
);
796 /* non-decimal radix */
802 /* Check for a hex constant. */
803 for (s
= input_line_pointer
; hex_p (*s
); s
++)
805 if (*s
== 'h' || *s
== 'H')
807 --input_line_pointer
;
808 integer_constant (0, expressionP
);
813 c
= *input_line_pointer
;
824 integer_constant (0, expressionP
);
830 if (c
&& strchr (FLT_CHARS
, c
))
832 input_line_pointer
++;
833 floating_constant (expressionP
);
834 expressionP
->X_add_number
=
835 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
839 /* The string was only zero */
840 expressionP
->X_op
= O_constant
;
841 expressionP
->X_add_number
= 0;
850 input_line_pointer
++;
851 integer_constant (16, expressionP
);
855 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
)
857 /* This code used to check for '+' and '-' here, and, in
858 some conditions, fall through to call
859 integer_constant. However, that didn't make sense,
860 as integer_constant only accepts digits. */
861 /* Some of our code elsewhere does permit digits greater
862 than the expected base; for consistency, do the same
864 if (input_line_pointer
[1] < '0'
865 || input_line_pointer
[1] > '9')
867 /* Parse this as a back reference to label 0. */
868 input_line_pointer
--;
869 integer_constant (10, expressionP
);
872 /* Otherwise, parse this as a binary number. */
876 input_line_pointer
++;
879 integer_constant (2, expressionP
);
890 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
896 /* If it says "0f" and it could possibly be a floating point
897 number, make it one. Otherwise, make it a local label,
898 and try to deal with parsing the rest later. */
899 if (!input_line_pointer
[1]
900 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
903 char *cp
= input_line_pointer
+ 1;
904 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
905 &generic_floating_point_number
);
909 case ERROR_EXPONENT_OVERFLOW
:
910 if (*cp
== 'f' || *cp
== 'b')
911 /* looks like a difference expression */
916 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
921 /* Okay, now we've sorted it out. We resume at one of these
922 two labels, depending on what we've decided we're probably
925 input_line_pointer
--;
926 integer_constant (10, expressionP
);
938 integer_constant (0, expressionP
);
948 input_line_pointer
++;
949 floating_constant (expressionP
);
950 expressionP
->X_add_number
=
951 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
955 if (LOCAL_LABELS_DOLLAR
)
957 integer_constant (10, expressionP
);
968 /* didn't begin with digit & not a name */
969 segment
= expression (expressionP
);
970 /* Expression() will pass trailing whitespace */
971 if ((c
== '(' && *input_line_pointer
++ != ')')
972 || (c
== '[' && *input_line_pointer
++ != ']'))
974 as_bad (_("Missing ')' assumed"));
975 input_line_pointer
--;
978 /* here with input_line_pointer->char after "(...)" */
982 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
984 as_bad (_("EBCDIC constants are not supported"));
987 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
989 ++input_line_pointer
;
994 /* Warning: to conform to other people's assemblers NO
995 ESCAPEMENT is permitted for a single quote. The next
996 character, parity errors and all, is taken as the value
997 of the operand. VERY KINKY. */
998 expressionP
->X_op
= O_constant
;
999 expressionP
->X_add_number
= *input_line_pointer
++;
1003 mri_char_constant (expressionP
);
1007 (void) operand (expressionP
);
1011 /* Double quote is the bitwise not operator in MRI mode. */
1012 if (! flag_m68k_mri
)
1016 /* ~ is permitted to start a label on the Delta. */
1017 if (is_name_beginner (c
))
1022 operand (expressionP
);
1023 if (expressionP
->X_op
== O_constant
)
1025 /* input_line_pointer -> char after operand */
1028 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1029 /* Notice: '-' may overflow: no warning is given. This is
1030 compatible with other people's assemblers. Sigh. */
1031 expressionP
->X_unsigned
= 0;
1033 else if (c
== '~' || c
== '"')
1034 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1036 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1038 else if (expressionP
->X_op
!= O_illegal
1039 && expressionP
->X_op
!= O_absent
)
1041 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1043 expressionP
->X_op
= O_uminus
;
1044 else if (c
== '~' || c
== '"')
1045 expressionP
->X_op
= O_bit_not
;
1047 expressionP
->X_op
= O_logical_not
;
1048 expressionP
->X_add_number
= 0;
1051 as_warn (_("Unary operator %c ignored because bad operand follows"),
1057 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1060 if (! flag_m68k_mri
)
1063 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
1065 /* In MRI mode, $ is also used as the prefix for a
1066 hexadecimal constant. */
1067 integer_constant (16, expressionP
);
1071 if (is_part_of_name (*input_line_pointer
))
1074 current_location (expressionP
);
1078 if (!is_part_of_name (*input_line_pointer
))
1080 current_location (expressionP
);
1083 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1084 && ! is_part_of_name (input_line_pointer
[8]))
1085 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1086 && ! is_part_of_name (input_line_pointer
[7])))
1090 start
= (input_line_pointer
[1] == 't'
1091 || input_line_pointer
[1] == 'T');
1092 input_line_pointer
+= start
? 8 : 7;
1094 if (*input_line_pointer
!= '(')
1095 as_bad (_("syntax error in .startof. or .sizeof."));
1100 ++input_line_pointer
;
1102 name
= input_line_pointer
;
1103 c
= get_symbol_end ();
1105 buf
= (char *) xmalloc (strlen (name
) + 10);
1107 sprintf (buf
, ".startof.%s", name
);
1109 sprintf (buf
, ".sizeof.%s", name
);
1110 symbolP
= symbol_make (buf
);
1113 expressionP
->X_op
= O_symbol
;
1114 expressionP
->X_add_symbol
= symbolP
;
1115 expressionP
->X_add_number
= 0;
1117 *input_line_pointer
= c
;
1119 if (*input_line_pointer
!= ')')
1120 as_bad (_("syntax error in .startof. or .sizeof."));
1122 ++input_line_pointer
;
1134 /* can't imagine any other kind of operand */
1135 expressionP
->X_op
= O_absent
;
1136 input_line_pointer
--;
1140 if (! flag_m68k_mri
)
1142 integer_constant (2, expressionP
);
1146 if (! flag_m68k_mri
)
1148 integer_constant (8, expressionP
);
1152 if (! flag_m68k_mri
)
1155 /* In MRI mode, this is a floating point constant represented
1156 using hexadecimal digits. */
1158 ++input_line_pointer
;
1159 integer_constant (16, expressionP
);
1163 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1166 current_location (expressionP
);
1171 if (is_end_of_line
[(unsigned char) c
])
1173 if (is_name_beginner (c
)) /* here if did not begin with a digit */
1176 * Identifier begins here.
1177 * This is kludged for speed, so code is repeated.
1180 name
= --input_line_pointer
;
1181 c
= get_symbol_end ();
1183 #ifdef md_parse_name
1184 /* This is a hook for the backend to parse certain names
1185 specially in certain contexts. If a name always has a
1186 specific value, it can often be handled by simply
1187 entering it in the symbol table. */
1188 if (md_parse_name (name
, expressionP
))
1190 *input_line_pointer
= c
;
1196 /* The MRI i960 assembler permits
1198 FIXME: This should use md_parse_name. */
1200 && (strcasecmp (name
, "sizeof") == 0
1201 || strcasecmp (name
, "startof") == 0))
1206 start
= (name
[1] == 't'
1209 *input_line_pointer
= c
;
1212 name
= input_line_pointer
;
1213 c
= get_symbol_end ();
1215 buf
= (char *) xmalloc (strlen (name
) + 10);
1217 sprintf (buf
, ".startof.%s", name
);
1219 sprintf (buf
, ".sizeof.%s", name
);
1220 symbolP
= symbol_make (buf
);
1223 expressionP
->X_op
= O_symbol
;
1224 expressionP
->X_add_symbol
= symbolP
;
1225 expressionP
->X_add_number
= 0;
1227 *input_line_pointer
= c
;
1234 symbolP
= symbol_find_or_make (name
);
1236 /* If we have an absolute symbol or a reg, then we know its
1238 segment
= S_GET_SEGMENT (symbolP
);
1239 if (segment
== absolute_section
)
1241 expressionP
->X_op
= O_constant
;
1242 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1244 else if (segment
== reg_section
)
1246 expressionP
->X_op
= O_register
;
1247 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1251 expressionP
->X_op
= O_symbol
;
1252 expressionP
->X_add_symbol
= symbolP
;
1253 expressionP
->X_add_number
= 0;
1255 *input_line_pointer
= c
;
1259 /* Let the target try to parse it. Success is indicated by changing
1260 the X_op field to something other than O_absent and pointing
1261 input_line_pointer passed the expression. If it can't parse the
1262 expression, X_op and input_line_pointer should be unchanged. */
1263 expressionP
->X_op
= O_absent
;
1264 --input_line_pointer
;
1265 md_operand (expressionP
);
1266 if (expressionP
->X_op
== O_absent
)
1268 ++input_line_pointer
;
1269 as_bad (_("Bad expression"));
1270 expressionP
->X_op
= O_constant
;
1271 expressionP
->X_add_number
= 0;
1278 * It is more 'efficient' to clean up the expressionS when they are created.
1279 * Doing it here saves lines of code.
1281 clean_up_expression (expressionP
);
1282 SKIP_WHITESPACE (); /*->1st char after operand. */
1283 know (*input_line_pointer
!= ' ');
1285 /* The PA port needs this information. */
1286 if (expressionP
->X_add_symbol
)
1287 expressionP
->X_add_symbol
->sy_used
= 1;
1289 switch (expressionP
->X_op
)
1292 return absolute_section
;
1294 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1300 /* Internal. Simplify a struct expression for use by expr() */
1303 * In: address of a expressionS.
1304 * The X_op field of the expressionS may only take certain values.
1305 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1306 * Out: expressionS may have been modified:
1307 * 'foo-foo' symbol references cancelled to 0,
1308 * which changes X_op from O_subtract to O_constant.
1309 * Unused fields zeroed to help expr().
1313 clean_up_expression (expressionP
)
1314 expressionS
*expressionP
;
1316 switch (expressionP
->X_op
)
1320 expressionP
->X_add_number
= 0;
1325 expressionP
->X_add_symbol
= NULL
;
1330 expressionP
->X_op_symbol
= NULL
;
1333 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1334 || ((expressionP
->X_op_symbol
->sy_frag
1335 == expressionP
->X_add_symbol
->sy_frag
)
1336 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1337 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1338 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1340 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1341 - S_GET_VALUE (expressionP
->X_op_symbol
));
1343 expressionP
->X_op
= O_constant
;
1344 expressionP
->X_add_symbol
= NULL
;
1345 expressionP
->X_op_symbol
= NULL
;
1346 expressionP
->X_add_number
+= diff
;
1354 /* Expression parser. */
1357 * We allow an empty expression, and just assume (absolute,0) silently.
1358 * Unary operators and parenthetical expressions are treated as operands.
1359 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1361 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1362 * warped that I flushed it and wrote a recursive-descent parser instead.
1363 * Now things are stable, would anybody like to write a fast parser?
1364 * Most expressions are either register (which does not even reach here)
1365 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1366 * So I guess it doesn't really matter how inefficient more complex expressions
1369 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1370 * Also, we have consumed any leading or trailing spaces (operand does that)
1371 * and done all intervening operators.
1373 * This returns the segment of the result, which will be
1374 * absolute_section or the segment of a symbol.
1378 #define __ O_illegal
1380 static const operatorT op_encoding
[256] =
1381 { /* maps ASCII->operators */
1383 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1384 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1386 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1387 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1388 __
, __
, __
, __
, __
, __
, __
, __
,
1389 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1390 __
, __
, __
, __
, __
, __
, __
, __
,
1391 __
, __
, __
, __
, __
, __
, __
, __
,
1392 __
, __
, __
, __
, __
, __
, __
, __
,
1393 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1394 __
, __
, __
, __
, __
, __
, __
, __
,
1395 __
, __
, __
, __
, __
, __
, __
, __
,
1396 __
, __
, __
, __
, __
, __
, __
, __
,
1397 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1399 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1400 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1401 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1402 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1403 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1404 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1405 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1406 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1412 * 0 operand, (expression)
1417 * 5 used for * / % in MRI mode
1422 static operator_rankT op_rank
[] =
1428 0, /* O_symbol_rva */
1433 8, /* O_logical_not */
1437 7, /* O_left_shift */
1438 7, /* O_right_shift */
1439 6, /* O_bit_inclusive_or */
1440 6, /* O_bit_or_not */
1441 6, /* O_bit_exclusive_or */
1451 2, /* O_logical_and */
1452 1 /* O_logical_or */
1455 /* Unfortunately, in MRI mode for the m68k, multiplication and
1456 division have lower precedence than the bit wise operators. This
1457 function sets the operator precedences correctly for the current
1458 mode. Also, MRI uses a different bit_not operator, and this fixes
1461 #define STANDARD_MUL_PRECEDENCE (7)
1462 #define MRI_MUL_PRECEDENCE (5)
1465 expr_set_precedence ()
1469 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1470 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1471 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1475 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1476 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1477 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1481 /* Initialize the expression parser. */
1486 expr_set_precedence ();
1488 /* Verify that X_op field is wide enough. */
1492 assert (e
.X_op
== O_max
);
1496 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1497 Advance INPUT_LINE_POINTER to the last character in the operator
1498 (i.e., don't change it for a single character operator). */
1500 static inline operatorT
1506 c
= *input_line_pointer
;
1511 return op_encoding
[c
];
1514 switch (input_line_pointer
[1])
1517 return op_encoding
[c
];
1528 ++input_line_pointer
;
1532 if (input_line_pointer
[1] != '=')
1533 return op_encoding
[c
];
1535 ++input_line_pointer
;
1539 switch (input_line_pointer
[1])
1542 return op_encoding
[c
];
1544 ret
= O_right_shift
;
1550 ++input_line_pointer
;
1554 /* We accept !! as equivalent to ^ for MRI compatibility. */
1555 if (input_line_pointer
[1] != '!')
1558 return O_bit_inclusive_or
;
1559 return op_encoding
[c
];
1561 ++input_line_pointer
;
1562 return O_bit_exclusive_or
;
1565 if (input_line_pointer
[1] != '|')
1566 return op_encoding
[c
];
1568 ++input_line_pointer
;
1569 return O_logical_or
;
1572 if (input_line_pointer
[1] != '&')
1573 return op_encoding
[c
];
1575 ++input_line_pointer
;
1576 return O_logical_and
;
1582 /* Parse an expression. */
1585 expr (rank
, resultP
)
1586 operator_rankT rank
; /* Larger # is higher rank. */
1587 expressionS
*resultP
; /* Deliver result here. */
1596 retval
= operand (resultP
);
1598 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1600 op_left
= operator ();
1601 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1605 input_line_pointer
++; /*->after 1st character of operator. */
1607 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1608 if (right
.X_op
== O_absent
)
1610 as_warn (_("missing operand; zero assumed"));
1611 right
.X_op
= O_constant
;
1612 right
.X_add_number
= 0;
1613 right
.X_add_symbol
= NULL
;
1614 right
.X_op_symbol
= NULL
;
1617 know (*input_line_pointer
!= ' ');
1619 if (retval
== undefined_section
)
1621 if (SEG_NORMAL (rightseg
))
1624 else if (! SEG_NORMAL (retval
))
1626 else if (SEG_NORMAL (rightseg
)
1627 && retval
!= rightseg
1629 && op_left
!= O_subtract
1632 as_bad (_("operation combines symbols in different segments"));
1634 op_right
= operator ();
1636 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1637 know ((int) op_left
>= (int) O_multiply
1638 && (int) op_left
<= (int) O_logical_or
);
1640 /* input_line_pointer->after right-hand quantity. */
1641 /* left-hand quantity in resultP */
1642 /* right-hand quantity in right. */
1643 /* operator in op_left. */
1645 if (resultP
->X_op
== O_big
)
1647 if (resultP
->X_add_number
> 0)
1648 as_warn (_("left operand is a bignum; integer 0 assumed"));
1650 as_warn (_("left operand is a float; integer 0 assumed"));
1651 resultP
->X_op
= O_constant
;
1652 resultP
->X_add_number
= 0;
1653 resultP
->X_add_symbol
= NULL
;
1654 resultP
->X_op_symbol
= NULL
;
1656 if (right
.X_op
== O_big
)
1658 if (right
.X_add_number
> 0)
1659 as_warn (_("right operand is a bignum; integer 0 assumed"));
1660 as_warn (_("right operand is a float; integer 0 assumed"));
1661 right
.X_op
= O_constant
;
1662 right
.X_add_number
= 0;
1663 right
.X_add_symbol
= NULL
;
1664 right
.X_op_symbol
= NULL
;
1667 /* Optimize common cases. */
1668 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1671 resultP
->X_add_number
+= right
.X_add_number
;
1673 /* This case comes up in PIC code. */
1674 else if (op_left
== O_subtract
1675 && right
.X_op
== O_symbol
1676 && resultP
->X_op
== O_symbol
1677 && (right
.X_add_symbol
->sy_frag
1678 == resultP
->X_add_symbol
->sy_frag
)
1679 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1682 resultP
->X_add_number
-= right
.X_add_number
;
1683 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1684 - S_GET_VALUE (right
.X_add_symbol
));
1685 resultP
->X_op
= O_constant
;
1686 resultP
->X_add_symbol
= 0;
1688 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1691 resultP
->X_add_number
-= right
.X_add_number
;
1693 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1696 resultP
->X_op
= right
.X_op
;
1697 resultP
->X_add_symbol
= right
.X_add_symbol
;
1698 resultP
->X_op_symbol
= right
.X_op_symbol
;
1699 resultP
->X_add_number
+= right
.X_add_number
;
1702 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1704 /* Constant OP constant. */
1705 offsetT v
= right
.X_add_number
;
1706 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1708 as_warn (_("division by zero"));
1714 case O_multiply
: resultP
->X_add_number
*= v
; break;
1715 case O_divide
: resultP
->X_add_number
/= v
; break;
1716 case O_modulus
: resultP
->X_add_number
%= v
; break;
1717 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1719 /* We always use unsigned shifts, to avoid relying on
1720 characteristics of the compiler used to compile gas. */
1721 resultP
->X_add_number
=
1722 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1724 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1725 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1726 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1727 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1728 case O_add
: resultP
->X_add_number
+= v
; break;
1729 case O_subtract
: resultP
->X_add_number
-= v
; break;
1731 resultP
->X_add_number
=
1732 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1735 resultP
->X_add_number
=
1736 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1739 resultP
->X_add_number
=
1740 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1743 resultP
->X_add_number
=
1744 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1747 resultP
->X_add_number
=
1748 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1751 resultP
->X_add_number
=
1752 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1755 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1758 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1762 else if (resultP
->X_op
== O_symbol
1763 && right
.X_op
== O_symbol
1764 && (op_left
== O_add
1765 || op_left
== O_subtract
1766 || (resultP
->X_add_number
== 0
1767 && right
.X_add_number
== 0)))
1769 /* Symbol OP symbol. */
1770 resultP
->X_op
= op_left
;
1771 resultP
->X_op_symbol
= right
.X_add_symbol
;
1772 if (op_left
== O_add
)
1773 resultP
->X_add_number
+= right
.X_add_number
;
1774 else if (op_left
== O_subtract
)
1775 resultP
->X_add_number
-= right
.X_add_number
;
1779 /* The general case. */
1780 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1781 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1782 resultP
->X_op
= op_left
;
1783 resultP
->X_add_number
= 0;
1784 resultP
->X_unsigned
= 1;
1788 } /* While next operator is >= this rank. */
1790 /* The PA port needs this information. */
1791 if (resultP
->X_add_symbol
)
1792 resultP
->X_add_symbol
->sy_used
= 1;
1794 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1800 * This lives here because it belongs equally in expr.c & read.c.
1801 * Expr.c is just a branch office read.c anyway, and putting it
1802 * here lessens the crowd at read.c.
1804 * Assume input_line_pointer is at start of symbol name.
1805 * Advance input_line_pointer past symbol name.
1806 * Turn that character into a '\0', returning its former value.
1807 * This allows a string compare (RMS wants symbol names to be strings)
1808 * of the symbol name.
1809 * There will always be a char following symbol name, because all good
1810 * lines end in end-of-line.
1817 /* We accept \001 in a name in case this is being called with a
1818 constructed string. */
1819 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1820 while (is_part_of_name (c
= *input_line_pointer
++)
1823 *--input_line_pointer
= 0;
1829 get_single_number ()
1833 return exp
.X_add_number
;