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