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 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 the string table offsets header. 95 void emitStringOffsetsTableHeader(MCSection *Section); 96 97 /// Emit all of the units to the section listed with the given 98 /// abbreviation section. 99 void emitUnits(bool UseOffsets); 100 101 /// Emit the given unit to its section. 102 void emitUnit(DwarfUnit *U, bool UseOffsets); 103 104 /// Emit a set of abbreviations to the specific section. 105 void emitAbbrevs(MCSection *); 106 107 /// Emit all of the strings to the section given. If OffsetSection is 108 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets 109 /// is false, emit absolute offsets to the strings. Otherwise, emit 110 /// relocatable references to the strings if they are supported by the target. 111 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr, 112 bool UseRelativeOffsets = false); 113 114 /// Returns the string pool. 115 DwarfStringPool &getStringPool() { return StrPool; } 116 117 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; } 118 119 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; } 120 121 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; } 122 123 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; } 124 125 /// \returns false if the variable was merged with a previous one. 126 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var); 127 128 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() { 129 return ScopeVariables; 130 } 131 132 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() { 133 return AbstractSPDies; 134 } 135 136 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() { 137 return AbstractVariables; 138 } 139 140 void insertDIE(const MDNode *TypeMD, DIE *Die) { 141 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die)); 142 } 143 144 DIE *getDIE(const MDNode *TypeMD) { 145 return DITypeNodeToDieMap.lookup(TypeMD); 146 } 147 }; 148 149 } // end namespace llvm 150 151 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 152