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/MCRegisterInfo.h"
19 #include "llvm/Support/MemoryObject.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Support/TargetSelect.h"
28 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
29 // disassembly is supported by passing a block of information in the DisInfo
30 // parameter and specifying the TagType and callback functions as described in
31 // the header llvm-c/Disassembler.h . The pointer to the block and the
32 // functions can all be passed as NULL. If successful, this returns a
33 // disassembler context. If not, it returns NULL.
35 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
36 int TagType, LLVMOpInfoCallback GetOpInfo,
37 LLVMSymbolLookupCallback SymbolLookUp) {
38 // Initialize targets and assembly printers/parsers.
39 llvm::InitializeAllTargetInfos();
40 llvm::InitializeAllTargetMCs();
41 llvm::InitializeAllAsmParsers();
42 llvm::InitializeAllDisassemblers();
46 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
47 assert(TheTarget && "Unable to create target!");
49 // Get the assembler info needed to setup the MCContext.
50 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
51 assert(MAI && "Unable to create target asm info!");
53 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
54 assert(MRI && "Unable to create target register info!");
56 // Package up features to be passed to target/subtarget
57 std::string FeaturesStr;
60 const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU,
62 assert(STI && "Unable to create subtarget info!");
64 // Set up the MCContext for creating symbols and MCExpr's.
65 MCContext *Ctx = new MCContext(*MAI, *MRI, 0);
66 assert(Ctx && "Unable to create MCContext!");
68 // Set up disassembler.
69 MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI);
70 assert(DisAsm && "Unable to create disassembler!");
71 DisAsm->setupForSymbolicDisassembly(GetOpInfo, SymbolLookUp, DisInfo, Ctx);
73 // Set up the instruction printer.
74 int AsmPrinterVariant = MAI->getAssemblerDialect();
75 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
77 assert(IP && "Unable to create instruction printer!");
79 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
80 GetOpInfo, SymbolLookUp,
83 assert(DC && "Allocation failure!");
89 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
91 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
92 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
98 // The memory object created by LLVMDisasmInstruction().
100 class DisasmMemoryObject : public MemoryObject {
105 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
106 Bytes(bytes), Size(size), BasePC(basePC) {}
108 uint64_t getBase() const { return BasePC; }
109 uint64_t getExtent() const { return Size; }
111 int readByte(uint64_t Addr, uint8_t *Byte) const {
112 if (Addr - BasePC >= Size)
114 *Byte = Bytes[Addr - BasePC];
118 } // end anonymous namespace
121 // LLVMDisasmInstruction() disassembles a single instruction using the
122 // disassembler context specified in the parameter DC. The bytes of the
123 // instruction are specified in the parameter Bytes, and contains at least
124 // BytesSize number of bytes. The instruction is at the address specified by
125 // the PC parameter. If a valid instruction can be disassembled its string is
126 // returned indirectly in OutString which whos size is specified in the
127 // parameter OutStringSize. This function returns the number of bytes in the
128 // instruction or zero if there was no valid instruction. If this function
129 // returns zero the caller will have to pick how many bytes they want to step
130 // over by printing a .byte, .long etc. to continue.
132 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
133 uint64_t BytesSize, uint64_t PC, char *OutString,
134 size_t OutStringSize){
135 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
136 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
137 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
141 const MCDisassembler *DisAsm = DC->getDisAsm();
142 MCInstPrinter *IP = DC->getIP();
143 MCDisassembler::DecodeStatus S;
144 S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
145 /*REMOVE*/ nulls(), DC->CommentStream);
147 case MCDisassembler::Fail:
148 case MCDisassembler::SoftFail:
149 // FIXME: Do something different for soft failure modes?
152 case MCDisassembler::Success: {
153 DC->CommentStream.flush();
154 StringRef Comments = DC->CommentsToEmit.str();
156 SmallVector<char, 64> InsnStr;
157 raw_svector_ostream OS(InsnStr);
158 IP->printInst(&Inst, OS, Comments);
161 // Tell the comment stream that the vector changed underneath it.
162 DC->CommentsToEmit.clear();
163 DC->CommentStream.resync();
165 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
166 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
167 std::memcpy(OutString, InsnStr.data(), OutputSize);
168 OutString[OutputSize] = '\0'; // Terminate string.