From 6da72bf1ae1a2c25c91bde23a190c4acab888904 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 2 Feb 2022 20:27:44 +0000 Subject: wasm-objdump: Print local names during disassembly (#1818) Fixes #1815 --- src/binary-reader-objdump.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/binary-reader-objdump.cc') diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc index 05e2c15e..bf7028da 100644 --- a/src/binary-reader-objdump.cc +++ b/src/binary-reader-objdump.cc @@ -54,6 +54,7 @@ class BinaryReaderObjdumpBase : public BinaryReaderNop { protected: string_view GetFunctionName(Index index) const; string_view GetGlobalName(Index index) const; + string_view GetLocalName(Index function_index, Index local_index) const; string_view GetSectionName(Index index) const; string_view GetTagName(Index index) const; string_view GetSymbolName(Index index) const; @@ -145,6 +146,11 @@ string_view BinaryReaderObjdumpBase::GetGlobalName(Index index) const { return objdump_state_->global_names.Get(index); } +string_view BinaryReaderObjdumpBase::GetLocalName(Index function_index, + Index local_index) const { + return objdump_state_->local_names.Get(function_index, local_index); +} + string_view BinaryReaderObjdumpBase::GetSectionName(Index index) const { return objdump_state_->section_names.Get(index); } @@ -278,6 +284,13 @@ class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase { return Result::Ok; } + Result OnLocalName(Index function_index, + Index local_index, + string_view local_name) override { + SetLocalName(function_index, local_index, local_name); + return Result::Ok; + } + Result OnSymbolCount(Index count) override { objdump_state_->symtab.resize(count); return Result::Ok; @@ -428,6 +441,7 @@ class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase { protected: void SetFunctionName(Index index, string_view name); void SetGlobalName(Index index, string_view name); + void SetLocalName(Index function_index, Index local_index, string_view name); void SetTagName(Index index, string_view name); void SetTableName(Index index, string_view name); void SetSegmentName(Index index, string_view name); @@ -442,6 +456,12 @@ void BinaryReaderObjdumpPrepass::SetGlobalName(Index index, string_view name) { objdump_state_->global_names.Set(index, name); } +void BinaryReaderObjdumpPrepass::SetLocalName(Index function_index, + Index local_index, + string_view name) { + objdump_state_->local_names.Set(function_index, local_index, name); +} + void BinaryReaderObjdumpPrepass::SetTagName(Index index, string_view name) { objdump_state_->tag_names.Set(index, name); } @@ -509,6 +529,7 @@ class BinaryReaderObjdumpDisassemble : public BinaryReaderObjdumpBase { Offset last_opcode_end = 0; int indent_level = 0; Index next_reloc = 0; + Index current_function_index = 0; Index local_index_ = 0; bool in_function_body = false; }; @@ -694,6 +715,11 @@ Result BinaryReaderObjdumpDisassemble::OnOpcodeIndex(Index value) { !(name = GetGlobalName(value)).empty()) { LogOpcode("%d <" PRIstringview ">", value, WABT_PRINTF_STRING_VIEW_ARG(name)); + } else if ((current_opcode == Opcode::LocalGet || + current_opcode == Opcode::LocalSet) && + !(name = GetLocalName(current_function_index, value)).empty()) { + LogOpcode("%d <" PRIstringview ">", value, + WABT_PRINTF_STRING_VIEW_ARG(name)); } else { LogOpcode("%d", value); } @@ -853,6 +879,7 @@ Result BinaryReaderObjdumpDisassemble::BeginFunctionBody(Index index, last_opcode_end = 0; in_function_body = true; + current_function_index = index; local_index_ = objdump_state_->function_param_counts[index]; return Result::Ok; } @@ -2170,6 +2197,21 @@ void ObjdumpNames::Set(Index index, string_view name) { names[index] = name.to_string(); } +string_view ObjdumpLocalNames::Get(Index function_index, + Index local_index) const { + auto iter = names.find(std::pair(function_index, local_index)); + if (iter == names.end()) + return string_view(); + return iter->second; +} + +void ObjdumpLocalNames::Set(Index function_index, + Index local_index, + string_view name) { + names[std::pair(function_index, local_index)] = + name.to_string(); +} + Result ReadBinaryObjdump(const uint8_t* data, size_t size, ObjdumpOptions* options, -- cgit v1.2.3