a73cb27c25f45e5e97d66f5edaba0d8e339844fb
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfCompileUnit.h
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/Support/Dwarf.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/DebugInfo.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   unsigned stmtListIndex;
35
36   /// Skeleton unit associated with this unit.
37   DwarfCompileUnit *Skeleton;
38
39   /// A label at the start of the non-dwo section related to this unit.
40   MCSymbol *SectionSym;
41
42   /// The start of the unit within its section.
43   MCSymbol *LabelBegin;
44
45   /// GlobalNames - A map of globally visible named entities for this unit.
46   StringMap<const DIE *> GlobalNames;
47
48   /// GlobalTypes - A map of globally visible types for this unit.
49   StringMap<const DIE *> GlobalTypes;
50
51   // List of range lists for a given compile unit, separate from the ranges for
52   // the CU itself.
53   SmallVector<RangeSpanList, 1> CURangeLists;
54
55   // List of ranges for a given compile unit.
56   SmallVector<RangeSpan, 1> CURanges;
57
58   // The base address of this unit, if any. Used for relative references in
59   // ranges/locs.
60   const MCSymbol *BaseAddress;
61
62   /// \brief Construct a DIE for the given DbgVariable without initializing the
63   /// DbgVariable's DIE reference.
64   std::unique_ptr<DIE> constructVariableDIEImpl(const DbgVariable &DV,
65                                                 bool Abstract);
66
67   bool isDwoUnit() const override;
68
69 public:
70   DwarfCompileUnit(unsigned UID, DICompileUnit Node, AsmPrinter *A,
71                    DwarfDebug *DW, DwarfFile *DWU);
72
73   DwarfCompileUnit *getSkeleton() const {
74     return Skeleton;
75   }
76
77   void initStmtList(MCSymbol *DwarfLineSectionSym);
78
79   /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
80   void applyStmtList(DIE &D);
81
82   /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
83   DIE *getOrCreateGlobalVariableDIE(DIGlobalVariable GV);
84
85   /// addLabelAddress - Add a dwarf label attribute data and value using
86   /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
87   void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
88                        const MCSymbol *Label);
89
90   /// addLocalLabelAddress - Add a dwarf label attribute data and value using
91   /// DW_FORM_addr only.
92   void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
93                             const MCSymbol *Label);
94
95   /// addSectionDelta - Add a label delta attribute data and value.
96   void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
97                        const MCSymbol *Lo);
98
99   DwarfCompileUnit &getCU() override { return *this; }
100
101   unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
102
103   /// addRange - Add an address range to the list of ranges for this unit.
104   void addRange(RangeSpan Range);
105
106   void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
107
108   /// addSectionLabel - Add a Dwarf section label attribute data and value.
109   ///
110   void addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
111                        const MCSymbol *Label, const MCSymbol *Sec);
112
113   /// \brief Find DIE for the given subprogram and attach appropriate
114   /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
115   /// variables in this scope then create and insert DIEs for these
116   /// variables.
117   DIE &updateSubprogramScopeDIE(DISubprogram SP);
118
119   void constructScopeDIE(LexicalScope *Scope,
120                          SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren);
121
122   /// \brief A helper function to construct a RangeSpanList for a given
123   /// lexical scope.
124   void addScopeRangeList(DIE &ScopeDIE,
125                          const SmallVectorImpl<InsnRange> &Range);
126
127   void attachRangesOrLowHighPC(DIE &D,
128                                const SmallVectorImpl<InsnRange> &Ranges);
129
130   /// \brief This scope represents inlined body of a function. Construct
131   /// DIE to represent this concrete inlined copy of the function.
132   std::unique_ptr<DIE> constructInlinedScopeDIE(LexicalScope *Scope);
133
134   /// \brief Construct new DW_TAG_lexical_block for this scope and
135   /// attach DW_AT_low_pc/DW_AT_high_pc labels.
136   std::unique_ptr<DIE> constructLexicalScopeDIE(LexicalScope *Scope);
137
138   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
139   std::unique_ptr<DIE> constructVariableDIE(DbgVariable &DV,
140                                             bool Abstract = false);
141
142   std::unique_ptr<DIE> constructVariableDIE(DbgVariable &DV,
143                                             const LexicalScope &Scope,
144                                             DIE *&ObjectPointer);
145
146   /// A helper function to create children of a Scope DIE.
147   DIE *createScopeChildrenDIE(LexicalScope *Scope,
148                               SmallVectorImpl<std::unique_ptr<DIE>> &Children,
149                               unsigned *ChildScopeCount = nullptr);
150
151   /// \brief Construct a DIE for this subprogram scope.
152   void constructSubprogramScopeDIE(LexicalScope *Scope);
153
154   DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
155
156   void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
157
158   /// \brief Construct import_module DIE.
159   std::unique_ptr<DIE>
160   constructImportedEntityDIE(const DIImportedEntity &Module);
161
162   void finishSubprogramDefinition(DISubprogram SP);
163
164   void collectDeadVariables(DISubprogram SP);
165
166   /// Set the skeleton unit associated with this unit.
167   void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
168
169   MCSymbol *getSectionSym() const {
170     assert(Section);
171     return SectionSym;
172   }
173
174   /// Pass in the SectionSym even though we could recreate it in every compile
175   /// unit (type units will have actually distinct symbols once they're in
176   /// comdat sections).
177   void initSection(const MCSection *Section, MCSymbol *SectionSym) {
178     DwarfUnit::initSection(Section);
179     this->SectionSym = SectionSym;
180
181     // Don't bother labeling the .dwo unit, as its offset isn't used.
182     if (!Skeleton)
183       LabelBegin =
184           Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
185   }
186
187   unsigned getLength() {
188     return sizeof(uint32_t) + // Length field
189         getHeaderSize() + UnitDie.getSize();
190   }
191
192   void emitHeader(const MCSymbol *ASectionSym) const override;
193
194   MCSymbol *getLabelBegin() const {
195     assert(Section);
196     return LabelBegin;
197   }
198
199   /// Add a new global name to the compile unit.
200   void addGlobalName(StringRef Name, DIE &Die, DIScope Context) override;
201
202   /// Add a new global type to the compile unit.
203   void addGlobalType(DIType Ty, const DIE &Die, DIScope Context) override;
204
205   const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
206   const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
207
208   /// Add DW_AT_location attribute for a DbgVariable based on provided
209   /// MachineLocation.
210   void addVariableAddress(const DbgVariable &DV, DIE &Die,
211                           MachineLocation Location);
212   /// Add an address attribute to a die based on the location provided.
213   void addAddress(DIE &Die, dwarf::Attribute Attribute,
214                   const MachineLocation &Location, bool Indirect = false);
215
216   /// Start with the address based on the location provided, and generate the
217   /// DWARF information necessary to find the actual variable (navigating the
218   /// extra location information encoded in the type) based on the starting
219   /// location.  Add the DWARF information to the die.
220   void addComplexAddress(const DbgVariable &DV, DIE &Die,
221                          dwarf::Attribute Attribute,
222                          const MachineLocation &Location);
223
224   /// Add a Dwarf loclistptr attribute data and value.
225   void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
226   void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
227
228   /// Add a Dwarf expression attribute data and value.
229   void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
230
231   void applySubprogramAttributesToDefinition(DISubprogram SP, DIE &SPDie);
232
233   /// getRangeLists - Get the vector of range lists.
234   const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
235     return (Skeleton ? Skeleton : this)->CURangeLists;
236   }
237
238   /// getRanges - Get the list of ranges for this unit.
239   const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
240
241   void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
242   const MCSymbol *getBaseAddress() const { return BaseAddress; }
243 };
244
245 } // end llvm namespace
246
247 #endif