From 966daa65b2b017bae3560ab4ae64135fbfd094e5 Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 10 Oct 2022 21:06:27 +0800 Subject: [PATCH 1/2] fixed f3ff08d from https://gitee.com/hufeng20/arkcompiler_ets_frontend/pulls/575 Add compiler testcase Signed-off-by: hufeng Change-Id: Iac491aff3f568bc7a8f3c1d22ea1fb5bac6f82a1 --- .../try-finally-with-return-in-switch-expected.txt | 1 + .../try/try-finally-with-return-in-switch.js | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch-expected.txt create mode 100644 es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch.js diff --git a/es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch-expected.txt b/es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch-expected.txt new file mode 100644 index 0000000000..b62420bf91 --- /dev/null +++ b/es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch-expected.txt @@ -0,0 +1 @@ +Run finally before return diff --git a/es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch.js b/es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch.js new file mode 100644 index 0000000000..53dd21db3f --- /dev/null +++ b/es2panda/test/compiler/js/language/statements/try/try-finally-with-return-in-switch.js @@ -0,0 +1,12 @@ +function test() { + let a = 0; + try { + switch (a) { + case 0: { + return 123; + } + } + } catch {} finally {print("Run finally before return");} +} + +test(); -- Gitee From 67523eee4e19416ea9e5538f2386e006155e2a4d Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 10 Oct 2022 16:53:07 +0800 Subject: [PATCH 2/2] fixed a13e875 from https://gitee.com/hufeng20/arkcompiler_ets_frontend/pulls/575 Fix control flow change's compiling issue with try-finally Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I5UX0Z Signed-off-by: hufeng Change-Id: I201ca275ba5dd6f48eb83e66036d29efa6f56af1 --- es2panda/compiler/core/labelTarget.h | 1 + es2panda/compiler/core/pandagen.cpp | 9 +++++++++ es2panda/compiler/core/pandagen.h | 1 + es2panda/compiler/function/functionBuilder.cpp | 6 +++--- es2panda/ir/statements/returnStatement.cpp | 2 +- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/es2panda/compiler/core/labelTarget.h b/es2panda/compiler/core/labelTarget.h index 6c9ee9792b..702adbb036 100644 --- a/es2panda/compiler/core/labelTarget.h +++ b/es2panda/compiler/core/labelTarget.h @@ -77,6 +77,7 @@ public: static constexpr std::string_view BREAK_LABEL = "#b"; static constexpr std::string_view CONTINUE_LABEL = "#c"; + static constexpr std::string_view RETURN_LABEL = "#r"; private: util::StringView breakLabel_ {}; diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index ef06720971..fae67651e4 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -709,6 +709,15 @@ Label *PandaGen::ControlFlowChangeContinue(const ir::Identifier *label) return continueTarget; } +void PandaGen::ControlFlowChangeReturn() +{ + auto *iter = dynamicContext_; + while (iter) { + iter->AbortContext(ControlFlowChange::BREAK, LabelTarget::RETURN_LABEL); + iter = iter->Prev(); + } +} + void PandaGen::Condition(const ir::AstNode *node, lexer::TokenType op, VReg lhs, Label *ifFalse) { switch (op) { diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index afeed11653..445d4abb0d 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -267,6 +267,7 @@ public: bool CheckControlFlowChange(); Label *ControlFlowChangeBreak(const ir::Identifier *label = nullptr); Label *ControlFlowChangeContinue(const ir::Identifier *label); + void ControlFlowChangeReturn(); void Condition(const ir::AstNode *node, lexer::TokenType op, VReg lhs, class Label *ifFalse); void Unary(const ir::AstNode *node, lexer::TokenType op, VReg operand); diff --git a/es2panda/compiler/function/functionBuilder.cpp b/es2panda/compiler/function/functionBuilder.cpp index e6ba14286f..2532df116c 100644 --- a/es2panda/compiler/function/functionBuilder.cpp +++ b/es2panda/compiler/function/functionBuilder.cpp @@ -129,7 +129,7 @@ void FunctionBuilder::HandleCompletion(const ir::AstNode *node, VReg completionT pg_->Condition(node, lexer::TokenType::PUNCTUATOR_EQUAL, completionType, notRetLabel); if (!handleReturn_) { handleReturn_ = true; - pg_->ControlFlowChangeBreak(); + pg_->ControlFlowChangeReturn(); handleReturn_ = false; } @@ -223,7 +223,7 @@ void FunctionBuilder::YieldStar(const ir::AstNode *node) pg_->BranchIfNotUndefined(node, callMethod); // 1. If generatorKind is async, set received.[[Value]] to ? Await(received.[[Value]]). - pg_->ControlFlowChangeBreak(); + pg_->ControlFlowChangeReturn(); pg_->LoadAccumulator(node, receivedValue); if (GeneratorKind() == IteratorType::ASYNC) { @@ -307,7 +307,7 @@ void FunctionBuilder::YieldStar(const ir::AstNode *node) if (pg_->CheckControlFlowChange()) { pg_->StoreAccumulator(node, receivedValue); - pg_->ControlFlowChangeBreak(); + pg_->ControlFlowChangeReturn(); pg_->LoadAccumulator(node, receivedValue); } diff --git a/es2panda/ir/statements/returnStatement.cpp b/es2panda/ir/statements/returnStatement.cpp index 5d65a90860..897db1d0e5 100644 --- a/es2panda/ir/statements/returnStatement.cpp +++ b/es2panda/ir/statements/returnStatement.cpp @@ -51,7 +51,7 @@ void ReturnStatement::Compile(compiler::PandaGen *pg) const compiler::VReg res = pg->AllocReg(); pg->StoreAccumulator(this, res); - pg->ControlFlowChangeBreak(); + pg->ControlFlowChangeReturn(); pg->LoadAccumulator(this, res); } -- Gitee