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
|
2010-04-25 00:31:27 +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-14 12:37:48 +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-14 12:37:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-11-06 21:45:30 +00:00
|
|
|
* ix86 core v0.9.1
|
2009-04-14 12:37:48 +00:00
|
|
|
*
|
|
|
|
* Original Authors (v0.6.2 and prior):
|
|
|
|
* linuzappz <linuzappz@pcsx.net>
|
|
|
|
* alexey silinov
|
|
|
|
* goldfinger
|
|
|
|
* zerofrog(@gmail.com)
|
|
|
|
*
|
2009-11-06 21:45:30 +00:00
|
|
|
* Authors of v0.9.1:
|
2009-04-14 12:37:48 +00:00
|
|
|
* Jake.Stine(@gmail.com)
|
|
|
|
* cottonvibes(@gmail.com)
|
|
|
|
* sudonim(1@gmail.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
2009-07-03 00:49:40 +00:00
|
|
|
#include "internal.h"
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
namespace x86Emitter
|
|
|
|
{
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void xImpl_JmpCall::operator()(const xRegisterInt &absreg) const { xOpWrite(0, 0xff, isJmp ? 4 : 2, absreg); }
|
|
|
|
void xImpl_JmpCall::operator()(const xIndirect64orLess &src) const { xOpWrite(0, 0xff, isJmp ? 4 : 2, src); }
|
2009-11-06 21:45:30 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
const xImpl_JmpCall xJMP = {true};
|
|
|
|
const xImpl_JmpCall xCALL = {false};
|
2009-04-19 02:14:50 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
const xImpl_FastCall xFastCall = {};
|
2015-12-03 19:15:52 +00:00
|
|
|
|
2009-04-20 03:10:05 +00:00
|
|
|
void xSmartJump::SetTarget()
|
2009-04-14 12:37:48 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
u8 *target = xGetPtr();
|
|
|
|
if (m_baseptr == NULL)
|
|
|
|
return;
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
xSetPtr(m_baseptr);
|
|
|
|
u8 *const saveme = m_baseptr + GetMaxInstructionSize();
|
|
|
|
xJccKnownTarget(m_cc, target, true);
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
// Copy recompiled data inward if the jump instruction didn't fill the
|
|
|
|
// alloted buffer (means that we optimized things to a j8!)
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
const int spacer = (sptr)saveme - (sptr)xGetPtr();
|
|
|
|
if (spacer != 0) {
|
|
|
|
u8 *destpos = xGetPtr();
|
|
|
|
const int copylen = (sptr)target - (sptr)saveme;
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
memcpy(destpos, saveme, copylen);
|
|
|
|
xSetPtr(target - spacer);
|
|
|
|
}
|
2009-04-19 02:14:50 +00:00
|
|
|
}
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2009-04-20 03:10:05 +00:00
|
|
|
xSmartJump::~xSmartJump()
|
2009-04-19 02:14:50 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
SetTarget();
|
|
|
|
m_baseptr = NULL; // just in case (sometimes helps in debugging too)
|
2009-04-14 12:37:48 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 08:21:57 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Emits a 32 bit jump, and returns a pointer to the 32 bit displacement.
|
|
|
|
// (displacements should be assigned relative to the end of the jump instruction,
|
|
|
|
// or in other words *(retval+1) )
|
2016-11-12 15:28:37 +00:00
|
|
|
__emitinline s32 *xJcc32(JccComparisonType comparison, s32 displacement)
|
2009-05-07 08:21:57 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
if (comparison == Jcc_Unconditional)
|
|
|
|
xWrite8(0xe9);
|
|
|
|
else {
|
|
|
|
xWrite8(0x0f);
|
|
|
|
xWrite8(0x80 | comparison);
|
|
|
|
}
|
|
|
|
xWrite<s32>(displacement);
|
|
|
|
|
|
|
|
return ((s32 *)xGetPtr()) - 1;
|
2009-05-07 08:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Emits a 32 bit jump, and returns a pointer to the 8 bit displacement.
|
|
|
|
// (displacements should be assigned relative to the end of the jump instruction,
|
|
|
|
// or in other words *(retval+1) )
|
2016-11-12 15:28:37 +00:00
|
|
|
__emitinline s8 *xJcc8(JccComparisonType comparison, s8 displacement)
|
2009-05-07 08:21:57 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
xWrite8((comparison == Jcc_Unconditional) ? 0xeb : (0x70 | comparison));
|
|
|
|
xWrite<s8>(displacement);
|
|
|
|
return (s8 *)xGetPtr() - 1;
|
2009-05-07 08:21:57 +00:00
|
|
|
}
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2009-04-15 15:45:52 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Writes a jump at the current x86Ptr, which targets a pre-established target address.
|
|
|
|
// (usually a backwards jump)
|
|
|
|
//
|
2009-04-20 03:10:05 +00:00
|
|
|
// slideForward - used internally by xSmartJump to indicate that the jump target is going
|
2009-04-15 15:45:52 +00:00
|
|
|
// to slide forward in the event of an 8 bit displacement.
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
__emitinline void xJccKnownTarget(JccComparisonType comparison, const void *target, bool slideForward)
|
2009-04-14 12:37:48 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
// Calculate the potential j8 displacement first, assuming an instruction length of 2:
|
|
|
|
sptr displacement8 = (sptr)target - (sptr)(xGetPtr() + 2);
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
const int slideVal = slideForward ? ((comparison == Jcc_Unconditional) ? 3 : 4) : 0;
|
|
|
|
displacement8 -= slideVal;
|
2009-04-14 12:37:48 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
if (slideForward) {
|
|
|
|
pxAssertDev(displacement8 >= 0, "Used slideForward on a backward jump; nothing to slide!");
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
if (is_s8(displacement8))
|
|
|
|
xJcc8(comparison, displacement8);
|
|
|
|
else {
|
|
|
|
// Perform a 32 bit jump instead. :(
|
|
|
|
s32 *bah = xJcc32(comparison);
|
|
|
|
sptr distance = (sptr)target - (sptr)xGetPtr();
|
2014-07-15 20:13:15 +00:00
|
|
|
|
2016-08-12 17:17:35 +00:00
|
|
|
#ifdef __x86_64__
|
2016-11-12 15:28:37 +00:00
|
|
|
// This assert won't physically happen on x86 targets
|
|
|
|
pxAssertDev(distance >= -0x80000000LL && distance < 0x80000000LL, "Jump target is too far away, needs an indirect register");
|
2016-08-12 17:17:35 +00:00
|
|
|
#endif
|
2014-07-15 20:13:15 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
*bah = (s32)distance;
|
|
|
|
}
|
2009-04-14 12:37:48 +00:00
|
|
|
}
|
|
|
|
|
2009-04-24 11:25:10 +00:00
|
|
|
// Low-level jump instruction! Specify a comparison type and a target in void* form, and
|
|
|
|
// a jump (either 8 or 32 bit) is generated.
|
2016-11-12 15:28:37 +00:00
|
|
|
__emitinline void xJcc(JccComparisonType comparison, const void *target)
|
2009-04-24 11:25:10 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
xJccKnownTarget(comparison, target, false);
|
2009-04-24 11:25:10 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
xForwardJumpBase::xForwardJumpBase(uint opsize, JccComparisonType cctype)
|
2009-11-06 21:45:30 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssert(opsize == 1 || opsize == 4);
|
|
|
|
pxAssertDev(cctype != Jcc_Unknown, "Invalid ForwardJump conditional type.");
|
|
|
|
|
|
|
|
BasePtr = (s8 *)xGetPtr() +
|
|
|
|
((opsize == 1) ? 2 : // j8's are always 2 bytes.
|
|
|
|
((cctype == Jcc_Unconditional) ? 5 : 6)); // j32's are either 5 or 6 bytes
|
|
|
|
|
|
|
|
if (opsize == 1)
|
|
|
|
xWrite8((cctype == Jcc_Unconditional) ? 0xeb : (0x70 | cctype));
|
|
|
|
else {
|
|
|
|
if (cctype == Jcc_Unconditional)
|
|
|
|
xWrite8(0xe9);
|
|
|
|
else {
|
|
|
|
xWrite8(0x0f);
|
|
|
|
xWrite8(0x80 | cctype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xAdvancePtr(opsize);
|
2009-04-24 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
void xForwardJumpBase::_setTarget(uint opsize) const
|
2009-11-06 21:45:30 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssertDev(BasePtr != NULL, "");
|
|
|
|
|
|
|
|
sptr displacement = (sptr)xGetPtr() - (sptr)BasePtr;
|
|
|
|
if (opsize == 1) {
|
|
|
|
pxAssertDev(is_s8(displacement), "Emitter Error: Invalid short jump displacement.");
|
|
|
|
BasePtr[-1] = (s8)displacement;
|
|
|
|
} else {
|
|
|
|
// full displacement, no sanity checks needed :D
|
|
|
|
((s32 *)BasePtr)[-1] = displacement;
|
|
|
|
}
|
2009-11-06 21:45:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the inverted conditional type for this Jcc condition. Ie, JNS will become JS.
|
2016-11-12 15:28:37 +00:00
|
|
|
__fi JccComparisonType xInvertCond(JccComparisonType src)
|
2009-11-06 21:45:30 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssert(src != Jcc_Unknown);
|
|
|
|
if (Jcc_Unconditional == src)
|
|
|
|
return Jcc_Unconditional;
|
2009-11-06 21:45:30 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
// x86 conditionals are clever! To invert conditional types, just invert the lower bit:
|
|
|
|
return (JccComparisonType)((int)src ^ 1);
|
2009-11-06 21:45:30 +00:00
|
|
|
}
|
|
|
|
}
|