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