From 5ebf38eb5f052cc5ae88afc062c6b3334b85c32d Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Mon, 25 Jul 2022 16:40:38 +0800 Subject: [PATCH] Support DebugInfo of Lexical Variables on es2abc Issue:I5IRZR Signed-off-by: ctw-ian Change-Id: I4e813500aacc0bd35fbb86b44fd5fd20fd5075d7 --- es2panda/binder/scope.h | 14 +++++++++++++- es2panda/binder/variable.cpp | 6 ++++-- es2panda/compiler/core/envScope.cpp | 4 ++-- es2panda/compiler/core/pandagen.cpp | 29 ++++++++++++++++++++++++++++- es2panda/compiler/core/pandagen.h | 3 +++ 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index 0dfda446f9..7b95f5c062 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 fb57097d38..fecc73e586 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 93fa239666..a6c123937c 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 82b4eea600..4beb127b32 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 7e82902931..8224f5d791 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); -- Gitee