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
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_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34
35 namespace {
TransformIrDynReturnundefinedInstValid(AbckitGraph * graph,AbckitInst * (* returnundefinedInstToCheck)(AbckitGraph * graph))36 void TransformIrDynReturnundefinedInstValid(AbckitGraph *graph,
37 AbckitInst *(*returnundefinedInstToCheck)(AbckitGraph *graph))
38 {
39 AbckitInst *retOp = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
40 ASSERT_NE(retOp, nullptr);
41
42 auto *mainInst = returnundefinedInstToCheck(graph);
43 ASSERT_NE(mainInst, nullptr);
44 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
45 g_implG->iInsertBefore(mainInst, retOp);
46 g_implG->iRemove(retOp);
47 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
48 }
49
TransformIrDynReturnInstValid(AbckitGraph * graph,AbckitInst * (* returnInstToCheck)(AbckitGraph * graph,AbckitInst * acc))50 void TransformIrDynReturnInstValid(AbckitGraph *graph,
51 AbckitInst *(*returnInstToCheck)(AbckitGraph *graph, AbckitInst *acc))
52 {
53 AbckitInst *retOp = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
54 ASSERT_NE(retOp, nullptr);
55
56 auto *constZero = g_implG->gFindOrCreateConstantU64(graph, 0);
57 auto *mainInst = returnInstToCheck(graph, constZero);
58 ASSERT_NE(mainInst, nullptr);
59 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
60 g_implG->iInsertBefore(mainInst, retOp);
61 g_implG->iRemove(retOp);
62 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
63 }
64
CreateBBSchemaForDynReturnundefined()65 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> CreateBBSchemaForDynReturnundefined()
66 {
67 return {{{},
68 {1},
69 {{1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
70 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
71 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
72 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}}}},
73 {{0}, {2}, {{5, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}}}},
74 {{1}, {}, {}}};
75 }
76
CreateBBSchemaForDynReturn()77 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> CreateBBSchemaForDynReturn()
78 {
79 return {{{},
80 {1},
81 {{1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
82 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
83 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
84 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
85 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}}}},
86 {{0}, {2}, {{6, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {5}}}},
87 {{1}, {}, {}}};
88 }
89 } // namespace
90
91 class LibAbcKitCreateDynReturnInstTest : public ::testing::Test {};
92
93 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateReturnundefined, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynReturnInstTest,CreateDynReturnundefinedValid)94 TEST_F(LibAbcKitCreateDynReturnInstTest, CreateDynReturnundefinedValid)
95 {
96 auto output =
97 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic.abc", "return_dynamic");
98 EXPECT_TRUE(helpers::Match(output, "10\n"));
99
100 helpers::TransformMethod(
101 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic.abc",
102 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic_modified.abc", "foo",
103 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
104 TransformIrDynReturnundefinedInstValid(graph, g_dynG->iCreateReturnundefined);
105 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
106 },
107 [&](AbckitGraph *graph) {
108 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(CreateBBSchemaForDynReturnundefined());
109 helpers::VerifyGraph(graph, bbSchemas);
110 });
111
112 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic_modified.abc",
113 "return_dynamic");
114 EXPECT_TRUE(helpers::Match(output, "undefined\n"));
115 }
116
117 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateReturn, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynReturnInstTest,CreateDynReturnValid)118 TEST_F(LibAbcKitCreateDynReturnInstTest, CreateDynReturnValid)
119 {
120 auto output =
121 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic.abc", "return_dynamic");
122 EXPECT_TRUE(helpers::Match(output, "10\n"));
123
124 helpers::TransformMethod(
125 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic.abc",
126 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic_modified_0.abc", "foo",
127 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
128 TransformIrDynReturnInstValid(graph, g_dynG->iCreateReturn);
129 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
130 g_implG->gGetStartBasicBlock(graph);
131 },
132 [&](AbckitGraph *graph) {
133 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(CreateBBSchemaForDynReturn());
134 helpers::VerifyGraph(graph, bbSchemas);
135 });
136
137 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/return/return_dynamic_modified_0.abc",
138 "return_dynamic");
139 EXPECT_TRUE(helpers::Match(output, "0\n"));
140 }
141
142 } // namespace libabckit::test
143 // NOLINTEND(readability-magic-numbers)
144