symtab.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * (C) Mike van Emmerik
  3. * These could probably be replaced by functions from libg++
  4. */
  5. /* * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  6. * *
  7. * S y m b o l t a b l e F u n c t i o n s *
  8. * *
  9. \* * * * * * * * * * * * * * * * * * * * * * * * * * * */
  10. /* This file implements a symbol table with a symbolic name, a symbol value
  11. (word), and a procedure number. Two tables are maintained, to be able to
  12. look up by name or by value. Pointers are used for the duplicated symbolic
  13. name to save space. Both tables have the same structure.
  14. The hash tables automatically expand when they get 90% full; they are
  15. never compressed. Expanding the tables could take some time, since about
  16. half of the entries have to be moved on average.
  17. Linear probing is used, due to the difficulty of implementing (e.g.)
  18. quadratic probing with a variable table size.
  19. */
  20. #include <cstdio>
  21. #include <cassert>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <unordered_map>
  25. #include "dcc.h"
  26. #include "symtab.h"
  27. #define TABLESIZE 16 /* Number of entries added each expansion */
  28. /* Probably has to be a power of 2 */
  29. #define STRTABSIZE 256 /* Size string table is inc'd by */
  30. #define NIL ((word)-1)
  31. using namespace std;
  32. static char *pStrTab; /* Pointer to the current string table */
  33. static int strTabNext; /* Next free index into pStrTab */
  34. namespace std
  35. {
  36. template<>
  37. struct hash<SYMTABLE> : public unary_function<const SYMTABLE &,size_t>
  38. {
  39. size_t operator()(const SYMTABLE & key) const
  40. {
  41. word h = 0;
  42. h = (word)(key.symOff ^ (key.symOff >> 8));
  43. return h;
  44. }
  45. };
  46. }
  47. static tableType curTableType; /* Which table is current */
  48. struct TABLEINFO_TYPE
  49. {
  50. TABLEINFO_TYPE()
  51. {
  52. symTab=valTab=0;
  53. }
  54. void deleteVal(dword symOff, Function *symProc, boolT bSymToo);
  55. void create(tableType type);
  56. void destroy();
  57. private:
  58. SYMTABLE *symTab; /* Pointer to the symbol hashed table */
  59. SYMTABLE *valTab; /* Pointer to the value hashed table */
  60. word numEntry; /* Number of entries in this table */
  61. word tableSize;/* Size of the table (entries) */
  62. unordered_map<string,SYMTABLE> z;
  63. unordered_map<SYMTABLE,string> z2;
  64. };
  65. TABLEINFO_TYPE tableInfo[NUM_TABLE_TYPES]; /* Array of info about tables */
  66. TABLEINFO_TYPE currentTabInfo;
  67. /* Create a new symbol table. Returns "handle" */
  68. void TABLEINFO_TYPE::create(tableType type)
  69. {
  70. switch(type)
  71. {
  72. case Comment:
  73. numEntry = 0;
  74. tableSize = TABLESIZE;
  75. valTab = new SYMTABLE [TABLESIZE];
  76. symTab = 0;
  77. break;
  78. case Label:
  79. currentTabInfo.numEntry = 0;
  80. currentTabInfo.tableSize = TABLESIZE;
  81. currentTabInfo.symTab = new SYMTABLE [TABLESIZE];
  82. currentTabInfo.valTab = new SYMTABLE [TABLESIZE];
  83. break;
  84. }
  85. }
  86. void createSymTables(void)
  87. {
  88. /* Initilise the comment table */
  89. /* NB - there is no symbol hashed comment table */
  90. currentTabInfo.create(Comment);
  91. tableInfo[Comment] = currentTabInfo;
  92. /* Initialise the label table */
  93. currentTabInfo.create(Label);
  94. tableInfo[Label] = currentTabInfo;
  95. curTableType = Label;
  96. /* Now the string table */
  97. strTabNext = 0;
  98. pStrTab = (char *)allocMem(STRTABSIZE);
  99. curTableType = Label;
  100. }
  101. void selectTable(tableType tt)
  102. {
  103. if (curTableType == tt)
  104. return; /* Nothing to do */
  105. currentTabInfo = tableInfo[tt];
  106. curTableType = tt;
  107. }
  108. void TABLEINFO_TYPE::destroy()
  109. {
  110. delete [] symTab; // The symbol hashed label table
  111. delete [] valTab; // And the value hashed label table
  112. }
  113. void destroySymTables(void)
  114. {
  115. selectTable(Label);
  116. currentTabInfo.destroy();
  117. selectTable(Comment);
  118. currentTabInfo.destroy();
  119. }
  120. /* Using the value, read the symbolic name */
  121. boolT readVal(std::ostringstream &symName, dword symOff, Function * symProc)
  122. {
  123. return false; // no symbolic names for now
  124. }