blob: 58e65c06d4e0685118674b993199b77b78c1d9ac [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
Eric Christopherc36145f2012-01-06 04:35:23 +000016#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000017#include "DwarfCompileUnit.h"
18#include "DwarfDebug.h"
19#include "llvm/Constants.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000021#include "llvm/GlobalVariable.h"
22#include "llvm/Instructions.h"
Eric Christopherd61c34b2011-11-11 03:16:32 +000023#include "llvm/Support/Debug.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000024#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetFrameLowering.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Target/TargetRegisterInfo.h"
29#include "llvm/ADT/APFloat.h"
30#include "llvm/Support/ErrorHandling.h"
31
32using namespace llvm;
33
34/// CompileUnit - Compile unit constructor.
Eric Christopher438b0922012-02-22 08:46:13 +000035CompileUnit::CompileUnit(unsigned I, unsigned L, DIE *D, AsmPrinter *A,
Eric Christopher4d069bf2012-05-22 18:45:24 +000036 DwarfDebug *DW)
Eric Christopher438b0922012-02-22 08:46:13 +000037 : ID(I), 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;
135
136 unsigned Line = V.getLineNumber();
137 if (Line == 0)
138 return;
139 unsigned FileID = DD->GetOrCreateSourceID(V.getContext().getFilename(),
140 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;
Nick Lewycky746cb672011-10-26 22:55:33 +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
Nick Lewycky746cb672011-10-26 22:55:33 +0000174 unsigned FileID = DD->GetOrCreateSourceID(SP.getFilename(),
175 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;
Nick Lewycky746cb672011-10-26 22:55:33 +0000191 unsigned FileID = DD->GetOrCreateSourceID(Ty.getFilename(),
192 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();
209 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
228 unsigned FileID = DD->GetOrCreateSourceID(FN, NS.getDirectory());
229 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
Devang Patele1cdf842011-04-27 22:45:24 +0000234/// addVariableAddress - Add DW_AT_location attribute for a
235/// DbgVariable based on provided MachineLocation.
236void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
237 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 }
Devang Patel4ec14b02011-07-20 21:57:04 +0000495 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
496 : 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.
513 bool LittleEndian = Asm->getTargetData().isLittleEndian();
514 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;
538 default:
539 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.
555 bool LittleEndian = Asm->getTargetData().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 Christopher1b3f9192011-11-10 19:52:58 +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();
673 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000674 && (!Context || Context.isCompileUnit() || Context.isFile()
675 || 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);
833 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)
881 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
882 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
954/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
955/// 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
968/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
969/// 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());
Devang Patel161b2f42011-04-12 23:21:44 +0000980 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
981 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);
Devang Pateldbc64af2011-08-15 17:24:54 +00001098
1099 // 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);
Eric Christophere5212782012-09-12 23:36:19 +00001112 if (ATy.isObjectPointer())
1113 addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, dwarf::DW_FORM_ref4,
1114 Arg);
Devang Pateldbc64af2011-08-15 17:24:54 +00001115 SPDie->addChild(Arg);
1116 }
1117 }
1118
1119 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001120 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001121
1122 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001123 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001124
1125 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001126 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001127
1128 if (unsigned isa = Asm->getISAEncoding()) {
1129 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1130 }
1131
1132 return SPDie;
1133}
1134
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001135// Return const expression if value is a GEP to access merged global
1136// constant. e.g.
1137// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1138static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1139 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1140 if (!CE || CE->getNumOperands() != 3 ||
1141 CE->getOpcode() != Instruction::GetElementPtr)
1142 return NULL;
1143
1144 // First operand points to a global struct.
1145 Value *Ptr = CE->getOperand(0);
1146 if (!isa<GlobalValue>(Ptr) ||
1147 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1148 return NULL;
1149
1150 // Second operand is zero.
1151 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1152 if (!CI || !CI->isZero())
1153 return NULL;
1154
1155 // Third operand is offset.
1156 if (!isa<ConstantInt>(CE->getOperand(2)))
1157 return NULL;
1158
1159 return CE;
1160}
1161
1162/// createGlobalVariableDIE - create global variable DIE.
1163void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001164 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001165 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001166 return;
1167
Devang Patel49e2f032011-08-18 22:21:50 +00001168 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001169 if (!GV.Verify())
1170 return;
1171
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001172 DIE *VariableDIE = new DIE(GV.getTag());
Devang Patel49e2f032011-08-18 22:21:50 +00001173 // Add to map.
1174 insertDIE(N, VariableDIE);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001175
1176 // Add name.
Nick Lewycky390c40d2011-10-27 06:44:11 +00001177 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001178 StringRef LinkageName = GV.getLinkageName();
Devang Patel49e2f032011-08-18 22:21:50 +00001179 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001180 if (!LinkageName.empty() && isGlobalVariable)
Nick Lewycky746cb672011-10-26 22:55:33 +00001181 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Nick Lewycky390c40d2011-10-27 06:44:11 +00001182 getRealLinkageName(LinkageName));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001183 // Add type.
Devang Patel49e2f032011-08-18 22:21:50 +00001184 DIType GTy = GV.getType();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001185 addType(VariableDIE, GTy);
1186
1187 // Add scoping info.
Eric Christopherdfa30e12011-11-09 05:24:07 +00001188 if (!GV.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001189 addFlag(VariableDIE, dwarf::DW_AT_external);
Eric Christopherdfa30e12011-11-09 05:24:07 +00001190
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001191 // Add line number info.
1192 addSourceLine(VariableDIE, GV);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001193 // Add to context owner.
1194 DIDescriptor GVContext = GV.getContext();
1195 addToContextOwner(VariableDIE, GVContext);
1196 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001197 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001198 DIE *VariableSpecDIE = NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001199 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001200 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001201 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1202 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1203 addLabel(Block, 0, dwarf::DW_FORM_udata,
1204 Asm->Mang->getSymbol(GV.getGlobal()));
1205 // Do not create specification DIE if context is either compile unit
1206 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001207 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001208 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1209 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001210 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001211 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1212 dwarf::DW_FORM_ref4, VariableDIE);
1213 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher873cf0a2012-08-24 01:14:27 +00001214 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001215 addDie(VariableSpecDIE);
1216 } else {
1217 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001218 }
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001219 } else if (const ConstantInt *CI =
1220 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1221 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1222 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001223 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001224 // GV is a merged global.
1225 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1226 Value *Ptr = CE->getOperand(0);
1227 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1228 addLabel(Block, 0, dwarf::DW_FORM_udata,
1229 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1230 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1231 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
1232 addUInt(Block, 0, dwarf::DW_FORM_udata,
1233 Asm->getTargetData().getIndexedOffset(Ptr->getType(), Idx));
1234 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1235 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1236 }
1237
Eric Christopherd117fbb2011-11-11 01:55:22 +00001238 if (addToAccelTable) {
1239 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1240 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001241
Eric Christopherd117fbb2011-11-11 01:55:22 +00001242 // If the linkage name is different than the name, go ahead and output
1243 // that as well into the name table.
1244 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1245 addAccelName(GV.getLinkageName(), AddrDIE);
1246 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001247
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001248 return;
1249}
1250
Devang Patel161b2f42011-04-12 23:21:44 +00001251/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001252void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1253 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001254 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1255 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelce35d8b2011-11-17 23:43:15 +00001256 uint64_t L = SR.getLo();
1257 uint64_t H = SR.getHi();
Devang Patel161b2f42011-04-12 23:21:44 +00001258
1259 // The L value defines the lower bounds which is typically zero for C/C++. The
1260 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
1261 // of the array. If L > H then do not emit DW_AT_lower_bound and
1262 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
1263 // array has one element and in such case do not emit lower bound.
1264
1265 if (L > H) {
1266 Buffer.addChild(DW_Subrange);
1267 return;
1268 }
1269 if (L)
Devang Patelce35d8b2011-11-17 23:43:15 +00001270 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1271 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Devang Patel161b2f42011-04-12 23:21:44 +00001272 Buffer.addChild(DW_Subrange);
1273}
1274
1275/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1276void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1277 DICompositeType *CTy) {
1278 Buffer.setTag(dwarf::DW_TAG_array_type);
1279 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Eric Christopher873cf0a2012-08-24 01:14:27 +00001280 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001281
1282 // Emit derived type.
1283 addType(&Buffer, CTy->getTypeDerivedFrom());
1284 DIArray Elements = CTy->getTypeArray();
1285
1286 // Get an anonymous type for index type.
1287 DIE *IdxTy = getIndexTyDie();
1288 if (!IdxTy) {
1289 // Construct an anonymous type for index type.
1290 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1291 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1292 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1293 dwarf::DW_ATE_signed);
1294 addDie(IdxTy);
1295 setIndexTyDie(IdxTy);
1296 }
1297
1298 // Add subranges to array type.
1299 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1300 DIDescriptor Element = Elements.getElement(i);
1301 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1302 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1303 }
1304}
1305
1306/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1307DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1308 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1309 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001310 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001311 int64_t Value = ETy.getEnumValue();
1312 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1313 return Enumerator;
1314}
1315
Devang Pateldbc64af2011-08-15 17:24:54 +00001316/// constructContainingTypeDIEs - Construct DIEs for types that contain
1317/// vtables.
1318void CompileUnit::constructContainingTypeDIEs() {
1319 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1320 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1321 DIE *SPDie = CI->first;
1322 const MDNode *N = CI->second;
1323 if (!N) continue;
1324 DIE *NDie = getDIE(N);
1325 if (!NDie) continue;
1326 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1327 }
1328}
1329
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001330/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1331DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1332 StringRef Name = DV->getName();
1333 if (Name.empty())
1334 return NULL;
1335
1336 // Translate tag to proper Dwarf tag.
1337 unsigned Tag = DV->getTag();
1338
1339 // Define variable debug information entry.
1340 DIE *VariableDie = new DIE(Tag);
1341 DbgVariable *AbsVar = DV->getAbstractVariable();
1342 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1343 if (AbsDIE)
1344 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1345 dwarf::DW_FORM_ref4, AbsDIE);
1346 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001347 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001348 addSourceLine(VariableDie, DV->getVariable());
1349 addType(VariableDie, DV->getType());
1350 }
1351
1352 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001353 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001354
1355 if (isScopeAbstract) {
1356 DV->setDIE(VariableDie);
1357 return VariableDie;
1358 }
1359
1360 // Add variable address.
1361
1362 unsigned Offset = DV->getDotDebugLocOffset();
1363 if (Offset != ~0U) {
1364 addLabel(VariableDie, dwarf::DW_AT_location,
1365 dwarf::DW_FORM_data4,
1366 Asm->GetTempSymbol("debug_loc", Offset));
1367 DV->setDIE(VariableDie);
1368 return VariableDie;
1369 }
1370
Eric Christopher8cf5e742011-10-03 15:49:20 +00001371 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001372 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1373 bool updated = false;
1374 if (DVInsn->getNumOperands() == 3) {
1375 if (DVInsn->getOperand(0).isReg()) {
1376 const MachineOperand RegOp = DVInsn->getOperand(0);
1377 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1378 if (DVInsn->getOperand(1).isImm() &&
1379 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1380 unsigned FrameReg = 0;
1381 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1382 int Offset =
1383 TFI->getFrameIndexReference(*Asm->MF,
1384 DVInsn->getOperand(1).getImm(),
1385 FrameReg);
1386 MachineLocation Location(FrameReg, Offset);
1387 addVariableAddress(DV, VariableDie, Location);
1388
1389 } else if (RegOp.getReg())
1390 addVariableAddress(DV, VariableDie,
1391 MachineLocation(RegOp.getReg()));
1392 updated = true;
1393 }
1394 else if (DVInsn->getOperand(0).isImm())
1395 updated =
1396 addConstantValue(VariableDie, DVInsn->getOperand(0),
1397 DV->getType());
1398 else if (DVInsn->getOperand(0).isFPImm())
1399 updated =
1400 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1401 else if (DVInsn->getOperand(0).isCImm())
1402 updated =
1403 addConstantValue(VariableDie,
1404 DVInsn->getOperand(0).getCImm(),
1405 DV->getType().isUnsignedDIType());
1406 } else {
1407 addVariableAddress(DV, VariableDie,
1408 Asm->getDebugValueLocation(DVInsn));
1409 updated = true;
1410 }
1411 if (!updated) {
1412 // If variableDie is not updated then DBG_VALUE instruction does not
1413 // have valid variable info.
1414 delete VariableDie;
1415 return NULL;
1416 }
1417 DV->setDIE(VariableDie);
1418 return VariableDie;
1419 } else {
1420 // .. else use frame index.
1421 int FI = DV->getFrameIndex();
1422 if (FI != ~0) {
1423 unsigned FrameReg = 0;
1424 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1425 int Offset =
1426 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1427 MachineLocation Location(FrameReg, Offset);
1428 addVariableAddress(DV, VariableDie, Location);
1429 }
1430 }
1431
1432 DV->setDIE(VariableDie);
1433 return VariableDie;
1434}
1435
Devang Patel161b2f42011-04-12 23:21:44 +00001436/// createMemberDIE - Create new member DIE.
1437DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1438 DIE *MemberDie = new DIE(DT.getTag());
1439 StringRef Name = DT.getName();
1440 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001441 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001442
1443 addType(MemberDie, DT.getTypeDerivedFrom());
1444
1445 addSourceLine(MemberDie, DT);
1446
1447 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1448 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1449
1450 uint64_t Size = DT.getSizeInBits();
1451 uint64_t FieldSize = DT.getOriginalTypeSize();
1452
1453 if (Size != FieldSize) {
1454 // Handle bitfield.
1455 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1456 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1457
1458 uint64_t Offset = DT.getOffsetInBits();
1459 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1460 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1461 uint64_t FieldOffset = (HiMark - FieldSize);
1462 Offset -= FieldOffset;
1463
1464 // Maybe we need to work from the other end.
1465 if (Asm->getTargetData().isLittleEndian())
1466 Offset = FieldSize - (Offset + Size);
1467 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1468
1469 // Here WD_AT_data_member_location points to the anonymous
1470 // field that includes this bit field.
1471 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1472
1473 } else
1474 // This is not a bitfield.
1475 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1476
1477 if (DT.getTag() == dwarf::DW_TAG_inheritance
1478 && DT.isVirtual()) {
1479
1480 // For C++, virtual base classes are not at fixed offset. Use following
1481 // expression to extract appropriate offset from vtable.
1482 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1483
1484 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1485 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1486 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1487 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1488 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1489 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1490 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1491 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1492
1493 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1494 VBaseLocationDie);
1495 } else
1496 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1497
1498 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001499 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001500 dwarf::DW_ACCESS_protected);
1501 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001502 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001503 dwarf::DW_ACCESS_private);
1504 // Otherwise C++ member and base classes are considered public.
Devang Patel94c7ddb2011-08-16 22:09:43 +00001505 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001506 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001507 dwarf::DW_ACCESS_public);
1508 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001509 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001510 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001511
1512 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001513 if (MDNode *PNode = DT.getObjCProperty())
1514 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1515 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1516 PropertyDie);
1517
Devang Patel9e11eb12012-02-04 01:30:32 +00001518 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001519 StringRef PropertyName = DT.getObjCPropertyName();
1520 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001521 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001522 StringRef GetterName = DT.getObjCPropertyGetterName();
1523 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001524 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001525 StringRef SetterName = DT.getObjCPropertySetterName();
1526 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001527 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001528 unsigned PropertyAttributes = 0;
1529 if (DT.isReadOnlyObjCProperty())
1530 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1531 if (DT.isReadWriteObjCProperty())
1532 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1533 if (DT.isAssignObjCProperty())
1534 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1535 if (DT.isRetainObjCProperty())
1536 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1537 if (DT.isCopyObjCProperty())
1538 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1539 if (DT.isNonAtomicObjCProperty())
1540 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1541 if (PropertyAttributes)
1542 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1543 PropertyAttributes);
1544 }
Devang Patel161b2f42011-04-12 23:21:44 +00001545 return MemberDie;
1546}