blob: c80a167ace71434ea011637af7eae1700fa1dfcd [file] [log] [blame]
Nick Lewycky2e2d75f2011-09-01 21:09:04 +00001//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
Kevin Enderbyf3070dc2011-03-28 18:25:07 +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//===----------------------------------------------------------------------===//
Chris Lattner0388fb02011-05-22 04:52:24 +00009
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000010#include "Disassembler.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000011#include "llvm-c/Disassembler.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000012#include "llvm/MC/MCAsmInfo.h"
Evan Cheng345b6b42011-07-20 06:54:19 +000013#include "llvm/MC/MCContext.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000014#include "llvm/MC/MCDisassembler.h"
15#include "llvm/MC/MCInst.h"
16#include "llvm/MC/MCInstPrinter.h"
Sean Callanane804b5b2012-04-06 18:21:09 +000017#include "llvm/MC/MCInstrInfo.h"
Evan Cheng345b6b42011-07-20 06:54:19 +000018#include "llvm/MC/MCRegisterInfo.h"
Sean Callanane804b5b2012-04-06 18:21:09 +000019#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Support/ErrorHandling.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000021#include "llvm/Support/MemoryObject.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000023
24namespace llvm {
25class Target;
26} // namespace llvm
27using namespace llvm;
28
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000029// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
30// disassembly is supported by passing a block of information in the DisInfo
Chris Lattner0388fb02011-05-22 04:52:24 +000031// parameter and specifying the TagType and callback functions as described in
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000032// the header llvm-c/Disassembler.h . The pointer to the block and the
Chris Lattner0388fb02011-05-22 04:52:24 +000033// functions can all be passed as NULL. If successful, this returns a
34// disassembler context. If not, it returns NULL.
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000035//
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +000036LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
37 void *DisInfo, int TagType,
38 LLVMOpInfoCallback GetOpInfo,
39 LLVMSymbolLookupCallback SymbolLookUp){
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000040 // Get the target.
41 std::string Error;
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +000042 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
Kevin Enderby64d93452013-05-23 00:32:34 +000043 if (!TheTarget)
44 return 0;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000045
Rafael Espindola227144c2013-05-13 01:16:13 +000046 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
47 if (!MRI)
48 return 0;
49
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000050 // Get the assembler info needed to setup the MCContext.
Rafael Espindola227144c2013-05-13 01:16:13 +000051 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000052 if (!MAI)
53 return 0;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000054
Craig Topper54bfde72012-04-02 06:09:36 +000055 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000056 if (!MII)
57 return 0;
Craig Topper54bfde72012-04-02 06:09:36 +000058
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000059 // Package up features to be passed to target/subtarget
60 std::string FeaturesStr;
61
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +000062 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
James Molloy4c493e82011-09-07 17:24:38 +000063 FeaturesStr);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000064 if (!STI)
65 return 0;
James Molloy4c493e82011-09-07 17:24:38 +000066
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000067 // Set up the MCContext for creating symbols and MCExpr's.
Evan Chengbbf3b0d2011-07-20 19:50:42 +000068 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000069 if (!Ctx)
70 return 0;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000071
72 // Set up disassembler.
James Molloy4c493e82011-09-07 17:24:38 +000073 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000074 if (!DisAsm)
75 return 0;
Kevin Enderby5dcda642011-10-04 22:44:48 +000076 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000077
78 // Set up the instruction printer.
79 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengab37af92011-07-06 19:45:42 +000080 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Craig Topper54bfde72012-04-02 06:09:36 +000081 *MAI, *MII, *MRI, *STI);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000082 if (!IP)
83 return 0;
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000084
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +000085 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000086 GetOpInfo, SymbolLookUp,
Evan Chengbbf3b0d2011-07-20 19:50:42 +000087 TheTarget, MAI, MRI,
Sean Callanane804b5b2012-04-06 18:21:09 +000088 STI, MII, Ctx, DisAsm, IP);
Kevin Enderbyf536c6a2013-03-12 18:12:17 +000089 if (!DC)
90 return 0;
Owen Anderson233f1302011-09-15 18:37:20 +000091
Kevin Enderbyf3070dc2011-03-28 18:25:07 +000092 return DC;
93}
94
Jim Grosbach0ca9d5b2012-12-07 23:53:27 +000095LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
96 int TagType, LLVMOpInfoCallback GetOpInfo,
97 LLVMSymbolLookupCallback SymbolLookUp) {
98 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
99 SymbolLookUp);
100}
101
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000102//
103// LLVMDisasmDispose() disposes of the disassembler specified by the context.
104//
105void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
106 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
107 delete DC;
108}
109
110namespace {
111//
112// The memory object created by LLVMDisasmInstruction().
113//
114class DisasmMemoryObject : public MemoryObject {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000115 uint8_t *Bytes;
116 uint64_t Size;
117 uint64_t BasePC;
118public:
119 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
120 Bytes(bytes), Size(size), BasePC(basePC) {}
121
122 uint64_t getBase() const { return BasePC; }
Derek Schuff56b662c2012-02-29 01:09:06 +0000123 uint64_t getExtent() const { return Size; }
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000124
Derek Schuff56b662c2012-02-29 01:09:06 +0000125 int readByte(uint64_t Addr, uint8_t *Byte) const {
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000126 if (Addr - BasePC >= Size)
127 return -1;
128 *Byte = Bytes[Addr - BasePC];
129 return 0;
130 }
131};
Chris Lattner0388fb02011-05-22 04:52:24 +0000132} // end anonymous namespace
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000133
134//
Benjamin Kramer26e77682011-04-09 14:06:12 +0000135// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000136// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer26e77682011-04-09 14:06:12 +0000137// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000138// BytesSize number of bytes. The instruction is at the address specified by
139// the PC parameter. If a valid instruction can be disassembled its string is
140// returned indirectly in OutString which whos size is specified in the
141// parameter OutStringSize. This function returns the number of bytes in the
142// instruction or zero if there was no valid instruction. If this function
143// returns zero the caller will have to pick how many bytes they want to step
144// over by printing a .byte, .long etc. to continue.
145//
146size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
147 uint64_t BytesSize, uint64_t PC, char *OutString,
148 size_t OutStringSize){
149 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
150 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
151 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
152
153 uint64_t Size;
154 MCInst Inst;
155 const MCDisassembler *DisAsm = DC->getDisAsm();
156 MCInstPrinter *IP = DC->getIP();
James Molloy5ada2a72011-09-01 22:01:14 +0000157 MCDisassembler::DecodeStatus S;
158 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
Owen Andersona0c3b972011-09-15 23:38:46 +0000159 /*REMOVE*/ nulls(), DC->CommentStream);
James Molloy5ada2a72011-09-01 22:01:14 +0000160 switch (S) {
161 case MCDisassembler::Fail:
162 case MCDisassembler::SoftFail:
James Molloydb4ce602011-09-01 18:02:14 +0000163 // FIXME: Do something different for soft failure modes?
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000164 return 0;
James Molloy5ada2a72011-09-01 22:01:14 +0000165
166 case MCDisassembler::Success: {
Owen Anderson233f1302011-09-15 18:37:20 +0000167 DC->CommentStream.flush();
Owen Anderson233f1302011-09-15 18:37:20 +0000168 StringRef Comments = DC->CommentsToEmit.str();
169
Owen Andersona0c3b972011-09-15 23:38:46 +0000170 SmallVector<char, 64> InsnStr;
171 raw_svector_ostream OS(InsnStr);
172 IP->printInst(&Inst, OS, Comments);
173 OS.flush();
Owen Anderson233f1302011-09-15 18:37:20 +0000174
Owen Anderson233f1302011-09-15 18:37:20 +0000175 // Tell the comment stream that the vector changed underneath it.
Owen Andersona0c3b972011-09-15 23:38:46 +0000176 DC->CommentsToEmit.clear();
Owen Anderson233f1302011-09-15 18:37:20 +0000177 DC->CommentStream.resync();
178
James Molloy5ada2a72011-09-01 22:01:14 +0000179 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
180 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
181 std::memcpy(OutString, InsnStr.data(), OutputSize);
182 OutString[OutputSize] = '\0'; // Terminate string.
183
184 return Size;
James Molloydb4ce602011-09-01 18:02:14 +0000185 }
James Molloy5ada2a72011-09-01 22:01:14 +0000186 }
David Blaikie46a9f012012-01-20 21:51:11 +0000187 llvm_unreachable("Invalid DecodeStatus!");
Kevin Enderbyf3070dc2011-03-28 18:25:07 +0000188}
Kevin Enderby62183c42012-10-22 22:31:46 +0000189
190//
191// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
192// can set all the Options and 0 otherwise.
193//
194int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
195 if (Options & LLVMDisassembler_Option_UseMarkup){
196 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
197 MCInstPrinter *IP = DC->getIP();
198 IP->setUseMarkup(1);
199 Options &= ~LLVMDisassembler_Option_UseMarkup;
200 }
Kevin Enderby168ffb32012-12-05 18:13:19 +0000201 if (Options & LLVMDisassembler_Option_PrintImmHex){
202 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
203 MCInstPrinter *IP = DC->getIP();
204 IP->setPrintImmHex(1);
205 Options &= ~LLVMDisassembler_Option_PrintImmHex;
206 }
Kevin Enderby85cf5312012-12-18 23:47:28 +0000207 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
208 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
209 // Try to set up the new instruction printer.
210 const MCAsmInfo *MAI = DC->getAsmInfo();
211 const MCInstrInfo *MII = DC->getInstrInfo();
212 const MCRegisterInfo *MRI = DC->getRegisterInfo();
213 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
214 int AsmPrinterVariant = MAI->getAssemblerDialect();
215 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
216 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
217 AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
218 if (IP) {
219 DC->setIP(IP);
220 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
221 }
222 }
Kevin Enderby62183c42012-10-22 22:31:46 +0000223 return (Options == 0);
224}