blob: eb101ca2dcbbeee70ee45ee63f284a560e39d43b [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#include "DwarfDebug.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000016#include "llvm/ADT/SmallBitVector.h"
Adrian Prantla4c30d62015-01-12 23:36:56 +000017#include "llvm/CodeGen/AsmPrinter.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000018#include "llvm/Support/Dwarf.h"
19#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetRegisterInfo.h"
21#include "llvm/Target/TargetSubtargetInfo.h"
22
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000023using namespace llvm;
24
Adrian Prantla4c30d62015-01-12 23:36:56 +000025const TargetRegisterInfo *DwarfExpression::getTRI() const {
26 return AP.TM.getSubtargetImpl()->getRegisterInfo();
27}
28
Adrian Prantl66f25952015-01-13 00:04:06 +000029unsigned DwarfExpression::getDwarfVersion() const {
30 return AP.getDwarfDebug()->getDwarfVersion();
31}
32
33void DwarfExpression::AddReg(int DwarfReg, const char *Comment) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000034 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
35 if (DwarfReg < 32) {
36 EmitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
37 } else {
38 EmitOp(dwarf::DW_OP_regx, Comment);
39 EmitUnsigned(DwarfReg);
40 }
41}
42
43void DwarfExpression::AddRegIndirect(int DwarfReg, int Offset, bool Deref) {
44 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
45 if (DwarfReg < 32) {
46 EmitOp(dwarf::DW_OP_breg0 + DwarfReg);
47 } else {
48 EmitOp(dwarf::DW_OP_bregx);
49 EmitUnsigned(DwarfReg);
50 }
51 EmitSigned(Offset);
52 if (Deref)
53 EmitOp(dwarf::DW_OP_deref);
54}
55
Adrian Prantl66f25952015-01-13 00:04:06 +000056void DwarfExpression::AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000057 assert(SizeInBits > 0 && "piece has size zero");
58 const unsigned SizeOfByte = 8;
59 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
60 EmitOp(dwarf::DW_OP_bit_piece);
61 EmitUnsigned(SizeInBits);
62 EmitUnsigned(OffsetInBits);
63 } else {
64 EmitOp(dwarf::DW_OP_piece);
65 unsigned ByteSize = SizeInBits / SizeOfByte;
66 EmitUnsigned(ByteSize);
67 }
68}
69
70void DwarfExpression::AddShr(unsigned ShiftBy) {
71 EmitOp(dwarf::DW_OP_constu);
72 EmitUnsigned(ShiftBy);
73 EmitOp(dwarf::DW_OP_shr);
74}
75
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000076bool DwarfExpression::AddMachineRegIndirect(unsigned MachineReg, int Offset) {
Adrian Prantla4c30d62015-01-12 23:36:56 +000077 int DwarfReg = getTRI()->getDwarfRegNum(MachineReg, false);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000078 if (DwarfReg < 0)
79 return false;
80
Adrian Prantl8995f5c2015-01-13 23:10:43 +000081 if (isFrameRegister(MachineReg)) {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000082 // If variable offset is based in frame register then use fbreg.
83 EmitOp(dwarf::DW_OP_fbreg);
84 EmitSigned(Offset);
85 } else {
86 AddRegIndirect(DwarfReg, Offset);
87 }
88 return true;
89}
90
Adrian Prantlad768c32015-01-14 01:01:28 +000091bool DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000092 unsigned PieceSizeInBits,
93 unsigned PieceOffsetInBits) {
Adrian Prantla4c30d62015-01-12 23:36:56 +000094 const TargetRegisterInfo *TRI = getTRI();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000095 int Reg = TRI->getDwarfRegNum(MachineReg, false);
96
97 // If this is a valid register number, emit it.
98 if (Reg >= 0) {
99 AddReg(Reg);
Adrian Prantl0e6ffb92015-01-12 22:37:16 +0000100 if (PieceSizeInBits)
101 AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +0000102 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000103 }
104
105 // Walk up the super-register chain until we find a valid number.
106 // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
107 for (MCSuperRegIterator SR(MachineReg, TRI); SR.isValid(); ++SR) {
108 Reg = TRI->getDwarfRegNum(*SR, false);
109 if (Reg >= 0) {
110 unsigned Idx = TRI->getSubRegIndex(*SR, MachineReg);
111 unsigned Size = TRI->getSubRegIdxSize(Idx);
112 unsigned RegOffset = TRI->getSubRegIdxOffset(Idx);
113 AddReg(Reg, "super-register");
114 if (PieceOffsetInBits == RegOffset) {
115 AddOpPiece(Size, RegOffset);
116 } else {
117 // If this is part of a variable in a sub-register at a
118 // non-zero offset, we need to manually shift the value into
119 // place, since the DW_OP_piece describes the part of the
120 // variable, not the position of the subregister.
121 if (RegOffset)
122 AddShr(RegOffset);
123 AddOpPiece(Size, PieceOffsetInBits);
124 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000125 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000126 }
127 }
128
129 // Otherwise, attempt to find a covering set of sub-register numbers.
130 // For example, Q0 on ARM is a composition of D0+D1.
131 //
132 // Keep track of the current position so we can emit the more
133 // efficient DW_OP_piece.
134 unsigned CurPos = PieceOffsetInBits;
135 // The size of the register in bits, assuming 8 bits per byte.
136 unsigned RegSize = TRI->getMinimalPhysRegClass(MachineReg)->getSize() * 8;
137 // Keep track of the bits in the register we already emitted, so we
138 // can avoid emitting redundant aliasing subregs.
139 SmallBitVector Coverage(RegSize, false);
140 for (MCSubRegIterator SR(MachineReg, TRI); SR.isValid(); ++SR) {
141 unsigned Idx = TRI->getSubRegIndex(MachineReg, *SR);
142 unsigned Size = TRI->getSubRegIdxSize(Idx);
143 unsigned Offset = TRI->getSubRegIdxOffset(Idx);
144 Reg = TRI->getDwarfRegNum(*SR, false);
145
146 // Intersection between the bits we already emitted and the bits
147 // covered by this subregister.
148 SmallBitVector Intersection(RegSize, false);
149 Intersection.set(Offset, Offset + Size);
150 Intersection ^= Coverage;
151
152 // If this sub-register has a DWARF number and we haven't covered
153 // its range, emit a DWARF piece for it.
154 if (Reg >= 0 && Intersection.any()) {
155 AddReg(Reg, "sub-register");
156 AddOpPiece(Size, Offset == CurPos ? 0 : Offset);
157 CurPos = Offset + Size;
158
159 // Mark it as emitted.
160 Coverage.set(Offset, Offset + Size);
161 }
162 }
163
Adrian Prantlad768c32015-01-14 01:01:28 +0000164 return CurPos > PieceOffsetInBits;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000165}
Adrian Prantl66f25952015-01-13 00:04:06 +0000166
167void DwarfExpression::AddSignedConstant(int Value) {
168 EmitOp(dwarf::DW_OP_consts);
169 EmitSigned(Value);
170 // The proper way to describe a constant value is
171 // DW_OP_constu <const>, DW_OP_stack_value.
172 // Unfortunately, DW_OP_stack_value was not available until DWARF-4,
173 // so we will continue to generate DW_OP_constu <const> for DWARF-2
174 // and DWARF-3. Technically, this is incorrect since DW_OP_const <const>
175 // actually describes a value at a constant addess, not a constant value.
176 // However, in the past there was no better way to describe a constant
177 // value, so the producers and consumers started to rely on heuristics
178 // to disambiguate the value vs. location status of the expression.
179 // See PR21176 for more details.
180 if (getDwarfVersion() >= 4)
181 EmitOp(dwarf::DW_OP_stack_value);
182}
183
184void DwarfExpression::AddUnsignedConstant(unsigned Value) {
185 EmitOp(dwarf::DW_OP_constu);
186 EmitUnsigned(Value);
187 // cf. comment in DwarfExpression::AddSignedConstant().
188 if (getDwarfVersion() >= 4)
189 EmitOp(dwarf::DW_OP_stack_value);
190}
Adrian Prantl092d9482015-01-13 23:39:11 +0000191
192static unsigned getOffsetOrZero(unsigned OffsetInBits,
193 unsigned PieceOffsetInBits) {
194 if (OffsetInBits == PieceOffsetInBits)
195 return 0;
196 assert(OffsetInBits >= PieceOffsetInBits && "overlapping pieces");
197 return OffsetInBits;
198}
199
Adrian Prantlad768c32015-01-14 01:01:28 +0000200bool DwarfExpression::AddMachineRegExpression(DIExpression Expr,
Adrian Prantl092d9482015-01-13 23:39:11 +0000201 unsigned MachineReg,
202 unsigned PieceOffsetInBits) {
Adrian Prantl531641a2015-01-22 00:00:59 +0000203 auto I = Expr.begin();
Adrian Prantl092d9482015-01-13 23:39:11 +0000204 // Pattern-match combinations for which more efficient representations exist
205 // first.
Adrian Prantl531641a2015-01-22 00:00:59 +0000206 if (I == Expr.end())
207 return AddMachineRegPiece(MachineReg);
208
209 bool ValidReg = false;
210 switch (*I) {
211 case dwarf::DW_OP_piece: {
Adrian Prantl092d9482015-01-13 23:39:11 +0000212 unsigned SizeOfByte = 8;
Adrian Prantl531641a2015-01-22 00:00:59 +0000213 unsigned OffsetInBits = I.getArg(1) * SizeOfByte;
214 unsigned SizeInBits = I.getArg(2) * SizeOfByte;
215 // Piece always comes at the end of the expression.
216 return AddMachineRegPiece(MachineReg, SizeInBits,
217 getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
218 }
219 case dwarf::DW_OP_plus:
Adrian Prantl092d9482015-01-13 23:39:11 +0000220 // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
Adrian Prantl531641a2015-01-22 00:00:59 +0000221 if (*std::next(I) == dwarf::DW_OP_deref) {
222 unsigned Offset = I.getArg(1);
223 ValidReg = AddMachineRegIndirect(MachineReg, Offset);
224 std::advance(I, 2);
225 break;
226 } else
227 ValidReg = AddMachineRegPiece(MachineReg);
228 case dwarf::DW_OP_deref:
Adrian Prantl092d9482015-01-13 23:39:11 +0000229 // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
Adrian Prantlad768c32015-01-14 01:01:28 +0000230 ValidReg = AddMachineRegIndirect(MachineReg);
Adrian Prantl531641a2015-01-22 00:00:59 +0000231 ++I;
232 break;
233 default:
234 llvm_unreachable("unsupported operand");
235 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000236
237 if (!ValidReg)
238 return false;
Adrian Prantl092d9482015-01-13 23:39:11 +0000239
240 // Emit remaining elements of the expression.
Adrian Prantl531641a2015-01-22 00:00:59 +0000241 AddExpression(I, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +0000242 return true;
Adrian Prantl092d9482015-01-13 23:39:11 +0000243}
244
Adrian Prantl2585a982015-01-22 16:55:20 +0000245void DwarfExpression::AddExpression(DIExpression::iterator I,
Adrian Prantl092d9482015-01-13 23:39:11 +0000246 unsigned PieceOffsetInBits) {
Adrian Prantl2585a982015-01-22 16:55:20 +0000247 for (; I != DIExpression::iterator(); ++I) {
Adrian Prantl531641a2015-01-22 00:00:59 +0000248 switch (*I) {
Adrian Prantl092d9482015-01-13 23:39:11 +0000249 case dwarf::DW_OP_piece: {
250 unsigned SizeOfByte = 8;
Adrian Prantl531641a2015-01-22 00:00:59 +0000251 unsigned OffsetInBits = I.getArg(1) * SizeOfByte;
252 unsigned SizeInBits = I.getArg(2) * SizeOfByte;
Adrian Prantl092d9482015-01-13 23:39:11 +0000253 AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
254 break;
255 }
256 case dwarf::DW_OP_plus:
257 EmitOp(dwarf::DW_OP_plus_uconst);
Adrian Prantl531641a2015-01-22 00:00:59 +0000258 EmitUnsigned(I.getArg(1));
Adrian Prantl092d9482015-01-13 23:39:11 +0000259 break;
260 case dwarf::DW_OP_deref:
261 EmitOp(dwarf::DW_OP_deref);
262 break;
263 default:
264 llvm_unreachable("unhandled opcode found in DIExpression");
265 }
266 }
267}