* symbols.c (symbol_clone_if_forward_ref): Don't limit cloning
[external/binutils.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
4    Free Software Foundation, Inc.
5
6    This file is part of GAS, the GNU Assembler.
7
8    GAS is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GAS is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GAS; see the file COPYING.  If not, write to the Free
20    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21    02110-1301, USA.  */
22
23 /* This is really a branch office of as-read.c. I split it out to clearly
24    distinguish the world of expressions from the world of statements.
25    (It also gives smaller files to re-compile.)
26    Here, "operand"s are of expressions, not instructions.  */
27
28 #define min(a, b)       ((a) < (b) ? (a) : (b))
29
30 #include "as.h"
31 #include "safe-ctype.h"
32 #include "obstack.h"
33
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #endif
37 #ifndef CHAR_BIT
38 #define CHAR_BIT 8
39 #endif
40
41 static void floating_constant (expressionS * expressionP);
42 static valueT generic_bignum_to_int32 (void);
43 #ifdef BFD64
44 static valueT generic_bignum_to_int64 (void);
45 #endif
46 static void integer_constant (int radix, expressionS * expressionP);
47 static void mri_char_constant (expressionS *);
48 static void clean_up_expression (expressionS * expressionP);
49 static segT operand (expressionS *, enum expr_mode);
50 static operatorT operatorf (int *);
51
52 extern const char EXP_CHARS[], FLT_CHARS[];
53
54 /* We keep a mapping of expression symbols to file positions, so that
55    we can provide better error messages.  */
56
57 struct expr_symbol_line {
58   struct expr_symbol_line *next;
59   symbolS *sym;
60   char *file;
61   unsigned int line;
62 };
63
64 static struct expr_symbol_line *expr_symbol_lines;
65 \f
66 /* Build a dummy symbol to hold a complex expression.  This is how we
67    build expressions up out of other expressions.  The symbol is put
68    into the fake section expr_section.  */
69
70 symbolS *
71 make_expr_symbol (expressionS *expressionP)
72 {
73   expressionS zero;
74   symbolS *symbolP;
75   struct expr_symbol_line *n;
76
77   if (expressionP->X_op == O_symbol
78       && expressionP->X_add_number == 0)
79     return expressionP->X_add_symbol;
80
81   if (expressionP->X_op == O_big)
82     {
83       /* This won't work, because the actual value is stored in
84          generic_floating_point_number or generic_bignum, and we are
85          going to lose it if we haven't already.  */
86       if (expressionP->X_add_number > 0)
87         as_bad (_("bignum invalid"));
88       else
89         as_bad (_("floating point number invalid"));
90       zero.X_op = O_constant;
91       zero.X_add_number = 0;
92       zero.X_unsigned = 0;
93       clean_up_expression (&zero);
94       expressionP = &zero;
95     }
96
97   /* Putting constant symbols in absolute_section rather than
98      expr_section is convenient for the old a.out code, for which
99      S_GET_SEGMENT does not always retrieve the value put in by
100      S_SET_SEGMENT.  */
101   symbolP = symbol_create (FAKE_LABEL_NAME,
102                            (expressionP->X_op == O_constant
103                             ? absolute_section
104                             : expressionP->X_op == O_register
105                               ? reg_section
106                               : expr_section),
107                            0, &zero_address_frag);
108   symbol_set_value_expression (symbolP, expressionP);
109
110   if (expressionP->X_op == O_constant)
111     resolve_symbol_value (symbolP);
112
113   n = (struct expr_symbol_line *) xmalloc (sizeof *n);
114   n->sym = symbolP;
115   as_where (&n->file, &n->line);
116   n->next = expr_symbol_lines;
117   expr_symbol_lines = n;
118
119   return symbolP;
120 }
121
122 /* Return the file and line number for an expr symbol.  Return
123    non-zero if something was found, 0 if no information is known for
124    the symbol.  */
125
126 int
127 expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
128 {
129   register struct expr_symbol_line *l;
130
131   for (l = expr_symbol_lines; l != NULL; l = l->next)
132     {
133       if (l->sym == sym)
134         {
135           *pfile = l->file;
136           *pline = l->line;
137           return 1;
138         }
139     }
140
141   return 0;
142 }
143 \f
144 /* Utilities for building expressions.
145    Since complex expressions are recorded as symbols for use in other
146    expressions these return a symbolS * and not an expressionS *.
147    These explicitly do not take an "add_number" argument.  */
148 /* ??? For completeness' sake one might want expr_build_symbol.
149    It would just return its argument.  */
150
151 /* Build an expression for an unsigned constant.
152    The corresponding one for signed constants is missing because
153    there's currently no need for it.  One could add an unsigned_p flag
154    but that seems more clumsy.  */
155
156 symbolS *
157 expr_build_uconstant (offsetT value)
158 {
159   expressionS e;
160
161   e.X_op = O_constant;
162   e.X_add_number = value;
163   e.X_unsigned = 1;
164   return make_expr_symbol (&e);
165 }
166
167 /* Build an expression for the current location ('.').  */
168
169 symbolS *
170 expr_build_dot (void)
171 {
172   expressionS e;
173
174   current_location (&e);
175   return make_expr_symbol (&e);
176 }
177 \f
178 /* Build any floating-point literal here.
179    Also build any bignum literal here.  */
180
181 /* Seems atof_machine can backscan through generic_bignum and hit whatever
182    happens to be loaded before it in memory.  And its way too complicated
183    for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
184    and never write into the early words, thus they'll always be zero.
185    I hate Dean's floating-point code.  Bleh.  */
186 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
187
188 FLONUM_TYPE generic_floating_point_number = {
189   &generic_bignum[6],           /* low.  (JF: Was 0)  */
190   &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high.  JF: (added +6)  */
191   0,                            /* leader.  */
192   0,                            /* exponent.  */
193   0                             /* sign.  */
194 };
195
196 \f
197 static void
198 floating_constant (expressionS *expressionP)
199 {
200   /* input_line_pointer -> floating-point constant.  */
201   int error_code;
202
203   error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
204                              &generic_floating_point_number);
205
206   if (error_code)
207     {
208       if (error_code == ERROR_EXPONENT_OVERFLOW)
209         {
210           as_bad (_("bad floating-point constant: exponent overflow"));
211         }
212       else
213         {
214           as_bad (_("bad floating-point constant: unknown error code=%d"),
215                   error_code);
216         }
217     }
218   expressionP->X_op = O_big;
219   /* input_line_pointer -> just after constant, which may point to
220      whitespace.  */
221   expressionP->X_add_number = -1;
222 }
223
224 static valueT
225 generic_bignum_to_int32 (void)
226 {
227   valueT number =
228            ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
229            | (generic_bignum[0] & LITTLENUM_MASK);
230   number &= 0xffffffff;
231   return number;
232 }
233
234 #ifdef BFD64
235 static valueT
236 generic_bignum_to_int64 (void)
237 {
238   valueT number =
239     ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
240           << LITTLENUM_NUMBER_OF_BITS)
241          | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
242         << LITTLENUM_NUMBER_OF_BITS)
243        | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
244       << LITTLENUM_NUMBER_OF_BITS)
245      | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
246   return number;
247 }
248 #endif
249
250 static void
251 integer_constant (int radix, expressionS *expressionP)
252 {
253   char *start;          /* Start of number.  */
254   char *suffix = NULL;
255   char c;
256   valueT number;        /* Offset or (absolute) value.  */
257   short int digit;      /* Value of next digit in current radix.  */
258   short int maxdig = 0; /* Highest permitted digit value.  */
259   int too_many_digits = 0;      /* If we see >= this number of.  */
260   char *name;           /* Points to name of symbol.  */
261   symbolS *symbolP;     /* Points to symbol.  */
262
263   int small;                    /* True if fits in 32 bits.  */
264
265   /* May be bignum, or may fit in 32 bits.  */
266   /* Most numbers fit into 32 bits, and we want this case to be fast.
267      so we pretend it will fit into 32 bits.  If, after making up a 32
268      bit number, we realise that we have scanned more digits than
269      comfortably fit into 32 bits, we re-scan the digits coding them
270      into a bignum.  For decimal and octal numbers we are
271      conservative: Some numbers may be assumed bignums when in fact
272      they do fit into 32 bits.  Numbers of any radix can have excess
273      leading zeros: We strive to recognise this and cast them back
274      into 32 bits.  We must check that the bignum really is more than
275      32 bits, and change it back to a 32-bit number if it fits.  The
276      number we are looking for is expected to be positive, but if it
277      fits into 32 bits as an unsigned number, we let it be a 32-bit
278      number.  The cavalier approach is for speed in ordinary cases.  */
279   /* This has been extended for 64 bits.  We blindly assume that if
280      you're compiling in 64-bit mode, the target is a 64-bit machine.
281      This should be cleaned up.  */
282
283 #ifdef BFD64
284 #define valuesize 64
285 #else /* includes non-bfd case, mostly */
286 #define valuesize 32
287 #endif
288
289   if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
290     {
291       int flt = 0;
292
293       /* In MRI mode, the number may have a suffix indicating the
294          radix.  For that matter, it might actually be a floating
295          point constant.  */
296       for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
297         {
298           if (*suffix == 'e' || *suffix == 'E')
299             flt = 1;
300         }
301
302       if (suffix == input_line_pointer)
303         {
304           radix = 10;
305           suffix = NULL;
306         }
307       else
308         {
309           c = *--suffix;
310           c = TOUPPER (c);
311           /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
312              we distinguish between 'B' and 'b'.  This is the case for
313              Z80.  */
314           if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
315             radix = 2;
316           else if (c == 'D')
317             radix = 10;
318           else if (c == 'O' || c == 'Q')
319             radix = 8;
320           else if (c == 'H')
321             radix = 16;
322           else if (suffix[1] == '.' || c == 'E' || flt)
323             {
324               floating_constant (expressionP);
325               return;
326             }
327           else
328             {
329               radix = 10;
330               suffix = NULL;
331             }
332         }
333     }
334
335   switch (radix)
336     {
337     case 2:
338       maxdig = 2;
339       too_many_digits = valuesize + 1;
340       break;
341     case 8:
342       maxdig = radix = 8;
343       too_many_digits = (valuesize + 2) / 3 + 1;
344       break;
345     case 16:
346       maxdig = radix = 16;
347       too_many_digits = (valuesize + 3) / 4 + 1;
348       break;
349     case 10:
350       maxdig = radix = 10;
351       too_many_digits = (valuesize + 11) / 4; /* Very rough.  */
352     }
353 #undef valuesize
354   start = input_line_pointer;
355   c = *input_line_pointer++;
356   for (number = 0;
357        (digit = hex_value (c)) < maxdig;
358        c = *input_line_pointer++)
359     {
360       number = number * radix + digit;
361     }
362   /* c contains character after number.  */
363   /* input_line_pointer->char after c.  */
364   small = (input_line_pointer - start - 1) < too_many_digits;
365
366   if (radix == 16 && c == '_')
367     {
368       /* This is literal of the form 0x333_0_12345678_1.
369          This example is equivalent to 0x00000333000000001234567800000001.  */
370
371       int num_little_digits = 0;
372       int i;
373       input_line_pointer = start;       /* -> 1st digit.  */
374
375       know (LITTLENUM_NUMBER_OF_BITS == 16);
376
377       for (c = '_'; c == '_'; num_little_digits += 2)
378         {
379
380           /* Convert one 64-bit word.  */
381           int ndigit = 0;
382           number = 0;
383           for (c = *input_line_pointer++;
384                (digit = hex_value (c)) < maxdig;
385                c = *(input_line_pointer++))
386             {
387               number = number * radix + digit;
388               ndigit++;
389             }
390
391           /* Check for 8 digit per word max.  */
392           if (ndigit > 8)
393             as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
394
395           /* Add this chunk to the bignum.
396              Shift things down 2 little digits.  */
397           know (LITTLENUM_NUMBER_OF_BITS == 16);
398           for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
399                i >= 2;
400                i--)
401             generic_bignum[i] = generic_bignum[i - 2];
402
403           /* Add the new digits as the least significant new ones.  */
404           generic_bignum[0] = number & 0xffffffff;
405           generic_bignum[1] = number >> 16;
406         }
407
408       /* Again, c is char after number, input_line_pointer->after c.  */
409
410       if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
411         num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
412
413       gas_assert (num_little_digits >= 4);
414
415       if (num_little_digits != 8)
416         as_bad (_("a bignum with underscores must have exactly 4 words"));
417
418       /* We might have some leading zeros.  These can be trimmed to give
419          us a change to fit this constant into a small number.  */
420       while (generic_bignum[num_little_digits - 1] == 0
421              && num_little_digits > 1)
422         num_little_digits--;
423
424       if (num_little_digits <= 2)
425         {
426           /* will fit into 32 bits.  */
427           number = generic_bignum_to_int32 ();
428           small = 1;
429         }
430 #ifdef BFD64
431       else if (num_little_digits <= 4)
432         {
433           /* Will fit into 64 bits.  */
434           number = generic_bignum_to_int64 ();
435           small = 1;
436         }
437 #endif
438       else
439         {
440           small = 0;
441
442           /* Number of littlenums in the bignum.  */
443           number = num_little_digits;
444         }
445     }
446   else if (!small)
447     {
448       /* We saw a lot of digits. manufacture a bignum the hard way.  */
449       LITTLENUM_TYPE *leader;   /* -> high order littlenum of the bignum.  */
450       LITTLENUM_TYPE *pointer;  /* -> littlenum we are frobbing now.  */
451       long carry;
452
453       leader = generic_bignum;
454       generic_bignum[0] = 0;
455       generic_bignum[1] = 0;
456       generic_bignum[2] = 0;
457       generic_bignum[3] = 0;
458       input_line_pointer = start;       /* -> 1st digit.  */
459       c = *input_line_pointer++;
460       for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
461         {
462           for (pointer = generic_bignum; pointer <= leader; pointer++)
463             {
464               long work;
465
466               work = carry + radix * *pointer;
467               *pointer = work & LITTLENUM_MASK;
468               carry = work >> LITTLENUM_NUMBER_OF_BITS;
469             }
470           if (carry)
471             {
472               if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
473                 {
474                   /* Room to grow a longer bignum.  */
475                   *++leader = carry;
476                 }
477             }
478         }
479       /* Again, c is char after number.  */
480       /* input_line_pointer -> after c.  */
481       know (LITTLENUM_NUMBER_OF_BITS == 16);
482       if (leader < generic_bignum + 2)
483         {
484           /* Will fit into 32 bits.  */
485           number = generic_bignum_to_int32 ();
486           small = 1;
487         }
488 #ifdef BFD64
489       else if (leader < generic_bignum + 4)
490         {
491           /* Will fit into 64 bits.  */
492           number = generic_bignum_to_int64 ();
493           small = 1;
494         }
495 #endif
496       else
497         {
498           /* Number of littlenums in the bignum.  */
499           number = leader - generic_bignum + 1;
500         }
501     }
502
503   if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
504       && suffix != NULL
505       && input_line_pointer - 1 == suffix)
506     c = *input_line_pointer++;
507
508   if (small)
509     {
510       /* Here with number, in correct radix. c is the next char.
511          Note that unlike un*x, we allow "011f" "0x9f" to both mean
512          the same as the (conventional) "9f".
513          This is simply easier than checking for strict canonical
514          form.  Syntax sux!  */
515
516       if (LOCAL_LABELS_FB && c == 'b')
517         {
518           /* Backward ref to local label.
519              Because it is backward, expect it to be defined.  */
520           /* Construct a local label.  */
521           name = fb_label_name ((int) number, 0);
522
523           /* Seen before, or symbol is defined: OK.  */
524           symbolP = symbol_find (name);
525           if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
526             {
527               /* Local labels are never absolute.  Don't waste time
528                  checking absoluteness.  */
529               know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
530
531               expressionP->X_op = O_symbol;
532               expressionP->X_add_symbol = symbolP;
533             }
534           else
535             {
536               /* Either not seen or not defined.  */
537               /* @@ Should print out the original string instead of
538                  the parsed number.  */
539               as_bad (_("backward ref to unknown label \"%d:\""),
540                       (int) number);
541               expressionP->X_op = O_constant;
542             }
543
544           expressionP->X_add_number = 0;
545         }                       /* case 'b' */
546       else if (LOCAL_LABELS_FB && c == 'f')
547         {
548           /* Forward reference.  Expect symbol to be undefined or
549              unknown.  undefined: seen it before.  unknown: never seen
550              it before.
551
552              Construct a local label name, then an undefined symbol.
553              Don't create a xseg frag for it: caller may do that.
554              Just return it as never seen before.  */
555           name = fb_label_name ((int) number, 1);
556           symbolP = symbol_find_or_make (name);
557           /* We have no need to check symbol properties.  */
558 #ifndef many_segments
559           /* Since "know" puts its arg into a "string", we
560              can't have newlines in the argument.  */
561           know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
562 #endif
563           expressionP->X_op = O_symbol;
564           expressionP->X_add_symbol = symbolP;
565           expressionP->X_add_number = 0;
566         }                       /* case 'f' */
567       else if (LOCAL_LABELS_DOLLAR && c == '$')
568         {
569           /* If the dollar label is *currently* defined, then this is just
570              another reference to it.  If it is not *currently* defined,
571              then this is a fresh instantiation of that number, so create
572              it.  */
573
574           if (dollar_label_defined ((long) number))
575             {
576               name = dollar_label_name ((long) number, 0);
577               symbolP = symbol_find (name);
578               know (symbolP != NULL);
579             }
580           else
581             {
582               name = dollar_label_name ((long) number, 1);
583               symbolP = symbol_find_or_make (name);
584             }
585
586           expressionP->X_op = O_symbol;
587           expressionP->X_add_symbol = symbolP;
588           expressionP->X_add_number = 0;
589         }                       /* case '$' */
590       else
591         {
592           expressionP->X_op = O_constant;
593           expressionP->X_add_number = number;
594           input_line_pointer--; /* Restore following character.  */
595         }                       /* Really just a number.  */
596     }
597   else
598     {
599       /* Not a small number.  */
600       expressionP->X_op = O_big;
601       expressionP->X_add_number = number;       /* Number of littlenums.  */
602       input_line_pointer--;     /* -> char following number.  */
603     }
604 }
605
606 /* Parse an MRI multi character constant.  */
607
608 static void
609 mri_char_constant (expressionS *expressionP)
610 {
611   int i;
612
613   if (*input_line_pointer == '\''
614       && input_line_pointer[1] != '\'')
615     {
616       expressionP->X_op = O_constant;
617       expressionP->X_add_number = 0;
618       return;
619     }
620
621   /* In order to get the correct byte ordering, we must build the
622      number in reverse.  */
623   for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
624     {
625       int j;
626
627       generic_bignum[i] = 0;
628       for (j = 0; j < CHARS_PER_LITTLENUM; j++)
629         {
630           if (*input_line_pointer == '\'')
631             {
632               if (input_line_pointer[1] != '\'')
633                 break;
634               ++input_line_pointer;
635             }
636           generic_bignum[i] <<= 8;
637           generic_bignum[i] += *input_line_pointer;
638           ++input_line_pointer;
639         }
640
641       if (i < SIZE_OF_LARGE_NUMBER - 1)
642         {
643           /* If there is more than one littlenum, left justify the
644              last one to make it match the earlier ones.  If there is
645              only one, we can just use the value directly.  */
646           for (; j < CHARS_PER_LITTLENUM; j++)
647             generic_bignum[i] <<= 8;
648         }
649
650       if (*input_line_pointer == '\''
651           && input_line_pointer[1] != '\'')
652         break;
653     }
654
655   if (i < 0)
656     {
657       as_bad (_("character constant too large"));
658       i = 0;
659     }
660
661   if (i > 0)
662     {
663       int c;
664       int j;
665
666       c = SIZE_OF_LARGE_NUMBER - i;
667       for (j = 0; j < c; j++)
668         generic_bignum[j] = generic_bignum[i + j];
669       i = c;
670     }
671
672   know (LITTLENUM_NUMBER_OF_BITS == 16);
673   if (i > 2)
674     {
675       expressionP->X_op = O_big;
676       expressionP->X_add_number = i;
677     }
678   else
679     {
680       expressionP->X_op = O_constant;
681       if (i < 2)
682         expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
683       else
684         expressionP->X_add_number =
685           (((generic_bignum[1] & LITTLENUM_MASK)
686             << LITTLENUM_NUMBER_OF_BITS)
687            | (generic_bignum[0] & LITTLENUM_MASK));
688     }
689
690   /* Skip the final closing quote.  */
691   ++input_line_pointer;
692 }
693
694 /* Return an expression representing the current location.  This
695    handles the magic symbol `.'.  */
696
697 void
698 current_location (expressionS *expressionp)
699 {
700   if (now_seg == absolute_section)
701     {
702       expressionp->X_op = O_constant;
703       expressionp->X_add_number = abs_section_offset;
704     }
705   else
706     {
707       expressionp->X_op = O_symbol;
708       expressionp->X_add_symbol = symbol_temp_new_now ();
709       expressionp->X_add_number = 0;
710     }
711 }
712
713 /* In:  Input_line_pointer points to 1st char of operand, which may
714         be a space.
715
716    Out: An expressionS.
717         The operand may have been empty: in this case X_op == O_absent.
718         Input_line_pointer->(next non-blank) char after operand.  */
719
720 static segT
721 operand (expressionS *expressionP, enum expr_mode mode)
722 {
723   char c;
724   symbolS *symbolP;     /* Points to symbol.  */
725   char *name;           /* Points to name of symbol.  */
726   segT segment;
727
728   /* All integers are regarded as unsigned unless they are negated.
729      This is because the only thing which cares whether a number is
730      unsigned is the code in emit_expr which extends constants into
731      bignums.  It should only sign extend negative numbers, so that
732      something like ``.quad 0x80000000'' is not sign extended even
733      though it appears negative if valueT is 32 bits.  */
734   expressionP->X_unsigned = 1;
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       if (mode != expr_defer)
963         segment = expression (expressionP);
964       else
965         segment = deferred_expression (expressionP);
966       /* expression () will pass trailing whitespace.  */
967       if ((c == '(' && *input_line_pointer != ')')
968           || (c == '[' && *input_line_pointer != ']'))
969         as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
970       else
971         input_line_pointer++;
972       SKIP_WHITESPACE ();
973       /* Here with input_line_pointer -> char after "(...)".  */
974       return segment;
975
976 #ifdef TC_M68K
977     case 'E':
978       if (! flag_m68k_mri || *input_line_pointer != '\'')
979         goto de_fault;
980       as_bad (_("EBCDIC constants are not supported"));
981       /* Fall through.  */
982     case 'A':
983       if (! flag_m68k_mri || *input_line_pointer != '\'')
984         goto de_fault;
985       ++input_line_pointer;
986       /* Fall through.  */
987 #endif
988     case '\'':
989       if (! flag_m68k_mri)
990         {
991           /* Warning: to conform to other people's assemblers NO
992              ESCAPEMENT is permitted for a single quote.  The next
993              character, parity errors and all, is taken as the value
994              of the operand.  VERY KINKY.  */
995           expressionP->X_op = O_constant;
996           expressionP->X_add_number = *input_line_pointer++;
997           break;
998         }
999
1000       mri_char_constant (expressionP);
1001       break;
1002
1003 #ifdef TC_M68K
1004     case '"':
1005       /* Double quote is the bitwise not operator in MRI mode.  */
1006       if (! flag_m68k_mri)
1007         goto de_fault;
1008       /* Fall through.  */
1009 #endif
1010     case '~':
1011       /* '~' is permitted to start a label on the Delta.  */
1012       if (is_name_beginner (c))
1013         goto isname;
1014     case '!':
1015     case '-':
1016     case '+':
1017       {
1018 #ifdef md_operator
1019       unary:
1020 #endif
1021         operand (expressionP, mode);
1022         if (expressionP->X_op == O_constant)
1023           {
1024             /* input_line_pointer -> char after operand.  */
1025             if (c == '-')
1026               {
1027                 expressionP->X_add_number = - expressionP->X_add_number;
1028                 /* Notice: '-' may overflow: no warning is given.
1029                    This is compatible with other people's
1030                    assemblers.  Sigh.  */
1031                 expressionP->X_unsigned = 0;
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               }
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 && segment == absolute_section)
1329             {
1330               expressionP->X_op = O_constant;
1331               expressionP->X_add_number = S_GET_VALUE (symbolP);
1332             }
1333           else if (mode != expr_defer && segment == reg_section)
1334             {
1335               expressionP->X_op = O_register;
1336               expressionP->X_add_number = S_GET_VALUE (symbolP);
1337             }
1338           else
1339             {
1340               expressionP->X_op = O_symbol;
1341               expressionP->X_add_symbol = symbolP;
1342               expressionP->X_add_number = 0;
1343             }
1344           *input_line_pointer = c;
1345         }
1346       else
1347         {
1348           /* Let the target try to parse it.  Success is indicated by changing
1349              the X_op field to something other than O_absent and pointing
1350              input_line_pointer past the expression.  If it can't parse the
1351              expression, X_op and input_line_pointer should be unchanged.  */
1352           expressionP->X_op = O_absent;
1353           --input_line_pointer;
1354           md_operand (expressionP);
1355           if (expressionP->X_op == O_absent)
1356             {
1357               ++input_line_pointer;
1358               as_bad (_("bad expression"));
1359               expressionP->X_op = O_constant;
1360               expressionP->X_add_number = 0;
1361             }
1362         }
1363       break;
1364     }
1365
1366   /* It is more 'efficient' to clean up the expressionS when they are
1367      created.  Doing it here saves lines of code.  */
1368   clean_up_expression (expressionP);
1369   SKIP_WHITESPACE ();           /* -> 1st char after operand.  */
1370   know (*input_line_pointer != ' ');
1371
1372   /* The PA port needs this information.  */
1373   if (expressionP->X_add_symbol)
1374     symbol_mark_used (expressionP->X_add_symbol);
1375
1376   if (mode != expr_defer)
1377     {
1378       expressionP->X_add_symbol
1379         = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1380       expressionP->X_op_symbol
1381         = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1382     }
1383
1384   switch (expressionP->X_op)
1385     {
1386     default:
1387       return absolute_section;
1388     case O_symbol:
1389       return S_GET_SEGMENT (expressionP->X_add_symbol);
1390     case O_register:
1391       return reg_section;
1392     }
1393 }
1394 \f
1395 /* Internal.  Simplify a struct expression for use by expr ().  */
1396
1397 /* In:  address of an expressionS.
1398         The X_op field of the expressionS may only take certain values.
1399         Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1400
1401    Out: expressionS may have been modified:
1402         Unused fields zeroed to help expr ().  */
1403
1404 static void
1405 clean_up_expression (expressionS *expressionP)
1406 {
1407   switch (expressionP->X_op)
1408     {
1409     case O_illegal:
1410     case O_absent:
1411       expressionP->X_add_number = 0;
1412       /* Fall through.  */
1413     case O_big:
1414     case O_constant:
1415     case O_register:
1416       expressionP->X_add_symbol = NULL;
1417       /* Fall through.  */
1418     case O_symbol:
1419     case O_uminus:
1420     case O_bit_not:
1421       expressionP->X_op_symbol = NULL;
1422       break;
1423     default:
1424       break;
1425     }
1426 }
1427 \f
1428 /* Expression parser.  */
1429
1430 /* We allow an empty expression, and just assume (absolute,0) silently.
1431    Unary operators and parenthetical expressions are treated as operands.
1432    As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1433
1434    We used to do an aho/ullman shift-reduce parser, but the logic got so
1435    warped that I flushed it and wrote a recursive-descent parser instead.
1436    Now things are stable, would anybody like to write a fast parser?
1437    Most expressions are either register (which does not even reach here)
1438    or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1439    So I guess it doesn't really matter how inefficient more complex expressions
1440    are parsed.
1441
1442    After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1443    Also, we have consumed any leading or trailing spaces (operand does that)
1444    and done all intervening operators.
1445
1446    This returns the segment of the result, which will be
1447    absolute_section or the segment of a symbol.  */
1448
1449 #undef __
1450 #define __ O_illegal
1451 #ifndef O_SINGLE_EQ
1452 #define O_SINGLE_EQ O_illegal
1453 #endif
1454
1455 /* Maps ASCII -> operators.  */
1456 static const operatorT op_encoding[256] = {
1457   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1458   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1459
1460   __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1461   __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1462   __, __, __, __, __, __, __, __,
1463   __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1464   __, __, __, __, __, __, __, __,
1465   __, __, __, __, __, __, __, __,
1466   __, __, __, __, __, __, __, __,
1467   __, __, __,
1468 #ifdef NEED_INDEX_OPERATOR
1469   O_index,
1470 #else
1471   __,
1472 #endif
1473   __, __, O_bit_exclusive_or, __,
1474   __, __, __, __, __, __, __, __,
1475   __, __, __, __, __, __, __, __,
1476   __, __, __, __, __, __, __, __,
1477   __, __, __, __, O_bit_inclusive_or, __, __, __,
1478
1479   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1480   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1481   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1482   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1483   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1484   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1485   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1486   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1487 };
1488
1489 /* Rank Examples
1490    0    operand, (expression)
1491    1    ||
1492    2    &&
1493    3    == <> < <= >= >
1494    4    + -
1495    5    used for * / % in MRI mode
1496    6    & ^ ! |
1497    7    * / % << >>
1498    8    unary - unary ~
1499 */
1500 static operator_rankT op_rank[O_max] = {
1501   0,    /* O_illegal */
1502   0,    /* O_absent */
1503   0,    /* O_constant */
1504   0,    /* O_symbol */
1505   0,    /* O_symbol_rva */
1506   0,    /* O_register */
1507   0,    /* O_big */
1508   9,    /* O_uminus */
1509   9,    /* O_bit_not */
1510   9,    /* O_logical_not */
1511   8,    /* O_multiply */
1512   8,    /* O_divide */
1513   8,    /* O_modulus */
1514   8,    /* O_left_shift */
1515   8,    /* O_right_shift */
1516   7,    /* O_bit_inclusive_or */
1517   7,    /* O_bit_or_not */
1518   7,    /* O_bit_exclusive_or */
1519   7,    /* O_bit_and */
1520   5,    /* O_add */
1521   5,    /* O_subtract */
1522   4,    /* O_eq */
1523   4,    /* O_ne */
1524   4,    /* O_lt */
1525   4,    /* O_le */
1526   4,    /* O_ge */
1527   4,    /* O_gt */
1528   3,    /* O_logical_and */
1529   2,    /* O_logical_or */
1530   1,    /* O_index */
1531 };
1532
1533 /* Unfortunately, in MRI mode for the m68k, multiplication and
1534    division have lower precedence than the bit wise operators.  This
1535    function sets the operator precedences correctly for the current
1536    mode.  Also, MRI uses a different bit_not operator, and this fixes
1537    that as well.  */
1538
1539 #define STANDARD_MUL_PRECEDENCE 8
1540 #define MRI_MUL_PRECEDENCE 6
1541
1542 void
1543 expr_set_precedence (void)
1544 {
1545   if (flag_m68k_mri)
1546     {
1547       op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1548       op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1549       op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1550     }
1551   else
1552     {
1553       op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1554       op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1555       op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1556     }
1557 }
1558
1559 void
1560 expr_set_rank (operatorT op, operator_rankT rank)
1561 {
1562   gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1563   op_rank[op] = rank;
1564 }
1565
1566 /* Initialize the expression parser.  */
1567
1568 void
1569 expr_begin (void)
1570 {
1571   expr_set_precedence ();
1572
1573   /* Verify that X_op field is wide enough.  */
1574   {
1575     expressionS e;
1576     e.X_op = O_max;
1577     gas_assert (e.X_op == O_max);
1578   }
1579 }
1580 \f
1581 /* Return the encoding for the operator at INPUT_LINE_POINTER, and
1582    sets NUM_CHARS to the number of characters in the operator.
1583    Does not advance INPUT_LINE_POINTER.  */
1584
1585 static inline operatorT
1586 operatorf (int *num_chars)
1587 {
1588   int c;
1589   operatorT ret;
1590
1591   c = *input_line_pointer & 0xff;
1592   *num_chars = 1;
1593
1594   if (is_end_of_line[c])
1595     return O_illegal;
1596
1597 #ifdef md_operator
1598   if (is_name_beginner (c))
1599     {
1600       char *name = input_line_pointer;
1601       char ec = get_symbol_end ();
1602
1603       ret = md_operator (name, 2, &ec);
1604       switch (ret)
1605         {
1606         case O_absent:
1607           *input_line_pointer = ec;
1608           input_line_pointer = name;
1609           break;
1610         case O_uminus:
1611         case O_bit_not:
1612         case O_logical_not:
1613           as_bad (_("invalid use of operator \"%s\""), name);
1614           ret = O_illegal;
1615           /* FALLTHROUGH */
1616         default:
1617           *input_line_pointer = ec;
1618           *num_chars = input_line_pointer - name;
1619           input_line_pointer = name;
1620           return ret;
1621         }
1622     }
1623 #endif
1624
1625   switch (c)
1626     {
1627     default:
1628       ret = op_encoding[c];
1629 #ifdef md_operator
1630       if (ret == O_illegal)
1631         {
1632           char *start = input_line_pointer;
1633
1634           ret = md_operator (NULL, 2, NULL);
1635           if (ret != O_illegal)
1636             *num_chars = input_line_pointer - start;
1637           input_line_pointer = start;
1638         }
1639 #endif
1640       return ret;
1641
1642     case '+':
1643     case '-':
1644       return op_encoding[c];
1645
1646     case '<':
1647       switch (input_line_pointer[1])
1648         {
1649         default:
1650           return op_encoding[c];
1651         case '<':
1652           ret = O_left_shift;
1653           break;
1654         case '>':
1655           ret = O_ne;
1656           break;
1657         case '=':
1658           ret = O_le;
1659           break;
1660         }
1661       *num_chars = 2;
1662       return ret;
1663
1664     case '=':
1665       if (input_line_pointer[1] != '=')
1666         return op_encoding[c];
1667
1668       *num_chars = 2;
1669       return O_eq;
1670
1671     case '>':
1672       switch (input_line_pointer[1])
1673         {
1674         default:
1675           return op_encoding[c];
1676         case '>':
1677           ret = O_right_shift;
1678           break;
1679         case '=':
1680           ret = O_ge;
1681           break;
1682         }
1683       *num_chars = 2;
1684       return ret;
1685
1686     case '!':
1687       switch (input_line_pointer[1])
1688         {
1689         case '!':
1690           /* We accept !! as equivalent to ^ for MRI compatibility. */
1691           *num_chars = 2;
1692           return O_bit_exclusive_or;
1693         case '=':
1694           /* We accept != as equivalent to <>.  */
1695           *num_chars = 2;
1696           return O_ne;
1697         default:
1698           if (flag_m68k_mri)
1699             return O_bit_inclusive_or;
1700           return op_encoding[c];
1701         }
1702
1703     case '|':
1704       if (input_line_pointer[1] != '|')
1705         return op_encoding[c];
1706
1707       *num_chars = 2;
1708       return O_logical_or;
1709
1710     case '&':
1711       if (input_line_pointer[1] != '&')
1712         return op_encoding[c];
1713
1714       *num_chars = 2;
1715       return O_logical_and;
1716     }
1717
1718   /* NOTREACHED  */
1719 }
1720
1721 /* Parse an expression.  */
1722
1723 segT
1724 expr (int rankarg,              /* Larger # is higher rank.  */
1725       expressionS *resultP,     /* Deliver result here.  */
1726       enum expr_mode mode       /* Controls behavior.  */)
1727 {
1728   operator_rankT rank = (operator_rankT) rankarg;
1729   segT retval;
1730   expressionS right;
1731   operatorT op_left;
1732   operatorT op_right;
1733   int op_chars;
1734
1735   know (rankarg >= 0);
1736
1737   /* Save the value of dot for the fixup code.  */
1738   if (rank == 0)
1739     dot_value = frag_now_fix ();
1740
1741   retval = operand (resultP, mode);
1742
1743   /* operand () gobbles spaces.  */
1744   know (*input_line_pointer != ' ');
1745
1746   op_left = operatorf (&op_chars);
1747   while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1748     {
1749       segT rightseg;
1750       bfd_vma frag_off;
1751
1752       input_line_pointer += op_chars;   /* -> after operator.  */
1753
1754       right.X_md = 0;
1755       rightseg = expr (op_rank[(int) op_left], &right, mode);
1756       if (right.X_op == O_absent)
1757         {
1758           as_warn (_("missing operand; zero assumed"));
1759           right.X_op = O_constant;
1760           right.X_add_number = 0;
1761           right.X_add_symbol = NULL;
1762           right.X_op_symbol = NULL;
1763         }
1764
1765       know (*input_line_pointer != ' ');
1766
1767       if (op_left == O_index)
1768         {
1769           if (*input_line_pointer != ']')
1770             as_bad ("missing right bracket");
1771           else
1772             {
1773               ++input_line_pointer;
1774               SKIP_WHITESPACE ();
1775             }
1776         }
1777
1778       op_right = operatorf (&op_chars);
1779
1780       know (op_right == O_illegal || op_left == O_index
1781             || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1782       know ((int) op_left >= (int) O_multiply);
1783 #ifndef md_operator
1784       know ((int) op_left <= (int) O_index);
1785 #else
1786       know ((int) op_left < (int) O_max);
1787 #endif
1788
1789       /* input_line_pointer->after right-hand quantity.  */
1790       /* left-hand quantity in resultP.  */
1791       /* right-hand quantity in right.  */
1792       /* operator in op_left.  */
1793
1794       if (resultP->X_op == O_big)
1795         {
1796           if (resultP->X_add_number > 0)
1797             as_warn (_("left operand is a bignum; integer 0 assumed"));
1798           else
1799             as_warn (_("left operand is a float; integer 0 assumed"));
1800           resultP->X_op = O_constant;
1801           resultP->X_add_number = 0;
1802           resultP->X_add_symbol = NULL;
1803           resultP->X_op_symbol = NULL;
1804         }
1805       if (right.X_op == O_big)
1806         {
1807           if (right.X_add_number > 0)
1808             as_warn (_("right operand is a bignum; integer 0 assumed"));
1809           else
1810             as_warn (_("right operand is a float; integer 0 assumed"));
1811           right.X_op = O_constant;
1812           right.X_add_number = 0;
1813           right.X_add_symbol = NULL;
1814           right.X_op_symbol = NULL;
1815         }
1816
1817       /* Optimize common cases.  */
1818 #ifdef md_optimize_expr
1819       if (md_optimize_expr (resultP, op_left, &right))
1820         {
1821           /* Skip.  */
1822           ;
1823         }
1824       else
1825 #endif
1826 #ifndef md_register_arithmetic
1827 # define md_register_arithmetic 1
1828 #endif
1829       if (op_left == O_add && right.X_op == O_constant
1830           && (md_register_arithmetic || resultP->X_op != O_register))
1831         {
1832           /* X + constant.  */
1833           resultP->X_add_number += right.X_add_number;
1834         }
1835       /* This case comes up in PIC code.  */
1836       else if (op_left == O_subtract
1837                && right.X_op == O_symbol
1838                && resultP->X_op == O_symbol
1839                && retval == rightseg
1840 #ifdef md_allow_local_subtract
1841                && md_allow_local_subtract (resultP, & right, rightseg)
1842 #endif
1843                && (SEG_NORMAL (rightseg)
1844                    || right.X_add_symbol == resultP->X_add_symbol)
1845                && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1846                                        symbol_get_frag (right.X_add_symbol),
1847                                        &frag_off))
1848         {
1849           resultP->X_add_number -= right.X_add_number;
1850           resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
1851           resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1852                                     - S_GET_VALUE (right.X_add_symbol));
1853           resultP->X_op = O_constant;
1854           resultP->X_add_symbol = 0;
1855         }
1856       else if (op_left == O_subtract && right.X_op == O_constant
1857                && (md_register_arithmetic || resultP->X_op != O_register))
1858         {
1859           /* X - constant.  */
1860           resultP->X_add_number -= right.X_add_number;
1861         }
1862       else if (op_left == O_add && resultP->X_op == O_constant
1863                && (md_register_arithmetic || right.X_op != O_register))
1864         {
1865           /* Constant + X.  */
1866           resultP->X_op = right.X_op;
1867           resultP->X_add_symbol = right.X_add_symbol;
1868           resultP->X_op_symbol = right.X_op_symbol;
1869           resultP->X_add_number += right.X_add_number;
1870           retval = rightseg;
1871         }
1872       else if (resultP->X_op == O_constant && right.X_op == O_constant)
1873         {
1874           /* Constant OP constant.  */
1875           offsetT v = right.X_add_number;
1876           if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1877             {
1878               as_warn (_("division by zero"));
1879               v = 1;
1880             }
1881           if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1882               && (op_left == O_left_shift || op_left == O_right_shift))
1883             {
1884               as_warn_value_out_of_range (_("shift count"), v, 0,
1885                                           sizeof(valueT) * CHAR_BIT - 1,
1886                                           NULL, 0);
1887               resultP->X_add_number = v = 0;
1888             }
1889           switch (op_left)
1890             {
1891             default:                    goto general;
1892             case O_multiply:            resultP->X_add_number *= v; break;
1893             case O_divide:              resultP->X_add_number /= v; break;
1894             case O_modulus:             resultP->X_add_number %= v; break;
1895             case O_left_shift:          resultP->X_add_number <<= v; break;
1896             case O_right_shift:
1897               /* We always use unsigned shifts, to avoid relying on
1898                  characteristics of the compiler used to compile gas.  */
1899               resultP->X_add_number =
1900                 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1901               break;
1902             case O_bit_inclusive_or:    resultP->X_add_number |= v; break;
1903             case O_bit_or_not:          resultP->X_add_number |= ~v; break;
1904             case O_bit_exclusive_or:    resultP->X_add_number ^= v; break;
1905             case O_bit_and:             resultP->X_add_number &= v; break;
1906               /* Constant + constant (O_add) is handled by the
1907                  previous if statement for constant + X, so is omitted
1908                  here.  */
1909             case O_subtract:            resultP->X_add_number -= v; break;
1910             case O_eq:
1911               resultP->X_add_number =
1912                 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1913               break;
1914             case O_ne:
1915               resultP->X_add_number =
1916                 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1917               break;
1918             case O_lt:
1919               resultP->X_add_number =
1920                 resultP->X_add_number <  v ? ~ (offsetT) 0 : 0;
1921               break;
1922             case O_le:
1923               resultP->X_add_number =
1924                 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1925               break;
1926             case O_ge:
1927               resultP->X_add_number =
1928                 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1929               break;
1930             case O_gt:
1931               resultP->X_add_number =
1932                 resultP->X_add_number >  v ? ~ (offsetT) 0 : 0;
1933               break;
1934             case O_logical_and:
1935               resultP->X_add_number = resultP->X_add_number && v;
1936               break;
1937             case O_logical_or:
1938               resultP->X_add_number = resultP->X_add_number || v;
1939               break;
1940             }
1941         }
1942       else if (resultP->X_op == O_symbol
1943                && right.X_op == O_symbol
1944                && (op_left == O_add
1945                    || op_left == O_subtract
1946                    || (resultP->X_add_number == 0
1947                        && right.X_add_number == 0)))
1948         {
1949           /* Symbol OP symbol.  */
1950           resultP->X_op = op_left;
1951           resultP->X_op_symbol = right.X_add_symbol;
1952           if (op_left == O_add)
1953             resultP->X_add_number += right.X_add_number;
1954           else if (op_left == O_subtract)
1955             {
1956               resultP->X_add_number -= right.X_add_number;
1957               if (retval == rightseg && SEG_NORMAL (retval))
1958                 {
1959                   retval = absolute_section;
1960                   rightseg = absolute_section;
1961                 }
1962             }
1963         }
1964       else
1965         {
1966         general:
1967           /* The general case.  */
1968           resultP->X_add_symbol = make_expr_symbol (resultP);
1969           resultP->X_op_symbol = make_expr_symbol (&right);
1970           resultP->X_op = op_left;
1971           resultP->X_add_number = 0;
1972           resultP->X_unsigned = 1;
1973         }
1974
1975       if (retval != rightseg)
1976         {
1977           if (retval == undefined_section)
1978             ;
1979           else if (rightseg == undefined_section)
1980             retval = rightseg;
1981           else if (retval == expr_section)
1982             ;
1983           else if (rightseg == expr_section)
1984             retval = rightseg;
1985           else if (retval == reg_section)
1986             ;
1987           else if (rightseg == reg_section)
1988             retval = rightseg;
1989           else if (rightseg == absolute_section)
1990             ;
1991           else if (retval == absolute_section)
1992             retval = rightseg;
1993 #ifdef DIFF_EXPR_OK
1994           else if (op_left == O_subtract)
1995             ;
1996 #endif
1997           else
1998             as_bad (_("operation combines symbols in different segments"));
1999         }
2000
2001       op_left = op_right;
2002     }                           /* While next operator is >= this rank.  */
2003
2004   /* The PA port needs this information.  */
2005   if (resultP->X_add_symbol)
2006     symbol_mark_used (resultP->X_add_symbol);
2007
2008   if (rank == 0 && mode == expr_evaluate)
2009     resolve_expression (resultP);
2010
2011   return resultP->X_op == O_constant ? absolute_section : retval;
2012 }
2013
2014 /* Resolve an expression without changing any symbols/sub-expressions
2015    used.  */
2016
2017 int
2018 resolve_expression (expressionS *expressionP)
2019 {
2020   /* Help out with CSE.  */
2021   valueT final_val = expressionP->X_add_number;
2022   symbolS *add_symbol = expressionP->X_add_symbol;
2023   symbolS *orig_add_symbol = add_symbol;
2024   symbolS *op_symbol = expressionP->X_op_symbol;
2025   operatorT op = expressionP->X_op;
2026   valueT left, right;
2027   segT seg_left, seg_right;
2028   fragS *frag_left, *frag_right;
2029   bfd_vma frag_off;
2030
2031   switch (op)
2032     {
2033     default:
2034       return 0;
2035
2036     case O_constant:
2037     case O_register:
2038       left = 0;
2039       break;
2040
2041     case O_symbol:
2042     case O_symbol_rva:
2043       if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2044         return 0;
2045
2046       break;
2047
2048     case O_uminus:
2049     case O_bit_not:
2050     case O_logical_not:
2051       if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2052         return 0;
2053
2054       if (seg_left != absolute_section)
2055         return 0;
2056
2057       if (op == O_logical_not)
2058         left = !left;
2059       else if (op == O_uminus)
2060         left = -left;
2061       else
2062         left = ~left;
2063       op = O_constant;
2064       break;
2065
2066     case O_multiply:
2067     case O_divide:
2068     case O_modulus:
2069     case O_left_shift:
2070     case O_right_shift:
2071     case O_bit_inclusive_or:
2072     case O_bit_or_not:
2073     case O_bit_exclusive_or:
2074     case O_bit_and:
2075     case O_add:
2076     case O_subtract:
2077     case O_eq:
2078     case O_ne:
2079     case O_lt:
2080     case O_le:
2081     case O_ge:
2082     case O_gt:
2083     case O_logical_and:
2084     case O_logical_or:
2085       if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2086           || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2087         return 0;
2088
2089       /* Simplify addition or subtraction of a constant by folding the
2090          constant into X_add_number.  */
2091       if (op == O_add)
2092         {
2093           if (seg_right == absolute_section)
2094             {
2095               final_val += right;
2096               op = O_symbol;
2097               break;
2098             }
2099           else if (seg_left == absolute_section)
2100             {
2101               final_val += left;
2102               left = right;
2103               seg_left = seg_right;
2104               add_symbol = op_symbol;
2105               orig_add_symbol = expressionP->X_op_symbol;
2106               op = O_symbol;
2107               break;
2108             }
2109         }
2110       else if (op == O_subtract)
2111         {
2112           if (seg_right == absolute_section)
2113             {
2114               final_val -= right;
2115               op = O_symbol;
2116               break;
2117             }
2118         }
2119
2120       /* Equality and non-equality tests are permitted on anything.
2121          Subtraction, and other comparison operators are permitted if
2122          both operands are in the same section.
2123          Shifts by constant zero are permitted on anything.
2124          Multiplies, bit-ors, and bit-ands with constant zero are
2125          permitted on anything.
2126          Multiplies and divides by constant one are permitted on
2127          anything.
2128          Binary operations with both operands being the same register
2129          or undefined symbol are permitted if the result doesn't depend
2130          on the input value.
2131          Otherwise, both operands must be absolute.  We already handled
2132          the case of addition or subtraction of a constant above.  */
2133       frag_off = 0;
2134       if (!(seg_left == absolute_section
2135                && seg_right == absolute_section)
2136           && !(op == O_eq || op == O_ne)
2137           && !((op == O_subtract
2138                 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
2139                && seg_left == seg_right
2140                && (finalize_syms
2141                    || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2142                && (seg_left != reg_section || left == right)
2143                && (seg_left != undefined_section || add_symbol == op_symbol)))
2144         {
2145           if ((seg_left == absolute_section && left == 0)
2146               || (seg_right == absolute_section && right == 0))
2147             {
2148               if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2149                 {
2150                   if (!(seg_right == absolute_section && right == 0))
2151                     {
2152                       seg_left = seg_right;
2153                       left = right;
2154                       add_symbol = op_symbol;
2155                       orig_add_symbol = expressionP->X_op_symbol;
2156                     }
2157                   op = O_symbol;
2158                   break;
2159                 }
2160               else if (op == O_left_shift || op == O_right_shift)
2161                 {
2162                   if (!(seg_left == absolute_section && left == 0))
2163                     {
2164                       op = O_symbol;
2165                       break;
2166                     }
2167                 }
2168               else if (op != O_multiply
2169                        && op != O_bit_or_not && op != O_bit_and)
2170                 return 0;
2171             }
2172           else if (op == O_multiply
2173                    && seg_left == absolute_section && left == 1)
2174             {
2175               seg_left = seg_right;
2176               left = right;
2177               add_symbol = op_symbol;
2178               orig_add_symbol = expressionP->X_op_symbol;
2179               op = O_symbol;
2180               break;
2181             }
2182           else if ((op == O_multiply || op == O_divide)
2183                    && seg_right == absolute_section && right == 1)
2184             {
2185               op = O_symbol;
2186               break;
2187             }
2188           else if (!(left == right
2189                      && ((seg_left == reg_section && seg_right == reg_section)
2190                          || (seg_left == undefined_section
2191                              && seg_right == undefined_section
2192                              && add_symbol == op_symbol))))
2193             return 0;
2194           else if (op == O_bit_and || op == O_bit_inclusive_or)
2195             {
2196               op = O_symbol;
2197               break;
2198             }
2199           else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2200             return 0;
2201         }
2202
2203       right += frag_off / OCTETS_PER_BYTE;
2204       switch (op)
2205         {
2206         case O_add:                     left += right; break;
2207         case O_subtract:                left -= right; break;
2208         case O_multiply:                left *= right; break;
2209         case O_divide:
2210           if (right == 0)
2211             return 0;
2212           left = (offsetT) left / (offsetT) right;
2213           break;
2214         case O_modulus:
2215           if (right == 0)
2216             return 0;
2217           left = (offsetT) left % (offsetT) right;
2218           break;
2219         case O_left_shift:              left <<= right; break;
2220         case O_right_shift:             left >>= right; break;
2221         case O_bit_inclusive_or:        left |= right; break;
2222         case O_bit_or_not:              left |= ~right; break;
2223         case O_bit_exclusive_or:        left ^= right; break;
2224         case O_bit_and:                 left &= right; break;
2225         case O_eq:
2226         case O_ne:
2227           left = (left == right
2228                   && seg_left == seg_right
2229                   && (finalize_syms || frag_left == frag_right)
2230                   && (seg_left != undefined_section
2231                       || add_symbol == op_symbol)
2232                   ? ~ (valueT) 0 : 0);
2233           if (op == O_ne)
2234             left = ~left;
2235           break;
2236         case O_lt:
2237           left = (offsetT) left <  (offsetT) right ? ~ (valueT) 0 : 0;
2238           break;
2239         case O_le:
2240           left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2241           break;
2242         case O_ge:
2243           left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2244           break;
2245         case O_gt:
2246           left = (offsetT) left >  (offsetT) right ? ~ (valueT) 0 : 0;
2247           break;
2248         case O_logical_and:     left = left && right; break;
2249         case O_logical_or:      left = left || right; break;
2250         default:                abort ();
2251         }
2252
2253       op = O_constant;
2254       break;
2255     }
2256
2257   if (op == O_symbol)
2258     {
2259       if (seg_left == absolute_section)
2260         op = O_constant;
2261       else if (seg_left == reg_section && final_val == 0)
2262         op = O_register;
2263       else if (!symbol_same_p (add_symbol, orig_add_symbol))
2264         final_val += left;
2265       expressionP->X_add_symbol = add_symbol;
2266     }
2267   expressionP->X_op = op;
2268
2269   if (op == O_constant || op == O_register)
2270     final_val += left;
2271   expressionP->X_add_number = final_val;
2272
2273   return 1;
2274 }
2275 \f
2276 /* This lives here because it belongs equally in expr.c & read.c.
2277    expr.c is just a branch office read.c anyway, and putting it
2278    here lessens the crowd at read.c.
2279
2280    Assume input_line_pointer is at start of symbol name.
2281    Advance input_line_pointer past symbol name.
2282    Turn that character into a '\0', returning its former value.
2283    This allows a string compare (RMS wants symbol names to be strings)
2284    of the symbol name.
2285    There will always be a char following symbol name, because all good
2286    lines end in end-of-line.  */
2287
2288 char
2289 get_symbol_end (void)
2290 {
2291   char c;
2292
2293   /* We accept \001 in a name in case this is being called with a
2294      constructed string.  */
2295   if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2296     {
2297       while (is_part_of_name (c = *input_line_pointer++)
2298              || c == '\001')
2299         ;
2300       if (is_name_ender (c))
2301         c = *input_line_pointer++;
2302     }
2303   *--input_line_pointer = 0;
2304   return (c);
2305 }
2306
2307 unsigned int
2308 get_single_number (void)
2309 {
2310   expressionS exp;
2311   operand (&exp, expr_normal);
2312   return exp.X_add_number;
2313 }