LLVM API Documentation

MCDisassembler.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCDisassembler.h - Disassembler interface -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 #ifndef LLVM_MC_MCDISASSEMBLER_H
00010 #define LLVM_MC_MCDISASSEMBLER_H
00011 
00012 #include "llvm-c/Disassembler.h"
00013 #include "llvm/Support/DataTypes.h"
00014 
00015 namespace llvm {
00016 
00017 class MCInst;
00018 class MCSubtargetInfo;
00019 class MemoryObject;
00020 class raw_ostream;
00021 class MCContext;
00022 
00023 /// MCDisassembler - Superclass for all disassemblers.  Consumes a memory region
00024 ///   and provides an array of assembly instructions.
00025 class MCDisassembler {
00026 public:
00027   /// Ternary decode status. Most backends will just use Fail and
00028   /// Success, however some have a concept of an instruction with
00029   /// understandable semantics but which is architecturally
00030   /// incorrect. An example of this is ARM UNPREDICTABLE instructions
00031   /// which are disassemblable but cause undefined behaviour.
00032   ///
00033   /// Because it makes sense to disassemble these instructions, there
00034   /// is a "soft fail" failure mode that indicates the MCInst& is
00035   /// valid but architecturally incorrect.
00036   ///
00037   /// The enum numbers are deliberately chosen such that reduction
00038   /// from Success->SoftFail ->Fail can be done with a simple
00039   /// bitwise-AND:
00040   ///
00041   ///   LEFT & TOP =  | Success       Unpredictable   Fail
00042   ///   --------------+-----------------------------------
00043   ///   Success       | Success       Unpredictable   Fail
00044   ///   Unpredictable | Unpredictable Unpredictable   Fail
00045   ///   Fail          | Fail          Fail            Fail
00046   ///
00047   /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
00048   /// Success, SoftFail, Fail respectively.
00049   enum DecodeStatus {
00050     Fail = 0,
00051     SoftFail = 1,
00052     Success = 3
00053   };
00054 
00055   /// Constructor     - Performs initial setup for the disassembler.
00056   MCDisassembler(const MCSubtargetInfo &STI) : GetOpInfo(0), SymbolLookUp(0),
00057                                                DisInfo(0), Ctx(0),
00058                                                STI(STI), CommentStream(0) {}
00059 
00060   virtual ~MCDisassembler();
00061 
00062   /// getInstruction  - Returns the disassembly of a single instruction.
00063   ///
00064   /// @param instr    - An MCInst to populate with the contents of the
00065   ///                   instruction.
00066   /// @param size     - A value to populate with the size of the instruction, or
00067   ///                   the number of bytes consumed while attempting to decode
00068   ///                   an invalid instruction.
00069   /// @param region   - The memory object to use as a source for machine code.
00070   /// @param address  - The address, in the memory space of region, of the first
00071   ///                   byte of the instruction.
00072   /// @param vStream  - The stream to print warnings and diagnostic messages on.
00073   /// @param cStream  - The stream to print comments and annotations on.
00074   /// @return         - MCDisassembler::Success if the instruction is valid,
00075   ///                   MCDisassembler::SoftFail if the instruction was
00076   ///                                            disassemblable but invalid,
00077   ///                   MCDisassembler::Fail if the instruction was invalid.
00078   virtual DecodeStatus  getInstruction(MCInst& instr,
00079                                        uint64_t& size,
00080                                        const MemoryObject &region,
00081                                        uint64_t address,
00082                                        raw_ostream &vStream,
00083                                        raw_ostream &cStream) const = 0;
00084 
00085 private:
00086   //
00087   // Hooks for symbolic disassembly via the public 'C' interface.
00088   //
00089   // The function to get the symbolic information for operands.
00090   LLVMOpInfoCallback GetOpInfo;
00091   // The function to lookup a symbol name.
00092   LLVMSymbolLookupCallback SymbolLookUp;
00093   // The pointer to the block of symbolic information for above call back.
00094   void *DisInfo;
00095   // The assembly context for creating symbols and MCExprs in place of
00096   // immediate operands when there is symbolic information.
00097   MCContext *Ctx;
00098 protected:
00099   // Subtarget information, for instruction decoding predicates if required.
00100   const MCSubtargetInfo &STI;
00101 
00102 public:
00103   void setupForSymbolicDisassembly(LLVMOpInfoCallback getOpInfo,
00104                                    LLVMSymbolLookupCallback symbolLookUp,
00105                                    void *disInfo,
00106                                    MCContext *ctx) {
00107     GetOpInfo = getOpInfo;
00108     SymbolLookUp = symbolLookUp;
00109     DisInfo = disInfo;
00110     Ctx = ctx;
00111   }
00112   LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
00113   LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const {
00114     return SymbolLookUp;
00115   }
00116   void *getDisInfoBlock() const { return DisInfo; }
00117   MCContext *getMCContext() const { return Ctx; }
00118 
00119   // Marked mutable because we cache it inside the disassembler, rather than
00120   // having to pass it around as an argument through all the autogenerated code.
00121   mutable raw_ostream *CommentStream;
00122 };
00123 
00124 } // namespace llvm
00125 
00126 #endif