2005-05-05 Paul Brook <paul@codesourcery.com>
[binutils.git] / gas / hash.c
blob0f4742a5b02ead3cd967334b3ba67d0f1a70f31f
1 /* hash.c -- gas hash table code
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
3 2000, 2001, 2002, 2003, 2005
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This version of the hash table code is a wholescale replacement of
24 the old hash table code, which was fairly bad. This is based on
25 the hash table code in BFD, but optimized slightly for the
26 assembler. The assembler does not need to derive structures that
27 are stored in the hash table. Instead, it always stores a pointer.
28 The assembler uses the hash table mostly to store symbols, and we
29 don't need to confuse the symbol structure with a hash table
30 structure. */
32 #include "as.h"
33 #include "safe-ctype.h"
34 #include "obstack.h"
36 /* An entry in a hash table. */
38 struct hash_entry {
39 /* Next entry for this hash code. */
40 struct hash_entry *next;
41 /* String being hashed. */
42 const char *string;
43 /* Hash code. This is the full hash code, not the index into the
44 table. */
45 unsigned long hash;
46 /* Pointer being stored in the hash table. */
47 PTR data;
50 /* A hash table. */
52 struct hash_control {
53 /* The hash array. */
54 struct hash_entry **table;
55 /* The number of slots in the hash table. */
56 unsigned int size;
57 /* An obstack for this hash table. */
58 struct obstack memory;
60 #ifdef HASH_STATISTICS
61 /* Statistics. */
62 unsigned long lookups;
63 unsigned long hash_compares;
64 unsigned long string_compares;
65 unsigned long insertions;
66 unsigned long replacements;
67 unsigned long deletions;
68 #endif /* HASH_STATISTICS */
71 /* The default number of entries to use when creating a hash table.
72 Note this value can be reduced to 4051 by using the command line
73 switch --reduce-memory-overheads, or set to other values by using
74 the --hash-size=<NUMBER> switch. */
76 static unsigned long gas_hash_table_size = 65537;
78 void
79 set_gas_hash_table_size (unsigned long size)
81 gas_hash_table_size = size;
84 /* FIXME: This function should be amalgmated with bfd/hash.c:bfd_hash_set_default_size(). */
85 static unsigned long
86 get_gas_hash_table_size (void)
88 /* Extend this prime list if you want more granularity of hash table size. */
89 static const unsigned long hash_size_primes[] =
91 1021, 4051, 8599, 16699, 65537
93 unsigned int index;
95 /* Work out the best prime number near the hash_size.
96 FIXME: This could be a more sophisticated algorithm,
97 but is it really worth implementing it ? */
98 for (index = 0; index < ARRAY_SIZE (hash_size_primes) - 1; ++index)
99 if (gas_hash_table_size <= hash_size_primes[index])
100 break;
102 return hash_size_primes[index];
105 /* Create a hash table. This return a control block. */
107 struct hash_control *
108 hash_new (void)
110 unsigned long size;
111 unsigned long alloc;
112 struct hash_control *ret;
114 size = get_gas_hash_table_size ();
116 ret = xmalloc (sizeof *ret);
117 obstack_begin (&ret->memory, chunksize);
118 alloc = size * sizeof (struct hash_entry *);
119 ret->table = obstack_alloc (&ret->memory, alloc);
120 memset (ret->table, 0, alloc);
121 ret->size = size;
123 #ifdef HASH_STATISTICS
124 ret->lookups = 0;
125 ret->hash_compares = 0;
126 ret->string_compares = 0;
127 ret->insertions = 0;
128 ret->replacements = 0;
129 ret->deletions = 0;
130 #endif
132 return ret;
135 /* Delete a hash table, freeing all allocated memory. */
137 void
138 hash_die (struct hash_control *table)
140 obstack_free (&table->memory, 0);
141 free (table);
144 /* Look up a string in a hash table. This returns a pointer to the
145 hash_entry, or NULL if the string is not in the table. If PLIST is
146 not NULL, this sets *PLIST to point to the start of the list which
147 would hold this hash entry. If PHASH is not NULL, this sets *PHASH
148 to the hash code for KEY.
150 Each time we look up a string, we move it to the start of the list
151 for its hash code, to take advantage of referential locality. */
153 static struct hash_entry *hash_lookup (struct hash_control *,
154 const char *,
155 struct hash_entry ***,
156 unsigned long *);
158 static struct hash_entry *
159 hash_lookup (struct hash_control *table, const char *key,
160 struct hash_entry ***plist, unsigned long *phash)
162 register unsigned long hash;
163 unsigned int len;
164 register const unsigned char *s;
165 register unsigned int c;
166 unsigned int index;
167 struct hash_entry **list;
168 struct hash_entry *p;
169 struct hash_entry *prev;
171 #ifdef HASH_STATISTICS
172 ++table->lookups;
173 #endif
175 hash = 0;
176 len = 0;
177 s = (const unsigned char *) key;
178 while ((c = *s++) != '\0')
180 hash += c + (c << 17);
181 hash ^= hash >> 2;
182 ++len;
184 hash += len + (len << 17);
185 hash ^= hash >> 2;
187 if (phash != NULL)
188 *phash = hash;
190 index = hash % table->size;
191 list = table->table + index;
193 if (plist != NULL)
194 *plist = list;
196 prev = NULL;
197 for (p = *list; p != NULL; p = p->next)
199 #ifdef HASH_STATISTICS
200 ++table->hash_compares;
201 #endif
203 if (p->hash == hash)
205 #ifdef HASH_STATISTICS
206 ++table->string_compares;
207 #endif
209 if (strcmp (p->string, key) == 0)
211 if (prev != NULL)
213 prev->next = p->next;
214 p->next = *list;
215 *list = p;
218 return p;
222 prev = p;
225 return NULL;
228 /* Insert an entry into a hash table. This returns NULL on success.
229 On error, it returns a printable string indicating the error. It
230 is considered to be an error if the entry already exists in the
231 hash table. */
233 const char *
234 hash_insert (struct hash_control *table, const char *key, PTR value)
236 struct hash_entry *p;
237 struct hash_entry **list;
238 unsigned long hash;
240 p = hash_lookup (table, key, &list, &hash);
241 if (p != NULL)
242 return "exists";
244 #ifdef HASH_STATISTICS
245 ++table->insertions;
246 #endif
248 p = (struct hash_entry *) obstack_alloc (&table->memory, sizeof (*p));
249 p->string = key;
250 p->hash = hash;
251 p->data = value;
253 p->next = *list;
254 *list = p;
256 return NULL;
259 /* Insert or replace an entry in a hash table. This returns NULL on
260 success. On error, it returns a printable string indicating the
261 error. If an entry already exists, its value is replaced. */
263 const char *
264 hash_jam (struct hash_control *table, const char *key, PTR value)
266 struct hash_entry *p;
267 struct hash_entry **list;
268 unsigned long hash;
270 p = hash_lookup (table, key, &list, &hash);
271 if (p != NULL)
273 #ifdef HASH_STATISTICS
274 ++table->replacements;
275 #endif
277 p->data = value;
279 else
281 #ifdef HASH_STATISTICS
282 ++table->insertions;
283 #endif
285 p = (struct hash_entry *) obstack_alloc (&table->memory, sizeof (*p));
286 p->string = key;
287 p->hash = hash;
288 p->data = value;
290 p->next = *list;
291 *list = p;
294 return NULL;
297 /* Replace an existing entry in a hash table. This returns the old
298 value stored for the entry. If the entry is not found in the hash
299 table, this does nothing and returns NULL. */
302 hash_replace (struct hash_control *table, const char *key, PTR value)
304 struct hash_entry *p;
305 PTR ret;
307 p = hash_lookup (table, key, NULL, NULL);
308 if (p == NULL)
309 return NULL;
311 #ifdef HASH_STATISTICS
312 ++table->replacements;
313 #endif
315 ret = p->data;
317 p->data = value;
319 return ret;
322 /* Find an entry in a hash table, returning its value. Returns NULL
323 if the entry is not found. */
326 hash_find (struct hash_control *table, const char *key)
328 struct hash_entry *p;
330 p = hash_lookup (table, key, NULL, NULL);
331 if (p == NULL)
332 return NULL;
334 return p->data;
337 /* Delete an entry from a hash table. This returns the value stored
338 for that entry, or NULL if there is no such entry. */
341 hash_delete (struct hash_control *table, const char *key)
343 struct hash_entry *p;
344 struct hash_entry **list;
346 p = hash_lookup (table, key, &list, NULL);
347 if (p == NULL)
348 return NULL;
350 if (p != *list)
351 abort ();
353 #ifdef HASH_STATISTICS
354 ++table->deletions;
355 #endif
357 *list = p->next;
359 /* Note that we never reclaim the memory for this entry. If gas
360 ever starts deleting hash table entries in a big way, this will
361 have to change. */
363 return p->data;
366 /* Traverse a hash table. Call the function on every entry in the
367 hash table. */
369 void
370 hash_traverse (struct hash_control *table,
371 void (*pfn) (const char *key, PTR value))
373 unsigned int i;
375 for (i = 0; i < table->size; ++i)
377 struct hash_entry *p;
379 for (p = table->table[i]; p != NULL; p = p->next)
380 (*pfn) (p->string, p->data);
384 /* Print hash table statistics on the specified file. NAME is the
385 name of the hash table, used for printing a header. */
387 void
388 hash_print_statistics (FILE *f ATTRIBUTE_UNUSED,
389 const char *name ATTRIBUTE_UNUSED,
390 struct hash_control *table ATTRIBUTE_UNUSED)
392 #ifdef HASH_STATISTICS
393 unsigned int i;
394 unsigned long total;
395 unsigned long empty;
397 fprintf (f, "%s hash statistics:\n", name);
398 fprintf (f, "\t%lu lookups\n", table->lookups);
399 fprintf (f, "\t%lu hash comparisons\n", table->hash_compares);
400 fprintf (f, "\t%lu string comparisons\n", table->string_compares);
401 fprintf (f, "\t%lu insertions\n", table->insertions);
402 fprintf (f, "\t%lu replacements\n", table->replacements);
403 fprintf (f, "\t%lu deletions\n", table->deletions);
405 total = 0;
406 empty = 0;
407 for (i = 0; i < table->size; ++i)
409 struct hash_entry *p;
411 if (table->table[i] == NULL)
412 ++empty;
413 else
415 for (p = table->table[i]; p != NULL; p = p->next)
416 ++total;
420 fprintf (f, "\t%g average chain length\n", (double) total / table->size);
421 fprintf (f, "\t%lu empty slots\n", empty);
422 #endif
425 #ifdef TEST
427 /* This test program is left over from the old hash table code. */
429 /* Number of hash tables to maintain (at once) in any testing. */
430 #define TABLES (6)
432 /* We can have 12 statistics. */
433 #define STATBUFSIZE (12)
435 /* Display statistics here. */
436 int statbuf[STATBUFSIZE];
438 /* Human farts here. */
439 char answer[100];
441 /* We test many hash tables at once. */
442 char *hashtable[TABLES];
444 /* Points to current hash_control. */
445 char *h;
446 char **pp;
447 char *p;
448 char *name;
449 char *value;
450 int size;
451 int used;
452 char command;
454 /* Number 0:TABLES-1 of current hashed symbol table. */
455 int number;
458 main ()
460 void applicatee ();
461 void destroy ();
462 char *what ();
463 int *ip;
465 number = 0;
466 h = 0;
467 printf ("type h <RETURN> for help\n");
468 for (;;)
470 printf ("hash_test command: ");
471 gets (answer);
472 command = answer[0];
473 command = TOLOWER (command); /* Ecch! */
474 switch (command)
476 case '#':
477 printf ("old hash table #=%d.\n", number);
478 whattable ();
479 break;
480 case '?':
481 for (pp = hashtable; pp < hashtable + TABLES; pp++)
483 printf ("address of hash table #%d control block is %xx\n",
484 pp - hashtable, *pp);
486 break;
487 case 'a':
488 hash_traverse (h, applicatee);
489 break;
490 case 'd':
491 hash_traverse (h, destroy);
492 hash_die (h);
493 break;
494 case 'f':
495 p = hash_find (h, name = what ("symbol"));
496 printf ("value of \"%s\" is \"%s\"\n", name, p ? p : "NOT-PRESENT");
497 break;
498 case 'h':
499 printf ("# show old, select new default hash table number\n");
500 printf ("? display all hashtable control block addresses\n");
501 printf ("a apply a simple display-er to each symbol in table\n");
502 printf ("d die: destroy hashtable\n");
503 printf ("f find value of nominated symbol\n");
504 printf ("h this help\n");
505 printf ("i insert value into symbol\n");
506 printf ("j jam value into symbol\n");
507 printf ("n new hashtable\n");
508 printf ("r replace a value with another\n");
509 printf ("s say what %% of table is used\n");
510 printf ("q exit this program\n");
511 printf ("x delete a symbol from table, report its value\n");
512 break;
513 case 'i':
514 p = hash_insert (h, name = what ("symbol"), value = what ("value"));
515 if (p)
517 printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value,
520 break;
521 case 'j':
522 p = hash_jam (h, name = what ("symbol"), value = what ("value"));
523 if (p)
525 printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, p);
527 break;
528 case 'n':
529 h = hashtable[number] = (char *) hash_new ();
530 break;
531 case 'q':
532 exit (EXIT_SUCCESS);
533 case 'r':
534 p = hash_replace (h, name = what ("symbol"), value = what ("value"));
535 printf ("old value was \"%s\"\n", p ? p : "{}");
536 break;
537 case 's':
538 hash_say (h, statbuf, STATBUFSIZE);
539 for (ip = statbuf; ip < statbuf + STATBUFSIZE; ip++)
541 printf ("%d ", *ip);
543 printf ("\n");
544 break;
545 case 'x':
546 p = hash_delete (h, name = what ("symbol"));
547 printf ("old value was \"%s\"\n", p ? p : "{}");
548 break;
549 default:
550 printf ("I can't understand command \"%c\"\n", command);
551 break;
556 char *
557 what (description)
558 char *description;
560 printf (" %s : ", description);
561 gets (answer);
562 return xstrdup (answer);
565 void
566 destroy (string, value)
567 char *string;
568 char *value;
570 free (string);
571 free (value);
574 void
575 applicatee (string, value)
576 char *string;
577 char *value;
579 printf ("%.20s-%.20s\n", string, value);
582 /* Determine number: what hash table to use.
583 Also determine h: points to hash_control. */
585 void
586 whattable ()
588 for (;;)
590 printf (" what hash table (%d:%d) ? ", 0, TABLES - 1);
591 gets (answer);
592 sscanf (answer, "%d", &number);
593 if (number >= 0 && number < TABLES)
595 h = hashtable[number];
596 if (!h)
598 printf ("warning: current hash-table-#%d. has no hash-control\n", number);
600 return;
602 else
604 printf ("invalid hash table number: %d\n", number);
609 #endif /* TEST */