Debian Codegraph @

Source/UnitTests/Common/Arm64EmitterTest.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <bit>
5
6 #include "Common/Arm64Emitter.h"
7 #include "Common/BitSet.h"
8
9 #include <gtest/gtest.h>
10
11 using namespace Arm64Gen;
12
13 namespace
14 {
15 u32 ZeroParameterFunction()
16 {
17 return 123;
18 }
19
20 u32 OneParameterFunction(u64 a)
21 {
22 return a + 23;
23 }
24
25 u32 TwoParameterFunction(u64 a, u64 b)
26 {
27 return a * 10 + b + 3;
28 }
29
30 u32 ThreeParameterFunction(u64 a, u64 b, u64 c)
31 {
32 return a * 10 + b + c / 10;
33 }
34
35 u32 EightParameterFunction(u64 a, u64 b, u64 c, u64 d, u64 e, u64 f, u64 g, u64 h)
36 {
37 return a / 20 + b / 8 + c / 10 + d / 2 + e / 5 - f + g + h / 3;
38 }
39
40 class TestCallFunction : public ARM64CodeBlock
41 {
42 public:
43 TestCallFunction() { AllocCodeSpace(4096); }
44
45 template <typename F>
46 void Emit(F f)
47 {
48 ResetCodePtr();
49
50 m_code_pointer = GetCodePtr();
51 {
52 const Common::ScopedJITPageWriteAndNoExecute enable_jit_page_writes;
53
54 constexpr BitSet32 link_register{DecodeReg(ARM64Reg::X30)};
55 ABI_PushRegisters(link_register);
56 f();
57 ABI_PopRegisters(link_register);
58 RET();
59 }
60
61 FlushIcacheSection(const_cast<u8*>(m_code_pointer), const_cast<u8*>(GetCodePtr()));
62 }
63
64 void Run()
65 {
66 const u64 actual = std::bit_cast<u64 (*)()>(m_code_pointer)();
67 constexpr u64 expected = 123;
68 EXPECT_EQ(expected, actual);
69 }
70
71 private:
72 const u8* m_code_pointer = nullptr;
73 };
74
75 } // namespace
76
77 TEST(Arm64Emitter, CallFunction_ZeroParameters)
78 {
79 TestCallFunction test;
80 test.Emit([&] { test.ABI_CallFunction(&ZeroParameterFunction); });
81 test.Run();
82 }
83
84 TEST(Arm64Emitter, CallFunction_OneConstantParameter)
85 {
86 TestCallFunction test;
87 test.Emit([&] { test.ABI_CallFunction(&OneParameterFunction, 100); });
88 test.Run();
89 }
90
91 TEST(Arm64Emitter, CallFunction_OneRegisterParameterNoMov)
92 {
93 TestCallFunction test;
94 test.Emit([&] {
95 test.MOVI2R(ARM64Reg::X0, 100);
96 test.ABI_CallFunction(&OneParameterFunction, ARM64Reg::X0);
97 });
98 test.Run();
99 }
100
101 TEST(Arm64Emitter, CallFunction_OneRegisterParameterMov)
102 {
103 TestCallFunction test;
104 test.Emit([&] {
105 test.MOVI2R(ARM64Reg::X8, 100);
106 test.ABI_CallFunction(&OneParameterFunction, ARM64Reg::X8);
107 });
108 test.Run();
109 }
110
111 TEST(Arm64Emitter, CallFunction_TwoRegistersMixed)
112 {
113 TestCallFunction test;
114 test.Emit([&] {
115 test.MOVI2R(ARM64Reg::X0, 20);
116 test.ABI_CallFunction(&TwoParameterFunction, 10, ARM64Reg::X0);
117 });
118 test.Run();
119 }
120
121 TEST(Arm64Emitter, CallFunction_TwoRegistersCycle)
122 {
123 TestCallFunction test;
124 test.Emit([&] {
125 test.MOVI2R(ARM64Reg::X0, 20);
126 test.MOVI2R(ARM64Reg::X1, 10);
127 test.ABI_CallFunction(&TwoParameterFunction, ARM64Reg::X1, ARM64Reg::X0);
128 });
129 test.Run();
130 }
131
132 TEST(Arm64Emitter, CallFunction_ThreeRegistersMixed)
133 {
134 TestCallFunction test;
135 test.Emit([&] {
136 test.MOVI2R(ARM64Reg::X1, 10);
137 test.MOVI2R(ARM64Reg::X2, 20);
138 test.ABI_CallFunction(&ThreeParameterFunction, ARM64Reg::X1, ARM64Reg::X2, 30);
139 });
140 test.Run();
141 }
142
143 TEST(Arm64Emitter, CallFunction_ThreeRegistersCycle1)
144 {
145 TestCallFunction test;
146 test.Emit([&] {
147 test.MOVI2R(ARM64Reg::X0, 30);
148 test.MOVI2R(ARM64Reg::X1, 10);
149 test.MOVI2R(ARM64Reg::X2, 20);
150 test.ABI_CallFunction(&ThreeParameterFunction, ARM64Reg::X1, ARM64Reg::X2, ARM64Reg::X0);
151 });
152 test.Run();
153 }
154
155 TEST(Arm64Emitter, CallFunction_ThreeRegistersCycle2)
156 {
157 TestCallFunction test;
158 test.Emit([&] {
159 test.MOVI2R(ARM64Reg::X0, 20);
160 test.MOVI2R(ARM64Reg::X1, 30);
161 test.MOVI2R(ARM64Reg::X2, 10);
162 test.ABI_CallFunction(&ThreeParameterFunction, ARM64Reg::X2, ARM64Reg::X0, ARM64Reg::X1);
163 });
164 test.Run();
165 }
166
167 TEST(Arm64Emitter, CallFunction_EightRegistersMixed)
168 {
169 TestCallFunction test;
170 test.Emit([&] {
171 test.MOVI2R(ARM64Reg::X3, 12);
172 test.MOVI2R(ARM64Reg::X4, 23);
173 test.MOVI2R(ARM64Reg::X5, 24);
174 test.MOVI2R(ARM64Reg::X30, 2000);
175 test.ABI_CallFunction(&EightParameterFunction, ARM64Reg::X30, 40, ARM64Reg::X4, ARM64Reg::X5,
176 ARM64Reg::X4, ARM64Reg::X3, 5, ARM64Reg::X4);
177 });
178 test.Run();
179 }