2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // EXPR.C - Expression Analyzer
4 // Copyright (C) 199x Landon Dyer, 2011 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)
38 '!', '~', UNMINUS, 0, // UNARY
39 '*', '/', '%', 0, // MULT
42 LE, GE, '<', '>', NE, '=', 0, // REL
49 const char missym_error[] = "missing symbol";
50 const char str_error[] = "missing symbol or string";
52 // Convert expression to postfix
53 static TOKEN * evalTokenBuffer; // Deposit tokens here (this is really a
54 // pointer to exprbuf from direct.c)
55 // (Can also be from others, like
57 static symbolNum; // Pointer to the entry in symbolPtr[]
61 // Obtain a string value
63 static VALUE str_value(char * p)
68 v = (v << 8) | (*p & 0xFF);
75 // Initialize expression analyzer
77 void InitExpression(void)
82 // Initialize token-class table (all set to END)
86 for(i=0, 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 '-')
134 class = tokenClass[*tok];
136 if (*tok == '-' || class == UNARY)
146 *evalTokenBuffer++ = t;
148 else if (class == SUNARY)
153 *evalTokenBuffer++ = CONST;
154 *evalTokenBuffer++ = dos_time();
157 *evalTokenBuffer++ = CONST;
158 *evalTokenBuffer++ = dos_date();
160 case CR_MACDEF: // ^^macdef <macro-name>
161 if (*tok++ != SYMBOL)
162 return error(missym_error);
165 w = (lookup(p, MACRO, 0) == NULL ? 0 : 1);
166 *evalTokenBuffer++ = CONST;
167 *evalTokenBuffer++ = (TOKEN)w;
175 if (*tok++ != SYMBOL)
176 return error(missym_error);
179 j = (*p == '.' ? curenv : 0);
180 w = ((sy = lookup(p, LABEL, j)) != NULL && (sy->sattr & w) ? 1 : 0);
181 *evalTokenBuffer++ = CONST;
182 *evalTokenBuffer++ = (TOKEN)w;
185 if (*tok != SYMBOL && *tok != STRING)
186 return error(str_error);
192 return error(comma_error);
194 if (*tok != SYMBOL && *tok != STRING)
195 return error(str_error);
200 w = (WORD)(!strcmp(p, p2));
201 *evalTokenBuffer++ = CONST;
202 *evalTokenBuffer++ = (TOKEN)w;
214 // Terminals (CONSTs) and parenthesis grouping
225 *evalTokenBuffer++ = CONST;
226 *evalTokenBuffer++ = *tok++;
230 j = (*p == '.' ? curenv : 0);
231 sy = lookup(p, LABEL, j);
234 sy = NewSymbol(p, LABEL, j);
236 // Check register bank usage
237 if (sy->sattre & EQUATEDREG)
239 if ((regbank == BANK_0) && (sy->sattre & BANK_1) && !altbankok)
240 warns("equated symbol \'%s\' cannot be used in register bank 0", sy->sname);
242 if ((regbank == BANK_1) && (sy->sattre & BANK_0) && !altbankok)
243 warns("equated symbol \'%s\' cannot be used in register bank 1", sy->sname);
246 *evalTokenBuffer++ = SYMBOL;
247 *evalTokenBuffer++ = symbolNum;
248 symbolPtr[symbolNum] = sy;
252 *evalTokenBuffer++ = CONST;
253 *evalTokenBuffer++ = str_value(string[*tok++]);
260 return error("missing close parenthesis ')'");
268 return error("missing close parenthesis ']'");
272 *evalTokenBuffer++ = ACONST; // Attributed const
273 *evalTokenBuffer++ = sloc; // Current location
274 *evalTokenBuffer++ = cursect | DEFINED; // Store attribs
277 *evalTokenBuffer++ = ACONST; // Attributed const
279 // pcloc == location at start of line
280 *evalTokenBuffer++ = (orgactive ? orgaddr : pcloc);
281 *evalTokenBuffer++ = ABS | DEFINED; // Store attribs
284 return error("bad expression");
292 // Recursive-descent expression analyzer (with some simple speed hacks)
294 int expr(TOKEN * otk, VALUE * a_value, WORD * a_attr, SYM ** a_esym)
296 // Passed in values (once derefenced, that is) can all be zero. They are
297 // there so that the expression analyzer can fill them in as needed. The
298 // expression analyzer gets its input from the global token pointer "tok",
299 // and not from anything passed in by the user.
304 evalTokenBuffer = otk; // Set token pointer to 'exprbuf' (direct.c)
305 // Also set in various other places too (riscasm.c, e.g.)
307 //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]]);
308 // Optimize for single constant or single symbol.
309 // Shamus: Subtle bug here. EOL token is 101; if you have a constant token
310 // followed by the value 101, it will trigger a bad evaluation here.
311 // This is probably a really bad assumption to be making here...!
312 // (assuming tok[1] == EOL is a single token that is)
313 // if ((tok[1] == EOL)
314 if ((tok[1] == EOL && tok[0] != CONST)
315 || (((*tok == CONST || *tok == SYMBOL) || (*tok >= KW_R0 && *tok <= KW_R31))
316 && (tokenClass[tok[2]] < UNARY)))
318 if (*tok >= KW_R0 && *tok <= KW_R31)
320 *evalTokenBuffer++ = CONST;
321 *evalTokenBuffer++ = *a_value = (*tok - KW_R0);
322 *a_attr = ABS | DEFINED;
329 else if (*tok == CONST)
331 *evalTokenBuffer++ = CONST;
332 *evalTokenBuffer++ = *a_value = tok[1];
333 *a_attr = ABS | DEFINED;
339 //printf("Quick eval in expr(): CONST = %i, tokenClass[tok[2]] = %i\n", *a_value, tokenClass[*tok]);
341 else if (*tok == '*')
343 *evalTokenBuffer++ = CONST;
346 *evalTokenBuffer++ = *a_value = orgaddr;
348 *evalTokenBuffer++ = *a_value = pcloc;
350 *a_attr = ABS | DEFINED;
357 else if (*tok == STRING || *tok == SYMBOL)
360 j = (*p == '.' ? curenv : 0);
361 symbol = lookup(p, LABEL, j);
363 printf("eval: Looking up symbol [=%08X]\n", symbol);
365 printf(" attr=%04X, attre=%08X, val=%i, name=%s\n", symbol->sattr, symbol->sattre, symbol->svalue, symbol->sname);
369 symbol = NewSymbol(p, LABEL, j);
371 symbol->sattr |= REFERENCED;
373 // Check for undefined register equates, but only if it's not part
374 // of a #<SYMBOL> construct, as it could be that the label that's
375 // been undefined may later be used as an address label--which
376 // means it will be fixed up later, and thus, not an error.
377 if ((symbol->sattre & UNDEF_EQUR) && !riscImmTokenSeen)
379 errors("undefined register equate '%s'", symbol->sname);
380 //if we return right away, it returns some spurious errors...
384 // Check register bank usage
385 if (symbol->sattre & EQUATEDREG)
387 if ((regbank == BANK_0) && (symbol->sattre & BANK_1) && !altbankok)
388 warns("equated symbol '%s' cannot be used in register bank 0", symbol->sname);
390 if ((regbank == BANK_1) && (symbol->sattre & BANK_0) && !altbankok)
391 warns("equated symbol '%s' cannot be used in register bank 1", symbol->sname);
394 *evalTokenBuffer++ = SYMBOL;
396 *evalTokenBuffer++ = (TOKEN)symbol;
399 While this approach works, it's wasteful. It would be better to use something
400 that's already available, like the symbol "order defined" table (which needs to
401 be converted from a linked list into an array).
403 *evalTokenBuffer++ = symbolNum;
404 symbolPtr[symbolNum] = symbol;
408 if (symbol->sattr & DEFINED)
409 *a_value = symbol->svalue;
414 All that extra crap that was put into the svalue when doing the equr stuff is
415 thrown away right here. What the hell is it for?
417 if (symbol->sattre & EQUATEDREG)
420 *a_attr = (WORD)(symbol->sattr & ~GLOBAL);
422 if ((symbol->sattr & (GLOBAL | DEFINED)) == GLOBAL && a_esym != NULL)
429 // Unknown type here... Alert the user!,
430 error("undefined RISC register in expression");
431 // Prevent spurious error reporting...
436 *evalTokenBuffer++ = ENDEXPR;
443 *evalTokenBuffer++ = ENDEXPR;
444 return evexpr(otk, a_value, a_attr, a_esym);
449 // Evaluate expression.
450 // If the expression involves only ONE external symbol, the expression is
451 // UNDEFINED, but it's value includes everything but the symbol value, and
452 // `a_esym' is set to the external symbol.
454 int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym)
463 sval = evstk; // (Empty) initial stack
465 esym = NULL; // No external symbol involved
468 while (*tk != ENDEXPR)
473 //printf("evexpr(): SYMBOL\n");
474 sy = symbolPtr[*tk++];
475 sy->sattr |= REFERENCED; // Set "referenced" bit
477 if (!(sy->sattr & DEFINED))
479 // Reference to undefined symbol
480 if (!(sy->sattr & GLOBAL))
487 if (esym != NULL) // Check for multiple externals
488 return error(seg_error);
493 if (sy->sattr & DEFINED)
495 *++sval = sy->svalue; // Push symbol's value
499 *++sval = 0; // 0 for undefined symbols
502 *++sattr = (WORD)(sy->sattr & ~GLOBAL); // Push attribs
503 sym_seg = (WORD)(sy->sattr & (TEXT | DATA | BSS));
506 //printf("evexpr(): CONST = %i\n", *tk);
507 *++sval = *tk++; // Push value
508 *++sattr = ABS | DEFINED; // Push simple attribs
511 //printf("evexpr(): ACONST = %i\n", *tk);
512 *++sval = *tk++; // Push value
513 *++sattr = (WORD)*tk++; // Push attribs
516 // Binary "+" and "-" matrix:
519 // ----------------------------
520 // ABS | ABS | Sect | Other |
521 // Sect | Sect | [1] | Error |
522 // Other | Other | Error | [1] |
523 // ----------------------------
528 //printf("evexpr(): +\n");
530 --sattr; // Pop attrib
531 *sval += sval[1]; // Compute value
533 if (!(*sattr & (TEXT | DATA | BSS)))
535 else if (sattr[1] & (TEXT | DATA | BSS))
536 return error(seg_error);
540 //printf("evexpr(): -\n");
542 --sattr; // Pop attrib
543 *sval -= sval[1]; // Compute value
545 attr = (WORD)(*sattr & (TEXT | DATA | BSS));
549 else if (sattr[1] & (TEXT | DATA | BSS))
551 if (!(attr & sattr[1]))
552 return error(seg_error);
554 *sattr &= ~(TEXT | DATA | BSS);
558 // Unary operators only work on ABS items
560 //printf("evexpr(): UNMINUS\n");
561 if (*sattr & (TEXT | DATA | BSS))
565 *sattr = ABS | DEFINED; // Expr becomes absolute
568 //printf("evexpr(): !\n");
569 if (*sattr & (TEXT | DATA | BSS))
573 *sattr = ABS | DEFINED; // Expr becomes absolute
576 //printf("evexpr(): ~\n");
577 if (*sattr & (TEXT | DATA | BSS))
581 *sattr = ABS | DEFINED; // Expr becomes absolute
583 // Comparison operators must have two values that
584 // are in the same segment, but that's the only requirement.
586 //printf("evexpr(): LE\n");
590 if ((*sattr & TDB) != (sattr[1] & TDB))
593 *sattr = ABS | DEFINED;
594 *sval = *sval <= sval[1];
597 //printf("evexpr(): GE\n");
601 if ((*sattr & TDB) != (sattr[1] & TDB))
604 *sattr = ABS | DEFINED;
605 *sval = *sval >= sval[1];
608 //printf("evexpr(): >\n");
612 if ((*sattr & TDB) != (sattr[1] & TDB))
615 *sattr = ABS | DEFINED;
616 *sval = *sval > sval[1];
619 //printf("evexpr(): <\n");
623 if ((*sattr & TDB) != (sattr[1] & TDB))
626 *sattr = ABS | DEFINED;
627 *sval = *sval < sval[1];
630 //printf("evexpr(): NE\n");
634 if ((*sattr & TDB) != (sattr[1] & TDB))
637 *sattr = ABS | DEFINED;
638 *sval = *sval != sval[1];
641 //printf("evexpr(): =\n");
645 if ((*sattr & TDB) != (sattr[1] & TDB))
648 *sattr = ABS | DEFINED;
649 *sval = *sval == sval[1];
651 // All other binary operators must have two ABS items
652 // to work with. They all produce an ABS value.
654 //printf("evexpr(): default\n");
655 // GH - Removed for v1.0.15 as part of the fix for indexed loads.
656 //if ((*sattr & (TEXT|DATA|BSS)) || (*--sattr & (TEXT|DATA|BSS)))
658 *sattr = ABS | DEFINED; // Expr becomes absolute
664 --sattr; // Pop attrib
669 --sattr; // Pop attrib
672 return error("divide by zero");
678 --sattr; // Pop attrib
681 return error("mod (%) by zero");
687 --sattr; // Pop attrib
692 --sattr; // Pop attrib
697 --sattr; // Pop attrib
702 --sattr; // Pop attrib
707 --sattr; // Pop attrib
711 interror(5); // Bad operator in expression stream
722 // sym_seg added in 1.0.16 to solve a problem with forward symbols in
723 // expressions where absolute values also existed. The absolutes were
724 // overiding the symbol segments and not being included :(
725 //*a_attr = *sattr | sym_seg; // Copy value + attrib
727 *a_attr = *sattr; // Copy value + attrib