blob: 9fe07909284c2e984cf0dcf4ea4951371db9b30a [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
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000050static bool PrintInsts(const MCDisassembler &DisAsm,
Chris Lattnerd3740872010-04-04 05:04:31 +000051 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
Sean Callanan2e235a82010-02-03 03:46:41 +000056 // Disassemble it to strings.
Chris Lattner665e9472009-12-22 06:56:51 +000057 uint64_t Size;
Sean Callanan2e235a82010-02-03 03:46:41 +000058 uint64_t Index;
Sean Callananba847da2009-12-17 01:49:59 +000059
Sean Callanan2e235a82010-02-03 03:46:41 +000060 for (Index = 0; Index < Bytes.size(); Index += Size) {
61 MCInst Inst;
62
63 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
64 /*REMOVE*/ nulls())) {
Chris Lattnerd3740872010-04-04 05:04:31 +000065 Printer.printInst(&Inst, outs());
Sean Callanan2e235a82010-02-03 03:46:41 +000066 outs() << "\n";
Chris Lattnercfc99a92010-04-09 04:24:20 +000067 }
68 else {
Sean Callanan2e235a82010-02-03 03:46:41 +000069 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
70 "invalid instruction encoding", "warning");
71 if (Size == 0)
72 Size = 1; // skip illegible bytes
73 }
Chris Lattner665e9472009-12-22 06:56:51 +000074 }
75
76 return false;
Sean Callananba847da2009-12-17 01:49:59 +000077}
78
Chris Lattnera3dcfb12009-12-22 22:50:29 +000079int Disassembler::disassemble(const Target &T, const std::string &Triple,
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000080 MemoryBuffer &Buffer) {
Chris Lattner222af462009-12-22 06:24:00 +000081 // Set up disassembler.
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000082 OwningPtr<const MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
Sean Callananba847da2009-12-17 01:49:59 +000083
Chris Lattner222af462009-12-22 06:24:00 +000084 if (!AsmInfo) {
85 errs() << "error: no assembly info for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000086 return -1;
87 }
88
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000089 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler());
Chris Lattner222af462009-12-22 06:24:00 +000090 if (!DisAsm) {
91 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callananba847da2009-12-17 01:49:59 +000092 return -1;
93 }
94
Chris Lattnerd3740872010-04-04 05:04:31 +000095 OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(0, *AsmInfo));
Daniel Dunbarc6ab1902010-03-20 22:36:35 +000096 if (!IP) {
Chris Lattner665e9472009-12-22 06:56:51 +000097 errs() << "error: no instruction printer for target " << Triple << '\n';
Sean Callananba847da2009-12-17 01:49:59 +000098 return -1;
99 }
100
Chris Lattner665e9472009-12-22 06:56:51 +0000101 bool ErrorOccurred = false;
102
103 SourceMgr SM;
104 SM.AddNewSourceBuffer(&Buffer, SMLoc());
Chris Lattnerc3de94f2009-12-22 06:45:48 +0000105
Sean Callananba847da2009-12-17 01:49:59 +0000106 // Convert the input to a vector for disassembly.
Chris Lattner665e9472009-12-22 06:56:51 +0000107 ByteArrayTy ByteArray;
Sean Callanan8c0a1602010-04-09 01:43:16 +0000108
Chris Lattnercfc99a92010-04-09 04:24:20 +0000109 StringRef Str = Buffer.getBuffer();
110 while (!Str.empty()) {
111 // Strip horizontal whitespace.
112 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
113 Str = Str.substr(Pos);
114 continue;
115 }
116
117 // If this is the end of a line or start of a comment, remove the rest of
118 // the line.
119 if (Str[0] == '\n' || Str[0] == '#') {
120 // Strip to the end of line if we already processed any bytes on this
121 // line. This strips the comment and/or the \n.
122 if (Str[0] == '\n')
123 Str = Str.substr(1);
124 else {
125 Str = Str.substr(Str.find_first_of('\n'));
126 if (!Str.empty())
127 Str = Str.substr(1);
128 }
129 continue;
130 }
131
132 // Get the current token.
133 size_t Next = Str.find_first_of(" \t\n\r#");
134 StringRef Value = Str.substr(0, Next);
135
136 // Convert to a byte and add to the byte vector.
137 unsigned ByteVal;
138 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
139 // If we have an error, print it and skip to the end of line.
140 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
141 "invalid input token", "error");
142 ErrorOccurred = true;
143 Str = Str.substr(Str.find('\n'));
144 ByteArray.clear();
145 continue;
146 }
147
148 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
149 Str = Str.substr(Next);
150 }
Sean Callananba847da2009-12-17 01:49:59 +0000151
Chris Lattner222af462009-12-22 06:24:00 +0000152 if (!ByteArray.empty())
Daniel Dunbarc6ab1902010-03-20 22:36:35 +0000153 ErrorOccurred |= PrintInsts(*DisAsm, *IP, ByteArray, SM);
Sean Callananba847da2009-12-17 01:49:59 +0000154
Chris Lattner665e9472009-12-22 06:56:51 +0000155 return ErrorOccurred;
Sean Callananba847da2009-12-17 01:49:59 +0000156}