sym.c 3.8 KB

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