blob: 5f2fdb8071423e4358b631d4ec79c37836f38ce5 [file] [log] [blame]
Chris Lattnera3dcfb12009-12-22 22:50:29 +00001//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
Sean Callananba847da2009-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 Lattnera3dcfb12009-12-22 22:50:29 +000015#include "Disassembler.h"
Chris Lattner847da552010-07-20 18:25:19 +000016#include "../../lib/MC/MCDisassembler/EDDisassembler.h"
17#include "../../lib/MC/MCDisassembler/EDInst.h"
18#include "../../lib/MC/MCDisassembler/EDOperand.h"
19#include "../../lib/MC/MCDisassembler/EDToken.h"
Sean Callananba847da2009-12-17 01:49:59 +000020#include "llvm/MC/MCDisassembler.h"
21#include "llvm/MC/MCInst.h"
Richard Bartond0c478d2012-04-16 11:32:10 +000022#include "llvm/MC/MCStreamer.h"
James Molloyb9505852011-09-07 17:24:38 +000023#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner847da552010-07-20 18:25:19 +000024#include "llvm/ADT/OwningPtr.h"
25#include "llvm/ADT/Triple.h"
Sean Callananba847da2009-12-17 01:49:59 +000026#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/MemoryObject.h"
Chris Lattnerc3de94f2009-12-22 06:45:48 +000028#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
30#include "llvm/Support/raw_ostream.h"
Richard Bartond0c478d2012-04-16 11:32:10 +000031
Sean Callananba847da2009-12-17 01:49:59 +000032using namespace llvm;
33
Chris Lattner665e9472009-12-22 06:56:51 +000034typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
35
36namespace {
Sean Callananba847da2009-12-17 01:49:59 +000037class VectorMemoryObject : public MemoryObject {
38private:
Chris Lattner665e9472009-12-22 06:56:51 +000039 const ByteArrayTy &Bytes;
Sean Callananba847da2009-12-17 01:49:59 +000040public:
Chris Lattner665e9472009-12-22 06:56:51 +000041 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000042
Chris Lattner665e9472009-12-22 06:56:51 +000043 uint64_t getBase() const { return 0; }
Derek Schuffadef06a2012-02-29 01:09:06 +000044 uint64_t getExtent() const { return Bytes.size(); }
Sean Callananba847da2009-12-17 01:49:59 +000045
Derek Schuffadef06a2012-02-29 01:09:06 +000046 int readByte(uint64_t Addr, uint8_t *Byte) const {
Rafael Espindola2f867a62011-01-06 16:48:42 +000047 if (Addr >= getExtent())
Sean Callananba847da2009-12-17 01:49:59 +000048 return -1;
Chris Lattner665e9472009-12-22 06:56:51 +000049 *Byte = Bytes[Addr].first;
Sean Callananba847da2009-12-17 01:49:59 +000050 return 0;
51 }
52};
Chris Lattner665e9472009-12-22 06:56:51 +000053}
Sean Callananba847da2009-12-17 01:49:59 +000054
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000055static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartond0c478d2012-04-16 11:32:10 +000056 const ByteArrayTy &Bytes,
57 SourceMgr &SM, raw_ostream &Out,
58 MCStreamer &Streamer) {
Sean Callananba847da2009-12-17 01:49:59 +000059 // Wrap the vector in a MemoryObject.
Chris Lattner665e9472009-12-22 06:56:51 +000060 VectorMemoryObject memoryObject(Bytes);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000061
Sean Callanan2e235a82010-02-03 03:46:41 +000062 // Disassemble it to strings.
Chris Lattner665e9472009-12-22 06:56:51 +000063 uint64_t Size;
Sean Callanan2e235a82010-02-03 03:46:41 +000064 uint64_t Index;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000065
Sean Callanan2e235a82010-02-03 03:46:41 +000066 for (Index = 0; Index < Bytes.size(); Index += Size) {
67 MCInst Inst;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000068
Owen Anderson83e3f672011-08-17 17:44:15 +000069 MCDisassembler::DecodeStatus S;
70 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
Owen Anderson98c5dda2011-09-15 23:38:46 +000071 /*REMOVE*/ nulls(), nulls());
Owen Anderson83e3f672011-08-17 17:44:15 +000072 switch (S) {
73 case MCDisassembler::Fail:
Sean Callanan2e235a82010-02-03 03:46:41 +000074 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner3f2d5f62011-10-16 05:43:57 +000075 SourceMgr::DK_Warning,
76 "invalid instruction encoding");
Sean Callanan2e235a82010-02-03 03:46:41 +000077 if (Size == 0)
78 Size = 1; // skip illegible bytes
Owen Anderson83e3f672011-08-17 17:44:15 +000079 break;
80
81 case MCDisassembler::SoftFail:
82 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner3f2d5f62011-10-16 05:43:57 +000083 SourceMgr::DK_Warning,
84 "potentially undefined instruction encoding");
Owen Anderson83e3f672011-08-17 17:44:15 +000085 // Fall through
86
87 case MCDisassembler::Success:
Richard Bartond0c478d2012-04-16 11:32:10 +000088 Streamer.EmitInstruction(Inst);
Owen Anderson83e3f672011-08-17 17:44:15 +000089 break;
Sean Callanan2e235a82010-02-03 03:46:41 +000090 }
Chris Lattner665e9472009-12-22 06:56:51 +000091 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000092
Chris Lattner665e9472009-12-22 06:56:51 +000093 return false;
Sean Callananba847da2009-12-17 01:49:59 +000094}
95
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +000096static bool ByteArrayFromString(ByteArrayTy &ByteArray,
97 StringRef &Str,
Sean Callanan668b1542010-04-12 19:43:00 +000098 SourceMgr &SM) {
99 while (!Str.empty()) {
100 // Strip horizontal whitespace.
101 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
102 Str = Str.substr(Pos);
103 continue;
104 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000105
Sean Callanan668b1542010-04-12 19:43:00 +0000106 // If this is the end of a line or start of a comment, remove the rest of
107 // the line.
108 if (Str[0] == '\n' || Str[0] == '#') {
109 // Strip to the end of line if we already processed any bytes on this
110 // line. This strips the comment and/or the \n.
111 if (Str[0] == '\n') {
112 Str = Str.substr(1);
113 } else {
114 Str = Str.substr(Str.find_first_of('\n'));
115 if (!Str.empty())
116 Str = Str.substr(1);
117 }
118 continue;
119 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000120
Sean Callanan668b1542010-04-12 19:43:00 +0000121 // Get the current token.
122 size_t Next = Str.find_first_of(" \t\n\r#");
123 StringRef Value = Str.substr(0, Next);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000124
Sean Callanan668b1542010-04-12 19:43:00 +0000125 // Convert to a byte and add to the byte vector.
126 unsigned ByteVal;
127 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
128 // If we have an error, print it and skip to the end of line.
Chris Lattner3f2d5f62011-10-16 05:43:57 +0000129 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
130 "invalid input token");
Sean Callanan668b1542010-04-12 19:43:00 +0000131 Str = Str.substr(Str.find('\n'));
132 ByteArray.clear();
133 continue;
134 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000135
Sean Callanan668b1542010-04-12 19:43:00 +0000136 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
137 Str = Str.substr(Next);
138 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000139
Sean Callanan668b1542010-04-12 19:43:00 +0000140 return false;
141}
142
Evan Chengb2627992011-07-06 19:45:42 +0000143int Disassembler::disassemble(const Target &T,
Bill Wendlinga5c177e2011-03-21 04:13:46 +0000144 const std::string &Triple,
Richard Bartond0c478d2012-04-16 11:32:10 +0000145 MCSubtargetInfo &STI,
146 MCStreamer &Streamer,
Dan Gohmand5826a32010-08-20 01:07:01 +0000147 MemoryBuffer &Buffer,
Richard Bartond0c478d2012-04-16 11:32:10 +0000148 SourceMgr &SM,
Dan Gohmand5826a32010-08-20 01:07:01 +0000149 raw_ostream &Out) {
Richard Bartond0c478d2012-04-16 11:32:10 +0000150 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
Chris Lattner222af462009-12-22 06:24:00 +0000151 if (!DisAsm) {
152 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000153 return -1;
154 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000155
Richard Bartond0c478d2012-04-16 11:32:10 +0000156 // Set up initial section manually here
157 Streamer.InitSections();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000158
Chris Lattner665e9472009-12-22 06:56:51 +0000159 bool ErrorOccurred = false;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000160
Sean Callananba847da2009-12-17 01:49:59 +0000161 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000162 ByteArrayTy ByteArray;
Chris Lattnercfc99a92010-04-09 04:24:20 +0000163 StringRef Str = Buffer.getBuffer();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000164
Sean Callanan668b1542010-04-12 19:43:00 +0000165 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000166
Chris Lattner222af462009-12-22 06:24:00 +0000167 if (!ByteArray.empty())
Richard Bartond0c478d2012-04-16 11:32:10 +0000168 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000169
Chris Lattner665e9472009-12-22 06:56:51 +0000170 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000171}
Sean Callanan668b1542010-04-12 19:43:00 +0000172
173static int byteArrayReader(uint8_t *B, uint64_t A, void *Arg) {
174 ByteArrayTy &ByteArray = *((ByteArrayTy*)Arg);
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000175
Sean Callanan668b1542010-04-12 19:43:00 +0000176 if (A >= ByteArray.size())
177 return -1;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000178
Sean Callanan668b1542010-04-12 19:43:00 +0000179 *B = ByteArray[A].first;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000180
Sean Callanan668b1542010-04-12 19:43:00 +0000181 return 0;
182}
183
184static int verboseEvaluator(uint64_t *V, unsigned R, void *Arg) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000185 EDDisassembler &disassembler = *(EDDisassembler *)((void **)Arg)[0];
186 raw_ostream &Out = *(raw_ostream *)((void **)Arg)[1];
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000187
Chris Lattner847da552010-07-20 18:25:19 +0000188 if (const char *regName = disassembler.nameWithRegisterID(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000189 Out << "[" << regName << "/" << R << "]";
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000190
Chris Lattner847da552010-07-20 18:25:19 +0000191 if (disassembler.registerIsStackPointer(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000192 Out << "(sp)";
Chris Lattner847da552010-07-20 18:25:19 +0000193 if (disassembler.registerIsProgramCounter(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000194 Out << "(pc)";
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000195
Sean Callanan668b1542010-04-12 19:43:00 +0000196 *V = 0;
Sean Callanan668b1542010-04-12 19:43:00 +0000197 return 0;
198}
199
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000200int Disassembler::disassembleEnhanced(const std::string &TS,
Dan Gohmand5826a32010-08-20 01:07:01 +0000201 MemoryBuffer &Buffer,
Richard Bartond0c478d2012-04-16 11:32:10 +0000202 SourceMgr &SM,
Dan Gohmand5826a32010-08-20 01:07:01 +0000203 raw_ostream &Out) {
Sean Callanan668b1542010-04-12 19:43:00 +0000204 ByteArrayTy ByteArray;
205 StringRef Str = Buffer.getBuffer();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000206
Sean Callanan668b1542010-04-12 19:43:00 +0000207 if (ByteArrayFromString(ByteArray, Str, SM)) {
208 return -1;
209 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000210
Sean Callanan668b1542010-04-12 19:43:00 +0000211 Triple T(TS);
Chris Lattner847da552010-07-20 18:25:19 +0000212 EDDisassembler::AssemblySyntax AS;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000213
Sean Callanan668b1542010-04-12 19:43:00 +0000214 switch (T.getArch()) {
215 default:
216 errs() << "error: no default assembly syntax for " << TS.c_str() << "\n";
217 return -1;
218 case Triple::arm:
219 case Triple::thumb:
Chris Lattner847da552010-07-20 18:25:19 +0000220 AS = EDDisassembler::kEDAssemblySyntaxARMUAL;
Sean Callanan668b1542010-04-12 19:43:00 +0000221 break;
222 case Triple::x86:
223 case Triple::x86_64:
Chris Lattner847da552010-07-20 18:25:19 +0000224 AS = EDDisassembler::kEDAssemblySyntaxX86ATT;
Sean Callanan668b1542010-04-12 19:43:00 +0000225 break;
226 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000227
Benjamin Kramerf26be1e2011-02-24 11:03:19 +0000228 OwningPtr<EDDisassembler>
229 disassembler(EDDisassembler::getDisassembler(TS.c_str(), AS));
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000230
Chris Lattner847da552010-07-20 18:25:19 +0000231 if (disassembler == 0) {
232 errs() << "error: couldn't get disassembler for " << TS << '\n';
Sean Callanan668b1542010-04-12 19:43:00 +0000233 return -1;
234 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000235
Sean Callanan3894a792011-02-23 03:29:41 +0000236 while (ByteArray.size()) {
Benjamin Kramerf26be1e2011-02-24 11:03:19 +0000237 OwningPtr<EDInst>
238 inst(disassembler->createInst(byteArrayReader, 0, &ByteArray));
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000239
Sean Callanan3894a792011-02-23 03:29:41 +0000240 if (inst == 0) {
241 errs() << "error: Didn't get an instruction\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000242 return -1;
243 }
Sean Callanan052aa2c2011-04-09 00:21:04 +0000244
245 ByteArray.erase (ByteArray.begin(), ByteArray.begin() + inst->byteSize());
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000246
Sean Callanan3894a792011-02-23 03:29:41 +0000247 unsigned numTokens = inst->numTokens();
248 if ((int)numTokens < 0) {
249 errs() << "error: couldn't count the instruction's tokens\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000250 return -1;
251 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000252
Sean Callanan3894a792011-02-23 03:29:41 +0000253 for (unsigned tokenIndex = 0; tokenIndex != numTokens; ++tokenIndex) {
254 EDToken *token;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000255
Sean Callanan3894a792011-02-23 03:29:41 +0000256 if (inst->getToken(token, tokenIndex)) {
257 errs() << "error: Couldn't get token\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000258 return -1;
259 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000260
Sean Callanan3894a792011-02-23 03:29:41 +0000261 const char *buf;
262 if (token->getString(buf)) {
263 errs() << "error: Couldn't get string for token\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000264 return -1;
265 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000266
Sean Callanan3894a792011-02-23 03:29:41 +0000267 Out << '[';
268 int operandIndex = token->operandID();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000269
Sean Callanan3894a792011-02-23 03:29:41 +0000270 if (operandIndex >= 0)
271 Out << operandIndex << "-";
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000272
Sean Callanan3894a792011-02-23 03:29:41 +0000273 switch (token->type()) {
Sean Callanan3894a792011-02-23 03:29:41 +0000274 case EDToken::kTokenWhitespace: Out << "w"; break;
275 case EDToken::kTokenPunctuation: Out << "p"; break;
276 case EDToken::kTokenOpcode: Out << "o"; break;
277 case EDToken::kTokenLiteral: Out << "l"; break;
278 case EDToken::kTokenRegister: Out << "r"; break;
279 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000280
Sean Callanan3894a792011-02-23 03:29:41 +0000281 Out << ":" << buf;
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000282
Sean Callanan3894a792011-02-23 03:29:41 +0000283 if (token->type() == EDToken::kTokenLiteral) {
284 Out << "=";
285 if (token->literalSign())
286 Out << "-";
287 uint64_t absoluteValue;
288 if (token->literalAbsoluteValue(absoluteValue)) {
289 errs() << "error: Couldn't get the value of a literal token\n";
290 return -1;
291 }
292 Out << absoluteValue;
293 } else if (token->type() == EDToken::kTokenRegister) {
294 Out << "=";
295 unsigned regID;
296 if (token->registerID(regID)) {
297 errs() << "error: Couldn't get the ID of a register token\n";
298 return -1;
299 }
300 Out << "r" << regID;
301 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000302
Sean Callanan3894a792011-02-23 03:29:41 +0000303 Out << "]";
Sean Callanan668b1542010-04-12 19:43:00 +0000304 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000305
Sean Callanan3894a792011-02-23 03:29:41 +0000306 Out << " ";
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000307
Sean Callanan3894a792011-02-23 03:29:41 +0000308 if (inst->isBranch())
309 Out << "<br> ";
310 if (inst->isMove())
311 Out << "<mov> ";
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000312
Sean Callanan3894a792011-02-23 03:29:41 +0000313 unsigned numOperands = inst->numOperands();
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000314
Sean Callanan3894a792011-02-23 03:29:41 +0000315 if ((int)numOperands < 0) {
316 errs() << "error: Couldn't count operands\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000317 return -1;
318 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000319
320 for (unsigned operandIndex = 0; operandIndex != numOperands;
321 ++operandIndex) {
Sean Callanan3894a792011-02-23 03:29:41 +0000322 Out << operandIndex << ":";
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000323
Sean Callanan3894a792011-02-23 03:29:41 +0000324 EDOperand *operand;
325 if (inst->getOperand(operand, operandIndex)) {
326 errs() << "error: couldn't get operand\n";
327 return -1;
328 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000329
Sean Callanan3894a792011-02-23 03:29:41 +0000330 uint64_t evaluatedResult;
Benjamin Kramerf26be1e2011-02-24 11:03:19 +0000331 void *Arg[] = { disassembler.get(), &Out };
Sean Callanan3894a792011-02-23 03:29:41 +0000332 if (operand->evaluate(evaluatedResult, verboseEvaluator, Arg)) {
333 errs() << "error: Couldn't evaluate an operand\n";
334 return -1;
335 }
336 Out << "=" << evaluatedResult << " ";
Sean Callananb21e49c2011-02-22 02:09:15 +0000337 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000338
Sean Callanan3894a792011-02-23 03:29:41 +0000339 Out << '\n';
Sean Callanan668b1542010-04-12 19:43:00 +0000340 }
Jim Grosbachf5bf3cf2011-05-09 20:05:25 +0000341
Sean Callanan668b1542010-04-12 19:43:00 +0000342 return 0;
343}