1 /* expr.c -operands, expressions-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
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.
35 static void floating_constant
PARAMS ((expressionS
* expressionP
));
36 static void integer_constant
PARAMS ((int radix
, expressionS
* expressionP
));
37 static void mri_char_constant
PARAMS ((expressionS
*));
38 static void current_location
PARAMS ((expressionS
*));
39 static void clean_up_expression
PARAMS ((expressionS
* expressionP
));
41 extern const char EXP_CHARS
[], FLT_CHARS
[];
43 /* We keep a mapping of expression symbols to file positions, so that
44 we can provide better error messages. */
46 struct expr_symbol_line
48 struct expr_symbol_line
*next
;
54 static struct expr_symbol_line
*expr_symbol_lines
;
56 /* Build a dummy symbol to hold a complex expression. This is how we
57 build expressions up out of other expressions. The symbol is put
58 into the fake section expr_section. */
61 make_expr_symbol (expressionP
)
62 expressionS
*expressionP
;
66 struct expr_symbol_line
*n
;
68 if (expressionP
->X_op
== O_symbol
69 && expressionP
->X_add_number
== 0)
70 return expressionP
->X_add_symbol
;
72 /* FIXME: This should be something which decode_local_label_name
74 fake
= FAKE_LABEL_NAME
;
76 /* Putting constant symbols in absolute_section rather than
77 expr_section is convenient for the old a.out code, for which
78 S_GET_SEGMENT does not always retrieve the value put in by
80 symbolP
= symbol_create (fake
,
81 (expressionP
->X_op
== O_constant
84 0, &zero_address_frag
);
85 symbolP
->sy_value
= *expressionP
;
87 if (expressionP
->X_op
== O_constant
)
88 resolve_symbol_value (symbolP
);
90 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
92 as_where (&n
->file
, &n
->line
);
93 n
->next
= expr_symbol_lines
;
94 expr_symbol_lines
= n
;
99 /* Return the file and line number for an expr symbol. Return
100 non-zero if something was found, 0 if no information is known for
104 expr_symbol_where (sym
, pfile
, pline
)
109 register struct expr_symbol_line
*l
;
111 for (l
= expr_symbol_lines
; l
!= NULL
; l
= l
->next
)
125 * Build any floating-point literal here.
126 * Also build any bignum literal here.
129 /* Seems atof_machine can backscan through generic_bignum and hit whatever
130 happens to be loaded before it in memory. And its way too complicated
131 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
132 and never write into the early words, thus they'll always be zero.
133 I hate Dean's floating-point code. Bleh. */
134 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6];
135 FLONUM_TYPE generic_floating_point_number
=
137 &generic_bignum
[6], /* low (JF: Was 0) */
138 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1], /* high JF: (added +6) */
143 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
144 int generic_floating_point_magic
;
147 floating_constant (expressionP
)
148 expressionS
*expressionP
;
150 /* input_line_pointer->*/
151 /* floating-point constant. */
154 error_code
= atof_generic (&input_line_pointer
, ".", EXP_CHARS
,
155 &generic_floating_point_number
);
159 if (error_code
== ERROR_EXPONENT_OVERFLOW
)
161 as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
165 as_bad ("bad floating-point constant: unknown error code=%d.", error_code
);
168 expressionP
->X_op
= O_big
;
169 /* input_line_pointer->just after constant, */
170 /* which may point to whitespace. */
171 expressionP
->X_add_number
= -1;
175 integer_constant (radix
, expressionP
)
177 expressionS
*expressionP
;
179 char *start
; /* start of number. */
182 valueT number
; /* offset or (absolute) value */
183 short int digit
; /* value of next digit in current radix */
184 short int maxdig
= 0;/* highest permitted digit value. */
185 int too_many_digits
= 0; /* if we see >= this number of */
186 char *name
; /* points to name of symbol */
187 symbolS
*symbolP
; /* points to symbol */
189 int small
; /* true if fits in 32 bits. */
191 /* May be bignum, or may fit in 32 bits. */
192 /* Most numbers fit into 32 bits, and we want this case to be fast.
193 so we pretend it will fit into 32 bits. If, after making up a 32
194 bit number, we realise that we have scanned more digits than
195 comfortably fit into 32 bits, we re-scan the digits coding them
196 into a bignum. For decimal and octal numbers we are
197 conservative: Some numbers may be assumed bignums when in fact
198 they do fit into 32 bits. Numbers of any radix can have excess
199 leading zeros: We strive to recognise this and cast them back
200 into 32 bits. We must check that the bignum really is more than
201 32 bits, and change it back to a 32-bit number if it fits. The
202 number we are looking for is expected to be positive, but if it
203 fits into 32 bits as an unsigned number, we let it be a 32-bit
204 number. The cavalier approach is for speed in ordinary cases. */
205 /* This has been extended for 64 bits. We blindly assume that if
206 you're compiling in 64-bit mode, the target is a 64-bit machine.
207 This should be cleaned up. */
211 #else /* includes non-bfd case, mostly */
215 if (flag_m68k_mri
&& radix
== 0)
219 /* In MRI mode, the number may have a suffix indicating the
220 radix. For that matter, it might actually be a floating
222 for (suffix
= input_line_pointer
; isalnum (*suffix
); suffix
++)
224 if (*suffix
== 'e' || *suffix
== 'E')
228 if (suffix
== input_line_pointer
)
242 else if (c
== 'O' || c
== 'Q')
246 else if (suffix
[1] == '.' || c
== 'E' || flt
)
248 floating_constant (expressionP
);
263 too_many_digits
= valuesize
+ 1;
267 too_many_digits
= (valuesize
+ 2) / 3 + 1;
271 too_many_digits
= (valuesize
+ 3) / 4 + 1;
275 too_many_digits
= (valuesize
+ 12) / 4; /* very rough */
278 start
= input_line_pointer
;
279 c
= *input_line_pointer
++;
281 (digit
= hex_value (c
)) < maxdig
;
282 c
= *input_line_pointer
++)
284 number
= number
* radix
+ digit
;
286 /* c contains character after number. */
287 /* input_line_pointer->char after c. */
288 small
= (input_line_pointer
- start
- 1) < too_many_digits
;
292 * we saw a lot of digits. manufacture a bignum the hard way.
294 LITTLENUM_TYPE
*leader
; /*->high order littlenum of the bignum. */
295 LITTLENUM_TYPE
*pointer
; /*->littlenum we are frobbing now. */
298 leader
= generic_bignum
;
299 generic_bignum
[0] = 0;
300 generic_bignum
[1] = 0;
301 input_line_pointer
= start
; /*->1st digit. */
302 c
= *input_line_pointer
++;
304 (carry
= hex_value (c
)) < maxdig
;
305 c
= *input_line_pointer
++)
307 for (pointer
= generic_bignum
;
313 work
= carry
+ radix
* *pointer
;
314 *pointer
= work
& LITTLENUM_MASK
;
315 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
319 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
321 /* room to grow a longer bignum. */
326 /* again, c is char after number, */
327 /* input_line_pointer->after c. */
328 know (LITTLENUM_NUMBER_OF_BITS
== 16);
329 if (leader
< generic_bignum
+ 2)
331 /* will fit into 32 bits. */
333 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
334 | (generic_bignum
[0] & LITTLENUM_MASK
);
339 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
343 if (flag_m68k_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
344 c
= *input_line_pointer
++;
349 * here with number, in correct radix. c is the next char.
350 * note that unlike un*x, we allow "011f" "0x9f" to
351 * both mean the same as the (conventional) "9f". this is simply easier
352 * than checking for strict canonical form. syntax sux!
355 if (LOCAL_LABELS_FB
&& c
== 'b')
358 * backward ref to local label.
359 * because it is backward, expect it to be defined.
361 /* Construct a local label. */
362 name
= fb_label_name ((int) number
, 0);
364 /* seen before, or symbol is defined: ok */
365 symbolP
= symbol_find (name
);
366 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
368 /* local labels are never absolute. don't waste time
369 checking absoluteness. */
370 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
372 expressionP
->X_op
= O_symbol
;
373 expressionP
->X_add_symbol
= symbolP
;
377 /* either not seen or not defined. */
378 /* @@ Should print out the original string instead of
379 the parsed number. */
380 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
382 expressionP
->X_op
= O_constant
;
385 expressionP
->X_add_number
= 0;
387 else if (LOCAL_LABELS_FB
&& c
== 'f')
390 * forward reference. expect symbol to be undefined or
391 * unknown. undefined: seen it before. unknown: never seen
393 * construct a local label name, then an undefined symbol.
394 * don't create a xseg frag for it: caller may do that.
395 * just return it as never seen before.
397 name
= fb_label_name ((int) number
, 1);
398 symbolP
= symbol_find_or_make (name
);
399 /* we have no need to check symbol properties. */
400 #ifndef many_segments
401 /* since "know" puts its arg into a "string", we
402 can't have newlines in the argument. */
403 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
405 expressionP
->X_op
= O_symbol
;
406 expressionP
->X_add_symbol
= symbolP
;
407 expressionP
->X_add_number
= 0;
409 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
411 /* If the dollar label is *currently* defined, then this is just
412 another reference to it. If it is not *currently* defined,
413 then this is a fresh instantiation of that number, so create
416 if (dollar_label_defined ((long) number
))
418 name
= dollar_label_name ((long) number
, 0);
419 symbolP
= symbol_find (name
);
420 know (symbolP
!= NULL
);
424 name
= dollar_label_name ((long) number
, 1);
425 symbolP
= symbol_find_or_make (name
);
428 expressionP
->X_op
= O_symbol
;
429 expressionP
->X_add_symbol
= symbolP
;
430 expressionP
->X_add_number
= 0;
434 expressionP
->X_op
= O_constant
;
435 #ifdef TARGET_WORD_SIZE
436 /* Sign extend NUMBER. */
437 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
439 expressionP
->X_add_number
= number
;
440 input_line_pointer
--; /* restore following character. */
441 } /* really just a number */
445 /* not a small number */
446 expressionP
->X_op
= O_big
;
447 expressionP
->X_add_number
= number
; /* number of littlenums */
448 input_line_pointer
--; /*->char following number. */
452 /* Parse an MRI multi character constant. */
455 mri_char_constant (expressionP
)
456 expressionS
*expressionP
;
460 if (*input_line_pointer
== '\''
461 && input_line_pointer
[1] != '\'')
463 expressionP
->X_op
= O_constant
;
464 expressionP
->X_add_number
= 0;
468 /* In order to get the correct byte ordering, we must build the
469 number in reverse. */
470 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
474 generic_bignum
[i
] = 0;
475 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
477 if (*input_line_pointer
== '\'')
479 if (input_line_pointer
[1] != '\'')
481 ++input_line_pointer
;
483 generic_bignum
[i
] <<= 8;
484 generic_bignum
[i
] += *input_line_pointer
;
485 ++input_line_pointer
;
488 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
490 /* If there is more than one littlenum, left justify the
491 last one to make it match the earlier ones. If there is
492 only one, we can just use the value directly. */
493 for (; j
< CHARS_PER_LITTLENUM
; j
++)
494 generic_bignum
[i
] <<= 8;
497 if (*input_line_pointer
== '\''
498 && input_line_pointer
[1] != '\'')
504 as_bad ("Character constant too large");
513 c
= SIZE_OF_LARGE_NUMBER
- i
;
514 for (j
= 0; j
< c
; j
++)
515 generic_bignum
[j
] = generic_bignum
[i
+ j
];
519 know (LITTLENUM_NUMBER_OF_BITS
== 16);
522 expressionP
->X_op
= O_big
;
523 expressionP
->X_add_number
= i
;
527 expressionP
->X_op
= O_constant
;
529 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
531 expressionP
->X_add_number
=
532 (((generic_bignum
[1] & LITTLENUM_MASK
)
533 << LITTLENUM_NUMBER_OF_BITS
)
534 | (generic_bignum
[0] & LITTLENUM_MASK
));
537 /* Skip the final closing quote. */
538 ++input_line_pointer
;
541 /* Return an expression representing the current location. This
542 handles the magic symbol `.'. */
545 current_location (expressionp
)
546 expressionS
*expressionp
;
548 if (now_seg
== absolute_section
)
550 expressionp
->X_op
= O_constant
;
551 expressionp
->X_add_number
= abs_section_offset
;
557 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
558 (valueT
) frag_now_fix (),
560 expressionp
->X_op
= O_symbol
;
561 expressionp
->X_add_symbol
= symbolp
;
562 expressionp
->X_add_number
= 0;
567 * Summary of operand().
569 * in: Input_line_pointer points to 1st char of operand, which may
572 * out: A expressionS.
573 * The operand may have been empty: in this case X_op == O_absent.
574 * Input_line_pointer->(next non-blank) char after operand.
578 operand (expressionP
)
579 expressionS
*expressionP
;
582 symbolS
*symbolP
; /* points to symbol */
583 char *name
; /* points to name of symbol */
586 /* All integers are regarded as unsigned unless they are negated.
587 This is because the only thing which cares whether a number is
588 unsigned is the code in emit_expr which extends constants into
589 bignums. It should only sign extend negative numbers, so that
590 something like ``.quad 0x80000000'' is not sign extended even
591 though it appears negative if valueT is 32 bits. */
592 expressionP
->X_unsigned
= 1;
594 /* digits, assume it is a bignum. */
596 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
597 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
610 input_line_pointer
--;
612 integer_constant (flag_m68k_mri
? 0 : 10, expressionP
);
616 /* non-decimal radix */
622 /* Check for a hex constant. */
623 for (s
= input_line_pointer
; hex_p (*s
); s
++)
625 if (*s
== 'h' || *s
== 'H')
627 --input_line_pointer
;
628 integer_constant (0, expressionP
);
633 c
= *input_line_pointer
;
644 integer_constant (0, expressionP
);
650 if (c
&& strchr (FLT_CHARS
, c
))
652 input_line_pointer
++;
653 floating_constant (expressionP
);
654 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
658 /* The string was only zero */
659 expressionP
->X_op
= O_constant
;
660 expressionP
->X_add_number
= 0;
669 input_line_pointer
++;
670 integer_constant (16, expressionP
);
674 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
)
676 /* This code used to check for '+' and '-' here, and, in
677 some conditions, fall through to call
678 integer_constant. However, that didn't make sense,
679 as integer_constant only accepts digits. */
680 /* Some of our code elsewhere does permit digits greater
681 than the expected base; for consistency, do the same
683 if (input_line_pointer
[1] < '0'
684 || input_line_pointer
[1] > '9')
686 /* Parse this as a back reference to label 0. */
687 input_line_pointer
--;
688 integer_constant (10, expressionP
);
691 /* Otherwise, parse this as a binary number. */
695 input_line_pointer
++;
698 integer_constant (2, expressionP
);
709 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
715 /* If it says "0f" and it could possibly be a floating point
716 number, make it one. Otherwise, make it a local label,
717 and try to deal with parsing the rest later. */
718 if (!input_line_pointer
[1]
719 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
722 char *cp
= input_line_pointer
+ 1;
723 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
724 &generic_floating_point_number
);
728 case ERROR_EXPONENT_OVERFLOW
:
729 if (*cp
== 'f' || *cp
== 'b')
730 /* looks like a difference expression */
735 as_fatal ("expr.c(operand): bad atof_generic return val %d",
740 /* Okay, now we've sorted it out. We resume at one of these
741 two labels, depending on what we've decided we're probably
744 input_line_pointer
--;
745 integer_constant (10, expressionP
);
757 integer_constant (0, expressionP
);
767 input_line_pointer
++;
768 floating_constant (expressionP
);
769 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
773 if (LOCAL_LABELS_DOLLAR
)
775 integer_constant (10, expressionP
);
786 /* didn't begin with digit & not a name */
787 segment
= expression (expressionP
);
788 /* Expression() will pass trailing whitespace */
789 if ((c
== '(' && *input_line_pointer
++ != ')')
790 || (c
== '[' && *input_line_pointer
++ != ']'))
792 as_bad ("Missing ')' assumed");
793 input_line_pointer
--;
796 /* here with input_line_pointer->char after "(...)" */
800 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
802 as_bad ("EBCDIC constants are not supported");
805 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
807 ++input_line_pointer
;
812 /* Warning: to conform to other people's assemblers NO
813 ESCAPEMENT is permitted for a single quote. The next
814 character, parity errors and all, is taken as the value
815 of the operand. VERY KINKY. */
816 expressionP
->X_op
= O_constant
;
817 expressionP
->X_add_number
= *input_line_pointer
++;
821 mri_char_constant (expressionP
);
825 (void) operand (expressionP
);
829 /* Double quote is the bitwise not operator in MRI mode. */
834 /* ~ is permitted to start a label on the Delta. */
835 if (is_name_beginner (c
))
840 operand (expressionP
);
841 if (expressionP
->X_op
== O_constant
)
843 /* input_line_pointer -> char after operand */
846 expressionP
->X_add_number
= - expressionP
->X_add_number
;
847 /* Notice: '-' may overflow: no warning is given. This is
848 compatible with other people's assemblers. Sigh. */
849 expressionP
->X_unsigned
= 0;
851 else if (c
== '~' || c
== '"')
852 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
854 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
856 else if (expressionP
->X_op
!= O_illegal
857 && expressionP
->X_op
!= O_absent
)
859 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
861 expressionP
->X_op
= O_uminus
;
862 else if (c
== '~' || c
== '"')
863 expressionP
->X_op
= O_bit_not
;
865 expressionP
->X_op
= O_logical_not
;
866 expressionP
->X_add_number
= 0;
869 as_warn ("Unary operator %c ignored because bad operand follows",
875 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
881 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
883 /* In MRI mode, $ is also used as the prefix for a
884 hexadecimal constant. */
885 integer_constant (16, expressionP
);
889 if (is_part_of_name (*input_line_pointer
))
892 current_location (expressionP
);
896 if (!is_part_of_name (*input_line_pointer
))
898 current_location (expressionP
);
901 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
902 && ! is_part_of_name (input_line_pointer
[8]))
903 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
904 && ! is_part_of_name (input_line_pointer
[7])))
908 start
= (input_line_pointer
[1] == 't'
909 || input_line_pointer
[1] == 'T');
910 input_line_pointer
+= start
? 8 : 7;
912 if (*input_line_pointer
!= '(')
913 as_bad ("syntax error in .startof. or .sizeof.");
918 ++input_line_pointer
;
920 name
= input_line_pointer
;
921 c
= get_symbol_end ();
923 buf
= (char *) xmalloc (strlen (name
) + 10);
925 sprintf (buf
, ".startof.%s", name
);
927 sprintf (buf
, ".sizeof.%s", name
);
928 symbolP
= symbol_make (buf
);
931 expressionP
->X_op
= O_symbol
;
932 expressionP
->X_add_symbol
= symbolP
;
933 expressionP
->X_add_number
= 0;
935 *input_line_pointer
= c
;
937 if (*input_line_pointer
!= ')')
938 as_bad ("syntax error in .startof. or .sizeof.");
940 ++input_line_pointer
;
952 /* can't imagine any other kind of operand */
953 expressionP
->X_op
= O_absent
;
954 input_line_pointer
--;
960 integer_constant (2, expressionP
);
966 integer_constant (8, expressionP
);
973 /* In MRI mode, this is a floating point constant represented
974 using hexadecimal digits. */
976 ++input_line_pointer
;
977 integer_constant (16, expressionP
);
981 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
984 current_location (expressionP
);
989 if (is_end_of_line
[(unsigned char) c
])
991 if (is_name_beginner (c
)) /* here if did not begin with a digit */
994 * Identifier begins here.
995 * This is kludged for speed, so code is repeated.
998 name
= --input_line_pointer
;
999 c
= get_symbol_end ();
1001 #ifdef md_parse_name
1002 /* This is a hook for the backend to parse certain names
1003 specially in certain contexts. If a name always has a
1004 specific value, it can often be handled by simply
1005 entering it in the symbol table. */
1006 if (md_parse_name (name
, expressionP
))
1008 *input_line_pointer
= c
;
1014 /* The MRI i960 assembler permits
1016 FIXME: This should use md_parse_name. */
1018 && (strcasecmp (name
, "sizeof") == 0
1019 || strcasecmp (name
, "startof") == 0))
1024 start
= (name
[1] == 't'
1027 *input_line_pointer
= c
;
1030 name
= input_line_pointer
;
1031 c
= get_symbol_end ();
1033 buf
= (char *) xmalloc (strlen (name
) + 10);
1035 sprintf (buf
, ".startof.%s", name
);
1037 sprintf (buf
, ".sizeof.%s", name
);
1038 symbolP
= symbol_make (buf
);
1041 expressionP
->X_op
= O_symbol
;
1042 expressionP
->X_add_symbol
= symbolP
;
1043 expressionP
->X_add_number
= 0;
1045 *input_line_pointer
= c
;
1052 symbolP
= symbol_find_or_make (name
);
1054 /* If we have an absolute symbol or a reg, then we know its
1056 segment
= S_GET_SEGMENT (symbolP
);
1057 if (segment
== absolute_section
)
1059 expressionP
->X_op
= O_constant
;
1060 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1062 else if (segment
== reg_section
)
1064 expressionP
->X_op
= O_register
;
1065 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1069 expressionP
->X_op
= O_symbol
;
1070 expressionP
->X_add_symbol
= symbolP
;
1071 expressionP
->X_add_number
= 0;
1073 *input_line_pointer
= c
;
1077 /* Let the target try to parse it. Success is indicated by changing
1078 the X_op field to something other than O_absent and pointing
1079 input_line_pointer passed the expression. If it can't parse the
1080 expression, X_op and input_line_pointer should be unchanged. */
1081 expressionP
->X_op
= O_absent
;
1082 --input_line_pointer
;
1083 md_operand (expressionP
);
1084 if (expressionP
->X_op
== O_absent
)
1086 ++input_line_pointer
;
1087 as_bad ("Bad expression");
1088 expressionP
->X_op
= O_constant
;
1089 expressionP
->X_add_number
= 0;
1096 * It is more 'efficient' to clean up the expressionS when they are created.
1097 * Doing it here saves lines of code.
1099 clean_up_expression (expressionP
);
1100 SKIP_WHITESPACE (); /*->1st char after operand. */
1101 know (*input_line_pointer
!= ' ');
1103 /* The PA port needs this information. */
1104 if (expressionP
->X_add_symbol
)
1105 expressionP
->X_add_symbol
->sy_used
= 1;
1107 switch (expressionP
->X_op
)
1110 return absolute_section
;
1112 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1118 /* Internal. Simplify a struct expression for use by expr() */
1121 * In: address of a expressionS.
1122 * The X_op field of the expressionS may only take certain values.
1123 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1124 * Out: expressionS may have been modified:
1125 * 'foo-foo' symbol references cancelled to 0,
1126 * which changes X_op from O_subtract to O_constant.
1127 * Unused fields zeroed to help expr().
1131 clean_up_expression (expressionP
)
1132 expressionS
*expressionP
;
1134 switch (expressionP
->X_op
)
1138 expressionP
->X_add_number
= 0;
1143 expressionP
->X_add_symbol
= NULL
;
1148 expressionP
->X_op_symbol
= NULL
;
1151 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1152 || ((expressionP
->X_op_symbol
->sy_frag
1153 == expressionP
->X_add_symbol
->sy_frag
)
1154 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1155 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1156 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1158 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1159 - S_GET_VALUE (expressionP
->X_op_symbol
));
1161 expressionP
->X_op
= O_constant
;
1162 expressionP
->X_add_symbol
= NULL
;
1163 expressionP
->X_op_symbol
= NULL
;
1164 expressionP
->X_add_number
+= diff
;
1172 /* Expression parser. */
1175 * We allow an empty expression, and just assume (absolute,0) silently.
1176 * Unary operators and parenthetical expressions are treated as operands.
1177 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1179 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1180 * warped that I flushed it and wrote a recursive-descent parser instead.
1181 * Now things are stable, would anybody like to write a fast parser?
1182 * Most expressions are either register (which does not even reach here)
1183 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1184 * So I guess it doesn't really matter how inefficient more complex expressions
1187 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1188 * Also, we have consumed any leading or trailing spaces (operand does that)
1189 * and done all intervening operators.
1191 * This returns the segment of the result, which will be
1192 * absolute_section or the segment of a symbol.
1196 #define __ O_illegal
1198 static operatorT op_encoding
[256] =
1199 { /* maps ASCII->operators */
1201 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1202 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1204 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1205 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1206 __
, __
, __
, __
, __
, __
, __
, __
,
1207 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1208 __
, __
, __
, __
, __
, __
, __
, __
,
1209 __
, __
, __
, __
, __
, __
, __
, __
,
1210 __
, __
, __
, __
, __
, __
, __
, __
,
1211 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1212 __
, __
, __
, __
, __
, __
, __
, __
,
1213 __
, __
, __
, __
, __
, __
, __
, __
,
1214 __
, __
, __
, __
, __
, __
, __
, __
,
1215 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1217 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1218 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1219 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1220 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1221 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1222 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1223 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1224 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1230 * 0 operand, (expression)
1235 * 5 used for * / % in MRI mode
1240 static operator_rankT op_rank
[] =
1246 0, /* O_symbol_rva */
1251 8, /* O_logical_not */
1255 7, /* O_left_shift */
1256 7, /* O_right_shift */
1257 6, /* O_bit_inclusive_or */
1258 6, /* O_bit_or_not */
1259 6, /* O_bit_exclusive_or */
1269 2, /* O_logical_and */
1270 1 /* O_logical_or */
1273 /* Initialize the expression parser. */
1278 /* In MRI mode for the m68k, multiplication and division have lower
1279 precedence than the bit wise operators. */
1282 op_rank
[O_multiply
] = 5;
1283 op_rank
[O_divide
] = 5;
1284 op_rank
[O_modulus
] = 5;
1285 op_encoding
['"'] = O_bit_not
;
1288 /* Verify that X_op field is wide enough. */
1292 assert (e
.X_op
== O_max
);
1296 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1297 Advance INPUT_LINE_POINTER to the last character in the operator
1298 (i.e., don't change it for a single character operator). */
1300 static inline operatorT
1306 c
= *input_line_pointer
;
1311 return op_encoding
[c
];
1314 switch (input_line_pointer
[1])
1317 return op_encoding
[c
];
1328 ++input_line_pointer
;
1332 switch (input_line_pointer
[1])
1335 return op_encoding
[c
];
1337 ret
= O_right_shift
;
1343 ++input_line_pointer
;
1347 /* We accept !! as equivalent to ^ for MRI compatibility. */
1348 if (input_line_pointer
[1] != '!')
1351 return O_bit_inclusive_or
;
1352 return op_encoding
[c
];
1354 ++input_line_pointer
;
1355 return O_bit_exclusive_or
;
1358 if (input_line_pointer
[1] != '|')
1359 return op_encoding
[c
];
1361 ++input_line_pointer
;
1362 return O_logical_or
;
1365 if (input_line_pointer
[1] != '&')
1366 return op_encoding
[c
];
1368 ++input_line_pointer
;
1369 return O_logical_and
;
1375 /* Parse an expression. */
1378 expr (rank
, resultP
)
1379 operator_rankT rank
; /* Larger # is higher rank. */
1380 expressionS
*resultP
; /* Deliver result here. */
1389 retval
= operand (resultP
);
1391 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1393 op_left
= operator ();
1394 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1398 input_line_pointer
++; /*->after 1st character of operator. */
1400 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1401 if (right
.X_op
== O_absent
)
1403 as_warn ("missing operand; zero assumed");
1404 right
.X_op
= O_constant
;
1405 right
.X_add_number
= 0;
1406 right
.X_add_symbol
= NULL
;
1407 right
.X_op_symbol
= NULL
;
1410 know (*input_line_pointer
!= ' ');
1412 if (retval
== undefined_section
)
1414 if (SEG_NORMAL (rightseg
))
1417 else if (! SEG_NORMAL (retval
))
1419 else if (SEG_NORMAL (rightseg
)
1420 && retval
!= rightseg
1422 && op_left
!= O_subtract
1425 as_bad ("operation combines symbols in different segments");
1427 op_right
= operator ();
1429 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1430 know ((int) op_left
>= (int) O_multiply
1431 && (int) op_left
<= (int) O_logical_or
);
1433 /* input_line_pointer->after right-hand quantity. */
1434 /* left-hand quantity in resultP */
1435 /* right-hand quantity in right. */
1436 /* operator in op_left. */
1438 if (resultP
->X_op
== O_big
)
1440 as_warn ("left operand is a %s; integer 0 assumed",
1441 resultP
->X_add_number
> 0 ? "bignum" : "float");
1442 resultP
->X_op
= O_constant
;
1443 resultP
->X_add_number
= 0;
1444 resultP
->X_add_symbol
= NULL
;
1445 resultP
->X_op_symbol
= NULL
;
1447 if (right
.X_op
== O_big
)
1449 as_warn ("right operand is a %s; integer 0 assumed",
1450 right
.X_add_number
> 0 ? "bignum" : "float");
1451 right
.X_op
= O_constant
;
1452 right
.X_add_number
= 0;
1453 right
.X_add_symbol
= NULL
;
1454 right
.X_op_symbol
= NULL
;
1457 /* Optimize common cases. */
1458 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1461 resultP
->X_add_number
+= right
.X_add_number
;
1463 /* This case comes up in PIC code. */
1464 else if (op_left
== O_subtract
1465 && right
.X_op
== O_symbol
1466 && resultP
->X_op
== O_symbol
1467 && (right
.X_add_symbol
->sy_frag
1468 == resultP
->X_add_symbol
->sy_frag
)
1469 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1472 resultP
->X_add_number
+= right
.X_add_number
;
1473 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1474 - S_GET_VALUE (right
.X_add_symbol
));
1475 resultP
->X_op
= O_constant
;
1476 resultP
->X_add_symbol
= 0;
1478 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1481 resultP
->X_add_number
-= right
.X_add_number
;
1483 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1486 resultP
->X_op
= right
.X_op
;
1487 resultP
->X_add_symbol
= right
.X_add_symbol
;
1488 resultP
->X_op_symbol
= right
.X_op_symbol
;
1489 resultP
->X_add_number
+= right
.X_add_number
;
1492 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1494 /* Constant OP constant. */
1495 offsetT v
= right
.X_add_number
;
1496 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1498 as_warn ("division by zero");
1504 case O_multiply
: resultP
->X_add_number
*= v
; break;
1505 case O_divide
: resultP
->X_add_number
/= v
; break;
1506 case O_modulus
: resultP
->X_add_number
%= v
; break;
1507 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1509 /* We always use unsigned shifts, to avoid relying on
1510 characteristics of the compiler used to compile gas. */
1511 resultP
->X_add_number
=
1512 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1514 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1515 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1516 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1517 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1518 case O_add
: resultP
->X_add_number
+= v
; break;
1519 case O_subtract
: resultP
->X_add_number
-= v
; break;
1521 resultP
->X_add_number
=
1522 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1525 resultP
->X_add_number
=
1526 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1529 resultP
->X_add_number
=
1530 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1533 resultP
->X_add_number
=
1534 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1537 resultP
->X_add_number
=
1538 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1541 resultP
->X_add_number
=
1542 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1545 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1548 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1552 else if (resultP
->X_op
== O_symbol
1553 && right
.X_op
== O_symbol
1554 && (op_left
== O_add
1555 || op_left
== O_subtract
1556 || (resultP
->X_add_number
== 0
1557 && right
.X_add_number
== 0)))
1559 /* Symbol OP symbol. */
1560 resultP
->X_op
= op_left
;
1561 resultP
->X_op_symbol
= right
.X_add_symbol
;
1562 if (op_left
== O_add
)
1563 resultP
->X_add_number
+= right
.X_add_number
;
1564 else if (op_left
== O_subtract
)
1565 resultP
->X_add_number
-= right
.X_add_number
;
1569 /* The general case. */
1570 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1571 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1572 resultP
->X_op
= op_left
;
1573 resultP
->X_add_number
= 0;
1574 resultP
->X_unsigned
= 1;
1578 } /* While next operator is >= this rank. */
1580 /* The PA port needs this information. */
1581 if (resultP
->X_add_symbol
)
1582 resultP
->X_add_symbol
->sy_used
= 1;
1584 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1590 * This lives here because it belongs equally in expr.c & read.c.
1591 * Expr.c is just a branch office read.c anyway, and putting it
1592 * here lessens the crowd at read.c.
1594 * Assume input_line_pointer is at start of symbol name.
1595 * Advance input_line_pointer past symbol name.
1596 * Turn that character into a '\0', returning its former value.
1597 * This allows a string compare (RMS wants symbol names to be strings)
1598 * of the symbol name.
1599 * There will always be a char following symbol name, because all good
1600 * lines end in end-of-line.
1607 /* We accept \001 in a name in case this is being called with a
1608 constructed string. */
1609 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1610 while (is_part_of_name (c
= *input_line_pointer
++)
1613 *--input_line_pointer
= 0;
1619 get_single_number ()
1623 return exp
.X_add_number
;