1 //===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for constructing a dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dwarfdebug"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfAccelTable.h"
18 #include "DwarfDebug.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/DIBuilder.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Target/TargetFrameLowering.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetRegisterInfo.h"
34 /// CompileUnit - Compile unit constructor.
35 CompileUnit::CompileUnit(unsigned UID, unsigned L, DIE *D, const MDNode *N,
36 AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU)
37 : UniqueID(UID), Language(L), CUDie(D), Asm(A), DD(DW), DU(DWU),
38 IndexTyDie(0), DebugInfoOffset(0) {
39 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
43 /// ~CompileUnit - Destructor for compile unit.
44 CompileUnit::~CompileUnit() {
45 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
46 DIEBlocks[j]->~DIEBlock();
49 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
50 /// information entry.
51 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
52 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
56 /// getDefaultLowerBound - Return the default lower bound for an array. If the
57 /// DWARF version doesn't handle the language, return -1.
58 int64_t CompileUnit::getDefaultLowerBound() const {
63 case dwarf::DW_LANG_C89:
64 case dwarf::DW_LANG_C99:
65 case dwarf::DW_LANG_C:
66 case dwarf::DW_LANG_C_plus_plus:
67 case dwarf::DW_LANG_ObjC:
68 case dwarf::DW_LANG_ObjC_plus_plus:
71 case dwarf::DW_LANG_Fortran77:
72 case dwarf::DW_LANG_Fortran90:
73 case dwarf::DW_LANG_Fortran95:
76 // The languages below have valid values only if the DWARF version >= 4.
77 case dwarf::DW_LANG_Java:
78 case dwarf::DW_LANG_Python:
79 case dwarf::DW_LANG_UPC:
80 case dwarf::DW_LANG_D:
81 if (dwarf::DWARF_VERSION >= 4)
85 case dwarf::DW_LANG_Ada83:
86 case dwarf::DW_LANG_Ada95:
87 case dwarf::DW_LANG_Cobol74:
88 case dwarf::DW_LANG_Cobol85:
89 case dwarf::DW_LANG_Modula2:
90 case dwarf::DW_LANG_Pascal83:
91 case dwarf::DW_LANG_PLI:
92 if (dwarf::DWARF_VERSION >= 4)
100 /// addFlag - Add a flag that is true.
101 void CompileUnit::addFlag(DIE *Die, unsigned Attribute) {
102 if (!DD->useDarwinGDBCompat())
103 Die->addValue(Attribute, dwarf::DW_FORM_flag_present,
106 addUInt(Die, Attribute, dwarf::DW_FORM_flag, 1);
109 /// addUInt - Add an unsigned integer attribute data and value.
111 void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
112 unsigned Form, uint64_t Integer) {
113 if (!Form) Form = DIEInteger::BestForm(false, Integer);
114 DIEValue *Value = Integer == 1 ?
115 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
116 Die->addValue(Attribute, Form, Value);
119 /// addSInt - Add an signed integer attribute data and value.
121 void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
122 unsigned Form, int64_t Integer) {
123 if (!Form) Form = DIEInteger::BestForm(true, Integer);
124 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
125 Die->addValue(Attribute, Form, Value);
128 /// addString - Add a string attribute data and value. We always emit a
129 /// reference to the string pool instead of immediate strings so that DIEs have
130 /// more predictable sizes. In the case of split dwarf we emit an index
131 /// into another table which gets us the static offset into the string
133 void CompileUnit::addString(DIE *Die, unsigned Attribute, StringRef String) {
134 if (!DD->useSplitDwarf()) {
135 MCSymbol *Symb = DU->getStringPoolEntry(String);
137 if (Asm->needsRelocationsForDwarfStringPool())
138 Value = new (DIEValueAllocator) DIELabel(Symb);
140 MCSymbol *StringPool = DU->getStringPoolSym();
141 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
143 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
145 unsigned idx = DU->getStringPoolIndex(String);
146 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
147 Die->addValue(Attribute, dwarf::DW_FORM_GNU_str_index, Value);
151 /// addLocalString - Add a string attribute data and value. This is guaranteed
152 /// to be in the local string pool instead of indirected.
153 void CompileUnit::addLocalString(DIE *Die, unsigned Attribute,
155 MCSymbol *Symb = DU->getStringPoolEntry(String);
157 if (Asm->needsRelocationsForDwarfStringPool())
158 Value = new (DIEValueAllocator) DIELabel(Symb);
160 MCSymbol *StringPool = DU->getStringPoolSym();
161 Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool);
163 Die->addValue(Attribute, dwarf::DW_FORM_strp, Value);
166 /// addLabel - Add a Dwarf label attribute data and value.
168 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
169 const MCSymbol *Label) {
170 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
171 Die->addValue(Attribute, Form, Value);
174 /// addLabelAddress - Add a dwarf label attribute data and value using
175 /// DW_FORM_addr or DW_FORM_GNU_addr_index.
177 void CompileUnit::addLabelAddress(DIE *Die, unsigned Attribute,
179 if (!DD->useSplitDwarf()) {
181 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
182 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
184 DIEValue *Value = new (DIEValueAllocator) DIEInteger(0);
185 Die->addValue(Attribute, dwarf::DW_FORM_addr, Value);
188 unsigned idx = DU->getAddrPoolIndex(Label);
189 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
190 Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
194 /// addOpAddress - Add a dwarf op address data and value using the
195 /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
197 void CompileUnit::addOpAddress(DIE *Die, MCSymbol *Sym) {
199 if (!DD->useSplitDwarf()) {
200 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
201 addLabel(Die, 0, dwarf::DW_FORM_udata, Sym);
203 unsigned idx = DU->getAddrPoolIndex(Sym);
204 DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
205 addUInt(Die, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
206 Die->addValue(0, dwarf::DW_FORM_GNU_addr_index, Value);
210 /// addDelta - Add a label delta attribute data and value.
212 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
213 const MCSymbol *Hi, const MCSymbol *Lo) {
214 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
215 Die->addValue(Attribute, Form, Value);
218 /// addDIEEntry - Add a DIE attribute data and value.
220 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
222 Die->addValue(Attribute, Form, createDIEEntry(Entry));
225 /// addBlock - Add block data.
227 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
229 Block->ComputeSize(Asm);
230 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
231 Die->addValue(Attribute, Block->BestForm(), Block);
234 /// addSourceLine - Add location information to specified debug information
236 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
241 unsigned Line = V.getLineNumber();
244 unsigned FileID = DD->getOrCreateSourceID(V.getContext().getFilename(),
245 V.getContext().getDirectory(),
247 assert(FileID && "Invalid file id");
248 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
249 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
252 /// addSourceLine - Add location information to specified debug information
254 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
255 // Verify global variable.
259 unsigned Line = G.getLineNumber();
262 unsigned FileID = DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(),
264 assert(FileID && "Invalid file id");
265 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
266 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
269 /// addSourceLine - Add location information to specified debug information
271 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
272 // Verify subprogram.
276 // If the line number is 0, don't add it.
277 unsigned Line = SP.getLineNumber();
281 unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(),
282 SP.getDirectory(), getUniqueID());
283 assert(FileID && "Invalid file id");
284 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
285 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
288 /// addSourceLine - Add location information to specified debug information
290 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
295 unsigned Line = Ty.getLineNumber();
298 unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(),
299 Ty.getDirectory(), getUniqueID());
300 assert(FileID && "Invalid file id");
301 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
302 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
305 /// addSourceLine - Add location information to specified debug information
307 void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) {
312 unsigned Line = Ty.getLineNumber();
315 DIFile File = Ty.getFile();
316 unsigned FileID = DD->getOrCreateSourceID(File.getFilename(),
317 File.getDirectory(), getUniqueID());
318 assert(FileID && "Invalid file id");
319 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
320 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
323 /// addSourceLine - Add location information to specified debug information
325 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
330 unsigned Line = NS.getLineNumber();
333 StringRef FN = NS.getFilename();
335 unsigned FileID = DD->getOrCreateSourceID(FN, NS.getDirectory(),
337 assert(FileID && "Invalid file id");
338 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
339 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
342 /// addVariableAddress - Add DW_AT_location attribute for a
343 /// DbgVariable based on provided MachineLocation.
344 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
345 MachineLocation Location) {
346 if (DV->variableHasComplexAddress())
347 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
348 else if (DV->isBlockByrefVariable())
349 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
351 addAddress(Die, dwarf::DW_AT_location, Location);
354 /// addRegisterOp - Add register operand.
355 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
356 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
357 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
359 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
361 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
362 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
366 /// addRegisterOffset - Add register offset.
367 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
369 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
370 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
371 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
372 if (Reg == TRI->getFrameRegister(*Asm->MF))
373 // If variable offset is based in frame register then use fbreg.
374 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
376 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
378 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
379 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
381 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
384 /// addAddress - Add an address attribute to a die based on the location
386 void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
387 const MachineLocation &Location) {
388 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
390 if (Location.isReg())
391 addRegisterOp(Block, Location.getReg());
393 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
395 // Now attach the location information to the DIE.
396 addBlock(Die, Attribute, 0, Block);
399 /// addComplexAddress - Start with the address based on the location provided,
400 /// and generate the DWARF information necessary to find the actual variable
401 /// given the extra address information encoded in the DIVariable, starting from
402 /// the starting location. Add the DWARF information to the die.
404 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
406 const MachineLocation &Location) {
407 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
408 unsigned N = DV->getNumAddrElements();
410 if (Location.isReg()) {
411 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
412 // If first address element is OpPlus then emit
413 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
414 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
417 addRegisterOp(Block, Location.getReg());
420 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
423 uint64_t Element = DV->getAddrElement(i);
424 if (Element == DIBuilder::OpPlus) {
425 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
426 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
427 } else if (Element == DIBuilder::OpDeref) {
428 if (!Location.isReg())
429 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
430 } else llvm_unreachable("unknown DIBuilder Opcode");
433 // Now attach the location information to the DIE.
434 addBlock(Die, Attribute, 0, Block);
437 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
438 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
439 gives the variable VarName either the struct, or a pointer to the struct, as
440 its type. This is necessary for various behind-the-scenes things the
441 compiler needs to do with by-reference variables in Blocks.
443 However, as far as the original *programmer* is concerned, the variable
444 should still have type 'SomeType', as originally declared.
446 The function getBlockByrefType dives into the __Block_byref_x_VarName
447 struct to find the original type of the variable, which is then assigned to
448 the variable's Debug Information Entry as its real type. So far, so good.
449 However now the debugger will expect the variable VarName to have the type
450 SomeType. So we need the location attribute for the variable to be an
451 expression that explains to the debugger how to navigate through the
452 pointers and struct to find the actual variable of type SomeType.
454 The following function does just that. We start by getting
455 the "normal" location for the variable. This will be the location
456 of either the struct __Block_byref_x_VarName or the pointer to the
457 struct __Block_byref_x_VarName.
459 The struct will look something like:
461 struct __Block_byref_x_VarName {
463 struct __Block_byref_x_VarName *forwarding;
464 ... <various other fields>
466 ... <maybe more fields>
469 If we are given the struct directly (as our starting point) we
470 need to tell the debugger to:
472 1). Add the offset of the forwarding field.
474 2). Follow that pointer to get the real __Block_byref_x_VarName
475 struct to use (the real one may have been copied onto the heap).
477 3). Add the offset for the field VarName, to find the actual variable.
479 If we started with a pointer to the struct, then we need to
480 dereference that pointer first, before the other steps.
481 Translating this into DWARF ops, we will need to append the following
482 to the current location description for the variable:
484 DW_OP_deref -- optional, if we start with a pointer
485 DW_OP_plus_uconst <forward_fld_offset>
487 DW_OP_plus_uconst <varName_fld_offset>
489 That is what this function does. */
491 /// addBlockByrefAddress - Start with the address based on the location
492 /// provided, and generate the DWARF information necessary to find the
493 /// actual Block variable (navigating the Block struct) based on the
494 /// starting location. Add the DWARF information to the die. For
495 /// more information, read large comment just above here.
497 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
499 const MachineLocation &Location) {
500 DIType Ty = DV->getType();
502 unsigned Tag = Ty.getTag();
503 bool isPointer = false;
505 StringRef varName = DV->getName();
507 if (Tag == dwarf::DW_TAG_pointer_type) {
508 DIDerivedType DTy = DIDerivedType(Ty);
509 TmpTy = DTy.getTypeDerivedFrom();
513 DICompositeType blockStruct = DICompositeType(TmpTy);
515 // Find the __forwarding field and the variable field in the __Block_byref
517 DIArray Fields = blockStruct.getTypeArray();
518 DIDescriptor varField = DIDescriptor();
519 DIDescriptor forwardingField = DIDescriptor();
521 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
522 DIDescriptor Element = Fields.getElement(i);
523 DIDerivedType DT = DIDerivedType(Element);
524 StringRef fieldName = DT.getName();
525 if (fieldName == "__forwarding")
526 forwardingField = Element;
527 else if (fieldName == varName)
531 // Get the offsets for the forwarding field and the variable field.
532 unsigned forwardingFieldOffset =
533 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
534 unsigned varFieldOffset =
535 DIDerivedType(varField).getOffsetInBits() >> 3;
537 // Decode the original location, and use that as the start of the byref
538 // variable's location.
539 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
541 if (Location.isReg())
542 addRegisterOp(Block, Location.getReg());
544 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
546 // If we started with a pointer to the __Block_byref... struct, then
547 // the first thing we need to do is dereference the pointer (DW_OP_deref).
549 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
551 // Next add the offset for the '__forwarding' field:
552 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
553 // adding the offset if it's 0.
554 if (forwardingFieldOffset > 0) {
555 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
556 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
559 // Now dereference the __forwarding field to get to the real __Block_byref
560 // struct: DW_OP_deref.
561 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
563 // Now that we've got the real __Block_byref... struct, add the offset
564 // for the variable's field to get to the location of the actual variable:
565 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
566 if (varFieldOffset > 0) {
567 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
568 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
571 // Now attach the location information to the DIE.
572 addBlock(Die, Attribute, 0, Block);
575 /// isTypeSigned - Return true if the type is signed.
576 static bool isTypeSigned(DIType Ty, int *SizeInBits) {
577 if (Ty.isDerivedType())
578 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
579 if (Ty.isBasicType())
580 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
581 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
582 *SizeInBits = Ty.getSizeInBits();
588 /// addConstantValue - Add constant value entry in variable DIE.
589 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
591 assert(MO.isImm() && "Invalid machine operand!");
592 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
594 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
595 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
596 switch (SizeInBits) {
597 case 8: Form = dwarf::DW_FORM_data1; break;
598 case 16: Form = dwarf::DW_FORM_data2; break;
599 case 32: Form = dwarf::DW_FORM_data4; break;
600 case 64: Form = dwarf::DW_FORM_data8; break;
603 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
604 : addUInt(Block, 0, Form, MO.getImm());
606 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
610 /// addConstantFPValue - Add constant value entry in variable DIE.
611 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
612 assert (MO.isFPImm() && "Invalid machine operand!");
613 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
614 APFloat FPImm = MO.getFPImm()->getValueAPF();
616 // Get the raw data form of the floating point.
617 const APInt FltVal = FPImm.bitcastToAPInt();
618 const char *FltPtr = (const char*)FltVal.getRawData();
620 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
621 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
622 int Incr = (LittleEndian ? 1 : -1);
623 int Start = (LittleEndian ? 0 : NumBytes - 1);
624 int Stop = (LittleEndian ? NumBytes : -1);
626 // Output the constant to DWARF one byte at a time.
627 for (; Start != Stop; Start += Incr)
628 addUInt(Block, 0, dwarf::DW_FORM_data1,
629 (unsigned char)0xFF & FltPtr[Start]);
631 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
635 /// addConstantFPValue - Add constant value entry in variable DIE.
636 bool CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) {
637 return addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), false);
640 /// addConstantValue - Add constant value entry in variable DIE.
641 bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
643 return addConstantValue(Die, CI->getValue(), Unsigned);
646 // addConstantValue - Add constant value entry in variable DIE.
647 bool CompileUnit::addConstantValue(DIE *Die, const APInt &Val,
649 unsigned CIBitWidth = Val.getBitWidth();
650 if (CIBitWidth <= 64) {
652 switch (CIBitWidth) {
653 case 8: form = dwarf::DW_FORM_data1; break;
654 case 16: form = dwarf::DW_FORM_data2; break;
655 case 32: form = dwarf::DW_FORM_data4; break;
656 case 64: form = dwarf::DW_FORM_data8; break;
658 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
661 addUInt(Die, dwarf::DW_AT_const_value, form, Val.getZExtValue());
663 addSInt(Die, dwarf::DW_AT_const_value, form, Val.getSExtValue());
667 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
669 // Get the raw data form of the large APInt.
670 const uint64_t *Ptr64 = Val.getRawData();
672 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
673 bool LittleEndian = Asm->getDataLayout().isLittleEndian();
675 // Output the constant to DWARF one byte at a time.
676 for (int i = 0; i < NumBytes; i++) {
679 c = Ptr64[i / 8] >> (8 * (i & 7));
681 c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
682 addUInt(Block, 0, dwarf::DW_FORM_data1, c);
685 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
689 /// addTemplateParams - Add template parameters into buffer.
690 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
691 // Add template parameters.
692 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
693 DIDescriptor Element = TParams.getElement(i);
694 if (Element.isTemplateTypeParameter())
695 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
696 DITemplateTypeParameter(Element)));
697 else if (Element.isTemplateValueParameter())
698 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
699 DITemplateValueParameter(Element)));
703 /// getOrCreateContextDIE - Get context owner's DIE.
704 DIE *CompileUnit::getOrCreateContextDIE(DIDescriptor Context) {
705 if (Context.isType())
706 return getOrCreateTypeDIE(DIType(Context));
707 else if (Context.isNameSpace())
708 return getOrCreateNameSpace(DINameSpace(Context));
709 else if (Context.isSubprogram())
710 return getOrCreateSubprogramDIE(DISubprogram(Context));
712 return getDIE(Context);
715 /// addToContextOwner - Add Die into the list of its context owner's children.
716 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
717 if (DIE *ContextDIE = getOrCreateContextDIE(Context))
718 ContextDIE->addChild(Die);
723 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
725 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
729 DIE *TyDIE = getDIE(Ty);
734 TyDIE = new DIE(dwarf::DW_TAG_base_type);
735 insertDIE(Ty, TyDIE);
736 if (Ty.isBasicType())
737 constructTypeDIE(*TyDIE, DIBasicType(Ty));
738 else if (Ty.isCompositeType())
739 constructTypeDIE(*TyDIE, DICompositeType(Ty));
741 assert(Ty.isDerivedType() && "Unknown kind of DIType");
742 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
744 // If this is a named finished type then include it in the list of types
745 // for the accelerator tables.
746 if (!Ty.getName().empty() && !Ty.isForwardDecl()) {
747 bool IsImplementation = 0;
748 if (Ty.isCompositeType()) {
749 DICompositeType CT(Ty);
750 // A runtime language of 0 actually means C/C++ and that any
751 // non-negative value is some version of Objective-C/C++.
752 IsImplementation = (CT.getRunTimeLang() == 0) ||
753 CT.isObjcClassComplete();
755 unsigned Flags = IsImplementation ?
756 DwarfAccelTable::eTypeFlagClassIsImplementation : 0;
757 addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags));
760 addToContextOwner(TyDIE, Ty.getContext());
764 /// addType - Add a new type attribute to the specified entity.
765 void CompileUnit::addType(DIE *Entity, DIType Ty, unsigned Attribute) {
769 // Check for pre-existence.
770 DIEEntry *Entry = getDIEEntry(Ty);
771 // If it exists then use the existing value.
773 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
778 DIE *Buffer = getOrCreateTypeDIE(Ty);
781 Entry = createDIEEntry(Buffer);
782 insertDIEEntry(Ty, Entry);
783 Entity->addValue(Attribute, dwarf::DW_FORM_ref4, Entry);
785 // If this is a complete composite type then include it in the
786 // list of global types.
790 /// addGlobalType - Add a new global type to the compile unit.
792 void CompileUnit::addGlobalType(DIType Ty) {
793 DIDescriptor Context = Ty.getContext();
794 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
795 && (!Context || Context.isCompileUnit() || Context.isFile()
796 || Context.isNameSpace()))
797 if (DIEEntry *Entry = getDIEEntry(Ty))
798 GlobalTypes[Ty.getName()] = Entry->getEntry();
801 /// addPubTypes - Add type for pubtypes section.
802 void CompileUnit::addPubTypes(DISubprogram SP) {
803 DICompositeType SPTy = SP.getType();
804 unsigned SPTag = SPTy.getTag();
805 if (SPTag != dwarf::DW_TAG_subroutine_type)
808 DIArray Args = SPTy.getTypeArray();
809 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
810 DIType ATy(Args.getElement(i));
817 /// constructTypeDIE - Construct basic type die from DIBasicType.
818 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
819 // Get core information.
820 StringRef Name = BTy.getName();
821 // Add name if not anonymous or intermediate type.
823 addString(&Buffer, dwarf::DW_AT_name, Name);
825 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
826 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
827 // Unspecified types has only name, nothing else.
831 Buffer.setTag(dwarf::DW_TAG_base_type);
832 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
835 uint64_t Size = BTy.getSizeInBits() >> 3;
836 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
839 /// constructTypeDIE - Construct derived type die from DIDerivedType.
840 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
841 // Get core information.
842 StringRef Name = DTy.getName();
843 uint64_t Size = DTy.getSizeInBits() >> 3;
844 unsigned Tag = DTy.getTag();
846 // FIXME - Workaround for templates.
847 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
851 // Map to main type, void will not have a type.
852 DIType FromTy = DTy.getTypeDerivedFrom();
853 addType(&Buffer, FromTy);
855 // Add name if not anonymous or intermediate type.
857 addString(&Buffer, dwarf::DW_AT_name, Name);
859 // Add size if non-zero (derived types might be zero-sized.)
860 if (Size && Tag != dwarf::DW_TAG_pointer_type)
861 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
863 if (Tag == dwarf::DW_TAG_ptr_to_member_type)
864 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
865 getOrCreateTypeDIE(DTy.getClassType()));
866 // Add source line info if available and TyDesc is not a forward declaration.
867 if (!DTy.isForwardDecl())
868 addSourceLine(&Buffer, DTy);
871 /// constructTypeDIE - Construct type DIE from DICompositeType.
872 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
873 // Get core information.
874 StringRef Name = CTy.getName();
876 uint64_t Size = CTy.getSizeInBits() >> 3;
877 unsigned Tag = CTy.getTag();
881 case dwarf::DW_TAG_array_type:
882 constructArrayTypeDIE(Buffer, &CTy);
884 case dwarf::DW_TAG_enumeration_type: {
885 DIArray Elements = CTy.getTypeArray();
887 // Add enumerators to enumeration type.
888 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
890 DIDescriptor Enum(Elements.getElement(i));
891 if (Enum.isEnumerator()) {
892 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
893 Buffer.addChild(ElemDie);
896 DIType DTy = CTy.getTypeDerivedFrom();
898 addType(&Buffer, DTy);
899 addUInt(&Buffer, dwarf::DW_AT_enum_class, dwarf::DW_FORM_flag, 1);
903 case dwarf::DW_TAG_subroutine_type: {
905 DIArray Elements = CTy.getTypeArray();
906 DIDescriptor RTy = Elements.getElement(0);
907 addType(&Buffer, DIType(RTy));
909 bool isPrototyped = true;
911 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
912 DIDescriptor Ty = Elements.getElement(i);
913 if (Ty.isUnspecifiedParameter()) {
914 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
915 Buffer.addChild(Arg);
916 isPrototyped = false;
918 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
919 addType(Arg, DIType(Ty));
920 if (DIType(Ty).isArtificial())
921 addFlag(Arg, dwarf::DW_AT_artificial);
922 Buffer.addChild(Arg);
925 // Add prototype flag if we're dealing with a C language and the
926 // function has been prototyped.
928 (Language == dwarf::DW_LANG_C89 ||
929 Language == dwarf::DW_LANG_C99 ||
930 Language == dwarf::DW_LANG_ObjC))
931 addFlag(&Buffer, dwarf::DW_AT_prototyped);
934 case dwarf::DW_TAG_structure_type:
935 case dwarf::DW_TAG_union_type:
936 case dwarf::DW_TAG_class_type: {
937 // Add elements to structure type.
938 DIArray Elements = CTy.getTypeArray();
940 // A forward struct declared type may not have elements available.
941 unsigned N = Elements.getNumElements();
945 // Add elements to structure type.
946 for (unsigned i = 0; i < N; ++i) {
947 DIDescriptor Element = Elements.getElement(i);
949 if (Element.isSubprogram()) {
950 DISubprogram SP(Element);
951 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
952 if (SP.isProtected())
953 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
954 dwarf::DW_ACCESS_protected);
955 else if (SP.isPrivate())
956 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
957 dwarf::DW_ACCESS_private);
959 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
960 dwarf::DW_ACCESS_public);
962 addFlag(ElemDie, dwarf::DW_AT_explicit);
963 } else if (Element.isDerivedType()) {
964 DIDerivedType DDTy(Element);
965 if (DDTy.getTag() == dwarf::DW_TAG_friend) {
966 ElemDie = new DIE(dwarf::DW_TAG_friend);
967 addType(ElemDie, DDTy.getTypeDerivedFrom(), dwarf::DW_AT_friend);
968 } else if (DDTy.isStaticMember())
969 ElemDie = createStaticMemberDIE(DDTy);
971 ElemDie = createMemberDIE(DDTy);
972 } else if (Element.isObjCProperty()) {
973 DIObjCProperty Property(Element);
974 ElemDie = new DIE(Property.getTag());
975 StringRef PropertyName = Property.getObjCPropertyName();
976 addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
977 addType(ElemDie, Property.getType());
978 addSourceLine(ElemDie, Property);
979 StringRef GetterName = Property.getObjCPropertyGetterName();
980 if (!GetterName.empty())
981 addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
982 StringRef SetterName = Property.getObjCPropertySetterName();
983 if (!SetterName.empty())
984 addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
985 unsigned PropertyAttributes = 0;
986 if (Property.isReadOnlyObjCProperty())
987 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
988 if (Property.isReadWriteObjCProperty())
989 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
990 if (Property.isAssignObjCProperty())
991 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
992 if (Property.isRetainObjCProperty())
993 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
994 if (Property.isCopyObjCProperty())
995 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
996 if (Property.isNonAtomicObjCProperty())
997 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
998 if (PropertyAttributes)
999 addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1000 PropertyAttributes);
1002 DIEEntry *Entry = getDIEEntry(Element);
1004 Entry = createDIEEntry(ElemDie);
1005 insertDIEEntry(Element, Entry);
1009 Buffer.addChild(ElemDie);
1012 if (CTy.isAppleBlockExtension())
1013 addFlag(&Buffer, dwarf::DW_AT_APPLE_block);
1015 DICompositeType ContainingType = CTy.getContainingType();
1016 if (DIDescriptor(ContainingType).isCompositeType())
1017 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
1018 getOrCreateTypeDIE(DIType(ContainingType)));
1020 DIDescriptor Context = CTy.getContext();
1021 addToContextOwner(&Buffer, Context);
1024 if (CTy.isObjcClassComplete())
1025 addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
1027 // Add template parameters to a class, structure or union types.
1028 // FIXME: The support isn't in the metadata for this yet.
1029 if (Tag == dwarf::DW_TAG_class_type ||
1030 Tag == dwarf::DW_TAG_structure_type ||
1031 Tag == dwarf::DW_TAG_union_type)
1032 addTemplateParams(Buffer, CTy.getTemplateParams());
1040 // Add name if not anonymous or intermediate type.
1042 addString(&Buffer, dwarf::DW_AT_name, Name);
1044 if (Tag == dwarf::DW_TAG_enumeration_type ||
1045 Tag == dwarf::DW_TAG_class_type ||
1046 Tag == dwarf::DW_TAG_structure_type ||
1047 Tag == dwarf::DW_TAG_union_type) {
1048 // Add size if non-zero (derived types might be zero-sized.)
1049 // TODO: Do we care about size for enum forward declarations?
1051 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
1052 else if (!CTy.isForwardDecl())
1053 // Add zero size if it is not a forward declaration.
1054 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
1056 // If we're a forward decl, say so.
1057 if (CTy.isForwardDecl())
1058 addFlag(&Buffer, dwarf::DW_AT_declaration);
1060 // Add source line info if available.
1061 if (!CTy.isForwardDecl())
1062 addSourceLine(&Buffer, CTy);
1064 // No harm in adding the runtime language to the declaration.
1065 unsigned RLang = CTy.getRunTimeLang();
1067 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
1068 dwarf::DW_FORM_data1, RLang);
1072 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
1073 /// for the given DITemplateTypeParameter.
1075 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
1076 DIE *ParamDIE = getDIE(TP);
1080 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
1081 addType(ParamDIE, TP.getType());
1082 addString(ParamDIE, dwarf::DW_AT_name, TP.getName());
1086 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
1087 /// for the given DITemplateValueParameter.
1089 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV){
1090 DIE *ParamDIE = getDIE(TPV);
1094 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
1095 addType(ParamDIE, TPV.getType());
1096 if (!TPV.getName().empty())
1097 addString(ParamDIE, dwarf::DW_AT_name, TPV.getName());
1098 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
1103 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
1104 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
1105 DIE *NDie = getDIE(NS);
1108 NDie = new DIE(dwarf::DW_TAG_namespace);
1109 insertDIE(NS, NDie);
1110 if (!NS.getName().empty()) {
1111 addString(NDie, dwarf::DW_AT_name, NS.getName());
1112 addAccelNamespace(NS.getName(), NDie);
1114 addAccelNamespace("(anonymous namespace)", NDie);
1115 addSourceLine(NDie, NS);
1116 addToContextOwner(NDie, NS.getContext());
1120 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
1121 /// printer to not emit usual symbol prefix before the symbol name is used then
1122 /// return linkage name after skipping this special LLVM prefix.
1123 static StringRef getRealLinkageName(StringRef LinkageName) {
1125 if (LinkageName.startswith(StringRef(&One, 1)))
1126 return LinkageName.substr(1);
1130 /// getOrCreateSubprogramDIE - Create new DIE using SP.
1131 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
1132 DIE *SPDie = getDIE(SP);
1136 SPDie = new DIE(dwarf::DW_TAG_subprogram);
1138 // DW_TAG_inlined_subroutine may refer to this DIE.
1139 insertDIE(SP, SPDie);
1141 DISubprogram SPDecl = SP.getFunctionDeclaration();
1142 DIE *DeclDie = NULL;
1143 if (SPDecl.isSubprogram()) {
1144 DeclDie = getOrCreateSubprogramDIE(SPDecl);
1147 // Add to context owner.
1148 addToContextOwner(SPDie, SP.getContext());
1150 // Add function template parameters.
1151 addTemplateParams(*SPDie, SP.getTemplateParams());
1153 // Unfortunately this code needs to stay here instead of below the
1154 // AT_specification code in order to work around a bug in older
1155 // gdbs that requires the linkage name to resolve multiple template
1157 // TODO: Remove this set of code when we get rid of the old gdb
1159 StringRef LinkageName = SP.getLinkageName();
1160 if (!LinkageName.empty() && DD->useDarwinGDBCompat())
1161 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1162 getRealLinkageName(LinkageName));
1164 // If this DIE is going to refer declaration info using AT_specification
1165 // then there is no need to add other attributes.
1167 // Refer function declaration directly.
1168 addDIEEntry(SPDie, dwarf::DW_AT_specification, dwarf::DW_FORM_ref4,
1174 // Add the linkage name if we have one.
1175 if (!LinkageName.empty() && !DD->useDarwinGDBCompat())
1176 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
1177 getRealLinkageName(LinkageName));
1179 // Constructors and operators for anonymous aggregates do not have names.
1180 if (!SP.getName().empty())
1181 addString(SPDie, dwarf::DW_AT_name, SP.getName());
1183 addSourceLine(SPDie, SP);
1185 // Add the prototype if we have a prototype and we have a C like
1187 if (SP.isPrototyped() &&
1188 (Language == dwarf::DW_LANG_C89 ||
1189 Language == dwarf::DW_LANG_C99 ||
1190 Language == dwarf::DW_LANG_ObjC))
1191 addFlag(SPDie, dwarf::DW_AT_prototyped);
1194 DICompositeType SPTy = SP.getType();
1195 DIArray Args = SPTy.getTypeArray();
1196 unsigned SPTag = SPTy.getTag();
1198 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
1199 addType(SPDie, SPTy);
1201 addType(SPDie, DIType(Args.getElement(0)));
1203 unsigned VK = SP.getVirtuality();
1205 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1206 DIEBlock *Block = getDIEBlock();
1207 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1208 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
1209 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
1210 ContainingTypeMap.insert(std::make_pair(SPDie,
1211 SP.getContainingType()));
1214 if (!SP.isDefinition()) {
1215 addFlag(SPDie, dwarf::DW_AT_declaration);
1217 // Add arguments. Do not add arguments for subprogram definition. They will
1218 // be handled while processing variables.
1219 DICompositeType SPTy = SP.getType();
1220 DIArray Args = SPTy.getTypeArray();
1221 unsigned SPTag = SPTy.getTag();
1223 if (SPTag == dwarf::DW_TAG_subroutine_type)
1224 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1225 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
1226 DIType ATy = DIType(Args.getElement(i));
1228 if (ATy.isArtificial())
1229 addFlag(Arg, dwarf::DW_AT_artificial);
1230 SPDie->addChild(Arg);
1234 if (SP.isArtificial())
1235 addFlag(SPDie, dwarf::DW_AT_artificial);
1237 if (!SP.isLocalToUnit())
1238 addFlag(SPDie, dwarf::DW_AT_external);
1240 if (SP.isOptimized())
1241 addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1243 if (unsigned isa = Asm->getISAEncoding()) {
1244 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1250 // Return const expression if value is a GEP to access merged global
1252 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1253 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1254 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1255 if (!CE || CE->getNumOperands() != 3 ||
1256 CE->getOpcode() != Instruction::GetElementPtr)
1259 // First operand points to a global struct.
1260 Value *Ptr = CE->getOperand(0);
1261 if (!isa<GlobalValue>(Ptr) ||
1262 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1265 // Second operand is zero.
1266 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1267 if (!CI || !CI->isZero())
1270 // Third operand is offset.
1271 if (!isa<ConstantInt>(CE->getOperand(2)))
1277 /// createGlobalVariableDIE - create global variable DIE.
1278 void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
1279 // Check for pre-existence.
1283 DIGlobalVariable GV(N);
1287 DIDescriptor GVContext = GV.getContext();
1288 DIType GTy = GV.getType();
1290 // If this is a static data member definition, some attributes belong
1291 // to the declaration DIE.
1292 DIE *VariableDIE = NULL;
1293 bool IsStaticMember = false;
1294 DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
1295 if (SDMDecl.Verify()) {
1296 assert(SDMDecl.isStaticMember() && "Expected static member decl");
1297 // We need the declaration DIE that is in the static member's class.
1298 // But that class might not exist in the DWARF yet.
1299 // Creating the class will create the static member decl DIE.
1300 getOrCreateContextDIE(SDMDecl.getContext());
1301 VariableDIE = getDIE(SDMDecl);
1302 assert(VariableDIE && "Static member decl has no context?");
1303 IsStaticMember = true;
1306 // If this is not a static data member definition, create the variable
1307 // DIE and add the initial set of attributes to it.
1309 VariableDIE = new DIE(GV.getTag());
1311 insertDIE(N, VariableDIE);
1313 // Add name and type.
1314 addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
1315 addType(VariableDIE, GTy);
1317 // Add scoping info.
1318 if (!GV.isLocalToUnit()) {
1319 addFlag(VariableDIE, dwarf::DW_AT_external);
1320 addGlobalName(GV.getName(), VariableDIE);
1323 // Add line number info.
1324 addSourceLine(VariableDIE, GV);
1325 // Add to context owner.
1326 addToContextOwner(VariableDIE, GVContext);
1330 bool addToAccelTable = false;
1331 DIE *VariableSpecDIE = NULL;
1332 bool isGlobalVariable = GV.getGlobal() != NULL;
1333 if (isGlobalVariable) {
1334 addToAccelTable = true;
1335 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1336 addOpAddress(Block, Asm->Mang->getSymbol(GV.getGlobal()));
1337 // Do not create specification DIE if context is either compile unit
1339 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1340 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1341 // Create specification DIE.
1342 VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1343 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1344 dwarf::DW_FORM_ref4, VariableDIE);
1345 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1346 // A static member's declaration is already flagged as such.
1347 if (!SDMDecl.Verify())
1348 addFlag(VariableDIE, dwarf::DW_AT_declaration);
1349 addDie(VariableSpecDIE);
1351 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1353 // Add linkage name.
1354 StringRef LinkageName = GV.getLinkageName();
1355 if (!LinkageName.empty()) {
1356 // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
1357 // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
1359 addString(IsStaticMember && VariableSpecDIE ?
1360 VariableSpecDIE : VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1361 getRealLinkageName(LinkageName));
1362 // In compatibility mode with older gdbs we put the linkage name on both
1363 // the TAG_variable DIE and on the TAG_member DIE.
1364 if (IsStaticMember && VariableSpecDIE && DD->useDarwinGDBCompat())
1365 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1366 getRealLinkageName(LinkageName));
1368 } else if (const ConstantInt *CI =
1369 dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
1370 // AT_const_value was added when the static member was created. To avoid
1371 // emitting AT_const_value multiple times, we only add AT_const_value when
1372 // it is not a static member.
1373 if (!IsStaticMember)
1374 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1375 } else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1376 addToAccelTable = true;
1377 // GV is a merged global.
1378 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1379 Value *Ptr = CE->getOperand(0);
1380 addOpAddress(Block, Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1381 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1382 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
1383 addUInt(Block, 0, dwarf::DW_FORM_udata,
1384 Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
1385 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1386 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1389 if (addToAccelTable) {
1390 DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
1391 addAccelName(GV.getName(), AddrDIE);
1393 // If the linkage name is different than the name, go ahead and output
1394 // that as well into the name table.
1395 if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
1396 addAccelName(GV.getLinkageName(), AddrDIE);
1402 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1403 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR,
1405 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1406 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1408 // The LowerBound value defines the lower bounds which is typically zero for
1409 // C/C++. The Count value is the number of elements. Values are 64 bit. If
1410 // Count == -1 then the array is unbounded and we do not emit
1411 // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and
1412 // Count == 0, then the array has zero elements in which case we do not emit
1414 int64_t LowerBound = SR.getLo();
1415 int64_t DefaultLowerBound = getDefaultLowerBound();
1416 int64_t Count = SR.getCount();
1418 if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1419 addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, LowerBound);
1421 if (Count != -1 && Count != 0)
1422 // FIXME: An unbounded array should reference the expression that defines
1424 addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, LowerBound + Count - 1);
1426 Buffer.addChild(DW_Subrange);
1429 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1430 void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1431 DICompositeType *CTy) {
1432 Buffer.setTag(dwarf::DW_TAG_array_type);
1433 if (CTy->isVector())
1434 addFlag(&Buffer, dwarf::DW_AT_GNU_vector);
1436 // Emit derived type.
1437 addType(&Buffer, CTy->getTypeDerivedFrom());
1438 DIArray Elements = CTy->getTypeArray();
1440 // Get an anonymous type for index type.
1441 // FIXME: This type should be passed down from the front end
1442 // as different languages may have different sizes for indexes.
1443 DIE *IdxTy = getIndexTyDie();
1445 // Construct an anonymous type for index type.
1446 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1447 addString(IdxTy, dwarf::DW_AT_name, "int");
1448 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1449 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1450 dwarf::DW_ATE_signed);
1452 setIndexTyDie(IdxTy);
1455 // Add subranges to array type.
1456 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1457 DIDescriptor Element = Elements.getElement(i);
1458 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1459 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1463 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1464 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1465 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1466 StringRef Name = ETy.getName();
1467 addString(Enumerator, dwarf::DW_AT_name, Name);
1468 int64_t Value = ETy.getEnumValue();
1469 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1473 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1475 void CompileUnit::constructContainingTypeDIEs() {
1476 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1477 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1478 DIE *SPDie = CI->first;
1479 const MDNode *N = CI->second;
1481 DIE *NDie = getDIE(N);
1482 if (!NDie) continue;
1483 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1487 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1488 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1489 StringRef Name = DV->getName();
1491 // Translate tag to proper Dwarf tag.
1492 unsigned Tag = DV->getTag();
1494 // Define variable debug information entry.
1495 DIE *VariableDie = new DIE(Tag);
1496 DbgVariable *AbsVar = DV->getAbstractVariable();
1497 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1499 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1500 dwarf::DW_FORM_ref4, AbsDIE);
1502 addString(VariableDie, dwarf::DW_AT_name, Name);
1503 addSourceLine(VariableDie, DV->getVariable());
1504 addType(VariableDie, DV->getType());
1507 if (DV->isArtificial())
1508 addFlag(VariableDie, dwarf::DW_AT_artificial);
1510 if (isScopeAbstract) {
1511 DV->setDIE(VariableDie);
1515 // Add variable address.
1517 unsigned Offset = DV->getDotDebugLocOffset();
1518 if (Offset != ~0U) {
1519 addLabel(VariableDie, dwarf::DW_AT_location,
1520 dwarf::DW_FORM_data4,
1521 Asm->GetTempSymbol("debug_loc", Offset));
1522 DV->setDIE(VariableDie);
1526 // Check if variable is described by a DBG_VALUE instruction.
1527 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1528 bool updated = false;
1529 if (DVInsn->getNumOperands() == 3) {
1530 if (DVInsn->getOperand(0).isReg()) {
1531 const MachineOperand RegOp = DVInsn->getOperand(0);
1532 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1533 if (DVInsn->getOperand(1).isImm() &&
1534 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1535 unsigned FrameReg = 0;
1536 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1538 TFI->getFrameIndexReference(*Asm->MF,
1539 DVInsn->getOperand(1).getImm(),
1541 MachineLocation Location(FrameReg, Offset);
1542 addVariableAddress(DV, VariableDie, Location);
1544 } else if (RegOp.getReg())
1545 addVariableAddress(DV, VariableDie,
1546 MachineLocation(RegOp.getReg()));
1549 else if (DVInsn->getOperand(0).isImm())
1551 addConstantValue(VariableDie, DVInsn->getOperand(0),
1553 else if (DVInsn->getOperand(0).isFPImm())
1555 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1556 else if (DVInsn->getOperand(0).isCImm())
1558 addConstantValue(VariableDie,
1559 DVInsn->getOperand(0).getCImm(),
1560 DV->getType().isUnsignedDIType());
1562 addVariableAddress(DV, VariableDie,
1563 Asm->getDebugValueLocation(DVInsn));
1567 // If variableDie is not updated then DBG_VALUE instruction does not
1568 // have valid variable info.
1572 DV->setDIE(VariableDie);
1575 // .. else use frame index.
1576 int FI = DV->getFrameIndex();
1578 unsigned FrameReg = 0;
1579 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1581 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1582 MachineLocation Location(FrameReg, Offset);
1583 addVariableAddress(DV, VariableDie, Location);
1587 DV->setDIE(VariableDie);
1591 /// createMemberDIE - Create new member DIE.
1592 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1593 DIE *MemberDie = new DIE(DT.getTag());
1594 StringRef Name = DT.getName();
1596 addString(MemberDie, dwarf::DW_AT_name, Name);
1598 addType(MemberDie, DT.getTypeDerivedFrom());
1600 addSourceLine(MemberDie, DT);
1602 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1603 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1605 uint64_t Size = DT.getSizeInBits();
1606 uint64_t FieldSize = DT.getOriginalTypeSize();
1608 if (Size != FieldSize) {
1610 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1611 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1613 uint64_t Offset = DT.getOffsetInBits();
1614 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1615 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1616 uint64_t FieldOffset = (HiMark - FieldSize);
1617 Offset -= FieldOffset;
1619 // Maybe we need to work from the other end.
1620 if (Asm->getDataLayout().isLittleEndian())
1621 Offset = FieldSize - (Offset + Size);
1622 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1624 // Here WD_AT_data_member_location points to the anonymous
1625 // field that includes this bit field.
1626 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1629 // This is not a bitfield.
1630 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1632 if (DT.getTag() == dwarf::DW_TAG_inheritance
1633 && DT.isVirtual()) {
1635 // For C++, virtual base classes are not at fixed offset. Use following
1636 // expression to extract appropriate offset from vtable.
1637 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1639 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1640 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1641 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1642 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1643 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1644 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1645 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1646 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1648 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1651 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1653 if (DT.isProtected())
1654 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1655 dwarf::DW_ACCESS_protected);
1656 else if (DT.isPrivate())
1657 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1658 dwarf::DW_ACCESS_private);
1659 // Otherwise C++ member and base classes are considered public.
1661 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1662 dwarf::DW_ACCESS_public);
1664 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1665 dwarf::DW_VIRTUALITY_virtual);
1667 // Objective-C properties.
1668 if (MDNode *PNode = DT.getObjCProperty())
1669 if (DIEEntry *PropertyDie = getDIEEntry(PNode))
1670 MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4,
1673 if (DT.isArtificial())
1674 addFlag(MemberDie, dwarf::DW_AT_artificial);
1679 /// createStaticMemberDIE - Create new DIE for C++ static member.
1680 DIE *CompileUnit::createStaticMemberDIE(const DIDerivedType DT) {
1684 DIE *StaticMemberDIE = new DIE(DT.getTag());
1685 DIType Ty = DT.getTypeDerivedFrom();
1687 addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName());
1688 addType(StaticMemberDIE, Ty);
1689 addSourceLine(StaticMemberDIE, DT);
1690 addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1691 addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1693 // FIXME: We could omit private if the parent is a class_type, and
1694 // public if the parent is something else.
1695 if (DT.isProtected())
1696 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1697 dwarf::DW_ACCESS_protected);
1698 else if (DT.isPrivate())
1699 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1700 dwarf::DW_ACCESS_private);
1702 addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1703 dwarf::DW_ACCESS_public);
1705 if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT.getConstant()))
1706 addConstantValue(StaticMemberDIE, CI, Ty.isUnsignedDIType());
1707 if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT.getConstant()))
1708 addConstantFPValue(StaticMemberDIE, CFP);
1710 insertDIE(DT, StaticMemberDIE);
1711 return StaticMemberDIE;