Remove support for not using .loc directives.
[oota-llvm.git] / tools / llvm-mc / Disassembler.cpp
1 //===- Disassembler.cpp - Disassembler for hex strings --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class implements the disassembler of strings of bytes written in
11 // hexadecimal, from standard input or from a file.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
27
28 using namespace llvm;
29
30 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
31
32 namespace {
33 class VectorMemoryObject : public MemoryObject {
34 private:
35   const ByteArrayTy &Bytes;
36 public:
37   VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
38
39   uint64_t getBase() const { return 0; }
40   uint64_t getExtent() const { return Bytes.size(); }
41
42   int readByte(uint64_t Addr, uint8_t *Byte) const {
43     if (Addr >= getExtent())
44       return -1;
45     *Byte = Bytes[Addr].first;
46     return 0;
47   }
48 };
49 }
50
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);
58
59   // Disassemble it to strings.
60   uint64_t Size;
61   uint64_t Index;
62
63   for (Index = 0; Index < Bytes.size(); Index += Size) {
64     MCInst Inst;
65
66     MCDisassembler::DecodeStatus S;
67     S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
68                               /*REMOVE*/ nulls(), nulls());
69     switch (S) {
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
75       if (InAtomicBlock)
76         return true;
77
78       if (Size == 0)
79         Size = 1; // skip illegible bytes
80
81       break;
82
83     case MCDisassembler::SoftFail:
84       SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
85                       SourceMgr::DK_Warning,
86                       "potentially undefined instruction encoding");
87       // Fall through
88
89     case MCDisassembler::Success:
90       Streamer.EmitInstruction(Inst, STI);
91       break;
92     }
93   }
94
95   return false;
96 }
97
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);
103     }
104
105     // If this is the end of a line or start of a comment, remove the rest of
106     // the line.
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') {
111         Str = Str.substr(1);
112       } else {
113         Str = Str.substr(Str.find_first_of('\n'));
114         if (!Str.empty())
115           Str = Str.substr(1);
116       }
117       continue;
118     }
119   }
120
121   return !Str.empty();
122 }
123
124
125 static bool ByteArrayFromString(ByteArrayTy &ByteArray,
126                                 StringRef &Str,
127                                 SourceMgr &SM) {
128   while (SkipToToken(Str)) {
129     // Handled by higher level
130     if (Str[0] == '[' || Str[0] == ']')
131       return false;
132
133     // Get the current token.
134     size_t Next = Str.find_first_of(" \t\n\r,#[]");
135     StringRef Value = Str.substr(0, Next);
136
137     // Convert to a byte and add to the byte vector.
138     unsigned ByteVal;
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'));
144       ByteArray.clear();
145       continue;
146     }
147
148     ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
149     Str = Str.substr(Next);
150   }
151
152   return false;
153 }
154
155 int Disassembler::disassemble(const Target &T,
156                               const std::string &Triple,
157                               MCSubtargetInfo &STI,
158                               MCStreamer &Streamer,
159                               MemoryBuffer &Buffer,
160                               SourceMgr &SM,
161                               raw_ostream &Out) {
162   OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
163   if (!DisAsm) {
164     errs() << "error: no disassembler for target " << Triple << "\n";
165     return -1;
166   }
167
168   // Set up initial section manually here
169   Streamer.InitSections();
170
171   bool ErrorOccurred = false;
172
173   // Convert the input to a vector for disassembly.
174   ByteArrayTy ByteArray;
175   StringRef Str = Buffer.getBuffer();
176   bool InAtomicBlock = false;
177
178   while (SkipToToken(Str)) {
179     ByteArray.clear();
180
181     if (Str[0] == '[') {
182       if (InAtomicBlock) {
183         SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
184                         "nested atomic blocks make no sense");
185         ErrorOccurred = true;
186       }
187       InAtomicBlock = true;
188       Str = Str.drop_front();
189       continue;
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;
195       }
196       InAtomicBlock = false;
197       Str = Str.drop_front();
198       continue;
199     }
200
201     // It's a real token, get the bytes and emit them
202     ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
203
204     if (!ByteArray.empty())
205       ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
206                                   InAtomicBlock, STI);
207   }
208
209   if (InAtomicBlock) {
210     SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
211                     "unclosed atomic block");
212     ErrorOccurred = true;
213   }
214
215   return ErrorOccurred;
216 }