sym.c 3.8 KB

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