blob: 06fa59bced11d49756a7e7eb230ec6af2d8cbb68 [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
42 /// Consume one operation.
43 Optional<DIExpression::ExprOperand> take() {
44 if (Start == End)
45 return None;
46 return *(Start++);
47 }
48
49 /// Consume N operations.
50 void consume(unsigned N) { std::advance(Start, N); }
51
52 /// Return the current operation.
53 Optional<DIExpression::ExprOperand> peek() const {
54 if (Start == End)
55 return None;
56 return *(Start);
57 }
58
59 /// Return the next operation.
60 Optional<DIExpression::ExprOperand> peekNext() const {
61 if (Start == End)
62 return None;
63
64 auto Next = Start.getNext();
65 if (Next == End)
66 return None;
67
68 return *Next;
69 }
Adrian Prantlf148d692016-11-02 16:20:37 +000070 /// Determine whether there are any operations left in this expression.
Adrian Prantl54286bd2016-11-02 16:12:20 +000071 operator bool() const { return Start != End; }
72};
73
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000074/// Base class containing the logic for constructing DWARF expressions
75/// independently of whether they are emitted into a DIE or into a .debug_loc
76/// entry.
77class DwarfExpression {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000078protected:
Adrian Prantla4c30d62015-01-12 23:36:56 +000079 // Various convenience accessors that extract things out of AsmPrinter.
Adrian Prantl92da14b2015-03-02 22:02:33 +000080 unsigned DwarfVersion;
Adrian Prantla4c30d62015-01-12 23:36:56 +000081
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000082public:
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000083 DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
Adrian Prantl9cffbd82015-01-12 23:36:50 +000084 virtual ~DwarfExpression() {}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000085
Adrian Prantl172ab662015-01-13 23:11:07 +000086 /// Output a dwarf operand and an optional assembler comment.
87 virtual void EmitOp(uint8_t Op, const char *Comment = nullptr) = 0;
88 /// Emit a raw signed value.
Adrian Prantl51233682015-03-10 19:23:37 +000089 virtual void EmitSigned(int64_t Value) = 0;
Adrian Prantl172ab662015-01-13 23:11:07 +000090 /// Emit a raw unsigned value.
Adrian Prantl51233682015-03-10 19:23:37 +000091 virtual void EmitUnsigned(uint64_t Value) = 0;
Adrian Prantl172ab662015-01-13 23:11:07 +000092 /// Return whether the given machine register is the frame register in the
93 /// current function.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000094 virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000095
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000096 /// Emit a dwarf register operation.
Adrian Prantl172ab662015-01-13 23:11:07 +000097 void AddReg(int DwarfReg, const char *Comment = nullptr);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000098 /// Emit an (double-)indirect dwarf register operation.
99 void AddRegIndirect(int DwarfReg, int Offset, bool Deref = false);
100
Adrian Prantlf148d692016-11-02 16:20:37 +0000101 /// Emit DW_OP_piece operation.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000102 void AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
103 /// Emit a shift-right dwarf expression.
104 void AddShr(unsigned ShiftBy);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000105 /// Emit a DW_OP_stack_value, if supported.
106 ///
Adrian Prantlf148d692016-11-02 16:20:37 +0000107 /// The proper way to describe a constant value is DW_OP_constu <const>,
108 /// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available
109 /// until DWARF 4, so we will continue to generate DW_OP_constu <const> for
110 /// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const
111 /// <const> actually describes a value at a constant addess, not a constant
112 /// value. However, in the past there was no better way to describe a
113 /// constant value, so the producers and consumers started to rely on
114 /// heuristics to disambiguate the value vs. location status of the
115 /// expression. See PR21176 for more details.
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000116 void AddStackValue();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000117
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000118 /// Emit an indirect dwarf register operation for the given machine register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000119 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000120 bool AddMachineRegIndirect(const TargetRegisterInfo &TRI, unsigned MachineReg,
121 int Offset = 0);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +0000122
Adrian Prantl54286bd2016-11-02 16:12:20 +0000123 /// Emit a partial DWARF register operation.
Adrian Prantl172ab662015-01-13 23:11:07 +0000124 /// \param MachineReg the register
125 /// \param PieceSizeInBits size and
126 /// \param PieceOffsetInBits offset of the piece in bits, if this is one
127 /// piece of an aggregate value.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000128 ///
129 /// If size and offset is zero an operation for the entire
130 /// register is emitted: Some targets do not provide a DWARF
131 /// register number for every register. If this is the case, this
132 /// function will attempt to emit a DWARF register by emitting a
133 /// piece of a super-register or by piecing together multiple
134 /// subregisters that alias the register.
Adrian Prantlad768c32015-01-14 01:01:28 +0000135 ///
136 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000137 bool AddMachineRegPiece(const TargetRegisterInfo &TRI, unsigned MachineReg,
138 unsigned PieceSizeInBits = 0,
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000139 unsigned PieceOffsetInBits = 0);
Adrian Prantl66f25952015-01-13 00:04:06 +0000140
141 /// Emit a signed constant.
Adrian Prantl29ce7012016-06-24 21:35:09 +0000142 void AddSignedConstant(int64_t Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000143 /// Emit an unsigned constant.
Adrian Prantl29ce7012016-06-24 21:35:09 +0000144 void AddUnsignedConstant(uint64_t Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000145 /// Emit an unsigned constant.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000146 void AddUnsignedConstant(const APInt &Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000147
Adrian Prantlf148d692016-11-02 16:20:37 +0000148 /// Emit a machine register location. As an optimization this may also consume
149 /// the prefix of a DwarfExpression if a more efficient representation for
150 /// combining the register location and the first operation exists.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000151 ///
Adrian Prantl54286bd2016-11-02 16:12:20 +0000152 /// \param PieceOffsetInBits If this is one piece out of a fragmented
Adrian Prantl092d9482015-01-13 23:39:11 +0000153 /// location, this is the offset of the piece inside the entire variable.
Adrian Prantlad768c32015-01-14 01:01:28 +0000154 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000155 bool AddMachineRegExpression(const TargetRegisterInfo &TRI,
Adrian Prantl54286bd2016-11-02 16:12:20 +0000156 DIExpressionCursor &Expr, unsigned MachineReg,
Adrian Prantl092d9482015-01-13 23:39:11 +0000157 unsigned PieceOffsetInBits = 0);
Adrian Prantl54286bd2016-11-02 16:12:20 +0000158 /// Emit all remaining operations in the DIExpressionCursor.
159 /// \param PieceOffsetInBits If this is one piece out of a fragmented
Adrian Prantl092d9482015-01-13 23:39:11 +0000160 /// location, this is the offset of the piece inside the entire variable.
Adrian Prantl54286bd2016-11-02 16:12:20 +0000161 void AddExpression(DIExpressionCursor &&Expr,
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000162 unsigned PieceOffsetInBits = 0);
Adrian Prantl092d9482015-01-13 23:39:11 +0000163};
Adrian Prantl66f25952015-01-13 00:04:06 +0000164
165/// DwarfExpression implementation for .debug_loc entries.
166class DebugLocDwarfExpression : public DwarfExpression {
167 ByteStreamer &BS;
168
169public:
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000170 DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
171 : DwarfExpression(DwarfVersion), BS(BS) {}
Adrian Prantl66f25952015-01-13 00:04:06 +0000172
Adrian Prantl172ab662015-01-13 23:11:07 +0000173 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
Adrian Prantl51233682015-03-10 19:23:37 +0000174 void EmitSigned(int64_t Value) override;
175 void EmitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000176 bool isFrameRegister(const TargetRegisterInfo &TRI,
177 unsigned MachineReg) override;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000178};
Adrian Prantl658676c2015-01-14 01:01:22 +0000179
180/// DwarfExpression implementation for singular DW_AT_location.
181class DIEDwarfExpression : public DwarfExpression {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000182const AsmPrinter &AP;
Adrian Prantl658676c2015-01-14 01:01:22 +0000183 DwarfUnit &DU;
184 DIELoc &DIE;
185
186public:
Adrian Prantl92da14b2015-03-02 22:02:33 +0000187 DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
Adrian Prantl658676c2015-01-14 01:01:22 +0000188 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
Adrian Prantl51233682015-03-10 19:23:37 +0000189 void EmitSigned(int64_t Value) override;
190 void EmitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000191 bool isFrameRegister(const TargetRegisterInfo &TRI,
192 unsigned MachineReg) override;
Adrian Prantl658676c2015-01-14 01:01:22 +0000193};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000194}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000195
196#endif