1 /* $OpenBSD: symtab.c,v 1.12 2005/06/10 16:40:45 pvalchev Exp $ */ 2 /* $NetBSD: symtab.c,v 1.4 1996/03/19 03:21:48 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1989 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Robert Paul Corbett. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)symtab.c 5.3 (Berkeley) 6/1/90"; 39 #else 40 static char rcsid[] = "$OpenBSD: symtab.c,v 1.12 2005/06/10 16:40:45 pvalchev Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include "defs.h" 45 46 /* TABLE_SIZE is the number of entries in the symbol table. */ 47 /* TABLE_SIZE must be a power of two. */ 48 49 #define TABLE_SIZE 1024 50 51 52 bucket **symbol_table; 53 bucket *first_symbol; 54 bucket *last_symbol; 55 56 int hash(char *); 57 58 59 int 60 hash(char *name) 61 { 62 char *s; 63 int c, k; 64 65 assert(name && *name); 66 s = name; 67 k = *s; 68 while ((c = *++s)) 69 k = (31*k + c) & (TABLE_SIZE - 1); 70 71 return (k); 72 } 73 74 75 bucket * 76 make_bucket(char *name) 77 { 78 bucket *bp; 79 80 assert(name); 81 bp = (bucket *) MALLOC(sizeof(bucket)); 82 if (bp == 0) no_space(); 83 bp->link = 0; 84 bp->next = 0; 85 bp->name = strdup(name); 86 if (bp->name == 0) no_space(); 87 bp->tag = 0; 88 bp->value = UNDEFINED; 89 bp->index = 0; 90 bp->prec = 0; 91 bp-> class = UNKNOWN; 92 bp->assoc = TOKEN; 93 94 return (bp); 95 } 96 97 98 bucket * 99 lookup(char *name) 100 { 101 bucket *bp, **bpp; 102 103 bpp = symbol_table + hash(name); 104 bp = *bpp; 105 106 while (bp) 107 { 108 if (strcmp(name, bp->name) == 0) return (bp); 109 bpp = &bp->link; 110 bp = *bpp; 111 } 112 113 *bpp = bp = make_bucket(name); 114 last_symbol->next = bp; 115 last_symbol = bp; 116 117 return (bp); 118 } 119 120 121 void 122 create_symbol_table(void) 123 { 124 int i; 125 bucket *bp; 126 127 symbol_table = (bucket **) MALLOC(TABLE_SIZE*sizeof(bucket *)); 128 if (symbol_table == 0) no_space(); 129 for (i = 0; i < TABLE_SIZE; i++) 130 symbol_table[i] = 0; 131 132 bp = make_bucket("error"); 133 bp->index = 1; 134 bp->class = TERM; 135 136 first_symbol = bp; 137 last_symbol = bp; 138 symbol_table[hash("error")] = bp; 139 } 140 141 142 void 143 free_symbol_table(void) 144 { 145 FREE(symbol_table); 146 symbol_table = 0; 147 } 148 149 150 void 151 free_symbols(void) 152 { 153 bucket *p, *q; 154 155 for (p = first_symbol; p; p = q) 156 { 157 q = p->next; 158 FREE(p); 159 } 160 } 161