LLVM  15.0.0git
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 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
16 #include <cstdint>
17 #include <memory>
18 #include <vector>
19 
20 namespace llvm {
21 
25  bool IsLabel;
27  Optional<uint32_t> Idx, bool Label)
28  : StorageMappingClass(Smc), Index(Idx), IsLabel(Label) {}
29 
30  bool operator<(const XCOFFSymbolInfo &SymInfo) const;
31 };
32 
33 struct SymbolInfoTy {
36  union {
37  uint8_t Type;
39  };
40 
41 private:
42  bool IsXCOFF;
43  bool HasType;
44 
45 public:
48  bool Label)
49  : Addr(Addr), Name(Name), XCOFFSymInfo(Smc, Idx, Label), IsXCOFF(true),
50  HasType(false) {}
52  bool IsXCOFF = false)
53  : Addr(Addr), Name(Name), Type(Type), IsXCOFF(IsXCOFF), HasType(true) {}
54  bool isXCOFF() const { return IsXCOFF; }
55 
56 private:
57  friend bool operator<(const SymbolInfoTy &P1, const SymbolInfoTy &P2) {
58  assert((P1.IsXCOFF == P2.IsXCOFF && P1.HasType == P2.HasType) &&
59  "The value of IsXCOFF and HasType in P1 and P2 should be the same "
60  "respectively.");
61 
62  if (P1.IsXCOFF && P1.HasType)
63  return std::tie(P1.Addr, P1.Type, P1.Name) <
64  std::tie(P2.Addr, P2.Type, P2.Name);
65 
66  if (P1.IsXCOFF)
67  return std::tie(P1.Addr, P1.XCOFFSymInfo, P1.Name) <
68  std::tie(P2.Addr, P2.XCOFFSymInfo, P2.Name);
69 
70  return std::tie(P1.Addr, P1.Name, P1.Type) <
71  std::tie(P2.Addr, P2.Name, P2.Type);
72  }
73 };
74 
75 using SectionSymbolsTy = std::vector<SymbolInfoTy>;
76 
77 template <typename T> class ArrayRef;
78 class MCContext;
79 class MCInst;
80 class MCSubtargetInfo;
81 class raw_ostream;
82 
83 /// Superclass for all disassemblers. Consumes a memory region and provides an
84 /// array of assembly instructions.
86 public:
87  /// Ternary decode status. Most backends will just use Fail and
88  /// Success, however some have a concept of an instruction with
89  /// understandable semantics but which is architecturally
90  /// incorrect. An example of this is ARM UNPREDICTABLE instructions
91  /// which are disassemblable but cause undefined behaviour.
92  ///
93  /// Because it makes sense to disassemble these instructions, there
94  /// is a "soft fail" failure mode that indicates the MCInst& is
95  /// valid but architecturally incorrect.
96  ///
97  /// The enum numbers are deliberately chosen such that reduction
98  /// from Success->SoftFail ->Fail can be done with a simple
99  /// bitwise-AND:
100  ///
101  /// LEFT & TOP = | Success Unpredictable Fail
102  /// --------------+-----------------------------------
103  /// Success | Success Unpredictable Fail
104  /// Unpredictable | Unpredictable Unpredictable Fail
105  /// Fail | Fail Fail Fail
106  ///
107  /// An easy way of encoding this is as 0b11, 0b01, 0b00 for
108  /// Success, SoftFail, Fail respectively.
110  Fail = 0,
111  SoftFail = 1,
113  };
114 
116  : Ctx(Ctx), STI(STI) {}
117 
118  virtual ~MCDisassembler();
119 
120  /// Returns the disassembly of a single instruction.
121  ///
122  /// \param Instr - An MCInst to populate with the contents of the
123  /// instruction.
124  /// \param Size - A value to populate with the size of the instruction, or
125  /// the number of bytes consumed while attempting to decode
126  /// an invalid instruction.
127  /// \param Address - The address, in the memory space of region, of the first
128  /// byte of the instruction.
129  /// \param Bytes - A reference to the actual bytes of the instruction.
130  /// \param CStream - The stream to print comments and annotations on.
131  /// \return - MCDisassembler::Success if the instruction is valid,
132  /// MCDisassembler::SoftFail if the instruction was
133  /// disassemblable but invalid,
134  /// MCDisassembler::Fail if the instruction was invalid.
135  virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size,
136  ArrayRef<uint8_t> Bytes, uint64_t Address,
137  raw_ostream &CStream) const = 0;
138 
139  /// Used to perform separate target specific disassembly for a particular
140  /// symbol. May parse any prelude that precedes instructions after the
141  /// start of a symbol, or the entire symbol.
142  /// This is used for example by WebAssembly to decode preludes.
143  ///
144  /// Base implementation returns None. So all targets by default ignore to
145  /// treat symbols separately.
146  ///
147  /// \param Symbol - The symbol.
148  /// \param Size - The number of bytes consumed.
149  /// \param Address - The address, in the memory space of region, of the first
150  /// byte of the symbol.
151  /// \param Bytes - A reference to the actual bytes at the symbol location.
152  /// \param CStream - The stream to print comments and annotations on.
153  /// \return - MCDisassembler::Success if bytes are decoded
154  /// successfully. Size must hold the number of bytes that
155  /// were decoded.
156  /// - MCDisassembler::Fail if the bytes are invalid. Size
157  /// must hold the number of bytes that were decoded before
158  /// failing. The target must print nothing. This can be
159  /// done by buffering the output if needed.
160  /// - None if the target doesn't want to handle the symbol
161  /// separately. Value of Size is ignored in this case.
162  virtual Optional<DecodeStatus>
164  uint64_t Address, raw_ostream &CStream) const;
165  // TODO:
166  // Implement similar hooks that can be used at other points during
167  // disassembly. Something along the following lines:
168  // - onBeforeInstructionDecode()
169  // - onAfterInstructionDecode()
170  // - onSymbolEnd()
171  // It should help move much of the target specific code from llvm-objdump to
172  // respective target disassemblers.
173 
174 private:
175  MCContext &Ctx;
176 
177 protected:
178  // Subtarget information, for instruction decoding predicates if required.
180  std::unique_ptr<MCSymbolizer> Symbolizer;
181 
182 public:
183  // Helpers around MCSymbolizer
184  bool tryAddingSymbolicOperand(MCInst &Inst,
185  int64_t Value,
186  uint64_t Address, bool IsBranch,
187  uint64_t Offset, uint64_t InstSize) const;
188 
189  void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const;
190 
191  /// Set \p Symzer as the current symbolizer.
192  /// This takes ownership of \p Symzer, and deletes the previously set one.
193  void setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer);
194 
195  MCContext& getContext() const { return Ctx; }
196 
197  const MCSubtargetInfo& getSubtargetInfo() const { return STI; }
198 
199  // Marked mutable because we cache it inside the disassembler, rather than
200  // having to pass it around as an argument through all the autogenerated code.
201  mutable raw_ostream *CommentStream = nullptr;
202 };
203 
204 } // end namespace llvm
205 
206 #endif // LLVM_MC_MCDISASSEMBLER_MCDISASSEMBLER_H
llvm::XCOFFSymbolInfo
Definition: MCDisassembler.h:22
llvm::XCOFFSymbolInfo::operator<
bool operator<(const XCOFFSymbolInfo &SymInfo) const
The function is for symbol sorting when symbols have the same address.
Definition: MCDisassembler.cpp:79
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
Optional.h
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:74
llvm::SymbolInfoTy
Definition: MCDisassembler.h:33
StringRef.h
llvm::MCDisassembler::onSymbolStart
virtual Optional< DecodeStatus > onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const
Used to perform separate target specific disassembly for a particular symbol.
Definition: MCDisassembler.cpp:17
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1909
llvm::MCDisassembler::SoftFail
@ SoftFail
Definition: MCDisassembler.h:111
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Optional< XCOFF::StorageMappingClass >
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::XCOFFSymbolInfo::XCOFFSymbolInfo
XCOFFSymbolInfo(Optional< XCOFF::StorageMappingClass > Smc, Optional< uint32_t > Idx, bool Label)
Definition: MCDisassembler.h:26
llvm::XCOFFSymbolInfo::StorageMappingClass
Optional< XCOFF::StorageMappingClass > StorageMappingClass
Definition: MCDisassembler.h:23
XCOFF.h
llvm::SymbolInfoTy::Addr
uint64_t Addr
Definition: MCDisassembler.h:34
llvm::MCDisassembler::MCDisassembler
MCDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx)
Definition: MCDisassembler.h:115
P2
This might compile to this xmm1 xorps xmm0 movss xmm0 ret Now consider if the code caused xmm1 to get spilled This might produce this xmm1 movaps xmm0 movaps xmm1 movss xmm0 ret since the reload is only used by these we could fold it into the producing something like xmm1 movaps xmm0 ret saving two instructions The basic idea is that a reload from a spill if only one byte chunk is bring in zeros the one element instead of elements This can be used to simplify a variety of shuffle where the elements are fixed zeros This code generates ugly probably due to costs being off or< 4 x float > * P2
Definition: README-SSE.txt:278
llvm::MCDisassembler::Success
@ Success
Definition: MCDisassembler.h:112
llvm::MCDisassembler::CommentStream
raw_ostream * CommentStream
Definition: MCDisassembler.h:201
false
Definition: StackSlotColoring.cpp:141
llvm::SymbolInfoTy::Name
StringRef Name
Definition: MCDisassembler.h:35
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::SectionSymbolsTy
std::vector< SymbolInfoTy > SectionSymbolsTy
Definition: MCDisassembler.h:75
llvm::MCDisassembler::getInstruction
virtual DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, ArrayRef< uint8_t > Bytes, uint64_t Address, raw_ostream &CStream) const =0
Returns the disassembly of a single instruction.
llvm::MCDisassembler::DecodeStatus
DecodeStatus
Ternary decode status.
Definition: MCDisassembler.h:109
llvm::MCDisassembler::setSymbolizer
void setSymbolizer(std::unique_ptr< MCSymbolizer > Symzer)
Set Symzer as the current symbolizer.
Definition: MCDisassembler.cpp:39
llvm::XCOFFSymbolInfo::Index
Optional< uint32_t > Index
Definition: MCDisassembler.h:24
llvm::MCDisassembler::STI
const MCSubtargetInfo & STI
Definition: MCDisassembler.h:179
llvm::XCOFF::StorageMappingClass
StorageMappingClass
Storage Mapping Class definitions.
Definition: XCOFF.h:87
uint64_t
llvm::MCDisassembler::~MCDisassembler
virtual ~MCDisassembler()
llvm::MCDisassembler::tryAddingPcLoadReferenceComment
void tryAddingPcLoadReferenceComment(int64_t Value, uint64_t Address) const
Definition: MCDisassembler.cpp:33
llvm::MCDisassembler
Superclass for all disassemblers.
Definition: MCDisassembler.h:85
llvm::SymbolInfoTy::operator<
friend bool operator<(const SymbolInfoTy &P1, const SymbolInfoTy &P2)
Definition: MCDisassembler.h:57
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SymbolInfoTy::isXCOFF
bool isXCOFF() const
Definition: MCDisassembler.h:54
MCSymbolizer.h
llvm::MCDisassembler::tryAddingSymbolicOperand
bool tryAddingSymbolicOperand(MCInst &Inst, int64_t Value, uint64_t Address, bool IsBranch, uint64_t Offset, uint64_t InstSize) const
Definition: MCDisassembler.cpp:23
DecodeStatus
MCDisassembler::DecodeStatus DecodeStatus
Definition: AArch64Disassembler.cpp:37
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCDisassembler::Fail
@ Fail
Definition: MCDisassembler.h:110
llvm::MCDisassembler::getContext
MCContext & getContext() const
Definition: MCDisassembler.h:195
llvm::SymbolInfoTy::SymbolInfoTy
SymbolInfoTy(uint64_t Addr, StringRef Name, Optional< XCOFF::StorageMappingClass > Smc, Optional< uint32_t > Idx, bool Label)
Definition: MCDisassembler.h:46
llvm::SymbolInfoTy::SymbolInfoTy
SymbolInfoTy(uint64_t Addr, StringRef Name, uint8_t Type, bool IsXCOFF=false)
Definition: MCDisassembler.h:51
llvm::MCDisassembler::getSubtargetInfo
const MCSubtargetInfo & getSubtargetInfo() const
Definition: MCDisassembler.h:197
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:83
llvm::XCOFFSymbolInfo::IsLabel
bool IsLabel
Definition: MCDisassembler.h:25
SymInfo
SymInfo contains information about symbol: it's address and section index which is -1LL for absolute ...
Definition: DWARFContext.cpp:1458
llvm::SymbolInfoTy::Type
uint8_t Type
Definition: MCDisassembler.h:37
llvm::SymbolInfoTy::XCOFFSymInfo
XCOFFSymbolInfo XCOFFSymInfo
Definition: MCDisassembler.h:38
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCDisassembler::Symbolizer
std::unique_ptr< MCSymbolizer > Symbolizer
Definition: MCDisassembler.h:180