1 //===-- llvm/CodeGen/DwarfFile.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 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
11 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
12 
13 #include "AddressPool.h"
14 #include "DwarfStringPool.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/Support/Allocator.h"
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace llvm {
26 class AsmPrinter;
27 class DbgVariable;
28 class DwarfCompileUnit;
29 class DwarfUnit;
30 class DIEAbbrev;
31 class MCSymbol;
32 class DIE;
33 class LexicalScope;
34 class StringRef;
35 class DwarfDebug;
36 class MCSection;
37 class MDNode;
38 class DwarfFile {
39   // Target of Dwarf emission, used for sizing of abbreviations.
40   AsmPrinter *Asm;
41 
42   BumpPtrAllocator AbbrevAllocator;
43 
44   // Used to uniquely define abbreviations.
45   FoldingSet<DIEAbbrev> AbbreviationsSet;
46 
47   // A list of all the unique abbreviations in use.
48   std::vector<DIEAbbrev *> Abbreviations;
49 
50   // A pointer to all units in the section.
51   SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
52 
53   DwarfStringPool StrPool;
54 
55   // Collection of dbg variables of a scope.
56   DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8>> ScopeVariables;
57 
58   // Collection of abstract subprogram DIEs.
59   DenseMap<const MDNode *, DIE *> AbstractSPDies;
60 
61   /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
62   /// be shared across CUs, that is why we keep the map here instead
63   /// of in DwarfCompileUnit.
64   DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
65 
66 public:
67   DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
68 
69   ~DwarfFile();
70 
71   const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
72     return CUs;
73   }
74 
75   /// \brief Compute the size and offset of a DIE given an incoming Offset.
76   unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
77 
78   /// \brief Compute the size and offset of all the DIEs.
79   void computeSizeAndOffsets();
80 
81   /// \brief Compute the size and offset of all the DIEs in the given unit.
82   /// \returns The size of the root DIE.
83   unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
84 
85   /// Define a unique number for the abbreviation.
86   ///
87   /// Compute the abbreviation for \c Die, look up its unique number, and
88   /// return a reference to it in the uniquing table.
89   DIEAbbrev &assignAbbrevNumber(DIE &Die);
90 
91   /// \brief Add a unit to the list of CUs.
92   void addUnit(std::unique_ptr<DwarfCompileUnit> U);
93 
94   /// \brief Emit all of the units to the section listed with the given
95   /// abbreviation section.
96   void emitUnits(bool UseOffsets);
97 
98   /// \brief Emit the given unit to its section.
99   void emitUnit(DwarfUnit *U, bool UseOffsets);
100 
101   /// \brief Emit a set of abbreviations to the specific section.
102   void emitAbbrevs(MCSection *);
103 
104   /// \brief Emit all of the strings to the section given.
105   void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr);
106 
107   /// \brief Returns the string pool.
108   DwarfStringPool &getStringPool() { return StrPool; }
109 
110   /// \returns false if the variable was merged with a previous one.
111   bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
112 
113   DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8>> &getScopeVariables() {
114     return ScopeVariables;
115   }
116 
117   DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
118     return AbstractSPDies;
119   }
120 
121   void insertDIE(const MDNode *TypeMD, DIE *Die) {
122     DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
123   }
124   DIE *getDIE(const MDNode *TypeMD) {
125     return DITypeNodeToDieMap.lookup(TypeMD);
126   }
127 };
128 }
129 #endif
130