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