blob: 4c92c57a2576ad01e323ed43e4fb4ad88b450ec8 [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 Cheng1abf2cb2011-07-14 23:50:31 +000040 llvm::InitializeAllMCAsmInfos();
Evan Cheng43966132011-07-19 06:37:02 +000041 llvm::InitializeAllMCCodeGenInfos();
Evan Cheng0e6a0522011-07-18 20:57:22 +000042 llvm::InitializeAllMCRegisterInfos();
Kevin Enderby93f79362011-03-28 18:25:07 +000043 llvm::InitializeAllAsmPrinters();
44 llvm::InitializeAllAsmParsers();
45 llvm::InitializeAllDisassemblers();
46
47 // Get the target.
48 std::string Error;
49 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
50 assert(TheTarget && "Unable to create target!");
51
52 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000053 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000054 assert(MAI && "Unable to create target asm info!");
55
Evan Cheng0e6a0522011-07-18 20:57:22 +000056 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
57 assert(MRI && "Unable to create target register info!");
58
Kevin Enderby93f79362011-03-28 18:25:07 +000059 // Package up features to be passed to target/subtarget
60 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000061 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000062
Kevin Enderby93f79362011-03-28 18:25:07 +000063 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000064 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby93f79362011-03-28 18:25:07 +000065 assert(Ctx && "Unable to create MCContext!");
66
67 // Set up disassembler.
Kevin Enderbybd332762011-04-11 18:08:50 +000068 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
Kevin Enderby93f79362011-03-28 18:25:07 +000069 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000070 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000071
72 // Set up the instruction printer.
73 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000074 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
Kevin Enderby93f79362011-03-28 18:25:07 +000075 *MAI);
76 assert(IP && "Unable to create instruction printer!");
77
78 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
79 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000080 TheTarget, MAI, MRI,
Evan Cheng0e6a0522011-07-18 20:57:22 +000081 Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000082 assert(DC && "Allocation failure!");
83 return DC;
84}
85
86//
87// LLVMDisasmDispose() disposes of the disassembler specified by the context.
88//
89void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
90 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
91 delete DC;
92}
93
94namespace {
95//
96// The memory object created by LLVMDisasmInstruction().
97//
98class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +000099 uint8_t *Bytes;
100 uint64_t Size;
101 uint64_t BasePC;
102public:
103 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
104 Bytes(bytes), Size(size), BasePC(basePC) {}
105
106 uint64_t getBase() const { return BasePC; }
107 uint64_t getExtent() const { return Size; }
108
109 int readByte(uint64_t Addr, uint8_t *Byte) const {
110 if (Addr - BasePC >= Size)
111 return -1;
112 *Byte = Bytes[Addr - BasePC];
113 return 0;
114 }
115};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000116} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000117
118//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000119// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000120// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000121// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000122// BytesSize number of bytes. The instruction is at the address specified by
123// the PC parameter. If a valid instruction can be disassembled its string is
124// returned indirectly in OutString which whos size is specified in the
125// parameter OutStringSize. This function returns the number of bytes in the
126// instruction or zero if there was no valid instruction. If this function
127// returns zero the caller will have to pick how many bytes they want to step
128// over by printing a .byte, .long etc. to continue.
129//
130size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
131 uint64_t BytesSize, uint64_t PC, char *OutString,
132 size_t OutStringSize){
133 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
134 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
135 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
136
137 uint64_t Size;
138 MCInst Inst;
139 const MCDisassembler *DisAsm = DC->getDisAsm();
140 MCInstPrinter *IP = DC->getIP();
141 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
142 return 0;
143
Chris Lattner9063b552011-05-22 04:53:24 +0000144 SmallVector<char, 64> InsnStr;
145 raw_svector_ostream OS(InsnStr);
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000146 IP->printInst(&Inst, OS);
147 OS.flush();
Kevin Enderby93f79362011-03-28 18:25:07 +0000148
Chris Lattner97ff42d2011-05-22 04:52:24 +0000149 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000150 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
151 std::memcpy(OutString, InsnStr.data(), OutputSize);
152 OutString[OutputSize] = '\0'; // Terminate string.
153
Kevin Enderby93f79362011-03-28 18:25:07 +0000154 return Size;
155}