2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-10-21 02:23:42 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
2009-04-19 02:14:50 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-04-19 02:14:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Implementations found here: CALL and JMP! (unconditional only)
|
2009-11-06 21:45:30 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
namespace x86Emitter
|
|
|
|
{
|
2009-04-19 02:14:50 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
extern void xJccKnownTarget(JccComparisonType comparison, const void *target, bool slideForward);
|
2009-11-06 21:45:30 +00:00
|
|
|
|
2009-04-19 02:14:50 +00:00
|
|
|
// ------------------------------------------------------------------------
|
2009-11-06 21:45:30 +00:00
|
|
|
struct xImpl_JmpCall
|
2009-04-19 02:14:50 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
bool isJmp;
|
|
|
|
|
|
|
|
void operator()(const xRegisterInt &absreg) const;
|
|
|
|
void operator()(const xIndirect64orLess &src) const;
|
|
|
|
|
|
|
|
// Special form for calling functions. This form automatically resolves the
|
|
|
|
// correct displacement based on the size of the instruction being generated.
|
|
|
|
void operator()(void *func) const
|
|
|
|
{
|
|
|
|
if (isJmp)
|
|
|
|
xJccKnownTarget(Jcc_Unconditional, (void *)(uptr)func, false); // double cast to/from (uptr) needed to appease GCC
|
|
|
|
else {
|
|
|
|
// calls are relative to the instruction after this one, and length is
|
|
|
|
// always 5 bytes (16 bit calls are bad mojo, so no bother to do special logic).
|
|
|
|
|
|
|
|
sptr dest = (sptr)func - ((sptr)xGetPtr() + 5);
|
|
|
|
xWrite8(0xe8);
|
|
|
|
xWrite32(dest);
|
|
|
|
}
|
|
|
|
}
|
2009-04-19 02:14:50 +00:00
|
|
|
};
|
2009-04-24 11:25:10 +00:00
|
|
|
|
2015-12-03 19:15:52 +00:00
|
|
|
// yes it is awful. Due to template code is in a header with a nice circular dep.
|
2016-11-12 15:28:37 +00:00
|
|
|
extern const xImpl_Mov xMOV;
|
|
|
|
extern const xImpl_JmpCall xCALL;
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
struct xImpl_FastCall
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
// FIXME: current 64 bits is mostly a copy/past potentially it would require to push/pop
|
|
|
|
// some registers. But I think it is enough to handle the first call.
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
// Type unsafety is nice
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
|
|
|
#define XFASTCALL \
|
2016-11-12 15:28:37 +00:00
|
|
|
xCALL(f);
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#define XFASTCALL1 \
|
2016-11-12 15:28:37 +00:00
|
|
|
xMOV(rdi, a1); \
|
|
|
|
xCALL(f);
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#define XFASTCALL2 \
|
2016-11-12 15:28:37 +00:00
|
|
|
xMOV(rdi, a1); \
|
|
|
|
xMOV(rsi, a2); \
|
|
|
|
xCALL(f);
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define XFASTCALL \
|
2016-11-12 15:28:37 +00:00
|
|
|
xCALL(f);
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#define XFASTCALL1 \
|
2016-11-12 15:28:37 +00:00
|
|
|
xMOV(ecx, a1); \
|
|
|
|
xCALL(f);
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#define XFASTCALL2 \
|
2016-11-12 15:28:37 +00:00
|
|
|
xMOV(ecx, a1); \
|
|
|
|
xMOV(edx, a2); \
|
|
|
|
xCALL(f);
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void operator()(void *f, const xRegisterLong &a1 = xEmptyReg, const xRegisterLong &a2 = xEmptyReg) const
|
|
|
|
{
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
if (a1.IsEmpty()) {
|
|
|
|
XFASTCALL;
|
|
|
|
} else if (a2.IsEmpty()) {
|
|
|
|
XFASTCALL1;
|
|
|
|
} else {
|
|
|
|
XFASTCALL2;
|
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
if (a1.IsEmpty()) {
|
|
|
|
XFASTCALL;
|
|
|
|
} else if (a2.IsEmpty()) {
|
|
|
|
XFASTCALL1;
|
|
|
|
} else {
|
|
|
|
XFASTCALL2;
|
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
#endif
|
2016-11-12 15:28:37 +00:00
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
|
|
|
__fi void operator()(T *func, u32 a1, const xRegisterLong &a2) const
|
|
|
|
{
|
|
|
|
void *f = (void *)func;
|
2016-08-14 12:01:04 +00:00
|
|
|
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL2;
|
2015-12-03 19:15:52 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL2;
|
2015-12-03 19:15:52 +00:00
|
|
|
#endif
|
2016-11-12 15:28:37 +00:00
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
|
|
|
__fi void operator()(T *func, const xIndirectVoid &a1) const
|
|
|
|
{
|
|
|
|
void *f = (void *)func;
|
2016-08-14 12:01:04 +00:00
|
|
|
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL1;
|
2015-12-03 19:15:52 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL1;
|
2015-12-03 19:15:52 +00:00
|
|
|
#endif
|
2016-11-12 15:28:37 +00:00
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
|
|
|
__fi void operator()(T *func, u32 a1, u32 a2) const
|
|
|
|
{
|
|
|
|
void *f = (void *)func;
|
2016-08-14 12:01:04 +00:00
|
|
|
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL2;
|
2015-12-03 19:15:52 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL2;
|
2015-12-03 19:15:52 +00:00
|
|
|
#endif
|
2016-11-12 15:28:37 +00:00
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
|
|
|
__fi void operator()(T *func, u32 a1) const
|
|
|
|
{
|
|
|
|
void *f = (void *)func;
|
2016-08-14 12:01:04 +00:00
|
|
|
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL1;
|
2015-12-03 19:15:52 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
XFASTCALL1;
|
2015-12-03 19:15:52 +00:00
|
|
|
#endif
|
2016-11-12 15:28:37 +00:00
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void operator()(const xIndirect32 &f, const xRegisterLong &a1 = xEmptyReg, const xRegisterLong &a2 = xEmptyReg) const
|
|
|
|
{
|
2015-12-03 19:15:52 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
if (a1.IsEmpty()) {
|
|
|
|
XFASTCALL;
|
|
|
|
} else if (a2.IsEmpty()) {
|
|
|
|
XFASTCALL1;
|
|
|
|
} else {
|
|
|
|
XFASTCALL2;
|
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
#else
|
2016-11-12 15:28:37 +00:00
|
|
|
if (a1.IsEmpty()) {
|
|
|
|
XFASTCALL;
|
|
|
|
} else if (a2.IsEmpty()) {
|
|
|
|
XFASTCALL1;
|
|
|
|
} else {
|
|
|
|
XFASTCALL2;
|
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
#endif
|
2016-11-12 15:28:37 +00:00
|
|
|
}
|
2015-12-03 19:15:52 +00:00
|
|
|
|
|
|
|
#undef XFASTCALL
|
|
|
|
#undef XFASTCALL1
|
|
|
|
#undef XFASTCALL2
|
|
|
|
};
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
} // End namespace x86Emitter
|