blob: c315f44a8d837bfe9d1239ad6516d252dc1821ce [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===//
David Blaikie85f80d72014-04-23 18:54:00 +00002//
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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
11#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
David Blaikie85f80d72014-04-23 18:54:00 +000012
Chandler Carruthd9903882015-01-14 11:23:27 +000013#include "DwarfStringPool.h"
David Blaikie85f80d72014-04-23 18:54:00 +000014#include "llvm/ADT/DenseMap.h"
David Blaikie85f80d72014-04-23 18:54:00 +000015#include "llvm/ADT/SmallVector.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000016#include "llvm/ADT/StringRef.h"
Greg Clayton3462a422016-12-08 01:03:48 +000017#include "llvm/CodeGen/DIE.h"
David Blaikie526f30b2017-11-03 20:24:19 +000018#include "llvm/IR/Metadata.h"
David Blaikie85f80d72014-04-23 18:54:00 +000019#include "llvm/Support/Allocator.h"
Adrian Prantlc929f7a2018-02-06 22:17:45 +000020#include <map>
David Blaikie85f80d72014-04-23 18:54:00 +000021#include <memory>
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000022#include <utility>
David Blaikie85f80d72014-04-23 18:54:00 +000023
24namespace llvm {
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000025
David Blaikie85f80d72014-04-23 18:54:00 +000026class AsmPrinter;
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +000027class DbgEntity;
David Blaikief2999472014-10-23 00:16:05 +000028class DbgVariable;
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +000029class DbgLabel;
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000030class DwarfCompileUnit;
David Blaikie85f80d72014-04-23 18:54:00 +000031class DwarfUnit;
David Blaikief2999472014-10-23 00:16:05 +000032class LexicalScope;
David Blaikie85f80d72014-04-23 18:54:00 +000033class MCSection;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000034
David Blaikie85f80d72014-04-23 18:54:00 +000035class DwarfFile {
36 // Target of Dwarf emission, used for sizing of abbreviations.
37 AsmPrinter *Asm;
38
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000039 BumpPtrAllocator AbbrevAllocator;
40
David Blaikie85f80d72014-04-23 18:54:00 +000041 // Used to uniquely define abbreviations.
Greg Clayton3462a422016-12-08 01:03:48 +000042 DIEAbbrevSet Abbrevs;
David Blaikie85f80d72014-04-23 18:54:00 +000043
44 // A pointer to all units in the section.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000045 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
David Blaikie85f80d72014-04-23 18:54:00 +000046
David Blaikiedaefdbf2014-04-25 21:34:35 +000047 DwarfStringPool StrPool;
David Blaikie85f80d72014-04-23 18:54:00 +000048
Wolfgang Pieb456b5552018-01-26 18:52:58 +000049 /// DWARF v5: The symbol that designates the start of the contribution to
50 /// the string offsets table. The contribution is shared by all units.
51 MCSymbol *StringOffsetsStartSym = nullptr;
52
Wolfgang Piebfcf38102018-07-12 18:18:21 +000053 /// DWARF v5: The symbol that designates the base of the range list table.
54 /// The table is shared by all units.
55 MCSymbol *RnglistsTableBaseSym = nullptr;
56
Adrian Prantlc929f7a2018-02-06 22:17:45 +000057 /// The variables of a lexical scope.
58 struct ScopeVars {
59 /// We need to sort Args by ArgNo and check for duplicates. This could also
60 /// be implemented as a list or vector + std::lower_bound().
61 std::map<unsigned, DbgVariable *> Args;
62 SmallVector<DbgVariable *, 8> Locals;
63 };
64 /// Collection of DbgVariables of each lexical scope.
65 DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
David Blaikie80e5b1e2014-10-24 17:57:34 +000066
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +000067 /// Collection of DbgLabels of each lexical scope.
68 using LabelList = SmallVector<DbgLabel *, 4>;
69 DenseMap<LexicalScope *, LabelList> ScopeLabels;
70
David Blaikief6dac292014-11-01 17:21:26 +000071 // Collection of abstract subprogram DIEs.
72 DenseMap<const MDNode *, DIE *> AbstractSPDies;
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +000073 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
David Blaikief6dac292014-11-01 17:21:26 +000074
David Blaikie9bfd7a92014-11-04 22:12:18 +000075 /// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
76 /// be shared across CUs, that is why we keep the map here instead
77 /// of in DwarfCompileUnit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000078 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
David Blaikie9bfd7a92014-11-04 22:12:18 +000079
David Blaikie85f80d72014-04-23 18:54:00 +000080public:
Frederic Riss9412d632015-03-04 02:30:17 +000081 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
David Blaikie85f80d72014-04-23 18:54:00 +000082
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000083 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
84 return CUs;
85 }
David Blaikie85f80d72014-04-23 18:54:00 +000086
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000087 /// Compute the size and offset of a DIE given an incoming Offset.
David Blaikie85f80d72014-04-23 18:54:00 +000088 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
89
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000090 /// Compute the size and offset of all the DIEs.
David Blaikie85f80d72014-04-23 18:54:00 +000091 void computeSizeAndOffsets();
92
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000093 /// Compute the size and offset of all the DIEs in the given unit.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000094 /// \returns The size of the root DIE.
95 unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
96
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000097 /// Add a unit to the list of CUs.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000098 void addUnit(std::unique_ptr<DwarfCompileUnit> U);
David Blaikie85f80d72014-04-23 18:54:00 +000099
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000100 /// Emit all of the units to the section listed with the given
David Blaikie85f80d72014-04-23 18:54:00 +0000101 /// abbreviation section.
Rafael Espindola063d7252015-03-10 16:58:10 +0000102 void emitUnits(bool UseOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +0000103
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000104 /// Emit the given unit to its section.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000105 void emitUnit(DwarfUnit *U, bool UseOffsets);
106
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000107 /// Emit a set of abbreviations to the specific section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000108 void emitAbbrevs(MCSection *);
David Blaikie85f80d72014-04-23 18:54:00 +0000109
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000110 /// Emit all of the strings to the section given. If OffsetSection is
111 /// non-null, emit a table of string offsets to it. If UseRelativeOffsets
112 /// is false, emit absolute offsets to the strings. Otherwise, emit
113 /// relocatable references to the strings if they are supported by the target.
114 void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
115 bool UseRelativeOffsets = false);
David Blaikie85f80d72014-04-23 18:54:00 +0000116
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000117 /// Returns the string pool.
David Blaikiedaefdbf2014-04-25 21:34:35 +0000118 DwarfStringPool &getStringPool() { return StrPool; }
David Blaikief2999472014-10-23 00:16:05 +0000119
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000120 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
121
122 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
123
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000124 MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
125
126 void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
127
Adrian Prantlca7e4702015-02-10 23:18:28 +0000128 /// \returns false if the variable was merged with a previous one.
129 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
David Blaikie80e5b1e2014-10-24 17:57:34 +0000130
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +0000131 void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
132
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000133 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000134 return ScopeVariables;
135 }
David Blaikief6dac292014-11-01 17:21:26 +0000136
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +0000137 DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
138 return ScopeLabels;
139 }
140
David Blaikief6dac292014-11-01 17:21:26 +0000141 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
142 return AbstractSPDies;
143 }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000144
Hsiangkai Wangcbc58ad2018-07-31 14:48:32 +0000145 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
146 return AbstractEntities;
David Blaikie488393f2017-05-12 01:13:45 +0000147 }
David Blaikie9bfd7a92014-11-04 22:12:18 +0000148
149 void insertDIE(const MDNode *TypeMD, DIE *Die) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000150 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
David Blaikie9bfd7a92014-11-04 22:12:18 +0000151 }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000152
David Blaikie9bfd7a92014-11-04 22:12:18 +0000153 DIE *getDIE(const MDNode *TypeMD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000154 return DITypeNodeToDieMap.lookup(TypeMD);
David Blaikie9bfd7a92014-11-04 22:12:18 +0000155 }
David Blaikie85f80d72014-04-23 18:54:00 +0000156};
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000157
158} // end namespace llvm
159
160#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H