1 //===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for constructing a dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dwarfdebug"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfAccelTable.h"
18 #include "DwarfDebug.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DIBuilder.h"
22 #include "llvm/DataLayout.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Target/TargetFrameLowering.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetRegisterInfo.h"
34 /// CompileUnit - Compile unit constructor.
35 CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, AsmPrinter *A,
37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) {
38 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
41 /// ~CompileUnit - Destructor for compile unit.
42 CompileUnit::~CompileUnit() {
43 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
44 DIEBlocks[j]->~DIEBlock();
47 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
48 /// information entry.
49 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
50 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
54 /// getDefaultLowerBound - Return the default lower bound for an array. If the
55 /// DWARF version doesn't handle the language, return -1.
56 int64_t CompileUnit::getDefaultLowerBound() const {
61 case dwarf::DW_LANG_C89:
62 case dwarf::DW_LANG_C99:
63 case dwarf::DW_LANG_C:
64 case dwarf::DW_LANG_C_plus_plus:
65 case dwarf::DW_LANG_ObjC:
66 case dwarf::DW_LANG_ObjC_plus_plus:
69 case dwarf::DW_LANG_Fortran77:
70 case dwarf::DW_LANG_Fortran90:
71 case dwarf::DW_LANG_Fortran95:
74 // The languages below have valid values only if the DWARF version >= 4.
75 case dwarf::DW_LANG_Java:
76 case dwarf::DW_LANG_Python:
77 case dwarf::DW_LANG_UPC:
78 case dwarf::DW_LANG_D:
79 if (dwarf::DWARF_VERSION >= 4)
83 case dwarf::DW_LANG_Ada83:
84 case dwarf::DW_LANG_Ada95:
85 case dwarf::DW_LANG_Cobol74:
86 case dwarf::DW_LANG_Cobol85:
87 case dwarf::DW_LANG_Modula2:
88 case dwarf::DW_LANG_Pascal83:
89 case dwarf::DW_LANG_PLI:
90 if (dwarf::DWARF_VERSION >= 4)
98 /// addFlag - Add a flag that is true.
99 void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
100 if (!DD->useDarwinGDBCompat())
101 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
104 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
107 /// addUInt - Add an unsigned integer attribute data and value.
109 void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
110 unsigned Form, uint64_t Integer) {
111 if (!Form) Form = DIEInteger::BestForm(false, Integer);
112 DIEValue *Value = Integer == 1 ?
113 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
114 Die->addValue(Attribute, Form, Value);
117 /// addSInt - Add an signed integer attribute data and value.
119 void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
120 unsigned Form, int64_t Integer) {
121 if (!Form) Form = DIEInteger::BestForm(true, Integer);
122 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
123 Die->addValue(Attribute, Form, Value);
126 /// addString - Add a string attribute data and value. We always emit a
127 /// reference to the string pool instead of immediate strings so that DIEs have
128 /// more predictable sizes.
129 void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
130 MCSymbol *Symb = DD->getStringPoolEntry(String);
132 if (Asm->needsRelocationsForDwarfStringPool())
133 Value = new (DIEValueAllocator) DIELabel(Symb);
135 MCSymbol *StringPool = DD->getStringPool();
136 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
138 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
141 /// addLabel - Add a Dwarf label attribute data and value.
143 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
144 const MCSymbol *Label) {
145 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
146 Die->addValue(Attribute, Form, Value);
149 /// addDelta - Add a label delta attribute data and value.
151 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
152 const MCSymbol *Hi, const MCSymbol *Lo) {
153 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
154 Die->addValue(Attribute, Form, Value);
157 /// addDIEEntry - Add a DIE attribute data and value.
159 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
161 Die->addValue(Attribute, Form, createDIEEntry(Entry));
164 /// addBlock - Add block data.
166 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
168 Block->ComputeSize(Asm);
169 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
170 Die->addValue(Attribute, Block->BestForm(), Block);
173 /// addSourceLine - Add location information to specified debug information
175 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
180 unsigned Line = V.getLineNumber();
183 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
184 V.getContext().getDirectory());
185 assert(FileID && "Invalid file id");
186 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
187 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
190 /// addSourceLine - Add location information to specified debug information
192 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
193 // Verify global variable.
197 unsigned Line = G.getLineNumber();
200 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.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);
206 /// addSourceLine - Add location information to specified debug information
208 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
209 // Verify subprogram.
213 // If the line number is 0, don't add it.
214 unsigned Line = SP.getLineNumber();
218 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
220 assert(FileID && "Invalid file id");
221 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
222 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
225 /// addSourceLine - Add location information to specified debug information
227 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
232 unsigned Line = Ty.getLineNumber();
235 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
237 assert(FileID && "Invalid file id");
238 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
239 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
242 /// addSourceLine - Add location information to specified debug information
244 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
249 unsigned Line = Ty.getLineNumber();
252 DIFile File = Ty.getFile();
253 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
254 File.getDirectory());
255 assert(FileID && "Invalid file id");
256 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
257 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
260 /// addSourceLine - Add location information to specified debug information
262 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
267 unsigned Line = NS.getLineNumber();
270 StringRef FN = NS.getFilename();
272 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
273 assert(FileID && "Invalid file id");
274 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
275 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
278 /// addVariableAddress - Add DW_AT_location attribute for a
279 /// DbgVariable based on provided MachineLocation.
280 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
281 MachineLocation Location) {
282 if (DV->variableHasComplexAddress())
283 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
284 else if (DV->isBlockByrefVariable())
285 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
287 addAddress(Die, dwarf::DW_AT_location, Location);
290 /// addRegisterOp - Add register operand.
291 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
292 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
293 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
295 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
297 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
298 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
302 /// addRegisterOffset - Add register offset.
303 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
305 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
306 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
307 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
308 if (Reg == TRI->getFrameRegister(*Asm->MF))
309 // If variable offset is based in frame register then use fbreg.
310 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
312 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
314 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
315 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
317 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
320 /// addAddress - Add an address attribute to a die based on the location
322 void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
323 const MachineLocation &Location) {
324 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
326 if (Location.isReg())
327 addRegisterOp(Block, Location.getReg());
329 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
331 // Now attach the location information to the DIE.
332 addBlock(Die, Attribute, 0, Block);
335 /// addComplexAddress - Start with the address based on the location provided,
336 /// and generate the DWARF information necessary to find the actual variable
337 /// given the extra address information encoded in the DIVariable, starting from
338 /// the starting location. Add the DWARF information to the die.
340 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
342 const MachineLocation &Location) {
343 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
344 unsigned N = DV->getNumAddrElements();
346 if (Location.isReg()) {
347 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
348 // If first address element is OpPlus then emit
349 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
350 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
353 addRegisterOp(Block, Location.getReg());
356 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
359 uint64_t Element = DV->getAddrElement(i);
360 if (Element == DIBuilder::OpPlus) {
361 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
362 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
363 } else if (Element == DIBuilder::OpDeref) {
364 if (!Location.isReg())
365 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
366 } else llvm_unreachable("unknown DIBuilder Opcode");
369 // Now attach the location information to the DIE.
370 addBlock(Die, Attribute, 0, Block);
373 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
374 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
375 gives the variable VarName either the struct, or a pointer to the struct, as
376 its type. This is necessary for various behind-the-scenes things the
377 compiler needs to do with by-reference variables in Blocks.
379 However, as far as the original *programmer* is concerned, the variable
380 should still have type 'SomeType', as originally declared.
382 The function getBlockByrefType dives into the __Block_byref_x_VarName
383 struct to find the original type of the variable, which is then assigned to
384 the variable's Debug Information Entry as its real type. So far, so good.
385 However now the debugger will expect the variable VarName to have the type
386 SomeType. So we need the location attribute for the variable to be an
387 expression that explains to the debugger how to navigate through the
388 pointers and struct to find the actual variable of type SomeType.
390 The following function does just that. We start by getting
391 the "normal" location for the variable. This will be the location
392 of either the struct __Block_byref_x_VarName or the pointer to the
393 struct __Block_byref_x_VarName.
395 The struct will look something like:
397 struct __Block_byref_x_VarName {
399 struct __Block_byref_x_VarName *forwarding;
400 ... <various other fields>
402 ... <maybe more fields>
405 If we are given the struct directly (as our starting point) we
406 need to tell the debugger to:
408 1). Add the offset of the forwarding field.
410 2). Follow that pointer to get the real __Block_byref_x_VarName
411 struct to use (the real one may have been copied onto the heap).
413 3). Add the offset for the field VarName, to find the actual variable.
415 If we started with a pointer to the struct, then we need to
416 dereference that pointer first, before the other steps.
417 Translating this into DWARF ops, we will need to append the following
418 to the current location description for the variable:
420 DW_OP_deref -- optional, if we start with a pointer
421 DW_OP_plus_uconst <forward_fld_offset>
423 DW_OP_plus_uconst <varName_fld_offset>
425 That is what this function does. */
427 /// addBlockByrefAddress - Start with the address based on the location
428 /// provided, and generate the DWARF information necessary to find the
429 /// actual Block variable (navigating the Block struct) based on the
430 /// starting location. Add the DWARF information to the die. For
431 /// more information, read large comment just above here.
433 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
435 const MachineLocation &Location) {
436 DIType Ty = DV->getType();
438 unsigned Tag = Ty.getTag();
439 bool isPointer = false;
441 StringRef varName = DV->getName();
443 if (Tag == dwarf::DW_TAG_pointer_type) {
444 DIDerivedType DTy = DIDerivedType(Ty);
445 TmpTy = DTy.getTypeDerivedFrom();
449 DICompositeType blockStruct = DICompositeType(TmpTy);
451 // Find the __forwarding field and the variable field in the __Block_byref
453 DIArray Fields = blockStruct.getTypeArray();
454 DIDescriptor varField = DIDescriptor();
455 DIDescriptor forwardingField = DIDescriptor();
457 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
458 DIDescriptor Element = Fields.getElement(i);
459 DIDerivedType DT = DIDerivedType(Element);
460 StringRef fieldName = DT.getName();
461 if (fieldName == "__forwarding")
462 forwardingField = Element;
463 else if (fieldName == varName)
467 // Get the offsets for the forwarding field and the variable field.
468 unsigned forwardingFieldOffset =
469 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
470 unsigned varFieldOffset =
471 DIDerivedType(varField).getOffsetInBits() >> 3;
473 // Decode the original location, and use that as the start of the byref
474 // variable's location.
475 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
477 if (Location.isReg())
478 addRegisterOp(Block, Location.getReg());
480 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
482 // If we started with a pointer to the __Block_byref... struct, then
483 // the first thing we need to do is dereference the pointer (DW_OP_deref).
485 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
487 // Next add the offset for the '__forwarding' field:
488 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
489 // adding the offset if it's 0.
490 if (forwardingFieldOffset > 0) {
491 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
492 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
495 // Now dereference the __forwarding field to get to the real __Block_byref
496 // struct: DW_OP_deref.
497 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
499 // Now that we've got the real __Block_byref... struct, add the offset
500 // for the variable's field to get to the location of the actual variable:
501 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
502 if (varFieldOffset > 0) {
503 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
504 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
507 // Now attach the location information to the DIE.
508 addBlock(Die, Attribute, 0, Block);
511 /// isTypeSigned - Return true if the type is signed.
512 static bool isTypeSigned(DIType Ty, int *SizeInBits) {
513 if (Ty.isDerivedType())
514 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
515 if (Ty.isBasicType())
516 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
517 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
518 *SizeInBits = Ty.getSizeInBits();
524 /// addConstantValue - Add constant value entry in variable DIE.
525 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
527 assert(MO.isImm() && "Invalid machine operand!");
528 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
530 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
531 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
532 switch (SizeInBits) {
533 case 8: Form = dwarf::DW_FORM_data1; break;
534 case 16: Form = dwarf::DW_FORM_data2; break;
535 case 32: Form = dwarf::DW_FORM_data4; break;
536 case 64: Form = dwarf::DW_FORM_data8; break;
539 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
540 : addUInt(Block, 0, Form, MO.getImm());
542 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
546 /// addConstantFPValue - Add constant value entry in variable DIE.
547 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
548 assert (MO.isFPImm() && "Invalid machine operand!");
549 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
550 APFloat FPImm = MO.getFPImm()->getValueAPF();
552 // Get the raw data form of the floating point.
553 const APInt FltVal = FPImm.bitcastToAPInt();
554 const char *FltPtr = (const char*)FltVal.getRawData();
556 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
557 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
558 int Incr = (LittleEndian ? 1 : -1);
559 int Start = (LittleEndian ? 0 : NumBytes - 1);
560 int Stop = (LittleEndian ? NumBytes : -1);
562 // Output the constant to DWARF one byte at a time.
563 for (; Start != Stop; Start += Incr)
564 addUInt(Block, 0, dwarf::DW_FORM_data1,
565 (unsigned char)0xFF & FltPtr[Start]);
567 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
571 /// addConstantValue - Add constant value entry in variable DIE.
572 bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
574 unsigned CIBitWidth = CI->getBitWidth();
575 if (CIBitWidth <= 64) {
577 switch (CIBitWidth) {
578 case 8: form = dwarf::DW_FORM_data1; break;
579 case 16: form = dwarf::DW_FORM_data2; break;
580 case 32: form = dwarf::DW_FORM_data4; break;
581 case 64: form = dwarf::DW_FORM_data8; break;
583 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
586 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
588 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
592 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
594 // Get the raw data form of the large APInt.
595 const APInt Val = CI->getValue();
596 const uint64_t *Ptr64 = Val.getRawData();
598 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
599 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
601 // Output the constant to DWARF one byte at a time.
602 for (int i = 0; i < NumBytes; i++) {
605 c = Ptr64[i / 8] >> (8 * (i & 7));
607 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
608 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
611 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
615 /// addTemplateParams - Add template parameters in buffer.
616 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
617 // Add template parameters.
618 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
619 DIDescriptor Element = TParams.getElement(i);
620 if (Element.isTemplateTypeParameter())
621 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
622 DITemplateTypeParameter(Element)));
623 else if (Element.isTemplateValueParameter())
624 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
625 DITemplateValueParameter(Element)));
629 /// addToContextOwner - Add Die into the list of its context owner's children.
630 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
631 if (Context.isType()) {
632 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
633 ContextDIE->addChild(Die);
634 } else if (Context.isNameSpace()) {
635 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
636 ContextDIE->addChild(Die);
637 } else if (Context.isSubprogram()) {
638 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
639 ContextDIE->addChild(Die);
640 } else if (DIE *ContextDIE = getDIE(Context))
641 ContextDIE->addChild(Die);
646 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
648 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
652 DIE *TyDIE = getDIE(Ty);
657 TyDIE = new DIE(dwarf::DW_TAG_base_type);
658 insertDIE(Ty, TyDIE);
659 if (Ty.isBasicType())
660 constructTypeDIE(*TyDIE, DIBasicType(Ty));
661 else if (Ty.isCompositeType())
662 constructTypeDIE(*TyDIE, DICompositeType(Ty));
664 assert(Ty.isDerivedType() && "Unknown kind of DIType");
665 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
667 // If this is a named finished type then include it in the list of types
668 // for the accelerator tables.
669 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
670 bool IsImplementation = 0;
671 if (Ty.isCompositeType()) {
672 DICompositeType CT(Ty);
673 // A runtime language of 0 actually means C/C++ and that any
674 // non-negative value is some version of Objective-C/C++.
675 IsImplementation = (CT.getRunTimeLang() == 0) ||
676 CT.isObjcClassComplete();
678 unsigned Flags = IsImplementation ?
679 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
680 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
683 addToContextOwner(TyDIE, Ty.getContext());
687 /// addType - Add a new type attribute to the specified entity.
688 void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
692 // Check for pre-existence.
693 DIEEntry *Entry = getDIEEntry(Ty);
694 // If it exists then use the existing value.
696 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
701 DIE *Buffer = getOrCreateTypeDIE(Ty);
704 Entry = createDIEEntry(Buffer);
705 insertDIEEntry(Ty, Entry);
706 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
708 // If this is a complete composite type then include it in the
709 // list of global types.
713 /// addGlobalType - Add a new global type to the compile unit.
715 void CompileUnit::addGlobalType(DIType Ty) {
716 DIDescriptor Context = Ty.getContext();
717 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
718 && (!Context || Context.isCompileUnit() || Context.isFile()
719 || Context.isNameSpace()))
720 if (DIEEntry *Entry = getDIEEntry(Ty))
721 GlobalTypes[Ty.getName()] = Entry->getEntry();
724 /// addPubTypes - Add type for pubtypes section.
725 void CompileUnit::addPubTypes(DISubprogram SP) {
726 DICompositeType SPTy = SP.getType();
727 unsigned SPTag = SPTy.getTag();
728 if (SPTag != dwarf::DW_TAG_subroutine_type)
731 DIArray Args = SPTy.getTypeArray();
732 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
733 DIType ATy(Args.getElement(i));
740 /// constructTypeDIE - Construct basic type die from DIBasicType.
741 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
742 // Get core information.
743 StringRef Name = BTy.getName();
744 // Add name if not anonymous or intermediate type.
746 addString(&Buffer, dwarf::DW_AT_name, Name);
748 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
749 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
750 // Unspecified types has only name, nothing else.
754 Buffer.setTag(dwarf::DW_TAG_base_type);
755 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
758 uint64_t Size = BTy.getSizeInBits() >> 3;
759 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
762 /// constructTypeDIE - Construct derived type die from DIDerivedType.
763 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
764 // Get core information.
765 StringRef Name = DTy.getName();
766 uint64_t Size = DTy.getSizeInBits() >> 3;
767 unsigned Tag = DTy.getTag();
769 // FIXME - Workaround for templates.
770 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
774 // Map to main type, void will not have a type.
775 DIType FromTy = DTy.getTypeDerivedFrom();
776 addType(&Buffer, FromTy);
778 // Add name if not anonymous or intermediate type.
780 addString(&Buffer, dwarf::DW_AT_name, Name);
782 // Add size if non-zero (derived types might be zero-sized.)
783 if (Size && Tag != dwarf::DW_TAG_pointer_type)
784 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
786 // Add source line info if available and TyDesc is not a forward declaration.
787 if (!DTy.isForwardDecl())
788 addSourceLine(&Buffer, DTy);
791 /// constructTypeDIE - Construct type DIE from DICompositeType.
792 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
793 // Get core information.
794 StringRef Name = CTy.getName();
796 uint64_t Size = CTy.getSizeInBits() >> 3;
797 unsigned Tag = CTy.getTag();
801 case dwarf::DW_TAG_vector_type:
802 case dwarf::DW_TAG_array_type:
803 constructArrayTypeDIE(Buffer, &CTy);
805 case dwarf::DW_TAG_enumeration_type: {
806 DIArray Elements = CTy.getTypeArray();
808 // Add enumerators to enumeration type.
809 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
811 DIDescriptor Enum(Elements.getElement(i));
812 if (Enum.isEnumerator()) {
813 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
814 Buffer.addChild(ElemDie);
817 DIType DTy = CTy.getTypeDerivedFrom();
819 addType(&Buffer, DTy);
820 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
824 case dwarf::DW_TAG_subroutine_type: {
826 DIArray Elements = CTy.getTypeArray();
827 DIDescriptor RTy = Elements.getElement(0);
828 addType(&Buffer, DIType(RTy));
830 bool isPrototyped = true;
832 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
833 DIDescriptor Ty = Elements.getElement(i);
834 if (Ty.isUnspecifiedParameter()) {
835 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
836 Buffer.addChild(Arg);
837 isPrototyped = false;
839 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
840 addType(Arg, DIType(Ty));
841 Buffer.addChild(Arg);
844 // Add prototype flag if we're dealing with a C language and the
845 // function has been prototyped.
847 (Language == dwarf::DW_LANG_C89 ||
848 Language == dwarf::DW_LANG_C99 ||
849 Language == dwarf::DW_LANG_ObjC))
850 addFlag(&Buffer, dwarf::DW_AT_prototyped);
853 case dwarf::DW_TAG_structure_type:
854 case dwarf::DW_TAG_union_type:
855 case dwarf::DW_TAG_class_type: {
856 // Add elements to structure type.
857 DIArray Elements = CTy.getTypeArray();
859 // A forward struct declared type may not have elements available.
860 unsigned N = Elements.getNumElements();
864 // Add elements to structure type.
865 for (unsigned i = 0; i < N; ++i) {
866 DIDescriptor Element = Elements.getElement(i);
868 if (Element.isSubprogram()) {
869 DISubprogram SP(Element);
870 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
871 if (SP.isProtected())
872 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
873 dwarf::DW_ACCESS_protected);
874 else if (SP.isPrivate())
875 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
876 dwarf::DW_ACCESS_private);
878 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
879 dwarf::DW_ACCESS_public);
881 addFlag(ElemDie, dwarf::DW_AT_explicit);
883 else if (Element.isVariable()) {
884 DIVariable DV(Element);
885 ElemDie = new DIE(dwarf::DW_TAG_variable);
886 addString(ElemDie, dwarf::DW_AT_name, DV.getName());
887 addType(ElemDie, DV.getType());
888 addFlag(ElemDie, dwarf::DW_AT_declaration);
889 addFlag(ElemDie, dwarf::DW_AT_external);
890 addSourceLine(ElemDie, DV);
891 } else if (Element.isDerivedType()) {
892 DIDerivedType DDTy(Element);
893 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
894 ElemDie = new DIE(dwarf::DW_TAG_friend);
895 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
897 ElemDie = createMemberDIE(DIDerivedType(Element));
898 } else if (Element.isObjCProperty()) {
899 DIObjCProperty Property(Element);
900 ElemDie = new DIE(Property.getTag());
901 StringRef PropertyName = Property.getObjCPropertyName();
902 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
903 addType(ElemDie, Property.getType());
904 addSourceLine(ElemDie, Property);
905 StringRef GetterName = Property.getObjCPropertyGetterName();
906 if (!GetterName.empty())
907 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
908 StringRef SetterName = Property.getObjCPropertySetterName();
909 if (!SetterName.empty())
910 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
911 unsigned PropertyAttributes = 0;
912 if (Property.isReadOnlyObjCProperty())
913 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
914 if (Property.isReadWriteObjCProperty())
915 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
916 if (Property.isAssignObjCProperty())
917 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
918 if (Property.isRetainObjCProperty())
919 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
920 if (Property.isCopyObjCProperty())
921 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
922 if (Property.isNonAtomicObjCProperty())
923 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
924 if (PropertyAttributes)
925 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
928 DIEEntry *Entry = getDIEEntry(Element);
930 Entry = createDIEEntry(ElemDie);
931 insertDIEEntry(Element, Entry);
935 Buffer.addChild(ElemDie);
938 if (CTy.isAppleBlockExtension())
939 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
941 DICompositeType ContainingType = CTy.getContainingType();
942 if (DIDescriptor(ContainingType).isCompositeType())
943 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
944 getOrCreateTypeDIE(DIType(ContainingType)));
946 DIDescriptor Context = CTy.getContext();
947 addToContextOwner(&Buffer, Context);
950 if (CTy.isObjcClassComplete())
951 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
953 // Add template parameters to a class, structure or union types.
954 // FIXME: The support isn't in the metadata for this yet.
955 if (Tag == dwarf::DW_TAG_class_type ||
956 Tag == dwarf::DW_TAG_structure_type ||
957 Tag == dwarf::DW_TAG_union_type)
958 addTemplateParams(Buffer, CTy.getTemplateParams());
966 // Add name if not anonymous or intermediate type.
968 addString(&Buffer, dwarf::DW_AT_name, Name);
970 if (Tag == dwarf::DW_TAG_enumeration_type ||
971 Tag == dwarf::DW_TAG_class_type ||
972 Tag == dwarf::DW_TAG_structure_type ||
973 Tag == dwarf::DW_TAG_union_type) {
974 // Add size if non-zero (derived types might be zero-sized.)
975 // TODO: Do we care about size for enum forward declarations?
977 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
978 else if (!CTy.isForwardDecl())
979 // Add zero size if it is not a forward declaration.
980 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
982 // If we're a forward decl, say so.
983 if (CTy.isForwardDecl())
984 addFlag(&Buffer, dwarf::DW_AT_declaration);
986 // Add source line info if available.
987 if (!CTy.isForwardDecl())
988 addSourceLine(&Buffer, CTy);
990 // No harm in adding the runtime language to the declaration.
991 unsigned RLang = CTy.getRunTimeLang();
993 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
994 dwarf::DW_FORM_data1, RLang);
998 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
999 /// for the given DITemplateTypeParameter.
1001 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1002 DIE *ParamDIE = getDIE(TP);
1006 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1007 addType(ParamDIE, TP.getType());
1008 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1012 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
1013 /// for the given DITemplateValueParameter.
1015 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
1016 DIE *ParamDIE = getDIE(TPV);
1020 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1021 addType(ParamDIE, TPV.getType());
1022 if (!TPV.getName().empty())
1023 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
1024 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1029 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1030 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1031 DIE *NDie = getDIE(NS);
1034 NDie = new DIE(dwarf::DW_TAG_namespace);
1035 insertDIE(NS, NDie);
1036 if (!NS.getName().empty()) {
1037 addString(NDie, dwarf::DW_AT_name, NS.getName());
1038 addAccelNamespace(NS.getName(), NDie);
1040 addAccelNamespace("(anonymous namespace)", NDie);
1041 addSourceLine(NDie, NS);
1042 addToContextOwner(NDie, NS.getContext());
1046 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1047 /// printer to not emit usual symbol prefix before the symbol name is used then
1048 /// return linkage name after skipping this special LLVM prefix.
1049 static StringRef getRealLinkageName(StringRef LinkageName) {
1051 if (LinkageName.startswith(StringRef(&One, 1)))
1052 return LinkageName.substr(1);
1056 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1057 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1058 DIE *SPDie = getDIE(SP);
1062 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1064 // DW_TAG_inlined_subroutine may refer to this DIE.
1065 insertDIE(SP, SPDie);
1067 DISubprogram SPDecl = SP.getFunctionDeclaration();
1068 DIE *DeclDie = NULL;
1069 if (SPDecl.isSubprogram()) {
1070 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1073 // Add to context owner.
1074 addToContextOwner(SPDie, SP.getContext());
1076 // Add function template parameters.
1077 addTemplateParams(*SPDie, SP.getTemplateParams());
1079 // Unfortunately this code needs to stay here instead of below the
1080 // AT_specification code in order to work around a bug in older
1081 // gdbs that requires the linkage name to resolve multiple template
1083 // TODO: Remove this set of code when we get rid of the old gdb
1085 StringRef LinkageName = SP.getLinkageName();
1086 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1087 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1088 getRealLinkageName(LinkageName));
1090 // If this DIE is going to refer declaration info using AT_specification
1091 // then there is no need to add other attributes.
1093 // Refer function declaration directly.
1094 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1100 // Add the linkage name if we have one.
1101 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1102 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1103 getRealLinkageName(LinkageName));
1105 // Constructors and operators for anonymous aggregates do not have names.
1106 if (!SP.getName().empty())
1107 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1109 addSourceLine(SPDie, SP);
1111 // Add the prototype if we have a prototype and we have a C like
1113 if (SP.isPrototyped() &&
1114 (Language == dwarf::DW_LANG_C89 ||
1115 Language == dwarf::DW_LANG_C99 ||
1116 Language == dwarf::DW_LANG_ObjC))
1117 addFlag(SPDie, dwarf::DW_AT_prototyped);
1120 DICompositeType SPTy = SP.getType();
1121 DIArray Args = SPTy.getTypeArray();
1122 unsigned SPTag = SPTy.getTag();
1124 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1125 addType(SPDie, SPTy);
1127 addType(SPDie, DIType(Args.getElement(0)));
1129 unsigned VK = SP.getVirtuality();
1131 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1132 DIEBlock *Block = getDIEBlock();
1133 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1134 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1135 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1136 ContainingTypeMap.insert(std::make_pair(SPDie,
1137 SP.getContainingType()));
1140 if (!SP.isDefinition()) {
1141 addFlag(SPDie, dwarf::DW_AT_declaration);
1143 // Add arguments. Do not add arguments for subprogram definition. They will
1144 // be handled while processing variables.
1145 DICompositeType SPTy = SP.getType();
1146 DIArray Args = SPTy.getTypeArray();
1147 unsigned SPTag = SPTy.getTag();
1149 if (SPTag == dwarf::DW_TAG_subroutine_type)
1150 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1151 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1152 DIType ATy = DIType(Args.getElement(i));
1154 if (ATy.isArtificial())
1155 addFlag(Arg, dwarf::DW_AT_artificial);
1156 SPDie->addChild(Arg);
1160 if (SP.isArtificial())
1161 addFlag(SPDie, dwarf::DW_AT_artificial);
1163 if (!SP.isLocalToUnit())
1164 addFlag(SPDie, dwarf::DW_AT_external);
1166 if (SP.isOptimized())
1167 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1169 if (unsigned isa = Asm->getISAEncoding()) {
1170 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1176 // Return const expression if value is a GEP to access merged global
1178 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1179 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1180 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1181 if (!CE || CE->getNumOperands() != 3 ||
1182 CE->getOpcode() != Instruction::GetElementPtr)
1185 // First operand points to a global struct.
1186 Value *Ptr = CE->getOperand(0);
1187 if (!isa<GlobalValue>(Ptr) ||
1188 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1191 // Second operand is zero.
1192 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1193 if (!CI || !CI->isZero())
1196 // Third operand is offset.
1197 if (!isa<ConstantInt>(CE->getOperand(2)))
1203 /// createGlobalVariableDIE - create global variable DIE.
1204 void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
1205 // Check for pre-existence.
1209 DIGlobalVariable GV(N);
1213 DIE *VariableDIE = new DIE(GV.getTag());
1215 insertDIE(N, VariableDIE);
1218 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1219 StringRef LinkageName = GV.getLinkageName();
1220 bool isGlobalVariable = GV.getGlobal() != NULL;
1221 if (!LinkageName.empty() && isGlobalVariable)
1222 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1223 getRealLinkageName(LinkageName));
1225 DIType GTy = GV.getType();
1226 addType(VariableDIE, GTy);
1228 // Add scoping info.
1229 if (!GV.isLocalToUnit())
1230 addFlag(VariableDIE, dwarf::DW_AT_external);
1232 // Add line number info.
1233 addSourceLine(VariableDIE, GV);
1234 // Add to context owner.
1235 DIDescriptor GVContext = GV.getContext();
1236 addToContextOwner(VariableDIE, GVContext);
1238 bool addToAccelTable = false;
1239 DIE *VariableSpecDIE = NULL;
1240 if (isGlobalVariable) {
1241 addToAccelTable = true;
1242 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1243 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1244 addLabel(Block, 0, dwarf::DW_FORM_udata,
1245 Asm->Mang->getSymbol(GV.getGlobal()));
1246 // Do not create specification DIE if context is either compile unit
1248 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1249 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1250 // Create specification DIE.
1251 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1252 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1253 dwarf::DW_FORM_ref4, VariableDIE);
1254 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1255 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1256 addDie(VariableSpecDIE);
1258 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1260 } else if (const ConstantInt *CI =
1261 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1262 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1263 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1264 addToAccelTable = true;
1265 // GV is a merged global.
1266 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1267 Value *Ptr = CE->getOperand(0);
1268 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1269 addLabel(Block, 0, dwarf::DW_FORM_udata,
1270 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1271 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1272 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
1273 addUInt(Block, 0, dwarf::DW_FORM_udata,
1274 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1275 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1276 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1279 if (addToAccelTable) {
1280 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1281 addAccelName(GV.getName(), AddrDIE);
1283 // If the linkage name is different than the name, go ahead and output
1284 // that as well into the name table.
1285 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1286 addAccelName(GV.getLinkageName(), AddrDIE);
1292 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1293 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1295 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1296 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1298 // The LowerBound value defines the lower bounds which is typically zero for
1299 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1300 // Count == -1 then the array is unbounded and we do not emit
1301 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1302 // Count == 0, then the array has zero elements in which case we do not emit
1304 int64_t LowerBound = SR.getLo();
1305 int64_t DefaultLowerBound = getDefaultLowerBound();
1306 int64_t Count = SR.getCount();
1308 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1309 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1311 if (Count != -1 && Count != 0)
1312 // FIXME: An unbounded array should reference the expression that defines
1314 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
1316 Buffer.addChild(DW_Subrange);
1319 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1320 void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1321 DICompositeType *CTy) {
1322 Buffer.setTag(dwarf::DW_TAG_array_type);
1323 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1324 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1326 // Emit derived type.
1327 addType(&Buffer, CTy->getTypeDerivedFrom());
1328 DIArray Elements = CTy->getTypeArray();
1330 // Get an anonymous type for index type.
1331 DIE *IdxTy = getIndexTyDie();
1333 // Construct an anonymous type for index type.
1334 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1335 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1336 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1337 dwarf::DW_ATE_signed);
1339 setIndexTyDie(IdxTy);
1342 // Add subranges to array type.
1343 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1344 DIDescriptor Element = Elements.getElement(i);
1345 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1346 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1350 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1351 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1352 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1353 StringRef Name = ETy.getName();
1354 addString(Enumerator, dwarf::DW_AT_name, Name);
1355 int64_t Value = ETy.getEnumValue();
1356 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1360 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1362 void CompileUnit::constructContainingTypeDIEs() {
1363 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1364 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1365 DIE *SPDie = CI->first;
1366 const MDNode *N = CI->second;
1368 DIE *NDie = getDIE(N);
1369 if (!NDie) continue;
1370 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1374 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1375 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1376 StringRef Name = DV->getName();
1380 // Translate tag to proper Dwarf tag.
1381 unsigned Tag = DV->getTag();
1383 // Define variable debug information entry.
1384 DIE *VariableDie = new DIE(Tag);
1385 DbgVariable *AbsVar = DV->getAbstractVariable();
1386 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1388 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1389 dwarf::DW_FORM_ref4, AbsDIE);
1391 addString(VariableDie, dwarf::DW_AT_name, Name);
1392 addSourceLine(VariableDie, DV->getVariable());
1393 addType(VariableDie, DV->getType());
1396 if (DV->isArtificial())
1397 addFlag(VariableDie, dwarf::DW_AT_artificial);
1399 if (isScopeAbstract) {
1400 DV->setDIE(VariableDie);
1404 // Add variable address.
1406 unsigned Offset = DV->getDotDebugLocOffset();
1407 if (Offset != ~0U) {
1408 addLabel(VariableDie, dwarf::DW_AT_location,
1409 dwarf::DW_FORM_data4,
1410 Asm->GetTempSymbol("debug_loc", Offset));
1411 DV->setDIE(VariableDie);
1415 // Check if variable is described by a DBG_VALUE instruction.
1416 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1417 bool updated = false;
1418 if (DVInsn->getNumOperands() == 3) {
1419 if (DVInsn->getOperand(0).isReg()) {
1420 const MachineOperand RegOp = DVInsn->getOperand(0);
1421 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1422 if (DVInsn->getOperand(1).isImm() &&
1423 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1424 unsigned FrameReg = 0;
1425 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1427 TFI->getFrameIndexReference(*Asm->MF,
1428 DVInsn->getOperand(1).getImm(),
1430 MachineLocation Location(FrameReg, Offset);
1431 addVariableAddress(DV, VariableDie, Location);
1433 } else if (RegOp.getReg())
1434 addVariableAddress(DV, VariableDie,
1435 MachineLocation(RegOp.getReg()));
1438 else if (DVInsn->getOperand(0).isImm())
1440 addConstantValue(VariableDie, DVInsn->getOperand(0),
1442 else if (DVInsn->getOperand(0).isFPImm())
1444 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1445 else if (DVInsn->getOperand(0).isCImm())
1447 addConstantValue(VariableDie,
1448 DVInsn->getOperand(0).getCImm(),
1449 DV->getType().isUnsignedDIType());
1451 addVariableAddress(DV, VariableDie,
1452 Asm->getDebugValueLocation(DVInsn));
1456 // If variableDie is not updated then DBG_VALUE instruction does not
1457 // have valid variable info.
1461 DV->setDIE(VariableDie);
1464 // .. else use frame index.
1465 int FI = DV->getFrameIndex();
1467 unsigned FrameReg = 0;
1468 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1470 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1471 MachineLocation Location(FrameReg, Offset);
1472 addVariableAddress(DV, VariableDie, Location);
1476 DV->setDIE(VariableDie);
1480 /// createMemberDIE - Create new member DIE.
1481 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1482 DIE *MemberDie = new DIE(DT.getTag());
1483 StringRef Name = DT.getName();
1485 addString(MemberDie, dwarf::DW_AT_name, Name);
1487 addType(MemberDie, DT.getTypeDerivedFrom());
1489 addSourceLine(MemberDie, DT);
1491 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1492 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1494 uint64_t Size = DT.getSizeInBits();
1495 uint64_t FieldSize = DT.getOriginalTypeSize();
1497 if (Size != FieldSize) {
1499 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1500 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1502 uint64_t Offset = DT.getOffsetInBits();
1503 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1504 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1505 uint64_t FieldOffset = (HiMark - FieldSize);
1506 Offset -= FieldOffset;
1508 // Maybe we need to work from the other end.
1509 if (Asm->getDataLayout().isLittleEndian())
1510 Offset = FieldSize - (Offset + Size);
1511 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1513 // Here WD_AT_data_member_location points to the anonymous
1514 // field that includes this bit field.
1515 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1518 // This is not a bitfield.
1519 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1521 if (DT.getTag() == dwarf::DW_TAG_inheritance
1522 && DT.isVirtual()) {
1524 // For C++, virtual base classes are not at fixed offset. Use following
1525 // expression to extract appropriate offset from vtable.
1526 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1528 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1529 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1530 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1531 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1532 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1533 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1534 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1535 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1537 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1540 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1542 if (DT.isProtected())
1543 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1544 dwarf::DW_ACCESS_protected);
1545 else if (DT.isPrivate())
1546 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1547 dwarf::DW_ACCESS_private);
1548 // Otherwise C++ member and base classes are considered public.
1550 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1551 dwarf::DW_ACCESS_public);
1553 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1554 dwarf::DW_VIRTUALITY_virtual);
1556 // Objective-C properties.
1557 if (MDNode *PNode = DT.getObjCProperty())
1558 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1559 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1562 if (DT.isArtificial())
1563 addFlag(MemberDie, dwarf::DW_AT_artificial);
1565 // This is only for backward compatibility.
1566 StringRef PropertyName = DT.getObjCPropertyName();
1567 if (!PropertyName.empty()) {
1568 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1569 StringRef GetterName = DT.getObjCPropertyGetterName();
1570 if (!GetterName.empty())
1571 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1572 StringRef SetterName = DT.getObjCPropertySetterName();
1573 if (!SetterName.empty())
1574 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1575 unsigned PropertyAttributes = 0;
1576 if (DT.isReadOnlyObjCProperty())
1577 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1578 if (DT.isReadWriteObjCProperty())
1579 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1580 if (DT.isAssignObjCProperty())
1581 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1582 if (DT.isRetainObjCProperty())
1583 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1584 if (DT.isCopyObjCProperty())
1585 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1586 if (DT.isNonAtomicObjCProperty())
1587 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1588 if (PropertyAttributes)
1589 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1590 PropertyAttributes);