• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
17 #include <gtest/gtest.h>
18 
19 #include "abc2program/abc2program_driver.h"
20 #include "assembler/assembly-emitter.h"
21 #include "bytecode_optimizer/bytecode_analysis_results.h"
22 #include "bytecode_optimizer/module_constant_analyzer.h"
23 #include "bytecode_optimizer/optimize_bytecode.h"
24 #include "mem/pool_manager.h"
25 
26 using namespace testing::ext;
27 
28 namespace panda::bytecodeopt {
29 class AnalysisBytecodeTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {}
TearDownTestCase(void)32     static void TearDownTestCase(void) {}
SetUp()33     void SetUp()
34     {
35         compiler::options.SetCompilerUseSafepoint(false);
36         compiler::options.SetCompilerSupportInitObjectInst(true);
37     }
TearDown()38     void TearDown() {}
39 };
40 
41 /**
42  * @tc.name: analysis_bytecode_test_001
43  * @tc.desc: Verify the AnalysisBytecode function.
44  * @tc.type: FUNC
45  * @tc.require:
46  */
47 HWTEST_F(AnalysisBytecodeTest, analysis_bytecode_test_001, TestSize.Level1)
48 {
49     const std::string RECORD_NAME = "bytecodeAnalysis";
50     const std::string TEST_FILE_NAME = std::string(GRAPH_TEST_ABC_DIR) + RECORD_NAME + ".abc";
51     const std::unordered_set<uint32_t> const_slots = {0, 1};
52     const std::string SOURCE_RECORD_MYAPP_TEST = "myapp/test";
53     const std::string SOURCE_RECORD_NORMALIZED_MYAPP_TEST = "bundle&myapp/test&";
54     const std::string ABC_FILE_NAME = "bytecodeAnalysis.unopt.abc";
55     const std::string MOCK_CONST_NAME = "i";
56     const std::string MOCK_CONST_VALUE = "0";
57     constexpr uint32_t MOCK_SLOT = 0;
58 
59     bool exists = false;
60     auto &result = BytecodeAnalysisResults::GetOrCreateBytecodeAnalysisResult(RECORD_NAME, exists);
61     result.SetConstantLocalExportSlots(const_slots);
62     EXPECT_FALSE(exists);
63 
64     bool ignored;
65     auto &source_result =
66         BytecodeAnalysisResults::GetOrCreateBytecodeAnalysisResult(SOURCE_RECORD_MYAPP_TEST, ignored);
67     ModuleConstantAnalysisResult mock_result;
68     ConstantValue mock_value(MOCK_CONST_VALUE);
69     mock_result[0] = &mock_value;
70     source_result.SetLocalExportInfo(MOCK_SLOT, MOCK_CONST_NAME);
71     source_result.SetModuleConstantAnalysisResult(mock_result);
72 
73     auto &source_result_normalized =
74         BytecodeAnalysisResults::GetOrCreateBytecodeAnalysisResult(SOURCE_RECORD_NORMALIZED_MYAPP_TEST, ignored);
75     source_result_normalized.SetLocalExportInfo(MOCK_SLOT, MOCK_CONST_NAME);
76     source_result_normalized.SetModuleConstantAnalysisResult(mock_result);
77 
78     abc2program::Abc2ProgramDriver driver;
79     ASSERT_TRUE(driver.Compile(TEST_FILE_NAME));
80     auto &program = driver.GetProgram();
81     panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps panda_file_to_asm_maps {};
82     EXPECT_TRUE(panda::pandasm::AsmEmitter::Emit(ABC_FILE_NAME, program, nullptr, &panda_file_to_asm_maps, false));
83     EXPECT_TRUE(panda::bytecodeopt::AnalysisBytecode(const_cast<pandasm::Program *>(&program),
84                                                      &panda_file_to_asm_maps, ABC_FILE_NAME, true, false));
85 
86     ConstantValue analysis_result_value;
87 
88     // slot 0: module variable a, Constant(INT32, 1)
89     EXPECT_TRUE(BytecodeAnalysisResults::GetLocalExportConstForRecord(RECORD_NAME, 0, analysis_result_value));
90     EXPECT_TRUE(analysis_result_value == ConstantValue(static_cast<int32_t>(1)));
91 
92     // slot 1: module variable b, Constant(INT32, 2)
93     EXPECT_TRUE(BytecodeAnalysisResults::GetLocalExportConstForRecord(RECORD_NAME, 1, analysis_result_value));
94     EXPECT_TRUE(analysis_result_value == ConstantValue(static_cast<int32_t>(2)));
95 
96     // slot 0: module variable i1, MOCK_CONST_VALUE
97     EXPECT_TRUE(BytecodeAnalysisResults::GetRegularImportConstForRecord(RECORD_NAME, 0, analysis_result_value));
98     EXPECT_TRUE(analysis_result_value == ConstantValue(MOCK_CONST_VALUE));
99 
100     // slot 1: module variable i2, MOCK_CONST_VALUE
101     EXPECT_TRUE(BytecodeAnalysisResults::GetRegularImportConstForRecord(RECORD_NAME, 1, analysis_result_value));
102     EXPECT_TRUE(analysis_result_value == ConstantValue(MOCK_CONST_VALUE));
103 
104     // slot 2: module variable i3, MOCK_CONST_VALUE
105     EXPECT_TRUE(BytecodeAnalysisResults::GetRegularImportConstForRecord(RECORD_NAME, 2, analysis_result_value));
106     EXPECT_TRUE(analysis_result_value == ConstantValue(MOCK_CONST_VALUE));
107 
108     // slot 3: module variable i4, MOCK_CONST_VALUE
109     EXPECT_FALSE(BytecodeAnalysisResults::GetRegularImportConstForRecord(RECORD_NAME, 3, analysis_result_value));
110 
111     // namespace import m4.i
112     EXPECT_TRUE(BytecodeAnalysisResults::GetModuleNamespaceConstForRecord(
113         RECORD_NAME, 0, MOCK_CONST_NAME, analysis_result_value));
114     EXPECT_TRUE(analysis_result_value == ConstantValue(MOCK_CONST_VALUE));
115 }
116 }  // namespace panda::bytecodeopt