This commit was generated by cvs2svn to track changes on a CVS vendor
[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 == 'f' || *cp == 'b')
928                       /* looks like a difference expression */
929                       goto is_0f_label;
930                     else if (cp == input_line_pointer + 1)
931                       /* No characters has been accepted -- looks like
932                          end of operand. */
933                       goto is_0f_label;
934                     else
935                       goto is_0f_float;
936                   default:
937                     as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
938                               r);
939                   }
940               }
941
942               /* Okay, now we've sorted it out.  We resume at one of these
943                  two labels, depending on what we've decided we're probably
944                  looking at.  */
945             is_0f_label:
946               input_line_pointer--;
947               integer_constant (10, expressionP);
948               break;
949
950             is_0f_float:
951               /* fall through */
952               ;
953             }
954
955         case 'd':
956         case 'D':
957           if (flag_m68k_mri)
958             {
959               integer_constant (0, expressionP);
960               break;
961             }
962           /* Fall through.  */
963         case 'F':
964         case 'r':
965         case 'e':
966         case 'E':
967         case 'g':
968         case 'G':
969           input_line_pointer++;
970           floating_constant (expressionP);
971           expressionP->X_add_number =
972             - (isupper ((unsigned char) c) ? tolower (c) : c);
973           break;
974
975         case '$':
976           if (LOCAL_LABELS_DOLLAR)
977             {
978               integer_constant (10, expressionP);
979               break;
980             }
981           else
982             goto default_case;
983         }
984
985       break;
986
987     case '(':
988     case '[':
989       /* didn't begin with digit & not a name */
990       segment = expression (expressionP);
991       /* Expression() will pass trailing whitespace */
992       if ((c == '(' && *input_line_pointer++ != ')')
993           || (c == '[' && *input_line_pointer++ != ']'))
994         {
995           as_bad (_("Missing ')' assumed"));
996           input_line_pointer--;
997         }
998       SKIP_WHITESPACE ();
999       /* here with input_line_pointer->char after "(...)" */
1000       return segment;
1001
1002     case 'E':
1003       if (! flag_m68k_mri || *input_line_pointer != '\'')
1004         goto de_fault;
1005       as_bad (_("EBCDIC constants are not supported"));
1006       /* Fall through.  */
1007     case 'A':
1008       if (! flag_m68k_mri || *input_line_pointer != '\'')
1009         goto de_fault;
1010       ++input_line_pointer;
1011       /* Fall through.  */
1012     case '\'':
1013       if (! flag_m68k_mri)
1014         {
1015           /* Warning: to conform to other people's assemblers NO
1016              ESCAPEMENT is permitted for a single quote. The next
1017              character, parity errors and all, is taken as the value
1018              of the operand. VERY KINKY.  */
1019           expressionP->X_op = O_constant;
1020           expressionP->X_add_number = *input_line_pointer++;
1021           break;
1022         }
1023
1024       mri_char_constant (expressionP);
1025       break;
1026
1027     case '+':
1028       (void) operand (expressionP);
1029       break;
1030
1031     case '"':
1032       /* Double quote is the bitwise not operator in MRI mode.  */
1033       if (! flag_m68k_mri)
1034         goto de_fault;
1035       /* Fall through.  */
1036     case '~':
1037       /* ~ is permitted to start a label on the Delta.  */
1038       if (is_name_beginner (c))
1039         goto isname;
1040     case '!':
1041     case '-':
1042       {
1043         operand (expressionP);
1044         if (expressionP->X_op == O_constant)
1045           {
1046             /* input_line_pointer -> char after operand */
1047             if (c == '-')
1048               {
1049                 expressionP->X_add_number = - expressionP->X_add_number;
1050                 /* Notice: '-' may overflow: no warning is given. This is
1051                    compatible with other people's assemblers. Sigh.  */
1052                 expressionP->X_unsigned = 0;
1053               }
1054             else if (c == '~' || c == '"')
1055               expressionP->X_add_number = ~ expressionP->X_add_number;
1056             else
1057               expressionP->X_add_number = ! expressionP->X_add_number;
1058           }
1059         else if (expressionP->X_op != O_illegal
1060                  && expressionP->X_op != O_absent)
1061           {
1062             expressionP->X_add_symbol = make_expr_symbol (expressionP);
1063             if (c == '-')
1064               expressionP->X_op = O_uminus;
1065             else if (c == '~' || c == '"')
1066               expressionP->X_op = O_bit_not;
1067             else
1068               expressionP->X_op = O_logical_not;
1069             expressionP->X_add_number = 0;
1070           }
1071         else
1072           as_warn (_("Unary operator %c ignored because bad operand follows"),
1073                    c);
1074       }
1075       break;
1076
1077     case '$':
1078       /* $ is the program counter when in MRI mode, or when DOLLAR_DOT
1079          is defined.  */
1080 #ifndef DOLLAR_DOT
1081       if (! flag_m68k_mri)
1082         goto de_fault;
1083 #endif
1084       if (flag_m68k_mri && hex_p (*input_line_pointer))
1085         {
1086           /* In MRI mode, $ is also used as the prefix for a
1087              hexadecimal constant.  */
1088           integer_constant (16, expressionP);
1089           break;
1090         }
1091
1092       if (is_part_of_name (*input_line_pointer))
1093         goto isname;
1094
1095       current_location (expressionP);
1096       break;
1097
1098     case '.':
1099       if (!is_part_of_name (*input_line_pointer))
1100         {
1101           current_location (expressionP);
1102           break;
1103         }
1104       else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1105                 && ! is_part_of_name (input_line_pointer[8]))
1106                || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1107                    && ! is_part_of_name (input_line_pointer[7])))
1108         {
1109           int start;
1110
1111           start = (input_line_pointer[1] == 't'
1112                    || input_line_pointer[1] == 'T');
1113           input_line_pointer += start ? 8 : 7;
1114           SKIP_WHITESPACE ();
1115           if (*input_line_pointer != '(')
1116             as_bad (_("syntax error in .startof. or .sizeof."));
1117           else
1118             {
1119               char *buf;
1120
1121               ++input_line_pointer;
1122               SKIP_WHITESPACE ();
1123               name = input_line_pointer;
1124               c = get_symbol_end ();
1125
1126               buf = (char *) xmalloc (strlen (name) + 10);
1127               if (start)
1128                 sprintf (buf, ".startof.%s", name);
1129               else
1130                 sprintf (buf, ".sizeof.%s", name);
1131               symbolP = symbol_make (buf);
1132               free (buf);
1133
1134               expressionP->X_op = O_symbol;
1135               expressionP->X_add_symbol = symbolP;
1136               expressionP->X_add_number = 0;
1137
1138               *input_line_pointer = c;
1139               SKIP_WHITESPACE ();
1140               if (*input_line_pointer != ')')
1141                 as_bad (_("syntax error in .startof. or .sizeof."));
1142               else
1143                 ++input_line_pointer;
1144             }
1145           break;
1146         }
1147       else
1148         {
1149           goto isname;
1150         }
1151     case ',':
1152     case '\n':
1153     case '\0':
1154     eol:
1155       /* can't imagine any other kind of operand */
1156       expressionP->X_op = O_absent;
1157       input_line_pointer--;
1158       break;
1159
1160     case '%':
1161       if (! flag_m68k_mri)
1162         goto de_fault;
1163       integer_constant (2, expressionP);
1164       break;
1165
1166     case '@':
1167       if (! flag_m68k_mri)
1168         goto de_fault;
1169       integer_constant (8, expressionP);
1170       break;
1171
1172     case ':':
1173       if (! flag_m68k_mri)
1174         goto de_fault;
1175
1176       /* In MRI mode, this is a floating point constant represented
1177          using hexadecimal digits.  */
1178
1179       ++input_line_pointer;
1180       integer_constant (16, expressionP);
1181       break;
1182
1183     case '*':
1184       if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1185         goto de_fault;
1186
1187       current_location (expressionP);
1188       break;
1189
1190     default:
1191     de_fault:
1192       if (is_end_of_line[(unsigned char) c])
1193         goto eol;
1194       if (is_name_beginner (c)) /* here if did not begin with a digit */
1195         {
1196           /*
1197            * Identifier begins here.
1198            * This is kludged for speed, so code is repeated.
1199            */
1200         isname:
1201           name = --input_line_pointer;
1202           c = get_symbol_end ();
1203
1204 #ifdef md_parse_name
1205           /* This is a hook for the backend to parse certain names
1206              specially in certain contexts.  If a name always has a
1207              specific value, it can often be handled by simply
1208              entering it in the symbol table.  */
1209           if (md_parse_name (name, expressionP))
1210             {
1211               *input_line_pointer = c;
1212               break;
1213             }
1214 #endif
1215
1216 #ifdef TC_I960
1217           /* The MRI i960 assembler permits
1218                  lda sizeof code,g13
1219              FIXME: This should use md_parse_name.  */
1220           if (flag_mri
1221               && (strcasecmp (name, "sizeof") == 0
1222                   || strcasecmp (name, "startof") == 0))
1223             {
1224               int start;
1225               char *buf;
1226
1227               start = (name[1] == 't'
1228                        || name[1] == 'T');
1229
1230               *input_line_pointer = c;
1231               SKIP_WHITESPACE ();
1232
1233               name = input_line_pointer;
1234               c = get_symbol_end ();
1235
1236               buf = (char *) xmalloc (strlen (name) + 10);
1237               if (start)
1238                 sprintf (buf, ".startof.%s", name);
1239               else
1240                 sprintf (buf, ".sizeof.%s", name);
1241               symbolP = symbol_make (buf);
1242               free (buf);
1243
1244               expressionP->X_op = O_symbol;
1245               expressionP->X_add_symbol = symbolP;
1246               expressionP->X_add_number = 0;
1247
1248               *input_line_pointer = c;
1249               SKIP_WHITESPACE ();
1250
1251               break;
1252             }         
1253 #endif
1254
1255           symbolP = symbol_find_or_make (name);
1256
1257           /* If we have an absolute symbol or a reg, then we know its
1258              value now.  */
1259           segment = S_GET_SEGMENT (symbolP);
1260           if (segment == absolute_section)
1261             {
1262               expressionP->X_op = O_constant;
1263               expressionP->X_add_number = S_GET_VALUE (symbolP);
1264             }
1265           else if (segment == reg_section)
1266             {
1267               expressionP->X_op = O_register;
1268               expressionP->X_add_number = S_GET_VALUE (symbolP);
1269             }
1270           else
1271             {
1272               expressionP->X_op = O_symbol;
1273               expressionP->X_add_symbol = symbolP;
1274               expressionP->X_add_number = 0;
1275             }
1276           *input_line_pointer = c;
1277         }
1278       else
1279         {
1280           /* Let the target try to parse it.  Success is indicated by changing
1281              the X_op field to something other than O_absent and pointing
1282              input_line_pointer passed the expression.  If it can't parse the
1283              expression, X_op and input_line_pointer should be unchanged.  */
1284           expressionP->X_op = O_absent;
1285           --input_line_pointer;
1286           md_operand (expressionP);
1287           if (expressionP->X_op == O_absent)
1288             {
1289               ++input_line_pointer;
1290               as_bad (_("Bad expression"));
1291               expressionP->X_op = O_constant;
1292               expressionP->X_add_number = 0;
1293             }
1294         }
1295       break;
1296     }
1297
1298   /*
1299    * It is more 'efficient' to clean up the expressionS when they are created.
1300    * Doing it here saves lines of code.
1301    */
1302   clean_up_expression (expressionP);
1303   SKIP_WHITESPACE ();           /*->1st char after operand. */
1304   know (*input_line_pointer != ' ');
1305
1306   /* The PA port needs this information.  */
1307   if (expressionP->X_add_symbol)
1308     expressionP->X_add_symbol->sy_used = 1;
1309
1310   switch (expressionP->X_op)
1311     {
1312     default:
1313       return absolute_section;
1314     case O_symbol:
1315       return S_GET_SEGMENT (expressionP->X_add_symbol);
1316     case O_register:
1317       return reg_section;
1318     }
1319 }                               /* operand() */
1320 \f
1321 /* Internal. Simplify a struct expression for use by expr() */
1322
1323 /*
1324  * In:  address of a expressionS.
1325  *      The X_op field of the expressionS may only take certain values.
1326  *      Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1327  * Out: expressionS may have been modified:
1328  *      'foo-foo' symbol references cancelled to 0,
1329  *              which changes X_op from O_subtract to O_constant.
1330  *      Unused fields zeroed to help expr().
1331  */
1332
1333 static void
1334 clean_up_expression (expressionP)
1335      expressionS *expressionP;
1336 {
1337   switch (expressionP->X_op)
1338     {
1339     case O_illegal:
1340     case O_absent:
1341       expressionP->X_add_number = 0;
1342       /* Fall through.  */
1343     case O_big:
1344     case O_constant:
1345     case O_register:
1346       expressionP->X_add_symbol = NULL;
1347       /* Fall through.  */
1348     case O_symbol:
1349     case O_uminus:
1350     case O_bit_not:
1351       expressionP->X_op_symbol = NULL;
1352       break;
1353     case O_subtract:
1354       if (expressionP->X_op_symbol == expressionP->X_add_symbol
1355           || ((expressionP->X_op_symbol->sy_frag
1356                == expressionP->X_add_symbol->sy_frag)
1357               && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
1358               && (S_GET_VALUE (expressionP->X_op_symbol)
1359                   == S_GET_VALUE (expressionP->X_add_symbol))))
1360         {
1361           addressT diff = (S_GET_VALUE (expressionP->X_add_symbol)
1362                            - S_GET_VALUE (expressionP->X_op_symbol));
1363
1364           expressionP->X_op = O_constant;
1365           expressionP->X_add_symbol = NULL;
1366           expressionP->X_op_symbol = NULL;
1367           expressionP->X_add_number += diff;
1368         }
1369       break;
1370     default:
1371       break;
1372     }
1373 }
1374 \f
1375 /* Expression parser. */
1376
1377 /*
1378  * We allow an empty expression, and just assume (absolute,0) silently.
1379  * Unary operators and parenthetical expressions are treated as operands.
1380  * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1381  *
1382  * We used to do a aho/ullman shift-reduce parser, but the logic got so
1383  * warped that I flushed it and wrote a recursive-descent parser instead.
1384  * Now things are stable, would anybody like to write a fast parser?
1385  * Most expressions are either register (which does not even reach here)
1386  * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1387  * So I guess it doesn't really matter how inefficient more complex expressions
1388  * are parsed.
1389  *
1390  * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1391  * Also, we have consumed any leading or trailing spaces (operand does that)
1392  * and done all intervening operators.
1393  *
1394  * This returns the segment of the result, which will be
1395  * absolute_section or the segment of a symbol.
1396  */
1397
1398 #undef __
1399 #define __ O_illegal
1400
1401 static const operatorT op_encoding[256] =
1402 {                               /* maps ASCII->operators */
1403
1404   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1405   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1406
1407   __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1408   __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1409   __, __, __, __, __, __, __, __,
1410   __, __, __, __, O_lt, __, O_gt, __,
1411   __, __, __, __, __, __, __, __,
1412   __, __, __, __, __, __, __, __,
1413   __, __, __, __, __, __, __, __,
1414   __, __, __, __, __, __, O_bit_exclusive_or, __,
1415   __, __, __, __, __, __, __, __,
1416   __, __, __, __, __, __, __, __,
1417   __, __, __, __, __, __, __, __,
1418   __, __, __, __, O_bit_inclusive_or, __, __, __,
1419
1420   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1421   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1422   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1423   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1424   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1425   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1426   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1427   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1428 };
1429
1430
1431 /*
1432  *      Rank    Examples
1433  *      0       operand, (expression)
1434  *      1       ||
1435  *      2       &&
1436  *      3       = <> < <= >= >
1437  *      4       + -
1438  *      5       used for * / % in MRI mode
1439  *      6       & ^ ! |
1440  *      7       * / % << >>
1441  *      8       unary - unary ~
1442  */
1443 static operator_rankT op_rank[] =
1444 {
1445   0,    /* O_illegal */
1446   0,    /* O_absent */
1447   0,    /* O_constant */
1448   0,    /* O_symbol */
1449   0,    /* O_symbol_rva */
1450   0,    /* O_register */
1451   0,    /* O_bit */
1452   8,    /* O_uminus */
1453   8,    /* O_bit_not */
1454   8,    /* O_logical_not */
1455   7,    /* O_multiply */
1456   7,    /* O_divide */
1457   7,    /* O_modulus */
1458   7,    /* O_left_shift */
1459   7,    /* O_right_shift */
1460   6,    /* O_bit_inclusive_or */
1461   6,    /* O_bit_or_not */
1462   6,    /* O_bit_exclusive_or */
1463   6,    /* O_bit_and */
1464   4,    /* O_add */
1465   4,    /* O_subtract */
1466   3,    /* O_eq */
1467   3,    /* O_ne */
1468   3,    /* O_lt */
1469   3,    /* O_le */
1470   3,    /* O_ge */
1471   3,    /* O_gt */
1472   2,    /* O_logical_and */
1473   1     /* O_logical_or */
1474 };
1475
1476 /* Unfortunately, in MRI mode for the m68k, multiplication and
1477    division have lower precedence than the bit wise operators.  This
1478    function sets the operator precedences correctly for the current
1479    mode.  Also, MRI uses a different bit_not operator, and this fixes
1480    that as well.  */
1481
1482 #define STANDARD_MUL_PRECEDENCE (7)
1483 #define MRI_MUL_PRECEDENCE (5)
1484
1485 void
1486 expr_set_precedence ()
1487 {
1488   if (flag_m68k_mri)
1489     {
1490       op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1491       op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1492       op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1493     }
1494   else
1495     {
1496       op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1497       op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1498       op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1499     }
1500 }
1501
1502 /* Initialize the expression parser.  */
1503
1504 void
1505 expr_begin ()
1506 {
1507   expr_set_precedence ();
1508
1509   /* Verify that X_op field is wide enough.  */
1510   {
1511     expressionS e;
1512     e.X_op = O_max;
1513     assert (e.X_op == O_max);
1514   }
1515 }
1516 \f
1517 /* Return the encoding for the operator at INPUT_LINE_POINTER.
1518    Advance INPUT_LINE_POINTER to the last character in the operator
1519    (i.e., don't change it for a single character operator).  */
1520
1521 static inline operatorT
1522 operator ()
1523 {
1524   int c;
1525   operatorT ret;
1526
1527   c = *input_line_pointer & 0xff;
1528
1529   switch (c)
1530     {
1531     default:
1532       return op_encoding[c];
1533
1534     case '<':
1535       switch (input_line_pointer[1])
1536         {
1537         default:
1538           return op_encoding[c];
1539         case '<':
1540           ret = O_left_shift;
1541           break;
1542         case '>':
1543           ret = O_ne;
1544           break;
1545         case '=':
1546           ret = O_le;
1547           break;
1548         }
1549       ++input_line_pointer;
1550       return ret;
1551
1552     case '=':
1553       if (input_line_pointer[1] != '=')
1554         return op_encoding[c];
1555
1556       ++input_line_pointer;
1557       return O_eq;
1558
1559     case '>':
1560       switch (input_line_pointer[1])
1561         {
1562         default:
1563           return op_encoding[c];
1564         case '>':
1565           ret = O_right_shift;
1566           break;
1567         case '=':
1568           ret = O_ge;
1569           break;
1570         }
1571       ++input_line_pointer;
1572       return ret;
1573
1574     case '!':
1575       /* We accept !! as equivalent to ^ for MRI compatibility.  */
1576       if (input_line_pointer[1] != '!')
1577         {
1578           if (flag_m68k_mri)
1579             return O_bit_inclusive_or;
1580           return op_encoding[c];
1581         }
1582       ++input_line_pointer;
1583       return O_bit_exclusive_or;
1584
1585     case '|':
1586       if (input_line_pointer[1] != '|')
1587         return op_encoding[c];
1588
1589       ++input_line_pointer;
1590       return O_logical_or;
1591
1592     case '&':
1593       if (input_line_pointer[1] != '&')
1594         return op_encoding[c];
1595
1596       ++input_line_pointer;
1597       return O_logical_and;
1598     }
1599
1600   /*NOTREACHED*/
1601 }
1602
1603 /* Parse an expression.  */
1604
1605 segT
1606 expr (rank, resultP)
1607      operator_rankT rank;       /* Larger # is higher rank. */
1608      expressionS *resultP;      /* Deliver result here. */
1609 {
1610   segT retval;
1611   expressionS right;
1612   operatorT op_left;
1613   operatorT op_right;
1614
1615   know (rank >= 0);
1616
1617   retval = operand (resultP);
1618
1619   know (*input_line_pointer != ' ');    /* Operand() gobbles spaces. */
1620
1621   op_left = operator ();
1622   while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1623     {
1624       segT rightseg;
1625
1626       input_line_pointer++;     /*->after 1st character of operator. */
1627
1628       rightseg = expr (op_rank[(int) op_left], &right);
1629       if (right.X_op == O_absent)
1630         {
1631           as_warn (_("missing operand; zero assumed"));
1632           right.X_op = O_constant;
1633           right.X_add_number = 0;
1634           right.X_add_symbol = NULL;
1635           right.X_op_symbol = NULL;
1636         }
1637
1638       know (*input_line_pointer != ' ');
1639
1640       if (retval == undefined_section)
1641         {
1642           if (SEG_NORMAL (rightseg))
1643             retval = rightseg;
1644         }
1645       else if (! SEG_NORMAL (retval))
1646         retval = rightseg;
1647       else if (SEG_NORMAL (rightseg)
1648                && retval != rightseg
1649 #ifdef DIFF_EXPR_OK
1650                && op_left != O_subtract
1651 #endif
1652                )
1653         as_bad (_("operation combines symbols in different segments"));
1654
1655       op_right = operator ();
1656
1657       know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1658       know ((int) op_left >= (int) O_multiply
1659             && (int) op_left <= (int) O_logical_or);
1660
1661       /* input_line_pointer->after right-hand quantity. */
1662       /* left-hand quantity in resultP */
1663       /* right-hand quantity in right. */
1664       /* operator in op_left. */
1665
1666       if (resultP->X_op == O_big)
1667         {
1668           if (resultP->X_add_number > 0)
1669             as_warn (_("left operand is a bignum; integer 0 assumed"));
1670           else
1671             as_warn (_("left operand is a float; integer 0 assumed"));
1672           resultP->X_op = O_constant;
1673           resultP->X_add_number = 0;
1674           resultP->X_add_symbol = NULL;
1675           resultP->X_op_symbol = NULL;
1676         }
1677       if (right.X_op == O_big)
1678         {
1679           if (right.X_add_number > 0)
1680             as_warn (_("right operand is a bignum; integer 0 assumed"));
1681           else
1682             as_warn (_("right operand is a float; integer 0 assumed"));
1683           right.X_op = O_constant;
1684           right.X_add_number = 0;
1685           right.X_add_symbol = NULL;
1686           right.X_op_symbol = NULL;
1687         }
1688
1689       /* Optimize common cases.  */
1690       if (op_left == O_add && right.X_op == O_constant)
1691         {
1692           /* X + constant.  */
1693           resultP->X_add_number += right.X_add_number;
1694         }
1695       /* This case comes up in PIC code.  */
1696       else if (op_left == O_subtract
1697                && right.X_op == O_symbol
1698                && resultP->X_op == O_symbol
1699                && (right.X_add_symbol->sy_frag
1700                    == resultP->X_add_symbol->sy_frag)
1701                && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol)))
1702
1703         {
1704           resultP->X_add_number -= right.X_add_number;
1705           resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
1706                                     - S_GET_VALUE (right.X_add_symbol));
1707           resultP->X_op = O_constant;
1708           resultP->X_add_symbol = 0;
1709         }
1710       else if (op_left == O_subtract && right.X_op == O_constant)
1711         {
1712           /* X - constant.  */
1713           resultP->X_add_number -= right.X_add_number;
1714         }
1715       else if (op_left == O_add && resultP->X_op == O_constant)
1716         {
1717           /* Constant + X.  */
1718           resultP->X_op = right.X_op;
1719           resultP->X_add_symbol = right.X_add_symbol;
1720           resultP->X_op_symbol = right.X_op_symbol;
1721           resultP->X_add_number += right.X_add_number;
1722           retval = rightseg;
1723         }
1724       else if (resultP->X_op == O_constant && right.X_op == O_constant)
1725         {
1726           /* Constant OP constant.  */
1727           offsetT v = right.X_add_number;
1728           if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1729             {
1730               as_warn (_("division by zero"));
1731               v = 1;
1732             }
1733           switch (op_left)
1734             {
1735             default:                    abort ();
1736             case O_multiply:            resultP->X_add_number *= v; break;
1737             case O_divide:              resultP->X_add_number /= v; break;
1738             case O_modulus:             resultP->X_add_number %= v; break;
1739             case O_left_shift:          resultP->X_add_number <<= v; break;
1740             case O_right_shift:
1741               /* We always use unsigned shifts, to avoid relying on
1742                  characteristics of the compiler used to compile gas.  */
1743               resultP->X_add_number =
1744                 (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1745               break;
1746             case O_bit_inclusive_or:    resultP->X_add_number |= v; break;
1747             case O_bit_or_not:          resultP->X_add_number |= ~v; break;
1748             case O_bit_exclusive_or:    resultP->X_add_number ^= v; break;
1749             case O_bit_and:             resultP->X_add_number &= v; break;
1750             case O_add:                 resultP->X_add_number += v; break;
1751             case O_subtract:            resultP->X_add_number -= v; break;
1752             case O_eq:
1753               resultP->X_add_number =
1754                 resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1755               break;
1756             case O_ne:
1757               resultP->X_add_number =
1758                 resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1759               break;
1760             case O_lt:
1761               resultP->X_add_number =
1762                 resultP->X_add_number <  v ? ~ (offsetT) 0 : 0;
1763               break;
1764             case O_le:
1765               resultP->X_add_number =
1766                 resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1767               break;
1768             case O_ge:
1769               resultP->X_add_number =
1770                 resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1771               break;
1772             case O_gt:
1773               resultP->X_add_number =
1774                 resultP->X_add_number >  v ? ~ (offsetT) 0 : 0;
1775               break;
1776             case O_logical_and:
1777               resultP->X_add_number = resultP->X_add_number && v;
1778               break;
1779             case O_logical_or:
1780               resultP->X_add_number = resultP->X_add_number || v;
1781               break;
1782             }
1783         }
1784       else if (resultP->X_op == O_symbol
1785                && right.X_op == O_symbol
1786                && (op_left == O_add
1787                    || op_left == O_subtract
1788                    || (resultP->X_add_number == 0
1789                        && right.X_add_number == 0)))
1790         {
1791           /* Symbol OP symbol.  */
1792           resultP->X_op = op_left;
1793           resultP->X_op_symbol = right.X_add_symbol;
1794           if (op_left == O_add)
1795             resultP->X_add_number += right.X_add_number;
1796           else if (op_left == O_subtract)
1797             resultP->X_add_number -= right.X_add_number;
1798         }
1799       else
1800         {
1801           /* The general case.  */
1802           resultP->X_add_symbol = make_expr_symbol (resultP);
1803           resultP->X_op_symbol = make_expr_symbol (&right);
1804           resultP->X_op = op_left;
1805           resultP->X_add_number = 0;
1806           resultP->X_unsigned = 1;
1807         }
1808
1809       op_left = op_right;
1810     }                           /* While next operator is >= this rank. */
1811
1812   /* The PA port needs this information.  */
1813   if (resultP->X_add_symbol)
1814     resultP->X_add_symbol->sy_used = 1;
1815
1816   return resultP->X_op == O_constant ? absolute_section : retval;
1817 }
1818 \f
1819 /*
1820  *                      get_symbol_end()
1821  *
1822  * This lives here because it belongs equally in expr.c & read.c.
1823  * Expr.c is just a branch office read.c anyway, and putting it
1824  * here lessens the crowd at read.c.
1825  *
1826  * Assume input_line_pointer is at start of symbol name.
1827  * Advance input_line_pointer past symbol name.
1828  * Turn that character into a '\0', returning its former value.
1829  * This allows a string compare (RMS wants symbol names to be strings)
1830  * of the symbol name.
1831  * There will always be a char following symbol name, because all good
1832  * lines end in end-of-line.
1833  */
1834 char
1835 get_symbol_end ()
1836 {
1837   char c;
1838
1839   /* We accept \001 in a name in case this is being called with a
1840      constructed string.  */
1841   if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
1842     while (is_part_of_name (c = *input_line_pointer++)
1843            || c == '\001')
1844       ;
1845   *--input_line_pointer = 0;
1846   return (c);
1847 }
1848
1849
1850 unsigned int
1851 get_single_number ()
1852 {
1853   expressionS exp;
1854   operand (&exp);
1855   return exp.X_add_number;
1856
1857 }
1858
1859 /* end of expr.c */