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/FormattedStream.h"
24 #include "llvm/Support/MemoryObject.h"
25 #include "llvm/Support/TargetRegistry.h"
32 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
33 // disassembly is supported by passing a block of information in the DisInfo
34 // parameter and specifying the TagType and callback functions as described in
35 // the header llvm-c/Disassembler.h . The pointer to the block and the
36 // functions can all be passed as NULL. If successful, this returns a
37 // disassembler context. If not, it returns NULL.
39 LLVMDisasmContextRef LLVMCreateDisasmCPU(const char *Triple, const char *CPU,
40 void *DisInfo, int TagType,
41 LLVMOpInfoCallback GetOpInfo,
42 LLVMSymbolLookupCallback SymbolLookUp){
45 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
49 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(Triple);
53 // Get the assembler info needed to setup the MCContext.
54 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(*MRI, Triple);
58 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
62 // Package up features to be passed to target/subtarget
63 std::string FeaturesStr;
65 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(Triple, CPU,
70 // Set up the MCContext for creating symbols and MCExpr's.
71 MCContext *Ctx = new MCContext(MAI, MRI, 0);
75 // Set up disassembler.
76 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
80 OwningPtr<MCRelocationInfo> RelInfo(
81 TheTarget->createMCRelocationInfo(Triple, *Ctx));
85 OwningPtr<MCSymbolizer> Symbolizer(
86 TheTarget->createMCSymbolizer(Triple, GetOpInfo, SymbolLookUp, DisInfo,
87 Ctx, RelInfo.take()));
88 DisAsm->setSymbolizer(Symbolizer);
89 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo,
91 // Set up the instruction printer.
92 int AsmPrinterVariant = MAI->getAssemblerDialect();
93 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
94 *MAI, *MII, *MRI, *STI);
98 LLVMDisasmContext *DC = new LLVMDisasmContext(Triple, DisInfo, TagType,
99 GetOpInfo, SymbolLookUp,
101 STI, MII, Ctx, DisAsm, IP);
108 LLVMDisasmContextRef LLVMCreateDisasm(const char *Triple, void *DisInfo,
109 int TagType, LLVMOpInfoCallback GetOpInfo,
110 LLVMSymbolLookupCallback SymbolLookUp) {
111 return LLVMCreateDisasmCPU(Triple, "", DisInfo, TagType, GetOpInfo,
116 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
118 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
119 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
125 // The memory object created by LLVMDisasmInstruction().
127 class DisasmMemoryObject : public MemoryObject {
132 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
133 Bytes(bytes), Size(size), BasePC(basePC) {}
135 uint64_t getBase() const { return BasePC; }
136 uint64_t getExtent() const { return Size; }
138 int readByte(uint64_t Addr, uint8_t *Byte) const {
139 if (Addr - BasePC >= Size)
141 *Byte = Bytes[Addr - BasePC];
145 } // end anonymous namespace
147 /// \brief Emits the comments that are stored in \p DC comment stream.
148 /// Each comment in the comment stream must end with a newline.
149 static void emitComments(LLVMDisasmContext *DC,
150 formatted_raw_ostream &FormattedOS) {
151 // Flush the stream before taking its content.
152 DC->CommentStream.flush();
153 StringRef Comments = DC->CommentsToEmit.str();
154 // Get the default information for printing a comment.
155 const MCAsmInfo *MAI = DC->getAsmInfo();
156 const char *CommentBegin = MAI->getCommentString();
157 unsigned CommentColumn = MAI->getCommentColumn();
159 while (!Comments.empty()) {
162 // Emit a line of comments.
163 FormattedOS.PadToColumn(CommentColumn);
164 size_t Position = Comments.find('\n');
165 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
166 // Move after the newline character.
167 Comments = Comments.substr(Position+1);
172 // Tell the comment stream that the vector changed underneath it.
173 DC->CommentsToEmit.clear();
174 DC->CommentStream.resync();
178 // LLVMDisasmInstruction() disassembles a single instruction using the
179 // disassembler context specified in the parameter DC. The bytes of the
180 // instruction are specified in the parameter Bytes, and contains at least
181 // BytesSize number of bytes. The instruction is at the address specified by
182 // the PC parameter. If a valid instruction can be disassembled its string is
183 // returned indirectly in OutString which whos size is specified in the
184 // parameter OutStringSize. This function returns the number of bytes in the
185 // instruction or zero if there was no valid instruction. If this function
186 // returns zero the caller will have to pick how many bytes they want to step
187 // over by printing a .byte, .long etc. to continue.
189 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
190 uint64_t BytesSize, uint64_t PC, char *OutString,
191 size_t OutStringSize){
192 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
193 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
194 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
198 const MCDisassembler *DisAsm = DC->getDisAsm();
199 MCInstPrinter *IP = DC->getIP();
200 MCDisassembler::DecodeStatus S;
201 SmallVector<char, 64> InsnStr;
202 raw_svector_ostream Annotations(InsnStr);
203 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
204 /*REMOVE*/ nulls(), Annotations);
206 case MCDisassembler::Fail:
207 case MCDisassembler::SoftFail:
208 // FIXME: Do something different for soft failure modes?
211 case MCDisassembler::Success: {
213 StringRef AnnotationsStr = Annotations.str();
215 SmallVector<char, 64> InsnStr;
216 raw_svector_ostream OS(InsnStr);
217 formatted_raw_ostream FormattedOS(OS);
218 IP->printInst(&Inst, FormattedOS, AnnotationsStr);
220 emitComments(DC, FormattedOS);
222 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
223 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
224 std::memcpy(OutString, InsnStr.data(), OutputSize);
225 OutString[OutputSize] = '\0'; // Terminate string.
230 llvm_unreachable("Invalid DecodeStatus!");
234 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
235 // can set all the Options and 0 otherwise.
237 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
238 if (Options & LLVMDisassembler_Option_UseMarkup){
239 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
240 MCInstPrinter *IP = DC->getIP();
242 Options &= ~LLVMDisassembler_Option_UseMarkup;
244 if (Options & LLVMDisassembler_Option_PrintImmHex){
245 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
246 MCInstPrinter *IP = DC->getIP();
247 IP->setPrintImmHex(1);
248 Options &= ~LLVMDisassembler_Option_PrintImmHex;
250 if (Options & LLVMDisassembler_Option_AsmPrinterVariant){
251 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
252 // Try to set up the new instruction printer.
253 const MCAsmInfo *MAI = DC->getAsmInfo();
254 const MCInstrInfo *MII = DC->getInstrInfo();
255 const MCRegisterInfo *MRI = DC->getRegisterInfo();
256 const MCSubtargetInfo *STI = DC->getSubtargetInfo();
257 int AsmPrinterVariant = MAI->getAssemblerDialect();
258 AsmPrinterVariant = AsmPrinterVariant == 0 ? 1 : 0;
259 MCInstPrinter *IP = DC->getTarget()->createMCInstPrinter(
260 AsmPrinterVariant, *MAI, *MII, *MRI, *STI);
263 Options &= ~LLVMDisassembler_Option_AsmPrinterVariant;
266 if (Options & LLVMDisassembler_Option_SetInstrComments) {
267 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
268 MCInstPrinter *IP = DC->getIP();
269 IP->setCommentStream(DC->CommentStream);
270 Options &= ~LLVMDisassembler_Option_SetInstrComments;
272 return (Options == 0);