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"
21 #include "llvm/Support/ErrorHandling.h"
28 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
29 // disassembly is supported by passing a block of information in the DisInfo
30 // parameter and specifying the TagType and callback functions as described in
31 // the header llvm-c/Disassembler.h . The pointer to the block and the
32 // functions can all be passed as NULL. If successful, this returns a
33 // disassembler context. If not, it returns NULL.
35 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
36 int TagType, LLVMOpInfoCallback GetOpInfo,
37 LLVMSymbolLookupCallback SymbolLookUp) {
40 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
41 assert(TheTarget && "Unable to create target!");
43 // Get the assembler info needed to setup the MCContext.
44 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
45 assert(MAI && "Unable to create target asm info!");
47 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
48 assert(MRI && "Unable to create target register info!");
50 // Package up features to be passed to target/subtarget
51 std::string FeaturesStr;
54 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
56 assert(STI && "Unable to create subtarget info!");
58 // Set up the MCContext for creating symbols and MCExpr's.
59 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
60 assert(Ctx && "Unable to create MCContext!");
62 // Set up disassembler.
63 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
64 assert(DisAsm && "Unable to create disassembler!");
65 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
67 // Set up the instruction printer.
68 int AsmPrinterVariant = MAI->getAssemblerDialect();
69 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
71 assert(IP && "Unable to create instruction printer!");
73 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
74 GetOpInfo, SymbolLookUp,
77 assert(DC && "Allocation failure!");
83 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
85 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
86 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
92 // The memory object created by LLVMDisasmInstruction().
94 class DisasmMemoryObject : public MemoryObject {
99 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
100 Bytes(bytes), Size(size), BasePC(basePC) {}
102 uint64_t getBase() const { return BasePC; }
103 uint64_t getExtent() const { return Size; }
105 int readByte(uint64_t Addr, uint8_t *Byte) const {
106 if (Addr - BasePC >= Size)
108 *Byte = Bytes[Addr - BasePC];
112 } // end anonymous namespace
115 // LLVMDisasmInstruction() disassembles a single instruction using the
116 // disassembler context specified in the parameter DC. The bytes of the
117 // instruction are specified in the parameter Bytes, and contains at least
118 // 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.
126 size_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);
135 const MCDisassembler *DisAsm = DC->getDisAsm();
136 MCInstPrinter *IP = DC->getIP();
137 MCDisassembler::DecodeStatus S;
138 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
139 /*REMOVE*/ nulls(), DC->CommentStream);
141 case MCDisassembler::Fail:
142 case MCDisassembler::SoftFail:
143 // FIXME: Do something different for soft failure modes?
146 case MCDisassembler::Success: {
147 DC->CommentStream.flush();
148 StringRef Comments = DC->CommentsToEmit.str();
150 SmallVector<char, 64> InsnStr;
151 raw_svector_ostream OS(InsnStr);
152 IP->printInst(&Inst, OS, Comments);
155 // Tell the comment stream that the vector changed underneath it.
156 DC->CommentsToEmit.clear();
157 DC->CommentStream.resync();
159 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.
167 llvm_unreachable("Invalid DecodeStatus!");