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