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