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 "DwarfStringPool.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/Support/Allocator.h"
20 #include <map>
21 #include <memory>
22 #include <utility>
23 
24 namespace llvm {
25 
26 class AsmPrinter;
27 class DbgVariable;
28 class DwarfCompileUnit;
29 class DwarfUnit;
30 class LexicalScope;
31 class MCSection;
32 
33 class DwarfFile {
34   // Target of Dwarf emission, used for sizing of abbreviations.
35   AsmPrinter *Asm;
36 
37   BumpPtrAllocator AbbrevAllocator;
38 
39   // Used to uniquely define abbreviations.
40   DIEAbbrevSet Abbrevs;
41 
42   // A pointer to all units in the section.
43   SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
44 
45   DwarfStringPool StrPool;
46 
47   /// DWARF v5: The symbol that designates the start of the contribution to
48   /// the string offsets table. The contribution is shared by all units.
49   MCSymbol *StringOffsetsStartSym = nullptr;
50 
51   /// The variables of a lexical scope.
52   struct ScopeVars {
53     /// We need to sort Args by ArgNo and check for duplicates. This could also
54     /// be implemented as a list or vector + std::lower_bound().
55     std::map<unsigned, DbgVariable *> Args;
56     SmallVector<DbgVariable *, 8> Locals;
57   };
58   /// Collection of DbgVariables of each lexical scope.
59   DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
60 
61   // Collection of abstract subprogram DIEs.
62   DenseMap<const MDNode *, DIE *> AbstractSPDies;
63   DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
64 
65   /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
66   /// be shared across CUs, that is why we keep the map here instead
67   /// of in DwarfCompileUnit.
68   DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
69 
70 public:
71   DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
72 
73   const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
74     return CUs;
75   }
76 
77   /// Compute the size and offset of a DIE given an incoming Offset.
78   unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
79 
80   /// Compute the size and offset of all the DIEs.
81   void computeSizeAndOffsets();
82 
83   /// Compute the size and offset of all the DIEs in the given unit.
84   /// \returns The size of the root DIE.
85   unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
86 
87   /// Add a unit to the list of CUs.
88   void addUnit(std::unique_ptr<DwarfCompileUnit> U);
89 
90   /// Emit the string table offsets header.
91   void emitStringOffsetsTableHeader(MCSection *Section);
92 
93   /// Emit all of the units to the section listed with the given
94   /// abbreviation section.
95   void emitUnits(bool UseOffsets);
96 
97   /// Emit the given unit to its section.
98   void emitUnit(DwarfUnit *U, bool UseOffsets);
99 
100   /// Emit a set of abbreviations to the specific section.
101   void emitAbbrevs(MCSection *);
102 
103   /// Emit all of the strings to the section given. If OffsetSection is
104   /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
105   /// is false, emit absolute offsets to the strings. Otherwise, emit
106   /// relocatable references to the strings if they are supported by the target.
107   void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
108                    bool UseRelativeOffsets = false);
109 
110   /// Returns the string pool.
111   DwarfStringPool &getStringPool() { return StrPool; }
112 
113   MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
114 
115   void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
116 
117   /// \returns false if the variable was merged with a previous one.
118   bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
119 
120   DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
121     return ScopeVariables;
122   }
123 
124   DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
125     return AbstractSPDies;
126   }
127 
128   DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() {
129     return AbstractVariables;
130   }
131 
132   void insertDIE(const MDNode *TypeMD, DIE *Die) {
133     DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
134   }
135 
136   DIE *getDIE(const MDNode *TypeMD) {
137     return DITypeNodeToDieMap.lookup(TypeMD);
138   }
139 };
140 
141 } // end namespace llvm
142 
143 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
144