blob: f43a5bf84213e49a311a89f0334973f4009e75c4 [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//
10// This file contains support for writing dwarf compile unit.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "dwarfdebug"
15
16#include "DwarfCompileUnit.h"
17#include "DwarfDebug.h"
18#include "llvm/Constants.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000019#include "llvm/GlobalVariable.h"
20#include "llvm/Instructions.h"
Devang Patel161b2f42011-04-12 23:21:44 +000021#include "llvm/Analysis/DIBuilder.h"
Eric Christopherd61c34b2011-11-11 03:16:32 +000022#include "llvm/Support/Debug.h"
Devang Patel6f9d8ff2011-08-15 17:57:41 +000023#include "llvm/Target/Mangler.h"
Devang Patel161b2f42011-04-12 23:21:44 +000024#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetFrameLowering.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Target/TargetRegisterInfo.h"
28#include "llvm/ADT/APFloat.h"
29#include "llvm/Support/ErrorHandling.h"
30
31using namespace llvm;
32
33/// CompileUnit - Compile unit constructor.
34CompileUnit::CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW)
35 : ID(I), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) {
36 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
37}
38
39/// ~CompileUnit - Destructor for compile unit.
40CompileUnit::~CompileUnit() {
41 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
42 DIEBlocks[j]->~DIEBlock();
43}
44
45/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
46/// information entry.
47DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
48 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
49 return Value;
50}
51
52/// addUInt - Add an unsigned integer attribute data and value.
53///
54void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
55 unsigned Form, uint64_t Integer) {
56 if (!Form) Form = DIEInteger::BestForm(false, Integer);
57 DIEValue *Value = Integer == 1 ?
58 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
59 Die->addValue(Attribute, Form, Value);
60}
61
62/// addSInt - Add an signed integer attribute data and value.
63///
64void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
65 unsigned Form, int64_t Integer) {
66 if (!Form) Form = DIEInteger::BestForm(true, Integer);
67 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
68 Die->addValue(Attribute, Form, Value);
69}
70
Nick Lewycky6a7efcf2011-10-28 05:29:47 +000071/// addString - Add a string attribute data and value. We always emit a
72/// reference to the string pool instead of immediate strings so that DIEs have
73/// more predictable sizes.
Nick Lewycky390c40d2011-10-27 06:44:11 +000074void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
Nick Lewycky6a7efcf2011-10-28 05:29:47 +000075 MCSymbol *Symb = DD->getStringPoolEntry(String);
76 DIEValue *Value;
77 if (Asm->needsRelocationsForDwarfStringPool())
78 Value = new (DIEValueAllocator) DIELabel(Symb);
79 else {
80 MCSymbol *StringPool = DD->getStringPool();
81 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
Nick Lewycky390c40d2011-10-27 06:44:11 +000082 }
Nick Lewycky6a7efcf2011-10-28 05:29:47 +000083 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
Devang Patel161b2f42011-04-12 23:21:44 +000084}
85
86/// addLabel - Add a Dwarf label attribute data and value.
87///
88void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
89 const MCSymbol *Label) {
90 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
91 Die->addValue(Attribute, Form, Value);
92}
93
94/// addDelta - Add a label delta attribute data and value.
95///
96void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
97 const MCSymbol *Hi, const MCSymbol *Lo) {
98 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
99 Die->addValue(Attribute, Form, Value);
100}
101
102/// addDIEEntry - Add a DIE attribute data and value.
103///
104void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
105 DIE *Entry) {
106 Die->addValue(Attribute, Form, createDIEEntry(Entry));
107}
108
Devang Patel161b2f42011-04-12 23:21:44 +0000109/// addBlock - Add block data.
110///
111void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
112 DIEBlock *Block) {
113 Block->ComputeSize(Asm);
114 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
115 Die->addValue(Attribute, Block->BestForm(), Block);
116}
117
118/// addSourceLine - Add location information to specified debug information
119/// entry.
120void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
121 // Verify variable.
122 if (!V.Verify())
123 return;
124
125 unsigned Line = V.getLineNumber();
126 if (Line == 0)
127 return;
128 unsigned FileID = DD->GetOrCreateSourceID(V.getContext().getFilename(),
129 V.getContext().getDirectory());
130 assert(FileID && "Invalid file id");
131 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
132 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
133}
134
135/// addSourceLine - Add location information to specified debug information
136/// entry.
137void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
138 // Verify global variable.
139 if (!G.Verify())
140 return;
141
142 unsigned Line = G.getLineNumber();
143 if (Line == 0)
144 return;
Nick Lewycky746cb672011-10-26 22:55:33 +0000145 unsigned FileID = DD->GetOrCreateSourceID(G.getFilename(), G.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000146 assert(FileID && "Invalid file id");
147 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
148 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
149}
150
151/// addSourceLine - Add location information to specified debug information
152/// entry.
153void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
154 // Verify subprogram.
155 if (!SP.Verify())
156 return;
157 // If the line number is 0, don't add it.
158 if (SP.getLineNumber() == 0)
159 return;
160
161 unsigned Line = SP.getLineNumber();
162 if (!SP.getContext().Verify())
163 return;
Nick Lewycky746cb672011-10-26 22:55:33 +0000164 unsigned FileID = DD->GetOrCreateSourceID(SP.getFilename(),
165 SP.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000166 assert(FileID && "Invalid file id");
167 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
168 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
169}
170
171/// addSourceLine - Add location information to specified debug information
172/// entry.
173void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
174 // Verify type.
175 if (!Ty.Verify())
176 return;
177
178 unsigned Line = Ty.getLineNumber();
179 if (Line == 0 || !Ty.getContext().Verify())
180 return;
Nick Lewycky746cb672011-10-26 22:55:33 +0000181 unsigned FileID = DD->GetOrCreateSourceID(Ty.getFilename(),
182 Ty.getDirectory());
Devang Patel161b2f42011-04-12 23:21:44 +0000183 assert(FileID && "Invalid file id");
184 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
185 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
186}
187
188/// addSourceLine - Add location information to specified debug information
189/// entry.
190void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
191 // Verify namespace.
192 if (!NS.Verify())
193 return;
194
195 unsigned Line = NS.getLineNumber();
196 if (Line == 0)
197 return;
198 StringRef FN = NS.getFilename();
199
200 unsigned FileID = DD->GetOrCreateSourceID(FN, NS.getDirectory());
201 assert(FileID && "Invalid file id");
202 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
203 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
204}
205
Devang Patele1cdf842011-04-27 22:45:24 +0000206/// addVariableAddress - Add DW_AT_location attribute for a
207/// DbgVariable based on provided MachineLocation.
208void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
209 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000210 if (DV->variableHasComplexAddress())
211 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
212 else if (DV->isBlockByrefVariable())
213 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
214 else
215 addAddress(Die, dwarf::DW_AT_location, Location);
216}
217
Devang Patel116da2f2011-04-26 19:06:18 +0000218/// addRegisterOp - Add register operand.
219void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
220 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
221 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
222 if (DWReg < 32)
223 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
224 else {
225 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
226 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
227 }
228}
229
230/// addRegisterOffset - Add register offset.
231void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
232 int64_t Offset) {
233 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
234 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
235 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
236 if (Reg == TRI->getFrameRegister(*Asm->MF))
237 // If variable offset is based in frame register then use fbreg.
238 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
239 else if (DWReg < 32)
240 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
241 else {
242 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
243 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
244 }
245 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
246}
247
248/// addAddress - Add an address attribute to a die based on the location
249/// provided.
250void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
251 const MachineLocation &Location) {
252 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
253
254 if (Location.isReg())
255 addRegisterOp(Block, Location.getReg());
256 else
257 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
258
259 // Now attach the location information to the DIE.
260 addBlock(Die, Attribute, 0, Block);
261}
262
Devang Patel161b2f42011-04-12 23:21:44 +0000263/// addComplexAddress - Start with the address based on the location provided,
264/// and generate the DWARF information necessary to find the actual variable
265/// given the extra address information encoded in the DIVariable, starting from
266/// the starting location. Add the DWARF information to the die.
267///
268void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
269 unsigned Attribute,
270 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000271 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000272 unsigned N = DV->getNumAddrElements();
273 unsigned i = 0;
274 if (Location.isReg()) {
275 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
276 // If first address element is OpPlus then emit
277 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
278 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
279 i = 2;
280 } else
281 addRegisterOp(Block, Location.getReg());
282 }
Devang Patel116da2f2011-04-26 19:06:18 +0000283 else
284 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000285
Devang Patelc26f5442011-04-28 02:22:40 +0000286 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000287 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000288 if (Element == DIBuilder::OpPlus) {
289 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
290 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
291 } else if (Element == DIBuilder::OpDeref) {
292 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
293 } else llvm_unreachable("unknown DIBuilder Opcode");
294 }
295
296 // Now attach the location information to the DIE.
297 addBlock(Die, Attribute, 0, Block);
298}
299
300/* Byref variables, in Blocks, are declared by the programmer as "SomeType
301 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
302 gives the variable VarName either the struct, or a pointer to the struct, as
303 its type. This is necessary for various behind-the-scenes things the
304 compiler needs to do with by-reference variables in Blocks.
305
306 However, as far as the original *programmer* is concerned, the variable
307 should still have type 'SomeType', as originally declared.
308
309 The function getBlockByrefType dives into the __Block_byref_x_VarName
310 struct to find the original type of the variable, which is then assigned to
311 the variable's Debug Information Entry as its real type. So far, so good.
312 However now the debugger will expect the variable VarName to have the type
313 SomeType. So we need the location attribute for the variable to be an
314 expression that explains to the debugger how to navigate through the
315 pointers and struct to find the actual variable of type SomeType.
316
317 The following function does just that. We start by getting
318 the "normal" location for the variable. This will be the location
319 of either the struct __Block_byref_x_VarName or the pointer to the
320 struct __Block_byref_x_VarName.
321
322 The struct will look something like:
323
324 struct __Block_byref_x_VarName {
325 ... <various fields>
326 struct __Block_byref_x_VarName *forwarding;
327 ... <various other fields>
328 SomeType VarName;
329 ... <maybe more fields>
330 };
331
332 If we are given the struct directly (as our starting point) we
333 need to tell the debugger to:
334
335 1). Add the offset of the forwarding field.
336
337 2). Follow that pointer to get the real __Block_byref_x_VarName
338 struct to use (the real one may have been copied onto the heap).
339
340 3). Add the offset for the field VarName, to find the actual variable.
341
342 If we started with a pointer to the struct, then we need to
343 dereference that pointer first, before the other steps.
344 Translating this into DWARF ops, we will need to append the following
345 to the current location description for the variable:
346
347 DW_OP_deref -- optional, if we start with a pointer
348 DW_OP_plus_uconst <forward_fld_offset>
349 DW_OP_deref
350 DW_OP_plus_uconst <varName_fld_offset>
351
352 That is what this function does. */
353
354/// addBlockByrefAddress - Start with the address based on the location
355/// provided, and generate the DWARF information necessary to find the
356/// actual Block variable (navigating the Block struct) based on the
357/// starting location. Add the DWARF information to the die. For
358/// more information, read large comment just above here.
359///
360void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
361 unsigned Attribute,
362 const MachineLocation &Location) {
363 DIType Ty = DV->getType();
364 DIType TmpTy = Ty;
365 unsigned Tag = Ty.getTag();
366 bool isPointer = false;
367
368 StringRef varName = DV->getName();
369
370 if (Tag == dwarf::DW_TAG_pointer_type) {
371 DIDerivedType DTy = DIDerivedType(Ty);
372 TmpTy = DTy.getTypeDerivedFrom();
373 isPointer = true;
374 }
375
376 DICompositeType blockStruct = DICompositeType(TmpTy);
377
378 // Find the __forwarding field and the variable field in the __Block_byref
379 // struct.
380 DIArray Fields = blockStruct.getTypeArray();
381 DIDescriptor varField = DIDescriptor();
382 DIDescriptor forwardingField = DIDescriptor();
383
384 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
385 DIDescriptor Element = Fields.getElement(i);
386 DIDerivedType DT = DIDerivedType(Element);
387 StringRef fieldName = DT.getName();
388 if (fieldName == "__forwarding")
389 forwardingField = Element;
390 else if (fieldName == varName)
391 varField = Element;
392 }
393
394 // Get the offsets for the forwarding field and the variable field.
395 unsigned forwardingFieldOffset =
396 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
397 unsigned varFieldOffset =
398 DIDerivedType(varField).getOffsetInBits() >> 3;
399
400 // Decode the original location, and use that as the start of the byref
401 // variable's location.
402 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
403 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
404 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
405
406 if (Location.isReg()) {
407 if (Reg < 32)
408 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
409 else {
410 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
411 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
412 }
413 } else {
414 if (Reg < 32)
415 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
416 else {
417 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
418 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
419 }
420
421 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
422 }
423
424 // If we started with a pointer to the __Block_byref... struct, then
425 // the first thing we need to do is dereference the pointer (DW_OP_deref).
426 if (isPointer)
427 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
428
429 // Next add the offset for the '__forwarding' field:
430 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
431 // adding the offset if it's 0.
432 if (forwardingFieldOffset > 0) {
433 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
434 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
435 }
436
437 // Now dereference the __forwarding field to get to the real __Block_byref
438 // struct: DW_OP_deref.
439 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
440
441 // Now that we've got the real __Block_byref... struct, add the offset
442 // for the variable's field to get to the location of the actual variable:
443 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
444 if (varFieldOffset > 0) {
445 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
446 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
447 }
448
449 // Now attach the location information to the DIE.
450 addBlock(Die, Attribute, 0, Block);
451}
452
Devang Patel4ec14b02011-07-20 21:57:04 +0000453/// isTypeSigned - Return true if the type is signed.
454static bool isTypeSigned(DIType Ty, int *SizeInBits) {
455 if (Ty.isDerivedType())
456 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
457 if (Ty.isBasicType())
458 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
459 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
460 *SizeInBits = Ty.getSizeInBits();
461 return true;
462 }
463 return false;
464}
465
Devang Patel161b2f42011-04-12 23:21:44 +0000466/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000467bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
468 DIType Ty) {
Nick Lewycky746cb672011-10-26 22:55:33 +0000469 assert(MO.isImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000470 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patel4ec14b02011-07-20 21:57:04 +0000471 int SizeInBits = -1;
472 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
473 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
474 switch (SizeInBits) {
475 case 8: Form = dwarf::DW_FORM_data1; break;
476 case 16: Form = dwarf::DW_FORM_data2; break;
477 case 32: Form = dwarf::DW_FORM_data4; break;
478 case 64: Form = dwarf::DW_FORM_data8; break;
Devang Patel045c1d42011-05-27 19:13:26 +0000479 default: break;
480 }
Devang Patel4ec14b02011-07-20 21:57:04 +0000481 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
482 : addUInt(Block, 0, Form, MO.getImm());
Devang Patel72f0d9c2011-05-27 18:15:52 +0000483
Devang Patel161b2f42011-04-12 23:21:44 +0000484 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
485 return true;
486}
487
488/// addConstantFPValue - Add constant value entry in variable DIE.
489bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000490 assert (MO.isFPImm() && "Invalid machine operand!");
Devang Patel161b2f42011-04-12 23:21:44 +0000491 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
492 APFloat FPImm = MO.getFPImm()->getValueAPF();
493
494 // Get the raw data form of the floating point.
495 const APInt FltVal = FPImm.bitcastToAPInt();
496 const char *FltPtr = (const char*)FltVal.getRawData();
497
498 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
499 bool LittleEndian = Asm->getTargetData().isLittleEndian();
500 int Incr = (LittleEndian ? 1 : -1);
501 int Start = (LittleEndian ? 0 : NumBytes - 1);
502 int Stop = (LittleEndian ? NumBytes : -1);
503
504 // Output the constant to DWARF one byte at a time.
505 for (; Start != Stop; Start += Incr)
506 addUInt(Block, 0, dwarf::DW_FORM_data1,
507 (unsigned char)0xFF & FltPtr[Start]);
508
509 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
510 return true;
511}
512
513/// addConstantValue - Add constant value entry in variable DIE.
Devang Patel8594d422011-06-24 20:46:11 +0000514bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
Devang Patel161b2f42011-04-12 23:21:44 +0000515 bool Unsigned) {
Devang Pateld6a81362011-05-28 00:39:18 +0000516 unsigned CIBitWidth = CI->getBitWidth();
517 if (CIBitWidth <= 64) {
518 unsigned form = 0;
519 switch (CIBitWidth) {
520 case 8: form = dwarf::DW_FORM_data1; break;
521 case 16: form = dwarf::DW_FORM_data2; break;
522 case 32: form = dwarf::DW_FORM_data4; break;
523 case 64: form = dwarf::DW_FORM_data8; break;
524 default:
525 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
526 }
Devang Patel161b2f42011-04-12 23:21:44 +0000527 if (Unsigned)
Devang Pateld6a81362011-05-28 00:39:18 +0000528 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000529 else
Devang Pateld6a81362011-05-28 00:39:18 +0000530 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
Devang Patel161b2f42011-04-12 23:21:44 +0000531 return true;
532 }
533
534 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
535
536 // Get the raw data form of the large APInt.
537 const APInt Val = CI->getValue();
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000538 const uint64_t *Ptr64 = Val.getRawData();
Devang Patel161b2f42011-04-12 23:21:44 +0000539
540 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
541 bool LittleEndian = Asm->getTargetData().isLittleEndian();
Devang Patel161b2f42011-04-12 23:21:44 +0000542
543 // Output the constant to DWARF one byte at a time.
NAKAMURA Takumic3e48c32011-10-28 14:12:22 +0000544 for (int i = 0; i < NumBytes; i++) {
545 uint8_t c;
546 if (LittleEndian)
547 c = Ptr64[i / 8] >> (8 * (i & 7));
548 else
549 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
550 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
551 }
Devang Patel161b2f42011-04-12 23:21:44 +0000552
553 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
554 return true;
555}
556
557/// addTemplateParams - Add template parameters in buffer.
558void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
559 // Add template parameters.
560 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
561 DIDescriptor Element = TParams.getElement(i);
562 if (Element.isTemplateTypeParameter())
563 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
564 DITemplateTypeParameter(Element)));
565 else if (Element.isTemplateValueParameter())
566 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
567 DITemplateValueParameter(Element)));
568 }
Devang Patel161b2f42011-04-12 23:21:44 +0000569}
Nick Lewycky746cb672011-10-26 22:55:33 +0000570
Devang Patel161b2f42011-04-12 23:21:44 +0000571/// addToContextOwner - Add Die into the list of its context owner's children.
572void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
573 if (Context.isType()) {
574 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
575 ContextDIE->addChild(Die);
576 } else if (Context.isNameSpace()) {
577 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
578 ContextDIE->addChild(Die);
579 } else if (Context.isSubprogram()) {
Devang Pateldbc64af2011-08-15 17:24:54 +0000580 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
Devang Patel161b2f42011-04-12 23:21:44 +0000581 ContextDIE->addChild(Die);
582 } else if (DIE *ContextDIE = getDIE(Context))
583 ContextDIE->addChild(Die);
584 else
585 addDie(Die);
586}
587
588/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
589/// given DIType.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000590DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
591 DIType Ty(TyNode);
592 if (!Ty.Verify())
593 return NULL;
Devang Patel161b2f42011-04-12 23:21:44 +0000594 DIE *TyDIE = getDIE(Ty);
595 if (TyDIE)
596 return TyDIE;
597
598 // Create new type.
599 TyDIE = new DIE(dwarf::DW_TAG_base_type);
600 insertDIE(Ty, TyDIE);
601 if (Ty.isBasicType())
602 constructTypeDIE(*TyDIE, DIBasicType(Ty));
603 else if (Ty.isCompositeType())
604 constructTypeDIE(*TyDIE, DICompositeType(Ty));
605 else {
606 assert(Ty.isDerivedType() && "Unknown kind of DIType");
607 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
608 }
Eric Christopher1b3f9192011-11-10 19:52:58 +0000609 // If this is a named finished type then include it in the list of types
610 // for the accelerator tables.
611 if (!Ty.getName().empty() && !Ty.isForwardDecl())
612 addAccelType(Ty.getName(), TyDIE);
613
Devang Patel161b2f42011-04-12 23:21:44 +0000614 addToContextOwner(TyDIE, Ty.getContext());
615 return TyDIE;
616}
617
618/// addType - Add a new type attribute to the specified entity.
619void CompileUnit::addType(DIE *Entity, DIType Ty) {
620 if (!Ty.Verify())
621 return;
622
623 // Check for pre-existence.
624 DIEEntry *Entry = getDIEEntry(Ty);
625 // If it exists then use the existing value.
626 if (Entry) {
627 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
628 return;
629 }
630
631 // Construct type.
632 DIE *Buffer = getOrCreateTypeDIE(Ty);
633
634 // Set up proxy.
635 Entry = createDIEEntry(Buffer);
636 insertDIEEntry(Ty, Entry);
Devang Patel161b2f42011-04-12 23:21:44 +0000637 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
Devang Patele9ae06c2011-05-31 22:56:51 +0000638
639 // If this is a complete composite type then include it in the
640 // list of global types.
Devang Patelc20bdf12011-06-01 00:23:24 +0000641 addGlobalType(Ty);
Devang Patel66658e42011-05-31 23:30:30 +0000642}
643
644/// addGlobalType - Add a new global type to the compile unit.
645///
Devang Patelc20bdf12011-06-01 00:23:24 +0000646void CompileUnit::addGlobalType(DIType Ty) {
Devang Patele9ae06c2011-05-31 22:56:51 +0000647 DIDescriptor Context = Ty.getContext();
648 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
Devang Patel94c7ddb2011-08-16 22:09:43 +0000649 && (!Context || Context.isCompileUnit() || Context.isFile()
650 || Context.isNameSpace()))
Devang Patelc20bdf12011-06-01 00:23:24 +0000651 if (DIEEntry *Entry = getDIEEntry(Ty))
652 GlobalTypes[Ty.getName()] = Entry->getEntry();
Devang Patel161b2f42011-04-12 23:21:44 +0000653}
654
Devang Patel31c5d052011-05-06 16:57:54 +0000655/// addPubTypes - Add type for pubtypes section.
656void CompileUnit::addPubTypes(DISubprogram SP) {
657 DICompositeType SPTy = SP.getType();
658 unsigned SPTag = SPTy.getTag();
659 if (SPTag != dwarf::DW_TAG_subroutine_type)
660 return;
661
662 DIArray Args = SPTy.getTypeArray();
663 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
664 DIType ATy(Args.getElement(i));
665 if (!ATy.Verify())
666 continue;
Devang Patelc20bdf12011-06-01 00:23:24 +0000667 addGlobalType(ATy);
Devang Patel31c5d052011-05-06 16:57:54 +0000668 }
669}
670
Devang Patel161b2f42011-04-12 23:21:44 +0000671/// constructTypeDIE - Construct basic type die from DIBasicType.
672void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
673 // Get core information.
674 StringRef Name = BTy.getName();
Devang Patel161b2f42011-04-12 23:21:44 +0000675 // Add name if not anonymous or intermediate type.
676 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000677 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel734a67c2011-09-14 23:13:28 +0000678
679 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
680 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
681 // Unspecified types has only name, nothing else.
682 return;
683 }
684
685 Buffer.setTag(dwarf::DW_TAG_base_type);
Nick Lewycky746cb672011-10-26 22:55:33 +0000686 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
Devang Patel734a67c2011-09-14 23:13:28 +0000687 BTy.getEncoding());
688
Devang Patel161b2f42011-04-12 23:21:44 +0000689 uint64_t Size = BTy.getSizeInBits() >> 3;
690 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
691}
692
693/// constructTypeDIE - Construct derived type die from DIDerivedType.
694void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
695 // Get core information.
696 StringRef Name = DTy.getName();
697 uint64_t Size = DTy.getSizeInBits() >> 3;
698 unsigned Tag = DTy.getTag();
699
700 // FIXME - Workaround for templates.
701 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
702
703 Buffer.setTag(Tag);
704
705 // Map to main type, void will not have a type.
706 DIType FromTy = DTy.getTypeDerivedFrom();
707 addType(&Buffer, FromTy);
708
709 // Add name if not anonymous or intermediate type.
710 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000711 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000712
713 // Add size if non-zero (derived types might be zero-sized.)
714 if (Size)
715 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
716
717 // Add source line info if available and TyDesc is not a forward declaration.
718 if (!DTy.isForwardDecl())
719 addSourceLine(&Buffer, DTy);
720}
721
722/// constructTypeDIE - Construct type DIE from DICompositeType.
723void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
724 // Get core information.
725 StringRef Name = CTy.getName();
726
727 uint64_t Size = CTy.getSizeInBits() >> 3;
728 unsigned Tag = CTy.getTag();
729 Buffer.setTag(Tag);
730
731 switch (Tag) {
732 case dwarf::DW_TAG_vector_type:
733 case dwarf::DW_TAG_array_type:
734 constructArrayTypeDIE(Buffer, &CTy);
735 break;
736 case dwarf::DW_TAG_enumeration_type: {
737 DIArray Elements = CTy.getTypeArray();
738
739 // Add enumerators to enumeration type.
740 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
741 DIE *ElemDie = NULL;
742 DIDescriptor Enum(Elements.getElement(i));
743 if (Enum.isEnumerator()) {
744 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
745 Buffer.addChild(ElemDie);
746 }
747 }
748 }
749 break;
750 case dwarf::DW_TAG_subroutine_type: {
751 // Add return type.
752 DIArray Elements = CTy.getTypeArray();
753 DIDescriptor RTy = Elements.getElement(0);
754 addType(&Buffer, DIType(RTy));
755
756 bool isPrototyped = true;
757 // Add arguments.
758 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
759 DIDescriptor Ty = Elements.getElement(i);
760 if (Ty.isUnspecifiedParameter()) {
761 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
762 Buffer.addChild(Arg);
763 isPrototyped = false;
764 } else {
765 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
766 addType(Arg, DIType(Ty));
767 Buffer.addChild(Arg);
768 }
769 }
770 // Add prototype flag.
771 if (isPrototyped)
772 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
773 }
774 break;
775 case dwarf::DW_TAG_structure_type:
776 case dwarf::DW_TAG_union_type:
777 case dwarf::DW_TAG_class_type: {
778 // Add elements to structure type.
779 DIArray Elements = CTy.getTypeArray();
780
781 // A forward struct declared type may not have elements available.
782 unsigned N = Elements.getNumElements();
783 if (N == 0)
784 break;
785
786 // Add elements to structure type.
787 for (unsigned i = 0; i < N; ++i) {
788 DIDescriptor Element = Elements.getElement(i);
789 DIE *ElemDie = NULL;
790 if (Element.isSubprogram()) {
791 DISubprogram SP(Element);
Devang Pateldbc64af2011-08-15 17:24:54 +0000792 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
Devang Patel161b2f42011-04-12 23:21:44 +0000793 if (SP.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000794 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000795 dwarf::DW_ACCESS_protected);
796 else if (SP.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +0000797 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000798 dwarf::DW_ACCESS_private);
799 else
Nick Lewycky13aaca52011-12-13 05:09:11 +0000800 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +0000801 dwarf::DW_ACCESS_public);
802 if (SP.isExplicit())
803 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
804 }
805 else if (Element.isVariable()) {
806 DIVariable DV(Element);
807 ElemDie = new DIE(dwarf::DW_TAG_variable);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000808 addString(ElemDie, dwarf::DW_AT_name, DV.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000809 addType(ElemDie, DV.getType());
810 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
811 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
812 addSourceLine(ElemDie, DV);
813 } else if (Element.isDerivedType())
814 ElemDie = createMemberDIE(DIDerivedType(Element));
815 else
816 continue;
817 Buffer.addChild(ElemDie);
818 }
819
820 if (CTy.isAppleBlockExtension())
821 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
822
823 unsigned RLang = CTy.getRunTimeLang();
824 if (RLang)
825 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
826 dwarf::DW_FORM_data1, RLang);
827
828 DICompositeType ContainingType = CTy.getContainingType();
829 if (DIDescriptor(ContainingType).isCompositeType())
830 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
831 getOrCreateTypeDIE(DIType(ContainingType)));
832 else {
833 DIDescriptor Context = CTy.getContext();
834 addToContextOwner(&Buffer, Context);
835 }
836
Devang Patel201e6cd2011-05-12 21:29:42 +0000837 if (CTy.isObjcClassComplete())
838 addUInt(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type,
Devang Patelb11f80e2011-05-12 19:06:16 +0000839 dwarf::DW_FORM_flag, 1);
840
Eric Christopher1a8e8862011-12-16 23:42:42 +0000841 // Add template parameters to a class, structure or union types.
842 // FIXME: The support isn't in the metadata for this yet.
843 if (Tag == dwarf::DW_TAG_class_type ||
844 Tag == dwarf::DW_TAG_structure_type ||
845 Tag == dwarf::DW_TAG_union_type)
Devang Patel161b2f42011-04-12 23:21:44 +0000846 addTemplateParams(Buffer, CTy.getTemplateParams());
847
848 break;
849 }
850 default:
851 break;
852 }
853
854 // Add name if not anonymous or intermediate type.
855 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000856 addString(&Buffer, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +0000857
858 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
859 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
Nick Lewycky746cb672011-10-26 22:55:33 +0000860 {
Devang Patel161b2f42011-04-12 23:21:44 +0000861 // Add size if non-zero (derived types might be zero-sized.)
862 if (Size)
863 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
864 else {
865 // Add zero size if it is not a forward declaration.
866 if (CTy.isForwardDecl())
867 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
868 else
869 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
870 }
871
872 // Add source line info if available.
873 if (!CTy.isForwardDecl())
874 addSourceLine(&Buffer, CTy);
875 }
876}
877
878/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
879/// for the given DITemplateTypeParameter.
880DIE *
881CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
882 DIE *ParamDIE = getDIE(TP);
883 if (ParamDIE)
884 return ParamDIE;
885
886 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
887 addType(ParamDIE, TP.getType());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000888 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000889 return ParamDIE;
890}
891
892/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
893/// for the given DITemplateValueParameter.
894DIE *
895CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
896 DIE *ParamDIE = getDIE(TPV);
897 if (ParamDIE)
898 return ParamDIE;
899
900 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
901 addType(ParamDIE, TPV.getType());
902 if (!TPV.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000903 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
Devang Patel161b2f42011-04-12 23:21:44 +0000904 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
905 TPV.getValue());
906 return ParamDIE;
907}
908
Devang Patel31c5d052011-05-06 16:57:54 +0000909/// getOrCreateNameSpace - Create a DIE for DINameSpace.
910DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
911 DIE *NDie = getDIE(NS);
912 if (NDie)
913 return NDie;
914 NDie = new DIE(dwarf::DW_TAG_namespace);
915 insertDIE(NS, NDie);
Eric Christopher09ac3d82011-11-07 09:24:32 +0000916 if (!NS.getName().empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +0000917 addString(NDie, dwarf::DW_AT_name, NS.getName());
Eric Christopher09ac3d82011-11-07 09:24:32 +0000918 addAccelNamespace(NS.getName(), NDie);
919 } else
920 addAccelNamespace("(anonymous namespace)", NDie);
Devang Patel31c5d052011-05-06 16:57:54 +0000921 addSourceLine(NDie, NS);
922 addToContextOwner(NDie, NS.getContext());
923 return NDie;
924}
925
Devang Pateldbc64af2011-08-15 17:24:54 +0000926/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
927/// printer to not emit usual symbol prefix before the symbol name is used then
928/// return linkage name after skipping this special LLVM prefix.
929static StringRef getRealLinkageName(StringRef LinkageName) {
930 char One = '\1';
931 if (LinkageName.startswith(StringRef(&One, 1)))
932 return LinkageName.substr(1);
933 return LinkageName;
934}
935
936/// getOrCreateSubprogramDIE - Create new DIE using SP.
937DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
938 DIE *SPDie = getDIE(SP);
939 if (SPDie)
940 return SPDie;
941
Rafael Espindola01b55b42011-11-10 22:34:29 +0000942 DISubprogram SPDecl = SP.getFunctionDeclaration();
943 DIE *DeclDie = NULL;
944 if (SPDecl.isSubprogram()) {
945 DeclDie = getOrCreateSubprogramDIE(SPDecl);
946 }
947
Devang Pateldbc64af2011-08-15 17:24:54 +0000948 SPDie = new DIE(dwarf::DW_TAG_subprogram);
949
950 // DW_TAG_inlined_subroutine may refer to this DIE.
951 insertDIE(SP, SPDie);
952
953 // Add to context owner.
954 addToContextOwner(SPDie, SP.getContext());
955
956 // Add function template parameters.
957 addTemplateParams(*SPDie, SP.getTemplateParams());
958
959 StringRef LinkageName = SP.getLinkageName();
960 if (!LinkageName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000961 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
Nick Lewycky746cb672011-10-26 22:55:33 +0000962 getRealLinkageName(LinkageName));
Devang Pateldbc64af2011-08-15 17:24:54 +0000963
964 // If this DIE is going to refer declaration info using AT_specification
965 // then there is no need to add other attributes.
Rafael Espindola01b55b42011-11-10 22:34:29 +0000966 if (DeclDie) {
967 // Refer function declaration directly.
968 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
969 DeclDie);
970
Devang Pateldbc64af2011-08-15 17:24:54 +0000971 return SPDie;
Rafael Espindola01b55b42011-11-10 22:34:29 +0000972 }
Devang Pateldbc64af2011-08-15 17:24:54 +0000973
974 // Constructors and operators for anonymous aggregates do not have names.
975 if (!SP.getName().empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000976 addString(SPDie, dwarf::DW_AT_name, SP.getName());
Devang Pateldbc64af2011-08-15 17:24:54 +0000977
978 addSourceLine(SPDie, SP);
979
980 if (SP.isPrototyped())
981 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
982
983 // Add Return Type.
984 DICompositeType SPTy = SP.getType();
985 DIArray Args = SPTy.getTypeArray();
986 unsigned SPTag = SPTy.getTag();
987
988 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
989 addType(SPDie, SPTy);
990 else
991 addType(SPDie, DIType(Args.getElement(0)));
992
993 unsigned VK = SP.getVirtuality();
994 if (VK) {
Nick Lewycky798313d2011-12-14 00:56:07 +0000995 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
Devang Pateldbc64af2011-08-15 17:24:54 +0000996 DIEBlock *Block = getDIEBlock();
997 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
998 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
999 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1000 ContainingTypeMap.insert(std::make_pair(SPDie,
1001 SP.getContainingType()));
1002 }
1003
1004 if (!SP.isDefinition()) {
1005 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
1006
1007 // Add arguments. Do not add arguments for subprogram definition. They will
1008 // be handled while processing variables.
1009 DICompositeType SPTy = SP.getType();
1010 DIArray Args = SPTy.getTypeArray();
1011 unsigned SPTag = SPTy.getTag();
1012
1013 if (SPTag == dwarf::DW_TAG_subroutine_type)
1014 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1015 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1016 DIType ATy = DIType(DIType(Args.getElement(i)));
1017 addType(Arg, ATy);
1018 if (ATy.isArtificial())
1019 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1020 SPDie->addChild(Arg);
1021 }
1022 }
1023
1024 if (SP.isArtificial())
1025 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
1026
1027 if (!SP.isLocalToUnit())
1028 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1029
1030 if (SP.isOptimized())
1031 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1032
1033 if (unsigned isa = Asm->getISAEncoding()) {
1034 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1035 }
1036
1037 return SPDie;
1038}
1039
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001040// Return const expression if value is a GEP to access merged global
1041// constant. e.g.
1042// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1043static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1044 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1045 if (!CE || CE->getNumOperands() != 3 ||
1046 CE->getOpcode() != Instruction::GetElementPtr)
1047 return NULL;
1048
1049 // First operand points to a global struct.
1050 Value *Ptr = CE->getOperand(0);
1051 if (!isa<GlobalValue>(Ptr) ||
1052 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1053 return NULL;
1054
1055 // Second operand is zero.
1056 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1057 if (!CI || !CI->isZero())
1058 return NULL;
1059
1060 // Third operand is offset.
1061 if (!isa<ConstantInt>(CE->getOperand(2)))
1062 return NULL;
1063
1064 return CE;
1065}
1066
1067/// createGlobalVariableDIE - create global variable DIE.
1068void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001069 // Check for pre-existence.
Devang Patel49e2f032011-08-18 22:21:50 +00001070 if (getDIE(N))
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001071 return;
1072
Devang Patel49e2f032011-08-18 22:21:50 +00001073 DIGlobalVariable GV(N);
Devang Patel28bea082011-08-18 23:17:55 +00001074 if (!GV.Verify())
1075 return;
1076
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001077 DIE *VariableDIE = new DIE(GV.getTag());
Devang Patel49e2f032011-08-18 22:21:50 +00001078 // Add to map.
1079 insertDIE(N, VariableDIE);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001080
1081 // Add name.
Nick Lewycky390c40d2011-10-27 06:44:11 +00001082 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001083 StringRef LinkageName = GV.getLinkageName();
Devang Patel49e2f032011-08-18 22:21:50 +00001084 bool isGlobalVariable = GV.getGlobal() != NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001085 if (!LinkageName.empty() && isGlobalVariable)
Nick Lewycky746cb672011-10-26 22:55:33 +00001086 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
Nick Lewycky390c40d2011-10-27 06:44:11 +00001087 getRealLinkageName(LinkageName));
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001088 // Add type.
Devang Patel49e2f032011-08-18 22:21:50 +00001089 DIType GTy = GV.getType();
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001090 addType(VariableDIE, GTy);
1091
1092 // Add scoping info.
Eric Christopherdfa30e12011-11-09 05:24:07 +00001093 if (!GV.isLocalToUnit())
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001094 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
Eric Christopherdfa30e12011-11-09 05:24:07 +00001095
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001096 // Add line number info.
1097 addSourceLine(VariableDIE, GV);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001098 // Add to context owner.
1099 DIDescriptor GVContext = GV.getContext();
1100 addToContextOwner(VariableDIE, GVContext);
1101 // Add location.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001102 bool addToAccelTable = false;
Eric Christopherd61c34b2011-11-11 03:16:32 +00001103 DIE *VariableSpecDIE = NULL;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001104 if (isGlobalVariable) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001105 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001106 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1107 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1108 addLabel(Block, 0, dwarf::DW_FORM_udata,
1109 Asm->Mang->getSymbol(GV.getGlobal()));
1110 // Do not create specification DIE if context is either compile unit
1111 // or a subprogram.
Devang Patel1dd4e562011-09-21 23:41:11 +00001112 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001113 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1114 // Create specification DIE.
Eric Christopherd117fbb2011-11-11 01:55:22 +00001115 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001116 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1117 dwarf::DW_FORM_ref4, VariableDIE);
1118 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1119 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag,
1120 1);
1121 addDie(VariableSpecDIE);
1122 } else {
1123 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001124 }
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001125 } else if (const ConstantInt *CI =
1126 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1127 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1128 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001129 addToAccelTable = true;
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001130 // GV is a merged global.
1131 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1132 Value *Ptr = CE->getOperand(0);
1133 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1134 addLabel(Block, 0, dwarf::DW_FORM_udata,
1135 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1136 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1137 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
1138 addUInt(Block, 0, dwarf::DW_FORM_udata,
1139 Asm->getTargetData().getIndexedOffset(Ptr->getType(), Idx));
1140 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1141 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1142 }
1143
Eric Christopherd117fbb2011-11-11 01:55:22 +00001144 if (addToAccelTable) {
1145 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1146 addAccelName(GV.getName(), AddrDIE);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001147
Eric Christopherd117fbb2011-11-11 01:55:22 +00001148 // If the linkage name is different than the name, go ahead and output
1149 // that as well into the name table.
1150 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1151 addAccelName(GV.getLinkageName(), AddrDIE);
1152 }
Eric Christopher74d8a872011-11-08 21:56:23 +00001153
Devang Patel6f9d8ff2011-08-15 17:57:41 +00001154 return;
1155}
1156
Devang Patel161b2f42011-04-12 23:21:44 +00001157/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1158void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1159 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1160 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
Devang Patelce35d8b2011-11-17 23:43:15 +00001161 uint64_t L = SR.getLo();
1162 uint64_t H = SR.getHi();
Devang Patel161b2f42011-04-12 23:21:44 +00001163
1164 // The L value defines the lower bounds which is typically zero for C/C++. The
1165 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
1166 // of the array. If L > H then do not emit DW_AT_lower_bound and
1167 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
1168 // array has one element and in such case do not emit lower bound.
1169
1170 if (L > H) {
1171 Buffer.addChild(DW_Subrange);
1172 return;
1173 }
1174 if (L)
Devang Patelce35d8b2011-11-17 23:43:15 +00001175 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1176 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
Devang Patel161b2f42011-04-12 23:21:44 +00001177 Buffer.addChild(DW_Subrange);
1178}
1179
1180/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1181void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1182 DICompositeType *CTy) {
1183 Buffer.setTag(dwarf::DW_TAG_array_type);
1184 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1185 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1186
1187 // Emit derived type.
1188 addType(&Buffer, CTy->getTypeDerivedFrom());
1189 DIArray Elements = CTy->getTypeArray();
1190
1191 // Get an anonymous type for index type.
1192 DIE *IdxTy = getIndexTyDie();
1193 if (!IdxTy) {
1194 // Construct an anonymous type for index type.
1195 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1196 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1197 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1198 dwarf::DW_ATE_signed);
1199 addDie(IdxTy);
1200 setIndexTyDie(IdxTy);
1201 }
1202
1203 // Add subranges to array type.
1204 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1205 DIDescriptor Element = Elements.getElement(i);
1206 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1207 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1208 }
1209}
1210
1211/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1212DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1213 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1214 StringRef Name = ETy.getName();
Nick Lewycky390c40d2011-10-27 06:44:11 +00001215 addString(Enumerator, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001216 int64_t Value = ETy.getEnumValue();
1217 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1218 return Enumerator;
1219}
1220
Devang Pateldbc64af2011-08-15 17:24:54 +00001221/// constructContainingTypeDIEs - Construct DIEs for types that contain
1222/// vtables.
1223void CompileUnit::constructContainingTypeDIEs() {
1224 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1225 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1226 DIE *SPDie = CI->first;
1227 const MDNode *N = CI->second;
1228 if (!N) continue;
1229 DIE *NDie = getDIE(N);
1230 if (!NDie) continue;
1231 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1232 }
1233}
1234
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001235/// constructVariableDIE - Construct a DIE for the given DbgVariable.
1236DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1237 StringRef Name = DV->getName();
1238 if (Name.empty())
1239 return NULL;
1240
1241 // Translate tag to proper Dwarf tag.
1242 unsigned Tag = DV->getTag();
1243
1244 // Define variable debug information entry.
1245 DIE *VariableDie = new DIE(Tag);
1246 DbgVariable *AbsVar = DV->getAbstractVariable();
1247 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1248 if (AbsDIE)
1249 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1250 dwarf::DW_FORM_ref4, AbsDIE);
1251 else {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001252 addString(VariableDie, dwarf::DW_AT_name, Name);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001253 addSourceLine(VariableDie, DV->getVariable());
1254 addType(VariableDie, DV->getType());
1255 }
1256
1257 if (DV->isArtificial())
1258 addUInt(VariableDie, dwarf::DW_AT_artificial,
1259 dwarf::DW_FORM_flag, 1);
1260
1261 if (isScopeAbstract) {
1262 DV->setDIE(VariableDie);
1263 return VariableDie;
1264 }
1265
1266 // Add variable address.
1267
1268 unsigned Offset = DV->getDotDebugLocOffset();
1269 if (Offset != ~0U) {
1270 addLabel(VariableDie, dwarf::DW_AT_location,
1271 dwarf::DW_FORM_data4,
1272 Asm->GetTempSymbol("debug_loc", Offset));
1273 DV->setDIE(VariableDie);
1274 return VariableDie;
1275 }
1276
Eric Christopher8cf5e742011-10-03 15:49:20 +00001277 // Check if variable is described by a DBG_VALUE instruction.
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001278 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1279 bool updated = false;
1280 if (DVInsn->getNumOperands() == 3) {
1281 if (DVInsn->getOperand(0).isReg()) {
1282 const MachineOperand RegOp = DVInsn->getOperand(0);
1283 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1284 if (DVInsn->getOperand(1).isImm() &&
1285 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1286 unsigned FrameReg = 0;
1287 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1288 int Offset =
1289 TFI->getFrameIndexReference(*Asm->MF,
1290 DVInsn->getOperand(1).getImm(),
1291 FrameReg);
1292 MachineLocation Location(FrameReg, Offset);
1293 addVariableAddress(DV, VariableDie, Location);
1294
1295 } else if (RegOp.getReg())
1296 addVariableAddress(DV, VariableDie,
1297 MachineLocation(RegOp.getReg()));
1298 updated = true;
1299 }
1300 else if (DVInsn->getOperand(0).isImm())
1301 updated =
1302 addConstantValue(VariableDie, DVInsn->getOperand(0),
1303 DV->getType());
1304 else if (DVInsn->getOperand(0).isFPImm())
1305 updated =
1306 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1307 else if (DVInsn->getOperand(0).isCImm())
1308 updated =
1309 addConstantValue(VariableDie,
1310 DVInsn->getOperand(0).getCImm(),
1311 DV->getType().isUnsignedDIType());
1312 } else {
1313 addVariableAddress(DV, VariableDie,
1314 Asm->getDebugValueLocation(DVInsn));
1315 updated = true;
1316 }
1317 if (!updated) {
1318 // If variableDie is not updated then DBG_VALUE instruction does not
1319 // have valid variable info.
1320 delete VariableDie;
1321 return NULL;
1322 }
1323 DV->setDIE(VariableDie);
1324 return VariableDie;
1325 } else {
1326 // .. else use frame index.
1327 int FI = DV->getFrameIndex();
1328 if (FI != ~0) {
1329 unsigned FrameReg = 0;
1330 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1331 int Offset =
1332 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1333 MachineLocation Location(FrameReg, Offset);
1334 addVariableAddress(DV, VariableDie, Location);
1335 }
1336 }
1337
1338 DV->setDIE(VariableDie);
1339 return VariableDie;
1340}
1341
Devang Patel161b2f42011-04-12 23:21:44 +00001342/// createMemberDIE - Create new member DIE.
1343DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1344 DIE *MemberDie = new DIE(DT.getTag());
1345 StringRef Name = DT.getName();
1346 if (!Name.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001347 addString(MemberDie, dwarf::DW_AT_name, Name);
Devang Patel161b2f42011-04-12 23:21:44 +00001348
1349 addType(MemberDie, DT.getTypeDerivedFrom());
1350
1351 addSourceLine(MemberDie, DT);
1352
1353 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1354 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1355
1356 uint64_t Size = DT.getSizeInBits();
1357 uint64_t FieldSize = DT.getOriginalTypeSize();
1358
1359 if (Size != FieldSize) {
1360 // Handle bitfield.
1361 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1362 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1363
1364 uint64_t Offset = DT.getOffsetInBits();
1365 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1366 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1367 uint64_t FieldOffset = (HiMark - FieldSize);
1368 Offset -= FieldOffset;
1369
1370 // Maybe we need to work from the other end.
1371 if (Asm->getTargetData().isLittleEndian())
1372 Offset = FieldSize - (Offset + Size);
1373 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1374
1375 // Here WD_AT_data_member_location points to the anonymous
1376 // field that includes this bit field.
1377 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1378
1379 } else
1380 // This is not a bitfield.
1381 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1382
1383 if (DT.getTag() == dwarf::DW_TAG_inheritance
1384 && DT.isVirtual()) {
1385
1386 // For C++, virtual base classes are not at fixed offset. Use following
1387 // expression to extract appropriate offset from vtable.
1388 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1389
1390 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1391 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1392 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1393 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1394 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1395 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1396 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1397 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1398
1399 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1400 VBaseLocationDie);
1401 } else
1402 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1403
1404 if (DT.isProtected())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001405 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001406 dwarf::DW_ACCESS_protected);
1407 else if (DT.isPrivate())
Nick Lewycky13aaca52011-12-13 05:09:11 +00001408 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001409 dwarf::DW_ACCESS_private);
1410 // Otherwise C++ member and base classes are considered public.
Devang Patel94c7ddb2011-08-16 22:09:43 +00001411 else
Nick Lewycky13aaca52011-12-13 05:09:11 +00001412 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001413 dwarf::DW_ACCESS_public);
1414 if (DT.isVirtual())
Nick Lewycky798313d2011-12-14 00:56:07 +00001415 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
Devang Patel161b2f42011-04-12 23:21:44 +00001416 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001417
1418 // Objective-C properties.
1419 StringRef PropertyName = DT.getObjCPropertyName();
1420 if (!PropertyName.empty()) {
Nick Lewycky390c40d2011-10-27 06:44:11 +00001421 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
Devang Patele9db5e22011-04-16 00:11:51 +00001422 StringRef GetterName = DT.getObjCPropertyGetterName();
1423 if (!GetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001424 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001425 StringRef SetterName = DT.getObjCPropertySetterName();
1426 if (!SetterName.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +00001427 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
Devang Patele9db5e22011-04-16 00:11:51 +00001428 unsigned PropertyAttributes = 0;
1429 if (DT.isReadOnlyObjCProperty())
1430 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1431 if (DT.isReadWriteObjCProperty())
1432 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1433 if (DT.isAssignObjCProperty())
1434 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1435 if (DT.isRetainObjCProperty())
1436 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1437 if (DT.isCopyObjCProperty())
1438 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1439 if (DT.isNonAtomicObjCProperty())
1440 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1441 if (PropertyAttributes)
1442 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1443 PropertyAttributes);
1444 }
Devang Patel161b2f42011-04-12 23:21:44 +00001445 return MemberDie;
1446}