]>
Commit | Line | Data |
---|---|---|
40675e7c DM |
1 | /* Symbol table manager for Bison, |
2 | Copyright (C) 1984, 1989 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of Bison, the GNU Compiler Compiler. | |
5 | ||
6 | Bison is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | Bison is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with Bison; see the file COPYING. If not, write to | |
c49a8e71 JT |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
40675e7c DM |
20 | |
21 | ||
22 | #include <stdio.h> | |
23 | #include "system.h" | |
7612000c | 24 | #include "alloc.h" |
40675e7c DM |
25 | #include "symtab.h" |
26 | #include "gram.h" | |
27 | ||
28 | ||
40675e7c | 29 | bucket *firstsymbol; |
4a120d45 JT |
30 | static bucket *lastsymbol; |
31 | static bucket **symtab; | |
40675e7c | 32 | |
4a120d45 JT |
33 | extern void tabinit PARAMS((void)); |
34 | extern void free_symtab PARAMS((void)); | |
40675e7c DM |
35 | |
36 | ||
d2729d44 | 37 | static int |
4a120d45 | 38 | hash (const char *key) |
40675e7c | 39 | { |
4a120d45 | 40 | register const char *cp; |
40675e7c DM |
41 | register int k; |
42 | ||
43 | cp = key; | |
44 | k = 0; | |
45 | while (*cp) | |
46 | k = ((k << 1) ^ (*cp++)) & 0x3fff; | |
47 | ||
36281465 | 48 | return k % TABSIZE; |
40675e7c DM |
49 | } |
50 | ||
51 | ||
52 | ||
d2729d44 | 53 | static char * |
4a120d45 | 54 | copys (const char *s) |
40675e7c DM |
55 | { |
56 | register int i; | |
4a120d45 | 57 | register const char *cp; |
40675e7c DM |
58 | register char *result; |
59 | ||
60 | i = 1; | |
61 | for (cp = s; *cp; cp++) | |
62 | i++; | |
63 | ||
64 | result = xmalloc((unsigned int)i); | |
65 | strcpy(result, s); | |
36281465 | 66 | return result; |
40675e7c DM |
67 | } |
68 | ||
69 | ||
70 | void | |
d2729d44 | 71 | tabinit (void) |
40675e7c DM |
72 | { |
73 | /* register int i; JF unused */ | |
74 | ||
75 | symtab = NEW2(TABSIZE, bucket *); | |
76 | ||
77 | firstsymbol = NULL; | |
78 | lastsymbol = NULL; | |
79 | } | |
80 | ||
81 | ||
82 | bucket * | |
4a120d45 | 83 | getsym (const char *key) |
40675e7c DM |
84 | { |
85 | register int hashval; | |
86 | register bucket *bp; | |
87 | register int found; | |
88 | ||
89 | hashval = hash(key); | |
90 | bp = symtab[hashval]; | |
91 | ||
92 | found = 0; | |
93 | while (bp != NULL && found == 0) | |
94 | { | |
95 | if (strcmp(key, bp->tag) == 0) | |
96 | found = 1; | |
97 | else | |
98 | bp = bp->link; | |
99 | } | |
100 | ||
101 | if (found == 0) | |
102 | { | |
103 | nsyms++; | |
104 | ||
105 | bp = NEW(bucket); | |
106 | bp->link = symtab[hashval]; | |
107 | bp->next = NULL; | |
108 | bp->tag = copys(key); | |
109 | bp->class = SUNKNOWN; | |
110 | ||
111 | if (firstsymbol == NULL) | |
112 | { | |
113 | firstsymbol = bp; | |
114 | lastsymbol = bp; | |
115 | } | |
116 | else | |
117 | { | |
118 | lastsymbol->next = bp; | |
119 | lastsymbol = bp; | |
120 | } | |
121 | ||
122 | symtab[hashval] = bp; | |
123 | } | |
124 | ||
36281465 | 125 | return bp; |
40675e7c DM |
126 | } |
127 | ||
128 | ||
129 | void | |
d2729d44 | 130 | free_symtab (void) |
40675e7c DM |
131 | { |
132 | register int i; | |
133 | register bucket *bp,*bptmp;/* JF don't use ptr after free */ | |
134 | ||
135 | for (i = 0; i < TABSIZE; i++) | |
136 | { | |
137 | bp = symtab[i]; | |
138 | while (bp) | |
139 | { | |
140 | bptmp = bp->link; | |
141 | #if 0 /* This causes crashes because one string can appear more than once. */ | |
142 | if (bp->type_name) | |
143 | FREE(bp->type_name); | |
144 | #endif | |
145 | FREE(bp); | |
146 | bp = bptmp; | |
147 | } | |
148 | } | |
149 | FREE(symtab); | |
150 | } |