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