LLVM  9.0.0svn
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_DWARFEXPRESSION_H
10 #define LLVM_DEBUGINFO_DWARFEXPRESSION_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/iterator.h"
16 
17 namespace llvm {
18 class DWARFUnit;
19 class MCRegisterInfo;
20 class raw_ostream;
21 
23 public:
24  class iterator;
25 
26  /// This class represents an Operation in the Expression. Each operation can
27  /// have up to 2 oprerands.
28  ///
29  /// An Operation can be in Error state (check with isError()). This
30  /// means that it couldn't be decoded successfully and if it is the
31  /// case, all others fields contain undefined values.
32  class Operation {
33  public:
34  /// Size and signedness of expression operations' operands.
35  enum Encoding : uint8_t {
36  Size1 = 0,
37  Size2 = 1,
38  Size4 = 2,
39  Size8 = 3,
40  SizeLEB = 4,
41  SizeAddr = 5,
43  SizeBlock = 7, ///< Preceding operand contains block size
45  SignBit = 0x80,
51  SizeNA = 0xFF ///< Unused operands get this encoding.
52  };
53 
54  enum DwarfVersion : uint8_t {
55  DwarfNA, ///< Serves as a marker for unused entries
56  Dwarf2 = 2,
60  };
61 
62  /// Description of the encoding of one expression Op.
63  struct Description {
64  DwarfVersion Version; ///< Dwarf version where the Op was introduced.
65  Encoding Op[2]; ///< Encoding for Op operands, or SizeNA.
66 
68  Encoding Op2 = SizeNA)
69  : Version(Version) {
70  Op[0] = Op1;
71  Op[1] = Op2;
72  }
73  };
74 
75  private:
77  uint8_t Opcode; ///< The Op Opcode, DW_OP_<something>.
78  Description Desc;
79  bool Error;
80  uint32_t EndOffset;
81  uint64_t Operands[2];
82 
83  public:
84  Description &getDescription() { return Desc; }
85  uint8_t getCode() { return Opcode; }
86  uint64_t getRawOperand(unsigned Idx) { return Operands[Idx]; }
87  uint32_t getEndOffset() { return EndOffset; }
88  bool extract(DataExtractor Data, uint16_t Version, uint8_t AddressSize,
90  bool isError() { return Error; }
91  bool print(raw_ostream &OS, const DWARFExpression *Expr,
92  const MCRegisterInfo *RegInfo, DWARFUnit *U, bool isEH);
93  bool verify(DWARFUnit *U);
94  };
95 
96  /// An iterator to go through the expression operations.
97  class iterator
98  : public iterator_facade_base<iterator, std::forward_iterator_tag,
99  Operation> {
100  friend class DWARFExpression;
101  const DWARFExpression *Expr;
103  Operation Op;
104  iterator(const DWARFExpression *Expr, uint32_t Offset)
105  : Expr(Expr), Offset(Offset) {
106  Op.Error =
107  Offset >= Expr->Data.getData().size() ||
108  !Op.extract(Expr->Data, Expr->Version, Expr->AddressSize, Offset);
109  }
110 
111  public:
113  Offset = Op.isError() ? Expr->Data.getData().size() : Op.EndOffset;
114  Op.Error =
115  Offset >= Expr->Data.getData().size() ||
116  !Op.extract(Expr->Data, Expr->Version, Expr->AddressSize, Offset);
117  return Op;
118  }
119 
120  class Operation &operator*() {
121  return Op;
122  }
123 
124  // Comparison operators are provided out of line.
125  friend bool operator==(const iterator &, const iterator &);
126  };
127 
128  DWARFExpression(DataExtractor Data, uint16_t Version, uint8_t AddressSize)
129  : Data(Data), Version(Version), AddressSize(AddressSize) {
130  assert(AddressSize == 8 || AddressSize == 4);
131  }
132 
133  iterator begin() const { return iterator(this, 0); }
134  iterator end() const { return iterator(this, Data.getData().size()); }
135 
136  void print(raw_ostream &OS, const MCRegisterInfo *RegInfo, DWARFUnit *U,
137  bool IsEH = false) const;
138 
139  bool verify(DWARFUnit *U);
140 
141 private:
143  uint16_t Version;
144  uint8_t AddressSize;
145 };
146 
147 inline bool operator==(const DWARFExpression::iterator &LHS,
148  const DWARFExpression::iterator &RHS) {
149  return LHS.Expr == RHS.Expr && LHS.Offset == RHS.Offset;
150 }
151 
152 inline bool operator!=(const DWARFExpression::iterator &LHS,
153  const DWARFExpression::iterator &RHS) {
154  return !(LHS == RHS);
155 }
156 }
157 #endif
Unused operands get this encoding.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An iterator to go through the expression operations.
Description(DwarfVersion Version=DwarfNA, Encoding Op1=SizeNA, Encoding Op2=SizeNA)
iterator end() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:54
This class represents an Operation in the Expression.
bool print(raw_ostream &OS, const DWARFExpression *Expr, const MCRegisterInfo *RegInfo, DWARFUnit *U, bool isEH)
iterator begin() const
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:67
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Preceding operand contains block size.
Serves as a marker for unused entries.
Encoding Op[2]
Encoding for Op operands, or SizeNA.
Encoding
Size and signedness of expression operations&#39; operands.
uint64_t getRawOperand(unsigned Idx)
DWARFExpression(DataExtractor Data, uint16_t Version, uint8_t AddressSize)
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1968
DwarfVersion Version
Dwarf version where the Op was introduced.
bool extract(DataExtractor Data, uint16_t Version, uint8_t AddressSize, uint32_t Offset)
Description of the encoding of one expression Op.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1966