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