1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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 valueT generic_bignum_to_int32
PARAMS ((void));
39 static valueT generic_bignum_to_int64
PARAMS ((void));
41 static void integer_constant
PARAMS ((int radix
, expressionS
* expressionP
));
42 static void mri_char_constant
PARAMS ((expressionS
*));
43 static void current_location
PARAMS ((expressionS
*));
44 static void clean_up_expression
PARAMS ((expressionS
* expressionP
));
45 static segT operand
PARAMS ((expressionS
*));
46 static operatorT
operator PARAMS ((void));
48 extern const char EXP_CHARS
[], FLT_CHARS
[];
50 /* We keep a mapping of expression symbols to file positions, so that
51 we can provide better error messages. */
53 struct expr_symbol_line
55 struct expr_symbol_line
*next
;
61 static struct expr_symbol_line
*expr_symbol_lines
;
63 /* Build a dummy symbol to hold a complex expression. This is how we
64 build expressions up out of other expressions. The symbol is put
65 into the fake section expr_section. */
68 make_expr_symbol (expressionP
)
69 expressionS
*expressionP
;
74 struct expr_symbol_line
*n
;
76 if (expressionP
->X_op
== O_symbol
77 && expressionP
->X_add_number
== 0)
78 return expressionP
->X_add_symbol
;
80 if (expressionP
->X_op
== O_big
)
82 /* This won't work, because the actual value is stored in
83 generic_floating_point_number or generic_bignum, and we are
84 going to lose it if we haven't already. */
85 if (expressionP
->X_add_number
> 0)
86 as_bad (_("bignum invalid; zero assumed"));
88 as_bad (_("floating point number invalid; zero assumed"));
89 zero
.X_op
= O_constant
;
90 zero
.X_add_number
= 0;
92 clean_up_expression (&zero
);
96 fake
= FAKE_LABEL_NAME
;
98 /* Putting constant symbols in absolute_section rather than
99 expr_section is convenient for the old a.out code, for which
100 S_GET_SEGMENT does not always retrieve the value put in by
102 symbolP
= symbol_create (fake
,
103 (expressionP
->X_op
== O_constant
106 0, &zero_address_frag
);
107 symbol_set_value_expression (symbolP
, expressionP
);
109 if (expressionP
->X_op
== O_constant
)
110 resolve_symbol_value (symbolP
, 1);
112 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
114 as_where (&n
->file
, &n
->line
);
115 n
->next
= expr_symbol_lines
;
116 expr_symbol_lines
= n
;
121 /* Return the file and line number for an expr symbol. Return
122 non-zero if something was found, 0 if no information is known for
126 expr_symbol_where (sym
, pfile
, pline
)
131 register struct expr_symbol_line
*l
;
133 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
146 /* Utilities for building expressions.
147 Since complex expressions are recorded as symbols for use in other
148 expressions these return a symbolS * and not an expressionS *.
149 These explicitly do not take an "add_number" argument. */
150 /* ??? For completeness' sake one might want expr_build_symbol.
151 It would just return its argument. */
153 /* Build an expression for an unsigned constant.
154 The corresponding one for signed constants is missing because
155 there's currently no need for it. One could add an unsigned_p flag
156 but that seems more clumsy. */
159 expr_build_uconstant (value
)
165 e
.X_add_number
= value
;
167 return make_expr_symbol (&e
);
170 /* Build an expression for OP s1. */
173 expr_build_unary (op
, s1
)
182 return make_expr_symbol (&e
);
185 /* Build an expression for s1 OP s2. */
188 expr_build_binary (op
, s1
, s2
)
199 return make_expr_symbol (&e
);
202 /* Build an expression for the current location ('.'). */
209 current_location (&e
);
210 return make_expr_symbol (&e
);
214 * Build any floating-point literal here.
215 * Also build any bignum literal here.
218 /* Seems atof_machine can backscan through generic_bignum and hit whatever
219 happens to be loaded before it in memory. And its way too complicated
220 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
221 and never write into the early words, thus they'll always be zero.
222 I hate Dean's floating-point code. Bleh. */
223 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
224 FLONUM_TYPE generic_floating_point_number
=
226 &generic_bignum
[6], /* low (JF: Was 0) */
227 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high JF: (added +6) */
232 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
233 int generic_floating_point_magic
;
236 floating_constant (expressionP
)
237 expressionS
*expressionP
;
239 /* input_line_pointer->*/
240 /* floating-point constant. */
243 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
244 &generic_floating_point_number
);
248 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
250 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
254 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code
);
257 expressionP
->X_op
= O_big
;
258 /* input_line_pointer->just after constant, */
259 /* which may point to whitespace. */
260 expressionP
->X_add_number
= -1;
264 generic_bignum_to_int32 ()
267 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
268 | (generic_bignum
[0] & LITTLENUM_MASK
);
269 number
&= 0xffffffff;
275 generic_bignum_to_int64 ()
278 ((((((((valueT
) generic_bignum
[3] & LITTLENUM_MASK
)
279 << LITTLENUM_NUMBER_OF_BITS
)
280 | ((valueT
) generic_bignum
[2] & LITTLENUM_MASK
))
281 << LITTLENUM_NUMBER_OF_BITS
)
282 | ((valueT
) generic_bignum
[1] & LITTLENUM_MASK
))
283 << LITTLENUM_NUMBER_OF_BITS
)
284 | ((valueT
) generic_bignum
[0] & LITTLENUM_MASK
));
290 integer_constant (radix
, expressionP
)
292 expressionS
*expressionP
;
294 char *start
; /* start of number. */
297 valueT number
; /* offset or (absolute) value */
298 short int digit
; /* value of next digit in current radix */
299 short int maxdig
= 0;/* highest permitted digit value. */
300 int too_many_digits
= 0; /* if we see >= this number of */
301 char *name
; /* points to name of symbol */
302 symbolS
*symbolP
; /* points to symbol */
304 int small
; /* true if fits in 32 bits. */
306 /* May be bignum, or may fit in 32 bits. */
307 /* Most numbers fit into 32 bits, and we want this case to be fast.
308 so we pretend it will fit into 32 bits. If, after making up a 32
309 bit number, we realise that we have scanned more digits than
310 comfortably fit into 32 bits, we re-scan the digits coding them
311 into a bignum. For decimal and octal numbers we are
312 conservative: Some numbers may be assumed bignums when in fact
313 they do fit into 32 bits. Numbers of any radix can have excess
314 leading zeros: We strive to recognise this and cast them back
315 into 32 bits. We must check that the bignum really is more than
316 32 bits, and change it back to a 32-bit number if it fits. The
317 number we are looking for is expected to be positive, but if it
318 fits into 32 bits as an unsigned number, we let it be a 32-bit
319 number. The cavalier approach is for speed in ordinary cases. */
320 /* This has been extended for 64 bits. We blindly assume that if
321 you're compiling in 64-bit mode, the target is a 64-bit machine.
322 This should be cleaned up. */
326 #else /* includes non-bfd case, mostly */
330 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
) && radix
== 0)
334 /* In MRI mode, the number may have a suffix indicating the
335 radix. For that matter, it might actually be a floating
337 for (suffix
= input_line_pointer
;
338 isalnum ((unsigned char) *suffix
);
341 if (*suffix
== 'e' || *suffix
== 'E')
345 if (suffix
== input_line_pointer
)
353 if (islower ((unsigned char) c
))
359 else if (c
== 'O' || c
== 'Q')
363 else if (suffix
[1] == '.' || c
== 'E' || flt
)
365 floating_constant (expressionP
);
380 too_many_digits
= valuesize
+ 1;
384 too_many_digits
= (valuesize
+ 2) / 3 + 1;
388 too_many_digits
= (valuesize
+ 3) / 4 + 1;
392 too_many_digits
= (valuesize
+ 11) / 4; /* very rough */
395 start
= input_line_pointer
;
396 c
= *input_line_pointer
++;
398 (digit
= hex_value (c
)) < maxdig
;
399 c
= *input_line_pointer
++)
401 number
= number
* radix
+ digit
;
403 /* c contains character after number. */
404 /* input_line_pointer->char after c. */
405 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
407 if (radix
== 16 && c
== '_')
409 /* This is literal of the form 0x333_0_12345678_1.
410 This example is equivalent to 0x00000333000000001234567800000001. */
412 int num_little_digits
= 0;
414 input_line_pointer
= start
; /*->1st digit. */
416 know (LITTLENUM_NUMBER_OF_BITS
== 16);
418 for (c
= '_'; c
== '_'; num_little_digits
+=2)
421 /* Convert one 64-bit word. */
424 for (c
= *input_line_pointer
++;
425 (digit
= hex_value (c
)) < maxdig
;
426 c
= *(input_line_pointer
++))
428 number
= number
* radix
+ digit
;
432 /* Check for 8 digit per word max. */
434 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
436 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
437 know (LITTLENUM_NUMBER_OF_BITS
== 16);
438 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1); i
>= 2; i
--)
439 generic_bignum
[i
] = generic_bignum
[i
-2];
441 /* Add the new digits as the least significant new ones. */
442 generic_bignum
[0] = number
& 0xffffffff;
443 generic_bignum
[1] = number
>> 16;
446 /* Again, c is char after number, input_line_pointer->after c. */
448 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
449 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
451 assert (num_little_digits
>= 4);
453 if (num_little_digits
!= 8)
454 as_bad (_("A bignum with underscores must have exactly 4 words."));
456 /* We might have some leading zeros. These can be trimmed to give
457 * us a change to fit this constant into a small number.
459 while (generic_bignum
[num_little_digits
-1] == 0 && num_little_digits
> 1)
462 if (num_little_digits
<= 2)
464 /* will fit into 32 bits. */
465 number
= generic_bignum_to_int32 ();
469 else if (num_little_digits
<= 4)
471 /* Will fit into 64 bits. */
472 number
= generic_bignum_to_int64 ();
479 number
= num_little_digits
; /* number of littlenums in the bignum. */
485 * we saw a lot of digits. manufacture a bignum the hard way.
487 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
488 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
491 leader
= generic_bignum
;
492 generic_bignum
[0] = 0;
493 generic_bignum
[1] = 0;
494 generic_bignum
[2] = 0;
495 generic_bignum
[3] = 0;
496 input_line_pointer
= start
; /*->1st digit. */
497 c
= *input_line_pointer
++;
499 (carry
= hex_value (c
)) < maxdig
;
500 c
= *input_line_pointer
++)
502 for (pointer
= generic_bignum
;
508 work
= carry
+ radix
* *pointer
;
509 *pointer
= work
& LITTLENUM_MASK
;
510 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
514 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
516 /* room to grow a longer bignum. */
521 /* again, c is char after number, */
522 /* input_line_pointer->after c. */
523 know (LITTLENUM_NUMBER_OF_BITS
== 16);
524 if (leader
< generic_bignum
+ 2)
526 /* will fit into 32 bits. */
527 number
= generic_bignum_to_int32 ();
531 else if (leader
< generic_bignum
+ 4)
533 /* Will fit into 64 bits. */
534 number
= generic_bignum_to_int64 ();
540 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
544 if ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
546 && input_line_pointer
- 1 == suffix
)
547 c
= *input_line_pointer
++;
552 * here with number, in correct radix. c is the next char.
553 * note that unlike un*x, we allow "011f" "0x9f" to
554 * both mean the same as the (conventional) "9f". this is simply easier
555 * than checking for strict canonical form. syntax sux!
558 if (LOCAL_LABELS_FB
&& c
== 'b')
561 * backward ref to local label.
562 * because it is backward, expect it to be defined.
564 /* Construct a local label. */
565 name
= fb_label_name ((int) number
, 0);
567 /* seen before, or symbol is defined: ok */
568 symbolP
= symbol_find (name
);
569 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
571 /* local labels are never absolute. don't waste time
572 checking absoluteness. */
573 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
575 expressionP
->X_op
= O_symbol
;
576 expressionP
->X_add_symbol
= symbolP
;
580 /* either not seen or not defined. */
581 /* @@ Should print out the original string instead of
582 the parsed number. */
583 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
585 expressionP
->X_op
= O_constant
;
588 expressionP
->X_add_number
= 0;
590 else if (LOCAL_LABELS_FB
&& c
== 'f')
593 * forward reference. expect symbol to be undefined or
594 * unknown. undefined: seen it before. unknown: never seen
596 * construct a local label name, then an undefined symbol.
597 * don't create a xseg frag for it: caller may do that.
598 * just return it as never seen before.
600 name
= fb_label_name ((int) number
, 1);
601 symbolP
= symbol_find_or_make (name
);
602 /* we have no need to check symbol properties. */
603 #ifndef many_segments
604 /* since "know" puts its arg into a "string", we
605 can't have newlines in the argument. */
606 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
608 expressionP
->X_op
= O_symbol
;
609 expressionP
->X_add_symbol
= symbolP
;
610 expressionP
->X_add_number
= 0;
612 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
614 /* If the dollar label is *currently* defined, then this is just
615 another reference to it. If it is not *currently* defined,
616 then this is a fresh instantiation of that number, so create
619 if (dollar_label_defined ((long) number
))
621 name
= dollar_label_name ((long) number
, 0);
622 symbolP
= symbol_find (name
);
623 know (symbolP
!= NULL
);
627 name
= dollar_label_name ((long) number
, 1);
628 symbolP
= symbol_find_or_make (name
);
631 expressionP
->X_op
= O_symbol
;
632 expressionP
->X_add_symbol
= symbolP
;
633 expressionP
->X_add_number
= 0;
637 expressionP
->X_op
= O_constant
;
638 #ifdef TARGET_WORD_SIZE
639 /* Sign extend NUMBER. */
640 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
642 expressionP
->X_add_number
= number
;
643 input_line_pointer
--; /* restore following character. */
644 } /* really just a number */
648 /* not a small number */
649 expressionP
->X_op
= O_big
;
650 expressionP
->X_add_number
= number
; /* number of littlenums */
651 input_line_pointer
--; /*->char following number. */
655 /* Parse an MRI multi character constant. */
658 mri_char_constant (expressionP
)
659 expressionS
*expressionP
;
663 if (*input_line_pointer
== '\''
664 && input_line_pointer
[1] != '\'')
666 expressionP
->X_op
= O_constant
;
667 expressionP
->X_add_number
= 0;
671 /* In order to get the correct byte ordering, we must build the
672 number in reverse. */
673 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
677 generic_bignum
[i
] = 0;
678 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
680 if (*input_line_pointer
== '\'')
682 if (input_line_pointer
[1] != '\'')
684 ++input_line_pointer
;
686 generic_bignum
[i
] <<= 8;
687 generic_bignum
[i
] += *input_line_pointer
;
688 ++input_line_pointer
;
691 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
693 /* If there is more than one littlenum, left justify the
694 last one to make it match the earlier ones. If there is
695 only one, we can just use the value directly. */
696 for (; j
< CHARS_PER_LITTLENUM
; j
++)
697 generic_bignum
[i
] <<= 8;
700 if (*input_line_pointer
== '\''
701 && input_line_pointer
[1] != '\'')
707 as_bad (_("Character constant too large"));
716 c
= SIZE_OF_LARGE_NUMBER
- i
;
717 for (j
= 0; j
< c
; j
++)
718 generic_bignum
[j
] = generic_bignum
[i
+ j
];
722 know (LITTLENUM_NUMBER_OF_BITS
== 16);
725 expressionP
->X_op
= O_big
;
726 expressionP
->X_add_number
= i
;
730 expressionP
->X_op
= O_constant
;
732 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
734 expressionP
->X_add_number
=
735 (((generic_bignum
[1] & LITTLENUM_MASK
)
736 << LITTLENUM_NUMBER_OF_BITS
)
737 | (generic_bignum
[0] & LITTLENUM_MASK
));
740 /* Skip the final closing quote. */
741 ++input_line_pointer
;
744 /* Return an expression representing the current location. This
745 handles the magic symbol `.'. */
748 current_location (expressionp
)
749 expressionS
*expressionp
;
751 if (now_seg
== absolute_section
)
753 expressionp
->X_op
= O_constant
;
754 expressionp
->X_add_number
= abs_section_offset
;
760 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
761 (valueT
) frag_now_fix (),
763 expressionp
->X_op
= O_symbol
;
764 expressionp
->X_add_symbol
= symbolp
;
765 expressionp
->X_add_number
= 0;
770 * Summary of operand().
772 * in: Input_line_pointer points to 1st char of operand, which may
775 * out: A expressionS.
776 * The operand may have been empty: in this case X_op == O_absent.
777 * Input_line_pointer->(next non-blank) char after operand.
781 operand (expressionP
)
782 expressionS
*expressionP
;
785 symbolS
*symbolP
; /* points to symbol */
786 char *name
; /* points to name of symbol */
789 /* All integers are regarded as unsigned unless they are negated.
790 This is because the only thing which cares whether a number is
791 unsigned is the code in emit_expr which extends constants into
792 bignums. It should only sign extend negative numbers, so that
793 something like ``.quad 0x80000000'' is not sign extended even
794 though it appears negative if valueT is 32 bits. */
795 expressionP
->X_unsigned
= 1;
797 /* digits, assume it is a bignum. */
799 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
800 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
813 input_line_pointer
--;
815 integer_constant ((NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
821 /* non-decimal radix */
823 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
827 /* Check for a hex constant. */
828 for (s
= input_line_pointer
; hex_p (*s
); s
++)
830 if (*s
== 'h' || *s
== 'H')
832 --input_line_pointer
;
833 integer_constant (0, expressionP
);
836 if (NUMBERS_WITH_SUFFIX
)
838 /* Check for a binary constant. */
839 for (s
= input_line_pointer
; *s
== '0' || *s
== '1'; s
++)
841 if (toupper (*s
) == 'B')
843 integer_constant (0, expressionP
);
846 /* Check for an octal constant. */
847 for (s
= input_line_pointer
; *s
>= '0' && *s
<= '7'; s
++)
849 if (toupper (*s
) == 'Q')
851 integer_constant (0, expressionP
);
856 c
= *input_line_pointer
;
865 if (NUMBERS_WITH_SUFFIX
|| flag_m68k_mri
)
867 integer_constant (0, expressionP
);
873 if (c
&& strchr (FLT_CHARS
, c
))
875 input_line_pointer
++;
876 floating_constant (expressionP
);
877 expressionP
->X_add_number
=
878 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
882 /* The string was only zero */
883 expressionP
->X_op
= O_constant
;
884 expressionP
->X_add_number
= 0;
893 input_line_pointer
++;
894 integer_constant (16, expressionP
);
898 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
&& ! NUMBERS_WITH_SUFFIX
)
900 /* This code used to check for '+' and '-' here, and, in
901 some conditions, fall through to call
902 integer_constant. However, that didn't make sense,
903 as integer_constant only accepts digits. */
904 /* Some of our code elsewhere does permit digits greater
905 than the expected base; for consistency, do the same
907 if (input_line_pointer
[1] < '0'
908 || input_line_pointer
[1] > '9')
910 /* Parse this as a back reference to label 0. */
911 input_line_pointer
--;
912 integer_constant (10, expressionP
);
915 /* Otherwise, parse this as a binary number. */
919 input_line_pointer
++;
922 integer_constant (2, expressionP
);
933 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
939 /* If it says "0f" and it could possibly be a floating point
940 number, make it one. Otherwise, make it a local label,
941 and try to deal with parsing the rest later. */
942 if (!input_line_pointer
[1]
943 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
946 char *cp
= input_line_pointer
+ 1;
947 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
948 &generic_floating_point_number
);
952 case ERROR_EXPONENT_OVERFLOW
:
953 if (*cp
== 'f' || *cp
== 'b')
954 /* looks like a difference expression */
956 else if (cp
== input_line_pointer
+ 1)
957 /* No characters has been accepted -- looks like
963 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
968 /* Okay, now we've sorted it out. We resume at one of these
969 two labels, depending on what we've decided we're probably
972 input_line_pointer
--;
973 integer_constant (10, expressionP
);
985 integer_constant (0, expressionP
);
995 input_line_pointer
++;
996 floating_constant (expressionP
);
997 expressionP
->X_add_number
=
998 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
1002 if (LOCAL_LABELS_DOLLAR
)
1004 integer_constant (10, expressionP
);
1014 #ifndef NEED_INDEX_OPERATOR
1017 /* didn't begin with digit & not a name */
1018 segment
= expression (expressionP
);
1019 /* Expression() will pass trailing whitespace */
1020 if ((c
== '(' && *input_line_pointer
++ != ')')
1021 || (c
== '[' && *input_line_pointer
++ != ']'))
1023 as_bad (_("Missing ')' assumed"));
1024 input_line_pointer
--;
1027 /* here with input_line_pointer->char after "(...)" */
1031 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1033 as_bad (_("EBCDIC constants are not supported"));
1036 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1038 ++input_line_pointer
;
1041 if (! flag_m68k_mri
)
1043 /* Warning: to conform to other people's assemblers NO
1044 ESCAPEMENT is permitted for a single quote. The next
1045 character, parity errors and all, is taken as the value
1046 of the operand. VERY KINKY. */
1047 expressionP
->X_op
= O_constant
;
1048 expressionP
->X_add_number
= *input_line_pointer
++;
1052 mri_char_constant (expressionP
);
1056 (void) operand (expressionP
);
1060 /* Double quote is the bitwise not operator in MRI mode. */
1061 if (! flag_m68k_mri
)
1065 /* ~ is permitted to start a label on the Delta. */
1066 if (is_name_beginner (c
))
1071 operand (expressionP
);
1072 if (expressionP
->X_op
== O_constant
)
1074 /* input_line_pointer -> char after operand */
1077 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1078 /* Notice: '-' may overflow: no warning is given. This is
1079 compatible with other people's assemblers. Sigh. */
1080 expressionP
->X_unsigned
= 0;
1082 else if (c
== '~' || c
== '"')
1083 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1085 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1087 else if (expressionP
->X_op
!= O_illegal
1088 && expressionP
->X_op
!= O_absent
)
1090 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1092 expressionP
->X_op
= O_uminus
;
1093 else if (c
== '~' || c
== '"')
1094 expressionP
->X_op
= O_bit_not
;
1096 expressionP
->X_op
= O_logical_not
;
1097 expressionP
->X_add_number
= 0;
1100 as_warn (_("Unary operator %c ignored because bad operand follows"),
1106 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1109 if (! flag_m68k_mri
)
1112 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
1114 /* In MRI mode, $ is also used as the prefix for a
1115 hexadecimal constant. */
1116 integer_constant (16, expressionP
);
1120 if (is_part_of_name (*input_line_pointer
))
1123 current_location (expressionP
);
1127 if (!is_part_of_name (*input_line_pointer
))
1129 current_location (expressionP
);
1132 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1133 && ! is_part_of_name (input_line_pointer
[8]))
1134 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1135 && ! is_part_of_name (input_line_pointer
[7])))
1139 start
= (input_line_pointer
[1] == 't'
1140 || input_line_pointer
[1] == 'T');
1141 input_line_pointer
+= start
? 8 : 7;
1143 if (*input_line_pointer
!= '(')
1144 as_bad (_("syntax error in .startof. or .sizeof."));
1149 ++input_line_pointer
;
1151 name
= input_line_pointer
;
1152 c
= get_symbol_end ();
1154 buf
= (char *) xmalloc (strlen (name
) + 10);
1156 sprintf (buf
, ".startof.%s", name
);
1158 sprintf (buf
, ".sizeof.%s", name
);
1159 symbolP
= symbol_make (buf
);
1162 expressionP
->X_op
= O_symbol
;
1163 expressionP
->X_add_symbol
= symbolP
;
1164 expressionP
->X_add_number
= 0;
1166 *input_line_pointer
= c
;
1168 if (*input_line_pointer
!= ')')
1169 as_bad (_("syntax error in .startof. or .sizeof."));
1171 ++input_line_pointer
;
1183 /* can't imagine any other kind of operand */
1184 expressionP
->X_op
= O_absent
;
1185 input_line_pointer
--;
1189 if (! flag_m68k_mri
)
1191 integer_constant (2, expressionP
);
1195 if (! flag_m68k_mri
)
1197 integer_constant (8, expressionP
);
1201 if (! flag_m68k_mri
)
1204 /* In MRI mode, this is a floating point constant represented
1205 using hexadecimal digits. */
1207 ++input_line_pointer
;
1208 integer_constant (16, expressionP
);
1212 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1215 current_location (expressionP
);
1220 if (is_end_of_line
[(unsigned char) c
])
1222 if (is_name_beginner (c
)) /* here if did not begin with a digit */
1225 * Identifier begins here.
1226 * This is kludged for speed, so code is repeated.
1229 name
= --input_line_pointer
;
1230 c
= get_symbol_end ();
1232 #ifdef md_parse_name
1233 /* This is a hook for the backend to parse certain names
1234 specially in certain contexts. If a name always has a
1235 specific value, it can often be handled by simply
1236 entering it in the symbol table. */
1237 if (md_parse_name (name
, expressionP
))
1239 *input_line_pointer
= c
;
1245 /* The MRI i960 assembler permits
1247 FIXME: This should use md_parse_name. */
1249 && (strcasecmp (name
, "sizeof") == 0
1250 || strcasecmp (name
, "startof") == 0))
1255 start
= (name
[1] == 't'
1258 *input_line_pointer
= c
;
1261 name
= input_line_pointer
;
1262 c
= get_symbol_end ();
1264 buf
= (char *) xmalloc (strlen (name
) + 10);
1266 sprintf (buf
, ".startof.%s", name
);
1268 sprintf (buf
, ".sizeof.%s", name
);
1269 symbolP
= symbol_make (buf
);
1272 expressionP
->X_op
= O_symbol
;
1273 expressionP
->X_add_symbol
= symbolP
;
1274 expressionP
->X_add_number
= 0;
1276 *input_line_pointer
= c
;
1283 symbolP
= symbol_find_or_make (name
);
1285 /* If we have an absolute symbol or a reg, then we know its
1287 segment
= S_GET_SEGMENT (symbolP
);
1288 if (segment
== absolute_section
)
1290 expressionP
->X_op
= O_constant
;
1291 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1293 else if (segment
== reg_section
)
1295 expressionP
->X_op
= O_register
;
1296 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1300 expressionP
->X_op
= O_symbol
;
1301 expressionP
->X_add_symbol
= symbolP
;
1302 expressionP
->X_add_number
= 0;
1304 *input_line_pointer
= c
;
1308 /* Let the target try to parse it. Success is indicated by changing
1309 the X_op field to something other than O_absent and pointing
1310 input_line_pointer passed the expression. If it can't parse the
1311 expression, X_op and input_line_pointer should be unchanged. */
1312 expressionP
->X_op
= O_absent
;
1313 --input_line_pointer
;
1314 md_operand (expressionP
);
1315 if (expressionP
->X_op
== O_absent
)
1317 ++input_line_pointer
;
1318 as_bad (_("Bad expression"));
1319 expressionP
->X_op
= O_constant
;
1320 expressionP
->X_add_number
= 0;
1327 * It is more 'efficient' to clean up the expressionS when they are created.
1328 * Doing it here saves lines of code.
1330 clean_up_expression (expressionP
);
1331 SKIP_WHITESPACE (); /*->1st char after operand. */
1332 know (*input_line_pointer
!= ' ');
1334 /* The PA port needs this information. */
1335 if (expressionP
->X_add_symbol
)
1336 symbol_mark_used (expressionP
->X_add_symbol
);
1338 switch (expressionP
->X_op
)
1341 return absolute_section
;
1343 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1349 /* Internal. Simplify a struct expression for use by expr() */
1352 * In: address of a expressionS.
1353 * The X_op field of the expressionS may only take certain values.
1354 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1355 * Out: expressionS may have been modified:
1356 * 'foo-foo' symbol references cancelled to 0,
1357 * which changes X_op from O_subtract to O_constant.
1358 * Unused fields zeroed to help expr().
1362 clean_up_expression (expressionP
)
1363 expressionS
*expressionP
;
1365 switch (expressionP
->X_op
)
1369 expressionP
->X_add_number
= 0;
1374 expressionP
->X_add_symbol
= NULL
;
1379 expressionP
->X_op_symbol
= NULL
;
1382 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1383 || ((symbol_get_frag (expressionP
->X_op_symbol
)
1384 == symbol_get_frag (expressionP
->X_add_symbol
))
1385 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1386 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1387 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1389 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1390 - S_GET_VALUE (expressionP
->X_op_symbol
));
1392 expressionP
->X_op
= O_constant
;
1393 expressionP
->X_add_symbol
= NULL
;
1394 expressionP
->X_op_symbol
= NULL
;
1395 expressionP
->X_add_number
+= diff
;
1403 /* Expression parser. */
1406 * We allow an empty expression, and just assume (absolute,0) silently.
1407 * Unary operators and parenthetical expressions are treated as operands.
1408 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1410 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1411 * warped that I flushed it and wrote a recursive-descent parser instead.
1412 * Now things are stable, would anybody like to write a fast parser?
1413 * Most expressions are either register (which does not even reach here)
1414 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1415 * So I guess it doesn't really matter how inefficient more complex expressions
1418 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1419 * Also, we have consumed any leading or trailing spaces (operand does that)
1420 * and done all intervening operators.
1422 * This returns the segment of the result, which will be
1423 * absolute_section or the segment of a symbol.
1427 #define __ O_illegal
1429 static const operatorT op_encoding
[256] =
1430 { /* maps ASCII->operators */
1432 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1433 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1435 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1436 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1437 __
, __
, __
, __
, __
, __
, __
, __
,
1438 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1439 __
, __
, __
, __
, __
, __
, __
, __
,
1440 __
, __
, __
, __
, __
, __
, __
, __
,
1441 __
, __
, __
, __
, __
, __
, __
, __
,
1443 #ifdef NEED_INDEX_OPERATOR
1448 __
, __
, O_bit_exclusive_or
, __
,
1449 __
, __
, __
, __
, __
, __
, __
, __
,
1450 __
, __
, __
, __
, __
, __
, __
, __
,
1451 __
, __
, __
, __
, __
, __
, __
, __
,
1452 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1454 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1455 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1456 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1457 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1458 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1459 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1460 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1461 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1467 * 0 operand, (expression)
1472 * 5 used for * / % in MRI mode
1477 static operator_rankT op_rank
[] =
1483 0, /* O_symbol_rva */
1488 9, /* O_logical_not */
1492 8, /* O_left_shift */
1493 8, /* O_right_shift */
1494 7, /* O_bit_inclusive_or */
1495 7, /* O_bit_or_not */
1496 7, /* O_bit_exclusive_or */
1506 3, /* O_logical_and */
1507 2, /* O_logical_or */
1527 /* Unfortunately, in MRI mode for the m68k, multiplication and
1528 division have lower precedence than the bit wise operators. This
1529 function sets the operator precedences correctly for the current
1530 mode. Also, MRI uses a different bit_not operator, and this fixes
1533 #define STANDARD_MUL_PRECEDENCE (7)
1534 #define MRI_MUL_PRECEDENCE (5)
1537 expr_set_precedence ()
1541 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1542 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1543 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1547 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1548 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1549 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1553 /* Initialize the expression parser. */
1558 expr_set_precedence ();
1560 /* Verify that X_op field is wide enough. */
1564 assert (e
.X_op
== O_max
);
1568 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1569 Advance INPUT_LINE_POINTER to the last character in the operator
1570 (i.e., don't change it for a single character operator). */
1572 static inline operatorT
1578 c
= *input_line_pointer
& 0xff;
1583 return op_encoding
[c
];
1586 switch (input_line_pointer
[1])
1589 return op_encoding
[c
];
1600 ++input_line_pointer
;
1604 if (input_line_pointer
[1] != '=')
1605 return op_encoding
[c
];
1607 ++input_line_pointer
;
1611 switch (input_line_pointer
[1])
1614 return op_encoding
[c
];
1616 ret
= O_right_shift
;
1622 ++input_line_pointer
;
1626 /* We accept !! as equivalent to ^ for MRI compatibility. */
1627 if (input_line_pointer
[1] != '!')
1630 return O_bit_inclusive_or
;
1631 return op_encoding
[c
];
1633 ++input_line_pointer
;
1634 return O_bit_exclusive_or
;
1637 if (input_line_pointer
[1] != '|')
1638 return op_encoding
[c
];
1640 ++input_line_pointer
;
1641 return O_logical_or
;
1644 if (input_line_pointer
[1] != '&')
1645 return op_encoding
[c
];
1647 ++input_line_pointer
;
1648 return O_logical_and
;
1654 /* Parse an expression. */
1657 expr (rankarg
, resultP
)
1658 int rankarg
; /* Larger # is higher rank. */
1659 expressionS
*resultP
; /* Deliver result here. */
1661 operator_rankT rank
= (operator_rankT
) rankarg
;
1669 retval
= operand (resultP
);
1671 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1673 op_left
= operator ();
1674 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1678 input_line_pointer
++; /*->after 1st character of operator. */
1680 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1681 if (right
.X_op
== O_absent
)
1683 as_warn (_("missing operand; zero assumed"));
1684 right
.X_op
= O_constant
;
1685 right
.X_add_number
= 0;
1686 right
.X_add_symbol
= NULL
;
1687 right
.X_op_symbol
= NULL
;
1690 know (*input_line_pointer
!= ' ');
1692 if (op_left
== O_index
)
1694 if (*input_line_pointer
!= ']')
1695 as_bad ("missing right bracket");
1698 ++input_line_pointer
;
1703 if (retval
== undefined_section
)
1705 if (SEG_NORMAL (rightseg
))
1708 else if (! SEG_NORMAL (retval
))
1710 else if (SEG_NORMAL (rightseg
)
1711 && retval
!= rightseg
1713 && op_left
!= O_subtract
1716 as_bad (_("operation combines symbols in different segments"));
1718 op_right
= operator ();
1720 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1721 know ((int) op_left
>= (int) O_multiply
1722 && (int) op_left
<= (int) O_logical_or
);
1724 /* input_line_pointer->after right-hand quantity. */
1725 /* left-hand quantity in resultP */
1726 /* right-hand quantity in right. */
1727 /* operator in op_left. */
1729 if (resultP
->X_op
== O_big
)
1731 if (resultP
->X_add_number
> 0)
1732 as_warn (_("left operand is a bignum; integer 0 assumed"));
1734 as_warn (_("left operand is a float; integer 0 assumed"));
1735 resultP
->X_op
= O_constant
;
1736 resultP
->X_add_number
= 0;
1737 resultP
->X_add_symbol
= NULL
;
1738 resultP
->X_op_symbol
= NULL
;
1740 if (right
.X_op
== O_big
)
1742 if (right
.X_add_number
> 0)
1743 as_warn (_("right operand is a bignum; integer 0 assumed"));
1745 as_warn (_("right operand is a float; integer 0 assumed"));
1746 right
.X_op
= O_constant
;
1747 right
.X_add_number
= 0;
1748 right
.X_add_symbol
= NULL
;
1749 right
.X_op_symbol
= NULL
;
1752 /* Optimize common cases. */
1753 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1756 resultP
->X_add_number
+= right
.X_add_number
;
1758 /* This case comes up in PIC code. */
1759 else if (op_left
== O_subtract
1760 && right
.X_op
== O_symbol
1761 && resultP
->X_op
== O_symbol
1762 && (symbol_get_frag (right
.X_add_symbol
)
1763 == symbol_get_frag (resultP
->X_add_symbol
))
1764 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1767 resultP
->X_add_number
-= right
.X_add_number
;
1768 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1769 - S_GET_VALUE (right
.X_add_symbol
));
1770 resultP
->X_op
= O_constant
;
1771 resultP
->X_add_symbol
= 0;
1773 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1776 resultP
->X_add_number
-= right
.X_add_number
;
1778 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1781 resultP
->X_op
= right
.X_op
;
1782 resultP
->X_add_symbol
= right
.X_add_symbol
;
1783 resultP
->X_op_symbol
= right
.X_op_symbol
;
1784 resultP
->X_add_number
+= right
.X_add_number
;
1787 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1789 /* Constant OP constant. */
1790 offsetT v
= right
.X_add_number
;
1791 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1793 as_warn (_("division by zero"));
1799 case O_multiply
: resultP
->X_add_number
*= v
; break;
1800 case O_divide
: resultP
->X_add_number
/= v
; break;
1801 case O_modulus
: resultP
->X_add_number
%= v
; break;
1802 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1804 /* We always use unsigned shifts, to avoid relying on
1805 characteristics of the compiler used to compile gas. */
1806 resultP
->X_add_number
=
1807 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1809 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1810 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1811 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1812 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1813 case O_add
: resultP
->X_add_number
+= v
; break;
1814 case O_subtract
: resultP
->X_add_number
-= v
; break;
1816 resultP
->X_add_number
=
1817 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1820 resultP
->X_add_number
=
1821 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1824 resultP
->X_add_number
=
1825 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1828 resultP
->X_add_number
=
1829 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1832 resultP
->X_add_number
=
1833 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1836 resultP
->X_add_number
=
1837 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1840 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1843 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1847 else if (resultP
->X_op
== O_symbol
1848 && right
.X_op
== O_symbol
1849 && (op_left
== O_add
1850 || op_left
== O_subtract
1851 || (resultP
->X_add_number
== 0
1852 && right
.X_add_number
== 0)))
1854 /* Symbol OP symbol. */
1855 resultP
->X_op
= op_left
;
1856 resultP
->X_op_symbol
= right
.X_add_symbol
;
1857 if (op_left
== O_add
)
1858 resultP
->X_add_number
+= right
.X_add_number
;
1859 else if (op_left
== O_subtract
)
1860 resultP
->X_add_number
-= right
.X_add_number
;
1864 /* The general case. */
1865 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1866 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1867 resultP
->X_op
= op_left
;
1868 resultP
->X_add_number
= 0;
1869 resultP
->X_unsigned
= 1;
1873 } /* While next operator is >= this rank. */
1875 /* The PA port needs this information. */
1876 if (resultP
->X_add_symbol
)
1877 symbol_mark_used (resultP
->X_add_symbol
);
1879 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1885 * This lives here because it belongs equally in expr.c & read.c.
1886 * Expr.c is just a branch office read.c anyway, and putting it
1887 * here lessens the crowd at read.c.
1889 * Assume input_line_pointer is at start of symbol name.
1890 * Advance input_line_pointer past symbol name.
1891 * Turn that character into a '\0', returning its former value.
1892 * This allows a string compare (RMS wants symbol names to be strings)
1893 * of the symbol name.
1894 * There will always be a char following symbol name, because all good
1895 * lines end in end-of-line.
1902 /* We accept \001 in a name in case this is being called with a
1903 constructed string. */
1904 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1906 while (is_part_of_name (c
= *input_line_pointer
++)
1909 if (is_name_ender (c
))
1910 c
= *input_line_pointer
++;
1912 *--input_line_pointer
= 0;
1918 get_single_number ()
1922 return exp
.X_add_number
;