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/DIBuilder.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/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,
36 DwarfDebug *DW, DwarfUnits *DWU)
37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
39 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
42 /// ~CompileUnit - Destructor for compile unit.
43 CompileUnit::~CompileUnit() {
44 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
45 DIEBlocks[j]->~DIEBlock();
48 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
49 /// information entry.
50 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
51 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
55 /// getDefaultLowerBound - Return the default lower bound for an array. If the
56 /// DWARF version doesn't handle the language, return -1.
57 int64_t CompileUnit::getDefaultLowerBound() const {
62 case dwarf::DW_LANG_C89:
63 case dwarf::DW_LANG_C99:
64 case dwarf::DW_LANG_C:
65 case dwarf::DW_LANG_C_plus_plus:
66 case dwarf::DW_LANG_ObjC:
67 case dwarf::DW_LANG_ObjC_plus_plus:
70 case dwarf::DW_LANG_Fortran77:
71 case dwarf::DW_LANG_Fortran90:
72 case dwarf::DW_LANG_Fortran95:
75 // The languages below have valid values only if the DWARF version >= 4.
76 case dwarf::DW_LANG_Java:
77 case dwarf::DW_LANG_Python:
78 case dwarf::DW_LANG_UPC:
79 case dwarf::DW_LANG_D:
80 if (dwarf::DWARF_VERSION >= 4)
84 case dwarf::DW_LANG_Ada83:
85 case dwarf::DW_LANG_Ada95:
86 case dwarf::DW_LANG_Cobol74:
87 case dwarf::DW_LANG_Cobol85:
88 case dwarf::DW_LANG_Modula2:
89 case dwarf::DW_LANG_Pascal83:
90 case dwarf::DW_LANG_PLI:
91 if (dwarf::DWARF_VERSION >= 4)
99 /// addFlag - Add a flag that is true.
100 void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
101 if (!DD->useDarwinGDBCompat())
102 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
105 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
108 /// addUInt - Add an unsigned integer attribute data and value.
110 void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
111 unsigned Form, uint64_t Integer) {
112 if (!Form) Form = DIEInteger::BestForm(false, Integer);
113 DIEValue *Value = Integer == 1 ?
114 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
115 Die->addValue(Attribute, Form, Value);
118 /// addSInt - Add an signed integer attribute data and value.
120 void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
121 unsigned Form, int64_t Integer) {
122 if (!Form) Form = DIEInteger::BestForm(true, Integer);
123 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
124 Die->addValue(Attribute, Form, Value);
127 /// addString - Add a string attribute data and value. We always emit a
128 /// reference to the string pool instead of immediate strings so that DIEs have
129 /// more predictable sizes.
130 void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
131 MCSymbol *Symb = DU->getStringPoolEntry(String);
133 if (Asm->needsRelocationsForDwarfStringPool())
134 Value = new (DIEValueAllocator) DIELabel(Symb);
136 MCSymbol *StringPool = DU->getStringPoolSym();
137 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
139 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
142 /// addLabel - Add a Dwarf label attribute data and value.
144 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
145 const MCSymbol *Label) {
146 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
147 Die->addValue(Attribute, Form, Value);
150 /// addDelta - Add a label delta attribute data and value.
152 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
153 const MCSymbol *Hi, const MCSymbol *Lo) {
154 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
155 Die->addValue(Attribute, Form, Value);
158 /// addDIEEntry - Add a DIE attribute data and value.
160 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
162 Die->addValue(Attribute, Form, createDIEEntry(Entry));
165 /// addBlock - Add block data.
167 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
169 Block->ComputeSize(Asm);
170 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
171 Die->addValue(Attribute, Block->BestForm(), Block);
174 /// addSourceLine - Add location information to specified debug information
176 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
181 unsigned Line = V.getLineNumber();
184 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
185 V.getContext().getDirectory());
186 assert(FileID && "Invalid file id");
187 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
188 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
191 /// addSourceLine - Add location information to specified debug information
193 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
194 // Verify global variable.
198 unsigned Line = G.getLineNumber();
201 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory());
202 assert(FileID && "Invalid file id");
203 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
204 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
207 /// addSourceLine - Add location information to specified debug information
209 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
210 // Verify subprogram.
214 // If the line number is 0, don't add it.
215 unsigned Line = SP.getLineNumber();
219 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
221 assert(FileID && "Invalid file id");
222 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
223 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
226 /// addSourceLine - Add location information to specified debug information
228 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
233 unsigned Line = Ty.getLineNumber();
236 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
238 assert(FileID && "Invalid file id");
239 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
240 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
243 /// addSourceLine - Add location information to specified debug information
245 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
250 unsigned Line = Ty.getLineNumber();
253 DIFile File = Ty.getFile();
254 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
255 File.getDirectory());
256 assert(FileID && "Invalid file id");
257 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
258 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
261 /// addSourceLine - Add location information to specified debug information
263 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
268 unsigned Line = NS.getLineNumber();
271 StringRef FN = NS.getFilename();
273 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory());
274 assert(FileID && "Invalid file id");
275 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
276 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
279 /// addVariableAddress - Add DW_AT_location attribute for a
280 /// DbgVariable based on provided MachineLocation.
281 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
282 MachineLocation Location) {
283 if (DV->variableHasComplexAddress())
284 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
285 else if (DV->isBlockByrefVariable())
286 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
288 addAddress(Die, dwarf::DW_AT_location, Location);
291 /// addRegisterOp - Add register operand.
292 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
293 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
294 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
296 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
298 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
299 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
303 /// addRegisterOffset - Add register offset.
304 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
306 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
307 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
308 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
309 if (Reg == TRI->getFrameRegister(*Asm->MF))
310 // If variable offset is based in frame register then use fbreg.
311 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
313 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
315 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
316 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
318 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
321 /// addAddress - Add an address attribute to a die based on the location
323 void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
324 const MachineLocation &Location) {
325 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
327 if (Location.isReg())
328 addRegisterOp(Block, Location.getReg());
330 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
332 // Now attach the location information to the DIE.
333 addBlock(Die, Attribute, 0, Block);
336 /// addComplexAddress - Start with the address based on the location provided,
337 /// and generate the DWARF information necessary to find the actual variable
338 /// given the extra address information encoded in the DIVariable, starting from
339 /// the starting location. Add the DWARF information to the die.
341 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
343 const MachineLocation &Location) {
344 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
345 unsigned N = DV->getNumAddrElements();
347 if (Location.isReg()) {
348 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
349 // If first address element is OpPlus then emit
350 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
351 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
354 addRegisterOp(Block, Location.getReg());
357 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
360 uint64_t Element = DV->getAddrElement(i);
361 if (Element == DIBuilder::OpPlus) {
362 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
363 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
364 } else if (Element == DIBuilder::OpDeref) {
365 if (!Location.isReg())
366 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
367 } else llvm_unreachable("unknown DIBuilder Opcode");
370 // Now attach the location information to the DIE.
371 addBlock(Die, Attribute, 0, Block);
374 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
375 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
376 gives the variable VarName either the struct, or a pointer to the struct, as
377 its type. This is necessary for various behind-the-scenes things the
378 compiler needs to do with by-reference variables in Blocks.
380 However, as far as the original *programmer* is concerned, the variable
381 should still have type 'SomeType', as originally declared.
383 The function getBlockByrefType dives into the __Block_byref_x_VarName
384 struct to find the original type of the variable, which is then assigned to
385 the variable's Debug Information Entry as its real type. So far, so good.
386 However now the debugger will expect the variable VarName to have the type
387 SomeType. So we need the location attribute for the variable to be an
388 expression that explains to the debugger how to navigate through the
389 pointers and struct to find the actual variable of type SomeType.
391 The following function does just that. We start by getting
392 the "normal" location for the variable. This will be the location
393 of either the struct __Block_byref_x_VarName or the pointer to the
394 struct __Block_byref_x_VarName.
396 The struct will look something like:
398 struct __Block_byref_x_VarName {
400 struct __Block_byref_x_VarName *forwarding;
401 ... <various other fields>
403 ... <maybe more fields>
406 If we are given the struct directly (as our starting point) we
407 need to tell the debugger to:
409 1). Add the offset of the forwarding field.
411 2). Follow that pointer to get the real __Block_byref_x_VarName
412 struct to use (the real one may have been copied onto the heap).
414 3). Add the offset for the field VarName, to find the actual variable.
416 If we started with a pointer to the struct, then we need to
417 dereference that pointer first, before the other steps.
418 Translating this into DWARF ops, we will need to append the following
419 to the current location description for the variable:
421 DW_OP_deref -- optional, if we start with a pointer
422 DW_OP_plus_uconst <forward_fld_offset>
424 DW_OP_plus_uconst <varName_fld_offset>
426 That is what this function does. */
428 /// addBlockByrefAddress - Start with the address based on the location
429 /// provided, and generate the DWARF information necessary to find the
430 /// actual Block variable (navigating the Block struct) based on the
431 /// starting location. Add the DWARF information to the die. For
432 /// more information, read large comment just above here.
434 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
436 const MachineLocation &Location) {
437 DIType Ty = DV->getType();
439 unsigned Tag = Ty.getTag();
440 bool isPointer = false;
442 StringRef varName = DV->getName();
444 if (Tag == dwarf::DW_TAG_pointer_type) {
445 DIDerivedType DTy = DIDerivedType(Ty);
446 TmpTy = DTy.getTypeDerivedFrom();
450 DICompositeType blockStruct = DICompositeType(TmpTy);
452 // Find the __forwarding field and the variable field in the __Block_byref
454 DIArray Fields = blockStruct.getTypeArray();
455 DIDescriptor varField = DIDescriptor();
456 DIDescriptor forwardingField = DIDescriptor();
458 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
459 DIDescriptor Element = Fields.getElement(i);
460 DIDerivedType DT = DIDerivedType(Element);
461 StringRef fieldName = DT.getName();
462 if (fieldName == "__forwarding")
463 forwardingField = Element;
464 else if (fieldName == varName)
468 // Get the offsets for the forwarding field and the variable field.
469 unsigned forwardingFieldOffset =
470 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
471 unsigned varFieldOffset =
472 DIDerivedType(varField).getOffsetInBits() >> 3;
474 // Decode the original location, and use that as the start of the byref
475 // variable's location.
476 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
478 if (Location.isReg())
479 addRegisterOp(Block, Location.getReg());
481 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
483 // If we started with a pointer to the __Block_byref... struct, then
484 // the first thing we need to do is dereference the pointer (DW_OP_deref).
486 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
488 // Next add the offset for the '__forwarding' field:
489 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
490 // adding the offset if it's 0.
491 if (forwardingFieldOffset > 0) {
492 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
493 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
496 // Now dereference the __forwarding field to get to the real __Block_byref
497 // struct: DW_OP_deref.
498 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
500 // Now that we've got the real __Block_byref... struct, add the offset
501 // for the variable's field to get to the location of the actual variable:
502 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
503 if (varFieldOffset > 0) {
504 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
505 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
508 // Now attach the location information to the DIE.
509 addBlock(Die, Attribute, 0, Block);
512 /// isTypeSigned - Return true if the type is signed.
513 static bool isTypeSigned(DIType Ty, int *SizeInBits) {
514 if (Ty.isDerivedType())
515 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
516 if (Ty.isBasicType())
517 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
518 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
519 *SizeInBits = Ty.getSizeInBits();
525 /// addConstantValue - Add constant value entry in variable DIE.
526 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
528 assert(MO.isImm() && "Invalid machine operand!");
529 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
531 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
532 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
533 switch (SizeInBits) {
534 case 8: Form = dwarf::DW_FORM_data1; break;
535 case 16: Form = dwarf::DW_FORM_data2; break;
536 case 32: Form = dwarf::DW_FORM_data4; break;
537 case 64: Form = dwarf::DW_FORM_data8; break;
540 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
541 : addUInt(Block, 0, Form, MO.getImm());
543 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
547 /// addConstantFPValue - Add constant value entry in variable DIE.
548 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
549 assert (MO.isFPImm() && "Invalid machine operand!");
550 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
551 APFloat FPImm = MO.getFPImm()->getValueAPF();
553 // Get the raw data form of the floating point.
554 const APInt FltVal = FPImm.bitcastToAPInt();
555 const char *FltPtr = (const char*)FltVal.getRawData();
557 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
558 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
559 int Incr = (LittleEndian ? 1 : -1);
560 int Start = (LittleEndian ? 0 : NumBytes - 1);
561 int Stop = (LittleEndian ? NumBytes : -1);
563 // Output the constant to DWARF one byte at a time.
564 for (; Start != Stop; Start += Incr)
565 addUInt(Block, 0, dwarf::DW_FORM_data1,
566 (unsigned char)0xFF & FltPtr[Start]);
568 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
572 /// addConstantValue - Add constant value entry in variable DIE.
573 bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
575 unsigned CIBitWidth = CI->getBitWidth();
576 if (CIBitWidth <= 64) {
578 switch (CIBitWidth) {
579 case 8: form = dwarf::DW_FORM_data1; break;
580 case 16: form = dwarf::DW_FORM_data2; break;
581 case 32: form = dwarf::DW_FORM_data4; break;
582 case 64: form = dwarf::DW_FORM_data8; break;
584 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
587 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
589 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
593 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
595 // Get the raw data form of the large APInt.
596 const APInt Val = CI->getValue();
597 const uint64_t *Ptr64 = Val.getRawData();
599 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
600 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
602 // Output the constant to DWARF one byte at a time.
603 for (int i = 0; i < NumBytes; i++) {
606 c = Ptr64[i / 8] >> (8 * (i & 7));
608 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
609 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
612 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
616 /// addTemplateParams - Add template parameters in buffer.
617 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
618 // Add template parameters.
619 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
620 DIDescriptor Element = TParams.getElement(i);
621 if (Element.isTemplateTypeParameter())
622 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
623 DITemplateTypeParameter(Element)));
624 else if (Element.isTemplateValueParameter())
625 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
626 DITemplateValueParameter(Element)));
630 /// addToContextOwner - Add Die into the list of its context owner's children.
631 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
632 if (Context.isType()) {
633 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
634 ContextDIE->addChild(Die);
635 } else if (Context.isNameSpace()) {
636 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
637 ContextDIE->addChild(Die);
638 } else if (Context.isSubprogram()) {
639 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
640 ContextDIE->addChild(Die);
641 } else if (DIE *ContextDIE = getDIE(Context))
642 ContextDIE->addChild(Die);
647 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
649 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
653 DIE *TyDIE = getDIE(Ty);
658 TyDIE = new DIE(dwarf::DW_TAG_base_type);
659 insertDIE(Ty, TyDIE);
660 if (Ty.isBasicType())
661 constructTypeDIE(*TyDIE, DIBasicType(Ty));
662 else if (Ty.isCompositeType())
663 constructTypeDIE(*TyDIE, DICompositeType(Ty));
665 assert(Ty.isDerivedType() && "Unknown kind of DIType");
666 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
668 // If this is a named finished type then include it in the list of types
669 // for the accelerator tables.
670 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
671 bool IsImplementation = 0;
672 if (Ty.isCompositeType()) {
673 DICompositeType CT(Ty);
674 // A runtime language of 0 actually means C/C++ and that any
675 // non-negative value is some version of Objective-C/C++.
676 IsImplementation = (CT.getRunTimeLang() == 0) ||
677 CT.isObjcClassComplete();
679 unsigned Flags = IsImplementation ?
680 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
681 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
684 addToContextOwner(TyDIE, Ty.getContext());
688 /// addType - Add a new type attribute to the specified entity.
689 void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
693 // Check for pre-existence.
694 DIEEntry *Entry = getDIEEntry(Ty);
695 // If it exists then use the existing value.
697 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
702 DIE *Buffer = getOrCreateTypeDIE(Ty);
705 Entry = createDIEEntry(Buffer);
706 insertDIEEntry(Ty, Entry);
707 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
709 // If this is a complete composite type then include it in the
710 // list of global types.
714 /// addGlobalType - Add a new global type to the compile unit.
716 void CompileUnit::addGlobalType(DIType Ty) {
717 DIDescriptor Context = Ty.getContext();
718 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
719 && (!Context || Context.isCompileUnit() || Context.isFile()
720 || Context.isNameSpace()))
721 if (DIEEntry *Entry = getDIEEntry(Ty))
722 GlobalTypes[Ty.getName()] = Entry->getEntry();
725 /// addPubTypes - Add type for pubtypes section.
726 void CompileUnit::addPubTypes(DISubprogram SP) {
727 DICompositeType SPTy = SP.getType();
728 unsigned SPTag = SPTy.getTag();
729 if (SPTag != dwarf::DW_TAG_subroutine_type)
732 DIArray Args = SPTy.getTypeArray();
733 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
734 DIType ATy(Args.getElement(i));
741 /// constructTypeDIE - Construct basic type die from DIBasicType.
742 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
743 // Get core information.
744 StringRef Name = BTy.getName();
745 // Add name if not anonymous or intermediate type.
747 addString(&Buffer, dwarf::DW_AT_name, Name);
749 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
750 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
751 // Unspecified types has only name, nothing else.
755 Buffer.setTag(dwarf::DW_TAG_base_type);
756 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
759 uint64_t Size = BTy.getSizeInBits() >> 3;
760 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
763 /// constructTypeDIE - Construct derived type die from DIDerivedType.
764 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
765 // Get core information.
766 StringRef Name = DTy.getName();
767 uint64_t Size = DTy.getSizeInBits() >> 3;
768 unsigned Tag = DTy.getTag();
770 // FIXME - Workaround for templates.
771 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
775 // Map to main type, void will not have a type.
776 DIType FromTy = DTy.getTypeDerivedFrom();
777 addType(&Buffer, FromTy);
779 // Add name if not anonymous or intermediate type.
781 addString(&Buffer, dwarf::DW_AT_name, Name);
783 // Add size if non-zero (derived types might be zero-sized.)
784 if (Size && Tag != dwarf::DW_TAG_pointer_type)
785 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
787 // Add source line info if available and TyDesc is not a forward declaration.
788 if (!DTy.isForwardDecl())
789 addSourceLine(&Buffer, DTy);
792 /// constructTypeDIE - Construct type DIE from DICompositeType.
793 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
794 // Get core information.
795 StringRef Name = CTy.getName();
797 uint64_t Size = CTy.getSizeInBits() >> 3;
798 unsigned Tag = CTy.getTag();
802 case dwarf::DW_TAG_vector_type:
803 case dwarf::DW_TAG_array_type:
804 constructArrayTypeDIE(Buffer, &CTy);
806 case dwarf::DW_TAG_enumeration_type: {
807 DIArray Elements = CTy.getTypeArray();
809 // Add enumerators to enumeration type.
810 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
812 DIDescriptor Enum(Elements.getElement(i));
813 if (Enum.isEnumerator()) {
814 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
815 Buffer.addChild(ElemDie);
818 DIType DTy = CTy.getTypeDerivedFrom();
820 addType(&Buffer, DTy);
821 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
825 case dwarf::DW_TAG_subroutine_type: {
827 DIArray Elements = CTy.getTypeArray();
828 DIDescriptor RTy = Elements.getElement(0);
829 addType(&Buffer, DIType(RTy));
831 bool isPrototyped = true;
833 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
834 DIDescriptor Ty = Elements.getElement(i);
835 if (Ty.isUnspecifiedParameter()) {
836 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
837 Buffer.addChild(Arg);
838 isPrototyped = false;
840 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
841 addType(Arg, DIType(Ty));
842 Buffer.addChild(Arg);
845 // Add prototype flag if we're dealing with a C language and the
846 // function has been prototyped.
848 (Language == dwarf::DW_LANG_C89 ||
849 Language == dwarf::DW_LANG_C99 ||
850 Language == dwarf::DW_LANG_ObjC))
851 addFlag(&Buffer, dwarf::DW_AT_prototyped);
854 case dwarf::DW_TAG_structure_type:
855 case dwarf::DW_TAG_union_type:
856 case dwarf::DW_TAG_class_type: {
857 // Add elements to structure type.
858 DIArray Elements = CTy.getTypeArray();
860 // A forward struct declared type may not have elements available.
861 unsigned N = Elements.getNumElements();
865 // Add elements to structure type.
866 for (unsigned i = 0; i < N; ++i) {
867 DIDescriptor Element = Elements.getElement(i);
869 if (Element.isSubprogram()) {
870 DISubprogram SP(Element);
871 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
872 if (SP.isProtected())
873 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
874 dwarf::DW_ACCESS_protected);
875 else if (SP.isPrivate())
876 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
877 dwarf::DW_ACCESS_private);
879 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
880 dwarf::DW_ACCESS_public);
882 addFlag(ElemDie, dwarf::DW_AT_explicit);
884 else if (Element.isVariable()) {
885 DIVariable DV(Element);
886 ElemDie = new DIE(dwarf::DW_TAG_variable);
887 addString(ElemDie, dwarf::DW_AT_name, DV.getName());
888 addType(ElemDie, DV.getType());
889 addFlag(ElemDie, dwarf::DW_AT_declaration);
890 addFlag(ElemDie, dwarf::DW_AT_external);
891 addSourceLine(ElemDie, DV);
892 } else if (Element.isDerivedType()) {
893 DIDerivedType DDTy(Element);
894 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
895 ElemDie = new DIE(dwarf::DW_TAG_friend);
896 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
898 ElemDie = createMemberDIE(DIDerivedType(Element));
899 } else if (Element.isObjCProperty()) {
900 DIObjCProperty Property(Element);
901 ElemDie = new DIE(Property.getTag());
902 StringRef PropertyName = Property.getObjCPropertyName();
903 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
904 addType(ElemDie, Property.getType());
905 addSourceLine(ElemDie, Property);
906 StringRef GetterName = Property.getObjCPropertyGetterName();
907 if (!GetterName.empty())
908 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
909 StringRef SetterName = Property.getObjCPropertySetterName();
910 if (!SetterName.empty())
911 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
912 unsigned PropertyAttributes = 0;
913 if (Property.isReadOnlyObjCProperty())
914 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
915 if (Property.isReadWriteObjCProperty())
916 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
917 if (Property.isAssignObjCProperty())
918 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
919 if (Property.isRetainObjCProperty())
920 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
921 if (Property.isCopyObjCProperty())
922 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
923 if (Property.isNonAtomicObjCProperty())
924 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
925 if (PropertyAttributes)
926 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
929 DIEEntry *Entry = getDIEEntry(Element);
931 Entry = createDIEEntry(ElemDie);
932 insertDIEEntry(Element, Entry);
936 Buffer.addChild(ElemDie);
939 if (CTy.isAppleBlockExtension())
940 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
942 DICompositeType ContainingType = CTy.getContainingType();
943 if (DIDescriptor(ContainingType).isCompositeType())
944 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
945 getOrCreateTypeDIE(DIType(ContainingType)));
947 DIDescriptor Context = CTy.getContext();
948 addToContextOwner(&Buffer, Context);
951 if (CTy.isObjcClassComplete())
952 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
954 // Add template parameters to a class, structure or union types.
955 // FIXME: The support isn't in the metadata for this yet.
956 if (Tag == dwarf::DW_TAG_class_type ||
957 Tag == dwarf::DW_TAG_structure_type ||
958 Tag == dwarf::DW_TAG_union_type)
959 addTemplateParams(Buffer, CTy.getTemplateParams());
967 // Add name if not anonymous or intermediate type.
969 addString(&Buffer, dwarf::DW_AT_name, Name);
971 if (Tag == dwarf::DW_TAG_enumeration_type ||
972 Tag == dwarf::DW_TAG_class_type ||
973 Tag == dwarf::DW_TAG_structure_type ||
974 Tag == dwarf::DW_TAG_union_type) {
975 // Add size if non-zero (derived types might be zero-sized.)
976 // TODO: Do we care about size for enum forward declarations?
978 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
979 else if (!CTy.isForwardDecl())
980 // Add zero size if it is not a forward declaration.
981 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
983 // If we're a forward decl, say so.
984 if (CTy.isForwardDecl())
985 addFlag(&Buffer, dwarf::DW_AT_declaration);
987 // Add source line info if available.
988 if (!CTy.isForwardDecl())
989 addSourceLine(&Buffer, CTy);
991 // No harm in adding the runtime language to the declaration.
992 unsigned RLang = CTy.getRunTimeLang();
994 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
995 dwarf::DW_FORM_data1, RLang);
999 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
1000 /// for the given DITemplateTypeParameter.
1002 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1003 DIE *ParamDIE = getDIE(TP);
1007 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1008 addType(ParamDIE, TP.getType());
1009 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1013 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
1014 /// for the given DITemplateValueParameter.
1016 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
1017 DIE *ParamDIE = getDIE(TPV);
1021 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1022 addType(ParamDIE, TPV.getType());
1023 if (!TPV.getName().empty())
1024 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
1025 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1030 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1031 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1032 DIE *NDie = getDIE(NS);
1035 NDie = new DIE(dwarf::DW_TAG_namespace);
1036 insertDIE(NS, NDie);
1037 if (!NS.getName().empty()) {
1038 addString(NDie, dwarf::DW_AT_name, NS.getName());
1039 addAccelNamespace(NS.getName(), NDie);
1041 addAccelNamespace("(anonymous namespace)", NDie);
1042 addSourceLine(NDie, NS);
1043 addToContextOwner(NDie, NS.getContext());
1047 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1048 /// printer to not emit usual symbol prefix before the symbol name is used then
1049 /// return linkage name after skipping this special LLVM prefix.
1050 static StringRef getRealLinkageName(StringRef LinkageName) {
1052 if (LinkageName.startswith(StringRef(&One, 1)))
1053 return LinkageName.substr(1);
1057 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1058 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1059 DIE *SPDie = getDIE(SP);
1063 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1065 // DW_TAG_inlined_subroutine may refer to this DIE.
1066 insertDIE(SP, SPDie);
1068 DISubprogram SPDecl = SP.getFunctionDeclaration();
1069 DIE *DeclDie = NULL;
1070 if (SPDecl.isSubprogram()) {
1071 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1074 // Add to context owner.
1075 addToContextOwner(SPDie, SP.getContext());
1077 // Add function template parameters.
1078 addTemplateParams(*SPDie, SP.getTemplateParams());
1080 // Unfortunately this code needs to stay here instead of below the
1081 // AT_specification code in order to work around a bug in older
1082 // gdbs that requires the linkage name to resolve multiple template
1084 // TODO: Remove this set of code when we get rid of the old gdb
1086 StringRef LinkageName = SP.getLinkageName();
1087 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1088 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1089 getRealLinkageName(LinkageName));
1091 // If this DIE is going to refer declaration info using AT_specification
1092 // then there is no need to add other attributes.
1094 // Refer function declaration directly.
1095 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1101 // Add the linkage name if we have one.
1102 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1103 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1104 getRealLinkageName(LinkageName));
1106 // Constructors and operators for anonymous aggregates do not have names.
1107 if (!SP.getName().empty())
1108 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1110 addSourceLine(SPDie, SP);
1112 // Add the prototype if we have a prototype and we have a C like
1114 if (SP.isPrototyped() &&
1115 (Language == dwarf::DW_LANG_C89 ||
1116 Language == dwarf::DW_LANG_C99 ||
1117 Language == dwarf::DW_LANG_ObjC))
1118 addFlag(SPDie, dwarf::DW_AT_prototyped);
1121 DICompositeType SPTy = SP.getType();
1122 DIArray Args = SPTy.getTypeArray();
1123 unsigned SPTag = SPTy.getTag();
1125 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1126 addType(SPDie, SPTy);
1128 addType(SPDie, DIType(Args.getElement(0)));
1130 unsigned VK = SP.getVirtuality();
1132 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1133 DIEBlock *Block = getDIEBlock();
1134 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1135 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1136 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1137 ContainingTypeMap.insert(std::make_pair(SPDie,
1138 SP.getContainingType()));
1141 if (!SP.isDefinition()) {
1142 addFlag(SPDie, dwarf::DW_AT_declaration);
1144 // Add arguments. Do not add arguments for subprogram definition. They will
1145 // be handled while processing variables.
1146 DICompositeType SPTy = SP.getType();
1147 DIArray Args = SPTy.getTypeArray();
1148 unsigned SPTag = SPTy.getTag();
1150 if (SPTag == dwarf::DW_TAG_subroutine_type)
1151 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1152 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1153 DIType ATy = DIType(Args.getElement(i));
1155 if (ATy.isArtificial())
1156 addFlag(Arg, dwarf::DW_AT_artificial);
1157 SPDie->addChild(Arg);
1161 if (SP.isArtificial())
1162 addFlag(SPDie, dwarf::DW_AT_artificial);
1164 if (!SP.isLocalToUnit())
1165 addFlag(SPDie, dwarf::DW_AT_external);
1167 if (SP.isOptimized())
1168 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1170 if (unsigned isa = Asm->getISAEncoding()) {
1171 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1177 // Return const expression if value is a GEP to access merged global
1179 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1180 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1181 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1182 if (!CE || CE->getNumOperands() != 3 ||
1183 CE->getOpcode() != Instruction::GetElementPtr)
1186 // First operand points to a global struct.
1187 Value *Ptr = CE->getOperand(0);
1188 if (!isa<GlobalValue>(Ptr) ||
1189 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1192 // Second operand is zero.
1193 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1194 if (!CI || !CI->isZero())
1197 // Third operand is offset.
1198 if (!isa<ConstantInt>(CE->getOperand(2)))
1204 /// createGlobalVariableDIE - create global variable DIE.
1205 void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
1206 // Check for pre-existence.
1210 DIGlobalVariable GV(N);
1214 DIE *VariableDIE = new DIE(GV.getTag());
1216 insertDIE(N, VariableDIE);
1219 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1220 StringRef LinkageName = GV.getLinkageName();
1221 bool isGlobalVariable = GV.getGlobal() != NULL;
1222 if (!LinkageName.empty() && isGlobalVariable)
1223 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1224 getRealLinkageName(LinkageName));
1226 DIType GTy = GV.getType();
1227 addType(VariableDIE, GTy);
1229 // Add scoping info.
1230 if (!GV.isLocalToUnit())
1231 addFlag(VariableDIE, dwarf::DW_AT_external);
1233 // Add line number info.
1234 addSourceLine(VariableDIE, GV);
1235 // Add to context owner.
1236 DIDescriptor GVContext = GV.getContext();
1237 addToContextOwner(VariableDIE, GVContext);
1239 bool addToAccelTable = false;
1240 DIE *VariableSpecDIE = NULL;
1241 if (isGlobalVariable) {
1242 addToAccelTable = true;
1243 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1244 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1245 addLabel(Block, 0, dwarf::DW_FORM_udata,
1246 Asm->Mang->getSymbol(GV.getGlobal()));
1247 // Do not create specification DIE if context is either compile unit
1249 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1250 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1251 // Create specification DIE.
1252 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1253 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1254 dwarf::DW_FORM_ref4, VariableDIE);
1255 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1256 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1257 addDie(VariableSpecDIE);
1259 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1261 } else if (const ConstantInt *CI =
1262 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1263 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1264 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1265 addToAccelTable = true;
1266 // GV is a merged global.
1267 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1268 Value *Ptr = CE->getOperand(0);
1269 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1270 addLabel(Block, 0, dwarf::DW_FORM_udata,
1271 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1272 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1273 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
1274 addUInt(Block, 0, dwarf::DW_FORM_udata,
1275 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1276 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1277 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1280 if (addToAccelTable) {
1281 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1282 addAccelName(GV.getName(), AddrDIE);
1284 // If the linkage name is different than the name, go ahead and output
1285 // that as well into the name table.
1286 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1287 addAccelName(GV.getLinkageName(), AddrDIE);
1293 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1294 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1296 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1297 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1299 // The LowerBound value defines the lower bounds which is typically zero for
1300 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1301 // Count == -1 then the array is unbounded and we do not emit
1302 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1303 // Count == 0, then the array has zero elements in which case we do not emit
1305 int64_t LowerBound = SR.getLo();
1306 int64_t DefaultLowerBound = getDefaultLowerBound();
1307 int64_t Count = SR.getCount();
1309 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1310 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1312 if (Count != -1 && Count != 0)
1313 // FIXME: An unbounded array should reference the expression that defines
1315 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
1317 Buffer.addChild(DW_Subrange);
1320 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1321 void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1322 DICompositeType *CTy) {
1323 Buffer.setTag(dwarf::DW_TAG_array_type);
1324 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1325 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1327 // Emit derived type.
1328 addType(&Buffer, CTy->getTypeDerivedFrom());
1329 DIArray Elements = CTy->getTypeArray();
1331 // Get an anonymous type for index type.
1332 // FIXME: This type should be passed down from the front end
1333 // as different languages may have different sizes for indexes.
1334 DIE *IdxTy = getIndexTyDie();
1336 // Construct an anonymous type for index type.
1337 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1338 addString(IdxTy, dwarf::DW_AT_name, "int");
1339 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1340 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1341 dwarf::DW_ATE_signed);
1343 setIndexTyDie(IdxTy);
1346 // Add subranges to array type.
1347 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1348 DIDescriptor Element = Elements.getElement(i);
1349 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1350 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1354 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1355 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1356 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1357 StringRef Name = ETy.getName();
1358 addString(Enumerator, dwarf::DW_AT_name, Name);
1359 int64_t Value = ETy.getEnumValue();
1360 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1364 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1366 void CompileUnit::constructContainingTypeDIEs() {
1367 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1368 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1369 DIE *SPDie = CI->first;
1370 const MDNode *N = CI->second;
1372 DIE *NDie = getDIE(N);
1373 if (!NDie) continue;
1374 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1378 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1379 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1380 StringRef Name = DV->getName();
1382 // Translate tag to proper Dwarf tag.
1383 unsigned Tag = DV->getTag();
1385 // Define variable debug information entry.
1386 DIE *VariableDie = new DIE(Tag);
1387 DbgVariable *AbsVar = DV->getAbstractVariable();
1388 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1390 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1391 dwarf::DW_FORM_ref4, AbsDIE);
1393 addString(VariableDie, dwarf::DW_AT_name, Name);
1394 addSourceLine(VariableDie, DV->getVariable());
1395 addType(VariableDie, DV->getType());
1398 if (DV->isArtificial())
1399 addFlag(VariableDie, dwarf::DW_AT_artificial);
1401 if (isScopeAbstract) {
1402 DV->setDIE(VariableDie);
1406 // Add variable address.
1408 unsigned Offset = DV->getDotDebugLocOffset();
1409 if (Offset != ~0U) {
1410 addLabel(VariableDie, dwarf::DW_AT_location,
1411 dwarf::DW_FORM_data4,
1412 Asm->GetTempSymbol("debug_loc", Offset));
1413 DV->setDIE(VariableDie);
1417 // Check if variable is described by a DBG_VALUE instruction.
1418 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1419 bool updated = false;
1420 if (DVInsn->getNumOperands() == 3) {
1421 if (DVInsn->getOperand(0).isReg()) {
1422 const MachineOperand RegOp = DVInsn->getOperand(0);
1423 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1424 if (DVInsn->getOperand(1).isImm() &&
1425 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1426 unsigned FrameReg = 0;
1427 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1429 TFI->getFrameIndexReference(*Asm->MF,
1430 DVInsn->getOperand(1).getImm(),
1432 MachineLocation Location(FrameReg, Offset);
1433 addVariableAddress(DV, VariableDie, Location);
1435 } else if (RegOp.getReg())
1436 addVariableAddress(DV, VariableDie,
1437 MachineLocation(RegOp.getReg()));
1440 else if (DVInsn->getOperand(0).isImm())
1442 addConstantValue(VariableDie, DVInsn->getOperand(0),
1444 else if (DVInsn->getOperand(0).isFPImm())
1446 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1447 else if (DVInsn->getOperand(0).isCImm())
1449 addConstantValue(VariableDie,
1450 DVInsn->getOperand(0).getCImm(),
1451 DV->getType().isUnsignedDIType());
1453 addVariableAddress(DV, VariableDie,
1454 Asm->getDebugValueLocation(DVInsn));
1458 // If variableDie is not updated then DBG_VALUE instruction does not
1459 // have valid variable info.
1463 DV->setDIE(VariableDie);
1466 // .. else use frame index.
1467 int FI = DV->getFrameIndex();
1469 unsigned FrameReg = 0;
1470 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1472 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1473 MachineLocation Location(FrameReg, Offset);
1474 addVariableAddress(DV, VariableDie, Location);
1478 DV->setDIE(VariableDie);
1482 /// createMemberDIE - Create new member DIE.
1483 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1484 DIE *MemberDie = new DIE(DT.getTag());
1485 StringRef Name = DT.getName();
1487 addString(MemberDie, dwarf::DW_AT_name, Name);
1489 addType(MemberDie, DT.getTypeDerivedFrom());
1491 addSourceLine(MemberDie, DT);
1493 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1494 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1496 uint64_t Size = DT.getSizeInBits();
1497 uint64_t FieldSize = DT.getOriginalTypeSize();
1499 if (Size != FieldSize) {
1501 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1502 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1504 uint64_t Offset = DT.getOffsetInBits();
1505 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1506 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1507 uint64_t FieldOffset = (HiMark - FieldSize);
1508 Offset -= FieldOffset;
1510 // Maybe we need to work from the other end.
1511 if (Asm->getDataLayout().isLittleEndian())
1512 Offset = FieldSize - (Offset + Size);
1513 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1515 // Here WD_AT_data_member_location points to the anonymous
1516 // field that includes this bit field.
1517 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1520 // This is not a bitfield.
1521 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1523 if (DT.getTag() == dwarf::DW_TAG_inheritance
1524 && DT.isVirtual()) {
1526 // For C++, virtual base classes are not at fixed offset. Use following
1527 // expression to extract appropriate offset from vtable.
1528 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1530 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1531 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1532 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1533 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1534 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1535 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1536 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1537 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1539 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1542 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1544 if (DT.isProtected())
1545 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1546 dwarf::DW_ACCESS_protected);
1547 else if (DT.isPrivate())
1548 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1549 dwarf::DW_ACCESS_private);
1550 // Otherwise C++ member and base classes are considered public.
1552 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1553 dwarf::DW_ACCESS_public);
1555 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1556 dwarf::DW_VIRTUALITY_virtual);
1558 // Objective-C properties.
1559 if (MDNode *PNode = DT.getObjCProperty())
1560 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1561 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1564 if (DT.isArtificial())
1565 addFlag(MemberDie, dwarf::DW_AT_artificial);
1567 // This is only for backward compatibility.
1568 StringRef PropertyName = DT.getObjCPropertyName();
1569 if (!PropertyName.empty()) {
1570 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1571 StringRef GetterName = DT.getObjCPropertyGetterName();
1572 if (!GetterName.empty())
1573 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1574 StringRef SetterName = DT.getObjCPropertySetterName();
1575 if (!SetterName.empty())
1576 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1577 unsigned PropertyAttributes = 0;
1578 if (DT.isReadOnlyObjCProperty())
1579 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1580 if (DT.isReadWriteObjCProperty())
1581 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1582 if (DT.isAssignObjCProperty())
1583 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1584 if (DT.isRetainObjCProperty())
1585 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1586 if (DT.isCopyObjCProperty())
1587 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1588 if (DT.isNonAtomicObjCProperty())
1589 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1590 if (PropertyAttributes)
1591 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1592 PropertyAttributes);