blob: 12273aa18248ea6f8889ce2bbadf5d63225c2c16 [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
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000068bool DwarfExpression::AddMachineRegIndirect(const TargetRegisterInfo &TRI,
69 unsigned MachineReg, int Offset) {
70 if (isFrameRegister(TRI, MachineReg)) {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000071 // If variable offset is based in frame register then use fbreg.
72 EmitOp(dwarf::DW_OP_fbreg);
73 EmitSigned(Offset);
Adrian Prantlb2838152015-03-03 20:12:52 +000074 return true;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000075 }
Adrian Prantlb2838152015-03-03 20:12:52 +000076
77 int DwarfReg = TRI.getDwarfRegNum(MachineReg, false);
78 if (DwarfReg < 0)
79 return false;
80
81 AddRegIndirect(DwarfReg, Offset);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000082 return true;
83}
84
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000085bool DwarfExpression::AddMachineRegPiece(const TargetRegisterInfo &TRI,
86 unsigned MachineReg,
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000087 unsigned PieceSizeInBits,
88 unsigned PieceOffsetInBits) {
Adrian Prantl92da14b2015-03-02 22:02:33 +000089 if (!TRI.isPhysicalRegister(MachineReg))
Adrian Prantl40cb8192015-01-25 19:04:08 +000090 return false;
91
Adrian Prantl92da14b2015-03-02 22:02:33 +000092 int Reg = TRI.getDwarfRegNum(MachineReg, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000093
94 // If this is a valid register number, emit it.
95 if (Reg >= 0) {
96 AddReg(Reg);
Adrian Prantl0e6ffb92015-01-12 22:37:16 +000097 if (PieceSizeInBits)
98 AddOpPiece(PieceSizeInBits, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +000099 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000100 }
101
102 // Walk up the super-register chain until we find a valid number.
103 // For example, EAX on x86_64 is a 32-bit piece of RAX with offset 0.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000104 for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
105 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000106 if (Reg >= 0) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000107 unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
108 unsigned Size = TRI.getSubRegIdxSize(Idx);
109 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000110 AddReg(Reg, "super-register");
111 if (PieceOffsetInBits == RegOffset) {
112 AddOpPiece(Size, RegOffset);
113 } else {
114 // If this is part of a variable in a sub-register at a
115 // non-zero offset, we need to manually shift the value into
116 // place, since the DW_OP_piece describes the part of the
117 // variable, not the position of the subregister.
118 if (RegOffset)
119 AddShr(RegOffset);
120 AddOpPiece(Size, PieceOffsetInBits);
121 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000122 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000123 }
124 }
125
126 // Otherwise, attempt to find a covering set of sub-register numbers.
127 // For example, Q0 on ARM is a composition of D0+D1.
128 //
129 // Keep track of the current position so we can emit the more
130 // efficient DW_OP_piece.
131 unsigned CurPos = PieceOffsetInBits;
132 // The size of the register in bits, assuming 8 bits per byte.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000133 unsigned RegSize = TRI.getMinimalPhysRegClass(MachineReg)->getSize() * 8;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000134 // Keep track of the bits in the register we already emitted, so we
135 // can avoid emitting redundant aliasing subregs.
136 SmallBitVector Coverage(RegSize, false);
Adrian Prantl92da14b2015-03-02 22:02:33 +0000137 for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
138 unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
139 unsigned Size = TRI.getSubRegIdxSize(Idx);
140 unsigned Offset = TRI.getSubRegIdxOffset(Idx);
141 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000142
143 // Intersection between the bits we already emitted and the bits
144 // covered by this subregister.
145 SmallBitVector Intersection(RegSize, false);
146 Intersection.set(Offset, Offset + Size);
147 Intersection ^= Coverage;
148
149 // If this sub-register has a DWARF number and we haven't covered
150 // its range, emit a DWARF piece for it.
151 if (Reg >= 0 && Intersection.any()) {
152 AddReg(Reg, "sub-register");
153 AddOpPiece(Size, Offset == CurPos ? 0 : Offset);
154 CurPos = Offset + Size;
155
156 // Mark it as emitted.
157 Coverage.set(Offset, Offset + Size);
158 }
159 }
160
Adrian Prantlad768c32015-01-14 01:01:28 +0000161 return CurPos > PieceOffsetInBits;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000162}
Adrian Prantl66f25952015-01-13 00:04:06 +0000163
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000164void DwarfExpression::AddStackValue() {
165 if (DwarfVersion >= 4)
166 EmitOp(dwarf::DW_OP_stack_value);
167}
168
Adrian Prantl29ce7012016-06-24 21:35:09 +0000169void DwarfExpression::AddSignedConstant(int64_t Value) {
Adrian Prantl66f25952015-01-13 00:04:06 +0000170 EmitOp(dwarf::DW_OP_consts);
171 EmitSigned(Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000172 AddStackValue();
Adrian Prantl66f25952015-01-13 00:04:06 +0000173}
174
Adrian Prantl29ce7012016-06-24 21:35:09 +0000175void DwarfExpression::AddUnsignedConstant(uint64_t Value) {
Adrian Prantl66f25952015-01-13 00:04:06 +0000176 EmitOp(dwarf::DW_OP_constu);
177 EmitUnsigned(Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000178 AddStackValue();
179}
180
Benjamin Kramerc321e532016-06-08 19:09:22 +0000181void DwarfExpression::AddUnsignedConstant(const APInt &Value) {
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000182 unsigned Size = Value.getBitWidth();
183 const uint64_t *Data = Value.getRawData();
184
185 // Chop it up into 64-bit pieces, because that's the maximum that
186 // AddUnsignedConstant takes.
187 unsigned Offset = 0;
188 while (Offset < Size) {
189 AddUnsignedConstant(*Data++);
190 if (Offset == 0 && Size <= 64)
191 break;
192 AddOpPiece(std::min(Size-Offset, 64u), Offset);
193 Offset += 64;
194 }
Adrian Prantl66f25952015-01-13 00:04:06 +0000195}
Adrian Prantl092d9482015-01-13 23:39:11 +0000196
197static unsigned getOffsetOrZero(unsigned OffsetInBits,
198 unsigned PieceOffsetInBits) {
199 if (OffsetInBits == PieceOffsetInBits)
200 return 0;
201 assert(OffsetInBits >= PieceOffsetInBits && "overlapping pieces");
202 return OffsetInBits;
203}
204
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000205bool DwarfExpression::AddMachineRegExpression(const TargetRegisterInfo &TRI,
Adrian Prantl54286bd2016-11-02 16:12:20 +0000206 DIExpressionCursor &ExprCursor,
Adrian Prantl092d9482015-01-13 23:39:11 +0000207 unsigned MachineReg,
208 unsigned PieceOffsetInBits) {
Adrian Prantl54286bd2016-11-02 16:12:20 +0000209 if (!ExprCursor)
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000210 return AddMachineRegPiece(TRI, MachineReg);
Adrian Prantl531641a2015-01-22 00:00:59 +0000211
Adrian Prantl0f615792015-03-04 17:39:33 +0000212 // Pattern-match combinations for which more efficient representations exist
213 // first.
Adrian Prantl531641a2015-01-22 00:00:59 +0000214 bool ValidReg = false;
Adrian Prantl54286bd2016-11-02 16:12:20 +0000215 auto Op = ExprCursor.peek();
216 switch (Op->getOp()) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000217 case dwarf::DW_OP_bit_piece: {
Adrian Prantl54286bd2016-11-02 16:12:20 +0000218 unsigned OffsetInBits = Op->getArg(0);
219 unsigned SizeInBits = Op->getArg(1);
Adrian Prantl531641a2015-01-22 00:00:59 +0000220 // Piece always comes at the end of the expression.
Adrian Prantl54286bd2016-11-02 16:12:20 +0000221 AddMachineRegPiece(TRI, MachineReg, SizeInBits,
222 getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
223 ExprCursor.take();
224 break;
Adrian Prantl531641a2015-01-22 00:00:59 +0000225 }
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000226 case dwarf::DW_OP_plus:
227 case dwarf::DW_OP_minus: {
228 // [DW_OP_reg,Offset,DW_OP_plus, DW_OP_deref] --> [DW_OP_breg, Offset].
229 // [DW_OP_reg,Offset,DW_OP_minus,DW_OP_deref] --> [DW_OP_breg,-Offset].
Adrian Prantl54286bd2016-11-02 16:12:20 +0000230 auto N = ExprCursor.peekNext();
231 if (N && N->getOp() == dwarf::DW_OP_deref) {
232 unsigned Offset = Op->getArg(0);
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000233 ValidReg = AddMachineRegIndirect(
Adrian Prantl54286bd2016-11-02 16:12:20 +0000234 TRI, MachineReg, Op->getOp() == dwarf::DW_OP_plus ? Offset : -Offset);
235 ExprCursor.consume(2);
David Blaikie0ebe35b2015-06-09 18:01:51 +0000236 } else
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000237 ValidReg = AddMachineRegPiece(TRI, MachineReg);
Adrian Prantl54286bd2016-11-02 16:12:20 +0000238 break;
Adrian Prantl0f615792015-03-04 17:39:33 +0000239 }
Adrian Prantl54286bd2016-11-02 16:12:20 +0000240 case dwarf::DW_OP_deref:
241 // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
242 ValidReg = AddMachineRegIndirect(TRI, MachineReg);
243 ExprCursor.take();
244 break;
Adrian Prantl531641a2015-01-22 00:00:59 +0000245 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000246
Adrian Prantl54286bd2016-11-02 16:12:20 +0000247 return ValidReg;
Adrian Prantl092d9482015-01-13 23:39:11 +0000248}
249
Adrian Prantl54286bd2016-11-02 16:12:20 +0000250void DwarfExpression::AddExpression(DIExpressionCursor &&ExprCursor,
Adrian Prantl092d9482015-01-13 23:39:11 +0000251 unsigned PieceOffsetInBits) {
Adrian Prantl54286bd2016-11-02 16:12:20 +0000252 while (ExprCursor) {
253 auto Op = ExprCursor.take();
254 switch (Op->getOp()) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000255 case dwarf::DW_OP_bit_piece: {
Adrian Prantl54286bd2016-11-02 16:12:20 +0000256 unsigned OffsetInBits = Op->getArg(0);
257 unsigned SizeInBits = Op->getArg(1);
Adrian Prantl092d9482015-01-13 23:39:11 +0000258 AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
259 break;
260 }
261 case dwarf::DW_OP_plus:
262 EmitOp(dwarf::DW_OP_plus_uconst);
Adrian Prantl54286bd2016-11-02 16:12:20 +0000263 EmitUnsigned(Op->getArg(0));
Adrian Prantl092d9482015-01-13 23:39:11 +0000264 break;
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000265 case dwarf::DW_OP_minus:
266 // There is no OP_minus_uconst.
267 EmitOp(dwarf::DW_OP_constu);
Adrian Prantl54286bd2016-11-02 16:12:20 +0000268 EmitUnsigned(Op->getArg(0));
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000269 EmitOp(dwarf::DW_OP_minus);
270 break;
Adrian Prantl092d9482015-01-13 23:39:11 +0000271 case dwarf::DW_OP_deref:
272 EmitOp(dwarf::DW_OP_deref);
273 break;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000274 case dwarf::DW_OP_constu:
275 EmitOp(dwarf::DW_OP_constu);
Adrian Prantl54286bd2016-11-02 16:12:20 +0000276 EmitUnsigned(Op->getArg(0));
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000277 break;
278 case dwarf::DW_OP_stack_value:
279 AddStackValue();
280 break;
Adrian Prantl092d9482015-01-13 23:39:11 +0000281 default:
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000282 llvm_unreachable("unhandled opcode found in expression");
Adrian Prantl092d9482015-01-13 23:39:11 +0000283 }
284 }
285}