diff --git a/ets2panda/compiler/core/dynamicContext.cpp b/ets2panda/compiler/core/dynamicContext.cpp index 529caf5327c5c922cd6e31c3dee52074e795a76e..608d4018375296167d268968b20cd5a30fde5061 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 0000000000000000000000000000000000000000..3cc995e9d885a166e40f9a693aea056354a841f0 --- /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 134b2c9511a761a51fa5f546e98cd90d932f6c1a..46fa686579245f1313cac4bc5e24fdae83e0a2c2 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 a27bac139ca92ee86ee2aae2e7c000ae54fe4799..40603149914ff10b03caa287e348106222531148 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