From 6c04df6567421c48d19edce89fa5207a71a3fc11 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 8 Mar 2024 12:56:40 +0100 Subject: [PATCH] Fix union type property load/store for wide types Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I97KSO Internal issue: #15208 Test: build, new runtime test Signed-off-by: Martin Sajti --- ets2panda/compiler/base/lreference.cpp | 2 +- ets2panda/compiler/core/ETSGen.cpp | 19 +++++++-- ets2panda/compiler/core/ETSGen.h | 3 +- .../ets/union_wide_property_access.ets | 42 +++++++++++++++++++ .../ets-runtime/ets-runtime-ignored.txt | 6 ++- 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 ets2panda/test/runtime/ets/union_wide_property_access.ets diff --git a/ets2panda/compiler/base/lreference.cpp b/ets2panda/compiler/base/lreference.cpp index a047c992aa..8fa1c8ab96 100644 --- a/ets2panda/compiler/base/lreference.cpp +++ b/ets2panda/compiler/base/lreference.cpp @@ -365,7 +365,7 @@ void ETSLReference::SetValue() const } if (objectType->IsETSUnionType()) { - etsg_->StoreUnionProperty(Node(), baseReg_, propName); + etsg_->StoreUnionProperty(Node(), memberExprTsType, baseReg_, propName); return; } diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 54425151d3..a5094b8d8e 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -464,11 +464,18 @@ void ETSGen::LoadProperty(const ir::AstNode *const node, const checker::Type *pr SetAccumulatorType(propType); } -void ETSGen::StoreUnionProperty([[maybe_unused]] const ir::AstNode *node, [[maybe_unused]] VReg objReg, +void ETSGen::StoreUnionProperty([[maybe_unused]] const ir::AstNode *node, + [[maybe_unused]] const checker::Type *propType, [[maybe_unused]] VReg objReg, [[maybe_unused]] const util::StringView &propName) { #ifdef PANDA_WITH_ETS - Ra().Emit(node, objReg, propName); + if (propType->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { + Ra().Emit(node, objReg, propName); + } else if (propType->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { + Ra().Emit(node, objReg, propName); + } else { + Ra().Emit(node, objReg, propName); + } #else UNREACHABLE(); #endif // PANDA_WITH_ETS @@ -479,7 +486,13 @@ void ETSGen::LoadUnionProperty([[maybe_unused]] const ir::AstNode *const node, [[maybe_unused]] const util::StringView &propName) { #ifdef PANDA_WITH_ETS - Ra().Emit(node, objReg, propName); + if (propType->HasTypeFlag(TYPE_FLAG_BYTECODE_REF)) { + Ra().Emit(node, objReg, propName); + } else if (propType->HasTypeFlag(checker::TypeFlag::ETS_WIDE_NUMERIC)) { + Ra().Emit(node, objReg, propName); + } else { + Ra().Emit(node, objReg, propName); + } SetAccumulatorType(propType); #else UNREACHABLE(); diff --git a/ets2panda/compiler/core/ETSGen.h b/ets2panda/compiler/core/ETSGen.h index b06ec78733..f4cf2f43dc 100644 --- a/ets2panda/compiler/core/ETSGen.h +++ b/ets2panda/compiler/core/ETSGen.h @@ -77,7 +77,8 @@ public: void StoreElementDynamic(const ir::AstNode *node, VReg objectReg, VReg index); void LoadElementDynamic(const ir::AstNode *node, VReg objectReg); - void StoreUnionProperty(const ir::AstNode *node, VReg objReg, const util::StringView &propName); + void StoreUnionProperty(const ir::AstNode *node, const checker::Type *propType, VReg objReg, + const util::StringView &propName); void LoadUnionProperty(const ir::AstNode *node, const checker::Type *propType, VReg objReg, const util::StringView &propName); diff --git a/ets2panda/test/runtime/ets/union_wide_property_access.ets b/ets2panda/test/runtime/ets/union_wide_property_access.ets new file mode 100644 index 0000000000..1a496dcc79 --- /dev/null +++ b/ets2panda/test/runtime/ets/union_wide_property_access.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 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. + */ + +class A { memb: number = 2; } +class B { memb: number = 6; } +class C { memb: A|B = new A(); } + +function bar(a0: A | B): void { + a0.memb = 50; +} + +function foo(arr: Int8Array | Int16Array): void { + let len = arr.length + assert len == 42; +} + +function main() { + let buffer: ArrayBuffer = new ArrayBuffer(42); + let byteArr: Int8Array= new Int8Array(buffer) + foo(byteArr); + assert byteArr.length == 42; + + let ab: A|B = new A(); + bar(ab); + assert ab.memb == 50 + + let cClass: C = new C(); + cClass.memb = new B(); + assert cClass.memb.memb == 6; +} diff --git a/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt b/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt index 7b6b2e0d97..b766e9e536 100644 --- a/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt +++ b/ets2panda/test/test-lists/ets-runtime/ets-runtime-ignored.txt @@ -58,5 +58,9 @@ lambdaExpressionWithRestParameter.ets castSequence.ets # ignored due to interface implementation modification -local-class-standard-example1.ets +local-class-standard-example1.ets local-class-standard-example2.ets + +# verifier false positive for 64 bit instructions on union types. +# Issue: #16216 +union_wide_property_access.ets -- Gitee