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/MC/MCRelocationInfo.h"
00014 #include "llvm/MC/MCSymbolizer.h"
00015 #include "llvm/Support/DataTypes.h"
00016 
00017 namespace llvm {
00018 
00019 class MCInst;
00020 class MCSubtargetInfo;
00021 class MemoryObject;
00022 class raw_ostream;
00023 class MCContext;
00024 
00025 /// MCDisassembler - Superclass for all disassemblers.  Consumes a memory region
00026 ///   and provides an array of assembly instructions.
00027 class MCDisassembler {
00028 public:
00029   /// Ternary decode status. Most backends will just use Fail and
00030   /// Success, however some have a concept of an instruction with
00031   /// understandable semantics but which is architecturally
00032   /// incorrect. An example of this is ARM UNPREDICTABLE instructions
00033   /// which are disassemblable but cause undefined behaviour.
00034   ///
00035   /// Because it makes sense to disassemble these instructions, there
00036   /// is a "soft fail" failure mode that indicates the MCInst& is
00037   /// valid but architecturally incorrect.
00038   ///
00039   /// The enum numbers are deliberately chosen such that reduction
00040   /// from Success->SoftFail ->Fail can be done with a simple
00041   /// bitwise-AND:
00042   ///
00043   ///   LEFT & TOP =  | Success       Unpredictable   Fail
00044   ///   --------------+-----------------------------------
00045   ///   Success       | Success       Unpredictable   Fail
00046   ///   Unpredictable | Unpredictable Unpredictable   Fail
00047   ///   Fail          | Fail          Fail            Fail
00048   ///
00049   /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
00050   /// Success, SoftFail, Fail respectively.
00051   enum DecodeStatus {
00052     Fail = 0,
00053     SoftFail = 1,
00054     Success = 3
00055   };
00056 
00057   /// Constructor     - Performs initial setup for the disassembler.
00058   MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
00059     : Ctx(Ctx), STI(STI), Symbolizer(), CommentStream(nullptr) {}
00060 
00061   virtual ~MCDisassembler();
00062 
00063   /// getInstruction  - Returns the disassembly of a single instruction.
00064   ///
00065   /// @param instr    - An MCInst to populate with the contents of the
00066   ///                   instruction.
00067   /// @param size     - A value to populate with the size of the instruction, or
00068   ///                   the number of bytes consumed while attempting to decode
00069   ///                   an invalid instruction.
00070   /// @param region   - The memory object to use as a source for machine code.
00071   /// @param address  - The address, in the memory space of region, of the first
00072   ///                   byte of the instruction.
00073   /// @param vStream  - The stream to print warnings and diagnostic messages on.
00074   /// @param cStream  - The stream to print comments and annotations on.
00075   /// @return         - MCDisassembler::Success if the instruction is valid,
00076   ///                   MCDisassembler::SoftFail if the instruction was
00077   ///                                            disassemblable but invalid,
00078   ///                   MCDisassembler::Fail if the instruction was invalid.
00079   virtual DecodeStatus  getInstruction(MCInst& instr,
00080                                        uint64_t& size,
00081                                        const MemoryObject &region,
00082                                        uint64_t address,
00083                                        raw_ostream &vStream,
00084                                        raw_ostream &cStream) const = 0;
00085 private:
00086   MCContext &Ctx;
00087 
00088 protected:
00089   // Subtarget information, for instruction decoding predicates if required.
00090   const MCSubtargetInfo &STI;
00091   std::unique_ptr<MCSymbolizer> Symbolizer;
00092 
00093 public:
00094   // Helpers around MCSymbolizer
00095   bool tryAddingSymbolicOperand(MCInst &Inst,
00096                                 int64_t Value,
00097                                 uint64_t Address, bool IsBranch,
00098                                 uint64_t Offset, uint64_t InstSize) const;
00099 
00100   void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
00101 
00102   /// Set \p Symzer as the current symbolizer.
00103   /// This takes ownership of \p Symzer, and deletes the previously set one.
00104   void setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer);
00105 
00106   MCContext& getContext() const { return Ctx; }
00107 
00108   const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
00109 
00110   // Marked mutable because we cache it inside the disassembler, rather than
00111   // having to pass it around as an argument through all the autogenerated code.
00112   mutable raw_ostream *CommentStream;
00113 };
00114 
00115 } // namespace llvm
00116 
00117 #endif