LLVM  9.0.0svn
MCDisassembler.h
Go to the documentation of this file.
1 //===- llvm/MC/MCDisassembler.h - Disassembler interface --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_MC_MCDISASSEMBLER_MCDISASSEMBLER_H
10 #define LLVM_MC_MCDISASSEMBLER_MCDISASSEMBLER_H
11 
13 #include <cstdint>
14 #include <memory>
15 
16 namespace llvm {
17 
18 template <typename T> class ArrayRef;
19 class StringRef;
20 class MCContext;
21 class MCInst;
22 class MCSubtargetInfo;
23 class raw_ostream;
24 
25 /// Superclass for all disassemblers. Consumes a memory region and provides an
26 /// array of assembly instructions.
28 public:
29  /// Ternary decode status. Most backends will just use Fail and
30  /// Success, however some have a concept of an instruction with
31  /// understandable semantics but which is architecturally
32  /// incorrect. An example of this is ARM UNPREDICTABLE instructions
33  /// which are disassemblable but cause undefined behaviour.
34  ///
35  /// Because it makes sense to disassemble these instructions, there
36  /// is a "soft fail" failure mode that indicates the MCInst& is
37  /// valid but architecturally incorrect.
38  ///
39  /// The enum numbers are deliberately chosen such that reduction
40  /// from Success->SoftFail ->Fail can be done with a simple
41  /// bitwise-AND:
42  ///
43  /// LEFT & TOP = | Success Unpredictable Fail
44  /// --------------+-----------------------------------
45  /// Success | Success Unpredictable Fail
46  /// Unpredictable | Unpredictable Unpredictable Fail
47  /// Fail | Fail Fail Fail
48  ///
49  /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
50  /// Success, SoftFail, Fail respectively.
51  enum DecodeStatus {
52  Fail = 0,
53  SoftFail = 1,
54  Success = 3
55  };
56 
58  : Ctx(Ctx), STI(STI) {}
59 
60  virtual ~MCDisassembler();
61 
62  /// Returns the disassembly of a single instruction.
63  ///
64  /// \param Instr - An MCInst to populate with the contents of the
65  /// instruction.
66  /// \param Size - A value to populate with the size of the instruction, or
67  /// the number of bytes consumed while attempting to decode
68  /// an invalid instruction.
69  /// \param Address - The address, in the memory space of region, of the first
70  /// byte of the instruction.
71  /// \param Bytes - A reference to the actual bytes of the instruction.
72  /// \param VStream - The stream to print warnings and diagnostic messages on.
73  /// \param CStream - The stream to print comments and annotations on.
74  /// \return - MCDisassembler::Success if the instruction is valid,
75  /// MCDisassembler::SoftFail if the instruction was
76  /// disassemblable but invalid,
77  /// MCDisassembler::Fail if the instruction was invalid.
78  virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
79  ArrayRef<uint8_t> Bytes, uint64_t Address,
80  raw_ostream &VStream,
81  raw_ostream &CStream) const = 0;
82 
83  /// May parse any prelude that precedes instructions after the start of a
84  /// symbol. Needed for some targets, e.g. WebAssembly.
85  ///
86  /// \param Name - The name of the symbol.
87  /// \param Size - The number of bytes consumed.
88  /// \param Address - The address, in the memory space of region, of the first
89  /// byte of the symbol.
90  /// \param Bytes - A reference to the actual bytes at the symbol location.
91  /// \param VStream - The stream to print warnings and diagnostic messages on.
92  /// \param CStream - The stream to print comments and annotations on.
93  /// \return - MCDisassembler::Success if the bytes are valid,
94  /// MCDisassembler::Fail if the bytes were invalid.
95  virtual DecodeStatus onSymbolStart(StringRef Name, uint64_t &Size,
96  ArrayRef<uint8_t> Bytes, uint64_t Address,
97  raw_ostream &VStream,
98  raw_ostream &CStream) const;
99 
100 private:
101  MCContext &Ctx;
102 
103 protected:
104  // Subtarget information, for instruction decoding predicates if required.
106  std::unique_ptr<MCSymbolizer> Symbolizer;
107 
108 public:
109  // Helpers around MCSymbolizer
110  bool tryAddingSymbolicOperand(MCInst &Inst,
111  int64_t Value,
112  uint64_t Address, bool IsBranch,
113  uint64_t Offset, uint64_t InstSize) const;
114 
115  void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
116 
117  /// Set \p Symzer as the current symbolizer.
118  /// This takes ownership of \p Symzer, and deletes the previously set one.
119  void setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer);
120 
121  MCContext& getContext() const { return Ctx; }
122 
123  const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
124 
125  // Marked mutable because we cache it inside the disassembler, rather than
126  // having to pass it around as an argument through all the autogenerated code.
127  mutable raw_ostream *CommentStream = nullptr;
128 };
129 
130 } // end namespace llvm
131 
132 #endif // LLVM_MC_MCDISASSEMBLER_MCDISASSEMBLER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
DecodeStatus
Ternary decode status.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Superclass for all disassemblers.
raw_ostream * CommentStream
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize) const
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:64
const MCSubtargetInfo & STI
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const
virtual ~MCDisassembler()
virtual DecodeStatus onSymbolStart(StringRef Name, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &VStream, raw_ostream &CStream) const
May parse any prelude that precedes instructions after the start of a symbol.
MCContext & getContext() const
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:46
LLVM Value Representation.
Definition: Value.h:72
const MCSubtargetInfo & getSubtargetInfo() const
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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.