1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 * This is really a branch office of as-read.c. I split it out to clearly
24 * distinguish the world of expressions from the world of statements.
25 * (It also gives smaller files to re-compile.)
26 * Here, "operand"s are of expressions, not instructions.
31 #define min(a, b) ((a) < (b) ? (a) : (b))
36 static void floating_constant
PARAMS ((expressionS
* expressionP
));
37 static void integer_constant
PARAMS ((int radix
, expressionS
* expressionP
));
38 static void mri_char_constant
PARAMS ((expressionS
*));
39 static void current_location
PARAMS ((expressionS
*));
40 static void clean_up_expression
PARAMS ((expressionS
* expressionP
));
41 static segT operand
PARAMS ((expressionS
*));
42 static operatorT
operator PARAMS ((void));
44 extern const char EXP_CHARS
[], FLT_CHARS
[];
46 /* We keep a mapping of expression symbols to file positions, so that
47 we can provide better error messages. */
49 struct expr_symbol_line
51 struct expr_symbol_line
*next
;
57 static struct expr_symbol_line
*expr_symbol_lines
;
59 /* Build a dummy symbol to hold a complex expression. This is how we
60 build expressions up out of other expressions. The symbol is put
61 into the fake section expr_section. */
64 make_expr_symbol (expressionP
)
65 expressionS
*expressionP
;
69 struct expr_symbol_line
*n
;
71 if (expressionP
->X_op
== O_symbol
72 && expressionP
->X_add_number
== 0)
73 return expressionP
->X_add_symbol
;
75 fake
= FAKE_LABEL_NAME
;
77 /* Putting constant symbols in absolute_section rather than
78 expr_section is convenient for the old a.out code, for which
79 S_GET_SEGMENT does not always retrieve the value put in by
81 symbolP
= symbol_create (fake
,
82 (expressionP
->X_op
== O_constant
85 0, &zero_address_frag
);
86 symbolP
->sy_value
= *expressionP
;
88 if (expressionP
->X_op
== O_constant
)
89 resolve_symbol_value (symbolP
, 1);
91 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
93 as_where (&n
->file
, &n
->line
);
94 n
->next
= expr_symbol_lines
;
95 expr_symbol_lines
= n
;
100 /* Return the file and line number for an expr symbol. Return
101 non-zero if something was found, 0 if no information is known for
105 expr_symbol_where (sym
, pfile
, pline
)
110 register struct expr_symbol_line
*l
;
112 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
125 /* Utilities for building expressions.
126 Since complex expressions are recorded as symbols for use in other
127 expressions these return a symbolS * and not an expressionS *.
128 These explicitly do not take an "add_number" argument. */
129 /* ??? For completeness' sake one might want expr_build_symbol.
130 It would just return its argument. */
132 /* Build an expression for an unsigned constant.
133 The corresponding one for signed constants is missing because
134 there's currently no need for it. One could add an unsigned_p flag
135 but that seems more clumsy. */
138 expr_build_uconstant (value
)
144 e
.X_add_number
= value
;
146 return make_expr_symbol (&e
);
149 /* Build an expression for OP s1. */
152 expr_build_unary (op
, s1
)
161 return make_expr_symbol (&e
);
164 /* Build an expression for s1 OP s2. */
167 expr_build_binary (op
, s1
, s2
)
178 return make_expr_symbol (&e
);
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
;
306 isalnum ((unsigned char) *suffix
);
309 if (*suffix
== 'e' || *suffix
== 'E')
313 if (suffix
== input_line_pointer
)
321 if (islower ((unsigned char) c
))
327 else if (c
== 'O' || c
== 'Q')
331 else if (suffix
[1] == '.' || c
== 'E' || flt
)
333 floating_constant (expressionP
);
348 too_many_digits
= valuesize
+ 1;
352 too_many_digits
= (valuesize
+ 2) / 3 + 1;
356 too_many_digits
= (valuesize
+ 3) / 4 + 1;
360 too_many_digits
= (valuesize
+ 12) / 4; /* very rough */
363 start
= input_line_pointer
;
364 c
= *input_line_pointer
++;
366 (digit
= hex_value (c
)) < maxdig
;
367 c
= *input_line_pointer
++)
369 number
= number
* radix
+ digit
;
371 /* c contains character after number. */
372 /* input_line_pointer->char after c. */
373 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
375 if (radix
== 16 && c
== '_')
377 /* This is literal of the form 0x333_0_12345678_1.
378 This example is equivalent to 0x00000333000000001234567800000001. */
380 int num_little_digits
= 0;
382 input_line_pointer
= start
; /*->1st digit. */
384 know (LITTLENUM_NUMBER_OF_BITS
== 16);
386 for (c
= '_'; c
== '_'; num_little_digits
+=2)
389 /* Convert one 64-bit word. */
392 for (c
= *input_line_pointer
++;
393 (digit
= hex_value (c
)) < maxdig
;
394 c
= *(input_line_pointer
++))
396 number
= number
* radix
+ digit
;
400 /* Check for 8 digit per word max. */
402 as_bad ("An bignum with underscores may not have more than 8 hex digits in any word.");
404 /* Add this chunk to the bignum. Shift things down 2 little digits.*/
405 know (LITTLENUM_NUMBER_OF_BITS
== 16);
406 for (i
= min (num_little_digits
+ 1, SIZE_OF_LARGE_NUMBER
- 1); i
>= 2; i
--)
407 generic_bignum
[i
] = generic_bignum
[i
-2];
409 /* Add the new digits as the least significant new ones. */
410 generic_bignum
[0] = number
& 0xffffffff;
411 generic_bignum
[1] = number
>> 16;
414 /* Again, c is char after number, input_line_pointer->after c. */
416 if (num_little_digits
> SIZE_OF_LARGE_NUMBER
- 1)
417 num_little_digits
= SIZE_OF_LARGE_NUMBER
- 1;
419 assert (num_little_digits
>= 4);
421 if (num_little_digits
!= 8)
422 as_bad ("A bignum with underscores must have exactly 4 words.");
424 /* We might have some leading zeros. These can be trimmed to give
425 * us a change to fit this constant into a small number.
427 while (generic_bignum
[num_little_digits
-1] == 0 && num_little_digits
> 1)
430 if (num_little_digits
<= 2)
432 /* will fit into 32 bits. */
433 number
= generic_bignum_to_int32 ();
437 else if (num_little_digits
<= 4)
439 /* Will fit into 64 bits. */
440 number
= generic_bignum_to_int64 ();
447 number
= num_little_digits
; /* number of littlenums in the bignum. */
453 * we saw a lot of digits. manufacture a bignum the hard way.
455 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
456 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
459 leader
= generic_bignum
;
460 generic_bignum
[0] = 0;
461 generic_bignum
[1] = 0;
462 generic_bignum
[2] = 0;
463 generic_bignum
[3] = 0;
464 input_line_pointer
= start
; /*->1st digit. */
465 c
= *input_line_pointer
++;
467 (carry
= hex_value (c
)) < maxdig
;
468 c
= *input_line_pointer
++)
470 for (pointer
= generic_bignum
;
476 work
= carry
+ radix
* *pointer
;
477 *pointer
= work
& LITTLENUM_MASK
;
478 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
482 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
484 /* room to grow a longer bignum. */
489 /* again, c is char after number, */
490 /* input_line_pointer->after c. */
491 know (LITTLENUM_NUMBER_OF_BITS
== 16);
492 if (leader
< generic_bignum
+ 2)
494 /* will fit into 32 bits. */
495 number
= generic_bignum_to_int32 ();
499 else if (leader
< generic_bignum
+ 4)
501 /* Will fit into 64 bits. */
502 number
= generic_bignum_to_int64 ();
508 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
512 if (flag_m68k_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
513 c
= *input_line_pointer
++;
518 * here with number, in correct radix. c is the next char.
519 * note that unlike un*x, we allow "011f" "0x9f" to
520 * both mean the same as the (conventional) "9f". this is simply easier
521 * than checking for strict canonical form. syntax sux!
524 if (LOCAL_LABELS_FB
&& c
== 'b')
527 * backward ref to local label.
528 * because it is backward, expect it to be defined.
530 /* Construct a local label. */
531 name
= fb_label_name ((int) number
, 0);
533 /* seen before, or symbol is defined: ok */
534 symbolP
= symbol_find (name
);
535 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
537 /* local labels are never absolute. don't waste time
538 checking absoluteness. */
539 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
541 expressionP
->X_op
= O_symbol
;
542 expressionP
->X_add_symbol
= symbolP
;
546 /* either not seen or not defined. */
547 /* @@ Should print out the original string instead of
548 the parsed number. */
549 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
551 expressionP
->X_op
= O_constant
;
554 expressionP
->X_add_number
= 0;
556 else if (LOCAL_LABELS_FB
&& c
== 'f')
559 * forward reference. expect symbol to be undefined or
560 * unknown. undefined: seen it before. unknown: never seen
562 * construct a local label name, then an undefined symbol.
563 * don't create a xseg frag for it: caller may do that.
564 * just return it as never seen before.
566 name
= fb_label_name ((int) number
, 1);
567 symbolP
= symbol_find_or_make (name
);
568 /* we have no need to check symbol properties. */
569 #ifndef many_segments
570 /* since "know" puts its arg into a "string", we
571 can't have newlines in the argument. */
572 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
574 expressionP
->X_op
= O_symbol
;
575 expressionP
->X_add_symbol
= symbolP
;
576 expressionP
->X_add_number
= 0;
578 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
580 /* If the dollar label is *currently* defined, then this is just
581 another reference to it. If it is not *currently* defined,
582 then this is a fresh instantiation of that number, so create
585 if (dollar_label_defined ((long) number
))
587 name
= dollar_label_name ((long) number
, 0);
588 symbolP
= symbol_find (name
);
589 know (symbolP
!= NULL
);
593 name
= dollar_label_name ((long) number
, 1);
594 symbolP
= symbol_find_or_make (name
);
597 expressionP
->X_op
= O_symbol
;
598 expressionP
->X_add_symbol
= symbolP
;
599 expressionP
->X_add_number
= 0;
603 expressionP
->X_op
= O_constant
;
604 #ifdef TARGET_WORD_SIZE
605 /* Sign extend NUMBER. */
606 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
608 expressionP
->X_add_number
= number
;
609 input_line_pointer
--; /* restore following character. */
610 } /* really just a number */
614 /* not a small number */
615 expressionP
->X_op
= O_big
;
616 expressionP
->X_add_number
= number
; /* number of littlenums */
617 input_line_pointer
--; /*->char following number. */
621 /* Parse an MRI multi character constant. */
624 mri_char_constant (expressionP
)
625 expressionS
*expressionP
;
629 if (*input_line_pointer
== '\''
630 && input_line_pointer
[1] != '\'')
632 expressionP
->X_op
= O_constant
;
633 expressionP
->X_add_number
= 0;
637 /* In order to get the correct byte ordering, we must build the
638 number in reverse. */
639 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
643 generic_bignum
[i
] = 0;
644 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
646 if (*input_line_pointer
== '\'')
648 if (input_line_pointer
[1] != '\'')
650 ++input_line_pointer
;
652 generic_bignum
[i
] <<= 8;
653 generic_bignum
[i
] += *input_line_pointer
;
654 ++input_line_pointer
;
657 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
659 /* If there is more than one littlenum, left justify the
660 last one to make it match the earlier ones. If there is
661 only one, we can just use the value directly. */
662 for (; j
< CHARS_PER_LITTLENUM
; j
++)
663 generic_bignum
[i
] <<= 8;
666 if (*input_line_pointer
== '\''
667 && input_line_pointer
[1] != '\'')
673 as_bad ("Character constant too large");
682 c
= SIZE_OF_LARGE_NUMBER
- i
;
683 for (j
= 0; j
< c
; j
++)
684 generic_bignum
[j
] = generic_bignum
[i
+ j
];
688 know (LITTLENUM_NUMBER_OF_BITS
== 16);
691 expressionP
->X_op
= O_big
;
692 expressionP
->X_add_number
= i
;
696 expressionP
->X_op
= O_constant
;
698 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
700 expressionP
->X_add_number
=
701 (((generic_bignum
[1] & LITTLENUM_MASK
)
702 << LITTLENUM_NUMBER_OF_BITS
)
703 | (generic_bignum
[0] & LITTLENUM_MASK
));
706 /* Skip the final closing quote. */
707 ++input_line_pointer
;
710 /* Return an expression representing the current location. This
711 handles the magic symbol `.'. */
714 current_location (expressionp
)
715 expressionS
*expressionp
;
717 if (now_seg
== absolute_section
)
719 expressionp
->X_op
= O_constant
;
720 expressionp
->X_add_number
= abs_section_offset
;
726 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
727 (valueT
) frag_now_fix (),
729 expressionp
->X_op
= O_symbol
;
730 expressionp
->X_add_symbol
= symbolp
;
731 expressionp
->X_add_number
= 0;
736 * Summary of operand().
738 * in: Input_line_pointer points to 1st char of operand, which may
741 * out: A expressionS.
742 * The operand may have been empty: in this case X_op == O_absent.
743 * Input_line_pointer->(next non-blank) char after operand.
747 operand (expressionP
)
748 expressionS
*expressionP
;
751 symbolS
*symbolP
; /* points to symbol */
752 char *name
; /* points to name of symbol */
755 /* All integers are regarded as unsigned unless they are negated.
756 This is because the only thing which cares whether a number is
757 unsigned is the code in emit_expr which extends constants into
758 bignums. It should only sign extend negative numbers, so that
759 something like ``.quad 0x80000000'' is not sign extended even
760 though it appears negative if valueT is 32 bits. */
761 expressionP
->X_unsigned
= 1;
763 /* digits, assume it is a bignum. */
765 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
766 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
779 input_line_pointer
--;
781 integer_constant (flag_m68k_mri
? 0 : 10, expressionP
);
785 /* non-decimal radix */
791 /* Check for a hex constant. */
792 for (s
= input_line_pointer
; hex_p (*s
); s
++)
794 if (*s
== 'h' || *s
== 'H')
796 --input_line_pointer
;
797 integer_constant (0, expressionP
);
802 c
= *input_line_pointer
;
813 integer_constant (0, expressionP
);
819 if (c
&& strchr (FLT_CHARS
, c
))
821 input_line_pointer
++;
822 floating_constant (expressionP
);
823 expressionP
->X_add_number
=
824 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
828 /* The string was only zero */
829 expressionP
->X_op
= O_constant
;
830 expressionP
->X_add_number
= 0;
839 input_line_pointer
++;
840 integer_constant (16, expressionP
);
844 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
)
846 /* This code used to check for '+' and '-' here, and, in
847 some conditions, fall through to call
848 integer_constant. However, that didn't make sense,
849 as integer_constant only accepts digits. */
850 /* Some of our code elsewhere does permit digits greater
851 than the expected base; for consistency, do the same
853 if (input_line_pointer
[1] < '0'
854 || input_line_pointer
[1] > '9')
856 /* Parse this as a back reference to label 0. */
857 input_line_pointer
--;
858 integer_constant (10, expressionP
);
861 /* Otherwise, parse this as a binary number. */
865 input_line_pointer
++;
868 integer_constant (2, expressionP
);
879 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
885 /* If it says "0f" and it could possibly be a floating point
886 number, make it one. Otherwise, make it a local label,
887 and try to deal with parsing the rest later. */
888 if (!input_line_pointer
[1]
889 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
892 char *cp
= input_line_pointer
+ 1;
893 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
894 &generic_floating_point_number
);
898 case ERROR_EXPONENT_OVERFLOW
:
899 if (*cp
== 'f' || *cp
== 'b')
900 /* looks like a difference expression */
905 as_fatal ("expr.c(operand): bad atof_generic return val %d",
910 /* Okay, now we've sorted it out. We resume at one of these
911 two labels, depending on what we've decided we're probably
914 input_line_pointer
--;
915 integer_constant (10, expressionP
);
927 integer_constant (0, expressionP
);
937 input_line_pointer
++;
938 floating_constant (expressionP
);
939 expressionP
->X_add_number
=
940 - (isupper ((unsigned char) c
) ? tolower (c
) : c
);
944 if (LOCAL_LABELS_DOLLAR
)
946 integer_constant (10, expressionP
);
957 /* didn't begin with digit & not a name */
958 segment
= expression (expressionP
);
959 /* Expression() will pass trailing whitespace */
960 if ((c
== '(' && *input_line_pointer
++ != ')')
961 || (c
== '[' && *input_line_pointer
++ != ']'))
963 as_bad ("Missing ')' assumed");
964 input_line_pointer
--;
967 /* here with input_line_pointer->char after "(...)" */
971 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
973 as_bad ("EBCDIC constants are not supported");
976 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
978 ++input_line_pointer
;
983 /* Warning: to conform to other people's assemblers NO
984 ESCAPEMENT is permitted for a single quote. The next
985 character, parity errors and all, is taken as the value
986 of the operand. VERY KINKY. */
987 expressionP
->X_op
= O_constant
;
988 expressionP
->X_add_number
= *input_line_pointer
++;
992 mri_char_constant (expressionP
);
996 (void) operand (expressionP
);
1000 /* Double quote is the bitwise not operator in MRI mode. */
1001 if (! flag_m68k_mri
)
1005 /* ~ is permitted to start a label on the Delta. */
1006 if (is_name_beginner (c
))
1011 operand (expressionP
);
1012 if (expressionP
->X_op
== O_constant
)
1014 /* input_line_pointer -> char after operand */
1017 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1018 /* Notice: '-' may overflow: no warning is given. This is
1019 compatible with other people's assemblers. Sigh. */
1020 expressionP
->X_unsigned
= 0;
1022 else if (c
== '~' || c
== '"')
1023 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1025 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1027 else if (expressionP
->X_op
!= O_illegal
1028 && expressionP
->X_op
!= O_absent
)
1030 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1032 expressionP
->X_op
= O_uminus
;
1033 else if (c
== '~' || c
== '"')
1034 expressionP
->X_op
= O_bit_not
;
1036 expressionP
->X_op
= O_logical_not
;
1037 expressionP
->X_add_number
= 0;
1040 as_warn ("Unary operator %c ignored because bad operand follows",
1046 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1049 if (! flag_m68k_mri
)
1052 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
1054 /* In MRI mode, $ is also used as the prefix for a
1055 hexadecimal constant. */
1056 integer_constant (16, expressionP
);
1060 if (is_part_of_name (*input_line_pointer
))
1063 current_location (expressionP
);
1067 if (!is_part_of_name (*input_line_pointer
))
1069 current_location (expressionP
);
1072 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
1073 && ! is_part_of_name (input_line_pointer
[8]))
1074 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
1075 && ! is_part_of_name (input_line_pointer
[7])))
1079 start
= (input_line_pointer
[1] == 't'
1080 || input_line_pointer
[1] == 'T');
1081 input_line_pointer
+= start
? 8 : 7;
1083 if (*input_line_pointer
!= '(')
1084 as_bad ("syntax error in .startof. or .sizeof.");
1089 ++input_line_pointer
;
1091 name
= input_line_pointer
;
1092 c
= get_symbol_end ();
1094 buf
= (char *) xmalloc (strlen (name
) + 10);
1096 sprintf (buf
, ".startof.%s", name
);
1098 sprintf (buf
, ".sizeof.%s", name
);
1099 symbolP
= symbol_make (buf
);
1102 expressionP
->X_op
= O_symbol
;
1103 expressionP
->X_add_symbol
= symbolP
;
1104 expressionP
->X_add_number
= 0;
1106 *input_line_pointer
= c
;
1108 if (*input_line_pointer
!= ')')
1109 as_bad ("syntax error in .startof. or .sizeof.");
1111 ++input_line_pointer
;
1123 /* can't imagine any other kind of operand */
1124 expressionP
->X_op
= O_absent
;
1125 input_line_pointer
--;
1129 if (! flag_m68k_mri
)
1131 integer_constant (2, expressionP
);
1135 if (! flag_m68k_mri
)
1137 integer_constant (8, expressionP
);
1141 if (! flag_m68k_mri
)
1144 /* In MRI mode, this is a floating point constant represented
1145 using hexadecimal digits. */
1147 ++input_line_pointer
;
1148 integer_constant (16, expressionP
);
1152 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1155 current_location (expressionP
);
1160 if (is_end_of_line
[(unsigned char) c
])
1162 if (is_name_beginner (c
)) /* here if did not begin with a digit */
1165 * Identifier begins here.
1166 * This is kludged for speed, so code is repeated.
1169 name
= --input_line_pointer
;
1170 c
= get_symbol_end ();
1172 #ifdef md_parse_name
1173 /* This is a hook for the backend to parse certain names
1174 specially in certain contexts. If a name always has a
1175 specific value, it can often be handled by simply
1176 entering it in the symbol table. */
1177 if (md_parse_name (name
, expressionP
))
1179 *input_line_pointer
= c
;
1185 /* The MRI i960 assembler permits
1187 FIXME: This should use md_parse_name. */
1189 && (strcasecmp (name
, "sizeof") == 0
1190 || strcasecmp (name
, "startof") == 0))
1195 start
= (name
[1] == 't'
1198 *input_line_pointer
= c
;
1201 name
= input_line_pointer
;
1202 c
= get_symbol_end ();
1204 buf
= (char *) xmalloc (strlen (name
) + 10);
1206 sprintf (buf
, ".startof.%s", name
);
1208 sprintf (buf
, ".sizeof.%s", name
);
1209 symbolP
= symbol_make (buf
);
1212 expressionP
->X_op
= O_symbol
;
1213 expressionP
->X_add_symbol
= symbolP
;
1214 expressionP
->X_add_number
= 0;
1216 *input_line_pointer
= c
;
1223 symbolP
= symbol_find_or_make (name
);
1225 /* If we have an absolute symbol or a reg, then we know its
1227 segment
= S_GET_SEGMENT (symbolP
);
1228 if (segment
== absolute_section
)
1230 expressionP
->X_op
= O_constant
;
1231 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1233 else if (segment
== reg_section
)
1235 expressionP
->X_op
= O_register
;
1236 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1240 expressionP
->X_op
= O_symbol
;
1241 expressionP
->X_add_symbol
= symbolP
;
1242 expressionP
->X_add_number
= 0;
1244 *input_line_pointer
= c
;
1248 /* Let the target try to parse it. Success is indicated by changing
1249 the X_op field to something other than O_absent and pointing
1250 input_line_pointer passed the expression. If it can't parse the
1251 expression, X_op and input_line_pointer should be unchanged. */
1252 expressionP
->X_op
= O_absent
;
1253 --input_line_pointer
;
1254 md_operand (expressionP
);
1255 if (expressionP
->X_op
== O_absent
)
1257 ++input_line_pointer
;
1258 as_bad ("Bad expression");
1259 expressionP
->X_op
= O_constant
;
1260 expressionP
->X_add_number
= 0;
1267 * It is more 'efficient' to clean up the expressionS when they are created.
1268 * Doing it here saves lines of code.
1270 clean_up_expression (expressionP
);
1271 SKIP_WHITESPACE (); /*->1st char after operand. */
1272 know (*input_line_pointer
!= ' ');
1274 /* The PA port needs this information. */
1275 if (expressionP
->X_add_symbol
)
1276 expressionP
->X_add_symbol
->sy_used
= 1;
1278 switch (expressionP
->X_op
)
1281 return absolute_section
;
1283 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1289 /* Internal. Simplify a struct expression for use by expr() */
1292 * In: address of a expressionS.
1293 * The X_op field of the expressionS may only take certain values.
1294 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1295 * Out: expressionS may have been modified:
1296 * 'foo-foo' symbol references cancelled to 0,
1297 * which changes X_op from O_subtract to O_constant.
1298 * Unused fields zeroed to help expr().
1302 clean_up_expression (expressionP
)
1303 expressionS
*expressionP
;
1305 switch (expressionP
->X_op
)
1309 expressionP
->X_add_number
= 0;
1314 expressionP
->X_add_symbol
= NULL
;
1319 expressionP
->X_op_symbol
= NULL
;
1322 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1323 || ((expressionP
->X_op_symbol
->sy_frag
1324 == expressionP
->X_add_symbol
->sy_frag
)
1325 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1326 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1327 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1329 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1330 - S_GET_VALUE (expressionP
->X_op_symbol
));
1332 expressionP
->X_op
= O_constant
;
1333 expressionP
->X_add_symbol
= NULL
;
1334 expressionP
->X_op_symbol
= NULL
;
1335 expressionP
->X_add_number
+= diff
;
1343 /* Expression parser. */
1346 * We allow an empty expression, and just assume (absolute,0) silently.
1347 * Unary operators and parenthetical expressions are treated as operands.
1348 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1350 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1351 * warped that I flushed it and wrote a recursive-descent parser instead.
1352 * Now things are stable, would anybody like to write a fast parser?
1353 * Most expressions are either register (which does not even reach here)
1354 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1355 * So I guess it doesn't really matter how inefficient more complex expressions
1358 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1359 * Also, we have consumed any leading or trailing spaces (operand does that)
1360 * and done all intervening operators.
1362 * This returns the segment of the result, which will be
1363 * absolute_section or the segment of a symbol.
1367 #define __ O_illegal
1369 static operatorT op_encoding
[256] =
1370 { /* maps ASCII->operators */
1372 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1373 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1375 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1376 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1377 __
, __
, __
, __
, __
, __
, __
, __
,
1378 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1379 __
, __
, __
, __
, __
, __
, __
, __
,
1380 __
, __
, __
, __
, __
, __
, __
, __
,
1381 __
, __
, __
, __
, __
, __
, __
, __
,
1382 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1383 __
, __
, __
, __
, __
, __
, __
, __
,
1384 __
, __
, __
, __
, __
, __
, __
, __
,
1385 __
, __
, __
, __
, __
, __
, __
, __
,
1386 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1388 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1389 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1390 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1391 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1392 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1393 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1394 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1395 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1401 * 0 operand, (expression)
1406 * 5 used for * / % in MRI mode
1411 static operator_rankT op_rank
[] =
1417 0, /* O_symbol_rva */
1422 8, /* O_logical_not */
1426 7, /* O_left_shift */
1427 7, /* O_right_shift */
1428 6, /* O_bit_inclusive_or */
1429 6, /* O_bit_or_not */
1430 6, /* O_bit_exclusive_or */
1440 2, /* O_logical_and */
1441 1 /* O_logical_or */
1444 /* Initialize the expression parser. */
1449 /* In MRI mode for the m68k, multiplication and division have lower
1450 precedence than the bit wise operators. */
1453 op_rank
[O_multiply
] = 5;
1454 op_rank
[O_divide
] = 5;
1455 op_rank
[O_modulus
] = 5;
1456 op_encoding
['"'] = O_bit_not
;
1459 /* Verify that X_op field is wide enough. */
1463 assert (e
.X_op
== O_max
);
1467 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1468 Advance INPUT_LINE_POINTER to the last character in the operator
1469 (i.e., don't change it for a single character operator). */
1471 static inline operatorT
1477 c
= *input_line_pointer
;
1482 return op_encoding
[c
];
1485 switch (input_line_pointer
[1])
1488 return op_encoding
[c
];
1499 ++input_line_pointer
;
1503 if (input_line_pointer
[1] != '=')
1504 return op_encoding
[c
];
1506 ++input_line_pointer
;
1510 switch (input_line_pointer
[1])
1513 return op_encoding
[c
];
1515 ret
= O_right_shift
;
1521 ++input_line_pointer
;
1525 /* We accept !! as equivalent to ^ for MRI compatibility. */
1526 if (input_line_pointer
[1] != '!')
1529 return O_bit_inclusive_or
;
1530 return op_encoding
[c
];
1532 ++input_line_pointer
;
1533 return O_bit_exclusive_or
;
1536 if (input_line_pointer
[1] != '|')
1537 return op_encoding
[c
];
1539 ++input_line_pointer
;
1540 return O_logical_or
;
1543 if (input_line_pointer
[1] != '&')
1544 return op_encoding
[c
];
1546 ++input_line_pointer
;
1547 return O_logical_and
;
1553 /* Parse an expression. */
1556 expr (rank
, resultP
)
1557 operator_rankT rank
; /* Larger # is higher rank. */
1558 expressionS
*resultP
; /* Deliver result here. */
1567 retval
= operand (resultP
);
1569 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1571 op_left
= operator ();
1572 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1576 input_line_pointer
++; /*->after 1st character of operator. */
1578 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1579 if (right
.X_op
== O_absent
)
1581 as_warn ("missing operand; zero assumed");
1582 right
.X_op
= O_constant
;
1583 right
.X_add_number
= 0;
1584 right
.X_add_symbol
= NULL
;
1585 right
.X_op_symbol
= NULL
;
1588 know (*input_line_pointer
!= ' ');
1590 if (retval
== undefined_section
)
1592 if (SEG_NORMAL (rightseg
))
1595 else if (! SEG_NORMAL (retval
))
1597 else if (SEG_NORMAL (rightseg
)
1598 && retval
!= rightseg
1600 && op_left
!= O_subtract
1603 as_bad ("operation combines symbols in different segments");
1605 op_right
= operator ();
1607 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1608 know ((int) op_left
>= (int) O_multiply
1609 && (int) op_left
<= (int) O_logical_or
);
1611 /* input_line_pointer->after right-hand quantity. */
1612 /* left-hand quantity in resultP */
1613 /* right-hand quantity in right. */
1614 /* operator in op_left. */
1616 if (resultP
->X_op
== O_big
)
1618 as_warn ("left operand is a %s; integer 0 assumed",
1619 resultP
->X_add_number
> 0 ? "bignum" : "float");
1620 resultP
->X_op
= O_constant
;
1621 resultP
->X_add_number
= 0;
1622 resultP
->X_add_symbol
= NULL
;
1623 resultP
->X_op_symbol
= NULL
;
1625 if (right
.X_op
== O_big
)
1627 as_warn ("right operand is a %s; integer 0 assumed",
1628 right
.X_add_number
> 0 ? "bignum" : "float");
1629 right
.X_op
= O_constant
;
1630 right
.X_add_number
= 0;
1631 right
.X_add_symbol
= NULL
;
1632 right
.X_op_symbol
= NULL
;
1635 /* Optimize common cases. */
1636 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1639 resultP
->X_add_number
+= right
.X_add_number
;
1641 /* This case comes up in PIC code. */
1642 else if (op_left
== O_subtract
1643 && right
.X_op
== O_symbol
1644 && resultP
->X_op
== O_symbol
1645 && (right
.X_add_symbol
->sy_frag
1646 == resultP
->X_add_symbol
->sy_frag
)
1647 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1650 resultP
->X_add_number
-= right
.X_add_number
;
1651 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1652 - S_GET_VALUE (right
.X_add_symbol
));
1653 resultP
->X_op
= O_constant
;
1654 resultP
->X_add_symbol
= 0;
1656 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1659 resultP
->X_add_number
-= right
.X_add_number
;
1661 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1664 resultP
->X_op
= right
.X_op
;
1665 resultP
->X_add_symbol
= right
.X_add_symbol
;
1666 resultP
->X_op_symbol
= right
.X_op_symbol
;
1667 resultP
->X_add_number
+= right
.X_add_number
;
1670 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1672 /* Constant OP constant. */
1673 offsetT v
= right
.X_add_number
;
1674 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1676 as_warn ("division by zero");
1682 case O_multiply
: resultP
->X_add_number
*= v
; break;
1683 case O_divide
: resultP
->X_add_number
/= v
; break;
1684 case O_modulus
: resultP
->X_add_number
%= v
; break;
1685 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1687 /* We always use unsigned shifts, to avoid relying on
1688 characteristics of the compiler used to compile gas. */
1689 resultP
->X_add_number
=
1690 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1692 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1693 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1694 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1695 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1696 case O_add
: resultP
->X_add_number
+= v
; break;
1697 case O_subtract
: resultP
->X_add_number
-= v
; break;
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
=
1720 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1723 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1726 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1730 else if (resultP
->X_op
== O_symbol
1731 && right
.X_op
== O_symbol
1732 && (op_left
== O_add
1733 || op_left
== O_subtract
1734 || (resultP
->X_add_number
== 0
1735 && right
.X_add_number
== 0)))
1737 /* Symbol OP symbol. */
1738 resultP
->X_op
= op_left
;
1739 resultP
->X_op_symbol
= right
.X_add_symbol
;
1740 if (op_left
== O_add
)
1741 resultP
->X_add_number
+= right
.X_add_number
;
1742 else if (op_left
== O_subtract
)
1743 resultP
->X_add_number
-= right
.X_add_number
;
1747 /* The general case. */
1748 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1749 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1750 resultP
->X_op
= op_left
;
1751 resultP
->X_add_number
= 0;
1752 resultP
->X_unsigned
= 1;
1756 } /* While next operator is >= this rank. */
1758 /* The PA port needs this information. */
1759 if (resultP
->X_add_symbol
)
1760 resultP
->X_add_symbol
->sy_used
= 1;
1762 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1768 * This lives here because it belongs equally in expr.c & read.c.
1769 * Expr.c is just a branch office read.c anyway, and putting it
1770 * here lessens the crowd at read.c.
1772 * Assume input_line_pointer is at start of symbol name.
1773 * Advance input_line_pointer past symbol name.
1774 * Turn that character into a '\0', returning its former value.
1775 * This allows a string compare (RMS wants symbol names to be strings)
1776 * of the symbol name.
1777 * There will always be a char following symbol name, because all good
1778 * lines end in end-of-line.
1785 /* We accept \001 in a name in case this is being called with a
1786 constructed string. */
1787 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1788 while (is_part_of_name (c
= *input_line_pointer
++)
1791 *--input_line_pointer
= 0;
1797 get_single_number ()
1801 return exp
.X_add_number
;