blob: 2f5a701499ddace650b43b8de99337a845b656cb [file] [log] [blame]
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00001//===-- 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
Adrian Prantl092d9482015-01-13 23:39:11 +000017#include "llvm/IR/DebugInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/Support/DataTypes.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000019
20namespace llvm {
21
Adrian Prantla4c30d62015-01-12 23:36:56 +000022class AsmPrinter;
Adrian Prantl66f25952015-01-13 00:04:06 +000023class ByteStreamer;
Adrian Prantla4c30d62015-01-12 23:36:56 +000024class TargetRegisterInfo;
Adrian Prantl658676c2015-01-14 01:01:22 +000025class DwarfUnit;
26class DIELoc;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000027
Adrian Prantl54286bd2016-11-02 16:12:20 +000028/// Holds a DIExpression and keeps track of how many operands have been consumed
29/// so far.
30class DIExpressionCursor {
31 DIExpression::expr_op_iterator Start, End;
32public:
33 DIExpressionCursor(const DIExpression *Expr) {
34 if (!Expr) {
35 assert(Start == End);
36 return;
37 }
38 Start = Expr->expr_op_begin();
39 End = Expr->expr_op_end();
40 }
41
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000042 DIExpressionCursor(ArrayRef<uint64_t> Expr)
43 : Start(Expr.begin()), End(Expr.end()) {}
44
Adrian Prantl54286bd2016-11-02 16:12:20 +000045 /// Consume one operation.
46 Optional<DIExpression::ExprOperand> take() {
47 if (Start == End)
48 return None;
49 return *(Start++);
50 }
51
52 /// Consume N operations.
53 void consume(unsigned N) { std::advance(Start, N); }
54
55 /// Return the current operation.
56 Optional<DIExpression::ExprOperand> peek() const {
57 if (Start == End)
58 return None;
59 return *(Start);
60 }
61
62 /// Return the next operation.
63 Optional<DIExpression::ExprOperand> peekNext() const {
64 if (Start == End)
65 return None;
66
67 auto Next = Start.getNext();
68 if (Next == End)
69 return None;
70
71 return *Next;
72 }
Adrian Prantlf148d692016-11-02 16:20:37 +000073 /// Determine whether there are any operations left in this expression.
Adrian Prantl54286bd2016-11-02 16:12:20 +000074 operator bool() const { return Start != End; }
Adrian Prantl5542da42016-12-22 06:10:41 +000075
76 /// Retrieve the fragment information, if any.
77 Optional<DIExpression::FragmentInfo> getFragmentInfo() const {
78 return DIExpression::getFragmentInfo(Start, End);
79 }
Adrian Prantl54286bd2016-11-02 16:12:20 +000080};
81
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000082/// Base class containing the logic for constructing DWARF expressions
83/// independently of whether they are emitted into a DIE or into a .debug_loc
84/// entry.
85class DwarfExpression {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000086protected:
Adrian Prantl92da14b2015-03-02 22:02:33 +000087 unsigned DwarfVersion;
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000088 /// Current Fragment Offset in Bits.
89 uint64_t OffsetInBits = 0;
90
91 /// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
92 unsigned SubRegisterSizeInBits = 0;
93 unsigned SubRegisterOffsetInBits = 0;
94
95 /// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed
96 /// to represent a subregister.
97 void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {
98 SubRegisterSizeInBits = SizeInBits;
99 SubRegisterOffsetInBits = OffsetInBits;
100 }
Adrian Prantla4c30d62015-01-12 23:36:56 +0000101
Adrian Prantl981f03e2017-03-16 17:14:56 +0000102 /// Add masking operations to stencil out a subregister.
103 void maskSubRegister();
104
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000105public:
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000106 DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000107 virtual ~DwarfExpression() {};
108
109 /// This needs to be called last to commit any pending changes.
110 void finalize();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000111
Adrian Prantl172ab662015-01-13 23:11:07 +0000112 /// Output a dwarf operand and an optional assembler comment.
113 virtual void EmitOp(uint8_t Op, const char *Comment = nullptr) = 0;
114 /// Emit a raw signed value.
Adrian Prantl51233682015-03-10 19:23:37 +0000115 virtual void EmitSigned(int64_t Value) = 0;
Adrian Prantl172ab662015-01-13 23:11:07 +0000116 /// Emit a raw unsigned value.
Adrian Prantl51233682015-03-10 19:23:37 +0000117 virtual void EmitUnsigned(uint64_t Value) = 0;
Adrian Prantl172ab662015-01-13 23:11:07 +0000118 /// Return whether the given machine register is the frame register in the
119 /// current function.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000120 virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000121
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000122 /// Emit a dwarf register operation.
Adrian Prantl172ab662015-01-13 23:11:07 +0000123 void AddReg(int DwarfReg, const char *Comment = nullptr);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000124 /// Emit an (double-)indirect dwarf register operation.
Tim Northover5d270632017-01-25 20:58:07 +0000125 void AddRegIndirect(int DwarfReg, int Offset);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000126
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000127 /// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
128 /// \param OffsetInBits This is an optional offset into the location that
129 /// is at the top of the DWARF stack.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000130 void AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
Adrian Prantl941fa752016-12-05 18:04:47 +0000131
Adrian Prantl981f03e2017-03-16 17:14:56 +0000132 /// Emit a shift-right dwarf operation.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000133 void AddShr(unsigned ShiftBy);
Adrian Prantl981f03e2017-03-16 17:14:56 +0000134 /// Emit a bitwise and dwarf operation.
135 void AddAnd(unsigned Mask);
Adrian Prantl941fa752016-12-05 18:04:47 +0000136
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000137 /// Emit a DW_OP_stack_value, if supported.
138 ///
Adrian Prantlf148d692016-11-02 16:20:37 +0000139 /// The proper way to describe a constant value is DW_OP_constu <const>,
140 /// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available
141 /// until DWARF 4, so we will continue to generate DW_OP_constu <const> for
142 /// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const
143 /// <const> actually describes a value at a constant addess, not a constant
144 /// value. However, in the past there was no better way to describe a
145 /// constant value, so the producers and consumers started to rely on
146 /// heuristics to disambiguate the value vs. location status of the
147 /// expression. See PR21176 for more details.
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000148 void AddStackValue();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000149
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000150 /// Emit an indirect dwarf register operation for the given machine register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000151 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000152 bool AddMachineRegIndirect(const TargetRegisterInfo &TRI, unsigned MachineReg,
153 int Offset = 0);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000154
Adrian Prantl54286bd2016-11-02 16:12:20 +0000155 /// Emit a partial DWARF register operation.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000156 ///
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000157 /// \param MachineReg The register number.
Adrian Prantl5542da42016-12-22 06:10:41 +0000158 /// \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.
Adrian Prantl941fa752016-12-05 18:04:47 +0000162 ///
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.
Adrian Prantlad768c32015-01-14 01:01:28 +0000168 ///
169 /// \return false if no DWARF register exists for MachineReg.
Adrian Prantl5542da42016-12-22 06:10:41 +0000170 bool AddMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg,
171 unsigned MaxSize = ~1U);
Adrian Prantl66f25952015-01-13 00:04:06 +0000172
173 /// Emit a signed constant.
Adrian Prantl29ce7012016-06-24 21:35:09 +0000174 void AddSignedConstant(int64_t Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000175 /// Emit an unsigned constant.
Adrian Prantl29ce7012016-06-24 21:35:09 +0000176 void AddUnsignedConstant(uint64_t Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000177 /// Emit an unsigned constant.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000178 void AddUnsignedConstant(const APInt &Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000179
Adrian Prantlf148d692016-11-02 16:20:37 +0000180 /// Emit a machine register location. As an optimization this may also consume
181 /// the prefix of a DwarfExpression if a more efficient representation for
182 /// combining the register location and the first operation exists.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000183 ///
Adrian Prantl941fa752016-12-05 18:04:47 +0000184 /// \param FragmentOffsetInBits If this is one fragment out of a fragmented
185 /// location, this is the offset of the
186 /// fragment inside the entire variable.
187 /// \return false if no DWARF register exists
188 /// for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000189 bool AddMachineRegExpression(const TargetRegisterInfo &TRI,
Adrian Prantl54286bd2016-11-02 16:12:20 +0000190 DIExpressionCursor &Expr, unsigned MachineReg,
Adrian Prantl941fa752016-12-05 18:04:47 +0000191 unsigned FragmentOffsetInBits = 0);
Adrian Prantl54286bd2016-11-02 16:12:20 +0000192 /// Emit all remaining operations in the DIExpressionCursor.
Adrian Prantl941fa752016-12-05 18:04:47 +0000193 ///
194 /// \param FragmentOffsetInBits If this is one fragment out of multiple
195 /// locations, this is the offset of the
196 /// fragment inside the entire variable.
Adrian Prantl54286bd2016-11-02 16:12:20 +0000197 void AddExpression(DIExpressionCursor &&Expr,
Adrian Prantl941fa752016-12-05 18:04:47 +0000198 unsigned FragmentOffsetInBits = 0);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000199
200 /// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
201 /// the fragment described by \c Expr.
202 void addFragmentOffset(const DIExpression *Expr);
Adrian Prantl092d9482015-01-13 23:39:11 +0000203};
Adrian Prantl66f25952015-01-13 00:04:06 +0000204
205/// DwarfExpression implementation for .debug_loc entries.
206class DebugLocDwarfExpression : public DwarfExpression {
207 ByteStreamer &BS;
208
209public:
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000210 DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
211 : DwarfExpression(DwarfVersion), BS(BS) {}
Adrian Prantl66f25952015-01-13 00:04:06 +0000212
Adrian Prantl172ab662015-01-13 23:11:07 +0000213 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
Adrian Prantl51233682015-03-10 19:23:37 +0000214 void EmitSigned(int64_t Value) override;
215 void EmitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000216 bool isFrameRegister(const TargetRegisterInfo &TRI,
217 unsigned MachineReg) override;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000218};
Adrian Prantl658676c2015-01-14 01:01:22 +0000219
220/// DwarfExpression implementation for singular DW_AT_location.
221class DIEDwarfExpression : public DwarfExpression {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000222const AsmPrinter &AP;
Adrian Prantl658676c2015-01-14 01:01:22 +0000223 DwarfUnit &DU;
224 DIELoc &DIE;
225
226public:
Adrian Prantl92da14b2015-03-02 22:02:33 +0000227 DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
Adrian Prantl658676c2015-01-14 01:01:22 +0000228 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
Adrian Prantl51233682015-03-10 19:23:37 +0000229 void EmitSigned(int64_t Value) override;
230 void EmitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000231 bool isFrameRegister(const TargetRegisterInfo &TRI,
232 unsigned MachineReg) override;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000233 DIELoc *finalize() {
234 DwarfExpression::finalize();
235 return &DIE;
236 }
Adrian Prantl658676c2015-01-14 01:01:22 +0000237};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000238}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000239
240#endif