blob: a6bae16b6947ee0429f1d5f20f4a2705679cd4cd [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"
Rafael Espindola894843c2014-01-07 21:19:40 +000025#include "llvm/IR/Mangler.h"
Eric Christopher84588622013-12-28 01:39:17 +000026#include "llvm/MC/MCAsmInfo.h"
Adrian Prantlcbcd5782014-02-11 22:22:15 +000027#include "llvm/MC/MCContext.h"
David Blaikie6b288cf2013-10-30 20:42:41 +000028#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetFrameLowering.h"
David Blaikief2694972013-06-28 20:05:11 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000033#include "llvm/Target/TargetMachine.h"
Devang Patel0e821f42011-04-12 23:21:44 +000034#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000035
36using namespace llvm;
37
Eric Christopher4287a492013-12-09 23:57:44 +000038static cl::opt<bool>
39GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
40 cl::desc("Generate DWARF4 type units."),
41 cl::init(false));
David Blaikie409dd9c2013-11-19 23:08:21 +000042
David Blaikie2a80e442013-12-02 22:09:48 +000043/// Unit - Unit constructor.
David Blaikie7480ae62014-01-09 03:03:27 +000044DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
45 DwarfDebug *DW, DwarfFile *DWU)
David Blaikief645f962014-01-09 03:23:41 +000046 : UniqueID(UID), CUNode(Node), UnitDie(D), DebugInfoOffset(0), Asm(A),
47 DD(DW), DU(DWU), IndexTyDie(0), Section(0), Skeleton(0) {
David Blaikie319a05f2013-12-02 19:33:10 +000048 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
49}
50
Eric Christopher4287a492013-12-09 23:57:44 +000051DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
52 AsmPrinter *A, DwarfDebug *DW,
53 DwarfFile *DWU)
David Blaikie7480ae62014-01-09 03:03:27 +000054 : DwarfUnit(UID, D, Node, A, DW, DWU) {
David Blaikie5a152402013-11-15 23:52:02 +000055 insertDIE(Node, D);
Devang Patel0e821f42011-04-12 23:21:44 +000056}
57
David Blaikie15632ae2014-02-12 00:31:30 +000058DwarfTypeUnit::DwarfTypeUnit(unsigned UID, DIE *D, DwarfCompileUnit &CU,
Eric Christopher4287a492013-12-09 23:57:44 +000059 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
David Blaikie15632ae2014-02-12 00:31:30 +000060 : DwarfUnit(UID, D, CU.getCUNode(), A, DW, DWU), CU(CU) {
61 (void)CU;
62}
David Blaikie409dd9c2013-11-19 23:08:21 +000063
David Blaikie319a05f2013-12-02 19:33:10 +000064/// ~Unit - Destructor for compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +000065DwarfUnit::~DwarfUnit() {
Devang Patel0e821f42011-04-12 23:21:44 +000066 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
67 DIEBlocks[j]->~DIEBlock();
68}
69
70/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
71/// information entry.
Eric Christophera5a79422013-12-09 23:32:48 +000072DIEEntry *DwarfUnit::createDIEEntry(DIE *Entry) {
Devang Patel0e821f42011-04-12 23:21:44 +000073 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
74 return Value;
75}
76
Bill Wendling3495f9b2012-12-06 07:55:19 +000077/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000078/// DWARF version doesn't handle the language, return -1.
Eric Christophera5a79422013-12-09 23:32:48 +000079int64_t DwarfUnit::getDefaultLowerBound() const {
David Blaikiecb8e4352013-11-15 23:50:53 +000080 switch (getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000081 default:
82 break;
83
84 case dwarf::DW_LANG_C89:
85 case dwarf::DW_LANG_C99:
86 case dwarf::DW_LANG_C:
87 case dwarf::DW_LANG_C_plus_plus:
88 case dwarf::DW_LANG_ObjC:
89 case dwarf::DW_LANG_ObjC_plus_plus:
90 return 0;
91
92 case dwarf::DW_LANG_Fortran77:
93 case dwarf::DW_LANG_Fortran90:
94 case dwarf::DW_LANG_Fortran95:
95 return 1;
96
97 // The languages below have valid values only if the DWARF version >= 4.
98 case dwarf::DW_LANG_Java:
99 case dwarf::DW_LANG_Python:
100 case dwarf::DW_LANG_UPC:
101 case dwarf::DW_LANG_D:
102 if (dwarf::DWARF_VERSION >= 4)
103 return 0;
104 break;
105
106 case dwarf::DW_LANG_Ada83:
107 case dwarf::DW_LANG_Ada95:
108 case dwarf::DW_LANG_Cobol74:
109 case dwarf::DW_LANG_Cobol85:
110 case dwarf::DW_LANG_Modula2:
111 case dwarf::DW_LANG_Pascal83:
112 case dwarf::DW_LANG_PLI:
113 if (dwarf::DWARF_VERSION >= 4)
114 return 1;
115 break;
116 }
117
118 return -1;
119}
120
Manman Ren4dbdc902013-10-31 17:54:35 +0000121/// Check whether the DIE for this MDNode can be shared across CUs.
David Blaikie4201ddf2013-11-15 22:59:36 +0000122static bool isShareableAcrossCUs(DIDescriptor D) {
David Blaikiebcb418e2013-11-20 18:40:16 +0000123 // When the MDNode can be part of the type system, the DIE can be shared
124 // across CUs.
125 // Combining type units and cross-CU DIE sharing is lower value (since
126 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
127 // level already) but may be implementable for some value in projects
128 // building multiple independent libraries with LTO and then linking those
129 // together.
David Blaikie409dd9c2013-11-19 23:08:21 +0000130 return (D.isType() ||
131 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
Eric Christopher4287a492013-12-09 23:57:44 +0000132 !GenerateDwarfTypeUnits;
Manman Ren4dbdc902013-10-31 17:54:35 +0000133}
134
135/// getDIE - Returns the debug information entry map slot for the
136/// specified debug variable. We delegate the request to DwarfDebug
137/// when the DIE for this MDNode can be shared across CUs. The mappings
138/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000139DIE *DwarfUnit::getDIE(DIDescriptor D) const {
David Blaikie2ad00162013-11-15 23:09:13 +0000140 if (isShareableAcrossCUs(D))
141 return DD->getDIE(D);
142 return MDNodeToDieMap.lookup(D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000143}
144
145/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
146/// when the DIE for this MDNode can be shared across CUs. The mappings
147/// will be kept in DwarfDebug for shareable DIEs.
Eric Christophera5a79422013-12-09 23:32:48 +0000148void DwarfUnit::insertDIE(DIDescriptor Desc, DIE *D) {
David Blaikie2ad00162013-11-15 23:09:13 +0000149 if (isShareableAcrossCUs(Desc)) {
150 DD->insertDIE(Desc, D);
Manman Ren4dbdc902013-10-31 17:54:35 +0000151 return;
152 }
David Blaikie2ad00162013-11-15 23:09:13 +0000153 MDNodeToDieMap.insert(std::make_pair(Desc, D));
Manman Ren4dbdc902013-10-31 17:54:35 +0000154}
155
Eric Christopherbb69a272012-08-24 01:14:27 +0000156/// addFlag - Add a flag that is true.
Eric Christophera5a79422013-12-09 23:32:48 +0000157void DwarfUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000158 if (DD->getDwarfVersion() >= 4)
Eric Christopherdccd3282013-10-04 22:40:05 +0000159 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000160 else
Eric Christopherdccd3282013-10-04 22:40:05 +0000161 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
Eric Christopherbb69a272012-08-24 01:14:27 +0000162}
163
Devang Patel0e821f42011-04-12 23:21:44 +0000164/// addUInt - Add an unsigned integer attribute data and value.
165///
Eric Christophera5a79422013-12-09 23:32:48 +0000166void DwarfUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
167 Optional<dwarf::Form> Form, uint64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000168 if (!Form)
169 Form = DIEInteger::BestForm(false, Integer);
170 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
171 DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000172 Die->addValue(Attribute, *Form, Value);
173}
174
Eric Christophera5a79422013-12-09 23:32:48 +0000175void DwarfUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000176 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000177}
178
179/// addSInt - Add an signed integer attribute data and value.
180///
Eric Christophera5a79422013-12-09 23:32:48 +0000181void DwarfUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
182 Optional<dwarf::Form> Form, int64_t Integer) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000183 if (!Form)
184 Form = DIEInteger::BestForm(true, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000185 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
David Blaikief2443192013-10-21 17:28:37 +0000186 Die->addValue(Attribute, *Form, Value);
187}
188
Eric Christophera5a79422013-12-09 23:32:48 +0000189void DwarfUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
190 int64_t Integer) {
David Blaikief2443192013-10-21 17:28:37 +0000191 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
Devang Patel0e821f42011-04-12 23:21:44 +0000192}
193
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000194/// addString - Add a string attribute data and value. We always emit a
195/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000196/// more predictable sizes. In the case of split dwarf we emit an index
197/// into another table which gets us the static offset into the string
198/// table.
Eric Christophera5a79422013-12-09 23:32:48 +0000199void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
200 StringRef String) {
Eric Christopher05893f42013-12-30 18:32:31 +0000201
202 if (!DD->useSplitDwarf())
203 return addLocalString(Die, Attribute, String);
204
205 unsigned idx = DU->getStringPoolIndex(String);
206 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
Eric Christopher67646432013-07-26 17:02:41 +0000207 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
Eric Christopher05893f42013-12-30 18:32:31 +0000208 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000209}
210
211/// addLocalString - Add a string attribute data and value. This is guaranteed
212/// to be in the local string pool instead of indirected.
Eric Christophera5a79422013-12-09 23:32:48 +0000213void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
214 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000215 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000216 DIEValue *Value;
Eric Christopher84588622013-12-28 01:39:17 +0000217 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000218 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000219 else {
Eric Christophere698f532012-12-20 21:58:36 +0000220 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000221 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000222 }
Eric Christopher05893f42013-12-30 18:32:31 +0000223 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
224 Die->addValue(Attribute, dwarf::DW_FORM_strp, Str);
Devang Patel0e821f42011-04-12 23:21:44 +0000225}
226
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000227/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000228///
Eric Christophera5a79422013-12-09 23:32:48 +0000229void DwarfUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000230 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
David Blaikief2443192013-10-21 17:28:37 +0000231 Die->addValue((dwarf::Attribute)0, Form, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000232}
233
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000234/// addLabel - Add a Dwarf label attribute data and value.
235///
Eric Christophera5a79422013-12-09 23:32:48 +0000236void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
237 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000238 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
239 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000240}
241
Eric Christophera5a79422013-12-09 23:32:48 +0000242void DwarfUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
243 const MCSymbol *Label) {
David Blaikief2443192013-10-21 17:28:37 +0000244 addLabel(Die, (dwarf::Attribute)0, Form, Label);
245}
246
Eric Christopher33ff6972013-11-21 23:46:41 +0000247/// addSectionLabel - Add a Dwarf section label attribute data and value.
248///
Eric Christophera5a79422013-12-09 23:32:48 +0000249void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
250 const MCSymbol *Label) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000251 if (DD->getDwarfVersion() >= 4)
252 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
253 else
254 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
255}
256
257/// addSectionOffset - Add an offset into a section attribute data and value.
258///
Eric Christophera5a79422013-12-09 23:32:48 +0000259void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
260 uint64_t Integer) {
Eric Christopher33ff6972013-11-21 23:46:41 +0000261 if (DD->getDwarfVersion() >= 4)
262 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
263 else
264 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
265}
266
Eric Christopher962c9082013-01-15 23:56:56 +0000267/// addLabelAddress - Add a dwarf label attribute data and value using
268/// DW_FORM_addr or DW_FORM_GNU_addr_index.
269///
Eric Christopher4287a492013-12-09 23:57:44 +0000270void DwarfCompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
271 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000272 if (Label)
273 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000274
Eric Christophercf48ade2014-01-24 11:52:53 +0000275 if (!DD->useSplitDwarf()) {
276 if (Label) {
277 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
278 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
279 } else {
280 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
281 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
282 }
Eric Christopher962c9082013-01-15 23:56:56 +0000283 } else {
Eric Christophercf48ade2014-01-24 11:52:53 +0000284 unsigned idx = DU->getAddrPoolIndex(Label);
285 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
286 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
Eric Christopher962c9082013-01-15 23:56:56 +0000287 }
288}
289
Eric Christophere9ec2452013-01-18 22:11:33 +0000290/// addOpAddress - Add a dwarf op address data and value using the
291/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
292///
Eric Christophera5a79422013-12-09 23:32:48 +0000293void DwarfUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
Eric Christophere9ec2452013-01-18 22:11:33 +0000294 if (!DD->useSplitDwarf()) {
David Blaikief2443192013-10-21 17:28:37 +0000295 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
296 addLabel(Die, dwarf::DW_FORM_udata, Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000297 } else {
David Blaikief2443192013-10-21 17:28:37 +0000298 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
299 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000300 }
301}
302
Eric Christopher33ff6972013-11-21 23:46:41 +0000303/// addSectionDelta - Add a section label delta attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000304///
Eric Christophera5a79422013-12-09 23:32:48 +0000305void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
306 const MCSymbol *Hi, const MCSymbol *Lo) {
Devang Patel0e821f42011-04-12 23:21:44 +0000307 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
Eric Christopher33ff6972013-11-21 23:46:41 +0000308 if (DD->getDwarfVersion() >= 4)
309 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
310 else
311 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000312}
313
314/// addDIEEntry - Add a DIE attribute data and value.
315///
Eric Christophera5a79422013-12-09 23:32:48 +0000316void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000317 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
318}
319
David Blaikie47f615e2013-12-17 23:32:35 +0000320void DwarfUnit::addDIETypeSignature(DIE *Die, const DwarfTypeUnit &Type) {
321 Die->addValue(dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
322 new (DIEValueAllocator) DIETypeSignature(Type));
323}
324
Eric Christophera5a79422013-12-09 23:32:48 +0000325void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
326 DIEEntry *Entry) {
David Blaikie409dd9c2013-11-19 23:08:21 +0000327 const DIE *DieCU = Die->getUnitOrNull();
328 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
Manman Ren4dbdc902013-10-31 17:54:35 +0000329 if (!DieCU)
330 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
David Blaikie2a80e442013-12-02 22:09:48 +0000331 DieCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000332 if (!EntryCU)
David Blaikie2a80e442013-12-02 22:09:48 +0000333 EntryCU = getUnitDie();
Manman Ren4dbdc902013-10-31 17:54:35 +0000334 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
335 : dwarf::DW_FORM_ref_addr,
336 Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000337}
338
Manman Renb987e512013-10-29 00:53:03 +0000339/// Create a DIE with the given Tag, add the DIE to its parent, and
340/// call insertDIE if MD is not null.
Eric Christophera5a79422013-12-09 23:32:48 +0000341DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
Manman Renb987e512013-10-29 00:53:03 +0000342 DIE *Die = new DIE(Tag);
343 Parent.addChild(Die);
David Blaikie52c50202013-11-16 00:29:01 +0000344 if (N)
345 insertDIE(N, Die);
Manman Renb987e512013-10-29 00:53:03 +0000346 return Die;
347}
348
Devang Patel0e821f42011-04-12 23:21:44 +0000349/// addBlock - Add block data.
350///
Eric Christophera5a79422013-12-09 23:32:48 +0000351void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
352 DIEBlock *Block) {
Devang Patel0e821f42011-04-12 23:21:44 +0000353 Block->ComputeSize(Asm);
354 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
355 Die->addValue(Attribute, Block->BestForm(), Block);
356}
357
358/// addSourceLine - Add location information to specified debug information
359/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000360void DwarfUnit::addSourceLine(DIE *Die, unsigned Line, StringRef File,
361 StringRef Directory) {
Devang Patel0e821f42011-04-12 23:21:44 +0000362 if (Line == 0)
363 return;
David Blaikie101613e2014-02-12 00:11:25 +0000364
David Blaikie5b858582014-02-12 00:40:47 +0000365 unsigned FileID =
366 DD->getOrCreateSourceID(File, Directory, getCU().getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000367 assert(FileID && "Invalid file id");
David Blaikief2443192013-10-21 17:28:37 +0000368 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
369 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
Devang Patel0e821f42011-04-12 23:21:44 +0000370}
371
372/// addSourceLine - Add location information to specified debug information
373/// entry.
David Blaikie101613e2014-02-12 00:11:25 +0000374void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
375 assert(V.isVariable());
376
377 addSourceLine(Die, V.getLineNumber(), V.getContext().getFilename(), V.getContext().getDirectory());
378}
379
380/// addSourceLine - Add location information to specified debug information
381/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000382void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
David Blaikie52019302014-02-11 23:57:03 +0000383 assert(G.isGlobalVariable());
Devang Patel0e821f42011-04-12 23:21:44 +0000384
David Blaikie101613e2014-02-12 00:11:25 +0000385 addSourceLine(Die, G.getLineNumber(), G.getFilename(), G.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000386}
387
388/// addSourceLine - Add location information to specified debug information
389/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000390void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
David Blaikie52019302014-02-11 23:57:03 +0000391 assert(SP.isSubprogram());
Eric Christopher7734ca22012-03-15 23:55:40 +0000392
David Blaikie101613e2014-02-12 00:11:25 +0000393 addSourceLine(Die, SP.getLineNumber(), SP.getFilename(), SP.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000394}
395
396/// addSourceLine - Add location information to specified debug information
397/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000398void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000399 assert(Ty.isType());
Devang Patel0e821f42011-04-12 23:21:44 +0000400
David Blaikie101613e2014-02-12 00:11:25 +0000401 addSourceLine(Die, Ty.getLineNumber(), Ty.getFilename(), Ty.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000402}
403
404/// addSourceLine - Add location information to specified debug information
405/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000406void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
David Blaikie52019302014-02-11 23:57:03 +0000407 assert(Ty.isObjCProperty());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000408
Eric Christopher70e1bd82012-03-29 08:42:56 +0000409 DIFile File = Ty.getFile();
David Blaikie101613e2014-02-12 00:11:25 +0000410 addSourceLine(Die, Ty.getLineNumber(), File.getFilename(),
411 File.getDirectory());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000412}
413
414/// addSourceLine - Add location information to specified debug information
415/// entry.
Eric Christophera5a79422013-12-09 23:32:48 +0000416void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
David Blaikie52019302014-02-11 23:57:03 +0000417 assert(NS.Verify());
Devang Patel0e821f42011-04-12 23:21:44 +0000418
David Blaikie101613e2014-02-12 00:11:25 +0000419 addSourceLine(Die, NS.getLineNumber(), NS.getFilename(), NS.getDirectory());
Devang Patel0e821f42011-04-12 23:21:44 +0000420}
421
Eric Christopher92331fd2012-11-21 00:34:38 +0000422/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000423/// DbgVariable based on provided MachineLocation.
Eric Christophera5a79422013-12-09 23:32:48 +0000424void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
425 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000426 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000427 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000428 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000429 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
430 else
David Blaikieea2605d2013-06-20 00:25:24 +0000431 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000432 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000433}
434
Devang Patelba5fbf12011-04-26 19:06:18 +0000435/// addRegisterOp - Add register operand.
Eric Christophera5a79422013-12-09 23:32:48 +0000436void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000437 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000438 int DWReg = RI->getDwarfRegNum(Reg, false);
439 bool isSubRegister = DWReg < 0;
440
441 unsigned Idx = 0;
442
443 // Go up the super-register chain until we hit a valid dwarf register number.
444 for (MCSuperRegIterator SR(Reg, RI); SR.isValid() && DWReg < 0; ++SR) {
445 DWReg = RI->getDwarfRegNum(*SR, false);
446 if (DWReg >= 0)
447 Idx = RI->getSubRegIndex(*SR, Reg);
448 }
449
450 if (DWReg < 0) {
451 DEBUG(llvm::dbgs() << "Invalid Dwarf register number.\n");
452 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_nop);
453 return;
454 }
455
456 // Emit register
Devang Patelba5fbf12011-04-26 19:06:18 +0000457 if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000458 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000459 else {
David Blaikief2443192013-10-21 17:28:37 +0000460 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
461 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000462 }
Adrian Prantlcbcd5782014-02-11 22:22:15 +0000463
464 // Emit Mask
465 if (isSubRegister) {
466 unsigned Size = RI->getSubRegIdxSize(Idx);
467 unsigned Offset = RI->getSubRegIdxOffset(Idx);
468 if (Offset > 0) {
469 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bit_piece);
470 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
471 addUInt(TheDie, dwarf::DW_FORM_data1, Offset);
472 } else {
473 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_piece);
474 addUInt(TheDie, dwarf::DW_FORM_data1, Size);
475 }
476 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000477}
478
479/// addRegisterOffset - Add register offset.
Eric Christophera5a79422013-12-09 23:32:48 +0000480void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
481 int64_t Offset) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000482 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
483 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
484 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
485 if (Reg == TRI->getFrameRegister(*Asm->MF))
486 // If variable offset is based in frame register then use fbreg.
David Blaikief2443192013-10-21 17:28:37 +0000487 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000488 else if (DWReg < 32)
David Blaikief2443192013-10-21 17:28:37 +0000489 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000490 else {
David Blaikief2443192013-10-21 17:28:37 +0000491 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
492 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
Devang Patelba5fbf12011-04-26 19:06:18 +0000493 }
David Blaikief2443192013-10-21 17:28:37 +0000494 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
Devang Patelba5fbf12011-04-26 19:06:18 +0000495}
496
497/// addAddress - Add an address attribute to a die based on the location
498/// provided.
Eric Christophera5a79422013-12-09 23:32:48 +0000499void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
500 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000501 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
502
David Blaikieea2605d2013-06-20 00:25:24 +0000503 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000504 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000505 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000506 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000507 if (Indirect && !Location.isReg()) {
David Blaikief2443192013-10-21 17:28:37 +0000508 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
David Blaikieea2605d2013-06-20 00:25:24 +0000509 }
510 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000511
512 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000513 addBlock(Die, Attribute, Block);
Devang Patelba5fbf12011-04-26 19:06:18 +0000514}
515
Devang Patel0e821f42011-04-12 23:21:44 +0000516/// addComplexAddress - Start with the address based on the location provided,
517/// and generate the DWARF information necessary to find the actual variable
Eric Christopher1c70b672013-12-05 00:13:15 +0000518/// given the extra address information encoded in the DbgVariable, starting
519/// from the starting location. Add the DWARF information to the die.
Devang Patel0e821f42011-04-12 23:21:44 +0000520///
Eric Christophera5a79422013-12-09 23:32:48 +0000521void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
522 dwarf::Attribute Attribute,
523 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000524 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000525 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000526 unsigned i = 0;
527 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000528 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000529 // If first address element is OpPlus then emit
530 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000531 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000532 i = 2;
533 } else
534 addRegisterOp(Block, Location.getReg());
Eric Christopherc2697f82013-10-19 01:04:47 +0000535 } else
Devang Patelba5fbf12011-04-26 19:06:18 +0000536 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000537
Eric Christopherc2697f82013-10-19 01:04:47 +0000538 for (; i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000539 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000540 if (Element == DIBuilder::OpPlus) {
David Blaikief2443192013-10-21 17:28:37 +0000541 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
542 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000543 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000544 if (!Location.isReg())
David Blaikief2443192013-10-21 17:28:37 +0000545 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Eric Christopherc2697f82013-10-19 01:04:47 +0000546 } else
547 llvm_unreachable("unknown DIBuilder Opcode");
Devang Patel0e821f42011-04-12 23:21:44 +0000548 }
549
550 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000551 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000552}
553
554/* Byref variables, in Blocks, are declared by the programmer as "SomeType
555 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
556 gives the variable VarName either the struct, or a pointer to the struct, as
557 its type. This is necessary for various behind-the-scenes things the
558 compiler needs to do with by-reference variables in Blocks.
559
560 However, as far as the original *programmer* is concerned, the variable
561 should still have type 'SomeType', as originally declared.
562
563 The function getBlockByrefType dives into the __Block_byref_x_VarName
564 struct to find the original type of the variable, which is then assigned to
565 the variable's Debug Information Entry as its real type. So far, so good.
566 However now the debugger will expect the variable VarName to have the type
567 SomeType. So we need the location attribute for the variable to be an
568 expression that explains to the debugger how to navigate through the
569 pointers and struct to find the actual variable of type SomeType.
570
571 The following function does just that. We start by getting
572 the "normal" location for the variable. This will be the location
573 of either the struct __Block_byref_x_VarName or the pointer to the
574 struct __Block_byref_x_VarName.
575
576 The struct will look something like:
577
578 struct __Block_byref_x_VarName {
579 ... <various fields>
580 struct __Block_byref_x_VarName *forwarding;
581 ... <various other fields>
582 SomeType VarName;
583 ... <maybe more fields>
584 };
585
586 If we are given the struct directly (as our starting point) we
587 need to tell the debugger to:
588
589 1). Add the offset of the forwarding field.
590
591 2). Follow that pointer to get the real __Block_byref_x_VarName
592 struct to use (the real one may have been copied onto the heap).
593
594 3). Add the offset for the field VarName, to find the actual variable.
595
596 If we started with a pointer to the struct, then we need to
597 dereference that pointer first, before the other steps.
598 Translating this into DWARF ops, we will need to append the following
599 to the current location description for the variable:
600
601 DW_OP_deref -- optional, if we start with a pointer
602 DW_OP_plus_uconst <forward_fld_offset>
603 DW_OP_deref
604 DW_OP_plus_uconst <varName_fld_offset>
605
606 That is what this function does. */
607
608/// addBlockByrefAddress - Start with the address based on the location
609/// provided, and generate the DWARF information necessary to find the
610/// actual Block variable (navigating the Block struct) based on the
611/// starting location. Add the DWARF information to the die. For
612/// more information, read large comment just above here.
613///
Eric Christophera5a79422013-12-09 23:32:48 +0000614void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
615 dwarf::Attribute Attribute,
616 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000617 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000618 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000619 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000620 bool isPointer = false;
621
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000622 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000623
624 if (Tag == dwarf::DW_TAG_pointer_type) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000625 DIDerivedType DTy(Ty);
Manman Ren93b30902013-10-08 18:42:58 +0000626 TmpTy = resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000627 isPointer = true;
628 }
629
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000630 DICompositeType blockStruct(TmpTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000631
632 // Find the __forwarding field and the variable field in the __Block_byref
633 // struct.
634 DIArray Fields = blockStruct.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000635 DIDerivedType varField;
636 DIDerivedType forwardingField;
Devang Patel0e821f42011-04-12 23:21:44 +0000637
638 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000639 DIDerivedType DT(Fields.getElement(i));
Devang Patel0e821f42011-04-12 23:21:44 +0000640 StringRef fieldName = DT.getName();
641 if (fieldName == "__forwarding")
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000642 forwardingField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000643 else if (fieldName == varName)
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000644 varField = DT;
Devang Patel0e821f42011-04-12 23:21:44 +0000645 }
646
647 // Get the offsets for the forwarding field and the variable field.
David Blaikie4f6bf27a2013-11-18 23:33:32 +0000648 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
649 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
Devang Patel0e821f42011-04-12 23:21:44 +0000650
651 // Decode the original location, and use that as the start of the byref
652 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000653 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
654
Eric Christopheref9d7102012-07-04 02:02:18 +0000655 if (Location.isReg())
656 addRegisterOp(Block, Location.getReg());
657 else
658 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000659
660 // If we started with a pointer to the __Block_byref... struct, then
661 // the first thing we need to do is dereference the pointer (DW_OP_deref).
662 if (isPointer)
David Blaikief2443192013-10-21 17:28:37 +0000663 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000664
665 // Next add the offset for the '__forwarding' field:
666 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
667 // adding the offset if it's 0.
668 if (forwardingFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000669 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
670 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000671 }
672
673 // Now dereference the __forwarding field to get to the real __Block_byref
674 // struct: DW_OP_deref.
David Blaikief2443192013-10-21 17:28:37 +0000675 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000676
677 // Now that we've got the real __Block_byref... struct, add the offset
678 // for the variable's field to get to the location of the actual variable:
679 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
680 if (varFieldOffset > 0) {
David Blaikief2443192013-10-21 17:28:37 +0000681 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
682 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
Devang Patel0e821f42011-04-12 23:21:44 +0000683 }
684
685 // Now attach the location information to the DIE.
David Blaikief2443192013-10-21 17:28:37 +0000686 addBlock(Die, Attribute, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000687}
688
Devang Patelbcd50a12011-07-20 21:57:04 +0000689/// isTypeSigned - Return true if the type is signed.
Manman Renb3388602013-10-05 01:43:03 +0000690static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000691 if (Ty.isDerivedType())
Manman Renb3388602013-10-05 01:43:03 +0000692 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
693 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000694 if (Ty.isBasicType())
Eric Christopherc2697f82013-10-19 01:04:47 +0000695 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
696 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000697 *SizeInBits = Ty.getSizeInBits();
698 return true;
699 }
700 return false;
701}
702
Manman Renb3388602013-10-05 01:43:03 +0000703/// Return true if type encoding is unsigned.
704static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
705 DIDerivedType DTy(Ty);
706 if (DTy.isDerivedType())
707 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
708
709 DIBasicType BTy(Ty);
710 if (BTy.isBasicType()) {
711 unsigned Encoding = BTy.getEncoding();
712 if (Encoding == dwarf::DW_ATE_unsigned ||
713 Encoding == dwarf::DW_ATE_unsigned_char ||
714 Encoding == dwarf::DW_ATE_boolean)
715 return true;
716 }
717 return false;
718}
719
720/// If this type is derived from a base type then return base type size.
Manman Renbda410f2013-10-08 18:46:58 +0000721static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
Manman Renb3388602013-10-05 01:43:03 +0000722 unsigned Tag = Ty.getTag();
723
724 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
725 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
726 Tag != dwarf::DW_TAG_restrict_type)
727 return Ty.getSizeInBits();
728
729 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
730
731 // If this type is not derived from any type then take conservative approach.
732 if (!BaseType.isValid())
733 return Ty.getSizeInBits();
734
735 // If this is a derived type, go ahead and get the base type, unless it's a
736 // reference then it's just the size of the field. Pointer types have no need
737 // of this since they're a different type of qualification on the type.
738 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
739 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
740 return Ty.getSizeInBits();
741
742 if (BaseType.isDerivedType())
Manman Renbda410f2013-10-08 18:46:58 +0000743 return getBaseTypeSize(DD, DIDerivedType(BaseType));
Manman Renb3388602013-10-05 01:43:03 +0000744
745 return BaseType.getSizeInBits();
746}
747
Devang Patel0e821f42011-04-12 23:21:44 +0000748/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000749void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
750 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000751 // FIXME: This is a bit conservative/simple - it emits negative values at
752 // their maximum bit width which is a bit unfortunate (& doesn't prefer
753 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000754 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000755 int SizeInBits = -1;
Manman Renb3388602013-10-05 01:43:03 +0000756 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
David Blaikief2443192013-10-21 17:28:37 +0000757 dwarf::Form Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000758
Eric Christopher9d1daa82013-08-27 23:49:04 +0000759 // If we're a signed constant definitely use sdata.
760 if (SignedConstant) {
761 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
762 return;
763 }
764
765 // Else use data for now unless it's larger than we can deal with.
766 switch (SizeInBits) {
767 case 8:
768 Form = dwarf::DW_FORM_data1;
769 break;
770 case 16:
771 Form = dwarf::DW_FORM_data2;
772 break;
773 case 32:
774 Form = dwarf::DW_FORM_data4;
775 break;
776 case 64:
777 Form = dwarf::DW_FORM_data8;
778 break;
779 default:
780 Form = dwarf::DW_FORM_udata;
781 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
782 return;
783 }
784 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000785}
786
787/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000788void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Eric Christopherc2697f82013-10-19 01:04:47 +0000789 assert(MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000790 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
791 APFloat FPImm = MO.getFPImm()->getValueAPF();
792
793 // Get the raw data form of the floating point.
794 const APInt FltVal = FPImm.bitcastToAPInt();
Eric Christopherc2697f82013-10-19 01:04:47 +0000795 const char *FltPtr = (const char *)FltVal.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000796
797 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000798 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000799 int Incr = (LittleEndian ? 1 : -1);
800 int Start = (LittleEndian ? 0 : NumBytes - 1);
801 int Stop = (LittleEndian ? NumBytes : -1);
802
803 // Output the constant to DWARF one byte at a time.
804 for (; Start != Stop; Start += Incr)
Eric Christopher98b7f172013-11-11 18:52:36 +0000805 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
Devang Patel0e821f42011-04-12 23:21:44 +0000806
David Blaikief2443192013-10-21 17:28:37 +0000807 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000808}
809
David Blaikiea39a76e2013-01-20 01:18:01 +0000810/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000811void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000812 // Pass this down to addConstantValue as an unsigned bag of bits.
813 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000814}
815
Devang Patel0e821f42011-04-12 23:21:44 +0000816/// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000817void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
818 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000819 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000820}
821
822// addConstantValue - Add constant value entry in variable DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000823void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000824 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000825 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000826 // If we're a signed constant definitely use sdata.
827 if (!Unsigned) {
828 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
829 Val.getSExtValue());
830 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000831 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000832
833 // Else use data for now unless it's larger than we can deal with.
David Blaikief2443192013-10-21 17:28:37 +0000834 dwarf::Form Form;
Eric Christopher9d1daa82013-08-27 23:49:04 +0000835 switch (CIBitWidth) {
836 case 8:
837 Form = dwarf::DW_FORM_data1;
838 break;
839 case 16:
840 Form = dwarf::DW_FORM_data2;
841 break;
842 case 32:
843 Form = dwarf::DW_FORM_data4;
844 break;
845 case 64:
846 Form = dwarf::DW_FORM_data8;
847 break;
848 default:
849 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
850 Val.getZExtValue());
851 return;
852 }
853 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000854 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000855 }
856
857 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
858
859 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000860 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000861
862 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000863 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000864
865 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000866 for (int i = 0; i < NumBytes; i++) {
867 uint8_t c;
868 if (LittleEndian)
869 c = Ptr64[i / 8] >> (8 * (i & 7));
870 else
871 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
David Blaikief2443192013-10-21 17:28:37 +0000872 addUInt(Block, dwarf::DW_FORM_data1, c);
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000873 }
Devang Patel0e821f42011-04-12 23:21:44 +0000874
David Blaikief2443192013-10-21 17:28:37 +0000875 addBlock(Die, dwarf::DW_AT_const_value, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000876}
877
Eric Christopher25e35092013-04-22 07:47:40 +0000878/// addTemplateParams - Add template parameters into buffer.
Eric Christophera5a79422013-12-09 23:32:48 +0000879void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
Devang Patel0e821f42011-04-12 23:21:44 +0000880 // Add template parameters.
881 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
882 DIDescriptor Element = TParams.getElement(i);
883 if (Element.isTemplateTypeParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000884 constructTemplateTypeParameterDIE(Buffer,
885 DITemplateTypeParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000886 else if (Element.isTemplateValueParameter())
Manman Renffc9a712013-10-23 23:05:28 +0000887 constructTemplateValueParameterDIE(Buffer,
888 DITemplateValueParameter(Element));
Devang Patel0e821f42011-04-12 23:21:44 +0000889 }
Devang Patel0e821f42011-04-12 23:21:44 +0000890}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000891
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000892/// getOrCreateContextDIE - Get context owner's DIE.
Eric Christophera5a79422013-12-09 23:32:48 +0000893DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
David Blaikiebd700e42013-11-14 21:24:34 +0000894 if (!Context || Context.isFile())
David Blaikie2a80e442013-12-02 22:09:48 +0000895 return getUnitDie();
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000896 if (Context.isType())
897 return getOrCreateTypeDIE(DIType(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000898 if (Context.isNameSpace())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000899 return getOrCreateNameSpace(DINameSpace(Context));
David Blaikie1dbca702013-11-14 19:37:56 +0000900 if (Context.isSubprogram())
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000901 return getOrCreateSubprogramDIE(DISubprogram(Context));
Adrian Prantl7d828bb2013-11-15 21:05:09 +0000902 return getDIE(Context);
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000903}
904
Eric Christophera5a79422013-12-09 23:32:48 +0000905DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
David Blaikie9d861be2013-11-26 00:15:27 +0000906 DIScope Context = resolve(Ty.getContext());
907 DIE *ContextDIE = getOrCreateContextDIE(Context);
David Blaikie409dd9c2013-11-19 23:08:21 +0000908
909 DIE *TyDIE = getDIE(Ty);
910 if (TyDIE)
911 return TyDIE;
912
913 // Create new type.
914 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
915
David Blaikiefbd29eb2013-11-26 00:35:04 +0000916 constructTypeDIE(*TyDIE, Ty);
David Blaikie409dd9c2013-11-19 23:08:21 +0000917
David Blaikie9d861be2013-11-26 00:15:27 +0000918 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie409dd9c2013-11-19 23:08:21 +0000919 return TyDIE;
920}
921
Devang Patel0e821f42011-04-12 23:21:44 +0000922/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
923/// given DIType.
Eric Christophera5a79422013-12-09 23:32:48 +0000924DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
David Blaikie32887552013-11-14 22:25:02 +0000925 if (!TyNode)
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000926 return NULL;
Manman Renf4c339e2013-10-29 22:49:29 +0000927
David Blaikie32887552013-11-14 22:25:02 +0000928 DIType Ty(TyNode);
929 assert(Ty.isType());
930
Manman Renf4c339e2013-10-29 22:49:29 +0000931 // Construct the context before querying for the existence of the DIE in case
932 // such construction creates the DIE.
David Blaikie9d861be2013-11-26 00:15:27 +0000933 DIScope Context = resolve(Ty.getContext());
934 DIE *ContextDIE = getOrCreateContextDIE(Context);
Adrian Prantl4583f7d2013-11-15 23:21:39 +0000935 assert(ContextDIE);
Manman Renf4c339e2013-10-29 22:49:29 +0000936
Eric Christophere595bae2013-10-04 17:08:38 +0000937 DIE *TyDIE = getDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000938 if (TyDIE)
939 return TyDIE;
940
941 // Create new type.
Manman Renf4c339e2013-10-29 22:49:29 +0000942 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
943
Devang Patel0e821f42011-04-12 23:21:44 +0000944 if (Ty.isBasicType())
945 constructTypeDIE(*TyDIE, DIBasicType(Ty));
David Blaikie8a263cb2013-11-26 00:22:37 +0000946 else if (Ty.isCompositeType()) {
947 DICompositeType CTy(Ty);
David Blaikiecfb21152014-01-03 18:59:42 +0000948 if (GenerateDwarfTypeUnits && !Ty.isForwardDecl())
949 if (MDString *TypeId = CTy.getIdentifier()) {
David Blaikie15632ae2014-02-12 00:31:30 +0000950 DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
David Blaikiecfb21152014-01-03 18:59:42 +0000951 // Skip updating the accellerator tables since this is not the full type
952 return TyDIE;
953 }
David Blaikiefbd29eb2013-11-26 00:35:04 +0000954 constructTypeDIE(*TyDIE, CTy);
David Blaikie8a263cb2013-11-26 00:22:37 +0000955 } else {
Devang Patel0e821f42011-04-12 23:21:44 +0000956 assert(Ty.isDerivedType() && "Unknown kind of DIType");
957 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
958 }
David Blaikie2ea848b2013-11-19 22:51:04 +0000959
David Blaikie9d861be2013-11-26 00:15:27 +0000960 updateAcceleratorTables(Context, Ty, TyDIE);
David Blaikie2ea848b2013-11-19 22:51:04 +0000961
962 return TyDIE;
963}
964
Eric Christophera5a79422013-12-09 23:32:48 +0000965void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
966 const DIE *TyDIE) {
Eric Christopher21bde872012-01-06 04:35:23 +0000967 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
968 bool IsImplementation = 0;
969 if (Ty.isCompositeType()) {
970 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000971 // A runtime language of 0 actually means C/C++ and that any
972 // non-negative value is some version of Objective-C/C++.
Eric Christopherc2697f82013-10-19 01:04:47 +0000973 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000974 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000975 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000976 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
David Blaikie9d861be2013-11-26 00:15:27 +0000977
978 if (!Context || Context.isCompileUnit() || Context.isFile() ||
979 Context.isNameSpace())
980 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
Eric Christopher21bde872012-01-06 04:35:23 +0000981 }
Devang Patel0e821f42011-04-12 23:21:44 +0000982}
983
984/// addType - Add a new type attribute to the specified entity.
Eric Christophera5a79422013-12-09 23:32:48 +0000985void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000986 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000987
988 // Check for pre-existence.
989 DIEEntry *Entry = getDIEEntry(Ty);
990 // If it exists then use the existing value.
991 if (Entry) {
Manman Ren4dbdc902013-10-31 17:54:35 +0000992 addDIEEntry(Entity, Attribute, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000993 return;
994 }
995
996 // Construct type.
997 DIE *Buffer = getOrCreateTypeDIE(Ty);
998
999 // Set up proxy.
1000 Entry = createDIEEntry(Buffer);
1001 insertDIEEntry(Ty, Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +00001002 addDIEEntry(Entity, Attribute, Entry);
Devang Patel1cb8ab42011-05-31 23:30:30 +00001003}
1004
Eric Christopher9cd26af2013-09-20 23:22:52 +00001005// Accelerator table mutators - add each name along with its companion
1006// DIE to the proper table while ensuring that the name that we're going
1007// to reference is in the string table. We do this since the names we
1008// add may not only be identical to the names in the DIE.
Eric Christophera5a79422013-12-09 23:32:48 +00001009void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001010 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001011 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001012 std::vector<const DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001013 DIEs.push_back(Die);
1014}
1015
Eric Christophera5a79422013-12-09 23:32:48 +00001016void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001017 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001018 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001019 std::vector<const DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001020 DIEs.push_back(Die);
1021}
1022
Eric Christophera5a79422013-12-09 23:32:48 +00001023void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001024 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001025 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001026 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001027 DIEs.push_back(Die);
1028}
1029
Eric Christophera5a79422013-12-09 23:32:48 +00001030void DwarfUnit::addAccelType(StringRef Name,
1031 std::pair<const DIE *, unsigned> Die) {
Paul Robinson3878a782014-01-31 20:39:19 +00001032 if (!DD->useDwarfAccelTables()) return;
Eric Christopher9cd26af2013-09-20 23:22:52 +00001033 DU->getStringPoolEntry(Name);
David Blaikie2ea848b2013-11-19 22:51:04 +00001034 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +00001035 DIEs.push_back(Die);
1036}
1037
Eric Christopher9c58f312013-09-20 22:20:55 +00001038/// addGlobalName - Add a new global name to the compile unit.
Eric Christophera5a79422013-12-09 23:32:48 +00001039void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
Eric Christopher8dba0d52013-10-19 01:04:42 +00001040 std::string FullName = getParentContextString(Context) + Name.str();
Eric Christopher2c8b7902013-10-17 02:06:06 +00001041 GlobalNames[FullName] = Die;
Eric Christopher9c58f312013-09-20 22:20:55 +00001042}
1043
Eric Christopher2c8b7902013-10-17 02:06:06 +00001044/// getParentContextString - Walks the metadata parent chain in a language
1045/// specific manner (using the compile unit language) and returns
1046/// it as a string. This is done at the metadata level because DIEs may
1047/// not currently have been added to the parent context and walking the
1048/// DIEs looking for names is more expensive than walking the metadata.
Eric Christophera5a79422013-12-09 23:32:48 +00001049std::string DwarfUnit::getParentContextString(DIScope Context) const {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001050 if (!Context)
1051 return "";
1052
1053 // FIXME: Decide whether to implement this for non-C++ languages.
1054 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1055 return "";
1056
Eric Christopher8dba0d52013-10-19 01:04:42 +00001057 std::string CS;
Eric Christopher2c8b7902013-10-17 02:06:06 +00001058 SmallVector<DIScope, 1> Parents;
1059 while (!Context.isCompileUnit()) {
1060 Parents.push_back(Context);
1061 if (Context.getContext())
1062 Context = resolve(Context.getContext());
1063 else
1064 // Structure, etc types will have a NULL context if they're at the top
1065 // level.
1066 break;
1067 }
1068
1069 // Reverse iterate over our list to go from the outermost construct to the
1070 // innermost.
1071 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1072 E = Parents.rend();
1073 I != E; ++I) {
1074 DIScope Ctx = *I;
1075 StringRef Name = Ctx.getName();
Eric Christopher8dba0d52013-10-19 01:04:42 +00001076 if (!Name.empty()) {
Eric Christopher2c8b7902013-10-17 02:06:06 +00001077 CS += Name;
1078 CS += "::";
1079 }
1080 }
1081 return CS;
Devang Patel0e821f42011-04-12 23:21:44 +00001082}
1083
1084/// constructTypeDIE - Construct basic type die from DIBasicType.
Eric Christophera5a79422013-12-09 23:32:48 +00001085void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001086 // Get core information.
1087 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +00001088 // Add name if not anonymous or intermediate type.
1089 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001090 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +00001091
David Blaikiefac56122013-10-04 23:21:16 +00001092 // An unspecified type only has a name attribute.
1093 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
Devang Patel04d6d472011-09-14 23:13:28 +00001094 return;
Devang Patel04d6d472011-09-14 23:13:28 +00001095
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001096 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +00001097 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +00001098
Devang Patel0e821f42011-04-12 23:21:44 +00001099 uint64_t Size = BTy.getSizeInBits() >> 3;
David Blaikief2443192013-10-21 17:28:37 +00001100 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001101}
1102
1103/// constructTypeDIE - Construct derived type die from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001104void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001105 // Get core information.
1106 StringRef Name = DTy.getName();
1107 uint64_t Size = DTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001108 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001109
1110 // Map to main type, void will not have a type.
Manman Ren93b30902013-10-08 18:42:58 +00001111 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +00001112 if (FromTy)
1113 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001114
1115 // Add name if not anonymous or intermediate type.
1116 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001117 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001118
1119 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +00001120 if (Size && Tag != dwarf::DW_TAG_pointer_type)
David Blaikief2443192013-10-21 17:28:37 +00001121 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Devang Patel0e821f42011-04-12 23:21:44 +00001122
David Blaikie5d3249b2013-01-07 05:51:15 +00001123 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
Eric Christopherc2697f82013-10-19 01:04:47 +00001124 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1125 getOrCreateTypeDIE(resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +00001126 // Add source line info if available and TyDesc is not a forward declaration.
1127 if (!DTy.isForwardDecl())
1128 addSourceLine(&Buffer, DTy);
1129}
1130
1131/// constructTypeDIE - Construct type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001132void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
David Blaikie409dd9c2013-11-19 23:08:21 +00001133 // Add name if not anonymous or intermediate type.
Devang Patel0e821f42011-04-12 23:21:44 +00001134 StringRef Name = CTy.getName();
1135
1136 uint64_t Size = CTy.getSizeInBits() >> 3;
David Blaikiefac56122013-10-04 23:21:16 +00001137 uint16_t Tag = Buffer.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001138
1139 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001140 case dwarf::DW_TAG_array_type:
Eric Christopherdf9955d2013-11-11 18:52:31 +00001141 constructArrayTypeDIE(Buffer, CTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001142 break;
Eric Christopheraeb105f2013-11-11 18:52:39 +00001143 case dwarf::DW_TAG_enumeration_type:
1144 constructEnumTypeDIE(Buffer, CTy);
1145 break;
Devang Patel0e821f42011-04-12 23:21:44 +00001146 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001147 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001148 DIArray Elements = CTy.getTypeArray();
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001149 DIType RTy(Elements.getElement(0));
Eric Christopher0df08e22013-08-08 07:40:37 +00001150 if (RTy)
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001151 addType(&Buffer, RTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001152
1153 bool isPrototyped = true;
1154 // Add arguments.
1155 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1156 DIDescriptor Ty = Elements.getElement(i);
1157 if (Ty.isUnspecifiedParameter()) {
Manman Renb987e512013-10-29 00:53:03 +00001158 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001159 isPrototyped = false;
1160 } else {
Manman Renb987e512013-10-29 00:53:03 +00001161 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001162 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001163 if (DIType(Ty).isArtificial())
1164 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001165 }
1166 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001167 // Add prototype flag if we're dealing with a C language and the
1168 // function has been prototyped.
David Blaikiecb8e4352013-11-15 23:50:53 +00001169 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001170 if (isPrototyped &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001171 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopherd42b92f2012-05-22 18:45:24 +00001172 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001173 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Adrian Prantl99c7af22013-12-18 21:48:19 +00001174
1175 if (CTy.isLValueReference())
1176 addFlag(&Buffer, dwarf::DW_AT_reference);
1177
1178 if (CTy.isRValueReference())
1179 addFlag(&Buffer, dwarf::DW_AT_rvalue_reference);
Eric Christopherc2697f82013-10-19 01:04:47 +00001180 } break;
Devang Patel0e821f42011-04-12 23:21:44 +00001181 case dwarf::DW_TAG_structure_type:
1182 case dwarf::DW_TAG_union_type:
1183 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001184 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001185 DIArray Elements = CTy.getTypeArray();
1186 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001187 DIDescriptor Element = Elements.getElement(i);
1188 DIE *ElemDie = NULL;
Adrian Prantlef129fb2014-01-18 02:12:00 +00001189 if (Element.isSubprogram())
1190 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1191 else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001192 DIDerivedType DDTy(Element);
1193 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
Manman Renb987e512013-10-29 00:53:03 +00001194 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
Manman Ren93b30902013-10-08 18:42:58 +00001195 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
Manman Renb3388602013-10-05 01:43:03 +00001196 dwarf::DW_AT_friend);
Manman Renc6b63922013-10-14 20:33:57 +00001197 } else if (DDTy.isStaticMember()) {
Manman Ren57e6ff72013-10-23 22:57:12 +00001198 getOrCreateStaticMemberDIE(DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001199 } else {
Manman Ren230ec862013-10-23 23:00:44 +00001200 constructMemberDIE(Buffer, DDTy);
Manman Renc6b63922013-10-14 20:33:57 +00001201 }
Eric Christopher7285c7d2012-03-28 07:34:31 +00001202 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001203 DIObjCProperty Property(Element);
Manman Renb987e512013-10-29 00:53:03 +00001204 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
Devang Pateld925d1a2012-02-07 23:33:58 +00001205 StringRef PropertyName = Property.getObjCPropertyName();
1206 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4c960562014-01-23 19:16:28 +00001207 if (Property.getType())
1208 addType(ElemDie, Property.getType());
Eric Christopherd42b92f2012-05-22 18:45:24 +00001209 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001210 StringRef GetterName = Property.getObjCPropertyGetterName();
1211 if (!GetterName.empty())
1212 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1213 StringRef SetterName = Property.getObjCPropertySetterName();
1214 if (!SetterName.empty())
1215 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1216 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001217 if (Property.isReadOnlyObjCProperty())
1218 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1219 if (Property.isReadWriteObjCProperty())
1220 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1221 if (Property.isAssignObjCProperty())
1222 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1223 if (Property.isRetainObjCProperty())
1224 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1225 if (Property.isCopyObjCProperty())
1226 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1227 if (Property.isNonAtomicObjCProperty())
1228 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1229 if (PropertyAttributes)
David Blaikief2443192013-10-21 17:28:37 +00001230 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
Eric Christopherc2697f82013-10-19 01:04:47 +00001231 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001232
Devang Pateld925d1a2012-02-07 23:33:58 +00001233 DIEEntry *Entry = getDIEEntry(Element);
1234 if (!Entry) {
1235 Entry = createDIEEntry(ElemDie);
1236 insertDIEEntry(Element, Entry);
1237 }
Devang Patel403e8192012-02-04 01:30:32 +00001238 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001239 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001240 }
1241
1242 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001243 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001244
Eric Christopher9e429ae2013-10-05 00:27:02 +00001245 DICompositeType ContainingType(resolve(CTy.getContainingType()));
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001246 if (ContainingType)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001247 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001248 getOrCreateTypeDIE(ContainingType));
Devang Patel0e821f42011-04-12 23:21:44 +00001249
Devang Patel12419ae2011-05-12 21:29:42 +00001250 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001251 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001252
Eric Christopherda011dd2011-12-16 23:42:42 +00001253 // Add template parameters to a class, structure or union types.
1254 // FIXME: The support isn't in the metadata for this yet.
1255 if (Tag == dwarf::DW_TAG_class_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001256 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001257 addTemplateParams(Buffer, CTy.getTemplateParams());
1258
1259 break;
1260 }
1261 default:
1262 break;
1263 }
1264
1265 // Add name if not anonymous or intermediate type.
1266 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001267 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001268
Eric Christopher775cbd22012-05-22 18:45:18 +00001269 if (Tag == dwarf::DW_TAG_enumeration_type ||
Eric Christopherc2697f82013-10-19 01:04:47 +00001270 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
Eric Christopher775cbd22012-05-22 18:45:18 +00001271 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001272 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001273 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001274 if (Size)
David Blaikief2443192013-10-21 17:28:37 +00001275 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001276 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001277 // Add zero size if it is not a forward declaration.
David Blaikief2443192013-10-21 17:28:37 +00001278 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
Eric Christopher1cf33382012-06-01 00:22:32 +00001279
1280 // If we're a forward decl, say so.
1281 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001282 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001283
1284 // Add source line info if available.
1285 if (!CTy.isForwardDecl())
1286 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001287
1288 // No harm in adding the runtime language to the declaration.
1289 unsigned RLang = CTy.getRunTimeLang();
1290 if (RLang)
Eric Christopherc2697f82013-10-19 01:04:47 +00001291 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1292 RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001293 }
1294}
1295
Manman Renffc9a712013-10-23 23:05:28 +00001296/// constructTemplateTypeParameterDIE - Construct new DIE for the given
1297/// DITemplateTypeParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001298void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1299 DITemplateTypeParameter TP) {
Manman Renb987e512013-10-29 00:53:03 +00001300 DIE *ParamDIE =
1301 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
Eric Christopher056b6472013-08-08 08:09:43 +00001302 // Add the type if it exists, it could be void and therefore no type.
1303 if (TP.getType())
Manman Ren88b0f942013-10-09 19:46:28 +00001304 addType(ParamDIE, resolve(TP.getType()));
David Blaikie2b380232013-06-22 18:59:11 +00001305 if (!TP.getName().empty())
1306 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001307}
1308
Manman Renffc9a712013-10-23 23:05:28 +00001309/// constructTemplateValueParameterDIE - Construct new DIE for the given
1310/// DITemplateValueParameter.
Eric Christophera5a79422013-12-09 23:32:48 +00001311void
1312DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
David Blaikie319a05f2013-12-02 19:33:10 +00001313 DITemplateValueParameter VP) {
Manman Renb987e512013-10-29 00:53:03 +00001314 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
Eric Christopher0df08e22013-08-08 07:40:37 +00001315
1316 // Add the type if there is one, template template and template parameter
1317 // packs will not have a type.
Eric Christopher691281b2013-10-21 17:48:51 +00001318 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
Manman Ren88b0f942013-10-09 19:46:28 +00001319 addType(ParamDIE, resolve(VP.getType()));
Eric Christopherafb2c412013-08-08 07:40:31 +00001320 if (!VP.getName().empty())
1321 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1322 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001323 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren88b0f942013-10-09 19:46:28 +00001324 addConstantValue(ParamDIE, CI,
1325 isUnsignedDIType(DD, resolve(VP.getType())));
David Blaikiea1e813d2013-05-10 21:52:07 +00001326 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1327 // For declaration non-type template parameters (such as global values and
1328 // functions)
1329 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001330 addOpAddress(Block, Asm->getSymbol(GV));
David Blaikiea1e813d2013-05-10 21:52:07 +00001331 // Emit DW_OP_stack_value to use the address as the immediate value of the
1332 // parameter, rather than a pointer to it.
David Blaikief2443192013-10-21 17:28:37 +00001333 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1334 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001335 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001336 assert(isa<MDString>(Val));
1337 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1338 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001339 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001340 assert(isa<MDNode>(Val));
1341 DIArray A(cast<MDNode>(Val));
1342 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001343 }
1344 }
Devang Patel0e821f42011-04-12 23:21:44 +00001345}
1346
Devang Patel17b53272011-05-06 16:57:54 +00001347/// getOrCreateNameSpace - Create a DIE for DINameSpace.
Eric Christophera5a79422013-12-09 23:32:48 +00001348DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
Manman Renf6b936b2013-10-29 05:49:41 +00001349 // Construct the context before querying for the existence of the DIE in case
1350 // such construction creates the DIE.
1351 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
Manman Renf6b936b2013-10-29 05:49:41 +00001352
Devang Patel17b53272011-05-06 16:57:54 +00001353 DIE *NDie = getDIE(NS);
1354 if (NDie)
1355 return NDie;
Manman Renf6b936b2013-10-29 05:49:41 +00001356 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1357
Eric Christopher4996c702011-11-07 09:24:32 +00001358 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001359 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001360 addAccelNamespace(NS.getName(), NDie);
Eric Christopher2c8b7902013-10-17 02:06:06 +00001361 addGlobalName(NS.getName(), NDie, NS.getContext());
Eric Christopher4996c702011-11-07 09:24:32 +00001362 } else
1363 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001364 addSourceLine(NDie, NS);
Devang Patel17b53272011-05-06 16:57:54 +00001365 return NDie;
1366}
1367
Devang Patel89543712011-08-15 17:24:54 +00001368/// getOrCreateSubprogramDIE - Create new DIE using SP.
Eric Christophera5a79422013-12-09 23:32:48 +00001369DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001370 // Construct the context before querying for the existence of the DIE in case
1371 // such construction creates the DIE (as is the case for member function
1372 // declarations).
Manman Renc50fa112013-10-10 18:40:01 +00001373 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
David Blaikie309ffe42013-10-04 01:39:59 +00001374
Eric Christophere595bae2013-10-04 17:08:38 +00001375 DIE *SPDie = getDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001376 if (SPDie)
1377 return SPDie;
1378
Manman Ren73d697c2013-10-29 00:58:04 +00001379 DISubprogram SPDecl = SP.getFunctionDeclaration();
1380 if (SPDecl.isSubprogram())
1381 // Add subprogram definitions to the CU die directly.
David Blaikie2a80e442013-12-02 22:09:48 +00001382 ContextDIE = UnitDie.get();
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001383
1384 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren73d697c2013-10-29 00:58:04 +00001385 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001386
Rafael Espindola79278362011-11-10 22:34:29 +00001387 DIE *DeclDie = NULL;
Manman Renb9512a72013-10-23 22:12:26 +00001388 if (SPDecl.isSubprogram())
Rafael Espindola79278362011-11-10 22:34:29 +00001389 DeclDie = getOrCreateSubprogramDIE(SPDecl);
Rafael Espindola79278362011-11-10 22:34:29 +00001390
Devang Patel89543712011-08-15 17:24:54 +00001391 // Add function template parameters.
1392 addTemplateParams(*SPDie, SP.getTemplateParams());
1393
Devang Patel89543712011-08-15 17:24:54 +00001394 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001395 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001396 if (DeclDie) {
1397 // Refer function declaration directly.
Manman Ren4c4b69c2013-10-11 23:58:05 +00001398 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
Rafael Espindola79278362011-11-10 22:34:29 +00001399
Devang Patel89543712011-08-15 17:24:54 +00001400 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001401 }
Devang Patel89543712011-08-15 17:24:54 +00001402
Eric Christopheracb71152012-08-23 22:52:55 +00001403 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001404 StringRef LinkageName = SP.getLinkageName();
1405 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001406 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001407 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001408
Devang Patel89543712011-08-15 17:24:54 +00001409 // Constructors and operators for anonymous aggregates do not have names.
1410 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001411 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001412
1413 addSourceLine(SPDie, SP);
1414
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001415 // Add the prototype if we have a prototype and we have a C like
1416 // language.
David Blaikiecb8e4352013-11-15 23:50:53 +00001417 uint16_t Language = getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001418 if (SP.isPrototyped() &&
Eric Christopherc2697f82013-10-19 01:04:47 +00001419 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001420 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001421 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001422
Devang Patel89543712011-08-15 17:24:54 +00001423 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001424 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1425 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001426
David Blaikie5174c842013-05-22 23:22:18 +00001427 DIArray Args = SPTy.getTypeArray();
Eric Christopher691281b2013-10-21 17:48:51 +00001428 // Add a return type. If this is a type like a C/C++ void type we don't add a
1429 // return type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001430 if (Args.getElement(0))
1431 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001432
1433 unsigned VK = SP.getVirtuality();
1434 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001435 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001436 DIEBlock *Block = getDIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001437 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1438 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1439 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
Eric Christopher98b7f172013-11-11 18:52:36 +00001440 ContainingTypeMap.insert(
1441 std::make_pair(SPDie, resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001442 }
1443
1444 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001445 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001446
Devang Patel89543712011-08-15 17:24:54 +00001447 // Add arguments. Do not add arguments for subprogram definition. They will
1448 // be handled while processing variables.
Eric Christopherc2697f82013-10-19 01:04:47 +00001449 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Renb987e512013-10-29 00:53:03 +00001450 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001451 DIType ATy(Args.getElement(i));
David Blaikie5174c842013-05-22 23:22:18 +00001452 addType(Arg, ATy);
1453 if (ATy.isArtificial())
1454 addFlag(Arg, dwarf::DW_AT_artificial);
David Blaikie5174c842013-05-22 23:22:18 +00001455 }
Devang Patel89543712011-08-15 17:24:54 +00001456 }
1457
1458 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001459 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001460
1461 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001462 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001463
1464 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001465 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001466
1467 if (unsigned isa = Asm->getISAEncoding()) {
1468 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1469 }
1470
Adrian Prantl99c7af22013-12-18 21:48:19 +00001471 if (SP.isLValueReference())
1472 addFlag(SPDie, dwarf::DW_AT_reference);
1473
1474 if (SP.isRValueReference())
1475 addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1476
Adrian Prantlef129fb2014-01-18 02:12:00 +00001477 if (SP.isProtected())
1478 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1479 dwarf::DW_ACCESS_protected);
1480 else if (SP.isPrivate())
1481 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1482 dwarf::DW_ACCESS_private);
1483 else
1484 addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1485 dwarf::DW_ACCESS_public);
1486
1487 if (SP.isExplicit())
1488 addFlag(SPDie, dwarf::DW_AT_explicit);
1489
Devang Patel89543712011-08-15 17:24:54 +00001490 return SPDie;
1491}
1492
Devang Pateldfd6ec32011-08-15 17:57:41 +00001493// Return const expression if value is a GEP to access merged global
1494// constant. e.g.
1495// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1496static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1497 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1498 if (!CE || CE->getNumOperands() != 3 ||
1499 CE->getOpcode() != Instruction::GetElementPtr)
1500 return NULL;
1501
1502 // First operand points to a global struct.
1503 Value *Ptr = CE->getOperand(0);
1504 if (!isa<GlobalValue>(Ptr) ||
1505 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1506 return NULL;
1507
1508 // Second operand is zero.
1509 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1510 if (!CI || !CI->isZero())
1511 return NULL;
1512
1513 // Third operand is offset.
1514 if (!isa<ConstantInt>(CE->getOperand(2)))
1515 return NULL;
1516
1517 return CE;
1518}
1519
1520/// createGlobalVariableDIE - create global variable DIE.
Eric Christopher4287a492013-12-09 23:57:44 +00001521void DwarfCompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001522 // Check for pre-existence.
David Blaikie2ad00162013-11-15 23:09:13 +00001523 if (getDIE(GV))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001524 return;
1525
David Blaikie5e390e42014-02-04 01:23:52 +00001526 assert(GV.isGlobalVariable());
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001527
Eric Christopher08f7c8f2013-10-04 23:49:26 +00001528 DIScope GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001529 DIType GTy = GV.getType();
1530
1531 // If this is a static data member definition, some attributes belong
1532 // to the declaration DIE.
1533 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001534 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001535 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1536 if (SDMDecl.Verify()) {
1537 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1538 // We need the declaration DIE that is in the static member's class.
Manman Renc6b63922013-10-14 20:33:57 +00001539 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
Manman Rene697d3c2013-02-01 23:54:37 +00001540 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001541 }
1542
1543 // If this is not a static data member definition, create the variable
1544 // DIE and add the initial set of attributes to it.
1545 if (!VariableDIE) {
Manman Renf6b936b2013-10-29 05:49:41 +00001546 // Construct the context before querying for the existence of the DIE in
1547 // case such construction creates the DIE.
1548 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
Manman Renf6b936b2013-10-29 05:49:41 +00001549
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001550 // Add to map.
David Blaikie52c50202013-11-16 00:29:01 +00001551 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001552
1553 // Add name and type.
1554 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1555 addType(VariableDIE, GTy);
1556
1557 // Add scoping info.
Eric Christopherd2b497b2013-10-16 01:37:49 +00001558 if (!GV.isLocalToUnit())
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001559 addFlag(VariableDIE, dwarf::DW_AT_external);
1560
1561 // Add line number info.
1562 addSourceLine(VariableDIE, GV);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001563 }
1564
Devang Pateldfd6ec32011-08-15 17:57:41 +00001565 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001566 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001567 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001568 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001569 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001570 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001571 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Rafael Espindola79858aa2013-10-29 17:07:16 +00001572 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
David Blaikief2694972013-06-28 20:05:11 +00001573 if (GV.getGlobal()->isThreadLocal()) {
1574 // FIXME: Make this work with -gsplit-dwarf.
1575 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1576 assert((PointerSize == 4 || PointerSize == 8) &&
1577 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001578 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001579 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001580 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001581 if (!DD->useSplitDwarf()) {
1582 // 1) Start with a constNu of the appropriate pointer size
David Blaikief2443192013-10-21 17:28:37 +00001583 addUInt(Block, dwarf::DW_FORM_data1,
David Blaikie8466ca82013-07-01 23:55:52 +00001584 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
Richard Mitton0aafb582013-10-07 18:39:18 +00001585 // 2) containing the (relocated) offset of the TLS variable
1586 // within the module's TLS block.
David Blaikief2443192013-10-21 17:28:37 +00001587 addExpr(Block, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001588 } else {
David Blaikief2443192013-10-21 17:28:37 +00001589 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1590 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001591 }
Richard Mitton0aafb582013-10-07 18:39:18 +00001592 // 3) followed by a custom OP to make the debugger do a TLS lookup.
David Blaikief2443192013-10-21 17:28:37 +00001593 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001594 } else {
1595 DD->addArangeLabel(SymbolCU(this, Sym));
David Blaikief2694972013-06-28 20:05:11 +00001596 addOpAddress(Block, Sym);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001597 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001598 // Do not create specification DIE if context is either compile unit
1599 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001600 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001601 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001602 // Create specification DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001603 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001604 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
David Blaikief2443192013-10-21 17:28:37 +00001605 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001606 // A static member's declaration is already flagged as such.
1607 if (!SDMDecl.Verify())
1608 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001609 } else {
David Blaikief2443192013-10-21 17:28:37 +00001610 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001611 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001612 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001613 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001614 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001615 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1616 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1617 // TAG_variable.
Eric Christopherc2697f82013-10-19 01:04:47 +00001618 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1619 : VariableDIE,
1620 dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001621 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001622 } else if (const ConstantInt *CI =
Eric Christopherc2697f82013-10-19 01:04:47 +00001623 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001624 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001625 // emitting AT_const_value multiple times, we only add AT_const_value when
1626 // it is not a static member.
1627 if (!IsStaticMember)
Manman Renb3388602013-10-05 01:43:03 +00001628 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
David Blaikiea781b25b2013-11-17 21:55:13 +00001629 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001630 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001631 // GV is a merged global.
1632 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1633 Value *Ptr = CE->getOperand(0);
David Blaikiebc7e0d42013-11-27 23:53:52 +00001634 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1635 DD->addArangeLabel(SymbolCU(this, Sym));
1636 addOpAddress(Block, Sym);
David Blaikief2443192013-10-21 17:28:37 +00001637 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
Eric Christopherc2697f82013-10-19 01:04:47 +00001638 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
David Blaikief2443192013-10-21 17:28:37 +00001639 addUInt(Block, dwarf::DW_FORM_udata,
Eric Christopherc2697f82013-10-19 01:04:47 +00001640 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
David Blaikief2443192013-10-21 17:28:37 +00001641 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1642 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001643 }
1644
Eric Christopherc12c2112011-11-11 01:55:22 +00001645 if (addToAccelTable) {
1646 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1647 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001648
Eric Christopherc12c2112011-11-11 01:55:22 +00001649 // If the linkage name is different than the name, go ahead and output
1650 // that as well into the name table.
1651 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1652 addAccelName(GV.getLinkageName(), AddrDIE);
1653 }
Eric Christopherd2b497b2013-10-16 01:37:49 +00001654
1655 if (!GV.isLocalToUnit())
Eric Christopher2c8b7902013-10-17 02:06:06 +00001656 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1657 GV.getContext());
Devang Pateldfd6ec32011-08-15 17:57:41 +00001658}
1659
Devang Patel0e821f42011-04-12 23:21:44 +00001660/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christophera5a79422013-12-09 23:32:48 +00001661void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
Manman Renb987e512013-10-29 00:53:03 +00001662 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
Manman Ren4c4b69c2013-10-11 23:58:05 +00001663 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001664
Bill Wendling28fe9e72012-12-06 07:38:10 +00001665 // The LowerBound value defines the lower bounds which is typically zero for
1666 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1667 // Count == -1 then the array is unbounded and we do not emit
1668 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1669 // Count == 0, then the array has zero elements in which case we do not emit
1670 // an upper bound.
1671 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001672 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001673 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001674
Bill Wendling3495f9b2012-12-06 07:55:19 +00001675 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
David Blaikief2443192013-10-21 17:28:37 +00001676 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
Bill Wendling28fe9e72012-12-06 07:38:10 +00001677
1678 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001679 // FIXME: An unbounded array should reference the expression that defines
1680 // the array.
Eric Christopher98b7f172013-11-11 18:52:36 +00001681 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1682 LowerBound + Count - 1);
Devang Patel0e821f42011-04-12 23:21:44 +00001683}
1684
1685/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001686void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopherdf9955d2013-11-11 18:52:31 +00001687 if (CTy.isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001688 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001689
Eric Christopher0df08e22013-08-08 07:40:37 +00001690 // Emit the element type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001691 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001692
1693 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001694 // FIXME: This type should be passed down from the front end
1695 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001696 DIE *IdxTy = getIndexTyDie();
1697 if (!IdxTy) {
1698 // Construct an anonymous type for index type.
David Blaikie2a80e442013-12-02 22:09:48 +00001699 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
Eric Christophercad9b532013-01-04 21:51:53 +00001700 addString(IdxTy, dwarf::DW_AT_name, "int");
David Blaikief2443192013-10-21 17:28:37 +00001701 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
Devang Patel0e821f42011-04-12 23:21:44 +00001702 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1703 dwarf::DW_ATE_signed);
Devang Patel0e821f42011-04-12 23:21:44 +00001704 setIndexTyDie(IdxTy);
1705 }
1706
1707 // Add subranges to array type.
Eric Christopherdf9955d2013-11-11 18:52:31 +00001708 DIArray Elements = CTy.getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001709 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1710 DIDescriptor Element = Elements.getElement(i);
1711 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1712 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1713 }
1714}
1715
Eric Christopheraeb105f2013-11-11 18:52:39 +00001716/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
Eric Christophera5a79422013-12-09 23:32:48 +00001717void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
Eric Christopheraeb105f2013-11-11 18:52:39 +00001718 DIArray Elements = CTy.getTypeArray();
1719
1720 // Add enumerators to enumeration type.
1721 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001722 DIEnumerator Enum(Elements.getElement(i));
Eric Christopheraeb105f2013-11-11 18:52:39 +00001723 if (Enum.isEnumerator()) {
1724 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001725 StringRef Name = Enum.getName();
Eric Christopheraeb105f2013-11-11 18:52:39 +00001726 addString(Enumerator, dwarf::DW_AT_name, Name);
David Blaikie4f6bf27a2013-11-18 23:33:32 +00001727 int64_t Value = Enum.getEnumValue();
Eric Christophera07e4f52013-11-19 09:28:34 +00001728 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1729 Value);
Eric Christopheraeb105f2013-11-11 18:52:39 +00001730 }
1731 }
1732 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1733 if (DTy) {
1734 addType(&Buffer, DTy);
1735 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1736 }
Devang Patel0e821f42011-04-12 23:21:44 +00001737}
1738
Devang Patel89543712011-08-15 17:24:54 +00001739/// constructContainingTypeDIEs - Construct DIEs for types that contain
1740/// vtables.
Eric Christophera5a79422013-12-09 23:32:48 +00001741void DwarfUnit::constructContainingTypeDIEs() {
Devang Patel89543712011-08-15 17:24:54 +00001742 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
Eric Christopherc2697f82013-10-19 01:04:47 +00001743 CE = ContainingTypeMap.end();
1744 CI != CE; ++CI) {
Devang Patel89543712011-08-15 17:24:54 +00001745 DIE *SPDie = CI->first;
David Blaikie2ad00162013-11-15 23:09:13 +00001746 DIDescriptor D(CI->second);
1747 if (!D)
Eric Christopherc2697f82013-10-19 01:04:47 +00001748 continue;
David Blaikie2ad00162013-11-15 23:09:13 +00001749 DIE *NDie = getDIE(D);
Eric Christopherc2697f82013-10-19 01:04:47 +00001750 if (!NDie)
1751 continue;
Manman Ren4c4b69c2013-10-11 23:58:05 +00001752 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
Devang Patel89543712011-08-15 17:24:54 +00001753 }
1754}
1755
Devang Patel3acc70e2011-08-15 22:04:40 +00001756/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christophera5a79422013-12-09 23:32:48 +00001757DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001758 StringRef Name = DV.getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001759
Devang Patel3acc70e2011-08-15 22:04:40 +00001760 // Define variable debug information entry.
Eric Christopher34a2c872013-11-15 01:43:19 +00001761 DIE *VariableDie = new DIE(DV.getTag());
1762 DbgVariable *AbsVar = DV.getAbstractVariable();
Devang Patel3acc70e2011-08-15 22:04:40 +00001763 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001764 if (AbsDIE)
Manman Ren4c4b69c2013-10-11 23:58:05 +00001765 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001766 else {
David Blaikie715528b2013-08-19 03:34:03 +00001767 if (!Name.empty())
1768 addString(VariableDie, dwarf::DW_AT_name, Name);
Eric Christopher34a2c872013-11-15 01:43:19 +00001769 addSourceLine(VariableDie, DV.getVariable());
1770 addType(VariableDie, DV.getType());
Devang Patel3acc70e2011-08-15 22:04:40 +00001771 }
1772
Eric Christopher34a2c872013-11-15 01:43:19 +00001773 if (DV.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001774 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001775
1776 if (isScopeAbstract) {
Eric Christopher34a2c872013-11-15 01:43:19 +00001777 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001778 return VariableDie;
1779 }
1780
1781 // Add variable address.
1782
Eric Christopher34a2c872013-11-15 01:43:19 +00001783 unsigned Offset = DV.getDotDebugLocOffset();
Devang Patel3acc70e2011-08-15 22:04:40 +00001784 if (Offset != ~0U) {
Eric Christopher33ff6972013-11-21 23:46:41 +00001785 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1786 Asm->GetTempSymbol("debug_loc", Offset));
Eric Christopher34a2c872013-11-15 01:43:19 +00001787 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001788 return VariableDie;
1789 }
1790
Eric Christophercead0332011-10-03 15:49:20 +00001791 // Check if variable is described by a DBG_VALUE instruction.
Eric Christopher34a2c872013-11-15 01:43:19 +00001792 if (const MachineInstr *DVInsn = DV.getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001793 assert(DVInsn->getNumOperands() == 3);
1794 if (DVInsn->getOperand(0).isReg()) {
1795 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001796 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001797 if (DVInsn->getOperand(1).isImm()) {
Eric Christopherc2697f82013-10-19 01:04:47 +00001798 MachineLocation Location(RegOp.getReg(),
1799 DVInsn->getOperand(1).getImm());
Eric Christopher34a2c872013-11-15 01:43:19 +00001800 addVariableAddress(DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001801 } else if (RegOp.getReg())
Eric Christopher34a2c872013-11-15 01:43:19 +00001802 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001803 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher34a2c872013-11-15 01:43:19 +00001804 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001805 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001806 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001807 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001808 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Eric Christopher34a2c872013-11-15 01:43:19 +00001809 isUnsignedDIType(DD, DV.getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001810
Eric Christopher34a2c872013-11-15 01:43:19 +00001811 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001812 return VariableDie;
1813 } else {
1814 // .. else use frame index.
Eric Christopher34a2c872013-11-15 01:43:19 +00001815 int FI = DV.getFrameIndex();
Devang Patel3acc70e2011-08-15 22:04:40 +00001816 if (FI != ~0) {
1817 unsigned FrameReg = 0;
1818 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopherc2697f82013-10-19 01:04:47 +00001819 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
Devang Patel3acc70e2011-08-15 22:04:40 +00001820 MachineLocation Location(FrameReg, Offset);
Eric Christopher34a2c872013-11-15 01:43:19 +00001821 addVariableAddress(DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001822 }
1823 }
1824
Eric Christopher34a2c872013-11-15 01:43:19 +00001825 DV.setDIE(VariableDie);
Devang Patel3acc70e2011-08-15 22:04:40 +00001826 return VariableDie;
1827}
1828
Manman Ren230ec862013-10-23 23:00:44 +00001829/// constructMemberDIE - Construct member DIE from DIDerivedType.
Eric Christophera5a79422013-12-09 23:32:48 +00001830void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
Manman Renb987e512013-10-29 00:53:03 +00001831 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001832 StringRef Name = DT.getName();
1833 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001834 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001835
Manman Ren93b30902013-10-08 18:42:58 +00001836 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001837
1838 addSourceLine(MemberDie, DT);
1839
Eric Christopherc2697f82013-10-19 01:04:47 +00001840 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
Devang Patel0e821f42011-04-12 23:21:44 +00001841
1842 // For C++, virtual base classes are not at fixed offset. Use following
1843 // expression to extract appropriate offset from vtable.
1844 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1845
1846 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
David Blaikief2443192013-10-21 17:28:37 +00001847 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1848 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1849 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1850 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1851 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1852 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1853 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
Devang Patel0e821f42011-04-12 23:21:44 +00001854
David Blaikief2443192013-10-21 17:28:37 +00001855 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
David Blaikie71d34a22013-11-01 00:25:45 +00001856 } else {
1857 uint64_t Size = DT.getSizeInBits();
1858 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1859 uint64_t OffsetInBytes;
1860
1861 if (Size != FieldSize) {
1862 // Handle bitfield.
1863 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1864 getBaseTypeSize(DD, DT) >> 3);
1865 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1866
1867 uint64_t Offset = DT.getOffsetInBits();
1868 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1869 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1870 uint64_t FieldOffset = (HiMark - FieldSize);
1871 Offset -= FieldOffset;
1872
1873 // Maybe we need to work from the other end.
1874 if (Asm->getDataLayout().isLittleEndian())
1875 Offset = FieldSize - (Offset + Size);
1876 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1877
Adrian Prantlc67655a2014-01-28 18:13:47 +00001878 // Here DW_AT_data_member_location points to the anonymous
David Blaikie71d34a22013-11-01 00:25:45 +00001879 // field that includes this bit field.
1880 OffsetInBytes = FieldOffset >> 3;
1881 } else
1882 // This is not a bitfield.
1883 OffsetInBytes = DT.getOffsetInBits() >> 3;
David Blaikie2ada1162014-01-03 00:48:38 +00001884
David Blaikie22b29a52014-01-03 01:30:05 +00001885 if (DD->getDwarfVersion() <= 2) {
1886 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1887 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1888 addUInt(MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1889 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1890 } else
1891 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1892 OffsetInBytes);
David Blaikie71d34a22013-11-01 00:25:45 +00001893 }
Devang Patel0e821f42011-04-12 23:21:44 +00001894
1895 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001896 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001897 dwarf::DW_ACCESS_protected);
1898 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001899 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001900 dwarf::DW_ACCESS_private);
1901 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001902 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001903 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001904 dwarf::DW_ACCESS_public);
1905 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001906 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001907 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001908
1909 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001910 if (MDNode *PNode = DT.getObjCProperty())
1911 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001912 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001913 PropertyDie);
1914
David Blaikie37fefc32012-12-13 22:43:07 +00001915 if (DT.isArtificial())
1916 addFlag(MemberDie, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001917}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001918
Manman Renc6b63922013-10-14 20:33:57 +00001919/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
Eric Christophera5a79422013-12-09 23:32:48 +00001920DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001921 if (!DT.Verify())
1922 return NULL;
1923
Manman Renc6b63922013-10-14 20:33:57 +00001924 // Construct the context before querying for the existence of the DIE in case
1925 // such construction creates the DIE.
1926 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
David Blaikiebd700e42013-11-14 21:24:34 +00001927 assert(dwarf::isType(ContextDIE->getTag()) &&
1928 "Static member should belong to a type.");
Manman Renc6b63922013-10-14 20:33:57 +00001929
1930 DIE *StaticMemberDIE = getDIE(DT);
1931 if (StaticMemberDIE)
1932 return StaticMemberDIE;
1933
Manman Renb987e512013-10-29 00:53:03 +00001934 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
Manman Renc6b63922013-10-14 20:33:57 +00001935
Manman Ren93b30902013-10-08 18:42:58 +00001936 DIType Ty = resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001937
1938 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1939 addType(StaticMemberDIE, Ty);
1940 addSourceLine(StaticMemberDIE, DT);
1941 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1942 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1943
1944 // FIXME: We could omit private if the parent is a class_type, and
1945 // public if the parent is something else.
1946 if (DT.isProtected())
1947 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1948 dwarf::DW_ACCESS_protected);
1949 else if (DT.isPrivate())
1950 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1951 dwarf::DW_ACCESS_private);
1952 else
1953 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1954 dwarf::DW_ACCESS_public);
1955
1956 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Renb3388602013-10-05 01:43:03 +00001957 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001958 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1959 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001960
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001961 return StaticMemberDIE;
1962}
David Blaikie6b288cf2013-10-30 20:42:41 +00001963
Eric Christophera5a79422013-12-09 23:32:48 +00001964void DwarfUnit::emitHeader(const MCSection *ASection,
David Blaikie3332d4c2013-12-11 21:14:02 +00001965 const MCSymbol *ASectionSym) const {
David Blaikie6b288cf2013-10-30 20:42:41 +00001966 Asm->OutStreamer.AddComment("DWARF version number");
1967 Asm->EmitInt16(DD->getDwarfVersion());
1968 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
David Blaikie6896e192013-12-04 23:39:02 +00001969 // We share one abbreviations table across all units so it's always at the
1970 // start of the section. Use a relocatable offset where needed to ensure
1971 // linking doesn't invalidate that offset.
David Blaikie91db9ab2013-12-04 18:12:28 +00001972 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
David Blaikie6b288cf2013-10-30 20:42:41 +00001973 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1974 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
1975}
David Blaikiebc563272013-12-13 21:33:40 +00001976
Juergen Ributzkadb9ee002013-12-14 12:23:14 +00001977DwarfCompileUnit::~DwarfCompileUnit() {}
1978DwarfTypeUnit::~DwarfTypeUnit() {}
1979
David Blaikiebc563272013-12-13 21:33:40 +00001980void DwarfTypeUnit::emitHeader(const MCSection *ASection,
1981 const MCSymbol *ASectionSym) const {
1982 DwarfUnit::emitHeader(ASection, ASectionSym);
1983 Asm->OutStreamer.AddComment("Type Signature");
1984 Asm->OutStreamer.EmitIntValue(TypeSignature, sizeof(TypeSignature));
1985 Asm->OutStreamer.AddComment("Type DIE Offset");
David Blaikie15ed5eb2014-01-10 01:38:41 +00001986 // In a skeleton type unit there is no type DIE so emit a zero offset.
1987 Asm->OutStreamer.EmitIntValue(Ty ? Ty->getOffset() : 0,
1988 sizeof(Ty->getOffset()));
David Blaikiebc563272013-12-13 21:33:40 +00001989}
1990
1991void DwarfTypeUnit::initSection(const MCSection *Section) {
1992 assert(!this->Section);
1993 this->Section = Section;
1994 // Since each type unit is contained in its own COMDAT section, the begin
1995 // label and the section label are the same. Using the begin label emission in
1996 // DwarfDebug to emit the section label as well is slightly subtle/sneaky, but
1997 // the only other alternative of lazily constructing start-of-section labels
1998 // and storing a mapping in DwarfDebug (or AsmPrinter).
1999 this->SectionSym = this->LabelBegin =
2000 Asm->GetTempSymbol(Section->getLabelBeginName(), getUniqueID());
2001 this->LabelEnd =
2002 Asm->GetTempSymbol(Section->getLabelEndName(), getUniqueID());
2003 this->LabelRange = Asm->GetTempSymbol("gnu_ranges", getUniqueID());
2004}