1 //===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains support for writing dwarf compile unit. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H 16 17 #include "DwarfUnit.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/IR/DebugInfo.h" 20 #include "llvm/Support/Dwarf.h" 21 22 namespace llvm { 23 24 class AsmPrinter; 25 class DIE; 26 class DwarfDebug; 27 class DwarfFile; 28 class MCSymbol; 29 class LexicalScope; 30 31 class DwarfCompileUnit : public DwarfUnit { 32 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding 33 /// the need to search for it in applyStmtList. 34 unsigned stmtListIndex; 35 36 /// Skeleton unit associated with this unit. 37 DwarfCompileUnit *Skeleton; 38 39 /// A label at the start of the non-dwo section related to this unit. 40 MCSymbol *SectionSym; 41 42 /// The start of the unit within its section. 43 MCSymbol *LabelBegin; 44 45 /// GlobalNames - A map of globally visible named entities for this unit. 46 StringMap<const DIE *> GlobalNames; 47 48 /// GlobalTypes - A map of globally visible types for this unit. 49 StringMap<const DIE *> GlobalTypes; 50 51 // List of range lists for a given compile unit, separate from the ranges for 52 // the CU itself. 53 SmallVector<RangeSpanList, 1> CURangeLists; 54 55 // List of ranges for a given compile unit. 56 SmallVector<RangeSpan, 2> CURanges; 57 58 // The base address of this unit, if any. Used for relative references in 59 // ranges/locs. 60 const MCSymbol *BaseAddress; 61 62 /// \brief Construct a DIE for the given DbgVariable without initializing the 63 /// DbgVariable's DIE reference. 64 std::unique_ptr<DIE> constructVariableDIEImpl(const DbgVariable &DV, 65 bool Abstract); 66 67 bool isDwoUnit() const override; 68 69 bool includeMinimalInlineScopes() const; 70 71 public: 72 DwarfCompileUnit(unsigned UID, DICompileUnit Node, AsmPrinter *A, 73 DwarfDebug *DW, DwarfFile *DWU); 74 getSkeleton()75 DwarfCompileUnit *getSkeleton() const { 76 return Skeleton; 77 } 78 79 void initStmtList(MCSymbol *DwarfLineSectionSym); 80 81 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE. 82 void applyStmtList(DIE &D); 83 84 /// getOrCreateGlobalVariableDIE - get or create global variable DIE. 85 DIE *getOrCreateGlobalVariableDIE(DIGlobalVariable GV); 86 87 /// addLabelAddress - Add a dwarf label attribute data and value using 88 /// either DW_FORM_addr or DW_FORM_GNU_addr_index. 89 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 90 const MCSymbol *Label); 91 92 /// addLocalLabelAddress - Add a dwarf label attribute data and value using 93 /// DW_FORM_addr only. 94 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, 95 const MCSymbol *Label); 96 97 /// addSectionDelta - Add a label delta attribute data and value. 98 void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi, 99 const MCSymbol *Lo); 100 getCU()101 DwarfCompileUnit &getCU() override { return *this; } 102 103 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override; 104 105 /// addRange - Add an address range to the list of ranges for this unit. 106 void addRange(RangeSpan Range); 107 108 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End); 109 110 /// addSectionLabel - Add a Dwarf section label attribute data and value. 111 /// 112 void addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 113 const MCSymbol *Label, const MCSymbol *Sec); 114 115 /// \brief Find DIE for the given subprogram and attach appropriate 116 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global 117 /// variables in this scope then create and insert DIEs for these 118 /// variables. 119 DIE &updateSubprogramScopeDIE(DISubprogram SP); 120 121 void constructScopeDIE(LexicalScope *Scope, 122 SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren); 123 124 /// \brief A helper function to construct a RangeSpanList for a given 125 /// lexical scope. 126 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range); 127 128 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges); 129 130 void attachRangesOrLowHighPC(DIE &D, 131 const SmallVectorImpl<InsnRange> &Ranges); 132 /// \brief This scope represents inlined body of a function. Construct 133 /// DIE to represent this concrete inlined copy of the function. 134 std::unique_ptr<DIE> constructInlinedScopeDIE(LexicalScope *Scope); 135 136 /// \brief Construct new DW_TAG_lexical_block for this scope and 137 /// attach DW_AT_low_pc/DW_AT_high_pc labels. 138 std::unique_ptr<DIE> constructLexicalScopeDIE(LexicalScope *Scope); 139 140 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 141 std::unique_ptr<DIE> constructVariableDIE(DbgVariable &DV, 142 bool Abstract = false); 143 144 std::unique_ptr<DIE> constructVariableDIE(DbgVariable &DV, 145 const LexicalScope &Scope, 146 DIE *&ObjectPointer); 147 148 /// A helper function to create children of a Scope DIE. 149 DIE *createScopeChildrenDIE(LexicalScope *Scope, 150 SmallVectorImpl<std::unique_ptr<DIE>> &Children, 151 unsigned *ChildScopeCount = nullptr); 152 153 /// \brief Construct a DIE for this subprogram scope. 154 void constructSubprogramScopeDIE(LexicalScope *Scope); 155 156 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE); 157 158 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 159 160 /// \brief Construct import_module DIE. 161 std::unique_ptr<DIE> 162 constructImportedEntityDIE(const DIImportedEntity &Module); 163 164 void finishSubprogramDefinition(DISubprogram SP); 165 166 void collectDeadVariables(DISubprogram SP); 167 168 /// Set the skeleton unit associated with this unit. setSkeleton(DwarfCompileUnit & Skel)169 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; } 170 getSectionSym()171 MCSymbol *getSectionSym() const { 172 assert(Section); 173 return SectionSym; 174 } 175 176 /// Pass in the SectionSym even though we could recreate it in every compile 177 /// unit (type units will have actually distinct symbols once they're in 178 /// comdat sections). initSection(const MCSection * Section,MCSymbol * SectionSym)179 void initSection(const MCSection *Section, MCSymbol *SectionSym) { 180 DwarfUnit::initSection(Section); 181 this->SectionSym = SectionSym; 182 183 // Don't bother labeling the .dwo unit, as its offset isn't used. 184 if (!Skeleton) 185 LabelBegin = 186 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID()); 187 } 188 getLength()189 unsigned getLength() { 190 return sizeof(uint32_t) + // Length field 191 getHeaderSize() + UnitDie.getSize(); 192 } 193 194 void emitHeader(const MCSymbol *ASectionSym) const override; 195 getLabelBegin()196 MCSymbol *getLabelBegin() const { 197 assert(Section); 198 return LabelBegin; 199 } 200 201 /// Add a new global name to the compile unit. 202 void addGlobalName(StringRef Name, DIE &Die, DIScope Context) override; 203 204 /// Add a new global type to the compile unit. 205 void addGlobalType(DIType Ty, const DIE &Die, DIScope Context) override; 206 getGlobalNames()207 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; } getGlobalTypes()208 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; } 209 210 /// Add DW_AT_location attribute for a DbgVariable based on provided 211 /// MachineLocation. 212 void addVariableAddress(const DbgVariable &DV, DIE &Die, 213 MachineLocation Location); 214 /// Add an address attribute to a die based on the location provided. 215 void addAddress(DIE &Die, dwarf::Attribute Attribute, 216 const MachineLocation &Location, bool Indirect = false); 217 218 /// Start with the address based on the location provided, and generate the 219 /// DWARF information necessary to find the actual variable (navigating the 220 /// extra location information encoded in the type) based on the starting 221 /// location. Add the DWARF information to the die. 222 void addComplexAddress(const DbgVariable &DV, DIE &Die, 223 dwarf::Attribute Attribute, 224 const MachineLocation &Location); 225 226 /// Add a Dwarf loclistptr attribute data and value. 227 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index); 228 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie); 229 230 /// Add a Dwarf expression attribute data and value. 231 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr); 232 233 void applySubprogramAttributesToDefinition(DISubprogram SP, DIE &SPDie); 234 235 /// getRangeLists - Get the vector of range lists. getRangeLists()236 const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 237 return (Skeleton ? Skeleton : this)->CURangeLists; 238 } 239 240 /// getRanges - Get the list of ranges for this unit. getRanges()241 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; } takeRanges()242 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); } 243 setBaseAddress(const MCSymbol * Base)244 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; } getBaseAddress()245 const MCSymbol *getBaseAddress() const { return BaseAddress; } 246 }; 247 248 } // end llvm namespace 249 250 #endif 251