ByteCodeGenerator.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #if defined(_M_ARM32_OR_ARM64) || defined(_M_X64)
  6. const int32 AstBytecodeRatioEstimate = 4;
  7. #else
  8. const int32 AstBytecodeRatioEstimate = 5;
  9. #endif
  10. enum DynamicLoadKind
  11. {
  12. Invalid,
  13. Local,
  14. Env,
  15. LocalWith,
  16. EnvWith
  17. };
  18. struct DynamicLoadRecord
  19. {
  20. DynamicLoadRecord();
  21. DynamicLoadKind kind;
  22. Js::ByteCodeLabel label;
  23. union
  24. {
  25. uint32 index;
  26. Js::RegSlot instance;
  27. };
  28. };
  29. struct JumpCleanupInfo
  30. {
  31. // Used for loop nodes
  32. ParseNode* loopNode;
  33. uint loopId;
  34. // Used for try and finally nodes
  35. Js::OpCode tryOp;
  36. Js::ByteCodeLabel label;
  37. Js::RegSlot regSlot1;
  38. Js::RegSlot regSlot2;
  39. };
  40. class ByteCodeGenerator
  41. {
  42. private:
  43. Js::ScriptContext* scriptContext;
  44. ArenaAllocator *alloc;
  45. uint32 flags;
  46. SList<FuncInfo*> *funcInfoStack;
  47. ParseNodeBlock *currentBlock;
  48. ParseNode *currentTopStatement;
  49. Scope *currentScope;
  50. Scope *globalScope; // the global members will be in this scope
  51. Js::ScopeInfo* parentScopeInfo;
  52. Js::ByteCodeWriter m_writer;
  53. // pointer to the root function wrapper that will be invoked by the caller
  54. Js::ParseableFunctionInfo * pRootFunc;
  55. SList<FuncInfo*> * funcInfosToFinalize;
  56. using JumpCleanupList = DList<JumpCleanupInfo, ArenaAllocator>;
  57. JumpCleanupList* jumpCleanupList;
  58. int32 maxAstSize;
  59. uint16 envDepth;
  60. uint sourceIndex;
  61. uint dynamicScopeCount;
  62. uint loopDepth;
  63. uint16 m_callSiteId;
  64. uint16 m_callApplyCallSiteCount;
  65. bool isBinding;
  66. bool trackEnvDepth;
  67. bool funcEscapes;
  68. bool inPrologue;
  69. bool inDestructuredPattern;
  70. Parser* parser; // currently active parser (used for AST transformation)
  71. Js::Utf8SourceInfo *m_utf8SourceInfo;
  72. // The stack walker won't be able to find the current function being defer parse, pass in
  73. // The address so we can patch it up if it is a stack function and we need to box it.
  74. Js::ScriptFunction ** functionRef;
  75. public:
  76. // This points to the current function body which can be reused when parsing a subtree (called due to deferred parsing logic).
  77. Js::FunctionBody * pCurrentFunction;
  78. bool InDestructuredPattern() const { return inDestructuredPattern; }
  79. void SetInDestructuredPattern(bool in) { inDestructuredPattern = in; }
  80. bool InPrologue() const { return inPrologue; }
  81. void SetInPrologue(bool val) { inPrologue = val; }
  82. Parser* GetParser() { return parser; }
  83. Js::ParseableFunctionInfo * GetRootFunc(){return pRootFunc;}
  84. void SetRootFuncInfo(FuncInfo* funcInfo);
  85. // Treat the return value register like a constant register so that the byte code writer maps it to the bottom
  86. // of the register range.
  87. static const Js::RegSlot ReturnRegister = REGSLOT_TO_CONSTREG(Js::FunctionBody::ReturnValueRegSlot);
  88. static const Js::RegSlot RootObjectRegister = REGSLOT_TO_CONSTREG(Js::FunctionBody::RootObjectRegSlot);
  89. static const unsigned int DefaultArraySize = 0; // This __must__ be '0' so that "(new Array()).length == 0"
  90. static const unsigned int MinArgumentsForCallOptimization = 16;
  91. bool forceNoNative;
  92. // A flag that when set will force bytecode opcodes to be emitted in strict mode when avaliable.
  93. // This flag is set outside of emit calls under the condition that the bytecode being emitted
  94. // corresponds to computed property names within classes. This fixes a bug where computed property
  95. // names would not enforce strict mode when inside a class even though the spec requires that
  96. // all code within a class must be strict.
  97. bool forceStrictModeForClassComputedPropertyName = false;
  98. ByteCodeGenerator(Js::ScriptContext* scriptContext, Js::ScopeInfo* parentScopeInfo);
  99. #if DBG_DUMP
  100. bool Trace() const
  101. {
  102. return Js::Configuration::Global.flags.Trace.IsEnabled(Js::ByteCodePhase);
  103. }
  104. #else
  105. bool Trace() const
  106. {
  107. return false;
  108. }
  109. #endif
  110. Js::ScriptContext* GetScriptContext() { return scriptContext; }
  111. Scope *GetCurrentScope() const { return currentScope; }
  112. void SetCurrentBlock(ParseNodeBlock *pnode) { currentBlock = pnode; }
  113. ParseNodeBlock *GetCurrentBlock() const { return currentBlock; }
  114. void SetCurrentTopStatement(ParseNode *pnode) { currentTopStatement = pnode; }
  115. ParseNode *GetCurrentTopStatement() const { return currentTopStatement; }
  116. Js::ModuleID GetModuleID() const
  117. {
  118. return m_utf8SourceInfo->GetSrcInfo()->moduleID;
  119. }
  120. void SetFlags(uint32 grfscr)
  121. {
  122. flags = grfscr;
  123. }
  124. uint32 GetFlags(void)
  125. {
  126. return flags;
  127. }
  128. bool IsConsoleScopeEval(void)
  129. {
  130. return (flags & fscrConsoleScopeEval) == fscrConsoleScopeEval;
  131. }
  132. bool IsModuleCode()
  133. {
  134. return (flags & fscrIsModuleCode) == fscrIsModuleCode;
  135. }
  136. bool IsBinding() const {
  137. return isBinding;
  138. }
  139. Js::ByteCodeWriter *Writer() {
  140. return &m_writer;
  141. }
  142. ArenaAllocator *GetAllocator() {
  143. return alloc;
  144. }
  145. bool IsEvalWithNoParentScopeInfo()
  146. {
  147. return (flags & fscrEvalCode) && !HasParentScopeInfo();
  148. }
  149. Js::ProfileId GetNextCallSiteId(Js::OpCode op)
  150. {
  151. if (m_writer.ShouldIncrementCallSiteId(op))
  152. {
  153. if (m_callSiteId != Js::Constants::NoProfileId)
  154. {
  155. return m_callSiteId++;
  156. }
  157. }
  158. return m_callSiteId;
  159. }
  160. Js::ProfileId GetCurrentCallSiteId() { return m_callSiteId; }
  161. Js::ProfileId GetNextCallApplyCallSiteId(Js::OpCode op)
  162. {
  163. if (m_writer.ShouldIncrementCallSiteId(op))
  164. {
  165. if (m_callApplyCallSiteCount != Js::Constants::NoProfileId)
  166. {
  167. return m_callApplyCallSiteCount++;
  168. }
  169. }
  170. return m_callApplyCallSiteCount;
  171. }
  172. Js::RegSlot NextVarRegister();
  173. Js::RegSlot NextConstRegister();
  174. FuncInfo *TopFuncInfo() const;
  175. void EnterLoop();
  176. void ExitLoop() { loopDepth--; }
  177. BOOL IsInLoop() const { return loopDepth > 0; }
  178. // TODO: per-function register assignment for env and global symbols
  179. void AssignRegister(Symbol *sym);
  180. void AddTargetStmt(ParseNodeStmt *pnodeStmt);
  181. Js::RegSlot AssignNullConstRegister();
  182. Js::RegSlot AssignUndefinedConstRegister();
  183. Js::RegSlot AssignTrueConstRegister();
  184. Js::RegSlot AssignFalseConstRegister();
  185. Js::RegSlot AssignThisConstRegister();
  186. void SetNeedEnvRegister();
  187. void AssignFrameObjRegister();
  188. void AssignFrameSlotsRegister();
  189. void AssignParamSlotsRegister();
  190. void AssignFrameDisplayRegister();
  191. void ProcessCapturedSym(Symbol *sym);
  192. void ProcessScopeWithCapturedSym(Scope *scope);
  193. void InitScopeSlotArray(FuncInfo * funcInfo);
  194. void FinalizeRegisters(FuncInfo * funcInfo, Js::FunctionBody * byteCodeFunction);
  195. void SetClosureRegisters(FuncInfo * funcInfo, Js::FunctionBody * byteCodeFunction);
  196. void SetHasTry(bool has);
  197. void SetHasFinally(bool has);
  198. void SetNumberOfInArgs(Js::ArgSlot argCount);
  199. Js::RegSlot EnregisterConstant(unsigned int constant);
  200. Js::RegSlot EnregisterStringConstant(IdentPtr pid);
  201. Js::RegSlot EnregisterDoubleConstant(double d);
  202. Js::RegSlot EnregisterBigIntConstant(ParseNodePtr pid);
  203. Js::RegSlot EnregisterStringTemplateCallsiteConstant(ParseNode* pnode);
  204. static Js::JavascriptArray* BuildArrayFromStringList(ParseNode* stringNodeList, uint arrayLength, Js::ScriptContext* scriptContext);
  205. bool HasParentScopeInfo() const
  206. {
  207. return this->parentScopeInfo != nullptr;
  208. }
  209. Js::RegSlot EmitLdObjProto(Js::OpCode op, Js::RegSlot objReg, FuncInfo *funcInfo)
  210. {
  211. // LdHomeObjProto protoReg, objReg
  212. // LdFuncObjProto protoReg, objReg
  213. Js::RegSlot protoReg = funcInfo->AcquireTmpRegister();
  214. this->Writer()->Reg2(op, protoReg, objReg);
  215. funcInfo->ReleaseTmpRegister(protoReg);
  216. return protoReg;
  217. }
  218. void RestoreScopeInfo(Js::ScopeInfo *scopeInfo, FuncInfo * func);
  219. void RestoreOneScope(Js::ScopeInfo * scopeInfo, FuncInfo * func);
  220. FuncInfo *StartBindGlobalStatements(ParseNodeProg *pnode);
  221. void AssignPropertyId(Symbol *sym, Js::ParseableFunctionInfo* functionInfo);
  222. void AssignPropertyId(IdentPtr pid);
  223. void ProcessCapturedSyms(ParseNode *pnodeFnc);
  224. void RecordAllIntConstants(FuncInfo * funcInfo);
  225. void RecordAllStrConstants(FuncInfo * funcInfo);
  226. void RecordAllBigIntConstants(FuncInfo * funcInfo);
  227. void RecordAllStringTemplateCallsiteConstants(FuncInfo* funcInfo);
  228. // For now, this just assigns field ids for the current script.
  229. // Later, we will combine this information with the global field ID map.
  230. // This temporary code will not work if a global member is accessed both with and without a LHS.
  231. void AssignPropertyIds(Js::ParseableFunctionInfo* functionInfo);
  232. void MapCacheIdsToPropertyIds(FuncInfo *funcInfo);
  233. void MapReferencedPropertyIds(FuncInfo *funcInfo);
  234. #if ENABLE_NATIVE_CODEGEN
  235. void MapCallSiteToCallApplyCallSiteMap(FuncInfo * funcInfo);
  236. #endif
  237. FuncInfo *StartBindFunction(const char16 *name, uint nameLength, uint shortNameOffset, bool* pfuncExprWithName, ParseNodeFnc *pnodeFnc, Js::ParseableFunctionInfo * reuseNestedFunc);
  238. void EndBindFunction(bool funcExprWithName);
  239. void StartBindCatch(ParseNode *pnode);
  240. // Block scopes related functions
  241. template<class Fn> void IterateBlockScopedVariables(ParseNodeBlock *pnodeBlock, Fn fn);
  242. void InitBlockScopedContent(ParseNodeBlock *pnodeBlock, Js::DebuggerScope *debuggerScope, FuncInfo *funcInfo);
  243. Js::DebuggerScope* RecordStartScopeObject(ParseNode *pnodeBlock, Js::DiagExtraScopesType scopeType, Js::RegSlot scopeLocation = Js::Constants::NoRegister, int* index = nullptr);
  244. void RecordEndScopeObject(ParseNode *pnodeBlock);
  245. void EndBindCatch();
  246. void StartEmitFunction(ParseNodeFnc *pnodeFnc);
  247. void EndEmitFunction(ParseNodeFnc *pnodeFnc);
  248. void StartEmitBlock(ParseNodeBlock *pnodeBlock);
  249. void EndEmitBlock(ParseNodeBlock *pnodeBlock);
  250. void StartEmitCatch(ParseNodeCatch *pnodeCatch);
  251. void EndEmitCatch(ParseNodeCatch *pnodeCatch);
  252. void StartEmitWith(ParseNode *pnodeWith);
  253. void EndEmitWith(ParseNode *pnodeWith);
  254. void EnsureFncScopeSlots(ParseNode *pnode, FuncInfo *funcInfo);
  255. void EnsureLetConstScopeSlots(ParseNodeBlock *pnodeBlock, FuncInfo *funcInfo);
  256. bool EnsureSymbolModuleSlots(Symbol* sym, FuncInfo* funcInfo);
  257. void EmitAssignmentToDefaultModuleExport(ParseNode* pnode, FuncInfo* funcInfo);
  258. void EmitModuleExportAccess(Symbol* sym, Js::OpCode opcode, Js::RegSlot location, FuncInfo* funcInfo);
  259. void PushScope(Scope *innerScope);
  260. void PopScope();
  261. void PushBlock(ParseNodeBlock *pnode);
  262. void PopBlock();
  263. void PushFuncInfo(char16 const * location, FuncInfo* funcInfo);
  264. void PopFuncInfo(char16 const * location);
  265. Js::RegSlot PrependLocalScopes(Js::RegSlot evalEnv, Js::RegSlot tempLoc, FuncInfo *funcInfo);
  266. Symbol *FindSymbol(Symbol **symRef, IdentPtr pid, bool forReference = false);
  267. Symbol *AddSymbolToScope(Scope *scope, const char16 *key, int keyLength, ParseNode *varDecl, SymbolType symbolType);
  268. Symbol *AddSymbolToFunctionScope(const char16 *key, int keyLength, ParseNode *varDecl, SymbolType symbolType);
  269. void FuncEscapes(Scope *scope);
  270. void EmitTopLevelStatement(ParseNode *stmt, FuncInfo *funcInfo, BOOL fReturnValue);
  271. void EmitInvertedLoop(ParseNodeStmt* outerLoop,ParseNodeFor* invertedLoop,FuncInfo* funcInfo);
  272. void DefineFunctions(FuncInfo *funcInfoParent);
  273. Js::RegSlot DefineOneFunction(ParseNodeFnc *pnodeFnc, FuncInfo *funcInfoParent, bool generateAssignment=true, Js::RegSlot regEnv = Js::Constants::NoRegister, Js::RegSlot frameDisplayTemp = Js::Constants::NoRegister);
  274. void DefineCachedFunctions(FuncInfo *funcInfoParent);
  275. void DefineUncachedFunctions(FuncInfo *funcInfoParent);
  276. void DefineUserVars(FuncInfo *funcInfo);
  277. void InitBlockScopedNonTemps(ParseNode *pnode, FuncInfo *funcInfo);
  278. // temporarily load all constants and special registers in a single block
  279. void LoadAllConstants(FuncInfo *funcInfo);
  280. void LoadHeapArguments(FuncInfo *funcInfo);
  281. void LoadUncachedHeapArguments(FuncInfo *funcInfo);
  282. void LoadCachedHeapArguments(FuncInfo *funcInfo);
  283. void LoadThisObject(FuncInfo *funcInfo, bool thisLoadedFromParams = false);
  284. void EmitThis(FuncInfo *funcInfo, Js::RegSlot lhsLocation, Js::RegSlot fromRegister);
  285. void LoadNewTargetObject(FuncInfo *funcInfo);
  286. void LoadImportMetaObject(FuncInfo* funcInfo);
  287. void LoadSuperObject(FuncInfo *funcInfo);
  288. void LoadSuperConstructorObject(FuncInfo *funcInfo);
  289. void EmitSuperCall(FuncInfo* funcInfo, ParseNodeSuperCall * pnodeSuperCall, BOOL fReturnValue, BOOL fEvaluateComponents);
  290. void EmitClassConstructorEndCode(FuncInfo *funcInfo);
  291. // TODO: home the 'this' argument
  292. void EmitLoadFormalIntoRegister(ParseNode *pnodeFormal, Js::RegSlot pos, FuncInfo *funcInfo);
  293. void HomeArguments(FuncInfo *funcInfo);
  294. void EnsureNoRedeclarations(ParseNodeBlock *pnodeBlock, FuncInfo *funcInfo);
  295. void DefineLabels(FuncInfo *funcInfo);
  296. void EmitProgram(ParseNodeProg *pnodeProg);
  297. void EmitScopeList(ParseNode *pnode, ParseNode *breakOnBodyScopeNode = nullptr);
  298. void EmitDefaultArgs(FuncInfo *funcInfo, ParseNodeFnc *pnode);
  299. void EmitOneFunction(ParseNodeFnc *pnodeFnc);
  300. void EmitGlobalFncDeclInit(Js::RegSlot rhsLocation, Js::PropertyId propertyId, FuncInfo * funcInfo);
  301. void EmitLocalPropInit(Js::RegSlot rhsLocation, Symbol *sym, FuncInfo *funcInfo);
  302. void EmitPropStore(Js::RegSlot rhsLocation, Symbol *sym, IdentPtr pid, FuncInfo *funcInfo, bool isLet = false, bool isConst = false, bool isFncDeclVar = false, bool skipUseBeforeDeclarationCheck = false);
  303. void EmitPropLoad(Js::RegSlot lhsLocation, Symbol *sym, IdentPtr pid, FuncInfo *funcInfo, bool skipUseBeforeDeclarationCheck = false);
  304. void EmitPropDelete(Js::RegSlot lhsLocation, Symbol *sym, IdentPtr pid, FuncInfo *funcInfo);
  305. void EmitPropTypeof(Js::RegSlot lhsLocation, Symbol *sym, IdentPtr pid, FuncInfo *funcInfo);
  306. void EmitTypeOfFld(FuncInfo * funcInfo, Js::PropertyId propertyId, Js::RegSlot value, Js::RegSlot instance, Js::OpCode op1, bool reuseLoc = false);
  307. bool ShouldLoadConstThis(FuncInfo* funcInfo);
  308. void EmitPropLoadThis(Js::RegSlot lhsLocation, ParseNodeSpecialName *pnode, FuncInfo *funcInfo, bool chkUndecl);
  309. void EmitPropStoreForSpecialSymbol(Js::RegSlot rhsLocation, Symbol *sym, IdentPtr pid, FuncInfo *funcInfo, bool init);
  310. void EmitLoadInstance(Symbol *sym, IdentPtr pid, Js::RegSlot *pThisLocation, Js::RegSlot *pTargetLocation, FuncInfo *funcInfo);
  311. void EmitGlobalBody(FuncInfo *funcInfo);
  312. void EmitFunctionBody(FuncInfo *funcInfo);
  313. void EmitAsmFunctionBody(FuncInfo *funcInfo);
  314. void EmitScopeObjectInit(FuncInfo *funcInfo);
  315. void EmitPatchableRootProperty(Js::OpCode opcode, Js::RegSlot regSlot, Js::PropertyId propertyId, bool isLoadMethod, bool isStore, FuncInfo *funcInfo);
  316. struct TryScopeRecord;
  317. JsUtil::DoublyLinkedList<TryScopeRecord> tryScopeRecordsList;
  318. void EmitLeaveOpCodesBeforeYield();
  319. void EmitTryBlockHeadersAfterYield();
  320. void InvalidateCachedOuterScopes(FuncInfo *funcInfo);
  321. bool InDynamicScope() const { return dynamicScopeCount != 0; }
  322. Scope * FindScopeForSym(Scope *symScope, Scope *scope, Js::PropertyId *envIndex, FuncInfo *funcInfo) const;
  323. static Js::OpCode GetStFldOpCode(bool isStrictMode, bool isRoot, bool isLetDecl, bool isConstDecl, bool isClassMemberInit)
  324. {
  325. return isClassMemberInit ? Js::OpCode::InitClassMember :
  326. isConstDecl ? (isRoot ? Js::OpCode::InitRootConstFld : Js::OpCode::InitConstFld) :
  327. isLetDecl ? (isRoot ? Js::OpCode::InitRootLetFld : Js::OpCode::InitLetFld) :
  328. isStrictMode ? (isRoot ? Js::OpCode::StRootFldStrict : Js::OpCode::StFldStrict) :
  329. isRoot ? Js::OpCode::StRootFld : Js::OpCode::StFld;
  330. }
  331. static Js::OpCode GetStFldOpCode(FuncInfo* funcInfo, bool isRoot, bool isLetDecl, bool isConstDecl, bool isClassMemberInit, bool forceStrictModeForClassComputedPropertyName = false);
  332. static Js::OpCode GetScopedStFldOpCode(bool isStrictMode, bool isConsoleScope = false)
  333. {
  334. return isStrictMode ?
  335. (isConsoleScope ? Js::OpCode::ConsoleScopedStFldStrict : Js::OpCode::ScopedStFldStrict) :
  336. (isConsoleScope ? Js::OpCode::ConsoleScopedStFld : Js::OpCode::ScopedStFld);
  337. }
  338. static Js::OpCode GetScopedStFldOpCode(FuncInfo* funcInfo, bool isConsoleScopeLetConst = false);
  339. static Js::OpCode GetStElemIOpCode(bool isStrictMode)
  340. {
  341. return isStrictMode ? Js::OpCode::StElemI_A_Strict : Js::OpCode::StElemI_A;
  342. }
  343. static Js::OpCode GetStElemIOpCode(FuncInfo* funcInfo);
  344. bool DoJitLoopBodies(FuncInfo *funcInfo) const;
  345. static void Generate(__in ParseNodeProg *pnode, uint32 grfscr, __in ByteCodeGenerator* byteCodeGenerator, __inout Js::ParseableFunctionInfo ** ppRootFunc, __in uint sourceIndex, __in bool forceNoNative, __in Parser* parser, Js::ScriptFunction ** functionRef);
  346. void Begin(
  347. __in ArenaAllocator *alloc,
  348. __in uint32 grfscr,
  349. __in Js::ParseableFunctionInfo* pRootFunc);
  350. void SetCurrentSourceIndex(uint sourceIndex) { this->sourceIndex = sourceIndex; }
  351. uint GetCurrentSourceIndex() { return sourceIndex; }
  352. static bool IsFalse(ParseNode* node);
  353. static bool IsThis(ParseNode* pnode);
  354. static bool IsSuper(ParseNode* pnode);
  355. void StartStatement(ParseNode* node);
  356. void EndStatement(ParseNode* node);
  357. void StartSubexpression(ParseNode* node);
  358. void EndSubexpression(ParseNode* node);
  359. // Debugger methods.
  360. bool IsInDebugMode() const;
  361. bool IsInNonDebugMode() const;
  362. bool ShouldTrackDebuggerMetadata() const;
  363. void TrackRegisterPropertyForDebugger(Js::DebuggerScope *debuggerScope, Symbol *symbol, FuncInfo *funcInfo, Js::DebuggerScopePropertyFlags flags = Js::DebuggerScopePropertyFlags_None, bool isFunctionDeclaration = false);
  364. void TrackActivationObjectPropertyForDebugger(Js::DebuggerScope *debuggerScope, Symbol *symbol, Js::DebuggerScopePropertyFlags flags = Js::DebuggerScopePropertyFlags_None, bool isFunctionDeclaration = false);
  365. void TrackSlotArrayPropertyForDebugger(Js::DebuggerScope *debuggerScope, Symbol* symbol, Js::PropertyId propertyId, Js::DebuggerScopePropertyFlags flags = Js::DebuggerScopePropertyFlags_None, bool isFunctionDeclaration = false);
  366. void TrackFunctionDeclarationPropertyForDebugger(Symbol *functionDeclarationSymbol, FuncInfo *funcInfoParent);
  367. void UpdateDebuggerPropertyInitializationOffset(Js::RegSlot location, Js::PropertyId propertyId, bool shouldConsumeRegister = true);
  368. void PopulateFormalsScope(uint beginOffset, FuncInfo *funcInfo, ParseNodeFnc *pnodeFnc);
  369. void InsertPropertyToDebuggerScope(FuncInfo* funcInfo, Js::DebuggerScope* debuggerScope, Symbol* sym);
  370. FuncInfo *FindEnclosingNonLambda();
  371. static FuncInfo* GetParentFuncInfo(FuncInfo* child);
  372. FuncInfo* GetEnclosingFuncInfo();
  373. bool CanStackNestedFunc(FuncInfo * funcInfo, bool trace = false);
  374. void CheckDeferParseHasMaybeEscapedNestedFunc();
  375. bool NeedObjectAsFunctionScope(FuncInfo * funcInfo, ParseNodeFnc * pnodeFnc) const;
  376. bool HasInterleavingDynamicScope(Symbol * sym) const;
  377. Js::FunctionBody *EnsureFakeGlobalFuncForUndefer(ParseNode *pnode);
  378. Js::FunctionBody *MakeGlobalFunctionBody(ParseNode *pnode);
  379. bool NeedScopeObjectForArguments(FuncInfo *funcInfo, ParseNodeFnc *pnodeFnc) const;
  380. void AddFuncInfoToFinalizationSet(FuncInfo *funcInfo);
  381. void FinalizeFuncInfos();
  382. void CheckFncDeclScopeSlot(ParseNodeFnc *pnodeFnc, FuncInfo *funcInfo);
  383. void EnsureFncDeclScopeSlot(ParseNodeFnc *pnodeFnc, FuncInfo *funcInfo);
  384. Js::OpCode GetStSlotOp(Scope *scope, int envIndex, Js::RegSlot scopeLocation, bool chkBlockVar, FuncInfo *funcInfo);
  385. Js::OpCode GetLdSlotOp(Scope *scope, int envIndex, Js::RegSlot scopeLocation, FuncInfo *funcInfo);
  386. Js::OpCode GetInitFldOp(Scope *scope, Js::RegSlot scopeLocation, FuncInfo *funcInfo, bool letDecl = false);
  387. void PushJumpCleanupForLoop(ParseNode* loopNode, uint loopId)
  388. {
  389. this->jumpCleanupList->Prepend({
  390. loopNode,
  391. loopId,
  392. Js::OpCode::Nop,
  393. 0,
  394. Js::Constants::NoRegister,
  395. Js::Constants::NoRegister
  396. });
  397. }
  398. void PushJumpCleanupForTry(
  399. Js::OpCode tryOp,
  400. Js::ByteCodeLabel label = 0,
  401. Js::RegSlot regSlot1 = Js::Constants::NoRegister,
  402. Js::RegSlot regSlot2 = Js::Constants::NoRegister)
  403. {
  404. this->jumpCleanupList->Prepend({nullptr, 0, tryOp, label, regSlot1, regSlot2});
  405. }
  406. void PopJumpCleanup() { this->jumpCleanupList->RemoveHead(); }
  407. bool HasJumpCleanup() { return !this->jumpCleanupList->Empty(); }
  408. void EmitJumpCleanup(ParseNode* target, FuncInfo* funcInfo);
  409. private:
  410. bool NeedCheckBlockVar(Symbol* sym, Scope* scope, FuncInfo* funcInfo) const;
  411. Js::OpCode ToChkUndeclOp(Js::OpCode op) const;
  412. };
  413. template<class Fn> void ByteCodeGenerator::IterateBlockScopedVariables(ParseNodeBlock *pnodeBlock, Fn fn)
  414. {
  415. Assert(pnodeBlock->nop == knopBlock);
  416. for (auto lexvar = pnodeBlock->pnodeLexVars; lexvar; lexvar = lexvar->AsParseNodeVar()->pnodeNext)
  417. {
  418. fn(lexvar);
  419. }
  420. }
  421. struct ApplyCheck
  422. {
  423. bool matches;
  424. bool insideApplyCall;
  425. bool sawApply;
  426. };