blob: eafc2df322391b851187e250a986244b977ff4e4 [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 +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
60 std::string verboseOStr;
61 llvm::raw_string_ostream verboseOS(verboseOStr);
62
Chris Lattner665e9472009-12-22 06:56:51 +000063 if (!DisAsm.getInstruction(Inst, Size, memoryObject, 0, verboseOS)) {
64 // FIXME: Caret.
65 errs() << "error: invalid instruction\n";
66 errs() << "Diagnostic log:" << '\n';
67 errs() << verboseOS.str() << '\n';
68 return true;
Sean Callananba847da2009-12-17 01:49:59 +000069 }
Chris Lattner665e9472009-12-22 06:56:51 +000070
71 Printer.printInst(&Inst);
72 outs() << "\n";
73
74 // If the disassembled instruction was smaller than the number of bytes we
75 // read, reject the excess bytes.
76 if (Bytes.size() != Size) {
77 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Size].second),
78 "excess data detected in input", "error");
79 return true;
80 }
81
82 return false;
Sean Callananba847da2009-12-17 01:49:59 +000083}
84
Chris Lattner222af462009-12-22 06:24:00 +000085int HexDisassembler::disassemble(const Target &T, const std::string &Triple,
86 MemoryBuffer &Buffer) {
87 // Set up disassembler.
88 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +000089
Chris Lattner222af462009-12-22 06:24:00 +000090 if (!AsmInfo) {
91 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000092 return -1;
93 }
94
Chris Lattner222af462009-12-22 06:24:00 +000095 llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
96 if (!DisAsm) {
97 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000098 return -1;
99 }
100
Chris Lattner222af462009-12-22 06:24:00 +0000101 llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
Sean Callananba847da2009-12-17 01:49:59 +0000102
Chris Lattner222af462009-12-22 06:24:00 +0000103 if (!InstPrinter) {
Chris Lattner665e9472009-12-22 06:56:51 +0000104 errs() << "error: no instruction printer for target " << Triple << '\n';
Sean Callananba847da2009-12-17 01:49:59 +0000105 return -1;
106 }
107
Chris Lattner665e9472009-12-22 06:56:51 +0000108 bool ErrorOccurred = false;
109
110 SourceMgr SM;
111 SM.AddNewSourceBuffer(&Buffer, SMLoc());
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000112
Sean Callananba847da2009-12-17 01:49:59 +0000113 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000114 ByteArrayTy ByteArray;
Sean Callananba847da2009-12-17 01:49:59 +0000115
Chris Lattner222af462009-12-22 06:24:00 +0000116 StringRef Str = Buffer.getBuffer();
Chris Lattner222af462009-12-22 06:24:00 +0000117 while (!Str.empty()) {
Chris Lattner2adbef02009-12-22 06:37:58 +0000118 // Strip horizontal whitespace.
119 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
120 Str = Str.substr(Pos);
121 continue;
Sean Callananba847da2009-12-17 01:49:59 +0000122 }
123
Chris Lattner2adbef02009-12-22 06:37:58 +0000124 // If this is the end of a line or start of a comment, process the
125 // instruction we have so far.
126 if (Str[0] == '\n' || Str[0] == '#') {
127 // If we have bytes to process, do so.
128 if (!ByteArray.empty()) {
Chris Lattner665e9472009-12-22 06:56:51 +0000129 ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
Chris Lattner2adbef02009-12-22 06:37:58 +0000130 ByteArray.clear();
131 }
132
133 // Strip to the end of line if we already processed any bytes on this
134 // line. This strips the comment and/or the \n.
135 if (Str[0] == '\n')
136 Str = Str.substr(1);
137 else {
138 Str = Str.substr(Str.find_first_of('\n'));
139 if (!Str.empty())
140 Str = Str.substr(1);
141 }
142 continue;
143 }
Sean Callananba847da2009-12-17 01:49:59 +0000144
145 // Get the current token.
Chris Lattner2adbef02009-12-22 06:37:58 +0000146 size_t Next = Str.find_first_of(" \t\n\r#");
147 StringRef Value = Str.substr(0, Next);
Sean Callananba847da2009-12-17 01:49:59 +0000148
149 // Convert to a byte and add to the byte vector.
Chris Lattner222af462009-12-22 06:24:00 +0000150 unsigned ByteVal;
151 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000152 // If we have an error, print it and skip to the end of line.
Chris Lattner665e9472009-12-22 06:56:51 +0000153 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000154 "invalid input token", "error");
Chris Lattner665e9472009-12-22 06:56:51 +0000155 ErrorOccurred = true;
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000156 Str = Str.substr(Str.find('\n'));
157 ByteArray.clear();
158 continue;
Sean Callananba847da2009-12-17 01:49:59 +0000159 }
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000160
Chris Lattner665e9472009-12-22 06:56:51 +0000161 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
Chris Lattner222af462009-12-22 06:24:00 +0000162 Str = Str.substr(Next);
Sean Callananba847da2009-12-17 01:49:59 +0000163 }
164
Chris Lattner222af462009-12-22 06:24:00 +0000165 if (!ByteArray.empty())
Chris Lattner665e9472009-12-22 06:56:51 +0000166 ErrorOccurred |= PrintInst(*DisAsm, *InstPrinter, ByteArray, SM);
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}