blob: 7f6fd5d13c3ed934b18731a271e008958dc60a65 [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
Devang Patel0e821f42011-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 Christopher160522c2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel0e821f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
David Blaikie2c86a722013-12-02 19:33:15 +000016#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000021#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000026#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000027#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49}
50
Eric Christopher4287a492013-12-09 23:57:44 +000051DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000054 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000056}
57
David Blaikie15632ae2014-02-12 00:31:30 +000058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
David Blaikie4a2f95f2014-03-18 01:17:26 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU,
David Blaikie8287aff2014-03-18 02:13:23 +000060 MCDwarfDwoLineTable *SplitLineTable)
David Blaikie4a2f95f2014-03-18 01:17:26 +000061 : 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}
David Blaikie409dd9c2013-11-19 23:08:21 +000066
David Blaikie319a05f2013-12-02 19:33:10 +000067/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000068DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000069 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
70 DIEBlocks[j]->~DIEBlock();
Eric Christopher4a741042014-02-16 08:46:55 +000071 for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
72 DIELocs[j]->~DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +000073}
74
75/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
76/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000077DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000078 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
79 return Value;
80}
81
Bill Wendling3495f9b2012-12-06 07:55:19 +000082/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000083/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000084int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000085 switch (getLanguage()) {
Bill Wendling28fe9e72012-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 Ren4dbdc902013-10-31 17:54:35 +0000126/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000127static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000128 // 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.
David Blaikie409dd9c2013-11-19 23:08:21 +0000135 return (D.isType() ||
136 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000137 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-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.
Eric Christophera5a79422013-12-09 23:32:48 +0000144DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000145 if (isShareableAcrossCUs(D))
146 return DD->getDIE(D);
147 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-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.
Eric Christophera5a79422013-12-09 23:32:48 +0000153void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000154 if (isShareableAcrossCUs(Desc)) {
155 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000156 return;
157 }
David Blaikie2ad00162013-11-15 23:09:13 +0000158 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000159}
160
Eric Christopherbb69a272012-08-24 01:14:27 +0000161/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000162void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000163 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000164 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000165 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000166 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000167}
168
Devang Patel0e821f42011-04-12 23:21:44 +0000169/// addUInt - Add an unsigned integer attribute data and value.
170///
Eric Christophera5a79422013-12-09 23:32:48 +0000171void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
172 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-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 Blaikief2443192013-10-21 17:28:37 +0000177 Die->addValue(Attribute, *Form, Value);
178}
179
Eric Christopher4a741042014-02-16 08:46:55 +0000180void DwarfUnit::addUInt(DIE *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000181 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000182}
183
184/// addSInt - Add an signed integer attribute data and value.
185///
Eric Christophera5a79422013-12-09 23:32:48 +0000186void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
187 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000188 if (!Form)
189 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000190 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000191 Die->addValue(Attribute, *Form, Value);
192}
193
Eric Christopher4a741042014-02-16 08:46:55 +0000194void DwarfUnit::addSInt(DIELoc *Die, Optional<dwarf::Form> Form,
Eric Christophera5a79422013-12-09 23:32:48 +0000195 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000196 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000197}
198
Nick Lewyckycc64ae12011-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 Christopherfba22602013-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.
Eric Christophera5a79422013-12-09 23:32:48 +0000204void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
205 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000206
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 Christopher67646432013-07-26 17:02:41 +0000212 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000213 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-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.
Eric Christophera5a79422013-12-09 23:32:48 +0000218void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
219 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000220 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000222 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000223 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000224 else {
Eric Christophere698f532012-12-20 21:58:36 +0000225 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000226 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000227 }
Eric Christopher05893f42013-12-30 18:32:31 +0000228 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
229 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000230}
231
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000232/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000233///
Eric Christopher4a741042014-02-16 08:46:55 +0000234void DwarfUnit::addExpr(DIELoc *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000235 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000236 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000237}
238
Eric Christophera27220f2014-03-05 22:41:20 +0000239/// 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 Weigand396ba8b2013-07-02 18:46:26 +0000249/// addLabel - Add a Dwarf label attribute data and value.
250///
Eric Christophera5a79422013-12-09 23:32:48 +0000251void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
252 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000253 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
254 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000255}
256
Eric Christopher4a741042014-02-16 08:46:55 +0000257void DwarfUnit::addLabel(DIELoc *Die, dwarf::Form Form, const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000258 addLabel(Die, (dwarf::Attribute)0, Form, Label);
259}
260
Eric Christopher33ff6972013-11-21 23:46:41 +0000261/// addSectionLabel - Add a Dwarf section label attribute data and value.
262///
Eric Christophera5a79422013-12-09 23:32:48 +0000263void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
264 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000265 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///
Eric Christophera5a79422013-12-09 23:32:48 +0000273void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
274 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000275 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 Christopher962c9082013-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///
Eric Christopher4287a492013-12-09 23:57:44 +0000284void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
Eric Christopher384f3fe2014-03-20 19:16:16 +0000285 const MCSymbol *Label) {
286
287 if (!DD->useSplitDwarf())
288 return addLocalLabelAddress(Die, Attribute, Label);
289
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000290 if (Label)
291 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000292
Eric Christopher384f3fe2014-03-20 19:16:16 +0000293 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 Christopher962c9082013-01-15 23:56:56 +0000307 } else {
Eric Christopher384f3fe2014-03-20 19:16:16 +0000308 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
309 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000310 }
311}
312
David Blaikie0e8d4012014-03-17 23:53:25 +0000313unsigned 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
David Blaikie4a2f95f2014-03-18 01:17:26 +0000324unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
325 return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
326 : getCU().getOrCreateSourceID(FileName, DirName);
327}
328
Eric Christophere9ec2452013-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///
Eric Christopher4a741042014-02-16 08:46:55 +0000332void DwarfUnit::addOpAddress(DIELoc *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000333 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-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 Christophere9ec2452013-01-18 22:11:33 +0000336 } else {
David Blaikief2443192013-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 Christophere9ec2452013-01-18 22:11:33 +0000339 }
340}
341
Eric Christopher33ff6972013-11-21 23:46:41 +0000342/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000343///
Eric Christophera5a79422013-12-09 23:32:48 +0000344void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
345 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000346 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000347 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);
Devang Patel0e821f42011-04-12 23:21:44 +0000351}
352
David Blaikie48b1bdc2014-03-07 01:30:55 +0000353void 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);
357}
358
Devang Patel0e821f42011-04-12 23:21:44 +0000359/// addDIEEntry - Add a DIE attribute data and value.
360///
Eric Christophera5a79422013-12-09 23:32:48 +0000361void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000362 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
363}
364
David Blaikie47f615e2013-12-17 23:32:35 +0000365void 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
Eric Christophera5a79422013-12-09 23:32:48 +0000370void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
371 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000372 const DIE *DieCU = Die->getUnitOrNull();
373 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-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.
David Blaikie2a80e442013-12-02 22:09:48 +0000376 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000377 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000378 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000379 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
380 : dwarf::DW_FORM_ref_addr,
381 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000382}
383
Manman Renb987e512013-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.
Eric Christophera5a79422013-12-09 23:32:48 +0000386DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
David Blaikieefc403b2014-04-12 02:24:04 +0000387 assert(Tag != dwarf::DW_TAG_auto_variable &&
388 Tag != dwarf::DW_TAG_arg_variable);
389 DIE *Die = new DIE((dwarf::Tag)Tag);
Manman Renb987e512013-10-29 00:53:03 +0000390 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000391 if (N)
392 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000393 return Die;
394}
395
Devang Patel0e821f42011-04-12 23:21:44 +0000396/// addBlock - Add block data.
397///
Eric Christopher4a741042014-02-16 08:46:55 +0000398void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIELoc *Loc) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000399 Loc->ComputeSize(Asm);
Eric Christopher4a741042014-02-16 08:46:55 +0000400 DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
401 Die->addValue(Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
402}
403
Eric Christophera5a79422013-12-09 23:32:48 +0000404void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
405 DIEBlock *Block) {
Eric Christopher8bdab432014-02-27 18:36:10 +0000406 Block->ComputeSize(Asm);
Devang Patel0e821f42011-04-12 23:21:44 +0000407 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
408 Die->addValue(Attribute, Block->BestForm(), Block);
409}
410
411/// addSourceLine - Add location information to specified debug information
412/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000413void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
414 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000415 if (Line == 0)
416 return;
David Blaikie101613e2014-02-12 00:11:25 +0000417
David Blaikie4a2f95f2014-03-18 01:17:26 +0000418 unsigned FileID = getOrCreateSourceID(File, Directory);
Devang Patel0e821f42011-04-12 23:21:44 +0000419 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000420 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
421 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000422}
423
424/// addSourceLine - Add location information to specified debug information
425/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000426void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
427 assert(V.isVariable());
428
Eric Christopher89a575c2014-02-12 22:38:04 +0000429 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(),
430 V.getContext().getDirectory());
David Blaikie101613e2014-02-12 00:11:25 +0000431}
432
433/// addSourceLine - Add location information to specified debug information
434/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000435void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000436 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000437
David Blaikie101613e2014-02-12 00:11:25 +0000438 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000439}
440
441/// addSourceLine - Add location information to specified debug information
442/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000443void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000444 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000445
David Blaikie101613e2014-02-12 00:11:25 +0000446 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000447}
448
449/// addSourceLine - Add location information to specified debug information
450/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000451void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000452 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000453
David Blaikie101613e2014-02-12 00:11:25 +0000454 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000455}
456
457/// addSourceLine - Add location information to specified debug information
458/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000459void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000460 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000461
Eric Christopher70e1bd82012-03-29 08:42:56 +0000462 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000463 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
464 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000465}
466
467/// addSourceLine - Add location information to specified debug information
468/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000469void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000470 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000471
David Blaikie101613e2014-02-12 00:11:25 +0000472 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000473}
474
Eric Christopher92331fd2012-11-21 00:34:38 +0000475/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000476/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000477void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
478 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000479 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000480 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000481 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000482 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
483 else
David Blaikieea2605d2013-06-20 00:25:24 +0000484 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000485 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000486}
487
Devang Patelba5fbf12011-04-26 19:06:18 +0000488/// addRegisterOp - Add register operand.
Eric Christopher4a741042014-02-16 08:46:55 +0000489void DwarfUnit::addRegisterOp(DIELoc *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000490 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000491 int DWReg = RI->getDwarfRegNum(Reg, false);
492 bool isSubRegister = DWReg < 0;
493
494 unsigned Idx = 0;
495
496 // Go up the super-register chain until we hit a valid dwarf register number.
497 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
498 DWReg = RI->getDwarfRegNum(*SR, false);
499 if (DWReg >= 0)
500 Idx = RI->getSubRegIndex(*SR, Reg);
501 }
502
503 if (DWReg < 0) {
Eric Christophera13839f2014-02-26 23:27:16 +0000504 DEBUG(dbgs() << "Invalid Dwarf register number.\n");
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000505 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
506 return;
507 }
508
509 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000510 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000511 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000512 else {
David Blaikief2443192013-10-21 17:28:37 +0000513 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
514 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000515 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000516
517 // Emit Mask
518 if (isSubRegister) {
519 unsigned Size = RI->getSubRegIdxSize(Idx);
520 unsigned Offset = RI->getSubRegIdxOffset(Idx);
521 if (Offset > 0) {
522 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
523 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
524 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
525 } else {
Adrian Prantl7199fd52014-02-12 19:34:44 +0000526 unsigned ByteSize = Size / 8; // Assuming 8 bits per byte.
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000527 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
Adrian Prantl7199fd52014-02-12 19:34:44 +0000528 addUInt(TheDie, dwarf::DW_FORM_data1, ByteSize);
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000529 }
530 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000531}
532
533/// addRegisterOffset - Add register offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000534void DwarfUnit::addRegisterOffset(DIELoc *TheDie, unsigned Reg,
Eric Christophera5a79422013-12-09 23:32:48 +0000535 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000536 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
537 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
538 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
539 if (Reg == TRI->getFrameRegister(*Asm->MF))
540 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000541 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000542 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000543 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000544 else {
David Blaikief2443192013-10-21 17:28:37 +0000545 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
546 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000547 }
David Blaikief2443192013-10-21 17:28:37 +0000548 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000549}
550
551/// addAddress - Add an address attribute to a die based on the location
552/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000553void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
554 const MachineLocation &Location, bool Indirect) {
Eric Christopher4a741042014-02-16 08:46:55 +0000555 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patelba5fbf12011-04-26 19:06:18 +0000556
David Blaikieea2605d2013-06-20 00:25:24 +0000557 if (Location.isReg() && !Indirect)
Eric Christopher4a741042014-02-16 08:46:55 +0000558 addRegisterOp(Loc, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000559 else {
Eric Christopher4a741042014-02-16 08:46:55 +0000560 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000561 if (Indirect && !Location.isReg()) {
Eric Christopher4a741042014-02-16 08:46:55 +0000562 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000563 }
564 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000565
566 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000567 addBlock(Die, Attribute, Loc);
Devang Patelba5fbf12011-04-26 19:06:18 +0000568}
569
Devang Patel0e821f42011-04-12 23:21:44 +0000570/// addComplexAddress - Start with the address based on the location provided,
571/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000572/// given the extra address information encoded in the DbgVariable, starting
573/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000574///
Eric Christophera5a79422013-12-09 23:32:48 +0000575void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
576 dwarf::Attribute Attribute,
577 const MachineLocation &Location) {
Eric Christopher4a741042014-02-16 08:46:55 +0000578 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000579 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000580 unsigned i = 0;
581 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000582 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000583 // If first address element is OpPlus then emit
584 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher4a741042014-02-16 08:46:55 +0000585 addRegisterOffset(Loc, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000586 i = 2;
587 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000588 addRegisterOp(Loc, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000589 } else
Eric Christopher4a741042014-02-16 08:46:55 +0000590 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000591
Eric Christopherc2697f82013-10-19 01:04:47 +0000592 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000593 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000594 if (Element == DIBuilder::OpPlus) {
Eric Christopher4a741042014-02-16 08:46:55 +0000595 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
596 addUInt(Loc, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000597 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000598 if (!Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000599 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000600 } else
601 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000602 }
603
604 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000605 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000606}
607
608/* Byref variables, in Blocks, are declared by the programmer as "SomeType
609 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
610 gives the variable VarName either the struct, or a pointer to the struct, as
611 its type. This is necessary for various behind-the-scenes things the
612 compiler needs to do with by-reference variables in Blocks.
613
614 However, as far as the original *programmer* is concerned, the variable
615 should still have type 'SomeType', as originally declared.
616
617 The function getBlockByrefType dives into the __Block_byref_x_VarName
618 struct to find the original type of the variable, which is then assigned to
619 the variable's Debug Information Entry as its real type. So far, so good.
620 However now the debugger will expect the variable VarName to have the type
621 SomeType. So we need the location attribute for the variable to be an
622 expression that explains to the debugger how to navigate through the
623 pointers and struct to find the actual variable of type SomeType.
624
625 The following function does just that. We start by getting
626 the "normal" location for the variable. This will be the location
627 of either the struct __Block_byref_x_VarName or the pointer to the
628 struct __Block_byref_x_VarName.
629
630 The struct will look something like:
631
632 struct __Block_byref_x_VarName {
633 ... <various fields>
634 struct __Block_byref_x_VarName *forwarding;
635 ... <various other fields>
636 SomeType VarName;
637 ... <maybe more fields>
638 };
639
640 If we are given the struct directly (as our starting point) we
641 need to tell the debugger to:
642
643 1). Add the offset of the forwarding field.
644
645 2). Follow that pointer to get the real __Block_byref_x_VarName
646 struct to use (the real one may have been copied onto the heap).
647
648 3). Add the offset for the field VarName, to find the actual variable.
649
650 If we started with a pointer to the struct, then we need to
651 dereference that pointer first, before the other steps.
652 Translating this into DWARF ops, we will need to append the following
653 to the current location description for the variable:
654
655 DW_OP_deref -- optional, if we start with a pointer
656 DW_OP_plus_uconst <forward_fld_offset>
657 DW_OP_deref
658 DW_OP_plus_uconst <varName_fld_offset>
659
660 That is what this function does. */
661
662/// addBlockByrefAddress - Start with the address based on the location
663/// provided, and generate the DWARF information necessary to find the
664/// actual Block variable (navigating the Block struct) based on the
665/// starting location. Add the DWARF information to the die. For
666/// more information, read large comment just above here.
667///
Eric Christophera5a79422013-12-09 23:32:48 +0000668void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
669 dwarf::Attribute Attribute,
670 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000671 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000672 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000673 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000674 bool isPointer = false;
675
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000676 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000677
678 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000679 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000680 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000681 isPointer = true;
682 }
683
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000684 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000685
686 // Find the __forwarding field and the variable field in the __Block_byref
687 // struct.
688 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000689 DIDerivedType varField;
690 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000691
692 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000693 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000694 StringRef fieldName = DT.getName();
695 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000696 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000697 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000698 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000699 }
700
701 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000702 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
703 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000704
705 // Decode the original location, and use that as the start of the byref
706 // variable's location.
Eric Christopher4a741042014-02-16 08:46:55 +0000707 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Patel0e821f42011-04-12 23:21:44 +0000708
Eric Christopheref9d7102012-07-04 02:02:18 +0000709 if (Location.isReg())
Eric Christopher4a741042014-02-16 08:46:55 +0000710 addRegisterOp(Loc, Location.getReg());
Eric Christopheref9d7102012-07-04 02:02:18 +0000711 else
Eric Christopher4a741042014-02-16 08:46:55 +0000712 addRegisterOffset(Loc, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000713
714 // If we started with a pointer to the __Block_byref... struct, then
715 // the first thing we need to do is dereference the pointer (DW_OP_deref).
716 if (isPointer)
Eric Christopher4a741042014-02-16 08:46:55 +0000717 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000718
719 // Next add the offset for the '__forwarding' field:
720 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
721 // adding the offset if it's 0.
722 if (forwardingFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000723 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
724 addUInt(Loc, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000725 }
726
727 // Now dereference the __forwarding field to get to the real __Block_byref
728 // struct: DW_OP_deref.
Eric Christopher4a741042014-02-16 08:46:55 +0000729 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000730
731 // Now that we've got the real __Block_byref... struct, add the offset
732 // for the variable's field to get to the location of the actual variable:
733 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
734 if (varFieldOffset > 0) {
Eric Christopher4a741042014-02-16 08:46:55 +0000735 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
736 addUInt(Loc, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000737 }
738
739 // Now attach the location information to the DIE.
Eric Christopher4a741042014-02-16 08:46:55 +0000740 addBlock(Die, Attribute, Loc);
Devang Patel0e821f42011-04-12 23:21:44 +0000741}
742
Devang Patelbcd50a12011-07-20 21:57:04 +0000743/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000744static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000745 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000746 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
747 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000748 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000749 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
750 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000751 *SizeInBits = Ty.getSizeInBits();
752 return true;
753 }
754 return false;
755}
756
Manman Renb3388602013-10-05 01:43:03 +0000757/// Return true if type encoding is unsigned.
758static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
759 DIDerivedType DTy(Ty);
760 if (DTy.isDerivedType())
761 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
762
763 DIBasicType BTy(Ty);
764 if (BTy.isBasicType()) {
765 unsigned Encoding = BTy.getEncoding();
766 if (Encoding == dwarf::DW_ATE_unsigned ||
767 Encoding == dwarf::DW_ATE_unsigned_char ||
768 Encoding == dwarf::DW_ATE_boolean)
769 return true;
770 }
771 return false;
772}
773
774/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000775static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000776 unsigned Tag = Ty.getTag();
777
778 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
779 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
780 Tag != dwarf::DW_TAG_restrict_type)
781 return Ty.getSizeInBits();
782
783 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
784
Eric Christopher8cc04fc2014-03-12 18:18:05 +0000785 // If this type is not derived from any type or the type is a declaration then
786 // take conservative approach.
787 if (!BaseType.isValid() || BaseType.isForwardDecl())
Manman Renb3388602013-10-05 01:43:03 +0000788 return Ty.getSizeInBits();
789
790 // If this is a derived type, go ahead and get the base type, unless it's a
791 // reference then it's just the size of the field. Pointer types have no need
792 // of this since they're a different type of qualification on the type.
793 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
794 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
795 return Ty.getSizeInBits();
796
797 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000798 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000799
800 return BaseType.getSizeInBits();
801}
802
Devang Patel0e821f42011-04-12 23:21:44 +0000803/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000804void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
805 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000806 // FIXME: This is a bit conservative/simple - it emits negative values at
807 // their maximum bit width which is a bit unfortunate (& doesn't prefer
808 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000809 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000810 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000811 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000812 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000813
Eric Christopher9d1daa82013-08-27 23:49:04 +0000814 // If we're a signed constant definitely use sdata.
815 if (SignedConstant) {
816 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
817 return;
818 }
819
820 // Else use data for now unless it's larger than we can deal with.
821 switch (SizeInBits) {
822 case 8:
823 Form = dwarf::DW_FORM_data1;
824 break;
825 case 16:
826 Form = dwarf::DW_FORM_data2;
827 break;
828 case 32:
829 Form = dwarf::DW_FORM_data4;
830 break;
831 case 64:
832 Form = dwarf::DW_FORM_data8;
833 break;
834 default:
835 Form = dwarf::DW_FORM_udata;
836 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
837 return;
838 }
839 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000840}
841
842/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000843void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000844 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000845 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
846 APFloat FPImm = MO.getFPImm()->getValueAPF();
847
848 // Get the raw data form of the floating point.
849 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000850 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000851
852 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000853 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000854 int Incr = (LittleEndian ? 1 : -1);
855 int Start = (LittleEndian ? 0 : NumBytes - 1);
856 int Stop = (LittleEndian ? NumBytes : -1);
857
858 // Output the constant to DWARF one byte at a time.
859 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000860 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000861
David Blaikief2443192013-10-21 17:28:37 +0000862 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000863}
864
David Blaikiea39a76e2013-01-20 01:18:01 +0000865/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000866void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000867 // Pass this down to addConstantValue as an unsigned bag of bits.
868 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000869}
870
Devang Patel0e821f42011-04-12 23:21:44 +0000871/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000872void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
873 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000874 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000875}
876
877// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000878void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000879 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000880 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000881 // If we're a signed constant definitely use sdata.
882 if (!Unsigned) {
883 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
884 Val.getSExtValue());
885 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000886 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000887
888 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000889 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000890 switch (CIBitWidth) {
891 case 8:
892 Form = dwarf::DW_FORM_data1;
893 break;
894 case 16:
895 Form = dwarf::DW_FORM_data2;
896 break;
897 case 32:
898 Form = dwarf::DW_FORM_data4;
899 break;
900 case 64:
901 Form = dwarf::DW_FORM_data8;
902 break;
903 default:
904 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
905 Val.getZExtValue());
906 return;
907 }
908 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000909 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000910 }
911
912 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
913
914 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000915 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000916
917 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000918 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000919
920 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000921 for (int i = 0; i < NumBytes; i++) {
922 uint8_t c;
923 if (LittleEndian)
924 c = Ptr64[i / 8] >> (8 * (i & 7));
925 else
926 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000927 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000928 }
Devang Patel0e821f42011-04-12 23:21:44 +0000929
David Blaikief2443192013-10-21 17:28:37 +0000930 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000931}
932
Eric Christopher25e35092013-04-22 07:47:40 +0000933/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000934void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000935 // Add template parameters.
936 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
937 DIDescriptor Element = TParams.getElement(i);
938 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000939 constructTemplateTypeParameterDIE(Buffer,
940 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000941 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000942 constructTemplateValueParameterDIE(Buffer,
943 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000944 }
Devang Patel0e821f42011-04-12 23:21:44 +0000945}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000946
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000947/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000948DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000949 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000950 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000951 if (Context.isType())
952 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000953 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000954 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000955 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000956 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000957 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000958}
959
Eric Christophera5a79422013-12-09 23:32:48 +0000960DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000961 DIScope Context = resolve(Ty.getContext());
962 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000963
964 DIE *TyDIE = getDIE(Ty);
965 if (TyDIE)
966 return TyDIE;
967
968 // Create new type.
969 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
970
David Blaikiefbd29eb2013-11-26 00:35:04 +0000971 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000972
David Blaikie9d861be2013-11-26 00:15:27 +0000973 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000974 return TyDIE;
975}
976
Devang Patel0e821f42011-04-12 23:21:44 +0000977/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
978/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000979DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000980 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000981 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000982
David Blaikie32887552013-11-14 22:25:02 +0000983 DIType Ty(TyNode);
984 assert(Ty.isType());
Adrian Prantlc95ec912014-03-24 21:33:01 +0000985 assert(Ty == resolve(Ty.getRef()) &&
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000986 "type was not uniqued, possible ODR violation.");
David Blaikie32887552013-11-14 22:25:02 +0000987
David Blaikieee11f222014-04-12 05:35:59 +0000988 // DW_TAG_restrict_type is not supported in DWARF2
989 if (Ty.getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
990 return getOrCreateTypeDIE(resolve(DIDerivedType(Ty).getTypeDerivedFrom()));
991
Manman Renf4c339e2013-10-29 22:49:29 +0000992 // Construct the context before querying for the existence of the DIE in case
993 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000994 DIScope Context = resolve(Ty.getContext());
995 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000996 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000997
Eric Christophere595bae2013-10-04 17:08:38 +0000998 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000999 if (TyDIE)
1000 return TyDIE;
1001
1002 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +00001003 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
1004
David Blaikiec3d9e9e2014-03-06 01:42:00 +00001005 updateAcceleratorTables(Context, Ty, TyDIE);
1006
Devang Patel0e821f42011-04-12 23:21:44 +00001007 if (Ty.isBasicType())
1008 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +00001009 else if (Ty.isCompositeType()) {
1010 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +00001011 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
1012 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +00001013 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
Adrian Prantle2da17f2014-03-14 23:08:17 +00001014 // Skip updating the accelerator tables since this is not the full type.
David Blaikiecfb21152014-01-03 18:59:42 +00001015 return TyDIE;
1016 }
David Blaikiefbd29eb2013-11-26 00:35:04 +00001017 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +00001018 } else {
Devang Patel0e821f42011-04-12 23:21:44 +00001019 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1020 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1021 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001022
David Blaikie2ea848b2013-11-19 22:51:04 +00001023 return TyDIE;
1024}
1025
Eric Christophera5a79422013-12-09 23:32:48 +00001026void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1027 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001028 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1029 bool IsImplementation = 0;
1030 if (Ty.isCompositeType()) {
1031 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001032 // A runtime language of 0 actually means C/C++ and that any
1033 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001034 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001035 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001036 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001037 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001038
David Blaikieadbea1e2014-03-12 03:34:38 +00001039 if ((!Context || Context.isCompileUnit() || Context.isFile() ||
1040 Context.isNameSpace()) &&
1041 getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
David Blaikie9d861be2013-11-26 00:15:27 +00001042 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001043 }
Devang Patel0e821f42011-04-12 23:21:44 +00001044}
1045
1046/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001047void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001048 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001049
1050 // Check for pre-existence.
1051 DIEEntry *Entry = getDIEEntry(Ty);
1052 // If it exists then use the existing value.
1053 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001054 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001055 return;
1056 }
1057
1058 // Construct type.
1059 DIE *Buffer = getOrCreateTypeDIE(Ty);
1060
1061 // Set up proxy.
1062 Entry = createDIEEntry(Buffer);
1063 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001064 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001065}
1066
Eric Christopher9cd26af2013-09-20 23:22:52 +00001067// Accelerator table mutators - add each name along with its companion
1068// DIE to the proper table while ensuring that the name that we're going
1069// to reference is in the string table. We do this since the names we
1070// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001071void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001072 if (!DD->useDwarfAccelTables())
1073 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001074 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001075 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001076 DIEs.push_back(Die);
1077}
1078
Eric Christophera5a79422013-12-09 23:32:48 +00001079void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001080 if (!DD->useDwarfAccelTables())
1081 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001082 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001083 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001084 DIEs.push_back(Die);
1085}
1086
Eric Christophera5a79422013-12-09 23:32:48 +00001087void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001088 if (!DD->useDwarfAccelTables())
1089 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001090 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001091 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001092 DIEs.push_back(Die);
1093}
1094
Eric Christophera5a79422013-12-09 23:32:48 +00001095void DwarfUnit::addAccelType(StringRef Name,
1096 std::pair<const DIE *, unsigned> Die) {
Eric Christopherd0d5bba2014-02-12 22:47:09 +00001097 if (!DD->useDwarfAccelTables())
1098 return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001099 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001100 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001101 DIEs.push_back(Die);
1102}
1103
Eric Christopher9c58f312013-09-20 22:20:55 +00001104/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001105void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
David Blaikieadbea1e2014-03-12 03:34:38 +00001106 if (getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly)
1107 return;
Eric Christopher8dba0d52013-10-19 01:04:42 +00001108 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001109 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001110}
1111
Eric Christopher2c8b7902013-10-17 02:06:06 +00001112/// getParentContextString - Walks the metadata parent chain in a language
1113/// specific manner (using the compile unit language) and returns
1114/// it as a string. This is done at the metadata level because DIEs may
1115/// not currently have been added to the parent context and walking the
1116/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001117std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001118 if (!Context)
1119 return "";
1120
1121 // FIXME: Decide whether to implement this for non-C++ languages.
1122 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1123 return "";
1124
Eric Christopher8dba0d52013-10-19 01:04:42 +00001125 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001126 SmallVector<DIScope, 1> Parents;
1127 while (!Context.isCompileUnit()) {
1128 Parents.push_back(Context);
1129 if (Context.getContext())
1130 Context = resolve(Context.getContext());
1131 else
1132 // Structure, etc types will have a NULL context if they're at the top
1133 // level.
1134 break;
1135 }
1136
1137 // Reverse iterate over our list to go from the outermost construct to the
1138 // innermost.
1139 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1140 E = Parents.rend();
1141 I != E; ++I) {
1142 DIScope Ctx = *I;
1143 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001144 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001145 CS += Name;
1146 CS += "::";
1147 }
1148 }
1149 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001150}
1151
1152/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001153void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001154 // Get core information.
1155 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001156 // Add name if not anonymous or intermediate type.
1157 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001158 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001159
David Blaikiefac56122013-10-04 23:21:16 +00001160 // An unspecified type only has a name attribute.
1161 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001162 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001163
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001164 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001165 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001166
Devang Patel0e821f42011-04-12 23:21:44 +00001167 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001168 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001169}
1170
1171/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001172void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001173 // Get core information.
1174 StringRef Name = DTy.getName();
1175 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001176 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001177
1178 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001179 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001180 if (FromTy)
1181 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001182
1183 // Add name if not anonymous or intermediate type.
1184 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001185 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001186
1187 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001188 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001189 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001190
David Blaikie5d3249b2013-01-07 05:51:15 +00001191 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001192 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1193 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001194 // Add source line info if available and TyDesc is not a forward declaration.
1195 if (!DTy.isForwardDecl())
1196 addSourceLine(&Buffer, DTy);
1197}
1198
Adrian Prantl3f49c892014-02-25 19:57:42 +00001199/// constructSubprogramArguments - Construct function argument DIEs.
1200void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DIArray Args) {
Adrian Prantl69140d22014-02-25 22:27:14 +00001201 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1202 DIDescriptor Ty = Args.getElement(i);
1203 if (Ty.isUnspecifiedParameter()) {
1204 assert(i == N-1 && "Unspecified parameter must be the last argument");
1205 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1206 } else {
1207 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1208 addType(Arg, DIType(Ty));
1209 if (DIType(Ty).isArtificial())
1210 addFlag(Arg, dwarf::DW_AT_artificial);
Adrian Prantl3f49c892014-02-25 19:57:42 +00001211 }
Adrian Prantl69140d22014-02-25 22:27:14 +00001212 }
Adrian Prantl3f49c892014-02-25 19:57:42 +00001213}
1214
Devang Patel0e821f42011-04-12 23:21:44 +00001215/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001216void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001217 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001218 StringRef Name = CTy.getName();
1219
1220 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001221 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001222
1223 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001224 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001225 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001226 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001227 case dwarf::DW_TAG_enumeration_type:
1228 constructEnumTypeDIE(Buffer, CTy);
1229 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001230 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001231 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001232 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001233 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001234 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001235 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001236
1237 bool isPrototyped = true;
Adrian Prantl3f49c892014-02-25 19:57:42 +00001238 if (Elements.getNumElements() == 2 &&
1239 Elements.getElement(1).isUnspecifiedParameter())
1240 isPrototyped = false;
1241
1242 constructSubprogramArguments(Buffer, Elements);
1243
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001244 // Add prototype flag if we're dealing with a C language and the
1245 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001246 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001247 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001248 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001249 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001250 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001251
1252 if (CTy.isLValueReference())
1253 addFlag(&Buffer, dwarf::DW_AT_reference);
1254
1255 if (CTy.isRValueReference())
1256 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001257 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001258 case dwarf::DW_TAG_structure_type:
1259 case dwarf::DW_TAG_union_type:
1260 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001261 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001262 DIArray Elements = CTy.getTypeArray();
1263 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001264 DIDescriptor Element = Elements.getElement(i);
1265 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001266 if (Element.isSubprogram())
1267 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1268 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001269 DIDerivedType DDTy(Element);
1270 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001271 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001272 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001273 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001274 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001275 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001276 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001277 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001278 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001279 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001280 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001281 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001282 StringRef PropertyName = Property.getObjCPropertyName();
1283 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001284 if (Property.getType())
1285 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001286 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001287 StringRef GetterName = Property.getObjCPropertyGetterName();
1288 if (!GetterName.empty())
1289 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1290 StringRef SetterName = Property.getObjCPropertySetterName();
1291 if (!SetterName.empty())
1292 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1293 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001294 if (Property.isReadOnlyObjCProperty())
1295 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1296 if (Property.isReadWriteObjCProperty())
1297 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1298 if (Property.isAssignObjCProperty())
1299 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1300 if (Property.isRetainObjCProperty())
1301 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1302 if (Property.isCopyObjCProperty())
1303 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1304 if (Property.isNonAtomicObjCProperty())
1305 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1306 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001307 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001308 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001309
Devang Pateld925d1a2012-02-07 23:33:58 +00001310 DIEEntry *Entry = getDIEEntry(Element);
1311 if (!Entry) {
1312 Entry = createDIEEntry(ElemDie);
1313 insertDIEEntry(Element, Entry);
1314 }
Devang Patel403e8192012-02-04 01:30:32 +00001315 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001316 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001317 }
1318
1319 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001320 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001321
Eric Christopher9e429ae2013-10-05 00:27:02 +00001322 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001323 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001324 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001325 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001326
Devang Patel12419ae2011-05-12 21:29:42 +00001327 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001328 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001329
Eric Christopherda011dd2011-12-16 23:42:42 +00001330 // Add template parameters to a class, structure or union types.
1331 // FIXME: The support isn't in the metadata for this yet.
1332 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001333 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001334 addTemplateParams(Buffer, CTy.getTemplateParams());
1335
1336 break;
1337 }
1338 default:
1339 break;
1340 }
1341
1342 // Add name if not anonymous or intermediate type.
1343 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001344 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001345
Eric Christopher775cbd22012-05-22 18:45:18 +00001346 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001347 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001348 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001349 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001350 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001351 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001352 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001353 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001354 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001355 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001356
1357 // If we're a forward decl, say so.
1358 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001359 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001360
1361 // Add source line info if available.
1362 if (!CTy.isForwardDecl())
1363 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001364
1365 // No harm in adding the runtime language to the declaration.
1366 unsigned RLang = CTy.getRunTimeLang();
1367 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001368 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1369 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001370 }
1371}
1372
Manman Renffc9a712013-10-23 23:05:28 +00001373/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1374/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001375void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1376 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001377 DIE *ParamDIE =
1378 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001379 // Add the type if it exists, it could be void and therefore no type.
1380 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001381 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001382 if (!TP.getName().empty())
1383 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001384}
1385
Manman Renffc9a712013-10-23 23:05:28 +00001386/// constructTemplateValueParameterDIE - Construct new DIE for the given
1387/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001388void
1389DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001390 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001391 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001392
1393 // Add the type if there is one, template template and template parameter
1394 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001395 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001396 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001397 if (!VP.getName().empty())
1398 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1399 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001400 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001401 addConstantValue(ParamDIE, CI,
1402 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001403 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1404 // For declaration non-type template parameters (such as global values and
1405 // functions)
Eric Christopher4a741042014-02-16 08:46:55 +00001406 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
1407 addOpAddress(Loc, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001408 // Emit DW_OP_stack_value to use the address as the immediate value of the
1409 // parameter, rather than a pointer to it.
Eric Christopher4a741042014-02-16 08:46:55 +00001410 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1411 addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
Eric Christopherafb2c412013-08-08 07:40:31 +00001412 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001413 assert(isa<MDString>(Val));
1414 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1415 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001416 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001417 assert(isa<MDNode>(Val));
1418 DIArray A(cast<MDNode>(Val));
1419 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001420 }
1421 }
Devang Patel0e821f42011-04-12 23:21:44 +00001422}
1423
Devang Patel17b53272011-05-06 16:57:54 +00001424/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001425DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001426 // Construct the context before querying for the existence of the DIE in case
1427 // such construction creates the DIE.
1428 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001429
Devang Patel17b53272011-05-06 16:57:54 +00001430 DIE *NDie = getDIE(NS);
1431 if (NDie)
1432 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001433 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1434
Eric Christopher4996c702011-11-07 09:24:32 +00001435 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001436 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001437 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001438 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001439 } else
1440 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001441 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001442 return NDie;
1443}
1444
Devang Patel89543712011-08-15 17:24:54 +00001445/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001446DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001447 // Construct the context before querying for the existence of the DIE in case
1448 // such construction creates the DIE (as is the case for member function
1449 // declarations).
Adrian Prantld1e6a4e2014-03-14 23:08:25 +00001450 DIScope Context = resolve(SP.getContext());
1451 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantld486b342014-03-18 17:41:15 +00001452
Adrian Prantld1e6a4e2014-03-14 23:08:25 +00001453 // Unique declarations based on the ODR, where applicable.
Adrian Prantld486b342014-03-18 17:41:15 +00001454 SP = DISubprogram(DD->resolve(SP.getRef()));
1455 assert(SP.Verify());
David Blaikie309ffe42013-10-04 01:39:59 +00001456
Eric Christophere595bae2013-10-04 17:08:38 +00001457 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001458 if (SPDie)
1459 return SPDie;
1460
Manman Ren73d697c2013-10-29 00:58:04 +00001461 DISubprogram SPDecl = SP.getFunctionDeclaration();
1462 if (SPDecl.isSubprogram())
1463 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001464 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001465
1466 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001467 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001468
Rafael Espindola79278362011-11-10 22:34:29 +00001469 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001470 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001471 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001472
Devang Patel89543712011-08-15 17:24:54 +00001473 // Add function template parameters.
1474 addTemplateParams(*SPDie, SP.getTemplateParams());
1475
Adrian Prantld3dd11d2014-04-12 04:25:02 +00001476 // If this DIE is going to refer declaration info using AT_specification
1477 // then there is no need to add other attributes.
1478 if (DeclDie) {
Rafael Espindola79278362011-11-10 22:34:29 +00001479 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001480 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001481
Adrian Prantld3dd11d2014-04-12 04:25:02 +00001482 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001483 }
Devang Patel89543712011-08-15 17:24:54 +00001484
Adrian Prantld3dd11d2014-04-12 04:25:02 +00001485 // Add the linkage name if we have one.
1486 StringRef LinkageName = SP.getLinkageName();
1487 if (!LinkageName.empty())
1488 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1489 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001490
Devang Patel89543712011-08-15 17:24:54 +00001491 // Constructors and operators for anonymous aggregates do not have names.
1492 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001493 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001494
1495 addSourceLine(SPDie, SP);
1496
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001497 // Add the prototype if we have a prototype and we have a C like
1498 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001499 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001500 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001501 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001502 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001503 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001504
Devang Patel89543712011-08-15 17:24:54 +00001505 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001506 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1507 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001508
David Blaikie5174c842013-05-22 23:22:18 +00001509 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001510 // Add a return type. If this is a type like a C/C++ void type we don't add a
1511 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001512 if (Args.getElement(0))
1513 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001514
1515 unsigned VK = SP.getVirtuality();
1516 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001517 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Eric Christopher4a741042014-02-16 08:46:55 +00001518 DIELoc *Block = getDIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001519 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1520 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1521 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001522 ContainingTypeMap.insert(
1523 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001524 }
1525
1526 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001527 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001528
Devang Patel89543712011-08-15 17:24:54 +00001529 // Add arguments. Do not add arguments for subprogram definition. They will
1530 // be handled while processing variables.
Adrian Prantl3f49c892014-02-25 19:57:42 +00001531 constructSubprogramArguments(*SPDie, Args);
Devang Patel89543712011-08-15 17:24:54 +00001532 }
1533
1534 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001535 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001536
1537 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001538 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001539
1540 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001541 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001542
1543 if (unsigned isa = Asm->getISAEncoding()) {
1544 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1545 }
1546
Adrian Prantl99c7af22013-12-18 21:48:19 +00001547 if (SP.isLValueReference())
1548 addFlag(SPDie, dwarf::DW_AT_reference);
1549
1550 if (SP.isRValueReference())
1551 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1552
Adrian Prantlef129fb2014-01-18 02:12:00 +00001553 if (SP.isProtected())
1554 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1555 dwarf::DW_ACCESS_protected);
1556 else if (SP.isPrivate())
1557 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1558 dwarf::DW_ACCESS_private);
1559 else
1560 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1561 dwarf::DW_ACCESS_public);
1562
1563 if (SP.isExplicit())
1564 addFlag(SPDie, dwarf::DW_AT_explicit);
1565
Devang Patel89543712011-08-15 17:24:54 +00001566 return SPDie;
1567}
1568
Devang Pateldfd6ec32011-08-15 17:57:41 +00001569// Return const expression if value is a GEP to access merged global
1570// constant. e.g.
1571// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1572static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1573 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1574 if (!CE || CE->getNumOperands() != 3 ||
1575 CE->getOpcode() != Instruction::GetElementPtr)
1576 return NULL;
1577
1578 // First operand points to a global struct.
1579 Value *Ptr = CE->getOperand(0);
1580 if (!isa<GlobalValue>(Ptr) ||
1581 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1582 return NULL;
1583
1584 // Second operand is zero.
1585 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1586 if (!CI || !CI->isZero())
1587 return NULL;
1588
1589 // Third operand is offset.
1590 if (!isa<ConstantInt>(CE->getOperand(2)))
1591 return NULL;
1592
1593 return CE;
1594}
1595
1596/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001597void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001598 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001599 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001600 return;
1601
David Blaikie5e390e42014-02-04 01:23:52 +00001602 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001603
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001604 DIScope GVContext = GV.getContext();
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001605 DIType GTy = DD->resolve(GV.getType());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001606
1607 // If this is a static data member definition, some attributes belong
1608 // to the declaration DIE.
1609 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001610 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001611 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1612 if (SDMDecl.Verify()) {
1613 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1614 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001615 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001616 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001617 }
1618
1619 // If this is not a static data member definition, create the variable
1620 // DIE and add the initial set of attributes to it.
1621 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001622 // Construct the context before querying for the existence of the DIE in
1623 // case such construction creates the DIE.
1624 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001625
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001626 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001627 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001628
1629 // Add name and type.
1630 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1631 addType(VariableDIE, GTy);
1632
1633 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001634 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001635 addFlag(VariableDIE, dwarf::DW_AT_external);
1636
1637 // Add line number info.
1638 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001639 }
1640
Devang Pateldfd6ec32011-08-15 17:57:41 +00001641 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001642 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001643 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001644 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001645 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001646 addToAccelTable = true;
Eric Christopher4a741042014-02-16 08:46:55 +00001647 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001648 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001649 if (GV.getGlobal()->isThreadLocal()) {
1650 // FIXME: Make this work with -gsplit-dwarf.
1651 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1652 assert((PointerSize == 4 || PointerSize == 8) &&
1653 "Add support for other sizes if necessary");
1654 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001655 if (!DD->useSplitDwarf()) {
1656 // 1) Start with a constNu of the appropriate pointer size
Eric Christopher4a741042014-02-16 08:46:55 +00001657 addUInt(Loc, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001658 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001659 // 2) containing the (relocated) offset of the TLS variable
1660 // within the module's TLS block.
Eric Christopher4a741042014-02-16 08:46:55 +00001661 addExpr(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001662 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
David Blaikie8466ca82013-07-01 23:55:52 +00001663 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001664 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1665 addUInt(Loc, dwarf::DW_FORM_udata,
David Blaikief1a6dea2014-02-15 19:34:03 +00001666 DU->getAddrPoolIndex(Sym, /* TLS */ true));
David Blaikie8466ca82013-07-01 23:55:52 +00001667 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001668 // 3) followed by a custom OP to make the debugger do a TLS lookup.
Eric Christopher4a741042014-02-16 08:46:55 +00001669 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001670 } else {
1671 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001672 addOpAddress(Loc, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001673 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001674 // Do not create specification DIE if context is either compile unit
1675 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001676 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001677 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001678 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001679 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001680 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
Eric Christopher4a741042014-02-16 08:46:55 +00001681 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001682 // A static member's declaration is already flagged as such.
1683 if (!SDMDecl.Verify())
1684 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001685 } else {
Eric Christopher4a741042014-02-16 08:46:55 +00001686 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Eric Christopher4996c702011-11-07 09:24:32 +00001687 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001688 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001689 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001690 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001691 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1692 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1693 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001694 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1695 : VariableDIE,
Eric Christopheraf7eca22014-03-13 23:26:25 +00001696 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
1697 : dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001698 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001699 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001700 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001701 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001702 // emitting AT_const_value multiple times, we only add AT_const_value when
1703 // it is not a static member.
1704 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001705 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001706 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001707 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001708 // GV is a merged global.
Eric Christopher4a741042014-02-16 08:46:55 +00001709 DIELoc *Loc = new (DIEValueAllocator) DIELoc();
Devang Pateldfd6ec32011-08-15 17:57:41 +00001710 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001711 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1712 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christopher4a741042014-02-16 08:46:55 +00001713 addOpAddress(Loc, Sym);
1714 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001715 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
Eric Christopher4a741042014-02-16 08:46:55 +00001716 addUInt(Loc, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001717 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Eric Christopher4a741042014-02-16 08:46:55 +00001718 addUInt(Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1719 addBlock(VariableDIE, dwarf::DW_AT_location, Loc);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001720 }
1721
Eric Christopherc12c2112011-11-11 01:55:22 +00001722 if (addToAccelTable) {
1723 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1724 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001725
Eric Christopherc12c2112011-11-11 01:55:22 +00001726 // If the linkage name is different than the name, go ahead and output
1727 // that as well into the name table.
1728 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1729 addAccelName(GV.getLinkageName(), AddrDIE);
1730 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001731
1732 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001733 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1734 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001735}
1736
Devang Patel0e821f42011-04-12 23:21:44 +00001737/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001738void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001739 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001740 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001741
Bill Wendling28fe9e72012-12-06 07:38:10 +00001742 // The LowerBound value defines the lower bounds which is typically zero for
1743 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1744 // Count == -1 then the array is unbounded and we do not emit
1745 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1746 // Count == 0, then the array has zero elements in which case we do not emit
1747 // an upper bound.
1748 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001749 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001750 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001751
Bill Wendling3495f9b2012-12-06 07:55:19 +00001752 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001753 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001754
1755 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001756 // FIXME: An unbounded array should reference the expression that defines
1757 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001758 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1759 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001760}
1761
1762/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001763void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001764 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001765 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001766
Eric Christopher0df08e22013-08-08 07:40:37 +00001767 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001768 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001769
1770 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001771 // FIXME: This type should be passed down from the front end
1772 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001773 DIE *IdxTy = getIndexTyDie();
1774 if (!IdxTy) {
David Blaikie12e00fc2014-04-03 06:28:20 +00001775 // Construct an integer type to use for indexes.
David Blaikie2a80e442013-12-02 22:09:48 +00001776 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
David Blaikie12e00fc2014-04-03 06:28:20 +00001777 addString(IdxTy, dwarf::DW_AT_name, "sizetype");
1778 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001779 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
David Blaikie12e00fc2014-04-03 06:28:20 +00001780 dwarf::DW_ATE_unsigned);
Devang Patel0e821f42011-04-12 23:21:44 +00001781 setIndexTyDie(IdxTy);
1782 }
1783
1784 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001785 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001786 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1787 DIDescriptor Element = Elements.getElement(i);
1788 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1789 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1790 }
1791}
1792
Eric Christopheraeb105f2013-11-11 18:52:39 +00001793/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001794void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001795 DIArray Elements = CTy.getTypeArray();
1796
1797 // Add enumerators to enumeration type.
1798 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001799 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001800 if (Enum.isEnumerator()) {
1801 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001802 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001803 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001804 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001805 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1806 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001807 }
1808 }
1809 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1810 if (DTy) {
1811 addType(&Buffer, DTy);
1812 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1813 }
Devang Patel0e821f42011-04-12 23:21:44 +00001814}
1815
Devang Patel89543712011-08-15 17:24:54 +00001816/// constructContainingTypeDIEs - Construct DIEs for types that contain
1817/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001818void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001819 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001820 CE = ContainingTypeMap.end();
1821 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001822 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001823 DIDescriptor D(CI->second);
1824 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001825 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001826 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001827 if (!NDie)
1828 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001829 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001830 }
1831}
1832
Devang Patel3acc70e2011-08-15 22:04:40 +00001833/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001834DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001835 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001836
Devang Patel3acc70e2011-08-15 22:04:40 +00001837 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001838 DIE *VariableDie = new DIE(DV.getTag());
1839 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001840 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001841 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001842 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001843 else {
David Blaikie715528b2013-08-19 03:34:03 +00001844 if (!Name.empty())
1845 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001846 addSourceLine(VariableDie, DV.getVariable());
1847 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001848 }
1849
Eric Christopher34a2c872013-11-15 01:43:19 +00001850 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001851 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001852
1853 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001854 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001855 return VariableDie;
1856 }
1857
1858 // Add variable address.
1859
Eric Christopher34a2c872013-11-15 01:43:19 +00001860 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001861 if (Offset != ~0U) {
Eric Christophera27220f2014-03-05 22:41:20 +00001862 addLocationList(VariableDie, dwarf::DW_AT_location, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001863 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001864 return VariableDie;
1865 }
1866
Eric Christophercead0332011-10-03 15:49:20 +00001867 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001868 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001869 assert(DVInsn->getNumOperands() == 3);
1870 if (DVInsn->getOperand(0).isReg()) {
1871 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001872 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001873 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001874 MachineLocation Location(RegOp.getReg(),
1875 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001876 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001877 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001878 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001879 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001880 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001881 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001882 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001883 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001884 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001885 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001886
Eric Christopher34a2c872013-11-15 01:43:19 +00001887 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001888 return VariableDie;
1889 } else {
1890 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001891 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001892 if (FI != ~0) {
1893 unsigned FrameReg = 0;
1894 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001895 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001896 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001897 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001898 }
1899 }
1900
Eric Christopher34a2c872013-11-15 01:43:19 +00001901 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001902 return VariableDie;
1903}
1904
Manman Ren230ec862013-10-23 23:00:44 +00001905/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001906void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001907 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001908 StringRef Name = DT.getName();
1909 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001910 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001911
Manman Ren93b30902013-10-08 18:42:58 +00001912 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001913
1914 addSourceLine(MemberDie, DT);
1915
Eric Christopherc2697f82013-10-19 01:04:47 +00001916 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001917
1918 // For C++, virtual base classes are not at fixed offset. Use following
1919 // expression to extract appropriate offset from vtable.
1920 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1921
Eric Christopher4a741042014-02-16 08:46:55 +00001922 DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikief2443192013-10-21 17:28:37 +00001923 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1924 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1925 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1926 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1927 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1928 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1929 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001930
David Blaikief2443192013-10-21 17:28:37 +00001931 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001932 } else {
1933 uint64_t Size = DT.getSizeInBits();
1934 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1935 uint64_t OffsetInBytes;
1936
1937 if (Size != FieldSize) {
Eric Christopher1acdbb82014-03-12 17:14:46 +00001938 // Handle bitfield, assume bytes are 8 bits.
1939 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1940 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
David Blaikie71d34a22013-11-01 00:25:45 +00001941
1942 uint64_t Offset = DT.getOffsetInBits();
1943 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1944 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1945 uint64_t FieldOffset = (HiMark - FieldSize);
1946 Offset -= FieldOffset;
1947
1948 // Maybe we need to work from the other end.
1949 if (Asm->getDataLayout().isLittleEndian())
1950 Offset = FieldSize - (Offset + Size);
1951 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1952
Adrian Prantlc67655a2014-01-28 18:13:47 +00001953 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001954 // field that includes this bit field.
1955 OffsetInBytes = FieldOffset >> 3;
1956 } else
1957 // This is not a bitfield.
1958 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001959
David Blaikie22b29a52014-01-03 01:30:05 +00001960 if (DD->getDwarfVersion() <= 2) {
Eric Christopher4a741042014-02-16 08:46:55 +00001961 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc();
David Blaikie22b29a52014-01-03 01:30:05 +00001962 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1963 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1964 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1965 } else
1966 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1967 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001968 }
Devang Patel0e821f42011-04-12 23:21:44 +00001969
1970 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001971 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001972 dwarf::DW_ACCESS_protected);
1973 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001974 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001975 dwarf::DW_ACCESS_private);
1976 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001977 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001978 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001979 dwarf::DW_ACCESS_public);
1980 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001981 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001982 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001983
1984 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001985 if (MDNode *PNode = DT.getObjCProperty())
1986 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001987 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001988 PropertyDie);
1989
David Blaikie37fefc32012-12-13 22:43:07 +00001990 if (DT.isArtificial())
1991 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001992}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001993
Manman Renc6b63922013-10-14 20:33:57 +00001994/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001995DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001996 if (!DT.Verify())
1997 return NULL;
1998
Manman Renc6b63922013-10-14 20:33:57 +00001999 // Construct the context before querying for the existence of the DIE in case
2000 // such construction creates the DIE.
2001 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00002002 assert(dwarf::isType(ContextDIE->getTag()) &&
2003 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00002004
2005 DIE *StaticMemberDIE = getDIE(DT);
2006 if (StaticMemberDIE)
2007 return StaticMemberDIE;
2008
Manman Renb987e512013-10-29 00:53:03 +00002009 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00002010
Manman Ren93b30902013-10-08 18:42:58 +00002011 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002012
2013 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
2014 addType(StaticMemberDIE, Ty);
2015 addSourceLine(StaticMemberDIE, DT);
2016 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
2017 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
2018
2019 // FIXME: We could omit private if the parent is a class_type, and
2020 // public if the parent is something else.
2021 if (DT.isProtected())
2022 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2023 dwarf::DW_ACCESS_protected);
2024 else if (DT.isPrivate())
2025 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2026 dwarf::DW_ACCESS_private);
2027 else
2028 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
2029 dwarf::DW_ACCESS_public);
2030
2031 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00002032 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00002033 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
2034 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002035
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002036 return StaticMemberDIE;
2037}
David Blaikie6b288cf2013-10-30 20:42:41 +00002038
David Blaikied82b2372014-03-24 20:28:10 +00002039void DwarfUnit::emitHeader(const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00002040 Asm->OutStreamer.AddComment("DWARF version number");
2041 Asm->EmitInt16(DD->getDwarfVersion());
2042 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00002043 // We share one abbreviations table across all units so it's always at the
2044 // start of the section. Use a relocatable offset where needed to ensure
2045 // linking doesn't invalidate that offset.
David Blaikie3c9a3cc2014-03-24 20:53:02 +00002046 if (ASectionSym)
2047 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
2048 else
David Blaikie326e1fa2014-04-02 02:04:51 +00002049 // Use a constant value when no symbol is provided.
David Blaikie3c9a3cc2014-03-24 20:53:02 +00002050 Asm->EmitInt32(0);
David Blaikie6b288cf2013-10-30 20:42:41 +00002051 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2052 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2053}
David Blaikiebc563272013-12-13 21:33:40 +00002054
Eric Christopher384f3fe2014-03-20 19:16:16 +00002055void DwarfUnit::addRange(RangeSpan Range) {
2056 // Only add a range for this unit if we're emitting full debug.
2057 if (getCUNode().getEmissionKind() == DIBuilder::FullDebug) {
2058 // If we have no current ranges just add the range and return, otherwise,
2059 // check the current section and CU against the previous section and CU we
2060 // emitted into and the subprogram was contained within. If these are the
2061 // same then extend our current range, otherwise add this as a new range.
2062 if (CURanges.size() == 0 ||
2063 this != DD->getPrevCU() ||
2064 Asm->getCurrentSection() != DD->getPrevSection()) {
2065 CURanges.push_back(Range);
2066 return;
2067 }
2068
2069 assert(&(CURanges.back().getEnd()->getSection()) ==
2070 &(Range.getEnd()->getSection()) &&
2071 "We can only append to a range in the same section!");
2072 CURanges.back().setEnd(Range.getEnd());
2073 }
2074}
2075
David Blaikie2494fdb2014-02-14 22:41:51 +00002076void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
2077 // Define start line table label for each Compile Unit.
2078 MCSymbol *LineTableStartSym =
David Blaikie34641612014-04-01 08:07:52 +00002079 Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
David Blaikie2494fdb2014-02-14 22:41:51 +00002080
David Blaikie60e63862014-02-14 23:58:13 +00002081 stmtListIndex = UnitDie->getValues().size();
2082
David Blaikie2494fdb2014-02-14 22:41:51 +00002083 // DW_AT_stmt_list is a offset of line number information for this
2084 // compile unit in debug_line section. For split dwarf this is
2085 // left in the skeleton CU and so not included.
2086 // The line table entries are not always emitted in assembly, so it
2087 // is not okay to use line_table_start here.
2088 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
David Blaikie34641612014-04-01 08:07:52 +00002089 addSectionLabel(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym);
David Blaikie2494fdb2014-02-14 22:41:51 +00002090 else
2091 addSectionDelta(UnitDie.get(), dwarf::DW_AT_stmt_list, LineTableStartSym,
2092 DwarfLineSectionSym);
2093}
2094
David Blaikie60e63862014-02-14 23:58:13 +00002095void DwarfCompileUnit::applyStmtList(DIE &D) {
2096 D.addValue(dwarf::DW_AT_stmt_list,
2097 UnitDie->getAbbrev().getData()[stmtListIndex].getForm(),
2098 UnitDie->getValues()[stmtListIndex]);
2099}
2100
David Blaikied82b2372014-03-24 20:28:10 +00002101void DwarfTypeUnit::emitHeader(const MCSymbol *ASectionSym) const {
2102 DwarfUnit::emitHeader(ASectionSym);
David Blaikiebc563272013-12-13 21:33:40 +00002103 Asm->OutStreamer.AddComment("Type Signature");
2104 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2105 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00002106 // In a skeleton type unit there is no type DIE so emit a zero offset.
2107 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
2108 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00002109}
2110
2111void DwarfTypeUnit::initSection(const MCSection *Section) {
2112 assert(!this->Section);
2113 this->Section = Section;
2114 // Since each type unit is contained in its own COMDAT section, the begin
2115 // label and the section label are the same. Using the begin label emission in
2116 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2117 // the only other alternative of lazily constructing start-of-section labels
2118 // and storing a mapping in DwarfDebug (or AsmPrinter).
2119 this->SectionSym = this->LabelBegin =
2120 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2121 this->LabelEnd =
2122 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2123 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2124}