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/SourceMgr.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
31 typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
34 static bool PrintInsts(const MCDisassembler &DisAsm,
35 const ByteArrayTy &Bytes,
36 SourceMgr &SM, raw_ostream &Out,
37 MCStreamer &Streamer, bool InAtomicBlock,
38 const MCSubtargetInfo &STI) {
39 ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
41 // Disassemble it to strings.
45 for (Index = 0; Index < Bytes.first.size(); Index += Size) {
48 MCDisassembler::DecodeStatus S;
49 S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index,
50 /*REMOVE*/ nulls(), nulls());
52 case MCDisassembler::Fail:
53 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
54 SourceMgr::DK_Warning,
55 "invalid instruction encoding");
56 // Don't try to resynchronise the stream in a block
61 Size = 1; // skip illegible bytes
65 case MCDisassembler::SoftFail:
66 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
67 SourceMgr::DK_Warning,
68 "potentially undefined instruction encoding");
71 case MCDisassembler::Success:
72 Streamer.EmitInstruction(Inst, STI);
80 static bool SkipToToken(StringRef &Str) {
85 // Strip horizontal whitespace and commas.
86 if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
87 Str = Str.substr(Pos);
91 // If this is the start of a comment, remove the rest of the line.
93 Str = Str.substr(Str.find_first_of('\n'));
101 static bool ByteArrayFromString(ByteArrayTy &ByteArray,
104 while (SkipToToken(Str)) {
105 // Handled by higher level
106 if (Str[0] == '[' || Str[0] == ']')
109 // Get the current token.
110 size_t Next = Str.find_first_of(" \t\n\r,#[]");
111 StringRef Value = Str.substr(0, Next);
113 // Convert to a byte and add to the byte vector.
115 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
116 // If we have an error, print it and skip to the end of line.
117 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
118 "invalid input token");
119 Str = Str.substr(Str.find('\n'));
120 ByteArray.first.clear();
121 ByteArray.second.clear();
125 ByteArray.first.push_back(ByteVal);
126 ByteArray.second.push_back(Value.data());
127 Str = Str.substr(Next);
133 int Disassembler::disassemble(const Target &T,
134 const std::string &Triple,
135 MCSubtargetInfo &STI,
136 MCStreamer &Streamer,
137 MemoryBuffer &Buffer,
141 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
143 errs() << "error: no register info for target " << Triple << "\n";
147 std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
149 errs() << "error: no assembly info for target " << Triple << "\n";
153 // Set up the MCContext for creating symbols and MCExpr's.
154 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
156 std::unique_ptr<const MCDisassembler> DisAsm(
157 T.createMCDisassembler(STI, Ctx));
159 errs() << "error: no disassembler for target " << Triple << "\n";
163 // Set up initial section manually here
164 Streamer.InitSections(false);
166 bool ErrorOccurred = false;
168 // Convert the input to a vector for disassembly.
169 ByteArrayTy ByteArray;
170 StringRef Str = Buffer.getBuffer();
171 bool InAtomicBlock = false;
173 while (SkipToToken(Str)) {
174 ByteArray.first.clear();
175 ByteArray.second.clear();
179 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
180 "nested atomic blocks make no sense");
181 ErrorOccurred = true;
183 InAtomicBlock = true;
184 Str = Str.drop_front();
186 } else if (Str[0] == ']') {
187 if (!InAtomicBlock) {
188 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
189 "attempt to close atomic block without opening");
190 ErrorOccurred = true;
192 InAtomicBlock = false;
193 Str = Str.drop_front();
197 // It's a real token, get the bytes and emit them
198 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
200 if (!ByteArray.first.empty())
201 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
206 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
207 "unclosed atomic block");
208 ErrorOccurred = true;
211 return ErrorOccurred;