SymTable.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. void
  7. SymTable::Init(Func* func)
  8. {
  9. m_func = func;
  10. m_propertyMap = JitAnew(func->m_alloc, PropertyMap, func->m_alloc);
  11. m_propertyEquivBvMap = JitAnew(func->m_alloc, PropertyEquivBvMap, func->m_alloc);
  12. }
  13. ///----------------------------------------------------------------------------
  14. ///
  15. /// SymTable::Add
  16. ///
  17. /// Add newSym to this symbol table.
  18. ///
  19. ///----------------------------------------------------------------------------
  20. void
  21. SymTable::Add(Sym * newSym)
  22. {
  23. int hash;
  24. newSym->m_id += this->m_IDAdjustment;
  25. hash = this->Hash(newSym->m_id);
  26. AssertMsg(newSym->m_next == NULL, "Error inserting a symbol in the SymTable with a non-NULL next ptr.");
  27. newSym->m_next = m_table[hash];
  28. m_table[hash] = newSym;
  29. if (newSym->IsPropertySym())
  30. {
  31. PropertySym * propertySym = newSym->AsPropertySym();
  32. if (propertySym->m_fieldKind != PropertyKindWriteGuard)
  33. {
  34. SymIdPropIdPair pair(propertySym->m_stackSym->m_id, propertySym->m_propertyId);
  35. #if DBG
  36. PropertySym * foundPropertySym;
  37. Assert(!this->m_propertyMap->TryGetValue(pair, &foundPropertySym));
  38. #endif
  39. this->m_propertyMap->Add(pair, propertySym);
  40. }
  41. if (propertySym->m_fieldKind == PropertyKindSlots ||
  42. propertySym->m_fieldKind == PropertyKindData ||
  43. propertySym->m_fieldKind == PropertyKindWriteGuard)
  44. {
  45. BVSparse<JitArenaAllocator> *bvEquivSet;
  46. if (!this->m_propertyEquivBvMap->TryGetValue(propertySym->m_propertyId, &bvEquivSet))
  47. {
  48. bvEquivSet = JitAnew(this->m_func->m_alloc, BVSparse<JitArenaAllocator>, this->m_func->m_alloc);
  49. this->m_propertyEquivBvMap->Add(propertySym->m_propertyId, bvEquivSet);
  50. }
  51. bvEquivSet->Set(propertySym->m_id);
  52. propertySym->m_propertyEquivSet = bvEquivSet;
  53. }
  54. }
  55. m_func->OnAddSym(newSym);
  56. }
  57. ///----------------------------------------------------------------------------
  58. ///
  59. /// SymTable::Find
  60. ///
  61. /// Find the symbol with the given SymID in this table.
  62. /// Returns NULL if it can't find one.
  63. ///
  64. ///----------------------------------------------------------------------------
  65. Sym *
  66. SymTable::Find(SymID id) const
  67. {
  68. int hash;
  69. id += this->m_IDAdjustment;
  70. hash = this->Hash(id);
  71. for (Sym *sym = m_table[hash]; sym != NULL; sym = sym->m_next)
  72. {
  73. if (sym->m_id == id)
  74. {
  75. return sym;
  76. }
  77. }
  78. return NULL;
  79. }
  80. ///----------------------------------------------------------------------------
  81. ///
  82. /// SymTable::FindStackSym
  83. ///
  84. /// Find the stack symbol with the given SymID in this table. If the
  85. /// symbol exists, it needs to be a StackSym.
  86. /// Returns NULL if it can't find one.
  87. ///
  88. ///----------------------------------------------------------------------------
  89. StackSym *
  90. SymTable::FindStackSym(SymID id) const
  91. {
  92. Sym * sym;
  93. sym = this->Find(id);
  94. if (sym)
  95. {
  96. AssertMsg(sym->IsStackSym(), "Looking for StackSym, found something else");
  97. return sym->AsStackSym();
  98. }
  99. return NULL;
  100. }
  101. ///----------------------------------------------------------------------------
  102. ///
  103. /// SymTable::FindPropertySym
  104. ///
  105. /// Find the field symbol with the given SymID in this table. If the
  106. /// symbol exists, it needs to be a PropertySym.
  107. /// Returns NULL if it can't find one.
  108. ///
  109. ///----------------------------------------------------------------------------
  110. PropertySym *
  111. SymTable::FindPropertySym(SymID id) const
  112. {
  113. Sym * sym;
  114. sym = this->Find(id);
  115. if (sym)
  116. {
  117. AssertMsg(sym->IsPropertySym(), "Looking for PropertySym, found something else");
  118. return sym->AsPropertySym();
  119. }
  120. return NULL;
  121. }
  122. ///----------------------------------------------------------------------------
  123. ///
  124. /// SymTable::FindPropertySym
  125. ///
  126. /// Find the stack symbol with the corresponding to given StackSym and
  127. /// propertyId.
  128. /// Returns NULL if it can't find one.
  129. ///
  130. ///----------------------------------------------------------------------------
  131. PropertySym *
  132. SymTable::FindPropertySym(SymID stackSymID, int32 propertyId) const
  133. {
  134. PropertySym * propertySym;
  135. stackSymID += this->m_IDAdjustment;
  136. SymIdPropIdPair pair(stackSymID, propertyId);
  137. if (this->m_propertyMap->TryGetValue(pair, &propertySym))
  138. {
  139. Assert(propertySym->m_propertyId == propertyId);
  140. Assert(propertySym->m_stackSym->m_id == stackSymID);
  141. return propertySym;
  142. }
  143. return NULL;
  144. }
  145. ///----------------------------------------------------------------------------
  146. ///
  147. /// SymTable::Hash
  148. ///
  149. ///----------------------------------------------------------------------------
  150. int
  151. SymTable::Hash(SymID id) const
  152. {
  153. return (id % k_symTableSize);
  154. }
  155. ///----------------------------------------------------------------------------
  156. ///
  157. /// SymTable::SetStartingID
  158. ///
  159. ///----------------------------------------------------------------------------
  160. void
  161. SymTable::SetStartingID(SymID startingID)
  162. {
  163. AssertMsg(m_currentID == 0, "SymTable::SetStarting() can only be called before any symbols are allocated");
  164. m_currentID = startingID;
  165. }
  166. void
  167. SymTable::IncreaseStartingID(SymID IDIncrease)
  168. {
  169. m_currentID += IDIncrease;
  170. }
  171. ///----------------------------------------------------------------------------
  172. ///
  173. /// SymTable::NewID
  174. ///
  175. ///----------------------------------------------------------------------------
  176. SymID
  177. SymTable::NewID()
  178. {
  179. SymID newId = m_currentID++;
  180. AssertMsg(m_currentID > newId, "Too many symbols: m_currentID overflow!");
  181. return newId - m_IDAdjustment;
  182. }
  183. ///----------------------------------------------------------------------------
  184. ///
  185. /// SymTable::GetArgSlotSym
  186. ///
  187. /// Get a StackSym to represent this argument slot.
  188. ///
  189. ///----------------------------------------------------------------------------
  190. StackSym *
  191. SymTable::GetArgSlotSym(Js::ArgSlot argSlotNum)
  192. {
  193. StackSym * argSym;
  194. argSym = StackSym::NewArgSlotSym(argSlotNum, m_func);
  195. argSym->m_offset = (argSlotNum - 1) * MachPtr;
  196. argSym->m_allocated = true;
  197. return argSym;
  198. }
  199. ///----------------------------------------------------------------------------
  200. ///
  201. /// SymTable::GetMaxSymID
  202. ///
  203. /// Returns the largest SymID in the table at this point.
  204. ///
  205. ///----------------------------------------------------------------------------
  206. SymID
  207. SymTable::GetMaxSymID() const
  208. {
  209. return m_currentID - 1;
  210. }
  211. ///----------------------------------------------------------------------------
  212. ///
  213. /// SymTable::ClearStackSymScratch
  214. ///
  215. ///----------------------------------------------------------------------------
  216. void
  217. SymTable::ClearStackSymScratch()
  218. {
  219. FOREACH_SYM_IN_TABLE(sym, this)
  220. {
  221. if (sym->IsStackSym())
  222. {
  223. memset(&(sym->AsStackSym()->scratch), 0, sizeof(sym->AsStackSym()->scratch));
  224. }
  225. } NEXT_SYM_IN_TABLE;
  226. }