blob: 5fde71238dc93bcd9a2647541aa2ae2e7e769c39 [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"
Sean Callananba847da2009-12-17 01:49:59 +000016
17#include "llvm/ADT/OwningPtr.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCDisassembler.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstPrinter.h"
22#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/MemoryObject.h"
25#include "llvm/Support/raw_ostream.h"
Chris Lattnerc3de94f2009-12-22 06:45:48 +000026#include "llvm/Support/SourceMgr.h"
Sean Callananba847da2009-12-17 01:49:59 +000027using namespace llvm;
28
Chris Lattner665e9472009-12-22 06:56:51 +000029typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
30
31namespace {
Sean Callananba847da2009-12-17 01:49:59 +000032class VectorMemoryObject : public MemoryObject {
33private:
Chris Lattner665e9472009-12-22 06:56:51 +000034 const ByteArrayTy &Bytes;
Sean Callananba847da2009-12-17 01:49:59 +000035public:
Chris Lattner665e9472009-12-22 06:56:51 +000036 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Sean Callananba847da2009-12-17 01:49:59 +000037
Chris Lattner665e9472009-12-22 06:56:51 +000038 uint64_t getBase() const { return 0; }
39 uint64_t getExtent() const { return Bytes.size(); }
Sean Callananba847da2009-12-17 01:49:59 +000040
Chris Lattner665e9472009-12-22 06:56:51 +000041 int readByte(uint64_t Addr, uint8_t *Byte) const {
42 if (Addr > getExtent())
Sean Callananba847da2009-12-17 01:49:59 +000043 return -1;
Chris Lattner665e9472009-12-22 06:56:51 +000044 *Byte = Bytes[Addr].first;
Sean Callananba847da2009-12-17 01:49:59 +000045 return 0;
46 }
47};
Chris Lattner665e9472009-12-22 06:56:51 +000048}
Sean Callananba847da2009-12-17 01:49:59 +000049
Chris Lattner665e9472009-12-22 06:56:51 +000050static bool PrintInst(const llvm::MCDisassembler &DisAsm,
51 llvm::MCInstPrinter &Printer, const ByteArrayTy &Bytes,
52 SourceMgr &SM) {
Sean Callananba847da2009-12-17 01:49:59 +000053 // Wrap the vector in a MemoryObject.
Chris Lattner665e9472009-12-22 06:56:51 +000054 VectorMemoryObject memoryObject(Bytes);
Sean Callananba847da2009-12-17 01:49:59 +000055
Chris Lattner665e9472009-12-22 06:56:51 +000056 // Disassemble it to a string and get the size of the instruction.
57 MCInst Inst;
58 uint64_t Size;
Sean Callananba847da2009-12-17 01:49:59 +000059
Chris Lattnerf444c062009-12-22 22:47:43 +000060 if (!DisAsm.getInstruction(Inst, Size, memoryObject, 0,
61 /*REMOVE*/ nulls())) {
Chris Lattner5a996152009-12-22 06:58:29 +000062 SM.PrintMessage(SMLoc::getFromPointer(Bytes[0].second),
Chris Lattnera1aa8bb2009-12-22 07:03:21 +000063 "invalid instruction encoding", "error");
Chris Lattner665e9472009-12-22 06:56:51 +000064 return true;
Sean Callananba847da2009-12-17 01:49:59 +000065 }
Chris Lattner665e9472009-12-22 06:56:51 +000066
67 Printer.printInst(&Inst);
68 outs() << "\n";
69
70 // If the disassembled instruction was smaller than the number of bytes we
71 // read, reject the excess bytes.
72 if (Bytes.size() != Size) {
73 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Size].second),
74 "excess data detected in input", "error");
75 return true;
76 }
77
78 return false;
Sean Callananba847da2009-12-17 01:49:59 +000079}
80
Chris Lattnera3dcfb12009-12-22 22:50:29 +000081int Disassembler::disassemble(const Target &T, const std::string &Triple,
Chris Lattner222af462009-12-22 06:24:00 +000082 MemoryBuffer &Buffer) {
83 // Set up disassembler.
84 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +000085
Chris Lattner222af462009-12-22 06:24:00 +000086 if (!AsmInfo) {
87 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000088 return -1;
89 }
90
Chris Lattner222af462009-12-22 06:24:00 +000091 llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
92 if (!DisAsm) {
93 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000094 return -1;
95 }
96
Chris Lattner222af462009-12-22 06:24:00 +000097 llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
Sean Callananba847da2009-12-17 01:49:59 +000098
Chris Lattner222af462009-12-22 06:24:00 +000099 if (!InstPrinter) {
Chris Lattner665e9472009-12-22 06:56:51 +0000100 errs() << "error: no instruction printer for target " << Triple << '\n';
Sean Callananba847da2009-12-17 01:49:59 +0000101 return -1;
102 }
103
Chris Lattner665e9472009-12-22 06:56:51 +0000104 bool ErrorOccurred = false;
105
106 SourceMgr SM;
107 SM.AddNewSourceBuffer(&Buffer, SMLoc());
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000108
Sean Callananba847da2009-12-17 01:49:59 +0000109 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000110 ByteArrayTy ByteArray;
Sean Callananba847da2009-12-17 01:49:59 +0000111
Chris Lattner222af462009-12-22 06:24:00 +0000112 StringRef Str = Buffer.getBuffer();
Chris Lattner222af462009-12-22 06:24:00 +0000113 while (!Str.empty()) {
Chris Lattner2adbef02009-12-22 06:37:58 +0000114 // Strip horizontal whitespace.
115 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
116 Str = Str.substr(Pos);
117 continue;
Sean Callananba847da2009-12-17 01:49:59 +0000118 }
119
Chris Lattner2adbef02009-12-22 06:37:58 +0000120 // If this is the end of a line or start of a comment, process the
121 // instruction we have so far.
122 if (Str[0] == '\n' || Str[0] == '#') {
123 // If we have bytes to process, do so.
124 if (!ByteArray.empty()) {
Chris Lattner665e9472009-12-22 06:56:51 +0000125 ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
Chris Lattner2adbef02009-12-22 06:37:58 +0000126 ByteArray.clear();
127 }
128
129 // Strip to the end of line if we already processed any bytes on this
130 // line. This strips the comment and/or the \n.
131 if (Str[0] == '\n')
132 Str = Str.substr(1);
133 else {
134 Str = Str.substr(Str.find_first_of('\n'));
135 if (!Str.empty())
136 Str = Str.substr(1);
137 }
138 continue;
139 }
Sean Callananba847da2009-12-17 01:49:59 +0000140
141 // Get the current token.
Chris Lattner2adbef02009-12-22 06:37:58 +0000142 size_t Next = Str.find_first_of(" \t\n\r#");
143 StringRef Value = Str.substr(0, Next);
Sean Callananba847da2009-12-17 01:49:59 +0000144
145 // Convert to a byte and add to the byte vector.
Chris Lattner222af462009-12-22 06:24:00 +0000146 unsigned ByteVal;
147 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000148 // If we have an error, print it and skip to the end of line.
Chris Lattner665e9472009-12-22 06:56:51 +0000149 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000150 "invalid input token", "error");
Chris Lattner665e9472009-12-22 06:56:51 +0000151 ErrorOccurred = true;
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000152 Str = Str.substr(Str.find('\n'));
153 ByteArray.clear();
154 continue;
Sean Callananba847da2009-12-17 01:49:59 +0000155 }
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000156
Chris Lattner665e9472009-12-22 06:56:51 +0000157 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
Chris Lattner222af462009-12-22 06:24:00 +0000158 Str = Str.substr(Next);
Sean Callananba847da2009-12-17 01:49:59 +0000159 }
160
Chris Lattner222af462009-12-22 06:24:00 +0000161 if (!ByteArray.empty())
Chris Lattner665e9472009-12-22 06:56:51 +0000162 ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
Sean Callananba847da2009-12-17 01:49:59 +0000163
Chris Lattner665e9472009-12-22 06:56:51 +0000164 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000165}