1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997, 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
);
182 * Build any floating-point literal here.
183 * Also build any bignum literal here.
186 /* Seems atof_machine can backscan through generic_bignum and hit whatever
187 happens to be loaded before it in memory. And its way too complicated
188 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
189 and never write into the early words, thus they'll always be zero.
190 I hate Dean's floating-point code. Bleh. */
191 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
192 FLONUM_TYPE generic_floating_point_number
=
194 &generic_bignum
[6], /* low (JF: Was 0) */
195 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high JF: (added +6) */
200 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
201 int generic_floating_point_magic
;
204 floating_constant (expressionP
)
205 expressionS
*expressionP
;
207 /* input_line_pointer->*/
208 /* floating-point constant. */
211 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
212 &generic_floating_point_number
);
216 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
218 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
222 as_bad ("bad floating-point constant: unknown error code=%d.", error_code
);
225 expressionP
->X_op
= O_big
;
226 /* input_line_pointer->just after constant, */
227 /* which may point to whitespace. */
228 expressionP
->X_add_number
= -1;
232 generic_bignum_to_int32 ()
235 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
236 | (generic_bignum
[0] & LITTLENUM_MASK
);
237 number
&= 0xffffffff;
243 generic_bignum_to_int64 ()
246 ((((((((valueT
) generic_bignum
[3] & LITTLENUM_MASK
)
247 << LITTLENUM_NUMBER_OF_BITS
)
248 | ((valueT
) generic_bignum
[2] & LITTLENUM_MASK
))
249 << LITTLENUM_NUMBER_OF_BITS
)
250 | ((valueT
) generic_bignum
[1] & LITTLENUM_MASK
))
251 << LITTLENUM_NUMBER_OF_BITS
)
252 | ((valueT
) generic_bignum
[0] & LITTLENUM_MASK
));
258 integer_constant (radix
, expressionP
)
260 expressionS
*expressionP
;
262 char *start
; /* start of number. */
265 valueT number
; /* offset or (absolute) value */
266 short int digit
; /* value of next digit in current radix */
267 short int maxdig
= 0;/* highest permitted digit value. */
268 int too_many_digits
= 0; /* if we see >= this number of */
269 char *name
; /* points to name of symbol */
270 symbolS
*symbolP
; /* points to symbol */
272 int small
; /* true if fits in 32 bits. */
274 /* May be bignum, or may fit in 32 bits. */
275 /* Most numbers fit into 32 bits, and we want this case to be fast.
276 so we pretend it will fit into 32 bits. If, after making up a 32
277 bit number, we realise that we have scanned more digits than
278 comfortably fit into 32 bits, we re-scan the digits coding them
279 into a bignum. For decimal and octal numbers we are
280 conservative: Some numbers may be assumed bignums when in fact
281 they do fit into 32 bits. Numbers of any radix can have excess
282 leading zeros: We strive to recognise this and cast them back
283 into 32 bits. We must check that the bignum really is more than
284 32 bits, and change it back to a 32-bit number if it fits. The
285 number we are looking for is expected to be positive, but if it
286 fits into 32 bits as an unsigned number, we let it be a 32-bit
287 number. The cavalier approach is for speed in ordinary cases. */
288 /* This has been extended for 64 bits. We blindly assume that if
289 you're compiling in 64-bit mode, the target is a 64-bit machine.
290 This should be cleaned up. */
294 #else /* includes non-bfd case, mostly */
298 if (flag_m68k_mri
&& radix
== 0)
302 /* In MRI mode, the number may have a suffix indicating the
303 radix. For that matter, it might actually be a floating
305 for (suffix
= input_line_pointer
; isalnum (*suffix
); suffix
++)
307 if (*suffix
== 'e' || *suffix
== 'E')
311 if (suffix
== input_line_pointer
)
325 else if (c
== 'O' || c
== 'Q')
329 else if (suffix
[1] == '.' || c
== 'E' || flt
)
331 floating_constant (expressionP
);
346 too_many_digits
= valuesize
+ 1;
350 too_many_digits
= (valuesize
+ 2) / 3 + 1;
354 too_many_digits
= (valuesize
+ 3) / 4 + 1;
358 too_many_digits
= (valuesize
+ 12) / 4; /* very rough */
361 start
= input_line_pointer
;
362 c
= *input_line_pointer
++;
364 (digit
= hex_value (c
)) < maxdig
;
365 c
= *input_line_pointer
++)
367 number
= number
* radix
+ digit
;
369 /* c contains character after number. */
370 /* input_line_pointer->char after c. */
371 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
373 if (radix
== 16 && c
== '_')
375 /* This is literal of the form 0x333_0_12345678_1.
376 This example is equivalent to 0x00000333000000001234567800000001. */
378 int num_little_digits
= 0;
380 input_line_pointer
= start
; /*->1st digit. */
382 know (LITTLENUM_NUMBER_OF_BITS
== 16);
384 for (c
= '_'; c
== '_'; num_little_digits
+=2)
387 /* Convert one 64-bit word. */
390 for (c
= *input_line_pointer
++;
391 (digit
= hex_value (c
)) < maxdig
;
392 c
= *(input_line_pointer
++))
394 number
= number
* radix
+ digit
;
398 /* Check for 8 digit per word max. */
400 as_bad ("An bignum with underscores may not have more than 8 hex digits in any word.");
402 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
403 know (LITTLENUM_NUMBER_OF_BITS
== 16);
404 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1); i
>= 2; i
--)
405 generic_bignum
[i
] = generic_bignum
[i
-2];
407 /* Add the new digits as the least significant new ones. */
408 generic_bignum
[0] = number
& 0xffffffff;
409 generic_bignum
[1] = number
>> 16;
412 /* Again, c is char after number, input_line_pointer->after c. */
414 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
415 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
417 assert (num_little_digits
>= 4);
419 if (num_little_digits
!= 8)
420 as_bad ("A bignum with underscores must have exactly 4 words.");
422 /* We might have some leading zeros. These can be trimmed to give
423 * us a change to fit this constant into a small number.
425 while (generic_bignum
[num_little_digits
-1] == 0 && num_little_digits
> 1)
428 if (num_little_digits
<= 2)
430 /* will fit into 32 bits. */
431 number
= generic_bignum_to_int32 ();
435 else if (num_little_digits
<= 4)
437 /* Will fit into 64 bits. */
438 number
= generic_bignum_to_int64 ();
445 number
= num_little_digits
; /* number of littlenums in the bignum. */
451 * we saw a lot of digits. manufacture a bignum the hard way.
453 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
454 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
457 leader
= generic_bignum
;
458 generic_bignum
[0] = 0;
459 generic_bignum
[1] = 0;
460 generic_bignum
[2] = 0;
461 generic_bignum
[3] = 0;
462 input_line_pointer
= start
; /*->1st digit. */
463 c
= *input_line_pointer
++;
465 (carry
= hex_value (c
)) < maxdig
;
466 c
= *input_line_pointer
++)
468 for (pointer
= generic_bignum
;
474 work
= carry
+ radix
* *pointer
;
475 *pointer
= work
& LITTLENUM_MASK
;
476 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
480 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
482 /* room to grow a longer bignum. */
487 /* again, c is char after number, */
488 /* input_line_pointer->after c. */
489 know (LITTLENUM_NUMBER_OF_BITS
== 16);
490 if (leader
< generic_bignum
+ 2)
492 /* will fit into 32 bits. */
493 number
= generic_bignum_to_int32 ();
497 else if (leader
< generic_bignum
+ 4)
499 /* Will fit into 64 bits. */
500 number
= generic_bignum_to_int64 ();
506 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
510 if (flag_m68k_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
511 c
= *input_line_pointer
++;
516 * here with number, in correct radix. c is the next char.
517 * note that unlike un*x, we allow "011f" "0x9f" to
518 * both mean the same as the (conventional) "9f". this is simply easier
519 * than checking for strict canonical form. syntax sux!
522 if (LOCAL_LABELS_FB
&& c
== 'b')
525 * backward ref to local label.
526 * because it is backward, expect it to be defined.
528 /* Construct a local label. */
529 name
= fb_label_name ((int) number
, 0);
531 /* seen before, or symbol is defined: ok */
532 symbolP
= symbol_find (name
);
533 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
535 /* local labels are never absolute. don't waste time
536 checking absoluteness. */
537 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
539 expressionP
->X_op
= O_symbol
;
540 expressionP
->X_add_symbol
= symbolP
;
544 /* either not seen or not defined. */
545 /* @@ Should print out the original string instead of
546 the parsed number. */
547 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
549 expressionP
->X_op
= O_constant
;
552 expressionP
->X_add_number
= 0;
554 else if (LOCAL_LABELS_FB
&& c
== 'f')
557 * forward reference. expect symbol to be undefined or
558 * unknown. undefined: seen it before. unknown: never seen
560 * construct a local label name, then an undefined symbol.
561 * don't create a xseg frag for it: caller may do that.
562 * just return it as never seen before.
564 name
= fb_label_name ((int) number
, 1);
565 symbolP
= symbol_find_or_make (name
);
566 /* we have no need to check symbol properties. */
567 #ifndef many_segments
568 /* since "know" puts its arg into a "string", we
569 can't have newlines in the argument. */
570 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
572 expressionP
->X_op
= O_symbol
;
573 expressionP
->X_add_symbol
= symbolP
;
574 expressionP
->X_add_number
= 0;
576 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
578 /* If the dollar label is *currently* defined, then this is just
579 another reference to it. If it is not *currently* defined,
580 then this is a fresh instantiation of that number, so create
583 if (dollar_label_defined ((long) number
))
585 name
= dollar_label_name ((long) number
, 0);
586 symbolP
= symbol_find (name
);
587 know (symbolP
!= NULL
);
591 name
= dollar_label_name ((long) number
, 1);
592 symbolP
= symbol_find_or_make (name
);
595 expressionP
->X_op
= O_symbol
;
596 expressionP
->X_add_symbol
= symbolP
;
597 expressionP
->X_add_number
= 0;
601 expressionP
->X_op
= O_constant
;
602 #ifdef TARGET_WORD_SIZE
603 /* Sign extend NUMBER. */
604 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
606 expressionP
->X_add_number
= number
;
607 input_line_pointer
--; /* restore following character. */
608 } /* really just a number */
612 /* not a small number */
613 expressionP
->X_op
= O_big
;
614 expressionP
->X_add_number
= number
; /* number of littlenums */
615 input_line_pointer
--; /*->char following number. */
619 /* Parse an MRI multi character constant. */
622 mri_char_constant (expressionP
)
623 expressionS
*expressionP
;
627 if (*input_line_pointer
== '\''
628 && input_line_pointer
[1] != '\'')
630 expressionP
->X_op
= O_constant
;
631 expressionP
->X_add_number
= 0;
635 /* In order to get the correct byte ordering, we must build the
636 number in reverse. */
637 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
641 generic_bignum
[i
] = 0;
642 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
644 if (*input_line_pointer
== '\'')
646 if (input_line_pointer
[1] != '\'')
648 ++input_line_pointer
;
650 generic_bignum
[i
] <<= 8;
651 generic_bignum
[i
] += *input_line_pointer
;
652 ++input_line_pointer
;
655 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
657 /* If there is more than one littlenum, left justify the
658 last one to make it match the earlier ones. If there is
659 only one, we can just use the value directly. */
660 for (; j
< CHARS_PER_LITTLENUM
; j
++)
661 generic_bignum
[i
] <<= 8;
664 if (*input_line_pointer
== '\''
665 && input_line_pointer
[1] != '\'')
671 as_bad ("Character constant too large");
680 c
= SIZE_OF_LARGE_NUMBER
- i
;
681 for (j
= 0; j
< c
; j
++)
682 generic_bignum
[j
] = generic_bignum
[i
+ j
];
686 know (LITTLENUM_NUMBER_OF_BITS
== 16);
689 expressionP
->X_op
= O_big
;
690 expressionP
->X_add_number
= i
;
694 expressionP
->X_op
= O_constant
;
696 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
698 expressionP
->X_add_number
=
699 (((generic_bignum
[1] & LITTLENUM_MASK
)
700 << LITTLENUM_NUMBER_OF_BITS
)
701 | (generic_bignum
[0] & LITTLENUM_MASK
));
704 /* Skip the final closing quote. */
705 ++input_line_pointer
;
708 /* Return an expression representing the current location. This
709 handles the magic symbol `.'. */
712 current_location (expressionp
)
713 expressionS
*expressionp
;
715 if (now_seg
== absolute_section
)
717 expressionp
->X_op
= O_constant
;
718 expressionp
->X_add_number
= abs_section_offset
;
724 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
725 (valueT
) frag_now_fix (),
727 expressionp
->X_op
= O_symbol
;
728 expressionp
->X_add_symbol
= symbolp
;
729 expressionp
->X_add_number
= 0;
734 * Summary of operand().
736 * in: Input_line_pointer points to 1st char of operand, which may
739 * out: A expressionS.
740 * The operand may have been empty: in this case X_op == O_absent.
741 * Input_line_pointer->(next non-blank) char after operand.
745 operand (expressionP
)
746 expressionS
*expressionP
;
749 symbolS
*symbolP
; /* points to symbol */
750 char *name
; /* points to name of symbol */
753 /* All integers are regarded as unsigned unless they are negated.
754 This is because the only thing which cares whether a number is
755 unsigned is the code in emit_expr which extends constants into
756 bignums. It should only sign extend negative numbers, so that
757 something like ``.quad 0x80000000'' is not sign extended even
758 though it appears negative if valueT is 32 bits. */
759 expressionP
->X_unsigned
= 1;
761 /* digits, assume it is a bignum. */
763 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
764 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
777 input_line_pointer
--;
779 integer_constant (flag_m68k_mri
? 0 : 10, expressionP
);
783 /* non-decimal radix */
789 /* Check for a hex constant. */
790 for (s
= input_line_pointer
; hex_p (*s
); s
++)
792 if (*s
== 'h' || *s
== 'H')
794 --input_line_pointer
;
795 integer_constant (0, expressionP
);
800 c
= *input_line_pointer
;
811 integer_constant (0, expressionP
);
817 if (c
&& strchr (FLT_CHARS
, c
))
819 input_line_pointer
++;
820 floating_constant (expressionP
);
821 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
825 /* The string was only zero */
826 expressionP
->X_op
= O_constant
;
827 expressionP
->X_add_number
= 0;
836 input_line_pointer
++;
837 integer_constant (16, expressionP
);
841 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
)
843 /* This code used to check for '+' and '-' here, and, in
844 some conditions, fall through to call
845 integer_constant. However, that didn't make sense,
846 as integer_constant only accepts digits. */
847 /* Some of our code elsewhere does permit digits greater
848 than the expected base; for consistency, do the same
850 if (input_line_pointer
[1] < '0'
851 || input_line_pointer
[1] > '9')
853 /* Parse this as a back reference to label 0. */
854 input_line_pointer
--;
855 integer_constant (10, expressionP
);
858 /* Otherwise, parse this as a binary number. */
862 input_line_pointer
++;
865 integer_constant (2, expressionP
);
876 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
882 /* If it says "0f" and it could possibly be a floating point
883 number, make it one. Otherwise, make it a local label,
884 and try to deal with parsing the rest later. */
885 if (!input_line_pointer
[1]
886 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
889 char *cp
= input_line_pointer
+ 1;
890 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
891 &generic_floating_point_number
);
895 case ERROR_EXPONENT_OVERFLOW
:
896 if (*cp
== 'f' || *cp
== 'b')
897 /* looks like a difference expression */
902 as_fatal ("expr.c(operand): bad atof_generic return val %d",
907 /* Okay, now we've sorted it out. We resume at one of these
908 two labels, depending on what we've decided we're probably
911 input_line_pointer
--;
912 integer_constant (10, expressionP
);
924 integer_constant (0, expressionP
);
934 input_line_pointer
++;
935 floating_constant (expressionP
);
936 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
940 if (LOCAL_LABELS_DOLLAR
)
942 integer_constant (10, expressionP
);
953 /* didn't begin with digit & not a name */
954 segment
= expression (expressionP
);
955 /* Expression() will pass trailing whitespace */
956 if ((c
== '(' && *input_line_pointer
++ != ')')
957 || (c
== '[' && *input_line_pointer
++ != ']'))
959 as_bad ("Missing ')' assumed");
960 input_line_pointer
--;
963 /* here with input_line_pointer->char after "(...)" */
967 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
969 as_bad ("EBCDIC constants are not supported");
972 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
974 ++input_line_pointer
;
979 /* Warning: to conform to other people's assemblers NO
980 ESCAPEMENT is permitted for a single quote. The next
981 character, parity errors and all, is taken as the value
982 of the operand. VERY KINKY. */
983 expressionP
->X_op
= O_constant
;
984 expressionP
->X_add_number
= *input_line_pointer
++;
988 mri_char_constant (expressionP
);
992 (void) operand (expressionP
);
996 /* Double quote is the bitwise not operator in MRI mode. */
1001 /* ~ is permitted to start a label on the Delta. */
1002 if (is_name_beginner (c
))
1007 operand (expressionP
);
1008 if (expressionP
->X_op
== O_constant
)
1010 /* input_line_pointer -> char after operand */
1013 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1014 /* Notice: '-' may overflow: no warning is given. This is
1015 compatible with other people's assemblers. Sigh. */
1016 expressionP
->X_unsigned
= 0;
1018 else if (c
== '~' || c
== '"')
1019 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1021 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1023 else if (expressionP
->X_op
!= O_illegal
1024 && expressionP
->X_op
!= O_absent
)
1026 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1028 expressionP
->X_op
= O_uminus
;
1029 else if (c
== '~' || c
== '"')
1030 expressionP
->X_op
= O_bit_not
;
1032 expressionP
->X_op
= O_logical_not
;
1033 expressionP
->X_add_number
= 0;
1036 as_warn ("Unary operator %c ignored because bad operand follows",
1042 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1045 if (! flag_m68k_mri
)
1048 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
1050 /* In MRI mode, $ is also used as the prefix for a
1051 hexadecimal constant. */
1052 integer_constant (16, expressionP
);
1056 if (is_part_of_name (*input_line_pointer
))
1059 current_location (expressionP
);
1063 if (!is_part_of_name (*input_line_pointer
))
1065 current_location (expressionP
);
1068 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1069 && ! is_part_of_name (input_line_pointer
[8]))
1070 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1071 && ! is_part_of_name (input_line_pointer
[7])))
1075 start
= (input_line_pointer
[1] == 't'
1076 || input_line_pointer
[1] == 'T');
1077 input_line_pointer
+= start
? 8 : 7;
1079 if (*input_line_pointer
!= '(')
1080 as_bad ("syntax error in .startof. or .sizeof.");
1085 ++input_line_pointer
;
1087 name
= input_line_pointer
;
1088 c
= get_symbol_end ();
1090 buf
= (char *) xmalloc (strlen (name
) + 10);
1092 sprintf (buf
, ".startof.%s", name
);
1094 sprintf (buf
, ".sizeof.%s", name
);
1095 symbolP
= symbol_make (buf
);
1098 expressionP
->X_op
= O_symbol
;
1099 expressionP
->X_add_symbol
= symbolP
;
1100 expressionP
->X_add_number
= 0;
1102 *input_line_pointer
= c
;
1104 if (*input_line_pointer
!= ')')
1105 as_bad ("syntax error in .startof. or .sizeof.");
1107 ++input_line_pointer
;
1119 /* can't imagine any other kind of operand */
1120 expressionP
->X_op
= O_absent
;
1121 input_line_pointer
--;
1125 if (! flag_m68k_mri
)
1127 integer_constant (2, expressionP
);
1131 if (! flag_m68k_mri
)
1133 integer_constant (8, expressionP
);
1137 if (! flag_m68k_mri
)
1140 /* In MRI mode, this is a floating point constant represented
1141 using hexadecimal digits. */
1143 ++input_line_pointer
;
1144 integer_constant (16, expressionP
);
1148 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1151 current_location (expressionP
);
1156 if (is_end_of_line
[(unsigned char) c
])
1158 if (is_name_beginner (c
)) /* here if did not begin with a digit */
1161 * Identifier begins here.
1162 * This is kludged for speed, so code is repeated.
1165 name
= --input_line_pointer
;
1166 c
= get_symbol_end ();
1168 #ifdef md_parse_name
1169 /* This is a hook for the backend to parse certain names
1170 specially in certain contexts. If a name always has a
1171 specific value, it can often be handled by simply
1172 entering it in the symbol table. */
1173 if (md_parse_name (name
, expressionP
))
1175 *input_line_pointer
= c
;
1181 /* The MRI i960 assembler permits
1183 FIXME: This should use md_parse_name. */
1185 && (strcasecmp (name
, "sizeof") == 0
1186 || strcasecmp (name
, "startof") == 0))
1191 start
= (name
[1] == 't'
1194 *input_line_pointer
= c
;
1197 name
= input_line_pointer
;
1198 c
= get_symbol_end ();
1200 buf
= (char *) xmalloc (strlen (name
) + 10);
1202 sprintf (buf
, ".startof.%s", name
);
1204 sprintf (buf
, ".sizeof.%s", name
);
1205 symbolP
= symbol_make (buf
);
1208 expressionP
->X_op
= O_symbol
;
1209 expressionP
->X_add_symbol
= symbolP
;
1210 expressionP
->X_add_number
= 0;
1212 *input_line_pointer
= c
;
1219 symbolP
= symbol_find_or_make (name
);
1221 /* If we have an absolute symbol or a reg, then we know its
1223 segment
= S_GET_SEGMENT (symbolP
);
1224 if (segment
== absolute_section
)
1226 expressionP
->X_op
= O_constant
;
1227 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1229 else if (segment
== reg_section
)
1231 expressionP
->X_op
= O_register
;
1232 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1236 expressionP
->X_op
= O_symbol
;
1237 expressionP
->X_add_symbol
= symbolP
;
1238 expressionP
->X_add_number
= 0;
1240 *input_line_pointer
= c
;
1244 /* Let the target try to parse it. Success is indicated by changing
1245 the X_op field to something other than O_absent and pointing
1246 input_line_pointer passed the expression. If it can't parse the
1247 expression, X_op and input_line_pointer should be unchanged. */
1248 expressionP
->X_op
= O_absent
;
1249 --input_line_pointer
;
1250 md_operand (expressionP
);
1251 if (expressionP
->X_op
== O_absent
)
1253 ++input_line_pointer
;
1254 as_bad ("Bad expression");
1255 expressionP
->X_op
= O_constant
;
1256 expressionP
->X_add_number
= 0;
1263 * It is more 'efficient' to clean up the expressionS when they are created.
1264 * Doing it here saves lines of code.
1266 clean_up_expression (expressionP
);
1267 SKIP_WHITESPACE (); /*->1st char after operand. */
1268 know (*input_line_pointer
!= ' ');
1270 /* The PA port needs this information. */
1271 if (expressionP
->X_add_symbol
)
1272 expressionP
->X_add_symbol
->sy_used
= 1;
1274 switch (expressionP
->X_op
)
1277 return absolute_section
;
1279 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1285 /* Internal. Simplify a struct expression for use by expr() */
1288 * In: address of a expressionS.
1289 * The X_op field of the expressionS may only take certain values.
1290 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1291 * Out: expressionS may have been modified:
1292 * 'foo-foo' symbol references cancelled to 0,
1293 * which changes X_op from O_subtract to O_constant.
1294 * Unused fields zeroed to help expr().
1298 clean_up_expression (expressionP
)
1299 expressionS
*expressionP
;
1301 switch (expressionP
->X_op
)
1305 expressionP
->X_add_number
= 0;
1310 expressionP
->X_add_symbol
= NULL
;
1315 expressionP
->X_op_symbol
= NULL
;
1318 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1319 || ((expressionP
->X_op_symbol
->sy_frag
1320 == expressionP
->X_add_symbol
->sy_frag
)
1321 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1322 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1323 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1325 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1326 - S_GET_VALUE (expressionP
->X_op_symbol
));
1328 expressionP
->X_op
= O_constant
;
1329 expressionP
->X_add_symbol
= NULL
;
1330 expressionP
->X_op_symbol
= NULL
;
1331 expressionP
->X_add_number
+= diff
;
1339 /* Expression parser. */
1342 * We allow an empty expression, and just assume (absolute,0) silently.
1343 * Unary operators and parenthetical expressions are treated as operands.
1344 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1346 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1347 * warped that I flushed it and wrote a recursive-descent parser instead.
1348 * Now things are stable, would anybody like to write a fast parser?
1349 * Most expressions are either register (which does not even reach here)
1350 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1351 * So I guess it doesn't really matter how inefficient more complex expressions
1354 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1355 * Also, we have consumed any leading or trailing spaces (operand does that)
1356 * and done all intervening operators.
1358 * This returns the segment of the result, which will be
1359 * absolute_section or the segment of a symbol.
1363 #define __ O_illegal
1365 static operatorT op_encoding
[256] =
1366 { /* maps ASCII->operators */
1368 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1369 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1371 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1372 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1373 __
, __
, __
, __
, __
, __
, __
, __
,
1374 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1375 __
, __
, __
, __
, __
, __
, __
, __
,
1376 __
, __
, __
, __
, __
, __
, __
, __
,
1377 __
, __
, __
, __
, __
, __
, __
, __
,
1378 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1379 __
, __
, __
, __
, __
, __
, __
, __
,
1380 __
, __
, __
, __
, __
, __
, __
, __
,
1381 __
, __
, __
, __
, __
, __
, __
, __
,
1382 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1384 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1385 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1386 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1387 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1388 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1389 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1390 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1391 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1397 * 0 operand, (expression)
1402 * 5 used for * / % in MRI mode
1407 static operator_rankT op_rank
[] =
1413 0, /* O_symbol_rva */
1418 8, /* O_logical_not */
1422 7, /* O_left_shift */
1423 7, /* O_right_shift */
1424 6, /* O_bit_inclusive_or */
1425 6, /* O_bit_or_not */
1426 6, /* O_bit_exclusive_or */
1436 2, /* O_logical_and */
1437 1 /* O_logical_or */
1440 /* Initialize the expression parser. */
1445 /* In MRI mode for the m68k, multiplication and division have lower
1446 precedence than the bit wise operators. */
1449 op_rank
[O_multiply
] = 5;
1450 op_rank
[O_divide
] = 5;
1451 op_rank
[O_modulus
] = 5;
1452 op_encoding
['"'] = O_bit_not
;
1455 /* Verify that X_op field is wide enough. */
1459 assert (e
.X_op
== O_max
);
1463 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1464 Advance INPUT_LINE_POINTER to the last character in the operator
1465 (i.e., don't change it for a single character operator). */
1467 static inline operatorT
1473 c
= *input_line_pointer
;
1478 return op_encoding
[c
];
1481 switch (input_line_pointer
[1])
1484 return op_encoding
[c
];
1495 ++input_line_pointer
;
1499 if (input_line_pointer
[1] != '=')
1500 return op_encoding
[c
];
1502 ++input_line_pointer
;
1506 switch (input_line_pointer
[1])
1509 return op_encoding
[c
];
1511 ret
= O_right_shift
;
1517 ++input_line_pointer
;
1521 /* We accept !! as equivalent to ^ for MRI compatibility. */
1522 if (input_line_pointer
[1] != '!')
1525 return O_bit_inclusive_or
;
1526 return op_encoding
[c
];
1528 ++input_line_pointer
;
1529 return O_bit_exclusive_or
;
1532 if (input_line_pointer
[1] != '|')
1533 return op_encoding
[c
];
1535 ++input_line_pointer
;
1536 return O_logical_or
;
1539 if (input_line_pointer
[1] != '&')
1540 return op_encoding
[c
];
1542 ++input_line_pointer
;
1543 return O_logical_and
;
1549 /* Parse an expression. */
1552 expr (rank
, resultP
)
1553 operator_rankT rank
; /* Larger # is higher rank. */
1554 expressionS
*resultP
; /* Deliver result here. */
1563 retval
= operand (resultP
);
1565 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1567 op_left
= operator ();
1568 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1572 input_line_pointer
++; /*->after 1st character of operator. */
1574 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1575 if (right
.X_op
== O_absent
)
1577 as_warn ("missing operand; zero assumed");
1578 right
.X_op
= O_constant
;
1579 right
.X_add_number
= 0;
1580 right
.X_add_symbol
= NULL
;
1581 right
.X_op_symbol
= NULL
;
1584 know (*input_line_pointer
!= ' ');
1586 if (retval
== undefined_section
)
1588 if (SEG_NORMAL (rightseg
))
1591 else if (! SEG_NORMAL (retval
))
1593 else if (SEG_NORMAL (rightseg
)
1594 && retval
!= rightseg
1596 && op_left
!= O_subtract
1599 as_bad ("operation combines symbols in different segments");
1601 op_right
= operator ();
1603 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1604 know ((int) op_left
>= (int) O_multiply
1605 && (int) op_left
<= (int) O_logical_or
);
1607 /* input_line_pointer->after right-hand quantity. */
1608 /* left-hand quantity in resultP */
1609 /* right-hand quantity in right. */
1610 /* operator in op_left. */
1612 if (resultP
->X_op
== O_big
)
1614 as_warn ("left operand is a %s; integer 0 assumed",
1615 resultP
->X_add_number
> 0 ? "bignum" : "float");
1616 resultP
->X_op
= O_constant
;
1617 resultP
->X_add_number
= 0;
1618 resultP
->X_add_symbol
= NULL
;
1619 resultP
->X_op_symbol
= NULL
;
1621 if (right
.X_op
== O_big
)
1623 as_warn ("right operand is a %s; integer 0 assumed",
1624 right
.X_add_number
> 0 ? "bignum" : "float");
1625 right
.X_op
= O_constant
;
1626 right
.X_add_number
= 0;
1627 right
.X_add_symbol
= NULL
;
1628 right
.X_op_symbol
= NULL
;
1631 /* Optimize common cases. */
1632 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1635 resultP
->X_add_number
+= right
.X_add_number
;
1637 /* This case comes up in PIC code. */
1638 else if (op_left
== O_subtract
1639 && right
.X_op
== O_symbol
1640 && resultP
->X_op
== O_symbol
1641 && (right
.X_add_symbol
->sy_frag
1642 == resultP
->X_add_symbol
->sy_frag
)
1643 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1646 resultP
->X_add_number
-= right
.X_add_number
;
1647 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1648 - S_GET_VALUE (right
.X_add_symbol
));
1649 resultP
->X_op
= O_constant
;
1650 resultP
->X_add_symbol
= 0;
1652 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1655 resultP
->X_add_number
-= right
.X_add_number
;
1657 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1660 resultP
->X_op
= right
.X_op
;
1661 resultP
->X_add_symbol
= right
.X_add_symbol
;
1662 resultP
->X_op_symbol
= right
.X_op_symbol
;
1663 resultP
->X_add_number
+= right
.X_add_number
;
1666 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1668 /* Constant OP constant. */
1669 offsetT v
= right
.X_add_number
;
1670 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1672 as_warn ("division by zero");
1678 case O_multiply
: resultP
->X_add_number
*= v
; break;
1679 case O_divide
: resultP
->X_add_number
/= v
; break;
1680 case O_modulus
: resultP
->X_add_number
%= v
; break;
1681 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1683 /* We always use unsigned shifts, to avoid relying on
1684 characteristics of the compiler used to compile gas. */
1685 resultP
->X_add_number
=
1686 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1688 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1689 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1690 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1691 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1692 case O_add
: resultP
->X_add_number
+= v
; break;
1693 case O_subtract
: resultP
->X_add_number
-= v
; break;
1695 resultP
->X_add_number
=
1696 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1699 resultP
->X_add_number
=
1700 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1703 resultP
->X_add_number
=
1704 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1707 resultP
->X_add_number
=
1708 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1711 resultP
->X_add_number
=
1712 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1715 resultP
->X_add_number
=
1716 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1719 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1722 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1726 else if (resultP
->X_op
== O_symbol
1727 && right
.X_op
== O_symbol
1728 && (op_left
== O_add
1729 || op_left
== O_subtract
1730 || (resultP
->X_add_number
== 0
1731 && right
.X_add_number
== 0)))
1733 /* Symbol OP symbol. */
1734 resultP
->X_op
= op_left
;
1735 resultP
->X_op_symbol
= right
.X_add_symbol
;
1736 if (op_left
== O_add
)
1737 resultP
->X_add_number
+= right
.X_add_number
;
1738 else if (op_left
== O_subtract
)
1739 resultP
->X_add_number
-= right
.X_add_number
;
1743 /* The general case. */
1744 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1745 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1746 resultP
->X_op
= op_left
;
1747 resultP
->X_add_number
= 0;
1748 resultP
->X_unsigned
= 1;
1752 } /* While next operator is >= this rank. */
1754 /* The PA port needs this information. */
1755 if (resultP
->X_add_symbol
)
1756 resultP
->X_add_symbol
->sy_used
= 1;
1758 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1764 * This lives here because it belongs equally in expr.c & read.c.
1765 * Expr.c is just a branch office read.c anyway, and putting it
1766 * here lessens the crowd at read.c.
1768 * Assume input_line_pointer is at start of symbol name.
1769 * Advance input_line_pointer past symbol name.
1770 * Turn that character into a '\0', returning its former value.
1771 * This allows a string compare (RMS wants symbol names to be strings)
1772 * of the symbol name.
1773 * There will always be a char following symbol name, because all good
1774 * lines end in end-of-line.
1781 /* We accept \001 in a name in case this is being called with a
1782 constructed string. */
1783 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1784 while (is_part_of_name (c
= *input_line_pointer
++)
1787 *--input_line_pointer
= 0;
1793 get_single_number ()
1797 return exp
.X_add_number
;