blob: 8185947fc5e573d76bb9b7ba552a3fd264ffbb6b [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/Triple.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000017#include "llvm/MC/MCAsmInfo.h"
18#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000019#include "llvm/MC/MCDisassembler/MCDisassembler.h"
Sean Callanan7e645502009-12-17 01:49:59 +000020#include "llvm/MC/MCInst.h"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000021#include "llvm/MC/MCRegisterInfo.h"
Richard Bartondef81b92012-04-16 11:32:10 +000022#include "llvm/MC/MCStreamer.h"
James Molloy4c493e82011-09-07 17:24:38 +000023#include "llvm/MC/MCSubtargetInfo.h"
Sean Callanan7e645502009-12-17 01:49:59 +000024#include "llvm/Support/MemoryBuffer.h"
Chris Lattner88799182009-12-22 06:45:48 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Support/raw_ostream.h"
Richard Bartondef81b92012-04-16 11:32:10 +000028
Sean Callanan7e645502009-12-17 01:49:59 +000029using namespace llvm;
30
Rafael Espindola45a753a2014-11-07 17:59:05 +000031typedef std::pair<std::vector<unsigned char>, std::vector<const char *>>
32 ByteArrayTy;
Sean Callanan7e645502009-12-17 01:49:59 +000033
Daniel Dunbard9d5b312010-03-20 22:36:35 +000034static bool PrintInsts(const MCDisassembler &DisAsm,
Richard Bartondef81b92012-04-16 11:32:10 +000035 const ByteArrayTy &Bytes,
36 SourceMgr &SM, raw_ostream &Out,
David Woodhousee6c13e42014-01-28 23:12:42 +000037 MCStreamer &Streamer, bool InAtomicBlock,
38 const MCSubtargetInfo &STI) {
Rafael Espindola7fc5b872014-11-12 02:04:27 +000039 ArrayRef<uint8_t> Data(Bytes.first.data(), Bytes.first.size());
Jim Grosbach112a2de2011-05-09 20:05:25 +000040
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000041 // Disassemble it to strings.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000042 uint64_t Size;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000043 uint64_t Index;
Jim Grosbach112a2de2011-05-09 20:05:25 +000044
Rafael Espindola45a753a2014-11-07 17:59:05 +000045 for (Index = 0; Index < Bytes.first.size(); Index += Size) {
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000046 MCInst Inst;
Jim Grosbach112a2de2011-05-09 20:05:25 +000047
Owen Andersona4043c42011-08-17 17:44:15 +000048 MCDisassembler::DecodeStatus S;
Rafael Espindola7fc5b872014-11-12 02:04:27 +000049 S = DisAsm.getInstruction(Inst, Size, Data.slice(Index), Index,
Owen Andersona0c3b972011-09-15 23:38:46 +000050 /*REMOVE*/ nulls(), nulls());
Owen Andersona4043c42011-08-17 17:44:15 +000051 switch (S) {
52 case MCDisassembler::Fail:
Rafael Espindola45a753a2014-11-07 17:59:05 +000053 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
Chris Lattner03b80a42011-10-16 05:43:57 +000054 SourceMgr::DK_Warning,
55 "invalid instruction encoding");
Tim Northover48cf6cc2013-07-19 10:05:04 +000056 // Don't try to resynchronise the stream in a block
57 if (InAtomicBlock)
58 return true;
59
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000060 if (Size == 0)
61 Size = 1; // skip illegible bytes
Tim Northover48cf6cc2013-07-19 10:05:04 +000062
Owen Andersona4043c42011-08-17 17:44:15 +000063 break;
64
65 case MCDisassembler::SoftFail:
Rafael Espindola45a753a2014-11-07 17:59:05 +000066 SM.PrintMessage(SMLoc::getFromPointer(Bytes.second[Index]),
Chris Lattner03b80a42011-10-16 05:43:57 +000067 SourceMgr::DK_Warning,
68 "potentially undefined instruction encoding");
Owen Andersona4043c42011-08-17 17:44:15 +000069 // Fall through
70
71 case MCDisassembler::Success:
David Woodhousee6c13e42014-01-28 23:12:42 +000072 Streamer.EmitInstruction(Inst, STI);
Owen Andersona4043c42011-08-17 17:44:15 +000073 break;
Sean Callanan6a6f9cc2010-02-03 03:46:41 +000074 }
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000075 }
Jim Grosbach112a2de2011-05-09 20:05:25 +000076
Chris Lattnerdd0c01b2009-12-22 06:56:51 +000077 return false;
Sean Callanan7e645502009-12-17 01:49:59 +000078}
79
Tim Northover48cf6cc2013-07-19 10:05:04 +000080static bool SkipToToken(StringRef &Str) {
Colin LeMahieueb4675f2014-11-11 21:03:09 +000081 for (;;) {
82 if (Str.empty())
83 return false;
Jim Grosbach112a2de2011-05-09 20:05:25 +000084
Colin LeMahieueb4675f2014-11-11 21:03:09 +000085 // Strip horizontal whitespace and commas.
86 if (size_t Pos = Str.find_first_not_of(" \t\r\n,")) {
87 Str = Str.substr(Pos);
Sean Callanan2d03d3a2010-04-12 19:43:00 +000088 continue;
89 }
Tim Northover48cf6cc2013-07-19 10:05:04 +000090
Colin LeMahieueb4675f2014-11-11 21:03:09 +000091 // If this is the start of a comment, remove the rest of the line.
92 if (Str[0] == '#') {
93 Str = Str.substr(Str.find_first_of('\n'));
94 continue;
95 }
96 return true;
97 }
Tim Northover48cf6cc2013-07-19 10:05:04 +000098}
99
100
101static bool ByteArrayFromString(ByteArrayTy &ByteArray,
102 StringRef &Str,
103 SourceMgr &SM) {
104 while (SkipToToken(Str)) {
105 // Handled by higher level
106 if (Str[0] == '[' || Str[0] == ']')
107 return false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000108
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000109 // Get the current token.
Tim Northover48cf6cc2013-07-19 10:05:04 +0000110 size_t Next = Str.find_first_of(" \t\n\r,#[]");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000111 StringRef Value = Str.substr(0, Next);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000112
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000113 // Convert to a byte and add to the byte vector.
114 unsigned ByteVal;
115 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
116 // If we have an error, print it and skip to the end of line.
Chris Lattner03b80a42011-10-16 05:43:57 +0000117 SM.PrintMessage(SMLoc::getFromPointer(Value.data()), SourceMgr::DK_Error,
118 "invalid input token");
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000119 Str = Str.substr(Str.find('\n'));
Rafael Espindola45a753a2014-11-07 17:59:05 +0000120 ByteArray.first.clear();
121 ByteArray.second.clear();
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000122 continue;
123 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000124
Rafael Espindola45a753a2014-11-07 17:59:05 +0000125 ByteArray.first.push_back(ByteVal);
126 ByteArray.second.push_back(Value.data());
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000127 Str = Str.substr(Next);
128 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000129
Sean Callanan2d03d3a2010-04-12 19:43:00 +0000130 return false;
131}
132
Evan Chengab37af92011-07-06 19:45:42 +0000133int Disassembler::disassemble(const Target &T,
Bill Wendling00f0cdd2011-03-21 04:13:46 +0000134 const std::string &Triple,
Richard Bartondef81b92012-04-16 11:32:10 +0000135 MCSubtargetInfo &STI,
136 MCStreamer &Streamer,
Dan Gohman268b0f42010-08-20 01:07:01 +0000137 MemoryBuffer &Buffer,
Richard Bartondef81b92012-04-16 11:32:10 +0000138 SourceMgr &SM,
Dan Gohman268b0f42010-08-20 01:07:01 +0000139 raw_ostream &Out) {
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000140
141 std::unique_ptr<const MCRegisterInfo> MRI(T.createMCRegInfo(Triple));
142 if (!MRI) {
143 errs() << "error: no register info for target " << Triple << "\n";
144 return -1;
145 }
146
147 std::unique_ptr<const MCAsmInfo> MAI(T.createMCAsmInfo(*MRI, Triple));
148 if (!MAI) {
149 errs() << "error: no assembly info for target " << Triple << "\n";
150 return -1;
151 }
152
153 // Set up the MCContext for creating symbols and MCExpr's.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000154 MCContext Ctx(MAI.get(), MRI.get(), nullptr);
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000155
156 std::unique_ptr<const MCDisassembler> DisAsm(
157 T.createMCDisassembler(STI, Ctx));
Chris Lattner72cbaa22009-12-22 06:24:00 +0000158 if (!DisAsm) {
159 errs() << "error: no disassembler for target " << Triple << "\n";
Sean Callanan7e645502009-12-17 01:49:59 +0000160 return -1;
161 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000162
Richard Bartondef81b92012-04-16 11:32:10 +0000163 // Set up initial section manually here
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000164 Streamer.InitSections(false);
Jim Grosbach112a2de2011-05-09 20:05:25 +0000165
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000166 bool ErrorOccurred = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000167
Sean Callanan7e645502009-12-17 01:49:59 +0000168 // Convert the input to a vector for disassembly.
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000169 ByteArrayTy ByteArray;
Chris Lattner55ddc302010-04-09 04:24:20 +0000170 StringRef Str = Buffer.getBuffer();
Tim Northover48cf6cc2013-07-19 10:05:04 +0000171 bool InAtomicBlock = false;
Jim Grosbach112a2de2011-05-09 20:05:25 +0000172
Tim Northover48cf6cc2013-07-19 10:05:04 +0000173 while (SkipToToken(Str)) {
Rafael Espindola45a753a2014-11-07 17:59:05 +0000174 ByteArray.first.clear();
175 ByteArray.second.clear();
Jim Grosbach112a2de2011-05-09 20:05:25 +0000176
Tim Northover48cf6cc2013-07-19 10:05:04 +0000177 if (Str[0] == '[') {
178 if (InAtomicBlock) {
179 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
180 "nested atomic blocks make no sense");
181 ErrorOccurred = true;
182 }
183 InAtomicBlock = true;
184 Str = Str.drop_front();
185 continue;
186 } else if (Str[0] == ']') {
187 if (!InAtomicBlock) {
188 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
189 "attempt to close atomic block without opening");
190 ErrorOccurred = true;
191 }
192 InAtomicBlock = false;
193 Str = Str.drop_front();
194 continue;
195 }
196
197 // It's a real token, get the bytes and emit them
198 ErrorOccurred |= ByteArrayFromString(ByteArray, Str, SM);
199
Rafael Espindola45a753a2014-11-07 17:59:05 +0000200 if (!ByteArray.first.empty())
Tim Northover48cf6cc2013-07-19 10:05:04 +0000201 ErrorOccurred |= PrintInsts(*DisAsm, ByteArray, SM, Out, Streamer,
David Woodhousee6c13e42014-01-28 23:12:42 +0000202 InAtomicBlock, STI);
Tim Northover48cf6cc2013-07-19 10:05:04 +0000203 }
204
205 if (InAtomicBlock) {
206 SM.PrintMessage(SMLoc::getFromPointer(Str.data()), SourceMgr::DK_Error,
207 "unclosed atomic block");
208 ErrorOccurred = true;
209 }
Jim Grosbach112a2de2011-05-09 20:05:25 +0000210
Chris Lattnerdd0c01b2009-12-22 06:56:51 +0000211 return ErrorOccurred;
Sean Callanan7e645502009-12-17 01:49:59 +0000212}