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" |
Evan Cheng | 4c81648 | 2011-07-20 06:54:19 +0000 | [diff] [blame^] | 14 | #include "llvm/MC/MCContext.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCDisassembler.h" |
| 16 | #include "llvm/MC/MCInst.h" |
| 17 | #include "llvm/MC/MCInstPrinter.h" |
Evan Cheng | 4c81648 | 2011-07-20 06:54:19 +0000 | [diff] [blame^] | 18 | #include "llvm/MC/MCRegisterInfo.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 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" |
| 24 | |
| 25 | namespace llvm { |
| 26 | class Target; |
| 27 | } // namespace llvm |
| 28 | using namespace llvm; |
| 29 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 30 | // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic |
| 31 | // disassembly is supported by passing a block of information in the DisInfo |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 32 | // parameter and specifying the TagType and callback functions as described in |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 33 | // 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] | 34 | // functions can all be passed as NULL. If successful, this returns a |
| 35 | // disassembler context. If not, it returns NULL. |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 36 | // |
| 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(); |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 44 | llvm::InitializeAllMCAsmInfos(); |
Evan Cheng | 4396613 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 45 | llvm::InitializeAllMCCodeGenInfos(); |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 46 | llvm::InitializeAllMCRegisterInfos(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 47 | llvm::InitializeAllAsmPrinters(); |
| 48 | llvm::InitializeAllAsmParsers(); |
| 49 | llvm::InitializeAllDisassemblers(); |
| 50 | |
| 51 | // Get the target. |
| 52 | std::string Error; |
| 53 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 54 | assert(TheTarget && "Unable to create target!"); |
| 55 | |
| 56 | // Get the assembler info needed to setup the MCContext. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 57 | const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 58 | assert(MAI && "Unable to create target asm info!"); |
| 59 | |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 60 | const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); |
| 61 | assert(MRI && "Unable to create target register info!"); |
| 62 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 63 | // Package up features to be passed to target/subtarget |
| 64 | std::string FeaturesStr; |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 65 | std::string CPU; |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 66 | |
| 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. |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 70 | TargetMachine *TM = TheTarget->createTargetMachine(TripleName, CPU, |
| 71 | FeaturesStr); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 72 | assert(TM && "Unable to create target machine!"); |
| 73 | |
| 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!"); |
| 77 | |
| 78 | // Set up the MCContext for creating symbols and MCExpr's. |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 79 | MCContext *Ctx = new MCContext(*MAI, *MRI, 0, tai); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 80 | assert(Ctx && "Unable to create MCContext!"); |
| 81 | |
| 82 | // Set up disassembler. |
Kevin Enderby | bd33276 | 2011-04-11 18:08:50 +0000 | [diff] [blame] | 83 | MCDisassembler *DisAsm = TheTarget->createMCDisassembler(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 84 | assert(DisAsm && "Unable to create disassembler!"); |
Kevin Enderby | bd33276 | 2011-04-11 18:08:50 +0000 | [diff] [blame] | 85 | DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 86 | |
| 87 | // Set up the instruction printer. |
| 88 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 89 | MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 90 | *MAI); |
| 91 | assert(IP && "Unable to create instruction printer!"); |
| 92 | |
| 93 | LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, |
| 94 | GetOpInfo, SymbolLookUp, |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 95 | TheTarget, MAI, MRI, TM, tai, |
| 96 | Ctx, DisAsm, IP); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 97 | assert(DC && "Allocation failure!"); |
| 98 | return DC; |
| 99 | } |
| 100 | |
| 101 | // |
| 102 | // LLVMDisasmDispose() disposes of the disassembler specified by the context. |
| 103 | // |
| 104 | void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ |
| 105 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 106 | delete DC; |
| 107 | } |
| 108 | |
| 109 | namespace { |
| 110 | // |
| 111 | // The memory object created by LLVMDisasmInstruction(). |
| 112 | // |
| 113 | class DisasmMemoryObject : public MemoryObject { |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 114 | uint8_t *Bytes; |
| 115 | uint64_t Size; |
| 116 | uint64_t BasePC; |
| 117 | public: |
| 118 | DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : |
| 119 | Bytes(bytes), Size(size), BasePC(basePC) {} |
| 120 | |
| 121 | uint64_t getBase() const { return BasePC; } |
| 122 | uint64_t getExtent() const { return Size; } |
| 123 | |
| 124 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
| 125 | if (Addr - BasePC >= Size) |
| 126 | return -1; |
| 127 | *Byte = Bytes[Addr - BasePC]; |
| 128 | return 0; |
| 129 | } |
| 130 | }; |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 131 | } // end anonymous namespace |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 132 | |
| 133 | // |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 134 | // LLVMDisasmInstruction() disassembles a single instruction using the |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 135 | // disassembler context specified in the parameter DC. The bytes of the |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 136 | // instruction are specified in the parameter Bytes, and contains at least |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 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. |
| 144 | // |
| 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); |
| 151 | |
| 152 | uint64_t Size; |
| 153 | MCInst Inst; |
| 154 | const MCDisassembler *DisAsm = DC->getDisAsm(); |
| 155 | MCInstPrinter *IP = DC->getIP(); |
| 156 | if (!DisAsm->getInstruction(Inst, Size, MemoryObject, PC, /*REMOVE*/ nulls())) |
| 157 | return 0; |
| 158 | |
Chris Lattner | 9063b55 | 2011-05-22 04:53:24 +0000 | [diff] [blame] | 159 | SmallVector<char, 64> InsnStr; |
| 160 | raw_svector_ostream OS(InsnStr); |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 161 | IP->printInst(&Inst, OS); |
| 162 | OS.flush(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 164 | assert(OutStringSize != 0 && "Output buffer cannot be zero size"); |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 165 | size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); |
| 166 | std::memcpy(OutString, InsnStr.data(), OutputSize); |
| 167 | OutString[OutputSize] = '\0'; // Terminate string. |
| 168 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 169 | return Size; |
| 170 | } |