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/IR/DebugInfo.h"
19 #include "llvm/Support/Dwarf.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 : 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                                     const GlobalVariable *Global);
102 
103   /// addLabelAddress - Add a dwarf label attribute data and value using
104   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
105   void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
106                        const MCSymbol *Label);
107 
108   /// addLocalLabelAddress - Add a dwarf label attribute data and value using
109   /// DW_FORM_addr only.
110   void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
111                             const MCSymbol *Label);
112 
113   /// addSectionDelta - Add a label delta attribute data and value.
114   DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
115                                       const MCSymbol *Hi, const MCSymbol *Lo);
116 
117   DwarfCompileUnit &getCU() override { return *this; }
118 
119   unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
120 
121   void addImportedEntity(const DIImportedEntity* IE) {
122     DIScope *Scope = IE->getScope();
123     assert(Scope && "Invalid Scope encoding!");
124     if (!isa<DILocalScope>(Scope))
125       // No need to add imported enities that are not local declaration.
126       return;
127 
128     auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
129     ImportedEntities[LocalScope].push_back(IE);
130   }
131 
132   /// addRange - Add an address range to the list of ranges for this unit.
133   void addRange(RangeSpan Range);
134 
135   void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
136 
137   /// addSectionLabel - Add a Dwarf section label attribute data and value.
138   ///
139   DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
140                                       const MCSymbol *Label,
141                                       const MCSymbol *Sec);
142 
143   /// \brief Find DIE for the given subprogram and attach appropriate
144   /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
145   /// variables in this scope then create and insert DIEs for these
146   /// variables.
147   DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
148 
149   void constructScopeDIE(LexicalScope *Scope,
150                          SmallVectorImpl<DIE *> &FinalChildren);
151 
152   /// \brief A helper function to construct a RangeSpanList for a given
153   /// lexical scope.
154   void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
155 
156   void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
157 
158   void attachRangesOrLowHighPC(DIE &D,
159                                const SmallVectorImpl<InsnRange> &Ranges);
160   /// \brief This scope represents inlined body of a function. Construct
161   /// DIE to represent this concrete inlined copy of the function.
162   DIE *constructInlinedScopeDIE(LexicalScope *Scope);
163 
164   /// \brief Construct new DW_TAG_lexical_block for this scope and
165   /// attach DW_AT_low_pc/DW_AT_high_pc labels.
166   DIE *constructLexicalScopeDIE(LexicalScope *Scope);
167 
168   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
169   DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
170 
171   DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
172                             DIE *&ObjectPointer);
173 
174   /// A helper function to create children of a Scope DIE.
175   DIE *createScopeChildrenDIE(LexicalScope *Scope,
176                               SmallVectorImpl<DIE *> &Children,
177                               unsigned *ChildScopeCount = nullptr);
178 
179   /// \brief Construct a DIE for this subprogram scope.
180   void constructSubprogramScopeDIE(LexicalScope *Scope);
181 
182   DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
183 
184   void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
185 
186   /// \brief Construct import_module DIE.
187   DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
188 
189   void finishSubprogramDefinition(const DISubprogram *SP);
190 
191   /// Set the skeleton unit associated with this unit.
192   void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
193 
194   const MCSymbol *getSectionSym() const {
195     assert(Section);
196     return Section->getBeginSymbol();
197   }
198 
199   unsigned getLength() {
200     return sizeof(uint32_t) + // Length field
201         getHeaderSize() + UnitDie.getSize();
202   }
203 
204   void emitHeader(bool UseOffsets) override;
205 
206   MCSymbol *getLabelBegin() const {
207     assert(Section);
208     return LabelBegin;
209   }
210 
211   MCSymbol *getMacroLabelBegin() const {
212     return MacroLabelBegin;
213   }
214 
215   /// Add a new global name to the compile unit.
216   void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override;
217 
218   /// Add a new global type to the compile unit.
219   void addGlobalType(const DIType *Ty, const DIE &Die,
220                      const DIScope *Context) override;
221 
222   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
223   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
224 
225   /// Add DW_AT_location attribute for a DbgVariable based on provided
226   /// MachineLocation.
227   void addVariableAddress(const DbgVariable &DV, DIE &Die,
228                           MachineLocation Location);
229   /// Add an address attribute to a die based on the location provided.
230   void addAddress(DIE &Die, dwarf::Attribute Attribute,
231                   const MachineLocation &Location);
232 
233   /// Start with the address based on the location provided, and generate the
234   /// DWARF information necessary to find the actual variable (navigating the
235   /// extra location information encoded in the type) based on the starting
236   /// location.  Add the DWARF information to the die.
237   void addComplexAddress(const DbgVariable &DV, DIE &Die,
238                          dwarf::Attribute Attribute,
239                          const MachineLocation &Location);
240 
241   /// Add a Dwarf loclistptr attribute data and value.
242   void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
243   void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
244 
245   /// Add a Dwarf expression attribute data and value.
246   void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
247 
248   void applySubprogramAttributesToDefinition(const DISubprogram *SP,
249                                              DIE &SPDie);
250 
251   /// getRangeLists - Get the vector of range lists.
252   const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
253     return (Skeleton ? Skeleton : this)->CURangeLists;
254   }
255 
256   /// getRanges - Get the list of ranges for this unit.
257   const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
258   SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
259 
260   void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
261   const MCSymbol *getBaseAddress() const { return BaseAddress; }
262 };
263 
264 } // end llvm namespace
265 
266 #endif
267