From d0c478d95f440b4db76279fe47d6cf734a28fa9a Mon Sep 17 00:00:00 2001 From: Richard Barton Date: Mon, 16 Apr 2012 11:32:10 +0000 Subject: Add -disassemble support for -show-inst and -show-encode capability llvm-mc. Also refactor so all MC paraphernalia are created once for all uses as much as possible. The test change is to account for the fact that the default disassembler behaviour has changed with regards to specifying the assembly syntax to use. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154809 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/llvm-mc/Disassembler.cpp | 67 +++++++++--------------------------------- 1 file changed, 14 insertions(+), 53 deletions(-) (limited to 'tools/llvm-mc/Disassembler.cpp') diff --git a/tools/llvm-mc/Disassembler.cpp b/tools/llvm-mc/Disassembler.cpp index a8cd7c1c89..5f2fdb8071 100644 --- a/tools/llvm-mc/Disassembler.cpp +++ b/tools/llvm-mc/Disassembler.cpp @@ -17,21 +17,18 @@ #include "../../lib/MC/MCDisassembler/EDInst.h" #include "../../lib/MC/MCDisassembler/EDOperand.h" #include "../../lib/MC/MCDisassembler/EDToken.h" -#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCDisassembler.h" #include "llvm/MC/MCInst.h" -#include "llvm/MC/MCInstPrinter.h" -#include "llvm/MC/MCInstrInfo.h" -#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSubtargetInfo.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/Triple.h" -#include "llvm/ADT/Twine.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryObject.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/raw_ostream.h" + using namespace llvm; typedef std::vector > ByteArrayTy; @@ -56,8 +53,9 @@ public: } static bool PrintInsts(const MCDisassembler &DisAsm, - MCInstPrinter &Printer, const ByteArrayTy &Bytes, - SourceMgr &SM, raw_ostream &Out) { + const ByteArrayTy &Bytes, + SourceMgr &SM, raw_ostream &Out, + MCStreamer &Streamer) { // Wrap the vector in a MemoryObject. VectorMemoryObject memoryObject(Bytes); @@ -87,8 +85,7 @@ static bool PrintInsts(const MCDisassembler &DisAsm, // Fall through case MCDisassembler::Success: - Printer.printInst(&Inst, Out, ""); - Out << "\n"; + Streamer.EmitInstruction(Inst); break; } } @@ -145,56 +142,22 @@ static bool ByteArrayFromString(ByteArrayTy &ByteArray, int Disassembler::disassemble(const Target &T, const std::string &Triple, - const std::string &Cpu, - const std::string &FeaturesStr, + MCSubtargetInfo &STI, + MCStreamer &Streamer, MemoryBuffer &Buffer, + SourceMgr &SM, raw_ostream &Out) { - // Set up disassembler. - OwningPtr AsmInfo(T.createMCAsmInfo(Triple)); - - if (!AsmInfo) { - errs() << "error: no assembly info for target " << Triple << "\n"; - return -1; - } - - OwningPtr STI(T.createMCSubtargetInfo(Triple, Cpu, - FeaturesStr)); - if (!STI) { - errs() << "error: no subtarget info for target " << Triple << "\n"; - return -1; - } - - OwningPtr DisAsm(T.createMCDisassembler(*STI)); + OwningPtr DisAsm(T.createMCDisassembler(STI)); if (!DisAsm) { errs() << "error: no disassembler for target " << Triple << "\n"; return -1; } - OwningPtr MRI(T.createMCRegInfo(Triple)); - if (!MRI) { - errs() << "error: no register info for target " << Triple << "\n"; - return -1; - } - - OwningPtr MII(T.createMCInstrInfo()); - if (!MII) { - errs() << "error: no instruction info for target " << Triple << "\n"; - return -1; - } - - int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); - OwningPtr IP(T.createMCInstPrinter(AsmPrinterVariant, *AsmInfo, - *MII, *MRI, *STI)); - if (!IP) { - errs() << "error: no instruction printer for target " << Triple << '\n'; - return -1; - } + // Set up initial section manually here + Streamer.InitSections(); bool ErrorOccurred = false; - SourceMgr SM; - SM.AddNewSourceBuffer(&Buffer, SMLoc()); - // Convert the input to a vector for disassembly. ByteArrayTy ByteArray; StringRef Str = Buffer.getBuffer(); @@ -202,7 +165,7 @@ int Disassembler::disassemble(const Target &T, ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM); if (!ByteArray.empty()) - ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM, Out); + ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer); return ErrorOccurred; } @@ -236,12 +199,10 @@ static int verboseEvaluator(uint64_t *V, unsigned R, void *Arg) { int Disassembler::disassembleEnhanced(const std::string &TS, MemoryBuffer &Buffer, + SourceMgr &SM, raw_ostream &Out) { ByteArrayTy ByteArray; StringRef Str = Buffer.getBuffer(); - SourceMgr SM; - - SM.AddNewSourceBuffer(&Buffer, SMLoc()); if (ByteArrayFromString(ByteArray, Str, SM)) { return -1; -- cgit v1.2.3-18-g5258