blob: 7dbc6cb3995174eee5cd2d87c6af8e8ccf121421 [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,
206 const DIExpression *Expr,
Adrian Prantl092d9482015-01-13 23:39:11 +0000207 unsigned MachineReg,
208 unsigned PieceOffsetInBits) {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000209 auto I = Expr->expr_op_begin();
210 auto E = Expr->expr_op_end();
Adrian Prantl0f615792015-03-04 17:39:33 +0000211 if (I == E)
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000212 return AddMachineRegPiece(TRI, MachineReg);
Adrian Prantl531641a2015-01-22 00:00:59 +0000213
Adrian Prantl0f615792015-03-04 17:39:33 +0000214 // Pattern-match combinations for which more efficient representations exist
215 // first.
Adrian Prantl531641a2015-01-22 00:00:59 +0000216 bool ValidReg = false;
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000217 switch (I->getOp()) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000218 case dwarf::DW_OP_bit_piece: {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000219 unsigned OffsetInBits = I->getArg(0);
220 unsigned SizeInBits = I->getArg(1);
Adrian Prantl531641a2015-01-22 00:00:59 +0000221 // Piece always comes at the end of the expression.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000222 return AddMachineRegPiece(TRI, MachineReg, SizeInBits,
Adrian Prantl531641a2015-01-22 00:00:59 +0000223 getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
224 }
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000225 case dwarf::DW_OP_plus:
226 case dwarf::DW_OP_minus: {
227 // [DW_OP_reg,Offset,DW_OP_plus, DW_OP_deref] --> [DW_OP_breg, Offset].
228 // [DW_OP_reg,Offset,DW_OP_minus,DW_OP_deref] --> [DW_OP_breg,-Offset].
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000229 auto N = I.getNext();
230 if (N != E && N->getOp() == dwarf::DW_OP_deref) {
David Blaikie0ebe35b2015-06-09 18:01:51 +0000231 unsigned Offset = I->getArg(0);
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000232 ValidReg = AddMachineRegIndirect(
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000233 TRI, MachineReg, I->getOp() == dwarf::DW_OP_plus ? Offset : -Offset);
Adrian Prantl531641a2015-01-22 00:00:59 +0000234 std::advance(I, 2);
David Blaikie0ebe35b2015-06-09 18:01:51 +0000235 break;
236 } else
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000237 ValidReg = AddMachineRegPiece(TRI, MachineReg);
Adrian Prantl0f615792015-03-04 17:39:33 +0000238 }
239 case dwarf::DW_OP_deref: {
240 // [DW_OP_reg,DW_OP_deref] --> [DW_OP_breg].
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000241 ValidReg = AddMachineRegIndirect(TRI, MachineReg);
Adrian Prantl0f615792015-03-04 17:39:33 +0000242 ++I;
243 break;
244 }
Adrian Prantl531641a2015-01-22 00:00:59 +0000245 default:
246 llvm_unreachable("unsupported operand");
247 }
Adrian Prantlad768c32015-01-14 01:01:28 +0000248
249 if (!ValidReg)
250 return false;
Adrian Prantl092d9482015-01-13 23:39:11 +0000251
252 // Emit remaining elements of the expression.
Adrian Prantl0f615792015-03-04 17:39:33 +0000253 AddExpression(I, E, PieceOffsetInBits);
Adrian Prantlad768c32015-01-14 01:01:28 +0000254 return true;
Adrian Prantl092d9482015-01-13 23:39:11 +0000255}
256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000257void DwarfExpression::AddExpression(DIExpression::expr_op_iterator I,
258 DIExpression::expr_op_iterator E,
Adrian Prantl092d9482015-01-13 23:39:11 +0000259 unsigned PieceOffsetInBits) {
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000260 for (; I != E; ++I) {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000261 switch (I->getOp()) {
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000262 case dwarf::DW_OP_bit_piece: {
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000263 unsigned OffsetInBits = I->getArg(0);
264 unsigned SizeInBits = I->getArg(1);
Adrian Prantl092d9482015-01-13 23:39:11 +0000265 AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
266 break;
267 }
268 case dwarf::DW_OP_plus:
269 EmitOp(dwarf::DW_OP_plus_uconst);
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +0000270 EmitUnsigned(I->getArg(0));
Adrian Prantl092d9482015-01-13 23:39:11 +0000271 break;
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000272 case dwarf::DW_OP_minus:
273 // There is no OP_minus_uconst.
274 EmitOp(dwarf::DW_OP_constu);
275 EmitUnsigned(I->getArg(0));
276 EmitOp(dwarf::DW_OP_minus);
277 break;
Adrian Prantl092d9482015-01-13 23:39:11 +0000278 case dwarf::DW_OP_deref:
279 EmitOp(dwarf::DW_OP_deref);
280 break;
281 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}