blob: 9eb99d28e725ee24c45517f53901014934885ace [file] [log] [blame]
Nick Lewycky833a0032011-09-01 21:09:04 +00001//===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
Kevin Enderby93f79362011-03-28 18:25:07 +00002//
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/Support/MemoryObject.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000020#include "llvm/Support/TargetRegistry.h"
David Blaikie4d6ccb52012-01-20 21:51:11 +000021#include "llvm/Support/ErrorHandling.h"
Kevin Enderby93f79362011-03-28 18:25:07 +000022
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) {
Kevin Enderby93f79362011-03-28 18:25:07 +000038 // Get the target.
39 std::string Error;
40 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
41 assert(TheTarget && "Unable to create target!");
42
43 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000044 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000045 assert(MAI && "Unable to create target asm info!");
46
Evan Cheng0e6a0522011-07-18 20:57:22 +000047 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
48 assert(MRI && "Unable to create target register info!");
49
Kevin Enderby93f79362011-03-28 18:25:07 +000050 // Package up features to be passed to target/subtarget
51 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000052 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000053
James Molloyb9505852011-09-07 17:24:38 +000054 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
55 FeaturesStr);
56 assert(STI && "Unable to create subtarget info!");
57
Kevin Enderby93f79362011-03-28 18:25:07 +000058 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000059 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby93f79362011-03-28 18:25:07 +000060 assert(Ctx && "Unable to create MCContext!");
61
62 // Set up disassembler.
James Molloyb9505852011-09-07 17:24:38 +000063 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000064 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderby9e5887b2011-10-04 22:44:48 +000065 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000066
67 // Set up the instruction printer.
68 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000069 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
James Molloyb9505852011-09-07 17:24:38 +000070 *MAI, *STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000071 assert(IP && "Unable to create instruction printer!");
72
73 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
74 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000075 TheTarget, MAI, MRI,
Evan Cheng0e6a0522011-07-18 20:57:22 +000076 Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000077 assert(DC && "Allocation failure!");
Owen Anderson8f29e692011-09-15 18:37:20 +000078
Kevin Enderby93f79362011-03-28 18:25:07 +000079 return DC;
80}
81
82//
83// LLVMDisasmDispose() disposes of the disassembler specified by the context.
84//
85void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
86 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
87 delete DC;
88}
89
90namespace {
91//
92// The memory object created by LLVMDisasmInstruction().
93//
94class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +000095 uint8_t *Bytes;
96 uint64_t Size;
97 uint64_t BasePC;
98public:
99 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
100 Bytes(bytes), Size(size), BasePC(basePC) {}
101
102 uint64_t getBase() const { return BasePC; }
103 uint64_t getExtent() const { return Size; }
104
105 int readByte(uint64_t Addr, uint8_t *Byte) const {
106 if (Addr - BasePC >= Size)
107 return -1;
108 *Byte = Bytes[Addr - BasePC];
109 return 0;
110 }
111};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000112} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000113
114//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000115// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000116// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000117// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000118// BytesSize number of bytes. The instruction is at the address specified by
119// the PC parameter. If a valid instruction can be disassembled its string is
120// returned indirectly in OutString which whos size is specified in the
121// parameter OutStringSize. This function returns the number of bytes in the
122// instruction or zero if there was no valid instruction. If this function
123// returns zero the caller will have to pick how many bytes they want to step
124// over by printing a .byte, .long etc. to continue.
125//
126size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
127 uint64_t BytesSize, uint64_t PC, char *OutString,
128 size_t OutStringSize){
129 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
130 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
131 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
132
133 uint64_t Size;
134 MCInst Inst;
135 const MCDisassembler *DisAsm = DC->getDisAsm();
136 MCInstPrinter *IP = DC->getIP();
James Molloyee064432011-09-01 22:01:14 +0000137 MCDisassembler::DecodeStatus S;
138 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
Owen Anderson98c5dda2011-09-15 23:38:46 +0000139 /*REMOVE*/ nulls(), DC->CommentStream);
James Molloyee064432011-09-01 22:01:14 +0000140 switch (S) {
141 case MCDisassembler::Fail:
142 case MCDisassembler::SoftFail:
James Molloyc047dca2011-09-01 18:02:14 +0000143 // FIXME: Do something different for soft failure modes?
Kevin Enderby93f79362011-03-28 18:25:07 +0000144 return 0;
James Molloyee064432011-09-01 22:01:14 +0000145
146 case MCDisassembler::Success: {
Owen Anderson8f29e692011-09-15 18:37:20 +0000147 DC->CommentStream.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000148 StringRef Comments = DC->CommentsToEmit.str();
149
Owen Anderson98c5dda2011-09-15 23:38:46 +0000150 SmallVector<char, 64> InsnStr;
151 raw_svector_ostream OS(InsnStr);
152 IP->printInst(&Inst, OS, Comments);
153 OS.flush();
Owen Anderson8f29e692011-09-15 18:37:20 +0000154
Owen Anderson8f29e692011-09-15 18:37:20 +0000155 // Tell the comment stream that the vector changed underneath it.
Owen Anderson98c5dda2011-09-15 23:38:46 +0000156 DC->CommentsToEmit.clear();
Owen Anderson8f29e692011-09-15 18:37:20 +0000157 DC->CommentStream.resync();
158
James Molloyee064432011-09-01 22:01:14 +0000159 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
160 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
161 std::memcpy(OutString, InsnStr.data(), OutputSize);
162 OutString[OutputSize] = '\0'; // Terminate string.
163
164 return Size;
James Molloyc047dca2011-09-01 18:02:14 +0000165 }
James Molloyee064432011-09-01 22:01:14 +0000166 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000167 llvm_unreachable("Invalid DecodeStatus!");
Kevin Enderby93f79362011-03-28 18:25:07 +0000168}