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) {
37 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
41 /// ~CompileUnit - Destructor for compile unit.
42 CompileUnit::~CompileUnit() {
43 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
44 DIEBlocks[j]->~DIEBlock();
47 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
48 /// information entry.
49 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
50 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
54 /// getDefaultLowerBound - Return the default lower bound for an array. If the
55 /// DWARF version doesn't handle the language, return -1.
56 int64_t CompileUnit::getDefaultLowerBound() const {
57 switch (DICompileUnit(Node).getLanguage()) {
61 case dwarf::DW_LANG_C89:
62 case dwarf::DW_LANG_C99:
63 case dwarf::DW_LANG_C:
64 case dwarf::DW_LANG_C_plus_plus:
65 case dwarf::DW_LANG_ObjC:
66 case dwarf::DW_LANG_ObjC_plus_plus:
69 case dwarf::DW_LANG_Fortran77:
70 case dwarf::DW_LANG_Fortran90:
71 case dwarf::DW_LANG_Fortran95:
74 // The languages below have valid values only if the DWARF version >= 4.
75 case dwarf::DW_LANG_Java:
76 case dwarf::DW_LANG_Python:
77 case dwarf::DW_LANG_UPC:
78 case dwarf::DW_LANG_D:
79 if (dwarf::DWARF_VERSION >= 4)
83 case dwarf::DW_LANG_Ada83:
84 case dwarf::DW_LANG_Ada95:
85 case dwarf::DW_LANG_Cobol74:
86 case dwarf::DW_LANG_Cobol85:
87 case dwarf::DW_LANG_Modula2:
88 case dwarf::DW_LANG_Pascal83:
89 case dwarf::DW_LANG_PLI:
90 if (dwarf::DWARF_VERSION >= 4)
98 /// addFlag - Add a flag that is true.
99 void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
100 if (DD->getDwarfVersion() >= 4)
101 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
103 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
106 /// addUInt - Add an unsigned integer attribute data and value.
108 void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute,
109 Optional<dwarf::Form> Form, uint64_t Integer) {
111 Form = DIEInteger::BestForm(false, Integer);
112 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
114 Die->addValue(Attribute, *Form, Value);
117 void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
118 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
121 /// addSInt - Add an signed integer attribute data and value.
123 void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute,
124 Optional<dwarf::Form> Form, int64_t Integer) {
126 Form = DIEInteger::BestForm(true, Integer);
127 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
128 Die->addValue(Attribute, *Form, Value);
131 void CompileUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
133 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
136 /// addString - Add a string attribute data and value. We always emit a
137 /// reference to the string pool instead of immediate strings so that DIEs have
138 /// more predictable sizes. In the case of split dwarf we emit an index
139 /// into another table which gets us the static offset into the string
141 void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute, StringRef String) {
144 if (!DD->useSplitDwarf()) {
145 MCSymbol *Symb = DU->getStringPoolEntry(String);
146 if (Asm->needsRelocationsForDwarfStringPool())
147 Value = new (DIEValueAllocator) DIELabel(Symb);
149 MCSymbol *StringPool = DU->getStringPoolSym();
150 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
152 Form = dwarf::DW_FORM_strp;
154 unsigned idx = DU->getStringPoolIndex(String);
155 Value = new (DIEValueAllocator) DIEInteger(idx);
156 Form = dwarf::DW_FORM_GNU_str_index;
158 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
159 Die->addValue(Attribute, Form, Str);
162 /// addLocalString - Add a string attribute data and value. This is guaranteed
163 /// to be in the local string pool instead of indirected.
164 void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
166 MCSymbol *Symb = DU->getStringPoolEntry(String);
168 if (Asm->needsRelocationsForDwarfStringPool())
169 Value = new (DIEValueAllocator) DIELabel(Symb);
171 MCSymbol *StringPool = DU->getStringPoolSym();
172 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
174 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
177 /// addExpr - Add a Dwarf expression attribute data and value.
179 void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
180 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
181 Die->addValue((dwarf::Attribute)0, Form, Value);
184 /// addLabel - Add a Dwarf label attribute data and value.
186 void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
187 const MCSymbol *Label) {
188 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
189 Die->addValue(Attribute, Form, Value);
192 void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
193 const MCSymbol *Label) {
194 addLabel(Die, (dwarf::Attribute)0, Form, Label);
197 /// addLabelAddress - Add a dwarf label attribute data and value using
198 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
200 void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
203 DD->addArangeLabel(SymbolCU(this, Label));
205 if (!DD->useSplitDwarf()) {
207 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
208 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
210 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
211 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
214 unsigned idx = DU->getAddrPoolIndex(Label);
215 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
216 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
220 /// addOpAddress - Add a dwarf op address data and value using the
221 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
223 void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
224 DD->addArangeLabel(SymbolCU(this, Sym));
225 if (!DD->useSplitDwarf()) {
226 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
227 addLabel(Die, dwarf::DW_FORM_udata, Sym);
229 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
230 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
234 /// addDelta - Add a label delta attribute data and value.
236 void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
237 const MCSymbol *Hi, const MCSymbol *Lo) {
238 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
239 Die->addValue(Attribute, Form, Value);
242 /// addDIEEntry - Add a DIE attribute data and value.
244 void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
245 // We currently only use ref4.
246 Die->addValue(Attribute, dwarf::DW_FORM_ref4, createDIEEntry(Entry));
249 /// Create a DIE with the given Tag, add the DIE to its parent, and
250 /// call insertDIE if MD is not null.
251 DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const MDNode *MD) {
252 DIE *Die = new DIE(Tag);
253 Parent.addChild(Die);
259 /// addBlock - Add block data.
261 void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
263 Block->ComputeSize(Asm);
264 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
265 Die->addValue(Attribute, Block->BestForm(), Block);
268 /// addSourceLine - Add location information to specified debug information
270 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
275 unsigned Line = V.getLineNumber();
279 DD->getOrCreateSourceID(V.getContext().getFilename(),
280 V.getContext().getDirectory(), getUniqueID());
281 assert(FileID && "Invalid file id");
282 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
283 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
286 /// addSourceLine - Add location information to specified debug information
288 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
289 // Verify global variable.
290 if (!G.isGlobalVariable())
293 unsigned Line = G.getLineNumber();
297 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
298 assert(FileID && "Invalid file id");
299 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
300 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
303 /// addSourceLine - Add location information to specified debug information
305 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
306 // Verify subprogram.
307 if (!SP.isSubprogram())
310 // If the line number is 0, don't add it.
311 unsigned Line = SP.getLineNumber();
315 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
317 assert(FileID && "Invalid file id");
318 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
319 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
322 /// addSourceLine - Add location information to specified debug information
324 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
329 unsigned Line = Ty.getLineNumber();
332 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
334 assert(FileID && "Invalid file id");
335 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
336 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
339 /// addSourceLine - Add location information to specified debug information
341 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
343 if (!Ty.isObjCProperty())
346 unsigned Line = Ty.getLineNumber();
349 DIFile File = Ty.getFile();
350 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
351 File.getDirectory(), getUniqueID());
352 assert(FileID && "Invalid file id");
353 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
354 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
357 /// addSourceLine - Add location information to specified debug information
359 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
364 unsigned Line = NS.getLineNumber();
367 StringRef FN = NS.getFilename();
370 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
371 assert(FileID && "Invalid file id");
372 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
373 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
376 /// addVariableAddress - Add DW_AT_location attribute for a
377 /// DbgVariable based on provided MachineLocation.
378 void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
379 MachineLocation Location) {
380 if (DV.variableHasComplexAddress())
381 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
382 else if (DV.isBlockByrefVariable())
383 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
385 addAddress(Die, dwarf::DW_AT_location, Location,
386 DV.getVariable().isIndirect());
389 /// addRegisterOp - Add register operand.
390 void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
391 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
392 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
394 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
396 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
397 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
401 /// addRegisterOffset - Add register offset.
402 void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
404 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
405 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
406 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
407 if (Reg == TRI->getFrameRegister(*Asm->MF))
408 // If variable offset is based in frame register then use fbreg.
409 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
411 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
413 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
414 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
416 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
419 /// addAddress - Add an address attribute to a die based on the location
421 void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
422 const MachineLocation &Location, bool Indirect) {
423 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
425 if (Location.isReg() && !Indirect)
426 addRegisterOp(Block, Location.getReg());
428 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
429 if (Indirect && !Location.isReg()) {
430 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
434 // Now attach the location information to the DIE.
435 addBlock(Die, Attribute, Block);
438 /// addComplexAddress - Start with the address based on the location provided,
439 /// and generate the DWARF information necessary to find the actual variable
440 /// given the extra address information encoded in the DIVariable, starting from
441 /// the starting location. Add the DWARF information to the die.
443 void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
444 dwarf::Attribute Attribute,
445 const MachineLocation &Location) {
446 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
447 unsigned N = DV.getNumAddrElements();
449 if (Location.isReg()) {
450 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
451 // If first address element is OpPlus then emit
452 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
453 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
456 addRegisterOp(Block, Location.getReg());
458 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
461 uint64_t Element = DV.getAddrElement(i);
462 if (Element == DIBuilder::OpPlus) {
463 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
464 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
465 } else if (Element == DIBuilder::OpDeref) {
466 if (!Location.isReg())
467 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
469 llvm_unreachable("unknown DIBuilder Opcode");
472 // Now attach the location information to the DIE.
473 addBlock(Die, Attribute, Block);
476 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
477 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
478 gives the variable VarName either the struct, or a pointer to the struct, as
479 its type. This is necessary for various behind-the-scenes things the
480 compiler needs to do with by-reference variables in Blocks.
482 However, as far as the original *programmer* is concerned, the variable
483 should still have type 'SomeType', as originally declared.
485 The function getBlockByrefType dives into the __Block_byref_x_VarName
486 struct to find the original type of the variable, which is then assigned to
487 the variable's Debug Information Entry as its real type. So far, so good.
488 However now the debugger will expect the variable VarName to have the type
489 SomeType. So we need the location attribute for the variable to be an
490 expression that explains to the debugger how to navigate through the
491 pointers and struct to find the actual variable of type SomeType.
493 The following function does just that. We start by getting
494 the "normal" location for the variable. This will be the location
495 of either the struct __Block_byref_x_VarName or the pointer to the
496 struct __Block_byref_x_VarName.
498 The struct will look something like:
500 struct __Block_byref_x_VarName {
502 struct __Block_byref_x_VarName *forwarding;
503 ... <various other fields>
505 ... <maybe more fields>
508 If we are given the struct directly (as our starting point) we
509 need to tell the debugger to:
511 1). Add the offset of the forwarding field.
513 2). Follow that pointer to get the real __Block_byref_x_VarName
514 struct to use (the real one may have been copied onto the heap).
516 3). Add the offset for the field VarName, to find the actual variable.
518 If we started with a pointer to the struct, then we need to
519 dereference that pointer first, before the other steps.
520 Translating this into DWARF ops, we will need to append the following
521 to the current location description for the variable:
523 DW_OP_deref -- optional, if we start with a pointer
524 DW_OP_plus_uconst <forward_fld_offset>
526 DW_OP_plus_uconst <varName_fld_offset>
528 That is what this function does. */
530 /// addBlockByrefAddress - Start with the address based on the location
531 /// provided, and generate the DWARF information necessary to find the
532 /// actual Block variable (navigating the Block struct) based on the
533 /// starting location. Add the DWARF information to the die. For
534 /// more information, read large comment just above here.
536 void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
537 dwarf::Attribute Attribute,
538 const MachineLocation &Location) {
539 DIType Ty = DV.getType();
541 uint16_t Tag = Ty.getTag();
542 bool isPointer = false;
544 StringRef varName = DV.getName();
546 if (Tag == dwarf::DW_TAG_pointer_type) {
547 DIDerivedType DTy = DIDerivedType(Ty);
548 TmpTy = resolve(DTy.getTypeDerivedFrom());
552 DICompositeType blockStruct = DICompositeType(TmpTy);
554 // Find the __forwarding field and the variable field in the __Block_byref
556 DIArray Fields = blockStruct.getTypeArray();
557 DIDescriptor varField = DIDescriptor();
558 DIDescriptor forwardingField = DIDescriptor();
560 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
561 DIDescriptor Element = Fields.getElement(i);
562 DIDerivedType DT = DIDerivedType(Element);
563 StringRef fieldName = DT.getName();
564 if (fieldName == "__forwarding")
565 forwardingField = Element;
566 else if (fieldName == varName)
570 // Get the offsets for the forwarding field and the variable field.
571 unsigned forwardingFieldOffset =
572 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
573 unsigned varFieldOffset = DIDerivedType(varField).getOffsetInBits() >> 3;
575 // Decode the original location, and use that as the start of the byref
576 // variable's location.
577 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
579 if (Location.isReg())
580 addRegisterOp(Block, Location.getReg());
582 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
584 // If we started with a pointer to the __Block_byref... struct, then
585 // the first thing we need to do is dereference the pointer (DW_OP_deref).
587 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
589 // Next add the offset for the '__forwarding' field:
590 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
591 // adding the offset if it's 0.
592 if (forwardingFieldOffset > 0) {
593 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
594 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
597 // Now dereference the __forwarding field to get to the real __Block_byref
598 // struct: DW_OP_deref.
599 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
601 // Now that we've got the real __Block_byref... struct, add the offset
602 // for the variable's field to get to the location of the actual variable:
603 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
604 if (varFieldOffset > 0) {
605 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
606 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
609 // Now attach the location information to the DIE.
610 addBlock(Die, Attribute, Block);
613 /// isTypeSigned - Return true if the type is signed.
614 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
615 if (Ty.isDerivedType())
616 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
618 if (Ty.isBasicType())
619 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
620 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
621 *SizeInBits = Ty.getSizeInBits();
627 /// Return true if type encoding is unsigned.
628 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
629 DIDerivedType DTy(Ty);
630 if (DTy.isDerivedType())
631 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
634 if (BTy.isBasicType()) {
635 unsigned Encoding = BTy.getEncoding();
636 if (Encoding == dwarf::DW_ATE_unsigned ||
637 Encoding == dwarf::DW_ATE_unsigned_char ||
638 Encoding == dwarf::DW_ATE_boolean)
644 /// If this type is derived from a base type then return base type size.
645 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
646 unsigned Tag = Ty.getTag();
648 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
649 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
650 Tag != dwarf::DW_TAG_restrict_type)
651 return Ty.getSizeInBits();
653 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
655 // If this type is not derived from any type then take conservative approach.
656 if (!BaseType.isValid())
657 return Ty.getSizeInBits();
659 // If this is a derived type, go ahead and get the base type, unless it's a
660 // reference then it's just the size of the field. Pointer types have no need
661 // of this since they're a different type of qualification on the type.
662 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
663 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
664 return Ty.getSizeInBits();
666 if (BaseType.isDerivedType())
667 return getBaseTypeSize(DD, DIDerivedType(BaseType));
669 return BaseType.getSizeInBits();
672 /// addConstantValue - Add constant value entry in variable DIE.
673 void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
675 // FIXME: This is a bit conservative/simple - it emits negative values at
676 // their maximum bit width which is a bit unfortunate (& doesn't prefer
677 // udata/sdata over dataN as suggested by the DWARF spec)
678 assert(MO.isImm() && "Invalid machine operand!");
680 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
683 // If we're a signed constant definitely use sdata.
684 if (SignedConstant) {
685 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
689 // Else use data for now unless it's larger than we can deal with.
690 switch (SizeInBits) {
692 Form = dwarf::DW_FORM_data1;
695 Form = dwarf::DW_FORM_data2;
698 Form = dwarf::DW_FORM_data4;
701 Form = dwarf::DW_FORM_data8;
704 Form = dwarf::DW_FORM_udata;
705 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
708 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
711 /// addConstantFPValue - Add constant value entry in variable DIE.
712 void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
713 assert(MO.isFPImm() && "Invalid machine operand!");
714 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
715 APFloat FPImm = MO.getFPImm()->getValueAPF();
717 // Get the raw data form of the floating point.
718 const APInt FltVal = FPImm.bitcastToAPInt();
719 const char *FltPtr = (const char *)FltVal.getRawData();
721 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
722 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
723 int Incr = (LittleEndian ? 1 : -1);
724 int Start = (LittleEndian ? 0 : NumBytes - 1);
725 int Stop = (LittleEndian ? NumBytes : -1);
727 // Output the constant to DWARF one byte at a time.
728 for (; Start != Stop; Start += Incr)
729 addUInt(Block, dwarf::DW_FORM_data1,
730 (unsigned char)0xFF & FltPtr[Start]);
732 addBlock(Die, dwarf::DW_AT_const_value, Block);
735 /// addConstantFPValue - Add constant value entry in variable DIE.
736 void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
737 // Pass this down to addConstantValue as an unsigned bag of bits.
738 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
741 /// addConstantValue - Add constant value entry in variable DIE.
742 void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
744 addConstantValue(Die, CI->getValue(), Unsigned);
747 // addConstantValue - Add constant value entry in variable DIE.
748 void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
749 unsigned CIBitWidth = Val.getBitWidth();
750 if (CIBitWidth <= 64) {
751 // If we're a signed constant definitely use sdata.
753 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
758 // Else use data for now unless it's larger than we can deal with.
760 switch (CIBitWidth) {
762 Form = dwarf::DW_FORM_data1;
765 Form = dwarf::DW_FORM_data2;
768 Form = dwarf::DW_FORM_data4;
771 Form = dwarf::DW_FORM_data8;
774 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
778 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
782 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
784 // Get the raw data form of the large APInt.
785 const uint64_t *Ptr64 = Val.getRawData();
787 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
788 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
790 // Output the constant to DWARF one byte at a time.
791 for (int i = 0; i < NumBytes; i++) {
794 c = Ptr64[i / 8] >> (8 * (i & 7));
796 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
797 addUInt(Block, dwarf::DW_FORM_data1, c);
800 addBlock(Die, dwarf::DW_AT_const_value, Block);
803 /// addTemplateParams - Add template parameters into buffer.
804 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
805 // Add template parameters.
806 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
807 DIDescriptor Element = TParams.getElement(i);
808 if (Element.isTemplateTypeParameter())
809 constructTemplateTypeParameterDIE(Buffer,
810 DITemplateTypeParameter(Element));
811 else if (Element.isTemplateValueParameter())
812 constructTemplateValueParameterDIE(Buffer,
813 DITemplateValueParameter(Element));
817 /// getOrCreateContextDIE - Get context owner's DIE.
818 DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) {
819 if (Context.isType())
820 return getOrCreateTypeDIE(DIType(Context));
821 else if (Context.isNameSpace())
822 return getOrCreateNameSpace(DINameSpace(Context));
823 else if (Context.isSubprogram())
824 return getOrCreateSubprogramDIE(DISubprogram(Context));
826 return getDIE(Context);
829 /// addToContextOwner - Add Die into the list of its context owner's children.
830 void CompileUnit::addToContextOwner(DIE *Die, DIScope Context) {
831 assert(!Die->getParent());
832 if (DIE *ContextDIE = getOrCreateContextDIE(Context)) {
833 if (Die->getParent()) {
834 // While creating the context, if this is a type member, we will have
835 // added the child to the context already.
836 assert(Die->getParent() == ContextDIE);
839 ContextDIE->addChild(Die);
844 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
846 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
850 DIE *TyDIE = getDIE(Ty);
855 TyDIE = new DIE(Ty.getTag());
856 insertDIE(Ty, TyDIE);
857 if (Ty.isBasicType())
858 constructTypeDIE(*TyDIE, DIBasicType(Ty));
859 else if (Ty.isCompositeType())
860 constructTypeDIE(*TyDIE, DICompositeType(Ty));
862 assert(Ty.isDerivedType() && "Unknown kind of DIType");
863 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
865 // If this is a named finished type then include it in the list of types
866 // for the accelerator tables.
867 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
868 bool IsImplementation = 0;
869 if (Ty.isCompositeType()) {
870 DICompositeType CT(Ty);
871 // A runtime language of 0 actually means C/C++ and that any
872 // non-negative value is some version of Objective-C/C++.
873 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
875 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
876 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
879 addToContextOwner(TyDIE, resolve(Ty.getContext()));
883 /// addType - Add a new type attribute to the specified entity.
884 void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
885 assert(Ty && "Trying to add a type that doesn't exist?");
887 // Check for pre-existence.
888 DIEEntry *Entry = getDIEEntry(Ty);
889 // If it exists then use the existing value.
891 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
896 DIE *Buffer = getOrCreateTypeDIE(Ty);
899 Entry = createDIEEntry(Buffer);
900 insertDIEEntry(Ty, Entry);
901 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
903 // If this is a complete composite type then include it in the
904 // list of global types.
908 // Accelerator table mutators - add each name along with its companion
909 // DIE to the proper table while ensuring that the name that we're going
910 // to reference is in the string table. We do this since the names we
911 // add may not only be identical to the names in the DIE.
912 void CompileUnit::addAccelName(StringRef Name, DIE *Die) {
913 DU->getStringPoolEntry(Name);
914 std::vector<DIE *> &DIEs = AccelNames[Name];
918 void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) {
919 DU->getStringPoolEntry(Name);
920 std::vector<DIE *> &DIEs = AccelObjC[Name];
924 void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) {
925 DU->getStringPoolEntry(Name);
926 std::vector<DIE *> &DIEs = AccelNamespace[Name];
930 void CompileUnit::addAccelType(StringRef Name, std::pair<DIE *, unsigned> Die) {
931 DU->getStringPoolEntry(Name);
932 std::vector<std::pair<DIE *, unsigned> > &DIEs = AccelTypes[Name];
936 /// addGlobalName - Add a new global name to the compile unit.
937 void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
938 std::string FullName = getParentContextString(Context) + Name.str();
939 GlobalNames[FullName] = Die;
942 /// addGlobalType - Add a new global type to the compile unit.
944 void CompileUnit::addGlobalType(DIType Ty) {
945 DIScope Context = resolve(Ty.getContext());
946 if (!Ty.getName().empty() && !Ty.isForwardDecl() &&
947 (!Context || Context.isCompileUnit() || Context.isFile() ||
948 Context.isNameSpace()))
949 if (DIEEntry *Entry = getDIEEntry(Ty)) {
950 std::string FullName =
951 getParentContextString(Context) + Ty.getName().str();
952 GlobalTypes[FullName] = Entry->getEntry();
956 /// getParentContextString - Walks the metadata parent chain in a language
957 /// specific manner (using the compile unit language) and returns
958 /// it as a string. This is done at the metadata level because DIEs may
959 /// not currently have been added to the parent context and walking the
960 /// DIEs looking for names is more expensive than walking the metadata.
961 std::string CompileUnit::getParentContextString(DIScope Context) const {
965 // FIXME: Decide whether to implement this for non-C++ languages.
966 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
970 SmallVector<DIScope, 1> Parents;
971 while (!Context.isCompileUnit()) {
972 Parents.push_back(Context);
973 if (Context.getContext())
974 Context = resolve(Context.getContext());
976 // Structure, etc types will have a NULL context if they're at the top
981 // Reverse iterate over our list to go from the outermost construct to the
983 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
987 StringRef Name = Ctx.getName();
996 /// addPubTypes - Add subprogram argument types for pubtypes section.
997 void CompileUnit::addPubTypes(DISubprogram SP) {
998 DICompositeType SPTy = SP.getType();
999 uint16_t SPTag = SPTy.getTag();
1000 if (SPTag != dwarf::DW_TAG_subroutine_type)
1003 DIArray Args = SPTy.getTypeArray();
1004 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
1005 DIType ATy(Args.getElement(i));
1012 /// constructTypeDIE - Construct basic type die from DIBasicType.
1013 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1014 // Get core information.
1015 StringRef Name = BTy.getName();
1016 // Add name if not anonymous or intermediate type.
1018 addString(&Buffer, dwarf::DW_AT_name, Name);
1020 // An unspecified type only has a name attribute.
1021 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
1024 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1027 uint64_t Size = BTy.getSizeInBits() >> 3;
1028 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1031 /// constructTypeDIE - Construct derived type die from DIDerivedType.
1032 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1033 // Get core information.
1034 StringRef Name = DTy.getName();
1035 uint64_t Size = DTy.getSizeInBits() >> 3;
1036 uint16_t Tag = Buffer.getTag();
1038 // Map to main type, void will not have a type.
1039 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
1041 addType(&Buffer, FromTy);
1043 // Add name if not anonymous or intermediate type.
1045 addString(&Buffer, dwarf::DW_AT_name, Name);
1047 // Add size if non-zero (derived types might be zero-sized.)
1048 if (Size && Tag != dwarf::DW_TAG_pointer_type)
1049 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1051 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
1052 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1053 getOrCreateTypeDIE(resolve(DTy.getClassType())));
1054 // Add source line info if available and TyDesc is not a forward declaration.
1055 if (!DTy.isForwardDecl())
1056 addSourceLine(&Buffer, DTy);
1059 /// Return true if the type is appropriately scoped to be contained inside
1060 /// its own type unit.
1061 static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
1062 DIScope Parent = DD->resolve(Ty.getContext());
1064 // Don't generate a hash for anything scoped inside a function.
1065 if (Parent.isSubprogram())
1067 Parent = DD->resolve(Parent.getContext());
1072 /// Return true if the type should be split out into a type unit.
1073 static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
1074 uint16_t Tag = CTy.getTag();
1077 case dwarf::DW_TAG_structure_type:
1078 case dwarf::DW_TAG_union_type:
1079 case dwarf::DW_TAG_enumeration_type:
1080 case dwarf::DW_TAG_class_type:
1081 // If this is a class, structure, union, or enumeration type
1082 // that is a definition (not a declaration), and not scoped
1083 // inside a function then separate this out as a type unit.
1084 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
1090 /// constructTypeDIE - Construct type DIE from DICompositeType.
1091 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1092 // Get core information.
1093 StringRef Name = CTy.getName();
1095 uint64_t Size = CTy.getSizeInBits() >> 3;
1096 uint16_t Tag = Buffer.getTag();
1099 case dwarf::DW_TAG_array_type:
1100 constructArrayTypeDIE(Buffer, &CTy);
1102 case dwarf::DW_TAG_enumeration_type: {
1103 DIArray Elements = CTy.getTypeArray();
1105 // Add enumerators to enumeration type.
1106 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1107 DIDescriptor Enum(Elements.getElement(i));
1108 if (Enum.isEnumerator())
1109 constructEnumTypeDIE(Buffer, DIEnumerator(Enum));
1111 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1113 addType(&Buffer, DTy);
1114 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1117 case dwarf::DW_TAG_subroutine_type: {
1118 // Add return type. A void return won't have a type.
1119 DIArray Elements = CTy.getTypeArray();
1120 DIDescriptor RTy = Elements.getElement(0);
1122 addType(&Buffer, DIType(RTy));
1124 bool isPrototyped = true;
1126 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1127 DIDescriptor Ty = Elements.getElement(i);
1128 if (Ty.isUnspecifiedParameter()) {
1129 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1130 isPrototyped = false;
1132 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1133 addType(Arg, DIType(Ty));
1134 if (DIType(Ty).isArtificial())
1135 addFlag(Arg, dwarf::DW_AT_artificial);
1138 // Add prototype flag if we're dealing with a C language and the
1139 // function has been prototyped.
1140 uint16_t Language = DICompileUnit(Node).getLanguage();
1142 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1143 Language == dwarf::DW_LANG_ObjC))
1144 addFlag(&Buffer, dwarf::DW_AT_prototyped);
1146 case dwarf::DW_TAG_structure_type:
1147 case dwarf::DW_TAG_union_type:
1148 case dwarf::DW_TAG_class_type: {
1149 // Add elements to structure type.
1150 DIArray Elements = CTy.getTypeArray();
1151 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1152 DIDescriptor Element = Elements.getElement(i);
1153 DIE *ElemDie = NULL;
1154 if (Element.isSubprogram()) {
1155 DISubprogram SP(Element);
1156 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
1157 if (SP.isProtected())
1158 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1159 dwarf::DW_ACCESS_protected);
1160 else if (SP.isPrivate())
1161 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1162 dwarf::DW_ACCESS_private);
1164 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1165 dwarf::DW_ACCESS_public);
1166 if (SP.isExplicit())
1167 addFlag(ElemDie, dwarf::DW_AT_explicit);
1168 } else if (Element.isDerivedType()) {
1169 DIDerivedType DDTy(Element);
1170 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1171 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1172 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
1173 dwarf::DW_AT_friend);
1174 } else if (DDTy.isStaticMember()) {
1175 getOrCreateStaticMemberDIE(DDTy);
1177 constructMemberDIE(Buffer, DDTy);
1179 } else if (Element.isObjCProperty()) {
1180 DIObjCProperty Property(Element);
1181 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
1182 StringRef PropertyName = Property.getObjCPropertyName();
1183 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1184 addType(ElemDie, Property.getType());
1185 addSourceLine(ElemDie, Property);
1186 StringRef GetterName = Property.getObjCPropertyGetterName();
1187 if (!GetterName.empty())
1188 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1189 StringRef SetterName = Property.getObjCPropertySetterName();
1190 if (!SetterName.empty())
1191 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1192 unsigned PropertyAttributes = 0;
1193 if (Property.isReadOnlyObjCProperty())
1194 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1195 if (Property.isReadWriteObjCProperty())
1196 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1197 if (Property.isAssignObjCProperty())
1198 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1199 if (Property.isRetainObjCProperty())
1200 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1201 if (Property.isCopyObjCProperty())
1202 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1203 if (Property.isNonAtomicObjCProperty())
1204 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1205 if (PropertyAttributes)
1206 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
1207 PropertyAttributes);
1209 DIEEntry *Entry = getDIEEntry(Element);
1211 Entry = createDIEEntry(ElemDie);
1212 insertDIEEntry(Element, Entry);
1218 if (CTy.isAppleBlockExtension())
1219 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
1221 DICompositeType ContainingType(resolve(CTy.getContainingType()));
1222 if (DIDescriptor(ContainingType).isCompositeType())
1223 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1224 getOrCreateTypeDIE(DIType(ContainingType)));
1226 if (CTy.isObjcClassComplete())
1227 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1229 // Add template parameters to a class, structure or union types.
1230 // FIXME: The support isn't in the metadata for this yet.
1231 if (Tag == dwarf::DW_TAG_class_type ||
1232 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1233 addTemplateParams(Buffer, CTy.getTemplateParams());
1241 // Add name if not anonymous or intermediate type.
1243 addString(&Buffer, dwarf::DW_AT_name, Name);
1245 if (Tag == dwarf::DW_TAG_enumeration_type ||
1246 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1247 Tag == dwarf::DW_TAG_union_type) {
1248 // Add size if non-zero (derived types might be zero-sized.)
1249 // TODO: Do we care about size for enum forward declarations?
1251 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1252 else if (!CTy.isForwardDecl())
1253 // Add zero size if it is not a forward declaration.
1254 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
1256 // If we're a forward decl, say so.
1257 if (CTy.isForwardDecl())
1258 addFlag(&Buffer, dwarf::DW_AT_declaration);
1260 // Add source line info if available.
1261 if (!CTy.isForwardDecl())
1262 addSourceLine(&Buffer, CTy);
1264 // No harm in adding the runtime language to the declaration.
1265 unsigned RLang = CTy.getRunTimeLang();
1267 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1270 // If this is a type applicable to a type unit it then add it to the
1271 // list of types we'll compute a hash for later.
1272 if (shouldCreateTypeUnit(CTy, DD))
1273 DD->addTypeUnitType(&Buffer);
1276 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
1277 /// DITemplateTypeParameter.
1279 CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1280 DITemplateTypeParameter TP) {
1282 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1283 // Add the type if it exists, it could be void and therefore no type.
1285 addType(ParamDIE, resolve(TP.getType()));
1286 if (!TP.getName().empty())
1287 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1290 /// constructTemplateValueParameterDIE - Construct new DIE for the given
1291 /// DITemplateValueParameter.
1293 CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1294 DITemplateValueParameter VP) {
1295 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
1297 // Add the type if there is one, template template and template parameter
1298 // packs will not have a type.
1299 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
1300 addType(ParamDIE, resolve(VP.getType()));
1301 if (!VP.getName().empty())
1302 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1303 if (Value *Val = VP.getValue()) {
1304 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1305 addConstantValue(ParamDIE, CI,
1306 isUnsignedDIType(DD, resolve(VP.getType())));
1307 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1308 // For declaration non-type template parameters (such as global values and
1310 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1311 addOpAddress(Block, Asm->getSymbol(GV));
1312 // Emit DW_OP_stack_value to use the address as the immediate value of the
1313 // parameter, rather than a pointer to it.
1314 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1315 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
1316 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1317 assert(isa<MDString>(Val));
1318 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1319 cast<MDString>(Val)->getString());
1320 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1321 assert(isa<MDNode>(Val));
1322 DIArray A(cast<MDNode>(Val));
1323 addTemplateParams(*ParamDIE, A);
1328 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1329 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1330 // Construct the context before querying for the existence of the DIE in case
1331 // such construction creates the DIE.
1332 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
1334 // If the context is null, DIE should belong to the CU we call construct
1336 ContextDIE = CUDie.get();
1338 DIE *NDie = getDIE(NS);
1341 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1343 if (!NS.getName().empty()) {
1344 addString(NDie, dwarf::DW_AT_name, NS.getName());
1345 addAccelNamespace(NS.getName(), NDie);
1346 addGlobalName(NS.getName(), NDie, NS.getContext());
1348 addAccelNamespace("(anonymous namespace)", NDie);
1349 addSourceLine(NDie, NS);
1353 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1354 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1355 // Construct the context before querying for the existence of the DIE in case
1356 // such construction creates the DIE (as is the case for member function
1358 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
1360 ContextDIE = CUDie.get();
1362 DIE *SPDie = getDIE(SP);
1366 DISubprogram SPDecl = SP.getFunctionDeclaration();
1367 if (SPDecl.isSubprogram())
1368 // Add subprogram definitions to the CU die directly.
1369 ContextDIE = CUDie.get();
1371 // DW_TAG_inlined_subroutine may refer to this DIE.
1372 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1374 DIE *DeclDie = NULL;
1375 if (SPDecl.isSubprogram())
1376 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1378 // Add function template parameters.
1379 addTemplateParams(*SPDie, SP.getTemplateParams());
1381 // If this DIE is going to refer declaration info using AT_specification
1382 // then there is no need to add other attributes.
1384 // Refer function declaration directly.
1385 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
1390 // Add the linkage name if we have one.
1391 StringRef LinkageName = SP.getLinkageName();
1392 if (!LinkageName.empty())
1393 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1394 GlobalValue::getRealLinkageName(LinkageName));
1396 // Constructors and operators for anonymous aggregates do not have names.
1397 if (!SP.getName().empty())
1398 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1400 addSourceLine(SPDie, SP);
1402 // Add the prototype if we have a prototype and we have a C like
1404 uint16_t Language = DICompileUnit(Node).getLanguage();
1405 if (SP.isPrototyped() &&
1406 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1407 Language == dwarf::DW_LANG_ObjC))
1408 addFlag(SPDie, dwarf::DW_AT_prototyped);
1410 DICompositeType SPTy = SP.getType();
1411 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1412 "the type of a subprogram should be a subroutine");
1414 DIArray Args = SPTy.getTypeArray();
1415 // Add a return type. If this is a type like a C/C++ void type we don't add a
1417 if (Args.getElement(0))
1418 addType(SPDie, DIType(Args.getElement(0)));
1420 unsigned VK = SP.getVirtuality();
1422 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1423 DIEBlock *Block = getDIEBlock();
1424 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1425 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1426 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1427 ContainingTypeMap.insert(std::make_pair(SPDie,
1428 resolve(SP.getContainingType())));
1431 if (!SP.isDefinition()) {
1432 addFlag(SPDie, dwarf::DW_AT_declaration);
1434 // Add arguments. Do not add arguments for subprogram definition. They will
1435 // be handled while processing variables.
1436 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1437 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
1438 DIType ATy = DIType(Args.getElement(i));
1440 if (ATy.isArtificial())
1441 addFlag(Arg, dwarf::DW_AT_artificial);
1445 if (SP.isArtificial())
1446 addFlag(SPDie, dwarf::DW_AT_artificial);
1448 if (!SP.isLocalToUnit())
1449 addFlag(SPDie, dwarf::DW_AT_external);
1451 if (SP.isOptimized())
1452 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1454 if (unsigned isa = Asm->getISAEncoding()) {
1455 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1461 // Return const expression if value is a GEP to access merged global
1463 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1464 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1465 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1466 if (!CE || CE->getNumOperands() != 3 ||
1467 CE->getOpcode() != Instruction::GetElementPtr)
1470 // First operand points to a global struct.
1471 Value *Ptr = CE->getOperand(0);
1472 if (!isa<GlobalValue>(Ptr) ||
1473 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1476 // Second operand is zero.
1477 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1478 if (!CI || !CI->isZero())
1481 // Third operand is offset.
1482 if (!isa<ConstantInt>(CE->getOperand(2)))
1488 /// createGlobalVariableDIE - create global variable DIE.
1489 void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
1490 // Check for pre-existence.
1494 DIGlobalVariable GV(N);
1495 if (!GV.isGlobalVariable())
1498 DIScope GVContext = GV.getContext();
1499 DIType GTy = GV.getType();
1501 // If this is a static data member definition, some attributes belong
1502 // to the declaration DIE.
1503 DIE *VariableDIE = NULL;
1504 bool IsStaticMember = false;
1505 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1506 if (SDMDecl.Verify()) {
1507 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1508 // We need the declaration DIE that is in the static member's class.
1509 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
1510 IsStaticMember = true;
1513 // If this is not a static data member definition, create the variable
1514 // DIE and add the initial set of attributes to it.
1516 // Construct the context before querying for the existence of the DIE in
1517 // case such construction creates the DIE.
1518 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
1520 ContextDIE = CUDie.get();
1523 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, N);
1525 // Add name and type.
1526 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1527 addType(VariableDIE, GTy);
1529 // Add scoping info.
1530 if (!GV.isLocalToUnit())
1531 addFlag(VariableDIE, dwarf::DW_AT_external);
1533 // Add line number info.
1534 addSourceLine(VariableDIE, GV);
1538 bool addToAccelTable = false;
1539 DIE *VariableSpecDIE = NULL;
1540 bool isGlobalVariable = GV.getGlobal() != NULL;
1541 if (isGlobalVariable) {
1542 addToAccelTable = true;
1543 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1544 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
1545 if (GV.getGlobal()->isThreadLocal()) {
1546 // FIXME: Make this work with -gsplit-dwarf.
1547 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1548 assert((PointerSize == 4 || PointerSize == 8) &&
1549 "Add support for other sizes if necessary");
1550 const MCExpr *Expr =
1551 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
1552 // Based on GCC's support for TLS:
1553 if (!DD->useSplitDwarf()) {
1554 // 1) Start with a constNu of the appropriate pointer size
1555 addUInt(Block, dwarf::DW_FORM_data1,
1556 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1557 // 2) containing the (relocated) offset of the TLS variable
1558 // within the module's TLS block.
1559 addExpr(Block, dwarf::DW_FORM_udata, Expr);
1561 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1562 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
1564 // 3) followed by a custom OP to make the debugger do a TLS lookup.
1565 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
1567 addOpAddress(Block, Sym);
1568 // Do not create specification DIE if context is either compile unit
1570 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1571 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
1572 // Create specification DIE.
1573 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie.get());
1574 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
1575 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
1576 // A static member's declaration is already flagged as such.
1577 if (!SDMDecl.Verify())
1578 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1580 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1582 // Add the linkage name.
1583 StringRef LinkageName = GV.getLinkageName();
1584 if (!LinkageName.empty())
1585 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1586 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1588 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1590 dwarf::DW_AT_MIPS_linkage_name,
1591 GlobalValue::getRealLinkageName(LinkageName));
1592 } else if (const ConstantInt *CI =
1593 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
1594 // AT_const_value was added when the static member was created. To avoid
1595 // emitting AT_const_value multiple times, we only add AT_const_value when
1596 // it is not a static member.
1597 if (!IsStaticMember)
1598 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
1599 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1600 addToAccelTable = true;
1601 // GV is a merged global.
1602 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1603 Value *Ptr = CE->getOperand(0);
1604 addOpAddress(Block, Asm->getSymbol(cast<GlobalValue>(Ptr)));
1605 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1606 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
1607 addUInt(Block, dwarf::DW_FORM_udata,
1608 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1609 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1610 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1613 if (addToAccelTable) {
1614 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1615 addAccelName(GV.getName(), AddrDIE);
1617 // If the linkage name is different than the name, go ahead and output
1618 // that as well into the name table.
1619 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1620 addAccelName(GV.getLinkageName(), AddrDIE);
1623 if (!GV.isLocalToUnit())
1624 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1628 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1629 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1631 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1632 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
1634 // The LowerBound value defines the lower bounds which is typically zero for
1635 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1636 // Count == -1 then the array is unbounded and we do not emit
1637 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1638 // Count == 0, then the array has zero elements in which case we do not emit
1640 int64_t LowerBound = SR.getLo();
1641 int64_t DefaultLowerBound = getDefaultLowerBound();
1642 int64_t Count = SR.getCount();
1644 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1645 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1647 if (Count != -1 && Count != 0)
1648 // FIXME: An unbounded array should reference the expression that defines
1650 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, LowerBound + Count - 1);
1653 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1654 void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType *CTy) {
1655 if (CTy->isVector())
1656 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1658 // Emit the element type.
1659 addType(&Buffer, resolve(CTy->getTypeDerivedFrom()));
1661 // Get an anonymous type for index type.
1662 // FIXME: This type should be passed down from the front end
1663 // as different languages may have different sizes for indexes.
1664 DIE *IdxTy = getIndexTyDie();
1666 // Construct an anonymous type for index type.
1667 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
1668 addString(IdxTy, dwarf::DW_AT_name, "int");
1669 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
1670 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1671 dwarf::DW_ATE_signed);
1672 setIndexTyDie(IdxTy);
1675 // Add subranges to array type.
1676 DIArray Elements = CTy->getTypeArray();
1677 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1678 DIDescriptor Element = Elements.getElement(i);
1679 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1680 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1684 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1685 void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DIEnumerator ETy) {
1686 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1687 StringRef Name = ETy.getName();
1688 addString(Enumerator, dwarf::DW_AT_name, Name);
1689 int64_t Value = ETy.getEnumValue();
1690 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1693 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1695 void CompileUnit::constructContainingTypeDIEs() {
1696 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1697 CE = ContainingTypeMap.end();
1699 DIE *SPDie = CI->first;
1700 const MDNode *N = CI->second;
1703 DIE *NDie = getDIE(N);
1706 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
1710 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1711 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1712 StringRef Name = DV->getName();
1714 // Define variable debug information entry.
1715 DIE *VariableDie = new DIE(DV->getTag());
1716 DbgVariable *AbsVar = DV->getAbstractVariable();
1717 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1719 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
1722 addString(VariableDie, dwarf::DW_AT_name, Name);
1723 addSourceLine(VariableDie, DV->getVariable());
1724 addType(VariableDie, DV->getType());
1727 if (DV->isArtificial())
1728 addFlag(VariableDie, dwarf::DW_AT_artificial);
1730 if (isScopeAbstract) {
1731 DV->setDIE(VariableDie);
1735 // Add variable address.
1737 unsigned Offset = DV->getDotDebugLocOffset();
1738 if (Offset != ~0U) {
1739 addLabel(VariableDie, dwarf::DW_AT_location, dwarf::DW_FORM_data4,
1740 Asm->GetTempSymbol("debug_loc", Offset));
1741 DV->setDIE(VariableDie);
1745 // Check if variable is described by a DBG_VALUE instruction.
1746 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1747 assert(DVInsn->getNumOperands() == 3);
1748 if (DVInsn->getOperand(0).isReg()) {
1749 const MachineOperand RegOp = DVInsn->getOperand(0);
1750 // If the second operand is an immediate, this is an indirect value.
1751 if (DVInsn->getOperand(1).isImm()) {
1752 MachineLocation Location(RegOp.getReg(),
1753 DVInsn->getOperand(1).getImm());
1754 addVariableAddress(*DV, VariableDie, Location);
1755 } else if (RegOp.getReg())
1756 addVariableAddress(*DV, VariableDie, MachineLocation(RegOp.getReg()));
1757 } else if (DVInsn->getOperand(0).isImm())
1758 addConstantValue(VariableDie, DVInsn->getOperand(0), DV->getType());
1759 else if (DVInsn->getOperand(0).isFPImm())
1760 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1761 else if (DVInsn->getOperand(0).isCImm())
1762 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1763 isUnsignedDIType(DD, DV->getType()));
1765 DV->setDIE(VariableDie);
1768 // .. else use frame index.
1769 int FI = DV->getFrameIndex();
1771 unsigned FrameReg = 0;
1772 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1773 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1774 MachineLocation Location(FrameReg, Offset);
1775 addVariableAddress(*DV, VariableDie, Location);
1779 DV->setDIE(VariableDie);
1783 /// constructMemberDIE - Construct member DIE from DIDerivedType.
1784 void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
1785 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
1786 StringRef Name = DT.getName();
1788 addString(MemberDie, dwarf::DW_AT_name, Name);
1790 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
1792 addSourceLine(MemberDie, DT);
1794 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1795 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1797 uint64_t Size = DT.getSizeInBits();
1798 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1800 if (Size != FieldSize) {
1802 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1803 getBaseTypeSize(DD, DT) >> 3);
1804 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1806 uint64_t Offset = DT.getOffsetInBits();
1807 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1808 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1809 uint64_t FieldOffset = (HiMark - FieldSize);
1810 Offset -= FieldOffset;
1812 // Maybe we need to work from the other end.
1813 if (Asm->getDataLayout().isLittleEndian())
1814 Offset = FieldSize - (Offset + Size);
1815 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1817 // Here WD_AT_data_member_location points to the anonymous
1818 // field that includes this bit field.
1819 addUInt(MemLocationDie, dwarf::DW_FORM_udata, FieldOffset >> 3);
1822 // This is not a bitfield.
1823 addUInt(MemLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1825 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
1827 // For C++, virtual base classes are not at fixed offset. Use following
1828 // expression to extract appropriate offset from vtable.
1829 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1831 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1832 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1833 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1834 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1835 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1836 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1837 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1838 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1840 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1842 addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1844 if (DT.isProtected())
1845 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1846 dwarf::DW_ACCESS_protected);
1847 else if (DT.isPrivate())
1848 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1849 dwarf::DW_ACCESS_private);
1850 // Otherwise C++ member and base classes are considered public.
1852 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1853 dwarf::DW_ACCESS_public);
1855 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1856 dwarf::DW_VIRTUALITY_virtual);
1858 // Objective-C properties.
1859 if (MDNode *PNode = DT.getObjCProperty())
1860 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1861 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1864 if (DT.isArtificial())
1865 addFlag(MemberDie, dwarf::DW_AT_artificial);
1868 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
1869 DIE *CompileUnit::getOrCreateStaticMemberDIE(const DIDerivedType DT) {
1873 // Construct the context before querying for the existence of the DIE in case
1874 // such construction creates the DIE.
1875 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
1876 assert(ContextDIE && "Static member should belong to a non-CU context.");
1878 DIE *StaticMemberDIE = getDIE(DT);
1879 if (StaticMemberDIE)
1880 return StaticMemberDIE;
1882 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
1884 DIType Ty = resolve(DT.getTypeDerivedFrom());
1886 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1887 addType(StaticMemberDIE, Ty);
1888 addSourceLine(StaticMemberDIE, DT);
1889 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1890 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1892 // FIXME: We could omit private if the parent is a class_type, and
1893 // public if the parent is something else.
1894 if (DT.isProtected())
1895 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1896 dwarf::DW_ACCESS_protected);
1897 else if (DT.isPrivate())
1898 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1899 dwarf::DW_ACCESS_private);
1901 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1902 dwarf::DW_ACCESS_public);
1904 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1905 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
1906 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1907 addConstantFPValue(StaticMemberDIE, CFP);
1909 return StaticMemberDIE;