blob: 35f675dc6d1be73706294d175d891a842ac54a4d [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"
Kevin Enderbyfc0d7402012-02-17 19:18:29 +000023#include "llvm/Support/TargetSelect.h"
David Blaikie4d6ccb52012-01-20 21:51:11 +000024#include "llvm/Support/ErrorHandling.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000025
26namespace llvm {
27class Target;
28} // namespace llvm
29using namespace llvm;
30
Kevin Enderby93f79362011-03-28 18:25:07 +000031// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
32// disassembly is supported by passing a block of information in the DisInfo
Chris Lattner97ff42d2011-05-22 04:52:24 +000033// parameter and specifying the TagType and callback functions as described in
Kevin Enderby93f79362011-03-28 18:25:07 +000034// the header llvm-c/Disassembler.h . The pointer to the block and the
Chris Lattner97ff42d2011-05-22 04:52:24 +000035// functions can all be passed as NULL. If successful, this returns a
36// disassembler context. If not, it returns NULL.
Kevin Enderby93f79362011-03-28 18:25:07 +000037//
38LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
39 int TagType, LLVMOpInfoCallback GetOpInfo,
40 LLVMSymbolLookupCallback SymbolLookUp) {
Kevin Enderbyfc0d7402012-02-17 19:18:29 +000041 // Initialize targets and assembly printers/parsers.
42 // FIXME: Clients are responsible for initializing the targets. And this
43 // would be done by calling routines in "llvm-c/Target.h" which are static
44 // line functions. But the current use of LLVMCreateDisasm() is to dynamically
Kevin Enderbyd9165eb2012-02-17 19:26:00 +000045 // load libLTO with dlopen() and then lookup the symbols using dlsym().
Kevin Enderbyfc0d7402012-02-17 19:18:29 +000046 // And since these initialize routines are static that does not work which
47 // is why the call to them in this 'C' library API was added back.
48 llvm::InitializeAllTargetInfos();
49 llvm::InitializeAllTargetMCs();
50 llvm::InitializeAllAsmParsers();
51 llvm::InitializeAllDisassemblers();
52
Kevin Enderby93f79362011-03-28 18:25:07 +000053 // Get the target.
54 std::string Error;
55 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
56 assert(TheTarget && "Unable to create target!");
57
58 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000059 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000060 assert(MAI && "Unable to create target asm info!");
61
Craig Topper17463b32012-04-02 06:09:36 +000062 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
63 assert(MII && "Unable to create target instruction info!");
64
Evan Cheng0e6a0522011-07-18 20:57:22 +000065 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
66 assert(MRI && "Unable to create target register info!");
67
Kevin Enderby93f79362011-03-28 18:25:07 +000068 // Package up features to be passed to target/subtarget
69 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000070 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000071
James Molloyb9505852011-09-07 17:24:38 +000072 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
73 FeaturesStr);
74 assert(STI && "Unable to create subtarget info!");
75
Kevin Enderby93f79362011-03-28 18:25:07 +000076 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000077 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby93f79362011-03-28 18:25:07 +000078 assert(Ctx && "Unable to create MCContext!");
79
80 // Set up disassembler.
James Molloyb9505852011-09-07 17:24:38 +000081 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000082 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderby9e5887b2011-10-04 22:44:48 +000083 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000084
85 // Set up the instruction printer.
86 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000087 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Craig Topper17463b32012-04-02 06:09:36 +000088 *MAI, *MII, *MRI, *STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000089 assert(IP && "Unable to create instruction printer!");
90
91 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
92 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000093 TheTarget, MAI, MRI,
Sean Callanan55e79802012-04-06 18:21:09 +000094 STI, MII, Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000095 assert(DC && "Allocation failure!");
Owen Anderson8f29e692011-09-15 18:37:20 +000096
Kevin Enderby93f79362011-03-28 18:25:07 +000097 return DC;
98}
99
100//
101// LLVMDisasmDispose() disposes of the disassembler specified by the context.
102//
103void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
104 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
105 delete DC;
106}
107
108namespace {
109//
110// The memory object created by LLVMDisasmInstruction().
111//
112class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +0000113 uint8_t *Bytes;
114 uint64_t Size;
115 uint64_t BasePC;
116public:
117 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
118 Bytes(bytes), Size(size), BasePC(basePC) {}
119
120 uint64_t getBase() const { return BasePC; }
Derek Schuffadef06a2012-02-29 01:09:06 +0000121 uint64_t getExtent() const { return Size; }
Kevin Enderby93f79362011-03-28 18:25:07 +0000122
Derek Schuffadef06a2012-02-29 01:09:06 +0000123 int readByte(uint64_t Addr, uint8_t *Byte) const {
Kevin Enderby93f79362011-03-28 18:25:07 +0000124 if (Addr - BasePC >= Size)
125 return -1;
126 *Byte = Bytes[Addr - BasePC];
127 return 0;
128 }
129};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000130} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000131
132//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000133// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000134// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000135// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000136// BytesSize number of bytes. The instruction is at the address specified by
137// the PC parameter. If a valid instruction can be disassembled its string is
138// returned indirectly in OutString which whos size is specified in the
139// parameter OutStringSize. This function returns the number of bytes in the
140// instruction or zero if there was no valid instruction. If this function
141// returns zero the caller will have to pick how many bytes they want to step
142// over by printing a .byte, .long etc. to continue.
143//
144size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
145 uint64_t BytesSize, uint64_t PC, char *OutString,
146 size_t OutStringSize){
147 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
148 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
149 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
150
151 uint64_t Size;
152 MCInst Inst;
153 const MCDisassembler *DisAsm = DC->getDisAsm();
154 MCInstPrinter *IP = DC->getIP();
James Molloyee064432011-09-01 22:01:14 +0000155 MCDisassembler::DecodeStatus S;
156 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
Owen Anderson98c5dda2011-09-15 23:38:46 +0000157 /*REMOVE*/ nulls(), DC->CommentStream);
James Molloyee064432011-09-01 22:01:14 +0000158 switch (S) {
159 case MCDisassembler::Fail:
160 case MCDisassembler::SoftFail:
James Molloyc047dca2011-09-01 18:02:14 +0000161 // FIXME: Do something different for soft failure modes?
Kevin Enderby93f79362011-03-28 18:25:07 +0000162 return 0;
James Molloyee064432011-09-01 22:01:14 +0000163
164 case MCDisassembler::Success: {
Owen Anderson8f29e692011-09-15 18:37:20 +0000165 DC->CommentStream.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000166 StringRef Comments = DC->CommentsToEmit.str();
167
Owen Anderson98c5dda2011-09-15 23:38:46 +0000168 SmallVector<char, 64> InsnStr;
169 raw_svector_ostream OS(InsnStr);
170 IP->printInst(&Inst, OS, Comments);
171 OS.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000172
Owen Anderson8f29e692011-09-15 18:37:20 +0000173 // Tell the comment stream that the vector changed underneath it.
Owen Anderson98c5dda2011-09-15 23:38:46 +0000174 DC->CommentsToEmit.clear();
Owen Anderson8f29e692011-09-15 18:37:20 +0000175 DC->CommentStream.resync();
176
James Molloyee064432011-09-01 22:01:14 +0000177 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
178 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
179 std::memcpy(OutString, InsnStr.data(), OutputSize);
180 OutString[OutputSize] = '\0'; // Terminate string.
181
182 return Size;
James Molloyc047dca2011-09-01 18:02:14 +0000183 }
James Molloyee064432011-09-01 22:01:14 +0000184 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000185 llvm_unreachable("Invalid DecodeStatus!");
Kevin Enderby93f79362011-03-28 18:25:07 +0000186}