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