Index: include/llvm/MC/MCInstPrinter.h =================================================================== --- include/llvm/MC/MCInstPrinter.h +++ include/llvm/MC/MCInstPrinter.h @@ -10,6 +10,7 @@ #ifndef LLVM_MC_MCINSTPRINTER_H #define LLVM_MC_MCINSTPRINTER_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Format.h" @@ -21,6 +22,9 @@ class MCRegisterInfo; class StringRef; +/// Convert `Bytes' to a hex string and output to `OS' +void DumpBytes(ArrayRef Bytes, raw_ostream &OS); + namespace HexStyle { enum Style { C, ///< 0xff @@ -71,6 +75,11 @@ virtual void printInst(const MCInst *MI, raw_ostream &OS, StringRef Annot) = 0; + /// Print the MCInst along with disassembly context + virtual void printInst(const MCInst *MI, bool ShowRawInsn, + ArrayRef Bytes, uint64_t Address, + raw_ostream &OS); + /// getOpcodeName - Return the name of the specified opcode enum (e.g. /// "MOV32ri") or empty if we can't resolve it. StringRef getOpcodeName(unsigned Opcode) const; Index: lib/MC/MCInstPrinter.cpp =================================================================== --- lib/MC/MCInstPrinter.cpp +++ lib/MC/MCInstPrinter.cpp @@ -7,8 +7,9 @@ // //===----------------------------------------------------------------------===// +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/SmallString.h" #include "llvm/MC/MCInstPrinter.h" -#include "llvm/ADT/StringRef.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCInstrInfo.h" #include "llvm/Support/ErrorHandling.h" @@ -16,9 +17,33 @@ #include "llvm/Support/raw_ostream.h" using namespace llvm; +void llvm::DumpBytes(ArrayRef bytes, raw_ostream &OS) { + static const char hex_rep[] = "0123456789abcdef"; + SmallString<64> output; + + for (char i: bytes) { + output.push_back(hex_rep[(i & 0xF0) >> 4]); + output.push_back(hex_rep[i & 0xF]); + output.push_back(' '); + } + + OS << output.c_str(); +} + MCInstPrinter::~MCInstPrinter() { } +void MCInstPrinter::printInst(const MCInst *MI, bool ShowRawInsn, + ArrayRef Bytes, uint64_t Address, + raw_ostream &OS) { + outs() << format("%8" PRIx64 ":", Address); + if (ShowRawInsn) { + outs() << "\t"; + DumpBytes(Bytes, OS); + } + printInst(MI, outs(), ""); +} + /// getOpcodeName - Return the name of the specified opcode enum (e.g. /// "MOV32ri") or empty if we can't resolve it. StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const { Index: tools/llvm-objdump/llvm-objdump.h =================================================================== --- tools/llvm-objdump/llvm-objdump.h +++ tools/llvm-objdump/llvm-objdump.h @@ -54,7 +54,6 @@ // Various helper functions. bool error(std::error_code ec); bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b); -void DumpBytes(ArrayRef bytes); void ParseInputMachO(StringRef Filename); void printCOFFUnwindInfo(const object::COFFObjectFile* o); void printMachOUnwindInfo(const object::MachOObjectFile* o); Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -194,19 +194,6 @@ return TheTarget; } -void llvm::DumpBytes(ArrayRef bytes) { - static const char hex_rep[] = "0123456789abcdef"; - SmallString<64> output; - - for (char i: bytes) { - output.push_back(hex_rep[(i & 0xF0) >> 4]); - output.push_back(hex_rep[i & 0xF]); - output.push_back(' '); - } - - outs() << output.c_str(); -} - bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) { uint64_t a_addr, b_addr; if (error(a.getOffset(a_addr))) return false; @@ -396,12 +383,9 @@ if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), SectionAddr + Index, DebugOut, CommentStream)) { - outs() << format("%8" PRIx64 ":", SectionAddr + Index); - if (!NoShowRawInsn) { - outs() << "\t"; - DumpBytes(ArrayRef(Bytes.data() + Index, Size)); - } - IP->printInst(&Inst, outs(), ""); + IP->printInst(&Inst, !NoShowRawInsn, + ArrayRef(Bytes.data (), Size), + SectionAddr + Index, outs()); outs() << CommentStream.str(); Comments.clear(); outs() << "\n";