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 LLVMCreateDisasm(const char *TripleName, void *DisInfo,
37 int TagType, LLVMOpInfoCallback GetOpInfo,
38 LLVMSymbolLookupCallback SymbolLookUp) {
41 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
42 assert(TheTarget && "Unable to create target!");
44 // Get the assembler info needed to setup the MCContext.
45 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
46 assert(MAI && "Unable to create target asm info!");
48 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
49 assert(MII && "Unable to create target instruction info!");
51 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
52 assert(MRI && "Unable to create target register info!");
54 // Package up features to be passed to target/subtarget
55 std::string FeaturesStr;
58 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
60 assert(STI && "Unable to create subtarget info!");
62 // Set up the MCContext for creating symbols and MCExpr's.
63 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
64 assert(Ctx && "Unable to create MCContext!");
66 // Set up disassembler.
67 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
68 assert(DisAsm && "Unable to create disassembler!");
69 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
71 // Set up the instruction printer.
72 int AsmPrinterVariant = MAI->getAssemblerDialect();
73 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
74 *MAI, *MII, *MRI, *STI);
75 assert(IP && "Unable to create instruction printer!");
77 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
78 GetOpInfo, SymbolLookUp,
80 STI, MII, Ctx, DisAsm, IP);
81 assert(DC && "Allocation failure!");
87 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
89 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
90 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
96 // The memory object created by LLVMDisasmInstruction().
98 class DisasmMemoryObject : public MemoryObject {
103 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
104 Bytes(bytes), Size(size), BasePC(basePC) {}
106 uint64_t getBase() const { return BasePC; }
107 uint64_t getExtent() const { return Size; }
109 int readByte(uint64_t Addr, uint8_t *Byte) const {
110 if (Addr - BasePC >= Size)
112 *Byte = Bytes[Addr - BasePC];
116 } // end anonymous namespace
119 // LLVMDisasmInstruction() disassembles a single instruction using the
120 // disassembler context specified in the parameter DC. The bytes of the
121 // instruction are specified in the parameter Bytes, and contains at least
122 // BytesSize number of bytes. The instruction is at the address specified by
123 // the PC parameter. If a valid instruction can be disassembled its string is
124 // returned indirectly in OutString which whos size is specified in the
125 // parameter OutStringSize. This function returns the number of bytes in the
126 // instruction or zero if there was no valid instruction. If this function
127 // returns zero the caller will have to pick how many bytes they want to step
128 // over by printing a .byte, .long etc. to continue.
130 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
131 uint64_t BytesSize, uint64_t PC, char *OutString,
132 size_t OutStringSize){
133 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
134 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
135 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
139 const MCDisassembler *DisAsm = DC->getDisAsm();
140 MCInstPrinter *IP = DC->getIP();
141 MCDisassembler::DecodeStatus S;
142 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
143 /*REMOVE*/ nulls(), DC->CommentStream);
145 case MCDisassembler::Fail:
146 case MCDisassembler::SoftFail:
147 // FIXME: Do something different for soft failure modes?
150 case MCDisassembler::Success: {
151 DC->CommentStream.flush();
152 StringRef Comments = DC->CommentsToEmit.str();
154 SmallVector<char, 64> InsnStr;
155 raw_svector_ostream OS(InsnStr);
156 IP->printInst(&Inst, OS, Comments);
159 // Tell the comment stream that the vector changed underneath it.
160 DC->CommentsToEmit.clear();
161 DC->CommentStream.resync();
163 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
164 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
165 std::memcpy(OutString, InsnStr.data(), OutputSize);
166 OutString[OutputSize] = '\0'; // Terminate string.
171 llvm_unreachable("Invalid DecodeStatus!");
175 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
176 // can set all the Options and 0 otherwise.
178 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
179 if (Options & LLVMDisassembler_Option_UseMarkup){
180 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
181 MCInstPrinter *IP = DC->getIP();
183 Options &= ~LLVMDisassembler_Option_UseMarkup;
185 if (Options & LLVMDisassembler_Option_PrintImmHex){
186 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
187 MCInstPrinter *IP = DC->getIP();
188 IP->setPrintImmHex(1);
189 Options &= ~LLVMDisassembler_Option_PrintImmHex;
191 return (Options == 0);