Replace unnecessary #include directive with forward declarations.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfDebug.h
1 //===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- 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 debug info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15 #define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
16
17 #include "AsmPrinterHandler.h"
18 #include "DIE.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/CodeGen/LexicalScopes.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/MC/MachineLocation.h"
29 #include "llvm/MC/MCDwarf.h"
30 #include "llvm/Support/Allocator.h"
31
32 namespace llvm {
33
34 class AsmPrinter;
35 class ByteStreamer;
36 class DwarfDebug;
37 class DwarfUnit;
38 class DwarfCompileUnit;
39 class ConstantInt;
40 class ConstantFP;
41 class DbgVariable;
42 class MachineFrameInfo;
43 class MachineModuleInfo;
44 class MachineOperand;
45 class MCAsmInfo;
46 class MCObjectFileInfo;
47 class DIEAbbrev;
48 class DIE;
49 class DIELoc;
50 class DIEEntry;
51
52 //===----------------------------------------------------------------------===//
53 /// \brief This class is used to record source line correspondence.
54 class SrcLineInfo {
55   unsigned Line;     // Source line number.
56   unsigned Column;   // Source column.
57   unsigned SourceID; // Source ID number.
58   MCSymbol *Label;   // Label in code ID number.
59 public:
60   SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
61       : Line(L), Column(C), SourceID(S), Label(label) {}
62
63   // Accessors
64   unsigned getLine() const { return Line; }
65   unsigned getColumn() const { return Column; }
66   unsigned getSourceID() const { return SourceID; }
67   MCSymbol *getLabel() const { return Label; }
68 };
69
70 /// \brief This struct describes location entries emitted in the .debug_loc
71 /// section.
72 class DebugLocEntry {
73   // Begin and end symbols for the address range that this location is valid.
74   const MCSymbol *Begin;
75   const MCSymbol *End;
76
77   // Type of entry that this represents.
78   enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
79   enum EntryType EntryKind;
80
81   union {
82     int64_t Int;
83     const ConstantFP *CFP;
84     const ConstantInt *CIP;
85   } Constants;
86
87   // The location in the machine frame.
88   MachineLocation Loc;
89
90   // The variable to which this location entry corresponds.
91   const MDNode *Variable;
92
93   // Whether this location has been merged.
94   bool Merged;
95
96 public:
97   DebugLocEntry() : Begin(0), End(0), Variable(0), Merged(false) {
98     Constants.Int = 0;
99   }
100   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
101                 const MDNode *V)
102       : Begin(B), End(E), Loc(L), Variable(V), Merged(false) {
103     Constants.Int = 0;
104     EntryKind = E_Location;
105   }
106   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i)
107       : Begin(B), End(E), Variable(0), Merged(false) {
108     Constants.Int = i;
109     EntryKind = E_Integer;
110   }
111   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr)
112       : Begin(B), End(E), Variable(0), Merged(false) {
113     Constants.CFP = FPtr;
114     EntryKind = E_ConstantFP;
115   }
116   DebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantInt *IPtr)
117       : Begin(B), End(E), Variable(0), Merged(false) {
118     Constants.CIP = IPtr;
119     EntryKind = E_ConstantInt;
120   }
121
122   /// \brief Empty entries are also used as a trigger to emit temp label. Such
123   /// labels are referenced is used to find debug_loc offset for a given DIE.
124   bool isEmpty() const { return Begin == 0 && End == 0; }
125   bool isMerged() const { return Merged; }
126   void Merge(DebugLocEntry *Next) {
127     if (!(Begin && Loc == Next->Loc && End == Next->Begin))
128       return;
129     Next->Begin = Begin;
130     Merged = true;
131   }
132   bool isLocation() const { return EntryKind == E_Location; }
133   bool isInt() const { return EntryKind == E_Integer; }
134   bool isConstantFP() const { return EntryKind == E_ConstantFP; }
135   bool isConstantInt() const { return EntryKind == E_ConstantInt; }
136   int64_t getInt() const { return Constants.Int; }
137   const ConstantFP *getConstantFP() const { return Constants.CFP; }
138   const ConstantInt *getConstantInt() const { return Constants.CIP; }
139   const MDNode *getVariable() const { return Variable; }
140   const MCSymbol *getBeginSym() const { return Begin; }
141   const MCSymbol *getEndSym() const { return End; }
142   MachineLocation getLoc() const { return Loc; }
143 };
144
145 //===----------------------------------------------------------------------===//
146 /// \brief This class is used to track local variable information.
147 class DbgVariable {
148   DIVariable Var;             // Variable Descriptor.
149   DIE *TheDIE;                // Variable DIE.
150   unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
151   DbgVariable *AbsVar;        // Corresponding Abstract variable, if any.
152   const MachineInstr *MInsn;  // DBG_VALUE instruction of the variable.
153   int FrameIndex;
154   DwarfDebug *DD;
155
156 public:
157   // AbsVar may be NULL.
158   DbgVariable(DIVariable V, DbgVariable *AV, DwarfDebug *DD)
159       : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
160         FrameIndex(~0), DD(DD) {}
161
162   // Accessors.
163   DIVariable getVariable() const { return Var; }
164   void setDIE(DIE *D) { TheDIE = D; }
165   DIE *getDIE() const { return TheDIE; }
166   void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
167   unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
168   StringRef getName() const { return Var.getName(); }
169   DbgVariable *getAbstractVariable() const { return AbsVar; }
170   const MachineInstr *getMInsn() const { return MInsn; }
171   void setMInsn(const MachineInstr *M) { MInsn = M; }
172   int getFrameIndex() const { return FrameIndex; }
173   void setFrameIndex(int FI) { FrameIndex = FI; }
174   // Translate tag to proper Dwarf tag.
175   uint16_t getTag() const {
176     if (Var.getTag() == dwarf::DW_TAG_arg_variable)
177       return dwarf::DW_TAG_formal_parameter;
178
179     return dwarf::DW_TAG_variable;
180   }
181   /// \brief Return true if DbgVariable is artificial.
182   bool isArtificial() const {
183     if (Var.isArtificial())
184       return true;
185     if (getType().isArtificial())
186       return true;
187     return false;
188   }
189
190   bool isObjectPointer() const {
191     if (Var.isObjectPointer())
192       return true;
193     if (getType().isObjectPointer())
194       return true;
195     return false;
196   }
197
198   bool variableHasComplexAddress() const {
199     assert(Var.isVariable() && "Invalid complex DbgVariable!");
200     return Var.hasComplexAddress();
201   }
202   bool isBlockByrefVariable() const {
203     assert(Var.isVariable() && "Invalid complex DbgVariable!");
204     return Var.isBlockByrefVariable();
205   }
206   unsigned getNumAddrElements() const {
207     assert(Var.isVariable() && "Invalid complex DbgVariable!");
208     return Var.getNumAddrElements();
209   }
210   uint64_t getAddrElement(unsigned i) const { return Var.getAddrElement(i); }
211   DIType getType() const;
212
213 private:
214   /// resolve - Look in the DwarfDebug map for the MDNode that
215   /// corresponds to the reference.
216   template <typename T> T resolve(DIRef<T> Ref) const;
217 };
218
219 /// \brief Collects and handles information specific to a particular
220 /// collection of units. This collection represents all of the units
221 /// that will be ultimately output into a single object file.
222 class DwarfFile {
223   // Target of Dwarf emission, used for sizing of abbreviations.
224   AsmPrinter *Asm;
225
226   // Used to uniquely define abbreviations.
227   FoldingSet<DIEAbbrev> AbbreviationsSet;
228
229   // A list of all the unique abbreviations in use.
230   std::vector<DIEAbbrev *> Abbreviations;
231
232   // A pointer to all units in the section.
233   SmallVector<DwarfUnit *, 1> CUs;
234
235   // Collection of strings for this unit and assorted symbols.
236   // A String->Symbol mapping of strings used by indirect
237   // references.
238   typedef StringMap<std::pair<MCSymbol *, unsigned>, BumpPtrAllocator &>
239   StrPool;
240   StrPool StringPool;
241   unsigned NextStringPoolNumber;
242   std::string StringPref;
243
244   struct AddressPoolEntry {
245     unsigned Number;
246     bool TLS;
247     AddressPoolEntry(unsigned Number, bool TLS) : Number(Number), TLS(TLS) {}
248   };
249   // Collection of addresses for this unit and assorted labels.
250   // A Symbol->unsigned mapping of addresses used by indirect
251   // references.
252   typedef DenseMap<const MCSymbol *, AddressPoolEntry> AddrPool;
253   AddrPool AddressPool;
254   unsigned NextAddrPoolNumber;
255
256 public:
257   DwarfFile(AsmPrinter *AP, const char *Pref, BumpPtrAllocator &DA)
258       : Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref),
259         AddressPool(), NextAddrPoolNumber(0) {}
260
261   ~DwarfFile();
262
263   const SmallVectorImpl<DwarfUnit *> &getUnits() { return CUs; }
264
265   /// \brief Compute the size and offset of a DIE given an incoming Offset.
266   unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
267
268   /// \brief Compute the size and offset of all the DIEs.
269   void computeSizeAndOffsets();
270
271   /// \brief Define a unique number for the abbreviation.
272   void assignAbbrevNumber(DIEAbbrev &Abbrev);
273
274   /// \brief Add a unit to the list of CUs.
275   void addUnit(DwarfUnit *CU) { CUs.push_back(CU); }
276
277   /// \brief Emit all of the units to the section listed with the given
278   /// abbreviation section.
279   void emitUnits(DwarfDebug *DD, const MCSection *ASection,
280                  const MCSymbol *ASectionSym);
281
282   /// \brief Emit a set of abbreviations to the specific section.
283   void emitAbbrevs(const MCSection *);
284
285   /// \brief Emit all of the strings to the section given.
286   void emitStrings(const MCSection *StrSection, const MCSection *OffsetSection,
287                    const MCSymbol *StrSecSym);
288
289   /// \brief Emit all of the addresses to the section given.
290   void emitAddresses(const MCSection *AddrSection);
291
292   /// \brief Returns the entry into the start of the pool.
293   MCSymbol *getStringPoolSym();
294
295   /// \brief Returns an entry into the string pool with the given
296   /// string text.
297   MCSymbol *getStringPoolEntry(StringRef Str);
298
299   /// \brief Returns the index into the string pool with the given
300   /// string text.
301   unsigned getStringPoolIndex(StringRef Str);
302
303   /// \brief Returns the string pool.
304   StrPool *getStringPool() { return &StringPool; }
305
306   /// \brief Returns the index into the address pool with the given
307   /// label/symbol.
308   unsigned getAddrPoolIndex(const MCSymbol *Sym, bool TLS = false);
309
310   /// \brief Returns the address pool.
311   AddrPool *getAddrPool() { return &AddressPool; }
312 };
313
314 /// \brief Helper used to pair up a symbol and its DWARF compile unit.
315 struct SymbolCU {
316   SymbolCU(DwarfCompileUnit *CU, const MCSymbol *Sym) : Sym(Sym), CU(CU) {}
317   const MCSymbol *Sym;
318   DwarfCompileUnit *CU;
319 };
320
321 /// \brief Collects and handles dwarf debug information.
322 class DwarfDebug : public AsmPrinterHandler {
323   // Target of Dwarf emission.
324   AsmPrinter *Asm;
325
326   // Collected machine module information.
327   MachineModuleInfo *MMI;
328
329   // All DIEValues are allocated through this allocator.
330   BumpPtrAllocator DIEValueAllocator;
331
332   // Handle to the compile unit used for the inline extension handling,
333   // this is just so that the DIEValue allocator has a place to store
334   // the particular elements.
335   // FIXME: Store these off of DwarfDebug instead?
336   DwarfCompileUnit *FirstCU;
337
338   // Maps MDNode with its corresponding DwarfCompileUnit.
339   MapVector<const MDNode *, DwarfCompileUnit *> CUMap;
340
341   // Maps subprogram MDNode with its corresponding DwarfCompileUnit.
342   DenseMap<const MDNode *, DwarfCompileUnit *> SPMap;
343
344   // Maps a CU DIE with its corresponding DwarfCompileUnit.
345   DenseMap<const DIE *, DwarfCompileUnit *> CUDieMap;
346
347   /// Maps MDNodes for type sysstem with the corresponding DIEs. These DIEs can
348   /// be shared across CUs, that is why we keep the map here instead
349   /// of in DwarfCompileUnit.
350   DenseMap<const MDNode *, DIE *> MDTypeNodeToDieMap;
351
352   // Used to unique C++ member function declarations.
353   StringMap<const MDNode *> OdrMemberMap;
354
355   // List of all labels used in aranges generation.
356   std::vector<SymbolCU> ArangeLabels;
357
358   // Size of each symbol emitted (for those symbols that have a specific size).
359   DenseMap<const MCSymbol *, uint64_t> SymSize;
360
361   // Provides a unique id per text section.
362   typedef DenseMap<const MCSection *, SmallVector<SymbolCU, 8> > SectionMapType;
363   SectionMapType SectionMap;
364
365   // List of arguments for current function.
366   SmallVector<DbgVariable *, 8> CurrentFnArguments;
367
368   LexicalScopes LScopes;
369
370   // Collection of abstract subprogram DIEs.
371   DenseMap<const MDNode *, DIE *> AbstractSPDies;
372
373   // Collection of dbg variables of a scope.
374   typedef DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> >
375   ScopeVariablesMap;
376   ScopeVariablesMap ScopeVariables;
377
378   // Collection of abstract variables.
379   DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
380
381   // Collection of DebugLocEntry.
382   SmallVector<DebugLocEntry, 4> DotDebugLocEntries;
383
384   // Collection of subprogram DIEs that are marked (at the end of the module)
385   // as DW_AT_inline.
386   SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
387
388   // This is a collection of subprogram MDNodes that are processed to
389   // create DIEs.
390   SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
391
392   // Maps instruction with label emitted before instruction.
393   DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
394
395   // Maps instruction with label emitted after instruction.
396   DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
397
398   // Every user variable mentioned by a DBG_VALUE instruction in order of
399   // appearance.
400   SmallVector<const MDNode *, 8> UserVariables;
401
402   // For each user variable, keep a list of DBG_VALUE instructions in order.
403   // The list can also contain normal instructions that clobber the previous
404   // DBG_VALUE.
405   typedef DenseMap<const MDNode *, SmallVector<const MachineInstr *, 4> >
406   DbgValueHistoryMap;
407   DbgValueHistoryMap DbgValues;
408
409   // Previous instruction's location information. This is used to determine
410   // label location to indicate scope boundries in dwarf debug info.
411   DebugLoc PrevInstLoc;
412   MCSymbol *PrevLabel;
413
414   // This location indicates end of function prologue and beginning of function
415   // body.
416   DebugLoc PrologEndLoc;
417
418   // If nonnull, stores the current machine function we're processing.
419   const MachineFunction *CurFn;
420
421   // If nonnull, stores the current machine instruction we're processing.
422   const MachineInstr *CurMI;
423
424   // Section Symbols: these are assembler temporary labels that are emitted at
425   // the beginning of each supported dwarf section.  These are used to form
426   // section offsets and are created by EmitSectionLabels.
427   MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
428   MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
429   MCSymbol *DwarfDebugLocSectionSym, *DwarfLineSectionSym, *DwarfAddrSectionSym;
430   MCSymbol *FunctionBeginSym, *FunctionEndSym;
431   MCSymbol *DwarfInfoDWOSectionSym, *DwarfAbbrevDWOSectionSym;
432   MCSymbol *DwarfStrDWOSectionSym;
433   MCSymbol *DwarfGnuPubNamesSectionSym, *DwarfGnuPubTypesSectionSym;
434
435   // As an optimization, there is no need to emit an entry in the directory
436   // table for the same directory as DW_AT_comp_dir.
437   StringRef CompilationDir;
438
439   // Counter for assigning globally unique IDs for ranges.
440   unsigned GlobalRangeCount;
441
442   // Holder for the file specific debug information.
443   DwarfFile InfoHolder;
444
445   // Holders for the various debug information flags that we might need to
446   // have exposed. See accessor functions below for description.
447
448   // Holder for imported entities.
449   typedef SmallVector<std::pair<const MDNode *, const MDNode *>, 32>
450   ImportedEntityMap;
451   ImportedEntityMap ScopesWithImportedEntities;
452
453   // Map from MDNodes for user-defined types to the type units that describe
454   // them.
455   DenseMap<const MDNode *, const DwarfTypeUnit *> DwarfTypeUnits;
456
457   // Whether to emit the pubnames/pubtypes sections.
458   bool HasDwarfPubSections;
459
460   // Whether or not to use AT_ranges for compilation units.
461   bool HasCURanges;
462
463   // Whether we emitted a function into a section other than the default
464   // text.
465   bool UsedNonDefaultText;
466
467   // Version of dwarf we're emitting.
468   unsigned DwarfVersion;
469
470   // Maps from a type identifier to the actual MDNode.
471   DITypeIdentifierMap TypeIdentifierMap;
472
473   // DWARF5 Experimental Options
474   bool HasDwarfAccelTables;
475   bool HasSplitDwarf;
476
477   // Separated Dwarf Variables
478   // In general these will all be for bits that are left in the
479   // original object file, rather than things that are meant
480   // to be in the .dwo sections.
481
482   // Holder for the skeleton information.
483   DwarfFile SkeletonHolder;
484
485   // Store file names for type units under fission in a line table header that
486   // will be emitted into debug_line.dwo.
487   MCDwarfDwoLineTable SplitTypeUnitFileTable;
488
489   void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
490
491   const SmallVectorImpl<DwarfUnit *> &getUnits() {
492     return InfoHolder.getUnits();
493   }
494
495   /// \brief Find abstract variable associated with Var.
496   DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
497
498   /// \brief Find DIE for the given subprogram and attach appropriate
499   /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
500   /// variables in this scope then create and insert DIEs for these
501   /// variables.
502   DIE *updateSubprogramScopeDIE(DwarfCompileUnit *SPCU, DISubprogram SP);
503
504   /// \brief A helper function to check whether the DIE for a given Scope is
505   /// going to be null.
506   bool isLexicalScopeDIENull(LexicalScope *Scope);
507
508   /// \brief A helper function to construct a RangeSpanList for a given
509   /// lexical scope.
510   void addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
511                          const SmallVectorImpl<InsnRange> &Range);
512
513   /// \brief Construct new DW_TAG_lexical_block for this scope and
514   /// attach DW_AT_low_pc/DW_AT_high_pc labels.
515   DIE *constructLexicalScopeDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope);
516
517   /// \brief This scope represents inlined body of a function. Construct
518   /// DIE to represent this concrete inlined copy of the function.
519   DIE *constructInlinedScopeDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope);
520
521   /// \brief Construct a DIE for this scope.
522   DIE *constructScopeDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope);
523   /// A helper function to create children of a Scope DIE.
524   DIE *createScopeChildrenDIE(DwarfCompileUnit *TheCU, LexicalScope *Scope,
525                               SmallVectorImpl<DIE *> &Children);
526
527   /// \brief Emit initial Dwarf sections with a label at the start of each one.
528   void emitSectionLabels();
529
530   /// \brief Compute the size and offset of a DIE given an incoming Offset.
531   unsigned computeSizeAndOffset(DIE *Die, unsigned Offset);
532
533   /// \brief Compute the size and offset of all the DIEs.
534   void computeSizeAndOffsets();
535
536   /// \brief Attach DW_AT_inline attribute with inlined subprogram DIEs.
537   void computeInlinedDIEs();
538
539   /// \brief Collect info for variables that were optimized out.
540   void collectDeadVariables();
541
542   /// \brief Finish off debug information after all functions have been
543   /// processed.
544   void finalizeModuleInfo();
545
546   /// \brief Emit labels to close any remaining sections that have been left
547   /// open.
548   void endSections();
549
550   /// \brief Emit the debug info section.
551   void emitDebugInfo();
552
553   /// \brief Emit the abbreviation section.
554   void emitAbbreviations();
555
556   /// \brief Emit the last address of the section and the end of
557   /// the line matrix.
558   void emitEndOfLineMatrix(unsigned SectionEnd);
559
560   /// \brief Emit visible names into a hashed accelerator table section.
561   void emitAccelNames();
562
563   /// \brief Emit objective C classes and categories into a hashed
564   /// accelerator table section.
565   void emitAccelObjC();
566
567   /// \brief Emit namespace dies into a hashed accelerator table.
568   void emitAccelNamespaces();
569
570   /// \brief Emit type dies into a hashed accelerator table.
571   void emitAccelTypes();
572
573   /// \brief Emit visible names into a debug pubnames section.
574   /// \param GnuStyle determines whether or not we want to emit
575   /// additional information into the table ala newer gcc for gdb
576   /// index.
577   void emitDebugPubNames(bool GnuStyle = false);
578
579   /// \brief Emit visible types into a debug pubtypes section.
580   /// \param GnuStyle determines whether or not we want to emit
581   /// additional information into the table ala newer gcc for gdb
582   /// index.
583   void emitDebugPubTypes(bool GnuStyle = false);
584
585   void
586   emitDebugPubSection(bool GnuStyle, const MCSection *PSec, StringRef Name,
587                       const StringMap<const DIE *> &(DwarfUnit::*Accessor)()
588                       const);
589
590   /// \brief Emit visible names into a debug str section.
591   void emitDebugStr();
592
593   /// \brief Emit visible names into a debug loc section.
594   void emitDebugLoc();
595
596   /// \brief Emit visible names into a debug aranges section.
597   void emitDebugARanges();
598
599   /// \brief Emit visible names into a debug ranges section.
600   void emitDebugRanges();
601
602   /// \brief Emit inline info using custom format.
603   void emitDebugInlineInfo();
604
605   /// DWARF 5 Experimental Split Dwarf Emitters
606
607   /// \brief Initialize common features of skeleton units.
608   void initSkeletonUnit(const DwarfUnit *U, DIE *Die, DwarfUnit *NewU);
609
610   /// \brief Construct the split debug info compile unit for the debug info
611   /// section.
612   DwarfCompileUnit *constructSkeletonCU(const DwarfCompileUnit *CU);
613
614   /// \brief Construct the split debug info compile unit for the debug info
615   /// section.
616   DwarfTypeUnit *constructSkeletonTU(DwarfTypeUnit *TU);
617
618   /// \brief Emit the debug info dwo section.
619   void emitDebugInfoDWO();
620
621   /// \brief Emit the debug abbrev dwo section.
622   void emitDebugAbbrevDWO();
623
624   /// \brief Emit the debug line dwo section.
625   void emitDebugLineDWO();
626
627   /// \brief Emit the debug str dwo section.
628   void emitDebugStrDWO();
629
630   /// Flags to let the linker know we have emitted new style pubnames. Only
631   /// emit it here if we don't have a skeleton CU for split dwarf.
632   void addGnuPubAttributes(DwarfUnit *U, DIE *D) const;
633
634   /// \brief Create new DwarfCompileUnit for the given metadata node with tag
635   /// DW_TAG_compile_unit.
636   DwarfCompileUnit *constructDwarfCompileUnit(DICompileUnit DIUnit,
637                                               bool Singular);
638
639   /// \brief Construct subprogram DIE.
640   void constructSubprogramDIE(DwarfCompileUnit *TheCU, const MDNode *N);
641
642   /// \brief Construct imported_module or imported_declaration DIE.
643   void constructImportedEntityDIE(DwarfCompileUnit *TheCU, const MDNode *N);
644
645   /// \brief Construct import_module DIE.
646   void constructImportedEntityDIE(DwarfCompileUnit *TheCU, const MDNode *N,
647                                   DIE *Context);
648
649   /// \brief Construct import_module DIE.
650   void constructImportedEntityDIE(DwarfCompileUnit *TheCU,
651                                   const DIImportedEntity &Module, DIE *Context);
652
653   /// \brief Register a source line with debug info. Returns the unique
654   /// label that was emitted and which provides correspondence to the
655   /// source line list.
656   void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
657                         unsigned Flags);
658
659   /// \brief Indentify instructions that are marking the beginning of or
660   /// ending of a scope.
661   void identifyScopeMarkers();
662
663   /// \brief If Var is an current function argument that add it in
664   /// CurrentFnArguments list.
665   bool addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope);
666
667   /// \brief Populate LexicalScope entries with variables' info.
668   void collectVariableInfo(SmallPtrSet<const MDNode *, 16> &ProcessedVars);
669
670   /// \brief Collect variable information from the side table maintained
671   /// by MMI.
672   void collectVariableInfoFromMMITable(SmallPtrSet<const MDNode *, 16> &P);
673
674   /// \brief Ensure that a label will be emitted before MI.
675   void requestLabelBeforeInsn(const MachineInstr *MI) {
676     LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol *)0));
677   }
678
679   /// \brief Return Label preceding the instruction.
680   MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
681
682   /// \brief Ensure that a label will be emitted after MI.
683   void requestLabelAfterInsn(const MachineInstr *MI) {
684     LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol *)0));
685   }
686
687   /// \brief Return Label immediately following the instruction.
688   MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
689
690   void attachLowHighPC(DwarfCompileUnit *Unit, DIE *D, MCSymbol *Begin,
691                        MCSymbol *End);
692
693 public:
694   //===--------------------------------------------------------------------===//
695   // Main entry points.
696   //
697   DwarfDebug(AsmPrinter *A, Module *M);
698
699   void insertDIE(const MDNode *TypeMD, DIE *Die) {
700     MDTypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
701   }
702   DIE *getDIE(const MDNode *TypeMD) {
703     return MDTypeNodeToDieMap.lookup(TypeMD);
704   }
705
706   /// \brief Look up or create an entry in the OdrMemberMap.
707   const MDNode *&getOrCreateOdrMember(StringRef Key) {
708     return OdrMemberMap.GetOrCreateValue(Key).getValue();
709   }
710
711   /// \brief Emit all Dwarf sections that should come prior to the
712   /// content.
713   void beginModule();
714
715   /// \brief Emit all Dwarf sections that should come after the content.
716   void endModule() override;
717
718   /// \brief Gather pre-function debug information.
719   void beginFunction(const MachineFunction *MF) override;
720
721   /// \brief Gather and emit post-function debug information.
722   void endFunction(const MachineFunction *MF) override;
723
724   /// \brief Process beginning of an instruction.
725   void beginInstruction(const MachineInstr *MI) override;
726
727   /// \brief Process end of an instruction.
728   void endInstruction() override;
729
730   /// \brief Add a DIE to the set of types that we're going to pull into
731   /// type units.
732   void addDwarfTypeUnitType(DwarfCompileUnit &CU, StringRef Identifier,
733                             DIE *Die, DICompositeType CTy);
734
735   /// \brief Add a label so that arange data can be generated for it.
736   void addArangeLabel(SymbolCU SCU) { ArangeLabels.push_back(SCU); }
737
738   /// \brief For symbols that have a size designated (e.g. common symbols),
739   /// this tracks that size.
740   void setSymbolSize(const MCSymbol *Sym, uint64_t Size) override {
741     SymSize[Sym] = Size;
742   }
743
744   /// \brief Recursively Emits a debug information entry.
745   void emitDIE(DIE *Die);
746
747   // Experimental DWARF5 features.
748
749   /// \brief Returns whether or not to emit tables that dwarf consumers can
750   /// use to accelerate lookup.
751   bool useDwarfAccelTables() const { return HasDwarfAccelTables; }
752
753   /// \brief Returns whether or not to change the current debug info for the
754   /// split dwarf proposal support.
755   bool useSplitDwarf() const { return HasSplitDwarf; }
756
757   /// \brief Returns whether or not to use AT_ranges for compilation units.
758   bool useCURanges() const { return HasCURanges; }
759
760   /// Returns the Dwarf Version.
761   unsigned getDwarfVersion() const { return DwarfVersion; }
762
763   /// Returns the section symbol for the .debug_loc section.
764   MCSymbol *getDebugLocSym() const { return DwarfDebugLocSectionSym; }
765
766   /// Returns the entries for the .debug_loc section.
767   const SmallVectorImpl<DebugLocEntry> &getDebugLocEntries() const {
768     return DotDebugLocEntries;
769   }
770
771   /// \brief Emit an entry for the debug loc section. This can be used to
772   /// handle an entry that's going to be emitted into the debug loc section.
773   void emitDebugLocEntry(ByteStreamer &Streamer, const DebugLocEntry &Entry);
774
775   /// Find the MDNode for the given reference.
776   template <typename T> T resolve(DIRef<T> Ref) const {
777     return Ref.resolve(TypeIdentifierMap);
778   }
779
780   /// Find the DwarfCompileUnit for the given CU Die.
781   DwarfCompileUnit *lookupUnit(const DIE *CU) const {
782     return CUDieMap.lookup(CU);
783   }
784   /// isSubprogramContext - Return true if Context is either a subprogram
785   /// or another context nested inside a subprogram.
786   bool isSubprogramContext(const MDNode *Context);
787 };
788 } // End of namespace llvm
789
790 #endif