blob: 06c7721d7e8ca251b8821eaf62531c71767118a2 [file] [log] [blame]
Chris Lattnerb257d242009-12-22 22:50:29 +00001//===- Disassembler.cpp - Disassembler for hex strings --------------------===//
Sean Callanan7e645502009-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 Lattnerb257d242009-12-22 22:50:29 +000015#include "Disassembler.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/OwningPtr.h"
17#include "llvm/ADT/Triple.h"
Sean Callanan7e645502009-12-17 01:49:59 +000018#include "llvm/MC/MCDisassembler.h"
19#include "llvm/MC/MCInst.h"
Richard Bartondef81b92012-04-16 11:32:10 +000020#include "llvm/MC/MCStreamer.h"
James Molloy4c493e82011-09-07 17:24:38 +000021#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanan7e645502009-12-17 01:49:59 +000022#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/MemoryObject.h"
Chris Lattner88799182009-12-22 06:45:48 +000024#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
26#include "llvm/Support/raw_ostream.h"
Richard Bartondef81b92012-04-16 11:32:10 +000027
Sean Callanan7e645502009-12-17 01:49:59 +000028using namespace llvm;
29
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000030typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
31
32namespace {
Sean Callanan7e645502009-12-17 01:49:59 +000033class VectorMemoryObject : public MemoryObject {
34private:
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000035 const ByteArrayTy &Bytes;
Sean Callanan7e645502009-12-17 01:49:59 +000036public:
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000037 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
Jim Grosbach112a2de2011-05-09 20:05:25 +000038
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000039 uint64_t getBase() const { return 0; }
Derek Schuff56b662c2012-02-29 01:09:06 +000040 uint64_t getExtent() const { return Bytes.size(); }
Sean Callanan7e645502009-12-17 01:49:59 +000041
Derek Schuff56b662c2012-02-29 01:09:06 +000042 int readByte(uint64_t Addr, uint8_t *Byte) const {
Rafael Espindola9f9a1062011-01-06 16:48:42 +000043 if (Addr >= getExtent())
Sean Callanan7e645502009-12-17 01:49:59 +000044 return -1;
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000045 *Byte = Bytes[Addr].first;
Sean Callanan7e645502009-12-17 01:49:59 +000046 return 0;
47 }
48};
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000049}
Sean Callanan7e645502009-12-17 01:49:59 +000050
Daniel Dunbard9d5b312010-03-20 22:36:35 +000051static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartondef81b92012-04-16 11:32:10 +000052 const ByteArrayTy &Bytes,
53 SourceMgr &SM, raw_ostream &Out,
54 MCStreamer &Streamer) {
Sean Callanan7e645502009-12-17 01:49:59 +000055 // Wrap the vector in a MemoryObject.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000056 VectorMemoryObject memoryObject(Bytes);
Jim Grosbach112a2de2011-05-09 20:05:25 +000057
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000058 // Disassemble it to strings.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000059 uint64_t Size;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000060 uint64_t Index;
Jim Grosbach112a2de2011-05-09 20:05:25 +000061
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000062 for (Index = 0; Index < Bytes.size(); Index += Size) {
63 MCInst Inst;
Jim Grosbach112a2de2011-05-09 20:05:25 +000064
Owen Andersona4043c42011-08-17 17:44:15 +000065 MCDisassembler::DecodeStatus S;
66 S = DisAsm.getInstruction(Inst, Size, memoryObject, Index,
Owen Andersona0c3b972011-09-15 23:38:46 +000067 /*REMOVE*/ nulls(), nulls());
Owen Andersona4043c42011-08-17 17:44:15 +000068 switch (S) {
69 case MCDisassembler::Fail:
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000070 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner03b80a42011-10-16 05:43:57 +000071 SourceMgr::DK_Warning,
72 "invalid instruction encoding");
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000073 if (Size == 0)
74 Size = 1; // skip illegible bytes
Owen Andersona4043c42011-08-17 17:44:15 +000075 break;
76
77 case MCDisassembler::SoftFail:
78 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
Chris Lattner03b80a42011-10-16 05:43:57 +000079 SourceMgr::DK_Warning,
80 "potentially undefined instruction encoding");
Owen Andersona4043c42011-08-17 17:44:15 +000081 // Fall through
82
83 case MCDisassembler::Success:
Richard Bartondef81b92012-04-16 11:32:10 +000084 Streamer.EmitInstruction(Inst);
Owen Andersona4043c42011-08-17 17:44:15 +000085 break;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000086 }
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000087 }
Jim Grosbach112a2de2011-05-09 20:05:25 +000088
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000089 return false;
Sean Callanan7e645502009-12-17 01:49:59 +000090}
91
Jim Grosbach112a2de2011-05-09 20:05:25 +000092static bool ByteArrayFromString(ByteArrayTy &ByteArray,
93 StringRef &Str,
Sean Callanan2d03d3a2010-04-12 19:43:00 +000094 SourceMgr &SM) {
95 while (!Str.empty()) {
96 // Strip horizontal whitespace.
97 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
98 Str = Str.substr(Pos);
99 continue;
100 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000101
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000102 // If this is the end of a line or start of a comment, remove the rest of
103 // the line.
104 if (Str[0] == '\n' || Str[0] == '#') {
105 // Strip to the end of line if we already processed any bytes on this
106 // line. This strips the comment and/or the \n.
107 if (Str[0] == '\n') {
108 Str = Str.substr(1);
109 } else {
110 Str = Str.substr(Str.find_first_of('\n'));
111 if (!Str.empty())
112 Str = Str.substr(1);
113 }
114 continue;
115 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000116
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000117 // Get the current token.
118 size_t Next = Str.find_first_of(" \t\n\r#");
119 StringRef Value = Str.substr(0, Next);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000120
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000121 // Convert to a byte and add to the byte vector.
122 unsigned ByteVal;
123 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
124 // If we have an error, print it and skip to the end of line.
Chris Lattner03b80a42011-10-16 05:43:57 +0000125 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
126 "invalid input token");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000127 Str = Str.substr(Str.find('\n'));
128 ByteArray.clear();
129 continue;
130 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000131
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000132 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
133 Str = Str.substr(Next);
134 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000135
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000136 return false;
137}
138
Evan Chengab37af92011-07-06 19:45:42 +0000139int Disassembler::disassemble(const Target &T,
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000140 const std::string &Triple,
Richard Bartondef81b92012-04-16 11:32:10 +0000141 MCSubtargetInfo &STI,
142 MCStreamer &Streamer,
Dan Gohman268b0f42010-08-20 01:07:01 +0000143 MemoryBuffer &Buffer,
Richard Bartondef81b92012-04-16 11:32:10 +0000144 SourceMgr &SM,
Dan Gohman268b0f42010-08-20 01:07:01 +0000145 raw_ostream &Out) {
Richard Bartondef81b92012-04-16 11:32:10 +0000146 OwningPtr<const MCDisassembler> DisAsm(T.createMCDisassembler(STI));
Chris Lattner72cbaa22009-12-22 06:24:00 +0000147 if (!DisAsm) {
148 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callanan7e645502009-12-17 01:49:59 +0000149 return -1;
150 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000151
Richard Bartondef81b92012-04-16 11:32:10 +0000152 // Set up initial section manually here
153 Streamer.InitSections();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000154
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000155 bool ErrorOccurred = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000156
Sean Callanan7e645502009-12-17 01:49:59 +0000157 // Convert the input to a vector for disassembly.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000158 ByteArrayTy ByteArray;
Chris Lattner55ddc302010-04-09 04:24:20 +0000159 StringRef Str = Buffer.getBuffer();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000160
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000161 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000162
Chris Lattner72cbaa22009-12-22 06:24:00 +0000163 if (!ByteArray.empty())
Richard Bartondef81b92012-04-16 11:32:10 +0000164 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000165
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000166 return ErrorOccurred;
Sean Callanan7e645502009-12-17 01:49:59 +0000167}