blob: 82e9bb008a00181e6837170ada1e4eae3f08a043 [file] [log] [blame]
Stephen Hines36b56882014-04-23 16:57:46 -07001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel161b2f42011-04-12 23:21:44 +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//
Eric Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
Stephen Hines36b56882014-04-23 16:57:46 -070016#include "DwarfUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Stephen Hines36b56882014-04-23 16:57:46 -070021#include "llvm/IR/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/IR/Mangler.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCContext.h"
David Blaikie1d361132013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Stephen Hines36b56882014-04-23 16:57:46 -070030#include "llvm/Support/CommandLine.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikie59eaa382013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Stephen Hines36b56882014-04-23 16:57:46 -070033#include "llvm/Target/TargetMachine.h"
Devang Patel161b2f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Stephen Hines36b56882014-04-23 16:57:46 -070038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
42
43/// Unit - Unit constructor.
44DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
46 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Stephen Hines36b56882014-04-23 16:57:46 -070049}
50
51DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
54 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie942431f2013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel161b2f42011-04-12 23:21:44 +000056}
57
Stephen Hines36b56882014-04-23 16:57:46 -070058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
59 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU,
60 MCDwarfDwoLineTable *SplitLineTable)
61 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU),
62 SplitLineTable(SplitLineTable) {
63 if (SplitLineTable)
64 addSectionOffset(UnitDie.get(), dwarf::DW_AT_stmt_list, 0);
65}
66
67/// ~Unit - Destructor for compile unit.
68DwarfUnit::~DwarfUnit() {
Devang Patel161b2f42011-04-12 23:21:44 +000069 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
70 DIEBlocks[j]->~DIEBlock();
Stephen Hines36b56882014-04-23 16:57:46 -070071 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
72 DIELocs[j]->~DIELoc();
Devang Patel161b2f42011-04-12 23:21:44 +000073}
74
75/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
76/// information entry.
Stephen Hines36b56882014-04-23 16:57:46 -070077DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel161b2f42011-04-12 23:21:44 +000078 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
79 return Value;
80}
81
Bill Wendling6afe4782012-12-06 07:55:19 +000082/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000083/// DWARF version doesn't handle the language, return -1.
Stephen Hines36b56882014-04-23 16:57:46 -070084int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikieaedaa722013-11-15 23:50:53 +000085 switch (getLanguage()) {
Bill Wendling222c2fd2012-12-06 07:38:10 +000086 default:
87 break;
88
89 case dwarf::DW_LANG_C89:
90 case dwarf::DW_LANG_C99:
91 case dwarf::DW_LANG_C:
92 case dwarf::DW_LANG_C_plus_plus:
93 case dwarf::DW_LANG_ObjC:
94 case dwarf::DW_LANG_ObjC_plus_plus:
95 return 0;
96
97 case dwarf::DW_LANG_Fortran77:
98 case dwarf::DW_LANG_Fortran90:
99 case dwarf::DW_LANG_Fortran95:
100 return 1;
101
102 // The languages below have valid values only if the DWARF version >= 4.
103 case dwarf::DW_LANG_Java:
104 case dwarf::DW_LANG_Python:
105 case dwarf::DW_LANG_UPC:
106 case dwarf::DW_LANG_D:
107 if (dwarf::DWARF_VERSION >= 4)
108 return 0;
109 break;
110
111 case dwarf::DW_LANG_Ada83:
112 case dwarf::DW_LANG_Ada95:
113 case dwarf::DW_LANG_Cobol74:
114 case dwarf::DW_LANG_Cobol85:
115 case dwarf::DW_LANG_Modula2:
116 case dwarf::DW_LANG_Pascal83:
117 case dwarf::DW_LANG_PLI:
118 if (dwarf::DWARF_VERSION >= 4)
119 return 1;
120 break;
121 }
122
123 return -1;
124}
125
Manman Renb8b70e12013-10-31 17:54:35 +0000126/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie86a33482013-11-15 22:59:36 +0000127static bool isShareableAcrossCUs(DIDescriptor D) {
Stephen Hines36b56882014-04-23 16:57:46 -0700128 // When the MDNode can be part of the type system, the DIE can be shared
129 // across CUs.
130 // Combining type units and cross-CU DIE sharing is lower value (since
131 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
132 // level already) but may be implementable for some value in projects
133 // building multiple independent libraries with LTO and then linking those
134 // together.
135 return (D.isType() ||
136 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
137 !GenerateDwarfTypeUnits;
Manman Renb8b70e12013-10-31 17:54:35 +0000138}
139
140/// getDIE - Returns the debug information entry map slot for the
141/// specified debug variable. We delegate the request to DwarfDebug
142/// when the DIE for this MDNode can be shared across CUs. The mappings
143/// will be kept in DwarfDebug for shareable DIEs.
Stephen Hines36b56882014-04-23 16:57:46 -0700144DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikiecbc85a22013-11-15 23:09:13 +0000145 if (isShareableAcrossCUs(D))
146 return DD->getDIE(D);
147 return MDNodeToDieMap.lookup(D);
Manman Renb8b70e12013-10-31 17:54:35 +0000148}
149
150/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
151/// when the DIE for this MDNode can be shared across CUs. The mappings
152/// will be kept in DwarfDebug for shareable DIEs.
Stephen Hines36b56882014-04-23 16:57:46 -0700153void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikiecbc85a22013-11-15 23:09:13 +0000154 if (isShareableAcrossCUs(Desc)) {
155 DD->insertDIE(Desc, D);
Manman Renb8b70e12013-10-31 17:54:35 +0000156 return;
157 }
David Blaikiecbc85a22013-11-15 23:09:13 +0000158 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Renb8b70e12013-10-31 17:54:35 +0000159}
160
Eric Christopher873cf0a2012-08-24 01:14:27 +0000161/// addFlag - Add a flag that is true.
Stephen Hines36b56882014-04-23 16:57:46 -0700162void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmandc42d032013-09-04 04:39:38 +0000163 if (DD->getDwarfVersion() >= 4)
Eric Christopher9c0c9482013-10-04 22:40:05 +0000164 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopher873cf0a2012-08-24 01:14:27 +0000165 else
Eric Christopher9c0c9482013-10-04 22:40:05 +0000166 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopher873cf0a2012-08-24 01:14:27 +0000167}
168
Devang Patel161b2f42011-04-12 23:21:44 +0000169/// addUInt - Add an unsigned integer attribute data and value.
170///
Stephen Hines36b56882014-04-23 16:57:46 -0700171void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
172 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopher6efc0432013-10-19 01:04:47 +0000173 if (!Form)
174 Form = DIEInteger::BestForm(false, Integer);
175 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
176 DIEInteger(Integer);
David Blaikie770530b2013-10-21 17:28:37 +0000177 Die->addValue(Attribute, *Form, Value);
178}
179
Stephen Hines36b56882014-04-23 16:57:46 -0700180void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikie770530b2013-10-21 17:28:37 +0000181 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel161b2f42011-04-12 23:21:44 +0000182}
183
184/// addSInt - Add an signed integer attribute data and value.
185///
Stephen Hines36b56882014-04-23 16:57:46 -0700186void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
187 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopher6efc0432013-10-19 01:04:47 +0000188 if (!Form)
189 Form = DIEInteger::BestForm(true, Integer);
Devang Patel161b2f42011-04-12 23:21:44 +0000190 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikie770530b2013-10-21 17:28:37 +0000191 Die->addValue(Attribute, *Form, Value);
192}
193
Stephen Hines36b56882014-04-23 16:57:46 -0700194void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form,
195 int64_t Integer) {
David Blaikie770530b2013-10-21 17:28:37 +0000196 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel161b2f42011-04-12 23:21:44 +0000197}
198
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000199/// addString - Add a string attribute data and value. We always emit a
200/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopher3cc42202013-01-07 19:32:45 +0000201/// more predictable sizes. In the case of split dwarf we emit an index
202/// into another table which gets us the static offset into the string
203/// table.
Stephen Hines36b56882014-04-23 16:57:46 -0700204void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
205 StringRef String) {
206
207 if (!DD->useSplitDwarf())
208 return addLocalString(Die, Attribute, String);
209
210 unsigned idx = DU->getStringPoolIndex(String);
211 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher3dee5752013-07-26 17:02:41 +0000212 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Stephen Hines36b56882014-04-23 16:57:46 -0700213 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000214}
215
216/// addLocalString - Add a string attribute data and value. This is guaranteed
217/// to be in the local string pool instead of indirected.
Stephen Hines36b56882014-04-23 16:57:46 -0700218void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
219 StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000220 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000221 DIEValue *Value;
Stephen Hines36b56882014-04-23 16:57:46 -0700222 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000223 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000224 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000225 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000226 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000227 }
Stephen Hines36b56882014-04-23 16:57:46 -0700228 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
229 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel161b2f42011-04-12 23:21:44 +0000230}
231
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000232/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel161b2f42011-04-12 23:21:44 +0000233///
Stephen Hines36b56882014-04-23 16:57:46 -0700234void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000235 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikie770530b2013-10-21 17:28:37 +0000236 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000237}
238
Stephen Hines36b56882014-04-23 16:57:46 -0700239/// addLocationList - Add a Dwarf loclistptr attribute data and value.
240///
241void DwarfUnit::addLocationList(DIE *Die, dwarf::Attribute Attribute,
242 unsigned Index) {
243 DIEValue *Value = new (DIEValueAllocator) DIELocList(Index);
244 dwarf::Form Form = DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
245 : dwarf::DW_FORM_data4;
246 Die->addValue(Attribute, Form, Value);
247}
248
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000249/// addLabel - Add a Dwarf label attribute data and value.
250///
Stephen Hines36b56882014-04-23 16:57:46 -0700251void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
252 const MCSymbol *Label) {
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +0000253 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
254 Die->addValue(Attribute, Form, Value);
David Blaikie95e72c92013-06-28 20:05:04 +0000255}
256
Stephen Hines36b56882014-04-23 16:57:46 -0700257void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikie770530b2013-10-21 17:28:37 +0000258 addLabel(Die, (dwarf::Attribute)0, Form, Label);
259}
260
Stephen Hines36b56882014-04-23 16:57:46 -0700261/// addSectionLabel - Add a Dwarf section label attribute data and value.
262///
263void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
264 const MCSymbol *Label) {
265 if (DD->getDwarfVersion() >= 4)
266 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
267 else
268 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
269}
270
271/// addSectionOffset - Add an offset into a section attribute data and value.
272///
273void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
274 uint64_t Integer) {
275 if (DD->getDwarfVersion() >= 4)
276 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
277 else
278 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
279}
280
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000281/// addLabelAddress - Add a dwarf label attribute data and value using
282/// DW_FORM_addr or DW_FORM_GNU_addr_index.
283///
Stephen Hines36b56882014-04-23 16:57:46 -0700284void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
285 const MCSymbol *Label) {
286
287 if (!DD->useSplitDwarf())
288 return addLocalLabelAddress(Die, Attribute, Label);
289
Alexey Samsonov9d08d692013-10-03 08:54:43 +0000290 if (Label)
291 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton5cc319a2013-09-19 23:21:01 +0000292
Stephen Hines36b56882014-04-23 16:57:46 -0700293 unsigned idx = DU->getAddrPoolIndex(Label);
294 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
295 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
296}
297
298void DwarfCompileUnit::addLocalLabelAddress(DIE *Die,
299 dwarf::Attribute Attribute,
300 const MCSymbol *Label) {
301 if (Label)
302 DD->addArangeLabel(SymbolCU(this, Label));
303
304 if (Label) {
305 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
306 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000307 } else {
Stephen Hines36b56882014-04-23 16:57:46 -0700308 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
309 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000310 }
311}
312
Stephen Hines36b56882014-04-23 16:57:46 -0700313unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
314 // If we print assembly, we can't separate .file entries according to
315 // compile units. Thus all files will belong to the default compile unit.
316
317 // FIXME: add a better feature test than hasRawTextSupport. Even better,
318 // extend .file to support this.
319 return Asm->OutStreamer.EmitDwarfFileDirective(
320 0, DirName, FileName,
321 Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
322}
323
324unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
325 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
326 : getCU().getOrCreateSourceID(FileName, DirName);
327}
328
Eric Christopher0969ddf2013-01-18 22:11:33 +0000329/// addOpAddress - Add a dwarf op address data and value using the
330/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
331///
Stephen Hines36b56882014-04-23 16:57:46 -0700332void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) {
Eric Christopher0969ddf2013-01-18 22:11:33 +0000333 if (!DD->useSplitDwarf()) {
David Blaikie770530b2013-10-21 17:28:37 +0000334 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
335 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christopher0969ddf2013-01-18 22:11:33 +0000336 } else {
David Blaikie770530b2013-10-21 17:28:37 +0000337 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
338 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christopher0969ddf2013-01-18 22:11:33 +0000339 }
340}
341
Stephen Hines36b56882014-04-23 16:57:46 -0700342/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel161b2f42011-04-12 23:21:44 +0000343///
Stephen Hines36b56882014-04-23 16:57:46 -0700344void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
345 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel161b2f42011-04-12 23:21:44 +0000346 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Stephen Hines36b56882014-04-23 16:57:46 -0700347 if (DD->getDwarfVersion() >= 4)
348 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
349 else
350 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
351}
352
353void DwarfUnit::addLabelDelta(DIE *Die, dwarf::Attribute Attribute,
354 const MCSymbol *Hi, const MCSymbol *Lo) {
355 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
356 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000357}
358
359/// addDIEEntry - Add a DIE attribute data and value.
360///
Stephen Hines36b56882014-04-23 16:57:46 -0700361void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Renb8b70e12013-10-31 17:54:35 +0000362 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
363}
364
Stephen Hines36b56882014-04-23 16:57:46 -0700365void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
366 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
367 new (DIEValueAllocator) DIETypeSignature(Type));
368}
369
370void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
371 DIEEntry *Entry) {
372 const DIE *DieCU = Die->getUnitOrNull();
373 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Renb8b70e12013-10-31 17:54:35 +0000374 if (!DieCU)
375 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
Stephen Hines36b56882014-04-23 16:57:46 -0700376 DieCU = getUnitDie();
Manman Renb8b70e12013-10-31 17:54:35 +0000377 if (!EntryCU)
Stephen Hines36b56882014-04-23 16:57:46 -0700378 EntryCU = getUnitDie();
Manman Renb8b70e12013-10-31 17:54:35 +0000379 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
380 : dwarf::DW_FORM_ref_addr,
381 Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000382}
383
Manman Ren1a5e7872013-10-29 00:53:03 +0000384/// Create a DIE with the given Tag, add the DIE to its parent, and
385/// call insertDIE if MD is not null.
Stephen Hines36b56882014-04-23 16:57:46 -0700386DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Ren1a5e7872013-10-29 00:53:03 +0000387 DIE *Die = new DIE(Tag);
388 Parent.addChild(Die);
David Blaikie1dc27232013-11-16 00:29:01 +0000389 if (N)
390 insertDIE(N, Die);
Manman Ren1a5e7872013-10-29 00:53:03 +0000391 return Die;
392}
393
Devang Patel161b2f42011-04-12 23:21:44 +0000394/// addBlock - Add block data.
395///
Stephen Hines36b56882014-04-23 16:57:46 -0700396void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) {
397 Loc->ComputeSize(Asm);
398 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
399 Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
400}
401
402void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
403 DIEBlock *Block) {
Devang Patel161b2f42011-04-12 23:21:44 +0000404 Block->ComputeSize(Asm);
405 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
406 Die->addValue(Attribute, Block->BestForm(), Block);
407}
408
409/// addSourceLine - Add location information to specified debug information
410/// entry.
Stephen Hines36b56882014-04-23 16:57:46 -0700411void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
412 StringRef Directory) {
Devang Patel161b2f42011-04-12 23:21:44 +0000413 if (Line == 0)
414 return;
Stephen Hines36b56882014-04-23 16:57:46 -0700415
416 unsigned FileID = getOrCreateSourceID(File, Directory);
Devang Patel161b2f42011-04-12 23:21:44 +0000417 assert(FileID && "Invalid file id");
David Blaikie770530b2013-10-21 17:28:37 +0000418 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
419 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel161b2f42011-04-12 23:21:44 +0000420}
421
422/// addSourceLine - Add location information to specified debug information
423/// entry.
Stephen Hines36b56882014-04-23 16:57:46 -0700424void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
425 assert(V.isVariable());
Devang Patel161b2f42011-04-12 23:21:44 +0000426
Stephen Hines36b56882014-04-23 16:57:46 -0700427 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
428 V.getContext().getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000429}
430
431/// addSourceLine - Add location information to specified debug information
432/// entry.
Stephen Hines36b56882014-04-23 16:57:46 -0700433void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
434 assert(G.isGlobalVariable());
Eric Christopher2125d5a2012-03-15 23:55:40 +0000435
Stephen Hines36b56882014-04-23 16:57:46 -0700436 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000437}
438
439/// addSourceLine - Add location information to specified debug information
440/// entry.
Stephen Hines36b56882014-04-23 16:57:46 -0700441void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
442 assert(SP.isSubprogram());
Devang Patel161b2f42011-04-12 23:21:44 +0000443
Stephen Hines36b56882014-04-23 16:57:46 -0700444 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000445}
446
447/// addSourceLine - Add location information to specified debug information
448/// entry.
Stephen Hines36b56882014-04-23 16:57:46 -0700449void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
450 assert(Ty.isType());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000451
Stephen Hines36b56882014-04-23 16:57:46 -0700452 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
453}
454
455/// addSourceLine - Add location information to specified debug information
456/// entry.
457void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
458 assert(Ty.isObjCProperty());
459
Eric Christopherb8ca9882012-03-29 08:42:56 +0000460 DIFile File = Ty.getFile();
Stephen Hines36b56882014-04-23 16:57:46 -0700461 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
462 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000463}
464
465/// addSourceLine - Add location information to specified debug information
466/// entry.
Stephen Hines36b56882014-04-23 16:57:46 -0700467void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
468 assert(NS.Verify());
Devang Patel161b2f42011-04-12 23:21:44 +0000469
Stephen Hines36b56882014-04-23 16:57:46 -0700470 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000471}
472
Eric Christopher8b4310b2012-11-21 00:34:38 +0000473/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000474/// DbgVariable based on provided MachineLocation.
Stephen Hines36b56882014-04-23 16:57:46 -0700475void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
476 MachineLocation Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000477 if (DV.variableHasComplexAddress())
Devang Patel161b2f42011-04-12 23:21:44 +0000478 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherf61dbc12013-06-24 21:07:27 +0000479 else if (DV.isBlockByrefVariable())
Devang Patel161b2f42011-04-12 23:21:44 +0000480 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
481 else
David Blaikie4532c282013-06-20 00:25:24 +0000482 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherf61dbc12013-06-24 21:07:27 +0000483 DV.getVariable().isIndirect());
Devang Patel161b2f42011-04-12 23:21:44 +0000484}
485
Devang Patel116da2f2011-04-26 19:06:18 +0000486/// addRegisterOp - Add register operand.
Stephen Hines36b56882014-04-23 16:57:46 -0700487void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) {
Devang Patel116da2f2011-04-26 19:06:18 +0000488 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Stephen Hines36b56882014-04-23 16:57:46 -0700489 int DWReg = RI->getDwarfRegNum(Reg, false);
490 bool isSubRegister = DWReg < 0;
491
492 unsigned Idx = 0;
493
494 // Go up the super-register chain until we hit a valid dwarf register number.
495 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
496 DWReg = RI->getDwarfRegNum(*SR, false);
497 if (DWReg >= 0)
498 Idx = RI->getSubRegIndex(*SR, Reg);
499 }
500
501 if (DWReg < 0) {
502 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
503 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
504 return;
505 }
506
507 // Emit register
Devang Patel116da2f2011-04-26 19:06:18 +0000508 if (DWReg < 32)
David Blaikie770530b2013-10-21 17:28:37 +0000509 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patel116da2f2011-04-26 19:06:18 +0000510 else {
David Blaikie770530b2013-10-21 17:28:37 +0000511 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
512 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patel116da2f2011-04-26 19:06:18 +0000513 }
Stephen Hines36b56882014-04-23 16:57:46 -0700514
515 // Emit Mask
516 if (isSubRegister) {
517 unsigned Size = RI->getSubRegIdxSize(Idx);
518 unsigned Offset = RI->getSubRegIdxOffset(Idx);
519 if (Offset > 0) {
520 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
521 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
522 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
523 } else {
524 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
525 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
526 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
527 }
528 }
Devang Patel116da2f2011-04-26 19:06:18 +0000529}
530
531/// addRegisterOffset - Add register offset.
Stephen Hines36b56882014-04-23 16:57:46 -0700532void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg,
533 int64_t Offset) {
Devang Patel116da2f2011-04-26 19:06:18 +0000534 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
535 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
536 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
537 if (Reg == TRI->getFrameRegister(*Asm->MF))
538 // If variable offset is based in frame register then use fbreg.
David Blaikie770530b2013-10-21 17:28:37 +0000539 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patel116da2f2011-04-26 19:06:18 +0000540 else if (DWReg < 32)
David Blaikie770530b2013-10-21 17:28:37 +0000541 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patel116da2f2011-04-26 19:06:18 +0000542 else {
David Blaikie770530b2013-10-21 17:28:37 +0000543 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
544 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patel116da2f2011-04-26 19:06:18 +0000545 }
David Blaikie770530b2013-10-21 17:28:37 +0000546 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patel116da2f2011-04-26 19:06:18 +0000547}
548
549/// addAddress - Add an address attribute to a die based on the location
550/// provided.
Stephen Hines36b56882014-04-23 16:57:46 -0700551void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
552 const MachineLocation &Location, bool Indirect) {
553 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel116da2f2011-04-26 19:06:18 +0000554
David Blaikie4532c282013-06-20 00:25:24 +0000555 if (Location.isReg() && !Indirect)
Stephen Hines36b56882014-04-23 16:57:46 -0700556 addRegisterOp(Loc, Location.getReg());
David Blaikie4532c282013-06-20 00:25:24 +0000557 else {
Stephen Hines36b56882014-04-23 16:57:46 -0700558 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
David Blaikie4532c282013-06-20 00:25:24 +0000559 if (Indirect && !Location.isReg()) {
Stephen Hines36b56882014-04-23 16:57:46 -0700560 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikie4532c282013-06-20 00:25:24 +0000561 }
562 }
Devang Patel116da2f2011-04-26 19:06:18 +0000563
564 // Now attach the location information to the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700565 addBlock(Die, Attribute, Loc);
Devang Patel116da2f2011-04-26 19:06:18 +0000566}
567
Devang Patel161b2f42011-04-12 23:21:44 +0000568/// addComplexAddress - Start with the address based on the location provided,
569/// and generate the DWARF information necessary to find the actual variable
Stephen Hines36b56882014-04-23 16:57:46 -0700570/// given the extra address information encoded in the DbgVariable, starting
571/// from the starting location. Add the DWARF information to the die.
Devang Patel161b2f42011-04-12 23:21:44 +0000572///
Stephen Hines36b56882014-04-23 16:57:46 -0700573void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
574 dwarf::Attribute Attribute,
575 const MachineLocation &Location) {
576 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherf61dbc12013-06-24 21:07:27 +0000577 unsigned N = DV.getNumAddrElements();
Devang Patelc26f5442011-04-28 02:22:40 +0000578 unsigned i = 0;
579 if (Location.isReg()) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000580 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patelc26f5442011-04-28 02:22:40 +0000581 // If first address element is OpPlus then emit
582 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Stephen Hines36b56882014-04-23 16:57:46 -0700583 addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patelc26f5442011-04-28 02:22:40 +0000584 i = 2;
585 } else
Stephen Hines36b56882014-04-23 16:57:46 -0700586 addRegisterOp(Loc, Location.getReg());
Eric Christopher6efc0432013-10-19 01:04:47 +0000587 } else
Stephen Hines36b56882014-04-23 16:57:46 -0700588 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000589
Eric Christopher6efc0432013-10-19 01:04:47 +0000590 for (; i < N; ++i) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000591 uint64_t Element = DV.getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000592 if (Element == DIBuilder::OpPlus) {
Stephen Hines36b56882014-04-23 16:57:46 -0700593 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
594 addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel161b2f42011-04-12 23:21:44 +0000595 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000596 if (!Location.isReg())
Stephen Hines36b56882014-04-23 16:57:46 -0700597 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopher6efc0432013-10-19 01:04:47 +0000598 } else
599 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel161b2f42011-04-12 23:21:44 +0000600 }
601
602 // Now attach the location information to the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700603 addBlock(Die, Attribute, Loc);
Devang Patel161b2f42011-04-12 23:21:44 +0000604}
605
606/* Byref variables, in Blocks, are declared by the programmer as "SomeType
607 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
608 gives the variable VarName either the struct, or a pointer to the struct, as
609 its type. This is necessary for various behind-the-scenes things the
610 compiler needs to do with by-reference variables in Blocks.
611
612 However, as far as the original *programmer* is concerned, the variable
613 should still have type 'SomeType', as originally declared.
614
615 The function getBlockByrefType dives into the __Block_byref_x_VarName
616 struct to find the original type of the variable, which is then assigned to
617 the variable's Debug Information Entry as its real type. So far, so good.
618 However now the debugger will expect the variable VarName to have the type
619 SomeType. So we need the location attribute for the variable to be an
620 expression that explains to the debugger how to navigate through the
621 pointers and struct to find the actual variable of type SomeType.
622
623 The following function does just that. We start by getting
624 the "normal" location for the variable. This will be the location
625 of either the struct __Block_byref_x_VarName or the pointer to the
626 struct __Block_byref_x_VarName.
627
628 The struct will look something like:
629
630 struct __Block_byref_x_VarName {
631 ... <various fields>
632 struct __Block_byref_x_VarName *forwarding;
633 ... <various other fields>
634 SomeType VarName;
635 ... <maybe more fields>
636 };
637
638 If we are given the struct directly (as our starting point) we
639 need to tell the debugger to:
640
641 1). Add the offset of the forwarding field.
642
643 2). Follow that pointer to get the real __Block_byref_x_VarName
644 struct to use (the real one may have been copied onto the heap).
645
646 3). Add the offset for the field VarName, to find the actual variable.
647
648 If we started with a pointer to the struct, then we need to
649 dereference that pointer first, before the other steps.
650 Translating this into DWARF ops, we will need to append the following
651 to the current location description for the variable:
652
653 DW_OP_deref -- optional, if we start with a pointer
654 DW_OP_plus_uconst <forward_fld_offset>
655 DW_OP_deref
656 DW_OP_plus_uconst <varName_fld_offset>
657
658 That is what this function does. */
659
660/// addBlockByrefAddress - Start with the address based on the location
661/// provided, and generate the DWARF information necessary to find the
662/// actual Block variable (navigating the Block struct) based on the
663/// starting location. Add the DWARF information to the die. For
664/// more information, read large comment just above here.
665///
Stephen Hines36b56882014-04-23 16:57:46 -0700666void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
667 dwarf::Attribute Attribute,
668 const MachineLocation &Location) {
Eric Christopherf61dbc12013-06-24 21:07:27 +0000669 DIType Ty = DV.getType();
Devang Patel161b2f42011-04-12 23:21:44 +0000670 DIType TmpTy = Ty;
Eric Christopher31667622013-08-08 01:41:00 +0000671 uint16_t Tag = Ty.getTag();
Devang Patel161b2f42011-04-12 23:21:44 +0000672 bool isPointer = false;
673
Eric Christopherf61dbc12013-06-24 21:07:27 +0000674 StringRef varName = DV.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000675
676 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4adba522013-11-18 23:33:32 +0000677 DIDerivedType DTy(Ty);
Manman Ren017ceda2013-10-08 18:42:58 +0000678 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel161b2f42011-04-12 23:21:44 +0000679 isPointer = true;
680 }
681
David Blaikie4adba522013-11-18 23:33:32 +0000682 DICompositeType blockStruct(TmpTy);
Devang Patel161b2f42011-04-12 23:21:44 +0000683
684 // Find the __forwarding field and the variable field in the __Block_byref
685 // struct.
686 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4adba522013-11-18 23:33:32 +0000687 DIDerivedType varField;
688 DIDerivedType forwardingField;
Devang Patel161b2f42011-04-12 23:21:44 +0000689
690 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4adba522013-11-18 23:33:32 +0000691 DIDerivedType DT(Fields.getElement(i));
Devang Patel161b2f42011-04-12 23:21:44 +0000692 StringRef fieldName = DT.getName();
693 if (fieldName == "__forwarding")
David Blaikie4adba522013-11-18 23:33:32 +0000694 forwardingField = DT;
Devang Patel161b2f42011-04-12 23:21:44 +0000695 else if (fieldName == varName)
David Blaikie4adba522013-11-18 23:33:32 +0000696 varField = DT;
Devang Patel161b2f42011-04-12 23:21:44 +0000697 }
698
699 // Get the offsets for the forwarding field and the variable field.
David Blaikie4adba522013-11-18 23:33:32 +0000700 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
701 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel161b2f42011-04-12 23:21:44 +0000702
703 // Decode the original location, and use that as the start of the byref
704 // variable's location.
Stephen Hines36b56882014-04-23 16:57:46 -0700705 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel161b2f42011-04-12 23:21:44 +0000706
Eric Christophercaba2632012-07-04 02:02:18 +0000707 if (Location.isReg())
Stephen Hines36b56882014-04-23 16:57:46 -0700708 addRegisterOp(Loc, Location.getReg());
Eric Christophercaba2632012-07-04 02:02:18 +0000709 else
Stephen Hines36b56882014-04-23 16:57:46 -0700710 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000711
712 // If we started with a pointer to the __Block_byref... struct, then
713 // the first thing we need to do is dereference the pointer (DW_OP_deref).
714 if (isPointer)
Stephen Hines36b56882014-04-23 16:57:46 -0700715 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000716
717 // Next add the offset for the '__forwarding' field:
718 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
719 // adding the offset if it's 0.
720 if (forwardingFieldOffset > 0) {
Stephen Hines36b56882014-04-23 16:57:46 -0700721 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
722 addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel161b2f42011-04-12 23:21:44 +0000723 }
724
725 // Now dereference the __forwarding field to get to the real __Block_byref
726 // struct: DW_OP_deref.
Stephen Hines36b56882014-04-23 16:57:46 -0700727 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000728
729 // Now that we've got the real __Block_byref... struct, add the offset
730 // for the variable's field to get to the location of the actual variable:
731 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
732 if (varFieldOffset > 0) {
Stephen Hines36b56882014-04-23 16:57:46 -0700733 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
734 addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel161b2f42011-04-12 23:21:44 +0000735 }
736
737 // Now attach the location information to the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700738 addBlock(Die, Attribute, Loc);
Devang Patel161b2f42011-04-12 23:21:44 +0000739}
740
Devang Patel4ec14b02011-07-20 21:57:04 +0000741/// isTypeSigned - Return true if the type is signed.
Manman Renc664d762013-10-05 01:43:03 +0000742static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patel4ec14b02011-07-20 21:57:04 +0000743 if (Ty.isDerivedType())
Manman Renc664d762013-10-05 01:43:03 +0000744 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
745 SizeInBits);
Devang Patel4ec14b02011-07-20 21:57:04 +0000746 if (Ty.isBasicType())
Eric Christopher6efc0432013-10-19 01:04:47 +0000747 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
748 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patel4ec14b02011-07-20 21:57:04 +0000749 *SizeInBits = Ty.getSizeInBits();
750 return true;
751 }
752 return false;
753}
754
Manman Renc664d762013-10-05 01:43:03 +0000755/// Return true if type encoding is unsigned.
756static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
757 DIDerivedType DTy(Ty);
758 if (DTy.isDerivedType())
759 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
760
761 DIBasicType BTy(Ty);
762 if (BTy.isBasicType()) {
763 unsigned Encoding = BTy.getEncoding();
764 if (Encoding == dwarf::DW_ATE_unsigned ||
765 Encoding == dwarf::DW_ATE_unsigned_char ||
766 Encoding == dwarf::DW_ATE_boolean)
767 return true;
768 }
769 return false;
770}
771
772/// If this type is derived from a base type then return base type size.
Manman Ren43251002013-10-08 18:46:58 +0000773static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renc664d762013-10-05 01:43:03 +0000774 unsigned Tag = Ty.getTag();
775
776 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
777 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
778 Tag != dwarf::DW_TAG_restrict_type)
779 return Ty.getSizeInBits();
780
781 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
782
Stephen Hines36b56882014-04-23 16:57:46 -0700783 // If this type is not derived from any type or the type is a declaration then
784 // take conservative approach.
785 if (!BaseType.isValid() || BaseType.isForwardDecl())
Manman Renc664d762013-10-05 01:43:03 +0000786 return Ty.getSizeInBits();
787
788 // If this is a derived type, go ahead and get the base type, unless it's a
789 // reference then it's just the size of the field. Pointer types have no need
790 // of this since they're a different type of qualification on the type.
791 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
792 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
793 return Ty.getSizeInBits();
794
795 if (BaseType.isDerivedType())
Manman Ren43251002013-10-08 18:46:58 +0000796 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renc664d762013-10-05 01:43:03 +0000797
798 return BaseType.getSizeInBits();
799}
800
Devang Patel161b2f42011-04-12 23:21:44 +0000801/// addConstantValue - Add constant value entry in variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700802void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
803 DIType Ty) {
David Blaikie4de9d722013-05-10 21:52:07 +0000804 // FIXME: This is a bit conservative/simple - it emits negative values at
805 // their maximum bit width which is a bit unfortunate (& doesn't prefer
806 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky746cb672011-10-26 22:55:33 +0000807 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel4ec14b02011-07-20 21:57:04 +0000808 int SizeInBits = -1;
Manman Renc664d762013-10-05 01:43:03 +0000809 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikie770530b2013-10-21 17:28:37 +0000810 dwarf::Form Form;
Devang Patel72f0d9c2011-05-27 18:15:52 +0000811
Eric Christopher7b2ee392013-08-27 23:49:04 +0000812 // If we're a signed constant definitely use sdata.
813 if (SignedConstant) {
814 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
815 return;
816 }
817
818 // Else use data for now unless it's larger than we can deal with.
819 switch (SizeInBits) {
820 case 8:
821 Form = dwarf::DW_FORM_data1;
822 break;
823 case 16:
824 Form = dwarf::DW_FORM_data2;
825 break;
826 case 32:
827 Form = dwarf::DW_FORM_data4;
828 break;
829 case 64:
830 Form = dwarf::DW_FORM_data8;
831 break;
832 default:
833 Form = dwarf::DW_FORM_udata;
834 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
835 return;
836 }
837 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel161b2f42011-04-12 23:21:44 +0000838}
839
840/// addConstantFPValue - Add constant value entry in variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700841void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopher6efc0432013-10-19 01:04:47 +0000842 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000843 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
844 APFloat FPImm = MO.getFPImm()->getValueAPF();
845
846 // Get the raw data form of the floating point.
847 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopher6efc0432013-10-19 01:04:47 +0000848 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000849
850 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000851 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000852 int Incr = (LittleEndian ? 1 : -1);
853 int Start = (LittleEndian ? 0 : NumBytes - 1);
854 int Stop = (LittleEndian ? NumBytes : -1);
855
856 // Output the constant to DWARF one byte at a time.
857 for (; Start != Stop; Start += Incr)
Eric Christopher61290022013-11-11 18:52:36 +0000858 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel161b2f42011-04-12 23:21:44 +0000859
David Blaikie770530b2013-10-21 17:28:37 +0000860 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel161b2f42011-04-12 23:21:44 +0000861}
862
David Blaikie14268412013-01-20 01:18:01 +0000863/// addConstantFPValue - Add constant value entry in variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700864void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher7b2ee392013-08-27 23:49:04 +0000865 // Pass this down to addConstantValue as an unsigned bag of bits.
866 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikie14268412013-01-20 01:18:01 +0000867}
868
Devang Patel161b2f42011-04-12 23:21:44 +0000869/// addConstantValue - Add constant value entry in variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700870void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
871 bool Unsigned) {
Eric Christophere4721492013-07-03 01:08:30 +0000872 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikie14268412013-01-20 01:18:01 +0000873}
874
875// addConstantValue - Add constant value entry in variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700876void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000877 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000878 if (CIBitWidth <= 64) {
Eric Christopher7b2ee392013-08-27 23:49:04 +0000879 // If we're a signed constant definitely use sdata.
880 if (!Unsigned) {
881 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
882 Val.getSExtValue());
883 return;
Devang Pateld6a81362011-05-28 00:39:18 +0000884 }
Eric Christopher7b2ee392013-08-27 23:49:04 +0000885
886 // Else use data for now unless it's larger than we can deal with.
David Blaikie770530b2013-10-21 17:28:37 +0000887 dwarf::Form Form;
Eric Christopher7b2ee392013-08-27 23:49:04 +0000888 switch (CIBitWidth) {
889 case 8:
890 Form = dwarf::DW_FORM_data1;
891 break;
892 case 16:
893 Form = dwarf::DW_FORM_data2;
894 break;
895 case 32:
896 Form = dwarf::DW_FORM_data4;
897 break;
898 case 64:
899 Form = dwarf::DW_FORM_data8;
900 break;
901 default:
902 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
903 Val.getZExtValue());
904 return;
905 }
906 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christophere4721492013-07-03 01:08:30 +0000907 return;
Devang Patel161b2f42011-04-12 23:21:44 +0000908 }
909
910 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
911
912 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000913 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000914
915 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000916 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000917
918 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000919 for (int i = 0; i < NumBytes; i++) {
920 uint8_t c;
921 if (LittleEndian)
922 c = Ptr64[i / 8] >> (8 * (i & 7));
923 else
924 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikie770530b2013-10-21 17:28:37 +0000925 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000926 }
Devang Patel161b2f42011-04-12 23:21:44 +0000927
David Blaikie770530b2013-10-21 17:28:37 +0000928 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel161b2f42011-04-12 23:21:44 +0000929}
930
Eric Christopher6c3bb942013-04-22 07:47:40 +0000931/// addTemplateParams - Add template parameters into buffer.
Stephen Hines36b56882014-04-23 16:57:46 -0700932void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel161b2f42011-04-12 23:21:44 +0000933 // Add template parameters.
934 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
935 DIDescriptor Element = TParams.getElement(i);
936 if (Element.isTemplateTypeParameter())
Manman Renbe87b692013-10-23 23:05:28 +0000937 constructTemplateTypeParameterDIE(Buffer,
938 DITemplateTypeParameter(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000939 else if (Element.isTemplateValueParameter())
Manman Renbe87b692013-10-23 23:05:28 +0000940 constructTemplateValueParameterDIE(Buffer,
941 DITemplateValueParameter(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000942 }
Devang Patel161b2f42011-04-12 23:21:44 +0000943}
Nick Lewycky746cb672011-10-26 22:55:33 +0000944
Eric Christopher6b6061f2013-01-16 01:22:23 +0000945/// getOrCreateContextDIE - Get context owner's DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700946DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikie8c0fecc2013-11-14 21:24:34 +0000947 if (!Context || Context.isFile())
Stephen Hines36b56882014-04-23 16:57:46 -0700948 return getUnitDie();
Eric Christopher6b6061f2013-01-16 01:22:23 +0000949 if (Context.isType())
950 return getOrCreateTypeDIE(DIType(Context));
David Blaikief4837be2013-11-14 19:37:56 +0000951 if (Context.isNameSpace())
Eric Christopher6b6061f2013-01-16 01:22:23 +0000952 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikief4837be2013-11-14 19:37:56 +0000953 if (Context.isSubprogram())
Eric Christopher6b6061f2013-01-16 01:22:23 +0000954 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl0cbdb812013-11-15 21:05:09 +0000955 return getDIE(Context);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000956}
957
Stephen Hines36b56882014-04-23 16:57:46 -0700958DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
959 DIScope Context = resolve(Ty.getContext());
960 DIE *ContextDIE = getOrCreateContextDIE(Context);
961
962 DIE *TyDIE = getDIE(Ty);
963 if (TyDIE)
964 return TyDIE;
965
966 // Create new type.
967 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
968
969 constructTypeDIE(*TyDIE, Ty);
970
971 updateAcceleratorTables(Context, Ty, TyDIE);
972 return TyDIE;
973}
974
Devang Patel161b2f42011-04-12 23:21:44 +0000975/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
976/// given DIType.
Stephen Hines36b56882014-04-23 16:57:46 -0700977DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie41e1a182013-11-14 22:25:02 +0000978 if (!TyNode)
Devang Patel94c7ddb2011-08-16 22:09:43 +0000979 return NULL;
Manman Rend498e5e2013-10-29 22:49:29 +0000980
David Blaikie41e1a182013-11-14 22:25:02 +0000981 DIType Ty(TyNode);
982 assert(Ty.isType());
Stephen Hines36b56882014-04-23 16:57:46 -0700983 assert(Ty == resolve(Ty.getRef()) &&
984 "type was not uniqued, possible ODR violation.");
David Blaikie41e1a182013-11-14 22:25:02 +0000985
Manman Rend498e5e2013-10-29 22:49:29 +0000986 // Construct the context before querying for the existence of the DIE in case
987 // such construction creates the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -0700988 DIScope Context = resolve(Ty.getContext());
989 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl6bc810a2013-11-15 23:21:39 +0000990 assert(ContextDIE);
Manman Rend498e5e2013-10-29 22:49:29 +0000991
Eric Christopher3f045002013-10-04 17:08:38 +0000992 DIE *TyDIE = getDIE(Ty);
Devang Patel161b2f42011-04-12 23:21:44 +0000993 if (TyDIE)
994 return TyDIE;
995
996 // Create new type.
Manman Rend498e5e2013-10-29 22:49:29 +0000997 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
998
Stephen Hines36b56882014-04-23 16:57:46 -0700999 updateAcceleratorTables(Context, Ty, TyDIE);
1000
Devang Patel161b2f42011-04-12 23:21:44 +00001001 if (Ty.isBasicType())
1002 constructTypeDIE(*TyDIE, DIBasicType(Ty));
Stephen Hines36b56882014-04-23 16:57:46 -07001003 else if (Ty.isCompositeType()) {
1004 DICompositeType CTy(Ty);
1005 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
1006 if (MDString *TypeId = CTy.getIdentifier()) {
1007 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
1008 // Skip updating the accelerator tables since this is not the full type.
1009 return TyDIE;
1010 }
1011 constructTypeDIE(*TyDIE, CTy);
1012 } else {
Devang Patel161b2f42011-04-12 23:21:44 +00001013 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1014 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1015 }
Stephen Hines36b56882014-04-23 16:57:46 -07001016
1017 return TyDIE;
1018}
1019
1020void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1021 const DIE *TyDIE) {
Eric Christopherc36145f2012-01-06 04:35:23 +00001022 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1023 bool IsImplementation = 0;
1024 if (Ty.isCompositeType()) {
1025 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +00001026 // A runtime language of 0 actually means C/C++ and that any
1027 // non-negative value is some version of Objective-C/C++.
Eric Christopher6efc0432013-10-19 01:04:47 +00001028 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +00001029 }
Eric Christopher577056f2013-09-05 18:20:16 +00001030 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christophere0167892012-01-06 23:03:37 +00001031 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopher8b4310b2012-11-21 00:34:38 +00001032
Stephen Hines36b56882014-04-23 16:57:46 -07001033 if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1034 Context.isNameSpace()) &&
1035 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
1036 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
1037 }
Devang Patel161b2f42011-04-12 23:21:44 +00001038}
1039
1040/// addType - Add a new type attribute to the specified entity.
Stephen Hines36b56882014-04-23 16:57:46 -07001041void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopherdc1363f2013-08-08 07:40:37 +00001042 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel161b2f42011-04-12 23:21:44 +00001043
1044 // Check for pre-existence.
1045 DIEEntry *Entry = getDIEEntry(Ty);
1046 // If it exists then use the existing value.
1047 if (Entry) {
Manman Renb8b70e12013-10-31 17:54:35 +00001048 addDIEEntry(Entity, Attribute, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +00001049 return;
1050 }
1051
1052 // Construct type.
1053 DIE *Buffer = getOrCreateTypeDIE(Ty);
1054
1055 // Set up proxy.
1056 Entry = createDIEEntry(Buffer);
1057 insertDIEEntry(Ty, Entry);
Manman Renb8b70e12013-10-31 17:54:35 +00001058 addDIEEntry(Entity, Attribute, Entry);
Devang Patel66658e42011-05-31 23:30:30 +00001059}
1060
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001061// Accelerator table mutators - add each name along with its companion
1062// DIE to the proper table while ensuring that the name that we're going
1063// to reference is in the string table. We do this since the names we
1064// add may not only be identical to the names in the DIE.
Stephen Hines36b56882014-04-23 16:57:46 -07001065void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
1066 if (!DD->useDwarfAccelTables())
1067 return;
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001068 DU->getStringPoolEntry(Name);
Stephen Hines36b56882014-04-23 16:57:46 -07001069 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001070 DIEs.push_back(Die);
1071}
1072
Stephen Hines36b56882014-04-23 16:57:46 -07001073void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
1074 if (!DD->useDwarfAccelTables())
1075 return;
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001076 DU->getStringPoolEntry(Name);
Stephen Hines36b56882014-04-23 16:57:46 -07001077 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001078 DIEs.push_back(Die);
1079}
1080
Stephen Hines36b56882014-04-23 16:57:46 -07001081void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
1082 if (!DD->useDwarfAccelTables())
1083 return;
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001084 DU->getStringPoolEntry(Name);
Stephen Hines36b56882014-04-23 16:57:46 -07001085 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001086 DIEs.push_back(Die);
1087}
1088
Stephen Hines36b56882014-04-23 16:57:46 -07001089void DwarfUnit::addAccelType(StringRef Name,
1090 std::pair<const DIE *, unsigned> Die) {
1091 if (!DD->useDwarfAccelTables())
1092 return;
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001093 DU->getStringPoolEntry(Name);
Stephen Hines36b56882014-04-23 16:57:46 -07001094 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001095 DIEs.push_back(Die);
1096}
1097
Eric Christopher50d37a42013-09-20 22:20:55 +00001098/// addGlobalName - Add a new global name to the compile unit.
Stephen Hines36b56882014-04-23 16:57:46 -07001099void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
1100 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1101 return;
Eric Christopher0ab64392013-10-19 01:04:42 +00001102 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher91986572013-10-17 02:06:06 +00001103 GlobalNames[FullName] = Die;
Eric Christopher50d37a42013-09-20 22:20:55 +00001104}
1105
Eric Christopher91986572013-10-17 02:06:06 +00001106/// getParentContextString - Walks the metadata parent chain in a language
1107/// specific manner (using the compile unit language) and returns
1108/// it as a string. This is done at the metadata level because DIEs may
1109/// not currently have been added to the parent context and walking the
1110/// DIEs looking for names is more expensive than walking the metadata.
Stephen Hines36b56882014-04-23 16:57:46 -07001111std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher91986572013-10-17 02:06:06 +00001112 if (!Context)
1113 return "";
1114
1115 // FIXME: Decide whether to implement this for non-C++ languages.
1116 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1117 return "";
1118
Eric Christopher0ab64392013-10-19 01:04:42 +00001119 std::string CS;
Eric Christopher91986572013-10-17 02:06:06 +00001120 SmallVector<DIScope, 1> Parents;
1121 while (!Context.isCompileUnit()) {
1122 Parents.push_back(Context);
1123 if (Context.getContext())
1124 Context = resolve(Context.getContext());
1125 else
1126 // Structure, etc types will have a NULL context if they're at the top
1127 // level.
1128 break;
1129 }
1130
1131 // Reverse iterate over our list to go from the outermost construct to the
1132 // innermost.
1133 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1134 E = Parents.rend();
1135 I != E; ++I) {
1136 DIScope Ctx = *I;
1137 StringRef Name = Ctx.getName();
Eric Christopher0ab64392013-10-19 01:04:42 +00001138 if (!Name.empty()) {
Eric Christopher91986572013-10-17 02:06:06 +00001139 CS += Name;
1140 CS += "::";
1141 }
1142 }
1143 return CS;
Devang Patel161b2f42011-04-12 23:21:44 +00001144}
1145
1146/// constructTypeDIE - Construct basic type die from DIBasicType.
Stephen Hines36b56882014-04-23 16:57:46 -07001147void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001148 // Get core information.
1149 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +00001150 // Add name if not anonymous or intermediate type.
1151 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001152 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +00001153
David Blaikie916d49e2013-10-04 23:21:16 +00001154 // An unspecified type only has a name attribute.
1155 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel734a67c2011-09-14 23:13:28 +00001156 return;
Devang Patel734a67c2011-09-14 23:13:28 +00001157
Nick Lewycky746cb672011-10-26 22:55:33 +00001158 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +00001159 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +00001160
Devang Patel161b2f42011-04-12 23:21:44 +00001161 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikie770530b2013-10-21 17:28:37 +00001162 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel161b2f42011-04-12 23:21:44 +00001163}
1164
1165/// constructTypeDIE - Construct derived type die from DIDerivedType.
Stephen Hines36b56882014-04-23 16:57:46 -07001166void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001167 // Get core information.
1168 StringRef Name = DTy.getName();
1169 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikie916d49e2013-10-04 23:21:16 +00001170 uint16_t Tag = Buffer.getTag();
Devang Patel161b2f42011-04-12 23:21:44 +00001171
1172 // Map to main type, void will not have a type.
Manman Ren017ceda2013-10-08 18:42:58 +00001173 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopherdc1363f2013-08-08 07:40:37 +00001174 if (FromTy)
1175 addType(&Buffer, FromTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001176
1177 // Add name if not anonymous or intermediate type.
1178 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001179 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001180
1181 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +00001182 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikie770530b2013-10-21 17:28:37 +00001183 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel161b2f42011-04-12 23:21:44 +00001184
David Blaikie62fdfb52013-01-07 05:51:15 +00001185 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopher6efc0432013-10-19 01:04:47 +00001186 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1187 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel161b2f42011-04-12 23:21:44 +00001188 // Add source line info if available and TyDesc is not a forward declaration.
1189 if (!DTy.isForwardDecl())
1190 addSourceLine(&Buffer, DTy);
1191}
1192
Stephen Hines36b56882014-04-23 16:57:46 -07001193/// constructSubprogramArguments - Construct function argument DIEs.
1194void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
1195 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1196 DIDescriptor Ty = Args.getElement(i);
1197 if (Ty.isUnspecifiedParameter()) {
1198 assert(i == N-1 && "Unspecified parameter must be the last argument");
1199 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1200 } else {
1201 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1202 addType(Arg, DIType(Ty));
1203 if (DIType(Ty).isArtificial())
1204 addFlag(Arg, dwarf::DW_AT_artificial);
1205 }
Eric Christopher3dee5752013-07-26 17:02:41 +00001206 }
1207}
1208
Devang Patel161b2f42011-04-12 23:21:44 +00001209/// constructTypeDIE - Construct type DIE from DICompositeType.
Stephen Hines36b56882014-04-23 16:57:46 -07001210void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1211 // Add name if not anonymous or intermediate type.
Devang Patel161b2f42011-04-12 23:21:44 +00001212 StringRef Name = CTy.getName();
1213
1214 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikie916d49e2013-10-04 23:21:16 +00001215 uint16_t Tag = Buffer.getTag();
Devang Patel161b2f42011-04-12 23:21:44 +00001216
1217 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +00001218 case dwarf::DW_TAG_array_type:
Eric Christopher883ed6b2013-11-11 18:52:31 +00001219 constructArrayTypeDIE(Buffer, CTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001220 break;
Eric Christopher95339892013-11-11 18:52:39 +00001221 case dwarf::DW_TAG_enumeration_type:
1222 constructEnumTypeDIE(Buffer, CTy);
1223 break;
Devang Patel161b2f42011-04-12 23:21:44 +00001224 case dwarf::DW_TAG_subroutine_type: {
Eric Christopherdc1363f2013-08-08 07:40:37 +00001225 // Add return type. A void return won't have a type.
Devang Patel161b2f42011-04-12 23:21:44 +00001226 DIArray Elements = CTy.getTypeArray();
David Blaikie4adba522013-11-18 23:33:32 +00001227 DIType RTy(Elements.getElement(0));
Eric Christopherdc1363f2013-08-08 07:40:37 +00001228 if (RTy)
David Blaikie4adba522013-11-18 23:33:32 +00001229 addType(&Buffer, RTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001230
1231 bool isPrototyped = true;
Stephen Hines36b56882014-04-23 16:57:46 -07001232 if (Elements.getNumElements() == 2 &&
1233 Elements.getElement(1).isUnspecifiedParameter())
1234 isPrototyped = false;
1235
1236 constructSubprogramArguments(Buffer, Elements);
1237
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001238 // Add prototype flag if we're dealing with a C language and the
1239 // function has been prototyped.
David Blaikieaedaa722013-11-15 23:50:53 +00001240 uint16_t Language = getLanguage();
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001241 if (isPrototyped &&
Eric Christopher6efc0432013-10-19 01:04:47 +00001242 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher4d069bf2012-05-22 18:45:24 +00001243 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001244 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Stephen Hines36b56882014-04-23 16:57:46 -07001245
1246 if (CTy.isLValueReference())
1247 addFlag(&Buffer, dwarf::DW_AT_reference);
1248
1249 if (CTy.isRValueReference())
1250 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopher6efc0432013-10-19 01:04:47 +00001251 } break;
Devang Patel161b2f42011-04-12 23:21:44 +00001252 case dwarf::DW_TAG_structure_type:
1253 case dwarf::DW_TAG_union_type:
1254 case dwarf::DW_TAG_class_type: {
Devang Patel161b2f42011-04-12 23:21:44 +00001255 // Add elements to structure type.
David Blaikiec8b93552013-08-01 20:30:22 +00001256 DIArray Elements = CTy.getTypeArray();
1257 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +00001258 DIDescriptor Element = Elements.getElement(i);
1259 DIE *ElemDie = NULL;
Stephen Hines36b56882014-04-23 16:57:46 -07001260 if (Element.isSubprogram())
1261 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1262 else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +00001263 DIDerivedType DDTy(Element);
1264 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Ren1a5e7872013-10-29 00:53:03 +00001265 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren017ceda2013-10-08 18:42:58 +00001266 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renc664d762013-10-05 01:43:03 +00001267 dwarf::DW_AT_friend);
Manman Ren655a10d2013-10-14 20:33:57 +00001268 } else if (DDTy.isStaticMember()) {
Manman Ren99addee2013-10-23 22:57:12 +00001269 getOrCreateStaticMemberDIE(DDTy);
Manman Ren655a10d2013-10-14 20:33:57 +00001270 } else {
Manman Rena1d25b62013-10-23 23:00:44 +00001271 constructMemberDIE(Buffer, DDTy);
Manman Ren655a10d2013-10-14 20:33:57 +00001272 }
Eric Christopher663e0cf2012-03-28 07:34:31 +00001273 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +00001274 DIObjCProperty Property(Element);
Manman Ren1a5e7872013-10-29 00:53:03 +00001275 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Patel30d409c2012-02-07 23:33:58 +00001276 StringRef PropertyName = Property.getObjCPropertyName();
1277 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Stephen Hines36b56882014-04-23 16:57:46 -07001278 if (Property.getType())
1279 addType(ElemDie, Property.getType());
Eric Christopher4d069bf2012-05-22 18:45:24 +00001280 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +00001281 StringRef GetterName = Property.getObjCPropertyGetterName();
1282 if (!GetterName.empty())
1283 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1284 StringRef SetterName = Property.getObjCPropertySetterName();
1285 if (!SetterName.empty())
1286 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1287 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +00001288 if (Property.isReadOnlyObjCProperty())
1289 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1290 if (Property.isReadWriteObjCProperty())
1291 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1292 if (Property.isAssignObjCProperty())
1293 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1294 if (Property.isRetainObjCProperty())
1295 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1296 if (Property.isCopyObjCProperty())
1297 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1298 if (Property.isNonAtomicObjCProperty())
1299 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1300 if (PropertyAttributes)
David Blaikie770530b2013-10-21 17:28:37 +00001301 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopher6efc0432013-10-19 01:04:47 +00001302 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001303
Devang Patel30d409c2012-02-07 23:33:58 +00001304 DIEEntry *Entry = getDIEEntry(Element);
1305 if (!Entry) {
1306 Entry = createDIEEntry(ElemDie);
1307 insertDIEEntry(Element, Entry);
1308 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001309 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001310 continue;
Devang Patel161b2f42011-04-12 23:21:44 +00001311 }
1312
1313 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001314 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001315
Eric Christophereee74fb2013-10-05 00:27:02 +00001316 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4adba522013-11-18 23:33:32 +00001317 if (ContainingType)
Manman Ren87b110a2013-10-11 23:58:05 +00001318 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4adba522013-11-18 23:33:32 +00001319 getOrCreateTypeDIE(ContainingType));
Devang Patel161b2f42011-04-12 23:21:44 +00001320
Devang Patel201e6cd2011-05-12 21:29:42 +00001321 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001322 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001323
Eric Christopher1a8e8862011-12-16 23:42:42 +00001324 // Add template parameters to a class, structure or union types.
1325 // FIXME: The support isn't in the metadata for this yet.
1326 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopher6efc0432013-10-19 01:04:47 +00001327 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001328 addTemplateParams(Buffer, CTy.getTemplateParams());
1329
1330 break;
1331 }
1332 default:
1333 break;
1334 }
1335
1336 // Add name if not anonymous or intermediate type.
1337 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001338 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001339
Eric Christopher4a5d8392012-05-22 18:45:18 +00001340 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopher6efc0432013-10-19 01:04:47 +00001341 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher4a5d8392012-05-22 18:45:18 +00001342 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001343 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001344 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001345 if (Size)
David Blaikie770530b2013-10-21 17:28:37 +00001346 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001347 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001348 // Add zero size if it is not a forward declaration.
David Blaikie770530b2013-10-21 17:28:37 +00001349 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001350
1351 // If we're a forward decl, say so.
1352 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001353 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001354
1355 // Add source line info if available.
1356 if (!CTy.isForwardDecl())
1357 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001358
1359 // No harm in adding the runtime language to the declaration.
1360 unsigned RLang = CTy.getRunTimeLang();
1361 if (RLang)
Eric Christopher6efc0432013-10-19 01:04:47 +00001362 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1363 RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001364 }
1365}
1366
Manman Renbe87b692013-10-23 23:05:28 +00001367/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1368/// DITemplateTypeParameter.
Stephen Hines36b56882014-04-23 16:57:46 -07001369void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1370 DITemplateTypeParameter TP) {
Manman Ren1a5e7872013-10-29 00:53:03 +00001371 DIE *ParamDIE =
1372 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher76ef79f2013-08-08 08:09:43 +00001373 // Add the type if it exists, it could be void and therefore no type.
1374 if (TP.getType())
Manman Renb4d9c112013-10-09 19:46:28 +00001375 addType(ParamDIE, resolve(TP.getType()));
David Blaikiee88939c2013-06-22 18:59:11 +00001376 if (!TP.getName().empty())
1377 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001378}
1379
Manman Renbe87b692013-10-23 23:05:28 +00001380/// constructTemplateValueParameterDIE - Construct new DIE for the given
1381/// DITemplateValueParameter.
Manman Ren99addee2013-10-23 22:57:12 +00001382void
Stephen Hines36b56882014-04-23 16:57:46 -07001383DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1384 DITemplateValueParameter VP) {
Manman Ren1a5e7872013-10-29 00:53:03 +00001385 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopherdc1363f2013-08-08 07:40:37 +00001386
1387 // Add the type if there is one, template template and template parameter
1388 // packs will not have a type.
Eric Christopheref2d9192013-10-21 17:48:51 +00001389 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Renb4d9c112013-10-09 19:46:28 +00001390 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafdd1f82013-08-08 07:40:31 +00001391 if (!VP.getName().empty())
1392 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1393 if (Value *Val = VP.getValue()) {
David Blaikie4de9d722013-05-10 21:52:07 +00001394 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Renb4d9c112013-10-09 19:46:28 +00001395 addConstantValue(ParamDIE, CI,
1396 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikie4de9d722013-05-10 21:52:07 +00001397 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1398 // For declaration non-type template parameters (such as global values and
1399 // functions)
Stephen Hines36b56882014-04-23 16:57:46 -07001400 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1401 addOpAddress(Loc, Asm->getSymbol(GV));
David Blaikie4de9d722013-05-10 21:52:07 +00001402 // Emit DW_OP_stack_value to use the address as the immediate value of the
1403 // parameter, rather than a pointer to it.
Stephen Hines36b56882014-04-23 16:57:46 -07001404 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1405 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafdd1f82013-08-08 07:40:31 +00001406 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikiee88939c2013-06-22 18:59:11 +00001407 assert(isa<MDString>(Val));
1408 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1409 cast<MDString>(Val)->getString());
Eric Christopherafdd1f82013-08-08 07:40:31 +00001410 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikiee88939c2013-06-22 18:59:11 +00001411 assert(isa<MDNode>(Val));
1412 DIArray A(cast<MDNode>(Val));
1413 addTemplateParams(*ParamDIE, A);
David Blaikie4de9d722013-05-10 21:52:07 +00001414 }
1415 }
Devang Patel161b2f42011-04-12 23:21:44 +00001416}
1417
Devang Patel31c5d052011-05-06 16:57:54 +00001418/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Stephen Hines36b56882014-04-23 16:57:46 -07001419DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Ren6b713802013-10-29 05:49:41 +00001420 // Construct the context before querying for the existence of the DIE in case
1421 // such construction creates the DIE.
1422 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Ren6b713802013-10-29 05:49:41 +00001423
Devang Patel31c5d052011-05-06 16:57:54 +00001424 DIE *NDie = getDIE(NS);
1425 if (NDie)
1426 return NDie;
Manman Ren6b713802013-10-29 05:49:41 +00001427 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1428
Eric Christopher09ac3d82011-11-07 09:24:32 +00001429 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001430 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001431 addAccelNamespace(NS.getName(), NDie);
Eric Christopher91986572013-10-17 02:06:06 +00001432 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001433 } else
1434 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001435 addSourceLine(NDie, NS);
Devang Patel31c5d052011-05-06 16:57:54 +00001436 return NDie;
1437}
1438
Devang Pateldbc64af2011-08-15 17:24:54 +00001439/// getOrCreateSubprogramDIE - Create new DIE using SP.
Stephen Hines36b56882014-04-23 16:57:46 -07001440DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikiec32f2332013-10-04 01:39:59 +00001441 // Construct the context before querying for the existence of the DIE in case
1442 // such construction creates the DIE (as is the case for member function
1443 // declarations).
Stephen Hines36b56882014-04-23 16:57:46 -07001444 DIScope Context = resolve(SP.getContext());
1445 DIE *ContextDIE = getOrCreateContextDIE(Context);
1446
1447 // Unique declarations based on the ODR, where applicable.
1448 SP = DISubprogram(DD->resolve(SP.getRef()));
1449 assert(SP.Verify());
David Blaikiec32f2332013-10-04 01:39:59 +00001450
Eric Christopher3f045002013-10-04 17:08:38 +00001451 DIE *SPDie = getDIE(SP);
Devang Pateldbc64af2011-08-15 17:24:54 +00001452 if (SPDie)
1453 return SPDie;
1454
Manman Renbbdd02c2013-10-29 00:58:04 +00001455 DISubprogram SPDecl = SP.getFunctionDeclaration();
1456 if (SPDecl.isSubprogram())
1457 // Add subprogram definitions to the CU die directly.
Stephen Hines36b56882014-04-23 16:57:46 -07001458 ContextDIE = UnitDie.get();
Peter Collingbourne27302f02012-05-27 18:36:44 +00001459
1460 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Renbbdd02c2013-10-29 00:58:04 +00001461 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne27302f02012-05-27 18:36:44 +00001462
Rafael Espindola01b55b42011-11-10 22:34:29 +00001463 DIE *DeclDie = NULL;
Manman Ren250f4e82013-10-23 22:12:26 +00001464 if (SPDecl.isSubprogram())
Rafael Espindola01b55b42011-11-10 22:34:29 +00001465 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola01b55b42011-11-10 22:34:29 +00001466
Devang Pateldbc64af2011-08-15 17:24:54 +00001467 // Add function template parameters.
1468 addTemplateParams(*SPDie, SP.getTemplateParams());
1469
Devang Pateldbc64af2011-08-15 17:24:54 +00001470 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherc4968752013-05-09 00:42:33 +00001471 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001472 if (DeclDie) {
1473 // Refer function declaration directly.
Manman Ren87b110a2013-10-11 23:58:05 +00001474 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola01b55b42011-11-10 22:34:29 +00001475
Devang Pateldbc64af2011-08-15 17:24:54 +00001476 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001477 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001478
Eric Christophercbbd5b12012-08-23 22:52:55 +00001479 // Add the linkage name if we have one.
Michael Gottesmandc42d032013-09-04 04:39:38 +00001480 StringRef LinkageName = SP.getLinkageName();
1481 if (!LinkageName.empty())
Eric Christophercbbd5b12012-08-23 22:52:55 +00001482 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001483 GlobalValue::getRealLinkageName(LinkageName));
Eric Christophercbbd5b12012-08-23 22:52:55 +00001484
Devang Pateldbc64af2011-08-15 17:24:54 +00001485 // Constructors and operators for anonymous aggregates do not have names.
1486 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001487 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001488
1489 addSourceLine(SPDie, SP);
1490
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001491 // Add the prototype if we have a prototype and we have a C like
1492 // language.
David Blaikieaedaa722013-11-15 23:50:53 +00001493 uint16_t Language = getLanguage();
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001494 if (SP.isPrototyped() &&
Eric Christopher6efc0432013-10-19 01:04:47 +00001495 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001496 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001497 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001498
Devang Pateldbc64af2011-08-15 17:24:54 +00001499 DICompositeType SPTy = SP.getType();
David Blaikie3d331842013-05-22 23:22:18 +00001500 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1501 "the type of a subprogram should be a subroutine");
Devang Pateldbc64af2011-08-15 17:24:54 +00001502
David Blaikie3d331842013-05-22 23:22:18 +00001503 DIArray Args = SPTy.getTypeArray();
Eric Christopheref2d9192013-10-21 17:48:51 +00001504 // Add a return type. If this is a type like a C/C++ void type we don't add a
1505 // return type.
Eric Christopherdc1363f2013-08-08 07:40:37 +00001506 if (Args.getElement(0))
1507 addType(SPDie, DIType(Args.getElement(0)));
Devang Pateldbc64af2011-08-15 17:24:54 +00001508
1509 unsigned VK = SP.getVirtuality();
1510 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001511 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Stephen Hines36b56882014-04-23 16:57:46 -07001512 DIELoc *Block = getDIELoc();
David Blaikie770530b2013-10-21 17:28:37 +00001513 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1514 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1515 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher61290022013-11-11 18:52:36 +00001516 ContainingTypeMap.insert(
1517 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Pateldbc64af2011-08-15 17:24:54 +00001518 }
1519
1520 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001521 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001522
Devang Pateldbc64af2011-08-15 17:24:54 +00001523 // Add arguments. Do not add arguments for subprogram definition. They will
1524 // be handled while processing variables.
Stephen Hines36b56882014-04-23 16:57:46 -07001525 constructSubprogramArguments(*SPDie, Args);
Devang Pateldbc64af2011-08-15 17:24:54 +00001526 }
1527
1528 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001529 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001530
1531 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001532 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001533
1534 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001535 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001536
1537 if (unsigned isa = Asm->getISAEncoding()) {
1538 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1539 }
1540
Stephen Hines36b56882014-04-23 16:57:46 -07001541 if (SP.isLValueReference())
1542 addFlag(SPDie, dwarf::DW_AT_reference);
1543
1544 if (SP.isRValueReference())
1545 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1546
1547 if (SP.isProtected())
1548 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1549 dwarf::DW_ACCESS_protected);
1550 else if (SP.isPrivate())
1551 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1552 dwarf::DW_ACCESS_private);
1553 else
1554 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1555 dwarf::DW_ACCESS_public);
1556
1557 if (SP.isExplicit())
1558 addFlag(SPDie, dwarf::DW_AT_explicit);
1559
Devang Pateldbc64af2011-08-15 17:24:54 +00001560 return SPDie;
1561}
1562
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001563// Return const expression if value is a GEP to access merged global
1564// constant. e.g.
1565// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1566static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1567 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1568 if (!CE || CE->getNumOperands() != 3 ||
1569 CE->getOpcode() != Instruction::GetElementPtr)
1570 return NULL;
1571
1572 // First operand points to a global struct.
1573 Value *Ptr = CE->getOperand(0);
1574 if (!isa<GlobalValue>(Ptr) ||
1575 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1576 return NULL;
1577
1578 // Second operand is zero.
1579 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1580 if (!CI || !CI->isZero())
1581 return NULL;
1582
1583 // Third operand is offset.
1584 if (!isa<ConstantInt>(CE->getOperand(2)))
1585 return NULL;
1586
1587 return CE;
1588}
1589
1590/// createGlobalVariableDIE - create global variable DIE.
Stephen Hines36b56882014-04-23 16:57:46 -07001591void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001592 // Check for pre-existence.
David Blaikiecbc85a22013-11-15 23:09:13 +00001593 if (getDIE(GV))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001594 return;
1595
Stephen Hines36b56882014-04-23 16:57:46 -07001596 assert(GV.isGlobalVariable());
Devang Patel28bea082011-08-18 23:17:55 +00001597
Eric Christopherccb66362013-10-04 23:49:26 +00001598 DIScope GVContext = GV.getContext();
Stephen Hines36b56882014-04-23 16:57:46 -07001599 DIType GTy = DD->resolve(GV.getType());
Eric Christopher6b6061f2013-01-16 01:22:23 +00001600
1601 // If this is a static data member definition, some attributes belong
1602 // to the declaration DIE.
1603 DIE *VariableDIE = NULL;
Manman Ren945e8282013-02-01 23:54:37 +00001604 bool IsStaticMember = false;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001605 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1606 if (SDMDecl.Verify()) {
1607 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1608 // We need the declaration DIE that is in the static member's class.
Manman Ren655a10d2013-10-14 20:33:57 +00001609 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Ren945e8282013-02-01 23:54:37 +00001610 IsStaticMember = true;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001611 }
1612
1613 // If this is not a static data member definition, create the variable
1614 // DIE and add the initial set of attributes to it.
1615 if (!VariableDIE) {
Manman Ren6b713802013-10-29 05:49:41 +00001616 // Construct the context before querying for the existence of the DIE in
1617 // case such construction creates the DIE.
1618 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Ren6b713802013-10-29 05:49:41 +00001619
Eric Christopher6b6061f2013-01-16 01:22:23 +00001620 // Add to map.
David Blaikie1dc27232013-11-16 00:29:01 +00001621 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001622
1623 // Add name and type.
1624 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1625 addType(VariableDIE, GTy);
1626
1627 // Add scoping info.
Eric Christophera486f552013-10-16 01:37:49 +00001628 if (!GV.isLocalToUnit())
Eric Christopher6b6061f2013-01-16 01:22:23 +00001629 addFlag(VariableDIE, dwarf::DW_AT_external);
1630
1631 // Add line number info.
1632 addSourceLine(VariableDIE, GV);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001633 }
1634
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001635 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001636 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001637 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001638 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001639 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001640 addToAccelTable = true;
Stephen Hines36b56882014-04-23 16:57:46 -07001641 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindolaffc7dca2013-10-29 17:07:16 +00001642 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikie59eaa382013-06-28 20:05:11 +00001643 if (GV.getGlobal()->isThreadLocal()) {
1644 // FIXME: Make this work with -gsplit-dwarf.
1645 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1646 assert((PointerSize == 4 || PointerSize == 8) &&
1647 "Add support for other sizes if necessary");
1648 // Based on GCC's support for TLS:
David Blaikie8fed05e2013-07-01 23:55:52 +00001649 if (!DD->useSplitDwarf()) {
1650 // 1) Start with a constNu of the appropriate pointer size
Stephen Hines36b56882014-04-23 16:57:46 -07001651 addUInt(Loc, dwarf::DW_FORM_data1,
David Blaikie8fed05e2013-07-01 23:55:52 +00001652 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton379f76e2013-10-07 18:39:18 +00001653 // 2) containing the (relocated) offset of the TLS variable
1654 // within the module's TLS block.
Stephen Hines36b56882014-04-23 16:57:46 -07001655 addExpr(Loc, dwarf::DW_FORM_udata,
1656 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8fed05e2013-07-01 23:55:52 +00001657 } else {
Stephen Hines36b56882014-04-23 16:57:46 -07001658 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1659 addUInt(Loc, dwarf::DW_FORM_udata,
1660 DU->getAddrPoolIndex(Sym, /* TLS */ true));
David Blaikie8fed05e2013-07-01 23:55:52 +00001661 }
Richard Mitton379f76e2013-10-07 18:39:18 +00001662 // 3) followed by a custom OP to make the debugger do a TLS lookup.
Stephen Hines36b56882014-04-23 16:57:46 -07001663 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
1664 } else {
1665 DD->addArangeLabel(SymbolCU(this, Sym));
1666 addOpAddress(Loc, Sym);
1667 }
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001668 // Do not create specification DIE if context is either compile unit
1669 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001670 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren02d29672013-09-09 19:05:21 +00001671 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001672 // Create specification DIE.
Stephen Hines36b56882014-04-23 16:57:46 -07001673 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren87b110a2013-10-11 23:58:05 +00001674 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
Stephen Hines36b56882014-04-23 16:57:46 -07001675 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001676 // A static member's declaration is already flagged as such.
1677 if (!SDMDecl.Verify())
1678 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001679 } else {
Stephen Hines36b56882014-04-23 16:57:46 -07001680 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001681 }
Michael Gottesmandc42d032013-09-04 04:39:38 +00001682 // Add the linkage name.
Eric Christopher6b6061f2013-01-16 01:22:23 +00001683 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmandc42d032013-09-04 04:39:38 +00001684 if (!LinkageName.empty())
Eric Christopher8d45a982013-02-27 23:49:47 +00001685 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1686 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1687 // TAG_variable.
Eric Christopher6efc0432013-10-19 01:04:47 +00001688 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1689 : VariableDIE,
Stephen Hines36b56882014-04-23 16:57:46 -07001690 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1691 : dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c2b4be2013-06-01 17:51:14 +00001692 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher8b4310b2012-11-21 00:34:38 +00001693 } else if (const ConstantInt *CI =
Eric Christopher6efc0432013-10-19 01:04:47 +00001694 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001695 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001696 // emitting AT_const_value multiple times, we only add AT_const_value when
1697 // it is not a static member.
1698 if (!IsStaticMember)
Manman Renc664d762013-10-05 01:43:03 +00001699 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikie08e51e12013-11-17 21:55:13 +00001700 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001701 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001702 // GV is a merged global.
Stephen Hines36b56882014-04-23 16:57:46 -07001703 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001704 Value *Ptr = CE->getOperand(0);
Stephen Hines36b56882014-04-23 16:57:46 -07001705 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1706 DD->addArangeLabel(SymbolCU(this, Sym));
1707 addOpAddress(Loc, Sym);
1708 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopher6efc0432013-10-19 01:04:47 +00001709 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
Stephen Hines36b56882014-04-23 16:57:46 -07001710 addUInt(Loc, dwarf::DW_FORM_udata,
Eric Christopher6efc0432013-10-19 01:04:47 +00001711 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Stephen Hines36b56882014-04-23 16:57:46 -07001712 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1713 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001714 }
1715
Eric Christopherd117fbb2011-11-11 01:55:22 +00001716 if (addToAccelTable) {
1717 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1718 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001719
Eric Christopherd117fbb2011-11-11 01:55:22 +00001720 // If the linkage name is different than the name, go ahead and output
1721 // that as well into the name table.
1722 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1723 addAccelName(GV.getLinkageName(), AddrDIE);
1724 }
Eric Christophera486f552013-10-16 01:37:49 +00001725
1726 if (!GV.isLocalToUnit())
Eric Christopher91986572013-10-17 02:06:06 +00001727 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1728 GV.getContext());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001729}
1730
Devang Patel161b2f42011-04-12 23:21:44 +00001731/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Stephen Hines36b56882014-04-23 16:57:46 -07001732void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Ren1a5e7872013-10-29 00:53:03 +00001733 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren87b110a2013-10-11 23:58:05 +00001734 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001735
Bill Wendling222c2fd2012-12-06 07:38:10 +00001736 // The LowerBound value defines the lower bounds which is typically zero for
1737 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1738 // Count == -1 then the array is unbounded and we do not emit
1739 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1740 // Count == 0, then the array has zero elements in which case we do not emit
1741 // an upper bound.
1742 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001743 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001744 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001745
Bill Wendling6afe4782012-12-06 07:55:19 +00001746 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikie770530b2013-10-21 17:28:37 +00001747 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling222c2fd2012-12-06 07:38:10 +00001748
1749 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001750 // FIXME: An unbounded array should reference the expression that defines
1751 // the array.
Eric Christopher61290022013-11-11 18:52:36 +00001752 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1753 LowerBound + Count - 1);
Devang Patel161b2f42011-04-12 23:21:44 +00001754}
1755
1756/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Stephen Hines36b56882014-04-23 16:57:46 -07001757void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopher883ed6b2013-11-11 18:52:31 +00001758 if (CTy.isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001759 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001760
Eric Christopherdc1363f2013-08-08 07:40:37 +00001761 // Emit the element type.
Eric Christopher883ed6b2013-11-11 18:52:31 +00001762 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel161b2f42011-04-12 23:21:44 +00001763
1764 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001765 // FIXME: This type should be passed down from the front end
1766 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001767 DIE *IdxTy = getIndexTyDie();
1768 if (!IdxTy) {
Stephen Hines36b56882014-04-23 16:57:46 -07001769 // Construct an integer type to use for indexes.
1770 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
1771 addString(IdxTy, dwarf::DW_AT_name, "sizetype");
1772 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
Devang Patel161b2f42011-04-12 23:21:44 +00001773 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Stephen Hines36b56882014-04-23 16:57:46 -07001774 dwarf::DW_ATE_unsigned);
Devang Patel161b2f42011-04-12 23:21:44 +00001775 setIndexTyDie(IdxTy);
1776 }
1777
1778 // Add subranges to array type.
Eric Christopher883ed6b2013-11-11 18:52:31 +00001779 DIArray Elements = CTy.getTypeArray();
Devang Patel161b2f42011-04-12 23:21:44 +00001780 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1781 DIDescriptor Element = Elements.getElement(i);
1782 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1783 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1784 }
1785}
1786
Eric Christopher95339892013-11-11 18:52:39 +00001787/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Stephen Hines36b56882014-04-23 16:57:46 -07001788void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopher95339892013-11-11 18:52:39 +00001789 DIArray Elements = CTy.getTypeArray();
1790
1791 // Add enumerators to enumeration type.
1792 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4adba522013-11-18 23:33:32 +00001793 DIEnumerator Enum(Elements.getElement(i));
Eric Christopher95339892013-11-11 18:52:39 +00001794 if (Enum.isEnumerator()) {
1795 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4adba522013-11-18 23:33:32 +00001796 StringRef Name = Enum.getName();
Eric Christopher95339892013-11-11 18:52:39 +00001797 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4adba522013-11-18 23:33:32 +00001798 int64_t Value = Enum.getEnumValue();
Stephen Hines36b56882014-04-23 16:57:46 -07001799 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1800 Value);
Eric Christopher95339892013-11-11 18:52:39 +00001801 }
1802 }
1803 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1804 if (DTy) {
1805 addType(&Buffer, DTy);
1806 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1807 }
Devang Patel161b2f42011-04-12 23:21:44 +00001808}
1809
Devang Pateldbc64af2011-08-15 17:24:54 +00001810/// constructContainingTypeDIEs - Construct DIEs for types that contain
1811/// vtables.
Stephen Hines36b56882014-04-23 16:57:46 -07001812void DwarfUnit::constructContainingTypeDIEs() {
Devang Pateldbc64af2011-08-15 17:24:54 +00001813 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopher6efc0432013-10-19 01:04:47 +00001814 CE = ContainingTypeMap.end();
1815 CI != CE; ++CI) {
Devang Pateldbc64af2011-08-15 17:24:54 +00001816 DIE *SPDie = CI->first;
David Blaikiecbc85a22013-11-15 23:09:13 +00001817 DIDescriptor D(CI->second);
1818 if (!D)
Eric Christopher6efc0432013-10-19 01:04:47 +00001819 continue;
David Blaikiecbc85a22013-11-15 23:09:13 +00001820 DIE *NDie = getDIE(D);
Eric Christopher6efc0432013-10-19 01:04:47 +00001821 if (!NDie)
1822 continue;
Manman Ren87b110a2013-10-11 23:58:05 +00001823 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Pateldbc64af2011-08-15 17:24:54 +00001824 }
1825}
1826
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001827/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Stephen Hines36b56882014-04-23 16:57:46 -07001828DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher7b641812013-11-15 01:43:19 +00001829 StringRef Name = DV.getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001830
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001831 // Define variable debug information entry.
Eric Christopher7b641812013-11-15 01:43:19 +00001832 DIE *VariableDie = new DIE(DV.getTag());
1833 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001834 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren742671b2013-05-29 17:16:59 +00001835 if (AbsDIE)
Manman Ren87b110a2013-10-11 23:58:05 +00001836 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001837 else {
David Blaikie2883fd42013-08-19 03:34:03 +00001838 if (!Name.empty())
1839 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher7b641812013-11-15 01:43:19 +00001840 addSourceLine(VariableDie, DV.getVariable());
1841 addType(VariableDie, DV.getType());
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001842 }
1843
Eric Christopher7b641812013-11-15 01:43:19 +00001844 if (DV.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001845 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001846
1847 if (isScopeAbstract) {
Eric Christopher7b641812013-11-15 01:43:19 +00001848 DV.setDIE(VariableDie);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001849 return VariableDie;
1850 }
1851
1852 // Add variable address.
1853
Eric Christopher7b641812013-11-15 01:43:19 +00001854 unsigned Offset = DV.getDotDebugLocOffset();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001855 if (Offset != ~0U) {
Stephen Hines36b56882014-04-23 16:57:46 -07001856 addLocationList(VariableDie, dwarf::DW_AT_location, Offset);
Eric Christopher7b641812013-11-15 01:43:19 +00001857 DV.setDIE(VariableDie);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001858 return VariableDie;
1859 }
1860
Eric Christopher8cf5e742011-10-03 15:49:20 +00001861 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher7b641812013-11-15 01:43:19 +00001862 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie6d9dbd52013-06-16 20:34:15 +00001863 assert(DVInsn->getNumOperands() == 3);
1864 if (DVInsn->getOperand(0).isReg()) {
1865 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantlb2754912013-07-09 21:44:06 +00001866 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl35176402013-07-09 20:28:37 +00001867 if (DVInsn->getOperand(1).isImm()) {
Eric Christopher6efc0432013-10-19 01:04:47 +00001868 MachineLocation Location(RegOp.getReg(),
1869 DVInsn->getOperand(1).getImm());
Eric Christopher7b641812013-11-15 01:43:19 +00001870 addVariableAddress(DV, VariableDie, Location);
David Blaikie6d9dbd52013-06-16 20:34:15 +00001871 } else if (RegOp.getReg())
Eric Christopher7b641812013-11-15 01:43:19 +00001872 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie6d9dbd52013-06-16 20:34:15 +00001873 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher7b641812013-11-15 01:43:19 +00001874 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie6d9dbd52013-06-16 20:34:15 +00001875 else if (DVInsn->getOperand(0).isFPImm())
Eric Christophere4721492013-07-03 01:08:30 +00001876 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie6d9dbd52013-06-16 20:34:15 +00001877 else if (DVInsn->getOperand(0).isCImm())
Eric Christophere4721492013-07-03 01:08:30 +00001878 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher7b641812013-11-15 01:43:19 +00001879 isUnsignedDIType(DD, DV.getType()));
Eric Christophere4721492013-07-03 01:08:30 +00001880
Eric Christopher7b641812013-11-15 01:43:19 +00001881 DV.setDIE(VariableDie);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001882 return VariableDie;
1883 } else {
1884 // .. else use frame index.
Eric Christopher7b641812013-11-15 01:43:19 +00001885 int FI = DV.getFrameIndex();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001886 if (FI != ~0) {
1887 unsigned FrameReg = 0;
1888 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher6efc0432013-10-19 01:04:47 +00001889 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001890 MachineLocation Location(FrameReg, Offset);
Eric Christopher7b641812013-11-15 01:43:19 +00001891 addVariableAddress(DV, VariableDie, Location);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001892 }
1893 }
1894
Eric Christopher7b641812013-11-15 01:43:19 +00001895 DV.setDIE(VariableDie);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001896 return VariableDie;
1897}
1898
Manman Rena1d25b62013-10-23 23:00:44 +00001899/// constructMemberDIE - Construct member DIE from DIDerivedType.
Stephen Hines36b56882014-04-23 16:57:46 -07001900void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Ren1a5e7872013-10-29 00:53:03 +00001901 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel161b2f42011-04-12 23:21:44 +00001902 StringRef Name = DT.getName();
1903 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001904 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001905
Manman Ren017ceda2013-10-08 18:42:58 +00001906 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel161b2f42011-04-12 23:21:44 +00001907
1908 addSourceLine(MemberDie, DT);
1909
Eric Christopher6efc0432013-10-19 01:04:47 +00001910 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel161b2f42011-04-12 23:21:44 +00001911
1912 // For C++, virtual base classes are not at fixed offset. Use following
1913 // expression to extract appropriate offset from vtable.
1914 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1915
Stephen Hines36b56882014-04-23 16:57:46 -07001916 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie770530b2013-10-21 17:28:37 +00001917 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1918 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1919 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1920 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1921 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1922 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1923 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel161b2f42011-04-12 23:21:44 +00001924
David Blaikie770530b2013-10-21 17:28:37 +00001925 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie9b933922013-11-01 00:25:45 +00001926 } else {
1927 uint64_t Size = DT.getSizeInBits();
1928 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1929 uint64_t OffsetInBytes;
1930
1931 if (Size != FieldSize) {
Stephen Hines36b56882014-04-23 16:57:46 -07001932 // Handle bitfield, assume bytes are 8 bits.
1933 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1934 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
David Blaikie9b933922013-11-01 00:25:45 +00001935
1936 uint64_t Offset = DT.getOffsetInBits();
1937 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1938 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1939 uint64_t FieldOffset = (HiMark - FieldSize);
1940 Offset -= FieldOffset;
1941
1942 // Maybe we need to work from the other end.
1943 if (Asm->getDataLayout().isLittleEndian())
1944 Offset = FieldSize - (Offset + Size);
1945 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1946
Stephen Hines36b56882014-04-23 16:57:46 -07001947 // Here DW_AT_data_member_location points to the anonymous
David Blaikie9b933922013-11-01 00:25:45 +00001948 // field that includes this bit field.
1949 OffsetInBytes = FieldOffset >> 3;
1950 } else
1951 // This is not a bitfield.
1952 OffsetInBytes = DT.getOffsetInBits() >> 3;
Stephen Hines36b56882014-04-23 16:57:46 -07001953
1954 if (DD->getDwarfVersion() <= 2) {
1955 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
1956 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1957 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1958 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1959 } else
1960 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1961 OffsetInBytes);
David Blaikie9b933922013-11-01 00:25:45 +00001962 }
Devang Patel161b2f42011-04-12 23:21:44 +00001963
1964 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001965 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001966 dwarf::DW_ACCESS_protected);
1967 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001968 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001969 dwarf::DW_ACCESS_private);
1970 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001971 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001972 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001973 dwarf::DW_ACCESS_public);
1974 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001975 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001976 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001977
1978 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001979 if (MDNode *PNode = DT.getObjCProperty())
1980 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001981 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001982 PropertyDie);
1983
David Blaikie01bc2b32012-12-13 22:43:07 +00001984 if (DT.isArtificial())
1985 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +00001986}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001987
Manman Ren655a10d2013-10-14 20:33:57 +00001988/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Stephen Hines36b56882014-04-23 16:57:46 -07001989DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher6b6061f2013-01-16 01:22:23 +00001990 if (!DT.Verify())
1991 return NULL;
1992
Manman Ren655a10d2013-10-14 20:33:57 +00001993 // Construct the context before querying for the existence of the DIE in case
1994 // such construction creates the DIE.
1995 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikie8c0fecc2013-11-14 21:24:34 +00001996 assert(dwarf::isType(ContextDIE->getTag()) &&
1997 "Static member should belong to a type.");
Manman Ren655a10d2013-10-14 20:33:57 +00001998
1999 DIE *StaticMemberDIE = getDIE(DT);
2000 if (StaticMemberDIE)
2001 return StaticMemberDIE;
2002
Manman Ren1a5e7872013-10-29 00:53:03 +00002003 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Ren655a10d2013-10-14 20:33:57 +00002004
Manman Ren017ceda2013-10-08 18:42:58 +00002005 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher6b6061f2013-01-16 01:22:23 +00002006
2007 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
2008 addType(StaticMemberDIE, Ty);
2009 addSourceLine(StaticMemberDIE, DT);
2010 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
2011 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
2012
2013 // FIXME: We could omit private if the parent is a class_type, and
2014 // public if the parent is something else.
2015 if (DT.isProtected())
2016 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2017 dwarf::DW_ACCESS_protected);
2018 else if (DT.isPrivate())
2019 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2020 dwarf::DW_ACCESS_private);
2021 else
2022 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2023 dwarf::DW_ACCESS_public);
2024
2025 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renc664d762013-10-05 01:43:03 +00002026 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikie14268412013-01-20 01:18:01 +00002027 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
2028 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00002029
Eric Christopher6b6061f2013-01-16 01:22:23 +00002030 return StaticMemberDIE;
2031}
David Blaikie1d361132013-10-30 20:42:41 +00002032
Stephen Hines36b56882014-04-23 16:57:46 -07002033void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const {
David Blaikie1d361132013-10-30 20:42:41 +00002034 Asm->OutStreamer.AddComment("DWARF version number");
2035 Asm->EmitInt16(DD->getDwarfVersion());
2036 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Stephen Hines36b56882014-04-23 16:57:46 -07002037 // We share one abbreviations table across all units so it's always at the
2038 // start of the section. Use a relocatable offset where needed to ensure
2039 // linking doesn't invalidate that offset.
2040 if (ASectionSym)
2041 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
2042 else
2043 // Use a constant value when no symbol is provided.
2044 Asm->EmitInt32(0);
David Blaikie1d361132013-10-30 20:42:41 +00002045 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2046 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2047}
Stephen Hines36b56882014-04-23 16:57:46 -07002048
2049void DwarfUnit::addRange(RangeSpan Range) {
2050 // Only add a range for this unit if we're emitting full debug.
2051 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) {
2052 // If we have no current ranges just add the range and return, otherwise,
2053 // check the current section and CU against the previous section and CU we
2054 // emitted into and the subprogram was contained within. If these are the
2055 // same then extend our current range, otherwise add this as a new range.
2056 if (CURanges.size() == 0 ||
2057 this != DD->getPrevCU() ||
2058 Asm->getCurrentSection() != DD->getPrevSection()) {
2059 CURanges.push_back(Range);
2060 return;
2061 }
2062
2063 assert(&(CURanges.back().getEnd()->getSection()) ==
2064 &(Range.getEnd()->getSection()) &&
2065 "We can only append to a range in the same section!");
2066 CURanges.back().setEnd(Range.getEnd());
2067 }
2068}
2069
2070void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2071 // Define start line table label for each Compile Unit.
2072 MCSymbol *LineTableStartSym =
2073 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
2074
2075 stmtListIndex = UnitDie->getValues().size();
2076
2077 // DW_AT_stmt_list is a offset of line number information for this
2078 // compile unit in debug_line section. For split dwarf this is
2079 // left in the skeleton CU and so not included.
2080 // The line table entries are not always emitted in assembly, so it
2081 // is not okay to use line_table_start here.
2082 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2083 addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym);
2084 else
2085 addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2086 DwarfLineSectionSym);
2087}
2088
2089void DwarfCompileUnit::applyStmtList(DIE &D) {
2090 D.addValue(dwarf::DW_AT_stmt_list,
2091 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2092 UnitDie->getValues()[stmtListIndex]);
2093}
2094
2095void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const {
2096 DwarfUnit::emitHeader(ASectionSym);
2097 Asm->OutStreamer.AddComment("Type Signature");
2098 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2099 Asm->OutStreamer.AddComment("Type DIE Offset");
2100 // In a skeleton type unit there is no type DIE so emit a zero offset.
2101 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2102 sizeof(Ty->getOffset()));
2103}
2104
2105void DwarfTypeUnit::initSection(const MCSection *Section) {
2106 assert(!this->Section);
2107 this->Section = Section;
2108 // Since each type unit is contained in its own COMDAT section, the begin
2109 // label and the section label are the same. Using the begin label emission in
2110 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2111 // the only other alternative of lazily constructing start-of-section labels
2112 // and storing a mapping in DwarfDebug (or AsmPrinter).
2113 this->SectionSym = this->LabelBegin =
2114 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2115 this->LabelEnd =
2116 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2117 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2118}