2007-12-06 23:05:38 +00:00
|
|
|
#include "MipsExecutor.h"
|
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
CMipsExecutor::CMipsExecutor(CMIPS& context, uint32 maxAddress)
|
|
|
|
: m_context(context)
|
|
|
|
, m_subTableCount(0)
|
2007-12-06 23:05:38 +00:00
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
if(maxAddress < 0x10000)
|
|
|
|
{
|
|
|
|
maxAddress = 0x10000;
|
|
|
|
}
|
|
|
|
assert((maxAddress & 0xFFFF) == 0);
|
|
|
|
if(maxAddress == 0)
|
|
|
|
{
|
|
|
|
m_subTableCount = 0x10000;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_subTableCount = maxAddress / 0x10000;
|
|
|
|
}
|
|
|
|
m_blockTable = new CBasicBlock**[m_subTableCount];
|
|
|
|
memset(m_blockTable, 0, sizeof(CBasicBlock**) * m_subTableCount);
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CMipsExecutor::~CMipsExecutor()
|
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
for(unsigned int i = 0; i < m_subTableCount; i++)
|
|
|
|
{
|
|
|
|
CBasicBlock** subTable = m_blockTable[i];
|
|
|
|
if(subTable != NULL)
|
|
|
|
{
|
|
|
|
delete [] subTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete [] m_blockTable;
|
2010-11-17 03:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMipsExecutor::Reset()
|
|
|
|
{
|
|
|
|
ClearActiveBlocks();
|
2007-12-17 04:08:46 +00:00
|
|
|
}
|
2007-12-06 23:05:38 +00:00
|
|
|
|
2010-11-17 03:59:29 +00:00
|
|
|
void CMipsExecutor::ClearActiveBlocks()
|
2007-12-17 04:08:46 +00:00
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
for(unsigned int i = 0; i < m_subTableCount; i++)
|
|
|
|
{
|
|
|
|
CBasicBlock** subTable = m_blockTable[i];
|
|
|
|
if(subTable != NULL)
|
|
|
|
{
|
|
|
|
delete [] subTable;
|
|
|
|
m_blockTable[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-11-17 03:59:29 +00:00
|
|
|
|
|
|
|
m_blocks.clear();
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CMipsExecutor::Execute(int cycles)
|
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
CBasicBlock* block(NULL);
|
2011-02-26 22:42:59 +00:00
|
|
|
#ifdef DEBUGGER_INCLUDED
|
|
|
|
bool firstExec = true;
|
|
|
|
#endif
|
2012-03-13 06:16:33 +00:00
|
|
|
while(cycles > 0)
|
|
|
|
{
|
2011-02-26 22:42:59 +00:00
|
|
|
#ifdef DEBUGGER_INCLUDED
|
2012-03-13 06:16:33 +00:00
|
|
|
if(!firstExec && MustBreak()) break;
|
2011-02-26 22:42:59 +00:00
|
|
|
firstExec = false;
|
|
|
|
#endif
|
2012-03-13 06:16:33 +00:00
|
|
|
uint32 address = m_context.m_pAddrTranslator(&m_context, 0, m_context.m_State.nPC);
|
|
|
|
if(!block || address != block->GetBeginAddress())
|
|
|
|
{
|
|
|
|
block = FindBlockStartingAt(address);
|
|
|
|
if(block == NULL)
|
|
|
|
{
|
|
|
|
//We need to partition the space and compile the blocks
|
|
|
|
PartitionFunction(address);
|
|
|
|
block = FindBlockStartingAt(address);
|
|
|
|
if(block == NULL)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Couldn't create block starting at address.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!block->IsCompiled())
|
|
|
|
{
|
|
|
|
block->Compile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(block != NULL)
|
|
|
|
{
|
|
|
|
block->SetSelfLoopCount(block->GetSelfLoopCount() + 1);
|
|
|
|
}
|
2009-11-18 02:23:38 +00:00
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
cycles -= block->Execute();
|
|
|
|
if(m_context.m_State.nHasException) break;
|
|
|
|
}
|
|
|
|
return cycles;
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
2009-06-06 15:38:03 +00:00
|
|
|
bool CMipsExecutor::MustBreak() const
|
2007-12-06 23:05:38 +00:00
|
|
|
{
|
2007-12-12 03:09:57 +00:00
|
|
|
#ifdef DEBUGGER_INCLUDED
|
2012-03-13 06:16:33 +00:00
|
|
|
uint32 currentPc = m_context.m_pAddrTranslator(&m_context, 0, m_context.m_State.nPC);
|
|
|
|
BasicBlockPtr block = FindBlockAt(currentPc);
|
|
|
|
for(CMIPS::BreakpointSet::const_iterator breakPointIterator(m_context.m_breakpoints.begin());
|
|
|
|
breakPointIterator != m_context.m_breakpoints.end(); breakPointIterator++)
|
|
|
|
{
|
|
|
|
uint32 breakPointAddress = *breakPointIterator;
|
|
|
|
if(currentPc == breakPointAddress) return true;
|
|
|
|
if(block != NULL)
|
|
|
|
{
|
|
|
|
if(breakPointAddress >= block->GetBeginAddress() && breakPointAddress <= block->GetEndAddress()) return true;
|
|
|
|
}
|
|
|
|
}
|
2007-12-12 03:09:57 +00:00
|
|
|
#endif
|
2012-03-13 06:16:33 +00:00
|
|
|
return false;
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
CBasicBlock* CMipsExecutor::FindBlockAt(uint32 address) const
|
2007-12-06 23:05:38 +00:00
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
uint32 hiAddress = address >> 16;
|
|
|
|
uint32 loAddress = address & 0xFFFF;
|
|
|
|
assert(hiAddress < m_subTableCount);
|
|
|
|
CBasicBlock**& subTable = m_blockTable[hiAddress];
|
|
|
|
if(subTable == NULL) return NULL;
|
|
|
|
CBasicBlock* result = subTable[loAddress / 4];
|
|
|
|
return result;
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
CBasicBlock* CMipsExecutor::FindBlockStartingAt(uint32 address) const
|
2007-12-06 23:05:38 +00:00
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
uint32 hiAddress = address >> 16;
|
|
|
|
uint32 loAddress = address & 0xFFFF;
|
|
|
|
assert(hiAddress < m_subTableCount);
|
|
|
|
CBasicBlock**& subTable = m_blockTable[hiAddress];
|
|
|
|
if(subTable == NULL) return NULL;
|
|
|
|
CBasicBlock* result = subTable[loAddress / 4];
|
|
|
|
if((address != 0) && (FindBlockAt(address - 4) == result))
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return result;
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMipsExecutor::CreateBlock(uint32 start, uint32 end)
|
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
{
|
|
|
|
CBasicBlock* block = FindBlockAt(start);
|
|
|
|
if(block)
|
|
|
|
{
|
|
|
|
//If the block starts and ends at the same place, block already exists and doesn't need
|
|
|
|
//to be re-created
|
|
|
|
uint32 otherBegin = block->GetBeginAddress();
|
|
|
|
uint32 otherEnd = block->GetEndAddress();
|
|
|
|
if((otherBegin == start) && (otherEnd == end))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(otherEnd == end)
|
|
|
|
{
|
|
|
|
//Repartition the existing block if end of both blocks are the same
|
|
|
|
DeleteBlock(block);
|
|
|
|
CreateBlock(otherBegin, start - 4);
|
|
|
|
assert(FindBlockAt(start) == NULL);
|
|
|
|
}
|
|
|
|
else if(otherBegin == start)
|
|
|
|
{
|
|
|
|
DeleteBlock(block);
|
|
|
|
CreateBlock(end + 4, otherEnd);
|
|
|
|
assert(FindBlockAt(end) == NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Delete the currently existing block otherwise
|
|
|
|
printf("MipsExecutor: Warning. Deleting block at %0.8X.\r\n", block->GetEndAddress());
|
|
|
|
DeleteBlock(block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(FindBlockAt(end) == NULL);
|
|
|
|
{
|
|
|
|
BasicBlockPtr block = BlockFactory(m_context, start, end);
|
|
|
|
m_blocks.push_back(block);
|
|
|
|
for(uint32 address = block->GetBeginAddress(); address <= block->GetEndAddress(); address += 4)
|
|
|
|
{
|
|
|
|
uint32 hiAddress = address >> 16;
|
|
|
|
uint32 loAddress = address & 0xFFFF;
|
|
|
|
assert(hiAddress < m_subTableCount);
|
|
|
|
CBasicBlock**& subTable = m_blockTable[hiAddress];
|
|
|
|
if(subTable == NULL)
|
|
|
|
{
|
|
|
|
const uint32 subTableSize = 0x10000 / 4;
|
|
|
|
subTable = new CBasicBlock*[subTableSize];
|
|
|
|
memset(subTable, 0, sizeof(CBasicBlock*) * subTableSize);
|
|
|
|
}
|
|
|
|
assert(subTable[loAddress / 4] == NULL);
|
|
|
|
subTable[loAddress / 4] = block.get();
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
void CMipsExecutor::DeleteBlock(CBasicBlock* block)
|
2008-01-03 07:42:54 +00:00
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
for(uint32 address = block->GetBeginAddress(); address <= block->GetEndAddress(); address += 4)
|
|
|
|
{
|
|
|
|
uint32 hiAddress = address >> 16;
|
|
|
|
uint32 loAddress = address & 0xFFFF;
|
|
|
|
assert(hiAddress < m_subTableCount);
|
|
|
|
CBasicBlock**& subTable = m_blockTable[hiAddress];
|
|
|
|
assert(subTable != NULL);
|
|
|
|
assert(subTable[loAddress / 4] != NULL);
|
|
|
|
subTable[loAddress / 4] = NULL;
|
|
|
|
}
|
2008-08-24 21:28:42 +00:00
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
//Remove block from our lists
|
|
|
|
m_blocks.remove(block);
|
2008-01-03 07:42:54 +00:00
|
|
|
}
|
|
|
|
|
2010-11-17 03:59:29 +00:00
|
|
|
BasicBlockPtr CMipsExecutor::BlockFactory(CMIPS& context, uint32 start, uint32 end)
|
2009-06-06 15:38:03 +00:00
|
|
|
{
|
2010-11-17 03:59:29 +00:00
|
|
|
return BasicBlockPtr(new CBasicBlock(context, start, end));
|
2009-06-06 15:38:03 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 23:05:38 +00:00
|
|
|
void CMipsExecutor::PartitionFunction(uint32 functionAddress)
|
|
|
|
{
|
2012-03-13 06:16:33 +00:00
|
|
|
typedef std::set<uint32> PartitionPointSet;
|
|
|
|
uint32 endAddress = 0;
|
|
|
|
PartitionPointSet partitionPoints;
|
2007-12-06 23:05:38 +00:00
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
//Insert begin point
|
|
|
|
partitionPoints.insert(functionAddress);
|
2007-12-06 23:05:38 +00:00
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
//Find the end
|
|
|
|
for(uint32 address = functionAddress; ; address += 4)
|
|
|
|
{
|
|
|
|
//Probably going too far...
|
|
|
|
if((address - functionAddress) > 0x10000)
|
|
|
|
{
|
|
|
|
printf("MipsExecutor: Warning. Found no JR after a big distance.\r\n");
|
|
|
|
endAddress = address;
|
|
|
|
partitionPoints.insert(endAddress);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint32 opcode = m_context.m_pMemoryMap->GetInstruction(address);
|
|
|
|
if(opcode == 0x03E00008)
|
|
|
|
{
|
|
|
|
//+4 for delay slot
|
|
|
|
endAddress = address + 4;
|
|
|
|
partitionPoints.insert(endAddress + 4);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 23:05:38 +00:00
|
|
|
|
2012-03-13 06:16:33 +00:00
|
|
|
//Find partition points within the function
|
|
|
|
for(uint32 address = functionAddress; address <= endAddress; address += 4)
|
|
|
|
{
|
|
|
|
uint32 opcode = m_context.m_pMemoryMap->GetInstruction(address);
|
2011-12-10 20:49:50 +00:00
|
|
|
MIPS_BRANCH_TYPE branchType = m_context.m_pArch->IsInstructionBranch(&m_context, address, opcode);
|
2012-03-13 06:16:33 +00:00
|
|
|
if(branchType == MIPS_BRANCH_NORMAL)
|
|
|
|
{
|
|
|
|
partitionPoints.insert(address + 8);
|
|
|
|
uint32 target = m_context.m_pArch->GetInstructionEffectiveAddress(&m_context, address, opcode);
|
|
|
|
if(target > functionAddress && target < endAddress)
|
|
|
|
{
|
|
|
|
partitionPoints.insert(target);
|
|
|
|
}
|
|
|
|
}
|
2011-12-10 20:49:50 +00:00
|
|
|
else if(branchType == MIPS_BRANCH_NODELAY)
|
|
|
|
{
|
|
|
|
partitionPoints.insert(address + 4);
|
|
|
|
}
|
2012-03-13 06:16:33 +00:00
|
|
|
//Check if there's a block already exising that this address
|
|
|
|
if(address != endAddress)
|
|
|
|
{
|
|
|
|
BasicBlockPtr possibleBlock = FindBlockStartingAt(address);
|
|
|
|
if(possibleBlock)
|
|
|
|
{
|
|
|
|
//assert(possibleBlock->GetEndAddress() <= endAddress);
|
|
|
|
//Add its beginning and end in the partition points
|
|
|
|
partitionPoints.insert(possibleBlock->GetBeginAddress());
|
|
|
|
partitionPoints.insert(possibleBlock->GetEndAddress() + 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 23:05:38 +00:00
|
|
|
|
2011-12-10 20:49:50 +00:00
|
|
|
//Check if blocks are too big
|
|
|
|
{
|
|
|
|
uint32 currentPoint = -1;
|
|
|
|
for(PartitionPointSet::const_iterator pointIterator(partitionPoints.begin());
|
|
|
|
pointIterator != partitionPoints.end(); pointIterator++)
|
|
|
|
{
|
|
|
|
if(currentPoint != -1)
|
|
|
|
{
|
|
|
|
uint32 startPos = currentPoint;
|
|
|
|
uint32 endPos = *pointIterator;
|
|
|
|
uint32 distance = (endPos - startPos);
|
|
|
|
if(distance > 0x400)
|
|
|
|
{
|
|
|
|
uint32 middlePos = ((endPos + startPos) / 2) & ~0x03;
|
|
|
|
pointIterator = partitionPoints.insert(middlePos).first;
|
|
|
|
pointIterator--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
currentPoint = *pointIterator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create blocks
|
|
|
|
{
|
|
|
|
uint32 currentPoint = -1;
|
|
|
|
for(PartitionPointSet::const_iterator pointIterator(partitionPoints.begin());
|
|
|
|
pointIterator != partitionPoints.end(); pointIterator++)
|
|
|
|
{
|
|
|
|
if(currentPoint != -1)
|
|
|
|
{
|
|
|
|
CreateBlock(currentPoint, *pointIterator - 4);
|
|
|
|
}
|
|
|
|
currentPoint = *pointIterator;
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 23:05:38 +00:00
|
|
|
}
|