1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "libabckit/include/c/metadata_core.h"
17 #include "libabckit/include/c/ir_core.h"
18 #include "libabckit/include/c/isa/isa_dynamic.h"
19 #include "libabckit/include/c/abckit.h"
20
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23 #include "libabckit/include/c/statuses.h"
24
25 #include <gtest/gtest.h>
26 #include <string_view>
27
28 // NOLINTBEGIN(readability-magic-numbers)
29 namespace libabckit::test {
30
31 namespace {
32
33 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37 auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
38
__anon9763bebe0202(AbckitFile *file, AbckitCoreFunction * , AbckitGraph *graph) 39 auto g_icreateDefineFuncTransformCb = [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
40 AbckitCoreFunction *coreFunc = nullptr;
41 g_implI->fileEnumerateModules(file, &coreFunc, [](AbckitCoreModule *module, void *data) {
42 g_implI->moduleEnumerateTopLevelFunctions(module, data, [](AbckitCoreFunction *func, void *data) {
43 if (std::string_view(g_implI->abckitStringToString(g_implI->functionGetName(func))) == "foo") {
44 *reinterpret_cast<AbckitCoreFunction **>(data) = func;
45 }
46 return true;
47 });
48 return true;
49 });
50
51 auto *newFunc = g_dynG->iCreateDefinefunc(graph, coreFunc, 0x0);
52 ASSERT_NE(newFunc, nullptr);
53 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
54
55 auto *func = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC);
56 ASSERT_NE(func, nullptr);
57
58 helpers::ReplaceInst(func, newFunc);
59 };
60
61 } // namespace
62
63 class LibAbcKitCreateDynDefineFunc : public ::testing::Test {};
64
65 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateDefinefunc, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynDefineFunc,IcreateDefinefunc)66 TEST_F(LibAbcKitCreateDynDefineFunc, IcreateDefinefunc)
67 {
68 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefunc_dynamic.abc",
69 "definefunc_dynamic");
70 EXPECT_TRUE(helpers::Match(output, "foo\nbar\n"));
71
72 auto cb = [](AbckitGraph *graph) {
73 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchema = {
74 {{},
75 {1},
76 {
77 {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
78 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
79 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
80 }},
81 {{0},
82 {2},
83 {
84 {9, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC, {}},
85 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_DEFINEFUNC, {}},
86 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG0, {4}},
87 {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG0, {9}},
88 {7, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURNUNDEFINED, {}},
89 }},
90 {{1}, {}, {}}};
91 // CC-OFFNXT(G.FMT.02)
92 helpers::VerifyGraph(graph, bbSchema);
93 };
94
95 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefunc_dynamic.abc",
96 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefunc_dynamic_modified.abc", "func_main_0",
97 g_icreateDefineFuncTransformCb, cb);
98
99 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/define/definefunc_dynamic_modified.abc",
100 "definefunc_dynamic");
101 EXPECT_TRUE(helpers::Match(output, "foo\nfoo\n"));
102 }
103
104 } // namespace libabckit::test
105 // NOLINTEND(readability-magic-numbers)
106