rpcsx/rpcs3/Emu/SysCalls/SC_FUNC.h

215 lines
6.3 KiB
C
Raw Normal View History

#pragma once
2014-07-11 13:59:13 +02:00
#include "Emu/Cell/PPUThread.h"
2015-02-21 12:30:26 +01:00
typedef void(*ppu_func_caller)(PPUThread&);
2015-02-20 18:58:15 +01:00
2015-01-19 19:02:33 +01:00
namespace ppu_func_detail
{
enum bind_arg_type
{
ARG_GENERAL,
ARG_FLOAT,
ARG_VECTOR,
ARG_STACK,
};
template<typename T, bind_arg_type type, int g_count, int f_count, int v_count>
struct bind_arg;
template<typename T, int g_count, int f_count, int v_count>
struct bind_arg<T, ARG_GENERAL, g_count, f_count, v_count>
{
2014-09-08 02:54:17 +02:00
static_assert(sizeof(T) <= 8, "Invalid function argument type for ARG_GENERAL");
2014-08-26 23:09:50 +02:00
static __forceinline T func(PPUThread& CPU)
{
return cast_from_ppu_gpr<T>(CPU.GPR[g_count + 2]);
2014-08-26 23:09:50 +02:00
}
};
template<typename T, int g_count, int f_count, int v_count>
struct bind_arg<T, ARG_FLOAT, g_count, f_count, v_count>
{
2014-09-08 02:54:17 +02:00
static_assert(sizeof(T) <= 8, "Invalid function argument type for ARG_FLOAT");
2014-08-26 23:09:50 +02:00
static __forceinline T func(PPUThread& CPU)
{
return static_cast<T>(CPU.FPR[f_count]);
2014-08-26 23:09:50 +02:00
}
};
template<typename T, int g_count, int f_count, int v_count>
struct bind_arg<T, ARG_VECTOR, g_count, f_count, v_count>
{
2015-01-07 17:44:47 +01:00
static_assert(std::is_same<T, u128>::value, "Invalid function argument type for ARG_VECTOR");
2014-08-26 23:09:50 +02:00
static __forceinline T func(PPUThread& CPU)
{
2015-01-07 17:44:47 +01:00
return CPU.VPR[v_count + 1];
2014-08-26 23:09:50 +02:00
}
};
template<typename T, int g_count, int f_count, int v_count>
struct bind_arg<T, ARG_STACK, g_count, f_count, v_count>
{
2014-09-23 16:27:18 +02:00
static_assert(f_count <= 13, "TODO: Unsupported stack argument type (float)");
2014-09-08 02:54:17 +02:00
static_assert(v_count <= 12, "TODO: Unsupported stack argument type (vector)");
2014-09-12 15:08:24 +02:00
static_assert(sizeof(T) <= 8, "Invalid function argument type for ARG_STACK");
2014-08-26 23:09:50 +02:00
static __forceinline T func(PPUThread& CPU)
{
2014-09-08 02:54:17 +02:00
// TODO: check stack argument displacement
2014-09-23 16:27:18 +02:00
const u64 res = CPU.GetStackArg(8 + std::max(g_count - 8, 0) + std::max(f_count - 13, 0) + std::max(v_count - 12, 0));
return cast_from_ppu_gpr<T>(res);
2014-08-26 23:09:50 +02:00
}
};
2014-09-16 15:56:27 +02:00
template<typename T, bind_arg_type type>
struct bind_result
{
2014-09-16 15:56:27 +02:00
static_assert(type == ARG_GENERAL, "Wrong use of bind_result template");
static_assert(sizeof(T) <= 8, "Invalid function result type for ARG_GENERAL");
2015-01-07 17:44:47 +01:00
static __forceinline void func(PPUThread& CPU, const T& result)
{
2015-01-14 20:45:36 +01:00
CPU.GPR[3] = cast_to_ppu_gpr<T>(result);
}
};
2014-09-16 15:56:27 +02:00
template<typename T>
struct bind_result<T, ARG_FLOAT>
{
2014-09-16 15:56:27 +02:00
static_assert(sizeof(T) <= 8, "Invalid function result type for ARG_FLOAT");
2015-01-07 17:44:47 +01:00
static __forceinline void func(PPUThread& CPU, const T& result)
2014-09-15 00:17:24 +02:00
{
CPU.FPR[1] = static_cast<T>(result);
2014-09-15 00:17:24 +02:00
}
};
2014-09-16 15:56:27 +02:00
template<typename T>
struct bind_result<T, ARG_VECTOR>
2014-09-15 00:17:24 +02:00
{
2015-01-07 17:44:47 +01:00
static_assert(std::is_same<T, u128>::value, "Invalid function result type for ARG_VECTOR");
2014-09-16 15:56:27 +02:00
2015-01-07 17:44:47 +01:00
static __forceinline void func(PPUThread& CPU, const T& result)
{
2015-01-07 17:44:47 +01:00
CPU.VPR[2] = result;
}
};
template <typename RT, typename F, typename Tuple, bool Done, int Total, int... N>
struct call_impl
{
static __forceinline RT call(F f, Tuple && t)
{
return call_impl<RT, F, Tuple, Total == 1 + sizeof...(N), Total, N..., sizeof...(N)>::call(f, std::forward<Tuple>(t));
}
};
template <typename RT, typename F, typename Tuple, int Total, int... N>
struct call_impl<RT, F, Tuple, true, Total, N...>
{
static __forceinline RT call(F f, Tuple && t)
{
return f(std::get<N>(std::forward<Tuple>(t))...);
}
};
template <typename RT, typename F, typename Tuple>
__forceinline RT call(F f, Tuple && t)
{
typedef typename std::decay<Tuple>::type ttype;
2015-01-19 19:02:33 +01:00
return ppu_func_detail::call_impl<RT, F, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value>::call(f, std::forward<Tuple>(t));
}
template<int g_count, int f_count, int v_count>
__forceinline std::tuple<> iterate(PPUThread& CPU)
{
2014-09-12 15:08:24 +02:00
// terminator
return std::tuple<>();
}
template<int g_count, int f_count, int v_count, typename T, typename... A>
__forceinline std::tuple<T, A...> iterate(PPUThread& CPU)
{
2014-09-08 02:54:17 +02:00
static_assert(!std::is_pointer<T>::value, "Invalid function argument type (pointer)");
static_assert(!std::is_reference<T>::value, "Invalid function argument type (reference)");
// TODO: check calculations
2014-08-26 23:09:50 +02:00
const bool is_float = std::is_floating_point<T>::value;
const bool is_vector = std::is_same<T, u128>::value;
2014-08-26 23:09:50 +02:00
const bind_arg_type t = is_float
2014-09-23 16:27:18 +02:00
? ((f_count >= 13) ? ARG_STACK : ARG_FLOAT)
: (is_vector ? ((v_count >= 12) ? ARG_STACK : ARG_VECTOR) : ((g_count >= 8) ? ARG_STACK : ARG_GENERAL));
const int g = g_count + (is_float || is_vector ? 0 : 1);
2014-08-26 23:09:50 +02:00
const int f = f_count + (is_float ? 1 : 0);
const int v = v_count + (is_vector ? 1 : 0);
2014-09-12 15:08:24 +02:00
2014-08-26 23:09:50 +02:00
return std::tuple_cat(std::tuple<T>(bind_arg<T, t, g, f, v>::func(CPU)), iterate<g, f, v, A...>(CPU));
}
2015-01-19 19:02:33 +01:00
template<typename RT>
struct result_type
{
static_assert(!std::is_pointer<RT>::value, "Invalid function result type (pointer)");
static_assert(!std::is_reference<RT>::value, "Invalid function result type (reference)");
static const bool is_float = std::is_floating_point<RT>::value;
static const bool is_vector = std::is_same<RT, u128>::value;
static const bind_arg_type value = is_float ? ARG_FLOAT : (is_vector ? ARG_VECTOR : ARG_GENERAL);
};
2015-02-21 12:30:26 +01:00
template<typename RT, typename... T>
struct func_binder;
2015-02-21 12:30:26 +01:00
template<typename... T>
struct func_binder<void, PPUThread&, T...>
{
2015-02-20 20:45:00 +01:00
typedef void(*func_t)(PPUThread&, T...);
2015-02-20 18:58:15 +01:00
static void do_call(PPUThread& CPU, func_t _func)
{
2015-02-20 20:45:00 +01:00
call<void>(_func, std::tuple_cat(std::tuple<PPUThread&>(CPU), iterate<0, 0, 0, T...>(CPU)));
2015-02-20 18:58:15 +01:00
}
};
2015-02-21 12:30:26 +01:00
template<typename... T>
struct func_binder<void, T...>
{
2015-02-20 20:45:00 +01:00
typedef void(*func_t)(T...);
2015-02-20 18:58:15 +01:00
static void do_call(PPUThread& CPU, func_t _func)
{
2015-02-20 20:45:00 +01:00
call<void>(_func, iterate<0, 0, 0, T...>(CPU));
2015-02-20 18:58:15 +01:00
}
};
2015-02-21 12:30:26 +01:00
template<typename RT, typename... T>
struct func_binder<RT, PPUThread&, T...>
{
2015-02-20 20:45:00 +01:00
typedef RT(*func_t)(PPUThread&, T...);
2015-02-20 18:58:15 +01:00
static void do_call(PPUThread& CPU, func_t _func)
{
2015-02-20 20:45:00 +01:00
bind_result<RT, result_type<RT>::value>::func(CPU, call<RT>(_func, std::tuple_cat(std::tuple<PPUThread&>(CPU), iterate<0, 0, 0, T...>(CPU))));
2015-02-20 18:58:15 +01:00
}
};
2015-02-21 12:30:26 +01:00
template<typename RT, typename... T>
2015-02-20 20:45:00 +01:00
struct func_binder
{
2015-02-20 20:45:00 +01:00
typedef RT(*func_t)(T...);
2015-02-20 18:58:15 +01:00
static void do_call(PPUThread& CPU, func_t _func)
{
2015-02-20 20:45:00 +01:00
bind_result<RT, result_type<RT>::value>::func(CPU, call<RT>(_func, iterate<0, 0, 0, T...>(CPU)));
2015-02-20 18:58:15 +01:00
}
};
}
2015-02-21 12:30:26 +01:00
template<typename RT, typename... T> __forceinline void call_ppu_func(PPUThread& CPU, RT(*func)(T...))
{
ppu_func_detail::func_binder<RT, T...>::do_call(CPU, func);
}
2015-02-20 18:58:15 +01:00
2015-02-21 12:30:26 +01:00
#define bind_func(func) [](PPUThread& CPU){ call_ppu_func(CPU, func); }