blob: 95d97c66175a27c8558fa16378c70b72cc435a64 [file] [log] [blame]
Devang Patel0e821f42011-04-12 23:21:44 +00001//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
2//
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
16#include "DwarfCompileUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel0e821f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendlingf799efd2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Eric Christopher0a917b72011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Pateldfd6ec32011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel0e821f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
David Blaikief2694972013-06-28 20:05:11 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Devang Patel0e821f42011-04-12 23:21:44 +000031#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel0e821f42011-04-12 23:21:44 +000032
33using namespace llvm;
34
35/// CompileUnit - Compile unit constructor.
Eric Christopherbfceb2f2013-08-26 23:50:38 +000036CompileUnit::CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A,
37 DwarfDebug *DW, DwarfUnits *DWU)
38 : UniqueID(UID), Node(N), CUDie(D), Asm(A), DD(DW), DU(DWU), IndexTyDie(0),
39 DebugInfoOffset(0) {
Devang Patel0e821f42011-04-12 23:21:44 +000040 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
Eric Christopherc57baee2013-05-08 00:58:51 +000041 insertDIE(N, D);
Devang Patel0e821f42011-04-12 23:21:44 +000042}
43
44/// ~CompileUnit - Destructor for compile unit.
45CompileUnit::~CompileUnit() {
46 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
47 DIEBlocks[j]->~DIEBlock();
48}
49
50/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
51/// information entry.
52DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
53 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
54 return Value;
55}
56
Bill Wendling3495f9b2012-12-06 07:55:19 +000057/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling28fe9e72012-12-06 07:38:10 +000058/// DWARF version doesn't handle the language, return -1.
Bill Wendling3495f9b2012-12-06 07:55:19 +000059int64_t CompileUnit::getDefaultLowerBound() const {
Eric Christopherbfceb2f2013-08-26 23:50:38 +000060 switch (DICompileUnit(Node).getLanguage()) {
Bill Wendling28fe9e72012-12-06 07:38:10 +000061 default:
62 break;
63
64 case dwarf::DW_LANG_C89:
65 case dwarf::DW_LANG_C99:
66 case dwarf::DW_LANG_C:
67 case dwarf::DW_LANG_C_plus_plus:
68 case dwarf::DW_LANG_ObjC:
69 case dwarf::DW_LANG_ObjC_plus_plus:
70 return 0;
71
72 case dwarf::DW_LANG_Fortran77:
73 case dwarf::DW_LANG_Fortran90:
74 case dwarf::DW_LANG_Fortran95:
75 return 1;
76
77 // The languages below have valid values only if the DWARF version >= 4.
78 case dwarf::DW_LANG_Java:
79 case dwarf::DW_LANG_Python:
80 case dwarf::DW_LANG_UPC:
81 case dwarf::DW_LANG_D:
82 if (dwarf::DWARF_VERSION >= 4)
83 return 0;
84 break;
85
86 case dwarf::DW_LANG_Ada83:
87 case dwarf::DW_LANG_Ada95:
88 case dwarf::DW_LANG_Cobol74:
89 case dwarf::DW_LANG_Cobol85:
90 case dwarf::DW_LANG_Modula2:
91 case dwarf::DW_LANG_Pascal83:
92 case dwarf::DW_LANG_PLI:
93 if (dwarf::DWARF_VERSION >= 4)
94 return 1;
95 break;
96 }
97
98 return -1;
99}
100
Eric Christopherbb69a272012-08-24 01:14:27 +0000101/// addFlag - Add a flag that is true.
Eric Christopher31b05762013-08-08 01:41:00 +0000102void CompileUnit::addFlag(DIE *Die, uint16_t Attribute) {
Michael Gottesmanc89466f2013-09-04 04:39:38 +0000103 if (DD->getDwarfVersion() >= 4)
Eric Christopherbb69a272012-08-24 01:14:27 +0000104 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
105 DIEIntegerOne);
106 else
107 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
108}
109
Devang Patel0e821f42011-04-12 23:21:44 +0000110/// addUInt - Add an unsigned integer attribute data and value.
111///
Eric Christopher31b05762013-08-08 01:41:00 +0000112void CompileUnit::addUInt(DIE *Die, uint16_t Attribute,
113 uint16_t Form, uint64_t Integer) {
Devang Patel0e821f42011-04-12 23:21:44 +0000114 if (!Form) Form = DIEInteger::BestForm(false, Integer);
115 DIEValue *Value = Integer == 1 ?
116 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
117 Die->addValue(Attribute, Form, Value);
118}
119
120/// addSInt - Add an signed integer attribute data and value.
121///
Eric Christopher31b05762013-08-08 01:41:00 +0000122void CompileUnit::addSInt(DIE *Die, uint16_t Attribute,
123 uint16_t Form, int64_t Integer) {
Devang Patel0e821f42011-04-12 23:21:44 +0000124 if (!Form) Form = DIEInteger::BestForm(true, Integer);
125 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
126 Die->addValue(Attribute, Form, Value);
127}
128
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000129/// addString - Add a string attribute data and value. We always emit a
130/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopherfba22602013-01-07 19:32:45 +0000131/// more predictable sizes. In the case of split dwarf we emit an index
132/// into another table which gets us the static offset into the string
133/// table.
Eric Christopher31b05762013-08-08 01:41:00 +0000134void CompileUnit::addString(DIE *Die, uint16_t Attribute, StringRef String) {
Eric Christopher67646432013-07-26 17:02:41 +0000135 DIEValue *Value;
Eric Christopher31b05762013-08-08 01:41:00 +0000136 uint16_t Form;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000137 if (!DD->useSplitDwarf()) {
138 MCSymbol *Symb = DU->getStringPoolEntry(String);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000139 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000140 Value = new (DIEValueAllocator) DIELabel(Symb);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000141 else {
142 MCSymbol *StringPool = DU->getStringPoolSym();
143 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
144 }
Eric Christopher67646432013-07-26 17:02:41 +0000145 Form = dwarf::DW_FORM_strp;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000146 } else {
147 unsigned idx = DU->getStringPoolIndex(String);
Eric Christopher67646432013-07-26 17:02:41 +0000148 Value = new (DIEValueAllocator) DIEInteger(idx);
149 Form = dwarf::DW_FORM_GNU_str_index;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000150 }
Eric Christopher67646432013-07-26 17:02:41 +0000151 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
152 Die->addValue(Attribute, Form, Str);
Eric Christopher2cbd5762013-01-07 19:32:41 +0000153}
154
155/// addLocalString - Add a string attribute data and value. This is guaranteed
156/// to be in the local string pool instead of indirected.
Eric Christopher31b05762013-08-08 01:41:00 +0000157void CompileUnit::addLocalString(DIE *Die, uint16_t Attribute,
Eric Christopher2cbd5762013-01-07 19:32:41 +0000158 StringRef String) {
Eric Christophere698f532012-12-20 21:58:36 +0000159 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000160 DIEValue *Value;
161 if (Asm->needsRelocationsForDwarfStringPool())
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000162 Value = new (DIEValueAllocator) DIELabel(Symb);
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000163 else {
Eric Christophere698f532012-12-20 21:58:36 +0000164 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000165 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000166 }
Nick Lewyckycc64ae12011-10-28 05:29:47 +0000167 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel0e821f42011-04-12 23:21:44 +0000168}
169
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000170/// addExpr - Add a Dwarf expression attribute data and value.
Devang Patel0e821f42011-04-12 23:21:44 +0000171///
Eric Christopher31b05762013-08-08 01:41:00 +0000172void CompileUnit::addExpr(DIE *Die, uint16_t Attribute, uint16_t Form,
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000173 const MCExpr *Expr) {
174 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
Devang Patel0e821f42011-04-12 23:21:44 +0000175 Die->addValue(Attribute, Form, Value);
176}
177
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000178/// addLabel - Add a Dwarf label attribute data and value.
179///
Eric Christopher31b05762013-08-08 01:41:00 +0000180void CompileUnit::addLabel(DIE *Die, uint16_t Attribute, uint16_t Form,
David Blaikief3cd7c52013-06-28 20:05:04 +0000181 const MCSymbol *Label) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000182 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
183 Die->addValue(Attribute, Form, Value);
David Blaikief3cd7c52013-06-28 20:05:04 +0000184}
185
Eric Christopher962c9082013-01-15 23:56:56 +0000186/// addLabelAddress - Add a dwarf label attribute data and value using
187/// DW_FORM_addr or DW_FORM_GNU_addr_index.
188///
Eric Christopher31b05762013-08-08 01:41:00 +0000189void CompileUnit::addLabelAddress(DIE *Die, uint16_t Attribute,
Eric Christopher962c9082013-01-15 23:56:56 +0000190 MCSymbol *Label) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000191 if (Label)
192 DD->addArangeLabel(SymbolCU(this, Label));
Richard Mitton21101b32013-09-19 23:21:01 +0000193
Eric Christopher962c9082013-01-15 23:56:56 +0000194 if (!DD->useSplitDwarf()) {
195 if (Label != NULL) {
Ulrich Weigand396ba8b2013-07-02 18:46:26 +0000196 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
Eric Christopher962c9082013-01-15 23:56:56 +0000197 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
198 } else {
199 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
200 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
201 }
202 } else {
203 unsigned idx = DU->getAddrPoolIndex(Label);
204 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
205 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
206 }
207}
208
Eric Christophere9ec2452013-01-18 22:11:33 +0000209/// addOpAddress - Add a dwarf op address data and value using the
210/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
211///
David Blaikief3cd7c52013-06-28 20:05:04 +0000212void CompileUnit::addOpAddress(DIE *Die, const MCSymbol *Sym) {
Alexey Samsonov4436bf02013-10-03 08:54:43 +0000213 DD->addArangeLabel(SymbolCU(this, Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000214 if (!DD->useSplitDwarf()) {
215 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
216 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
217 } else {
Eric Christophere9ec2452013-01-18 22:11:33 +0000218 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
David Blaikie8466ca82013-07-01 23:55:52 +0000219 addUInt(Die, 0, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
Eric Christophere9ec2452013-01-18 22:11:33 +0000220 }
221}
222
Devang Patel0e821f42011-04-12 23:21:44 +0000223/// addDelta - Add a label delta attribute data and value.
224///
Eric Christopher31b05762013-08-08 01:41:00 +0000225void CompileUnit::addDelta(DIE *Die, uint16_t Attribute, uint16_t Form,
Devang Patel0e821f42011-04-12 23:21:44 +0000226 const MCSymbol *Hi, const MCSymbol *Lo) {
227 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
228 Die->addValue(Attribute, Form, Value);
229}
230
231/// addDIEEntry - Add a DIE attribute data and value.
232///
Eric Christopher31b05762013-08-08 01:41:00 +0000233void CompileUnit::addDIEEntry(DIE *Die, uint16_t Attribute, uint16_t Form,
Devang Patel0e821f42011-04-12 23:21:44 +0000234 DIE *Entry) {
Manman Ren8990d7e2013-10-01 19:52:23 +0000235 DD->addDIEEntry(Die, Attribute, Form, createDIEEntry(Entry));
Devang Patel0e821f42011-04-12 23:21:44 +0000236}
237
Devang Patel0e821f42011-04-12 23:21:44 +0000238/// addBlock - Add block data.
239///
Eric Christopher31b05762013-08-08 01:41:00 +0000240void CompileUnit::addBlock(DIE *Die, uint16_t Attribute, uint16_t Form,
Devang Patel0e821f42011-04-12 23:21:44 +0000241 DIEBlock *Block) {
242 Block->ComputeSize(Asm);
243 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
244 Die->addValue(Attribute, Block->BestForm(), Block);
245}
246
247/// addSourceLine - Add location information to specified debug information
248/// entry.
249void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
250 // Verify variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000251 if (!V.isVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000252 return;
Eric Christopher92331fd2012-11-21 00:34:38 +0000253
Devang Patel0e821f42011-04-12 23:21:44 +0000254 unsigned Line = V.getLineNumber();
255 if (Line == 0)
256 return;
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000257 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000258 V.getContext().getDirectory(),
259 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000260 assert(FileID && "Invalid file id");
261 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
262 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
263}
264
265/// addSourceLine - Add location information to specified debug information
266/// entry.
267void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
268 // Verify global variable.
Manman Ren7504ed42013-07-08 18:33:29 +0000269 if (!G.isGlobalVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000270 return;
271
272 unsigned Line = G.getLineNumber();
273 if (Line == 0)
274 return;
Manman Ren1e427202013-03-07 01:42:00 +0000275 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
276 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000277 assert(FileID && "Invalid file id");
278 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
279 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
280}
281
282/// addSourceLine - Add location information to specified debug information
283/// entry.
284void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
285 // Verify subprogram.
Manman Ren7504ed42013-07-08 18:33:29 +0000286 if (!SP.isSubprogram())
Devang Patel0e821f42011-04-12 23:21:44 +0000287 return;
Eric Christopher7734ca22012-03-15 23:55:40 +0000288
Devang Patel0e821f42011-04-12 23:21:44 +0000289 // If the line number is 0, don't add it.
Eric Christopher7734ca22012-03-15 23:55:40 +0000290 unsigned Line = SP.getLineNumber();
291 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000292 return;
293
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000294 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000295 SP.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000296 assert(FileID && "Invalid file id");
297 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
298 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
299}
300
301/// addSourceLine - Add location information to specified debug information
302/// entry.
303void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
304 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000305 if (!Ty.isType())
Devang Patel0e821f42011-04-12 23:21:44 +0000306 return;
307
308 unsigned Line = Ty.getLineNumber();
Eric Christopher7734ca22012-03-15 23:55:40 +0000309 if (Line == 0)
Devang Patel0e821f42011-04-12 23:21:44 +0000310 return;
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000311 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000312 Ty.getDirectory(), getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000313 assert(FileID && "Invalid file id");
314 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
315 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
316}
317
318/// addSourceLine - Add location information to specified debug information
319/// entry.
Eric Christopher70e1bd82012-03-29 08:42:56 +0000320void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
321 // Verify type.
Manman Ren7504ed42013-07-08 18:33:29 +0000322 if (!Ty.isObjCProperty())
Eric Christopher70e1bd82012-03-29 08:42:56 +0000323 return;
324
325 unsigned Line = Ty.getLineNumber();
326 if (Line == 0)
327 return;
328 DIFile File = Ty.getFile();
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000329 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren1e427202013-03-07 01:42:00 +0000330 File.getDirectory(), getUniqueID());
Eric Christopher70e1bd82012-03-29 08:42:56 +0000331 assert(FileID && "Invalid file id");
332 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
333 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
334}
335
336/// addSourceLine - Add location information to specified debug information
337/// entry.
Devang Patel0e821f42011-04-12 23:21:44 +0000338void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
339 // Verify namespace.
340 if (!NS.Verify())
341 return;
342
343 unsigned Line = NS.getLineNumber();
344 if (Line == 0)
345 return;
346 StringRef FN = NS.getFilename();
347
Manman Ren1e427202013-03-07 01:42:00 +0000348 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
349 getUniqueID());
Devang Patel0e821f42011-04-12 23:21:44 +0000350 assert(FileID && "Invalid file id");
351 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
352 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
353}
354
Eric Christopher92331fd2012-11-21 00:34:38 +0000355/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patel77dc5412011-04-27 22:45:24 +0000356/// DbgVariable based on provided MachineLocation.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000357void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
Devang Patel77dc5412011-04-27 22:45:24 +0000358 MachineLocation Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000359 if (DV.variableHasComplexAddress())
Devang Patel0e821f42011-04-12 23:21:44 +0000360 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000361 else if (DV.isBlockByrefVariable())
Devang Patel0e821f42011-04-12 23:21:44 +0000362 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
363 else
David Blaikieea2605d2013-06-20 00:25:24 +0000364 addAddress(Die, dwarf::DW_AT_location, Location,
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000365 DV.getVariable().isIndirect());
Devang Patel0e821f42011-04-12 23:21:44 +0000366}
367
Devang Patelba5fbf12011-04-26 19:06:18 +0000368/// addRegisterOp - Add register operand.
369void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
370 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
371 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
372 if (DWReg < 32)
373 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
374 else {
375 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
376 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
377 }
378}
379
380/// addRegisterOffset - Add register offset.
381void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
382 int64_t Offset) {
383 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
384 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
385 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
386 if (Reg == TRI->getFrameRegister(*Asm->MF))
387 // If variable offset is based in frame register then use fbreg.
388 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
389 else if (DWReg < 32)
390 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
391 else {
392 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
393 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
394 }
395 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
396}
397
398/// addAddress - Add an address attribute to a die based on the location
399/// provided.
Eric Christopher31b05762013-08-08 01:41:00 +0000400void CompileUnit::addAddress(DIE *Die, uint16_t Attribute,
David Blaikieea2605d2013-06-20 00:25:24 +0000401 const MachineLocation &Location, bool Indirect) {
Devang Patelba5fbf12011-04-26 19:06:18 +0000402 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
403
David Blaikieea2605d2013-06-20 00:25:24 +0000404 if (Location.isReg() && !Indirect)
Devang Patelba5fbf12011-04-26 19:06:18 +0000405 addRegisterOp(Block, Location.getReg());
David Blaikieea2605d2013-06-20 00:25:24 +0000406 else {
Devang Patelba5fbf12011-04-26 19:06:18 +0000407 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
David Blaikieea2605d2013-06-20 00:25:24 +0000408 if (Indirect && !Location.isReg()) {
409 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
410 }
411 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000412
413 // Now attach the location information to the DIE.
414 addBlock(Die, Attribute, 0, Block);
415}
416
Devang Patel0e821f42011-04-12 23:21:44 +0000417/// addComplexAddress - Start with the address based on the location provided,
418/// and generate the DWARF information necessary to find the actual variable
419/// given the extra address information encoded in the DIVariable, starting from
420/// the starting location. Add the DWARF information to the die.
421///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000422void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
Eric Christopher31b05762013-08-08 01:41:00 +0000423 uint16_t Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000424 const MachineLocation &Location) {
Devang Patel0e821f42011-04-12 23:21:44 +0000425 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000426 unsigned N = DV.getNumAddrElements();
Devang Patel3e021532011-04-28 02:22:40 +0000427 unsigned i = 0;
428 if (Location.isReg()) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000429 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Devang Patel3e021532011-04-28 02:22:40 +0000430 // If first address element is OpPlus then emit
431 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000432 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
Devang Patel3e021532011-04-28 02:22:40 +0000433 i = 2;
434 } else
435 addRegisterOp(Block, Location.getReg());
436 }
Devang Patelba5fbf12011-04-26 19:06:18 +0000437 else
438 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000439
Devang Patel3e021532011-04-28 02:22:40 +0000440 for (;i < N; ++i) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000441 uint64_t Element = DV.getAddrElement(i);
Devang Patel0e821f42011-04-12 23:21:44 +0000442 if (Element == DIBuilder::OpPlus) {
443 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000444 addUInt(Block, 0, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
Devang Patel0e821f42011-04-12 23:21:44 +0000445 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher4d250522012-05-08 18:56:00 +0000446 if (!Location.isReg())
447 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel0e821f42011-04-12 23:21:44 +0000448 } else llvm_unreachable("unknown DIBuilder Opcode");
449 }
450
451 // Now attach the location information to the DIE.
452 addBlock(Die, Attribute, 0, Block);
453}
454
455/* Byref variables, in Blocks, are declared by the programmer as "SomeType
456 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
457 gives the variable VarName either the struct, or a pointer to the struct, as
458 its type. This is necessary for various behind-the-scenes things the
459 compiler needs to do with by-reference variables in Blocks.
460
461 However, as far as the original *programmer* is concerned, the variable
462 should still have type 'SomeType', as originally declared.
463
464 The function getBlockByrefType dives into the __Block_byref_x_VarName
465 struct to find the original type of the variable, which is then assigned to
466 the variable's Debug Information Entry as its real type. So far, so good.
467 However now the debugger will expect the variable VarName to have the type
468 SomeType. So we need the location attribute for the variable to be an
469 expression that explains to the debugger how to navigate through the
470 pointers and struct to find the actual variable of type SomeType.
471
472 The following function does just that. We start by getting
473 the "normal" location for the variable. This will be the location
474 of either the struct __Block_byref_x_VarName or the pointer to the
475 struct __Block_byref_x_VarName.
476
477 The struct will look something like:
478
479 struct __Block_byref_x_VarName {
480 ... <various fields>
481 struct __Block_byref_x_VarName *forwarding;
482 ... <various other fields>
483 SomeType VarName;
484 ... <maybe more fields>
485 };
486
487 If we are given the struct directly (as our starting point) we
488 need to tell the debugger to:
489
490 1). Add the offset of the forwarding field.
491
492 2). Follow that pointer to get the real __Block_byref_x_VarName
493 struct to use (the real one may have been copied onto the heap).
494
495 3). Add the offset for the field VarName, to find the actual variable.
496
497 If we started with a pointer to the struct, then we need to
498 dereference that pointer first, before the other steps.
499 Translating this into DWARF ops, we will need to append the following
500 to the current location description for the variable:
501
502 DW_OP_deref -- optional, if we start with a pointer
503 DW_OP_plus_uconst <forward_fld_offset>
504 DW_OP_deref
505 DW_OP_plus_uconst <varName_fld_offset>
506
507 That is what this function does. */
508
509/// addBlockByrefAddress - Start with the address based on the location
510/// provided, and generate the DWARF information necessary to find the
511/// actual Block variable (navigating the Block struct) based on the
512/// starting location. Add the DWARF information to the die. For
513/// more information, read large comment just above here.
514///
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000515void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
Eric Christopher31b05762013-08-08 01:41:00 +0000516 uint16_t Attribute,
Devang Patel0e821f42011-04-12 23:21:44 +0000517 const MachineLocation &Location) {
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000518 DIType Ty = DV.getType();
Devang Patel0e821f42011-04-12 23:21:44 +0000519 DIType TmpTy = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000520 uint16_t Tag = Ty.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000521 bool isPointer = false;
522
Eric Christopherbf2d23c2013-06-24 21:07:27 +0000523 StringRef varName = DV.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000524
525 if (Tag == dwarf::DW_TAG_pointer_type) {
526 DIDerivedType DTy = DIDerivedType(Ty);
Manman Ren9a0a6702013-10-01 23:45:54 +0000527 TmpTy = DD->resolve(DTy.getTypeDerivedFrom());
Devang Patel0e821f42011-04-12 23:21:44 +0000528 isPointer = true;
529 }
530
531 DICompositeType blockStruct = DICompositeType(TmpTy);
532
533 // Find the __forwarding field and the variable field in the __Block_byref
534 // struct.
535 DIArray Fields = blockStruct.getTypeArray();
536 DIDescriptor varField = DIDescriptor();
537 DIDescriptor forwardingField = DIDescriptor();
538
539 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
540 DIDescriptor Element = Fields.getElement(i);
541 DIDerivedType DT = DIDerivedType(Element);
542 StringRef fieldName = DT.getName();
543 if (fieldName == "__forwarding")
544 forwardingField = Element;
545 else if (fieldName == varName)
546 varField = Element;
547 }
548
549 // Get the offsets for the forwarding field and the variable field.
550 unsigned forwardingFieldOffset =
551 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
552 unsigned varFieldOffset =
553 DIDerivedType(varField).getOffsetInBits() >> 3;
554
555 // Decode the original location, and use that as the start of the byref
556 // variable's location.
Devang Patel0e821f42011-04-12 23:21:44 +0000557 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
558
Eric Christopheref9d7102012-07-04 02:02:18 +0000559 if (Location.isReg())
560 addRegisterOp(Block, Location.getReg());
561 else
562 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel0e821f42011-04-12 23:21:44 +0000563
564 // If we started with a pointer to the __Block_byref... struct, then
565 // the first thing we need to do is dereference the pointer (DW_OP_deref).
566 if (isPointer)
567 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
568
569 // Next add the offset for the '__forwarding' field:
570 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
571 // adding the offset if it's 0.
572 if (forwardingFieldOffset > 0) {
573 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
574 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
575 }
576
577 // Now dereference the __forwarding field to get to the real __Block_byref
578 // struct: DW_OP_deref.
579 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
580
581 // Now that we've got the real __Block_byref... struct, add the offset
582 // for the variable's field to get to the location of the actual variable:
583 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
584 if (varFieldOffset > 0) {
585 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
586 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
587 }
588
589 // Now attach the location information to the DIE.
590 addBlock(Die, Attribute, 0, Block);
591}
592
Devang Patelbcd50a12011-07-20 21:57:04 +0000593/// isTypeSigned - Return true if the type is signed.
Manman Ren9a0a6702013-10-01 23:45:54 +0000594static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
Devang Patelbcd50a12011-07-20 21:57:04 +0000595 if (Ty.isDerivedType())
Manman Ren9a0a6702013-10-01 23:45:54 +0000596 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
597 SizeInBits);
Devang Patelbcd50a12011-07-20 21:57:04 +0000598 if (Ty.isBasicType())
599 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
600 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
601 *SizeInBits = Ty.getSizeInBits();
602 return true;
603 }
604 return false;
605}
606
Manman Ren9a0a6702013-10-01 23:45:54 +0000607/// Return true if type encoding is unsigned.
608static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
609 DIDerivedType DTy(Ty);
610 if (DTy.isDerivedType())
611 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
612
613 DIBasicType BTy(Ty);
614 if (BTy.isBasicType()) {
615 unsigned Encoding = BTy.getEncoding();
616 if (Encoding == dwarf::DW_ATE_unsigned ||
617 Encoding == dwarf::DW_ATE_unsigned_char ||
618 Encoding == dwarf::DW_ATE_boolean)
619 return true;
620 }
621 return false;
622}
623
624/// If this type is derived from a base type then return base type size.
625static uint64_t getOriginalTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
626 unsigned Tag = Ty.getTag();
627
628 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
629 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
630 Tag != dwarf::DW_TAG_restrict_type)
631 return Ty.getSizeInBits();
632
633 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
634
635 // If this type is not derived from any type then take conservative approach.
636 if (!BaseType.isValid())
637 return Ty.getSizeInBits();
638
639 // If this is a derived type, go ahead and get the base type, unless it's a
640 // reference then it's just the size of the field. Pointer types have no need
641 // of this since they're a different type of qualification on the type.
642 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
643 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
644 return Ty.getSizeInBits();
645
646 if (BaseType.isDerivedType())
647 return getOriginalTypeSize(DD, DIDerivedType(BaseType));
648
649 return BaseType.getSizeInBits();
650}
651
Devang Patel0e821f42011-04-12 23:21:44 +0000652/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000653void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
Devang Patel3c6aed22011-05-27 16:45:18 +0000654 DIType Ty) {
David Blaikiea1e813d2013-05-10 21:52:07 +0000655 // FIXME: This is a bit conservative/simple - it emits negative values at
656 // their maximum bit width which is a bit unfortunate (& doesn't prefer
657 // udata/sdata over dataN as suggested by the DWARF spec)
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000658 assert(MO.isImm() && "Invalid machine operand!");
Devang Patelbcd50a12011-07-20 21:57:04 +0000659 int SizeInBits = -1;
Manman Ren9a0a6702013-10-01 23:45:54 +0000660 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
Eric Christopher9d1daa82013-08-27 23:49:04 +0000661 uint16_t Form;
Devang Patelf1d04702011-05-27 18:15:52 +0000662
Eric Christopher9d1daa82013-08-27 23:49:04 +0000663 // If we're a signed constant definitely use sdata.
664 if (SignedConstant) {
665 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
666 return;
667 }
668
669 // Else use data for now unless it's larger than we can deal with.
670 switch (SizeInBits) {
671 case 8:
672 Form = dwarf::DW_FORM_data1;
673 break;
674 case 16:
675 Form = dwarf::DW_FORM_data2;
676 break;
677 case 32:
678 Form = dwarf::DW_FORM_data4;
679 break;
680 case 64:
681 Form = dwarf::DW_FORM_data8;
682 break;
683 default:
684 Form = dwarf::DW_FORM_udata;
685 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
686 return;
687 }
688 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
Devang Patel0e821f42011-04-12 23:21:44 +0000689}
690
691/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000692void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000693 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel0e821f42011-04-12 23:21:44 +0000694 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
695 APFloat FPImm = MO.getFPImm()->getValueAPF();
696
697 // Get the raw data form of the floating point.
698 const APInt FltVal = FPImm.bitcastToAPInt();
699 const char *FltPtr = (const char*)FltVal.getRawData();
700
701 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000702 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000703 int Incr = (LittleEndian ? 1 : -1);
704 int Start = (LittleEndian ? 0 : NumBytes - 1);
705 int Stop = (LittleEndian ? NumBytes : -1);
706
707 // Output the constant to DWARF one byte at a time.
708 for (; Start != Stop; Start += Incr)
709 addUInt(Block, 0, dwarf::DW_FORM_data1,
710 (unsigned char)0xFF & FltPtr[Start]);
711
712 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000713}
714
David Blaikiea39a76e2013-01-20 01:18:01 +0000715/// addConstantFPValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000716void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000717 // Pass this down to addConstantValue as an unsigned bag of bits.
718 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
David Blaikiea39a76e2013-01-20 01:18:01 +0000719}
720
Devang Patel0e821f42011-04-12 23:21:44 +0000721/// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000722void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel0e821f42011-04-12 23:21:44 +0000723 bool Unsigned) {
Eric Christopher78fcf4902013-07-03 01:08:30 +0000724 addConstantValue(Die, CI->getValue(), Unsigned);
David Blaikiea39a76e2013-01-20 01:18:01 +0000725}
726
727// addConstantValue - Add constant value entry in variable DIE.
Eric Christopher78fcf4902013-07-03 01:08:30 +0000728void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
David Blaikiea39a76e2013-01-20 01:18:01 +0000729 unsigned CIBitWidth = Val.getBitWidth();
Devang Patel8816bbc2011-05-28 00:39:18 +0000730 if (CIBitWidth <= 64) {
Eric Christopher9d1daa82013-08-27 23:49:04 +0000731 // If we're a signed constant definitely use sdata.
732 if (!Unsigned) {
733 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
734 Val.getSExtValue());
735 return;
Devang Patel8816bbc2011-05-28 00:39:18 +0000736 }
Eric Christopher9d1daa82013-08-27 23:49:04 +0000737
738 // Else use data for now unless it's larger than we can deal with.
739 uint16_t Form;
740 switch (CIBitWidth) {
741 case 8:
742 Form = dwarf::DW_FORM_data1;
743 break;
744 case 16:
745 Form = dwarf::DW_FORM_data2;
746 break;
747 case 32:
748 Form = dwarf::DW_FORM_data4;
749 break;
750 case 64:
751 Form = dwarf::DW_FORM_data8;
752 break;
753 default:
754 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
755 Val.getZExtValue());
756 return;
757 }
758 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
Eric Christopher78fcf4902013-07-03 01:08:30 +0000759 return;
Devang Patel0e821f42011-04-12 23:21:44 +0000760 }
761
762 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
763
764 // Get the raw data form of the large APInt.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000765 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel0e821f42011-04-12 23:21:44 +0000766
767 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000768 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel0e821f42011-04-12 23:21:44 +0000769
770 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumi29ccdd82011-10-28 14:12:22 +0000771 for (int i = 0; i < NumBytes; i++) {
772 uint8_t c;
773 if (LittleEndian)
774 c = Ptr64[i / 8] >> (8 * (i & 7));
775 else
776 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
777 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
778 }
Devang Patel0e821f42011-04-12 23:21:44 +0000779
780 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
Devang Patel0e821f42011-04-12 23:21:44 +0000781}
782
Eric Christopher25e35092013-04-22 07:47:40 +0000783/// addTemplateParams - Add template parameters into buffer.
Devang Patel0e821f42011-04-12 23:21:44 +0000784void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
785 // Add template parameters.
786 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
787 DIDescriptor Element = TParams.getElement(i);
788 if (Element.isTemplateTypeParameter())
789 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
790 DITemplateTypeParameter(Element)));
791 else if (Element.isTemplateValueParameter())
792 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
793 DITemplateValueParameter(Element)));
794 }
Devang Patel0e821f42011-04-12 23:21:44 +0000795}
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000796
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000797/// getOrCreateContextDIE - Get context owner's DIE.
798DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
799 if (Context.isType())
800 return getOrCreateTypeDIE(DIType(Context));
801 else if (Context.isNameSpace())
802 return getOrCreateNameSpace(DINameSpace(Context));
803 else if (Context.isSubprogram())
804 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher25e35092013-04-22 07:47:40 +0000805 else
Eric Christopher4d23a4a2013-01-16 01:22:23 +0000806 return getDIE(Context);
807}
808
Devang Patel0e821f42011-04-12 23:21:44 +0000809/// addToContextOwner - Add Die into the list of its context owner's children.
810void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
David Blaikie811bfe62013-10-03 20:07:20 +0000811 assert(!Die->getParent());
812 if (DIE *ContextDIE = getOrCreateContextDIE(Context)) {
813 if (Die->getParent()) {
814 // While creating the context, if this is a type member, we will have
815 // added the child to the context already.
816 assert(Die->getParent() == ContextDIE);
817 return;
818 }
Devang Patel0e821f42011-04-12 23:21:44 +0000819 ContextDIE->addChild(Die);
David Blaikie811bfe62013-10-03 20:07:20 +0000820 } else
Devang Patel0e821f42011-04-12 23:21:44 +0000821 addDie(Die);
822}
823
824/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
825/// given DIType.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000826DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
827 DIType Ty(TyNode);
Manman Ren7504ed42013-07-08 18:33:29 +0000828 if (!Ty.isType())
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000829 return NULL;
Manman Ren8990d7e2013-10-01 19:52:23 +0000830 DIE *TyDIE = DD->getTypeDIE(Ty);
Devang Patel0e821f42011-04-12 23:21:44 +0000831 if (TyDIE)
832 return TyDIE;
833
834 // Create new type.
835 TyDIE = new DIE(dwarf::DW_TAG_base_type);
Manman Ren8990d7e2013-10-01 19:52:23 +0000836 DD->insertTypeDIE(Ty, TyDIE);
Devang Patel0e821f42011-04-12 23:21:44 +0000837 if (Ty.isBasicType())
838 constructTypeDIE(*TyDIE, DIBasicType(Ty));
839 else if (Ty.isCompositeType())
840 constructTypeDIE(*TyDIE, DICompositeType(Ty));
841 else {
842 assert(Ty.isDerivedType() && "Unknown kind of DIType");
843 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
844 }
Eric Christophere2887932011-11-10 19:52:58 +0000845 // If this is a named finished type then include it in the list of types
846 // for the accelerator tables.
Eric Christopher21bde872012-01-06 04:35:23 +0000847 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
848 bool IsImplementation = 0;
849 if (Ty.isCompositeType()) {
850 DICompositeType CT(Ty);
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000851 // A runtime language of 0 actually means C/C++ and that any
852 // non-negative value is some version of Objective-C/C++.
Eric Christopher21bde872012-01-06 04:35:23 +0000853 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christopheref64b462012-02-22 08:46:02 +0000854 CT.isObjcClassComplete();
Eric Christopher21bde872012-01-06 04:35:23 +0000855 }
Eric Christophercf7289f2013-09-05 18:20:16 +0000856 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
Eric Christopher8ea8e4f2012-01-06 23:03:37 +0000857 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopher21bde872012-01-06 04:35:23 +0000858 }
Eric Christopher92331fd2012-11-21 00:34:38 +0000859
Manman Ren116868e2013-09-09 19:47:11 +0000860 addToContextOwner(TyDIE, DD->resolve(Ty.getContext()));
Devang Patel0e821f42011-04-12 23:21:44 +0000861 return TyDIE;
862}
863
864/// addType - Add a new type attribute to the specified entity.
Eric Christopher31b05762013-08-08 01:41:00 +0000865void CompileUnit::addType(DIE *Entity, DIType Ty, uint16_t Attribute) {
Eric Christopher0df08e22013-08-08 07:40:37 +0000866 assert(Ty && "Trying to add a type that doesn't exist?");
Devang Patel0e821f42011-04-12 23:21:44 +0000867
868 // Check for pre-existence.
869 DIEEntry *Entry = getDIEEntry(Ty);
870 // If it exists then use the existing value.
871 if (Entry) {
Manman Ren8990d7e2013-10-01 19:52:23 +0000872 DD->addDIEEntry(Entity, Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel0e821f42011-04-12 23:21:44 +0000873 return;
874 }
875
876 // Construct type.
877 DIE *Buffer = getOrCreateTypeDIE(Ty);
878
879 // Set up proxy.
880 Entry = createDIEEntry(Buffer);
881 insertDIEEntry(Ty, Entry);
Manman Ren8990d7e2013-10-01 19:52:23 +0000882 DD->addDIEEntry(Entity, Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9853f22011-05-31 22:56:51 +0000883
884 // If this is a complete composite type then include it in the
885 // list of global types.
Devang Patel562c7422011-06-01 00:23:24 +0000886 addGlobalType(Ty);
Devang Patel1cb8ab42011-05-31 23:30:30 +0000887}
888
Eric Christopher9cd26af2013-09-20 23:22:52 +0000889// Accelerator table mutators - add each name along with its companion
890// DIE to the proper table while ensuring that the name that we're going
891// to reference is in the string table. We do this since the names we
892// add may not only be identical to the names in the DIE.
893void CompileUnit::addAccelName(StringRef Name, DIE *Die) {
894 DU->getStringPoolEntry(Name);
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000895 std::vector<DIE *> &DIEs = AccelNames[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000896 DIEs.push_back(Die);
897}
898
899void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) {
900 DU->getStringPoolEntry(Name);
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000901 std::vector<DIE *> &DIEs = AccelObjC[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000902 DIEs.push_back(Die);
903}
904
905void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) {
906 DU->getStringPoolEntry(Name);
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000907 std::vector<DIE *> &DIEs = AccelNamespace[Name];
Eric Christopher9cd26af2013-09-20 23:22:52 +0000908 DIEs.push_back(Die);
909}
910
911void CompileUnit::addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die) {
912 DU->getStringPoolEntry(Name);
913 std::vector<std::pair<DIE *, unsigned> > &DIEs = AccelTypes[Name];
914 DIEs.push_back(Die);
915}
916
Eric Christopher9c58f312013-09-20 22:20:55 +0000917/// addGlobalName - Add a new global name to the compile unit.
918void CompileUnit::addGlobalName(StringRef Name, DIE *Die) {
919 GlobalNames[Name] = Die;
920}
921
Devang Patel1cb8ab42011-05-31 23:30:30 +0000922/// addGlobalType - Add a new global type to the compile unit.
923///
Devang Patel562c7422011-06-01 00:23:24 +0000924void CompileUnit::addGlobalType(DIType Ty) {
Manman Ren116868e2013-09-09 19:47:11 +0000925 DIDescriptor Context = DD->resolve(Ty.getContext());
Eric Christopher6d0f1e62013-09-23 23:15:58 +0000926 if (!Ty.getName().empty() && !Ty.isForwardDecl() &&
Eric Christopherb0fc0b92013-09-23 22:59:11 +0000927 (!Context || Context.isCompileUnit() || Context.isFile() ||
928 Context.isNameSpace()))
Devang Patel562c7422011-06-01 00:23:24 +0000929 if (DIEEntry *Entry = getDIEEntry(Ty))
930 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel0e821f42011-04-12 23:21:44 +0000931}
932
Devang Patel17b53272011-05-06 16:57:54 +0000933/// addPubTypes - Add type for pubtypes section.
934void CompileUnit::addPubTypes(DISubprogram SP) {
935 DICompositeType SPTy = SP.getType();
Eric Christopher31b05762013-08-08 01:41:00 +0000936 uint16_t SPTag = SPTy.getTag();
Devang Patel17b53272011-05-06 16:57:54 +0000937 if (SPTag != dwarf::DW_TAG_subroutine_type)
938 return;
939
940 DIArray Args = SPTy.getTypeArray();
941 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
942 DIType ATy(Args.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +0000943 if (!ATy.isType())
Devang Patel17b53272011-05-06 16:57:54 +0000944 continue;
Devang Patel562c7422011-06-01 00:23:24 +0000945 addGlobalType(ATy);
Devang Patel17b53272011-05-06 16:57:54 +0000946 }
947}
948
Devang Patel0e821f42011-04-12 23:21:44 +0000949/// constructTypeDIE - Construct basic type die from DIBasicType.
950void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
951 // Get core information.
952 StringRef Name = BTy.getName();
Devang Patel0e821f42011-04-12 23:21:44 +0000953 // Add name if not anonymous or intermediate type.
954 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000955 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel04d6d472011-09-14 23:13:28 +0000956
957 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
958 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
Eric Christopher93ebdd72013-07-09 23:48:45 +0000959 // An unspecified type only has a name attribute.
Devang Patel04d6d472011-09-14 23:13:28 +0000960 return;
961 }
962
963 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000964 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Pateld925d1a2012-02-07 23:33:58 +0000965 BTy.getEncoding());
Devang Patel04d6d472011-09-14 23:13:28 +0000966
Devang Patel0e821f42011-04-12 23:21:44 +0000967 uint64_t Size = BTy.getSizeInBits() >> 3;
968 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
969}
970
971/// constructTypeDIE - Construct derived type die from DIDerivedType.
972void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
973 // Get core information.
974 StringRef Name = DTy.getName();
975 uint64_t Size = DTy.getSizeInBits() >> 3;
Eric Christopher31b05762013-08-08 01:41:00 +0000976 uint16_t Tag = DTy.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +0000977
978 // FIXME - Workaround for templates.
979 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
980
981 Buffer.setTag(Tag);
982
983 // Map to main type, void will not have a type.
Manman Ren9a0a6702013-10-01 23:45:54 +0000984 DIType FromTy = DD->resolve(DTy.getTypeDerivedFrom());
Eric Christopher0df08e22013-08-08 07:40:37 +0000985 if (FromTy)
986 addType(&Buffer, FromTy);
Devang Patel0e821f42011-04-12 23:21:44 +0000987
988 // Add name if not anonymous or intermediate type.
989 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000990 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +0000991
992 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher85757902012-02-21 22:25:53 +0000993 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel0e821f42011-04-12 23:21:44 +0000994 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
995
David Blaikie5d3249b2013-01-07 05:51:15 +0000996 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
997 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
Manman Ren60352032013-09-05 18:48:31 +0000998 getOrCreateTypeDIE(DD->resolve(DTy.getClassType())));
Devang Patel0e821f42011-04-12 23:21:44 +0000999 // Add source line info if available and TyDesc is not a forward declaration.
1000 if (!DTy.isForwardDecl())
1001 addSourceLine(&Buffer, DTy);
1002}
1003
Eric Christopher67646432013-07-26 17:02:41 +00001004/// Return true if the type is appropriately scoped to be contained inside
1005/// its own type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001006static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
Manman Ren116868e2013-09-09 19:47:11 +00001007 DIScope Parent = DD->resolve(Ty.getContext());
Eric Christopher67646432013-07-26 17:02:41 +00001008 while (Parent) {
1009 // Don't generate a hash for anything scoped inside a function.
1010 if (Parent.isSubprogram())
1011 return false;
Manman Rende897a362013-09-09 22:35:23 +00001012 Parent = DD->resolve(Parent.getContext());
Eric Christopher67646432013-07-26 17:02:41 +00001013 }
1014 return true;
1015}
1016
1017/// Return true if the type should be split out into a type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001018static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
Eric Christopher31b05762013-08-08 01:41:00 +00001019 uint16_t Tag = CTy.getTag();
Eric Christopher67646432013-07-26 17:02:41 +00001020
1021 switch (Tag) {
1022 case dwarf::DW_TAG_structure_type:
1023 case dwarf::DW_TAG_union_type:
1024 case dwarf::DW_TAG_enumeration_type:
1025 case dwarf::DW_TAG_class_type:
1026 // If this is a class, structure, union, or enumeration type
1027 // that is not a declaration, is a type definition, and not scoped
1028 // inside a function then separate this out as a type unit.
Manman Ren33796c52013-09-09 19:23:58 +00001029 if (CTy.isForwardDecl() || !isTypeUnitScoped(CTy, DD))
Eric Christopher67646432013-07-26 17:02:41 +00001030 return 0;
1031 return 1;
1032 default:
1033 return 0;
1034 }
1035}
1036
Devang Patel0e821f42011-04-12 23:21:44 +00001037/// constructTypeDIE - Construct type DIE from DICompositeType.
1038void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1039 // Get core information.
1040 StringRef Name = CTy.getName();
1041
1042 uint64_t Size = CTy.getSizeInBits() >> 3;
Eric Christopher31b05762013-08-08 01:41:00 +00001043 uint16_t Tag = CTy.getTag();
Devang Patel0e821f42011-04-12 23:21:44 +00001044 Buffer.setTag(Tag);
1045
1046 switch (Tag) {
Devang Patel0e821f42011-04-12 23:21:44 +00001047 case dwarf::DW_TAG_array_type:
1048 constructArrayTypeDIE(Buffer, &CTy);
1049 break;
1050 case dwarf::DW_TAG_enumeration_type: {
1051 DIArray Elements = CTy.getTypeArray();
1052
1053 // Add enumerators to enumeration type.
1054 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1055 DIE *ElemDie = NULL;
1056 DIDescriptor Enum(Elements.getElement(i));
1057 if (Enum.isEnumerator()) {
1058 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
1059 Buffer.addChild(ElemDie);
1060 }
1061 }
Manman Ren9a0a6702013-10-01 23:45:54 +00001062 DIType DTy = DD->resolve(CTy.getTypeDerivedFrom());
Eric Christopher49e17b22013-08-08 07:40:42 +00001063 if (DTy) {
Eric Christopherc4964352012-05-23 00:09:20 +00001064 addType(&Buffer, DTy);
1065 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
1066 }
Devang Patel0e821f42011-04-12 23:21:44 +00001067 }
1068 break;
1069 case dwarf::DW_TAG_subroutine_type: {
Eric Christopher0df08e22013-08-08 07:40:37 +00001070 // Add return type. A void return won't have a type.
Devang Patel0e821f42011-04-12 23:21:44 +00001071 DIArray Elements = CTy.getTypeArray();
1072 DIDescriptor RTy = Elements.getElement(0);
Eric Christopher0df08e22013-08-08 07:40:37 +00001073 if (RTy)
1074 addType(&Buffer, DIType(RTy));
Devang Patel0e821f42011-04-12 23:21:44 +00001075
1076 bool isPrototyped = true;
1077 // Add arguments.
1078 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1079 DIDescriptor Ty = Elements.getElement(i);
1080 if (Ty.isUnspecifiedParameter()) {
1081 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
1082 Buffer.addChild(Arg);
1083 isPrototyped = false;
1084 } else {
1085 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1086 addType(Arg, DIType(Ty));
David Blaikie9a7a7a92013-01-29 19:35:24 +00001087 if (DIType(Ty).isArtificial())
1088 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel0e821f42011-04-12 23:21:44 +00001089 Buffer.addChild(Arg);
1090 }
1091 }
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001092 // Add prototype flag if we're dealing with a C language and the
1093 // function has been prototyped.
Eric Christopherbfceb2f2013-08-26 23:50:38 +00001094 uint16_t Language = DICompileUnit(Node).getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001095 if (isPrototyped &&
Eric Christopherd42b92f2012-05-22 18:45:24 +00001096 (Language == dwarf::DW_LANG_C89 ||
1097 Language == dwarf::DW_LANG_C99 ||
1098 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001099 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel0e821f42011-04-12 23:21:44 +00001100 }
1101 break;
1102 case dwarf::DW_TAG_structure_type:
1103 case dwarf::DW_TAG_union_type:
1104 case dwarf::DW_TAG_class_type: {
Devang Patel0e821f42011-04-12 23:21:44 +00001105 // Add elements to structure type.
David Blaikiea1ae0e62013-08-01 20:30:22 +00001106 DIArray Elements = CTy.getTypeArray();
1107 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Devang Patel0e821f42011-04-12 23:21:44 +00001108 DIDescriptor Element = Elements.getElement(i);
1109 DIE *ElemDie = NULL;
1110 if (Element.isSubprogram()) {
1111 DISubprogram SP(Element);
Devang Patel89543712011-08-15 17:24:54 +00001112 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel0e821f42011-04-12 23:21:44 +00001113 if (SP.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001114 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001115 dwarf::DW_ACCESS_protected);
1116 else if (SP.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001117 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001118 dwarf::DW_ACCESS_private);
Eric Christopher92331fd2012-11-21 00:34:38 +00001119 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001120 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001121 dwarf::DW_ACCESS_public);
1122 if (SP.isExplicit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001123 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001124 } else if (Element.isDerivedType()) {
Eric Christopherd42b92f2012-05-22 18:45:24 +00001125 DIDerivedType DDTy(Element);
1126 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1127 ElemDie = new DIE(dwarf::DW_TAG_friend);
Manman Ren9a0a6702013-10-01 23:45:54 +00001128 addType(ElemDie, DD->resolve(DDTy.getTypeDerivedFrom()),
1129 dwarf::DW_AT_friend);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001130 } else if (DDTy.isStaticMember())
1131 ElemDie = createStaticMemberDIE(DDTy);
1132 else
1133 ElemDie = createMemberDIE(DDTy);
David Blaikie811bfe62013-10-03 20:07:20 +00001134 Buffer.addChild(ElemDie);
Eric Christopher7285c7d2012-03-28 07:34:31 +00001135 } else if (Element.isObjCProperty()) {
Devang Pateld925d1a2012-02-07 23:33:58 +00001136 DIObjCProperty Property(Element);
1137 ElemDie = new DIE(Property.getTag());
1138 StringRef PropertyName = Property.getObjCPropertyName();
1139 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopherd42b92f2012-05-22 18:45:24 +00001140 addType(ElemDie, Property.getType());
1141 addSourceLine(ElemDie, Property);
Devang Pateld925d1a2012-02-07 23:33:58 +00001142 StringRef GetterName = Property.getObjCPropertyGetterName();
1143 if (!GetterName.empty())
1144 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1145 StringRef SetterName = Property.getObjCPropertySetterName();
1146 if (!SetterName.empty())
1147 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1148 unsigned PropertyAttributes = 0;
Devang Patel403e8192012-02-04 01:30:32 +00001149 if (Property.isReadOnlyObjCProperty())
1150 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1151 if (Property.isReadWriteObjCProperty())
1152 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1153 if (Property.isAssignObjCProperty())
1154 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1155 if (Property.isRetainObjCProperty())
1156 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1157 if (Property.isCopyObjCProperty())
1158 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1159 if (Property.isNonAtomicObjCProperty())
1160 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1161 if (PropertyAttributes)
Eric Christopher92331fd2012-11-21 00:34:38 +00001162 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel403e8192012-02-04 01:30:32 +00001163 PropertyAttributes);
Devang Patel44882172012-02-06 17:49:43 +00001164
Devang Pateld925d1a2012-02-07 23:33:58 +00001165 DIEEntry *Entry = getDIEEntry(Element);
1166 if (!Entry) {
1167 Entry = createDIEEntry(ElemDie);
1168 insertDIEEntry(Element, Entry);
1169 }
David Blaikie811bfe62013-10-03 20:07:20 +00001170 Buffer.addChild(ElemDie);
Devang Patel403e8192012-02-04 01:30:32 +00001171 } else
Devang Patel0e821f42011-04-12 23:21:44 +00001172 continue;
Devang Patel0e821f42011-04-12 23:21:44 +00001173 }
1174
1175 if (CTy.isAppleBlockExtension())
Eric Christopherbb69a272012-08-24 01:14:27 +00001176 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel0e821f42011-04-12 23:21:44 +00001177
Manman Rend8f798e2013-09-06 18:46:00 +00001178 DICompositeType ContainingType(DD->resolve(CTy.getContainingType()));
Devang Patel0e821f42011-04-12 23:21:44 +00001179 if (DIDescriptor(ContainingType).isCompositeType())
1180 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1181 getOrCreateTypeDIE(DIType(ContainingType)));
Devang Patel0e821f42011-04-12 23:21:44 +00001182
Devang Patel12419ae2011-05-12 21:29:42 +00001183 if (CTy.isObjcClassComplete())
Eric Christopherbb69a272012-08-24 01:14:27 +00001184 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patel2409e782011-05-12 19:06:16 +00001185
Eric Christopherda011dd2011-12-16 23:42:42 +00001186 // Add template parameters to a class, structure or union types.
1187 // FIXME: The support isn't in the metadata for this yet.
1188 if (Tag == dwarf::DW_TAG_class_type ||
1189 Tag == dwarf::DW_TAG_structure_type ||
1190 Tag == dwarf::DW_TAG_union_type)
Devang Patel0e821f42011-04-12 23:21:44 +00001191 addTemplateParams(Buffer, CTy.getTemplateParams());
1192
1193 break;
1194 }
1195 default:
1196 break;
1197 }
1198
1199 // Add name if not anonymous or intermediate type.
1200 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001201 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001202
Eric Christopher775cbd22012-05-22 18:45:18 +00001203 if (Tag == dwarf::DW_TAG_enumeration_type ||
1204 Tag == dwarf::DW_TAG_class_type ||
1205 Tag == dwarf::DW_TAG_structure_type ||
1206 Tag == dwarf::DW_TAG_union_type) {
Devang Patel0e821f42011-04-12 23:21:44 +00001207 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher1cf33382012-06-01 00:22:32 +00001208 // TODO: Do we care about size for enum forward declarations?
Devang Patel0e821f42011-04-12 23:21:44 +00001209 if (Size)
1210 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopher1cf33382012-06-01 00:22:32 +00001211 else if (!CTy.isForwardDecl())
Devang Patel0e821f42011-04-12 23:21:44 +00001212 // Add zero size if it is not a forward declaration.
Eric Christopher1cf33382012-06-01 00:22:32 +00001213 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1214
1215 // If we're a forward decl, say so.
1216 if (CTy.isForwardDecl())
Eric Christopherbb69a272012-08-24 01:14:27 +00001217 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel0e821f42011-04-12 23:21:44 +00001218
1219 // Add source line info if available.
1220 if (!CTy.isForwardDecl())
1221 addSourceLine(&Buffer, CTy);
Eric Christopher54cf8ff2012-03-07 00:15:19 +00001222
1223 // No harm in adding the runtime language to the declaration.
1224 unsigned RLang = CTy.getRunTimeLang();
1225 if (RLang)
1226 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1227 dwarf::DW_FORM_data1, RLang);
Devang Patel0e821f42011-04-12 23:21:44 +00001228 }
Eric Christopher67646432013-07-26 17:02:41 +00001229 // If this is a type applicable to a type unit it then add it to the
1230 // list of types we'll compute a hash for later.
Manman Ren33796c52013-09-09 19:23:58 +00001231 if (shouldCreateTypeUnit(CTy, DD))
Eric Christopher67646432013-07-26 17:02:41 +00001232 DD->addTypeUnitType(&Buffer);
Devang Patel0e821f42011-04-12 23:21:44 +00001233}
1234
Eric Christopher92331fd2012-11-21 00:34:38 +00001235/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel0e821f42011-04-12 23:21:44 +00001236/// for the given DITemplateTypeParameter.
1237DIE *
1238CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1239 DIE *ParamDIE = getDIE(TP);
1240 if (ParamDIE)
1241 return ParamDIE;
1242
1243 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
Eric Christopher056b6472013-08-08 08:09:43 +00001244 // Add the type if it exists, it could be void and therefore no type.
1245 if (TP.getType())
1246 addType(ParamDIE, TP.getType());
David Blaikie2b380232013-06-22 18:59:11 +00001247 if (!TP.getName().empty())
1248 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel0e821f42011-04-12 23:21:44 +00001249 return ParamDIE;
1250}
1251
Eric Christopher92331fd2012-11-21 00:34:38 +00001252/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel0e821f42011-04-12 23:21:44 +00001253/// for the given DITemplateValueParameter.
1254DIE *
Eric Christopherafb2c412013-08-08 07:40:31 +00001255CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter VP) {
1256 DIE *ParamDIE = getDIE(VP);
Devang Patel0e821f42011-04-12 23:21:44 +00001257 if (ParamDIE)
1258 return ParamDIE;
1259
Eric Christopherafb2c412013-08-08 07:40:31 +00001260 ParamDIE = new DIE(VP.getTag());
Eric Christopher0df08e22013-08-08 07:40:37 +00001261
1262 // Add the type if there is one, template template and template parameter
1263 // packs will not have a type.
1264 if (VP.getType())
1265 addType(ParamDIE, VP.getType());
Eric Christopherafb2c412013-08-08 07:40:31 +00001266 if (!VP.getName().empty())
1267 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1268 if (Value *Val = VP.getValue()) {
David Blaikiea1e813d2013-05-10 21:52:07 +00001269 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
Manman Ren9a0a6702013-10-01 23:45:54 +00001270 addConstantValue(ParamDIE, CI, isUnsignedDIType(DD, VP.getType()));
David Blaikiea1e813d2013-05-10 21:52:07 +00001271 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1272 // For declaration non-type template parameters (such as global values and
1273 // functions)
1274 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1275 addOpAddress(Block, Asm->Mang->getSymbol(GV));
1276 // Emit DW_OP_stack_value to use the address as the immediate value of the
1277 // parameter, rather than a pointer to it.
1278 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1279 addBlock(ParamDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopherafb2c412013-08-08 07:40:31 +00001280 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
David Blaikie2b380232013-06-22 18:59:11 +00001281 assert(isa<MDString>(Val));
1282 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1283 cast<MDString>(Val)->getString());
Eric Christopherafb2c412013-08-08 07:40:31 +00001284 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
David Blaikie2b380232013-06-22 18:59:11 +00001285 assert(isa<MDNode>(Val));
1286 DIArray A(cast<MDNode>(Val));
1287 addTemplateParams(*ParamDIE, A);
David Blaikiea1e813d2013-05-10 21:52:07 +00001288 }
1289 }
1290
Devang Patel0e821f42011-04-12 23:21:44 +00001291 return ParamDIE;
1292}
1293
Devang Patel17b53272011-05-06 16:57:54 +00001294/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1295DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1296 DIE *NDie = getDIE(NS);
1297 if (NDie)
1298 return NDie;
1299 NDie = new DIE(dwarf::DW_TAG_namespace);
1300 insertDIE(NS, NDie);
Eric Christopher4996c702011-11-07 09:24:32 +00001301 if (!NS.getName().empty()) {
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001302 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher4996c702011-11-07 09:24:32 +00001303 addAccelNamespace(NS.getName(), NDie);
Eric Christopher55364d72013-09-24 00:17:57 +00001304 addGlobalName(NS.getName(), NDie);
Eric Christopher4996c702011-11-07 09:24:32 +00001305 } else
1306 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel17b53272011-05-06 16:57:54 +00001307 addSourceLine(NDie, NS);
1308 addToContextOwner(NDie, NS.getContext());
1309 return NDie;
1310}
1311
Devang Patel89543712011-08-15 17:24:54 +00001312/// getOrCreateSubprogramDIE - Create new DIE using SP.
1313DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
David Blaikie309ffe42013-10-04 01:39:59 +00001314 // Construct the context before querying for the existence of the DIE in case
1315 // such construction creates the DIE (as is the case for member function
1316 // declarations).
1317 DIE *ContextDIE = getOrCreateContextDIE(SP.getContext());
1318 if (!ContextDIE)
1319 ContextDIE = CUDie.get();
1320
Manman Ren8990d7e2013-10-01 19:52:23 +00001321 DIE *SPDie = DD->getSPDIE(SP);
Devang Patel89543712011-08-15 17:24:54 +00001322 if (SPDie)
1323 return SPDie;
1324
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001325 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1326
1327 // DW_TAG_inlined_subroutine may refer to this DIE.
Manman Ren8990d7e2013-10-01 19:52:23 +00001328 DD->insertSPDIE(SP, SPDie);
Peter Collingbourne4d358b52012-05-27 18:36:44 +00001329
Rafael Espindola79278362011-11-10 22:34:29 +00001330 DISubprogram SPDecl = SP.getFunctionDeclaration();
1331 DIE *DeclDie = NULL;
1332 if (SPDecl.isSubprogram()) {
1333 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1334 }
1335
Devang Patel89543712011-08-15 17:24:54 +00001336 // Add to context owner.
David Blaikie309ffe42013-10-04 01:39:59 +00001337 ContextDIE->addChild(SPDie);
Devang Patel89543712011-08-15 17:24:54 +00001338
1339 // Add function template parameters.
1340 addTemplateParams(*SPDie, SP.getTemplateParams());
1341
Devang Patel89543712011-08-15 17:24:54 +00001342 // If this DIE is going to refer declaration info using AT_specification
Eric Christopherf20ff972013-05-09 00:42:33 +00001343 // then there is no need to add other attributes.
Rafael Espindola79278362011-11-10 22:34:29 +00001344 if (DeclDie) {
1345 // Refer function declaration directly.
1346 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1347 DeclDie);
1348
Devang Patel89543712011-08-15 17:24:54 +00001349 return SPDie;
Rafael Espindola79278362011-11-10 22:34:29 +00001350 }
Devang Patel89543712011-08-15 17:24:54 +00001351
Eric Christopheracb71152012-08-23 22:52:55 +00001352 // Add the linkage name if we have one.
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001353 StringRef LinkageName = SP.getLinkageName();
1354 if (!LinkageName.empty())
Eric Christopheracb71152012-08-23 22:52:55 +00001355 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001356 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopheracb71152012-08-23 22:52:55 +00001357
Devang Patel89543712011-08-15 17:24:54 +00001358 // Constructors and operators for anonymous aggregates do not have names.
1359 if (!SP.getName().empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001360 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Patel89543712011-08-15 17:24:54 +00001361
1362 addSourceLine(SPDie, SP);
1363
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001364 // Add the prototype if we have a prototype and we have a C like
1365 // language.
Eric Christopherbfceb2f2013-08-26 23:50:38 +00001366 uint16_t Language = DICompileUnit(Node).getLanguage();
Eric Christopher5cd2a9d2012-02-22 08:46:21 +00001367 if (SP.isPrototyped() &&
1368 (Language == dwarf::DW_LANG_C89 ||
1369 Language == dwarf::DW_LANG_C99 ||
1370 Language == dwarf::DW_LANG_ObjC))
Eric Christopherbb69a272012-08-24 01:14:27 +00001371 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Patel89543712011-08-15 17:24:54 +00001372
Eric Christopher0df08e22013-08-08 07:40:37 +00001373 // Add Return Type. A void return type will not have a type.
Devang Patel89543712011-08-15 17:24:54 +00001374 DICompositeType SPTy = SP.getType();
David Blaikie5174c842013-05-22 23:22:18 +00001375 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1376 "the type of a subprogram should be a subroutine");
Devang Patel89543712011-08-15 17:24:54 +00001377
David Blaikie5174c842013-05-22 23:22:18 +00001378 DIArray Args = SPTy.getTypeArray();
Eric Christopher0df08e22013-08-08 07:40:37 +00001379 if (Args.getElement(0))
1380 addType(SPDie, DIType(Args.getElement(0)));
Devang Patel89543712011-08-15 17:24:54 +00001381
1382 unsigned VK = SP.getVirtuality();
1383 if (VK) {
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001384 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Patel89543712011-08-15 17:24:54 +00001385 DIEBlock *Block = getDIEBlock();
1386 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1387 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1388 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1389 ContainingTypeMap.insert(std::make_pair(SPDie,
Manman Renc4ae9b32013-09-07 00:04:05 +00001390 DD->resolve(SP.getContainingType())));
Devang Patel89543712011-08-15 17:24:54 +00001391 }
1392
1393 if (!SP.isDefinition()) {
Eric Christopherbb69a272012-08-24 01:14:27 +00001394 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher92331fd2012-11-21 00:34:38 +00001395
Devang Patel89543712011-08-15 17:24:54 +00001396 // Add arguments. Do not add arguments for subprogram definition. They will
1397 // be handled while processing variables.
David Blaikie5174c842013-05-22 23:22:18 +00001398 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1399 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1400 DIType ATy = DIType(Args.getElement(i));
1401 addType(Arg, ATy);
1402 if (ATy.isArtificial())
1403 addFlag(Arg, dwarf::DW_AT_artificial);
1404 SPDie->addChild(Arg);
1405 }
Devang Patel89543712011-08-15 17:24:54 +00001406 }
1407
1408 if (SP.isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001409 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Patel89543712011-08-15 17:24:54 +00001410
1411 if (!SP.isLocalToUnit())
Eric Christopherbb69a272012-08-24 01:14:27 +00001412 addFlag(SPDie, dwarf::DW_AT_external);
Devang Patel89543712011-08-15 17:24:54 +00001413
1414 if (SP.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +00001415 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Patel89543712011-08-15 17:24:54 +00001416
1417 if (unsigned isa = Asm->getISAEncoding()) {
1418 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1419 }
1420
1421 return SPDie;
1422}
1423
Devang Pateldfd6ec32011-08-15 17:57:41 +00001424// Return const expression if value is a GEP to access merged global
1425// constant. e.g.
1426// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1427static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1428 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1429 if (!CE || CE->getNumOperands() != 3 ||
1430 CE->getOpcode() != Instruction::GetElementPtr)
1431 return NULL;
1432
1433 // First operand points to a global struct.
1434 Value *Ptr = CE->getOperand(0);
1435 if (!isa<GlobalValue>(Ptr) ||
1436 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1437 return NULL;
1438
1439 // Second operand is zero.
1440 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1441 if (!CI || !CI->isZero())
1442 return NULL;
1443
1444 // Third operand is offset.
1445 if (!isa<ConstantInt>(CE->getOperand(2)))
1446 return NULL;
1447
1448 return CE;
1449}
1450
1451/// createGlobalVariableDIE - create global variable DIE.
1452void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001453 // Check for pre-existence.
Devang Patela6576a12011-08-18 22:21:50 +00001454 if (getDIE(N))
Devang Pateldfd6ec32011-08-15 17:57:41 +00001455 return;
1456
Devang Patela6576a12011-08-18 22:21:50 +00001457 DIGlobalVariable GV(N);
Manman Ren7504ed42013-07-08 18:33:29 +00001458 if (!GV.isGlobalVariable())
Devang Patel0ecbcbd2011-08-18 23:17:55 +00001459 return;
1460
Devang Pateldfd6ec32011-08-15 17:57:41 +00001461 DIDescriptor GVContext = GV.getContext();
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001462 DIType GTy = GV.getType();
1463
1464 // If this is a static data member definition, some attributes belong
1465 // to the declaration DIE.
1466 DIE *VariableDIE = NULL;
Manman Rene697d3c2013-02-01 23:54:37 +00001467 bool IsStaticMember = false;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001468 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1469 if (SDMDecl.Verify()) {
1470 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1471 // We need the declaration DIE that is in the static member's class.
1472 // But that class might not exist in the DWARF yet.
1473 // Creating the class will create the static member decl DIE.
Manman Ren116868e2013-09-09 19:47:11 +00001474 getOrCreateContextDIE(DD->resolve(SDMDecl.getContext()));
Manman Ren8990d7e2013-10-01 19:52:23 +00001475 VariableDIE = DD->getStaticMemberDIE(SDMDecl);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001476 assert(VariableDIE && "Static member decl has no context?");
Manman Rene697d3c2013-02-01 23:54:37 +00001477 IsStaticMember = true;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001478 }
1479
1480 // If this is not a static data member definition, create the variable
1481 // DIE and add the initial set of attributes to it.
1482 if (!VariableDIE) {
1483 VariableDIE = new DIE(GV.getTag());
1484 // Add to map.
1485 insertDIE(N, VariableDIE);
1486
1487 // Add name and type.
1488 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1489 addType(VariableDIE, GTy);
1490
1491 // Add scoping info.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001492 if (!GV.isLocalToUnit()) {
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001493 addFlag(VariableDIE, dwarf::DW_AT_external);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001494 addGlobalName(GV.getName(), VariableDIE);
1495 }
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001496
1497 // Add line number info.
1498 addSourceLine(VariableDIE, GV);
1499 // Add to context owner.
1500 addToContextOwner(VariableDIE, GVContext);
1501 }
1502
Devang Pateldfd6ec32011-08-15 17:57:41 +00001503 // Add location.
Eric Christopher4996c702011-11-07 09:24:32 +00001504 bool addToAccelTable = false;
Eric Christopher0a917b72011-11-11 03:16:32 +00001505 DIE *VariableSpecDIE = NULL;
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001506 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001507 if (isGlobalVariable) {
Eric Christopher4996c702011-11-07 09:24:32 +00001508 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001509 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
David Blaikief2694972013-06-28 20:05:11 +00001510 const MCSymbol *Sym = Asm->Mang->getSymbol(GV.getGlobal());
1511 if (GV.getGlobal()->isThreadLocal()) {
1512 // FIXME: Make this work with -gsplit-dwarf.
1513 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1514 assert((PointerSize == 4 || PointerSize == 8) &&
1515 "Add support for other sizes if necessary");
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001516 const MCExpr *Expr =
David Blaikie8466ca82013-07-01 23:55:52 +00001517 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
David Blaikief2694972013-06-28 20:05:11 +00001518 // Based on GCC's support for TLS:
David Blaikie8466ca82013-07-01 23:55:52 +00001519 if (!DD->useSplitDwarf()) {
1520 // 1) Start with a constNu of the appropriate pointer size
1521 addUInt(Block, 0, dwarf::DW_FORM_data1,
1522 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1523 // 2) containing the (relocated) address of the TLS variable
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001524 addExpr(Block, 0, dwarf::DW_FORM_udata, Expr);
David Blaikie8466ca82013-07-01 23:55:52 +00001525 } else {
1526 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +00001527 addUInt(Block, 0, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
David Blaikie8466ca82013-07-01 23:55:52 +00001528 }
David Blaikief2694972013-06-28 20:05:11 +00001529 // 3) followed by a custom OP to tell the debugger about TLS (presumably)
1530 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_lo_user);
1531 } else
1532 addOpAddress(Block, Sym);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001533 // Do not create specification DIE if context is either compile unit
1534 // or a subprogram.
Devang Patel5e6b65c2011-09-21 23:41:11 +00001535 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Manman Ren3eb9dff2013-09-09 19:05:21 +00001536 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
Devang Pateldfd6ec32011-08-15 17:57:41 +00001537 // Create specification DIE.
Eric Christopherc12c2112011-11-11 01:55:22 +00001538 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001539 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1540 dwarf::DW_FORM_ref4, VariableDIE);
1541 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001542 // A static member's declaration is already flagged as such.
1543 if (!SDMDecl.Verify())
1544 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Pateldfd6ec32011-08-15 17:57:41 +00001545 addDie(VariableSpecDIE);
1546 } else {
1547 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher4996c702011-11-07 09:24:32 +00001548 }
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001549 // Add the linkage name.
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001550 StringRef LinkageName = GV.getLinkageName();
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001551 if (!LinkageName.empty())
Eric Christopher3f79b8c2013-02-27 23:49:47 +00001552 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1553 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1554 // TAG_variable.
Manman Ren584e4c02013-02-27 23:21:02 +00001555 addString(IsStaticMember && VariableSpecDIE ?
1556 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Benjamin Kramer7c275642013-06-01 17:51:14 +00001557 GlobalValue::getRealLinkageName(LinkageName));
Eric Christopher92331fd2012-11-21 00:34:38 +00001558 } else if (const ConstantInt *CI =
Manman Rene697d3c2013-02-01 23:54:37 +00001559 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl322f41d2013-04-04 22:56:49 +00001560 // AT_const_value was added when the static member was created. To avoid
Manman Rene697d3c2013-02-01 23:54:37 +00001561 // emitting AT_const_value multiple times, we only add AT_const_value when
1562 // it is not a static member.
1563 if (!IsStaticMember)
Manman Ren9a0a6702013-10-01 23:45:54 +00001564 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
Manman Rene697d3c2013-02-01 23:54:37 +00001565 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher4996c702011-11-07 09:24:32 +00001566 addToAccelTable = true;
Devang Pateldfd6ec32011-08-15 17:57:41 +00001567 // GV is a merged global.
1568 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1569 Value *Ptr = CE->getOperand(0);
Eric Christophere9ec2452013-01-18 22:11:33 +00001570 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Pateldfd6ec32011-08-15 17:57:41 +00001571 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1572 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher92331fd2012-11-21 00:34:38 +00001573 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001574 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Pateldfd6ec32011-08-15 17:57:41 +00001575 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1576 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1577 }
1578
Eric Christopherc12c2112011-11-11 01:55:22 +00001579 if (addToAccelTable) {
1580 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1581 addAccelName(GV.getName(), AddrDIE);
Eric Christopher4996c702011-11-07 09:24:32 +00001582
Eric Christopherc12c2112011-11-11 01:55:22 +00001583 // If the linkage name is different than the name, go ahead and output
1584 // that as well into the name table.
1585 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1586 addAccelName(GV.getLinkageName(), AddrDIE);
1587 }
Devang Pateldfd6ec32011-08-15 17:57:41 +00001588}
1589
Devang Patel0e821f42011-04-12 23:21:44 +00001590/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopherd42b92f2012-05-22 18:45:24 +00001591void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1592 DIE *IndexTy) {
Devang Patel0e821f42011-04-12 23:21:44 +00001593 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1594 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel0e821f42011-04-12 23:21:44 +00001595
Bill Wendling28fe9e72012-12-06 07:38:10 +00001596 // The LowerBound value defines the lower bounds which is typically zero for
1597 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1598 // Count == -1 then the array is unbounded and we do not emit
1599 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1600 // Count == 0, then the array has zero elements in which case we do not emit
1601 // an upper bound.
1602 int64_t LowerBound = SR.getLo();
Bill Wendling3495f9b2012-12-06 07:55:19 +00001603 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendlingd7767122012-12-04 21:34:03 +00001604 int64_t Count = SR.getCount();
Devang Patel0e821f42011-04-12 23:21:44 +00001605
Bill Wendling3495f9b2012-12-06 07:55:19 +00001606 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling28fe9e72012-12-06 07:38:10 +00001607 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1608
1609 if (Count != -1 && Count != 0)
Bill Wendlingd7767122012-12-04 21:34:03 +00001610 // FIXME: An unbounded array should reference the expression that defines
1611 // the array.
Bill Wendling28fe9e72012-12-06 07:38:10 +00001612 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendlingd7767122012-12-04 21:34:03 +00001613
Devang Patel0e821f42011-04-12 23:21:44 +00001614 Buffer.addChild(DW_Subrange);
1615}
1616
1617/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1618void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1619 DICompositeType *CTy) {
1620 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher72a52952013-01-08 01:53:52 +00001621 if (CTy->isVector())
Eric Christopherbb69a272012-08-24 01:14:27 +00001622 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel0e821f42011-04-12 23:21:44 +00001623
Eric Christopher0df08e22013-08-08 07:40:37 +00001624 // Emit the element type.
Manman Ren9a0a6702013-10-01 23:45:54 +00001625 addType(&Buffer, DD->resolve(CTy->getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001626
1627 // Get an anonymous type for index type.
Eric Christophercad9b532013-01-04 21:51:53 +00001628 // FIXME: This type should be passed down from the front end
1629 // as different languages may have different sizes for indexes.
Devang Patel0e821f42011-04-12 23:21:44 +00001630 DIE *IdxTy = getIndexTyDie();
1631 if (!IdxTy) {
1632 // Construct an anonymous type for index type.
1633 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christophercad9b532013-01-04 21:51:53 +00001634 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel0e821f42011-04-12 23:21:44 +00001635 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1636 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1637 dwarf::DW_ATE_signed);
1638 addDie(IdxTy);
1639 setIndexTyDie(IdxTy);
1640 }
1641
1642 // Add subranges to array type.
Eric Christopher0df08e22013-08-08 07:40:37 +00001643 DIArray Elements = CTy->getTypeArray();
Devang Patel0e821f42011-04-12 23:21:44 +00001644 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1645 DIDescriptor Element = Elements.getElement(i);
1646 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1647 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1648 }
1649}
1650
1651/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1652DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1653 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1654 StringRef Name = ETy.getName();
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001655 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001656 int64_t Value = ETy.getEnumValue();
1657 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1658 return Enumerator;
1659}
1660
Devang Patel89543712011-08-15 17:24:54 +00001661/// constructContainingTypeDIEs - Construct DIEs for types that contain
1662/// vtables.
1663void CompileUnit::constructContainingTypeDIEs() {
1664 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1665 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1666 DIE *SPDie = CI->first;
1667 const MDNode *N = CI->second;
1668 if (!N) continue;
Manman Ren8990d7e2013-10-01 19:52:23 +00001669 DIE *NDie = DD->getTypeDIE(N);
Devang Patel89543712011-08-15 17:24:54 +00001670 if (!NDie) continue;
1671 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1672 }
1673}
1674
Devang Patel3acc70e2011-08-15 22:04:40 +00001675/// constructVariableDIE - Construct a DIE for the given DbgVariable.
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001676DIE *CompileUnit::constructVariableDIE(DbgVariable *DV,
1677 bool isScopeAbstract) {
Devang Patel3acc70e2011-08-15 22:04:40 +00001678 StringRef Name = DV->getName();
Devang Patel3acc70e2011-08-15 22:04:40 +00001679
1680 // Translate tag to proper Dwarf tag.
Eric Christopher31b05762013-08-08 01:41:00 +00001681 uint16_t Tag = DV->getTag();
Devang Patel3acc70e2011-08-15 22:04:40 +00001682
1683 // Define variable debug information entry.
1684 DIE *VariableDie = new DIE(Tag);
1685 DbgVariable *AbsVar = DV->getAbstractVariable();
1686 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
Manman Ren4213c392013-05-29 17:16:59 +00001687 if (AbsDIE)
Devang Patel3acc70e2011-08-15 22:04:40 +00001688 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
Manman Ren4213c392013-05-29 17:16:59 +00001689 dwarf::DW_FORM_ref4, AbsDIE);
Devang Patel3acc70e2011-08-15 22:04:40 +00001690 else {
David Blaikie715528b2013-08-19 03:34:03 +00001691 if (!Name.empty())
1692 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Patel3acc70e2011-08-15 22:04:40 +00001693 addSourceLine(VariableDie, DV->getVariable());
1694 addType(VariableDie, DV->getType());
1695 }
1696
1697 if (DV->isArtificial())
Eric Christopherbb69a272012-08-24 01:14:27 +00001698 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Patel3acc70e2011-08-15 22:04:40 +00001699
1700 if (isScopeAbstract) {
1701 DV->setDIE(VariableDie);
1702 return VariableDie;
1703 }
1704
1705 // Add variable address.
1706
1707 unsigned Offset = DV->getDotDebugLocOffset();
1708 if (Offset != ~0U) {
Eric Christopher40b6bf62013-06-24 21:34:55 +00001709 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1710 Asm->GetTempSymbol("debug_loc", Offset));
Devang Patel3acc70e2011-08-15 22:04:40 +00001711 DV->setDIE(VariableDie);
1712 return VariableDie;
1713 }
1714
Eric Christophercead0332011-10-03 15:49:20 +00001715 // Check if variable is described by a DBG_VALUE instruction.
Devang Patel3acc70e2011-08-15 22:04:40 +00001716 if (const MachineInstr *DVInsn = DV->getMInsn()) {
David Blaikie0252265b2013-06-16 20:34:15 +00001717 assert(DVInsn->getNumOperands() == 3);
1718 if (DVInsn->getOperand(0).isReg()) {
1719 const MachineOperand RegOp = DVInsn->getOperand(0);
Adrian Prantl19942882013-07-09 21:44:06 +00001720 // If the second operand is an immediate, this is an indirect value.
Adrian Prantl418d1d12013-07-09 20:28:37 +00001721 if (DVInsn->getOperand(1).isImm()) {
1722 MachineLocation Location(RegOp.getReg(), DVInsn->getOperand(1).getImm());
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001723 addVariableAddress(*DV, VariableDie, Location);
David Blaikie0252265b2013-06-16 20:34:15 +00001724 } else if (RegOp.getReg())
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001725 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg()));
David Blaikie0252265b2013-06-16 20:34:15 +00001726 } else if (DVInsn->getOperand(0).isImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001727 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
David Blaikie0252265b2013-06-16 20:34:15 +00001728 else if (DVInsn->getOperand(0).isFPImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001729 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
David Blaikie0252265b2013-06-16 20:34:15 +00001730 else if (DVInsn->getOperand(0).isCImm())
Eric Christopher78fcf4902013-07-03 01:08:30 +00001731 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
Manman Ren9a0a6702013-10-01 23:45:54 +00001732 isUnsignedDIType(DD, DV->getType()));
Eric Christopher78fcf4902013-07-03 01:08:30 +00001733
Devang Patel3acc70e2011-08-15 22:04:40 +00001734 DV->setDIE(VariableDie);
1735 return VariableDie;
1736 } else {
1737 // .. else use frame index.
1738 int FI = DV->getFrameIndex();
1739 if (FI != ~0) {
1740 unsigned FrameReg = 0;
1741 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher92331fd2012-11-21 00:34:38 +00001742 int Offset =
Devang Patel3acc70e2011-08-15 22:04:40 +00001743 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1744 MachineLocation Location(FrameReg, Offset);
Eric Christopherbf2d23c2013-06-24 21:07:27 +00001745 addVariableAddress(*DV, VariableDie, Location);
Devang Patel3acc70e2011-08-15 22:04:40 +00001746 }
1747 }
1748
1749 DV->setDIE(VariableDie);
1750 return VariableDie;
1751}
1752
Devang Patel0e821f42011-04-12 23:21:44 +00001753/// createMemberDIE - Create new member DIE.
1754DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1755 DIE *MemberDie = new DIE(DT.getTag());
1756 StringRef Name = DT.getName();
1757 if (!Name.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +00001758 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel0e821f42011-04-12 23:21:44 +00001759
Manman Ren9a0a6702013-10-01 23:45:54 +00001760 addType(MemberDie, DD->resolve(DT.getTypeDerivedFrom()));
Devang Patel0e821f42011-04-12 23:21:44 +00001761
1762 addSourceLine(MemberDie, DT);
1763
1764 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1765 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1766
1767 uint64_t Size = DT.getSizeInBits();
Manman Ren9a0a6702013-10-01 23:45:54 +00001768 uint64_t FieldSize = getOriginalTypeSize(DD, DT);
Devang Patel0e821f42011-04-12 23:21:44 +00001769
1770 if (Size != FieldSize) {
1771 // Handle bitfield.
Manman Ren9a0a6702013-10-01 23:45:54 +00001772 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0,
1773 getOriginalTypeSize(DD, DT)>>3);
Devang Patel0e821f42011-04-12 23:21:44 +00001774 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1775
1776 uint64_t Offset = DT.getOffsetInBits();
1777 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1778 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1779 uint64_t FieldOffset = (HiMark - FieldSize);
1780 Offset -= FieldOffset;
1781
1782 // Maybe we need to work from the other end.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001783 if (Asm->getDataLayout().isLittleEndian())
Devang Patel0e821f42011-04-12 23:21:44 +00001784 Offset = FieldSize - (Offset + Size);
1785 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1786
1787 // Here WD_AT_data_member_location points to the anonymous
1788 // field that includes this bit field.
1789 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1790
1791 } else
1792 // This is not a bitfield.
1793 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1794
1795 if (DT.getTag() == dwarf::DW_TAG_inheritance
1796 && DT.isVirtual()) {
1797
1798 // For C++, virtual base classes are not at fixed offset. Use following
1799 // expression to extract appropriate offset from vtable.
1800 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1801
1802 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1803 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1804 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1805 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1806 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1807 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1808 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1809 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1810
1811 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1812 VBaseLocationDie);
1813 } else
1814 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1815
1816 if (DT.isProtected())
Nick Lewyckycb918492011-12-13 05:09:11 +00001817 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001818 dwarf::DW_ACCESS_protected);
1819 else if (DT.isPrivate())
Nick Lewyckycb918492011-12-13 05:09:11 +00001820 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001821 dwarf::DW_ACCESS_private);
1822 // Otherwise C++ member and base classes are considered public.
Eric Christopher92331fd2012-11-21 00:34:38 +00001823 else
Nick Lewyckycb918492011-12-13 05:09:11 +00001824 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001825 dwarf::DW_ACCESS_public);
1826 if (DT.isVirtual())
Nick Lewyckycfde1a22011-12-14 00:56:07 +00001827 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel0e821f42011-04-12 23:21:44 +00001828 dwarf::DW_VIRTUALITY_virtual);
Devang Patel514b4002011-04-16 00:11:51 +00001829
1830 // Objective-C properties.
Devang Patel44882172012-02-06 17:49:43 +00001831 if (MDNode *PNode = DT.getObjCProperty())
1832 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher92331fd2012-11-21 00:34:38 +00001833 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel44882172012-02-06 17:49:43 +00001834 PropertyDie);
1835
David Blaikie37fefc32012-12-13 22:43:07 +00001836 if (DT.isArtificial())
1837 addFlag(MemberDie, dwarf::DW_AT_artificial);
1838
Devang Patel0e821f42011-04-12 23:21:44 +00001839 return MemberDie;
1840}
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001841
1842/// createStaticMemberDIE - Create new DIE for C++ static member.
1843DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1844 if (!DT.Verify())
1845 return NULL;
1846
1847 DIE *StaticMemberDIE = new DIE(DT.getTag());
Manman Ren9a0a6702013-10-01 23:45:54 +00001848 DIType Ty = DD->resolve(DT.getTypeDerivedFrom());
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001849
1850 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1851 addType(StaticMemberDIE, Ty);
1852 addSourceLine(StaticMemberDIE, DT);
1853 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1854 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1855
1856 // FIXME: We could omit private if the parent is a class_type, and
1857 // public if the parent is something else.
1858 if (DT.isProtected())
1859 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1860 dwarf::DW_ACCESS_protected);
1861 else if (DT.isPrivate())
1862 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1863 dwarf::DW_ACCESS_private);
1864 else
1865 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1866 dwarf::DW_ACCESS_public);
1867
1868 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
Manman Ren9a0a6702013-10-01 23:45:54 +00001869 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
David Blaikiea39a76e2013-01-20 01:18:01 +00001870 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1871 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001872
Manman Ren8990d7e2013-10-01 19:52:23 +00001873 DD->insertStaticMemberDIE(DT, StaticMemberDIE);
Eric Christopher4d23a4a2013-01-16 01:22:23 +00001874 return StaticMemberDIE;
1875}