blob: 5480b4b12b2c1f5a245b750a031519d6732cf989 [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();
Kevin Enderby93f79362011-03-28 18:25:07 +000044 llvm::InitializeAllAsmPrinters();
45 llvm::InitializeAllAsmParsers();
46 llvm::InitializeAllDisassemblers();
47
48 // Get the target.
49 std::string Error;
50 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
51 assert(TheTarget && "Unable to create target!");
52
53 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000054 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000055 assert(MAI && "Unable to create target asm info!");
56
57 // Package up features to be passed to target/subtarget
58 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000059 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000060
61 // FIXME: We shouldn't need to do this (and link in codegen).
62 // When we split this out, we should do it in a way that makes
63 // it straightforward to switch subtargets on the fly.
Evan Cheng276365d2011-06-30 01:53:36 +000064 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU,
65 FeaturesStr);
Kevin Enderby93f79362011-03-28 18:25:07 +000066 assert(TM && "Unable to create target machine!");
67
68 // Get the target assembler info needed to setup the context.
69 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
70 assert(tai && "Unable to create target assembler!");
71
72 // Set up the MCContext for creating symbols and MCExpr's.
73 MCContext *Ctx = new MCContext(*MAI, tai);
74 assert(Ctx && "Unable to create MCContext!");
75
76 // Set up disassembler.
Kevin Enderbybd332762011-04-11 18:08:50 +000077 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
Kevin Enderby93f79362011-03-28 18:25:07 +000078 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000079 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000080
81 // Set up the instruction printer.
82 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000083 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Kevin Enderby93f79362011-03-28 18:25:07 +000084 *MAI);
85 assert(IP && "Unable to create instruction printer!");
86
87 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
88 GetOpInfo, SymbolLookUp,
89 TheTarget, MAI, TM, tai, Ctx,
90 DisAsm, IP);
91 assert(DC && "Allocation failure!");
92 return DC;
93}
94
95//
96// LLVMDisasmDispose() disposes of the disassembler specified by the context.
97//
98void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
99 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
100 delete DC;
101}
102
103namespace {
104//
105// The memory object created by LLVMDisasmInstruction().
106//
107class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +0000108 uint8_t *Bytes;
109 uint64_t Size;
110 uint64_t BasePC;
111public:
112 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
113 Bytes(bytes), Size(size), BasePC(basePC) {}
114
115 uint64_t getBase() const { return BasePC; }
116 uint64_t getExtent() const { return Size; }
117
118 int readByte(uint64_t Addr, uint8_t *Byte) const {
119 if (Addr - BasePC >= Size)
120 return -1;
121 *Byte = Bytes[Addr - BasePC];
122 return 0;
123 }
124};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000125} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000126
127//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000128// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000129// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000130// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000131// BytesSize number of bytes. The instruction is at the address specified by
132// the PC parameter. If a valid instruction can be disassembled its string is
133// returned indirectly in OutString which whos size is specified in the
134// parameter OutStringSize. This function returns the number of bytes in the
135// instruction or zero if there was no valid instruction. If this function
136// returns zero the caller will have to pick how many bytes they want to step
137// over by printing a .byte, .long etc. to continue.
138//
139size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
140 uint64_t BytesSize, uint64_t PC, char *OutString,
141 size_t OutStringSize){
142 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
143 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
144 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
145
146 uint64_t Size;
147 MCInst Inst;
148 const MCDisassembler *DisAsm = DC->getDisAsm();
149 MCInstPrinter *IP = DC->getIP();
150 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
151 return 0;
152
Chris Lattner9063b552011-05-22 04:53:24 +0000153 SmallVector<char, 64> InsnStr;
154 raw_svector_ostream OS(InsnStr);
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000155 IP->printInst(&Inst, OS);
156 OS.flush();
Kevin Enderby93f79362011-03-28 18:25:07 +0000157
Chris Lattner97ff42d2011-05-22 04:52:24 +0000158 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000159 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
160 std::memcpy(OutString, InsnStr.data(), OutputSize);
161 OutString[OutputSize] = '\0'; // Terminate string.
162
Kevin Enderby93f79362011-03-28 18:25:07 +0000163 return Size;
164}