symtab.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Symbol table manager for Bison,
  2. Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <stdio.h>
  16. #include "system.h"
  17. #include "new.h"
  18. #include "symtab.h"
  19. #include "gram.h"
  20. bucket **symtab;
  21. bucket *firstsymbol;
  22. bucket *lastsymbol;
  23. int
  24. hash(key)
  25. char *key;
  26. {
  27. register char *cp;
  28. register int k;
  29. cp = key;
  30. k = 0;
  31. while (*cp)
  32. k = ((k << 1) ^ (*cp++)) & 0x3fff;
  33. return (k % TABSIZE);
  34. }
  35. char *
  36. copys(s)
  37. char *s;
  38. {
  39. register int i;
  40. register char *cp;
  41. register char *result;
  42. i = 1;
  43. for (cp = s; *cp; cp++)
  44. i++;
  45. result = mallocate((unsigned int)i);
  46. strcpy(result, s);
  47. return (result);
  48. }
  49. void
  50. tabinit()
  51. {
  52. /* register int i; JF unused */
  53. symtab = NEW2(TABSIZE, bucket *);
  54. firstsymbol = NULL;
  55. lastsymbol = NULL;
  56. }
  57. bucket *
  58. getsym(key)
  59. char *key;
  60. {
  61. register int hashval;
  62. register bucket *bp;
  63. register int found;
  64. hashval = hash(key);
  65. bp = symtab[hashval];
  66. found = 0;
  67. while (bp != NULL && found == 0)
  68. {
  69. if (strcmp(key, bp->tag) == 0)
  70. found = 1;
  71. else
  72. bp = bp->link;
  73. }
  74. if (found == 0)
  75. {
  76. nsyms++;
  77. bp = NEW(bucket);
  78. bp->link = symtab[hashval];
  79. bp->next = NULL;
  80. bp->tag = copys(key);
  81. bp->class = SUNKNOWN;
  82. if (firstsymbol == NULL)
  83. {
  84. firstsymbol = bp;
  85. lastsymbol = bp;
  86. }
  87. else
  88. {
  89. lastsymbol->next = bp;
  90. lastsymbol = bp;
  91. }
  92. symtab[hashval] = bp;
  93. }
  94. return (bp);
  95. }
  96. void
  97. free_symtab()
  98. {
  99. register int i;
  100. register bucket *bp,*bptmp;/* JF don't use ptr after free */
  101. for (i = 0; i < TABSIZE; i++)
  102. {
  103. bp = symtab[i];
  104. while (bp)
  105. {
  106. bptmp = bp->link;
  107. #if 0 /* This causes crashes because one string can appear more than once. */
  108. if (bp->type_name)
  109. FREE(bp->type_name);
  110. #endif
  111. FREE(bp);
  112. bp = bptmp;
  113. }
  114. }
  115. FREE(symtab);
  116. }