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 <gtest/gtest.h>
17 #include <fstream>
18
19 #include "libabckit/include/c/abckit.h"
20 #include "adapter_static/ir_static.h"
21 #include "metadata_inspect_impl.h"
22 #include "libabckit/include/c/statuses.h"
23 #include "libabckit/include/c/metadata_core.h"
24 #include "helpers/helpers.h"
25 #include "helpers/helpers_runtime.h"
26
27 // NOLINTBEGIN(readability-magic-numbers)
28
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_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37
38 class LibAbcKitIrInstTest : public ::testing::Test {};
39
40 // Test: test-kind=api, api=GraphApiImpl::iSetInput, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IsetInput_1)41 TEST_F(LibAbcKitIrInstTest, IsetInput_1)
42 {
43 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
44 "test2:inst_manipulation_static.Test;i32;i32;",
45 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
46 auto *start = g_implG->gGetStartBasicBlock(graph);
47 auto *input = g_implG->iGetNext(g_implG->bbGetFirstInst(start));
48 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_IF);
49
50 g_implG->iSetInput(inst, input, 0);
51 ASSERT_EQ(g_implG->iGetInput(inst, 0), input);
52 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
53 });
54 }
55
56 // Test: test-kind=api, api=GraphApiImpl::iSetInput, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IsetInput_2)57 TEST_F(LibAbcKitIrInstTest, IsetInput_2)
58 {
59 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func",
60 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
61 auto *start = g_implG->gGetStartBasicBlock(graph);
62 auto *input = g_implG->iGetNext(g_implG->bbGetFirstInst(start));
63 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_IF);
64
65 g_implG->iSetInput(inst, input, 0);
66 ASSERT_EQ(g_implG->iGetInput(inst, 0), input);
67 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
68 });
69 }
70
71 // Test: test-kind=api, api=GraphApiImpl::iGetInput, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetInput_1)72 TEST_F(LibAbcKitIrInstTest, IgetInput_1)
73 {
74 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
75 "test2:inst_manipulation_static.Test;i32;i32;",
76 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
77 auto *start = g_implG->gGetStartBasicBlock(graph);
78 auto *input = g_implG->iGetNext(g_implG->iGetNext(g_implG->bbGetFirstInst(start)));
79 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_IF);
80 ASSERT_NE(inst, nullptr);
81 ASSERT_EQ(g_implG->iGetInput(inst, 0), input);
82 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
83 });
84 }
85
86 // Test: test-kind=api, api=GraphApiImpl::iGetInput, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,IgetInput_2)87 TEST_F(LibAbcKitIrInstTest, IgetInput_2)
88 {
89 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
90 "test2:inst_manipulation_static.Test;i32;i32;",
91 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
92 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_IF);
93
94 ASSERT_EQ(g_implG->iGetInput(inst, -1), nullptr);
95 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
96 ASSERT_EQ(g_implG->iGetInput(inst, 2), nullptr);
97 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
98 });
99 }
100
101 // Test: test-kind=api, api=GraphApiImpl::iGetInput, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetInput_3)102 TEST_F(LibAbcKitIrInstTest, IgetInput_3)
103 {
104 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func",
105 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
106 auto *input = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_GREATER);
107 auto *instIf = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_IF);
108
109 ASSERT_EQ(g_implG->iGetInput(instIf, 0), input);
110 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
111 });
112 }
113
114 // Test: test-kind=api, api=GraphApiImpl::iGetInput, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IgetInput_4)115 TEST_F(LibAbcKitIrInstTest, IgetInput_4)
116 {
117 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func",
118 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
119 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_IF);
120
121 ASSERT_EQ(g_implG->iGetInput(inst, -1), nullptr);
122 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
123 ASSERT_EQ(g_implG->iGetInput(inst, 2), nullptr);
124 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
125 });
126 }
127
128 // Test: test-kind=api, api=GraphApiImpl::iGetInputCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetInputCount_1)129 TEST_F(LibAbcKitIrInstTest, IgetInputCount_1)
130 {
131 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
132 "test2:inst_manipulation_static.Test;i32;i32;",
133 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
134 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_IF);
135
136 ASSERT_EQ(g_implG->iGetInputCount(inst), 2);
137 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
138 });
139 }
140
141 // Test: test-kind=api, api=GraphApiImpl::iGetInputCount, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetInputCount_2)142 TEST_F(LibAbcKitIrInstTest, IgetInputCount_2)
143 {
144 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func",
145 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
146 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_IF);
147
148 ASSERT_EQ(g_implG->iGetInputCount(inst), 2);
149 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
150 });
151 }
152
153 // Test: test-kind=api, api=GraphApiImpl::iGetImmediateCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediateCount_1)154 TEST_F(LibAbcKitIrInstTest, IgetImmediateCount_1)
155 {
156 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
157 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
158 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
159 auto const2 = g_implG->gFindOrCreateConstantU64(graph, 2);
160 auto *addi = g_statG->iCreateAddI(graph, const1, 2);
161 auto *add = g_statG->iCreateAdd(graph, const1, const2);
162
163 ASSERT_EQ(g_implG->iGetImmediateCount(addi), 1);
164 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
165 ASSERT_EQ(g_implG->iGetImmediateCount(add), 0);
166 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
167 });
168 }
169
170 // Test: test-kind=api, api=GraphApiImpl::iGetImmediateCount, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediateCount_2)171 TEST_F(LibAbcKitIrInstTest, IgetImmediateCount_2)
172 {
173 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
174 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
175 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
176 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
177 auto *getnextpropname = g_dynG->iCreateGetnextpropname(graph, const1);
178
179 ASSERT_EQ(g_implG->iGetImmediateCount(newlexenv), 1);
180 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
181 ASSERT_EQ(g_implG->iGetImmediateCount(getnextpropname), 0);
182 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
183 });
184 }
185
186 // Test: test-kind=api, api=GraphApiImpl::iGetImmediate, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediate_1)187 TEST_F(LibAbcKitIrInstTest, IgetImmediate_1)
188 {
189 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
190 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
191 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
192 auto *addi = g_statG->iCreateAddI(graph, const1, 2);
193
194 ASSERT_EQ(g_implG->iGetImmediate(addi, 0), 2);
195 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
196 });
197 }
198
199 // Test: test-kind=api, api=GraphApiImpl::iGetImmediate, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,IgetImmediate_2)200 TEST_F(LibAbcKitIrInstTest, IgetImmediate_2)
201 {
202 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
203 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
204 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
205 auto const2 = g_implG->gFindOrCreateConstantU64(graph, 2);
206 auto *addi = g_statG->iCreateAddI(graph, const1, 2);
207 auto *add = g_statG->iCreateAdd(graph, const1, const2);
208
209 ASSERT_EQ(g_implG->iGetImmediate(addi, -1), 0);
210 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
211 ASSERT_EQ(g_implG->iGetImmediate(addi, 1), 0);
212 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
213 ASSERT_EQ(g_implG->iGetImmediate(add, 0), 0);
214 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
215 });
216 }
217
218 // Test: test-kind=api, api=GraphApiImpl::iGetImmediate, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediate_3)219 TEST_F(LibAbcKitIrInstTest, IgetImmediate_3)
220 {
221 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
222 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
223 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
224
225 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, 0), 2);
226 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
227 });
228 }
229
230 // Test: test-kind=api, api=GraphApiImpl::iGetImmediate, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IgetImmediate_4)231 TEST_F(LibAbcKitIrInstTest, IgetImmediate_4)
232 {
233 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
234 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
235 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
236 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
237 auto *getnextpropname = g_dynG->iCreateGetnextpropname(graph, const1);
238
239 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, -1), 0);
240 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
241 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, 1), 0);
242 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
243 ASSERT_EQ(g_implG->iGetImmediate(getnextpropname, 0), 0);
244 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
245 });
246 }
247
248 // Test: test-kind=api, api=GraphApiImpl::iSetImmediate, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IsetImmediate_1)249 TEST_F(LibAbcKitIrInstTest, IsetImmediate_1)
250 {
251 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
252 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
253 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
254 auto *addi = g_statG->iCreateAddI(graph, const1, 2);
255
256 ASSERT_EQ(g_implG->iGetImmediate(addi, 0), 2);
257 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
258 g_implG->iSetImmediate(addi, 0, 3);
259 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
260 ASSERT_EQ(g_implG->iGetImmediate(addi, 0), 3);
261 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
262 });
263 }
264
265 // Test: test-kind=api, api=GraphApiImpl::iSetImmediate, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,IsetImmediate_2)266 TEST_F(LibAbcKitIrInstTest, IsetImmediate_2)
267 {
268 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
269 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
270 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
271 auto const2 = g_implG->gFindOrCreateConstantU64(graph, 2);
272 auto *addi = g_statG->iCreateAddI(graph, const1, 2);
273 auto *add = g_statG->iCreateAdd(graph, const1, const2);
274
275 g_implG->iSetImmediate(addi, -1, 1);
276 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
277 g_implG->iSetImmediate(addi, 1, 1);
278 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
279 g_implG->iSetImmediate(add, 0, 1);
280 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
281 });
282 }
283
284 // Test: test-kind=api, api=GraphApiImpl::iSetImmediate, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IsetImmediate_3)285 TEST_F(LibAbcKitIrInstTest, IsetImmediate_3)
286 {
287 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
288 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
289 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
290
291 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, 0), 2);
292 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
293 g_implG->iSetImmediate(newlexenv, 0, 3);
294 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
295 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, 0), 3);
296 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
297 });
298 }
299
300 // Test: test-kind=api, api=GraphApiImpl::iSetImmediate, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IsetImmediate_4)301 TEST_F(LibAbcKitIrInstTest, IsetImmediate_4)
302 {
303 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
304 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
305 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
306 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
307 auto *getnextpropname = g_dynG->iCreateGetnextpropname(graph, const1);
308
309 g_implG->iSetImmediate(newlexenv, -1, 1);
310 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
311 g_implG->iSetImmediate(newlexenv, 1, 1);
312 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
313 g_implG->iSetImmediate(getnextpropname, 0, 1);
314 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
315 });
316 }
317
318 // Test: test-kind=api, api=GraphApiImpl::iSetImmediate, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IsetImmediate_5)319 TEST_F(LibAbcKitIrInstTest, IsetImmediate_5)
320 {
321 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
322 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
323 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
324
325 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, 0), 2);
326 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
327 g_implG->iSetImmediate(newlexenv, 0, 0x100);
328 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
329 ASSERT_EQ(g_implG->iGetImmediate(newlexenv, 0), 2);
330 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
331 });
332 }
333
334 // Test: test-kind=api, api=GraphApiImpl::iGetImmediateSize, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediateSize_1)335 TEST_F(LibAbcKitIrInstTest, IgetImmediateSize_1)
336 {
337 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
338 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
339 auto *newlexenv = g_dynG->iCreateNewlexenv(graph, 2);
340
341 ASSERT_EQ(g_implG->iGetImmediateSize(newlexenv, 0), AbckitBitImmSize::BITSIZE_8);
342 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
343 });
344 }
345
346 // Test: test-kind=api, api=GraphApiImpl::iGetImmediateSize, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediateSize_2)347 TEST_F(LibAbcKitIrInstTest, IgetImmediateSize_2)
348 {
349 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "test",
350 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
351 auto *wideldlexvar = g_dynG->iCreateWideLdlexvar(graph, 1000, 1);
352
353 ASSERT_EQ(g_implG->iGetImmediateSize(wideldlexvar, 1), AbckitBitImmSize::BITSIZE_16);
354 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
355 });
356 }
357
358 // Test: test-kind=api, api=GraphApiImpl::iGetImmediateSize, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediateSize_3)359 TEST_F(LibAbcKitIrInstTest, IgetImmediateSize_3)
360 {
361 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
362 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
363 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
364 auto *addi = g_statG->iCreateAddI(graph, const1, 2);
365
366 ASSERT_EQ(g_implG->iGetImmediateSize(addi, 0), AbckitBitImmSize::BITSIZE_8);
367 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
368 });
369 }
370
371 // Test: test-kind=api, api=GraphApiImpl::iGetImmediateSize, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetImmediateSize_4)372 TEST_F(LibAbcKitIrInstTest, IgetImmediateSize_4)
373 {
374 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test",
375 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
376 auto const1 = g_implG->gFindOrCreateConstantU64(graph, 1);
377 auto *xori = g_statG->iCreateXorI(graph, const1, 2);
378
379 ASSERT_EQ(g_implG->iGetImmediateSize(xori, 0), AbckitBitImmSize::BITSIZE_32);
380 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
381 });
382 }
383
384 // Test: test-kind=api, api=GraphApiImpl::iGetNext, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetNext_1)385 TEST_F(LibAbcKitIrInstTest, IgetNext_1)
386 {
387 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
388 "test2:inst_manipulation_static.Test;i32;i32;",
389 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
390 auto *start = g_implG->gGetStartBasicBlock(graph);
391 auto *inst = g_implG->bbGetFirstInst(start);
392 auto *next = g_implG->iGetNext(inst);
393 ASSERT_EQ(g_statG->iGetOpcode(next), ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER);
394 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
395 });
396 }
397
398 // Test: test-kind=api, api=GraphApiImpl::iGetNext, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetNext_2)399 TEST_F(LibAbcKitIrInstTest, IgetNext_2)
400 {
401 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
402 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
403 auto *start = g_implG->gGetStartBasicBlock(graph);
404 auto *inst = g_implG->bbGetFirstInst(start);
405 auto *next = g_implG->iGetNext(inst);
406 ASSERT_EQ(g_dynG->iGetOpcode(next), ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
407 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
408 });
409 }
410
411 // Test: test-kind=api, api=GraphApiImpl::iGetPrev, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetPrev_1)412 TEST_F(LibAbcKitIrInstTest, IgetPrev_1)
413 {
414 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
415 "test2:inst_manipulation_static.Test;i32;i32;",
416 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
417 auto *start = g_implG->gGetStartBasicBlock(graph);
418 auto *inst = g_implG->bbGetFirstInst(start);
419 auto *next = g_implG->iGetNext(inst);
420 auto *prev = g_implG->iGetPrev(next);
421 ASSERT_EQ(g_statG->iGetOpcode(prev), ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER);
422 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
423 });
424 }
425
426 // Test: test-kind=api, api=GraphApiImpl::iGetPrev, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetPrev_2)427 TEST_F(LibAbcKitIrInstTest, IgetPrev_2)
428 {
429 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
430 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
431 auto *start = g_implG->gGetStartBasicBlock(graph);
432 auto *inst = g_implG->bbGetFirstInst(start);
433 auto *next = g_implG->iGetNext(inst);
434 auto *prev = g_implG->iGetPrev(next);
435 ASSERT_EQ(g_dynG->iGetOpcode(prev), ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
436 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
437 });
438 }
439
440 // Test: test-kind=api, api=IsaApiDynamicImpl::iGetOpcode, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetOpcode_1)441 TEST_F(LibAbcKitIrInstTest, IgetOpcode_1)
442 {
443 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
444 "test2:inst_manipulation_static.Test;i32;i32;",
445 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
446 auto *start = g_implG->gGetStartBasicBlock(graph);
447 auto *inst = g_implG->bbGetFirstInst(start);
448 ASSERT_EQ(g_statG->iGetOpcode(inst), ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER);
449 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
450 });
451 }
452
453 // Test: test-kind=api, api=GraphApiImpl::iInsertBefore, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IinsertBefore_1)454 TEST_F(LibAbcKitIrInstTest, IinsertBefore_1)
455 {
456 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
457 "test2:inst_manipulation_static.Test;i32;i32;",
458 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
459 auto *start = helpers::BBgetSuccBlocks(g_implG->gGetStartBasicBlock(graph))[0];
460 auto *inst = g_implG->bbGetLastInst(start);
461 auto *constOne = g_implG->gFindOrCreateConstantI64(graph, 1U);
462 auto *constZero = g_implG->gFindOrCreateConstantI64(graph, 0U);
463 auto *instAdd = g_statG->iCreateAdd(graph, constOne, constZero);
464 g_implG->iInsertBefore(instAdd, inst);
465 ASSERT_EQ(g_implG->iGetPrev(inst), instAdd);
466 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
467 });
468 }
469
470 // Test: test-kind=api, api=GraphApiImpl::iInsertBefore, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IinsertBefore_2)471 TEST_F(LibAbcKitIrInstTest, IinsertBefore_2)
472 {
473 helpers::TransformMethod(
474 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static.abc",
475 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
476 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
477 auto *start = g_implG->gGetStartBasicBlock(graph);
478 auto *bb = helpers::BBgetSuccBlocks(start)[0];
479 auto *inst = g_implG->iGetNext(g_implG->bbGetFirstInst(start));
480 auto *inst2 = g_implG->bbGetFirstInst(bb);
481 auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
482 auto *negInst = g_statG->iCreateNeg(graph, newInst);
483 g_implG->iInsertBefore(negInst, inst2);
484 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
485 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
486 {{{},
487 {1},
488 {{5, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
489 {{0},
490 {2},
491 {
492 {2, ABCKIT_ISA_API_STATIC_OPCODE_NEG, {3}},
493 {4, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
494 }},
495 {{1}, {}, {}}});
496 helpers::VerifyGraph(graph, bbSchemas);
497
498 ASSERT_EQ(inst, g_implG->iGetNext(newInst));
499 },
500 []([[maybe_unused]] AbckitGraph *graph) {});
501 }
502
503 // Test: test-kind=api, api=GraphApiImpl::iInsertBefore, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IinsertBefore_3)504 TEST_F(LibAbcKitIrInstTest, IinsertBefore_3)
505 {
506 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
507 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
508 auto *start = helpers::BBgetSuccBlocks(g_implG->gGetStartBasicBlock(graph))[0];
509 auto *inst = g_implG->bbGetLastInst(start);
510 auto *constOne = g_implG->gFindOrCreateConstantI64(graph, 1U);
511 auto *constZero = g_implG->gFindOrCreateConstantI64(graph, 0U);
512 auto *instAdd = g_dynG->iCreateAdd2(graph, constOne, constZero);
513 g_implG->iInsertBefore(instAdd, inst);
514 ASSERT_EQ(g_implG->iGetPrev(inst), instAdd);
515 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
516 });
517 }
518
519 // Test: test-kind=api, api=GraphApiImpl::iInsertBefore, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IinsertBefore_4)520 TEST_F(LibAbcKitIrInstTest, IinsertBefore_4)
521 {
522 helpers::TransformMethod(
523 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_dynamic.abc",
524 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_dynamic_modified.abc", "test",
525 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
526 auto *start = g_implG->gGetStartBasicBlock(graph);
527 auto *bb = helpers::BBgetSuccBlocks(start)[0];
528 auto *inst = g_implG->iGetNext(g_implG->iGetNext(g_implG->iGetNext(g_implG->bbGetFirstInst(start))));
529 auto *inst2 = g_implG->bbGetFirstInst(bb);
530 auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
531 auto *negInst = g_dynG->iCreateNeg(graph, newInst);
532 g_implG->iInsertBefore(negInst, inst2);
533 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
534 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(
535 {{{},
536 {1},
537 {{5, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
538 {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
539 {7, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
540 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}}}},
541 {{0},
542 {2},
543 {
544 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_NEG, {3}},
545 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}},
546 }},
547 {{1}, {}, {}}});
548 helpers::VerifyGraph(graph, bbSchemas);
549
550 ASSERT_EQ(inst, g_implG->iGetNext(newInst));
551 },
552 []([[maybe_unused]] AbckitGraph *graph) {});
553 }
554
555 // Test: test-kind=api, api=GraphApiImpl::iInsertAfter, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IinsertAfter_1)556 TEST_F(LibAbcKitIrInstTest, IinsertAfter_1)
557 {
558 helpers::TransformMethod(
559 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static.abc",
560 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_static_modified.abc", "test",
561 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
562 auto *start = g_implG->gGetStartBasicBlock(graph);
563 auto *bb = helpers::BBgetSuccBlocks(start)[0];
564 auto *inst = g_implG->iGetNext(g_implG->bbGetFirstInst(start));
565 auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
566 auto *negInst = g_statG->iCreateNeg(graph, newInst);
567 auto *addInst = g_statG->iCreateAdd(graph, negInst, newInst);
568
569 g_implG->bbDisconnectSuccBlock(start, 0);
570 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
571 auto *empty = g_implG->bbCreateEmpty(graph);
572 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
573 g_implG->bbAppendSuccBlock(start, empty);
574 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
575 g_implG->bbAppendSuccBlock(empty, bb);
576 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
577
578 g_implG->bbAddInstBack(empty, negInst);
579 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
580
581 g_implG->iInsertAfter(addInst, negInst);
582 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
583 std::vector<helpers::BBSchema<AbckitIsaApiStaticOpcode>> bbSchemas(
584 {{{},
585 {1},
586 {{6, ABCKIT_ISA_API_STATIC_OPCODE_PARAMETER, {}}, {3, ABCKIT_ISA_API_STATIC_OPCODE_CONSTANT, {}}}},
587 {{0},
588 {2},
589 {{4, ABCKIT_ISA_API_STATIC_OPCODE_NEG, {3}}, {5, ABCKIT_ISA_API_STATIC_OPCODE_ADD, {4, 3}}}},
590 {{1},
591 {3},
592 {
593 {2, ABCKIT_ISA_API_STATIC_OPCODE_RETURN_VOID, {}},
594 }},
595 {{2}, {}, {}}});
596 helpers::VerifyGraph(graph, bbSchemas);
597
598 ASSERT_EQ(inst, g_implG->iGetNext(newInst));
599 },
600 []([[maybe_unused]] AbckitGraph *graph) {});
601 }
602
603 // Test: test-kind=api, api=GraphApiImpl::iInsertAfter, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IinsertAfter_2)604 TEST_F(LibAbcKitIrInstTest, IinsertAfter_2)
605 {
606 helpers::TransformMethod(
607 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_dynamic.abc",
608 ABCKIT_ABC_DIR "ut/ir_core/basic_blocks/basic_blocks_dynamic_modified.abc", "test",
609 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
610 auto *start = g_implG->gGetStartBasicBlock(graph);
611 auto *bb = helpers::BBgetSuccBlocks(start)[0];
612 auto *inst = g_implG->iGetNext(g_implG->iGetNext(g_implG->iGetNext(g_implG->bbGetFirstInst(start))));
613 auto *newInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
614 auto *negInst = g_dynG->iCreateNeg(graph, newInst);
615 auto *addInst = g_dynG->iCreateAdd2(graph, negInst, newInst);
616
617 g_implG->bbDisconnectSuccBlock(start, 0);
618 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
619 auto *empty = g_implG->bbCreateEmpty(graph);
620 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
621 g_implG->bbAppendSuccBlock(start, empty);
622 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
623 g_implG->bbAppendSuccBlock(empty, bb);
624 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
625
626 g_implG->bbAddInstBack(empty, negInst);
627 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
628
629 g_implG->iInsertAfter(addInst, negInst);
630 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
631 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(
632 {{{},
633 {1},
634 {{6, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
635 {7, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
636 {8, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
637 {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}}}},
638 {{0},
639 {2},
640 {{4, ABCKIT_ISA_API_DYNAMIC_OPCODE_NEG, {3}}, {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_ADD2, {4, 3}}}},
641 {{1},
642 {3},
643 {
644 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}},
645 }},
646 {{2}, {}, {}}});
647 helpers::VerifyGraph(graph, bbSchemas);
648
649 ASSERT_EQ(inst, g_implG->iGetNext(newInst));
650 },
651 []([[maybe_unused]] AbckitGraph *graph) {});
652 }
653
654 // Test: test-kind=api, api=GraphApiImpl::iGetBasicBlock, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetBasicBlock_1)655 TEST_F(LibAbcKitIrInstTest, IgetBasicBlock_1)
656 {
657 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
658 "test2:inst_manipulation_static.Test;i32;i32;",
659 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
660 auto *start = g_implG->gGetStartBasicBlock(graph);
661 auto *inst = g_implG->bbGetFirstInst(start);
662
663 ASSERT_EQ(g_implG->iGetBasicBlock(inst), start);
664 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
665 });
666 }
667
668 // Test: test-kind=api, api=GraphApiImpl::iGetBasicBlock, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetBasicBlock_2)669 TEST_F(LibAbcKitIrInstTest, IgetBasicBlock_2)
670 {
671 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
672 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
673 auto *start = g_implG->gGetStartBasicBlock(graph);
674 auto *inst = g_implG->bbGetFirstInst(start);
675
676 ASSERT_EQ(g_implG->iGetBasicBlock(inst), start);
677 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
678 });
679 }
680
681 // Test: test-kind=api, api=GraphApiImpl::iGetGraph, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetGraph_1)682 TEST_F(LibAbcKitIrInstTest, IgetGraph_1)
683 {
684 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
685 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
686 auto *start = g_implG->gGetStartBasicBlock(graph);
687 auto *inst = g_implG->bbGetFirstInst(start);
688
689 ASSERT_EQ(g_implG->iGetGraph(inst), graph);
690 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
691 });
692 }
693
694 // Test: test-kind=api, api=GraphApiImpl::iCheckDominance, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IcheckDominance_1)695 TEST_F(LibAbcKitIrInstTest, IcheckDominance_1)
696 {
697 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
698 "test2:inst_manipulation_static.Test;i32;i32;",
699 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
700 auto *start = g_implG->gGetStartBasicBlock(graph);
701 auto *inst = g_implG->bbGetFirstInst(start);
702 auto *next = g_implG->iGetNext(inst);
703
704 ASSERT_FALSE(g_implG->iCheckDominance(next, inst));
705 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
706 });
707 }
708
709 // Test: test-kind=api, api=GraphApiImpl::iCheckDominance, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IcheckDominance_2)710 TEST_F(LibAbcKitIrInstTest, IcheckDominance_2)
711 {
712 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
713 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
714 auto *start = g_implG->gGetStartBasicBlock(graph);
715 auto *inst = g_implG->bbGetFirstInst(start);
716 auto *next = g_implG->iGetNext(inst);
717
718 ASSERT_FALSE(g_implG->iCheckDominance(next, inst));
719 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
720 });
721 }
722
723 // Test: test-kind=api, api=ApiImpl::GetLastError, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,GetLastError)724 TEST_F(LibAbcKitIrInstTest, GetLastError)
725 {
726 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
727 "test2:inst_manipulation_static.Test;i32;i32;",
728 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
729 auto *start = g_implG->gGetStartBasicBlock(graph);
730 auto *inst = g_implG->bbGetFirstInst(start);
731 auto *next = g_implG->iGetNext(inst);
732
733 ASSERT_FALSE(g_implG->iCheckDominance(next, inst));
734 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
735 });
736 }
737
738 // Test: test-kind=api, api=ApiImpl::GetLastError, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,GetLastError_2)739 TEST_F(LibAbcKitIrInstTest, GetLastError_2)
740 {
741 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
742 [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) {
743 auto *start = g_implG->gGetStartBasicBlock(graph);
744 auto *inst = g_implG->bbGetFirstInst(start);
745 auto *next = g_implG->iGetNext(inst);
746
747 ASSERT_FALSE(g_implG->iCheckDominance(next, inst));
748 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
749 });
750 }
751
752 // Test: test-kind=api, api=GraphApiImpl::iGetUserCount, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetUserCount_1)753 TEST_F(LibAbcKitIrInstTest, IgetUserCount_1)
754 {
755 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
756 "test2:inst_manipulation_static.Test;i32;i32;",
757 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
758 auto *start = g_implG->gGetStartBasicBlock(graph);
759 auto *inst = g_implG->iGetNext(g_implG->bbGetFirstInst(start));
760
761 ASSERT_EQ(g_implG->iGetUserCount(inst), 1);
762 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
763 });
764 }
765
766 // Test: test-kind=api, api=GraphApiImpl::iGetUserCount, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetUserCount_2)767 TEST_F(LibAbcKitIrInstTest, IgetUserCount_2)
768 {
769 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "Test",
770 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
771 auto *start = g_implG->gGetStartBasicBlock(graph);
772 auto *inst = g_implG->iGetNext(g_implG->iGetNext(g_implG->bbGetFirstInst(start)));
773
774 ASSERT_EQ(g_implG->iGetUserCount(inst), 1);
775 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
776 });
777 }
778
779 // Test: test-kind=api, api=GraphApiImpl::iVisitUsers, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IvisitUsers_1)780 TEST_F(LibAbcKitIrInstTest, IvisitUsers_1)
781 {
782 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
783 auto *start = g_implG->gGetStartBasicBlock(graph);
784 auto *inst = g_implG->bbGetFirstInst(start);
785 auto *inst2 = g_implG->iGetNext(g_implG->iGetNext(g_implG->iGetNext(inst)));
786 uint32_t counter = 0;
787 auto cbVisit = [](AbckitInst * /*input*/, void *data) {
788 (*(reinterpret_cast<uint32_t *>(data)))++;
789 return true;
790 };
791 g_implG->iVisitUsers(inst2, &counter, cbVisit);
792 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
793 ASSERT_EQ(counter, 1U);
794 };
795
796 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
797 "test2:inst_manipulation_static.Test;i32;i32;", cb);
798 }
799
800 // Test: test-kind=api, api=GraphApiImpl::iVisitUsers, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IvisitUsers_2)801 TEST_F(LibAbcKitIrInstTest, IvisitUsers_2)
802 {
803 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
804 auto *start = g_implG->gGetStartBasicBlock(graph);
805 auto *inst = g_implG->bbGetFirstInst(start);
806 auto *inst2 = g_implG->iGetNext(g_implG->iGetNext(g_implG->iGetNext(g_implG->iGetNext(inst))));
807 uint32_t counter = 0;
808 auto cbVisit = [](AbckitInst * /*input*/, void *data) {
809 (*(reinterpret_cast<uint32_t *>(data)))++;
810 return true;
811 };
812 g_implG->iVisitUsers(inst2, &counter, cbVisit);
813 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
814 ASSERT_EQ(counter, 2U);
815 };
816 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func", cb);
817 }
818
819 // Test: test-kind=api, api=GraphApiImpl::iVisitInputs, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IvisitInputs_1)820 TEST_F(LibAbcKitIrInstTest, IvisitInputs_1)
821 {
822 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
823 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_IF);
824 uint32_t counter = 0;
825 auto cbVisit = [](AbckitInst * /*input*/, void *data) {
826 (*(reinterpret_cast<uint32_t *>(data)))++;
827 return true;
828 };
829 g_implG->iVisitUsers(inst, &counter, cbVisit);
830 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
831 ASSERT_EQ(counter, 0);
832 };
833 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
834 "test2:inst_manipulation_static.Test;i32;i32;", cb);
835 }
836
837 // Test: test-kind=api, api=GraphApiImpl::iVisitInputs, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IvisitInputs_2)838 TEST_F(LibAbcKitIrInstTest, IvisitInputs_2)
839 {
840 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
841 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_IF);
842 uint32_t counter = 0;
843 auto cbVisit = [](AbckitInst * /*input*/, void *data) {
844 (*(reinterpret_cast<uint32_t *>(data)))++;
845 return true;
846 };
847 g_implG->iVisitUsers(inst, &counter, cbVisit);
848 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
849 ASSERT_EQ(counter, 0);
850 };
851 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func", cb);
852 }
853
854 // Test: test-kind=api, api=GraphApiImpl::iSetLiteralArray, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IsetLiteralArray_1)855 TEST_F(LibAbcKitIrInstTest, IsetLiteralArray_1)
856 {
857 AbckitFile *file = nullptr;
858 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", &file);
859 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
860 helpers::TransformMethod(file, "test", [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
861 auto *constInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
862 auto arr = std::vector<AbckitLiteral *>();
863 AbckitLiteral *res1 = g_implM->createLiteralString(file, "asdf", strlen("asdf"));
864 AbckitLiteral *res2 = g_implM->createLiteralDouble(file, 1.0);
865 arr.emplace_back(res1);
866 arr.emplace_back(res2);
867 auto litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
868 g_implG->iSetLiteralArray(constInst, litArr);
869 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
870 });
871 g_impl->closeFile(file);
872 }
873
874 // Test: test-kind=api, api=GraphApiImpl::iSetLiteralArray, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IsetLiteralArray_2)875 TEST_F(LibAbcKitIrInstTest, IsetLiteralArray_2)
876 {
877 AbckitFile *file = nullptr;
878 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", &file);
879 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
880 helpers::TransformMethod(
881 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
882 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc", "test",
883 [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
884 auto *start = g_implG->gGetStartBasicBlock(graph);
885 auto *bb = g_implG->bbGetSuccBlock(start, 0);
886 auto *inst = g_implG->bbGetFirstInst(bb);
887 auto arr = std::vector<AbckitLiteral *>();
888 AbckitLiteral *res1 = g_implM->createLiteralString(file, "asdf", strlen("asdf"));
889 AbckitLiteral *res2 = g_implM->createLiteralDouble(file, 1.0);
890 arr.emplace_back(res1);
891 arr.emplace_back(res2);
892 auto litArr = g_implM->createLiteralArray(file, arr.data(), arr.size());
893 g_implG->iSetLiteralArray(inst, litArr);
894 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
895 },
896 [&]([[maybe_unused]] AbckitGraph *graph) {});
897 g_impl->closeFile(file);
898 auto output =
899 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
900 "inst_manipulation_dynamic");
901 EXPECT_TRUE(helpers::Match(output, "asdf\n1\nasdf\n4 5 6 7 undefined\n"));
902 }
903
904 // Test: test-kind=api, api=GraphApiImpl::iGetLiteralArray, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IgetLiteralArray_1)905 TEST_F(LibAbcKitIrInstTest, IgetLiteralArray_1)
906 {
907 AbckitFile *file = nullptr;
908 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", &file);
909 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
910 helpers::TransformMethod(file, "test",
911 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
912 auto *constInst = g_implG->gFindOrCreateConstantI64(graph, 1U);
913 g_implG->iGetLiteralArray(constInst);
914 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
915 });
916 g_impl->closeFile(file);
917 }
918
919 // Test: test-kind=api, api=GraphApiImpl::iGetLiteralArray, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetLiteralArray_2)920 TEST_F(LibAbcKitIrInstTest, IgetLiteralArray_2)
921 {
922 AbckitFile *file = nullptr;
923 helpers::AssertOpenAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", &file);
924 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
925 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
926 auto *start = g_implG->gGetStartBasicBlock(graph);
927 auto *bb = g_implG->bbGetSuccBlock(start, 0);
928 auto *inst = g_implG->bbGetFirstInst(bb);
929 auto *litArr = g_implG->iGetLiteralArray(inst);
930
931 // Test that iGetLiteralArray returns same pointer for same instruction
932 auto *litArr2 = g_implG->iGetLiteralArray(inst);
933 ASSERT_EQ(litArr, litArr2);
934
935 uint32_t counter = 0;
936 g_implI->literalArrayEnumerateElements(
937 litArr, &counter, [](AbckitFile * /*file*/, AbckitLiteral *lit, void *data) {
938 if (*(reinterpret_cast<uint32_t *>(data)) == 2U) {
939 auto *str = g_implI->literalGetString(lit);
940 EXPECT_TRUE(std::string_view(helpers::AbckitStringToString(str)) == "aa");
941 return true;
942 }
943 // CC-OFFNXT(G.FMT.02)
944 if (*(reinterpret_cast<uint32_t *>(data)) == 3U) {
945 auto val = g_implI->literalGetU32(lit);
946 EXPECT_TRUE(val == 10U);
947 return true;
948 }
949 // CC-OFFNXT(G.FMT.02)
950 (*(reinterpret_cast<uint32_t *>(data)))++;
951 return true;
952 });
953 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
954 };
955 helpers::TransformMethod(file, "test", cb);
956 g_impl->closeFile(file);
957 }
958
959 // Test: test-kind=api, api=GraphApiImpl::iGetId, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetId_1)960 TEST_F(LibAbcKitIrInstTest, IgetId_1)
961 {
962 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
963 auto *start = g_implG->gGetStartBasicBlock(graph);
964 auto *bb = g_implG->bbGetSuccBlock(start, 0);
965 auto *inst = g_implG->bbGetFirstInst(bb);
966 auto id = g_implG->iGetId(inst);
967 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
968 ASSERT_EQ(id, 3);
969 };
970 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test3", cb);
971 }
972
973 // Test: test-kind=api, api=GraphApiImpl::iGetId, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetId_2)974 TEST_F(LibAbcKitIrInstTest, IgetId_2)
975 {
976 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
977 auto *start = g_implG->gGetStartBasicBlock(graph);
978 auto *bb = g_implG->bbGetSuccBlock(start, 0);
979 auto *inst = g_implG->bbGetFirstInst(bb);
980 auto id = g_implG->iGetId(inst);
981 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
982
983 std::vector<AbckitBasicBlock *> bbs;
984 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
985 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
986 return true;
987 });
988 // CC-OFFNXT(G.FMT.02)
989 for (auto *bb : bbs) {
990 auto *curInst = g_implG->bbGetFirstInst(bb);
991 while (curInst != nullptr) {
992 auto idCurr = g_implG->iGetId(curInst);
993 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
994 ASSERT_TRUE((curInst == inst) || (idCurr != id));
995 curInst = g_implG->iGetNext(curInst);
996 }
997 }
998 };
999 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test3", cb);
1000 }
1001
1002 // Test: test-kind=api, api=GraphApiImpl::iGetId, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetId_3)1003 TEST_F(LibAbcKitIrInstTest, IgetId_3)
1004 {
1005 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1006 auto *start = g_implG->gGetStartBasicBlock(graph);
1007 auto *bb = g_implG->bbGetSuccBlock(start, 0);
1008 auto *inst = g_implG->bbGetFirstInst(bb);
1009 auto id = g_implG->iGetId(inst);
1010 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1011 ASSERT_EQ(id, 3);
1012 };
1013 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func_main_0",
1014 cb);
1015 }
1016
1017 // Test: test-kind=api, api=GraphApiImpl::iGetId, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetId_4)1018 TEST_F(LibAbcKitIrInstTest, IgetId_4)
1019 {
1020 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1021 auto *start = g_implG->gGetStartBasicBlock(graph);
1022 auto *bb = g_implG->bbGetSuccBlock(start, 0);
1023 auto *inst = g_implG->bbGetFirstInst(bb);
1024 auto id = g_implG->iGetId(inst);
1025 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1026
1027 std::vector<AbckitBasicBlock *> bbs;
1028 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
1029 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
1030 return true;
1031 });
1032 for (auto *bb : bbs) {
1033 auto *curInst = g_implG->bbGetFirstInst(bb);
1034 while (curInst != nullptr) {
1035 auto idCurr = g_implG->iGetId(curInst);
1036 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1037 ASSERT_TRUE((curInst == inst) || (idCurr != id));
1038 curInst = g_implG->iGetNext(curInst);
1039 }
1040 }
1041 };
1042 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func_main_0",
1043 cb);
1044 }
1045
1046 // Test: test-kind=api, api=GraphApiImpl::iGetFunction, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetFunction_1)1047 TEST_F(LibAbcKitIrInstTest, IgetFunction_1)
1048 {
1049 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1050 AbckitInst *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_VIRTUAL);
1051 // CC-OFFNXT(G.FMT.02)
1052 AbckitCoreFunction *currMethod = g_implG->iGetFunction(call);
1053 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1054 auto name = g_implI->functionGetName(currMethod);
1055 auto nameStr = helpers::AbckitStringToString(name);
1056 ASSERT_EQ(nameStr, "test:inst_manipulation_static.Test;void;");
1057 AbckitInst *call2 = g_implG->iGetNext(call);
1058 AbckitCoreFunction *currMethod2 = g_implG->iGetFunction(call2);
1059 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1060 auto name2 = g_implI->functionGetName(currMethod2);
1061 auto nameStr2 = helpers::AbckitStringToString(name2);
1062 ASSERT_EQ(nameStr2, "test2:inst_manipulation_static.Test;i32;i32;");
1063 };
1064 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "main", cb);
1065 }
1066
1067 // Test: test-kind=api, api=GraphApiImpl::iGetFunction, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IgetFunction_2)1068 TEST_F(LibAbcKitIrInstTest, IgetFunction_2)
1069 {
1070 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1071 auto defInst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
1072 AbckitCoreFunction *currMethod = g_implG->iGetFunction(defInst);
1073 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1074 auto name = g_implI->functionGetName(currMethod);
1075 auto nameStr = helpers::AbckitStringToString(name);
1076 ASSERT_EQ(nameStr, "func");
1077 auto defInst2 = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINECLASSWITHBUFFER);
1078 // CC-OFFNXT(G.FMT.02)
1079 AbckitCoreFunction *currMethod2 = g_implG->iGetFunction(defInst2);
1080 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1081 auto name2 = g_implI->functionGetName(currMethod2);
1082 auto nameStr2 = helpers::AbckitStringToString(name2);
1083 ASSERT_EQ(nameStr2, "Test");
1084 };
1085 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func_main_0",
1086 cb);
1087 }
1088
1089 // Test: test-kind=api, api=GraphApiImpl::iGetFunction, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,IgetFunction_3)1090 TEST_F(LibAbcKitIrInstTest, IgetFunction_3)
1091 {
1092 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1093 auto *start = g_implG->gGetStartBasicBlock(graph);
1094 auto *firstInst = g_implG->bbGetFirstInst(start);
1095 [[maybe_unused]] AbckitCoreFunction *currMethod = g_implG->iGetFunction(firstInst);
1096 // CC-OFFNXT(G.FMT.02)
1097 ASSERT_NE(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1098 ASSERT_EQ(currMethod, nullptr);
1099 };
1100 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "test3", cb);
1101 }
1102
1103 // Test: test-kind=api, api=GraphApiImpl::iGetFunction, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IgetFunction_4)1104 TEST_F(LibAbcKitIrInstTest, IgetFunction_4)
1105 {
1106 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1107 auto *start = g_implG->gGetStartBasicBlock(graph);
1108 auto *firstInst = g_implG->bbGetFirstInst(start);
1109 [[maybe_unused]] AbckitCoreFunction *currMethod = g_implG->iGetFunction(firstInst);
1110 ASSERT_NE(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1111 ASSERT_EQ(currMethod, nullptr);
1112 };
1113 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func_main_0",
1114 cb);
1115 }
1116
1117 // Test: test-kind=api, api=GraphApiImpl::iRemove, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,Iremove_1)1118 TEST_F(LibAbcKitIrInstTest, Iremove_1)
1119 {
1120 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1121 "inst_manipulation_static/ETSGLOBAL", "main");
1122 EXPECT_TRUE(helpers::Match(output, "test\ntest2\n1 2 3 4 5 0\n"));
1123 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1124 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1125 "main", [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1126 auto *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_VIRTUAL);
1127 g_implG->iRemove(call);
1128 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1129 });
1130 output =
1131 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1132 "inst_manipulation_static/ETSGLOBAL", "main");
1133 EXPECT_TRUE(helpers::Match(output, "test2\n1 2 3 4 5 0\n"));
1134 }
1135
1136 // Test: test-kind=api, api=GraphApiImpl::iRemove, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,Iremove_2)1137 TEST_F(LibAbcKitIrInstTest, Iremove_2)
1138 {
1139 auto output = helpers::ExecuteDynamicAbc(
1140 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "inst_manipulation_dynamic");
1141 EXPECT_TRUE(helpers::Match(output, "aa\n10\naa\n4 5 6 7 undefined\n"));
1142 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1143 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1144 "func_main_0",
1145 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1146 auto *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
1147 g_implG->iRemove(call);
1148 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1149 });
1150 output =
1151 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1152 "inst_manipulation_dynamic");
1153 EXPECT_TRUE(helpers::Match(output, "10\naa\n4 5 6 7 undefined\n"));
1154 }
1155
1156 // Test: test-kind=api, api=GraphApiImpl::iRemove, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,Iremove_3)1157 TEST_F(LibAbcKitIrInstTest, Iremove_3)
1158 {
1159 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1160 "inst_manipulation_static/ETSGLOBAL", "main");
1161 EXPECT_TRUE(helpers::Match(output, "test\ntest2\n1 2 3 4 5 0\n"));
1162 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1163 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1164 "main", [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1165 auto *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_VIRTUAL);
1166 g_implG->iRemove(call);
1167 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1168 g_implG->iRemove(call);
1169 ASSERT_NE(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1170 });
1171 output =
1172 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1173 "inst_manipulation_static/ETSGLOBAL", "main");
1174 EXPECT_TRUE(helpers::Match(output, "test2\n1 2 3 4 5 0\n"));
1175 }
1176
1177 // Test: test-kind=api, api=GraphApiImpl::iRemove, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,Iremove_4)1178 TEST_F(LibAbcKitIrInstTest, Iremove_4)
1179 {
1180 auto output = helpers::ExecuteDynamicAbc(
1181 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "inst_manipulation_dynamic");
1182 EXPECT_TRUE(helpers::Match(output, "aa\n10\naa\n4 5 6 7 undefined\n"));
1183 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1184 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1185 "func_main_0",
1186 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1187 auto *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
1188 g_implG->iRemove(call);
1189 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1190 g_implG->iRemove(call);
1191 ASSERT_NE(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1192 });
1193 output =
1194 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1195 "inst_manipulation_dynamic");
1196 EXPECT_TRUE(helpers::Match(output, "10\naa\n4 5 6 7 undefined\n"));
1197 }
1198
1199 // Test: test-kind=api, api=GraphApiImpl::iRemove, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IcreateCallrange)1200 TEST_F(LibAbcKitIrInstTest, IcreateCallrange)
1201 {
1202 auto output = helpers::ExecuteDynamicAbc(
1203 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "inst_manipulation_dynamic");
1204 EXPECT_TRUE(helpers::Match(output, "aa\n10\naa\n4 5 6 7 undefined\n"));
1205 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1206 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1207 "func_main_0",
1208 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1209 auto *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
1210 g_implG->iRemove(call);
1211 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1212 g_implG->iRemove(call);
1213 ASSERT_NE(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1214 });
1215 output =
1216 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1217 "inst_manipulation_dynamic");
1218 EXPECT_TRUE(helpers::Match(output, "10\naa\n4 5 6 7 undefined\n"));
1219 }
1220
1221 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IappendInput_1)1222 TEST_F(LibAbcKitIrInstTest, IappendInput_1)
1223 {
1224 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1225 "inst_manipulation_static/ETSGLOBAL", "main");
1226 EXPECT_TRUE(helpers::Match(output, "test\ntest2\n1 2 3 4 5 0\n"));
1227 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1228 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1229 "main", [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1230 auto *call = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
1231 auto *found = helpers::FindMethodByName(file, "test0:i32;void;");
1232 ASSERT_NE(found, nullptr);
1233 g_implG->iSetFunction(call, found);
1234 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 6);
1235 g_implG->iAppendInput(call, const1);
1236 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1237 });
1238 output =
1239 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1240 "inst_manipulation_static/ETSGLOBAL", "main");
1241 EXPECT_TRUE(helpers::Match(output, "test\ntest2\ntest0 6\n1 2 3 4 5 0\n"));
1242 }
1243
1244 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IappendInput_2)1245 TEST_F(LibAbcKitIrInstTest, IappendInput_2)
1246 {
1247 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1248 "inst_manipulation_static/ETSGLOBAL", "main");
1249 auto cb = [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1250 auto *call = helpers::FindLastInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_VIRTUAL);
1251 auto *found = helpers::FindMethodByName(file, "test2:inst_manipulation_static.Test;i32;i32;");
1252 // CC-OFFNXT(G.FMT.02)
1253 ASSERT_NE(found, nullptr);
1254 g_implG->iSetFunction(call, found);
1255 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 6);
1256 g_implG->iAppendInput(call, const1);
1257 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1258 };
1259 EXPECT_TRUE(helpers::Match(output, "test\ntest2\n1 2 3 4 5 0\n"));
1260 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1261 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1262 "main", cb);
1263 output =
1264 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1265 "inst_manipulation_static/ETSGLOBAL", "main");
1266 EXPECT_TRUE(helpers::Match(output, "test\ntest2\ntest2\n1 2 3 4 5 0\n"));
1267 }
1268
1269 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IappendInput_3)1270 TEST_F(LibAbcKitIrInstTest, IappendInput_3)
1271 {
1272 auto output = helpers::ExecuteDynamicAbc(
1273 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "inst_manipulation_dynamic");
1274 EXPECT_TRUE(helpers::Match(output, "aa\n10\naa\n4 5 6 7 undefined\n"));
1275 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1276 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1277 "func_main_0",
1278 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1279 auto *call = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLRANGE);
1280 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1281 g_implG->iSetImmediate(call, 1, 5);
1282 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1283 g_implG->iAppendInput(call, const1);
1284 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1285 });
1286 output =
1287 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1288 "inst_manipulation_dynamic");
1289 EXPECT_TRUE(helpers::Match(output, "aa\n10\naa\n4 5 6 7 8\n"));
1290 }
1291
1292 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,IappendInput_4)1293 TEST_F(LibAbcKitIrInstTest, IappendInput_4)
1294 {
1295 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1296 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1297 "main", [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1298 auto *start = g_implG->gGetStartBasicBlock(graph);
1299 auto *inst = g_implG->bbGetFirstInst(start);
1300 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1301 g_implG->iAppendInput(inst, const1);
1302 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1303 });
1304 }
1305
1306 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitIrInstTest,IappendInput_5)1307 TEST_F(LibAbcKitIrInstTest, IappendInput_5)
1308 {
1309 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1310 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1311 "main", [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1312 auto *inst = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_LOADSTRING);
1313 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1314 g_implG->iAppendInput(inst, const1);
1315 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1316 });
1317 }
1318
1319 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IappendInput_6)1320 TEST_F(LibAbcKitIrInstTest, IappendInput_6)
1321 {
1322 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1323 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1324 "func_main_0",
1325 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1326 auto *start = helpers::BBgetSuccBlocks(g_implG->gGetStartBasicBlock(graph))[0];
1327 auto *inst = g_implG->bbGetFirstInst(start);
1328 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1329 g_implG->iAppendInput(inst, const1);
1330 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1331 });
1332 }
1333
1334 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IappendInput_7)1335 TEST_F(LibAbcKitIrInstTest, IappendInput_7)
1336 {
1337 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1338 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1339 "func_main_0",
1340 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1341 auto *start = g_implG->gGetStartBasicBlock(graph);
1342 auto *inst = g_implG->bbGetFirstInst(start);
1343 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1344 g_implG->iAppendInput(inst, const1);
1345 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1346 });
1347 }
1348
1349 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS1, category=negative
TEST_F(LibAbcKitIrInstTest,IappendInput_8)1350 TEST_F(LibAbcKitIrInstTest, IappendInput_8)
1351 {
1352 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc",
1353 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic_modified.abc",
1354 "func_main_0",
1355 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1356 auto *call = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS0);
1357 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1358 g_implG->iAppendInput(call, const1);
1359 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
1360 });
1361 }
1362
1363 // Test: test-kind=api, api=GraphApiImpl::iAppendInput, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IappendInput_9)1364 TEST_F(LibAbcKitIrInstTest, IappendInput_9)
1365 {
1366 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1367 "inst_manipulation_static/ETSGLOBAL", "main");
1368 EXPECT_TRUE(helpers::Match(output, "test\ntest2\n1 2 3 4 5 0\n"));
1369 auto cb = [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1370 [[maybe_unused]] auto *start = g_implG->gGetStartBasicBlock(graph);
1371 // CC-OFFNXT(G.FMT.02)
1372 auto *call = helpers::FindLastInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_INITOBJECT);
1373 auto *found =
1374 helpers::FindMethodByName(file, "_ctor_:inst_manipulation_static.Test;i32;i32;i32;i32;i32;i32;void;");
1375 ASSERT_NE(found, nullptr);
1376 g_implG->iSetFunction(call, found);
1377 auto *const1 = g_implG->gFindOrCreateConstantI32(graph, 8);
1378 g_implG->iAppendInput(call, const1);
1379 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1380 };
1381 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc",
1382 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1383 "main", cb);
1384 output =
1385 helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static_modified.abc",
1386 "inst_manipulation_static/ETSGLOBAL", "main");
1387 EXPECT_TRUE(helpers::Match(output, "test\ntest2\n1 2 3 4 5 8\n"));
1388 }
1389
1390 // Test: test-kind=api, api=GraphApiImpl::iCheckIsCall, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IcheckIsCall_1)1391 TEST_F(LibAbcKitIrInstTest, IcheckIsCall_1)
1392 {
1393 auto cb = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1394 std::vector<AbckitBasicBlock *> bbs;
1395 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
1396 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
1397 return true;
1398 });
1399 // CC-OFFNXT(G.FMT.02)
1400 std::set<AbckitIsaApiDynamicOpcode> callInsns = {
1401 ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1,
1402 ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLRANGE,
1403 ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLTHIS0,
1404 };
1405 for (auto *bb : bbs) {
1406 if (g_implG->bbIsStart(bb)) {
1407 continue;
1408 }
1409 // CC-OFFNXT(G.FMT.02)
1410 auto *curInst = g_implG->bbGetFirstInst(bb);
1411 while (curInst != nullptr) {
1412 auto isCall = g_implG->iCheckIsCall(curInst);
1413 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1414 ASSERT_EQ(isCall, callInsns.find(g_dynG->iGetOpcode(curInst)) != callInsns.end());
1415 // CC-OFFNXT(G.FMT.02)
1416 curInst = g_implG->iGetNext(curInst);
1417 }
1418 }
1419 };
1420 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func_main_0",
1421 cb);
1422 }
1423
1424 // Test: test-kind=api, api=GraphApiImpl::iCheckIsCall, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,IcheckIsCall_2)1425 TEST_F(LibAbcKitIrInstTest, IcheckIsCall_2)
1426 {
1427 helpers::InspectMethod(
1428 ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "main",
1429 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1430 std::vector<AbckitBasicBlock *> bbs;
1431 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
1432 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
1433 return true;
1434 });
1435 std::set<AbckitIsaApiStaticOpcode> callInsns = {ABCKIT_ISA_API_STATIC_OPCODE_INITOBJECT,
1436 ABCKIT_ISA_API_STATIC_OPCODE_CALL_VIRTUAL,
1437 ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC};
1438 for (auto *bb : bbs) {
1439 if (g_implG->bbIsStart(bb)) {
1440 continue;
1441 }
1442 auto *curInst = g_implG->bbGetFirstInst(bb);
1443 while (curInst != nullptr) {
1444 auto isCall = g_implG->iCheckIsCall(curInst);
1445 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1446 ASSERT_EQ(isCall, callInsns.find(g_statG->iGetOpcode(curInst)) != callInsns.end());
1447 curInst = g_implG->iGetNext(curInst);
1448 }
1449 }
1450 });
1451 }
1452
CompareDump1(std::stringstream & ss)1453 static void CompareDump1(std::stringstream &ss)
1454 {
1455 auto expected =
1456 "\\s+0.any Parameter\\s+arg 0\\s+\n"
1457 "r8187 -> r8187 \\[u64\\]\n"
1458 "\\s+1.any Parameter\\s+arg 1\\s+\n"
1459 "r8188 -> r8188 \\[u64\\]\n"
1460 "\\s+2.any Parameter\\s+arg 2\\s+\n"
1461 "r8189 -> r8189 \\[u64\\]\n"
1462 "\\s+12.i32 Constant\\s+0x0 -> \\(v13\\)\\s+\n"
1463 "\\s+18.i32 Constant\\s+0x1 -> \\(v19\\)\\s+\n"
1464 "\\s+22.i32 Constant\\s+0x4 -> \\(v26\\)\\s+\n"
1465 "\\s+23.i32 Constant\\s+0x5 -> \\(v26\\)\\s+\n"
1466 "\\s+24.i32 Constant\\s+0x6 -> \\(v26\\)\\s+\n"
1467 "\\s+25.i32 Constant\\s+0x7 -> \\(v26\\)\\s+\n"
1468 "\\s+3.any Intrinsic.definefunc\\s+0x[a-f0-9]+, 0x[a-f0-9]+, 0x1 \\(v21\\)\\s+bc: 0x[a-f0-9]+\n"
1469 "\\s+4.any Intrinsic.definefunc\\s+0x[a-f0-9]+, 0x[a-f0-9]+, 0x5 \\(v26\\)\\s+bc: 0x[a-f0-9]+\n"
1470 "\\s+5.any Intrinsic.ldhole\\s+\\(v6\\)\\s+bc: 0x[a-f0-9]+\n"
1471 "\\s+6.any Intrinsic.defineclasswithbuffer 0x[a-f0-9]+, 0x[a-f0-9]+, "
1472 // CC-OFFNXT(WordsTool.190)
1473 "0x[a-f0-9]+, 0x[a-f0-9]+ v5 -> \\(v8, v7\\)\\s+bc: 0x[a-f0-9]+\n"
1474 "\\s+7.any Intrinsic.ldobjbyname\\s+0x[a-f0-9]+, 0x[a-f0-9]+ v6\\s+bc: 0x[a-f0-9]+\n"
1475 "\\s+8.any Intrinsic.newobjrange\\s+0x5, 0x[a-f0-9]+ v6 -> "
1476 "\\(v21, v17, v16, v11, v10\\)\\s+bc: 0x[a-f0-9]+\n"
1477 "\\s+9.any Intrinsic.tryldglobalbyname 0x[a-f0-9]+, 0x[a-f0-9]+ \\(v14\\)\\s+bc: 0x[a-f0-9]+\n"
1478 // CC-OFFNXT(WordsTool.190)
1479 "\\s+10.any Intrinsic.ldobjbyname\\s+0x[a-f0-9]+, 0x[a-f0-9]+ v8 -> \\(v11\\)\\s+bc: 0x[a-f0-9]+\n"
1480 // CC-OFFNXT(WordsTool.190)
1481 "\\s+11.any Intrinsic.callthis0\\s+0x[a-f0-9]+ v10, v8 -> \\(v13\\)\\s+bc: 0x[a-f0-9]+\n"
1482 "\\s+13.any Intrinsic.ldobjbyvalue\\s+0x[a-f0-9]+ v12, v11 -> \\(v14\\)\\s+bc: 0x[a-f0-9]+\n"
1483 "\\s+14.any Intrinsic.callarg1\\s+0x[a-f0-9]+ v9, v13\\s+bc: 0x[a-f0-9]+\n"
1484 "\\s+15.any Intrinsic.tryldglobalbyname 0x[a-f0-9]+, 0x[a-f0-9]+ \\(v20\\)\\s+bc: 0x[a-f0-9]+\n"
1485 // CC-OFFNXT(WordsTool.190)
1486 "\\s+16.any Intrinsic.ldobjbyname\\s+0x[a-f0-9]+, 0x[a-f0-9]+ v8 -> \\(v17\\)\\s+bc: 0x[a-f0-9]+\n"
1487 // CC-OFFNXT(WordsTool.190)
1488 "\\s+17.any Intrinsic.callthis0\\s+0x[a-f0-9]+ v16, v8 -> \\(v19\\)\\s+bc: 0x[a-f0-9]+\n"
1489 "\\s+19.any Intrinsic.ldobjbyvalue\\s+0x[a-f0-9]+ v18, v17 -> \\(v20\\)\\s+bc: 0x[a-f0-9]+\n"
1490 "\\s+20.any Intrinsic.callarg1\\s+0x[a-f0-9]+ v15, v19\\s+bc: 0x[a-f0-9]+\n"
1491 // CC-OFFNXT(WordsTool.190)
1492 "\\s+21.any Intrinsic.callarg1\\s+0x[a-f0-9]+ v3, v8\\s+bc: 0x[a-f0-9]+\n"
1493 "\\s+26.any Intrinsic.callrange\\s+0x[a-f0-9]+, 0x4 v4, v22, v23, v24, v25\\s+bc: 0x[a-f0-9]+\n"
1494 "\\s+27.void Intrinsic.returnundefined\\s+bc: 0x[a-f0-9]+\n\n";
1495 EXPECT_TRUE(helpers::Match(ss.str(), expected));
1496 }
1497
1498 // Test: test-kind=api, api=GraphApiImpl::iDump, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,Idump_1)1499 TEST_F(LibAbcKitIrInstTest, Idump_1)
1500 {
1501 auto userInspector = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1502 auto fileName = ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/tmpID.txt";
1503 std::FILE *fp = fopen(fileName, "we");
1504 ASSERT_NE(fp, nullptr);
1505 auto fd = fileno(fp);
1506 std::vector<AbckitBasicBlock *> bbs;
1507 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
1508 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
1509 return true;
1510 });
1511 for (auto *bb : bbs) {
1512 auto *curInst = g_implG->bbGetFirstInst(bb);
1513 while (curInst != nullptr) {
1514 g_implG->iDump(curInst, fd);
1515 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1516 curInst = g_implG->iGetNext(curInst);
1517 }
1518 }
1519 ASSERT_EQ(fclose(fp), 0);
1520 std::ifstream tmpFile(fileName);
1521 std::string line;
1522 std::stringstream ss;
1523 ASSERT_TRUE(tmpFile.is_open());
1524 while (tmpFile.good()) {
1525 getline(tmpFile, line);
1526 ss << line << std::endl;
1527 }
1528 tmpFile.close();
1529 std::remove(fileName);
1530
1531 CompareDump1(ss);
1532 };
1533 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_dynamic.abc", "func_main_0",
1534 userInspector);
1535 }
1536
CompareDump2(std::stringstream & ss)1537 static void CompareDump2(std::stringstream &ss)
1538 {
1539 auto expected =
1540 "\\s+2.i32 Constant\\s+0x1 -> \\(v12, v3\\)\\s+\n"
1541 "\\s+8.i32 Constant\\s+0x2 -> \\(v12\\)\\s+\n"
1542 "\\s+9.i32 Constant\\s+0x3 -> \\(v12\\)\\s+\n"
1543 "\\s+10.i32 Constant\\s+0x4 -> \\(v12\\)\\s+\n"
1544 "\\s+11.i32 Constant\\s+0x5 -> \\(v12\\)\\s+\n"
1545 "\\s+0.ref Intrinsic.AbckitInitObjectShort 0x[a-f0-9]+ \\(v7, v3, v1\\)\\s+bc: 0x[a-f0-9]+\n"
1546 "\\s+1.void CallVirtual [a-f0-9]+ inst_manipulation_static.Test::test v0\\s+bc: 0x[a-f0-9]+\n"
1547 "\\s+3.i32 CallVirtual [a-f0-9]+ inst_manipulation_static.Test::test2 v0, v2\\s+bc: 0x[a-f0-9]+\n"
1548 "\\s+4.void CallStatic [a-f0-9]+ inst_manipulation_static.ETSGLOBAL::test0\\s+bc: 0x[a-f0-9]+\n"
1549 "\\s+5.ref Intrinsic.AbckitLoadString\\s+0x[a-f0-9]+ \\(v6\\)\\s+bc: 0x[a-f0-9]+\n"
1550 "\\s+6.void CallStatic [a-f0-9]+ inst_manipulation_static.ETSGLOBAL::test1 v5\\s+bc: 0x[a-f0-9]+\n"
1551 "\\s+7.void CallVirtual [a-f0-9]+ inst_manipulation_static.Test::test2 v0\\s+bc: 0x[a-f0-9]+\n"
1552 // CC-OFFNXT(WordsTool.190)
1553 "\\s+12.ref Intrinsic.AbckitInitObjectRange 0x[a-f0-9]+ v2, v8, v9, v10, v11\\s+bc: 0x[a-f0-9]+\n"
1554 "\\s+13.void ReturnVoid\\s+bc: 0x[a-f0-9]+\n\n";
1555 EXPECT_TRUE(helpers::Match(ss.str(), expected));
1556 }
1557
1558 // Test: test-kind=api, api=GraphApiImpl::iDump, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitIrInstTest,Idump_2)1559 TEST_F(LibAbcKitIrInstTest, Idump_2)
1560 {
1561 auto userInspector = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
1562 auto fileName = ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/tmpIS.txt";
1563 std::FILE *fp = fopen(fileName, "we");
1564 ASSERT_NE(fp, nullptr);
1565 auto fd = fileno(fp);
1566 std::vector<AbckitBasicBlock *> bbs;
1567 g_implG->gVisitBlocksRpo(graph, &bbs, [](AbckitBasicBlock *bb, void *data) {
1568 reinterpret_cast<std::vector<AbckitBasicBlock *> *>(data)->emplace_back(bb);
1569 return true;
1570 });
1571 for (auto *bb : bbs) {
1572 auto *curInst = g_implG->bbGetFirstInst(bb);
1573 while (curInst != nullptr) {
1574 g_implG->iDump(curInst, fd);
1575 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
1576 curInst = g_implG->iGetNext(curInst);
1577 }
1578 }
1579 ASSERT_TRUE(fclose(fp) == 0);
1580 std::ifstream tmpFile(fileName);
1581 std::string line;
1582 std::stringstream ss;
1583 ASSERT_TRUE(tmpFile.is_open());
1584 while (tmpFile.good()) {
1585 getline(tmpFile, line);
1586 ss << line << std::endl;
1587 }
1588 tmpFile.close();
1589 std::remove(fileName);
1590
1591 CompareDump2(ss);
1592 };
1593 helpers::InspectMethod(ABCKIT_ABC_DIR "ut/ir_core/inst_manipulation/inst_manipulation_static.abc", "main",
1594 userInspector);
1595 }
1596 } // namespace libabckit::test
1597
1598 // NOLINTEND(readability-magic-numbers)
1599