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 PrintInsts(const MCDisassembler &DisAsm,
51 MCInstPrinter &Printer, const ByteArrayTy &Bytes,
53 // Wrap the vector in a MemoryObject.
54 VectorMemoryObject memoryObject(Bytes);
56 // Disassemble it to strings.
60 for (Index = 0; Index < Bytes.size(); Index += Size) {
63 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
64 /*REMOVE*/ nulls())) {
65 Printer.printInst(&Inst);
69 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
70 "invalid instruction encoding", "warning");
72 Size = 1; // skip illegible bytes
79 int Disassembler::disassemble(const Target &T, const std::string &Triple,
80 MemoryBuffer &Buffer) {
81 // Set up disassembler.
82 OwningPtr<const MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
85 errs() << "error: no assembly info for target " << Triple << "\n";
89 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler());
91 errs() << "error: no disassembler for target " << Triple << "\n";
95 OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(0, *AsmInfo, outs()));
97 errs() << "error: no instruction printer for target " << Triple << '\n';
101 bool ErrorOccurred = false;
104 SM.AddNewSourceBuffer(&Buffer, SMLoc());
106 // Convert the input to a vector for disassembly.
107 ByteArrayTy ByteArray;
109 StringRef Str = Buffer.getBuffer();
110 while (!Str.empty()) {
111 // Strip horizontal whitespace.
112 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
113 Str = Str.substr(Pos);
117 // If this is the end of a line or start of a comment, remove the rest of
119 if (Str[0] == '\n' || Str[0] == '#') {
120 // Strip to the end of line if we already processed any bytes on this
121 // line. This strips the comment and/or the \n.
125 Str = Str.substr(Str.find_first_of('\n'));
132 // Get the current token.
133 size_t Next = Str.find_first_of(" \t\n\r#");
134 StringRef Value = Str.substr(0, Next);
136 // Convert to a byte and add to the byte vector.
138 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
139 // If we have an error, print it and skip to the end of line.
140 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
141 "invalid input token", "error");
142 ErrorOccurred = true;
143 Str = Str.substr(Str.find('\n'));
148 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
149 Str = Str.substr(Next);
152 if (!ByteArray.empty())
153 ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM);
155 return ErrorOccurred;