blob: ea5cbc40ba35370d4ba17bdf2d6d20115905c053 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ------*- C++ -*-===//
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00002//
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
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000017#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"
22#include <cassert>
23#include <cstdint>
24#include <iterator>
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000025
26namespace llvm {
27
Adrian Prantla4c30d62015-01-12 23:36:56 +000028class AsmPrinter;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000029class APInt;
Adrian Prantl66f25952015-01-13 00:04:06 +000030class ByteStreamer;
Adrian Prantl658676c2015-01-14 01:01:22 +000031class DwarfUnit;
32class DIELoc;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000033class TargetRegisterInfo;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000034
Adrian Prantl54286bd2016-11-02 16:12:20 +000035/// Holds a DIExpression and keeps track of how many operands have been consumed
36/// so far.
37class DIExpressionCursor {
38 DIExpression::expr_op_iterator Start, End;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039
Adrian Prantl54286bd2016-11-02 16:12:20 +000040public:
41 DIExpressionCursor(const DIExpression *Expr) {
42 if (!Expr) {
43 assert(Start == End);
44 return;
45 }
46 Start = Expr->expr_op_begin();
47 End = Expr->expr_op_end();
48 }
49
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000050 DIExpressionCursor(ArrayRef<uint64_t> Expr)
51 : Start(Expr.begin()), End(Expr.end()) {}
52
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000053 DIExpressionCursor(const DIExpressionCursor &) = default;
Florian Hahnffc498d2017-06-14 13:14:38 +000054
Adrian Prantl54286bd2016-11-02 16:12:20 +000055 /// Consume one operation.
56 Optional<DIExpression::ExprOperand> take() {
57 if (Start == End)
58 return None;
59 return *(Start++);
60 }
61
62 /// Consume N operations.
63 void consume(unsigned N) { std::advance(Start, N); }
64
65 /// Return the current operation.
66 Optional<DIExpression::ExprOperand> peek() const {
67 if (Start == End)
68 return None;
69 return *(Start);
70 }
71
72 /// Return the next operation.
73 Optional<DIExpression::ExprOperand> peekNext() const {
74 if (Start == End)
75 return None;
76
77 auto Next = Start.getNext();
78 if (Next == End)
79 return None;
80
81 return *Next;
82 }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000083
Adrian Prantlf148d692016-11-02 16:20:37 +000084 /// Determine whether there are any operations left in this expression.
Adrian Prantl54286bd2016-11-02 16:12:20 +000085 operator bool() const { return Start != End; }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000086
Adrian Prantlada10482017-04-20 20:42:33 +000087 DIExpression::expr_op_iterator begin() const { return Start; }
88 DIExpression::expr_op_iterator end() const { return End; }
Adrian Prantl5542da42016-12-22 06:10:41 +000089
90 /// Retrieve the fragment information, if any.
91 Optional<DIExpression::FragmentInfo> getFragmentInfo() const {
92 return DIExpression::getFragmentInfo(Start, End);
93 }
Adrian Prantl54286bd2016-11-02 16:12:20 +000094};
95
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000096/// Base class containing the logic for constructing DWARF expressions
97/// independently of whether they are emitted into a DIE or into a .debug_loc
98/// entry.
99class DwarfExpression {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000100protected:
Adrian Prantl80e188d2017-03-22 01:15:57 +0000101 /// Holds information about all subregisters comprising a register location.
102 struct Register {
103 int DwarfRegNo;
104 unsigned Size;
105 const char *Comment;
106 };
107
108 /// The register location, if any.
109 SmallVector<Register, 2> DwarfRegs;
110
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000111 /// Current Fragment Offset in Bits.
112 uint64_t OffsetInBits = 0;
Adrian Prantl36213092017-03-16 17:42:47 +0000113 unsigned DwarfVersion;
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000114
115 /// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
116 unsigned SubRegisterSizeInBits = 0;
117 unsigned SubRegisterOffsetInBits = 0;
118
Adrian Prantl6825fb62017-04-18 01:21:53 +0000119 /// The kind of location description being produced.
120 enum { Unknown = 0, Register, Memory, Implicit } LocationKind = Unknown;
121
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000122 /// 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;
127 }
Adrian Prantla4c30d62015-01-12 23:36:56 +0000128
Adrian Prantl981f03e2017-03-16 17:14:56 +0000129 /// Add masking operations to stencil out a subregister.
130 void maskSubRegister();
131
Adrian Prantl172ab662015-01-13 23:11:07 +0000132 /// Output a dwarf operand and an optional assembler comment.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000133 virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000134
Adrian Prantl172ab662015-01-13 23:11:07 +0000135 /// Emit a raw signed value.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000136 virtual void emitSigned(int64_t Value) = 0;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000137
Adrian Prantl172ab662015-01-13 23:11:07 +0000138 /// Emit a raw unsigned value.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000139 virtual void emitUnsigned(uint64_t Value) = 0;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000140
Adrian Prantl172ab662015-01-13 23:11:07 +0000141 /// Return whether the given machine register is the frame register in the
142 /// current function.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000143 virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000144
Adrian Prantl6825fb62017-04-18 01:21:53 +0000145 /// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF
146 /// register location description.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000147 void addReg(int DwarfReg, const char *Comment = nullptr);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000148
Adrian Prantla2719882017-03-22 17:19:55 +0000149 /// Emit a DW_OP_breg operation.
150 void addBReg(int DwarfReg, int Offset);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000151
Adrian Prantl80e188d2017-03-22 01:15:57 +0000152 /// Emit DW_OP_fbreg <Offset>.
153 void addFBReg(int Offset);
Adrian Prantl956484b2017-03-20 21:35:09 +0000154
155 /// Emit a partial DWARF register operation.
156 ///
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
161 /// may cover.
162 ///
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.
168 ///
169 /// \return false if no DWARF register exists for MachineReg.
170 bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg,
171 unsigned MaxSize = ~1U);
172
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000173 /// 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.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000176 void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
Adrian Prantl941fa752016-12-05 18:04:47 +0000177
Adrian Prantl981f03e2017-03-16 17:14:56 +0000178 /// Emit a shift-right dwarf operation.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000179 void addShr(unsigned ShiftBy);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000180
Adrian Prantl981f03e2017-03-16 17:14:56 +0000181 /// Emit a bitwise and dwarf operation.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000182 void addAnd(unsigned Mask);
Adrian Prantl941fa752016-12-05 18:04:47 +0000183
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000184 /// Emit a DW_OP_stack_value, if supported.
185 ///
Adrian Prantlf148d692016-11-02 16:20:37 +0000186 /// 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.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000195 void addStackValue();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000196
Adrian Prantl035862b2017-03-27 17:34:04 +0000197 ~DwarfExpression() = default;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000198
Adrian Prantl52884b72017-03-20 21:34:19 +0000199public:
200 DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
Adrian Prantl52884b72017-03-20 21:34:19 +0000201
202 /// This needs to be called last to commit any pending changes.
203 void finalize();
204
Adrian Prantl66f25952015-01-13 00:04:06 +0000205 /// Emit a signed constant.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000206 void addSignedConstant(int64_t Value);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000207
Adrian Prantl66f25952015-01-13 00:04:06 +0000208 /// Emit an unsigned constant.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000209 void addUnsignedConstant(uint64_t Value);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000210
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000211 /// Emit an unsigned constant.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000212 void addUnsignedConstant(const APInt &Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000213
Adrian Prantlc12cee32017-04-19 23:42:25 +0000214 /// Lock this down to become a memory location description.
215 void setMemoryLocationKind() {
216 assert(LocationKind == Unknown);
217 LocationKind = Memory;
218 }
219
Adrian Prantlf148d692016-11-02 16:20:37 +0000220 /// Emit a machine register location. As an optimization this may also consume
221 /// the prefix of a DwarfExpression if a more efficient representation for
222 /// combining the register location and the first operation exists.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000223 ///
Adrian Prantlc12cee32017-04-19 23:42:25 +0000224 /// \param FragmentOffsetInBits If this is one fragment out of a
225 /// fragmented
Adrian Prantl941fa752016-12-05 18:04:47 +0000226 /// location, this is the offset of the
227 /// fragment inside the entire variable.
228 /// \return false if no DWARF register exists
229 /// for MachineReg.
Adrian Prantlc12cee32017-04-19 23:42:25 +0000230 bool addMachineRegExpression(const TargetRegisterInfo &TRI,
231 DIExpressionCursor &Expr, unsigned MachineReg,
Adrian Prantl941fa752016-12-05 18:04:47 +0000232 unsigned FragmentOffsetInBits = 0);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000233
Adrian Prantl54286bd2016-11-02 16:12:20 +0000234 /// Emit all remaining operations in the DIExpressionCursor.
Adrian Prantl941fa752016-12-05 18:04:47 +0000235 ///
236 /// \param FragmentOffsetInBits If this is one fragment out of multiple
237 /// locations, this is the offset of the
238 /// fragment inside the entire variable.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000239 void addExpression(DIExpressionCursor &&Expr,
Adrian Prantl941fa752016-12-05 18:04:47 +0000240 unsigned FragmentOffsetInBits = 0);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000241
242 /// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
243 /// the fragment described by \c Expr.
244 void addFragmentOffset(const DIExpression *Expr);
Adrian Prantl092d9482015-01-13 23:39:11 +0000245};
Adrian Prantl66f25952015-01-13 00:04:06 +0000246
247/// DwarfExpression implementation for .debug_loc entries.
Adrian Prantl035862b2017-03-27 17:34:04 +0000248class DebugLocDwarfExpression final : public DwarfExpression {
Adrian Prantl66f25952015-01-13 00:04:06 +0000249 ByteStreamer &BS;
250
Adrian Prantla63b8e82017-03-16 17:42:45 +0000251 void emitOp(uint8_t Op, const char *Comment = nullptr) override;
252 void emitSigned(int64_t Value) override;
253 void emitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000254 bool isFrameRegister(const TargetRegisterInfo &TRI,
255 unsigned MachineReg) override;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000256
Adrian Prantl52884b72017-03-20 21:34:19 +0000257public:
258 DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
259 : DwarfExpression(DwarfVersion), BS(BS) {}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000260};
Adrian Prantl658676c2015-01-14 01:01:22 +0000261
262/// DwarfExpression implementation for singular DW_AT_location.
Adrian Prantl035862b2017-03-27 17:34:04 +0000263class DIEDwarfExpression final : public DwarfExpression {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000264const AsmPrinter &AP;
Adrian Prantl658676c2015-01-14 01:01:22 +0000265 DwarfUnit &DU;
266 DIELoc &DIE;
267
Adrian Prantla63b8e82017-03-16 17:42:45 +0000268 void emitOp(uint8_t Op, const char *Comment = nullptr) override;
269 void emitSigned(int64_t Value) override;
270 void emitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000271 bool isFrameRegister(const TargetRegisterInfo &TRI,
272 unsigned MachineReg) override;
Adrian Prantl52884b72017-03-20 21:34:19 +0000273public:
274 DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000275
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000276 DIELoc *finalize() {
277 DwarfExpression::finalize();
278 return &DIE;
279 }
Adrian Prantl658676c2015-01-14 01:01:22 +0000280};
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000281
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000282} // end namespace llvm
283
284#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H