arc/nps400 : New cmem instructions and associated relocation
[external/binutils.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2    Copyright (C) 1987-2016 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
38 static void floating_constant (expressionS * expressionP);
39 static valueT generic_bignum_to_int32 (void);
40 #ifdef BFD64
41 static valueT generic_bignum_to_int64 (void);
42 #endif
43 static void integer_constant (int radix, expressionS * expressionP);
44 static void mri_char_constant (expressionS *);
45 static void clean_up_expression (expressionS * expressionP);
46 static segT operand (expressionS *, enum expr_mode);
47 static 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
52 struct expr_symbol_line {
53   struct expr_symbol_line *next;
54   symbolS *sym;
55   const char *file;
56   unsigned int line;
57 };
58
59 static struct expr_symbol_line *expr_symbol_lines;
60 \f
61 /* Build a dummy symbol to hold a complex expression.  This is how we
62    build expressions up out of other expressions.  The symbol is put
63    into the fake section expr_section.  */
64
65 symbolS *
66 make_expr_symbol (expressionS *expressionP)
67 {
68   expressionS zero;
69   symbolS *symbolP;
70   struct expr_symbol_line *n;
71
72   if (expressionP->X_op == O_symbol
73       && expressionP->X_add_number == 0)
74     return expressionP->X_add_symbol;
75
76   if (expressionP->X_op == O_big)
77     {
78       /* This won't work, because the actual value is stored in
79          generic_floating_point_number or generic_bignum, and we are
80          going to lose it if we haven't already.  */
81       if (expressionP->X_add_number > 0)
82         as_bad (_("bignum invalid"));
83       else
84         as_bad (_("floating point number invalid"));
85       zero.X_op = O_constant;
86       zero.X_add_number = 0;
87       zero.X_unsigned = 0;
88       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
122 int
123 expr_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 \f
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
152 symbolS *
153 expr_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
166 symbolS *
167 expr_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 \f
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.  */
183 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
184
185 FLONUM_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 \f
194 static void
195 floating_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
221 static valueT
222 generic_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
232 static valueT
233 generic_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
247 static void
248 integer_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   if (small)
519     {
520       /* Here with number, in correct radix. c is the next char.
521          Note that unlike un*x, we allow "011f" "0x9f" to both mean
522          the same as the (conventional) "9f".
523          This is simply easier than checking for strict canonical
524          form.  Syntax sux!  */
525
526       if (LOCAL_LABELS_FB && c == 'b')
527         {
528           /* Backward ref to local label.
529              Because it is backward, expect it to be defined.  */
530           /* Construct a local label.  */
531           name = fb_label_name ((int) number, 0);
532
533           /* Seen before, or symbol is defined: OK.  */
534           symbolP = symbol_find (name);
535           if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
536             {
537               /* Local labels are never absolute.  Don't waste time
538                  checking absoluteness.  */
539               know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
540
541               expressionP->X_op = O_symbol;
542               expressionP->X_add_symbol = symbolP;
543             }
544           else
545             {
546               /* Either not seen or not defined.  */
547               /* @@ Should print out the original string instead of
548                  the parsed number.  */
549               as_bad (_("backward ref to unknown label \"%d:\""),
550                       (int) number);
551               expressionP->X_op = O_constant;
552             }
553
554           expressionP->X_add_number = 0;
555         }                       /* case 'b' */
556       else if (LOCAL_LABELS_FB && c == 'f')
557         {
558           /* Forward reference.  Expect symbol to be undefined or
559              unknown.  undefined: seen it before.  unknown: never seen
560              it before.
561
562              Construct a local label name, then an undefined symbol.
563              Don't create a xseg frag for it: caller may do that.
564              Just return it as never seen before.  */
565           name = fb_label_name ((int) number, 1);
566           symbolP = symbol_find_or_make (name);
567           /* We have no need to check symbol properties.  */
568 #ifndef many_segments
569           /* Since "know" puts its arg into a "string", we
570              can't have newlines in the argument.  */
571           know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
572 #endif
573           expressionP->X_op = O_symbol;
574           expressionP->X_add_symbol = symbolP;
575           expressionP->X_add_number = 0;
576         }                       /* case 'f' */
577       else if (LOCAL_LABELS_DOLLAR && c == '$')
578         {
579           /* If the dollar label is *currently* defined, then this is just
580              another reference to it.  If it is not *currently* defined,
581              then this is a fresh instantiation of that number, so create
582              it.  */
583
584           if (dollar_label_defined ((long) number))
585             {
586               name = dollar_label_name ((long) number, 0);
587               symbolP = symbol_find (name);
588               know (symbolP != NULL);
589             }
590           else
591             {
592               name = dollar_label_name ((long) number, 1);
593               symbolP = symbol_find_or_make (name);
594             }
595
596           expressionP->X_op = O_symbol;
597           expressionP->X_add_symbol = symbolP;
598           expressionP->X_add_number = 0;
599         }                       /* case '$' */
600       else
601         {
602           expressionP->X_op = O_constant;
603           expressionP->X_add_number = number;
604           input_line_pointer--; /* Restore following character.  */
605         }                       /* Really just a number.  */
606     }
607   else
608     {
609       /* Not a small number.  */
610       expressionP->X_op = O_big;
611       expressionP->X_add_number = number;       /* Number of littlenums.  */
612       input_line_pointer--;     /* -> char following number.  */
613     }
614 }
615
616 /* Parse an MRI multi character constant.  */
617
618 static void
619 mri_char_constant (expressionS *expressionP)
620 {
621   int i;
622
623   if (*input_line_pointer == '\''
624       && input_line_pointer[1] != '\'')
625     {
626       expressionP->X_op = O_constant;
627       expressionP->X_add_number = 0;
628       return;
629     }
630
631   /* In order to get the correct byte ordering, we must build the
632      number in reverse.  */
633   for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
634     {
635       int j;
636
637       generic_bignum[i] = 0;
638       for (j = 0; j < CHARS_PER_LITTLENUM; j++)
639         {
640           if (*input_line_pointer == '\'')
641             {
642               if (input_line_pointer[1] != '\'')
643                 break;
644               ++input_line_pointer;
645             }
646           generic_bignum[i] <<= 8;
647           generic_bignum[i] += *input_line_pointer;
648           ++input_line_pointer;
649         }
650
651       if (i < SIZE_OF_LARGE_NUMBER - 1)
652         {
653           /* If there is more than one littlenum, left justify the
654              last one to make it match the earlier ones.  If there is
655              only one, we can just use the value directly.  */
656           for (; j < CHARS_PER_LITTLENUM; j++)
657             generic_bignum[i] <<= 8;
658         }
659
660       if (*input_line_pointer == '\''
661           && input_line_pointer[1] != '\'')
662         break;
663     }
664
665   if (i < 0)
666     {
667       as_bad (_("character constant too large"));
668       i = 0;
669     }
670
671   if (i > 0)
672     {
673       int c;
674       int j;
675
676       c = SIZE_OF_LARGE_NUMBER - i;
677       for (j = 0; j < c; j++)
678         generic_bignum[j] = generic_bignum[i + j];
679       i = c;
680     }
681
682   know (LITTLENUM_NUMBER_OF_BITS == 16);
683   if (i > 2)
684     {
685       expressionP->X_op = O_big;
686       expressionP->X_add_number = i;
687     }
688   else
689     {
690       expressionP->X_op = O_constant;
691       if (i < 2)
692         expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
693       else
694         expressionP->X_add_number =
695           (((generic_bignum[1] & LITTLENUM_MASK)
696             << LITTLENUM_NUMBER_OF_BITS)
697            | (generic_bignum[0] & LITTLENUM_MASK));
698     }
699
700   /* Skip the final closing quote.  */
701   ++input_line_pointer;
702 }
703
704 /* Return an expression representing the current location.  This
705    handles the magic symbol `.'.  */
706
707 void
708 current_location (expressionS *expressionp)
709 {
710   if (now_seg == absolute_section)
711     {
712       expressionp->X_op = O_constant;
713       expressionp->X_add_number = abs_section_offset;
714     }
715   else
716     {
717       expressionp->X_op = O_symbol;
718       expressionp->X_add_symbol = &dot_symbol;
719       expressionp->X_add_number = 0;
720     }
721 }
722
723 /* In:  Input_line_pointer points to 1st char of operand, which may
724         be a space.
725
726    Out: An expressionS.
727         The operand may have been empty: in this case X_op == O_absent.
728         Input_line_pointer->(next non-blank) char after operand.  */
729
730 static segT
731 operand (expressionS *expressionP, enum expr_mode mode)
732 {
733   char c;
734   symbolS *symbolP;     /* Points to symbol.  */
735   char *name;           /* Points to name of symbol.  */
736   segT segment;
737
738   /* All integers are regarded as unsigned unless they are negated.
739      This is because the only thing which cares whether a number is
740      unsigned is the code in emit_expr which extends constants into
741      bignums.  It should only sign extend negative numbers, so that
742      something like ``.quad 0x80000000'' is not sign extended even
743      though it appears negative if valueT is 32 bits.  */
744   expressionP->X_unsigned = 1;
745   expressionP->X_extrabit = 0;
746
747   /* Digits, assume it is a bignum.  */
748
749   SKIP_WHITESPACE ();           /* Leading whitespace is part of operand.  */
750   c = *input_line_pointer++;    /* input_line_pointer -> past char in c.  */
751
752   if (is_end_of_line[(unsigned char) c])
753     goto eol;
754
755   switch (c)
756     {
757     case '1':
758     case '2':
759     case '3':
760     case '4':
761     case '5':
762     case '6':
763     case '7':
764     case '8':
765     case '9':
766       input_line_pointer--;
767
768       integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
769                         ? 0 : 10,
770                         expressionP);
771       break;
772
773 #ifdef LITERAL_PREFIXDOLLAR_HEX
774     case '$':
775       /* $L is the start of a local label, not a hex constant.  */
776       if (* input_line_pointer == 'L')
777       goto isname;
778       integer_constant (16, expressionP);
779       break;
780 #endif
781
782 #ifdef LITERAL_PREFIXPERCENT_BIN
783     case '%':
784       integer_constant (2, expressionP);
785       break;
786 #endif
787
788     case '0':
789       /* Non-decimal radix.  */
790
791       if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
792         {
793           char *s;
794
795           /* Check for a hex or float constant.  */
796           for (s = input_line_pointer; hex_p (*s); s++)
797             ;
798           if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
799             {
800               --input_line_pointer;
801               integer_constant (0, expressionP);
802               break;
803             }
804         }
805       c = *input_line_pointer;
806       switch (c)
807         {
808         case 'o':
809         case 'O':
810         case 'q':
811         case 'Q':
812         case '8':
813         case '9':
814           if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
815             {
816               integer_constant (0, expressionP);
817               break;
818             }
819           /* Fall through.  */
820         default:
821         default_case:
822           if (c && strchr (FLT_CHARS, c))
823             {
824               input_line_pointer++;
825               floating_constant (expressionP);
826               expressionP->X_add_number = - TOLOWER (c);
827             }
828           else
829             {
830               /* The string was only zero.  */
831               expressionP->X_op = O_constant;
832               expressionP->X_add_number = 0;
833             }
834
835           break;
836
837         case 'x':
838         case 'X':
839           if (flag_m68k_mri)
840             goto default_case;
841           input_line_pointer++;
842           integer_constant (16, expressionP);
843           break;
844
845         case 'b':
846           if (LOCAL_LABELS_FB && !flag_m68k_mri
847               && input_line_pointer[1] != '0'
848               && input_line_pointer[1] != '1')
849             {
850               /* Parse this as a back reference to label 0.  */
851               input_line_pointer--;
852               integer_constant (10, expressionP);
853               break;
854             }
855           /* Otherwise, parse this as a binary number.  */
856           /* Fall through.  */
857         case 'B':
858           if (input_line_pointer[1] == '0'
859               || input_line_pointer[1] == '1')
860             {
861               input_line_pointer++;
862               integer_constant (2, expressionP);
863               break;
864             }
865           if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
866             input_line_pointer++;
867           goto default_case;
868
869         case '0':
870         case '1':
871         case '2':
872         case '3':
873         case '4':
874         case '5':
875         case '6':
876         case '7':
877           integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
878                             ? 0 : 8,
879                             expressionP);
880           break;
881
882         case 'f':
883           if (LOCAL_LABELS_FB)
884             {
885               int is_label = 1;
886
887               /* If it says "0f" and it could possibly be a floating point
888                  number, make it one.  Otherwise, make it a local label,
889                  and try to deal with parsing the rest later.  */
890               if (!is_end_of_line[(unsigned char) input_line_pointer[1]]
891                   && strchr (FLT_CHARS, 'f') != NULL)
892                 {
893                   char *cp = input_line_pointer + 1;
894
895                   atof_generic (&cp, ".", EXP_CHARS,
896                                 &generic_floating_point_number);
897
898                   /* Was nothing parsed, or does it look like an
899                      expression?  */
900                   is_label = (cp == input_line_pointer + 1
901                               || (cp == input_line_pointer + 2
902                                   && (cp[-1] == '-' || cp[-1] == '+'))
903                               || *cp == 'f'
904                               || *cp == 'b');
905                 }
906               if (is_label)
907                 {
908                   input_line_pointer--;
909                   integer_constant (10, expressionP);
910                   break;
911                 }
912             }
913           /* Fall through.  */
914
915         case 'd':
916         case 'D':
917           if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
918             {
919               integer_constant (0, expressionP);
920               break;
921             }
922           /* Fall through.  */
923         case 'F':
924         case 'r':
925         case 'e':
926         case 'E':
927         case 'g':
928         case 'G':
929           input_line_pointer++;
930           floating_constant (expressionP);
931           expressionP->X_add_number = - TOLOWER (c);
932           break;
933
934         case '$':
935           if (LOCAL_LABELS_DOLLAR)
936             {
937               integer_constant (10, expressionP);
938               break;
939             }
940           else
941             goto default_case;
942         }
943
944       break;
945
946 #ifndef NEED_INDEX_OPERATOR
947     case '[':
948 # ifdef md_need_index_operator
949       if (md_need_index_operator())
950         goto de_fault;
951 # endif
952       /* FALLTHROUGH */
953 #endif
954     case '(':
955       /* Didn't begin with digit & not a name.  */
956       segment = expr (0, expressionP, mode);
957       /* expression () will pass trailing whitespace.  */
958       if ((c == '(' && *input_line_pointer != ')')
959           || (c == '[' && *input_line_pointer != ']'))
960         {
961           if (* input_line_pointer)
962             as_bad (_("found '%c', expected: '%c'"),
963                     * input_line_pointer, c == '(' ? ')' : ']');
964           else
965             as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
966         }           
967       else
968         input_line_pointer++;
969       SKIP_WHITESPACE ();
970       /* Here with input_line_pointer -> char after "(...)".  */
971       return segment;
972
973 #ifdef TC_M68K
974     case 'E':
975       if (! flag_m68k_mri || *input_line_pointer != '\'')
976         goto de_fault;
977       as_bad (_("EBCDIC constants are not supported"));
978       /* Fall through.  */
979     case 'A':
980       if (! flag_m68k_mri || *input_line_pointer != '\'')
981         goto de_fault;
982       ++input_line_pointer;
983       /* Fall through.  */
984 #endif
985     case '\'':
986       if (! flag_m68k_mri)
987         {
988           /* Warning: to conform to other people's assemblers NO
989              ESCAPEMENT is permitted for a single quote.  The next
990              character, parity errors and all, is taken as the value
991              of the operand.  VERY KINKY.  */
992           expressionP->X_op = O_constant;
993           expressionP->X_add_number = *input_line_pointer++;
994           break;
995         }
996
997       mri_char_constant (expressionP);
998       break;
999
1000 #ifdef TC_M68K
1001     case '"':
1002       /* Double quote is the bitwise not operator in MRI mode.  */
1003       if (! flag_m68k_mri)
1004         goto de_fault;
1005       /* Fall through.  */
1006 #endif
1007     case '~':
1008       /* '~' is permitted to start a label on the Delta.  */
1009       if (is_name_beginner (c))
1010         goto isname;
1011     case '!':
1012     case '-':
1013     case '+':
1014       {
1015 #ifdef md_operator
1016       unary:
1017 #endif
1018         operand (expressionP, mode);
1019         if (expressionP->X_op == O_constant)
1020           {
1021             /* input_line_pointer -> char after operand.  */
1022             if (c == '-')
1023               {
1024                 expressionP->X_add_number
1025                   = - (addressT) expressionP->X_add_number;
1026                 /* Notice: '-' may overflow: no warning is given.
1027                    This is compatible with other people's
1028                    assemblers.  Sigh.  */
1029                 expressionP->X_unsigned = 0;
1030                 if (expressionP->X_add_number)
1031                   expressionP->X_extrabit ^= 1;
1032               }
1033             else if (c == '~' || c == '"')
1034               expressionP->X_add_number = ~ expressionP->X_add_number;
1035             else if (c == '!')
1036               expressionP->X_add_number = ! expressionP->X_add_number;
1037           }
1038         else if (expressionP->X_op == O_big
1039                  && expressionP->X_add_number <= 0
1040                  && c == '-'
1041                  && (generic_floating_point_number.sign == '+'
1042                      || generic_floating_point_number.sign == 'P'))
1043           {
1044             /* Negative flonum (eg, -1.000e0).  */
1045             if (generic_floating_point_number.sign == '+')
1046               generic_floating_point_number.sign = '-';
1047             else
1048               generic_floating_point_number.sign = 'N';
1049           }
1050         else if (expressionP->X_op == O_big
1051                  && expressionP->X_add_number > 0)
1052           {
1053             int i;
1054
1055             if (c == '~' || c == '-')
1056               {
1057                 for (i = 0; i < expressionP->X_add_number; ++i)
1058                   generic_bignum[i] = ~generic_bignum[i];
1059
1060                 /* Extend the bignum to at least the size of .octa.  */
1061                 if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1062                   {
1063                     expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1064                     for (; i < expressionP->X_add_number; ++i)
1065                       generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1066                   }
1067
1068                 if (c == '-')
1069                   for (i = 0; i < expressionP->X_add_number; ++i)
1070                     {
1071                       generic_bignum[i] += 1;
1072                       if (generic_bignum[i])
1073                         break;
1074                     }
1075               }
1076             else if (c == '!')
1077               {
1078                 for (i = 0; i < expressionP->X_add_number; ++i)
1079                   if (generic_bignum[i] != 0)
1080                     break;
1081                 expressionP->X_add_number = i >= expressionP->X_add_number;
1082                 expressionP->X_op = O_constant;
1083                 expressionP->X_unsigned = 1;
1084                 expressionP->X_extrabit = 0;
1085               }
1086           }
1087         else if (expressionP->X_op != O_illegal
1088                  && expressionP->X_op != O_absent)
1089           {
1090             if (c != '+')
1091               {
1092                 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1093                 if (c == '-')
1094                   expressionP->X_op = O_uminus;
1095                 else if (c == '~' || c == '"')
1096                   expressionP->X_op = O_bit_not;
1097                 else
1098                   expressionP->X_op = O_logical_not;
1099                 expressionP->X_add_number = 0;
1100               }
1101           }
1102         else
1103           as_warn (_("Unary operator %c ignored because bad operand follows"),
1104                    c);
1105       }
1106       break;
1107
1108 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1109     case '$':
1110       /* '$' is the program counter when in MRI mode, or when
1111          DOLLAR_DOT is defined.  */
1112 #ifndef DOLLAR_DOT
1113       if (! flag_m68k_mri)
1114         goto de_fault;
1115 #endif
1116       if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1117         {
1118           /* In MRI mode and on Z80, '$' is also used as the prefix
1119              for a hexadecimal constant.  */
1120           integer_constant (16, expressionP);
1121           break;
1122         }
1123
1124       if (is_part_of_name (*input_line_pointer))
1125         goto isname;
1126
1127       current_location (expressionP);
1128       break;
1129 #endif
1130
1131     case '.':
1132       if (!is_part_of_name (*input_line_pointer))
1133         {
1134           current_location (expressionP);
1135           break;
1136         }
1137       else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1138                 && ! is_part_of_name (input_line_pointer[8]))
1139                || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1140                    && ! is_part_of_name (input_line_pointer[7])))
1141         {
1142           int start;
1143
1144           start = (input_line_pointer[1] == 't'
1145                    || input_line_pointer[1] == 'T');
1146           input_line_pointer += start ? 8 : 7;
1147           SKIP_WHITESPACE ();
1148           if (*input_line_pointer != '(')
1149             as_bad (_("syntax error in .startof. or .sizeof."));
1150           else
1151             {
1152               char *buf;
1153
1154               ++input_line_pointer;
1155               SKIP_WHITESPACE ();
1156               c = get_symbol_name (& name);
1157
1158               buf = (char *) xmalloc (strlen (name) + 10);
1159               if (start)
1160                 sprintf (buf, ".startof.%s", name);
1161               else
1162                 sprintf (buf, ".sizeof.%s", name);
1163               symbolP = symbol_make (buf);
1164               free (buf);
1165
1166               expressionP->X_op = O_symbol;
1167               expressionP->X_add_symbol = symbolP;
1168               expressionP->X_add_number = 0;
1169
1170               *input_line_pointer = c;
1171               SKIP_WHITESPACE_AFTER_NAME ();
1172               if (*input_line_pointer != ')')
1173                 as_bad (_("syntax error in .startof. or .sizeof."));
1174               else
1175                 ++input_line_pointer;
1176             }
1177           break;
1178         }
1179       else
1180         {
1181           goto isname;
1182         }
1183
1184     case ',':
1185     eol:
1186       /* Can't imagine any other kind of operand.  */
1187       expressionP->X_op = O_absent;
1188       input_line_pointer--;
1189       break;
1190
1191 #ifdef TC_M68K
1192     case '%':
1193       if (! flag_m68k_mri)
1194         goto de_fault;
1195       integer_constant (2, expressionP);
1196       break;
1197
1198     case '@':
1199       if (! flag_m68k_mri)
1200         goto de_fault;
1201       integer_constant (8, expressionP);
1202       break;
1203
1204     case ':':
1205       if (! flag_m68k_mri)
1206         goto de_fault;
1207
1208       /* In MRI mode, this is a floating point constant represented
1209          using hexadecimal digits.  */
1210
1211       ++input_line_pointer;
1212       integer_constant (16, expressionP);
1213       break;
1214
1215     case '*':
1216       if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1217         goto de_fault;
1218
1219       current_location (expressionP);
1220       break;
1221 #endif
1222
1223     default:
1224 #if defined(md_need_index_operator) || defined(TC_M68K)
1225     de_fault:
1226 #endif
1227       if (is_name_beginner (c) || c == '"')     /* Here if did not begin with a digit.  */
1228         {
1229           /* Identifier begins here.
1230              This is kludged for speed, so code is repeated.  */
1231         isname:
1232           -- input_line_pointer;
1233           c = get_symbol_name (&name);
1234
1235 #ifdef md_operator
1236           {
1237             operatorT op = md_operator (name, 1, &c);
1238
1239             switch (op)
1240               {
1241               case O_uminus:
1242                 restore_line_pointer (c);
1243                 c = '-';
1244                 goto unary;
1245               case O_bit_not:
1246                 restore_line_pointer (c);
1247                 c = '~';
1248                 goto unary;
1249               case O_logical_not:
1250                 restore_line_pointer (c);
1251                 c = '!';
1252                 goto unary;
1253               case O_illegal:
1254                 as_bad (_("invalid use of operator \"%s\""), name);
1255                 break;
1256               default:
1257                 break;
1258               }
1259
1260             if (op != O_absent && op != O_illegal)
1261               {
1262                 restore_line_pointer (c);
1263                 expr (9, expressionP, mode);
1264                 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1265                 expressionP->X_op_symbol = NULL;
1266                 expressionP->X_add_number = 0;
1267                 expressionP->X_op = op;
1268                 break;
1269               }
1270           }
1271 #endif
1272
1273 #ifdef md_parse_name
1274           /* This is a hook for the backend to parse certain names
1275              specially in certain contexts.  If a name always has a
1276              specific value, it can often be handled by simply
1277              entering it in the symbol table.  */
1278           if (md_parse_name (name, expressionP, mode, &c))
1279             {
1280               restore_line_pointer (c);
1281               break;
1282             }
1283 #endif
1284
1285 #ifdef TC_I960
1286           /* The MRI i960 assembler permits
1287                  lda sizeof code,g13
1288              FIXME: This should use md_parse_name.  */
1289           if (flag_mri
1290               && (strcasecmp (name, "sizeof") == 0
1291                   || strcasecmp (name, "startof") == 0))
1292             {
1293               int start;
1294               char *buf;
1295
1296               start = (name[1] == 't'
1297                        || name[1] == 'T');
1298
1299               *input_line_pointer = c;
1300               SKIP_WHITESPACE_AFTER_NAME ();
1301
1302               c = get_symbol_name (& name);
1303
1304               buf = (char *) xmalloc (strlen (name) + 10);
1305               if (start)
1306                 sprintf (buf, ".startof.%s", name);
1307               else
1308                 sprintf (buf, ".sizeof.%s", name);
1309               symbolP = symbol_make (buf);
1310               free (buf);
1311
1312               expressionP->X_op = O_symbol;
1313               expressionP->X_add_symbol = symbolP;
1314               expressionP->X_add_number = 0;
1315
1316               *input_line_pointer = c;
1317               SKIP_WHITESPACE_AFTER_NAME ();
1318               break;
1319             }
1320 #endif
1321
1322           symbolP = symbol_find_or_make (name);
1323
1324           /* If we have an absolute symbol or a reg, then we know its
1325              value now.  */
1326           segment = S_GET_SEGMENT (symbolP);
1327           if (mode != expr_defer
1328               && segment == absolute_section
1329               && !S_FORCE_RELOC (symbolP, 0))
1330             {
1331               expressionP->X_op = O_constant;
1332               expressionP->X_add_number = S_GET_VALUE (symbolP);
1333             }
1334           else if (mode != expr_defer && segment == reg_section)
1335             {
1336               expressionP->X_op = O_register;
1337               expressionP->X_add_number = S_GET_VALUE (symbolP);
1338             }
1339           else
1340             {
1341               expressionP->X_op = O_symbol;
1342               expressionP->X_add_symbol = symbolP;
1343               expressionP->X_add_number = 0;
1344             }
1345
1346           restore_line_pointer (c);
1347         }
1348       else
1349         {
1350           /* Let the target try to parse it.  Success is indicated by changing
1351              the X_op field to something other than O_absent and pointing
1352              input_line_pointer past the expression.  If it can't parse the
1353              expression, X_op and input_line_pointer should be unchanged.  */
1354           expressionP->X_op = O_absent;
1355           --input_line_pointer;
1356           md_operand (expressionP);
1357           if (expressionP->X_op == O_absent)
1358             {
1359               ++input_line_pointer;
1360               as_bad (_("bad expression"));
1361               expressionP->X_op = O_constant;
1362               expressionP->X_add_number = 0;
1363             }
1364         }
1365       break;
1366     }
1367
1368   /* It is more 'efficient' to clean up the expressionS when they are
1369      created.  Doing it here saves lines of code.  */
1370   clean_up_expression (expressionP);
1371   SKIP_WHITESPACE ();           /* -> 1st char after operand.  */
1372   know (*input_line_pointer != ' ');
1373
1374   /* The PA port needs this information.  */
1375   if (expressionP->X_add_symbol)
1376     symbol_mark_used (expressionP->X_add_symbol);
1377
1378   if (mode != expr_defer)
1379     {
1380       expressionP->X_add_symbol
1381         = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1382       expressionP->X_op_symbol
1383         = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1384     }
1385
1386   switch (expressionP->X_op)
1387     {
1388     default:
1389       return absolute_section;
1390     case O_symbol:
1391       return S_GET_SEGMENT (expressionP->X_add_symbol);
1392     case O_register:
1393       return reg_section;
1394     }
1395 }
1396 \f
1397 /* Internal.  Simplify a struct expression for use by expr ().  */
1398
1399 /* In:  address of an expressionS.
1400         The X_op field of the expressionS may only take certain values.
1401         Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1402
1403    Out: expressionS may have been modified:
1404         Unused fields zeroed to help expr ().  */
1405
1406 static void
1407 clean_up_expression (expressionS *expressionP)
1408 {
1409   switch (expressionP->X_op)
1410     {
1411     case O_illegal:
1412     case O_absent:
1413       expressionP->X_add_number = 0;
1414       /* Fall through.  */
1415     case O_big:
1416     case O_constant:
1417     case O_register:
1418       expressionP->X_add_symbol = NULL;
1419       /* Fall through.  */
1420     case O_symbol:
1421     case O_uminus:
1422     case O_bit_not:
1423       expressionP->X_op_symbol = NULL;
1424       break;
1425     default:
1426       break;
1427     }
1428 }
1429 \f
1430 /* Expression parser.  */
1431
1432 /* We allow an empty expression, and just assume (absolute,0) silently.
1433    Unary operators and parenthetical expressions are treated as operands.
1434    As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1435
1436    We used to do an aho/ullman shift-reduce parser, but the logic got so
1437    warped that I flushed it and wrote a recursive-descent parser instead.
1438    Now things are stable, would anybody like to write a fast parser?
1439    Most expressions are either register (which does not even reach here)
1440    or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1441    So I guess it doesn't really matter how inefficient more complex expressions
1442    are parsed.
1443
1444    After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1445    Also, we have consumed any leading or trailing spaces (operand does that)
1446    and done all intervening operators.
1447
1448    This returns the segment of the result, which will be
1449    absolute_section or the segment of a symbol.  */
1450
1451 #undef __
1452 #define __ O_illegal
1453 #ifndef O_SINGLE_EQ
1454 #define O_SINGLE_EQ O_illegal
1455 #endif
1456
1457 /* Maps ASCII -> operators.  */
1458 static const operatorT op_encoding[256] = {
1459   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1460   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1461
1462   __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1463   __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1464   __, __, __, __, __, __, __, __,
1465   __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1466   __, __, __, __, __, __, __, __,
1467   __, __, __, __, __, __, __, __,
1468   __, __, __, __, __, __, __, __,
1469   __, __, __,
1470 #ifdef NEED_INDEX_OPERATOR
1471   O_index,
1472 #else
1473   __,
1474 #endif
1475   __, __, O_bit_exclusive_or, __,
1476   __, __, __, __, __, __, __, __,
1477   __, __, __, __, __, __, __, __,
1478   __, __, __, __, __, __, __, __,
1479   __, __, __, __, O_bit_inclusive_or, __, __, __,
1480
1481   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1482   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1483   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1484   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1485   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1486   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1487   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1488   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1489 };
1490
1491 /* Rank Examples
1492    0    operand, (expression)
1493    1    ||
1494    2    &&
1495    3    == <> < <= >= >
1496    4    + -
1497    5    used for * / % in MRI mode
1498    6    & ^ ! |
1499    7    * / % << >>
1500    8    unary - unary ~
1501 */
1502 static operator_rankT op_rank[O_max] = {
1503   0,    /* O_illegal */
1504   0,    /* O_absent */
1505   0,    /* O_constant */
1506   0,    /* O_symbol */
1507   0,    /* O_symbol_rva */
1508   0,    /* O_register */
1509   0,    /* O_big */
1510   9,    /* O_uminus */
1511   9,    /* O_bit_not */
1512   9,    /* O_logical_not */
1513   8,    /* O_multiply */
1514   8,    /* O_divide */
1515   8,    /* O_modulus */
1516   8,    /* O_left_shift */
1517   8,    /* O_right_shift */
1518   7,    /* O_bit_inclusive_or */
1519   7,    /* O_bit_or_not */
1520   7,    /* O_bit_exclusive_or */
1521   7,    /* O_bit_and */
1522   5,    /* O_add */
1523   5,    /* O_subtract */
1524   4,    /* O_eq */
1525   4,    /* O_ne */
1526   4,    /* O_lt */
1527   4,    /* O_le */
1528   4,    /* O_ge */
1529   4,    /* O_gt */
1530   3,    /* O_logical_and */
1531   2,    /* O_logical_or */
1532   1,    /* O_index */
1533 };
1534
1535 /* Unfortunately, in MRI mode for the m68k, multiplication and
1536    division have lower precedence than the bit wise operators.  This
1537    function sets the operator precedences correctly for the current
1538    mode.  Also, MRI uses a different bit_not operator, and this fixes
1539    that as well.  */
1540
1541 #define STANDARD_MUL_PRECEDENCE 8
1542 #define MRI_MUL_PRECEDENCE 6
1543
1544 void
1545 expr_set_precedence (void)
1546 {
1547   if (flag_m68k_mri)
1548     {
1549       op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1550       op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1551       op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1552     }
1553   else
1554     {
1555       op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1556       op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1557       op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1558     }
1559 }
1560
1561 void
1562 expr_set_rank (operatorT op, operator_rankT rank)
1563 {
1564   gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1565   op_rank[op] = rank;
1566 }
1567
1568 /* Initialize the expression parser.  */
1569
1570 void
1571 expr_begin (void)
1572 {
1573   expr_set_precedence ();
1574
1575   /* Verify that X_op field is wide enough.  */
1576   {
1577     expressionS e;
1578     e.X_op = O_max;
1579     gas_assert (e.X_op == O_max);
1580   }
1581 }
1582 \f
1583 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1584    sets NUM_CHARS to the number of characters in the operator.
1585    Does not advance INPUT_LINE_POINTER.  */
1586
1587 static inline operatorT
1588 operatorf (int *num_chars)
1589 {
1590   int c;
1591   operatorT ret;
1592
1593   c = *input_line_pointer & 0xff;
1594   *num_chars = 1;
1595
1596   if (is_end_of_line[c])
1597     return O_illegal;
1598
1599 #ifdef md_operator
1600   if (is_name_beginner (c))
1601     {
1602       char *name;
1603       char ec = get_symbol_name (& name);
1604
1605       ret = md_operator (name, 2, &ec);
1606       switch (ret)
1607         {
1608         case O_absent:
1609           *input_line_pointer = ec;
1610           input_line_pointer = name;
1611           break;
1612         case O_uminus:
1613         case O_bit_not:
1614         case O_logical_not:
1615           as_bad (_("invalid use of operator \"%s\""), name);
1616           ret = O_illegal;
1617           /* FALLTHROUGH */
1618         default:
1619           *input_line_pointer = ec;
1620           *num_chars = input_line_pointer - name;
1621           input_line_pointer = name;
1622           return ret;
1623         }
1624     }
1625 #endif
1626
1627   switch (c)
1628     {
1629     default:
1630       ret = op_encoding[c];
1631 #ifdef md_operator
1632       if (ret == O_illegal)
1633         {
1634           char *start = input_line_pointer;
1635
1636           ret = md_operator (NULL, 2, NULL);
1637           if (ret != O_illegal)
1638             *num_chars = input_line_pointer - start;
1639           input_line_pointer = start;
1640         }
1641 #endif
1642       return ret;
1643
1644     case '+':
1645     case '-':
1646       return op_encoding[c];
1647
1648     case '<':
1649       switch (input_line_pointer[1])
1650         {
1651         default:
1652           return op_encoding[c];
1653         case '<':
1654           ret = O_left_shift;
1655           break;
1656         case '>':
1657           ret = O_ne;
1658           break;
1659         case '=':
1660           ret = O_le;
1661           break;
1662         }
1663       *num_chars = 2;
1664       return ret;
1665
1666     case '=':
1667       if (input_line_pointer[1] != '=')
1668         return op_encoding[c];
1669
1670       *num_chars = 2;
1671       return O_eq;
1672
1673     case '>':
1674       switch (input_line_pointer[1])
1675         {
1676         default:
1677           return op_encoding[c];
1678         case '>':
1679           ret = O_right_shift;
1680           break;
1681         case '=':
1682           ret = O_ge;
1683           break;
1684         }
1685       *num_chars = 2;
1686       return ret;
1687
1688     case '!':
1689       switch (input_line_pointer[1])
1690         {
1691         case '!':
1692           /* We accept !! as equivalent to ^ for MRI compatibility. */
1693           *num_chars = 2;
1694           return O_bit_exclusive_or;
1695         case '=':
1696           /* We accept != as equivalent to <>.  */
1697           *num_chars = 2;
1698           return O_ne;
1699         default:
1700           if (flag_m68k_mri)
1701             return O_bit_inclusive_or;
1702           return op_encoding[c];
1703         }
1704
1705     case '|':
1706       if (input_line_pointer[1] != '|')
1707         return op_encoding[c];
1708
1709       *num_chars = 2;
1710       return O_logical_or;
1711
1712     case '&':
1713       if (input_line_pointer[1] != '&')
1714         return op_encoding[c];
1715
1716       *num_chars = 2;
1717       return O_logical_and;
1718     }
1719
1720   /* NOTREACHED  */
1721 }
1722
1723 /* Implement "word-size + 1 bit" addition for
1724    {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}.  This
1725    is used so that the full range of unsigned word values and the full range of
1726    signed word values can be represented in an O_constant expression, which is
1727    useful e.g. for .sleb128 directives.  */
1728
1729 void
1730 add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1731 {
1732   valueT ures = resultP->X_add_number;
1733   valueT uamount = amount;
1734
1735   resultP->X_add_number += amount;
1736
1737   resultP->X_extrabit ^= rhs_highbit;
1738
1739   if (ures + uamount < ures)
1740     resultP->X_extrabit ^= 1;
1741 }
1742
1743 /* Similarly, for subtraction.  */
1744
1745 void
1746 subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1747 {
1748   valueT ures = resultP->X_add_number;
1749   valueT uamount = amount;
1750
1751   resultP->X_add_number -= amount;
1752
1753   resultP->X_extrabit ^= rhs_highbit;
1754
1755   if (ures < uamount)
1756     resultP->X_extrabit ^= 1;
1757 }
1758
1759 /* Parse an expression.  */
1760
1761 segT
1762 expr (int rankarg,              /* Larger # is higher rank.  */
1763       expressionS *resultP,     /* Deliver result here.  */
1764       enum expr_mode mode       /* Controls behavior.  */)
1765 {
1766   operator_rankT rank = (operator_rankT) rankarg;
1767   segT retval;
1768   expressionS right;
1769   operatorT op_left;
1770   operatorT op_right;
1771   int op_chars;
1772
1773   know (rankarg >= 0);
1774
1775   /* Save the value of dot for the fixup code.  */
1776   if (rank == 0)
1777     {
1778       dot_value = frag_now_fix ();
1779       dot_frag = frag_now;
1780     }
1781
1782   retval = operand (resultP, mode);
1783
1784   /* operand () gobbles spaces.  */
1785   know (*input_line_pointer != ' ');
1786
1787   op_left = operatorf (&op_chars);
1788   while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1789     {
1790       segT rightseg;
1791       offsetT frag_off;
1792
1793       input_line_pointer += op_chars;   /* -> after operator.  */
1794
1795       right.X_md = 0;
1796       rightseg = expr (op_rank[(int) op_left], &right, mode);
1797       if (right.X_op == O_absent)
1798         {
1799           as_warn (_("missing operand; zero assumed"));
1800           right.X_op = O_constant;
1801           right.X_add_number = 0;
1802           right.X_add_symbol = NULL;
1803           right.X_op_symbol = NULL;
1804         }
1805
1806       know (*input_line_pointer != ' ');
1807
1808       if (op_left == O_index)
1809         {
1810           if (*input_line_pointer != ']')
1811             as_bad ("missing right bracket");
1812           else
1813             {
1814               ++input_line_pointer;
1815               SKIP_WHITESPACE ();
1816             }
1817         }
1818
1819       op_right = operatorf (&op_chars);
1820
1821       know (op_right == O_illegal || op_left == O_index
1822             || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1823       know ((int) op_left >= (int) O_multiply);
1824 #ifndef md_operator
1825       know ((int) op_left <= (int) O_index);
1826 #else
1827       know ((int) op_left < (int) O_max);
1828 #endif
1829
1830       /* input_line_pointer->after right-hand quantity.  */
1831       /* left-hand quantity in resultP.  */
1832       /* right-hand quantity in right.  */
1833       /* operator in op_left.  */
1834
1835       if (resultP->X_op == O_big)
1836         {
1837           if (resultP->X_add_number > 0)
1838             as_warn (_("left operand is a bignum; integer 0 assumed"));
1839           else
1840             as_warn (_("left operand is a float; integer 0 assumed"));
1841           resultP->X_op = O_constant;
1842           resultP->X_add_number = 0;
1843           resultP->X_add_symbol = NULL;
1844           resultP->X_op_symbol = NULL;
1845         }
1846       if (right.X_op == O_big)
1847         {
1848           if (right.X_add_number > 0)
1849             as_warn (_("right operand is a bignum; integer 0 assumed"));
1850           else
1851             as_warn (_("right operand is a float; integer 0 assumed"));
1852           right.X_op = O_constant;
1853           right.X_add_number = 0;
1854           right.X_add_symbol = NULL;
1855           right.X_op_symbol = NULL;
1856         }
1857
1858       /* Optimize common cases.  */
1859 #ifdef md_optimize_expr
1860       if (md_optimize_expr (resultP, op_left, &right))
1861         {
1862           /* Skip.  */
1863           ;
1864         }
1865       else
1866 #endif
1867 #ifndef md_register_arithmetic
1868 # define md_register_arithmetic 1
1869 #endif
1870       if (op_left == O_add && right.X_op == O_constant
1871           && (md_register_arithmetic || resultP->X_op != O_register))
1872         {
1873           /* X + constant.  */
1874           add_to_result (resultP, right.X_add_number, right.X_extrabit);
1875         }
1876       /* This case comes up in PIC code.  */
1877       else if (op_left == O_subtract
1878                && right.X_op == O_symbol
1879                && resultP->X_op == O_symbol
1880                && retval == rightseg
1881 #ifdef md_allow_local_subtract
1882                && md_allow_local_subtract (resultP, & right, rightseg)
1883 #endif
1884                && ((SEG_NORMAL (rightseg)
1885                     && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1886                     && !S_FORCE_RELOC (right.X_add_symbol, 0))
1887                    || right.X_add_symbol == resultP->X_add_symbol)
1888                && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1889                                        symbol_get_frag (right.X_add_symbol),
1890                                        &frag_off))
1891         {
1892           offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1893                                 - S_GET_VALUE (right.X_add_symbol);
1894           subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1895           subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1896           add_to_result (resultP, symval_diff, symval_diff < 0);
1897           resultP->X_op = O_constant;
1898           resultP->X_add_symbol = 0;
1899         }
1900       else if (op_left == O_subtract && right.X_op == O_constant
1901                && (md_register_arithmetic || resultP->X_op != O_register))
1902         {
1903           /* X - constant.  */
1904           subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1905         }
1906       else if (op_left == O_add && resultP->X_op == O_constant
1907                && (md_register_arithmetic || right.X_op != O_register))
1908         {
1909           /* Constant + X.  */
1910           resultP->X_op = right.X_op;
1911           resultP->X_add_symbol = right.X_add_symbol;
1912           resultP->X_op_symbol = right.X_op_symbol;
1913           add_to_result (resultP, right.X_add_number, right.X_extrabit);
1914           retval = rightseg;
1915         }
1916       else if (resultP->X_op == O_constant && right.X_op == O_constant)
1917         {
1918           /* Constant OP constant.  */
1919           offsetT v = right.X_add_number;
1920           if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1921             {
1922               as_warn (_("division by zero"));
1923               v = 1;
1924             }
1925           if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1926               && (op_left == O_left_shift || op_left == O_right_shift))
1927             {
1928               as_warn_value_out_of_range (_("shift count"), v, 0,
1929                                           sizeof(valueT) * CHAR_BIT - 1,
1930                                           NULL, 0);
1931               resultP->X_add_number = v = 0;
1932             }
1933           switch (op_left)
1934             {
1935             default:                    goto general;
1936             case O_multiply:            resultP->X_add_number *= v; break;
1937             case O_divide:              resultP->X_add_number /= v; break;
1938             case O_modulus:             resultP->X_add_number %= v; break;
1939             case O_left_shift:          resultP->X_add_number <<= v; break;
1940             case O_right_shift:
1941               /* We always use unsigned shifts, to avoid relying on
1942                  characteristics of the compiler used to compile gas.  */
1943               resultP->X_add_number =
1944                 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1945               break;
1946             case O_bit_inclusive_or:    resultP->X_add_number |= v; break;
1947             case O_bit_or_not:          resultP->X_add_number |= ~v; break;
1948             case O_bit_exclusive_or:    resultP->X_add_number ^= v; break;
1949             case O_bit_and:             resultP->X_add_number &= v; break;
1950               /* Constant + constant (O_add) is handled by the
1951                  previous if statement for constant + X, so is omitted
1952                  here.  */
1953             case O_subtract:
1954               subtract_from_result (resultP, v, 0);
1955               break;
1956             case O_eq:
1957               resultP->X_add_number =
1958                 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1959               break;
1960             case O_ne:
1961               resultP->X_add_number =
1962                 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1963               break;
1964             case O_lt:
1965               resultP->X_add_number =
1966                 resultP->X_add_number <  v ? ~ (offsetT) 0 : 0;
1967               break;
1968             case O_le:
1969               resultP->X_add_number =
1970                 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1971               break;
1972             case O_ge:
1973               resultP->X_add_number =
1974                 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1975               break;
1976             case O_gt:
1977               resultP->X_add_number =
1978                 resultP->X_add_number >  v ? ~ (offsetT) 0 : 0;
1979               break;
1980             case O_logical_and:
1981               resultP->X_add_number = resultP->X_add_number && v;
1982               break;
1983             case O_logical_or:
1984               resultP->X_add_number = resultP->X_add_number || v;
1985               break;
1986             }
1987         }
1988       else if (resultP->X_op == O_symbol
1989                && right.X_op == O_symbol
1990                && (op_left == O_add
1991                    || op_left == O_subtract
1992                    || (resultP->X_add_number == 0
1993                        && right.X_add_number == 0)))
1994         {
1995           /* Symbol OP symbol.  */
1996           resultP->X_op = op_left;
1997           resultP->X_op_symbol = right.X_add_symbol;
1998           if (op_left == O_add)
1999             add_to_result (resultP, right.X_add_number, right.X_extrabit);
2000           else if (op_left == O_subtract)
2001             {
2002               subtract_from_result (resultP, right.X_add_number,
2003                                     right.X_extrabit);
2004               if (retval == rightseg
2005                   && SEG_NORMAL (retval)
2006                   && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2007                   && !S_FORCE_RELOC (right.X_add_symbol, 0))
2008                 {
2009                   retval = absolute_section;
2010                   rightseg = absolute_section;
2011                 }
2012             }
2013         }
2014       else
2015         {
2016         general:
2017           /* The general case.  */
2018           resultP->X_add_symbol = make_expr_symbol (resultP);
2019           resultP->X_op_symbol = make_expr_symbol (&right);
2020           resultP->X_op = op_left;
2021           resultP->X_add_number = 0;
2022           resultP->X_unsigned = 1;
2023           resultP->X_extrabit = 0;
2024         }
2025
2026       if (retval != rightseg)
2027         {
2028           if (retval == undefined_section)
2029             ;
2030           else if (rightseg == undefined_section)
2031             retval = rightseg;
2032           else if (retval == expr_section)
2033             ;
2034           else if (rightseg == expr_section)
2035             retval = rightseg;
2036           else if (retval == reg_section)
2037             ;
2038           else if (rightseg == reg_section)
2039             retval = rightseg;
2040           else if (rightseg == absolute_section)
2041             ;
2042           else if (retval == absolute_section)
2043             retval = rightseg;
2044 #ifdef DIFF_EXPR_OK
2045           else if (op_left == O_subtract)
2046             ;
2047 #endif
2048           else
2049             as_bad (_("operation combines symbols in different segments"));
2050         }
2051
2052       op_left = op_right;
2053     }                           /* While next operator is >= this rank.  */
2054
2055   /* The PA port needs this information.  */
2056   if (resultP->X_add_symbol)
2057     symbol_mark_used (resultP->X_add_symbol);
2058
2059   if (rank == 0 && mode == expr_evaluate)
2060     resolve_expression (resultP);
2061
2062   return resultP->X_op == O_constant ? absolute_section : retval;
2063 }
2064
2065 /* Resolve an expression without changing any symbols/sub-expressions
2066    used.  */
2067
2068 int
2069 resolve_expression (expressionS *expressionP)
2070 {
2071   /* Help out with CSE.  */
2072   valueT final_val = expressionP->X_add_number;
2073   symbolS *add_symbol = expressionP->X_add_symbol;
2074   symbolS *orig_add_symbol = add_symbol;
2075   symbolS *op_symbol = expressionP->X_op_symbol;
2076   operatorT op = expressionP->X_op;
2077   valueT left, right;
2078   segT seg_left, seg_right;
2079   fragS *frag_left, *frag_right;
2080   offsetT frag_off;
2081
2082   switch (op)
2083     {
2084     default:
2085       return 0;
2086
2087     case O_constant:
2088     case O_register:
2089       left = 0;
2090       break;
2091
2092     case O_symbol:
2093     case O_symbol_rva:
2094       if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2095         return 0;
2096
2097       break;
2098
2099     case O_uminus:
2100     case O_bit_not:
2101     case O_logical_not:
2102       if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2103         return 0;
2104
2105       if (seg_left != absolute_section)
2106         return 0;
2107
2108       if (op == O_logical_not)
2109         left = !left;
2110       else if (op == O_uminus)
2111         left = -left;
2112       else
2113         left = ~left;
2114       op = O_constant;
2115       break;
2116
2117     case O_multiply:
2118     case O_divide:
2119     case O_modulus:
2120     case O_left_shift:
2121     case O_right_shift:
2122     case O_bit_inclusive_or:
2123     case O_bit_or_not:
2124     case O_bit_exclusive_or:
2125     case O_bit_and:
2126     case O_add:
2127     case O_subtract:
2128     case O_eq:
2129     case O_ne:
2130     case O_lt:
2131     case O_le:
2132     case O_ge:
2133     case O_gt:
2134     case O_logical_and:
2135     case O_logical_or:
2136       if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2137           || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2138         return 0;
2139
2140       /* Simplify addition or subtraction of a constant by folding the
2141          constant into X_add_number.  */
2142       if (op == O_add)
2143         {
2144           if (seg_right == absolute_section)
2145             {
2146               final_val += right;
2147               op = O_symbol;
2148               break;
2149             }
2150           else if (seg_left == absolute_section)
2151             {
2152               final_val += left;
2153               left = right;
2154               seg_left = seg_right;
2155               add_symbol = op_symbol;
2156               orig_add_symbol = expressionP->X_op_symbol;
2157               op = O_symbol;
2158               break;
2159             }
2160         }
2161       else if (op == O_subtract)
2162         {
2163           if (seg_right == absolute_section)
2164             {
2165               final_val -= right;
2166               op = O_symbol;
2167               break;
2168             }
2169         }
2170
2171       /* Equality and non-equality tests are permitted on anything.
2172          Subtraction, and other comparison operators are permitted if
2173          both operands are in the same section.
2174          Shifts by constant zero are permitted on anything.
2175          Multiplies, bit-ors, and bit-ands with constant zero are
2176          permitted on anything.
2177          Multiplies and divides by constant one are permitted on
2178          anything.
2179          Binary operations with both operands being the same register
2180          or undefined symbol are permitted if the result doesn't depend
2181          on the input value.
2182          Otherwise, both operands must be absolute.  We already handled
2183          the case of addition or subtraction of a constant above.  */
2184       frag_off = 0;
2185       if (!(seg_left == absolute_section
2186                && seg_right == absolute_section)
2187           && !(op == O_eq || op == O_ne)
2188           && !((op == O_subtract
2189                 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2190                && seg_left == seg_right
2191                && (finalize_syms
2192                    || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2193                && (seg_left != reg_section || left == right)
2194                && (seg_left != undefined_section || add_symbol == op_symbol)))
2195         {
2196           if ((seg_left == absolute_section && left == 0)
2197               || (seg_right == absolute_section && right == 0))
2198             {
2199               if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2200                 {
2201                   if (!(seg_right == absolute_section && right == 0))
2202                     {
2203                       seg_left = seg_right;
2204                       left = right;
2205                       add_symbol = op_symbol;
2206                       orig_add_symbol = expressionP->X_op_symbol;
2207                     }
2208                   op = O_symbol;
2209                   break;
2210                 }
2211               else if (op == O_left_shift || op == O_right_shift)
2212                 {
2213                   if (!(seg_left == absolute_section && left == 0))
2214                     {
2215                       op = O_symbol;
2216                       break;
2217                     }
2218                 }
2219               else if (op != O_multiply
2220                        && op != O_bit_or_not && op != O_bit_and)
2221                 return 0;
2222             }
2223           else if (op == O_multiply
2224                    && seg_left == absolute_section && left == 1)
2225             {
2226               seg_left = seg_right;
2227               left = right;
2228               add_symbol = op_symbol;
2229               orig_add_symbol = expressionP->X_op_symbol;
2230               op = O_symbol;
2231               break;
2232             }
2233           else if ((op == O_multiply || op == O_divide)
2234                    && seg_right == absolute_section && right == 1)
2235             {
2236               op = O_symbol;
2237               break;
2238             }
2239           else if (!(left == right
2240                      && ((seg_left == reg_section && seg_right == reg_section)
2241                          || (seg_left == undefined_section
2242                              && seg_right == undefined_section
2243                              && add_symbol == op_symbol))))
2244             return 0;
2245           else if (op == O_bit_and || op == O_bit_inclusive_or)
2246             {
2247               op = O_symbol;
2248               break;
2249             }
2250           else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2251             return 0;
2252         }
2253
2254       right += frag_off / OCTETS_PER_BYTE;
2255       switch (op)
2256         {
2257         case O_add:                     left += right; break;
2258         case O_subtract:                left -= right; break;
2259         case O_multiply:                left *= right; break;
2260         case O_divide:
2261           if (right == 0)
2262             return 0;
2263           left = (offsetT) left / (offsetT) right;
2264           break;
2265         case O_modulus:
2266           if (right == 0)
2267             return 0;
2268           left = (offsetT) left % (offsetT) right;
2269           break;
2270         case O_left_shift:              left <<= right; break;
2271         case O_right_shift:             left >>= right; break;
2272         case O_bit_inclusive_or:        left |= right; break;
2273         case O_bit_or_not:              left |= ~right; break;
2274         case O_bit_exclusive_or:        left ^= right; break;
2275         case O_bit_and:                 left &= right; break;
2276         case O_eq:
2277         case O_ne:
2278           left = (left == right
2279                   && seg_left == seg_right
2280                   && (finalize_syms || frag_left == frag_right)
2281                   && (seg_left != undefined_section
2282                       || add_symbol == op_symbol)
2283                   ? ~ (valueT) 0 : 0);
2284           if (op == O_ne)
2285             left = ~left;
2286           break;
2287         case O_lt:
2288           left = (offsetT) left <  (offsetT) right ? ~ (valueT) 0 : 0;
2289           break;
2290         case O_le:
2291           left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2292           break;
2293         case O_ge:
2294           left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2295           break;
2296         case O_gt:
2297           left = (offsetT) left >  (offsetT) right ? ~ (valueT) 0 : 0;
2298           break;
2299         case O_logical_and:     left = left && right; break;
2300         case O_logical_or:      left = left || right; break;
2301         default:                abort ();
2302         }
2303
2304       op = O_constant;
2305       break;
2306     }
2307
2308   if (op == O_symbol)
2309     {
2310       if (seg_left == absolute_section)
2311         op = O_constant;
2312       else if (seg_left == reg_section && final_val == 0)
2313         op = O_register;
2314       else if (!symbol_same_p (add_symbol, orig_add_symbol))
2315         final_val += left;
2316       expressionP->X_add_symbol = add_symbol;
2317     }
2318   expressionP->X_op = op;
2319
2320   if (op == O_constant || op == O_register)
2321     final_val += left;
2322   expressionP->X_add_number = final_val;
2323
2324   return 1;
2325 }
2326 \f
2327 /* This lives here because it belongs equally in expr.c & read.c.
2328    expr.c is just a branch office read.c anyway, and putting it
2329    here lessens the crowd at read.c.
2330
2331    Assume input_line_pointer is at start of symbol name, or the
2332     start of a double quote enclosed symbol name.
2333    Advance input_line_pointer past symbol name.
2334    Turn that character into a '\0', returning its former value,
2335     which may be the closing double quote.
2336    This allows a string compare (RMS wants symbol names to be strings)
2337     of the symbol name.
2338    There will always be a char following symbol name, because all good
2339    lines end in end-of-line.  */
2340
2341 char
2342 get_symbol_name (char ** ilp_return)
2343 {
2344   char c;
2345
2346   * ilp_return = input_line_pointer;
2347   /* We accept \001 in a name in case this is being called with a
2348      constructed string.  */
2349   if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2350     {
2351       while (is_part_of_name (c = *input_line_pointer++)
2352              || c == '\001')
2353         ;
2354       if (is_name_ender (c))
2355         c = *input_line_pointer++;
2356     }
2357   else if (c == '"')
2358     {
2359       bfd_boolean backslash_seen;
2360
2361       * ilp_return = input_line_pointer;
2362       do
2363         {
2364           backslash_seen = c == '\\';
2365           c = * input_line_pointer ++;
2366         }
2367       while (c != 0 && (c != '"' || backslash_seen));
2368
2369       if (c == 0)
2370         as_warn (_("missing closing '\"'"));
2371     }
2372   *--input_line_pointer = 0;
2373   return c;
2374 }
2375
2376 /* Replace the NUL character pointed to by input_line_pointer
2377    with C.  If C is \" then advance past it.  Return the character
2378    now pointed to by input_line_pointer.  */
2379
2380 char
2381 restore_line_pointer (char c)
2382 {
2383   * input_line_pointer = c;
2384   if (c == '"')
2385     c = * ++ input_line_pointer;
2386   return c;
2387 }
2388
2389 unsigned int
2390 get_single_number (void)
2391 {
2392   expressionS exp;
2393   operand (&exp, expr_normal);
2394   return exp.X_add_number;
2395 }