blob: af51d27663449cb2dab3f4da3b0a26c83e3ad0e8 [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//
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"
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"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetRegisterInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000019#include "llvm/IR/DebugInfoMetadata.h"
20#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000021#include <algorithm>
22#include <cassert>
23#include <cstdint>
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000024
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000025using namespace llvm;
26
Jonas Devlieghere965b5982018-09-05 10:18:36 +000027void DwarfExpression::emitConstu(uint64_t Value) {
28 if (Value < 32)
29 emitOp(dwarf::DW_OP_lit0 + Value);
30 else if (Value == std::numeric_limits<uint64_t>::max()) {
31 // Only do this for 64-bit values as the DWARF expression stack uses
32 // target-address-size values.
33 emitOp(dwarf::DW_OP_lit0);
34 emitOp(dwarf::DW_OP_not);
35 } else {
36 emitOp(dwarf::DW_OP_constu);
37 emitUnsigned(Value);
38 }
39}
40
Adrian Prantla63b8e82017-03-16 17:42:45 +000041void DwarfExpression::addReg(int DwarfReg, const char *Comment) {
Adrian Prantl6825fb62017-04-18 01:21:53 +000042 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
43 assert((LocationKind == Unknown || LocationKind == Register) &&
44 "location description already locked down");
45 LocationKind = Register;
46 if (DwarfReg < 32) {
47 emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000048 } else {
Adrian Prantla63b8e82017-03-16 17:42:45 +000049 emitOp(dwarf::DW_OP_regx, Comment);
50 emitUnsigned(DwarfReg);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000051 }
52}
53
Adrian Prantla2719882017-03-22 17:19:55 +000054void DwarfExpression::addBReg(int DwarfReg, int Offset) {
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000055 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
Adrian Prantl6825fb62017-04-18 01:21:53 +000056 assert(LocationKind != Register && "location description already locked down");
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000057 if (DwarfReg < 32) {
Adrian Prantla63b8e82017-03-16 17:42:45 +000058 emitOp(dwarf::DW_OP_breg0 + DwarfReg);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000059 } else {
Adrian Prantla63b8e82017-03-16 17:42:45 +000060 emitOp(dwarf::DW_OP_bregx);
61 emitUnsigned(DwarfReg);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000062 }
Adrian Prantla63b8e82017-03-16 17:42:45 +000063 emitSigned(Offset);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000064}
65
Adrian Prantl80e188d2017-03-22 01:15:57 +000066void DwarfExpression::addFBReg(int Offset) {
67 emitOp(dwarf::DW_OP_fbreg);
68 emitSigned(Offset);
69}
70
Adrian Prantla63b8e82017-03-16 17:42:45 +000071void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000072 if (!SizeInBits)
73 return;
74
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000075 const unsigned SizeOfByte = 8;
76 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
Adrian Prantla63b8e82017-03-16 17:42:45 +000077 emitOp(dwarf::DW_OP_bit_piece);
78 emitUnsigned(SizeInBits);
79 emitUnsigned(OffsetInBits);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000080 } else {
Adrian Prantla63b8e82017-03-16 17:42:45 +000081 emitOp(dwarf::DW_OP_piece);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000082 unsigned ByteSize = SizeInBits / SizeOfByte;
Adrian Prantla63b8e82017-03-16 17:42:45 +000083 emitUnsigned(ByteSize);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000084 }
Adrian Prantl8fafb8d2016-12-09 20:43:40 +000085 this->OffsetInBits += SizeInBits;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000086}
87
Adrian Prantla63b8e82017-03-16 17:42:45 +000088void DwarfExpression::addShr(unsigned ShiftBy) {
Jonas Devlieghere965b5982018-09-05 10:18:36 +000089 emitConstu(ShiftBy);
Adrian Prantla63b8e82017-03-16 17:42:45 +000090 emitOp(dwarf::DW_OP_shr);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000091}
92
Adrian Prantla63b8e82017-03-16 17:42:45 +000093void DwarfExpression::addAnd(unsigned Mask) {
Jonas Devlieghere965b5982018-09-05 10:18:36 +000094 emitConstu(Mask);
Adrian Prantla63b8e82017-03-16 17:42:45 +000095 emitOp(dwarf::DW_OP_and);
Adrian Prantl981f03e2017-03-16 17:14:56 +000096}
97
Adrian Prantla63b8e82017-03-16 17:42:45 +000098bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
Adrian Prantl5542da42016-12-22 06:10:41 +000099 unsigned MachineReg, unsigned MaxSize) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000100 if (!TRI.isPhysicalRegister(MachineReg)) {
101 if (isFrameRegister(TRI, MachineReg)) {
102 DwarfRegs.push_back({-1, 0, nullptr});
103 return true;
104 }
Adrian Prantl40cb8192015-01-25 19:04:08 +0000105 return false;
Adrian Prantl80e188d2017-03-22 01:15:57 +0000106 }
Adrian Prantl40cb8192015-01-25 19:04:08 +0000107
Adrian Prantl92da14b2015-03-02 22:02:33 +0000108 int Reg = TRI.getDwarfRegNum(MachineReg, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000109
110 // If this is a valid register number, emit it.
111 if (Reg >= 0) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000112 DwarfRegs.push_back({Reg, 0, nullptr});
Adrian Prantlad768c32015-01-14 01:01:28 +0000113 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000114 }
115
116 // Walk up the super-register chain until we find a valid number.
Adrian Prantl941fa752016-12-05 18:04:47 +0000117 // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0.
Adrian Prantl92da14b2015-03-02 22:02:33 +0000118 for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
119 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000120 if (Reg >= 0) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000121 unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg);
122 unsigned Size = TRI.getSubRegIdxSize(Idx);
123 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
Adrian Prantl80e188d2017-03-22 01:15:57 +0000124 DwarfRegs.push_back({Reg, 0, "super-register"});
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000125 // Use a DW_OP_bit_piece to describe the sub-register.
126 setSubRegisterPiece(Size, RegOffset);
Adrian Prantlad768c32015-01-14 01:01:28 +0000127 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000128 }
129 }
130
131 // Otherwise, attempt to find a covering set of sub-register numbers.
132 // For example, Q0 on ARM is a composition of D0+D1.
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000133 unsigned CurPos = 0;
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000134 // The size of the register in bits.
135 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(MachineReg);
136 unsigned RegSize = TRI.getRegSizeInBits(*RC);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000137 // Keep track of the bits in the register we already emitted, so we
Adrian Prantl984251c2018-02-13 19:54:00 +0000138 // can avoid emitting redundant aliasing subregs. Because this is
139 // just doing a greedy scan of all subregisters, it is possible that
140 // this doesn't find a combination of subregisters that fully cover
141 // the register (even though one may exist).
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000142 SmallBitVector Coverage(RegSize, false);
Adrian Prantl92da14b2015-03-02 22:02:33 +0000143 for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) {
144 unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR);
145 unsigned Size = TRI.getSubRegIdxSize(Idx);
146 unsigned Offset = TRI.getSubRegIdxOffset(Idx);
147 Reg = TRI.getDwarfRegNum(*SR, false);
Adrian Prantl3a3ba772017-10-10 20:33:43 +0000148 if (Reg < 0)
149 continue;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000150
151 // Intersection between the bits we already emitted and the bits
152 // covered by this subregister.
Adrian Prantl4cae1082017-08-28 23:07:43 +0000153 SmallBitVector CurSubReg(RegSize, false);
154 CurSubReg.set(Offset, Offset + Size);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000155
156 // If this sub-register has a DWARF number and we haven't covered
157 // its range, emit a DWARF piece for it.
Adrian Prantl3a3ba772017-10-10 20:33:43 +0000158 if (CurSubReg.test(Coverage)) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000159 // Emit a piece for any gap in the coverage.
160 if (Offset > CurPos)
Adrian Prantl984251c2018-02-13 19:54:00 +0000161 DwarfRegs.push_back({-1, Offset - CurPos, "no DWARF register encoding"});
Adrian Prantl80e188d2017-03-22 01:15:57 +0000162 DwarfRegs.push_back(
163 {Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"});
Adrian Prantl5542da42016-12-22 06:10:41 +0000164 if (Offset >= MaxSize)
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000165 break;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000166
167 // Mark it as emitted.
168 Coverage.set(Offset, Offset + Size);
Adrian Prantl80e188d2017-03-22 01:15:57 +0000169 CurPos = Offset + Size;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000170 }
171 }
Adrian Prantl984251c2018-02-13 19:54:00 +0000172 // Failed to find any DWARF encoding.
173 if (CurPos == 0)
174 return false;
175 // Found a partial or complete DWARF encoding.
176 if (CurPos < RegSize)
177 DwarfRegs.push_back({-1, RegSize - CurPos, "no DWARF register encoding"});
178 return true;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000179}
Adrian Prantl66f25952015-01-13 00:04:06 +0000180
Adrian Prantla63b8e82017-03-16 17:42:45 +0000181void DwarfExpression::addStackValue() {
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000182 if (DwarfVersion >= 4)
Adrian Prantla63b8e82017-03-16 17:42:45 +0000183 emitOp(dwarf::DW_OP_stack_value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000184}
185
Adrian Prantla63b8e82017-03-16 17:42:45 +0000186void DwarfExpression::addSignedConstant(int64_t Value) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000187 assert(LocationKind == Implicit || LocationKind == Unknown);
188 LocationKind = Implicit;
Adrian Prantla63b8e82017-03-16 17:42:45 +0000189 emitOp(dwarf::DW_OP_consts);
190 emitSigned(Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000191}
192
Adrian Prantla63b8e82017-03-16 17:42:45 +0000193void DwarfExpression::addUnsignedConstant(uint64_t Value) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000194 assert(LocationKind == Implicit || LocationKind == Unknown);
195 LocationKind = Implicit;
Jonas Devlieghere965b5982018-09-05 10:18:36 +0000196 emitConstu(Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000197}
198
Adrian Prantla63b8e82017-03-16 17:42:45 +0000199void DwarfExpression::addUnsignedConstant(const APInt &Value) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000200 assert(LocationKind == Implicit || LocationKind == Unknown);
201 LocationKind = Implicit;
202
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000203 unsigned Size = Value.getBitWidth();
204 const uint64_t *Data = Value.getRawData();
205
206 // Chop it up into 64-bit pieces, because that's the maximum that
Adrian Prantla63b8e82017-03-16 17:42:45 +0000207 // addUnsignedConstant takes.
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000208 unsigned Offset = 0;
209 while (Offset < Size) {
Adrian Prantla63b8e82017-03-16 17:42:45 +0000210 addUnsignedConstant(*Data++);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000211 if (Offset == 0 && Size <= 64)
212 break;
Adrian Prantl6825fb62017-04-18 01:21:53 +0000213 addStackValue();
214 addOpPiece(std::min(Size - Offset, 64u), Offset);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000215 Offset += 64;
216 }
Adrian Prantl66f25952015-01-13 00:04:06 +0000217}
Adrian Prantl092d9482015-01-13 23:39:11 +0000218
Adrian Prantlc12cee32017-04-19 23:42:25 +0000219bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
Adrian Prantl54286bd2016-11-02 16:12:20 +0000220 DIExpressionCursor &ExprCursor,
Adrian Prantlc12cee32017-04-19 23:42:25 +0000221 unsigned MachineReg,
Adrian Prantl941fa752016-12-05 18:04:47 +0000222 unsigned FragmentOffsetInBits) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000223 auto Fragment = ExprCursor.getFragmentInfo();
Adrian Prantldd215022017-04-25 19:40:53 +0000224 if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) {
225 LocationKind = Unknown;
Adrian Prantl80e188d2017-03-22 01:15:57 +0000226 return false;
Adrian Prantldd215022017-04-25 19:40:53 +0000227 }
Adrian Prantl531641a2015-01-22 00:00:59 +0000228
Adrian Prantl80e188d2017-03-22 01:15:57 +0000229 bool HasComplexExpression = false;
Adrian Prantl4dc03242017-03-21 17:14:30 +0000230 auto Op = ExprCursor.peek();
Adrian Prantl80e188d2017-03-22 01:15:57 +0000231 if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
232 HasComplexExpression = true;
233
Adrian Prantl0498baa2017-03-22 01:16:01 +0000234 // If the register can only be described by a complex expression (i.e.,
235 // multiple subregisters) it doesn't safely compose with another complex
236 // expression. For example, it is not possible to apply a DW_OP_deref
237 // operation to multiple DW_OP_pieces.
238 if (HasComplexExpression && DwarfRegs.size() > 1) {
239 DwarfRegs.clear();
Adrian Prantldd215022017-04-25 19:40:53 +0000240 LocationKind = Unknown;
Adrian Prantl0498baa2017-03-22 01:16:01 +0000241 return false;
242 }
243
Adrian Prantl80e188d2017-03-22 01:15:57 +0000244 // Handle simple register locations.
Adrian Prantl6825fb62017-04-18 01:21:53 +0000245 if (LocationKind != Memory && !HasComplexExpression) {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000246 for (auto &Reg : DwarfRegs) {
247 if (Reg.DwarfRegNo >= 0)
248 addReg(Reg.DwarfRegNo, Reg.Comment);
249 addOpPiece(Reg.Size);
250 }
251 DwarfRegs.clear();
252 return true;
253 }
254
Adrian Prantl6825fb62017-04-18 01:21:53 +0000255 // Don't emit locations that cannot be expressed without DW_OP_stack_value.
Adrian Prantlada10482017-04-20 20:42:33 +0000256 if (DwarfVersion < 4)
257 if (std::any_of(ExprCursor.begin(), ExprCursor.end(),
258 [](DIExpression::ExprOperand Op) -> bool {
259 return Op.getOp() == dwarf::DW_OP_stack_value;
260 })) {
261 DwarfRegs.clear();
Adrian Prantldd215022017-04-25 19:40:53 +0000262 LocationKind = Unknown;
Adrian Prantlada10482017-04-20 20:42:33 +0000263 return false;
264 }
Adrian Prantl6825fb62017-04-18 01:21:53 +0000265
Adrian Prantl80e188d2017-03-22 01:15:57 +0000266 assert(DwarfRegs.size() == 1);
267 auto Reg = DwarfRegs[0];
Adrian Prantl6825fb62017-04-18 01:21:53 +0000268 bool FBReg = isFrameRegister(TRI, MachineReg);
269 int SignedOffset = 0;
Adrian Prantl80e188d2017-03-22 01:15:57 +0000270 assert(Reg.Size == 0 && "subregister has same size as superregister");
271
272 // Pattern-match combinations for which more efficient representations exist.
Florian Hahnc9c403c2017-06-13 16:54:44 +0000273 // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].
274 if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) {
275 SignedOffset = Op->getArg(0);
276 ExprCursor.take();
277 }
278
Florian Hahnffc498d2017-06-14 13:14:38 +0000279 // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]
280 // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset]
Adrian Prantl6825fb62017-04-18 01:21:53 +0000281 // If Reg is a subregister we need to mask it out before subtracting.
Florian Hahnffc498d2017-06-14 13:14:38 +0000282 if (Op && Op->getOp() == dwarf::DW_OP_constu) {
283 auto N = ExprCursor.peekNext();
284 if (N && (N->getOp() == dwarf::DW_OP_plus ||
285 (N->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) {
286 int Offset = Op->getArg(0);
287 SignedOffset = (N->getOp() == dwarf::DW_OP_minus) ? -Offset : Offset;
288 ExprCursor.consume(2);
289 }
Adrian Prantl531641a2015-01-22 00:00:59 +0000290 }
Florian Hahnffc498d2017-06-14 13:14:38 +0000291
Adrian Prantl6825fb62017-04-18 01:21:53 +0000292 if (FBReg)
293 addFBReg(SignedOffset);
294 else
295 addBReg(Reg.DwarfRegNo, SignedOffset);
Adrian Prantl80e188d2017-03-22 01:15:57 +0000296 DwarfRegs.clear();
297 return true;
Adrian Prantl092d9482015-01-13 23:39:11 +0000298}
299
Adrian Prantl6825fb62017-04-18 01:21:53 +0000300/// Assuming a well-formed expression, match "DW_OP_deref* DW_OP_LLVM_fragment?".
301static bool isMemoryLocation(DIExpressionCursor ExprCursor) {
302 while (ExprCursor) {
303 auto Op = ExprCursor.take();
304 switch (Op->getOp()) {
305 case dwarf::DW_OP_deref:
306 case dwarf::DW_OP_LLVM_fragment:
307 break;
308 default:
309 return false;
310 }
311 }
312 return true;
313}
314
Adrian Prantla63b8e82017-03-16 17:42:45 +0000315void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor,
Adrian Prantl941fa752016-12-05 18:04:47 +0000316 unsigned FragmentOffsetInBits) {
Adrian Prantl6825fb62017-04-18 01:21:53 +0000317 // If we need to mask out a subregister, do it now, unless the next
318 // operation would emit an OpPiece anyway.
319 auto N = ExprCursor.peek();
320 if (SubRegisterSizeInBits && N && (N->getOp() != dwarf::DW_OP_LLVM_fragment))
321 maskSubRegister();
322
Adrian Prantl54286bd2016-11-02 16:12:20 +0000323 while (ExprCursor) {
324 auto Op = ExprCursor.take();
325 switch (Op->getOp()) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000326 case dwarf::DW_OP_LLVM_fragment: {
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000327 unsigned SizeInBits = Op->getArg(1);
328 unsigned FragmentOffset = Op->getArg(0);
329 // The fragment offset must have already been adjusted by emitting an
330 // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base
331 // location.
332 assert(OffsetInBits >= FragmentOffset && "fragment offset not added?");
333
Adrian Prantl6825fb62017-04-18 01:21:53 +0000334 // If addMachineReg already emitted DW_OP_piece operations to represent
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000335 // a super-register by splicing together sub-registers, subtract the size
336 // of the pieces that was already emitted.
337 SizeInBits -= OffsetInBits - FragmentOffset;
338
Adrian Prantl6825fb62017-04-18 01:21:53 +0000339 // If addMachineReg requested a DW_OP_bit_piece to stencil out a
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000340 // sub-register that is smaller than the current fragment's size, use it.
341 if (SubRegisterSizeInBits)
342 SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000343
344 // Emit a DW_OP_stack_value for implicit location descriptions.
345 if (LocationKind == Implicit)
346 addStackValue();
347
348 // Emit the DW_OP_piece.
Adrian Prantla63b8e82017-03-16 17:42:45 +0000349 addOpPiece(SizeInBits, SubRegisterOffsetInBits);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000350 setSubRegisterPiece(0, 0);
Adrian Prantl6825fb62017-04-18 01:21:53 +0000351 // Reset the location description kind.
352 LocationKind = Unknown;
353 return;
Adrian Prantl092d9482015-01-13 23:39:11 +0000354 }
Florian Hahnc9c403c2017-06-13 16:54:44 +0000355 case dwarf::DW_OP_plus_uconst:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000356 assert(LocationKind != Register);
Adrian Prantla63b8e82017-03-16 17:42:45 +0000357 emitOp(dwarf::DW_OP_plus_uconst);
358 emitUnsigned(Op->getArg(0));
Adrian Prantl092d9482015-01-13 23:39:11 +0000359 break;
Florian Hahnffc498d2017-06-14 13:14:38 +0000360 case dwarf::DW_OP_plus:
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000361 case dwarf::DW_OP_minus:
Strahinja Petrovic29202f62017-09-21 10:04:02 +0000362 case dwarf::DW_OP_mul:
Vedant Kumar4011c262018-02-13 01:09:52 +0000363 case dwarf::DW_OP_div:
364 case dwarf::DW_OP_mod:
Vedant Kumar04386d82018-02-09 19:19:55 +0000365 case dwarf::DW_OP_or:
Petar Jovanovic17689572018-02-14 13:10:35 +0000366 case dwarf::DW_OP_and:
Vedant Kumar96b7dc02018-02-13 01:09:46 +0000367 case dwarf::DW_OP_xor:
Vedant Kumar31ec3562018-02-13 01:09:49 +0000368 case dwarf::DW_OP_shl:
369 case dwarf::DW_OP_shr:
370 case dwarf::DW_OP_shra:
Vedant Kumar6379a622018-07-06 17:32:39 +0000371 case dwarf::DW_OP_lit0:
372 case dwarf::DW_OP_not:
373 case dwarf::DW_OP_dup:
Florian Hahnffc498d2017-06-14 13:14:38 +0000374 emitOp(Op->getOp());
Evgeniy Stepanovf6081112015-09-30 19:55:43 +0000375 break;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000376 case dwarf::DW_OP_deref:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000377 assert(LocationKind != Register);
Adrian Prantl4b542c62018-04-27 22:05:31 +0000378 if (LocationKind != Memory && ::isMemoryLocation(ExprCursor))
Adrian Prantl6825fb62017-04-18 01:21:53 +0000379 // Turning this into a memory location description makes the deref
380 // implicit.
381 LocationKind = Memory;
382 else
383 emitOp(dwarf::DW_OP_deref);
Adrian Prantl092d9482015-01-13 23:39:11 +0000384 break;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000385 case dwarf::DW_OP_constu:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000386 assert(LocationKind != Register);
Jonas Devlieghere965b5982018-09-05 10:18:36 +0000387 emitConstu(Op->getArg(0));
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000388 break;
389 case dwarf::DW_OP_stack_value:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000390 LocationKind = Implicit;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000391 break;
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000392 case dwarf::DW_OP_swap:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000393 assert(LocationKind != Register);
Adrian Prantla63b8e82017-03-16 17:42:45 +0000394 emitOp(dwarf::DW_OP_swap);
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000395 break;
396 case dwarf::DW_OP_xderef:
Adrian Prantl6825fb62017-04-18 01:21:53 +0000397 assert(LocationKind != Register);
Adrian Prantla63b8e82017-03-16 17:42:45 +0000398 emitOp(dwarf::DW_OP_xderef);
Konstantin Zhuravlyovf9b41cd2017-03-08 00:28:57 +0000399 break;
Adrian Prantl092d9482015-01-13 23:39:11 +0000400 default:
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000401 llvm_unreachable("unhandled opcode found in expression");
Adrian Prantl092d9482015-01-13 23:39:11 +0000402 }
403 }
Adrian Prantl6825fb62017-04-18 01:21:53 +0000404
405 if (LocationKind == Implicit)
406 // Turn this into an implicit location description.
407 addStackValue();
Adrian Prantl092d9482015-01-13 23:39:11 +0000408}
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000409
Adrian Prantla63b8e82017-03-16 17:42:45 +0000410/// add masking operations to stencil out a subregister.
Adrian Prantl981f03e2017-03-16 17:14:56 +0000411void DwarfExpression::maskSubRegister() {
412 assert(SubRegisterSizeInBits && "no subregister was registered");
413 if (SubRegisterOffsetInBits > 0)
Adrian Prantla63b8e82017-03-16 17:42:45 +0000414 addShr(SubRegisterOffsetInBits);
Adrian Prantldc855222017-03-16 18:06:04 +0000415 uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL;
Adrian Prantla63b8e82017-03-16 17:42:45 +0000416 addAnd(Mask);
Adrian Prantl981f03e2017-03-16 17:14:56 +0000417}
418
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000419void DwarfExpression::finalize() {
Adrian Prantl80e188d2017-03-22 01:15:57 +0000420 assert(DwarfRegs.size() == 0 && "dwarf registers not emitted");
Adrian Prantl981f03e2017-03-16 17:14:56 +0000421 // Emit any outstanding DW_OP_piece operations to mask out subregisters.
422 if (SubRegisterSizeInBits == 0)
423 return;
424 // Don't emit a DW_OP_piece for a subregister at offset 0.
425 if (SubRegisterOffsetInBits == 0)
426 return;
Adrian Prantla63b8e82017-03-16 17:42:45 +0000427 addOpPiece(SubRegisterSizeInBits, SubRegisterOffsetInBits);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000428}
429
430void DwarfExpression::addFragmentOffset(const DIExpression *Expr) {
431 if (!Expr || !Expr->isFragment())
432 return;
433
Adrian Prantl49797ca2016-12-22 05:27:12 +0000434 uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000435 assert(FragmentOffset >= OffsetInBits &&
436 "overlapping or duplicate fragments");
437 if (FragmentOffset > OffsetInBits)
Adrian Prantla63b8e82017-03-16 17:42:45 +0000438 addOpPiece(FragmentOffset - OffsetInBits);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +0000439 OffsetInBits = FragmentOffset;
440}