blob: fe9a67303116b7182b0de96a23dcedd6959de198 [file] [log] [blame]
Sean Callananba847da2009-12-17 01:49:59 +00001//===- HexDisassembler.cpp - Disassembler for hex strings -----------------===//
2//
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
15#include "HexDisassembler.h"
16
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 +000027
28using namespace llvm;
29
30class VectorMemoryObject : public MemoryObject {
31private:
32 const std::vector<unsigned char> &Bytes;
33public:
34 VectorMemoryObject(const std::vector<unsigned char> &bytes) :
35 Bytes(bytes) {
36 }
37
38 uint64_t getBase() const {
39 return 0;
40 }
41
42 uint64_t getExtent() const {
43 return Bytes.size();
44 }
45
46 int readByte(uint64_t addr, uint8_t *byte) const {
47 if (addr > getExtent())
48 return -1;
Chris Lattnerc3de94f2009-12-22 06:45:48 +000049 *byte = Bytes[addr];
Sean Callananba847da2009-12-17 01:49:59 +000050 return 0;
51 }
52};
53
54void printInst(const llvm::MCDisassembler &disassembler,
55 llvm::MCInstPrinter &instPrinter,
56 const std::vector<unsigned char> &bytes) {
57 // Wrap the vector in a MemoryObject.
Sean Callananba847da2009-12-17 01:49:59 +000058 VectorMemoryObject memoryObject(bytes);
59
60 // Disassemble it.
Sean Callananba847da2009-12-17 01:49:59 +000061 MCInst inst;
62 uint64_t size;
63
64 std::string verboseOStr;
65 llvm::raw_string_ostream verboseOS(verboseOStr);
66
Chris Lattnerc3de94f2009-12-22 06:45:48 +000067 if (disassembler.getInstruction(inst, size, memoryObject, 0, verboseOS)) {
Sean Callananba847da2009-12-17 01:49:59 +000068 instPrinter.printInst(&inst);
69 outs() << "\n";
Chris Lattnerc3de94f2009-12-22 06:45:48 +000070 } else {
Sean Callananba847da2009-12-17 01:49:59 +000071 errs() << "error: invalid instruction" << "\n";
72 errs() << "Diagnostic log:" << "\n";
73 errs() << verboseOStr.c_str() << "\n";
74 }
75}
76
Chris Lattner222af462009-12-22 06:24:00 +000077int HexDisassembler::disassemble(const Target &T, const std::string &Triple,
78 MemoryBuffer &Buffer) {
79 // Set up disassembler.
80 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +000081
Chris Lattner222af462009-12-22 06:24:00 +000082 if (!AsmInfo) {
83 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000084 return -1;
85 }
86
Chris Lattner222af462009-12-22 06:24:00 +000087 llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
88 if (!DisAsm) {
89 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000090 return -1;
91 }
92
Chris Lattner222af462009-12-22 06:24:00 +000093 llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
Sean Callananba847da2009-12-17 01:49:59 +000094
Chris Lattner222af462009-12-22 06:24:00 +000095 if (!InstPrinter) {
96 errs() << "error: no instruction printer for target " << Triple
Sean Callananba847da2009-12-17 01:49:59 +000097 << "\n";
98 return -1;
99 }
100
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000101 SourceMgr SourceManager;
102 SourceManager.AddNewSourceBuffer(&Buffer, SMLoc());
103
Sean Callananba847da2009-12-17 01:49:59 +0000104 // Convert the input to a vector for disassembly.
Chris Lattner222af462009-12-22 06:24:00 +0000105 std::vector<unsigned char> ByteArray;
Sean Callananba847da2009-12-17 01:49:59 +0000106
Chris Lattner222af462009-12-22 06:24:00 +0000107 StringRef Str = Buffer.getBuffer();
Chris Lattner222af462009-12-22 06:24:00 +0000108 while (!Str.empty()) {
Chris Lattner2adbef02009-12-22 06:37:58 +0000109 // Strip horizontal whitespace.
110 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
111 Str = Str.substr(Pos);
112 continue;
Sean Callananba847da2009-12-17 01:49:59 +0000113 }
114
Chris Lattner2adbef02009-12-22 06:37:58 +0000115 // If this is the end of a line or start of a comment, process the
116 // instruction we have so far.
117 if (Str[0] == '\n' || Str[0] == '#') {
118 // If we have bytes to process, do so.
119 if (!ByteArray.empty()) {
120 printInst(*DisAsm, *InstPrinter, ByteArray);
121 ByteArray.clear();
122 }
123
124 // Strip to the end of line if we already processed any bytes on this
125 // line. This strips the comment and/or the \n.
126 if (Str[0] == '\n')
127 Str = Str.substr(1);
128 else {
129 Str = Str.substr(Str.find_first_of('\n'));
130 if (!Str.empty())
131 Str = Str.substr(1);
132 }
133 continue;
134 }
Sean Callananba847da2009-12-17 01:49:59 +0000135
136 // Get the current token.
Chris Lattner2adbef02009-12-22 06:37:58 +0000137 size_t Next = Str.find_first_of(" \t\n\r#");
138 StringRef Value = Str.substr(0, Next);
Sean Callananba847da2009-12-17 01:49:59 +0000139
140 // Convert to a byte and add to the byte vector.
Chris Lattner222af462009-12-22 06:24:00 +0000141 unsigned ByteVal;
142 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000143 // If we have an error, print it and skip to the end of line.
144 SourceManager.PrintMessage(SMLoc::getFromPointer(Value.data()),
145 "invalid input token", "error");
146 Str = Str.substr(Str.find('\n'));
147 ByteArray.clear();
148 continue;
Sean Callananba847da2009-12-17 01:49:59 +0000149 }
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000150
151 ByteArray.push_back((unsigned char)ByteVal);
Chris Lattner222af462009-12-22 06:24:00 +0000152 Str = Str.substr(Next);
Sean Callananba847da2009-12-17 01:49:59 +0000153 }
154
Chris Lattner222af462009-12-22 06:24:00 +0000155 if (!ByteArray.empty())
156 printInst(*DisAsm, *InstPrinter, ByteArray);
Sean Callananba847da2009-12-17 01:49:59 +0000157
158 return 0;
159}