blob: 95c7b845b49795ccdc009920ecb7fb4d0a028756 [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//===----------------------------------------------------------------------===//
9#include "Disassembler.h"
10#include <stdio.h>
11#include "llvm-c/Disassembler.h"
12
13#include <string>
14#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCDisassembler.h"
16#include "llvm/MC/MCInst.h"
17#include "llvm/MC/MCInstPrinter.h"
18#include "llvm/MC/MCContext.h"
19#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
30#ifdef __cplusplus
31extern "C" {
32#endif // __cplusplus
33
34//
35// LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
36// disassembly is supported by passing a block of information in the DisInfo
37// parameter and specifing the TagType and call back functions as described in
38// the header llvm-c/Disassembler.h . The pointer to the block and the
39// functions can all be passed as NULL. If successfull this returns a
40// disassembler context if not it returns NULL.
41//
42LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
43 int TagType, LLVMOpInfoCallback GetOpInfo,
44 LLVMSymbolLookupCallback SymbolLookUp) {
45 // Initialize targets and assembly printers/parsers.
46 llvm::InitializeAllTargetInfos();
47 // FIXME: We shouldn't need to initialize the Target(Machine)s.
48 llvm::InitializeAllTargets();
49 llvm::InitializeAllAsmPrinters();
50 llvm::InitializeAllAsmParsers();
51 llvm::InitializeAllDisassemblers();
52
53 // Get the target.
54 std::string Error;
55 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
56 assert(TheTarget && "Unable to create target!");
57
58 // Get the assembler info needed to setup the MCContext.
59 const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
60 assert(MAI && "Unable to create target asm info!");
61
62 // Package up features to be passed to target/subtarget
63 std::string FeaturesStr;
64
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.
68 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, FeaturesStr);
69 assert(TM && "Unable to create target machine!");
70
71 // Get the target assembler info needed to setup the context.
72 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
73 assert(tai && "Unable to create target assembler!");
74
75 // Set up the MCContext for creating symbols and MCExpr's.
76 MCContext *Ctx = new MCContext(*MAI, tai);
77 assert(Ctx && "Unable to create MCContext!");
78
79 // Set up disassembler.
Kevin Enderbybd332762011-04-11 18:08:50 +000080 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
Kevin Enderby93f79362011-03-28 18:25:07 +000081 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000082 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000083
84 // Set up the instruction printer.
85 int AsmPrinterVariant = MAI->getAssemblerDialect();
86 MCInstPrinter *IP = TheTarget->createMCInstPrinter(*TM, AsmPrinterVariant,
87 *MAI);
88 assert(IP && "Unable to create instruction printer!");
89
90 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
91 GetOpInfo, SymbolLookUp,
92 TheTarget, MAI, TM, tai, Ctx,
93 DisAsm, IP);
94 assert(DC && "Allocation failure!");
95 return DC;
96}
97
98//
99// LLVMDisasmDispose() disposes of the disassembler specified by the context.
100//
101void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
102 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
103 delete DC;
104}
105
106namespace {
107//
108// The memory object created by LLVMDisasmInstruction().
109//
110class DisasmMemoryObject : public MemoryObject {
111private:
112 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};
129} // namespace
130
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
157 std::string InsnStr;
158 raw_string_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
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000162 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
163 std::memcpy(OutString, InsnStr.data(), OutputSize);
164 OutString[OutputSize] = '\0'; // Terminate string.
165
Kevin Enderby93f79362011-03-28 18:25:07 +0000166 return Size;
167}
168
169#ifdef __cplusplus
170}
171#endif // __cplusplus