Symbol.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 char16 * const SymbolTypeNames[] = { _u("Function"), _u("Variable"), _u("MemberName"), _u("Formal"), _u("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. // If body and param scopes are not merged then an inner scope slot is used
  82. if (!this->GetIsArguments() && this->scope->GetScopeType() == ScopeType_Parameter && !this->scope->GetCanMergeWithBodyScope())
  83. {
  84. return true;
  85. }
  86. return this->GetHasNonLocalReference() && (ensureSlotAlloc || this->GetIsCommittedToSlot());
  87. }
  88. bool Symbol::GetIsCommittedToSlot() const
  89. {
  90. return isCommittedToSlot || this->scope->GetFunc()->GetCallsEval() || this->scope->GetFunc()->GetChildCallsEval();
  91. }
  92. Js::PropertyId Symbol::EnsureScopeSlot(FuncInfo *funcInfo)
  93. {
  94. if (this->NeedsSlotAlloc(funcInfo) && this->scopeSlot == Js::Constants::NoProperty)
  95. {
  96. this->scopeSlot = this->scope->AddScopeSlot();
  97. }
  98. return this->scopeSlot;
  99. }
  100. void Symbol::SetHasNonLocalReference(bool b, ByteCodeGenerator *byteCodeGenerator)
  101. {
  102. this->hasNonLocalReference = b;
  103. // The symbol's home function will tell us which child function we're currently processing.
  104. // This is the one that captures the symbol, from the declaring function's perspective.
  105. // So based on that information, note either that, (a.) the symbol is committed to the heap from its
  106. // inception, (b.) the symbol must be committed when the capturing function is instantiated.
  107. FuncInfo *funcHome = this->scope->GetFunc();
  108. FuncInfo *funcChild = funcHome->GetCurrentChildFunction();
  109. // If this is not a local property, or not all its references can be tracked, or
  110. // it's not scoped to the function, or we're in debug mode, disable the delayed capture optimization.
  111. if (funcHome->IsGlobalFunction() ||
  112. funcHome->GetCallsEval() ||
  113. funcHome->GetChildCallsEval() ||
  114. funcChild == nullptr ||
  115. this->GetScope() != funcHome->GetBodyScope() ||
  116. byteCodeGenerator->IsInDebugMode() ||
  117. PHASE_OFF(Js::DelayCapturePhase, funcHome->byteCodeFunction))
  118. {
  119. this->SetIsCommittedToSlot();
  120. }
  121. if (this->isCommittedToSlot)
  122. {
  123. return;
  124. }
  125. AnalysisAssert(funcChild);
  126. ParseNode *pnodeChild = funcChild->root;
  127. Assert(pnodeChild && pnodeChild->nop == knopFncDecl);
  128. if (pnodeChild->sxFnc.IsDeclaration())
  129. {
  130. // The capturing function is a declaration but may still be limited to an inner scope.
  131. Scope *scopeChild = funcHome->GetCurrentChildScope();
  132. if (scopeChild == this->scope || scopeChild->GetScopeType() == ScopeType_FunctionBody)
  133. {
  134. // The symbol is captured on entry to the scope in which it's declared.
  135. // (Check the scope type separately so that we get the special parameter list and
  136. // named function expression cases as well.)
  137. this->SetIsCommittedToSlot();
  138. return;
  139. }
  140. }
  141. // There is a chance we can limit the region in which the symbol lives on the heap.
  142. // Note which function captures the symbol.
  143. funcChild->AddCapturedSym(this);
  144. }
  145. void Symbol::SetHasMaybeEscapedUse(ByteCodeGenerator * byteCodeGenerator)
  146. {
  147. Assert(!this->GetIsMember());
  148. if (!hasMaybeEscapedUse)
  149. {
  150. SetHasMaybeEscapedUseInternal(byteCodeGenerator);
  151. }
  152. }
  153. void Symbol::SetHasMaybeEscapedUseInternal(ByteCodeGenerator * byteCodeGenerator)
  154. {
  155. Assert(!hasMaybeEscapedUse);
  156. Assert(!this->GetIsFormal());
  157. hasMaybeEscapedUse = true;
  158. if (PHASE_TESTTRACE(Js::StackFuncPhase, byteCodeGenerator->TopFuncInfo()->byteCodeFunction))
  159. {
  160. Output::Print(_u("HasMaybeEscapedUse: %s\n"), this->GetName().GetBuffer());
  161. Output::Flush();
  162. }
  163. if (this->GetHasFuncAssignment())
  164. {
  165. this->GetScope()->GetFunc()->SetHasMaybeEscapedNestedFunc(
  166. DebugOnly(this->symbolType == STFunction ? _u("MaybeEscapedUseFuncDecl") : _u("MaybeEscapedUse")));
  167. }
  168. }
  169. void Symbol::SetHasFuncAssignment(ByteCodeGenerator * byteCodeGenerator)
  170. {
  171. Assert(!this->GetIsMember());
  172. if (!hasFuncAssignment)
  173. {
  174. SetHasFuncAssignmentInternal(byteCodeGenerator);
  175. }
  176. }
  177. void Symbol::SetHasFuncAssignmentInternal(ByteCodeGenerator * byteCodeGenerator)
  178. {
  179. Assert(!hasFuncAssignment);
  180. hasFuncAssignment = true;
  181. FuncInfo * top = byteCodeGenerator->TopFuncInfo();
  182. if (PHASE_TESTTRACE(Js::StackFuncPhase, top->byteCodeFunction))
  183. {
  184. Output::Print(_u("HasFuncAssignment: %s\n"), this->GetName().GetBuffer());
  185. Output::Flush();
  186. }
  187. if (this->GetHasMaybeEscapedUse() || this->GetScope()->GetIsObject())
  188. {
  189. byteCodeGenerator->TopFuncInfo()->SetHasMaybeEscapedNestedFunc(DebugOnly(
  190. this->GetIsFormal() ? _u("FormalAssignment") :
  191. this->GetScope()->GetIsObject() ? _u("ObjectScopeAssignment") :
  192. _u("MaybeEscapedUse")));
  193. }
  194. }
  195. void Symbol::RestoreHasFuncAssignment()
  196. {
  197. Assert(hasFuncAssignment == (this->symbolType == STFunction));
  198. Assert(this->GetIsFormal() || !this->GetHasMaybeEscapedUse());
  199. hasFuncAssignment = true;
  200. if (PHASE_TESTTRACE1(Js::StackFuncPhase))
  201. {
  202. Output::Print(_u("RestoreHasFuncAssignment: %s\n"), this->GetName().GetBuffer());
  203. Output::Flush();
  204. }
  205. }
  206. Symbol * Symbol::GetFuncScopeVarSym() const
  207. {
  208. if (!this->GetIsBlockVar())
  209. {
  210. return nullptr;
  211. }
  212. FuncInfo * parentFuncInfo = this->GetScope()->GetFunc();
  213. if (parentFuncInfo->GetIsStrictMode())
  214. {
  215. return nullptr;
  216. }
  217. Symbol *fncScopeSym = parentFuncInfo->GetBodyScope()->FindLocalSymbol(this->GetName());
  218. if (fncScopeSym == nullptr && parentFuncInfo->GetParamScope() != nullptr)
  219. {
  220. // We couldn't find the sym in the body scope, try finding it in the parameter scope.
  221. Scope* paramScope = parentFuncInfo->GetParamScope();
  222. fncScopeSym = paramScope->FindLocalSymbol(this->GetName());
  223. if (fncScopeSym == nullptr)
  224. {
  225. FuncInfo* parentParentFuncInfo = paramScope->GetEnclosingScope()->GetFunc();
  226. if (parentParentFuncInfo->root->sxFnc.IsAsync())
  227. {
  228. // In the case of async functions the func-scope var sym might have
  229. // come from the parent function parameter scope due to the syntax
  230. // desugaring implementation of async functions.
  231. fncScopeSym = parentParentFuncInfo->GetBodyScope()->FindLocalSymbol(this->GetName());
  232. if (fncScopeSym == nullptr)
  233. {
  234. fncScopeSym = parentParentFuncInfo->GetParamScope()->FindLocalSymbol(this->GetName());
  235. }
  236. }
  237. }
  238. }
  239. Assert(fncScopeSym);
  240. // Parser should have added a fake var decl node for block scoped functions in non-strict mode
  241. // IsBlockVar() indicates a user let declared variable at function scope which
  242. // shadows the function's var binding, thus only emit the var binding init if
  243. // we do not have a block var symbol.
  244. if (!fncScopeSym || fncScopeSym->GetIsBlockVar())
  245. {
  246. return nullptr;
  247. }
  248. return fncScopeSym;
  249. }
  250. #if DBG_DUMP
  251. const char16 * Symbol::GetSymbolTypeName()
  252. {
  253. return SymbolTypeNames[symbolType];
  254. }
  255. #endif