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