blob: 255ae1ec500c013fbf8692683c5d495381c7bce1 [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"
Devang Patel161b2f42011-04-12 23:21:44 +000020#include "llvm/Constants.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000021#include "llvm/DIBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/DataLayout.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000023#include "llvm/GlobalVariable.h"
24#include "llvm/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 Christopher4d069bf2012-05-22 18:45:24 +000036 DwarfDebug *DW)
Eli Benderskyd4a05e02012-12-03 18:45:45 +000037 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000038 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
39}
40
41/// ~CompileUnit - Destructor for compile unit.
42CompileUnit::~CompileUnit() {
43 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
44 DIEBlocks[j]->~DIEBlock();
45}
46
47/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
48/// information entry.
49DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
50 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
51 return Value;
52}
53
Bill Wendling222c2fd2012-12-06 07:38:10 +000054/// getLowerBoundDefault - Return the default lower bound for an array. If the
55/// DWARF version doesn't handle the language, return -1.
56int64_t CompileUnit::getLowerBoundDefault() const {
57 switch (Language) {
58 default:
59 break;
60
61 case dwarf::DW_LANG_C89:
62 case dwarf::DW_LANG_C99:
63 case dwarf::DW_LANG_C:
64 case dwarf::DW_LANG_C_plus_plus:
65 case dwarf::DW_LANG_ObjC:
66 case dwarf::DW_LANG_ObjC_plus_plus:
67 return 0;
68
69 case dwarf::DW_LANG_Fortran77:
70 case dwarf::DW_LANG_Fortran90:
71 case dwarf::DW_LANG_Fortran95:
72 return 1;
73
74 // The languages below have valid values only if the DWARF version >= 4.
75 case dwarf::DW_LANG_Java:
76 case dwarf::DW_LANG_Python:
77 case dwarf::DW_LANG_UPC:
78 case dwarf::DW_LANG_D:
79 if (dwarf::DWARF_VERSION >= 4)
80 return 0;
81 break;
82
83 case dwarf::DW_LANG_Ada83:
84 case dwarf::DW_LANG_Ada95:
85 case dwarf::DW_LANG_Cobol74:
86 case dwarf::DW_LANG_Cobol85:
87 case dwarf::DW_LANG_Modula2:
88 case dwarf::DW_LANG_Pascal83:
89 case dwarf::DW_LANG_PLI:
90 if (dwarf::DWARF_VERSION >= 4)
91 return 1;
92 break;
93 }
94
95 return -1;
96}
97
Eric Christopher873cf0a2012-08-24 01:14:27 +000098/// addFlag - Add a flag that is true.
99void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
100 if (!DD->useDarwinGDBCompat())
101 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
102 DIEIntegerOne);
103 else
104 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
105}
106
Devang Patel161b2f42011-04-12 23:21:44 +0000107/// addUInt - Add an unsigned integer attribute data and value.
108///
109void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
110 unsigned Form, uint64_t Integer) {
111 if (!Form) Form = DIEInteger::BestForm(false, Integer);
112 DIEValue *Value = Integer == 1 ?
113 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
114 Die->addValue(Attribute, Form, Value);
115}
116
117/// addSInt - Add an signed integer attribute data and value.
118///
119void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
120 unsigned Form, int64_t Integer) {
121 if (!Form) Form = DIEInteger::BestForm(true, Integer);
122 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
123 Die->addValue(Attribute, Form, Value);
124}
125
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000126/// addString - Add a string attribute data and value. We always emit a
127/// reference to the string pool instead of immediate strings so that DIEs have
128/// more predictable sizes.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000129void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000130 MCSymbol *Symb = DD->getStringPoolEntry(String);
131 DIEValue *Value;
132 if (Asm->needsRelocationsForDwarfStringPool())
133 Value = new (DIEValueAllocator) DIELabel(Symb);
134 else {
135 MCSymbol *StringPool = DD->getStringPool();
136 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000137 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000138 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000139}
140
141/// addLabel - Add a Dwarf label attribute data and value.
142///
143void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
144 const MCSymbol *Label) {
145 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
146 Die->addValue(Attribute, Form, Value);
147}
148
149/// addDelta - Add a label delta attribute data and value.
150///
151void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
152 const MCSymbol *Hi, const MCSymbol *Lo) {
153 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
154 Die->addValue(Attribute, Form, Value);
155}
156
157/// addDIEEntry - Add a DIE attribute data and value.
158///
159void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
160 DIE *Entry) {
161 Die->addValue(Attribute, Form, createDIEEntry(Entry));
162}
163
Devang Patel161b2f42011-04-12 23:21:44 +0000164/// addBlock - Add block data.
165///
166void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
167 DIEBlock *Block) {
168 Block->ComputeSize(Asm);
169 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
170 Die->addValue(Attribute, Block->BestForm(), Block);
171}
172
173/// addSourceLine - Add location information to specified debug information
174/// entry.
175void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
176 // Verify variable.
177 if (!V.Verify())
178 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000179
Devang Patel161b2f42011-04-12 23:21:44 +0000180 unsigned Line = V.getLineNumber();
181 if (Line == 0)
182 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000183 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Devang Patel161b2f42011-04-12 23:21:44 +0000184 V.getContext().getDirectory());
185 assert(FileID && "Invalid file id");
186 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
187 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
188}
189
190/// addSourceLine - Add location information to specified debug information
191/// entry.
192void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
193 // Verify global variable.
194 if (!G.Verify())
195 return;
196
197 unsigned Line = G.getLineNumber();
198 if (Line == 0)
199 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000200 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000201 assert(FileID && "Invalid file id");
202 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
203 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
204}
205
206/// addSourceLine - Add location information to specified debug information
207/// entry.
208void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
209 // Verify subprogram.
210 if (!SP.Verify())
211 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000212
Devang Patel161b2f42011-04-12 23:21:44 +0000213 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000214 unsigned Line = SP.getLineNumber();
215 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000216 return;
217
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000218 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000219 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000220 assert(FileID && "Invalid file id");
221 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
222 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
223}
224
225/// addSourceLine - Add location information to specified debug information
226/// entry.
227void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
228 // Verify type.
229 if (!Ty.Verify())
230 return;
231
232 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000233 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000234 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000235 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000236 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000237 assert(FileID && "Invalid file id");
238 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
239 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
240}
241
242/// addSourceLine - Add location information to specified debug information
243/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000244void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
245 // Verify type.
246 if (!Ty.Verify())
247 return;
248
249 unsigned Line = Ty.getLineNumber();
250 if (Line == 0)
251 return;
252 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000253 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Eric Christopher4d069bf2012-05-22 18:45:24 +0000254 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000255 assert(FileID && "Invalid file id");
256 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
257 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
258}
259
260/// addSourceLine - Add location information to specified debug information
261/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000262void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
263 // Verify namespace.
264 if (!NS.Verify())
265 return;
266
267 unsigned Line = NS.getLineNumber();
268 if (Line == 0)
269 return;
270 StringRef FN = NS.getFilename();
271
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000272 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000273 assert(FileID && "Invalid file id");
274 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
275 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
276}
277
Eric Christopher8b4310b2012-11-21 00:34:38 +0000278/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000279/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000280void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000281 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000282 if (DV->variableHasComplexAddress())
283 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
284 else if (DV->isBlockByrefVariable())
285 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
286 else
287 addAddress(Die, dwarf::DW_AT_location, Location);
288}
289
Devang Patel116da2f2011-04-26 19:06:18 +0000290/// addRegisterOp - Add register operand.
291void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
292 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
293 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
294 if (DWReg < 32)
295 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
296 else {
297 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
298 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
299 }
300}
301
302/// addRegisterOffset - Add register offset.
303void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
304 int64_t Offset) {
305 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
306 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
307 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
308 if (Reg == TRI->getFrameRegister(*Asm->MF))
309 // If variable offset is based in frame register then use fbreg.
310 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
311 else if (DWReg < 32)
312 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
313 else {
314 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
315 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
316 }
317 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
318}
319
320/// addAddress - Add an address attribute to a die based on the location
321/// provided.
322void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
323 const MachineLocation &Location) {
324 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
325
326 if (Location.isReg())
327 addRegisterOp(Block, Location.getReg());
328 else
329 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
330
331 // Now attach the location information to the DIE.
332 addBlock(Die, Attribute, 0, Block);
333}
334
Devang Patel161b2f42011-04-12 23:21:44 +0000335/// addComplexAddress - Start with the address based on the location provided,
336/// and generate the DWARF information necessary to find the actual variable
337/// given the extra address information encoded in the DIVariable, starting from
338/// the starting location. Add the DWARF information to the die.
339///
340void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
341 unsigned Attribute,
342 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000343 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000344 unsigned N = DV->getNumAddrElements();
345 unsigned i = 0;
346 if (Location.isReg()) {
347 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
348 // If first address element is OpPlus then emit
349 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
350 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
351 i = 2;
352 } else
353 addRegisterOp(Block, Location.getReg());
354 }
Devang Patel116da2f2011-04-26 19:06:18 +0000355 else
356 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000357
Devang Patelc26f5442011-04-28 02:22:40 +0000358 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000359 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000360 if (Element == DIBuilder::OpPlus) {
361 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
362 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
363 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000364 if (!Location.isReg())
365 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000366 } else llvm_unreachable("unknown DIBuilder Opcode");
367 }
368
369 // Now attach the location information to the DIE.
370 addBlock(Die, Attribute, 0, Block);
371}
372
373/* Byref variables, in Blocks, are declared by the programmer as "SomeType
374 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
375 gives the variable VarName either the struct, or a pointer to the struct, as
376 its type. This is necessary for various behind-the-scenes things the
377 compiler needs to do with by-reference variables in Blocks.
378
379 However, as far as the original *programmer* is concerned, the variable
380 should still have type 'SomeType', as originally declared.
381
382 The function getBlockByrefType dives into the __Block_byref_x_VarName
383 struct to find the original type of the variable, which is then assigned to
384 the variable's Debug Information Entry as its real type. So far, so good.
385 However now the debugger will expect the variable VarName to have the type
386 SomeType. So we need the location attribute for the variable to be an
387 expression that explains to the debugger how to navigate through the
388 pointers and struct to find the actual variable of type SomeType.
389
390 The following function does just that. We start by getting
391 the "normal" location for the variable. This will be the location
392 of either the struct __Block_byref_x_VarName or the pointer to the
393 struct __Block_byref_x_VarName.
394
395 The struct will look something like:
396
397 struct __Block_byref_x_VarName {
398 ... <various fields>
399 struct __Block_byref_x_VarName *forwarding;
400 ... <various other fields>
401 SomeType VarName;
402 ... <maybe more fields>
403 };
404
405 If we are given the struct directly (as our starting point) we
406 need to tell the debugger to:
407
408 1). Add the offset of the forwarding field.
409
410 2). Follow that pointer to get the real __Block_byref_x_VarName
411 struct to use (the real one may have been copied onto the heap).
412
413 3). Add the offset for the field VarName, to find the actual variable.
414
415 If we started with a pointer to the struct, then we need to
416 dereference that pointer first, before the other steps.
417 Translating this into DWARF ops, we will need to append the following
418 to the current location description for the variable:
419
420 DW_OP_deref -- optional, if we start with a pointer
421 DW_OP_plus_uconst <forward_fld_offset>
422 DW_OP_deref
423 DW_OP_plus_uconst <varName_fld_offset>
424
425 That is what this function does. */
426
427/// addBlockByrefAddress - Start with the address based on the location
428/// provided, and generate the DWARF information necessary to find the
429/// actual Block variable (navigating the Block struct) based on the
430/// starting location. Add the DWARF information to the die. For
431/// more information, read large comment just above here.
432///
433void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
434 unsigned Attribute,
435 const MachineLocation &Location) {
436 DIType Ty = DV->getType();
437 DIType TmpTy = Ty;
438 unsigned Tag = Ty.getTag();
439 bool isPointer = false;
440
441 StringRef varName = DV->getName();
442
443 if (Tag == dwarf::DW_TAG_pointer_type) {
444 DIDerivedType DTy = DIDerivedType(Ty);
445 TmpTy = DTy.getTypeDerivedFrom();
446 isPointer = true;
447 }
448
449 DICompositeType blockStruct = DICompositeType(TmpTy);
450
451 // Find the __forwarding field and the variable field in the __Block_byref
452 // struct.
453 DIArray Fields = blockStruct.getTypeArray();
454 DIDescriptor varField = DIDescriptor();
455 DIDescriptor forwardingField = DIDescriptor();
456
457 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
458 DIDescriptor Element = Fields.getElement(i);
459 DIDerivedType DT = DIDerivedType(Element);
460 StringRef fieldName = DT.getName();
461 if (fieldName == "__forwarding")
462 forwardingField = Element;
463 else if (fieldName == varName)
464 varField = Element;
465 }
466
467 // Get the offsets for the forwarding field and the variable field.
468 unsigned forwardingFieldOffset =
469 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
470 unsigned varFieldOffset =
471 DIDerivedType(varField).getOffsetInBits() >> 3;
472
473 // Decode the original location, and use that as the start of the byref
474 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000475 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
476
Eric Christophercaba2632012-07-04 02:02:18 +0000477 if (Location.isReg())
478 addRegisterOp(Block, Location.getReg());
479 else
480 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000481
482 // If we started with a pointer to the __Block_byref... struct, then
483 // the first thing we need to do is dereference the pointer (DW_OP_deref).
484 if (isPointer)
485 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
486
487 // Next add the offset for the '__forwarding' field:
488 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
489 // adding the offset if it's 0.
490 if (forwardingFieldOffset > 0) {
491 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
492 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
493 }
494
495 // Now dereference the __forwarding field to get to the real __Block_byref
496 // struct: DW_OP_deref.
497 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
498
499 // Now that we've got the real __Block_byref... struct, add the offset
500 // for the variable's field to get to the location of the actual variable:
501 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
502 if (varFieldOffset > 0) {
503 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
504 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
505 }
506
507 // Now attach the location information to the DIE.
508 addBlock(Die, Attribute, 0, Block);
509}
510
Devang Patel4ec14b02011-07-20 21:57:04 +0000511/// isTypeSigned - Return true if the type is signed.
512static bool isTypeSigned(DIType Ty, int *SizeInBits) {
513 if (Ty.isDerivedType())
514 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
515 if (Ty.isBasicType())
516 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
517 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
518 *SizeInBits = Ty.getSizeInBits();
519 return true;
520 }
521 return false;
522}
523
Devang Patel161b2f42011-04-12 23:21:44 +0000524/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000525bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
526 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000527 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000528 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000529 int SizeInBits = -1;
530 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
531 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
532 switch (SizeInBits) {
533 case 8: Form = dwarf::DW_FORM_data1; break;
534 case 16: Form = dwarf::DW_FORM_data2; break;
535 case 32: Form = dwarf::DW_FORM_data4; break;
536 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000537 default: break;
538 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000539 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000540 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000541
Devang Patel161b2f42011-04-12 23:21:44 +0000542 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
543 return true;
544}
545
546/// addConstantFPValue - Add constant value entry in variable DIE.
547bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000548 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000549 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
550 APFloat FPImm = MO.getFPImm()->getValueAPF();
551
552 // Get the raw data form of the floating point.
553 const APInt FltVal = FPImm.bitcastToAPInt();
554 const char *FltPtr = (const char*)FltVal.getRawData();
555
556 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000557 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000558 int Incr = (LittleEndian ? 1 : -1);
559 int Start = (LittleEndian ? 0 : NumBytes - 1);
560 int Stop = (LittleEndian ? NumBytes : -1);
561
562 // Output the constant to DWARF one byte at a time.
563 for (; Start != Stop; Start += Incr)
564 addUInt(Block, 0, dwarf::DW_FORM_data1,
565 (unsigned char)0xFF & FltPtr[Start]);
566
567 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
568 return true;
569}
570
571/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000572bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000573 bool Unsigned) {
Devang Pateld6a81362011-05-28 00:39:18 +0000574 unsigned CIBitWidth = CI->getBitWidth();
575 if (CIBitWidth <= 64) {
576 unsigned form = 0;
577 switch (CIBitWidth) {
578 case 8: form = dwarf::DW_FORM_data1; break;
579 case 16: form = dwarf::DW_FORM_data2; break;
580 case 32: form = dwarf::DW_FORM_data4; break;
581 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000582 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000583 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
584 }
Devang Patel161b2f42011-04-12 23:21:44 +0000585 if (Unsigned)
Devang Pateld6a81362011-05-28 00:39:18 +0000586 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000587 else
Devang Pateld6a81362011-05-28 00:39:18 +0000588 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000589 return true;
590 }
591
592 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
593
594 // Get the raw data form of the large APInt.
595 const APInt Val = CI->getValue();
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000596 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000597
598 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000599 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000600
601 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000602 for (int i = 0; i < NumBytes; i++) {
603 uint8_t c;
604 if (LittleEndian)
605 c = Ptr64[i / 8] >> (8 * (i & 7));
606 else
607 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
608 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
609 }
Devang Patel161b2f42011-04-12 23:21:44 +0000610
611 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
612 return true;
613}
614
615/// addTemplateParams - Add template parameters in buffer.
616void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
617 // Add template parameters.
618 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
619 DIDescriptor Element = TParams.getElement(i);
620 if (Element.isTemplateTypeParameter())
621 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
622 DITemplateTypeParameter(Element)));
623 else if (Element.isTemplateValueParameter())
624 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
625 DITemplateValueParameter(Element)));
626 }
Devang Patel161b2f42011-04-12 23:21:44 +0000627}
Nick Lewycky746cb672011-10-26 22:55:33 +0000628
Devang Patel161b2f42011-04-12 23:21:44 +0000629/// addToContextOwner - Add Die into the list of its context owner's children.
630void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
631 if (Context.isType()) {
632 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
633 ContextDIE->addChild(Die);
634 } else if (Context.isNameSpace()) {
635 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
636 ContextDIE->addChild(Die);
637 } else if (Context.isSubprogram()) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000638 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
Devang Patel161b2f42011-04-12 23:21:44 +0000639 ContextDIE->addChild(Die);
640 } else if (DIE *ContextDIE = getDIE(Context))
641 ContextDIE->addChild(Die);
642 else
643 addDie(Die);
644}
645
646/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
647/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000648DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
649 DIType Ty(TyNode);
650 if (!Ty.Verify())
651 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000652 DIE *TyDIE = getDIE(Ty);
653 if (TyDIE)
654 return TyDIE;
655
656 // Create new type.
657 TyDIE = new DIE(dwarf::DW_TAG_base_type);
658 insertDIE(Ty, TyDIE);
659 if (Ty.isBasicType())
660 constructTypeDIE(*TyDIE, DIBasicType(Ty));
661 else if (Ty.isCompositeType())
662 constructTypeDIE(*TyDIE, DICompositeType(Ty));
663 else {
664 assert(Ty.isDerivedType() && "Unknown kind of DIType");
665 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
666 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000667 // If this is a named finished type then include it in the list of types
668 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000669 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
670 bool IsImplementation = 0;
671 if (Ty.isCompositeType()) {
672 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000673 // A runtime language of 0 actually means C/C++ and that any
674 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000675 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000676 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000677 }
Eric Christophere0167892012-01-06 23:03:37 +0000678 unsigned Flags = IsImplementation ?
679 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
680 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000681 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000682
Devang Patel161b2f42011-04-12 23:21:44 +0000683 addToContextOwner(TyDIE, Ty.getContext());
684 return TyDIE;
685}
686
687/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000688void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000689 if (!Ty.Verify())
690 return;
691
692 // Check for pre-existence.
693 DIEEntry *Entry = getDIEEntry(Ty);
694 // If it exists then use the existing value.
695 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000696 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000697 return;
698 }
699
700 // Construct type.
701 DIE *Buffer = getOrCreateTypeDIE(Ty);
702
703 // Set up proxy.
704 Entry = createDIEEntry(Buffer);
705 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000706 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000707
708 // If this is a complete composite type then include it in the
709 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000710 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000711}
712
713/// addGlobalType - Add a new global type to the compile unit.
714///
Devang Patelc20bdf12011-06-01 00:23:24 +0000715void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000716 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000717 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
718 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000719 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000720 if (DIEEntry *Entry = getDIEEntry(Ty))
721 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000722}
723
Devang Patel31c5d052011-05-06 16:57:54 +0000724/// addPubTypes - Add type for pubtypes section.
725void CompileUnit::addPubTypes(DISubprogram SP) {
726 DICompositeType SPTy = SP.getType();
727 unsigned SPTag = SPTy.getTag();
728 if (SPTag != dwarf::DW_TAG_subroutine_type)
729 return;
730
731 DIArray Args = SPTy.getTypeArray();
732 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
733 DIType ATy(Args.getElement(i));
734 if (!ATy.Verify())
735 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000736 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000737 }
738}
739
Devang Patel161b2f42011-04-12 23:21:44 +0000740/// constructTypeDIE - Construct basic type die from DIBasicType.
741void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
742 // Get core information.
743 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000744 // Add name if not anonymous or intermediate type.
745 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000746 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000747
748 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
749 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
750 // Unspecified types has only name, nothing else.
751 return;
752 }
753
754 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000755 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000756 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000757
Devang Patel161b2f42011-04-12 23:21:44 +0000758 uint64_t Size = BTy.getSizeInBits() >> 3;
759 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
760}
761
762/// constructTypeDIE - Construct derived type die from DIDerivedType.
763void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
764 // Get core information.
765 StringRef Name = DTy.getName();
766 uint64_t Size = DTy.getSizeInBits() >> 3;
767 unsigned Tag = DTy.getTag();
768
769 // FIXME - Workaround for templates.
770 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
771
772 Buffer.setTag(Tag);
773
774 // Map to main type, void will not have a type.
775 DIType FromTy = DTy.getTypeDerivedFrom();
776 addType(&Buffer, FromTy);
777
778 // Add name if not anonymous or intermediate type.
779 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000780 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000781
782 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000783 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000784 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
785
786 // Add source line info if available and TyDesc is not a forward declaration.
787 if (!DTy.isForwardDecl())
788 addSourceLine(&Buffer, DTy);
789}
790
791/// constructTypeDIE - Construct type DIE from DICompositeType.
792void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
793 // Get core information.
794 StringRef Name = CTy.getName();
795
796 uint64_t Size = CTy.getSizeInBits() >> 3;
797 unsigned Tag = CTy.getTag();
798 Buffer.setTag(Tag);
799
800 switch (Tag) {
801 case dwarf::DW_TAG_vector_type:
802 case dwarf::DW_TAG_array_type:
803 constructArrayTypeDIE(Buffer, &CTy);
804 break;
805 case dwarf::DW_TAG_enumeration_type: {
806 DIArray Elements = CTy.getTypeArray();
807
808 // Add enumerators to enumeration type.
809 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
810 DIE *ElemDie = NULL;
811 DIDescriptor Enum(Elements.getElement(i));
812 if (Enum.isEnumerator()) {
813 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
814 Buffer.addChild(ElemDie);
815 }
816 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000817 DIType DTy = CTy.getTypeDerivedFrom();
818 if (DTy.Verify()) {
819 addType(&Buffer, DTy);
820 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
821 }
Devang Patel161b2f42011-04-12 23:21:44 +0000822 }
823 break;
824 case dwarf::DW_TAG_subroutine_type: {
825 // Add return type.
826 DIArray Elements = CTy.getTypeArray();
827 DIDescriptor RTy = Elements.getElement(0);
828 addType(&Buffer, DIType(RTy));
829
830 bool isPrototyped = true;
831 // Add arguments.
832 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
833 DIDescriptor Ty = Elements.getElement(i);
834 if (Ty.isUnspecifiedParameter()) {
835 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
836 Buffer.addChild(Arg);
837 isPrototyped = false;
838 } else {
839 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
840 addType(Arg, DIType(Ty));
841 Buffer.addChild(Arg);
842 }
843 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000844 // Add prototype flag if we're dealing with a C language and the
845 // function has been prototyped.
846 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000847 (Language == dwarf::DW_LANG_C89 ||
848 Language == dwarf::DW_LANG_C99 ||
849 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000850 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000851 }
852 break;
853 case dwarf::DW_TAG_structure_type:
854 case dwarf::DW_TAG_union_type:
855 case dwarf::DW_TAG_class_type: {
856 // Add elements to structure type.
857 DIArray Elements = CTy.getTypeArray();
858
859 // A forward struct declared type may not have elements available.
860 unsigned N = Elements.getNumElements();
861 if (N == 0)
862 break;
863
864 // Add elements to structure type.
865 for (unsigned i = 0; i < N; ++i) {
866 DIDescriptor Element = Elements.getElement(i);
867 DIE *ElemDie = NULL;
868 if (Element.isSubprogram()) {
869 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000870 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000871 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000872 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000873 dwarf::DW_ACCESS_protected);
874 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000875 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000876 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000877 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000878 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000879 dwarf::DW_ACCESS_public);
880 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000881 addFlag(ElemDie, dwarf::DW_AT_explicit);
Devang Patel161b2f42011-04-12 23:21:44 +0000882 }
883 else if (Element.isVariable()) {
884 DIVariable DV(Element);
885 ElemDie = new DIE(dwarf::DW_TAG_variable);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000886 addString(ElemDie, dwarf::DW_AT_name, DV.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000887 addType(ElemDie, DV.getType());
Eric Christopher873cf0a2012-08-24 01:14:27 +0000888 addFlag(ElemDie, dwarf::DW_AT_declaration);
889 addFlag(ElemDie, dwarf::DW_AT_external);
Devang Patel161b2f42011-04-12 23:21:44 +0000890 addSourceLine(ElemDie, DV);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000891 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000892 DIDerivedType DDTy(Element);
893 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
894 ElemDie = new DIE(dwarf::DW_TAG_friend);
895 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
896 } else
897 ElemDie = createMemberDIE(DIDerivedType(Element));
Eric Christopher663e0cf2012-03-28 07:34:31 +0000898 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000899 DIObjCProperty Property(Element);
900 ElemDie = new DIE(Property.getTag());
901 StringRef PropertyName = Property.getObjCPropertyName();
902 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000903 addType(ElemDie, Property.getType());
904 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000905 StringRef GetterName = Property.getObjCPropertyGetterName();
906 if (!GetterName.empty())
907 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
908 StringRef SetterName = Property.getObjCPropertySetterName();
909 if (!SetterName.empty())
910 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
911 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000912 if (Property.isReadOnlyObjCProperty())
913 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
914 if (Property.isReadWriteObjCProperty())
915 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
916 if (Property.isAssignObjCProperty())
917 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
918 if (Property.isRetainObjCProperty())
919 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
920 if (Property.isCopyObjCProperty())
921 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
922 if (Property.isNonAtomicObjCProperty())
923 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
924 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000925 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000926 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +0000927
Devang Patel30d409c2012-02-07 23:33:58 +0000928 DIEEntry *Entry = getDIEEntry(Element);
929 if (!Entry) {
930 Entry = createDIEEntry(ElemDie);
931 insertDIEEntry(Element, Entry);
932 }
Devang Patel9e11eb12012-02-04 01:30:32 +0000933 } else
Devang Patel161b2f42011-04-12 23:21:44 +0000934 continue;
935 Buffer.addChild(ElemDie);
936 }
937
938 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000939 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +0000940
Devang Patel161b2f42011-04-12 23:21:44 +0000941 DICompositeType ContainingType = CTy.getContainingType();
942 if (DIDescriptor(ContainingType).isCompositeType())
943 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
944 getOrCreateTypeDIE(DIType(ContainingType)));
945 else {
946 DIDescriptor Context = CTy.getContext();
947 addToContextOwner(&Buffer, Context);
948 }
949
Devang Patel201e6cd2011-05-12 21:29:42 +0000950 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000951 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +0000952
Eric Christopher1a8e8862011-12-16 23:42:42 +0000953 // Add template parameters to a class, structure or union types.
954 // FIXME: The support isn't in the metadata for this yet.
955 if (Tag == dwarf::DW_TAG_class_type ||
956 Tag == dwarf::DW_TAG_structure_type ||
957 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000958 addTemplateParams(Buffer, CTy.getTemplateParams());
959
960 break;
961 }
962 default:
963 break;
964 }
965
966 // Add name if not anonymous or intermediate type.
967 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000968 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000969
Eric Christopher4a5d8392012-05-22 18:45:18 +0000970 if (Tag == dwarf::DW_TAG_enumeration_type ||
971 Tag == dwarf::DW_TAG_class_type ||
972 Tag == dwarf::DW_TAG_structure_type ||
973 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +0000974 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +0000975 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +0000976 if (Size)
977 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +0000978 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +0000979 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +0000980 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
981
982 // If we're a forward decl, say so.
983 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000984 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +0000985
986 // Add source line info if available.
987 if (!CTy.isForwardDecl())
988 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +0000989
990 // No harm in adding the runtime language to the declaration.
991 unsigned RLang = CTy.getRunTimeLang();
992 if (RLang)
993 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
994 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +0000995 }
996}
997
Eric Christopher8b4310b2012-11-21 00:34:38 +0000998/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +0000999/// for the given DITemplateTypeParameter.
1000DIE *
1001CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1002 DIE *ParamDIE = getDIE(TP);
1003 if (ParamDIE)
1004 return ParamDIE;
1005
1006 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1007 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001008 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001009 return ParamDIE;
1010}
1011
Eric Christopher8b4310b2012-11-21 00:34:38 +00001012/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001013/// for the given DITemplateValueParameter.
1014DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001015CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001016 DIE *ParamDIE = getDIE(TPV);
1017 if (ParamDIE)
1018 return ParamDIE;
1019
1020 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1021 addType(ParamDIE, TPV.getType());
1022 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001023 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001024 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +00001025 TPV.getValue());
1026 return ParamDIE;
1027}
1028
Devang Patel31c5d052011-05-06 16:57:54 +00001029/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1030DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1031 DIE *NDie = getDIE(NS);
1032 if (NDie)
1033 return NDie;
1034 NDie = new DIE(dwarf::DW_TAG_namespace);
1035 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001036 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001037 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001038 addAccelNamespace(NS.getName(), NDie);
1039 } else
1040 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001041 addSourceLine(NDie, NS);
1042 addToContextOwner(NDie, NS.getContext());
1043 return NDie;
1044}
1045
Devang Pateldbc64af2011-08-15 17:24:54 +00001046/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1047/// printer to not emit usual symbol prefix before the symbol name is used then
1048/// return linkage name after skipping this special LLVM prefix.
1049static StringRef getRealLinkageName(StringRef LinkageName) {
1050 char One = '\1';
1051 if (LinkageName.startswith(StringRef(&One, 1)))
1052 return LinkageName.substr(1);
1053 return LinkageName;
1054}
1055
1056/// getOrCreateSubprogramDIE - Create new DIE using SP.
1057DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1058 DIE *SPDie = getDIE(SP);
1059 if (SPDie)
1060 return SPDie;
1061
Peter Collingbourne27302f02012-05-27 18:36:44 +00001062 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1063
1064 // DW_TAG_inlined_subroutine may refer to this DIE.
1065 insertDIE(SP, SPDie);
1066
Rafael Espindola01b55b42011-11-10 22:34:29 +00001067 DISubprogram SPDecl = SP.getFunctionDeclaration();
1068 DIE *DeclDie = NULL;
1069 if (SPDecl.isSubprogram()) {
1070 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1071 }
1072
Devang Pateldbc64af2011-08-15 17:24:54 +00001073 // Add to context owner.
1074 addToContextOwner(SPDie, SP.getContext());
1075
1076 // Add function template parameters.
1077 addTemplateParams(*SPDie, SP.getTemplateParams());
1078
Eric Christophere9722e12012-04-16 23:54:23 +00001079 // Unfortunately this code needs to stay here instead of below the
1080 // AT_specification code in order to work around a bug in older
1081 // gdbs that requires the linkage name to resolve multiple template
1082 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001083 // TODO: Remove this set of code when we get rid of the old gdb
1084 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001085 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001086 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001087 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1088 getRealLinkageName(LinkageName));
1089
Devang Pateldbc64af2011-08-15 17:24:54 +00001090 // If this DIE is going to refer declaration info using AT_specification
1091 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001092 if (DeclDie) {
1093 // Refer function declaration directly.
1094 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1095 DeclDie);
1096
Devang Pateldbc64af2011-08-15 17:24:54 +00001097 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001098 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001099
Eric Christophercbbd5b12012-08-23 22:52:55 +00001100 // Add the linkage name if we have one.
1101 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1102 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1103 getRealLinkageName(LinkageName));
1104
Devang Pateldbc64af2011-08-15 17:24:54 +00001105 // Constructors and operators for anonymous aggregates do not have names.
1106 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001107 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001108
1109 addSourceLine(SPDie, SP);
1110
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001111 // Add the prototype if we have a prototype and we have a C like
1112 // language.
1113 if (SP.isPrototyped() &&
1114 (Language == dwarf::DW_LANG_C89 ||
1115 Language == dwarf::DW_LANG_C99 ||
1116 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001117 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001118
1119 // Add Return Type.
1120 DICompositeType SPTy = SP.getType();
1121 DIArray Args = SPTy.getTypeArray();
1122 unsigned SPTag = SPTy.getTag();
1123
1124 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1125 addType(SPDie, SPTy);
1126 else
1127 addType(SPDie, DIType(Args.getElement(0)));
1128
1129 unsigned VK = SP.getVirtuality();
1130 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001131 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001132 DIEBlock *Block = getDIEBlock();
1133 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1134 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1135 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1136 ContainingTypeMap.insert(std::make_pair(SPDie,
1137 SP.getContainingType()));
1138 }
1139
1140 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001141 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001142
Devang Pateldbc64af2011-08-15 17:24:54 +00001143 // Add arguments. Do not add arguments for subprogram definition. They will
1144 // be handled while processing variables.
1145 DICompositeType SPTy = SP.getType();
1146 DIArray Args = SPTy.getTypeArray();
1147 unsigned SPTag = SPTy.getTag();
1148
1149 if (SPTag == dwarf::DW_TAG_subroutine_type)
1150 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1151 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001152 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001153 addType(Arg, ATy);
1154 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001155 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001156 SPDie->addChild(Arg);
1157 }
1158 }
1159
1160 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001161 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001162
1163 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001164 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001165
1166 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001167 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001168
1169 if (unsigned isa = Asm->getISAEncoding()) {
1170 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1171 }
1172
1173 return SPDie;
1174}
1175
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001176// Return const expression if value is a GEP to access merged global
1177// constant. e.g.
1178// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1179static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1180 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1181 if (!CE || CE->getNumOperands() != 3 ||
1182 CE->getOpcode() != Instruction::GetElementPtr)
1183 return NULL;
1184
1185 // First operand points to a global struct.
1186 Value *Ptr = CE->getOperand(0);
1187 if (!isa<GlobalValue>(Ptr) ||
1188 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1189 return NULL;
1190
1191 // Second operand is zero.
1192 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1193 if (!CI || !CI->isZero())
1194 return NULL;
1195
1196 // Third operand is offset.
1197 if (!isa<ConstantInt>(CE->getOperand(2)))
1198 return NULL;
1199
1200 return CE;
1201}
1202
1203/// createGlobalVariableDIE - create global variable DIE.
1204void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001205 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001206 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001207 return;
1208
Devang Patel49e2f032011-08-18 22:21:50 +00001209 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001210 if (!GV.Verify())
1211 return;
1212
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001213 DIE *VariableDIE = new DIE(GV.getTag());
Devang Patel49e2f032011-08-18 22:21:50 +00001214 // Add to map.
1215 insertDIE(N, VariableDIE);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001216
1217 // Add name.
Nick Lewycky390c40d2011-10-27 06:44:11 +00001218 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001219 StringRef LinkageName = GV.getLinkageName();
Devang Patel49e2f032011-08-18 22:21:50 +00001220 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001221 if (!LinkageName.empty() && isGlobalVariable)
Nick Lewycky746cb672011-10-26 22:55:33 +00001222 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Nick Lewycky390c40d2011-10-27 06:44:11 +00001223 getRealLinkageName(LinkageName));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001224 // Add type.
Devang Patel49e2f032011-08-18 22:21:50 +00001225 DIType GTy = GV.getType();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001226 addType(VariableDIE, GTy);
1227
1228 // Add scoping info.
Eric Christopherdfa30e12011-11-09 05:24:07 +00001229 if (!GV.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001230 addFlag(VariableDIE, dwarf::DW_AT_external);
Eric Christopherdfa30e12011-11-09 05:24:07 +00001231
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001232 // Add line number info.
1233 addSourceLine(VariableDIE, GV);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001234 // Add to context owner.
1235 DIDescriptor GVContext = GV.getContext();
1236 addToContextOwner(VariableDIE, GVContext);
1237 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001238 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001239 DIE *VariableSpecDIE = NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001240 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001241 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001242 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1243 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1244 addLabel(Block, 0, dwarf::DW_FORM_udata,
1245 Asm->Mang->getSymbol(GV.getGlobal()));
1246 // Do not create specification DIE if context is either compile unit
1247 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001248 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001249 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1250 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001251 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001252 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1253 dwarf::DW_FORM_ref4, VariableDIE);
1254 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher873cf0a2012-08-24 01:14:27 +00001255 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001256 addDie(VariableSpecDIE);
1257 } else {
1258 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001259 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001260 } else if (const ConstantInt *CI =
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001261 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1262 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1263 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001264 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001265 // GV is a merged global.
1266 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1267 Value *Ptr = CE->getOperand(0);
1268 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1269 addLabel(Block, 0, dwarf::DW_FORM_udata,
1270 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1271 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1272 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001273 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001274 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001275 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1276 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1277 }
1278
Eric Christopherd117fbb2011-11-11 01:55:22 +00001279 if (addToAccelTable) {
1280 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1281 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001282
Eric Christopherd117fbb2011-11-11 01:55:22 +00001283 // If the linkage name is different than the name, go ahead and output
1284 // that as well into the name table.
1285 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1286 addAccelName(GV.getLinkageName(), AddrDIE);
1287 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001288
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001289 return;
1290}
1291
Devang Patel161b2f42011-04-12 23:21:44 +00001292/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001293void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1294 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001295 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1296 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001297
Bill Wendling222c2fd2012-12-06 07:38:10 +00001298 // The LowerBound value defines the lower bounds which is typically zero for
1299 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1300 // Count == -1 then the array is unbounded and we do not emit
1301 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1302 // Count == 0, then the array has zero elements in which case we do not emit
1303 // an upper bound.
1304 int64_t LowerBound = SR.getLo();
1305 int64_t DefaultLowerBound = getLowerBoundDefault();
Bill Wendling9493dae2012-12-04 21:34:03 +00001306 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001307
Bill Wendling222c2fd2012-12-06 07:38:10 +00001308 if (LowerBound != DefaultLowerBound || DefaultLowerBound == -1)
1309 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1310
1311 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001312 // FIXME: An unbounded array should reference the expression that defines
1313 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001314 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001315
Devang Patel161b2f42011-04-12 23:21:44 +00001316 Buffer.addChild(DW_Subrange);
1317}
1318
1319/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1320void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1321 DICompositeType *CTy) {
1322 Buffer.setTag(dwarf::DW_TAG_array_type);
1323 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Eric Christopher873cf0a2012-08-24 01:14:27 +00001324 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001325
1326 // Emit derived type.
1327 addType(&Buffer, CTy->getTypeDerivedFrom());
1328 DIArray Elements = CTy->getTypeArray();
1329
1330 // Get an anonymous type for index type.
1331 DIE *IdxTy = getIndexTyDie();
1332 if (!IdxTy) {
1333 // Construct an anonymous type for index type.
1334 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1335 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1336 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1337 dwarf::DW_ATE_signed);
1338 addDie(IdxTy);
1339 setIndexTyDie(IdxTy);
1340 }
1341
1342 // Add subranges to array type.
1343 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1344 DIDescriptor Element = Elements.getElement(i);
1345 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1346 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1347 }
1348}
1349
1350/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1351DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1352 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1353 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001354 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001355 int64_t Value = ETy.getEnumValue();
1356 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1357 return Enumerator;
1358}
1359
Devang Pateldbc64af2011-08-15 17:24:54 +00001360/// constructContainingTypeDIEs - Construct DIEs for types that contain
1361/// vtables.
1362void CompileUnit::constructContainingTypeDIEs() {
1363 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1364 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1365 DIE *SPDie = CI->first;
1366 const MDNode *N = CI->second;
1367 if (!N) continue;
1368 DIE *NDie = getDIE(N);
1369 if (!NDie) continue;
1370 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1371 }
1372}
1373
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001374/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1375DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1376 StringRef Name = DV->getName();
1377 if (Name.empty())
1378 return NULL;
1379
1380 // Translate tag to proper Dwarf tag.
1381 unsigned Tag = DV->getTag();
1382
1383 // Define variable debug information entry.
1384 DIE *VariableDie = new DIE(Tag);
1385 DbgVariable *AbsVar = DV->getAbstractVariable();
1386 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1387 if (AbsDIE)
1388 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1389 dwarf::DW_FORM_ref4, AbsDIE);
1390 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001391 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001392 addSourceLine(VariableDie, DV->getVariable());
1393 addType(VariableDie, DV->getType());
1394 }
1395
1396 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001397 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001398
1399 if (isScopeAbstract) {
1400 DV->setDIE(VariableDie);
1401 return VariableDie;
1402 }
1403
1404 // Add variable address.
1405
1406 unsigned Offset = DV->getDotDebugLocOffset();
1407 if (Offset != ~0U) {
1408 addLabel(VariableDie, dwarf::DW_AT_location,
1409 dwarf::DW_FORM_data4,
1410 Asm->GetTempSymbol("debug_loc", Offset));
1411 DV->setDIE(VariableDie);
1412 return VariableDie;
1413 }
1414
Eric Christopher8cf5e742011-10-03 15:49:20 +00001415 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001416 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1417 bool updated = false;
1418 if (DVInsn->getNumOperands() == 3) {
1419 if (DVInsn->getOperand(0).isReg()) {
1420 const MachineOperand RegOp = DVInsn->getOperand(0);
1421 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1422 if (DVInsn->getOperand(1).isImm() &&
1423 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1424 unsigned FrameReg = 0;
1425 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001426 int Offset =
1427 TFI->getFrameIndexReference(*Asm->MF,
1428 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001429 FrameReg);
1430 MachineLocation Location(FrameReg, Offset);
1431 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001432
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001433 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001434 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001435 MachineLocation(RegOp.getReg()));
1436 updated = true;
1437 }
1438 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001439 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001440 addConstantValue(VariableDie, DVInsn->getOperand(0),
1441 DV->getType());
1442 else if (DVInsn->getOperand(0).isFPImm())
1443 updated =
1444 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1445 else if (DVInsn->getOperand(0).isCImm())
1446 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001447 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001448 DVInsn->getOperand(0).getCImm(),
1449 DV->getType().isUnsignedDIType());
1450 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001451 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001452 Asm->getDebugValueLocation(DVInsn));
1453 updated = true;
1454 }
1455 if (!updated) {
1456 // If variableDie is not updated then DBG_VALUE instruction does not
1457 // have valid variable info.
1458 delete VariableDie;
1459 return NULL;
1460 }
1461 DV->setDIE(VariableDie);
1462 return VariableDie;
1463 } else {
1464 // .. else use frame index.
1465 int FI = DV->getFrameIndex();
1466 if (FI != ~0) {
1467 unsigned FrameReg = 0;
1468 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001469 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001470 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1471 MachineLocation Location(FrameReg, Offset);
1472 addVariableAddress(DV, VariableDie, Location);
1473 }
1474 }
1475
1476 DV->setDIE(VariableDie);
1477 return VariableDie;
1478}
1479
Devang Patel161b2f42011-04-12 23:21:44 +00001480/// createMemberDIE - Create new member DIE.
1481DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1482 DIE *MemberDie = new DIE(DT.getTag());
1483 StringRef Name = DT.getName();
1484 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001485 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001486
1487 addType(MemberDie, DT.getTypeDerivedFrom());
1488
1489 addSourceLine(MemberDie, DT);
1490
1491 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1492 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1493
1494 uint64_t Size = DT.getSizeInBits();
1495 uint64_t FieldSize = DT.getOriginalTypeSize();
1496
1497 if (Size != FieldSize) {
1498 // Handle bitfield.
1499 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1500 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1501
1502 uint64_t Offset = DT.getOffsetInBits();
1503 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1504 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1505 uint64_t FieldOffset = (HiMark - FieldSize);
1506 Offset -= FieldOffset;
1507
1508 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001509 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001510 Offset = FieldSize - (Offset + Size);
1511 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1512
1513 // Here WD_AT_data_member_location points to the anonymous
1514 // field that includes this bit field.
1515 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1516
1517 } else
1518 // This is not a bitfield.
1519 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1520
1521 if (DT.getTag() == dwarf::DW_TAG_inheritance
1522 && DT.isVirtual()) {
1523
1524 // For C++, virtual base classes are not at fixed offset. Use following
1525 // expression to extract appropriate offset from vtable.
1526 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1527
1528 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1529 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1530 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1531 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1532 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1533 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1534 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1535 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1536
1537 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1538 VBaseLocationDie);
1539 } else
1540 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1541
1542 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001543 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001544 dwarf::DW_ACCESS_protected);
1545 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001546 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001547 dwarf::DW_ACCESS_private);
1548 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001549 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001550 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001551 dwarf::DW_ACCESS_public);
1552 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001553 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001554 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001555
1556 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001557 if (MDNode *PNode = DT.getObjCProperty())
1558 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001559 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001560 PropertyDie);
1561
Devang Patel9e11eb12012-02-04 01:30:32 +00001562 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001563 StringRef PropertyName = DT.getObjCPropertyName();
1564 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001565 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001566 StringRef GetterName = DT.getObjCPropertyGetterName();
1567 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001568 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001569 StringRef SetterName = DT.getObjCPropertySetterName();
1570 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001571 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001572 unsigned PropertyAttributes = 0;
1573 if (DT.isReadOnlyObjCProperty())
1574 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1575 if (DT.isReadWriteObjCProperty())
1576 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1577 if (DT.isAssignObjCProperty())
1578 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1579 if (DT.isRetainObjCProperty())
1580 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1581 if (DT.isCopyObjCProperty())
1582 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1583 if (DT.isNonAtomicObjCProperty())
1584 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1585 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001586 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patele9db5e22011-04-16 00:11:51 +00001587 PropertyAttributes);
1588 }
Devang Patel161b2f42011-04-12 23:21:44 +00001589 return MemberDie;
1590}