LLVM  14.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/ArrayRef.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/iterator.h"
19 
20 namespace llvm {
21 class DWARFUnit;
22 class MCRegisterInfo;
23 class raw_ostream;
24 
26 public:
27  class iterator;
28 
29  /// This class represents an Operation in the Expression. Each operation can
30  /// have up to 2 oprerands.
31  ///
32  /// An Operation can be in Error state (check with isError()). This
33  /// means that it couldn't be decoded successfully and if it is the
34  /// case, all others fields contain undefined values.
35  class Operation {
36  public:
37  /// Size and signedness of expression operations' operands.
38  enum Encoding : uint8_t {
39  Size1 = 0,
40  Size2 = 1,
41  Size4 = 2,
42  Size8 = 3,
43  SizeLEB = 4,
44  SizeAddr = 5,
46  SizeBlock = 7, ///< Preceding operand contains block size
49  SignBit = 0x80,
55  SizeNA = 0xFF ///< Unused operands get this encoding.
56  };
57 
58  enum DwarfVersion : uint8_t {
59  DwarfNA, ///< Serves as a marker for unused entries
60  Dwarf2 = 2,
64  };
65 
66  /// Description of the encoding of one expression Op.
67  struct Description {
68  DwarfVersion Version; ///< Dwarf version where the Op was introduced.
69  Encoding Op[2]; ///< Encoding for Op operands, or SizeNA.
70 
72  Encoding Op2 = SizeNA)
73  : Version(Version) {
74  Op[0] = Op1;
75  Op[1] = Op2;
76  }
77  };
78 
79  private:
81  uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
82  Description Desc;
83  bool Error = false;
84  uint64_t EndOffset;
85  uint64_t Operands[2];
86  uint64_t OperandEndOffsets[2];
87 
88  public:
89  Description &getDescription() { return Desc; }
90  uint8_t getCode() { return Opcode; }
91  uint64_t getRawOperand(unsigned Idx) { return Operands[Idx]; }
92  uint64_t getOperandEndOffset(unsigned Idx) { return OperandEndOffsets[Idx]; }
93  uint64_t getEndOffset() { return EndOffset; }
94  bool extract(DataExtractor Data, uint8_t AddressSize, uint64_t Offset,
96  bool isError() { return Error; }
97  bool print(raw_ostream &OS, DIDumpOptions DumpOpts,
98  const DWARFExpression *Expr, const MCRegisterInfo *RegInfo,
99  DWARFUnit *U, bool isEH);
100  bool verify(DWARFUnit *U);
101  };
102 
103  /// An iterator to go through the expression operations.
104  class iterator
105  : public iterator_facade_base<iterator, std::forward_iterator_tag,
106  Operation> {
107  friend class DWARFExpression;
108  const DWARFExpression *Expr;
110  Operation Op;
112  : Expr(Expr), Offset(Offset) {
113  Op.Error =
114  Offset >= Expr->Data.getData().size() ||
115  !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
116  }
117 
118  public:
120  Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
121  Op.Error =
122  Offset >= Expr->Data.getData().size() ||
123  !Op.extract(Expr->Data, Expr->AddressSize, Offset, Expr->Format);
124  return Op;
125  }
126 
127  class Operation &operator*() {
128  return Op;
129  }
130 
132  return iterator(Expr, Op.EndOffset + Add);
133  }
134 
135  // Comparison operators are provided out of line.
136  friend bool operator==(const iterator &, const iterator &);
137  };
138 
139  DWARFExpression(DataExtractor Data, uint8_t AddressSize,
141  : Data(Data), AddressSize(AddressSize), Format(Format) {
142  assert(AddressSize == 8 || AddressSize == 4 || AddressSize == 2);
143  }
144 
145  iterator begin() const { return iterator(this, 0); }
146  iterator end() const { return iterator(this, Data.getData().size()); }
147 
148  void print(raw_ostream &OS, DIDumpOptions DumpOpts,
149  const MCRegisterInfo *RegInfo, 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.
157 
158  bool verify(DWARFUnit *U);
159 
160  bool operator==(const DWARFExpression &RHS) const;
161 
162  StringRef getData() const { return Data.getData(); }
163 
164 private:
166  uint8_t AddressSize;
168 };
169 
170 inline bool operator==(const DWARFExpression::iterator &LHS,
171  const DWARFExpression::iterator &RHS) {
172  return LHS.Expr == RHS.Expr && LHS.Offset == RHS.Offset;
173 }
174 }
175 #endif
llvm::DWARFExpression::iterator
An iterator to go through the expression operations.
Definition: DWARFExpression.h:104
llvm::DWARFExpression::Operation::Dwarf5
@ Dwarf5
Definition: DWARFExpression.h:63
llvm::DWARFExpression::Operation::getDescription
Description & getDescription()
Definition: DWARFExpression.h:89
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::DWARFExpression::Operation::Description::Version
DwarfVersion Version
Dwarf version where the Op was introduced.
Definition: DWARFExpression.h:68
llvm::lltok::Error
@ Error
Definition: LLToken.h:21
Optional.h
llvm::DWARFExpression::Operation::SignedSize2
@ SignedSize2
Definition: DWARFExpression.h:51
llvm::DWARFExpression::Operation::Description::Description
Description(DwarfVersion Version=DwarfNA, Encoding Op1=SizeNA, Encoding Op2=SizeNA)
Definition: DWARFExpression.h:71
llvm::DWARFExpression::Operation::print
bool print(raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool isEH)
Definition: DWARFExpression.cpp:262
llvm::DWARFExpression::Operation::SignBit
@ SignBit
Definition: DWARFExpression.h:49
llvm::DWARFExpression::operator==
bool operator==(const DWARFExpression &RHS) const
Definition: DWARFExpression.cpp:515
llvm::DWARFExpression::Operation::Size8
@ Size8
Definition: DWARFExpression.h:42
llvm::DWARFExpression::Operation::getRawOperand
uint64_t getRawOperand(unsigned Idx)
Definition: DWARFExpression.h:91
llvm::DWARFExpression::Operation::SignedSize1
@ SignedSize1
Definition: DWARFExpression.h:50
llvm::Optional< dwarf::DwarfFormat >
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::DWARFExpression::Operation::Encoding
Encoding
Size and signedness of expression operations' operands.
Definition: DWARFExpression.h:38
llvm::DWARFExpression::print
void print(raw_ostream &OS, DIDumpOptions DumpOpts, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool IsEH=false) const
Definition: DWARFExpression.cpp:324
llvm::DWARFExpression::Operation::getEndOffset
uint64_t getEndOffset()
Definition: DWARFExpression.h:93
llvm::DWARFExpression::Operation::WasmLocationArg
@ WasmLocationArg
Definition: DWARFExpression.h:48
llvm::DWARFExpression::Operation::DwarfNA
@ DwarfNA
Serves as a marker for unused entries.
Definition: DWARFExpression.h:59
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::DWARFExpression::Operation::SizeRefAddr
@ SizeRefAddr
Definition: DWARFExpression.h:45
llvm::DWARFExpression::iterator::operator++
class Operation & operator++()
Definition: DWARFExpression.h:119
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:511
llvm::DWARFExpression::Operation::Dwarf4
@ Dwarf4
Definition: DWARFExpression.h:62
llvm::DWARFExpression::DWARFExpression
DWARFExpression(DataExtractor Data, uint8_t AddressSize, Optional< dwarf::DwarfFormat > Format=None)
Definition: DWARFExpression.h:139
llvm::DWARFExpression::Operation::BaseTypeRef
@ BaseTypeRef
Definition: DWARFExpression.h:47
llvm::DWARFExpression::Operation::SizeNA
@ SizeNA
Unused operands get this encoding.
Definition: DWARFExpression.h:55
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition: DWARFExpression.h:35
llvm::DWARFExpression::Operation::getCode
uint8_t getCode()
Definition: DWARFExpression.h:90
llvm::DWARFExpression::begin
iterator begin() const
Definition: DWARFExpression.h:145
llvm::DWARFExpression::Operation::extract
bool extract(DataExtractor Data, uint8_t AddressSize, uint64_t Offset, Optional< dwarf::DwarfFormat > Format)
Definition: DWARFExpression.cpp:120
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::DWARFExpression::iterator::operator==
friend bool operator==(const iterator &, const iterator &)
Definition: DWARFExpression.h:170
llvm::None
const NoneType None
Definition: None.h:23
llvm::DWARFExpression::getData
StringRef getData() const
Definition: DWARFExpression.h:162
llvm::DWARFExpression::Operation::SignedSize4
@ SignedSize4
Definition: DWARFExpression.h:52
Operands
mir Rename Register Operands
Definition: MIRNamerPass.cpp:78
llvm::DWARFExpression::Operation::DwarfVersion
DwarfVersion
Definition: DWARFExpression.h:58
llvm::DWARFExpression::Operation::Dwarf2
@ Dwarf2
Definition: DWARFExpression.h:60
uint64_t
llvm::DWARFExpression::iterator::operator*
class Operation & operator*()
Definition: DWARFExpression.h:127
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:66
llvm::DWARFExpression::Operation::SignedSizeLEB
@ SignedSizeLEB
Definition: DWARFExpression.h:54
llvm::DWARFExpression::Operation::Description
Description of the encoding of one expression Op.
Definition: DWARFExpression.h:67
ArrayRef.h
llvm::DWARFExpression
Definition: DWARFExpression.h:25
llvm::DWARFExpression::end
iterator end() const
Definition: DWARFExpression.h:146
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DWARFExpression::Operation::Size2
@ Size2
Definition: DWARFExpression.h:40
llvm::operator==
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1981
iterator_range.h
RegInfo
Definition: AMDGPUAsmParser.cpp:2384
llvm::DWARFExpression::iterator::skipBytes
iterator skipBytes(uint64_t Add)
Definition: DWARFExpression.h:131
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:61
DIContext.h
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:324
llvm::DWARFUnit
Definition: DWARFUnit.h:203
llvm::DWARFExpression::Operation::Description::Op
Encoding Op[2]
Encoding for Op operands, or SizeNA.
Definition: DWARFExpression.h:69
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::DWARFExpression::Operation::SizeLEB
@ SizeLEB
Definition: DWARFExpression.h:43
DataExtractor.h
llvm::DWARFExpression::Operation::verify
bool verify(DWARFUnit *U)
Definition: DWARFExpression.cpp:359
llvm::MCID::Add
@ Add
Definition: MCInstrDesc.h:183
llvm::DataExtractor
Definition: DataExtractor.h:41
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:385
llvm::DWARFExpression::Operation::getOperandEndOffset
uint64_t getOperandEndOffset(unsigned Idx)
Definition: DWARFExpression.h:92
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::DWARFExpression::Operation::Size4
@ Size4
Definition: DWARFExpression.h:41
llvm::DWARFExpression::Operation::Size1
@ Size1
Definition: DWARFExpression.h:39
llvm::DWARFExpression::Operation::SizeAddr
@ SizeAddr
Definition: DWARFExpression.h:44
llvm::DWARFExpression::Operation::SignedSize8
@ SignedSize8
Definition: DWARFExpression.h:53
llvm::DWARFExpression::Operation::isError
bool isError()
Definition: DWARFExpression.h:96
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:180
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:46