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/amd64/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 amd64::Amd64Encoder encoder(GetAllocator());
58 ASSERT_TRUE(encoder.InitMasm());
59
60 amd64::Amd64RegisterDescription regfile(GetAllocator());
61 encoder.SetRegfile(®file);
62
63 auto FloatType = FLOAT64_TYPE;
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