xref: /minix3/external/bsd/byacc/dist/symtab.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: symtab.c,v 1.8 2015/01/03 23:22:52 christos Exp $	*/
284d9c625SLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /* Id: symtab.c,v 1.11 2014/03/26 00:17:09 Tom.Shields Exp  */
44a17663cSThomas Veerman 
54a17663cSThomas Veerman #include "defs.h"
64a17663cSThomas Veerman 
74a17663cSThomas Veerman #include <sys/cdefs.h>
8*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: symtab.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
94a17663cSThomas Veerman 
104a17663cSThomas Veerman /* TABLE_SIZE is the number of entries in the symbol table. */
114a17663cSThomas Veerman /* TABLE_SIZE must be a power of two.			    */
124a17663cSThomas Veerman 
134a17663cSThomas Veerman #define	TABLE_SIZE 1024
144a17663cSThomas Veerman 
154a17663cSThomas Veerman static bucket **symbol_table = 0;
164a17663cSThomas Veerman bucket *first_symbol;
174a17663cSThomas Veerman bucket *last_symbol;
184a17663cSThomas Veerman 
194a17663cSThomas Veerman static int
hash(const char * name)204a17663cSThomas Veerman hash(const char *name)
214a17663cSThomas Veerman {
224a17663cSThomas Veerman     const char *s;
234a17663cSThomas Veerman     int c, k;
244a17663cSThomas Veerman 
254a17663cSThomas Veerman     assert(name && *name);
264a17663cSThomas Veerman     s = name;
274a17663cSThomas Veerman     k = *s;
284a17663cSThomas Veerman     while ((c = *++s) != 0)
294a17663cSThomas Veerman 	k = (31 * k + c) & (TABLE_SIZE - 1);
304a17663cSThomas Veerman 
314a17663cSThomas Veerman     return (k);
324a17663cSThomas Veerman }
334a17663cSThomas Veerman 
344a17663cSThomas Veerman bucket *
make_bucket(const char * name)354a17663cSThomas Veerman make_bucket(const char *name)
364a17663cSThomas Veerman {
374a17663cSThomas Veerman     bucket *bp;
384a17663cSThomas Veerman 
394a17663cSThomas Veerman     assert(name != 0);
404a17663cSThomas Veerman 
4184d9c625SLionel Sambuc     bp = TMALLOC(bucket, 1);
424a17663cSThomas Veerman     NO_SPACE(bp);
434a17663cSThomas Veerman 
444a17663cSThomas Veerman     bp->link = 0;
454a17663cSThomas Veerman     bp->next = 0;
464a17663cSThomas Veerman 
4784d9c625SLionel Sambuc     bp->name = TMALLOC(char, strlen(name) + 1);
484a17663cSThomas Veerman     NO_SPACE(bp->name);
494a17663cSThomas Veerman 
504a17663cSThomas Veerman     bp->tag = 0;
514a17663cSThomas Veerman     bp->value = UNDEFINED;
524a17663cSThomas Veerman     bp->index = 0;
534a17663cSThomas Veerman     bp->prec = 0;
544a17663cSThomas Veerman     bp->class = UNKNOWN;
554a17663cSThomas Veerman     bp->assoc = TOKEN;
56*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
57*0a6a1f1dSLionel Sambuc     bp->args = -1;
58*0a6a1f1dSLionel Sambuc     bp->argnames = 0;
59*0a6a1f1dSLionel Sambuc     bp->argtags = 0;
60*0a6a1f1dSLionel Sambuc     bp->destructor = 0;
61*0a6a1f1dSLionel Sambuc #endif
624a17663cSThomas Veerman     strcpy(bp->name, name);
634a17663cSThomas Veerman 
644a17663cSThomas Veerman     return (bp);
654a17663cSThomas Veerman }
664a17663cSThomas Veerman 
674a17663cSThomas Veerman bucket *
lookup(const char * name)684a17663cSThomas Veerman lookup(const char *name)
694a17663cSThomas Veerman {
704a17663cSThomas Veerman     bucket *bp, **bpp;
714a17663cSThomas Veerman 
724a17663cSThomas Veerman     bpp = symbol_table + hash(name);
734a17663cSThomas Veerman     bp = *bpp;
744a17663cSThomas Veerman 
754a17663cSThomas Veerman     while (bp)
764a17663cSThomas Veerman     {
774a17663cSThomas Veerman 	if (strcmp(name, bp->name) == 0)
784a17663cSThomas Veerman 	    return (bp);
794a17663cSThomas Veerman 	bpp = &bp->link;
804a17663cSThomas Veerman 	bp = *bpp;
814a17663cSThomas Veerman     }
824a17663cSThomas Veerman 
834a17663cSThomas Veerman     *bpp = bp = make_bucket(name);
844a17663cSThomas Veerman     last_symbol->next = bp;
854a17663cSThomas Veerman     last_symbol = bp;
864a17663cSThomas Veerman 
874a17663cSThomas Veerman     return (bp);
884a17663cSThomas Veerman }
894a17663cSThomas Veerman 
904a17663cSThomas Veerman void
create_symbol_table(void)914a17663cSThomas Veerman create_symbol_table(void)
924a17663cSThomas Veerman {
934a17663cSThomas Veerman     int i;
944a17663cSThomas Veerman     bucket *bp;
954a17663cSThomas Veerman 
9684d9c625SLionel Sambuc     symbol_table = TMALLOC(bucket *, TABLE_SIZE);
974a17663cSThomas Veerman     NO_SPACE(symbol_table);
984a17663cSThomas Veerman 
994a17663cSThomas Veerman     for (i = 0; i < TABLE_SIZE; i++)
1004a17663cSThomas Veerman 	symbol_table[i] = 0;
1014a17663cSThomas Veerman 
1024a17663cSThomas Veerman     bp = make_bucket("error");
1034a17663cSThomas Veerman     bp->index = 1;
1044a17663cSThomas Veerman     bp->class = TERM;
1054a17663cSThomas Veerman 
1064a17663cSThomas Veerman     first_symbol = bp;
1074a17663cSThomas Veerman     last_symbol = bp;
1084a17663cSThomas Veerman     symbol_table[hash("error")] = bp;
1094a17663cSThomas Veerman }
1104a17663cSThomas Veerman 
1114a17663cSThomas Veerman void
free_symbol_table(void)1124a17663cSThomas Veerman free_symbol_table(void)
1134a17663cSThomas Veerman {
1144a17663cSThomas Veerman     FREE(symbol_table);
1154a17663cSThomas Veerman     symbol_table = 0;
1164a17663cSThomas Veerman }
1174a17663cSThomas Veerman 
1184a17663cSThomas Veerman void
free_symbols(void)1194a17663cSThomas Veerman free_symbols(void)
1204a17663cSThomas Veerman {
1214a17663cSThomas Veerman     bucket *p, *q;
1224a17663cSThomas Veerman 
1234a17663cSThomas Veerman     for (p = first_symbol; p; p = q)
1244a17663cSThomas Veerman     {
1254a17663cSThomas Veerman 	q = p->next;
1264a17663cSThomas Veerman 	FREE(p);
1274a17663cSThomas Veerman     }
1284a17663cSThomas Veerman }
129