forked from winnerspiros/UnleashedRecomp_Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.h
More file actions
377 lines (326 loc) · 11.3 KB
/
Copy pathfunction.h
File metadata and controls
377 lines (326 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#pragma once
#include <cpu/ppc_context.h>
#include <array>
#include "xbox.h"
#include "memory.h"
template <typename R, typename... T>
constexpr std::tuple<T...> function_args(R(*)(T...)) noexcept
{
return std::tuple<T...>();
}
template<auto V>
static constexpr decltype(V) constant_v = V;
template<typename T>
static constexpr bool is_precise_v = std::is_same_v<T, float> || std::is_same_v<T, double>;
template<auto Func>
struct arg_count_t
{
static constexpr size_t value = std::tuple_size_v<decltype(function_args(Func))>;
};
template<typename TCallable, int I = 0, typename ...TArgs>
std::enable_if_t<(I >= sizeof...(TArgs)), void> _tuple_for(std::tuple<TArgs...>&, const TCallable& callable) noexcept
{
}
template<typename TCallable, int I = 0, typename ...TArgs>
std::enable_if_t<(I < sizeof...(TArgs)), void> _tuple_for(std::tuple<TArgs...>& tpl, const TCallable& callable) noexcept
{
callable(std::get<I>(tpl), I);
_tuple_for<TCallable, I + 1>(tpl, callable);
}
struct ArgTranslator
{
template<size_t N>
constexpr static uint64_t GetRegister(const PPCContext& ctx) noexcept
{
if constexpr (N == 0) return ctx.r3.u64;
if constexpr (N == 1) return ctx.r4.u64;
if constexpr (N == 2) return ctx.r5.u64;
if constexpr (N == 3) return ctx.r6.u64;
if constexpr (N == 4) return ctx.r7.u64;
if constexpr (N == 5) return ctx.r8.u64;
if constexpr (N == 6) return ctx.r9.u64;
if constexpr (N == 7) return ctx.r10.u64;
return 0;
}
template<size_t N>
constexpr static double GetFloatRegister(const PPCContext& ctx) noexcept
{
if constexpr (N == 0) return ctx.f1.f64;
if constexpr (N == 1) return ctx.f2.f64;
if constexpr (N == 2) return ctx.f3.f64;
if constexpr (N == 3) return ctx.f4.f64;
if constexpr (N == 4) return ctx.f5.f64;
if constexpr (N == 5) return ctx.f6.f64;
if constexpr (N == 6) return ctx.f7.f64;
if constexpr (N == 7) return ctx.f8.f64;
if constexpr (N == 8) return ctx.f9.f64;
if constexpr (N == 9) return ctx.f10.f64;
if constexpr (N == 10) return ctx.f11.f64;
if constexpr (N == 11) return ctx.f12.f64;
if constexpr (N == 12) return ctx.f13.f64;
return 0.0;
}
template<size_t ArgIndex, typename T>
constexpr static uint64_t GetIntegerArgumentValue(const PPCContext& ctx, uint8_t* base) noexcept
{
if constexpr (ArgIndex < 8)
{
return GetRegister<ArgIndex>(ctx);
}
else
{
uint32_t offset = 0x54 + ((ArgIndex - 8) * 8);
if constexpr (sizeof(T) == 8)
{
return *reinterpret_cast<be<uint64_t>*>(base + ctx.r1.u32 + offset);
}
else
{
return *reinterpret_cast<be<uint32_t>*>(base + ctx.r1.u32 + offset + 4);
}
}
}
template<size_t ArgIndex, size_t AbsoluteIndex>
static double GetPrecisionArgumentValue(const PPCContext& ctx, uint8_t* base) noexcept
{
if constexpr (ArgIndex <= 12)
{
return GetFloatRegister<ArgIndex>(ctx);
}
else
{
return *reinterpret_cast<be<double>*>(base + ctx.r1.u32 + 0x54 + ((AbsoluteIndex - 8) * 8));
}
}
constexpr static void SetIntegerArgumentValue(PPCContext& ctx, uint8_t* base, size_t arg, uint64_t value) noexcept
{
if (arg <= 7)
{
switch (arg)
{
case 0: ctx.r3.u64 = value; return;
case 1: ctx.r4.u64 = value; return;
case 2: ctx.r5.u64 = value; return;
case 3: ctx.r6.u64 = value; return;
case 4: ctx.r7.u64 = value; return;
case 5: ctx.r8.u64 = value; return;
case 6: ctx.r9.u64 = value; return;
case 7: ctx.r10.u64 = value; return;
[[unlikely]] default: break;
}
}
assert(arg < 7 && "Pushing to stack memory is not yet supported.");
}
static void SetPrecisionArgumentValue(PPCContext& ctx, uint8_t* base, size_t arg, double value) noexcept
{
switch (arg)
{
case 0: ctx.f1.f64 = value; return;
case 1: ctx.f2.f64 = value; return;
case 2: ctx.f3.f64 = value; return;
case 3: ctx.f4.f64 = value; return;
case 4: ctx.f5.f64 = value; return;
case 5: ctx.f6.f64 = value; return;
case 6: ctx.f7.f64 = value; return;
case 7: ctx.f8.f64 = value; return;
case 8: ctx.f9.f64 = value; return;
case 9: ctx.f10.f64 = value; return;
case 10: ctx.f11.f64 = value; return;
case 11: ctx.f12.f64 = value; return;
case 12: ctx.f13.f64 = value; return;
[[unlikely]] default: break;
}
assert(arg < 12 && "Pushing to stack memory is not yet supported.");
}
template<typename T, size_t idx, size_t absoluteIndex>
constexpr static std::enable_if_t<!std::is_pointer_v<T>, T> GetValue(PPCContext& ctx, uint8_t* base) noexcept
{
if constexpr (is_precise_v<T>)
{
return static_cast<T>(GetPrecisionArgumentValue<idx, absoluteIndex>(ctx, base));
}
else
{
return static_cast<T>(GetIntegerArgumentValue<idx, T>(ctx, base));
}
}
template<typename T, size_t idx, size_t absoluteIndex>
constexpr static std::enable_if_t<std::is_pointer_v<T>, T> GetValue(PPCContext& ctx, uint8_t* base) noexcept
{
const auto v = GetIntegerArgumentValue<idx, T>(ctx, base);
if (!v)
{
return nullptr;
}
return reinterpret_cast<T>(base + static_cast<uint32_t>(v));
}
template<typename T>
constexpr static std::enable_if_t<!std::is_pointer_v<T>, void> SetValue(PPCContext& ctx, uint8_t* base, size_t idx, T value) noexcept
{
if constexpr (is_precise_v<T>)
{
SetPrecisionArgumentValue(ctx, base, idx, value);
}
else if constexpr (std::is_null_pointer_v<T>)
{
SetIntegerArgumentValue(ctx, base, idx, 0);
}
else if constexpr (std::is_pointer_v<T>)
{
SetIntegerArgumentValue(ctx, base, idx, g_memory.MapVirtual(value));
}
else
{
SetIntegerArgumentValue(ctx, base, idx, value);
}
}
template<typename T>
constexpr static std::enable_if_t<std::is_pointer_v<T>, void> SetValue(PPCContext& ctx, uint8_t* base, size_t idx, T value) noexcept
{
const auto v = g_memory.MapVirtual((void*)value);
if (!v)
{
return;
}
SetValue(ctx, base, idx, v);
}
};
struct Argument
{
int type{};
int ordinal{};
};
template<typename T1>
constexpr std::array<Argument, std::tuple_size_v<T1>> GatherFunctionArguments(const T1& tpl)
{
std::array<Argument, std::tuple_size_v<T1>> args{};
int floatOrdinal{};
size_t i{};
if constexpr (!args.empty())
{
std::apply([&](const auto& first, const auto&... rest)
{
auto append = [&]<typename T2>(const T2& v)
{
if constexpr (is_precise_v<T2>)
{
args[i] = { 1, floatOrdinal++ };
}
else
{
args[i] = { 0, static_cast<int>(i) }; // what the fuck
}
i++;
};
append(first);
(append(rest), ...);
}, tpl);
}
return args;
}
template<auto Func>
constexpr std::array<Argument, arg_count_t<Func>::value> GatherFunctionArguments()
{
return GatherFunctionArguments(function_args(Func));
}
template<auto Func, size_t I>
struct arg_ordinal_t
{
static constexpr size_t value = GatherFunctionArguments<Func>()[I].ordinal;
};
template<auto Func, int I = 0, typename ...TArgs>
void _translate_args_to_host(PPCContext& ctx, uint8_t* base, std::tuple<TArgs...>&) noexcept
requires (I >= sizeof...(TArgs))
{
}
template <auto Func, int I = 0, typename ...TArgs>
std::enable_if_t<(I < sizeof...(TArgs)), void> _translate_args_to_host(PPCContext& ctx, uint8_t* base, std::tuple<TArgs...>& tpl) noexcept
{
using T = std::tuple_element_t<I, std::remove_reference_t<decltype(tpl)>>;
std::get<I>(tpl) = ArgTranslator::GetValue<T, arg_ordinal_t<Func, I>::value, I>(ctx, base);
_translate_args_to_host<Func, I + 1>(ctx, base, tpl);
}
template<int I = 0, typename ...TArgs>
void _translate_args_to_guest(PPCContext& ctx, uint8_t* base, std::tuple<TArgs...>&) noexcept
requires (I >= sizeof...(TArgs))
{
}
template <int I = 0, typename ...TArgs>
std::enable_if_t<(I < sizeof...(TArgs)), void> _translate_args_to_guest(PPCContext& ctx, uint8_t* base, std::tuple<TArgs...>& tpl) noexcept
{
using T = std::tuple_element_t<I, std::remove_reference_t<decltype(tpl)>>;
ArgTranslator::SetValue<T>(ctx, base, GatherFunctionArguments(std::tuple<TArgs...>{})[I].ordinal, std::get<I>(tpl));
_translate_args_to_guest<I + 1>(ctx, base, tpl);
}
template<auto Func>
PPC_FUNC(HostToGuestFunction)
{
using ret_t = decltype(std::apply(Func, function_args(Func)));
auto args = function_args(Func);
_translate_args_to_host<Func>(ctx, base, args);
if constexpr (std::is_same_v<ret_t, void>)
{
std::apply(Func, args);
}
else
{
auto v = std::apply(Func, args);
if constexpr (std::is_pointer<ret_t>())
{
if (v != nullptr)
{
ctx.r3.u64 = static_cast<uint32_t>(reinterpret_cast<size_t>(v) - reinterpret_cast<size_t>(base));
}
else
{
ctx.r3.u64 = 0;
}
}
else if constexpr (is_precise_v<ret_t>)
{
ctx.f1.f64 = v;
}
else
{
ctx.r3.u64 = (uint64_t)v;
}
}
}
template<typename T, typename TFunction, typename... TArgs>
T GuestToHostFunction(const TFunction& func, TArgs&&... argv)
{
auto args = std::make_tuple(std::forward<TArgs>(argv)...);
auto& currentCtx = *GetPPCContext();
PPCContext newCtx; // NOTE: No need for zero initialization, has lots of unnecessary code generation.
newCtx.r1 = currentCtx.r1;
newCtx.r13 = currentCtx.r13;
newCtx.fpscr = currentCtx.fpscr;
_translate_args_to_guest(newCtx, g_memory.base, args);
SetPPCContext(newCtx);
if constexpr (std::is_function_v<TFunction>)
func(newCtx, g_memory.base);
else
g_memory.FindFunction(func)(newCtx, g_memory.base);
currentCtx.fpscr = newCtx.fpscr;
SetPPCContext(currentCtx);
if constexpr (std::is_pointer_v<T>)
{
return reinterpret_cast<T>((uint64_t)g_memory.Translate(newCtx.r3.u32));
}
else if constexpr (is_precise_v<T>)
{
return static_cast<T>(newCtx.f1.f64);
}
else if constexpr (std::is_integral_v<T>)
{
return static_cast<T>(newCtx.r3.u64);
}
else
{
static_assert(std::is_void_v<T>, "Unsupported return type.");
}
}
#define GUEST_FUNCTION_HOOK(subroutine, function) \
PPC_FUNC(subroutine) { HostToGuestFunction<function>(ctx, base); }
#define GUEST_FUNCTION_STUB(subroutine) \
PPC_FUNC(subroutine) { }