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