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/MCDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/MemoryObject.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Support/raw_ostream.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 const ByteArrayTy &Bytes,
52 SourceMgr &SM, raw_ostream &Out,
53 MCStreamer &Streamer, bool InAtomicBlock,
54 const MCSubtargetInfo &STI) {
55 // Wrap the vector in a MemoryObject.
56 VectorMemoryObject memoryObject(Bytes);
58 // Disassemble it to strings.
62 for (Index = 0; Index < Bytes.size(); Index += Size) {
65 MCDisassembler::DecodeStatus S;
66 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
67 /*REMOVE*/ nulls(), nulls());
69 case MCDisassembler::Fail:
70 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
71 SourceMgr::DK_Warning,
72 "invalid instruction encoding");
73 // Don't try to resynchronise the stream in a block
78 Size = 1; // skip illegible bytes
82 case MCDisassembler::SoftFail:
83 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
84 SourceMgr::DK_Warning,
85 "potentially undefined instruction encoding");
88 case MCDisassembler::Success:
89 Streamer.EmitInstruction(Inst, STI);
97 static bool SkipToToken(StringRef &Str) {
98 while (!Str.empty() && Str.find_first_not_of(" \t\r\n#,") != 0) {
99 // Strip horizontal whitespace and commas.
100 if (size_t Pos = Str.find_first_not_of(" \t\r,")) {
101 Str = Str.substr(Pos);
104 // If this is the end of a line or start of a comment, remove the rest of
106 if (Str[0] == '\n' || Str[0] == '#') {
107 // Strip to the end of line if we already processed any bytes on this
108 // line. This strips the comment and/or the \n.
109 if (Str[0] == '\n') {
112 Str = Str.substr(Str.find_first_of('\n'));
124 static bool ByteArrayFromString(ByteArrayTy &ByteArray,
127 while (SkipToToken(Str)) {
128 // Handled by higher level
129 if (Str[0] == '[' || Str[0] == ']')
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()), SourceMgr::DK_Error,
141 "invalid input token");
142 Str = Str.substr(Str.find('\n'));
147 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
148 Str = Str.substr(Next);
154 int Disassembler::disassemble(const Target &T,
155 const std::string &Triple,
156 MCSubtargetInfo &STI,
157 MCStreamer &Streamer,
158 MemoryBuffer &Buffer,
161 std::unique_ptr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
163 errs() << "error: no disassembler for target " << Triple << "\n";
167 // Set up initial section manually here
168 Streamer.InitSections();
170 bool ErrorOccurred = false;
172 // Convert the input to a vector for disassembly.
173 ByteArrayTy ByteArray;
174 StringRef Str = Buffer.getBuffer();
175 bool InAtomicBlock = false;
177 while (SkipToToken(Str)) {
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.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;