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/StringRefMemoryObject.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
32 typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
35 static bool PrintInsts(const MCDisassembler &DisAsm,
36 const ByteArrayTy &Bytes,
37 SourceMgr &SM, raw_ostream &Out,
38 MCStreamer &Streamer, bool InAtomicBlock,
39 const MCSubtargetInfo &STI) {
40 // Wrap the vector in a MemoryObject.
41 StringRef Data((const char*)Bytes.first.data(), Bytes.first.size());
42 StringRefMemoryObject memoryObject(Data);
44 // Disassemble it to strings.
48 for (Index = 0; Index < Bytes.first.size(); Index += Size) {
51 MCDisassembler::DecodeStatus S;
52 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
53 /*REMOVE*/ nulls(), nulls());
55 case MCDisassembler::Fail:
56 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
57 SourceMgr::DK_Warning,
58 "invalid instruction encoding");
59 // Don't try to resynchronise the stream in a block
64 Size = 1; // skip illegible bytes
68 case MCDisassembler::SoftFail:
69 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
70 SourceMgr::DK_Warning,
71 "potentially undefined instruction encoding");
74 case MCDisassembler::Success:
75 Streamer.EmitInstruction(Inst, STI);
83 static bool SkipToToken(StringRef &Str) {
88 // Strip horizontal whitespace and commas.
89 if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
90 Str = Str.substr(Pos);
94 // If this is the start of a comment, remove the rest of the line.
96 Str = Str.substr(Str.find_first_of('\n'));
104 static bool ByteArrayFromString(ByteArrayTy &ByteArray,
107 while (SkipToToken(Str)) {
108 // Handled by higher level
109 if (Str[0] == '[' || Str[0] == ']')
112 // Get the current token.
113 size_t Next = Str.find_first_of(" \t\n\r,#[]");
114 StringRef Value = Str.substr(0, Next);
116 // Convert to a byte and add to the byte vector.
118 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
119 // If we have an error, print it and skip to the end of line.
120 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
121 "invalid input token");
122 Str = Str.substr(Str.find('\n'));
123 ByteArray.first.clear();
124 ByteArray.second.clear();
128 ByteArray.first.push_back(ByteVal);
129 ByteArray.second.push_back(Value.data());
130 Str = Str.substr(Next);
136 int Disassembler::disassemble(const Target &T,
137 const std::string &Triple,
138 MCSubtargetInfo &STI,
139 MCStreamer &Streamer,
140 MemoryBuffer &Buffer,
144 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
146 errs() << "error: no register info for target " << Triple << "\n";
150 std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
152 errs() << "error: no assembly info for target " << Triple << "\n";
156 // Set up the MCContext for creating symbols and MCExpr's.
157 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
159 std::unique_ptr<const MCDisassembler> DisAsm(
160 T.createMCDisassembler(STI, Ctx));
162 errs() << "error: no disassembler for target " << Triple << "\n";
166 // Set up initial section manually here
167 Streamer.InitSections(false);
169 bool ErrorOccurred = false;
171 // Convert the input to a vector for disassembly.
172 ByteArrayTy ByteArray;
173 StringRef Str = Buffer.getBuffer();
174 bool InAtomicBlock = false;
176 while (SkipToToken(Str)) {
177 ByteArray.first.clear();
178 ByteArray.second.clear();
182 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
183 "nested atomic blocks make no sense");
184 ErrorOccurred = true;
186 InAtomicBlock = true;
187 Str = Str.drop_front();
189 } else if (Str[0] == ']') {
190 if (!InAtomicBlock) {
191 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
192 "attempt to close atomic block without opening");
193 ErrorOccurred = true;
195 InAtomicBlock = false;
196 Str = Str.drop_front();
200 // It's a real token, get the bytes and emit them
201 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
203 if (!ByteArray.first.empty())
204 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
209 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
210 "unclosed atomic block");
211 ErrorOccurred = true;
214 return ErrorOccurred;