blob: 8c767fc8ca7dae5e77bed50f5851ab4da7deb204 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework -----------===//
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing dwarf debug info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfExpression.h"
Markus Lavinb86ce212019-03-19 13:16:28 +000014#include "DwarfCompileUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000015#include "llvm/ADT/APInt.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000016#include "llvm/ADT/SmallBitVector.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/Dwarf.h"
Daniel Sanders2bea69b2019-08-01 23:27:28 +000018#include "llvm/CodeGen/Register.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000020#include "llvm/IR/DebugInfoMetadata.h"
21#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000022#include <algorithm>
23#include <cassert>
24#include <cstdint>
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000025
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000026using namespace llvm;
27
Jonas Devlieghere965b5982018-09-05 10:18:36 +000028void DwarfExpression::emitConstu(uint64_t Value) {
29 if (Value < 32)
30 emitOp(dwarf::DW_OP_lit0 + Value);
31 else if (Value == std::numeric_limits<uint64_t>::max()) {
32 // Only do this for 64-bit values as the DWARF expression stack uses
33 // target-address-size values.
34 emitOp(dwarf::DW_OP_lit0);
35 emitOp(dwarf::DW_OP_not);
36 } else {
37 emitOp(dwarf::DW_OP_constu);
38 emitUnsigned(Value);
39 }
40}
41
Adrian Prantla63b8e82017-03-16 17:42:45 +000042void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
Adrian Prantl6825fb62017-04-18 01:21:53 +000043 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
Petar Jovanovicff47d832019-05-23 10:37:13 +000044 assert((isUnknownLocation() || isRegisterLocation()) &&
Adrian Prantl6825fb62017-04-18 01:21:53 +000045 "location description already locked down");
46 LocationKind = Register;
47 if (DwarfReg < 32) {
48 emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000049 } else {
Adrian Prantla63b8e82017-03-16 17:42:45 +000050 emitOp(dwarf::DW_OP_regx, Comment);
51 emitUnsigned(DwarfReg);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000052 }
53}
54
Adrian Prantla2719882017-03-22 17:19:55 +000055void DwarfExpression::addBReg(int DwarfReg, int Offset) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000056 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
Petar Jovanovicff47d832019-05-23 10:37:13 +000057 assert(!isRegisterLocation() && "location description already locked down");
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000058 if (DwarfReg < 32) {
Adrian Prantla63b8e82017-03-16 17:42:45 +000059 emitOp(dwarf::DW_OP_breg0 + DwarfReg);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000060 } else {
Adrian Prantla63b8e82017-03-16 17:42:45 +000061 emitOp(dwarf::DW_OP_bregx);
62 emitUnsigned(DwarfReg);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000063 }
Adrian Prantla63b8e82017-03-16 17:42:45 +000064 emitSigned(Offset);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000065}
66
Adrian Prantl80e188d2017-03-22 01:15:57 +000067void DwarfExpression::addFBReg(int Offset) {
68 emitOp(dwarf::DW_OP_fbreg);
69 emitSigned(Offset);
70}
71
Adrian Prantla63b8e82017-03-16 17:42:45 +000072void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000073 if (!SizeInBits)
74 return;
75
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000076 const unsigned SizeOfByte = 8;
77 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
Adrian Prantla63b8e82017-03-16 17:42:45 +000078 emitOp(dwarf::DW_OP_bit_piece);
79 emitUnsigned(SizeInBits);
80 emitUnsigned(OffsetInBits);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000081 } else {
Adrian Prantla63b8e82017-03-16 17:42:45 +000082 emitOp(dwarf::DW_OP_piece);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000083 unsigned ByteSize = SizeInBits / SizeOfByte;
Adrian Prantla63b8e82017-03-16 17:42:45 +000084 emitUnsigned(ByteSize);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000085 }
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000086 this->OffsetInBits += SizeInBits;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000087}
88
Adrian Prantla63b8e82017-03-16 17:42:45 +000089void DwarfExpression::addShr(unsigned ShiftBy) {
Jonas Devlieghere965b5982018-09-05 10:18:36 +000090 emitConstu(ShiftBy);
Adrian Prantla63b8e82017-03-16 17:42:45 +000091 emitOp(dwarf::DW_OP_shr);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000092}
93
Adrian Prantla63b8e82017-03-16 17:42:45 +000094void DwarfExpression::addAnd(unsigned Mask) {
Jonas Devlieghere965b5982018-09-05 10:18:36 +000095 emitConstu(Mask);
Adrian Prantla63b8e82017-03-16 17:42:45 +000096 emitOp(dwarf::DW_OP_and);
Adrian Prantl981f03e2017-03-16 17:14:56 +000097}
98
Adrian Prantla63b8e82017-03-16 17:42:45 +000099bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
Adrian Prantl5542da42016-12-22 06:10:41 +0000100 unsigned MachineReg, unsigned MaxSize) {
Daniel Sanders2bea69b2019-08-01 23:27:28 +0000101 if (!llvm::Register::isPhysicalRegister(MachineReg)) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000102 if (isFrameRegister(TRI, MachineReg)) {
103 DwarfRegs.push_back({-1, 0, nullptr});
104 return true;
105 }
Adrian Prantl40cb8192015-01-25 19:04:08 +0000106 return false;
Adrian Prantl80e188d2017-03-22 01:15:57 +0000107 }
Adrian Prantl40cb8192015-01-25 19:04:08 +0000108
Adrian Prantl92da14b2015-03-02 22:02:33 +0000109 int Reg = TRI.getDwarfRegNum(MachineReg, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000110
111 // If this is a valid register number, emit it.
112 if (Reg >= 0) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000113 DwarfRegs.push_back({Reg, 0, nullptr});
Adrian Prantlad768c32015-01-14 01:01:28 +0000114 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000115 }
116
117 // Walk up the super-register chain until we find a valid number.
Adrian Prantl941fa752016-12-05 18:04:47 +0000118 // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000119 for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
120 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000121 if (Reg >= 0) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000122 unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
123 unsigned Size = TRI.getSubRegIdxSize(Idx);
124 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
Adrian Prantl80e188d2017-03-22 01:15:57 +0000125 DwarfRegs.push_back({Reg, 0, "super-register"});
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000126 // Use a DW_OP_bit_piece to describe the sub-register.
127 setSubRegisterPiece(Size, RegOffset);
Adrian Prantlad768c32015-01-14 01:01:28 +0000128 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000129 }
130 }
131
132 // Otherwise, attempt to find a covering set of sub-register numbers.
133 // For example, Q0 on ARM is a composition of D0+D1.
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000134 unsigned CurPos = 0;
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000135 // The size of the register in bits.
136 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(MachineReg);
137 unsigned RegSize = TRI.getRegSizeInBits(*RC);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000138 // Keep track of the bits in the register we already emitted, so we
Adrian Prantl984251c2018-02-13 19:54:00 +0000139 // can avoid emitting redundant aliasing subregs. Because this is
140 // just doing a greedy scan of all subregisters, it is possible that
141 // this doesn't find a combination of subregisters that fully cover
142 // the register (even though one may exist).
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000143 SmallBitVector Coverage(RegSize, false);
Adrian Prantl92da14b2015-03-02 22:02:33 +0000144 for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
145 unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
146 unsigned Size = TRI.getSubRegIdxSize(Idx);
147 unsigned Offset = TRI.getSubRegIdxOffset(Idx);
148 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantl3a3ba772017-10-10 20:33:43 +0000149 if (Reg < 0)
150 continue;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000151
152 // Intersection between the bits we already emitted and the bits
153 // covered by this subregister.
Adrian Prantl4cae1082017-08-28 23:07:43 +0000154 SmallBitVector CurSubReg(RegSize, false);
155 CurSubReg.set(Offset, Offset + Size);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000156
157 // If this sub-register has a DWARF number and we haven't covered
158 // its range, emit a DWARF piece for it.
Adrian Prantl3a3ba772017-10-10 20:33:43 +0000159 if (CurSubReg.test(Coverage)) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000160 // Emit a piece for any gap in the coverage.
161 if (Offset > CurPos)
Adrian Prantl984251c2018-02-13 19:54:00 +0000162 DwarfRegs.push_back({-1, Offset - CurPos, "no DWARF register encoding"});
Adrian Prantl80e188d2017-03-22 01:15:57 +0000163 DwarfRegs.push_back(
164 {Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"});
Adrian Prantl5542da42016-12-22 06:10:41 +0000165 if (Offset >= MaxSize)
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000166 break;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000167
168 // Mark it as emitted.
169 Coverage.set(Offset, Offset + Size);
Adrian Prantl80e188d2017-03-22 01:15:57 +0000170 CurPos = Offset + Size;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000171 }
172 }
Adrian Prantl984251c2018-02-13 19:54:00 +0000173 // Failed to find any DWARF encoding.
174 if (CurPos == 0)
175 return false;
176 // Found a partial or complete DWARF encoding.
177 if (CurPos < RegSize)
178 DwarfRegs.push_back({-1, RegSize - CurPos, "no DWARF register encoding"});
179 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000180}
Adrian Prantl66f25952015-01-13 00:04:06 +0000181
Adrian Prantla63b8e82017-03-16 17:42:45 +0000182void DwarfExpression::addStackValue() {
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000183 if (DwarfVersion >= 4)
Adrian Prantla63b8e82017-03-16 17:42:45 +0000184 emitOp(dwarf::DW_OP_stack_value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000185}
186
Adrian Prantla63b8e82017-03-16 17:42:45 +0000187void DwarfExpression::addSignedConstant(int64_t Value) {
Petar Jovanovicff47d832019-05-23 10:37:13 +0000188 assert(isImplicitLocation() || isUnknownLocation());
Adrian Prantl6825fb62017-04-18 01:21:53 +0000189 LocationKind = Implicit;
Adrian Prantla63b8e82017-03-16 17:42:45 +0000190 emitOp(dwarf::DW_OP_consts);
191 emitSigned(Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000192}
193
Adrian Prantla63b8e82017-03-16 17:42:45 +0000194void DwarfExpression::addUnsignedConstant(uint64_t Value) {
Petar Jovanovicff47d832019-05-23 10:37:13 +0000195 assert(isImplicitLocation() || isUnknownLocation());
Adrian Prantl6825fb62017-04-18 01:21:53 +0000196 LocationKind = Implicit;
Jonas Devlieghere965b5982018-09-05 10:18:36 +0000197 emitConstu(Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000198}
199
Adrian Prantla63b8e82017-03-16 17:42:45 +0000200void DwarfExpression::addUnsignedConstant(const APInt &Value) {
Petar Jovanovicff47d832019-05-23 10:37:13 +0000201 assert(isImplicitLocation() || isUnknownLocation());
Adrian Prantl6825fb62017-04-18 01:21:53 +0000202 LocationKind = Implicit;
203
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000204 unsigned Size = Value.getBitWidth();
205 const uint64_t *Data = Value.getRawData();
206
207 // Chop it up into 64-bit pieces, because that's the maximum that
Adrian Prantla63b8e82017-03-16 17:42:45 +0000208 // addUnsignedConstant takes.
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000209 unsigned Offset = 0;
210 while (Offset < Size) {
Adrian Prantla63b8e82017-03-16 17:42:45 +0000211 addUnsignedConstant(*Data++);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000212 if (Offset == 0 && Size <= 64)
213 break;
Adrian Prantl6825fb62017-04-18 01:21:53 +0000214 addStackValue();
215 addOpPiece(std::min(Size - Offset, 64u), Offset);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000216 Offset += 64;
217 }
Adrian Prantl66f25952015-01-13 00:04:06 +0000218}
Adrian Prantl092d9482015-01-13 23:39:11 +0000219
Adrian Prantlc12cee32017-04-19 23:42:25 +0000220bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
Adrian Prantl54286bd2016-11-02 16:12:20 +0000221 DIExpressionCursor &ExprCursor,
Adrian Prantlc12cee32017-04-19 23:42:25 +0000222 unsigned MachineReg,
Adrian Prantl941fa752016-12-05 18:04:47 +0000223 unsigned FragmentOffsetInBits) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000224 auto Fragment = ExprCursor.getFragmentInfo();
Adrian Prantldd215022017-04-25 19:40:53 +0000225 if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) {
226 LocationKind = Unknown;
Adrian Prantl80e188d2017-03-22 01:15:57 +0000227 return false;
Adrian Prantldd215022017-04-25 19:40:53 +0000228 }
Adrian Prantl531641a2015-01-22 00:00:59 +0000229
Adrian Prantl80e188d2017-03-22 01:15:57 +0000230 bool HasComplexExpression = false;
Adrian Prantl4dc03242017-03-21 17:14:30 +0000231 auto Op = ExprCursor.peek();
Adrian Prantl80e188d2017-03-22 01:15:57 +0000232 if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
233 HasComplexExpression = true;
234
Adrian Prantl0498baa2017-03-22 01:16:01 +0000235 // If the register can only be described by a complex expression (i.e.,
236 // multiple subregisters) it doesn't safely compose with another complex
237 // expression. For example, it is not possible to apply a DW_OP_deref
238 // operation to multiple DW_OP_pieces.
239 if (HasComplexExpression && DwarfRegs.size() > 1) {
240 DwarfRegs.clear();
Adrian Prantldd215022017-04-25 19:40:53 +0000241 LocationKind = Unknown;
Adrian Prantl0498baa2017-03-22 01:16:01 +0000242 return false;
243 }
244
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000245 // Handle simple register locations. If we are supposed to emit
246 // a call site parameter expression and if that expression is just a register
247 // location, emit it with addBReg and offset 0, because we should emit a DWARF
248 // expression representing a value, rather than a location.
249 if (!isMemoryLocation() && !HasComplexExpression && (!isParameterValue() ||
250 isEntryValue())) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000251 for (auto &Reg : DwarfRegs) {
252 if (Reg.DwarfRegNo >= 0)
253 addReg(Reg.DwarfRegNo, Reg.Comment);
254 addOpPiece(Reg.Size);
255 }
Djordje Todorovica0d45052019-06-27 13:52:34 +0000256
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000257 if (isEntryValue() && !isParameterValue() && DwarfVersion >= 4)
Djordje Todorovica0d45052019-06-27 13:52:34 +0000258 emitOp(dwarf::DW_OP_stack_value);
259
Adrian Prantl80e188d2017-03-22 01:15:57 +0000260 DwarfRegs.clear();
261 return true;
262 }
263
Adrian Prantl6825fb62017-04-18 01:21:53 +0000264 // Don't emit locations that cannot be expressed without DW_OP_stack_value.
Adrian Prantlada10482017-04-20 20:42:33 +0000265 if (DwarfVersion < 4)
Fangrui Song2e83b2e2018-10-19 06:12:02 +0000266 if (any_of(ExprCursor, [](DIExpression::ExprOperand Op) -> bool {
267 return Op.getOp() == dwarf::DW_OP_stack_value;
268 })) {
Adrian Prantlada10482017-04-20 20:42:33 +0000269 DwarfRegs.clear();
Adrian Prantldd215022017-04-25 19:40:53 +0000270 LocationKind = Unknown;
Adrian Prantlada10482017-04-20 20:42:33 +0000271 return false;
272 }
Adrian Prantl6825fb62017-04-18 01:21:53 +0000273
Adrian Prantl80e188d2017-03-22 01:15:57 +0000274 assert(DwarfRegs.size() == 1);
275 auto Reg = DwarfRegs[0];
Adrian Prantl6825fb62017-04-18 01:21:53 +0000276 bool FBReg = isFrameRegister(TRI, MachineReg);
277 int SignedOffset = 0;
Adrian Prantl80e188d2017-03-22 01:15:57 +0000278 assert(Reg.Size == 0 && "subregister has same size as superregister");
279
280 // Pattern-match combinations for which more efficient representations exist.
Florian Hahnc9c403c2017-06-13 16:54:44 +0000281 // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].
282 if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) {
283 SignedOffset = Op->getArg(0);
284 ExprCursor.take();
285 }
286
Florian Hahnffc498d2017-06-14 13:14:38 +0000287 // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]
288 // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset]
Adrian Prantl6825fb62017-04-18 01:21:53 +0000289 // If Reg is a subregister we need to mask it out before subtracting.
Florian Hahnffc498d2017-06-14 13:14:38 +0000290 if (Op && Op->getOp() == dwarf::DW_OP_constu) {
291 auto N = ExprCursor.peekNext();
292 if (N && (N->getOp() == dwarf::DW_OP_plus ||
293 (N->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) {
294 int Offset = Op->getArg(0);
295 SignedOffset = (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset;
296 ExprCursor.consume(2);
297 }
Adrian Prantl531641a2015-01-22 00:00:59 +0000298 }
Florian Hahnffc498d2017-06-14 13:14:38 +0000299
Adrian Prantl6825fb62017-04-18 01:21:53 +0000300 if (FBReg)
301 addFBReg(SignedOffset);
302 else
303 addBReg(Reg.DwarfRegNo, SignedOffset);
Adrian Prantl80e188d2017-03-22 01:15:57 +0000304 DwarfRegs.clear();
305 return true;
Adrian Prantl092d9482015-01-13 23:39:11 +0000306}
307
Djordje Todorovica0d45052019-06-27 13:52:34 +0000308void DwarfExpression::addEntryValueExpression(DIExpressionCursor &ExprCursor) {
309 auto Op = ExprCursor.take();
310 assert(Op && Op->getOp() == dwarf::DW_OP_entry_value);
311 assert(!isMemoryLocation() &&
312 "We don't support entry values of memory locations yet");
313
314 if (DwarfVersion >= 5)
315 emitOp(dwarf::DW_OP_entry_value);
316 else
317 emitOp(dwarf::DW_OP_GNU_entry_value);
318 emitUnsigned(Op->getArg(0));
319}
320
Adrian Prantl6825fb62017-04-18 01:21:53 +0000321/// Assuming a well-formed expression, match "DW_OP_deref* DW_OP_LLVM_fragment?".
322static bool isMemoryLocation(DIExpressionCursor ExprCursor) {
323 while (ExprCursor) {
324 auto Op = ExprCursor.take();
325 switch (Op->getOp()) {
326 case dwarf::DW_OP_deref:
327 case dwarf::DW_OP_LLVM_fragment:
328 break;
329 default:
330 return false;
331 }
332 }
333 return true;
334}
335
Adrian Prantla63b8e82017-03-16 17:42:45 +0000336void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
Adrian Prantl941fa752016-12-05 18:04:47 +0000337 unsigned FragmentOffsetInBits) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000338 // If we need to mask out a subregister, do it now, unless the next
339 // operation would emit an OpPiece anyway.
340 auto N = ExprCursor.peek();
341 if (SubRegisterSizeInBits && N && (N->getOp() != dwarf::DW_OP_LLVM_fragment))
342 maskSubRegister();
343
Markus Lavinb86ce212019-03-19 13:16:28 +0000344 Optional<DIExpression::ExprOperand> PrevConvertOp = None;
345
Adrian Prantl54286bd2016-11-02 16:12:20 +0000346 while (ExprCursor) {
347 auto Op = ExprCursor.take();
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000348 uint64_t OpNum = Op->getOp();
349
350 if (OpNum >= dwarf::DW_OP_reg0 && OpNum <= dwarf::DW_OP_reg31) {
351 emitOp(OpNum);
352 continue;
353 } else if (OpNum >= dwarf::DW_OP_breg0 && OpNum <= dwarf::DW_OP_breg31) {
354 addBReg(OpNum - dwarf::DW_OP_breg0, Op->getArg(0));
355 continue;
356 }
357
358 switch (OpNum) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000359 case dwarf::DW_OP_LLVM_fragment: {
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000360 unsigned SizeInBits = Op->getArg(1);
361 unsigned FragmentOffset = Op->getArg(0);
362 // The fragment offset must have already been adjusted by emitting an
363 // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base
364 // location.
365 assert(OffsetInBits >= FragmentOffset && "fragment offset not added?");
366
Adrian Prantl6825fb62017-04-18 01:21:53 +0000367 // If addMachineReg already emitted DW_OP_piece operations to represent
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000368 // a super-register by splicing together sub-registers, subtract the size
369 // of the pieces that was already emitted.
370 SizeInBits -= OffsetInBits - FragmentOffset;
371
Adrian Prantl6825fb62017-04-18 01:21:53 +0000372 // If addMachineReg requested a DW_OP_bit_piece to stencil out a
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000373 // sub-register that is smaller than the current fragment's size, use it.
374 if (SubRegisterSizeInBits)
375 SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000376
377 // Emit a DW_OP_stack_value for implicit location descriptions.
Petar Jovanovicff47d832019-05-23 10:37:13 +0000378 if (isImplicitLocation())
Adrian Prantl6825fb62017-04-18 01:21:53 +0000379 addStackValue();
380
381 // Emit the DW_OP_piece.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000382 addOpPiece(SizeInBits, SubRegisterOffsetInBits);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000383 setSubRegisterPiece(0, 0);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000384 // Reset the location description kind.
385 LocationKind = Unknown;
386 return;
Adrian Prantl092d9482015-01-13 23:39:11 +0000387 }
Florian Hahnc9c403c2017-06-13 16:54:44 +0000388 case dwarf::DW_OP_plus_uconst:
Petar Jovanovicff47d832019-05-23 10:37:13 +0000389 assert(!isRegisterLocation());
Adrian Prantla63b8e82017-03-16 17:42:45 +0000390 emitOp(dwarf::DW_OP_plus_uconst);
391 emitUnsigned(Op->getArg(0));
Adrian Prantl092d9482015-01-13 23:39:11 +0000392 break;
Florian Hahnffc498d2017-06-14 13:14:38 +0000393 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000394 case dwarf::DW_OP_minus:
Strahinja Petrovic29202f62017-09-21 10:04:02 +0000395 case dwarf::DW_OP_mul:
Vedant Kumar4011c262018-02-13 01:09:52 +0000396 case dwarf::DW_OP_div:
397 case dwarf::DW_OP_mod:
Vedant Kumar04386d82018-02-09 19:19:55 +0000398 case dwarf::DW_OP_or:
Petar Jovanovic17689572018-02-14 13:10:35 +0000399 case dwarf::DW_OP_and:
Vedant Kumar96b7dc02018-02-13 01:09:46 +0000400 case dwarf::DW_OP_xor:
Vedant Kumar31ec3562018-02-13 01:09:49 +0000401 case dwarf::DW_OP_shl:
402 case dwarf::DW_OP_shr:
403 case dwarf::DW_OP_shra:
Vedant Kumar6379a622018-07-06 17:32:39 +0000404 case dwarf::DW_OP_lit0:
405 case dwarf::DW_OP_not:
406 case dwarf::DW_OP_dup:
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000407 emitOp(OpNum);
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000408 break;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000409 case dwarf::DW_OP_deref:
Petar Jovanovicff47d832019-05-23 10:37:13 +0000410 assert(!isRegisterLocation());
411 if (!isMemoryLocation() && ::isMemoryLocation(ExprCursor))
Adrian Prantl6825fb62017-04-18 01:21:53 +0000412 // Turning this into a memory location description makes the deref
413 // implicit.
414 LocationKind = Memory;
415 else
416 emitOp(dwarf::DW_OP_deref);
Adrian Prantl092d9482015-01-13 23:39:11 +0000417 break;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000418 case dwarf::DW_OP_constu:
Petar Jovanovicff47d832019-05-23 10:37:13 +0000419 assert(!isRegisterLocation());
Jonas Devlieghere965b5982018-09-05 10:18:36 +0000420 emitConstu(Op->getArg(0));
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000421 break;
Markus Lavinb86ce212019-03-19 13:16:28 +0000422 case dwarf::DW_OP_LLVM_convert: {
423 unsigned BitSize = Op->getArg(0);
424 dwarf::TypeKind Encoding = static_cast<dwarf::TypeKind>(Op->getArg(1));
425 if (DwarfVersion >= 5) {
426 emitOp(dwarf::DW_OP_convert);
427 // Reuse the base_type if we already have one in this CU otherwise we
428 // create a new one.
429 unsigned I = 0, E = CU.ExprRefedBaseTypes.size();
430 for (; I != E; ++I)
431 if (CU.ExprRefedBaseTypes[I].BitSize == BitSize &&
432 CU.ExprRefedBaseTypes[I].Encoding == Encoding)
433 break;
434
435 if (I == E)
436 CU.ExprRefedBaseTypes.emplace_back(BitSize, Encoding);
437
438 // If targeting a location-list; simply emit the index into the raw
439 // byte stream as ULEB128, DwarfDebug::emitDebugLocEntry has been
440 // fitted with means to extract it later.
441 // If targeting a inlined DW_AT_location; insert a DIEBaseTypeRef
442 // (containing the index and a resolve mechanism during emit) into the
443 // DIE value list.
444 emitBaseTypeRef(I);
445 } else {
446 if (PrevConvertOp && PrevConvertOp->getArg(0) < BitSize) {
447 if (Encoding == dwarf::DW_ATE_signed)
448 emitLegacySExt(PrevConvertOp->getArg(0));
449 else if (Encoding == dwarf::DW_ATE_unsigned)
450 emitLegacyZExt(PrevConvertOp->getArg(0));
451 PrevConvertOp = None;
452 } else {
453 PrevConvertOp = Op;
454 }
455 }
456 break;
457 }
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000458 case dwarf::DW_OP_stack_value:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000459 LocationKind = Implicit;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000460 break;
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000461 case dwarf::DW_OP_swap:
Petar Jovanovicff47d832019-05-23 10:37:13 +0000462 assert(!isRegisterLocation());
Adrian Prantla63b8e82017-03-16 17:42:45 +0000463 emitOp(dwarf::DW_OP_swap);
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000464 break;
465 case dwarf::DW_OP_xderef:
Petar Jovanovicff47d832019-05-23 10:37:13 +0000466 assert(!isRegisterLocation());
Adrian Prantla63b8e82017-03-16 17:42:45 +0000467 emitOp(dwarf::DW_OP_xderef);
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000468 break;
Markus Lavina475da32019-04-30 07:58:57 +0000469 case dwarf::DW_OP_deref_size:
470 emitOp(dwarf::DW_OP_deref_size);
471 emitData1(Op->getArg(0));
472 break;
Peter Collingbournefb9ce102019-06-17 23:39:41 +0000473 case dwarf::DW_OP_LLVM_tag_offset:
474 TagOffset = Op->getArg(0);
475 break;
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000476 case dwarf::DW_OP_regx:
477 emitOp(dwarf::DW_OP_regx);
478 emitUnsigned(Op->getArg(0));
479 break;
480 case dwarf::DW_OP_bregx:
481 emitOp(dwarf::DW_OP_bregx);
482 emitUnsigned(Op->getArg(0));
483 emitSigned(Op->getArg(1));
484 break;
Adrian Prantl092d9482015-01-13 23:39:11 +0000485 default:
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000486 llvm_unreachable("unhandled opcode found in expression");
Adrian Prantl092d9482015-01-13 23:39:11 +0000487 }
488 }
Adrian Prantl6825fb62017-04-18 01:21:53 +0000489
Djordje Todorovicb9973f82019-07-31 16:51:28 +0000490 if (isImplicitLocation() && !isParameterValue())
Adrian Prantl6825fb62017-04-18 01:21:53 +0000491 // Turn this into an implicit location description.
492 addStackValue();
Adrian Prantl092d9482015-01-13 23:39:11 +0000493}
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000494
Adrian Prantla63b8e82017-03-16 17:42:45 +0000495/// add masking operations to stencil out a subregister.
Adrian Prantl981f03e2017-03-16 17:14:56 +0000496void DwarfExpression::maskSubRegister() {
497 assert(SubRegisterSizeInBits && "no subregister was registered");
498 if (SubRegisterOffsetInBits > 0)
Adrian Prantla63b8e82017-03-16 17:42:45 +0000499 addShr(SubRegisterOffsetInBits);
Adrian Prantldc855222017-03-16 18:06:04 +0000500 uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL;
Adrian Prantla63b8e82017-03-16 17:42:45 +0000501 addAnd(Mask);
Adrian Prantl981f03e2017-03-16 17:14:56 +0000502}
503
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000504void DwarfExpression::finalize() {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000505 assert(DwarfRegs.size() == 0 && "dwarf registers not emitted");
Adrian Prantl981f03e2017-03-16 17:14:56 +0000506 // Emit any outstanding DW_OP_piece operations to mask out subregisters.
507 if (SubRegisterSizeInBits == 0)
508 return;
509 // Don't emit a DW_OP_piece for a subregister at offset 0.
510 if (SubRegisterOffsetInBits == 0)
511 return;
Adrian Prantla63b8e82017-03-16 17:42:45 +0000512 addOpPiece(SubRegisterSizeInBits, SubRegisterOffsetInBits);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000513}
514
515void DwarfExpression::addFragmentOffset(const DIExpression *Expr) {
516 if (!Expr || !Expr->isFragment())
517 return;
518
Adrian Prantl49797ca2016-12-22 05:27:12 +0000519 uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000520 assert(FragmentOffset >= OffsetInBits &&
521 "overlapping or duplicate fragments");
522 if (FragmentOffset > OffsetInBits)
Adrian Prantla63b8e82017-03-16 17:42:45 +0000523 addOpPiece(FragmentOffset - OffsetInBits);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000524 OffsetInBits = FragmentOffset;
525}
Markus Lavinb86ce212019-03-19 13:16:28 +0000526
527void DwarfExpression::emitLegacySExt(unsigned FromBits) {
528 // (((X >> (FromBits - 1)) * (~0)) << FromBits) | X
529 emitOp(dwarf::DW_OP_dup);
530 emitOp(dwarf::DW_OP_constu);
531 emitUnsigned(FromBits - 1);
532 emitOp(dwarf::DW_OP_shr);
533 emitOp(dwarf::DW_OP_lit0);
534 emitOp(dwarf::DW_OP_not);
535 emitOp(dwarf::DW_OP_mul);
536 emitOp(dwarf::DW_OP_constu);
537 emitUnsigned(FromBits);
538 emitOp(dwarf::DW_OP_shl);
539 emitOp(dwarf::DW_OP_or);
540}
541
542void DwarfExpression::emitLegacyZExt(unsigned FromBits) {
543 // (X & (1 << FromBits - 1))
544 emitOp(dwarf::DW_OP_constu);
545 emitUnsigned((1ULL << FromBits) - 1);
546 emitOp(dwarf::DW_OP_and);
547}