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/Target/Mangler.h"
26 #include "llvm/Target/TargetFrameLowering.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 #include "llvm/Target/TargetRegisterInfo.h"
33 /// CompileUnit - Compile unit constructor.
34 CompileUnit::CompileUnit(unsigned UID, DIE *D, const MDNode *N, AsmPrinter *A,
35 DwarfDebug *DW, DwarfUnits *DWU)
36 : UniqueID(UID), Node(N), CUDie(D), Asm(A), DD(DW), DU(DWU), IndexTyDie(0),
38 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 {
58 switch (DICompileUnit(Node).getLanguage()) {
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, dwarf::Attribute Attribute) {
101 if (DD->getDwarfVersion() >= 4)
102 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
104 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
107 /// addUInt - Add an unsigned integer attribute data and value.
109 void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
110 Optional<dwarf::Form> Form, uint64_t Integer) {
112 Form = DIEInteger::BestForm(false, Integer);
113 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
115 Die->addValue(Attribute, *Form, Value);
118 void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
119 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
122 /// addSInt - Add an signed integer attribute data and value.
124 void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
125 Optional<dwarf::Form> Form, int64_t Integer) {
127 Form = DIEInteger::BestForm(true, Integer);
128 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
129 Die->addValue(Attribute, *Form, Value);
132 void CompileUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
134 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
137 /// addString - Add a string attribute data and value. We always emit a
138 /// reference to the string pool instead of immediate strings so that DIEs have
139 /// more predictable sizes. In the case of split dwarf we emit an index
140 /// into another table which gets us the static offset into the string
142 void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute, StringRef String) {
145 if (!DD->useSplitDwarf()) {
146 MCSymbol *Symb = DU->getStringPoolEntry(String);
147 if (Asm->needsRelocationsForDwarfStringPool())
148 Value = new (DIEValueAllocator) DIELabel(Symb);
150 MCSymbol *StringPool = DU->getStringPoolSym();
151 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
153 Form = dwarf::DW_FORM_strp;
155 unsigned idx = DU->getStringPoolIndex(String);
156 Value = new (DIEValueAllocator) DIEInteger(idx);
157 Form = dwarf::DW_FORM_GNU_str_index;
159 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
160 Die->addValue(Attribute, Form, Str);
163 /// addLocalString - Add a string attribute data and value. This is guaranteed
164 /// to be in the local string pool instead of indirected.
165 void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
167 MCSymbol *Symb = DU->getStringPoolEntry(String);
169 if (Asm->needsRelocationsForDwarfStringPool())
170 Value = new (DIEValueAllocator) DIELabel(Symb);
172 MCSymbol *StringPool = DU->getStringPoolSym();
173 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
175 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
178 /// addExpr - Add a Dwarf expression attribute data and value.
180 void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
181 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
182 Die->addValue((dwarf::Attribute)0, Form, Value);
185 /// addLabel - Add a Dwarf label attribute data and value.
187 void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
188 const MCSymbol *Label) {
189 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
190 Die->addValue(Attribute, Form, Value);
193 void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
194 const MCSymbol *Label) {
195 addLabel(Die, (dwarf::Attribute)0, Form, Label);
198 /// addLabelAddress - Add a dwarf label attribute data and value using
199 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
201 void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
204 DD->addArangeLabel(SymbolCU(this, Label));
206 if (!DD->useSplitDwarf()) {
208 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
209 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
211 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
212 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
215 unsigned idx = DU->getAddrPoolIndex(Label);
216 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
217 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
221 /// addOpAddress - Add a dwarf op address data and value using the
222 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
224 void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
225 DD->addArangeLabel(SymbolCU(this, Sym));
226 if (!DD->useSplitDwarf()) {
227 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
228 addLabel(Die, dwarf::DW_FORM_udata, Sym);
230 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
231 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
235 /// addDelta - Add a label delta attribute data and value.
237 void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
238 const MCSymbol *Hi, const MCSymbol *Lo) {
239 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
240 Die->addValue(Attribute, Form, Value);
243 /// addDIEEntry - Add a DIE attribute data and value.
245 void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
246 // We currently only use ref4.
247 Die->addValue(Attribute, dwarf::DW_FORM_ref4, createDIEEntry(Entry));
250 /// Create a DIE with the given Tag, add the DIE to its parent, and
251 /// call insertDIE if MD is not null.
252 DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const MDNode *MD) {
253 DIE *Die = new DIE(Tag);
254 Parent.addChild(Die);
260 /// addBlock - Add block data.
262 void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
264 Block->ComputeSize(Asm);
265 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
266 Die->addValue(Attribute, Block->BestForm(), Block);
269 /// addSourceLine - Add location information to specified debug information
271 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
276 unsigned Line = V.getLineNumber();
280 DD->getOrCreateSourceID(V.getContext().getFilename(),
281 V.getContext().getDirectory(), getUniqueID());
282 assert(FileID && "Invalid file id");
283 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
284 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
287 /// addSourceLine - Add location information to specified debug information
289 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
290 // Verify global variable.
291 if (!G.isGlobalVariable())
294 unsigned Line = G.getLineNumber();
298 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
299 assert(FileID && "Invalid file id");
300 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
301 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
304 /// addSourceLine - Add location information to specified debug information
306 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
307 // Verify subprogram.
308 if (!SP.isSubprogram())
311 // If the line number is 0, don't add it.
312 unsigned Line = SP.getLineNumber();
316 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
318 assert(FileID && "Invalid file id");
319 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
320 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
323 /// addSourceLine - Add location information to specified debug information
325 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
330 unsigned Line = Ty.getLineNumber();
333 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
335 assert(FileID && "Invalid file id");
336 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
337 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
340 /// addSourceLine - Add location information to specified debug information
342 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
344 if (!Ty.isObjCProperty())
347 unsigned Line = Ty.getLineNumber();
350 DIFile File = Ty.getFile();
351 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
352 File.getDirectory(), getUniqueID());
353 assert(FileID && "Invalid file id");
354 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
355 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
358 /// addSourceLine - Add location information to specified debug information
360 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
365 unsigned Line = NS.getLineNumber();
368 StringRef FN = NS.getFilename();
371 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
372 assert(FileID && "Invalid file id");
373 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
374 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
377 /// addVariableAddress - Add DW_AT_location attribute for a
378 /// DbgVariable based on provided MachineLocation.
379 void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
380 MachineLocation Location) {
381 if (DV.variableHasComplexAddress())
382 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
383 else if (DV.isBlockByrefVariable())
384 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
386 addAddress(Die, dwarf::DW_AT_location, Location,
387 DV.getVariable().isIndirect());
390 /// addRegisterOp - Add register operand.
391 void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
392 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
393 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
395 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
397 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
398 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
402 /// addRegisterOffset - Add register offset.
403 void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
405 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
406 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
407 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
408 if (Reg == TRI->getFrameRegister(*Asm->MF))
409 // If variable offset is based in frame register then use fbreg.
410 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
412 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
414 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
415 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
417 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
420 /// addAddress - Add an address attribute to a die based on the location
422 void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
423 const MachineLocation &Location, bool Indirect) {
424 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
426 if (Location.isReg() && !Indirect)
427 addRegisterOp(Block, Location.getReg());
429 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
430 if (Indirect && !Location.isReg()) {
431 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
435 // Now attach the location information to the DIE.
436 addBlock(Die, Attribute, Block);
439 /// addComplexAddress - Start with the address based on the location provided,
440 /// and generate the DWARF information necessary to find the actual variable
441 /// given the extra address information encoded in the DIVariable, starting from
442 /// the starting location. Add the DWARF information to the die.
444 void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
445 dwarf::Attribute Attribute,
446 const MachineLocation &Location) {
447 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
448 unsigned N = DV.getNumAddrElements();
450 if (Location.isReg()) {
451 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
452 // If first address element is OpPlus then emit
453 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
454 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
457 addRegisterOp(Block, Location.getReg());
459 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
462 uint64_t Element = DV.getAddrElement(i);
463 if (Element == DIBuilder::OpPlus) {
464 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
465 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
466 } else if (Element == DIBuilder::OpDeref) {
467 if (!Location.isReg())
468 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
470 llvm_unreachable("unknown DIBuilder Opcode");
473 // Now attach the location information to the DIE.
474 addBlock(Die, Attribute, Block);
477 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
478 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
479 gives the variable VarName either the struct, or a pointer to the struct, as
480 its type. This is necessary for various behind-the-scenes things the
481 compiler needs to do with by-reference variables in Blocks.
483 However, as far as the original *programmer* is concerned, the variable
484 should still have type 'SomeType', as originally declared.
486 The function getBlockByrefType dives into the __Block_byref_x_VarName
487 struct to find the original type of the variable, which is then assigned to
488 the variable's Debug Information Entry as its real type. So far, so good.
489 However now the debugger will expect the variable VarName to have the type
490 SomeType. So we need the location attribute for the variable to be an
491 expression that explains to the debugger how to navigate through the
492 pointers and struct to find the actual variable of type SomeType.
494 The following function does just that. We start by getting
495 the "normal" location for the variable. This will be the location
496 of either the struct __Block_byref_x_VarName or the pointer to the
497 struct __Block_byref_x_VarName.
499 The struct will look something like:
501 struct __Block_byref_x_VarName {
503 struct __Block_byref_x_VarName *forwarding;
504 ... <various other fields>
506 ... <maybe more fields>
509 If we are given the struct directly (as our starting point) we
510 need to tell the debugger to:
512 1). Add the offset of the forwarding field.
514 2). Follow that pointer to get the real __Block_byref_x_VarName
515 struct to use (the real one may have been copied onto the heap).
517 3). Add the offset for the field VarName, to find the actual variable.
519 If we started with a pointer to the struct, then we need to
520 dereference that pointer first, before the other steps.
521 Translating this into DWARF ops, we will need to append the following
522 to the current location description for the variable:
524 DW_OP_deref -- optional, if we start with a pointer
525 DW_OP_plus_uconst <forward_fld_offset>
527 DW_OP_plus_uconst <varName_fld_offset>
529 That is what this function does. */
531 /// addBlockByrefAddress - Start with the address based on the location
532 /// provided, and generate the DWARF information necessary to find the
533 /// actual Block variable (navigating the Block struct) based on the
534 /// starting location. Add the DWARF information to the die. For
535 /// more information, read large comment just above here.
537 void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
538 dwarf::Attribute Attribute,
539 const MachineLocation &Location) {
540 DIType Ty = DV.getType();
542 uint16_t Tag = Ty.getTag();
543 bool isPointer = false;
545 StringRef varName = DV.getName();
547 if (Tag == dwarf::DW_TAG_pointer_type) {
548 DIDerivedType DTy = DIDerivedType(Ty);
549 TmpTy = resolve(DTy.getTypeDerivedFrom());
553 DICompositeType blockStruct = DICompositeType(TmpTy);
555 // Find the __forwarding field and the variable field in the __Block_byref
557 DIArray Fields = blockStruct.getTypeArray();
558 DIDescriptor varField = DIDescriptor();
559 DIDescriptor forwardingField = DIDescriptor();
561 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
562 DIDescriptor Element = Fields.getElement(i);
563 DIDerivedType DT = DIDerivedType(Element);
564 StringRef fieldName = DT.getName();
565 if (fieldName == "__forwarding")
566 forwardingField = Element;
567 else if (fieldName == varName)
571 // Get the offsets for the forwarding field and the variable field.
572 unsigned forwardingFieldOffset =
573 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
574 unsigned varFieldOffset = DIDerivedType(varField).getOffsetInBits() >> 3;
576 // Decode the original location, and use that as the start of the byref
577 // variable's location.
578 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
580 if (Location.isReg())
581 addRegisterOp(Block, Location.getReg());
583 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
585 // If we started with a pointer to the __Block_byref... struct, then
586 // the first thing we need to do is dereference the pointer (DW_OP_deref).
588 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
590 // Next add the offset for the '__forwarding' field:
591 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
592 // adding the offset if it's 0.
593 if (forwardingFieldOffset > 0) {
594 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
595 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
598 // Now dereference the __forwarding field to get to the real __Block_byref
599 // struct: DW_OP_deref.
600 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
602 // Now that we've got the real __Block_byref... struct, add the offset
603 // for the variable's field to get to the location of the actual variable:
604 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
605 if (varFieldOffset > 0) {
606 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
607 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
610 // Now attach the location information to the DIE.
611 addBlock(Die, Attribute, Block);
614 /// isTypeSigned - Return true if the type is signed.
615 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
616 if (Ty.isDerivedType())
617 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
619 if (Ty.isBasicType())
620 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
621 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
622 *SizeInBits = Ty.getSizeInBits();
628 /// Return true if type encoding is unsigned.
629 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
630 DIDerivedType DTy(Ty);
631 if (DTy.isDerivedType())
632 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
635 if (BTy.isBasicType()) {
636 unsigned Encoding = BTy.getEncoding();
637 if (Encoding == dwarf::DW_ATE_unsigned ||
638 Encoding == dwarf::DW_ATE_unsigned_char ||
639 Encoding == dwarf::DW_ATE_boolean)
645 /// If this type is derived from a base type then return base type size.
646 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
647 unsigned Tag = Ty.getTag();
649 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
650 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
651 Tag != dwarf::DW_TAG_restrict_type)
652 return Ty.getSizeInBits();
654 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
656 // If this type is not derived from any type then take conservative approach.
657 if (!BaseType.isValid())
658 return Ty.getSizeInBits();
660 // If this is a derived type, go ahead and get the base type, unless it's a
661 // reference then it's just the size of the field. Pointer types have no need
662 // of this since they're a different type of qualification on the type.
663 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
664 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
665 return Ty.getSizeInBits();
667 if (BaseType.isDerivedType())
668 return getBaseTypeSize(DD, DIDerivedType(BaseType));
670 return BaseType.getSizeInBits();
673 /// addConstantValue - Add constant value entry in variable DIE.
674 void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
676 // FIXME: This is a bit conservative/simple - it emits negative values at
677 // their maximum bit width which is a bit unfortunate (& doesn't prefer
678 // udata/sdata over dataN as suggested by the DWARF spec)
679 assert(MO.isImm() && "Invalid machine operand!");
681 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
684 // If we're a signed constant definitely use sdata.
685 if (SignedConstant) {
686 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
690 // Else use data for now unless it's larger than we can deal with.
691 switch (SizeInBits) {
693 Form = dwarf::DW_FORM_data1;
696 Form = dwarf::DW_FORM_data2;
699 Form = dwarf::DW_FORM_data4;
702 Form = dwarf::DW_FORM_data8;
705 Form = dwarf::DW_FORM_udata;
706 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
709 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
712 /// addConstantFPValue - Add constant value entry in variable DIE.
713 void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
714 assert(MO.isFPImm() && "Invalid machine operand!");
715 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
716 APFloat FPImm = MO.getFPImm()->getValueAPF();
718 // Get the raw data form of the floating point.
719 const APInt FltVal = FPImm.bitcastToAPInt();
720 const char *FltPtr = (const char *)FltVal.getRawData();
722 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
723 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
724 int Incr = (LittleEndian ? 1 : -1);
725 int Start = (LittleEndian ? 0 : NumBytes - 1);
726 int Stop = (LittleEndian ? NumBytes : -1);
728 // Output the constant to DWARF one byte at a time.
729 for (; Start != Stop; Start += Incr)
730 addUInt(Block, dwarf::DW_FORM_data1,
731 (unsigned char)0xFF & FltPtr[Start]);
733 addBlock(Die, dwarf::DW_AT_const_value, Block);
736 /// addConstantFPValue - Add constant value entry in variable DIE.
737 void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
738 // Pass this down to addConstantValue as an unsigned bag of bits.
739 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
742 /// addConstantValue - Add constant value entry in variable DIE.
743 void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
745 addConstantValue(Die, CI->getValue(), Unsigned);
748 // addConstantValue - Add constant value entry in variable DIE.
749 void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
750 unsigned CIBitWidth = Val.getBitWidth();
751 if (CIBitWidth <= 64) {
752 // If we're a signed constant definitely use sdata.
754 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
759 // Else use data for now unless it's larger than we can deal with.
761 switch (CIBitWidth) {
763 Form = dwarf::DW_FORM_data1;
766 Form = dwarf::DW_FORM_data2;
769 Form = dwarf::DW_FORM_data4;
772 Form = dwarf::DW_FORM_data8;
775 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
779 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
783 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
785 // Get the raw data form of the large APInt.
786 const uint64_t *Ptr64 = Val.getRawData();
788 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
789 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
791 // Output the constant to DWARF one byte at a time.
792 for (int i = 0; i < NumBytes; i++) {
795 c = Ptr64[i / 8] >> (8 * (i & 7));
797 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
798 addUInt(Block, dwarf::DW_FORM_data1, c);
801 addBlock(Die, dwarf::DW_AT_const_value, Block);
804 /// addTemplateParams - Add template parameters into buffer.
805 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
806 // Add template parameters.
807 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
808 DIDescriptor Element = TParams.getElement(i);
809 if (Element.isTemplateTypeParameter())
810 constructTemplateTypeParameterDIE(Buffer,
811 DITemplateTypeParameter(Element));
812 else if (Element.isTemplateValueParameter())
813 constructTemplateValueParameterDIE(Buffer,
814 DITemplateValueParameter(Element));
818 /// getOrCreateContextDIE - Get context owner's DIE.
819 DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) {
820 if (Context.isType())
821 return getOrCreateTypeDIE(DIType(Context));
822 else if (Context.isNameSpace())
823 return getOrCreateNameSpace(DINameSpace(Context));
824 else if (Context.isSubprogram())
825 return getOrCreateSubprogramDIE(DISubprogram(Context));
827 return getDIE(Context);
830 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
832 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
837 // Construct the context before querying for the existence of the DIE in case
838 // such construction creates the DIE.
839 DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext()));
841 ContextDIE = CUDie.get();
843 DIE *TyDIE = getDIE(Ty);
848 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
850 if (Ty.isBasicType())
851 constructTypeDIE(*TyDIE, DIBasicType(Ty));
852 else if (Ty.isCompositeType())
853 constructTypeDIE(*TyDIE, DICompositeType(Ty));
855 assert(Ty.isDerivedType() && "Unknown kind of DIType");
856 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
858 // If this is a named finished type then include it in the list of types
859 // for the accelerator tables.
860 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
861 bool IsImplementation = 0;
862 if (Ty.isCompositeType()) {
863 DICompositeType CT(Ty);
864 // A runtime language of 0 actually means C/C++ and that any
865 // non-negative value is some version of Objective-C/C++.
866 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
868 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
869 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
875 /// addType - Add a new type attribute to the specified entity.
876 void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
877 assert(Ty && "Trying to add a type that doesn't exist?");
879 // Check for pre-existence.
880 DIEEntry *Entry = getDIEEntry(Ty);
881 // If it exists then use the existing value.
883 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
888 DIE *Buffer = getOrCreateTypeDIE(Ty);
891 Entry = createDIEEntry(Buffer);
892 insertDIEEntry(Ty, Entry);
893 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
895 // If this is a complete composite type then include it in the
896 // list of global types.
900 // Accelerator table mutators - add each name along with its companion
901 // DIE to the proper table while ensuring that the name that we're going
902 // to reference is in the string table. We do this since the names we
903 // add may not only be identical to the names in the DIE.
904 void CompileUnit::addAccelName(StringRef Name, DIE *Die) {
905 DU->getStringPoolEntry(Name);
906 std::vector<DIE *> &DIEs = AccelNames[Name];
910 void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) {
911 DU->getStringPoolEntry(Name);
912 std::vector<DIE *> &DIEs = AccelObjC[Name];
916 void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) {
917 DU->getStringPoolEntry(Name);
918 std::vector<DIE *> &DIEs = AccelNamespace[Name];
922 void CompileUnit::addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die) {
923 DU->getStringPoolEntry(Name);
924 std::vector<std::pair<DIE *, unsigned> > &DIEs = AccelTypes[Name];
928 /// addGlobalName - Add a new global name to the compile unit.
929 void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
930 std::string FullName = getParentContextString(Context) + Name.str();
931 GlobalNames[FullName] = Die;
934 /// addGlobalType - Add a new global type to the compile unit.
936 void CompileUnit::addGlobalType(DIType Ty) {
937 DIScope Context = resolve(Ty.getContext());
938 if (!Ty.getName().empty() && !Ty.isForwardDecl() &&
939 (!Context || Context.isCompileUnit() || Context.isFile() ||
940 Context.isNameSpace()))
941 if (DIEEntry *Entry = getDIEEntry(Ty)) {
942 std::string FullName =
943 getParentContextString(Context) + Ty.getName().str();
944 GlobalTypes[FullName] = Entry->getEntry();
948 /// getParentContextString - Walks the metadata parent chain in a language
949 /// specific manner (using the compile unit language) and returns
950 /// it as a string. This is done at the metadata level because DIEs may
951 /// not currently have been added to the parent context and walking the
952 /// DIEs looking for names is more expensive than walking the metadata.
953 std::string CompileUnit::getParentContextString(DIScope Context) const {
957 // FIXME: Decide whether to implement this for non-C++ languages.
958 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
962 SmallVector<DIScope, 1> Parents;
963 while (!Context.isCompileUnit()) {
964 Parents.push_back(Context);
965 if (Context.getContext())
966 Context = resolve(Context.getContext());
968 // Structure, etc types will have a NULL context if they're at the top
973 // Reverse iterate over our list to go from the outermost construct to the
975 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
979 StringRef Name = Ctx.getName();
988 /// addPubTypes - Add subprogram argument types for pubtypes section.
989 void CompileUnit::addPubTypes(DISubprogram SP) {
990 DICompositeType SPTy = SP.getType();
991 uint16_t SPTag = SPTy.getTag();
992 if (SPTag != dwarf::DW_TAG_subroutine_type)
995 DIArray Args = SPTy.getTypeArray();
996 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
997 DIType ATy(Args.getElement(i));
1004 /// constructTypeDIE - Construct basic type die from DIBasicType.
1005 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1006 // Get core information.
1007 StringRef Name = BTy.getName();
1008 // Add name if not anonymous or intermediate type.
1010 addString(&Buffer, dwarf::DW_AT_name, Name);
1012 // An unspecified type only has a name attribute.
1013 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
1016 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1019 uint64_t Size = BTy.getSizeInBits() >> 3;
1020 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1023 /// constructTypeDIE - Construct derived type die from DIDerivedType.
1024 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1025 // Get core information.
1026 StringRef Name = DTy.getName();
1027 uint64_t Size = DTy.getSizeInBits() >> 3;
1028 uint16_t Tag = Buffer.getTag();
1030 // Map to main type, void will not have a type.
1031 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
1033 addType(&Buffer, FromTy);
1035 // Add name if not anonymous or intermediate type.
1037 addString(&Buffer, dwarf::DW_AT_name, Name);
1039 // Add size if non-zero (derived types might be zero-sized.)
1040 if (Size && Tag != dwarf::DW_TAG_pointer_type)
1041 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1043 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
1044 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1045 getOrCreateTypeDIE(resolve(DTy.getClassType())));
1046 // Add source line info if available and TyDesc is not a forward declaration.
1047 if (!DTy.isForwardDecl())
1048 addSourceLine(&Buffer, DTy);
1051 /// Return true if the type is appropriately scoped to be contained inside
1052 /// its own type unit.
1053 static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
1054 DIScope Parent = DD->resolve(Ty.getContext());
1056 // Don't generate a hash for anything scoped inside a function.
1057 if (Parent.isSubprogram())
1059 Parent = DD->resolve(Parent.getContext());
1064 /// Return true if the type should be split out into a type unit.
1065 static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
1066 uint16_t Tag = CTy.getTag();
1069 case dwarf::DW_TAG_structure_type:
1070 case dwarf::DW_TAG_union_type:
1071 case dwarf::DW_TAG_enumeration_type:
1072 case dwarf::DW_TAG_class_type:
1073 // If this is a class, structure, union, or enumeration type
1074 // that is a definition (not a declaration), and not scoped
1075 // inside a function then separate this out as a type unit.
1076 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
1082 /// constructTypeDIE - Construct type DIE from DICompositeType.
1083 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1084 // Get core information.
1085 StringRef Name = CTy.getName();
1087 uint64_t Size = CTy.getSizeInBits() >> 3;
1088 uint16_t Tag = Buffer.getTag();
1091 case dwarf::DW_TAG_array_type:
1092 constructArrayTypeDIE(Buffer, &CTy);
1094 case dwarf::DW_TAG_enumeration_type: {
1095 DIArray Elements = CTy.getTypeArray();
1097 // Add enumerators to enumeration type.
1098 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1099 DIDescriptor Enum(Elements.getElement(i));
1100 if (Enum.isEnumerator())
1101 constructEnumTypeDIE(Buffer, DIEnumerator(Enum));
1103 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1105 addType(&Buffer, DTy);
1106 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1109 case dwarf::DW_TAG_subroutine_type: {
1110 // Add return type. A void return won't have a type.
1111 DIArray Elements = CTy.getTypeArray();
1112 DIDescriptor RTy = Elements.getElement(0);
1114 addType(&Buffer, DIType(RTy));
1116 bool isPrototyped = true;
1118 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1119 DIDescriptor Ty = Elements.getElement(i);
1120 if (Ty.isUnspecifiedParameter()) {
1121 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1122 isPrototyped = false;
1124 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1125 addType(Arg, DIType(Ty));
1126 if (DIType(Ty).isArtificial())
1127 addFlag(Arg, dwarf::DW_AT_artificial);
1130 // Add prototype flag if we're dealing with a C language and the
1131 // function has been prototyped.
1132 uint16_t Language = DICompileUnit(Node).getLanguage();
1134 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1135 Language == dwarf::DW_LANG_ObjC))
1136 addFlag(&Buffer, dwarf::DW_AT_prototyped);
1138 case dwarf::DW_TAG_structure_type:
1139 case dwarf::DW_TAG_union_type:
1140 case dwarf::DW_TAG_class_type: {
1141 // Add elements to structure type.
1142 DIArray Elements = CTy.getTypeArray();
1143 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1144 DIDescriptor Element = Elements.getElement(i);
1145 DIE *ElemDie = NULL;
1146 if (Element.isSubprogram()) {
1147 DISubprogram SP(Element);
1148 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1149 if (SP.isProtected())
1150 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1151 dwarf::DW_ACCESS_protected);
1152 else if (SP.isPrivate())
1153 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1154 dwarf::DW_ACCESS_private);
1156 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1157 dwarf::DW_ACCESS_public);
1158 if (SP.isExplicit())
1159 addFlag(ElemDie, dwarf::DW_AT_explicit);
1160 } else if (Element.isDerivedType()) {
1161 DIDerivedType DDTy(Element);
1162 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1163 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1164 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
1165 dwarf::DW_AT_friend);
1166 } else if (DDTy.isStaticMember()) {
1167 getOrCreateStaticMemberDIE(DDTy);
1169 constructMemberDIE(Buffer, DDTy);
1171 } else if (Element.isObjCProperty()) {
1172 DIObjCProperty Property(Element);
1173 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
1174 StringRef PropertyName = Property.getObjCPropertyName();
1175 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1176 addType(ElemDie, Property.getType());
1177 addSourceLine(ElemDie, Property);
1178 StringRef GetterName = Property.getObjCPropertyGetterName();
1179 if (!GetterName.empty())
1180 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1181 StringRef SetterName = Property.getObjCPropertySetterName();
1182 if (!SetterName.empty())
1183 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1184 unsigned PropertyAttributes = 0;
1185 if (Property.isReadOnlyObjCProperty())
1186 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1187 if (Property.isReadWriteObjCProperty())
1188 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1189 if (Property.isAssignObjCProperty())
1190 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1191 if (Property.isRetainObjCProperty())
1192 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1193 if (Property.isCopyObjCProperty())
1194 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1195 if (Property.isNonAtomicObjCProperty())
1196 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1197 if (PropertyAttributes)
1198 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
1199 PropertyAttributes);
1201 DIEEntry *Entry = getDIEEntry(Element);
1203 Entry = createDIEEntry(ElemDie);
1204 insertDIEEntry(Element, Entry);
1210 if (CTy.isAppleBlockExtension())
1211 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
1213 DICompositeType ContainingType(resolve(CTy.getContainingType()));
1214 if (DIDescriptor(ContainingType).isCompositeType())
1215 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1216 getOrCreateTypeDIE(DIType(ContainingType)));
1218 if (CTy.isObjcClassComplete())
1219 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1221 // Add template parameters to a class, structure or union types.
1222 // FIXME: The support isn't in the metadata for this yet.
1223 if (Tag == dwarf::DW_TAG_class_type ||
1224 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1225 addTemplateParams(Buffer, CTy.getTemplateParams());
1233 // Add name if not anonymous or intermediate type.
1235 addString(&Buffer, dwarf::DW_AT_name, Name);
1237 if (Tag == dwarf::DW_TAG_enumeration_type ||
1238 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1239 Tag == dwarf::DW_TAG_union_type) {
1240 // Add size if non-zero (derived types might be zero-sized.)
1241 // TODO: Do we care about size for enum forward declarations?
1243 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1244 else if (!CTy.isForwardDecl())
1245 // Add zero size if it is not a forward declaration.
1246 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
1248 // If we're a forward decl, say so.
1249 if (CTy.isForwardDecl())
1250 addFlag(&Buffer, dwarf::DW_AT_declaration);
1252 // Add source line info if available.
1253 if (!CTy.isForwardDecl())
1254 addSourceLine(&Buffer, CTy);
1256 // No harm in adding the runtime language to the declaration.
1257 unsigned RLang = CTy.getRunTimeLang();
1259 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1262 // If this is a type applicable to a type unit it then add it to the
1263 // list of types we'll compute a hash for later.
1264 if (shouldCreateTypeUnit(CTy, DD))
1265 DD->addTypeUnitType(&Buffer);
1268 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
1269 /// DITemplateTypeParameter.
1271 CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1272 DITemplateTypeParameter TP) {
1274 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1275 // Add the type if it exists, it could be void and therefore no type.
1277 addType(ParamDIE, resolve(TP.getType()));
1278 if (!TP.getName().empty())
1279 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1282 /// constructTemplateValueParameterDIE - Construct new DIE for the given
1283 /// DITemplateValueParameter.
1285 CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1286 DITemplateValueParameter VP) {
1287 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
1289 // Add the type if there is one, template template and template parameter
1290 // packs will not have a type.
1291 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
1292 addType(ParamDIE, resolve(VP.getType()));
1293 if (!VP.getName().empty())
1294 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1295 if (Value *Val = VP.getValue()) {
1296 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1297 addConstantValue(ParamDIE, CI,
1298 isUnsignedDIType(DD, resolve(VP.getType())));
1299 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1300 // For declaration non-type template parameters (such as global values and
1302 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1303 addOpAddress(Block, Asm->getSymbol(GV));
1304 // Emit DW_OP_stack_value to use the address as the immediate value of the
1305 // parameter, rather than a pointer to it.
1306 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1307 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
1308 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1309 assert(isa<MDString>(Val));
1310 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1311 cast<MDString>(Val)->getString());
1312 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1313 assert(isa<MDNode>(Val));
1314 DIArray A(cast<MDNode>(Val));
1315 addTemplateParams(*ParamDIE, A);
1320 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1321 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1322 // Construct the context before querying for the existence of the DIE in case
1323 // such construction creates the DIE.
1324 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
1326 // If the context is null, DIE should belong to the CU we call construct
1328 ContextDIE = CUDie.get();
1330 DIE *NDie = getDIE(NS);
1333 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1335 if (!NS.getName().empty()) {
1336 addString(NDie, dwarf::DW_AT_name, NS.getName());
1337 addAccelNamespace(NS.getName(), NDie);
1338 addGlobalName(NS.getName(), NDie, NS.getContext());
1340 addAccelNamespace("(anonymous namespace)", NDie);
1341 addSourceLine(NDie, NS);
1345 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1346 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1347 // Construct the context before querying for the existence of the DIE in case
1348 // such construction creates the DIE (as is the case for member function
1350 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
1352 ContextDIE = CUDie.get();
1354 DIE *SPDie = getDIE(SP);
1358 DISubprogram SPDecl = SP.getFunctionDeclaration();
1359 if (SPDecl.isSubprogram())
1360 // Add subprogram definitions to the CU die directly.
1361 ContextDIE = CUDie.get();
1363 // DW_TAG_inlined_subroutine may refer to this DIE.
1364 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1366 DIE *DeclDie = NULL;
1367 if (SPDecl.isSubprogram())
1368 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1370 // Add function template parameters.
1371 addTemplateParams(*SPDie, SP.getTemplateParams());
1373 // If this DIE is going to refer declaration info using AT_specification
1374 // then there is no need to add other attributes.
1376 // Refer function declaration directly.
1377 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
1382 // Add the linkage name if we have one.
1383 StringRef LinkageName = SP.getLinkageName();
1384 if (!LinkageName.empty())
1385 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1386 GlobalValue::getRealLinkageName(LinkageName));
1388 // Constructors and operators for anonymous aggregates do not have names.
1389 if (!SP.getName().empty())
1390 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1392 addSourceLine(SPDie, SP);
1394 // Add the prototype if we have a prototype and we have a C like
1396 uint16_t Language = DICompileUnit(Node).getLanguage();
1397 if (SP.isPrototyped() &&
1398 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1399 Language == dwarf::DW_LANG_ObjC))
1400 addFlag(SPDie, dwarf::DW_AT_prototyped);
1402 DICompositeType SPTy = SP.getType();
1403 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1404 "the type of a subprogram should be a subroutine");
1406 DIArray Args = SPTy.getTypeArray();
1407 // Add a return type. If this is a type like a C/C++ void type we don't add a
1409 if (Args.getElement(0))
1410 addType(SPDie, DIType(Args.getElement(0)));
1412 unsigned VK = SP.getVirtuality();
1414 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1415 DIEBlock *Block = getDIEBlock();
1416 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1417 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1418 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1419 ContainingTypeMap.insert(std::make_pair(SPDie,
1420 resolve(SP.getContainingType())));
1423 if (!SP.isDefinition()) {
1424 addFlag(SPDie, dwarf::DW_AT_declaration);
1426 // Add arguments. Do not add arguments for subprogram definition. They will
1427 // be handled while processing variables.
1428 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1429 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
1430 DIType ATy = DIType(Args.getElement(i));
1432 if (ATy.isArtificial())
1433 addFlag(Arg, dwarf::DW_AT_artificial);
1437 if (SP.isArtificial())
1438 addFlag(SPDie, dwarf::DW_AT_artificial);
1440 if (!SP.isLocalToUnit())
1441 addFlag(SPDie, dwarf::DW_AT_external);
1443 if (SP.isOptimized())
1444 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1446 if (unsigned isa = Asm->getISAEncoding()) {
1447 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1453 // Return const expression if value is a GEP to access merged global
1455 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1456 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1457 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1458 if (!CE || CE->getNumOperands() != 3 ||
1459 CE->getOpcode() != Instruction::GetElementPtr)
1462 // First operand points to a global struct.
1463 Value *Ptr = CE->getOperand(0);
1464 if (!isa<GlobalValue>(Ptr) ||
1465 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1468 // Second operand is zero.
1469 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1470 if (!CI || !CI->isZero())
1473 // Third operand is offset.
1474 if (!isa<ConstantInt>(CE->getOperand(2)))
1480 /// createGlobalVariableDIE - create global variable DIE.
1481 void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
1482 // Check for pre-existence.
1486 DIGlobalVariable GV(N);
1487 if (!GV.isGlobalVariable())
1490 DIScope GVContext = GV.getContext();
1491 DIType GTy = GV.getType();
1493 // If this is a static data member definition, some attributes belong
1494 // to the declaration DIE.
1495 DIE *VariableDIE = NULL;
1496 bool IsStaticMember = false;
1497 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1498 if (SDMDecl.Verify()) {
1499 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1500 // We need the declaration DIE that is in the static member's class.
1501 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
1502 IsStaticMember = true;
1505 // If this is not a static data member definition, create the variable
1506 // DIE and add the initial set of attributes to it.
1508 // Construct the context before querying for the existence of the DIE in
1509 // case such construction creates the DIE.
1510 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
1512 ContextDIE = CUDie.get();
1515 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, N);
1517 // Add name and type.
1518 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1519 addType(VariableDIE, GTy);
1521 // Add scoping info.
1522 if (!GV.isLocalToUnit())
1523 addFlag(VariableDIE, dwarf::DW_AT_external);
1525 // Add line number info.
1526 addSourceLine(VariableDIE, GV);
1530 bool addToAccelTable = false;
1531 DIE *VariableSpecDIE = NULL;
1532 bool isGlobalVariable = GV.getGlobal() != NULL;
1533 if (isGlobalVariable) {
1534 addToAccelTable = true;
1535 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1536 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
1537 if (GV.getGlobal()->isThreadLocal()) {
1538 // FIXME: Make this work with -gsplit-dwarf.
1539 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1540 assert((PointerSize == 4 || PointerSize == 8) &&
1541 "Add support for other sizes if necessary");
1542 const MCExpr *Expr =
1543 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
1544 // Based on GCC's support for TLS:
1545 if (!DD->useSplitDwarf()) {
1546 // 1) Start with a constNu of the appropriate pointer size
1547 addUInt(Block, dwarf::DW_FORM_data1,
1548 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1549 // 2) containing the (relocated) offset of the TLS variable
1550 // within the module's TLS block.
1551 addExpr(Block, dwarf::DW_FORM_udata, Expr);
1553 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1554 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
1556 // 3) followed by a custom OP to make the debugger do a TLS lookup.
1557 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
1559 addOpAddress(Block, Sym);
1560 // Do not create specification DIE if context is either compile unit
1562 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1563 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
1564 // Create specification DIE.
1565 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie.get());
1566 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
1567 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
1568 // A static member's declaration is already flagged as such.
1569 if (!SDMDecl.Verify())
1570 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1572 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1574 // Add the linkage name.
1575 StringRef LinkageName = GV.getLinkageName();
1576 if (!LinkageName.empty())
1577 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1578 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1580 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1582 dwarf::DW_AT_MIPS_linkage_name,
1583 GlobalValue::getRealLinkageName(LinkageName));
1584 } else if (const ConstantInt *CI =
1585 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
1586 // AT_const_value was added when the static member was created. To avoid
1587 // emitting AT_const_value multiple times, we only add AT_const_value when
1588 // it is not a static member.
1589 if (!IsStaticMember)
1590 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
1591 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1592 addToAccelTable = true;
1593 // GV is a merged global.
1594 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1595 Value *Ptr = CE->getOperand(0);
1596 addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr)));
1597 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1598 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
1599 addUInt(Block, dwarf::DW_FORM_udata,
1600 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1601 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1602 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1605 if (addToAccelTable) {
1606 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1607 addAccelName(GV.getName(), AddrDIE);
1609 // If the linkage name is different than the name, go ahead and output
1610 // that as well into the name table.
1611 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1612 addAccelName(GV.getLinkageName(), AddrDIE);
1615 if (!GV.isLocalToUnit())
1616 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1620 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1621 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1623 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1624 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
1626 // The LowerBound value defines the lower bounds which is typically zero for
1627 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1628 // Count == -1 then the array is unbounded and we do not emit
1629 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1630 // Count == 0, then the array has zero elements in which case we do not emit
1632 int64_t LowerBound = SR.getLo();
1633 int64_t DefaultLowerBound = getDefaultLowerBound();
1634 int64_t Count = SR.getCount();
1636 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1637 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1639 if (Count != -1 && Count != 0)
1640 // FIXME: An unbounded array should reference the expression that defines
1642 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, LowerBound + Count - 1);
1645 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1646 void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType *CTy) {
1647 if (CTy->isVector())
1648 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1650 // Emit the element type.
1651 addType(&Buffer, resolve(CTy->getTypeDerivedFrom()));
1653 // Get an anonymous type for index type.
1654 // FIXME: This type should be passed down from the front end
1655 // as different languages may have different sizes for indexes.
1656 DIE *IdxTy = getIndexTyDie();
1658 // Construct an anonymous type for index type.
1659 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
1660 addString(IdxTy, dwarf::DW_AT_name, "int");
1661 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
1662 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1663 dwarf::DW_ATE_signed);
1664 setIndexTyDie(IdxTy);
1667 // Add subranges to array type.
1668 DIArray Elements = CTy->getTypeArray();
1669 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1670 DIDescriptor Element = Elements.getElement(i);
1671 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1672 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1676 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1677 void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DIEnumerator ETy) {
1678 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1679 StringRef Name = ETy.getName();
1680 addString(Enumerator, dwarf::DW_AT_name, Name);
1681 int64_t Value = ETy.getEnumValue();
1682 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1685 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1687 void CompileUnit::constructContainingTypeDIEs() {
1688 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1689 CE = ContainingTypeMap.end();
1691 DIE *SPDie = CI->first;
1692 const MDNode *N = CI->second;
1695 DIE *NDie = getDIE(N);
1698 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
1702 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1703 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1704 StringRef Name = DV->getName();
1706 // Define variable debug information entry.
1707 DIE *VariableDie = new DIE(DV->getTag());
1708 DbgVariable *AbsVar = DV->getAbstractVariable();
1709 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1711 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
1714 addString(VariableDie, dwarf::DW_AT_name, Name);
1715 addSourceLine(VariableDie, DV->getVariable());
1716 addType(VariableDie, DV->getType());
1719 if (DV->isArtificial())
1720 addFlag(VariableDie, dwarf::DW_AT_artificial);
1722 if (isScopeAbstract) {
1723 DV->setDIE(VariableDie);
1727 // Add variable address.
1729 unsigned Offset = DV->getDotDebugLocOffset();
1730 if (Offset != ~0U) {
1731 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1732 Asm->GetTempSymbol("debug_loc", Offset));
1733 DV->setDIE(VariableDie);
1737 // Check if variable is described by a DBG_VALUE instruction.
1738 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1739 assert(DVInsn->getNumOperands() == 3);
1740 if (DVInsn->getOperand(0).isReg()) {
1741 const MachineOperand RegOp = DVInsn->getOperand(0);
1742 // If the second operand is an immediate, this is an indirect value.
1743 if (DVInsn->getOperand(1).isImm()) {
1744 MachineLocation Location(RegOp.getReg(),
1745 DVInsn->getOperand(1).getImm());
1746 addVariableAddress(*DV, VariableDie, Location);
1747 } else if (RegOp.getReg())
1748 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg()));
1749 } else if (DVInsn->getOperand(0).isImm())
1750 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
1751 else if (DVInsn->getOperand(0).isFPImm())
1752 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1753 else if (DVInsn->getOperand(0).isCImm())
1754 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1755 isUnsignedDIType(DD, DV->getType()));
1757 DV->setDIE(VariableDie);
1760 // .. else use frame index.
1761 int FI = DV->getFrameIndex();
1763 unsigned FrameReg = 0;
1764 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1765 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1766 MachineLocation Location(FrameReg, Offset);
1767 addVariableAddress(*DV, VariableDie, Location);
1771 DV->setDIE(VariableDie);
1775 /// constructMemberDIE - Construct member DIE from DIDerivedType.
1776 void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
1777 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
1778 StringRef Name = DT.getName();
1780 addString(MemberDie, dwarf::DW_AT_name, Name);
1782 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
1784 addSourceLine(MemberDie, DT);
1786 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1787 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1789 uint64_t Size = DT.getSizeInBits();
1790 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1792 if (Size != FieldSize) {
1794 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1795 getBaseTypeSize(DD, DT) >> 3);
1796 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1798 uint64_t Offset = DT.getOffsetInBits();
1799 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1800 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1801 uint64_t FieldOffset = (HiMark - FieldSize);
1802 Offset -= FieldOffset;
1804 // Maybe we need to work from the other end.
1805 if (Asm->getDataLayout().isLittleEndian())
1806 Offset = FieldSize - (Offset + Size);
1807 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1809 // Here WD_AT_data_member_location points to the anonymous
1810 // field that includes this bit field.
1811 addUInt(MemLocationDie, dwarf::DW_FORM_udata, FieldOffset >> 3);
1814 // This is not a bitfield.
1815 addUInt(MemLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1817 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
1819 // For C++, virtual base classes are not at fixed offset. Use following
1820 // expression to extract appropriate offset from vtable.
1821 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1823 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1824 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1825 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1826 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1827 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1828 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1829 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1830 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1832 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1834 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1836 if (DT.isProtected())
1837 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1838 dwarf::DW_ACCESS_protected);
1839 else if (DT.isPrivate())
1840 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1841 dwarf::DW_ACCESS_private);
1842 // Otherwise C++ member and base classes are considered public.
1844 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1845 dwarf::DW_ACCESS_public);
1847 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1848 dwarf::DW_VIRTUALITY_virtual);
1850 // Objective-C properties.
1851 if (MDNode *PNode = DT.getObjCProperty())
1852 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1853 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1856 if (DT.isArtificial())
1857 addFlag(MemberDie, dwarf::DW_AT_artificial);
1860 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
1861 DIE *CompileUnit::getOrCreateStaticMemberDIE(const DIDerivedType DT) {
1865 // Construct the context before querying for the existence of the DIE in case
1866 // such construction creates the DIE.
1867 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
1868 assert(ContextDIE && "Static member should belong to a non-CU context.");
1870 DIE *StaticMemberDIE = getDIE(DT);
1871 if (StaticMemberDIE)
1872 return StaticMemberDIE;
1874 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
1876 DIType Ty = resolve(DT.getTypeDerivedFrom());
1878 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1879 addType(StaticMemberDIE, Ty);
1880 addSourceLine(StaticMemberDIE, DT);
1881 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1882 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1884 // FIXME: We could omit private if the parent is a class_type, and
1885 // public if the parent is something else.
1886 if (DT.isProtected())
1887 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1888 dwarf::DW_ACCESS_protected);
1889 else if (DT.isPrivate())
1890 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1891 dwarf::DW_ACCESS_private);
1893 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1894 dwarf::DW_ACCESS_public);
1896 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1897 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
1898 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1899 addConstantFPValue(StaticMemberDIE, CFP);
1901 return StaticMemberDIE;