From 14a574eb4b504ba94fb583a8e17d2c9415b682d9 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Sat, 30 Jul 2022 15:52:39 +0800 Subject: [PATCH 1/2] Fix implementation of ins debuginfo Issue:I5JQ8K Signed-off-by: ctw-ian Change-Id: Ia129650f9df0659bafa0de9486097c56fc6482f5 --- es2panda/compiler/core/emitter.cpp | 8 +++- es2panda/compiler/core/function.cpp | 2 + es2panda/compiler/core/pandagen.h | 14 +++++++ es2panda/compiler/core/regAllocator.h | 23 +++++++++++- es2panda/compiler/core/regScope.cpp | 2 + es2panda/ir/astNodeMapping.h | 3 +- es2panda/ir/inValidNode.h | 49 +++++++++++++++++++++++++ es2panda/lexer/lexer.cpp | 8 +++- es2panda/lexer/token/sourceLocation.cpp | 4 +- 9 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 es2panda/ir/inValidNode.h diff --git a/es2panda/compiler/core/emitter.cpp b/es2panda/compiler/core/emitter.cpp index d807dfc245..dac1ad806a 100644 --- a/es2panda/compiler/core/emitter.cpp +++ b/es2panda/compiler/core/emitter.cpp @@ -201,6 +201,8 @@ static size_t GetIRNodeWholeLength(const IRNode *node) void FunctionEmitter::GenInstructionDebugInfo(const IRNode *ins, panda::pandasm::Ins *pandaIns) { const ir::AstNode *astNode = ins->Node(); + constexpr size_t INVALID_LINE = -1; + constexpr uint32_t INVALID_COL = -1; ASSERT(astNode != nullptr); @@ -211,7 +213,7 @@ void FunctionEmitter::GenInstructionDebugInfo(const IRNode *ins, panda::pandasm: } } - pandaIns->ins_debug.line_number = astNode->Range().start.line; + pandaIns->ins_debug.line_number = astNode->IsInValidNode() ? INVALID_LINE : astNode->Range().start.line; if (pg_->IsDebug()) { size_t insLen = GetIRNodeWholeLength(ins); @@ -222,7 +224,9 @@ void FunctionEmitter::GenInstructionDebugInfo(const IRNode *ins, panda::pandasm: offset_ += insLen; - pandaIns->ins_debug.column_number = astNode->Range().start.index; + lexer::LineIndex lineIndex(SourceCode()); + pandaIns->ins_debug.column_number = astNode->IsInValidNode() ? INVALID_COL : + lineIndex.GetLocation(astNode->Range().start).col - 1; } } diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index 8e1aafacb7..026abcc17d 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -199,8 +199,10 @@ static VReg CompileFunctionOrProgram(PandaGen *pg) void Function::Compile(PandaGen *pg) { VReg lexEnv = CompileFunctionOrProgram(pg); + pg->SetIgnoreLocation(); pg->CopyFunctionArguments(pg->RootNode()); pg->InitializeLexEnv(pg->RootNode(), lexEnv); + pg->ResetIgnoreLocation(); pg->SortCatchTables(); } diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index cf6cc329cd..0ea2cb179d 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -179,6 +179,20 @@ public: return ic_.Size(); } + void SetIgnoreLocation() + { + sa_.SetIgnoreLocation(); + ra_.SetIgnoreLocation(); + rra_.SetIgnoreLocation(); + } + + void ResetIgnoreLocation() + { + sa_.ResetIgnoreLocation(); + ra_.ResetIgnoreLocation(); + rra_.ResetIgnoreLocation(); + } + bool IsDebug() const; uint32_t ParamCount() const; uint32_t FormalParametersCount() const; diff --git a/es2panda/compiler/core/regAllocator.h b/es2panda/compiler/core/regAllocator.h index ec7bfefc63..93ee974e63 100644 --- a/es2panda/compiler/core/regAllocator.h +++ b/es2panda/compiler/core/regAllocator.h @@ -17,6 +17,7 @@ #define ES2PANDA_COMPILER_CORE_REG_ALLOCATOR_H #include +#include #include namespace panda::es2panda::ir { @@ -34,6 +35,21 @@ public: NO_MOVE_SEMANTIC(AllocatorBase); ~AllocatorBase() = default; + void SetIgnoreLocation() + { + ignoreLocation_ = true; + } + + void ResetIgnoreLocation() + { + ignoreLocation_ = false; + } + + bool IgnoreLocation() const + { + return ignoreLocation_; + } + protected: void PushBack(IRNode *ins); ArenaAllocator *Allocator() const; @@ -41,7 +57,11 @@ protected: template T *Alloc(const ir::AstNode *node, Args &&... args) { - return Allocator()->New(node, std::forward(args)...); + ir::AstNode *invalidNode = nullptr; + if (IgnoreLocation()) { + invalidNode = Allocator()->New(ir::AstNodeType::INVALID); + } + return Allocator()->New(IgnoreLocation() ? invalidNode : node, std::forward(args)...); } template @@ -51,6 +71,7 @@ protected: } PandaGen *pg_; + bool ignoreLocation_ {false}; // for instructions that need to be set with invalid debuginfo }; class SimpleAllocator : public AllocatorBase { diff --git a/es2panda/compiler/core/regScope.cpp b/es2panda/compiler/core/regScope.cpp index 588340ed6b..69966b4696 100644 --- a/es2panda/compiler/core/regScope.cpp +++ b/es2panda/compiler/core/regScope.cpp @@ -112,6 +112,7 @@ FunctionRegScope::FunctionRegScope(PandaGen *pg) : RegScope(pg), envScope_(pg->A pg_->debugInfo_.variableDebugInfo.push_back(funcScope); } + pg_->SetIgnoreLocation(); pg_->LoadAccFromArgs(pg_->rootNode_); if (funcScope->IsModuleScope()) { @@ -119,6 +120,7 @@ FunctionRegScope::FunctionRegScope(PandaGen *pg) : RegScope(pg), envScope_(pg->A } Hoisting::Hoist(pg); + pg_->ResetIgnoreLocation(); } FunctionRegScope::~FunctionRegScope() diff --git a/es2panda/ir/astNodeMapping.h b/es2panda/ir/astNodeMapping.h index decf76bb9d..e2d53a34d7 100644 --- a/es2panda/ir/astNodeMapping.h +++ b/es2panda/ir/astNodeMapping.h @@ -138,7 +138,8 @@ _(VARIABLE_DECLARATOR, VariableDeclarator) \ _(WHILE_STATEMENT, WhileStatement) \ _(WITH_STATEMENT, WithStatement) \ - _(YIELD_EXPRESSION, YieldExpression) + _(YIELD_EXPRESSION, YieldExpression) \ + _(INVALID, InValidNode) #define AST_NODE_REINTERPRET_MAPPING(_) \ _(ARRAY_EXPRESSION, ARRAY_PATTERN, ArrayExpression, ArrayPattern) \ diff --git a/es2panda/ir/inValidNode.h b/es2panda/ir/inValidNode.h new file mode 100644 index 0000000000..b039918379 --- /dev/null +++ b/es2panda/ir/inValidNode.h @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ES2PANDA_IR_INVALID_H +#define ES2PANDA_IR_INVALID_H + +#include + +namespace panda::es2panda::ir { + +class InValidNode : public AstNode { +public: + explicit InValidNode(ir::AstNodeType type) : AstNode(type) {} + + ~InValidNode() = default; + + void Iterate([[maybe_unused]] const NodeTraverser &cb) const override + { + } + + void Dump([[maybe_unused]] ir::AstDumper *dumper) const override + { + } + + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override + { + } + + checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override + { + return nullptr; + } +}; + +} // namespace panda::es2panda::ir + +#endif /* ES2PANDA_IR_INVALID_H */ \ No newline at end of file diff --git a/es2panda/lexer/lexer.cpp b/es2panda/lexer/lexer.cpp index 880527188e..bd7b785c80 100644 --- a/es2panda/lexer/lexer.cpp +++ b/es2panda/lexer/lexer.cpp @@ -145,8 +145,14 @@ void Lexer::SkipMultiLineComment() ThrowError("Unterminated multi-line comment"); break; } + case LEX_CHAR_CR: { + if (Iterator().Peek() == LEX_CHAR_LF) { + Iterator().Forward(1); + } + + [[fallthrough]]; + } case LEX_CHAR_LF: - case LEX_CHAR_CR: case LEX_CHAR_LS: case LEX_CHAR_PS: { pos_.nextTokenLine++; diff --git a/es2panda/lexer/token/sourceLocation.cpp b/es2panda/lexer/token/sourceLocation.cpp index 9d1e4ea604..2fb3443837 100644 --- a/es2panda/lexer/token/sourceLocation.cpp +++ b/es2panda/lexer/token/sourceLocation.cpp @@ -79,8 +79,8 @@ SourceLocation LineIndex::GetLocation(SourcePosition pos) noexcept size_t diff = pos.index - entry.lineStart; for (const auto &range : entry.ranges) { - if (diff < range.cnt) { - col += diff; + if (diff < (range.cnt * range.byteSize)) { + col += (diff / range.byteSize) ; break; } -- Gitee From 744326c619f731aba49b29dfb5348f948b596204 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Wed, 3 Aug 2022 11:33:32 +0800 Subject: [PATCH 2/2] Modify implementation of debuginfo Use nullptr to replace InvalidNode and put lineIndex into class Program Issue:I5JQ8K Signed-off-by: ctw-ian Change-Id: I1d4964f44a34d9eb179883b5a04497b313efce93 --- es2panda/compiler/core/emitter.cpp | 12 ++++--- es2panda/compiler/core/emitter.h | 1 + es2panda/compiler/core/function.cpp | 4 +-- es2panda/compiler/core/pandagen.h | 15 +++----- es2panda/compiler/core/regAllocator.h | 28 +++++++-------- es2panda/compiler/core/regScope.cpp | 4 +-- es2panda/ir/astNodeMapping.h | 3 +- es2panda/ir/inValidNode.h | 49 --------------------------- es2panda/lexer/token/sourceLocation.h | 10 ++++-- es2panda/parser/program/program.cpp | 4 ++- es2panda/parser/program/program.h | 8 +++++ 11 files changed, 49 insertions(+), 89 deletions(-) delete mode 100644 es2panda/ir/inValidNode.h diff --git a/es2panda/compiler/core/emitter.cpp b/es2panda/compiler/core/emitter.cpp index dac1ad806a..cb117f420f 100644 --- a/es2panda/compiler/core/emitter.cpp +++ b/es2panda/compiler/core/emitter.cpp @@ -148,6 +148,11 @@ util::StringView FunctionEmitter::SourceCode() const return pg_->Binder()->Program()->SourceCode(); } +lexer::LineIndex &FunctionEmitter::GetLineIndex() const +{ + return const_cast(pg_->Binder()->Program()->GetLineIndex()); +} + static Format MatchFormat(const IRNode *node, const Formats &formats) { std::array regs {}; @@ -213,7 +218,7 @@ void FunctionEmitter::GenInstructionDebugInfo(const IRNode *ins, panda::pandasm: } } - pandaIns->ins_debug.line_number = astNode->IsInValidNode() ? INVALID_LINE : astNode->Range().start.line; + pandaIns->ins_debug.line_number = astNode ? astNode->Range().start.line : INVALID_LINE; if (pg_->IsDebug()) { size_t insLen = GetIRNodeWholeLength(ins); @@ -224,9 +229,8 @@ void FunctionEmitter::GenInstructionDebugInfo(const IRNode *ins, panda::pandasm: offset_ += insLen; - lexer::LineIndex lineIndex(SourceCode()); - pandaIns->ins_debug.column_number = astNode->IsInValidNode() ? INVALID_COL : - lineIndex.GetLocation(astNode->Range().start).col - 1; + pandaIns->ins_debug.column_number = astNode ? + (GetLineIndex().GetLocation(astNode->Range().start).col - 1) : INVALID_COL; } } diff --git a/es2panda/compiler/core/emitter.h b/es2panda/compiler/core/emitter.h index addf4b142b..7054a6d1b1 100644 --- a/es2panda/compiler/core/emitter.h +++ b/es2panda/compiler/core/emitter.h @@ -81,6 +81,7 @@ private: void GenSourceFileDebugInfo(); void GenVariablesDebugInfo(); util::StringView SourceCode() const; + lexer::LineIndex &GetLineIndex() const; void GenLiteralBuffers(); void GenBufferLiterals(const LiteralBuffer *buff); diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index 026abcc17d..3f4a050ea0 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -199,10 +199,10 @@ static VReg CompileFunctionOrProgram(PandaGen *pg) void Function::Compile(PandaGen *pg) { VReg lexEnv = CompileFunctionOrProgram(pg); - pg->SetIgnoreLocation(); + pg->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION); pg->CopyFunctionArguments(pg->RootNode()); pg->InitializeLexEnv(pg->RootNode(), lexEnv); - pg->ResetIgnoreLocation(); + pg->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION); pg->SortCatchTables(); } diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index 0ea2cb179d..842deb1f87 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -179,18 +179,11 @@ public: return ic_.Size(); } - void SetIgnoreLocation() + void SetSourceLocationFlag(lexer::SourceLocationFlag flag) { - sa_.SetIgnoreLocation(); - ra_.SetIgnoreLocation(); - rra_.SetIgnoreLocation(); - } - - void ResetIgnoreLocation() - { - sa_.ResetIgnoreLocation(); - ra_.ResetIgnoreLocation(); - rra_.ResetIgnoreLocation(); + sa_.SetSourceLocationFlag(flag); + ra_.SetSourceLocationFlag(flag); + rra_.SetSourceLocationFlag(flag); } bool IsDebug() const; diff --git a/es2panda/compiler/core/regAllocator.h b/es2panda/compiler/core/regAllocator.h index 93ee974e63..e028d96983 100644 --- a/es2panda/compiler/core/regAllocator.h +++ b/es2panda/compiler/core/regAllocator.h @@ -17,7 +17,7 @@ #define ES2PANDA_COMPILER_CORE_REG_ALLOCATOR_H #include -#include +#include #include namespace panda::es2panda::ir { @@ -30,24 +30,22 @@ class PandaGen; class AllocatorBase { public: - explicit AllocatorBase(PandaGen *pg) : pg_(pg) {}; + explicit AllocatorBase(PandaGen *pg) + : pg_(pg), sourceLocationFlag_(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION) + { + } NO_COPY_SEMANTIC(AllocatorBase); NO_MOVE_SEMANTIC(AllocatorBase); ~AllocatorBase() = default; - void SetIgnoreLocation() - { - ignoreLocation_ = true; - } - - void ResetIgnoreLocation() + void SetSourceLocationFlag(lexer::SourceLocationFlag flag) { - ignoreLocation_ = false; + sourceLocationFlag_ = flag; } - bool IgnoreLocation() const + lexer::SourceLocationFlag GetSourceLocationFlag() const { - return ignoreLocation_; + return sourceLocationFlag_; } protected: @@ -58,10 +56,8 @@ protected: T *Alloc(const ir::AstNode *node, Args &&... args) { ir::AstNode *invalidNode = nullptr; - if (IgnoreLocation()) { - invalidNode = Allocator()->New(ir::AstNodeType::INVALID); - } - return Allocator()->New(IgnoreLocation() ? invalidNode : node, std::forward(args)...); + return Allocator()->New((GetSourceLocationFlag() == lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION) ? + invalidNode : node, std::forward(args)...); } template @@ -71,7 +67,7 @@ protected: } PandaGen *pg_; - bool ignoreLocation_ {false}; // for instructions that need to be set with invalid debuginfo + lexer::SourceLocationFlag sourceLocationFlag_; // for instructions that need to be set with invalid debuginfo }; class SimpleAllocator : public AllocatorBase { diff --git a/es2panda/compiler/core/regScope.cpp b/es2panda/compiler/core/regScope.cpp index 69966b4696..6426709aad 100644 --- a/es2panda/compiler/core/regScope.cpp +++ b/es2panda/compiler/core/regScope.cpp @@ -112,7 +112,7 @@ FunctionRegScope::FunctionRegScope(PandaGen *pg) : RegScope(pg), envScope_(pg->A pg_->debugInfo_.variableDebugInfo.push_back(funcScope); } - pg_->SetIgnoreLocation(); + pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::INVALID_SOURCE_LOCATION); pg_->LoadAccFromArgs(pg_->rootNode_); if (funcScope->IsModuleScope()) { @@ -120,7 +120,7 @@ FunctionRegScope::FunctionRegScope(PandaGen *pg) : RegScope(pg), envScope_(pg->A } Hoisting::Hoist(pg); - pg_->ResetIgnoreLocation(); + pg_->SetSourceLocationFlag(lexer::SourceLocationFlag::VALID_SOURCE_LOCATION); } FunctionRegScope::~FunctionRegScope() diff --git a/es2panda/ir/astNodeMapping.h b/es2panda/ir/astNodeMapping.h index e2d53a34d7..decf76bb9d 100644 --- a/es2panda/ir/astNodeMapping.h +++ b/es2panda/ir/astNodeMapping.h @@ -138,8 +138,7 @@ _(VARIABLE_DECLARATOR, VariableDeclarator) \ _(WHILE_STATEMENT, WhileStatement) \ _(WITH_STATEMENT, WithStatement) \ - _(YIELD_EXPRESSION, YieldExpression) \ - _(INVALID, InValidNode) + _(YIELD_EXPRESSION, YieldExpression) #define AST_NODE_REINTERPRET_MAPPING(_) \ _(ARRAY_EXPRESSION, ARRAY_PATTERN, ArrayExpression, ArrayPattern) \ diff --git a/es2panda/ir/inValidNode.h b/es2panda/ir/inValidNode.h deleted file mode 100644 index b039918379..0000000000 --- a/es2panda/ir/inValidNode.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ES2PANDA_IR_INVALID_H -#define ES2PANDA_IR_INVALID_H - -#include - -namespace panda::es2panda::ir { - -class InValidNode : public AstNode { -public: - explicit InValidNode(ir::AstNodeType type) : AstNode(type) {} - - ~InValidNode() = default; - - void Iterate([[maybe_unused]] const NodeTraverser &cb) const override - { - } - - void Dump([[maybe_unused]] ir::AstDumper *dumper) const override - { - } - - void Compile([[maybe_unused]] compiler::PandaGen *pg) const override - { - } - - checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override - { - return nullptr; - } -}; - -} // namespace panda::es2panda::ir - -#endif /* ES2PANDA_IR_INVALID_H */ \ No newline at end of file diff --git a/es2panda/lexer/token/sourceLocation.h b/es2panda/lexer/token/sourceLocation.h index 7f0fa27a2c..a9c193ed16 100644 --- a/es2panda/lexer/token/sourceLocation.h +++ b/es2panda/lexer/token/sourceLocation.h @@ -25,6 +25,11 @@ namespace panda::es2panda::lexer { +enum class SourceLocationFlag { + VALID_SOURCE_LOCATION, + INVALID_SOURCE_LOCATION +}; + class SourcePosition { public: explicit SourcePosition() noexcept = default; @@ -93,8 +98,9 @@ private: class LineIndex { public: explicit LineIndex(const util::StringView &source) noexcept; - NO_COPY_SEMANTIC(LineIndex); - NO_MOVE_SEMANTIC(LineIndex); + explicit LineIndex() noexcept = default; + DEFAULT_COPY_SEMANTIC(LineIndex); + DEFAULT_MOVE_SEMANTIC(LineIndex); ~LineIndex() = default; SourceLocation GetLocation(SourcePosition pos) noexcept; diff --git a/es2panda/parser/program/program.cpp b/es2panda/parser/program/program.cpp index 42bee03416..128c80dd13 100644 --- a/es2panda/parser/program/program.cpp +++ b/es2panda/parser/program/program.cpp @@ -36,7 +36,8 @@ Program::Program(Program &&other) sourceCode_(other.sourceCode_), sourceFile_(other.sourceFile_), kind_(other.kind_), - extension_(other.extension_) + extension_(other.extension_), + lineIndex_(other.lineIndex_) { other.binder_ = nullptr; other.ast_ = nullptr; @@ -51,6 +52,7 @@ Program &Program::operator=(Program &&other) sourceFile_ = other.sourceFile_; kind_ = other.kind_; extension_ = other.extension_; + lineIndex_ = other.lineIndex_; other.ast_ = nullptr; diff --git a/es2panda/parser/program/program.h b/es2panda/parser/program/program.h index 75f399e879..c32afee7a9 100644 --- a/es2panda/parser/program/program.h +++ b/es2panda/parser/program/program.h @@ -16,6 +16,7 @@ #ifndef ES2PANDA_PARSER_INCLUDE_PROGRAM_H #define ES2PANDA_PARSER_INCLUDE_PROGRAM_H +#include #include #include #include @@ -77,6 +78,11 @@ public: return sourceFile_.View(); } + const lexer::LineIndex &GetLineIndex() const + { + return lineIndex_; + } + ir::BlockStatement *Ast() { return ast_; @@ -96,6 +102,7 @@ public: { sourceCode_ = util::UString(sourceCode, Allocator()); sourceFile_ = util::UString(sourceFile, Allocator()); + lineIndex_ = lexer::LineIndex(SourceCode()); } std::string Dump() const; @@ -109,6 +116,7 @@ private: util::UString sourceFile_ {}; ScriptKind kind_ {}; ScriptExtension extension_ {}; + lexer::LineIndex lineIndex_ {}; }; } // namespace panda::es2panda::parser -- Gitee