1277323Sdim//===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
2277323Sdim//
3277323Sdim//                     The LLVM Compiler Infrastructure
4277323Sdim//
5277323Sdim// This file is distributed under the University of Illinois Open Source
6277323Sdim// License. See LICENSE.TXT for details.
7277323Sdim//
8277323Sdim//===----------------------------------------------------------------------===//
9277323Sdim//
10277323Sdim// This file contains support for writing dwarf compile unit.
11277323Sdim//
12277323Sdim//===----------------------------------------------------------------------===//
13277323Sdim
14277323Sdim#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15277323Sdim#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16277323Sdim
17277323Sdim#include "DwarfUnit.h"
18277323Sdim#include "llvm/ADT/StringRef.h"
19277323Sdim#include "llvm/IR/DebugInfo.h"
20277323Sdim#include "llvm/Support/Dwarf.h"
21277323Sdim
22277323Sdimnamespace llvm {
23277323Sdim
24277323Sdimclass AsmPrinter;
25277323Sdimclass DIE;
26277323Sdimclass DwarfDebug;
27277323Sdimclass DwarfFile;
28277323Sdimclass MCSymbol;
29277323Sdimclass LexicalScope;
30277323Sdim
31277323Sdimclass DwarfCompileUnit : public DwarfUnit {
32277323Sdim  /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
33277323Sdim  /// the need to search for it in applyStmtList.
34288943Sdim  DIE::value_iterator StmtListValue;
35277323Sdim
36277323Sdim  /// Skeleton unit associated with this unit.
37277323Sdim  DwarfCompileUnit *Skeleton;
38277323Sdim
39277323Sdim  /// The start of the unit within its section.
40277323Sdim  MCSymbol *LabelBegin;
41277323Sdim
42296417Sdim  typedef llvm::SmallVector<const MDNode *, 8> ImportedEntityList;
43296417Sdim  typedef llvm::DenseMap<const MDNode *, ImportedEntityList>
44296417Sdim  ImportedEntityMap;
45296417Sdim
46296417Sdim  ImportedEntityMap ImportedEntities;
47296417Sdim
48277323Sdim  /// GlobalNames - A map of globally visible named entities for this unit.
49277323Sdim  StringMap<const DIE *> GlobalNames;
50277323Sdim
51277323Sdim  /// GlobalTypes - A map of globally visible types for this unit.
52277323Sdim  StringMap<const DIE *> GlobalTypes;
53277323Sdim
54277323Sdim  // List of range lists for a given compile unit, separate from the ranges for
55277323Sdim  // the CU itself.
56277323Sdim  SmallVector<RangeSpanList, 1> CURangeLists;
57277323Sdim
58277323Sdim  // List of ranges for a given compile unit.
59277323Sdim  SmallVector<RangeSpan, 2> CURanges;
60277323Sdim
61277323Sdim  // The base address of this unit, if any. Used for relative references in
62277323Sdim  // ranges/locs.
63277323Sdim  const MCSymbol *BaseAddress;
64277323Sdim
65277323Sdim  /// \brief Construct a DIE for the given DbgVariable without initializing the
66277323Sdim  /// DbgVariable's DIE reference.
67288943Sdim  DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
68277323Sdim
69277323Sdim  bool isDwoUnit() const override;
70277323Sdim
71277323Sdim  bool includeMinimalInlineScopes() const;
72277323Sdim
73277323Sdimpublic:
74288943Sdim  DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
75277323Sdim                   DwarfDebug *DW, DwarfFile *DWU);
76277323Sdim
77277323Sdim  DwarfCompileUnit *getSkeleton() const {
78277323Sdim    return Skeleton;
79277323Sdim  }
80277323Sdim
81288943Sdim  void initStmtList();
82277323Sdim
83277323Sdim  /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
84277323Sdim  void applyStmtList(DIE &D);
85277323Sdim
86277323Sdim  /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
87288943Sdim  DIE *getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV);
88277323Sdim
89277323Sdim  /// addLabelAddress - Add a dwarf label attribute data and value using
90277323Sdim  /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
91277323Sdim  void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
92277323Sdim                       const MCSymbol *Label);
93277323Sdim
94277323Sdim  /// addLocalLabelAddress - Add a dwarf label attribute data and value using
95277323Sdim  /// DW_FORM_addr only.
96277323Sdim  void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
97277323Sdim                            const MCSymbol *Label);
98277323Sdim
99277323Sdim  /// addSectionDelta - Add a label delta attribute data and value.
100288943Sdim  DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
101288943Sdim                                      const MCSymbol *Hi, const MCSymbol *Lo);
102277323Sdim
103277323Sdim  DwarfCompileUnit &getCU() override { return *this; }
104277323Sdim
105277323Sdim  unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
106277323Sdim
107296417Sdim  void addImportedEntity(const DIImportedEntity* IE) {
108296417Sdim    ImportedEntities[IE->getScope()].push_back(IE);
109296417Sdim  }
110296417Sdim
111277323Sdim  /// addRange - Add an address range to the list of ranges for this unit.
112277323Sdim  void addRange(RangeSpan Range);
113277323Sdim
114277323Sdim  void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
115277323Sdim
116277323Sdim  /// addSectionLabel - Add a Dwarf section label attribute data and value.
117277323Sdim  ///
118288943Sdim  DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
119288943Sdim                                      const MCSymbol *Label,
120288943Sdim                                      const MCSymbol *Sec);
121277323Sdim
122277323Sdim  /// \brief Find DIE for the given subprogram and attach appropriate
123277323Sdim  /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
124277323Sdim  /// variables in this scope then create and insert DIEs for these
125277323Sdim  /// variables.
126288943Sdim  DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
127277323Sdim
128277323Sdim  void constructScopeDIE(LexicalScope *Scope,
129288943Sdim                         SmallVectorImpl<DIE *> &FinalChildren);
130277323Sdim
131277323Sdim  /// \brief A helper function to construct a RangeSpanList for a given
132277323Sdim  /// lexical scope.
133277323Sdim  void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
134277323Sdim
135277323Sdim  void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
136277323Sdim
137277323Sdim  void attachRangesOrLowHighPC(DIE &D,
138277323Sdim                               const SmallVectorImpl<InsnRange> &Ranges);
139277323Sdim  /// \brief This scope represents inlined body of a function. Construct
140277323Sdim  /// DIE to represent this concrete inlined copy of the function.
141288943Sdim  DIE *constructInlinedScopeDIE(LexicalScope *Scope);
142277323Sdim
143277323Sdim  /// \brief Construct new DW_TAG_lexical_block for this scope and
144277323Sdim  /// attach DW_AT_low_pc/DW_AT_high_pc labels.
145288943Sdim  DIE *constructLexicalScopeDIE(LexicalScope *Scope);
146277323Sdim
147277323Sdim  /// constructVariableDIE - Construct a DIE for the given DbgVariable.
148288943Sdim  DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
149277323Sdim
150288943Sdim  DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
151288943Sdim                            DIE *&ObjectPointer);
152277323Sdim
153277323Sdim  /// A helper function to create children of a Scope DIE.
154277323Sdim  DIE *createScopeChildrenDIE(LexicalScope *Scope,
155288943Sdim                              SmallVectorImpl<DIE *> &Children,
156277323Sdim                              unsigned *ChildScopeCount = nullptr);
157277323Sdim
158277323Sdim  /// \brief Construct a DIE for this subprogram scope.
159277323Sdim  void constructSubprogramScopeDIE(LexicalScope *Scope);
160277323Sdim
161277323Sdim  DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
162277323Sdim
163277323Sdim  void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
164277323Sdim
165277323Sdim  /// \brief Construct import_module DIE.
166288943Sdim  DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
167277323Sdim
168288943Sdim  void finishSubprogramDefinition(const DISubprogram *SP);
169277323Sdim
170288943Sdim  void collectDeadVariables(const DISubprogram *SP);
171277323Sdim
172277323Sdim  /// Set the skeleton unit associated with this unit.
173277323Sdim  void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
174277323Sdim
175288943Sdim  const MCSymbol *getSectionSym() const {
176277323Sdim    assert(Section);
177288943Sdim    return Section->getBeginSymbol();
178277323Sdim  }
179277323Sdim
180277323Sdim  unsigned getLength() {
181277323Sdim    return sizeof(uint32_t) + // Length field
182277323Sdim        getHeaderSize() + UnitDie.getSize();
183277323Sdim  }
184277323Sdim
185288943Sdim  void emitHeader(bool UseOffsets) override;
186277323Sdim
187277323Sdim  MCSymbol *getLabelBegin() const {
188277323Sdim    assert(Section);
189277323Sdim    return LabelBegin;
190277323Sdim  }
191277323Sdim
192277323Sdim  /// Add a new global name to the compile unit.
193288943Sdim  void addGlobalName(StringRef Name, DIE &Die, const DIScope *Context) override;
194277323Sdim
195277323Sdim  /// Add a new global type to the compile unit.
196288943Sdim  void addGlobalType(const DIType *Ty, const DIE &Die,
197288943Sdim                     const DIScope *Context) override;
198277323Sdim
199277323Sdim  const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
200277323Sdim  const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
201277323Sdim
202277323Sdim  /// Add DW_AT_location attribute for a DbgVariable based on provided
203277323Sdim  /// MachineLocation.
204277323Sdim  void addVariableAddress(const DbgVariable &DV, DIE &Die,
205277323Sdim                          MachineLocation Location);
206277323Sdim  /// Add an address attribute to a die based on the location provided.
207277323Sdim  void addAddress(DIE &Die, dwarf::Attribute Attribute,
208288943Sdim                  const MachineLocation &Location);
209277323Sdim
210277323Sdim  /// Start with the address based on the location provided, and generate the
211277323Sdim  /// DWARF information necessary to find the actual variable (navigating the
212277323Sdim  /// extra location information encoded in the type) based on the starting
213277323Sdim  /// location.  Add the DWARF information to the die.
214277323Sdim  void addComplexAddress(const DbgVariable &DV, DIE &Die,
215277323Sdim                         dwarf::Attribute Attribute,
216277323Sdim                         const MachineLocation &Location);
217277323Sdim
218277323Sdim  /// Add a Dwarf loclistptr attribute data and value.
219277323Sdim  void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
220277323Sdim  void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
221277323Sdim
222277323Sdim  /// Add a Dwarf expression attribute data and value.
223277323Sdim  void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
224277323Sdim
225288943Sdim  void applySubprogramAttributesToDefinition(const DISubprogram *SP,
226288943Sdim                                             DIE &SPDie);
227277323Sdim
228277323Sdim  /// getRangeLists - Get the vector of range lists.
229277323Sdim  const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
230277323Sdim    return (Skeleton ? Skeleton : this)->CURangeLists;
231277323Sdim  }
232277323Sdim
233277323Sdim  /// getRanges - Get the list of ranges for this unit.
234277323Sdim  const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
235277323Sdim  SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
236277323Sdim
237277323Sdim  void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
238277323Sdim  const MCSymbol *getBaseAddress() const { return BaseAddress; }
239277323Sdim};
240277323Sdim
241277323Sdim} // end llvm namespace
242277323Sdim
243277323Sdim#endif
244