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