Replace OwningPtr<T> with std::unique_ptr<T>.
[oota-llvm.git] / include / llvm / MC / MCDisassembler.h
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/MCRelocationInfo.h"
15 #include "llvm/MC/MCSymbolizer.h"
16 #include "llvm/Support/DataTypes.h"
17
18 namespace llvm {
19
20 class MCInst;
21 class MCSubtargetInfo;
22 class MemoryObject;
23 class raw_ostream;
24 class MCContext;
25
26 /// MCDisassembler - Superclass for all disassemblers.  Consumes a memory region
27 ///   and provides an array of assembly instructions.
28 class MCDisassembler {
29 public:
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)
60       : GetOpInfo(0), SymbolLookUp(0), DisInfo(0), Ctx(0), STI(STI),
61         Symbolizer(), CommentStream(0) {}
62
63   virtual ~MCDisassembler();
64
65   /// getInstruction  - Returns the disassembly of a single instruction.
66   ///
67   /// @param instr    - An MCInst to populate with the contents of the
68   ///                   instruction.
69   /// @param size     - A value to populate with the size of the instruction, or
70   ///                   the number of bytes consumed while attempting to decode
71   ///                   an invalid instruction.
72   /// @param region   - The memory object to use as a source for machine code.
73   /// @param address  - The address, in the memory space of region, of the first
74   ///                   byte of the instruction.
75   /// @param vStream  - The stream to print warnings and diagnostic messages on.
76   /// @param cStream  - The stream to print comments and annotations on.
77   /// @return         - MCDisassembler::Success if the instruction is valid,
78   ///                   MCDisassembler::SoftFail if the instruction was
79   ///                                            disassemblable but invalid,
80   ///                   MCDisassembler::Fail if the instruction was invalid.
81   virtual DecodeStatus  getInstruction(MCInst& instr,
82                                        uint64_t& size,
83                                        const MemoryObject &region,
84                                        uint64_t address,
85                                        raw_ostream &vStream,
86                                        raw_ostream &cStream) const = 0;
87 private:
88   //
89   // Hooks for symbolic disassembly via the public 'C' interface.
90   //
91   // The function to get the symbolic information for operands.
92   LLVMOpInfoCallback GetOpInfo;
93   // The function to lookup a symbol name.
94   LLVMSymbolLookupCallback SymbolLookUp;
95   // The pointer to the block of symbolic information for above call back.
96   void *DisInfo;
97   // The assembly context for creating symbols and MCExprs in place of
98   // immediate operands when there is symbolic information.
99   MCContext *Ctx;
100
101 protected:
102   // Subtarget information, for instruction decoding predicates if required.
103   const MCSubtargetInfo &STI;
104   std::unique_ptr<MCSymbolizer> Symbolizer;
105
106 public:
107   // Helpers around MCSymbolizer
108   bool tryAddingSymbolicOperand(MCInst &Inst,
109                                 int64_t Value,
110                                 uint64_t Address, bool IsBranch,
111                                 uint64_t Offset, uint64_t InstSize) const;
112
113   void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
114
115   /// Set \p Symzer as the current symbolizer.
116   /// This takes ownership of \p Symzer, and deletes the previously set one.
117   void setSymbolizer(std::unique_ptr<MCSymbolizer> &Symzer);
118
119   /// Sets up an external symbolizer that uses the C API callbacks.
120   void setupForSymbolicDisassembly(LLVMOpInfoCallback GetOpInfo,
121                                    LLVMSymbolLookupCallback SymbolLookUp,
122                                    void *DisInfo,
123                                    MCContext *Ctx,
124                                    OwningPtr<MCRelocationInfo> &RelInfo);
125   void setupForSymbolicDisassembly(LLVMOpInfoCallback GetOpInfo,
126                                    LLVMSymbolLookupCallback SymbolLookUp,
127                                    void *DisInfo,
128                                    MCContext *Ctx,
129                                    std::unique_ptr<MCRelocationInfo> &RelInfo);
130
131   LLVMOpInfoCallback getLLVMOpInfoCallback() const { return GetOpInfo; }
132   LLVMSymbolLookupCallback getLLVMSymbolLookupCallback() const {
133     return SymbolLookUp;
134   }
135   void *getDisInfoBlock() const { return DisInfo; }
136   MCContext *getMCContext() const { return Ctx; }
137
138   const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
139
140   // Marked mutable because we cache it inside the disassembler, rather than
141   // having to pass it around as an argument through all the autogenerated code.
142   mutable raw_ostream *CommentStream;
143 };
144
145 } // namespace llvm
146
147 #endif