blob: 17be320f209b3f4c8810cd4a6c4145b8b840a709 [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"
Adrian Prantl702bf5a2014-03-18 02:34:52 +000022#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000023#include "llvm/IR/DIBuilder.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000024#include "llvm/IR/DebugInfo.h"
David Blaikief3cd7c52013-06-28 20:05:04 +000025#include "llvm/MC/MCExpr.h"
David Blaikie7d734602013-12-06 22:33:05 +000026#include "llvm/MC/MCSection.h"
David Blaikie4a2f95f2014-03-18 01:17:26 +000027#include "llvm/MC/MCDwarf.h"
Devang Patel5eb43192011-04-12 17:40:32 +000028
29namespace llvm {
30
Devang Patelf20c4f72011-04-12 22:53:02 +000031class MachineLocation;
32class MachineOperand;
33class ConstantInt;
David Blaikiea39a76e2013-01-20 01:18:01 +000034class ConstantFP;
Devang Patelf20c4f72011-04-12 22:53:02 +000035class DbgVariable;
David Blaikie15632ae2014-02-12 00:31:30 +000036class DwarfCompileUnit;
Devang Patelf20c4f72011-04-12 22:53:02 +000037
Eric Christopher0f63d062013-12-03 00:45:45 +000038// Data structure to hold a range for range lists.
39class RangeSpan {
40public:
41 RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
Eric Christopher270ba4a2013-12-04 19:06:58 +000042 const MCSymbol *getStart() const { return Start; }
43 const MCSymbol *getEnd() const { return End; }
Eric Christopher384f3fe2014-03-20 19:16:16 +000044 void setEnd(const MCSymbol *E) { End = E; }
Eric Christopher0f63d062013-12-03 00:45:45 +000045
46private:
47 const MCSymbol *Start, *End;
48};
49
50class RangeSpanList {
51private:
52 // Index for locating within the debug_range section this particular span.
Eric Christopherf8790642013-12-04 22:04:50 +000053 MCSymbol *RangeSym;
Eric Christopher0f63d062013-12-03 00:45:45 +000054 // List of ranges.
55 SmallVector<RangeSpan, 2> Ranges;
56
57public:
Eric Christopherf8790642013-12-04 22:04:50 +000058 RangeSpanList(MCSymbol *Sym) : RangeSym(Sym) {}
59 MCSymbol *getSym() const { return RangeSym; }
Eric Christopher0f63d062013-12-03 00:45:45 +000060 const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
61 void addRange(RangeSpan Range) { Ranges.push_back(Range); }
62};
63
Devang Patel5eb43192011-04-12 17:40:32 +000064//===----------------------------------------------------------------------===//
David Blaikie319a05f2013-12-02 19:33:10 +000065/// Unit - This dwarf writer support class manages information associated
Devang Patel5eb43192011-04-12 17:40:32 +000066/// with a source file.
Eric Christophera5a79422013-12-09 23:32:48 +000067class DwarfUnit {
David Blaikie319a05f2013-12-02 19:33:10 +000068protected:
Eli Benderskyb42d1462012-12-03 18:45:45 +000069 /// UniqueID - a numeric ID unique among all CUs in the module
Eli Benderskyb42d1462012-12-03 18:45:45 +000070 unsigned UniqueID;
Devang Patel5eb43192011-04-12 17:40:32 +000071
David Blaikie7480ae62014-01-09 03:03:27 +000072 /// Node - MDNode for the compile unit.
David Blaikief645f962014-01-09 03:23:41 +000073 DICompileUnit CUNode;
David Blaikie7480ae62014-01-09 03:03:27 +000074
David Blaikie2a80e442013-12-02 22:09:48 +000075 /// Unit debug information entry.
Ahmed Charles56440fd2014-03-06 05:51:42 +000076 const std::unique_ptr<DIE> UnitDie;
Devang Patel5eb43192011-04-12 17:40:32 +000077
David Blaikie2a80e442013-12-02 22:09:48 +000078 /// Offset of the UnitDie from beginning of debug info section.
Eric Christophera16725b2013-11-21 01:29:16 +000079 unsigned DebugInfoOffset;
80
Devang Patelf20c4f72011-04-12 22:53:02 +000081 /// Asm - Target of Dwarf emission.
82 AsmPrinter *Asm;
83
Eric Christophere698f532012-12-20 21:58:36 +000084 // Holders for some common dwarf information.
Devang Patelf20c4f72011-04-12 22:53:02 +000085 DwarfDebug *DD;
Eric Christopherf8194852013-12-05 18:06:10 +000086 DwarfFile *DU;
Devang Patelf20c4f72011-04-12 22:53:02 +000087
David Blaikie2a80e442013-12-02 22:09:48 +000088 /// IndexTyDie - An anonymous type for index type. Owned by UnitDie.
Devang Patel5eb43192011-04-12 17:40:32 +000089 DIE *IndexTyDie;
90
Eric Christopher61560112013-05-08 00:11:10 +000091 /// MDNodeToDieMap - Tracks the mapping of unit level debug information
Devang Patel5eb43192011-04-12 17:40:32 +000092 /// variables to debug information entries.
93 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
94
Eric Christopher61560112013-05-08 00:11:10 +000095 /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information
Devang Patel5eb43192011-04-12 17:40:32 +000096 /// descriptors to debug information entries using a DIEEntry proxy.
97 DenseMap<const MDNode *, DIEEntry *> MDNodeToDIEEntryMap;
98
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +000099 /// GlobalNames - A map of globally visible named entities for this unit.
Eric Christopher0fe676a2013-11-21 00:48:22 +0000100 StringMap<const DIE *> GlobalNames;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000101
Devang Patel5eb43192011-04-12 17:40:32 +0000102 /// GlobalTypes - A map of globally visible types for this unit.
Eric Christopher0fe676a2013-11-21 00:48:22 +0000103 StringMap<const DIE *> GlobalTypes;
Devang Patel5eb43192011-04-12 17:40:32 +0000104
Devang Patelf20c4f72011-04-12 22:53:02 +0000105 /// DIEBlocks - A list of all the DIEBlocks in use.
106 std::vector<DIEBlock *> DIEBlocks;
Eric Christopher4a741042014-02-16 08:46:55 +0000107
108 /// DIELocs - A list of all the DIELocs in use.
109 std::vector<DIELoc *> DIELocs;
Devang Patelf20c4f72011-04-12 22:53:02 +0000110
Devang Patel89543712011-08-15 17:24:54 +0000111 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
112 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
113 /// corresponds to the MDNode mapped with the subprogram DIE.
114 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
115
Eric Christopher46e23432013-12-20 04:16:18 +0000116 // List of ranges for a given compile unit.
117 SmallVector<RangeSpan, 1> CURanges;
118
Eric Christopher0f63d062013-12-03 00:45:45 +0000119 // List of range lists for a given compile unit, separate from the ranges for
120 // the CU itself.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000121 SmallVector<RangeSpanList, 1> CURangeLists;
Eric Christopher0f63d062013-12-03 00:45:45 +0000122
Eric Christopher3264a482013-10-05 00:39:55 +0000123 // DIEValueAllocator - All DIEValues are allocated through this allocator.
124 BumpPtrAllocator DIEValueAllocator;
125
126 // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently.
127 DIEInteger *DIEIntegerOne;
128
David Blaikie03073f72013-12-06 22:14:48 +0000129 /// The section this unit will be emitted in.
130 const MCSection *Section;
131
132 /// A label at the start of the non-dwo section related to this unit.
133 MCSymbol *SectionSym;
134
David Blaikie7d734602013-12-06 22:33:05 +0000135 /// The start of the unit within its section.
136 MCSymbol *LabelBegin;
137
138 /// The end of the unit within its section.
139 MCSymbol *LabelEnd;
140
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000141 /// The label for the start of the range sets for the elements of this unit.
142 MCSymbol *LabelRange;
143
Eric Christopherd8667202013-12-30 17:22:27 +0000144 /// Skeleton unit associated with this unit.
145 DwarfUnit *Skeleton;
146
David Blaikie7480ae62014-01-09 03:03:27 +0000147 DwarfUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A,
148 DwarfDebug *DW, DwarfFile *DWU);
David Blaikie319a05f2013-12-02 19:33:10 +0000149
Devang Patel5eb43192011-04-12 17:40:32 +0000150public:
Eric Christophera5a79422013-12-09 23:32:48 +0000151 virtual ~DwarfUnit();
Devang Patel5eb43192011-04-12 17:40:32 +0000152
Eric Christopherd8667202013-12-30 17:22:27 +0000153 /// Set the skeleton unit associated with this unit.
David Blaikief9b6a552014-04-22 22:39:41 +0000154 void setSkeleton(DwarfUnit &Skel) { Skeleton = &Skel; }
Eric Christopherd8667202013-12-30 17:22:27 +0000155
156 /// Get the skeleton unit associated with this unit.
157 DwarfUnit *getSkeleton() const { return Skeleton; }
158
David Blaikie03073f72013-12-06 22:14:48 +0000159 /// Pass in the SectionSym even though we could recreate it in every compile
160 /// unit (type units will have actually distinct symbols once they're in
161 /// comdat sections).
162 void initSection(const MCSection *Section, MCSymbol *SectionSym) {
163 assert(!this->Section);
164 this->Section = Section;
165 this->SectionSym = SectionSym;
David Blaikie7d734602013-12-06 22:33:05 +0000166 this->LabelBegin =
167 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
168 this->LabelEnd =
169 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000170 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
David Blaikie03073f72013-12-06 22:14:48 +0000171 }
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000172
David Blaikie03073f72013-12-06 22:14:48 +0000173 const MCSection *getSection() const {
174 assert(Section);
175 return Section;
176 }
177
Eric Christopherd8667202013-12-30 17:22:27 +0000178 /// If there's a skeleton then return the section symbol for the skeleton
179 /// unit, otherwise return the section symbol for this unit.
180 MCSymbol *getLocalSectionSym() const {
181 if (Skeleton)
182 return Skeleton->getSectionSym();
Eric Christopherd4368fd2014-01-02 21:03:28 +0000183 return getSectionSym();
Eric Christopherd8667202013-12-30 17:22:27 +0000184 }
185
David Blaikie7d734602013-12-06 22:33:05 +0000186 MCSymbol *getSectionSym() const {
David Blaikie03073f72013-12-06 22:14:48 +0000187 assert(Section);
188 return SectionSym;
189 }
190
Eric Christopherd8667202013-12-30 17:22:27 +0000191 /// If there's a skeleton then return the begin label for the skeleton unit,
192 /// otherwise return the local label for this unit.
193 MCSymbol *getLocalLabelBegin() const {
194 if (Skeleton)
195 return Skeleton->getLabelBegin();
Eric Christopherd4368fd2014-01-02 21:03:28 +0000196 return getLabelBegin();
Eric Christopherd8667202013-12-30 17:22:27 +0000197 }
198
David Blaikie7d734602013-12-06 22:33:05 +0000199 MCSymbol *getLabelBegin() const {
200 assert(Section);
201 return LabelBegin;
202 }
203
204 MCSymbol *getLabelEnd() const {
205 assert(Section);
206 return LabelEnd;
207 }
208
David Blaikie1ab7c2d2013-12-09 17:51:30 +0000209 MCSymbol *getLabelRange() const {
210 assert(Section);
211 return LabelRange;
212 }
213
Devang Patel5eb43192011-04-12 17:40:32 +0000214 // Accessors.
Eric Christopherca68bbf2013-08-26 23:58:22 +0000215 unsigned getUniqueID() const { return UniqueID; }
David Blaikief645f962014-01-09 03:23:41 +0000216 uint16_t getLanguage() const { return CUNode.getLanguage(); }
217 DICompileUnit getCUNode() const { return CUNode; }
David Blaikieadcde362014-04-25 18:35:57 +0000218 DIE &getUnitDie() const { return *UnitDie; }
Eric Christopher0fe676a2013-11-21 00:48:22 +0000219 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
220 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
Devang Patel5eb43192011-04-12 17:40:32 +0000221
Manman Rence20d462013-10-29 22:57:10 +0000222 unsigned getDebugInfoOffset() const { return DebugInfoOffset; }
223 void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; }
224
Devang Patel5eb43192011-04-12 17:40:32 +0000225 /// hasContent - Return true if this compile unit has something to write out.
David Blaikie2a80e442013-12-02 22:09:48 +0000226 bool hasContent() const { return !UnitDie->getChildren().empty(); }
Devang Patel5eb43192011-04-12 17:40:32 +0000227
Eric Christopher46e23432013-12-20 04:16:18 +0000228 /// addRange - Add an address range to the list of ranges for this unit.
Eric Christopher384f3fe2014-03-20 19:16:16 +0000229 void addRange(RangeSpan Range);
Eric Christopher46e23432013-12-20 04:16:18 +0000230
231 /// getRanges - Get the list of ranges for this unit.
232 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
233 SmallVectorImpl<RangeSpan> &getRanges() { return CURanges; }
234
Eric Christopher0f63d062013-12-03 00:45:45 +0000235 /// addRangeList - Add an address range list to the list of range lists.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000236 void addRangeList(RangeSpanList Ranges) { CURangeLists.push_back(Ranges); }
Eric Christopher0f63d062013-12-03 00:45:45 +0000237
238 /// getRangeLists - Get the vector of range lists.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000239 const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
Eric Christopher0f63d062013-12-03 00:45:45 +0000240 return CURangeLists;
241 }
Eric Christopher270ba4a2013-12-04 19:06:58 +0000242 SmallVectorImpl<RangeSpanList> &getRangeLists() { return CURangeLists; }
Eric Christopher0f63d062013-12-03 00:45:45 +0000243
Eric Christopher2c8b7902013-10-17 02:06:06 +0000244 /// getParentContextString - Get a string containing the language specific
245 /// context for a global name.
246 std::string getParentContextString(DIScope Context) const;
247
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000248 /// addGlobalName - Add a new global entity to the compile unit.
249 ///
David Blaikie65a74662014-04-25 18:26:14 +0000250 void addGlobalName(StringRef Name, DIE &Die, DIScope Context);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000251
Eric Christopher9cd26af2013-09-20 23:22:52 +0000252 /// addAccelNamespace - Add a new name to the namespace accelerator table.
David Blaikie65a74662014-04-25 18:26:14 +0000253 void addAccelNamespace(StringRef Name, const DIE &Die);
Eric Christopher9cd26af2013-09-20 23:22:52 +0000254
Devang Patel5eb43192011-04-12 17:40:32 +0000255 /// getDIE - Returns the debug information entry map slot for the
Manman Ren4dbdc902013-10-31 17:54:35 +0000256 /// specified debug variable. We delegate the request to DwarfDebug
257 /// when the MDNode can be part of the type system, since DIEs for
258 /// the type system can be shared across CUs and the mappings are
259 /// kept in DwarfDebug.
David Blaikie2ad00162013-11-15 23:09:13 +0000260 DIE *getDIE(DIDescriptor D) const;
Devang Patel5eb43192011-04-12 17:40:32 +0000261
Eric Christopher4a741042014-02-16 08:46:55 +0000262 /// getDIELoc - Returns a fresh newly allocated DIELoc.
263 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc(); }
Devang Patelf20c4f72011-04-12 22:53:02 +0000264
Manman Ren4dbdc902013-10-31 17:54:35 +0000265 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
266 /// when the MDNode can be part of the type system, since DIEs for
267 /// the type system can be shared across CUs and the mappings are
268 /// kept in DwarfDebug.
David Blaikie2ad00162013-11-15 23:09:13 +0000269 void insertDIE(DIDescriptor Desc, DIE *D);
Devang Patel5eb43192011-04-12 17:40:32 +0000270
Devang Patel5eb43192011-04-12 17:40:32 +0000271 /// addDie - Adds or interns the DIE to the compile unit.
272 ///
David Blaikie2a80e442013-12-02 22:09:48 +0000273 void addDie(DIE *Buffer) { UnitDie->addChild(Buffer); }
Devang Patel5eb43192011-04-12 17:40:32 +0000274
Eric Christopherbb69a272012-08-24 01:14:27 +0000275 /// addFlag - Add a flag that is true to the DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000276 void addFlag(DIE &Die, dwarf::Attribute Attribute);
Eric Christopher48fef592012-12-20 21:58:40 +0000277
Devang Patelf20c4f72011-04-12 22:53:02 +0000278 /// addUInt - Add an unsigned integer attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000279 void addUInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
David Blaikief2443192013-10-21 17:28:37 +0000280 uint64_t Integer);
281
David Blaikie65a74662014-04-25 18:26:14 +0000282 void addUInt(DIE &Block, dwarf::Form Form, uint64_t Integer);
Devang Patelf20c4f72011-04-12 22:53:02 +0000283
284 /// addSInt - Add an signed integer attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000285 void addSInt(DIE &Die, dwarf::Attribute Attribute, Optional<dwarf::Form> Form,
David Blaikief2443192013-10-21 17:28:37 +0000286 int64_t Integer);
287
David Blaikie65a74662014-04-25 18:26:14 +0000288 void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
Devang Patelf20c4f72011-04-12 22:53:02 +0000289
290 /// addString - Add a string attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000291 void addString(DIE &Die, dwarf::Attribute Attribute, const StringRef Str);
Devang Patelf20c4f72011-04-12 22:53:02 +0000292
Eric Christopher2cbd5762013-01-07 19:32:41 +0000293 /// addLocalString - Add a string attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000294 void addLocalString(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera07e4f52013-11-19 09:28:34 +0000295 const StringRef Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000296
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000297 /// addExpr - Add a Dwarf expression attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000298 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000299
Devang Patelf20c4f72011-04-12 22:53:02 +0000300 /// addLabel - Add a Dwarf label attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000301 void addLabel(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
Devang Patelf20c4f72011-04-12 22:53:02 +0000302 const MCSymbol *Label);
303
David Blaikie65a74662014-04-25 18:26:14 +0000304 void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
David Blaikief2443192013-10-21 17:28:37 +0000305
Eric Christophera27220f2014-03-05 22:41:20 +0000306 /// addLocationList - Add a Dwarf loclistptr attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000307 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
Eric Christophera27220f2014-03-05 22:41:20 +0000308
Eric Christopher33ff6972013-11-21 23:46:41 +0000309 /// addSectionLabel - Add a Dwarf section label attribute data and value.
310 ///
David Blaikie65a74662014-04-25 18:26:14 +0000311 void addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
Eric Christopherf52eddf2013-11-26 22:23:27 +0000312 const MCSymbol *Label);
Eric Christopher33ff6972013-11-21 23:46:41 +0000313
314 /// addSectionOffset - Add an offset into a section attribute data and value.
315 ///
David Blaikie65a74662014-04-25 18:26:14 +0000316 void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
Eric Christopher33ff6972013-11-21 23:46:41 +0000317
Eric Christophere9ec2452013-01-18 22:11:33 +0000318 /// addOpAddress - Add a dwarf op address data and value using the
319 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
David Blaikie65a74662014-04-25 18:26:14 +0000320 void addOpAddress(DIELoc &Die, const MCSymbol *Label);
Eric Christophere9ec2452013-01-18 22:11:33 +0000321
Eric Christopher33ff6972013-11-21 23:46:41 +0000322 /// addSectionDelta - Add a label delta attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000323 void addSectionDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
Eric Christopher33ff6972013-11-21 23:46:41 +0000324 const MCSymbol *Lo);
Devang Patelf20c4f72011-04-12 22:53:02 +0000325
David Blaikie48b1bdc2014-03-07 01:30:55 +0000326 /// addLabelDelta - Add a label delta attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000327 void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
David Blaikie48b1bdc2014-03-07 01:30:55 +0000328 const MCSymbol *Lo);
329
Devang Patelf20c4f72011-04-12 22:53:02 +0000330 /// addDIEEntry - Add a DIE attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000331 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE *Entry);
Eric Christopher48fef592012-12-20 21:58:40 +0000332
Manman Ren4dbdc902013-10-31 17:54:35 +0000333 /// addDIEEntry - Add a DIE attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000334 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry *Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +0000335
David Blaikie65a74662014-04-25 18:26:14 +0000336 void addDIETypeSignature(DIE &Die, const DwarfTypeUnit &Type);
David Blaikie47f615e2013-12-17 23:32:35 +0000337
Devang Patelf20c4f72011-04-12 22:53:02 +0000338 /// addBlock - Add block data.
David Blaikie65a74662014-04-25 18:26:14 +0000339 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Block);
Eric Christopher4a741042014-02-16 08:46:55 +0000340
341 /// addBlock - Add block data.
David Blaikie65a74662014-04-25 18:26:14 +0000342 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
Devang Patelf20c4f72011-04-12 22:53:02 +0000343
344 /// addSourceLine - Add location information to specified debug information
345 /// entry.
David Blaikie65a74662014-04-25 18:26:14 +0000346 void addSourceLine(DIE &Die, unsigned Line, StringRef File,
David Blaikie101613e2014-02-12 00:11:25 +0000347 StringRef Directory);
David Blaikie65a74662014-04-25 18:26:14 +0000348 void addSourceLine(DIE &Die, DIVariable V);
349 void addSourceLine(DIE &Die, DIGlobalVariable G);
350 void addSourceLine(DIE &Die, DISubprogram SP);
351 void addSourceLine(DIE &Die, DIType Ty);
352 void addSourceLine(DIE &Die, DINameSpace NS);
353 void addSourceLine(DIE &Die, DIObjCProperty Ty);
Devang Patelf20c4f72011-04-12 22:53:02 +0000354
355 /// addAddress - Add an address attribute to a die based on the location
356 /// provided.
David Blaikie65a74662014-04-25 18:26:14 +0000357 void addAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christophera07e4f52013-11-19 09:28:34 +0000358 const MachineLocation &Location, bool Indirect = false);
Devang Patelf20c4f72011-04-12 22:53:02 +0000359
Devang Patelf20c4f72011-04-12 22:53:02 +0000360 /// addConstantValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000361 void addConstantValue(DIE &Die, const MachineOperand &MO, DIType Ty);
362 void addConstantValue(DIE &Die, const ConstantInt *CI, bool Unsigned);
363 void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
Devang Patelf20c4f72011-04-12 22:53:02 +0000364
365 /// addConstantFPValue - Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000366 void addConstantFPValue(DIE &Die, const MachineOperand &MO);
367 void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
Devang Patelf20c4f72011-04-12 22:53:02 +0000368
369 /// addTemplateParams - Add template parameters in buffer.
370 void addTemplateParams(DIE &Buffer, DIArray TParams);
371
Devang Patelba5fbf12011-04-26 19:06:18 +0000372 /// addRegisterOp - Add register operand.
David Blaikie65a74662014-04-25 18:26:14 +0000373 void addRegisterOp(DIELoc &TheDie, unsigned Reg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000374
375 /// addRegisterOffset - Add register offset.
David Blaikie65a74662014-04-25 18:26:14 +0000376 void addRegisterOffset(DIELoc &TheDie, unsigned Reg, int64_t Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000377
Devang Patelf20c4f72011-04-12 22:53:02 +0000378 /// addComplexAddress - Start with the address based on the location provided,
379 /// and generate the DWARF information necessary to find the actual variable
380 /// (navigating the extra location information encoded in the type) based on
381 /// the starting location. Add the DWARF information to the die.
David Blaikie65a74662014-04-25 18:26:14 +0000382 void addComplexAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera07e4f52013-11-19 09:28:34 +0000383 dwarf::Attribute Attribute,
Devang Patelf20c4f72011-04-12 22:53:02 +0000384 const MachineLocation &Location);
385
386 // FIXME: Should be reformulated in terms of addComplexAddress.
387 /// addBlockByrefAddress - Start with the address based on the location
388 /// provided, and generate the DWARF information necessary to find the
389 /// actual Block variable (navigating the Block struct) based on the
390 /// starting location. Add the DWARF information to the die. Obsolete,
391 /// please use addComplexAddress instead.
David Blaikie65a74662014-04-25 18:26:14 +0000392 void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera07e4f52013-11-19 09:28:34 +0000393 dwarf::Attribute Attribute,
Devang Patelf20c4f72011-04-12 22:53:02 +0000394 const MachineLocation &Location);
395
Eric Christopher48fef592012-12-20 21:58:40 +0000396 /// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000397 /// DbgVariable based on provided MachineLocation.
David Blaikie65a74662014-04-25 18:26:14 +0000398 void addVariableAddress(const DbgVariable &DV, DIE &Die,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000399 MachineLocation Location);
Devang Patelf20c4f72011-04-12 22:53:02 +0000400
Eric Christopher7285c7d2012-03-28 07:34:31 +0000401 /// addType - Add a new type attribute to the specified entity. This takes
402 /// and attribute parameter because DW_AT_friend attributes are also
403 /// type references.
David Blaikie65a74662014-04-25 18:26:14 +0000404 void addType(DIE &Entity, DIType Ty,
Eric Christophera07e4f52013-11-19 09:28:34 +0000405 dwarf::Attribute Attribute = dwarf::DW_AT_type);
Devang Patelf20c4f72011-04-12 22:53:02 +0000406
407 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
408 DIE *getOrCreateNameSpace(DINameSpace NS);
409
Devang Patel89543712011-08-15 17:24:54 +0000410 /// getOrCreateSubprogramDIE - Create new DIE using SP.
411 DIE *getOrCreateSubprogramDIE(DISubprogram SP);
412
Devang Patelf20c4f72011-04-12 22:53:02 +0000413 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
414 /// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000415 DIE *getOrCreateTypeDIE(const MDNode *N);
Devang Patelf20c4f72011-04-12 22:53:02 +0000416
Eric Christopherfa205ca2013-10-05 00:05:51 +0000417 /// getOrCreateContextDIE - Get context owner's DIE.
David Blaikie409dd9c2013-11-19 23:08:21 +0000418 DIE *createTypeDIE(DICompositeType Ty);
419
420 /// getOrCreateContextDIE - Get context owner's DIE.
Eric Christopherfa205ca2013-10-05 00:05:51 +0000421 DIE *getOrCreateContextDIE(DIScope Context);
Devang Patelf20c4f72011-04-12 22:53:02 +0000422
Eric Christopherfa205ca2013-10-05 00:05:51 +0000423 /// constructContainingTypeDIEs - Construct DIEs for types that contain
424 /// vtables.
425 void constructContainingTypeDIEs();
Devang Patelf20c4f72011-04-12 22:53:02 +0000426
Eric Christopherfa205ca2013-10-05 00:05:51 +0000427 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopher34a2c872013-11-15 01:43:19 +0000428 DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract);
Eric Christopherfa205ca2013-10-05 00:05:51 +0000429
Adrian Prantl69140d22014-02-25 22:27:14 +0000430 /// constructSubprogramArguments - Construct function argument DIEs.
431 void constructSubprogramArguments(DIE &Buffer, DIArray Args);
432
Manman Renb987e512013-10-29 00:53:03 +0000433 /// Create a DIE with the given Tag, add the DIE to its parent, and
434 /// call insertDIE if MD is not null.
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000435 DIE &createAndAddDIE(unsigned Tag, DIE &Parent,
Eric Christophera07e4f52013-11-19 09:28:34 +0000436 DIDescriptor N = DIDescriptor());
Manman Renb987e512013-10-29 00:53:03 +0000437
David Blaikie6b288cf2013-10-30 20:42:41 +0000438 /// Compute the size of a header for this unit, not including the initial
439 /// length field.
David Blaikiebc563272013-12-13 21:33:40 +0000440 virtual unsigned getHeaderSize() const {
David Blaikie6b288cf2013-10-30 20:42:41 +0000441 return sizeof(int16_t) + // DWARF version number
442 sizeof(int32_t) + // Offset Into Abbrev. Section
443 sizeof(int8_t); // Pointer Size (in bytes)
444 }
445
446 /// Emit the header for this unit, not including the initial length field.
David Blaikied82b2372014-03-24 20:28:10 +0000447 virtual void emitHeader(const MCSymbol *ASectionSym) const;
David Blaikie6b288cf2013-10-30 20:42:41 +0000448
David Blaikie15632ae2014-02-12 00:31:30 +0000449 virtual DwarfCompileUnit &getCU() = 0;
David Blaikied696fac2014-02-12 00:32:05 +0000450
David Blaikie319a05f2013-12-02 19:33:10 +0000451protected:
452 /// getOrCreateStaticMemberDIE - Create new static data member DIE.
453 DIE *getOrCreateStaticMemberDIE(DIDerivedType DT);
454
David Blaikie4a2f95f2014-03-18 01:17:26 +0000455 /// Look up the source ID with the given directory and source file names. If
456 /// none currently exists, create a new ID and insert it in the line table.
457 virtual unsigned getOrCreateSourceID(StringRef File, StringRef Directory) = 0;
458
Eric Christopherfa205ca2013-10-05 00:05:51 +0000459private:
David Blaikiee071fc82014-04-25 17:32:19 +0000460 /// \brief Construct a DIE for the given DbgVariable without initializing the
461 /// DbgVariable's DIE reference.
462 DIE *constructVariableDIEImpl(const DbgVariable &DV, bool isScopeAbstract);
463
Devang Patelf20c4f72011-04-12 22:53:02 +0000464 /// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christopherf0388b72013-10-04 23:49:29 +0000465 void constructTypeDIE(DIE &Buffer, DIBasicType BTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000466
467 /// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christopherf0388b72013-10-04 23:49:29 +0000468 void constructTypeDIE(DIE &Buffer, DIDerivedType DTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000469
470 /// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christopherf0388b72013-10-04 23:49:29 +0000471 void constructTypeDIE(DIE &Buffer, DICompositeType CTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000472
473 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
474 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
475
476 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christopherdf9955d2013-11-11 18:52:31 +0000477 void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000478
479 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
Eric Christopheraeb105f2013-11-11 18:52:39 +0000480 void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy);
Devang Patelf20c4f72011-04-12 22:53:02 +0000481
Manman Ren230ec862013-10-23 23:00:44 +0000482 /// constructMemberDIE - Construct member DIE from DIDerivedType.
483 void constructMemberDIE(DIE &Buffer, DIDerivedType DT);
Manman Ren7cc62702013-10-18 21:14:19 +0000484
Manman Renffc9a712013-10-23 23:05:28 +0000485 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
486 /// DITemplateTypeParameter.
487 void constructTemplateTypeParameterDIE(DIE &Buffer,
488 DITemplateTypeParameter TP);
Manman Ren7cc62702013-10-18 21:14:19 +0000489
Manman Renffc9a712013-10-23 23:05:28 +0000490 /// constructTemplateValueParameterDIE - Construct new DIE for the given
491 /// DITemplateValueParameter.
492 void constructTemplateValueParameterDIE(DIE &Buffer,
493 DITemplateValueParameter TVP);
Devang Patelf20c4f72011-04-12 22:53:02 +0000494
Eric Christopherfa205ca2013-10-05 00:05:51 +0000495 /// getLowerBoundDefault - Return the default lower bound for an array. If the
496 /// DWARF version doesn't handle the language, return -1.
497 int64_t getDefaultLowerBound() const;
498
499 /// getDIEEntry - Returns the debug information entry for the specified
500 /// debug variable.
501 DIEEntry *getDIEEntry(const MDNode *N) const {
502 return MDNodeToDIEEntryMap.lookup(N);
503 }
504
505 /// insertDIEEntry - Insert debug information entry into the map.
506 void insertDIEEntry(const MDNode *N, DIEEntry *E) {
507 MDNodeToDIEEntryMap.insert(std::make_pair(N, E));
508 }
509
510 // getIndexTyDie - Get an anonymous type for index type.
511 DIE *getIndexTyDie() { return IndexTyDie; }
512
513 // setIndexTyDie - Set D as anonymous type for index which can be reused
514 // later.
515 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
516
517 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
518 /// information entry.
519 DIEEntry *createDIEEntry(DIE *Entry);
David Blaikie684fc532013-05-06 23:33:07 +0000520
Eric Christopher9e429ae2013-10-05 00:27:02 +0000521 /// resolve - Look in the DwarfDebug map for the MDNode that
Eric Christopher87b9c492013-10-05 00:32:34 +0000522 /// corresponds to the reference.
Eric Christopher9e429ae2013-10-05 00:27:02 +0000523 template <typename T> T resolve(DIRef<T> Ref) const {
524 return DD->resolve(Ref);
525 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000526
527 /// If this is a named finished type then include it in the list of types for
528 /// the accelerator tables.
David Blaikieb0b3fcf2014-04-25 18:52:29 +0000529 void updateAcceleratorTables(DIScope Context, DIType Ty, const DIE &TyDIE);
Devang Patel5eb43192011-04-12 17:40:32 +0000530};
531
Eric Christopher4287a492013-12-09 23:57:44 +0000532class DwarfCompileUnit : public DwarfUnit {
David Blaikie60e63862014-02-14 23:58:13 +0000533 /// The attribute index of DW_AT_stmt_list in the compile unit DIE, avoiding
534 /// the need to search for it in applyStmtList.
535 unsigned stmtListIndex;
536
David Blaikie319a05f2013-12-02 19:33:10 +0000537public:
Eric Christopher4287a492013-12-09 23:57:44 +0000538 DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
539 DwarfDebug *DW, DwarfFile *DWU);
David Blaikie319a05f2013-12-02 19:33:10 +0000540
David Blaikie2494fdb2014-02-14 22:41:51 +0000541 void initStmtList(MCSymbol *DwarfLineSectionSym);
542
David Blaikie60e63862014-02-14 23:58:13 +0000543 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
544 void applyStmtList(DIE &D);
545
David Blaikie319a05f2013-12-02 19:33:10 +0000546 /// createGlobalVariableDIE - create global variable DIE.
547 void createGlobalVariableDIE(DIGlobalVariable GV);
548
549 /// addLabelAddress - Add a dwarf label attribute data and value using
550 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
David Blaikie65a74662014-04-25 18:26:14 +0000551 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000552 const MCSymbol *Label);
553
554 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
555 /// DW_FORM_addr only.
David Blaikie65a74662014-04-25 18:26:14 +0000556 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000557 const MCSymbol *Label);
David Blaikie15632ae2014-02-12 00:31:30 +0000558
Craig Topper73156022014-03-02 09:09:27 +0000559 DwarfCompileUnit &getCU() override { return *this; }
David Blaikie0e8d4012014-03-17 23:53:25 +0000560
David Blaikie4a2f95f2014-03-18 01:17:26 +0000561 unsigned getOrCreateSourceID(StringRef FileName, StringRef DirName) override;
David Blaikie319a05f2013-12-02 19:33:10 +0000562};
563
Eric Christopher4287a492013-12-09 23:57:44 +0000564class DwarfTypeUnit : public DwarfUnit {
David Blaikie319a05f2013-12-02 19:33:10 +0000565private:
David Blaikiebc563272013-12-13 21:33:40 +0000566 uint64_t TypeSignature;
567 const DIE *Ty;
David Blaikie15632ae2014-02-12 00:31:30 +0000568 DwarfCompileUnit &CU;
David Blaikie8287aff2014-03-18 02:13:23 +0000569 MCDwarfDwoLineTable *SplitLineTable;
David Blaikie319a05f2013-12-02 19:33:10 +0000570
571public:
David Blaikie15632ae2014-02-12 00:31:30 +0000572 DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU, AsmPrinter *A,
David Blaikie4a2f95f2014-03-18 01:17:26 +0000573 DwarfDebug *DW, DwarfFile *DWU,
David Blaikie8287aff2014-03-18 02:13:23 +0000574 MCDwarfDwoLineTable *SplitLineTable = nullptr);
David Blaikie319a05f2013-12-02 19:33:10 +0000575
David Blaikiebc563272013-12-13 21:33:40 +0000576 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
David Blaikie47f615e2013-12-17 23:32:35 +0000577 uint64_t getTypeSignature() const { return TypeSignature; }
David Blaikiebc563272013-12-13 21:33:40 +0000578 void setType(const DIE *Ty) { this->Ty = Ty; }
579
David Blaikiebc563272013-12-13 21:33:40 +0000580 /// Emit the header for this unit, not including the initial length field.
David Blaikied82b2372014-03-24 20:28:10 +0000581 void emitHeader(const MCSymbol *ASectionSym) const override;
Craig Topper73156022014-03-02 09:09:27 +0000582 unsigned getHeaderSize() const override {
David Blaikiebc563272013-12-13 21:33:40 +0000583 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
584 sizeof(uint32_t); // Type DIE Offset
585 }
586 void initSection(const MCSection *Section);
Craig Topper73156022014-03-02 09:09:27 +0000587 DwarfCompileUnit &getCU() override { return CU; }
David Blaikie4a2f95f2014-03-18 01:17:26 +0000588
589protected:
590 unsigned getOrCreateSourceID(StringRef File, StringRef Directory) override;
David Blaikie319a05f2013-12-02 19:33:10 +0000591};
Devang Patel5eb43192011-04-12 17:40:32 +0000592} // end llvm namespace
593#endif