blob: 948dc92e25e51ae81c6aa6865510834ba9670447 [file] [log] [blame]
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00001//===-- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework ----------===//
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 debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfExpression.h"
Adrian Prantla4c30d62015-01-12 23:36:56 +000015
16#include "DwarfDebug.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000017#include "llvm/ADT/SmallBitVector.h"
Adrian Prantla4c30d62015-01-12 23:36:56 +000018#include "llvm/CodeGen/AsmPrinter.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000019#include "llvm/Support/Dwarf.h"
20#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/TargetRegisterInfo.h"
22#include "llvm/Target/TargetSubtargetInfo.h"
23
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000024using namespace llvm;
25
Adrian Prantla4c30d62015-01-12 23:36:56 +000026const TargetRegisterInfo *DwarfExpression::getTRI() const {
27 return AP.TM.getSubtargetImpl()->getRegisterInfo();
28}
29
Adrian Prantl66f25952015-01-13 00:04:06 +000030unsigned DwarfExpression::getDwarfVersion() const {
31 return AP.getDwarfDebug()->getDwarfVersion();
32}
33
34void DwarfExpression::AddReg(int DwarfReg, const char *Comment) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000035 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
36 if (DwarfReg < 32) {
37 EmitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
38 } else {
39 EmitOp(dwarf::DW_OP_regx, Comment);
40 EmitUnsigned(DwarfReg);
41 }
42}
43
44void DwarfExpression::AddRegIndirect(int DwarfReg, int Offset, bool Deref) {
45 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
46 if (DwarfReg < 32) {
47 EmitOp(dwarf::DW_OP_breg0 + DwarfReg);
48 } else {
49 EmitOp(dwarf::DW_OP_bregx);
50 EmitUnsigned(DwarfReg);
51 }
52 EmitSigned(Offset);
53 if (Deref)
54 EmitOp(dwarf::DW_OP_deref);
55}
56
Adrian Prantl66f25952015-01-13 00:04:06 +000057void DwarfExpression::AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000058 assert(SizeInBits > 0 && "piece has size zero");
59 const unsigned SizeOfByte = 8;
60 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
61 EmitOp(dwarf::DW_OP_bit_piece);
62 EmitUnsigned(SizeInBits);
63 EmitUnsigned(OffsetInBits);
64 } else {
65 EmitOp(dwarf::DW_OP_piece);
66 unsigned ByteSize = SizeInBits / SizeOfByte;
67 EmitUnsigned(ByteSize);
68 }
69}
70
71void DwarfExpression::AddShr(unsigned ShiftBy) {
72 EmitOp(dwarf::DW_OP_constu);
73 EmitUnsigned(ShiftBy);
74 EmitOp(dwarf::DW_OP_shr);
75}
76
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000077bool DwarfExpression::AddMachineRegIndirect(unsigned MachineReg, int Offset) {
Adrian Prantla4c30d62015-01-12 23:36:56 +000078 int DwarfReg = getTRI()->getDwarfRegNum(MachineReg, false);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000079 if (DwarfReg < 0)
80 return false;
81
Adrian Prantl8995f5c2015-01-13 23:10:43 +000082 if (isFrameRegister(MachineReg)) {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000083 // If variable offset is based in frame register then use fbreg.
84 EmitOp(dwarf::DW_OP_fbreg);
85 EmitSigned(Offset);
86 } else {
87 AddRegIndirect(DwarfReg, Offset);
88 }
89 return true;
90}
91
Adrian Prantlad768c32015-01-14 01:01:28 +000092bool DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000093 unsigned PieceSizeInBits,
94 unsigned PieceOffsetInBits) {
Adrian Prantla4c30d62015-01-12 23:36:56 +000095 const TargetRegisterInfo *TRI = getTRI();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000096 int Reg = TRI->getDwarfRegNum(MachineReg, false);
97
98 // If this is a valid register number, emit it.
99 if (Reg >= 0) {
100 AddReg(Reg);
Adrian Prantl0e6ffb92015-01-12 22:37:16 +0000101 if (PieceSizeInBits)
102 AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +0000103 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000104 }
105
106 // Walk up the super-register chain until we find a valid number.
107 // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
108 for (MCSuperRegIterator SR(MachineReg, TRI); SR.isValid(); ++SR) {
109 Reg = TRI->getDwarfRegNum(*SR, false);
110 if (Reg >= 0) {
111 unsigned Idx = TRI->getSubRegIndex(*SR, MachineReg);
112 unsigned Size = TRI->getSubRegIdxSize(Idx);
113 unsigned RegOffset = TRI->getSubRegIdxOffset(Idx);
114 AddReg(Reg, "super-register");
115 if (PieceOffsetInBits == RegOffset) {
116 AddOpPiece(Size, RegOffset);
117 } else {
118 // If this is part of a variable in a sub-register at a
119 // non-zero offset, we need to manually shift the value into
120 // place, since the DW_OP_piece describes the part of the
121 // variable, not the position of the subregister.
122 if (RegOffset)
123 AddShr(RegOffset);
124 AddOpPiece(Size, PieceOffsetInBits);
125 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000126 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000127 }
128 }
129
130 // Otherwise, attempt to find a covering set of sub-register numbers.
131 // For example, Q0 on ARM is a composition of D0+D1.
132 //
133 // Keep track of the current position so we can emit the more
134 // efficient DW_OP_piece.
135 unsigned CurPos = PieceOffsetInBits;
136 // The size of the register in bits, assuming 8 bits per byte.
137 unsigned RegSize = TRI->getMinimalPhysRegClass(MachineReg)->getSize() * 8;
138 // Keep track of the bits in the register we already emitted, so we
139 // can avoid emitting redundant aliasing subregs.
140 SmallBitVector Coverage(RegSize, false);
141 for (MCSubRegIterator SR(MachineReg, TRI); SR.isValid(); ++SR) {
142 unsigned Idx = TRI->getSubRegIndex(MachineReg, *SR);
143 unsigned Size = TRI->getSubRegIdxSize(Idx);
144 unsigned Offset = TRI->getSubRegIdxOffset(Idx);
145 Reg = TRI->getDwarfRegNum(*SR, false);
146
147 // Intersection between the bits we already emitted and the bits
148 // covered by this subregister.
149 SmallBitVector Intersection(RegSize, false);
150 Intersection.set(Offset, Offset + Size);
151 Intersection ^= Coverage;
152
153 // If this sub-register has a DWARF number and we haven't covered
154 // its range, emit a DWARF piece for it.
155 if (Reg >= 0 && Intersection.any()) {
156 AddReg(Reg, "sub-register");
157 AddOpPiece(Size, Offset == CurPos ? 0 : Offset);
158 CurPos = Offset + Size;
159
160 // Mark it as emitted.
161 Coverage.set(Offset, Offset + Size);
162 }
163 }
164
Adrian Prantlad768c32015-01-14 01:01:28 +0000165 return CurPos > PieceOffsetInBits;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000166}
Adrian Prantl66f25952015-01-13 00:04:06 +0000167
168void DwarfExpression::AddSignedConstant(int Value) {
169 EmitOp(dwarf::DW_OP_consts);
170 EmitSigned(Value);
171 // The proper way to describe a constant value is
172 // DW_OP_constu <const>, DW_OP_stack_value.
173 // Unfortunately, DW_OP_stack_value was not available until DWARF-4,
174 // so we will continue to generate DW_OP_constu <const> for DWARF-2
175 // and DWARF-3. Technically, this is incorrect since DW_OP_const <const>
176 // actually describes a value at a constant addess, not a constant value.
177 // However, in the past there was no better way to describe a constant
178 // value, so the producers and consumers started to rely on heuristics
179 // to disambiguate the value vs. location status of the expression.
180 // See PR21176 for more details.
181 if (getDwarfVersion() >= 4)
182 EmitOp(dwarf::DW_OP_stack_value);
183}
184
185void DwarfExpression::AddUnsignedConstant(unsigned Value) {
186 EmitOp(dwarf::DW_OP_constu);
187 EmitUnsigned(Value);
188 // cf. comment in DwarfExpression::AddSignedConstant().
189 if (getDwarfVersion() >= 4)
190 EmitOp(dwarf::DW_OP_stack_value);
191}
Adrian Prantl092d9482015-01-13 23:39:11 +0000192
193static unsigned getOffsetOrZero(unsigned OffsetInBits,
194 unsigned PieceOffsetInBits) {
195 if (OffsetInBits == PieceOffsetInBits)
196 return 0;
197 assert(OffsetInBits >= PieceOffsetInBits && "overlapping pieces");
198 return OffsetInBits;
199}
200
Adrian Prantlad768c32015-01-14 01:01:28 +0000201bool DwarfExpression::AddMachineRegExpression(DIExpression Expr,
Adrian Prantl092d9482015-01-13 23:39:11 +0000202 unsigned MachineReg,
203 unsigned PieceOffsetInBits) {
204 unsigned N = Expr.getNumElements();
205 unsigned I = 0;
Adrian Prantlad768c32015-01-14 01:01:28 +0000206 bool ValidReg = false;
Adrian Prantl092d9482015-01-13 23:39:11 +0000207 // Pattern-match combinations for which more efficient representations exist
208 // first.
209 if (N >= 3 && Expr.getElement(0) == dwarf::DW_OP_piece) {
210 unsigned SizeOfByte = 8;
211 unsigned OffsetInBits = Expr.getElement(1) * SizeOfByte;
212 unsigned SizeInBits = Expr.getElement(2) * SizeOfByte;
Adrian Prantlad768c32015-01-14 01:01:28 +0000213 ValidReg =
214 AddMachineRegPiece(MachineReg, SizeInBits,
215 getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
Adrian Prantl092d9482015-01-13 23:39:11 +0000216 I = 3;
217 } else if (N >= 3 && Expr.getElement(0) == dwarf::DW_OP_plus &&
218 Expr.getElement(2) == dwarf::DW_OP_deref) {
219 // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
220 unsigned Offset = Expr.getElement(1);
Adrian Prantlad768c32015-01-14 01:01:28 +0000221 ValidReg = AddMachineRegIndirect(MachineReg, Offset);
Adrian Prantl092d9482015-01-13 23:39:11 +0000222 I = 3;
223 } else if (N >= 1 && Expr.getElement(0) == dwarf::DW_OP_deref) {
224 // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
Adrian Prantlad768c32015-01-14 01:01:28 +0000225 ValidReg = AddMachineRegIndirect(MachineReg);
Adrian Prantl092d9482015-01-13 23:39:11 +0000226 I = 1;
227 } else
Adrian Prantlad768c32015-01-14 01:01:28 +0000228 ValidReg = AddMachineRegPiece(MachineReg);
229
230 if (!ValidReg)
231 return false;
Adrian Prantl092d9482015-01-13 23:39:11 +0000232
233 // Emit remaining elements of the expression.
234 AddExpression(Expr, I);
Adrian Prantlad768c32015-01-14 01:01:28 +0000235 return true;
Adrian Prantl092d9482015-01-13 23:39:11 +0000236}
237
238void DwarfExpression::AddExpression(DIExpression Expr, unsigned I,
239 unsigned PieceOffsetInBits) {
240 unsigned N = Expr.getNumElements();
241 for (; I < N; ++I) {
242 switch (Expr.getElement(I)) {
243 case dwarf::DW_OP_piece: {
244 unsigned SizeOfByte = 8;
245 unsigned OffsetInBits = Expr.getElement(++I) * SizeOfByte;
246 unsigned SizeInBits = Expr.getElement(++I) * SizeOfByte;
247 AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
248 break;
249 }
250 case dwarf::DW_OP_plus:
251 EmitOp(dwarf::DW_OP_plus_uconst);
252 EmitUnsigned(Expr.getElement(++I));
253 break;
254 case dwarf::DW_OP_deref:
255 EmitOp(dwarf::DW_OP_deref);
256 break;
257 default:
258 llvm_unreachable("unhandled opcode found in DIExpression");
259 }
260 }
261}