1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "libabckit/include/c/metadata_core.h"
17 #include "libabckit/include/c/ir_core.h"
18 #include "libabckit/include/c/abckit.h"
19 #include "helpers/helpers_runtime.h"
20
21 #include "helpers/helpers.h"
22 #include "ut/isa/isa_dynamic/arithmetic/helpers_arithmetic.h"
23
24 #include <gtest/gtest.h>
25
26 // NOLINTBEGIN(readability-magic-numbers)
27 namespace libabckit::test {
28
29 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34
ValidTest(AbckitInst * (* binaryInstToCheck)(AbckitGraph * graph,AbckitInst * input0,uint64_t imm),AbckitIsaApiStaticOpcode expectedOpcode,const std::string & expectedOutput)35 static void ValidTest(AbckitInst *(*binaryInstToCheck)(AbckitGraph *graph, AbckitInst *input0, uint64_t imm),
36 AbckitIsaApiStaticOpcode expectedOpcode, const std::string &expectedOutput)
37 {
38 auto output =
39 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_logical_imm_static.abc",
40 "bininst_logical_imm_static/ETSGLOBAL", "main");
41 EXPECT_TRUE(helpers::Match(output, "9\n"));
42
43 helpers::TransformMethod(
44 ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_logical_imm_static.abc",
45 ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_logical_imm_static_modified.abc", "foo",
46 [&](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
47 helpers::arithmetic::TransformIrBinInstrWithImmValid(graph, binaryInstToCheck);
48 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
49 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
50 helpers::arithmetic::CreateBBSchemaForBinaryWithImm(expectedOpcode));
51 helpers::VerifyGraph(graph, bbSchemas);
52 },
53 [&]([[maybe_unused]] AbckitGraph *graph) {});
54
55 output =
56 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/isa/isa_static/arithmetic/bininst_logical_imm_static_modified.abc",
57 "bininst_logical_imm_static/ETSGLOBAL", "main");
58 EXPECT_TRUE(helpers::Match(output, expectedOutput));
59 }
60
61 class LibAbcKitCreateBinInstLogicalImmTest : public ::testing::Test {};
62
63 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateOrI, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstLogicalImmTest,CreateOrIValid)64 TEST_F(LibAbcKitCreateBinInstLogicalImmTest, CreateOrIValid)
65 {
66 ValidTest(g_statG->iCreateOrI, ABCKIT_ISA_API_STATIC_OPCODE_ORI, "5\n");
67 }
68
69 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateXorI, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstLogicalImmTest,CreateXorIValid)70 TEST_F(LibAbcKitCreateBinInstLogicalImmTest, CreateXorIValid)
71 {
72 ValidTest(g_statG->iCreateXorI, ABCKIT_ISA_API_STATIC_OPCODE_XORI, "1\n");
73 }
74
75 // Test: test-kind=api, api=IsaApiStaticImpl::iCreateAndI, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitCreateBinInstLogicalImmTest,CreateAndIValid)76 TEST_F(LibAbcKitCreateBinInstLogicalImmTest, CreateAndIValid)
77 {
78 ValidTest(g_statG->iCreateAndI, ABCKIT_ISA_API_STATIC_OPCODE_ANDI, "4\n");
79 }
80
81 } // namespace libabckit::test
82 // NOLINTEND(readability-magic-numbers)
83