1*0c8de5b0SBaptiste Daroussin /* $Id: symtab.c,v 1.11 2014/03/26 00:17:09 Tom.Shields Exp $ */ 298e903e7SBaptiste Daroussin 398e903e7SBaptiste Daroussin #include "defs.h" 498e903e7SBaptiste Daroussin 598e903e7SBaptiste Daroussin /* TABLE_SIZE is the number of entries in the symbol table. */ 698e903e7SBaptiste Daroussin /* TABLE_SIZE must be a power of two. */ 798e903e7SBaptiste Daroussin 898e903e7SBaptiste Daroussin #define TABLE_SIZE 1024 998e903e7SBaptiste Daroussin 1098e903e7SBaptiste Daroussin static bucket **symbol_table = 0; 1198e903e7SBaptiste Daroussin bucket *first_symbol; 1298e903e7SBaptiste Daroussin bucket *last_symbol; 1398e903e7SBaptiste Daroussin 1498e903e7SBaptiste Daroussin static int hash(const char * name)1598e903e7SBaptiste Daroussinhash(const char *name) 1698e903e7SBaptiste Daroussin { 1798e903e7SBaptiste Daroussin const char *s; 1898e903e7SBaptiste Daroussin int c, k; 1998e903e7SBaptiste Daroussin 2098e903e7SBaptiste Daroussin assert(name && *name); 2198e903e7SBaptiste Daroussin s = name; 2298e903e7SBaptiste Daroussin k = *s; 2398e903e7SBaptiste Daroussin while ((c = *++s) != 0) 2498e903e7SBaptiste Daroussin k = (31 * k + c) & (TABLE_SIZE - 1); 2598e903e7SBaptiste Daroussin 2698e903e7SBaptiste Daroussin return (k); 2798e903e7SBaptiste Daroussin } 2898e903e7SBaptiste Daroussin 2998e903e7SBaptiste Daroussin bucket * make_bucket(const char * name)3098e903e7SBaptiste Daroussinmake_bucket(const char *name) 3198e903e7SBaptiste Daroussin { 3298e903e7SBaptiste Daroussin bucket *bp; 3398e903e7SBaptiste Daroussin 3498e903e7SBaptiste Daroussin assert(name != 0); 3598e903e7SBaptiste Daroussin 363e066022SBaptiste Daroussin bp = TMALLOC(bucket, 1); 3798e903e7SBaptiste Daroussin NO_SPACE(bp); 3898e903e7SBaptiste Daroussin 3998e903e7SBaptiste Daroussin bp->link = 0; 4098e903e7SBaptiste Daroussin bp->next = 0; 4198e903e7SBaptiste Daroussin 423e066022SBaptiste Daroussin bp->name = TMALLOC(char, strlen(name) + 1); 4398e903e7SBaptiste Daroussin NO_SPACE(bp->name); 4498e903e7SBaptiste Daroussin 4598e903e7SBaptiste Daroussin bp->tag = 0; 4698e903e7SBaptiste Daroussin bp->value = UNDEFINED; 4798e903e7SBaptiste Daroussin bp->index = 0; 4898e903e7SBaptiste Daroussin bp->prec = 0; 4998e903e7SBaptiste Daroussin bp->class = UNKNOWN; 5098e903e7SBaptiste Daroussin bp->assoc = TOKEN; 51*0c8de5b0SBaptiste Daroussin #if defined(YYBTYACC) 52*0c8de5b0SBaptiste Daroussin bp->args = -1; 53*0c8de5b0SBaptiste Daroussin bp->argnames = 0; 54*0c8de5b0SBaptiste Daroussin bp->argtags = 0; 55*0c8de5b0SBaptiste Daroussin bp->destructor = 0; 56*0c8de5b0SBaptiste Daroussin #endif 5798e903e7SBaptiste Daroussin strcpy(bp->name, name); 5898e903e7SBaptiste Daroussin 5998e903e7SBaptiste Daroussin return (bp); 6098e903e7SBaptiste Daroussin } 6198e903e7SBaptiste Daroussin 6298e903e7SBaptiste Daroussin bucket * lookup(const char * name)6398e903e7SBaptiste Daroussinlookup(const char *name) 6498e903e7SBaptiste Daroussin { 6598e903e7SBaptiste Daroussin bucket *bp, **bpp; 6698e903e7SBaptiste Daroussin 6798e903e7SBaptiste Daroussin bpp = symbol_table + hash(name); 6898e903e7SBaptiste Daroussin bp = *bpp; 6998e903e7SBaptiste Daroussin 7098e903e7SBaptiste Daroussin while (bp) 7198e903e7SBaptiste Daroussin { 7298e903e7SBaptiste Daroussin if (strcmp(name, bp->name) == 0) 7398e903e7SBaptiste Daroussin return (bp); 7498e903e7SBaptiste Daroussin bpp = &bp->link; 7598e903e7SBaptiste Daroussin bp = *bpp; 7698e903e7SBaptiste Daroussin } 7798e903e7SBaptiste Daroussin 7898e903e7SBaptiste Daroussin *bpp = bp = make_bucket(name); 7998e903e7SBaptiste Daroussin last_symbol->next = bp; 8098e903e7SBaptiste Daroussin last_symbol = bp; 8198e903e7SBaptiste Daroussin 8298e903e7SBaptiste Daroussin return (bp); 8398e903e7SBaptiste Daroussin } 8498e903e7SBaptiste Daroussin 8598e903e7SBaptiste Daroussin void create_symbol_table(void)8698e903e7SBaptiste Daroussincreate_symbol_table(void) 8798e903e7SBaptiste Daroussin { 8898e903e7SBaptiste Daroussin int i; 8998e903e7SBaptiste Daroussin bucket *bp; 9098e903e7SBaptiste Daroussin 913e066022SBaptiste Daroussin symbol_table = TMALLOC(bucket *, TABLE_SIZE); 9298e903e7SBaptiste Daroussin NO_SPACE(symbol_table); 9398e903e7SBaptiste Daroussin 9498e903e7SBaptiste Daroussin for (i = 0; i < TABLE_SIZE; i++) 9598e903e7SBaptiste Daroussin symbol_table[i] = 0; 9698e903e7SBaptiste Daroussin 9798e903e7SBaptiste Daroussin bp = make_bucket("error"); 9898e903e7SBaptiste Daroussin bp->index = 1; 9998e903e7SBaptiste Daroussin bp->class = TERM; 10098e903e7SBaptiste Daroussin 10198e903e7SBaptiste Daroussin first_symbol = bp; 10298e903e7SBaptiste Daroussin last_symbol = bp; 10398e903e7SBaptiste Daroussin symbol_table[hash("error")] = bp; 10498e903e7SBaptiste Daroussin } 10598e903e7SBaptiste Daroussin 10698e903e7SBaptiste Daroussin void free_symbol_table(void)10798e903e7SBaptiste Daroussinfree_symbol_table(void) 10898e903e7SBaptiste Daroussin { 10998e903e7SBaptiste Daroussin FREE(symbol_table); 11098e903e7SBaptiste Daroussin symbol_table = 0; 11198e903e7SBaptiste Daroussin } 11298e903e7SBaptiste Daroussin 11398e903e7SBaptiste Daroussin void free_symbols(void)11498e903e7SBaptiste Daroussinfree_symbols(void) 11598e903e7SBaptiste Daroussin { 11698e903e7SBaptiste Daroussin bucket *p, *q; 11798e903e7SBaptiste Daroussin 11898e903e7SBaptiste Daroussin for (p = first_symbol; p; p = q) 11998e903e7SBaptiste Daroussin { 12098e903e7SBaptiste Daroussin q = p->next; 12198e903e7SBaptiste Daroussin FREE(p); 12298e903e7SBaptiste Daroussin } 12398e903e7SBaptiste Daroussin } 124