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
;
70 struct expr_symbol_line
*n
;
72 if (expressionP
->X_op
== O_symbol
73 && expressionP
->X_add_number
== 0)
74 return expressionP
->X_add_symbol
;
76 if (expressionP
->X_op
== O_big
)
78 /* This won't work, because the actual value is stored in
79 generic_floating_point_number or generic_bignum, and we are
80 going to lose it if we haven't already. */
81 if (expressionP
->X_add_number
> 0)
82 as_bad (_("bignum invalid; zero assumed"));
84 as_bad (_("floating point number invalid; zero assumed"));
85 zero
.X_op
= O_constant
;
86 zero
.X_add_number
= 0;
88 clean_up_expression (&zero
);
92 fake
= FAKE_LABEL_NAME
;
94 /* Putting constant symbols in absolute_section rather than
95 expr_section is convenient for the old a.out code, for which
96 S_GET_SEGMENT does not always retrieve the value put in by
98 symbolP
= symbol_create (fake
,
99 (expressionP
->X_op
== O_constant
102 0, &zero_address_frag
);
103 symbolP
->sy_value
= *expressionP
;
105 if (expressionP
->X_op
== O_constant
)
106 resolve_symbol_value (symbolP
, 1);
108 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
110 as_where (&n
->file
, &n
->line
);
111 n
->next
= expr_symbol_lines
;
112 expr_symbol_lines
= n
;
117 /* Return the file and line number for an expr symbol. Return
118 non-zero if something was found, 0 if no information is known for
122 expr_symbol_where (sym
, pfile
, pline
)
127 register struct expr_symbol_line
*l
;
129 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
142 /* Utilities for building expressions.
143 Since complex expressions are recorded as symbols for use in other
144 expressions these return a symbolS * and not an expressionS *.
145 These explicitly do not take an "add_number" argument. */
146 /* ??? For completeness' sake one might want expr_build_symbol.
147 It would just return its argument. */
149 /* Build an expression for an unsigned constant.
150 The corresponding one for signed constants is missing because
151 there's currently no need for it. One could add an unsigned_p flag
152 but that seems more clumsy. */
155 expr_build_uconstant (value
)
161 e
.X_add_number
= value
;
163 return make_expr_symbol (&e
);
166 /* Build an expression for OP s1. */
169 expr_build_unary (op
, s1
)
178 return make_expr_symbol (&e
);
181 /* Build an expression for s1 OP s2. */
184 expr_build_binary (op
, s1
, s2
)
195 return make_expr_symbol (&e
);
198 /* Build an expression for the current location ('.'). */
205 current_location (&e
);
206 return make_expr_symbol (&e
);
210 * Build any floating-point literal here.
211 * 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];
220 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) */
228 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
229 int generic_floating_point_magic
;
232 floating_constant (expressionP
)
233 expressionS
*expressionP
;
235 /* input_line_pointer->*/
236 /* 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, probably assembling junk"));
250 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code
);
253 expressionP
->X_op
= O_big
;
254 /* input_line_pointer->just after constant, */
255 /* which may point to whitespace. */
256 expressionP
->X_add_number
= -1;
260 generic_bignum_to_int32 ()
263 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
264 | (generic_bignum
[0] & LITTLENUM_MASK
);
265 number
&= 0xffffffff;
271 generic_bignum_to_int64 ()
274 ((((((((valueT
) generic_bignum
[3] & LITTLENUM_MASK
)
275 << LITTLENUM_NUMBER_OF_BITS
)
276 | ((valueT
) generic_bignum
[2] & LITTLENUM_MASK
))
277 << LITTLENUM_NUMBER_OF_BITS
)
278 | ((valueT
) generic_bignum
[1] & LITTLENUM_MASK
))
279 << LITTLENUM_NUMBER_OF_BITS
)
280 | ((valueT
) generic_bignum
[0] & LITTLENUM_MASK
));
286 integer_constant (radix
, expressionP
)
288 expressionS
*expressionP
;
290 char *start
; /* start of number. */
293 valueT number
; /* offset or (absolute) value */
294 short int digit
; /* value of next digit in current radix */
295 short int maxdig
= 0;/* highest permitted digit value. */
296 int too_many_digits
= 0; /* if we see >= this number of */
297 char *name
; /* points to name of symbol */
298 symbolS
*symbolP
; /* points to symbol */
300 int small
; /* true if fits in 32 bits. */
302 /* May be bignum, or may fit in 32 bits. */
303 /* Most numbers fit into 32 bits, and we want this case to be fast.
304 so we pretend it will fit into 32 bits. If, after making up a 32
305 bit number, we realise that we have scanned more digits than
306 comfortably fit into 32 bits, we re-scan the digits coding them
307 into a bignum. For decimal and octal numbers we are
308 conservative: Some numbers may be assumed bignums when in fact
309 they do fit into 32 bits. Numbers of any radix can have excess
310 leading zeros: We strive to recognise this and cast them back
311 into 32 bits. We must check that the bignum really is more than
312 32 bits, and change it back to a 32-bit number if it fits. The
313 number we are looking for is expected to be positive, but if it
314 fits into 32 bits as an unsigned number, we let it be a 32-bit
315 number. The cavalier approach is for speed in ordinary cases. */
316 /* This has been extended for 64 bits. We blindly assume that if
317 you're compiling in 64-bit mode, the target is a 64-bit machine.
318 This should be cleaned up. */
322 #else /* includes non-bfd case, mostly */
326 if (flag_m68k_mri
&& radix
== 0)
330 /* In MRI mode, the number may have a suffix indicating the
331 radix. For that matter, it might actually be a floating
333 for (suffix
= input_line_pointer
;
334 isalnum ((unsigned char) *suffix
);
337 if (*suffix
== 'e' || *suffix
== 'E')
341 if (suffix
== input_line_pointer
)
349 if (islower ((unsigned char) c
))
355 else if (c
== 'O' || c
== 'Q')
359 else if (suffix
[1] == '.' || c
== 'E' || flt
)
361 floating_constant (expressionP
);
376 too_many_digits
= valuesize
+ 1;
380 too_many_digits
= (valuesize
+ 2) / 3 + 1;
384 too_many_digits
= (valuesize
+ 3) / 4 + 1;
388 too_many_digits
= (valuesize
+ 12) / 4; /* very rough */
391 start
= input_line_pointer
;
392 c
= *input_line_pointer
++;
394 (digit
= hex_value (c
)) < maxdig
;
395 c
= *input_line_pointer
++)
397 number
= number
* radix
+ digit
;
399 /* c contains character after number. */
400 /* input_line_pointer->char after c. */
401 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
403 if (radix
== 16 && c
== '_')
405 /* This is literal of the form 0x333_0_12345678_1.
406 This example is equivalent to 0x00000333000000001234567800000001. */
408 int num_little_digits
= 0;
410 input_line_pointer
= start
; /*->1st digit. */
412 know (LITTLENUM_NUMBER_OF_BITS
== 16);
414 for (c
= '_'; c
== '_'; num_little_digits
+=2)
417 /* Convert one 64-bit word. */
420 for (c
= *input_line_pointer
++;
421 (digit
= hex_value (c
)) < maxdig
;
422 c
= *(input_line_pointer
++))
424 number
= number
* radix
+ digit
;
428 /* Check for 8 digit per word max. */
430 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
432 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
433 know (LITTLENUM_NUMBER_OF_BITS
== 16);
434 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1); i
>= 2; i
--)
435 generic_bignum
[i
] = generic_bignum
[i
-2];
437 /* Add the new digits as the least significant new ones. */
438 generic_bignum
[0] = number
& 0xffffffff;
439 generic_bignum
[1] = number
>> 16;
442 /* Again, c is char after number, input_line_pointer->after c. */
444 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
445 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
447 assert (num_little_digits
>= 4);
449 if (num_little_digits
!= 8)
450 as_bad (_("A bignum with underscores must have exactly 4 words."));
452 /* We might have some leading zeros. These can be trimmed to give
453 * us a change to fit this constant into a small number.
455 while (generic_bignum
[num_little_digits
-1] == 0 && num_little_digits
> 1)
458 if (num_little_digits
<= 2)
460 /* will fit into 32 bits. */
461 number
= generic_bignum_to_int32 ();
465 else if (num_little_digits
<= 4)
467 /* Will fit into 64 bits. */
468 number
= generic_bignum_to_int64 ();
475 number
= num_little_digits
; /* number of littlenums in the bignum. */
481 * we saw a lot of digits. manufacture a bignum the hard way.
483 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
484 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
487 leader
= generic_bignum
;
488 generic_bignum
[0] = 0;
489 generic_bignum
[1] = 0;
490 generic_bignum
[2] = 0;
491 generic_bignum
[3] = 0;
492 input_line_pointer
= start
; /*->1st digit. */
493 c
= *input_line_pointer
++;
495 (carry
= hex_value (c
)) < maxdig
;
496 c
= *input_line_pointer
++)
498 for (pointer
= generic_bignum
;
504 work
= carry
+ radix
* *pointer
;
505 *pointer
= work
& LITTLENUM_MASK
;
506 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
510 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
512 /* room to grow a longer bignum. */
517 /* again, c is char after number, */
518 /* input_line_pointer->after c. */
519 know (LITTLENUM_NUMBER_OF_BITS
== 16);
520 if (leader
< generic_bignum
+ 2)
522 /* will fit into 32 bits. */
523 number
= generic_bignum_to_int32 ();
527 else if (leader
< generic_bignum
+ 4)
529 /* Will fit into 64 bits. */
530 number
= generic_bignum_to_int64 ();
536 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
540 if (flag_m68k_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
541 c
= *input_line_pointer
++;
546 * here with number, in correct radix. c is the next char.
547 * note that unlike un*x, we allow "011f" "0x9f" to
548 * both mean the same as the (conventional) "9f". this is simply easier
549 * than checking for strict canonical form. syntax sux!
552 if (LOCAL_LABELS_FB
&& c
== 'b')
555 * backward ref to local label.
556 * because it is backward, expect it to be defined.
558 /* Construct a local label. */
559 name
= fb_label_name ((int) number
, 0);
561 /* seen before, or symbol is defined: ok */
562 symbolP
= symbol_find (name
);
563 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
565 /* local labels are never absolute. don't waste time
566 checking absoluteness. */
567 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
569 expressionP
->X_op
= O_symbol
;
570 expressionP
->X_add_symbol
= symbolP
;
574 /* either not seen or not defined. */
575 /* @@ Should print out the original string instead of
576 the parsed number. */
577 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
579 expressionP
->X_op
= O_constant
;
582 expressionP
->X_add_number
= 0;
584 else if (LOCAL_LABELS_FB
&& c
== 'f')
587 * forward reference. expect symbol to be undefined or
588 * unknown. undefined: seen it before. unknown: never seen
590 * construct a local label name, then an undefined symbol.
591 * don't create a xseg frag for it: caller may do that.
592 * just return it as never seen before.
594 name
= fb_label_name ((int) number
, 1);
595 symbolP
= symbol_find_or_make (name
);
596 /* we have no need to check symbol properties. */
597 #ifndef many_segments
598 /* since "know" puts its arg into a "string", we
599 can't have newlines in the argument. */
600 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
602 expressionP
->X_op
= O_symbol
;
603 expressionP
->X_add_symbol
= symbolP
;
604 expressionP
->X_add_number
= 0;
606 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
608 /* If the dollar label is *currently* defined, then this is just
609 another reference to it. If it is not *currently* defined,
610 then this is a fresh instantiation of that number, so create
613 if (dollar_label_defined ((long) number
))
615 name
= dollar_label_name ((long) number
, 0);
616 symbolP
= symbol_find (name
);
617 know (symbolP
!= NULL
);
621 name
= dollar_label_name ((long) number
, 1);
622 symbolP
= symbol_find_or_make (name
);
625 expressionP
->X_op
= O_symbol
;
626 expressionP
->X_add_symbol
= symbolP
;
627 expressionP
->X_add_number
= 0;
631 expressionP
->X_op
= O_constant
;
632 #ifdef TARGET_WORD_SIZE
633 /* Sign extend NUMBER. */
634 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
636 expressionP
->X_add_number
= number
;
637 input_line_pointer
--; /* restore following character. */
638 } /* really just a number */
642 /* not a small number */
643 expressionP
->X_op
= O_big
;
644 expressionP
->X_add_number
= number
; /* number of littlenums */
645 input_line_pointer
--; /*->char following number. */
649 /* Parse an MRI multi character constant. */
652 mri_char_constant (expressionP
)
653 expressionS
*expressionP
;
657 if (*input_line_pointer
== '\''
658 && input_line_pointer
[1] != '\'')
660 expressionP
->X_op
= O_constant
;
661 expressionP
->X_add_number
= 0;
665 /* In order to get the correct byte ordering, we must build the
666 number in reverse. */
667 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
671 generic_bignum
[i
] = 0;
672 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
674 if (*input_line_pointer
== '\'')
676 if (input_line_pointer
[1] != '\'')
678 ++input_line_pointer
;
680 generic_bignum
[i
] <<= 8;
681 generic_bignum
[i
] += *input_line_pointer
;
682 ++input_line_pointer
;
685 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
687 /* If there is more than one littlenum, left justify the
688 last one to make it match the earlier ones. If there is
689 only one, we can just use the value directly. */
690 for (; j
< CHARS_PER_LITTLENUM
; j
++)
691 generic_bignum
[i
] <<= 8;
694 if (*input_line_pointer
== '\''
695 && input_line_pointer
[1] != '\'')
701 as_bad (_("Character constant too large"));
710 c
= SIZE_OF_LARGE_NUMBER
- i
;
711 for (j
= 0; j
< c
; j
++)
712 generic_bignum
[j
] = generic_bignum
[i
+ j
];
716 know (LITTLENUM_NUMBER_OF_BITS
== 16);
719 expressionP
->X_op
= O_big
;
720 expressionP
->X_add_number
= i
;
724 expressionP
->X_op
= O_constant
;
726 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
728 expressionP
->X_add_number
=
729 (((generic_bignum
[1] & LITTLENUM_MASK
)
730 << LITTLENUM_NUMBER_OF_BITS
)
731 | (generic_bignum
[0] & LITTLENUM_MASK
));
734 /* Skip the final closing quote. */
735 ++input_line_pointer
;
738 /* Return an expression representing the current location. This
739 handles the magic symbol `.'. */
742 current_location (expressionp
)
743 expressionS
*expressionp
;
745 if (now_seg
== absolute_section
)
747 expressionp
->X_op
= O_constant
;
748 expressionp
->X_add_number
= abs_section_offset
;
754 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
755 (valueT
) frag_now_fix (),
757 expressionp
->X_op
= O_symbol
;
758 expressionp
->X_add_symbol
= symbolp
;
759 expressionp
->X_add_number
= 0;
764 * Summary of operand().
766 * in: Input_line_pointer points to 1st char of operand, which may
769 * out: A expressionS.
770 * The operand may have been empty: in this case X_op == O_absent.
771 * Input_line_pointer->(next non-blank) char after operand.
775 operand (expressionP
)
776 expressionS
*expressionP
;
779 symbolS
*symbolP
; /* points to symbol */
780 char *name
; /* points to name of symbol */
783 /* All integers are regarded as unsigned unless they are negated.
784 This is because the only thing which cares whether a number is
785 unsigned is the code in emit_expr which extends constants into
786 bignums. It should only sign extend negative numbers, so that
787 something like ``.quad 0x80000000'' is not sign extended even
788 though it appears negative if valueT is 32 bits. */
789 expressionP
->X_unsigned
= 1;
791 /* digits, assume it is a bignum. */
793 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
794 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
807 input_line_pointer
--;
809 integer_constant (flag_m68k_mri
? 0 : 10, expressionP
);
813 /* non-decimal radix */
819 /* Check for a hex constant. */
820 for (s
= input_line_pointer
; hex_p (*s
); s
++)
822 if (*s
== 'h' || *s
== 'H')
824 --input_line_pointer
;
825 integer_constant (0, expressionP
);
830 c
= *input_line_pointer
;
841 integer_constant (0, expressionP
);
847 if (c
&& strchr (FLT_CHARS
, c
))
849 input_line_pointer
++;
850 floating_constant (expressionP
);
851 expressionP
->X_add_number
=
852 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
856 /* The string was only zero */
857 expressionP
->X_op
= O_constant
;
858 expressionP
->X_add_number
= 0;
867 input_line_pointer
++;
868 integer_constant (16, expressionP
);
872 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
)
874 /* This code used to check for '+' and '-' here, and, in
875 some conditions, fall through to call
876 integer_constant. However, that didn't make sense,
877 as integer_constant only accepts digits. */
878 /* Some of our code elsewhere does permit digits greater
879 than the expected base; for consistency, do the same
881 if (input_line_pointer
[1] < '0'
882 || input_line_pointer
[1] > '9')
884 /* Parse this as a back reference to label 0. */
885 input_line_pointer
--;
886 integer_constant (10, expressionP
);
889 /* Otherwise, parse this as a binary number. */
893 input_line_pointer
++;
896 integer_constant (2, expressionP
);
907 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
913 /* If it says "0f" and it could possibly be a floating point
914 number, make it one. Otherwise, make it a local label,
915 and try to deal with parsing the rest later. */
916 if (!input_line_pointer
[1]
917 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
920 char *cp
= input_line_pointer
+ 1;
921 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
922 &generic_floating_point_number
);
926 case ERROR_EXPONENT_OVERFLOW
:
927 if (*cp
== 'f' || *cp
== 'b')
928 /* looks like a difference expression */
930 else if (cp
== input_line_pointer
+ 1)
931 /* No characters has been accepted -- looks like
937 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
942 /* Okay, now we've sorted it out. We resume at one of these
943 two labels, depending on what we've decided we're probably
946 input_line_pointer
--;
947 integer_constant (10, expressionP
);
959 integer_constant (0, expressionP
);
969 input_line_pointer
++;
970 floating_constant (expressionP
);
971 expressionP
->X_add_number
=
972 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
976 if (LOCAL_LABELS_DOLLAR
)
978 integer_constant (10, expressionP
);
989 /* didn't begin with digit & not a name */
990 segment
= expression (expressionP
);
991 /* Expression() will pass trailing whitespace */
992 if ((c
== '(' && *input_line_pointer
++ != ')')
993 || (c
== '[' && *input_line_pointer
++ != ']'))
995 as_bad (_("Missing ')' assumed"));
996 input_line_pointer
--;
999 /* here with input_line_pointer->char after "(...)" */
1003 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1005 as_bad (_("EBCDIC constants are not supported"));
1008 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
1010 ++input_line_pointer
;
1013 if (! flag_m68k_mri
)
1015 /* Warning: to conform to other people's assemblers NO
1016 ESCAPEMENT is permitted for a single quote. The next
1017 character, parity errors and all, is taken as the value
1018 of the operand. VERY KINKY. */
1019 expressionP
->X_op
= O_constant
;
1020 expressionP
->X_add_number
= *input_line_pointer
++;
1024 mri_char_constant (expressionP
);
1028 (void) operand (expressionP
);
1032 /* Double quote is the bitwise not operator in MRI mode. */
1033 if (! flag_m68k_mri
)
1037 /* ~ is permitted to start a label on the Delta. */
1038 if (is_name_beginner (c
))
1043 operand (expressionP
);
1044 if (expressionP
->X_op
== O_constant
)
1046 /* input_line_pointer -> char after operand */
1049 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1050 /* Notice: '-' may overflow: no warning is given. This is
1051 compatible with other people's assemblers. Sigh. */
1052 expressionP
->X_unsigned
= 0;
1054 else if (c
== '~' || c
== '"')
1055 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1057 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1059 else if (expressionP
->X_op
!= O_illegal
1060 && expressionP
->X_op
!= O_absent
)
1062 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1064 expressionP
->X_op
= O_uminus
;
1065 else if (c
== '~' || c
== '"')
1066 expressionP
->X_op
= O_bit_not
;
1068 expressionP
->X_op
= O_logical_not
;
1069 expressionP
->X_add_number
= 0;
1072 as_warn (_("Unary operator %c ignored because bad operand follows"),
1078 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1081 if (! flag_m68k_mri
)
1084 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
1086 /* In MRI mode, $ is also used as the prefix for a
1087 hexadecimal constant. */
1088 integer_constant (16, expressionP
);
1092 if (is_part_of_name (*input_line_pointer
))
1095 current_location (expressionP
);
1099 if (!is_part_of_name (*input_line_pointer
))
1101 current_location (expressionP
);
1104 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1105 && ! is_part_of_name (input_line_pointer
[8]))
1106 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1107 && ! is_part_of_name (input_line_pointer
[7])))
1111 start
= (input_line_pointer
[1] == 't'
1112 || input_line_pointer
[1] == 'T');
1113 input_line_pointer
+= start
? 8 : 7;
1115 if (*input_line_pointer
!= '(')
1116 as_bad (_("syntax error in .startof. or .sizeof."));
1121 ++input_line_pointer
;
1123 name
= input_line_pointer
;
1124 c
= get_symbol_end ();
1126 buf
= (char *) xmalloc (strlen (name
) + 10);
1128 sprintf (buf
, ".startof.%s", name
);
1130 sprintf (buf
, ".sizeof.%s", name
);
1131 symbolP
= symbol_make (buf
);
1134 expressionP
->X_op
= O_symbol
;
1135 expressionP
->X_add_symbol
= symbolP
;
1136 expressionP
->X_add_number
= 0;
1138 *input_line_pointer
= c
;
1140 if (*input_line_pointer
!= ')')
1141 as_bad (_("syntax error in .startof. or .sizeof."));
1143 ++input_line_pointer
;
1155 /* can't imagine any other kind of operand */
1156 expressionP
->X_op
= O_absent
;
1157 input_line_pointer
--;
1161 if (! flag_m68k_mri
)
1163 integer_constant (2, expressionP
);
1167 if (! flag_m68k_mri
)
1169 integer_constant (8, expressionP
);
1173 if (! flag_m68k_mri
)
1176 /* In MRI mode, this is a floating point constant represented
1177 using hexadecimal digits. */
1179 ++input_line_pointer
;
1180 integer_constant (16, expressionP
);
1184 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1187 current_location (expressionP
);
1192 if (is_end_of_line
[(unsigned char) c
])
1194 if (is_name_beginner (c
)) /* here if did not begin with a digit */
1197 * Identifier begins here.
1198 * This is kludged for speed, so code is repeated.
1201 name
= --input_line_pointer
;
1202 c
= get_symbol_end ();
1204 #ifdef md_parse_name
1205 /* This is a hook for the backend to parse certain names
1206 specially in certain contexts. If a name always has a
1207 specific value, it can often be handled by simply
1208 entering it in the symbol table. */
1209 if (md_parse_name (name
, expressionP
))
1211 *input_line_pointer
= c
;
1217 /* The MRI i960 assembler permits
1219 FIXME: This should use md_parse_name. */
1221 && (strcasecmp (name
, "sizeof") == 0
1222 || strcasecmp (name
, "startof") == 0))
1227 start
= (name
[1] == 't'
1230 *input_line_pointer
= c
;
1233 name
= input_line_pointer
;
1234 c
= get_symbol_end ();
1236 buf
= (char *) xmalloc (strlen (name
) + 10);
1238 sprintf (buf
, ".startof.%s", name
);
1240 sprintf (buf
, ".sizeof.%s", name
);
1241 symbolP
= symbol_make (buf
);
1244 expressionP
->X_op
= O_symbol
;
1245 expressionP
->X_add_symbol
= symbolP
;
1246 expressionP
->X_add_number
= 0;
1248 *input_line_pointer
= c
;
1255 symbolP
= symbol_find_or_make (name
);
1257 /* If we have an absolute symbol or a reg, then we know its
1259 segment
= S_GET_SEGMENT (symbolP
);
1260 if (segment
== absolute_section
)
1262 expressionP
->X_op
= O_constant
;
1263 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1265 else if (segment
== reg_section
)
1267 expressionP
->X_op
= O_register
;
1268 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1272 expressionP
->X_op
= O_symbol
;
1273 expressionP
->X_add_symbol
= symbolP
;
1274 expressionP
->X_add_number
= 0;
1276 *input_line_pointer
= c
;
1280 /* Let the target try to parse it. Success is indicated by changing
1281 the X_op field to something other than O_absent and pointing
1282 input_line_pointer passed the expression. If it can't parse the
1283 expression, X_op and input_line_pointer should be unchanged. */
1284 expressionP
->X_op
= O_absent
;
1285 --input_line_pointer
;
1286 md_operand (expressionP
);
1287 if (expressionP
->X_op
== O_absent
)
1289 ++input_line_pointer
;
1290 as_bad (_("Bad expression"));
1291 expressionP
->X_op
= O_constant
;
1292 expressionP
->X_add_number
= 0;
1299 * It is more 'efficient' to clean up the expressionS when they are created.
1300 * Doing it here saves lines of code.
1302 clean_up_expression (expressionP
);
1303 SKIP_WHITESPACE (); /*->1st char after operand. */
1304 know (*input_line_pointer
!= ' ');
1306 /* The PA port needs this information. */
1307 if (expressionP
->X_add_symbol
)
1308 expressionP
->X_add_symbol
->sy_used
= 1;
1310 switch (expressionP
->X_op
)
1313 return absolute_section
;
1315 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1321 /* Internal. Simplify a struct expression for use by expr() */
1324 * In: address of a expressionS.
1325 * The X_op field of the expressionS may only take certain values.
1326 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1327 * Out: expressionS may have been modified:
1328 * 'foo-foo' symbol references cancelled to 0,
1329 * which changes X_op from O_subtract to O_constant.
1330 * Unused fields zeroed to help expr().
1334 clean_up_expression (expressionP
)
1335 expressionS
*expressionP
;
1337 switch (expressionP
->X_op
)
1341 expressionP
->X_add_number
= 0;
1346 expressionP
->X_add_symbol
= NULL
;
1351 expressionP
->X_op_symbol
= NULL
;
1354 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1355 || ((expressionP
->X_op_symbol
->sy_frag
1356 == expressionP
->X_add_symbol
->sy_frag
)
1357 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1358 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1359 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1361 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1362 - S_GET_VALUE (expressionP
->X_op_symbol
));
1364 expressionP
->X_op
= O_constant
;
1365 expressionP
->X_add_symbol
= NULL
;
1366 expressionP
->X_op_symbol
= NULL
;
1367 expressionP
->X_add_number
+= diff
;
1375 /* Expression parser. */
1378 * We allow an empty expression, and just assume (absolute,0) silently.
1379 * Unary operators and parenthetical expressions are treated as operands.
1380 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1382 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1383 * warped that I flushed it and wrote a recursive-descent parser instead.
1384 * Now things are stable, would anybody like to write a fast parser?
1385 * Most expressions are either register (which does not even reach here)
1386 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1387 * So I guess it doesn't really matter how inefficient more complex expressions
1390 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1391 * Also, we have consumed any leading or trailing spaces (operand does that)
1392 * and done all intervening operators.
1394 * This returns the segment of the result, which will be
1395 * absolute_section or the segment of a symbol.
1399 #define __ O_illegal
1401 static const operatorT op_encoding
[256] =
1402 { /* maps ASCII->operators */
1404 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1405 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1407 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1408 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1409 __
, __
, __
, __
, __
, __
, __
, __
,
1410 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1411 __
, __
, __
, __
, __
, __
, __
, __
,
1412 __
, __
, __
, __
, __
, __
, __
, __
,
1413 __
, __
, __
, __
, __
, __
, __
, __
,
1414 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1415 __
, __
, __
, __
, __
, __
, __
, __
,
1416 __
, __
, __
, __
, __
, __
, __
, __
,
1417 __
, __
, __
, __
, __
, __
, __
, __
,
1418 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1420 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1421 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1422 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1423 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1424 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1425 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1426 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1427 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1433 * 0 operand, (expression)
1438 * 5 used for * / % in MRI mode
1443 static operator_rankT op_rank
[] =
1449 0, /* O_symbol_rva */
1454 8, /* O_logical_not */
1458 7, /* O_left_shift */
1459 7, /* O_right_shift */
1460 6, /* O_bit_inclusive_or */
1461 6, /* O_bit_or_not */
1462 6, /* O_bit_exclusive_or */
1472 2, /* O_logical_and */
1473 1 /* O_logical_or */
1476 /* Unfortunately, in MRI mode for the m68k, multiplication and
1477 division have lower precedence than the bit wise operators. This
1478 function sets the operator precedences correctly for the current
1479 mode. Also, MRI uses a different bit_not operator, and this fixes
1482 #define STANDARD_MUL_PRECEDENCE (7)
1483 #define MRI_MUL_PRECEDENCE (5)
1486 expr_set_precedence ()
1490 op_rank
[O_multiply
] = MRI_MUL_PRECEDENCE
;
1491 op_rank
[O_divide
] = MRI_MUL_PRECEDENCE
;
1492 op_rank
[O_modulus
] = MRI_MUL_PRECEDENCE
;
1496 op_rank
[O_multiply
] = STANDARD_MUL_PRECEDENCE
;
1497 op_rank
[O_divide
] = STANDARD_MUL_PRECEDENCE
;
1498 op_rank
[O_modulus
] = STANDARD_MUL_PRECEDENCE
;
1502 /* Initialize the expression parser. */
1507 expr_set_precedence ();
1509 /* Verify that X_op field is wide enough. */
1513 assert (e
.X_op
== O_max
);
1517 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1518 Advance INPUT_LINE_POINTER to the last character in the operator
1519 (i.e., don't change it for a single character operator). */
1521 static inline operatorT
1527 c
= *input_line_pointer
& 0xff;
1532 return op_encoding
[c
];
1535 switch (input_line_pointer
[1])
1538 return op_encoding
[c
];
1549 ++input_line_pointer
;
1553 if (input_line_pointer
[1] != '=')
1554 return op_encoding
[c
];
1556 ++input_line_pointer
;
1560 switch (input_line_pointer
[1])
1563 return op_encoding
[c
];
1565 ret
= O_right_shift
;
1571 ++input_line_pointer
;
1575 /* We accept !! as equivalent to ^ for MRI compatibility. */
1576 if (input_line_pointer
[1] != '!')
1579 return O_bit_inclusive_or
;
1580 return op_encoding
[c
];
1582 ++input_line_pointer
;
1583 return O_bit_exclusive_or
;
1586 if (input_line_pointer
[1] != '|')
1587 return op_encoding
[c
];
1589 ++input_line_pointer
;
1590 return O_logical_or
;
1593 if (input_line_pointer
[1] != '&')
1594 return op_encoding
[c
];
1596 ++input_line_pointer
;
1597 return O_logical_and
;
1603 /* Parse an expression. */
1606 expr (rank
, resultP
)
1607 operator_rankT rank
; /* Larger # is higher rank. */
1608 expressionS
*resultP
; /* Deliver result here. */
1617 retval
= operand (resultP
);
1619 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1621 op_left
= operator ();
1622 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1626 input_line_pointer
++; /*->after 1st character of operator. */
1628 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1629 if (right
.X_op
== O_absent
)
1631 as_warn (_("missing operand; zero assumed"));
1632 right
.X_op
= O_constant
;
1633 right
.X_add_number
= 0;
1634 right
.X_add_symbol
= NULL
;
1635 right
.X_op_symbol
= NULL
;
1638 know (*input_line_pointer
!= ' ');
1640 if (retval
== undefined_section
)
1642 if (SEG_NORMAL (rightseg
))
1645 else if (! SEG_NORMAL (retval
))
1647 else if (SEG_NORMAL (rightseg
)
1648 && retval
!= rightseg
1650 && op_left
!= O_subtract
1653 as_bad (_("operation combines symbols in different segments"));
1655 op_right
= operator ();
1657 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1658 know ((int) op_left
>= (int) O_multiply
1659 && (int) op_left
<= (int) O_logical_or
);
1661 /* input_line_pointer->after right-hand quantity. */
1662 /* left-hand quantity in resultP */
1663 /* right-hand quantity in right. */
1664 /* operator in op_left. */
1666 if (resultP
->X_op
== O_big
)
1668 if (resultP
->X_add_number
> 0)
1669 as_warn (_("left operand is a bignum; integer 0 assumed"));
1671 as_warn (_("left operand is a float; integer 0 assumed"));
1672 resultP
->X_op
= O_constant
;
1673 resultP
->X_add_number
= 0;
1674 resultP
->X_add_symbol
= NULL
;
1675 resultP
->X_op_symbol
= NULL
;
1677 if (right
.X_op
== O_big
)
1679 if (right
.X_add_number
> 0)
1680 as_warn (_("right operand is a bignum; integer 0 assumed"));
1682 as_warn (_("right operand is a float; integer 0 assumed"));
1683 right
.X_op
= O_constant
;
1684 right
.X_add_number
= 0;
1685 right
.X_add_symbol
= NULL
;
1686 right
.X_op_symbol
= NULL
;
1689 /* Optimize common cases. */
1690 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1693 resultP
->X_add_number
+= right
.X_add_number
;
1695 /* This case comes up in PIC code. */
1696 else if (op_left
== O_subtract
1697 && right
.X_op
== O_symbol
1698 && resultP
->X_op
== O_symbol
1699 && (right
.X_add_symbol
->sy_frag
1700 == resultP
->X_add_symbol
->sy_frag
)
1701 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1704 resultP
->X_add_number
-= right
.X_add_number
;
1705 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1706 - S_GET_VALUE (right
.X_add_symbol
));
1707 resultP
->X_op
= O_constant
;
1708 resultP
->X_add_symbol
= 0;
1710 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1713 resultP
->X_add_number
-= right
.X_add_number
;
1715 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1718 resultP
->X_op
= right
.X_op
;
1719 resultP
->X_add_symbol
= right
.X_add_symbol
;
1720 resultP
->X_op_symbol
= right
.X_op_symbol
;
1721 resultP
->X_add_number
+= right
.X_add_number
;
1724 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1726 /* Constant OP constant. */
1727 offsetT v
= right
.X_add_number
;
1728 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1730 as_warn (_("division by zero"));
1736 case O_multiply
: resultP
->X_add_number
*= v
; break;
1737 case O_divide
: resultP
->X_add_number
/= v
; break;
1738 case O_modulus
: resultP
->X_add_number
%= v
; break;
1739 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1741 /* We always use unsigned shifts, to avoid relying on
1742 characteristics of the compiler used to compile gas. */
1743 resultP
->X_add_number
=
1744 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1746 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1747 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1748 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1749 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1750 case O_add
: resultP
->X_add_number
+= v
; break;
1751 case O_subtract
: resultP
->X_add_number
-= v
; break;
1753 resultP
->X_add_number
=
1754 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1757 resultP
->X_add_number
=
1758 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1761 resultP
->X_add_number
=
1762 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1765 resultP
->X_add_number
=
1766 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1769 resultP
->X_add_number
=
1770 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1773 resultP
->X_add_number
=
1774 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1777 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1780 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1784 else if (resultP
->X_op
== O_symbol
1785 && right
.X_op
== O_symbol
1786 && (op_left
== O_add
1787 || op_left
== O_subtract
1788 || (resultP
->X_add_number
== 0
1789 && right
.X_add_number
== 0)))
1791 /* Symbol OP symbol. */
1792 resultP
->X_op
= op_left
;
1793 resultP
->X_op_symbol
= right
.X_add_symbol
;
1794 if (op_left
== O_add
)
1795 resultP
->X_add_number
+= right
.X_add_number
;
1796 else if (op_left
== O_subtract
)
1797 resultP
->X_add_number
-= right
.X_add_number
;
1801 /* The general case. */
1802 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1803 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1804 resultP
->X_op
= op_left
;
1805 resultP
->X_add_number
= 0;
1806 resultP
->X_unsigned
= 1;
1810 } /* While next operator is >= this rank. */
1812 /* The PA port needs this information. */
1813 if (resultP
->X_add_symbol
)
1814 resultP
->X_add_symbol
->sy_used
= 1;
1816 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1822 * This lives here because it belongs equally in expr.c & read.c.
1823 * Expr.c is just a branch office read.c anyway, and putting it
1824 * here lessens the crowd at read.c.
1826 * Assume input_line_pointer is at start of symbol name.
1827 * Advance input_line_pointer past symbol name.
1828 * Turn that character into a '\0', returning its former value.
1829 * This allows a string compare (RMS wants symbol names to be strings)
1830 * of the symbol name.
1831 * There will always be a char following symbol name, because all good
1832 * lines end in end-of-line.
1839 /* We accept \001 in a name in case this is being called with a
1840 constructed string. */
1841 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1842 while (is_part_of_name (c
= *input_line_pointer
++)
1845 *--input_line_pointer
= 0;
1851 get_single_number ()
1855 return exp
.X_add_number
;