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/OwningPtr.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/MC/MCDisassembler.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/MemoryObject.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Support/raw_ostream.h"
30 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
33 class VectorMemoryObject : public MemoryObject {
35 const ByteArrayTy &Bytes;
37 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
39 uint64_t getBase() const { return 0; }
40 uint64_t getExtent() const { return Bytes.size(); }
42 int readByte(uint64_t Addr, uint8_t *Byte) const {
43 if (Addr >= getExtent())
45 *Byte = Bytes[Addr].first;
51 static bool PrintInsts(const MCDisassembler &DisAsm,
52 const ByteArrayTy &Bytes,
53 SourceMgr &SM, raw_ostream &Out,
54 MCStreamer &Streamer, bool InAtomicBlock,
55 const MCSubtargetInfo &STI) {
56 // Wrap the vector in a MemoryObject.
57 VectorMemoryObject memoryObject(Bytes);
59 // Disassemble it to strings.
63 for (Index = 0; Index < Bytes.size(); Index += Size) {
66 MCDisassembler::DecodeStatus S;
67 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
68 /*REMOVE*/ nulls(), nulls());
70 case MCDisassembler::Fail:
71 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
72 SourceMgr::DK_Warning,
73 "invalid instruction encoding");
74 // Don't try to resynchronise the stream in a block
79 Size = 1; // skip illegible bytes
83 case MCDisassembler::SoftFail:
84 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
85 SourceMgr::DK_Warning,
86 "potentially undefined instruction encoding");
89 case MCDisassembler::Success:
90 Streamer.EmitInstruction(Inst, STI);
98 static bool SkipToToken(StringRef &Str) {
99 while (!Str.empty() && Str.find_first_not_of(" \t\r\n#,") != 0) {
100 // Strip horizontal whitespace and commas.
101 if (size_t Pos = Str.find_first_not_of(" \t\r,")) {
102 Str = Str.substr(Pos);
105 // If this is the end of a line or start of a comment, remove the rest of
107 if (Str[0] == '\n' || Str[0] == '#') {
108 // Strip to the end of line if we already processed any bytes on this
109 // line. This strips the comment and/or the \n.
110 if (Str[0] == '\n') {
113 Str = Str.substr(Str.find_first_of('\n'));
125 static bool ByteArrayFromString(ByteArrayTy &ByteArray,
128 while (SkipToToken(Str)) {
129 // Handled by higher level
130 if (Str[0] == '[' || Str[0] == ']')
133 // Get the current token.
134 size_t Next = Str.find_first_of(" \t\n\r,#[]");
135 StringRef Value = Str.substr(0, Next);
137 // Convert to a byte and add to the byte vector.
139 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
140 // If we have an error, print it and skip to the end of line.
141 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
142 "invalid input token");
143 Str = Str.substr(Str.find('\n'));
148 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
149 Str = Str.substr(Next);
155 int Disassembler::disassemble(const Target &T,
156 const std::string &Triple,
157 MCSubtargetInfo &STI,
158 MCStreamer &Streamer,
159 MemoryBuffer &Buffer,
162 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
164 errs() << "error: no disassembler for target " << Triple << "\n";
168 // Set up initial section manually here
169 Streamer.InitSections();
171 bool ErrorOccurred = false;
173 // Convert the input to a vector for disassembly.
174 ByteArrayTy ByteArray;
175 StringRef Str = Buffer.getBuffer();
176 bool InAtomicBlock = false;
178 while (SkipToToken(Str)) {
183 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
184 "nested atomic blocks make no sense");
185 ErrorOccurred = true;
187 InAtomicBlock = true;
188 Str = Str.drop_front();
190 } else if (Str[0] == ']') {
191 if (!InAtomicBlock) {
192 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
193 "attempt to close atomic block without opening");
194 ErrorOccurred = true;
196 InAtomicBlock = false;
197 Str = Str.drop_front();
201 // It's a real token, get the bytes and emit them
202 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
204 if (!ByteArray.empty())
205 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
210 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
211 "unclosed atomic block");
212 ErrorOccurred = true;
215 return ErrorOccurred;