]>
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 | ||
40675e7c | 22 | #include "system.h" |
7612000c | 23 | #include "alloc.h" |
40675e7c DM |
24 | #include "symtab.h" |
25 | #include "gram.h" | |
26 | ||
27 | ||
40675e7c | 28 | bucket *firstsymbol; |
4a120d45 JT |
29 | static bucket *lastsymbol; |
30 | static bucket **symtab; | |
40675e7c | 31 | |
4a120d45 JT |
32 | extern void tabinit PARAMS((void)); |
33 | extern void free_symtab PARAMS((void)); | |
40675e7c DM |
34 | |
35 | ||
d2729d44 | 36 | static int |
4a120d45 | 37 | hash (const char *key) |
40675e7c | 38 | { |
4a120d45 | 39 | register const char *cp; |
40675e7c DM |
40 | register int k; |
41 | ||
42 | cp = key; | |
43 | k = 0; | |
44 | while (*cp) | |
45 | k = ((k << 1) ^ (*cp++)) & 0x3fff; | |
46 | ||
36281465 | 47 | return k % TABSIZE; |
40675e7c DM |
48 | } |
49 | ||
50 | ||
51 | ||
d2729d44 | 52 | static char * |
4a120d45 | 53 | copys (const char *s) |
40675e7c DM |
54 | { |
55 | register int i; | |
4a120d45 | 56 | register const char *cp; |
40675e7c DM |
57 | register char *result; |
58 | ||
59 | i = 1; | |
60 | for (cp = s; *cp; cp++) | |
61 | i++; | |
62 | ||
63 | result = xmalloc((unsigned int)i); | |
64 | strcpy(result, s); | |
36281465 | 65 | return result; |
40675e7c DM |
66 | } |
67 | ||
68 | ||
69 | void | |
d2729d44 | 70 | tabinit (void) |
40675e7c DM |
71 | { |
72 | /* register int i; JF unused */ | |
73 | ||
74 | symtab = NEW2(TABSIZE, bucket *); | |
75 | ||
76 | firstsymbol = NULL; | |
77 | lastsymbol = NULL; | |
78 | } | |
79 | ||
80 | ||
81 | bucket * | |
4a120d45 | 82 | getsym (const char *key) |
40675e7c DM |
83 | { |
84 | register int hashval; | |
85 | register bucket *bp; | |
86 | register int found; | |
87 | ||
88 | hashval = hash(key); | |
89 | bp = symtab[hashval]; | |
90 | ||
91 | found = 0; | |
92 | while (bp != NULL && found == 0) | |
93 | { | |
94 | if (strcmp(key, bp->tag) == 0) | |
95 | found = 1; | |
96 | else | |
97 | bp = bp->link; | |
98 | } | |
99 | ||
100 | if (found == 0) | |
101 | { | |
102 | nsyms++; | |
103 | ||
104 | bp = NEW(bucket); | |
105 | bp->link = symtab[hashval]; | |
106 | bp->next = NULL; | |
107 | bp->tag = copys(key); | |
108 | bp->class = SUNKNOWN; | |
109 | ||
110 | if (firstsymbol == NULL) | |
111 | { | |
112 | firstsymbol = bp; | |
113 | lastsymbol = bp; | |
114 | } | |
115 | else | |
116 | { | |
117 | lastsymbol->next = bp; | |
118 | lastsymbol = bp; | |
119 | } | |
120 | ||
121 | symtab[hashval] = bp; | |
122 | } | |
123 | ||
36281465 | 124 | return bp; |
40675e7c DM |
125 | } |
126 | ||
127 | ||
128 | void | |
d2729d44 | 129 | free_symtab (void) |
40675e7c DM |
130 | { |
131 | register int i; | |
132 | register bucket *bp,*bptmp;/* JF don't use ptr after free */ | |
133 | ||
134 | for (i = 0; i < TABSIZE; i++) | |
135 | { | |
136 | bp = symtab[i]; | |
137 | while (bp) | |
138 | { | |
139 | bptmp = bp->link; | |
140 | #if 0 /* This causes crashes because one string can appear more than once. */ | |
141 | if (bp->type_name) | |
142 | FREE(bp->type_name); | |
143 | #endif | |
144 | FREE(bp); | |
145 | bp = bptmp; | |
146 | } | |
147 | } | |
148 | FREE(symtab); | |
149 | } |