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