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"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/MemoryObject.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
32 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
35 class VectorMemoryObject : public MemoryObject {
37 const ByteArrayTy &Bytes;
39 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
41 uint64_t getBase() const override { return 0; }
42 uint64_t getExtent() const override { return Bytes.size(); }
44 int readByte(uint64_t Addr, uint8_t *Byte) const override {
45 if (Addr >= getExtent())
47 *Byte = Bytes[Addr].first;
53 static bool PrintInsts(const MCDisassembler &DisAsm,
54 const ByteArrayTy &Bytes,
55 SourceMgr &SM, raw_ostream &Out,
56 MCStreamer &Streamer, bool InAtomicBlock,
57 const MCSubtargetInfo &STI) {
58 // Wrap the vector in a MemoryObject.
59 VectorMemoryObject memoryObject(Bytes);
61 // Disassemble it to strings.
65 for (Index = 0; Index < Bytes.size(); Index += Size) {
68 MCDisassembler::DecodeStatus S;
69 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
70 /*REMOVE*/ nulls(), nulls());
72 case MCDisassembler::Fail:
73 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
74 SourceMgr::DK_Warning,
75 "invalid instruction encoding");
76 // Don't try to resynchronise the stream in a block
81 Size = 1; // skip illegible bytes
85 case MCDisassembler::SoftFail:
86 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
87 SourceMgr::DK_Warning,
88 "potentially undefined instruction encoding");
91 case MCDisassembler::Success:
92 Streamer.EmitInstruction(Inst, STI);
100 static bool SkipToToken(StringRef &Str) {
101 while (!Str.empty() && Str.find_first_not_of(" \t\r\n#,") != 0) {
102 // Strip horizontal whitespace and commas.
103 if (size_t Pos = Str.find_first_not_of(" \t\r,")) {
104 Str = Str.substr(Pos);
107 // If this is the end of a line or start of a comment, remove the rest of
109 if (Str[0] == '\n' || Str[0] == '#') {
110 // Strip to the end of line if we already processed any bytes on this
111 // line. This strips the comment and/or the \n.
112 if (Str[0] == '\n') {
115 Str = Str.substr(Str.find_first_of('\n'));
127 static bool ByteArrayFromString(ByteArrayTy &ByteArray,
130 while (SkipToToken(Str)) {
131 // Handled by higher level
132 if (Str[0] == '[' || Str[0] == ']')
135 // Get the current token.
136 size_t Next = Str.find_first_of(" \t\n\r,#[]");
137 StringRef Value = Str.substr(0, Next);
139 // Convert to a byte and add to the byte vector.
141 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
142 // If we have an error, print it and skip to the end of line.
143 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
144 "invalid input token");
145 Str = Str.substr(Str.find('\n'));
150 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
151 Str = Str.substr(Next);
157 int Disassembler::disassemble(const Target &T,
158 const std::string &Triple,
159 MCSubtargetInfo &STI,
160 MCStreamer &Streamer,
161 MemoryBuffer &Buffer,
165 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
167 errs() << "error: no register info for target " << Triple << "\n";
171 std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
173 errs() << "error: no assembly info for target " << Triple << "\n";
177 // Set up the MCContext for creating symbols and MCExpr's.
178 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
180 std::unique_ptr<const MCDisassembler> DisAsm(
181 T.createMCDisassembler(STI, Ctx));
183 errs() << "error: no disassembler for target " << Triple << "\n";
187 // Set up initial section manually here
188 Streamer.InitSections();
190 bool ErrorOccurred = false;
192 // Convert the input to a vector for disassembly.
193 ByteArrayTy ByteArray;
194 StringRef Str = Buffer.getBuffer();
195 bool InAtomicBlock = false;
197 while (SkipToToken(Str)) {
202 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
203 "nested atomic blocks make no sense");
204 ErrorOccurred = true;
206 InAtomicBlock = true;
207 Str = Str.drop_front();
209 } else if (Str[0] == ']') {
210 if (!InAtomicBlock) {
211 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
212 "attempt to close atomic block without opening");
213 ErrorOccurred = true;
215 InAtomicBlock = false;
216 Str = Str.drop_front();
220 // It's a real token, get the bytes and emit them
221 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
223 if (!ByteArray.empty())
224 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
229 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
230 "unclosed atomic block");
231 ErrorOccurred = true;
234 return ErrorOccurred;