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