LLVM  15.0.0git
DWARFExpression.h
Go to the documentation of this file.
1 //===--- DWARFExpression.h - DWARF Expression handling ----------*- 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_DEBUGINFO_DWARF_DWARFEXPRESSION_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFEXPRESSION_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/iterator.h"
17 
18 namespace llvm {
19 class DWARFUnit;
20 struct DIDumpOptions;
21 class MCRegisterInfo;
22 class raw_ostream;
23 
25 public:
26  class iterator;
27 
28  /// This class represents an Operation in the Expression. Each operation can
29  /// have up to 2 oprerands.
30  ///
31  /// An Operation can be in Error state (check with isError()). This
32  /// means that it couldn't be decoded successfully and if it is the
33  /// case, all others fields contain undefined values.
34  class Operation {
35  public:
36  /// Size and signedness of expression operations' operands.
37  enum Encoding : uint8_t {
38  Size1 = 0,
39  Size2 = 1,
40  Size4 = 2,
41  Size8 = 3,
42  SizeLEB = 4,
43  SizeAddr = 5,
45  SizeBlock = 7, ///< Preceding operand contains block size
48  SignBit = 0x80,
54  SizeNA = 0xFF ///< Unused operands get this encoding.
55  };
56 
57  enum DwarfVersion : uint8_t {
58  DwarfNA, ///< Serves as a marker for unused entries
59  Dwarf2 = 2,
63  };
64 
65  /// Description of the encoding of one expression Op.
66  struct Description {
67  DwarfVersion Version; ///< Dwarf version where the Op was introduced.
68  Encoding Op[2]; ///< Encoding for Op operands, or SizeNA.
69 
71  Encoding Op2 = SizeNA)
72  : Version(Version) {
73  Op[0] = Op1;
74  Op[1] = Op2;
75  }
76  };
77 
78  private:
80  uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
81  Description Desc;
82  bool Error = false;
83  uint64_t EndOffset;
84  uint64_t Operands[2];
85  uint64_t OperandEndOffsets[2];
86 
87  public:
88  const Description &getDescription() const { return Desc; }
89  uint8_t getCode() const { return Opcode; }
90  uint64_t getRawOperand(unsigned Idx) const { return Operands[Idx]; }
91  uint64_t getOperandEndOffset(unsigned Idx) const {
92  return OperandEndOffsets[Idx];
93  }
94  uint64_t getEndOffset() const { return EndOffset; }
95  bool isError() const { return Error; }
96  bool print(raw_ostream &OS, DIDumpOptions DumpOpts,
97  const DWARFExpression *Expr, const MCRegisterInfo *RegInfo,
98  DWARFUnit *U, bool isEH) const;
99 
100  /// Verify \p Op. Does not affect the return of \a isError().
101  static bool verify(const Operation &Op, DWARFUnit *U);
102 
103  private:
104  bool extract(DataExtractor Data, uint8_t AddressSize, uint64_t Offset,
106  };
107 
108  /// An iterator to go through the expression operations.
109  class iterator
110  : public iterator_facade_base<iterator, std::forward_iterator_tag,
111  const Operation> {
112  friend class DWARFExpression;
113  const DWARFExpression *Expr;
115  Operation Op;
117  : Expr(Expr), Offset(Offset) {
118  Op.Error =
119  Offset >= Expr->Data.getData().size() ||
120  !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
121  }
122 
123  public:
125  Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
126  Op.Error =
127  Offset >= Expr->Data.getData().size() ||
128  !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
129  return *this;
130  }
131 
132  const Operation &operator*() const { return Op; }
133 
135  return iterator(Expr, Op.EndOffset + Add);
136  }
137 
138  // Comparison operators are provided out of line.
139  friend bool operator==(const iterator &, const iterator &);
140  };
141 
142  DWARFExpression(DataExtractor Data, uint8_t AddressSize,
144  : Data(Data), AddressSize(AddressSize), Format(Format) {
145  assert(AddressSize == 8 || AddressSize == 4 || AddressSize == 2);
146  }
147 
148  iterator begin() const { return iterator(this, 0); }
149  iterator end() const { return iterator(this, Data.getData().size()); }
150 
151  void print(raw_ostream &OS, DIDumpOptions DumpOpts,
152  const MCRegisterInfo *RegInfo, DWARFUnit *U,
153  bool IsEH = false) const;
154 
155  /// Print the expression in a format intended to be compact and useful to a
156  /// user, but not perfectly unambiguous, or capable of representing every
157  /// valid DWARF expression. Returns true if the expression was sucessfully
158  /// printed.
160 
161  bool verify(DWARFUnit *U);
162 
163  bool operator==(const DWARFExpression &RHS) const;
164 
165  StringRef getData() const { return Data.getData(); }
166 
167 private:
169  uint8_t AddressSize;
171 };
172 
175  return LHS.Expr == RHS.Expr && LHS.Offset == RHS.Offset;
176 }
177 }
178 #endif
llvm::DWARFExpression::iterator
An iterator to go through the expression operations.
Definition: DWARFExpression.h:109
llvm::DWARFExpression::Operation::Dwarf5
@ Dwarf5
Definition: DWARFExpression.h:62
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DWARFExpression::Operation::Description::Version
DwarfVersion Version
Dwarf version where the Op was introduced.
Definition: DWARFExpression.h:67
llvm::lltok::Error
@ Error
Definition: LLToken.h:21
Optional.h
llvm::DWARFExpression::Operation::SignedSize2
@ SignedSize2
Definition: DWARFExpression.h:50
llvm::DWARFExpression::Operation::Description::Description
Description(DwarfVersion Version=DwarfNA, Encoding Op1=SizeNA, Encoding Op2=SizeNA)
Definition: DWARFExpression.h:70
StringRef.h
llvm::DWARFExpression::Operation::isError
bool isError() const
Definition: DWARFExpression.h:95
llvm::DWARFExpression::Operation::SignBit
@ SignBit
Definition: DWARFExpression.h:48
llvm::DWARFExpression::iterator::operator++
iterator & operator++()
Definition: DWARFExpression.h:124
llvm::DWARFExpression::Operation::verify
static bool verify(const Operation &Op, DWARFUnit *U)
Verify Op. Does not affect the return of isError().
Definition: DWARFExpression.cpp:360
llvm::DWARFExpression::operator==
bool operator==(const DWARFExpression &RHS) const
Definition: DWARFExpression.cpp:513
llvm::DWARFExpression::iterator::operator*
const Operation & operator*() const
Definition: DWARFExpression.h:132
llvm::DWARFExpression::Operation::getOperandEndOffset
uint64_t getOperandEndOffset(unsigned Idx) const
Definition: DWARFExpression.h:91
llvm::DWARFExpression::Operation::Size8
@ Size8
Definition: DWARFExpression.h:41
llvm::DWARFExpression::Operation::getCode
uint8_t getCode() const
Definition: DWARFExpression.h:89
llvm::DWARFExpression::Operation::SignedSize1
@ SignedSize1
Definition: DWARFExpression.h:49
llvm::Optional< dwarf::DwarfFormat >
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
llvm::DWARFExpression::Operation::Encoding
Encoding
Size and signedness of expression operations' operands.
Definition: DWARFExpression.h:37
llvm::DWARFExpression::print
void print(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool IsEH=false) const
Definition: DWARFExpression.cpp:325
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::DWARFExpression::Operation::WasmLocationArg
@ WasmLocationArg
Definition: DWARFExpression.h:47
llvm::DWARFExpression::Operation::DwarfNA
@ DwarfNA
Serves as a marker for unused entries.
Definition: DWARFExpression.h:58
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DWARFExpression::Operation::SizeRefAddr
@ SizeRefAddr
Definition: DWARFExpression.h:44
llvm::DWARFExpression::printCompact
bool printCompact(raw_ostream &OS, const MCRegisterInfo &RegInfo)
Print the expression in a format intended to be compact and useful to a user, but not perfectly unamb...
Definition: DWARFExpression.cpp:509
llvm::DWARFExpression::Operation::Dwarf4
@ Dwarf4
Definition: DWARFExpression.h:61
llvm::DWARFExpression::DWARFExpression
DWARFExpression(DataExtractor Data, uint8_t AddressSize, Optional< dwarf::DwarfFormat > Format=None)
Definition: DWARFExpression.h:142
llvm::DWARFExpression::Operation::BaseTypeRef
@ BaseTypeRef
Definition: DWARFExpression.h:46
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::DWARFExpression::Operation::SizeNA
@ SizeNA
Unused operands get this encoding.
Definition: DWARFExpression.h:54
llvm::DWARFExpression::Operation::getDescription
const Description & getDescription() const
Definition: DWARFExpression.h:88
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition: DWARFExpression.h:34
llvm::DWARFExpression::begin
iterator begin() const
Definition: DWARFExpression.h:148
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::DWARFExpression::iterator::operator==
friend bool operator==(const iterator &, const iterator &)
Definition: DWARFExpression.h:173
llvm::None
const NoneType None
Definition: None.h:24
llvm::DWARFExpression::getData
StringRef getData() const
Definition: DWARFExpression.h:165
llvm::DWARFExpression::Operation::SignedSize4
@ SignedSize4
Definition: DWARFExpression.h:51
Operands
mir Rename Register Operands
Definition: MIRNamerPass.cpp:74
llvm::DWARFExpression::Operation::DwarfVersion
DwarfVersion
Definition: DWARFExpression.h:57
llvm::DWARFExpression::Operation::Dwarf2
@ Dwarf2
Definition: DWARFExpression.h:59
uint64_t
iterator.h
llvm::iterator_facade_base
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
llvm::DWARFExpression::Operation::SignedSizeLEB
@ SignedSizeLEB
Definition: DWARFExpression.h:53
llvm::DWARFExpression::Operation::Description
Description of the encoding of one expression Op.
Definition: DWARFExpression.h:66
llvm::DWARFExpression
Definition: DWARFExpression.h:24
llvm::DWARFExpression::end
iterator end() const
Definition: DWARFExpression.h:149
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DWARFExpression::Operation::Size2
@ Size2
Definition: DWARFExpression.h:39
llvm::DWARFExpression::iterator::skipBytes
iterator skipBytes(uint64_t Add) const
Definition: DWARFExpression.h:134
extract
loop extract
Definition: LoopExtractor.cpp:92
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:2002
RegInfo
Definition: AMDGPUAsmParser.cpp:2504
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Dwarf.h
llvm::MCRegisterInfo
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Definition: MCRegisterInfo.h:135
llvm::DWARFExpression::Operation::Dwarf3
@ Dwarf3
Definition: DWARFExpression.h:60
llvm::StringRef::size
constexpr LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:344
llvm::DWARFUnit
Definition: DWARFUnit.h:207
llvm::DWARFExpression::Operation::Description::Op
Encoding Op[2]
Encoding for Op operands, or SizeNA.
Definition: DWARFExpression.h:68
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::DWARFExpression::Operation::SizeLEB
@ SizeLEB
Definition: DWARFExpression.h:42
DataExtractor.h
llvm::MCID::Add
@ Add
Definition: MCInstrDesc.h:185
llvm::DWARFExpression::Operation::getRawOperand
uint64_t getRawOperand(unsigned Idx) const
Definition: DWARFExpression.h:90
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFExpression::Operation::getEndOffset
uint64_t getEndOffset() const
Definition: DWARFExpression.h:94
llvm::DataExtractor::getData
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:95
llvm::DWARFExpression::verify
bool verify(DWARFUnit *U)
Definition: DWARFExpression.cpp:383
llvm::DWARFExpression::Operation::Size4
@ Size4
Definition: DWARFExpression.h:40
llvm::DWARFExpression::Operation::Size1
@ Size1
Definition: DWARFExpression.h:38
llvm::DWARFExpression::Operation::SizeAddr
@ SizeAddr
Definition: DWARFExpression.h:43
llvm::DWARFExpression::Operation::SignedSize8
@ SignedSize8
Definition: DWARFExpression.h:52
llvm::DWARFExpression::Operation::print
bool print(raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool isEH) const
Definition: DWARFExpression.cpp:263
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:188
llvm::remarks::Format
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25
llvm::DWARFExpression::Operation::SizeBlock
@ SizeBlock
Preceding operand contains block size.
Definition: DWARFExpression.h:45