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