blob: 858a58cf35e988da5ee358f403d2147330e10803 [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"
21#include "llvm/Support/TargetSelect.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) {
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::InitializeAllAsmParsers();
42 llvm::InitializeAllDisassemblers();
43
44 // Get the target.
45 std::string Error;
46 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
47 assert(TheTarget && "Unable to create target!");
48
49 // Get the assembler info needed to setup the MCContext.
Evan Cheng1abf2cb2011-07-14 23:50:31 +000050 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
Kevin Enderby93f79362011-03-28 18:25:07 +000051 assert(MAI && "Unable to create target asm info!");
52
Evan Cheng0e6a0522011-07-18 20:57:22 +000053 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
54 assert(MRI && "Unable to create target register info!");
55
Kevin Enderby93f79362011-03-28 18:25:07 +000056 // Package up features to be passed to target/subtarget
57 std::string FeaturesStr;
Evan Cheng276365d2011-06-30 01:53:36 +000058 std::string CPU;
Kevin Enderby93f79362011-03-28 18:25:07 +000059
James Molloyb9505852011-09-07 17:24:38 +000060 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
61 FeaturesStr);
62 assert(STI && "Unable to create subtarget info!");
63
Kevin Enderby93f79362011-03-28 18:25:07 +000064 // Set up the MCContext for creating symbols and MCExpr's.
Evan Cheng203576a2011-07-20 19:50:42 +000065 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
Kevin Enderby93f79362011-03-28 18:25:07 +000066 assert(Ctx && "Unable to create MCContext!");
67
68 // Set up disassembler.
James Molloyb9505852011-09-07 17:24:38 +000069 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000070 assert(DisAsm && "Unable to create disassembler!");
Kevin Enderbybd332762011-04-11 18:08:50 +000071 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
Kevin Enderby93f79362011-03-28 18:25:07 +000072
73 // Set up the instruction printer.
74 int AsmPrinterVariant = MAI->getAssemblerDialect();
Evan Chengb2627992011-07-06 19:45:42 +000075 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
James Molloyb9505852011-09-07 17:24:38 +000076 *MAI, *STI);
Kevin Enderby93f79362011-03-28 18:25:07 +000077 assert(IP && "Unable to create instruction printer!");
78
79 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
80 GetOpInfo, SymbolLookUp,
Evan Cheng203576a2011-07-20 19:50:42 +000081 TheTarget, MAI, MRI,
Evan Cheng0e6a0522011-07-18 20:57:22 +000082 Ctx, DisAsm, IP);
Kevin Enderby93f79362011-03-28 18:25:07 +000083 assert(DC && "Allocation failure!");
Owen Anderson8f29e692011-09-15 18:37:20 +000084
85 IP->setCommentStream(DC->CommentStream);
86
Kevin Enderby93f79362011-03-28 18:25:07 +000087 return DC;
88}
89
90//
91// LLVMDisasmDispose() disposes of the disassembler specified by the context.
92//
93void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
94 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
95 delete DC;
96}
97
98namespace {
99//
100// The memory object created by LLVMDisasmInstruction().
101//
102class DisasmMemoryObject : public MemoryObject {
Kevin Enderby93f79362011-03-28 18:25:07 +0000103 uint8_t *Bytes;
104 uint64_t Size;
105 uint64_t BasePC;
106public:
107 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
108 Bytes(bytes), Size(size), BasePC(basePC) {}
109
110 uint64_t getBase() const { return BasePC; }
111 uint64_t getExtent() const { return Size; }
112
113 int readByte(uint64_t Addr, uint8_t *Byte) const {
114 if (Addr - BasePC >= Size)
115 return -1;
116 *Byte = Bytes[Addr - BasePC];
117 return 0;
118 }
119};
Chris Lattner97ff42d2011-05-22 04:52:24 +0000120} // end anonymous namespace
Kevin Enderby93f79362011-03-28 18:25:07 +0000121
122//
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000123// LLVMDisasmInstruction() disassembles a single instruction using the
Kevin Enderby93f79362011-03-28 18:25:07 +0000124// disassembler context specified in the parameter DC. The bytes of the
Benjamin Kramer5731ff62011-04-09 14:06:12 +0000125// instruction are specified in the parameter Bytes, and contains at least
Kevin Enderby93f79362011-03-28 18:25:07 +0000126// BytesSize number of bytes. The instruction is at the address specified by
127// the PC parameter. If a valid instruction can be disassembled its string is
128// returned indirectly in OutString which whos size is specified in the
129// parameter OutStringSize. This function returns the number of bytes in the
130// instruction or zero if there was no valid instruction. If this function
131// returns zero the caller will have to pick how many bytes they want to step
132// over by printing a .byte, .long etc. to continue.
133//
134size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
135 uint64_t BytesSize, uint64_t PC, char *OutString,
136 size_t OutStringSize){
137 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
138 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
139 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
140
141 uint64_t Size;
142 MCInst Inst;
143 const MCDisassembler *DisAsm = DC->getDisAsm();
144 MCInstPrinter *IP = DC->getIP();
James Molloyee064432011-09-01 22:01:14 +0000145 MCDisassembler::DecodeStatus S;
146 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
147 /*REMOVE*/ nulls());
148 switch (S) {
149 case MCDisassembler::Fail:
150 case MCDisassembler::SoftFail:
James Molloyc047dca2011-09-01 18:02:14 +0000151 // FIXME: Do something different for soft failure modes?
Kevin Enderby93f79362011-03-28 18:25:07 +0000152 return 0;
James Molloyee064432011-09-01 22:01:14 +0000153
154 case MCDisassembler::Success: {
155 SmallVector<char, 64> InsnStr;
156 raw_svector_ostream OS(InsnStr);
157 IP->printInst(&Inst, OS);
158 OS.flush();
159
Owen Anderson8f29e692011-09-15 18:37:20 +0000160 DC->CommentStream.flush();
161 assert(DC->CommentsToEmit.back() == '\n');
162
163 DC->CommentsToEmit.push_back('\n');
164 StringRef Comments = DC->CommentsToEmit.str();
165
166 do {
167 // Emit a line of comments.
168 size_t Position = Comments.find('\n');
169 OS << ' ' << DC->getAsmInfo()->getCommentString()
170 << ' ' << Comments.substr(0, Position) << '\n';
171
172 Comments = Comments.substr(Position+1);
173 } while (!Comments.empty());
174
175 DC->CommentsToEmit.clear();
176 // Tell the comment stream that the vector changed underneath it.
177 DC->CommentStream.resync();
178
James Molloyee064432011-09-01 22:01:14 +0000179 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
180 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
181 std::memcpy(OutString, InsnStr.data(), OutputSize);
182 OutString[OutputSize] = '\0'; // Terminate string.
183
184 return Size;
James Molloyc047dca2011-09-01 18:02:14 +0000185 }
James Molloyee064432011-09-01 22:01:14 +0000186 }
187 return 0;
Kevin Enderby93f79362011-03-28 18:25:07 +0000188}