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