sym.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #ifndef lint
  6. static char rcsid[] = "$Id$";
  7. #endif
  8. /*
  9. * Symbol table management.
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "out.h"
  14. #include "const.h"
  15. #include "arch.h"
  16. #include "memory.h"
  17. #include "debug.h"
  18. #include "defs.h"
  19. /*
  20. * Symbol table types. Each hash table entry contains the offset of a symbol
  21. * struct. `Sy_name' contains the offset the name in the piece of global
  22. * names. `Sy_next' contains the offset of the next symbol of which the
  23. * corresponding name has the same hash value.
  24. */
  25. struct symbol {
  26. ind_t sy_name;
  27. ind_t sy_next;
  28. };
  29. #define NHASH 307 /* Size of hash table. Must be odd. */
  30. static ind_t hashtable[NHASH];
  31. /*
  32. * Initialize the symbol table. All indices should be noticeably invalid.
  33. */
  34. void init_symboltable()
  35. {
  36. ind_t *rap;
  37. for (rap = hashtable; rap < &hashtable[NHASH]; rap++)
  38. *rap = BADOFF;
  39. }
  40. /*
  41. * Search for `string' in the symboltable. The hash value of `string' is in
  42. * `hashval'. The linked list belonging to the entry of hashval
  43. * in the hash table is followed. If the names match, a pointer to the outname
  44. * in this element of the list is returned. When a match cannot be found,
  45. * NIL is returned.
  46. */
  47. struct outname *searchname(char *string, int hashval)
  48. {
  49. char *rcp;
  50. char *namestring;
  51. ind_t symindex;
  52. struct outname *name;
  53. struct symbol *sym;
  54. symindex = hashtable[hashval];
  55. debug("looking for %s %d %ld:", string, hashval, hashtable[hashval]);
  56. while (symindex != BADOFF) {
  57. sym = (struct symbol *)address(ALLOSYMB, symindex);
  58. name = (struct outname *)address(ALLOGLOB, sym->sy_name);
  59. namestring = address(ALLOGCHR, (ind_t)name->on_foff);
  60. rcp = string;
  61. debug("comp %s;", namestring);
  62. while (*rcp == *namestring++)
  63. if (*rcp++ == '\0') {
  64. debug("found %x, %x, %lx\n",
  65. name->on_type, name->on_desc, name->on_valu);
  66. return name;
  67. }
  68. symindex = sym->sy_next;
  69. }
  70. /* Not found. */
  71. debug("not found\n");
  72. return (struct outname *)0;
  73. }
  74. /*
  75. * Enter a new name in the symbol table. We must copy everything to a
  76. * new entry. `Name' is a private copy, i.e. the pointer to it will not be
  77. * destroyed by allocation. However, the string of which name->on_foff is the
  78. * offset can be destroyed, so we save it first.
  79. */
  80. void entername(struct outname *name, int hashval)
  81. {
  82. ind_t savindex;
  83. ind_t symindex;
  84. ind_t namindex;
  85. struct symbol *sym;
  86. struct outname *newname;
  87. debug("entername %s %d %x %x", modulptr((ind_t)name->on_foff), hashval, name->on_type, name->on_desc);
  88. savindex = savechar(ALLOGCHR, (ind_t)name->on_foff);
  89. symindex = hard_alloc(ALLOSYMB, (long)sizeof(struct symbol));
  90. debug("; %ld\n", symindex);
  91. namindex = hard_alloc(ALLOGLOB, (long)sizeof(struct outname));
  92. if (savindex == BADOFF || symindex == BADOFF || namindex == BADOFF)
  93. fatal("symbol table overflow");
  94. sym = (struct symbol *)address(ALLOSYMB, symindex);
  95. sym->sy_name = namindex;
  96. newname = (struct outname *)address(ALLOGLOB, namindex);
  97. *newname = *name;
  98. newname->on_foff = savindex;
  99. sym->sy_next = hashtable[hashval];
  100. hashtable[hashval] = symindex;
  101. }
  102. /*
  103. * Return the index of `name' in the symbol table in the order in which
  104. * it was entered. We need a REAL index, not a byte offset.
  105. */
  106. unsigned int indexof(struct outname *name)
  107. {
  108. return name - (struct outname *)address(ALLOGLOB, (ind_t)0);
  109. }
  110. /*
  111. * Assign an integer to the string in p.
  112. * 0 <= hash(p) < NHASH, so it can - and will - be used
  113. * as index in a hash table.
  114. */
  115. int hash(char *p)
  116. {
  117. unsigned short h = 0;
  118. int c;
  119. while ((c = *p++)) {
  120. h <<= 2;
  121. h += c;
  122. }
  123. return h % NHASH;
  124. }