blob: a2799b8d6300251c7dc478d2727e7c793c70c5a5 [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 Prantl66f25952015-01-13 00:04:06 +000025void DwarfExpression::AddReg(int DwarfReg, const char *Comment) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000026 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
27 if (DwarfReg < 32) {
28 EmitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
29 } else {
30 EmitOp(dwarf::DW_OP_regx, Comment);
31 EmitUnsigned(DwarfReg);
32 }
33}
34
35void DwarfExpression::AddRegIndirect(int DwarfReg, int Offset, bool Deref) {
36 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
37 if (DwarfReg < 32) {
38 EmitOp(dwarf::DW_OP_breg0 + DwarfReg);
39 } else {
40 EmitOp(dwarf::DW_OP_bregx);
41 EmitUnsigned(DwarfReg);
42 }
43 EmitSigned(Offset);
44 if (Deref)
45 EmitOp(dwarf::DW_OP_deref);
46}
47
Adrian Prantl66f25952015-01-13 00:04:06 +000048void DwarfExpression::AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000049 assert(SizeInBits > 0 && "piece has size zero");
50 const unsigned SizeOfByte = 8;
51 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
52 EmitOp(dwarf::DW_OP_bit_piece);
53 EmitUnsigned(SizeInBits);
54 EmitUnsigned(OffsetInBits);
55 } else {
56 EmitOp(dwarf::DW_OP_piece);
57 unsigned ByteSize = SizeInBits / SizeOfByte;
58 EmitUnsigned(ByteSize);
59 }
60}
61
62void DwarfExpression::AddShr(unsigned ShiftBy) {
63 EmitOp(dwarf::DW_OP_constu);
64 EmitUnsigned(ShiftBy);
65 EmitOp(dwarf::DW_OP_shr);
66}
67
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000068bool DwarfExpression::AddMachineRegIndirect(unsigned MachineReg, int Offset) {
Adrian Prantl8995f5c2015-01-13 23:10:43 +000069 if (isFrameRegister(MachineReg)) {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000070 // If variable offset is based in frame register then use fbreg.
71 EmitOp(dwarf::DW_OP_fbreg);
72 EmitSigned(Offset);
Adrian Prantlb2838152015-03-03 20:12:52 +000073 return true;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000074 }
Adrian Prantlb2838152015-03-03 20:12:52 +000075
76 int DwarfReg = TRI.getDwarfRegNum(MachineReg, false);
77 if (DwarfReg < 0)
78 return false;
79
80 AddRegIndirect(DwarfReg, Offset);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000081 return true;
82}
83
Adrian Prantlad768c32015-01-14 01:01:28 +000084bool DwarfExpression::AddMachineRegPiece(unsigned MachineReg,
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000085 unsigned PieceSizeInBits,
86 unsigned PieceOffsetInBits) {
Adrian Prantl92da14b2015-03-02 22:02:33 +000087 if (!TRI.isPhysicalRegister(MachineReg))
Adrian Prantl40cb8192015-01-25 19:04:08 +000088 return false;
89
Adrian Prantl92da14b2015-03-02 22:02:33 +000090 int Reg = TRI.getDwarfRegNum(MachineReg, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000091
92 // If this is a valid register number, emit it.
93 if (Reg >= 0) {
94 AddReg(Reg);
Adrian Prantl0e6ffb92015-01-12 22:37:16 +000095 if (PieceSizeInBits)
96 AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +000097 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000098 }
99
100 // Walk up the super-register chain until we find a valid number.
101 // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000102 for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
103 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000104 if (Reg >= 0) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000105 unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
106 unsigned Size = TRI.getSubRegIdxSize(Idx);
107 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000108 AddReg(Reg, "super-register");
109 if (PieceOffsetInBits == RegOffset) {
110 AddOpPiece(Size, RegOffset);
111 } else {
112 // If this is part of a variable in a sub-register at a
113 // non-zero offset, we need to manually shift the value into
114 // place, since the DW_OP_piece describes the part of the
115 // variable, not the position of the subregister.
116 if (RegOffset)
117 AddShr(RegOffset);
118 AddOpPiece(Size, PieceOffsetInBits);
119 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000120 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000121 }
122 }
123
124 // Otherwise, attempt to find a covering set of sub-register numbers.
125 // For example, Q0 on ARM is a composition of D0+D1.
126 //
127 // Keep track of the current position so we can emit the more
128 // efficient DW_OP_piece.
129 unsigned CurPos = PieceOffsetInBits;
130 // The size of the register in bits, assuming 8 bits per byte.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000131 unsigned RegSize = TRI.getMinimalPhysRegClass(MachineReg)->getSize() * 8;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000132 // Keep track of the bits in the register we already emitted, so we
133 // can avoid emitting redundant aliasing subregs.
134 SmallBitVector Coverage(RegSize, false);
Adrian Prantl92da14b2015-03-02 22:02:33 +0000135 for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
136 unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
137 unsigned Size = TRI.getSubRegIdxSize(Idx);
138 unsigned Offset = TRI.getSubRegIdxOffset(Idx);
139 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000140
141 // Intersection between the bits we already emitted and the bits
142 // covered by this subregister.
143 SmallBitVector Intersection(RegSize, false);
144 Intersection.set(Offset, Offset + Size);
145 Intersection ^= Coverage;
146
147 // If this sub-register has a DWARF number and we haven't covered
148 // its range, emit a DWARF piece for it.
149 if (Reg >= 0 && Intersection.any()) {
150 AddReg(Reg, "sub-register");
151 AddOpPiece(Size, Offset == CurPos ? 0 : Offset);
152 CurPos = Offset + Size;
153
154 // Mark it as emitted.
155 Coverage.set(Offset, Offset + Size);
156 }
157 }
158
Adrian Prantlad768c32015-01-14 01:01:28 +0000159 return CurPos > PieceOffsetInBits;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000160}
Adrian Prantl66f25952015-01-13 00:04:06 +0000161
162void DwarfExpression::AddSignedConstant(int Value) {
163 EmitOp(dwarf::DW_OP_consts);
164 EmitSigned(Value);
165 // The proper way to describe a constant value is
166 // DW_OP_constu <const>, DW_OP_stack_value.
167 // Unfortunately, DW_OP_stack_value was not available until DWARF-4,
168 // so we will continue to generate DW_OP_constu <const> for DWARF-2
169 // and DWARF-3. Technically, this is incorrect since DW_OP_const <const>
170 // actually describes a value at a constant addess, not a constant value.
171 // However, in the past there was no better way to describe a constant
172 // value, so the producers and consumers started to rely on heuristics
173 // to disambiguate the value vs. location status of the expression.
174 // See PR21176 for more details.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000175 if (DwarfVersion >= 4)
Adrian Prantl66f25952015-01-13 00:04:06 +0000176 EmitOp(dwarf::DW_OP_stack_value);
177}
178
179void DwarfExpression::AddUnsignedConstant(unsigned Value) {
180 EmitOp(dwarf::DW_OP_constu);
181 EmitUnsigned(Value);
182 // cf. comment in DwarfExpression::AddSignedConstant().
Adrian Prantl92da14b2015-03-02 22:02:33 +0000183 if (DwarfVersion >= 4)
Adrian Prantl66f25952015-01-13 00:04:06 +0000184 EmitOp(dwarf::DW_OP_stack_value);
185}
Adrian Prantl092d9482015-01-13 23:39:11 +0000186
187static unsigned getOffsetOrZero(unsigned OffsetInBits,
188 unsigned PieceOffsetInBits) {
189 if (OffsetInBits == PieceOffsetInBits)
190 return 0;
191 assert(OffsetInBits >= PieceOffsetInBits && "overlapping pieces");
192 return OffsetInBits;
193}
194
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000195bool DwarfExpression::AddMachineRegExpression(const DIExpression *Expr,
Adrian Prantl092d9482015-01-13 23:39:11 +0000196 unsigned MachineReg,
197 unsigned PieceOffsetInBits) {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000198 auto I = Expr->expr_op_begin();
199 auto E = Expr->expr_op_end();
Adrian Prantl0f615792015-03-04 17:39:33 +0000200 if (I == E)
Adrian Prantl531641a2015-01-22 00:00:59 +0000201 return AddMachineRegPiece(MachineReg);
202
Adrian Prantl0f615792015-03-04 17:39:33 +0000203 // Pattern-match combinations for which more efficient representations exist
204 // first.
Adrian Prantl531641a2015-01-22 00:00:59 +0000205 bool ValidReg = false;
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000206 switch (I->getOp()) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000207 case dwarf::DW_OP_bit_piece: {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000208 unsigned OffsetInBits = I->getArg(0);
209 unsigned SizeInBits = I->getArg(1);
Adrian Prantl531641a2015-01-22 00:00:59 +0000210 // Piece always comes at the end of the expression.
211 return AddMachineRegPiece(MachineReg, SizeInBits,
212 getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
213 }
Adrian Prantl0f615792015-03-04 17:39:33 +0000214 case dwarf::DW_OP_plus: {
Adrian Prantl092d9482015-01-13 23:39:11 +0000215 // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000216 auto N = I.getNext();
217 if (N != E && N->getOp() == dwarf::DW_OP_deref) {
218 unsigned Offset = I->getArg(0);
Adrian Prantl531641a2015-01-22 00:00:59 +0000219 ValidReg = AddMachineRegIndirect(MachineReg, Offset);
220 std::advance(I, 2);
221 break;
222 } else
223 ValidReg = AddMachineRegPiece(MachineReg);
Adrian Prantl0f615792015-03-04 17:39:33 +0000224 }
225 case dwarf::DW_OP_deref: {
226 // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
227 ValidReg = AddMachineRegIndirect(MachineReg);
228 ++I;
229 break;
230 }
Adrian Prantl531641a2015-01-22 00:00:59 +0000231 default:
232 llvm_unreachable("unsupported operand");
233 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000234
235 if (!ValidReg)
236 return false;
Adrian Prantl092d9482015-01-13 23:39:11 +0000237
238 // Emit remaining elements of the expression.
Adrian Prantl0f615792015-03-04 17:39:33 +0000239 AddExpression(I, E, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +0000240 return true;
Adrian Prantl092d9482015-01-13 23:39:11 +0000241}
242
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243void DwarfExpression::AddExpression(DIExpression::expr_op_iterator I,
244 DIExpression::expr_op_iterator E,
Adrian Prantl092d9482015-01-13 23:39:11 +0000245 unsigned PieceOffsetInBits) {
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000246 for (; I != E; ++I) {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000247 switch (I->getOp()) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000248 case dwarf::DW_OP_bit_piece: {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000249 unsigned OffsetInBits = I->getArg(0);
250 unsigned SizeInBits = I->getArg(1);
Adrian Prantl092d9482015-01-13 23:39:11 +0000251 AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
252 break;
253 }
254 case dwarf::DW_OP_plus:
255 EmitOp(dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000256 EmitUnsigned(I->getArg(0));
Adrian Prantl092d9482015-01-13 23:39:11 +0000257 break;
258 case dwarf::DW_OP_deref:
259 EmitOp(dwarf::DW_OP_deref);
260 break;
261 default:
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000262 llvm_unreachable("unhandled opcode found in expression");
Adrian Prantl092d9482015-01-13 23:39:11 +0000263 }
264 }
265}