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