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