1 /**
2 * Copyright (c) 2021-2022 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 #include <random>
16 #include <gtest/gtest.h>
17
18 #include "mem/pool_manager.h"
19 #include "target/aarch32/target.h"
20
21 const uint64_t SEED = 0x1234;
22 #ifndef PANDA_NIGHTLY_TEST_ON
23 const uint64_t ITERATION = 20;
24 #else
25 const uint64_t ITERATION = 0xffffff;
26 #endif
27 static inline auto random_gen = std::mt19937_64(SEED);
28
29 namespace panda::compiler {
30 class Register32Test : public ::testing::Test {
31 public:
Register32Test()32 Register32Test()
33 {
34 panda::mem::MemConfig::Initialize(64_MB, 64_MB, 64_MB, 32_MB);
35 PoolManager::Initialize();
36 allocator_ = new ArenaAllocator(SpaceType::SPACE_TYPE_COMPILER);
37 }
~Register32Test()38 ~Register32Test()
39 {
40 delete allocator_;
41 PoolManager::Finalize();
42 panda::mem::MemConfig::Finalize();
43 }
44
GetAllocator()45 ArenaAllocator *GetAllocator()
46 {
47 return allocator_;
48 }
49
50 private:
51 ArenaAllocator *allocator_;
52 };
53
TEST_F(Register32Test,TmpReg)54 TEST_F(Register32Test, TmpReg)
55 {
56 aarch32::Aarch32Encoder encoder(GetAllocator());
57 encoder.InitMasm();
58
59 auto FloatType = FLOAT32_TYPE;
60
61 if (encoder.GetScratchFPRegistersCount() == 0) {
62 encoder.GetMasm()->GetScratchVRegisterList()->Combine(vixl::aarch32::SRegister(1));
63 }
64
65 auto initial_count = encoder.GetScratchRegistersCount();
66 auto initial_fp_count = encoder.GetScratchFPRegistersCount();
67 ASSERT_NE(initial_count, 0);
68 ASSERT_NE(initial_fp_count, 0);
69
70 std::vector<Reg> regs;
71 for (size_t i = 0; i < initial_count; i++) {
72 regs.push_back(encoder.AcquireScratchRegister(INT64_TYPE));
73 }
74 ASSERT_EQ(encoder.GetScratchRegistersCount(), 0);
75 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), initial_fp_count);
76 for (auto reg : regs) {
77 encoder.ReleaseScratchRegister(reg);
78 }
79 ASSERT_EQ(encoder.GetScratchRegistersCount(), initial_count);
80
81 regs.clear();
82 for (size_t i = 0; i < initial_fp_count; i++) {
83 regs.push_back(encoder.AcquireScratchRegister(FloatType));
84 }
85
86 ASSERT_EQ(encoder.GetScratchRegistersCount(), initial_count);
87 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), 0);
88 for (auto reg : regs) {
89 encoder.ReleaseScratchRegister(reg);
90 }
91 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), initial_fp_count);
92
93 {
94 ScopedTmpRegRef reg(&encoder);
95 ASSERT_EQ(encoder.GetScratchRegistersCount(), initial_count - 1);
96 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), initial_fp_count);
97 if (encoder.GetScratchRegistersCount() != 0) {
98 ScopedTmpRegU32 reg2(&encoder);
99 ASSERT_EQ(encoder.GetScratchRegistersCount(), initial_count - 2);
100 }
101 {
102 ScopedTmpReg reg2(&encoder, FloatType);
103 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), initial_fp_count - 1);
104 ASSERT_EQ(encoder.GetScratchRegistersCount(), initial_count - 1);
105 }
106 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), initial_fp_count);
107 }
108 ASSERT_EQ(encoder.GetScratchRegistersCount(), initial_count);
109 ASSERT_EQ(encoder.GetScratchFPRegistersCount(), initial_fp_count);
110 }
111 } // namespace panda::compiler
112