blob: 81a0045897a20dedd38a4fea338d0bcfa6d2aca7 [file] [log] [blame]
Chris Lattnerb257d242009-12-22 22:50:29 +00001//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
Sean Callanan7e645502009-12-17 01:49:59 +00002//
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
Chris Lattnerb257d242009-12-22 22:50:29 +000015#include "Disassembler.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
Sean Callanan7e645502009-12-17 01:49:59 +000018#include "llvm/MC/MCDisassembler.h"
19#include "llvm/MC/MCInst.h"
Richard Bartondef81b92012-04-16 11:32:10 +000020#include "llvm/MC/MCStreamer.h"
James Molloy4c493e82011-09-07 17:24:38 +000021#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanan7e645502009-12-17 01:49:59 +000022#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/MemoryObject.h"
Chris Lattner88799182009-12-22 06:45:48 +000024#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Support/raw_ostream.h"
Richard Bartondef81b92012-04-16 11:32:10 +000027
Sean Callanan7e645502009-12-17 01:49:59 +000028using namespace llvm;
29
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000030typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
31
32namespace {
Sean Callanan7e645502009-12-17 01:49:59 +000033class VectorMemoryObject : public MemoryObject {
34private:
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000035 const ByteArrayTy &Bytes;
Sean Callanan7e645502009-12-17 01:49:59 +000036public:
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000037 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Jim Grosbach112a2de2011-05-09 20:05:25 +000038
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000039 uint64_t getBase() const { return 0; }
Derek Schuff56b662c2012-02-29 01:09:06 +000040 uint64_t getExtent() const { return Bytes.size(); }
Sean Callanan7e645502009-12-17 01:49:59 +000041
Derek Schuff56b662c2012-02-29 01:09:06 +000042 int readByte(uint64_t Addr, uint8_t *Byte) const {
Rafael Espindola9f9a1062011-01-06 16:48:42 +000043 if (Addr >= getExtent())
Sean Callanan7e645502009-12-17 01:49:59 +000044 return -1;
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000045 *Byte = Bytes[Addr].first;
Sean Callanan7e645502009-12-17 01:49:59 +000046 return 0;
47 }
48};
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000049}
Sean Callanan7e645502009-12-17 01:49:59 +000050
Daniel Dunbard9d5b312010-03-20 22:36:35 +000051static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartondef81b92012-04-16 11:32:10 +000052 const ByteArrayTy &Bytes,
53 SourceMgr &SM, raw_ostream &Out,
Tim Northover48cf6cc2013-07-19 10:05:04 +000054 MCStreamer &Streamer, bool InAtomicBlock) {
Sean Callanan7e645502009-12-17 01:49:59 +000055 // Wrap the vector in a MemoryObject.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000056 VectorMemoryObject memoryObject(Bytes);
Jim Grosbach112a2de2011-05-09 20:05:25 +000057
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000058 // Disassemble it to strings.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000059 uint64_t Size;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000060 uint64_t Index;
Jim Grosbach112a2de2011-05-09 20:05:25 +000061
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000062 for (Index = 0; Index < Bytes.size(); Index += Size) {
63 MCInst Inst;
Jim Grosbach112a2de2011-05-09 20:05:25 +000064
Owen Andersona4043c42011-08-17 17:44:15 +000065 MCDisassembler::DecodeStatus S;
66 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
Owen Andersona0c3b972011-09-15 23:38:46 +000067 /*REMOVE*/ nulls(), nulls());
Owen Andersona4043c42011-08-17 17:44:15 +000068 switch (S) {
69 case MCDisassembler::Fail:
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000070 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner03b80a42011-10-16 05:43:57 +000071 SourceMgr::DK_Warning,
72 "invalid instruction encoding");
Tim Northover48cf6cc2013-07-19 10:05:04 +000073 // Don't try to resynchronise the stream in a block
74 if (InAtomicBlock)
75 return true;
76
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000077 if (Size == 0)
78 Size = 1; // skip illegible bytes
Tim Northover48cf6cc2013-07-19 10:05:04 +000079
Owen Andersona4043c42011-08-17 17:44:15 +000080 break;
81
82 case MCDisassembler::SoftFail:
83 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner03b80a42011-10-16 05:43:57 +000084 SourceMgr::DK_Warning,
85 "potentially undefined instruction encoding");
Owen Andersona4043c42011-08-17 17:44:15 +000086 // Fall through
87
88 case MCDisassembler::Success:
Richard Bartondef81b92012-04-16 11:32:10 +000089 Streamer.EmitInstruction(Inst);
Owen Andersona4043c42011-08-17 17:44:15 +000090 break;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000091 }
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000092 }
Jim Grosbach112a2de2011-05-09 20:05:25 +000093
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000094 return false;
Sean Callanan7e645502009-12-17 01:49:59 +000095}
96
Tim Northover48cf6cc2013-07-19 10:05:04 +000097static 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,")) {
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000101 Str = Str.substr(Pos);
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000102 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000103
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000104 // If this is the end of a line or start of a comment, remove the rest of
105 // the line.
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') {
110 Str = Str.substr(1);
111 } else {
112 Str = Str.substr(Str.find_first_of('\n'));
113 if (!Str.empty())
114 Str = Str.substr(1);
115 }
116 continue;
117 }
Tim Northover48cf6cc2013-07-19 10:05:04 +0000118 }
119
120 return !Str.empty();
121}
122
123
124static bool ByteArrayFromString(ByteArrayTy &ByteArray,
125 StringRef &Str,
126 SourceMgr &SM) {
127 while (SkipToToken(Str)) {
128 // Handled by higher level
129 if (Str[0] == '[' || Str[0] == ']')
130 return false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000131
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000132 // Get the current token.
Tim Northover48cf6cc2013-07-19 10:05:04 +0000133 size_t Next = Str.find_first_of(" \t\n\r,#[]");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000134 StringRef Value = Str.substr(0, Next);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000135
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000136 // Convert to a byte and add to the byte vector.
137 unsigned ByteVal;
138 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
139 // If we have an error, print it and skip to the end of line.
Chris Lattner03b80a42011-10-16 05:43:57 +0000140 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
141 "invalid input token");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000142 Str = Str.substr(Str.find('\n'));
143 ByteArray.clear();
144 continue;
145 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000146
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000147 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
148 Str = Str.substr(Next);
149 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000150
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000151 return false;
152}
153
Evan Chengab37af92011-07-06 19:45:42 +0000154int Disassembler::disassemble(const Target &T,
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000155 const std::string &Triple,
Richard Bartondef81b92012-04-16 11:32:10 +0000156 MCSubtargetInfo &STI,
157 MCStreamer &Streamer,
Dan Gohman268b0f42010-08-20 01:07:01 +0000158 MemoryBuffer &Buffer,
Richard Bartondef81b92012-04-16 11:32:10 +0000159 SourceMgr &SM,
Dan Gohman268b0f42010-08-20 01:07:01 +0000160 raw_ostream &Out) {
Richard Bartondef81b92012-04-16 11:32:10 +0000161 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
Chris Lattner72cbaa22009-12-22 06:24:00 +0000162 if (!DisAsm) {
163 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callanan7e645502009-12-17 01:49:59 +0000164 return -1;
165 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000166
Richard Bartondef81b92012-04-16 11:32:10 +0000167 // Set up initial section manually here
168 Streamer.InitSections();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000169
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000170 bool ErrorOccurred = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000171
Sean Callanan7e645502009-12-17 01:49:59 +0000172 // Convert the input to a vector for disassembly.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000173 ByteArrayTy ByteArray;
Chris Lattner55ddc302010-04-09 04:24:20 +0000174 StringRef Str = Buffer.getBuffer();
Tim Northover48cf6cc2013-07-19 10:05:04 +0000175 bool InAtomicBlock = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000176
Tim Northover48cf6cc2013-07-19 10:05:04 +0000177 while (SkipToToken(Str)) {
178 ByteArray.clear();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000179
Tim Northover48cf6cc2013-07-19 10:05:04 +0000180 if (Str[0] == '[') {
181 if (InAtomicBlock) {
182 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
183 "nested atomic blocks make no sense");
184 ErrorOccurred = true;
185 }
186 InAtomicBlock = true;
187 Str = Str.drop_front();
188 continue;
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;
194 }
195 InAtomicBlock = false;
196 Str = Str.drop_front();
197 continue;
198 }
199
200 // It's a real token, get the bytes and emit them
201 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
202
203 if (!ByteArray.empty())
204 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
205 InAtomicBlock);
206 }
207
208 if (InAtomicBlock) {
209 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
210 "unclosed atomic block");
211 ErrorOccurred = true;
212 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000213
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000214 return ErrorOccurred;
Sean Callanan7e645502009-12-17 01:49:59 +0000215}