blob: 854d82c2c58c9be264f3a288413bcb99632a9d86 [file] [log] [blame]
Devang Patel161b2f42011-04-12 23:21:44 +00001//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Eric Christopher443c9ed2012-08-14 05:13:29 +000010// This file contains support for constructing a dwarf compile unit.
Devang Patel161b2f42011-04-12 23:21:44 +000011//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "DwarfAccelTable.h"
Devang Patel161b2f42011-04-12 23:21:44 +000018#include "DwarfDebug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/APFloat.h"
Bill Wendling16eeb6f2012-06-29 08:32:07 +000020#include "llvm/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/GlobalVariable.h"
24#include "llvm/IR/Instructions.h"
Eric Christopherd61c34b2011-11-11 03:16:32 +000025#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Support/ErrorHandling.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000027#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000028#include "llvm/Target/TargetFrameLowering.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetRegisterInfo.h"
Devang Patel161b2f42011-04-12 23:21:44 +000031
32using namespace llvm;
33
34/// CompileUnit - Compile unit constructor.
Eli Benderskyd4a05e02012-12-03 18:45:45 +000035CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, AsmPrinter *A,
Eric Christopher2e5d8702012-12-20 21:58:36 +000036 DwarfDebug *DW, DwarfUnits *DWU)
37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
38 IndexTyDie(0) {
Devang Patel161b2f42011-04-12 23:21:44 +000039 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
40}
41
42/// ~CompileUnit - Destructor for compile unit.
43CompileUnit::~CompileUnit() {
44 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
45 DIEBlocks[j]->~DIEBlock();
46}
47
48/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
49/// information entry.
50DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
51 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
52 return Value;
53}
54
Bill Wendling6afe4782012-12-06 07:55:19 +000055/// getDefaultLowerBound - Return the default lower bound for an array. If the
Bill Wendling222c2fd2012-12-06 07:38:10 +000056/// DWARF version doesn't handle the language, return -1.
Bill Wendling6afe4782012-12-06 07:55:19 +000057int64_t CompileUnit::getDefaultLowerBound() const {
Bill Wendling222c2fd2012-12-06 07:38:10 +000058 switch (Language) {
59 default:
60 break;
61
62 case dwarf::DW_LANG_C89:
63 case dwarf::DW_LANG_C99:
64 case dwarf::DW_LANG_C:
65 case dwarf::DW_LANG_C_plus_plus:
66 case dwarf::DW_LANG_ObjC:
67 case dwarf::DW_LANG_ObjC_plus_plus:
68 return 0;
69
70 case dwarf::DW_LANG_Fortran77:
71 case dwarf::DW_LANG_Fortran90:
72 case dwarf::DW_LANG_Fortran95:
73 return 1;
74
75 // The languages below have valid values only if the DWARF version >= 4.
76 case dwarf::DW_LANG_Java:
77 case dwarf::DW_LANG_Python:
78 case dwarf::DW_LANG_UPC:
79 case dwarf::DW_LANG_D:
80 if (dwarf::DWARF_VERSION >= 4)
81 return 0;
82 break;
83
84 case dwarf::DW_LANG_Ada83:
85 case dwarf::DW_LANG_Ada95:
86 case dwarf::DW_LANG_Cobol74:
87 case dwarf::DW_LANG_Cobol85:
88 case dwarf::DW_LANG_Modula2:
89 case dwarf::DW_LANG_Pascal83:
90 case dwarf::DW_LANG_PLI:
91 if (dwarf::DWARF_VERSION >= 4)
92 return 1;
93 break;
94 }
95
96 return -1;
97}
98
Eric Christopher873cf0a2012-08-24 01:14:27 +000099/// addFlag - Add a flag that is true.
100void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
101 if (!DD->useDarwinGDBCompat())
102 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
103 DIEIntegerOne);
104 else
105 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
106}
107
Devang Patel161b2f42011-04-12 23:21:44 +0000108/// addUInt - Add an unsigned integer attribute data and value.
109///
110void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
111 unsigned Form, uint64_t Integer) {
112 if (!Form) Form = DIEInteger::BestForm(false, Integer);
113 DIEValue *Value = Integer == 1 ?
114 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
115 Die->addValue(Attribute, Form, Value);
116}
117
118/// addSInt - Add an signed integer attribute data and value.
119///
120void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
121 unsigned Form, int64_t Integer) {
122 if (!Form) Form = DIEInteger::BestForm(true, Integer);
123 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
124 Die->addValue(Attribute, Form, Value);
125}
126
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000127/// addString - Add a string attribute data and value. We always emit a
128/// reference to the string pool instead of immediate strings so that DIEs have
129/// more predictable sizes.
Nick Lewycky390c40d2011-10-27 06:44:11 +0000130void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000131 MCSymbol *Symb = DU->getStringPoolEntry(String);
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000132 DIEValue *Value;
133 if (Asm->needsRelocationsForDwarfStringPool())
134 Value = new (DIEValueAllocator) DIELabel(Symb);
135 else {
Eric Christopher2e5d8702012-12-20 21:58:36 +0000136 MCSymbol *StringPool = DU->getStringPoolSym();
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000137 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000138 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +0000139 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +0000140}
141
142/// addLabel - Add a Dwarf label attribute data and value.
143///
144void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
145 const MCSymbol *Label) {
146 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
147 Die->addValue(Attribute, Form, Value);
148}
149
150/// addDelta - Add a label delta attribute data and value.
151///
152void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
153 const MCSymbol *Hi, const MCSymbol *Lo) {
154 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
155 Die->addValue(Attribute, Form, Value);
156}
157
158/// addDIEEntry - Add a DIE attribute data and value.
159///
160void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
161 DIE *Entry) {
162 Die->addValue(Attribute, Form, createDIEEntry(Entry));
163}
164
Devang Patel161b2f42011-04-12 23:21:44 +0000165/// addBlock - Add block data.
166///
167void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
168 DIEBlock *Block) {
169 Block->ComputeSize(Asm);
170 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
171 Die->addValue(Attribute, Block->BestForm(), Block);
172}
173
174/// addSourceLine - Add location information to specified debug information
175/// entry.
176void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
177 // Verify variable.
178 if (!V.Verify())
179 return;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000180
Devang Patel161b2f42011-04-12 23:21:44 +0000181 unsigned Line = V.getLineNumber();
182 if (Line == 0)
183 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000184 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
Devang Patel161b2f42011-04-12 23:21:44 +0000185 V.getContext().getDirectory());
186 assert(FileID && "Invalid file id");
187 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
188 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
189}
190
191/// addSourceLine - Add location information to specified debug information
192/// entry.
193void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
194 // Verify global variable.
195 if (!G.Verify())
196 return;
197
198 unsigned Line = G.getLineNumber();
199 if (Line == 0)
200 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000201 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000202 assert(FileID && "Invalid file id");
203 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
204 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
205}
206
207/// addSourceLine - Add location information to specified debug information
208/// entry.
209void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
210 // Verify subprogram.
211 if (!SP.Verify())
212 return;
Eric Christopher2125d5a2012-03-15 23:55:40 +0000213
Devang Patel161b2f42011-04-12 23:21:44 +0000214 // If the line number is 0, don't add it.
Eric Christopher2125d5a2012-03-15 23:55:40 +0000215 unsigned Line = SP.getLineNumber();
216 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000217 return;
218
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000219 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000220 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000221 assert(FileID && "Invalid file id");
222 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
223 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
224}
225
226/// addSourceLine - Add location information to specified debug information
227/// entry.
228void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
229 // Verify type.
230 if (!Ty.Verify())
231 return;
232
233 unsigned Line = Ty.getLineNumber();
Eric Christopher2125d5a2012-03-15 23:55:40 +0000234 if (Line == 0)
Devang Patel161b2f42011-04-12 23:21:44 +0000235 return;
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000236 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
Nick Lewycky746cb672011-10-26 22:55:33 +0000237 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000238 assert(FileID && "Invalid file id");
239 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
240 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
241}
242
243/// addSourceLine - Add location information to specified debug information
244/// entry.
Eric Christopherb8ca9882012-03-29 08:42:56 +0000245void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
246 // Verify type.
247 if (!Ty.Verify())
248 return;
249
250 unsigned Line = Ty.getLineNumber();
251 if (Line == 0)
252 return;
253 DIFile File = Ty.getFile();
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000254 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
Eric Christopher4d069bf2012-05-22 18:45:24 +0000255 File.getDirectory());
Eric Christopherb8ca9882012-03-29 08:42:56 +0000256 assert(FileID && "Invalid file id");
257 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
258 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
259}
260
261/// addSourceLine - Add location information to specified debug information
262/// entry.
Devang Patel161b2f42011-04-12 23:21:44 +0000263void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
264 // Verify namespace.
265 if (!NS.Verify())
266 return;
267
268 unsigned Line = NS.getLineNumber();
269 if (Line == 0)
270 return;
271 StringRef FN = NS.getFilename();
272
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000273 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000274 assert(FileID && "Invalid file id");
275 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
276 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
277}
278
Eric Christopher8b4310b2012-11-21 00:34:38 +0000279/// addVariableAddress - Add DW_AT_location attribute for a
Devang Patele1cdf842011-04-27 22:45:24 +0000280/// DbgVariable based on provided MachineLocation.
Eric Christopher8b4310b2012-11-21 00:34:38 +0000281void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
Devang Patele1cdf842011-04-27 22:45:24 +0000282 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000283 if (DV->variableHasComplexAddress())
284 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
285 else if (DV->isBlockByrefVariable())
286 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
287 else
288 addAddress(Die, dwarf::DW_AT_location, Location);
289}
290
Devang Patel116da2f2011-04-26 19:06:18 +0000291/// addRegisterOp - Add register operand.
292void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
293 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
294 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
295 if (DWReg < 32)
296 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
297 else {
298 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
299 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
300 }
301}
302
303/// addRegisterOffset - Add register offset.
304void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
305 int64_t Offset) {
306 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
307 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
308 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
309 if (Reg == TRI->getFrameRegister(*Asm->MF))
310 // If variable offset is based in frame register then use fbreg.
311 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
312 else if (DWReg < 32)
313 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
314 else {
315 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
316 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
317 }
318 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
319}
320
321/// addAddress - Add an address attribute to a die based on the location
322/// provided.
323void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
324 const MachineLocation &Location) {
325 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
326
327 if (Location.isReg())
328 addRegisterOp(Block, Location.getReg());
329 else
330 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
331
332 // Now attach the location information to the DIE.
333 addBlock(Die, Attribute, 0, Block);
334}
335
Devang Patel161b2f42011-04-12 23:21:44 +0000336/// addComplexAddress - Start with the address based on the location provided,
337/// and generate the DWARF information necessary to find the actual variable
338/// given the extra address information encoded in the DIVariable, starting from
339/// the starting location. Add the DWARF information to the die.
340///
341void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
342 unsigned Attribute,
343 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000344 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000345 unsigned N = DV->getNumAddrElements();
346 unsigned i = 0;
347 if (Location.isReg()) {
348 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
349 // If first address element is OpPlus then emit
350 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
351 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
352 i = 2;
353 } else
354 addRegisterOp(Block, Location.getReg());
355 }
Devang Patel116da2f2011-04-26 19:06:18 +0000356 else
357 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000358
Devang Patelc26f5442011-04-28 02:22:40 +0000359 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000360 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000361 if (Element == DIBuilder::OpPlus) {
362 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
363 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
364 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher50120762012-05-08 18:56:00 +0000365 if (!Location.isReg())
366 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
Devang Patel161b2f42011-04-12 23:21:44 +0000367 } else llvm_unreachable("unknown DIBuilder Opcode");
368 }
369
370 // Now attach the location information to the DIE.
371 addBlock(Die, Attribute, 0, Block);
372}
373
374/* Byref variables, in Blocks, are declared by the programmer as "SomeType
375 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
376 gives the variable VarName either the struct, or a pointer to the struct, as
377 its type. This is necessary for various behind-the-scenes things the
378 compiler needs to do with by-reference variables in Blocks.
379
380 However, as far as the original *programmer* is concerned, the variable
381 should still have type 'SomeType', as originally declared.
382
383 The function getBlockByrefType dives into the __Block_byref_x_VarName
384 struct to find the original type of the variable, which is then assigned to
385 the variable's Debug Information Entry as its real type. So far, so good.
386 However now the debugger will expect the variable VarName to have the type
387 SomeType. So we need the location attribute for the variable to be an
388 expression that explains to the debugger how to navigate through the
389 pointers and struct to find the actual variable of type SomeType.
390
391 The following function does just that. We start by getting
392 the "normal" location for the variable. This will be the location
393 of either the struct __Block_byref_x_VarName or the pointer to the
394 struct __Block_byref_x_VarName.
395
396 The struct will look something like:
397
398 struct __Block_byref_x_VarName {
399 ... <various fields>
400 struct __Block_byref_x_VarName *forwarding;
401 ... <various other fields>
402 SomeType VarName;
403 ... <maybe more fields>
404 };
405
406 If we are given the struct directly (as our starting point) we
407 need to tell the debugger to:
408
409 1). Add the offset of the forwarding field.
410
411 2). Follow that pointer to get the real __Block_byref_x_VarName
412 struct to use (the real one may have been copied onto the heap).
413
414 3). Add the offset for the field VarName, to find the actual variable.
415
416 If we started with a pointer to the struct, then we need to
417 dereference that pointer first, before the other steps.
418 Translating this into DWARF ops, we will need to append the following
419 to the current location description for the variable:
420
421 DW_OP_deref -- optional, if we start with a pointer
422 DW_OP_plus_uconst <forward_fld_offset>
423 DW_OP_deref
424 DW_OP_plus_uconst <varName_fld_offset>
425
426 That is what this function does. */
427
428/// addBlockByrefAddress - Start with the address based on the location
429/// provided, and generate the DWARF information necessary to find the
430/// actual Block variable (navigating the Block struct) based on the
431/// starting location. Add the DWARF information to the die. For
432/// more information, read large comment just above here.
433///
434void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
435 unsigned Attribute,
436 const MachineLocation &Location) {
437 DIType Ty = DV->getType();
438 DIType TmpTy = Ty;
439 unsigned Tag = Ty.getTag();
440 bool isPointer = false;
441
442 StringRef varName = DV->getName();
443
444 if (Tag == dwarf::DW_TAG_pointer_type) {
445 DIDerivedType DTy = DIDerivedType(Ty);
446 TmpTy = DTy.getTypeDerivedFrom();
447 isPointer = true;
448 }
449
450 DICompositeType blockStruct = DICompositeType(TmpTy);
451
452 // Find the __forwarding field and the variable field in the __Block_byref
453 // struct.
454 DIArray Fields = blockStruct.getTypeArray();
455 DIDescriptor varField = DIDescriptor();
456 DIDescriptor forwardingField = DIDescriptor();
457
458 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
459 DIDescriptor Element = Fields.getElement(i);
460 DIDerivedType DT = DIDerivedType(Element);
461 StringRef fieldName = DT.getName();
462 if (fieldName == "__forwarding")
463 forwardingField = Element;
464 else if (fieldName == varName)
465 varField = Element;
466 }
467
468 // Get the offsets for the forwarding field and the variable field.
469 unsigned forwardingFieldOffset =
470 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
471 unsigned varFieldOffset =
472 DIDerivedType(varField).getOffsetInBits() >> 3;
473
474 // Decode the original location, and use that as the start of the byref
475 // variable's location.
Devang Patel161b2f42011-04-12 23:21:44 +0000476 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
477
Eric Christophercaba2632012-07-04 02:02:18 +0000478 if (Location.isReg())
479 addRegisterOp(Block, Location.getReg());
480 else
481 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000482
483 // If we started with a pointer to the __Block_byref... struct, then
484 // the first thing we need to do is dereference the pointer (DW_OP_deref).
485 if (isPointer)
486 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
487
488 // Next add the offset for the '__forwarding' field:
489 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
490 // adding the offset if it's 0.
491 if (forwardingFieldOffset > 0) {
492 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
493 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
494 }
495
496 // Now dereference the __forwarding field to get to the real __Block_byref
497 // struct: DW_OP_deref.
498 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
499
500 // Now that we've got the real __Block_byref... struct, add the offset
501 // for the variable's field to get to the location of the actual variable:
502 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
503 if (varFieldOffset > 0) {
504 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
505 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
506 }
507
508 // Now attach the location information to the DIE.
509 addBlock(Die, Attribute, 0, Block);
510}
511
Devang Patel4ec14b02011-07-20 21:57:04 +0000512/// isTypeSigned - Return true if the type is signed.
513static bool isTypeSigned(DIType Ty, int *SizeInBits) {
514 if (Ty.isDerivedType())
515 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
516 if (Ty.isBasicType())
517 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
518 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
519 *SizeInBits = Ty.getSizeInBits();
520 return true;
521 }
522 return false;
523}
524
Devang Patel161b2f42011-04-12 23:21:44 +0000525/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000526bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
527 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000528 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000529 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000530 int SizeInBits = -1;
531 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
532 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
533 switch (SizeInBits) {
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;
Devang Patel045c1d42011-05-27 19:13:26 +0000538 default: break;
539 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000540 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
Devang Patel4ec14b02011-07-20 21:57:04 +0000541 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000542
Devang Patel161b2f42011-04-12 23:21:44 +0000543 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
544 return true;
545}
546
547/// addConstantFPValue - Add constant value entry in variable DIE.
548bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000549 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000550 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
551 APFloat FPImm = MO.getFPImm()->getValueAPF();
552
553 // Get the raw data form of the floating point.
554 const APInt FltVal = FPImm.bitcastToAPInt();
555 const char *FltPtr = (const char*)FltVal.getRawData();
556
557 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000558 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000559 int Incr = (LittleEndian ? 1 : -1);
560 int Start = (LittleEndian ? 0 : NumBytes - 1);
561 int Stop = (LittleEndian ? NumBytes : -1);
562
563 // Output the constant to DWARF one byte at a time.
564 for (; Start != Stop; Start += Incr)
565 addUInt(Block, 0, dwarf::DW_FORM_data1,
566 (unsigned char)0xFF & FltPtr[Start]);
567
568 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
569 return true;
570}
571
572/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000573bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000574 bool Unsigned) {
Devang Pateld6a81362011-05-28 00:39:18 +0000575 unsigned CIBitWidth = CI->getBitWidth();
576 if (CIBitWidth <= 64) {
577 unsigned form = 0;
578 switch (CIBitWidth) {
579 case 8: form = dwarf::DW_FORM_data1; break;
580 case 16: form = dwarf::DW_FORM_data2; break;
581 case 32: form = dwarf::DW_FORM_data4; break;
582 case 64: form = dwarf::DW_FORM_data8; break;
Eric Christopher8b4310b2012-11-21 00:34:38 +0000583 default:
Devang Pateld6a81362011-05-28 00:39:18 +0000584 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
585 }
Devang Patel161b2f42011-04-12 23:21:44 +0000586 if (Unsigned)
Devang Pateld6a81362011-05-28 00:39:18 +0000587 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000588 else
Devang Pateld6a81362011-05-28 00:39:18 +0000589 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000590 return true;
591 }
592
593 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
594
595 // Get the raw data form of the large APInt.
596 const APInt Val = CI->getValue();
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000597 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000598
599 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
Micah Villmow3574eca2012-10-08 16:38:25 +0000600 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000601
602 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000603 for (int i = 0; i < NumBytes; i++) {
604 uint8_t c;
605 if (LittleEndian)
606 c = Ptr64[i / 8] >> (8 * (i & 7));
607 else
608 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
609 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
610 }
Devang Patel161b2f42011-04-12 23:21:44 +0000611
612 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
613 return true;
614}
615
616/// addTemplateParams - Add template parameters in buffer.
617void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
618 // Add template parameters.
619 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
620 DIDescriptor Element = TParams.getElement(i);
621 if (Element.isTemplateTypeParameter())
622 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
623 DITemplateTypeParameter(Element)));
624 else if (Element.isTemplateValueParameter())
625 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
626 DITemplateValueParameter(Element)));
627 }
Devang Patel161b2f42011-04-12 23:21:44 +0000628}
Nick Lewycky746cb672011-10-26 22:55:33 +0000629
Devang Patel161b2f42011-04-12 23:21:44 +0000630/// addToContextOwner - Add Die into the list of its context owner's children.
631void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
632 if (Context.isType()) {
633 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
634 ContextDIE->addChild(Die);
635 } else if (Context.isNameSpace()) {
636 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
637 ContextDIE->addChild(Die);
638 } else if (Context.isSubprogram()) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000639 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
Devang Patel161b2f42011-04-12 23:21:44 +0000640 ContextDIE->addChild(Die);
641 } else if (DIE *ContextDIE = getDIE(Context))
642 ContextDIE->addChild(Die);
643 else
644 addDie(Die);
645}
646
647/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
648/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000649DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
650 DIType Ty(TyNode);
651 if (!Ty.Verify())
652 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000653 DIE *TyDIE = getDIE(Ty);
654 if (TyDIE)
655 return TyDIE;
656
657 // Create new type.
658 TyDIE = new DIE(dwarf::DW_TAG_base_type);
659 insertDIE(Ty, TyDIE);
660 if (Ty.isBasicType())
661 constructTypeDIE(*TyDIE, DIBasicType(Ty));
662 else if (Ty.isCompositeType())
663 constructTypeDIE(*TyDIE, DICompositeType(Ty));
664 else {
665 assert(Ty.isDerivedType() && "Unknown kind of DIType");
666 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
667 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000668 // If this is a named finished type then include it in the list of types
669 // for the accelerator tables.
Eric Christopherc36145f2012-01-06 04:35:23 +0000670 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
671 bool IsImplementation = 0;
672 if (Ty.isCompositeType()) {
673 DICompositeType CT(Ty);
Eric Christophere0167892012-01-06 23:03:37 +0000674 // A runtime language of 0 actually means C/C++ and that any
675 // non-negative value is some version of Objective-C/C++.
Eric Christopherc36145f2012-01-06 04:35:23 +0000676 IsImplementation = (CT.getRunTimeLang() == 0) ||
Eric Christophere2dc9332012-02-22 08:46:02 +0000677 CT.isObjcClassComplete();
Eric Christopherc36145f2012-01-06 04:35:23 +0000678 }
Eric Christophere0167892012-01-06 23:03:37 +0000679 unsigned Flags = IsImplementation ?
680 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
681 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
Eric Christopherc36145f2012-01-06 04:35:23 +0000682 }
Eric Christopher8b4310b2012-11-21 00:34:38 +0000683
Devang Patel161b2f42011-04-12 23:21:44 +0000684 addToContextOwner(TyDIE, Ty.getContext());
685 return TyDIE;
686}
687
688/// addType - Add a new type attribute to the specified entity.
Eric Christopher4d069bf2012-05-22 18:45:24 +0000689void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
Devang Patel161b2f42011-04-12 23:21:44 +0000690 if (!Ty.Verify())
691 return;
692
693 // Check for pre-existence.
694 DIEEntry *Entry = getDIEEntry(Ty);
695 // If it exists then use the existing value.
696 if (Entry) {
Eric Christopher663e0cf2012-03-28 07:34:31 +0000697 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000698 return;
699 }
700
701 // Construct type.
702 DIE *Buffer = getOrCreateTypeDIE(Ty);
703
704 // Set up proxy.
705 Entry = createDIEEntry(Buffer);
706 insertDIEEntry(Ty, Entry);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000707 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000708
709 // If this is a complete composite type then include it in the
710 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000711 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000712}
713
714/// addGlobalType - Add a new global type to the compile unit.
715///
Devang Patelc20bdf12011-06-01 00:23:24 +0000716void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000717 DIDescriptor Context = Ty.getContext();
Eric Christopher8b4310b2012-11-21 00:34:38 +0000718 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
719 && (!Context || Context.isCompileUnit() || Context.isFile()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000720 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000721 if (DIEEntry *Entry = getDIEEntry(Ty))
722 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000723}
724
Devang Patel31c5d052011-05-06 16:57:54 +0000725/// addPubTypes - Add type for pubtypes section.
726void CompileUnit::addPubTypes(DISubprogram SP) {
727 DICompositeType SPTy = SP.getType();
728 unsigned SPTag = SPTy.getTag();
729 if (SPTag != dwarf::DW_TAG_subroutine_type)
730 return;
731
732 DIArray Args = SPTy.getTypeArray();
733 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
734 DIType ATy(Args.getElement(i));
735 if (!ATy.Verify())
736 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000737 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000738 }
739}
740
Devang Patel161b2f42011-04-12 23:21:44 +0000741/// constructTypeDIE - Construct basic type die from DIBasicType.
742void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
743 // Get core information.
744 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000745 // Add name if not anonymous or intermediate type.
746 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000747 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000748
749 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
750 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
751 // Unspecified types has only name, nothing else.
752 return;
753 }
754
755 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000756 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel30d409c2012-02-07 23:33:58 +0000757 BTy.getEncoding());
Devang Patel734a67c2011-09-14 23:13:28 +0000758
Devang Patel161b2f42011-04-12 23:21:44 +0000759 uint64_t Size = BTy.getSizeInBits() >> 3;
760 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
761}
762
763/// constructTypeDIE - Construct derived type die from DIDerivedType.
764void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
765 // Get core information.
766 StringRef Name = DTy.getName();
767 uint64_t Size = DTy.getSizeInBits() >> 3;
768 unsigned Tag = DTy.getTag();
769
770 // FIXME - Workaround for templates.
771 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
772
773 Buffer.setTag(Tag);
774
775 // Map to main type, void will not have a type.
776 DIType FromTy = DTy.getTypeDerivedFrom();
777 addType(&Buffer, FromTy);
778
779 // Add name if not anonymous or intermediate type.
780 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000781 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000782
783 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopher35f225a2012-02-21 22:25:53 +0000784 if (Size && Tag != dwarf::DW_TAG_pointer_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000785 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
786
David Blaikie62fdfb52013-01-07 05:51:15 +0000787 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
788 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
789 getOrCreateTypeDIE(DTy.getClassType()));
Devang Patel161b2f42011-04-12 23:21:44 +0000790 // Add source line info if available and TyDesc is not a forward declaration.
791 if (!DTy.isForwardDecl())
792 addSourceLine(&Buffer, DTy);
793}
794
795/// constructTypeDIE - Construct type DIE from DICompositeType.
796void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
797 // Get core information.
798 StringRef Name = CTy.getName();
799
800 uint64_t Size = CTy.getSizeInBits() >> 3;
801 unsigned Tag = CTy.getTag();
802 Buffer.setTag(Tag);
803
804 switch (Tag) {
805 case dwarf::DW_TAG_vector_type:
806 case dwarf::DW_TAG_array_type:
807 constructArrayTypeDIE(Buffer, &CTy);
808 break;
809 case dwarf::DW_TAG_enumeration_type: {
810 DIArray Elements = CTy.getTypeArray();
811
812 // Add enumerators to enumeration type.
813 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
814 DIE *ElemDie = NULL;
815 DIDescriptor Enum(Elements.getElement(i));
816 if (Enum.isEnumerator()) {
817 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
818 Buffer.addChild(ElemDie);
819 }
820 }
Eric Christopherbb0f6ea2012-05-23 00:09:20 +0000821 DIType DTy = CTy.getTypeDerivedFrom();
822 if (DTy.Verify()) {
823 addType(&Buffer, DTy);
824 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
825 }
Devang Patel161b2f42011-04-12 23:21:44 +0000826 }
827 break;
828 case dwarf::DW_TAG_subroutine_type: {
829 // Add return type.
830 DIArray Elements = CTy.getTypeArray();
831 DIDescriptor RTy = Elements.getElement(0);
832 addType(&Buffer, DIType(RTy));
833
834 bool isPrototyped = true;
835 // Add arguments.
836 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
837 DIDescriptor Ty = Elements.getElement(i);
838 if (Ty.isUnspecifiedParameter()) {
839 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
840 Buffer.addChild(Arg);
841 isPrototyped = false;
842 } else {
843 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
844 addType(Arg, DIType(Ty));
845 Buffer.addChild(Arg);
846 }
847 }
Eric Christopher8b6fe6b2012-02-22 08:46:21 +0000848 // Add prototype flag if we're dealing with a C language and the
849 // function has been prototyped.
850 if (isPrototyped &&
Eric Christopher4d069bf2012-05-22 18:45:24 +0000851 (Language == dwarf::DW_LANG_C89 ||
852 Language == dwarf::DW_LANG_C99 ||
853 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +0000854 addFlag(&Buffer, dwarf::DW_AT_prototyped);
Devang Patel161b2f42011-04-12 23:21:44 +0000855 }
856 break;
857 case dwarf::DW_TAG_structure_type:
858 case dwarf::DW_TAG_union_type:
859 case dwarf::DW_TAG_class_type: {
860 // Add elements to structure type.
861 DIArray Elements = CTy.getTypeArray();
862
863 // A forward struct declared type may not have elements available.
864 unsigned N = Elements.getNumElements();
865 if (N == 0)
866 break;
867
868 // Add elements to structure type.
869 for (unsigned i = 0; i < N; ++i) {
870 DIDescriptor Element = Elements.getElement(i);
871 DIE *ElemDie = NULL;
872 if (Element.isSubprogram()) {
873 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000874 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000875 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000876 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000877 dwarf::DW_ACCESS_protected);
878 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000879 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000880 dwarf::DW_ACCESS_private);
Eric Christopher8b4310b2012-11-21 00:34:38 +0000881 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000882 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000883 dwarf::DW_ACCESS_public);
884 if (SP.isExplicit())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000885 addFlag(ElemDie, dwarf::DW_AT_explicit);
Devang Patel161b2f42011-04-12 23:21:44 +0000886 }
887 else if (Element.isVariable()) {
888 DIVariable DV(Element);
889 ElemDie = new DIE(dwarf::DW_TAG_variable);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000890 addString(ElemDie, dwarf::DW_AT_name, DV.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000891 addType(ElemDie, DV.getType());
Eric Christopher873cf0a2012-08-24 01:14:27 +0000892 addFlag(ElemDie, dwarf::DW_AT_declaration);
893 addFlag(ElemDie, dwarf::DW_AT_external);
Devang Patel161b2f42011-04-12 23:21:44 +0000894 addSourceLine(ElemDie, DV);
Eric Christopher663e0cf2012-03-28 07:34:31 +0000895 } else if (Element.isDerivedType()) {
Eric Christopher4d069bf2012-05-22 18:45:24 +0000896 DIDerivedType DDTy(Element);
897 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
898 ElemDie = new DIE(dwarf::DW_TAG_friend);
899 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
900 } else
901 ElemDie = createMemberDIE(DIDerivedType(Element));
Eric Christopher663e0cf2012-03-28 07:34:31 +0000902 } else if (Element.isObjCProperty()) {
Devang Patel30d409c2012-02-07 23:33:58 +0000903 DIObjCProperty Property(Element);
904 ElemDie = new DIE(Property.getTag());
905 StringRef PropertyName = Property.getObjCPropertyName();
906 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Eric Christopher4d069bf2012-05-22 18:45:24 +0000907 addType(ElemDie, Property.getType());
908 addSourceLine(ElemDie, Property);
Devang Patel30d409c2012-02-07 23:33:58 +0000909 StringRef GetterName = Property.getObjCPropertyGetterName();
910 if (!GetterName.empty())
911 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
912 StringRef SetterName = Property.getObjCPropertySetterName();
913 if (!SetterName.empty())
914 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
915 unsigned PropertyAttributes = 0;
Devang Patel9e11eb12012-02-04 01:30:32 +0000916 if (Property.isReadOnlyObjCProperty())
917 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
918 if (Property.isReadWriteObjCProperty())
919 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
920 if (Property.isAssignObjCProperty())
921 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
922 if (Property.isRetainObjCProperty())
923 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
924 if (Property.isCopyObjCProperty())
925 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
926 if (Property.isNonAtomicObjCProperty())
927 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
928 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +0000929 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patel9e11eb12012-02-04 01:30:32 +0000930 PropertyAttributes);
Devang Patel6588abf2012-02-06 17:49:43 +0000931
Devang Patel30d409c2012-02-07 23:33:58 +0000932 DIEEntry *Entry = getDIEEntry(Element);
933 if (!Entry) {
934 Entry = createDIEEntry(ElemDie);
935 insertDIEEntry(Element, Entry);
936 }
Devang Patel9e11eb12012-02-04 01:30:32 +0000937 } else
Devang Patel161b2f42011-04-12 23:21:44 +0000938 continue;
939 Buffer.addChild(ElemDie);
940 }
941
942 if (CTy.isAppleBlockExtension())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000943 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
Devang Patel161b2f42011-04-12 23:21:44 +0000944
Devang Patel161b2f42011-04-12 23:21:44 +0000945 DICompositeType ContainingType = CTy.getContainingType();
946 if (DIDescriptor(ContainingType).isCompositeType())
947 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
948 getOrCreateTypeDIE(DIType(ContainingType)));
949 else {
950 DIDescriptor Context = CTy.getContext();
951 addToContextOwner(&Buffer, Context);
952 }
953
Devang Patel201e6cd2011-05-12 21:29:42 +0000954 if (CTy.isObjcClassComplete())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000955 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
Devang Patelb11f80e2011-05-12 19:06:16 +0000956
Eric Christopher1a8e8862011-12-16 23:42:42 +0000957 // Add template parameters to a class, structure or union types.
958 // FIXME: The support isn't in the metadata for this yet.
959 if (Tag == dwarf::DW_TAG_class_type ||
960 Tag == dwarf::DW_TAG_structure_type ||
961 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000962 addTemplateParams(Buffer, CTy.getTemplateParams());
963
964 break;
965 }
966 default:
967 break;
968 }
969
970 // Add name if not anonymous or intermediate type.
971 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000972 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000973
Eric Christopher4a5d8392012-05-22 18:45:18 +0000974 if (Tag == dwarf::DW_TAG_enumeration_type ||
975 Tag == dwarf::DW_TAG_class_type ||
976 Tag == dwarf::DW_TAG_structure_type ||
977 Tag == dwarf::DW_TAG_union_type) {
Devang Patel161b2f42011-04-12 23:21:44 +0000978 // Add size if non-zero (derived types might be zero-sized.)
Eric Christopherfc4199b2012-06-01 00:22:32 +0000979 // TODO: Do we care about size for enum forward declarations?
Devang Patel161b2f42011-04-12 23:21:44 +0000980 if (Size)
981 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
Eric Christopherfc4199b2012-06-01 00:22:32 +0000982 else if (!CTy.isForwardDecl())
Devang Patel161b2f42011-04-12 23:21:44 +0000983 // Add zero size if it is not a forward declaration.
Eric Christopherfc4199b2012-06-01 00:22:32 +0000984 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
985
986 // If we're a forward decl, say so.
987 if (CTy.isForwardDecl())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000988 addFlag(&Buffer, dwarf::DW_AT_declaration);
Devang Patel161b2f42011-04-12 23:21:44 +0000989
990 // Add source line info if available.
991 if (!CTy.isForwardDecl())
992 addSourceLine(&Buffer, CTy);
Eric Christopher89388952012-03-07 00:15:19 +0000993
994 // No harm in adding the runtime language to the declaration.
995 unsigned RLang = CTy.getRunTimeLang();
996 if (RLang)
997 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
998 dwarf::DW_FORM_data1, RLang);
Devang Patel161b2f42011-04-12 23:21:44 +0000999 }
1000}
1001
Eric Christopher8b4310b2012-11-21 00:34:38 +00001002/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001003/// for the given DITemplateTypeParameter.
1004DIE *
1005CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1006 DIE *ParamDIE = getDIE(TP);
1007 if (ParamDIE)
1008 return ParamDIE;
1009
1010 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1011 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +00001012 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +00001013 return ParamDIE;
1014}
1015
Eric Christopher8b4310b2012-11-21 00:34:38 +00001016/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
Devang Patel161b2f42011-04-12 23:21:44 +00001017/// for the given DITemplateValueParameter.
1018DIE *
Eric Christopher4d069bf2012-05-22 18:45:24 +00001019CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
Devang Patel161b2f42011-04-12 23:21:44 +00001020 DIE *ParamDIE = getDIE(TPV);
1021 if (ParamDIE)
1022 return ParamDIE;
1023
1024 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1025 addType(ParamDIE, TPV.getType());
1026 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001027 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001028 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
Devang Patel161b2f42011-04-12 23:21:44 +00001029 TPV.getValue());
1030 return ParamDIE;
1031}
1032
Devang Patel31c5d052011-05-06 16:57:54 +00001033/// getOrCreateNameSpace - Create a DIE for DINameSpace.
1034DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1035 DIE *NDie = getDIE(NS);
1036 if (NDie)
1037 return NDie;
1038 NDie = new DIE(dwarf::DW_TAG_namespace);
1039 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001040 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001041 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +00001042 addAccelNamespace(NS.getName(), NDie);
1043 } else
1044 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +00001045 addSourceLine(NDie, NS);
1046 addToContextOwner(NDie, NS.getContext());
1047 return NDie;
1048}
1049
Devang Pateldbc64af2011-08-15 17:24:54 +00001050/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1051/// printer to not emit usual symbol prefix before the symbol name is used then
1052/// return linkage name after skipping this special LLVM prefix.
1053static StringRef getRealLinkageName(StringRef LinkageName) {
1054 char One = '\1';
1055 if (LinkageName.startswith(StringRef(&One, 1)))
1056 return LinkageName.substr(1);
1057 return LinkageName;
1058}
1059
1060/// getOrCreateSubprogramDIE - Create new DIE using SP.
1061DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1062 DIE *SPDie = getDIE(SP);
1063 if (SPDie)
1064 return SPDie;
1065
Peter Collingbourne27302f02012-05-27 18:36:44 +00001066 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1067
1068 // DW_TAG_inlined_subroutine may refer to this DIE.
1069 insertDIE(SP, SPDie);
1070
Rafael Espindola01b55b42011-11-10 22:34:29 +00001071 DISubprogram SPDecl = SP.getFunctionDeclaration();
1072 DIE *DeclDie = NULL;
1073 if (SPDecl.isSubprogram()) {
1074 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1075 }
1076
Devang Pateldbc64af2011-08-15 17:24:54 +00001077 // Add to context owner.
1078 addToContextOwner(SPDie, SP.getContext());
1079
1080 // Add function template parameters.
1081 addTemplateParams(*SPDie, SP.getTemplateParams());
1082
Eric Christophere9722e12012-04-16 23:54:23 +00001083 // Unfortunately this code needs to stay here instead of below the
1084 // AT_specification code in order to work around a bug in older
1085 // gdbs that requires the linkage name to resolve multiple template
1086 // functions.
Eric Christophercbbd5b12012-08-23 22:52:55 +00001087 // TODO: Remove this set of code when we get rid of the old gdb
1088 // compatibility.
Eric Christopher8d101c32012-03-15 08:19:33 +00001089 StringRef LinkageName = SP.getLinkageName();
Eric Christophercbbd5b12012-08-23 22:52:55 +00001090 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
Eric Christopher8d101c32012-03-15 08:19:33 +00001091 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1092 getRealLinkageName(LinkageName));
1093
Devang Pateldbc64af2011-08-15 17:24:54 +00001094 // If this DIE is going to refer declaration info using AT_specification
1095 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +00001096 if (DeclDie) {
1097 // Refer function declaration directly.
1098 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1099 DeclDie);
1100
Devang Pateldbc64af2011-08-15 17:24:54 +00001101 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +00001102 }
Devang Pateldbc64af2011-08-15 17:24:54 +00001103
Eric Christophercbbd5b12012-08-23 22:52:55 +00001104 // Add the linkage name if we have one.
1105 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1106 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1107 getRealLinkageName(LinkageName));
1108
Devang Pateldbc64af2011-08-15 17:24:54 +00001109 // Constructors and operators for anonymous aggregates do not have names.
1110 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001111 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +00001112
1113 addSourceLine(SPDie, SP);
1114
Eric Christopher8b6fe6b2012-02-22 08:46:21 +00001115 // Add the prototype if we have a prototype and we have a C like
1116 // language.
1117 if (SP.isPrototyped() &&
1118 (Language == dwarf::DW_LANG_C89 ||
1119 Language == dwarf::DW_LANG_C99 ||
1120 Language == dwarf::DW_LANG_ObjC))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001121 addFlag(SPDie, dwarf::DW_AT_prototyped);
Devang Pateldbc64af2011-08-15 17:24:54 +00001122
1123 // Add Return Type.
1124 DICompositeType SPTy = SP.getType();
1125 DIArray Args = SPTy.getTypeArray();
1126 unsigned SPTag = SPTy.getTag();
1127
1128 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1129 addType(SPDie, SPTy);
1130 else
1131 addType(SPDie, DIType(Args.getElement(0)));
1132
1133 unsigned VK = SP.getVirtuality();
1134 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +00001135 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +00001136 DIEBlock *Block = getDIEBlock();
1137 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1138 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1139 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1140 ContainingTypeMap.insert(std::make_pair(SPDie,
1141 SP.getContainingType()));
1142 }
1143
1144 if (!SP.isDefinition()) {
Eric Christopher873cf0a2012-08-24 01:14:27 +00001145 addFlag(SPDie, dwarf::DW_AT_declaration);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001146
Devang Pateldbc64af2011-08-15 17:24:54 +00001147 // Add arguments. Do not add arguments for subprogram definition. They will
1148 // be handled while processing variables.
1149 DICompositeType SPTy = SP.getType();
1150 DIArray Args = SPTy.getTypeArray();
1151 unsigned SPTag = SPTy.getTag();
1152
1153 if (SPTag == dwarf::DW_TAG_subroutine_type)
1154 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1155 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
Eric Christopher5c38de92012-09-10 23:33:57 +00001156 DIType ATy = DIType(Args.getElement(i));
Devang Pateldbc64af2011-08-15 17:24:54 +00001157 addType(Arg, ATy);
1158 if (ATy.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001159 addFlag(Arg, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001160 SPDie->addChild(Arg);
1161 }
1162 }
1163
1164 if (SP.isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001165 addFlag(SPDie, dwarf::DW_AT_artificial);
Devang Pateldbc64af2011-08-15 17:24:54 +00001166
1167 if (!SP.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001168 addFlag(SPDie, dwarf::DW_AT_external);
Devang Pateldbc64af2011-08-15 17:24:54 +00001169
1170 if (SP.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001171 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
Devang Pateldbc64af2011-08-15 17:24:54 +00001172
1173 if (unsigned isa = Asm->getISAEncoding()) {
1174 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1175 }
1176
1177 return SPDie;
1178}
1179
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001180// Return const expression if value is a GEP to access merged global
1181// constant. e.g.
1182// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1183static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1184 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1185 if (!CE || CE->getNumOperands() != 3 ||
1186 CE->getOpcode() != Instruction::GetElementPtr)
1187 return NULL;
1188
1189 // First operand points to a global struct.
1190 Value *Ptr = CE->getOperand(0);
1191 if (!isa<GlobalValue>(Ptr) ||
1192 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1193 return NULL;
1194
1195 // Second operand is zero.
1196 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1197 if (!CI || !CI->isZero())
1198 return NULL;
1199
1200 // Third operand is offset.
1201 if (!isa<ConstantInt>(CE->getOperand(2)))
1202 return NULL;
1203
1204 return CE;
1205}
1206
1207/// createGlobalVariableDIE - create global variable DIE.
1208void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001209 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001210 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001211 return;
1212
Devang Patel49e2f032011-08-18 22:21:50 +00001213 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001214 if (!GV.Verify())
1215 return;
1216
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001217 DIE *VariableDIE = new DIE(GV.getTag());
Devang Patel49e2f032011-08-18 22:21:50 +00001218 // Add to map.
1219 insertDIE(N, VariableDIE);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001220
1221 // Add name.
Nick Lewycky390c40d2011-10-27 06:44:11 +00001222 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001223 StringRef LinkageName = GV.getLinkageName();
Devang Patel49e2f032011-08-18 22:21:50 +00001224 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001225 if (!LinkageName.empty() && isGlobalVariable)
Nick Lewycky746cb672011-10-26 22:55:33 +00001226 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Nick Lewycky390c40d2011-10-27 06:44:11 +00001227 getRealLinkageName(LinkageName));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001228 // Add type.
Devang Patel49e2f032011-08-18 22:21:50 +00001229 DIType GTy = GV.getType();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001230 addType(VariableDIE, GTy);
1231
1232 // Add scoping info.
Eric Christopherdfa30e12011-11-09 05:24:07 +00001233 if (!GV.isLocalToUnit())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001234 addFlag(VariableDIE, dwarf::DW_AT_external);
Eric Christopherdfa30e12011-11-09 05:24:07 +00001235
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001236 // Add line number info.
1237 addSourceLine(VariableDIE, GV);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001238 // Add to context owner.
1239 DIDescriptor GVContext = GV.getContext();
1240 addToContextOwner(VariableDIE, GVContext);
1241 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001242 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001243 DIE *VariableSpecDIE = NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001244 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001245 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001246 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1247 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1248 addLabel(Block, 0, dwarf::DW_FORM_udata,
1249 Asm->Mang->getSymbol(GV.getGlobal()));
1250 // Do not create specification DIE if context is either compile unit
1251 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001252 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001253 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1254 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001255 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001256 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1257 dwarf::DW_FORM_ref4, VariableDIE);
1258 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher873cf0a2012-08-24 01:14:27 +00001259 addFlag(VariableDIE, dwarf::DW_AT_declaration);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001260 addDie(VariableSpecDIE);
1261 } else {
1262 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001263 }
Eric Christopher8b4310b2012-11-21 00:34:38 +00001264 } else if (const ConstantInt *CI =
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001265 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1266 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1267 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001268 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001269 // GV is a merged global.
1270 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1271 Value *Ptr = CE->getOperand(0);
1272 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1273 addLabel(Block, 0, dwarf::DW_FORM_udata,
1274 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1275 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1276 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
Eric Christopher8b4310b2012-11-21 00:34:38 +00001277 addUInt(Block, 0, dwarf::DW_FORM_udata,
Micah Villmow3574eca2012-10-08 16:38:25 +00001278 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001279 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1280 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1281 }
1282
Eric Christopherd117fbb2011-11-11 01:55:22 +00001283 if (addToAccelTable) {
1284 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1285 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001286
Eric Christopherd117fbb2011-11-11 01:55:22 +00001287 // If the linkage name is different than the name, go ahead and output
1288 // that as well into the name table.
1289 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1290 addAccelName(GV.getLinkageName(), AddrDIE);
1291 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001292
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001293 return;
1294}
1295
Devang Patel161b2f42011-04-12 23:21:44 +00001296/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
Eric Christopher4d069bf2012-05-22 18:45:24 +00001297void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1298 DIE *IndexTy) {
Devang Patel161b2f42011-04-12 23:21:44 +00001299 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1300 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patel161b2f42011-04-12 23:21:44 +00001301
Bill Wendling222c2fd2012-12-06 07:38:10 +00001302 // The LowerBound value defines the lower bounds which is typically zero for
1303 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1304 // Count == -1 then the array is unbounded and we do not emit
1305 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1306 // Count == 0, then the array has zero elements in which case we do not emit
1307 // an upper bound.
1308 int64_t LowerBound = SR.getLo();
Bill Wendling6afe4782012-12-06 07:55:19 +00001309 int64_t DefaultLowerBound = getDefaultLowerBound();
Bill Wendling9493dae2012-12-04 21:34:03 +00001310 int64_t Count = SR.getCount();
Devang Patel161b2f42011-04-12 23:21:44 +00001311
Bill Wendling6afe4782012-12-06 07:55:19 +00001312 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
Bill Wendling222c2fd2012-12-06 07:38:10 +00001313 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1314
1315 if (Count != -1 && Count != 0)
Bill Wendling9493dae2012-12-04 21:34:03 +00001316 // FIXME: An unbounded array should reference the expression that defines
1317 // the array.
Bill Wendling222c2fd2012-12-06 07:38:10 +00001318 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
Bill Wendling9493dae2012-12-04 21:34:03 +00001319
Devang Patel161b2f42011-04-12 23:21:44 +00001320 Buffer.addChild(DW_Subrange);
1321}
1322
1323/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1324void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1325 DICompositeType *CTy) {
1326 Buffer.setTag(dwarf::DW_TAG_array_type);
1327 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
Eric Christopher873cf0a2012-08-24 01:14:27 +00001328 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
Devang Patel161b2f42011-04-12 23:21:44 +00001329
1330 // Emit derived type.
1331 addType(&Buffer, CTy->getTypeDerivedFrom());
1332 DIArray Elements = CTy->getTypeArray();
1333
1334 // Get an anonymous type for index type.
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001335 // FIXME: This type should be passed down from the front end
1336 // as different languages may have different sizes for indexes.
Devang Patel161b2f42011-04-12 23:21:44 +00001337 DIE *IdxTy = getIndexTyDie();
1338 if (!IdxTy) {
1339 // Construct an anonymous type for index type.
1340 IdxTy = new DIE(dwarf::DW_TAG_base_type);
Eric Christopher8cab6ed2013-01-04 21:51:53 +00001341 addString(IdxTy, dwarf::DW_AT_name, "int");
Devang Patel161b2f42011-04-12 23:21:44 +00001342 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1343 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1344 dwarf::DW_ATE_signed);
1345 addDie(IdxTy);
1346 setIndexTyDie(IdxTy);
1347 }
1348
1349 // Add subranges to array type.
1350 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1351 DIDescriptor Element = Elements.getElement(i);
1352 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1353 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1354 }
1355}
1356
1357/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1358DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1359 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1360 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001361 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001362 int64_t Value = ETy.getEnumValue();
1363 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1364 return Enumerator;
1365}
1366
Devang Pateldbc64af2011-08-15 17:24:54 +00001367/// constructContainingTypeDIEs - Construct DIEs for types that contain
1368/// vtables.
1369void CompileUnit::constructContainingTypeDIEs() {
1370 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1371 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1372 DIE *SPDie = CI->first;
1373 const MDNode *N = CI->second;
1374 if (!N) continue;
1375 DIE *NDie = getDIE(N);
1376 if (!NDie) continue;
1377 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1378 }
1379}
1380
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001381/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1382DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1383 StringRef Name = DV->getName();
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001384
1385 // Translate tag to proper Dwarf tag.
1386 unsigned Tag = DV->getTag();
1387
1388 // Define variable debug information entry.
1389 DIE *VariableDie = new DIE(Tag);
1390 DbgVariable *AbsVar = DV->getAbstractVariable();
1391 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1392 if (AbsDIE)
1393 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1394 dwarf::DW_FORM_ref4, AbsDIE);
1395 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001396 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001397 addSourceLine(VariableDie, DV->getVariable());
1398 addType(VariableDie, DV->getType());
1399 }
1400
1401 if (DV->isArtificial())
Eric Christopher873cf0a2012-08-24 01:14:27 +00001402 addFlag(VariableDie, dwarf::DW_AT_artificial);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001403
1404 if (isScopeAbstract) {
1405 DV->setDIE(VariableDie);
1406 return VariableDie;
1407 }
1408
1409 // Add variable address.
1410
1411 unsigned Offset = DV->getDotDebugLocOffset();
1412 if (Offset != ~0U) {
1413 addLabel(VariableDie, dwarf::DW_AT_location,
1414 dwarf::DW_FORM_data4,
1415 Asm->GetTempSymbol("debug_loc", Offset));
1416 DV->setDIE(VariableDie);
1417 return VariableDie;
1418 }
1419
Eric Christopher8cf5e742011-10-03 15:49:20 +00001420 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001421 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1422 bool updated = false;
1423 if (DVInsn->getNumOperands() == 3) {
1424 if (DVInsn->getOperand(0).isReg()) {
1425 const MachineOperand RegOp = DVInsn->getOperand(0);
1426 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1427 if (DVInsn->getOperand(1).isImm() &&
1428 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1429 unsigned FrameReg = 0;
1430 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001431 int Offset =
1432 TFI->getFrameIndexReference(*Asm->MF,
1433 DVInsn->getOperand(1).getImm(),
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001434 FrameReg);
1435 MachineLocation Location(FrameReg, Offset);
1436 addVariableAddress(DV, VariableDie, Location);
Eric Christopher8b4310b2012-11-21 00:34:38 +00001437
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001438 } else if (RegOp.getReg())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001439 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001440 MachineLocation(RegOp.getReg()));
1441 updated = true;
1442 }
1443 else if (DVInsn->getOperand(0).isImm())
Eric Christopher8b4310b2012-11-21 00:34:38 +00001444 updated =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001445 addConstantValue(VariableDie, DVInsn->getOperand(0),
1446 DV->getType());
1447 else if (DVInsn->getOperand(0).isFPImm())
1448 updated =
1449 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1450 else if (DVInsn->getOperand(0).isCImm())
1451 updated =
Eric Christopher8b4310b2012-11-21 00:34:38 +00001452 addConstantValue(VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001453 DVInsn->getOperand(0).getCImm(),
1454 DV->getType().isUnsignedDIType());
1455 } else {
Eric Christopher8b4310b2012-11-21 00:34:38 +00001456 addVariableAddress(DV, VariableDie,
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001457 Asm->getDebugValueLocation(DVInsn));
1458 updated = true;
1459 }
1460 if (!updated) {
1461 // If variableDie is not updated then DBG_VALUE instruction does not
1462 // have valid variable info.
1463 delete VariableDie;
1464 return NULL;
1465 }
1466 DV->setDIE(VariableDie);
1467 return VariableDie;
1468 } else {
1469 // .. else use frame index.
1470 int FI = DV->getFrameIndex();
1471 if (FI != ~0) {
1472 unsigned FrameReg = 0;
1473 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
Eric Christopher8b4310b2012-11-21 00:34:38 +00001474 int Offset =
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001475 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1476 MachineLocation Location(FrameReg, Offset);
1477 addVariableAddress(DV, VariableDie, Location);
1478 }
1479 }
1480
1481 DV->setDIE(VariableDie);
1482 return VariableDie;
1483}
1484
Devang Patel161b2f42011-04-12 23:21:44 +00001485/// createMemberDIE - Create new member DIE.
1486DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1487 DIE *MemberDie = new DIE(DT.getTag());
1488 StringRef Name = DT.getName();
1489 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001490 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001491
1492 addType(MemberDie, DT.getTypeDerivedFrom());
1493
1494 addSourceLine(MemberDie, DT);
1495
1496 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1497 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1498
1499 uint64_t Size = DT.getSizeInBits();
1500 uint64_t FieldSize = DT.getOriginalTypeSize();
1501
1502 if (Size != FieldSize) {
1503 // Handle bitfield.
1504 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1505 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1506
1507 uint64_t Offset = DT.getOffsetInBits();
1508 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1509 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1510 uint64_t FieldOffset = (HiMark - FieldSize);
1511 Offset -= FieldOffset;
1512
1513 // Maybe we need to work from the other end.
Micah Villmow3574eca2012-10-08 16:38:25 +00001514 if (Asm->getDataLayout().isLittleEndian())
Devang Patel161b2f42011-04-12 23:21:44 +00001515 Offset = FieldSize - (Offset + Size);
1516 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1517
1518 // Here WD_AT_data_member_location points to the anonymous
1519 // field that includes this bit field.
1520 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1521
1522 } else
1523 // This is not a bitfield.
1524 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1525
1526 if (DT.getTag() == dwarf::DW_TAG_inheritance
1527 && DT.isVirtual()) {
1528
1529 // For C++, virtual base classes are not at fixed offset. Use following
1530 // expression to extract appropriate offset from vtable.
1531 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1532
1533 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1534 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1535 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1536 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1537 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1538 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1539 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1540 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1541
1542 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1543 VBaseLocationDie);
1544 } else
1545 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1546
1547 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001548 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001549 dwarf::DW_ACCESS_protected);
1550 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001551 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001552 dwarf::DW_ACCESS_private);
1553 // Otherwise C++ member and base classes are considered public.
Eric Christopher8b4310b2012-11-21 00:34:38 +00001554 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001555 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001556 dwarf::DW_ACCESS_public);
1557 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001558 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001559 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001560
1561 // Objective-C properties.
Devang Patel6588abf2012-02-06 17:49:43 +00001562 if (MDNode *PNode = DT.getObjCProperty())
1563 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
Eric Christopher8b4310b2012-11-21 00:34:38 +00001564 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
Devang Patel6588abf2012-02-06 17:49:43 +00001565 PropertyDie);
1566
David Blaikie01bc2b32012-12-13 22:43:07 +00001567 if (DT.isArtificial())
1568 addFlag(MemberDie, dwarf::DW_AT_artificial);
1569
Devang Patel9e11eb12012-02-04 01:30:32 +00001570 // This is only for backward compatibility.
Devang Patele9db5e22011-04-16 00:11:51 +00001571 StringRef PropertyName = DT.getObjCPropertyName();
1572 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001573 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001574 StringRef GetterName = DT.getObjCPropertyGetterName();
1575 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001576 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001577 StringRef SetterName = DT.getObjCPropertySetterName();
1578 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001579 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001580 unsigned PropertyAttributes = 0;
1581 if (DT.isReadOnlyObjCProperty())
1582 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1583 if (DT.isReadWriteObjCProperty())
1584 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1585 if (DT.isAssignObjCProperty())
1586 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1587 if (DT.isRetainObjCProperty())
1588 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1589 if (DT.isCopyObjCProperty())
1590 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1591 if (DT.isNonAtomicObjCProperty())
1592 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1593 if (PropertyAttributes)
Eric Christopher8b4310b2012-11-21 00:34:38 +00001594 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
Devang Patele9db5e22011-04-16 00:11:51 +00001595 PropertyAttributes);
1596 }
Devang Patel161b2f42011-04-12 23:21:44 +00001597 return MemberDie;
1598}