1 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Disassembler.h"
11 #include "llvm-c/Disassembler.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDisassembler.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/Support/MemoryObject.h"
20 #include "llvm/Support/TargetRegistry.h"
27 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
28 // disassembly is supported by passing a block of information in the DisInfo
29 // parameter and specifying the TagType and callback functions as described in
30 // the header llvm-c/Disassembler.h . The pointer to the block and the
31 // functions can all be passed as NULL. If successful, this returns a
32 // disassembler context. If not, it returns NULL.
34 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
35 int TagType, LLVMOpInfoCallback GetOpInfo,
36 LLVMSymbolLookupCallback SymbolLookUp) {
39 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
40 assert(TheTarget && "Unable to create target!");
42 // Get the assembler info needed to setup the MCContext.
43 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
44 assert(MAI && "Unable to create target asm info!");
46 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
47 assert(MRI && "Unable to create target register info!");
49 // Package up features to be passed to target/subtarget
50 std::string FeaturesStr;
53 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
55 assert(STI && "Unable to create subtarget info!");
57 // Set up the MCContext for creating symbols and MCExpr's.
58 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
59 assert(Ctx && "Unable to create MCContext!");
61 // Set up disassembler.
62 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
63 assert(DisAsm && "Unable to create disassembler!");
64 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
66 // Set up the instruction printer.
67 int AsmPrinterVariant = MAI->getAssemblerDialect();
68 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
70 assert(IP && "Unable to create instruction printer!");
72 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
73 GetOpInfo, SymbolLookUp,
76 assert(DC && "Allocation failure!");
82 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
84 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
85 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
91 // The memory object created by LLVMDisasmInstruction().
93 class DisasmMemoryObject : public MemoryObject {
98 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
99 Bytes(bytes), Size(size), BasePC(basePC) {}
101 uint64_t getBase() const { return BasePC; }
102 uint64_t getExtent() const { return Size; }
104 int readByte(uint64_t Addr, uint8_t *Byte) const {
105 if (Addr - BasePC >= Size)
107 *Byte = Bytes[Addr - BasePC];
111 } // end anonymous namespace
114 // LLVMDisasmInstruction() disassembles a single instruction using the
115 // disassembler context specified in the parameter DC. The bytes of the
116 // instruction are specified in the parameter Bytes, and contains at least
117 // BytesSize number of bytes. The instruction is at the address specified by
118 // the PC parameter. If a valid instruction can be disassembled its string is
119 // returned indirectly in OutString which whos size is specified in the
120 // parameter OutStringSize. This function returns the number of bytes in the
121 // instruction or zero if there was no valid instruction. If this function
122 // returns zero the caller will have to pick how many bytes they want to step
123 // over by printing a .byte, .long etc. to continue.
125 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
126 uint64_t BytesSize, uint64_t PC, char *OutString,
127 size_t OutStringSize){
128 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
129 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
130 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
134 const MCDisassembler *DisAsm = DC->getDisAsm();
135 MCInstPrinter *IP = DC->getIP();
136 MCDisassembler::DecodeStatus S;
137 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
138 /*REMOVE*/ nulls(), DC->CommentStream);
140 case MCDisassembler::Fail:
141 case MCDisassembler::SoftFail:
142 // FIXME: Do something different for soft failure modes?
145 case MCDisassembler::Success: {
146 DC->CommentStream.flush();
147 StringRef Comments = DC->CommentsToEmit.str();
149 SmallVector<char, 64> InsnStr;
150 raw_svector_ostream OS(InsnStr);
151 IP->printInst(&Inst, OS, Comments);
154 // Tell the comment stream that the vector changed underneath it.
155 DC->CommentsToEmit.clear();
156 DC->CommentStream.resync();
158 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
159 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
160 std::memcpy(OutString, InsnStr.data(), OutputSize);
161 OutString[OutputSize] = '\0'; // Terminate string.