2007-12-27 20:35:09 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "MIPSInstructionFactory.h"
|
|
|
|
#include "MIPS.h"
|
|
|
|
#include "PtrMacro.h"
|
|
|
|
#include "offsetof_def.h"
|
|
|
|
|
2009-03-30 04:57:52 +00:00
|
|
|
CMIPSInstructionFactory::CMIPSInstructionFactory(MIPS_REGSIZE nRegSize) :
|
|
|
|
m_pCtx(NULL),
|
|
|
|
m_codeGen(NULL),
|
|
|
|
m_nAddress(0),
|
|
|
|
m_nOpcode(0),
|
|
|
|
m_regSize(nRegSize)
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
2009-03-30 04:57:52 +00:00
|
|
|
|
2007-12-27 20:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CMIPSInstructionFactory::~CMIPSInstructionFactory()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
void CMIPSInstructionFactory::SetupQuickVariables(uint32 nAddress, CMipsJitter* codeGen, CMIPS* pCtx)
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
|
|
|
m_pCtx = pCtx;
|
2008-04-09 02:52:38 +00:00
|
|
|
m_codeGen = codeGen;
|
2007-12-27 20:35:09 +00:00
|
|
|
m_nAddress = nAddress;
|
|
|
|
|
2008-06-15 19:55:28 +00:00
|
|
|
m_nOpcode = m_pCtx->m_pMemoryMap->GetInstruction(m_nAddress);
|
2007-12-27 20:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMIPSInstructionFactory::ComputeMemAccessAddr()
|
|
|
|
{
|
2009-05-02 15:34:39 +00:00
|
|
|
uint8 nRS = (uint8) ((m_nOpcode >> 21) & 0x001F);
|
|
|
|
uint16 nImmediate = (uint16)((m_nOpcode >> 0) & 0xFFFF);
|
2007-12-27 20:35:09 +00:00
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
if(m_pCtx->m_pAddrTranslator == &CMIPS::TranslateAddress64)
|
|
|
|
{
|
|
|
|
m_codeGen->PushRel(offsetof(CMIPS, m_State.nGPR[nRS].nV[0]));
|
|
|
|
if(nImmediate != 0)
|
|
|
|
{
|
|
|
|
m_codeGen->PushCst((int16)nImmediate);
|
|
|
|
m_codeGen->Add();
|
|
|
|
}
|
|
|
|
m_codeGen->PushCst(0x1FFFFFFF);
|
|
|
|
m_codeGen->And();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: Compute the complete 64-bit address
|
2007-12-27 20:35:09 +00:00
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
//Translate the address
|
2007-12-27 20:35:09 +00:00
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
//Push context
|
|
|
|
m_codeGen->PushCtx();
|
2007-12-27 20:35:09 +00:00
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
//Push high part
|
|
|
|
m_codeGen->PushRel(offsetof(CMIPS, m_State.nGPR[nRS].nV[1]));
|
2007-12-27 20:35:09 +00:00
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
//Push low part of address
|
|
|
|
m_codeGen->PushRel(offsetof(CMIPS, m_State.nGPR[nRS].nV[0]));
|
|
|
|
if(nImmediate != 0)
|
|
|
|
{
|
|
|
|
m_codeGen->PushCst((int16)nImmediate);
|
|
|
|
m_codeGen->Add();
|
|
|
|
}
|
2007-12-27 20:35:09 +00:00
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
//Call
|
|
|
|
m_codeGen->Call(reinterpret_cast<void*>(m_pCtx->m_pAddrTranslator), 3, true);
|
|
|
|
}
|
2007-12-27 20:35:09 +00:00
|
|
|
}
|
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
void CMIPSInstructionFactory::Branch(Jitter::CONDITION condition)
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
|
|
|
uint16 nImmediate = (uint16)(m_nOpcode & 0xFFFF);
|
|
|
|
|
|
|
|
m_codeGen->PushCst(MIPS_INVALID_PC);
|
|
|
|
m_codeGen->PullRel(offsetof(CMIPS, m_State.nDelayedJumpAddr));
|
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
m_codeGen->BeginIf(condition);
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
|
|
|
m_codeGen->PushCst((m_nAddress + 4) + CMIPS::GetBranch(nImmediate));
|
|
|
|
m_codeGen->PullRel(offsetof(CMIPS, m_State.nDelayedJumpAddr));
|
|
|
|
}
|
|
|
|
m_codeGen->EndIf();
|
|
|
|
}
|
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
void CMIPSInstructionFactory::BranchLikely(Jitter::CONDITION condition)
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
|
|
|
uint16 nImmediate = (uint16)(m_nOpcode & 0xFFFF);
|
|
|
|
|
|
|
|
m_codeGen->PushCst(MIPS_INVALID_PC);
|
|
|
|
m_codeGen->PullRel(offsetof(CMIPS, m_State.nDelayedJumpAddr));
|
|
|
|
|
2010-08-11 03:47:19 +00:00
|
|
|
m_codeGen->BeginIf(condition);
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
|
|
|
m_codeGen->PushCst((m_nAddress + 4) + CMIPS::GetBranch(nImmediate));
|
|
|
|
m_codeGen->PullRel(offsetof(CMIPS, m_State.nDelayedJumpAddr));
|
|
|
|
}
|
2010-08-11 03:47:19 +00:00
|
|
|
m_codeGen->Else();
|
2007-12-27 20:35:09 +00:00
|
|
|
{
|
2010-08-11 03:47:19 +00:00
|
|
|
m_codeGen->PushCst(m_nAddress + 8);
|
2007-12-27 20:35:09 +00:00
|
|
|
m_codeGen->PullRel(offsetof(CMIPS, m_State.nPC));
|
2010-08-11 03:47:19 +00:00
|
|
|
m_codeGen->Goto(m_codeGen->GetFinalBlockLabel());
|
2007-12-27 20:35:09 +00:00
|
|
|
}
|
|
|
|
m_codeGen->EndIf();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMIPSInstructionFactory::Illegal()
|
|
|
|
{
|
2010-08-11 03:47:19 +00:00
|
|
|
throw std::runtime_error("Illegal instruction.");
|
2007-12-27 20:35:09 +00:00
|
|
|
}
|