From 8b58ea283ba543f4183c0abe8a8d5876a5f07673 Mon Sep 17 00:00:00 2001 From: Tao Wu Date: Tue, 9 Sep 2025 20:34:14 +0800 Subject: [PATCH] Fix invalid bytecode of try-finally Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICX786 Signed-off-by: Tao Wu --- ets2panda/compiler/core/dynamicContext.cpp | 14 ++++++++++--- ets2panda/test/runtime/ets/try_finally.ets | 23 ++++++++++++++++++++++ ets2panda/util/generateBin.cpp | 1 + ets2panda/util/options.yaml | 5 +++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/runtime/ets/try_finally.ets diff --git a/ets2panda/compiler/core/dynamicContext.cpp b/ets2panda/compiler/core/dynamicContext.cpp index 529caf5327..608d401837 100644 --- a/ets2panda/compiler/core/dynamicContext.cpp +++ b/ets2panda/compiler/core/dynamicContext.cpp @@ -254,9 +254,13 @@ void ETSTryContext::EmitFinalizerInsertion(ETSGen *etsg, compiler::LabelPair lab compiler::VReg res = etsg->AllocReg(); if (isReturn) { - etsg->SetAccumulatorType(statement->AsReturnStatement()->ReturnType()); + auto arg = statement->AsReturnStatement()->Argument(); + auto accumulatorType = arg != nullptr && arg->TsType()->IsETSPrimitiveType() + ? arg->TsType() + : statement->AsReturnStatement()->ReturnType(); + etsg->SetAccumulatorType(accumulatorType); etsg->StoreAccumulator(tryStmt_, res); - etsg->SetVRegType(res, statement->AsReturnStatement()->ReturnType()); + etsg->SetVRegType(res, accumulatorType); } // Second compile of the finaly clause, executed if the statement executed normally, but abrupted by @@ -264,7 +268,11 @@ void ETSTryContext::EmitFinalizerInsertion(ETSGen *etsg, compiler::LabelPair lab tryStmt_->FinallyBlock()->Compile(etsg); if (isReturn) { - etsg->SetAccumulatorType(statement->AsReturnStatement()->ReturnType()); + auto arg = statement->AsReturnStatement()->Argument(); + auto accumulatorType = arg != nullptr && arg->TsType()->IsETSPrimitiveType() + ? arg->TsType() + : statement->AsReturnStatement()->ReturnType(); + etsg->SetAccumulatorType(accumulatorType); etsg->LoadAccumulator(tryStmt_, res); } diff --git a/ets2panda/test/runtime/ets/try_finally.ets b/ets2panda/test/runtime/ets/try_finally.ets new file mode 100644 index 0000000000..3cc995e9d8 --- /dev/null +++ b/ets2panda/test/runtime/ets/try_finally.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 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. + */ + +function foo(): int { + try { + return 0; + } finally { + } +} + +arktest.assertTrue(foo() == 0); diff --git a/ets2panda/util/generateBin.cpp b/ets2panda/util/generateBin.cpp index 134b2c9511..46fa686579 100644 --- a/ets2panda/util/generateBin.cpp +++ b/ets2panda/util/generateBin.cpp @@ -50,6 +50,7 @@ static int OptimizeBytecode(ark::pandasm::Program *prog, const util::Options &op } ark::bytecodeopt::g_options.SetOptLevel(options.GetOptLevel()); + ark::bytecodeopt::g_options.SetSkipMethodsWithEh(!options.IsOptTryCatchFunc()); // Set default value instead of maximum set in ark::bytecodeopt::SetCompilerOptions() ark::compiler::CompilerLogger::Init({"all"}); ark::compiler::g_options.SetCompilerMaxBytecodeSize(ark::compiler::g_options.GetCompilerMaxBytecodeSize()); diff --git a/ets2panda/util/options.yaml b/ets2panda/util/options.yaml index 0e3f72acae..c8c7236390 100644 --- a/ets2panda/util/options.yaml +++ b/ets2panda/util/options.yaml @@ -101,6 +101,11 @@ options: description: Compiler optimization level range: 0-2 +- name: opt-try-catch-func + type: bool + default: false + description: Enable optimizations for functions with try-catch blocks + - name: ets-module type: bool default: false -- Gitee