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"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDisassembler.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCInstPrinter.h"
17 #include "llvm/MC/MCInstrInfo.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCRelocationInfo.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbolizer.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MemoryObject.h"
24 #include "llvm/Support/TargetRegistry.h"
31 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
32 // disassembly is supported by passing a block of information in the DisInfo
33 // parameter and specifying the TagType and callback functions as described in
34 // the header llvm-c/Disassembler.h . The pointer to the block and the
35 // functions can all be passed as NULL. If successful, this returns a
36 // disassembler context. If not, it returns NULL.
38 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
39 void *DisInfo, int TagType,
40 LLVMOpInfoCallback GetOpInfo,
41 LLVMSymbolLookupCallback SymbolLookUp){
44 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
48 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
52 // Get the assembler info needed to setup the MCContext.
53 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
57 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
61 // Package up features to be passed to target/subtarget
62 std::string FeaturesStr;
64 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
69 // Set up the MCContext for creating symbols and MCExpr's.
70 MCContext *Ctx = new MCContext(MAI, MRI, 0);
74 // Set up disassembler.
75 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
79 OwningPtr<MCRelocationInfo> RelInfo(
80 TheTarget->createMCRelocationInfo(Triple, *Ctx));
84 OwningPtr<MCSymbolizer> Symbolizer(
85 TheTarget->createMCSymbolizer(Triple, GetOpInfo, SymbolLookUp, DisInfo,
86 Ctx, RelInfo.take()));
87 DisAsm->setSymbolizer(Symbolizer);
88 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo,
90 // Set up the instruction printer.
91 int AsmPrinterVariant = MAI->getAssemblerDialect();
92 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
93 *MAI, *MII, *MRI, *STI);
97 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
98 GetOpInfo, SymbolLookUp,
100 STI, MII, Ctx, DisAsm, IP);
107 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
108 int TagType, LLVMOpInfoCallback GetOpInfo,
109 LLVMSymbolLookupCallback SymbolLookUp) {
110 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
115 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
117 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
118 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
124 // The memory object created by LLVMDisasmInstruction().
126 class DisasmMemoryObject : public MemoryObject {
131 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
132 Bytes(bytes), Size(size), BasePC(basePC) {}
134 uint64_t getBase() const { return BasePC; }
135 uint64_t getExtent() const { return Size; }
137 int readByte(uint64_t Addr, uint8_t *Byte) const {
138 if (Addr - BasePC >= Size)
140 *Byte = Bytes[Addr - BasePC];
144 } // end anonymous namespace
147 // LLVMDisasmInstruction() disassembles a single instruction using the
148 // disassembler context specified in the parameter DC. The bytes of the
149 // instruction are specified in the parameter Bytes, and contains at least
150 // BytesSize number of bytes. The instruction is at the address specified by
151 // the PC parameter. If a valid instruction can be disassembled its string is
152 // returned indirectly in OutString which whos size is specified in the
153 // parameter OutStringSize. This function returns the number of bytes in the
154 // instruction or zero if there was no valid instruction. If this function
155 // returns zero the caller will have to pick how many bytes they want to step
156 // over by printing a .byte, .long etc. to continue.
158 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
159 uint64_t BytesSize, uint64_t PC, char *OutString,
160 size_t OutStringSize){
161 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
162 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
163 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
167 const MCDisassembler *DisAsm = DC->getDisAsm();
168 MCInstPrinter *IP = DC->getIP();
169 MCDisassembler::DecodeStatus S;
170 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
171 /*REMOVE*/ nulls(), DC->CommentStream);
173 case MCDisassembler::Fail:
174 case MCDisassembler::SoftFail:
175 // FIXME: Do something different for soft failure modes?
178 case MCDisassembler::Success: {
179 DC->CommentStream.flush();
180 StringRef Comments = DC->CommentsToEmit.str();
182 SmallVector<char, 64> InsnStr;
183 raw_svector_ostream OS(InsnStr);
184 IP->printInst(&Inst, OS, Comments);
187 // Tell the comment stream that the vector changed underneath it.
188 DC->CommentsToEmit.clear();
189 DC->CommentStream.resync();
191 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
192 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
193 std::memcpy(OutString, InsnStr.data(), OutputSize);
194 OutString[OutputSize] = '\0'; // Terminate string.
199 llvm_unreachable("Invalid DecodeStatus!");
203 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
204 // can set all the Options and 0 otherwise.
206 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
207 if (Options & LLVMDisassembler_Option_UseMarkup){
208 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
209 MCInstPrinter *IP = DC->getIP();
211 Options &= ~LLVMDisassembler_Option_UseMarkup;
213 if (Options & LLVMDisassembler_Option_PrintImmHex){
214 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
215 MCInstPrinter *IP = DC->getIP();
216 IP->setPrintImmHex(1);
217 Options &= ~LLVMDisassembler_Option_PrintImmHex;
219 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
220 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
221 // Try to set up the new instruction printer.
222 const MCAsmInfo *MAI = DC->getAsmInfo();
223 const MCInstrInfo *MII = DC->getInstrInfo();
224 const MCRegisterInfo *MRI = DC->getRegisterInfo();
225 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
226 int AsmPrinterVariant = MAI->getAssemblerDialect();
227 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
228 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
229 AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
232 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
235 return (Options == 0);