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
));
40 static segT operand
PARAMS ((expressionS
*));
41 static operatorT
operator PARAMS ((void));
43 extern const char EXP_CHARS
[], FLT_CHARS
[];
45 /* We keep a mapping of expression symbols to file positions, so that
46 we can provide better error messages. */
48 struct expr_symbol_line
50 struct expr_symbol_line
*next
;
56 static struct expr_symbol_line
*expr_symbol_lines
;
58 /* Build a dummy symbol to hold a complex expression. This is how we
59 build expressions up out of other expressions. The symbol is put
60 into the fake section expr_section. */
63 make_expr_symbol (expressionP
)
64 expressionS
*expressionP
;
68 struct expr_symbol_line
*n
;
70 if (expressionP
->X_op
== O_symbol
71 && expressionP
->X_add_number
== 0)
72 return expressionP
->X_add_symbol
;
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
, 1);
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 generic_bignum
[2] = 0;
302 generic_bignum
[3] = 0;
303 input_line_pointer
= start
; /*->1st digit. */
304 c
= *input_line_pointer
++;
306 (carry
= hex_value (c
)) < maxdig
;
307 c
= *input_line_pointer
++)
309 for (pointer
= generic_bignum
;
315 work
= carry
+ radix
* *pointer
;
316 *pointer
= work
& LITTLENUM_MASK
;
317 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
321 if (leader
< generic_bignum
+ SIZE_OF_LARGE_NUMBER
- 1)
323 /* room to grow a longer bignum. */
328 /* again, c is char after number, */
329 /* input_line_pointer->after c. */
330 know (LITTLENUM_NUMBER_OF_BITS
== 16);
331 if (leader
< generic_bignum
+ 2)
333 /* will fit into 32 bits. */
335 ((generic_bignum
[1] & LITTLENUM_MASK
) << LITTLENUM_NUMBER_OF_BITS
)
336 | (generic_bignum
[0] & LITTLENUM_MASK
);
341 else if (leader
< generic_bignum
+ 4)
343 /* Will fit into 64 bits. */
345 ((((((((valueT
) generic_bignum
[3] & LITTLENUM_MASK
)
346 << LITTLENUM_NUMBER_OF_BITS
)
347 | ((valueT
) generic_bignum
[2] & LITTLENUM_MASK
))
348 << LITTLENUM_NUMBER_OF_BITS
)
349 | ((valueT
) generic_bignum
[1] & LITTLENUM_MASK
))
350 << LITTLENUM_NUMBER_OF_BITS
)
351 | ((valueT
) generic_bignum
[0] & LITTLENUM_MASK
));
357 number
= leader
- generic_bignum
+ 1; /* number of littlenums in the bignum. */
361 if (flag_m68k_mri
&& suffix
!= NULL
&& input_line_pointer
- 1 == suffix
)
362 c
= *input_line_pointer
++;
367 * here with number, in correct radix. c is the next char.
368 * note that unlike un*x, we allow "011f" "0x9f" to
369 * both mean the same as the (conventional) "9f". this is simply easier
370 * than checking for strict canonical form. syntax sux!
373 if (LOCAL_LABELS_FB
&& c
== 'b')
376 * backward ref to local label.
377 * because it is backward, expect it to be defined.
379 /* Construct a local label. */
380 name
= fb_label_name ((int) number
, 0);
382 /* seen before, or symbol is defined: ok */
383 symbolP
= symbol_find (name
);
384 if ((symbolP
!= NULL
) && (S_IS_DEFINED (symbolP
)))
386 /* local labels are never absolute. don't waste time
387 checking absoluteness. */
388 know (SEG_NORMAL (S_GET_SEGMENT (symbolP
)));
390 expressionP
->X_op
= O_symbol
;
391 expressionP
->X_add_symbol
= symbolP
;
395 /* either not seen or not defined. */
396 /* @@ Should print out the original string instead of
397 the parsed number. */
398 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
400 expressionP
->X_op
= O_constant
;
403 expressionP
->X_add_number
= 0;
405 else if (LOCAL_LABELS_FB
&& c
== 'f')
408 * forward reference. expect symbol to be undefined or
409 * unknown. undefined: seen it before. unknown: never seen
411 * construct a local label name, then an undefined symbol.
412 * don't create a xseg frag for it: caller may do that.
413 * just return it as never seen before.
415 name
= fb_label_name ((int) number
, 1);
416 symbolP
= symbol_find_or_make (name
);
417 /* we have no need to check symbol properties. */
418 #ifndef many_segments
419 /* since "know" puts its arg into a "string", we
420 can't have newlines in the argument. */
421 know (S_GET_SEGMENT (symbolP
) == undefined_section
|| S_GET_SEGMENT (symbolP
) == text_section
|| S_GET_SEGMENT (symbolP
) == data_section
);
423 expressionP
->X_op
= O_symbol
;
424 expressionP
->X_add_symbol
= symbolP
;
425 expressionP
->X_add_number
= 0;
427 else if (LOCAL_LABELS_DOLLAR
&& c
== '$')
429 /* If the dollar label is *currently* defined, then this is just
430 another reference to it. If it is not *currently* defined,
431 then this is a fresh instantiation of that number, so create
434 if (dollar_label_defined ((long) number
))
436 name
= dollar_label_name ((long) number
, 0);
437 symbolP
= symbol_find (name
);
438 know (symbolP
!= NULL
);
442 name
= dollar_label_name ((long) number
, 1);
443 symbolP
= symbol_find_or_make (name
);
446 expressionP
->X_op
= O_symbol
;
447 expressionP
->X_add_symbol
= symbolP
;
448 expressionP
->X_add_number
= 0;
452 expressionP
->X_op
= O_constant
;
453 #ifdef TARGET_WORD_SIZE
454 /* Sign extend NUMBER. */
455 number
|= (-(number
>> (TARGET_WORD_SIZE
- 1))) << (TARGET_WORD_SIZE
- 1);
457 expressionP
->X_add_number
= number
;
458 input_line_pointer
--; /* restore following character. */
459 } /* really just a number */
463 /* not a small number */
464 expressionP
->X_op
= O_big
;
465 expressionP
->X_add_number
= number
; /* number of littlenums */
466 input_line_pointer
--; /*->char following number. */
470 /* Parse an MRI multi character constant. */
473 mri_char_constant (expressionP
)
474 expressionS
*expressionP
;
478 if (*input_line_pointer
== '\''
479 && input_line_pointer
[1] != '\'')
481 expressionP
->X_op
= O_constant
;
482 expressionP
->X_add_number
= 0;
486 /* In order to get the correct byte ordering, we must build the
487 number in reverse. */
488 for (i
= SIZE_OF_LARGE_NUMBER
- 1; i
>= 0; i
--)
492 generic_bignum
[i
] = 0;
493 for (j
= 0; j
< CHARS_PER_LITTLENUM
; j
++)
495 if (*input_line_pointer
== '\'')
497 if (input_line_pointer
[1] != '\'')
499 ++input_line_pointer
;
501 generic_bignum
[i
] <<= 8;
502 generic_bignum
[i
] += *input_line_pointer
;
503 ++input_line_pointer
;
506 if (i
< SIZE_OF_LARGE_NUMBER
- 1)
508 /* If there is more than one littlenum, left justify the
509 last one to make it match the earlier ones. If there is
510 only one, we can just use the value directly. */
511 for (; j
< CHARS_PER_LITTLENUM
; j
++)
512 generic_bignum
[i
] <<= 8;
515 if (*input_line_pointer
== '\''
516 && input_line_pointer
[1] != '\'')
522 as_bad ("Character constant too large");
531 c
= SIZE_OF_LARGE_NUMBER
- i
;
532 for (j
= 0; j
< c
; j
++)
533 generic_bignum
[j
] = generic_bignum
[i
+ j
];
537 know (LITTLENUM_NUMBER_OF_BITS
== 16);
540 expressionP
->X_op
= O_big
;
541 expressionP
->X_add_number
= i
;
545 expressionP
->X_op
= O_constant
;
547 expressionP
->X_add_number
= generic_bignum
[0] & LITTLENUM_MASK
;
549 expressionP
->X_add_number
=
550 (((generic_bignum
[1] & LITTLENUM_MASK
)
551 << LITTLENUM_NUMBER_OF_BITS
)
552 | (generic_bignum
[0] & LITTLENUM_MASK
));
555 /* Skip the final closing quote. */
556 ++input_line_pointer
;
559 /* Return an expression representing the current location. This
560 handles the magic symbol `.'. */
563 current_location (expressionp
)
564 expressionS
*expressionp
;
566 if (now_seg
== absolute_section
)
568 expressionp
->X_op
= O_constant
;
569 expressionp
->X_add_number
= abs_section_offset
;
575 symbolp
= symbol_new (FAKE_LABEL_NAME
, now_seg
,
576 (valueT
) frag_now_fix (),
578 expressionp
->X_op
= O_symbol
;
579 expressionp
->X_add_symbol
= symbolp
;
580 expressionp
->X_add_number
= 0;
585 * Summary of operand().
587 * in: Input_line_pointer points to 1st char of operand, which may
590 * out: A expressionS.
591 * The operand may have been empty: in this case X_op == O_absent.
592 * Input_line_pointer->(next non-blank) char after operand.
596 operand (expressionP
)
597 expressionS
*expressionP
;
600 symbolS
*symbolP
; /* points to symbol */
601 char *name
; /* points to name of symbol */
604 /* All integers are regarded as unsigned unless they are negated.
605 This is because the only thing which cares whether a number is
606 unsigned is the code in emit_expr which extends constants into
607 bignums. It should only sign extend negative numbers, so that
608 something like ``.quad 0x80000000'' is not sign extended even
609 though it appears negative if valueT is 32 bits. */
610 expressionP
->X_unsigned
= 1;
612 /* digits, assume it is a bignum. */
614 SKIP_WHITESPACE (); /* leading whitespace is part of operand. */
615 c
= *input_line_pointer
++; /* input_line_pointer->past char in c. */
628 input_line_pointer
--;
630 integer_constant (flag_m68k_mri
? 0 : 10, expressionP
);
634 /* non-decimal radix */
640 /* Check for a hex constant. */
641 for (s
= input_line_pointer
; hex_p (*s
); s
++)
643 if (*s
== 'h' || *s
== 'H')
645 --input_line_pointer
;
646 integer_constant (0, expressionP
);
651 c
= *input_line_pointer
;
662 integer_constant (0, expressionP
);
668 if (c
&& strchr (FLT_CHARS
, c
))
670 input_line_pointer
++;
671 floating_constant (expressionP
);
672 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
676 /* The string was only zero */
677 expressionP
->X_op
= O_constant
;
678 expressionP
->X_add_number
= 0;
687 input_line_pointer
++;
688 integer_constant (16, expressionP
);
692 if (LOCAL_LABELS_FB
&& ! flag_m68k_mri
)
694 /* This code used to check for '+' and '-' here, and, in
695 some conditions, fall through to call
696 integer_constant. However, that didn't make sense,
697 as integer_constant only accepts digits. */
698 /* Some of our code elsewhere does permit digits greater
699 than the expected base; for consistency, do the same
701 if (input_line_pointer
[1] < '0'
702 || input_line_pointer
[1] > '9')
704 /* Parse this as a back reference to label 0. */
705 input_line_pointer
--;
706 integer_constant (10, expressionP
);
709 /* Otherwise, parse this as a binary number. */
713 input_line_pointer
++;
716 integer_constant (2, expressionP
);
727 integer_constant (flag_m68k_mri
? 0 : 8, expressionP
);
733 /* If it says "0f" and it could possibly be a floating point
734 number, make it one. Otherwise, make it a local label,
735 and try to deal with parsing the rest later. */
736 if (!input_line_pointer
[1]
737 || (is_end_of_line
[0xff & input_line_pointer
[1]]))
740 char *cp
= input_line_pointer
+ 1;
741 int r
= atof_generic (&cp
, ".", EXP_CHARS
,
742 &generic_floating_point_number
);
746 case ERROR_EXPONENT_OVERFLOW
:
747 if (*cp
== 'f' || *cp
== 'b')
748 /* looks like a difference expression */
753 as_fatal ("expr.c(operand): bad atof_generic return val %d",
758 /* Okay, now we've sorted it out. We resume at one of these
759 two labels, depending on what we've decided we're probably
762 input_line_pointer
--;
763 integer_constant (10, expressionP
);
775 integer_constant (0, expressionP
);
785 input_line_pointer
++;
786 floating_constant (expressionP
);
787 expressionP
->X_add_number
= -(isupper (c
) ? tolower (c
) : c
);
791 if (LOCAL_LABELS_DOLLAR
)
793 integer_constant (10, expressionP
);
804 /* didn't begin with digit & not a name */
805 segment
= expression (expressionP
);
806 /* Expression() will pass trailing whitespace */
807 if ((c
== '(' && *input_line_pointer
++ != ')')
808 || (c
== '[' && *input_line_pointer
++ != ']'))
810 as_bad ("Missing ')' assumed");
811 input_line_pointer
--;
814 /* here with input_line_pointer->char after "(...)" */
818 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
820 as_bad ("EBCDIC constants are not supported");
823 if (! flag_m68k_mri
|| *input_line_pointer
!= '\'')
825 ++input_line_pointer
;
830 /* Warning: to conform to other people's assemblers NO
831 ESCAPEMENT is permitted for a single quote. The next
832 character, parity errors and all, is taken as the value
833 of the operand. VERY KINKY. */
834 expressionP
->X_op
= O_constant
;
835 expressionP
->X_add_number
= *input_line_pointer
++;
839 mri_char_constant (expressionP
);
843 (void) operand (expressionP
);
847 /* Double quote is the bitwise not operator in MRI mode. */
852 /* ~ is permitted to start a label on the Delta. */
853 if (is_name_beginner (c
))
858 operand (expressionP
);
859 if (expressionP
->X_op
== O_constant
)
861 /* input_line_pointer -> char after operand */
864 expressionP
->X_add_number
= - expressionP
->X_add_number
;
865 /* Notice: '-' may overflow: no warning is given. This is
866 compatible with other people's assemblers. Sigh. */
867 expressionP
->X_unsigned
= 0;
869 else if (c
== '~' || c
== '"')
870 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
872 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
874 else if (expressionP
->X_op
!= O_illegal
875 && expressionP
->X_op
!= O_absent
)
877 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
879 expressionP
->X_op
= O_uminus
;
880 else if (c
== '~' || c
== '"')
881 expressionP
->X_op
= O_bit_not
;
883 expressionP
->X_op
= O_logical_not
;
884 expressionP
->X_add_number
= 0;
887 as_warn ("Unary operator %c ignored because bad operand follows",
893 /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
899 if (flag_m68k_mri
&& hex_p (*input_line_pointer
))
901 /* In MRI mode, $ is also used as the prefix for a
902 hexadecimal constant. */
903 integer_constant (16, expressionP
);
907 if (is_part_of_name (*input_line_pointer
))
910 current_location (expressionP
);
914 if (!is_part_of_name (*input_line_pointer
))
916 current_location (expressionP
);
919 else if ((strncasecmp (input_line_pointer
, "startof.", 8) == 0
920 && ! is_part_of_name (input_line_pointer
[8]))
921 || (strncasecmp (input_line_pointer
, "sizeof.", 7) == 0
922 && ! is_part_of_name (input_line_pointer
[7])))
926 start
= (input_line_pointer
[1] == 't'
927 || input_line_pointer
[1] == 'T');
928 input_line_pointer
+= start
? 8 : 7;
930 if (*input_line_pointer
!= '(')
931 as_bad ("syntax error in .startof. or .sizeof.");
936 ++input_line_pointer
;
938 name
= input_line_pointer
;
939 c
= get_symbol_end ();
941 buf
= (char *) xmalloc (strlen (name
) + 10);
943 sprintf (buf
, ".startof.%s", name
);
945 sprintf (buf
, ".sizeof.%s", name
);
946 symbolP
= symbol_make (buf
);
949 expressionP
->X_op
= O_symbol
;
950 expressionP
->X_add_symbol
= symbolP
;
951 expressionP
->X_add_number
= 0;
953 *input_line_pointer
= c
;
955 if (*input_line_pointer
!= ')')
956 as_bad ("syntax error in .startof. or .sizeof.");
958 ++input_line_pointer
;
970 /* can't imagine any other kind of operand */
971 expressionP
->X_op
= O_absent
;
972 input_line_pointer
--;
978 integer_constant (2, expressionP
);
984 integer_constant (8, expressionP
);
991 /* In MRI mode, this is a floating point constant represented
992 using hexadecimal digits. */
994 ++input_line_pointer
;
995 integer_constant (16, expressionP
);
999 if (! flag_m68k_mri
|| is_part_of_name (*input_line_pointer
))
1002 current_location (expressionP
);
1007 if (is_end_of_line
[(unsigned char) c
])
1009 if (is_name_beginner (c
)) /* here if did not begin with a digit */
1012 * Identifier begins here.
1013 * This is kludged for speed, so code is repeated.
1016 name
= --input_line_pointer
;
1017 c
= get_symbol_end ();
1019 #ifdef md_parse_name
1020 /* This is a hook for the backend to parse certain names
1021 specially in certain contexts. If a name always has a
1022 specific value, it can often be handled by simply
1023 entering it in the symbol table. */
1024 if (md_parse_name (name
, expressionP
))
1026 *input_line_pointer
= c
;
1032 /* The MRI i960 assembler permits
1034 FIXME: This should use md_parse_name. */
1036 && (strcasecmp (name
, "sizeof") == 0
1037 || strcasecmp (name
, "startof") == 0))
1042 start
= (name
[1] == 't'
1045 *input_line_pointer
= c
;
1048 name
= input_line_pointer
;
1049 c
= get_symbol_end ();
1051 buf
= (char *) xmalloc (strlen (name
) + 10);
1053 sprintf (buf
, ".startof.%s", name
);
1055 sprintf (buf
, ".sizeof.%s", name
);
1056 symbolP
= symbol_make (buf
);
1059 expressionP
->X_op
= O_symbol
;
1060 expressionP
->X_add_symbol
= symbolP
;
1061 expressionP
->X_add_number
= 0;
1063 *input_line_pointer
= c
;
1070 symbolP
= symbol_find_or_make (name
);
1072 /* If we have an absolute symbol or a reg, then we know its
1074 segment
= S_GET_SEGMENT (symbolP
);
1075 if (segment
== absolute_section
)
1077 expressionP
->X_op
= O_constant
;
1078 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1080 else if (segment
== reg_section
)
1082 expressionP
->X_op
= O_register
;
1083 expressionP
->X_add_number
= S_GET_VALUE (symbolP
);
1087 expressionP
->X_op
= O_symbol
;
1088 expressionP
->X_add_symbol
= symbolP
;
1089 expressionP
->X_add_number
= 0;
1091 *input_line_pointer
= c
;
1095 /* Let the target try to parse it. Success is indicated by changing
1096 the X_op field to something other than O_absent and pointing
1097 input_line_pointer passed the expression. If it can't parse the
1098 expression, X_op and input_line_pointer should be unchanged. */
1099 expressionP
->X_op
= O_absent
;
1100 --input_line_pointer
;
1101 md_operand (expressionP
);
1102 if (expressionP
->X_op
== O_absent
)
1104 ++input_line_pointer
;
1105 as_bad ("Bad expression");
1106 expressionP
->X_op
= O_constant
;
1107 expressionP
->X_add_number
= 0;
1114 * It is more 'efficient' to clean up the expressionS when they are created.
1115 * Doing it here saves lines of code.
1117 clean_up_expression (expressionP
);
1118 SKIP_WHITESPACE (); /*->1st char after operand. */
1119 know (*input_line_pointer
!= ' ');
1121 /* The PA port needs this information. */
1122 if (expressionP
->X_add_symbol
)
1123 expressionP
->X_add_symbol
->sy_used
= 1;
1125 switch (expressionP
->X_op
)
1128 return absolute_section
;
1130 return S_GET_SEGMENT (expressionP
->X_add_symbol
);
1136 /* Internal. Simplify a struct expression for use by expr() */
1139 * In: address of a expressionS.
1140 * The X_op field of the expressionS may only take certain values.
1141 * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1142 * Out: expressionS may have been modified:
1143 * 'foo-foo' symbol references cancelled to 0,
1144 * which changes X_op from O_subtract to O_constant.
1145 * Unused fields zeroed to help expr().
1149 clean_up_expression (expressionP
)
1150 expressionS
*expressionP
;
1152 switch (expressionP
->X_op
)
1156 expressionP
->X_add_number
= 0;
1161 expressionP
->X_add_symbol
= NULL
;
1166 expressionP
->X_op_symbol
= NULL
;
1169 if (expressionP
->X_op_symbol
== expressionP
->X_add_symbol
1170 || ((expressionP
->X_op_symbol
->sy_frag
1171 == expressionP
->X_add_symbol
->sy_frag
)
1172 && SEG_NORMAL (S_GET_SEGMENT (expressionP
->X_add_symbol
))
1173 && (S_GET_VALUE (expressionP
->X_op_symbol
)
1174 == S_GET_VALUE (expressionP
->X_add_symbol
))))
1176 addressT diff
= (S_GET_VALUE (expressionP
->X_add_symbol
)
1177 - S_GET_VALUE (expressionP
->X_op_symbol
));
1179 expressionP
->X_op
= O_constant
;
1180 expressionP
->X_add_symbol
= NULL
;
1181 expressionP
->X_op_symbol
= NULL
;
1182 expressionP
->X_add_number
+= diff
;
1190 /* Expression parser. */
1193 * We allow an empty expression, and just assume (absolute,0) silently.
1194 * Unary operators and parenthetical expressions are treated as operands.
1195 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1197 * We used to do a aho/ullman shift-reduce parser, but the logic got so
1198 * warped that I flushed it and wrote a recursive-descent parser instead.
1199 * Now things are stable, would anybody like to write a fast parser?
1200 * Most expressions are either register (which does not even reach here)
1201 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1202 * So I guess it doesn't really matter how inefficient more complex expressions
1205 * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1206 * Also, we have consumed any leading or trailing spaces (operand does that)
1207 * and done all intervening operators.
1209 * This returns the segment of the result, which will be
1210 * absolute_section or the segment of a symbol.
1214 #define __ O_illegal
1216 static operatorT op_encoding
[256] =
1217 { /* maps ASCII->operators */
1219 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1220 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1222 __
, O_bit_or_not
, __
, __
, __
, O_modulus
, O_bit_and
, __
,
1223 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
1224 __
, __
, __
, __
, __
, __
, __
, __
,
1225 __
, __
, __
, __
, O_lt
, __
, O_gt
, __
,
1226 __
, __
, __
, __
, __
, __
, __
, __
,
1227 __
, __
, __
, __
, __
, __
, __
, __
,
1228 __
, __
, __
, __
, __
, __
, __
, __
,
1229 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
1230 __
, __
, __
, __
, __
, __
, __
, __
,
1231 __
, __
, __
, __
, __
, __
, __
, __
,
1232 __
, __
, __
, __
, __
, __
, __
, __
,
1233 __
, __
, __
, __
, O_bit_inclusive_or
, __
, __
, __
,
1235 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1236 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1237 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1238 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1239 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1240 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1241 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
1242 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
1248 * 0 operand, (expression)
1253 * 5 used for * / % in MRI mode
1258 static operator_rankT op_rank
[] =
1264 0, /* O_symbol_rva */
1269 8, /* O_logical_not */
1273 7, /* O_left_shift */
1274 7, /* O_right_shift */
1275 6, /* O_bit_inclusive_or */
1276 6, /* O_bit_or_not */
1277 6, /* O_bit_exclusive_or */
1287 2, /* O_logical_and */
1288 1 /* O_logical_or */
1291 /* Initialize the expression parser. */
1296 /* In MRI mode for the m68k, multiplication and division have lower
1297 precedence than the bit wise operators. */
1300 op_rank
[O_multiply
] = 5;
1301 op_rank
[O_divide
] = 5;
1302 op_rank
[O_modulus
] = 5;
1303 op_encoding
['"'] = O_bit_not
;
1306 /* Verify that X_op field is wide enough. */
1310 assert (e
.X_op
== O_max
);
1314 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1315 Advance INPUT_LINE_POINTER to the last character in the operator
1316 (i.e., don't change it for a single character operator). */
1318 static inline operatorT
1324 c
= *input_line_pointer
;
1329 return op_encoding
[c
];
1332 switch (input_line_pointer
[1])
1335 return op_encoding
[c
];
1346 ++input_line_pointer
;
1350 if (input_line_pointer
[1] != '=')
1351 return op_encoding
[c
];
1353 ++input_line_pointer
;
1357 switch (input_line_pointer
[1])
1360 return op_encoding
[c
];
1362 ret
= O_right_shift
;
1368 ++input_line_pointer
;
1372 /* We accept !! as equivalent to ^ for MRI compatibility. */
1373 if (input_line_pointer
[1] != '!')
1376 return O_bit_inclusive_or
;
1377 return op_encoding
[c
];
1379 ++input_line_pointer
;
1380 return O_bit_exclusive_or
;
1383 if (input_line_pointer
[1] != '|')
1384 return op_encoding
[c
];
1386 ++input_line_pointer
;
1387 return O_logical_or
;
1390 if (input_line_pointer
[1] != '&')
1391 return op_encoding
[c
];
1393 ++input_line_pointer
;
1394 return O_logical_and
;
1400 /* Parse an expression. */
1403 expr (rank
, resultP
)
1404 operator_rankT rank
; /* Larger # is higher rank. */
1405 expressionS
*resultP
; /* Deliver result here. */
1414 retval
= operand (resultP
);
1416 know (*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
1418 op_left
= operator ();
1419 while (op_left
!= O_illegal
&& op_rank
[(int) op_left
] > rank
)
1423 input_line_pointer
++; /*->after 1st character of operator. */
1425 rightseg
= expr (op_rank
[(int) op_left
], &right
);
1426 if (right
.X_op
== O_absent
)
1428 as_warn ("missing operand; zero assumed");
1429 right
.X_op
= O_constant
;
1430 right
.X_add_number
= 0;
1431 right
.X_add_symbol
= NULL
;
1432 right
.X_op_symbol
= NULL
;
1435 know (*input_line_pointer
!= ' ');
1437 if (retval
== undefined_section
)
1439 if (SEG_NORMAL (rightseg
))
1442 else if (! SEG_NORMAL (retval
))
1444 else if (SEG_NORMAL (rightseg
)
1445 && retval
!= rightseg
1447 && op_left
!= O_subtract
1450 as_bad ("operation combines symbols in different segments");
1452 op_right
= operator ();
1454 know (op_right
== O_illegal
|| op_rank
[(int) op_right
] <= op_rank
[(int) op_left
]);
1455 know ((int) op_left
>= (int) O_multiply
1456 && (int) op_left
<= (int) O_logical_or
);
1458 /* input_line_pointer->after right-hand quantity. */
1459 /* left-hand quantity in resultP */
1460 /* right-hand quantity in right. */
1461 /* operator in op_left. */
1463 if (resultP
->X_op
== O_big
)
1465 as_warn ("left operand is a %s; integer 0 assumed",
1466 resultP
->X_add_number
> 0 ? "bignum" : "float");
1467 resultP
->X_op
= O_constant
;
1468 resultP
->X_add_number
= 0;
1469 resultP
->X_add_symbol
= NULL
;
1470 resultP
->X_op_symbol
= NULL
;
1472 if (right
.X_op
== O_big
)
1474 as_warn ("right operand is a %s; integer 0 assumed",
1475 right
.X_add_number
> 0 ? "bignum" : "float");
1476 right
.X_op
= O_constant
;
1477 right
.X_add_number
= 0;
1478 right
.X_add_symbol
= NULL
;
1479 right
.X_op_symbol
= NULL
;
1482 /* Optimize common cases. */
1483 if (op_left
== O_add
&& right
.X_op
== O_constant
)
1486 resultP
->X_add_number
+= right
.X_add_number
;
1488 /* This case comes up in PIC code. */
1489 else if (op_left
== O_subtract
1490 && right
.X_op
== O_symbol
1491 && resultP
->X_op
== O_symbol
1492 && (right
.X_add_symbol
->sy_frag
1493 == resultP
->X_add_symbol
->sy_frag
)
1494 && SEG_NORMAL (S_GET_SEGMENT (right
.X_add_symbol
)))
1497 resultP
->X_add_number
-= right
.X_add_number
;
1498 resultP
->X_add_number
+= (S_GET_VALUE (resultP
->X_add_symbol
)
1499 - S_GET_VALUE (right
.X_add_symbol
));
1500 resultP
->X_op
= O_constant
;
1501 resultP
->X_add_symbol
= 0;
1503 else if (op_left
== O_subtract
&& right
.X_op
== O_constant
)
1506 resultP
->X_add_number
-= right
.X_add_number
;
1508 else if (op_left
== O_add
&& resultP
->X_op
== O_constant
)
1511 resultP
->X_op
= right
.X_op
;
1512 resultP
->X_add_symbol
= right
.X_add_symbol
;
1513 resultP
->X_op_symbol
= right
.X_op_symbol
;
1514 resultP
->X_add_number
+= right
.X_add_number
;
1517 else if (resultP
->X_op
== O_constant
&& right
.X_op
== O_constant
)
1519 /* Constant OP constant. */
1520 offsetT v
= right
.X_add_number
;
1521 if (v
== 0 && (op_left
== O_divide
|| op_left
== O_modulus
))
1523 as_warn ("division by zero");
1529 case O_multiply
: resultP
->X_add_number
*= v
; break;
1530 case O_divide
: resultP
->X_add_number
/= v
; break;
1531 case O_modulus
: resultP
->X_add_number
%= v
; break;
1532 case O_left_shift
: resultP
->X_add_number
<<= v
; break;
1534 /* We always use unsigned shifts, to avoid relying on
1535 characteristics of the compiler used to compile gas. */
1536 resultP
->X_add_number
=
1537 (offsetT
) ((valueT
) resultP
->X_add_number
>> (valueT
) v
);
1539 case O_bit_inclusive_or
: resultP
->X_add_number
|= v
; break;
1540 case O_bit_or_not
: resultP
->X_add_number
|= ~v
; break;
1541 case O_bit_exclusive_or
: resultP
->X_add_number
^= v
; break;
1542 case O_bit_and
: resultP
->X_add_number
&= v
; break;
1543 case O_add
: resultP
->X_add_number
+= v
; break;
1544 case O_subtract
: resultP
->X_add_number
-= v
; break;
1546 resultP
->X_add_number
=
1547 resultP
->X_add_number
== v
? ~ (offsetT
) 0 : 0;
1550 resultP
->X_add_number
=
1551 resultP
->X_add_number
!= v
? ~ (offsetT
) 0 : 0;
1554 resultP
->X_add_number
=
1555 resultP
->X_add_number
< v
? ~ (offsetT
) 0 : 0;
1558 resultP
->X_add_number
=
1559 resultP
->X_add_number
<= v
? ~ (offsetT
) 0 : 0;
1562 resultP
->X_add_number
=
1563 resultP
->X_add_number
>= v
? ~ (offsetT
) 0 : 0;
1566 resultP
->X_add_number
=
1567 resultP
->X_add_number
> v
? ~ (offsetT
) 0 : 0;
1570 resultP
->X_add_number
= resultP
->X_add_number
&& v
;
1573 resultP
->X_add_number
= resultP
->X_add_number
|| v
;
1577 else if (resultP
->X_op
== O_symbol
1578 && right
.X_op
== O_symbol
1579 && (op_left
== O_add
1580 || op_left
== O_subtract
1581 || (resultP
->X_add_number
== 0
1582 && right
.X_add_number
== 0)))
1584 /* Symbol OP symbol. */
1585 resultP
->X_op
= op_left
;
1586 resultP
->X_op_symbol
= right
.X_add_symbol
;
1587 if (op_left
== O_add
)
1588 resultP
->X_add_number
+= right
.X_add_number
;
1589 else if (op_left
== O_subtract
)
1590 resultP
->X_add_number
-= right
.X_add_number
;
1594 /* The general case. */
1595 resultP
->X_add_symbol
= make_expr_symbol (resultP
);
1596 resultP
->X_op_symbol
= make_expr_symbol (&right
);
1597 resultP
->X_op
= op_left
;
1598 resultP
->X_add_number
= 0;
1599 resultP
->X_unsigned
= 1;
1603 } /* While next operator is >= this rank. */
1605 /* The PA port needs this information. */
1606 if (resultP
->X_add_symbol
)
1607 resultP
->X_add_symbol
->sy_used
= 1;
1609 return resultP
->X_op
== O_constant
? absolute_section
: retval
;
1615 * This lives here because it belongs equally in expr.c & read.c.
1616 * Expr.c is just a branch office read.c anyway, and putting it
1617 * here lessens the crowd at read.c.
1619 * Assume input_line_pointer is at start of symbol name.
1620 * Advance input_line_pointer past symbol name.
1621 * Turn that character into a '\0', returning its former value.
1622 * This allows a string compare (RMS wants symbol names to be strings)
1623 * of the symbol name.
1624 * There will always be a char following symbol name, because all good
1625 * lines end in end-of-line.
1632 /* We accept \001 in a name in case this is being called with a
1633 constructed string. */
1634 if (is_name_beginner (c
= *input_line_pointer
++) || c
== '\001')
1635 while (is_part_of_name (c
= *input_line_pointer
++)
1638 *--input_line_pointer
= 0;
1644 get_single_number ()
1648 return exp
.X_add_number
;