1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
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/MC/MCSection.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Target/TargetFrameLowering.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
32 #include "llvm/Support/CommandLine.h"
36 static cl::opt<bool> GenerateTypeUnits("generate-type-units", cl::Hidden,
37 cl::desc("Generate DWARF4 type units."),
40 /// Unit - Compile unit constructor.
41 Unit::Unit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
42 DwarfDebug *DW, DwarfUnits *DWU)
43 : UniqueID(UID), Node(Node), CUDie(D), DebugInfoOffset(0), Asm(A), DD(DW),
44 DU(DWU), IndexTyDie(0) {
45 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
48 CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
49 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
50 : Unit(UID, D, Node, A, DW, DWU) {
54 TypeUnit::TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
55 DwarfDebug *DW, DwarfUnits *DWU)
56 : Unit(UID, D, DICompileUnit(), A, DW, DWU), Language(Language) {}
58 /// ~Unit - Destructor for compile unit.
60 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
61 DIEBlocks[j]->~DIEBlock();
64 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
65 /// information entry.
66 DIEEntry *Unit::createDIEEntry(DIE *Entry) {
67 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
71 /// getDefaultLowerBound - Return the default lower bound for an array. If the
72 /// DWARF version doesn't handle the language, return -1.
73 int64_t Unit::getDefaultLowerBound() const {
74 switch (getLanguage()) {
78 case dwarf::DW_LANG_C89:
79 case dwarf::DW_LANG_C99:
80 case dwarf::DW_LANG_C:
81 case dwarf::DW_LANG_C_plus_plus:
82 case dwarf::DW_LANG_ObjC:
83 case dwarf::DW_LANG_ObjC_plus_plus:
86 case dwarf::DW_LANG_Fortran77:
87 case dwarf::DW_LANG_Fortran90:
88 case dwarf::DW_LANG_Fortran95:
91 // The languages below have valid values only if the DWARF version >= 4.
92 case dwarf::DW_LANG_Java:
93 case dwarf::DW_LANG_Python:
94 case dwarf::DW_LANG_UPC:
95 case dwarf::DW_LANG_D:
96 if (dwarf::DWARF_VERSION >= 4)
100 case dwarf::DW_LANG_Ada83:
101 case dwarf::DW_LANG_Ada95:
102 case dwarf::DW_LANG_Cobol74:
103 case dwarf::DW_LANG_Cobol85:
104 case dwarf::DW_LANG_Modula2:
105 case dwarf::DW_LANG_Pascal83:
106 case dwarf::DW_LANG_PLI:
107 if (dwarf::DWARF_VERSION >= 4)
115 /// Check whether the DIE for this MDNode can be shared across CUs.
116 static bool isShareableAcrossCUs(DIDescriptor D) {
117 // When the MDNode can be part of the type system, the DIE can be shared
119 // Combining type units and cross-CU DIE sharing is lower value (since
120 // cross-CU DIE sharing is used in LTO and removes type redundancy at that
121 // level already) but may be implementable for some value in projects
122 // building multiple independent libraries with LTO and then linking those
124 return (D.isType() ||
125 (D.isSubprogram() && !DISubprogram(D).isDefinition())) &&
129 /// getDIE - Returns the debug information entry map slot for the
130 /// specified debug variable. We delegate the request to DwarfDebug
131 /// when the DIE for this MDNode can be shared across CUs. The mappings
132 /// will be kept in DwarfDebug for shareable DIEs.
133 DIE *Unit::getDIE(DIDescriptor D) const {
134 if (isShareableAcrossCUs(D))
135 return DD->getDIE(D);
136 return MDNodeToDieMap.lookup(D);
139 /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug
140 /// when the DIE for this MDNode can be shared across CUs. The mappings
141 /// will be kept in DwarfDebug for shareable DIEs.
142 void Unit::insertDIE(DIDescriptor Desc, DIE *D) {
143 if (isShareableAcrossCUs(Desc)) {
144 DD->insertDIE(Desc, D);
147 MDNodeToDieMap.insert(std::make_pair(Desc, D));
150 /// addFlag - Add a flag that is true.
151 void Unit::addFlag(DIE *Die, dwarf::Attribute Attribute) {
152 if (DD->getDwarfVersion() >= 4)
153 Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne);
155 Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne);
158 /// addUInt - Add an unsigned integer attribute data and value.
160 void Unit::addUInt(DIE *Die, dwarf::Attribute Attribute,
161 Optional<dwarf::Form> Form, uint64_t Integer) {
163 Form = DIEInteger::BestForm(false, Integer);
164 DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator)
166 Die->addValue(Attribute, *Form, Value);
169 void Unit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) {
170 addUInt(Block, (dwarf::Attribute)0, Form, Integer);
173 /// addSInt - Add an signed integer attribute data and value.
175 void Unit::addSInt(DIE *Die, dwarf::Attribute Attribute,
176 Optional<dwarf::Form> Form, int64_t Integer) {
178 Form = DIEInteger::BestForm(true, Integer);
179 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
180 Die->addValue(Attribute, *Form, Value);
183 void Unit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form, int64_t Integer) {
184 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
187 /// addString - Add a string attribute data and value. We always emit a
188 /// reference to the string pool instead of immediate strings so that DIEs have
189 /// more predictable sizes. In the case of split dwarf we emit an index
190 /// into another table which gets us the static offset into the string
192 void Unit::addString(DIE *Die, dwarf::Attribute Attribute, StringRef String) {
195 if (!DD->useSplitDwarf()) {
196 MCSymbol *Symb = DU->getStringPoolEntry(String);
197 if (Asm->needsRelocationsForDwarfStringPool())
198 Value = new (DIEValueAllocator) DIELabel(Symb);
200 MCSymbol *StringPool = DU->getStringPoolSym();
201 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
203 Form = dwarf::DW_FORM_strp;
205 unsigned idx = DU->getStringPoolIndex(String);
206 Value = new (DIEValueAllocator) DIEInteger(idx);
207 Form = dwarf::DW_FORM_GNU_str_index;
209 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
210 Die->addValue(Attribute, Form, Str);
213 /// addLocalString - Add a string attribute data and value. This is guaranteed
214 /// to be in the local string pool instead of indirected.
215 void Unit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
217 MCSymbol *Symb = DU->getStringPoolEntry(String);
219 if (Asm->needsRelocationsForDwarfStringPool())
220 Value = new (DIEValueAllocator) DIELabel(Symb);
222 MCSymbol *StringPool = DU->getStringPoolSym();
223 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
225 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
228 /// addExpr - Add a Dwarf expression attribute data and value.
230 void Unit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
231 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
232 Die->addValue((dwarf::Attribute)0, Form, Value);
235 /// addLabel - Add a Dwarf label attribute data and value.
237 void Unit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
238 const MCSymbol *Label) {
239 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
240 Die->addValue(Attribute, Form, Value);
243 void Unit::addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label) {
244 addLabel(Die, (dwarf::Attribute)0, Form, Label);
247 /// addSectionLabel - Add a Dwarf section label attribute data and value.
249 void Unit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
250 const MCSymbol *Label) {
251 if (DD->getDwarfVersion() >= 4)
252 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
254 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
257 /// addSectionOffset - Add an offset into a section attribute data and value.
259 void Unit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
261 if (DD->getDwarfVersion() >= 4)
262 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
264 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
267 /// addLabelAddress - Add a dwarf label attribute data and value using
268 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
270 void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
273 DD->addArangeLabel(SymbolCU(this, Label));
275 if (!DD->useSplitDwarf()) {
277 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
278 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
280 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
281 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
284 unsigned idx = DU->getAddrPoolIndex(Label);
285 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
286 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
290 /// addOpAddress - Add a dwarf op address data and value using the
291 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
293 void Unit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
294 if (!DD->useSplitDwarf()) {
295 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
296 addLabel(Die, dwarf::DW_FORM_udata, Sym);
298 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
299 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
303 /// addSectionDelta - Add a section label delta attribute data and value.
305 void Unit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
306 const MCSymbol *Hi, const MCSymbol *Lo) {
307 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
308 if (DD->getDwarfVersion() >= 4)
309 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
311 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
314 /// addDIEEntry - Add a DIE attribute data and value.
316 void Unit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
317 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
320 void Unit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry) {
321 const DIE *DieCU = Die->getUnitOrNull();
322 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
324 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
327 EntryCU = getCUDie();
328 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
329 : dwarf::DW_FORM_ref_addr,
333 /// Create a DIE with the given Tag, add the DIE to its parent, and
334 /// call insertDIE if MD is not null.
335 DIE *Unit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
336 DIE *Die = new DIE(Tag);
337 Parent.addChild(Die);
343 /// addBlock - Add block data.
345 void Unit::addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block) {
346 Block->ComputeSize(Asm);
347 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
348 Die->addValue(Attribute, Block->BestForm(), Block);
351 /// addSourceLine - Add location information to specified debug information
353 void Unit::addSourceLine(DIE *Die, DIVariable V) {
358 unsigned Line = V.getLineNumber();
362 DD->getOrCreateSourceID(V.getContext().getFilename(),
363 V.getContext().getDirectory(), getUniqueID());
364 assert(FileID && "Invalid file id");
365 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
366 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
369 /// addSourceLine - Add location information to specified debug information
371 void Unit::addSourceLine(DIE *Die, DIGlobalVariable G) {
372 // Verify global variable.
373 if (!G.isGlobalVariable())
376 unsigned Line = G.getLineNumber();
380 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
381 assert(FileID && "Invalid file id");
382 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
383 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
386 /// addSourceLine - Add location information to specified debug information
388 void Unit::addSourceLine(DIE *Die, DISubprogram SP) {
389 // Verify subprogram.
390 if (!SP.isSubprogram())
393 // If the line number is 0, don't add it.
394 unsigned Line = SP.getLineNumber();
398 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
400 assert(FileID && "Invalid file id");
401 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
402 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
405 /// addSourceLine - Add location information to specified debug information
407 void Unit::addSourceLine(DIE *Die, DIType Ty) {
412 unsigned Line = Ty.getLineNumber();
415 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
417 assert(FileID && "Invalid file id");
418 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
419 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
422 /// addSourceLine - Add location information to specified debug information
424 void Unit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
426 if (!Ty.isObjCProperty())
429 unsigned Line = Ty.getLineNumber();
432 DIFile File = Ty.getFile();
433 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
434 File.getDirectory(), getUniqueID());
435 assert(FileID && "Invalid file id");
436 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
437 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
440 /// addSourceLine - Add location information to specified debug information
442 void Unit::addSourceLine(DIE *Die, DINameSpace NS) {
447 unsigned Line = NS.getLineNumber();
450 StringRef FN = NS.getFilename();
453 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
454 assert(FileID && "Invalid file id");
455 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
456 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
459 /// addVariableAddress - Add DW_AT_location attribute for a
460 /// DbgVariable based on provided MachineLocation.
461 void Unit::addVariableAddress(const DbgVariable &DV, DIE *Die,
462 MachineLocation Location) {
463 if (DV.variableHasComplexAddress())
464 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
465 else if (DV.isBlockByrefVariable())
466 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
468 addAddress(Die, dwarf::DW_AT_location, Location,
469 DV.getVariable().isIndirect());
472 /// addRegisterOp - Add register operand.
473 void Unit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
474 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
475 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
477 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
479 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
480 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
484 /// addRegisterOffset - Add register offset.
485 void Unit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset) {
486 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
487 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
488 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
489 if (Reg == TRI->getFrameRegister(*Asm->MF))
490 // If variable offset is based in frame register then use fbreg.
491 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
493 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
495 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
496 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
498 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
501 /// addAddress - Add an address attribute to a die based on the location
503 void Unit::addAddress(DIE *Die, dwarf::Attribute Attribute,
504 const MachineLocation &Location, bool Indirect) {
505 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
507 if (Location.isReg() && !Indirect)
508 addRegisterOp(Block, Location.getReg());
510 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
511 if (Indirect && !Location.isReg()) {
512 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
516 // Now attach the location information to the DIE.
517 addBlock(Die, Attribute, Block);
520 /// addComplexAddress - Start with the address based on the location provided,
521 /// and generate the DWARF information necessary to find the actual variable
522 /// given the extra address information encoded in the DIVariable, starting from
523 /// the starting location. Add the DWARF information to the die.
525 void Unit::addComplexAddress(const DbgVariable &DV, DIE *Die,
526 dwarf::Attribute Attribute,
527 const MachineLocation &Location) {
528 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
529 unsigned N = DV.getNumAddrElements();
531 if (Location.isReg()) {
532 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
533 // If first address element is OpPlus then emit
534 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
535 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
538 addRegisterOp(Block, Location.getReg());
540 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
543 uint64_t Element = DV.getAddrElement(i);
544 if (Element == DIBuilder::OpPlus) {
545 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
546 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
547 } else if (Element == DIBuilder::OpDeref) {
548 if (!Location.isReg())
549 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
551 llvm_unreachable("unknown DIBuilder Opcode");
554 // Now attach the location information to the DIE.
555 addBlock(Die, Attribute, Block);
558 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
559 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
560 gives the variable VarName either the struct, or a pointer to the struct, as
561 its type. This is necessary for various behind-the-scenes things the
562 compiler needs to do with by-reference variables in Blocks.
564 However, as far as the original *programmer* is concerned, the variable
565 should still have type 'SomeType', as originally declared.
567 The function getBlockByrefType dives into the __Block_byref_x_VarName
568 struct to find the original type of the variable, which is then assigned to
569 the variable's Debug Information Entry as its real type. So far, so good.
570 However now the debugger will expect the variable VarName to have the type
571 SomeType. So we need the location attribute for the variable to be an
572 expression that explains to the debugger how to navigate through the
573 pointers and struct to find the actual variable of type SomeType.
575 The following function does just that. We start by getting
576 the "normal" location for the variable. This will be the location
577 of either the struct __Block_byref_x_VarName or the pointer to the
578 struct __Block_byref_x_VarName.
580 The struct will look something like:
582 struct __Block_byref_x_VarName {
584 struct __Block_byref_x_VarName *forwarding;
585 ... <various other fields>
587 ... <maybe more fields>
590 If we are given the struct directly (as our starting point) we
591 need to tell the debugger to:
593 1). Add the offset of the forwarding field.
595 2). Follow that pointer to get the real __Block_byref_x_VarName
596 struct to use (the real one may have been copied onto the heap).
598 3). Add the offset for the field VarName, to find the actual variable.
600 If we started with a pointer to the struct, then we need to
601 dereference that pointer first, before the other steps.
602 Translating this into DWARF ops, we will need to append the following
603 to the current location description for the variable:
605 DW_OP_deref -- optional, if we start with a pointer
606 DW_OP_plus_uconst <forward_fld_offset>
608 DW_OP_plus_uconst <varName_fld_offset>
610 That is what this function does. */
612 /// addBlockByrefAddress - Start with the address based on the location
613 /// provided, and generate the DWARF information necessary to find the
614 /// actual Block variable (navigating the Block struct) based on the
615 /// starting location. Add the DWARF information to the die. For
616 /// more information, read large comment just above here.
618 void Unit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
619 dwarf::Attribute Attribute,
620 const MachineLocation &Location) {
621 DIType Ty = DV.getType();
623 uint16_t Tag = Ty.getTag();
624 bool isPointer = false;
626 StringRef varName = DV.getName();
628 if (Tag == dwarf::DW_TAG_pointer_type) {
629 DIDerivedType DTy(Ty);
630 TmpTy = resolve(DTy.getTypeDerivedFrom());
634 DICompositeType blockStruct(TmpTy);
636 // Find the __forwarding field and the variable field in the __Block_byref
638 DIArray Fields = blockStruct.getTypeArray();
639 DIDerivedType varField;
640 DIDerivedType forwardingField;
642 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
643 DIDerivedType DT(Fields.getElement(i));
644 StringRef fieldName = DT.getName();
645 if (fieldName == "__forwarding")
646 forwardingField = DT;
647 else if (fieldName == varName)
651 // Get the offsets for the forwarding field and the variable field.
652 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
653 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
655 // Decode the original location, and use that as the start of the byref
656 // variable's location.
657 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
659 if (Location.isReg())
660 addRegisterOp(Block, Location.getReg());
662 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
664 // If we started with a pointer to the __Block_byref... struct, then
665 // the first thing we need to do is dereference the pointer (DW_OP_deref).
667 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
669 // Next add the offset for the '__forwarding' field:
670 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
671 // adding the offset if it's 0.
672 if (forwardingFieldOffset > 0) {
673 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
674 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
677 // Now dereference the __forwarding field to get to the real __Block_byref
678 // struct: DW_OP_deref.
679 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
681 // Now that we've got the real __Block_byref... struct, add the offset
682 // for the variable's field to get to the location of the actual variable:
683 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
684 if (varFieldOffset > 0) {
685 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
686 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
689 // Now attach the location information to the DIE.
690 addBlock(Die, Attribute, Block);
693 /// isTypeSigned - Return true if the type is signed.
694 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
695 if (Ty.isDerivedType())
696 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
698 if (Ty.isBasicType())
699 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
700 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
701 *SizeInBits = Ty.getSizeInBits();
707 /// Return true if type encoding is unsigned.
708 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
709 DIDerivedType DTy(Ty);
710 if (DTy.isDerivedType())
711 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
714 if (BTy.isBasicType()) {
715 unsigned Encoding = BTy.getEncoding();
716 if (Encoding == dwarf::DW_ATE_unsigned ||
717 Encoding == dwarf::DW_ATE_unsigned_char ||
718 Encoding == dwarf::DW_ATE_boolean)
724 /// If this type is derived from a base type then return base type size.
725 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
726 unsigned Tag = Ty.getTag();
728 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
729 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
730 Tag != dwarf::DW_TAG_restrict_type)
731 return Ty.getSizeInBits();
733 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
735 // If this type is not derived from any type then take conservative approach.
736 if (!BaseType.isValid())
737 return Ty.getSizeInBits();
739 // If this is a derived type, go ahead and get the base type, unless it's a
740 // reference then it's just the size of the field. Pointer types have no need
741 // of this since they're a different type of qualification on the type.
742 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
743 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
744 return Ty.getSizeInBits();
746 if (BaseType.isDerivedType())
747 return getBaseTypeSize(DD, DIDerivedType(BaseType));
749 return BaseType.getSizeInBits();
752 /// addConstantValue - Add constant value entry in variable DIE.
753 void Unit::addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty) {
754 // FIXME: This is a bit conservative/simple - it emits negative values at
755 // their maximum bit width which is a bit unfortunate (& doesn't prefer
756 // udata/sdata over dataN as suggested by the DWARF spec)
757 assert(MO.isImm() && "Invalid machine operand!");
759 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
762 // If we're a signed constant definitely use sdata.
763 if (SignedConstant) {
764 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
768 // Else use data for now unless it's larger than we can deal with.
769 switch (SizeInBits) {
771 Form = dwarf::DW_FORM_data1;
774 Form = dwarf::DW_FORM_data2;
777 Form = dwarf::DW_FORM_data4;
780 Form = dwarf::DW_FORM_data8;
783 Form = dwarf::DW_FORM_udata;
784 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
787 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
790 /// addConstantFPValue - Add constant value entry in variable DIE.
791 void Unit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
792 assert(MO.isFPImm() && "Invalid machine operand!");
793 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
794 APFloat FPImm = MO.getFPImm()->getValueAPF();
796 // Get the raw data form of the floating point.
797 const APInt FltVal = FPImm.bitcastToAPInt();
798 const char *FltPtr = (const char *)FltVal.getRawData();
800 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
801 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
802 int Incr = (LittleEndian ? 1 : -1);
803 int Start = (LittleEndian ? 0 : NumBytes - 1);
804 int Stop = (LittleEndian ? NumBytes : -1);
806 // Output the constant to DWARF one byte at a time.
807 for (; Start != Stop; Start += Incr)
808 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
810 addBlock(Die, dwarf::DW_AT_const_value, Block);
813 /// addConstantFPValue - Add constant value entry in variable DIE.
814 void Unit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
815 // Pass this down to addConstantValue as an unsigned bag of bits.
816 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
819 /// addConstantValue - Add constant value entry in variable DIE.
820 void Unit::addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned) {
821 addConstantValue(Die, CI->getValue(), Unsigned);
824 // addConstantValue - Add constant value entry in variable DIE.
825 void Unit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
826 unsigned CIBitWidth = Val.getBitWidth();
827 if (CIBitWidth <= 64) {
828 // If we're a signed constant definitely use sdata.
830 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
835 // Else use data for now unless it's larger than we can deal with.
837 switch (CIBitWidth) {
839 Form = dwarf::DW_FORM_data1;
842 Form = dwarf::DW_FORM_data2;
845 Form = dwarf::DW_FORM_data4;
848 Form = dwarf::DW_FORM_data8;
851 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
855 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
859 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
861 // Get the raw data form of the large APInt.
862 const uint64_t *Ptr64 = Val.getRawData();
864 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
865 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
867 // Output the constant to DWARF one byte at a time.
868 for (int i = 0; i < NumBytes; i++) {
871 c = Ptr64[i / 8] >> (8 * (i & 7));
873 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
874 addUInt(Block, dwarf::DW_FORM_data1, c);
877 addBlock(Die, dwarf::DW_AT_const_value, Block);
880 /// addTemplateParams - Add template parameters into buffer.
881 void Unit::addTemplateParams(DIE &Buffer, DIArray TParams) {
882 // Add template parameters.
883 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
884 DIDescriptor Element = TParams.getElement(i);
885 if (Element.isTemplateTypeParameter())
886 constructTemplateTypeParameterDIE(Buffer,
887 DITemplateTypeParameter(Element));
888 else if (Element.isTemplateValueParameter())
889 constructTemplateValueParameterDIE(Buffer,
890 DITemplateValueParameter(Element));
894 /// getOrCreateContextDIE - Get context owner's DIE.
895 DIE *Unit::getOrCreateContextDIE(DIScope Context) {
896 if (!Context || Context.isFile())
898 if (Context.isType())
899 return getOrCreateTypeDIE(DIType(Context));
900 if (Context.isNameSpace())
901 return getOrCreateNameSpace(DINameSpace(Context));
902 if (Context.isSubprogram())
903 return getOrCreateSubprogramDIE(DISubprogram(Context));
904 return getDIE(Context);
907 DIE *Unit::createTypeDIE(DICompositeType Ty) {
908 DIScope Context = resolve(Ty.getContext());
909 DIE *ContextDIE = getOrCreateContextDIE(Context);
911 DIE *TyDIE = getDIE(Ty);
916 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
918 constructTypeDIE(*TyDIE, Ty);
920 updateAcceleratorTables(Context, Ty, TyDIE);
924 /// Return true if the type is appropriately scoped to be contained inside
925 /// its own type unit.
926 static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
927 DIScope Parent = DD->resolve(Ty.getContext());
929 // Don't generate a hash for anything scoped inside a function.
930 if (Parent.isSubprogram())
932 Parent = DD->resolve(Parent.getContext());
937 /// Return true if the type should be split out into a type unit.
938 static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
939 if (!GenerateTypeUnits)
942 uint16_t Tag = CTy.getTag();
945 case dwarf::DW_TAG_structure_type:
946 case dwarf::DW_TAG_union_type:
947 case dwarf::DW_TAG_enumeration_type:
948 case dwarf::DW_TAG_class_type:
949 // If this is a class, structure, union, or enumeration type
950 // that is a definition (not a declaration), and not scoped
951 // inside a function then separate this out as a type unit.
952 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
958 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
960 DIE *Unit::getOrCreateTypeDIE(const MDNode *TyNode) {
967 // Construct the context before querying for the existence of the DIE in case
968 // such construction creates the DIE.
969 DIScope Context = resolve(Ty.getContext());
970 DIE *ContextDIE = getOrCreateContextDIE(Context);
973 DIE *TyDIE = getDIE(Ty);
978 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
980 if (Ty.isBasicType())
981 constructTypeDIE(*TyDIE, DIBasicType(Ty));
982 else if (Ty.isCompositeType()) {
983 DICompositeType CTy(Ty);
984 if (shouldCreateTypeUnit(CTy, DD)) {
985 DD->addTypeUnitType(getLanguage(), TyDIE, CTy);
986 // Skip updating the accellerator tables since this is not the full type
989 constructTypeDIE(*TyDIE, CTy);
991 assert(Ty.isDerivedType() && "Unknown kind of DIType");
992 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
995 updateAcceleratorTables(Context, Ty, TyDIE);
1000 void Unit::updateAcceleratorTables(DIScope Context, DIType Ty,
1002 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1003 bool IsImplementation = 0;
1004 if (Ty.isCompositeType()) {
1005 DICompositeType CT(Ty);
1006 // A runtime language of 0 actually means C/C++ and that any
1007 // non-negative value is some version of Objective-C/C++.
1008 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
1010 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
1011 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
1013 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1014 Context.isNameSpace())
1015 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
1019 /// addType - Add a new type attribute to the specified entity.
1020 void Unit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
1021 assert(Ty && "Trying to add a type that doesn't exist?");
1023 // Check for pre-existence.
1024 DIEEntry *Entry = getDIEEntry(Ty);
1025 // If it exists then use the existing value.
1027 addDIEEntry(Entity, Attribute, Entry);
1032 DIE *Buffer = getOrCreateTypeDIE(Ty);
1035 Entry = createDIEEntry(Buffer);
1036 insertDIEEntry(Ty, Entry);
1037 addDIEEntry(Entity, Attribute, Entry);
1040 // Accelerator table mutators - add each name along with its companion
1041 // DIE to the proper table while ensuring that the name that we're going
1042 // to reference is in the string table. We do this since the names we
1043 // add may not only be identical to the names in the DIE.
1044 void Unit::addAccelName(StringRef Name, const DIE *Die) {
1045 DU->getStringPoolEntry(Name);
1046 std::vector<const DIE *> &DIEs = AccelNames[Name];
1047 DIEs.push_back(Die);
1050 void Unit::addAccelObjC(StringRef Name, const DIE *Die) {
1051 DU->getStringPoolEntry(Name);
1052 std::vector<const DIE *> &DIEs = AccelObjC[Name];
1053 DIEs.push_back(Die);
1056 void Unit::addAccelNamespace(StringRef Name, const DIE *Die) {
1057 DU->getStringPoolEntry(Name);
1058 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
1059 DIEs.push_back(Die);
1062 void Unit::addAccelType(StringRef Name, std::pair<const DIE *, unsigned> Die) {
1063 DU->getStringPoolEntry(Name);
1064 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
1065 DIEs.push_back(Die);
1068 /// addGlobalName - Add a new global name to the compile unit.
1069 void Unit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
1070 std::string FullName = getParentContextString(Context) + Name.str();
1071 GlobalNames[FullName] = Die;
1074 /// getParentContextString - Walks the metadata parent chain in a language
1075 /// specific manner (using the compile unit language) and returns
1076 /// it as a string. This is done at the metadata level because DIEs may
1077 /// not currently have been added to the parent context and walking the
1078 /// DIEs looking for names is more expensive than walking the metadata.
1079 std::string Unit::getParentContextString(DIScope Context) const {
1083 // FIXME: Decide whether to implement this for non-C++ languages.
1084 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1088 SmallVector<DIScope, 1> Parents;
1089 while (!Context.isCompileUnit()) {
1090 Parents.push_back(Context);
1091 if (Context.getContext())
1092 Context = resolve(Context.getContext());
1094 // Structure, etc types will have a NULL context if they're at the top
1099 // Reverse iterate over our list to go from the outermost construct to the
1101 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1105 StringRef Name = Ctx.getName();
1106 if (!Name.empty()) {
1114 /// constructTypeDIE - Construct basic type die from DIBasicType.
1115 void Unit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1116 // Get core information.
1117 StringRef Name = BTy.getName();
1118 // Add name if not anonymous or intermediate type.
1120 addString(&Buffer, dwarf::DW_AT_name, Name);
1122 // An unspecified type only has a name attribute.
1123 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
1126 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1129 uint64_t Size = BTy.getSizeInBits() >> 3;
1130 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1133 /// constructTypeDIE - Construct derived type die from DIDerivedType.
1134 void Unit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1135 // Get core information.
1136 StringRef Name = DTy.getName();
1137 uint64_t Size = DTy.getSizeInBits() >> 3;
1138 uint16_t Tag = Buffer.getTag();
1140 // Map to main type, void will not have a type.
1141 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
1143 addType(&Buffer, FromTy);
1145 // Add name if not anonymous or intermediate type.
1147 addString(&Buffer, dwarf::DW_AT_name, Name);
1149 // Add size if non-zero (derived types might be zero-sized.)
1150 if (Size && Tag != dwarf::DW_TAG_pointer_type)
1151 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1153 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
1154 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1155 getOrCreateTypeDIE(resolve(DTy.getClassType())));
1156 // Add source line info if available and TyDesc is not a forward declaration.
1157 if (!DTy.isForwardDecl())
1158 addSourceLine(&Buffer, DTy);
1161 /// constructTypeDIE - Construct type DIE from DICompositeType.
1162 void Unit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1163 // Add name if not anonymous or intermediate type.
1164 StringRef Name = CTy.getName();
1166 uint64_t Size = CTy.getSizeInBits() >> 3;
1167 uint16_t Tag = Buffer.getTag();
1170 case dwarf::DW_TAG_array_type:
1171 constructArrayTypeDIE(Buffer, CTy);
1173 case dwarf::DW_TAG_enumeration_type:
1174 constructEnumTypeDIE(Buffer, CTy);
1176 case dwarf::DW_TAG_subroutine_type: {
1177 // Add return type. A void return won't have a type.
1178 DIArray Elements = CTy.getTypeArray();
1179 DIType RTy(Elements.getElement(0));
1181 addType(&Buffer, RTy);
1183 bool isPrototyped = true;
1185 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1186 DIDescriptor Ty = Elements.getElement(i);
1187 if (Ty.isUnspecifiedParameter()) {
1188 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1189 isPrototyped = false;
1191 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1192 addType(Arg, DIType(Ty));
1193 if (DIType(Ty).isArtificial())
1194 addFlag(Arg, dwarf::DW_AT_artificial);
1197 // Add prototype flag if we're dealing with a C language and the
1198 // function has been prototyped.
1199 uint16_t Language = getLanguage();
1201 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1202 Language == dwarf::DW_LANG_ObjC))
1203 addFlag(&Buffer, dwarf::DW_AT_prototyped);
1205 case dwarf::DW_TAG_structure_type:
1206 case dwarf::DW_TAG_union_type:
1207 case dwarf::DW_TAG_class_type: {
1208 // Add elements to structure type.
1209 DIArray Elements = CTy.getTypeArray();
1210 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1211 DIDescriptor Element = Elements.getElement(i);
1212 DIE *ElemDie = NULL;
1213 if (Element.isSubprogram()) {
1214 DISubprogram SP(Element);
1215 ElemDie = getOrCreateSubprogramDIE(SP);
1216 if (SP.isProtected())
1217 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1218 dwarf::DW_ACCESS_protected);
1219 else if (SP.isPrivate())
1220 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1221 dwarf::DW_ACCESS_private);
1223 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1224 dwarf::DW_ACCESS_public);
1225 if (SP.isExplicit())
1226 addFlag(ElemDie, dwarf::DW_AT_explicit);
1227 } else if (Element.isDerivedType()) {
1228 DIDerivedType DDTy(Element);
1229 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1230 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1231 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
1232 dwarf::DW_AT_friend);
1233 } else if (DDTy.isStaticMember()) {
1234 getOrCreateStaticMemberDIE(DDTy);
1236 constructMemberDIE(Buffer, DDTy);
1238 } else if (Element.isObjCProperty()) {
1239 DIObjCProperty Property(Element);
1240 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
1241 StringRef PropertyName = Property.getObjCPropertyName();
1242 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1243 addType(ElemDie, Property.getType());
1244 addSourceLine(ElemDie, Property);
1245 StringRef GetterName = Property.getObjCPropertyGetterName();
1246 if (!GetterName.empty())
1247 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1248 StringRef SetterName = Property.getObjCPropertySetterName();
1249 if (!SetterName.empty())
1250 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1251 unsigned PropertyAttributes = 0;
1252 if (Property.isReadOnlyObjCProperty())
1253 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1254 if (Property.isReadWriteObjCProperty())
1255 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1256 if (Property.isAssignObjCProperty())
1257 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1258 if (Property.isRetainObjCProperty())
1259 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1260 if (Property.isCopyObjCProperty())
1261 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1262 if (Property.isNonAtomicObjCProperty())
1263 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1264 if (PropertyAttributes)
1265 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
1266 PropertyAttributes);
1268 DIEEntry *Entry = getDIEEntry(Element);
1270 Entry = createDIEEntry(ElemDie);
1271 insertDIEEntry(Element, Entry);
1277 if (CTy.isAppleBlockExtension())
1278 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
1280 DICompositeType ContainingType(resolve(CTy.getContainingType()));
1282 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1283 getOrCreateTypeDIE(ContainingType));
1285 if (CTy.isObjcClassComplete())
1286 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1288 // Add template parameters to a class, structure or union types.
1289 // FIXME: The support isn't in the metadata for this yet.
1290 if (Tag == dwarf::DW_TAG_class_type ||
1291 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1292 addTemplateParams(Buffer, CTy.getTemplateParams());
1300 // Add name if not anonymous or intermediate type.
1302 addString(&Buffer, dwarf::DW_AT_name, Name);
1304 if (Tag == dwarf::DW_TAG_enumeration_type ||
1305 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1306 Tag == dwarf::DW_TAG_union_type) {
1307 // Add size if non-zero (derived types might be zero-sized.)
1308 // TODO: Do we care about size for enum forward declarations?
1310 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1311 else if (!CTy.isForwardDecl())
1312 // Add zero size if it is not a forward declaration.
1313 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
1315 // If we're a forward decl, say so.
1316 if (CTy.isForwardDecl())
1317 addFlag(&Buffer, dwarf::DW_AT_declaration);
1319 // Add source line info if available.
1320 if (!CTy.isForwardDecl())
1321 addSourceLine(&Buffer, CTy);
1323 // No harm in adding the runtime language to the declaration.
1324 unsigned RLang = CTy.getRunTimeLang();
1326 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1331 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
1332 /// DITemplateTypeParameter.
1333 void Unit::constructTemplateTypeParameterDIE(DIE &Buffer,
1334 DITemplateTypeParameter TP) {
1336 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1337 // Add the type if it exists, it could be void and therefore no type.
1339 addType(ParamDIE, resolve(TP.getType()));
1340 if (!TP.getName().empty())
1341 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1344 /// constructTemplateValueParameterDIE - Construct new DIE for the given
1345 /// DITemplateValueParameter.
1346 void Unit::constructTemplateValueParameterDIE(DIE &Buffer,
1347 DITemplateValueParameter VP) {
1348 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
1350 // Add the type if there is one, template template and template parameter
1351 // packs will not have a type.
1352 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
1353 addType(ParamDIE, resolve(VP.getType()));
1354 if (!VP.getName().empty())
1355 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1356 if (Value *Val = VP.getValue()) {
1357 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1358 addConstantValue(ParamDIE, CI,
1359 isUnsignedDIType(DD, resolve(VP.getType())));
1360 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1361 // For declaration non-type template parameters (such as global values and
1363 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1364 addOpAddress(Block, Asm->getSymbol(GV));
1365 // Emit DW_OP_stack_value to use the address as the immediate value of the
1366 // parameter, rather than a pointer to it.
1367 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1368 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
1369 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1370 assert(isa<MDString>(Val));
1371 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1372 cast<MDString>(Val)->getString());
1373 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1374 assert(isa<MDNode>(Val));
1375 DIArray A(cast<MDNode>(Val));
1376 addTemplateParams(*ParamDIE, A);
1381 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1382 DIE *Unit::getOrCreateNameSpace(DINameSpace NS) {
1383 // Construct the context before querying for the existence of the DIE in case
1384 // such construction creates the DIE.
1385 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
1387 DIE *NDie = getDIE(NS);
1390 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1392 if (!NS.getName().empty()) {
1393 addString(NDie, dwarf::DW_AT_name, NS.getName());
1394 addAccelNamespace(NS.getName(), NDie);
1395 addGlobalName(NS.getName(), NDie, NS.getContext());
1397 addAccelNamespace("(anonymous namespace)", NDie);
1398 addSourceLine(NDie, NS);
1402 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1403 DIE *Unit::getOrCreateSubprogramDIE(DISubprogram SP) {
1404 // Construct the context before querying for the existence of the DIE in case
1405 // such construction creates the DIE (as is the case for member function
1407 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
1409 DIE *SPDie = getDIE(SP);
1413 DISubprogram SPDecl = SP.getFunctionDeclaration();
1414 if (SPDecl.isSubprogram())
1415 // Add subprogram definitions to the CU die directly.
1416 ContextDIE = CUDie.get();
1418 // DW_TAG_inlined_subroutine may refer to this DIE.
1419 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1421 DIE *DeclDie = NULL;
1422 if (SPDecl.isSubprogram())
1423 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1425 // Add function template parameters.
1426 addTemplateParams(*SPDie, SP.getTemplateParams());
1428 // If this DIE is going to refer declaration info using AT_specification
1429 // then there is no need to add other attributes.
1431 // Refer function declaration directly.
1432 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
1437 // Add the linkage name if we have one.
1438 StringRef LinkageName = SP.getLinkageName();
1439 if (!LinkageName.empty())
1440 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1441 GlobalValue::getRealLinkageName(LinkageName));
1443 // Constructors and operators for anonymous aggregates do not have names.
1444 if (!SP.getName().empty())
1445 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1447 addSourceLine(SPDie, SP);
1449 // Add the prototype if we have a prototype and we have a C like
1451 uint16_t Language = getLanguage();
1452 if (SP.isPrototyped() &&
1453 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1454 Language == dwarf::DW_LANG_ObjC))
1455 addFlag(SPDie, dwarf::DW_AT_prototyped);
1457 DICompositeType SPTy = SP.getType();
1458 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1459 "the type of a subprogram should be a subroutine");
1461 DIArray Args = SPTy.getTypeArray();
1462 // Add a return type. If this is a type like a C/C++ void type we don't add a
1464 if (Args.getElement(0))
1465 addType(SPDie, DIType(Args.getElement(0)));
1467 unsigned VK = SP.getVirtuality();
1469 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1470 DIEBlock *Block = getDIEBlock();
1471 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1472 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1473 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1474 ContainingTypeMap.insert(
1475 std::make_pair(SPDie, resolve(SP.getContainingType())));
1478 if (!SP.isDefinition()) {
1479 addFlag(SPDie, dwarf::DW_AT_declaration);
1481 // Add arguments. Do not add arguments for subprogram definition. They will
1482 // be handled while processing variables.
1483 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1484 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
1485 DIType ATy(Args.getElement(i));
1487 if (ATy.isArtificial())
1488 addFlag(Arg, dwarf::DW_AT_artificial);
1492 if (SP.isArtificial())
1493 addFlag(SPDie, dwarf::DW_AT_artificial);
1495 if (!SP.isLocalToUnit())
1496 addFlag(SPDie, dwarf::DW_AT_external);
1498 if (SP.isOptimized())
1499 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1501 if (unsigned isa = Asm->getISAEncoding()) {
1502 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1508 // Return const expression if value is a GEP to access merged global
1510 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1511 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1512 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1513 if (!CE || CE->getNumOperands() != 3 ||
1514 CE->getOpcode() != Instruction::GetElementPtr)
1517 // First operand points to a global struct.
1518 Value *Ptr = CE->getOperand(0);
1519 if (!isa<GlobalValue>(Ptr) ||
1520 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1523 // Second operand is zero.
1524 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1525 if (!CI || !CI->isZero())
1528 // Third operand is offset.
1529 if (!isa<ConstantInt>(CE->getOperand(2)))
1535 /// createGlobalVariableDIE - create global variable DIE.
1536 void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
1537 // Check for pre-existence.
1541 if (!GV.isGlobalVariable())
1544 DIScope GVContext = GV.getContext();
1545 DIType GTy = GV.getType();
1547 // If this is a static data member definition, some attributes belong
1548 // to the declaration DIE.
1549 DIE *VariableDIE = NULL;
1550 bool IsStaticMember = false;
1551 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1552 if (SDMDecl.Verify()) {
1553 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1554 // We need the declaration DIE that is in the static member's class.
1555 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
1556 IsStaticMember = true;
1559 // If this is not a static data member definition, create the variable
1560 // DIE and add the initial set of attributes to it.
1562 // Construct the context before querying for the existence of the DIE in
1563 // case such construction creates the DIE.
1564 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
1567 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
1569 // Add name and type.
1570 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1571 addType(VariableDIE, GTy);
1573 // Add scoping info.
1574 if (!GV.isLocalToUnit())
1575 addFlag(VariableDIE, dwarf::DW_AT_external);
1577 // Add line number info.
1578 addSourceLine(VariableDIE, GV);
1582 bool addToAccelTable = false;
1583 DIE *VariableSpecDIE = NULL;
1584 bool isGlobalVariable = GV.getGlobal() != NULL;
1585 if (isGlobalVariable) {
1586 addToAccelTable = true;
1587 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1588 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
1589 if (GV.getGlobal()->isThreadLocal()) {
1590 // FIXME: Make this work with -gsplit-dwarf.
1591 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1592 assert((PointerSize == 4 || PointerSize == 8) &&
1593 "Add support for other sizes if necessary");
1594 const MCExpr *Expr =
1595 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
1596 // Based on GCC's support for TLS:
1597 if (!DD->useSplitDwarf()) {
1598 // 1) Start with a constNu of the appropriate pointer size
1599 addUInt(Block, dwarf::DW_FORM_data1,
1600 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1601 // 2) containing the (relocated) offset of the TLS variable
1602 // within the module's TLS block.
1603 addExpr(Block, dwarf::DW_FORM_udata, Expr);
1605 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1606 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
1608 // 3) followed by a custom OP to make the debugger do a TLS lookup.
1609 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
1611 DD->addArangeLabel(SymbolCU(this, Sym));
1612 addOpAddress(Block, Sym);
1614 // Do not create specification DIE if context is either compile unit
1616 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1617 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
1618 // Create specification DIE.
1619 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie);
1620 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
1621 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
1622 // A static member's declaration is already flagged as such.
1623 if (!SDMDecl.Verify())
1624 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1626 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1628 // Add the linkage name.
1629 StringRef LinkageName = GV.getLinkageName();
1630 if (!LinkageName.empty())
1631 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1632 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1634 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1636 dwarf::DW_AT_MIPS_linkage_name,
1637 GlobalValue::getRealLinkageName(LinkageName));
1638 } else if (const ConstantInt *CI =
1639 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
1640 // AT_const_value was added when the static member was created. To avoid
1641 // emitting AT_const_value multiple times, we only add AT_const_value when
1642 // it is not a static member.
1643 if (!IsStaticMember)
1644 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
1645 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
1646 addToAccelTable = true;
1647 // GV is a merged global.
1648 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1649 Value *Ptr = CE->getOperand(0);
1650 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1651 DD->addArangeLabel(SymbolCU(this, Sym));
1652 addOpAddress(Block, Sym);
1653 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1654 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
1655 addUInt(Block, dwarf::DW_FORM_udata,
1656 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1657 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1658 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1661 if (addToAccelTable) {
1662 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1663 addAccelName(GV.getName(), AddrDIE);
1665 // If the linkage name is different than the name, go ahead and output
1666 // that as well into the name table.
1667 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1668 addAccelName(GV.getLinkageName(), AddrDIE);
1671 if (!GV.isLocalToUnit())
1672 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1676 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1677 void Unit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1678 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1679 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
1681 // The LowerBound value defines the lower bounds which is typically zero for
1682 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1683 // Count == -1 then the array is unbounded and we do not emit
1684 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1685 // Count == 0, then the array has zero elements in which case we do not emit
1687 int64_t LowerBound = SR.getLo();
1688 int64_t DefaultLowerBound = getDefaultLowerBound();
1689 int64_t Count = SR.getCount();
1691 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1692 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1694 if (Count != -1 && Count != 0)
1695 // FIXME: An unbounded array should reference the expression that defines
1697 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1698 LowerBound + Count - 1);
1701 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1702 void Unit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
1704 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1706 // Emit the element type.
1707 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
1709 // Get an anonymous type for index type.
1710 // FIXME: This type should be passed down from the front end
1711 // as different languages may have different sizes for indexes.
1712 DIE *IdxTy = getIndexTyDie();
1714 // Construct an anonymous type for index type.
1715 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get());
1716 addString(IdxTy, dwarf::DW_AT_name, "int");
1717 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
1718 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1719 dwarf::DW_ATE_signed);
1720 setIndexTyDie(IdxTy);
1723 // Add subranges to array type.
1724 DIArray Elements = CTy.getTypeArray();
1725 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1726 DIDescriptor Element = Elements.getElement(i);
1727 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1728 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1732 /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
1733 void Unit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
1734 DIArray Elements = CTy.getTypeArray();
1736 // Add enumerators to enumeration type.
1737 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1738 DIEnumerator Enum(Elements.getElement(i));
1739 if (Enum.isEnumerator()) {
1740 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1741 StringRef Name = Enum.getName();
1742 addString(Enumerator, dwarf::DW_AT_name, Name);
1743 int64_t Value = Enum.getEnumValue();
1744 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1748 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1750 addType(&Buffer, DTy);
1751 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1755 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1757 void Unit::constructContainingTypeDIEs() {
1758 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1759 CE = ContainingTypeMap.end();
1761 DIE *SPDie = CI->first;
1762 DIDescriptor D(CI->second);
1765 DIE *NDie = getDIE(D);
1768 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
1772 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1773 DIE *Unit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
1774 StringRef Name = DV.getName();
1776 // Define variable debug information entry.
1777 DIE *VariableDie = new DIE(DV.getTag());
1778 DbgVariable *AbsVar = DV.getAbstractVariable();
1779 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1781 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
1784 addString(VariableDie, dwarf::DW_AT_name, Name);
1785 addSourceLine(VariableDie, DV.getVariable());
1786 addType(VariableDie, DV.getType());
1789 if (DV.isArtificial())
1790 addFlag(VariableDie, dwarf::DW_AT_artificial);
1792 if (isScopeAbstract) {
1793 DV.setDIE(VariableDie);
1797 // Add variable address.
1799 unsigned Offset = DV.getDotDebugLocOffset();
1800 if (Offset != ~0U) {
1801 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1802 Asm->GetTempSymbol("debug_loc", Offset));
1803 DV.setDIE(VariableDie);
1807 // Check if variable is described by a DBG_VALUE instruction.
1808 if (const MachineInstr *DVInsn = DV.getMInsn()) {
1809 assert(DVInsn->getNumOperands() == 3);
1810 if (DVInsn->getOperand(0).isReg()) {
1811 const MachineOperand RegOp = DVInsn->getOperand(0);
1812 // If the second operand is an immediate, this is an indirect value.
1813 if (DVInsn->getOperand(1).isImm()) {
1814 MachineLocation Location(RegOp.getReg(),
1815 DVInsn->getOperand(1).getImm());
1816 addVariableAddress(DV, VariableDie, Location);
1817 } else if (RegOp.getReg())
1818 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
1819 } else if (DVInsn->getOperand(0).isImm())
1820 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
1821 else if (DVInsn->getOperand(0).isFPImm())
1822 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1823 else if (DVInsn->getOperand(0).isCImm())
1824 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1825 isUnsignedDIType(DD, DV.getType()));
1827 DV.setDIE(VariableDie);
1830 // .. else use frame index.
1831 int FI = DV.getFrameIndex();
1833 unsigned FrameReg = 0;
1834 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1835 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1836 MachineLocation Location(FrameReg, Offset);
1837 addVariableAddress(DV, VariableDie, Location);
1841 DV.setDIE(VariableDie);
1845 /// constructMemberDIE - Construct member DIE from DIDerivedType.
1846 void Unit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
1847 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
1848 StringRef Name = DT.getName();
1850 addString(MemberDie, dwarf::DW_AT_name, Name);
1852 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
1854 addSourceLine(MemberDie, DT);
1856 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1857 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1859 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
1861 // For C++, virtual base classes are not at fixed offset. Use following
1862 // expression to extract appropriate offset from vtable.
1863 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1865 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1866 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1867 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1868 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1869 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1870 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1871 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1872 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1874 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1876 uint64_t Size = DT.getSizeInBits();
1877 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1878 uint64_t OffsetInBytes;
1880 if (Size != FieldSize) {
1882 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1883 getBaseTypeSize(DD, DT) >> 3);
1884 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1886 uint64_t Offset = DT.getOffsetInBits();
1887 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1888 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1889 uint64_t FieldOffset = (HiMark - FieldSize);
1890 Offset -= FieldOffset;
1892 // Maybe we need to work from the other end.
1893 if (Asm->getDataLayout().isLittleEndian())
1894 Offset = FieldSize - (Offset + Size);
1895 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1897 // Here WD_AT_data_member_location points to the anonymous
1898 // field that includes this bit field.
1899 OffsetInBytes = FieldOffset >> 3;
1901 // This is not a bitfield.
1902 OffsetInBytes = DT.getOffsetInBits() >> 3;
1903 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
1906 if (DT.isProtected())
1907 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1908 dwarf::DW_ACCESS_protected);
1909 else if (DT.isPrivate())
1910 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1911 dwarf::DW_ACCESS_private);
1912 // Otherwise C++ member and base classes are considered public.
1914 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1915 dwarf::DW_ACCESS_public);
1917 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1918 dwarf::DW_VIRTUALITY_virtual);
1920 // Objective-C properties.
1921 if (MDNode *PNode = DT.getObjCProperty())
1922 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1923 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1926 if (DT.isArtificial())
1927 addFlag(MemberDie, dwarf::DW_AT_artificial);
1930 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
1931 DIE *Unit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
1935 // Construct the context before querying for the existence of the DIE in case
1936 // such construction creates the DIE.
1937 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
1938 assert(dwarf::isType(ContextDIE->getTag()) &&
1939 "Static member should belong to a type.");
1941 DIE *StaticMemberDIE = getDIE(DT);
1942 if (StaticMemberDIE)
1943 return StaticMemberDIE;
1945 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
1947 DIType Ty = resolve(DT.getTypeDerivedFrom());
1949 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1950 addType(StaticMemberDIE, Ty);
1951 addSourceLine(StaticMemberDIE, DT);
1952 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1953 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1955 // FIXME: We could omit private if the parent is a class_type, and
1956 // public if the parent is something else.
1957 if (DT.isProtected())
1958 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1959 dwarf::DW_ACCESS_protected);
1960 else if (DT.isPrivate())
1961 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1962 dwarf::DW_ACCESS_private);
1964 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1965 dwarf::DW_ACCESS_public);
1967 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1968 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
1969 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1970 addConstantFPValue(StaticMemberDIE, CFP);
1972 return StaticMemberDIE;
1975 void Unit::emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym) {
1976 Asm->OutStreamer.AddComment("DWARF version number");
1977 Asm->EmitInt16(DD->getDwarfVersion());
1978 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1979 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
1981 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1982 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());