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/MCSubtargetInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MemoryObject.h"
22 #include "llvm/Support/TargetRegistry.h"
29 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
30 // disassembly is supported by passing a block of information in the DisInfo
31 // parameter and specifying the TagType and callback functions as described in
32 // the header llvm-c/Disassembler.h . The pointer to the block and the
33 // functions can all be passed as NULL. If successful, this returns a
34 // disassembler context. If not, it returns NULL.
36 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
37 void *DisInfo, int TagType,
38 LLVMOpInfoCallback GetOpInfo,
39 LLVMSymbolLookupCallback SymbolLookUp){
42 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
46 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
50 // Get the assembler info needed to setup the MCContext.
51 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
55 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
59 // Package up features to be passed to target/subtarget
60 std::string FeaturesStr;
62 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
67 // Set up the MCContext for creating symbols and MCExpr's.
68 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
72 // Set up disassembler.
73 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
76 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
78 // Set up the instruction printer.
79 int AsmPrinterVariant = MAI->getAssemblerDialect();
80 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
81 *MAI, *MII, *MRI, *STI);
85 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
86 GetOpInfo, SymbolLookUp,
88 STI, MII, Ctx, DisAsm, IP);
95 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
96 int TagType, LLVMOpInfoCallback GetOpInfo,
97 LLVMSymbolLookupCallback SymbolLookUp) {
98 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
103 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
105 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
106 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
112 // The memory object created by LLVMDisasmInstruction().
114 class DisasmMemoryObject : public MemoryObject {
119 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
120 Bytes(bytes), Size(size), BasePC(basePC) {}
122 uint64_t getBase() const { return BasePC; }
123 uint64_t getExtent() const { return Size; }
125 int readByte(uint64_t Addr, uint8_t *Byte) const {
126 if (Addr - BasePC >= Size)
128 *Byte = Bytes[Addr - BasePC];
132 } // end anonymous namespace
135 // LLVMDisasmInstruction() disassembles a single instruction using the
136 // disassembler context specified in the parameter DC. The bytes of the
137 // instruction are specified in the parameter Bytes, and contains at least
138 // BytesSize number of bytes. The instruction is at the address specified by
139 // the PC parameter. If a valid instruction can be disassembled its string is
140 // returned indirectly in OutString which whos size is specified in the
141 // parameter OutStringSize. This function returns the number of bytes in the
142 // instruction or zero if there was no valid instruction. If this function
143 // returns zero the caller will have to pick how many bytes they want to step
144 // over by printing a .byte, .long etc. to continue.
146 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
147 uint64_t BytesSize, uint64_t PC, char *OutString,
148 size_t OutStringSize){
149 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
150 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
151 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
155 const MCDisassembler *DisAsm = DC->getDisAsm();
156 MCInstPrinter *IP = DC->getIP();
157 MCDisassembler::DecodeStatus S;
158 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
159 /*REMOVE*/ nulls(), DC->CommentStream);
161 case MCDisassembler::Fail:
162 case MCDisassembler::SoftFail:
163 // FIXME: Do something different for soft failure modes?
166 case MCDisassembler::Success: {
167 DC->CommentStream.flush();
168 StringRef Comments = DC->CommentsToEmit.str();
170 SmallVector<char, 64> InsnStr;
171 raw_svector_ostream OS(InsnStr);
172 IP->printInst(&Inst, OS, Comments);
175 // Tell the comment stream that the vector changed underneath it.
176 DC->CommentsToEmit.clear();
177 DC->CommentStream.resync();
179 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.
187 llvm_unreachable("Invalid DecodeStatus!");
191 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
192 // can set all the Options and 0 otherwise.
194 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
195 if (Options & LLVMDisassembler_Option_UseMarkup){
196 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
197 MCInstPrinter *IP = DC->getIP();
199 Options &= ~LLVMDisassembler_Option_UseMarkup;
201 if (Options & LLVMDisassembler_Option_PrintImmHex){
202 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
203 MCInstPrinter *IP = DC->getIP();
204 IP->setPrintImmHex(1);
205 Options &= ~LLVMDisassembler_Option_PrintImmHex;
207 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
208 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
209 // Try to set up the new instruction printer.
210 const MCAsmInfo *MAI = DC->getAsmInfo();
211 const MCInstrInfo *MII = DC->getInstrInfo();
212 const MCRegisterInfo *MRI = DC->getRegisterInfo();
213 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
214 int AsmPrinterVariant = MAI->getAssemblerDialect();
215 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
216 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
217 AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
220 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
223 return (Options == 0);