Major fixes to disassembler

This commit is contained in:
Mark Long 2016-10-30 22:45:45 -05:00
parent 570b87dfd5
commit e75ed40ce0
5 changed files with 191 additions and 136 deletions

View File

@ -8,14 +8,17 @@ Disassembler::Disassembler(QByteArray memimage)
{
m_memimage = memimage, makeOpcodeTable();
m_memusagemap.clearData();
}
QList<DisassembledItem> Disassembler::disassemble(quint16 from, quint16 to,
QList<quint16> entryPoints,
bool processRecursively) {
QList<DisassembledItem> retval;
qDebug() << "Disassemble: From"<<uint16ToHex(from)<<"to"<<uint16ToHex(to);
qDebug() << "\n\n*****************\n\nDisassemble: From"<<uint16ToHex(from)<<"to"<<uint16ToHex(to);
//#define OLDDISSEM
#ifdef OLDDISSEM
for (int idx = from; idx <= to; )
{
@ -34,10 +37,11 @@ QList<DisassembledItem> Disassembler::disassemble(quint16 from, quint16 to,
bool stopping = false;
quint16 next = from;
if (entryPoints.count())
while (entryPoints.count())
{
next = entryPoints.takeFirst();
m_jumps.append(entryPoints);
//m_jumps.append(entryPoints);
m_stack.push(next);
}
while (!stopping)
@ -59,18 +63,30 @@ QList<DisassembledItem> Disassembler::disassemble(quint16 from, quint16 to,
if (item.isBranch())
{
qDebug() << "Is Branch";
if (!m_jumps.contains(item.targetAddress()))
{
m_jumps.append(item.targetAddress());
}
m_stack.push(item.targetAddress());
// if (!m_jumps.contains(item.targetAddress()))
// {
// m_jumps.append(item.targetAddress());
// qDebug() << "Appending branch" << uint16ToHex(item.targetAddress()) << "to jump table";
// }
}
if (item.isJsr() && !item.canNotFollow())
{
if (item.targetAddress() <= to) //TODO: Remove this to not limit disassembly to program range
if (!m_jumps.contains(item.targetAddress()))
{
m_jumps.append(item.targetAddress());
if (m_stack.push(item.targetAddress()))
//if (!m_jumps.contains(item.targetAddress()))
{
qDebug() << "Appending" << uint16ToHex(item.targetAddress()) << "to jump table";
//m_jumps.append(item.targetAddress());
}
else
{
qDebug() << "Not adding" << uint16ToHex(item.targetAddress()) << "to jump table";
}
}
}
@ -99,9 +115,11 @@ QList<DisassembledItem> Disassembler::disassemble(quint16 from, quint16 to,
m_memusagemap.merge(memuse);
if (processRecursively)
while (m_jumps.size())
//while (m_jumps.size())
while (!m_stack.isEmpty())
{
quint16 num = m_jumps.takeFirst();
// quint16 num = m_jumps.takeFirst();
quint16 num = m_stack.pop();
if (!m_memusagemap[num].testFlag(Operation))
{
if (num >= from && num <= to) // TODO: remove this to not limit disassembly to program range
@ -173,7 +191,11 @@ bool Disassembler::disassembleOp(quint16 address, DisassembledItem &retval, Memo
if (op.numArgs() == 1)
argval = (quint8) hexValues[1];
else if (op.numArgs() == 2)
argval = (quint8) hexValues[1] + ((quint8) hexValues[2] * 256);
{
argval = makeWord(hexValues[1],hexValues[2]);
}
for (int idx = 0; idx < hexValues.length(); idx++) {
hexValueString.append(QString("%1 ").arg((quint8) hexValues[idx],2,16,QChar('0')));
@ -278,15 +300,18 @@ bool Disassembler::disassembleOp(quint16 address, DisassembledItem &retval, Memo
}
if (opcode == 0x20 || opcode == 0x4c) {
retval.setTargetAddress(hexValues[2]*256 + hexValues[1]);
retval.setTargetAddress(makeWord(hexValues[1],hexValues[2]));
}
if (opcode == 0x4c)
{
qDebug() << "JMP: Setting next flow address to"<<uint16ToHex(hexValues[2]*256 + hexValues[1]);
retval.setNextFlowAddress(hexValues[2]*256 + hexValues[1]);
}
qDebug() << "JMP: Adding flow address "
<< uint16ToHex(makeWord(hexValues[1],hexValues[2])) << "to jump table";
m_stack.push(argval);
retval.setCanNotFollow(true);
}
retval.setAddress(address);
retval.setDisassembledString(disassemblyLine.toUpper());
@ -889,6 +914,7 @@ quint8 AssyInstruction::numArgs() {
}
DisassembledItem::DisassembledItem(AssyInstruction instr) {
m_canNotFollow = false;
setInstruction(instr);
}

View File

@ -8,6 +8,7 @@
#include <QStringList>
#include <QHash>
#include <QDebug>
#include <QStack>
enum AddressMode {
AM_InvalidOp,
@ -29,6 +30,30 @@ enum AddressMode {
};
//////////////////////////////////////////////////////////////////////////////
class AddressStack
{
public:
AddressStack() { }
bool push(quint16 address, bool force = false) {
if (force || (!m_stack.contains(address)))
{
qDebug() << " PUSH: " << uint16ToHex(address);
m_stack.push(address);
return true;
}
return false;
}
bool isEmpty() { return m_stack.isEmpty(); }
quint16 pop() { return m_stack.pop(); }
private:
QStack<quint16> m_stack;
};
//////////////////////////////////////////////////////////////////////////////
struct AssyInstruction {
public:
@ -93,7 +118,7 @@ public:
bool stopsProcessing() {
if (isReturn()) qDebug() << "Is Return";
if (isInvalidOp()) qDebug() << "Is Invalid Op" << uint8ToHex(m_instruction.opcode());
if (canNotFollow()) qDebug() << "Can not follow indirect jump";
if (canNotFollow()) qDebug() << "Not following jump";
if (isBreak()) qDebug() << "Is Break";
return isBreak() || isInvalidOp() || isReturn() || canNotFollow(); }
@ -168,7 +193,7 @@ private:
QHash<quint8,AssyInstruction> m_opcodeinfo;
QByteArray m_memimage;
QList<quint16> m_jumps;
AddressStack m_stack;
MemoryUsageMap m_memusagemap;

View File

@ -39,6 +39,7 @@ public slots:
protected:
QString makeTextStr(QByteArray data);
QString makeHexStr(QByteArray data);
private:
Ui::TextHexDumpViewer *ui;

View File

@ -111,7 +111,7 @@ void ViewerBase::setFile(GenericFile *file)
}
else if (dynamic_cast<TextFile*>(file))
{
BinaryFile *bf = dynamic_cast<BinaryFile*>(file);
TextFile *bf = dynamic_cast<TextFile*>(file);
TextHexDumpViewer *thdv = new TextHexDumpViewer();
thdv->setFile(bf);

View File

@ -69,7 +69,10 @@ inline QString uint32ToHex(quint32 val) {
return retval;
}
inline quint16 makeWord(quint8 lo, quint8 hi)
{
return hi*256 + lo;
}