LLVM  3.7.0
MCDisassembler.h
Go to the documentation of this file.
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.
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 
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.
87  std::unique_ptr<MCSymbolizer> Symbolizer;
88 
89 public:
90  // Helpers around MCSymbolizer
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.
109 };
110 
111 } // namespace llvm
112 
113 #endif
DecodeStatus
Ternary decode status.
Superclass for all disassemblers.
raw_ostream * CommentStream
void setSymbolizer(std::unique_ptr< MCSymbolizer > Symzer)
Set Symzer as the current symbolizer.
std::unique_ptr< MCSymbolizer > Symbolizer
MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
Context object for machine code objects.
Definition: MCContext.h:48
const MCSubtargetInfo & STI
MCContext & getContext() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
const MCSubtargetInfo & getSubtargetInfo() const
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize) const
MCSubtargetInfo - Generic base class for all target subtargets.
LLVM Value Representation.
Definition: Value.h:69
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &VStream, raw_ostream &CStream) const =0
Returns the disassembly of a single instruction.
void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const