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"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDisassembler.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCInstrInfo.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/MemoryObject.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Support/ErrorHandling.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 LLVMCreateDisasm(const char *TripleName, void *DisInfo,
38 int TagType, LLVMOpInfoCallback GetOpInfo,
39 LLVMSymbolLookupCallback SymbolLookUp) {
42 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
43 assert(TheTarget && "Unable to create target!");
45 // Get the assembler info needed to setup the MCContext.
46 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
47 assert(MAI && "Unable to create target asm info!");
49 const MCInstrInfo *MII = TheTarget->createMCInstrInfo();
50 assert(MII && "Unable to create target instruction info!");
52 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
53 assert(MRI && "Unable to create target register info!");
55 // Package up features to be passed to target/subtarget
56 std::string FeaturesStr;
59 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
61 assert(STI && "Unable to create subtarget info!");
63 // Set up the MCContext for creating symbols and MCExpr's.
64 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
65 assert(Ctx && "Unable to create MCContext!");
67 // Set up disassembler.
68 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
69 assert(DisAsm && "Unable to create disassembler!");
70 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
72 // Set up the instruction printer.
73 int AsmPrinterVariant = MAI->getAssemblerDialect();
74 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
75 *MAI, *MII, *MRI, *STI);
76 assert(IP && "Unable to create instruction printer!");
78 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
79 GetOpInfo, SymbolLookUp,
81 STI, MII, Ctx, DisAsm, IP);
82 assert(DC && "Allocation failure!");
88 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
90 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
91 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
97 // The memory object created by LLVMDisasmInstruction().
99 class DisasmMemoryObject : public MemoryObject {
104 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
105 Bytes(bytes), Size(size), BasePC(basePC) {}
107 uint64_t getBase() const { return BasePC; }
108 uint64_t getExtent() const { return Size; }
110 int readByte(uint64_t Addr, uint8_t *Byte) const {
111 if (Addr - BasePC >= Size)
113 *Byte = Bytes[Addr - BasePC];
117 } // end anonymous namespace
120 // LLVMDisasmInstruction() disassembles a single instruction using the
121 // disassembler context specified in the parameter DC. The bytes of the
122 // instruction are specified in the parameter Bytes, and contains at least
123 // BytesSize number of bytes. The instruction is at the address specified by
124 // the PC parameter. If a valid instruction can be disassembled its string is
125 // returned indirectly in OutString which whos size is specified in the
126 // parameter OutStringSize. This function returns the number of bytes in the
127 // instruction or zero if there was no valid instruction. If this function
128 // returns zero the caller will have to pick how many bytes they want to step
129 // over by printing a .byte, .long etc. to continue.
131 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
132 uint64_t BytesSize, uint64_t PC, char *OutString,
133 size_t OutStringSize){
134 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
135 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
136 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
140 const MCDisassembler *DisAsm = DC->getDisAsm();
141 MCInstPrinter *IP = DC->getIP();
142 MCDisassembler::DecodeStatus S;
143 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
144 /*REMOVE*/ nulls(), DC->CommentStream);
146 case MCDisassembler::Fail:
147 case MCDisassembler::SoftFail:
148 // FIXME: Do something different for soft failure modes?
151 case MCDisassembler::Success: {
152 DC->CommentStream.flush();
153 StringRef Comments = DC->CommentsToEmit.str();
155 SmallVector<char, 64> InsnStr;
156 raw_svector_ostream OS(InsnStr);
157 IP->printInst(&Inst, OS, Comments);
160 // Tell the comment stream that the vector changed underneath it.
161 DC->CommentsToEmit.clear();
162 DC->CommentStream.resync();
164 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
165 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
166 std::memcpy(OutString, InsnStr.data(), OutputSize);
167 OutString[OutputSize] = '\0'; // Terminate string.
172 llvm_unreachable("Invalid DecodeStatus!");
176 // LLVMSetDisasmOptions() sets the disassembler's options. It returns 1 if it
177 // can set all the Options and 0 otherwise.
179 int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
180 if (Options & LLVMDisassembler_Option_UseMarkup){
181 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
182 MCInstPrinter *IP = DC->getIP();
184 Options &= ~LLVMDisassembler_Option_UseMarkup;
186 return (Options == 0);