From 6b6b8f0fedfa4506b1b785513aab6169d0ffcb18 Mon Sep 17 00:00:00 2001 From: xuyunan-hw Date: Fri, 28 Mar 2025 14:31:26 +0800 Subject: [PATCH] string get inlining Issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IBWYK3 Signed-off-by: xuyunan-hw --- .../intrinsics_ir_build_virtual_call_ets.inl | 1 + .../ets/runtime/ets_libbase_runtime.yaml | 9 +++ .../runtime/intrinsics/std_core_String.cpp | 5 ++ .../plugins/ets/stdlib/std/core/String.ets | 4 +- .../plugins/ets/tests/checked/CMakeLists.txt | 1 + .../ets/tests/checked/ets_string_get.ets | 58 +++++++++++++++++++ 6 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 static_core/plugins/ets/tests/checked/ets_string_get.ets diff --git a/static_core/plugins/ets/compiler/intrinsics_ir_build_virtual_call_ets.inl b/static_core/plugins/ets/compiler/intrinsics_ir_build_virtual_call_ets.inl index 89a5886c72..cfe648017c 100644 --- a/static_core/plugins/ets/compiler/intrinsics_ir_build_virtual_call_ets.inl +++ b/static_core/plugins/ets/compiler/intrinsics_ir_build_virtual_call_ets.inl @@ -17,6 +17,7 @@ case RuntimeInterface::IntrinsicId::INTRINSIC_STD_CORE_RUNTIME_IS_SAME_REFERENCE Builder()->BuildStdRuntimeEquals(bcInst_, ACC_READ); break; } +case RuntimeInterface::IntrinsicId::INTRINSIC_STD_CORE_STRING_GET: case RuntimeInterface::IntrinsicId::INTRINSIC_STD_CORE_STRING_CHAR_AT: { if (!Builder()->TryBuildStringCharAtIntrinsic(bcInst_, ACC_READ)) { BuildDefaultVirtualCallIntrinsic(intrinsicId); diff --git a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml index f0285951dd..f195bb2095 100644 --- a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml +++ b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml @@ -1196,6 +1196,15 @@ intrinsics: codegen_func: CreateStringRepeat llvm_codegen_func: EmitStringRepeat + - name: StdCoreStringGet + space: ets + class_name: std.core.String + method_name: $_get + static: false + signature: + ret: u16 + args: [ i32 ] + impl: ark::ets::intrinsics::StdCoreStringGet ########################## # std.core.StringBuilder # ########################## diff --git a/static_core/plugins/ets/runtime/intrinsics/std_core_String.cpp b/static_core/plugins/ets/runtime/intrinsics/std_core_String.cpp index 96c129fbcb..5bea431fc9 100644 --- a/static_core/plugins/ets/runtime/intrinsics/std_core_String.cpp +++ b/static_core/plugins/ets/runtime/intrinsics/std_core_String.cpp @@ -581,4 +581,9 @@ EtsString *StdCoreStringRepeat(EtsString *str, EtsInt count) return rep; } +uint16_t StdCoreStringGet(EtsString *str, EtsInt index) +{ + return StdCoreStringCharAt(str, index); +} + } // namespace ark::ets::intrinsics diff --git a/static_core/plugins/ets/stdlib/std/core/String.ets b/static_core/plugins/ets/stdlib/std/core/String.ets index 647f8a0643..82c529dd0b 100644 --- a/static_core/plugins/ets/stdlib/std/core/String.ets +++ b/static_core/plugins/ets/stdlib/std/core/String.ets @@ -92,9 +92,7 @@ export final class String extends Object implements Comparable, JSONable * * @throws StringIndexOutOfBoundsError if index is negative or >= length */ - public $_get(index: int): char { - return this.charAt(index); - } + public native $_get(index: int): char; /** * Checks equality of this string and another Object as String diff --git a/static_core/plugins/ets/tests/checked/CMakeLists.txt b/static_core/plugins/ets/tests/checked/CMakeLists.txt index c0ba7b818a..e79494687a 100644 --- a/static_core/plugins/ets/tests/checked/CMakeLists.txt +++ b/static_core/plugins/ets/tests/checked/CMakeLists.txt @@ -183,6 +183,7 @@ if (PANDA_TARGET_AMD64 OR NOT PANDA_ARM64_TESTS_WITH_SANITIZER) if (PANDA_TARGET_ARM64) panda_add_checked_test_ets(FILE ${CMAKE_CURRENT_SOURCE_DIR}/memory_coalescing.ets) panda_add_checked_test_ets(FILE ${CMAKE_CURRENT_SOURCE_DIR}/charAt.ets) + panda_add_checked_test_ets(FILE ${CMAKE_CURRENT_SOURCE_DIR}/ets_string_get.ets) panda_add_checked_test_ets(FILE ${CMAKE_CURRENT_SOURCE_DIR}/memory_coalescing1.ets) panda_add_checked_test_ets(FILE ${CMAKE_CURRENT_SOURCE_DIR}/memory_coalescing2.ets) panda_add_checked_test_ets(FILE ${CMAKE_CURRENT_SOURCE_DIR}/memory_coalescing3.ets) diff --git a/static_core/plugins/ets/tests/checked/ets_string_get.ets b/static_core/plugins/ets/tests/checked/ets_string_get.ets new file mode 100644 index 0000000000..1bd22c7ae7 --- /dev/null +++ b/static_core/plugins/ets/tests/checked/ets_string_get.ets @@ -0,0 +1,58 @@ +/* + * 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. + */ + +//! CHECKER Test StringGet inlining in compiler +//! RUN force_jit: true, options: "", entry: "ets_string_get.ETSGLOBAL::main" +//! METHOD "ets_string_get.ETSGLOBAL::main" +//! PASS_AFTER "IrBuilder" +//! INST_NOT "Intrinsic.StdCoreStringGet" +//! INST /NullCheck/ +//! INST /BoundsCheck/ +//! INST /LoadCompressedStringChar/ + +//! CHECKER AOT IR Builder StringGet get inlining. +//! RUN_PAOC options: "--compiler-regex='.*main.*'" +//! METHOD "ets_string_get.ETSGLOBAL::main" +//! PASS_AFTER "IrBuilder" +//! INST_NOT /Intrinsic.StdCoreStringGet/ +//! INST /NullCheck/ +//! INST /BoundsCheck/ +//! INST /LoadCompressedStringChar/ + +function main(): void { + let str = "abcdefg"; + assertEQ(str[0], c'a', "Wrong str.get(0)"); + assertEQ(str[6], c'g', "Wrong str.get(6)"); + + let exceptionFlag = 0; + try { + console.log(str[-1]); + } catch (e: StringIndexOutOfBoundsError) { + exceptionFlag = 1; + } catch (e) { + exceptionFlag = 2; + } + assertEQ(1, exceptionFlag); + + exceptionFlag = 0; + try { + console.log(str[7]); + } catch (e: StringIndexOutOfBoundsError) { + exceptionFlag = 1; + } catch (e) { + exceptionFlag = 2; + } + assertEQ(1, exceptionFlag); +} \ No newline at end of file -- Gitee