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