blob: dd20f63193af0888c2b211b0f68636a1bb0149bc [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"
Kevin Enderby93f79362011-03-28 18:25:07 +000020#include "llvm/Target/TargetSelect.h"
21#include "llvm/Support/MemoryObject.h"
22
23namespace llvm {
24class Target;
25} // namespace llvm
26using namespace llvm;
27
Kevin Enderby93f79362011-03-28 18:25:07 +000028// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
29// disassembly is supported by passing a block of information in the DisInfo
Chris Lattner97ff42d2011-05-22 04:52:24 +000030// parameter and specifying the TagType and callback functions as described in
Kevin Enderby93f79362011-03-28 18:25:07 +000031// the header llvm-c/Disassembler.h . The pointer to the block and the
Chris Lattner97ff42d2011-05-22 04:52:24 +000032// functions can all be passed as NULL. If successful, this returns a
33// disassembler context. If not, it returns NULL.
Kevin Enderby93f79362011-03-28 18:25:07 +000034//
35LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
36 int TagType, LLVMOpInfoCallback GetOpInfo,
37 LLVMSymbolLookupCallback SymbolLookUp) {
38 // Initialize targets and assembly printers/parsers.
39 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +000040 llvm::InitializeAllTargetMCs();
Kevin Enderby93f79362011-03-28 18:25:07 +000041 llvm::InitializeAllAsmPrinters();
42 llvm::InitializeAllAsmParsers();
43 llvm::InitializeAllDisassemblers();
44
45 // Get the target.
46 std::string Error;
47 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
48 assert(TheTarget && "Unable to create target!");
49
50 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000051 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000052 assert(MAI && "Unable to create target asm info!");
53
Evan Cheng0e6a0522011-07-18 20:57:22 +000054 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
55 assert(MRI && "Unable to create target register info!");
56
Kevin Enderby93f79362011-03-28 18:25:07 +000057 // 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
Kevin Enderby93f79362011-03-28 18:25:07 +000061 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000062 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby93f79362011-03-28 18:25:07 +000063 assert(Ctx && "Unable to create MCContext!");
64
65 // Set up disassembler.
Kevin Enderbybd332762011-04-11 18:08:50 +000066 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
Kevin Enderby93f79362011-03-28 18:25:07 +000067 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000068 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000069
70 // Set up the instruction printer.
71 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000072 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Kevin Enderby93f79362011-03-28 18:25:07 +000073 *MAI);
74 assert(IP && "Unable to create instruction printer!");
75
76 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
77 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000078 TheTarget, MAI, MRI,
Evan Cheng0e6a0522011-07-18 20:57:22 +000079 Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000080 assert(DC && "Allocation failure!");
81 return DC;
82}
83
84//
85// LLVMDisasmDispose() disposes of the disassembler specified by the context.
86//
87void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
88 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
89 delete DC;
90}
91
92namespace {
93//
94// The memory object created by LLVMDisasmInstruction().
95//
96class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +000097 uint8_t *Bytes;
98 uint64_t Size;
99 uint64_t BasePC;
100public:
101 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
102 Bytes(bytes), Size(size), BasePC(basePC) {}
103
104 uint64_t getBase() const { return BasePC; }
105 uint64_t getExtent() const { return Size; }
106
107 int readByte(uint64_t Addr, uint8_t *Byte) const {
108 if (Addr - BasePC >= Size)
109 return -1;
110 *Byte = Bytes[Addr - BasePC];
111 return 0;
112 }
113};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000114} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000115
116//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000117// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000118// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000119// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000120// BytesSize number of bytes. The instruction is at the address specified by
121// the PC parameter. If a valid instruction can be disassembled its string is
122// returned indirectly in OutString which whos size is specified in the
123// parameter OutStringSize. This function returns the number of bytes in the
124// instruction or zero if there was no valid instruction. If this function
125// returns zero the caller will have to pick how many bytes they want to step
126// over by printing a .byte, .long etc. to continue.
127//
128size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
129 uint64_t BytesSize, uint64_t PC, char *OutString,
130 size_t OutStringSize){
131 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
132 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
133 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
134
135 uint64_t Size;
136 MCInst Inst;
137 const MCDisassembler *DisAsm = DC->getDisAsm();
138 MCInstPrinter *IP = DC->getIP();
139 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
140 return 0;
141
Chris Lattner9063b552011-05-22 04:53:24 +0000142 SmallVector<char, 64> InsnStr;
143 raw_svector_ostream OS(InsnStr);
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000144 IP->printInst(&Inst, OS);
145 OS.flush();
Kevin Enderby93f79362011-03-28 18:25:07 +0000146
Chris Lattner97ff42d2011-05-22 04:52:24 +0000147 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000148 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
149 std::memcpy(OutString, InsnStr.data(), OutputSize);
150 OutString[OutputSize] = '\0'; // Terminate string.
151
Kevin Enderby93f79362011-03-28 18:25:07 +0000152 return Size;
153}