diff options
author | Craig Topper <craig.topper@sifive.com> | 2024-04-26 11:27:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-26 11:27:28 -0700 |
commit | b27f86b40b20942c0e809128214b43d6edde365a (patch) | |
tree | f98412620b0b02898f7b4df61ddd7255630932bb /llvm/tools/llvm-objdump/llvm-objdump.cpp | |
parent | 760910ddb918d77e7632be1678f69909384d69ae (diff) | |
download | llvm-b27f86b40b20942c0e809128214b43d6edde365a.zip llvm-b27f86b40b20942c0e809128214b43d6edde365a.tar.gz llvm-b27f86b40b20942c0e809128214b43d6edde365a.tar.bz2 |
[RISCV] Add an instruction PrettyPrinter to llvm-objdump (#90093)
This prints the opcode bytes in the same order as GNU objdump without a
space between them.
Diffstat (limited to 'llvm/tools/llvm-objdump/llvm-objdump.cpp')
-rw-r--r-- | llvm/tools/llvm-objdump/llvm-objdump.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index 339822e..675364a 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -947,6 +947,55 @@ public: }; AArch64PrettyPrinter AArch64PrettyPrinterInst; +class RISCVPrettyPrinter : public PrettyPrinter { +public: + void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, + object::SectionedAddress Address, formatted_raw_ostream &OS, + StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP, + StringRef ObjectFilename, std::vector<RelocationRef> *Rels, + LiveVariablePrinter &LVP) override { + if (SP && (PrintSource || PrintLines)) + SP->printSourceLine(OS, Address, ObjectFilename, LVP); + LVP.printBetweenInsts(OS, false); + + size_t Start = OS.tell(); + if (LeadingAddr) + OS << format("%8" PRIx64 ":", Address.Address); + if (ShowRawInsn) { + size_t Pos = 0, End = Bytes.size(); + if (End % 4 == 0) { + // 32-bit and 64-bit instructions. + for (; Pos + 4 <= End; Pos += 4) + OS << ' ' + << format_hex_no_prefix( + llvm::support::endian::read<uint32_t>( + Bytes.data() + Pos, llvm::endianness::little), + 8); + } else if (End % 2 == 0) { + // 16-bit and 48-bits instructions. + for (; Pos + 2 <= End; Pos += 2) + OS << ' ' + << format_hex_no_prefix( + llvm::support::endian::read<uint16_t>( + Bytes.data() + Pos, llvm::endianness::little), + 4); + } + if (Pos < End) { + OS << ' '; + dumpBytes(Bytes.slice(Pos), OS); + } + } + + AlignToInstStartColumn(Start, STI, OS); + + if (MI) { + IP.printInst(MI, Address.Address, "", STI, OS); + } else + OS << "\t<unknown>"; + } +}; +RISCVPrettyPrinter RISCVPrettyPrinterInst; + PrettyPrinter &selectPrettyPrinter(Triple const &Triple) { switch(Triple.getArch()) { default: @@ -967,6 +1016,9 @@ PrettyPrinter &selectPrettyPrinter(Triple const &Triple) { case Triple::aarch64_be: case Triple::aarch64_32: return AArch64PrettyPrinterInst; + case Triple::riscv32: + case Triple::riscv64: + return RISCVPrettyPrinterInst; } } |