Skip to content

Commit

Permalink
core: fix missing operand comma and operand label formatting, analyze…
Browse files Browse the repository at this point in the history
…r disassemble exported and global code ; ui_qt: remove test, add check before displaying invalid cfg ; arch_gb: fix operand formatting and operand types
  • Loading branch information
wisk committed Jul 12, 2014
1 parent 7baca0f commit 2b48804
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 87 deletions.
93 changes: 27 additions & 66 deletions src/arch/gb/gameboy_architecture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ bool GameBoyArchitecture::Disassemble(BinaryStream const& rBinStrm, TOffset Offs
else
Result = (this->*m_OpcodeMap[Opcode])(rBinStrm, Offset, rInsn);

// LATER: clean this
for (u8 i = 0; i < OPERAND_NO; ++i)
{
auto& rOperand = *rInsn.Operand(i);
if (rOperand.GetType() & O_REG)
{
switch (rOperand.GetReg())
{
case GB_RegA: case GB_RegB: case GB_RegC: case GB_RegD:
case GB_RegE: case GB_RegF: case GB_RegH: case GB_RegL:
rOperand.Type() |= O_REG8; break;
case GB_RegAF: case GB_RegBC: case GB_RegDE: case GB_RegHL:
case GB_RegPc: case GB_RegSp:
rOperand.Type() |= O_REG16; break;
default: break;
}
}
if (rOperand.GetType() & O_MEM)
rOperand.Type() |= O_MEM8;

if (rOperand.GetType() & O_REL)
rOperand.Type() |= O_REL16;

if (rOperand.GetType() & O_ABS)
rOperand.Type() |= O_ABS16;
}

return Result;
}

Expand All @@ -161,72 +188,6 @@ u16 GameBoyArchitecture::GetRegisterByOpcode(u8 Opcode)
}
}

bool GameBoyArchitecture::FormatOperand(
Document const& rDoc,
Address const& rAddress,
Instruction const& rInstruction,
Operand const& rOperand,
u8 OperandNo,
PrintData & rPrintData) const
{
// TODO: Handle this info somewhere...
//if (rOperand.GetType() & O_REG)
//{
// switch (rOperand.GetReg())
// {
// case GB_RegA: case GB_RegB: case GB_RegC: case GB_RegD:
// case GB_RegE: case GB_RegF: case GB_RegH: case GB_RegL:
// rOperand.GetType() |= O_REG8; break;
// case GB_RegAF: case GB_RegBC: case GB_RegDE: case GB_RegHL:
// case GB_RegPc: case GB_RegSp:
// rOperand.GetType() |= O_REG16; break;
// default: break;
// }
//}

rPrintData.MarkOffset();

if (rOperand.GetType() & O_MEM)
{
//rOperand.GetType() |= O_MEM8;
rPrintData.AppendOperator("[");
}

if (rOperand.GetType() & O_REG)
{
for (GameBoyArchitecture::TRegName const* pRegName = m_RegName;
pRegName->m_Value != GB_Invalid_Reg; ++pRegName)
{
if (pRegName->m_Value == rOperand.GetReg())
{
rPrintData.AppendRegister(pRegName->m_Name);
break;
}
}
}

u16 Offset = 0;
if (rOperand.GetType() & O_REL)
{
//rOperand.GetType() |= O_REL16;
rPrintData.AppendImmediate(static_cast<u16>((Offset + rOperand.GetValue()) & 0xffff), 16);
}

if (rOperand.GetType() & O_ABS)
{
//rOperand.GetType() |= O_ABS16;
rPrintData.AppendImmediate(static_cast<u16>((Offset + rOperand.GetValue()) & 0xffff), 16);
}

if (rOperand.GetType() & O_IMM)
rPrintData.AppendImmediate(static_cast<u16>((Offset + rOperand.GetValue()) & 0xffff), 16);

if (rOperand.GetType() & O_MEM)
rPrintData.AppendOperator("]");

return true;
}

bool GameBoyArchitecture::Insn_Invalid(BinaryStream const& rBinStrm, TOffset Offset, Instruction& rInsn)
{
rInsn.SetName("invalid");
Expand Down
8 changes: 0 additions & 8 deletions src/arch/gb/gameboy_architecture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ class GameBoyArchitecture : public Architecture
virtual CpuContext* MakeCpuContext(void) const { return nullptr; }
virtual MemoryContext* MakeMemoryContext(void) const { return new MemoryContext(m_CpuInfo); }

virtual bool FormatOperand(
Document const& rDoc,
Address const& rAddress,
Instruction const& rInstruction,
Operand const& rOperand,
u8 OperandNo,
PrintData & rPrintData) const;

private:
typedef bool (GameBoyArchitecture:: *TDisassembler)(BinaryStream const& rBinStrm, TOffset Offset, Instruction& rInsn);

Expand Down
3 changes: 2 additions & 1 deletion src/core/analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,9 @@ void Analyzer::DisassembleAllFunctionsTask::Run(void)
{
u16 LblType = rLabel.GetType() & Label::CellMask;
bool IsExported = ((rLabel.GetType() & Label::AccessMask) == Label::Exported) ? true : false;
bool IsGlobal = ((rLabel.GetType() & Label::AccessMask) == Label::Global) ? true : false;

if (!(LblType == Label::Function || ((LblType == Label::Code) && IsExported)))
if (!(LblType == Label::Function || ((LblType == Label::Code) && (IsExported || IsGlobal))))
return;

Log::Write("core") << "disassembling function " << rAddress << LogEnd;
Expand Down
12 changes: 7 additions & 5 deletions src/core/architecture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ bool Architecture::FormatInstruction(
Instruction const& rInsn,
PrintData & rPrintData) const
{
char Sep = '\0';
char const* Sep = nullptr;

rPrintData.AppendMnemonic(rInsn.GetName());

Expand All @@ -164,8 +164,10 @@ bool Architecture::FormatInstruction(
if (pOprd->GetType() == O_NONE)
break;

if (Sep != '\0')
rPrintData.AppendOperator(",").AppendSpace();
if (Sep != nullptr)
rPrintData.AppendOperator(Sep).AppendSpace();
else
Sep = ",";

if (!FormatOperand(rDoc, rAddr, rInsn, *pOprd, i, rPrintData))
return false;
Expand Down Expand Up @@ -256,8 +258,8 @@ bool Architecture::FormatOperand(

Address OprdAddr = rDoc.MakeAddress(rOprd.GetSegValue(), rOprd.GetValue());
auto Lbl = rDoc.GetLabelFromAddress(OprdAddr);
if (Lbl.GetType() == Label::Unknown)
rPrintData.AppendImmediate(rOprd.GetValue(), rAddr.GetOffsetSize());
if (Lbl.GetType() != Label::Unknown)
rPrintData.AppendLabel(Lbl.GetLabel());
else
rPrintData.AppendAddress(OprdAddr);
}
Expand Down
6 changes: 5 additions & 1 deletion src/ui/qt/ControlFlowGraphScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ ControlFlowGraphScene::ControlFlowGraphScene(QObject * parent, medusa::Medusa& c


medusa::ControlFlowGraph cfg;
core.BuildControlFlowGraph(cfgAddr, cfg);
if (!core.BuildControlFlowGraph(cfgAddr, cfg))
{
medusa::Log::Write("ui_qt") << "failed to build CFG for: " << cfgAddr << medusa::LogEnd;
return;
}

qreal maxBbWidth = 0.0, maxBbHeight = 0.0;

Expand Down
6 changes: 0 additions & 6 deletions src/ui/qt/DisassemblyView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,6 @@ void DisassemblyView::setCursorPosition(int x, int y)
if (!SetCursor(x, y))
return;

medusa::u8 OperandNo;
if (m_PrintData.GetOperandNo(m_Cursor.m_Address, m_Cursor.m_xAddressOffset, m_Cursor.m_yAddressOffset, OperandNo))
{
medusa::Log::Write("ui_qt") << "operand no: " << OperandNo << medusa::LogEnd;
}

_cursorTimer.start();
_cursorBlink = false;
updateCursor();
Expand Down

0 comments on commit 2b48804

Please sign in to comment.