diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index 0dfda446f97fd19617d51a7fbdc27babaa2843d1..7b95f5c0627ee331632642f1c535b6bbc1142a20 100644 --- a/es2panda/binder/scope.h +++ b/es2panda/binder/scope.h @@ -279,8 +279,19 @@ public: return slotIndex_ != 0; } + void AddLexicalVarName(uint32_t slot, util::StringView name) + { + lexicalVarNames_.emplace(slot, name); + } + + ArenaMap &GetLexicalVarNames() + { + return lexicalVarNames_; + } + protected: - explicit VariableScope(ArenaAllocator *allocator, Scope *parent) : Scope(allocator, parent) {} + explicit VariableScope(ArenaAllocator *allocator, Scope *parent) : Scope(allocator, parent), + lexicalVarNames_(allocator->Adapter()) {} template bool AddVar(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl); @@ -297,6 +308,7 @@ protected: VariableScopeFlags flags_ {}; uint32_t slotIndex_ {}; + ArenaMap lexicalVarNames_; // for debuginfo }; class ParamScope : public Scope { diff --git a/es2panda/binder/variable.cpp b/es2panda/binder/variable.cpp index fb57097d38c93a584423ed6ca4f3937b10ea614a..fecc73e586b120aa9269a43d5364a150a9a463d8 100644 --- a/es2panda/binder/variable.cpp +++ b/es2panda/binder/variable.cpp @@ -47,8 +47,10 @@ void LocalVariable::SetLexical(Scope *scope) } VariableScope *varScope = scope->EnclosingVariableScope(); - - BindLexEnvSlot(varScope->NextSlot()); + uint32_t slot = varScope->NextSlot(); + auto name = Declaration()->Name(); + varScope->AddLexicalVarName(slot, name); // gather lexical variables for debuginfo + BindLexEnvSlot(slot); } void GlobalVariable::SetLexical([[maybe_unused]] Scope *scope) {} diff --git a/es2panda/compiler/core/envScope.cpp b/es2panda/compiler/core/envScope.cpp index 93fa239666a9f0cab650072dbb4b6ac52c1c080e..a6c123937c6f11cf1f0d2398dc394504f5ebffed 100644 --- a/es2panda/compiler/core/envScope.cpp +++ b/es2panda/compiler/core/envScope.cpp @@ -55,7 +55,7 @@ void LoopEnvScope::CopyBindings(PandaGen *pg, binder::VariableScope *scope, bind Initialize(pg, pg->AllocReg()); - pg_->NewLexEnv(scope_->Node(), scope->LexicalSlots()); + pg_->NewLexicalEnv(scope_->Node(), scope->LexicalSlots(), scope_); pg_->StoreAccumulator(scope_->Node(), lexEnv_); ASSERT(scope->NeedLexEnv()); @@ -88,7 +88,7 @@ void LoopEnvScope::CopyPetIterationCtx() lexicals.push_back(lexical); } pg_->PopLexEnv(scope_->Node()); - pg_->NewLexEnv(scope_->Node(), num); + pg_->NewLexicalEnv(scope_->Node(), num, scope_); pg_->StoreAccumulator(scope_->Node(), lexEnv_); for (uint32_t i = 0; i < num; i++) { diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 82b4eea600673eab4ce0d97325643bdaa76b79db..4beb127b32a8fd70850fd07302e81641a158da25 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -149,7 +149,7 @@ void PandaGen::InitializeLexEnv(const ir::AstNode *node, VReg lexEnv) FrontAllocator fa(this); if (topScope_->NeedLexEnv()) { - NewLexEnv(node, topScope_->LexicalSlots()); + NewLexicalEnv(node, topScope_->LexicalSlots(), topScope_); } else { LdLexEnv(node); } @@ -183,6 +183,17 @@ int32_t PandaGen::AddLiteralBuffer(LiteralBuffer *buf) return buf->Index(); } +int32_t PandaGen::AddLexicalVarNamesForDebugInfo(ArenaMap &lexicalVars) +{ + auto *buf = NewLiteralBuffer(); + buf->Add(Allocator()->New(lexicalVars.size())); + for (auto iter : lexicalVars) { + buf->Add(Allocator()->New(iter.second)); + buf->Add(Allocator()->New(iter.first)); + } + return AddLiteralBuffer(buf); +} + void PandaGen::GetFunctionObject(const ir::AstNode *node) { LoadAccFromLexEnv(node, scope_->Find(binder::Binder::MANDATORY_PARAM_FUNC)); @@ -1508,11 +1519,27 @@ void PandaGen::CopyLexEnv(const ir::AstNode *node) */ } +void PandaGen::NewLexicalEnv(const ir::AstNode *node, uint32_t num, binder::VariableScope *scope) +{ + if (IsDebug()) { + int32_t scopeInfoIdx = AddLexicalVarNamesForDebugInfo(scope->GetLexicalVarNames()); + NewLexEnvWithScopeInfo(node, num, scopeInfoIdx); + return; + } + + NewLexEnv(node, num); +} + void PandaGen::NewLexEnv(const ir::AstNode *node, uint32_t num) { sa_.Emit(node, num); } +void PandaGen::NewLexEnvWithScopeInfo(const ir::AstNode *node, uint32_t num, int32_t scopeInfoIdx) +{ + sa_.Emit(node, num, scopeInfoIdx); +} + void PandaGen::LdLexEnv(const ir::AstNode *node) { sa_.Emit(node); diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index 7e82902931a418aa2eed6a7f9cdc03f18d391c8b..8224f5d791ff7f4276ee27210c71289c83addf1c 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -198,6 +198,7 @@ public: LiteralBuffer *NewLiteralBuffer(); int32_t AddLiteralBuffer(LiteralBuffer *buf); + int32_t AddLexicalVarNamesForDebugInfo(ArenaMap &lexicalMap); void InitializeLexEnv(const ir::AstNode *node, VReg lexEnv); void CopyFunctionArguments(const ir::AstNode *node); @@ -351,7 +352,9 @@ public: void LdLexEnv(const ir::AstNode *node); void PopLexEnv(const ir::AstNode *node); void CopyLexEnv(const ir::AstNode *node); + void NewLexicalEnv(const ir::AstNode *node, uint32_t num, binder::VariableScope *scope); void NewLexEnv(const ir::AstNode *node, uint32_t num); + void NewLexEnvWithScopeInfo(const ir::AstNode *node, uint32_t num, int32_t scopeInfoIdx); void LoadLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot); void StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot); void StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot, VReg value);