blob: f5f36e461f19546f4e46e7634e71dac8e99fab00 [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
Eric Christopher873cf0a2012-08-24 01:14:27 +000054/// addFlag - Add a flag that is true.
55void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
56 if (!DD->useDarwinGDBCompat())
57 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
58 DIEIntegerOne);
59 else
60 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
61}
62
Devang Patel161b2f42011-04-12 23:21:44 +000063/// addUInt - Add an unsigned integer attribute data and value.
64///
65void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
66 unsigned Form, uint64_t Integer) {
67 if (!Form) Form = DIEInteger::BestForm(false, Integer);
68 DIEValue *Value = Integer == 1 ?
69 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
70 Die->addValue(Attribute, Form, Value);
71}
72
73/// addSInt - Add an signed integer attribute data and value.
74///
75void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
76 unsigned Form, int64_t Integer) {
77 if (!Form) Form = DIEInteger::BestForm(true, Integer);
78 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
79 Die->addValue(Attribute, Form, Value);
80}
81
Nick Lewycky6a7efcf2011-10-28 05:29:47 +000082/// addString - Add a string attribute data and value. We always emit a
83/// reference to the string pool instead of immediate strings so that DIEs have
84/// more predictable sizes.
Nick Lewycky390c40d2011-10-27 06:44:11 +000085void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Nick Lewycky6a7efcf2011-10-28 05:29:47 +000086 MCSymbol *Symb = DD->getStringPoolEntry(String);
87 DIEValue *Value;
88 if (Asm->needsRelocationsForDwarfStringPool())
89 Value = new (DIEValueAllocator) DIELabel(Symb);
90 else {
91 MCSymbol *StringPool = DD->getStringPool();
92 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +000093 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +000094 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +000095}
96
97/// addLabel - Add a Dwarf label attribute data and value.
98///
99void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
100 const MCSymbol *Label) {
101 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
102 Die->addValue(Attribute, Form, Value);
103}
104
105/// addDelta - Add a label delta attribute data and value.
106///
107void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
108 const MCSymbol *Hi, const MCSymbol *Lo) {
109 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
110 Die->addValue(Attribute, Form, Value);
111}
112
113/// addDIEEntry - Add a DIE attribute data and value.
114///
115void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
116 DIE *Entry) {
117 Die->addValue(Attribute, Form, createDIEEntry(Entry));
118}
119
Devang Patel161b2f42011-04-12 23:21:44 +0000120/// addBlock - Add block data.
121///
122void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
123 DIEBlock *Block) {
124 Block->ComputeSize(Asm);
125 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
126 Die->addValue(Attribute, Block->BestForm(), Block);
127}
128
129/// addSourceLine - Add location information to specified debug information
130/// entry.
131void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
132 // Verify variable.
133 if (!V.Verify())
134 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000135
Devang Patel161b2f42011-04-12 23:21:44 +0000136 unsigned Line = V.getLineNumber();
137 if (Line == 0)
138 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000139 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Devang Patel161b2f42011-04-12 23:21:44 +0000140 V.getContext().getDirectory());
141 assert(FileID && "Invalid file id");
142 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
143 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
144}
145
146/// addSourceLine - Add location information to specified debug information
147/// entry.
148void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
149 // Verify global variable.
150 if (!G.Verify())
151 return;
152
153 unsigned Line = G.getLineNumber();
154 if (Line == 0)
155 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000156 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000157 assert(FileID && "Invalid file id");
158 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
159 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
160}
161
162/// addSourceLine - Add location information to specified debug information
163/// entry.
164void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
165 // Verify subprogram.
166 if (!SP.Verify())
167 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000168
Devang Patel161b2f42011-04-12 23:21:44 +0000169 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000170 unsigned Line = SP.getLineNumber();
171 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000172 return;
173
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000174 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000175 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000176 assert(FileID && "Invalid file id");
177 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
178 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
179}
180
181/// addSourceLine - Add location information to specified debug information
182/// entry.
183void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
184 // Verify type.
185 if (!Ty.Verify())
186 return;
187
188 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000189 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000190 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000191 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000192 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000193 assert(FileID && "Invalid file id");
194 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
195 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
196}
197
198/// addSourceLine - Add location information to specified debug information
199/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000200void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
201 // Verify type.
202 if (!Ty.Verify())
203 return;
204
205 unsigned Line = Ty.getLineNumber();
206 if (Line == 0)
207 return;
208 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000209 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Eric Christopher4d069bf2012-05-22 18:45:24 +0000210 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000211 assert(FileID && "Invalid file id");
212 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
213 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
214}
215
216/// addSourceLine - Add location information to specified debug information
217/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000218void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
219 // Verify namespace.
220 if (!NS.Verify())
221 return;
222
223 unsigned Line = NS.getLineNumber();
224 if (Line == 0)
225 return;
226 StringRef FN = NS.getFilename();
227
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000228 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000229 assert(FileID && "Invalid file id");
230 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
231 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
232}
233
Eric Christopher8b4310b2012-11-21 00:34:38 +0000234/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000235/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000236void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000237 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000238 if (DV->variableHasComplexAddress())
239 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
240 else if (DV->isBlockByrefVariable())
241 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
242 else
243 addAddress(Die, dwarf::DW_AT_location, Location);
244}
245
Devang Patel116da2f2011-04-26 19:06:18 +0000246/// addRegisterOp - Add register operand.
247void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
248 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
249 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
250 if (DWReg < 32)
251 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
252 else {
253 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
254 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
255 }
256}
257
258/// addRegisterOffset - Add register offset.
259void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
260 int64_t Offset) {
261 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
262 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
263 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
264 if (Reg == TRI->getFrameRegister(*Asm->MF))
265 // If variable offset is based in frame register then use fbreg.
266 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
267 else if (DWReg < 32)
268 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
269 else {
270 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
271 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
272 }
273 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
274}
275
276/// addAddress - Add an address attribute to a die based on the location
277/// provided.
278void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
279 const MachineLocation &Location) {
280 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
281
282 if (Location.isReg())
283 addRegisterOp(Block, Location.getReg());
284 else
285 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
286
287 // Now attach the location information to the DIE.
288 addBlock(Die, Attribute, 0, Block);
289}
290
Devang Patel161b2f42011-04-12 23:21:44 +0000291/// addComplexAddress - Start with the address based on the location provided,
292/// and generate the DWARF information necessary to find the actual variable
293/// given the extra address information encoded in the DIVariable, starting from
294/// the starting location. Add the DWARF information to the die.
295///
296void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
297 unsigned Attribute,
298 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000299 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000300 unsigned N = DV->getNumAddrElements();
301 unsigned i = 0;
302 if (Location.isReg()) {
303 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
304 // If first address element is OpPlus then emit
305 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
306 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
307 i = 2;
308 } else
309 addRegisterOp(Block, Location.getReg());
310 }
Devang Patel116da2f2011-04-26 19:06:18 +0000311 else
312 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000313
Devang Patelc26f5442011-04-28 02:22:40 +0000314 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000315 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000316 if (Element == DIBuilder::OpPlus) {
317 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
318 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
319 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000320 if (!Location.isReg())
321 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000322 } else llvm_unreachable("unknown DIBuilder Opcode");
323 }
324
325 // Now attach the location information to the DIE.
326 addBlock(Die, Attribute, 0, Block);
327}
328
329/* Byref variables, in Blocks, are declared by the programmer as "SomeType
330 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
331 gives the variable VarName either the struct, or a pointer to the struct, as
332 its type. This is necessary for various behind-the-scenes things the
333 compiler needs to do with by-reference variables in Blocks.
334
335 However, as far as the original *programmer* is concerned, the variable
336 should still have type 'SomeType', as originally declared.
337
338 The function getBlockByrefType dives into the __Block_byref_x_VarName
339 struct to find the original type of the variable, which is then assigned to
340 the variable's Debug Information Entry as its real type. So far, so good.
341 However now the debugger will expect the variable VarName to have the type
342 SomeType. So we need the location attribute for the variable to be an
343 expression that explains to the debugger how to navigate through the
344 pointers and struct to find the actual variable of type SomeType.
345
346 The following function does just that. We start by getting
347 the "normal" location for the variable. This will be the location
348 of either the struct __Block_byref_x_VarName or the pointer to the
349 struct __Block_byref_x_VarName.
350
351 The struct will look something like:
352
353 struct __Block_byref_x_VarName {
354 ... <various fields>
355 struct __Block_byref_x_VarName *forwarding;
356 ... <various other fields>
357 SomeType VarName;
358 ... <maybe more fields>
359 };
360
361 If we are given the struct directly (as our starting point) we
362 need to tell the debugger to:
363
364 1). Add the offset of the forwarding field.
365
366 2). Follow that pointer to get the real __Block_byref_x_VarName
367 struct to use (the real one may have been copied onto the heap).
368
369 3). Add the offset for the field VarName, to find the actual variable.
370
371 If we started with a pointer to the struct, then we need to
372 dereference that pointer first, before the other steps.
373 Translating this into DWARF ops, we will need to append the following
374 to the current location description for the variable:
375
376 DW_OP_deref -- optional, if we start with a pointer
377 DW_OP_plus_uconst <forward_fld_offset>
378 DW_OP_deref
379 DW_OP_plus_uconst <varName_fld_offset>
380
381 That is what this function does. */
382
383/// addBlockByrefAddress - Start with the address based on the location
384/// provided, and generate the DWARF information necessary to find the
385/// actual Block variable (navigating the Block struct) based on the
386/// starting location. Add the DWARF information to the die. For
387/// more information, read large comment just above here.
388///
389void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
390 unsigned Attribute,
391 const MachineLocation &Location) {
392 DIType Ty = DV->getType();
393 DIType TmpTy = Ty;
394 unsigned Tag = Ty.getTag();
395 bool isPointer = false;
396
397 StringRef varName = DV->getName();
398
399 if (Tag == dwarf::DW_TAG_pointer_type) {
400 DIDerivedType DTy = DIDerivedType(Ty);
401 TmpTy = DTy.getTypeDerivedFrom();
402 isPointer = true;
403 }
404
405 DICompositeType blockStruct = DICompositeType(TmpTy);
406
407 // Find the __forwarding field and the variable field in the __Block_byref
408 // struct.
409 DIArray Fields = blockStruct.getTypeArray();
410 DIDescriptor varField = DIDescriptor();
411 DIDescriptor forwardingField = DIDescriptor();
412
413 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
414 DIDescriptor Element = Fields.getElement(i);
415 DIDerivedType DT = DIDerivedType(Element);
416 StringRef fieldName = DT.getName();
417 if (fieldName == "__forwarding")
418 forwardingField = Element;
419 else if (fieldName == varName)
420 varField = Element;
421 }
422
423 // Get the offsets for the forwarding field and the variable field.
424 unsigned forwardingFieldOffset =
425 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
426 unsigned varFieldOffset =
427 DIDerivedType(varField).getOffsetInBits() >> 3;
428
429 // Decode the original location, and use that as the start of the byref
430 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000431 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
432
Eric Christophercaba2632012-07-04 02:02:18 +0000433 if (Location.isReg())
434 addRegisterOp(Block, Location.getReg());
435 else
436 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000437
438 // If we started with a pointer to the __Block_byref... struct, then
439 // the first thing we need to do is dereference the pointer (DW_OP_deref).
440 if (isPointer)
441 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
442
443 // Next add the offset for the '__forwarding' field:
444 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
445 // adding the offset if it's 0.
446 if (forwardingFieldOffset > 0) {
447 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
448 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
449 }
450
451 // Now dereference the __forwarding field to get to the real __Block_byref
452 // struct: DW_OP_deref.
453 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
454
455 // Now that we've got the real __Block_byref... struct, add the offset
456 // for the variable's field to get to the location of the actual variable:
457 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
458 if (varFieldOffset > 0) {
459 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
460 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
461 }
462
463 // Now attach the location information to the DIE.
464 addBlock(Die, Attribute, 0, Block);
465}
466
Devang Patel4ec14b02011-07-20 21:57:04 +0000467/// isTypeSigned - Return true if the type is signed.
468static bool isTypeSigned(DIType Ty, int *SizeInBits) {
469 if (Ty.isDerivedType())
470 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
471 if (Ty.isBasicType())
472 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
473 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
474 *SizeInBits = Ty.getSizeInBits();
475 return true;
476 }
477 return false;
478}
479
Devang Patel161b2f42011-04-12 23:21:44 +0000480/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000481bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
482 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000483 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000484 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000485 int SizeInBits = -1;
486 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
487 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
488 switch (SizeInBits) {
489 case 8: Form = dwarf::DW_FORM_data1; break;
490 case 16: Form = dwarf::DW_FORM_data2; break;
491 case 32: Form = dwarf::DW_FORM_data4; break;
492 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000493 default: break;
494 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000495 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000496 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000497
Devang Patel161b2f42011-04-12 23:21:44 +0000498 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
499 return true;
500}
501
502/// addConstantFPValue - Add constant value entry in variable DIE.
503bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000504 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000505 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
506 APFloat FPImm = MO.getFPImm()->getValueAPF();
507
508 // Get the raw data form of the floating point.
509 const APInt FltVal = FPImm.bitcastToAPInt();
510 const char *FltPtr = (const char*)FltVal.getRawData();
511
512 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000513 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000514 int Incr = (LittleEndian ? 1 : -1);
515 int Start = (LittleEndian ? 0 : NumBytes - 1);
516 int Stop = (LittleEndian ? NumBytes : -1);
517
518 // Output the constant to DWARF one byte at a time.
519 for (; Start != Stop; Start += Incr)
520 addUInt(Block, 0, dwarf::DW_FORM_data1,
521 (unsigned char)0xFF & FltPtr[Start]);
522
523 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
524 return true;
525}
526
527/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000528bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000529 bool Unsigned) {
Devang Pateld6a81362011-05-28 00:39:18 +0000530 unsigned CIBitWidth = CI->getBitWidth();
531 if (CIBitWidth <= 64) {
532 unsigned form = 0;
533 switch (CIBitWidth) {
534 case 8: form = dwarf::DW_FORM_data1; break;
535 case 16: form = dwarf::DW_FORM_data2; break;
536 case 32: form = dwarf::DW_FORM_data4; break;
537 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000538 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000539 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
540 }
Devang Patel161b2f42011-04-12 23:21:44 +0000541 if (Unsigned)
Devang Pateld6a81362011-05-28 00:39:18 +0000542 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000543 else
Devang Pateld6a81362011-05-28 00:39:18 +0000544 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000545 return true;
546 }
547
548 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
549
550 // Get the raw data form of the large APInt.
551 const APInt Val = CI->getValue();
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000552 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000553
554 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000555 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000556
557 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000558 for (int i = 0; i < NumBytes; i++) {
559 uint8_t c;
560 if (LittleEndian)
561 c = Ptr64[i / 8] >> (8 * (i & 7));
562 else
563 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
564 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
565 }
Devang Patel161b2f42011-04-12 23:21:44 +0000566
567 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
568 return true;
569}
570
571/// addTemplateParams - Add template parameters in buffer.
572void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
573 // Add template parameters.
574 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
575 DIDescriptor Element = TParams.getElement(i);
576 if (Element.isTemplateTypeParameter())
577 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
578 DITemplateTypeParameter(Element)));
579 else if (Element.isTemplateValueParameter())
580 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
581 DITemplateValueParameter(Element)));
582 }
Devang Patel161b2f42011-04-12 23:21:44 +0000583}
Nick Lewycky746cb672011-10-26 22:55:33 +0000584
Devang Patel161b2f42011-04-12 23:21:44 +0000585/// addToContextOwner - Add Die into the list of its context owner's children.
586void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
587 if (Context.isType()) {
588 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
589 ContextDIE->addChild(Die);
590 } else if (Context.isNameSpace()) {
591 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
592 ContextDIE->addChild(Die);
593 } else if (Context.isSubprogram()) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000594 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
Devang Patel161b2f42011-04-12 23:21:44 +0000595 ContextDIE->addChild(Die);
596 } else if (DIE *ContextDIE = getDIE(Context))
597 ContextDIE->addChild(Die);
598 else
599 addDie(Die);
600}
601
602/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
603/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000604DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
605 DIType Ty(TyNode);
606 if (!Ty.Verify())
607 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000608 DIE *TyDIE = getDIE(Ty);
609 if (TyDIE)
610 return TyDIE;
611
612 // Create new type.
613 TyDIE = new DIE(dwarf::DW_TAG_base_type);
614 insertDIE(Ty, TyDIE);
615 if (Ty.isBasicType())
616 constructTypeDIE(*TyDIE, DIBasicType(Ty));
617 else if (Ty.isCompositeType())
618 constructTypeDIE(*TyDIE, DICompositeType(Ty));
619 else {
620 assert(Ty.isDerivedType() && "Unknown kind of DIType");
621 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
622 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000623 // If this is a named finished type then include it in the list of types
624 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000625 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
626 bool IsImplementation = 0;
627 if (Ty.isCompositeType()) {
628 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000629 // A runtime language of 0 actually means C/C++ and that any
630 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000631 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000632 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000633 }
Eric Christophere0167892012-01-06 23:03:37 +0000634 unsigned Flags = IsImplementation ?
635 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
636 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000637 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000638
Devang Patel161b2f42011-04-12 23:21:44 +0000639 addToContextOwner(TyDIE, Ty.getContext());
640 return TyDIE;
641}
642
643/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000644void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000645 if (!Ty.Verify())
646 return;
647
648 // Check for pre-existence.
649 DIEEntry *Entry = getDIEEntry(Ty);
650 // If it exists then use the existing value.
651 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000652 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000653 return;
654 }
655
656 // Construct type.
657 DIE *Buffer = getOrCreateTypeDIE(Ty);
658
659 // Set up proxy.
660 Entry = createDIEEntry(Buffer);
661 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000662 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000663
664 // If this is a complete composite type then include it in the
665 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000666 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000667}
668
669/// addGlobalType - Add a new global type to the compile unit.
670///
Devang Patelc20bdf12011-06-01 00:23:24 +0000671void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000672 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000673 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
674 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000675 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000676 if (DIEEntry *Entry = getDIEEntry(Ty))
677 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000678}
679
Devang Patel31c5d052011-05-06 16:57:54 +0000680/// addPubTypes - Add type for pubtypes section.
681void CompileUnit::addPubTypes(DISubprogram SP) {
682 DICompositeType SPTy = SP.getType();
683 unsigned SPTag = SPTy.getTag();
684 if (SPTag != dwarf::DW_TAG_subroutine_type)
685 return;
686
687 DIArray Args = SPTy.getTypeArray();
688 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
689 DIType ATy(Args.getElement(i));
690 if (!ATy.Verify())
691 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000692 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000693 }
694}
695
Devang Patel161b2f42011-04-12 23:21:44 +0000696/// constructTypeDIE - Construct basic type die from DIBasicType.
697void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
698 // Get core information.
699 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000700 // Add name if not anonymous or intermediate type.
701 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000702 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000703
704 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
705 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
706 // Unspecified types has only name, nothing else.
707 return;
708 }
709
710 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000711 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000712 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000713
Devang Patel161b2f42011-04-12 23:21:44 +0000714 uint64_t Size = BTy.getSizeInBits() >> 3;
715 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
716}
717
718/// constructTypeDIE - Construct derived type die from DIDerivedType.
719void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
720 // Get core information.
721 StringRef Name = DTy.getName();
722 uint64_t Size = DTy.getSizeInBits() >> 3;
723 unsigned Tag = DTy.getTag();
724
725 // FIXME - Workaround for templates.
726 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
727
728 Buffer.setTag(Tag);
729
730 // Map to main type, void will not have a type.
731 DIType FromTy = DTy.getTypeDerivedFrom();
732 addType(&Buffer, FromTy);
733
734 // Add name if not anonymous or intermediate type.
735 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000736 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000737
738 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000739 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000740 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
741
742 // Add source line info if available and TyDesc is not a forward declaration.
743 if (!DTy.isForwardDecl())
744 addSourceLine(&Buffer, DTy);
745}
746
747/// constructTypeDIE - Construct type DIE from DICompositeType.
748void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
749 // Get core information.
750 StringRef Name = CTy.getName();
751
752 uint64_t Size = CTy.getSizeInBits() >> 3;
753 unsigned Tag = CTy.getTag();
754 Buffer.setTag(Tag);
755
756 switch (Tag) {
757 case dwarf::DW_TAG_vector_type:
758 case dwarf::DW_TAG_array_type:
759 constructArrayTypeDIE(Buffer, &CTy);
760 break;
761 case dwarf::DW_TAG_enumeration_type: {
762 DIArray Elements = CTy.getTypeArray();
763
764 // Add enumerators to enumeration type.
765 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
766 DIE *ElemDie = NULL;
767 DIDescriptor Enum(Elements.getElement(i));
768 if (Enum.isEnumerator()) {
769 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
770 Buffer.addChild(ElemDie);
771 }
772 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000773 DIType DTy = CTy.getTypeDerivedFrom();
774 if (DTy.Verify()) {
775 addType(&Buffer, DTy);
776 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
777 }
Devang Patel161b2f42011-04-12 23:21:44 +0000778 }
779 break;
780 case dwarf::DW_TAG_subroutine_type: {
781 // Add return type.
782 DIArray Elements = CTy.getTypeArray();
783 DIDescriptor RTy = Elements.getElement(0);
784 addType(&Buffer, DIType(RTy));
785
786 bool isPrototyped = true;
787 // Add arguments.
788 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
789 DIDescriptor Ty = Elements.getElement(i);
790 if (Ty.isUnspecifiedParameter()) {
791 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
792 Buffer.addChild(Arg);
793 isPrototyped = false;
794 } else {
795 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
796 addType(Arg, DIType(Ty));
797 Buffer.addChild(Arg);
798 }
799 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000800 // Add prototype flag if we're dealing with a C language and the
801 // function has been prototyped.
802 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000803 (Language == dwarf::DW_LANG_C89 ||
804 Language == dwarf::DW_LANG_C99 ||
805 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000806 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000807 }
808 break;
809 case dwarf::DW_TAG_structure_type:
810 case dwarf::DW_TAG_union_type:
811 case dwarf::DW_TAG_class_type: {
812 // Add elements to structure type.
813 DIArray Elements = CTy.getTypeArray();
814
815 // A forward struct declared type may not have elements available.
816 unsigned N = Elements.getNumElements();
817 if (N == 0)
818 break;
819
820 // Add elements to structure type.
821 for (unsigned i = 0; i < N; ++i) {
822 DIDescriptor Element = Elements.getElement(i);
823 DIE *ElemDie = NULL;
824 if (Element.isSubprogram()) {
825 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000826 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000827 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000828 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000829 dwarf::DW_ACCESS_protected);
830 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000831 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000832 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000833 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000834 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000835 dwarf::DW_ACCESS_public);
836 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000837 addFlag(ElemDie, dwarf::DW_AT_explicit);
Devang Patel161b2f42011-04-12 23:21:44 +0000838 }
839 else if (Element.isVariable()) {
840 DIVariable DV(Element);
841 ElemDie = new DIE(dwarf::DW_TAG_variable);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000842 addString(ElemDie, dwarf::DW_AT_name, DV.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000843 addType(ElemDie, DV.getType());
Eric Christopher873cf0a2012-08-24 01:14:27 +0000844 addFlag(ElemDie, dwarf::DW_AT_declaration);
845 addFlag(ElemDie, dwarf::DW_AT_external);
Devang Patel161b2f42011-04-12 23:21:44 +0000846 addSourceLine(ElemDie, DV);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000847 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000848 DIDerivedType DDTy(Element);
849 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
850 ElemDie = new DIE(dwarf::DW_TAG_friend);
851 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
852 } else
853 ElemDie = createMemberDIE(DIDerivedType(Element));
Eric Christopher663e0cf2012-03-28 07:34:31 +0000854 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000855 DIObjCProperty Property(Element);
856 ElemDie = new DIE(Property.getTag());
857 StringRef PropertyName = Property.getObjCPropertyName();
858 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000859 addType(ElemDie, Property.getType());
860 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000861 StringRef GetterName = Property.getObjCPropertyGetterName();
862 if (!GetterName.empty())
863 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
864 StringRef SetterName = Property.getObjCPropertySetterName();
865 if (!SetterName.empty())
866 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
867 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000868 if (Property.isReadOnlyObjCProperty())
869 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
870 if (Property.isReadWriteObjCProperty())
871 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
872 if (Property.isAssignObjCProperty())
873 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
874 if (Property.isRetainObjCProperty())
875 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
876 if (Property.isCopyObjCProperty())
877 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
878 if (Property.isNonAtomicObjCProperty())
879 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
880 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000881 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000882 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +0000883
Devang Patel30d409c2012-02-07 23:33:58 +0000884 DIEEntry *Entry = getDIEEntry(Element);
885 if (!Entry) {
886 Entry = createDIEEntry(ElemDie);
887 insertDIEEntry(Element, Entry);
888 }
Devang Patel9e11eb12012-02-04 01:30:32 +0000889 } else
Devang Patel161b2f42011-04-12 23:21:44 +0000890 continue;
891 Buffer.addChild(ElemDie);
892 }
893
894 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000895 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +0000896
Devang Patel161b2f42011-04-12 23:21:44 +0000897 DICompositeType ContainingType = CTy.getContainingType();
898 if (DIDescriptor(ContainingType).isCompositeType())
899 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
900 getOrCreateTypeDIE(DIType(ContainingType)));
901 else {
902 DIDescriptor Context = CTy.getContext();
903 addToContextOwner(&Buffer, Context);
904 }
905
Devang Patel201e6cd2011-05-12 21:29:42 +0000906 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000907 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +0000908
Eric Christopher1a8e8862011-12-16 23:42:42 +0000909 // Add template parameters to a class, structure or union types.
910 // FIXME: The support isn't in the metadata for this yet.
911 if (Tag == dwarf::DW_TAG_class_type ||
912 Tag == dwarf::DW_TAG_structure_type ||
913 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000914 addTemplateParams(Buffer, CTy.getTemplateParams());
915
916 break;
917 }
918 default:
919 break;
920 }
921
922 // Add name if not anonymous or intermediate type.
923 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000924 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000925
Eric Christopher4a5d8392012-05-22 18:45:18 +0000926 if (Tag == dwarf::DW_TAG_enumeration_type ||
927 Tag == dwarf::DW_TAG_class_type ||
928 Tag == dwarf::DW_TAG_structure_type ||
929 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +0000930 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +0000931 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +0000932 if (Size)
933 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +0000934 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +0000935 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +0000936 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
937
938 // If we're a forward decl, say so.
939 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000940 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +0000941
942 // Add source line info if available.
943 if (!CTy.isForwardDecl())
944 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +0000945
946 // No harm in adding the runtime language to the declaration.
947 unsigned RLang = CTy.getRunTimeLang();
948 if (RLang)
949 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
950 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +0000951 }
952}
953
Eric Christopher8b4310b2012-11-21 00:34:38 +0000954/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +0000955/// for the given DITemplateTypeParameter.
956DIE *
957CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
958 DIE *ParamDIE = getDIE(TP);
959 if (ParamDIE)
960 return ParamDIE;
961
962 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
963 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000964 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000965 return ParamDIE;
966}
967
Eric Christopher8b4310b2012-11-21 00:34:38 +0000968/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +0000969/// for the given DITemplateValueParameter.
970DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +0000971CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +0000972 DIE *ParamDIE = getDIE(TPV);
973 if (ParamDIE)
974 return ParamDIE;
975
976 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
977 addType(ParamDIE, TPV.getType());
978 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000979 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +0000980 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +0000981 TPV.getValue());
982 return ParamDIE;
983}
984
Devang Patel31c5d052011-05-06 16:57:54 +0000985/// getOrCreateNameSpace - Create a DIE for DINameSpace.
986DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
987 DIE *NDie = getDIE(NS);
988 if (NDie)
989 return NDie;
990 NDie = new DIE(dwarf::DW_TAG_namespace);
991 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +0000992 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000993 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +0000994 addAccelNamespace(NS.getName(), NDie);
995 } else
996 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +0000997 addSourceLine(NDie, NS);
998 addToContextOwner(NDie, NS.getContext());
999 return NDie;
1000}
1001
Devang Pateldbc64af2011-08-15 17:24:54 +00001002/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1003/// printer to not emit usual symbol prefix before the symbol name is used then
1004/// return linkage name after skipping this special LLVM prefix.
1005static StringRef getRealLinkageName(StringRef LinkageName) {
1006 char One = '\1';
1007 if (LinkageName.startswith(StringRef(&One, 1)))
1008 return LinkageName.substr(1);
1009 return LinkageName;
1010}
1011
1012/// getOrCreateSubprogramDIE - Create new DIE using SP.
1013DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1014 DIE *SPDie = getDIE(SP);
1015 if (SPDie)
1016 return SPDie;
1017
Peter Collingbourne27302f02012-05-27 18:36:44 +00001018 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1019
1020 // DW_TAG_inlined_subroutine may refer to this DIE.
1021 insertDIE(SP, SPDie);
1022
Rafael Espindola01b55b42011-11-10 22:34:29 +00001023 DISubprogram SPDecl = SP.getFunctionDeclaration();
1024 DIE *DeclDie = NULL;
1025 if (SPDecl.isSubprogram()) {
1026 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1027 }
1028
Devang Pateldbc64af2011-08-15 17:24:54 +00001029 // Add to context owner.
1030 addToContextOwner(SPDie, SP.getContext());
1031
1032 // Add function template parameters.
1033 addTemplateParams(*SPDie, SP.getTemplateParams());
1034
Eric Christophere9722e12012-04-16 23:54:23 +00001035 // Unfortunately this code needs to stay here instead of below the
1036 // AT_specification code in order to work around a bug in older
1037 // gdbs that requires the linkage name to resolve multiple template
1038 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001039 // TODO: Remove this set of code when we get rid of the old gdb
1040 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001041 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001042 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001043 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1044 getRealLinkageName(LinkageName));
1045
Devang Pateldbc64af2011-08-15 17:24:54 +00001046 // If this DIE is going to refer declaration info using AT_specification
1047 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001048 if (DeclDie) {
1049 // Refer function declaration directly.
1050 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1051 DeclDie);
1052
Devang Pateldbc64af2011-08-15 17:24:54 +00001053 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001054 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001055
Eric Christophercbbd5b12012-08-23 22:52:55 +00001056 // Add the linkage name if we have one.
1057 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1058 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1059 getRealLinkageName(LinkageName));
1060
Devang Pateldbc64af2011-08-15 17:24:54 +00001061 // Constructors and operators for anonymous aggregates do not have names.
1062 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001063 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001064
1065 addSourceLine(SPDie, SP);
1066
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001067 // Add the prototype if we have a prototype and we have a C like
1068 // language.
1069 if (SP.isPrototyped() &&
1070 (Language == dwarf::DW_LANG_C89 ||
1071 Language == dwarf::DW_LANG_C99 ||
1072 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001073 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001074
1075 // Add Return Type.
1076 DICompositeType SPTy = SP.getType();
1077 DIArray Args = SPTy.getTypeArray();
1078 unsigned SPTag = SPTy.getTag();
1079
1080 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1081 addType(SPDie, SPTy);
1082 else
1083 addType(SPDie, DIType(Args.getElement(0)));
1084
1085 unsigned VK = SP.getVirtuality();
1086 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001087 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001088 DIEBlock *Block = getDIEBlock();
1089 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1090 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1091 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1092 ContainingTypeMap.insert(std::make_pair(SPDie,
1093 SP.getContainingType()));
1094 }
1095
1096 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001097 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001098
Devang Pateldbc64af2011-08-15 17:24:54 +00001099 // Add arguments. Do not add arguments for subprogram definition. They will
1100 // be handled while processing variables.
1101 DICompositeType SPTy = SP.getType();
1102 DIArray Args = SPTy.getTypeArray();
1103 unsigned SPTag = SPTy.getTag();
1104
1105 if (SPTag == dwarf::DW_TAG_subroutine_type)
1106 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1107 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001108 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001109 addType(Arg, ATy);
1110 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001111 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001112 SPDie->addChild(Arg);
1113 }
1114 }
1115
1116 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001117 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001118
1119 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001120 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001121
1122 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001123 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001124
1125 if (unsigned isa = Asm->getISAEncoding()) {
1126 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1127 }
1128
1129 return SPDie;
1130}
1131
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001132// Return const expression if value is a GEP to access merged global
1133// constant. e.g.
1134// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1135static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1136 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1137 if (!CE || CE->getNumOperands() != 3 ||
1138 CE->getOpcode() != Instruction::GetElementPtr)
1139 return NULL;
1140
1141 // First operand points to a global struct.
1142 Value *Ptr = CE->getOperand(0);
1143 if (!isa<GlobalValue>(Ptr) ||
1144 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1145 return NULL;
1146
1147 // Second operand is zero.
1148 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1149 if (!CI || !CI->isZero())
1150 return NULL;
1151
1152 // Third operand is offset.
1153 if (!isa<ConstantInt>(CE->getOperand(2)))
1154 return NULL;
1155
1156 return CE;
1157}
1158
1159/// createGlobalVariableDIE - create global variable DIE.
1160void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001161 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001162 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001163 return;
1164
Devang Patel49e2f032011-08-18 22:21:50 +00001165 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001166 if (!GV.Verify())
1167 return;
1168
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001169 DIE *VariableDIE = new DIE(GV.getTag());
Devang Patel49e2f032011-08-18 22:21:50 +00001170 // Add to map.
1171 insertDIE(N, VariableDIE);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001172
1173 // Add name.
Nick Lewycky390c40d2011-10-27 06:44:11 +00001174 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001175 StringRef LinkageName = GV.getLinkageName();
Devang Patel49e2f032011-08-18 22:21:50 +00001176 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001177 if (!LinkageName.empty() && isGlobalVariable)
Nick Lewycky746cb672011-10-26 22:55:33 +00001178 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Nick Lewycky390c40d2011-10-27 06:44:11 +00001179 getRealLinkageName(LinkageName));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001180 // Add type.
Devang Patel49e2f032011-08-18 22:21:50 +00001181 DIType GTy = GV.getType();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001182 addType(VariableDIE, GTy);
1183
1184 // Add scoping info.
Eric Christopherdfa30e12011-11-09 05:24:07 +00001185 if (!GV.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001186 addFlag(VariableDIE, dwarf::DW_AT_external);
Eric Christopherdfa30e12011-11-09 05:24:07 +00001187
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001188 // Add line number info.
1189 addSourceLine(VariableDIE, GV);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001190 // Add to context owner.
1191 DIDescriptor GVContext = GV.getContext();
1192 addToContextOwner(VariableDIE, GVContext);
1193 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001194 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001195 DIE *VariableSpecDIE = NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001196 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001197 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001198 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1199 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1200 addLabel(Block, 0, dwarf::DW_FORM_udata,
1201 Asm->Mang->getSymbol(GV.getGlobal()));
1202 // Do not create specification DIE if context is either compile unit
1203 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001204 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001205 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1206 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001207 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001208 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1209 dwarf::DW_FORM_ref4, VariableDIE);
1210 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher873cf0a2012-08-24 01:14:27 +00001211 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001212 addDie(VariableSpecDIE);
1213 } else {
1214 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001215 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001216 } else if (const ConstantInt *CI =
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001217 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1218 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1219 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001220 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001221 // GV is a merged global.
1222 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1223 Value *Ptr = CE->getOperand(0);
1224 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1225 addLabel(Block, 0, dwarf::DW_FORM_udata,
1226 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1227 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1228 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001229 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001230 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001231 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1232 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1233 }
1234
Eric Christopherd117fbb2011-11-11 01:55:22 +00001235 if (addToAccelTable) {
1236 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1237 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001238
Eric Christopherd117fbb2011-11-11 01:55:22 +00001239 // If the linkage name is different than the name, go ahead and output
1240 // that as well into the name table.
1241 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1242 addAccelName(GV.getLinkageName(), AddrDIE);
1243 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001244
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001245 return;
1246}
1247
Devang Patel161b2f42011-04-12 23:21:44 +00001248/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001249void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1250 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001251 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1252 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelce35d8b2011-11-17 23:43:15 +00001253 uint64_t L = SR.getLo();
1254 uint64_t H = SR.getHi();
Bill Wendlinga7645a32012-12-04 06:20:49 +00001255 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001256
1257 // The L value defines the lower bounds which is typically zero for C/C++. The
1258 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
Eric Christopher8b4310b2012-11-21 00:34:38 +00001259 // of the array. If L > H then do not emit DW_AT_lower_bound and
Devang Patel161b2f42011-04-12 23:21:44 +00001260 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
1261 // array has one element and in such case do not emit lower bound.
1262
1263 if (L > H) {
1264 Buffer.addChild(DW_Subrange);
1265 return;
1266 }
1267 if (L)
Devang Patelce35d8b2011-11-17 23:43:15 +00001268 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
Bill Wendlinga7645a32012-12-04 06:20:49 +00001269 if (H > 0 || Count != 0)
1270 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Devang Patel161b2f42011-04-12 23:21:44 +00001271 Buffer.addChild(DW_Subrange);
1272}
1273
1274/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1275void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1276 DICompositeType *CTy) {
1277 Buffer.setTag(dwarf::DW_TAG_array_type);
1278 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Eric Christopher873cf0a2012-08-24 01:14:27 +00001279 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001280
1281 // Emit derived type.
1282 addType(&Buffer, CTy->getTypeDerivedFrom());
1283 DIArray Elements = CTy->getTypeArray();
1284
1285 // Get an anonymous type for index type.
1286 DIE *IdxTy = getIndexTyDie();
1287 if (!IdxTy) {
1288 // Construct an anonymous type for index type.
1289 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1290 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1291 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1292 dwarf::DW_ATE_signed);
1293 addDie(IdxTy);
1294 setIndexTyDie(IdxTy);
1295 }
1296
1297 // Add subranges to array type.
1298 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1299 DIDescriptor Element = Elements.getElement(i);
1300 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1301 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1302 }
1303}
1304
1305/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1306DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1307 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1308 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001309 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001310 int64_t Value = ETy.getEnumValue();
1311 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1312 return Enumerator;
1313}
1314
Devang Pateldbc64af2011-08-15 17:24:54 +00001315/// constructContainingTypeDIEs - Construct DIEs for types that contain
1316/// vtables.
1317void CompileUnit::constructContainingTypeDIEs() {
1318 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1319 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1320 DIE *SPDie = CI->first;
1321 const MDNode *N = CI->second;
1322 if (!N) continue;
1323 DIE *NDie = getDIE(N);
1324 if (!NDie) continue;
1325 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1326 }
1327}
1328
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001329/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1330DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1331 StringRef Name = DV->getName();
1332 if (Name.empty())
1333 return NULL;
1334
1335 // Translate tag to proper Dwarf tag.
1336 unsigned Tag = DV->getTag();
1337
1338 // Define variable debug information entry.
1339 DIE *VariableDie = new DIE(Tag);
1340 DbgVariable *AbsVar = DV->getAbstractVariable();
1341 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1342 if (AbsDIE)
1343 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1344 dwarf::DW_FORM_ref4, AbsDIE);
1345 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001346 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001347 addSourceLine(VariableDie, DV->getVariable());
1348 addType(VariableDie, DV->getType());
1349 }
1350
1351 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001352 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001353
1354 if (isScopeAbstract) {
1355 DV->setDIE(VariableDie);
1356 return VariableDie;
1357 }
1358
1359 // Add variable address.
1360
1361 unsigned Offset = DV->getDotDebugLocOffset();
1362 if (Offset != ~0U) {
1363 addLabel(VariableDie, dwarf::DW_AT_location,
1364 dwarf::DW_FORM_data4,
1365 Asm->GetTempSymbol("debug_loc", Offset));
1366 DV->setDIE(VariableDie);
1367 return VariableDie;
1368 }
1369
Eric Christopher8cf5e742011-10-03 15:49:20 +00001370 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001371 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1372 bool updated = false;
1373 if (DVInsn->getNumOperands() == 3) {
1374 if (DVInsn->getOperand(0).isReg()) {
1375 const MachineOperand RegOp = DVInsn->getOperand(0);
1376 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1377 if (DVInsn->getOperand(1).isImm() &&
1378 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1379 unsigned FrameReg = 0;
1380 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001381 int Offset =
1382 TFI->getFrameIndexReference(*Asm->MF,
1383 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001384 FrameReg);
1385 MachineLocation Location(FrameReg, Offset);
1386 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001387
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001388 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001389 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001390 MachineLocation(RegOp.getReg()));
1391 updated = true;
1392 }
1393 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001394 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001395 addConstantValue(VariableDie, DVInsn->getOperand(0),
1396 DV->getType());
1397 else if (DVInsn->getOperand(0).isFPImm())
1398 updated =
1399 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1400 else if (DVInsn->getOperand(0).isCImm())
1401 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001402 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001403 DVInsn->getOperand(0).getCImm(),
1404 DV->getType().isUnsignedDIType());
1405 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001406 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001407 Asm->getDebugValueLocation(DVInsn));
1408 updated = true;
1409 }
1410 if (!updated) {
1411 // If variableDie is not updated then DBG_VALUE instruction does not
1412 // have valid variable info.
1413 delete VariableDie;
1414 return NULL;
1415 }
1416 DV->setDIE(VariableDie);
1417 return VariableDie;
1418 } else {
1419 // .. else use frame index.
1420 int FI = DV->getFrameIndex();
1421 if (FI != ~0) {
1422 unsigned FrameReg = 0;
1423 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001424 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001425 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1426 MachineLocation Location(FrameReg, Offset);
1427 addVariableAddress(DV, VariableDie, Location);
1428 }
1429 }
1430
1431 DV->setDIE(VariableDie);
1432 return VariableDie;
1433}
1434
Devang Patel161b2f42011-04-12 23:21:44 +00001435/// createMemberDIE - Create new member DIE.
1436DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1437 DIE *MemberDie = new DIE(DT.getTag());
1438 StringRef Name = DT.getName();
1439 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001440 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001441
1442 addType(MemberDie, DT.getTypeDerivedFrom());
1443
1444 addSourceLine(MemberDie, DT);
1445
1446 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1447 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1448
1449 uint64_t Size = DT.getSizeInBits();
1450 uint64_t FieldSize = DT.getOriginalTypeSize();
1451
1452 if (Size != FieldSize) {
1453 // Handle bitfield.
1454 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1455 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1456
1457 uint64_t Offset = DT.getOffsetInBits();
1458 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1459 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1460 uint64_t FieldOffset = (HiMark - FieldSize);
1461 Offset -= FieldOffset;
1462
1463 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001464 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001465 Offset = FieldSize - (Offset + Size);
1466 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1467
1468 // Here WD_AT_data_member_location points to the anonymous
1469 // field that includes this bit field.
1470 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1471
1472 } else
1473 // This is not a bitfield.
1474 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1475
1476 if (DT.getTag() == dwarf::DW_TAG_inheritance
1477 && DT.isVirtual()) {
1478
1479 // For C++, virtual base classes are not at fixed offset. Use following
1480 // expression to extract appropriate offset from vtable.
1481 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1482
1483 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1484 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1485 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1486 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1487 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1488 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1489 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1490 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1491
1492 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1493 VBaseLocationDie);
1494 } else
1495 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1496
1497 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001498 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001499 dwarf::DW_ACCESS_protected);
1500 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001501 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001502 dwarf::DW_ACCESS_private);
1503 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001504 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001505 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001506 dwarf::DW_ACCESS_public);
1507 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001508 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001509 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001510
1511 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001512 if (MDNode *PNode = DT.getObjCProperty())
1513 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001514 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001515 PropertyDie);
1516
Devang Patel9e11eb12012-02-04 01:30:32 +00001517 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001518 StringRef PropertyName = DT.getObjCPropertyName();
1519 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001520 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001521 StringRef GetterName = DT.getObjCPropertyGetterName();
1522 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001523 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001524 StringRef SetterName = DT.getObjCPropertySetterName();
1525 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001526 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001527 unsigned PropertyAttributes = 0;
1528 if (DT.isReadOnlyObjCProperty())
1529 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1530 if (DT.isReadWriteObjCProperty())
1531 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1532 if (DT.isAssignObjCProperty())
1533 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1534 if (DT.isRetainObjCProperty())
1535 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1536 if (DT.isCopyObjCProperty())
1537 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1538 if (DT.isNonAtomicObjCProperty())
1539 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1540 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001541 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patele9db5e22011-04-16 00:11:51 +00001542 PropertyAttributes);
1543 }
Devang Patel161b2f42011-04-12 23:21:44 +00001544 return MemberDie;
1545}