1 #include "DwarfCompileUnit.h" 2 3 #include "llvm/CodeGen/MachineFunction.h" 4 #include "llvm/IR/DataLayout.h" 5 #include "llvm/IR/GlobalValue.h" 6 #include "llvm/IR/GlobalVariable.h" 7 #include "llvm/IR/Instruction.h" 8 #include "llvm/MC/MCAsmInfo.h" 9 #include "llvm/MC/MCStreamer.h" 10 #include "llvm/Target/TargetFrameLowering.h" 11 #include "llvm/Target/TargetLoweringObjectFile.h" 12 #include "llvm/Target/TargetMachine.h" 13 #include "llvm/Target/TargetSubtargetInfo.h" 14 #include "llvm/Target/TargetRegisterInfo.h" 15 16 namespace llvm { 17 18 DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node, 19 AsmPrinter *A, DwarfDebug *DW, 20 DwarfFile *DWU) 21 : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) { 22 insertDIE(Node, &getUnitDie()); 23 } 24 25 /// addLabelAddress - Add a dwarf label attribute data and value using 26 /// DW_FORM_addr or DW_FORM_GNU_addr_index. 27 /// 28 void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 29 const MCSymbol *Label) { 30 31 // Don't use the address pool in non-fission or in the skeleton unit itself. 32 // FIXME: Once GDB supports this, it's probably worthwhile using the address 33 // pool from the skeleton - maybe even in non-fission (possibly fewer 34 // relocations by sharing them in the pool, but we have other ideas about how 35 // to reduce the number of relocations as well/instead). 36 if (!DD->useSplitDwarf() || !Skeleton) 37 return addLocalLabelAddress(Die, Attribute, Label); 38 39 if (Label) 40 DD->addArangeLabel(SymbolCU(this, Label)); 41 42 unsigned idx = DD->getAddressPool().getIndex(Label); 43 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); 44 Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); 45 } 46 47 void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 48 dwarf::Attribute Attribute, 49 const MCSymbol *Label) { 50 if (Label) 51 DD->addArangeLabel(SymbolCU(this, Label)); 52 53 Die.addValue(Attribute, dwarf::DW_FORM_addr, 54 Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label) 55 : new (DIEValueAllocator) DIEInteger(0)); 56 } 57 58 unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, 59 StringRef DirName) { 60 // If we print assembly, we can't separate .file entries according to 61 // compile units. Thus all files will belong to the default compile unit. 62 63 // FIXME: add a better feature test than hasRawTextSupport. Even better, 64 // extend .file to support this. 65 return Asm->OutStreamer.EmitDwarfFileDirective( 66 0, DirName, FileName, 67 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID()); 68 } 69 70 // Return const expression if value is a GEP to access merged global 71 // constant. e.g. 72 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) 73 static const ConstantExpr *getMergedGlobalExpr(const Value *V) { 74 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); 75 if (!CE || CE->getNumOperands() != 3 || 76 CE->getOpcode() != Instruction::GetElementPtr) 77 return nullptr; 78 79 // First operand points to a global struct. 80 Value *Ptr = CE->getOperand(0); 81 if (!isa<GlobalValue>(Ptr) || 82 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) 83 return nullptr; 84 85 // Second operand is zero. 86 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); 87 if (!CI || !CI->isZero()) 88 return nullptr; 89 90 // Third operand is offset. 91 if (!isa<ConstantInt>(CE->getOperand(2))) 92 return nullptr; 93 94 return CE; 95 } 96 97 /// getOrCreateGlobalVariableDIE - get or create global variable DIE. 98 DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) { 99 // Check for pre-existence. 100 if (DIE *Die = getDIE(GV)) 101 return Die; 102 103 assert(GV.isGlobalVariable()); 104 105 DIScope GVContext = GV.getContext(); 106 DIType GTy = DD->resolve(GV.getType()); 107 108 // If this is a static data member definition, some attributes belong 109 // to the declaration DIE. 110 DIE *VariableDIE = nullptr; 111 bool IsStaticMember = false; 112 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); 113 if (SDMDecl.Verify()) { 114 assert(SDMDecl.isStaticMember() && "Expected static member decl"); 115 // We need the declaration DIE that is in the static member's class. 116 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); 117 IsStaticMember = true; 118 } 119 120 // If this is not a static data member definition, create the variable 121 // DIE and add the initial set of attributes to it. 122 if (!VariableDIE) { 123 // Construct the context before querying for the existence of the DIE in 124 // case such construction creates the DIE. 125 DIE *ContextDIE = getOrCreateContextDIE(GVContext); 126 127 // Add to map. 128 VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV); 129 130 // Add name and type. 131 addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); 132 addType(*VariableDIE, GTy); 133 134 // Add scoping info. 135 if (!GV.isLocalToUnit()) 136 addFlag(*VariableDIE, dwarf::DW_AT_external); 137 138 // Add line number info. 139 addSourceLine(*VariableDIE, GV); 140 } 141 142 // Add location. 143 bool addToAccelTable = false; 144 DIE *VariableSpecDIE = nullptr; 145 bool isGlobalVariable = GV.getGlobal() != nullptr; 146 if (isGlobalVariable) { 147 addToAccelTable = true; 148 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 149 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); 150 if (GV.getGlobal()->isThreadLocal()) { 151 // FIXME: Make this work with -gsplit-dwarf. 152 unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 153 assert((PointerSize == 4 || PointerSize == 8) && 154 "Add support for other sizes if necessary"); 155 // Based on GCC's support for TLS: 156 if (!DD->useSplitDwarf()) { 157 // 1) Start with a constNu of the appropriate pointer size 158 addUInt(*Loc, dwarf::DW_FORM_data1, 159 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); 160 // 2) containing the (relocated) offset of the TLS variable 161 // within the module's TLS block. 162 addExpr(*Loc, dwarf::DW_FORM_udata, 163 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 164 } else { 165 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 166 addUInt(*Loc, dwarf::DW_FORM_udata, 167 DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 168 } 169 // 3) followed by a custom OP to make the debugger do a TLS lookup. 170 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); 171 } else { 172 DD->addArangeLabel(SymbolCU(this, Sym)); 173 addOpAddress(*Loc, Sym); 174 } 175 // A static member's declaration is already flagged as such. 176 if (!SDMDecl.Verify() && !GV.isDefinition()) 177 addFlag(*VariableDIE, dwarf::DW_AT_declaration); 178 // Do not create specification DIE if context is either compile unit 179 // or a subprogram. 180 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && 181 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { 182 // Create specification DIE. 183 VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie); 184 addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE); 185 addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc); 186 // A static member's declaration is already flagged as such. 187 if (!SDMDecl.Verify()) 188 addFlag(*VariableDIE, dwarf::DW_AT_declaration); 189 } else { 190 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 191 } 192 // Add the linkage name. 193 StringRef LinkageName = GV.getLinkageName(); 194 if (!LinkageName.empty()) 195 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: 196 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and 197 // TAG_variable. 198 addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE 199 : *VariableDIE, 200 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name 201 : dwarf::DW_AT_MIPS_linkage_name, 202 GlobalValue::getRealLinkageName(LinkageName)); 203 } else if (const ConstantInt *CI = 204 dyn_cast_or_null<ConstantInt>(GV.getConstant())) { 205 // AT_const_value was added when the static member was created. To avoid 206 // emitting AT_const_value multiple times, we only add AT_const_value when 207 // it is not a static member. 208 if (!IsStaticMember) 209 addConstantValue(*VariableDIE, CI, GTy); 210 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) { 211 addToAccelTable = true; 212 // GV is a merged global. 213 DIELoc *Loc = new (DIEValueAllocator) DIELoc(); 214 Value *Ptr = CE->getOperand(0); 215 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); 216 DD->addArangeLabel(SymbolCU(this, Sym)); 217 addOpAddress(*Loc, Sym); 218 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 219 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); 220 addUInt(*Loc, dwarf::DW_FORM_udata, 221 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); 222 addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 223 addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 224 } 225 226 DIE *ResultDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; 227 228 if (addToAccelTable) { 229 DD->addAccelName(GV.getName(), *ResultDIE); 230 231 // If the linkage name is different than the name, go ahead and output 232 // that as well into the name table. 233 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) 234 DD->addAccelName(GV.getLinkageName(), *ResultDIE); 235 } 236 237 addGlobalName(GV.getName(), *ResultDIE, GV.getContext()); 238 return ResultDIE; 239 } 240 241 void DwarfCompileUnit::addRange(RangeSpan Range) { 242 bool SameAsPrevCU = this == DD->getPrevCU(); 243 DD->setPrevCU(this); 244 // If we have no current ranges just add the range and return, otherwise, 245 // check the current section and CU against the previous section and CU we 246 // emitted into and the subprogram was contained within. If these are the 247 // same then extend our current range, otherwise add this as a new range. 248 if (CURanges.empty() || !SameAsPrevCU || 249 (&CURanges.back().getEnd()->getSection() != 250 &Range.getEnd()->getSection())) { 251 CURanges.push_back(Range); 252 return; 253 } 254 255 CURanges.back().setEnd(Range.getEnd()); 256 } 257 258 void DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 259 const MCSymbol *Label, 260 const MCSymbol *Sec) { 261 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 262 addLabel(Die, Attribute, 263 DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 264 : dwarf::DW_FORM_data4, 265 Label); 266 else 267 addSectionDelta(Die, Attribute, Label, Sec); 268 } 269 270 void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) { 271 // Define start line table label for each Compile Unit. 272 MCSymbol *LineTableStartSym = 273 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID()); 274 275 stmtListIndex = UnitDie.getValues().size(); 276 277 // DW_AT_stmt_list is a offset of line number information for this 278 // compile unit in debug_line section. For split dwarf this is 279 // left in the skeleton CU and so not included. 280 // The line table entries are not always emitted in assembly, so it 281 // is not okay to use line_table_start here. 282 addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, 283 DwarfLineSectionSym); 284 } 285 286 void DwarfCompileUnit::applyStmtList(DIE &D) { 287 D.addValue(dwarf::DW_AT_stmt_list, 288 UnitDie.getAbbrev().getData()[stmtListIndex].getForm(), 289 UnitDie.getValues()[stmtListIndex]); 290 } 291 292 void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, 293 const MCSymbol *End) { 294 assert(Begin && "Begin label should not be null!"); 295 assert(End && "End label should not be null!"); 296 assert(Begin->isDefined() && "Invalid starting label"); 297 assert(End->isDefined() && "Invalid end label"); 298 299 addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); 300 if (DD->getDwarfVersion() < 4) 301 addLabelAddress(D, dwarf::DW_AT_high_pc, End); 302 else 303 addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); 304 } 305 306 // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc 307 // and DW_AT_high_pc attributes. If there are global variables in this 308 // scope then create and insert DIEs for these variables. 309 DIE &DwarfCompileUnit::updateSubprogramScopeDIE(DISubprogram SP) { 310 DIE *SPDie = getOrCreateSubprogramDIE(SP); 311 312 attachLowHighPC(*SPDie, DD->getFunctionBeginSym(), DD->getFunctionEndSym()); 313 if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( 314 *DD->getCurrentFunction())) 315 addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); 316 317 // Only include DW_AT_frame_base in full debug info 318 if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) { 319 const TargetRegisterInfo *RI = 320 Asm->TM.getSubtargetImpl()->getRegisterInfo(); 321 MachineLocation Location(RI->getFrameRegister(*Asm->MF)); 322 addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); 323 } 324 325 // Add name to the name table, we do this here because we're guaranteed 326 // to have concrete versions of our DW_TAG_subprogram nodes. 327 DD->addSubprogramNames(SP, *SPDie); 328 329 return *SPDie; 330 } 331 332 // Construct a DIE for this scope. 333 void DwarfCompileUnit::constructScopeDIE( 334 LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren) { 335 if (!Scope || !Scope->getScopeNode()) 336 return; 337 338 DIScope DS(Scope->getScopeNode()); 339 340 assert((Scope->getInlinedAt() || !DS.isSubprogram()) && 341 "Only handle inlined subprograms here, use " 342 "constructSubprogramScopeDIE for non-inlined " 343 "subprograms"); 344 345 SmallVector<std::unique_ptr<DIE>, 8> Children; 346 347 // We try to create the scope DIE first, then the children DIEs. This will 348 // avoid creating un-used children then removing them later when we find out 349 // the scope DIE is null. 350 std::unique_ptr<DIE> ScopeDIE; 351 if (Scope->getParent() && DS.isSubprogram()) { 352 ScopeDIE = constructInlinedScopeDIE(Scope); 353 if (!ScopeDIE) 354 return; 355 // We create children when the scope DIE is not null. 356 createScopeChildrenDIE(Scope, Children); 357 } else { 358 // Early exit when we know the scope DIE is going to be null. 359 if (DD->isLexicalScopeDIENull(Scope)) 360 return; 361 362 unsigned ChildScopeCount; 363 364 // We create children here when we know the scope DIE is not going to be 365 // null and the children will be added to the scope DIE. 366 createScopeChildrenDIE(Scope, Children, &ChildScopeCount); 367 368 // There is no need to emit empty lexical block DIE. 369 for (const auto &E : DD->findImportedEntitiesForScope(DS)) 370 Children.push_back( 371 constructImportedEntityDIE(DIImportedEntity(E.second))); 372 // If there are only other scopes as children, put them directly in the 373 // parent instead, as this scope would serve no purpose. 374 if (Children.size() == ChildScopeCount) { 375 FinalChildren.insert(FinalChildren.end(), 376 std::make_move_iterator(Children.begin()), 377 std::make_move_iterator(Children.end())); 378 return; 379 } 380 ScopeDIE = constructLexicalScopeDIE(Scope); 381 assert(ScopeDIE && "Scope DIE should not be null."); 382 } 383 384 // Add children 385 for (auto &I : Children) 386 ScopeDIE->addChild(std::move(I)); 387 388 FinalChildren.push_back(std::move(ScopeDIE)); 389 } 390 391 void DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 392 const MCSymbol *Hi, const MCSymbol *Lo) { 393 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); 394 Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 395 : dwarf::DW_FORM_data4, 396 Value); 397 } 398 399 void 400 DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, 401 const SmallVectorImpl<InsnRange> &Range) { 402 // Emit offset in .debug_range as a relocatable label. emitDIE will handle 403 // emitting it appropriately. 404 MCSymbol *RangeSym = 405 Asm->GetTempSymbol("debug_ranges", DD->getNextRangeNumber()); 406 407 auto *RangeSectionSym = DD->getRangeSectionSym(); 408 409 // Under fission, ranges are specified by constant offsets relative to the 410 // CU's DW_AT_GNU_ranges_base. 411 if (DD->useSplitDwarf()) 412 addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym); 413 else 414 addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym); 415 416 RangeSpanList List(RangeSym); 417 for (const InsnRange &R : Range) 418 List.addRange(RangeSpan(DD->getLabelBeforeInsn(R.first), 419 DD->getLabelAfterInsn(R.second))); 420 421 // Add the range list to the set of ranges to be emitted. 422 addRangeList(std::move(List)); 423 } 424 425 void DwarfCompileUnit::attachRangesOrLowHighPC( 426 DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { 427 assert(!Ranges.empty()); 428 if (Ranges.size() == 1) 429 attachLowHighPC(Die, DD->getLabelBeforeInsn(Ranges.front().first), 430 DD->getLabelAfterInsn(Ranges.front().second)); 431 else 432 addScopeRangeList(Die, Ranges); 433 } 434 435 // This scope represents inlined body of a function. Construct DIE to 436 // represent this concrete inlined copy of the function. 437 std::unique_ptr<DIE> 438 DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { 439 assert(Scope->getScopeNode()); 440 DIScope DS(Scope->getScopeNode()); 441 DISubprogram InlinedSP = getDISubprogram(DS); 442 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 443 // was inlined from another compile unit. 444 DIE *OriginDIE = DD->getAbstractSPDies()[InlinedSP]; 445 assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); 446 447 auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_inlined_subroutine); 448 addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); 449 450 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 451 452 // Add the call site information to the DIE. 453 DILocation DL(Scope->getInlinedAt()); 454 addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, 455 getOrCreateSourceID(DL.getFilename(), DL.getDirectory())); 456 addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber()); 457 458 // Add name to the name table, we do this here because we're guaranteed 459 // to have concrete versions of our DW_TAG_inlined_subprogram nodes. 460 DD->addSubprogramNames(InlinedSP, *ScopeDIE); 461 462 return ScopeDIE; 463 } 464 465 // Construct new DW_TAG_lexical_block for this scope and attach 466 // DW_AT_low_pc/DW_AT_high_pc labels. 467 std::unique_ptr<DIE> 468 DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { 469 if (DD->isLexicalScopeDIENull(Scope)) 470 return nullptr; 471 472 auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_lexical_block); 473 if (Scope->isAbstractScope()) 474 return ScopeDIE; 475 476 attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 477 478 return ScopeDIE; 479 } 480 481 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 482 std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, 483 bool Abstract) { 484 auto D = constructVariableDIEImpl(DV, Abstract); 485 DV.setDIE(*D); 486 return D; 487 } 488 489 std::unique_ptr<DIE> 490 DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, 491 bool Abstract) { 492 // Define variable debug information entry. 493 auto VariableDie = make_unique<DIE>(DV.getTag()); 494 495 if (Abstract) { 496 applyVariableAttributes(DV, *VariableDie); 497 return VariableDie; 498 } 499 500 // Add variable address. 501 502 unsigned Offset = DV.getDotDebugLocOffset(); 503 if (Offset != ~0U) { 504 addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); 505 return VariableDie; 506 } 507 508 // Check if variable is described by a DBG_VALUE instruction. 509 if (const MachineInstr *DVInsn = DV.getMInsn()) { 510 assert(DVInsn->getNumOperands() == 4); 511 if (DVInsn->getOperand(0).isReg()) { 512 const MachineOperand RegOp = DVInsn->getOperand(0); 513 // If the second operand is an immediate, this is an indirect value. 514 if (DVInsn->getOperand(1).isImm()) { 515 MachineLocation Location(RegOp.getReg(), 516 DVInsn->getOperand(1).getImm()); 517 addVariableAddress(DV, *VariableDie, Location); 518 } else if (RegOp.getReg()) 519 addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); 520 } else if (DVInsn->getOperand(0).isImm()) 521 addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); 522 else if (DVInsn->getOperand(0).isFPImm()) 523 addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); 524 else if (DVInsn->getOperand(0).isCImm()) 525 addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), 526 DV.getType()); 527 528 return VariableDie; 529 } 530 531 // .. else use frame index. 532 int FI = DV.getFrameIndex(); 533 if (FI != ~0) { 534 unsigned FrameReg = 0; 535 const TargetFrameLowering *TFI = 536 Asm->TM.getSubtargetImpl()->getFrameLowering(); 537 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 538 MachineLocation Location(FrameReg, Offset); 539 addVariableAddress(DV, *VariableDie, Location); 540 } 541 542 return VariableDie; 543 } 544 545 std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE( 546 DbgVariable &DV, const LexicalScope &Scope, DIE *&ObjectPointer) { 547 auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); 548 if (DV.isObjectPointer()) 549 ObjectPointer = Var.get(); 550 return Var; 551 } 552 553 DIE *DwarfCompileUnit::createScopeChildrenDIE( 554 LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &Children, 555 unsigned *ChildScopeCount) { 556 DIE *ObjectPointer = nullptr; 557 558 for (DbgVariable *DV : DD->getScopeVariables().lookup(Scope)) 559 Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); 560 561 unsigned ChildCountWithoutScopes = Children.size(); 562 563 for (LexicalScope *LS : Scope->getChildren()) 564 constructScopeDIE(LS, Children); 565 566 if (ChildScopeCount) 567 *ChildScopeCount = Children.size() - ChildCountWithoutScopes; 568 569 return ObjectPointer; 570 } 571 572 void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) { 573 assert(Scope && Scope->getScopeNode()); 574 assert(!Scope->getInlinedAt()); 575 assert(!Scope->isAbstractScope()); 576 DISubprogram Sub(Scope->getScopeNode()); 577 578 assert(Sub.isSubprogram()); 579 580 DD->getProcessedSPNodes().insert(Sub); 581 582 DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); 583 584 // Collect arguments for current function. 585 DIE *ObjectPointer = nullptr; 586 for (DbgVariable *ArgDV : DD->getCurrentFnArguments()) 587 if (ArgDV) 588 ScopeDIE.addChild(constructVariableDIE(*ArgDV, *Scope, ObjectPointer)); 589 590 // If this is a variadic function, add an unspecified parameter. 591 DITypeArray FnArgs = Sub.getType().getTypeArray(); 592 // If we have a single element of null, it is a function that returns void. 593 // If we have more than one elements and the last one is null, it is a 594 // variadic function. 595 if (FnArgs.getNumElements() > 1 && 596 !FnArgs.getElement(FnArgs.getNumElements() - 1)) 597 ScopeDIE.addChild(make_unique<DIE>(dwarf::DW_TAG_unspecified_parameters)); 598 599 // Collect lexical scope children first. 600 // ObjectPointer might be a local (non-argument) local variable if it's a 601 // block's synthetic this pointer. 602 if (DIE *BlockObjPtr = createAndAddScopeChildren(Scope, ScopeDIE)) { 603 assert(!ObjectPointer && "multiple object pointers can't be described"); 604 ObjectPointer = BlockObjPtr; 605 } 606 607 if (ObjectPointer) 608 addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); 609 } 610 611 DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, 612 DIE &ScopeDIE) { 613 // We create children when the scope DIE is not null. 614 SmallVector<std::unique_ptr<DIE>, 8> Children; 615 DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); 616 617 // Add children 618 for (auto &I : Children) 619 ScopeDIE.addChild(std::move(I)); 620 621 return ObjectPointer; 622 } 623 624 DIE & 625 DwarfCompileUnit::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) { 626 DISubprogram SP(Scope->getScopeNode()); 627 628 DIE *ContextDIE; 629 630 // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with 631 // the important distinction that the DIDescriptor is not associated with the 632 // DIE (since the DIDescriptor will be associated with the concrete DIE, if 633 // any). It could be refactored to some common utility function. 634 if (DISubprogram SPDecl = SP.getFunctionDeclaration()) { 635 ContextDIE = &getUnitDie(); 636 getOrCreateSubprogramDIE(SPDecl); 637 } else 638 ContextDIE = getOrCreateContextDIE(resolve(SP.getContext())); 639 640 // Passing null as the associated DIDescriptor because the abstract definition 641 // shouldn't be found by lookup. 642 DIE &AbsDef = 643 createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, DIDescriptor()); 644 applySubprogramAttributesToDefinition(SP, AbsDef); 645 646 if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) 647 addUInt(AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); 648 if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, AbsDef)) 649 addDIEEntry(AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); 650 return AbsDef; 651 } 652 653 void DwarfCompileUnit::finishSubprogramDefinition(DISubprogram SP) { 654 DIE *D = getDIE(SP); 655 if (DIE *AbsSPDIE = DD->getAbstractSPDies().lookup(SP)) { 656 if (D) 657 // If this subprogram has an abstract definition, reference that 658 addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); 659 } else { 660 if (!D && getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) 661 // Lazily construct the subprogram if we didn't see either concrete or 662 // inlined versions during codegen. (except in -gmlt ^ where we want 663 // to omit these entirely) 664 D = getOrCreateSubprogramDIE(SP); 665 if (D) 666 // And attach the attributes 667 applySubprogramAttributesToDefinition(SP, *D); 668 } 669 } 670 671 } // end llvm namespace 672