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 getUniqueID()86 unsigned getUniqueID() const { return UniqueID; } getDebugInfoOffset()87 unsigned getDebugInfoOffset() const { return DebugInfoOffset; } setDebugInfoOffset(unsigned DbgInfoOff)88 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; } 89 getSkeleton()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 getCU()116 DwarfCompileUnit &getCU() override { return *this; } 117 118 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override; 119 addImportedEntity(const DIImportedEntity * IE)120 void addImportedEntity(const DIImportedEntity* IE) { 121 DIScope *Scope = IE->getScope(); 122 assert(Scope && "Invalid Scope encoding!"); 123 if (!isa<DILocalScope>(Scope)) 124 // No need to add imported enities that are not local declaration. 125 return; 126 127 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope(); 128 ImportedEntities[LocalScope].push_back(IE); 129 } 130 131 /// addRange - Add an address range to the list of ranges for this unit. 132 void addRange(RangeSpan Range); 133 134 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End); 135 136 /// addSectionLabel - Add a Dwarf section label attribute data and value. 137 /// 138 DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute, 139 const MCSymbol *Label, 140 const MCSymbol *Sec); 141 142 /// \brief Find DIE for the given subprogram and attach appropriate 143 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global 144 /// variables in this scope then create and insert DIEs for these 145 /// variables. 146 DIE &updateSubprogramScopeDIE(const DISubprogram *SP); 147 148 void constructScopeDIE(LexicalScope *Scope, 149 SmallVectorImpl<DIE *> &FinalChildren); 150 151 /// \brief A helper function to construct a RangeSpanList for a given 152 /// lexical scope. 153 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range); 154 155 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges); 156 157 void attachRangesOrLowHighPC(DIE &D, 158 const SmallVectorImpl<InsnRange> &Ranges); 159 /// \brief This scope represents inlined body of a function. Construct 160 /// DIE to represent this concrete inlined copy of the function. 161 DIE *constructInlinedScopeDIE(LexicalScope *Scope); 162 163 /// \brief Construct new DW_TAG_lexical_block for this scope and 164 /// attach DW_AT_low_pc/DW_AT_high_pc labels. 165 DIE *constructLexicalScopeDIE(LexicalScope *Scope); 166 167 /// constructVariableDIE - Construct a DIE for the given DbgVariable. 168 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false); 169 170 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope, 171 DIE *&ObjectPointer); 172 173 /// A helper function to create children of a Scope DIE. 174 DIE *createScopeChildrenDIE(LexicalScope *Scope, 175 SmallVectorImpl<DIE *> &Children, 176 unsigned *ChildScopeCount = nullptr); 177 178 /// \brief Construct a DIE for this subprogram scope. 179 void constructSubprogramScopeDIE(LexicalScope *Scope); 180 181 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE); 182 183 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope); 184 185 /// \brief Construct import_module DIE. 186 DIE *constructImportedEntityDIE(const DIImportedEntity *Module); 187 188 void finishSubprogramDefinition(const DISubprogram *SP); 189 190 /// Set the skeleton unit associated with this unit. setSkeleton(DwarfCompileUnit & Skel)191 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; } 192 getSectionSym()193 const MCSymbol *getSectionSym() const { 194 assert(Section); 195 return Section->getBeginSymbol(); 196 } 197 getLength()198 unsigned getLength() { 199 return sizeof(uint32_t) + // Length field 200 getHeaderSize() + UnitDie.getSize(); 201 } 202 203 void emitHeader(bool UseOffsets) override; 204 getLabelBegin()205 MCSymbol *getLabelBegin() const { 206 assert(Section); 207 return LabelBegin; 208 } 209 getMacroLabelBegin()210 MCSymbol *getMacroLabelBegin() const { 211 return MacroLabelBegin; 212 } 213 214 /// Add a new global name to the compile unit. 215 void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override; 216 217 /// Add a new global type to the compile unit. 218 void addGlobalType(const DIType *Ty, const DIE &Die, 219 const DIScope *Context) override; 220 getGlobalNames()221 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; } getGlobalTypes()222 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; } 223 224 /// Add DW_AT_location attribute for a DbgVariable based on provided 225 /// MachineLocation. 226 void addVariableAddress(const DbgVariable &DV, DIE &Die, 227 MachineLocation Location); 228 /// Add an address attribute to a die based on the location provided. 229 void addAddress(DIE &Die, dwarf::Attribute Attribute, 230 const MachineLocation &Location); 231 232 /// Start with the address based on the location provided, and generate the 233 /// DWARF information necessary to find the actual variable (navigating the 234 /// extra location information encoded in the type) based on the starting 235 /// location. Add the DWARF information to the die. 236 void addComplexAddress(const DbgVariable &DV, DIE &Die, 237 dwarf::Attribute Attribute, 238 const MachineLocation &Location); 239 240 /// Add a Dwarf loclistptr attribute data and value. 241 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index); 242 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie); 243 244 /// Add a Dwarf expression attribute data and value. 245 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr); 246 247 void applySubprogramAttributesToDefinition(const DISubprogram *SP, 248 DIE &SPDie); 249 250 /// getRangeLists - Get the vector of range lists. getRangeLists()251 const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 252 return (Skeleton ? Skeleton : this)->CURangeLists; 253 } 254 255 /// getRanges - Get the list of ranges for this unit. getRanges()256 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; } takeRanges()257 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); } 258 setBaseAddress(const MCSymbol * Base)259 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; } getBaseAddress()260 const MCSymbol *getBaseAddress() const { return BaseAddress; } 261 }; 262 263 } // end llvm namespace 264 265 #endif 266