sym.c 3.6 KB

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