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 //===----------------------------------------------------------------------===//
9 #include "Disassembler.h"
11 #include "llvm-c/Disassembler.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCDisassembler.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCContext.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"
35 // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic
36 // disassembly is supported by passing a block of information in the DisInfo
37 // parameter and specifing the TagType and call back functions as described in
38 // the header llvm-c/Disassembler.h . The pointer to the block and the
39 // functions can all be passed as NULL. If successful this returns a
40 // disassembler context if not it returns NULL.
42 LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo,
43 int TagType, LLVMOpInfoCallback GetOpInfo,
44 LLVMSymbolLookupCallback SymbolLookUp) {
45 // Initialize targets and assembly printers/parsers.
46 llvm::InitializeAllTargetInfos();
47 // FIXME: We shouldn't need to initialize the Target(Machine)s.
48 llvm::InitializeAllTargets();
49 llvm::InitializeAllAsmPrinters();
50 llvm::InitializeAllAsmParsers();
51 llvm::InitializeAllDisassemblers();
55 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
56 assert(TheTarget && "Unable to create target!");
58 // Get the assembler info needed to setup the MCContext.
59 const MCAsmInfo *MAI = TheTarget->createAsmInfo(TripleName);
60 assert(MAI && "Unable to create target asm info!");
62 // Package up features to be passed to target/subtarget
63 std::string FeaturesStr;
65 // FIXME: We shouldn't need to do this (and link in codegen).
66 // When we split this out, we should do it in a way that makes
67 // it straightforward to switch subtargets on the fly.
68 TargetMachine *TM = TheTarget->createTargetMachine(TripleName, FeaturesStr);
69 assert(TM && "Unable to create target machine!");
71 // Get the target assembler info needed to setup the context.
72 const TargetAsmInfo *tai = new TargetAsmInfo(*TM);
73 assert(tai && "Unable to create target assembler!");
75 // Set up the MCContext for creating symbols and MCExpr's.
76 MCContext *Ctx = new MCContext(*MAI, tai);
77 assert(Ctx && "Unable to create MCContext!");
79 // Set up disassembler.
80 MCDisassembler *DisAsm = TheTarget->createMCDisassembler();
81 assert(DisAsm && "Unable to create disassembler!");
82 DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx);
84 // Set up the instruction printer.
85 int AsmPrinterVariant = MAI->getAssemblerDialect();
86 MCInstPrinter *IP = TheTarget->createMCInstPrinter(*TM, AsmPrinterVariant,
88 assert(IP && "Unable to create instruction printer!");
90 LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType,
91 GetOpInfo, SymbolLookUp,
92 TheTarget, MAI, TM, tai, Ctx,
94 assert(DC && "Allocation failure!");
99 // LLVMDisasmDispose() disposes of the disassembler specified by the context.
101 void LLVMDisasmDispose(LLVMDisasmContextRef DCR){
102 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
108 // The memory object created by LLVMDisasmInstruction().
110 class DisasmMemoryObject : public MemoryObject {
116 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
117 Bytes(bytes), Size(size), BasePC(basePC) {}
119 uint64_t getBase() const { return BasePC; }
120 uint64_t getExtent() const { return Size; }
122 int readByte(uint64_t Addr, uint8_t *Byte) const {
123 if (Addr - BasePC >= Size)
125 *Byte = Bytes[Addr - BasePC];
132 // LLVMDisasmInstruction() disassembles a single instruction using the
133 // disassembler context specified in the parameter DC. The bytes of the
134 // instruction are specified in the parameter Bytes, and contains at least
135 // BytesSize number of bytes. The instruction is at the address specified by
136 // the PC parameter. If a valid instruction can be disassembled its string is
137 // returned indirectly in OutString which whos size is specified in the
138 // parameter OutStringSize. This function returns the number of bytes in the
139 // instruction or zero if there was no valid instruction. If this function
140 // returns zero the caller will have to pick how many bytes they want to step
141 // over by printing a .byte, .long etc. to continue.
143 size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
144 uint64_t BytesSize, uint64_t PC, char *OutString,
145 size_t OutStringSize){
146 LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR;
147 // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject.
148 DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC);
152 const MCDisassembler *DisAsm = DC->getDisAsm();
153 MCInstPrinter *IP = DC->getIP();
154 if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls()))
158 raw_string_ostream OS(InsnStr);
159 IP->printInst(&Inst, OS);
162 size_t OutputSize = std::min(OutStringSize-1, InsnStr.size());
163 std::memcpy(OutString, InsnStr.data(), OutputSize);
164 OutString[OutputSize] = '\0'; // Terminate string.
171 #endif // __cplusplus