blob: e32582e76b1a5bea97bf181c346c198712e9f9f2 [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"
Bill Wendlingf799efd2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Eric Christopher84588622013-12-28 01:39:17 +000025#include "llvm/MC/MCAsmInfo.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000026#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCStreamer.h"
Devang Pateldfd6ec32011-08-15 17:57:41 +000028#include "llvm/Target/Mangler.h"
Devang Patel0e821f42011-04-12 23:21:44 +000029#include "llvm/Target/TargetFrameLowering.h"
30#include "llvm/Target/TargetMachine.h"
David Blaikief2694972013-06-28 20:05:11 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Devang Patel0e821f42011-04-12 23:21:44 +000032#include "llvm/Target/TargetRegisterInfo.h"
David Blaikie409dd9c2013-11-19 23:08:21 +000033#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034
35using namespace llvm;
36
Eric Christopher4287a492013-12-09 23:57:44 +000037static cl::opt<bool>
38GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
39 cl::desc("Generate DWARF4 type units."),
40 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000041
David Blaikie2a80e442013-12-02 22:09:48 +000042/// Unit - Unit constructor.
Eric Christophera5a79422013-12-09 23:32:48 +000043DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
44 DwarfDebug *DW, DwarfFile *DWU)
David Blaikie2a80e442013-12-02 22:09:48 +000045 : UniqueID(UID), Node(Node), UnitDie(D), DebugInfoOffset(0), Asm(A), DD(DW),
Eric Christopherd8667202013-12-30 17:22:27 +000046 DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000047 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
48}
49
Eric Christopher4287a492013-12-09 23:57:44 +000050DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
51 AsmPrinter *A, DwarfDebug *DW,
52 DwarfFile *DWU)
Eric Christophera5a79422013-12-09 23:32:48 +000053 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000054 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000055}
56
Eric Christopher4287a492013-12-09 23:57:44 +000057DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, uint16_t Language,
58 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
Eric Christophera5a79422013-12-09 23:32:48 +000059 : DwarfUnit(UID, D, DICompileUnit(), A, DW, DWU), Language(Language) {}
David Blaikie409dd9c2013-11-19 23:08:21 +000060
David Blaikie319a05f2013-12-02 19:33:10 +000061/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000062DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000063 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
64 DIEBlocks[j]->~DIEBlock();
65}
66
67/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
68/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000069DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000070 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
71 return Value;
72}
73
Bill Wendling3495f9b2012-12-06 07:55:19 +000074/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000075/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000076int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000077 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000078 default:
79 break;
80
81 case dwarf::DW_LANG_C89:
82 case dwarf::DW_LANG_C99:
83 case dwarf::DW_LANG_C:
84 case dwarf::DW_LANG_C_plus_plus:
85 case dwarf::DW_LANG_ObjC:
86 case dwarf::DW_LANG_ObjC_plus_plus:
87 return 0;
88
89 case dwarf::DW_LANG_Fortran77:
90 case dwarf::DW_LANG_Fortran90:
91 case dwarf::DW_LANG_Fortran95:
92 return 1;
93
94 // The languages below have valid values only if the DWARF version >= 4.
95 case dwarf::DW_LANG_Java:
96 case dwarf::DW_LANG_Python:
97 case dwarf::DW_LANG_UPC:
98 case dwarf::DW_LANG_D:
99 if (dwarf::DWARF_VERSION >= 4)
100 return 0;
101 break;
102
103 case dwarf::DW_LANG_Ada83:
104 case dwarf::DW_LANG_Ada95:
105 case dwarf::DW_LANG_Cobol74:
106 case dwarf::DW_LANG_Cobol85:
107 case dwarf::DW_LANG_Modula2:
108 case dwarf::DW_LANG_Pascal83:
109 case dwarf::DW_LANG_PLI:
110 if (dwarf::DWARF_VERSION >= 4)
111 return 1;
112 break;
113 }
114
115 return -1;
116}
117
Manman Ren4dbdc902013-10-31 17:54:35 +0000118/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000119static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000120 // When the MDNode can be part of the type system, the DIE can be shared
121 // across CUs.
122 // Combining type units and cross-CU DIE sharing is lower value (since
123 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
124 // level already) but may be implementable for some value in projects
125 // building multiple independent libraries with LTO and then linking those
126 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000127 return (D.isType() ||
128 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000129 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000130}
131
132/// getDIE - Returns the debug information entry map slot for the
133/// specified debug variable. We delegate the request to DwarfDebug
134/// when the DIE for this MDNode can be shared across CUs. The mappings
135/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000136DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000137 if (isShareableAcrossCUs(D))
138 return DD->getDIE(D);
139 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000140}
141
142/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
143/// when the DIE for this MDNode can be shared across CUs. The mappings
144/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000145void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000146 if (isShareableAcrossCUs(Desc)) {
147 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000148 return;
149 }
David Blaikie2ad00162013-11-15 23:09:13 +0000150 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000151}
152
Eric Christopherbb69a272012-08-24 01:14:27 +0000153/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000154void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000155 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000156 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000157 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000158 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000159}
160
Devang Patel0e821f42011-04-12 23:21:44 +0000161/// addUInt - Add an unsigned integer attribute data and value.
162///
Eric Christophera5a79422013-12-09 23:32:48 +0000163void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
164 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000165 if (!Form)
166 Form = DIEInteger::BestForm(false, Integer);
167 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
168 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000169 Die->addValue(Attribute, *Form, Value);
170}
171
Eric Christophera5a79422013-12-09 23:32:48 +0000172void DwarfUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000173 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000174}
175
176/// addSInt - Add an signed integer attribute data and value.
177///
Eric Christophera5a79422013-12-09 23:32:48 +0000178void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
179 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000180 if (!Form)
181 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000182 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000183 Die->addValue(Attribute, *Form, Value);
184}
185
Eric Christophera5a79422013-12-09 23:32:48 +0000186void DwarfUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
187 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000188 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000189}
190
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000191/// addString - Add a string attribute data and value. We always emit a
192/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000193/// more predictable sizes. In the case of split dwarf we emit an index
194/// into another table which gets us the static offset into the string
195/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000196void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
197 StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000198 DIEValue *Value;
David Blaikief2443192013-10-21 17:28:37 +0000199 dwarf::Form Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000200 if (!DD->useSplitDwarf()) {
201 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher84588622013-12-28 01:39:17 +0000202 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000203 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000204 else {
205 MCSymbol *StringPool = DU->getStringPoolSym();
206 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
207 }
Eric Christopher67646432013-07-26 17:02:41 +0000208 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000209 } else {
210 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000211 Value = new (DIEValueAllocator) DIEInteger(idx);
212 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000213 }
Eric Christopher67646432013-07-26 17:02:41 +0000214 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
215 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000216}
217
218/// addLocalString - Add a string attribute data and value. This is guaranteed
219/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000220void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
221 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000222 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000223 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000224 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000225 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000226 else {
Eric Christophere698f532012-12-20 21:58:36 +0000227 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000228 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000229 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000230 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000231}
232
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000233/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000234///
Eric Christophera5a79422013-12-09 23:32:48 +0000235void DwarfUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000236 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000237 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000238}
239
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000240/// addLabel - Add a Dwarf label attribute data and value.
241///
Eric Christophera5a79422013-12-09 23:32:48 +0000242void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
243 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000244 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
245 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000246}
247
Eric Christophera5a79422013-12-09 23:32:48 +0000248void DwarfUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
249 const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000250 addLabel(Die, (dwarf::Attribute)0, Form, Label);
251}
252
Eric Christopher33ff6972013-11-21 23:46:41 +0000253/// addSectionLabel - Add a Dwarf section label attribute data and value.
254///
Eric Christophera5a79422013-12-09 23:32:48 +0000255void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
256 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000257 if (DD->getDwarfVersion() >= 4)
258 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
259 else
260 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
261}
262
263/// addSectionOffset - Add an offset into a section attribute data and value.
264///
Eric Christophera5a79422013-12-09 23:32:48 +0000265void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
266 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000267 if (DD->getDwarfVersion() >= 4)
268 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
269 else
270 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
271}
272
Eric Christopher962c9082013-01-15 23:56:56 +0000273/// addLabelAddress - Add a dwarf label attribute data and value using
274/// DW_FORM_addr or DW_FORM_GNU_addr_index.
275///
Eric Christopher4287a492013-12-09 23:57:44 +0000276void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
277 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000278 if (Label)
279 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000280
Eric Christopher962c9082013-01-15 23:56:56 +0000281 if (!DD->useSplitDwarf()) {
282 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000283 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000284 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
285 } else {
286 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
287 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
288 }
289 } else {
290 unsigned idx = DU->getAddrPoolIndex(Label);
291 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
292 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
293 }
294}
295
Eric Christophere9ec2452013-01-18 22:11:33 +0000296/// addOpAddress - Add a dwarf op address data and value using the
297/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
298///
Eric Christophera5a79422013-12-09 23:32:48 +0000299void DwarfUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000300 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000301 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
302 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000303 } else {
David Blaikief2443192013-10-21 17:28:37 +0000304 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
305 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000306 }
307}
308
Eric Christopher33ff6972013-11-21 23:46:41 +0000309/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000310///
Eric Christophera5a79422013-12-09 23:32:48 +0000311void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
312 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000313 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000314 if (DD->getDwarfVersion() >= 4)
315 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
316 else
317 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000318}
319
320/// addDIEEntry - Add a DIE attribute data and value.
321///
Eric Christophera5a79422013-12-09 23:32:48 +0000322void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000323 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
324}
325
David Blaikie47f615e2013-12-17 23:32:35 +0000326void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
327 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
328 new (DIEValueAllocator) DIETypeSignature(Type));
329}
330
Eric Christophera5a79422013-12-09 23:32:48 +0000331void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
332 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000333 const DIE *DieCU = Die->getUnitOrNull();
334 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000335 if (!DieCU)
336 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000337 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000338 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000339 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000340 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
341 : dwarf::DW_FORM_ref_addr,
342 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000343}
344
Manman Renb987e512013-10-29 00:53:03 +0000345/// Create a DIE with the given Tag, add the DIE to its parent, and
346/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000347DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000348 DIE *Die = new DIE(Tag);
349 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000350 if (N)
351 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000352 return Die;
353}
354
Devang Patel0e821f42011-04-12 23:21:44 +0000355/// addBlock - Add block data.
356///
Eric Christophera5a79422013-12-09 23:32:48 +0000357void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
358 DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000359 Block->ComputeSize(Asm);
360 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
361 Die->addValue(Attribute, Block->BestForm(), Block);
362}
363
364/// addSourceLine - Add location information to specified debug information
365/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000366void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
Devang Patel0e821f42011-04-12 23:21:44 +0000367 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000368 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000369 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000370
Devang Patel0e821f42011-04-12 23:21:44 +0000371 unsigned Line = V.getLineNumber();
372 if (Line == 0)
373 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000374 unsigned FileID =
375 DD->getOrCreateSourceID(V.getContext().getFilename(),
376 V.getContext().getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000377 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000378 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
379 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000380}
381
382/// addSourceLine - Add location information to specified debug information
383/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000384void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
Devang Patel0e821f42011-04-12 23:21:44 +0000385 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000386 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000387 return;
388
389 unsigned Line = G.getLineNumber();
390 if (Line == 0)
391 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000392 unsigned FileID =
393 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000394 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000395 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
396 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000397}
398
399/// addSourceLine - Add location information to specified debug information
400/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000401void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
Devang Patel0e821f42011-04-12 23:21:44 +0000402 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000403 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000404 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000405
Devang Patel0e821f42011-04-12 23:21:44 +0000406 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000407 unsigned Line = SP.getLineNumber();
408 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000409 return;
410
Eric Christopherc2697f82013-10-19 01:04:47 +0000411 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
412 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000413 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000414 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
415 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000416}
417
418/// addSourceLine - Add location information to specified debug information
419/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000420void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
Devang Patel0e821f42011-04-12 23:21:44 +0000421 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000422 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000423 return;
424
425 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000426 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000427 return;
Eric Christopherc2697f82013-10-19 01:04:47 +0000428 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
429 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000430 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000431 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
432 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000433}
434
435/// addSourceLine - Add location information to specified debug information
436/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000437void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
Eric Christopher70e1bd82012-03-29 08:42:56 +0000438 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000439 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000440 return;
441
442 unsigned Line = Ty.getLineNumber();
443 if (Line == 0)
444 return;
445 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000446 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000447 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000448 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000449 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
450 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Eric Christopher70e1bd82012-03-29 08:42:56 +0000451}
452
453/// addSourceLine - Add location information to specified debug information
454/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000455void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
Devang Patel0e821f42011-04-12 23:21:44 +0000456 // Verify namespace.
457 if (!NS.Verify())
458 return;
459
460 unsigned Line = NS.getLineNumber();
461 if (Line == 0)
462 return;
463 StringRef FN = NS.getFilename();
464
Eric Christopherc2697f82013-10-19 01:04:47 +0000465 unsigned FileID =
466 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000467 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000468 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
469 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000470}
471
Eric Christopher92331fd2012-11-21 00:34:38 +0000472/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000473/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000474void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
475 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000476 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000477 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000478 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000479 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
480 else
David Blaikieea2605d2013-06-20 00:25:24 +0000481 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000482 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000483}
484
Devang Patelba5fbf12011-04-26 19:06:18 +0000485/// addRegisterOp - Add register operand.
Eric Christophera5a79422013-12-09 23:32:48 +0000486void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000487 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
488 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
489 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000490 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000491 else {
David Blaikief2443192013-10-21 17:28:37 +0000492 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
493 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000494 }
495}
496
497/// addRegisterOffset - Add register offset.
Eric Christophera5a79422013-12-09 23:32:48 +0000498void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
499 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000500 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
501 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
502 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
503 if (Reg == TRI->getFrameRegister(*Asm->MF))
504 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000505 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000506 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000507 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000508 else {
David Blaikief2443192013-10-21 17:28:37 +0000509 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
510 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000511 }
David Blaikief2443192013-10-21 17:28:37 +0000512 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000513}
514
515/// addAddress - Add an address attribute to a die based on the location
516/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000517void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
518 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000519 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
520
David Blaikieea2605d2013-06-20 00:25:24 +0000521 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000522 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000523 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000524 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000525 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000526 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000527 }
528 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000529
530 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000531 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000532}
533
Devang Patel0e821f42011-04-12 23:21:44 +0000534/// addComplexAddress - Start with the address based on the location provided,
535/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000536/// given the extra address information encoded in the DbgVariable, starting
537/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000538///
Eric Christophera5a79422013-12-09 23:32:48 +0000539void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
540 dwarf::Attribute Attribute,
541 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000542 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000543 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000544 unsigned i = 0;
545 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000546 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000547 // If first address element is OpPlus then emit
548 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000549 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000550 i = 2;
551 } else
552 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000553 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000554 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000555
Eric Christopherc2697f82013-10-19 01:04:47 +0000556 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000557 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000558 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000559 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
560 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000561 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000562 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000563 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000564 } else
565 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000566 }
567
568 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000569 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000570}
571
572/* Byref variables, in Blocks, are declared by the programmer as "SomeType
573 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
574 gives the variable VarName either the struct, or a pointer to the struct, as
575 its type. This is necessary for various behind-the-scenes things the
576 compiler needs to do with by-reference variables in Blocks.
577
578 However, as far as the original *programmer* is concerned, the variable
579 should still have type 'SomeType', as originally declared.
580
581 The function getBlockByrefType dives into the __Block_byref_x_VarName
582 struct to find the original type of the variable, which is then assigned to
583 the variable's Debug Information Entry as its real type. So far, so good.
584 However now the debugger will expect the variable VarName to have the type
585 SomeType. So we need the location attribute for the variable to be an
586 expression that explains to the debugger how to navigate through the
587 pointers and struct to find the actual variable of type SomeType.
588
589 The following function does just that. We start by getting
590 the "normal" location for the variable. This will be the location
591 of either the struct __Block_byref_x_VarName or the pointer to the
592 struct __Block_byref_x_VarName.
593
594 The struct will look something like:
595
596 struct __Block_byref_x_VarName {
597 ... <various fields>
598 struct __Block_byref_x_VarName *forwarding;
599 ... <various other fields>
600 SomeType VarName;
601 ... <maybe more fields>
602 };
603
604 If we are given the struct directly (as our starting point) we
605 need to tell the debugger to:
606
607 1). Add the offset of the forwarding field.
608
609 2). Follow that pointer to get the real __Block_byref_x_VarName
610 struct to use (the real one may have been copied onto the heap).
611
612 3). Add the offset for the field VarName, to find the actual variable.
613
614 If we started with a pointer to the struct, then we need to
615 dereference that pointer first, before the other steps.
616 Translating this into DWARF ops, we will need to append the following
617 to the current location description for the variable:
618
619 DW_OP_deref -- optional, if we start with a pointer
620 DW_OP_plus_uconst <forward_fld_offset>
621 DW_OP_deref
622 DW_OP_plus_uconst <varName_fld_offset>
623
624 That is what this function does. */
625
626/// addBlockByrefAddress - Start with the address based on the location
627/// provided, and generate the DWARF information necessary to find the
628/// actual Block variable (navigating the Block struct) based on the
629/// starting location. Add the DWARF information to the die. For
630/// more information, read large comment just above here.
631///
Eric Christophera5a79422013-12-09 23:32:48 +0000632void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
633 dwarf::Attribute Attribute,
634 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000635 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000636 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000637 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000638 bool isPointer = false;
639
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000640 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000641
642 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000643 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000644 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000645 isPointer = true;
646 }
647
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000648 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000649
650 // Find the __forwarding field and the variable field in the __Block_byref
651 // struct.
652 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000653 DIDerivedType varField;
654 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000655
656 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000657 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000658 StringRef fieldName = DT.getName();
659 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000660 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000661 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000662 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000663 }
664
665 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000666 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
667 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000668
669 // Decode the original location, and use that as the start of the byref
670 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000671 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
672
Eric Christopheref9d7102012-07-04 02:02:18 +0000673 if (Location.isReg())
674 addRegisterOp(Block, Location.getReg());
675 else
676 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000677
678 // If we started with a pointer to the __Block_byref... struct, then
679 // the first thing we need to do is dereference the pointer (DW_OP_deref).
680 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000681 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000682
683 // Next add the offset for the '__forwarding' field:
684 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
685 // adding the offset if it's 0.
686 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000687 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
688 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000689 }
690
691 // Now dereference the __forwarding field to get to the real __Block_byref
692 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000693 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000694
695 // Now that we've got the real __Block_byref... struct, add the offset
696 // for the variable's field to get to the location of the actual variable:
697 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
698 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000699 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
700 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000701 }
702
703 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000704 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000705}
706
Devang Patelbcd50a12011-07-20 21:57:04 +0000707/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000708static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000709 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000710 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
711 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000712 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000713 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
714 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000715 *SizeInBits = Ty.getSizeInBits();
716 return true;
717 }
718 return false;
719}
720
Manman Renb3388602013-10-05 01:43:03 +0000721/// Return true if type encoding is unsigned.
722static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
723 DIDerivedType DTy(Ty);
724 if (DTy.isDerivedType())
725 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
726
727 DIBasicType BTy(Ty);
728 if (BTy.isBasicType()) {
729 unsigned Encoding = BTy.getEncoding();
730 if (Encoding == dwarf::DW_ATE_unsigned ||
731 Encoding == dwarf::DW_ATE_unsigned_char ||
732 Encoding == dwarf::DW_ATE_boolean)
733 return true;
734 }
735 return false;
736}
737
738/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000739static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000740 unsigned Tag = Ty.getTag();
741
742 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
743 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
744 Tag != dwarf::DW_TAG_restrict_type)
745 return Ty.getSizeInBits();
746
747 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
748
749 // If this type is not derived from any type then take conservative approach.
750 if (!BaseType.isValid())
751 return Ty.getSizeInBits();
752
753 // If this is a derived type, go ahead and get the base type, unless it's a
754 // reference then it's just the size of the field. Pointer types have no need
755 // of this since they're a different type of qualification on the type.
756 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
757 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
758 return Ty.getSizeInBits();
759
760 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000761 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000762
763 return BaseType.getSizeInBits();
764}
765
Devang Patel0e821f42011-04-12 23:21:44 +0000766/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000767void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
768 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000769 // FIXME: This is a bit conservative/simple - it emits negative values at
770 // their maximum bit width which is a bit unfortunate (& doesn't prefer
771 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000772 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000773 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000774 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000775 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000776
Eric Christopher9d1daa82013-08-27 23:49:04 +0000777 // If we're a signed constant definitely use sdata.
778 if (SignedConstant) {
779 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
780 return;
781 }
782
783 // Else use data for now unless it's larger than we can deal with.
784 switch (SizeInBits) {
785 case 8:
786 Form = dwarf::DW_FORM_data1;
787 break;
788 case 16:
789 Form = dwarf::DW_FORM_data2;
790 break;
791 case 32:
792 Form = dwarf::DW_FORM_data4;
793 break;
794 case 64:
795 Form = dwarf::DW_FORM_data8;
796 break;
797 default:
798 Form = dwarf::DW_FORM_udata;
799 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
800 return;
801 }
802 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000803}
804
805/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000806void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000807 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000808 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
809 APFloat FPImm = MO.getFPImm()->getValueAPF();
810
811 // Get the raw data form of the floating point.
812 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000813 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000814
815 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000816 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000817 int Incr = (LittleEndian ? 1 : -1);
818 int Start = (LittleEndian ? 0 : NumBytes - 1);
819 int Stop = (LittleEndian ? NumBytes : -1);
820
821 // Output the constant to DWARF one byte at a time.
822 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000823 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000824
David Blaikief2443192013-10-21 17:28:37 +0000825 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000826}
827
David Blaikiea39a76e2013-01-20 01:18:01 +0000828/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000829void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000830 // Pass this down to addConstantValue as an unsigned bag of bits.
831 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000832}
833
Devang Patel0e821f42011-04-12 23:21:44 +0000834/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000835void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
836 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000837 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000838}
839
840// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000841void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000842 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000843 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000844 // If we're a signed constant definitely use sdata.
845 if (!Unsigned) {
846 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
847 Val.getSExtValue());
848 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000849 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000850
851 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000852 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000853 switch (CIBitWidth) {
854 case 8:
855 Form = dwarf::DW_FORM_data1;
856 break;
857 case 16:
858 Form = dwarf::DW_FORM_data2;
859 break;
860 case 32:
861 Form = dwarf::DW_FORM_data4;
862 break;
863 case 64:
864 Form = dwarf::DW_FORM_data8;
865 break;
866 default:
867 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
868 Val.getZExtValue());
869 return;
870 }
871 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000872 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000873 }
874
875 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
876
877 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000878 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000879
880 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000881 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000882
883 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000884 for (int i = 0; i < NumBytes; i++) {
885 uint8_t c;
886 if (LittleEndian)
887 c = Ptr64[i / 8] >> (8 * (i & 7));
888 else
889 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000890 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000891 }
Devang Patel0e821f42011-04-12 23:21:44 +0000892
David Blaikief2443192013-10-21 17:28:37 +0000893 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000894}
895
Eric Christopher25e35092013-04-22 07:47:40 +0000896/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000897void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000898 // Add template parameters.
899 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
900 DIDescriptor Element = TParams.getElement(i);
901 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000902 constructTemplateTypeParameterDIE(Buffer,
903 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000904 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000905 constructTemplateValueParameterDIE(Buffer,
906 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000907 }
Devang Patel0e821f42011-04-12 23:21:44 +0000908}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000909
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000910/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000911DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000912 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000913 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000914 if (Context.isType())
915 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000916 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000917 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000918 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000919 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000920 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000921}
922
Eric Christophera5a79422013-12-09 23:32:48 +0000923DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000924 DIScope Context = resolve(Ty.getContext());
925 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000926
927 DIE *TyDIE = getDIE(Ty);
928 if (TyDIE)
929 return TyDIE;
930
931 // Create new type.
932 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
933
David Blaikiefbd29eb2013-11-26 00:35:04 +0000934 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000935
David Blaikie9d861be2013-11-26 00:15:27 +0000936 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000937 return TyDIE;
938}
939
David Blaikie8a263cb2013-11-26 00:22:37 +0000940/// Return true if the type is appropriately scoped to be contained inside
941/// its own type unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000942static bool isDwarfTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
David Blaikie8a263cb2013-11-26 00:22:37 +0000943 DIScope Parent = DD->resolve(Ty.getContext());
944 while (Parent) {
945 // Don't generate a hash for anything scoped inside a function.
946 if (Parent.isSubprogram())
947 return false;
948 Parent = DD->resolve(Parent.getContext());
949 }
950 return true;
951}
952
953/// Return true if the type should be split out into a type unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000954static bool shouldCreateDwarfTypeUnit(DICompositeType CTy,
955 const DwarfDebug *DD) {
956 if (!GenerateDwarfTypeUnits)
David Blaikie8a263cb2013-11-26 00:22:37 +0000957 return false;
958
959 uint16_t Tag = CTy.getTag();
960
961 switch (Tag) {
962 case dwarf::DW_TAG_structure_type:
963 case dwarf::DW_TAG_union_type:
964 case dwarf::DW_TAG_enumeration_type:
965 case dwarf::DW_TAG_class_type:
966 // If this is a class, structure, union, or enumeration type
967 // that is a definition (not a declaration), and not scoped
968 // inside a function then separate this out as a type unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000969 return !CTy.isForwardDecl() && isDwarfTypeUnitScoped(CTy, DD);
David Blaikie8a263cb2013-11-26 00:22:37 +0000970 default:
971 return false;
972 }
973}
974
Devang Patel0e821f42011-04-12 23:21:44 +0000975/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
976/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000977DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000978 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000979 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000980
David Blaikie32887552013-11-14 22:25:02 +0000981 DIType Ty(TyNode);
982 assert(Ty.isType());
983
Manman Renf4c339e2013-10-29 22:49:29 +0000984 // Construct the context before querying for the existence of the DIE in case
985 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000986 DIScope Context = resolve(Ty.getContext());
987 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000988 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000989
Eric Christophere595bae2013-10-04 17:08:38 +0000990 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000991 if (TyDIE)
992 return TyDIE;
993
994 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000995 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
996
Devang Patel0e821f42011-04-12 23:21:44 +0000997 if (Ty.isBasicType())
998 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000999 else if (Ty.isCompositeType()) {
1000 DICompositeType CTy(Ty);
Eric Christopher4287a492013-12-09 23:57:44 +00001001 if (shouldCreateDwarfTypeUnit(CTy, DD)) {
1002 DD->addDwarfTypeUnitType(getLanguage(), TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +00001003 // Skip updating the accellerator tables since this is not the full type
1004 return TyDIE;
1005 }
David Blaikiefbd29eb2013-11-26 00:35:04 +00001006 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +00001007 } else {
Devang Patel0e821f42011-04-12 23:21:44 +00001008 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1009 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1010 }
David Blaikie2ea848b2013-11-19 22:51:04 +00001011
David Blaikie9d861be2013-11-26 00:15:27 +00001012 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +00001013
1014 return TyDIE;
1015}
1016
Eric Christophera5a79422013-12-09 23:32:48 +00001017void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1018 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +00001019 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1020 bool IsImplementation = 0;
1021 if (Ty.isCompositeType()) {
1022 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001023 // A runtime language of 0 actually means C/C++ and that any
1024 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +00001025 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +00001026 }
Eric Christophercf7289f2013-09-05 18:20:16 +00001027 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +00001028 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +00001029
1030 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1031 Context.isNameSpace())
1032 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +00001033 }
Devang Patel0e821f42011-04-12 23:21:44 +00001034}
1035
1036/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +00001037void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +00001038 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +00001039
1040 // Check for pre-existence.
1041 DIEEntry *Entry = getDIEEntry(Ty);
1042 // If it exists then use the existing value.
1043 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +00001044 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +00001045 return;
1046 }
1047
1048 // Construct type.
1049 DIE *Buffer = getOrCreateTypeDIE(Ty);
1050
1051 // Set up proxy.
1052 Entry = createDIEEntry(Buffer);
1053 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001054 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001055}
1056
Eric Christopher9cd26af2013-09-20 23:22:52 +00001057// Accelerator table mutators - add each name along with its companion
1058// DIE to the proper table while ensuring that the name that we're going
1059// to reference is in the string table. We do this since the names we
1060// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001061void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001062 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001063 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001064 DIEs.push_back(Die);
1065}
1066
Eric Christophera5a79422013-12-09 23:32:48 +00001067void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001068 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001069 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001070 DIEs.push_back(Die);
1071}
1072
Eric Christophera5a79422013-12-09 23:32:48 +00001073void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001074 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001075 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001076 DIEs.push_back(Die);
1077}
1078
Eric Christophera5a79422013-12-09 23:32:48 +00001079void DwarfUnit::addAccelType(StringRef Name,
1080 std::pair<const DIE *, unsigned> Die) {
Eric Christopher9cd26af2013-09-20 23:22:52 +00001081 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001082 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001083 DIEs.push_back(Die);
1084}
1085
Eric Christopher9c58f312013-09-20 22:20:55 +00001086/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001087void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001088 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001089 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001090}
1091
Eric Christopher2c8b7902013-10-17 02:06:06 +00001092/// getParentContextString - Walks the metadata parent chain in a language
1093/// specific manner (using the compile unit language) and returns
1094/// it as a string. This is done at the metadata level because DIEs may
1095/// not currently have been added to the parent context and walking the
1096/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001097std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001098 if (!Context)
1099 return "";
1100
1101 // FIXME: Decide whether to implement this for non-C++ languages.
1102 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1103 return "";
1104
Eric Christopher8dba0d52013-10-19 01:04:42 +00001105 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001106 SmallVector<DIScope, 1> Parents;
1107 while (!Context.isCompileUnit()) {
1108 Parents.push_back(Context);
1109 if (Context.getContext())
1110 Context = resolve(Context.getContext());
1111 else
1112 // Structure, etc types will have a NULL context if they're at the top
1113 // level.
1114 break;
1115 }
1116
1117 // Reverse iterate over our list to go from the outermost construct to the
1118 // innermost.
1119 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1120 E = Parents.rend();
1121 I != E; ++I) {
1122 DIScope Ctx = *I;
1123 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001124 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001125 CS += Name;
1126 CS += "::";
1127 }
1128 }
1129 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001130}
1131
1132/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001133void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001134 // Get core information.
1135 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001136 // Add name if not anonymous or intermediate type.
1137 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001138 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001139
David Blaikiefac56122013-10-04 23:21:16 +00001140 // An unspecified type only has a name attribute.
1141 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001142 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001143
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001144 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001145 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001146
Devang Patel0e821f42011-04-12 23:21:44 +00001147 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001148 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001149}
1150
1151/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001152void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001153 // Get core information.
1154 StringRef Name = DTy.getName();
1155 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001156 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001157
1158 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001159 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001160 if (FromTy)
1161 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001162
1163 // Add name if not anonymous or intermediate type.
1164 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001165 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001166
1167 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001168 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001169 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001170
David Blaikie5d3249b2013-01-07 05:51:15 +00001171 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001172 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1173 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001174 // Add source line info if available and TyDesc is not a forward declaration.
1175 if (!DTy.isForwardDecl())
1176 addSourceLine(&Buffer, DTy);
1177}
1178
1179/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001180void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001181 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001182 StringRef Name = CTy.getName();
1183
1184 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001185 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001186
1187 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001188 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001189 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001190 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001191 case dwarf::DW_TAG_enumeration_type:
1192 constructEnumTypeDIE(Buffer, CTy);
1193 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001194 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001195 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001196 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001197 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001198 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001199 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001200
1201 bool isPrototyped = true;
1202 // Add arguments.
1203 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1204 DIDescriptor Ty = Elements.getElement(i);
1205 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001206 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001207 isPrototyped = false;
1208 } else {
Manman Renb987e512013-10-29 00:53:03 +00001209 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001210 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001211 if (DIType(Ty).isArtificial())
1212 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001213 }
1214 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001215 // Add prototype flag if we're dealing with a C language and the
1216 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001217 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001218 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001219 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001220 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001221 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001222
1223 if (CTy.isLValueReference())
1224 addFlag(&Buffer, dwarf::DW_AT_reference);
1225
1226 if (CTy.isRValueReference())
1227 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001228 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001229 case dwarf::DW_TAG_structure_type:
1230 case dwarf::DW_TAG_union_type:
1231 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001232 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001233 DIArray Elements = CTy.getTypeArray();
1234 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001235 DIDescriptor Element = Elements.getElement(i);
1236 DIE *ElemDie = NULL;
1237 if (Element.isSubprogram()) {
1238 DISubprogram SP(Element);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001239 ElemDie = getOrCreateSubprogramDIE(SP);
Devang Patel0e821f42011-04-12 23:21:44 +00001240 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001241 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001242 dwarf::DW_ACCESS_protected);
1243 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001244 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001245 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001246 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001247 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Eric Christopherc2697f82013-10-19 01:04:47 +00001248 dwarf::DW_ACCESS_public);
Devang Patel0e821f42011-04-12 23:21:44 +00001249 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001250 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001251 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001252 DIDerivedType DDTy(Element);
1253 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001254 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001255 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001256 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001257 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001258 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001259 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001260 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001261 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001262 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001263 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001264 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001265 StringRef PropertyName = Property.getObjCPropertyName();
1266 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001267 addType(ElemDie, Property.getType());
1268 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001269 StringRef GetterName = Property.getObjCPropertyGetterName();
1270 if (!GetterName.empty())
1271 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1272 StringRef SetterName = Property.getObjCPropertySetterName();
1273 if (!SetterName.empty())
1274 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1275 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001276 if (Property.isReadOnlyObjCProperty())
1277 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1278 if (Property.isReadWriteObjCProperty())
1279 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1280 if (Property.isAssignObjCProperty())
1281 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1282 if (Property.isRetainObjCProperty())
1283 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1284 if (Property.isCopyObjCProperty())
1285 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1286 if (Property.isNonAtomicObjCProperty())
1287 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1288 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001289 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001290 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001291
Devang Pateld925d1a2012-02-07 23:33:58 +00001292 DIEEntry *Entry = getDIEEntry(Element);
1293 if (!Entry) {
1294 Entry = createDIEEntry(ElemDie);
1295 insertDIEEntry(Element, Entry);
1296 }
Devang Patel403e8192012-02-04 01:30:32 +00001297 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001298 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001299 }
1300
1301 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001302 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001303
Eric Christopher9e429ae2013-10-05 00:27:02 +00001304 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001305 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001306 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001307 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001308
Devang Patel12419ae2011-05-12 21:29:42 +00001309 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001310 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001311
Eric Christopherda011dd2011-12-16 23:42:42 +00001312 // Add template parameters to a class, structure or union types.
1313 // FIXME: The support isn't in the metadata for this yet.
1314 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001315 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001316 addTemplateParams(Buffer, CTy.getTemplateParams());
1317
1318 break;
1319 }
1320 default:
1321 break;
1322 }
1323
1324 // Add name if not anonymous or intermediate type.
1325 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001326 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001327
Eric Christopher775cbd22012-05-22 18:45:18 +00001328 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001329 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001330 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001331 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001332 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001333 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001334 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001335 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001336 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001337 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001338
1339 // If we're a forward decl, say so.
1340 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001341 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001342
1343 // Add source line info if available.
1344 if (!CTy.isForwardDecl())
1345 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001346
1347 // No harm in adding the runtime language to the declaration.
1348 unsigned RLang = CTy.getRunTimeLang();
1349 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001350 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1351 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001352 }
1353}
1354
Manman Renffc9a712013-10-23 23:05:28 +00001355/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1356/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001357void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1358 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001359 DIE *ParamDIE =
1360 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001361 // Add the type if it exists, it could be void and therefore no type.
1362 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001363 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001364 if (!TP.getName().empty())
1365 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001366}
1367
Manman Renffc9a712013-10-23 23:05:28 +00001368/// constructTemplateValueParameterDIE - Construct new DIE for the given
1369/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001370void
1371DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001372 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001373 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001374
1375 // Add the type if there is one, template template and template parameter
1376 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001377 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001378 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001379 if (!VP.getName().empty())
1380 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1381 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001382 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001383 addConstantValue(ParamDIE, CI,
1384 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001385 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1386 // For declaration non-type template parameters (such as global values and
1387 // functions)
1388 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001389 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001390 // Emit DW_OP_stack_value to use the address as the immediate value of the
1391 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001392 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1393 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001394 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001395 assert(isa<MDString>(Val));
1396 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1397 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001398 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001399 assert(isa<MDNode>(Val));
1400 DIArray A(cast<MDNode>(Val));
1401 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001402 }
1403 }
Devang Patel0e821f42011-04-12 23:21:44 +00001404}
1405
Devang Patel17b53272011-05-06 16:57:54 +00001406/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001407DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001408 // Construct the context before querying for the existence of the DIE in case
1409 // such construction creates the DIE.
1410 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001411
Devang Patel17b53272011-05-06 16:57:54 +00001412 DIE *NDie = getDIE(NS);
1413 if (NDie)
1414 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001415 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1416
Eric Christopher4996c702011-11-07 09:24:32 +00001417 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001418 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001419 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001420 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001421 } else
1422 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001423 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001424 return NDie;
1425}
1426
Devang Patel89543712011-08-15 17:24:54 +00001427/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001428DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001429 // Construct the context before querying for the existence of the DIE in case
1430 // such construction creates the DIE (as is the case for member function
1431 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001432 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001433
Eric Christophere595bae2013-10-04 17:08:38 +00001434 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001435 if (SPDie)
1436 return SPDie;
1437
Manman Ren73d697c2013-10-29 00:58:04 +00001438 DISubprogram SPDecl = SP.getFunctionDeclaration();
1439 if (SPDecl.isSubprogram())
1440 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001441 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001442
1443 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001444 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001445
Rafael Espindola79278362011-11-10 22:34:29 +00001446 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001447 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001448 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001449
Devang Patel89543712011-08-15 17:24:54 +00001450 // Add function template parameters.
1451 addTemplateParams(*SPDie, SP.getTemplateParams());
1452
Devang Patel89543712011-08-15 17:24:54 +00001453 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001454 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001455 if (DeclDie) {
1456 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001457 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001458
Devang Patel89543712011-08-15 17:24:54 +00001459 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001460 }
Devang Patel89543712011-08-15 17:24:54 +00001461
Eric Christopheracb71152012-08-23 22:52:55 +00001462 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001463 StringRef LinkageName = SP.getLinkageName();
1464 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001465 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001466 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001467
Devang Patel89543712011-08-15 17:24:54 +00001468 // Constructors and operators for anonymous aggregates do not have names.
1469 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001470 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001471
1472 addSourceLine(SPDie, SP);
1473
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001474 // Add the prototype if we have a prototype and we have a C like
1475 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001476 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001477 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001478 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001479 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001480 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001481
Devang Patel89543712011-08-15 17:24:54 +00001482 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001483 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1484 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001485
David Blaikie5174c842013-05-22 23:22:18 +00001486 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001487 // Add a return type. If this is a type like a C/C++ void type we don't add a
1488 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001489 if (Args.getElement(0))
1490 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001491
1492 unsigned VK = SP.getVirtuality();
1493 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001494 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001495 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001496 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1497 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1498 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001499 ContainingTypeMap.insert(
1500 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001501 }
1502
1503 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001504 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001505
Devang Patel89543712011-08-15 17:24:54 +00001506 // Add arguments. Do not add arguments for subprogram definition. They will
1507 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001508 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001509 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001510 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001511 addType(Arg, ATy);
1512 if (ATy.isArtificial())
1513 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001514 }
Devang Patel89543712011-08-15 17:24:54 +00001515 }
1516
1517 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001518 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001519
1520 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001521 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001522
1523 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001524 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001525
1526 if (unsigned isa = Asm->getISAEncoding()) {
1527 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1528 }
1529
Adrian Prantl99c7af22013-12-18 21:48:19 +00001530 if (SP.isLValueReference())
1531 addFlag(SPDie, dwarf::DW_AT_reference);
1532
1533 if (SP.isRValueReference())
1534 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1535
Devang Patel89543712011-08-15 17:24:54 +00001536 return SPDie;
1537}
1538
Devang Pateldfd6ec32011-08-15 17:57:41 +00001539// Return const expression if value is a GEP to access merged global
1540// constant. e.g.
1541// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1542static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1543 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1544 if (!CE || CE->getNumOperands() != 3 ||
1545 CE->getOpcode() != Instruction::GetElementPtr)
1546 return NULL;
1547
1548 // First operand points to a global struct.
1549 Value *Ptr = CE->getOperand(0);
1550 if (!isa<GlobalValue>(Ptr) ||
1551 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1552 return NULL;
1553
1554 // Second operand is zero.
1555 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1556 if (!CI || !CI->isZero())
1557 return NULL;
1558
1559 // Third operand is offset.
1560 if (!isa<ConstantInt>(CE->getOperand(2)))
1561 return NULL;
1562
1563 return CE;
1564}
1565
1566/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001567void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001568 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001569 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001570 return;
1571
Manman Ren7504ed42013-07-08 18:33:29 +00001572 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001573 return;
1574
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001575 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001576 DIType GTy = GV.getType();
1577
1578 // If this is a static data member definition, some attributes belong
1579 // to the declaration DIE.
1580 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001581 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001582 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1583 if (SDMDecl.Verify()) {
1584 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1585 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001586 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001587 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001588 }
1589
1590 // If this is not a static data member definition, create the variable
1591 // DIE and add the initial set of attributes to it.
1592 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001593 // Construct the context before querying for the existence of the DIE in
1594 // case such construction creates the DIE.
1595 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001596
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001597 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001598 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001599
1600 // Add name and type.
1601 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1602 addType(VariableDIE, GTy);
1603
1604 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001605 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001606 addFlag(VariableDIE, dwarf::DW_AT_external);
1607
1608 // Add line number info.
1609 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001610 }
1611
Devang Pateldfd6ec32011-08-15 17:57:41 +00001612 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001613 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001614 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001615 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001616 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001617 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001618 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001619 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001620 if (GV.getGlobal()->isThreadLocal()) {
1621 // FIXME: Make this work with -gsplit-dwarf.
1622 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1623 assert((PointerSize == 4 || PointerSize == 8) &&
1624 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001625 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001626 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001627 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001628 if (!DD->useSplitDwarf()) {
1629 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001630 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001631 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001632 // 2) containing the (relocated) offset of the TLS variable
1633 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001634 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001635 } else {
David Blaikief2443192013-10-21 17:28:37 +00001636 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1637 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001638 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001639 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001640 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001641 } else {
1642 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001643 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001644 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001645 // Do not create specification DIE if context is either compile unit
1646 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001647 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001648 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001649 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001650 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001651 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001652 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001653 // A static member's declaration is already flagged as such.
1654 if (!SDMDecl.Verify())
1655 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001656 } else {
David Blaikief2443192013-10-21 17:28:37 +00001657 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001658 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001659 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001660 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001661 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001662 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1663 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1664 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001665 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1666 : VariableDIE,
1667 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001668 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001669 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001670 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001671 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001672 // emitting AT_const_value multiple times, we only add AT_const_value when
1673 // it is not a static member.
1674 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001675 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001676 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001677 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001678 // GV is a merged global.
1679 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1680 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001681 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1682 DD->addArangeLabel(SymbolCU(this, Sym));
1683 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001684 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001685 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001686 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001687 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001688 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1689 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001690 }
1691
Eric Christopherc12c2112011-11-11 01:55:22 +00001692 if (addToAccelTable) {
1693 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1694 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001695
Eric Christopherc12c2112011-11-11 01:55:22 +00001696 // If the linkage name is different than the name, go ahead and output
1697 // that as well into the name table.
1698 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1699 addAccelName(GV.getLinkageName(), AddrDIE);
1700 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001701
1702 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001703 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1704 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001705}
1706
Devang Patel0e821f42011-04-12 23:21:44 +00001707/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001708void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001709 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001710 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001711
Bill Wendling28fe9e72012-12-06 07:38:10 +00001712 // The LowerBound value defines the lower bounds which is typically zero for
1713 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1714 // Count == -1 then the array is unbounded and we do not emit
1715 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1716 // Count == 0, then the array has zero elements in which case we do not emit
1717 // an upper bound.
1718 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001719 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001720 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001721
Bill Wendling3495f9b2012-12-06 07:55:19 +00001722 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001723 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001724
1725 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001726 // FIXME: An unbounded array should reference the expression that defines
1727 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001728 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1729 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001730}
1731
1732/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001733void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001734 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001735 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001736
Eric Christopher0df08e22013-08-08 07:40:37 +00001737 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001738 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001739
1740 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001741 // FIXME: This type should be passed down from the front end
1742 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001743 DIE *IdxTy = getIndexTyDie();
1744 if (!IdxTy) {
1745 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001746 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001747 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001748 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001749 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1750 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001751 setIndexTyDie(IdxTy);
1752 }
1753
1754 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001755 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001756 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1757 DIDescriptor Element = Elements.getElement(i);
1758 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1759 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1760 }
1761}
1762
Eric Christopheraeb105f2013-11-11 18:52:39 +00001763/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001764void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001765 DIArray Elements = CTy.getTypeArray();
1766
1767 // Add enumerators to enumeration type.
1768 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001769 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001770 if (Enum.isEnumerator()) {
1771 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001772 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001773 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001774 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001775 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1776 Value);
Adrian Prantlad64aea2013-12-23 23:50:20 +00001777
1778 // Add the enumerator to the __apple_names accelerator table.
1779 addAccelName(Name, Enumerator);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001780 }
1781 }
1782 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1783 if (DTy) {
1784 addType(&Buffer, DTy);
1785 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1786 }
Devang Patel0e821f42011-04-12 23:21:44 +00001787}
1788
Devang Patel89543712011-08-15 17:24:54 +00001789/// constructContainingTypeDIEs - Construct DIEs for types that contain
1790/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001791void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001792 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001793 CE = ContainingTypeMap.end();
1794 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001795 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001796 DIDescriptor D(CI->second);
1797 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001798 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001799 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001800 if (!NDie)
1801 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001802 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001803 }
1804}
1805
Devang Patel3acc70e2011-08-15 22:04:40 +00001806/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001807DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001808 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001809
Devang Patel3acc70e2011-08-15 22:04:40 +00001810 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001811 DIE *VariableDie = new DIE(DV.getTag());
1812 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001813 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001814 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001815 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001816 else {
David Blaikie715528b2013-08-19 03:34:03 +00001817 if (!Name.empty())
1818 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001819 addSourceLine(VariableDie, DV.getVariable());
1820 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001821 }
1822
Eric Christopher34a2c872013-11-15 01:43:19 +00001823 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001824 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001825
1826 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001827 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001828 return VariableDie;
1829 }
1830
1831 // Add variable address.
1832
Eric Christopher34a2c872013-11-15 01:43:19 +00001833 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001834 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001835 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1836 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001837 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001838 return VariableDie;
1839 }
1840
Eric Christophercead0332011-10-03 15:49:20 +00001841 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001842 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001843 assert(DVInsn->getNumOperands() == 3);
1844 if (DVInsn->getOperand(0).isReg()) {
1845 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001846 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001847 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001848 MachineLocation Location(RegOp.getReg(),
1849 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001850 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001851 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001852 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001853 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001854 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001855 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001856 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001857 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001858 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001859 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001860
Eric Christopher34a2c872013-11-15 01:43:19 +00001861 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001862 return VariableDie;
1863 } else {
1864 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001865 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001866 if (FI != ~0) {
1867 unsigned FrameReg = 0;
1868 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001869 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001870 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001871 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001872 }
1873 }
1874
Eric Christopher34a2c872013-11-15 01:43:19 +00001875 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001876 return VariableDie;
1877}
1878
Manman Ren230ec862013-10-23 23:00:44 +00001879/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001880void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001881 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001882 StringRef Name = DT.getName();
1883 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001884 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001885
Manman Ren93b30902013-10-08 18:42:58 +00001886 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001887
1888 addSourceLine(MemberDie, DT);
1889
1890 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001891 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Devang Patel0e821f42011-04-12 23:21:44 +00001892
Eric Christopherc2697f82013-10-19 01:04:47 +00001893 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001894
1895 // For C++, virtual base classes are not at fixed offset. Use following
1896 // expression to extract appropriate offset from vtable.
1897 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1898
1899 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001900 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1901 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1902 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1903 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1904 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1905 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1906 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001907
David Blaikief2443192013-10-21 17:28:37 +00001908 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001909 } else {
1910 uint64_t Size = DT.getSizeInBits();
1911 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1912 uint64_t OffsetInBytes;
1913
1914 if (Size != FieldSize) {
1915 // Handle bitfield.
1916 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1917 getBaseTypeSize(DD, DT) >> 3);
1918 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1919
1920 uint64_t Offset = DT.getOffsetInBits();
1921 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1922 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1923 uint64_t FieldOffset = (HiMark - FieldSize);
1924 Offset -= FieldOffset;
1925
1926 // Maybe we need to work from the other end.
1927 if (Asm->getDataLayout().isLittleEndian())
1928 Offset = FieldSize - (Offset + Size);
1929 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1930
1931 // Here WD_AT_data_member_location points to the anonymous
1932 // field that includes this bit field.
1933 OffsetInBytes = FieldOffset >> 3;
1934 } else
1935 // This is not a bitfield.
1936 OffsetInBytes = DT.getOffsetInBits() >> 3;
Eric Christopher98b7f172013-11-11 18:52:36 +00001937 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001938 }
Devang Patel0e821f42011-04-12 23:21:44 +00001939
1940 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001941 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001942 dwarf::DW_ACCESS_protected);
1943 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001944 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001945 dwarf::DW_ACCESS_private);
1946 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001947 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001948 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001949 dwarf::DW_ACCESS_public);
1950 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001951 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001952 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001953
1954 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001955 if (MDNode *PNode = DT.getObjCProperty())
1956 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001957 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001958 PropertyDie);
1959
David Blaikie37fefc32012-12-13 22:43:07 +00001960 if (DT.isArtificial())
1961 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001962}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001963
Manman Renc6b63922013-10-14 20:33:57 +00001964/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001965DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001966 if (!DT.Verify())
1967 return NULL;
1968
Manman Renc6b63922013-10-14 20:33:57 +00001969 // Construct the context before querying for the existence of the DIE in case
1970 // such construction creates the DIE.
1971 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001972 assert(dwarf::isType(ContextDIE->getTag()) &&
1973 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001974
1975 DIE *StaticMemberDIE = getDIE(DT);
1976 if (StaticMemberDIE)
1977 return StaticMemberDIE;
1978
Manman Renb987e512013-10-29 00:53:03 +00001979 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001980
Manman Ren93b30902013-10-08 18:42:58 +00001981 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001982
1983 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1984 addType(StaticMemberDIE, Ty);
1985 addSourceLine(StaticMemberDIE, DT);
1986 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1987 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1988
1989 // FIXME: We could omit private if the parent is a class_type, and
1990 // public if the parent is something else.
1991 if (DT.isProtected())
1992 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1993 dwarf::DW_ACCESS_protected);
1994 else if (DT.isPrivate())
1995 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1996 dwarf::DW_ACCESS_private);
1997 else
1998 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1999 dwarf::DW_ACCESS_public);
2000
2001 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00002002 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00002003 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
2004 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002005
Eric Christopher4d23a4a2013-01-16 01:22:23 +00002006 return StaticMemberDIE;
2007}
David Blaikie6b288cf2013-10-30 20:42:41 +00002008
Eric Christophera5a79422013-12-09 23:32:48 +00002009void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00002010 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00002011 Asm->OutStreamer.AddComment("DWARF version number");
2012 Asm->EmitInt16(DD->getDwarfVersion());
2013 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00002014 // We share one abbreviations table across all units so it's always at the
2015 // start of the section. Use a relocatable offset where needed to ensure
2016 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00002017 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00002018 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2019 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
2020}
David Blaikiebc563272013-12-13 21:33:40 +00002021
Juergen Ributzkadb9ee002013-12-14 12:23:14 +00002022DwarfCompileUnit::~DwarfCompileUnit() {}
2023DwarfTypeUnit::~DwarfTypeUnit() {}
2024
David Blaikiebc563272013-12-13 21:33:40 +00002025void DwarfTypeUnit::emitHeader(const MCSection *ASection,
2026 const MCSymbol *ASectionSym) const {
2027 DwarfUnit::emitHeader(ASection, ASectionSym);
2028 Asm->OutStreamer.AddComment("Type Signature");
2029 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
2030 Asm->OutStreamer.AddComment("Type DIE Offset");
2031 Asm->OutStreamer.EmitIntValue(Ty->getOffset(), sizeof(Ty->getOffset()));
2032}
2033
2034void DwarfTypeUnit::initSection(const MCSection *Section) {
2035 assert(!this->Section);
2036 this->Section = Section;
2037 // Since each type unit is contained in its own COMDAT section, the begin
2038 // label and the section label are the same. Using the begin label emission in
2039 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
2040 // the only other alternative of lazily constructing start-of-section labels
2041 // and storing a mapping in DwarfDebug (or AsmPrinter).
2042 this->SectionSym = this->LabelBegin =
2043 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2044 this->LabelEnd =
2045 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2046 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2047}