blob: ab67c43f6f209bbe55a6ed2a188347804a7f2eab [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- C++ -*-===//
David Blaikie37c52312014-10-04 15:49:50 +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//
10// This file contains support for writing dwarf compile unit.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000017#include "DbgEntityHistoryCalculator.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000018#include "DwarfDebug.h"
David Blaikie37c52312014-10-04 15:49:50 +000019#include "DwarfUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000020#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000025#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000026#include "llvm/CodeGen/DIE.h"
27#include "llvm/CodeGen/LexicalScopes.h"
28#include "llvm/IR/DebugInfoMetadata.h"
29#include "llvm/Support/Casting.h"
30#include <algorithm>
31#include <cassert>
32#include <cstdint>
33#include <memory>
David Blaikie37c52312014-10-04 15:49:50 +000034
35namespace llvm {
36
37class AsmPrinter;
David Blaikie37c52312014-10-04 15:49:50 +000038class DwarfFile;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039class GlobalVariable;
40class MCExpr;
David Blaikie37c52312014-10-04 15:49:50 +000041class MCSymbol;
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000042class MDNode;
David Blaikie37c52312014-10-04 15:49:50 +000043
David Blaikiec0bb21f2017-04-22 02:18:00 +000044class DwarfCompileUnit final : public DwarfUnit {
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000045 /// A numeric ID unique among all CUs in the module
46 unsigned UniqueID;
47
David Blaikie37c52312014-10-04 15:49:50 +000048 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
49 /// the need to search for it in applyStmtList.
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000050 DIE::value_iterator StmtListValue;
David Blaikie37c52312014-10-04 15:49:50 +000051
David Blaikie7cbf58a2014-11-01 18:18:07 +000052 /// Skeleton unit associated with this unit.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000053 DwarfCompileUnit *Skeleton = nullptr;
David Blaikie7cbf58a2014-11-01 18:18:07 +000054
David Blaikief4bdc312014-11-02 01:21:40 +000055 /// The start of the unit within its section.
56 MCSymbol *LabelBegin;
57
Amjad Aboud8bbce8a2016-02-01 14:09:41 +000058 /// The start of the unit macro info within macro section.
59 MCSymbol *MacroLabelBegin;
60
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000061 using ImportedEntityList = SmallVector<const MDNode *, 8>;
62 using ImportedEntityMap = DenseMap<const MDNode *, ImportedEntityList>;
Ivan Krasin298639a2015-10-26 21:36:35 +000063
Amjad Aboud72da9392016-04-30 01:44:07 +000064 ImportedEntityMap ImportedEntities;
Ivan Krasin298639a2015-10-26 21:36:35 +000065
David Blaikie192b45c2014-11-02 06:16:39 +000066 /// GlobalNames - A map of globally visible named entities for this unit.
67 StringMap<const DIE *> GlobalNames;
68
69 /// GlobalTypes - A map of globally visible types for this unit.
70 StringMap<const DIE *> GlobalTypes;
71
David Blaikie89a26f02014-11-03 02:41:49 +000072 // List of range lists for a given compile unit, separate from the ranges for
73 // the CU itself.
74 SmallVector<RangeSpanList, 1> CURangeLists;
75
David Blaikiebc532b42014-11-03 16:40:43 +000076 // List of ranges for a given compile unit.
David Blaikie5b02a192014-11-03 23:10:59 +000077 SmallVector<RangeSpan, 2> CURanges;
David Blaikiebc532b42014-11-03 16:40:43 +000078
David Blaikiece343492014-11-03 21:15:30 +000079 // The base address of this unit, if any. Used for relative references in
80 // ranges/locs.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000081 const MCSymbol *BaseAddress = nullptr;
David Blaikiece343492014-11-03 21:15:30 +000082
David Blaikie488393f2017-05-12 01:13:45 +000083 DenseMap<const MDNode *, DIE *> AbstractSPDies;
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000084 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
David Blaikie488393f2017-05-12 01:13:45 +000085
Paul Robinson543c0e12018-05-22 17:27:31 +000086 /// DWO ID for correlating skeleton and split units.
87 uint64_t DWOId = 0;
88
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000089 /// Construct a DIE for the given DbgVariable without initializing the
David Blaikieee7df552014-10-09 17:56:36 +000090 /// DbgVariable's DIE reference.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +000091 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool Abstract);
David Blaikieee7df552014-10-09 17:56:36 +000092
David Blaikiecafd9622014-11-02 08:51:37 +000093 bool isDwoUnit() const override;
94
David Blaikie488393f2017-05-12 01:13:45 +000095 DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
96 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
97 return AbstractSPDies;
98 return DU->getAbstractSPDies();
99 }
100
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000101 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
David Blaikie488393f2017-05-12 01:13:45 +0000102 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000103 return AbstractEntities;
104 return DU->getAbstractEntities();
David Blaikie488393f2017-05-12 01:13:45 +0000105 }
106
David Blaikie37c52312014-10-04 15:49:50 +0000107public:
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000108 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
David Blaikie37c52312014-10-04 15:49:50 +0000109 DwarfDebug *DW, DwarfFile *DWU);
110
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000111 unsigned getUniqueID() const { return UniqueID; }
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000112
David Blaikiea33cd6a2014-11-01 00:50:34 +0000113 DwarfCompileUnit *getSkeleton() const {
David Blaikiea5437b62014-11-01 19:26:05 +0000114 return Skeleton;
David Blaikiea33cd6a2014-11-01 00:50:34 +0000115 }
116
David Blaikieb3cee2f2017-05-25 18:50:28 +0000117 bool includeMinimalInlineScopes() const;
118
Rafael Espindola063d7252015-03-10 16:58:10 +0000119 void initStmtList();
David Blaikie37c52312014-10-04 15:49:50 +0000120
121 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
122 void applyStmtList(DIE &D);
123
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000124 /// A pair of GlobalVariable and DIExpression.
125 struct GlobalExpr {
126 const GlobalVariable *Var;
127 const DIExpression *Expr;
128 };
129
130 /// Get or create global variable DIE.
131 DIE *
132 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
133 ArrayRef<GlobalExpr> GlobalExprs);
David Blaikie37c52312014-10-04 15:49:50 +0000134
135 /// addLabelAddress - Add a dwarf label attribute data and value using
136 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
137 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
138 const MCSymbol *Label);
139
140 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
141 /// DW_FORM_addr only.
142 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
143 const MCSymbol *Label);
144
145 DwarfCompileUnit &getCU() override { return *this; }
146
Paul Robinson612e89d2018-01-12 19:17:50 +0000147 unsigned getOrCreateSourceID(const DIFile *File) override;
David Blaikie37c52312014-10-04 15:49:50 +0000148
Amjad Aboud72da9392016-04-30 01:44:07 +0000149 void addImportedEntity(const DIImportedEntity* IE) {
150 DIScope *Scope = IE->getScope();
Amjad Abouda5ba9912016-04-21 16:58:49 +0000151 assert(Scope && "Invalid Scope encoding!");
152 if (!isa<DILocalScope>(Scope))
153 // No need to add imported enities that are not local declaration.
154 return;
155
156 auto *LocalScope = cast<DILocalScope>(Scope)->getNonLexicalBlockFileScope();
Amjad Aboud72da9392016-04-30 01:44:07 +0000157 ImportedEntities[LocalScope].push_back(IE);
Ivan Krasin298639a2015-10-26 21:36:35 +0000158 }
159
David Blaikie37c52312014-10-04 15:49:50 +0000160 /// addRange - Add an address range to the list of ranges for this unit.
161 void addRange(RangeSpan Range);
David Blaikie14499a72014-10-04 15:58:47 +0000162
163 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
David Blaikiecda2aa82014-10-04 16:24:00 +0000164
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000165 /// Find DIE for the given subprogram and attach appropriate
David Blaikiecda2aa82014-10-04 16:24:00 +0000166 /// DW_AT_low_pc and DW_AT_high_pc attributes. If there are global
167 /// variables in this scope then create and insert DIEs for these
168 /// variables.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000169 DIE &updateSubprogramScopeDIE(const DISubprogram *SP);
David Blaikie9c65b132014-10-08 22:20:02 +0000170
171 void constructScopeDIE(LexicalScope *Scope,
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000172 SmallVectorImpl<DIE *> &FinalChildren);
David Blaikie52400202014-10-09 00:11:39 +0000173
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000174 /// A helper function to construct a RangeSpanList for a given
David Blaikie52400202014-10-09 00:11:39 +0000175 /// lexical scope.
David Blaikie5b02a192014-11-03 23:10:59 +0000176 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
177
178 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
David Blaikiede123752014-10-09 00:21:42 +0000179
180 void attachRangesOrLowHighPC(DIE &D,
181 const SmallVectorImpl<InsnRange> &Ranges);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000182
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000183 /// This scope represents inlined body of a function. Construct
David Blaikie01b48a82014-10-09 16:50:53 +0000184 /// DIE to represent this concrete inlined copy of the function.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000185 DIE *constructInlinedScopeDIE(LexicalScope *Scope);
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000186
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000187 /// Construct new DW_TAG_lexical_block for this scope and
David Blaikie0fbf8bd2014-10-09 17:08:42 +0000188 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000189 DIE *constructLexicalScopeDIE(LexicalScope *Scope);
David Blaikieee7df552014-10-09 17:56:36 +0000190
191 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000192 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
David Blaikie4a1a44e2014-10-09 17:56:39 +0000193
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000194 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
195 DIE *&ObjectPointer);
David Blaikie8b2fdb82014-10-09 18:24:28 +0000196
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000197 /// Construct a DIE for the given DbgLabel.
198 DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
199
David Blaikie8b2fdb82014-10-09 18:24:28 +0000200 /// A helper function to create children of a Scope DIE.
201 DIE *createScopeChildrenDIE(LexicalScope *Scope,
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000202 SmallVectorImpl<DIE *> &Children,
David Blaikie2195e132017-07-27 00:06:53 +0000203 bool *HasNonScopeChildren = nullptr);
David Blaikie1d072342014-10-09 20:21:36 +0000204
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000205 /// Construct a DIE for this subprogram scope.
David Blaikie3e3eb332016-12-15 23:17:52 +0000206 void constructSubprogramScopeDIE(const DISubprogram *Sub, LexicalScope *Scope);
David Blaikie78b65b62014-10-09 20:26:15 +0000207
208 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
David Blaikie58410f22014-10-10 06:39:26 +0000209
David Blaikie49be5b32014-10-31 21:57:02 +0000210 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
David Blaikie4191cbc2014-10-10 06:39:29 +0000211
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000212 /// Construct import_module DIE.
Duncan P. N. Exon Smith827200c2015-06-25 23:52:10 +0000213 DIE *constructImportedEntityDIE(const DIImportedEntity *Module);
Frederic Riss987fe222014-10-24 21:31:09 +0000214
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000215 void finishSubprogramDefinition(const DISubprogram *SP);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000216 void finishEntityDefinition(const DbgEntity *Entity);
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000217
David Blaikie488393f2017-05-12 01:13:45 +0000218 /// Find abstract variable associated with Var.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000219 using InlinedVariable = DbgValueHistoryMap::InlinedVariable;
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000220 DbgEntity *getExistingAbstractEntity(const DINode *Node);
221 void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
David Blaikie1d96cc22014-10-31 22:30:30 +0000222
David Blaikie7cbf58a2014-11-01 18:18:07 +0000223 /// Set the skeleton unit associated with this unit.
David Blaikiea5437b62014-11-01 19:26:05 +0000224 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
David Blaikiea34568b2014-11-01 20:06:28 +0000225
Paul Robinson543c0e12018-05-22 17:27:31 +0000226 unsigned getHeaderSize() const override {
227 // DWARF v5 added the DWO ID to the header for split/skeleton units.
228 unsigned DWOIdSize =
229 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
230 : 0;
231 return DwarfUnit::getHeaderSize() + DWOIdSize;
232 }
David Blaikie983bfea2014-11-01 23:07:14 +0000233 unsigned getLength() {
234 return sizeof(uint32_t) + // Length field
Greg Clayton35630c32016-12-01 18:56:29 +0000235 getHeaderSize() + getUnitDie().getSize();
David Blaikie983bfea2014-11-01 23:07:14 +0000236 }
David Blaikieae57e662014-11-01 23:59:23 +0000237
Rafael Espindola063d7252015-03-10 16:58:10 +0000238 void emitHeader(bool UseOffsets) override;
David Blaikief4bdc312014-11-02 01:21:40 +0000239
240 MCSymbol *getLabelBegin() const {
David Blaikiee40caae2016-12-01 21:59:09 +0000241 assert(getSection());
David Blaikief4bdc312014-11-02 01:21:40 +0000242 return LabelBegin;
243 }
David Blaikie192b45c2014-11-02 06:16:39 +0000244
Amjad Aboud8bbce8a2016-02-01 14:09:41 +0000245 MCSymbol *getMacroLabelBegin() const {
246 return MacroLabelBegin;
247 }
248
David Blaikie192b45c2014-11-02 06:16:39 +0000249 /// Add a new global name to the compile unit.
David Blaikiea0e3c752017-02-03 00:44:18 +0000250 void addGlobalName(StringRef Name, const DIE &Die,
251 const DIScope *Context) override;
252
253 /// Add a new global name present in a type unit to this compile unit.
254 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
David Blaikie192b45c2014-11-02 06:16:39 +0000255
256 /// Add a new global type to the compile unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000257 void addGlobalType(const DIType *Ty, const DIE &Die,
258 const DIScope *Context) override;
David Blaikie192b45c2014-11-02 06:16:39 +0000259
David Blaikiea0e3c752017-02-03 00:44:18 +0000260 /// Add a new global type present in a type unit to this compile unit.
261 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
262
David Blaikie192b45c2014-11-02 06:16:39 +0000263 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
264 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
David Blaikie7d48be22014-11-02 06:37:23 +0000265
266 /// Add DW_AT_location attribute for a DbgVariable based on provided
267 /// MachineLocation.
268 void addVariableAddress(const DbgVariable &DV, DIE &Die,
269 MachineLocation Location);
David Blaikief7435ee2014-11-02 06:46:40 +0000270 /// Add an address attribute to a die based on the location provided.
271 void addAddress(DIE &Die, dwarf::Attribute Attribute,
Adrian Prantl5883af32015-01-19 17:57:29 +0000272 const MachineLocation &Location);
David Blaikie77895fb2014-11-02 06:58:44 +0000273
274 /// Start with the address based on the location provided, and generate the
275 /// DWARF information necessary to find the actual variable (navigating the
276 /// extra location information encoded in the type) based on the starting
277 /// location. Add the DWARF information to the die.
278 void addComplexAddress(const DbgVariable &DV, DIE &Die,
279 dwarf::Attribute Attribute,
280 const MachineLocation &Location);
David Blaikie4bc08812014-11-02 07:03:19 +0000281
282 /// Add a Dwarf loclistptr attribute data and value.
283 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
David Blaikie02a63332014-11-02 07:06:51 +0000284 void applyVariableAttributes(const DbgVariable &Var, DIE &VariableDie);
David Blaikie97802082014-11-02 07:11:55 +0000285
286 /// Add a Dwarf expression attribute data and value.
287 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
David Blaikie3363a572014-11-02 08:09:09 +0000288
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000289 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000290 DIE &SPDie);
David Blaikie89a26f02014-11-03 02:41:49 +0000291
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000292 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
293
David Blaikie89a26f02014-11-03 02:41:49 +0000294 /// getRangeLists - Get the vector of range lists.
295 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
David Blaikie542616d2014-11-03 21:52:56 +0000296 return (Skeleton ? Skeleton : this)->CURangeLists;
David Blaikie89a26f02014-11-03 02:41:49 +0000297 }
David Blaikiebc532b42014-11-03 16:40:43 +0000298
299 /// getRanges - Get the list of ranges for this unit.
300 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
David Blaikie5b02a192014-11-03 23:10:59 +0000301 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
David Blaikiece343492014-11-03 21:15:30 +0000302
303 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
304 const MCSymbol *getBaseAddress() const { return BaseAddress; }
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000305
Paul Robinson543c0e12018-05-22 17:27:31 +0000306 uint64_t getDWOId() const { return DWOId; }
307 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
308
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000309 bool hasDwarfPubSections() const;
David Blaikie37c52312014-10-04 15:49:50 +0000310};
311
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000312} // end namespace llvm
David Blaikie37c52312014-10-04 15:49:50 +0000313
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000314#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H