blob: 71a03303b3e8ff37e07f120ba931aa5f83b04fb2 [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
195/// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
196/// on provided frame index.
197void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI) {
198 MachineLocation Location;
199 unsigned FrameReg;
200 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
201 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
202 Location.set(FrameReg, Offset);
203
204 if (DV->variableHasComplexAddress())
205 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
206 else if (DV->isBlockByrefVariable())
207 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
208 else
209 addAddress(Die, dwarf::DW_AT_location, Location);
210}
211
212/// addComplexAddress - Start with the address based on the location provided,
213/// and generate the DWARF information necessary to find the actual variable
214/// given the extra address information encoded in the DIVariable, starting from
215/// the starting location. Add the DWARF information to the die.
216///
217void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
218 unsigned Attribute,
219 const MachineLocation &Location) {
220 DIType Ty = DV->getType();
221
222 // Decode the original location, and use that as the start of the byref
223 // variable's location.
224 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
225 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
226 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
227
228 if (Location.isReg()) {
229 if (Reg < 32) {
230 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
231 } else {
232 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
233 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
234 }
235 } else {
236 if (Reg < 32)
237 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
238 else {
239 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
240 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
241 }
242
243 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
244 }
245
246 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
247 uint64_t Element = DV->getAddrElement(i);
248
249 if (Element == DIBuilder::OpPlus) {
250 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
251 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
252 } else if (Element == DIBuilder::OpDeref) {
253 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
254 } else llvm_unreachable("unknown DIBuilder Opcode");
255 }
256
257 // Now attach the location information to the DIE.
258 addBlock(Die, Attribute, 0, Block);
259}
260
261/* Byref variables, in Blocks, are declared by the programmer as "SomeType
262 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
263 gives the variable VarName either the struct, or a pointer to the struct, as
264 its type. This is necessary for various behind-the-scenes things the
265 compiler needs to do with by-reference variables in Blocks.
266
267 However, as far as the original *programmer* is concerned, the variable
268 should still have type 'SomeType', as originally declared.
269
270 The function getBlockByrefType dives into the __Block_byref_x_VarName
271 struct to find the original type of the variable, which is then assigned to
272 the variable's Debug Information Entry as its real type. So far, so good.
273 However now the debugger will expect the variable VarName to have the type
274 SomeType. So we need the location attribute for the variable to be an
275 expression that explains to the debugger how to navigate through the
276 pointers and struct to find the actual variable of type SomeType.
277
278 The following function does just that. We start by getting
279 the "normal" location for the variable. This will be the location
280 of either the struct __Block_byref_x_VarName or the pointer to the
281 struct __Block_byref_x_VarName.
282
283 The struct will look something like:
284
285 struct __Block_byref_x_VarName {
286 ... <various fields>
287 struct __Block_byref_x_VarName *forwarding;
288 ... <various other fields>
289 SomeType VarName;
290 ... <maybe more fields>
291 };
292
293 If we are given the struct directly (as our starting point) we
294 need to tell the debugger to:
295
296 1). Add the offset of the forwarding field.
297
298 2). Follow that pointer to get the real __Block_byref_x_VarName
299 struct to use (the real one may have been copied onto the heap).
300
301 3). Add the offset for the field VarName, to find the actual variable.
302
303 If we started with a pointer to the struct, then we need to
304 dereference that pointer first, before the other steps.
305 Translating this into DWARF ops, we will need to append the following
306 to the current location description for the variable:
307
308 DW_OP_deref -- optional, if we start with a pointer
309 DW_OP_plus_uconst <forward_fld_offset>
310 DW_OP_deref
311 DW_OP_plus_uconst <varName_fld_offset>
312
313 That is what this function does. */
314
315/// addBlockByrefAddress - Start with the address based on the location
316/// provided, and generate the DWARF information necessary to find the
317/// actual Block variable (navigating the Block struct) based on the
318/// starting location. Add the DWARF information to the die. For
319/// more information, read large comment just above here.
320///
321void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
322 unsigned Attribute,
323 const MachineLocation &Location) {
324 DIType Ty = DV->getType();
325 DIType TmpTy = Ty;
326 unsigned Tag = Ty.getTag();
327 bool isPointer = false;
328
329 StringRef varName = DV->getName();
330
331 if (Tag == dwarf::DW_TAG_pointer_type) {
332 DIDerivedType DTy = DIDerivedType(Ty);
333 TmpTy = DTy.getTypeDerivedFrom();
334 isPointer = true;
335 }
336
337 DICompositeType blockStruct = DICompositeType(TmpTy);
338
339 // Find the __forwarding field and the variable field in the __Block_byref
340 // struct.
341 DIArray Fields = blockStruct.getTypeArray();
342 DIDescriptor varField = DIDescriptor();
343 DIDescriptor forwardingField = DIDescriptor();
344
345 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
346 DIDescriptor Element = Fields.getElement(i);
347 DIDerivedType DT = DIDerivedType(Element);
348 StringRef fieldName = DT.getName();
349 if (fieldName == "__forwarding")
350 forwardingField = Element;
351 else if (fieldName == varName)
352 varField = Element;
353 }
354
355 // Get the offsets for the forwarding field and the variable field.
356 unsigned forwardingFieldOffset =
357 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
358 unsigned varFieldOffset =
359 DIDerivedType(varField).getOffsetInBits() >> 3;
360
361 // Decode the original location, and use that as the start of the byref
362 // variable's location.
363 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
364 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
365 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
366
367 if (Location.isReg()) {
368 if (Reg < 32)
369 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
370 else {
371 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
372 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
373 }
374 } else {
375 if (Reg < 32)
376 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
377 else {
378 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
379 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
380 }
381
382 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
383 }
384
385 // If we started with a pointer to the __Block_byref... struct, then
386 // the first thing we need to do is dereference the pointer (DW_OP_deref).
387 if (isPointer)
388 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
389
390 // Next add the offset for the '__forwarding' field:
391 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
392 // adding the offset if it's 0.
393 if (forwardingFieldOffset > 0) {
394 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
395 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
396 }
397
398 // Now dereference the __forwarding field to get to the real __Block_byref
399 // struct: DW_OP_deref.
400 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
401
402 // Now that we've got the real __Block_byref... struct, add the offset
403 // for the variable's field to get to the location of the actual variable:
404 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
405 if (varFieldOffset > 0) {
406 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
407 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
408 }
409
410 // Now attach the location information to the DIE.
411 addBlock(Die, Attribute, 0, Block);
412}
413
414/// addAddress - Add an address attribute to a die based on the location
415/// provided.
416void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
417 const MachineLocation &Location) {
418 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
419 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
420 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
421
422 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
423 && Location.getOffset()) {
424 // If variable offset is based in frame register then use fbreg.
425 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
426 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
427 addBlock(Die, Attribute, 0, Block);
428 return;
429 }
430
431 if (Location.isReg()) {
432 if (Reg < 32) {
433 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
434 } else {
435 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
436 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
437 }
438 } else {
439 if (Reg < 32) {
440 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
441 } else {
442 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
443 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
444 }
445
446 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
447 }
448
449 addBlock(Die, Attribute, 0, Block);
450}
451
452/// addRegisterAddress - Add register location entry in variable DIE.
453bool CompileUnit::addRegisterAddress(DIE *Die, const MachineOperand &MO) {
454 assert (MO.isReg() && "Invalid machine operand!");
455 if (!MO.getReg())
456 return false;
457 MachineLocation Location;
458 Location.set(MO.getReg());
459 addAddress(Die, dwarf::DW_AT_location, Location);
460 return true;
461}
462
463/// addConstantValue - Add constant value entry in variable DIE.
464bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO) {
465 assert (MO.isImm() && "Invalid machine operand!");
466 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
467 unsigned Imm = MO.getImm();
468 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
469 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
470 return true;
471}
472
473/// addConstantFPValue - Add constant value entry in variable DIE.
474bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
475 assert (MO.isFPImm() && "Invalid machine operand!");
476 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
477 APFloat FPImm = MO.getFPImm()->getValueAPF();
478
479 // Get the raw data form of the floating point.
480 const APInt FltVal = FPImm.bitcastToAPInt();
481 const char *FltPtr = (const char*)FltVal.getRawData();
482
483 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
484 bool LittleEndian = Asm->getTargetData().isLittleEndian();
485 int Incr = (LittleEndian ? 1 : -1);
486 int Start = (LittleEndian ? 0 : NumBytes - 1);
487 int Stop = (LittleEndian ? NumBytes : -1);
488
489 // Output the constant to DWARF one byte at a time.
490 for (; Start != Stop; Start += Incr)
491 addUInt(Block, 0, dwarf::DW_FORM_data1,
492 (unsigned char)0xFF & FltPtr[Start]);
493
494 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
495 return true;
496}
497
498/// addConstantValue - Add constant value entry in variable DIE.
499bool CompileUnit::addConstantValue(DIE *Die, ConstantInt *CI,
500 bool Unsigned) {
501 if (CI->getBitWidth() <= 64) {
502 if (Unsigned)
503 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
504 CI->getZExtValue());
505 else
506 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
507 CI->getSExtValue());
508 return true;
509 }
510
511 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
512
513 // Get the raw data form of the large APInt.
514 const APInt Val = CI->getValue();
515 const char *Ptr = (const char*)Val.getRawData();
516
517 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
518 bool LittleEndian = Asm->getTargetData().isLittleEndian();
519 int Incr = (LittleEndian ? 1 : -1);
520 int Start = (LittleEndian ? 0 : NumBytes - 1);
521 int Stop = (LittleEndian ? NumBytes : -1);
522
523 // Output the constant to DWARF one byte at a time.
524 for (; Start != Stop; Start += Incr)
525 addUInt(Block, 0, dwarf::DW_FORM_data1,
526 (unsigned char)0xFF & Ptr[Start]);
527
528 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
529 return true;
530}
531
532/// addTemplateParams - Add template parameters in buffer.
533void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
534 // Add template parameters.
535 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
536 DIDescriptor Element = TParams.getElement(i);
537 if (Element.isTemplateTypeParameter())
538 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
539 DITemplateTypeParameter(Element)));
540 else if (Element.isTemplateValueParameter())
541 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
542 DITemplateValueParameter(Element)));
543 }
544
545}
546/// addToContextOwner - Add Die into the list of its context owner's children.
547void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
548 if (Context.isType()) {
549 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
550 ContextDIE->addChild(Die);
551 } else if (Context.isNameSpace()) {
552 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
553 ContextDIE->addChild(Die);
554 } else if (Context.isSubprogram()) {
555 DIE *ContextDIE = DD->createSubprogramDIE(DISubprogram(Context));
556 ContextDIE->addChild(Die);
557 } else if (DIE *ContextDIE = getDIE(Context))
558 ContextDIE->addChild(Die);
559 else
560 addDie(Die);
561}
562
563/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
564/// given DIType.
565DIE *CompileUnit::getOrCreateTypeDIE(DIType Ty) {
566 DIE *TyDIE = getDIE(Ty);
567 if (TyDIE)
568 return TyDIE;
569
570 // Create new type.
571 TyDIE = new DIE(dwarf::DW_TAG_base_type);
572 insertDIE(Ty, TyDIE);
573 if (Ty.isBasicType())
574 constructTypeDIE(*TyDIE, DIBasicType(Ty));
575 else if (Ty.isCompositeType())
576 constructTypeDIE(*TyDIE, DICompositeType(Ty));
577 else {
578 assert(Ty.isDerivedType() && "Unknown kind of DIType");
579 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
580 }
581
582 addToContextOwner(TyDIE, Ty.getContext());
583 return TyDIE;
584}
585
586/// addType - Add a new type attribute to the specified entity.
587void CompileUnit::addType(DIE *Entity, DIType Ty) {
588 if (!Ty.Verify())
589 return;
590
591 // Check for pre-existence.
592 DIEEntry *Entry = getDIEEntry(Ty);
593 // If it exists then use the existing value.
594 if (Entry) {
595 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
596 return;
597 }
598
599 // Construct type.
600 DIE *Buffer = getOrCreateTypeDIE(Ty);
601
602 // Set up proxy.
603 Entry = createDIEEntry(Buffer);
604 insertDIEEntry(Ty, Entry);
605
606 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
607}
608
609/// constructTypeDIE - Construct basic type die from DIBasicType.
610void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
611 // Get core information.
612 StringRef Name = BTy.getName();
613 Buffer.setTag(dwarf::DW_TAG_base_type);
614 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
615 BTy.getEncoding());
616
617 // Add name if not anonymous or intermediate type.
618 if (!Name.empty())
619 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
620 uint64_t Size = BTy.getSizeInBits() >> 3;
621 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
622}
623
624/// constructTypeDIE - Construct derived type die from DIDerivedType.
625void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
626 // Get core information.
627 StringRef Name = DTy.getName();
628 uint64_t Size = DTy.getSizeInBits() >> 3;
629 unsigned Tag = DTy.getTag();
630
631 // FIXME - Workaround for templates.
632 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
633
634 Buffer.setTag(Tag);
635
636 // Map to main type, void will not have a type.
637 DIType FromTy = DTy.getTypeDerivedFrom();
638 addType(&Buffer, FromTy);
639
640 // Add name if not anonymous or intermediate type.
641 if (!Name.empty())
642 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
643
644 // Add size if non-zero (derived types might be zero-sized.)
645 if (Size)
646 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
647
648 // Add source line info if available and TyDesc is not a forward declaration.
649 if (!DTy.isForwardDecl())
650 addSourceLine(&Buffer, DTy);
651}
652
653/// constructTypeDIE - Construct type DIE from DICompositeType.
654void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
655 // Get core information.
656 StringRef Name = CTy.getName();
657
658 uint64_t Size = CTy.getSizeInBits() >> 3;
659 unsigned Tag = CTy.getTag();
660 Buffer.setTag(Tag);
661
662 switch (Tag) {
663 case dwarf::DW_TAG_vector_type:
664 case dwarf::DW_TAG_array_type:
665 constructArrayTypeDIE(Buffer, &CTy);
666 break;
667 case dwarf::DW_TAG_enumeration_type: {
668 DIArray Elements = CTy.getTypeArray();
669
670 // Add enumerators to enumeration type.
671 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
672 DIE *ElemDie = NULL;
673 DIDescriptor Enum(Elements.getElement(i));
674 if (Enum.isEnumerator()) {
675 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
676 Buffer.addChild(ElemDie);
677 }
678 }
679 }
680 break;
681 case dwarf::DW_TAG_subroutine_type: {
682 // Add return type.
683 DIArray Elements = CTy.getTypeArray();
684 DIDescriptor RTy = Elements.getElement(0);
685 addType(&Buffer, DIType(RTy));
686
687 bool isPrototyped = true;
688 // Add arguments.
689 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
690 DIDescriptor Ty = Elements.getElement(i);
691 if (Ty.isUnspecifiedParameter()) {
692 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
693 Buffer.addChild(Arg);
694 isPrototyped = false;
695 } else {
696 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
697 addType(Arg, DIType(Ty));
698 Buffer.addChild(Arg);
699 }
700 }
701 // Add prototype flag.
702 if (isPrototyped)
703 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
704 }
705 break;
706 case dwarf::DW_TAG_structure_type:
707 case dwarf::DW_TAG_union_type:
708 case dwarf::DW_TAG_class_type: {
709 // Add elements to structure type.
710 DIArray Elements = CTy.getTypeArray();
711
712 // A forward struct declared type may not have elements available.
713 unsigned N = Elements.getNumElements();
714 if (N == 0)
715 break;
716
717 // Add elements to structure type.
718 for (unsigned i = 0; i < N; ++i) {
719 DIDescriptor Element = Elements.getElement(i);
720 DIE *ElemDie = NULL;
721 if (Element.isSubprogram()) {
722 DISubprogram SP(Element);
723 ElemDie = DD->createSubprogramDIE(DISubprogram(Element));
724 if (SP.isProtected())
725 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
726 dwarf::DW_ACCESS_protected);
727 else if (SP.isPrivate())
728 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
729 dwarf::DW_ACCESS_private);
730 else
731 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
732 dwarf::DW_ACCESS_public);
733 if (SP.isExplicit())
734 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
735 }
736 else if (Element.isVariable()) {
737 DIVariable DV(Element);
738 ElemDie = new DIE(dwarf::DW_TAG_variable);
739 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
740 DV.getName());
741 addType(ElemDie, DV.getType());
742 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
743 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
744 addSourceLine(ElemDie, DV);
745 } else if (Element.isDerivedType())
746 ElemDie = createMemberDIE(DIDerivedType(Element));
747 else
748 continue;
749 Buffer.addChild(ElemDie);
750 }
751
752 if (CTy.isAppleBlockExtension())
753 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
754
755 unsigned RLang = CTy.getRunTimeLang();
756 if (RLang)
757 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
758 dwarf::DW_FORM_data1, RLang);
759
760 DICompositeType ContainingType = CTy.getContainingType();
761 if (DIDescriptor(ContainingType).isCompositeType())
762 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
763 getOrCreateTypeDIE(DIType(ContainingType)));
764 else {
765 DIDescriptor Context = CTy.getContext();
766 addToContextOwner(&Buffer, Context);
767 }
768
769 if (Tag == dwarf::DW_TAG_class_type)
770 addTemplateParams(Buffer, CTy.getTemplateParams());
771
772 break;
773 }
774 default:
775 break;
776 }
777
778 // Add name if not anonymous or intermediate type.
779 if (!Name.empty())
780 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
781
782 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
783 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
784 {
785 // Add size if non-zero (derived types might be zero-sized.)
786 if (Size)
787 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
788 else {
789 // Add zero size if it is not a forward declaration.
790 if (CTy.isForwardDecl())
791 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
792 else
793 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
794 }
795
796 // Add source line info if available.
797 if (!CTy.isForwardDecl())
798 addSourceLine(&Buffer, CTy);
799 }
800}
801
802/// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
803/// for the given DITemplateTypeParameter.
804DIE *
805CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
806 DIE *ParamDIE = getDIE(TP);
807 if (ParamDIE)
808 return ParamDIE;
809
810 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
811 addType(ParamDIE, TP.getType());
812 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName());
813 return ParamDIE;
814}
815
816/// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
817/// for the given DITemplateValueParameter.
818DIE *
819CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
820 DIE *ParamDIE = getDIE(TPV);
821 if (ParamDIE)
822 return ParamDIE;
823
824 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
825 addType(ParamDIE, TPV.getType());
826 if (!TPV.getName().empty())
827 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName());
828 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
829 TPV.getValue());
830 return ParamDIE;
831}
832
833/// constructSubrangeDIE - Construct subrange DIE from DISubrange.
834void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
835 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
836 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
837 int64_t L = SR.getLo();
838 int64_t H = SR.getHi();
839
840 // The L value defines the lower bounds which is typically zero for C/C++. The
841 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
842 // of the array. If L > H then do not emit DW_AT_lower_bound and
843 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
844 // array has one element and in such case do not emit lower bound.
845
846 if (L > H) {
847 Buffer.addChild(DW_Subrange);
848 return;
849 }
850 if (L)
851 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
852 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
853 Buffer.addChild(DW_Subrange);
854}
855
856/// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
857void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
858 DICompositeType *CTy) {
859 Buffer.setTag(dwarf::DW_TAG_array_type);
860 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
861 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
862
863 // Emit derived type.
864 addType(&Buffer, CTy->getTypeDerivedFrom());
865 DIArray Elements = CTy->getTypeArray();
866
867 // Get an anonymous type for index type.
868 DIE *IdxTy = getIndexTyDie();
869 if (!IdxTy) {
870 // Construct an anonymous type for index type.
871 IdxTy = new DIE(dwarf::DW_TAG_base_type);
872 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
873 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
874 dwarf::DW_ATE_signed);
875 addDie(IdxTy);
876 setIndexTyDie(IdxTy);
877 }
878
879 // Add subranges to array type.
880 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
881 DIDescriptor Element = Elements.getElement(i);
882 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
883 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
884 }
885}
886
887/// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
888DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
889 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
890 StringRef Name = ETy.getName();
891 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
892 int64_t Value = ETy.getEnumValue();
893 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
894 return Enumerator;
895}
896
897/// createMemberDIE - Create new member DIE.
898DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
899 DIE *MemberDie = new DIE(DT.getTag());
900 StringRef Name = DT.getName();
901 if (!Name.empty())
902 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
903
904 addType(MemberDie, DT.getTypeDerivedFrom());
905
906 addSourceLine(MemberDie, DT);
907
908 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
909 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
910
911 uint64_t Size = DT.getSizeInBits();
912 uint64_t FieldSize = DT.getOriginalTypeSize();
913
914 if (Size != FieldSize) {
915 // Handle bitfield.
916 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
917 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
918
919 uint64_t Offset = DT.getOffsetInBits();
920 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
921 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
922 uint64_t FieldOffset = (HiMark - FieldSize);
923 Offset -= FieldOffset;
924
925 // Maybe we need to work from the other end.
926 if (Asm->getTargetData().isLittleEndian())
927 Offset = FieldSize - (Offset + Size);
928 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
929
930 // Here WD_AT_data_member_location points to the anonymous
931 // field that includes this bit field.
932 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
933
934 } else
935 // This is not a bitfield.
936 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
937
938 if (DT.getTag() == dwarf::DW_TAG_inheritance
939 && DT.isVirtual()) {
940
941 // For C++, virtual base classes are not at fixed offset. Use following
942 // expression to extract appropriate offset from vtable.
943 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
944
945 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
946 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
947 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
948 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
949 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
950 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
951 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
952 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
953
954 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
955 VBaseLocationDie);
956 } else
957 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
958
959 if (DT.isProtected())
960 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
961 dwarf::DW_ACCESS_protected);
962 else if (DT.isPrivate())
963 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
964 dwarf::DW_ACCESS_private);
965 // Otherwise C++ member and base classes are considered public.
966 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
967 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
968 dwarf::DW_ACCESS_public);
969 if (DT.isVirtual())
970 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
971 dwarf::DW_VIRTUALITY_virtual);
Devang Patele9db5e22011-04-16 00:11:51 +0000972
973 // Objective-C properties.
974 StringRef PropertyName = DT.getObjCPropertyName();
975 if (!PropertyName.empty()) {
976 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, dwarf::DW_FORM_string,
977 PropertyName);
978 StringRef GetterName = DT.getObjCPropertyGetterName();
979 if (!GetterName.empty())
980 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter,
981 dwarf::DW_FORM_string, GetterName);
982 StringRef SetterName = DT.getObjCPropertySetterName();
983 if (!SetterName.empty())
984 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter,
985 dwarf::DW_FORM_string, SetterName);
986 unsigned PropertyAttributes = 0;
987 if (DT.isReadOnlyObjCProperty())
988 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
989 if (DT.isReadWriteObjCProperty())
990 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
991 if (DT.isAssignObjCProperty())
992 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
993 if (DT.isRetainObjCProperty())
994 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
995 if (DT.isCopyObjCProperty())
996 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
997 if (DT.isNonAtomicObjCProperty())
998 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
999 if (PropertyAttributes)
1000 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1001 PropertyAttributes);
1002 }
Devang Patel161b2f42011-04-12 23:21:44 +00001003 return MemberDie;
1004}