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