blob: e5c31ca598eb9659b263b4690b48503735847f78 [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/MCAsmInfo.h"
21#include "llvm/MC/MCDisassembler.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/Target/TargetRegistry.h"
Chris Lattner847da552010-07-20 18:25:19 +000025#include "llvm/ADT/OwningPtr.h"
26#include "llvm/ADT/Triple.h"
Benjamin Kramerd1e17032010-09-27 17:42:11 +000027#include "llvm/ADT/Twine.h"
Sean Callananba847da2009-12-17 01:49:59 +000028#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/MemoryObject.h"
30#include "llvm/Support/raw_ostream.h"
Chris Lattnerc3de94f2009-12-22 06:45:48 +000031#include "llvm/Support/SourceMgr.h"
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) {}
Sean Callananba847da2009-12-17 01:49:59 +000042
Chris Lattner665e9472009-12-22 06:56:51 +000043 uint64_t getBase() const { return 0; }
44 uint64_t getExtent() const { return Bytes.size(); }
Sean Callananba847da2009-12-17 01:49:59 +000045
Chris Lattner665e9472009-12-22 06:56:51 +000046 int readByte(uint64_t Addr, uint8_t *Byte) const {
47 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,
Chris Lattnerd3740872010-04-04 05:04:31 +000056 MCInstPrinter &Printer, const ByteArrayTy &Bytes,
Dan Gohmand5826a32010-08-20 01:07:01 +000057 SourceMgr &SM, raw_ostream &Out) {
Sean Callananba847da2009-12-17 01:49:59 +000058 // Wrap the vector in a MemoryObject.
Chris Lattner665e9472009-12-22 06:56:51 +000059 VectorMemoryObject memoryObject(Bytes);
Sean Callananba847da2009-12-17 01:49:59 +000060
Sean Callanan2e235a82010-02-03 03:46:41 +000061 // Disassemble it to strings.
Chris Lattner665e9472009-12-22 06:56:51 +000062 uint64_t Size;
Sean Callanan2e235a82010-02-03 03:46:41 +000063 uint64_t Index;
Sean Callananba847da2009-12-17 01:49:59 +000064
Sean Callanan2e235a82010-02-03 03:46:41 +000065 for (Index = 0; Index < Bytes.size(); Index += Size) {
66 MCInst Inst;
67
68 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
69 /*REMOVE*/ nulls())) {
Dan Gohmand5826a32010-08-20 01:07:01 +000070 Printer.printInst(&Inst, Out);
71 Out << "\n";
Sean Callanan668b1542010-04-12 19:43:00 +000072 } else {
Sean Callanan2e235a82010-02-03 03:46:41 +000073 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
74 "invalid instruction encoding", "warning");
75 if (Size == 0)
76 Size = 1; // skip illegible bytes
77 }
Chris Lattner665e9472009-12-22 06:56:51 +000078 }
79
80 return false;
Sean Callananba847da2009-12-17 01:49:59 +000081}
82
Sean Callanan668b1542010-04-12 19:43:00 +000083static bool ByteArrayFromString(ByteArrayTy &ByteArray,
84 StringRef &Str,
85 SourceMgr &SM) {
86 while (!Str.empty()) {
87 // Strip horizontal whitespace.
88 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
89 Str = Str.substr(Pos);
90 continue;
91 }
92
93 // If this is the end of a line or start of a comment, remove the rest of
94 // the line.
95 if (Str[0] == '\n' || Str[0] == '#') {
96 // Strip to the end of line if we already processed any bytes on this
97 // line. This strips the comment and/or the \n.
98 if (Str[0] == '\n') {
99 Str = Str.substr(1);
100 } else {
101 Str = Str.substr(Str.find_first_of('\n'));
102 if (!Str.empty())
103 Str = Str.substr(1);
104 }
105 continue;
106 }
107
108 // Get the current token.
109 size_t Next = Str.find_first_of(" \t\n\r#");
110 StringRef Value = Str.substr(0, Next);
111
112 // Convert to a byte and add to the byte vector.
113 unsigned ByteVal;
114 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
115 // If we have an error, print it and skip to the end of line.
116 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
117 "invalid input token", "error");
118 Str = Str.substr(Str.find('\n'));
119 ByteArray.clear();
120 continue;
121 }
122
123 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
124 Str = Str.substr(Next);
125 }
126
127 return false;
128}
129
Chris Lattnera3dcfb12009-12-22 22:50:29 +0000130int Disassembler::disassemble(const Target &T, const std::string &Triple,
Dan Gohmand5826a32010-08-20 01:07:01 +0000131 MemoryBuffer &Buffer,
132 raw_ostream &Out) {
Chris Lattner222af462009-12-22 06:24:00 +0000133 // Set up disassembler.
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000134 OwningPtr<const MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +0000135
Chris Lattner222af462009-12-22 06:24:00 +0000136 if (!AsmInfo) {
137 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000138 return -1;
139 }
140
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000141 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler());
Chris Lattner222af462009-12-22 06:24:00 +0000142 if (!DisAsm) {
143 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000144 return -1;
145 }
146
Chris Lattnerf23c7692010-04-13 18:41:17 +0000147 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
148 OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(AsmPrinterVariant,
149 *AsmInfo));
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000150 if (!IP) {
Chris Lattner665e9472009-12-22 06:56:51 +0000151 errs() << "error: no instruction printer for target " << Triple << '\n';
Sean Callananba847da2009-12-17 01:49:59 +0000152 return -1;
153 }
154
Chris Lattner665e9472009-12-22 06:56:51 +0000155 bool ErrorOccurred = false;
156
157 SourceMgr SM;
158 SM.AddNewSourceBuffer(&Buffer, SMLoc());
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000159
Sean Callananba847da2009-12-17 01:49:59 +0000160 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000161 ByteArrayTy ByteArray;
Chris Lattnercfc99a92010-04-09 04:24:20 +0000162 StringRef Str = Buffer.getBuffer();
Sean Callanan668b1542010-04-12 19:43:00 +0000163
164 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
Sean Callananba847da2009-12-17 01:49:59 +0000165
Chris Lattner222af462009-12-22 06:24:00 +0000166 if (!ByteArray.empty())
Dan Gohmand5826a32010-08-20 01:07:01 +0000167 ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM, Out);
Sean Callananba847da2009-12-17 01:49:59 +0000168
Chris Lattner665e9472009-12-22 06:56:51 +0000169 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000170}
Sean Callanan668b1542010-04-12 19:43:00 +0000171
172static int byteArrayReader(uint8_t *B, uint64_t A, void *Arg) {
173 ByteArrayTy &ByteArray = *((ByteArrayTy*)Arg);
174
175 if (A >= ByteArray.size())
176 return -1;
177
178 *B = ByteArray[A].first;
179
180 return 0;
181}
182
183static int verboseEvaluator(uint64_t *V, unsigned R, void *Arg) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000184 EDDisassembler &disassembler = *(EDDisassembler *)((void **)Arg)[0];
185 raw_ostream &Out = *(raw_ostream *)((void **)Arg)[1];
Sean Callanan668b1542010-04-12 19:43:00 +0000186
Chris Lattner847da552010-07-20 18:25:19 +0000187 if (const char *regName = disassembler.nameWithRegisterID(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000188 Out << "[" << regName << "/" << R << "]";
Chris Lattner847da552010-07-20 18:25:19 +0000189
190 if (disassembler.registerIsStackPointer(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000191 Out << "(sp)";
Chris Lattner847da552010-07-20 18:25:19 +0000192 if (disassembler.registerIsProgramCounter(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000193 Out << "(pc)";
Sean Callanan668b1542010-04-12 19:43:00 +0000194
195 *V = 0;
Sean Callanan668b1542010-04-12 19:43:00 +0000196 return 0;
197}
198
199int Disassembler::disassembleEnhanced(const std::string &TS,
Dan Gohmand5826a32010-08-20 01:07:01 +0000200 MemoryBuffer &Buffer,
201 raw_ostream &Out) {
Sean Callanan668b1542010-04-12 19:43:00 +0000202 ByteArrayTy ByteArray;
203 StringRef Str = Buffer.getBuffer();
204 SourceMgr SM;
205
206 SM.AddNewSourceBuffer(&Buffer, SMLoc());
207
208 if (ByteArrayFromString(ByteArray, Str, SM)) {
209 return -1;
210 }
211
Sean Callanan668b1542010-04-12 19:43:00 +0000212 Triple T(TS);
Chris Lattner847da552010-07-20 18:25:19 +0000213 EDDisassembler::AssemblySyntax AS;
Sean Callanan668b1542010-04-12 19:43:00 +0000214
215 switch (T.getArch()) {
216 default:
217 errs() << "error: no default assembly syntax for " << TS.c_str() << "\n";
218 return -1;
219 case Triple::arm:
220 case Triple::thumb:
Chris Lattner847da552010-07-20 18:25:19 +0000221 AS = EDDisassembler::kEDAssemblySyntaxARMUAL;
Sean Callanan668b1542010-04-12 19:43:00 +0000222 break;
223 case Triple::x86:
224 case Triple::x86_64:
Chris Lattner847da552010-07-20 18:25:19 +0000225 AS = EDDisassembler::kEDAssemblySyntaxX86ATT;
Sean Callanan668b1542010-04-12 19:43:00 +0000226 break;
227 }
228
Chris Lattner847da552010-07-20 18:25:19 +0000229 EDDisassembler::initialize();
230 EDDisassembler *disassembler =
231 EDDisassembler::getDisassembler(TS.c_str(), AS);
232
233 if (disassembler == 0) {
234 errs() << "error: couldn't get disassembler for " << TS << '\n';
Sean Callanan668b1542010-04-12 19:43:00 +0000235 return -1;
236 }
237
Chris Lattner847da552010-07-20 18:25:19 +0000238 EDInst *inst =
239 disassembler->createInst(byteArrayReader, 0, &ByteArray);
240
241 if (inst == 0) {
Sean Callanan668b1542010-04-12 19:43:00 +0000242 errs() << "error: Didn't get an instruction\n";
243 return -1;
244 }
245
Chris Lattner847da552010-07-20 18:25:19 +0000246 unsigned numTokens = inst->numTokens();
247 if ((int)numTokens < 0) {
248 errs() << "error: couldn't count the instruction's tokens\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000249 return -1;
250 }
251
Chris Lattner847da552010-07-20 18:25:19 +0000252 for (unsigned tokenIndex = 0; tokenIndex != numTokens; ++tokenIndex) {
253 EDToken *token;
Sean Callanan668b1542010-04-12 19:43:00 +0000254
Chris Lattner847da552010-07-20 18:25:19 +0000255 if (inst->getToken(token, tokenIndex)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000256 errs() << "error: Couldn't get token\n";
257 return -1;
258 }
259
260 const char *buf;
Chris Lattner847da552010-07-20 18:25:19 +0000261 if (token->getString(buf)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000262 errs() << "error: Couldn't get string for token\n";
263 return -1;
264 }
265
Dan Gohmand5826a32010-08-20 01:07:01 +0000266 Out << '[';
Chris Lattner847da552010-07-20 18:25:19 +0000267 int operandIndex = token->operandID();
Sean Callanan668b1542010-04-12 19:43:00 +0000268
269 if (operandIndex >= 0)
Dan Gohmand5826a32010-08-20 01:07:01 +0000270 Out << operandIndex << "-";
Sean Callanan668b1542010-04-12 19:43:00 +0000271
Chris Lattner847da552010-07-20 18:25:19 +0000272 switch (token->type()) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000273 default: Out << "?"; break;
274 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;
Sean Callanan668b1542010-04-12 19:43:00 +0000279 }
280
Dan Gohmand5826a32010-08-20 01:07:01 +0000281 Out << ":" << buf;
Sean Callanan668b1542010-04-12 19:43:00 +0000282
Chris Lattner847da552010-07-20 18:25:19 +0000283 if (token->type() == EDToken::kTokenLiteral) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000284 Out << "=";
Chris Lattner847da552010-07-20 18:25:19 +0000285 if (token->literalSign())
Dan Gohmand5826a32010-08-20 01:07:01 +0000286 Out << "-";
Sean Callanan668b1542010-04-12 19:43:00 +0000287 uint64_t absoluteValue;
Chris Lattner847da552010-07-20 18:25:19 +0000288 if (token->literalAbsoluteValue(absoluteValue)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000289 errs() << "error: Couldn't get the value of a literal token\n";
290 return -1;
291 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000292 Out << absoluteValue;
Chris Lattner847da552010-07-20 18:25:19 +0000293 } else if (token->type() == EDToken::kTokenRegister) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000294 Out << "=";
Sean Callanan668b1542010-04-12 19:43:00 +0000295 unsigned regID;
Chris Lattner847da552010-07-20 18:25:19 +0000296 if (token->registerID(regID)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000297 errs() << "error: Couldn't get the ID of a register token\n";
298 return -1;
299 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000300 Out << "r" << regID;
Sean Callanan668b1542010-04-12 19:43:00 +0000301 }
302
Dan Gohmand5826a32010-08-20 01:07:01 +0000303 Out << "]";
Sean Callanan668b1542010-04-12 19:43:00 +0000304 }
305
Dan Gohmand5826a32010-08-20 01:07:01 +0000306 Out << " ";
Sean Callanan972bf8d2010-05-11 01:27:08 +0000307
Chris Lattner847da552010-07-20 18:25:19 +0000308 if (inst->isBranch())
Dan Gohmand5826a32010-08-20 01:07:01 +0000309 Out << "<br> ";
Chris Lattner847da552010-07-20 18:25:19 +0000310 if (inst->isMove())
Dan Gohmand5826a32010-08-20 01:07:01 +0000311 Out << "<mov> ";
Sean Callanan668b1542010-04-12 19:43:00 +0000312
Chris Lattner847da552010-07-20 18:25:19 +0000313 unsigned numOperands = inst->numOperands();
Sean Callanan668b1542010-04-12 19:43:00 +0000314
Chris Lattner847da552010-07-20 18:25:19 +0000315 if ((int)numOperands < 0) {
Sean Callanan668b1542010-04-12 19:43:00 +0000316 errs() << "error: Couldn't count operands\n";
317 return -1;
318 }
319
Chris Lattner847da552010-07-20 18:25:19 +0000320 for (unsigned operandIndex = 0; operandIndex != numOperands; ++operandIndex) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000321 Out << operandIndex << ":";
Sean Callanan668b1542010-04-12 19:43:00 +0000322
Chris Lattner847da552010-07-20 18:25:19 +0000323 EDOperand *operand;
324 if (inst->getOperand(operand, operandIndex)) {
325 errs() << "error: couldn't get operand\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000326 return -1;
327 }
328
329 uint64_t evaluatedResult;
Dan Gohmand5826a32010-08-20 01:07:01 +0000330 void *Arg[] = { disassembler, &Out };
331 evaluatedResult = operand->evaluate(evaluatedResult, verboseEvaluator, Arg);
332 Out << "=" << evaluatedResult << " ";
Sean Callanan668b1542010-04-12 19:43:00 +0000333 }
334
Dan Gohmand5826a32010-08-20 01:07:01 +0000335 Out << '\n';
Sean Callanan668b1542010-04-12 19:43:00 +0000336
337 return 0;
338}
339