13443575cSPaul Robinson //===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Units -----------===// 23443575cSPaul Robinson // 33443575cSPaul Robinson // The LLVM Compiler Infrastructure 43443575cSPaul Robinson // 53443575cSPaul Robinson // This file is distributed under the University of Illinois Open Source 63443575cSPaul Robinson // License. See LICENSE.TXT for details. 73443575cSPaul Robinson // 83443575cSPaul Robinson //===----------------------------------------------------------------------===// 93443575cSPaul Robinson // 103443575cSPaul Robinson // This file contains support for constructing a dwarf compile unit. 113443575cSPaul Robinson // 123443575cSPaul Robinson //===----------------------------------------------------------------------===// 133443575cSPaul Robinson 1437c52310SDavid Blaikie #include "DwarfCompileUnit.h" 157813d9c9SAdrian Prantl #include "DwarfExpression.h" 16cda2aa82SDavid Blaikie #include "llvm/CodeGen/MachineFunction.h" 17364a3005SDuncan P. N. Exon Smith #include "llvm/IR/Constants.h" 1837c52310SDavid Blaikie #include "llvm/IR/DataLayout.h" 1937c52310SDavid Blaikie #include "llvm/IR/GlobalValue.h" 2037c52310SDavid Blaikie #include "llvm/IR/GlobalVariable.h" 2137c52310SDavid Blaikie #include "llvm/IR/Instruction.h" 2237c52310SDavid Blaikie #include "llvm/MC/MCAsmInfo.h" 2337c52310SDavid Blaikie #include "llvm/MC/MCStreamer.h" 24ee7df553SDavid Blaikie #include "llvm/Target/TargetFrameLowering.h" 2537c52310SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h" 26cda2aa82SDavid Blaikie #include "llvm/Target/TargetMachine.h" 27cda2aa82SDavid Blaikie #include "llvm/Target/TargetRegisterInfo.h" 28d9903888SChandler Carruth #include "llvm/Target/TargetSubtargetInfo.h" 2937c52310SDavid Blaikie 3037c52310SDavid Blaikie namespace llvm { 3137c52310SDavid Blaikie 32a9308c49SDuncan P. N. Exon Smith DwarfCompileUnit::DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, 3337c52310SDavid Blaikie AsmPrinter *A, DwarfDebug *DW, 3437c52310SDavid Blaikie DwarfFile *DWU) 357c384cceSPeter Collingbourne : DwarfUnit(dwarf::DW_TAG_compile_unit, Node, A, DW, DWU), UniqueID(UID), 36063d725fSRafael Espindola Skeleton(nullptr), BaseAddress(nullptr) { 3737c52310SDavid Blaikie insertDIE(Node, &getUnitDie()); 388bbce8adSAmjad Aboud MacroLabelBegin = Asm->createTempSymbol("cu_macro_begin"); 3937c52310SDavid Blaikie } 4037c52310SDavid Blaikie 4137c52310SDavid Blaikie /// addLabelAddress - Add a dwarf label attribute data and value using 4237c52310SDavid Blaikie /// DW_FORM_addr or DW_FORM_GNU_addr_index. 4337c52310SDavid Blaikie /// 4437c52310SDavid Blaikie void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 4537c52310SDavid Blaikie const MCSymbol *Label) { 4637c52310SDavid Blaikie 4737c52310SDavid Blaikie // Don't use the address pool in non-fission or in the skeleton unit itself. 4837c52310SDavid Blaikie // FIXME: Once GDB supports this, it's probably worthwhile using the address 4937c52310SDavid Blaikie // pool from the skeleton - maybe even in non-fission (possibly fewer 5037c52310SDavid Blaikie // relocations by sharing them in the pool, but we have other ideas about how 5137c52310SDavid Blaikie // to reduce the number of relocations as well/instead). 5237c52310SDavid Blaikie if (!DD->useSplitDwarf() || !Skeleton) 5337c52310SDavid Blaikie return addLocalLabelAddress(Die, Attribute, Label); 5437c52310SDavid Blaikie 5537c52310SDavid Blaikie if (Label) 5637c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Label)); 5737c52310SDavid Blaikie 5837c52310SDavid Blaikie unsigned idx = DD->getAddressPool().getIndex(Label); 594fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_GNU_addr_index, 604fb1f9cdSDuncan P. N. Exon Smith DIEInteger(idx)); 6137c52310SDavid Blaikie } 6237c52310SDavid Blaikie 6337c52310SDavid Blaikie void DwarfCompileUnit::addLocalLabelAddress(DIE &Die, 6437c52310SDavid Blaikie dwarf::Attribute Attribute, 6537c52310SDavid Blaikie const MCSymbol *Label) { 6637c52310SDavid Blaikie if (Label) 6737c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Label)); 6837c52310SDavid Blaikie 69815a6eb5SDuncan P. N. Exon Smith if (Label) 704fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 714fb1f9cdSDuncan P. N. Exon Smith DIELabel(Label)); 72815a6eb5SDuncan P. N. Exon Smith else 734fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_addr, 744fb1f9cdSDuncan P. N. Exon Smith DIEInteger(0)); 7537c52310SDavid Blaikie } 7637c52310SDavid Blaikie 778945219dSDavid Blaikie unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, 788945219dSDavid Blaikie StringRef DirName) { 7937c52310SDavid Blaikie // If we print assembly, we can't separate .file entries according to 8037c52310SDavid Blaikie // compile units. Thus all files will belong to the default compile unit. 8137c52310SDavid Blaikie 8237c52310SDavid Blaikie // FIXME: add a better feature test than hasRawTextSupport. Even better, 8337c52310SDavid Blaikie // extend .file to support this. 849ff69c8fSLang Hames return Asm->OutStreamer->EmitDwarfFileDirective( 8537c52310SDavid Blaikie 0, DirName, FileName, 869ff69c8fSLang Hames Asm->OutStreamer->hasRawTextSupport() ? 0 : getUniqueID()); 8737c52310SDavid Blaikie } 8837c52310SDavid Blaikie 8960635e39SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE( 90bceaaa96SAdrian Prantl const DIGlobalVariable *GV, ArrayRef<GlobalExpr> GlobalExprs) { 9137c52310SDavid Blaikie // Check for pre-existence. 9237c52310SDavid Blaikie if (DIE *Die = getDIE(GV)) 9337c52310SDavid Blaikie return Die; 9437c52310SDavid Blaikie 95e686f159SDuncan P. N. Exon Smith assert(GV); 9637c52310SDavid Blaikie 97be9e4fe7SDuncan P. N. Exon Smith auto *GVContext = GV->getScope(); 98be9e4fe7SDuncan P. N. Exon Smith auto *GTy = DD->resolve(GV->getType()); 9937c52310SDavid Blaikie 10037c52310SDavid Blaikie // Construct the context before querying for the existence of the DIE in 10137c52310SDavid Blaikie // case such construction creates the DIE. 10272da9391SAmjad Aboud DIE *ContextDIE = getOrCreateContextDIE(GVContext); 10337c52310SDavid Blaikie 10472da9391SAmjad Aboud // Add to map. 10572da9391SAmjad Aboud DIE *VariableDIE = &createAndAddDIE(GV->getTag(), *ContextDIE, GV); 106a9308c49SDuncan P. N. Exon Smith DIScope *DeclContext; 107b1055640SDuncan P. N. Exon Smith if (auto *SDMDecl = GV->getStaticDataMemberDeclaration()) { 108b1055640SDuncan P. N. Exon Smith DeclContext = resolve(SDMDecl->getScope()); 109b1055640SDuncan P. N. Exon Smith assert(SDMDecl->isStaticMember() && "Expected static member decl"); 1107348ddaaSDuncan P. N. Exon Smith assert(GV->isDefinition()); 11149cfc8caSDavid Blaikie // We need the declaration DIE that is in the static member's class. 11249cfc8caSDavid Blaikie DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl); 11349cfc8caSDavid Blaikie addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE); 1143502f208SAdrian Prantl // If the global variable's type is different from the one in the class 1153502f208SAdrian Prantl // member type, assume that it's more specific and also emit it. 1163502f208SAdrian Prantl if (GTy != DD->resolve(SDMDecl->getBaseType())) 1173502f208SAdrian Prantl addType(*VariableDIE, GTy); 11849cfc8caSDavid Blaikie } else { 1197348ddaaSDuncan P. N. Exon Smith DeclContext = GV->getScope(); 12037c52310SDavid Blaikie // Add name and type. 1217348ddaaSDuncan P. N. Exon Smith addString(*VariableDIE, dwarf::DW_AT_name, GV->getDisplayName()); 12237c52310SDavid Blaikie addType(*VariableDIE, GTy); 12337c52310SDavid Blaikie 12437c52310SDavid Blaikie // Add scoping info. 1257348ddaaSDuncan P. N. Exon Smith if (!GV->isLocalToUnit()) 12637c52310SDavid Blaikie addFlag(*VariableDIE, dwarf::DW_AT_external); 12737c52310SDavid Blaikie 12837c52310SDavid Blaikie // Add line number info. 12937c52310SDavid Blaikie addSourceLine(*VariableDIE, GV); 13037c52310SDavid Blaikie } 13137c52310SDavid Blaikie 1327348ddaaSDuncan P. N. Exon Smith if (!GV->isDefinition()) 13349cfc8caSDavid Blaikie addFlag(*VariableDIE, dwarf::DW_AT_declaration); 134877354a2SDavid Blaikie else 135877354a2SDavid Blaikie addGlobalName(GV->getName(), *VariableDIE, DeclContext); 13649cfc8caSDavid Blaikie 1373c989984SVictor Leschuk if (uint32_t AlignInBytes = GV->getAlignInBytes()) 1383c989984SVictor Leschuk addUInt(*VariableDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 1393c989984SVictor Leschuk AlignInBytes); 1403c989984SVictor Leschuk 14137c52310SDavid Blaikie // Add location. 14237c52310SDavid Blaikie bool addToAccelTable = false; 143bceaaa96SAdrian Prantl DIELoc *Loc = nullptr; 144bceaaa96SAdrian Prantl std::unique_ptr<DIEDwarfExpression> DwarfExpr; 145bceaaa96SAdrian Prantl for (const auto &GE : GlobalExprs) { 146bceaaa96SAdrian Prantl const GlobalVariable *Global = GE.Var; 147bceaaa96SAdrian Prantl const DIExpression *Expr = GE.Expr; 148b216e3e9SAdrian Prantl 149d4135bbcSPeter Collingbourne // For compatibility with DWARF 3 and earlier, 150d4135bbcSPeter Collingbourne // DW_AT_location(DW_OP_constu, X, DW_OP_stack_value) becomes 151d4135bbcSPeter Collingbourne // DW_AT_const_value(X). 152bceaaa96SAdrian Prantl if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) { 153b216e3e9SAdrian Prantl addToAccelTable = true; 154d4135bbcSPeter Collingbourne addConstantValue(*VariableDIE, /*Unsigned=*/true, Expr->getElement(1)); 155b216e3e9SAdrian Prantl break; 156b216e3e9SAdrian Prantl } 157b216e3e9SAdrian Prantl 158bceaaa96SAdrian Prantl // We cannot describe the location of dllimport'd variables: the 159bceaaa96SAdrian Prantl // computation of their address requires loads from the IAT. 160b216e3e9SAdrian Prantl if (Global && Global->hasDLLImportStorageClass()) 161b216e3e9SAdrian Prantl continue; 162b216e3e9SAdrian Prantl 163b216e3e9SAdrian Prantl // Nothing to describe without address or constant. 164b216e3e9SAdrian Prantl if (!Global && (!Expr || !Expr->isConstant())) 165b216e3e9SAdrian Prantl continue; 166b216e3e9SAdrian Prantl 167bceaaa96SAdrian Prantl if (!Loc) { 168b216e3e9SAdrian Prantl addToAccelTable = true; 169bceaaa96SAdrian Prantl Loc = new (DIEValueAllocator) DIELoc; 170bceaaa96SAdrian Prantl DwarfExpr = llvm::make_unique<DIEDwarfExpression>(*Asm, *this, *Loc); 171bceaaa96SAdrian Prantl } 172b216e3e9SAdrian Prantl 173bceaaa96SAdrian Prantl if (Global) { 174cca5f68eSDuncan P. N. Exon Smith const MCSymbol *Sym = Asm->getSymbol(Global); 175cca5f68eSDuncan P. N. Exon Smith if (Global->isThreadLocal()) { 1761e859582SChih-Hung Hsieh if (Asm->TM.Options.EmulatedTLS) { 1771e859582SChih-Hung Hsieh // TODO: add debug info for emulated thread local mode. 1781e859582SChih-Hung Hsieh } else { 17937c52310SDavid Blaikie // FIXME: Make this work with -gsplit-dwarf. 18037c52310SDavid Blaikie unsigned PointerSize = Asm->getDataLayout().getPointerSize(); 18137c52310SDavid Blaikie assert((PointerSize == 4 || PointerSize == 8) && 18237c52310SDavid Blaikie "Add support for other sizes if necessary"); 18337c52310SDavid Blaikie // Based on GCC's support for TLS: 18437c52310SDavid Blaikie if (!DD->useSplitDwarf()) { 18537c52310SDavid Blaikie // 1) Start with a constNu of the appropriate pointer size 186bceaaa96SAdrian Prantl addUInt(*Loc, dwarf::DW_FORM_data1, 187bceaaa96SAdrian Prantl PointerSize == 4 ? dwarf::DW_OP_const4u 18854511353SDehao Chen : dwarf::DW_OP_const8u); 18937c52310SDavid Blaikie // 2) containing the (relocated) offset of the TLS variable 19037c52310SDavid Blaikie // within the module's TLS block. 19137c52310SDavid Blaikie addExpr(*Loc, dwarf::DW_FORM_udata, 19237c52310SDavid Blaikie Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym)); 19337c52310SDavid Blaikie } else { 19437c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); 19537c52310SDavid Blaikie addUInt(*Loc, dwarf::DW_FORM_udata, 19637c52310SDavid Blaikie DD->getAddressPool().getIndex(Sym, /* TLS */ true)); 19737c52310SDavid Blaikie } 19878cc0821SPaul Robinson // 3) followed by an OP to make the debugger do a TLS lookup. 19978cc0821SPaul Robinson addUInt(*Loc, dwarf::DW_FORM_data1, 20078cc0821SPaul Robinson DD->useGNUTLSOpcode() ? dwarf::DW_OP_GNU_push_tls_address 20178cc0821SPaul Robinson : dwarf::DW_OP_form_tls_address); 2021e859582SChih-Hung Hsieh } 20337c52310SDavid Blaikie } else { 20437c52310SDavid Blaikie DD->addArangeLabel(SymbolCU(this, Sym)); 20537c52310SDavid Blaikie addOpAddress(*Loc, Sym); 20637c52310SDavid Blaikie } 207bceaaa96SAdrian Prantl } 208d4135bbcSPeter Collingbourne if (Expr) { 209bceaaa96SAdrian Prantl DwarfExpr->addFragmentOffset(Expr); 210a63b8e82SAdrian Prantl DwarfExpr->addExpression(Expr); 211d4135bbcSPeter Collingbourne } 212d4135bbcSPeter Collingbourne } 213bceaaa96SAdrian Prantl if (Loc) 214bceaaa96SAdrian Prantl addBlock(*VariableDIE, dwarf::DW_AT_location, DwarfExpr->finalize()); 215d4135bbcSPeter Collingbourne 21643d1e453SPaul Robinson if (DD->useAllLinkageNames()) 2177348ddaaSDuncan P. N. Exon Smith addLinkageName(*VariableDIE, GV->getLinkageName()); 21837c52310SDavid Blaikie 21937c52310SDavid Blaikie if (addToAccelTable) { 2207348ddaaSDuncan P. N. Exon Smith DD->addAccelName(GV->getName(), *VariableDIE); 22137c52310SDavid Blaikie 22237c52310SDavid Blaikie // If the linkage name is different than the name, go ahead and output 22337c52310SDavid Blaikie // that as well into the name table. 2247348ddaaSDuncan P. N. Exon Smith if (GV->getLinkageName() != "" && GV->getName() != GV->getLinkageName()) 2257348ddaaSDuncan P. N. Exon Smith DD->addAccelName(GV->getLinkageName(), *VariableDIE); 22637c52310SDavid Blaikie } 22737c52310SDavid Blaikie 22849cfc8caSDavid Blaikie return VariableDIE; 22937c52310SDavid Blaikie } 23037c52310SDavid Blaikie 23137c52310SDavid Blaikie void DwarfCompileUnit::addRange(RangeSpan Range) { 23237c52310SDavid Blaikie bool SameAsPrevCU = this == DD->getPrevCU(); 23337c52310SDavid Blaikie DD->setPrevCU(this); 23437c52310SDavid Blaikie // If we have no current ranges just add the range and return, otherwise, 23537c52310SDavid Blaikie // check the current section and CU against the previous section and CU we 23637c52310SDavid Blaikie // emitted into and the subprogram was contained within. If these are the 23737c52310SDavid Blaikie // same then extend our current range, otherwise add this as a new range. 23837c52310SDavid Blaikie if (CURanges.empty() || !SameAsPrevCU || 23937c52310SDavid Blaikie (&CURanges.back().getEnd()->getSection() != 24037c52310SDavid Blaikie &Range.getEnd()->getSection())) { 24137c52310SDavid Blaikie CURanges.push_back(Range); 24237c52310SDavid Blaikie return; 24337c52310SDavid Blaikie } 24437c52310SDavid Blaikie 24537c52310SDavid Blaikie CURanges.back().setEnd(Range.getEnd()); 24637c52310SDavid Blaikie } 24737c52310SDavid Blaikie 2484fb1f9cdSDuncan P. N. Exon Smith DIE::value_iterator 2494fb1f9cdSDuncan P. N. Exon Smith DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 2504fb1f9cdSDuncan P. N. Exon Smith const MCSymbol *Label, const MCSymbol *Sec) { 2516c0ee4ecSDavid Blaikie if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) 2524fb1f9cdSDuncan P. N. Exon Smith return addLabel(Die, Attribute, 2536c0ee4ecSDavid Blaikie DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 2546c0ee4ecSDavid Blaikie : dwarf::DW_FORM_data4, 2556c0ee4ecSDavid Blaikie Label); 2564fb1f9cdSDuncan P. N. Exon Smith return addSectionDelta(Die, Attribute, Label, Sec); 2576c0ee4ecSDavid Blaikie } 2586c0ee4ecSDavid Blaikie 259063d725fSRafael Espindola void DwarfCompileUnit::initStmtList() { 26037c52310SDavid Blaikie // Define start line table label for each Compile Unit. 26137c52310SDavid Blaikie MCSymbol *LineTableStartSym = 2629ff69c8fSLang Hames Asm->OutStreamer->getDwarfLineTableSymbol(getUniqueID()); 26337c52310SDavid Blaikie 26437c52310SDavid Blaikie // DW_AT_stmt_list is a offset of line number information for this 26537c52310SDavid Blaikie // compile unit in debug_line section. For split dwarf this is 26637c52310SDavid Blaikie // left in the skeleton CU and so not included. 26737c52310SDavid Blaikie // The line table entries are not always emitted in assembly, so it 26837c52310SDavid Blaikie // is not okay to use line_table_start here. 269063d725fSRafael Espindola const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 2704fb1f9cdSDuncan P. N. Exon Smith StmtListValue = 27135630c33SGreg Clayton addSectionLabel(getUnitDie(), dwarf::DW_AT_stmt_list, LineTableStartSym, 272063d725fSRafael Espindola TLOF.getDwarfLineSection()->getBeginSymbol()); 27337c52310SDavid Blaikie } 27437c52310SDavid Blaikie 27537c52310SDavid Blaikie void DwarfCompileUnit::applyStmtList(DIE &D) { 2764fb1f9cdSDuncan P. N. Exon Smith D.addValue(DIEValueAllocator, *StmtListValue); 27737c52310SDavid Blaikie } 27837c52310SDavid Blaikie 27914499a7dSDavid Blaikie void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin, 28014499a7dSDavid Blaikie const MCSymbol *End) { 28114499a7dSDavid Blaikie assert(Begin && "Begin label should not be null!"); 28214499a7dSDavid Blaikie assert(End && "End label should not be null!"); 28314499a7dSDavid Blaikie assert(Begin->isDefined() && "Invalid starting label"); 28414499a7dSDavid Blaikie assert(End->isDefined() && "Invalid end label"); 28514499a7dSDavid Blaikie 28614499a7dSDavid Blaikie addLabelAddress(D, dwarf::DW_AT_low_pc, Begin); 28714499a7dSDavid Blaikie if (DD->getDwarfVersion() < 4) 28814499a7dSDavid Blaikie addLabelAddress(D, dwarf::DW_AT_high_pc, End); 28914499a7dSDavid Blaikie else 29014499a7dSDavid Blaikie addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin); 29114499a7dSDavid Blaikie } 29214499a7dSDavid Blaikie 293cda2aa82SDavid Blaikie // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc 294cda2aa82SDavid Blaikie // and DW_AT_high_pc attributes. If there are global variables in this 295cda2aa82SDavid Blaikie // scope then create and insert DIEs for these variables. 296a9308c49SDuncan P. N. Exon Smith DIE &DwarfCompileUnit::updateSubprogramScopeDIE(const DISubprogram *SP) { 2973a443c29SDavid Blaikie DIE *SPDie = getOrCreateSubprogramDIE(SP, includeMinimalInlineScopes()); 298cda2aa82SDavid Blaikie 29907c03d31SRafael Espindola attachLowHighPC(*SPDie, Asm->getFunctionBegin(), Asm->getFunctionEnd()); 300c53e18d9SDavid Blaikie if (DD->useAppleExtensionAttributes() && 301c53e18d9SDavid Blaikie !DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim( 302cda2aa82SDavid Blaikie *DD->getCurrentFunction())) 303cda2aa82SDavid Blaikie addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr); 304cda2aa82SDavid Blaikie 305cda2aa82SDavid Blaikie // Only include DW_AT_frame_base in full debug info 3063a443c29SDavid Blaikie if (!includeMinimalInlineScopes()) { 307d4e723f2SEric Christopher const TargetRegisterInfo *RI = Asm->MF->getSubtarget().getRegisterInfo(); 308cda2aa82SDavid Blaikie MachineLocation Location(RI->getFrameRegister(*Asm->MF)); 3098efadbf8SAdrian Prantl if (RI->isPhysicalRegister(Location.getReg())) 310cda2aa82SDavid Blaikie addAddress(*SPDie, dwarf::DW_AT_frame_base, Location); 311cda2aa82SDavid Blaikie } 312cda2aa82SDavid Blaikie 313cda2aa82SDavid Blaikie // Add name to the name table, we do this here because we're guaranteed 314cda2aa82SDavid Blaikie // to have concrete versions of our DW_TAG_subprogram nodes. 315cda2aa82SDavid Blaikie DD->addSubprogramNames(SP, *SPDie); 316cda2aa82SDavid Blaikie 317cda2aa82SDavid Blaikie return *SPDie; 318cda2aa82SDavid Blaikie } 319cda2aa82SDavid Blaikie 3209c65b135SDavid Blaikie // Construct a DIE for this scope. 3219c65b135SDavid Blaikie void DwarfCompileUnit::constructScopeDIE( 322827200c8SDuncan P. N. Exon Smith LexicalScope *Scope, SmallVectorImpl<DIE *> &FinalChildren) { 3239c65b135SDavid Blaikie if (!Scope || !Scope->getScopeNode()) 3249c65b135SDavid Blaikie return; 3259c65b135SDavid Blaikie 326be9e4fe7SDuncan P. N. Exon Smith auto *DS = Scope->getScopeNode(); 3279c65b135SDavid Blaikie 328a9308c49SDuncan P. N. Exon Smith assert((Scope->getInlinedAt() || !isa<DISubprogram>(DS)) && 3299c65b135SDavid Blaikie "Only handle inlined subprograms here, use " 3309c65b135SDavid Blaikie "constructSubprogramScopeDIE for non-inlined " 3319c65b135SDavid Blaikie "subprograms"); 3329c65b135SDavid Blaikie 333827200c8SDuncan P. N. Exon Smith SmallVector<DIE *, 8> Children; 3349c65b135SDavid Blaikie 3359c65b135SDavid Blaikie // We try to create the scope DIE first, then the children DIEs. This will 3369c65b135SDavid Blaikie // avoid creating un-used children then removing them later when we find out 3379c65b135SDavid Blaikie // the scope DIE is null. 338827200c8SDuncan P. N. Exon Smith DIE *ScopeDIE; 339a9308c49SDuncan P. N. Exon Smith if (Scope->getParent() && isa<DISubprogram>(DS)) { 34001b48a84SDavid Blaikie ScopeDIE = constructInlinedScopeDIE(Scope); 3419c65b135SDavid Blaikie if (!ScopeDIE) 3429c65b135SDavid Blaikie return; 3439c65b135SDavid Blaikie // We create children when the scope DIE is not null. 3448b2fdb83SDavid Blaikie createScopeChildrenDIE(Scope, Children); 3459c65b135SDavid Blaikie } else { 3469c65b135SDavid Blaikie // Early exit when we know the scope DIE is going to be null. 3479c65b135SDavid Blaikie if (DD->isLexicalScopeDIENull(Scope)) 3489c65b135SDavid Blaikie return; 3499c65b135SDavid Blaikie 35072da9391SAmjad Aboud unsigned ChildScopeCount; 3519c65b135SDavid Blaikie 3529c65b135SDavid Blaikie // We create children here when we know the scope DIE is not going to be 3539c65b135SDavid Blaikie // null and the children will be added to the scope DIE. 35472da9391SAmjad Aboud createScopeChildrenDIE(Scope, Children, &ChildScopeCount); 35572da9391SAmjad Aboud 35672da9391SAmjad Aboud // Skip imported directives in gmlt-like data. 35772da9391SAmjad Aboud if (!includeMinimalInlineScopes()) { 35872da9391SAmjad Aboud // There is no need to emit empty lexical block DIE. 35972da9391SAmjad Aboud for (const auto *IE : ImportedEntities[DS]) 36072da9391SAmjad Aboud Children.push_back( 36172da9391SAmjad Aboud constructImportedEntityDIE(cast<DIImportedEntity>(IE))); 36272da9391SAmjad Aboud } 3633a443c29SDavid Blaikie 3649c65b135SDavid Blaikie // If there are only other scopes as children, put them directly in the 3659c65b135SDavid Blaikie // parent instead, as this scope would serve no purpose. 36672da9391SAmjad Aboud if (Children.size() == ChildScopeCount) { 3679c65b135SDavid Blaikie FinalChildren.insert(FinalChildren.end(), 3689c65b135SDavid Blaikie std::make_move_iterator(Children.begin()), 3699c65b135SDavid Blaikie std::make_move_iterator(Children.end())); 3709c65b135SDavid Blaikie return; 3719c65b135SDavid Blaikie } 3720fbf8bdbSDavid Blaikie ScopeDIE = constructLexicalScopeDIE(Scope); 3739c65b135SDavid Blaikie assert(ScopeDIE && "Scope DIE should not be null."); 3749c65b135SDavid Blaikie } 3759c65b135SDavid Blaikie 3769c65b135SDavid Blaikie // Add children 3779c65b135SDavid Blaikie for (auto &I : Children) 3789c65b135SDavid Blaikie ScopeDIE->addChild(std::move(I)); 3799c65b135SDavid Blaikie 3809c65b135SDavid Blaikie FinalChildren.push_back(std::move(ScopeDIE)); 3819c65b135SDavid Blaikie } 3829c65b135SDavid Blaikie 3834fb1f9cdSDuncan P. N. Exon Smith DIE::value_iterator 3844fb1f9cdSDuncan P. N. Exon Smith DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 385e5feec50SDavid Blaikie const MCSymbol *Hi, const MCSymbol *Lo) { 3864fb1f9cdSDuncan P. N. Exon Smith return Die.addValue(DIEValueAllocator, Attribute, 3874fb1f9cdSDuncan P. N. Exon Smith DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 388e5feec50SDavid Blaikie : dwarf::DW_FORM_data4, 389e7e1d0c7SDuncan P. N. Exon Smith new (DIEValueAllocator) DIEDelta(Hi, Lo)); 390e5feec50SDavid Blaikie } 391e5feec50SDavid Blaikie 3925b02a19fSDavid Blaikie void DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE, 3935b02a19fSDavid Blaikie SmallVector<RangeSpan, 2> Range) { 394063d725fSRafael Espindola const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 395063d725fSRafael Espindola 39652400200SDavid Blaikie // Emit offset in .debug_range as a relocatable label. emitDIE will handle 39752400200SDavid Blaikie // emitting it appropriately. 398063d725fSRafael Espindola const MCSymbol *RangeSectionSym = 399063d725fSRafael Espindola TLOF.getDwarfRangesSection()->getBeginSymbol(); 40052400200SDavid Blaikie 4019ab09237SRafael Espindola RangeSpanList List(Asm->createTempSymbol("debug_ranges"), std::move(Range)); 4025b02a19fSDavid Blaikie 40352400200SDavid Blaikie // Under fission, ranges are specified by constant offsets relative to the 40452400200SDavid Blaikie // CU's DW_AT_GNU_ranges_base. 4055b02a19fSDavid Blaikie if (isDwoUnit()) 4065b02a19fSDavid Blaikie addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), 4075b02a19fSDavid Blaikie RangeSectionSym); 40852400200SDavid Blaikie else 4095b02a19fSDavid Blaikie addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, List.getSym(), 4105b02a19fSDavid Blaikie RangeSectionSym); 41152400200SDavid Blaikie 41252400200SDavid Blaikie // Add the range list to the set of ranges to be emitted. 413542616d4SDavid Blaikie (Skeleton ? Skeleton : this)->CURangeLists.push_back(std::move(List)); 41452400200SDavid Blaikie } 41552400200SDavid Blaikie 416de12375cSDavid Blaikie void DwarfCompileUnit::attachRangesOrLowHighPC( 4175b02a19fSDavid Blaikie DIE &Die, SmallVector<RangeSpan, 2> Ranges) { 4185b02a19fSDavid Blaikie if (Ranges.size() == 1) { 4195b02a19fSDavid Blaikie const auto &single = Ranges.front(); 4205b02a19fSDavid Blaikie attachLowHighPC(Die, single.getStart(), single.getEnd()); 4215b02a19fSDavid Blaikie } else 4225b02a19fSDavid Blaikie addScopeRangeList(Die, std::move(Ranges)); 4235b02a19fSDavid Blaikie } 4245b02a19fSDavid Blaikie 4255b02a19fSDavid Blaikie void DwarfCompileUnit::attachRangesOrLowHighPC( 426de12375cSDavid Blaikie DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) { 4275b02a19fSDavid Blaikie SmallVector<RangeSpan, 2> List; 4285b02a19fSDavid Blaikie List.reserve(Ranges.size()); 4295b02a19fSDavid Blaikie for (const InsnRange &R : Ranges) 4305b02a19fSDavid Blaikie List.push_back(RangeSpan(DD->getLabelBeforeInsn(R.first), 4315b02a19fSDavid Blaikie DD->getLabelAfterInsn(R.second))); 4325b02a19fSDavid Blaikie attachRangesOrLowHighPC(Die, std::move(List)); 433de12375cSDavid Blaikie } 434de12375cSDavid Blaikie 43501b48a84SDavid Blaikie // This scope represents inlined body of a function. Construct DIE to 43601b48a84SDavid Blaikie // represent this concrete inlined copy of the function. 437827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) { 43801b48a84SDavid Blaikie assert(Scope->getScopeNode()); 439be9e4fe7SDuncan P. N. Exon Smith auto *DS = Scope->getScopeNode(); 440be9e4fe7SDuncan P. N. Exon Smith auto *InlinedSP = getDISubprogram(DS); 44101b48a84SDavid Blaikie // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram 44201b48a84SDavid Blaikie // was inlined from another compile unit. 443488393f8SDavid Blaikie DIE *OriginDIE = getAbstractSPDies()[InlinedSP]; 44401b48a84SDavid Blaikie assert(OriginDIE && "Unable to find original DIE for an inlined subprogram."); 44501b48a84SDavid Blaikie 446827200c8SDuncan P. N. Exon Smith auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine); 44701b48a84SDavid Blaikie addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE); 44801b48a84SDavid Blaikie 44901b48a84SDavid Blaikie attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 45001b48a84SDavid Blaikie 45101b48a84SDavid Blaikie // Add the call site information to the DIE. 452a9308c49SDuncan P. N. Exon Smith const DILocation *IA = Scope->getInlinedAt(); 45301b48a84SDavid Blaikie addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None, 454b7e221baSDuncan P. N. Exon Smith getOrCreateSourceID(IA->getFilename(), IA->getDirectory())); 455b7e221baSDuncan P. N. Exon Smith addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, IA->getLine()); 4566e0c8446SDehao Chen if (IA->getDiscriminator() && DD->getDwarfVersion() >= 4) 45754511353SDehao Chen addUInt(*ScopeDIE, dwarf::DW_AT_GNU_discriminator, None, 45854511353SDehao Chen IA->getDiscriminator()); 45901b48a84SDavid Blaikie 46001b48a84SDavid Blaikie // Add name to the name table, we do this here because we're guaranteed 46101b48a84SDavid Blaikie // to have concrete versions of our DW_TAG_inlined_subprogram nodes. 46201b48a84SDavid Blaikie DD->addSubprogramNames(InlinedSP, *ScopeDIE); 46301b48a84SDavid Blaikie 46401b48a84SDavid Blaikie return ScopeDIE; 46501b48a84SDavid Blaikie } 46601b48a84SDavid Blaikie 4670fbf8bdbSDavid Blaikie // Construct new DW_TAG_lexical_block for this scope and attach 4680fbf8bdbSDavid Blaikie // DW_AT_low_pc/DW_AT_high_pc labels. 469827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) { 4700fbf8bdbSDavid Blaikie if (DD->isLexicalScopeDIENull(Scope)) 4710fbf8bdbSDavid Blaikie return nullptr; 4720fbf8bdbSDavid Blaikie 473827200c8SDuncan P. N. Exon Smith auto ScopeDIE = DIE::get(DIEValueAllocator, dwarf::DW_TAG_lexical_block); 4740fbf8bdbSDavid Blaikie if (Scope->isAbstractScope()) 4750fbf8bdbSDavid Blaikie return ScopeDIE; 4760fbf8bdbSDavid Blaikie 4770fbf8bdbSDavid Blaikie attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges()); 4780fbf8bdbSDavid Blaikie 4790fbf8bdbSDavid Blaikie return ScopeDIE; 4800fbf8bdbSDavid Blaikie } 4810fbf8bdbSDavid Blaikie 482ee7df553SDavid Blaikie /// constructVariableDIE - Construct a DIE for the given DbgVariable. 483827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, bool Abstract) { 484ee7df553SDavid Blaikie auto D = constructVariableDIEImpl(DV, Abstract); 485ee7df553SDavid Blaikie DV.setDIE(*D); 486ee7df553SDavid Blaikie return D; 487ee7df553SDavid Blaikie } 488ee7df553SDavid Blaikie 489827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV, 490ee7df553SDavid Blaikie bool Abstract) { 491ee7df553SDavid Blaikie // Define variable debug information entry. 492827200c8SDuncan P. N. Exon Smith auto VariableDie = DIE::get(DIEValueAllocator, DV.getTag()); 493ee7df553SDavid Blaikie 494ee7df553SDavid Blaikie if (Abstract) { 495ee7df553SDavid Blaikie applyVariableAttributes(DV, *VariableDie); 496ee7df553SDavid Blaikie return VariableDie; 497ee7df553SDavid Blaikie } 498ee7df553SDavid Blaikie 499ee7df553SDavid Blaikie // Add variable address. 500ee7df553SDavid Blaikie 501364a3005SDuncan P. N. Exon Smith unsigned Offset = DV.getDebugLocListIndex(); 502ee7df553SDavid Blaikie if (Offset != ~0U) { 503ee7df553SDavid Blaikie addLocationList(*VariableDie, dwarf::DW_AT_location, Offset); 504ee7df553SDavid Blaikie return VariableDie; 505ee7df553SDavid Blaikie } 506ee7df553SDavid Blaikie 507ee7df553SDavid Blaikie // Check if variable is described by a DBG_VALUE instruction. 508ee7df553SDavid Blaikie if (const MachineInstr *DVInsn = DV.getMInsn()) { 509ee7df553SDavid Blaikie assert(DVInsn->getNumOperands() == 4); 510ee7df553SDavid Blaikie if (DVInsn->getOperand(0).isReg()) { 511ee7df553SDavid Blaikie const MachineOperand RegOp = DVInsn->getOperand(0); 512ee7df553SDavid Blaikie // If the second operand is an immediate, this is an indirect value. 513ee7df553SDavid Blaikie if (DVInsn->getOperand(1).isImm()) { 514ee7df553SDavid Blaikie MachineLocation Location(RegOp.getReg(), 515ee7df553SDavid Blaikie DVInsn->getOperand(1).getImm()); 516ee7df553SDavid Blaikie addVariableAddress(DV, *VariableDie, Location); 517ee7df553SDavid Blaikie } else if (RegOp.getReg()) 518ee7df553SDavid Blaikie addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg())); 5193b89e663SAdrian Prantl } else if (DVInsn->getOperand(0).isImm()) { 5203b89e663SAdrian Prantl // This variable is described by a single constant. 5213b89e663SAdrian Prantl // Check whether it has a DIExpression. 5223b89e663SAdrian Prantl auto *Expr = DV.getSingleExpression(); 5233b89e663SAdrian Prantl if (Expr && Expr->getNumElements()) { 5243b89e663SAdrian Prantl DIELoc *Loc = new (DIEValueAllocator) DIELoc; 5253b89e663SAdrian Prantl DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 5263b89e663SAdrian Prantl // If there is an expression, emit raw unsigned bytes. 5278fafb8d3SAdrian Prantl DwarfExpr.addFragmentOffset(Expr); 528a63b8e82SAdrian Prantl DwarfExpr.addUnsignedConstant(DVInsn->getOperand(0).getImm()); 529a63b8e82SAdrian Prantl DwarfExpr.addExpression(Expr); 530bceaaa96SAdrian Prantl addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); 5313b89e663SAdrian Prantl } else 532ee7df553SDavid Blaikie addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType()); 5333b89e663SAdrian Prantl } else if (DVInsn->getOperand(0).isFPImm()) 534ee7df553SDavid Blaikie addConstantFPValue(*VariableDie, DVInsn->getOperand(0)); 535ee7df553SDavid Blaikie else if (DVInsn->getOperand(0).isCImm()) 536ee7df553SDavid Blaikie addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(), 537ee7df553SDavid Blaikie DV.getType()); 538ee7df553SDavid Blaikie 539ee7df553SDavid Blaikie return VariableDie; 540ee7df553SDavid Blaikie } 541ee7df553SDavid Blaikie 542ee7df553SDavid Blaikie // .. else use frame index. 54367c24422SAdrian Prantl if (!DV.hasFrameIndexExprs()) 544ca7e4702SAdrian Prantl return VariableDie; 545ca7e4702SAdrian Prantl 546e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 547ca7e4702SAdrian Prantl DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 54867c24422SAdrian Prantl for (auto &Fragment : DV.getFrameIndexExprs()) { 549ee7df553SDavid Blaikie unsigned FrameReg = 0; 5506825fb64SAdrian Prantl const DIExpression *Expr = Fragment.Expr; 551d4e723f2SEric Christopher const TargetFrameLowering *TFI = Asm->MF->getSubtarget().getFrameLowering(); 55267c24422SAdrian Prantl int Offset = TFI->getFrameIndexReference(*Asm->MF, Fragment.FI, FrameReg); 5536825fb64SAdrian Prantl DwarfExpr.addFragmentOffset(Expr); 554956484b7SAdrian Prantl SmallVector<uint64_t, 8> Ops; 555*ffc498dfSFlorian Hahn Ops.push_back(dwarf::DW_OP_plus_uconst); 556956484b7SAdrian Prantl Ops.push_back(Offset); 5576825fb64SAdrian Prantl Ops.append(Expr->elements_begin(), Expr->elements_end()); 5586825fb64SAdrian Prantl DIExpressionCursor Cursor(Ops); 559c12cee36SAdrian Prantl DwarfExpr.setMemoryLocationKind(); 560c12cee36SAdrian Prantl DwarfExpr.addMachineRegExpression( 561c12cee36SAdrian Prantl *Asm->MF->getSubtarget().getRegisterInfo(), Cursor, FrameReg); 5626825fb64SAdrian Prantl DwarfExpr.addExpression(std::move(Cursor)); 563ee7df553SDavid Blaikie } 564bceaaa96SAdrian Prantl addBlock(*VariableDie, dwarf::DW_AT_location, DwarfExpr.finalize()); 565ee7df553SDavid Blaikie 566ee7df553SDavid Blaikie return VariableDie; 567ee7df553SDavid Blaikie } 568ee7df553SDavid Blaikie 569827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructVariableDIE(DbgVariable &DV, 570827200c8SDuncan P. N. Exon Smith const LexicalScope &Scope, 571827200c8SDuncan P. N. Exon Smith DIE *&ObjectPointer) { 5724a1a44e3SDavid Blaikie auto Var = constructVariableDIE(DV, Scope.isAbstractScope()); 5734a1a44e3SDavid Blaikie if (DV.isObjectPointer()) 574827200c8SDuncan P. N. Exon Smith ObjectPointer = Var; 5754a1a44e3SDavid Blaikie return Var; 5764a1a44e3SDavid Blaikie } 5774a1a44e3SDavid Blaikie 578827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::createScopeChildrenDIE(LexicalScope *Scope, 579827200c8SDuncan P. N. Exon Smith SmallVectorImpl<DIE *> &Children, 58072da9391SAmjad Aboud unsigned *ChildScopeCount) { 5818b2fdb83SDavid Blaikie DIE *ObjectPointer = nullptr; 5828b2fdb83SDavid Blaikie 58380e5b1ebSDavid Blaikie for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope)) 5848b2fdb83SDavid Blaikie Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer)); 5858b2fdb83SDavid Blaikie 58672da9391SAmjad Aboud unsigned ChildCountWithoutScopes = Children.size(); 5878b2fdb83SDavid Blaikie 5888b2fdb83SDavid Blaikie for (LexicalScope *LS : Scope->getChildren()) 5898b2fdb83SDavid Blaikie constructScopeDIE(LS, Children); 5908b2fdb83SDavid Blaikie 59172da9391SAmjad Aboud if (ChildScopeCount) 59272da9391SAmjad Aboud *ChildScopeCount = Children.size() - ChildCountWithoutScopes; 59372da9391SAmjad Aboud 5948b2fdb83SDavid Blaikie return ObjectPointer; 5958b2fdb83SDavid Blaikie } 5968b2fdb83SDavid Blaikie 5973e3eb33eSDavid Blaikie void DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope) { 5981d072348SDavid Blaikie DIE &ScopeDIE = updateSubprogramScopeDIE(Sub); 5991d072348SDavid Blaikie 6003e3eb33eSDavid Blaikie if (Scope) { 6013e3eb33eSDavid Blaikie assert(!Scope->getInlinedAt()); 6023e3eb33eSDavid Blaikie assert(!Scope->isAbstractScope()); 6031dd573dbSDavid Blaikie // Collect lexical scope children first. 6041dd573dbSDavid Blaikie // ObjectPointer might be a local (non-argument) local variable if it's a 6051dd573dbSDavid Blaikie // block's synthetic this pointer. 6061dd573dbSDavid Blaikie if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE)) 6071dd573dbSDavid Blaikie addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer); 6083e3eb33eSDavid Blaikie } 6093e3eb33eSDavid Blaikie 6103e3eb33eSDavid Blaikie // If this is a variadic function, add an unspecified parameter. 6113e3eb33eSDavid Blaikie DITypeRefArray FnArgs = Sub->getType()->getTypeArray(); 6121dd573dbSDavid Blaikie 6131d072348SDavid Blaikie // If we have a single element of null, it is a function that returns void. 6141d072348SDavid Blaikie // If we have more than one elements and the last one is null, it is a 6151d072348SDavid Blaikie // variadic function. 616000fa2c6SDuncan P. N. Exon Smith if (FnArgs.size() > 1 && !FnArgs[FnArgs.size() - 1] && 6173a443c29SDavid Blaikie !includeMinimalInlineScopes()) 618827200c8SDuncan P. N. Exon Smith ScopeDIE.addChild( 619827200c8SDuncan P. N. Exon Smith DIE::get(DIEValueAllocator, dwarf::DW_TAG_unspecified_parameters)); 6201d072348SDavid Blaikie } 6211d072348SDavid Blaikie 62278b65b6fSDavid Blaikie DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope, 62378b65b6fSDavid Blaikie DIE &ScopeDIE) { 62478b65b6fSDavid Blaikie // We create children when the scope DIE is not null. 625827200c8SDuncan P. N. Exon Smith SmallVector<DIE *, 8> Children; 62678b65b6fSDavid Blaikie DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children); 62778b65b6fSDavid Blaikie 62878b65b6fSDavid Blaikie // Add children 62978b65b6fSDavid Blaikie for (auto &I : Children) 63078b65b6fSDavid Blaikie ScopeDIE.addChild(std::move(I)); 63178b65b6fSDavid Blaikie 63278b65b6fSDavid Blaikie return ObjectPointer; 63378b65b6fSDavid Blaikie } 63478b65b6fSDavid Blaikie 63554511353SDehao Chen void DwarfCompileUnit::constructAbstractSubprogramScopeDIE( 63654511353SDehao Chen LexicalScope *Scope) { 637488393f8SDavid Blaikie DIE *&AbsDef = getAbstractSPDies()[Scope->getScopeNode()]; 63849be5b35SDavid Blaikie if (AbsDef) 63949be5b35SDavid Blaikie return; 64049be5b35SDavid Blaikie 641a9308c49SDuncan P. N. Exon Smith auto *SP = cast<DISubprogram>(Scope->getScopeNode()); 64258410f24SDavid Blaikie 64358410f24SDavid Blaikie DIE *ContextDIE; 64458410f24SDavid Blaikie 6453a443c29SDavid Blaikie if (includeMinimalInlineScopes()) 6463a443c29SDavid Blaikie ContextDIE = &getUnitDie(); 64758410f24SDavid Blaikie // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with 6487c60f20eSDuncan P. N. Exon Smith // the important distinction that the debug node is not associated with the 6497c60f20eSDuncan P. N. Exon Smith // DIE (since the debug node will be associated with the concrete DIE, if 65058410f24SDavid Blaikie // any). It could be refactored to some common utility function. 651537b4a81SDuncan P. N. Exon Smith else if (auto *SPDecl = SP->getDeclaration()) { 65258410f24SDavid Blaikie ContextDIE = &getUnitDie(); 65358410f24SDavid Blaikie getOrCreateSubprogramDIE(SPDecl); 65458410f24SDavid Blaikie } else 655537b4a81SDuncan P. N. Exon Smith ContextDIE = getOrCreateContextDIE(resolve(SP->getScope())); 65658410f24SDavid Blaikie 6577c60f20eSDuncan P. N. Exon Smith // Passing null as the associated node because the abstract definition 65858410f24SDavid Blaikie // shouldn't be found by lookup. 6597c60f20eSDuncan P. N. Exon Smith AbsDef = &createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, nullptr); 66049be5b35SDavid Blaikie applySubprogramAttributesToDefinition(SP, *AbsDef); 66158410f24SDavid Blaikie 6623a443c29SDavid Blaikie if (!includeMinimalInlineScopes()) 66349be5b35SDavid Blaikie addUInt(*AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined); 66449be5b35SDavid Blaikie if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, *AbsDef)) 66549be5b35SDavid Blaikie addDIEEntry(*AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer); 66658410f24SDavid Blaikie } 66758410f24SDavid Blaikie 668827200c8SDuncan P. N. Exon Smith DIE *DwarfCompileUnit::constructImportedEntityDIE( 669827200c8SDuncan P. N. Exon Smith const DIImportedEntity *Module) { 67072da9391SAmjad Aboud DIE *IMDie = DIE::get(DIEValueAllocator, (dwarf::Tag)Module->getTag()); 67172da9391SAmjad Aboud insertDIE(Module, IMDie); 672987fe227SFrederic Riss DIE *EntityDie; 673de8e4273SDuncan P. N. Exon Smith auto *Entity = resolve(Module->getEntity()); 674a9308c49SDuncan P. N. Exon Smith if (auto *NS = dyn_cast<DINamespace>(Entity)) 675e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateNameSpace(NS); 67608a388baSAdrian Prantl else if (auto *M = dyn_cast<DIModule>(Entity)) 67708a388baSAdrian Prantl EntityDie = getOrCreateModule(M); 678a9308c49SDuncan P. N. Exon Smith else if (auto *SP = dyn_cast<DISubprogram>(Entity)) 679e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateSubprogramDIE(SP); 680a9308c49SDuncan P. N. Exon Smith else if (auto *T = dyn_cast<DIType>(Entity)) 681e686f159SDuncan P. N. Exon Smith EntityDie = getOrCreateTypeDIE(T); 682a9308c49SDuncan P. N. Exon Smith else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity)) 683bceaaa96SAdrian Prantl EntityDie = getOrCreateGlobalVariableDIE(GV, {}); 684987fe227SFrederic Riss else 685987fe227SFrederic Riss EntityDie = getDIE(Entity); 686987fe227SFrederic Riss assert(EntityDie); 687de8e4273SDuncan P. N. Exon Smith addSourceLine(*IMDie, Module->getLine(), Module->getScope()->getFilename(), 688de8e4273SDuncan P. N. Exon Smith Module->getScope()->getDirectory()); 689987fe227SFrederic Riss addDIEEntry(*IMDie, dwarf::DW_AT_import, *EntityDie); 690de8e4273SDuncan P. N. Exon Smith StringRef Name = Module->getName(); 691987fe227SFrederic Riss if (!Name.empty()) 692987fe227SFrederic Riss addString(*IMDie, dwarf::DW_AT_name, Name); 693987fe227SFrederic Riss 694987fe227SFrederic Riss return IMDie; 695987fe227SFrederic Riss } 696987fe227SFrederic Riss 697a9308c49SDuncan P. N. Exon Smith void DwarfCompileUnit::finishSubprogramDefinition(const DISubprogram *SP) { 69872da9391SAmjad Aboud DIE *D = getDIE(SP); 699488393f8SDavid Blaikie if (DIE *AbsSPDIE = getAbstractSPDies().lookup(SP)) { 70072da9391SAmjad Aboud if (D) 7014191cbceSDavid Blaikie // If this subprogram has an abstract definition, reference that 7024191cbceSDavid Blaikie addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE); 70372da9391SAmjad Aboud } else { 7043e3eb33eSDavid Blaikie assert(D || includeMinimalInlineScopes()); 70572da9391SAmjad Aboud if (D) 7064191cbceSDavid Blaikie // And attach the attributes 7074191cbceSDavid Blaikie applySubprogramAttributesToDefinition(SP, *D); 7084191cbceSDavid Blaikie } 7094191cbceSDavid Blaikie } 7104191cbceSDavid Blaikie 711488393f8SDavid Blaikie void DwarfCompileUnit::finishVariableDefinition(const DbgVariable &Var) { 712488393f8SDavid Blaikie DbgVariable *AbsVar = getExistingAbstractVariable( 713488393f8SDavid Blaikie InlinedVariable(Var.getVariable(), Var.getInlinedAt())); 714488393f8SDavid Blaikie auto *VariableDie = Var.getDIE(); 715488393f8SDavid Blaikie if (AbsVar && AbsVar->getDIE()) { 716488393f8SDavid Blaikie addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin, 717488393f8SDavid Blaikie *AbsVar->getDIE()); 718488393f8SDavid Blaikie } else 719488393f8SDavid Blaikie applyVariableAttributes(Var, *VariableDie); 720488393f8SDavid Blaikie } 721488393f8SDavid Blaikie 722488393f8SDavid Blaikie DbgVariable *DwarfCompileUnit::getExistingAbstractVariable(InlinedVariable IV) { 723488393f8SDavid Blaikie const DILocalVariable *Cleansed; 724488393f8SDavid Blaikie return getExistingAbstractVariable(IV, Cleansed); 725488393f8SDavid Blaikie } 726488393f8SDavid Blaikie 727488393f8SDavid Blaikie // Find abstract variable, if any, associated with Var. 728488393f8SDavid Blaikie DbgVariable *DwarfCompileUnit::getExistingAbstractVariable( 729488393f8SDavid Blaikie InlinedVariable IV, const DILocalVariable *&Cleansed) { 730488393f8SDavid Blaikie // More then one inlined variable corresponds to one abstract variable. 731488393f8SDavid Blaikie Cleansed = IV.first; 732488393f8SDavid Blaikie auto &AbstractVariables = getAbstractVariables(); 733488393f8SDavid Blaikie auto I = AbstractVariables.find(Cleansed); 734488393f8SDavid Blaikie if (I != AbstractVariables.end()) 735488393f8SDavid Blaikie return I->second.get(); 736488393f8SDavid Blaikie return nullptr; 737488393f8SDavid Blaikie } 738488393f8SDavid Blaikie 739488393f8SDavid Blaikie void DwarfCompileUnit::createAbstractVariable(const DILocalVariable *Var, 740488393f8SDavid Blaikie LexicalScope *Scope) { 741488393f8SDavid Blaikie assert(Scope && Scope->isAbstractScope()); 742488393f8SDavid Blaikie auto AbsDbgVariable = make_unique<DbgVariable>(Var, /* IA */ nullptr); 743488393f8SDavid Blaikie DU->addScopeVariable(Scope, AbsDbgVariable.get()); 744488393f8SDavid Blaikie getAbstractVariables()[Var] = std::move(AbsDbgVariable); 745488393f8SDavid Blaikie } 746488393f8SDavid Blaikie 747063d725fSRafael Espindola void DwarfCompileUnit::emitHeader(bool UseOffsets) { 748b6726a9eSDavid Blaikie // Don't bother labeling the .dwo unit, as its offset isn't used. 7493c311148SRafael Espindola if (!Skeleton) { 7509ab09237SRafael Espindola LabelBegin = Asm->createTempSymbol("cu_begin"); 7519ff69c8fSLang Hames Asm->OutStreamer->EmitLabel(LabelBegin); 7523c311148SRafael Espindola } 753ae57e66eSDavid Blaikie 754cddd6044SPaul Robinson dwarf::UnitType UT = Skeleton ? dwarf::DW_UT_split_compile 755cddd6044SPaul Robinson : DD->useSplitDwarf() ? dwarf::DW_UT_skeleton 756cddd6044SPaul Robinson : dwarf::DW_UT_compile; 757cddd6044SPaul Robinson DwarfUnit::emitCommonHeader(UseOffsets, UT); 758ae57e66eSDavid Blaikie } 759ae57e66eSDavid Blaikie 760192b45c1SDavid Blaikie /// addGlobalName - Add a new global name to the compile unit. 761a0e3c751SDavid Blaikie void DwarfCompileUnit::addGlobalName(StringRef Name, const DIE &Die, 762a9308c49SDuncan P. N. Exon Smith const DIScope *Context) { 763b3cee2fbSDavid Blaikie if (!DD->hasDwarfPubSections(includeMinimalInlineScopes())) 764192b45c1SDavid Blaikie return; 765192b45c1SDavid Blaikie std::string FullName = getParentContextString(Context) + Name.str(); 766192b45c1SDavid Blaikie GlobalNames[FullName] = &Die; 767192b45c1SDavid Blaikie } 768192b45c1SDavid Blaikie 769a0e3c751SDavid Blaikie void DwarfCompileUnit::addGlobalNameForTypeUnit(StringRef Name, 770a0e3c751SDavid Blaikie const DIScope *Context) { 771b3cee2fbSDavid Blaikie if (!DD->hasDwarfPubSections(includeMinimalInlineScopes())) 772a0e3c751SDavid Blaikie return; 773a0e3c751SDavid Blaikie std::string FullName = getParentContextString(Context) + Name.str(); 774a0e3c751SDavid Blaikie // Insert, allowing the entry to remain as-is if it's already present 775a0e3c751SDavid Blaikie // This way the CU-level type DIE is preferred over the "can't describe this 776a0e3c751SDavid Blaikie // type as a unit offset because it's not really in the CU at all, it's only 777a0e3c751SDavid Blaikie // in a type unit" 778a0e3c751SDavid Blaikie GlobalNames.insert(std::make_pair(std::move(FullName), &getUnitDie())); 779a0e3c751SDavid Blaikie } 780a0e3c751SDavid Blaikie 781192b45c1SDavid Blaikie /// Add a new global type to the unit. 782a9308c49SDuncan P. N. Exon Smith void DwarfCompileUnit::addGlobalType(const DIType *Ty, const DIE &Die, 783a9308c49SDuncan P. N. Exon Smith const DIScope *Context) { 784b3cee2fbSDavid Blaikie if (!DD->hasDwarfPubSections(includeMinimalInlineScopes())) 785192b45c1SDavid Blaikie return; 786b1055640SDuncan P. N. Exon Smith std::string FullName = getParentContextString(Context) + Ty->getName().str(); 787192b45c1SDavid Blaikie GlobalTypes[FullName] = &Die; 788192b45c1SDavid Blaikie } 789ae57e66eSDavid Blaikie 790a0e3c751SDavid Blaikie void DwarfCompileUnit::addGlobalTypeUnitType(const DIType *Ty, 791a0e3c751SDavid Blaikie const DIScope *Context) { 792b3cee2fbSDavid Blaikie if (!DD->hasDwarfPubSections(includeMinimalInlineScopes())) 793a0e3c751SDavid Blaikie return; 794a0e3c751SDavid Blaikie std::string FullName = getParentContextString(Context) + Ty->getName().str(); 795a0e3c751SDavid Blaikie // Insert, allowing the entry to remain as-is if it's already present 796a0e3c751SDavid Blaikie // This way the CU-level type DIE is preferred over the "can't describe this 797a0e3c751SDavid Blaikie // type as a unit offset because it's not really in the CU at all, it's only 798a0e3c751SDavid Blaikie // in a type unit" 799a0e3c751SDavid Blaikie GlobalTypes.insert(std::make_pair(std::move(FullName), &getUnitDie())); 800a0e3c751SDavid Blaikie } 801a0e3c751SDavid Blaikie 8027d48be2bSDavid Blaikie /// addVariableAddress - Add DW_AT_location attribute for a 8037d48be2bSDavid Blaikie /// DbgVariable based on provided MachineLocation. 8047d48be2bSDavid Blaikie void DwarfCompileUnit::addVariableAddress(const DbgVariable &DV, DIE &Die, 8057d48be2bSDavid Blaikie MachineLocation Location) { 806e6cc531bSDuncan P. N. Exon Smith if (DV.hasComplexAddress()) 8077d48be2bSDavid Blaikie addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); 8087d48be2bSDavid Blaikie else if (DV.isBlockByrefVariable()) 8097d48be2bSDavid Blaikie addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); 8107d48be2bSDavid Blaikie else 8115883af3fSAdrian Prantl addAddress(Die, dwarf::DW_AT_location, Location); 8127d48be2bSDavid Blaikie } 813f7435ee6SDavid Blaikie 814f7435ee6SDavid Blaikie /// Add an address attribute to a die based on the location provided. 815f7435ee6SDavid Blaikie void DwarfCompileUnit::addAddress(DIE &Die, dwarf::Attribute Attribute, 8165883af3fSAdrian Prantl const MachineLocation &Location) { 817e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 818956484b7SAdrian Prantl DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 819c12cee36SAdrian Prantl if (Location.isIndirect()) 820c12cee36SAdrian Prantl DwarfExpr.setMemoryLocationKind(); 821f7435ee6SDavid Blaikie 822956484b7SAdrian Prantl SmallVector<uint64_t, 8> Ops; 8236825fb64SAdrian Prantl if (Location.isIndirect() && Location.getOffset()) { 824*ffc498dfSFlorian Hahn Ops.push_back(dwarf::DW_OP_plus_uconst); 825956484b7SAdrian Prantl Ops.push_back(Location.getOffset()); 826956484b7SAdrian Prantl } 827956484b7SAdrian Prantl DIExpressionCursor Cursor(Ops); 828956484b7SAdrian Prantl const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); 829c12cee36SAdrian Prantl if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 830ab255fcdSAdrian Prantl return; 831956484b7SAdrian Prantl DwarfExpr.addExpression(std::move(Cursor)); 832ab255fcdSAdrian Prantl 833f7435ee6SDavid Blaikie // Now attach the location information to the DIE. 834956484b7SAdrian Prantl addBlock(Die, Attribute, DwarfExpr.finalize()); 835f7435ee6SDavid Blaikie } 83677895fb2SDavid Blaikie 83777895fb2SDavid Blaikie /// Start with the address based on the location provided, and generate the 83877895fb2SDavid Blaikie /// DWARF information necessary to find the actual variable given the extra 83977895fb2SDavid Blaikie /// address information encoded in the DbgVariable, starting from the starting 84077895fb2SDavid Blaikie /// location. Add the DWARF information to the die. 84177895fb2SDavid Blaikie void DwarfCompileUnit::addComplexAddress(const DbgVariable &DV, DIE &Die, 84277895fb2SDavid Blaikie dwarf::Attribute Attribute, 84377895fb2SDavid Blaikie const MachineLocation &Location) { 844e7e1d0c7SDuncan P. N. Exon Smith DIELoc *Loc = new (DIEValueAllocator) DIELoc; 8457813d9c9SAdrian Prantl DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc); 846956484b7SAdrian Prantl const DIExpression *DIExpr = DV.getSingleExpression(); 847956484b7SAdrian Prantl DwarfExpr.addFragmentOffset(DIExpr); 848c12cee36SAdrian Prantl if (Location.isIndirect()) 849c12cee36SAdrian Prantl DwarfExpr.setMemoryLocationKind(); 850956484b7SAdrian Prantl 851956484b7SAdrian Prantl SmallVector<uint64_t, 8> Ops; 8526825fb64SAdrian Prantl if (Location.isIndirect() && Location.getOffset()) { 853*ffc498dfSFlorian Hahn Ops.push_back(dwarf::DW_OP_plus_uconst); 854956484b7SAdrian Prantl Ops.push_back(Location.getOffset()); 855956484b7SAdrian Prantl } 856956484b7SAdrian Prantl Ops.append(DIExpr->elements_begin(), DIExpr->elements_end()); 857956484b7SAdrian Prantl DIExpressionCursor Cursor(Ops); 85896c9ae6aSPeter Collingbourne const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo(); 859c12cee36SAdrian Prantl if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg())) 86054286bdaSAdrian Prantl return; 861956484b7SAdrian Prantl DwarfExpr.addExpression(std::move(Cursor)); 86277895fb2SDavid Blaikie 86377895fb2SDavid Blaikie // Now attach the location information to the DIE. 864956484b7SAdrian Prantl addBlock(Die, Attribute, DwarfExpr.finalize()); 86577895fb2SDavid Blaikie } 8664bc0881aSDavid Blaikie 8674bc0881aSDavid Blaikie /// Add a Dwarf loclistptr attribute data and value. 8684bc0881aSDavid Blaikie void DwarfCompileUnit::addLocationList(DIE &Die, dwarf::Attribute Attribute, 8694bc0881aSDavid Blaikie unsigned Index) { 8704bc0881aSDavid Blaikie dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset 8714bc0881aSDavid Blaikie : dwarf::DW_FORM_data4; 8724fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, Attribute, Form, DIELocList(Index)); 8734bc0881aSDavid Blaikie } 87402a6333bSDavid Blaikie 8758c485b5dSDavid Blaikie void DwarfCompileUnit::applyVariableAttributes(const DbgVariable &Var, 87602a6333bSDavid Blaikie DIE &VariableDie) { 87702a6333bSDavid Blaikie StringRef Name = Var.getName(); 87802a6333bSDavid Blaikie if (!Name.empty()) 87902a6333bSDavid Blaikie addString(VariableDie, dwarf::DW_AT_name, Name); 8803c989984SVictor Leschuk const auto *DIVar = Var.getVariable(); 8813c989984SVictor Leschuk if (DIVar) 8823c989984SVictor Leschuk if (uint32_t AlignInBytes = DIVar->getAlignInBytes()) 8833c989984SVictor Leschuk addUInt(VariableDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata, 8843c989984SVictor Leschuk AlignInBytes); 8853c989984SVictor Leschuk 8863c989984SVictor Leschuk addSourceLine(VariableDie, DIVar); 88702a6333bSDavid Blaikie addType(VariableDie, Var.getType()); 88802a6333bSDavid Blaikie if (Var.isArtificial()) 88902a6333bSDavid Blaikie addFlag(VariableDie, dwarf::DW_AT_artificial); 89002a6333bSDavid Blaikie } 89197802080SDavid Blaikie 89297802080SDavid Blaikie /// Add a Dwarf expression attribute data and value. 89397802080SDavid Blaikie void DwarfCompileUnit::addExpr(DIELoc &Die, dwarf::Form Form, 89497802080SDavid Blaikie const MCExpr *Expr) { 8954fb1f9cdSDuncan P. N. Exon Smith Die.addValue(DIEValueAllocator, (dwarf::Attribute)0, Form, DIEExpr(Expr)); 89697802080SDavid Blaikie } 8973363a57cSDavid Blaikie 8982fbe1354SDuncan P. N. Exon Smith void DwarfCompileUnit::applySubprogramAttributesToDefinition( 899a9308c49SDuncan P. N. Exon Smith const DISubprogram *SP, DIE &SPDie) { 900537b4a81SDuncan P. N. Exon Smith auto *SPDecl = SP->getDeclaration(); 901be9e4fe7SDuncan P. N. Exon Smith auto *Context = resolve(SPDecl ? SPDecl->getScope() : SP->getScope()); 9023a443c29SDavid Blaikie applySubprogramAttributes(SP, SPDie, includeMinimalInlineScopes()); 903537b4a81SDuncan P. N. Exon Smith addGlobalName(SP->getName(), SPDie, Context); 9043363a57cSDavid Blaikie } 905cafd962dSDavid Blaikie 906cafd962dSDavid Blaikie bool DwarfCompileUnit::isDwoUnit() const { 907cafd962dSDavid Blaikie return DD->useSplitDwarf() && Skeleton; 908cafd962dSDavid Blaikie } 9093a443c29SDavid Blaikie 9103a443c29SDavid Blaikie bool DwarfCompileUnit::includeMinimalInlineScopes() const { 911b939a257SAdrian Prantl return getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly || 9123a443c29SDavid Blaikie (DD->useSplitDwarf() && !Skeleton); 9133a443c29SDavid Blaikie } 914f00654e3SAlexander Kornienko } // end llvm namespace 915