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 DbgEntity; 28 class DbgVariable; 29 class DbgLabel; 30 class DwarfCompileUnit; 31 class DwarfUnit; 32 class LexicalScope; 33 class MCSection; 34 35 // Data structure to hold a range for range lists. 36 class RangeSpan { 37 public: 38 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {} 39 const MCSymbol *getStart() const { return Start; } 40 const MCSymbol *getEnd() const { return End; } 41 void setEnd(const MCSymbol *E) { End = E; } 42 43 private: 44 const MCSymbol *Start, *End; 45 }; 46 47 class RangeSpanList { 48 private: 49 // Index for locating within the debug_range section this particular span. 50 MCSymbol *RangeSym; 51 const MCSymbol **CUBaseAddress; 52 // List of ranges. 53 SmallVector<RangeSpan, 2> Ranges; 54 55 public: 56 RangeSpanList(MCSymbol *Sym, const MCSymbol *&CUBaseAddress, 57 SmallVector<RangeSpan, 2> Ranges) 58 : RangeSym(Sym), CUBaseAddress(&CUBaseAddress), 59 Ranges(std::move(Ranges)) {} 60 MCSymbol *getSym() const { return RangeSym; } 61 const MCSymbol *&getBaseAddress() const { return *CUBaseAddress; } 62 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; } 63 void addRange(RangeSpan Range) { Ranges.push_back(Range); } 64 }; 65 66 class DwarfFile { 67 // Target of Dwarf emission, used for sizing of abbreviations. 68 AsmPrinter *Asm; 69 70 BumpPtrAllocator AbbrevAllocator; 71 72 // Used to uniquely define abbreviations. 73 DIEAbbrevSet Abbrevs; 74 75 // A pointer to all units in the section. 76 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs; 77 78 DwarfStringPool StrPool; 79 80 // List of range lists for a given compile unit, separate from the ranges for 81 // the CU itself. 82 SmallVector<RangeSpanList, 1> CURangeLists; 83 84 /// DWARF v5: The symbol that designates the start of the contribution to 85 /// the string offsets table. The contribution is shared by all units. 86 MCSymbol *StringOffsetsStartSym = nullptr; 87 88 /// DWARF v5: The symbol that designates the base of the range list table. 89 /// The table is shared by all units. 90 MCSymbol *RnglistsTableBaseSym = nullptr; 91 92 /// DWARF v5: The symbol that designates the base of the locations list table. 93 /// The table is shared by all units. 94 MCSymbol *LoclistsTableBaseSym = nullptr; 95 96 /// The variables of a lexical scope. 97 struct ScopeVars { 98 /// We need to sort Args by ArgNo and check for duplicates. This could also 99 /// be implemented as a list or vector + std::lower_bound(). 100 std::map<unsigned, DbgVariable *> Args; 101 SmallVector<DbgVariable *, 8> Locals; 102 }; 103 /// Collection of DbgVariables of each lexical scope. 104 DenseMap<LexicalScope *, ScopeVars> ScopeVariables; 105 106 /// Collection of DbgLabels of each lexical scope. 107 using LabelList = SmallVector<DbgLabel *, 4>; 108 DenseMap<LexicalScope *, LabelList> ScopeLabels; 109 110 // Collection of abstract subprogram DIEs. 111 DenseMap<const MDNode *, DIE *> AbstractSPDies; 112 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities; 113 114 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can 115 /// be shared across CUs, that is why we keep the map here instead 116 /// of in DwarfCompileUnit. 117 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap; 118 119 public: 120 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA); 121 122 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() { 123 return CUs; 124 } 125 126 std::pair<uint32_t, RangeSpanList *> addRange(const MCSymbol *&CUBaseAddress, 127 SmallVector<RangeSpan, 2> R); 128 129 /// getRangeLists - Get the vector of range lists. 130 const SmallVectorImpl<RangeSpanList> &getRangeLists() const { 131 return CURangeLists; 132 } 133 134 /// Compute the size and offset of a DIE given an incoming Offset. 135 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset); 136 137 /// Compute the size and offset of all the DIEs. 138 void computeSizeAndOffsets(); 139 140 /// Compute the size and offset of all the DIEs in the given unit. 141 /// \returns The size of the root DIE. 142 unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU); 143 144 /// Add a unit to the list of CUs. 145 void addUnit(std::unique_ptr<DwarfCompileUnit> U); 146 147 /// Emit all of the units to the section listed with the given 148 /// abbreviation section. 149 void emitUnits(bool UseOffsets); 150 151 /// Emit the given unit to its section. 152 void emitUnit(DwarfUnit *U, bool UseOffsets); 153 154 /// Emit a set of abbreviations to the specific section. 155 void emitAbbrevs(MCSection *); 156 157 /// Emit all of the strings to the section given. If OffsetSection is 158 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets 159 /// is false, emit absolute offsets to the strings. Otherwise, emit 160 /// relocatable references to the strings if they are supported by the target. 161 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr, 162 bool UseRelativeOffsets = false); 163 164 /// Returns the string pool. 165 DwarfStringPool &getStringPool() { return StrPool; } 166 167 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; } 168 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; } 169 170 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; } 171 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; } 172 173 MCSymbol *getLoclistsTableBaseSym() const { return LoclistsTableBaseSym; } 174 void setLoclistsTableBaseSym(MCSymbol *Sym) { LoclistsTableBaseSym = Sym; } 175 176 /// \returns false if the variable was merged with a previous one. 177 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var); 178 179 void addScopeLabel(LexicalScope *LS, DbgLabel *Label); 180 181 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() { 182 return ScopeVariables; 183 } 184 185 DenseMap<LexicalScope *, LabelList> &getScopeLabels() { 186 return ScopeLabels; 187 } 188 189 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() { 190 return AbstractSPDies; 191 } 192 193 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() { 194 return AbstractEntities; 195 } 196 197 void insertDIE(const MDNode *TypeMD, DIE *Die) { 198 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die)); 199 } 200 201 DIE *getDIE(const MDNode *TypeMD) { 202 return DITypeNodeToDieMap.lookup(TypeMD); 203 } 204 }; 205 206 } // end namespace llvm 207 208 #endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H 209