1//===- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework -----------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing dwarf debug info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DwarfExpression.h"
14#include "DwarfCompileUnit.h"
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/SmallBitVector.h"
17#include "llvm/BinaryFormat/Dwarf.h"
18#include "llvm/CodeGen/Register.h"
19#include "llvm/CodeGen/TargetRegisterInfo.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/Support/ErrorHandling.h"
23#include <algorithm>
24
25using namespace llvm;
26
27#define DEBUG_TYPE "dwarfdebug"
28
29void DwarfExpression::emitConstu(uint64_t Value) {
30 if (Value < 32)
31 emitOp(Op: dwarf::DW_OP_lit0 + Value);
32 else if (Value == std::numeric_limits<uint64_t>::max()) {
33 // Only do this for 64-bit values as the DWARF expression stack uses
34 // target-address-size values.
35 emitOp(Op: dwarf::DW_OP_lit0);
36 emitOp(Op: dwarf::DW_OP_not);
37 } else {
38 emitOp(Op: dwarf::DW_OP_constu);
39 emitUnsigned(Value);
40 }
41}
42
43void DwarfExpression::addReg(int64_t DwarfReg, const char *Comment) {
44 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
45 assert((isUnknownLocation() || isRegisterLocation()) &&
46 "location description already locked down");
47 LocationKind = Register;
48 if (DwarfReg < 32) {
49 emitOp(Op: dwarf::DW_OP_reg0 + DwarfReg, Comment);
50 } else {
51 emitOp(Op: dwarf::DW_OP_regx, Comment);
52 emitUnsigned(Value: DwarfReg);
53 }
54}
55
56void DwarfExpression::addBReg(int64_t DwarfReg, int64_t Offset) {
57 assert(DwarfReg >= 0 && "invalid negative dwarf register number");
58 assert(!isRegisterLocation() && "location description already locked down");
59 if (DwarfReg < 32) {
60 emitOp(Op: dwarf::DW_OP_breg0 + DwarfReg);
61 } else {
62 emitOp(Op: dwarf::DW_OP_bregx);
63 emitUnsigned(Value: DwarfReg);
64 }
65 emitSigned(Value: Offset);
66}
67
68void DwarfExpression::addFBReg(int64_t Offset) {
69 emitOp(Op: dwarf::DW_OP_fbreg);
70 emitSigned(Value: Offset);
71}
72
73void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) {
74 if (!SizeInBits)
75 return;
76
77 const unsigned SizeOfByte = 8;
78 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) {
79 emitOp(Op: dwarf::DW_OP_bit_piece);
80 emitUnsigned(Value: SizeInBits);
81 emitUnsigned(Value: OffsetInBits);
82 } else {
83 emitOp(Op: dwarf::DW_OP_piece);
84 unsigned ByteSize = SizeInBits / SizeOfByte;
85 emitUnsigned(Value: ByteSize);
86 }
87 this->OffsetInBits += SizeInBits;
88}
89
90void DwarfExpression::addShr(unsigned ShiftBy) {
91 emitConstu(Value: ShiftBy);
92 emitOp(Op: dwarf::DW_OP_shr);
93}
94
95void DwarfExpression::addAnd(unsigned Mask) {
96 emitConstu(Value: Mask);
97 emitOp(Op: dwarf::DW_OP_and);
98}
99
100bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI,
101 llvm::Register MachineReg,
102 unsigned MaxSize) {
103 if (!MachineReg.isPhysical()) {
104 if (isFrameRegister(TRI, MachineReg)) {
105 DwarfRegs.push_back(Elt: Register::createRegister(RegNo: -1, Comment: nullptr));
106 return true;
107 }
108 // Try getting dwarf register for targets that use virtual registers.
109 int64_t Reg = TRI.getDwarfRegNumForVirtReg(RegNum: MachineReg, isEH: false);
110 if (Reg > 0) {
111 DwarfRegs.push_back(Elt: Register::createRegister(RegNo: Reg, Comment: nullptr));
112 return true;
113 }
114 return false;
115 }
116
117 int64_t Reg = TRI.getDwarfRegNum(Reg: MachineReg, isEH: false);
118
119 // If this is a valid register number, emit it.
120 if (Reg >= 0) {
121 DwarfRegs.push_back(Elt: Register::createRegister(RegNo: Reg, Comment: nullptr));
122 return true;
123 }
124
125 // Walk up the super-register chain until we find a valid number.
126 // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0.
127 for (MCPhysReg SR : TRI.superregs(Reg: MachineReg)) {
128 Reg = TRI.getDwarfRegNum(Reg: SR, isEH: false);
129 if (Reg >= 0) {
130 unsigned Idx = TRI.getSubRegIndex(RegNo: SR, SubRegNo: MachineReg);
131 unsigned Size = TRI.getSubRegIdxSize(Idx);
132 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx);
133 DwarfRegs.push_back(Elt: Register::createRegister(RegNo: Reg, Comment: "super-register"));
134 // Use a DW_OP_bit_piece to describe the sub-register.
135 setSubRegisterPiece(SizeInBits: Size, OffsetInBits: RegOffset);
136 return true;
137 }
138 }
139
140 // Otherwise, attempt to find a covering set of sub-register numbers.
141 // For example, Q0 on ARM is a composition of D0+D1.
142 unsigned CurPos = 0;
143 // The size of the register in bits.
144 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg: MachineReg);
145 unsigned RegSize = TRI.getRegSizeInBits(RC: *RC);
146 // Keep track of the bits in the register we already emitted, so we
147 // can avoid emitting redundant aliasing subregs. Because this is
148 // just doing a greedy scan of all subregisters, it is possible that
149 // this doesn't find a combination of subregisters that fully cover
150 // the register (even though one may exist).
151 SmallBitVector Coverage(RegSize, false);
152 for (MCPhysReg SR : TRI.subregs(Reg: MachineReg)) {
153 unsigned Idx = TRI.getSubRegIndex(RegNo: MachineReg, SubRegNo: SR);
154 unsigned Size = TRI.getSubRegIdxSize(Idx);
155 unsigned Offset = TRI.getSubRegIdxOffset(Idx);
156 Reg = TRI.getDwarfRegNum(Reg: SR, isEH: false);
157 if (Reg < 0 || Offset + Size > RegSize)
158 continue;
159
160 // Used to build the intersection between the bits we already
161 // emitted and the bits covered by this subregister.
162 SmallBitVector CurSubReg(RegSize, false);
163 CurSubReg.set(I: Offset, E: Offset + Size);
164
165 // If this sub-register has a DWARF number and we haven't covered
166 // its range, and its range covers the value, emit a DWARF piece for it.
167 if (Offset < MaxSize && !CurSubReg.subsetOf(RHS: Coverage)) {
168 // Emit a piece for any gap in the coverage.
169 if (Offset > CurPos)
170 DwarfRegs.push_back(Elt: Register::createSubRegister(
171 RegNo: -1, SizeInBits: Offset - CurPos, Comment: "no DWARF register encoding"));
172 if (Offset == 0 && Size >= MaxSize)
173 DwarfRegs.push_back(Elt: Register::createRegister(RegNo: Reg, Comment: "sub-register"));
174 else
175 DwarfRegs.push_back(Elt: Register::createSubRegister(
176 RegNo: Reg, SizeInBits: std::min<unsigned>(a: Size, b: MaxSize - Offset), Comment: "sub-register"));
177 }
178 // Mark it as emitted.
179 Coverage.set(I: Offset, E: Offset + Size);
180 CurPos = Offset + Size;
181 }
182 // Failed to find any DWARF encoding.
183 if (CurPos == 0)
184 return false;
185 // Found a partial or complete DWARF encoding.
186 if (CurPos < RegSize)
187 DwarfRegs.push_back(Elt: Register::createSubRegister(
188 RegNo: -1, SizeInBits: RegSize - CurPos, Comment: "no DWARF register encoding"));
189 return true;
190}
191
192void DwarfExpression::addStackValue() {
193 if (DwarfVersion >= 4)
194 emitOp(Op: dwarf::DW_OP_stack_value);
195}
196
197void DwarfExpression::addBooleanConstant(int64_t Value) {
198 assert(isImplicitLocation() || isUnknownLocation());
199 LocationKind = Implicit;
200 if (Value == 0)
201 emitOp(Op: dwarf::DW_OP_lit0);
202 else
203 emitOp(Op: dwarf::DW_OP_lit1);
204}
205
206void DwarfExpression::addSignedConstant(int64_t Value) {
207 assert(isImplicitLocation() || isUnknownLocation());
208 LocationKind = Implicit;
209 emitOp(Op: dwarf::DW_OP_consts);
210 emitSigned(Value);
211}
212
213void DwarfExpression::addUnsignedConstant(uint64_t Value) {
214 assert(isImplicitLocation() || isUnknownLocation());
215 LocationKind = Implicit;
216 emitConstu(Value);
217}
218
219void DwarfExpression::addUnsignedConstant(const APInt &Value) {
220 assert(isImplicitLocation() || isUnknownLocation());
221 LocationKind = Implicit;
222
223 unsigned Size = Value.getBitWidth();
224 const uint64_t *Data = Value.getRawData();
225
226 // Chop it up into 64-bit pieces, because that's the maximum that
227 // addUnsignedConstant takes.
228 unsigned Offset = 0;
229 while (Offset < Size) {
230 addUnsignedConstant(Value: *Data++);
231 if (Offset == 0 && Size <= 64)
232 break;
233 addStackValue();
234 addOpPiece(SizeInBits: std::min(a: Size - Offset, b: 64u), OffsetInBits: Offset);
235 Offset += 64;
236 }
237}
238
239void DwarfExpression::addImplicitValue(const APInt &Value,
240 const AsmPrinter &AP) {
241 assert(isImplicitLocation() || isUnknownLocation());
242 assert(DwarfVersion >= 4);
243
244 APInt API = Value;
245 unsigned NumBytes = API.getBitWidth() / 8;
246 assert(API.getBitWidth() == NumBytes * 8 &&
247 "implicit value must be byte-sized");
248
249 emitOp(Op: dwarf::DW_OP_implicit_value);
250 emitUnsigned(Value: NumBytes);
251
252 // The loop below is emitting the value starting at the least significant
253 // byte, so byte-swap first for big-endian targets.
254 if (AP.getDataLayout().isBigEndian())
255 API = API.byteSwap();
256
257 for (unsigned I = 0; I < NumBytes; ++I)
258 emitData1(Value: API.extractBits(numBits: 8, bitPosition: I * 8).getZExtValue());
259}
260
261void DwarfExpression::addConstantFP(const APFloat &APF, const AsmPrinter &AP) {
262 assert(isImplicitLocation() || isUnknownLocation());
263 APInt API = APF.bitcastToAPInt();
264 int NumBytes = API.getBitWidth() / 8;
265 if (NumBytes == 4 /*float*/ || NumBytes == 8 /*double*/) {
266 // FIXME: Add support for `long double`.
267 emitOp(Op: dwarf::DW_OP_implicit_value);
268 emitUnsigned(Value: NumBytes /*Size of the block in bytes*/);
269
270 // The loop below is emitting the value starting at least significant byte,
271 // so we need to perform a byte-swap to get the byte order correct in case
272 // of a big-endian target.
273 if (AP.getDataLayout().isBigEndian())
274 API = API.byteSwap();
275
276 for (int i = 0; i < NumBytes; ++i) {
277 emitData1(Value: API.getZExtValue() & 0xFF);
278 API = API.lshr(shiftAmt: 8);
279 }
280
281 return;
282 }
283 LLVM_DEBUG(
284 dbgs() << "Skipped DW_OP_implicit_value creation for ConstantFP of size: "
285 << API.getBitWidth() << " bits\n");
286}
287
288bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
289 DIExpressionCursor &ExprCursor,
290 llvm::Register MachineReg,
291 unsigned FragmentOffsetInBits) {
292 auto Fragment = ExprCursor.getFragmentInfo();
293 if (!addMachineReg(TRI, MachineReg, MaxSize: Fragment ? Fragment->SizeInBits : ~1U)) {
294 LocationKind = Unknown;
295 return false;
296 }
297
298 bool HasComplexExpression = false;
299 auto Op = ExprCursor.peek();
300 if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
301 HasComplexExpression = true;
302
303 // If the register can only be described by a complex expression (i.e.,
304 // multiple subregisters) it doesn't safely compose with another complex
305 // expression. For example, it is not possible to apply a DW_OP_deref
306 // operation to multiple DW_OP_pieces, since composite location descriptions
307 // do not push anything on the DWARF stack.
308 //
309 // DW_OP_entry_value operations can only hold a DWARF expression or a
310 // register location description, so we can't emit a single entry value
311 // covering a composite location description. In the future we may want to
312 // emit entry value operations for each register location in the composite
313 // location, but until that is supported do not emit anything.
314 if ((HasComplexExpression || IsEmittingEntryValue) && DwarfRegs.size() > 1) {
315 if (IsEmittingEntryValue)
316 cancelEntryValue();
317 DwarfRegs.clear();
318 LocationKind = Unknown;
319 return false;
320 }
321
322 // Handle simple register locations. If we are supposed to emit
323 // a call site parameter expression and if that expression is just a register
324 // location, emit it with addBReg and offset 0, because we should emit a DWARF
325 // expression representing a value, rather than a location.
326 if ((!isParameterValue() && !isMemoryLocation() && !HasComplexExpression) ||
327 isEntryValue()) {
328 auto FragmentInfo = ExprCursor.getFragmentInfo();
329 unsigned RegSize = 0;
330 for (auto &Reg : DwarfRegs) {
331 RegSize += Reg.SubRegSize;
332 if (Reg.DwarfRegNo >= 0)
333 addReg(DwarfReg: Reg.DwarfRegNo, Comment: Reg.Comment);
334 if (FragmentInfo)
335 if (RegSize > FragmentInfo->SizeInBits)
336 // If the register is larger than the current fragment stop
337 // once the fragment is covered.
338 break;
339 addOpPiece(SizeInBits: Reg.SubRegSize);
340 }
341
342 if (isEntryValue()) {
343 finalizeEntryValue();
344
345 if (!isIndirect() && !isParameterValue() && !HasComplexExpression &&
346 DwarfVersion >= 4)
347 emitOp(Op: dwarf::DW_OP_stack_value);
348 }
349
350 DwarfRegs.clear();
351 // If we need to mask out a subregister, do it now, unless the next
352 // operation would emit an OpPiece anyway.
353 auto NextOp = ExprCursor.peek();
354 if (SubRegisterSizeInBits && NextOp &&
355 (NextOp->getOp() != dwarf::DW_OP_LLVM_fragment))
356 maskSubRegister();
357 return true;
358 }
359
360 // Don't emit locations that cannot be expressed without DW_OP_stack_value.
361 if (DwarfVersion < 4)
362 if (any_of(Range&: ExprCursor, P: [](DIExpression::ExprOperand Op) -> bool {
363 return Op.getOp() == dwarf::DW_OP_stack_value;
364 })) {
365 DwarfRegs.clear();
366 LocationKind = Unknown;
367 return false;
368 }
369
370 // TODO: We should not give up here but the following code needs to be changed
371 // to deal with multiple (sub)registers first.
372 if (DwarfRegs.size() > 1) {
373 LLVM_DEBUG(dbgs() << "TODO: giving up on debug information due to "
374 "multi-register usage.\n");
375 DwarfRegs.clear();
376 LocationKind = Unknown;
377 return false;
378 }
379
380 auto Reg = DwarfRegs[0];
381 bool FBReg = isFrameRegister(TRI, MachineReg);
382 int SignedOffset = 0;
383 assert(!Reg.isSubRegister() && "full register expected");
384
385 // Pattern-match combinations for which more efficient representations exist.
386 // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset].
387 if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) {
388 uint64_t Offset = Op->getArg(I: 0);
389 uint64_t IntMax = static_cast<uint64_t>(std::numeric_limits<int>::max());
390 if (Offset <= IntMax) {
391 SignedOffset = Offset;
392 ExprCursor.take();
393 }
394 }
395
396 // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]
397 // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset]
398 // If Reg is a subregister we need to mask it out before subtracting.
399 if (Op && Op->getOp() == dwarf::DW_OP_constu) {
400 uint64_t Offset = Op->getArg(I: 0);
401 uint64_t IntMax = static_cast<uint64_t>(std::numeric_limits<int>::max());
402 auto N = ExprCursor.peekNext();
403 if (N && N->getOp() == dwarf::DW_OP_plus && Offset <= IntMax) {
404 SignedOffset = Offset;
405 ExprCursor.consume(N: 2);
406 } else if (N && N->getOp() == dwarf::DW_OP_minus &&
407 !SubRegisterSizeInBits && Offset <= IntMax + 1) {
408 SignedOffset = -static_cast<int64_t>(Offset);
409 ExprCursor.consume(N: 2);
410 }
411 }
412
413 if (FBReg)
414 addFBReg(Offset: SignedOffset);
415 else
416 addBReg(DwarfReg: Reg.DwarfRegNo, Offset: SignedOffset);
417 DwarfRegs.clear();
418
419 // If we need to mask out a subregister, do it now, unless the next
420 // operation would emit an OpPiece anyway.
421 auto NextOp = ExprCursor.peek();
422 if (SubRegisterSizeInBits && NextOp &&
423 (NextOp->getOp() != dwarf::DW_OP_LLVM_fragment))
424 maskSubRegister();
425
426 return true;
427}
428
429void DwarfExpression::setEntryValueFlags(const MachineLocation &Loc) {
430 LocationFlags |= EntryValue;
431 if (Loc.isIndirect())
432 LocationFlags |= Indirect;
433}
434
435void DwarfExpression::setLocation(const MachineLocation &Loc,
436 const DIExpression *DIExpr) {
437 if (Loc.isIndirect())
438 setMemoryLocationKind();
439
440 if (DIExpr->isEntryValue())
441 setEntryValueFlags(Loc);
442}
443
444void DwarfExpression::beginEntryValueExpression(
445 DIExpressionCursor &ExprCursor) {
446 auto Op = ExprCursor.take();
447 (void)Op;
448 assert(Op && Op->getOp() == dwarf::DW_OP_LLVM_entry_value);
449 assert(!IsEmittingEntryValue && "Already emitting entry value?");
450 assert(Op->getArg(0) == 1 &&
451 "Can currently only emit entry values covering a single operation");
452
453 SavedLocationKind = LocationKind;
454 LocationKind = Register;
455 LocationFlags |= EntryValue;
456 IsEmittingEntryValue = true;
457 enableTemporaryBuffer();
458}
459
460void DwarfExpression::finalizeEntryValue() {
461 assert(IsEmittingEntryValue && "Entry value not open?");
462 disableTemporaryBuffer();
463
464 emitOp(Op: CU.getDwarf5OrGNULocationAtom(Loc: dwarf::DW_OP_entry_value));
465
466 // Emit the entry value's size operand.
467 unsigned Size = getTemporaryBufferSize();
468 emitUnsigned(Value: Size);
469
470 // Emit the entry value's DWARF block operand.
471 commitTemporaryBuffer();
472
473 LocationFlags &= ~EntryValue;
474 LocationKind = SavedLocationKind;
475 IsEmittingEntryValue = false;
476}
477
478void DwarfExpression::cancelEntryValue() {
479 assert(IsEmittingEntryValue && "Entry value not open?");
480 disableTemporaryBuffer();
481
482 // The temporary buffer can't be emptied, so for now just assert that nothing
483 // has been emitted to it.
484 assert(getTemporaryBufferSize() == 0 &&
485 "Began emitting entry value block before cancelling entry value");
486
487 LocationKind = SavedLocationKind;
488 IsEmittingEntryValue = false;
489}
490
491unsigned DwarfExpression::getOrCreateBaseType(unsigned BitSize,
492 dwarf::TypeKind Encoding) {
493 // Reuse the base_type if we already have one in this CU otherwise we
494 // create a new one.
495 unsigned I = 0, E = CU.ExprRefedBaseTypes.size();
496 for (; I != E; ++I)
497 if (CU.ExprRefedBaseTypes[I].BitSize == BitSize &&
498 CU.ExprRefedBaseTypes[I].Encoding == Encoding)
499 break;
500
501 if (I == E)
502 CU.ExprRefedBaseTypes.emplace_back(args&: BitSize, args&: Encoding);
503 return I;
504}
505
506/// Assuming a well-formed expression, match "DW_OP_deref*
507/// DW_OP_LLVM_fragment?".
508static bool isMemoryLocation(DIExpressionCursor ExprCursor) {
509 while (ExprCursor) {
510 auto Op = ExprCursor.take();
511 switch (Op->getOp()) {
512 case dwarf::DW_OP_deref:
513 case dwarf::DW_OP_LLVM_fragment:
514 break;
515 default:
516 return false;
517 }
518 }
519 return true;
520}
521
522void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor) {
523 addExpression(Expr: std::move(ExprCursor),
524 InsertArg: [](unsigned Idx, DIExpressionCursor &Cursor) -> bool {
525 llvm_unreachable("unhandled opcode found in expression");
526 });
527}
528
529bool DwarfExpression::addExpression(
530 DIExpressionCursor &&ExprCursor,
531 llvm::function_ref<bool(unsigned, DIExpressionCursor &)> InsertArg) {
532 // Entry values can currently only cover the initial register location,
533 // and not any other parts of the following DWARF expression.
534 assert(!IsEmittingEntryValue && "Can't emit entry value around expression");
535
536 std::optional<DIExpression::ExprOperand> PrevConvertOp;
537
538 while (ExprCursor) {
539 auto Op = ExprCursor.take();
540 uint64_t OpNum = Op->getOp();
541
542 if (OpNum >= dwarf::DW_OP_reg0 && OpNum <= dwarf::DW_OP_reg31) {
543 emitOp(Op: OpNum);
544 continue;
545 } else if (OpNum >= dwarf::DW_OP_breg0 && OpNum <= dwarf::DW_OP_breg31) {
546 addBReg(DwarfReg: OpNum - dwarf::DW_OP_breg0, Offset: Op->getArg(I: 0));
547 continue;
548 }
549
550 switch (OpNum) {
551 case dwarf::DW_OP_LLVM_arg:
552 if (!InsertArg(Op->getArg(I: 0), ExprCursor)) {
553 LocationKind = Unknown;
554 return false;
555 }
556 break;
557 case dwarf::DW_OP_LLVM_fragment: {
558 unsigned SizeInBits = Op->getArg(I: 1);
559 unsigned FragmentOffset = Op->getArg(I: 0);
560 // The fragment offset must have already been adjusted by emitting an
561 // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base
562 // location.
563 assert(OffsetInBits >= FragmentOffset && "fragment offset not added?");
564 assert(SizeInBits >= OffsetInBits - FragmentOffset && "size underflow");
565
566 // If addMachineReg already emitted DW_OP_piece operations to represent
567 // a super-register by splicing together sub-registers, subtract the size
568 // of the pieces that was already emitted.
569 SizeInBits -= OffsetInBits - FragmentOffset;
570
571 // If addMachineReg requested a DW_OP_bit_piece to stencil out a
572 // sub-register that is smaller than the current fragment's size, use it.
573 if (SubRegisterSizeInBits)
574 SizeInBits = std::min<unsigned>(a: SizeInBits, b: SubRegisterSizeInBits);
575
576 // Emit a DW_OP_stack_value for implicit location descriptions.
577 if (isImplicitLocation())
578 addStackValue();
579
580 // Emit the DW_OP_piece.
581 addOpPiece(SizeInBits, OffsetInBits: SubRegisterOffsetInBits);
582 setSubRegisterPiece(SizeInBits: 0, OffsetInBits: 0);
583 // Reset the location description kind.
584 LocationKind = Unknown;
585 return true;
586 }
587 case dwarf::DW_OP_LLVM_extract_bits_sext:
588 case dwarf::DW_OP_LLVM_extract_bits_zext: {
589 unsigned SizeInBits = Op->getArg(I: 1);
590 unsigned BitOffset = Op->getArg(I: 0);
591 unsigned DerefSize = 0;
592 // Operations are done in the DWARF "generic type" whose size
593 // is the size of a pointer.
594 unsigned PtrSizeInBytes = CU.getAsmPrinter()->MAI.getCodePointerSize();
595
596 // If we have a memory location then dereference to get the value, though
597 // we have to make sure we don't dereference any bytes past the end of the
598 // object.
599 if (isMemoryLocation()) {
600 DerefSize = alignTo(Value: BitOffset + SizeInBits, Align: 8) / 8;
601 if (DerefSize == PtrSizeInBytes) {
602 emitOp(Op: dwarf::DW_OP_deref);
603 } else {
604 emitOp(Op: dwarf::DW_OP_deref_size);
605 emitUnsigned(Value: DerefSize);
606 }
607 }
608
609 // If a dereference was emitted for an unsigned value, and
610 // there's no bit offset, then a bit of optimization is
611 // possible.
612 if (OpNum == dwarf::DW_OP_LLVM_extract_bits_zext && BitOffset == 0) {
613 if (8 * DerefSize == SizeInBits) {
614 // The correct value is already on the stack.
615 } else {
616 // No need to shift, we can just mask off the desired bits.
617 emitOp(Op: dwarf::DW_OP_constu);
618 emitUnsigned(Value: (1u << SizeInBits) - 1);
619 emitOp(Op: dwarf::DW_OP_and);
620 }
621 } else {
622 // Extract the bits by a shift left (to shift out the bits after what we
623 // want to extract) followed by shift right (to shift the bits to
624 // position 0 and also sign/zero extend).
625 unsigned LeftShift = PtrSizeInBytes * 8 - (SizeInBits + BitOffset);
626 unsigned RightShift = LeftShift + BitOffset;
627 if (LeftShift) {
628 emitOp(Op: dwarf::DW_OP_constu);
629 emitUnsigned(Value: LeftShift);
630 emitOp(Op: dwarf::DW_OP_shl);
631 }
632 if (RightShift) {
633 emitOp(Op: dwarf::DW_OP_constu);
634 emitUnsigned(Value: RightShift);
635 emitOp(Op: OpNum == dwarf::DW_OP_LLVM_extract_bits_sext
636 ? dwarf::DW_OP_shra
637 : dwarf::DW_OP_shr);
638 }
639 }
640
641 // The value is now at the top of the stack, so set the location to
642 // implicit so that we get a stack_value at the end.
643 LocationKind = Implicit;
644 break;
645 }
646 case dwarf::DW_OP_plus_uconst:
647 assert(!isRegisterLocation());
648 emitOp(Op: dwarf::DW_OP_plus_uconst);
649 emitUnsigned(Value: Op->getArg(I: 0));
650 break;
651 case dwarf::DW_OP_plus:
652 case dwarf::DW_OP_minus:
653 case dwarf::DW_OP_mul:
654 case dwarf::DW_OP_div:
655 case dwarf::DW_OP_mod:
656 case dwarf::DW_OP_or:
657 case dwarf::DW_OP_and:
658 case dwarf::DW_OP_xor:
659 case dwarf::DW_OP_shl:
660 case dwarf::DW_OP_shr:
661 case dwarf::DW_OP_shra:
662 case dwarf::DW_OP_lit0:
663 case dwarf::DW_OP_not:
664 case dwarf::DW_OP_dup:
665 case dwarf::DW_OP_push_object_address:
666 case dwarf::DW_OP_over:
667 case dwarf::DW_OP_rot:
668 case dwarf::DW_OP_eq:
669 case dwarf::DW_OP_ne:
670 case dwarf::DW_OP_gt:
671 case dwarf::DW_OP_ge:
672 case dwarf::DW_OP_lt:
673 case dwarf::DW_OP_le:
674 case dwarf::DW_OP_neg:
675 case dwarf::DW_OP_abs:
676 emitOp(Op: OpNum);
677 break;
678 case dwarf::DW_OP_deref:
679 assert(!isRegisterLocation());
680 if (!isMemoryLocation() && ::isMemoryLocation(ExprCursor))
681 // Turning this into a memory location description makes the deref
682 // implicit.
683 LocationKind = Memory;
684 else
685 emitOp(Op: dwarf::DW_OP_deref);
686 break;
687 case dwarf::DW_OP_constu:
688 assert(!isRegisterLocation());
689 emitConstu(Value: Op->getArg(I: 0));
690 break;
691 case dwarf::DW_OP_consts:
692 assert(!isRegisterLocation());
693 emitOp(Op: dwarf::DW_OP_consts);
694 emitSigned(Value: Op->getArg(I: 0));
695 break;
696 case dwarf::DW_OP_LLVM_convert: {
697 unsigned BitSize = Op->getArg(I: 0);
698 dwarf::TypeKind Encoding = static_cast<dwarf::TypeKind>(Op->getArg(I: 1));
699 if (DwarfVersion >= 5 && CU.getDwarfDebug().useOpConvert()) {
700 emitOp(Op: dwarf::DW_OP_convert);
701 // If targeting a location-list; simply emit the index into the raw
702 // byte stream as ULEB128, DwarfDebug::emitDebugLocEntry has been
703 // fitted with means to extract it later.
704 // If targeting a inlined DW_AT_location; insert a DIEBaseTypeRef
705 // (containing the index and a resolve mechanism during emit) into the
706 // DIE value list.
707 emitBaseTypeRef(Idx: getOrCreateBaseType(BitSize, Encoding));
708 } else {
709 if (PrevConvertOp && PrevConvertOp->getArg(I: 0) < BitSize) {
710 if (Encoding == dwarf::DW_ATE_signed)
711 emitLegacySExt(FromBits: PrevConvertOp->getArg(I: 0));
712 else if (Encoding == dwarf::DW_ATE_unsigned)
713 emitLegacyZExt(FromBits: PrevConvertOp->getArg(I: 0));
714 PrevConvertOp = std::nullopt;
715 } else {
716 PrevConvertOp = Op;
717 }
718 }
719 break;
720 }
721 case dwarf::DW_OP_stack_value:
722 LocationKind = Implicit;
723 break;
724 case dwarf::DW_OP_swap:
725 assert(!isRegisterLocation());
726 emitOp(Op: dwarf::DW_OP_swap);
727 break;
728 case dwarf::DW_OP_xderef:
729 assert(!isRegisterLocation());
730 emitOp(Op: dwarf::DW_OP_xderef);
731 break;
732 case dwarf::DW_OP_deref_size:
733 emitOp(Op: dwarf::DW_OP_deref_size);
734 emitData1(Value: Op->getArg(I: 0));
735 break;
736 case dwarf::DW_OP_LLVM_tag_offset:
737 TagOffset = Op->getArg(I: 0);
738 break;
739 case dwarf::DW_OP_regx:
740 emitOp(Op: dwarf::DW_OP_regx);
741 emitUnsigned(Value: Op->getArg(I: 0));
742 break;
743 case dwarf::DW_OP_bregx:
744 emitOp(Op: dwarf::DW_OP_bregx);
745 emitUnsigned(Value: Op->getArg(I: 0));
746 emitSigned(Value: Op->getArg(I: 1));
747 break;
748 case dwarf::DW_OP_LLVM_implicit_pointer:
749 // Handled in DwarfCompileUnit::emitImplicitPointerLocation for
750 // Loc::Single variables. If we reach here, the variable has a
751 // location list or other unsupported path. Drop the
752 // location rather than crashing.
753 return false;
754 default:
755 llvm_unreachable("unhandled opcode found in expression");
756 }
757 }
758
759 if (isImplicitLocation() && !isParameterValue())
760 // Turn this into an implicit location description.
761 addStackValue();
762
763 return true;
764}
765
766/// add masking operations to stencil out a subregister.
767void DwarfExpression::maskSubRegister() {
768 assert(SubRegisterSizeInBits && "no subregister was registered");
769 if (SubRegisterOffsetInBits > 0)
770 addShr(ShiftBy: SubRegisterOffsetInBits);
771 uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL;
772 addAnd(Mask);
773}
774
775void DwarfExpression::finalize() {
776 assert(DwarfRegs.size() == 0 && "dwarf registers not emitted");
777 // Emit any outstanding DW_OP_piece operations to mask out subregisters.
778 if (SubRegisterSizeInBits == 0)
779 return;
780 // Don't emit a DW_OP_piece for a subregister at offset 0.
781 if (SubRegisterOffsetInBits == 0)
782 return;
783 addOpPiece(SizeInBits: SubRegisterSizeInBits, OffsetInBits: SubRegisterOffsetInBits);
784}
785
786void DwarfExpression::addFragmentOffset(const DIExpression *Expr) {
787 if (!Expr || !Expr->isFragment())
788 return;
789
790 uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits;
791 assert(FragmentOffset >= OffsetInBits &&
792 "overlapping or duplicate fragments");
793 if (FragmentOffset > OffsetInBits)
794 addOpPiece(SizeInBits: FragmentOffset - OffsetInBits);
795 OffsetInBits = FragmentOffset;
796}
797
798void DwarfExpression::emitLegacySExt(unsigned FromBits) {
799 // (((X >> (FromBits - 1)) * (~0)) << FromBits) | X
800 emitOp(Op: dwarf::DW_OP_dup);
801 emitOp(Op: dwarf::DW_OP_constu);
802 emitUnsigned(Value: FromBits - 1);
803 emitOp(Op: dwarf::DW_OP_shr);
804 emitOp(Op: dwarf::DW_OP_lit0);
805 emitOp(Op: dwarf::DW_OP_not);
806 emitOp(Op: dwarf::DW_OP_mul);
807 emitOp(Op: dwarf::DW_OP_constu);
808 emitUnsigned(Value: FromBits);
809 emitOp(Op: dwarf::DW_OP_shl);
810 emitOp(Op: dwarf::DW_OP_or);
811}
812
813void DwarfExpression::emitLegacyZExt(unsigned FromBits) {
814 // Heuristic to decide the most efficient encoding.
815 // A ULEB can encode 7 1-bits per byte.
816 if (FromBits / 7 < 1+1+1+1+1) {
817 // (X & (1 << FromBits - 1))
818 emitOp(Op: dwarf::DW_OP_constu);
819 emitUnsigned(Value: (1ULL << FromBits) - 1);
820 } else {
821 // Note that the DWARF 4 stack consists of pointer-sized elements,
822 // so technically it doesn't make sense to shift left more than 64
823 // bits. We leave that for the consumer to decide though. LLDB for
824 // example uses APInt for the stack elements and can still deal
825 // with this.
826 emitOp(Op: dwarf::DW_OP_lit1);
827 emitOp(Op: dwarf::DW_OP_constu);
828 emitUnsigned(Value: FromBits);
829 emitOp(Op: dwarf::DW_OP_shl);
830 emitOp(Op: dwarf::DW_OP_lit1);
831 emitOp(Op: dwarf::DW_OP_minus);
832 }
833 emitOp(Op: dwarf::DW_OP_and);
834}
835
836void DwarfExpression::addWasmLocation(unsigned Index, uint64_t Offset) {
837 emitOp(Op: dwarf::DW_OP_WASM_location);
838 emitUnsigned(Value: Index == 4/*TI_LOCAL_INDIRECT*/ ? 0/*TI_LOCAL*/ : Index);
839 emitUnsigned(Value: Offset);
840 if (Index == 4 /*TI_LOCAL_INDIRECT*/) {
841 assert(LocationKind == Unknown);
842 LocationKind = Memory;
843 } else {
844 assert(LocationKind == Implicit || LocationKind == Unknown);
845 LocationKind = Implicit;
846 }
847}
848