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