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