blob: 13080b481f14926d50e3efaf0708bd6b07630bb7 [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"
Sean Callananba847da2009-12-17 01:49:59 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/MemoryObject.h"
29#include "llvm/Support/raw_ostream.h"
Chris Lattnerc3de94f2009-12-22 06:45:48 +000030#include "llvm/Support/SourceMgr.h"
Sean Callananba847da2009-12-17 01:49:59 +000031using namespace llvm;
32
Chris Lattner665e9472009-12-22 06:56:51 +000033typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
34
35namespace {
Sean Callananba847da2009-12-17 01:49:59 +000036class VectorMemoryObject : public MemoryObject {
37private:
Chris Lattner665e9472009-12-22 06:56:51 +000038 const ByteArrayTy &Bytes;
Sean Callananba847da2009-12-17 01:49:59 +000039public:
Chris Lattner665e9472009-12-22 06:56:51 +000040 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Sean Callananba847da2009-12-17 01:49:59 +000041
Chris Lattner665e9472009-12-22 06:56:51 +000042 uint64_t getBase() const { return 0; }
43 uint64_t getExtent() const { return Bytes.size(); }
Sean Callananba847da2009-12-17 01:49:59 +000044
Chris Lattner665e9472009-12-22 06:56:51 +000045 int readByte(uint64_t Addr, uint8_t *Byte) const {
46 if (Addr > getExtent())
Sean Callananba847da2009-12-17 01:49:59 +000047 return -1;
Chris Lattner665e9472009-12-22 06:56:51 +000048 *Byte = Bytes[Addr].first;
Sean Callananba847da2009-12-17 01:49:59 +000049 return 0;
50 }
51};
Chris Lattner665e9472009-12-22 06:56:51 +000052}
Sean Callananba847da2009-12-17 01:49:59 +000053
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000054static bool PrintInsts(const MCDisassembler &DisAsm,
Chris Lattnerd3740872010-04-04 05:04:31 +000055 MCInstPrinter &Printer, const ByteArrayTy &Bytes,
Dan Gohmand5826a32010-08-20 01:07:01 +000056 SourceMgr &SM, raw_ostream &Out) {
Sean Callananba847da2009-12-17 01:49:59 +000057 // Wrap the vector in a MemoryObject.
Chris Lattner665e9472009-12-22 06:56:51 +000058 VectorMemoryObject memoryObject(Bytes);
Sean Callananba847da2009-12-17 01:49:59 +000059
Sean Callanan2e235a82010-02-03 03:46:41 +000060 // Disassemble it to strings.
Chris Lattner665e9472009-12-22 06:56:51 +000061 uint64_t Size;
Sean Callanan2e235a82010-02-03 03:46:41 +000062 uint64_t Index;
Sean Callananba847da2009-12-17 01:49:59 +000063
Sean Callanan2e235a82010-02-03 03:46:41 +000064 for (Index = 0; Index < Bytes.size(); Index += Size) {
65 MCInst Inst;
66
67 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
68 /*REMOVE*/ nulls())) {
Dan Gohmand5826a32010-08-20 01:07:01 +000069 Printer.printInst(&Inst, Out);
70 Out << "\n";
Sean Callanan668b1542010-04-12 19:43:00 +000071 } else {
Sean Callanan2e235a82010-02-03 03:46:41 +000072 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
73 "invalid instruction encoding", "warning");
74 if (Size == 0)
75 Size = 1; // skip illegible bytes
76 }
Chris Lattner665e9472009-12-22 06:56:51 +000077 }
78
79 return false;
Sean Callananba847da2009-12-17 01:49:59 +000080}
81
Sean Callanan668b1542010-04-12 19:43:00 +000082static bool ByteArrayFromString(ByteArrayTy &ByteArray,
83 StringRef &Str,
84 SourceMgr &SM) {
85 while (!Str.empty()) {
86 // Strip horizontal whitespace.
87 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
88 Str = Str.substr(Pos);
89 continue;
90 }
91
92 // If this is the end of a line or start of a comment, remove the rest of
93 // the line.
94 if (Str[0] == '\n' || Str[0] == '#') {
95 // Strip to the end of line if we already processed any bytes on this
96 // line. This strips the comment and/or the \n.
97 if (Str[0] == '\n') {
98 Str = Str.substr(1);
99 } else {
100 Str = Str.substr(Str.find_first_of('\n'));
101 if (!Str.empty())
102 Str = Str.substr(1);
103 }
104 continue;
105 }
106
107 // Get the current token.
108 size_t Next = Str.find_first_of(" \t\n\r#");
109 StringRef Value = Str.substr(0, Next);
110
111 // Convert to a byte and add to the byte vector.
112 unsigned ByteVal;
113 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
114 // If we have an error, print it and skip to the end of line.
115 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
116 "invalid input token", "error");
117 Str = Str.substr(Str.find('\n'));
118 ByteArray.clear();
119 continue;
120 }
121
122 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
123 Str = Str.substr(Next);
124 }
125
126 return false;
127}
128
Chris Lattnera3dcfb12009-12-22 22:50:29 +0000129int Disassembler::disassemble(const Target &T, const std::string &Triple,
Dan Gohmand5826a32010-08-20 01:07:01 +0000130 MemoryBuffer &Buffer,
131 raw_ostream &Out) {
Chris Lattner222af462009-12-22 06:24:00 +0000132 // Set up disassembler.
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000133 OwningPtr<const MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +0000134
Chris Lattner222af462009-12-22 06:24:00 +0000135 if (!AsmInfo) {
136 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000137 return -1;
138 }
139
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000140 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler());
Chris Lattner222af462009-12-22 06:24:00 +0000141 if (!DisAsm) {
142 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +0000143 return -1;
144 }
145
Chris Lattnerf23c7692010-04-13 18:41:17 +0000146 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
147 OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(AsmPrinterVariant,
148 *AsmInfo));
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000149 if (!IP) {
Chris Lattner665e9472009-12-22 06:56:51 +0000150 errs() << "error: no instruction printer for target " << Triple << '\n';
Sean Callananba847da2009-12-17 01:49:59 +0000151 return -1;
152 }
153
Chris Lattner665e9472009-12-22 06:56:51 +0000154 bool ErrorOccurred = false;
155
156 SourceMgr SM;
157 SM.AddNewSourceBuffer(&Buffer, SMLoc());
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000158
Sean Callananba847da2009-12-17 01:49:59 +0000159 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000160 ByteArrayTy ByteArray;
Chris Lattnercfc99a92010-04-09 04:24:20 +0000161 StringRef Str = Buffer.getBuffer();
Sean Callanan668b1542010-04-12 19:43:00 +0000162
163 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
Sean Callananba847da2009-12-17 01:49:59 +0000164
Chris Lattner222af462009-12-22 06:24:00 +0000165 if (!ByteArray.empty())
Dan Gohmand5826a32010-08-20 01:07:01 +0000166 ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM, Out);
Sean Callananba847da2009-12-17 01:49:59 +0000167
Chris Lattner665e9472009-12-22 06:56:51 +0000168 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000169}
Sean Callanan668b1542010-04-12 19:43:00 +0000170
171static int byteArrayReader(uint8_t *B, uint64_t A, void *Arg) {
172 ByteArrayTy &ByteArray = *((ByteArrayTy*)Arg);
173
174 if (A >= ByteArray.size())
175 return -1;
176
177 *B = ByteArray[A].first;
178
179 return 0;
180}
181
182static int verboseEvaluator(uint64_t *V, unsigned R, void *Arg) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000183 EDDisassembler &disassembler = *(EDDisassembler *)((void **)Arg)[0];
184 raw_ostream &Out = *(raw_ostream *)((void **)Arg)[1];
Sean Callanan668b1542010-04-12 19:43:00 +0000185
Chris Lattner847da552010-07-20 18:25:19 +0000186 if (const char *regName = disassembler.nameWithRegisterID(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000187 Out << "[" << regName << "/" << R << "]";
Chris Lattner847da552010-07-20 18:25:19 +0000188
189 if (disassembler.registerIsStackPointer(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000190 Out << "(sp)";
Chris Lattner847da552010-07-20 18:25:19 +0000191 if (disassembler.registerIsProgramCounter(R))
Dan Gohmand5826a32010-08-20 01:07:01 +0000192 Out << "(pc)";
Sean Callanan668b1542010-04-12 19:43:00 +0000193
194 *V = 0;
Sean Callanan668b1542010-04-12 19:43:00 +0000195 return 0;
196}
197
198int Disassembler::disassembleEnhanced(const std::string &TS,
Dan Gohmand5826a32010-08-20 01:07:01 +0000199 MemoryBuffer &Buffer,
200 raw_ostream &Out) {
Sean Callanan668b1542010-04-12 19:43:00 +0000201 ByteArrayTy ByteArray;
202 StringRef Str = Buffer.getBuffer();
203 SourceMgr SM;
204
205 SM.AddNewSourceBuffer(&Buffer, SMLoc());
206
207 if (ByteArrayFromString(ByteArray, Str, SM)) {
208 return -1;
209 }
210
Sean Callanan668b1542010-04-12 19:43:00 +0000211 Triple T(TS);
Chris Lattner847da552010-07-20 18:25:19 +0000212 EDDisassembler::AssemblySyntax AS;
Sean Callanan668b1542010-04-12 19:43:00 +0000213
214 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 }
227
Chris Lattner847da552010-07-20 18:25:19 +0000228 EDDisassembler::initialize();
229 EDDisassembler *disassembler =
230 EDDisassembler::getDisassembler(TS.c_str(), AS);
231
232 if (disassembler == 0) {
233 errs() << "error: couldn't get disassembler for " << TS << '\n';
Sean Callanan668b1542010-04-12 19:43:00 +0000234 return -1;
235 }
236
Chris Lattner847da552010-07-20 18:25:19 +0000237 EDInst *inst =
238 disassembler->createInst(byteArrayReader, 0, &ByteArray);
239
240 if (inst == 0) {
Sean Callanan668b1542010-04-12 19:43:00 +0000241 errs() << "error: Didn't get an instruction\n";
242 return -1;
243 }
244
Chris Lattner847da552010-07-20 18:25:19 +0000245 unsigned numTokens = inst->numTokens();
246 if ((int)numTokens < 0) {
247 errs() << "error: couldn't count the instruction's tokens\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000248 return -1;
249 }
250
Chris Lattner847da552010-07-20 18:25:19 +0000251 for (unsigned tokenIndex = 0; tokenIndex != numTokens; ++tokenIndex) {
252 EDToken *token;
Sean Callanan668b1542010-04-12 19:43:00 +0000253
Chris Lattner847da552010-07-20 18:25:19 +0000254 if (inst->getToken(token, tokenIndex)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000255 errs() << "error: Couldn't get token\n";
256 return -1;
257 }
258
259 const char *buf;
Chris Lattner847da552010-07-20 18:25:19 +0000260 if (token->getString(buf)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000261 errs() << "error: Couldn't get string for token\n";
262 return -1;
263 }
264
Dan Gohmand5826a32010-08-20 01:07:01 +0000265 Out << '[';
Chris Lattner847da552010-07-20 18:25:19 +0000266 int operandIndex = token->operandID();
Sean Callanan668b1542010-04-12 19:43:00 +0000267
268 if (operandIndex >= 0)
Dan Gohmand5826a32010-08-20 01:07:01 +0000269 Out << operandIndex << "-";
Sean Callanan668b1542010-04-12 19:43:00 +0000270
Chris Lattner847da552010-07-20 18:25:19 +0000271 switch (token->type()) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000272 default: Out << "?"; break;
273 case EDToken::kTokenWhitespace: Out << "w"; break;
274 case EDToken::kTokenPunctuation: Out << "p"; break;
275 case EDToken::kTokenOpcode: Out << "o"; break;
276 case EDToken::kTokenLiteral: Out << "l"; break;
277 case EDToken::kTokenRegister: Out << "r"; break;
Sean Callanan668b1542010-04-12 19:43:00 +0000278 }
279
Dan Gohmand5826a32010-08-20 01:07:01 +0000280 Out << ":" << buf;
Sean Callanan668b1542010-04-12 19:43:00 +0000281
Chris Lattner847da552010-07-20 18:25:19 +0000282 if (token->type() == EDToken::kTokenLiteral) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000283 Out << "=";
Chris Lattner847da552010-07-20 18:25:19 +0000284 if (token->literalSign())
Dan Gohmand5826a32010-08-20 01:07:01 +0000285 Out << "-";
Sean Callanan668b1542010-04-12 19:43:00 +0000286 uint64_t absoluteValue;
Chris Lattner847da552010-07-20 18:25:19 +0000287 if (token->literalAbsoluteValue(absoluteValue)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000288 errs() << "error: Couldn't get the value of a literal token\n";
289 return -1;
290 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000291 Out << absoluteValue;
Chris Lattner847da552010-07-20 18:25:19 +0000292 } else if (token->type() == EDToken::kTokenRegister) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000293 Out << "=";
Sean Callanan668b1542010-04-12 19:43:00 +0000294 unsigned regID;
Chris Lattner847da552010-07-20 18:25:19 +0000295 if (token->registerID(regID)) {
Sean Callanan668b1542010-04-12 19:43:00 +0000296 errs() << "error: Couldn't get the ID of a register token\n";
297 return -1;
298 }
Dan Gohmand5826a32010-08-20 01:07:01 +0000299 Out << "r" << regID;
Sean Callanan668b1542010-04-12 19:43:00 +0000300 }
301
Dan Gohmand5826a32010-08-20 01:07:01 +0000302 Out << "]";
Sean Callanan668b1542010-04-12 19:43:00 +0000303 }
304
Dan Gohmand5826a32010-08-20 01:07:01 +0000305 Out << " ";
Sean Callanan972bf8d2010-05-11 01:27:08 +0000306
Chris Lattner847da552010-07-20 18:25:19 +0000307 if (inst->isBranch())
Dan Gohmand5826a32010-08-20 01:07:01 +0000308 Out << "<br> ";
Chris Lattner847da552010-07-20 18:25:19 +0000309 if (inst->isMove())
Dan Gohmand5826a32010-08-20 01:07:01 +0000310 Out << "<mov> ";
Sean Callanan668b1542010-04-12 19:43:00 +0000311
Chris Lattner847da552010-07-20 18:25:19 +0000312 unsigned numOperands = inst->numOperands();
Sean Callanan668b1542010-04-12 19:43:00 +0000313
Chris Lattner847da552010-07-20 18:25:19 +0000314 if ((int)numOperands < 0) {
Sean Callanan668b1542010-04-12 19:43:00 +0000315 errs() << "error: Couldn't count operands\n";
316 return -1;
317 }
318
Chris Lattner847da552010-07-20 18:25:19 +0000319 for (unsigned operandIndex = 0; operandIndex != numOperands; ++operandIndex) {
Dan Gohmand5826a32010-08-20 01:07:01 +0000320 Out << operandIndex << ":";
Sean Callanan668b1542010-04-12 19:43:00 +0000321
Chris Lattner847da552010-07-20 18:25:19 +0000322 EDOperand *operand;
323 if (inst->getOperand(operand, operandIndex)) {
324 errs() << "error: couldn't get operand\n";
Sean Callanan668b1542010-04-12 19:43:00 +0000325 return -1;
326 }
327
328 uint64_t evaluatedResult;
Dan Gohmand5826a32010-08-20 01:07:01 +0000329 void *Arg[] = { disassembler, &Out };
330 evaluatedResult = operand->evaluate(evaluatedResult, verboseEvaluator, Arg);
331 Out << "=" << evaluatedResult << " ";
Sean Callanan668b1542010-04-12 19:43:00 +0000332 }
333
Dan Gohmand5826a32010-08-20 01:07:01 +0000334 Out << '\n';
Sean Callanan668b1542010-04-12 19:43:00 +0000335
336 return 0;
337}
338