blob: 6985debe6138fca55ce41753dced1c3de451f783 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
14#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
15
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000016#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/None.h"
18#include "llvm/ADT/Optional.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/IR/DebugInfoMetadata.h"
21#include <cassert>
22#include <cstdint>
23#include <iterator>
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000024
25namespace llvm {
26
Adrian Prantla4c30d62015-01-12 23:36:56 +000027class AsmPrinter;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000028class APInt;
Adrian Prantl66f25952015-01-13 00:04:06 +000029class ByteStreamer;
Markus Lavinb86ce212019-03-19 13:16:28 +000030class DwarfCompileUnit;
Adrian Prantl658676c2015-01-14 01:01:22 +000031class DIELoc;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000032class TargetRegisterInfo;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000033
Adrian Prantl54286bd2016-11-02 16:12:20 +000034/// Holds a DIExpression and keeps track of how many operands have been consumed
35/// so far.
36class DIExpressionCursor {
37 DIExpression::expr_op_iterator Start, End;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000038
Adrian Prantl54286bd2016-11-02 16:12:20 +000039public:
40 DIExpressionCursor(const DIExpression *Expr) {
41 if (!Expr) {
42 assert(Start == End);
43 return;
44 }
45 Start = Expr->expr_op_begin();
46 End = Expr->expr_op_end();
47 }
48
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000049 DIExpressionCursor(ArrayRef<uint64_t> Expr)
50 : Start(Expr.begin()), End(Expr.end()) {}
51
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000052 DIExpressionCursor(const DIExpressionCursor &) = default;
Florian Hahnffc498d2017-06-14 13:14:38 +000053
Adrian Prantl54286bd2016-11-02 16:12:20 +000054 /// Consume one operation.
55 Optional<DIExpression::ExprOperand> take() {
56 if (Start == End)
57 return None;
58 return *(Start++);
59 }
60
61 /// Consume N operations.
62 void consume(unsigned N) { std::advance(Start, N); }
63
64 /// Return the current operation.
65 Optional<DIExpression::ExprOperand> peek() const {
66 if (Start == End)
67 return None;
68 return *(Start);
69 }
70
71 /// Return the next operation.
72 Optional<DIExpression::ExprOperand> peekNext() const {
73 if (Start == End)
74 return None;
75
76 auto Next = Start.getNext();
77 if (Next == End)
78 return None;
79
80 return *Next;
81 }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000082
Adrian Prantlf148d692016-11-02 16:20:37 +000083 /// Determine whether there are any operations left in this expression.
Adrian Prantl54286bd2016-11-02 16:12:20 +000084 operator bool() const { return Start != End; }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000085
Adrian Prantlada10482017-04-20 20:42:33 +000086 DIExpression::expr_op_iterator begin() const { return Start; }
87 DIExpression::expr_op_iterator end() const { return End; }
Adrian Prantl5542da42016-12-22 06:10:41 +000088
89 /// Retrieve the fragment information, if any.
90 Optional<DIExpression::FragmentInfo> getFragmentInfo() const {
91 return DIExpression::getFragmentInfo(Start, End);
92 }
Adrian Prantl54286bd2016-11-02 16:12:20 +000093};
94
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000095/// Base class containing the logic for constructing DWARF expressions
96/// independently of whether they are emitted into a DIE or into a .debug_loc
97/// entry.
98class DwarfExpression {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000099protected:
Adrian Prantl80e188d2017-03-22 01:15:57 +0000100 /// Holds information about all subregisters comprising a register location.
101 struct Register {
102 int DwarfRegNo;
103 unsigned Size;
104 const char *Comment;
105 };
106
Markus Lavinb86ce212019-03-19 13:16:28 +0000107 DwarfCompileUnit &CU;
108
Adrian Prantl80e188d2017-03-22 01:15:57 +0000109 /// The register location, if any.
110 SmallVector<Register, 2> DwarfRegs;
111
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000112 /// Current Fragment Offset in Bits.
113 uint64_t OffsetInBits = 0;
114
Fangrui Songf78650a2018-07-30 19:41:25 +0000115 /// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
Petar Jovanovicff47d832019-05-23 10:37:13 +0000116 unsigned SubRegisterSizeInBits : 16;
117 unsigned SubRegisterOffsetInBits : 16;
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000118
Adrian Prantl6825fb62017-04-18 01:21:53 +0000119 /// The kind of location description being produced.
Petar Jovanovicff47d832019-05-23 10:37:13 +0000120 enum { Unknown = 0, Register, Memory, Implicit };
Adrian Prantl6825fb62017-04-18 01:21:53 +0000121
Petar Jovanovicff47d832019-05-23 10:37:13 +0000122 unsigned LocationKind : 3;
123 unsigned LocationFlags : 2;
124 unsigned DwarfVersion : 4;
125
126public:
127 bool isUnknownLocation() const {
128 return LocationKind == Unknown;
129 }
130
131 bool isMemoryLocation() const {
132 return LocationKind == Memory;
133 }
134
135 bool isRegisterLocation() const {
136 return LocationKind == Register;
137 }
138
139 bool isImplicitLocation() const {
140 return LocationKind == Implicit;
141 }
142
143protected:
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000144 /// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed
145 /// to represent a subregister.
146 void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {
Petar Jovanovicff47d832019-05-23 10:37:13 +0000147 assert(SizeInBits < 65536 && OffsetInBits < 65536);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000148 SubRegisterSizeInBits = SizeInBits;
149 SubRegisterOffsetInBits = OffsetInBits;
150 }
Adrian Prantla4c30d62015-01-12 23:36:56 +0000151
Adrian Prantl981f03e2017-03-16 17:14:56 +0000152 /// Add masking operations to stencil out a subregister.
153 void maskSubRegister();
154
Adrian Prantl172ab662015-01-13 23:11:07 +0000155 /// Output a dwarf operand and an optional assembler comment.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000156 virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000157
Adrian Prantl172ab662015-01-13 23:11:07 +0000158 /// Emit a raw signed value.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000159 virtual void emitSigned(int64_t Value) = 0;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000160
Adrian Prantl172ab662015-01-13 23:11:07 +0000161 /// Emit a raw unsigned value.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000162 virtual void emitUnsigned(uint64_t Value) = 0;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000163
Markus Lavina475da32019-04-30 07:58:57 +0000164 virtual void emitData1(uint8_t Value) = 0;
165
Markus Lavinb86ce212019-03-19 13:16:28 +0000166 virtual void emitBaseTypeRef(uint64_t Idx) = 0;
167
Jonas Devlieghere965b5982018-09-05 10:18:36 +0000168 /// Emit a normalized unsigned constant.
169 void emitConstu(uint64_t Value);
170
Adrian Prantl172ab662015-01-13 23:11:07 +0000171 /// Return whether the given machine register is the frame register in the
172 /// current function.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000173 virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000174
Adrian Prantl6825fb62017-04-18 01:21:53 +0000175 /// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF
176 /// register location description.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000177 void addReg(int DwarfReg, const char *Comment = nullptr);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000178
Adrian Prantla2719882017-03-22 17:19:55 +0000179 /// Emit a DW_OP_breg operation.
180 void addBReg(int DwarfReg, int Offset);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000181
Adrian Prantl80e188d2017-03-22 01:15:57 +0000182 /// Emit DW_OP_fbreg <Offset>.
183 void addFBReg(int Offset);
Adrian Prantl956484b2017-03-20 21:35:09 +0000184
185 /// Emit a partial DWARF register operation.
186 ///
187 /// \param MachineReg The register number.
188 /// \param MaxSize If the register must be composed from
189 /// sub-registers this is an upper bound
190 /// for how many bits the emitted DW_OP_piece
191 /// may cover.
192 ///
193 /// If size and offset is zero an operation for the entire register is
194 /// emitted: Some targets do not provide a DWARF register number for every
195 /// register. If this is the case, this function will attempt to emit a DWARF
196 /// register by emitting a fragment of a super-register or by piecing together
197 /// multiple subregisters that alias the register.
198 ///
199 /// \return false if no DWARF register exists for MachineReg.
200 bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg,
201 unsigned MaxSize = ~1U);
202
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000203 /// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
204 /// \param OffsetInBits This is an optional offset into the location that
205 /// is at the top of the DWARF stack.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000206 void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
Adrian Prantl941fa752016-12-05 18:04:47 +0000207
Adrian Prantl981f03e2017-03-16 17:14:56 +0000208 /// Emit a shift-right dwarf operation.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000209 void addShr(unsigned ShiftBy);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000210
Adrian Prantl981f03e2017-03-16 17:14:56 +0000211 /// Emit a bitwise and dwarf operation.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000212 void addAnd(unsigned Mask);
Adrian Prantl941fa752016-12-05 18:04:47 +0000213
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000214 /// Emit a DW_OP_stack_value, if supported.
215 ///
Adrian Prantlf148d692016-11-02 16:20:37 +0000216 /// The proper way to describe a constant value is DW_OP_constu <const>,
217 /// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available
218 /// until DWARF 4, so we will continue to generate DW_OP_constu <const> for
219 /// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const
Matt Davis87b22682018-12-20 00:01:57 +0000220 /// <const> actually describes a value at a constant address, not a constant
Adrian Prantlf148d692016-11-02 16:20:37 +0000221 /// value. However, in the past there was no better way to describe a
222 /// constant value, so the producers and consumers started to rely on
223 /// heuristics to disambiguate the value vs. location status of the
224 /// expression. See PR21176 for more details.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000225 void addStackValue();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000226
Adrian Prantl035862b2017-03-27 17:34:04 +0000227 ~DwarfExpression() = default;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000228
Adrian Prantl52884b72017-03-20 21:34:19 +0000229public:
Markus Lavinb86ce212019-03-19 13:16:28 +0000230 DwarfExpression(unsigned DwarfVersion, DwarfCompileUnit &CU)
Petar Jovanovicff47d832019-05-23 10:37:13 +0000231 : CU(CU), SubRegisterSizeInBits(0), SubRegisterOffsetInBits(0),
232 LocationKind(Unknown), LocationFlags(Unknown),
233 DwarfVersion(DwarfVersion) {}
Adrian Prantl52884b72017-03-20 21:34:19 +0000234
235 /// This needs to be called last to commit any pending changes.
236 void finalize();
237
Adrian Prantl66f25952015-01-13 00:04:06 +0000238 /// Emit a signed constant.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000239 void addSignedConstant(int64_t Value);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000240
Adrian Prantl66f25952015-01-13 00:04:06 +0000241 /// Emit an unsigned constant.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000242 void addUnsignedConstant(uint64_t Value);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000243
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000244 /// Emit an unsigned constant.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000245 void addUnsignedConstant(const APInt &Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000246
Adrian Prantlc12cee32017-04-19 23:42:25 +0000247 /// Lock this down to become a memory location description.
248 void setMemoryLocationKind() {
Petar Jovanovicff47d832019-05-23 10:37:13 +0000249 assert(isUnknownLocation());
Adrian Prantlc12cee32017-04-19 23:42:25 +0000250 LocationKind = Memory;
251 }
252
Adrian Prantlf148d692016-11-02 16:20:37 +0000253 /// Emit a machine register location. As an optimization this may also consume
254 /// the prefix of a DwarfExpression if a more efficient representation for
255 /// combining the register location and the first operation exists.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000256 ///
Adrian Prantlc12cee32017-04-19 23:42:25 +0000257 /// \param FragmentOffsetInBits If this is one fragment out of a
258 /// fragmented
Adrian Prantl941fa752016-12-05 18:04:47 +0000259 /// location, this is the offset of the
260 /// fragment inside the entire variable.
261 /// \return false if no DWARF register exists
262 /// for MachineReg.
Adrian Prantlc12cee32017-04-19 23:42:25 +0000263 bool addMachineRegExpression(const TargetRegisterInfo &TRI,
264 DIExpressionCursor &Expr, unsigned MachineReg,
Adrian Prantl941fa752016-12-05 18:04:47 +0000265 unsigned FragmentOffsetInBits = 0);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000266
Adrian Prantl54286bd2016-11-02 16:12:20 +0000267 /// Emit all remaining operations in the DIExpressionCursor.
Adrian Prantl941fa752016-12-05 18:04:47 +0000268 ///
269 /// \param FragmentOffsetInBits If this is one fragment out of multiple
270 /// locations, this is the offset of the
271 /// fragment inside the entire variable.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000272 void addExpression(DIExpressionCursor &&Expr,
Adrian Prantl941fa752016-12-05 18:04:47 +0000273 unsigned FragmentOffsetInBits = 0);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000274
275 /// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
276 /// the fragment described by \c Expr.
277 void addFragmentOffset(const DIExpression *Expr);
Markus Lavinb86ce212019-03-19 13:16:28 +0000278
279 void emitLegacySExt(unsigned FromBits);
280 void emitLegacyZExt(unsigned FromBits);
Adrian Prantl092d9482015-01-13 23:39:11 +0000281};
Adrian Prantl66f25952015-01-13 00:04:06 +0000282
283/// DwarfExpression implementation for .debug_loc entries.
Adrian Prantl035862b2017-03-27 17:34:04 +0000284class DebugLocDwarfExpression final : public DwarfExpression {
Adrian Prantl66f25952015-01-13 00:04:06 +0000285 ByteStreamer &BS;
286
Adrian Prantla63b8e82017-03-16 17:42:45 +0000287 void emitOp(uint8_t Op, const char *Comment = nullptr) override;
288 void emitSigned(int64_t Value) override;
289 void emitUnsigned(uint64_t Value) override;
Markus Lavina475da32019-04-30 07:58:57 +0000290 void emitData1(uint8_t Value) override;
Markus Lavinb86ce212019-03-19 13:16:28 +0000291 void emitBaseTypeRef(uint64_t Idx) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000292 bool isFrameRegister(const TargetRegisterInfo &TRI,
293 unsigned MachineReg) override;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000294
Adrian Prantl52884b72017-03-20 21:34:19 +0000295public:
Markus Lavinb86ce212019-03-19 13:16:28 +0000296 DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS, DwarfCompileUnit &CU)
297 : DwarfExpression(DwarfVersion, CU), BS(BS) {}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000298};
Adrian Prantl658676c2015-01-14 01:01:22 +0000299
300/// DwarfExpression implementation for singular DW_AT_location.
Adrian Prantl035862b2017-03-27 17:34:04 +0000301class DIEDwarfExpression final : public DwarfExpression {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000302const AsmPrinter &AP;
Adrian Prantl658676c2015-01-14 01:01:22 +0000303 DIELoc &DIE;
304
Adrian Prantla63b8e82017-03-16 17:42:45 +0000305 void emitOp(uint8_t Op, const char *Comment = nullptr) override;
306 void emitSigned(int64_t Value) override;
307 void emitUnsigned(uint64_t Value) override;
Markus Lavina475da32019-04-30 07:58:57 +0000308 void emitData1(uint8_t Value) override;
Markus Lavinb86ce212019-03-19 13:16:28 +0000309 void emitBaseTypeRef(uint64_t Idx) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000310 bool isFrameRegister(const TargetRegisterInfo &TRI,
311 unsigned MachineReg) override;
Adrian Prantl52884b72017-03-20 21:34:19 +0000312public:
Markus Lavinb86ce212019-03-19 13:16:28 +0000313 DIEDwarfExpression(const AsmPrinter &AP, DwarfCompileUnit &CU, DIELoc &DIE);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000314
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000315 DIELoc *finalize() {
316 DwarfExpression::finalize();
317 return &DIE;
318 }
Adrian Prantl658676c2015-01-14 01:01:22 +0000319};
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000320
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000321} // end namespace llvm
322
323#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H