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 "DwarfUnit.h"
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/Support/Dwarf.h"
31 class DwarfCompileUnit : public DwarfUnit {
32 /// A numeric ID unique among all CUs in the module
35 /// Offset of the UnitDie from beginning of debug info section.
36 unsigned DebugInfoOffset = 0;
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;
42 /// Skeleton unit associated with this unit.
43 DwarfCompileUnit *Skeleton;
45 /// The start of the unit within its section.
48 /// The start of the unit macro info within macro section.
49 MCSymbol *MacroLabelBegin;
51 typedef llvm::SmallVector<const MDNode *, 8> ImportedEntityList;
52 typedef llvm::DenseMap<const MDNode *, ImportedEntityList>
55 ImportedEntityMap ImportedEntities;
57 /// GlobalNames - A map of globally visible named entities for this unit.
58 StringMap<const DIE *> GlobalNames;
60 /// GlobalTypes - A map of globally visible types for this unit.
61 StringMap<const DIE *> GlobalTypes;
63 // List of range lists for a given compile unit, separate from the ranges for
65 SmallVector<RangeSpanList, 1> CURangeLists;
67 // List of ranges for a given compile unit.
68 SmallVector<RangeSpan, 2> CURanges;
70 // The base address of this unit, if any. Used for relative references in
72 const MCSymbol *BaseAddress;
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);
78 bool isDwoUnit() const override;
80 bool includeMinimalInlineScopes() const;
83 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
84 DwarfDebug *DW, DwarfFile *DWU);
86 unsigned getUniqueID() const { return UniqueID; }
87 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
88 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
90 DwarfCompileUnit *getSkeleton() const {
96 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
97 void applyStmtList(DIE &D);
99 /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
100 DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV);
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);
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);
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);
116 DwarfCompileUnit &getCU() override { return *this; }
118 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
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.
127 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
128 ImportedEntities[LocalScope].push_back(IE);
131 /// addRange - Add an address range to the list of ranges for this unit.
132 void addRange(RangeSpan Range);
134 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
136 /// addSectionLabel - Add a Dwarf section label attribute data and value.
138 DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
139 const MCSymbol *Label,
140 const MCSymbol *Sec);
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
146 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
148 void constructScopeDIE(LexicalScope *Scope,
149 SmallVectorImpl<DIE *> &FinalChildren);
151 /// \brief A helper function to construct a RangeSpanList for a given
153 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
155 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
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);
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);
167 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
168 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
170 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
171 DIE *&ObjectPointer);
173 /// A helper function to create children of a Scope DIE.
174 DIE *createScopeChildrenDIE(LexicalScope *Scope,
175 SmallVectorImpl<DIE *> &Children,
176 unsigned *ChildScopeCount = nullptr);
178 /// \brief Construct a DIE for this subprogram scope.
179 void constructSubprogramScopeDIE(LexicalScope *Scope);
181 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
183 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
185 /// \brief Construct import_module DIE.
186 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
188 void finishSubprogramDefinition(const DISubprogram *SP);
190 /// Set the skeleton unit associated with this unit.
191 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
193 const MCSymbol *getSectionSym() const {
195 return Section->getBeginSymbol();
198 unsigned getLength() {
199 return sizeof(uint32_t) + // Length field
200 getHeaderSize() + UnitDie.getSize();
203 void emitHeader(bool UseOffsets) override;
205 MCSymbol *getLabelBegin() const {
210 MCSymbol *getMacroLabelBegin() const {
211 return MacroLabelBegin;
214 /// Add a new global name to the compile unit.
215 void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override;
217 /// Add a new global type to the compile unit.
218 void addGlobalType(const DIType *Ty, const DIE &Die,
219 const DIScope *Context) override;
221 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
222 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
224 /// Add DW_AT_location attribute for a DbgVariable based on provided
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);
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);
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);
244 /// Add a Dwarf expression attribute data and value.
245 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
247 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
250 /// getRangeLists - Get the vector of range lists.
251 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
252 return (Skeleton ? Skeleton : this)->CURangeLists;
255 /// getRanges - Get the list of ranges for this unit.
256 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
257 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
259 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
260 const MCSymbol *getBaseAddress() const { return BaseAddress; }
263 } // end llvm namespace