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/isa/isa_dynamic.h"
19 #include "libabckit/include/c/abckit.h"
20
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23 #include "libabckit/include/c/statuses.h"
24
25 #include <gtest/gtest.h>
26 #include <cstdint>
27
28 // NOLINTBEGIN(readability-magic-numbers)
29 namespace libabckit::test {
30
31 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36
37 class LibAbcKitCreateDynTestin : public ::testing::Test {};
38
TransformIr(AbckitGraph * graph,uint64_t slot)39 static void TransformIr(AbckitGraph *graph, uint64_t slot)
40 {
41 auto *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
42 ASSERT_NE(ret, nullptr);
43 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
44 auto *par = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
45 ASSERT_NE(par, nullptr);
46 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
47
48 auto *testin = g_dynG->iCreateTestin(graph, par, 0x0, slot);
49 ASSERT_NE(testin, nullptr);
50 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
51
52 g_implG->iInsertBefore(testin, ret);
53 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54
55 g_implG->iSetInput(ret, testin, 0);
56 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
57 }
58
VerifyIr(AbckitGraph * graph)59 static void VerifyIr(AbckitGraph *graph)
60 {
61 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchema = {
62 {{},
63 {1},
64 {
65 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
66 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
67 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
68 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
69 }},
70 {{0},
71 {2},
72 {
73 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_TESTIN, {3}},
74 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {4}},
75 }},
76 {{1}, {}, {}}};
77
78 helpers::VerifyGraph(graph, bbSchema);
79 }
80
TestImpl(uint64_t slot,const char * expectedOutput)81 static void TestImpl(uint64_t slot, const char *expectedOutput)
82 {
83 auto output =
84 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/testin/testin_dynamic.abc", "testin_dynamic");
85 EXPECT_TRUE(helpers::Match(output, "\\[object Object\\]\n\\[object Object\\]\n"));
86
87 helpers::TransformMethod(
88 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/testin/testin_dynamic.abc",
89 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/testin/testin_dynamic_modified.abc", "testin_dynamic.foo",
90 [&](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) { TransformIr(graph, slot); },
91 [&](AbckitGraph *graph) { VerifyIr(graph); });
92
93 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/testin/testin_dynamic_modified.abc",
94 "testin_dynamic");
95 EXPECT_TRUE(helpers::Match(output, expectedOutput));
96 }
97
98 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateTestin, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynTestin,CreateTestIn_1)99 TEST_F(LibAbcKitCreateDynTestin, CreateTestIn_1)
100 {
101 TestImpl(0x0, "true\nfalse\n");
102 }
103
104 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateTestin, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitCreateDynTestin,CreateTestIn_2)105 TEST_F(LibAbcKitCreateDynTestin, CreateTestIn_2)
106 {
107 TestImpl(0x1, "false\nfalse\n");
108 }
109
110 } // namespace libabckit::test
111 // NOLINTEND(readability-magic-numbers)
112