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/IR/DebugInfo.h" 19 #include "llvm/Support/Dwarf.h" 20 21 namespace llvm { 22 23 class StringRef; 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 /// A numeric ID unique among all CUs in the module 33 unsigned UniqueID; 34 35 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding 36 /// the need to search for it in applyStmtList. 37 DIE::value_iterator StmtListValue; 38 39 /// Skeleton unit associated with this unit. 40 DwarfCompileUnit *Skeleton; 41 42 /// The start of the unit within its section. 43 MCSymbol *LabelBegin; 44 45 /// The start of the unit macro info within macro section. 46 MCSymbol *MacroLabelBegin; 47 48 typedef llvm::SmallVector<const MDNode *, 8> ImportedEntityList; 49 typedef llvm::DenseMap<const MDNode *, ImportedEntityList> 50 ImportedEntityMap; 51 52 ImportedEntityMap ImportedEntities; 53 54 /// GlobalNames - A map of globally visible named entities for this unit. 55 StringMap<const DIE *> GlobalNames; 56 57 /// GlobalTypes - A map of globally visible types for this unit. 58 StringMap<const DIE *> GlobalTypes; 59 60 // List of range lists for a given compile unit, separate from the ranges for 61 // the CU itself. 62 SmallVector<RangeSpanList, 1> CURangeLists; 63 64 // List of ranges for a given compile unit. 65 SmallVector<RangeSpan, 2> CURanges; 66 67 // The base address of this unit, if any. Used for relative references in 68 // ranges/locs. 69 const MCSymbol *BaseAddress; 70 71 /// \brief Construct a DIE for the given DbgVariable without initializing the 72 /// DbgVariable's DIE reference. 73 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract); 74 75 bool isDwoUnit() const override; 76 77 bool includeMinimalInlineScopes() const; 78 79 public: 80 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A, 81 DwarfDebug *DW, DwarfFile *DWU); 82 83 unsigned getUniqueID() const { return UniqueID; } 84 85 DwarfCompileUnit *getSkeleton() const { 86 return Skeleton; 87 } 88 89 void initStmtList(); 90 91 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE. 92 void applyStmtList(DIE &D); 93 94 /// getOrCreateGlobalVariableDIE - get or create global variable DIE. 95 DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV, 96 const GlobalVariable *Global); 97 98 /// addLabelAddress - Add a dwarf label attribute data and value using 99 /// either DW_FORM_addr or DW_FORM_GNU_addr_index. 100 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute, 101 const MCSymbol *Label); 102 103 /// addLocalLabelAddress - Add a dwarf label attribute data and value using 104 /// DW_FORM_addr only. 105 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute, 106 const MCSymbol *Label); 107 108 /// addSectionDelta - Add a label delta attribute data and value. 109 DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute, 110 const MCSymbol *Hi, const MCSymbol *Lo); 111 112 DwarfCompileUnit &getCU() override { return *this; } 113 114 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override; 115 116 void addImportedEntity(const DIImportedEntity* IE) { 117 DIScope *Scope = IE->getScope(); 118 assert(Scope && "Invalid Scope encoding!"); 119 if (!isa<DILocalScope>(Scope)) 120 // No need to add imported enities that are not local declaration. 121 return; 122 123 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope(); 124 ImportedEntities[LocalScope].push_back(IE); 125 } 126 127 /// addRange - Add an address range to the list of ranges for this unit. 128 void addRange(RangeSpan Range); 129 130 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End); 131 132 /// addSectionLabel - Add a Dwarf section label attribute data and value. 133 /// 134 DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 135 const MCSymbol *Label, 136 const MCSymbol *Sec); 137 138 /// \brief Find DIE for the given subprogram and attach appropriate 139 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global 140 /// variables in this scope then create and insert DIEs for these 141 /// variables. 142 DIE &updateSubprogramScopeDIE(const DISubprogram *SP); 143 144 void constructScopeDIE(LexicalScope *Scope, 145 SmallVectorImpl<DIE *> &FinalChildren); 146 147 /// \brief A helper function to construct a RangeSpanList for a given 148 /// lexical scope. 149 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range); 150 151 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges); 152 153 void attachRangesOrLowHighPC(DIE &D, 154 const SmallVectorImpl<InsnRange> &Ranges); 155 /// \brief This scope represents inlined body of a function. Construct 156 /// DIE to represent this concrete inlined copy of the function. 157 DIE *constructInlinedScopeDIE(LexicalScope *Scope); 158 159 /// \brief Construct new DW_TAG_lexical_block for this scope and 160 /// attach DW_AT_low_pc/DW_AT_high_pc labels. 161 DIE *constructLexicalScopeDIE(LexicalScope *Scope); 162 163 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 164 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false); 165 166 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope, 167 DIE *&ObjectPointer); 168 169 /// A helper function to create children of a Scope DIE. 170 DIE *createScopeChildrenDIE(LexicalScope *Scope, 171 SmallVectorImpl<DIE *> &Children, 172 unsigned *ChildScopeCount = nullptr); 173 174 /// \brief Construct a DIE for this subprogram scope. 175 void constructSubprogramScopeDIE(LexicalScope *Scope); 176 177 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE); 178 179 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 180 181 /// \brief Construct import_module DIE. 182 DIE *constructImportedEntityDIE(const DIImportedEntity *Module); 183 184 void finishSubprogramDefinition(const DISubprogram *SP); 185 186 /// Set the skeleton unit associated with this unit. 187 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; } 188 189 unsigned getLength() { 190 return sizeof(uint32_t) + // Length field 191 getHeaderSize() + getUnitDie().getSize(); 192 } 193 194 void emitHeader(bool UseOffsets) override; 195 196 MCSymbol *getLabelBegin() const { 197 assert(getSection()); 198 return LabelBegin; 199 } 200 201 MCSymbol *getMacroLabelBegin() const { 202 return MacroLabelBegin; 203 } 204 205 /// Add a new global name to the compile unit. 206 void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override; 207 208 /// Add a new global type to the compile unit. 209 void addGlobalType(const DIType *Ty, const DIE &Die, 210 const DIScope *Context) override; 211 212 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; } 213 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; } 214 215 /// Add DW_AT_location attribute for a DbgVariable based on provided 216 /// MachineLocation. 217 void addVariableAddress(const DbgVariable &DV, DIE &Die, 218 MachineLocation Location); 219 /// Add an address attribute to a die based on the location provided. 220 void addAddress(DIE &Die, dwarf::Attribute Attribute, 221 const MachineLocation &Location); 222 223 /// Start with the address based on the location provided, and generate the 224 /// DWARF information necessary to find the actual variable (navigating the 225 /// extra location information encoded in the type) based on the starting 226 /// location. Add the DWARF information to the die. 227 void addComplexAddress(const DbgVariable &DV, DIE &Die, 228 dwarf::Attribute Attribute, 229 const MachineLocation &Location); 230 231 /// Add a Dwarf loclistptr attribute data and value. 232 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index); 233 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie); 234 235 /// Add a Dwarf expression attribute data and value. 236 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr); 237 238 void applySubprogramAttributesToDefinition(const DISubprogram *SP, 239 DIE &SPDie); 240 241 /// getRangeLists - Get the vector of range lists. 242 const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 243 return (Skeleton ? Skeleton : this)->CURangeLists; 244 } 245 246 /// getRanges - Get the list of ranges for this unit. 247 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; } 248 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); } 249 250 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; } 251 const MCSymbol *getBaseAddress() const { return BaseAddress; } 252 }; 253 254 } // end llvm namespace 255 256 #endif 257