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 namespace {
30
31 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36
__anon07a591420202(AbckitFile * , AbckitCoreFunction * , AbckitGraph *graph) 37 auto g_icreateGetresumemode1Lambda = [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
38 auto *param2 = helpers::FindLastInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER);
39
40 auto *bbStart = g_implG->gGetBasicBlock(graph, 5);
41 auto *bbEnd = g_implG->gGetBasicBlock(graph, 7);
42
43 auto *bbResume = g_implG->bbCreateEmpty(graph);
44 auto *bbEq = g_implG->bbCreateEmpty(graph);
45 auto *bbRet = g_implG->bbCreateEmpty(graph);
46 auto *bbThrow = g_implG->bbCreateEmpty(graph);
47
48 g_implG->bbDisconnectSuccBlock(bbStart, 0);
49 g_implG->bbInsertSuccBlock(bbStart, bbResume, 0);
50 g_implG->bbAppendSuccBlock(bbResume, bbEq);
51 g_implG->bbAppendSuccBlock(bbResume, bbRet);
52 g_implG->bbAppendSuccBlock(bbEq, bbEnd);
53 g_implG->bbAppendSuccBlock(bbEq, bbThrow);
54 g_implG->bbAppendSuccBlock(bbRet, g_implG->gGetEndBasicBlock(graph));
55 g_implG->bbAppendSuccBlock(bbThrow, g_implG->gGetEndBasicBlock(graph));
56
57 auto *add2 = g_dynG->iCreateAdd2(graph, g_implG->gFindOrCreateConstantU64(graph, 7), param2);
58 auto *ldfalse = g_dynG->iCreateLdfalse(graph);
59 auto *createiterresultobj = g_dynG->iCreateCreateiterresultobj(graph, add2, ldfalse);
60 auto *creategeneratorobj = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEGENERATOROBJ);
61 auto *suspendgenerator = g_dynG->iCreateSuspendgenerator(graph, createiterresultobj, creategeneratorobj);
62 auto *resumegenerator = g_dynG->iCreateResumegenerator(graph, creategeneratorobj);
63 auto *getresumemode = g_dynG->iCreateGetresumemode(graph, creategeneratorobj);
64 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
65 auto *const0 = g_implG->gFindOrCreateConstantU64(graph, 0);
66 auto *eq = g_dynG->iCreateEq(graph, getresumemode, const0);
67 auto *ifInst = g_dynG->iCreateIf(graph, eq, ABCKIT_ISA_API_DYNAMIC_CONDITION_CODE_CC_EQ);
68
69 g_implG->bbAddInstBack(bbResume, add2);
70 g_implG->bbAddInstBack(bbResume, ldfalse);
71 g_implG->bbAddInstBack(bbResume, createiterresultobj);
72 g_implG->bbAddInstBack(bbResume, suspendgenerator);
73 g_implG->bbAddInstBack(bbResume, resumegenerator);
74 g_implG->bbAddInstBack(bbResume, getresumemode);
75 g_implG->bbAddInstBack(bbResume, eq);
76 g_implG->bbAddInstBack(bbResume, ifInst);
77
78 auto *returnInst = g_dynG->iCreateReturn(graph, resumegenerator);
79 g_implG->bbAddInstBack(bbRet, returnInst);
80
81 auto *eq2 = g_dynG->iCreateEq(graph, getresumemode, g_implG->gFindOrCreateConstantU64(graph, 1));
82 auto *ifInst2 = g_dynG->iCreateIf(graph, eq2, ABCKIT_ISA_API_DYNAMIC_CONDITION_CODE_CC_EQ);
83 g_implG->bbAddInstBack(bbEq, eq2);
84 g_implG->bbAddInstBack(bbEq, ifInst2);
85
86 auto *throwInst = g_dynG->iCreateThrow(graph, resumegenerator);
87 g_implG->bbAddInstBack(bbThrow, throwInst);
88 };
89
90 } // namespace
91
92 class LibAbcKitCreateDynGetResumeMode : public ::testing::Test {};
93
94 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateGetresumemode, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitCreateDynGetResumeMode,IcreateGetresumemode_1)95 TEST_F(LibAbcKitCreateDynGetResumeMode, IcreateGetresumemode_1)
96 {
97 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getresumemode_dynamic.abc",
98 "getresumemode_dynamic");
99 EXPECT_TRUE(helpers::Match(output, "10\nundefined\n"));
100
101 helpers::TransformMethod(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getresumemode_dynamic.abc",
102 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getresumemode_dynamic_modified.abc", "foo",
103 g_icreateGetresumemode1Lambda);
104
105 output = helpers::ExecuteDynamicAbc(
106 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/get_insts/getresumemode_dynamic_modified.abc", "getresumemode_dynamic");
107 EXPECT_TRUE(helpers::Match(output, "10\n17\n"));
108 }
109
110 } // namespace libabckit::test
111 // NOLINTEND(readability-magic-numbers)
112