* expr.c (operand): For floating point operand with unusual fp char from
[external/binutils.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2    Copyright (C) 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    GAS is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GAS is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GAS; see the file COPYING.  If not, write to
18    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /*
21  * This is really a branch office of as-read.c. I split it out to clearly
22  * distinguish the world of expressions from the world of statements.
23  * (It also gives smaller files to re-compile.)
24  * Here, "operand"s are of expressions, not instructions.
25  */
26
27 #include <ctype.h>
28 #include <string.h>
29
30 #include "as.h"
31
32 #include "obstack.h"
33
34 static void floating_constant PARAMS ((expressionS * expressionP));
35 static void integer_constant PARAMS ((int radix, expressionS * expressionP));
36 static void clean_up_expression PARAMS ((expressionS * expressionP));
37 static symbolS *make_expr_symbol PARAMS ((expressionS * expressionP));
38
39 extern const char EXP_CHARS[], FLT_CHARS[];
40 \f
41 /* Build a dummy symbol to hold a complex expression.  This is how we
42    build expressions up out of other expressions.  The symbol is put
43    into the fake section expr_section.  */
44
45 static symbolS *
46 make_expr_symbol (expressionP)
47      expressionS *expressionP;
48 {
49   const char *fake;
50   symbolS *symbolP;
51
52   /* FIXME: This should be something which decode_local_label_name
53      will handle.  */
54   fake = FAKE_LABEL_NAME;
55
56   /* Putting constant symbols in absolute_section rather than
57      expr_section is convenient for the old a.out code, for which
58      S_GET_SEGMENT does not always retrieve the value put in by
59      S_SET_SEGMENT.  */
60   symbolP = symbol_new (fake,
61                         (expressionP->X_op == O_constant
62                          ? absolute_section
63                          : expr_section),
64                         0, &zero_address_frag);
65   symbolP->sy_value = *expressionP;
66   return symbolP;
67 }
68 \f
69 /*
70  * Build any floating-point literal here.
71  * Also build any bignum literal here.
72  */
73
74 /* Seems atof_machine can backscan through generic_bignum and hit whatever
75    happens to be loaded before it in memory.  And its way too complicated
76    for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
77    and never write into the early words, thus they'll always be zero.
78    I hate Dean's floating-point code.  Bleh.  */
79 LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
80 FLONUM_TYPE generic_floating_point_number =
81 {
82   &generic_bignum[6],           /* low (JF: Was 0) */
83   &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1],        /* high JF: (added +6) */
84   0,                            /* leader */
85   0,                            /* exponent */
86   0                             /* sign */
87 };
88 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
89 int generic_floating_point_magic;
90 \f
91 static void
92 floating_constant (expressionP)
93      expressionS *expressionP;
94 {
95   /* input_line_pointer->*/
96   /* floating-point constant. */
97   int error_code;
98
99   error_code = atof_generic
100     (&input_line_pointer, ".", EXP_CHARS,
101      &generic_floating_point_number);
102
103   if (error_code)
104     {
105       if (error_code == ERROR_EXPONENT_OVERFLOW)
106         {
107           as_bad ("bad floating-point constant: exponent overflow, probably assembling junk");
108         }
109       else
110         {
111           as_bad ("bad floating-point constant: unknown error code=%d.", error_code);
112         }
113     }
114   expressionP->X_op = O_big;
115   /* input_line_pointer->just after constant, */
116   /* which may point to whitespace. */
117   expressionP->X_add_number = -1;
118 }
119
120 static void
121 integer_constant (radix, expressionP)
122      int radix;
123      expressionS *expressionP;
124 {
125   char *start;          /* start of number. */
126   char c;
127
128   valueT number;        /* offset or (absolute) value */
129   short int digit;      /* value of next digit in current radix */
130   short int maxdig = 0;/* highest permitted digit value. */
131   int too_many_digits = 0;      /* if we see >= this number of */
132   char *name;           /* points to name of symbol */
133   symbolS *symbolP;     /* points to symbol */
134
135   int small;                    /* true if fits in 32 bits. */
136   extern const char hex_value[]; /* in hex_value.c */
137
138   /* May be bignum, or may fit in 32 bits. */
139   /* Most numbers fit into 32 bits, and we want this case to be fast.
140      so we pretend it will fit into 32 bits.  If, after making up a 32
141      bit number, we realise that we have scanned more digits than
142      comfortably fit into 32 bits, we re-scan the digits coding them
143      into a bignum.  For decimal and octal numbers we are
144      conservative: Some numbers may be assumed bignums when in fact
145      they do fit into 32 bits.  Numbers of any radix can have excess
146      leading zeros: We strive to recognise this and cast them back
147      into 32 bits.  We must check that the bignum really is more than
148      32 bits, and change it back to a 32-bit number if it fits.  The
149      number we are looking for is expected to be positive, but if it
150      fits into 32 bits as an unsigned number, we let it be a 32-bit
151      number.  The cavalier approach is for speed in ordinary cases. */
152   /* This has been extended for 64 bits.  We blindly assume that if
153      you're compiling in 64-bit mode, the target is a 64-bit machine.
154      This should be cleaned up.  */
155
156 #ifdef BFD64
157 #define valuesize 64
158 #else /* includes non-bfd case, mostly */
159 #define valuesize 32
160 #endif
161
162   switch (radix)
163     {
164     case 2:
165       maxdig = 2;
166       too_many_digits = valuesize + 1;
167       break;
168     case 8:
169       maxdig = radix = 8;
170       too_many_digits = (valuesize + 2) / 3 + 1;
171       break;
172     case 16:
173       maxdig = radix = 16;
174       too_many_digits = (valuesize + 3) / 4 + 1;
175       break;
176     case 10:
177       maxdig = radix = 10;
178       too_many_digits = (valuesize + 12) / 4; /* very rough */
179     }
180 #undef valuesize
181   start = input_line_pointer;
182   c = *input_line_pointer++;
183   for (number = 0;
184        (digit = hex_value[(unsigned char) c]) < maxdig;
185        c = *input_line_pointer++)
186     {
187       number = number * radix + digit;
188     }
189   /* c contains character after number. */
190   /* input_line_pointer->char after c. */
191   small = (input_line_pointer - start - 1) < too_many_digits;
192   if (!small)
193     {
194       /*
195        * we saw a lot of digits. manufacture a bignum the hard way.
196        */
197       LITTLENUM_TYPE *leader;   /*->high order littlenum of the bignum. */
198       LITTLENUM_TYPE *pointer;  /*->littlenum we are frobbing now. */
199       long carry;
200
201       leader = generic_bignum;
202       generic_bignum[0] = 0;
203       generic_bignum[1] = 0;
204       input_line_pointer = start;       /*->1st digit. */
205       c = *input_line_pointer++;
206       for (;
207            (carry = hex_value[(unsigned char) c]) < maxdig;
208            c = *input_line_pointer++)
209         {
210           for (pointer = generic_bignum;
211                pointer <= leader;
212                pointer++)
213             {
214               long work;
215
216               work = carry + radix * *pointer;
217               *pointer = work & LITTLENUM_MASK;
218               carry = work >> LITTLENUM_NUMBER_OF_BITS;
219             }
220           if (carry)
221             {
222               if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
223                 {               /* room to grow a longer bignum. */
224                   *++leader = carry;
225                 }
226             }
227         }
228       /* again, c is char after number, */
229       /* input_line_pointer->after c. */
230       know (LITTLENUM_NUMBER_OF_BITS == 16);
231       if (leader < generic_bignum + 2)
232         {                       /* will fit into 32 bits. */
233           number =
234             ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
235             | (generic_bignum[0] & LITTLENUM_MASK);
236           small = 1;
237         }
238       else
239         {
240           number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */
241         }
242     }
243   if (small)
244     {
245       /*
246        * here with number, in correct radix. c is the next char.
247        * note that unlike un*x, we allow "011f" "0x9f" to
248        * both mean the same as the (conventional) "9f". this is simply easier
249        * than checking for strict canonical form. syntax sux!
250        */
251
252       switch (c)
253         {
254
255 #ifdef LOCAL_LABELS_FB
256         case 'b':
257           {
258             /*
259              * backward ref to local label.
260              * because it is backward, expect it to be defined.
261              */
262             /* Construct a local label.  */
263             name = fb_label_name ((int) number, 0);
264
265             /* seen before, or symbol is defined: ok */
266             symbolP = symbol_find (name);
267             if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
268               {
269
270                 /* local labels are never absolute. don't waste time
271                    checking absoluteness. */
272                 know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
273
274                 expressionP->X_op = O_symbol;
275                 expressionP->X_add_symbol = symbolP;
276
277               }
278             else
279               {
280                 /* either not seen or not defined. */
281                 /* @@ Should print out the original string instead of
282                    the parsed number.  */
283                 as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.",
284                         (int) number);
285                 expressionP->X_op = O_constant;
286               }
287
288             expressionP->X_add_number = 0;
289             break;
290           }                     /* case 'b' */
291
292         case 'f':
293           {
294             /*
295              * forward reference. expect symbol to be undefined or
296              * unknown. undefined: seen it before. unknown: never seen
297              * it before.
298              * construct a local label name, then an undefined symbol.
299              * don't create a xseg frag for it: caller may do that.
300              * just return it as never seen before.
301              */
302             name = fb_label_name ((int) number, 1);
303             symbolP = symbol_find_or_make (name);
304             /* we have no need to check symbol properties. */
305 #ifndef many_segments
306             /* since "know" puts its arg into a "string", we
307                can't have newlines in the argument.  */
308             know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
309 #endif
310             expressionP->X_op = O_symbol;
311             expressionP->X_add_symbol = symbolP;
312             expressionP->X_add_number = 0;
313
314             break;
315           }                     /* case 'f' */
316
317 #endif /* LOCAL_LABELS_FB */
318
319 #ifdef LOCAL_LABELS_DOLLAR
320
321         case '$':
322           {
323
324             /* If the dollar label is *currently* defined, then this is just
325                another reference to it.  If it is not *currently* defined,
326                then this is a fresh instantiation of that number, so create
327                it.  */
328
329             if (dollar_label_defined ((long) number))
330               {
331                 name = dollar_label_name ((long) number, 0);
332                 symbolP = symbol_find (name);
333                 know (symbolP != NULL);
334               }
335             else
336               {
337                 name = dollar_label_name ((long) number, 1);
338                 symbolP = symbol_find_or_make (name);
339               }
340
341             expressionP->X_op = O_symbol;
342             expressionP->X_add_symbol = symbolP;
343             expressionP->X_add_number = 0;
344
345             break;
346           }                     /* case '$' */
347
348 #endif /* LOCAL_LABELS_DOLLAR */
349
350         default:
351           {
352             expressionP->X_op = O_constant;
353             expressionP->X_add_number = number;
354             input_line_pointer--;       /* restore following character. */
355             break;
356           }                     /* really just a number */
357
358         }                       /* switch on char following the number */
359
360     }
361   else
362     {
363       /* not a small number */
364       expressionP->X_op = O_big;
365       expressionP->X_add_number = number;       /* number of littlenums */
366       input_line_pointer--;     /*->char following number. */
367     }
368 }
369
370
371 /*
372  * Summary of operand().
373  *
374  * in:  Input_line_pointer points to 1st char of operand, which may
375  *      be a space.
376  *
377  * out: A expressionS.
378  *      The operand may have been empty: in this case X_op == O_absent.
379  *      Input_line_pointer->(next non-blank) char after operand.
380  */
381
382 static segT
383 operand (expressionP)
384      expressionS *expressionP;
385 {
386   char c;
387   symbolS *symbolP;     /* points to symbol */
388   char *name;           /* points to name of symbol */
389   segT segment;
390
391   /* All integers are regarded as unsigned unless they are negated.
392      This is because the only thing which cares whether a number is
393      unsigned is the code in emit_expr which extends constants into
394      bignums.  It should only sign extend negative numbers, so that
395      something like ``.quad 0x80000000'' is not sign extended even
396      though it appears negative if valueT is 32 bits.  */
397   expressionP->X_unsigned = 1;
398
399   /* digits, assume it is a bignum. */
400
401   SKIP_WHITESPACE ();           /* leading whitespace is part of operand. */
402   c = *input_line_pointer++;    /* input_line_pointer->past char in c. */
403
404   switch (c)
405     {
406 #ifdef MRI
407     case '%':
408       integer_constant (2, expressionP);
409       break;
410     case '@':
411       integer_constant (8, expressionP);
412       break;
413     case '$':
414       integer_constant (16, expressionP);
415       break;
416 #endif
417     case '1':
418     case '2':
419     case '3':
420     case '4':
421     case '5':
422     case '6':
423     case '7':
424     case '8':
425     case '9':
426       input_line_pointer--;
427
428       integer_constant (10, expressionP);
429       break;
430
431     case '0':
432       /* non-decimal radix */
433
434       c = *input_line_pointer;
435       switch (c)
436         {
437
438         default:
439           if (c && strchr (FLT_CHARS, c))
440             {
441               input_line_pointer++;
442               floating_constant (expressionP);
443               expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
444             }
445           else
446             {
447               /* The string was only zero */
448               expressionP->X_op = O_constant;
449               expressionP->X_add_number = 0;
450             }
451
452           break;
453
454         case 'x':
455         case 'X':
456           input_line_pointer++;
457           integer_constant (16, expressionP);
458           break;
459
460         case 'b':
461 #ifdef LOCAL_LABELS_FB
462           if (!input_line_pointer[1]
463               /* Strictly speaking, we should only need to check for
464                  "+-01", since that's all you'd normally have in a
465                  binary constant.  But some of our code does permit
466                  digits greater than the base we're expecting.  */
467               || !strchr ("+-0123456789", input_line_pointer[1]))
468             {
469               input_line_pointer--;
470               integer_constant (10, expressionP);
471               break;
472             }
473 #endif
474         case 'B':
475           input_line_pointer++;
476           integer_constant (2, expressionP);
477           break;
478
479         case '0':
480         case '1':
481         case '2':
482         case '3':
483         case '4':
484         case '5':
485         case '6':
486         case '7':
487           integer_constant (8, expressionP);
488           break;
489
490         case 'f':
491 #ifdef LOCAL_LABELS_FB
492           /* if it says '0f' and the line ends or it doesn't look like
493              a floating point #, its a local label ref.  dtrt */
494           /* likewise for the b's.  xoxorich. */
495           if (c == 'f'
496               && (!input_line_pointer[1]
497                   || (!strchr ("+-.0123456789", input_line_pointer[1])
498                       && !strchr (EXP_CHARS, input_line_pointer[1]))))
499             {
500               input_line_pointer -= 1;
501               integer_constant (10, expressionP);
502               break;
503             }
504 #endif
505
506         case 'd':
507         case 'D':
508         case 'F':
509         case 'r':
510         case 'e':
511         case 'E':
512         case 'g':
513         case 'G':
514
515           input_line_pointer++;
516           floating_constant (expressionP);
517           expressionP->X_add_number = -(isupper (c) ? tolower (c) : c);
518           break;
519
520 #ifdef LOCAL_LABELS_DOLLAR
521         case '$':
522           integer_constant (10, expressionP);
523           break;
524 #endif
525         }
526
527       break;
528
529     case '(':
530       /* didn't begin with digit & not a name */
531       segment = expression (expressionP);
532       /* Expression() will pass trailing whitespace */
533       if (*input_line_pointer++ != ')')
534         {
535           as_bad ("Missing ')' assumed");
536           input_line_pointer--;
537         }
538       /* here with input_line_pointer->char after "(...)" */
539       return segment;
540
541     case '\'':
542       /* Warning: to conform to other people's assemblers NO ESCAPEMENT is
543          permitted for a single quote. The next character, parity errors and
544          all, is taken as the value of the operand. VERY KINKY.  */
545       expressionP->X_op = O_constant;
546       expressionP->X_add_number = *input_line_pointer++;
547       break;
548
549     case '+':
550       (void) operand (expressionP);
551       break;
552
553     case '~':
554     case '-':
555       {
556         operand (expressionP);
557         if (expressionP->X_op == O_constant)
558           {
559             /* input_line_pointer -> char after operand */
560             if (c == '-')
561               {
562                 expressionP->X_add_number = - expressionP->X_add_number;
563                 /* Notice: '-' may overflow: no warning is given. This is
564                    compatible with other people's assemblers. Sigh.  */
565                 expressionP->X_unsigned = 0;
566               }
567             else
568               expressionP->X_add_number = ~ expressionP->X_add_number;
569           }
570         else if (expressionP->X_op != O_illegal
571                  && expressionP->X_op != O_absent)
572           {
573             expressionP->X_add_symbol = make_expr_symbol (expressionP);
574             if (c == '-')
575               expressionP->X_op = O_uminus;
576             else
577               expressionP->X_op = O_bit_not;
578             expressionP->X_add_number = 0;
579           }
580         else
581           as_warn ("Unary operator %c ignored because bad operand follows",
582                    c);
583       }
584       break;
585
586     case '.':
587       if (!is_part_of_name (*input_line_pointer))
588         {
589           const char *fake;
590
591           /* JF: '.' is pseudo symbol with value of current location
592              in current segment.  */
593           fake = FAKE_LABEL_NAME;
594           symbolP = symbol_new (fake,
595                                 now_seg,
596                                 (valueT) frag_now_fix (),
597                                 frag_now);
598
599           expressionP->X_op = O_symbol;
600           expressionP->X_add_symbol = symbolP;
601           expressionP->X_add_number = 0;
602           break;
603         }
604       else
605         {
606           goto isname;
607         }
608     case ',':
609     case '\n':
610     case '\0':
611     eol:
612       /* can't imagine any other kind of operand */
613       expressionP->X_op = O_absent;
614       input_line_pointer--;
615       md_operand (expressionP);
616       break;
617
618     default:
619       if (is_end_of_line[(unsigned char) c])
620         goto eol;
621       if (is_name_beginner (c)) /* here if did not begin with a digit */
622         {
623           /*
624            * Identifier begins here.
625            * This is kludged for speed, so code is repeated.
626            */
627         isname:
628           name = --input_line_pointer;
629           c = get_symbol_end ();
630           symbolP = symbol_find_or_make (name);
631
632           /* If we have an absolute symbol or a reg, then we know its
633              value now.  */
634           segment = S_GET_SEGMENT (symbolP);
635           if (segment == absolute_section)
636             {
637               expressionP->X_op = O_constant;
638               expressionP->X_add_number = S_GET_VALUE (symbolP);
639             }
640           else if (segment == reg_section)
641             {
642               expressionP->X_op = O_register;
643               expressionP->X_add_number = S_GET_VALUE (symbolP);
644             }
645           else
646             {
647               expressionP->X_op = O_symbol;
648               expressionP->X_add_symbol = symbolP;
649               expressionP->X_add_number = 0;
650             }
651           *input_line_pointer = c;
652         }
653       else
654         {
655           as_bad ("Bad expression");
656           expressionP->X_op = O_constant;
657           expressionP->X_add_number = 0;
658         }
659     }
660
661   /*
662    * It is more 'efficient' to clean up the expressionS when they are created.
663    * Doing it here saves lines of code.
664    */
665   clean_up_expression (expressionP);
666   SKIP_WHITESPACE ();           /*->1st char after operand. */
667   know (*input_line_pointer != ' ');
668
669   /* The PA port needs this information.  */
670   if (expressionP->X_add_symbol)
671     expressionP->X_add_symbol->sy_used = 1;
672
673   switch (expressionP->X_op)
674     {
675     default:
676       return absolute_section;
677     case O_symbol:
678       return S_GET_SEGMENT (expressionP->X_add_symbol);
679     case O_register:
680       return reg_section;
681     }
682 }                               /* operand() */
683 \f
684 /* Internal. Simplify a struct expression for use by expr() */
685
686 /*
687  * In:  address of a expressionS.
688  *      The X_op field of the expressionS may only take certain values.
689  *      Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
690  * Out: expressionS may have been modified:
691  *      'foo-foo' symbol references cancelled to 0,
692  *              which changes X_op from O_subtract to O_constant.
693  *      Unused fields zeroed to help expr().
694  */
695
696 static void
697 clean_up_expression (expressionP)
698      expressionS *expressionP;
699 {
700   switch (expressionP->X_op)
701     {
702     case O_illegal:
703     case O_absent:
704       expressionP->X_add_number = 0;
705       /* Fall through.  */
706     case O_big:
707     case O_constant:
708     case O_register:
709       expressionP->X_add_symbol = NULL;
710       /* Fall through.  */
711     case O_symbol:
712     case O_uminus:
713     case O_bit_not:
714       expressionP->X_op_symbol = NULL;
715       break;
716     case O_subtract:
717       if (expressionP->X_op_symbol == expressionP->X_add_symbol
718           || ((expressionP->X_op_symbol->sy_frag
719                == expressionP->X_add_symbol->sy_frag)
720               && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol))
721               && (S_GET_VALUE (expressionP->X_op_symbol)
722                   == S_GET_VALUE (expressionP->X_add_symbol))))
723         {
724           expressionP->X_op = O_constant;
725           expressionP->X_add_symbol = NULL;
726           expressionP->X_op_symbol = NULL;
727         }
728       break;
729     default:
730       break;
731     }
732 }
733 \f
734 /* Expression parser. */
735
736 /*
737  * We allow an empty expression, and just assume (absolute,0) silently.
738  * Unary operators and parenthetical expressions are treated as operands.
739  * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
740  *
741  * We used to do a aho/ullman shift-reduce parser, but the logic got so
742  * warped that I flushed it and wrote a recursive-descent parser instead.
743  * Now things are stable, would anybody like to write a fast parser?
744  * Most expressions are either register (which does not even reach here)
745  * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
746  * So I guess it doesn't really matter how inefficient more complex expressions
747  * are parsed.
748  *
749  * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
750  * Also, we have consumed any leading or trailing spaces (operand does that)
751  * and done all intervening operators.
752  *
753  * This returns the segment of the result, which will be
754  * absolute_section or the segment of a symbol.
755  */
756
757 #undef __
758 #define __ O_illegal
759
760 static const operatorT op_encoding[256] =
761 {                               /* maps ASCII->operators */
762
763   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
764   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
765
766   __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
767   __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
768   __, __, __, __, __, __, __, __,
769   __, __, __, __, O_left_shift, __, O_right_shift, __,
770   __, __, __, __, __, __, __, __,
771   __, __, __, __, __, __, __, __,
772   __, __, __, __, __, __, __, __,
773   __, __, __, __, __, __, O_bit_exclusive_or, __,
774   __, __, __, __, __, __, __, __,
775   __, __, __, __, __, __, __, __,
776   __, __, __, __, __, __, __, __,
777   __, __, __, __, O_bit_inclusive_or, __, __, __,
778
779   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
780   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
781   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
782   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
783   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
784   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
785   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
786   __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
787 };
788
789
790 /*
791  *      Rank    Examples
792  *      0       operand, (expression)
793  *      1       + -
794  *      2       & ^ ! |
795  *      3       * / % << >>
796  *      4       unary - unary ~
797  */
798 static const operator_rankT op_rank[] =
799 {
800   0,    /* O_illegal */
801   0,    /* O_absent */
802   0,    /* O_constant */
803   0,    /* O_symbol */
804   0,    /* O_register */
805   0,    /* O_bit */
806   4,    /* O_uminus */
807   4,    /* O_bit_now */
808   3,    /* O_multiply */
809   3,    /* O_divide */
810   3,    /* O_modulus */
811   3,    /* O_left_shift */
812   3,    /* O_right_shift */
813   2,    /* O_bit_inclusive_or */
814   2,    /* O_bit_or_not */
815   2,    /* O_bit_exclusive_or */
816   2,    /* O_bit_and */
817   1,    /* O_add */
818   1,    /* O_subtract */
819 };
820 \f
821 segT
822 expr (rank, resultP)
823      operator_rankT rank;       /* Larger # is higher rank. */
824      expressionS *resultP;      /* Deliver result here. */
825 {
826   segT retval;
827   expressionS right;
828   operatorT op_left;
829   char c_left;          /* 1st operator character. */
830   operatorT op_right;
831   char c_right;
832
833   know (rank >= 0);
834
835   retval = operand (resultP);
836
837   know (*input_line_pointer != ' ');    /* Operand() gobbles spaces. */
838
839   c_left = *input_line_pointer; /* Potential operator character. */
840   op_left = op_encoding[(unsigned char) c_left];
841   while (op_left != O_illegal && op_rank[(int) op_left] > rank)
842     {
843       segT rightseg;
844
845       input_line_pointer++;     /*->after 1st character of operator. */
846       /* Operators "<<" and ">>" have 2 characters. */
847       if (*input_line_pointer == c_left && (c_left == '<' || c_left == '>'))
848         ++input_line_pointer;
849
850       rightseg = expr (op_rank[(int) op_left], &right);
851       if (right.X_op == O_absent)
852         {
853           as_warn ("missing operand; zero assumed");
854           right.X_op = O_constant;
855           right.X_add_number = 0;
856           right.X_add_symbol = NULL;
857           right.X_op_symbol = NULL;
858         }
859
860       know (*input_line_pointer != ' ');
861
862       if (retval == undefined_section)
863         {
864           if (SEG_NORMAL (rightseg))
865             retval = rightseg;
866         }
867       else if (! SEG_NORMAL (retval))
868         retval = rightseg;
869       else if (SEG_NORMAL (rightseg)
870                && retval != rightseg
871 #ifdef DIFF_EXPR_OK
872                && op_left != O_subtract
873 #endif
874                )
875         as_bad ("operation combines symbols in different segments");
876
877       c_right = *input_line_pointer;
878       op_right = op_encoding[(unsigned char) c_right];
879       if (*input_line_pointer == c_right && (c_right == '<' || c_right == '>'))
880         ++input_line_pointer;
881
882       know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]);
883       know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_subtract);
884
885       /* input_line_pointer->after right-hand quantity. */
886       /* left-hand quantity in resultP */
887       /* right-hand quantity in right. */
888       /* operator in op_left. */
889
890       if (resultP->X_op == O_big)
891         {
892           as_warn ("left operand of %c is a %s; integer 0 assumed",
893                    c_left, resultP->X_add_number > 0 ? "bignum" : "float");
894           resultP->X_op = O_constant;
895           resultP->X_add_number = 0;
896           resultP->X_add_symbol = NULL;
897           resultP->X_op_symbol = NULL;
898         }
899       if (right.X_op == O_big)
900         {
901           as_warn ("right operand of %c is a %s; integer 0 assumed",
902                    c_left, right.X_add_number > 0 ? "bignum" : "float");
903           right.X_op = O_constant;
904           right.X_add_number = 0;
905           right.X_add_symbol = NULL;
906           right.X_op_symbol = NULL;
907         }
908
909       /* Optimize common cases.  */
910       if (op_left == O_add && right.X_op == O_constant)
911         {
912           /* X + constant.  */
913           resultP->X_add_number += right.X_add_number;
914         }
915       else if (op_left == O_subtract && right.X_op == O_constant)
916         {
917           /* X - constant.  */
918           resultP->X_add_number -= right.X_add_number;
919         }
920       else if (op_left == O_add && resultP->X_op == O_constant)
921         {
922           /* Constant + X.  */
923           resultP->X_op = right.X_op;
924           resultP->X_add_symbol = right.X_add_symbol;
925           resultP->X_op_symbol = right.X_op_symbol;
926           resultP->X_add_number += right.X_add_number;
927           retval = rightseg;
928         }
929       else if (resultP->X_op == O_constant && right.X_op == O_constant)
930         {
931           /* Constant OP constant.  */
932           offsetT v = right.X_add_number;
933           if (v == 0 && (op_left == O_divide || op_left == O_modulus))
934             {
935               as_warn ("division by zero");
936               v = 1;
937             }
938           switch (op_left)
939             {
940             case O_multiply:            resultP->X_add_number *= v; break;
941             case O_divide:              resultP->X_add_number /= v; break;
942             case O_modulus:             resultP->X_add_number %= v; break;
943             case O_left_shift:          resultP->X_add_number <<= v; break;
944             case O_right_shift:         resultP->X_add_number >>= v; break;
945             case O_bit_inclusive_or:    resultP->X_add_number |= v; break;
946             case O_bit_or_not:          resultP->X_add_number |= ~v; break;
947             case O_bit_exclusive_or:    resultP->X_add_number ^= v; break;
948             case O_bit_and:             resultP->X_add_number &= v; break;
949             case O_add:                 resultP->X_add_number += v; break;
950             case O_subtract:            resultP->X_add_number -= v; break;
951             default:                    abort ();
952             }
953         }
954       else if (resultP->X_op == O_symbol
955                && right.X_op == O_symbol
956                && (op_left == O_add
957                    || op_left == O_subtract
958                    || (resultP->X_add_number == 0
959                        && right.X_add_number == 0)))
960         {
961           /* Symbol OP symbol.  */
962           resultP->X_op = op_left;
963           resultP->X_op_symbol = right.X_add_symbol;
964           if (op_left == O_add)
965             resultP->X_add_number += right.X_add_number;
966           else if (op_left == O_subtract)
967             resultP->X_add_number -= right.X_add_number;
968         }
969       else
970         {
971           /* The general case.  */
972           resultP->X_add_symbol = make_expr_symbol (resultP);
973           resultP->X_op_symbol = make_expr_symbol (&right);
974           resultP->X_op = op_left;
975           resultP->X_add_number = 0;
976           resultP->X_unsigned = 1;
977         }
978           
979       op_left = op_right;
980     }                           /* While next operator is >= this rank. */
981
982   /* The PA port needs this information.  */
983   if (resultP->X_add_symbol)
984     resultP->X_add_symbol->sy_used = 1;
985
986   return resultP->X_op == O_constant ? absolute_section : retval;
987 }
988 \f
989 /*
990  *                      get_symbol_end()
991  *
992  * This lives here because it belongs equally in expr.c & read.c.
993  * Expr.c is just a branch office read.c anyway, and putting it
994  * here lessens the crowd at read.c.
995  *
996  * Assume input_line_pointer is at start of symbol name.
997  * Advance input_line_pointer past symbol name.
998  * Turn that character into a '\0', returning its former value.
999  * This allows a string compare (RMS wants symbol names to be strings)
1000  * of the symbol name.
1001  * There will always be a char following symbol name, because all good
1002  * lines end in end-of-line.
1003  */
1004 char
1005 get_symbol_end ()
1006 {
1007   char c;
1008
1009   while (is_part_of_name (c = *input_line_pointer++))
1010     ;
1011   *--input_line_pointer = 0;
1012   return (c);
1013 }
1014
1015
1016 unsigned int 
1017 get_single_number ()
1018 {
1019   expressionS exp;
1020   operand (&exp);
1021   return exp.X_add_number;
1022
1023 }
1024
1025 /* end of expr.c */