David 'Digit' Turner | 031fad7 | 2015-04-29 16:20:20 +0200 | [diff] [blame^] | 1 | //===-- llvm/MC/MCDisassembler.h - Disassembler 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 | //===----------------------------------------------------------------------===// |
| 9 | #ifndef LLVM_MC_MCDISASSEMBLER_H |
| 10 | #define LLVM_MC_MCDISASSEMBLER_H |
| 11 | |
| 12 | #include "llvm-c/Disassembler.h" |
| 13 | #include "llvm/MC/MCRelocationInfo.h" |
| 14 | #include "llvm/MC/MCSymbolizer.h" |
| 15 | #include "llvm/Support/DataTypes.h" |
| 16 | |
| 17 | namespace llvm { |
| 18 | |
| 19 | class MCInst; |
| 20 | class MCSubtargetInfo; |
| 21 | class MemoryObject; |
| 22 | class raw_ostream; |
| 23 | class MCContext; |
| 24 | |
| 25 | /// MCDisassembler - Superclass for all disassemblers. Consumes a memory region |
| 26 | /// and provides an array of assembly instructions. |
| 27 | class MCDisassembler { |
| 28 | public: |
| 29 | /// Ternary decode status. Most backends will just use Fail and |
| 30 | /// Success, however some have a concept of an instruction with |
| 31 | /// understandable semantics but which is architecturally |
| 32 | /// incorrect. An example of this is ARM UNPREDICTABLE instructions |
| 33 | /// which are disassemblable but cause undefined behaviour. |
| 34 | /// |
| 35 | /// Because it makes sense to disassemble these instructions, there |
| 36 | /// is a "soft fail" failure mode that indicates the MCInst& is |
| 37 | /// valid but architecturally incorrect. |
| 38 | /// |
| 39 | /// The enum numbers are deliberately chosen such that reduction |
| 40 | /// from Success->SoftFail ->Fail can be done with a simple |
| 41 | /// bitwise-AND: |
| 42 | /// |
| 43 | /// LEFT & TOP = | Success Unpredictable Fail |
| 44 | /// --------------+----------------------------------- |
| 45 | /// Success | Success Unpredictable Fail |
| 46 | /// Unpredictable | Unpredictable Unpredictable Fail |
| 47 | /// Fail | Fail Fail Fail |
| 48 | /// |
| 49 | /// An easy way of encoding this is as 0b11, 0b01, 0b00 for |
| 50 | /// Success, SoftFail, Fail respectively. |
| 51 | enum DecodeStatus { |
| 52 | Fail = 0, |
| 53 | SoftFail = 1, |
| 54 | Success = 3 |
| 55 | }; |
| 56 | |
| 57 | /// Constructor - Performs initial setup for the disassembler. |
| 58 | MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) |
| 59 | : Ctx(Ctx), STI(STI), Symbolizer(), CommentStream(nullptr) {} |
| 60 | |
| 61 | virtual ~MCDisassembler(); |
| 62 | |
| 63 | /// getInstruction - Returns the disassembly of a single instruction. |
| 64 | /// |
| 65 | /// @param instr - An MCInst to populate with the contents of the |
| 66 | /// instruction. |
| 67 | /// @param size - A value to populate with the size of the instruction, or |
| 68 | /// the number of bytes consumed while attempting to decode |
| 69 | /// an invalid instruction. |
| 70 | /// @param region - The memory object to use as a source for machine code. |
| 71 | /// @param address - The address, in the memory space of region, of the first |
| 72 | /// byte of the instruction. |
| 73 | /// @param vStream - The stream to print warnings and diagnostic messages on. |
| 74 | /// @param cStream - The stream to print comments and annotations on. |
| 75 | /// @return - MCDisassembler::Success if the instruction is valid, |
| 76 | /// MCDisassembler::SoftFail if the instruction was |
| 77 | /// disassemblable but invalid, |
| 78 | /// MCDisassembler::Fail if the instruction was invalid. |
| 79 | virtual DecodeStatus getInstruction(MCInst& instr, |
| 80 | uint64_t& size, |
| 81 | const MemoryObject ®ion, |
| 82 | uint64_t address, |
| 83 | raw_ostream &vStream, |
| 84 | raw_ostream &cStream) const = 0; |
| 85 | private: |
| 86 | MCContext &Ctx; |
| 87 | |
| 88 | protected: |
| 89 | // Subtarget information, for instruction decoding predicates if required. |
| 90 | const MCSubtargetInfo &STI; |
| 91 | std::unique_ptr<MCSymbolizer> Symbolizer; |
| 92 | |
| 93 | public: |
| 94 | // Helpers around MCSymbolizer |
| 95 | bool tryAddingSymbolicOperand(MCInst &Inst, |
| 96 | int64_t Value, |
| 97 | uint64_t Address, bool IsBranch, |
| 98 | uint64_t Offset, uint64_t InstSize) const; |
| 99 | |
| 100 | void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const; |
| 101 | |
| 102 | /// Set \p Symzer as the current symbolizer. |
| 103 | /// This takes ownership of \p Symzer, and deletes the previously set one. |
| 104 | void setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer); |
| 105 | |
| 106 | MCContext& getContext() const { return Ctx; } |
| 107 | |
| 108 | const MCSubtargetInfo& getSubtargetInfo() const { return STI; } |
| 109 | |
| 110 | // Marked mutable because we cache it inside the disassembler, rather than |
| 111 | // having to pass it around as an argument through all the autogenerated code. |
| 112 | mutable raw_ostream *CommentStream; |
| 113 | }; |
| 114 | |
| 115 | } // namespace llvm |
| 116 | |
| 117 | #endif |