1 //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the disassembler of strings of bytes written in
11 // hexadecimal, from standard input or from a file.
13 //===----------------------------------------------------------------------===//
15 #include "Disassembler.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstPrinter.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/MemoryObject.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/SourceMgr.h"
29 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
32 class VectorMemoryObject : public MemoryObject {
34 const ByteArrayTy &Bytes;
36 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
38 uint64_t getBase() const { return 0; }
39 uint64_t getExtent() const { return Bytes.size(); }
41 int readByte(uint64_t Addr, uint8_t *Byte) const {
42 if (Addr > getExtent())
44 *Byte = Bytes[Addr].first;
50 static bool PrintInst(const llvm::MCDisassembler &DisAsm,
51 llvm::MCInstPrinter &Printer, const ByteArrayTy &Bytes,
53 // Wrap the vector in a MemoryObject.
54 VectorMemoryObject memoryObject(Bytes);
56 // Disassemble it to a string and get the size of the instruction.
60 if (!DisAsm.getInstruction(Inst, Size, memoryObject, 0,
61 /*REMOVE*/ nulls())) {
62 SM.PrintMessage(SMLoc::getFromPointer(Bytes[0].second),
63 "invalid instruction encoding", "error");
67 Printer.printInst(&Inst);
70 // If the disassembled instruction was smaller than the number of bytes we
71 // read, reject the excess bytes.
72 if (Bytes.size() != Size) {
73 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Size].second),
74 "excess data detected in input", "error");
81 int Disassembler::disassemble(const Target &T, const std::string &Triple,
82 MemoryBuffer &Buffer) {
83 // Set up disassembler.
84 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
87 errs() << "error: no assembly info for target " << Triple << "\n";
91 llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
93 errs() << "error: no disassembler for target " << Triple << "\n";
97 llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
100 errs() << "error: no instruction printer for target " << Triple << '\n';
104 bool ErrorOccurred = false;
107 SM.AddNewSourceBuffer(&Buffer, SMLoc());
109 // Convert the input to a vector for disassembly.
110 ByteArrayTy ByteArray;
112 StringRef Str = Buffer.getBuffer();
113 while (!Str.empty()) {
114 // Strip horizontal whitespace.
115 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
116 Str = Str.substr(Pos);
120 // If this is the end of a line or start of a comment, process the
121 // instruction we have so far.
122 if (Str[0] == '\n' || Str[0] == '#') {
123 // If we have bytes to process, do so.
124 if (!ByteArray.empty()) {
125 ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
129 // Strip to the end of line if we already processed any bytes on this
130 // line. This strips the comment and/or the \n.
134 Str = Str.substr(Str.find_first_of('\n'));
141 // Get the current token.
142 size_t Next = Str.find_first_of(" \t\n\r#");
143 StringRef Value = Str.substr(0, Next);
145 // Convert to a byte and add to the byte vector.
147 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
148 // If we have an error, print it and skip to the end of line.
149 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
150 "invalid input token", "error");
151 ErrorOccurred = true;
152 Str = Str.substr(Str.find('\n'));
157 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
158 Str = Str.substr(Next);
161 if (!ByteArray.empty())
162 ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
164 return ErrorOccurred;