2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // EXPR.C - Expression Analyzer
4 // Copyright (C) 199x Landon Dyer, 2017 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
20 #define DEF_KW // Declare keyword values
21 #include "kwtab.h" // Incl generated keyword tables & defs
23 // N.B.: The size of tokenClass should be identical to the largest value of
24 // a token; we're assuming 256 but not 100% sure!
25 static char tokenClass[256]; // Generated table of token classes
26 static VALUE evstk[EVSTACKSIZE]; // Evaluator value stack
27 static WORD evattr[EVSTACKSIZE]; // Evaluator attribute stack
29 // Token-class initialization list
32 CONST, SYMBOL, 0, // ID
33 '(', '[', '{', 0, // OPAR
34 ')', ']', '}', 0, // CPAR
35 CR_DEFINED, CR_REFERENCED, // SUNARY (special unary)
39 '!', '~', UNMINUS, 0, // UNARY
40 '*', '/', '%', 0, // MULT
43 LE, GE, '<', '>', NE, '=', 0, // REL
50 const char missym_error[] = "missing symbol";
51 const char str_error[] = "missing symbol or string";
53 // Convert expression to postfix
54 static TOKEN * evalTokenBuffer; // Deposit tokens here (this is really a
55 // pointer to exprbuf from direct.c)
56 // (Can also be from others, like
58 static int symbolNum; // Pointer to the entry in symbolPtr[]
62 // Obtain a string value
64 static VALUE str_value(char * p)
69 v = (v << 8) | (*p & 0xFF);
76 // Initialize expression analyzer
78 void InitExpression(void)
80 // Initialize token-class table (all set to END)
81 for(int i=0; i<256; i++)
86 for(char * p=itokcl; *p!=1; p++)
91 tokenClass[(int)(*p)] = (char)i;
99 // Binary operators (all the same precedence)
108 while (tokenClass[*tok] >= MULT)
115 *evalTokenBuffer++ = t;
123 // Unary operators (detect unary '-')
124 // ggn: If expression starts with a plus then also eat it up.
125 // For some reason the parser gets confused when this happens and
126 // emits a "bad expression".
137 class = tokenClass[*tok];
139 if (*tok == '-' || *tok == '+' || class == UNARY)
149 *evalTokenBuffer++ = t;
151 else if (class == SUNARY)
156 *evalTokenBuffer++ = CONST;
157 *evalTokenBuffer++ = (LONG)sect[ABS].sloc;
160 *evalTokenBuffer++ = CONST;
161 *evalTokenBuffer++ = dos_time();
164 *evalTokenBuffer++ = CONST;
165 *evalTokenBuffer++ = dos_date();
167 case CR_MACDEF: // ^^macdef <macro-name>
168 if (*tok++ != SYMBOL)
169 return error(missym_error);
172 w = (lookup(p, MACRO, 0) == NULL ? 0 : 1);
173 *evalTokenBuffer++ = CONST;
174 *evalTokenBuffer++ = (TOKEN)w;
182 if (*tok++ != SYMBOL)
183 return error(missym_error);
186 j = (*p == '.' ? curenv : 0);
187 w = ((sy = lookup(p, LABEL, j)) != NULL && (sy->sattr & w) ? 1 : 0);
188 *evalTokenBuffer++ = CONST;
189 *evalTokenBuffer++ = (TOKEN)w;
192 if (*tok != SYMBOL && *tok != STRING)
193 return error(str_error);
199 return error(comma_error);
201 if (*tok != SYMBOL && *tok != STRING)
202 return error(str_error);
207 w = (WORD)(!strcmp(p, p2));
208 *evalTokenBuffer++ = CONST;
209 *evalTokenBuffer++ = (TOKEN)w;
221 // Terminals (CONSTs) and parenthesis grouping
232 *evalTokenBuffer++ = CONST;
233 *evalTokenBuffer++ = *tok++;
237 j = (*p == '.' ? curenv : 0);
238 sy = lookup(p, LABEL, j);
241 sy = NewSymbol(p, LABEL, j);
243 // Check register bank usage
244 if (sy->sattre & EQUATEDREG)
246 if ((regbank == BANK_0) && (sy->sattre & BANK_1) && !altbankok)
247 warns("equated symbol \'%s\' cannot be used in register bank 0", sy->sname);
249 if ((regbank == BANK_1) && (sy->sattre & BANK_0) && !altbankok)
250 warns("equated symbol \'%s\' cannot be used in register bank 1", sy->sname);
253 *evalTokenBuffer++ = SYMBOL;
254 *evalTokenBuffer++ = symbolNum;
255 symbolPtr[symbolNum] = sy;
259 *evalTokenBuffer++ = CONST;
260 *evalTokenBuffer++ = str_value(string[*tok++]);
267 return error("missing close parenthesis ')'");
275 return error("missing close parenthesis ']'");
279 *evalTokenBuffer++ = ACONST; // Attributed const
280 *evalTokenBuffer++ = sloc; // Current location
281 *evalTokenBuffer++ = cursect | DEFINED; // Store attribs
284 *evalTokenBuffer++ = ACONST; // Attributed const
286 // pcloc == location at start of line
287 *evalTokenBuffer++ = (orgactive ? orgaddr : pcloc);
288 // '*' takes attributes of current section, not ABS!
289 *evalTokenBuffer++ = cursect | DEFINED;
292 return error("bad expression");
300 // Recursive-descent expression analyzer (with some simple speed hacks)
302 int expr(TOKEN * otk, VALUE * a_value, WORD * a_attr, SYM ** a_esym)
304 // Passed in values (once derefenced, that is) can all be zero. They are
305 // there so that the expression analyzer can fill them in as needed. The
306 // expression analyzer gets its input from the global token pointer "tok",
307 // and not from anything passed in by the user.
312 evalTokenBuffer = otk; // Set token pointer to 'exprbuf' (direct.c)
313 // Also set in various other places too (riscasm.c, e.g.)
315 //printf("expr(): tokens 0-2: %i %i %i (%c %c %c); tc[2] = %i\n", tok[0], tok[1], tok[2], tok[0], tok[1], tok[2], tokenClass[tok[2]]);
316 // Optimize for single constant or single symbol.
317 // Shamus: Subtle bug here. EOL token is 101; if you have a constant token
318 // followed by the value 101, it will trigger a bad evaluation here.
319 // This is probably a really bad assumption to be making here...!
320 // (assuming tok[1] == EOL is a single token that is)
321 // Seems that even other tokens (SUNARY type) can fuck this up too.
322 // if ((tok[1] == EOL)
323 if ((tok[1] == EOL && (tok[0] != CONST && tokenClass[tok[0]] != SUNARY))
324 || (((*tok == CONST || *tok == SYMBOL) || (*tok >= KW_R0 && *tok <= KW_R31))
325 && (tokenClass[tok[2]] < UNARY)))
327 if (*tok >= KW_R0 && *tok <= KW_R31)
329 *evalTokenBuffer++ = CONST;
330 *evalTokenBuffer++ = *a_value = (*tok - KW_R0);
331 *a_attr = ABS | DEFINED;
338 else if (*tok == CONST)
340 *evalTokenBuffer++ = CONST;
341 *evalTokenBuffer++ = *a_value = tok[1];
342 *a_attr = ABS | DEFINED;
348 //printf("Quick eval in expr(): CONST = %i, tokenClass[tok[2]] = %i\n", *a_value, tokenClass[*tok]);
350 else if (*tok == '*')
352 *evalTokenBuffer++ = CONST;
355 *evalTokenBuffer++ = *a_value = orgaddr;
357 *evalTokenBuffer++ = *a_value = pcloc;
359 // '*' takes attributes of current section, not ABS!
360 *a_attr = cursect | DEFINED;
367 else if (*tok == STRING || *tok == SYMBOL)
370 j = (*p == '.' ? curenv : 0);
371 symbol = lookup(p, LABEL, j);
373 printf("eval: Looking up symbol (%s) [=%08X]\n", p, symbol);
375 printf(" attr=%04X, attre=%08X, val=%i, name=%s\n", symbol->sattr, symbol->sattre, symbol->svalue, symbol->sname);
379 symbol = NewSymbol(p, LABEL, j);
381 symbol->sattr |= REFERENCED;
383 // Check for undefined register equates, but only if it's not part
384 // of a #<SYMBOL> construct, as it could be that the label that's
385 // been undefined may later be used as an address label--which
386 // means it will be fixed up later, and thus, not an error.
387 if ((symbol->sattre & UNDEF_EQUR) && !riscImmTokenSeen)
389 errors("undefined register equate '%s'", symbol->sname);
390 //if we return right away, it returns some spurious errors...
394 // Check register bank usage
395 if (symbol->sattre & EQUATEDREG)
397 if ((regbank == BANK_0) && (symbol->sattre & BANK_1) && !altbankok)
398 warns("equated symbol '%s' cannot be used in register bank 0", symbol->sname);
400 if ((regbank == BANK_1) && (symbol->sattre & BANK_0) && !altbankok)
401 warns("equated symbol '%s' cannot be used in register bank 1", symbol->sname);
404 *evalTokenBuffer++ = SYMBOL;
406 *evalTokenBuffer++ = (TOKEN)symbol;
409 While this approach works, it's wasteful. It would be better to use something
410 that's already available, like the symbol "order defined" table (which needs to
411 be converted from a linked list into an array).
413 *evalTokenBuffer++ = symbolNum;
414 symbolPtr[symbolNum] = symbol;
418 if (symbol->sattr & DEFINED)
419 *a_value = symbol->svalue;
424 All that extra crap that was put into the svalue when doing the equr stuff is
425 thrown away right here. What the hell is it for?
427 if (symbol->sattre & EQUATEDREG)
430 *a_attr = (WORD)(symbol->sattr & ~GLOBAL);
432 if ((symbol->sattr & (GLOBAL | DEFINED)) == GLOBAL
440 // Unknown type here... Alert the user!,
441 error("undefined RISC register in expression");
442 // Prevent spurious error reporting...
447 *evalTokenBuffer++ = ENDEXPR;
454 *evalTokenBuffer++ = ENDEXPR;
455 return evexpr(otk, a_value, a_attr, a_esym);
460 // Evaluate expression.
461 // If the expression involves only ONE external symbol, the expression is
462 // UNDEFINED, but it's value includes everything but the symbol value, and
463 // `a_esym' is set to the external symbol.
465 int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym)
474 sval = evstk; // (Empty) initial stack
476 esym = NULL; // No external symbol involved
479 while (*tk != ENDEXPR)
484 //printf("evexpr(): SYMBOL\n");
485 sy = symbolPtr[*tk++];
486 sy->sattr |= REFERENCED; // Set "referenced" bit
488 if (!(sy->sattr & DEFINED))
490 // Reference to undefined symbol
491 if (!(sy->sattr & GLOBAL))
498 if (esym != NULL) // Check for multiple externals
499 return error(seg_error);
504 if (sy->sattr & DEFINED)
506 *++sval = sy->svalue; // Push symbol's value
510 *++sval = 0; // 0 for undefined symbols
513 *++sattr = (WORD)(sy->sattr & ~GLOBAL); // Push attribs
514 sym_seg = (WORD)(sy->sattr & TDB);
517 //printf("evexpr(): CONST = %i\n", *tk);
518 *++sval = *tk++; // Push value
519 *++sattr = ABS | DEFINED; // Push simple attribs
522 //printf("evexpr(): ACONST = %i\n", *tk);
523 *++sval = *tk++; // Push value
524 *++sattr = (WORD)*tk++; // Push attribs
527 // Binary "+" and "-" matrix:
530 // ----------------------------
531 // ABS | ABS | Sect | Other |
532 // Sect | Sect | [1] | Error |
533 // Other | Other | Error | [1] |
534 // ----------------------------
539 //printf("evexpr(): +\n");
541 --sattr; // Pop attrib
542 //printf("--> N+N: %i + %i = ", *sval, sval[1]);
543 *sval += sval[1]; // Compute value
544 //printf("%i\n", *sval);
548 else if (sattr[1] & TDB)
549 return error(seg_error);
553 //printf("evexpr(): -\n");
555 --sattr; // Pop attrib
556 //printf("--> N-N: %i - %i = ", *sval, sval[1]);
557 *sval -= sval[1]; // Compute value
558 //printf("%i\n", *sval);
560 attr = (WORD)(*sattr & TDB);
562 printf("EVEXPR (-): sym1 = %X, sym2 = %X\n", attr, sattr[1]);
564 // If symbol1 is ABS, take attributes from symbol2
567 // Otherwise, they're both TDB and so attributes cancel out
568 else if (sattr[1] & TDB)
572 // Unary operators only work on ABS items
574 //printf("evexpr(): UNMINUS\n");
579 *sattr = ABS | DEFINED; // Expr becomes absolute
582 //printf("evexpr(): !\n");
587 *sattr = ABS | DEFINED; // Expr becomes absolute
590 //printf("evexpr(): ~\n");
595 *sattr = ABS | DEFINED; // Expr becomes absolute
597 // Comparison operators must have two values that
598 // are in the same segment, but that's the only requirement.
600 //printf("evexpr(): LE\n");
604 if ((*sattr & TDB) != (sattr[1] & TDB))
607 *sattr = ABS | DEFINED;
608 *sval = *sval <= sval[1];
611 //printf("evexpr(): GE\n");
615 if ((*sattr & TDB) != (sattr[1] & TDB))
618 *sattr = ABS | DEFINED;
619 *sval = *sval >= sval[1];
622 //printf("evexpr(): >\n");
626 if ((*sattr & TDB) != (sattr[1] & TDB))
629 *sattr = ABS | DEFINED;
630 *sval = *sval > sval[1];
633 //printf("evexpr(): <\n");
637 if ((*sattr & TDB) != (sattr[1] & TDB))
640 *sattr = ABS | DEFINED;
641 *sval = *sval < sval[1];
644 //printf("evexpr(): NE\n");
648 if ((*sattr & TDB) != (sattr[1] & TDB))
651 *sattr = ABS | DEFINED;
652 *sval = *sval != sval[1];
655 //printf("evexpr(): =\n");
659 if ((*sattr & TDB) != (sattr[1] & TDB))
662 *sattr = ABS | DEFINED;
663 *sval = *sval == sval[1];
665 // All other binary operators must have two ABS items
666 // to work with. They all produce an ABS value.
668 //printf("evexpr(): default\n");
669 // GH - Removed for v1.0.15 as part of the fix for indexed loads.
670 //if ((*sattr & (TEXT|DATA|BSS)) || (*--sattr & (TEXT|DATA|BSS)))
672 *sattr = ABS | DEFINED; // Expr becomes absolute
678 sattr--; // Pop attrib
679 //printf("--> NxN: %i x %i = ", *sval, sval[1]);
681 //printf("%i\n", *sval);
685 sattr--; // Pop attrib
688 return error("divide by zero");
690 //printf("--> N/N: %i / %i = ", sval[0], sval[1]);
691 // Compiler is picky here: Without casting these, it discards
692 // the sign if dividing a negative # by a positive one,
693 // creating a bad result. :-/
694 // Probably a side effect of using VALUE intead of ints.
695 *sval = (int)sval[0] / (int)sval[1];
696 //printf("%i\n", *sval);
700 sattr--; // Pop attrib
703 return error("mod (%) by zero");
709 sattr--; // Pop attrib
714 sattr--; // Pop attrib
719 sattr--; // Pop attrib
724 sattr--; // Pop attrib
729 sattr--; // Pop attrib
733 interror(5); // Bad operator in expression stream
744 // sym_seg added in 1.0.16 to solve a problem with forward symbols in
745 // expressions where absolute values also existed. The absolutes were
746 // overiding the symbol segments and not being included :(
747 //*a_attr = *sattr | sym_seg; // Copy value + attrib
749 *a_attr = *sattr; // Copy value + attrib