blob: 23ed043afb9daee3a7a8c5846f9420aef85a2c39 [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;
David Blaikief2999472014-10-23 00:16:05 +000027class DbgVariable;
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000028class DwarfCompileUnit;
David Blaikie85f80d72014-04-23 18:54:00 +000029class DwarfUnit;
David Blaikief2999472014-10-23 00:16:05 +000030class LexicalScope;
David Blaikie85f80d72014-04-23 18:54:00 +000031class MCSection;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000032
David Blaikie85f80d72014-04-23 18:54:00 +000033class DwarfFile {
34 // Target of Dwarf emission, used for sizing of abbreviations.
35 AsmPrinter *Asm;
36
Duncan P. N. Exon Smith815a6eb52015-05-27 22:31:41 +000037 BumpPtrAllocator AbbrevAllocator;
38
David Blaikie85f80d72014-04-23 18:54:00 +000039 // Used to uniquely define abbreviations.
Greg Clayton3462a422016-12-08 01:03:48 +000040 DIEAbbrevSet Abbrevs;
David Blaikie85f80d72014-04-23 18:54:00 +000041
42 // A pointer to all units in the section.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000043 SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
David Blaikie85f80d72014-04-23 18:54:00 +000044
David Blaikiedaefdbf2014-04-25 21:34:35 +000045 DwarfStringPool StrPool;
David Blaikie85f80d72014-04-23 18:54:00 +000046
Wolfgang Pieb456b5552018-01-26 18:52:58 +000047 /// 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
Adrian Prantlc929f7a2018-02-06 22:17:45 +000051 /// 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;
David Blaikie80e5b1e2014-10-24 17:57:34 +000060
David Blaikief6dac292014-11-01 17:21:26 +000061 // Collection of abstract subprogram DIEs.
62 DenseMap<const MDNode *, DIE *> AbstractSPDies;
David Blaikie488393f2017-05-12 01:13:45 +000063 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> AbstractVariables;
David Blaikief6dac292014-11-01 17:21:26 +000064
David Blaikie9bfd7a92014-11-04 22:12:18 +000065 /// 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.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000068 DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
David Blaikie9bfd7a92014-11-04 22:12:18 +000069
David Blaikie85f80d72014-04-23 18:54:00 +000070public:
Frederic Riss9412d632015-03-04 02:30:17 +000071 DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
David Blaikie85f80d72014-04-23 18:54:00 +000072
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000073 const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
74 return CUs;
75 }
David Blaikie85f80d72014-04-23 18:54:00 +000076
77 /// \brief Compute the size and offset of a DIE given an incoming Offset.
78 unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
79
80 /// \brief Compute the size and offset of all the DIEs.
81 void computeSizeAndOffsets();
82
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000083 /// \brief 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
David Blaikie85f80d72014-04-23 18:54:00 +000087 /// \brief Add a unit to the list of CUs.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000088 void addUnit(std::unique_ptr<DwarfCompileUnit> U);
David Blaikie85f80d72014-04-23 18:54:00 +000089
Wolfgang Pieb456b5552018-01-26 18:52:58 +000090 /// Emit the string table offsets header.
91 void emitStringOffsetsTableHeader(MCSection *Section);
92
David Blaikie85f80d72014-04-23 18:54:00 +000093 /// \brief Emit all of the units to the section listed with the given
94 /// abbreviation section.
Rafael Espindola063d7252015-03-10 16:58:10 +000095 void emitUnits(bool UseOffsets);
David Blaikie85f80d72014-04-23 18:54:00 +000096
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000097 /// \brief Emit the given unit to its section.
98 void emitUnit(DwarfUnit *U, bool UseOffsets);
99
David Blaikie85f80d72014-04-23 18:54:00 +0000100 /// \brief Emit a set of abbreviations to the specific section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000101 void emitAbbrevs(MCSection *);
David Blaikie85f80d72014-04-23 18:54:00 +0000102
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000103 /// 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);
David Blaikie85f80d72014-04-23 18:54:00 +0000109
David Blaikie85f80d72014-04-23 18:54:00 +0000110 /// \brief Returns the string pool.
David Blaikiedaefdbf2014-04-25 21:34:35 +0000111 DwarfStringPool &getStringPool() { return StrPool; }
David Blaikief2999472014-10-23 00:16:05 +0000112
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000113 MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
114
115 void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
116
Adrian Prantlca7e4702015-02-10 23:18:28 +0000117 /// \returns false if the variable was merged with a previous one.
118 bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
David Blaikie80e5b1e2014-10-24 17:57:34 +0000119
Adrian Prantlc929f7a2018-02-06 22:17:45 +0000120 DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
David Blaikie80e5b1e2014-10-24 17:57:34 +0000121 return ScopeVariables;
122 }
David Blaikief6dac292014-11-01 17:21:26 +0000123
124 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
125 return AbstractSPDies;
126 }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000127
David Blaikie488393f2017-05-12 01:13:45 +0000128 DenseMap<const MDNode *, std::unique_ptr<DbgVariable>> &getAbstractVariables() {
129 return AbstractVariables;
130 }
David Blaikie9bfd7a92014-11-04 22:12:18 +0000131
132 void insertDIE(const MDNode *TypeMD, DIE *Die) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000133 DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
David Blaikie9bfd7a92014-11-04 22:12:18 +0000134 }
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000135
David Blaikie9bfd7a92014-11-04 22:12:18 +0000136 DIE *getDIE(const MDNode *TypeMD) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000137 return DITypeNodeToDieMap.lookup(TypeMD);
David Blaikie9bfd7a92014-11-04 22:12:18 +0000138 }
David Blaikie85f80d72014-04-23 18:54:00 +0000139};
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000140
141} // end namespace llvm
142
143#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H