1 /* expr.c -operands, expressions-
2 Copyright (C) 1987 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 * This is really a branch office of as-read.c. I split it out to clearly
22 * distinguish the world of expressions from the world of statements.
23 * (It also gives smaller files to re-compile.)
24 * Here, "operand"s are of expressions, not instructions.
30 #include "stuff/round.h"
33 #include "struc-symbol.h"
38 #include "hex_value.h"
43 typedef char operator_rankT
;
46 * The type operatorT is for the types of operators in expressions.
49 O_illegal
, /* (0) what we get for illegal op */
51 O_multiply
, /* (1) * Ordered by rank*/
53 O_modulus
, /* (3) % */
56 O_subtract
, /* (5) - */
58 O_right_shift
, /* (6) >> */
59 O_left_shift
, /* (7) << */
61 O_less_than
, /* (8) < */
62 O_greater_than
, /* (9) > */
63 O_less_than_or_equal
, /* (10) <= */
64 O_greater_than_or_equal
, /* (11) >= */
66 O_equal
, /* (12) == */
67 O_not_equal
, /* (13) != */ /* or <> */
69 O_bit_and
, /* (14) & */
71 O_bit_exclusive_or
, /* (15) ^ */
73 O_bit_inclusive_or
, /* (16) | */
74 O_bit_or_not
, /* (17) ! */
75 O_logical_and
, /* (18) && */
76 O_logical_or
, /* (19) || */
77 two_char_operator
/* (20) encoding for two char operator */
82 expressionS
*resultP
);
85 expressionS
*expressionP
);
87 static void clean_up_expression(
88 expressionS
*expressionP
);
90 static segT
expr_part(
91 struct symbol
**symbol_1_PP
,
92 struct symbol
*symbol_2_P
);
94 static operatorT
two_char_op_encoding(
97 static void ignore_c_ll_or_ull(
100 /* Build a dummy symbol to hold a complex expression. This is how we
101 build expressions up out of other expressions. The symbol is put
102 into the fake section expr_section. */
105 make_expr_symbol (expressionS
*expressionP
)
112 struct expr_symbol_line
*n
;
115 if (expressionP
->X_op
== O_symbol
116 && expressionP
->X_add_number
== 0)
117 return expressionP
->X_add_symbol
;
122 if (expressionP
->X_op
== O_big
)
124 /* This won't work, because the actual value is stored in
125 generic_floating_point_number or generic_bignum, and we are
126 going to lose it if we haven't already. */
127 if (expressionP
->X_add_number
> 0)
128 as_bad (_("bignum invalid"));
130 as_bad (_("floating point number invalid"));
131 zero
.X_op
= O_constant
;
132 zero
.X_add_number
= 0;
134 clean_up_expression (&zero
);
138 /* Putting constant symbols in absolute_section rather than
139 expr_section is convenient for the old a.out code, for which
140 S_GET_SEGMENT does not always retrieve the value put in by
142 symbolP
= symbol_create (FAKE_LABEL_NAME
,
143 (expressionP
->X_op
== O_constant
146 0, &zero_address_frag
);
147 symbol_set_value_expression (symbolP
, expressionP
);
149 if (expressionP
->X_op
== O_constant
)
150 resolve_symbol_value (symbolP
);
152 n
= (struct expr_symbol_line
*) xmalloc (sizeof *n
);
154 as_where (&n
->file
, &n
->line
);
155 n
->next
= expr_symbol_lines
;
156 expr_symbol_lines
= n
;
162 /* Build an expression for an unsigned constant.
163 The corresponding one for signed constants is missing because
164 there's currently no need for it. One could add an unsigned_p flag
165 but that seems more clumsy. */
168 expr_build_uconstant (offsetT value
)
173 e
.X_add_number
= value
;
175 return make_expr_symbol (&e
);
188 static int seg_N_TYPE
[] = {
189 N_ABS
, /* absolute */
190 N_SECT
, /* section */
192 N_UNDF
, /* unknown */
194 -1 /* bignum/flonum */
200 /* N_UNDF == 0, N_ABS == 2 */
201 SEG_UNKNOWN
, -1, SEG_ABSOLUTE
, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
203 SEG_SECT
, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
207 * SEG_BIG expressions encode either a floating point number or an integer
208 * larger than 32 bits in this manner:
209 * For a floating point number:
210 * X_add_number is < 0
211 * The result is in the global variable generic_floating_point_number.
212 * The value in X_add_number is -'c' where c is the character that
213 * introduced the constant. e.g. "0f6.9" will have -'f' as a
214 * X_add_number value.
215 * For an integer larger than 32 bits:
217 * The result is in the global variable generic_bignum.
218 * The value in X_add_number is a count of how many littlenums it
219 * took to represent the bignum.
222 /* LITTLENUM_TYPE generic_buffer [6]; JF this is a hack */
223 /* Seems atof_machine can backscan through generic_bignum and hit whatever
224 happens to be loaded before it in memory. And its way too complicated
225 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
226 and never write into the early words, thus they'll always be zero.
227 I hate Dean's floating-point code. Bleh.
229 LITTLENUM_TYPE generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6] = { 0 };
231 FLONUM_TYPE generic_floating_point_number
= {
232 &generic_bignum
[6], /* low (JF: Was 0) */
233 &generic_bignum
[SIZE_OF_LARGE_NUMBER
+ 6 - 1],/* high JF: (added +6) */
240 * op_size is indexed by an operatorT and tells the size of the operator
241 * which is used to advance the input_line_pointer over the operator.
243 static int op_size
[] =
244 { 0, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2 };
247 * op_rank is indexed by an operatorT and tells the rank of the operator.
260 * 0 operand, (expression)
262 static operator_rankT op_rank
[] =
263 { 0,10,10,10, 9, 9, 8, 8, 7, 7, 7, 7, 6, 6, 5, 4, 3, 3, 2, 1 };
266 * op_encoding is indexed by a an ASCII character and maps it to an operator.
269 static const operatorT op_encoding
[256] = {
270 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
271 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
273 __
, two_char_operator
, __
, __
, __
, O_modulus
, two_char_operator
, __
,
274 __
, __
, O_multiply
, O_add
, __
, O_subtract
, __
, O_divide
,
275 __
, __
, __
, __
, __
, __
, __
, __
,
276 __
, __
, __
, __
, two_char_operator
, two_char_operator
, two_char_operator
, __
,
277 __
, __
, __
, __
, __
, __
, __
, __
,
278 __
, __
, __
, __
, __
, __
, __
, __
,
279 __
, __
, __
, __
, __
, __
, __
, __
,
280 __
, __
, __
, __
, __
, __
, O_bit_exclusive_or
, __
,
281 __
, __
, __
, __
, __
, __
, __
, __
,
282 __
, __
, __
, __
, __
, __
, __
, __
,
283 __
, __
, __
, __
, __
, __
, __
, __
,
284 __
, __
, __
, __
, two_char_operator
, __
, __
, __
,
286 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
287 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
288 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
289 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
290 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
291 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
292 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
,
293 __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
, __
296 segT
/* Return resultP -> X_seg */
298 expressionS
*resultP
) /* deliver result here */
302 segment
= expr(0, resultP
);
304 /* what about caller's that just want to ignore this and print the're own
305 error message? ok I guess */
306 if(segment
== SEG_DIFFSECT
&&
307 resultP
->X_add_symbol
== NULL
&&
308 (resultP
->X_subtract_symbol
->sy_type
& N_TYPE
) != N_UNDF
){
309 as_warn("Subtracting symbol \"%s\"(segment\"%s\") is too "
310 "hard. Absolute segment assumed.",
311 resultP
->X_subtract_symbol
->sy_name
,
312 seg_name
[(int)N_TYPE_seg
[
313 resultP
->X_subtract_symbol
->sy_type
& N_TYPE
]]);
314 segment
= SEG_ABSOLUTE
;
315 /* Leave exp .X_add_number alone. */
320 /* Expression parser. */
323 * We allow an empty expression, and just assume (absolute,0) silently.
324 * Unary operators and parenthetical expressions are treated as operands.
325 * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
327 * Most expressions are either register (which does not even reach here)
328 * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
330 * After expr(RANK,resultP) input_line_pointer -> operator of rank <= RANK.
331 * Also, we have consumed any leading or trailing spaces (operand does that)
332 * and done all intervening operators.
335 segT
/* Return resultP -> X_seg */
337 operator_rankT rank
, /* larger # is higher rank */
338 expressionS
*resultP
) /* deliver result here */
342 char c_left
; /* 1st operator character. */
348 (void)operand(resultP
);
349 know(*input_line_pointer
!= ' '); /* Operand() gobbles spaces. */
351 c_left
= *input_line_pointer
; /* Potential operator character. */
352 op_left
= (operatorT
)op_encoding
[(int)c_left
];
353 if(op_left
== two_char_operator
)
354 op_left
= two_char_op_encoding(c_left
);
356 while(op_left
!= O_illegal
&& op_rank
[op_left
] > rank
){
358 input_line_pointer
+= op_size
[op_left
];
359 /* -> after 1st character of operator. */
361 if(SEG_NONE
== expr(op_rank
[op_left
], &right
)){
362 as_warn("Missing operand value assumed absolute 0.");
363 resultP
->X_add_number
= 0;
364 resultP
->X_subtract_symbol
= NULL
;
365 resultP
->X_add_symbol
= NULL
;
366 resultP
->X_seg
= SEG_ABSOLUTE
;
368 know(*input_line_pointer
!= ' ');
370 c_right
= *input_line_pointer
;
371 op_right
= (operatorT
)op_encoding
[(int)c_right
];
372 if(op_right
== two_char_operator
)
373 op_right
= two_char_op_encoding(c_right
);
375 /* -> after 1st character of operator. */
376 know(op_right
== 0 || op_rank
[op_right
] <= op_rank
[op_left
]);
378 /* input_line_pointer -> after right-hand quantity. */
379 /* left-hand quantity in resultP */
380 /* right-hand quantity in right. */
381 /* operator in op_left. */
384 * Operations are not supported on bignums or floating-point
387 if(resultP
->X_seg
== SEG_BIG
){
388 as_warn("Left operand of %c is a %s integer 0 assumed",
389 c_left
, resultP
->X_add_number
> 0 ? "bignum" :
391 resultP
->X_seg
= SEG_ABSOLUTE
;
392 resultP
->X_add_symbol
= 0;
393 resultP
->X_subtract_symbol
= 0;
394 resultP
->X_add_number
= 0;
396 if(right
.X_seg
== SEG_BIG
){
397 as_warn("Right operand of %c is a %s integer 0 assumed",
398 c_left
, right
.X_add_number
> 0 ? "bignum" :
400 right
.X_seg
= SEG_ABSOLUTE
;
401 right
.X_add_symbol
= 0;
402 right
.X_subtract_symbol
= 0;
403 right
.X_add_number
= 0;
405 if(op_left
== O_subtract
){
407 * Convert - into + by exchanging symbols and negating
408 * number. I know -infinity can't be negated in 2's
409 * complement: but then it can't be subtracted either.
410 * This trick does not cause any further inaccuracy.
412 struct symbol
*symbolP
;
414 right
.X_add_number
= - right
.X_add_number
;
415 symbolP
= right
.X_add_symbol
;
416 right
.X_add_symbol
= right
.X_subtract_symbol
;
417 right
.X_subtract_symbol
= symbolP
;
419 /* This is not used, as it drops in to the next if */
420 right
.X_seg
= SEG_DIFFSECT
;
424 if(op_left
== O_add
){
428 /* not SEG_NONE and not SEG_BIG */
429 know(resultP
->X_seg
== SEG_SECT
||
430 resultP
->X_seg
== SEG_UNKNOWN
||
431 resultP
->X_seg
== SEG_DIFFSECT
||
432 resultP
->X_seg
== SEG_ABSOLUTE
);
433 /* not SEG_NONE and not SEG_BIG */
434 know(right
.X_seg
== SEG_SECT
||
435 right
.X_seg
== SEG_UNKNOWN
||
436 right
.X_seg
== SEG_DIFFSECT
||
437 right
.X_seg
== SEG_ABSOLUTE
);
439 clean_up_expression(&right
);
440 clean_up_expression(resultP
);
442 /* could this just return -1 instead of SEG_PASS1? and tested in the below if
444 seg1
= expr_part(&resultP
->X_add_symbol
,
446 seg2
= expr_part(&resultP
->X_subtract_symbol
,
447 right
.X_subtract_symbol
);
448 if((int)seg1
== -1 || (int)seg2
== -1){
449 as_warn("Can't relocate expression. Absolute 0 assumed.");
450 resultP
->X_seg
= SEG_ABSOLUTE
;
451 resultP
->X_add_number
= 0;
454 if(seg2
== SEG_ABSOLUTE
){
455 resultP
->X_seg
= seg1
;
458 /* also know seg2 != -1 (SEG_PASS1) */
459 know(seg2
!= SEG_ABSOLUTE
);
460 /* seg2 is for the subtract symbols, since seg2 != SEG_ABSOLUTE as would be
461 returned when there is no subtract symbols then expr_part() must have
462 combined a symbol into resultP->X_subtract_symbol that is either undefined
463 or defined in a section. */
464 know(resultP
->X_subtract_symbol
);
466 * If we are not to use the new incompatible features
467 * then "symbol1 - symbol2" must both be in the same
468 * section and will turn out as absolute.
471 if(seg1
!= SEG_UNKNOWN
&&
472 seg1
!= SEG_ABSOLUTE
&&
473 seg2
!= SEG_UNKNOWN
&&
475 resultP
->X_add_symbol
->sy_other
!=
476 resultP
->X_subtract_symbol
->sy_other
){
477 know(seg1
== SEG_SECT
);
478 know(seg2
== SEG_SECT
);
479 know(resultP
->X_add_symbol
);
480 know(resultP
->X_subtract_symbol
);
481 as_warn("Expression too complex: "
482 "forgetting %s - %s",
483 resultP
->X_add_symbol
->sy_name
,
484 resultP
->X_subtract_symbol
->sy_name
);
485 resultP
->X_seg
= SEG_ABSOLUTE
;
486 /* Clean_up_expression() will do the rest */
489 /* this can result in returning an expression that is NULL - symbol and the
490 caller must deal with this being illegal. maybe this should be put in
491 expression() routine (not a macro). Note the code in cons() */
492 resultP
->X_seg
= SEG_DIFFSECT
;
493 } /* If relocation too complex. */
496 resultP
->X_seg
= SEG_DIFFSECT
;
499 } /* If seg2 == SEG_ABSOLUTE. */
500 } /* If need pass 2. */
501 resultP
->X_add_number
+= right
.X_add_number
;
502 clean_up_expression(resultP
);
505 if(resultP
->X_seg
== SEG_UNKNOWN
||
506 right
.X_seg
== SEG_UNKNOWN
){
507 as_warn("Can't relocate expression. Absolute 0 assumed.");
508 resultP
->X_seg
= SEG_ABSOLUTE
;
509 resultP
->X_add_number
= 0;
513 * Will be SEG_ABSOLUTE. (or error)
515 try_to_make_absolute(resultP
);
516 try_to_make_absolute(&right
);
518 * If we have the special assembly time constant expression
519 * of the difference of two symbols defined in the same
520 * section then divided by exactly 2 mark the expression
523 if(resultP
->X_seg
== SEG_DIFFSECT
&&
524 right
.X_seg
== SEG_ABSOLUTE
&&
525 op_left
== O_divide
&&
526 right
.X_add_number
== 2){
527 resultP
->X_sectdiff_divide_by_two
= 1;
530 resultP
->X_subtract_symbol
= NULL
;
531 resultP
->X_add_symbol
= NULL
;
532 if(resultP
->X_seg
!= SEG_ABSOLUTE
||
533 right
.X_seg
!= SEG_ABSOLUTE
){
534 as_warn("Relocation error. Absolute 0 assumed");
535 resultP
->X_seg
= SEG_ABSOLUTE
;
536 resultP
->X_add_number
= 0;
540 * Both are absolute so perform the operation
544 case O_bit_inclusive_or
:
545 resultP
->X_add_number
|= right
.X_add_number
;
549 resultP
->X_add_number
=
550 resultP
->X_add_number
|| right
.X_add_number
;
554 if(right
.X_add_number
){
555 resultP
->X_add_number
%=
559 as_warn("Division by 0. 0 assumed.");
560 resultP
->X_add_number
= 0;
565 resultP
->X_add_number
&= right
.X_add_number
;
569 resultP
->X_add_number
=
570 resultP
->X_add_number
&& right
.X_add_number
;
574 resultP
->X_add_number
*= right
.X_add_number
;
578 if(right
.X_add_number
){
579 resultP
->X_add_number
/=
583 as_warn("Division by 0. 0 assumed.");
584 resultP
->X_add_number
= 0;
589 resultP
->X_add_number
<<=
594 resultP
->X_add_number
>>=
598 case O_bit_exclusive_or
:
599 resultP
->X_add_number
^= right
.X_add_number
;
603 resultP
->X_add_number
|=
608 resultP
->X_add_number
=
609 (resultP
->X_add_number
<
614 resultP
->X_add_number
=
615 (resultP
->X_add_number
>
619 case O_less_than_or_equal
:
620 resultP
->X_add_number
=
621 (resultP
->X_add_number
<=
625 case O_greater_than_or_equal
:
626 resultP
->X_add_number
=
627 (resultP
->X_add_number
>=
632 resultP
->X_add_number
=
633 (resultP
->X_add_number
==
638 resultP
->X_add_number
=
639 (resultP
->X_add_number
!=
646 } /* switch(op_left) */
649 } /* If we have to force need_pass_2 */
650 } /* If operator was + */
652 } /* While next operator is >= this rank */
653 return(resultP
->X_seg
);
657 * Summary of operand().
659 * in: Input_line_pointer points to 1st char of operand, which may
662 * out: A expressionS. X_seg determines how to understand the rest of the
664 * The operand may have been empty: in this case X_seg == SEG_NONE.
665 * Input_line_pointer -> (next non-blank) char after operand.
671 expressionS
*expressionP
)
674 char *name
; /* points to name of symbol */
675 struct symbol
*symbolP
; /* Points to symbol */
678 SKIP_WHITESPACE(); /* Leading whitespace is part of operand. */
679 c
= *input_line_pointer
++;/* Input_line_pointer -> past char in c. */
683 number
; /* offset or (absolute) value */
684 int digit
; /* value of next digit in current radix */
685 /* invented for humans only, hope */
686 /* optimising compiler flushes it! */
687 int radix
; /* 8, 10 or 16 */
688 /* 0 means we saw start of a floating- */
689 /* point constant. */
690 int maxdig
; /* Highest permitted digit value. */
691 int too_many_digits
;/* If we see >= this number of */
692 /* digits, assume it is a bignum. */
693 char *digit_2
; /* -> 2nd digit of number. */
694 int small
; /* TRUE if fits in 32 bits. */
695 int force_bignum
; /* TRUE if number is 0xb... */
697 force_bignum
= FALSE
;
699 * These two initiaizations are to shut up compiler warning as the
700 * may be used with out being set. There used only if radix != 0
701 * when the number is not a floating-point number.
706 if(c
== '0'){ /* non-decimal radix */
707 c
= *input_line_pointer
++;
708 if(c
== 'x' || c
=='X'){
709 c
= *input_line_pointer
++; /* read past "0x" or "0X" */
715 * If we have "0b" and some hex digits then treat it as a hex
716 * number and return a bignum. This is for hex immediate
717 * bit-patterns for floating-point immediate constants.
719 else if((c
== 'b' || c
== 'B') &&
720 (*input_line_pointer
!= '\0') &&
721 strchr("0123456789abcdefABCDEF",
722 *input_line_pointer
) != NULL
){
724 c
= *input_line_pointer
++; /* read past "0b" or "0B" */
731 * If it says '0f' and the line ends or it DOESN'T look like
732 * a floating point #, its a local label ref.
735 (*input_line_pointer
== '\0' ||
736 (strchr("+-.0123456789", *input_line_pointer
) == NULL
&&
737 strchr(md_EXP_CHARS
, *input_line_pointer
) == NULL
) )){
740 too_many_digits
= 11;
742 input_line_pointer
-= 2;
744 else if(c
!= '\0' && strchr(md_FLT_CHARS
, c
) != NULL
){
745 radix
= 0;/* Start of floating-point constant. */
746 /* input_line_pointer -> 1st char of number */
747 expressionP
->X_add_number
=
748 - (isupper(c
) ? tolower(c
) : c
);
750 else{ /* By elimination, assume octal radix. */
752 maxdig
= 10; /* Un*x sux. Compatibility. */
753 too_many_digits
= 11;
756 /* c == char after "0" or "0x" or "0X" or "0e" etc.*/
761 too_many_digits
= 11;
765 * Expressions are now evaluated as 64-bit values so the number
766 * digits allowed is twice that for 32-bit expressions.
768 too_many_digits
*= 2;
770 if(radix
!= 0){ /* Fixed-point integer constant. */
771 /* May be bignum, or may fit in 32 bits. */
773 * Most numbers fit into 32 bits, and we want this case to be
774 * fast. So we pretend it will fit into 32 bits. If, after
775 * making up a 32 bit number, we realize that we have scanned
776 * more digits than comfortably fit into 32 bits, we re-scan the
777 * digits coding them into a bignum. For decimal and octal
778 * numbers we are conservative: some numbers may be assumed
779 * bignums when in fact they do fit into 32 bits. Numbers of
780 * any radix can have excess leading zeros: we strive to
781 * recognise this and cast them back into 32 bits. We must
782 * check that the bignum really is more than 32 bits, and
783 * change it back to a 32-bit number if it fits. The number we
784 * are looking for is expected to be positive, but if it fits
785 * into 32 bits as an unsigned number, we let it be a 32-bit
786 * number. The cavalier approach is for speed in ordinary cases.
788 digit_2
= input_line_pointer
;
790 (digit
= hex_value
[(int)c
]) < maxdig
;
791 c
= *input_line_pointer
++){
792 number
= number
* radix
+ digit
;
794 /* c contains character after number. */
795 /* Input_line_pointer -> char after c. */
796 small
= input_line_pointer
- digit_2
< too_many_digits
;
797 if(force_bignum
== TRUE
)
801 * Manufacture a bignum.
803 /* -> high order littlenum of the bignum. */
804 LITTLENUM_TYPE
*leader
;
805 /* -> littlenum we are frobbing now. */
806 LITTLENUM_TYPE
*pointer
;
809 leader
= generic_bignum
;
810 generic_bignum
[0] = 0;
811 /* We could just use digit_2, but lets be mnemonic. */
812 input_line_pointer
= --digit_2
; /* -> 1st digit. */
813 c
= *input_line_pointer
++;
815 (carry
= hex_value
[(int)c
]) < maxdig
;
816 c
= * input_line_pointer
++){
817 for(pointer
= generic_bignum
;
822 work
= carry
+ radix
* *pointer
;
823 *pointer
= work
& LITTLENUM_MASK
;
824 carry
= work
>> LITTLENUM_NUMBER_OF_BITS
;
827 if(leader
< generic_bignum
+
828 SIZE_OF_LARGE_NUMBER
- 1){
829 /* Room to grow a longer bignum. */
834 /* Again, C is char after number, */
835 /* input_line_pointer -> after C. */
836 /* know(BITS_PER_INT == 32); */
837 know(LITTLENUM_NUMBER_OF_BITS
== 16);
838 /* Hence the constant "2" in the next line. */
839 if(leader
< generic_bignum
+ 2 && force_bignum
== FALSE
)
840 { /* Will fit into 32 bits. */
841 number
= ((generic_bignum
[1] & LITTLENUM_MASK
) <<
842 LITTLENUM_NUMBER_OF_BITS
) |
843 (generic_bignum
[0] & LITTLENUM_MASK
);
847 /* Number of littlenums in the bignum. */
848 number
= leader
- generic_bignum
+ 1;
853 * Here with number, in correct radix. c is the next char.
854 * Note that unlike Un*x, we allow "011f" "0x9f" to both
855 * mean the same as the (conventional) "9f". This is simply
856 * easier than checking for strict canonical form.
860 * Backward ref to local label.
861 * Because it is backward, expect it to be DEFINED.
864 * Construct a local label.
866 name
= fb_label_name((int)number
, 0);
867 symbolP
= symbol_table_lookup(name
);
868 if((symbolP
!= NULL
) &&
869 (symbolP
->sy_type
& N_TYPE
) != N_UNDF
){
870 /* Expected path: symbol defined. */
871 /* Local labels are never absolute. Don't waste
872 time checking absoluteness. */
873 know((symbolP
->sy_type
& N_TYPE
) == N_SECT
);
874 expressionP
->X_add_symbol
= symbolP
;
875 expressionP
->X_add_number
= 0;
876 expressionP
->X_seg
= SEG_SECT
;
878 else{ /* Either not seen or not defined. */
879 as_warn("Backw. ref to unknown label \"%lld\","
880 "0 assumed.", number
);
881 expressionP
->X_add_number
= 0;
882 expressionP
->X_seg
= SEG_ABSOLUTE
;
887 * Forward reference. Expect symbol to be
888 * undefined or unknown. Undefined: seen it
889 * before. Unknown: never seen it in this pass.
890 * Construct a local label name, then an
891 * undefined symbol. Don't create a XSEG frag
892 * for it: caller may do that.
893 * Just return it as never seen before.
895 name
= fb_label_name((int)number
, 1);
896 symbolP
= symbol_table_lookup(name
);
898 /* We have no need to check symbol
900 know((symbolP
->sy_type
& N_TYPE
) == N_UNDF
||
901 (symbolP
->sy_type
& N_TYPE
) == N_SECT
);
904 symbolP
= symbol_new(name
, N_UNDF
, 0,0,0,
906 symbol_table_insert(symbolP
);
908 expressionP
->X_add_symbol
= symbolP
;
909 expressionP
->X_seg
= SEG_UNKNOWN
;
910 expressionP
->X_subtract_symbol
= NULL
;
911 expressionP
->X_add_number
= 0;
913 else{ /* a number >= 10 */
914 ignore_c_ll_or_ull(c
);
915 expressionP
->X_add_number
= number
;
916 expressionP
->X_seg
= SEG_ABSOLUTE
;
917 input_line_pointer
--; /* restore following char */
919 } /* not a small number encode returning a bignum */
921 ignore_c_ll_or_ull(c
);
922 expressionP
->X_add_number
= number
;
923 expressionP
->X_seg
= SEG_BIG
;
924 input_line_pointer
--; /* -> char following number. */
926 } /* (If integer constant) */
927 else{ /* input_line_pointer -> floating-point constant. */
931 error_code
= atof_generic(&input_line_pointer
, ".", md_EXP_CHARS
,
932 &generic_floating_point_number
);
935 if(error_code
== ERROR_EXPONENT_OVERFLOW
){
936 as_bad("Bad floating-point constant: exponent overflow" );
939 as_bad("Bad floating-point constant: unknown error "
940 "code=%d.", error_code
);
943 expressionP
->X_seg
= SEG_BIG
;
944 /* input_line_pointer -> just after constant, */
945 /* which may point to whitespace. */
946 know(expressionP
->X_add_number
< 0);
947 /* < 0 means "floating point". */
948 } /* if (not floating-point constant) */
951 else if((c
== '.' || c
== '$') && !is_part_of_name(*input_line_pointer
))
953 else if(c
== '.' && !is_part_of_name(*input_line_pointer
))
957 JF: '.' is pseudo symbol with value of current location in current
960 symbolP
= symbol_new("L0\001",
962 frchain_now
->frch_nsect
,
964 (valueT
)(obstack_next_free(&frags
) -
965 frag_now
->fr_literal
),
967 symbol_assign_index(symbolP
);
968 expressionP
->X_add_number
= 0;
969 expressionP
->X_add_symbol
= symbolP
;
970 expressionP
->X_seg
= SEG_SECT
;
972 /* here if did not begin with a digit */
973 else if(is_name_beginner(c
) || c
== '"'){
975 * Identifier begins here.
976 * This is kludged for speed, so code is repeated.
980 name
= input_line_pointer
-- ;
982 name
= -- input_line_pointer
;
983 c
= get_symbol_end();
984 symbolP
= symbol_table_lookup(name
);
987 * If we have an absolute symbol, then we know it's value now.
988 * Unless the symbol has an expression attached to it in which
989 * case it will have an absolute value but we do not know it
990 * now and will have to wait to evaluate after the symbols of
991 * the expression are known. This can happen with:
993 * where the symbol we have now is x but the value of x is not
994 * know at this point.
998 seg
= N_TYPE_seg
[(int)symbolP
->sy_type
& N_TYPE
];
999 expressionP
->X_seg
= seg
;
1000 if(seg
== SEG_ABSOLUTE
&& symbolP
->expression
== NULL
){
1001 expressionP
->X_add_number
=
1004 * For 32-bit assemblers the type n_value in a symbol
1005 * table entry is unsigned. But since we are using
1006 * this in a signed 64-bit expression we need to sign
1007 * extend this. So by casting it to a signed value and
1008 * then assigning it to the 64-bit value the expression
1016 expressionP
->X_add_number
= 0;
1017 expressionP
->X_add_symbol
= symbolP
;
1018 if(symbolP
->expression
!= NULL
)
1019 expressionP
->X_seg
= SEG_DIFFSECT
;
1023 symbolP
= symbol_new(name
, N_UNDF
, 0,0,0, &zero_address_frag
);
1024 expressionP
->X_add_symbol
= symbolP
;
1025 expressionP
->X_add_number
= 0;
1026 expressionP
->X_seg
= SEG_UNKNOWN
;
1027 symbol_table_insert(symbolP
);
1029 *input_line_pointer
= c
;
1031 input_line_pointer
[-1] = '"';
1032 expressionP
->X_subtract_symbol
= NULL
;
1034 /* didn't begin with digit & not a name */
1036 (void)expression(expressionP
);
1037 /* Expression() will pass trailing whitespace */
1038 if(*input_line_pointer
++ != ')'){
1039 as_warn("Missing ')' assumed");
1040 input_line_pointer
--;
1042 /* here with input_line_pointer -> char after "(...)" */
1044 /* unary operator: hope for SEG_ABSOLUTE */
1045 else if(c
== '~' || c
== '-' || c
== '!'){
1046 switch(operand(expressionP
)){
1048 /* input_line_pointer -> char after operand */
1051 * Notice: '-' may overflow: no warning is given. This is
1052 * compatible with other people's assemblers.
1054 expressionP
->X_add_number
= - expressionP
->X_add_number
;
1057 expressionP
->X_add_number
= ! expressionP
->X_add_number
;
1060 expressionP
->X_add_number
= ~ expressionP
->X_add_number
;
1065 if(c
== '-'){ /* JF I hope this hack works */
1066 expressionP
->X_subtract_symbol
= expressionP
->X_add_symbol
;
1067 expressionP
->X_add_symbol
= 0;
1068 expressionP
->X_seg
= SEG_DIFFSECT
;
1071 default: /* unary on non-absolute is unsuported */
1072 as_warn("Unary operator %c ignored because bad operand follows",
1075 /* Expression undisturbed from operand(). */
1079 * Warning: to conform to other people's assemblers NO ESCAPEMENT is
1080 * permitted for a single quote. The next character, parity errors and
1081 * all, is taken as the value of the operand. VERY KINKY.
1084 expressionP
->X_add_number
= *input_line_pointer
++;
1085 expressionP
->X_seg
= SEG_ABSOLUTE
;
1087 /* can't imagine any other kind of operand */
1089 expressionP
->X_seg
= SEG_NONE
;
1090 input_line_pointer
--;
1093 * It is more 'efficient' to clean up the expressions when they are
1094 * created. Doing it here saves lines of code.
1096 clean_up_expression(expressionP
);
1097 SKIP_WHITESPACE(); /* -> 1st char after operand. */
1098 know(*input_line_pointer
!= ' ');
1099 return(expressionP
->X_seg
);
1102 /* Internal. Simplify a struct expression for use by expr() */
1105 * In: address of a expressionS.
1106 * The X_seg field of the expressionS may only take certain values.
1107 * Now, we permit SEG_NONE to make code smaller & faster.
1108 * Elsewise we waste time special-case testing. Sigh.
1109 * Out: expressionS may have been modified:
1110 * 'foo-foo' symbol references cancelled to 0,
1111 * which changes X_seg from SEG_DIFFSECT to SEG_ABSOLUTE;
1112 * Unused fields zeroed to help expr().
1116 clean_up_expression(
1117 expressionS
*expressionP
)
1119 switch(expressionP
->X_seg
){
1121 expressionP
->X_add_symbol
= NULL
;
1122 expressionP
->X_subtract_symbol
= NULL
;
1123 expressionP
->X_add_number
= 0;
1128 expressionP
->X_subtract_symbol
= NULL
;
1129 expressionP
->X_add_symbol
= NULL
;
1134 expressionP
->X_subtract_symbol
= NULL
;
1139 * It does not hurt to 'cancel' NULL==NULL
1140 * when comparing symbols for 'eq'ness.
1141 * It is faster to re-cancel them to NULL
1142 * than to check for this special case.
1144 if(expressionP
->X_subtract_symbol
== expressionP
->X_add_symbol
){
1145 expressionP
->X_subtract_symbol
= NULL
;
1146 expressionP
->X_add_symbol
= NULL
;
1147 expressionP
->X_seg
= SEG_ABSOLUTE
;
1152 BAD_CASE(expressionP
->X_seg
);
1160 * Internal. Made a function because this code is used in 2 places.
1161 * Generate error or correct X_?????_symbol of expressionS.
1165 Combine and subsume symbol2 into symbol1 where the symbols come from
1166 expression's add or subtract symbols.
1167 The combining always occurs even if it would be an error.
1168 Either symbol maybe NULL which means there is no symbol.
1169 In that case symbol1 is set to the non NULL symbol.
1170 If both are NULL then SEG_ABSOLUTE is returned.
1171 Either symbol maybe undefined.
1172 The only combinations that are not errors are when one symbol does not exist.
1173 if one symbol is undefined and the other doesn't exist SEG_UNKNOWN is
1175 For errant combinations symbol1 is set to NULL and SEG_ABSOLUTE (or -1
1176 (SEG_PASS1) when one of the symbols is undefined and the other exists)
1178 * symbol_1 += symbol_2 ... well ... sort of.
1179 * symbol_1 -= symbol_2 ... well ... sort of.
1185 struct symbol
**symbol_1_PP
,
1186 struct symbol
*symbol_2_P
)
1190 /* The symbols can't be N_ABS as they are in expressions and whould just have
1191 their value copied into the X_add_number part. */
1192 know( (*symbol_1_PP
) == NULL
||
1193 ((*symbol_1_PP
)->sy_type
& N_TYPE
) == N_SECT
||
1194 ((*symbol_1_PP
)->sy_type
& N_TYPE
) == N_UNDF
);
1196 know( symbol_2_P
== NULL
||
1197 (symbol_2_P
->sy_type
& N_TYPE
) == N_SECT
||
1198 (symbol_2_P
->sy_type
& N_TYPE
) == N_UNDF
);
1200 /* check to see if there is a symbol1 */
1201 if(*symbol_1_PP
!= NULL
){
1202 /* there is a symbol1 */
1204 /* check to see if symbol1 is undefined */
1205 if(((*symbol_1_PP
)->sy_type
& N_TYPE
) == N_UNDF
){
1206 /* symbol1 is undefined */
1208 /* check to see if there is a symbol2 */
1209 if(symbol_2_P
!= NULL
){
1210 /* symbol1 is undefined and there is a symbol2 */
1211 *symbol_1_PP
= NULL
;
1215 /* symbol1 is undefined and there is no symbol2 */
1216 return_value
= SEG_UNKNOWN
;
1220 /* there is a defined symbol1 */
1222 /* check to see if there is a symbol2 */
1223 if(symbol_2_P
!= NULL
){
1224 /* there is a symbol2 */
1226 /* check to see if symbol2 is undefined */
1227 if((symbol_2_P
->sy_type
& N_TYPE
) == N_UNDF
){
1228 /* symbol2 is undefined and symbol1 is defined */
1229 *symbol_1_PP
= NULL
;
1233 /* symbol1 is defined and symbol2 is defined */
1234 /* + {symbol1} + {symbol2} or */
1235 /* - {symbol1} - {symbol2} */
1236 as_warn("Expression too complex, 2 symbols forgotten: "
1237 "\"%s\" \"%s\"", (*symbol_1_PP
)->sy_name
,
1238 symbol_2_P
->sy_name
);
1239 *symbol_1_PP
= NULL
;
1240 return_value
= SEG_ABSOLUTE
;
1244 /* symbol1 is defined and there is no symbol2 */
1245 return_value
= N_TYPE_seg
[(*symbol_1_PP
)->sy_type
& N_TYPE
];
1250 /* there is no symbol1 */
1252 /* check to see if there is a symbol2 */
1253 if(symbol_2_P
!= NULL
){
1254 /* symbol2 is defined and there is no symbol1 */
1255 *symbol_1_PP
= symbol_2_P
;
1256 return_value
= N_TYPE_seg
[(symbol_2_P
)->sy_type
& N_TYPE
];
1259 /* there is no symbol1 or symbol2 */
1260 /* ??? why not SEG_UNKNOWN or SEG_NONE */
1261 return_value
= SEG_ABSOLUTE
;
1265 know(return_value
== SEG_ABSOLUTE
||
1266 return_value
== SEG_SECT
||
1267 return_value
== SEG_UNKNOWN
||
1268 return_value
== -1);
1269 know((*symbol_1_PP
) == NULL
||
1270 ((*symbol_1_PP
)->sy_type
& N_TYPE
) ==
1271 seg_N_TYPE
[(int)return_value
]);
1273 return(return_value
);
1277 * DJA -- Here we make a last ditch effort to turn expressions into
1278 * absolutes. This is particularly useful for doing arithemtic
1279 * on already declared labels, for example in going through the
1280 * following table the moveq can really be evaluated.
1287 * moveq #((end-start) / 2) + 1,d0
1288 * loop: cmpw d1,a0@+
1291 segT
/* Return expressionP->X_seg. */
1292 try_to_make_absolute(
1293 expressionS
*expressionP
) /* Deliver result here. */
1295 symbolS
*add_symbol
;
1296 symbolS
*subtract_symbol
;
1298 if(expressionP
->X_seg
== SEG_DIFFSECT
){
1301 * The flag -dynamic is encoded as -k. If this is seen we can
1302 * use the general section difference relocation so we will leave
1308 add_symbol
= expressionP
->X_add_symbol
;
1309 if(add_symbol
== NULL
)
1311 if((add_symbol
->sy_type
& N_TYPE
) != N_SECT
)
1314 subtract_symbol
= expressionP
->X_subtract_symbol
;
1315 if(subtract_symbol
== NULL
)
1317 if((subtract_symbol
->sy_type
& N_TYPE
) != N_SECT
)
1320 if(add_symbol
->sy_frag
== subtract_symbol
->sy_frag
){
1321 if(add_symbol
->sy_frag
!= NULL
&&
1322 expressionP
->X_add_number
+
1323 (int)add_symbol
->sy_value
-
1324 (int)subtract_symbol
->sy_value
>= 0){
1325 expressionP
->X_add_number
+= add_symbol
->sy_value
-
1326 subtract_symbol
->sy_value
;
1327 expressionP
->X_seg
= SEG_ABSOLUTE
;
1328 expressionP
->X_add_symbol
= NULL
;
1329 expressionP
->X_subtract_symbol
= NULL
;
1335 * This logic works only if the chain of frags can't later be
1336 * separated by scattered loading. To make sure that this can't
1337 * happen we would have to make sure all symbols associated with
1338 * frags in the chain are of the Lx form and the -L flag is not
1339 * see so they will not appear in the output (if they are not in
1340 * the output then the link editor can't separate the chain of
1341 * frags by scattered loading). Since this code does not make
1342 * sure of this it is broken. But this is a known bug in the
1343 * NeXT 3.2 and earilier releases so this code is if'ed
1344 * !flagseen['k'] which will make it compatable with 3.2 and
1345 * previous releases.
1349 * Try to see if the chain of frags between the subtract
1350 * symbol and the add symbol is made up of only rs_fill and
1351 * rs_align frags and then calculate the difference. This
1352 * will always work on RISC machines since they won't have
1353 * any machine dependent frags of variable length in the
1356 unsigned long size
, fail
;
1359 if(add_symbol
->sy_frag
!= NULL
&&
1360 subtract_symbol
->sy_frag
!= NULL
){
1363 frag
= subtract_symbol
->sy_frag
;
1364 while(!fail
&& frag
!= NULL
&&
1365 frag
!= add_symbol
->sy_frag
){
1366 if(frag
->fr_type
== rs_align
)
1367 size
= round(size
+ frag
->fr_fix
,
1368 1 << frag
->fr_offset
);
1369 else if(frag
->fr_type
== rs_fill
)
1370 size
+= frag
->fr_fix
+
1371 frag
->fr_var
* frag
->fr_offset
;
1374 frag
= frag
->fr_next
;
1377 if(!fail
&& frag
== add_symbol
->sy_frag
){
1378 expressionP
->X_add_number
= size
+
1379 add_symbol
->sy_value
-
1380 subtract_symbol
->sy_value
;
1381 expressionP
->X_seg
= SEG_ABSOLUTE
;
1382 expressionP
->X_add_symbol
= NULL
;
1383 expressionP
->X_subtract_symbol
= NULL
;
1391 return(expressionP
->X_seg
);
1395 * two_char_op_encoding() return the operator type for two character operators.
1396 * The first_op_char is part of a two character operator and this routine is
1397 * then used to determine the operator type looking at the second character.
1401 two_char_op_encoding(
1404 char second_op_char
;
1406 second_op_char
= input_line_pointer
[1];
1407 switch(first_op_char
){
1409 if(second_op_char
== '<')
1410 return(O_left_shift
);
1411 if(second_op_char
== '=')
1412 return(O_less_than_or_equal
);
1413 if(second_op_char
== '>')
1414 return(O_not_equal
);
1415 return(O_less_than
);
1417 if(second_op_char
== '>')
1418 return(O_right_shift
);
1419 if(second_op_char
== '=')
1420 return(O_greater_than_or_equal
);
1421 return(O_greater_than
);
1423 if(second_op_char
== '=')
1427 if(second_op_char
== '=')
1428 return(O_not_equal
);
1431 if(second_op_char
== '&')
1432 return(O_logical_and
);
1435 if(second_op_char
== '|')
1436 return(O_logical_or
);
1437 return(O_bit_inclusive_or
);
1439 BAD_CASE(first_op_char
);
1447 * This lives here because it belongs equally in expr.c & read.c.
1448 * Expr.c is just a branch office read.c anyway, and putting it
1449 * here lessens the crowd at read.c.
1451 * Assume input_line_pointer is at start of symbol name.
1452 * Advance input_line_pointer past symbol name.
1453 * Turn that character into a '\0', returning its former value.
1454 * This allows a string compare (RMS wants symbol names to be strings)
1455 * of the symbol name.
1456 * There will always be a char following symbol name, because all good
1457 * lines end in end-of-line.
1466 * Symbol names are allowed to have surrounding ""s so that names can
1467 * have any characters in them (including spacesi, colons, etc). This
1468 * is done so names like "[Foo bar:fuz:]" can be used as symbol names.
1470 if(*input_line_pointer
== '"'){
1471 input_line_pointer
++;
1473 c
= *input_line_pointer
++ ;
1474 }while(c
!= '"' && c
!= '\0' && c
!= '\n');
1476 *(input_line_pointer
- 1) = 0;
1477 c
= *input_line_pointer
++;
1481 while(is_part_of_name(c
= *input_line_pointer
++))
1484 *--input_line_pointer
= 0;
1488 #include <stdlib.h> /* for abort */
1490 segT
S_GET_SEGMENT (symbolS
*s
)
1492 return s
->sy_nlist
.n_sect
;
1496 * ignore_c_ll_or_ull
1498 * Ignores 'LL' or 'ULL' after numbers. On entrance, if 'c' is not 'U' or
1499 * 'L', we return. If 'c' is 'U' or 'L', input_line_pointer points to the
1500 * next character. On return, input_line_pointer will have been adjusted
1501 * to be after 'ULL' or 'LL' if either of them starts with c followed by
1502 * input_line_pointer. If that is not the case, input_line_pointer will
1511 if(c
!= 'U' && c
!= 'L'){
1515 c
= *input_line_pointer
++;
1519 c
= *input_line_pointer
++;
1523 c
= *input_line_pointer
++;
1526 input_line_pointer
-= charsRead
;