💾 Archived View for uscoffings.net › retro-computing › systems › TI994a › assemblers › tiasm › src ›… captured on 2022-06-04 at 01:13:49.
-=-=-=-=-=-=-
#include "TIasm.h" #include <stdio.h> extern int Debug; struct stab *Symtab[SYMVECSIZ]; /* initab * Init the hash table. * * Returns: * Nothing. */ void initab() { register struct stab **tptr; register int x; tptr = Symtab; for (x = 0; x < SYMVECSIZ; ++x) *tptr++ = NULL; } /* hash * return a hash key based on the symbol. */ int hash (symbol) register char *symbol; { register int ac = 0; /* Accumulator */ register int x; for (x = 0; x < IDENTSIZ && *symbol != NULL; ++x) { DEBUG (200, "hash: adding %d to ", (int )*symbol); DEBUG (200, "accumulator which is %d\n", ac); ac += (int )*symbol++; } DEBUG (200, "hash: returning %d\n", ac % SYMVECSIZ); return (ac % SYMVECSIZ); } /* lookup * Look up a symbol in the symbol table. * * Returns: * pointer to entry if succesful. * NULL if not found. */ struct stab *lookup(symbol) char *symbol; { struct stab *entry; DEBUG (100, "lookup: symbol is \"%s\"\n", symbol); DEBUG (100, "lookup: hashcode = %d\n", hash(symbol)); if ((entry = Symtab[hash(symbol)]) == NULL) return (NULL); /* The vector was empty */ do if (strcmp(symbol, entry->st_symnm) == 0) return (entry); while ((entry = entry->st_next) != NULL); return (NULL); /* Doesn't exist */ } /* insert * Insert a symbol table entry into the symbol table. * * Returns: * The entry inserted. * * NOTE: The return here is fairly redundant as this CANNOT fail. But, * should a quadratic hashing scheme ever be implemented the fail here is * possible. So, for the sake of modularity, I include the redundant return. * A fail would be signified by a NULL return. */ struct stab *insert(entry) struct stab *entry; { struct stab *list, *eolist; int key; DEBUG (100, "insert: Inserting \"%s\" to vector", entry->st_symnm); DEBUG (100, " %d\n", hash(entry->st_symnm)); if ((list = Symtab[(key = hash(entry->st_symnm))]) == NULL) { /* vector was empty */ Symtab[key] = entry; return (entry); } /* Find the end of the list */ do { eolist = list; list = list->st_next; } while (list != NULL); /* Do the actual insert */ eolist->st_next = entry; return (entry); } /* newent * This routine creates and initializes to NULL's a symbol table entry. * * Returns: * NULL on error (malloc failed) * pointer to an initialized entry on success */ struct stab *newent() { extern char *calloc(); return ((struct stab *)calloc(1, sizeof(struct stab))); } /* The following routines and global variables are for use when dumping * the symbol table. */ static int vec; /* Which vector we are currently on */ static struct stab *stent; /* Which entry we are on */ /* sstab * * Set up to dump the table. Zero the globals. * * RETURNS: * nothing */ void sstab () { vec = 0; while (vec < SYMVECSIZ && Symtab[vec] == NULL) ++vec; stent = Symtab[vec]; } /* nxtent * * Return the next entry in the table. Does not return reserved words or * directives. * * RETURNS: * A symbol table entry on success * NULL if the table is empty */ struct stab *nxtent () { struct stab *tmp; while (vec < SYMVECSIZ) { while (stent != NULL) { if (!(stent->st_type & TYRSRVD || stent->st_type & TYDIR)) { tmp = stent; stent = stent->st_next; return (tmp); } stent = stent->st_next; } ++vec; stent = Symtab[vec]; } /* Oops, all done */ return (NULL); }