rpcsx/rpcs3/Emu/Memory/vm_ptr.h

471 lines
8.4 KiB
C
Raw Normal View History

#pragma once
namespace vm
{
template<typename T, int lvl = 1, typename AT = u32>
2014-09-01 18:16:44 +02:00
class _ptr_base
{
AT m_addr;
public:
2014-09-01 18:16:44 +02:00
typedef T type;
_ptr_base operator++ (int)
{
AT result = m_addr;
m_addr += sizeof(AT);
2014-09-01 18:16:44 +02:00
return make(result);
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator++ ()
{
m_addr += sizeof(AT);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base operator-- (int)
{
AT result = m_addr;
m_addr -= sizeof(AT);
2014-09-01 18:16:44 +02:00
return make(result);
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator-- ()
{
m_addr -= sizeof(AT);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator += (int count)
{
m_addr += count * sizeof(AT);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator -= (int count)
{
m_addr -= count * sizeof(AT);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base operator + (int count) const
{
2014-09-01 18:16:44 +02:00
return make(m_addr + count * sizeof(AT));
}
2014-09-01 18:16:44 +02:00
_ptr_base operator - (int count) const
{
2014-09-01 18:16:44 +02:00
return make(m_addr - count * sizeof(AT));
}
2014-09-01 18:16:44 +02:00
__forceinline _ptr_base<T, lvl - 1, AT>& operator *()
{
2014-09-01 18:16:44 +02:00
return vm::get_ref<_ptr_base<T, lvl - 1, AT>>(m_addr);
}
2014-09-01 18:16:44 +02:00
__forceinline const _ptr_base<T, lvl - 1, AT>& operator *() const
{
2014-09-01 18:16:44 +02:00
return vm::get_ref<const _ptr_base<T, lvl - 1, AT>>(m_addr);
}
2014-09-01 18:16:44 +02:00
__forceinline _ptr_base<T, lvl - 1, AT>& operator [](int index)
{
2014-09-01 18:16:44 +02:00
return vm::get_ref<_ptr_base<T, lvl - 1, AT>>(m_addr + sizeof(AT) * index);
}
2014-09-01 18:16:44 +02:00
__forceinline const _ptr_base<T, lvl - 1, AT>& operator [](int index) const
{
2014-09-01 18:16:44 +02:00
return vm::get_ref<const _ptr_base<T, lvl - 1, AT>>(m_addr + sizeof(AT) * index);
}
operator bool() const
{
return m_addr != 0;
}
AT addr() const
{
return m_addr;
}
2014-09-01 18:16:44 +02:00
static _ptr_base make(AT addr)
{
2014-09-01 18:16:44 +02:00
return (_ptr_base&)addr;
}
};
template<typename T, typename AT>
2014-09-01 18:16:44 +02:00
class _ptr_base<T, 1, AT>
{
AT m_addr;
public:
2014-08-31 11:54:12 +02:00
__forceinline T* const operator -> ()
{
2014-08-31 01:19:10 +02:00
return vm::get_ptr<T>(m_addr);
}
2014-08-31 11:54:12 +02:00
__forceinline const T* const operator -> () const
{
2014-08-31 01:19:10 +02:00
return vm::get_ptr<const T>(m_addr);
}
2014-09-01 18:16:44 +02:00
_ptr_base operator++ (int)
{
AT result = m_addr;
m_addr += sizeof(T);
2014-09-01 18:16:44 +02:00
return make(result);
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator++ ()
{
m_addr += sizeof(T);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base operator-- (int)
{
AT result = m_addr;
m_addr -= sizeof(T);
2014-09-01 18:16:44 +02:00
return make(result);
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator-- ()
{
m_addr -= sizeof(T);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator += (int count)
{
m_addr += count * sizeof(T);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base& operator -= (int count)
{
m_addr -= count * sizeof(T);
return *this;
}
2014-09-01 18:16:44 +02:00
_ptr_base operator + (int count) const
{
2014-09-01 18:16:44 +02:00
return make(m_addr + count * sizeof(T));
}
2014-09-01 18:16:44 +02:00
_ptr_base operator - (int count) const
{
2014-09-01 18:16:44 +02:00
return make(m_addr - count * sizeof(T));
}
__forceinline T& operator *()
{
return get_ref<T>(m_addr);
}
__forceinline const T& operator *() const
{
return get_ref<T>(m_addr);
}
__forceinline T& operator [](int index)
{
return get_ref<T>(m_addr + sizeof(T) * index);
}
__forceinline const T& operator [](int index) const
{
return get_ref<const T>(m_addr + sizeof(T) * index);
}
/*
2014-09-01 18:16:44 +02:00
operator _ref_base<T, AT>()
{
2014-09-01 18:16:44 +02:00
return _ref_base<T, AT>::make(m_addr);
}
2014-09-01 18:16:44 +02:00
operator const _ref_base<T, AT>() const
{
2014-09-01 18:16:44 +02:00
return _ref_base<T, AT>::make(m_addr);
}
*/
AT addr() const
{
return m_addr;
}
operator bool() const
{
return m_addr != 0;
}
2014-08-31 11:54:12 +02:00
T* const get_ptr() const
{
return vm::get_ptr<T>(m_addr);
}
2014-09-01 18:16:44 +02:00
static _ptr_base make(AT addr)
{
2014-09-01 18:16:44 +02:00
return (_ptr_base&)addr;
}
};
template<typename AT>
2014-09-01 18:16:44 +02:00
class _ptr_base<void, 1, AT>
{
AT m_addr;
public:
AT addr() const
{
return m_addr;
}
2014-08-31 11:54:12 +02:00
void* const get_ptr() const
{
return vm::get_ptr<void>(m_addr);
}
operator bool() const
{
return m_addr != 0;
}
2014-09-01 18:16:44 +02:00
static _ptr_base make(AT addr)
{
2014-09-01 18:16:44 +02:00
return (_ptr_base&)addr;
}
};
template<typename AT>
2014-09-01 18:16:44 +02:00
class _ptr_base<const void, 1, AT>
{
AT m_addr;
public:
AT addr() const
{
return m_addr;
}
const void* const get_ptr() const
{
return vm::get_ptr<const void>(m_addr);
}
operator bool() const
{
return m_addr != 0;
}
2014-09-01 18:16:44 +02:00
static _ptr_base make(AT addr)
{
2014-09-01 18:16:44 +02:00
return (_ptr_base&)addr;
}
};
template<typename RT, typename AT>
2014-09-01 18:16:44 +02:00
class _ptr_base<RT(*)(), 1, AT>
{
AT m_addr;
2014-09-02 00:59:46 +02:00
static_assert(!std::is_floating_point<RT>::value, "TODO: Unsupported callback result type (floating point)");
static_assert(!std::is_pointer<RT>::value, "Invalid callback result type (pointer)");
__forceinline RT call_func(bool is_async) const
{
Callback cb;
cb.SetAddr(m_addr);
return (RT)cb.Branch(!is_async);
}
public:
typedef RT(*type)();
__forceinline RT operator()() const
{
return call_func(false);
}
__forceinline void async() const
{
call_func(true);
}
AT addr() const
{
return m_addr;
}
operator bool() const
{
return m_addr != 0;
}
2014-09-01 18:16:44 +02:00
static _ptr_base make(AT addr)
{
2014-09-01 18:16:44 +02:00
return (_ptr_base&)addr;
}
2014-09-01 14:47:26 +02:00
operator std::function<RT()>() const
{
const AT addr = m_addr;
return [addr]() -> RT { return make(addr)(); };
}
};
template<typename AT, typename RT, typename ...T>
2014-09-01 18:16:44 +02:00
class _ptr_base<RT(*)(T...), 1, AT>
{
AT m_addr;
2014-09-02 00:59:46 +02:00
static_assert(!std::is_floating_point<RT>::value, "TODO: Unsupported callback result type (floating point)");
2014-08-31 01:38:55 +02:00
2014-09-02 00:59:46 +02:00
static_assert(!std::is_pointer<RT>::value, "Invalid callback result type (pointer)");
2014-09-01 19:38:55 +02:00
2014-09-02 00:59:46 +02:00
template<typename TT>
struct _func_arg
2014-09-01 19:38:55 +02:00
{
2014-09-02 00:59:46 +02:00
static_assert(!std::is_floating_point<TT>::value, "TODO: Unsupported callback argument type (floating point)");
2014-08-31 01:38:55 +02:00
2014-09-02 00:59:46 +02:00
static_assert(!std::is_pointer<TT>::value, "Invalid callback argument type (pointer)");
2014-09-02 00:59:46 +02:00
__forceinline static u64 get_value(const TT& arg)
2014-09-01 14:47:26 +02:00
{
2014-09-02 00:59:46 +02:00
u64 res = 0;
(TT&)res = arg;
return res;
2014-09-01 14:47:26 +02:00
}
};
2014-08-31 01:38:55 +02:00
__forceinline RT call_func(bool is_async, T... args) const
{
Callback cb;
cb.SetAddr(m_addr);
cb.Handle(_func_arg<T>::get_value(args)...);
return (RT)cb.Branch(!is_async);
}
public:
typedef RT(*type)(T...);
__forceinline RT operator()(T... args) const
{
return call_func(false, args...);
}
__forceinline void async(T... args) const
{
call_func(true, args...);
}
AT addr() const
{
return m_addr;
}
operator bool() const
{
return m_addr != 0;
}
2014-09-01 18:16:44 +02:00
static _ptr_base make(AT addr)
{
2014-09-01 18:16:44 +02:00
return (_ptr_base&)addr;
}
2014-09-01 14:47:26 +02:00
operator std::function<RT(T...)>() const
{
const AT addr = m_addr;
return [addr](T... args) -> RT { return make(addr)(args...); };
}
};
2014-09-01 18:16:44 +02:00
//BE pointer to LE data
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct bptrl : public _ptr_base<T, lvl, typename to_be_t<AT>::type>
{
static bptrl make(AT addr)
{
return (bptrl&)addr;
}
using _ptr_base<T, lvl, typename to_be_t<AT>::type>::operator=;
};
2014-09-01 18:16:44 +02:00
//BE pointer to BE data
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct bptrb : public _ptr_base<typename to_be_t<T>::type, lvl, typename to_be_t<AT>::type>
{
static bptrb make(AT addr)
{
return (bptrb&)addr;
}
using _ptr_base<typename to_be_t<T>::type, lvl, typename to_be_t<AT>::type>::operator=;
};
2014-09-01 18:16:44 +02:00
//LE pointer to BE data
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct lptrb : public _ptr_base<typename to_be_t<T>::type, lvl, AT>
{
static lptrb make(AT addr)
{
return (lptrb&)addr;
}
using _ptr_base<typename to_be_t<T>::type, lvl, AT>::operator=;
};
2014-09-01 18:16:44 +02:00
//LE pointer to LE data
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct lptrl : public _ptr_base<T, lvl, AT>
{
static lptrl make(AT addr)
{
return (lptrl&)addr;
}
using _ptr_base<T, lvl, AT>::operator=;
};
2014-09-01 18:16:44 +02:00
namespace ps3
{
//default pointer for HLE functions (LE ptrerence to BE data)
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct ptr : public lptrb<T, lvl, AT>
2014-09-01 18:16:44 +02:00
{
static ptr make(AT addr)
{
return (ptr&)addr;
}
2014-09-02 00:22:13 +02:00
using lptrb<T, lvl, AT>::operator=;
2014-09-01 18:16:44 +02:00
};
2014-09-02 00:22:13 +02:00
2014-09-01 18:16:44 +02:00
//default pointer for HLE structures (BE ptrerence to BE data)
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct bptr : public bptrb<T, lvl, AT>
{
static bptr make(AT addr)
{
return (bptr&)addr;
}
using bptrb<T, lvl, AT>::operator=;
};
2014-09-01 18:16:44 +02:00
}
2014-09-02 00:22:13 +02:00
2014-09-01 18:16:44 +02:00
namespace psv
{
//default pointer for HLE functions & structures (LE ptrerence to LE data)
2014-09-02 00:22:13 +02:00
template<typename T, int lvl = 1, typename AT = u32> struct ptr : public lptrl<T, lvl, AT>
{
static ptr make(AT addr)
{
return (ptr&)addr;
}
using lptrl<T, lvl, AT>::operator=;
};
2014-09-01 18:16:44 +02:00
}
2014-09-02 00:22:13 +02:00
2014-09-01 18:16:44 +02:00
//PS3 emulation is main now, so lets it be as default
using namespace ps3;
}