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