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