Prettier ppc disasm.

This commit is contained in:
Ben Vanik
2013-12-22 13:06:15 -08:00
parent de6dc92663
commit d861ef8aab
11 changed files with 311 additions and 21 deletions

View File

@@ -9,6 +9,8 @@
#include <alloy/frontend/ppc/ppc_scanner.h>
#include <map>
#include <alloy/frontend/tracing.h>
#include <alloy/frontend/ppc/ppc_frontend.h>
#include <alloy/frontend/ppc/ppc_instr.h>
@@ -278,3 +280,81 @@ int PPCScanner::FindExtents(FunctionInfo* symbol_info) {
XELOGSDB("Finished analyzing %.8X", start_address);
return 0;
}
std::vector<BlockInfo> PPCScanner::FindBlocks(FunctionInfo* symbol_info) {
Memory* memory = frontend_->memory();
const uint8_t* p = memory->membase();
std::map<uint64_t, BlockInfo> block_map;
uint64_t start_address = symbol_info->address();
uint64_t end_address = symbol_info->end_address();
bool in_block = false;
uint64_t block_start = 0;
InstrData i;
for (uint64_t address = start_address;
address <= end_address; address += 4) {
i.address = address;
i.code = XEGETUINT32BE(p + address);
if (!i.code) {
continue;
}
// TODO(benvanik): find a way to avoid using the opcode tables.
// This lookup is *expensive* and should be avoided when scanning.
i.type = GetInstrType(i.code);
if (!in_block) {
in_block = true;
block_start = address;
}
bool ends_block = false;
if (!i.type) {
// Invalid instruction.
} else if (i.code == 0x4E800020) {
// blr -- unconditional branch to LR.
ends_block = true;
} else if (i.code == 0x4E800420) {
// bctr -- unconditional branch to CTR.
ends_block = true;
} else if (i.type->opcode == 0x48000000) {
// b/ba/bl/bla
uint32_t target =
(uint32_t)XEEXTS26(i.I.LI << 2) + (i.I.AA ? 0 : (int32_t)address);
ends_block = true;
} else if (i.type->opcode == 0x40000000) {
// bc/bca/bcl/bcla
uint32_t target =
(uint32_t)XEEXTS16(i.B.BD << 2) + (i.B.AA ? 0 : (int32_t)address);
ends_block = true;
} else if (i.type->opcode == 0x4C000020) {
// bclr/bclrl
ends_block = true;
} else if (i.type->opcode == 0x4C000420) {
// bcctr/bcctrl
ends_block = true;
}
if (ends_block) {
in_block = false;
block_map[block_start] = {
block_start,
address,
};
}
}
if (in_block) {
block_map[block_start] = {
block_start,
end_address,
};
}
std::vector<BlockInfo> blocks;
for (auto it = block_map.begin(); it != block_map.end(); ++it) {
blocks.push_back(it->second);
}
return blocks;
}

View File

@@ -20,6 +20,11 @@ namespace ppc {
class PPCFrontend;
typedef struct BlockInfo_t {
uint64_t start_address;
uint64_t end_address;
} BlockInfo;
class PPCScanner {
public:
@@ -28,6 +33,8 @@ public:
int FindExtents(runtime::FunctionInfo* symbol_info);
std::vector<BlockInfo> FindBlocks(runtime::FunctionInfo* symbol_info);
private:
bool IsRestGprLr(uint64_t address);

View File

@@ -82,6 +82,10 @@ int PPCTranslator::Translate(
DumpSource(symbol_info, &string_buffer_);
debug_info->set_source_disasm(string_buffer_.ToString());
string_buffer_.Reset();
DumpSourceJson(symbol_info, &string_buffer_);
debug_info->set_source_json(string_buffer_.ToString());
string_buffer_.Reset();
}
// Emit function.
@@ -134,9 +138,12 @@ void PPCTranslator::DumpSource(
symbol_info->address(), symbol_info->end_address(),
"(symbol name)");
auto blocks = scanner_->FindBlocks(symbol_info);
uint64_t start_address = symbol_info->address();
uint64_t end_address = symbol_info->end_address();
InstrData i;
auto block_it = blocks.begin();
for (uint64_t address = start_address, offset = 0; address <= end_address;
address += 4, offset++) {
i.address = address;
@@ -144,7 +151,13 @@ void PPCTranslator::DumpSource(
// TODO(benvanik): find a way to avoid using the opcode tables.
i.type = GetInstrType(i.code);
// TODO(benvanik): labels and such
// Check labels.
if (block_it != blocks.end() &&
block_it->start_address == address) {
string_buffer->Append(
"%.8X loc_%.8X:\n", address, address);
++block_it;
}
if (!i.type) {
string_buffer->Append("%.8X %.8X ???", address, i.code);
@@ -160,3 +173,68 @@ void PPCTranslator::DumpSource(
string_buffer->Append("\n");
}
}
void PPCTranslator::DumpSourceJson(
runtime::FunctionInfo* symbol_info, StringBuffer* string_buffer) {
Memory* memory = frontend_->memory();
const uint8_t* p = memory->membase();
string_buffer->Append("{\n");
auto blocks = scanner_->FindBlocks(symbol_info);
string_buffer->Append("\"blocks\": [\n");
for (auto it = blocks.begin(); it != blocks.end(); ++it) {
string_buffer->Append("{ \"start\": %u, \"end\": %u }%c",
it->start_address, it->end_address,
(it + 1 != blocks.end()) ? ',' : ' ');
}
string_buffer->Append("],\n");
string_buffer->Append("\"lines\": [\n");
uint64_t start_address = symbol_info->address();
uint64_t end_address = symbol_info->end_address();
InstrData i;
auto block_it = blocks.begin();
for (uint64_t address = start_address, offset = 0; address <= end_address;
address += 4, offset++) {
i.address = address;
i.code = XEGETUINT32BE(p + address);
// TODO(benvanik): find a way to avoid using the opcode tables.
i.type = GetInstrType(i.code);
// Check labels.
if (block_it != blocks.end() &&
block_it->start_address == address) {
if (address != start_address) {
// Whitespace to pad blocks.
string_buffer->Append(
"[\"c\", %u, 0, \"\", \"\"],\n",
address);
}
string_buffer->Append(
"[\"l\", %u, 0, \"loc_%.8X:\", \"\"],\n",
address, address);
++block_it;
}
const char* disasm_str = "";
const char* comment_str = "";
std::string disasm;
if (!i.type) {
disasm_str = "?";
} else if (i.type->disassemble) {
ppc::InstrDisasm d;
i.type->disassemble(i, d);
d.Dump(disasm);
disasm_str = disasm.c_str();
} else {
disasm_str = i.type->name;
}
string_buffer->Append("[\"i\", %u, %u, \" %s\", \"%s\"]%c\n",
address, i.code, disasm_str, comment_str,
(address + 4 <= end_address) ? ',' : ' ');
}
string_buffer->Append("]\n");
string_buffer->Append("}\n");
}

View File

@@ -37,6 +37,8 @@ public:
private:
void DumpSource(runtime::FunctionInfo* symbol_info,
StringBuffer* string_buffer);
void DumpSourceJson(runtime::FunctionInfo* symbol_info,
StringBuffer* string_buffer);
private:
PPCFrontend* frontend_;

View File

@@ -14,7 +14,7 @@ using namespace alloy::runtime;
DebugInfo::DebugInfo() :
source_disasm_(0),
source_disasm_(0), source_json_(0),
raw_hir_disasm_(0),
hir_disasm_(0),
raw_lir_disasm_(0),
@@ -24,6 +24,7 @@ DebugInfo::DebugInfo() :
DebugInfo::~DebugInfo() {
xe_free(source_disasm_);
xe_free(source_json_);
xe_free(raw_hir_disasm_);
xe_free(hir_disasm_);
xe_free(raw_lir_disasm_);

View File

@@ -24,6 +24,8 @@ public:
const char* source_disasm() const { return source_disasm_; }
void set_source_disasm(char* value) { source_disasm_ = value; }
const char* source_json() const { return source_json_; }
void set_source_json(char* value) { source_json_ = value; }
const char* raw_hir_disasm() const { return raw_hir_disasm_; }
void set_raw_hir_disasm(char* value) { raw_hir_disasm_ = value; }
const char* hir_disasm() const { return hir_disasm_; }
@@ -42,6 +44,7 @@ public:
private:
char* source_disasm_;
char* source_json_;
char* raw_hir_disasm_;
char* hir_disasm_;
char* raw_lir_disasm_;

View File

@@ -256,7 +256,7 @@ json_t* Processor::OnDebugRequest(
json_t* disasm_json = json_object();
json_t* disasm_str_json;
disasm_str_json = json_string(debug_info->source_disasm());
disasm_str_json = json_loads(debug_info->source_json(), 0, NULL);
json_object_set_new(disasm_json, "source", disasm_str_json);
disasm_str_json = json_string(debug_info->raw_hir_disasm());
json_object_set_new(disasm_json, "rawHir", disasm_str_json);