]> Git Repo - binutils.git/blob - gas/expr.c
see devo/configure 1.11
[binutils.git] / gas / expr.c
1 /* expr.c -operands, expressions-
2    Copyright (C) 1987, 1990, 1991 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 1, 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 /* static const char rcsid[] = "$Id$"; */
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
32 #include "as.h"
33
34 #include "obstack.h"
35
36 #ifdef __STDC__
37 static void clean_up_expression(expressionS *expressionP);
38 #else /* __STDC__ */
39 static void clean_up_expression();      /* Internal. */
40 #endif /* __STDC__ */
41 extern const char EXP_CHARS[];  /* JF hide MD floating pt stuff all the same place */
42 extern const char FLT_CHARS[];
43
44 #ifdef LOCAL_LABELS_DOLLAR
45 extern int local_label_defined[];
46 #endif
47
48 /*
49  * Build any floating-point literal here.
50  * Also build any bignum literal here.
51  */
52
53 /* LITTLENUM_TYPE       generic_buffer [6]; */  /* JF this is a hack */
54 /* Seems atof_machine can backscan through generic_bignum and hit whatever
55    happens to be loaded before it in memory.  And its way too complicated
56    for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
57    and never write into the early words, thus they'll always be zero.
58    I hate Dean's floating-point code.  Bleh.
59  */
60 LITTLENUM_TYPE  generic_bignum [SIZE_OF_LARGE_NUMBER+6];
61 FLONUM_TYPE     generic_floating_point_number =
62 {
63   & generic_bignum [6],         /* low (JF: Was 0) */
64   & generic_bignum [SIZE_OF_LARGE_NUMBER+6 - 1], /* high JF: (added +6) */
65   0,                            /* leader */
66   0,                            /* exponent */
67   0                             /* sign */
68 };
69 /* If nonzero, we've been asked to assemble nan, +inf or -inf */
70 int generic_floating_point_magic;
71 \f
72 /*
73  * Summary of operand().
74  *
75  * in:  Input_line_pointer points to 1st char of operand, which may
76  *      be a space.
77  *
78  * out: A expressionS. X_seg determines how to understand the rest of the
79  *      expressionS.
80  *      The operand may have been empty: in this case X_seg == SEG_ABSENT.
81  *      Input_line_pointer->(next non-blank) char after operand.
82  *
83  */
84 \f
85 static segT
86 operand (expressionP)
87      register expressionS *     expressionP;
88 {
89   register char c;
90   register char *name;  /* points to name of symbol */
91   register symbolS *    symbolP; /* Points to symbol */
92
93   extern  char hex_value[];     /* In hex_value.c */
94
95   SKIP_WHITESPACE();            /* Leading whitespace is part of operand. */
96   c = * input_line_pointer ++;  /* Input_line_pointer->past char in c. */
97   if (isdigit(c))
98     {
99       register valueT   number; /* offset or (absolute) value */
100       register short int digit; /* value of next digit in current radix */
101                                 /* invented for humans only, hope */
102                                 /* optimising compiler flushes it! */
103       register short int radix; /* 2, 8, 10 or 16 */
104                                 /* 0 means we saw start of a floating- */
105                                 /* point constant. */
106       register short int maxdig = 0;/* Highest permitted digit value. */
107       register int too_many_digits = 0; /* If we see >= this number of */
108                                 /* digits, assume it is a bignum. */
109       register char *   digit_2; /*->2nd digit of number. */
110                int small;       /* TRUE if fits in 32 bits. */
111
112       if (c == '0') {                   /* non-decimal radix */
113               if ((c = *input_line_pointer ++)=='x' || c=='X') {
114                       c = *input_line_pointer ++; /* read past "0x" or "0X" */
115                       maxdig = radix = 16;
116                       too_many_digits = 9;
117               } else {
118                       /* If it says '0f' and the line ends or it DOESN'T look like
119                          a floating point #, its a local label ref.  DTRT */
120                       /* likewise for the b's.  xoxorich. */
121                       if ((c == 'f' || c == 'b' || c == 'B')
122                           && (!*input_line_pointer ||
123                               (!strchr("+-.0123456789",*input_line_pointer) &&
124                                !strchr(EXP_CHARS,*input_line_pointer)))) {
125                               maxdig = radix = 10;
126                               too_many_digits = 11;
127                               c = '0';
128                               input_line_pointer -= 2;
129
130                       } else if (c == 'b' || c == 'B') {
131                               c = *input_line_pointer++;
132                               maxdig = radix = 2;
133                               too_many_digits = 33;
134
135                       } else if (c && strchr(FLT_CHARS,c)) {
136                               radix = 0;        /* Start of floating-point constant. */
137                               /* input_line_pointer->1st char of number. */
138                               expressionP->X_add_number =  -(isupper(c) ? tolower(c) : c);
139
140                       } else {          /* By elimination, assume octal radix. */
141                               radix = maxdig = 8;
142                               too_many_digits = 11;
143                       }
144               } /* c == char after "0" or "0x" or "0X" or "0e" etc. */
145       } else {
146               maxdig = radix = 10;
147               too_many_digits = 11;
148       } /* if operand starts with a zero */
149
150       if (radix) {                      /* Fixed-point integer constant. */
151                                 /* May be bignum, or may fit in 32 bits. */
152 /*
153  * Most numbers fit into 32 bits, and we want this case to be fast.
154  * So we pretend it will fit into 32 bits. If, after making up a 32
155  * bit number, we realise that we have scanned more digits than
156  * comfortably fit into 32 bits, we re-scan the digits coding
157  * them into a bignum. For decimal and octal numbers we are conservative: some
158  * numbers may be assumed bignums when in fact they do fit into 32 bits.
159  * Numbers of any radix can have excess leading zeros: we strive
160  * to recognise this and cast them back into 32 bits.
161  * We must check that the bignum really is more than 32
162  * bits, and change it back to a 32-bit number if it fits.
163  * The number we are looking for is expected to be positive, but
164  * if it fits into 32 bits as an unsigned number, we let it be a 32-bit
165  * number. The cavalier approach is for speed in ordinary cases.
166  */
167           digit_2 = input_line_pointer;
168           for (number=0;  (digit=hex_value[c])<maxdig;  c = * input_line_pointer ++)
169             {
170               number = number * radix + digit;
171             }
172           /* C contains character after number. */
173           /* Input_line_pointer->char after C. */
174           small = input_line_pointer - digit_2 < too_many_digits;
175           if (! small)
176             {
177               /*
178                * We saw a lot of digits. Manufacture a bignum the hard way.
179  */
180               LITTLENUM_TYPE *  leader; /*->high order littlenum of the bignum. */
181               LITTLENUM_TYPE *  pointer; /*->littlenum we are frobbing now. */
182               long carry;
183
184               leader = generic_bignum;
185               generic_bignum [0] = 0;
186               generic_bignum [1] = 0;
187                                 /* We could just use digit_2, but lets be mnemonic. */
188               input_line_pointer = -- digit_2; /*->1st digit. */
189               c = *input_line_pointer ++;
190               for (;   (carry = hex_value [c]) < maxdig;   c = * input_line_pointer ++)
191                 {
192                   for (pointer = generic_bignum;
193                        pointer <= leader;
194                        pointer ++)
195                     {
196                       long work;
197
198                       work = carry + radix * * pointer;
199                       * pointer = work & LITTLENUM_MASK;
200                       carry = work >> LITTLENUM_NUMBER_OF_BITS;
201                     }
202                   if (carry)
203                     {
204                       if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
205                         {       /* Room to grow a longer bignum. */
206                           * ++ leader = carry;
207                         }
208                     }
209                 }
210               /* Again, C is char after number, */
211               /* input_line_pointer->after C. */
212               know(sizeof (int) * 8 == 32);
213               know(LITTLENUM_NUMBER_OF_BITS == 16);
214               /* Hence the constant "2" in the next line. */
215               if (leader < generic_bignum + 2)
216                 {               /* Will fit into 32 bits. */
217                   number =
218                     ((generic_bignum [1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
219                     | (generic_bignum [0] & LITTLENUM_MASK);
220                   small = 1;
221                 }
222               else
223                 {
224                   number = leader - generic_bignum + 1; /* Number of littlenums in the bignum. */
225                 }
226             }
227           if (small)
228             {
229               /*
230                * Here with number, in correct radix. c is the next char.
231                * Note that unlike Un*x, we allow "011f" "0x9f" to
232                * both mean the same as the (conventional) "9f". This is simply easier
233                * than checking for strict canonical form. Syntax sux!
234  */
235               if (number<10)
236                 {
237                   if (0
238 #ifdef LOCAL_LABELS_FB
239                        || c=='b'
240 #endif
241 #ifdef LOCAL_LABELS_DOLLAR
242                        || (c=='$' && local_label_defined[number])
243 #endif
244                       )
245                     {
246                       /*
247                        * Backward ref to local label.
248                        * Because it is backward, expect it to be DEFINED.
249                        */
250                       /*
251                        * Construct a local label.
252                        */
253                       name = local_label_name ((int)number, 0);
254                       if (((symbolP = symbol_find(name)) != NULL) /* seen before */
255                           && (S_IS_DEFINED(symbolP))) /* symbol is defined: OK */
256                         {               /* Expected path: symbol defined. */
257                           /* Local labels are never absolute. Don't waste time checking absoluteness. */
258                           know((S_GET_SEGMENT(symbolP) == SEG_DATA) || (S_GET_SEGMENT(symbolP) == SEG_TEXT));
259                           expressionP->X_add_symbol = symbolP;
260                           expressionP->X_add_number = 0;
261                           expressionP->X_seg = S_GET_SEGMENT(symbolP);
262                         }
263                       else
264                         {               /* Either not seen or not defined. */
265                           as_bad("Backw. ref to unknown label \"%d:\", 0 assumed.",
266                                   number);
267                           expressionP->X_add_number = 0;
268                           expressionP->X_seg        = SEG_ABSOLUTE;
269                         }
270                     }
271                   else
272                     {
273                       if (0
274 #ifdef LOCAL_LABELS_FB
275                           || c == 'f'
276 #endif
277 #ifdef LOCAL_LABELS_DOLLAR
278                           || (c=='$' && !local_label_defined[number])
279 #endif
280                           )
281                         {
282                           /*
283                            * Forward reference. Expect symbol to be undefined or
284                            * unknown. Undefined: seen it before. Unknown: never seen
285                            * it in this pass.
286                            * Construct a local label name, then an undefined symbol.
287                            * Don't create a XSEG frag for it: caller may do that.
288                            * Just return it as never seen before.
289                            */
290                           name = local_label_name((int)number, 1);
291                           symbolP = symbol_find_or_make(name);
292                           /* We have no need to check symbol properties. */
293                           know(S_GET_SEGMENT(symbolP) == SEG_UNKNOWN
294                                || S_GET_SEGMENT(symbolP) == SEG_TEXT
295                                || S_GET_SEGMENT(symbolP) == SEG_DATA);
296                           expressionP->X_add_symbol      = symbolP;
297                           expressionP->X_seg             = SEG_UNKNOWN;
298                           expressionP->X_subtract_symbol = NULL;
299                           expressionP->X_add_number      = 0;
300                         }
301                       else
302                         {               /* Really a number, not a local label. */
303                           expressionP->X_add_number = number;
304                           expressionP->X_seg        = SEG_ABSOLUTE;
305                           input_line_pointer --; /* Restore following character. */
306                         }               /* if (c=='f') */
307                     }                   /* if (c=='b') */
308                 }
309               else
310                 {                       /* Really a number. */
311                   expressionP->X_add_number = number;
312                   expressionP->X_seg        = SEG_ABSOLUTE;
313                   input_line_pointer --; /* Restore following character. */
314                 }                       /* if (number<10) */
315             }
316           else
317             {
318               expressionP->X_add_number = number;
319               expressionP->X_seg = SEG_BIG;
320               input_line_pointer --; /*->char following number. */
321             }                   /* if (small) */
322         }                       /* (If integer constant) */
323       else
324         {                       /* input_line_pointer->*/
325                                 /* floating-point constant. */
326           int error_code;
327
328           error_code = atof_generic
329             (& input_line_pointer, ".", EXP_CHARS,
330              & generic_floating_point_number);
331
332           if (error_code)
333             {
334               if (error_code == ERROR_EXPONENT_OVERFLOW)
335                 {
336                   as_bad("Bad floating-point constant: exponent overflow, probably assembling junk");
337                 }
338               else
339                 {
340                   as_bad("Bad floating-point constant: unknown error code=%d.", error_code);
341                 }
342             }
343           expressionP->X_seg = SEG_BIG;
344                                 /* input_line_pointer->just after constant, */
345                                 /* which may point to whitespace. */
346           know(expressionP->X_add_number < 0); /* < 0 means "floating point". */
347         }                       /* if (not floating-point constant) */
348     }
349   else if(c=='.' && !is_part_of_name(*input_line_pointer)) {
350     extern struct obstack frags;
351
352     /*
353        JF:  '.' is pseudo symbol with value of current location in current
354        segment. . .
355  */
356     symbolP = symbol_new("L0\001",
357                          now_seg,
358                          (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
359                          frag_now);
360
361     expressionP->X_add_number=0;
362     expressionP->X_add_symbol=symbolP;
363     expressionP->X_seg = now_seg;
364
365   } else if (is_name_beginner(c)) /* here if did not begin with a digit */
366     {
367       /*
368        * Identifier begins here.
369        * This is kludged for speed, so code is repeated.
370        */
371       name =  -- input_line_pointer;
372       c = get_symbol_end();
373       symbolP = symbol_find_or_make(name);
374       /*
375        * If we have an absolute symbol or a reg, then we know its value now.
376        */
377       expressionP->X_seg = S_GET_SEGMENT(symbolP);
378       switch (expressionP->X_seg)
379         {
380         case SEG_ABSOLUTE:
381         case SEG_REGISTER:
382           expressionP->X_add_number = S_GET_VALUE(symbolP);
383           break;
384
385         default:
386           expressionP->X_add_number  = 0;
387           expressionP->X_add_symbol  = symbolP;
388         }
389       * input_line_pointer = c;
390       expressionP->X_subtract_symbol = NULL;
391     }
392   else if (c=='(')/* didn't begin with digit & not a name */
393     {
394       (void)expression(expressionP);
395       /* Expression() will pass trailing whitespace */
396       if (* input_line_pointer ++ != ')')
397         {
398           as_bad("Missing ')' assumed");
399           input_line_pointer --;
400         }
401       /* here with input_line_pointer->char after "(...)" */
402     }
403   else if (c == '~' || c == '-' || c == '+') {
404           /* unary operator: hope for SEG_ABSOLUTE */
405       switch (operand (expressionP)) {
406       case SEG_ABSOLUTE:
407               /* input_line_pointer->char after operand */
408               if (c=='-') {
409                       expressionP->X_add_number = - expressionP->X_add_number;
410                       /*
411                        * Notice: '-' may  overflow: no warning is given. This is compatible
412                        * with other people's assemblers. Sigh.
413                        */
414               } else if (c == '~') {
415                       expressionP->X_add_number = ~ expressionP->X_add_number;
416               } else if (c != '+') {
417                       know(0);
418               } /* switch on unary operator */
419               break;
420
421       case SEG_TEXT:
422       case SEG_DATA:
423       case SEG_BSS:
424       case SEG_PASS1:
425       case SEG_UNKNOWN:
426         if(c=='-') {            /* JF I hope this hack works */
427           expressionP->X_subtract_symbol=expressionP->X_add_symbol;
428           expressionP->X_add_symbol=0;
429           expressionP->X_seg=SEG_DIFFERENCE;
430           break;
431         }
432       default:          /* unary on non-absolute is unsuported */
433         as_bad("Unary operator %c ignored because bad operand follows", c);
434         break;
435         /* Expression undisturbed from operand(). */
436       }
437     }
438   else if (c=='\'')
439     {
440 /*
441  * Warning: to conform to other people's assemblers NO ESCAPEMENT is permitted
442  * for a single quote. The next character, parity errors and all, is taken
443  * as the value of the operand. VERY KINKY.
444  */
445       expressionP->X_add_number = * input_line_pointer ++;
446       expressionP->X_seg        = SEG_ABSOLUTE;
447     }
448   else
449     {
450                       /* can't imagine any other kind of operand */
451       expressionP->X_seg = SEG_ABSENT;
452       input_line_pointer --;
453       md_operand (expressionP);
454     }
455 /*
456  * It is more 'efficient' to clean up the expressions when they are created.
457  * Doing it here saves lines of code.
458  */
459   clean_up_expression (expressionP);
460   SKIP_WHITESPACE();            /*->1st char after operand. */
461   know(* input_line_pointer != ' ');
462   return (expressionP->X_seg);
463 } /* operand() */
464 \f
465 /* Internal. Simplify a struct expression for use by expr() */
466
467 /*
468  * In:  address of a expressionS.
469  *      The X_seg field of the expressionS may only take certain values.
470  *      Now, we permit SEG_PASS1 to make code smaller & faster.
471  *      Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
472  * Out: expressionS may have been modified:
473  *      'foo-foo' symbol references cancelled to 0,
474  *              which changes X_seg from SEG_DIFFERENCE to SEG_ABSOLUTE;
475  *      Unused fields zeroed to help expr().
476  */
477
478 static void
479 clean_up_expression (expressionP)
480      register expressionS * expressionP;
481 {
482   switch (expressionP->X_seg)
483     {
484     case SEG_ABSENT:
485     case SEG_PASS1:
486       expressionP->X_add_symbol = NULL;
487       expressionP->X_subtract_symbol    = NULL;
488       expressionP->X_add_number = 0;
489       break;
490
491     case SEG_BIG:
492     case SEG_ABSOLUTE:
493       expressionP->X_subtract_symbol    = NULL;
494       expressionP->X_add_symbol = NULL;
495       break;
496
497     case SEG_TEXT:
498     case SEG_DATA:
499     case SEG_BSS:
500     case SEG_UNKNOWN:
501       expressionP->X_subtract_symbol    = NULL;
502       break;
503
504     case SEG_DIFFERENCE:
505       /*
506        * It does not hurt to 'cancel' NULL==NULL
507        * when comparing symbols for 'eq'ness.
508        * It is faster to re-cancel them to NULL
509        * than to check for this special case.
510  */
511       if (expressionP->X_subtract_symbol == expressionP->X_add_symbol
512           || (expressionP->X_subtract_symbol
513               && expressionP->X_add_symbol
514               && expressionP->X_subtract_symbol->sy_frag==expressionP->X_add_symbol->sy_frag
515               && S_GET_VALUE(expressionP->X_subtract_symbol) == S_GET_VALUE(expressionP->X_add_symbol))) {
516               expressionP->X_subtract_symbol    = NULL;
517               expressionP->X_add_symbol         = NULL;
518               expressionP->X_seg                        = SEG_ABSOLUTE;
519       }
520       break;
521
522     case SEG_REGISTER:
523       expressionP->X_add_symbol = NULL;
524       expressionP->X_subtract_symbol    = NULL;
525       break;
526
527     default:
528       BAD_CASE (expressionP->X_seg);
529       break;
530     }
531 } /* clean_up_expression() */
532 \f
533 /*
534  *                      expr_part ()
535  *
536  * Internal. Made a function because this code is used in 2 places.
537  * Generate error or correct X_?????_symbol of expressionS.
538  */
539
540 /*
541  * symbol_1 += symbol_2 ... well ... sort of.
542  */
543
544 static segT
545 expr_part (symbol_1_PP, symbol_2_P)
546      symbolS ** symbol_1_PP;
547      symbolS *  symbol_2_P;
548 {
549   segT                  return_value;
550
551   know((* symbol_1_PP)                          == NULL
552        || (S_GET_SEGMENT(*symbol_1_PP) == SEG_TEXT)
553        || (S_GET_SEGMENT(*symbol_1_PP) == SEG_DATA)
554        || (S_GET_SEGMENT(*symbol_1_PP) == SEG_BSS)
555        || (!S_IS_DEFINED(* symbol_1_PP)));
556   know(symbol_2_P             == NULL
557        || (S_GET_SEGMENT(symbol_2_P) == SEG_TEXT)
558        || (S_GET_SEGMENT(symbol_2_P) == SEG_DATA)
559        || (S_GET_SEGMENT(symbol_2_P) == SEG_BSS)
560        || (!S_IS_DEFINED(symbol_2_P)));
561   if (* symbol_1_PP)
562     {
563       if (!S_IS_DEFINED(* symbol_1_PP))
564         {
565           if (symbol_2_P)
566             {
567               return_value = SEG_PASS1;
568               * symbol_1_PP = NULL;
569             }
570           else
571             {
572               know(!S_IS_DEFINED(* symbol_1_PP));
573               return_value = SEG_UNKNOWN;
574             }
575         }
576       else
577         {
578           if (symbol_2_P)
579             {
580               if (!S_IS_DEFINED(symbol_2_P))
581                 {
582                   * symbol_1_PP = NULL;
583                   return_value = SEG_PASS1;
584                 }
585               else
586                 {
587                   /* {seg1} - {seg2} */
588                   as_bad("Expression too complex, 2 symbols forgotten: \"%s\" \"%s\"",
589                           S_GET_NAME(* symbol_1_PP), S_GET_NAME(symbol_2_P));
590                   * symbol_1_PP = NULL;
591                   return_value = SEG_ABSOLUTE;
592                 }
593             }
594           else
595             {
596               return_value = S_GET_SEGMENT(* symbol_1_PP);
597             }
598         }
599     }
600   else
601     {                           /* (* symbol_1_PP) == NULL */
602       if (symbol_2_P)
603         {
604           * symbol_1_PP = symbol_2_P;
605           return_value = S_GET_SEGMENT(symbol_2_P);
606         }
607       else
608         {
609           * symbol_1_PP = NULL;
610           return_value = SEG_ABSOLUTE;
611         }
612     }
613   know(return_value == SEG_ABSOLUTE
614        || return_value == SEG_TEXT
615        || return_value == SEG_DATA
616        || return_value == SEG_BSS
617        || return_value == SEG_UNKNOWN
618        || return_value == SEG_PASS1);
619   know((* symbol_1_PP) == NULL
620        || (S_GET_SEGMENT(* symbol_1_PP) == return_value));
621   return (return_value);
622 }                               /* expr_part() */
623 \f
624 /* Expression parser. */
625
626 /*
627  * We allow an empty expression, and just assume (absolute,0) silently.
628  * Unary operators and parenthetical expressions are treated as operands.
629  * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
630  *
631  * We used to do a aho/ullman shift-reduce parser, but the logic got so
632  * warped that I flushed it and wrote a recursive-descent parser instead.
633  * Now things are stable, would anybody like to write a fast parser?
634  * Most expressions are either register (which does not even reach here)
635  * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
636  * So I guess it doesn't really matter how inefficient more complex expressions
637  * are parsed.
638  *
639  * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
640  * Also, we have consumed any leading or trailing spaces (operand does that)
641  * and done all intervening operators.
642  */
643
644 typedef enum
645 {
646 O_illegal,                      /* (0)  what we get for illegal op */
647
648 O_multiply,                     /* (1)  * */
649 O_divide,                       /* (2)  / */
650 O_modulus,                      /* (3)  % */
651 O_left_shift,                   /* (4)  < */
652 O_right_shift,                  /* (5)  > */
653 O_bit_inclusive_or,             /* (6)  | */
654 O_bit_or_not,                   /* (7)  ! */
655 O_bit_exclusive_or,             /* (8)  ^ */
656 O_bit_and,                      /* (9)  & */
657 O_add,                          /* (10) + */
658 O_subtract                      /* (11) - */
659 }
660 operatorT;
661
662 #define __ O_illegal
663
664 static const operatorT op_encoding [256] = {    /* maps ASCII->operators */
665
666 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
667 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
668
669 __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
670 __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
671 __, __, __, __, __, __, __, __,
672 __, __, __, __, O_left_shift, __, O_right_shift, __,
673 __, __, __, __, __, __, __, __,
674 __, __, __, __, __, __, __, __,
675 __, __, __, __, __, __, __, __,
676 __, __, __, __, __, __, O_bit_exclusive_or, __,
677 __, __, __, __, __, __, __, __,
678 __, __, __, __, __, __, __, __,
679 __, __, __, __, __, __, __, __,
680 __, __, __, __, O_bit_inclusive_or, __, __, __,
681
682 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
683 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
684 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
685 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
686 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
687 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
688 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
689 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
690 };
691
692
693 /*
694  *      Rank    Examples
695  *      0       operand, (expression)
696  *      1       + -
697  *      2       & ^ ! |
698  *      3       * / % << >>
699  */
700 static const operator_rankT
701 op_rank [] = { 0, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
702 \f
703 /* Return resultP->X_seg. */
704 segT expr(rank, resultP)
705 register operator_rankT rank; /* Larger # is higher rank. */
706 register expressionS *resultP; /* Deliver result here. */
707 {
708   expressionS           right;
709   register operatorT    op_left;
710   register char c_left; /* 1st operator character. */
711   register operatorT    op_right;
712   register char c_right;
713
714   know(rank >= 0);
715   (void)operand (resultP);
716   know(* input_line_pointer != ' '); /* Operand() gobbles spaces. */
717   c_left = * input_line_pointer; /* Potential operator character. */
718   op_left = op_encoding [c_left];
719   while (op_left != O_illegal && op_rank [(int) op_left] > rank)
720     {
721       input_line_pointer ++;    /*->after 1st character of operator. */
722                                 /* Operators "<<" and ">>" have 2 characters. */
723       if (* input_line_pointer == c_left && (c_left == '<' || c_left == '>'))
724         {
725           input_line_pointer ++;
726         }                       /*->after operator. */
727       if (SEG_ABSENT == expr (op_rank[(int) op_left], &right))
728         {
729           as_warn("Missing operand value assumed absolute 0.");
730           resultP->X_add_number = 0;
731           resultP->X_subtract_symbol    = NULL;
732           resultP->X_add_symbol = NULL;
733           resultP->X_seg = SEG_ABSOLUTE;
734         }
735       know(* input_line_pointer != ' ');
736       c_right = * input_line_pointer;
737       op_right = op_encoding [c_right];
738       if (* input_line_pointer == c_right && (c_right == '<' || c_right == '>'))
739         {
740           input_line_pointer ++;
741         }                       /*->after operator. */
742       know((int) op_right == 0
743            || op_rank [(int) op_right] <= op_rank[(int) op_left]);
744       /* input_line_pointer->after right-hand quantity. */
745       /* left-hand quantity in resultP */
746       /* right-hand quantity in right. */
747       /* operator in op_left. */
748       if (resultP->X_seg == SEG_PASS1 || right . X_seg == SEG_PASS1)
749         {
750           resultP->X_seg = SEG_PASS1;
751         }
752       else
753         {
754           if (resultP->X_seg == SEG_BIG)
755             {
756               as_warn("Left operand of %c is a %s.  Integer 0 assumed.",
757                       c_left, resultP->X_add_number > 0 ? "bignum" : "float");
758               resultP->X_seg = SEG_ABSOLUTE;
759               resultP->X_add_symbol = 0;
760               resultP->X_subtract_symbol = 0;
761               resultP->X_add_number = 0;
762             }
763           if (right . X_seg == SEG_BIG)
764             {
765               as_warn("Right operand of %c is a %s.  Integer 0 assumed.",
766                       c_left, right . X_add_number > 0 ? "bignum" : "float");
767               right . X_seg = SEG_ABSOLUTE;
768               right . X_add_symbol = 0;
769               right . X_subtract_symbol = 0;
770               right . X_add_number = 0;
771             }
772           if (op_left == O_subtract)
773             {
774               /*
775                * Convert - into + by exchanging symbols and negating number.
776                * I know -infinity can't be negated in 2's complement:
777                * but then it can't be subtracted either. This trick
778                * does not cause any further inaccuracy.
779                */
780
781               register symbolS *        symbolP;
782
783               right . X_add_number      = - right . X_add_number;
784               symbolP                   = right . X_add_symbol;
785               right . X_add_symbol      = right . X_subtract_symbol;
786               right . X_subtract_symbol = symbolP;
787               if (symbolP)
788                 {
789                   right . X_seg         = SEG_DIFFERENCE;
790                 }
791               op_left = O_add;
792             }
793 \f
794           if (op_left == O_add)
795             {
796               segT      seg1;
797               segT      seg2;
798
799               know(resultP->X_seg == SEG_DATA
800                    || resultP->X_seg == SEG_TEXT
801                    || resultP->X_seg == SEG_BSS
802                    || resultP->X_seg == SEG_UNKNOWN
803                    || resultP->X_seg == SEG_DIFFERENCE
804                    || resultP->X_seg == SEG_ABSOLUTE
805                    || resultP->X_seg == SEG_PASS1);
806               know(right .  X_seg == SEG_DATA
807                    ||   right .  X_seg == SEG_TEXT
808                    ||   right .  X_seg == SEG_BSS
809                    ||   right .  X_seg == SEG_UNKNOWN
810                    ||   right .  X_seg == SEG_DIFFERENCE
811                    ||   right .  X_seg == SEG_ABSOLUTE
812                    ||   right .  X_seg == SEG_PASS1);
813
814               clean_up_expression (& right);
815               clean_up_expression (resultP);
816
817               seg1 = expr_part (& resultP->X_add_symbol, right . X_add_symbol);
818               seg2 = expr_part (& resultP->X_subtract_symbol, right . X_subtract_symbol);
819               if (seg1 == SEG_PASS1 || seg2 == SEG_PASS1) {
820                       need_pass_2 = 1;
821                       resultP->X_seg = SEG_PASS1;
822               } else if (seg2 == SEG_ABSOLUTE)
823                   resultP->X_seg = seg1;
824               else if (seg1 != SEG_UNKNOWN
825                        && seg1 != SEG_ABSOLUTE
826                        && seg2 != SEG_UNKNOWN
827                        && seg1 != seg2) {
828                       know(seg2 != SEG_ABSOLUTE);
829                       know(resultP->X_subtract_symbol);
830
831                       know(seg1 == SEG_TEXT || seg1 == SEG_DATA || seg1== SEG_BSS);
832                       know(seg2 == SEG_TEXT || seg2 == SEG_DATA || seg2== SEG_BSS);
833                       know(resultP->X_add_symbol);
834                       know(resultP->X_subtract_symbol);
835                       as_bad("Expression too complex: forgetting %s - %s",
836                               S_GET_NAME(resultP->X_add_symbol),
837                               S_GET_NAME(resultP->X_subtract_symbol));
838                       resultP->X_seg = SEG_ABSOLUTE;
839                       /* Clean_up_expression() will do the rest. */
840               } else
841                   resultP->X_seg = SEG_DIFFERENCE;
842
843               resultP->X_add_number += right . X_add_number;
844               clean_up_expression (resultP);
845       }
846           else
847             {                   /* Not +. */
848               if (resultP->X_seg == SEG_UNKNOWN || right . X_seg == SEG_UNKNOWN)
849                 {
850                   resultP->X_seg = SEG_PASS1;
851                   need_pass_2 = 1;
852                 }
853               else
854                 {
855                   resultP->X_subtract_symbol = NULL;
856                   resultP->X_add_symbol = NULL;
857                   /* Will be SEG_ABSOLUTE. */
858                   if (resultP->X_seg != SEG_ABSOLUTE || right . X_seg != SEG_ABSOLUTE)
859                     {
860                       as_bad("Relocation error. Absolute 0 assumed.");
861                       resultP->X_seg        = SEG_ABSOLUTE;
862                       resultP->X_add_number = 0;
863                     }
864                   else
865                     {
866                       switch (op_left)
867                         {
868                         case O_bit_inclusive_or:
869                           resultP->X_add_number |= right . X_add_number;
870                           break;
871
872                         case O_modulus:
873                           if (right . X_add_number)
874                             {
875                               resultP->X_add_number %= right . X_add_number;
876                             }
877                           else
878                             {
879                               as_warn("Division by 0. 0 assumed.");
880                               resultP->X_add_number = 0;
881                             }
882                           break;
883
884                         case O_bit_and:
885                           resultP->X_add_number &= right . X_add_number;
886                           break;
887
888                         case O_multiply:
889                           resultP->X_add_number *= right . X_add_number;
890                           break;
891
892                         case O_divide:
893                           if (right . X_add_number)
894                             {
895                               resultP->X_add_number /= right . X_add_number;
896                             }
897                           else
898                             {
899                               as_warn("Division by 0. 0 assumed.");
900                               resultP->X_add_number = 0;
901                             }
902                           break;
903
904                         case O_left_shift:
905                           resultP->X_add_number <<= right . X_add_number;
906                           break;
907
908                         case O_right_shift:
909                           resultP->X_add_number >>= right . X_add_number;
910                           break;
911
912                         case O_bit_exclusive_or:
913                           resultP->X_add_number ^= right . X_add_number;
914                           break;
915
916                         case O_bit_or_not:
917                           resultP->X_add_number |= ~ right . X_add_number;
918                           break;
919
920                         default:
921                           BAD_CASE(op_left);
922                           break;
923                         } /* switch(operator) */
924                     }
925                 }               /* If we have to force need_pass_2. */
926             }                   /* If operator was +. */
927         }                       /* If we didn't set need_pass_2. */
928       op_left = op_right;
929     }                           /* While next operator is >= this rank. */
930   return (resultP->X_seg);
931 }
932 \f
933 /*
934  *                      get_symbol_end()
935  *
936  * This lives here because it belongs equally in expr.c & read.c.
937  * Expr.c is just a branch office read.c anyway, and putting it
938  * here lessens the crowd at read.c.
939  *
940  * Assume input_line_pointer is at start of symbol name.
941  * Advance input_line_pointer past symbol name.
942  * Turn that character into a '\0', returning its former value.
943  * This allows a string compare (RMS wants symbol names to be strings)
944  * of the symbol name.
945  * There will always be a char following symbol name, because all good
946  * lines end in end-of-line.
947  */
948 char
949 get_symbol_end()
950 {
951   register char c;
952
953   while (is_part_of_name(c = * input_line_pointer ++))
954     ;
955   * -- input_line_pointer = 0;
956   return (c);
957 }
958
959 /*
960  * Local Variables:
961  * comment-column: 0
962  * fill-column: 131
963  * End:
964  */
965
966 /* end: expr.c */
This page took 0.079821 seconds and 4 git commands to generate.