1 //===- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/DebugInfoMetadata.h"
33 class TargetRegisterInfo;
35 /// Holds a DIExpression and keeps track of how many operands have been consumed
37 class DIExpressionCursor {
38 DIExpression::expr_op_iterator Start, End;
41 DIExpressionCursor(const DIExpression *Expr) {
46 Start = Expr->expr_op_begin();
47 End = Expr->expr_op_end();
50 DIExpressionCursor(ArrayRef<uint64_t> Expr)
51 : Start(Expr.begin()), End(Expr.end()) {}
53 DIExpressionCursor(const DIExpressionCursor &) = default;
55 /// Consume one operation.
56 Optional<DIExpression::ExprOperand> take() {
62 /// Consume N operations.
63 void consume(unsigned N) { std::advance(Start, N); }
65 /// Return the current operation.
66 Optional<DIExpression::ExprOperand> peek() const {
72 /// Return the next operation.
73 Optional<DIExpression::ExprOperand> peekNext() const {
77 auto Next = Start.getNext();
84 /// Determine whether there are any operations left in this expression.
85 operator bool() const { return Start != End; }
87 DIExpression::expr_op_iterator begin() const { return Start; }
88 DIExpression::expr_op_iterator end() const { return End; }
90 /// Retrieve the fragment information, if any.
91 Optional<DIExpression::FragmentInfo> getFragmentInfo() const {
92 return DIExpression::getFragmentInfo(Start, End);
96 /// Base class containing the logic for constructing DWARF expressions
97 /// independently of whether they are emitted into a DIE or into a .debug_loc
99 class DwarfExpression {
101 /// Holds information about all subregisters comprising a register location.
108 /// The register location, if any.
109 SmallVector<Register, 2> DwarfRegs;
111 /// Current Fragment Offset in Bits.
112 uint64_t OffsetInBits = 0;
113 unsigned DwarfVersion;
115 /// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
116 unsigned SubRegisterSizeInBits = 0;
117 unsigned SubRegisterOffsetInBits = 0;
119 /// The kind of location description being produced.
120 enum { Unknown = 0, Register, Memory, Implicit } LocationKind = Unknown;
122 /// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed
123 /// to represent a subregister.
124 void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {
125 SubRegisterSizeInBits = SizeInBits;
126 SubRegisterOffsetInBits = OffsetInBits;
129 /// Add masking operations to stencil out a subregister.
130 void maskSubRegister();
132 /// Output a dwarf operand and an optional assembler comment.
133 virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0;
135 /// Emit a raw signed value.
136 virtual void emitSigned(int64_t Value) = 0;
138 /// Emit a raw unsigned value.
139 virtual void emitUnsigned(uint64_t Value) = 0;
141 /// Return whether the given machine register is the frame register in the
142 /// current function.
143 virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
145 /// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF
146 /// register location description.
147 void addReg(int DwarfReg, const char *Comment = nullptr);
149 /// Emit a DW_OP_breg operation.
150 void addBReg(int DwarfReg, int Offset);
152 /// Emit DW_OP_fbreg <Offset>.
153 void addFBReg(int Offset);
155 /// Emit a partial DWARF register operation.
157 /// \param MachineReg The register number.
158 /// \param MaxSize If the register must be composed from
159 /// sub-registers this is an upper bound
160 /// for how many bits the emitted DW_OP_piece
163 /// If size and offset is zero an operation for the entire register is
164 /// emitted: Some targets do not provide a DWARF register number for every
165 /// register. If this is the case, this function will attempt to emit a DWARF
166 /// register by emitting a fragment of a super-register or by piecing together
167 /// multiple subregisters that alias the register.
169 /// \return false if no DWARF register exists for MachineReg.
170 bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg,
171 unsigned MaxSize = ~1U);
173 /// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
174 /// \param OffsetInBits This is an optional offset into the location that
175 /// is at the top of the DWARF stack.
176 void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
178 /// Emit a shift-right dwarf operation.
179 void addShr(unsigned ShiftBy);
181 /// Emit a bitwise and dwarf operation.
182 void addAnd(unsigned Mask);
184 /// Emit a DW_OP_stack_value, if supported.
186 /// The proper way to describe a constant value is DW_OP_constu <const>,
187 /// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available
188 /// until DWARF 4, so we will continue to generate DW_OP_constu <const> for
189 /// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const
190 /// <const> actually describes a value at a constant addess, not a constant
191 /// value. However, in the past there was no better way to describe a
192 /// constant value, so the producers and consumers started to rely on
193 /// heuristics to disambiguate the value vs. location status of the
194 /// expression. See PR21176 for more details.
195 void addStackValue();
197 ~DwarfExpression() = default;
200 DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
202 /// This needs to be called last to commit any pending changes.
205 /// Emit a signed constant.
206 void addSignedConstant(int64_t Value);
208 /// Emit an unsigned constant.
209 void addUnsignedConstant(uint64_t Value);
211 /// Emit an unsigned constant.
212 void addUnsignedConstant(const APInt &Value);
214 bool isMemoryLocation() const { return LocationKind == Memory; }
215 bool isUnknownLocation() const { return LocationKind == Unknown; }
217 /// Lock this down to become a memory location description.
218 void setMemoryLocationKind() {
219 assert(LocationKind == Unknown);
220 LocationKind = Memory;
223 /// Emit a machine register location. As an optimization this may also consume
224 /// the prefix of a DwarfExpression if a more efficient representation for
225 /// combining the register location and the first operation exists.
227 /// \param FragmentOffsetInBits If this is one fragment out of a
229 /// location, this is the offset of the
230 /// fragment inside the entire variable.
231 /// \return false if no DWARF register exists
233 bool addMachineRegExpression(const TargetRegisterInfo &TRI,
234 DIExpressionCursor &Expr, unsigned MachineReg,
235 unsigned FragmentOffsetInBits = 0);
237 /// Emit all remaining operations in the DIExpressionCursor.
239 /// \param FragmentOffsetInBits If this is one fragment out of multiple
240 /// locations, this is the offset of the
241 /// fragment inside the entire variable.
242 void addExpression(DIExpressionCursor &&Expr,
243 unsigned FragmentOffsetInBits = 0);
245 /// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
246 /// the fragment described by \c Expr.
247 void addFragmentOffset(const DIExpression *Expr);
250 /// DwarfExpression implementation for .debug_loc entries.
251 class DebugLocDwarfExpression final : public DwarfExpression {
254 void emitOp(uint8_t Op, const char *Comment = nullptr) override;
255 void emitSigned(int64_t Value) override;
256 void emitUnsigned(uint64_t Value) override;
257 bool isFrameRegister(const TargetRegisterInfo &TRI,
258 unsigned MachineReg) override;
261 DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
262 : DwarfExpression(DwarfVersion), BS(BS) {}
265 /// DwarfExpression implementation for singular DW_AT_location.
266 class DIEDwarfExpression final : public DwarfExpression {
267 const AsmPrinter &AP;
271 void emitOp(uint8_t Op, const char *Comment = nullptr) override;
272 void emitSigned(int64_t Value) override;
273 void emitUnsigned(uint64_t Value) override;
274 bool isFrameRegister(const TargetRegisterInfo &TRI,
275 unsigned MachineReg) override;
277 DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
280 DwarfExpression::finalize();
285 } // end namespace llvm
287 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H