1 //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface -*- C -*-===//
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/Target/TargetRegistry.h"
20 #include "llvm/Target/TargetSelect.h"
21 #include "llvm/Support/MemoryObject.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::InitializeAllMCAsmInfos();
41 llvm::InitializeAllMCCodeGenInfos();
42 llvm::InitializeAllMCRegisterInfos();
43 llvm::InitializeAllAsmPrinters();
44 llvm::InitializeAllAsmParsers();
45 llvm::InitializeAllDisassemblers();
49 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
50 assert(TheTarget && "Unable to create target!");
52 // Get the assembler info needed to setup the MCContext.
53 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
54 assert(MAI && "Unable to create target asm info!");
56 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
57 assert(MRI && "Unable to create target register info!");
59 // Package up features to be passed to target/subtarget
60 std::string FeaturesStr;
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();
69 assert(DisAsm && "Unable to create disassembler!");
70 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
72 // Set up the instruction printer.
73 int AsmPrinterVariant = MAI->getAssemblerDialect();
74 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
76 assert(IP && "Unable to create instruction printer!");
78 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
79 GetOpInfo, SymbolLookUp,
82 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 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
144 SmallVector<char, 64> InsnStr;
145 raw_svector_ostream OS(InsnStr);
146 IP->printInst(&Inst, OS);
149 assert(OutStringSize != 0 && "Output buffer cannot be zero size");
150 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
151 std::memcpy(OutString, InsnStr.data(), OutputSize);
152 OutString[OutputSize] = '\0'; // Terminate string.