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   /// 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;
35 
36   /// Skeleton unit associated with this unit.
37   DwarfCompileUnit *Skeleton;
38 
39   /// The start of the unit within its section.
40   MCSymbol *LabelBegin;
41 
42   typedef llvm::SmallVector<const MDNode *, 8> ImportedEntityList;
43   typedef llvm::DenseMap<const MDNode *, ImportedEntityList>
44   ImportedEntityMap;
45 
46   ImportedEntityMap ImportedEntities;
47 
48   /// GlobalNames - A map of globally visible named entities for this unit.
49   StringMap<const DIE *> GlobalNames;
50 
51   /// GlobalTypes - A map of globally visible types for this unit.
52   StringMap<const DIE *> GlobalTypes;
53 
54   // List of range lists for a given compile unit, separate from the ranges for
55   // the CU itself.
56   SmallVector<RangeSpanList, 1> CURangeLists;
57 
58   // List of ranges for a given compile unit.
59   SmallVector<RangeSpan, 2> CURanges;
60 
61   // The base address of this unit, if any. Used for relative references in
62   // ranges/locs.
63   const MCSymbol *BaseAddress;
64 
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);
68 
69   bool isDwoUnit() const override;
70 
71   bool includeMinimalInlineScopes() const;
72 
73 public:
74   DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
75                    DwarfDebug *DW, DwarfFile *DWU);
76 
getSkeleton()77   DwarfCompileUnit *getSkeleton() const {
78     return Skeleton;
79   }
80 
81   void initStmtList();
82 
83   /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
84   void applyStmtList(DIE &D);
85 
86   /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
87   DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV);
88 
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);
93 
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);
98 
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);
102 
getCU()103   DwarfCompileUnit &getCU() override { return *this; }
104 
105   unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
106 
addImportedEntity(const DIImportedEntity * IE)107   void addImportedEntity(const DIImportedEntity* IE) {
108     ImportedEntities[IE->getScope()].push_back(IE);
109   }
110 
111   /// addRange - Add an address range to the list of ranges for this unit.
112   void addRange(RangeSpan Range);
113 
114   void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
115 
116   /// addSectionLabel - Add a Dwarf section label attribute data and value.
117   ///
118   DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
119                                       const MCSymbol *Label,
120                                       const MCSymbol *Sec);
121 
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
125   /// variables.
126   DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
127 
128   void constructScopeDIE(LexicalScope *Scope,
129                          SmallVectorImpl<DIE *> &FinalChildren);
130 
131   /// \brief A helper function to construct a RangeSpanList for a given
132   /// lexical scope.
133   void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
134 
135   void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
136 
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);
142 
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);
146 
147   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
148   DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
149 
150   DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
151                             DIE *&ObjectPointer);
152 
153   /// A helper function to create children of a Scope DIE.
154   DIE *createScopeChildrenDIE(LexicalScope *Scope,
155                               SmallVectorImpl<DIE *> &Children,
156                               unsigned *ChildScopeCount = nullptr);
157 
158   /// \brief Construct a DIE for this subprogram scope.
159   void constructSubprogramScopeDIE(LexicalScope *Scope);
160 
161   DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
162 
163   void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
164 
165   /// \brief Construct import_module DIE.
166   DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
167 
168   void finishSubprogramDefinition(const DISubprogram *SP);
169 
170   void collectDeadVariables(const DISubprogram *SP);
171 
172   /// Set the skeleton unit associated with this unit.
setSkeleton(DwarfCompileUnit & Skel)173   void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
174 
getSectionSym()175   const MCSymbol *getSectionSym() const {
176     assert(Section);
177     return Section->getBeginSymbol();
178   }
179 
getLength()180   unsigned getLength() {
181     return sizeof(uint32_t) + // Length field
182         getHeaderSize() + UnitDie.getSize();
183   }
184 
185   void emitHeader(bool UseOffsets) override;
186 
getLabelBegin()187   MCSymbol *getLabelBegin() const {
188     assert(Section);
189     return LabelBegin;
190   }
191 
192   /// Add a new global name to the compile unit.
193   void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override;
194 
195   /// Add a new global type to the compile unit.
196   void addGlobalType(const DIType *Ty, const DIE &Die,
197                      const DIScope *Context) override;
198 
getGlobalNames()199   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
getGlobalTypes()200   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
201 
202   /// Add DW_AT_location attribute for a DbgVariable based on provided
203   /// MachineLocation.
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);
209 
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);
217 
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);
221 
222   /// Add a Dwarf expression attribute data and value.
223   void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
224 
225   void applySubprogramAttributesToDefinition(const DISubprogram *SP,
226                                              DIE &SPDie);
227 
228   /// getRangeLists - Get the vector of range lists.
getRangeLists()229   const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
230     return (Skeleton ? Skeleton : this)->CURangeLists;
231   }
232 
233   /// getRanges - Get the list of ranges for this unit.
getRanges()234   const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
takeRanges()235   SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
236 
setBaseAddress(const MCSymbol * Base)237   void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
getBaseAddress()238   const MCSymbol *getBaseAddress() const { return BaseAddress; }
239 };
240 
241 } // end llvm namespace
242 
243 #endif
244