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/ADT/StringRef.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/Support/Dwarf.h"
31 class DwarfCompileUnit : public DwarfUnit {
32 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
33 /// the need to search for it in applyStmtList.
34 DIE::value_iterator StmtListValue;
36 /// Skeleton unit associated with this unit.
37 DwarfCompileUnit *Skeleton;
39 /// The start of the unit within its section.
42 typedef llvm::SmallVector<const MDNode *, 8> ImportedEntityList;
43 typedef llvm::DenseMap<const MDNode *, ImportedEntityList>
46 ImportedEntityMap ImportedEntities;
48 /// GlobalNames - A map of globally visible named entities for this unit.
49 StringMap<const DIE *> GlobalNames;
51 /// GlobalTypes - A map of globally visible types for this unit.
52 StringMap<const DIE *> GlobalTypes;
54 // List of range lists for a given compile unit, separate from the ranges for
56 SmallVector<RangeSpanList, 1> CURangeLists;
58 // List of ranges for a given compile unit.
59 SmallVector<RangeSpan, 2> CURanges;
61 // The base address of this unit, if any. Used for relative references in
63 const MCSymbol *BaseAddress;
65 /// \brief Construct a DIE for the given DbgVariable without initializing the
66 /// DbgVariable's DIE reference.
67 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
69 bool isDwoUnit() const override;
71 bool includeMinimalInlineScopes() const;
74 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
75 DwarfDebug *DW, DwarfFile *DWU);
77 DwarfCompileUnit *getSkeleton() const {
83 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
84 void applyStmtList(DIE &D);
86 /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
87 DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV);
89 /// addLabelAddress - Add a dwarf label attribute data and value using
90 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
91 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
92 const MCSymbol *Label);
94 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
95 /// DW_FORM_addr only.
96 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
97 const MCSymbol *Label);
99 /// addSectionDelta - Add a label delta attribute data and value.
100 DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
101 const MCSymbol *Hi, const MCSymbol *Lo);
103 DwarfCompileUnit &getCU() override { return *this; }
105 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
107 void addImportedEntity(const DIImportedEntity* IE) {
108 ImportedEntities[IE->getScope()].push_back(IE);
111 /// addRange - Add an address range to the list of ranges for this unit.
112 void addRange(RangeSpan Range);
114 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
116 /// addSectionLabel - Add a Dwarf section label attribute data and value.
118 DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
119 const MCSymbol *Label,
120 const MCSymbol *Sec);
122 /// \brief Find DIE for the given subprogram and attach appropriate
123 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
124 /// variables in this scope then create and insert DIEs for these
126 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
128 void constructScopeDIE(LexicalScope *Scope,
129 SmallVectorImpl<DIE *> &FinalChildren);
131 /// \brief A helper function to construct a RangeSpanList for a given
133 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
135 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
137 void attachRangesOrLowHighPC(DIE &D,
138 const SmallVectorImpl<InsnRange> &Ranges);
139 /// \brief This scope represents inlined body of a function. Construct
140 /// DIE to represent this concrete inlined copy of the function.
141 DIE *constructInlinedScopeDIE(LexicalScope *Scope);
143 /// \brief Construct new DW_TAG_lexical_block for this scope and
144 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
145 DIE *constructLexicalScopeDIE(LexicalScope *Scope);
147 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
148 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
150 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
151 DIE *&ObjectPointer);
153 /// A helper function to create children of a Scope DIE.
154 DIE *createScopeChildrenDIE(LexicalScope *Scope,
155 SmallVectorImpl<DIE *> &Children,
156 unsigned *ChildScopeCount = nullptr);
158 /// \brief Construct a DIE for this subprogram scope.
159 void constructSubprogramScopeDIE(LexicalScope *Scope);
161 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
163 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
165 /// \brief Construct import_module DIE.
166 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
168 void finishSubprogramDefinition(const DISubprogram *SP);
170 void collectDeadVariables(const DISubprogram *SP);
172 /// Set the skeleton unit associated with this unit.
173 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
175 const MCSymbol *getSectionSym() const {
177 return Section->getBeginSymbol();
180 unsigned getLength() {
181 return sizeof(uint32_t) + // Length field
182 getHeaderSize() + UnitDie.getSize();
185 void emitHeader(bool UseOffsets) override;
187 MCSymbol *getLabelBegin() const {
192 /// Add a new global name to the compile unit.
193 void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override;
195 /// Add a new global type to the compile unit.
196 void addGlobalType(const DIType *Ty, const DIE &Die,
197 const DIScope *Context) override;
199 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
200 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
202 /// Add DW_AT_location attribute for a DbgVariable based on provided
204 void addVariableAddress(const DbgVariable &DV, DIE &Die,
205 MachineLocation Location);
206 /// Add an address attribute to a die based on the location provided.
207 void addAddress(DIE &Die, dwarf::Attribute Attribute,
208 const MachineLocation &Location);
210 /// Start with the address based on the location provided, and generate the
211 /// DWARF information necessary to find the actual variable (navigating the
212 /// extra location information encoded in the type) based on the starting
213 /// location. Add the DWARF information to the die.
214 void addComplexAddress(const DbgVariable &DV, DIE &Die,
215 dwarf::Attribute Attribute,
216 const MachineLocation &Location);
218 /// Add a Dwarf loclistptr attribute data and value.
219 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
220 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
222 /// Add a Dwarf expression attribute data and value.
223 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
225 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
228 /// getRangeLists - Get the vector of range lists.
229 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
230 return (Skeleton ? Skeleton : this)->CURangeLists;
233 /// getRanges - Get the list of ranges for this unit.
234 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
235 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
237 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
238 const MCSymbol *getBaseAddress() const { return BaseAddress; }
241 } // end llvm namespace