Nick Lewycky | 833a003 | 2011-09-01 21:09:04 +0000 | [diff] [blame] | 1 | //===-- lib/MC/Disassembler.cpp - Disassembler Public C Interface ---------===// |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 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/Support/MemoryObject.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TargetRegistry.h" |
| 21 | #include "llvm/Support/TargetSelect.h" |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | class Target; |
| 25 | } // namespace llvm |
| 26 | using namespace llvm; |
| 27 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 28 | // LLVMCreateDisasm() creates a disassembler for the TripleName. Symbolic |
| 29 | // disassembly is supported by passing a block of information in the DisInfo |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 30 | // parameter and specifying the TagType and callback functions as described in |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 31 | // 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] | 32 | // functions can all be passed as NULL. If successful, this returns a |
| 33 | // disassembler context. If not, it returns NULL. |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 34 | // |
| 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(); |
Evan Cheng | e78085a | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 40 | llvm::InitializeAllTargetMCs(); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 41 | llvm::InitializeAllAsmParsers(); |
| 42 | llvm::InitializeAllDisassemblers(); |
| 43 | |
| 44 | // Get the target. |
| 45 | std::string Error; |
| 46 | const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 47 | assert(TheTarget && "Unable to create target!"); |
| 48 | |
| 49 | // Get the assembler info needed to setup the MCContext. |
Evan Cheng | 1abf2cb | 2011-07-14 23:50:31 +0000 | [diff] [blame] | 50 | const MCAsmInfo *MAI = TheTarget->createMCAsmInfo(TripleName); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 51 | assert(MAI && "Unable to create target asm info!"); |
| 52 | |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 53 | const MCRegisterInfo *MRI = TheTarget->createMCRegInfo(TripleName); |
| 54 | assert(MRI && "Unable to create target register info!"); |
| 55 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 56 | // Package up features to be passed to target/subtarget |
| 57 | std::string FeaturesStr; |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 58 | std::string CPU; |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 59 | |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 60 | const MCSubtargetInfo *STI = TheTarget->createMCSubtargetInfo(TripleName, CPU, |
| 61 | FeaturesStr); |
| 62 | assert(STI && "Unable to create subtarget info!"); |
| 63 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 64 | // Set up the MCContext for creating symbols and MCExpr's. |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 65 | MCContext *Ctx = new MCContext(*MAI, *MRI, 0); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 66 | assert(Ctx && "Unable to create MCContext!"); |
| 67 | |
| 68 | // Set up disassembler. |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 69 | MCDisassembler *DisAsm = TheTarget->createMCDisassembler(*STI); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 70 | assert(DisAsm && "Unable to create disassembler!"); |
Kevin Enderby | bd33276 | 2011-04-11 18:08:50 +0000 | [diff] [blame] | 71 | DisAsm->setupForSymbolicDisassembly(GetOpInfo, DisInfo, Ctx); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 72 | |
| 73 | // Set up the instruction printer. |
| 74 | int AsmPrinterVariant = MAI->getAssemblerDialect(); |
Evan Cheng | b262799 | 2011-07-06 19:45:42 +0000 | [diff] [blame] | 75 | MCInstPrinter *IP = TheTarget->createMCInstPrinter(AsmPrinterVariant, |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 76 | *MAI, *STI); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 77 | assert(IP && "Unable to create instruction printer!"); |
| 78 | |
| 79 | LLVMDisasmContext *DC = new LLVMDisasmContext(TripleName, DisInfo, TagType, |
| 80 | GetOpInfo, SymbolLookUp, |
Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 81 | TheTarget, MAI, MRI, |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 82 | Ctx, DisAsm, IP); |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 83 | assert(DC && "Allocation failure!"); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 84 | |
| 85 | IP->setCommentStream(DC->CommentStream); |
| 86 | |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 87 | return DC; |
| 88 | } |
| 89 | |
| 90 | // |
| 91 | // LLVMDisasmDispose() disposes of the disassembler specified by the context. |
| 92 | // |
| 93 | void LLVMDisasmDispose(LLVMDisasmContextRef DCR){ |
| 94 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 95 | delete DC; |
| 96 | } |
| 97 | |
| 98 | namespace { |
| 99 | // |
| 100 | // The memory object created by LLVMDisasmInstruction(). |
| 101 | // |
| 102 | class DisasmMemoryObject : public MemoryObject { |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 103 | uint8_t *Bytes; |
| 104 | uint64_t Size; |
| 105 | uint64_t BasePC; |
| 106 | public: |
| 107 | DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) : |
| 108 | Bytes(bytes), Size(size), BasePC(basePC) {} |
| 109 | |
| 110 | uint64_t getBase() const { return BasePC; } |
| 111 | uint64_t getExtent() const { return Size; } |
| 112 | |
| 113 | int readByte(uint64_t Addr, uint8_t *Byte) const { |
| 114 | if (Addr - BasePC >= Size) |
| 115 | return -1; |
| 116 | *Byte = Bytes[Addr - BasePC]; |
| 117 | return 0; |
| 118 | } |
| 119 | }; |
Chris Lattner | 97ff42d | 2011-05-22 04:52:24 +0000 | [diff] [blame] | 120 | } // end anonymous namespace |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 121 | |
| 122 | // |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 123 | // LLVMDisasmInstruction() disassembles a single instruction using the |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 124 | // disassembler context specified in the parameter DC. The bytes of the |
Benjamin Kramer | 5731ff6 | 2011-04-09 14:06:12 +0000 | [diff] [blame] | 125 | // instruction are specified in the parameter Bytes, and contains at least |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 126 | // BytesSize number of bytes. The instruction is at the address specified by |
| 127 | // the PC parameter. If a valid instruction can be disassembled its string is |
| 128 | // returned indirectly in OutString which whos size is specified in the |
| 129 | // parameter OutStringSize. This function returns the number of bytes in the |
| 130 | // instruction or zero if there was no valid instruction. If this function |
| 131 | // returns zero the caller will have to pick how many bytes they want to step |
| 132 | // over by printing a .byte, .long etc. to continue. |
| 133 | // |
| 134 | size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes, |
| 135 | uint64_t BytesSize, uint64_t PC, char *OutString, |
| 136 | size_t OutStringSize){ |
| 137 | LLVMDisasmContext *DC = (LLVMDisasmContext *)DCR; |
| 138 | // Wrap the pointer to the Bytes, BytesSize and PC in a MemoryObject. |
| 139 | DisasmMemoryObject MemoryObject(Bytes, BytesSize, PC); |
| 140 | |
| 141 | uint64_t Size; |
| 142 | MCInst Inst; |
| 143 | const MCDisassembler *DisAsm = DC->getDisAsm(); |
| 144 | MCInstPrinter *IP = DC->getIP(); |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 145 | MCDisassembler::DecodeStatus S; |
| 146 | S = DisAsm->getInstruction(Inst, Size, MemoryObject, PC, |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame^] | 147 | /*REMOVE*/ nulls(), DC->CommentStream); |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 148 | switch (S) { |
| 149 | case MCDisassembler::Fail: |
| 150 | case MCDisassembler::SoftFail: |
James Molloy | c047dca | 2011-09-01 18:02:14 +0000 | [diff] [blame] | 151 | // FIXME: Do something different for soft failure modes? |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 152 | return 0; |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 153 | |
| 154 | case MCDisassembler::Success: { |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 155 | DC->CommentStream.flush(); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 156 | StringRef Comments = DC->CommentsToEmit.str(); |
| 157 | |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame^] | 158 | SmallVector<char, 64> InsnStr; |
| 159 | raw_svector_ostream OS(InsnStr); |
| 160 | IP->printInst(&Inst, OS, Comments); |
| 161 | OS.flush(); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 162 | |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 163 | // Tell the comment stream that the vector changed underneath it. |
Owen Anderson | 98c5dda | 2011-09-15 23:38:46 +0000 | [diff] [blame^] | 164 | DC->CommentsToEmit.clear(); |
Owen Anderson | 8f29e69 | 2011-09-15 18:37:20 +0000 | [diff] [blame] | 165 | DC->CommentStream.resync(); |
| 166 | |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 167 | assert(OutStringSize != 0 && "Output buffer cannot be zero size"); |
| 168 | size_t OutputSize = std::min(OutStringSize-1, InsnStr.size()); |
| 169 | std::memcpy(OutString, InsnStr.data(), OutputSize); |
| 170 | OutString[OutputSize] = '\0'; // Terminate string. |
| 171 | |
| 172 | return Size; |
James Molloy | c047dca | 2011-09-01 18:02:14 +0000 | [diff] [blame] | 173 | } |
James Molloy | ee06443 | 2011-09-01 22:01:14 +0000 | [diff] [blame] | 174 | } |
| 175 | return 0; |
Kevin Enderby | 93f7936 | 2011-03-28 18:25:07 +0000 | [diff] [blame] | 176 | } |