1 | /* GNU m4 -- A simple macro processor
|
---|
2 |
|
---|
3 | Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2003, 2006 Free
|
---|
4 | Software Foundation, Inc.
|
---|
5 |
|
---|
6 | This program 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 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program 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 this program; if not, write to the Free Software
|
---|
18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
19 | 02110-1301 USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | /* This file handles all the low level work around the symbol table. The
|
---|
23 | symbol table is a simple chained hash table. Each symbol is described
|
---|
24 | by a struct symbol, which is placed in the hash table based upon the
|
---|
25 | symbol name. Symbols that hash to the same entry in the table are
|
---|
26 | kept on a list, sorted by name. As a special case, to facilitate the
|
---|
27 | "pushdef" and "popdef" builtins, a symbol can be several times in the
|
---|
28 | symbol table, one for each definition. Since the name is the same,
|
---|
29 | all the entries for the symbol will be on the same list, and will
|
---|
30 | also, because the list is sorted, be adjacent. All the entries for a
|
---|
31 | name are simply ordered on the list by age. The current definition
|
---|
32 | will then always be the first found. */
|
---|
33 |
|
---|
34 | #include "m4.h"
|
---|
35 | #include <limits.h>
|
---|
36 |
|
---|
37 | #ifdef DEBUG_SYM
|
---|
38 | /* When evaluating hash table performance, this profiling code shows
|
---|
39 | how many collisions were encountered. */
|
---|
40 |
|
---|
41 | struct profile
|
---|
42 | {
|
---|
43 | int entry; /* Number of times lookup_symbol called with this mode. */
|
---|
44 | int comparisons; /* Number of times strcmp was called. */
|
---|
45 | int misses; /* Number of times strcmp did not return 0. */
|
---|
46 | long long bytes; /* Number of bytes compared. */
|
---|
47 | };
|
---|
48 |
|
---|
49 | static struct profile profiles[5];
|
---|
50 | static symbol_lookup current_mode;
|
---|
51 |
|
---|
52 | /* On exit, show a profile of symbol table performance. */
|
---|
53 | static void
|
---|
54 | show_profile (void)
|
---|
55 | {
|
---|
56 | int i;
|
---|
57 | for (i = 0; i < 5; i++)
|
---|
58 | {
|
---|
59 | fprintf(stderr, "m4: lookup mode %d called %d times, %d compares, "
|
---|
60 | "%d misses, %lld bytes\n",
|
---|
61 | i, profiles[i].entry, profiles[i].comparisons,
|
---|
62 | profiles[i].misses, profiles[i].bytes);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /* Like strcmp (S1, S2), but also track profiling statistics. */
|
---|
67 | static int
|
---|
68 | profile_strcmp (const char *s1, const char *s2)
|
---|
69 | {
|
---|
70 | int i = 1;
|
---|
71 | int result;
|
---|
72 | while (*s1 && *s1 == *s2)
|
---|
73 | {
|
---|
74 | s1++;
|
---|
75 | s2++;
|
---|
76 | i++;
|
---|
77 | }
|
---|
78 | result = (unsigned char) *s1 - (unsigned char) *s2;
|
---|
79 | profiles[current_mode].comparisons++;
|
---|
80 | if (result != 0)
|
---|
81 | profiles[current_mode].misses++;
|
---|
82 | profiles[current_mode].bytes += i;
|
---|
83 | return result;
|
---|
84 | }
|
---|
85 |
|
---|
86 | # define strcmp profile_strcmp
|
---|
87 | #endif /* DEBUG_SYM */
|
---|
88 |
|
---|
89 | |
---|
90 |
|
---|
91 | /*----------------------------------------------------------------------.
|
---|
92 | | Initialise the symbol table, by allocating the necessary storage, and |
|
---|
93 | | zeroing all the entries. |
|
---|
94 | `----------------------------------------------------------------------*/
|
---|
95 |
|
---|
96 | /* Pointer to symbol table. */
|
---|
97 | symbol **symtab;
|
---|
98 |
|
---|
99 | void
|
---|
100 | symtab_init (void)
|
---|
101 | {
|
---|
102 | size_t i;
|
---|
103 | symbol **s;
|
---|
104 |
|
---|
105 | s = symtab = (symbol **) xnmalloc (hash_table_size, sizeof (symbol *));
|
---|
106 |
|
---|
107 | for (i = 0; i < hash_table_size; i++)
|
---|
108 | s[i] = NULL;
|
---|
109 |
|
---|
110 | #ifdef DEBUG_SYM
|
---|
111 | {
|
---|
112 | int e = atexit(show_profile);
|
---|
113 | if (e != 0)
|
---|
114 | M4ERROR ((warning_status, 0,
|
---|
115 | "INTERNAL ERROR: unable to show symtab profile"));
|
---|
116 | }
|
---|
117 | #endif /* DEBUG_SYM */
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*--------------------------------------------------.
|
---|
121 | | Return a hashvalue for a string, from GNU-emacs. |
|
---|
122 | `--------------------------------------------------*/
|
---|
123 |
|
---|
124 | static size_t
|
---|
125 | hash (const char *s)
|
---|
126 | {
|
---|
127 | register size_t val = 0;
|
---|
128 |
|
---|
129 | register const char *ptr = s;
|
---|
130 | register char ch;
|
---|
131 |
|
---|
132 | while ((ch = *ptr++) != '\0')
|
---|
133 | val = (val << 7) + (val >> (sizeof (val) * CHAR_BIT - 7)) + ch;
|
---|
134 | return val;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*--------------------------------------------.
|
---|
138 | | Free all storage associated with a symbol. |
|
---|
139 | `--------------------------------------------*/
|
---|
140 |
|
---|
141 | void
|
---|
142 | free_symbol (symbol *sym)
|
---|
143 | {
|
---|
144 | if (SYMBOL_PENDING_EXPANSIONS (sym) > 0)
|
---|
145 | SYMBOL_DELETED (sym) = true;
|
---|
146 | else
|
---|
147 | {
|
---|
148 | free (SYMBOL_NAME (sym));
|
---|
149 | if (SYMBOL_TYPE (sym) == TOKEN_TEXT)
|
---|
150 | free (SYMBOL_TEXT (sym));
|
---|
151 | free (sym);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*-------------------------------------------------------------------.
|
---|
156 | | Search in, and manipulation of the symbol table, are all done by |
|
---|
157 | | lookup_symbol (). It basically hashes NAME to a list in the |
|
---|
158 | | symbol table, and searches this list for the first occurrence of a |
|
---|
159 | | symbol with the name. |
|
---|
160 | | |
|
---|
161 | | The MODE parameter determines what lookup_symbol () will do. It |
|
---|
162 | | can either just do a lookup, do a lookup and insert if not |
|
---|
163 | | present, do an insertion even if the name is already in the list, |
|
---|
164 | | delete the first occurrence of the name on the list, or delete all |
|
---|
165 | | occurrences of the name on the list. |
|
---|
166 | `-------------------------------------------------------------------*/
|
---|
167 |
|
---|
168 | symbol *
|
---|
169 | lookup_symbol (const char *name, symbol_lookup mode)
|
---|
170 | {
|
---|
171 | size_t h;
|
---|
172 | int cmp = 1;
|
---|
173 | symbol *sym, *prev;
|
---|
174 | symbol **spp;
|
---|
175 |
|
---|
176 | #if DEBUG_SYM
|
---|
177 | current_mode = mode;
|
---|
178 | profiles[mode].entry++;
|
---|
179 | #endif /* DEBUG_SYM */
|
---|
180 |
|
---|
181 | h = hash (name);
|
---|
182 | sym = symtab[h % hash_table_size];
|
---|
183 |
|
---|
184 | for (prev = NULL; sym != NULL; prev = sym, sym = sym->next)
|
---|
185 | {
|
---|
186 | cmp = strcmp (SYMBOL_NAME (sym), name);
|
---|
187 | if (cmp >= 0)
|
---|
188 | break;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* If just searching, return status of search. */
|
---|
192 |
|
---|
193 | if (mode == SYMBOL_LOOKUP)
|
---|
194 | return cmp == 0 ? sym : NULL;
|
---|
195 |
|
---|
196 | /* Symbol not found. */
|
---|
197 |
|
---|
198 | spp = (prev != NULL) ? &prev->next : &symtab[h % hash_table_size];
|
---|
199 |
|
---|
200 | switch (mode)
|
---|
201 | {
|
---|
202 |
|
---|
203 | case SYMBOL_INSERT:
|
---|
204 |
|
---|
205 | /* If the name was found in the table, check whether it is still in
|
---|
206 | use by a pending expansion. If so, replace the table element with
|
---|
207 | a new one; if not, just return the symbol. If not found, just
|
---|
208 | insert the name, and return the new symbol. */
|
---|
209 |
|
---|
210 | if (cmp == 0 && sym != NULL)
|
---|
211 | {
|
---|
212 | if (SYMBOL_PENDING_EXPANSIONS (sym) > 0)
|
---|
213 | {
|
---|
214 | symbol *old = sym;
|
---|
215 | SYMBOL_DELETED (old) = true;
|
---|
216 |
|
---|
217 | sym = (symbol *) xmalloc (sizeof (symbol));
|
---|
218 | SYMBOL_TYPE (sym) = TOKEN_VOID;
|
---|
219 | SYMBOL_TRACED (sym) = SYMBOL_TRACED (old);
|
---|
220 | SYMBOL_NAME (sym) = xstrdup (name);
|
---|
221 | SYMBOL_SHADOWED (sym) = false;
|
---|
222 | SYMBOL_MACRO_ARGS (sym) = false;
|
---|
223 | SYMBOL_BLIND_NO_ARGS (sym) = false;
|
---|
224 | SYMBOL_DELETED (sym) = false;
|
---|
225 | SYMBOL_PENDING_EXPANSIONS (sym) = 0;
|
---|
226 |
|
---|
227 | SYMBOL_NEXT (sym) = SYMBOL_NEXT (old);
|
---|
228 | SYMBOL_NEXT (old) = NULL;
|
---|
229 | (*spp) = sym;
|
---|
230 | }
|
---|
231 | return sym;
|
---|
232 | }
|
---|
233 | /* Fall through. */
|
---|
234 |
|
---|
235 | case SYMBOL_PUSHDEF:
|
---|
236 |
|
---|
237 | /* Insert a name in the symbol table. If there is already a symbol
|
---|
238 | with the name, insert this in front of it, and mark the old
|
---|
239 | symbol as "shadowed". */
|
---|
240 |
|
---|
241 | sym = (symbol *) xmalloc (sizeof (symbol));
|
---|
242 | SYMBOL_TYPE (sym) = TOKEN_VOID;
|
---|
243 | SYMBOL_TRACED (sym) = false;
|
---|
244 | SYMBOL_NAME (sym) = xstrdup (name);
|
---|
245 | SYMBOL_SHADOWED (sym) = false;
|
---|
246 | SYMBOL_MACRO_ARGS (sym) = false;
|
---|
247 | SYMBOL_BLIND_NO_ARGS (sym) = false;
|
---|
248 | SYMBOL_DELETED (sym) = false;
|
---|
249 | SYMBOL_PENDING_EXPANSIONS (sym) = 0;
|
---|
250 |
|
---|
251 | SYMBOL_NEXT (sym) = *spp;
|
---|
252 | (*spp) = sym;
|
---|
253 |
|
---|
254 | if (mode == SYMBOL_PUSHDEF && cmp == 0)
|
---|
255 | {
|
---|
256 | SYMBOL_SHADOWED (SYMBOL_NEXT (sym)) = true;
|
---|
257 | SYMBOL_TRACED (sym) = SYMBOL_TRACED (SYMBOL_NEXT (sym));
|
---|
258 | }
|
---|
259 | return sym;
|
---|
260 |
|
---|
261 | case SYMBOL_DELETE:
|
---|
262 | case SYMBOL_POPDEF:
|
---|
263 |
|
---|
264 | /* Delete occurrences of symbols with NAME. SYMBOL_DELETE kills
|
---|
265 | all definitions, SYMBOL_POPDEF kills only the first.
|
---|
266 | However, if the last instance of a symbol is marked for
|
---|
267 | tracing, reinsert a placeholder in the table. And if the
|
---|
268 | definition is still in use, let the caller free the memory
|
---|
269 | after it is done with the symbol. */
|
---|
270 |
|
---|
271 | if (cmp != 0 || sym == NULL)
|
---|
272 | return NULL;
|
---|
273 | {
|
---|
274 | bool traced = false;
|
---|
275 | if (SYMBOL_NEXT (sym) != NULL
|
---|
276 | && SYMBOL_SHADOWED (SYMBOL_NEXT (sym))
|
---|
277 | && mode == SYMBOL_POPDEF)
|
---|
278 | {
|
---|
279 | SYMBOL_SHADOWED (SYMBOL_NEXT (sym)) = false;
|
---|
280 | SYMBOL_TRACED (SYMBOL_NEXT (sym)) = SYMBOL_TRACED (sym);
|
---|
281 | }
|
---|
282 | else
|
---|
283 | traced = SYMBOL_TRACED (sym);
|
---|
284 | do
|
---|
285 | {
|
---|
286 | *spp = SYMBOL_NEXT (sym);
|
---|
287 | free_symbol (sym);
|
---|
288 | sym = *spp;
|
---|
289 | }
|
---|
290 | while (*spp != NULL && SYMBOL_SHADOWED (*spp)
|
---|
291 | && mode == SYMBOL_DELETE);
|
---|
292 | if (traced)
|
---|
293 | {
|
---|
294 | sym = (symbol *) xmalloc (sizeof (symbol));
|
---|
295 | SYMBOL_TYPE (sym) = TOKEN_VOID;
|
---|
296 | SYMBOL_TRACED (sym) = true;
|
---|
297 | SYMBOL_NAME (sym) = xstrdup (name);
|
---|
298 | SYMBOL_SHADOWED (sym) = false;
|
---|
299 | SYMBOL_MACRO_ARGS (sym) = false;
|
---|
300 | SYMBOL_BLIND_NO_ARGS (sym) = false;
|
---|
301 | SYMBOL_DELETED (sym) = false;
|
---|
302 | SYMBOL_PENDING_EXPANSIONS (sym) = 0;
|
---|
303 |
|
---|
304 | SYMBOL_NEXT (sym) = *spp;
|
---|
305 | (*spp) = sym;
|
---|
306 | }
|
---|
307 | }
|
---|
308 | return NULL;
|
---|
309 |
|
---|
310 | default:
|
---|
311 | M4ERROR ((warning_status, 0,
|
---|
312 | "INTERNAL ERROR: invalid mode to symbol_lookup ()"));
|
---|
313 | abort ();
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*-----------------------------------------------------------------.
|
---|
318 | | The following function is used for the cases where we want to do |
|
---|
319 | | something to each and every symbol in the table. The function |
|
---|
320 | | hack_all_symbols () traverses the symbol table, and calls a |
|
---|
321 | | specified function FUNC for each symbol in the table. FUNC is |
|
---|
322 | | called with a pointer to the symbol, and the DATA argument. |
|
---|
323 | | |
|
---|
324 | | FUNC may safely call lookup_symbol with mode SYMBOL_POPDEF or |
|
---|
325 | | SYMBOL_LOOKUP, but any other mode can break the iteration. |
|
---|
326 | `-----------------------------------------------------------------*/
|
---|
327 |
|
---|
328 | void
|
---|
329 | hack_all_symbols (hack_symbol *func, void *data)
|
---|
330 | {
|
---|
331 | size_t h;
|
---|
332 | symbol *sym;
|
---|
333 | symbol *next;
|
---|
334 |
|
---|
335 | for (h = 0; h < hash_table_size; h++)
|
---|
336 | {
|
---|
337 | /* We allow func to call SYMBOL_POPDEF, which can invalidate
|
---|
338 | sym, so we must grab the next element to traverse before
|
---|
339 | calling func. */
|
---|
340 | for (sym = symtab[h]; sym != NULL; sym = next)
|
---|
341 | {
|
---|
342 | next = SYMBOL_NEXT (sym);
|
---|
343 | func (sym, data);
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 | |
---|
348 |
|
---|
349 | #ifdef DEBUG_SYM
|
---|
350 |
|
---|
351 | static void symtab_print_list (int i);
|
---|
352 |
|
---|
353 | static void M4_GNUC_UNUSED
|
---|
354 | symtab_debug (void)
|
---|
355 | {
|
---|
356 | token_data td;
|
---|
357 | const char *text;
|
---|
358 | symbol *s;
|
---|
359 | int delete;
|
---|
360 | static int i;
|
---|
361 |
|
---|
362 | while (next_token (&td) == TOKEN_WORD)
|
---|
363 | {
|
---|
364 | text = TOKEN_DATA_TEXT (&td);
|
---|
365 | if (*text == '_')
|
---|
366 | {
|
---|
367 | delete = 1;
|
---|
368 | text++;
|
---|
369 | }
|
---|
370 | else
|
---|
371 | delete = 0;
|
---|
372 |
|
---|
373 | s = lookup_symbol (text, SYMBOL_LOOKUP);
|
---|
374 |
|
---|
375 | if (s == NULL)
|
---|
376 | printf ("Name `%s' is unknown\n", text);
|
---|
377 |
|
---|
378 | if (delete)
|
---|
379 | (void) lookup_symbol (text, SYMBOL_DELETE);
|
---|
380 | else
|
---|
381 | (void) lookup_symbol (text, SYMBOL_INSERT);
|
---|
382 | }
|
---|
383 | symtab_print_list (i++);
|
---|
384 | }
|
---|
385 |
|
---|
386 | static void
|
---|
387 | symtab_print_list (int i)
|
---|
388 | {
|
---|
389 | symbol *sym;
|
---|
390 | size_t h;
|
---|
391 |
|
---|
392 | printf ("Symbol dump #%d:\n", i);
|
---|
393 | for (h = 0; h < hash_table_size; h++)
|
---|
394 | for (sym = symtab[h]; sym != NULL; sym = sym->next)
|
---|
395 | printf ("\tname %s, bucket %lu, addr %p, next %p, "
|
---|
396 | "flags%s%s%s, pending %d\n",
|
---|
397 | SYMBOL_NAME (sym),
|
---|
398 | (unsigned long int) h, sym, SYMBOL_NEXT (sym),
|
---|
399 | SYMBOL_TRACED (sym) ? " traced" : "",
|
---|
400 | SYMBOL_SHADOWED (sym) ? " shadowed" : "",
|
---|
401 | SYMBOL_DELETED (sym) ? " deleted" : "",
|
---|
402 | SYMBOL_PENDING_EXPANSIONS (sym));
|
---|
403 | }
|
---|
404 |
|
---|
405 | #endif /* DEBUG_SYM */
|
---|