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