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/Support/ErrorHandling.h" 22 #include <algorithm> 23 24 using namespace llvm; 25 26 #define DEBUG_TYPE "dwarfdebug" 27 28 void 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 42 void DwarfExpression::addReg(int DwarfReg, const char *Comment) { 43 assert(DwarfReg >= 0 && "invalid negative dwarf register number"); 44 assert((isUnknownLocation() || isRegisterLocation()) && 45 "location description already locked down"); 46 LocationKind = Register; 47 if (DwarfReg < 32) { 48 emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment); 49 } else { 50 emitOp(dwarf::DW_OP_regx, Comment); 51 emitUnsigned(DwarfReg); 52 } 53 } 54 55 void DwarfExpression::addBReg(int DwarfReg, int Offset) { 56 assert(DwarfReg >= 0 && "invalid negative dwarf register number"); 57 assert(!isRegisterLocation() && "location description already locked down"); 58 if (DwarfReg < 32) { 59 emitOp(dwarf::DW_OP_breg0 + DwarfReg); 60 } else { 61 emitOp(dwarf::DW_OP_bregx); 62 emitUnsigned(DwarfReg); 63 } 64 emitSigned(Offset); 65 } 66 67 void DwarfExpression::addFBReg(int Offset) { 68 emitOp(dwarf::DW_OP_fbreg); 69 emitSigned(Offset); 70 } 71 72 void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) { 73 if (!SizeInBits) 74 return; 75 76 const unsigned SizeOfByte = 8; 77 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) { 78 emitOp(dwarf::DW_OP_bit_piece); 79 emitUnsigned(SizeInBits); 80 emitUnsigned(OffsetInBits); 81 } else { 82 emitOp(dwarf::DW_OP_piece); 83 unsigned ByteSize = SizeInBits / SizeOfByte; 84 emitUnsigned(ByteSize); 85 } 86 this->OffsetInBits += SizeInBits; 87 } 88 89 void DwarfExpression::addShr(unsigned ShiftBy) { 90 emitConstu(ShiftBy); 91 emitOp(dwarf::DW_OP_shr); 92 } 93 94 void DwarfExpression::addAnd(unsigned Mask) { 95 emitConstu(Mask); 96 emitOp(dwarf::DW_OP_and); 97 } 98 99 bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI, 100 llvm::Register MachineReg, 101 unsigned MaxSize) { 102 if (!llvm::Register::isPhysicalRegister(MachineReg)) { 103 if (isFrameRegister(TRI, MachineReg)) { 104 DwarfRegs.push_back(Register::createRegister(-1, nullptr)); 105 return true; 106 } 107 return false; 108 } 109 110 int Reg = TRI.getDwarfRegNum(MachineReg, false); 111 112 // If this is a valid register number, emit it. 113 if (Reg >= 0) { 114 DwarfRegs.push_back(Register::createRegister(Reg, nullptr)); 115 return true; 116 } 117 118 // Walk up the super-register chain until we find a valid number. 119 // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0. 120 for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) { 121 Reg = TRI.getDwarfRegNum(*SR, false); 122 if (Reg >= 0) { 123 unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg); 124 unsigned Size = TRI.getSubRegIdxSize(Idx); 125 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx); 126 DwarfRegs.push_back(Register::createRegister(Reg, "super-register")); 127 // Use a DW_OP_bit_piece to describe the sub-register. 128 setSubRegisterPiece(Size, RegOffset); 129 return true; 130 } 131 } 132 133 // Otherwise, attempt to find a covering set of sub-register numbers. 134 // For example, Q0 on ARM is a composition of D0+D1. 135 unsigned CurPos = 0; 136 // The size of the register in bits. 137 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(MachineReg); 138 unsigned RegSize = TRI.getRegSizeInBits(*RC); 139 // Keep track of the bits in the register we already emitted, so we 140 // can avoid emitting redundant aliasing subregs. Because this is 141 // just doing a greedy scan of all subregisters, it is possible that 142 // this doesn't find a combination of subregisters that fully cover 143 // the register (even though one may exist). 144 SmallBitVector Coverage(RegSize, false); 145 for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) { 146 unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR); 147 unsigned Size = TRI.getSubRegIdxSize(Idx); 148 unsigned Offset = TRI.getSubRegIdxOffset(Idx); 149 Reg = TRI.getDwarfRegNum(*SR, false); 150 if (Reg < 0) 151 continue; 152 153 // Used to build the intersection between the bits we already 154 // emitted and the bits covered by this subregister. 155 SmallBitVector CurSubReg(RegSize, false); 156 CurSubReg.set(Offset, Offset + Size); 157 158 // If this sub-register has a DWARF number and we haven't covered 159 // its range, and its range covers the value, emit a DWARF piece for it. 160 if (Offset < MaxSize && CurSubReg.test(Coverage)) { 161 // Emit a piece for any gap in the coverage. 162 if (Offset > CurPos) 163 DwarfRegs.push_back(Register::createSubRegister( 164 -1, Offset - CurPos, "no DWARF register encoding")); 165 if (Offset == 0 && Size >= MaxSize) 166 DwarfRegs.push_back(Register::createRegister(Reg, "sub-register")); 167 else 168 DwarfRegs.push_back(Register::createSubRegister( 169 Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register")); 170 } 171 // Mark it as emitted. 172 Coverage.set(Offset, Offset + Size); 173 CurPos = Offset + Size; 174 } 175 // Failed to find any DWARF encoding. 176 if (CurPos == 0) 177 return false; 178 // Found a partial or complete DWARF encoding. 179 if (CurPos < RegSize) 180 DwarfRegs.push_back(Register::createSubRegister( 181 -1, RegSize - CurPos, "no DWARF register encoding")); 182 return true; 183 } 184 185 void DwarfExpression::addStackValue() { 186 if (DwarfVersion >= 4) 187 emitOp(dwarf::DW_OP_stack_value); 188 } 189 190 void DwarfExpression::addSignedConstant(int64_t Value) { 191 assert(isImplicitLocation() || isUnknownLocation()); 192 LocationKind = Implicit; 193 emitOp(dwarf::DW_OP_consts); 194 emitSigned(Value); 195 } 196 197 void DwarfExpression::addUnsignedConstant(uint64_t Value) { 198 assert(isImplicitLocation() || isUnknownLocation()); 199 LocationKind = Implicit; 200 emitConstu(Value); 201 } 202 203 void DwarfExpression::addUnsignedConstant(APInt Value, const AsmPrinter &AP) { 204 assert(isImplicitLocation() || isUnknownLocation()); 205 LocationKind = Implicit; 206 207 if (AP.getDwarfVersion() >= 4 && !AP.getDwarfDebug()->tuneForSCE()) { 208 int NumBytes = Value.getBitWidth() / 8; 209 emitOp(dwarf::DW_OP_implicit_value); 210 emitUnsigned(NumBytes /*Size of the block in bytes*/); 211 212 // The loop below is emitting the value starting at least significant 213 // byte, so we need to perform a byte-swap to get the byte order correct 214 // in case of a big-endian target. 215 if (AP.getDataLayout().isBigEndian()) 216 Value = Value.byteSwap(); 217 218 for (int i = 0; i < NumBytes; ++i) { 219 emitData1(Value.getRawData()[0] & 0xFF); 220 Value = Value.lshr(8); 221 } 222 223 return; 224 } 225 226 unsigned Size = Value.getBitWidth(); 227 const uint64_t *Data = Value.getRawData(); 228 229 // Chop it up into 64-bit pieces, because that's the maximum that 230 // addUnsignedConstant takes. 231 unsigned Offset = 0; 232 while (Offset < Size) { 233 addUnsignedConstant(*Data++); 234 addStackValue(); 235 if (Offset == 0 && Size <= 64) 236 break; 237 addOpPiece(std::min(Size - Offset, 64u), Offset); 238 Offset += 64; 239 } 240 } 241 242 void DwarfExpression::addConstantFP(const APFloat &APF, const AsmPrinter &AP) { 243 assert(isImplicitLocation() || isUnknownLocation()); 244 APInt API = APF.bitcastToAPInt(); 245 int NumBytes = API.getBitWidth() / 8; 246 // FIXME: Add support for `long double`. 247 if (NumBytes <= 8 /*double*/) 248 addUnsignedConstant(API, AP); 249 else 250 LLVM_DEBUG( 251 dbgs() << "Skipped DwarfExpression creation for ConstantFP of size" 252 << API.getBitWidth() << " bits\n"); 253 } 254 255 bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI, 256 DIExpressionCursor &ExprCursor, 257 llvm::Register MachineReg, 258 unsigned FragmentOffsetInBits) { 259 auto Fragment = ExprCursor.getFragmentInfo(); 260 if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) { 261 LocationKind = Unknown; 262 return false; 263 } 264 265 bool HasComplexExpression = false; 266 auto Op = ExprCursor.peek(); 267 if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment) 268 HasComplexExpression = true; 269 270 // If the register can only be described by a complex expression (i.e., 271 // multiple subregisters) it doesn't safely compose with another complex 272 // expression. For example, it is not possible to apply a DW_OP_deref 273 // operation to multiple DW_OP_pieces, since composite location descriptions 274 // do not push anything on the DWARF stack. 275 // 276 // DW_OP_entry_value operations can only hold a DWARF expression or a 277 // register location description, so we can't emit a single entry value 278 // covering a composite location description. In the future we may want to 279 // emit entry value operations for each register location in the composite 280 // location, but until that is supported do not emit anything. 281 if ((HasComplexExpression || IsEmittingEntryValue) && DwarfRegs.size() > 1) { 282 if (IsEmittingEntryValue) 283 cancelEntryValue(); 284 DwarfRegs.clear(); 285 LocationKind = Unknown; 286 return false; 287 } 288 289 // Handle simple register locations. If we are supposed to emit 290 // a call site parameter expression and if that expression is just a register 291 // location, emit it with addBReg and offset 0, because we should emit a DWARF 292 // expression representing a value, rather than a location. 293 if (!isMemoryLocation() && !HasComplexExpression && 294 (!isParameterValue() || isEntryValue())) { 295 for (auto &Reg : DwarfRegs) { 296 if (Reg.DwarfRegNo >= 0) 297 addReg(Reg.DwarfRegNo, Reg.Comment); 298 addOpPiece(Reg.SubRegSize); 299 } 300 301 if (isEntryValue()) 302 finalizeEntryValue(); 303 304 if (isEntryValue() && !isIndirect() && !isParameterValue() && 305 DwarfVersion >= 4) 306 emitOp(dwarf::DW_OP_stack_value); 307 308 DwarfRegs.clear(); 309 return true; 310 } 311 312 // Don't emit locations that cannot be expressed without DW_OP_stack_value. 313 if (DwarfVersion < 4) 314 if (any_of(ExprCursor, [](DIExpression::ExprOperand Op) -> bool { 315 return Op.getOp() == dwarf::DW_OP_stack_value; 316 })) { 317 DwarfRegs.clear(); 318 LocationKind = Unknown; 319 return false; 320 } 321 322 assert(DwarfRegs.size() == 1); 323 auto Reg = DwarfRegs[0]; 324 bool FBReg = isFrameRegister(TRI, MachineReg); 325 int SignedOffset = 0; 326 assert(!Reg.isSubRegister() && "full register expected"); 327 328 // Pattern-match combinations for which more efficient representations exist. 329 // [Reg, DW_OP_plus_uconst, Offset] --> [DW_OP_breg, Offset]. 330 if (Op && (Op->getOp() == dwarf::DW_OP_plus_uconst)) { 331 uint64_t Offset = Op->getArg(0); 332 uint64_t IntMax = static_cast<uint64_t>(std::numeric_limits<int>::max()); 333 if (Offset <= IntMax) { 334 SignedOffset = Offset; 335 ExprCursor.take(); 336 } 337 } 338 339 // [Reg, DW_OP_constu, Offset, DW_OP_plus] --> [DW_OP_breg, Offset] 340 // [Reg, DW_OP_constu, Offset, DW_OP_minus] --> [DW_OP_breg,-Offset] 341 // If Reg is a subregister we need to mask it out before subtracting. 342 if (Op && Op->getOp() == dwarf::DW_OP_constu) { 343 uint64_t Offset = Op->getArg(0); 344 uint64_t IntMax = static_cast<uint64_t>(std::numeric_limits<int>::max()); 345 auto N = ExprCursor.peekNext(); 346 if (N && N->getOp() == dwarf::DW_OP_plus && Offset <= IntMax) { 347 SignedOffset = Offset; 348 ExprCursor.consume(2); 349 } else if (N && N->getOp() == dwarf::DW_OP_minus && 350 !SubRegisterSizeInBits && Offset <= IntMax + 1) { 351 SignedOffset = -static_cast<int64_t>(Offset); 352 ExprCursor.consume(2); 353 } 354 } 355 356 if (FBReg) 357 addFBReg(SignedOffset); 358 else 359 addBReg(Reg.DwarfRegNo, SignedOffset); 360 DwarfRegs.clear(); 361 return true; 362 } 363 364 void DwarfExpression::setEntryValueFlags(const MachineLocation &Loc) { 365 LocationFlags |= EntryValue; 366 if (Loc.isIndirect()) 367 LocationFlags |= Indirect; 368 } 369 370 void DwarfExpression::setLocation(const MachineLocation &Loc, 371 const DIExpression *DIExpr) { 372 if (Loc.isIndirect()) 373 // Do not treat entry value descriptions of indirect parameters as memory 374 // locations. This allows DwarfExpression::addReg() to add DW_OP_regN to an 375 // entry value description. 376 if (!DIExpr->isEntryValue()) 377 setMemoryLocationKind(); 378 379 if (DIExpr->isEntryValue()) 380 setEntryValueFlags(Loc); 381 } 382 383 void DwarfExpression::beginEntryValueExpression( 384 DIExpressionCursor &ExprCursor) { 385 auto Op = ExprCursor.take(); 386 (void)Op; 387 assert(Op && Op->getOp() == dwarf::DW_OP_LLVM_entry_value); 388 assert(!isMemoryLocation() && 389 "We don't support entry values of memory locations yet"); 390 assert(!IsEmittingEntryValue && "Already emitting entry value?"); 391 assert(Op->getArg(0) == 1 && 392 "Can currently only emit entry values covering a single operation"); 393 394 IsEmittingEntryValue = true; 395 enableTemporaryBuffer(); 396 } 397 398 void DwarfExpression::finalizeEntryValue() { 399 assert(IsEmittingEntryValue && "Entry value not open?"); 400 disableTemporaryBuffer(); 401 402 emitOp(CU.getDwarf5OrGNULocationAtom(dwarf::DW_OP_entry_value)); 403 404 // Emit the entry value's size operand. 405 unsigned Size = getTemporaryBufferSize(); 406 emitUnsigned(Size); 407 408 // Emit the entry value's DWARF block operand. 409 commitTemporaryBuffer(); 410 411 IsEmittingEntryValue = false; 412 } 413 414 void DwarfExpression::cancelEntryValue() { 415 assert(IsEmittingEntryValue && "Entry value not open?"); 416 disableTemporaryBuffer(); 417 418 // The temporary buffer can't be emptied, so for now just assert that nothing 419 // has been emitted to it. 420 assert(getTemporaryBufferSize() == 0 && 421 "Began emitting entry value block before cancelling entry value"); 422 423 IsEmittingEntryValue = false; 424 } 425 426 unsigned DwarfExpression::getOrCreateBaseType(unsigned BitSize, 427 dwarf::TypeKind Encoding) { 428 // Reuse the base_type if we already have one in this CU otherwise we 429 // create a new one. 430 unsigned I = 0, E = CU.ExprRefedBaseTypes.size(); 431 for (; I != E; ++I) 432 if (CU.ExprRefedBaseTypes[I].BitSize == BitSize && 433 CU.ExprRefedBaseTypes[I].Encoding == Encoding) 434 break; 435 436 if (I == E) 437 CU.ExprRefedBaseTypes.emplace_back(BitSize, Encoding); 438 return I; 439 } 440 441 /// Assuming a well-formed expression, match "DW_OP_deref* 442 /// DW_OP_LLVM_fragment?". 443 static bool isMemoryLocation(DIExpressionCursor ExprCursor) { 444 while (ExprCursor) { 445 auto Op = ExprCursor.take(); 446 switch (Op->getOp()) { 447 case dwarf::DW_OP_deref: 448 case dwarf::DW_OP_LLVM_fragment: 449 break; 450 default: 451 return false; 452 } 453 } 454 return true; 455 } 456 457 void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor, 458 unsigned FragmentOffsetInBits) { 459 // Entry values can currently only cover the initial register location, 460 // and not any other parts of the following DWARF expression. 461 assert(!IsEmittingEntryValue && "Can't emit entry value around expression"); 462 463 // If we need to mask out a subregister, do it now, unless the next 464 // operation would emit an OpPiece anyway. 465 auto N = ExprCursor.peek(); 466 if (SubRegisterSizeInBits && N && (N->getOp() != dwarf::DW_OP_LLVM_fragment)) 467 maskSubRegister(); 468 469 Optional<DIExpression::ExprOperand> PrevConvertOp = None; 470 471 while (ExprCursor) { 472 auto Op = ExprCursor.take(); 473 uint64_t OpNum = Op->getOp(); 474 475 if (OpNum >= dwarf::DW_OP_reg0 && OpNum <= dwarf::DW_OP_reg31) { 476 emitOp(OpNum); 477 continue; 478 } else if (OpNum >= dwarf::DW_OP_breg0 && OpNum <= dwarf::DW_OP_breg31) { 479 addBReg(OpNum - dwarf::DW_OP_breg0, Op->getArg(0)); 480 continue; 481 } 482 483 switch (OpNum) { 484 case dwarf::DW_OP_LLVM_fragment: { 485 unsigned SizeInBits = Op->getArg(1); 486 unsigned FragmentOffset = Op->getArg(0); 487 // The fragment offset must have already been adjusted by emitting an 488 // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base 489 // location. 490 assert(OffsetInBits >= FragmentOffset && "fragment offset not added?"); 491 assert(SizeInBits >= OffsetInBits - FragmentOffset && "size underflow"); 492 493 // If addMachineReg already emitted DW_OP_piece operations to represent 494 // a super-register by splicing together sub-registers, subtract the size 495 // of the pieces that was already emitted. 496 SizeInBits -= OffsetInBits - FragmentOffset; 497 498 // If addMachineReg requested a DW_OP_bit_piece to stencil out a 499 // sub-register that is smaller than the current fragment's size, use it. 500 if (SubRegisterSizeInBits) 501 SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits); 502 503 // Emit a DW_OP_stack_value for implicit location descriptions. 504 if (isImplicitLocation()) 505 addStackValue(); 506 507 // Emit the DW_OP_piece. 508 addOpPiece(SizeInBits, SubRegisterOffsetInBits); 509 setSubRegisterPiece(0, 0); 510 // Reset the location description kind. 511 LocationKind = Unknown; 512 return; 513 } 514 case dwarf::DW_OP_plus_uconst: 515 assert(!isRegisterLocation()); 516 emitOp(dwarf::DW_OP_plus_uconst); 517 emitUnsigned(Op->getArg(0)); 518 break; 519 case dwarf::DW_OP_plus: 520 case dwarf::DW_OP_minus: 521 case dwarf::DW_OP_mul: 522 case dwarf::DW_OP_div: 523 case dwarf::DW_OP_mod: 524 case dwarf::DW_OP_or: 525 case dwarf::DW_OP_and: 526 case dwarf::DW_OP_xor: 527 case dwarf::DW_OP_shl: 528 case dwarf::DW_OP_shr: 529 case dwarf::DW_OP_shra: 530 case dwarf::DW_OP_lit0: 531 case dwarf::DW_OP_not: 532 case dwarf::DW_OP_dup: 533 case dwarf::DW_OP_push_object_address: 534 case dwarf::DW_OP_over: 535 emitOp(OpNum); 536 break; 537 case dwarf::DW_OP_deref: 538 assert(!isRegisterLocation()); 539 if (!isMemoryLocation() && ::isMemoryLocation(ExprCursor)) 540 // Turning this into a memory location description makes the deref 541 // implicit. 542 LocationKind = Memory; 543 else 544 emitOp(dwarf::DW_OP_deref); 545 break; 546 case dwarf::DW_OP_constu: 547 assert(!isRegisterLocation()); 548 emitConstu(Op->getArg(0)); 549 break; 550 case dwarf::DW_OP_consts: 551 assert(!isRegisterLocation()); 552 emitOp(dwarf::DW_OP_consts); 553 emitSigned(Op->getArg(0)); 554 break; 555 case dwarf::DW_OP_LLVM_convert: { 556 unsigned BitSize = Op->getArg(0); 557 dwarf::TypeKind Encoding = static_cast<dwarf::TypeKind>(Op->getArg(1)); 558 if (DwarfVersion >= 5 && CU.getDwarfDebug().useOpConvert()) { 559 emitOp(dwarf::DW_OP_convert); 560 // If targeting a location-list; simply emit the index into the raw 561 // byte stream as ULEB128, DwarfDebug::emitDebugLocEntry has been 562 // fitted with means to extract it later. 563 // If targeting a inlined DW_AT_location; insert a DIEBaseTypeRef 564 // (containing the index and a resolve mechanism during emit) into the 565 // DIE value list. 566 emitBaseTypeRef(getOrCreateBaseType(BitSize, Encoding)); 567 } else { 568 if (PrevConvertOp && PrevConvertOp->getArg(0) < BitSize) { 569 if (Encoding == dwarf::DW_ATE_signed) 570 emitLegacySExt(PrevConvertOp->getArg(0)); 571 else if (Encoding == dwarf::DW_ATE_unsigned) 572 emitLegacyZExt(PrevConvertOp->getArg(0)); 573 PrevConvertOp = None; 574 } else { 575 PrevConvertOp = Op; 576 } 577 } 578 break; 579 } 580 case dwarf::DW_OP_stack_value: 581 LocationKind = Implicit; 582 break; 583 case dwarf::DW_OP_swap: 584 assert(!isRegisterLocation()); 585 emitOp(dwarf::DW_OP_swap); 586 break; 587 case dwarf::DW_OP_xderef: 588 assert(!isRegisterLocation()); 589 emitOp(dwarf::DW_OP_xderef); 590 break; 591 case dwarf::DW_OP_deref_size: 592 emitOp(dwarf::DW_OP_deref_size); 593 emitData1(Op->getArg(0)); 594 break; 595 case dwarf::DW_OP_LLVM_tag_offset: 596 TagOffset = Op->getArg(0); 597 break; 598 case dwarf::DW_OP_regx: 599 emitOp(dwarf::DW_OP_regx); 600 emitUnsigned(Op->getArg(0)); 601 break; 602 case dwarf::DW_OP_bregx: 603 emitOp(dwarf::DW_OP_bregx); 604 emitUnsigned(Op->getArg(0)); 605 emitSigned(Op->getArg(1)); 606 break; 607 default: 608 llvm_unreachable("unhandled opcode found in expression"); 609 } 610 } 611 612 if (isImplicitLocation() && !isParameterValue()) 613 // Turn this into an implicit location description. 614 addStackValue(); 615 } 616 617 /// add masking operations to stencil out a subregister. 618 void DwarfExpression::maskSubRegister() { 619 assert(SubRegisterSizeInBits && "no subregister was registered"); 620 if (SubRegisterOffsetInBits > 0) 621 addShr(SubRegisterOffsetInBits); 622 uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL; 623 addAnd(Mask); 624 } 625 626 void DwarfExpression::finalize() { 627 assert(DwarfRegs.size() == 0 && "dwarf registers not emitted"); 628 // Emit any outstanding DW_OP_piece operations to mask out subregisters. 629 if (SubRegisterSizeInBits == 0) 630 return; 631 // Don't emit a DW_OP_piece for a subregister at offset 0. 632 if (SubRegisterOffsetInBits == 0) 633 return; 634 addOpPiece(SubRegisterSizeInBits, SubRegisterOffsetInBits); 635 } 636 637 void DwarfExpression::addFragmentOffset(const DIExpression *Expr) { 638 if (!Expr || !Expr->isFragment()) 639 return; 640 641 uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits; 642 assert(FragmentOffset >= OffsetInBits && 643 "overlapping or duplicate fragments"); 644 if (FragmentOffset > OffsetInBits) 645 addOpPiece(FragmentOffset - OffsetInBits); 646 OffsetInBits = FragmentOffset; 647 } 648 649 void DwarfExpression::emitLegacySExt(unsigned FromBits) { 650 // (((X >> (FromBits - 1)) * (~0)) << FromBits) | X 651 emitOp(dwarf::DW_OP_dup); 652 emitOp(dwarf::DW_OP_constu); 653 emitUnsigned(FromBits - 1); 654 emitOp(dwarf::DW_OP_shr); 655 emitOp(dwarf::DW_OP_lit0); 656 emitOp(dwarf::DW_OP_not); 657 emitOp(dwarf::DW_OP_mul); 658 emitOp(dwarf::DW_OP_constu); 659 emitUnsigned(FromBits); 660 emitOp(dwarf::DW_OP_shl); 661 emitOp(dwarf::DW_OP_or); 662 } 663 664 void DwarfExpression::emitLegacyZExt(unsigned FromBits) { 665 // (X & (1 << FromBits - 1)) 666 emitOp(dwarf::DW_OP_constu); 667 emitUnsigned((1ULL << FromBits) - 1); 668 emitOp(dwarf::DW_OP_and); 669 } 670 671 void DwarfExpression::addWasmLocation(unsigned Index, uint64_t Offset) { 672 assert(LocationKind == Implicit || LocationKind == Unknown); 673 LocationKind = Implicit; 674 emitOp(dwarf::DW_OP_WASM_location); 675 emitUnsigned(Index); 676 emitUnsigned(Offset); 677 } 678