blob: 490ca75bc8cd36edf154965bafdf31d975bc8f71 [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"
12
Kevin Enderby93f79362011-03-28 18:25:07 +000013#include "llvm/MC/MCAsmInfo.h"
Evan Cheng4c816482011-07-20 06:54:19 +000014#include "llvm/MC/MCContext.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000015#include "llvm/MC/MCDisassembler.h"
16#include "llvm/MC/MCInst.h"
17#include "llvm/MC/MCInstPrinter.h"
Sean Callanan55e79802012-04-06 18:21:09 +000018#include "llvm/MC/MCInstrInfo.h"
Evan Cheng4c816482011-07-20 06:54:19 +000019#include "llvm/MC/MCRegisterInfo.h"
Sean Callanan55e79802012-04-06 18:21:09 +000020#include "llvm/MC/MCSubtargetInfo.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"
David Blaikie4d6ccb52012-01-20 21:51:11 +000023#include "llvm/Support/ErrorHandling.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000024
25namespace llvm {
26class Target;
27} // namespace llvm
28using namespace llvm;
29
Kevin Enderby93f79362011-03-28 18:25:07 +000030// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
31// disassembly is supported by passing a block of information in the DisInfo
Chris Lattner97ff42d2011-05-22 04:52:24 +000032// parameter and specifying the TagType and callback functions as described in
Kevin Enderby93f79362011-03-28 18:25:07 +000033// the header llvm-c/Disassembler.h . The pointer to the block and the
Chris Lattner97ff42d2011-05-22 04:52:24 +000034// functions can all be passed as NULL. If successful, this returns a
35// disassembler context. If not, it returns NULL.
Kevin Enderby93f79362011-03-28 18:25:07 +000036//
37LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
38 int TagType, LLVMOpInfoCallback GetOpInfo,
39 LLVMSymbolLookupCallback SymbolLookUp) {
Kevin Enderby93f79362011-03-28 18:25:07 +000040 // Get the target.
41 std::string Error;
42 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
43 assert(TheTarget && "Unable to create target!");
44
45 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000046 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000047 assert(MAI && "Unable to create target asm info!");
48
Craig Topper17463b32012-04-02 06:09:36 +000049 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
50 assert(MII && "Unable to create target instruction info!");
51
Evan Cheng0e6a0522011-07-18 20:57:22 +000052 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
53 assert(MRI && "Unable to create target register info!");
54
Kevin Enderby93f79362011-03-28 18:25:07 +000055 // Package up features to be passed to target/subtarget
56 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000057 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000058
James Molloyb9505852011-09-07 17:24:38 +000059 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
60 FeaturesStr);
61 assert(STI && "Unable to create subtarget info!");
62
Kevin Enderby93f79362011-03-28 18:25:07 +000063 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000064 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby93f79362011-03-28 18:25:07 +000065 assert(Ctx && "Unable to create MCContext!");
66
67 // Set up disassembler.
James Molloyb9505852011-09-07 17:24:38 +000068 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000069 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderby9e5887b2011-10-04 22:44:48 +000070 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000071
72 // Set up the instruction printer.
73 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000074 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Craig Topper17463b32012-04-02 06:09:36 +000075 *MAI, *MII, *MRI, *STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000076 assert(IP && "Unable to create instruction printer!");
77
78 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
79 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000080 TheTarget, MAI, MRI,
Sean Callanan55e79802012-04-06 18:21:09 +000081 STI, MII, Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000082 assert(DC && "Allocation failure!");
Owen Anderson8f29e692011-09-15 18:37:20 +000083
Kevin Enderby93f79362011-03-28 18:25:07 +000084 return DC;
85}
86
87//
88// LLVMDisasmDispose() disposes of the disassembler specified by the context.
89//
90void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
91 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
92 delete DC;
93}
94
95namespace {
96//
97// The memory object created by LLVMDisasmInstruction().
98//
99class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +0000100 uint8_t *Bytes;
101 uint64_t Size;
102 uint64_t BasePC;
103public:
104 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
105 Bytes(bytes), Size(size), BasePC(basePC) {}
106
107 uint64_t getBase() const { return BasePC; }
Derek Schuffadef06a2012-02-29 01:09:06 +0000108 uint64_t getExtent() const { return Size; }
Kevin Enderby93f79362011-03-28 18:25:07 +0000109
Derek Schuffadef06a2012-02-29 01:09:06 +0000110 int readByte(uint64_t Addr, uint8_t *Byte) const {
Kevin Enderby93f79362011-03-28 18:25:07 +0000111 if (Addr - BasePC >= Size)
112 return -1;
113 *Byte = Bytes[Addr - BasePC];
114 return 0;
115 }
116};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000117} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000118
119//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000120// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000121// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000122// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000123// BytesSize number of bytes. The instruction is at the address specified by
124// the PC parameter. If a valid instruction can be disassembled its string is
125// returned indirectly in OutString which whos size is specified in the
126// parameter OutStringSize. This function returns the number of bytes in the
127// instruction or zero if there was no valid instruction. If this function
128// returns zero the caller will have to pick how many bytes they want to step
129// over by printing a .byte, .long etc. to continue.
130//
131size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
132 uint64_t BytesSize, uint64_t PC, char *OutString,
133 size_t OutStringSize){
134 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
135 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
136 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
137
138 uint64_t Size;
139 MCInst Inst;
140 const MCDisassembler *DisAsm = DC->getDisAsm();
141 MCInstPrinter *IP = DC->getIP();
James Molloyee064432011-09-01 22:01:14 +0000142 MCDisassembler::DecodeStatus S;
143 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
Owen Anderson98c5dda2011-09-15 23:38:46 +0000144 /*REMOVE*/ nulls(), DC->CommentStream);
James Molloyee064432011-09-01 22:01:14 +0000145 switch (S) {
146 case MCDisassembler::Fail:
147 case MCDisassembler::SoftFail:
James Molloyc047dca2011-09-01 18:02:14 +0000148 // FIXME: Do something different for soft failure modes?
Kevin Enderby93f79362011-03-28 18:25:07 +0000149 return 0;
James Molloyee064432011-09-01 22:01:14 +0000150
151 case MCDisassembler::Success: {
Owen Anderson8f29e692011-09-15 18:37:20 +0000152 DC->CommentStream.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000153 StringRef Comments = DC->CommentsToEmit.str();
154
Owen Anderson98c5dda2011-09-15 23:38:46 +0000155 SmallVector<char, 64> InsnStr;
156 raw_svector_ostream OS(InsnStr);
157 IP->printInst(&Inst, OS, Comments);
158 OS.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000159
Owen Anderson8f29e692011-09-15 18:37:20 +0000160 // Tell the comment stream that the vector changed underneath it.
Owen Anderson98c5dda2011-09-15 23:38:46 +0000161 DC->CommentsToEmit.clear();
Owen Anderson8f29e692011-09-15 18:37:20 +0000162 DC->CommentStream.resync();
163
James Molloyee064432011-09-01 22:01:14 +0000164 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
165 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
166 std::memcpy(OutString, InsnStr.data(), OutputSize);
167 OutString[OutputSize] = '\0'; // Terminate string.
168
169 return Size;
James Molloyc047dca2011-09-01 18:02:14 +0000170 }
James Molloyee064432011-09-01 22:01:14 +0000171 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000172 llvm_unreachable("Invalid DecodeStatus!");
Kevin Enderby93f79362011-03-28 18:25:07 +0000173}
Kevin Enderby3ed03162012-10-22 22:31:46 +0000174
175//
176// LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
177// can set all the Options and 0 otherwise.
178//
179int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
180 if (Options & LLVMDisassembler_Option_UseMarkup){
181 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
182 MCInstPrinter *IP = DC->getIP();
183 IP->setUseMarkup(1);
184 Options &= ~LLVMDisassembler_Option_UseMarkup;
185 }
186 return (Options == 0);
187}