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