blob: 19d7349bbde1d3587f534f121f87032115703718 [file] [log] [blame]
Kevin Enderby93f79362011-03-28 18:25:07 +00001//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface -*- C -*-===//
2//
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"
14#include "llvm/MC/MCDisassembler.h"
15#include "llvm/MC/MCInst.h"
16#include "llvm/MC/MCInstPrinter.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmInfo.h" // FIXME.
20#include "llvm/Target/TargetMachine.h" // FIXME.
21#include "llvm/Target/TargetSelect.h"
22#include "llvm/Support/MemoryObject.h"
23
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//
36LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
37 int TagType, LLVMOpInfoCallback GetOpInfo,
38 LLVMSymbolLookupCallback SymbolLookUp) {
39 // Initialize targets and assembly printers/parsers.
40 llvm::InitializeAllTargetInfos();
41 // FIXME: We shouldn't need to initialize the Target(Machine)s.
42 llvm::InitializeAllTargets();
Evan Cheng1abf2cb2011-07-14 23:50:31 +000043 llvm::InitializeAllMCAsmInfos();
Evan Cheng0e6a0522011-07-18 20:57:22 +000044 llvm::InitializeAllMCRegisterInfos();
Kevin Enderby93f79362011-03-28 18:25:07 +000045 llvm::InitializeAllAsmPrinters();
46 llvm::InitializeAllAsmParsers();
47 llvm::InitializeAllDisassemblers();
48
49 // Get the target.
50 std::string Error;
51 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
52 assert(TheTarget && "Unable to create target!");
53
54 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000055 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000056 assert(MAI && "Unable to create target asm info!");
57
Evan Cheng0e6a0522011-07-18 20:57:22 +000058 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
59 assert(MRI && "Unable to create target register info!");
60
Kevin Enderby93f79362011-03-28 18:25:07 +000061 // Package up features to be passed to target/subtarget
62 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000063 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000064
65 // FIXME: We shouldn't need to do this (and link in codegen).
66 // When we split this out, we should do it in a way that makes
67 // it straightforward to switch subtargets on the fly.
Evan Cheng276365d2011-06-30 01:53:36 +000068 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU,
69 FeaturesStr);
Kevin Enderby93f79362011-03-28 18:25:07 +000070 assert(TM && "Unable to create target machine!");
71
72 // Get the target assembler info needed to setup the context.
73 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
74 assert(tai && "Unable to create target assembler!");
75
76 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng0e6a0522011-07-18 20:57:22 +000077 MCContext *Ctx = new MCContext(*MAI, *MRI, tai);
Kevin Enderby93f79362011-03-28 18:25:07 +000078 assert(Ctx && "Unable to create MCContext!");
79
80 // Set up disassembler.
Kevin Enderbybd332762011-04-11 18:08:50 +000081 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
Kevin Enderby93f79362011-03-28 18:25:07 +000082 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000083 DisAsm->setupForSymbolicDisassembly(GetOpInfo, 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,
Kevin Enderby93f79362011-03-28 18:25:07 +000088 *MAI);
89 assert(IP && "Unable to create instruction printer!");
90
91 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
92 GetOpInfo, SymbolLookUp,
Evan Cheng0e6a0522011-07-18 20:57:22 +000093 TheTarget, MAI, MRI, TM, tai,
94 Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000095 assert(DC && "Allocation failure!");
96 return DC;
97}
98
99//
100// LLVMDisasmDispose() disposes of the disassembler specified by the context.
101//
102void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
103 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
104 delete DC;
105}
106
107namespace {
108//
109// The memory object created by LLVMDisasmInstruction().
110//
111class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +0000112 uint8_t *Bytes;
113 uint64_t Size;
114 uint64_t BasePC;
115public:
116 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
117 Bytes(bytes), Size(size), BasePC(basePC) {}
118
119 uint64_t getBase() const { return BasePC; }
120 uint64_t getExtent() const { return Size; }
121
122 int readByte(uint64_t Addr, uint8_t *Byte) const {
123 if (Addr - BasePC >= Size)
124 return -1;
125 *Byte = Bytes[Addr - BasePC];
126 return 0;
127 }
128};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000129} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000130
131//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000132// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000133// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000134// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000135// BytesSize number of bytes. The instruction is at the address specified by
136// the PC parameter. If a valid instruction can be disassembled its string is
137// returned indirectly in OutString which whos size is specified in the
138// parameter OutStringSize. This function returns the number of bytes in the
139// instruction or zero if there was no valid instruction. If this function
140// returns zero the caller will have to pick how many bytes they want to step
141// over by printing a .byte, .long etc. to continue.
142//
143size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
144 uint64_t BytesSize, uint64_t PC, char *OutString,
145 size_t OutStringSize){
146 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
147 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
148 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
149
150 uint64_t Size;
151 MCInst Inst;
152 const MCDisassembler *DisAsm = DC->getDisAsm();
153 MCInstPrinter *IP = DC->getIP();
154 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
155 return 0;
156
Chris Lattner9063b552011-05-22 04:53:24 +0000157 SmallVector<char, 64> InsnStr;
158 raw_svector_ostream OS(InsnStr);
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000159 IP->printInst(&Inst, OS);
160 OS.flush();
Kevin Enderby93f79362011-03-28 18:25:07 +0000161
Chris Lattner97ff42d2011-05-22 04:52:24 +0000162 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000163 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
164 std::memcpy(OutString, InsnStr.data(), OutputSize);
165 OutString[OutputSize] = '\0'; // Terminate string.
166
Kevin Enderby93f79362011-03-28 18:25:07 +0000167 return Size;
168}