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/Support/ErrorHandling.h"
22 #include "llvm/Support/MemoryObject.h"
23 #include "llvm/Support/TargetRegistry.h"
30 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
31 // disassembly is supported by passing a block of information in the DisInfo
32 // parameter and specifying the TagType and callback functions as described in
33 // the header llvm-c/Disassembler.h . The pointer to the block and the
34 // functions can all be passed as NULL. If successful, this returns a
35 // disassembler context. If not, it returns NULL.
37 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
38 void *DisInfo, int TagType,
39 LLVMOpInfoCallback GetOpInfo,
40 LLVMSymbolLookupCallback SymbolLookUp){
43 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
47 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
51 // Get the assembler info needed to setup the MCContext.
52 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
56 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
60 // Package up features to be passed to target/subtarget
61 std::string FeaturesStr;
63 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
68 // Set up the MCContext for creating symbols and MCExpr's.
69 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
73 // Set up disassembler.
74 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
78 OwningPtr<MCRelocationInfo> RelInfo(
79 TheTarget->createMCRelocationInfo(Triple, *Ctx));
83 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo,
86 // Set up the instruction printer.
87 int AsmPrinterVariant = MAI->getAssemblerDialect();
88 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
89 *MAI, *MII, *MRI, *STI);
93 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
94 GetOpInfo, SymbolLookUp,
96 STI, MII, Ctx, DisAsm, IP);
103 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
104 int TagType, LLVMOpInfoCallback GetOpInfo,
105 LLVMSymbolLookupCallback SymbolLookUp) {
106 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
111 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
113 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
114 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
120 // The memory object created by LLVMDisasmInstruction().
122 class DisasmMemoryObject : public MemoryObject {
127 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
128 Bytes(bytes), Size(size), BasePC(basePC) {}
130 uint64_t getBase() const { return BasePC; }
131 uint64_t getExtent() const { return Size; }
133 int readByte(uint64_t Addr, uint8_t *Byte) const {
134 if (Addr - BasePC >= Size)
136 *Byte = Bytes[Addr - BasePC];
140 } // end anonymous namespace
143 // LLVMDisasmInstruction() disassembles a single instruction using the
144 // disassembler context specified in the parameter DC. The bytes of the
145 // instruction are specified in the parameter Bytes, and contains at least
146 // BytesSize number of bytes. The instruction is at the address specified by
147 // the PC parameter. If a valid instruction can be disassembled its string is
148 // returned indirectly in OutString which whos size is specified in the
149 // parameter OutStringSize. This function returns the number of bytes in the
150 // instruction or zero if there was no valid instruction. If this function
151 // returns zero the caller will have to pick how many bytes they want to step
152 // over by printing a .byte, .long etc. to continue.
154 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
155 uint64_t BytesSize, uint64_t PC, char *OutString,
156 size_t OutStringSize){
157 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
158 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
159 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
163 const MCDisassembler *DisAsm = DC->getDisAsm();
164 MCInstPrinter *IP = DC->getIP();
165 MCDisassembler::DecodeStatus S;
166 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
167 /*REMOVE*/ nulls(), DC->CommentStream);
169 case MCDisassembler::Fail:
170 case MCDisassembler::SoftFail:
171 // FIXME: Do something different for soft failure modes?
174 case MCDisassembler::Success: {
175 DC->CommentStream.flush();
176 StringRef Comments = DC->CommentsToEmit.str();
178 SmallVector<char, 64> InsnStr;
179 raw_svector_ostream OS(InsnStr);
180 IP->printInst(&Inst, OS, Comments);
183 // Tell the comment stream that the vector changed underneath it.
184 DC->CommentsToEmit.clear();
185 DC->CommentStream.resync();
187 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
188 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
189 std::memcpy(OutString, InsnStr.data(), OutputSize);
190 OutString[OutputSize] = '\0'; // Terminate string.
195 llvm_unreachable("Invalid DecodeStatus!");
199 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
200 // can set all the Options and 0 otherwise.
202 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
203 if (Options & LLVMDisassembler_Option_UseMarkup){
204 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
205 MCInstPrinter *IP = DC->getIP();
207 Options &= ~LLVMDisassembler_Option_UseMarkup;
209 if (Options & LLVMDisassembler_Option_PrintImmHex){
210 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
211 MCInstPrinter *IP = DC->getIP();
212 IP->setPrintImmHex(1);
213 Options &= ~LLVMDisassembler_Option_PrintImmHex;
215 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
216 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
217 // Try to set up the new instruction printer.
218 const MCAsmInfo *MAI = DC->getAsmInfo();
219 const MCInstrInfo *MII = DC->getInstrInfo();
220 const MCRegisterInfo *MRI = DC->getRegisterInfo();
221 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
222 int AsmPrinterVariant = MAI->getAssemblerDialect();
223 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
224 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
225 AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
228 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
231 return (Options == 0);