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 class LibAbcKitCreateDynGetunmappedargs : public ::testing::Test {};
36
37 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateGetunmappedargs, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynGetunmappedargs,IcreateGetunmappedargs_1)38 TEST_F(LibAbcKitCreateDynGetunmappedargs, IcreateGetunmappedargs_1)
39 {
40 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic.abc",
41 "getunmappedargs_dynamic");
42 EXPECT_TRUE(helpers::Match(output, ""));
43
44 auto cb = [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
45 auto *getunmappedargs = g_dynG->iCreateGetunmappedargs(graph);
46 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
47 auto *ldobjbyvalue =
48 g_dynG->iCreateLdobjbyvalue(graph, g_implG->gFindOrCreateConstantU64(graph, 0), getunmappedargs);
49 // CC-OFFNXT(G.FMT.02)
50 auto *stringPrint = g_implM->createString(file, "print", strlen("print"));
51 auto *tryldglobalbyname = g_dynG->iCreateTryldglobalbyname(graph, stringPrint);
52 auto *callarg1 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, ldobjbyvalue);
53 auto *returnundef = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
54 // CC-OFFNXT(G.FMT.02)
55 g_implG->iInsertBefore(getunmappedargs, returnundef);
56 g_implG->iInsertBefore(ldobjbyvalue, returnundef);
57 g_implG->iInsertBefore(tryldglobalbyname, returnundef);
58 g_implG->iInsertBefore(callarg1, returnundef);
59 };
60
61 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic.abc",
62 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic_modified.abc",
63 "func1", cb);
64
65 output = helpers::ExecuteDynamicAbc(
66 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic_modified.abc", "getunmappedargs_dynamic");
67 EXPECT_TRUE(helpers::Match(output, "1\n"));
68 }
69
70 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateGetunmappedargs, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynGetunmappedargs,IcreateGetunmappedargs_2)71 TEST_F(LibAbcKitCreateDynGetunmappedargs, IcreateGetunmappedargs_2)
72 {
73 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic.abc",
74 "getunmappedargs_dynamic");
75 EXPECT_TRUE(helpers::Match(output, ""));
76
77 helpers::TransformMethod(
78 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic.abc",
79 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic_modified.abc", "func2",
80 [&](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
81 auto *getunmappedargs = g_dynG->iCreateGetunmappedargs(graph);
82 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
83 auto *stringPrint = g_implM->createString(file, "print", strlen("print"));
84 auto *tryldglobalbyname = g_dynG->iCreateTryldglobalbyname(graph, stringPrint);
85 auto *ldobjbyvalue0 =
86 g_dynG->iCreateLdobjbyvalue(graph, g_implG->gFindOrCreateConstantU64(graph, 0), getunmappedargs);
87 auto *ldobjbyvalue1 =
88 g_dynG->iCreateLdobjbyvalue(graph, g_implG->gFindOrCreateConstantU64(graph, 1), getunmappedargs);
89 auto *ldobjbyvalue2 =
90 g_dynG->iCreateLdobjbyvalue(graph, g_implG->gFindOrCreateConstantU64(graph, 2), getunmappedargs);
91 auto *callarg10 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, ldobjbyvalue0);
92 auto *callarg11 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, ldobjbyvalue1);
93 auto *callarg12 = g_dynG->iCreateCallarg1(graph, tryldglobalbyname, ldobjbyvalue2);
94
95 auto *returnundef = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED);
96 g_implG->iInsertBefore(getunmappedargs, returnundef);
97 g_implG->iInsertBefore(tryldglobalbyname, returnundef);
98 g_implG->iInsertBefore(ldobjbyvalue0, returnundef);
99 g_implG->iInsertBefore(ldobjbyvalue1, returnundef);
100 g_implG->iInsertBefore(ldobjbyvalue2, returnundef);
101 g_implG->iInsertBefore(callarg10, returnundef);
102 g_implG->iInsertBefore(callarg11, returnundef);
103 g_implG->iInsertBefore(callarg12, returnundef);
104 });
105
106 output = helpers::ExecuteDynamicAbc(
107 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getunmappedargs_dynamic_modified.abc", "getunmappedargs_dynamic");
108 EXPECT_TRUE(helpers::Match(output, "1\nabc\n3.14\n"));
109 }
110
111 } // namespace libabckit::test
112 // NOLINTEND(readability-magic-numbers)
113