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/Analysis/DIBuilder.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetFrameLowering.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
24 #include "llvm/ADT/APFloat.h"
25 #include "llvm/Support/ErrorHandling.h"
29 /// CompileUnit - Compile unit constructor.
30 CompileUnit::CompileUnit(unsigned I, DIE *D, AsmPrinter *A, DwarfDebug *DW)
31 : ID(I), CUDie(D), Asm(A), DD(DW), IndexTyDie(0) {
32 DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1);
35 /// ~CompileUnit - Destructor for compile unit.
36 CompileUnit::~CompileUnit() {
37 for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
38 DIEBlocks[j]->~DIEBlock();
41 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
42 /// information entry.
43 DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) {
44 DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry);
48 /// addUInt - Add an unsigned integer attribute data and value.
50 void CompileUnit::addUInt(DIE *Die, unsigned Attribute,
51 unsigned Form, uint64_t Integer) {
52 if (!Form) Form = DIEInteger::BestForm(false, Integer);
53 DIEValue *Value = Integer == 1 ?
54 DIEIntegerOne : new (DIEValueAllocator) DIEInteger(Integer);
55 Die->addValue(Attribute, Form, Value);
58 /// addSInt - Add an signed integer attribute data and value.
60 void CompileUnit::addSInt(DIE *Die, unsigned Attribute,
61 unsigned Form, int64_t Integer) {
62 if (!Form) Form = DIEInteger::BestForm(true, Integer);
63 DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer);
64 Die->addValue(Attribute, Form, Value);
67 /// addString - Add a string attribute data and value. DIEString only
68 /// keeps string reference.
69 void CompileUnit::addString(DIE *Die, unsigned Attribute, unsigned Form,
71 DIEValue *Value = new (DIEValueAllocator) DIEString(String);
72 Die->addValue(Attribute, Form, Value);
75 /// addLabel - Add a Dwarf label attribute data and value.
77 void CompileUnit::addLabel(DIE *Die, unsigned Attribute, unsigned Form,
78 const MCSymbol *Label) {
79 DIEValue *Value = new (DIEValueAllocator) DIELabel(Label);
80 Die->addValue(Attribute, Form, Value);
83 /// addDelta - Add a label delta attribute data and value.
85 void CompileUnit::addDelta(DIE *Die, unsigned Attribute, unsigned Form,
86 const MCSymbol *Hi, const MCSymbol *Lo) {
87 DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
88 Die->addValue(Attribute, Form, Value);
91 /// addDIEEntry - Add a DIE attribute data and value.
93 void CompileUnit::addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form,
95 Die->addValue(Attribute, Form, createDIEEntry(Entry));
99 /// addBlock - Add block data.
101 void CompileUnit::addBlock(DIE *Die, unsigned Attribute, unsigned Form,
103 Block->ComputeSize(Asm);
104 DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
105 Die->addValue(Attribute, Block->BestForm(), Block);
108 /// addSourceLine - Add location information to specified debug information
110 void CompileUnit::addSourceLine(DIE *Die, DIVariable V) {
115 unsigned Line = V.getLineNumber();
118 unsigned FileID = DD->GetOrCreateSourceID(V.getContext().getFilename(),
119 V.getContext().getDirectory());
120 assert(FileID && "Invalid file id");
121 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
122 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
125 /// addSourceLine - Add location information to specified debug information
127 void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) {
128 // Verify global variable.
132 unsigned Line = G.getLineNumber();
135 unsigned FileID = DD->GetOrCreateSourceID(G.getContext().getFilename(),
136 G.getContext().getDirectory());
137 assert(FileID && "Invalid file id");
138 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
139 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
142 /// addSourceLine - Add location information to specified debug information
144 void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) {
145 // Verify subprogram.
148 // If the line number is 0, don't add it.
149 if (SP.getLineNumber() == 0)
152 unsigned Line = SP.getLineNumber();
153 if (!SP.getContext().Verify())
155 unsigned FileID = DD->GetOrCreateSourceID(SP.getFilename(), SP.getDirectory());
156 assert(FileID && "Invalid file id");
157 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
158 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
161 /// addSourceLine - Add location information to specified debug information
163 void CompileUnit::addSourceLine(DIE *Die, DIType Ty) {
168 unsigned Line = Ty.getLineNumber();
169 if (Line == 0 || !Ty.getContext().Verify())
171 unsigned FileID = DD->GetOrCreateSourceID(Ty.getFilename(), Ty.getDirectory());
172 assert(FileID && "Invalid file id");
173 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
174 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
177 /// addSourceLine - Add location information to specified debug information
179 void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) {
184 unsigned Line = NS.getLineNumber();
187 StringRef FN = NS.getFilename();
189 unsigned FileID = DD->GetOrCreateSourceID(FN, NS.getDirectory());
190 assert(FileID && "Invalid file id");
191 addUInt(Die, dwarf::DW_AT_decl_file, 0, FileID);
192 addUInt(Die, dwarf::DW_AT_decl_line, 0, Line);
195 /// addVariableAddress - Add DW_AT_location attribute for a
196 /// DbgVariable based on provided MachineLocation.
197 void CompileUnit::addVariableAddress(DbgVariable *&DV, DIE *Die,
198 MachineLocation Location) {
199 if (DV->variableHasComplexAddress())
200 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
201 else if (DV->isBlockByrefVariable())
202 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
204 addAddress(Die, dwarf::DW_AT_location, Location);
207 /// addRegisterOp - Add register operand.
208 void CompileUnit::addRegisterOp(DIE *TheDie, unsigned Reg) {
209 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
210 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
212 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg);
214 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
215 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
219 /// addRegisterOffset - Add register offset.
220 void CompileUnit::addRegisterOffset(DIE *TheDie, unsigned Reg,
222 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
223 unsigned DWReg = RI->getDwarfRegNum(Reg, false);
224 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
225 if (Reg == TRI->getFrameRegister(*Asm->MF))
226 // If variable offset is based in frame register then use fbreg.
227 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
229 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg);
231 addUInt(TheDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
232 addUInt(TheDie, 0, dwarf::DW_FORM_udata, DWReg);
234 addSInt(TheDie, 0, dwarf::DW_FORM_sdata, Offset);
237 /// addAddress - Add an address attribute to a die based on the location
239 void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
240 const MachineLocation &Location) {
241 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
243 if (Location.isReg())
244 addRegisterOp(Block, Location.getReg());
246 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
248 // Now attach the location information to the DIE.
249 addBlock(Die, Attribute, 0, Block);
252 /// addComplexAddress - Start with the address based on the location provided,
253 /// and generate the DWARF information necessary to find the actual variable
254 /// given the extra address information encoded in the DIVariable, starting from
255 /// the starting location. Add the DWARF information to the die.
257 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
259 const MachineLocation &Location) {
260 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
261 unsigned N = DV->getNumAddrElements();
263 if (Location.isReg()) {
264 if (N >= 2 && DV->getAddrElement(0) == DIBuilder::OpPlus) {
265 // If first address element is OpPlus then emit
266 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
267 addRegisterOffset(Block, Location.getReg(), DV->getAddrElement(1));
270 addRegisterOp(Block, Location.getReg());
273 addRegisterOffset(Block, Location.getReg(), Location.getOffset());
276 uint64_t Element = DV->getAddrElement(i);
277 if (Element == DIBuilder::OpPlus) {
278 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
279 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
280 } else if (Element == DIBuilder::OpDeref) {
281 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
282 } else llvm_unreachable("unknown DIBuilder Opcode");
285 // Now attach the location information to the DIE.
286 addBlock(Die, Attribute, 0, Block);
289 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
290 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
291 gives the variable VarName either the struct, or a pointer to the struct, as
292 its type. This is necessary for various behind-the-scenes things the
293 compiler needs to do with by-reference variables in Blocks.
295 However, as far as the original *programmer* is concerned, the variable
296 should still have type 'SomeType', as originally declared.
298 The function getBlockByrefType dives into the __Block_byref_x_VarName
299 struct to find the original type of the variable, which is then assigned to
300 the variable's Debug Information Entry as its real type. So far, so good.
301 However now the debugger will expect the variable VarName to have the type
302 SomeType. So we need the location attribute for the variable to be an
303 expression that explains to the debugger how to navigate through the
304 pointers and struct to find the actual variable of type SomeType.
306 The following function does just that. We start by getting
307 the "normal" location for the variable. This will be the location
308 of either the struct __Block_byref_x_VarName or the pointer to the
309 struct __Block_byref_x_VarName.
311 The struct will look something like:
313 struct __Block_byref_x_VarName {
315 struct __Block_byref_x_VarName *forwarding;
316 ... <various other fields>
318 ... <maybe more fields>
321 If we are given the struct directly (as our starting point) we
322 need to tell the debugger to:
324 1). Add the offset of the forwarding field.
326 2). Follow that pointer to get the real __Block_byref_x_VarName
327 struct to use (the real one may have been copied onto the heap).
329 3). Add the offset for the field VarName, to find the actual variable.
331 If we started with a pointer to the struct, then we need to
332 dereference that pointer first, before the other steps.
333 Translating this into DWARF ops, we will need to append the following
334 to the current location description for the variable:
336 DW_OP_deref -- optional, if we start with a pointer
337 DW_OP_plus_uconst <forward_fld_offset>
339 DW_OP_plus_uconst <varName_fld_offset>
341 That is what this function does. */
343 /// addBlockByrefAddress - Start with the address based on the location
344 /// provided, and generate the DWARF information necessary to find the
345 /// actual Block variable (navigating the Block struct) based on the
346 /// starting location. Add the DWARF information to the die. For
347 /// more information, read large comment just above here.
349 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
351 const MachineLocation &Location) {
352 DIType Ty = DV->getType();
354 unsigned Tag = Ty.getTag();
355 bool isPointer = false;
357 StringRef varName = DV->getName();
359 if (Tag == dwarf::DW_TAG_pointer_type) {
360 DIDerivedType DTy = DIDerivedType(Ty);
361 TmpTy = DTy.getTypeDerivedFrom();
365 DICompositeType blockStruct = DICompositeType(TmpTy);
367 // Find the __forwarding field and the variable field in the __Block_byref
369 DIArray Fields = blockStruct.getTypeArray();
370 DIDescriptor varField = DIDescriptor();
371 DIDescriptor forwardingField = DIDescriptor();
373 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
374 DIDescriptor Element = Fields.getElement(i);
375 DIDerivedType DT = DIDerivedType(Element);
376 StringRef fieldName = DT.getName();
377 if (fieldName == "__forwarding")
378 forwardingField = Element;
379 else if (fieldName == varName)
383 // Get the offsets for the forwarding field and the variable field.
384 unsigned forwardingFieldOffset =
385 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
386 unsigned varFieldOffset =
387 DIDerivedType(varField).getOffsetInBits() >> 3;
389 // Decode the original location, and use that as the start of the byref
390 // variable's location.
391 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
392 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
393 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
395 if (Location.isReg()) {
397 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
399 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
400 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
404 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
406 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
407 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
410 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
413 // If we started with a pointer to the __Block_byref... struct, then
414 // the first thing we need to do is dereference the pointer (DW_OP_deref).
416 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
418 // Next add the offset for the '__forwarding' field:
419 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
420 // adding the offset if it's 0.
421 if (forwardingFieldOffset > 0) {
422 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
423 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
426 // Now dereference the __forwarding field to get to the real __Block_byref
427 // struct: DW_OP_deref.
428 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
430 // Now that we've got the real __Block_byref... struct, add the offset
431 // for the variable's field to get to the location of the actual variable:
432 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
433 if (varFieldOffset > 0) {
434 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
435 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
438 // Now attach the location information to the DIE.
439 addBlock(Die, Attribute, 0, Block);
442 /// addConstantValue - Add constant value entry in variable DIE.
443 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO,
445 assert (MO.isImm() && "Invalid machine operand!");
446 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
447 unsigned form = dwarf::DW_FORM_udata;
448 switch (Ty.getSizeInBits()) {
449 case 8: form = dwarf::DW_FORM_data1; break;
450 case 16: form = dwarf::DW_FORM_data2; break;
451 case 32: form = dwarf::DW_FORM_data4; break;
452 case 64: form = dwarf::DW_FORM_data8; break;
458 (BTy.getEncoding() == dwarf::DW_ATE_signed
459 || BTy.getEncoding() == dwarf::DW_ATE_signed_char))
460 addSInt(Block, 0, form, MO.getImm());
462 addUInt(Block, 0, form, MO.getImm());
464 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
468 /// addConstantFPValue - Add constant value entry in variable DIE.
469 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
470 assert (MO.isFPImm() && "Invalid machine operand!");
471 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
472 APFloat FPImm = MO.getFPImm()->getValueAPF();
474 // Get the raw data form of the floating point.
475 const APInt FltVal = FPImm.bitcastToAPInt();
476 const char *FltPtr = (const char*)FltVal.getRawData();
478 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
479 bool LittleEndian = Asm->getTargetData().isLittleEndian();
480 int Incr = (LittleEndian ? 1 : -1);
481 int Start = (LittleEndian ? 0 : NumBytes - 1);
482 int Stop = (LittleEndian ? NumBytes : -1);
484 // Output the constant to DWARF one byte at a time.
485 for (; Start != Stop; Start += Incr)
486 addUInt(Block, 0, dwarf::DW_FORM_data1,
487 (unsigned char)0xFF & FltPtr[Start]);
489 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
493 /// addConstantValue - Add constant value entry in variable DIE.
494 bool CompileUnit::addConstantValue(DIE *Die, ConstantInt *CI,
496 unsigned CIBitWidth = CI->getBitWidth();
497 if (CIBitWidth <= 64) {
499 switch (CIBitWidth) {
500 case 8: form = dwarf::DW_FORM_data1; break;
501 case 16: form = dwarf::DW_FORM_data2; break;
502 case 32: form = dwarf::DW_FORM_data4; break;
503 case 64: form = dwarf::DW_FORM_data8; break;
505 form = Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata;
508 addUInt(Die, dwarf::DW_AT_const_value, form, CI->getZExtValue());
510 addSInt(Die, dwarf::DW_AT_const_value, form, CI->getSExtValue());
514 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
516 // Get the raw data form of the large APInt.
517 const APInt Val = CI->getValue();
518 const char *Ptr = (const char*)Val.getRawData();
520 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
521 bool LittleEndian = Asm->getTargetData().isLittleEndian();
522 int Incr = (LittleEndian ? 1 : -1);
523 int Start = (LittleEndian ? 0 : NumBytes - 1);
524 int Stop = (LittleEndian ? NumBytes : -1);
526 // Output the constant to DWARF one byte at a time.
527 for (; Start != Stop; Start += Incr)
528 addUInt(Block, 0, dwarf::DW_FORM_data1,
529 (unsigned char)0xFF & Ptr[Start]);
531 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
535 /// addTemplateParams - Add template parameters in buffer.
536 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
537 // Add template parameters.
538 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
539 DIDescriptor Element = TParams.getElement(i);
540 if (Element.isTemplateTypeParameter())
541 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
542 DITemplateTypeParameter(Element)));
543 else if (Element.isTemplateValueParameter())
544 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
545 DITemplateValueParameter(Element)));
549 /// addToContextOwner - Add Die into the list of its context owner's children.
550 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
551 if (Context.isType()) {
552 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
553 ContextDIE->addChild(Die);
554 } else if (Context.isNameSpace()) {
555 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
556 ContextDIE->addChild(Die);
557 } else if (Context.isSubprogram()) {
558 DIE *ContextDIE = DD->createSubprogramDIE(DISubprogram(Context));
559 ContextDIE->addChild(Die);
560 } else if (DIE *ContextDIE = getDIE(Context))
561 ContextDIE->addChild(Die);
566 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
568 DIE *CompileUnit::getOrCreateTypeDIE(DIType Ty) {
569 DIE *TyDIE = getDIE(Ty);
574 TyDIE = new DIE(dwarf::DW_TAG_base_type);
575 insertDIE(Ty, TyDIE);
576 if (Ty.isBasicType())
577 constructTypeDIE(*TyDIE, DIBasicType(Ty));
578 else if (Ty.isCompositeType())
579 constructTypeDIE(*TyDIE, DICompositeType(Ty));
581 assert(Ty.isDerivedType() && "Unknown kind of DIType");
582 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
585 addToContextOwner(TyDIE, Ty.getContext());
589 /// addType - Add a new type attribute to the specified entity.
590 void CompileUnit::addType(DIE *Entity, DIType Ty) {
594 // Check for pre-existence.
595 DIEEntry *Entry = getDIEEntry(Ty);
596 // If it exists then use the existing value.
598 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
603 DIE *Buffer = getOrCreateTypeDIE(Ty);
606 Entry = createDIEEntry(Buffer);
607 insertDIEEntry(Ty, Entry);
608 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
610 // If this is a complete composite type then include it in the
611 // list of global types.
612 addGlobalType(Ty, Entry->getEntry());
615 /// addGlobalType - Add a new global type to the compile unit.
617 void CompileUnit::addGlobalType(DIType Ty, DIE *Die) {
618 DIDescriptor Context = Ty.getContext();
619 if (Ty.isCompositeType() && !Ty.getName().empty() && !Ty.isForwardDecl()
620 && (Context.isCompileUnit() || Context.isFile() || Context.isNameSpace()))
621 GlobalTypes[Ty.getName()] = Die;
624 /// addPubTypes - Add type for pubtypes section.
625 void CompileUnit::addPubTypes(DISubprogram SP) {
626 DICompositeType SPTy = SP.getType();
627 unsigned SPTag = SPTy.getTag();
628 if (SPTag != dwarf::DW_TAG_subroutine_type)
631 DIArray Args = SPTy.getTypeArray();
632 for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) {
633 DIType ATy(Args.getElement(i));
636 addGlobalType(ATy, getDIEEntry(ATy)->getEntry());
640 /// constructTypeDIE - Construct basic type die from DIBasicType.
641 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
642 // Get core information.
643 StringRef Name = BTy.getName();
644 Buffer.setTag(dwarf::DW_TAG_base_type);
645 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
648 // Add name if not anonymous or intermediate type.
650 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
651 uint64_t Size = BTy.getSizeInBits() >> 3;
652 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
655 /// constructTypeDIE - Construct derived type die from DIDerivedType.
656 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
657 // Get core information.
658 StringRef Name = DTy.getName();
659 uint64_t Size = DTy.getSizeInBits() >> 3;
660 unsigned Tag = DTy.getTag();
662 // FIXME - Workaround for templates.
663 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
667 // Map to main type, void will not have a type.
668 DIType FromTy = DTy.getTypeDerivedFrom();
669 addType(&Buffer, FromTy);
671 // Add name if not anonymous or intermediate type.
673 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
675 // Add size if non-zero (derived types might be zero-sized.)
677 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
679 // Add source line info if available and TyDesc is not a forward declaration.
680 if (!DTy.isForwardDecl())
681 addSourceLine(&Buffer, DTy);
684 /// constructTypeDIE - Construct type DIE from DICompositeType.
685 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
686 // Get core information.
687 StringRef Name = CTy.getName();
689 uint64_t Size = CTy.getSizeInBits() >> 3;
690 unsigned Tag = CTy.getTag();
694 case dwarf::DW_TAG_vector_type:
695 case dwarf::DW_TAG_array_type:
696 constructArrayTypeDIE(Buffer, &CTy);
698 case dwarf::DW_TAG_enumeration_type: {
699 DIArray Elements = CTy.getTypeArray();
701 // Add enumerators to enumeration type.
702 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
704 DIDescriptor Enum(Elements.getElement(i));
705 if (Enum.isEnumerator()) {
706 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
707 Buffer.addChild(ElemDie);
712 case dwarf::DW_TAG_subroutine_type: {
714 DIArray Elements = CTy.getTypeArray();
715 DIDescriptor RTy = Elements.getElement(0);
716 addType(&Buffer, DIType(RTy));
718 bool isPrototyped = true;
720 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
721 DIDescriptor Ty = Elements.getElement(i);
722 if (Ty.isUnspecifiedParameter()) {
723 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
724 Buffer.addChild(Arg);
725 isPrototyped = false;
727 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
728 addType(Arg, DIType(Ty));
729 Buffer.addChild(Arg);
732 // Add prototype flag.
734 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
737 case dwarf::DW_TAG_structure_type:
738 case dwarf::DW_TAG_union_type:
739 case dwarf::DW_TAG_class_type: {
740 // Add elements to structure type.
741 DIArray Elements = CTy.getTypeArray();
743 // A forward struct declared type may not have elements available.
744 unsigned N = Elements.getNumElements();
748 // Add elements to structure type.
749 for (unsigned i = 0; i < N; ++i) {
750 DIDescriptor Element = Elements.getElement(i);
752 if (Element.isSubprogram()) {
753 DISubprogram SP(Element);
754 ElemDie = DD->createSubprogramDIE(DISubprogram(Element));
755 if (SP.isProtected())
756 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
757 dwarf::DW_ACCESS_protected);
758 else if (SP.isPrivate())
759 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
760 dwarf::DW_ACCESS_private);
762 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
763 dwarf::DW_ACCESS_public);
765 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
767 else if (Element.isVariable()) {
768 DIVariable DV(Element);
769 ElemDie = new DIE(dwarf::DW_TAG_variable);
770 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
772 addType(ElemDie, DV.getType());
773 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
774 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
775 addSourceLine(ElemDie, DV);
776 } else if (Element.isDerivedType())
777 ElemDie = createMemberDIE(DIDerivedType(Element));
780 Buffer.addChild(ElemDie);
783 if (CTy.isAppleBlockExtension())
784 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
786 unsigned RLang = CTy.getRunTimeLang();
788 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
789 dwarf::DW_FORM_data1, RLang);
791 DICompositeType ContainingType = CTy.getContainingType();
792 if (DIDescriptor(ContainingType).isCompositeType())
793 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
794 getOrCreateTypeDIE(DIType(ContainingType)));
796 DIDescriptor Context = CTy.getContext();
797 addToContextOwner(&Buffer, Context);
800 if (CTy.isObjcClassComplete())
801 addUInt(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type,
802 dwarf::DW_FORM_flag, 1);
804 if (Tag == dwarf::DW_TAG_class_type)
805 addTemplateParams(Buffer, CTy.getTemplateParams());
813 // Add name if not anonymous or intermediate type.
815 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
817 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
818 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
820 // Add size if non-zero (derived types might be zero-sized.)
822 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
824 // Add zero size if it is not a forward declaration.
825 if (CTy.isForwardDecl())
826 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
828 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
831 // Add source line info if available.
832 if (!CTy.isForwardDecl())
833 addSourceLine(&Buffer, CTy);
837 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
838 /// for the given DITemplateTypeParameter.
840 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
841 DIE *ParamDIE = getDIE(TP);
845 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
846 addType(ParamDIE, TP.getType());
847 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName());
851 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
852 /// for the given DITemplateValueParameter.
854 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
855 DIE *ParamDIE = getDIE(TPV);
859 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
860 addType(ParamDIE, TPV.getType());
861 if (!TPV.getName().empty())
862 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName());
863 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
868 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
869 DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) {
870 DIE *NDie = getDIE(NS);
873 NDie = new DIE(dwarf::DW_TAG_namespace);
875 if (!NS.getName().empty())
876 addString(NDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, NS.getName());
877 addSourceLine(NDie, NS);
878 addToContextOwner(NDie, NS.getContext());
882 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
883 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
884 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
885 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
886 int64_t L = SR.getLo();
887 int64_t H = SR.getHi();
889 // The L value defines the lower bounds which is typically zero for C/C++. The
890 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
891 // of the array. If L > H then do not emit DW_AT_lower_bound and
892 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
893 // array has one element and in such case do not emit lower bound.
896 Buffer.addChild(DW_Subrange);
900 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
901 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
902 Buffer.addChild(DW_Subrange);
905 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
906 void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
907 DICompositeType *CTy) {
908 Buffer.setTag(dwarf::DW_TAG_array_type);
909 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
910 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
912 // Emit derived type.
913 addType(&Buffer, CTy->getTypeDerivedFrom());
914 DIArray Elements = CTy->getTypeArray();
916 // Get an anonymous type for index type.
917 DIE *IdxTy = getIndexTyDie();
919 // Construct an anonymous type for index type.
920 IdxTy = new DIE(dwarf::DW_TAG_base_type);
921 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
922 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
923 dwarf::DW_ATE_signed);
925 setIndexTyDie(IdxTy);
928 // Add subranges to array type.
929 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
930 DIDescriptor Element = Elements.getElement(i);
931 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
932 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
936 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
937 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
938 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
939 StringRef Name = ETy.getName();
940 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
941 int64_t Value = ETy.getEnumValue();
942 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
946 /// createMemberDIE - Create new member DIE.
947 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
948 DIE *MemberDie = new DIE(DT.getTag());
949 StringRef Name = DT.getName();
951 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
953 addType(MemberDie, DT.getTypeDerivedFrom());
955 addSourceLine(MemberDie, DT);
957 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
958 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
960 uint64_t Size = DT.getSizeInBits();
961 uint64_t FieldSize = DT.getOriginalTypeSize();
963 if (Size != FieldSize) {
965 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
966 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
968 uint64_t Offset = DT.getOffsetInBits();
969 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
970 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
971 uint64_t FieldOffset = (HiMark - FieldSize);
972 Offset -= FieldOffset;
974 // Maybe we need to work from the other end.
975 if (Asm->getTargetData().isLittleEndian())
976 Offset = FieldSize - (Offset + Size);
977 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
979 // Here WD_AT_data_member_location points to the anonymous
980 // field that includes this bit field.
981 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
984 // This is not a bitfield.
985 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
987 if (DT.getTag() == dwarf::DW_TAG_inheritance
990 // For C++, virtual base classes are not at fixed offset. Use following
991 // expression to extract appropriate offset from vtable.
992 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
994 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
995 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
996 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
997 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
998 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
999 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1000 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1001 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1003 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
1006 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
1008 if (DT.isProtected())
1009 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1010 dwarf::DW_ACCESS_protected);
1011 else if (DT.isPrivate())
1012 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1013 dwarf::DW_ACCESS_private);
1014 // Otherwise C++ member and base classes are considered public.
1015 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
1016 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
1017 dwarf::DW_ACCESS_public);
1019 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
1020 dwarf::DW_VIRTUALITY_virtual);
1022 // Objective-C properties.
1023 StringRef PropertyName = DT.getObjCPropertyName();
1024 if (!PropertyName.empty()) {
1025 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, dwarf::DW_FORM_string,
1027 StringRef GetterName = DT.getObjCPropertyGetterName();
1028 if (!GetterName.empty())
1029 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter,
1030 dwarf::DW_FORM_string, GetterName);
1031 StringRef SetterName = DT.getObjCPropertySetterName();
1032 if (!SetterName.empty())
1033 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter,
1034 dwarf::DW_FORM_string, SetterName);
1035 unsigned PropertyAttributes = 0;
1036 if (DT.isReadOnlyObjCProperty())
1037 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
1038 if (DT.isReadWriteObjCProperty())
1039 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
1040 if (DT.isAssignObjCProperty())
1041 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
1042 if (DT.isRetainObjCProperty())
1043 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
1044 if (DT.isCopyObjCProperty())
1045 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
1046 if (DT.isNonAtomicObjCProperty())
1047 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1048 if (PropertyAttributes)
1049 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1050 PropertyAttributes);