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