blob: 212b0865e9d42cae3b612c8d842c41170d033509 [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"
19#include "llvm/Analysis/DIBuilder.h"
20#include "llvm/Target/TargetData.h"
21#include "llvm/Target/TargetFrameLowering.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetRegisterInfo.h"
24#include "llvm/ADT/APFloat.h"
25#include "llvm/Support/ErrorHandling.h"
26
27using namespace llvm;
28
29/// CompileUnit - Compile unit constructor.
30CompileUnit::CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW)
31 : ID(I), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) {
32 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
33}
34
35/// ~CompileUnit - Destructor for compile unit.
36CompileUnit::~CompileUnit() {
37 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
38 DIEBlocks[j]->~DIEBlock();
39}
40
41/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
42/// information entry.
43DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
44 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
45 return Value;
46}
47
48/// addUInt - Add an unsigned integer attribute data and value.
49///
50void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
51 unsigned Form, uint64_t Integer) {
52 if (!Form) Form = DIEInteger::BestForm(false, Integer);
53 DIEValue *Value = Integer == 1 ?
54 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
55 Die->addValue(Attribute, Form, Value);
56}
57
58/// addSInt - Add an signed integer attribute data and value.
59///
60void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
61 unsigned Form, int64_t Integer) {
62 if (!Form) Form = DIEInteger::BestForm(true, Integer);
63 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
64 Die->addValue(Attribute, Form, Value);
65}
66
67/// addString - Add a string attribute data and value. DIEString only
68/// keeps string reference.
69void CompileUnit::addString(DIE *Die, unsigned Attribute, unsigned Form,
70 StringRef String) {
71 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
72 Die->addValue(Attribute, Form, Value);
73}
74
75/// addLabel - Add a Dwarf label attribute data and value.
76///
77void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
78 const MCSymbol *Label) {
79 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
80 Die->addValue(Attribute, Form, Value);
81}
82
83/// addDelta - Add a label delta attribute data and value.
84///
85void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
86 const MCSymbol *Hi, const MCSymbol *Lo) {
87 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
88 Die->addValue(Attribute, Form, Value);
89}
90
91/// addDIEEntry - Add a DIE attribute data and value.
92///
93void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
94 DIE *Entry) {
95 Die->addValue(Attribute, Form, createDIEEntry(Entry));
96}
97
98
99/// addBlock - Add block data.
100///
101void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
102 DIEBlock *Block) {
103 Block->ComputeSize(Asm);
104 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
105 Die->addValue(Attribute, Block->BestForm(), Block);
106}
107
108/// addSourceLine - Add location information to specified debug information
109/// entry.
110void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
111 // Verify variable.
112 if (!V.Verify())
113 return;
114
115 unsigned Line = V.getLineNumber();
116 if (Line == 0)
117 return;
118 unsigned FileID = DD->GetOrCreateSourceID(V.getContext().getFilename(),
119 V.getContext().getDirectory());
120 assert(FileID && "Invalid file id");
121 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
122 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
123}
124
125/// addSourceLine - Add location information to specified debug information
126/// entry.
127void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
128 // Verify global variable.
129 if (!G.Verify())
130 return;
131
132 unsigned Line = G.getLineNumber();
133 if (Line == 0)
134 return;
135 unsigned FileID = DD->GetOrCreateSourceID(G.getContext().getFilename(),
136 G.getContext().getDirectory());
137 assert(FileID && "Invalid file id");
138 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
139 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
140}
141
142/// addSourceLine - Add location information to specified debug information
143/// entry.
144void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
145 // Verify subprogram.
146 if (!SP.Verify())
147 return;
148 // If the line number is 0, don't add it.
149 if (SP.getLineNumber() == 0)
150 return;
151
152 unsigned Line = SP.getLineNumber();
153 if (!SP.getContext().Verify())
154 return;
155 unsigned FileID = DD->GetOrCreateSourceID(SP.getFilename(), SP.getDirectory());
156 assert(FileID && "Invalid file id");
157 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
158 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
159}
160
161/// addSourceLine - Add location information to specified debug information
162/// entry.
163void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
164 // Verify type.
165 if (!Ty.Verify())
166 return;
167
168 unsigned Line = Ty.getLineNumber();
169 if (Line == 0 || !Ty.getContext().Verify())
170 return;
171 unsigned FileID = DD->GetOrCreateSourceID(Ty.getFilename(), Ty.getDirectory());
172 assert(FileID && "Invalid file id");
173 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
174 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
175}
176
177/// addSourceLine - Add location information to specified debug information
178/// entry.
179void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
180 // Verify namespace.
181 if (!NS.Verify())
182 return;
183
184 unsigned Line = NS.getLineNumber();
185 if (Line == 0)
186 return;
187 StringRef FN = NS.getFilename();
188
189 unsigned FileID = DD->GetOrCreateSourceID(FN, NS.getDirectory());
190 assert(FileID && "Invalid file id");
191 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
192 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
193}
194
Devang Patele1cdf842011-04-27 22:45:24 +0000195/// addVariableAddress - Add DW_AT_location attribute for a
196/// DbgVariable based on provided MachineLocation.
197void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
198 MachineLocation Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000199 if (DV->variableHasComplexAddress())
200 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
201 else if (DV->isBlockByrefVariable())
202 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
203 else
204 addAddress(Die, dwarf::DW_AT_location, Location);
205}
206
Devang Patel116da2f2011-04-26 19:06:18 +0000207/// addRegisterOp - Add register operand.
208void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
209 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
210 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
211 if (DWReg < 32)
212 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
213 else {
214 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
215 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
216 }
217}
218
219/// addRegisterOffset - Add register offset.
220void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
221 int64_t Offset) {
222 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
223 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
224 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
225 if (Reg == TRI->getFrameRegister(*Asm->MF))
226 // If variable offset is based in frame register then use fbreg.
227 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
228 else if (DWReg < 32)
229 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
230 else {
231 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
232 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
233 }
234 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
235}
236
237/// addAddress - Add an address attribute to a die based on the location
238/// provided.
239void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
240 const MachineLocation &Location) {
241 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
242
243 if (Location.isReg())
244 addRegisterOp(Block, Location.getReg());
245 else
246 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
247
248 // Now attach the location information to the DIE.
249 addBlock(Die, Attribute, 0, Block);
250}
251
Devang Patel161b2f42011-04-12 23:21:44 +0000252/// addComplexAddress - Start with the address based on the location provided,
253/// and generate the DWARF information necessary to find the actual variable
254/// given the extra address information encoded in the DIVariable, starting from
255/// the starting location. Add the DWARF information to the die.
256///
257void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
258 unsigned Attribute,
259 const MachineLocation &Location) {
Devang Patel161b2f42011-04-12 23:21:44 +0000260 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelc26f5442011-04-28 02:22:40 +0000261 unsigned N = DV->getNumAddrElements();
262 unsigned i = 0;
263 if (Location.isReg()) {
264 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
265 // If first address element is OpPlus then emit
266 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
267 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
268 i = 2;
269 } else
270 addRegisterOp(Block, Location.getReg());
271 }
Devang Patel116da2f2011-04-26 19:06:18 +0000272 else
273 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
Devang Patel161b2f42011-04-12 23:21:44 +0000274
Devang Patelc26f5442011-04-28 02:22:40 +0000275 for (;i < N; ++i) {
Devang Patel161b2f42011-04-12 23:21:44 +0000276 uint64_t Element = DV->getAddrElement(i);
Devang Patel161b2f42011-04-12 23:21:44 +0000277 if (Element == DIBuilder::OpPlus) {
278 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
279 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
280 } else if (Element == DIBuilder::OpDeref) {
281 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
282 } else llvm_unreachable("unknown DIBuilder Opcode");
283 }
284
285 // Now attach the location information to the DIE.
286 addBlock(Die, Attribute, 0, Block);
287}
288
289/* Byref variables, in Blocks, are declared by the programmer as "SomeType
290 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
291 gives the variable VarName either the struct, or a pointer to the struct, as
292 its type. This is necessary for various behind-the-scenes things the
293 compiler needs to do with by-reference variables in Blocks.
294
295 However, as far as the original *programmer* is concerned, the variable
296 should still have type 'SomeType', as originally declared.
297
298 The function getBlockByrefType dives into the __Block_byref_x_VarName
299 struct to find the original type of the variable, which is then assigned to
300 the variable's Debug Information Entry as its real type. So far, so good.
301 However now the debugger will expect the variable VarName to have the type
302 SomeType. So we need the location attribute for the variable to be an
303 expression that explains to the debugger how to navigate through the
304 pointers and struct to find the actual variable of type SomeType.
305
306 The following function does just that. We start by getting
307 the "normal" location for the variable. This will be the location
308 of either the struct __Block_byref_x_VarName or the pointer to the
309 struct __Block_byref_x_VarName.
310
311 The struct will look something like:
312
313 struct __Block_byref_x_VarName {
314 ... <various fields>
315 struct __Block_byref_x_VarName *forwarding;
316 ... <various other fields>
317 SomeType VarName;
318 ... <maybe more fields>
319 };
320
321 If we are given the struct directly (as our starting point) we
322 need to tell the debugger to:
323
324 1). Add the offset of the forwarding field.
325
326 2). Follow that pointer to get the real __Block_byref_x_VarName
327 struct to use (the real one may have been copied onto the heap).
328
329 3). Add the offset for the field VarName, to find the actual variable.
330
331 If we started with a pointer to the struct, then we need to
332 dereference that pointer first, before the other steps.
333 Translating this into DWARF ops, we will need to append the following
334 to the current location description for the variable:
335
336 DW_OP_deref -- optional, if we start with a pointer
337 DW_OP_plus_uconst <forward_fld_offset>
338 DW_OP_deref
339 DW_OP_plus_uconst <varName_fld_offset>
340
341 That is what this function does. */
342
343/// addBlockByrefAddress - Start with the address based on the location
344/// provided, and generate the DWARF information necessary to find the
345/// actual Block variable (navigating the Block struct) based on the
346/// starting location. Add the DWARF information to the die. For
347/// more information, read large comment just above here.
348///
349void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
350 unsigned Attribute,
351 const MachineLocation &Location) {
352 DIType Ty = DV->getType();
353 DIType TmpTy = Ty;
354 unsigned Tag = Ty.getTag();
355 bool isPointer = false;
356
357 StringRef varName = DV->getName();
358
359 if (Tag == dwarf::DW_TAG_pointer_type) {
360 DIDerivedType DTy = DIDerivedType(Ty);
361 TmpTy = DTy.getTypeDerivedFrom();
362 isPointer = true;
363 }
364
365 DICompositeType blockStruct = DICompositeType(TmpTy);
366
367 // Find the __forwarding field and the variable field in the __Block_byref
368 // struct.
369 DIArray Fields = blockStruct.getTypeArray();
370 DIDescriptor varField = DIDescriptor();
371 DIDescriptor forwardingField = DIDescriptor();
372
373 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
374 DIDescriptor Element = Fields.getElement(i);
375 DIDerivedType DT = DIDerivedType(Element);
376 StringRef fieldName = DT.getName();
377 if (fieldName == "__forwarding")
378 forwardingField = Element;
379 else if (fieldName == varName)
380 varField = Element;
381 }
382
383 // Get the offsets for the forwarding field and the variable field.
384 unsigned forwardingFieldOffset =
385 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
386 unsigned varFieldOffset =
387 DIDerivedType(varField).getOffsetInBits() >> 3;
388
389 // Decode the original location, and use that as the start of the byref
390 // variable's location.
391 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
392 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
393 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
394
395 if (Location.isReg()) {
396 if (Reg < 32)
397 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
398 else {
399 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
400 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
401 }
402 } else {
403 if (Reg < 32)
404 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
405 else {
406 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
407 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
408 }
409
410 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
411 }
412
413 // If we started with a pointer to the __Block_byref... struct, then
414 // the first thing we need to do is dereference the pointer (DW_OP_deref).
415 if (isPointer)
416 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
417
418 // Next add the offset for the '__forwarding' field:
419 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
420 // adding the offset if it's 0.
421 if (forwardingFieldOffset > 0) {
422 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
423 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
424 }
425
426 // Now dereference the __forwarding field to get to the real __Block_byref
427 // struct: DW_OP_deref.
428 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
429
430 // Now that we've got the real __Block_byref... struct, add the offset
431 // for the variable's field to get to the location of the actual variable:
432 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
433 if (varFieldOffset > 0) {
434 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
435 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
436 }
437
438 // Now attach the location information to the DIE.
439 addBlock(Die, Attribute, 0, Block);
440}
441
Devang Patel161b2f42011-04-12 23:21:44 +0000442/// addConstantValue - Add constant value entry in variable DIE.
Devang Patelb58128e2011-05-27 16:45:18 +0000443bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
444 DIType Ty) {
Devang Patel161b2f42011-04-12 23:21:44 +0000445 assert (MO.isImm() && "Invalid machine operand!");
446 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
Devang Patelb58128e2011-05-27 16:45:18 +0000447 unsigned form = dwarf::DW_FORM_udata;
Devang Patel045c1d42011-05-27 19:13:26 +0000448 switch (Ty.getSizeInBits()) {
449 case 8: form = dwarf::DW_FORM_data1; break;
450 case 16: form = dwarf::DW_FORM_data2; break;
451 case 32: form = dwarf::DW_FORM_data4; break;
452 case 64: form = dwarf::DW_FORM_data8; break;
453 default: break;
454 }
455
Devang Patel72f0d9c2011-05-27 18:15:52 +0000456 DIBasicType BTy(Ty);
Devang Patel045c1d42011-05-27 19:13:26 +0000457 if (BTy.Verify() &&
458 (BTy.getEncoding() == dwarf::DW_ATE_signed
459 || BTy.getEncoding() == dwarf::DW_ATE_signed_char))
460 addSInt(Block, 0, form, MO.getImm());
461 else
Devang Patel72f0d9c2011-05-27 18:15:52 +0000462 addUInt(Block, 0, form, MO.getImm());
463
Devang Patel161b2f42011-04-12 23:21:44 +0000464 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
465 return true;
466}
467
468/// addConstantFPValue - Add constant value entry in variable DIE.
469bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
470 assert (MO.isFPImm() && "Invalid machine operand!");
471 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
472 APFloat FPImm = MO.getFPImm()->getValueAPF();
473
474 // Get the raw data form of the floating point.
475 const APInt FltVal = FPImm.bitcastToAPInt();
476 const char *FltPtr = (const char*)FltVal.getRawData();
477
478 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
479 bool LittleEndian = Asm->getTargetData().isLittleEndian();
480 int Incr = (LittleEndian ? 1 : -1);
481 int Start = (LittleEndian ? 0 : NumBytes - 1);
482 int Stop = (LittleEndian ? NumBytes : -1);
483
484 // Output the constant to DWARF one byte at a time.
485 for (; Start != Stop; Start += Incr)
486 addUInt(Block, 0, dwarf::DW_FORM_data1,
487 (unsigned char)0xFF & FltPtr[Start]);
488
489 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
490 return true;
491}
492
493/// addConstantValue - Add constant value entry in variable DIE.
494bool CompileUnit::addConstantValue(DIE *Die, ConstantInt *CI,
495 bool Unsigned) {
496 if (CI->getBitWidth() <= 64) {
497 if (Unsigned)
498 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
499 CI->getZExtValue());
500 else
501 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
502 CI->getSExtValue());
503 return true;
504 }
505
506 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
507
508 // Get the raw data form of the large APInt.
509 const APInt Val = CI->getValue();
510 const char *Ptr = (const char*)Val.getRawData();
511
512 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
513 bool LittleEndian = Asm->getTargetData().isLittleEndian();
514 int Incr = (LittleEndian ? 1 : -1);
515 int Start = (LittleEndian ? 0 : NumBytes - 1);
516 int Stop = (LittleEndian ? NumBytes : -1);
517
518 // Output the constant to DWARF one byte at a time.
519 for (; Start != Stop; Start += Incr)
520 addUInt(Block, 0, dwarf::DW_FORM_data1,
521 (unsigned char)0xFF & Ptr[Start]);
522
523 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
524 return true;
525}
526
527/// addTemplateParams - Add template parameters in buffer.
528void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
529 // Add template parameters.
530 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
531 DIDescriptor Element = TParams.getElement(i);
532 if (Element.isTemplateTypeParameter())
533 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
534 DITemplateTypeParameter(Element)));
535 else if (Element.isTemplateValueParameter())
536 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
537 DITemplateValueParameter(Element)));
538 }
539
540}
541/// addToContextOwner - Add Die into the list of its context owner's children.
542void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
543 if (Context.isType()) {
544 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
545 ContextDIE->addChild(Die);
546 } else if (Context.isNameSpace()) {
547 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
548 ContextDIE->addChild(Die);
549 } else if (Context.isSubprogram()) {
550 DIE *ContextDIE = DD->createSubprogramDIE(DISubprogram(Context));
551 ContextDIE->addChild(Die);
552 } else if (DIE *ContextDIE = getDIE(Context))
553 ContextDIE->addChild(Die);
554 else
555 addDie(Die);
556}
557
558/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
559/// given DIType.
560DIE *CompileUnit::getOrCreateTypeDIE(DIType Ty) {
561 DIE *TyDIE = getDIE(Ty);
562 if (TyDIE)
563 return TyDIE;
564
565 // Create new type.
566 TyDIE = new DIE(dwarf::DW_TAG_base_type);
567 insertDIE(Ty, TyDIE);
568 if (Ty.isBasicType())
569 constructTypeDIE(*TyDIE, DIBasicType(Ty));
570 else if (Ty.isCompositeType())
571 constructTypeDIE(*TyDIE, DICompositeType(Ty));
572 else {
573 assert(Ty.isDerivedType() && "Unknown kind of DIType");
574 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
575 }
576
577 addToContextOwner(TyDIE, Ty.getContext());
578 return TyDIE;
579}
580
581/// addType - Add a new type attribute to the specified entity.
582void CompileUnit::addType(DIE *Entity, DIType Ty) {
583 if (!Ty.Verify())
584 return;
585
586 // Check for pre-existence.
587 DIEEntry *Entry = getDIEEntry(Ty);
588 // If it exists then use the existing value.
589 if (Entry) {
590 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
591 return;
592 }
593
594 // Construct type.
595 DIE *Buffer = getOrCreateTypeDIE(Ty);
596
597 // Set up proxy.
598 Entry = createDIEEntry(Buffer);
599 insertDIEEntry(Ty, Entry);
600
601 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
602}
603
Devang Patel31c5d052011-05-06 16:57:54 +0000604/// addPubTypes - Add type for pubtypes section.
605void CompileUnit::addPubTypes(DISubprogram SP) {
606 DICompositeType SPTy = SP.getType();
607 unsigned SPTag = SPTy.getTag();
608 if (SPTag != dwarf::DW_TAG_subroutine_type)
609 return;
610
611 DIArray Args = SPTy.getTypeArray();
612 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
613 DIType ATy(Args.getElement(i));
614 if (!ATy.Verify())
615 continue;
616 DICompositeType CATy = getDICompositeType(ATy);
617 if (DIDescriptor(CATy).Verify() && !CATy.getName().empty()
618 && !CATy.isForwardDecl()) {
619 if (DIEEntry *Entry = getDIEEntry(CATy))
620 addGlobalType(CATy.getName(), Entry->getEntry());
621 }
622 }
623}
624
Devang Patel161b2f42011-04-12 23:21:44 +0000625/// constructTypeDIE - Construct basic type die from DIBasicType.
626void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
627 // Get core information.
628 StringRef Name = BTy.getName();
629 Buffer.setTag(dwarf::DW_TAG_base_type);
630 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
631 BTy.getEncoding());
632
633 // Add name if not anonymous or intermediate type.
634 if (!Name.empty())
635 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
636 uint64_t Size = BTy.getSizeInBits() >> 3;
637 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
638}
639
640/// constructTypeDIE - Construct derived type die from DIDerivedType.
641void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
642 // Get core information.
643 StringRef Name = DTy.getName();
644 uint64_t Size = DTy.getSizeInBits() >> 3;
645 unsigned Tag = DTy.getTag();
646
647 // FIXME - Workaround for templates.
648 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
649
650 Buffer.setTag(Tag);
651
652 // Map to main type, void will not have a type.
653 DIType FromTy = DTy.getTypeDerivedFrom();
654 addType(&Buffer, FromTy);
655
656 // Add name if not anonymous or intermediate type.
657 if (!Name.empty())
658 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
659
660 // Add size if non-zero (derived types might be zero-sized.)
661 if (Size)
662 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
663
664 // Add source line info if available and TyDesc is not a forward declaration.
665 if (!DTy.isForwardDecl())
666 addSourceLine(&Buffer, DTy);
667}
668
669/// constructTypeDIE - Construct type DIE from DICompositeType.
670void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
671 // Get core information.
672 StringRef Name = CTy.getName();
673
674 uint64_t Size = CTy.getSizeInBits() >> 3;
675 unsigned Tag = CTy.getTag();
676 Buffer.setTag(Tag);
677
678 switch (Tag) {
679 case dwarf::DW_TAG_vector_type:
680 case dwarf::DW_TAG_array_type:
681 constructArrayTypeDIE(Buffer, &CTy);
682 break;
683 case dwarf::DW_TAG_enumeration_type: {
684 DIArray Elements = CTy.getTypeArray();
685
686 // Add enumerators to enumeration type.
687 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
688 DIE *ElemDie = NULL;
689 DIDescriptor Enum(Elements.getElement(i));
690 if (Enum.isEnumerator()) {
691 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
692 Buffer.addChild(ElemDie);
693 }
694 }
695 }
696 break;
697 case dwarf::DW_TAG_subroutine_type: {
698 // Add return type.
699 DIArray Elements = CTy.getTypeArray();
700 DIDescriptor RTy = Elements.getElement(0);
701 addType(&Buffer, DIType(RTy));
702
703 bool isPrototyped = true;
704 // Add arguments.
705 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
706 DIDescriptor Ty = Elements.getElement(i);
707 if (Ty.isUnspecifiedParameter()) {
708 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
709 Buffer.addChild(Arg);
710 isPrototyped = false;
711 } else {
712 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
713 addType(Arg, DIType(Ty));
714 Buffer.addChild(Arg);
715 }
716 }
717 // Add prototype flag.
718 if (isPrototyped)
719 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
720 }
721 break;
722 case dwarf::DW_TAG_structure_type:
723 case dwarf::DW_TAG_union_type:
724 case dwarf::DW_TAG_class_type: {
725 // Add elements to structure type.
726 DIArray Elements = CTy.getTypeArray();
727
728 // A forward struct declared type may not have elements available.
729 unsigned N = Elements.getNumElements();
730 if (N == 0)
731 break;
732
733 // Add elements to structure type.
734 for (unsigned i = 0; i < N; ++i) {
735 DIDescriptor Element = Elements.getElement(i);
736 DIE *ElemDie = NULL;
737 if (Element.isSubprogram()) {
738 DISubprogram SP(Element);
739 ElemDie = DD->createSubprogramDIE(DISubprogram(Element));
740 if (SP.isProtected())
741 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
742 dwarf::DW_ACCESS_protected);
743 else if (SP.isPrivate())
744 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
745 dwarf::DW_ACCESS_private);
746 else
747 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
748 dwarf::DW_ACCESS_public);
749 if (SP.isExplicit())
750 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
751 }
752 else if (Element.isVariable()) {
753 DIVariable DV(Element);
754 ElemDie = new DIE(dwarf::DW_TAG_variable);
755 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
756 DV.getName());
757 addType(ElemDie, DV.getType());
758 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
759 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
760 addSourceLine(ElemDie, DV);
761 } else if (Element.isDerivedType())
762 ElemDie = createMemberDIE(DIDerivedType(Element));
763 else
764 continue;
765 Buffer.addChild(ElemDie);
766 }
767
768 if (CTy.isAppleBlockExtension())
769 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
770
771 unsigned RLang = CTy.getRunTimeLang();
772 if (RLang)
773 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
774 dwarf::DW_FORM_data1, RLang);
775
776 DICompositeType ContainingType = CTy.getContainingType();
777 if (DIDescriptor(ContainingType).isCompositeType())
778 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
779 getOrCreateTypeDIE(DIType(ContainingType)));
780 else {
781 DIDescriptor Context = CTy.getContext();
782 addToContextOwner(&Buffer, Context);
783 }
784
Devang Patel201e6cd2011-05-12 21:29:42 +0000785 if (CTy.isObjcClassComplete())
786 addUInt(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type,
Devang Patelb11f80e2011-05-12 19:06:16 +0000787 dwarf::DW_FORM_flag, 1);
788
Devang Patel161b2f42011-04-12 23:21:44 +0000789 if (Tag == dwarf::DW_TAG_class_type)
790 addTemplateParams(Buffer, CTy.getTemplateParams());
791
792 break;
793 }
794 default:
795 break;
796 }
797
798 // Add name if not anonymous or intermediate type.
799 if (!Name.empty())
800 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
801
802 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
803 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
804 {
805 // Add size if non-zero (derived types might be zero-sized.)
806 if (Size)
807 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
808 else {
809 // Add zero size if it is not a forward declaration.
810 if (CTy.isForwardDecl())
811 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
812 else
813 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
814 }
815
816 // Add source line info if available.
817 if (!CTy.isForwardDecl())
818 addSourceLine(&Buffer, CTy);
819 }
820}
821
822/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
823/// for the given DITemplateTypeParameter.
824DIE *
825CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
826 DIE *ParamDIE = getDIE(TP);
827 if (ParamDIE)
828 return ParamDIE;
829
830 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
831 addType(ParamDIE, TP.getType());
832 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName());
833 return ParamDIE;
834}
835
836/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
837/// for the given DITemplateValueParameter.
838DIE *
839CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
840 DIE *ParamDIE = getDIE(TPV);
841 if (ParamDIE)
842 return ParamDIE;
843
844 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
845 addType(ParamDIE, TPV.getType());
846 if (!TPV.getName().empty())
847 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName());
848 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
849 TPV.getValue());
850 return ParamDIE;
851}
852
Devang Patel31c5d052011-05-06 16:57:54 +0000853/// getOrCreateNameSpace - Create a DIE for DINameSpace.
854DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
855 DIE *NDie = getDIE(NS);
856 if (NDie)
857 return NDie;
858 NDie = new DIE(dwarf::DW_TAG_namespace);
859 insertDIE(NS, NDie);
860 if (!NS.getName().empty())
861 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
862 addSourceLine(NDie, NS);
863 addToContextOwner(NDie, NS.getContext());
864 return NDie;
865}
866
Devang Patel161b2f42011-04-12 23:21:44 +0000867/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
868void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
869 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
870 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
871 int64_t L = SR.getLo();
872 int64_t H = SR.getHi();
873
874 // The L value defines the lower bounds which is typically zero for C/C++. The
875 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
876 // of the array. If L > H then do not emit DW_AT_lower_bound and
877 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
878 // array has one element and in such case do not emit lower bound.
879
880 if (L > H) {
881 Buffer.addChild(DW_Subrange);
882 return;
883 }
884 if (L)
885 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
886 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
887 Buffer.addChild(DW_Subrange);
888}
889
890/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
891void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
892 DICompositeType *CTy) {
893 Buffer.setTag(dwarf::DW_TAG_array_type);
894 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
895 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
896
897 // Emit derived type.
898 addType(&Buffer, CTy->getTypeDerivedFrom());
899 DIArray Elements = CTy->getTypeArray();
900
901 // Get an anonymous type for index type.
902 DIE *IdxTy = getIndexTyDie();
903 if (!IdxTy) {
904 // Construct an anonymous type for index type.
905 IdxTy = new DIE(dwarf::DW_TAG_base_type);
906 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
907 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
908 dwarf::DW_ATE_signed);
909 addDie(IdxTy);
910 setIndexTyDie(IdxTy);
911 }
912
913 // Add subranges to array type.
914 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
915 DIDescriptor Element = Elements.getElement(i);
916 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
917 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
918 }
919}
920
921/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
922DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
923 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
924 StringRef Name = ETy.getName();
925 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
926 int64_t Value = ETy.getEnumValue();
927 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
928 return Enumerator;
929}
930
931/// createMemberDIE - Create new member DIE.
932DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
933 DIE *MemberDie = new DIE(DT.getTag());
934 StringRef Name = DT.getName();
935 if (!Name.empty())
936 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
937
938 addType(MemberDie, DT.getTypeDerivedFrom());
939
940 addSourceLine(MemberDie, DT);
941
942 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
943 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
944
945 uint64_t Size = DT.getSizeInBits();
946 uint64_t FieldSize = DT.getOriginalTypeSize();
947
948 if (Size != FieldSize) {
949 // Handle bitfield.
950 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
951 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
952
953 uint64_t Offset = DT.getOffsetInBits();
954 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
955 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
956 uint64_t FieldOffset = (HiMark - FieldSize);
957 Offset -= FieldOffset;
958
959 // Maybe we need to work from the other end.
960 if (Asm->getTargetData().isLittleEndian())
961 Offset = FieldSize - (Offset + Size);
962 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
963
964 // Here WD_AT_data_member_location points to the anonymous
965 // field that includes this bit field.
966 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
967
968 } else
969 // This is not a bitfield.
970 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
971
972 if (DT.getTag() == dwarf::DW_TAG_inheritance
973 && DT.isVirtual()) {
974
975 // For C++, virtual base classes are not at fixed offset. Use following
976 // expression to extract appropriate offset from vtable.
977 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
978
979 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
980 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
981 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
982 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
983 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
984 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
985 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
986 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
987
988 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
989 VBaseLocationDie);
990 } else
991 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
992
993 if (DT.isProtected())
994 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
995 dwarf::DW_ACCESS_protected);
996 else if (DT.isPrivate())
997 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
998 dwarf::DW_ACCESS_private);
999 // Otherwise C++ member and base classes are considered public.
1000 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
1001 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1002 dwarf::DW_ACCESS_public);
1003 if (DT.isVirtual())
1004 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1005 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +00001006
1007 // Objective-C properties.
1008 StringRef PropertyName = DT.getObjCPropertyName();
1009 if (!PropertyName.empty()) {
1010 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, dwarf::DW_FORM_string,
1011 PropertyName);
1012 StringRef GetterName = DT.getObjCPropertyGetterName();
1013 if (!GetterName.empty())
1014 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter,
1015 dwarf::DW_FORM_string, GetterName);
1016 StringRef SetterName = DT.getObjCPropertySetterName();
1017 if (!SetterName.empty())
1018 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter,
1019 dwarf::DW_FORM_string, SetterName);
1020 unsigned PropertyAttributes = 0;
1021 if (DT.isReadOnlyObjCProperty())
1022 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1023 if (DT.isReadWriteObjCProperty())
1024 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1025 if (DT.isAssignObjCProperty())
1026 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1027 if (DT.isRetainObjCProperty())
1028 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1029 if (DT.isCopyObjCProperty())
1030 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1031 if (DT.isNonAtomicObjCProperty())
1032 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1033 if (PropertyAttributes)
1034 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1035 PropertyAttributes);
1036 }
Devang Patel161b2f42011-04-12 23:21:44 +00001037 return MemberDie;
1038}