daily update
[deliverable/binutils-gdb.git] / gas / hash.c
CommitLineData
54d22525 1/* hash.c -- gas hash table code
f7e42eb4 2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
ec2655a6 3 2000, 2001, 2002, 2003, 2005, 2007
252b5132
RH
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
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
ec2655a6 10 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
11 any later version.
12
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.
17
18 You should have received a copy of the GNU General Public License
54d22525 19 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
54d22525
ILT
22
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
436d9e46 26 assembler. The assembler does not need to derive structures that
54d22525
ILT
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. */
252b5132
RH
31
32#include "as.h"
3882b010 33#include "safe-ctype.h"
54d22525 34#include "obstack.h"
252b5132 35
54d22525 36/* An entry in a hash table. */
252b5132 37
b041f888 38struct hash_entry {
54d22525
ILT
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;
252b5132
RH
48};
49
54d22525
ILT
50/* A hash table. */
51
b041f888 52struct hash_control {
54d22525
ILT
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;
59
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 */
252b5132 69};
54d22525 70
4bdd3565
NC
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. */
75
f7a568ea 76static unsigned long gas_hash_table_size = 65537;
4bdd3565
NC
77
78void
f7a568ea 79set_gas_hash_table_size (unsigned long size)
4bdd3565
NC
80{
81 gas_hash_table_size = size;
82}
83
84/* FIXME: This function should be amalgmated with bfd/hash.c:bfd_hash_set_default_size(). */
f7a568ea 85static unsigned long
4bdd3565
NC
86get_gas_hash_table_size (void)
87{
88 /* Extend this prime list if you want more granularity of hash table size. */
f7a568ea 89 static const unsigned long hash_size_primes[] =
4bdd3565
NC
90 {
91 1021, 4051, 8599, 16699, 65537
92 };
93 unsigned int index;
94
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;
101
102 return hash_size_primes[index];
103}
104
54d22525
ILT
105/* Create a hash table. This return a control block. */
106
252b5132 107struct hash_control *
b1f1fa96 108hash_new (void)
252b5132 109{
f7a568ea
NC
110 unsigned long size;
111 unsigned long alloc;
54d22525 112 struct hash_control *ret;
54d22525 113
4bdd3565 114 size = get_gas_hash_table_size ();
54d22525 115
4bdd3565 116 ret = xmalloc (sizeof *ret);
54d22525
ILT
117 obstack_begin (&ret->memory, chunksize);
118 alloc = size * sizeof (struct hash_entry *);
4bdd3565 119 ret->table = obstack_alloc (&ret->memory, alloc);
54d22525
ILT
120 memset (ret->table, 0, alloc);
121 ret->size = size;
122
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
131
8fc2faa7 132 return ret;
252b5132
RH
133}
134
54d22525
ILT
135/* Delete a hash table, freeing all allocated memory. */
136
252b5132 137void
b1f1fa96 138hash_die (struct hash_control *table)
252b5132 139{
54d22525
ILT
140 obstack_free (&table->memory, 0);
141 free (table);
252b5132 142}
54d22525
ILT
143
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.
149
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. */
152
54d22525 153static struct hash_entry *
c19d1205 154hash_lookup (struct hash_control *table, const char *key, size_t len,
b1f1fa96 155 struct hash_entry ***plist, unsigned long *phash)
252b5132 156{
c19d1205
ZW
157 unsigned long hash;
158 size_t n;
159 unsigned int c;
54d22525
ILT
160 unsigned int index;
161 struct hash_entry **list;
162 struct hash_entry *p;
163 struct hash_entry *prev;
164
165#ifdef HASH_STATISTICS
166 ++table->lookups;
167#endif
252b5132 168
54d22525 169 hash = 0;
c19d1205 170 for (n = 0; n < len; n++)
252b5132 171 {
c19d1205 172 c = key[n];
54d22525
ILT
173 hash += c + (c << 17);
174 hash ^= hash >> 2;
252b5132 175 }
54d22525
ILT
176 hash += len + (len << 17);
177 hash ^= hash >> 2;
178
179 if (phash != NULL)
180 *phash = hash;
181
182 index = hash % table->size;
183 list = table->table + index;
252b5132 184
54d22525
ILT
185 if (plist != NULL)
186 *plist = list;
187
188 prev = NULL;
189 for (p = *list; p != NULL; p = p->next)
252b5132 190 {
54d22525
ILT
191#ifdef HASH_STATISTICS
192 ++table->hash_compares;
193#endif
194
195 if (p->hash == hash)
252b5132 196 {
54d22525
ILT
197#ifdef HASH_STATISTICS
198 ++table->string_compares;
199#endif
200
c19d1205 201 if (strncmp (p->string, key, len) == 0 && p->string[len] == '\0')
54d22525
ILT
202 {
203 if (prev != NULL)
204 {
205 prev->next = p->next;
206 p->next = *list;
207 *list = p;
208 }
209
210 return p;
211 }
252b5132 212 }
252b5132 213
54d22525 214 prev = p;
252b5132 215 }
54d22525
ILT
216
217 return NULL;
252b5132 218}
54d22525
ILT
219
220/* Insert an entry into a hash table. This returns NULL on success.
221 On error, it returns a printable string indicating the error. It
222 is considered to be an error if the entry already exists in the
223 hash table. */
224
225const char *
b1f1fa96 226hash_insert (struct hash_control *table, const char *key, PTR value)
252b5132 227{
54d22525
ILT
228 struct hash_entry *p;
229 struct hash_entry **list;
230 unsigned long hash;
252b5132 231
c19d1205 232 p = hash_lookup (table, key, strlen (key), &list, &hash);
54d22525
ILT
233 if (p != NULL)
234 return "exists";
235
236#ifdef HASH_STATISTICS
237 ++table->insertions;
238#endif
239
8fc2faa7 240 p = (struct hash_entry *) obstack_alloc (&table->memory, sizeof (*p));
54d22525
ILT
241 p->string = key;
242 p->hash = hash;
243 p->data = value;
244
245 p->next = *list;
246 *list = p;
247
248 return NULL;
252b5132 249}
54d22525
ILT
250
251/* Insert or replace an entry in a hash table. This returns NULL on
252 success. On error, it returns a printable string indicating the
253 error. If an entry already exists, its value is replaced. */
254
252b5132 255const char *
b1f1fa96 256hash_jam (struct hash_control *table, const char *key, PTR value)
252b5132 257{
54d22525
ILT
258 struct hash_entry *p;
259 struct hash_entry **list;
260 unsigned long hash;
252b5132 261
c19d1205 262 p = hash_lookup (table, key, strlen (key), &list, &hash);
54d22525 263 if (p != NULL)
252b5132 264 {
54d22525
ILT
265#ifdef HASH_STATISTICS
266 ++table->replacements;
267#endif
268
269 p->data = value;
252b5132 270 }
54d22525 271 else
252b5132 272 {
54d22525
ILT
273#ifdef HASH_STATISTICS
274 ++table->insertions;
275#endif
276
8fc2faa7 277 p = (struct hash_entry *) obstack_alloc (&table->memory, sizeof (*p));
54d22525
ILT
278 p->string = key;
279 p->hash = hash;
280 p->data = value;
281
282 p->next = *list;
283 *list = p;
252b5132 284 }
54d22525
ILT
285
286 return NULL;
252b5132
RH
287}
288
7e70f1af
L
289/* Replace an existing entry in a hash table. This returns the old
290 value stored for the entry. If the entry is not found in the hash
291 table, this does nothing and returns NULL. */
292
293PTR
294hash_replace (struct hash_control *table, const char *key, PTR value)
295{
296 struct hash_entry *p;
297 PTR ret;
298
c19d1205 299 p = hash_lookup (table, key, strlen (key), NULL, NULL);
7e70f1af
L
300 if (p == NULL)
301 return NULL;
302
303#ifdef HASH_STATISTICS
304 ++table->replacements;
305#endif
306
307 ret = p->data;
308
309 p->data = value;
310
311 return ret;
312}
313
54d22525
ILT
314/* Find an entry in a hash table, returning its value. Returns NULL
315 if the entry is not found. */
316
252b5132 317PTR
b1f1fa96 318hash_find (struct hash_control *table, const char *key)
252b5132 319{
54d22525 320 struct hash_entry *p;
252b5132 321
c19d1205
ZW
322 p = hash_lookup (table, key, strlen (key), NULL, NULL);
323 if (p == NULL)
324 return NULL;
325
326 return p->data;
327}
328
329/* As hash_find, but KEY is of length LEN and is not guaranteed to be
330 NUL-terminated. */
331
332PTR
333hash_find_n (struct hash_control *table, const char *key, size_t len)
334{
335 struct hash_entry *p;
336
337 p = hash_lookup (table, key, len, NULL, NULL);
54d22525 338 if (p == NULL)
252b5132 339 return NULL;
54d22525
ILT
340
341 return p->data;
252b5132 342}
54d22525 343
7e70f1af
L
344/* Delete an entry from a hash table. This returns the value stored
345 for that entry, or NULL if there is no such entry. */
346
347PTR
348hash_delete (struct hash_control *table, const char *key)
349{
350 struct hash_entry *p;
351 struct hash_entry **list;
352
c19d1205 353 p = hash_lookup (table, key, strlen (key), &list, NULL);
7e70f1af
L
354 if (p == NULL)
355 return NULL;
356
357 if (p != *list)
358 abort ();
359
360#ifdef HASH_STATISTICS
361 ++table->deletions;
362#endif
363
364 *list = p->next;
365
366 /* Note that we never reclaim the memory for this entry. If gas
367 ever starts deleting hash table entries in a big way, this will
368 have to change. */
369
370 return p->data;
371}
372
54d22525
ILT
373/* Traverse a hash table. Call the function on every entry in the
374 hash table. */
375
252b5132 376void
b1f1fa96
KH
377hash_traverse (struct hash_control *table,
378 void (*pfn) (const char *key, PTR value))
252b5132 379{
54d22525 380 unsigned int i;
252b5132 381
54d22525
ILT
382 for (i = 0; i < table->size; ++i)
383 {
384 struct hash_entry *p;
252b5132 385
54d22525
ILT
386 for (p = table->table[i]; p != NULL; p = p->next)
387 (*pfn) (p->string, p->data);
388 }
389}
252b5132 390
54d22525
ILT
391/* Print hash table statistics on the specified file. NAME is the
392 name of the hash table, used for printing a header. */
252b5132 393
54d22525 394void
b1f1fa96
KH
395hash_print_statistics (FILE *f ATTRIBUTE_UNUSED,
396 const char *name ATTRIBUTE_UNUSED,
397 struct hash_control *table ATTRIBUTE_UNUSED)
54d22525
ILT
398{
399#ifdef HASH_STATISTICS
400 unsigned int i;
401 unsigned long total;
402 unsigned long empty;
403
404 fprintf (f, "%s hash statistics:\n", name);
405 fprintf (f, "\t%lu lookups\n", table->lookups);
406 fprintf (f, "\t%lu hash comparisons\n", table->hash_compares);
407 fprintf (f, "\t%lu string comparisons\n", table->string_compares);
408 fprintf (f, "\t%lu insertions\n", table->insertions);
409 fprintf (f, "\t%lu replacements\n", table->replacements);
410 fprintf (f, "\t%lu deletions\n", table->deletions);
411
412 total = 0;
413 empty = 0;
414 for (i = 0; i < table->size; ++i)
415 {
416 struct hash_entry *p;
252b5132 417
54d22525
ILT
418 if (table->table[i] == NULL)
419 ++empty;
420 else
421 {
422 for (p = table->table[i]; p != NULL; p = p->next)
423 ++total;
424 }
425 }
252b5132 426
54d22525
ILT
427 fprintf (f, "\t%g average chain length\n", (double) total / table->size);
428 fprintf (f, "\t%lu empty slots\n", empty);
429#endif
252b5132
RH
430}
431\f
252b5132
RH
432#ifdef TEST
433
54d22525
ILT
434/* This test program is left over from the old hash table code. */
435
8fc2faa7
KH
436/* Number of hash tables to maintain (at once) in any testing. */
437#define TABLES (6)
438
439/* We can have 12 statistics. */
440#define STATBUFSIZE (12)
441
442/* Display statistics here. */
443int statbuf[STATBUFSIZE];
444
445/* Human farts here. */
446char answer[100];
447
448/* We test many hash tables at once. */
449char *hashtable[TABLES];
252b5132 450
47eebc20 451/* Points to current hash_control. */
8fc2faa7 452char *h;
252b5132
RH
453char **pp;
454char *p;
455char *name;
456char *value;
457int size;
458int used;
459char command;
8fc2faa7
KH
460
461/* Number 0:TABLES-1 of current hashed symbol table. */
462int number;
252b5132 463
54d22525 464int
252b5132
RH
465main ()
466{
467 void applicatee ();
468 void destroy ();
469 char *what ();
470 int *ip;
471
472 number = 0;
473 h = 0;
474 printf ("type h <RETURN> for help\n");
475 for (;;)
476 {
477 printf ("hash_test command: ");
478 gets (answer);
479 command = answer[0];
3882b010 480 command = TOLOWER (command); /* Ecch! */
252b5132
RH
481 switch (command)
482 {
483 case '#':
484 printf ("old hash table #=%d.\n", number);
485 whattable ();
486 break;
487 case '?':
488 for (pp = hashtable; pp < hashtable + TABLES; pp++)
489 {
8fc2faa7
KH
490 printf ("address of hash table #%d control block is %xx\n",
491 pp - hashtable, *pp);
252b5132
RH
492 }
493 break;
494 case 'a':
54d22525 495 hash_traverse (h, applicatee);
252b5132
RH
496 break;
497 case 'd':
54d22525 498 hash_traverse (h, destroy);
252b5132
RH
499 hash_die (h);
500 break;
501 case 'f':
502 p = hash_find (h, name = what ("symbol"));
503 printf ("value of \"%s\" is \"%s\"\n", name, p ? p : "NOT-PRESENT");
504 break;
505 case 'h':
506 printf ("# show old, select new default hash table number\n");
507 printf ("? display all hashtable control block addresses\n");
508 printf ("a apply a simple display-er to each symbol in table\n");
509 printf ("d die: destroy hashtable\n");
510 printf ("f find value of nominated symbol\n");
511 printf ("h this help\n");
512 printf ("i insert value into symbol\n");
513 printf ("j jam value into symbol\n");
514 printf ("n new hashtable\n");
515 printf ("r replace a value with another\n");
516 printf ("s say what %% of table is used\n");
517 printf ("q exit this program\n");
518 printf ("x delete a symbol from table, report its value\n");
519 break;
520 case 'i':
521 p = hash_insert (h, name = what ("symbol"), value = what ("value"));
522 if (p)
523 {
524 printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value,
525 p);
526 }
527 break;
528 case 'j':
529 p = hash_jam (h, name = what ("symbol"), value = what ("value"));
530 if (p)
531 {
532 printf ("symbol=\"%s\" value=\"%s\" error=%s\n", name, value, p);
533 }
534 break;
535 case 'n':
536 h = hashtable[number] = (char *) hash_new ();
537 break;
538 case 'q':
539 exit (EXIT_SUCCESS);
540 case 'r':
541 p = hash_replace (h, name = what ("symbol"), value = what ("value"));
542 printf ("old value was \"%s\"\n", p ? p : "{}");
543 break;
544 case 's':
545 hash_say (h, statbuf, STATBUFSIZE);
546 for (ip = statbuf; ip < statbuf + STATBUFSIZE; ip++)
547 {
548 printf ("%d ", *ip);
549 }
550 printf ("\n");
551 break;
552 case 'x':
553 p = hash_delete (h, name = what ("symbol"));
554 printf ("old value was \"%s\"\n", p ? p : "{}");
555 break;
556 default:
557 printf ("I can't understand command \"%c\"\n", command);
558 break;
559 }
560 }
561}
562
563char *
564what (description)
565 char *description;
566{
252b5132
RH
567 printf (" %s : ", description);
568 gets (answer);
a2c36061 569 return xstrdup (answer);
252b5132
RH
570}
571
572void
573destroy (string, value)
574 char *string;
575 char *value;
576{
577 free (string);
578 free (value);
579}
580
252b5132
RH
581void
582applicatee (string, value)
583 char *string;
584 char *value;
585{
586 printf ("%.20s-%.20s\n", string, value);
587}
588
8fc2faa7
KH
589/* Determine number: what hash table to use.
590 Also determine h: points to hash_control. */
591
54d22525 592void
8fc2faa7 593whattable ()
252b5132 594{
252b5132
RH
595 for (;;)
596 {
597 printf (" what hash table (%d:%d) ? ", 0, TABLES - 1);
598 gets (answer);
599 sscanf (answer, "%d", &number);
600 if (number >= 0 && number < TABLES)
601 {
602 h = hashtable[number];
603 if (!h)
604 {
605 printf ("warning: current hash-table-#%d. has no hash-control\n", number);
606 }
607 return;
608 }
609 else
610 {
611 printf ("invalid hash table number: %d\n", number);
612 }
613 }
614}
615
8fc2faa7 616#endif /* TEST */
This page took 0.482358 seconds and 4 git commands to generate.