LLVM  5.0.0
DwarfExpression.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ---*- C++ -*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
16 
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/Support/DataTypes.h"
19 
20 namespace llvm {
21 
22 class AsmPrinter;
23 class ByteStreamer;
24 class TargetRegisterInfo;
25 class DwarfUnit;
26 class DIELoc;
27 
28 /// Holds a DIExpression and keeps track of how many operands have been consumed
29 /// so far.
32 public:
34  if (!Expr) {
35  assert(Start == End);
36  return;
37  }
38  Start = Expr->expr_op_begin();
39  End = Expr->expr_op_end();
40  }
41 
43  : Start(Expr.begin()), End(Expr.end()) {}
44 
46  : Start(C.Start), End(C.End) {}
47 
48  /// Consume one operation.
50  if (Start == End)
51  return None;
52  return *(Start++);
53  }
54 
55  /// Consume N operations.
56  void consume(unsigned N) { std::advance(Start, N); }
57 
58  /// Return the current operation.
60  if (Start == End)
61  return None;
62  return *(Start);
63  }
64 
65  /// Return the next operation.
67  if (Start == End)
68  return None;
69 
70  auto Next = Start.getNext();
71  if (Next == End)
72  return None;
73 
74  return *Next;
75  }
76  /// Determine whether there are any operations left in this expression.
77  operator bool() const { return Start != End; }
78  DIExpression::expr_op_iterator begin() const { return Start; }
79  DIExpression::expr_op_iterator end() const { return End; }
80 
81  /// Retrieve the fragment information, if any.
83  return DIExpression::getFragmentInfo(Start, End);
84  }
85 };
86 
87 /// Base class containing the logic for constructing DWARF expressions
88 /// independently of whether they are emitted into a DIE or into a .debug_loc
89 /// entry.
91 protected:
92  /// Holds information about all subregisters comprising a register location.
93  struct Register {
95  unsigned Size;
96  const char *Comment;
97  };
98 
99  /// The register location, if any.
101 
102  /// Current Fragment Offset in Bits.
103  uint64_t OffsetInBits = 0;
104  unsigned DwarfVersion;
105 
106  /// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
107  unsigned SubRegisterSizeInBits = 0;
109 
110  /// The kind of location description being produced.
112 
113  /// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed
114  /// to represent a subregister.
115  void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {
116  SubRegisterSizeInBits = SizeInBits;
118  }
119 
120  /// Add masking operations to stencil out a subregister.
121  void maskSubRegister();
122 
123  /// Output a dwarf operand and an optional assembler comment.
124  virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0;
125  /// Emit a raw signed value.
126  virtual void emitSigned(int64_t Value) = 0;
127  /// Emit a raw unsigned value.
128  virtual void emitUnsigned(uint64_t Value) = 0;
129  /// Return whether the given machine register is the frame register in the
130  /// current function.
131  virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
132 
133  /// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF
134  /// register location description.
135  void addReg(int DwarfReg, const char *Comment = nullptr);
136  /// Emit a DW_OP_breg operation.
137  void addBReg(int DwarfReg, int Offset);
138  /// Emit DW_OP_fbreg <Offset>.
139  void addFBReg(int Offset);
140 
141  /// Emit a partial DWARF register operation.
142  ///
143  /// \param MachineReg The register number.
144  /// \param MaxSize If the register must be composed from
145  /// sub-registers this is an upper bound
146  /// for how many bits the emitted DW_OP_piece
147  /// may cover.
148  ///
149  /// If size and offset is zero an operation for the entire register is
150  /// emitted: Some targets do not provide a DWARF register number for every
151  /// register. If this is the case, this function will attempt to emit a DWARF
152  /// register by emitting a fragment of a super-register or by piecing together
153  /// multiple subregisters that alias the register.
154  ///
155  /// \return false if no DWARF register exists for MachineReg.
156  bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg,
157  unsigned MaxSize = ~1U);
158 
159 
160  /// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
161  /// \param OffsetInBits This is an optional offset into the location that
162  /// is at the top of the DWARF stack.
163  void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
164 
165  /// Emit a shift-right dwarf operation.
166  void addShr(unsigned ShiftBy);
167  /// Emit a bitwise and dwarf operation.
168  void addAnd(unsigned Mask);
169 
170  /// Emit a DW_OP_stack_value, if supported.
171  ///
172  /// The proper way to describe a constant value is DW_OP_constu <const>,
173  /// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available
174  /// until DWARF 4, so we will continue to generate DW_OP_constu <const> for
175  /// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const
176  /// <const> actually describes a value at a constant addess, not a constant
177  /// value. However, in the past there was no better way to describe a
178  /// constant value, so the producers and consumers started to rely on
179  /// heuristics to disambiguate the value vs. location status of the
180  /// expression. See PR21176 for more details.
181  void addStackValue();
182 
183  ~DwarfExpression() = default;
184 public:
185  DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
186 
187  /// This needs to be called last to commit any pending changes.
188  void finalize();
189 
190  /// Emit a signed constant.
191  void addSignedConstant(int64_t Value);
192  /// Emit an unsigned constant.
193  void addUnsignedConstant(uint64_t Value);
194  /// Emit an unsigned constant.
195  void addUnsignedConstant(const APInt &Value);
196 
197  /// Lock this down to become a memory location description.
201  }
202 
203  /// Emit a machine register location. As an optimization this may also consume
204  /// the prefix of a DwarfExpression if a more efficient representation for
205  /// combining the register location and the first operation exists.
206  ///
207  /// \param FragmentOffsetInBits If this is one fragment out of a
208  /// fragmented
209  /// location, this is the offset of the
210  /// fragment inside the entire variable.
211  /// \return false if no DWARF register exists
212  /// for MachineReg.
214  DIExpressionCursor &Expr, unsigned MachineReg,
215  unsigned FragmentOffsetInBits = 0);
216  /// Emit all remaining operations in the DIExpressionCursor.
217  ///
218  /// \param FragmentOffsetInBits If this is one fragment out of multiple
219  /// locations, this is the offset of the
220  /// fragment inside the entire variable.
221  void addExpression(DIExpressionCursor &&Expr,
222  unsigned FragmentOffsetInBits = 0);
223 
224  /// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
225  /// the fragment described by \c Expr.
226  void addFragmentOffset(const DIExpression *Expr);
227 };
228 
229 /// DwarfExpression implementation for .debug_loc entries.
231  ByteStreamer &BS;
232 
233  void emitOp(uint8_t Op, const char *Comment = nullptr) override;
234  void emitSigned(int64_t Value) override;
235  void emitUnsigned(uint64_t Value) override;
236  bool isFrameRegister(const TargetRegisterInfo &TRI,
237  unsigned MachineReg) override;
238 public:
240  : DwarfExpression(DwarfVersion), BS(BS) {}
241 };
242 
243 /// DwarfExpression implementation for singular DW_AT_location.
244 class DIEDwarfExpression final : public DwarfExpression {
245 const AsmPrinter &AP;
246  DwarfUnit &DU;
247  DIELoc &DIE;
248 
249  void emitOp(uint8_t Op, const char *Comment = nullptr) override;
250  void emitSigned(int64_t Value) override;
251  void emitUnsigned(uint64_t Value) override;
252  bool isFrameRegister(const TargetRegisterInfo &TRI,
253  unsigned MachineReg) override;
254 public:
258  return &DIE;
259  }
260 };
261 }
262 
263 #endif
void addShr(unsigned ShiftBy)
Emit a shift-right dwarf operation.
DIELoc - Represents an expression location.
Definition: DIE.h:846
DIExpression::expr_op_iterator begin() const
void addUnsignedConstant(uint64_t Value)
Emit an unsigned constant.
void setMemoryLocationKind()
Lock this down to become a memory location description.
Optional< DIExpression::ExprOperand > peek() const
Return the current operation.
Base class containing the logic for constructing DWARF expressions independently of whether they are ...
DwarfExpression(unsigned DwarfVersion)
void addBReg(int DwarfReg, int Offset)
Emit a DW_OP_breg operation.
void addFBReg(int Offset)
Emit DW_OP_fbreg <Offset>.
enum llvm::DwarfExpression::@277 LocationKind
The kind of location description being produced.
Holds a DIExpression and keeps track of how many operands have been consumed so far.
DwarfExpression implementation for .debug_loc entries.
void addReg(int DwarfReg, const char *Comment=nullptr)
Emit a DW_OP_reg operation.
void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits)
Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed to represent a subregister...
expr_op_iterator expr_op_begin() const
Visit the elements via ExprOperand wrappers.
void maskSubRegister()
Add masking operations to stencil out a subregister.
virtual void emitUnsigned(uint64_t Value)=0
Emit a raw unsigned value.
Optional< DIExpression::ExprOperand > take()
Consume one operation.
Optional< DIExpression::FragmentInfo > getFragmentInfo() const
Retrieve the fragment information, if any.
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:68
DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE)
Definition: DwarfUnit.cpp:53
DIExpressionCursor(const DIExpression *Expr)
virtual void emitOp(uint8_t Op, const char *Comment=nullptr)=0
Output a dwarf operand and an optional assembler comment.
Optional< DIExpression::ExprOperand > peekNext() const
Return the next operation.
expr_op_iterator getNext() const
Get the next iterator.
A structured debug information entry.
Definition: DIE.h:662
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:76
uint64_t OffsetInBits
Current Fragment Offset in Bits.
virtual void emitSigned(int64_t Value)=0
Emit a raw signed value.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
DIExpressionCursor(const DIExpressionCursor &C)
void addExpression(DIExpressionCursor &&Expr, unsigned FragmentOffsetInBits=0)
Emit all remaining operations in the DIExpressionCursor.
bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg, unsigned MaxSize=~1U)
Emit a partial DWARF register operation.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
~DwarfExpression()=default
void addStackValue()
Emit a DW_OP_stack_value, if supported.
const size_t N
Optional< FragmentInfo > getFragmentInfo() const
Retrieve the details of this fragment expression.
DWARF expression.
Class for arbitrary precision integers.
Definition: APInt.h:69
void addAnd(unsigned Mask)
Emit a bitwise and dwarf operation.
DIExpression::expr_op_iterator end() const
unsigned SubRegisterSizeInBits
Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
void consume(unsigned N)
Consume N operations.
void finalize()
This needs to be called last to commit any pending changes.
#define C
Definition: LargeTest.cpp:25
DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
DIExpressionCursor(ArrayRef< uint64_t > Expr)
DwarfExpression implementation for singular DW_AT_location.
void addFragmentOffset(const DIExpression *Expr)
If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to the fragment described by Ex...
Holds information about all subregisters comprising a register location.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits=0)
Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
An iterator for expression operands.
void addSignedConstant(int64_t Value)
Emit a signed constant.
SmallVector< Register, 2 > DwarfRegs
The register location, if any.
expr_op_iterator expr_op_end() const
bool addMachineRegExpression(const TargetRegisterInfo &TRI, DIExpressionCursor &Expr, unsigned MachineReg, unsigned FragmentOffsetInBits=0)
Emit a machine register location.
virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg)=0
Return whether the given machine register is the frame register in the current function.