fxos/lib/passes/print.cpp

70 lines
1.3 KiB
C++

//---
// fxos.passes.print: Print disassembly
//---
#include <fxos/disasm-passes/print.h>
#include <fxos/disassembly.h>
namespace FxOS {
PrintPass::PrintPass(Disassembly &disasm):
DisassemblyPass(disasm)
{
/* Default parameter set */
hide_resolved_pcjump = false;
hide_resolved_pcrel = false;
hide_movpc_address = Hide_MovPC_Never;
}
void PrintPass::run(void)
{
for(auto &pair: m_disasm.instructions())
{
analyze(pair.first, pair.second);
}
}
void PrintPass::analyze(uint32_t pc, ConcreteInstruction &ci)
{
Instruction const &i = ci.inst;
/* Mnemonic */
static std::map<int, std::string> suffixes = {
{ 1, ".b" }, { 2, ".w" }, { 4, ".l" } };
std::string mnemonic = i.mnemonic + suffixes[i.opsize];
if(i.args.size())
mnemonic += std::string(8 - mnemonic.size(), ' ');
printf(" %08x: %04x %s", pc, ci.inst.opcode, mnemonic.c_str());
/* Arguments */
for(size_t n = 0; n < i.args.size(); n++)
{
auto &a = i.args[n];
Location &l = ci.args[n].location;
if(n) printf(", ");
if(a.kind == Argument::PcJump && l && hide_resolved_pcjump)
{
printf("<%s>", l.str().c_str());
}
else if(a.kind == Argument::PcRel && l && hide_resolved_pcrel)
{
printf("<%s>", l.str().c_str());
}
else
{
printf("%s", a.str().c_str());
if(l) printf(" <%s>", l.str().c_str());
}
}
printf("\n");
}
} /* namespace FxOS */