• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /// DWARF v5: The symbol that designates the base of the range list table.
52   /// The table is shared by all units.
53   MCSymbol *RnglistsTableBaseSym = nullptr;
54 
55   /// The variables of a lexical scope.
56   struct ScopeVars {
57     /// We need to sort Args by ArgNo and check for duplicates. This could also
58     /// be implemented as a list or vector + std::lower_bound().
59     std::map<unsigned, DbgVariable *> Args;
60     SmallVector<DbgVariable *, 8> Locals;
61   };
62   /// Collection of DbgVariables of each lexical scope.
63   DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
64 
65   // Collection of abstract subprogram DIEs.
66   DenseMap<const MDNode *, DIE *> AbstractSPDies;
67   DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
68 
69   /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
70   /// be shared across CUs, that is why we keep the map here instead
71   /// of in DwarfCompileUnit.
72   DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
73 
74 public:
75   DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
76 
getUnits()77   const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
78     return CUs;
79   }
80 
81   /// Compute the size and offset of a DIE given an incoming Offset.
82   unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
83 
84   /// Compute the size and offset of all the DIEs.
85   void computeSizeAndOffsets();
86 
87   /// Compute the size and offset of all the DIEs in the given unit.
88   /// \returns The size of the root DIE.
89   unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
90 
91   /// Add a unit to the list of CUs.
92   void addUnit(std::unique_ptr<DwarfCompileUnit> U);
93 
94   /// Emit all of the units to the section listed with the given
95   /// abbreviation section.
96   void emitUnits(bool UseOffsets);
97 
98   /// Emit the given unit to its section.
99   void emitUnit(DwarfUnit *U, bool UseOffsets);
100 
101   /// Emit a set of abbreviations to the specific section.
102   void emitAbbrevs(MCSection *);
103 
104   /// Emit all of the strings to the section given. If OffsetSection is
105   /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
106   /// is false, emit absolute offsets to the strings. Otherwise, emit
107   /// relocatable references to the strings if they are supported by the target.
108   void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
109                    bool UseRelativeOffsets = false);
110 
111   /// Returns the string pool.
getStringPool()112   DwarfStringPool &getStringPool() { return StrPool; }
113 
getStringOffsetsStartSym()114   MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
115 
setStringOffsetsStartSym(MCSymbol * Sym)116   void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
117 
getRnglistsTableBaseSym()118   MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
119 
setRnglistsTableBaseSym(MCSymbol * Sym)120   void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
121 
122   /// \returns false if the variable was merged with a previous one.
123   bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
124 
getScopeVariables()125   DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
126     return ScopeVariables;
127   }
128 
getAbstractSPDies()129   DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
130     return AbstractSPDies;
131   }
132 
getAbstractVariables()133   DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() {
134     return AbstractVariables;
135   }
136 
insertDIE(const MDNode * TypeMD,DIE * Die)137   void insertDIE(const MDNode *TypeMD, DIE *Die) {
138     DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
139   }
140 
getDIE(const MDNode * TypeMD)141   DIE *getDIE(const MDNode *TypeMD) {
142     return DITypeNodeToDieMap.lookup(TypeMD);
143   }
144 };
145 
146 } // end namespace llvm
147 
148 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
149