X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=rmac;a=blobdiff_plain;f=expr.c;h=deeea40a893300bba19bf3b587b18b78ad47a072;hp=7cedc41350529f3df19e1ccc61752bc75a824d6d;hb=a48737de123e304866212f5382d6fa4174d496a0;hpb=ab99ead7ff1f6969b1c26876757c14dd1da40648 diff --git a/expr.c b/expr.c index 7cedc41..deeea40 100644 --- a/expr.c +++ b/expr.c @@ -17,45 +17,48 @@ #include "symbol.h" #include "token.h" -#define DEF_KW // Declare keyword values -#include "kwtab.h" // Incl generated keyword tables & defs +#define DEF_KW // Declare keyword values +#include "kwtab.h" // Incl generated keyword tables & defs -static char tokenClass[128]; // Generated table of token classes -static VALUE evstk[EVSTACKSIZE]; // Evaluator value stack -static WORD evattr[EVSTACKSIZE]; // Evaluator attribute stack +// N.B.: The size of tokenClass should be identical to the largest value of +// a token; we're assuming 256 but not 100% sure! +static char tokenClass[256]; // Generated table of token classes +static VALUE evstk[EVSTACKSIZE]; // Evaluator value stack +static WORD evattr[EVSTACKSIZE]; // Evaluator attribute stack // Token-class initialization list char itokcl[] = { - 0, // END - CONST, SYMBOL, 0, // ID - '(', '[', '{', 0, // OPAR - ')', ']', '}', 0, // CPAR - CR_DEFINED, CR_REFERENCED, // SUNARY (special unary) + 0, // END + CONST, SYMBOL, 0, // ID + '(', '[', '{', 0, // OPAR + ')', ']', '}', 0, // CPAR + CR_DEFINED, CR_REFERENCED, // SUNARY (special unary) CR_STREQ, CR_MACDEF, CR_DATE, CR_TIME, 0, - '!', '~', UNMINUS, 0, // UNARY - '*', '/', '%', 0, // MULT - '+', '-', 0, // ADD - SHL, SHR, 0, // SHIFT - LE, GE, '<', '>', NE, '=', 0, // REL - '&', 0, // AND - '^', 0, // XOR - '|', 0, // OR - 1 // (the end) + '!', '~', UNMINUS, 0, // UNARY + '*', '/', '%', 0, // MULT + '+', '-', 0, // ADD + SHL, SHR, 0, // SHIFT + LE, GE, '<', '>', NE, '=', 0, // REL + '&', 0, // AND + '^', 0, // XOR + '|', 0, // OR + 1 // (the end) }; const char missym_error[] = "missing symbol"; const char str_error[] = "missing symbol or string"; // Convert expression to postfix -static TOKEN * evalTokenBuffer; // Deposit tokens here (this is really a - // pointer to exprbuf from direct.c) - // (Can also be from others, like riscasm.c) -static symbolNum; // Pointer to the entry in symbolPtr[] +static TOKEN * evalTokenBuffer; // Deposit tokens here (this is really a + // pointer to exprbuf from direct.c) + // (Can also be from others, like + // riscasm.c) +static symbolNum; // Pointer to the entry in symbolPtr[] // -// Obtain a String Value +// Obtain a string value // static VALUE str_value(char * p) { @@ -69,15 +72,15 @@ static VALUE str_value(char * p) // -// Initialize Expression Analyzer +// Initialize expression analyzer // void InitExpression(void) { - int i; // Iterator - char * p; // Token pointer + int i; + char * p; // Initialize token-class table (all set to END) - for(i=0; i<128; i++) + for(i=0; i<256; i++) tokenClass[i] = END; for(i=0, p=itokcl; *p!=1; p++) @@ -241,13 +244,9 @@ int expr2(void) } *evalTokenBuffer++ = SYMBOL; -#if 0 - *evalTokenBuffer++ = (TOKEN)sy; -#else *evalTokenBuffer++ = symbolNum; symbolPtr[symbolNum] = sy; symbolNum++; -#endif break; case STRING: *evalTokenBuffer++ = CONST; @@ -277,11 +276,8 @@ int expr2(void) case '*': *evalTokenBuffer++ = ACONST; // Attributed const - if (orgactive) - *evalTokenBuffer++ = orgaddr; - else - *evalTokenBuffer++ = pcloc; // Location at start of line - + // pcloc == location at start of line + *evalTokenBuffer++ = (orgactive ? orgaddr : pcloc); *evalTokenBuffer++ = ABS | DEFINED; // Store attribs break; default: @@ -308,8 +304,14 @@ int expr(TOKEN * otk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) evalTokenBuffer = otk; // Set token pointer to 'exprbuf' (direct.c) // Also set in various other places too (riscasm.c, e.g.) +//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]]); // Optimize for single constant or single symbol. - if ((tok[1] == EOL) + // Shamus: Subtle bug here. EOL token is 101; if you have a constant token + // followed by the value 101, it will trigger a bad evaluation here. + // This is probably a really bad assumption to be making here...! + // (assuming tok[1] == EOL is a single token that is) +// if ((tok[1] == EOL) + if ((tok[1] == EOL && tok[0] != CONST) || (((*tok == CONST || *tok == SYMBOL) || (*tok >= KW_R0 && *tok <= KW_R31)) && (tokenClass[tok[2]] < UNARY))) { @@ -323,8 +325,6 @@ int expr(TOKEN * otk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *a_esym = NULL; tok++; - *evalTokenBuffer++ = ENDEXPR; - return OK; } else if (*tok == CONST) { @@ -334,6 +334,9 @@ int expr(TOKEN * otk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) if (a_esym != NULL) *a_esym = NULL; + + tok += 2; +//printf("Quick eval in expr(): CONST = %i, tokenClass[tok[2]] = %i\n", *a_value, tokenClass[*tok]); } else if (*tok == '*') { @@ -349,21 +352,29 @@ int expr(TOKEN * otk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) if (a_esym != NULL) *a_esym = NULL; - tok--; + tok++; } else if (*tok == STRING || *tok == SYMBOL) { p = string[tok[1]]; j = (*p == '.' ? curenv : 0); symbol = lookup(p, LABEL, j); +#if 0 +printf("eval: Looking up symbol (%s) [=%08X]\n", p, symbol); +if (symbol) + printf(" attr=%04X, attre=%08X, val=%i, name=%s\n", symbol->sattr, symbol->sattre, symbol->svalue, symbol->sname); +#endif if (symbol == NULL) symbol = NewSymbol(p, LABEL, j); symbol->sattr |= REFERENCED; - // Check for undefined register equates - if (symbol->sattre & UNDEF_EQUR) + // Check for undefined register equates, but only if it's not part + // of a # construct, as it could be that the label that's + // been undefined may later be used as an address label--which + // means it will be fixed up later, and thus, not an error. + if ((symbol->sattre & UNDEF_EQUR) && !riscImmTokenSeen) { errors("undefined register equate '%s'", symbol->sname); //if we return right away, it returns some spurious errors... @@ -405,21 +416,22 @@ thrown away right here. What the hell is it for? */ if (symbol->sattre & EQUATEDREG) *a_value &= 0x1F; - *a_attr = (WORD)(symbol->sattr & ~GLOBAL); if ((symbol->sattr & (GLOBAL | DEFINED)) == GLOBAL && a_esym != NULL) *a_esym = symbol; + + tok += 2; } else { - // Unknown type here... Alert the user! + // Unknown type here... Alert the user!, error("undefined RISC register in expression"); + // Prevent spurious error reporting... tok++; return ERROR; } - tok += 2; *evalTokenBuffer++ = ENDEXPR; return OK; } @@ -457,7 +469,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) switch ((int)*tk++) { case SYMBOL: -// sy = (SYM *)*tk++; +//printf("evexpr(): SYMBOL\n"); sy = symbolPtr[*tk++]; sy->sattr |= REFERENCED; // Set "referenced" bit @@ -490,10 +502,12 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) sym_seg = (WORD)(sy->sattr & (TEXT | DATA | BSS)); break; case CONST: +//printf("evexpr(): CONST = %i\n", *tk); *++sval = *tk++; // Push value *++sattr = ABS | DEFINED; // Push simple attribs break; case ACONST: +//printf("evexpr(): ACONST = %i\n", *tk); *++sval = *tk++; // Push value *++sattr = (WORD)*tk++; // Push attribs break; @@ -510,6 +524,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) // [1] + : Error // - : ABS case '+': +//printf("evexpr(): +\n"); --sval; // Pop value --sattr; // Pop attrib *sval += sval[1]; // Compute value @@ -521,6 +536,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) break; case '-': +//printf("evexpr(): -\n"); --sval; // Pop value --sattr; // Pop attrib *sval -= sval[1]; // Compute value @@ -540,6 +556,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) break; // Unary operators only work on ABS items case UNMINUS: +//printf("evexpr(): UNMINUS\n"); if (*sattr & (TEXT | DATA | BSS)) error(seg_error); @@ -547,6 +564,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sattr = ABS | DEFINED; // Expr becomes absolute break; case '!': +//printf("evexpr(): !\n"); if (*sattr & (TEXT | DATA | BSS)) error(seg_error); @@ -554,6 +572,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sattr = ABS | DEFINED; // Expr becomes absolute break; case '~': +//printf("evexpr(): ~\n"); if (*sattr & (TEXT | DATA | BSS)) error(seg_error); @@ -563,6 +582,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) // Comparison operators must have two values that // are in the same segment, but that's the only requirement. case LE: +//printf("evexpr(): LE\n"); --sattr; --sval; @@ -573,6 +593,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sval = *sval <= sval[1]; break; case GE: +//printf("evexpr(): GE\n"); --sattr; --sval; @@ -583,6 +604,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sval = *sval >= sval[1]; break; case '>': +//printf("evexpr(): >\n"); --sattr; --sval; @@ -593,6 +615,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sval = *sval > sval[1]; break; case '<': +//printf("evexpr(): <\n"); --sattr; --sval; @@ -603,6 +626,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sval = *sval < sval[1]; break; case NE: +//printf("evexpr(): NE\n"); --sattr; --sval; @@ -613,6 +637,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) *sval = *sval != sval[1]; break; case '=': +//printf("evexpr(): =\n"); --sattr; --sval; @@ -625,6 +650,7 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) // All other binary operators must have two ABS items // to work with. They all produce an ABS value. default: +//printf("evexpr(): default\n"); // GH - Removed for v1.0.15 as part of the fix for indexed loads. //if ((*sattr & (TEXT|DATA|BSS)) || (*--sattr & (TEXT|DATA|BSS))) //error(seg_error); @@ -702,3 +728,4 @@ int evexpr(TOKEN * tk, VALUE * a_value, WORD * a_attr, SYM ** a_esym) return OK; } +