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/TargetAsmInfo.h" // FIXME.
21 #include "llvm/Target/TargetMachine.h" // FIXME.
22 #include "llvm/Target/TargetSelect.h"
23 #include "llvm/Support/MemoryObject.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) {
40 // Initialize targets and assembly printers/parsers.
41 llvm::InitializeAllTargetInfos();
42 // FIXME: We shouldn't need to initialize the Target(Machine)s.
43 llvm::InitializeAllTargets();
44 llvm::InitializeAllMCAsmInfos();
45 llvm::InitializeAllMCCodeGenInfos();
46 llvm::InitializeAllMCRegisterInfos();
47 llvm::InitializeAllAsmPrinters();
48 llvm::InitializeAllAsmParsers();
49 llvm::InitializeAllDisassemblers();
53 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
54 assert(TheTarget && "Unable to create target!");
56 // Get the assembler info needed to setup the MCContext.
57 const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName);
58 assert(MAI && "Unable to create target asm info!");
60 const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName);
61 assert(MRI && "Unable to create target register info!");
63 // Package up features to be passed to target/subtarget
64 std::string FeaturesStr;
67 // FIXME: We shouldn't need to do this (and link in codegen).
68 // When we split this out, we should do it in a way that makes
69 // it straightforward to switch subtargets on the fly.
70 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU,
72 assert(TM && "Unable to create target machine!");
74 // Get the target assembler info needed to setup the context.
75 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
76 assert(tai && "Unable to create target assembler!");
78 // Set up the MCContext for creating symbols and MCExpr's.
79 MCContext *Ctx = new MCContext(*MAI, *MRI, 0, tai);
80 assert(Ctx && "Unable to create MCContext!");
82 // Set up disassembler.
83 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
84 assert(DisAsm && "Unable to create disassembler!");
85 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
87 // Set up the instruction printer.
88 int AsmPrinterVariant = MAI->getAssemblerDialect();
89 MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant,
91 assert(IP && "Unable to create instruction printer!");
93 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
94 GetOpInfo, SymbolLookUp,
95 TheTarget, MAI, MRI, TM, tai,
97 assert(DC && "Allocation failure!");
102 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
104 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
105 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
111 // The memory object created by LLVMDisasmInstruction().
113 class DisasmMemoryObject : public MemoryObject {
118 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
119 Bytes(bytes), Size(size), BasePC(basePC) {}
121 uint64_t getBase() const { return BasePC; }
122 uint64_t getExtent() const { return Size; }
124 int readByte(uint64_t Addr, uint8_t *Byte) const {
125 if (Addr - BasePC >= Size)
127 *Byte = Bytes[Addr - BasePC];
131 } // end anonymous namespace
134 // LLVMDisasmInstruction() disassembles a single instruction using the
135 // disassembler context specified in the parameter DC. The bytes of the
136 // instruction are specified in the parameter Bytes, and contains at least
137 // BytesSize number of bytes. The instruction is at the address specified by
138 // the PC parameter. If a valid instruction can be disassembled its string is
139 // returned indirectly in OutString which whos size is specified in the
140 // parameter OutStringSize. This function returns the number of bytes in the
141 // instruction or zero if there was no valid instruction. If this function
142 // returns zero the caller will have to pick how many bytes they want to step
143 // over by printing a .byte, .long etc. to continue.
145 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
146 uint64_t BytesSize, uint64_t PC, char *OutString,
147 size_t OutStringSize){
148 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
149 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
150 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
154 const MCDisassembler *DisAsm = DC->getDisAsm();
155 MCInstPrinter *IP = DC->getIP();
156 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
159 SmallVector<char, 64> InsnStr;
160 raw_svector_ostream OS(InsnStr);
161 IP->printInst(&Inst, OS);
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.