2000-11-28 David O'Brien <obrien@dragon.nuxi.com>
[deliverable/binutils-gdb.git] / gas / expr.c
CommitLineData
252b5132 1/* expr.c -operands, expressions-
abd63a32 2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
252b5132
RH
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
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)
10 any later version.
11
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.
16
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
929b12bc 20 02111-1307, USA. */
252b5132 21
929b12bc
KH
22/* This is really a branch office of as-read.c. I split it out to clearly
23 distinguish the world of expressions from the world of statements.
24 (It also gives smaller files to re-compile.)
25 Here, "operand"s are of expressions, not instructions. */
252b5132
RH
26
27#include <ctype.h>
28#include <string.h>
29#define min(a, b) ((a) < (b) ? (a) : (b))
30
31#include "as.h"
32#include "obstack.h"
33
34static void floating_constant PARAMS ((expressionS * expressionP));
6d4d30bb
AM
35static valueT generic_bignum_to_int32 PARAMS ((void));
36#ifdef BFD64
37static valueT generic_bignum_to_int64 PARAMS ((void));
38#endif
252b5132
RH
39static void integer_constant PARAMS ((int radix, expressionS * expressionP));
40static void mri_char_constant PARAMS ((expressionS *));
41static void current_location PARAMS ((expressionS *));
42static void clean_up_expression PARAMS ((expressionS * expressionP));
43static segT operand PARAMS ((expressionS *));
44static operatorT operator PARAMS ((void));
45
46extern const char EXP_CHARS[], FLT_CHARS[];
47
48/* We keep a mapping of expression symbols to file positions, so that
49 we can provide better error messages. */
50
e6c774b4 51struct expr_symbol_line {
252b5132
RH
52 struct expr_symbol_line *next;
53 symbolS *sym;
54 char *file;
55 unsigned int line;
56};
57
58static struct expr_symbol_line *expr_symbol_lines;
59\f
60/* Build a dummy symbol to hold a complex expression. This is how we
61 build expressions up out of other expressions. The symbol is put
62 into the fake section expr_section. */
63
64symbolS *
65make_expr_symbol (expressionP)
66 expressionS *expressionP;
67{
68 expressionS zero;
69 const char *fake;
70 symbolS *symbolP;
71 struct expr_symbol_line *n;
72
73 if (expressionP->X_op == O_symbol
74 && expressionP->X_add_number == 0)
75 return expressionP->X_add_symbol;
76
77 if (expressionP->X_op == O_big)
78 {
79 /* This won't work, because the actual value is stored in
80 generic_floating_point_number or generic_bignum, and we are
81 going to lose it if we haven't already. */
82 if (expressionP->X_add_number > 0)
83 as_bad (_("bignum invalid; zero assumed"));
84 else
85 as_bad (_("floating point number invalid; zero assumed"));
86 zero.X_op = O_constant;
87 zero.X_add_number = 0;
88 zero.X_unsigned = 0;
89 clean_up_expression (&zero);
90 expressionP = &zero;
91 }
92
93 fake = FAKE_LABEL_NAME;
94
95 /* Putting constant symbols in absolute_section rather than
96 expr_section is convenient for the old a.out code, for which
97 S_GET_SEGMENT does not always retrieve the value put in by
98 S_SET_SEGMENT. */
99 symbolP = symbol_create (fake,
100 (expressionP->X_op == O_constant
101 ? absolute_section
102 : expr_section),
103 0, &zero_address_frag);
49309057 104 symbol_set_value_expression (symbolP, expressionP);
252b5132
RH
105
106 if (expressionP->X_op == O_constant)
107 resolve_symbol_value (symbolP, 1);
108
109 n = (struct expr_symbol_line *) xmalloc (sizeof *n);
110 n->sym = symbolP;
111 as_where (&n->file, &n->line);
112 n->next = expr_symbol_lines;
113 expr_symbol_lines = n;
114
115 return symbolP;
116}
117
118/* Return the file and line number for an expr symbol. Return
119 non-zero if something was found, 0 if no information is known for
120 the symbol. */
121
122int
123expr_symbol_where (sym, pfile, pline)
124 symbolS *sym;
125 char **pfile;
126 unsigned int *pline;
127{
128 register struct expr_symbol_line *l;
129
130 for (l = expr_symbol_lines; l != NULL; l = l->next)
131 {
132 if (l->sym == sym)
133 {
134 *pfile = l->file;
135 *pline = l->line;
136 return 1;
137 }
138 }
139
140 return 0;
141}
142\f
143/* Utilities for building expressions.
144 Since complex expressions are recorded as symbols for use in other
145 expressions these return a symbolS * and not an expressionS *.
146 These explicitly do not take an "add_number" argument. */
147/* ??? For completeness' sake one might want expr_build_symbol.
148 It would just return its argument. */
149
150/* Build an expression for an unsigned constant.
151 The corresponding one for signed constants is missing because
152 there's currently no need for it. One could add an unsigned_p flag
153 but that seems more clumsy. */
154
155symbolS *
156expr_build_uconstant (value)
157 offsetT value;
158{
159 expressionS e;
160
161 e.X_op = O_constant;
162 e.X_add_number = value;
163 e.X_unsigned = 1;
164 return make_expr_symbol (&e);
165}
166
167/* Build an expression for OP s1. */
168
169symbolS *
170expr_build_unary (op, s1)
171 operatorT op;
172 symbolS *s1;
173{
174 expressionS e;
175
176 e.X_op = op;
177 e.X_add_symbol = s1;
178 e.X_add_number = 0;
179 return make_expr_symbol (&e);
180}
181
182/* Build an expression for s1 OP s2. */
183
184symbolS *
185expr_build_binary (op, s1, s2)
186 operatorT op;
187 symbolS *s1;
188 symbolS *s2;
189{
190 expressionS e;
191
192 e.X_op = op;
193 e.X_add_symbol = s1;
194 e.X_op_symbol = s2;
195 e.X_add_number = 0;
196 return make_expr_symbol (&e);
197}
198
199/* Build an expression for the current location ('.'). */
200
201symbolS *
202expr_build_dot ()
203{
204 expressionS e;
205
206 current_location (&e);
207 return make_expr_symbol (&e);
208}
209\f
929b12bc
KH
210/* Build any floating-point literal here.
211 Also build any bignum literal here. */
252b5132
RH
212
213/* Seems atof_machine can backscan through generic_bignum and hit whatever
214 happens to be loaded before it in memory. And its way too complicated
215 for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
216 and never write into the early words, thus they'll always be zero.
217 I hate Dean's floating-point code. Bleh. */
218LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
e6c774b4
KH
219
220FLONUM_TYPE generic_floating_point_number = {
bc4466dc
KH
221 &generic_bignum[6], /* low. (JF: Was 0) */
222 &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
223 0, /* leader. */
224 0, /* exponent. */
225 0 /* sign. */
252b5132 226};
929b12bc
KH
227
228/* If nonzero, we've been asked to assemble nan, +inf or -inf. */
252b5132
RH
229int generic_floating_point_magic;
230\f
231static void
232floating_constant (expressionP)
233 expressionS *expressionP;
234{
929b12bc 235 /* input_line_pointer -> floating-point constant. */
252b5132
RH
236 int error_code;
237
238 error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
239 &generic_floating_point_number);
240
241 if (error_code)
242 {
243 if (error_code == ERROR_EXPONENT_OVERFLOW)
244 {
245 as_bad (_("bad floating-point constant: exponent overflow, probably assembling junk"));
246 }
247 else
248 {
249 as_bad (_("bad floating-point constant: unknown error code=%d."), error_code);
250 }
251 }
252 expressionP->X_op = O_big;
929b12bc
KH
253 /* input_line_pointer -> just after constant, which may point to
254 whitespace. */
252b5132
RH
255 expressionP->X_add_number = -1;
256}
257
929b12bc
KH
258static valueT
259generic_bignum_to_int32 ()
252b5132
RH
260{
261 valueT number =
262 ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
263 | (generic_bignum[0] & LITTLENUM_MASK);
264 number &= 0xffffffff;
265 return number;
266}
267
268#ifdef BFD64
929b12bc
KH
269static valueT
270generic_bignum_to_int64 ()
252b5132 271{
929b12bc
KH
272 valueT number =
273 ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
274 << LITTLENUM_NUMBER_OF_BITS)
275 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
276 << LITTLENUM_NUMBER_OF_BITS)
277 | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
278 << LITTLENUM_NUMBER_OF_BITS)
279 | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
252b5132
RH
280 return number;
281}
282#endif
283
284static void
285integer_constant (radix, expressionP)
286 int radix;
287 expressionS *expressionP;
288{
929b12bc 289 char *start; /* Start of number. */
252b5132
RH
290 char *suffix = NULL;
291 char c;
929b12bc
KH
292 valueT number; /* Offset or (absolute) value. */
293 short int digit; /* Value of next digit in current radix. */
294 short int maxdig = 0; /* Highest permitted digit value. */
295 int too_many_digits = 0; /* If we see >= this number of. */
296 char *name; /* Points to name of symbol. */
297 symbolS *symbolP; /* Points to symbol. */
252b5132 298
929b12bc 299 int small; /* True if fits in 32 bits. */
252b5132 300
929b12bc 301 /* May be bignum, or may fit in 32 bits. */
252b5132
RH
302 /* Most numbers fit into 32 bits, and we want this case to be fast.
303 so we pretend it will fit into 32 bits. If, after making up a 32
304 bit number, we realise that we have scanned more digits than
305 comfortably fit into 32 bits, we re-scan the digits coding them
306 into a bignum. For decimal and octal numbers we are
307 conservative: Some numbers may be assumed bignums when in fact
308 they do fit into 32 bits. Numbers of any radix can have excess
309 leading zeros: We strive to recognise this and cast them back
310 into 32 bits. We must check that the bignum really is more than
311 32 bits, and change it back to a 32-bit number if it fits. The
312 number we are looking for is expected to be positive, but if it
313 fits into 32 bits as an unsigned number, we let it be a 32-bit
929b12bc 314 number. The cavalier approach is for speed in ordinary cases. */
252b5132
RH
315 /* This has been extended for 64 bits. We blindly assume that if
316 you're compiling in 64-bit mode, the target is a 64-bit machine.
317 This should be cleaned up. */
318
319#ifdef BFD64
320#define valuesize 64
321#else /* includes non-bfd case, mostly */
322#define valuesize 32
323#endif
324
f805106c 325 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
252b5132
RH
326 {
327 int flt = 0;
328
329 /* In MRI mode, the number may have a suffix indicating the
330 radix. For that matter, it might actually be a floating
331 point constant. */
332 for (suffix = input_line_pointer;
333 isalnum ((unsigned char) *suffix);
334 suffix++)
335 {
336 if (*suffix == 'e' || *suffix == 'E')
337 flt = 1;
338 }
339
340 if (suffix == input_line_pointer)
341 {
342 radix = 10;
343 suffix = NULL;
344 }
345 else
346 {
347 c = *--suffix;
348 if (islower ((unsigned char) c))
349 c = toupper (c);
350 if (c == 'B')
351 radix = 2;
352 else if (c == 'D')
353 radix = 10;
354 else if (c == 'O' || c == 'Q')
355 radix = 8;
356 else if (c == 'H')
357 radix = 16;
358 else if (suffix[1] == '.' || c == 'E' || flt)
359 {
360 floating_constant (expressionP);
361 return;
362 }
363 else
364 {
365 radix = 10;
366 suffix = NULL;
367 }
368 }
369 }
370
371 switch (radix)
372 {
373 case 2:
374 maxdig = 2;
375 too_many_digits = valuesize + 1;
376 break;
377 case 8:
378 maxdig = radix = 8;
379 too_many_digits = (valuesize + 2) / 3 + 1;
380 break;
381 case 16:
382 maxdig = radix = 16;
383 too_many_digits = (valuesize + 3) / 4 + 1;
384 break;
385 case 10:
386 maxdig = radix = 10;
929b12bc 387 too_many_digits = (valuesize + 11) / 4; /* Very rough. */
252b5132
RH
388 }
389#undef valuesize
390 start = input_line_pointer;
391 c = *input_line_pointer++;
392 for (number = 0;
393 (digit = hex_value (c)) < maxdig;
394 c = *input_line_pointer++)
395 {
396 number = number * radix + digit;
397 }
929b12bc
KH
398 /* c contains character after number. */
399 /* input_line_pointer->char after c. */
252b5132
RH
400 small = (input_line_pointer - start - 1) < too_many_digits;
401
929b12bc 402 if (radix == 16 && c == '_')
252b5132
RH
403 {
404 /* This is literal of the form 0x333_0_12345678_1.
405 This example is equivalent to 0x00000333000000001234567800000001. */
406
407 int num_little_digits = 0;
408 int i;
929b12bc 409 input_line_pointer = start; /* -> 1st digit. */
252b5132
RH
410
411 know (LITTLENUM_NUMBER_OF_BITS == 16);
412
929b12bc 413 for (c = '_'; c == '_'; num_little_digits += 2)
252b5132
RH
414 {
415
929b12bc
KH
416 /* Convert one 64-bit word. */
417 int ndigit = 0;
252b5132
RH
418 number = 0;
419 for (c = *input_line_pointer++;
420 (digit = hex_value (c)) < maxdig;
421 c = *(input_line_pointer++))
422 {
423 number = number * radix + digit;
424 ndigit++;
425 }
426
427 /* Check for 8 digit per word max. */
929b12bc 428 if (ndigit > 8)
252b5132
RH
429 as_bad (_("A bignum with underscores may not have more than 8 hex digits in any word."));
430
929b12bc
KH
431 /* Add this chunk to the bignum.
432 Shift things down 2 little digits. */
252b5132 433 know (LITTLENUM_NUMBER_OF_BITS == 16);
929b12bc
KH
434 for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
435 i >= 2;
436 i--)
437 generic_bignum[i] = generic_bignum[i - 2];
252b5132 438
929b12bc 439 /* Add the new digits as the least significant new ones. */
252b5132
RH
440 generic_bignum[0] = number & 0xffffffff;
441 generic_bignum[1] = number >> 16;
442 }
443
929b12bc 444 /* Again, c is char after number, input_line_pointer->after c. */
252b5132
RH
445
446 if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
447 num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
448
449 assert (num_little_digits >= 4);
450
451 if (num_little_digits != 8)
452 as_bad (_("A bignum with underscores must have exactly 4 words."));
453
454 /* We might have some leading zeros. These can be trimmed to give
929b12bc
KH
455 us a change to fit this constant into a small number. */
456 while (generic_bignum[num_little_digits - 1] == 0
457 && num_little_digits > 1)
252b5132 458 num_little_digits--;
929b12bc 459
252b5132
RH
460 if (num_little_digits <= 2)
461 {
929b12bc 462 /* will fit into 32 bits. */
252b5132
RH
463 number = generic_bignum_to_int32 ();
464 small = 1;
465 }
466#ifdef BFD64
467 else if (num_little_digits <= 4)
468 {
469 /* Will fit into 64 bits. */
470 number = generic_bignum_to_int64 ();
471 small = 1;
472 }
473#endif
474 else
475 {
476 small = 0;
929b12bc
KH
477
478 /* Number of littlenums in the bignum. */
479 number = num_little_digits;
252b5132
RH
480 }
481 }
482 else if (!small)
483 {
929b12bc
KH
484 /* We saw a lot of digits. manufacture a bignum the hard way. */
485 LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
486 LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
252b5132
RH
487 long carry;
488
489 leader = generic_bignum;
490 generic_bignum[0] = 0;
491 generic_bignum[1] = 0;
492 generic_bignum[2] = 0;
493 generic_bignum[3] = 0;
929b12bc 494 input_line_pointer = start; /* -> 1st digit. */
252b5132 495 c = *input_line_pointer++;
929b12bc 496 for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
252b5132 497 {
929b12bc 498 for (pointer = generic_bignum; pointer <= leader; pointer++)
252b5132
RH
499 {
500 long work;
501
502 work = carry + radix * *pointer;
503 *pointer = work & LITTLENUM_MASK;
504 carry = work >> LITTLENUM_NUMBER_OF_BITS;
505 }
506 if (carry)
507 {
508 if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
509 {
929b12bc 510 /* Room to grow a longer bignum. */
252b5132
RH
511 *++leader = carry;
512 }
513 }
514 }
929b12bc
KH
515 /* Again, c is char after number. */
516 /* input_line_pointer -> after c. */
252b5132
RH
517 know (LITTLENUM_NUMBER_OF_BITS == 16);
518 if (leader < generic_bignum + 2)
519 {
929b12bc 520 /* Will fit into 32 bits. */
252b5132
RH
521 number = generic_bignum_to_int32 ();
522 small = 1;
523 }
524#ifdef BFD64
525 else if (leader < generic_bignum + 4)
526 {
527 /* Will fit into 64 bits. */
528 number = generic_bignum_to_int64 ();
529 small = 1;
530 }
531#endif
532 else
533 {
929b12bc
KH
534 /* Number of littlenums in the bignum. */
535 number = leader - generic_bignum + 1;
252b5132
RH
536 }
537 }
538
60bcf0fa
NC
539 if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
540 && suffix != NULL
f805106c 541 && input_line_pointer - 1 == suffix)
252b5132
RH
542 c = *input_line_pointer++;
543
544 if (small)
545 {
929b12bc
KH
546 /* Here with number, in correct radix. c is the next char.
547 Note that unlike un*x, we allow "011f" "0x9f" to both mean
548 the same as the (conventional) "9f".
549 This is simply easier than checking for strict canonical
550 form. Syntax sux! */
252b5132
RH
551
552 if (LOCAL_LABELS_FB && c == 'b')
553 {
929b12bc
KH
554 /* Backward ref to local label.
555 Because it is backward, expect it to be defined. */
252b5132
RH
556 /* Construct a local label. */
557 name = fb_label_name ((int) number, 0);
558
929b12bc 559 /* Seen before, or symbol is defined: OK. */
252b5132
RH
560 symbolP = symbol_find (name);
561 if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
562 {
929b12bc
KH
563 /* Local labels are never absolute. Don't waste time
564 checking absoluteness. */
252b5132
RH
565 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
566
567 expressionP->X_op = O_symbol;
568 expressionP->X_add_symbol = symbolP;
569 }
570 else
571 {
929b12bc 572 /* Either not seen or not defined. */
252b5132
RH
573 /* @@ Should print out the original string instead of
574 the parsed number. */
575 as_bad (_("backw. ref to unknown label \"%d:\", 0 assumed."),
576 (int) number);
577 expressionP->X_op = O_constant;
578 }
579
580 expressionP->X_add_number = 0;
581 } /* case 'b' */
582 else if (LOCAL_LABELS_FB && c == 'f')
583 {
929b12bc
KH
584 /* Forward reference. Expect symbol to be undefined or
585 unknown. undefined: seen it before. unknown: never seen
586 it before.
587
588 Construct a local label name, then an undefined symbol.
589 Don't create a xseg frag for it: caller may do that.
590 Just return it as never seen before. */
252b5132
RH
591 name = fb_label_name ((int) number, 1);
592 symbolP = symbol_find_or_make (name);
929b12bc 593 /* We have no need to check symbol properties. */
252b5132 594#ifndef many_segments
929b12bc 595 /* Since "know" puts its arg into a "string", we
252b5132
RH
596 can't have newlines in the argument. */
597 know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
598#endif
599 expressionP->X_op = O_symbol;
600 expressionP->X_add_symbol = symbolP;
601 expressionP->X_add_number = 0;
602 } /* case 'f' */
603 else if (LOCAL_LABELS_DOLLAR && c == '$')
604 {
605 /* If the dollar label is *currently* defined, then this is just
606 another reference to it. If it is not *currently* defined,
607 then this is a fresh instantiation of that number, so create
608 it. */
609
610 if (dollar_label_defined ((long) number))
611 {
612 name = dollar_label_name ((long) number, 0);
613 symbolP = symbol_find (name);
614 know (symbolP != NULL);
615 }
616 else
617 {
618 name = dollar_label_name ((long) number, 1);
619 symbolP = symbol_find_or_make (name);
620 }
621
622 expressionP->X_op = O_symbol;
623 expressionP->X_add_symbol = symbolP;
624 expressionP->X_add_number = 0;
625 } /* case '$' */
626 else
627 {
628 expressionP->X_op = O_constant;
629#ifdef TARGET_WORD_SIZE
630 /* Sign extend NUMBER. */
631 number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1);
632#endif
633 expressionP->X_add_number = number;
bc4466dc 634 input_line_pointer--; /* Restore following character. */
929b12bc 635 } /* Really just a number. */
252b5132
RH
636 }
637 else
638 {
bc4466dc 639 /* Not a small number. */
252b5132 640 expressionP->X_op = O_big;
929b12bc
KH
641 expressionP->X_add_number = number; /* Number of littlenums. */
642 input_line_pointer--; /* -> char following number. */
252b5132
RH
643 }
644}
645
646/* Parse an MRI multi character constant. */
647
648static void
649mri_char_constant (expressionP)
650 expressionS *expressionP;
651{
652 int i;
653
654 if (*input_line_pointer == '\''
655 && input_line_pointer[1] != '\'')
656 {
657 expressionP->X_op = O_constant;
658 expressionP->X_add_number = 0;
659 return;
660 }
661
662 /* In order to get the correct byte ordering, we must build the
663 number in reverse. */
664 for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
665 {
666 int j;
667
668 generic_bignum[i] = 0;
669 for (j = 0; j < CHARS_PER_LITTLENUM; j++)
670 {
671 if (*input_line_pointer == '\'')
672 {
673 if (input_line_pointer[1] != '\'')
674 break;
675 ++input_line_pointer;
676 }
677 generic_bignum[i] <<= 8;
678 generic_bignum[i] += *input_line_pointer;
679 ++input_line_pointer;
680 }
681
682 if (i < SIZE_OF_LARGE_NUMBER - 1)
683 {
684 /* If there is more than one littlenum, left justify the
685 last one to make it match the earlier ones. If there is
686 only one, we can just use the value directly. */
687 for (; j < CHARS_PER_LITTLENUM; j++)
688 generic_bignum[i] <<= 8;
689 }
690
691 if (*input_line_pointer == '\''
692 && input_line_pointer[1] != '\'')
693 break;
694 }
695
696 if (i < 0)
697 {
698 as_bad (_("Character constant too large"));
699 i = 0;
700 }
701
702 if (i > 0)
703 {
704 int c;
705 int j;
706
707 c = SIZE_OF_LARGE_NUMBER - i;
708 for (j = 0; j < c; j++)
709 generic_bignum[j] = generic_bignum[i + j];
710 i = c;
711 }
712
713 know (LITTLENUM_NUMBER_OF_BITS == 16);
714 if (i > 2)
715 {
716 expressionP->X_op = O_big;
717 expressionP->X_add_number = i;
718 }
719 else
720 {
721 expressionP->X_op = O_constant;
722 if (i < 2)
723 expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
724 else
725 expressionP->X_add_number =
726 (((generic_bignum[1] & LITTLENUM_MASK)
727 << LITTLENUM_NUMBER_OF_BITS)
728 | (generic_bignum[0] & LITTLENUM_MASK));
729 }
730
731 /* Skip the final closing quote. */
732 ++input_line_pointer;
733}
734
735/* Return an expression representing the current location. This
736 handles the magic symbol `.'. */
737
738static void
739current_location (expressionp)
740 expressionS *expressionp;
741{
742 if (now_seg == absolute_section)
743 {
744 expressionp->X_op = O_constant;
745 expressionp->X_add_number = abs_section_offset;
746 }
747 else
748 {
749 symbolS *symbolp;
750
751 symbolp = symbol_new (FAKE_LABEL_NAME, now_seg,
752 (valueT) frag_now_fix (),
753 frag_now);
754 expressionp->X_op = O_symbol;
755 expressionp->X_add_symbol = symbolp;
756 expressionp->X_add_number = 0;
757 }
758}
759
929b12bc
KH
760/* In: Input_line_pointer points to 1st char of operand, which may
761 be a space.
762
763 Out: A expressionS.
764 The operand may have been empty: in this case X_op == O_absent.
765 Input_line_pointer->(next non-blank) char after operand. */
252b5132
RH
766
767static segT
768operand (expressionP)
769 expressionS *expressionP;
770{
771 char c;
929b12bc
KH
772 symbolS *symbolP; /* Points to symbol. */
773 char *name; /* Points to name of symbol. */
252b5132
RH
774 segT segment;
775
776 /* All integers are regarded as unsigned unless they are negated.
777 This is because the only thing which cares whether a number is
778 unsigned is the code in emit_expr which extends constants into
779 bignums. It should only sign extend negative numbers, so that
780 something like ``.quad 0x80000000'' is not sign extended even
781 though it appears negative if valueT is 32 bits. */
782 expressionP->X_unsigned = 1;
783
929b12bc 784 /* Digits, assume it is a bignum. */
252b5132 785
929b12bc 786 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
bc4466dc 787 c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
252b5132 788
b75c0c92
AM
789 if (is_end_of_line[(unsigned char) c])
790 goto eol;
791
252b5132
RH
792 switch (c)
793 {
794 case '1':
795 case '2':
796 case '3':
797 case '4':
798 case '5':
799 case '6':
800 case '7':
801 case '8':
802 case '9':
803 input_line_pointer--;
804
60bcf0fa 805 integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
929b12bc 806 ? 0 : 10,
f805106c 807 expressionP);
252b5132
RH
808 break;
809
ed6d6fd3
FCE
810#ifdef LITERAL_PREFIXDOLLAR_HEX
811 case '$':
812 integer_constant (16, expressionP);
813 break;
814#endif
815
977e771a
FCE
816#ifdef LITERAL_PREFIXPERCENT_BIN
817 case '%':
818 integer_constant (2, expressionP);
819 break;
820#endif
821
252b5132 822 case '0':
929b12bc 823 /* Non-decimal radix. */
252b5132 824
f805106c 825 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
826 {
827 char *s;
828
829 /* Check for a hex constant. */
830 for (s = input_line_pointer; hex_p (*s); s++)
831 ;
832 if (*s == 'h' || *s == 'H')
833 {
834 --input_line_pointer;
835 integer_constant (0, expressionP);
836 break;
837 }
929b12bc 838 }
252b5132
RH
839 c = *input_line_pointer;
840 switch (c)
841 {
842 case 'o':
843 case 'O':
844 case 'q':
845 case 'Q':
846 case '8':
847 case '9':
f805106c 848 if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
252b5132
RH
849 {
850 integer_constant (0, expressionP);
851 break;
852 }
853 /* Fall through. */
854 default:
855 default_case:
856 if (c && strchr (FLT_CHARS, c))
857 {
858 input_line_pointer++;
859 floating_constant (expressionP);
860 expressionP->X_add_number =
958b5f01 861 - (isupper ((unsigned char) c) ? tolower (c) : c);
252b5132
RH
862 }
863 else
864 {
929b12bc 865 /* The string was only zero. */
252b5132
RH
866 expressionP->X_op = O_constant;
867 expressionP->X_add_number = 0;
868 }
869
870 break;
871
872 case 'x':
873 case 'X':
ab266a97 874 if (flag_m68k_mri)
252b5132
RH
875 goto default_case;
876 input_line_pointer++;
877 integer_constant (16, expressionP);
878 break;
879
880 case 'b':
6dc19fc4 881 if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
252b5132
RH
882 {
883 /* This code used to check for '+' and '-' here, and, in
884 some conditions, fall through to call
885 integer_constant. However, that didn't make sense,
886 as integer_constant only accepts digits. */
887 /* Some of our code elsewhere does permit digits greater
888 than the expected base; for consistency, do the same
889 here. */
890 if (input_line_pointer[1] < '0'
891 || input_line_pointer[1] > '9')
892 {
893 /* Parse this as a back reference to label 0. */
894 input_line_pointer--;
895 integer_constant (10, expressionP);
896 break;
897 }
898 /* Otherwise, parse this as a binary number. */
899 }
900 /* Fall through. */
901 case 'B':
902 input_line_pointer++;
6dc19fc4 903 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
904 goto default_case;
905 integer_constant (2, expressionP);
906 break;
907
908 case '0':
909 case '1':
910 case '2':
911 case '3':
912 case '4':
913 case '5':
914 case '6':
915 case '7':
6dc19fc4 916 integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
929b12bc
KH
917 ? 0 : 8,
918 expressionP);
252b5132
RH
919 break;
920
921 case 'f':
922 if (LOCAL_LABELS_FB)
923 {
924 /* If it says "0f" and it could possibly be a floating point
925 number, make it one. Otherwise, make it a local label,
926 and try to deal with parsing the rest later. */
927 if (!input_line_pointer[1]
271bb601
HPN
928 || (is_end_of_line[0xff & input_line_pointer[1]])
929 || strchr (FLT_CHARS, 'f') == NULL)
252b5132
RH
930 goto is_0f_label;
931 {
932 char *cp = input_line_pointer + 1;
933 int r = atof_generic (&cp, ".", EXP_CHARS,
934 &generic_floating_point_number);
935 switch (r)
936 {
937 case 0:
938 case ERROR_EXPONENT_OVERFLOW:
939 if (*cp == 'f' || *cp == 'b')
929b12bc 940 /* Looks like a difference expression. */
252b5132
RH
941 goto is_0f_label;
942 else if (cp == input_line_pointer + 1)
943 /* No characters has been accepted -- looks like
929b12bc 944 end of operand. */
252b5132
RH
945 goto is_0f_label;
946 else
947 goto is_0f_float;
948 default:
949 as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
950 r);
951 }
952 }
953
954 /* Okay, now we've sorted it out. We resume at one of these
955 two labels, depending on what we've decided we're probably
956 looking at. */
957 is_0f_label:
958 input_line_pointer--;
959 integer_constant (10, expressionP);
960 break;
961
962 is_0f_float:
929b12bc 963 /* Fall through. */
252b5132
RH
964 ;
965 }
966
967 case 'd':
968 case 'D':
6dc19fc4 969 if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
252b5132
RH
970 {
971 integer_constant (0, expressionP);
972 break;
973 }
974 /* Fall through. */
975 case 'F':
976 case 'r':
977 case 'e':
978 case 'E':
979 case 'g':
980 case 'G':
981 input_line_pointer++;
982 floating_constant (expressionP);
983 expressionP->X_add_number =
958b5f01 984 - (isupper ((unsigned char) c) ? tolower (c) : c);
252b5132
RH
985 break;
986
987 case '$':
988 if (LOCAL_LABELS_DOLLAR)
989 {
990 integer_constant (10, expressionP);
991 break;
992 }
993 else
994 goto default_case;
995 }
996
997 break;
998
999 case '(':
b585bc2c 1000#ifndef NEED_INDEX_OPERATOR
252b5132 1001 case '[':
b585bc2c 1002#endif
929b12bc 1003 /* Didn't begin with digit & not a name. */
252b5132 1004 segment = expression (expressionP);
929b12bc 1005 /* expression () will pass trailing whitespace. */
f7c88872
AM
1006 if ((c == '(' && *input_line_pointer != ')')
1007 || (c == '[' && *input_line_pointer != ']'))
252b5132 1008 {
f7c88872
AM
1009#ifdef RELAX_PAREN_GROUPING
1010 if (c != '(')
1011#endif
1012 as_bad (_("Missing '%c' assumed"), c == '(' ? ')' : ']');
252b5132 1013 }
f7c88872 1014 else
929b12bc 1015 input_line_pointer++;
252b5132 1016 SKIP_WHITESPACE ();
929b12bc 1017 /* Here with input_line_pointer -> char after "(...)". */
252b5132
RH
1018 return segment;
1019
abd63a32 1020#ifdef TC_M68K
252b5132
RH
1021 case 'E':
1022 if (! flag_m68k_mri || *input_line_pointer != '\'')
1023 goto de_fault;
1024 as_bad (_("EBCDIC constants are not supported"));
1025 /* Fall through. */
1026 case 'A':
1027 if (! flag_m68k_mri || *input_line_pointer != '\'')
1028 goto de_fault;
1029 ++input_line_pointer;
1030 /* Fall through. */
abd63a32 1031#endif
252b5132
RH
1032 case '\'':
1033 if (! flag_m68k_mri)
1034 {
1035 /* Warning: to conform to other people's assemblers NO
929b12bc 1036 ESCAPEMENT is permitted for a single quote. The next
252b5132 1037 character, parity errors and all, is taken as the value
929b12bc 1038 of the operand. VERY KINKY. */
252b5132
RH
1039 expressionP->X_op = O_constant;
1040 expressionP->X_add_number = *input_line_pointer++;
1041 break;
1042 }
1043
1044 mri_char_constant (expressionP);
1045 break;
1046
1047 case '+':
1048 (void) operand (expressionP);
1049 break;
1050
abd63a32 1051#ifdef TC_M68K
252b5132
RH
1052 case '"':
1053 /* Double quote is the bitwise not operator in MRI mode. */
1054 if (! flag_m68k_mri)
1055 goto de_fault;
1056 /* Fall through. */
abd63a32 1057#endif
252b5132 1058 case '~':
929b12bc 1059 /* '~' is permitted to start a label on the Delta. */
252b5132
RH
1060 if (is_name_beginner (c))
1061 goto isname;
1062 case '!':
1063 case '-':
1064 {
1065 operand (expressionP);
1066 if (expressionP->X_op == O_constant)
1067 {
929b12bc 1068 /* input_line_pointer -> char after operand. */
252b5132
RH
1069 if (c == '-')
1070 {
958b5f01 1071 expressionP->X_add_number = - expressionP->X_add_number;
929b12bc
KH
1072 /* Notice: '-' may overflow: no warning is given.
1073 This is compatible with other people's
1074 assemblers. Sigh. */
252b5132
RH
1075 expressionP->X_unsigned = 0;
1076 }
1077 else if (c == '~' || c == '"')
1078 expressionP->X_add_number = ~ expressionP->X_add_number;
1079 else
1080 expressionP->X_add_number = ! expressionP->X_add_number;
1081 }
1082 else if (expressionP->X_op != O_illegal
1083 && expressionP->X_op != O_absent)
1084 {
1085 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1086 if (c == '-')
1087 expressionP->X_op = O_uminus;
1088 else if (c == '~' || c == '"')
1089 expressionP->X_op = O_bit_not;
1090 else
1091 expressionP->X_op = O_logical_not;
1092 expressionP->X_add_number = 0;
1093 }
1094 else
1095 as_warn (_("Unary operator %c ignored because bad operand follows"),
1096 c);
1097 }
1098 break;
1099
abd63a32 1100#if defined (DOLLAR_DOT) || defined (TC_M68K)
252b5132 1101 case '$':
929b12bc
KH
1102 /* '$' is the program counter when in MRI mode, or when
1103 DOLLAR_DOT is defined. */
252b5132
RH
1104#ifndef DOLLAR_DOT
1105 if (! flag_m68k_mri)
1106 goto de_fault;
1107#endif
1108 if (flag_m68k_mri && hex_p (*input_line_pointer))
1109 {
929b12bc 1110 /* In MRI mode, '$' is also used as the prefix for a
252b5132
RH
1111 hexadecimal constant. */
1112 integer_constant (16, expressionP);
1113 break;
1114 }
1115
1116 if (is_part_of_name (*input_line_pointer))
1117 goto isname;
1118
1119 current_location (expressionP);
1120 break;
abd63a32 1121#endif
252b5132
RH
1122
1123 case '.':
1124 if (!is_part_of_name (*input_line_pointer))
1125 {
1126 current_location (expressionP);
1127 break;
1128 }
1129 else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1130 && ! is_part_of_name (input_line_pointer[8]))
1131 || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1132 && ! is_part_of_name (input_line_pointer[7])))
1133 {
1134 int start;
1135
1136 start = (input_line_pointer[1] == 't'
1137 || input_line_pointer[1] == 'T');
1138 input_line_pointer += start ? 8 : 7;
1139 SKIP_WHITESPACE ();
1140 if (*input_line_pointer != '(')
1141 as_bad (_("syntax error in .startof. or .sizeof."));
1142 else
1143 {
1144 char *buf;
1145
1146 ++input_line_pointer;
1147 SKIP_WHITESPACE ();
1148 name = input_line_pointer;
1149 c = get_symbol_end ();
1150
1151 buf = (char *) xmalloc (strlen (name) + 10);
1152 if (start)
1153 sprintf (buf, ".startof.%s", name);
1154 else
1155 sprintf (buf, ".sizeof.%s", name);
1156 symbolP = symbol_make (buf);
1157 free (buf);
1158
1159 expressionP->X_op = O_symbol;
1160 expressionP->X_add_symbol = symbolP;
1161 expressionP->X_add_number = 0;
1162
1163 *input_line_pointer = c;
1164 SKIP_WHITESPACE ();
1165 if (*input_line_pointer != ')')
1166 as_bad (_("syntax error in .startof. or .sizeof."));
1167 else
1168 ++input_line_pointer;
1169 }
1170 break;
1171 }
1172 else
1173 {
1174 goto isname;
1175 }
b75c0c92 1176
252b5132 1177 case ',':
252b5132 1178 eol:
929b12bc 1179 /* Can't imagine any other kind of operand. */
252b5132
RH
1180 expressionP->X_op = O_absent;
1181 input_line_pointer--;
1182 break;
1183
abd63a32 1184#ifdef TC_M68K
252b5132
RH
1185 case '%':
1186 if (! flag_m68k_mri)
1187 goto de_fault;
1188 integer_constant (2, expressionP);
1189 break;
1190
1191 case '@':
1192 if (! flag_m68k_mri)
1193 goto de_fault;
1194 integer_constant (8, expressionP);
1195 break;
1196
1197 case ':':
1198 if (! flag_m68k_mri)
1199 goto de_fault;
1200
1201 /* In MRI mode, this is a floating point constant represented
1202 using hexadecimal digits. */
1203
1204 ++input_line_pointer;
1205 integer_constant (16, expressionP);
1206 break;
1207
1208 case '*':
1209 if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1210 goto de_fault;
1211
1212 current_location (expressionP);
1213 break;
abd63a32 1214#endif
252b5132
RH
1215
1216 default:
abd63a32 1217#ifdef TC_M68K
252b5132 1218 de_fault:
abd63a32 1219#endif
929b12bc 1220 if (is_name_beginner (c)) /* Here if did not begin with a digit. */
252b5132 1221 {
929b12bc
KH
1222 /* Identifier begins here.
1223 This is kludged for speed, so code is repeated. */
252b5132
RH
1224 isname:
1225 name = --input_line_pointer;
1226 c = get_symbol_end ();
1227
1228#ifdef md_parse_name
1229 /* This is a hook for the backend to parse certain names
1230 specially in certain contexts. If a name always has a
1231 specific value, it can often be handled by simply
1232 entering it in the symbol table. */
1233 if (md_parse_name (name, expressionP))
1234 {
1235 *input_line_pointer = c;
1236 break;
1237 }
1238#endif
1239
1240#ifdef TC_I960
1241 /* The MRI i960 assembler permits
1242 lda sizeof code,g13
1243 FIXME: This should use md_parse_name. */
1244 if (flag_mri
1245 && (strcasecmp (name, "sizeof") == 0
1246 || strcasecmp (name, "startof") == 0))
1247 {
1248 int start;
1249 char *buf;
1250
1251 start = (name[1] == 't'
1252 || name[1] == 'T');
1253
1254 *input_line_pointer = c;
1255 SKIP_WHITESPACE ();
1256
1257 name = input_line_pointer;
1258 c = get_symbol_end ();
1259
1260 buf = (char *) xmalloc (strlen (name) + 10);
1261 if (start)
1262 sprintf (buf, ".startof.%s", name);
1263 else
1264 sprintf (buf, ".sizeof.%s", name);
1265 symbolP = symbol_make (buf);
1266 free (buf);
1267
1268 expressionP->X_op = O_symbol;
1269 expressionP->X_add_symbol = symbolP;
1270 expressionP->X_add_number = 0;
1271
1272 *input_line_pointer = c;
1273 SKIP_WHITESPACE ();
1274
1275 break;
929b12bc 1276 }
252b5132
RH
1277#endif
1278
1279 symbolP = symbol_find_or_make (name);
1280
1281 /* If we have an absolute symbol or a reg, then we know its
1282 value now. */
1283 segment = S_GET_SEGMENT (symbolP);
1284 if (segment == absolute_section)
1285 {
1286 expressionP->X_op = O_constant;
1287 expressionP->X_add_number = S_GET_VALUE (symbolP);
1288 }
1289 else if (segment == reg_section)
1290 {
1291 expressionP->X_op = O_register;
1292 expressionP->X_add_number = S_GET_VALUE (symbolP);
1293 }
1294 else
1295 {
1296 expressionP->X_op = O_symbol;
1297 expressionP->X_add_symbol = symbolP;
1298 expressionP->X_add_number = 0;
1299 }
1300 *input_line_pointer = c;
1301 }
1302 else
1303 {
1304 /* Let the target try to parse it. Success is indicated by changing
1305 the X_op field to something other than O_absent and pointing
927781e2 1306 input_line_pointer past the expression. If it can't parse the
252b5132
RH
1307 expression, X_op and input_line_pointer should be unchanged. */
1308 expressionP->X_op = O_absent;
1309 --input_line_pointer;
1310 md_operand (expressionP);
1311 if (expressionP->X_op == O_absent)
1312 {
1313 ++input_line_pointer;
1314 as_bad (_("Bad expression"));
1315 expressionP->X_op = O_constant;
1316 expressionP->X_add_number = 0;
1317 }
1318 }
1319 break;
1320 }
1321
929b12bc
KH
1322 /* It is more 'efficient' to clean up the expressionS when they are
1323 created. Doing it here saves lines of code. */
252b5132 1324 clean_up_expression (expressionP);
929b12bc 1325 SKIP_WHITESPACE (); /* -> 1st char after operand. */
252b5132
RH
1326 know (*input_line_pointer != ' ');
1327
1328 /* The PA port needs this information. */
1329 if (expressionP->X_add_symbol)
49309057 1330 symbol_mark_used (expressionP->X_add_symbol);
252b5132
RH
1331
1332 switch (expressionP->X_op)
1333 {
1334 default:
1335 return absolute_section;
1336 case O_symbol:
1337 return S_GET_SEGMENT (expressionP->X_add_symbol);
1338 case O_register:
1339 return reg_section;
1340 }
929b12bc 1341}
252b5132 1342\f
929b12bc
KH
1343/* Internal. Simplify a struct expression for use by expr (). */
1344
1345/* In: address of a expressionS.
1346 The X_op field of the expressionS may only take certain values.
1347 Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1348
1349 Out: expressionS may have been modified:
1350 'foo-foo' symbol references cancelled to 0, which changes X_op
1351 from O_subtract to O_constant.
1352 Unused fields zeroed to help expr (). */
252b5132
RH
1353
1354static void
1355clean_up_expression (expressionP)
1356 expressionS *expressionP;
1357{
1358 switch (expressionP->X_op)
1359 {
1360 case O_illegal:
1361 case O_absent:
1362 expressionP->X_add_number = 0;
1363 /* Fall through. */
1364 case O_big:
1365 case O_constant:
1366 case O_register:
1367 expressionP->X_add_symbol = NULL;
1368 /* Fall through. */
1369 case O_symbol:
1370 case O_uminus:
1371 case O_bit_not:
1372 expressionP->X_op_symbol = NULL;
1373 break;
1374 case O_subtract:
1375 if (expressionP->X_op_symbol == expressionP->X_add_symbol
49309057
ILT
1376 || ((symbol_get_frag (expressionP->X_op_symbol)
1377 == symbol_get_frag (expressionP->X_add_symbol))
252b5132
RH
1378 && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1379 && (S_GET_VALUE (expressionP->X_op_symbol)
1380 == S_GET_VALUE (expressionP->X_add_symbol))))
1381 {
1382 addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1383 - S_GET_VALUE (expressionP->X_op_symbol));
1384
1385 expressionP->X_op = O_constant;
1386 expressionP->X_add_symbol = NULL;
1387 expressionP->X_op_symbol = NULL;
1388 expressionP->X_add_number += diff;
1389 }
1390 break;
1391 default:
1392 break;
1393 }
1394}
1395\f
929b12bc
KH
1396/* Expression parser. */
1397
1398/* We allow an empty expression, and just assume (absolute,0) silently.
1399 Unary operators and parenthetical expressions are treated as operands.
1400 As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1401
1402 We used to do a aho/ullman shift-reduce parser, but the logic got so
1403 warped that I flushed it and wrote a recursive-descent parser instead.
1404 Now things are stable, would anybody like to write a fast parser?
1405 Most expressions are either register (which does not even reach here)
1406 or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1407 So I guess it doesn't really matter how inefficient more complex expressions
1408 are parsed.
1409
1410 After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1411 Also, we have consumed any leading or trailing spaces (operand does that)
1412 and done all intervening operators.
1413
1414 This returns the segment of the result, which will be
1415 absolute_section or the segment of a symbol. */
252b5132
RH
1416
1417#undef __
1418#define __ O_illegal
1419
b041f888
KH
1420/* Maps ASCII -> operators. */
1421static const operatorT op_encoding[256] = {
252b5132
RH
1422 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1423 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1424
1425 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1426 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1427 __, __, __, __, __, __, __, __,
1428 __, __, __, __, O_lt, __, O_gt, __,
1429 __, __, __, __, __, __, __, __,
1430 __, __, __, __, __, __, __, __,
1431 __, __, __, __, __, __, __, __,
b585bc2c
RH
1432 __, __, __,
1433#ifdef NEED_INDEX_OPERATOR
1434 O_index,
1435#else
1436 __,
1437#endif
1438 __, __, O_bit_exclusive_or, __,
252b5132
RH
1439 __, __, __, __, __, __, __, __,
1440 __, __, __, __, __, __, __, __,
1441 __, __, __, __, __, __, __, __,
1442 __, __, __, __, O_bit_inclusive_or, __, __, __,
1443
1444 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1445 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1446 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1447 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1448 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1449 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1450 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1451 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1452};
1453
929b12bc
KH
1454/* Rank Examples
1455 0 operand, (expression)
1456 1 ||
1457 2 &&
1458 3 = <> < <= >= >
1459 4 + -
1460 5 used for * / % in MRI mode
1461 6 & ^ ! |
1462 7 * / % << >>
1463 8 unary - unary ~
1464*/
e6c774b4 1465static operator_rankT op_rank[] = {
252b5132
RH
1466 0, /* O_illegal */
1467 0, /* O_absent */
1468 0, /* O_constant */
1469 0, /* O_symbol */
1470 0, /* O_symbol_rva */
1471 0, /* O_register */
1472 0, /* O_bit */
b585bc2c
RH
1473 9, /* O_uminus */
1474 9, /* O_bit_not */
1475 9, /* O_logical_not */
1476 8, /* O_multiply */
1477 8, /* O_divide */
1478 8, /* O_modulus */
1479 8, /* O_left_shift */
1480 8, /* O_right_shift */
1481 7, /* O_bit_inclusive_or */
1482 7, /* O_bit_or_not */
1483 7, /* O_bit_exclusive_or */
1484 7, /* O_bit_and */
1485 5, /* O_add */
1486 5, /* O_subtract */
1487 4, /* O_eq */
1488 4, /* O_ne */
1489 4, /* O_lt */
1490 4, /* O_le */
1491 4, /* O_ge */
1492 4, /* O_gt */
1493 3, /* O_logical_and */
1494 2, /* O_logical_or */
1495 1, /* O_index */
446a06c9
MM
1496 0, /* O_md1 */
1497 0, /* O_md2 */
1498 0, /* O_md3 */
1499 0, /* O_md4 */
1500 0, /* O_md5 */
1501 0, /* O_md6 */
1502 0, /* O_md7 */
1503 0, /* O_md8 */
dd33dc0f
MM
1504 0, /* O_md9 */
1505 0, /* O_md10 */
1506 0, /* O_md11 */
1507 0, /* O_md12 */
1508 0, /* O_md13 */
1509 0, /* O_md14 */
1510 0, /* O_md15 */
1511 0, /* O_md16 */
252b5132
RH
1512};
1513
1514/* Unfortunately, in MRI mode for the m68k, multiplication and
1515 division have lower precedence than the bit wise operators. This
1516 function sets the operator precedences correctly for the current
1517 mode. Also, MRI uses a different bit_not operator, and this fixes
1518 that as well. */
1519
1520#define STANDARD_MUL_PRECEDENCE (7)
1521#define MRI_MUL_PRECEDENCE (5)
1522
1523void
1524expr_set_precedence ()
1525{
1526 if (flag_m68k_mri)
1527 {
1528 op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1529 op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1530 op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1531 }
1532 else
1533 {
1534 op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1535 op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1536 op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1537 }
1538}
1539
1540/* Initialize the expression parser. */
1541
1542void
1543expr_begin ()
1544{
1545 expr_set_precedence ();
1546
1547 /* Verify that X_op field is wide enough. */
1548 {
1549 expressionS e;
1550 e.X_op = O_max;
1551 assert (e.X_op == O_max);
1552 }
1553}
1554\f
1555/* Return the encoding for the operator at INPUT_LINE_POINTER.
1556 Advance INPUT_LINE_POINTER to the last character in the operator
1557 (i.e., don't change it for a single character operator). */
1558
1559static inline operatorT
1560operator ()
1561{
1562 int c;
1563 operatorT ret;
1564
1565 c = *input_line_pointer & 0xff;
1566
b75c0c92
AM
1567 if (is_end_of_line[c])
1568 return O_illegal;
1569
252b5132
RH
1570 switch (c)
1571 {
1572 default:
1573 return op_encoding[c];
1574
1575 case '<':
1576 switch (input_line_pointer[1])
1577 {
1578 default:
1579 return op_encoding[c];
1580 case '<':
1581 ret = O_left_shift;
1582 break;
1583 case '>':
1584 ret = O_ne;
1585 break;
1586 case '=':
1587 ret = O_le;
1588 break;
1589 }
1590 ++input_line_pointer;
1591 return ret;
1592
1593 case '=':
1594 if (input_line_pointer[1] != '=')
1595 return op_encoding[c];
1596
1597 ++input_line_pointer;
1598 return O_eq;
1599
1600 case '>':
1601 switch (input_line_pointer[1])
1602 {
1603 default:
1604 return op_encoding[c];
1605 case '>':
1606 ret = O_right_shift;
1607 break;
1608 case '=':
1609 ret = O_ge;
1610 break;
1611 }
1612 ++input_line_pointer;
1613 return ret;
1614
1615 case '!':
1616 /* We accept !! as equivalent to ^ for MRI compatibility. */
1617 if (input_line_pointer[1] != '!')
1618 {
1619 if (flag_m68k_mri)
1620 return O_bit_inclusive_or;
1621 return op_encoding[c];
1622 }
1623 ++input_line_pointer;
1624 return O_bit_exclusive_or;
1625
1626 case '|':
1627 if (input_line_pointer[1] != '|')
1628 return op_encoding[c];
1629
1630 ++input_line_pointer;
1631 return O_logical_or;
1632
1633 case '&':
1634 if (input_line_pointer[1] != '&')
1635 return op_encoding[c];
1636
1637 ++input_line_pointer;
1638 return O_logical_and;
1639 }
1640
929b12bc 1641 /* NOTREACHED */
252b5132
RH
1642}
1643
1644/* Parse an expression. */
1645
1646segT
0561a208 1647expr (rankarg, resultP)
929b12bc
KH
1648 int rankarg; /* Larger # is higher rank. */
1649 expressionS *resultP; /* Deliver result here. */
252b5132 1650{
0561a208 1651 operator_rankT rank = (operator_rankT) rankarg;
252b5132
RH
1652 segT retval;
1653 expressionS right;
1654 operatorT op_left;
1655 operatorT op_right;
1656
1657 know (rank >= 0);
1658
1659 retval = operand (resultP);
1660
929b12bc
KH
1661 /* operand () gobbles spaces. */
1662 know (*input_line_pointer != ' ');
252b5132
RH
1663
1664 op_left = operator ();
1665 while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1666 {
1667 segT rightseg;
1668
929b12bc 1669 input_line_pointer++; /* -> after 1st character of operator. */
252b5132
RH
1670
1671 rightseg = expr (op_rank[(int) op_left], &right);
1672 if (right.X_op == O_absent)
1673 {
1674 as_warn (_("missing operand; zero assumed"));
1675 right.X_op = O_constant;
1676 right.X_add_number = 0;
1677 right.X_add_symbol = NULL;
1678 right.X_op_symbol = NULL;
1679 }
1680
1681 know (*input_line_pointer != ' ');
1682
b585bc2c
RH
1683 if (op_left == O_index)
1684 {
1685 if (*input_line_pointer != ']')
1686 as_bad ("missing right bracket");
1687 else
1688 {
1689 ++input_line_pointer;
1690 SKIP_WHITESPACE ();
1691 }
1692 }
1693
252b5132
RH
1694 if (retval == undefined_section)
1695 {
1696 if (SEG_NORMAL (rightseg))
1697 retval = rightseg;
1698 }
1699 else if (! SEG_NORMAL (retval))
1700 retval = rightseg;
1701 else if (SEG_NORMAL (rightseg)
1702 && retval != rightseg
1703#ifdef DIFF_EXPR_OK
1704 && op_left != O_subtract
1705#endif
1706 )
1707 as_bad (_("operation combines symbols in different segments"));
1708
1709 op_right = operator ();
1710
929b12bc
KH
1711 know (op_right == O_illegal
1712 || op_rank[(int) op_right] <= op_rank[(int) op_left]);
252b5132
RH
1713 know ((int) op_left >= (int) O_multiply
1714 && (int) op_left <= (int) O_logical_or);
1715
929b12bc
KH
1716 /* input_line_pointer->after right-hand quantity. */
1717 /* left-hand quantity in resultP. */
1718 /* right-hand quantity in right. */
1719 /* operator in op_left. */
252b5132
RH
1720
1721 if (resultP->X_op == O_big)
1722 {
1723 if (resultP->X_add_number > 0)
1724 as_warn (_("left operand is a bignum; integer 0 assumed"));
1725 else
1726 as_warn (_("left operand is a float; integer 0 assumed"));
1727 resultP->X_op = O_constant;
1728 resultP->X_add_number = 0;
1729 resultP->X_add_symbol = NULL;
1730 resultP->X_op_symbol = NULL;
1731 }
1732 if (right.X_op == O_big)
1733 {
1734 if (right.X_add_number > 0)
1735 as_warn (_("right operand is a bignum; integer 0 assumed"));
1736 else
1737 as_warn (_("right operand is a float; integer 0 assumed"));
1738 right.X_op = O_constant;
1739 right.X_add_number = 0;
1740 right.X_add_symbol = NULL;
1741 right.X_op_symbol = NULL;
1742 }
1743
1744 /* Optimize common cases. */
800eeca4
JW
1745#ifdef md_optimize_expr
1746 if (md_optimize_expr (resultP, op_left, &right))
1747 {
929b12bc
KH
1748 /* Skip. */
1749 ;
800eeca4
JW
1750 }
1751 else
1752#endif
252b5132
RH
1753 if (op_left == O_add && right.X_op == O_constant)
1754 {
1755 /* X + constant. */
1756 resultP->X_add_number += right.X_add_number;
1757 }
1758 /* This case comes up in PIC code. */
1759 else if (op_left == O_subtract
1760 && right.X_op == O_symbol
1761 && resultP->X_op == O_symbol
49309057
ILT
1762 && (symbol_get_frag (right.X_add_symbol)
1763 == symbol_get_frag (resultP->X_add_symbol))
252b5132
RH
1764 && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1765
1766 {
1767 resultP->X_add_number -= right.X_add_number;
1768 resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1769 - S_GET_VALUE (right.X_add_symbol));
1770 resultP->X_op = O_constant;
1771 resultP->X_add_symbol = 0;
1772 }
1773 else if (op_left == O_subtract && right.X_op == O_constant)
1774 {
1775 /* X - constant. */
1776 resultP->X_add_number -= right.X_add_number;
1777 }
1778 else if (op_left == O_add && resultP->X_op == O_constant)
1779 {
1780 /* Constant + X. */
1781 resultP->X_op = right.X_op;
1782 resultP->X_add_symbol = right.X_add_symbol;
1783 resultP->X_op_symbol = right.X_op_symbol;
1784 resultP->X_add_number += right.X_add_number;
1785 retval = rightseg;
1786 }
1787 else if (resultP->X_op == O_constant && right.X_op == O_constant)
1788 {
1789 /* Constant OP constant. */
1790 offsetT v = right.X_add_number;
1791 if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1792 {
1793 as_warn (_("division by zero"));
1794 v = 1;
1795 }
1796 switch (op_left)
1797 {
1798 default: abort ();
1799 case O_multiply: resultP->X_add_number *= v; break;
1800 case O_divide: resultP->X_add_number /= v; break;
1801 case O_modulus: resultP->X_add_number %= v; break;
1802 case O_left_shift: resultP->X_add_number <<= v; break;
1803 case O_right_shift:
1804 /* We always use unsigned shifts, to avoid relying on
1805 characteristics of the compiler used to compile gas. */
1806 resultP->X_add_number =
1807 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1808 break;
1809 case O_bit_inclusive_or: resultP->X_add_number |= v; break;
1810 case O_bit_or_not: resultP->X_add_number |= ~v; break;
1811 case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
1812 case O_bit_and: resultP->X_add_number &= v; break;
1813 case O_add: resultP->X_add_number += v; break;
1814 case O_subtract: resultP->X_add_number -= v; break;
1815 case O_eq:
1816 resultP->X_add_number =
958b5f01 1817 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
252b5132
RH
1818 break;
1819 case O_ne:
1820 resultP->X_add_number =
958b5f01 1821 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
252b5132
RH
1822 break;
1823 case O_lt:
1824 resultP->X_add_number =
958b5f01 1825 resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
252b5132
RH
1826 break;
1827 case O_le:
1828 resultP->X_add_number =
958b5f01 1829 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
252b5132
RH
1830 break;
1831 case O_ge:
1832 resultP->X_add_number =
958b5f01 1833 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
252b5132
RH
1834 break;
1835 case O_gt:
1836 resultP->X_add_number =
958b5f01 1837 resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
252b5132
RH
1838 break;
1839 case O_logical_and:
1840 resultP->X_add_number = resultP->X_add_number && v;
1841 break;
1842 case O_logical_or:
1843 resultP->X_add_number = resultP->X_add_number || v;
1844 break;
1845 }
1846 }
1847 else if (resultP->X_op == O_symbol
1848 && right.X_op == O_symbol
1849 && (op_left == O_add
1850 || op_left == O_subtract
1851 || (resultP->X_add_number == 0
1852 && right.X_add_number == 0)))
1853 {
1854 /* Symbol OP symbol. */
1855 resultP->X_op = op_left;
1856 resultP->X_op_symbol = right.X_add_symbol;
1857 if (op_left == O_add)
1858 resultP->X_add_number += right.X_add_number;
1859 else if (op_left == O_subtract)
1860 resultP->X_add_number -= right.X_add_number;
1861 }
1862 else
1863 {
1864 /* The general case. */
1865 resultP->X_add_symbol = make_expr_symbol (resultP);
1866 resultP->X_op_symbol = make_expr_symbol (&right);
1867 resultP->X_op = op_left;
1868 resultP->X_add_number = 0;
1869 resultP->X_unsigned = 1;
1870 }
1871
1872 op_left = op_right;
929b12bc 1873 } /* While next operator is >= this rank. */
252b5132
RH
1874
1875 /* The PA port needs this information. */
1876 if (resultP->X_add_symbol)
49309057 1877 symbol_mark_used (resultP->X_add_symbol);
252b5132
RH
1878
1879 return resultP->X_op == O_constant ? absolute_section : retval;
1880}
1881\f
929b12bc
KH
1882/* This lives here because it belongs equally in expr.c & read.c.
1883 expr.c is just a branch office read.c anyway, and putting it
1884 here lessens the crowd at read.c.
1885
1886 Assume input_line_pointer is at start of symbol name.
1887 Advance input_line_pointer past symbol name.
1888 Turn that character into a '\0', returning its former value.
1889 This allows a string compare (RMS wants symbol names to be strings)
1890 of the symbol name.
1891 There will always be a char following symbol name, because all good
1892 lines end in end-of-line. */
1893
252b5132
RH
1894char
1895get_symbol_end ()
1896{
1897 char c;
1898
1899 /* We accept \001 in a name in case this is being called with a
1900 constructed string. */
1901 if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
58b5739a
RH
1902 {
1903 while (is_part_of_name (c = *input_line_pointer++)
1904 || c == '\001')
1905 ;
1906 if (is_name_ender (c))
1907 c = *input_line_pointer++;
1908 }
252b5132
RH
1909 *--input_line_pointer = 0;
1910 return (c);
1911}
1912
252b5132
RH
1913unsigned int
1914get_single_number ()
1915{
1916 expressionS exp;
1917 operand (&exp);
1918 return exp.X_add_number;
252b5132 1919}
This page took 0.137545 seconds and 4 git commands to generate.