Symbol.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "RuntimeByteCodePch.h"
  6. #if DBG_DUMP
  7. static const wchar_t * const SymbolTypeNames[] = { L"Function", L"Variable", L"MemberName", L"Formal", L"Unknown" };
  8. #endif
  9. bool Symbol::GetIsArguments() const
  10. {
  11. return decl != nullptr && (decl->grfpn & PNodeFlags::fpnArguments);
  12. }
  13. Js::PropertyId Symbol::EnsurePosition(ByteCodeGenerator* byteCodeGenerator)
  14. {
  15. // Guarantee that a symbol's name has a property ID mapping.
  16. if (this->position == Js::Constants::NoProperty)
  17. {
  18. this->position = this->EnsurePositionNoCheck(byteCodeGenerator->TopFuncInfo());
  19. }
  20. return this->position;
  21. }
  22. Js::PropertyId Symbol::EnsurePosition(FuncInfo *funcInfo)
  23. {
  24. // Guarantee that a symbol's name has a property ID mapping.
  25. if (this->position == Js::Constants::NoProperty)
  26. {
  27. this->position = this->EnsurePositionNoCheck(funcInfo);
  28. }
  29. return this->position;
  30. }
  31. Js::PropertyId Symbol::EnsurePositionNoCheck(FuncInfo *funcInfo)
  32. {
  33. return funcInfo->byteCodeFunction->GetOrAddPropertyIdTracked(this->GetName());
  34. }
  35. void Symbol::SaveToPropIdArray(Symbol *sym, Js::PropertyIdArray *propIds, ByteCodeGenerator *byteCodeGenerator, Js::PropertyId *pFirstSlot /* = null */)
  36. {
  37. if (sym)
  38. {
  39. Js::PropertyId slot = sym->scopeSlot;
  40. if (slot != Js::Constants::NoProperty)
  41. {
  42. Assert((uint32)slot < propIds->count);
  43. propIds->elements[slot] = sym->EnsurePosition(byteCodeGenerator);
  44. if (pFirstSlot && !sym->GetIsArguments())
  45. {
  46. if (*pFirstSlot == Js::Constants::NoProperty ||
  47. *pFirstSlot > slot)
  48. {
  49. *pFirstSlot = slot;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. bool Symbol::NeedsSlotAlloc(FuncInfo *funcInfo)
  56. {
  57. return IsInSlot(funcInfo, true);
  58. }
  59. bool Symbol::IsInSlot(FuncInfo *funcInfo, bool ensureSlotAlloc)
  60. {
  61. if (this->GetIsGlobal())
  62. {
  63. return false;
  64. }
  65. if (funcInfo->GetHasHeapArguments() && this->GetIsFormal() && ByteCodeGenerator::NeedScopeObjectForArguments(funcInfo, funcInfo->root))
  66. {
  67. // Rest is a special case - it will be in a register.
  68. if (funcInfo->root->sxFnc.pnodeRest != this->decl)
  69. {
  70. return true;
  71. }
  72. }
  73. if (this->GetIsGlobalCatch())
  74. {
  75. return true;
  76. }
  77. if (this->scope->GetCapturesAll())
  78. {
  79. return true;
  80. }
  81. return this->GetHasNonLocalReference() && (ensureSlotAlloc || this->GetIsCommittedToSlot());
  82. }
  83. bool Symbol::GetIsCommittedToSlot() const
  84. {
  85. return isCommittedToSlot || this->scope->GetFunc()->GetCallsEval() || this->scope->GetFunc()->GetChildCallsEval();
  86. }
  87. Js::PropertyId Symbol::EnsureScopeSlot(FuncInfo *funcInfo)
  88. {
  89. if (this->NeedsSlotAlloc(funcInfo) && this->scopeSlot == Js::Constants::NoProperty)
  90. {
  91. this->scopeSlot = this->scope->AddScopeSlot();
  92. }
  93. return this->scopeSlot;
  94. }
  95. void Symbol::SetHasNonLocalReference(bool b, ByteCodeGenerator *byteCodeGenerator)
  96. {
  97. this->hasNonLocalReference = b;
  98. // The symbol's home function will tell us which child function we're currently processing.
  99. // This is the one that captures the symbol, from the declaring function's perspective.
  100. // So based on that information, note either that, (a.) the symbol is committed to the heap from its
  101. // inception, (b.) the symbol must be committed when the capturing function is instantiated.
  102. FuncInfo *funcHome = this->scope->GetFunc();
  103. FuncInfo *funcChild = funcHome->GetCurrentChildFunction();
  104. // If this is not a local property, or not all its references can be tracked, or
  105. // it's not scoped to the function, or we're in debug mode, disable the delayed capture optimization.
  106. if (funcHome->IsGlobalFunction() ||
  107. funcHome->GetCallsEval() ||
  108. funcHome->GetChildCallsEval() ||
  109. funcChild == nullptr ||
  110. this->GetScope() != funcHome->GetBodyScope() ||
  111. byteCodeGenerator->IsInDebugMode() ||
  112. PHASE_OFF(Js::DelayCapturePhase, funcHome->byteCodeFunction))
  113. {
  114. this->SetIsCommittedToSlot();
  115. }
  116. if (this->isCommittedToSlot)
  117. {
  118. return;
  119. }
  120. AnalysisAssert(funcChild);
  121. ParseNode *pnodeChild = funcChild->root;
  122. Assert(pnodeChild && pnodeChild->nop == knopFncDecl);
  123. if (pnodeChild->sxFnc.IsDeclaration())
  124. {
  125. // The capturing function is a declaration but may still be limited to an inner scope.
  126. Scope *scopeChild = funcHome->GetCurrentChildScope();
  127. if (scopeChild == this->scope || scopeChild->GetScopeType() == ScopeType_FunctionBody)
  128. {
  129. // The symbol is captured on entry to the scope in which it's declared.
  130. // (Check the scope type separately so that we get the special parameter list and
  131. // named function expression cases as well.)
  132. this->SetIsCommittedToSlot();
  133. return;
  134. }
  135. }
  136. // There is a chance we can limit the region in which the symbol lives on the heap.
  137. // Note which function captures the symbol.
  138. funcChild->AddCapturedSym(this);
  139. }
  140. void Symbol::SetHasMaybeEscapedUse(ByteCodeGenerator * byteCodeGenerator)
  141. {
  142. Assert(!this->GetIsMember());
  143. if (!hasMaybeEscapedUse)
  144. {
  145. SetHasMaybeEscapedUseInternal(byteCodeGenerator);
  146. }
  147. }
  148. void Symbol::SetHasMaybeEscapedUseInternal(ByteCodeGenerator * byteCodeGenerator)
  149. {
  150. Assert(!hasMaybeEscapedUse);
  151. Assert(!this->GetIsFormal());
  152. hasMaybeEscapedUse = true;
  153. if (PHASE_TESTTRACE(Js::StackFuncPhase, byteCodeGenerator->TopFuncInfo()->byteCodeFunction))
  154. {
  155. Output::Print(L"HasMaybeEscapedUse: %s\n", this->GetName().GetBuffer());
  156. Output::Flush();
  157. }
  158. if (this->GetHasFuncAssignment())
  159. {
  160. this->GetScope()->GetFunc()->SetHasMaybeEscapedNestedFunc(
  161. DebugOnly(this->symbolType == STFunction ? L"MaybeEscapedUseFuncDecl" : L"MaybeEscapedUse"));
  162. }
  163. }
  164. void Symbol::SetHasFuncAssignment(ByteCodeGenerator * byteCodeGenerator)
  165. {
  166. Assert(!this->GetIsMember());
  167. if (!hasFuncAssignment)
  168. {
  169. SetHasFuncAssignmentInternal(byteCodeGenerator);
  170. }
  171. }
  172. void Symbol::SetHasFuncAssignmentInternal(ByteCodeGenerator * byteCodeGenerator)
  173. {
  174. Assert(!hasFuncAssignment);
  175. hasFuncAssignment = true;
  176. FuncInfo * top = byteCodeGenerator->TopFuncInfo();
  177. if (PHASE_TESTTRACE(Js::StackFuncPhase, top->byteCodeFunction))
  178. {
  179. Output::Print(L"HasFuncAssignment: %s\n", this->GetName().GetBuffer());
  180. Output::Flush();
  181. }
  182. if (this->GetHasMaybeEscapedUse() || this->GetScope()->GetIsObject())
  183. {
  184. byteCodeGenerator->TopFuncInfo()->SetHasMaybeEscapedNestedFunc(DebugOnly(
  185. this->GetIsFormal() ? L"FormalAssignment" :
  186. this->GetScope()->GetIsObject() ? L"ObjectScopeAssignment" :
  187. L"MaybeEscapedUse"));
  188. }
  189. }
  190. void Symbol::RestoreHasFuncAssignment()
  191. {
  192. Assert(hasFuncAssignment == (this->symbolType == STFunction));
  193. Assert(this->GetIsFormal() || !this->GetHasMaybeEscapedUse());
  194. hasFuncAssignment = true;
  195. if (PHASE_TESTTRACE1(Js::StackFuncPhase))
  196. {
  197. Output::Print(L"RestoreHasFuncAssignment: %s\n", this->GetName().GetBuffer());
  198. Output::Flush();
  199. }
  200. }
  201. Symbol * Symbol::GetFuncScopeVarSym() const
  202. {
  203. if (!this->GetIsBlockVar())
  204. {
  205. return nullptr;
  206. }
  207. FuncInfo * parentFuncInfo = this->GetScope()->GetFunc();
  208. if (parentFuncInfo->GetIsStrictMode())
  209. {
  210. return nullptr;
  211. }
  212. Symbol *fncScopeSym = parentFuncInfo->GetBodyScope()->FindLocalSymbol(this->GetName());
  213. if (fncScopeSym == nullptr && parentFuncInfo->GetParamScope() != nullptr)
  214. {
  215. // We couldn't find the sym in the body scope, try finding it in the parameter scope.
  216. fncScopeSym = parentFuncInfo->GetParamScope()->FindLocalSymbol(this->GetName());
  217. }
  218. Assert(fncScopeSym);
  219. // Parser should have added a fake var decl node for block scoped functions in non-strict mode
  220. // IsBlockVar() indicates a user let declared variable at function scope which
  221. // shadows the function's var binding, thus only emit the var binding init if
  222. // we do not have a block var symbol.
  223. if (!fncScopeSym || fncScopeSym->GetIsBlockVar())
  224. {
  225. return nullptr;
  226. }
  227. return fncScopeSym;
  228. }
  229. #if DBG_DUMP
  230. const wchar_t * Symbol::GetSymbolTypeName()
  231. {
  232. return SymbolTypeNames[symbolType];
  233. }
  234. #endif