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