1 //===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
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"
44 class DwarfCompileUnit final : public DwarfUnit {
45 /// A numeric ID unique among all CUs in the module
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;
52 /// Skeleton unit associated with this unit.
53 DwarfCompileUnit *Skeleton = nullptr;
55 /// The start of the unit within its section.
58 /// The start of the unit macro info within macro section.
59 MCSymbol *MacroLabelBegin;
61 using ImportedEntityList = SmallVector<const MDNode *, 8>;
62 using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
64 ImportedEntityMap ImportedEntities;
66 /// GlobalNames - A map of globally visible named entities for this unit.
67 StringMap<const DIE *> GlobalNames;
69 /// GlobalTypes - A map of globally visible types for this unit.
70 StringMap<const DIE *> GlobalTypes;
72 // List of range lists for a given compile unit, separate from the ranges for
74 SmallVector<RangeSpanList, 1> CURangeLists;
76 // List of ranges for a given compile unit.
77 SmallVector<RangeSpan, 2> CURanges;
79 // The base address of this unit, if any. Used for relative references in
81 const MCSymbol *BaseAddress = nullptr;
83 DenseMap<const MDNode *, DIE *> AbstractSPDies;
84 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
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);
90 bool isDwoUnit() const override;
92 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
93 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
94 return AbstractSPDies;
95 return DU->getAbstractSPDies();
98 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() {
99 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
100 return AbstractVariables;
101 return DU->getAbstractVariables();
105 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
106 DwarfDebug *DW, DwarfFile *DWU);
108 unsigned getUniqueID() const { return UniqueID; }
110 DwarfCompileUnit *getSkeleton() const {
114 bool includeMinimalInlineScopes() const;
118 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
119 void applyStmtList(DIE &D);
121 /// A pair of GlobalVariable and DIExpression.
123 const GlobalVariable *Var;
124 const DIExpression *Expr;
127 /// Get or create global variable DIE.
129 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
130 ArrayRef<GlobalExpr> GlobalExprs);
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);
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);
142 DwarfCompileUnit &getCU() override { return *this; }
144 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
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.
153 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
154 ImportedEntities[LocalScope].push_back(IE);
157 /// addRange - Add an address range to the list of ranges for this unit.
158 void addRange(RangeSpan Range);
160 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
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
166 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
168 void constructScopeDIE(LexicalScope *Scope,
169 SmallVectorImpl<DIE *> &FinalChildren);
171 /// \brief A helper function to construct a RangeSpanList for a given
173 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
175 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
177 void attachRangesOrLowHighPC(DIE &D,
178 const SmallVectorImpl<InsnRange> &Ranges);
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);
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);
188 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
189 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
191 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
192 DIE *&ObjectPointer);
194 /// A helper function to create children of a Scope DIE.
195 DIE *createScopeChildrenDIE(LexicalScope *Scope,
196 SmallVectorImpl<DIE *> &Children,
197 bool *HasNonScopeChildren = nullptr);
199 /// \brief Construct a DIE for this subprogram scope.
200 void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope);
202 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
204 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
206 /// \brief Construct import_module DIE.
207 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
209 void finishSubprogramDefinition(const DISubprogram *SP);
210 void finishVariableDefinition(const DbgVariable &Var);
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);
219 /// Set the skeleton unit associated with this unit.
220 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
222 unsigned getLength() {
223 return sizeof(uint32_t) + // Length field
224 getHeaderSize() + getUnitDie().getSize();
227 void emitHeader(bool UseOffsets) override;
229 MCSymbol *getLabelBegin() const {
230 assert(getSection());
234 MCSymbol *getMacroLabelBegin() const {
235 return MacroLabelBegin;
238 /// Add a new global name to the compile unit.
239 void addGlobalName(StringRef Name, const DIE &Die,
240 const DIScope *Context) override;
242 /// Add a new global name present in a type unit to this compile unit.
243 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
245 /// Add a new global type to the compile unit.
246 void addGlobalType(const DIType *Ty, const DIE &Die,
247 const DIScope *Context) override;
249 /// Add a new global type present in a type unit to this compile unit.
250 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
252 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
253 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
255 /// Add DW_AT_location attribute for a DbgVariable based on provided
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);
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);
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);
275 /// Add a Dwarf expression attribute data and value.
276 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
278 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
281 /// getRangeLists - Get the vector of range lists.
282 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
283 return (Skeleton ? Skeleton : this)->CURangeLists;
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); }
290 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
291 const MCSymbol *getBaseAddress() const { return BaseAddress; }
293 bool hasDwarfPubSections() const;
296 } // end namespace llvm
298 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H