blob: 4766b37476355a618af4842934ef1799f0e833bd [file] [log] [blame]
Nick Lewycky833a0032011-09-01 21:09:04 +00001//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
Kevin Enderby93f79362011-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 Lattner97ff42d2011-05-22 04:52:24 +00009
Kevin Enderby93f79362011-03-28 18:25:07 +000010#include "Disassembler.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000011#include "llvm-c/Disassembler.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000012#include "llvm/MC/MCAsmInfo.h"
Evan Cheng4c816482011-07-20 06:54:19 +000013#include "llvm/MC/MCContext.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000014#include "llvm/MC/MCDisassembler.h"
15#include "llvm/MC/MCInst.h"
16#include "llvm/MC/MCInstPrinter.h"
Sean Callanan55e79802012-04-06 18:21:09 +000017#include "llvm/MC/MCInstrInfo.h"
Evan Cheng4c816482011-07-20 06:54:19 +000018#include "llvm/MC/MCRegisterInfo.h"
Sean Callanan55e79802012-04-06 18:21:09 +000019#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/Support/ErrorHandling.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000021#include "llvm/Support/MemoryObject.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000022#include "llvm/Support/TargetRegistry.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000023
24namespace llvm {
25class Target;
26} // namespace llvm
27using namespace llvm;
28
Kevin Enderby93f79362011-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 Lattner97ff42d2011-05-22 04:52:24 +000031// parameter and specifying the TagType and callback functions as described in
Kevin Enderby93f79362011-03-28 18:25:07 +000032// the header llvm-c/Disassembler.h . The pointer to the block and the
Chris Lattner97ff42d2011-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 Enderby93f79362011-03-28 18:25:07 +000035//
Jim Grosbach68a590d2012-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 Enderby93f79362011-03-28 18:25:07 +000040 // Get the target.
41 std::string Error;
Jim Grosbach68a590d2012-12-07 23:53:27 +000042 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
Kevin Enderby93f79362011-03-28 18:25:07 +000043 assert(TheTarget && "Unable to create target!");
44
45 // Get the assembler info needed to setup the MCContext.
Jim Grosbach68a590d2012-12-07 23:53:27 +000046 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(Triple);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000047 if (!MAI)
48 return 0;
Kevin Enderby93f79362011-03-28 18:25:07 +000049
Craig Topper17463b32012-04-02 06:09:36 +000050 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
Kevin Enderby2ee69f12013-03-12 18:12:17 +000051 if (!MII)
52 return 0;
Craig Topper17463b32012-04-02 06:09:36 +000053
Jim Grosbach68a590d2012-12-07 23:53:27 +000054 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000055 if (!MRI)
56 return 0;
Evan Cheng0e6a0522011-07-18 20:57:22 +000057
Kevin Enderby93f79362011-03-28 18:25:07 +000058 // Package up features to be passed to target/subtarget
59 std::string FeaturesStr;
60
Jim Grosbach68a590d2012-12-07 23:53:27 +000061 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
James Molloyb9505852011-09-07 17:24:38 +000062 FeaturesStr);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000063 if (!STI)
64 return 0;
James Molloyb9505852011-09-07 17:24:38 +000065
Kevin Enderby93f79362011-03-28 18:25:07 +000066 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000067 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000068 if (!Ctx)
69 return 0;
Kevin Enderby93f79362011-03-28 18:25:07 +000070
71 // Set up disassembler.
James Molloyb9505852011-09-07 17:24:38 +000072 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000073 if (!DisAsm)
74 return 0;
Kevin Enderby9e5887b2011-10-04 22:44:48 +000075 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000076
77 // Set up the instruction printer.
78 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000079 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Craig Topper17463b32012-04-02 06:09:36 +000080 *MAI, *MII, *MRI, *STI);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000081 if (!IP)
82 return 0;
Kevin Enderby93f79362011-03-28 18:25:07 +000083
Jim Grosbach68a590d2012-12-07 23:53:27 +000084 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
Kevin Enderby93f79362011-03-28 18:25:07 +000085 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000086 TheTarget, MAI, MRI,
Sean Callanan55e79802012-04-06 18:21:09 +000087 STI, MII, Ctx, DisAsm, IP);
Kevin Enderby2ee69f12013-03-12 18:12:17 +000088 if (!DC)
89 return 0;
Owen Anderson8f29e692011-09-15 18:37:20 +000090
Kevin Enderby93f79362011-03-28 18:25:07 +000091 return DC;
92}
93
Jim Grosbach68a590d2012-12-07 23:53:27 +000094LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
95 int TagType, LLVMOpInfoCallback GetOpInfo,
96 LLVMSymbolLookupCallback SymbolLookUp) {
97 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
98 SymbolLookUp);
99}
100
Kevin Enderby93f79362011-03-28 18:25:07 +0000101//
102// LLVMDisasmDispose() disposes of the disassembler specified by the context.
103//
104void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
105 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
106 delete DC;
107}
108
109namespace {
110//
111// The memory object created by LLVMDisasmInstruction().
112//
113class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +0000114 uint8_t *Bytes;
115 uint64_t Size;
116 uint64_t BasePC;
117public:
118 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
119 Bytes(bytes), Size(size), BasePC(basePC) {}
120
121 uint64_t getBase() const { return BasePC; }
Derek Schuffadef06a2012-02-29 01:09:06 +0000122 uint64_t getExtent() const { return Size; }
Kevin Enderby93f79362011-03-28 18:25:07 +0000123
Derek Schuffadef06a2012-02-29 01:09:06 +0000124 int readByte(uint64_t Addr, uint8_t *Byte) const {
Kevin Enderby93f79362011-03-28 18:25:07 +0000125 if (Addr - BasePC >= Size)
126 return -1;
127 *Byte = Bytes[Addr - BasePC];
128 return 0;
129 }
130};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000131} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000132
133//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000134// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000135// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000136// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000137// BytesSize number of bytes. The instruction is at the address specified by
138// the PC parameter. If a valid instruction can be disassembled its string is
139// returned indirectly in OutString which whos size is specified in the
140// parameter OutStringSize. This function returns the number of bytes in the
141// instruction or zero if there was no valid instruction. If this function
142// returns zero the caller will have to pick how many bytes they want to step
143// over by printing a .byte, .long etc. to continue.
144//
145size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
146 uint64_t BytesSize, uint64_t PC, char *OutString,
147 size_t OutStringSize){
148 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
149 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
150 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
151
152 uint64_t Size;
153 MCInst Inst;
154 const MCDisassembler *DisAsm = DC->getDisAsm();
155 MCInstPrinter *IP = DC->getIP();
James Molloyee064432011-09-01 22:01:14 +0000156 MCDisassembler::DecodeStatus S;
157 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
Owen Anderson98c5dda2011-09-15 23:38:46 +0000158 /*REMOVE*/ nulls(), DC->CommentStream);
James Molloyee064432011-09-01 22:01:14 +0000159 switch (S) {
160 case MCDisassembler::Fail:
161 case MCDisassembler::SoftFail:
James Molloyc047dca2011-09-01 18:02:14 +0000162 // FIXME: Do something different for soft failure modes?
Kevin Enderby93f79362011-03-28 18:25:07 +0000163 return 0;
James Molloyee064432011-09-01 22:01:14 +0000164
165 case MCDisassembler::Success: {
Owen Anderson8f29e692011-09-15 18:37:20 +0000166 DC->CommentStream.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000167 StringRef Comments = DC->CommentsToEmit.str();
168
Owen Anderson98c5dda2011-09-15 23:38:46 +0000169 SmallVector<char, 64> InsnStr;
170 raw_svector_ostream OS(InsnStr);
171 IP->printInst(&Inst, OS, Comments);
172 OS.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000173
Owen Anderson8f29e692011-09-15 18:37:20 +0000174 // Tell the comment stream that the vector changed underneath it.
Owen Anderson98c5dda2011-09-15 23:38:46 +0000175 DC->CommentsToEmit.clear();
Owen Anderson8f29e692011-09-15 18:37:20 +0000176 DC->CommentStream.resync();
177
James Molloyee064432011-09-01 22:01:14 +0000178 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
179 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
180 std::memcpy(OutString, InsnStr.data(), OutputSize);
181 OutString[OutputSize] = '\0'; // Terminate string.
182
183 return Size;
James Molloyc047dca2011-09-01 18:02:14 +0000184 }
James Molloyee064432011-09-01 22:01:14 +0000185 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000186 llvm_unreachable("Invalid DecodeStatus!");
Kevin Enderby93f79362011-03-28 18:25:07 +0000187}
Kevin Enderby3ed03162012-10-22 22:31:46 +0000188
189//
190// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
191// can set all the Options and 0 otherwise.
192//
193int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
194 if (Options & LLVMDisassembler_Option_UseMarkup){
195 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
196 MCInstPrinter *IP = DC->getIP();
197 IP->setUseMarkup(1);
198 Options &= ~LLVMDisassembler_Option_UseMarkup;
199 }
Kevin Enderby14ccc902012-12-05 18:13:19 +0000200 if (Options & LLVMDisassembler_Option_PrintImmHex){
201 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
202 MCInstPrinter *IP = DC->getIP();
203 IP->setPrintImmHex(1);
204 Options &= ~LLVMDisassembler_Option_PrintImmHex;
205 }
Kevin Enderby5469f602012-12-18 23:47:28 +0000206 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
207 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
208 // Try to set up the new instruction printer.
209 const MCAsmInfo *MAI = DC->getAsmInfo();
210 const MCInstrInfo *MII = DC->getInstrInfo();
211 const MCRegisterInfo *MRI = DC->getRegisterInfo();
212 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
213 int AsmPrinterVariant = MAI->getAssemblerDialect();
214 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
215 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
216 AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
217 if (IP) {
218 DC->setIP(IP);
219 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
220 }
221 }
Kevin Enderby3ed03162012-10-22 22:31:46 +0000222 return (Options == 0);
223}