X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?p=rmac;a=blobdiff_plain;f=symbol.c;h=f8d8424ff99de0886df08bd03cd459b85cf94d68;hp=25829f41f7bbbdf5fc296a45fd28489f3afdcf49;hb=75969398d9b8a9f82ea76fc4e4cbfb97b11160a4;hpb=3385b366632d03745033fa6b19faabf60219bc6b diff --git a/symbol.c b/symbol.c index 25829f4..f8d8424 100644 --- a/symbol.c +++ b/symbol.c @@ -11,12 +11,17 @@ #include "procln.h" #include "error.h" -static SYM * sytab[NBUCKETS]; // User symbol-table header -int curenv; // Current enviroment number -SYM * sorder; // * -> Symbols, in order of reference -SYM * sordtail; // * -> Last symbol in sorder list -SYM * sdecl; // * -> Symbols, in order of declaration -SYM * sdecltail; // * -> Last symbol in sdecl list + +// Macros +#define NBUCKETS 256 // Number of hash buckets (power of 2) + +static SYM * symbolTable[NBUCKETS]; // User symbol-table header +int curenv; // Current enviroment number +static SYM * sorder; // * -> Symbols, in order of reference +static SYM * sordtail; // * -> Last symbol in sorder list +static SYM * sdecl; // * -> Symbols, in order of declaration +static SYM * sdecltail; // * -> Last symbol in sdecl list +static uint32_t currentUID; // Symbol UID tracking (done by NewSymbol()) // Tags for marking symbol spaces // a = absolute @@ -32,52 +37,30 @@ static char tdb_text[8] = { // // Initialize Symbol Table // -void init_sym(void) +void InitSymbolTable(void) { - int i; // Iterator + int i; // Iterator - for(i=0; isname = nstring(name); - // Fill-in the symbol - sy->stype = (BYTE)type; - sy->senv = (WORD)envno; - sy->sattr = 0; - - if (rgpu || rdsp) - sy->sattre = RISCSYM; - else - sy->sattre = 0; - - sy->svalue = 0; + symbol->sname = strdup(name); + symbol->stype = (BYTE)type; + symbol->senv = (WORD)envno; + symbol->sattr = 0; + symbol->sattre = (rgpu || rdsp ? RISCSYM : 0); + symbol->svalue = 0; + symbol->sorder = NULL; + symbol->uid = currentUID++; // Install symbol in symbol table - hash = syhash(name, envno); - sy->snext = sytab[hash]; - sytab[hash] = sy; + int hash = HashSymbol(name, envno); + symbol->snext = symbolTable[hash]; + symbolTable[hash] = symbol; // Append symbol to symbol-order list if (sorder == NULL) - sorder = sy; // Add first symbol + sorder = symbol; // Add first symbol else - sordtail->sorder = sy; // Or append to tail of list + sordtail->sorder = symbol; // Or append to tail of list - sy->sorder = NULL; - sordtail = sy; - - return sy; // Return pointer to symbol + sordtail = symbol; + return symbol; } // -// Lookup the symbol `name', of the specified type, with the specified -// enviroment level +// Look up the symbol name by its UID and return the pointer to the name. +// If it's not found, return NULL. // -SYM * lookup(char * name, int type, int envno) +char * GetSymbolNameByUID(uint32_t uid) { - SYM * sy; // Symbol record pointer - int k, sum; // Hash bucket calculation - char * s; // String pointer + //problem is with string lookup, that's why we're writing this + //so once this is written, we can put the uid in the token stream - // Pick a hash-bucket (SAME algorithm as syhash()) - k = 0; - s = name; + // A much better approach to the symbol order list would be to make an + // array--that way you can do away with the UIDs and all the rest, and + // simply do an array lookup based on position. But meh, let's do this for + // now until we can rewrite things so they make sense. + SYM * symbol = sorder; - for(sum=envno; *s;) + for(; symbol; symbol=symbol->sorder) { - if (k++ == 1) - sum += *s++ << 2; - else - sum += *s++; + if (symbol->uid == uid) + return symbol->sname; } - sy = sytab[sum & (NBUCKETS-1)]; + return NULL; +} + + +// +// Lookup the symbol `name', of the specified type, with the specified +// enviroment level +// +SYM * lookup(char * name, int type, int envno) +{ + SYM * symbol = symbolTable[HashSymbol(name, envno)]; // Do linear-search for symbol in bucket - while (sy != NULL) + while (symbol != NULL) { - if (sy->stype == type && // Type, envno and name must match - sy->senv == envno && - *name == *sy->sname && // Fast check for first character - !strcmp(name, sy->sname)) + if (symbol->stype == type // Type, envno and name must match + && symbol->senv == envno + && *name == *symbol->sname // Fast check for first character + && !strcmp(name, symbol->sname)) // More expensive check break; - else - sy = sy->snext; + + symbol = symbol->snext; } - return sy; // Return NULL or matching symbol + // Return NULL or matching symbol + return symbol; } // // Put symbol on "order-of-declaration" list of symbols // -void sym_decl(SYM * sym) +void sym_decl(SYM * symbol) { - if (sym->sattr & SDECLLIST) + if (symbol->sattr & SDECLLIST) return; // Already on list - sym->sattr |= SDECLLIST; // Mark "already on list" + symbol->sattr |= SDECLLIST; // Mark "already on list" if (sdecl == NULL) - sdecl = sym; // First on decl-list + sdecl = symbol; // First on decl-list else - sdecltail->sdecl = sym; // Add to end of list + sdecltail->sdecl = symbol; // Add to end of list - sym->sdecl = NULL; // Fix up list's tail - sdecltail = sym; + symbol->sdecl = NULL; // Fix up list's tail + sdecltail = symbol; } @@ -209,10 +193,10 @@ int syg_fix(void) // Scan through all symbols; // If a symbol is REFERENCED but not DEFINED, then make it global. - for(sy = sorder; sy != NULL; sy = sy->sorder) + for(sy=sorder; sy!=NULL; sy=sy->sorder) { if (sy->stype == LABEL && sy->senv == 0 - && ((sy->sattr & (REFERENCED|DEFINED)) == REFERENCED)) + && ((sy->sattr & (REFERENCED | DEFINED)) == REFERENCED)) sy->sattr |= GLOBAL; } @@ -225,7 +209,7 @@ int syg_fix(void) // int uc_string(char * s) { - for(; *s; ++s) + for(; *s; s++) { if (*s >= 'a' && *s <= 'z') *s -= 32; @@ -240,7 +224,7 @@ int uc_string(char * s) // number is put in `.senv'. Return the number of symbols that will be in the // symbol table. // -int sy_assign(char * buf, char *(*constr)()) +int sy_assign(char * buf, char *(* constr)()) { SYM * sy; int scount; @@ -282,8 +266,8 @@ int sy_assign(char * buf, char *(*constr)()) // Export or import external references, and export COMMON blocks. if ((sy->stype == LABEL) - && ((sy->sattr & (GLOBAL|DEFINED)) == (GLOBAL|DEFINED) - || (sy->sattr & (GLOBAL|REFERENCED)) == (GLOBAL|REFERENCED)) + && ((sy->sattr & (GLOBAL | DEFINED)) == (GLOBAL | DEFINED) + || (sy->sattr & (GLOBAL | REFERENCED)) == (GLOBAL | REFERENCED)) || (sy->sattr & COMMON)) { sy->senv = (WORD)scount++; @@ -294,7 +278,7 @@ int sy_assign(char * buf, char *(*constr)()) // Export vanilla labels (but don't make them global). An exception is // made for equates, which are not exported unless they are referenced. else if (sy->stype == LABEL && lsym_flag - && (sy->sattr & (DEFINED|REFERENCED)) != 0 + && (sy->sattr & (DEFINED | REFERENCED)) != 0 && (!as68_flag || *sy->sname != 'L')) { sy->senv = (WORD)scount++; @@ -332,21 +316,22 @@ int symtable(void) // Allocate storage for list headers and partition all labels. // Throw away macros and macro arguments. - sy = (SYM **)amem((LONG)(128 * sizeof(LONG))); +// sy = (SYM **)amem((LONG)(128 * sizeof(LONG))); + sy = (SYM **)malloc(128 * sizeof(LONG)); for(i=0; i<128; ++i) sy[i] = NULL; for(i=0; isnext; j = *p->sname; r = NULL; if (p->stype != LABEL) - continue; // Ignore non-labels + continue; // Ignore non-labels if (p->sattre & UNDEF_EQUR) continue; @@ -360,12 +345,12 @@ int symtable(void) } if (r == NULL) - { // Insert at front of list + { // Insert at front of list p->snext = sy[j]; sy[j] = p; } else - { // Insert in middle or append to list + { // Insert in middle or append to list p->snext = r->snext; r->snext = p; } @@ -446,7 +431,7 @@ int symtable(void) strcpy(ln2, "external"); else { - sprintf(ln2, "%08ux", q->svalue); + sprintf(ln2, "%08X", q->svalue); uc_string(ln2); }