137c52310SDavid Blaikie #include "DwarfCompileUnit.h" 27813d9c9SAdrian Prantl #include "DwarfExpression.h" 3cda2aa82SDavid Blaikie #include "llvm/CodeGen/MachineFunction.h" 4364a3005SDuncan P. N. Exon Smith #include "llvm/IR/Constants.h" 537c52310SDavid Blaikie #include "llvm/IR/DataLayout.h" 637c52310SDavid Blaikie #include "llvm/IR/GlobalValue.h" 737c52310SDavid Blaikie #include "llvm/IR/GlobalVariable.h" 837c52310SDavid Blaikie #include "llvm/IR/Instruction.h" 937c52310SDavid Blaikie #include "llvm/MC/MCAsmInfo.h" 1037c52310SDavid Blaikie #include "llvm/MC/MCStreamer.h" 11ee7df553SDavid Blaikie #include "llvm/Target/TargetFrameLowering.h" 1237c52310SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h" 13cda2aa82SDavid Blaikie #include "llvm/Target/TargetMachine.h" 14cda2aa82SDavid Blaikie #include "llvm/Target/TargetRegisterInfo.h" 15d9903888SChandler Carruth #include "llvm/Target/TargetSubtargetInfo.h" 1637c52310SDavid Blaikie 1737c52310SDavid Blaikie namespace llvm { 1837c52310SDavid Blaikie 19a9308c49SDuncan P. N. Exon Smith DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, 2037c52310SDavid Blaikie AsmPrinter *A, DwarfDebug *DW, 2137c52310SDavid Blaikie DwarfFile *DWU) 227cbf58afSDavid Blaikie : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU), 23063d725fSRafael Espindola Skeleton(nullptr), BaseAddress(nullptr) { 2437c52310SDavid Blaikie insertDIE(Node, &getUnitDie()); 2537c52310SDavid Blaikie } 2637c52310SDavid Blaikie 2737c52310SDavid Blaikie /// addLabelAddress - Add a dwarf label attribute data and value using 2837c52310SDavid Blaikie /// DW_FORM_addr or DW_FORM_GNU_addr_index. 2937c52310SDavid Blaikie /// 3037c52310SDavid Blaikie void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 3137c52310SDavid Blaikie const MCSymbol *Label) { 3237c52310SDavid Blaikie 3337c52310SDavid Blaikie // Don't use the address pool in non-fission or in the skeleton unit itself. 3437c52310SDavid Blaikie // FIXME: Once GDB supports this, it's probably worthwhile using the address 3537c52310SDavid Blaikie // pool from the skeleton - maybe even in non-fission (possibly fewer 3637c52310SDavid Blaikie // relocations by sharing them in the pool, but we have other ideas about how 3737c52310SDavid Blaikie // to reduce the number of relocations as well/instead). 3837c52310SDavid Blaikie if (!DD->useSplitDwarf() || !Skeleton) 3937c52310SDavid Blaikie return addLocalLabelAddress(Die, Attribute, Label); 4037c52310SDavid Blaikie 4137c52310SDavid Blaikie if (Label) 4237c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Label)); 4337c52310SDavid Blaikie 4437c52310SDavid Blaikie unsigned idx = DD->getAddressPool().getIndex(Label); 454fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_GNU_addr_index, 464fb1f9cdSDuncan P. N. Exon Smith DIEInteger(idx)); 4737c52310SDavid Blaikie } 4837c52310SDavid Blaikie 4937c52310SDavid Blaikie void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 5037c52310SDavid Blaikie dwarf::Attribute Attribute, 5137c52310SDavid Blaikie const MCSymbol *Label) { 5237c52310SDavid Blaikie if (Label) 5337c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Label)); 5437c52310SDavid Blaikie 55815a6eb5SDuncan P. N. Exon Smith if (Label) 564fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 574fb1f9cdSDuncan P. N. Exon Smith DIELabel(Label)); 58815a6eb5SDuncan P. N. Exon Smith else 594fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 604fb1f9cdSDuncan P. N. Exon Smith DIEInteger(0)); 6137c52310SDavid Blaikie } 6237c52310SDavid Blaikie 638945219dSDavid Blaikie unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, 648945219dSDavid Blaikie StringRef DirName) { 6537c52310SDavid Blaikie // If we print assembly, we can't separate .file entries according to 6637c52310SDavid Blaikie // compile units. Thus all files will belong to the default compile unit. 6737c52310SDavid Blaikie 6837c52310SDavid Blaikie // FIXME: add a better feature test than hasRawTextSupport. Even better, 6937c52310SDavid Blaikie // extend .file to support this. 709ff69c8fSLang Hames return Asm->OutStreamer->EmitDwarfFileDirective( 7137c52310SDavid Blaikie 0, DirName, FileName, 729ff69c8fSLang Hames Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID()); 7337c52310SDavid Blaikie } 7437c52310SDavid Blaikie 7537c52310SDavid Blaikie // Return const expression if value is a GEP to access merged global 7637c52310SDavid Blaikie // constant. e.g. 7737c52310SDavid Blaikie // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) 7837c52310SDavid Blaikie static const ConstantExpr *getMergedGlobalExpr(const Value *V) { 7937c52310SDavid Blaikie const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V); 8037c52310SDavid Blaikie if (!CE || CE->getNumOperands() != 3 || 8137c52310SDavid Blaikie CE->getOpcode() != Instruction::GetElementPtr) 8237c52310SDavid Blaikie return nullptr; 8337c52310SDavid Blaikie 8437c52310SDavid Blaikie // First operand points to a global struct. 8537c52310SDavid Blaikie Value *Ptr = CE->getOperand(0); 8637c52310SDavid Blaikie if (!isa<GlobalValue>(Ptr) || 8737c52310SDavid Blaikie !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType())) 8837c52310SDavid Blaikie return nullptr; 8937c52310SDavid Blaikie 9037c52310SDavid Blaikie // Second operand is zero. 9137c52310SDavid Blaikie const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1)); 9237c52310SDavid Blaikie if (!CI || !CI->isZero()) 9337c52310SDavid Blaikie return nullptr; 9437c52310SDavid Blaikie 9537c52310SDavid Blaikie // Third operand is offset. 9637c52310SDavid Blaikie if (!isa<ConstantInt>(CE->getOperand(2))) 9737c52310SDavid Blaikie return nullptr; 9837c52310SDavid Blaikie 9937c52310SDavid Blaikie return CE; 10037c52310SDavid Blaikie } 10137c52310SDavid Blaikie 10237c52310SDavid Blaikie /// getOrCreateGlobalVariableDIE - get or create global variable DIE. 10360635e39SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( 104d51dea67SDavid Blaikie const DIGlobalVariable *GV) { 10537c52310SDavid Blaikie // Check for pre-existence. 10637c52310SDavid Blaikie if (DIE *Die = getDIE(GV)) 10737c52310SDavid Blaikie return Die; 10837c52310SDavid Blaikie 109e686f159SDuncan P. N. Exon Smith assert(GV); 11037c52310SDavid Blaikie 111be9e4fe7SDuncan P. N. Exon Smith auto *GVContext = GV->getScope(); 112be9e4fe7SDuncan P. N. Exon Smith auto *GTy = DD->resolve(GV->getType()); 11337c52310SDavid Blaikie 11437c52310SDavid Blaikie // Construct the context before querying for the existence of the DIE in 11537c52310SDavid Blaikie // case such construction creates the DIE. 116d51dea67SDavid Blaikie DIE *ContextDIE = getOrCreateContextDIE(GVContext); 11737c52310SDavid Blaikie 11837c52310SDavid Blaikie // Add to map. 1197348ddaaSDuncan P. N. Exon Smith DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); 120a9308c49SDuncan P. N. Exon Smith DIScope *DeclContext; 121b1055640SDuncan P. N. Exon Smith if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { 122b1055640SDuncan P. N. Exon Smith DeclContext = resolve(SDMDecl->getScope()); 123b1055640SDuncan P. N. Exon Smith assert(SDMDecl->isStaticMember() && "Expected static member decl"); 1247348ddaaSDuncan P. N. Exon Smith assert(GV->isDefinition()); 12549cfc8caSDavid Blaikie // We need the declaration DIE that is in the static member's class. 12649cfc8caSDavid Blaikie DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); 12749cfc8caSDavid Blaikie addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); 12849cfc8caSDavid Blaikie } else { 1297348ddaaSDuncan P. N. Exon Smith DeclContext = GV->getScope(); 13037c52310SDavid Blaikie // Add name and type. 1317348ddaaSDuncan P. N. Exon Smith addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); 13237c52310SDavid Blaikie addType(*VariableDIE, GTy); 13337c52310SDavid Blaikie 13437c52310SDavid Blaikie // Add scoping info. 1357348ddaaSDuncan P. N. Exon Smith if (!GV->isLocalToUnit()) 13637c52310SDavid Blaikie addFlag(*VariableDIE, dwarf::DW_AT_external); 13737c52310SDavid Blaikie 13837c52310SDavid Blaikie // Add line number info. 13937c52310SDavid Blaikie addSourceLine(*VariableDIE, GV); 14037c52310SDavid Blaikie } 14137c52310SDavid Blaikie 1427348ddaaSDuncan P. N. Exon Smith if (!GV->isDefinition()) 14349cfc8caSDavid Blaikie addFlag(*VariableDIE, dwarf::DW_AT_declaration); 144877354a2SDavid Blaikie else 145877354a2SDavid Blaikie addGlobalName(GV->getName(), *VariableDIE, DeclContext); 14649cfc8caSDavid Blaikie 14737c52310SDavid Blaikie // Add location. 14837c52310SDavid Blaikie bool addToAccelTable = false; 1497348ddaaSDuncan P. N. Exon Smith if (auto *Global = dyn_cast_or_null<GlobalVariable>(GV->getVariable())) { 15037c52310SDavid Blaikie addToAccelTable = true; 151e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 152cca5f68eSDuncan P. N. Exon Smith const MCSymbol *Sym = Asm->getSymbol(Global); 153cca5f68eSDuncan P. N. Exon Smith if (Global->isThreadLocal()) { 1541e859582SChih-Hung Hsieh if (Asm->TM.Options.EmulatedTLS) { 1551e859582SChih-Hung Hsieh // TODO: add debug info for emulated thread local mode. 1561e859582SChih-Hung Hsieh } else { 15737c52310SDavid Blaikie // FIXME: Make this work with -gsplit-dwarf. 15837c52310SDavid Blaikie unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 15937c52310SDavid Blaikie assert((PointerSize == 4 || PointerSize == 8) && 16037c52310SDavid Blaikie "Add support for other sizes if necessary"); 16137c52310SDavid Blaikie // Based on GCC's support for TLS: 16237c52310SDavid Blaikie if (!DD->useSplitDwarf()) { 16337c52310SDavid Blaikie // 1) Start with a constNu of the appropriate pointer size 16437c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_data1, 16537c52310SDavid Blaikie PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); 16637c52310SDavid Blaikie // 2) containing the (relocated) offset of the TLS variable 16737c52310SDavid Blaikie // within the module's TLS block. 16837c52310SDavid Blaikie addExpr(*Loc, dwarf::DW_FORM_udata, 16937c52310SDavid Blaikie Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 17037c52310SDavid Blaikie } else { 17137c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 17237c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_udata, 17337c52310SDavid Blaikie DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 17437c52310SDavid Blaikie } 17578cc0821SPaul Robinson // 3) followed by an OP to make the debugger do a TLS lookup. 17678cc0821SPaul Robinson addUInt(*Loc, dwarf::DW_FORM_data1, 17778cc0821SPaul Robinson DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address 17878cc0821SPaul Robinson : dwarf::DW_OP_form_tls_address); 1791e859582SChih-Hung Hsieh } 18037c52310SDavid Blaikie } else { 18137c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Sym)); 18237c52310SDavid Blaikie addOpAddress(*Loc, Sym); 18337c52310SDavid Blaikie } 18449cfc8caSDavid Blaikie 18537c52310SDavid Blaikie addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 1867348ddaaSDuncan P. N. Exon Smith addLinkageName(*VariableDIE, GV->getLinkageName()); 18737c52310SDavid Blaikie } else if (const ConstantInt *CI = 1887348ddaaSDuncan P. N. Exon Smith dyn_cast_or_null<ConstantInt>(GV->getVariable())) { 18937c52310SDavid Blaikie addConstantValue(*VariableDIE, CI, GTy); 1907348ddaaSDuncan P. N. Exon Smith } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getVariable())) { 19137c52310SDavid Blaikie addToAccelTable = true; 19237c52310SDavid Blaikie // GV is a merged global. 193e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 19437c52310SDavid Blaikie Value *Ptr = CE->getOperand(0); 19537c52310SDavid Blaikie MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr)); 19637c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Sym)); 19737c52310SDavid Blaikie addOpAddress(*Loc, Sym); 19837c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); 19937c52310SDavid Blaikie SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end()); 20037c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_udata, 20137c52310SDavid Blaikie Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); 20237c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); 20337c52310SDavid Blaikie addBlock(*VariableDIE, dwarf::DW_AT_location, Loc); 20437c52310SDavid Blaikie } 20537c52310SDavid Blaikie 20637c52310SDavid Blaikie if (addToAccelTable) { 2077348ddaaSDuncan P. N. Exon Smith DD->addAccelName(GV->getName(), *VariableDIE); 20837c52310SDavid Blaikie 20937c52310SDavid Blaikie // If the linkage name is different than the name, go ahead and output 21037c52310SDavid Blaikie // that as well into the name table. 2117348ddaaSDuncan P. N. Exon Smith if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName()) 2127348ddaaSDuncan P. N. Exon Smith DD->addAccelName(GV->getLinkageName(), *VariableDIE); 21337c52310SDavid Blaikie } 21437c52310SDavid Blaikie 21549cfc8caSDavid Blaikie return VariableDIE; 21637c52310SDavid Blaikie } 21737c52310SDavid Blaikie 21837c52310SDavid Blaikie void DwarfCompileUnit::addRange(RangeSpan Range) { 21937c52310SDavid Blaikie bool SameAsPrevCU = this == DD->getPrevCU(); 22037c52310SDavid Blaikie DD->setPrevCU(this); 22137c52310SDavid Blaikie // If we have no current ranges just add the range and return, otherwise, 22237c52310SDavid Blaikie // check the current section and CU against the previous section and CU we 22337c52310SDavid Blaikie // emitted into and the subprogram was contained within. If these are the 22437c52310SDavid Blaikie // same then extend our current range, otherwise add this as a new range. 22537c52310SDavid Blaikie if (CURanges.empty() || !SameAsPrevCU || 22637c52310SDavid Blaikie (&CURanges.back().getEnd()->getSection() != 22737c52310SDavid Blaikie &Range.getEnd()->getSection())) { 22837c52310SDavid Blaikie CURanges.push_back(Range); 22937c52310SDavid Blaikie return; 23037c52310SDavid Blaikie } 23137c52310SDavid Blaikie 23237c52310SDavid Blaikie CURanges.back().setEnd(Range.getEnd()); 23337c52310SDavid Blaikie } 23437c52310SDavid Blaikie 2354fb1f9cdSDuncan P. N. Exon Smith DIE::value_iterator 2364fb1f9cdSDuncan P. N. Exon Smith DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 2374fb1f9cdSDuncan P. N. Exon Smith const MCSymbol *Label, const MCSymbol *Sec) { 2386c0ee4ecSDavid Blaikie if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 2394fb1f9cdSDuncan P. N. Exon Smith return addLabel(Die, Attribute, 2406c0ee4ecSDavid Blaikie DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 2416c0ee4ecSDavid Blaikie : dwarf::DW_FORM_data4, 2426c0ee4ecSDavid Blaikie Label); 2434fb1f9cdSDuncan P. N. Exon Smith return addSectionDelta(Die, Attribute, Label, Sec); 2446c0ee4ecSDavid Blaikie } 2456c0ee4ecSDavid Blaikie 246063d725fSRafael Espindola void DwarfCompileUnit::initStmtList() { 24737c52310SDavid Blaikie // Define start line table label for each Compile Unit. 24837c52310SDavid Blaikie MCSymbol *LineTableStartSym = 2499ff69c8fSLang Hames Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); 25037c52310SDavid Blaikie 25137c52310SDavid Blaikie // DW_AT_stmt_list is a offset of line number information for this 25237c52310SDavid Blaikie // compile unit in debug_line section. For split dwarf this is 25337c52310SDavid Blaikie // left in the skeleton CU and so not included. 25437c52310SDavid Blaikie // The line table entries are not always emitted in assembly, so it 25537c52310SDavid Blaikie // is not okay to use line_table_start here. 256063d725fSRafael Espindola const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 2574fb1f9cdSDuncan P. N. Exon Smith StmtListValue = 2586c0ee4ecSDavid Blaikie addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym, 259063d725fSRafael Espindola TLOF.getDwarfLineSection()->getBeginSymbol()); 26037c52310SDavid Blaikie } 26137c52310SDavid Blaikie 26237c52310SDavid Blaikie void DwarfCompileUnit::applyStmtList(DIE &D) { 2634fb1f9cdSDuncan P. N. Exon Smith D.addValue(DIEValueAllocator, *StmtListValue); 26437c52310SDavid Blaikie } 26537c52310SDavid Blaikie 26614499a7dSDavid Blaikie void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, 26714499a7dSDavid Blaikie const MCSymbol *End) { 26814499a7dSDavid Blaikie assert(Begin && "Begin label should not be null!"); 26914499a7dSDavid Blaikie assert(End && "End label should not be null!"); 27014499a7dSDavid Blaikie assert(Begin->isDefined() && "Invalid starting label"); 27114499a7dSDavid Blaikie assert(End->isDefined() && "Invalid end label"); 27214499a7dSDavid Blaikie 27314499a7dSDavid Blaikie addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); 27414499a7dSDavid Blaikie if (DD->getDwarfVersion() < 4) 27514499a7dSDavid Blaikie addLabelAddress(D, dwarf::DW_AT_high_pc, End); 27614499a7dSDavid Blaikie else 27714499a7dSDavid Blaikie addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); 27814499a7dSDavid Blaikie } 27914499a7dSDavid Blaikie 280cda2aa82SDavid Blaikie // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc 281cda2aa82SDavid Blaikie // and DW_AT_high_pc attributes. If there are global variables in this 282cda2aa82SDavid Blaikie // scope then create and insert DIEs for these variables. 283a9308c49SDuncan P. N. Exon Smith DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { 2843a443c29SDavid Blaikie DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); 285cda2aa82SDavid Blaikie 28607c03d31SRafael Espindola attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd()); 287cda2aa82SDavid Blaikie if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( 288cda2aa82SDavid Blaikie *DD->getCurrentFunction())) 289cda2aa82SDavid Blaikie addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); 290cda2aa82SDavid Blaikie 291cda2aa82SDavid Blaikie // Only include DW_AT_frame_base in full debug info 2923a443c29SDavid Blaikie if (!includeMinimalInlineScopes()) { 293d4e723f2SEric Christopher const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo(); 294cda2aa82SDavid Blaikie MachineLocation Location(RI->getFrameRegister(*Asm->MF)); 2958efadbf8SAdrian Prantl if (RI->isPhysicalRegister(Location.getReg())) 296cda2aa82SDavid Blaikie addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); 297cda2aa82SDavid Blaikie } 298cda2aa82SDavid Blaikie 299cda2aa82SDavid Blaikie // Add name to the name table, we do this here because we're guaranteed 300cda2aa82SDavid Blaikie // to have concrete versions of our DW_TAG_subprogram nodes. 301cda2aa82SDavid Blaikie DD->addSubprogramNames(SP, *SPDie); 302cda2aa82SDavid Blaikie 303cda2aa82SDavid Blaikie return *SPDie; 304cda2aa82SDavid Blaikie } 305cda2aa82SDavid Blaikie 3069c65b135SDavid Blaikie // Construct a DIE for this scope. 3079c65b135SDavid Blaikie void DwarfCompileUnit::constructScopeDIE( 308827200c8SDuncan P. N. Exon Smith LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { 3099c65b135SDavid Blaikie if (!Scope || !Scope->getScopeNode()) 3109c65b135SDavid Blaikie return; 3119c65b135SDavid Blaikie 312be9e4fe7SDuncan P. N. Exon Smith auto *DS = Scope->getScopeNode(); 3139c65b135SDavid Blaikie 314a9308c49SDuncan P. N. Exon Smith assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && 3159c65b135SDavid Blaikie "Only handle inlined subprograms here, use " 3169c65b135SDavid Blaikie "constructSubprogramScopeDIE for non-inlined " 3179c65b135SDavid Blaikie "subprograms"); 3189c65b135SDavid Blaikie 319827200c8SDuncan P. N. Exon Smith SmallVector<DIE *, 8> Children; 3209c65b135SDavid Blaikie 3219c65b135SDavid Blaikie // We try to create the scope DIE first, then the children DIEs. This will 3229c65b135SDavid Blaikie // avoid creating un-used children then removing them later when we find out 3239c65b135SDavid Blaikie // the scope DIE is null. 324827200c8SDuncan P. N. Exon Smith DIE *ScopeDIE; 325a9308c49SDuncan P. N. Exon Smith if (Scope->getParent() && isa<DISubprogram>(DS)) { 32601b48a84SDavid Blaikie ScopeDIE = constructInlinedScopeDIE(Scope); 3279c65b135SDavid Blaikie if (!ScopeDIE) 3289c65b135SDavid Blaikie return; 3299c65b135SDavid Blaikie // We create children when the scope DIE is not null. 3308b2fdb83SDavid Blaikie createScopeChildrenDIE(Scope, Children); 3319c65b135SDavid Blaikie } else { 3329c65b135SDavid Blaikie // Early exit when we know the scope DIE is going to be null. 3339c65b135SDavid Blaikie if (DD->isLexicalScopeDIENull(Scope)) 3349c65b135SDavid Blaikie return; 3359c65b135SDavid Blaikie 3369c65b135SDavid Blaikie unsigned ChildScopeCount; 3379c65b135SDavid Blaikie 3389c65b135SDavid Blaikie // We create children here when we know the scope DIE is not going to be 3399c65b135SDavid Blaikie // null and the children will be added to the scope DIE. 3408b2fdb83SDavid Blaikie createScopeChildrenDIE(Scope, Children, &ChildScopeCount); 3419c65b135SDavid Blaikie 342d51dea67SDavid Blaikie // Skip imported directives in gmlt-like data. 343d51dea67SDavid Blaikie if (!includeMinimalInlineScopes()) { 344d51dea67SDavid Blaikie // There is no need to emit empty lexical block DIE. 345298639a5SIvan Krasin for (const auto *IE : ImportedEntities[DS]) 346d51dea67SDavid Blaikie Children.push_back( 347298639a5SIvan Krasin constructImportedEntityDIE(cast<DIImportedEntity>(IE))); 348d51dea67SDavid Blaikie } 3493a443c29SDavid Blaikie 3509c65b135SDavid Blaikie // If there are only other scopes as children, put them directly in the 3519c65b135SDavid Blaikie // parent instead, as this scope would serve no purpose. 352d51dea67SDavid Blaikie if (Children.size() == ChildScopeCount) { 3539c65b135SDavid Blaikie FinalChildren.insert(FinalChildren.end(), 3549c65b135SDavid Blaikie std::make_move_iterator(Children.begin()), 3559c65b135SDavid Blaikie std::make_move_iterator(Children.end())); 3569c65b135SDavid Blaikie return; 3579c65b135SDavid Blaikie } 3580fbf8bdbSDavid Blaikie ScopeDIE = constructLexicalScopeDIE(Scope); 3599c65b135SDavid Blaikie assert(ScopeDIE && "Scope DIE should not be null."); 3609c65b135SDavid Blaikie } 3619c65b135SDavid Blaikie 3629c65b135SDavid Blaikie // Add children 3639c65b135SDavid Blaikie for (auto &I : Children) 3649c65b135SDavid Blaikie ScopeDIE->addChild(std::move(I)); 3659c65b135SDavid Blaikie 3669c65b135SDavid Blaikie FinalChildren.push_back(std::move(ScopeDIE)); 3679c65b135SDavid Blaikie } 3689c65b135SDavid Blaikie 3694fb1f9cdSDuncan P. N. Exon Smith DIE::value_iterator 3704fb1f9cdSDuncan P. N. Exon Smith DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 371e5feec50SDavid Blaikie const MCSymbol *Hi, const MCSymbol *Lo) { 3724fb1f9cdSDuncan P. N. Exon Smith return Die.addValue(DIEValueAllocator, Attribute, 3734fb1f9cdSDuncan P. N. Exon Smith DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 374e5feec50SDavid Blaikie : dwarf::DW_FORM_data4, 375e7e1d0c7SDuncan P. N. Exon Smith new (DIEValueAllocator) DIEDelta(Hi, Lo)); 376e5feec50SDavid Blaikie } 377e5feec50SDavid Blaikie 3785b02a19fSDavid Blaikie void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, 3795b02a19fSDavid Blaikie SmallVector<RangeSpan, 2> Range) { 380063d725fSRafael Espindola const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 381063d725fSRafael Espindola 38252400200SDavid Blaikie // Emit offset in .debug_range as a relocatable label. emitDIE will handle 38352400200SDavid Blaikie // emitting it appropriately. 384063d725fSRafael Espindola const MCSymbol *RangeSectionSym = 385063d725fSRafael Espindola TLOF.getDwarfRangesSection()->getBeginSymbol(); 38652400200SDavid Blaikie 3879ab09237SRafael Espindola RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range)); 3885b02a19fSDavid Blaikie 38952400200SDavid Blaikie // Under fission, ranges are specified by constant offsets relative to the 39052400200SDavid Blaikie // CU's DW_AT_GNU_ranges_base. 3915b02a19fSDavid Blaikie if (isDwoUnit()) 3925b02a19fSDavid Blaikie addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), 3935b02a19fSDavid Blaikie RangeSectionSym); 39452400200SDavid Blaikie else 3955b02a19fSDavid Blaikie addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), 3965b02a19fSDavid Blaikie RangeSectionSym); 39752400200SDavid Blaikie 39852400200SDavid Blaikie // Add the range list to the set of ranges to be emitted. 399542616d4SDavid Blaikie (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List)); 40052400200SDavid Blaikie } 40152400200SDavid Blaikie 402de12375cSDavid Blaikie void DwarfCompileUnit::attachRangesOrLowHighPC( 4035b02a19fSDavid Blaikie DIE &Die, SmallVector<RangeSpan, 2> Ranges) { 4045b02a19fSDavid Blaikie if (Ranges.size() == 1) { 4055b02a19fSDavid Blaikie const auto &single = Ranges.front(); 4065b02a19fSDavid Blaikie attachLowHighPC(Die, single.getStart(), single.getEnd()); 4075b02a19fSDavid Blaikie } else 4085b02a19fSDavid Blaikie addScopeRangeList(Die, std::move(Ranges)); 4095b02a19fSDavid Blaikie } 4105b02a19fSDavid Blaikie 4115b02a19fSDavid Blaikie void DwarfCompileUnit::attachRangesOrLowHighPC( 412de12375cSDavid Blaikie DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { 4135b02a19fSDavid Blaikie SmallVector<RangeSpan, 2> List; 4145b02a19fSDavid Blaikie List.reserve(Ranges.size()); 4155b02a19fSDavid Blaikie for (const InsnRange &R : Ranges) 4165b02a19fSDavid Blaikie List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first), 4175b02a19fSDavid Blaikie DD->getLabelAfterInsn(R.second))); 4185b02a19fSDavid Blaikie attachRangesOrLowHighPC(Die, std::move(List)); 419de12375cSDavid Blaikie } 420de12375cSDavid Blaikie 42101b48a84SDavid Blaikie // This scope represents inlined body of a function. Construct DIE to 42201b48a84SDavid Blaikie // represent this concrete inlined copy of the function. 423827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { 42401b48a84SDavid Blaikie assert(Scope->getScopeNode()); 425be9e4fe7SDuncan P. N. Exon Smith auto *DS = Scope->getScopeNode(); 426be9e4fe7SDuncan P. N. Exon Smith auto *InlinedSP = getDISubprogram(DS); 42701b48a84SDavid Blaikie // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 42801b48a84SDavid Blaikie // was inlined from another compile unit. 429f6dac29aSDavid Blaikie DIE *OriginDIE = DU->getAbstractSPDies()[InlinedSP]; 43001b48a84SDavid Blaikie assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); 43101b48a84SDavid Blaikie 432827200c8SDuncan P. N. Exon Smith auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); 43301b48a84SDavid Blaikie addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); 43401b48a84SDavid Blaikie 43501b48a84SDavid Blaikie attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 43601b48a84SDavid Blaikie 43701b48a84SDavid Blaikie // Add the call site information to the DIE. 438a9308c49SDuncan P. N. Exon Smith const DILocation *IA = Scope->getInlinedAt(); 43901b48a84SDavid Blaikie addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, 440b7e221baSDuncan P. N. Exon Smith getOrCreateSourceID(IA->getFilename(), IA->getDirectory())); 441b7e221baSDuncan P. N. Exon Smith addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); 442*72fdf444SDehao Chen if (IA->getDiscriminator()) 443*72fdf444SDehao Chen addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, IA->getDiscriminator()); 44401b48a84SDavid Blaikie 44501b48a84SDavid Blaikie // Add name to the name table, we do this here because we're guaranteed 44601b48a84SDavid Blaikie // to have concrete versions of our DW_TAG_inlined_subprogram nodes. 44701b48a84SDavid Blaikie DD->addSubprogramNames(InlinedSP, *ScopeDIE); 44801b48a84SDavid Blaikie 44901b48a84SDavid Blaikie return ScopeDIE; 45001b48a84SDavid Blaikie } 45101b48a84SDavid Blaikie 4520fbf8bdbSDavid Blaikie // Construct new DW_TAG_lexical_block for this scope and attach 4530fbf8bdbSDavid Blaikie // DW_AT_low_pc/DW_AT_high_pc labels. 454827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { 4550fbf8bdbSDavid Blaikie if (DD->isLexicalScopeDIENull(Scope)) 4560fbf8bdbSDavid Blaikie return nullptr; 4570fbf8bdbSDavid Blaikie 458827200c8SDuncan P. N. Exon Smith auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); 4590fbf8bdbSDavid Blaikie if (Scope->isAbstractScope()) 4600fbf8bdbSDavid Blaikie return ScopeDIE; 4610fbf8bdbSDavid Blaikie 4620fbf8bdbSDavid Blaikie attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 4630fbf8bdbSDavid Blaikie 4640fbf8bdbSDavid Blaikie return ScopeDIE; 4650fbf8bdbSDavid Blaikie } 4660fbf8bdbSDavid Blaikie 467ee7df553SDavid Blaikie /// constructVariableDIE - Construct a DIE for the given DbgVariable. 468827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { 469ee7df553SDavid Blaikie auto D = constructVariableDIEImpl(DV, Abstract); 470ee7df553SDavid Blaikie DV.setDIE(*D); 471ee7df553SDavid Blaikie return D; 472ee7df553SDavid Blaikie } 473ee7df553SDavid Blaikie 474827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, 475ee7df553SDavid Blaikie bool Abstract) { 476ee7df553SDavid Blaikie // Define variable debug information entry. 477827200c8SDuncan P. N. Exon Smith auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); 478ee7df553SDavid Blaikie 479ee7df553SDavid Blaikie if (Abstract) { 480ee7df553SDavid Blaikie applyVariableAttributes(DV, *VariableDie); 481ee7df553SDavid Blaikie return VariableDie; 482ee7df553SDavid Blaikie } 483ee7df553SDavid Blaikie 484ee7df553SDavid Blaikie // Add variable address. 485ee7df553SDavid Blaikie 486364a3005SDuncan P. N. Exon Smith unsigned Offset = DV.getDebugLocListIndex(); 487ee7df553SDavid Blaikie if (Offset != ~0U) { 488ee7df553SDavid Blaikie addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); 489ee7df553SDavid Blaikie return VariableDie; 490ee7df553SDavid Blaikie } 491ee7df553SDavid Blaikie 492ee7df553SDavid Blaikie // Check if variable is described by a DBG_VALUE instruction. 493ee7df553SDavid Blaikie if (const MachineInstr *DVInsn = DV.getMInsn()) { 494ee7df553SDavid Blaikie assert(DVInsn->getNumOperands() == 4); 495ee7df553SDavid Blaikie if (DVInsn->getOperand(0).isReg()) { 496ee7df553SDavid Blaikie const MachineOperand RegOp = DVInsn->getOperand(0); 497ee7df553SDavid Blaikie // If the second operand is an immediate, this is an indirect value. 498ee7df553SDavid Blaikie if (DVInsn->getOperand(1).isImm()) { 499ee7df553SDavid Blaikie MachineLocation Location(RegOp.getReg(), 500ee7df553SDavid Blaikie DVInsn->getOperand(1).getImm()); 501ee7df553SDavid Blaikie addVariableAddress(DV, *VariableDie, Location); 502ee7df553SDavid Blaikie } else if (RegOp.getReg()) 503ee7df553SDavid Blaikie addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); 504ee7df553SDavid Blaikie } else if (DVInsn->getOperand(0).isImm()) 505ee7df553SDavid Blaikie addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); 506ee7df553SDavid Blaikie else if (DVInsn->getOperand(0).isFPImm()) 507ee7df553SDavid Blaikie addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); 508ee7df553SDavid Blaikie else if (DVInsn->getOperand(0).isCImm()) 509ee7df553SDavid Blaikie addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), 510ee7df553SDavid Blaikie DV.getType()); 511ee7df553SDavid Blaikie 512ee7df553SDavid Blaikie return VariableDie; 513ee7df553SDavid Blaikie } 514ee7df553SDavid Blaikie 515ee7df553SDavid Blaikie // .. else use frame index. 516e6cc531bSDuncan P. N. Exon Smith if (DV.getFrameIndex().empty()) 517ca7e4702SAdrian Prantl return VariableDie; 518ca7e4702SAdrian Prantl 519ca7e4702SAdrian Prantl auto Expr = DV.getExpression().begin(); 520e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 521ca7e4702SAdrian Prantl DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 522ca7e4702SAdrian Prantl for (auto FI : DV.getFrameIndex()) { 523ee7df553SDavid Blaikie unsigned FrameReg = 0; 524d4e723f2SEric Christopher const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); 525ee7df553SDavid Blaikie int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); 52657bab0bcSDuncan P. N. Exon Smith assert(Expr != DV.getExpression().end() && 52757bab0bcSDuncan P. N. Exon Smith "Wrong number of expressions"); 528ca7e4702SAdrian Prantl DwarfExpr.AddMachineRegIndirect(FrameReg, Offset); 52976c91844SDuncan P. N. Exon Smith DwarfExpr.AddExpression((*Expr)->expr_op_begin(), (*Expr)->expr_op_end()); 53057bab0bcSDuncan P. N. Exon Smith ++Expr; 531ee7df553SDavid Blaikie } 532ca7e4702SAdrian Prantl addBlock(*VariableDie, dwarf::DW_AT_location, Loc); 533ee7df553SDavid Blaikie 534ee7df553SDavid Blaikie return VariableDie; 535ee7df553SDavid Blaikie } 536ee7df553SDavid Blaikie 537827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, 538827200c8SDuncan P. N. Exon Smith const LexicalScope &Scope, 539827200c8SDuncan P. N. Exon Smith DIE *&ObjectPointer) { 5404a1a44e3SDavid Blaikie auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); 5414a1a44e3SDavid Blaikie if (DV.isObjectPointer()) 542827200c8SDuncan P. N. Exon Smith ObjectPointer = Var; 5434a1a44e3SDavid Blaikie return Var; 5444a1a44e3SDavid Blaikie } 5454a1a44e3SDavid Blaikie 546827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, 547827200c8SDuncan P. N. Exon Smith SmallVectorImpl<DIE *> &Children, 5488b2fdb83SDavid Blaikie unsigned *ChildScopeCount) { 5498b2fdb83SDavid Blaikie DIE *ObjectPointer = nullptr; 5508b2fdb83SDavid Blaikie 55180e5b1ebSDavid Blaikie for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope)) 5528b2fdb83SDavid Blaikie Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); 5538b2fdb83SDavid Blaikie 5548b2fdb83SDavid Blaikie unsigned ChildCountWithoutScopes = Children.size(); 5558b2fdb83SDavid Blaikie 5568b2fdb83SDavid Blaikie for (LexicalScope *LS : Scope->getChildren()) 5578b2fdb83SDavid Blaikie constructScopeDIE(LS, Children); 5588b2fdb83SDavid Blaikie 5598b2fdb83SDavid Blaikie if (ChildScopeCount) 5608b2fdb83SDavid Blaikie *ChildScopeCount = Children.size() - ChildCountWithoutScopes; 5618b2fdb83SDavid Blaikie 5628b2fdb83SDavid Blaikie return ObjectPointer; 5638b2fdb83SDavid Blaikie } 5648b2fdb83SDavid Blaikie 5651d072348SDavid Blaikie void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) { 5661d072348SDavid Blaikie assert(Scope && Scope->getScopeNode()); 5671d072348SDavid Blaikie assert(!Scope->getInlinedAt()); 5681d072348SDavid Blaikie assert(!Scope->isAbstractScope()); 569a9308c49SDuncan P. N. Exon Smith auto *Sub = cast<DISubprogram>(Scope->getScopeNode()); 5701d072348SDavid Blaikie 5711d072348SDavid Blaikie DD->getProcessedSPNodes().insert(Sub); 5721d072348SDavid Blaikie 5731d072348SDavid Blaikie DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); 5741d072348SDavid Blaikie 5751d072348SDavid Blaikie // If this is a variadic function, add an unspecified parameter. 576a9308c49SDuncan P. N. Exon Smith DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); 5771dd573dbSDavid Blaikie 5781dd573dbSDavid Blaikie // Collect lexical scope children first. 5791dd573dbSDavid Blaikie // ObjectPointer might be a local (non-argument) local variable if it's a 5801dd573dbSDavid Blaikie // block's synthetic this pointer. 5811dd573dbSDavid Blaikie if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) 5821dd573dbSDavid Blaikie addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); 5831dd573dbSDavid Blaikie 5841d072348SDavid Blaikie // If we have a single element of null, it is a function that returns void. 5851d072348SDavid Blaikie // If we have more than one elements and the last one is null, it is a 5861d072348SDavid Blaikie // variadic function. 587000fa2c6SDuncan P. N. Exon Smith if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && 5883a443c29SDavid Blaikie !includeMinimalInlineScopes()) 589827200c8SDuncan P. N. Exon Smith ScopeDIE.addChild( 590827200c8SDuncan P. N. Exon Smith DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); 5911d072348SDavid Blaikie } 5921d072348SDavid Blaikie 59378b65b6fSDavid Blaikie DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, 59478b65b6fSDavid Blaikie DIE &ScopeDIE) { 59578b65b6fSDavid Blaikie // We create children when the scope DIE is not null. 596827200c8SDuncan P. N. Exon Smith SmallVector<DIE *, 8> Children; 59778b65b6fSDavid Blaikie DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); 59878b65b6fSDavid Blaikie 59978b65b6fSDavid Blaikie // Add children 60078b65b6fSDavid Blaikie for (auto &I : Children) 60178b65b6fSDavid Blaikie ScopeDIE.addChild(std::move(I)); 60278b65b6fSDavid Blaikie 60378b65b6fSDavid Blaikie return ObjectPointer; 60478b65b6fSDavid Blaikie } 60578b65b6fSDavid Blaikie 60649be5b35SDavid Blaikie void 60758410f24SDavid Blaikie DwarfCompileUnit::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) { 608f6dac29aSDavid Blaikie DIE *&AbsDef = DU->getAbstractSPDies()[Scope->getScopeNode()]; 60949be5b35SDavid Blaikie if (AbsDef) 61049be5b35SDavid Blaikie return; 61149be5b35SDavid Blaikie 612a9308c49SDuncan P. N. Exon Smith auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 61358410f24SDavid Blaikie 61458410f24SDavid Blaikie DIE *ContextDIE; 61558410f24SDavid Blaikie 6163a443c29SDavid Blaikie if (includeMinimalInlineScopes()) 6173a443c29SDavid Blaikie ContextDIE = &getUnitDie(); 61858410f24SDavid Blaikie // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with 6197c60f20eSDuncan P. N. Exon Smith // the important distinction that the debug node is not associated with the 6207c60f20eSDuncan P. N. Exon Smith // DIE (since the debug node will be associated with the concrete DIE, if 62158410f24SDavid Blaikie // any). It could be refactored to some common utility function. 622537b4a81SDuncan P. N. Exon Smith else if (auto *SPDecl = SP->getDeclaration()) { 62358410f24SDavid Blaikie ContextDIE = &getUnitDie(); 62458410f24SDavid Blaikie getOrCreateSubprogramDIE(SPDecl); 62558410f24SDavid Blaikie } else 626537b4a81SDuncan P. N. Exon Smith ContextDIE = getOrCreateContextDIE(resolve(SP->getScope())); 62758410f24SDavid Blaikie 6287c60f20eSDuncan P. N. Exon Smith // Passing null as the associated node because the abstract definition 62958410f24SDavid Blaikie // shouldn't be found by lookup. 6307c60f20eSDuncan P. N. Exon Smith AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); 63149be5b35SDavid Blaikie applySubprogramAttributesToDefinition(SP, *AbsDef); 63258410f24SDavid Blaikie 6333a443c29SDavid Blaikie if (!includeMinimalInlineScopes()) 63449be5b35SDavid Blaikie addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); 63549be5b35SDavid Blaikie if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef)) 63649be5b35SDavid Blaikie addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); 63758410f24SDavid Blaikie } 63858410f24SDavid Blaikie 639827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructImportedEntityDIE( 640827200c8SDuncan P. N. Exon Smith const DIImportedEntity *Module) { 641827200c8SDuncan P. N. Exon Smith DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); 642827200c8SDuncan P. N. Exon Smith insertDIE(Module, IMDie); 643987fe227SFrederic Riss DIE *EntityDie; 644de8e4273SDuncan P. N. Exon Smith auto *Entity = resolve(Module->getEntity()); 645a9308c49SDuncan P. N. Exon Smith if (auto *NS = dyn_cast<DINamespace>(Entity)) 646e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateNameSpace(NS); 64708a388baSAdrian Prantl else if (auto *M = dyn_cast<DIModule>(Entity)) 64808a388baSAdrian Prantl EntityDie = getOrCreateModule(M); 649a9308c49SDuncan P. N. Exon Smith else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 650e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateSubprogramDIE(SP); 651a9308c49SDuncan P. N. Exon Smith else if (auto *T = dyn_cast<DIType>(Entity)) 652e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateTypeDIE(T); 653a9308c49SDuncan P. N. Exon Smith else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) 654e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateGlobalVariableDIE(GV); 655987fe227SFrederic Riss else 656987fe227SFrederic Riss EntityDie = getDIE(Entity); 657987fe227SFrederic Riss assert(EntityDie); 658de8e4273SDuncan P. N. Exon Smith addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(), 659de8e4273SDuncan P. N. Exon Smith Module->getScope()->getDirectory()); 660987fe227SFrederic Riss addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); 661de8e4273SDuncan P. N. Exon Smith StringRef Name = Module->getName(); 662987fe227SFrederic Riss if (!Name.empty()) 663987fe227SFrederic Riss addString(*IMDie, dwarf::DW_AT_name, Name); 664987fe227SFrederic Riss 665987fe227SFrederic Riss return IMDie; 666987fe227SFrederic Riss } 667987fe227SFrederic Riss 668a9308c49SDuncan P. N. Exon Smith void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { 6694191cbceSDavid Blaikie DIE *D = getDIE(SP); 670f6dac29aSDavid Blaikie if (DIE *AbsSPDIE = DU->getAbstractSPDies().lookup(SP)) { 6714191cbceSDavid Blaikie if (D) 6724191cbceSDavid Blaikie // If this subprogram has an abstract definition, reference that 6734191cbceSDavid Blaikie addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); 6744191cbceSDavid Blaikie } else { 6753a443c29SDavid Blaikie if (!D && !includeMinimalInlineScopes()) 6764191cbceSDavid Blaikie // Lazily construct the subprogram if we didn't see either concrete or 6774191cbceSDavid Blaikie // inlined versions during codegen. (except in -gmlt ^ where we want 6784191cbceSDavid Blaikie // to omit these entirely) 6794191cbceSDavid Blaikie D = getOrCreateSubprogramDIE(SP); 6804191cbceSDavid Blaikie if (D) 6814191cbceSDavid Blaikie // And attach the attributes 6824191cbceSDavid Blaikie applySubprogramAttributesToDefinition(SP, *D); 6834191cbceSDavid Blaikie } 6844191cbceSDavid Blaikie } 685a9308c49SDuncan P. N. Exon Smith void DwarfCompileUnit::collectDeadVariables(const DISubprogram *SP) { 686e686f159SDuncan P. N. Exon Smith assert(SP && "CU's subprogram list contains a non-subprogram"); 687537b4a81SDuncan P. N. Exon Smith assert(SP->isDefinition() && 6881d96cc2fSDavid Blaikie "CU's subprogram list contains a subprogram declaration"); 689000fa2c6SDuncan P. N. Exon Smith auto Variables = SP->getVariables(); 690000fa2c6SDuncan P. N. Exon Smith if (Variables.size() == 0) 6911d96cc2fSDavid Blaikie return; 6921d96cc2fSDavid Blaikie 693f6dac29aSDavid Blaikie DIE *SPDIE = DU->getAbstractSPDies().lookup(SP); 6941d96cc2fSDavid Blaikie if (!SPDIE) 6951d96cc2fSDavid Blaikie SPDIE = getDIE(SP); 6961d96cc2fSDavid Blaikie assert(SPDIE); 697a9308c49SDuncan P. N. Exon Smith for (const DILocalVariable *DV : Variables) { 698e6cc531bSDuncan P. N. Exon Smith DbgVariable NewVar(DV, /* IA */ nullptr, DD); 6991d96cc2fSDavid Blaikie auto VariableDie = constructVariableDIE(NewVar); 7001d96cc2fSDavid Blaikie applyVariableAttributes(NewVar, *VariableDie); 7011d96cc2fSDavid Blaikie SPDIE->addChild(std::move(VariableDie)); 7021d96cc2fSDavid Blaikie } 7031d96cc2fSDavid Blaikie } 7044191cbceSDavid Blaikie 705063d725fSRafael Espindola void DwarfCompileUnit::emitHeader(bool UseOffsets) { 706b6726a9eSDavid Blaikie // Don't bother labeling the .dwo unit, as its offset isn't used. 7073c311148SRafael Espindola if (!Skeleton) { 7089ab09237SRafael Espindola LabelBegin = Asm->createTempSymbol("cu_begin"); 7099ff69c8fSLang Hames Asm->OutStreamer->EmitLabel(LabelBegin); 7103c311148SRafael Espindola } 711ae57e66eSDavid Blaikie 712063d725fSRafael Espindola DwarfUnit::emitHeader(UseOffsets); 713ae57e66eSDavid Blaikie } 714ae57e66eSDavid Blaikie 715192b45c1SDavid Blaikie /// addGlobalName - Add a new global name to the compile unit. 716192b45c1SDavid Blaikie void DwarfCompileUnit::addGlobalName(StringRef Name, DIE &Die, 717a9308c49SDuncan P. N. Exon Smith const DIScope *Context) { 7183a443c29SDavid Blaikie if (includeMinimalInlineScopes()) 719192b45c1SDavid Blaikie return; 720192b45c1SDavid Blaikie std::string FullName = getParentContextString(Context) + Name.str(); 721192b45c1SDavid Blaikie GlobalNames[FullName] = &Die; 722192b45c1SDavid Blaikie } 723192b45c1SDavid Blaikie 724192b45c1SDavid Blaikie /// Add a new global type to the unit. 725a9308c49SDuncan P. N. Exon Smith void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, 726a9308c49SDuncan P. N. Exon Smith const DIScope *Context) { 7273a443c29SDavid Blaikie if (includeMinimalInlineScopes()) 728192b45c1SDavid Blaikie return; 729b1055640SDuncan P. N. Exon Smith std::string FullName = getParentContextString(Context) + Ty->getName().str(); 730192b45c1SDavid Blaikie GlobalTypes[FullName] = &Die; 731192b45c1SDavid Blaikie } 732ae57e66eSDavid Blaikie 7337d48be2bSDavid Blaikie /// addVariableAddress - Add DW_AT_location attribute for a 7347d48be2bSDavid Blaikie /// DbgVariable based on provided MachineLocation. 7357d48be2bSDavid Blaikie void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, 7367d48be2bSDavid Blaikie MachineLocation Location) { 737e6cc531bSDuncan P. N. Exon Smith if (DV.hasComplexAddress()) 7387d48be2bSDavid Blaikie addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 7397d48be2bSDavid Blaikie else if (DV.isBlockByrefVariable()) 7407d48be2bSDavid Blaikie addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 7417d48be2bSDavid Blaikie else 7425883af3fSAdrian Prantl addAddress(Die, dwarf::DW_AT_location, Location); 7437d48be2bSDavid Blaikie } 744f7435ee6SDavid Blaikie 745f7435ee6SDavid Blaikie /// Add an address attribute to a die based on the location provided. 746f7435ee6SDavid Blaikie void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, 7475883af3fSAdrian Prantl const MachineLocation &Location) { 748e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 749f7435ee6SDavid Blaikie 750ab255fcdSAdrian Prantl bool validReg; 7515883af3fSAdrian Prantl if (Location.isReg()) 752ab255fcdSAdrian Prantl validReg = addRegisterOpPiece(*Loc, Location.getReg()); 753ab255fcdSAdrian Prantl else 754ab255fcdSAdrian Prantl validReg = addRegisterOffset(*Loc, Location.getReg(), Location.getOffset()); 755ab255fcdSAdrian Prantl 756ab255fcdSAdrian Prantl if (!validReg) 757ab255fcdSAdrian Prantl return; 758ab255fcdSAdrian Prantl 759f7435ee6SDavid Blaikie // Now attach the location information to the DIE. 760f7435ee6SDavid Blaikie addBlock(Die, Attribute, Loc); 761f7435ee6SDavid Blaikie } 76277895fb2SDavid Blaikie 76377895fb2SDavid Blaikie /// Start with the address based on the location provided, and generate the 76477895fb2SDavid Blaikie /// DWARF information necessary to find the actual variable given the extra 76577895fb2SDavid Blaikie /// address information encoded in the DbgVariable, starting from the starting 76677895fb2SDavid Blaikie /// location. Add the DWARF information to the die. 76777895fb2SDavid Blaikie void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, 76877895fb2SDavid Blaikie dwarf::Attribute Attribute, 76977895fb2SDavid Blaikie const MachineLocation &Location) { 770e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 7717813d9c9SAdrian Prantl DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 772ca7e4702SAdrian Prantl assert(DV.getExpression().size() == 1); 773a9308c49SDuncan P. N. Exon Smith const DIExpression *Expr = DV.getExpression().back(); 77440cb819cSAdrian Prantl bool ValidReg; 7757813d9c9SAdrian Prantl if (Location.getOffset()) { 77640cb819cSAdrian Prantl ValidReg = DwarfExpr.AddMachineRegIndirect(Location.getReg(), 77740cb819cSAdrian Prantl Location.getOffset()); 77840cb819cSAdrian Prantl if (ValidReg) 77976c91844SDuncan P. N. Exon Smith DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end()); 7805883af3fSAdrian Prantl } else 78140cb819cSAdrian Prantl ValidReg = DwarfExpr.AddMachineRegExpression(Expr, Location.getReg()); 78277895fb2SDavid Blaikie 78377895fb2SDavid Blaikie // Now attach the location information to the DIE. 78440cb819cSAdrian Prantl if (ValidReg) 78577895fb2SDavid Blaikie addBlock(Die, Attribute, Loc); 78677895fb2SDavid Blaikie } 7874bc0881aSDavid Blaikie 7884bc0881aSDavid Blaikie /// Add a Dwarf loclistptr attribute data and value. 7894bc0881aSDavid Blaikie void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, 7904bc0881aSDavid Blaikie unsigned Index) { 7914bc0881aSDavid Blaikie dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 7924bc0881aSDavid Blaikie : dwarf::DW_FORM_data4; 7934fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); 7944bc0881aSDavid Blaikie } 79502a6333bSDavid Blaikie 7968c485b5dSDavid Blaikie void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, 79702a6333bSDavid Blaikie DIE &VariableDie) { 79802a6333bSDavid Blaikie StringRef Name = Var.getName(); 79902a6333bSDavid Blaikie if (!Name.empty()) 80002a6333bSDavid Blaikie addString(VariableDie, dwarf::DW_AT_name, Name); 80102a6333bSDavid Blaikie addSourceLine(VariableDie, Var.getVariable()); 80202a6333bSDavid Blaikie addType(VariableDie, Var.getType()); 80302a6333bSDavid Blaikie if (Var.isArtificial()) 80402a6333bSDavid Blaikie addFlag(VariableDie, dwarf::DW_AT_artificial); 80502a6333bSDavid Blaikie } 80697802080SDavid Blaikie 80797802080SDavid Blaikie /// Add a Dwarf expression attribute data and value. 80897802080SDavid Blaikie void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, 80997802080SDavid Blaikie const MCExpr *Expr) { 8104fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); 81197802080SDavid Blaikie } 8123363a57cSDavid Blaikie 8132fbe1354SDuncan P. N. Exon Smith void DwarfCompileUnit::applySubprogramAttributesToDefinition( 814a9308c49SDuncan P. N. Exon Smith const DISubprogram *SP, DIE &SPDie) { 815537b4a81SDuncan P. N. Exon Smith auto *SPDecl = SP->getDeclaration(); 816be9e4fe7SDuncan P. N. Exon Smith auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope()); 8173a443c29SDavid Blaikie applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); 818537b4a81SDuncan P. N. Exon Smith addGlobalName(SP->getName(), SPDie, Context); 8193363a57cSDavid Blaikie } 820cafd962dSDavid Blaikie 821cafd962dSDavid Blaikie bool DwarfCompileUnit::isDwoUnit() const { 822cafd962dSDavid Blaikie return DD->useSplitDwarf() && Skeleton; 823cafd962dSDavid Blaikie } 8243a443c29SDavid Blaikie 8253a443c29SDavid Blaikie bool DwarfCompileUnit::includeMinimalInlineScopes() const { 82635ef22cfSDuncan P. N. Exon Smith return getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly || 8273a443c29SDavid Blaikie (DD->useSplitDwarf() && !Skeleton); 8283a443c29SDavid Blaikie } 829f00654e3SAlexander Kornienko } // end llvm namespace 830