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 writing dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "dwarfdebug"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfDebug.h"
18 #include "llvm/Constants.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Analysis/DIBuilder.h"
22 #include "llvm/Target/Mangler.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetFrameLowering.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Target/TargetRegisterInfo.h"
27 #include "llvm/ADT/APFloat.h"
28 #include "llvm/Support/ErrorHandling.h"
32 /// CompileUnit - Compile unit constructor.
33 CompileUnit::CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW)
34 : ID(I), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) {
35 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
38 /// ~CompileUnit - Destructor for compile unit.
39 CompileUnit::~CompileUnit() {
40 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
41 DIEBlocks[j]->~DIEBlock();
44 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
45 /// information entry.
46 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
47 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
51 /// addUInt - Add an unsigned integer attribute data and value.
53 void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
54 unsigned Form, uint64_t Integer) {
55 if (!Form) Form = DIEInteger::BestForm(false, Integer);
56 DIEValue *Value = Integer == 1 ?
57 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
58 Die->addValue(Attribute, Form, Value);
61 /// addSInt - Add an signed integer attribute data and value.
63 void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
64 unsigned Form, int64_t Integer) {
65 if (!Form) Form = DIEInteger::BestForm(true, Integer);
66 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
67 Die->addValue(Attribute, Form, Value);
70 /// addString - Add a string attribute data and value. DIEString only
71 /// keeps string reference.
72 void CompileUnit::addString(DIE *Die, unsigned Attribute, unsigned Form,
74 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
75 Die->addValue(Attribute, Form, Value);
78 /// addLabel - Add a Dwarf label attribute data and value.
80 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
81 const MCSymbol *Label) {
82 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
83 Die->addValue(Attribute, Form, Value);
86 /// addDelta - Add a label delta attribute data and value.
88 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
89 const MCSymbol *Hi, const MCSymbol *Lo) {
90 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
91 Die->addValue(Attribute, Form, Value);
94 /// addDIEEntry - Add a DIE attribute data and value.
96 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
98 Die->addValue(Attribute, Form, createDIEEntry(Entry));
102 /// addBlock - Add block data.
104 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
106 Block->ComputeSize(Asm);
107 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
108 Die->addValue(Attribute, Block->BestForm(), Block);
111 /// addSourceLine - Add location information to specified debug information
113 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
118 unsigned Line = V.getLineNumber();
121 unsigned FileID = DD->GetOrCreateSourceID(V.getContext().getFilename(),
122 V.getContext().getDirectory());
123 assert(FileID && "Invalid file id");
124 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
125 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
128 /// addSourceLine - Add location information to specified debug information
130 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
131 // Verify global variable.
135 unsigned Line = G.getLineNumber();
138 unsigned FileID = DD->GetOrCreateSourceID(G.getFilename(),
140 assert(FileID && "Invalid file id");
141 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
142 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
145 /// addSourceLine - Add location information to specified debug information
147 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
148 // Verify subprogram.
151 // If the line number is 0, don't add it.
152 if (SP.getLineNumber() == 0)
155 unsigned Line = SP.getLineNumber();
156 if (!SP.getContext().Verify())
158 unsigned FileID = DD->GetOrCreateSourceID(SP.getFilename(), SP.getDirectory());
159 assert(FileID && "Invalid file id");
160 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
161 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
164 /// addSourceLine - Add location information to specified debug information
166 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
171 unsigned Line = Ty.getLineNumber();
172 if (Line == 0 || !Ty.getContext().Verify())
174 unsigned FileID = DD->GetOrCreateSourceID(Ty.getFilename(), Ty.getDirectory());
175 assert(FileID && "Invalid file id");
176 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
177 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
180 /// addSourceLine - Add location information to specified debug information
182 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
187 unsigned Line = NS.getLineNumber();
190 StringRef FN = NS.getFilename();
192 unsigned FileID = DD->GetOrCreateSourceID(FN, NS.getDirectory());
193 assert(FileID && "Invalid file id");
194 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
195 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
198 /// addVariableAddress - Add DW_AT_location attribute for a
199 /// DbgVariable based on provided MachineLocation.
200 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
201 MachineLocation Location) {
202 if (DV->variableHasComplexAddress())
203 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
204 else if (DV->isBlockByrefVariable())
205 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
207 addAddress(Die, dwarf::DW_AT_location, Location);
210 /// addRegisterOp - Add register operand.
211 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
212 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
213 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
215 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
217 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
218 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
222 /// addRegisterOffset - Add register offset.
223 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
225 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
226 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
227 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
228 if (Reg == TRI->getFrameRegister(*Asm->MF))
229 // If variable offset is based in frame register then use fbreg.
230 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
232 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
234 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
235 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
237 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
240 /// addAddress - Add an address attribute to a die based on the location
242 void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
243 const MachineLocation &Location) {
244 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
246 if (Location.isReg())
247 addRegisterOp(Block, Location.getReg());
249 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
251 // Now attach the location information to the DIE.
252 addBlock(Die, Attribute, 0, Block);
255 /// addComplexAddress - Start with the address based on the location provided,
256 /// and generate the DWARF information necessary to find the actual variable
257 /// given the extra address information encoded in the DIVariable, starting from
258 /// the starting location. Add the DWARF information to the die.
260 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
262 const MachineLocation &Location) {
263 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
264 unsigned N = DV->getNumAddrElements();
266 if (Location.isReg()) {
267 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
268 // If first address element is OpPlus then emit
269 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
270 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
273 addRegisterOp(Block, Location.getReg());
276 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
279 uint64_t Element = DV->getAddrElement(i);
280 if (Element == DIBuilder::OpPlus) {
281 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
282 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
283 } else if (Element == DIBuilder::OpDeref) {
284 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
285 } else llvm_unreachable("unknown DIBuilder Opcode");
288 // Now attach the location information to the DIE.
289 addBlock(Die, Attribute, 0, Block);
292 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
293 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
294 gives the variable VarName either the struct, or a pointer to the struct, as
295 its type. This is necessary for various behind-the-scenes things the
296 compiler needs to do with by-reference variables in Blocks.
298 However, as far as the original *programmer* is concerned, the variable
299 should still have type 'SomeType', as originally declared.
301 The function getBlockByrefType dives into the __Block_byref_x_VarName
302 struct to find the original type of the variable, which is then assigned to
303 the variable's Debug Information Entry as its real type. So far, so good.
304 However now the debugger will expect the variable VarName to have the type
305 SomeType. So we need the location attribute for the variable to be an
306 expression that explains to the debugger how to navigate through the
307 pointers and struct to find the actual variable of type SomeType.
309 The following function does just that. We start by getting
310 the "normal" location for the variable. This will be the location
311 of either the struct __Block_byref_x_VarName or the pointer to the
312 struct __Block_byref_x_VarName.
314 The struct will look something like:
316 struct __Block_byref_x_VarName {
318 struct __Block_byref_x_VarName *forwarding;
319 ... <various other fields>
321 ... <maybe more fields>
324 If we are given the struct directly (as our starting point) we
325 need to tell the debugger to:
327 1). Add the offset of the forwarding field.
329 2). Follow that pointer to get the real __Block_byref_x_VarName
330 struct to use (the real one may have been copied onto the heap).
332 3). Add the offset for the field VarName, to find the actual variable.
334 If we started with a pointer to the struct, then we need to
335 dereference that pointer first, before the other steps.
336 Translating this into DWARF ops, we will need to append the following
337 to the current location description for the variable:
339 DW_OP_deref -- optional, if we start with a pointer
340 DW_OP_plus_uconst <forward_fld_offset>
342 DW_OP_plus_uconst <varName_fld_offset>
344 That is what this function does. */
346 /// addBlockByrefAddress - Start with the address based on the location
347 /// provided, and generate the DWARF information necessary to find the
348 /// actual Block variable (navigating the Block struct) based on the
349 /// starting location. Add the DWARF information to the die. For
350 /// more information, read large comment just above here.
352 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
354 const MachineLocation &Location) {
355 DIType Ty = DV->getType();
357 unsigned Tag = Ty.getTag();
358 bool isPointer = false;
360 StringRef varName = DV->getName();
362 if (Tag == dwarf::DW_TAG_pointer_type) {
363 DIDerivedType DTy = DIDerivedType(Ty);
364 TmpTy = DTy.getTypeDerivedFrom();
368 DICompositeType blockStruct = DICompositeType(TmpTy);
370 // Find the __forwarding field and the variable field in the __Block_byref
372 DIArray Fields = blockStruct.getTypeArray();
373 DIDescriptor varField = DIDescriptor();
374 DIDescriptor forwardingField = DIDescriptor();
376 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
377 DIDescriptor Element = Fields.getElement(i);
378 DIDerivedType DT = DIDerivedType(Element);
379 StringRef fieldName = DT.getName();
380 if (fieldName == "__forwarding")
381 forwardingField = Element;
382 else if (fieldName == varName)
386 // Get the offsets for the forwarding field and the variable field.
387 unsigned forwardingFieldOffset =
388 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
389 unsigned varFieldOffset =
390 DIDerivedType(varField).getOffsetInBits() >> 3;
392 // Decode the original location, and use that as the start of the byref
393 // variable's location.
394 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
395 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
396 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
398 if (Location.isReg()) {
400 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
402 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
403 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
407 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
409 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
410 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
413 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
416 // If we started with a pointer to the __Block_byref... struct, then
417 // the first thing we need to do is dereference the pointer (DW_OP_deref).
419 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
421 // Next add the offset for the '__forwarding' field:
422 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
423 // adding the offset if it's 0.
424 if (forwardingFieldOffset > 0) {
425 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
426 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
429 // Now dereference the __forwarding field to get to the real __Block_byref
430 // struct: DW_OP_deref.
431 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
433 // Now that we've got the real __Block_byref... struct, add the offset
434 // for the variable's field to get to the location of the actual variable:
435 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
436 if (varFieldOffset > 0) {
437 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
438 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
441 // Now attach the location information to the DIE.
442 addBlock(Die, Attribute, 0, Block);
445 /// isTypeSigned - Return true if the type is signed.
446 static bool isTypeSigned(DIType Ty, int *SizeInBits) {
447 if (Ty.isDerivedType())
448 return isTypeSigned(DIDerivedType(Ty).getTypeDerivedFrom(), SizeInBits);
449 if (Ty.isBasicType())
450 if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed
451 || DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) {
452 *SizeInBits = Ty.getSizeInBits();
458 /// addConstantValue - Add constant value entry in variable DIE.
459 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
461 assert (MO.isImm() && "Invalid machine operand!");
462 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
464 bool SignedConstant = isTypeSigned(Ty, &SizeInBits);
465 unsigned Form = SignedConstant ? dwarf::DW_FORM_sdata : dwarf::DW_FORM_udata;
466 switch (SizeInBits) {
467 case 8: Form = dwarf::DW_FORM_data1; break;
468 case 16: Form = dwarf::DW_FORM_data2; break;
469 case 32: Form = dwarf::DW_FORM_data4; break;
470 case 64: Form = dwarf::DW_FORM_data8; break;
473 SignedConstant ? addSInt(Block, 0, Form, MO.getImm())
474 : addUInt(Block, 0, Form, MO.getImm());
476 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
480 /// addConstantFPValue - Add constant value entry in variable DIE.
481 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
482 assert (MO.isFPImm() && "Invalid machine operand!");
483 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
484 APFloat FPImm = MO.getFPImm()->getValueAPF();
486 // Get the raw data form of the floating point.
487 const APInt FltVal = FPImm.bitcastToAPInt();
488 const char *FltPtr = (const char*)FltVal.getRawData();
490 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
491 bool LittleEndian = Asm->getTargetData().isLittleEndian();
492 int Incr = (LittleEndian ? 1 : -1);
493 int Start = (LittleEndian ? 0 : NumBytes - 1);
494 int Stop = (LittleEndian ? NumBytes : -1);
496 // Output the constant to DWARF one byte at a time.
497 for (; Start != Stop; Start += Incr)
498 addUInt(Block, 0, dwarf::DW_FORM_data1,
499 (unsigned char)0xFF & FltPtr[Start]);
501 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
505 /// addConstantValue - Add constant value entry in variable DIE.
506 bool CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI,
508 unsigned CIBitWidth = CI->getBitWidth();
509 if (CIBitWidth <= 64) {
511 switch (CIBitWidth) {
512 case 8: form = dwarf::DW_FORM_data1; break;
513 case 16: form = dwarf::DW_FORM_data2; break;
514 case 32: form = dwarf::DW_FORM_data4; break;
515 case 64: form = dwarf::DW_FORM_data8; break;
517 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
520 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
522 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
526 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
528 // Get the raw data form of the large APInt.
529 const APInt Val = CI->getValue();
530 const char *Ptr = (const char*)Val.getRawData();
532 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
533 bool LittleEndian = Asm->getTargetData().isLittleEndian();
534 int Incr = (LittleEndian ? 1 : -1);
535 int Start = (LittleEndian ? 0 : NumBytes - 1);
536 int Stop = (LittleEndian ? NumBytes : -1);
538 // Output the constant to DWARF one byte at a time.
539 for (; Start != Stop; Start += Incr)
540 addUInt(Block, 0, dwarf::DW_FORM_data1,
541 (unsigned char)0xFF & Ptr[Start]);
543 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
547 /// addTemplateParams - Add template parameters in buffer.
548 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
549 // Add template parameters.
550 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
551 DIDescriptor Element = TParams.getElement(i);
552 if (Element.isTemplateTypeParameter())
553 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
554 DITemplateTypeParameter(Element)));
555 else if (Element.isTemplateValueParameter())
556 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
557 DITemplateValueParameter(Element)));
561 /// addToContextOwner - Add Die into the list of its context owner's children.
562 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
563 if (Context.isType()) {
564 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
565 ContextDIE->addChild(Die);
566 } else if (Context.isNameSpace()) {
567 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
568 ContextDIE->addChild(Die);
569 } else if (Context.isSubprogram()) {
570 DIE *ContextDIE = getOrCreateSubprogramDIE(DISubprogram(Context));
571 ContextDIE->addChild(Die);
572 } else if (DIE *ContextDIE = getDIE(Context))
573 ContextDIE->addChild(Die);
578 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
580 DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
584 DIE *TyDIE = getDIE(Ty);
589 TyDIE = new DIE(dwarf::DW_TAG_base_type);
590 insertDIE(Ty, TyDIE);
591 if (Ty.isBasicType())
592 constructTypeDIE(*TyDIE, DIBasicType(Ty));
593 else if (Ty.isCompositeType())
594 constructTypeDIE(*TyDIE, DICompositeType(Ty));
596 assert(Ty.isDerivedType() && "Unknown kind of DIType");
597 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
600 addToContextOwner(TyDIE, Ty.getContext());
604 /// addType - Add a new type attribute to the specified entity.
605 void CompileUnit::addType(DIE *Entity, DIType Ty) {
609 // Check for pre-existence.
610 DIEEntry *Entry = getDIEEntry(Ty);
611 // If it exists then use the existing value.
613 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
618 DIE *Buffer = getOrCreateTypeDIE(Ty);
621 Entry = createDIEEntry(Buffer);
622 insertDIEEntry(Ty, Entry);
623 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
625 // If this is a complete composite type then include it in the
626 // list of global types.
630 /// addGlobalType - Add a new global type to the compile unit.
632 void CompileUnit::addGlobalType(DIType Ty) {
633 DIDescriptor Context = Ty.getContext();
634 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
635 && (!Context || Context.isCompileUnit() || Context.isFile()
636 || Context.isNameSpace()))
637 if (DIEEntry *Entry = getDIEEntry(Ty))
638 GlobalTypes[Ty.getName()] = Entry->getEntry();
641 /// addPubTypes - Add type for pubtypes section.
642 void CompileUnit::addPubTypes(DISubprogram SP) {
643 DICompositeType SPTy = SP.getType();
644 unsigned SPTag = SPTy.getTag();
645 if (SPTag != dwarf::DW_TAG_subroutine_type)
648 DIArray Args = SPTy.getTypeArray();
649 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
650 DIType ATy(Args.getElement(i));
657 /// constructTypeDIE - Construct basic type die from DIBasicType.
658 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
659 // Get core information.
660 StringRef Name = BTy.getName();
661 // Add name if not anonymous or intermediate type.
663 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
665 if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) {
666 Buffer.setTag(dwarf::DW_TAG_unspecified_type);
667 // Unspecified types has only name, nothing else.
671 Buffer.setTag(dwarf::DW_TAG_base_type);
672 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
675 uint64_t Size = BTy.getSizeInBits() >> 3;
676 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
679 /// constructTypeDIE - Construct derived type die from DIDerivedType.
680 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
681 // Get core information.
682 StringRef Name = DTy.getName();
683 uint64_t Size = DTy.getSizeInBits() >> 3;
684 unsigned Tag = DTy.getTag();
686 // FIXME - Workaround for templates.
687 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
691 // Map to main type, void will not have a type.
692 DIType FromTy = DTy.getTypeDerivedFrom();
693 addType(&Buffer, FromTy);
695 // Add name if not anonymous or intermediate type.
697 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
699 // Add size if non-zero (derived types might be zero-sized.)
701 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
703 // Add source line info if available and TyDesc is not a forward declaration.
704 if (!DTy.isForwardDecl())
705 addSourceLine(&Buffer, DTy);
708 /// constructTypeDIE - Construct type DIE from DICompositeType.
709 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
710 // Get core information.
711 StringRef Name = CTy.getName();
713 uint64_t Size = CTy.getSizeInBits() >> 3;
714 unsigned Tag = CTy.getTag();
718 case dwarf::DW_TAG_vector_type:
719 case dwarf::DW_TAG_array_type:
720 constructArrayTypeDIE(Buffer, &CTy);
722 case dwarf::DW_TAG_enumeration_type: {
723 DIArray Elements = CTy.getTypeArray();
725 // Add enumerators to enumeration type.
726 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
728 DIDescriptor Enum(Elements.getElement(i));
729 if (Enum.isEnumerator()) {
730 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
731 Buffer.addChild(ElemDie);
736 case dwarf::DW_TAG_subroutine_type: {
738 DIArray Elements = CTy.getTypeArray();
739 DIDescriptor RTy = Elements.getElement(0);
740 addType(&Buffer, DIType(RTy));
742 bool isPrototyped = true;
744 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
745 DIDescriptor Ty = Elements.getElement(i);
746 if (Ty.isUnspecifiedParameter()) {
747 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
748 Buffer.addChild(Arg);
749 isPrototyped = false;
751 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
752 addType(Arg, DIType(Ty));
753 Buffer.addChild(Arg);
756 // Add prototype flag.
758 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
761 case dwarf::DW_TAG_structure_type:
762 case dwarf::DW_TAG_union_type:
763 case dwarf::DW_TAG_class_type: {
764 // Add elements to structure type.
765 DIArray Elements = CTy.getTypeArray();
767 // A forward struct declared type may not have elements available.
768 unsigned N = Elements.getNumElements();
772 // Add elements to structure type.
773 for (unsigned i = 0; i < N; ++i) {
774 DIDescriptor Element = Elements.getElement(i);
776 if (Element.isSubprogram()) {
777 DISubprogram SP(Element);
778 ElemDie = getOrCreateSubprogramDIE(DISubprogram(Element));
779 if (SP.isProtected())
780 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
781 dwarf::DW_ACCESS_protected);
782 else if (SP.isPrivate())
783 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
784 dwarf::DW_ACCESS_private);
786 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
787 dwarf::DW_ACCESS_public);
789 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
791 else if (Element.isVariable()) {
792 DIVariable DV(Element);
793 ElemDie = new DIE(dwarf::DW_TAG_variable);
794 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
796 addType(ElemDie, DV.getType());
797 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
798 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
799 addSourceLine(ElemDie, DV);
800 } else if (Element.isDerivedType())
801 ElemDie = createMemberDIE(DIDerivedType(Element));
804 Buffer.addChild(ElemDie);
807 if (CTy.isAppleBlockExtension())
808 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
810 unsigned RLang = CTy.getRunTimeLang();
812 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
813 dwarf::DW_FORM_data1, RLang);
815 DICompositeType ContainingType = CTy.getContainingType();
816 if (DIDescriptor(ContainingType).isCompositeType())
817 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
818 getOrCreateTypeDIE(DIType(ContainingType)));
820 DIDescriptor Context = CTy.getContext();
821 addToContextOwner(&Buffer, Context);
824 if (CTy.isObjcClassComplete())
825 addUInt(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type,
826 dwarf::DW_FORM_flag, 1);
828 if (Tag == dwarf::DW_TAG_class_type)
829 addTemplateParams(Buffer, CTy.getTemplateParams());
837 // Add name if not anonymous or intermediate type.
839 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
841 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
842 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
844 // Add size if non-zero (derived types might be zero-sized.)
846 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
848 // Add zero size if it is not a forward declaration.
849 if (CTy.isForwardDecl())
850 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
852 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
855 // Add source line info if available.
856 if (!CTy.isForwardDecl())
857 addSourceLine(&Buffer, CTy);
861 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
862 /// for the given DITemplateTypeParameter.
864 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
865 DIE *ParamDIE = getDIE(TP);
869 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
870 addType(ParamDIE, TP.getType());
871 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName());
875 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
876 /// for the given DITemplateValueParameter.
878 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
879 DIE *ParamDIE = getDIE(TPV);
883 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
884 addType(ParamDIE, TPV.getType());
885 if (!TPV.getName().empty())
886 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName());
887 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
892 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
893 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
894 DIE *NDie = getDIE(NS);
897 NDie = new DIE(dwarf::DW_TAG_namespace);
899 if (!NS.getName().empty())
900 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
901 addSourceLine(NDie, NS);
902 addToContextOwner(NDie, NS.getContext());
906 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
907 /// printer to not emit usual symbol prefix before the symbol name is used then
908 /// return linkage name after skipping this special LLVM prefix.
909 static StringRef getRealLinkageName(StringRef LinkageName) {
911 if (LinkageName.startswith(StringRef(&One, 1)))
912 return LinkageName.substr(1);
916 /// getOrCreateSubprogramDIE - Create new DIE using SP.
917 DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) {
918 DIE *SPDie = getDIE(SP);
922 SPDie = new DIE(dwarf::DW_TAG_subprogram);
924 // DW_TAG_inlined_subroutine may refer to this DIE.
925 insertDIE(SP, SPDie);
927 // Add to context owner.
928 addToContextOwner(SPDie, SP.getContext());
930 // Add function template parameters.
931 addTemplateParams(*SPDie, SP.getTemplateParams());
933 StringRef LinkageName = SP.getLinkageName();
934 if (!LinkageName.empty())
935 addString(SPDie, dwarf::DW_AT_MIPS_linkage_name,
936 dwarf::DW_FORM_string,
937 getRealLinkageName(LinkageName));
939 // If this DIE is going to refer declaration info using AT_specification
940 // then there is no need to add other attributes.
941 if (SP.getFunctionDeclaration().isSubprogram())
944 // Constructors and operators for anonymous aggregates do not have names.
945 if (!SP.getName().empty())
946 addString(SPDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
949 addSourceLine(SPDie, SP);
951 if (SP.isPrototyped())
952 addUInt(SPDie, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
955 DICompositeType SPTy = SP.getType();
956 DIArray Args = SPTy.getTypeArray();
957 unsigned SPTag = SPTy.getTag();
959 if (Args.getNumElements() == 0 || SPTag != dwarf::DW_TAG_subroutine_type)
960 addType(SPDie, SPTy);
962 addType(SPDie, DIType(Args.getElement(0)));
964 unsigned VK = SP.getVirtuality();
966 addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag, VK);
967 DIEBlock *Block = getDIEBlock();
968 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
969 addUInt(Block, 0, dwarf::DW_FORM_udata, SP.getVirtualIndex());
970 addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, 0, Block);
971 ContainingTypeMap.insert(std::make_pair(SPDie,
972 SP.getContainingType()));
975 if (!SP.isDefinition()) {
976 addUInt(SPDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
978 // Add arguments. Do not add arguments for subprogram definition. They will
979 // be handled while processing variables.
980 DICompositeType SPTy = SP.getType();
981 DIArray Args = SPTy.getTypeArray();
982 unsigned SPTag = SPTy.getTag();
984 if (SPTag == dwarf::DW_TAG_subroutine_type)
985 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
986 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
987 DIType ATy = DIType(DIType(Args.getElement(i)));
989 if (ATy.isArtificial())
990 addUInt(Arg, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
991 SPDie->addChild(Arg);
995 if (SP.isArtificial())
996 addUInt(SPDie, dwarf::DW_AT_artificial, dwarf::DW_FORM_flag, 1);
998 if (!SP.isLocalToUnit())
999 addUInt(SPDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1001 if (SP.isOptimized())
1002 addUInt(SPDie, dwarf::DW_AT_APPLE_optimized, dwarf::DW_FORM_flag, 1);
1004 if (unsigned isa = Asm->getISAEncoding()) {
1005 addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1011 // Return const expression if value is a GEP to access merged global
1013 // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
1014 static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
1015 const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
1016 if (!CE || CE->getNumOperands() != 3 ||
1017 CE->getOpcode() != Instruction::GetElementPtr)
1020 // First operand points to a global struct.
1021 Value *Ptr = CE->getOperand(0);
1022 if (!isa<GlobalValue>(Ptr) ||
1023 !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
1026 // Second operand is zero.
1027 const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
1028 if (!CI || !CI->isZero())
1031 // Third operand is offset.
1032 if (!isa<ConstantInt>(CE->getOperand(2)))
1038 /// createGlobalVariableDIE - create global variable DIE.
1039 void CompileUnit::createGlobalVariableDIE(const MDNode *N) {
1040 // Check for pre-existence.
1044 DIGlobalVariable GV(N);
1048 DIE *VariableDIE = new DIE(GV.getTag());
1050 insertDIE(N, VariableDIE);
1053 addString(VariableDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string,
1054 GV.getDisplayName());
1055 StringRef LinkageName = GV.getLinkageName();
1056 bool isGlobalVariable = GV.getGlobal() != NULL;
1057 if (!LinkageName.empty() && isGlobalVariable)
1058 addString(VariableDIE, dwarf::DW_AT_MIPS_linkage_name,
1059 dwarf::DW_FORM_string,
1060 getRealLinkageName(LinkageName));
1062 DIType GTy = GV.getType();
1063 addType(VariableDIE, GTy);
1065 // Add scoping info.
1066 if (!GV.isLocalToUnit()) {
1067 addUInt(VariableDIE, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
1068 // Expose as global.
1069 addGlobal(GV.getName(), VariableDIE);
1071 // Add line number info.
1072 addSourceLine(VariableDIE, GV);
1073 // Add to context owner.
1074 DIDescriptor GVContext = GV.getContext();
1075 addToContextOwner(VariableDIE, GVContext);
1077 if (isGlobalVariable) {
1078 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1079 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1080 addLabel(Block, 0, dwarf::DW_FORM_udata,
1081 Asm->Mang->getSymbol(GV.getGlobal()));
1082 // Do not create specification DIE if context is either compile unit
1084 if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
1085 !GVContext.isFile() && !isSubprogramContext(GVContext)) {
1086 // Create specification DIE.
1087 DIE *VariableSpecDIE = new DIE(dwarf::DW_TAG_variable);
1088 addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification,
1089 dwarf::DW_FORM_ref4, VariableDIE);
1090 addBlock(VariableSpecDIE, dwarf::DW_AT_location, 0, Block);
1091 addUInt(VariableDIE, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag,
1093 addDie(VariableSpecDIE);
1095 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1097 } else if (const ConstantInt *CI =
1098 dyn_cast_or_null<ConstantInt>(GV.getConstant()))
1099 addConstantValue(VariableDIE, CI, GTy.isUnsignedDIType());
1100 else if (const ConstantExpr *CE = getMergedGlobalExpr(N->getOperand(11))) {
1101 // GV is a merged global.
1102 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
1103 Value *Ptr = CE->getOperand(0);
1104 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
1105 addLabel(Block, 0, dwarf::DW_FORM_udata,
1106 Asm->Mang->getSymbol(cast<GlobalValue>(Ptr)));
1107 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1108 SmallVector<Value*, 3> Idx(CE->op_begin()+1, CE->op_end());
1109 addUInt(Block, 0, dwarf::DW_FORM_udata,
1110 Asm->getTargetData().getIndexedOffset(Ptr->getType(), Idx));
1111 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1112 addBlock(VariableDIE, dwarf::DW_AT_location, 0, Block);
1118 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
1119 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
1120 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
1121 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
1122 int64_t L = SR.getLo();
1123 int64_t H = SR.getHi();
1125 // The L value defines the lower bounds which is typically zero for C/C++. The
1126 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
1127 // of the array. If L > H then do not emit DW_AT_lower_bound and
1128 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
1129 // array has one element and in such case do not emit lower bound.
1132 Buffer.addChild(DW_Subrange);
1136 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
1137 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
1138 Buffer.addChild(DW_Subrange);
1141 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
1142 void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
1143 DICompositeType *CTy) {
1144 Buffer.setTag(dwarf::DW_TAG_array_type);
1145 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
1146 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
1148 // Emit derived type.
1149 addType(&Buffer, CTy->getTypeDerivedFrom());
1150 DIArray Elements = CTy->getTypeArray();
1152 // Get an anonymous type for index type.
1153 DIE *IdxTy = getIndexTyDie();
1155 // Construct an anonymous type for index type.
1156 IdxTy = new DIE(dwarf::DW_TAG_base_type);
1157 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
1158 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1159 dwarf::DW_ATE_signed);
1161 setIndexTyDie(IdxTy);
1164 // Add subranges to array type.
1165 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1166 DIDescriptor Element = Elements.getElement(i);
1167 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1168 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
1172 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1173 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
1174 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
1175 StringRef Name = ETy.getName();
1176 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1177 int64_t Value = ETy.getEnumValue();
1178 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
1182 /// constructContainingTypeDIEs - Construct DIEs for types that contain
1184 void CompileUnit::constructContainingTypeDIEs() {
1185 for (DenseMap<DIE *, const MDNode *>::iterator CI = ContainingTypeMap.begin(),
1186 CE = ContainingTypeMap.end(); CI != CE; ++CI) {
1187 DIE *SPDie = CI->first;
1188 const MDNode *N = CI->second;
1190 DIE *NDie = getDIE(N);
1191 if (!NDie) continue;
1192 addDIEEntry(SPDie, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4, NDie);
1196 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
1197 DIE *CompileUnit::constructVariableDIE(DbgVariable *DV, bool isScopeAbstract) {
1198 StringRef Name = DV->getName();
1202 // Translate tag to proper Dwarf tag.
1203 unsigned Tag = DV->getTag();
1205 // Define variable debug information entry.
1206 DIE *VariableDie = new DIE(Tag);
1207 DbgVariable *AbsVar = DV->getAbstractVariable();
1208 DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL;
1210 addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin,
1211 dwarf::DW_FORM_ref4, AbsDIE);
1213 addString(VariableDie, dwarf::DW_AT_name,
1214 dwarf::DW_FORM_string, Name);
1215 addSourceLine(VariableDie, DV->getVariable());
1216 addType(VariableDie, DV->getType());
1219 if (DV->isArtificial())
1220 addUInt(VariableDie, dwarf::DW_AT_artificial,
1221 dwarf::DW_FORM_flag, 1);
1223 if (isScopeAbstract) {
1224 DV->setDIE(VariableDie);
1228 // Add variable address.
1230 unsigned Offset = DV->getDotDebugLocOffset();
1231 if (Offset != ~0U) {
1232 addLabel(VariableDie, dwarf::DW_AT_location,
1233 dwarf::DW_FORM_data4,
1234 Asm->GetTempSymbol("debug_loc", Offset));
1235 DV->setDIE(VariableDie);
1239 // Check if variable is described by a DBG_VALUE instruction.
1240 if (const MachineInstr *DVInsn = DV->getMInsn()) {
1241 bool updated = false;
1242 if (DVInsn->getNumOperands() == 3) {
1243 if (DVInsn->getOperand(0).isReg()) {
1244 const MachineOperand RegOp = DVInsn->getOperand(0);
1245 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
1246 if (DVInsn->getOperand(1).isImm() &&
1247 TRI->getFrameRegister(*Asm->MF) == RegOp.getReg()) {
1248 unsigned FrameReg = 0;
1249 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1251 TFI->getFrameIndexReference(*Asm->MF,
1252 DVInsn->getOperand(1).getImm(),
1254 MachineLocation Location(FrameReg, Offset);
1255 addVariableAddress(DV, VariableDie, Location);
1257 } else if (RegOp.getReg())
1258 addVariableAddress(DV, VariableDie,
1259 MachineLocation(RegOp.getReg()));
1262 else if (DVInsn->getOperand(0).isImm())
1264 addConstantValue(VariableDie, DVInsn->getOperand(0),
1266 else if (DVInsn->getOperand(0).isFPImm())
1268 addConstantFPValue(VariableDie, DVInsn->getOperand(0));
1269 else if (DVInsn->getOperand(0).isCImm())
1271 addConstantValue(VariableDie,
1272 DVInsn->getOperand(0).getCImm(),
1273 DV->getType().isUnsignedDIType());
1275 addVariableAddress(DV, VariableDie,
1276 Asm->getDebugValueLocation(DVInsn));
1280 // If variableDie is not updated then DBG_VALUE instruction does not
1281 // have valid variable info.
1285 DV->setDIE(VariableDie);
1288 // .. else use frame index.
1289 int FI = DV->getFrameIndex();
1291 unsigned FrameReg = 0;
1292 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
1294 TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
1295 MachineLocation Location(FrameReg, Offset);
1296 addVariableAddress(DV, VariableDie, Location);
1300 DV->setDIE(VariableDie);
1304 /// createMemberDIE - Create new member DIE.
1305 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
1306 DIE *MemberDie = new DIE(DT.getTag());
1307 StringRef Name = DT.getName();
1309 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
1311 addType(MemberDie, DT.getTypeDerivedFrom());
1313 addSourceLine(MemberDie, DT);
1315 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
1316 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1318 uint64_t Size = DT.getSizeInBits();
1319 uint64_t FieldSize = DT.getOriginalTypeSize();
1321 if (Size != FieldSize) {
1323 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
1324 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
1326 uint64_t Offset = DT.getOffsetInBits();
1327 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
1328 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1329 uint64_t FieldOffset = (HiMark - FieldSize);
1330 Offset -= FieldOffset;
1332 // Maybe we need to work from the other end.
1333 if (Asm->getTargetData().isLittleEndian())
1334 Offset = FieldSize - (Offset + Size);
1335 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
1337 // Here WD_AT_data_member_location points to the anonymous
1338 // field that includes this bit field.
1339 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
1342 // This is not a bitfield.
1343 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
1345 if (DT.getTag() == dwarf::DW_TAG_inheritance
1346 && DT.isVirtual()) {
1348 // For C++, virtual base classes are not at fixed offset. Use following
1349 // expression to extract appropriate offset from vtable.
1350 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1352 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
1353 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1354 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1355 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1356 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
1357 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1358 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1359 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1361 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1364 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1366 if (DT.isProtected())
1367 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1368 dwarf::DW_ACCESS_protected);
1369 else if (DT.isPrivate())
1370 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1371 dwarf::DW_ACCESS_private);
1372 // Otherwise C++ member and base classes are considered public.
1374 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1375 dwarf::DW_ACCESS_public);
1377 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1378 dwarf::DW_VIRTUALITY_virtual);
1380 // Objective-C properties.
1381 StringRef PropertyName = DT.getObjCPropertyName();
1382 if (!PropertyName.empty()) {
1383 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, dwarf::DW_FORM_string,
1385 StringRef GetterName = DT.getObjCPropertyGetterName();
1386 if (!GetterName.empty())
1387 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter,
1388 dwarf::DW_FORM_string, GetterName);
1389 StringRef SetterName = DT.getObjCPropertySetterName();
1390 if (!SetterName.empty())
1391 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter,
1392 dwarf::DW_FORM_string, SetterName);
1393 unsigned PropertyAttributes = 0;
1394 if (DT.isReadOnlyObjCProperty())
1395 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1396 if (DT.isReadWriteObjCProperty())
1397 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1398 if (DT.isAssignObjCProperty())
1399 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1400 if (DT.isRetainObjCProperty())
1401 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1402 if (DT.isCopyObjCProperty())
1403 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1404 if (DT.isNonAtomicObjCProperty())
1405 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1406 if (PropertyAttributes)
1407 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1408 PropertyAttributes);