Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 1 | //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface -*- C -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 9 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 10 | #include "Disassembler.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 11 | #include "llvm-c/Disassembler.h" |
| 12 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
| 14 | #include "llvm/MC/MCDisassembler.h" |
| 15 | #include "llvm/MC/MCInst.h" |
| 16 | #include "llvm/MC/MCInstPrinter.h" |
| 17 | #include "llvm/MC/MCContext.h" |
| 18 | #include "llvm/Target/TargetRegistry.h" |
| 19 | #include "llvm/Target/TargetAsmInfo.h" // FIXME. |
| 20 | #include "llvm/Target/TargetMachine.h" // FIXME. |
| 21 | #include "llvm/Target/TargetSelect.h" |
| 22 | #include "llvm/Support/MemoryObject.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | class Target; |
| 26 | } // namespace llvm |
| 27 | using namespace llvm; |
| 28 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 29 | // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic |
| 30 | // disassembly is supported by passing a block of information in the DisInfo |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 31 | // parameter and specifying the TagType and callback functions as described in |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 32 | // the header llvm-c/Disassembler.h . The pointer to the block and the |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 33 | // functions can all be passed as NULL. If successful, this returns a |
| 34 | // disassembler context. If not, it returns NULL. |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 35 | // |
| 36 | LLVMDisasmContextRef LLVMCreateDisasm(const char *TripleName, void *DisInfo, |
| 37 | int TagType, LLVMOpInfoCallback GetOpInfo, |
| 38 | LLVMSymbolLookupCallback SymbolLookUp) { |
| 39 | // Initialize targets and assembly printers/parsers. |
| 40 | llvm::InitializeAllTargetInfos(); |
| 41 | // FIXME: We shouldn't need to initialize the Target(Machine)s. |
| 42 | llvm::InitializeAllTargets(); |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 43 | llvm::InitializeAllMCAsmInfos(); |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 44 | llvm::InitializeAllMCCodeGenInfos(); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 45 | llvm::InitializeAllMCRegisterInfos(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 46 | llvm::InitializeAllAsmPrinters(); |
| 47 | llvm::InitializeAllAsmParsers(); |
| 48 | llvm::InitializeAllDisassemblers(); |
| 49 | |
| 50 | // Get the target. |
| 51 | std::string Error; |
| 52 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 53 | assert(TheTarget && "Unable to create target!"); |
| 54 | |
| 55 | // Get the assembler info needed to setup the MCContext. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 56 | const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 57 | assert(MAI && "Unable to create target asm info!"); |
| 58 | |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 59 | const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); |
| 60 | assert(MRI && "Unable to create target register info!"); |
| 61 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 62 | // Package up features to be passed to target/subtarget |
| 63 | std::string FeaturesStr; |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 64 | std::string CPU; |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 65 | |
| 66 | // FIXME: We shouldn't need to do this (and link in codegen). |
| 67 | // When we split this out, we should do it in a way that makes |
| 68 | // it straightforward to switch subtargets on the fly. |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 69 | TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU, |
| 70 | FeaturesStr); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 71 | assert(TM && "Unable to create target machine!"); |
| 72 | |
| 73 | // Get the target assembler info needed to setup the context. |
| 74 | const TargetAsmInfo *tai = new TargetAsmInfo(*TM); |
| 75 | assert(tai && "Unable to create target assembler!"); |
| 76 | |
| 77 | // Set up the MCContext for creating symbols and MCExpr's. |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 78 | MCContext *Ctx = new MCContext(*MAI, *MRI, tai); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 79 | assert(Ctx && "Unable to create MCContext!"); |
| 80 | |
| 81 | // Set up disassembler. |
Kevin Enderby | bd33276 | 2011-04-11 18:08:50 +0000 | [diff] [blame] | 82 | MCDisassembler *DisAsm = TheTarget->createMCDisassembler(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 83 | assert(DisAsm && "Unable to create disassembler!"); |
Kevin Enderby | bd33276 | 2011-04-11 18:08:50 +0000 | [diff] [blame] | 84 | DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 85 | |
| 86 | // Set up the instruction printer. |
| 87 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 88 | MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 89 | *MAI); |
| 90 | assert(IP && "Unable to create instruction printer!"); |
| 91 | |
| 92 | LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, |
| 93 | GetOpInfo, SymbolLookUp, |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 94 | TheTarget, MAI, MRI, TM, tai, |
| 95 | Ctx, DisAsm, IP); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 96 | assert(DC && "Allocation failure!"); |
| 97 | return DC; |
| 98 | } |
| 99 | |
| 100 | // |
| 101 | // LLVMDisasmDispose() disposes of the disassembler specified by the context. |
| 102 | // |
| 103 | void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ |
| 104 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 105 | delete DC; |
| 106 | } |
| 107 | |
| 108 | namespace { |
| 109 | // |
| 110 | // The memory object created by LLVMDisasmInstruction(). |
| 111 | // |
| 112 | class DisasmMemoryObject : public MemoryObject { |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 113 | uint8_t *Bytes; |
| 114 | uint64_t Size; |
| 115 | uint64_t BasePC; |
| 116 | public: |
| 117 | DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : |
| 118 | Bytes(bytes), Size(size), BasePC(basePC) {} |
| 119 | |
| 120 | uint64_t getBase() const { return BasePC; } |
| 121 | uint64_t getExtent() const { return Size; } |
| 122 | |
| 123 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
| 124 | if (Addr - BasePC >= Size) |
| 125 | return -1; |
| 126 | *Byte = Bytes[Addr - BasePC]; |
| 127 | return 0; |
| 128 | } |
| 129 | }; |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 130 | } // end anonymous namespace |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 131 | |
| 132 | // |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 133 | // LLVMDisasmInstruction() disassembles a single instruction using the |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 134 | // disassembler context specified in the parameter DC. The bytes of the |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 135 | // instruction are specified in the parameter Bytes, and contains at least |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 136 | // BytesSize number of bytes. The instruction is at the address specified by |
| 137 | // the PC parameter. If a valid instruction can be disassembled its string is |
| 138 | // returned indirectly in OutString which whos size is specified in the |
| 139 | // parameter OutStringSize. This function returns the number of bytes in the |
| 140 | // instruction or zero if there was no valid instruction. If this function |
| 141 | // returns zero the caller will have to pick how many bytes they want to step |
| 142 | // over by printing a .byte, .long etc. to continue. |
| 143 | // |
| 144 | size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, |
| 145 | uint64_t BytesSize, uint64_t PC, char *OutString, |
| 146 | size_t OutStringSize){ |
| 147 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 148 | // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. |
| 149 | DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); |
| 150 | |
| 151 | uint64_t Size; |
| 152 | MCInst Inst; |
| 153 | const MCDisassembler *DisAsm = DC->getDisAsm(); |
| 154 | MCInstPrinter *IP = DC->getIP(); |
| 155 | if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls())) |
| 156 | return 0; |
| 157 | |
Chris Lattner | 9063b55 | 2011-05-22 04:53:24 +0000 | [diff] [blame] | 158 | SmallVector<char, 64> InsnStr; |
| 159 | raw_svector_ostream OS(InsnStr); |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 160 | IP->printInst(&Inst, OS); |
| 161 | OS.flush(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 163 | assert(OutStringSize != 0 && "Output buffer cannot be zero size"); |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 164 | size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); |
| 165 | std::memcpy(OutString, InsnStr.data(), OutputSize); |
| 166 | OutString[OutputSize] = '\0'; // Terminate string. |
| 167 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 168 | return Size; |
| 169 | } |