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 "DwarfUnit.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 - Unit constructor.
41 DwarfUnit::DwarfUnit(unsigned UID, DIE *D, DICompileUnit Node, AsmPrinter *A,
42 DwarfDebug *DW, DwarfFile *DWU)
43 : UniqueID(UID), Node(Node), UnitDie(D), DebugInfoOffset(0), Asm(A), DD(DW),
44 DU(DWU), IndexTyDie(0), Section(0) {
45 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
48 CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node,
49 AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
50 : DwarfUnit(UID, D, Node, A, DW, DWU) {
54 TypeUnit::TypeUnit(unsigned UID, DIE *D, uint16_t Language, AsmPrinter *A,
55 DwarfDebug *DW, DwarfFile *DWU)
56 : DwarfUnit(UID, D, DICompileUnit(), A, DW, DWU), Language(Language) {}
58 /// ~Unit - Destructor for compile unit.
59 DwarfUnit::~DwarfUnit() {
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 *DwarfUnit::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 DwarfUnit::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 *DwarfUnit::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 DwarfUnit::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 DwarfUnit::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 DwarfUnit::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 DwarfUnit::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 DwarfUnit::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 DwarfUnit::addSInt(DIEBlock *Die, Optional<dwarf::Form> Form,
185 addSInt(Die, (dwarf::Attribute)0, Form, Integer);
188 /// addString - Add a string attribute data and value. We always emit a
189 /// reference to the string pool instead of immediate strings so that DIEs have
190 /// more predictable sizes. In the case of split dwarf we emit an index
191 /// into another table which gets us the static offset into the string
193 void DwarfUnit::addString(DIE *Die, dwarf::Attribute Attribute,
197 if (!DD->useSplitDwarf()) {
198 MCSymbol *Symb = DU->getStringPoolEntry(String);
199 if (Asm->needsRelocationsForDwarfStringPool())
200 Value = new (DIEValueAllocator) DIELabel(Symb);
202 MCSymbol *StringPool = DU->getStringPoolSym();
203 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
205 Form = dwarf::DW_FORM_strp;
207 unsigned idx = DU->getStringPoolIndex(String);
208 Value = new (DIEValueAllocator) DIEInteger(idx);
209 Form = dwarf::DW_FORM_GNU_str_index;
211 DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String);
212 Die->addValue(Attribute, Form, Str);
215 /// addLocalString - Add a string attribute data and value. This is guaranteed
216 /// to be in the local string pool instead of indirected.
217 void DwarfUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute,
219 MCSymbol *Symb = DU->getStringPoolEntry(String);
221 if (Asm->needsRelocationsForDwarfStringPool())
222 Value = new (DIEValueAllocator) DIELabel(Symb);
224 MCSymbol *StringPool = DU->getStringPoolSym();
225 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
227 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
230 /// addExpr - Add a Dwarf expression attribute data and value.
232 void DwarfUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) {
233 DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr);
234 Die->addValue((dwarf::Attribute)0, Form, Value);
237 /// addLabel - Add a Dwarf label attribute data and value.
239 void DwarfUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form,
240 const MCSymbol *Label) {
241 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
242 Die->addValue(Attribute, Form, Value);
245 void DwarfUnit::addLabel(DIEBlock *Die, dwarf::Form Form,
246 const MCSymbol *Label) {
247 addLabel(Die, (dwarf::Attribute)0, Form, Label);
250 /// addSectionLabel - Add a Dwarf section label attribute data and value.
252 void DwarfUnit::addSectionLabel(DIE *Die, dwarf::Attribute Attribute,
253 const MCSymbol *Label) {
254 if (DD->getDwarfVersion() >= 4)
255 addLabel(Die, Attribute, dwarf::DW_FORM_sec_offset, Label);
257 addLabel(Die, Attribute, dwarf::DW_FORM_data4, Label);
260 /// addSectionOffset - Add an offset into a section attribute data and value.
262 void DwarfUnit::addSectionOffset(DIE *Die, dwarf::Attribute Attribute,
264 if (DD->getDwarfVersion() >= 4)
265 addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
267 addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
270 /// addLabelAddress - Add a dwarf label attribute data and value using
271 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
273 void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute,
276 DD->addArangeLabel(SymbolCU(this, Label));
278 if (!DD->useSplitDwarf()) {
280 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
281 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
283 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
284 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
287 unsigned idx = DU->getAddrPoolIndex(Label);
288 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
289 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
293 /// addOpAddress - Add a dwarf op address data and value using the
294 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
296 void DwarfUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) {
297 if (!DD->useSplitDwarf()) {
298 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
299 addLabel(Die, dwarf::DW_FORM_udata, Sym);
301 addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
302 addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym));
306 /// addSectionDelta - Add a section label delta attribute data and value.
308 void DwarfUnit::addSectionDelta(DIE *Die, dwarf::Attribute Attribute,
309 const MCSymbol *Hi, const MCSymbol *Lo) {
310 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
311 if (DD->getDwarfVersion() >= 4)
312 Die->addValue(Attribute, dwarf::DW_FORM_sec_offset, Value);
314 Die->addValue(Attribute, dwarf::DW_FORM_data4, Value);
317 /// addDIEEntry - Add a DIE attribute data and value.
319 void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry) {
320 addDIEEntry(Die, Attribute, createDIEEntry(Entry));
323 void DwarfUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute,
325 const DIE *DieCU = Die->getUnitOrNull();
326 const DIE *EntryCU = Entry->getEntry()->getUnitOrNull();
328 // We assume that Die belongs to this CU, if it is not linked to any CU yet.
329 DieCU = getUnitDie();
331 EntryCU = getUnitDie();
332 Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4
333 : dwarf::DW_FORM_ref_addr,
337 /// Create a DIE with the given Tag, add the DIE to its parent, and
338 /// call insertDIE if MD is not null.
339 DIE *DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) {
340 DIE *Die = new DIE(Tag);
341 Parent.addChild(Die);
347 /// addBlock - Add block data.
349 void DwarfUnit::addBlock(DIE *Die, dwarf::Attribute Attribute,
351 Block->ComputeSize(Asm);
352 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
353 Die->addValue(Attribute, Block->BestForm(), Block);
356 /// addSourceLine - Add location information to specified debug information
358 void DwarfUnit::addSourceLine(DIE *Die, DIVariable V) {
363 unsigned Line = V.getLineNumber();
367 DD->getOrCreateSourceID(V.getContext().getFilename(),
368 V.getContext().getDirectory(), getUniqueID());
369 assert(FileID && "Invalid file id");
370 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
371 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
374 /// addSourceLine - Add location information to specified debug information
376 void DwarfUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
377 // Verify global variable.
378 if (!G.isGlobalVariable())
381 unsigned Line = G.getLineNumber();
385 DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID());
386 assert(FileID && "Invalid file id");
387 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
388 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
391 /// addSourceLine - Add location information to specified debug information
393 void DwarfUnit::addSourceLine(DIE *Die, DISubprogram SP) {
394 // Verify subprogram.
395 if (!SP.isSubprogram())
398 // If the line number is 0, don't add it.
399 unsigned Line = SP.getLineNumber();
403 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(),
405 assert(FileID && "Invalid file id");
406 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
407 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
410 /// addSourceLine - Add location information to specified debug information
412 void DwarfUnit::addSourceLine(DIE *Die, DIType Ty) {
417 unsigned Line = Ty.getLineNumber();
420 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(),
422 assert(FileID && "Invalid file id");
423 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
424 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
427 /// addSourceLine - Add location information to specified debug information
429 void DwarfUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
431 if (!Ty.isObjCProperty())
434 unsigned Line = Ty.getLineNumber();
437 DIFile File = Ty.getFile();
438 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
439 File.getDirectory(), getUniqueID());
440 assert(FileID && "Invalid file id");
441 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
442 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
445 /// addSourceLine - Add location information to specified debug information
447 void DwarfUnit::addSourceLine(DIE *Die, DINameSpace NS) {
452 unsigned Line = NS.getLineNumber();
455 StringRef FN = NS.getFilename();
458 DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID());
459 assert(FileID && "Invalid file id");
460 addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
461 addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
464 /// addVariableAddress - Add DW_AT_location attribute for a
465 /// DbgVariable based on provided MachineLocation.
466 void DwarfUnit::addVariableAddress(const DbgVariable &DV, DIE *Die,
467 MachineLocation Location) {
468 if (DV.variableHasComplexAddress())
469 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
470 else if (DV.isBlockByrefVariable())
471 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
473 addAddress(Die, dwarf::DW_AT_location, Location,
474 DV.getVariable().isIndirect());
477 /// addRegisterOp - Add register operand.
478 void DwarfUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) {
479 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
480 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
482 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
484 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
485 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
489 /// addRegisterOffset - Add register offset.
490 void DwarfUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg,
492 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
493 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
494 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
495 if (Reg == TRI->getFrameRegister(*Asm->MF))
496 // If variable offset is based in frame register then use fbreg.
497 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
499 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
501 addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
502 addUInt(TheDie, dwarf::DW_FORM_udata, DWReg);
504 addSInt(TheDie, dwarf::DW_FORM_sdata, Offset);
507 /// addAddress - Add an address attribute to a die based on the location
509 void DwarfUnit::addAddress(DIE *Die, dwarf::Attribute Attribute,
510 const MachineLocation &Location, bool Indirect) {
511 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
513 if (Location.isReg() && !Indirect)
514 addRegisterOp(Block, Location.getReg());
516 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
517 if (Indirect && !Location.isReg()) {
518 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
522 // Now attach the location information to the DIE.
523 addBlock(Die, Attribute, Block);
526 /// addComplexAddress - Start with the address based on the location provided,
527 /// and generate the DWARF information necessary to find the actual variable
528 /// given the extra address information encoded in the DbgVariable, starting
529 /// from the starting location. Add the DWARF information to the die.
531 void DwarfUnit::addComplexAddress(const DbgVariable &DV, DIE *Die,
532 dwarf::Attribute Attribute,
533 const MachineLocation &Location) {
534 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
535 unsigned N = DV.getNumAddrElements();
537 if (Location.isReg()) {
538 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
539 // If first address element is OpPlus then emit
540 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
541 addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1));
544 addRegisterOp(Block, Location.getReg());
546 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
549 uint64_t Element = DV.getAddrElement(i);
550 if (Element == DIBuilder::OpPlus) {
551 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
552 addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i));
553 } else if (Element == DIBuilder::OpDeref) {
554 if (!Location.isReg())
555 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
557 llvm_unreachable("unknown DIBuilder Opcode");
560 // Now attach the location information to the DIE.
561 addBlock(Die, Attribute, Block);
564 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
565 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
566 gives the variable VarName either the struct, or a pointer to the struct, as
567 its type. This is necessary for various behind-the-scenes things the
568 compiler needs to do with by-reference variables in Blocks.
570 However, as far as the original *programmer* is concerned, the variable
571 should still have type 'SomeType', as originally declared.
573 The function getBlockByrefType dives into the __Block_byref_x_VarName
574 struct to find the original type of the variable, which is then assigned to
575 the variable's Debug Information Entry as its real type. So far, so good.
576 However now the debugger will expect the variable VarName to have the type
577 SomeType. So we need the location attribute for the variable to be an
578 expression that explains to the debugger how to navigate through the
579 pointers and struct to find the actual variable of type SomeType.
581 The following function does just that. We start by getting
582 the "normal" location for the variable. This will be the location
583 of either the struct __Block_byref_x_VarName or the pointer to the
584 struct __Block_byref_x_VarName.
586 The struct will look something like:
588 struct __Block_byref_x_VarName {
590 struct __Block_byref_x_VarName *forwarding;
591 ... <various other fields>
593 ... <maybe more fields>
596 If we are given the struct directly (as our starting point) we
597 need to tell the debugger to:
599 1). Add the offset of the forwarding field.
601 2). Follow that pointer to get the real __Block_byref_x_VarName
602 struct to use (the real one may have been copied onto the heap).
604 3). Add the offset for the field VarName, to find the actual variable.
606 If we started with a pointer to the struct, then we need to
607 dereference that pointer first, before the other steps.
608 Translating this into DWARF ops, we will need to append the following
609 to the current location description for the variable:
611 DW_OP_deref -- optional, if we start with a pointer
612 DW_OP_plus_uconst <forward_fld_offset>
614 DW_OP_plus_uconst <varName_fld_offset>
616 That is what this function does. */
618 /// addBlockByrefAddress - Start with the address based on the location
619 /// provided, and generate the DWARF information necessary to find the
620 /// actual Block variable (navigating the Block struct) based on the
621 /// starting location. Add the DWARF information to the die. For
622 /// more information, read large comment just above here.
624 void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die,
625 dwarf::Attribute Attribute,
626 const MachineLocation &Location) {
627 DIType Ty = DV.getType();
629 uint16_t Tag = Ty.getTag();
630 bool isPointer = false;
632 StringRef varName = DV.getName();
634 if (Tag == dwarf::DW_TAG_pointer_type) {
635 DIDerivedType DTy(Ty);
636 TmpTy = resolve(DTy.getTypeDerivedFrom());
640 DICompositeType blockStruct(TmpTy);
642 // Find the __forwarding field and the variable field in the __Block_byref
644 DIArray Fields = blockStruct.getTypeArray();
645 DIDerivedType varField;
646 DIDerivedType forwardingField;
648 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
649 DIDerivedType DT(Fields.getElement(i));
650 StringRef fieldName = DT.getName();
651 if (fieldName == "__forwarding")
652 forwardingField = DT;
653 else if (fieldName == varName)
657 // Get the offsets for the forwarding field and the variable field.
658 unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3;
659 unsigned varFieldOffset = varField.getOffsetInBits() >> 2;
661 // Decode the original location, and use that as the start of the byref
662 // variable's location.
663 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
665 if (Location.isReg())
666 addRegisterOp(Block, Location.getReg());
668 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
670 // If we started with a pointer to the __Block_byref... struct, then
671 // the first thing we need to do is dereference the pointer (DW_OP_deref).
673 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
675 // Next add the offset for the '__forwarding' field:
676 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
677 // adding the offset if it's 0.
678 if (forwardingFieldOffset > 0) {
679 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
680 addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset);
683 // Now dereference the __forwarding field to get to the real __Block_byref
684 // struct: DW_OP_deref.
685 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
687 // Now that we've got the real __Block_byref... struct, add the offset
688 // for the variable's field to get to the location of the actual variable:
689 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
690 if (varFieldOffset > 0) {
691 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
692 addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset);
695 // Now attach the location information to the DIE.
696 addBlock(Die, Attribute, Block);
699 /// isTypeSigned - Return true if the type is signed.
700 static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) {
701 if (Ty.isDerivedType())
702 return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()),
704 if (Ty.isBasicType())
705 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed ||
706 DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
707 *SizeInBits = Ty.getSizeInBits();
713 /// Return true if type encoding is unsigned.
714 static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) {
715 DIDerivedType DTy(Ty);
716 if (DTy.isDerivedType())
717 return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom()));
720 if (BTy.isBasicType()) {
721 unsigned Encoding = BTy.getEncoding();
722 if (Encoding == dwarf::DW_ATE_unsigned ||
723 Encoding == dwarf::DW_ATE_unsigned_char ||
724 Encoding == dwarf::DW_ATE_boolean)
730 /// If this type is derived from a base type then return base type size.
731 static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) {
732 unsigned Tag = Ty.getTag();
734 if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
735 Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
736 Tag != dwarf::DW_TAG_restrict_type)
737 return Ty.getSizeInBits();
739 DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom());
741 // If this type is not derived from any type then take conservative approach.
742 if (!BaseType.isValid())
743 return Ty.getSizeInBits();
745 // If this is a derived type, go ahead and get the base type, unless it's a
746 // reference then it's just the size of the field. Pointer types have no need
747 // of this since they're a different type of qualification on the type.
748 if (BaseType.getTag() == dwarf::DW_TAG_reference_type ||
749 BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type)
750 return Ty.getSizeInBits();
752 if (BaseType.isDerivedType())
753 return getBaseTypeSize(DD, DIDerivedType(BaseType));
755 return BaseType.getSizeInBits();
758 /// addConstantValue - Add constant value entry in variable DIE.
759 void DwarfUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
761 // FIXME: This is a bit conservative/simple - it emits negative values at
762 // their maximum bit width which is a bit unfortunate (& doesn't prefer
763 // udata/sdata over dataN as suggested by the DWARF spec)
764 assert(MO.isImm() && "Invalid machine operand!");
766 bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits);
769 // If we're a signed constant definitely use sdata.
770 if (SignedConstant) {
771 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm());
775 // Else use data for now unless it's larger than we can deal with.
776 switch (SizeInBits) {
778 Form = dwarf::DW_FORM_data1;
781 Form = dwarf::DW_FORM_data2;
784 Form = dwarf::DW_FORM_data4;
787 Form = dwarf::DW_FORM_data8;
790 Form = dwarf::DW_FORM_udata;
791 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
794 addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm());
797 /// addConstantFPValue - Add constant value entry in variable DIE.
798 void DwarfUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
799 assert(MO.isFPImm() && "Invalid machine operand!");
800 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
801 APFloat FPImm = MO.getFPImm()->getValueAPF();
803 // Get the raw data form of the floating point.
804 const APInt FltVal = FPImm.bitcastToAPInt();
805 const char *FltPtr = (const char *)FltVal.getRawData();
807 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
808 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
809 int Incr = (LittleEndian ? 1 : -1);
810 int Start = (LittleEndian ? 0 : NumBytes - 1);
811 int Stop = (LittleEndian ? NumBytes : -1);
813 // Output the constant to DWARF one byte at a time.
814 for (; Start != Stop; Start += Incr)
815 addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
817 addBlock(Die, dwarf::DW_AT_const_value, Block);
820 /// addConstantFPValue - Add constant value entry in variable DIE.
821 void DwarfUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
822 // Pass this down to addConstantValue as an unsigned bag of bits.
823 addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
826 /// addConstantValue - Add constant value entry in variable DIE.
827 void DwarfUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
829 addConstantValue(Die, CI->getValue(), Unsigned);
832 // addConstantValue - Add constant value entry in variable DIE.
833 void DwarfUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) {
834 unsigned CIBitWidth = Val.getBitWidth();
835 if (CIBitWidth <= 64) {
836 // If we're a signed constant definitely use sdata.
838 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
843 // Else use data for now unless it's larger than we can deal with.
845 switch (CIBitWidth) {
847 Form = dwarf::DW_FORM_data1;
850 Form = dwarf::DW_FORM_data2;
853 Form = dwarf::DW_FORM_data4;
856 Form = dwarf::DW_FORM_data8;
859 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
863 addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue());
867 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
869 // Get the raw data form of the large APInt.
870 const uint64_t *Ptr64 = Val.getRawData();
872 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
873 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
875 // Output the constant to DWARF one byte at a time.
876 for (int i = 0; i < NumBytes; i++) {
879 c = Ptr64[i / 8] >> (8 * (i & 7));
881 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
882 addUInt(Block, dwarf::DW_FORM_data1, c);
885 addBlock(Die, dwarf::DW_AT_const_value, Block);
888 /// addTemplateParams - Add template parameters into buffer.
889 void DwarfUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
890 // Add template parameters.
891 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
892 DIDescriptor Element = TParams.getElement(i);
893 if (Element.isTemplateTypeParameter())
894 constructTemplateTypeParameterDIE(Buffer,
895 DITemplateTypeParameter(Element));
896 else if (Element.isTemplateValueParameter())
897 constructTemplateValueParameterDIE(Buffer,
898 DITemplateValueParameter(Element));
902 /// getOrCreateContextDIE - Get context owner's DIE.
903 DIE *DwarfUnit::getOrCreateContextDIE(DIScope Context) {
904 if (!Context || Context.isFile())
906 if (Context.isType())
907 return getOrCreateTypeDIE(DIType(Context));
908 if (Context.isNameSpace())
909 return getOrCreateNameSpace(DINameSpace(Context));
910 if (Context.isSubprogram())
911 return getOrCreateSubprogramDIE(DISubprogram(Context));
912 return getDIE(Context);
915 DIE *DwarfUnit::createTypeDIE(DICompositeType Ty) {
916 DIScope Context = resolve(Ty.getContext());
917 DIE *ContextDIE = getOrCreateContextDIE(Context);
919 DIE *TyDIE = getDIE(Ty);
924 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
926 constructTypeDIE(*TyDIE, Ty);
928 updateAcceleratorTables(Context, Ty, TyDIE);
932 /// Return true if the type is appropriately scoped to be contained inside
933 /// its own type unit.
934 static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) {
935 DIScope Parent = DD->resolve(Ty.getContext());
937 // Don't generate a hash for anything scoped inside a function.
938 if (Parent.isSubprogram())
940 Parent = DD->resolve(Parent.getContext());
945 /// Return true if the type should be split out into a type unit.
946 static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) {
947 if (!GenerateTypeUnits)
950 uint16_t Tag = CTy.getTag();
953 case dwarf::DW_TAG_structure_type:
954 case dwarf::DW_TAG_union_type:
955 case dwarf::DW_TAG_enumeration_type:
956 case dwarf::DW_TAG_class_type:
957 // If this is a class, structure, union, or enumeration type
958 // that is a definition (not a declaration), and not scoped
959 // inside a function then separate this out as a type unit.
960 return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD);
966 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
968 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
975 // Construct the context before querying for the existence of the DIE in case
976 // such construction creates the DIE.
977 DIScope Context = resolve(Ty.getContext());
978 DIE *ContextDIE = getOrCreateContextDIE(Context);
981 DIE *TyDIE = getDIE(Ty);
986 TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty);
988 if (Ty.isBasicType())
989 constructTypeDIE(*TyDIE, DIBasicType(Ty));
990 else if (Ty.isCompositeType()) {
991 DICompositeType CTy(Ty);
992 if (shouldCreateTypeUnit(CTy, DD)) {
993 DD->addTypeUnitType(getLanguage(), TyDIE, CTy);
994 // Skip updating the accellerator tables since this is not the full type
997 constructTypeDIE(*TyDIE, CTy);
999 assert(Ty.isDerivedType() && "Unknown kind of DIType");
1000 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
1003 updateAcceleratorTables(Context, Ty, TyDIE);
1008 void DwarfUnit::updateAcceleratorTables(DIScope Context, DIType Ty,
1010 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
1011 bool IsImplementation = 0;
1012 if (Ty.isCompositeType()) {
1013 DICompositeType CT(Ty);
1014 // A runtime language of 0 actually means C/C++ and that any
1015 // non-negative value is some version of Objective-C/C++.
1016 IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete();
1018 unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
1019 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
1021 if (!Context || Context.isCompileUnit() || Context.isFile() ||
1022 Context.isNameSpace())
1023 GlobalTypes[getParentContextString(Context) + Ty.getName().str()] = TyDIE;
1027 /// addType - Add a new type attribute to the specified entity.
1028 void DwarfUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) {
1029 assert(Ty && "Trying to add a type that doesn't exist?");
1031 // Check for pre-existence.
1032 DIEEntry *Entry = getDIEEntry(Ty);
1033 // If it exists then use the existing value.
1035 addDIEEntry(Entity, Attribute, Entry);
1040 DIE *Buffer = getOrCreateTypeDIE(Ty);
1043 Entry = createDIEEntry(Buffer);
1044 insertDIEEntry(Ty, Entry);
1045 addDIEEntry(Entity, Attribute, Entry);
1048 // Accelerator table mutators - add each name along with its companion
1049 // DIE to the proper table while ensuring that the name that we're going
1050 // to reference is in the string table. We do this since the names we
1051 // add may not only be identical to the names in the DIE.
1052 void DwarfUnit::addAccelName(StringRef Name, const DIE *Die) {
1053 DU->getStringPoolEntry(Name);
1054 std::vector<const DIE *> &DIEs = AccelNames[Name];
1055 DIEs.push_back(Die);
1058 void DwarfUnit::addAccelObjC(StringRef Name, const DIE *Die) {
1059 DU->getStringPoolEntry(Name);
1060 std::vector<const DIE *> &DIEs = AccelObjC[Name];
1061 DIEs.push_back(Die);
1064 void DwarfUnit::addAccelNamespace(StringRef Name, const DIE *Die) {
1065 DU->getStringPoolEntry(Name);
1066 std::vector<const DIE *> &DIEs = AccelNamespace[Name];
1067 DIEs.push_back(Die);
1070 void DwarfUnit::addAccelType(StringRef Name,
1071 std::pair<const DIE *, unsigned> Die) {
1072 DU->getStringPoolEntry(Name);
1073 std::vector<std::pair<const DIE *, unsigned> > &DIEs = AccelTypes[Name];
1074 DIEs.push_back(Die);
1077 /// addGlobalName - Add a new global name to the compile unit.
1078 void DwarfUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) {
1079 std::string FullName = getParentContextString(Context) + Name.str();
1080 GlobalNames[FullName] = Die;
1083 /// getParentContextString - Walks the metadata parent chain in a language
1084 /// specific manner (using the compile unit language) and returns
1085 /// it as a string. This is done at the metadata level because DIEs may
1086 /// not currently have been added to the parent context and walking the
1087 /// DIEs looking for names is more expensive than walking the metadata.
1088 std::string DwarfUnit::getParentContextString(DIScope Context) const {
1092 // FIXME: Decide whether to implement this for non-C++ languages.
1093 if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
1097 SmallVector<DIScope, 1> Parents;
1098 while (!Context.isCompileUnit()) {
1099 Parents.push_back(Context);
1100 if (Context.getContext())
1101 Context = resolve(Context.getContext());
1103 // Structure, etc types will have a NULL context if they're at the top
1108 // Reverse iterate over our list to go from the outermost construct to the
1110 for (SmallVectorImpl<DIScope>::reverse_iterator I = Parents.rbegin(),
1114 StringRef Name = Ctx.getName();
1115 if (!Name.empty()) {
1123 /// constructTypeDIE - Construct basic type die from DIBasicType.
1124 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
1125 // Get core information.
1126 StringRef Name = BTy.getName();
1127 // Add name if not anonymous or intermediate type.
1129 addString(&Buffer, dwarf::DW_AT_name, Name);
1131 // An unspecified type only has a name attribute.
1132 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type)
1135 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1138 uint64_t Size = BTy.getSizeInBits() >> 3;
1139 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1142 /// constructTypeDIE - Construct derived type die from DIDerivedType.
1143 void DwarfUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
1144 // Get core information.
1145 StringRef Name = DTy.getName();
1146 uint64_t Size = DTy.getSizeInBits() >> 3;
1147 uint16_t Tag = Buffer.getTag();
1149 // Map to main type, void will not have a type.
1150 DIType FromTy = resolve(DTy.getTypeDerivedFrom());
1152 addType(&Buffer, FromTy);
1154 // Add name if not anonymous or intermediate type.
1156 addString(&Buffer, dwarf::DW_AT_name, Name);
1158 // Add size if non-zero (derived types might be zero-sized.)
1159 if (Size && Tag != dwarf::DW_TAG_pointer_type)
1160 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1162 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
1163 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1164 getOrCreateTypeDIE(resolve(DTy.getClassType())));
1165 // Add source line info if available and TyDesc is not a forward declaration.
1166 if (!DTy.isForwardDecl())
1167 addSourceLine(&Buffer, DTy);
1170 /// constructTypeDIE - Construct type DIE from DICompositeType.
1171 void DwarfUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
1172 // Add name if not anonymous or intermediate type.
1173 StringRef Name = CTy.getName();
1175 uint64_t Size = CTy.getSizeInBits() >> 3;
1176 uint16_t Tag = Buffer.getTag();
1179 case dwarf::DW_TAG_array_type:
1180 constructArrayTypeDIE(Buffer, CTy);
1182 case dwarf::DW_TAG_enumeration_type:
1183 constructEnumTypeDIE(Buffer, CTy);
1185 case dwarf::DW_TAG_subroutine_type: {
1186 // Add return type. A void return won't have a type.
1187 DIArray Elements = CTy.getTypeArray();
1188 DIType RTy(Elements.getElement(0));
1190 addType(&Buffer, RTy);
1192 bool isPrototyped = true;
1194 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1195 DIDescriptor Ty = Elements.getElement(i);
1196 if (Ty.isUnspecifiedParameter()) {
1197 createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
1198 isPrototyped = false;
1200 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
1201 addType(Arg, DIType(Ty));
1202 if (DIType(Ty).isArtificial())
1203 addFlag(Arg, dwarf::DW_AT_artificial);
1206 // Add prototype flag if we're dealing with a C language and the
1207 // function has been prototyped.
1208 uint16_t Language = getLanguage();
1210 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1211 Language == dwarf::DW_LANG_ObjC))
1212 addFlag(&Buffer, dwarf::DW_AT_prototyped);
1214 case dwarf::DW_TAG_structure_type:
1215 case dwarf::DW_TAG_union_type:
1216 case dwarf::DW_TAG_class_type: {
1217 // Add elements to structure type.
1218 DIArray Elements = CTy.getTypeArray();
1219 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1220 DIDescriptor Element = Elements.getElement(i);
1221 DIE *ElemDie = NULL;
1222 if (Element.isSubprogram()) {
1223 DISubprogram SP(Element);
1224 ElemDie = getOrCreateSubprogramDIE(SP);
1225 if (SP.isProtected())
1226 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1227 dwarf::DW_ACCESS_protected);
1228 else if (SP.isPrivate())
1229 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1230 dwarf::DW_ACCESS_private);
1232 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1233 dwarf::DW_ACCESS_public);
1234 if (SP.isExplicit())
1235 addFlag(ElemDie, dwarf::DW_AT_explicit);
1236 } else if (Element.isDerivedType()) {
1237 DIDerivedType DDTy(Element);
1238 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
1239 ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
1240 addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()),
1241 dwarf::DW_AT_friend);
1242 } else if (DDTy.isStaticMember()) {
1243 getOrCreateStaticMemberDIE(DDTy);
1245 constructMemberDIE(Buffer, DDTy);
1247 } else if (Element.isObjCProperty()) {
1248 DIObjCProperty Property(Element);
1249 ElemDie = createAndAddDIE(Property.getTag(), Buffer);
1250 StringRef PropertyName = Property.getObjCPropertyName();
1251 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
1252 addType(ElemDie, Property.getType());
1253 addSourceLine(ElemDie, Property);
1254 StringRef GetterName = Property.getObjCPropertyGetterName();
1255 if (!GetterName.empty())
1256 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
1257 StringRef SetterName = Property.getObjCPropertySetterName();
1258 if (!SetterName.empty())
1259 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
1260 unsigned PropertyAttributes = 0;
1261 if (Property.isReadOnlyObjCProperty())
1262 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1263 if (Property.isReadWriteObjCProperty())
1264 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1265 if (Property.isAssignObjCProperty())
1266 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1267 if (Property.isRetainObjCProperty())
1268 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1269 if (Property.isCopyObjCProperty())
1270 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1271 if (Property.isNonAtomicObjCProperty())
1272 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1273 if (PropertyAttributes)
1274 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
1275 PropertyAttributes);
1277 DIEEntry *Entry = getDIEEntry(Element);
1279 Entry = createDIEEntry(ElemDie);
1280 insertDIEEntry(Element, Entry);
1286 if (CTy.isAppleBlockExtension())
1287 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
1289 DICompositeType ContainingType(resolve(CTy.getContainingType()));
1291 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type,
1292 getOrCreateTypeDIE(ContainingType));
1294 if (CTy.isObjcClassComplete())
1295 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1297 // Add template parameters to a class, structure or union types.
1298 // FIXME: The support isn't in the metadata for this yet.
1299 if (Tag == dwarf::DW_TAG_class_type ||
1300 Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
1301 addTemplateParams(Buffer, CTy.getTemplateParams());
1309 // Add name if not anonymous or intermediate type.
1311 addString(&Buffer, dwarf::DW_AT_name, Name);
1313 if (Tag == dwarf::DW_TAG_enumeration_type ||
1314 Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
1315 Tag == dwarf::DW_TAG_union_type) {
1316 // Add size if non-zero (derived types might be zero-sized.)
1317 // TODO: Do we care about size for enum forward declarations?
1319 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size);
1320 else if (!CTy.isForwardDecl())
1321 // Add zero size if it is not a forward declaration.
1322 addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0);
1324 // If we're a forward decl, say so.
1325 if (CTy.isForwardDecl())
1326 addFlag(&Buffer, dwarf::DW_AT_declaration);
1328 // Add source line info if available.
1329 if (!CTy.isForwardDecl())
1330 addSourceLine(&Buffer, CTy);
1332 // No harm in adding the runtime language to the declaration.
1333 unsigned RLang = CTy.getRunTimeLang();
1335 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1340 /// constructTemplateTypeParameterDIE - Construct new DIE for the given
1341 /// DITemplateTypeParameter.
1342 void DwarfUnit::constructTemplateTypeParameterDIE(DIE &Buffer,
1343 DITemplateTypeParameter TP) {
1345 createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1346 // Add the type if it exists, it could be void and therefore no type.
1348 addType(ParamDIE, resolve(TP.getType()));
1349 if (!TP.getName().empty())
1350 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1353 /// constructTemplateValueParameterDIE - Construct new DIE for the given
1354 /// DITemplateValueParameter.
1356 DwarfUnit::constructTemplateValueParameterDIE(DIE &Buffer,
1357 DITemplateValueParameter VP) {
1358 DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer);
1360 // Add the type if there is one, template template and template parameter
1361 // packs will not have a type.
1362 if (VP.getTag() == dwarf::DW_TAG_template_value_parameter)
1363 addType(ParamDIE, resolve(VP.getType()));
1364 if (!VP.getName().empty())
1365 addString(ParamDIE, dwarf::DW_AT_name, VP.getName());
1366 if (Value *Val = VP.getValue()) {
1367 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val))
1368 addConstantValue(ParamDIE, CI,
1369 isUnsignedDIType(DD, resolve(VP.getType())));
1370 else if (GlobalValue *GV = dyn_cast<GlobalValue>(Val)) {
1371 // For declaration non-type template parameters (such as global values and
1373 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1374 addOpAddress(Block, Asm->getSymbol(GV));
1375 // Emit DW_OP_stack_value to use the address as the immediate value of the
1376 // parameter, rather than a pointer to it.
1377 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1378 addBlock(ParamDIE, dwarf::DW_AT_location, Block);
1379 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1380 assert(isa<MDString>(Val));
1381 addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1382 cast<MDString>(Val)->getString());
1383 } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1384 assert(isa<MDNode>(Val));
1385 DIArray A(cast<MDNode>(Val));
1386 addTemplateParams(*ParamDIE, A);
1391 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1392 DIE *DwarfUnit::getOrCreateNameSpace(DINameSpace NS) {
1393 // Construct the context before querying for the existence of the DIE in case
1394 // such construction creates the DIE.
1395 DIE *ContextDIE = getOrCreateContextDIE(NS.getContext());
1397 DIE *NDie = getDIE(NS);
1400 NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1402 if (!NS.getName().empty()) {
1403 addString(NDie, dwarf::DW_AT_name, NS.getName());
1404 addAccelNamespace(NS.getName(), NDie);
1405 addGlobalName(NS.getName(), NDie, NS.getContext());
1407 addAccelNamespace("(anonymous namespace)", NDie);
1408 addSourceLine(NDie, NS);
1412 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1413 DIE *DwarfUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1414 // Construct the context before querying for the existence of the DIE in case
1415 // such construction creates the DIE (as is the case for member function
1417 DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
1419 DIE *SPDie = getDIE(SP);
1423 DISubprogram SPDecl = SP.getFunctionDeclaration();
1424 if (SPDecl.isSubprogram())
1425 // Add subprogram definitions to the CU die directly.
1426 ContextDIE = UnitDie.get();
1428 // DW_TAG_inlined_subroutine may refer to this DIE.
1429 SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1431 DIE *DeclDie = NULL;
1432 if (SPDecl.isSubprogram())
1433 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1435 // Add function template parameters.
1436 addTemplateParams(*SPDie, SP.getTemplateParams());
1438 // If this DIE is going to refer declaration info using AT_specification
1439 // then there is no need to add other attributes.
1441 // Refer function declaration directly.
1442 addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie);
1447 // Add the linkage name if we have one.
1448 StringRef LinkageName = SP.getLinkageName();
1449 if (!LinkageName.empty())
1450 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1451 GlobalValue::getRealLinkageName(LinkageName));
1453 // Constructors and operators for anonymous aggregates do not have names.
1454 if (!SP.getName().empty())
1455 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1457 addSourceLine(SPDie, SP);
1459 // Add the prototype if we have a prototype and we have a C like
1461 uint16_t Language = getLanguage();
1462 if (SP.isPrototyped() &&
1463 (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1464 Language == dwarf::DW_LANG_ObjC))
1465 addFlag(SPDie, dwarf::DW_AT_prototyped);
1467 DICompositeType SPTy = SP.getType();
1468 assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type &&
1469 "the type of a subprogram should be a subroutine");
1471 DIArray Args = SPTy.getTypeArray();
1472 // Add a return type. If this is a type like a C/C++ void type we don't add a
1474 if (Args.getElement(0))
1475 addType(SPDie, DIType(Args.getElement(0)));
1477 unsigned VK = SP.getVirtuality();
1479 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1480 DIEBlock *Block = getDIEBlock();
1481 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1482 addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1483 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1484 ContainingTypeMap.insert(
1485 std::make_pair(SPDie, resolve(SP.getContainingType())));
1488 if (!SP.isDefinition()) {
1489 addFlag(SPDie, dwarf::DW_AT_declaration);
1491 // Add arguments. Do not add arguments for subprogram definition. They will
1492 // be handled while processing variables.
1493 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1494 DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
1495 DIType ATy(Args.getElement(i));
1497 if (ATy.isArtificial())
1498 addFlag(Arg, dwarf::DW_AT_artificial);
1502 if (SP.isArtificial())
1503 addFlag(SPDie, dwarf::DW_AT_artificial);
1505 if (!SP.isLocalToUnit())
1506 addFlag(SPDie, dwarf::DW_AT_external);
1508 if (SP.isOptimized())
1509 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1511 if (unsigned isa = Asm->getISAEncoding()) {
1512 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1518 // Return const expression if value is a GEP to access merged global
1520 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1521 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1522 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1523 if (!CE || CE->getNumOperands() != 3 ||
1524 CE->getOpcode() != Instruction::GetElementPtr)
1527 // First operand points to a global struct.
1528 Value *Ptr = CE->getOperand(0);
1529 if (!isa<GlobalValue>(Ptr) ||
1530 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1533 // Second operand is zero.
1534 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1535 if (!CI || !CI->isZero())
1538 // Third operand is offset.
1539 if (!isa<ConstantInt>(CE->getOperand(2)))
1545 /// createGlobalVariableDIE - create global variable DIE.
1546 void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) {
1547 // Check for pre-existence.
1551 if (!GV.isGlobalVariable())
1554 DIScope GVContext = GV.getContext();
1555 DIType GTy = GV.getType();
1557 // If this is a static data member definition, some attributes belong
1558 // to the declaration DIE.
1559 DIE *VariableDIE = NULL;
1560 bool IsStaticMember = false;
1561 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1562 if (SDMDecl.Verify()) {
1563 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1564 // We need the declaration DIE that is in the static member's class.
1565 VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
1566 IsStaticMember = true;
1569 // If this is not a static data member definition, create the variable
1570 // DIE and add the initial set of attributes to it.
1572 // Construct the context before querying for the existence of the DIE in
1573 // case such construction creates the DIE.
1574 DIE *ContextDIE = getOrCreateContextDIE(GVContext);
1577 VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV);
1579 // Add name and type.
1580 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1581 addType(VariableDIE, GTy);
1583 // Add scoping info.
1584 if (!GV.isLocalToUnit())
1585 addFlag(VariableDIE, dwarf::DW_AT_external);
1587 // Add line number info.
1588 addSourceLine(VariableDIE, GV);
1592 bool addToAccelTable = false;
1593 DIE *VariableSpecDIE = NULL;
1594 bool isGlobalVariable = GV.getGlobal() != NULL;
1595 if (isGlobalVariable) {
1596 addToAccelTable = true;
1597 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1598 const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
1599 if (GV.getGlobal()->isThreadLocal()) {
1600 // FIXME: Make this work with -gsplit-dwarf.
1601 unsigned PointerSize = Asm->getDataLayout().getPointerSize();
1602 assert((PointerSize == 4 || PointerSize == 8) &&
1603 "Add support for other sizes if necessary");
1604 const MCExpr *Expr =
1605 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym);
1606 // Based on GCC's support for TLS:
1607 if (!DD->useSplitDwarf()) {
1608 // 1) Start with a constNu of the appropriate pointer size
1609 addUInt(Block, dwarf::DW_FORM_data1,
1610 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
1611 // 2) containing the (relocated) offset of the TLS variable
1612 // within the module's TLS block.
1613 addExpr(Block, dwarf::DW_FORM_udata, Expr);
1615 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
1616 addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr));
1618 // 3) followed by a custom OP to make the debugger do a TLS lookup.
1619 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
1621 DD->addArangeLabel(SymbolCU(this, Sym));
1622 addOpAddress(Block, Sym);
1624 // Do not create specification DIE if context is either compile unit
1626 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1627 !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
1628 // Create specification DIE.
1629 VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *UnitDie);
1630 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE);
1631 addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block);
1632 // A static member's declaration is already flagged as such.
1633 if (!SDMDecl.Verify())
1634 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1636 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1638 // Add the linkage name.
1639 StringRef LinkageName = GV.getLinkageName();
1640 if (!LinkageName.empty())
1641 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1642 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1644 addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE
1646 dwarf::DW_AT_MIPS_linkage_name,
1647 GlobalValue::getRealLinkageName(LinkageName));
1648 } else if (const ConstantInt *CI =
1649 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
1650 // AT_const_value was added when the static member was created. To avoid
1651 // emitting AT_const_value multiple times, we only add AT_const_value when
1652 // it is not a static member.
1653 if (!IsStaticMember)
1654 addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy));
1655 } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) {
1656 addToAccelTable = true;
1657 // GV is a merged global.
1658 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1659 Value *Ptr = CE->getOperand(0);
1660 MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
1661 DD->addArangeLabel(SymbolCU(this, Sym));
1662 addOpAddress(Block, Sym);
1663 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1664 SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
1665 addUInt(Block, dwarf::DW_FORM_udata,
1666 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1667 addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1668 addBlock(VariableDIE, dwarf::DW_AT_location, Block);
1671 if (addToAccelTable) {
1672 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1673 addAccelName(GV.getName(), AddrDIE);
1675 // If the linkage name is different than the name, go ahead and output
1676 // that as well into the name table.
1677 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1678 addAccelName(GV.getLinkageName(), AddrDIE);
1681 if (!GV.isLocalToUnit())
1682 addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE,
1686 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1687 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1688 DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1689 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy);
1691 // The LowerBound value defines the lower bounds which is typically zero for
1692 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1693 // Count == -1 then the array is unbounded and we do not emit
1694 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1695 // Count == 0, then the array has zero elements in which case we do not emit
1697 int64_t LowerBound = SR.getLo();
1698 int64_t DefaultLowerBound = getDefaultLowerBound();
1699 int64_t Count = SR.getCount();
1701 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1702 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1704 if (Count != -1 && Count != 0)
1705 // FIXME: An unbounded array should reference the expression that defines
1707 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None,
1708 LowerBound + Count - 1);
1711 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1712 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) {
1714 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1716 // Emit the element type.
1717 addType(&Buffer, resolve(CTy.getTypeDerivedFrom()));
1719 // Get an anonymous type for index type.
1720 // FIXME: This type should be passed down from the front end
1721 // as different languages may have different sizes for indexes.
1722 DIE *IdxTy = getIndexTyDie();
1724 // Construct an anonymous type for index type.
1725 IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *UnitDie);
1726 addString(IdxTy, dwarf::DW_AT_name, "int");
1727 addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t));
1728 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1729 dwarf::DW_ATE_signed);
1730 setIndexTyDie(IdxTy);
1733 // Add subranges to array type.
1734 DIArray Elements = CTy.getTypeArray();
1735 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1736 DIDescriptor Element = Elements.getElement(i);
1737 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1738 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1742 /// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType.
1743 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) {
1744 DIArray Elements = CTy.getTypeArray();
1746 // Add enumerators to enumeration type.
1747 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1748 DIEnumerator Enum(Elements.getElement(i));
1749 if (Enum.isEnumerator()) {
1750 DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1751 StringRef Name = Enum.getName();
1752 addString(Enumerator, dwarf::DW_AT_name, Name);
1753 int64_t Value = Enum.getEnumValue();
1754 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1758 DIType DTy = resolve(CTy.getTypeDerivedFrom());
1760 addType(&Buffer, DTy);
1761 addFlag(&Buffer, dwarf::DW_AT_enum_class);
1765 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1767 void DwarfUnit::constructContainingTypeDIEs() {
1768 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1769 CE = ContainingTypeMap.end();
1771 DIE *SPDie = CI->first;
1772 DIDescriptor D(CI->second);
1775 DIE *NDie = getDIE(D);
1778 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie);
1782 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1783 DIE *DwarfUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) {
1784 StringRef Name = DV.getName();
1786 // Define variable debug information entry.
1787 DIE *VariableDie = new DIE(DV.getTag());
1788 DbgVariable *AbsVar = DV.getAbstractVariable();
1789 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1791 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE);
1794 addString(VariableDie, dwarf::DW_AT_name, Name);
1795 addSourceLine(VariableDie, DV.getVariable());
1796 addType(VariableDie, DV.getType());
1799 if (DV.isArtificial())
1800 addFlag(VariableDie, dwarf::DW_AT_artificial);
1802 if (isScopeAbstract) {
1803 DV.setDIE(VariableDie);
1807 // Add variable address.
1809 unsigned Offset = DV.getDotDebugLocOffset();
1810 if (Offset != ~0U) {
1811 addSectionLabel(VariableDie, dwarf::DW_AT_location,
1812 Asm->GetTempSymbol("debug_loc", Offset));
1813 DV.setDIE(VariableDie);
1817 // Check if variable is described by a DBG_VALUE instruction.
1818 if (const MachineInstr *DVInsn = DV.getMInsn()) {
1819 assert(DVInsn->getNumOperands() == 3);
1820 if (DVInsn->getOperand(0).isReg()) {
1821 const MachineOperand RegOp = DVInsn->getOperand(0);
1822 // If the second operand is an immediate, this is an indirect value.
1823 if (DVInsn->getOperand(1).isImm()) {
1824 MachineLocation Location(RegOp.getReg(),
1825 DVInsn->getOperand(1).getImm());
1826 addVariableAddress(DV, VariableDie, Location);
1827 } else if (RegOp.getReg())
1828 addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg()));
1829 } else if (DVInsn->getOperand(0).isImm())
1830 addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType());
1831 else if (DVInsn->getOperand(0).isFPImm())
1832 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1833 else if (DVInsn->getOperand(0).isCImm())
1834 addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(),
1835 isUnsignedDIType(DD, DV.getType()));
1837 DV.setDIE(VariableDie);
1840 // .. else use frame index.
1841 int FI = DV.getFrameIndex();
1843 unsigned FrameReg = 0;
1844 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1845 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1846 MachineLocation Location(FrameReg, Offset);
1847 addVariableAddress(DV, VariableDie, Location);
1851 DV.setDIE(VariableDie);
1855 /// constructMemberDIE - Construct member DIE from DIDerivedType.
1856 void DwarfUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) {
1857 DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer);
1858 StringRef Name = DT.getName();
1860 addString(MemberDie, dwarf::DW_AT_name, Name);
1862 addType(MemberDie, resolve(DT.getTypeDerivedFrom()));
1864 addSourceLine(MemberDie, DT);
1866 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1867 addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1869 if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) {
1871 // For C++, virtual base classes are not at fixed offset. Use following
1872 // expression to extract appropriate offset from vtable.
1873 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1875 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1876 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1877 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1878 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1879 addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1880 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1881 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1882 addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1884 addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1886 uint64_t Size = DT.getSizeInBits();
1887 uint64_t FieldSize = getBaseTypeSize(DD, DT);
1888 uint64_t OffsetInBytes;
1890 if (Size != FieldSize) {
1892 addUInt(MemberDie, dwarf::DW_AT_byte_size, None,
1893 getBaseTypeSize(DD, DT) >> 3);
1894 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits());
1896 uint64_t Offset = DT.getOffsetInBits();
1897 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1898 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1899 uint64_t FieldOffset = (HiMark - FieldSize);
1900 Offset -= FieldOffset;
1902 // Maybe we need to work from the other end.
1903 if (Asm->getDataLayout().isLittleEndian())
1904 Offset = FieldSize - (Offset + Size);
1905 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1907 // Here WD_AT_data_member_location points to the anonymous
1908 // field that includes this bit field.
1909 OffsetInBytes = FieldOffset >> 3;
1911 // This is not a bitfield.
1912 OffsetInBytes = DT.getOffsetInBits() >> 3;
1913 addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes);
1916 if (DT.isProtected())
1917 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1918 dwarf::DW_ACCESS_protected);
1919 else if (DT.isPrivate())
1920 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1921 dwarf::DW_ACCESS_private);
1922 // Otherwise C++ member and base classes are considered public.
1924 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1925 dwarf::DW_ACCESS_public);
1927 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1928 dwarf::DW_VIRTUALITY_virtual);
1930 // Objective-C properties.
1931 if (MDNode *PNode = DT.getObjCProperty())
1932 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1933 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1936 if (DT.isArtificial())
1937 addFlag(MemberDie, dwarf::DW_AT_artificial);
1940 /// getOrCreateStaticMemberDIE - Create new DIE for C++ static member.
1941 DIE *DwarfUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) {
1945 // Construct the context before querying for the existence of the DIE in case
1946 // such construction creates the DIE.
1947 DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext()));
1948 assert(dwarf::isType(ContextDIE->getTag()) &&
1949 "Static member should belong to a type.");
1951 DIE *StaticMemberDIE = getDIE(DT);
1952 if (StaticMemberDIE)
1953 return StaticMemberDIE;
1955 StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT);
1957 DIType Ty = resolve(DT.getTypeDerivedFrom());
1959 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1960 addType(StaticMemberDIE, Ty);
1961 addSourceLine(StaticMemberDIE, DT);
1962 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1963 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1965 // FIXME: We could omit private if the parent is a class_type, and
1966 // public if the parent is something else.
1967 if (DT.isProtected())
1968 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1969 dwarf::DW_ACCESS_protected);
1970 else if (DT.isPrivate())
1971 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1972 dwarf::DW_ACCESS_private);
1974 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1975 dwarf::DW_ACCESS_public);
1977 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1978 addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty));
1979 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1980 addConstantFPValue(StaticMemberDIE, CFP);
1982 return StaticMemberDIE;
1985 void DwarfUnit::emitHeader(const MCSection *ASection,
1986 const MCSymbol *ASectionSym) {
1987 Asm->OutStreamer.AddComment("DWARF version number");
1988 Asm->EmitInt16(DD->getDwarfVersion());
1989 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
1990 // We share one abbreviations table across all units so it's always at the
1991 // start of the section. Use a relocatable offset where needed to ensure
1992 // linking doesn't invalidate that offset.
1993 Asm->EmitSectionOffset(ASectionSym, ASectionSym);
1994 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1995 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());