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