blob: a1c1fcc87a2704a437d52b0325f32cdc0e19c98a [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
Devang Patel5eb43192011-04-12 17:40:32 +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 CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15#define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
16
17#include "DIE.h"
Eric Christopher9e429ae2013-10-05 00:27:02 +000018#include "DwarfDebug.h"
Devang Patel5eb43192011-04-12 17:40:32 +000019#include "llvm/ADT/DenseMap.h"
David Blaikief2443192013-10-21 17:28:37 +000020#include "llvm/ADT/Optional.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000021#include "llvm/ADT/StringMap.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000022#include "llvm/IR/DIBuilder.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000023#include "llvm/IR/DebugInfo.h"
David Blaikief3cd7c52013-06-28 20:05:04 +000024#include "llvm/MC/MCExpr.h"
David Blaikie7d734602013-12-06 22:33:05 +000025#include "llvm/MC/MCSection.h"
David Blaikie4a2f95f2014-03-18 01:17:26 +000026#include "llvm/MC/MCDwarf.h"
Devang Patel5eb43192011-04-12 17:40:32 +000027
28namespace llvm {
29
Devang Patelf20c4f72011-04-12 22:53:02 +000030class MachineLocation;
31class MachineOperand;
32class ConstantInt;
David Blaikiea39a76e2013-01-20 01:18:01 +000033class ConstantFP;
Devang Patelf20c4f72011-04-12 22:53:02 +000034class DbgVariable;
David Blaikie15632ae2014-02-12 00:31:30 +000035class DwarfCompileUnit;
Devang Patelf20c4f72011-04-12 22:53:02 +000036
Eric Christopher0f63d062013-12-03 00:45:45 +000037// Data structure to hold a range for range lists.
38class RangeSpan {
39public:
40 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
Eric Christopher270ba4a2013-12-04 19:06:58 +000041 const MCSymbol *getStart() const { return Start; }
42 const MCSymbol *getEnd() const { return End; }
Eric Christopher0f63d062013-12-03 00:45:45 +000043
44private:
45 const MCSymbol *Start, *End;
46};
47
48class RangeSpanList {
49private:
50 // Index for locating within the debug_range section this particular span.
Eric Christopherf8790642013-12-04 22:04:50 +000051 MCSymbol *RangeSym;
Eric Christopher0f63d062013-12-03 00:45:45 +000052 // List of ranges.
53 SmallVector<RangeSpan, 2> Ranges;
54
55public:
Eric Christopherf8790642013-12-04 22:04:50 +000056 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
57 MCSymbol *getSym() const { return RangeSym; }
Eric Christopher0f63d062013-12-03 00:45:45 +000058 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
59 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
60};
61
Devang Patel5eb43192011-04-12 17:40:32 +000062//===----------------------------------------------------------------------===//
David Blaikie319a05f2013-12-02 19:33:10 +000063/// Unit - This dwarf writer support class manages information associated
Devang Patel5eb43192011-04-12 17:40:32 +000064/// with a source file.
Eric Christophera5a79422013-12-09 23:32:48 +000065class DwarfUnit {
David Blaikie319a05f2013-12-02 19:33:10 +000066protected:
Eli Benderskyb42d1462012-12-03 18:45:45 +000067 /// UniqueID - a numeric ID unique among all CUs in the module
Eli Benderskyb42d1462012-12-03 18:45:45 +000068 unsigned UniqueID;
Devang Patel5eb43192011-04-12 17:40:32 +000069
David Blaikie7480ae62014-01-09 03:03:27 +000070 /// Node - MDNode for the compile unit.
David Blaikief645f962014-01-09 03:23:41 +000071 DICompileUnit CUNode;
David Blaikie7480ae62014-01-09 03:03:27 +000072
David Blaikie2a80e442013-12-02 22:09:48 +000073 /// Unit debug information entry.
Ahmed Charles56440fd2014-03-06 05:51:42 +000074 const std::unique_ptr<DIE> UnitDie;
Devang Patel5eb43192011-04-12 17:40:32 +000075
David Blaikie2a80e442013-12-02 22:09:48 +000076 /// Offset of the UnitDie from beginning of debug info section.
Eric Christophera16725b2013-11-21 01:29:16 +000077 unsigned DebugInfoOffset;
78
Devang Patelf20c4f72011-04-12 22:53:02 +000079 /// Asm - Target of Dwarf emission.
80 AsmPrinter *Asm;
81
Eric Christophere698f532012-12-20 21:58:36 +000082 // Holders for some common dwarf information.
Devang Patelf20c4f72011-04-12 22:53:02 +000083 DwarfDebug *DD;
Eric Christopherf8194852013-12-05 18:06:10 +000084 DwarfFile *DU;
Devang Patelf20c4f72011-04-12 22:53:02 +000085
David Blaikie2a80e442013-12-02 22:09:48 +000086 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
Devang Patel5eb43192011-04-12 17:40:32 +000087 DIE *IndexTyDie;
88
Eric Christopher61560112013-05-08 00:11:10 +000089 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
Devang Patel5eb43192011-04-12 17:40:32 +000090 /// variables to debug information entries.
91 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
92
Eric Christopher61560112013-05-08 00:11:10 +000093 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
Devang Patel5eb43192011-04-12 17:40:32 +000094 /// descriptors to debug information entries using a DIEEntry proxy.
95 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
96
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +000097 /// GlobalNames - A map of globally visible named entities for this unit.
Eric Christopher0fe676a2013-11-21 00:48:22 +000098 StringMap<const DIE *> GlobalNames;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +000099
Devang Patel5eb43192011-04-12 17:40:32 +0000100 /// GlobalTypes - A map of globally visible types for this unit.
Eric Christopher0fe676a2013-11-21 00:48:22 +0000101 StringMap<const DIE *> GlobalTypes;
Devang Patel5eb43192011-04-12 17:40:32 +0000102
Eric Christopher4996c702011-11-07 09:24:32 +0000103 /// AccelNames - A map of names for the name accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000104 StringMap<std::vector<const DIE *> > AccelNames;
Eric Christopher4affe8c2013-11-21 01:29:13 +0000105
106 /// AccelObjC - A map of objc spec for the objc accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000107 StringMap<std::vector<const DIE *> > AccelObjC;
Eric Christopher4affe8c2013-11-21 01:29:13 +0000108
109 /// AccelNamespace - A map of names for the namespace accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000110 StringMap<std::vector<const DIE *> > AccelNamespace;
Eric Christopher4affe8c2013-11-21 01:29:13 +0000111
112 /// AccelTypes - A map of names for the type accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000113 StringMap<std::vector<std::pair<const DIE *, unsigned> > > AccelTypes;
Eric Christopher4996c702011-11-07 09:24:32 +0000114
Devang Patelf20c4f72011-04-12 22:53:02 +0000115 /// DIEBlocks - A list of all the DIEBlocks in use.
116 std::vector<DIEBlock *> DIEBlocks;
Eric Christopher4a741042014-02-16 08:46:55 +0000117
118 /// DIELocs - A list of all the DIELocs in use.
119 std::vector<DIELoc *> DIELocs;
Devang Patelf20c4f72011-04-12 22:53:02 +0000120
Devang Patel89543712011-08-15 17:24:54 +0000121 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
122 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
123 /// corresponds to the MDNode mapped with the subprogram DIE.
124 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
125
Eric Christopher46e23432013-12-20 04:16:18 +0000126 // List of ranges for a given compile unit.
127 SmallVector<RangeSpan, 1> CURanges;
128
Eric Christopher0f63d062013-12-03 00:45:45 +0000129 // List of range lists for a given compile unit, separate from the ranges for
130 // the CU itself.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000131 SmallVector<RangeSpanList, 1> CURangeLists;
Eric Christopher0f63d062013-12-03 00:45:45 +0000132
Eric Christopher3264a482013-10-05 00:39:55 +0000133 // DIEValueAllocator - All DIEValues are allocated through this allocator.
134 BumpPtrAllocator DIEValueAllocator;
135
136 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
137 DIEInteger *DIEIntegerOne;
138
David Blaikie03073f72013-12-06 22:14:48 +0000139 /// The section this unit will be emitted in.
140 const MCSection *Section;
141
142 /// A label at the start of the non-dwo section related to this unit.
143 MCSymbol *SectionSym;
144
David Blaikie7d734602013-12-06 22:33:05 +0000145 /// The start of the unit within its section.
146 MCSymbol *LabelBegin;
147
148 /// The end of the unit within its section.
149 MCSymbol *LabelEnd;
150
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000151 /// The label for the start of the range sets for the elements of this unit.
152 MCSymbol *LabelRange;
153
Eric Christopherd8667202013-12-30 17:22:27 +0000154 /// Skeleton unit associated with this unit.
155 DwarfUnit *Skeleton;
156
David Blaikie7480ae62014-01-09 03:03:27 +0000157 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
158 DwarfDebug *DW, DwarfFile *DWU);
David Blaikie319a05f2013-12-02 19:33:10 +0000159
Devang Patel5eb43192011-04-12 17:40:32 +0000160public:
Eric Christophera5a79422013-12-09 23:32:48 +0000161 virtual ~DwarfUnit();
Devang Patel5eb43192011-04-12 17:40:32 +0000162
Eric Christopherd8667202013-12-30 17:22:27 +0000163 /// Set the skeleton unit associated with this unit.
164 void setSkeleton(DwarfUnit *Skel) { Skeleton = Skel; }
165
166 /// Get the skeleton unit associated with this unit.
167 DwarfUnit *getSkeleton() const { return Skeleton; }
168
David Blaikie03073f72013-12-06 22:14:48 +0000169 /// Pass in the SectionSym even though we could recreate it in every compile
170 /// unit (type units will have actually distinct symbols once they're in
171 /// comdat sections).
172 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
173 assert(!this->Section);
174 this->Section = Section;
175 this->SectionSym = SectionSym;
David Blaikie7d734602013-12-06 22:33:05 +0000176 this->LabelBegin =
177 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
178 this->LabelEnd =
179 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000180 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
David Blaikie03073f72013-12-06 22:14:48 +0000181 }
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000182
David Blaikie03073f72013-12-06 22:14:48 +0000183 const MCSection *getSection() const {
184 assert(Section);
185 return Section;
186 }
187
Eric Christopherd8667202013-12-30 17:22:27 +0000188 /// If there's a skeleton then return the section symbol for the skeleton
189 /// unit, otherwise return the section symbol for this unit.
190 MCSymbol *getLocalSectionSym() const {
191 if (Skeleton)
192 return Skeleton->getSectionSym();
Eric Christopherd4368fd2014-01-02 21:03:28 +0000193 return getSectionSym();
Eric Christopherd8667202013-12-30 17:22:27 +0000194 }
195
David Blaikie7d734602013-12-06 22:33:05 +0000196 MCSymbol *getSectionSym() const {
David Blaikie03073f72013-12-06 22:14:48 +0000197 assert(Section);
198 return SectionSym;
199 }
200
Eric Christopherd8667202013-12-30 17:22:27 +0000201 /// If there's a skeleton then return the begin label for the skeleton unit,
202 /// otherwise return the local label for this unit.
203 MCSymbol *getLocalLabelBegin() const {
204 if (Skeleton)
205 return Skeleton->getLabelBegin();
Eric Christopherd4368fd2014-01-02 21:03:28 +0000206 return getLabelBegin();
Eric Christopherd8667202013-12-30 17:22:27 +0000207 }
208
David Blaikie7d734602013-12-06 22:33:05 +0000209 MCSymbol *getLabelBegin() const {
210 assert(Section);
211 return LabelBegin;
212 }
213
214 MCSymbol *getLabelEnd() const {
215 assert(Section);
216 return LabelEnd;
217 }
218
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000219 MCSymbol *getLabelRange() const {
220 assert(Section);
221 return LabelRange;
222 }
223
Devang Patel5eb43192011-04-12 17:40:32 +0000224 // Accessors.
Eric Christopherca68bbf2013-08-26 23:58:22 +0000225 unsigned getUniqueID() const { return UniqueID; }
David Blaikief645f962014-01-09 03:23:41 +0000226 uint16_t getLanguage() const { return CUNode.getLanguage(); }
227 DICompileUnit getCUNode() const { return CUNode; }
David Blaikie2a80e442013-12-02 22:09:48 +0000228 DIE *getUnitDie() const { return UnitDie.get(); }
Eric Christopher0fe676a2013-11-21 00:48:22 +0000229 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
230 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
Devang Patel5eb43192011-04-12 17:40:32 +0000231
David Blaikie2ea848b2013-11-19 22:51:04 +0000232 const StringMap<std::vector<const DIE *> > &getAccelNames() const {
Eric Christopherd9843b32011-11-10 19:25:34 +0000233 return AccelNames;
234 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000235 const StringMap<std::vector<const DIE *> > &getAccelObjC() const {
Eric Christopher4996c702011-11-07 09:24:32 +0000236 return AccelObjC;
237 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000238 const StringMap<std::vector<const DIE *> > &getAccelNamespace() const {
Eric Christopher66b37db2011-11-10 21:47:55 +0000239 return AccelNamespace;
240 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000241 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &
Eric Christopherca68bbf2013-08-26 23:58:22 +0000242 getAccelTypes() const {
Eric Christopher66b37db2011-11-10 21:47:55 +0000243 return AccelTypes;
244 }
Eric Christopher48fef592012-12-20 21:58:40 +0000245
Manman Rence20d462013-10-29 22:57:10 +0000246 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
247 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
248
Devang Patel5eb43192011-04-12 17:40:32 +0000249 /// hasContent - Return true if this compile unit has something to write out.
David Blaikie2a80e442013-12-02 22:09:48 +0000250 bool hasContent() const { return !UnitDie->getChildren().empty(); }
Devang Patel5eb43192011-04-12 17:40:32 +0000251
Eric Christopher46e23432013-12-20 04:16:18 +0000252 /// addRange - Add an address range to the list of ranges for this unit.
Eric Christopher740a8332014-02-27 01:25:00 +0000253 void addRange(RangeSpan Range) {
254 // Only add a range for this unit if we're emitting full debug.
255 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug)
256 CURanges.push_back(Range);
257 }
Eric Christopher46e23432013-12-20 04:16:18 +0000258
259 /// getRanges - Get the list of ranges for this unit.
260 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
261 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
262
Eric Christopher0f63d062013-12-03 00:45:45 +0000263 /// addRangeList - Add an address range list to the list of range lists.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000264 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
Eric Christopher0f63d062013-12-03 00:45:45 +0000265
266 /// getRangeLists - Get the vector of range lists.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000267 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
Eric Christopher0f63d062013-12-03 00:45:45 +0000268 return CURangeLists;
269 }
Eric Christopher270ba4a2013-12-04 19:06:58 +0000270 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
Eric Christopher0f63d062013-12-03 00:45:45 +0000271
Eric Christopher2c8b7902013-10-17 02:06:06 +0000272 /// getParentContextString - Get a string containing the language specific
273 /// context for a global name.
274 std::string getParentContextString(DIScope Context) const;
275
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000276 /// addGlobalName - Add a new global entity to the compile unit.
277 ///
Eric Christopher2c8b7902013-10-17 02:06:06 +0000278 void addGlobalName(StringRef Name, DIE *Die, DIScope Context);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000279
Eric Christopher4996c702011-11-07 09:24:32 +0000280 /// addAccelName - Add a new name to the name accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000281 void addAccelName(StringRef Name, const DIE *Die);
Eric Christopher9cd26af2013-09-20 23:22:52 +0000282
283 /// addAccelObjC - Add a new name to the ObjC accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000284 void addAccelObjC(StringRef Name, const DIE *Die);
Eric Christopher9cd26af2013-09-20 23:22:52 +0000285
286 /// addAccelNamespace - Add a new name to the namespace accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000287 void addAccelNamespace(StringRef Name, const DIE *Die);
Eric Christopher9cd26af2013-09-20 23:22:52 +0000288
289 /// addAccelType - Add a new type to the type accelerator table.
David Blaikie2ea848b2013-11-19 22:51:04 +0000290 void addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die);
Eric Christopher48fef592012-12-20 21:58:40 +0000291
Devang Patel5eb43192011-04-12 17:40:32 +0000292 /// getDIE - Returns the debug information entry map slot for the
Manman Ren4dbdc902013-10-31 17:54:35 +0000293 /// specified debug variable. We delegate the request to DwarfDebug
294 /// when the MDNode can be part of the type system, since DIEs for
295 /// the type system can be shared across CUs and the mappings are
296 /// kept in DwarfDebug.
David Blaikie2ad00162013-11-15 23:09:13 +0000297 DIE *getDIE(DIDescriptor D) const;
Devang Patel5eb43192011-04-12 17:40:32 +0000298
Eric Christopher4a741042014-02-16 08:46:55 +0000299 /// getDIELoc - Returns a fresh newly allocated DIELoc.
300 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
Devang Patelf20c4f72011-04-12 22:53:02 +0000301
Manman Ren4dbdc902013-10-31 17:54:35 +0000302 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
303 /// when the MDNode can be part of the type system, since DIEs for
304 /// the type system can be shared across CUs and the mappings are
305 /// kept in DwarfDebug.
David Blaikie2ad00162013-11-15 23:09:13 +0000306 void insertDIE(DIDescriptor Desc, DIE *D);
Devang Patel5eb43192011-04-12 17:40:32 +0000307
Devang Patel5eb43192011-04-12 17:40:32 +0000308 /// addDie - Adds or interns the DIE to the compile unit.
309 ///
David Blaikie2a80e442013-12-02 22:09:48 +0000310 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
Devang Patel5eb43192011-04-12 17:40:32 +0000311
Eric Christopherbb69a272012-08-24 01:14:27 +0000312 /// addFlag - Add a flag that is true to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000313 void addFlag(DIE *Die, dwarf::Attribute Attribute);
Eric Christopher48fef592012-12-20 21:58:40 +0000314
Devang Patelf20c4f72011-04-12 22:53:02 +0000315 /// addUInt - Add an unsigned integer attribute data and value.
David Blaikief2443192013-10-21 17:28:37 +0000316 void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
317 uint64_t Integer);
318
Eric Christopher4a741042014-02-16 08:46:55 +0000319 void addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer);
Devang Patelf20c4f72011-04-12 22:53:02 +0000320
321 /// addSInt - Add an signed integer attribute data and value.
David Blaikief2443192013-10-21 17:28:37 +0000322 void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
323 int64_t Integer);
324
Eric Christopher4a741042014-02-16 08:46:55 +0000325 void addSInt(DIELoc *Die, Optional<dwarf::Form> Form, int64_t Integer);
Devang Patelf20c4f72011-04-12 22:53:02 +0000326
327 /// addString - Add a string attribute data and value.
David Blaikief2443192013-10-21 17:28:37 +0000328 void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str);
Devang Patelf20c4f72011-04-12 22:53:02 +0000329
Eric Christopher2cbd5762013-01-07 19:32:41 +0000330 /// addLocalString - Add a string attribute data and value.
Eric Christophera07e4f52013-11-19 09:28:34 +0000331 void addLocalString(DIE *Die, dwarf::Attribute Attribute,
332 const StringRef Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000333
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000334 /// addExpr - Add a Dwarf expression attribute data and value.
Eric Christopher4a741042014-02-16 08:46:55 +0000335 void addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr);
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000336
Devang Patelf20c4f72011-04-12 22:53:02 +0000337 /// addLabel - Add a Dwarf label attribute data and value.
David Blaikief2443192013-10-21 17:28:37 +0000338 void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
Devang Patelf20c4f72011-04-12 22:53:02 +0000339 const MCSymbol *Label);
340
Eric Christopher4a741042014-02-16 08:46:55 +0000341 void addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label);
David Blaikief2443192013-10-21 17:28:37 +0000342
Eric Christophera27220f2014-03-05 22:41:20 +0000343 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
344 void addLocationList(DIE *Die, dwarf::Attribute Attribute, unsigned Index);
345
Eric Christopher33ff6972013-11-21 23:46:41 +0000346 /// addSectionLabel - Add a Dwarf section label attribute data and value.
347 ///
Eric Christopherf52eddf2013-11-26 22:23:27 +0000348 void addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
349 const MCSymbol *Label);
Eric Christopher33ff6972013-11-21 23:46:41 +0000350
351 /// addSectionOffset - Add an offset into a section attribute data and value.
352 ///
353 void addSectionOffset(DIE *Die, dwarf::Attribute Attribute, uint64_t Integer);
354
Eric Christophere9ec2452013-01-18 22:11:33 +0000355 /// addOpAddress - Add a dwarf op address data and value using the
356 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
Eric Christopher4a741042014-02-16 08:46:55 +0000357 void addOpAddress(DIELoc *Die, const MCSymbol *Label);
Eric Christophere9ec2452013-01-18 22:11:33 +0000358
Eric Christopher33ff6972013-11-21 23:46:41 +0000359 /// addSectionDelta - Add a label delta attribute data and value.
360 void addSectionDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
361 const MCSymbol *Lo);
Devang Patelf20c4f72011-04-12 22:53:02 +0000362
David Blaikie48b1bdc2014-03-07 01:30:55 +0000363 /// addLabelDelta - Add a label delta attribute data and value.
364 void addLabelDelta(DIE *Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
365 const MCSymbol *Lo);
366
Devang Patelf20c4f72011-04-12 22:53:02 +0000367 /// addDIEEntry - Add a DIE attribute data and value.
David Blaikief2443192013-10-21 17:28:37 +0000368 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry);
Eric Christopher48fef592012-12-20 21:58:40 +0000369
Manman Ren4dbdc902013-10-31 17:54:35 +0000370 /// addDIEEntry - Add a DIE attribute data and value.
Manman Ren4dbdc902013-10-31 17:54:35 +0000371 void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry);
372
David Blaikie47f615e2013-12-17 23:32:35 +0000373 void addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type);
374
Devang Patelf20c4f72011-04-12 22:53:02 +0000375 /// addBlock - Add block data.
Eric Christopher4a741042014-02-16 08:46:55 +0000376 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Block);
377
378 /// addBlock - Add block data.
David Blaikief2443192013-10-21 17:28:37 +0000379 void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block);
Devang Patelf20c4f72011-04-12 22:53:02 +0000380
381 /// addSourceLine - Add location information to specified debug information
382 /// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000383 void addSourceLine(DIE *Die, unsigned Line, StringRef File,
384 StringRef Directory);
Devang Patelf20c4f72011-04-12 22:53:02 +0000385 void addSourceLine(DIE *Die, DIVariable V);
386 void addSourceLine(DIE *Die, DIGlobalVariable G);
387 void addSourceLine(DIE *Die, DISubprogram SP);
388 void addSourceLine(DIE *Die, DIType Ty);
389 void addSourceLine(DIE *Die, DINameSpace NS);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000390 void addSourceLine(DIE *Die, DIObjCProperty Ty);
Devang Patelf20c4f72011-04-12 22:53:02 +0000391
392 /// addAddress - Add an address attribute to a die based on the location
393 /// provided.
Eric Christophera07e4f52013-11-19 09:28:34 +0000394 void addAddress(DIE *Die, dwarf::Attribute Attribute,
395 const MachineLocation &Location, bool Indirect = false);
Devang Patelf20c4f72011-04-12 22:53:02 +0000396
Devang Patelf20c4f72011-04-12 22:53:02 +0000397 /// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000398 void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty);
399 void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned);
400 void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned);
Devang Patelf20c4f72011-04-12 22:53:02 +0000401
402 /// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000403 void addConstantFPValue(DIE *Die, const MachineOperand &MO);
404 void addConstantFPValue(DIE *Die, const ConstantFP *CFP);
Devang Patelf20c4f72011-04-12 22:53:02 +0000405
406 /// addTemplateParams - Add template parameters in buffer.
407 void addTemplateParams(DIE &Buffer, DIArray TParams);
408
Devang Patelba5fbf12011-04-26 19:06:18 +0000409 /// addRegisterOp - Add register operand.
Eric Christopher4a741042014-02-16 08:46:55 +0000410 void addRegisterOp(DIELoc *TheDie, unsigned Reg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000411
412 /// addRegisterOffset - Add register offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000413 void addRegisterOffset(DIELoc *TheDie, unsigned Reg, int64_t Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000414
Devang Patelf20c4f72011-04-12 22:53:02 +0000415 /// addComplexAddress - Start with the address based on the location provided,
416 /// and generate the DWARF information necessary to find the actual variable
417 /// (navigating the extra location information encoded in the type) based on
418 /// the starting location. Add the DWARF information to the die.
Eric Christophera07e4f52013-11-19 09:28:34 +0000419 void addComplexAddress(const DbgVariable &DV, DIE *Die,
420 dwarf::Attribute Attribute,
Devang Patelf20c4f72011-04-12 22:53:02 +0000421 const MachineLocation &Location);
422
423 // FIXME: Should be reformulated in terms of addComplexAddress.
424 /// addBlockByrefAddress - Start with the address based on the location
425 /// provided, and generate the DWARF information necessary to find the
426 /// actual Block variable (navigating the Block struct) based on the
427 /// starting location. Add the DWARF information to the die. Obsolete,
428 /// please use addComplexAddress instead.
Eric Christophera07e4f52013-11-19 09:28:34 +0000429 void addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
430 dwarf::Attribute Attribute,
Devang Patelf20c4f72011-04-12 22:53:02 +0000431 const MachineLocation &Location);
432
Eric Christopher48fef592012-12-20 21:58:40 +0000433 /// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000434 /// DbgVariable based on provided MachineLocation.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000435 void addVariableAddress(const DbgVariable &DV, DIE *Die,
436 MachineLocation Location);
Devang Patelf20c4f72011-04-12 22:53:02 +0000437
Eric Christopher7285c7d2012-03-28 07:34:31 +0000438 /// addType - Add a new type attribute to the specified entity. This takes
439 /// and attribute parameter because DW_AT_friend attributes are also
440 /// type references.
Eric Christophera07e4f52013-11-19 09:28:34 +0000441 void addType(DIE *Entity, DIType Ty,
442 dwarf::Attribute Attribute = dwarf::DW_AT_type);
Devang Patelf20c4f72011-04-12 22:53:02 +0000443
444 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
445 DIE *getOrCreateNameSpace(DINameSpace NS);
446
Devang Patel89543712011-08-15 17:24:54 +0000447 /// getOrCreateSubprogramDIE - Create new DIE using SP.
448 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
449
Devang Patelf20c4f72011-04-12 22:53:02 +0000450 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
451 /// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000452 DIE *getOrCreateTypeDIE(const MDNode *N);
Devang Patelf20c4f72011-04-12 22:53:02 +0000453
Eric Christopherfa205ca2013-10-05 00:05:51 +0000454 /// getOrCreateContextDIE - Get context owner's DIE.
David Blaikie409dd9c2013-11-19 23:08:21 +0000455 DIE *createTypeDIE(DICompositeType Ty);
456
457 /// getOrCreateContextDIE - Get context owner's DIE.
Eric Christopherfa205ca2013-10-05 00:05:51 +0000458 DIE *getOrCreateContextDIE(DIScope Context);
Devang Patelf20c4f72011-04-12 22:53:02 +0000459
Eric Christopherfa205ca2013-10-05 00:05:51 +0000460 /// constructContainingTypeDIEs - Construct DIEs for types that contain
461 /// vtables.
462 void constructContainingTypeDIEs();
Devang Patelf20c4f72011-04-12 22:53:02 +0000463
Eric Christopherfa205ca2013-10-05 00:05:51 +0000464 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopher34a2c872013-11-15 01:43:19 +0000465 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
Eric Christopherfa205ca2013-10-05 00:05:51 +0000466
Adrian Prantl69140d22014-02-25 22:27:14 +0000467 /// constructSubprogramArguments - Construct function argument DIEs.
468 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
469
Manman Renb987e512013-10-29 00:53:03 +0000470 /// Create a DIE with the given Tag, add the DIE to its parent, and
471 /// call insertDIE if MD is not null.
Eric Christophera07e4f52013-11-19 09:28:34 +0000472 DIE *createAndAddDIE(unsigned Tag, DIE &Parent,
473 DIDescriptor N = DIDescriptor());
Manman Renb987e512013-10-29 00:53:03 +0000474
David Blaikie6b288cf2013-10-30 20:42:41 +0000475 /// Compute the size of a header for this unit, not including the initial
476 /// length field.
David Blaikiebc563272013-12-13 21:33:40 +0000477 virtual unsigned getHeaderSize() const {
David Blaikie6b288cf2013-10-30 20:42:41 +0000478 return sizeof(int16_t) + // DWARF version number
479 sizeof(int32_t) + // Offset Into Abbrev. Section
480 sizeof(int8_t); // Pointer Size (in bytes)
481 }
482
483 /// Emit the header for this unit, not including the initial length field.
David Blaikiebc563272013-12-13 21:33:40 +0000484 virtual void emitHeader(const MCSection *ASection,
485 const MCSymbol *ASectionSym) const;
David Blaikie6b288cf2013-10-30 20:42:41 +0000486
David Blaikie15632ae2014-02-12 00:31:30 +0000487 virtual DwarfCompileUnit &getCU() = 0;
David Blaikied696fac2014-02-12 00:32:05 +0000488
Adrian Prantld1e6a4e2014-03-14 23:08:25 +0000489 /// \brief Return whether this compilation unit has the
490 /// one-definition-rule (ODR). In C++ this allows the compiler to
491 /// perform type unique during LTO.
492 bool hasODR() const {
493 switch (getLanguage()) {
494 case dwarf::DW_LANG_C_plus_plus:
495 case dwarf::DW_LANG_C_plus_plus_03:
496 case dwarf::DW_LANG_C_plus_plus_11:
497 // For all we care, the C++ part of the language has the ODR and
498 // ObjC methods are not represented in a way that they could be
499 // confused with C++ member functions.
500 case dwarf::DW_LANG_ObjC_plus_plus:
501 return true;
502 default:
503 return false;
504 }
505 }
506
507 /// \brief Unique C++ member function declarations based on their
508 /// context+mangled name.
509 DISubprogram getOdrUniqueSubprogram(DIScope Context, DISubprogram SP) const;
510
David Blaikie319a05f2013-12-02 19:33:10 +0000511protected:
512 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
513 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
514
David Blaikie4a2f95f2014-03-18 01:17:26 +0000515 /// Look up the source ID with the given directory and source file names. If
516 /// none currently exists, create a new ID and insert it in the line table.
517 virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
518
Eric Christopherfa205ca2013-10-05 00:05:51 +0000519private:
Devang Patelf20c4f72011-04-12 22:53:02 +0000520 /// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christopherf0388b72013-10-04 23:49:29 +0000521 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000522
523 /// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christopherf0388b72013-10-04 23:49:29 +0000524 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000525
526 /// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christopherf0388b72013-10-04 23:49:29 +0000527 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000528
529 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
530 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
531
532 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christopherdf9955d2013-11-11 18:52:31 +0000533 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000534
535 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Eric Christopheraeb105f2013-11-11 18:52:39 +0000536 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000537
Manman Ren230ec862013-10-23 23:00:44 +0000538 /// constructMemberDIE - Construct member DIE from DIDerivedType.
539 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
Manman Ren7cc62702013-10-18 21:14:19 +0000540
Manman Renffc9a712013-10-23 23:05:28 +0000541 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
542 /// DITemplateTypeParameter.
543 void constructTemplateTypeParameterDIE(DIE &Buffer,
544 DITemplateTypeParameter TP);
Manman Ren7cc62702013-10-18 21:14:19 +0000545
Manman Renffc9a712013-10-23 23:05:28 +0000546 /// constructTemplateValueParameterDIE - Construct new DIE for the given
547 /// DITemplateValueParameter.
548 void constructTemplateValueParameterDIE(DIE &Buffer,
549 DITemplateValueParameter TVP);
Devang Patelf20c4f72011-04-12 22:53:02 +0000550
Eric Christopherfa205ca2013-10-05 00:05:51 +0000551 /// getLowerBoundDefault - Return the default lower bound for an array. If the
552 /// DWARF version doesn't handle the language, return -1.
553 int64_t getDefaultLowerBound() const;
554
555 /// getDIEEntry - Returns the debug information entry for the specified
556 /// debug variable.
557 DIEEntry *getDIEEntry(const MDNode *N) const {
558 return MDNodeToDIEEntryMap.lookup(N);
559 }
560
561 /// insertDIEEntry - Insert debug information entry into the map.
562 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
563 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
564 }
565
566 // getIndexTyDie - Get an anonymous type for index type.
567 DIE *getIndexTyDie() { return IndexTyDie; }
568
569 // setIndexTyDie - Set D as anonymous type for index which can be reused
570 // later.
571 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
572
573 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
574 /// information entry.
575 DIEEntry *createDIEEntry(DIE *Entry);
David Blaikie684fc532013-05-06 23:33:07 +0000576
Eric Christopher9e429ae2013-10-05 00:27:02 +0000577 /// resolve - Look in the DwarfDebug map for the MDNode that
Eric Christopher87b9c492013-10-05 00:32:34 +0000578 /// corresponds to the reference.
Eric Christopher9e429ae2013-10-05 00:27:02 +0000579 template <typename T> T resolve(DIRef<T> Ref) const {
580 return DD->resolve(Ref);
581 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000582
583 /// If this is a named finished type then include it in the list of types for
584 /// the accelerator tables.
David Blaikie9d861be2013-11-26 00:15:27 +0000585 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE *TyDIE);
Devang Patel5eb43192011-04-12 17:40:32 +0000586};
587
Eric Christopher4287a492013-12-09 23:57:44 +0000588class DwarfCompileUnit : public DwarfUnit {
David Blaikie60e63862014-02-14 23:58:13 +0000589 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
590 /// the need to search for it in applyStmtList.
591 unsigned stmtListIndex;
592
David Blaikie319a05f2013-12-02 19:33:10 +0000593public:
Eric Christopher4287a492013-12-09 23:57:44 +0000594 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
595 DwarfDebug *DW, DwarfFile *DWU);
David Blaikie319a05f2013-12-02 19:33:10 +0000596
David Blaikie2494fdb2014-02-14 22:41:51 +0000597 void initStmtList(MCSymbol *DwarfLineSectionSym);
598
David Blaikie60e63862014-02-14 23:58:13 +0000599 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
600 void applyStmtList(DIE &D);
601
David Blaikie319a05f2013-12-02 19:33:10 +0000602 /// createGlobalVariableDIE - create global variable DIE.
603 void createGlobalVariableDIE(DIGlobalVariable GV);
604
605 /// addLabelAddress - Add a dwarf label attribute data and value using
606 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
607 void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label);
David Blaikie15632ae2014-02-12 00:31:30 +0000608
Craig Topper73156022014-03-02 09:09:27 +0000609 DwarfCompileUnit &getCU() override { return *this; }
David Blaikie0e8d4012014-03-17 23:53:25 +0000610
David Blaikie4a2f95f2014-03-18 01:17:26 +0000611 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
David Blaikie319a05f2013-12-02 19:33:10 +0000612};
613
Eric Christopher4287a492013-12-09 23:57:44 +0000614class DwarfTypeUnit : public DwarfUnit {
David Blaikie319a05f2013-12-02 19:33:10 +0000615private:
David Blaikiebc563272013-12-13 21:33:40 +0000616 uint64_t TypeSignature;
617 const DIE *Ty;
David Blaikie15632ae2014-02-12 00:31:30 +0000618 DwarfCompileUnit &CU;
David Blaikie8287aff2014-03-18 02:13:23 +0000619 MCDwarfDwoLineTable *SplitLineTable;
David Blaikie319a05f2013-12-02 19:33:10 +0000620
621public:
David Blaikie15632ae2014-02-12 00:31:30 +0000622 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
David Blaikie4a2f95f2014-03-18 01:17:26 +0000623 DwarfDebug *DW, DwarfFile *DWU,
David Blaikie8287aff2014-03-18 02:13:23 +0000624 MCDwarfDwoLineTable *SplitLineTable = nullptr);
David Blaikie319a05f2013-12-02 19:33:10 +0000625
David Blaikiebc563272013-12-13 21:33:40 +0000626 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
David Blaikie47f615e2013-12-17 23:32:35 +0000627 uint64_t getTypeSignature() const { return TypeSignature; }
David Blaikiebc563272013-12-13 21:33:40 +0000628 void setType(const DIE *Ty) { this->Ty = Ty; }
629
David Blaikiebc563272013-12-13 21:33:40 +0000630 /// Emit the header for this unit, not including the initial length field.
631 void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) const
Craig Topper73156022014-03-02 09:09:27 +0000632 override;
633 unsigned getHeaderSize() const override {
David Blaikiebc563272013-12-13 21:33:40 +0000634 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
635 sizeof(uint32_t); // Type DIE Offset
636 }
637 void initSection(const MCSection *Section);
Craig Topper73156022014-03-02 09:09:27 +0000638 DwarfCompileUnit &getCU() override { return CU; }
David Blaikie4a2f95f2014-03-18 01:17:26 +0000639
640protected:
641 unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
David Blaikie319a05f2013-12-02 19:33:10 +0000642};
Devang Patel5eb43192011-04-12 17:40:32 +0000643} // end llvm namespace
644#endif