blob: f0655e678473a2b260f14c09234dd8865ef49017 [file] [log] [blame]
Devang Patel161b2f42011-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 Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth0b8c9a82013-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 Christopherd61c34b2011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031
32using namespace llvm;
33
34/// CompileUnit - Compile unit constructor.
Eli Benderskyd4a05e02012-12-03 18:45:45 +000035CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, AsmPrinter *A,
Eric Christopher2e5d8702012-12-20 21:58:36 +000036 DwarfDebug *DW, DwarfUnits *DWU)
37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
38 IndexTyDie(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000039 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
40}
41
42/// ~CompileUnit - Destructor for compile unit.
43CompileUnit::~CompileUnit() {
44 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
45 DIEBlocks[j]->~DIEBlock();
46}
47
48/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
49/// information entry.
50DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
51 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
52 return Value;
53}
54
Bill Wendling6afe4782012-12-06 07:55:19 +000055/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000056/// DWARF version doesn't handle the language, return -1.
Bill Wendling6afe4782012-12-06 07:55:19 +000057int64_t CompileUnit::getDefaultLowerBound() const {
Bill Wendling222c2fd2012-12-06 07:38:10 +000058 switch (Language) {
59 default:
60 break;
61
62 case dwarf::DW_LANG_C89:
63 case dwarf::DW_LANG_C99:
64 case dwarf::DW_LANG_C:
65 case dwarf::DW_LANG_C_plus_plus:
66 case dwarf::DW_LANG_ObjC:
67 case dwarf::DW_LANG_ObjC_plus_plus:
68 return 0;
69
70 case dwarf::DW_LANG_Fortran77:
71 case dwarf::DW_LANG_Fortran90:
72 case dwarf::DW_LANG_Fortran95:
73 return 1;
74
75 // The languages below have valid values only if the DWARF version >= 4.
76 case dwarf::DW_LANG_Java:
77 case dwarf::DW_LANG_Python:
78 case dwarf::DW_LANG_UPC:
79 case dwarf::DW_LANG_D:
80 if (dwarf::DWARF_VERSION >= 4)
81 return 0;
82 break;
83
84 case dwarf::DW_LANG_Ada83:
85 case dwarf::DW_LANG_Ada95:
86 case dwarf::DW_LANG_Cobol74:
87 case dwarf::DW_LANG_Cobol85:
88 case dwarf::DW_LANG_Modula2:
89 case dwarf::DW_LANG_Pascal83:
90 case dwarf::DW_LANG_PLI:
91 if (dwarf::DWARF_VERSION >= 4)
92 return 1;
93 break;
94 }
95
96 return -1;
97}
98
Eric Christopher873cf0a2012-08-24 01:14:27 +000099/// addFlag - Add a flag that is true.
100void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
101 if (!DD->useDarwinGDBCompat())
102 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
103 DIEIntegerOne);
104 else
105 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
106}
107
Devang Patel161b2f42011-04-12 23:21:44 +0000108/// addUInt - Add an unsigned integer attribute data and value.
109///
110void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
111 unsigned Form, uint64_t Integer) {
112 if (!Form) Form = DIEInteger::BestForm(false, Integer);
113 DIEValue *Value = Integer == 1 ?
114 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
115 Die->addValue(Attribute, Form, Value);
116}
117
118/// addSInt - Add an signed integer attribute data and value.
119///
120void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
121 unsigned Form, int64_t Integer) {
122 if (!Form) Form = DIEInteger::BestForm(true, Integer);
123 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
124 Die->addValue(Attribute, Form, Value);
125}
126
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000127/// addString - Add a string attribute data and value. We always emit a
128/// reference to the string pool instead of immediate strings so that DIEs have
Eric Christopher3cc42202013-01-07 19:32:45 +0000129/// more predictable sizes. In the case of split dwarf we emit an index
130/// into another table which gets us the static offset into the string
131/// table.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000132void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000133 if (!DD->useSplitDwarf()) {
134 MCSymbol *Symb = DU->getStringPoolEntry(String);
135 DIEValue *Value;
136 if (Asm->needsRelocationsForDwarfStringPool())
137 Value = new (DIEValueAllocator) DIELabel(Symb);
138 else {
139 MCSymbol *StringPool = DU->getStringPoolSym();
140 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
141 }
142 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
143 } else {
144 unsigned idx = DU->getStringPoolIndex(String);
145 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
146 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value);
147 }
148}
149
150/// addLocalString - Add a string attribute data and value. This is guaranteed
151/// to be in the local string pool instead of indirected.
152void CompileUnit::addLocalString(DIE *Die, unsigned Attribute,
153 StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000154 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000155 DIEValue *Value;
156 if (Asm->needsRelocationsForDwarfStringPool())
157 Value = new (DIEValueAllocator) DIELabel(Symb);
158 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000159 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000160 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000161 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000162 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000163}
164
165/// addLabel - Add a Dwarf label attribute data and value.
166///
167void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
168 const MCSymbol *Label) {
169 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
170 Die->addValue(Attribute, Form, Value);
171}
172
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000173/// addLabelAddress - Add a dwarf label attribute data and value using
174/// DW_FORM_addr or DW_FORM_GNU_addr_index.
175///
176void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute,
177 MCSymbol *Label) {
178 if (!DD->useSplitDwarf()) {
179 if (Label != NULL) {
180 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
181 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
182 } else {
183 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
184 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
185 }
186 } else {
187 unsigned idx = DU->getAddrPoolIndex(Label);
188 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
189 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
190 }
191}
192
Eric Christopher0969ddf2013-01-18 22:11:33 +0000193/// addOpAddress - Add a dwarf op address data and value using the
194/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
195///
196void CompileUnit::addOpAddress(DIE *Die, MCSymbol *Sym) {
197
198 if (!DD->useSplitDwarf()) {
199 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
200 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
201 } else {
202 unsigned idx = DU->getAddrPoolIndex(Sym);
203 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
204 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
205 Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value);
206 }
207}
208
Devang Patel161b2f42011-04-12 23:21:44 +0000209/// addDelta - Add a label delta attribute data and value.
210///
211void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
212 const MCSymbol *Hi, const MCSymbol *Lo) {
213 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
214 Die->addValue(Attribute, Form, Value);
215}
216
217/// addDIEEntry - Add a DIE attribute data and value.
218///
219void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
220 DIE *Entry) {
221 Die->addValue(Attribute, Form, createDIEEntry(Entry));
222}
223
Devang Patel161b2f42011-04-12 23:21:44 +0000224/// addBlock - Add block data.
225///
226void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
227 DIEBlock *Block) {
228 Block->ComputeSize(Asm);
229 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
230 Die->addValue(Attribute, Block->BestForm(), Block);
231}
232
233/// addSourceLine - Add location information to specified debug information
234/// entry.
235void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
236 // Verify variable.
237 if (!V.Verify())
238 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000239
Devang Patel161b2f42011-04-12 23:21:44 +0000240 unsigned Line = V.getLineNumber();
241 if (Line == 0)
242 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000243 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Devang Patel161b2f42011-04-12 23:21:44 +0000244 V.getContext().getDirectory());
245 assert(FileID && "Invalid file id");
246 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
247 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
248}
249
250/// addSourceLine - Add location information to specified debug information
251/// entry.
252void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
253 // Verify global variable.
254 if (!G.Verify())
255 return;
256
257 unsigned Line = G.getLineNumber();
258 if (Line == 0)
259 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000260 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000261 assert(FileID && "Invalid file id");
262 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
263 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
264}
265
266/// addSourceLine - Add location information to specified debug information
267/// entry.
268void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
269 // Verify subprogram.
270 if (!SP.Verify())
271 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000272
Devang Patel161b2f42011-04-12 23:21:44 +0000273 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000274 unsigned Line = SP.getLineNumber();
275 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000276 return;
277
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000278 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000279 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000280 assert(FileID && "Invalid file id");
281 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
282 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
283}
284
285/// addSourceLine - Add location information to specified debug information
286/// entry.
287void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
288 // Verify type.
289 if (!Ty.Verify())
290 return;
291
292 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000293 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000294 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000295 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000296 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000297 assert(FileID && "Invalid file id");
298 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
299 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
300}
301
302/// addSourceLine - Add location information to specified debug information
303/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000304void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
305 // Verify type.
306 if (!Ty.Verify())
307 return;
308
309 unsigned Line = Ty.getLineNumber();
310 if (Line == 0)
311 return;
312 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000313 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Eric Christopher4d069bf2012-05-22 18:45:24 +0000314 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000315 assert(FileID && "Invalid file id");
316 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
317 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
318}
319
320/// addSourceLine - Add location information to specified debug information
321/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000322void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
323 // Verify namespace.
324 if (!NS.Verify())
325 return;
326
327 unsigned Line = NS.getLineNumber();
328 if (Line == 0)
329 return;
330 StringRef FN = NS.getFilename();
331
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000332 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000333 assert(FileID && "Invalid file id");
334 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
335 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
336}
337
Eric Christopher8b4310b2012-11-21 00:34:38 +0000338/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000339/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000340void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000341 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000342 if (DV->variableHasComplexAddress())
343 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
344 else if (DV->isBlockByrefVariable())
345 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
346 else
347 addAddress(Die, dwarf::DW_AT_location, Location);
348}
349
Devang Patel116da2f2011-04-26 19:06:18 +0000350/// addRegisterOp - Add register operand.
351void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
352 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
353 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
354 if (DWReg < 32)
355 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
356 else {
357 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
358 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
359 }
360}
361
362/// addRegisterOffset - Add register offset.
363void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
364 int64_t Offset) {
365 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
366 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
367 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
368 if (Reg == TRI->getFrameRegister(*Asm->MF))
369 // If variable offset is based in frame register then use fbreg.
370 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
371 else if (DWReg < 32)
372 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
373 else {
374 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
375 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
376 }
377 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
378}
379
380/// addAddress - Add an address attribute to a die based on the location
381/// provided.
382void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
383 const MachineLocation &Location) {
384 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
385
386 if (Location.isReg())
387 addRegisterOp(Block, Location.getReg());
388 else
389 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
390
391 // Now attach the location information to the DIE.
392 addBlock(Die, Attribute, 0, Block);
393}
394
Devang Patel161b2f42011-04-12 23:21:44 +0000395/// addComplexAddress - Start with the address based on the location provided,
396/// and generate the DWARF information necessary to find the actual variable
397/// given the extra address information encoded in the DIVariable, starting from
398/// the starting location. Add the DWARF information to the die.
399///
400void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
401 unsigned Attribute,
402 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000403 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000404 unsigned N = DV->getNumAddrElements();
405 unsigned i = 0;
406 if (Location.isReg()) {
407 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
408 // If first address element is OpPlus then emit
409 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
410 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
411 i = 2;
412 } else
413 addRegisterOp(Block, Location.getReg());
414 }
Devang Patel116da2f2011-04-26 19:06:18 +0000415 else
416 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000417
Devang Patelc26f5442011-04-28 02:22:40 +0000418 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000419 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000420 if (Element == DIBuilder::OpPlus) {
421 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
422 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
423 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000424 if (!Location.isReg())
425 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000426 } else llvm_unreachable("unknown DIBuilder Opcode");
427 }
428
429 // Now attach the location information to the DIE.
430 addBlock(Die, Attribute, 0, Block);
431}
432
433/* Byref variables, in Blocks, are declared by the programmer as "SomeType
434 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
435 gives the variable VarName either the struct, or a pointer to the struct, as
436 its type. This is necessary for various behind-the-scenes things the
437 compiler needs to do with by-reference variables in Blocks.
438
439 However, as far as the original *programmer* is concerned, the variable
440 should still have type 'SomeType', as originally declared.
441
442 The function getBlockByrefType dives into the __Block_byref_x_VarName
443 struct to find the original type of the variable, which is then assigned to
444 the variable's Debug Information Entry as its real type. So far, so good.
445 However now the debugger will expect the variable VarName to have the type
446 SomeType. So we need the location attribute for the variable to be an
447 expression that explains to the debugger how to navigate through the
448 pointers and struct to find the actual variable of type SomeType.
449
450 The following function does just that. We start by getting
451 the "normal" location for the variable. This will be the location
452 of either the struct __Block_byref_x_VarName or the pointer to the
453 struct __Block_byref_x_VarName.
454
455 The struct will look something like:
456
457 struct __Block_byref_x_VarName {
458 ... <various fields>
459 struct __Block_byref_x_VarName *forwarding;
460 ... <various other fields>
461 SomeType VarName;
462 ... <maybe more fields>
463 };
464
465 If we are given the struct directly (as our starting point) we
466 need to tell the debugger to:
467
468 1). Add the offset of the forwarding field.
469
470 2). Follow that pointer to get the real __Block_byref_x_VarName
471 struct to use (the real one may have been copied onto the heap).
472
473 3). Add the offset for the field VarName, to find the actual variable.
474
475 If we started with a pointer to the struct, then we need to
476 dereference that pointer first, before the other steps.
477 Translating this into DWARF ops, we will need to append the following
478 to the current location description for the variable:
479
480 DW_OP_deref -- optional, if we start with a pointer
481 DW_OP_plus_uconst <forward_fld_offset>
482 DW_OP_deref
483 DW_OP_plus_uconst <varName_fld_offset>
484
485 That is what this function does. */
486
487/// addBlockByrefAddress - Start with the address based on the location
488/// provided, and generate the DWARF information necessary to find the
489/// actual Block variable (navigating the Block struct) based on the
490/// starting location. Add the DWARF information to the die. For
491/// more information, read large comment just above here.
492///
493void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
494 unsigned Attribute,
495 const MachineLocation &Location) {
496 DIType Ty = DV->getType();
497 DIType TmpTy = Ty;
498 unsigned Tag = Ty.getTag();
499 bool isPointer = false;
500
501 StringRef varName = DV->getName();
502
503 if (Tag == dwarf::DW_TAG_pointer_type) {
504 DIDerivedType DTy = DIDerivedType(Ty);
505 TmpTy = DTy.getTypeDerivedFrom();
506 isPointer = true;
507 }
508
509 DICompositeType blockStruct = DICompositeType(TmpTy);
510
511 // Find the __forwarding field and the variable field in the __Block_byref
512 // struct.
513 DIArray Fields = blockStruct.getTypeArray();
514 DIDescriptor varField = DIDescriptor();
515 DIDescriptor forwardingField = DIDescriptor();
516
517 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
518 DIDescriptor Element = Fields.getElement(i);
519 DIDerivedType DT = DIDerivedType(Element);
520 StringRef fieldName = DT.getName();
521 if (fieldName == "__forwarding")
522 forwardingField = Element;
523 else if (fieldName == varName)
524 varField = Element;
525 }
526
527 // Get the offsets for the forwarding field and the variable field.
528 unsigned forwardingFieldOffset =
529 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
530 unsigned varFieldOffset =
531 DIDerivedType(varField).getOffsetInBits() >> 3;
532
533 // Decode the original location, and use that as the start of the byref
534 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000535 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
536
Eric Christophercaba2632012-07-04 02:02:18 +0000537 if (Location.isReg())
538 addRegisterOp(Block, Location.getReg());
539 else
540 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000541
542 // If we started with a pointer to the __Block_byref... struct, then
543 // the first thing we need to do is dereference the pointer (DW_OP_deref).
544 if (isPointer)
545 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
546
547 // Next add the offset for the '__forwarding' field:
548 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
549 // adding the offset if it's 0.
550 if (forwardingFieldOffset > 0) {
551 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
552 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
553 }
554
555 // Now dereference the __forwarding field to get to the real __Block_byref
556 // struct: DW_OP_deref.
557 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
558
559 // Now that we've got the real __Block_byref... struct, add the offset
560 // for the variable's field to get to the location of the actual variable:
561 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
562 if (varFieldOffset > 0) {
563 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
564 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
565 }
566
567 // Now attach the location information to the DIE.
568 addBlock(Die, Attribute, 0, Block);
569}
570
Devang Patel4ec14b02011-07-20 21:57:04 +0000571/// isTypeSigned - Return true if the type is signed.
572static bool isTypeSigned(DIType Ty, int *SizeInBits) {
573 if (Ty.isDerivedType())
574 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
575 if (Ty.isBasicType())
576 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
577 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
578 *SizeInBits = Ty.getSizeInBits();
579 return true;
580 }
581 return false;
582}
583
Devang Patel161b2f42011-04-12 23:21:44 +0000584/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000585bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
586 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000587 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000588 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000589 int SizeInBits = -1;
590 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
591 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
592 switch (SizeInBits) {
593 case 8: Form = dwarf::DW_FORM_data1; break;
594 case 16: Form = dwarf::DW_FORM_data2; break;
595 case 32: Form = dwarf::DW_FORM_data4; break;
596 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000597 default: break;
598 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000599 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000600 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000601
Devang Patel161b2f42011-04-12 23:21:44 +0000602 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
603 return true;
604}
605
606/// addConstantFPValue - Add constant value entry in variable DIE.
607bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000608 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000609 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
610 APFloat FPImm = MO.getFPImm()->getValueAPF();
611
612 // Get the raw data form of the floating point.
613 const APInt FltVal = FPImm.bitcastToAPInt();
614 const char *FltPtr = (const char*)FltVal.getRawData();
615
616 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000617 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000618 int Incr = (LittleEndian ? 1 : -1);
619 int Start = (LittleEndian ? 0 : NumBytes - 1);
620 int Stop = (LittleEndian ? NumBytes : -1);
621
622 // Output the constant to DWARF one byte at a time.
623 for (; Start != Stop; Start += Incr)
624 addUInt(Block, 0, dwarf::DW_FORM_data1,
625 (unsigned char)0xFF & FltPtr[Start]);
626
627 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
628 return true;
629}
630
David Blaikie14268412013-01-20 01:18:01 +0000631/// addConstantFPValue - Add constant value entry in variable DIE.
632bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
633 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
634}
635
Devang Patel161b2f42011-04-12 23:21:44 +0000636/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000637bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000638 bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000639 return addConstantValue(Die, CI->getValue(), Unsigned);
640}
641
642// addConstantValue - Add constant value entry in variable DIE.
643bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
644 bool Unsigned) {
645 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000646 if (CIBitWidth <= 64) {
647 unsigned form = 0;
648 switch (CIBitWidth) {
649 case 8: form = dwarf::DW_FORM_data1; break;
650 case 16: form = dwarf::DW_FORM_data2; break;
651 case 32: form = dwarf::DW_FORM_data4; break;
652 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000653 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000654 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
655 }
Devang Patel161b2f42011-04-12 23:21:44 +0000656 if (Unsigned)
David Blaikie14268412013-01-20 01:18:01 +0000657 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000658 else
David Blaikie14268412013-01-20 01:18:01 +0000659 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000660 return true;
661 }
662
663 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
664
665 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000666 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000667
668 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000669 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000670
671 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000672 for (int i = 0; i < NumBytes; i++) {
673 uint8_t c;
674 if (LittleEndian)
675 c = Ptr64[i / 8] >> (8 * (i & 7));
676 else
677 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
678 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
679 }
Devang Patel161b2f42011-04-12 23:21:44 +0000680
681 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
682 return true;
683}
684
685/// addTemplateParams - Add template parameters in buffer.
686void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
687 // Add template parameters.
688 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
689 DIDescriptor Element = TParams.getElement(i);
690 if (Element.isTemplateTypeParameter())
691 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
692 DITemplateTypeParameter(Element)));
693 else if (Element.isTemplateValueParameter())
694 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
695 DITemplateValueParameter(Element)));
696 }
Devang Patel161b2f42011-04-12 23:21:44 +0000697}
Nick Lewycky746cb672011-10-26 22:55:33 +0000698
Eric Christopher6b6061f2013-01-16 01:22:23 +0000699/// getOrCreateContextDIE - Get context owner's DIE.
700DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
701 if (Context.isType())
702 return getOrCreateTypeDIE(DIType(Context));
703 else if (Context.isNameSpace())
704 return getOrCreateNameSpace(DINameSpace(Context));
705 else if (Context.isSubprogram())
706 return getOrCreateSubprogramDIE(DISubprogram(Context));
707 else
708 return getDIE(Context);
709}
710
Devang Patel161b2f42011-04-12 23:21:44 +0000711/// addToContextOwner - Add Die into the list of its context owner's children.
712void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000713 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000714 ContextDIE->addChild(Die);
715 else
716 addDie(Die);
717}
718
719/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
720/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000721DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
722 DIType Ty(TyNode);
723 if (!Ty.Verify())
724 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000725 DIE *TyDIE = getDIE(Ty);
726 if (TyDIE)
727 return TyDIE;
728
729 // Create new type.
730 TyDIE = new DIE(dwarf::DW_TAG_base_type);
731 insertDIE(Ty, TyDIE);
732 if (Ty.isBasicType())
733 constructTypeDIE(*TyDIE, DIBasicType(Ty));
734 else if (Ty.isCompositeType())
735 constructTypeDIE(*TyDIE, DICompositeType(Ty));
736 else {
737 assert(Ty.isDerivedType() && "Unknown kind of DIType");
738 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
739 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000740 // If this is a named finished type then include it in the list of types
741 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000742 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
743 bool IsImplementation = 0;
744 if (Ty.isCompositeType()) {
745 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000746 // A runtime language of 0 actually means C/C++ and that any
747 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000748 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000749 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000750 }
Eric Christophere0167892012-01-06 23:03:37 +0000751 unsigned Flags = IsImplementation ?
752 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
753 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000754 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000755
Devang Patel161b2f42011-04-12 23:21:44 +0000756 addToContextOwner(TyDIE, Ty.getContext());
757 return TyDIE;
758}
759
760/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000761void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000762 if (!Ty.Verify())
763 return;
764
765 // Check for pre-existence.
766 DIEEntry *Entry = getDIEEntry(Ty);
767 // If it exists then use the existing value.
768 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000769 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000770 return;
771 }
772
773 // Construct type.
774 DIE *Buffer = getOrCreateTypeDIE(Ty);
775
776 // Set up proxy.
777 Entry = createDIEEntry(Buffer);
778 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000779 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000780
781 // If this is a complete composite type then include it in the
782 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000783 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000784}
785
786/// addGlobalType - Add a new global type to the compile unit.
787///
Devang Patelc20bdf12011-06-01 00:23:24 +0000788void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000789 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000790 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
791 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000792 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000793 if (DIEEntry *Entry = getDIEEntry(Ty))
794 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000795}
796
Devang Patel31c5d052011-05-06 16:57:54 +0000797/// addPubTypes - Add type for pubtypes section.
798void CompileUnit::addPubTypes(DISubprogram SP) {
799 DICompositeType SPTy = SP.getType();
800 unsigned SPTag = SPTy.getTag();
801 if (SPTag != dwarf::DW_TAG_subroutine_type)
802 return;
803
804 DIArray Args = SPTy.getTypeArray();
805 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
806 DIType ATy(Args.getElement(i));
807 if (!ATy.Verify())
808 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000809 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000810 }
811}
812
Devang Patel161b2f42011-04-12 23:21:44 +0000813/// constructTypeDIE - Construct basic type die from DIBasicType.
814void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
815 // Get core information.
816 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000817 // Add name if not anonymous or intermediate type.
818 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000819 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000820
821 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
822 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
823 // Unspecified types has only name, nothing else.
824 return;
825 }
826
827 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000828 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000829 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000830
Devang Patel161b2f42011-04-12 23:21:44 +0000831 uint64_t Size = BTy.getSizeInBits() >> 3;
832 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
833}
834
835/// constructTypeDIE - Construct derived type die from DIDerivedType.
836void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
837 // Get core information.
838 StringRef Name = DTy.getName();
839 uint64_t Size = DTy.getSizeInBits() >> 3;
840 unsigned Tag = DTy.getTag();
841
842 // FIXME - Workaround for templates.
843 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
844
845 Buffer.setTag(Tag);
846
847 // Map to main type, void will not have a type.
848 DIType FromTy = DTy.getTypeDerivedFrom();
849 addType(&Buffer, FromTy);
850
851 // Add name if not anonymous or intermediate type.
852 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000853 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000854
855 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000856 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000857 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
858
David Blaikie62fdfb52013-01-07 05:51:15 +0000859 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
860 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
861 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000862 // Add source line info if available and TyDesc is not a forward declaration.
863 if (!DTy.isForwardDecl())
864 addSourceLine(&Buffer, DTy);
865}
866
867/// constructTypeDIE - Construct type DIE from DICompositeType.
868void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
869 // Get core information.
870 StringRef Name = CTy.getName();
871
872 uint64_t Size = CTy.getSizeInBits() >> 3;
873 unsigned Tag = CTy.getTag();
874 Buffer.setTag(Tag);
875
876 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000877 case dwarf::DW_TAG_array_type:
878 constructArrayTypeDIE(Buffer, &CTy);
879 break;
880 case dwarf::DW_TAG_enumeration_type: {
881 DIArray Elements = CTy.getTypeArray();
882
883 // Add enumerators to enumeration type.
884 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
885 DIE *ElemDie = NULL;
886 DIDescriptor Enum(Elements.getElement(i));
887 if (Enum.isEnumerator()) {
888 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
889 Buffer.addChild(ElemDie);
890 }
891 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000892 DIType DTy = CTy.getTypeDerivedFrom();
893 if (DTy.Verify()) {
894 addType(&Buffer, DTy);
895 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
896 }
Devang Patel161b2f42011-04-12 23:21:44 +0000897 }
898 break;
899 case dwarf::DW_TAG_subroutine_type: {
900 // Add return type.
901 DIArray Elements = CTy.getTypeArray();
902 DIDescriptor RTy = Elements.getElement(0);
903 addType(&Buffer, DIType(RTy));
904
905 bool isPrototyped = true;
906 // Add arguments.
907 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
908 DIDescriptor Ty = Elements.getElement(i);
909 if (Ty.isUnspecifiedParameter()) {
910 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
911 Buffer.addChild(Arg);
912 isPrototyped = false;
913 } else {
914 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
915 addType(Arg, DIType(Ty));
David Blaikieaaf2e632013-01-29 19:35:24 +0000916 if (DIType(Ty).isArtificial())
917 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +0000918 Buffer.addChild(Arg);
919 }
920 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000921 // Add prototype flag if we're dealing with a C language and the
922 // function has been prototyped.
923 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000924 (Language == dwarf::DW_LANG_C89 ||
925 Language == dwarf::DW_LANG_C99 ||
926 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000927 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000928 }
929 break;
930 case dwarf::DW_TAG_structure_type:
931 case dwarf::DW_TAG_union_type:
932 case dwarf::DW_TAG_class_type: {
933 // Add elements to structure type.
934 DIArray Elements = CTy.getTypeArray();
935
936 // A forward struct declared type may not have elements available.
937 unsigned N = Elements.getNumElements();
938 if (N == 0)
939 break;
940
941 // Add elements to structure type.
942 for (unsigned i = 0; i < N; ++i) {
943 DIDescriptor Element = Elements.getElement(i);
944 DIE *ElemDie = NULL;
945 if (Element.isSubprogram()) {
946 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000947 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000948 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000949 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000950 dwarf::DW_ACCESS_protected);
951 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000952 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000953 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000954 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000955 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000956 dwarf::DW_ACCESS_public);
957 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000958 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000959 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000960 DIDerivedType DDTy(Element);
961 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
962 ElemDie = new DIE(dwarf::DW_TAG_friend);
963 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000964 } else if (DDTy.isStaticMember())
965 ElemDie = createStaticMemberDIE(DDTy);
966 else
967 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000968 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000969 DIObjCProperty Property(Element);
970 ElemDie = new DIE(Property.getTag());
971 StringRef PropertyName = Property.getObjCPropertyName();
972 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000973 addType(ElemDie, Property.getType());
974 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000975 StringRef GetterName = Property.getObjCPropertyGetterName();
976 if (!GetterName.empty())
977 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
978 StringRef SetterName = Property.getObjCPropertySetterName();
979 if (!SetterName.empty())
980 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
981 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000982 if (Property.isReadOnlyObjCProperty())
983 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
984 if (Property.isReadWriteObjCProperty())
985 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
986 if (Property.isAssignObjCProperty())
987 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
988 if (Property.isRetainObjCProperty())
989 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
990 if (Property.isCopyObjCProperty())
991 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
992 if (Property.isNonAtomicObjCProperty())
993 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
994 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000995 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000996 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +0000997
Devang Patel30d409c2012-02-07 23:33:58 +0000998 DIEEntry *Entry = getDIEEntry(Element);
999 if (!Entry) {
1000 Entry = createDIEEntry(ElemDie);
1001 insertDIEEntry(Element, Entry);
1002 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001003 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001004 continue;
1005 Buffer.addChild(ElemDie);
1006 }
1007
1008 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001009 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001010
Devang Patel161b2f42011-04-12 23:21:44 +00001011 DICompositeType ContainingType = CTy.getContainingType();
1012 if (DIDescriptor(ContainingType).isCompositeType())
1013 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1014 getOrCreateTypeDIE(DIType(ContainingType)));
1015 else {
1016 DIDescriptor Context = CTy.getContext();
1017 addToContextOwner(&Buffer, Context);
1018 }
1019
Devang Patel201e6cd2011-05-12 21:29:42 +00001020 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001021 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001022
Eric Christopher1a8e8862011-12-16 23:42:42 +00001023 // Add template parameters to a class, structure or union types.
1024 // FIXME: The support isn't in the metadata for this yet.
1025 if (Tag == dwarf::DW_TAG_class_type ||
1026 Tag == dwarf::DW_TAG_structure_type ||
1027 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001028 addTemplateParams(Buffer, CTy.getTemplateParams());
1029
1030 break;
1031 }
1032 default:
1033 break;
1034 }
1035
1036 // Add name if not anonymous or intermediate type.
1037 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001038 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001039
Eric Christopher4a5d8392012-05-22 18:45:18 +00001040 if (Tag == dwarf::DW_TAG_enumeration_type ||
1041 Tag == dwarf::DW_TAG_class_type ||
1042 Tag == dwarf::DW_TAG_structure_type ||
1043 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001044 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001045 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001046 if (Size)
1047 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001048 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001049 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001050 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1051
1052 // If we're a forward decl, say so.
1053 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001054 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001055
1056 // Add source line info if available.
1057 if (!CTy.isForwardDecl())
1058 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001059
1060 // No harm in adding the runtime language to the declaration.
1061 unsigned RLang = CTy.getRunTimeLang();
1062 if (RLang)
1063 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1064 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001065 }
1066}
1067
Eric Christopher8b4310b2012-11-21 00:34:38 +00001068/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001069/// for the given DITemplateTypeParameter.
1070DIE *
1071CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1072 DIE *ParamDIE = getDIE(TP);
1073 if (ParamDIE)
1074 return ParamDIE;
1075
1076 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1077 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001078 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001079 return ParamDIE;
1080}
1081
Eric Christopher8b4310b2012-11-21 00:34:38 +00001082/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001083/// for the given DITemplateValueParameter.
1084DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001085CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001086 DIE *ParamDIE = getDIE(TPV);
1087 if (ParamDIE)
1088 return ParamDIE;
1089
1090 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1091 addType(ParamDIE, TPV.getType());
1092 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001093 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001094 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +00001095 TPV.getValue());
1096 return ParamDIE;
1097}
1098
Devang Patel31c5d052011-05-06 16:57:54 +00001099/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1100DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1101 DIE *NDie = getDIE(NS);
1102 if (NDie)
1103 return NDie;
1104 NDie = new DIE(dwarf::DW_TAG_namespace);
1105 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001106 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001107 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001108 addAccelNamespace(NS.getName(), NDie);
1109 } else
1110 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001111 addSourceLine(NDie, NS);
1112 addToContextOwner(NDie, NS.getContext());
1113 return NDie;
1114}
1115
Devang Pateldbc64af2011-08-15 17:24:54 +00001116/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1117/// printer to not emit usual symbol prefix before the symbol name is used then
1118/// return linkage name after skipping this special LLVM prefix.
1119static StringRef getRealLinkageName(StringRef LinkageName) {
1120 char One = '\1';
1121 if (LinkageName.startswith(StringRef(&One, 1)))
1122 return LinkageName.substr(1);
1123 return LinkageName;
1124}
1125
1126/// getOrCreateSubprogramDIE - Create new DIE using SP.
1127DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1128 DIE *SPDie = getDIE(SP);
1129 if (SPDie)
1130 return SPDie;
1131
Peter Collingbourne27302f02012-05-27 18:36:44 +00001132 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1133
1134 // DW_TAG_inlined_subroutine may refer to this DIE.
1135 insertDIE(SP, SPDie);
1136
Rafael Espindola01b55b42011-11-10 22:34:29 +00001137 DISubprogram SPDecl = SP.getFunctionDeclaration();
1138 DIE *DeclDie = NULL;
1139 if (SPDecl.isSubprogram()) {
1140 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1141 }
1142
Devang Pateldbc64af2011-08-15 17:24:54 +00001143 // Add to context owner.
1144 addToContextOwner(SPDie, SP.getContext());
1145
1146 // Add function template parameters.
1147 addTemplateParams(*SPDie, SP.getTemplateParams());
1148
Eric Christophere9722e12012-04-16 23:54:23 +00001149 // Unfortunately this code needs to stay here instead of below the
1150 // AT_specification code in order to work around a bug in older
1151 // gdbs that requires the linkage name to resolve multiple template
1152 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001153 // TODO: Remove this set of code when we get rid of the old gdb
1154 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001155 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001156 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001157 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1158 getRealLinkageName(LinkageName));
1159
Devang Pateldbc64af2011-08-15 17:24:54 +00001160 // If this DIE is going to refer declaration info using AT_specification
1161 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001162 if (DeclDie) {
1163 // Refer function declaration directly.
1164 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1165 DeclDie);
1166
Devang Pateldbc64af2011-08-15 17:24:54 +00001167 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001168 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001169
Eric Christophercbbd5b12012-08-23 22:52:55 +00001170 // Add the linkage name if we have one.
1171 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1172 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1173 getRealLinkageName(LinkageName));
1174
Devang Pateldbc64af2011-08-15 17:24:54 +00001175 // Constructors and operators for anonymous aggregates do not have names.
1176 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001177 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001178
1179 addSourceLine(SPDie, SP);
1180
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001181 // Add the prototype if we have a prototype and we have a C like
1182 // language.
1183 if (SP.isPrototyped() &&
1184 (Language == dwarf::DW_LANG_C89 ||
1185 Language == dwarf::DW_LANG_C99 ||
1186 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001187 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001188
1189 // Add Return Type.
1190 DICompositeType SPTy = SP.getType();
1191 DIArray Args = SPTy.getTypeArray();
1192 unsigned SPTag = SPTy.getTag();
1193
1194 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1195 addType(SPDie, SPTy);
1196 else
1197 addType(SPDie, DIType(Args.getElement(0)));
1198
1199 unsigned VK = SP.getVirtuality();
1200 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001201 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001202 DIEBlock *Block = getDIEBlock();
1203 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1204 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1205 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1206 ContainingTypeMap.insert(std::make_pair(SPDie,
1207 SP.getContainingType()));
1208 }
1209
1210 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001211 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001212
Devang Pateldbc64af2011-08-15 17:24:54 +00001213 // Add arguments. Do not add arguments for subprogram definition. They will
1214 // be handled while processing variables.
1215 DICompositeType SPTy = SP.getType();
1216 DIArray Args = SPTy.getTypeArray();
1217 unsigned SPTag = SPTy.getTag();
1218
1219 if (SPTag == dwarf::DW_TAG_subroutine_type)
1220 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1221 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001222 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001223 addType(Arg, ATy);
1224 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001225 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001226 SPDie->addChild(Arg);
1227 }
1228 }
1229
1230 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001231 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001232
1233 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001234 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001235
1236 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001237 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001238
1239 if (unsigned isa = Asm->getISAEncoding()) {
1240 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1241 }
1242
1243 return SPDie;
1244}
1245
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001246// Return const expression if value is a GEP to access merged global
1247// constant. e.g.
1248// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1249static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1250 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1251 if (!CE || CE->getNumOperands() != 3 ||
1252 CE->getOpcode() != Instruction::GetElementPtr)
1253 return NULL;
1254
1255 // First operand points to a global struct.
1256 Value *Ptr = CE->getOperand(0);
1257 if (!isa<GlobalValue>(Ptr) ||
1258 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1259 return NULL;
1260
1261 // Second operand is zero.
1262 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1263 if (!CI || !CI->isZero())
1264 return NULL;
1265
1266 // Third operand is offset.
1267 if (!isa<ConstantInt>(CE->getOperand(2)))
1268 return NULL;
1269
1270 return CE;
1271}
1272
1273/// createGlobalVariableDIE - create global variable DIE.
1274void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001275 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001276 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001277 return;
1278
Devang Patel49e2f032011-08-18 22:21:50 +00001279 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001280 if (!GV.Verify())
1281 return;
1282
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001283 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001284 DIType GTy = GV.getType();
1285
1286 // If this is a static data member definition, some attributes belong
1287 // to the declaration DIE.
1288 DIE *VariableDIE = NULL;
1289 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1290 if (SDMDecl.Verify()) {
1291 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1292 // We need the declaration DIE that is in the static member's class.
1293 // But that class might not exist in the DWARF yet.
1294 // Creating the class will create the static member decl DIE.
1295 getOrCreateContextDIE(SDMDecl.getContext());
1296 VariableDIE = getDIE(SDMDecl);
1297 assert(VariableDIE && "Static member decl has no context?");
1298 }
1299
1300 // If this is not a static data member definition, create the variable
1301 // DIE and add the initial set of attributes to it.
1302 if (!VariableDIE) {
1303 VariableDIE = new DIE(GV.getTag());
1304 // Add to map.
1305 insertDIE(N, VariableDIE);
1306
1307 // Add name and type.
1308 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1309 addType(VariableDIE, GTy);
1310
1311 // Add scoping info.
1312 if (!GV.isLocalToUnit())
1313 addFlag(VariableDIE, dwarf::DW_AT_external);
1314
1315 // Add line number info.
1316 addSourceLine(VariableDIE, GV);
1317 // Add to context owner.
1318 addToContextOwner(VariableDIE, GVContext);
1319 }
1320
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001321 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001322 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001323 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001324 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001325 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001326 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001327 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopher0969ddf2013-01-18 22:11:33 +00001328 addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal()));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001329 // Do not create specification DIE if context is either compile unit
1330 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001331 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001332 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1333 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001334 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001335 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1336 dwarf::DW_FORM_ref4, VariableDIE);
1337 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001338 // A static member's declaration is already flagged as such.
1339 if (!SDMDecl.Verify())
1340 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001341 addDie(VariableSpecDIE);
1342 } else {
1343 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001344 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001345 // Add linkage name.
1346 StringRef LinkageName = GV.getLinkageName();
1347 if (!LinkageName.empty() && isGlobalVariable)
1348 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1349 getRealLinkageName(LinkageName));
Eric Christopher8b4310b2012-11-21 00:34:38 +00001350 } else if (const ConstantInt *CI =
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001351 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1352 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1353 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001354 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001355 // GV is a merged global.
1356 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1357 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001358 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001359 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1360 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001361 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001362 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001363 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1364 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1365 }
1366
Eric Christopherd117fbb2011-11-11 01:55:22 +00001367 if (addToAccelTable) {
1368 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1369 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001370
Eric Christopherd117fbb2011-11-11 01:55:22 +00001371 // If the linkage name is different than the name, go ahead and output
1372 // that as well into the name table.
1373 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1374 addAccelName(GV.getLinkageName(), AddrDIE);
1375 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001376
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001377 return;
1378}
1379
Devang Patel161b2f42011-04-12 23:21:44 +00001380/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001381void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1382 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001383 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1384 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001385
Bill Wendling222c2fd2012-12-06 07:38:10 +00001386 // The LowerBound value defines the lower bounds which is typically zero for
1387 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1388 // Count == -1 then the array is unbounded and we do not emit
1389 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1390 // Count == 0, then the array has zero elements in which case we do not emit
1391 // an upper bound.
1392 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001393 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001394 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001395
Bill Wendling6afe4782012-12-06 07:55:19 +00001396 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001397 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1398
1399 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001400 // FIXME: An unbounded array should reference the expression that defines
1401 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001402 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001403
Devang Patel161b2f42011-04-12 23:21:44 +00001404 Buffer.addChild(DW_Subrange);
1405}
1406
1407/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1408void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1409 DICompositeType *CTy) {
1410 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001411 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001412 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001413
1414 // Emit derived type.
1415 addType(&Buffer, CTy->getTypeDerivedFrom());
1416 DIArray Elements = CTy->getTypeArray();
1417
1418 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001419 // FIXME: This type should be passed down from the front end
1420 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001421 DIE *IdxTy = getIndexTyDie();
1422 if (!IdxTy) {
1423 // Construct an anonymous type for index type.
1424 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001425 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001426 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1427 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1428 dwarf::DW_ATE_signed);
1429 addDie(IdxTy);
1430 setIndexTyDie(IdxTy);
1431 }
1432
1433 // Add subranges to array type.
1434 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1435 DIDescriptor Element = Elements.getElement(i);
1436 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1437 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1438 }
1439}
1440
1441/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1442DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1443 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1444 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001445 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001446 int64_t Value = ETy.getEnumValue();
1447 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1448 return Enumerator;
1449}
1450
Devang Pateldbc64af2011-08-15 17:24:54 +00001451/// constructContainingTypeDIEs - Construct DIEs for types that contain
1452/// vtables.
1453void CompileUnit::constructContainingTypeDIEs() {
1454 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1455 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1456 DIE *SPDie = CI->first;
1457 const MDNode *N = CI->second;
1458 if (!N) continue;
1459 DIE *NDie = getDIE(N);
1460 if (!NDie) continue;
1461 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1462 }
1463}
1464
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001465/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1466DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1467 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001468
1469 // Translate tag to proper Dwarf tag.
1470 unsigned Tag = DV->getTag();
1471
1472 // Define variable debug information entry.
1473 DIE *VariableDie = new DIE(Tag);
1474 DbgVariable *AbsVar = DV->getAbstractVariable();
1475 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1476 if (AbsDIE)
1477 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1478 dwarf::DW_FORM_ref4, AbsDIE);
1479 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001480 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001481 addSourceLine(VariableDie, DV->getVariable());
1482 addType(VariableDie, DV->getType());
1483 }
1484
1485 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001486 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001487
1488 if (isScopeAbstract) {
1489 DV->setDIE(VariableDie);
1490 return VariableDie;
1491 }
1492
1493 // Add variable address.
1494
1495 unsigned Offset = DV->getDotDebugLocOffset();
1496 if (Offset != ~0U) {
1497 addLabel(VariableDie, dwarf::DW_AT_location,
1498 dwarf::DW_FORM_data4,
1499 Asm->GetTempSymbol("debug_loc", Offset));
1500 DV->setDIE(VariableDie);
1501 return VariableDie;
1502 }
1503
Eric Christopher8cf5e742011-10-03 15:49:20 +00001504 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001505 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1506 bool updated = false;
1507 if (DVInsn->getNumOperands() == 3) {
1508 if (DVInsn->getOperand(0).isReg()) {
1509 const MachineOperand RegOp = DVInsn->getOperand(0);
1510 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1511 if (DVInsn->getOperand(1).isImm() &&
1512 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1513 unsigned FrameReg = 0;
1514 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001515 int Offset =
1516 TFI->getFrameIndexReference(*Asm->MF,
1517 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001518 FrameReg);
1519 MachineLocation Location(FrameReg, Offset);
1520 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001521
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001522 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001523 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001524 MachineLocation(RegOp.getReg()));
1525 updated = true;
1526 }
1527 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001528 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001529 addConstantValue(VariableDie, DVInsn->getOperand(0),
1530 DV->getType());
1531 else if (DVInsn->getOperand(0).isFPImm())
1532 updated =
1533 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1534 else if (DVInsn->getOperand(0).isCImm())
1535 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001536 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001537 DVInsn->getOperand(0).getCImm(),
1538 DV->getType().isUnsignedDIType());
1539 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001540 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001541 Asm->getDebugValueLocation(DVInsn));
1542 updated = true;
1543 }
1544 if (!updated) {
1545 // If variableDie is not updated then DBG_VALUE instruction does not
1546 // have valid variable info.
1547 delete VariableDie;
1548 return NULL;
1549 }
1550 DV->setDIE(VariableDie);
1551 return VariableDie;
1552 } else {
1553 // .. else use frame index.
1554 int FI = DV->getFrameIndex();
1555 if (FI != ~0) {
1556 unsigned FrameReg = 0;
1557 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001558 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001559 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1560 MachineLocation Location(FrameReg, Offset);
1561 addVariableAddress(DV, VariableDie, Location);
1562 }
1563 }
1564
1565 DV->setDIE(VariableDie);
1566 return VariableDie;
1567}
1568
Devang Patel161b2f42011-04-12 23:21:44 +00001569/// createMemberDIE - Create new member DIE.
1570DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1571 DIE *MemberDie = new DIE(DT.getTag());
1572 StringRef Name = DT.getName();
1573 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001574 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001575
1576 addType(MemberDie, DT.getTypeDerivedFrom());
1577
1578 addSourceLine(MemberDie, DT);
1579
1580 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1581 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1582
1583 uint64_t Size = DT.getSizeInBits();
1584 uint64_t FieldSize = DT.getOriginalTypeSize();
1585
1586 if (Size != FieldSize) {
1587 // Handle bitfield.
1588 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1589 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1590
1591 uint64_t Offset = DT.getOffsetInBits();
1592 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1593 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1594 uint64_t FieldOffset = (HiMark - FieldSize);
1595 Offset -= FieldOffset;
1596
1597 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001598 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001599 Offset = FieldSize - (Offset + Size);
1600 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1601
1602 // Here WD_AT_data_member_location points to the anonymous
1603 // field that includes this bit field.
1604 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1605
1606 } else
1607 // This is not a bitfield.
1608 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1609
1610 if (DT.getTag() == dwarf::DW_TAG_inheritance
1611 && DT.isVirtual()) {
1612
1613 // For C++, virtual base classes are not at fixed offset. Use following
1614 // expression to extract appropriate offset from vtable.
1615 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1616
1617 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1618 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1619 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1620 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1621 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1622 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1623 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1624 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1625
1626 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1627 VBaseLocationDie);
1628 } else
1629 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1630
1631 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001632 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001633 dwarf::DW_ACCESS_protected);
1634 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001635 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001636 dwarf::DW_ACCESS_private);
1637 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001638 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001639 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001640 dwarf::DW_ACCESS_public);
1641 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001642 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001643 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001644
1645 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001646 if (MDNode *PNode = DT.getObjCProperty())
1647 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001648 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001649 PropertyDie);
1650
David Blaikie01bc2b32012-12-13 22:43:07 +00001651 if (DT.isArtificial())
1652 addFlag(MemberDie, dwarf::DW_AT_artificial);
1653
Devang Patel9e11eb12012-02-04 01:30:32 +00001654 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001655 StringRef PropertyName = DT.getObjCPropertyName();
1656 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001657 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001658 StringRef GetterName = DT.getObjCPropertyGetterName();
1659 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001660 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001661 StringRef SetterName = DT.getObjCPropertySetterName();
1662 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001663 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001664 unsigned PropertyAttributes = 0;
1665 if (DT.isReadOnlyObjCProperty())
1666 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1667 if (DT.isReadWriteObjCProperty())
1668 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1669 if (DT.isAssignObjCProperty())
1670 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1671 if (DT.isRetainObjCProperty())
1672 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1673 if (DT.isCopyObjCProperty())
1674 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1675 if (DT.isNonAtomicObjCProperty())
1676 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1677 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001678 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patele9db5e22011-04-16 00:11:51 +00001679 PropertyAttributes);
1680 }
Devang Patel161b2f42011-04-12 23:21:44 +00001681 return MemberDie;
1682}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001683
1684/// createStaticMemberDIE - Create new DIE for C++ static member.
1685DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1686 if (!DT.Verify())
1687 return NULL;
1688
1689 DIE *StaticMemberDIE = new DIE(DT.getTag());
1690 DIType Ty = DT.getTypeDerivedFrom();
1691
1692 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1693 addType(StaticMemberDIE, Ty);
1694 addSourceLine(StaticMemberDIE, DT);
1695 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1696 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1697
1698 // FIXME: We could omit private if the parent is a class_type, and
1699 // public if the parent is something else.
1700 if (DT.isProtected())
1701 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1702 dwarf::DW_ACCESS_protected);
1703 else if (DT.isPrivate())
1704 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1705 dwarf::DW_ACCESS_private);
1706 else
1707 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1708 dwarf::DW_ACCESS_public);
1709
1710 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1711 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
David Blaikie14268412013-01-20 01:18:01 +00001712 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1713 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001714
1715 insertDIE(DT, StaticMemberDIE);
1716 return StaticMemberDIE;
1717}