Speeding up source map lookup for x64 disasm.

This commit is contained in:
Ben Vanik 2015-06-10 00:29:07 -07:00
parent 14e1438ec0
commit 9c58426231
4 changed files with 25 additions and 19 deletions

View File

@ -101,6 +101,11 @@ bool X64Assembler::Assemble(FunctionInfo* symbol_info, HIRBuilder* builder,
void X64Assembler::DumpMachineCode(DebugInfo* debug_info, void* machine_code,
size_t code_size, StringBuffer* str) {
auto source_map_entries = debug_info->source_map_entries();
auto source_map_count = debug_info->source_map_count();
auto source_map_index = 0;
uint32_t next_code_offset = source_map_entries[0].code_offset;
BE::DISASM disasm = {0};
disasm.Archi = 64;
disasm.Options = BE::Tabulation + BE::MasmSyntax + BE::PrefixedNumeral;
@ -109,24 +114,23 @@ void X64Assembler::DumpMachineCode(DebugInfo* debug_info, void* machine_code,
uint64_t prev_source_offset = 0;
while (disasm.EIP < eip_end) {
// Look up source offset.
auto map_entry = debug_info->LookupCodeOffset(
static_cast<uint32_t>(disasm.EIP - (BE::UIntPtr)machine_code));
if (map_entry) {
if (map_entry->source_offset == prev_source_offset) {
str->Append(" ");
} else {
str->AppendFormat("%.8X ", map_entry->source_offset);
prev_source_offset = map_entry->source_offset;
}
auto code_offset =
static_cast<uint32_t>(disasm.EIP - (BE::UIntPtr)machine_code);
if (code_offset >= next_code_offset &&
source_map_index < source_map_count) {
auto& source_map_entry = source_map_entries[source_map_index];
str->AppendFormat("%.8X ", source_map_entry.source_offset);
++source_map_index;
next_code_offset = source_map_entries[source_map_index].code_offset;
} else {
str->Append("? ");
str->Append(" ");
}
size_t len = BE::Disasm(&disasm);
if (len == BE::UNKNOWN_OPCODE) {
break;
}
str->AppendFormat("%p %s\n", disasm.EIP, disasm.CompleteInstr);
str->AppendFormat("%.8X %s\n", uint32_t(disasm.EIP), disasm.CompleteInstr);
disasm.EIP += len;
}
}

View File

@ -255,7 +255,7 @@ void X64Emitter::MarkSourceOffset(const Instr* i) {
auto entry = source_map_arena_.Alloc<SourceMapEntry>();
entry->source_offset = static_cast<uint32_t>(i->src1.offset);
entry->hir_offset = uint32_t(i->block->ordinal << 16) | i->ordinal;
entry->code_offset = static_cast<uint32_t>(getSize());
entry->code_offset = static_cast<uint32_t>(getSize() + 1);
source_map_count_++;
if (FLAGS_debug) {

View File

@ -20,10 +20,10 @@ DebugInfo::DebugInfo()
hir_disasm_(nullptr),
machine_code_disasm_(nullptr),
source_map_count_(0),
source_map_(nullptr) {}
source_map_entries_(nullptr) {}
DebugInfo::~DebugInfo() {
free(source_map_);
free(source_map_entries_);
free(source_disasm_);
free(raw_hir_disasm_);
free(hir_disasm_);
@ -33,7 +33,7 @@ DebugInfo::~DebugInfo() {
void DebugInfo::InitializeSourceMap(size_t source_map_count,
SourceMapEntry* source_map) {
source_map_count_ = source_map_count;
source_map_ = source_map;
source_map_entries_ = source_map;
// TODO(benvanik): ensure sorted in some way? MC offset?
}
@ -41,7 +41,7 @@ void DebugInfo::InitializeSourceMap(size_t source_map_count,
SourceMapEntry* DebugInfo::LookupSourceOffset(uint32_t offset) {
// TODO(benvanik): binary search? We know the list is sorted by code order.
for (size_t n = 0; n < source_map_count_; n++) {
auto entry = &source_map_[n];
auto entry = &source_map_entries_[n];
if (entry->source_offset == offset) {
return entry;
}
@ -52,7 +52,7 @@ SourceMapEntry* DebugInfo::LookupSourceOffset(uint32_t offset) {
SourceMapEntry* DebugInfo::LookupHIROffset(uint32_t offset) {
// TODO(benvanik): binary search? We know the list is sorted by code order.
for (size_t n = 0; n < source_map_count_; n++) {
auto entry = &source_map_[n];
auto entry = &source_map_entries_[n];
if (entry->hir_offset >= offset) {
return entry;
}
@ -63,7 +63,7 @@ SourceMapEntry* DebugInfo::LookupHIROffset(uint32_t offset) {
SourceMapEntry* DebugInfo::LookupCodeOffset(uint32_t offset) {
// TODO(benvanik): binary search? We know the list is sorted by code order.
for (int64_t n = source_map_count_ - 1; n >= 0; n--) {
auto entry = &source_map_[n];
auto entry = &source_map_entries_[n];
if (entry->code_offset <= offset) {
return entry;
}

View File

@ -71,6 +71,8 @@ class DebugInfo {
const char* machine_code_disasm() const { return machine_code_disasm_; }
void set_machine_code_disasm(char* value) { machine_code_disasm_ = value; }
size_t source_map_count() const { return source_map_count_; }
SourceMapEntry* source_map_entries() const { return source_map_entries_; }
void InitializeSourceMap(size_t source_map_count, SourceMapEntry* source_map);
SourceMapEntry* LookupSourceOffset(uint32_t offset);
SourceMapEntry* LookupHIROffset(uint32_t offset);
@ -90,7 +92,7 @@ class DebugInfo {
char* machine_code_disasm_;
size_t source_map_count_;
SourceMapEntry* source_map_;
SourceMapEntry* source_map_entries_;
};
} // namespace cpu