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