blob: a2aaced09bbfa3a010d8994b6a89d6f000b2badc [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"
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"
Evan Cheng4c816482011-07-20 06:54:19 +000018#include "llvm/MC/MCRegisterInfo.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000019#include "llvm/Target/TargetRegistry.h"
20#include "llvm/Target/TargetAsmInfo.h" // FIXME.
21#include "llvm/Target/TargetMachine.h" // FIXME.
22#include "llvm/Target/TargetSelect.h"
23#include "llvm/Support/MemoryObject.h"
24
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) {
40 // Initialize targets and assembly printers/parsers.
41 llvm::InitializeAllTargetInfos();
42 // FIXME: We shouldn't need to initialize the Target(Machine)s.
43 llvm::InitializeAllTargets();
Evan Cheng1abf2cb2011-07-14 23:50:31 +000044 llvm::InitializeAllMCAsmInfos();
Evan Cheng43966132011-07-19 06:37:02 +000045 llvm::InitializeAllMCCodeGenInfos();
Evan Cheng0e6a0522011-07-18 20:57:22 +000046 llvm::InitializeAllMCRegisterInfos();
Kevin Enderby93f79362011-03-28 18:25:07 +000047 llvm::InitializeAllAsmPrinters();
48 llvm::InitializeAllAsmParsers();
49 llvm::InitializeAllDisassemblers();
50
51 // Get the target.
52 std::string Error;
53 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
54 assert(TheTarget && "Unable to create target!");
55
56 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000057 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000058 assert(MAI && "Unable to create target asm info!");
59
Evan Cheng0e6a0522011-07-18 20:57:22 +000060 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
61 assert(MRI && "Unable to create target register info!");
62
Kevin Enderby93f79362011-03-28 18:25:07 +000063 // Package up features to be passed to target/subtarget
64 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000065 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000066
67 // FIXME: We shouldn't need to do this (and link in codegen).
68 // When we split this out, we should do it in a way that makes
69 // it straightforward to switch subtargets on the fly.
Evan Cheng276365d2011-06-30 01:53:36 +000070 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU,
71 FeaturesStr);
Kevin Enderby93f79362011-03-28 18:25:07 +000072 assert(TM && "Unable to create target machine!");
73
74 // Get the target assembler info needed to setup the context.
75 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
76 assert(tai && "Unable to create target assembler!");
77
78 // Set up the MCContext for creating symbols and MCExpr's.
Evan Chenge76a33b2011-07-20 05:58:47 +000079 MCContext *Ctx = new MCContext(*MAI, *MRI, 0, tai);
Kevin Enderby93f79362011-03-28 18:25:07 +000080 assert(Ctx && "Unable to create MCContext!");
81
82 // Set up disassembler.
Kevin Enderbybd332762011-04-11 18:08:50 +000083 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
Kevin Enderby93f79362011-03-28 18:25:07 +000084 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000085 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000086
87 // Set up the instruction printer.
88 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000089 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Kevin Enderby93f79362011-03-28 18:25:07 +000090 *MAI);
91 assert(IP && "Unable to create instruction printer!");
92
93 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
94 GetOpInfo, SymbolLookUp,
Evan Cheng0e6a0522011-07-18 20:57:22 +000095 TheTarget, MAI, MRI, TM, tai,
96 Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000097 assert(DC && "Allocation failure!");
98 return DC;
99}
100
101//
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; }
122 uint64_t getExtent() const { return Size; }
123
124 int readByte(uint64_t Addr, uint8_t *Byte) const {
125 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();
156 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
157 return 0;
158
Chris Lattner9063b552011-05-22 04:53:24 +0000159 SmallVector<char, 64> InsnStr;
160 raw_svector_ostream OS(InsnStr);
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000161 IP->printInst(&Inst, OS);
162 OS.flush();
Kevin Enderby93f79362011-03-28 18:25:07 +0000163
Chris Lattner97ff42d2011-05-22 04:52:24 +0000164 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000165 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
166 std::memcpy(OutString, InsnStr.data(), OutputSize);
167 OutString[OutputSize] = '\0'; // Terminate string.
168
Kevin Enderby93f79362011-03-28 18:25:07 +0000169 return Size;
170}