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)
469 VALUE * sval = evstk; // (Empty) initial stack
470 WORD * sattr = evattr;
471 SYM * esym = NULL; // No external symbol involved
474 while (*tk != ENDEXPR)
479 //printf("evexpr(): SYMBOL\n");
480 sy = symbolPtr[*tk++];
481 sy->sattr |= REFERENCED; // Set "referenced" bit
483 if (!(sy->sattr & DEFINED))
485 // Reference to undefined symbol
486 if (!(sy->sattr & GLOBAL))
493 if (esym != NULL) // Check for multiple externals
494 return error(seg_error);
499 if (sy->sattr & DEFINED)
501 *++sval = sy->svalue; // Push symbol's value
505 *++sval = 0; // 0 for undefined symbols
508 *++sattr = (WORD)(sy->sattr & ~GLOBAL); // Push attribs
509 sym_seg = (WORD)(sy->sattr & TDB);
512 //printf("evexpr(): CONST = %i\n", *tk);
513 *++sval = *tk++; // Push value
514 *++sattr = ABS | DEFINED; // Push simple attribs
517 //printf("evexpr(): ACONST = %i\n", *tk);
518 *++sval = *tk++; // Push value
519 *++sattr = (WORD)*tk++; // Push attribs
522 // Binary "+" and "-" matrix:
525 // ----------------------------
526 // ABS | ABS | Sect | Other |
527 // Sect | Sect | [1] | Error |
528 // Other | Other | Error | [1] |
529 // ----------------------------
534 //printf("evexpr(): +\n");
536 --sattr; // Pop attrib
537 //printf("--> N+N: %i + %i = ", *sval, sval[1]);
538 *sval += sval[1]; // Compute value
539 //printf("%i\n", *sval);
543 else if (sattr[1] & TDB)
544 return error(seg_error);
548 //printf("evexpr(): -\n");
550 --sattr; // Pop attrib
551 //printf("--> N-N: %i - %i = ", *sval, sval[1]);
552 *sval -= sval[1]; // Compute value
553 //printf("%i\n", *sval);
555 attr = (WORD)(*sattr & TDB);
557 printf("EVEXPR (-): sym1 = %X, sym2 = %X\n", attr, sattr[1]);
559 // If symbol1 is ABS, take attributes from symbol2
562 // Otherwise, they're both TDB and so attributes cancel out
563 else if (sattr[1] & TDB)
567 // Unary operators only work on ABS items
569 //printf("evexpr(): UNMINUS\n");
574 *sattr = ABS | DEFINED; // Expr becomes absolute
577 //printf("evexpr(): !\n");
582 *sattr = ABS | DEFINED; // Expr becomes absolute
585 //printf("evexpr(): ~\n");
590 *sattr = ABS | DEFINED; // Expr becomes absolute
592 // Comparison operators must have two values that
593 // are in the same segment, but that's the only requirement.
595 //printf("evexpr(): LE\n");
599 if ((*sattr & TDB) != (sattr[1] & TDB))
602 *sattr = ABS | DEFINED;
603 *sval = *sval <= sval[1];
606 //printf("evexpr(): GE\n");
610 if ((*sattr & TDB) != (sattr[1] & TDB))
613 *sattr = ABS | DEFINED;
614 *sval = *sval >= sval[1];
617 //printf("evexpr(): >\n");
621 if ((*sattr & TDB) != (sattr[1] & TDB))
624 *sattr = ABS | DEFINED;
625 *sval = *sval > sval[1];
628 //printf("evexpr(): <\n");
632 if ((*sattr & TDB) != (sattr[1] & TDB))
635 *sattr = ABS | DEFINED;
636 *sval = *sval < sval[1];
639 //printf("evexpr(): NE\n");
643 if ((*sattr & TDB) != (sattr[1] & TDB))
646 *sattr = ABS | DEFINED;
647 *sval = *sval != sval[1];
650 //printf("evexpr(): =\n");
654 if ((*sattr & TDB) != (sattr[1] & TDB))
657 *sattr = ABS | DEFINED;
658 *sval = *sval == sval[1];
660 // All other binary operators must have two ABS items
661 // to work with. They all produce an ABS value.
663 //printf("evexpr(): default\n");
664 // GH - Removed for v1.0.15 as part of the fix for indexed loads.
665 //if ((*sattr & (TEXT|DATA|BSS)) || (*--sattr & (TEXT|DATA|BSS)))
667 *sattr = ABS | DEFINED; // Expr becomes absolute
673 sattr--; // Pop attrib
674 //printf("--> NxN: %i x %i = ", *sval, sval[1]);
676 //printf("%i\n", *sval);
680 sattr--; // Pop attrib
683 return error("divide by zero");
685 //printf("--> N/N: %i / %i = ", sval[0], sval[1]);
686 // Compiler is picky here: Without casting these, it discards
687 // the sign if dividing a negative # by a positive one,
688 // creating a bad result. :-/
689 // Probably a side effect of using VALUE intead of ints.
690 *sval = (int)sval[0] / (int)sval[1];
691 //printf("%i\n", *sval);
695 sattr--; // Pop attrib
698 return error("mod (%) by zero");
704 sattr--; // Pop attrib
709 sattr--; // Pop attrib
714 sattr--; // Pop attrib
719 sattr--; // Pop attrib
724 sattr--; // Pop attrib
728 interror(5); // Bad operator in expression stream
739 // sym_seg added in 1.0.16 to solve a problem with forward symbols in
740 // expressions where absolute values also existed. The absolutes were
741 // overiding the symbol segments and not being included :(
742 //*a_attr = *sattr | sym_seg; // Copy value + attrib
744 *a_attr = *sattr; // Copy value + attrib