| 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;
|
|---|
|
|---|