1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <optional>
20
21 #include "allocator.h"
22
23 namespace berberis {
24
25 namespace {
26
TEST(AllocatorTest,Allocator)27 TEST(AllocatorTest, Allocator) {
28 Allocator<x86_64::Assembler::Register> allocator;
29 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rbx);
30 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rsi);
31 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rdi);
32 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r8);
33 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r9);
34 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r10);
35 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r11);
36 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r12);
37 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r13);
38 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r14);
39 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r15);
40 EXPECT_EQ(allocator.Alloc(), std::nullopt);
41 }
42
TEST(AllocatorTest,AllocTemp)43 TEST(AllocatorTest, AllocTemp) {
44 Allocator<x86_64::Assembler::Register> allocator;
45 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rbx);
46 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rsi);
47 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rdi);
48 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r8);
49 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r9);
50 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r15);
51 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r14);
52 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r13);
53 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r12);
54 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r11);
55 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r10);
56 EXPECT_EQ(allocator.AllocTemp(), std::nullopt);
57 allocator.FreeTemps();
58 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r15);
59 }
60
TEST(AllocatorTest,SeparateMappedRegsAndTempRegs)61 TEST(AllocatorTest, SeparateMappedRegsAndTempRegs) {
62 Allocator<x86_64::Assembler::Register> allocator;
63 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rbx);
64 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rsi);
65 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::rdi);
66 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r8);
67 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::r9);
68 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r15);
69 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r14);
70 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r13);
71 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r12);
72 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r11);
73 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::r10);
74 }
75
TEST(AllocatorTest,SimdAllocator)76 TEST(AllocatorTest, SimdAllocator) {
77 Allocator<x86_64::Assembler::XMMRegister> allocator;
78 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm0);
79 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm1);
80 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm2);
81 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm3);
82 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm4);
83 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm5);
84 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm6);
85 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm7);
86 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm8);
87 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm9);
88 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm10);
89 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm11);
90 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm12);
91 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm13);
92 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm14);
93 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm15);
94 EXPECT_EQ(allocator.Alloc(), std::nullopt);
95 }
96
TEST(AllocatorTest,AllocSimdTemp)97 TEST(AllocatorTest, AllocSimdTemp) {
98 Allocator<x86_64::Assembler::XMMRegister> allocator;
99 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm0);
100 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm1);
101 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm2);
102 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm3);
103 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm4);
104 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm5);
105 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm6);
106 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm7);
107 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm8);
108 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm9);
109 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm10);
110 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm11);
111 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm12);
112 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::xmm15);
113 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::xmm14);
114 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm13);
115 EXPECT_EQ(allocator.Alloc(), std::nullopt);
116 allocator.FreeTemps();
117 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::xmm15);
118 }
119
TEST(ALlocatorTest,SeparateMappedSimdRegsAndTempSimdRegs)120 TEST(ALlocatorTest, SeparateMappedSimdRegsAndTempSimdRegs) {
121 Allocator<x86_64::Assembler::XMMRegister> allocator;
122 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm0);
123 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm1);
124 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm2);
125 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm3);
126 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm4);
127 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm5);
128 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm6);
129 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm7);
130 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm8);
131 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm9);
132 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm10);
133 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm11);
134 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm12);
135 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::xmm15);
136 EXPECT_EQ(allocator.AllocTemp().value(), x86_64::Assembler::xmm14);
137 EXPECT_EQ(allocator.Alloc().value(), x86_64::Assembler::xmm13);
138 allocator.FreeTemps();
139 EXPECT_EQ(allocator.Alloc(), std::nullopt);
140 }
141
142 } // namespace
143
144 } // namespace berberis
145