blob: 89abcffd938a95b9e194a7e346223bf362ae0a7b [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),
Manman Renbc3e96f2013-03-12 18:27:15 +000038 IndexTyDie(0), DebugInfoOffset(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(),
Manman Ren3de61b42013-03-07 01:42:00 +0000244 V.getContext().getDirectory(),
245 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000246 assert(FileID && "Invalid file id");
247 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
248 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
249}
250
251/// addSourceLine - Add location information to specified debug information
252/// entry.
253void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
254 // Verify global variable.
255 if (!G.Verify())
256 return;
257
258 unsigned Line = G.getLineNumber();
259 if (Line == 0)
260 return;
Manman Ren3de61b42013-03-07 01:42:00 +0000261 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
262 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000263 assert(FileID && "Invalid file id");
264 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
265 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
266}
267
268/// addSourceLine - Add location information to specified debug information
269/// entry.
270void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
271 // Verify subprogram.
272 if (!SP.Verify())
273 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000274
Devang Patel161b2f42011-04-12 23:21:44 +0000275 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000276 unsigned Line = SP.getLineNumber();
277 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000278 return;
279
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000280 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000281 SP.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000282 assert(FileID && "Invalid file id");
283 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
284 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
285}
286
287/// addSourceLine - Add location information to specified debug information
288/// entry.
289void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
290 // Verify type.
291 if (!Ty.Verify())
292 return;
293
294 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000295 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000296 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000297 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000298 Ty.getDirectory(), getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000299 assert(FileID && "Invalid file id");
300 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
301 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
302}
303
304/// addSourceLine - Add location information to specified debug information
305/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000306void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
307 // Verify type.
308 if (!Ty.Verify())
309 return;
310
311 unsigned Line = Ty.getLineNumber();
312 if (Line == 0)
313 return;
314 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000315 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Manman Ren3de61b42013-03-07 01:42:00 +0000316 File.getDirectory(), getUniqueID());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000317 assert(FileID && "Invalid file id");
318 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
319 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
320}
321
322/// addSourceLine - Add location information to specified debug information
323/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000324void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
325 // Verify namespace.
326 if (!NS.Verify())
327 return;
328
329 unsigned Line = NS.getLineNumber();
330 if (Line == 0)
331 return;
332 StringRef FN = NS.getFilename();
333
Manman Ren3de61b42013-03-07 01:42:00 +0000334 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
335 getUniqueID());
Devang Patel161b2f42011-04-12 23:21:44 +0000336 assert(FileID && "Invalid file id");
337 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
338 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
339}
340
Eric Christopher8b4310b2012-11-21 00:34:38 +0000341/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000342/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000343void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000344 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000345 if (DV->variableHasComplexAddress())
346 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
347 else if (DV->isBlockByrefVariable())
348 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
349 else
350 addAddress(Die, dwarf::DW_AT_location, Location);
351}
352
Devang Patel116da2f2011-04-26 19:06:18 +0000353/// addRegisterOp - Add register operand.
354void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
355 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
356 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
357 if (DWReg < 32)
358 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
359 else {
360 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
361 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
362 }
363}
364
365/// addRegisterOffset - Add register offset.
366void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
367 int64_t Offset) {
368 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
369 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
370 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
371 if (Reg == TRI->getFrameRegister(*Asm->MF))
372 // If variable offset is based in frame register then use fbreg.
373 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
374 else if (DWReg < 32)
375 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
376 else {
377 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
378 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
379 }
380 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
381}
382
383/// addAddress - Add an address attribute to a die based on the location
384/// provided.
385void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
386 const MachineLocation &Location) {
387 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
388
389 if (Location.isReg())
390 addRegisterOp(Block, Location.getReg());
391 else
392 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
393
394 // Now attach the location information to the DIE.
395 addBlock(Die, Attribute, 0, Block);
396}
397
Devang Patel161b2f42011-04-12 23:21:44 +0000398/// addComplexAddress - Start with the address based on the location provided,
399/// and generate the DWARF information necessary to find the actual variable
400/// given the extra address information encoded in the DIVariable, starting from
401/// the starting location. Add the DWARF information to the die.
402///
403void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
404 unsigned Attribute,
405 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000406 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000407 unsigned N = DV->getNumAddrElements();
408 unsigned i = 0;
409 if (Location.isReg()) {
410 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
411 // If first address element is OpPlus then emit
412 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
413 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
414 i = 2;
415 } else
416 addRegisterOp(Block, Location.getReg());
417 }
Devang Patel116da2f2011-04-26 19:06:18 +0000418 else
419 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000420
Devang Patelc26f5442011-04-28 02:22:40 +0000421 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000422 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000423 if (Element == DIBuilder::OpPlus) {
424 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
425 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
426 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000427 if (!Location.isReg())
428 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000429 } else llvm_unreachable("unknown DIBuilder Opcode");
430 }
431
432 // Now attach the location information to the DIE.
433 addBlock(Die, Attribute, 0, Block);
434}
435
436/* Byref variables, in Blocks, are declared by the programmer as "SomeType
437 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
438 gives the variable VarName either the struct, or a pointer to the struct, as
439 its type. This is necessary for various behind-the-scenes things the
440 compiler needs to do with by-reference variables in Blocks.
441
442 However, as far as the original *programmer* is concerned, the variable
443 should still have type 'SomeType', as originally declared.
444
445 The function getBlockByrefType dives into the __Block_byref_x_VarName
446 struct to find the original type of the variable, which is then assigned to
447 the variable's Debug Information Entry as its real type. So far, so good.
448 However now the debugger will expect the variable VarName to have the type
449 SomeType. So we need the location attribute for the variable to be an
450 expression that explains to the debugger how to navigate through the
451 pointers and struct to find the actual variable of type SomeType.
452
453 The following function does just that. We start by getting
454 the "normal" location for the variable. This will be the location
455 of either the struct __Block_byref_x_VarName or the pointer to the
456 struct __Block_byref_x_VarName.
457
458 The struct will look something like:
459
460 struct __Block_byref_x_VarName {
461 ... <various fields>
462 struct __Block_byref_x_VarName *forwarding;
463 ... <various other fields>
464 SomeType VarName;
465 ... <maybe more fields>
466 };
467
468 If we are given the struct directly (as our starting point) we
469 need to tell the debugger to:
470
471 1). Add the offset of the forwarding field.
472
473 2). Follow that pointer to get the real __Block_byref_x_VarName
474 struct to use (the real one may have been copied onto the heap).
475
476 3). Add the offset for the field VarName, to find the actual variable.
477
478 If we started with a pointer to the struct, then we need to
479 dereference that pointer first, before the other steps.
480 Translating this into DWARF ops, we will need to append the following
481 to the current location description for the variable:
482
483 DW_OP_deref -- optional, if we start with a pointer
484 DW_OP_plus_uconst <forward_fld_offset>
485 DW_OP_deref
486 DW_OP_plus_uconst <varName_fld_offset>
487
488 That is what this function does. */
489
490/// addBlockByrefAddress - Start with the address based on the location
491/// provided, and generate the DWARF information necessary to find the
492/// actual Block variable (navigating the Block struct) based on the
493/// starting location. Add the DWARF information to the die. For
494/// more information, read large comment just above here.
495///
496void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
497 unsigned Attribute,
498 const MachineLocation &Location) {
499 DIType Ty = DV->getType();
500 DIType TmpTy = Ty;
501 unsigned Tag = Ty.getTag();
502 bool isPointer = false;
503
504 StringRef varName = DV->getName();
505
506 if (Tag == dwarf::DW_TAG_pointer_type) {
507 DIDerivedType DTy = DIDerivedType(Ty);
508 TmpTy = DTy.getTypeDerivedFrom();
509 isPointer = true;
510 }
511
512 DICompositeType blockStruct = DICompositeType(TmpTy);
513
514 // Find the __forwarding field and the variable field in the __Block_byref
515 // struct.
516 DIArray Fields = blockStruct.getTypeArray();
517 DIDescriptor varField = DIDescriptor();
518 DIDescriptor forwardingField = DIDescriptor();
519
520 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
521 DIDescriptor Element = Fields.getElement(i);
522 DIDerivedType DT = DIDerivedType(Element);
523 StringRef fieldName = DT.getName();
524 if (fieldName == "__forwarding")
525 forwardingField = Element;
526 else if (fieldName == varName)
527 varField = Element;
528 }
529
530 // Get the offsets for the forwarding field and the variable field.
531 unsigned forwardingFieldOffset =
532 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
533 unsigned varFieldOffset =
534 DIDerivedType(varField).getOffsetInBits() >> 3;
535
536 // Decode the original location, and use that as the start of the byref
537 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000538 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
539
Eric Christophercaba2632012-07-04 02:02:18 +0000540 if (Location.isReg())
541 addRegisterOp(Block, Location.getReg());
542 else
543 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000544
545 // If we started with a pointer to the __Block_byref... struct, then
546 // the first thing we need to do is dereference the pointer (DW_OP_deref).
547 if (isPointer)
548 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
549
550 // Next add the offset for the '__forwarding' field:
551 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
552 // adding the offset if it's 0.
553 if (forwardingFieldOffset > 0) {
554 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
555 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
556 }
557
558 // Now dereference the __forwarding field to get to the real __Block_byref
559 // struct: DW_OP_deref.
560 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
561
562 // Now that we've got the real __Block_byref... struct, add the offset
563 // for the variable's field to get to the location of the actual variable:
564 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
565 if (varFieldOffset > 0) {
566 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
567 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
568 }
569
570 // Now attach the location information to the DIE.
571 addBlock(Die, Attribute, 0, Block);
572}
573
Devang Patel4ec14b02011-07-20 21:57:04 +0000574/// isTypeSigned - Return true if the type is signed.
575static bool isTypeSigned(DIType Ty, int *SizeInBits) {
576 if (Ty.isDerivedType())
577 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
578 if (Ty.isBasicType())
579 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
580 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
581 *SizeInBits = Ty.getSizeInBits();
582 return true;
583 }
584 return false;
585}
586
Devang Patel161b2f42011-04-12 23:21:44 +0000587/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000588bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
589 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000590 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000591 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000592 int SizeInBits = -1;
593 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
594 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
595 switch (SizeInBits) {
596 case 8: Form = dwarf::DW_FORM_data1; break;
597 case 16: Form = dwarf::DW_FORM_data2; break;
598 case 32: Form = dwarf::DW_FORM_data4; break;
599 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000600 default: break;
601 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000602 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000603 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000604
Devang Patel161b2f42011-04-12 23:21:44 +0000605 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
606 return true;
607}
608
609/// addConstantFPValue - Add constant value entry in variable DIE.
610bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000611 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000612 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
613 APFloat FPImm = MO.getFPImm()->getValueAPF();
614
615 // Get the raw data form of the floating point.
616 const APInt FltVal = FPImm.bitcastToAPInt();
617 const char *FltPtr = (const char*)FltVal.getRawData();
618
619 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000620 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000621 int Incr = (LittleEndian ? 1 : -1);
622 int Start = (LittleEndian ? 0 : NumBytes - 1);
623 int Stop = (LittleEndian ? NumBytes : -1);
624
625 // Output the constant to DWARF one byte at a time.
626 for (; Start != Stop; Start += Incr)
627 addUInt(Block, 0, dwarf::DW_FORM_data1,
628 (unsigned char)0xFF & FltPtr[Start]);
629
630 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
631 return true;
632}
633
David Blaikie14268412013-01-20 01:18:01 +0000634/// addConstantFPValue - Add constant value entry in variable DIE.
635bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
636 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
637}
638
Devang Patel161b2f42011-04-12 23:21:44 +0000639/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000640bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000641 bool Unsigned) {
David Blaikie14268412013-01-20 01:18:01 +0000642 return addConstantValue(Die, CI->getValue(), Unsigned);
643}
644
645// addConstantValue - Add constant value entry in variable DIE.
646bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
647 bool Unsigned) {
648 unsigned CIBitWidth = Val.getBitWidth();
Devang Pateld6a81362011-05-28 00:39:18 +0000649 if (CIBitWidth <= 64) {
650 unsigned form = 0;
651 switch (CIBitWidth) {
652 case 8: form = dwarf::DW_FORM_data1; break;
653 case 16: form = dwarf::DW_FORM_data2; break;
654 case 32: form = dwarf::DW_FORM_data4; break;
655 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000656 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000657 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
658 }
Devang Patel161b2f42011-04-12 23:21:44 +0000659 if (Unsigned)
David Blaikie14268412013-01-20 01:18:01 +0000660 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000661 else
David Blaikie14268412013-01-20 01:18:01 +0000662 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000663 return true;
664 }
665
666 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
667
668 // Get the raw data form of the large APInt.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000669 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000670
671 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000672 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000673
674 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000675 for (int i = 0; i < NumBytes; i++) {
676 uint8_t c;
677 if (LittleEndian)
678 c = Ptr64[i / 8] >> (8 * (i & 7));
679 else
680 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
681 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
682 }
Devang Patel161b2f42011-04-12 23:21:44 +0000683
684 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
685 return true;
686}
687
Eric Christopher6c3bb942013-04-22 07:47:40 +0000688/// addTemplateParams - Add template parameters into buffer.
Devang Patel161b2f42011-04-12 23:21:44 +0000689void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
690 // Add template parameters.
691 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
692 DIDescriptor Element = TParams.getElement(i);
693 if (Element.isTemplateTypeParameter())
694 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
695 DITemplateTypeParameter(Element)));
696 else if (Element.isTemplateValueParameter())
697 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
698 DITemplateValueParameter(Element)));
699 }
Devang Patel161b2f42011-04-12 23:21:44 +0000700}
Nick Lewycky746cb672011-10-26 22:55:33 +0000701
Eric Christopher6b6061f2013-01-16 01:22:23 +0000702/// getOrCreateContextDIE - Get context owner's DIE.
703DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
704 if (Context.isType())
705 return getOrCreateTypeDIE(DIType(Context));
706 else if (Context.isNameSpace())
707 return getOrCreateNameSpace(DINameSpace(Context));
708 else if (Context.isSubprogram())
709 return getOrCreateSubprogramDIE(DISubprogram(Context));
Eric Christopher6c3bb942013-04-22 07:47:40 +0000710 else
Eric Christopher6b6061f2013-01-16 01:22:23 +0000711 return getDIE(Context);
712}
713
Devang Patel161b2f42011-04-12 23:21:44 +0000714/// addToContextOwner - Add Die into the list of its context owner's children.
715void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
Eric Christopher6b6061f2013-01-16 01:22:23 +0000716 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
Devang Patel161b2f42011-04-12 23:21:44 +0000717 ContextDIE->addChild(Die);
718 else
719 addDie(Die);
720}
721
722/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
723/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000724DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
725 DIType Ty(TyNode);
726 if (!Ty.Verify())
727 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000728 DIE *TyDIE = getDIE(Ty);
729 if (TyDIE)
730 return TyDIE;
731
732 // Create new type.
733 TyDIE = new DIE(dwarf::DW_TAG_base_type);
734 insertDIE(Ty, TyDIE);
735 if (Ty.isBasicType())
736 constructTypeDIE(*TyDIE, DIBasicType(Ty));
737 else if (Ty.isCompositeType())
738 constructTypeDIE(*TyDIE, DICompositeType(Ty));
739 else {
740 assert(Ty.isDerivedType() && "Unknown kind of DIType");
741 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
742 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000743 // If this is a named finished type then include it in the list of types
744 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000745 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
746 bool IsImplementation = 0;
747 if (Ty.isCompositeType()) {
748 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000749 // A runtime language of 0 actually means C/C++ and that any
750 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000751 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000752 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000753 }
Eric Christophere0167892012-01-06 23:03:37 +0000754 unsigned Flags = IsImplementation ?
755 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
756 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000757 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000758
Devang Patel161b2f42011-04-12 23:21:44 +0000759 addToContextOwner(TyDIE, Ty.getContext());
760 return TyDIE;
761}
762
763/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000764void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000765 if (!Ty.Verify())
766 return;
767
768 // Check for pre-existence.
769 DIEEntry *Entry = getDIEEntry(Ty);
770 // If it exists then use the existing value.
771 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000772 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000773 return;
774 }
775
776 // Construct type.
777 DIE *Buffer = getOrCreateTypeDIE(Ty);
778
779 // Set up proxy.
780 Entry = createDIEEntry(Buffer);
781 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000782 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000783
784 // If this is a complete composite type then include it in the
785 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000786 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000787}
788
789/// addGlobalType - Add a new global type to the compile unit.
790///
Devang Patelc20bdf12011-06-01 00:23:24 +0000791void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000792 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000793 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
794 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000795 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000796 if (DIEEntry *Entry = getDIEEntry(Ty))
797 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000798}
799
Devang Patel31c5d052011-05-06 16:57:54 +0000800/// addPubTypes - Add type for pubtypes section.
801void CompileUnit::addPubTypes(DISubprogram SP) {
802 DICompositeType SPTy = SP.getType();
803 unsigned SPTag = SPTy.getTag();
804 if (SPTag != dwarf::DW_TAG_subroutine_type)
805 return;
806
807 DIArray Args = SPTy.getTypeArray();
808 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
809 DIType ATy(Args.getElement(i));
810 if (!ATy.Verify())
811 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000812 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000813 }
814}
815
Devang Patel161b2f42011-04-12 23:21:44 +0000816/// constructTypeDIE - Construct basic type die from DIBasicType.
817void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
818 // Get core information.
819 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000820 // Add name if not anonymous or intermediate type.
821 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000822 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000823
824 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
825 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
826 // Unspecified types has only name, nothing else.
827 return;
828 }
829
830 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000831 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000832 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000833
Devang Patel161b2f42011-04-12 23:21:44 +0000834 uint64_t Size = BTy.getSizeInBits() >> 3;
835 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
836}
837
838/// constructTypeDIE - Construct derived type die from DIDerivedType.
839void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
840 // Get core information.
841 StringRef Name = DTy.getName();
842 uint64_t Size = DTy.getSizeInBits() >> 3;
843 unsigned Tag = DTy.getTag();
844
845 // FIXME - Workaround for templates.
846 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
847
848 Buffer.setTag(Tag);
849
850 // Map to main type, void will not have a type.
851 DIType FromTy = DTy.getTypeDerivedFrom();
852 addType(&Buffer, FromTy);
853
854 // Add name if not anonymous or intermediate type.
855 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000856 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000857
858 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000859 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000860 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
861
David Blaikie62fdfb52013-01-07 05:51:15 +0000862 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
863 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
864 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000865 // Add source line info if available and TyDesc is not a forward declaration.
866 if (!DTy.isForwardDecl())
867 addSourceLine(&Buffer, DTy);
868}
869
870/// constructTypeDIE - Construct type DIE from DICompositeType.
871void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
872 // Get core information.
873 StringRef Name = CTy.getName();
874
875 uint64_t Size = CTy.getSizeInBits() >> 3;
876 unsigned Tag = CTy.getTag();
877 Buffer.setTag(Tag);
878
879 switch (Tag) {
Devang Patel161b2f42011-04-12 23:21:44 +0000880 case dwarf::DW_TAG_array_type:
881 constructArrayTypeDIE(Buffer, &CTy);
882 break;
883 case dwarf::DW_TAG_enumeration_type: {
884 DIArray Elements = CTy.getTypeArray();
885
886 // Add enumerators to enumeration type.
887 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
888 DIE *ElemDie = NULL;
889 DIDescriptor Enum(Elements.getElement(i));
890 if (Enum.isEnumerator()) {
891 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
892 Buffer.addChild(ElemDie);
893 }
894 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000895 DIType DTy = CTy.getTypeDerivedFrom();
896 if (DTy.Verify()) {
897 addType(&Buffer, DTy);
898 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
899 }
Devang Patel161b2f42011-04-12 23:21:44 +0000900 }
901 break;
902 case dwarf::DW_TAG_subroutine_type: {
903 // Add return type.
904 DIArray Elements = CTy.getTypeArray();
905 DIDescriptor RTy = Elements.getElement(0);
906 addType(&Buffer, DIType(RTy));
907
908 bool isPrototyped = true;
909 // Add arguments.
910 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
911 DIDescriptor Ty = Elements.getElement(i);
912 if (Ty.isUnspecifiedParameter()) {
913 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
914 Buffer.addChild(Arg);
915 isPrototyped = false;
916 } else {
917 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
918 addType(Arg, DIType(Ty));
David Blaikieaaf2e632013-01-29 19:35:24 +0000919 if (DIType(Ty).isArtificial())
920 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Patel161b2f42011-04-12 23:21:44 +0000921 Buffer.addChild(Arg);
922 }
923 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000924 // Add prototype flag if we're dealing with a C language and the
925 // function has been prototyped.
926 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000927 (Language == dwarf::DW_LANG_C89 ||
928 Language == dwarf::DW_LANG_C99 ||
929 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000930 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000931 }
932 break;
933 case dwarf::DW_TAG_structure_type:
934 case dwarf::DW_TAG_union_type:
935 case dwarf::DW_TAG_class_type: {
936 // Add elements to structure type.
937 DIArray Elements = CTy.getTypeArray();
938
939 // A forward struct declared type may not have elements available.
940 unsigned N = Elements.getNumElements();
941 if (N == 0)
942 break;
943
944 // Add elements to structure type.
945 for (unsigned i = 0; i < N; ++i) {
946 DIDescriptor Element = Elements.getElement(i);
947 DIE *ElemDie = NULL;
948 if (Element.isSubprogram()) {
949 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000950 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000951 if (SP.isProtected())
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_protected);
954 else if (SP.isPrivate())
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_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000957 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000958 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000959 dwarf::DW_ACCESS_public);
960 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000961 addFlag(ElemDie, dwarf::DW_AT_explicit);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000962 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000963 DIDerivedType DDTy(Element);
964 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
965 ElemDie = new DIE(dwarf::DW_TAG_friend);
966 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
Eric Christopher6b6061f2013-01-16 01:22:23 +0000967 } else if (DDTy.isStaticMember())
968 ElemDie = createStaticMemberDIE(DDTy);
969 else
970 ElemDie = createMemberDIE(DDTy);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000971 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000972 DIObjCProperty Property(Element);
973 ElemDie = new DIE(Property.getTag());
974 StringRef PropertyName = Property.getObjCPropertyName();
975 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000976 addType(ElemDie, Property.getType());
977 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000978 StringRef GetterName = Property.getObjCPropertyGetterName();
979 if (!GetterName.empty())
980 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
981 StringRef SetterName = Property.getObjCPropertySetterName();
982 if (!SetterName.empty())
983 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
984 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000985 if (Property.isReadOnlyObjCProperty())
986 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
987 if (Property.isReadWriteObjCProperty())
988 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
989 if (Property.isAssignObjCProperty())
990 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
991 if (Property.isRetainObjCProperty())
992 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
993 if (Property.isCopyObjCProperty())
994 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
995 if (Property.isNonAtomicObjCProperty())
996 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
997 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000998 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000999 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +00001000
Devang Patel30d409c2012-02-07 23:33:58 +00001001 DIEEntry *Entry = getDIEEntry(Element);
1002 if (!Entry) {
1003 Entry = createDIEEntry(ElemDie);
1004 insertDIEEntry(Element, Entry);
1005 }
Devang Patel9e11eb12012-02-04 01:30:32 +00001006 } else
Devang Patel161b2f42011-04-12 23:21:44 +00001007 continue;
1008 Buffer.addChild(ElemDie);
1009 }
1010
1011 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001012 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +00001013
Devang Patel161b2f42011-04-12 23:21:44 +00001014 DICompositeType ContainingType = CTy.getContainingType();
1015 if (DIDescriptor(ContainingType).isCompositeType())
1016 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1017 getOrCreateTypeDIE(DIType(ContainingType)));
1018 else {
1019 DIDescriptor Context = CTy.getContext();
1020 addToContextOwner(&Buffer, Context);
1021 }
1022
Devang Patel201e6cd2011-05-12 21:29:42 +00001023 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001024 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +00001025
Eric Christopher1a8e8862011-12-16 23:42:42 +00001026 // Add template parameters to a class, structure or union types.
1027 // FIXME: The support isn't in the metadata for this yet.
1028 if (Tag == dwarf::DW_TAG_class_type ||
1029 Tag == dwarf::DW_TAG_structure_type ||
1030 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +00001031 addTemplateParams(Buffer, CTy.getTemplateParams());
1032
1033 break;
1034 }
1035 default:
1036 break;
1037 }
1038
1039 // Add name if not anonymous or intermediate type.
1040 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001041 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001042
Eric Christopher4a5d8392012-05-22 18:45:18 +00001043 if (Tag == dwarf::DW_TAG_enumeration_type ||
1044 Tag == dwarf::DW_TAG_class_type ||
1045 Tag == dwarf::DW_TAG_structure_type ||
1046 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +00001047 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +00001048 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +00001049 if (Size)
1050 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +00001051 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +00001052 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +00001053 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1054
1055 // If we're a forward decl, say so.
1056 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001057 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +00001058
1059 // Add source line info if available.
1060 if (!CTy.isForwardDecl())
1061 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +00001062
1063 // No harm in adding the runtime language to the declaration.
1064 unsigned RLang = CTy.getRunTimeLang();
1065 if (RLang)
1066 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1067 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +00001068 }
1069}
1070
Eric Christopher8b4310b2012-11-21 00:34:38 +00001071/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001072/// for the given DITemplateTypeParameter.
1073DIE *
1074CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1075 DIE *ParamDIE = getDIE(TP);
1076 if (ParamDIE)
1077 return ParamDIE;
1078
1079 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1080 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001081 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001082 return ParamDIE;
1083}
1084
Eric Christopher8b4310b2012-11-21 00:34:38 +00001085/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001086/// for the given DITemplateValueParameter.
1087DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001088CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001089 DIE *ParamDIE = getDIE(TPV);
1090 if (ParamDIE)
1091 return ParamDIE;
1092
1093 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1094 addType(ParamDIE, TPV.getType());
1095 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001096 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001097 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +00001098 TPV.getValue());
1099 return ParamDIE;
1100}
1101
Devang Patel31c5d052011-05-06 16:57:54 +00001102/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1103DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1104 DIE *NDie = getDIE(NS);
1105 if (NDie)
1106 return NDie;
1107 NDie = new DIE(dwarf::DW_TAG_namespace);
1108 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001109 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001110 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001111 addAccelNamespace(NS.getName(), NDie);
1112 } else
1113 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001114 addSourceLine(NDie, NS);
1115 addToContextOwner(NDie, NS.getContext());
1116 return NDie;
1117}
1118
Devang Pateldbc64af2011-08-15 17:24:54 +00001119/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1120/// printer to not emit usual symbol prefix before the symbol name is used then
1121/// return linkage name after skipping this special LLVM prefix.
1122static StringRef getRealLinkageName(StringRef LinkageName) {
1123 char One = '\1';
1124 if (LinkageName.startswith(StringRef(&One, 1)))
1125 return LinkageName.substr(1);
1126 return LinkageName;
1127}
1128
1129/// getOrCreateSubprogramDIE - Create new DIE using SP.
1130DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1131 DIE *SPDie = getDIE(SP);
1132 if (SPDie)
1133 return SPDie;
1134
Peter Collingbourne27302f02012-05-27 18:36:44 +00001135 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1136
1137 // DW_TAG_inlined_subroutine may refer to this DIE.
1138 insertDIE(SP, SPDie);
1139
Rafael Espindola01b55b42011-11-10 22:34:29 +00001140 DISubprogram SPDecl = SP.getFunctionDeclaration();
1141 DIE *DeclDie = NULL;
1142 if (SPDecl.isSubprogram()) {
1143 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1144 }
1145
Devang Pateldbc64af2011-08-15 17:24:54 +00001146 // Add to context owner.
1147 addToContextOwner(SPDie, SP.getContext());
1148
1149 // Add function template parameters.
1150 addTemplateParams(*SPDie, SP.getTemplateParams());
1151
Eric Christophere9722e12012-04-16 23:54:23 +00001152 // Unfortunately this code needs to stay here instead of below the
1153 // AT_specification code in order to work around a bug in older
1154 // gdbs that requires the linkage name to resolve multiple template
1155 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001156 // TODO: Remove this set of code when we get rid of the old gdb
1157 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001158 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001159 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001160 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1161 getRealLinkageName(LinkageName));
1162
Devang Pateldbc64af2011-08-15 17:24:54 +00001163 // If this DIE is going to refer declaration info using AT_specification
1164 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001165 if (DeclDie) {
1166 // Refer function declaration directly.
1167 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1168 DeclDie);
1169
Devang Pateldbc64af2011-08-15 17:24:54 +00001170 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001171 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001172
Eric Christophercbbd5b12012-08-23 22:52:55 +00001173 // Add the linkage name if we have one.
1174 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1175 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1176 getRealLinkageName(LinkageName));
1177
Devang Pateldbc64af2011-08-15 17:24:54 +00001178 // Constructors and operators for anonymous aggregates do not have names.
1179 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001180 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001181
1182 addSourceLine(SPDie, SP);
1183
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001184 // Add the prototype if we have a prototype and we have a C like
1185 // language.
1186 if (SP.isPrototyped() &&
1187 (Language == dwarf::DW_LANG_C89 ||
1188 Language == dwarf::DW_LANG_C99 ||
1189 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001190 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001191
1192 // Add Return Type.
1193 DICompositeType SPTy = SP.getType();
1194 DIArray Args = SPTy.getTypeArray();
1195 unsigned SPTag = SPTy.getTag();
1196
1197 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1198 addType(SPDie, SPTy);
1199 else
1200 addType(SPDie, DIType(Args.getElement(0)));
1201
1202 unsigned VK = SP.getVirtuality();
1203 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001204 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001205 DIEBlock *Block = getDIEBlock();
1206 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1207 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1208 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1209 ContainingTypeMap.insert(std::make_pair(SPDie,
1210 SP.getContainingType()));
1211 }
1212
1213 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001214 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001215
Devang Pateldbc64af2011-08-15 17:24:54 +00001216 // Add arguments. Do not add arguments for subprogram definition. They will
1217 // be handled while processing variables.
1218 DICompositeType SPTy = SP.getType();
1219 DIArray Args = SPTy.getTypeArray();
1220 unsigned SPTag = SPTy.getTag();
1221
1222 if (SPTag == dwarf::DW_TAG_subroutine_type)
1223 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1224 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001225 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001226 addType(Arg, ATy);
1227 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001228 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001229 SPDie->addChild(Arg);
1230 }
1231 }
1232
1233 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001234 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001235
1236 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001237 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001238
1239 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001240 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001241
1242 if (unsigned isa = Asm->getISAEncoding()) {
1243 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1244 }
1245
1246 return SPDie;
1247}
1248
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001249// Return const expression if value is a GEP to access merged global
1250// constant. e.g.
1251// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1252static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1253 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1254 if (!CE || CE->getNumOperands() != 3 ||
1255 CE->getOpcode() != Instruction::GetElementPtr)
1256 return NULL;
1257
1258 // First operand points to a global struct.
1259 Value *Ptr = CE->getOperand(0);
1260 if (!isa<GlobalValue>(Ptr) ||
1261 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1262 return NULL;
1263
1264 // Second operand is zero.
1265 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1266 if (!CI || !CI->isZero())
1267 return NULL;
1268
1269 // Third operand is offset.
1270 if (!isa<ConstantInt>(CE->getOperand(2)))
1271 return NULL;
1272
1273 return CE;
1274}
1275
1276/// createGlobalVariableDIE - create global variable DIE.
1277void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001278 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001279 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001280 return;
1281
Devang Patel49e2f032011-08-18 22:21:50 +00001282 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001283 if (!GV.Verify())
1284 return;
1285
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001286 DIDescriptor GVContext = GV.getContext();
Eric Christopher6b6061f2013-01-16 01:22:23 +00001287 DIType GTy = GV.getType();
1288
1289 // If this is a static data member definition, some attributes belong
1290 // to the declaration DIE.
1291 DIE *VariableDIE = NULL;
Manman Ren945e8282013-02-01 23:54:37 +00001292 bool IsStaticMember = false;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001293 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1294 if (SDMDecl.Verify()) {
1295 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1296 // We need the declaration DIE that is in the static member's class.
1297 // But that class might not exist in the DWARF yet.
1298 // Creating the class will create the static member decl DIE.
1299 getOrCreateContextDIE(SDMDecl.getContext());
1300 VariableDIE = getDIE(SDMDecl);
1301 assert(VariableDIE && "Static member decl has no context?");
Manman Ren945e8282013-02-01 23:54:37 +00001302 IsStaticMember = true;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001303 }
1304
1305 // If this is not a static data member definition, create the variable
1306 // DIE and add the initial set of attributes to it.
1307 if (!VariableDIE) {
1308 VariableDIE = new DIE(GV.getTag());
1309 // Add to map.
1310 insertDIE(N, VariableDIE);
1311
1312 // Add name and type.
1313 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1314 addType(VariableDIE, GTy);
1315
1316 // Add scoping info.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001317 if (!GV.isLocalToUnit()) {
Eric Christopher6b6061f2013-01-16 01:22:23 +00001318 addFlag(VariableDIE, dwarf::DW_AT_external);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001319 addGlobalName(GV.getName(), VariableDIE);
1320 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001321
1322 // Add line number info.
1323 addSourceLine(VariableDIE, GV);
1324 // Add to context owner.
1325 addToContextOwner(VariableDIE, GVContext);
1326 }
1327
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001328 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001329 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001330 DIE *VariableSpecDIE = NULL;
Eric Christopher6b6061f2013-01-16 01:22:23 +00001331 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001332 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001333 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001334 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Eric Christopher0969ddf2013-01-18 22:11:33 +00001335 addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal()));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001336 // Do not create specification DIE if context is either compile unit
1337 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001338 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001339 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1340 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001341 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001342 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1343 dwarf::DW_FORM_ref4, VariableDIE);
1344 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001345 // A static member's declaration is already flagged as such.
1346 if (!SDMDecl.Verify())
1347 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001348 addDie(VariableSpecDIE);
1349 } else {
1350 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001351 }
Eric Christopher6b6061f2013-01-16 01:22:23 +00001352 // Add linkage name.
1353 StringRef LinkageName = GV.getLinkageName();
Eric Christophera8ada252013-02-27 23:49:50 +00001354 if (!LinkageName.empty()) {
Eric Christopher8d45a982013-02-27 23:49:47 +00001355 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1356 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1357 // TAG_variable.
Manman Ren21a08a12013-02-27 23:21:02 +00001358 addString(IsStaticMember && VariableSpecDIE ?
1359 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Eric Christopher6b6061f2013-01-16 01:22:23 +00001360 getRealLinkageName(LinkageName));
Eric Christopher8d45a982013-02-27 23:49:47 +00001361 // In compatibility mode with older gdbs we put the linkage name on both
1362 // the TAG_variable DIE and on the TAG_member DIE.
Manman Ren21a08a12013-02-27 23:21:02 +00001363 if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat())
1364 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Manman Ren06df83c2013-02-27 00:02:32 +00001365 getRealLinkageName(LinkageName));
1366 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001367 } else if (const ConstantInt *CI =
Manman Ren945e8282013-02-01 23:54:37 +00001368 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
Adrian Prantl2e892e42013-04-04 22:56:49 +00001369 // AT_const_value was added when the static member was created. To avoid
Manman Ren945e8282013-02-01 23:54:37 +00001370 // emitting AT_const_value multiple times, we only add AT_const_value when
1371 // it is not a static member.
1372 if (!IsStaticMember)
1373 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1374 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001375 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001376 // GV is a merged global.
1377 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1378 Value *Ptr = CE->getOperand(0);
Eric Christopher0969ddf2013-01-18 22:11:33 +00001379 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001380 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1381 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001382 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001383 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001384 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1385 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1386 }
1387
Eric Christopherd117fbb2011-11-11 01:55:22 +00001388 if (addToAccelTable) {
1389 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1390 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001391
Eric Christopherd117fbb2011-11-11 01:55:22 +00001392 // If the linkage name is different than the name, go ahead and output
1393 // that as well into the name table.
1394 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1395 addAccelName(GV.getLinkageName(), AddrDIE);
1396 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001397
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001398 return;
1399}
1400
Devang Patel161b2f42011-04-12 23:21:44 +00001401/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001402void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1403 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001404 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1405 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001406
Bill Wendling222c2fd2012-12-06 07:38:10 +00001407 // The LowerBound value defines the lower bounds which is typically zero for
1408 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1409 // Count == -1 then the array is unbounded and we do not emit
1410 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1411 // Count == 0, then the array has zero elements in which case we do not emit
1412 // an upper bound.
1413 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001414 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001415 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001416
Bill Wendling6afe4782012-12-06 07:55:19 +00001417 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001418 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1419
1420 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001421 // FIXME: An unbounded array should reference the expression that defines
1422 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001423 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001424
Devang Patel161b2f42011-04-12 23:21:44 +00001425 Buffer.addChild(DW_Subrange);
1426}
1427
1428/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1429void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1430 DICompositeType *CTy) {
1431 Buffer.setTag(dwarf::DW_TAG_array_type);
Eric Christopher9a1e0e22013-01-08 01:53:52 +00001432 if (CTy->isVector())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001433 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001434
1435 // Emit derived type.
1436 addType(&Buffer, CTy->getTypeDerivedFrom());
1437 DIArray Elements = CTy->getTypeArray();
1438
1439 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001440 // FIXME: This type should be passed down from the front end
1441 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001442 DIE *IdxTy = getIndexTyDie();
1443 if (!IdxTy) {
1444 // Construct an anonymous type for index type.
1445 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001446 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001447 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1448 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1449 dwarf::DW_ATE_signed);
1450 addDie(IdxTy);
1451 setIndexTyDie(IdxTy);
1452 }
1453
1454 // Add subranges to array type.
1455 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1456 DIDescriptor Element = Elements.getElement(i);
1457 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1458 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1459 }
1460}
1461
1462/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1463DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1464 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1465 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001466 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001467 int64_t Value = ETy.getEnumValue();
1468 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1469 return Enumerator;
1470}
1471
Devang Pateldbc64af2011-08-15 17:24:54 +00001472/// constructContainingTypeDIEs - Construct DIEs for types that contain
1473/// vtables.
1474void CompileUnit::constructContainingTypeDIEs() {
1475 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1476 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1477 DIE *SPDie = CI->first;
1478 const MDNode *N = CI->second;
1479 if (!N) continue;
1480 DIE *NDie = getDIE(N);
1481 if (!NDie) continue;
1482 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1483 }
1484}
1485
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001486/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1487DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1488 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001489
1490 // Translate tag to proper Dwarf tag.
1491 unsigned Tag = DV->getTag();
1492
1493 // Define variable debug information entry.
1494 DIE *VariableDie = new DIE(Tag);
1495 DbgVariable *AbsVar = DV->getAbstractVariable();
1496 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1497 if (AbsDIE)
1498 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1499 dwarf::DW_FORM_ref4, AbsDIE);
1500 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001501 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001502 addSourceLine(VariableDie, DV->getVariable());
1503 addType(VariableDie, DV->getType());
1504 }
1505
1506 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001507 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001508
1509 if (isScopeAbstract) {
1510 DV->setDIE(VariableDie);
1511 return VariableDie;
1512 }
1513
1514 // Add variable address.
1515
1516 unsigned Offset = DV->getDotDebugLocOffset();
1517 if (Offset != ~0U) {
1518 addLabel(VariableDie, dwarf::DW_AT_location,
1519 dwarf::DW_FORM_data4,
1520 Asm->GetTempSymbol("debug_loc", Offset));
1521 DV->setDIE(VariableDie);
1522 return VariableDie;
1523 }
1524
Eric Christopher8cf5e742011-10-03 15:49:20 +00001525 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001526 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1527 bool updated = false;
1528 if (DVInsn->getNumOperands() == 3) {
1529 if (DVInsn->getOperand(0).isReg()) {
1530 const MachineOperand RegOp = DVInsn->getOperand(0);
1531 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1532 if (DVInsn->getOperand(1).isImm() &&
1533 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1534 unsigned FrameReg = 0;
1535 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001536 int Offset =
1537 TFI->getFrameIndexReference(*Asm->MF,
1538 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001539 FrameReg);
1540 MachineLocation Location(FrameReg, Offset);
1541 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001542
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001543 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001544 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001545 MachineLocation(RegOp.getReg()));
1546 updated = true;
1547 }
1548 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001549 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001550 addConstantValue(VariableDie, DVInsn->getOperand(0),
1551 DV->getType());
1552 else if (DVInsn->getOperand(0).isFPImm())
1553 updated =
1554 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1555 else if (DVInsn->getOperand(0).isCImm())
1556 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001557 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001558 DVInsn->getOperand(0).getCImm(),
1559 DV->getType().isUnsignedDIType());
1560 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001561 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001562 Asm->getDebugValueLocation(DVInsn));
1563 updated = true;
1564 }
1565 if (!updated) {
1566 // If variableDie is not updated then DBG_VALUE instruction does not
1567 // have valid variable info.
1568 delete VariableDie;
1569 return NULL;
1570 }
1571 DV->setDIE(VariableDie);
1572 return VariableDie;
1573 } else {
1574 // .. else use frame index.
1575 int FI = DV->getFrameIndex();
1576 if (FI != ~0) {
1577 unsigned FrameReg = 0;
1578 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001579 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001580 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1581 MachineLocation Location(FrameReg, Offset);
1582 addVariableAddress(DV, VariableDie, Location);
1583 }
1584 }
1585
1586 DV->setDIE(VariableDie);
1587 return VariableDie;
1588}
1589
Devang Patel161b2f42011-04-12 23:21:44 +00001590/// createMemberDIE - Create new member DIE.
1591DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1592 DIE *MemberDie = new DIE(DT.getTag());
1593 StringRef Name = DT.getName();
1594 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001595 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001596
1597 addType(MemberDie, DT.getTypeDerivedFrom());
1598
1599 addSourceLine(MemberDie, DT);
1600
1601 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1602 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1603
1604 uint64_t Size = DT.getSizeInBits();
1605 uint64_t FieldSize = DT.getOriginalTypeSize();
1606
1607 if (Size != FieldSize) {
1608 // Handle bitfield.
1609 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1610 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1611
1612 uint64_t Offset = DT.getOffsetInBits();
1613 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1614 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1615 uint64_t FieldOffset = (HiMark - FieldSize);
1616 Offset -= FieldOffset;
1617
1618 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001619 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001620 Offset = FieldSize - (Offset + Size);
1621 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1622
1623 // Here WD_AT_data_member_location points to the anonymous
1624 // field that includes this bit field.
1625 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1626
1627 } else
1628 // This is not a bitfield.
1629 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1630
1631 if (DT.getTag() == dwarf::DW_TAG_inheritance
1632 && DT.isVirtual()) {
1633
1634 // For C++, virtual base classes are not at fixed offset. Use following
1635 // expression to extract appropriate offset from vtable.
1636 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1637
1638 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1639 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1640 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1641 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1642 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1643 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1644 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1645 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1646
1647 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1648 VBaseLocationDie);
1649 } else
1650 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1651
1652 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001653 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001654 dwarf::DW_ACCESS_protected);
1655 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001656 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001657 dwarf::DW_ACCESS_private);
1658 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001659 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001660 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001661 dwarf::DW_ACCESS_public);
1662 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001663 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001664 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001665
1666 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001667 if (MDNode *PNode = DT.getObjCProperty())
1668 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001669 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001670 PropertyDie);
1671
David Blaikie01bc2b32012-12-13 22:43:07 +00001672 if (DT.isArtificial())
1673 addFlag(MemberDie, dwarf::DW_AT_artificial);
1674
Devang Patel161b2f42011-04-12 23:21:44 +00001675 return MemberDie;
1676}
Eric Christopher6b6061f2013-01-16 01:22:23 +00001677
1678/// createStaticMemberDIE - Create new DIE for C++ static member.
1679DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1680 if (!DT.Verify())
1681 return NULL;
1682
1683 DIE *StaticMemberDIE = new DIE(DT.getTag());
1684 DIType Ty = DT.getTypeDerivedFrom();
1685
1686 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1687 addType(StaticMemberDIE, Ty);
1688 addSourceLine(StaticMemberDIE, DT);
1689 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1690 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1691
1692 // FIXME: We could omit private if the parent is a class_type, and
1693 // public if the parent is something else.
1694 if (DT.isProtected())
1695 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1696 dwarf::DW_ACCESS_protected);
1697 else if (DT.isPrivate())
1698 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1699 dwarf::DW_ACCESS_private);
1700 else
1701 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1702 dwarf::DW_ACCESS_public);
1703
1704 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1705 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
David Blaikie14268412013-01-20 01:18:01 +00001706 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1707 addConstantFPValue(StaticMemberDIE, CFP);
Eric Christopher6b6061f2013-01-16 01:22:23 +00001708
1709 insertDIE(DT, StaticMemberDIE);
1710 return StaticMemberDIE;
1711}