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