Prepare gas for 64-bit obstacks
[external/binutils.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2    Copyright (C) 1987-2014 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 extern const char EXP_CHARS[], FLT_CHARS[];
50
51 /* We keep a mapping of expression symbols to file positions, so that
52    we can provide better error messages.  */
53
54 struct expr_symbol_line {
55   struct expr_symbol_line *next;
56   symbolS *sym;
57   char *file;
58   unsigned int line;
59 };
60
61 static struct expr_symbol_line *expr_symbol_lines;
62 \f
63 /* Build a dummy symbol to hold a complex expression.  This is how we
64    build expressions up out of other expressions.  The symbol is put
65    into the fake section expr_section.  */
66
67 symbolS *
68 make_expr_symbol (expressionS *expressionP)
69 {
70   expressionS zero;
71   symbolS *symbolP;
72   struct expr_symbol_line *n;
73
74   if (expressionP->X_op == O_symbol
75       && expressionP->X_add_number == 0)
76     return expressionP->X_add_symbol;
77
78   if (expressionP->X_op == O_big)
79     {
80       /* This won't work, because the actual value is stored in
81          generic_floating_point_number or generic_bignum, and we are
82          going to lose it if we haven't already.  */
83       if (expressionP->X_add_number > 0)
84         as_bad (_("bignum invalid"));
85       else
86         as_bad (_("floating point number invalid"));
87       zero.X_op = O_constant;
88       zero.X_add_number = 0;
89       zero.X_unsigned = 0;
90       zero.X_extrabit = 0;
91       clean_up_expression (&zero);
92       expressionP = &zero;
93     }
94
95   /* Putting constant symbols in absolute_section rather than
96      expr_section is convenient for the old a.out code, for which
97      S_GET_SEGMENT does not always retrieve the value put in by
98      S_SET_SEGMENT.  */
99   symbolP = symbol_create (FAKE_LABEL_NAME,
100                            (expressionP->X_op == O_constant
101                             ? absolute_section
102                             : expressionP->X_op == O_register
103                               ? reg_section
104                               : expr_section),
105                            0, &zero_address_frag);
106   symbol_set_value_expression (symbolP, expressionP);
107
108   if (expressionP->X_op == O_constant)
109     resolve_symbol_value (symbolP);
110
111   n = (struct expr_symbol_line *) xmalloc (sizeof *n);
112   n->sym = symbolP;
113   as_where (&n->file, &n->line);
114   n->next = expr_symbol_lines;
115   expr_symbol_lines = n;
116
117   return symbolP;
118 }
119
120 /* Return the file and line number for an expr symbol.  Return
121    non-zero if something was found, 0 if no information is known for
122    the symbol.  */
123
124 int
125 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
126 {
127   register struct expr_symbol_line *l;
128
129   for (l = expr_symbol_lines; l != NULL; l = l->next)
130     {
131       if (l->sym == sym)
132         {
133           *pfile = l->file;
134           *pline = l->line;
135           return 1;
136         }
137     }
138
139   return 0;
140 }
141 \f
142 /* Utilities for building expressions.
143    Since complex expressions are recorded as symbols for use in other
144    expressions these return a symbolS * and not an expressionS *.
145    These explicitly do not take an "add_number" argument.  */
146 /* ??? For completeness' sake one might want expr_build_symbol.
147    It would just return its argument.  */
148
149 /* Build an expression for an unsigned constant.
150    The corresponding one for signed constants is missing because
151    there's currently no need for it.  One could add an unsigned_p flag
152    but that seems more clumsy.  */
153
154 symbolS *
155 expr_build_uconstant (offsetT value)
156 {
157   expressionS e;
158
159   e.X_op = O_constant;
160   e.X_add_number = value;
161   e.X_unsigned = 1;
162   e.X_extrabit = 0;
163   return make_expr_symbol (&e);
164 }
165
166 /* Build an expression for the current location ('.').  */
167
168 symbolS *
169 expr_build_dot (void)
170 {
171   expressionS e;
172
173   current_location (&e);
174   return symbol_clone_if_forward_ref (make_expr_symbol (&e));
175 }
176 \f
177 /* Build any floating-point literal here.
178    Also build any bignum literal here.  */
179
180 /* Seems atof_machine can backscan through generic_bignum and hit whatever
181    happens to be loaded before it in memory.  And its way too complicated
182    for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
183    and never write into the early words, thus they'll always be zero.
184    I hate Dean's floating-point code.  Bleh.  */
185 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
186
187 FLONUM_TYPE generic_floating_point_number = {
188   &generic_bignum[6],           /* low.  (JF: Was 0)  */
189   &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high.  JF: (added +6)  */
190   0,                            /* leader.  */
191   0,                            /* exponent.  */
192   0                             /* sign.  */
193 };
194
195 \f
196 static void
197 floating_constant (expressionS *expressionP)
198 {
199   /* input_line_pointer -> floating-point constant.  */
200   int error_code;
201
202   error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
203                              &generic_floating_point_number);
204
205   if (error_code)
206     {
207       if (error_code == ERROR_EXPONENT_OVERFLOW)
208         {
209           as_bad (_("bad floating-point constant: exponent overflow"));
210         }
211       else
212         {
213           as_bad (_("bad floating-point constant: unknown error code=%d"),
214                   error_code);
215         }
216     }
217   expressionP->X_op = O_big;
218   /* input_line_pointer -> just after constant, which may point to
219      whitespace.  */
220   expressionP->X_add_number = -1;
221 }
222
223 static valueT
224 generic_bignum_to_int32 (void)
225 {
226   valueT number =
227            ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
228            | (generic_bignum[0] & LITTLENUM_MASK);
229   number &= 0xffffffff;
230   return number;
231 }
232
233 #ifdef BFD64
234 static valueT
235 generic_bignum_to_int64 (void)
236 {
237   valueT number =
238     ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
239           << LITTLENUM_NUMBER_OF_BITS)
240          | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
241         << LITTLENUM_NUMBER_OF_BITS)
242        | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
243       << LITTLENUM_NUMBER_OF_BITS)
244      | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
245   return number;
246 }
247 #endif
248
249 static void
250 integer_constant (int radix, expressionS *expressionP)
251 {
252   char *start;          /* Start of number.  */
253   char *suffix = NULL;
254   char c;
255   valueT number;        /* Offset or (absolute) value.  */
256   short int digit;      /* Value of next digit in current radix.  */
257   short int maxdig = 0; /* Highest permitted digit value.  */
258   int too_many_digits = 0;      /* If we see >= this number of.  */
259   char *name;           /* Points to name of symbol.  */
260   symbolS *symbolP;     /* Points to symbol.  */
261
262   int small;                    /* True if fits in 32 bits.  */
263
264   /* May be bignum, or may fit in 32 bits.  */
265   /* Most numbers fit into 32 bits, and we want this case to be fast.
266      so we pretend it will fit into 32 bits.  If, after making up a 32
267      bit number, we realise that we have scanned more digits than
268      comfortably fit into 32 bits, we re-scan the digits coding them
269      into a bignum.  For decimal and octal numbers we are
270      conservative: Some numbers may be assumed bignums when in fact
271      they do fit into 32 bits.  Numbers of any radix can have excess
272      leading zeros: We strive to recognise this and cast them back
273      into 32 bits.  We must check that the bignum really is more than
274      32 bits, and change it back to a 32-bit number if it fits.  The
275      number we are looking for is expected to be positive, but if it
276      fits into 32 bits as an unsigned number, we let it be a 32-bit
277      number.  The cavalier approach is for speed in ordinary cases.  */
278   /* This has been extended for 64 bits.  We blindly assume that if
279      you're compiling in 64-bit mode, the target is a 64-bit machine.
280      This should be cleaned up.  */
281
282 #ifdef BFD64
283 #define valuesize 64
284 #else /* includes non-bfd case, mostly */
285 #define valuesize 32
286 #endif
287
288   if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
289     {
290       int flt = 0;
291
292       /* In MRI mode, the number may have a suffix indicating the
293          radix.  For that matter, it might actually be a floating
294          point constant.  */
295       for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
296         {
297           if (*suffix == 'e' || *suffix == 'E')
298             flt = 1;
299         }
300
301       if (suffix == input_line_pointer)
302         {
303           radix = 10;
304           suffix = NULL;
305         }
306       else
307         {
308           c = *--suffix;
309           c = TOUPPER (c);
310           /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
311              we distinguish between 'B' and 'b'.  This is the case for
312              Z80.  */
313           if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
314             radix = 2;
315           else if (c == 'D')
316             radix = 10;
317           else if (c == 'O' || c == 'Q')
318             radix = 8;
319           else if (c == 'H')
320             radix = 16;
321           else if (suffix[1] == '.' || c == 'E' || flt)
322             {
323               floating_constant (expressionP);
324               return;
325             }
326           else
327             {
328               radix = 10;
329               suffix = NULL;
330             }
331         }
332     }
333
334   switch (radix)
335     {
336     case 2:
337       maxdig = 2;
338       too_many_digits = valuesize + 1;
339       break;
340     case 8:
341       maxdig = radix = 8;
342       too_many_digits = (valuesize + 2) / 3 + 1;
343       break;
344     case 16:
345       maxdig = radix = 16;
346       too_many_digits = (valuesize + 3) / 4 + 1;
347       break;
348     case 10:
349       maxdig = radix = 10;
350       too_many_digits = (valuesize + 11) / 4; /* Very rough.  */
351     }
352 #undef valuesize
353   start = input_line_pointer;
354   c = *input_line_pointer++;
355   for (number = 0;
356        (digit = hex_value (c)) < maxdig;
357        c = *input_line_pointer++)
358     {
359       number = number * radix + digit;
360     }
361   /* c contains character after number.  */
362   /* input_line_pointer->char after c.  */
363   small = (input_line_pointer - start - 1) < too_many_digits;
364
365   if (radix == 16 && c == '_')
366     {
367       /* This is literal of the form 0x333_0_12345678_1.
368          This example is equivalent to 0x00000333000000001234567800000001.  */
369
370       int num_little_digits = 0;
371       int i;
372       input_line_pointer = start;       /* -> 1st digit.  */
373
374       know (LITTLENUM_NUMBER_OF_BITS == 16);
375
376       for (c = '_'; c == '_'; num_little_digits += 2)
377         {
378
379           /* Convert one 64-bit word.  */
380           int ndigit = 0;
381           number = 0;
382           for (c = *input_line_pointer++;
383                (digit = hex_value (c)) < maxdig;
384                c = *(input_line_pointer++))
385             {
386               number = number * radix + digit;
387               ndigit++;
388             }
389
390           /* Check for 8 digit per word max.  */
391           if (ndigit > 8)
392             as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
393
394           /* Add this chunk to the bignum.
395              Shift things down 2 little digits.  */
396           know (LITTLENUM_NUMBER_OF_BITS == 16);
397           for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
398                i >= 2;
399                i--)
400             generic_bignum[i] = generic_bignum[i - 2];
401
402           /* Add the new digits as the least significant new ones.  */
403           generic_bignum[0] = number & 0xffffffff;
404           generic_bignum[1] = number >> 16;
405         }
406
407       /* Again, c is char after number, input_line_pointer->after c.  */
408
409       if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
410         num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
411
412       gas_assert (num_little_digits >= 4);
413
414       if (num_little_digits != 8)
415         as_bad (_("a bignum with underscores must have exactly 4 words"));
416
417       /* We might have some leading zeros.  These can be trimmed to give
418          us a change to fit this constant into a small number.  */
419       while (generic_bignum[num_little_digits - 1] == 0
420              && num_little_digits > 1)
421         num_little_digits--;
422
423       if (num_little_digits <= 2)
424         {
425           /* will fit into 32 bits.  */
426           number = generic_bignum_to_int32 ();
427           small = 1;
428         }
429 #ifdef BFD64
430       else if (num_little_digits <= 4)
431         {
432           /* Will fit into 64 bits.  */
433           number = generic_bignum_to_int64 ();
434           small = 1;
435         }
436 #endif
437       else
438         {
439           small = 0;
440
441           /* Number of littlenums in the bignum.  */
442           number = num_little_digits;
443         }
444     }
445   else if (!small)
446     {
447       /* We saw a lot of digits. manufacture a bignum the hard way.  */
448       LITTLENUM_TYPE *leader;   /* -> high order littlenum of the bignum.  */
449       LITTLENUM_TYPE *pointer;  /* -> littlenum we are frobbing now.  */
450       long carry;
451
452       leader = generic_bignum;
453       generic_bignum[0] = 0;
454       generic_bignum[1] = 0;
455       generic_bignum[2] = 0;
456       generic_bignum[3] = 0;
457       input_line_pointer = start;       /* -> 1st digit.  */
458       c = *input_line_pointer++;
459       for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
460         {
461           for (pointer = generic_bignum; pointer <= leader; pointer++)
462             {
463               long work;
464
465               work = carry + radix * *pointer;
466               *pointer = work & LITTLENUM_MASK;
467               carry = work >> LITTLENUM_NUMBER_OF_BITS;
468             }
469           if (carry)
470             {
471               if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
472                 {
473                   /* Room to grow a longer bignum.  */
474                   *++leader = carry;
475                 }
476             }
477         }
478       /* Again, c is char after number.  */
479       /* input_line_pointer -> after c.  */
480       know (LITTLENUM_NUMBER_OF_BITS == 16);
481       if (leader < generic_bignum + 2)
482         {
483           /* Will fit into 32 bits.  */
484           number = generic_bignum_to_int32 ();
485           small = 1;
486         }
487 #ifdef BFD64
488       else if (leader < generic_bignum + 4)
489         {
490           /* Will fit into 64 bits.  */
491           number = generic_bignum_to_int64 ();
492           small = 1;
493         }
494 #endif
495       else
496         {
497           /* Number of littlenums in the bignum.  */
498           number = leader - generic_bignum + 1;
499         }
500     }
501
502   if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
503       && suffix != NULL
504       && input_line_pointer - 1 == suffix)
505     c = *input_line_pointer++;
506
507   if (small)
508     {
509       /* Here with number, in correct radix. c is the next char.
510          Note that unlike un*x, we allow "011f" "0x9f" to both mean
511          the same as the (conventional) "9f".
512          This is simply easier than checking for strict canonical
513          form.  Syntax sux!  */
514
515       if (LOCAL_LABELS_FB && c == 'b')
516         {
517           /* Backward ref to local label.
518              Because it is backward, expect it to be defined.  */
519           /* Construct a local label.  */
520           name = fb_label_name ((int) number, 0);
521
522           /* Seen before, or symbol is defined: OK.  */
523           symbolP = symbol_find (name);
524           if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
525             {
526               /* Local labels are never absolute.  Don't waste time
527                  checking absoluteness.  */
528               know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
529
530               expressionP->X_op = O_symbol;
531               expressionP->X_add_symbol = symbolP;
532             }
533           else
534             {
535               /* Either not seen or not defined.  */
536               /* @@ Should print out the original string instead of
537                  the parsed number.  */
538               as_bad (_("backward ref to unknown label \"%d:\""),
539                       (int) number);
540               expressionP->X_op = O_constant;
541             }
542
543           expressionP->X_add_number = 0;
544         }                       /* case 'b' */
545       else if (LOCAL_LABELS_FB && c == 'f')
546         {
547           /* Forward reference.  Expect symbol to be undefined or
548              unknown.  undefined: seen it before.  unknown: never seen
549              it before.
550
551              Construct a local label name, then an undefined symbol.
552              Don't create a xseg frag for it: caller may do that.
553              Just return it as never seen before.  */
554           name = fb_label_name ((int) number, 1);
555           symbolP = symbol_find_or_make (name);
556           /* We have no need to check symbol properties.  */
557 #ifndef many_segments
558           /* Since "know" puts its arg into a "string", we
559              can't have newlines in the argument.  */
560           know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
561 #endif
562           expressionP->X_op = O_symbol;
563           expressionP->X_add_symbol = symbolP;
564           expressionP->X_add_number = 0;
565         }                       /* case 'f' */
566       else if (LOCAL_LABELS_DOLLAR && c == '$')
567         {
568           /* If the dollar label is *currently* defined, then this is just
569              another reference to it.  If it is not *currently* defined,
570              then this is a fresh instantiation of that number, so create
571              it.  */
572
573           if (dollar_label_defined ((long) number))
574             {
575               name = dollar_label_name ((long) number, 0);
576               symbolP = symbol_find (name);
577               know (symbolP != NULL);
578             }
579           else
580             {
581               name = dollar_label_name ((long) number, 1);
582               symbolP = symbol_find_or_make (name);
583             }
584
585           expressionP->X_op = O_symbol;
586           expressionP->X_add_symbol = symbolP;
587           expressionP->X_add_number = 0;
588         }                       /* case '$' */
589       else
590         {
591           expressionP->X_op = O_constant;
592           expressionP->X_add_number = number;
593           input_line_pointer--; /* Restore following character.  */
594         }                       /* Really just a number.  */
595     }
596   else
597     {
598       /* Not a small number.  */
599       expressionP->X_op = O_big;
600       expressionP->X_add_number = number;       /* Number of littlenums.  */
601       input_line_pointer--;     /* -> char following number.  */
602     }
603 }
604
605 /* Parse an MRI multi character constant.  */
606
607 static void
608 mri_char_constant (expressionS *expressionP)
609 {
610   int i;
611
612   if (*input_line_pointer == '\''
613       && input_line_pointer[1] != '\'')
614     {
615       expressionP->X_op = O_constant;
616       expressionP->X_add_number = 0;
617       return;
618     }
619
620   /* In order to get the correct byte ordering, we must build the
621      number in reverse.  */
622   for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
623     {
624       int j;
625
626       generic_bignum[i] = 0;
627       for (j = 0; j < CHARS_PER_LITTLENUM; j++)
628         {
629           if (*input_line_pointer == '\'')
630             {
631               if (input_line_pointer[1] != '\'')
632                 break;
633               ++input_line_pointer;
634             }
635           generic_bignum[i] <<= 8;
636           generic_bignum[i] += *input_line_pointer;
637           ++input_line_pointer;
638         }
639
640       if (i < SIZE_OF_LARGE_NUMBER - 1)
641         {
642           /* If there is more than one littlenum, left justify the
643              last one to make it match the earlier ones.  If there is
644              only one, we can just use the value directly.  */
645           for (; j < CHARS_PER_LITTLENUM; j++)
646             generic_bignum[i] <<= 8;
647         }
648
649       if (*input_line_pointer == '\''
650           && input_line_pointer[1] != '\'')
651         break;
652     }
653
654   if (i < 0)
655     {
656       as_bad (_("character constant too large"));
657       i = 0;
658     }
659
660   if (i > 0)
661     {
662       int c;
663       int j;
664
665       c = SIZE_OF_LARGE_NUMBER - i;
666       for (j = 0; j < c; j++)
667         generic_bignum[j] = generic_bignum[i + j];
668       i = c;
669     }
670
671   know (LITTLENUM_NUMBER_OF_BITS == 16);
672   if (i > 2)
673     {
674       expressionP->X_op = O_big;
675       expressionP->X_add_number = i;
676     }
677   else
678     {
679       expressionP->X_op = O_constant;
680       if (i < 2)
681         expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
682       else
683         expressionP->X_add_number =
684           (((generic_bignum[1] & LITTLENUM_MASK)
685             << LITTLENUM_NUMBER_OF_BITS)
686            | (generic_bignum[0] & LITTLENUM_MASK));
687     }
688
689   /* Skip the final closing quote.  */
690   ++input_line_pointer;
691 }
692
693 /* Return an expression representing the current location.  This
694    handles the magic symbol `.'.  */
695
696 void
697 current_location (expressionS *expressionp)
698 {
699   if (now_seg == absolute_section)
700     {
701       expressionp->X_op = O_constant;
702       expressionp->X_add_number = abs_section_offset;
703     }
704   else
705     {
706       expressionp->X_op = O_symbol;
707       expressionp->X_add_symbol = &dot_symbol;
708       expressionp->X_add_number = 0;
709     }
710 }
711
712 /* In:  Input_line_pointer points to 1st char of operand, which may
713         be a space.
714
715    Out: An expressionS.
716         The operand may have been empty: in this case X_op == O_absent.
717         Input_line_pointer->(next non-blank) char after operand.  */
718
719 static segT
720 operand (expressionS *expressionP, enum expr_mode mode)
721 {
722   char c;
723   symbolS *symbolP;     /* Points to symbol.  */
724   char *name;           /* Points to name of symbol.  */
725   segT segment;
726
727   /* All integers are regarded as unsigned unless they are negated.
728      This is because the only thing which cares whether a number is
729      unsigned is the code in emit_expr which extends constants into
730      bignums.  It should only sign extend negative numbers, so that
731      something like ``.quad 0x80000000'' is not sign extended even
732      though it appears negative if valueT is 32 bits.  */
733   expressionP->X_unsigned = 1;
734   expressionP->X_extrabit = 0;
735
736   /* Digits, assume it is a bignum.  */
737
738   SKIP_WHITESPACE ();           /* Leading whitespace is part of operand.  */
739   c = *input_line_pointer++;    /* input_line_pointer -> past char in c.  */
740
741   if (is_end_of_line[(unsigned char) c])
742     goto eol;
743
744   switch (c)
745     {
746     case '1':
747     case '2':
748     case '3':
749     case '4':
750     case '5':
751     case '6':
752     case '7':
753     case '8':
754     case '9':
755       input_line_pointer--;
756
757       integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
758                         ? 0 : 10,
759                         expressionP);
760       break;
761
762 #ifdef LITERAL_PREFIXDOLLAR_HEX
763     case '$':
764       /* $L is the start of a local label, not a hex constant.  */
765       if (* input_line_pointer == 'L')
766       goto isname;
767       integer_constant (16, expressionP);
768       break;
769 #endif
770
771 #ifdef LITERAL_PREFIXPERCENT_BIN
772     case '%':
773       integer_constant (2, expressionP);
774       break;
775 #endif
776
777     case '0':
778       /* Non-decimal radix.  */
779
780       if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
781         {
782           char *s;
783
784           /* Check for a hex or float constant.  */
785           for (s = input_line_pointer; hex_p (*s); s++)
786             ;
787           if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
788             {
789               --input_line_pointer;
790               integer_constant (0, expressionP);
791               break;
792             }
793         }
794       c = *input_line_pointer;
795       switch (c)
796         {
797         case 'o':
798         case 'O':
799         case 'q':
800         case 'Q':
801         case '8':
802         case '9':
803           if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
804             {
805               integer_constant (0, expressionP);
806               break;
807             }
808           /* Fall through.  */
809         default:
810         default_case:
811           if (c && strchr (FLT_CHARS, c))
812             {
813               input_line_pointer++;
814               floating_constant (expressionP);
815               expressionP->X_add_number = - TOLOWER (c);
816             }
817           else
818             {
819               /* The string was only zero.  */
820               expressionP->X_op = O_constant;
821               expressionP->X_add_number = 0;
822             }
823
824           break;
825
826         case 'x':
827         case 'X':
828           if (flag_m68k_mri)
829             goto default_case;
830           input_line_pointer++;
831           integer_constant (16, expressionP);
832           break;
833
834         case 'b':
835           if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
836             {
837               /* This code used to check for '+' and '-' here, and, in
838                  some conditions, fall through to call
839                  integer_constant.  However, that didn't make sense,
840                  as integer_constant only accepts digits.  */
841               /* Some of our code elsewhere does permit digits greater
842                  than the expected base; for consistency, do the same
843                  here.  */
844               if (input_line_pointer[1] < '0'
845                   || input_line_pointer[1] > '9')
846                 {
847                   /* Parse this as a back reference to label 0.  */
848                   input_line_pointer--;
849                   integer_constant (10, expressionP);
850                   break;
851                 }
852               /* Otherwise, parse this as a binary number.  */
853             }
854           /* Fall through.  */
855         case 'B':
856           input_line_pointer++;
857           if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
858             goto default_case;
859           integer_constant (2, expressionP);
860           break;
861
862         case '0':
863         case '1':
864         case '2':
865         case '3':
866         case '4':
867         case '5':
868         case '6':
869         case '7':
870           integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
871                             ? 0 : 8,
872                             expressionP);
873           break;
874
875         case 'f':
876           if (LOCAL_LABELS_FB)
877             {
878               /* If it says "0f" and it could possibly be a floating point
879                  number, make it one.  Otherwise, make it a local label,
880                  and try to deal with parsing the rest later.  */
881               if (!input_line_pointer[1]
882                   || (is_end_of_line[0xff & input_line_pointer[1]])
883                   || strchr (FLT_CHARS, 'f') == NULL)
884                 goto is_0f_label;
885               {
886                 char *cp = input_line_pointer + 1;
887                 int r = atof_generic (&cp, ".", EXP_CHARS,
888                                       &generic_floating_point_number);
889                 switch (r)
890                   {
891                   case 0:
892                   case ERROR_EXPONENT_OVERFLOW:
893                     if (*cp == 'f' || *cp == 'b')
894                       /* Looks like a difference expression.  */
895                       goto is_0f_label;
896                     else if (cp == input_line_pointer + 1)
897                       /* No characters has been accepted -- looks like
898                          end of operand.  */
899                       goto is_0f_label;
900                     else
901                       goto is_0f_float;
902                   default:
903                     as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
904                               r);
905                   }
906               }
907
908               /* Okay, now we've sorted it out.  We resume at one of these
909                  two labels, depending on what we've decided we're probably
910                  looking at.  */
911             is_0f_label:
912               input_line_pointer--;
913               integer_constant (10, expressionP);
914               break;
915
916             is_0f_float:
917               /* Fall through.  */
918               ;
919             }
920
921         case 'd':
922         case 'D':
923           if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
924             {
925               integer_constant (0, expressionP);
926               break;
927             }
928           /* Fall through.  */
929         case 'F':
930         case 'r':
931         case 'e':
932         case 'E':
933         case 'g':
934         case 'G':
935           input_line_pointer++;
936           floating_constant (expressionP);
937           expressionP->X_add_number = - TOLOWER (c);
938           break;
939
940         case '$':
941           if (LOCAL_LABELS_DOLLAR)
942             {
943               integer_constant (10, expressionP);
944               break;
945             }
946           else
947             goto default_case;
948         }
949
950       break;
951
952 #ifndef NEED_INDEX_OPERATOR
953     case '[':
954 # ifdef md_need_index_operator
955       if (md_need_index_operator())
956         goto de_fault;
957 # endif
958       /* FALLTHROUGH */
959 #endif
960     case '(':
961       /* Didn't begin with digit & not a name.  */
962       segment = expr (0, expressionP, mode);
963       /* expression () will pass trailing whitespace.  */
964       if ((c == '(' && *input_line_pointer != ')')
965           || (c == '[' && *input_line_pointer != ']'))
966         as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
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 = - expressionP->X_add_number;
1025                 /* Notice: '-' may overflow: no warning is given.
1026                    This is compatible with other people's
1027                    assemblers.  Sigh.  */
1028                 expressionP->X_unsigned = 0;
1029                 if (expressionP->X_add_number)
1030                   expressionP->X_extrabit ^= 1;
1031               }
1032             else if (c == '~' || c == '"')
1033               expressionP->X_add_number = ~ expressionP->X_add_number;
1034             else if (c == '!')
1035               expressionP->X_add_number = ! expressionP->X_add_number;
1036           }
1037         else if (expressionP->X_op == O_big
1038                  && expressionP->X_add_number <= 0
1039                  && c == '-'
1040                  && (generic_floating_point_number.sign == '+'
1041                      || generic_floating_point_number.sign == 'P'))
1042           {
1043             /* Negative flonum (eg, -1.000e0).  */
1044             if (generic_floating_point_number.sign == '+')
1045               generic_floating_point_number.sign = '-';
1046             else
1047               generic_floating_point_number.sign = 'N';
1048           }
1049         else if (expressionP->X_op == O_big
1050                  && expressionP->X_add_number > 0)
1051           {
1052             int i;
1053
1054             if (c == '~' || c == '-')
1055               {
1056                 for (i = 0; i < expressionP->X_add_number; ++i)
1057                   generic_bignum[i] = ~generic_bignum[i];
1058
1059                 /* Extend the bignum to at least the size of .octa.  */
1060                 if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1061                   {
1062                     expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1063                     for (; i < expressionP->X_add_number; ++i)
1064                       generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1065                   }
1066
1067                 if (c == '-')
1068                   for (i = 0; i < expressionP->X_add_number; ++i)
1069                     {
1070                       generic_bignum[i] += 1;
1071                       if (generic_bignum[i])
1072                         break;
1073                     }
1074               }
1075             else if (c == '!')
1076               {
1077                 for (i = 0; i < expressionP->X_add_number; ++i)
1078                   if (generic_bignum[i] != 0)
1079                     break;
1080                 expressionP->X_add_number = i >= expressionP->X_add_number;
1081                 expressionP->X_op = O_constant;
1082                 expressionP->X_unsigned = 1;
1083                 expressionP->X_extrabit = 0;
1084               }
1085           }
1086         else if (expressionP->X_op != O_illegal
1087                  && expressionP->X_op != O_absent)
1088           {
1089             if (c != '+')
1090               {
1091                 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1092                 if (c == '-')
1093                   expressionP->X_op = O_uminus;
1094                 else if (c == '~' || c == '"')
1095                   expressionP->X_op = O_bit_not;
1096                 else
1097                   expressionP->X_op = O_logical_not;
1098                 expressionP->X_add_number = 0;
1099               }
1100           }
1101         else
1102           as_warn (_("Unary operator %c ignored because bad operand follows"),
1103                    c);
1104       }
1105       break;
1106
1107 #if defined (DOLLAR_DOT) || defined (TC_M68K)
1108     case '$':
1109       /* '$' is the program counter when in MRI mode, or when
1110          DOLLAR_DOT is defined.  */
1111 #ifndef DOLLAR_DOT
1112       if (! flag_m68k_mri)
1113         goto de_fault;
1114 #endif
1115       if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1116         {
1117           /* In MRI mode and on Z80, '$' is also used as the prefix
1118              for a hexadecimal constant.  */
1119           integer_constant (16, expressionP);
1120           break;
1121         }
1122
1123       if (is_part_of_name (*input_line_pointer))
1124         goto isname;
1125
1126       current_location (expressionP);
1127       break;
1128 #endif
1129
1130     case '.':
1131       if (!is_part_of_name (*input_line_pointer))
1132         {
1133           current_location (expressionP);
1134           break;
1135         }
1136       else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1137                 && ! is_part_of_name (input_line_pointer[8]))
1138                || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1139                    && ! is_part_of_name (input_line_pointer[7])))
1140         {
1141           int start;
1142
1143           start = (input_line_pointer[1] == 't'
1144                    || input_line_pointer[1] == 'T');
1145           input_line_pointer += start ? 8 : 7;
1146           SKIP_WHITESPACE ();
1147           if (*input_line_pointer != '(')
1148             as_bad (_("syntax error in .startof. or .sizeof."));
1149           else
1150             {
1151               char *buf;
1152
1153               ++input_line_pointer;
1154               SKIP_WHITESPACE ();
1155               name = input_line_pointer;
1156               c = get_symbol_end ();
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 ();
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)) /* 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           name = --input_line_pointer;
1233           c = get_symbol_end ();
1234
1235 #ifdef md_operator
1236           {
1237             operatorT op = md_operator (name, 1, &c);
1238
1239             switch (op)
1240               {
1241               case O_uminus:
1242                 *input_line_pointer = c;
1243                 c = '-';
1244                 goto unary;
1245               case O_bit_not:
1246                 *input_line_pointer = c;
1247                 c = '~';
1248                 goto unary;
1249               case O_logical_not:
1250                 *input_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             if (op != O_absent && op != O_illegal)
1260               {
1261                 *input_line_pointer = c;
1262                 expr (9, expressionP, mode);
1263                 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1264                 expressionP->X_op_symbol = NULL;
1265                 expressionP->X_add_number = 0;
1266                 expressionP->X_op = op;
1267                 break;
1268               }
1269           }
1270 #endif
1271
1272 #ifdef md_parse_name
1273           /* This is a hook for the backend to parse certain names
1274              specially in certain contexts.  If a name always has a
1275              specific value, it can often be handled by simply
1276              entering it in the symbol table.  */
1277           if (md_parse_name (name, expressionP, mode, &c))
1278             {
1279               *input_line_pointer = c;
1280               break;
1281             }
1282 #endif
1283
1284 #ifdef TC_I960
1285           /* The MRI i960 assembler permits
1286                  lda sizeof code,g13
1287              FIXME: This should use md_parse_name.  */
1288           if (flag_mri
1289               && (strcasecmp (name, "sizeof") == 0
1290                   || strcasecmp (name, "startof") == 0))
1291             {
1292               int start;
1293               char *buf;
1294
1295               start = (name[1] == 't'
1296                        || name[1] == 'T');
1297
1298               *input_line_pointer = c;
1299               SKIP_WHITESPACE ();
1300
1301               name = input_line_pointer;
1302               c = get_symbol_end ();
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 ();
1318
1319               break;
1320             }
1321 #endif
1322
1323           symbolP = symbol_find_or_make (name);
1324
1325           /* If we have an absolute symbol or a reg, then we know its
1326              value now.  */
1327           segment = S_GET_SEGMENT (symbolP);
1328           if (mode != expr_defer
1329               && segment == absolute_section
1330               && !S_FORCE_RELOC (symbolP, 0))
1331             {
1332               expressionP->X_op = O_constant;
1333               expressionP->X_add_number = S_GET_VALUE (symbolP);
1334             }
1335           else if (mode != expr_defer && segment == reg_section)
1336             {
1337               expressionP->X_op = O_register;
1338               expressionP->X_add_number = S_GET_VALUE (symbolP);
1339             }
1340           else
1341             {
1342               expressionP->X_op = O_symbol;
1343               expressionP->X_add_symbol = symbolP;
1344               expressionP->X_add_number = 0;
1345             }
1346           *input_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 = input_line_pointer;
1603       char ec = get_symbol_end ();
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.
2332    Advance input_line_pointer past symbol name.
2333    Turn that character into a '\0', returning its former value.
2334    This allows a string compare (RMS wants symbol names to be strings)
2335    of the symbol name.
2336    There will always be a char following symbol name, because all good
2337    lines end in end-of-line.  */
2338
2339 char
2340 get_symbol_end (void)
2341 {
2342   char c;
2343
2344   /* We accept \001 in a name in case this is being called with a
2345      constructed string.  */
2346   if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2347     {
2348       while (is_part_of_name (c = *input_line_pointer++)
2349              || c == '\001')
2350         ;
2351       if (is_name_ender (c))
2352         c = *input_line_pointer++;
2353     }
2354   *--input_line_pointer = 0;
2355   return (c);
2356 }
2357
2358 unsigned int
2359 get_single_number (void)
2360 {
2361   expressionS exp;
2362   operand (&exp, expr_normal);
2363   return exp.X_add_number;
2364 }