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
diff --git a/tools/llvm-mc/Disassembler.cpp b/tools/llvm-mc/Disassembler.cpp
index a8cd7c1..5f2fdb8 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<std::pair<unsigned char, const char*> > ByteArrayTy;
@@ -56,8 +53,9 @@
}
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 @@
// Fall through
case MCDisassembler::Success:
- Printer.printInst(&Inst, Out, "");
- Out << "\n";
+ Streamer.EmitInstruction(Inst);
break;
}
}
@@ -145,56 +142,22 @@
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<const MCAsmInfo> AsmInfo(T.createMCAsmInfo(Triple));
-
- if (!AsmInfo) {
- errs() << "error: no assembly info for target " << Triple << "\n";
- return -1;
- }
-
- OwningPtr<const MCSubtargetInfo> STI(T.createMCSubtargetInfo(Triple, Cpu,
- FeaturesStr));
- if (!STI) {
- errs() << "error: no subtarget info for target " << Triple << "\n";
- return -1;
- }
-
- OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(*STI));
+ OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
if (!DisAsm) {
errs() << "error: no disassembler for target " << Triple << "\n";
return -1;
}
- OwningPtr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
- if (!MRI) {
- errs() << "error: no register info for target " << Triple << "\n";
- return -1;
- }
-
- OwningPtr<const MCInstrInfo> MII(T.createMCInstrInfo());
- if (!MII) {
- errs() << "error: no instruction info for target " << Triple << "\n";
- return -1;
- }
-
- int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
- OwningPtr<MCInstPrinter> 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 @@
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 @@
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;