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 /// addFrameVariableAddress - Add DW_AT_location attribute for a
196 /// DbgVariable based on provided frame index.
197 void CompileUnit::addFrameVariableAddress(DbgVariable *&DV, DIE *Die,
199 MachineLocation Location;
201 const TargetFrameLowering *TFI = Asm->TM.getFrameLowering();
202 int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
203 Location.set(FrameReg, Offset);
205 if (DV->variableHasComplexAddress())
206 addComplexAddress(DV, Die, dwarf::DW_AT_location, Location);
207 else if (DV->isBlockByrefVariable())
208 addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location);
210 addAddress(Die, dwarf::DW_AT_location, Location);
213 /// addComplexAddress - Start with the address based on the location provided,
214 /// and generate the DWARF information necessary to find the actual variable
215 /// given the extra address information encoded in the DIVariable, starting from
216 /// the starting location. Add the DWARF information to the die.
218 void CompileUnit::addComplexAddress(DbgVariable *&DV, DIE *Die,
220 const MachineLocation &Location) {
221 DIType Ty = DV->getType();
223 // Decode the original location, and use that as the start of the byref
224 // variable's location.
225 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
226 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
227 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
229 if (Location.isReg()) {
231 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
233 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
234 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
238 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
240 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
241 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
244 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
247 for (unsigned i = 0, N = DV->getNumAddrElements(); i < N; ++i) {
248 uint64_t Element = DV->getAddrElement(i);
250 if (Element == DIBuilder::OpPlus) {
251 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
252 addUInt(Block, 0, dwarf::DW_FORM_udata, DV->getAddrElement(++i));
253 } else if (Element == DIBuilder::OpDeref) {
254 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
255 } else llvm_unreachable("unknown DIBuilder Opcode");
258 // Now attach the location information to the DIE.
259 addBlock(Die, Attribute, 0, Block);
262 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
263 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
264 gives the variable VarName either the struct, or a pointer to the struct, as
265 its type. This is necessary for various behind-the-scenes things the
266 compiler needs to do with by-reference variables in Blocks.
268 However, as far as the original *programmer* is concerned, the variable
269 should still have type 'SomeType', as originally declared.
271 The function getBlockByrefType dives into the __Block_byref_x_VarName
272 struct to find the original type of the variable, which is then assigned to
273 the variable's Debug Information Entry as its real type. So far, so good.
274 However now the debugger will expect the variable VarName to have the type
275 SomeType. So we need the location attribute for the variable to be an
276 expression that explains to the debugger how to navigate through the
277 pointers and struct to find the actual variable of type SomeType.
279 The following function does just that. We start by getting
280 the "normal" location for the variable. This will be the location
281 of either the struct __Block_byref_x_VarName or the pointer to the
282 struct __Block_byref_x_VarName.
284 The struct will look something like:
286 struct __Block_byref_x_VarName {
288 struct __Block_byref_x_VarName *forwarding;
289 ... <various other fields>
291 ... <maybe more fields>
294 If we are given the struct directly (as our starting point) we
295 need to tell the debugger to:
297 1). Add the offset of the forwarding field.
299 2). Follow that pointer to get the real __Block_byref_x_VarName
300 struct to use (the real one may have been copied onto the heap).
302 3). Add the offset for the field VarName, to find the actual variable.
304 If we started with a pointer to the struct, then we need to
305 dereference that pointer first, before the other steps.
306 Translating this into DWARF ops, we will need to append the following
307 to the current location description for the variable:
309 DW_OP_deref -- optional, if we start with a pointer
310 DW_OP_plus_uconst <forward_fld_offset>
312 DW_OP_plus_uconst <varName_fld_offset>
314 That is what this function does. */
316 /// addBlockByrefAddress - Start with the address based on the location
317 /// provided, and generate the DWARF information necessary to find the
318 /// actual Block variable (navigating the Block struct) based on the
319 /// starting location. Add the DWARF information to the die. For
320 /// more information, read large comment just above here.
322 void CompileUnit::addBlockByrefAddress(DbgVariable *&DV, DIE *Die,
324 const MachineLocation &Location) {
325 DIType Ty = DV->getType();
327 unsigned Tag = Ty.getTag();
328 bool isPointer = false;
330 StringRef varName = DV->getName();
332 if (Tag == dwarf::DW_TAG_pointer_type) {
333 DIDerivedType DTy = DIDerivedType(Ty);
334 TmpTy = DTy.getTypeDerivedFrom();
338 DICompositeType blockStruct = DICompositeType(TmpTy);
340 // Find the __forwarding field and the variable field in the __Block_byref
342 DIArray Fields = blockStruct.getTypeArray();
343 DIDescriptor varField = DIDescriptor();
344 DIDescriptor forwardingField = DIDescriptor();
346 for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) {
347 DIDescriptor Element = Fields.getElement(i);
348 DIDerivedType DT = DIDerivedType(Element);
349 StringRef fieldName = DT.getName();
350 if (fieldName == "__forwarding")
351 forwardingField = Element;
352 else if (fieldName == varName)
356 // Get the offsets for the forwarding field and the variable field.
357 unsigned forwardingFieldOffset =
358 DIDerivedType(forwardingField).getOffsetInBits() >> 3;
359 unsigned varFieldOffset =
360 DIDerivedType(varField).getOffsetInBits() >> 3;
362 // Decode the original location, and use that as the start of the byref
363 // variable's location.
364 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
365 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
366 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
368 if (Location.isReg()) {
370 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
372 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
373 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
377 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
379 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
380 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
383 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
386 // If we started with a pointer to the __Block_byref... struct, then
387 // the first thing we need to do is dereference the pointer (DW_OP_deref).
389 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
391 // Next add the offset for the '__forwarding' field:
392 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
393 // adding the offset if it's 0.
394 if (forwardingFieldOffset > 0) {
395 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
396 addUInt(Block, 0, dwarf::DW_FORM_udata, forwardingFieldOffset);
399 // Now dereference the __forwarding field to get to the real __Block_byref
400 // struct: DW_OP_deref.
401 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
403 // Now that we've got the real __Block_byref... struct, add the offset
404 // for the variable's field to get to the location of the actual variable:
405 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
406 if (varFieldOffset > 0) {
407 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
408 addUInt(Block, 0, dwarf::DW_FORM_udata, varFieldOffset);
411 // Now attach the location information to the DIE.
412 addBlock(Die, Attribute, 0, Block);
415 /// addAddress - Add an address attribute to a die based on the location
417 void CompileUnit::addAddress(DIE *Die, unsigned Attribute,
418 const MachineLocation &Location) {
419 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
420 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
421 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
423 if (RI->getFrameRegister(*Asm->MF) == Location.getReg()
424 && Location.getOffset()) {
425 // If variable offset is based in frame register then use fbreg.
426 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg);
427 addSInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
428 addBlock(Die, Attribute, 0, Block);
432 if (Location.isReg()) {
434 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + Reg);
436 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_regx);
437 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
441 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + Reg);
443 addUInt(Block, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx);
444 addUInt(Block, 0, dwarf::DW_FORM_udata, Reg);
447 addUInt(Block, 0, dwarf::DW_FORM_sdata, Location.getOffset());
450 addBlock(Die, Attribute, 0, Block);
453 /// addRegisterAddress - Add register location entry in variable DIE.
454 bool CompileUnit::addRegisterAddress(DIE *Die, const MachineOperand &MO) {
455 assert (MO.isReg() && "Invalid machine operand!");
458 MachineLocation Location;
459 Location.set(MO.getReg());
460 addAddress(Die, dwarf::DW_AT_location, Location);
464 /// addConstantValue - Add constant value entry in variable DIE.
465 bool CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO) {
466 assert (MO.isImm() && "Invalid machine operand!");
467 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
468 unsigned Imm = MO.getImm();
469 addUInt(Block, 0, dwarf::DW_FORM_udata, Imm);
470 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
474 /// addConstantFPValue - Add constant value entry in variable DIE.
475 bool CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) {
476 assert (MO.isFPImm() && "Invalid machine operand!");
477 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
478 APFloat FPImm = MO.getFPImm()->getValueAPF();
480 // Get the raw data form of the floating point.
481 const APInt FltVal = FPImm.bitcastToAPInt();
482 const char *FltPtr = (const char*)FltVal.getRawData();
484 int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
485 bool LittleEndian = Asm->getTargetData().isLittleEndian();
486 int Incr = (LittleEndian ? 1 : -1);
487 int Start = (LittleEndian ? 0 : NumBytes - 1);
488 int Stop = (LittleEndian ? NumBytes : -1);
490 // Output the constant to DWARF one byte at a time.
491 for (; Start != Stop; Start += Incr)
492 addUInt(Block, 0, dwarf::DW_FORM_data1,
493 (unsigned char)0xFF & FltPtr[Start]);
495 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
499 /// addConstantValue - Add constant value entry in variable DIE.
500 bool CompileUnit::addConstantValue(DIE *Die, ConstantInt *CI,
502 if (CI->getBitWidth() <= 64) {
504 addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
507 addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
512 DIEBlock *Block = new (DIEValueAllocator) DIEBlock();
514 // Get the raw data form of the large APInt.
515 const APInt Val = CI->getValue();
516 const char *Ptr = (const char*)Val.getRawData();
518 int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
519 bool LittleEndian = Asm->getTargetData().isLittleEndian();
520 int Incr = (LittleEndian ? 1 : -1);
521 int Start = (LittleEndian ? 0 : NumBytes - 1);
522 int Stop = (LittleEndian ? NumBytes : -1);
524 // Output the constant to DWARF one byte at a time.
525 for (; Start != Stop; Start += Incr)
526 addUInt(Block, 0, dwarf::DW_FORM_data1,
527 (unsigned char)0xFF & Ptr[Start]);
529 addBlock(Die, dwarf::DW_AT_const_value, 0, Block);
533 /// addTemplateParams - Add template parameters in buffer.
534 void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) {
535 // Add template parameters.
536 for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) {
537 DIDescriptor Element = TParams.getElement(i);
538 if (Element.isTemplateTypeParameter())
539 Buffer.addChild(getOrCreateTemplateTypeParameterDIE(
540 DITemplateTypeParameter(Element)));
541 else if (Element.isTemplateValueParameter())
542 Buffer.addChild(getOrCreateTemplateValueParameterDIE(
543 DITemplateValueParameter(Element)));
547 /// addToContextOwner - Add Die into the list of its context owner's children.
548 void CompileUnit::addToContextOwner(DIE *Die, DIDescriptor Context) {
549 if (Context.isType()) {
550 DIE *ContextDIE = getOrCreateTypeDIE(DIType(Context));
551 ContextDIE->addChild(Die);
552 } else if (Context.isNameSpace()) {
553 DIE *ContextDIE = getOrCreateNameSpace(DINameSpace(Context));
554 ContextDIE->addChild(Die);
555 } else if (Context.isSubprogram()) {
556 DIE *ContextDIE = DD->createSubprogramDIE(DISubprogram(Context));
557 ContextDIE->addChild(Die);
558 } else if (DIE *ContextDIE = getDIE(Context))
559 ContextDIE->addChild(Die);
564 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
566 DIE *CompileUnit::getOrCreateTypeDIE(DIType Ty) {
567 DIE *TyDIE = getDIE(Ty);
572 TyDIE = new DIE(dwarf::DW_TAG_base_type);
573 insertDIE(Ty, TyDIE);
574 if (Ty.isBasicType())
575 constructTypeDIE(*TyDIE, DIBasicType(Ty));
576 else if (Ty.isCompositeType())
577 constructTypeDIE(*TyDIE, DICompositeType(Ty));
579 assert(Ty.isDerivedType() && "Unknown kind of DIType");
580 constructTypeDIE(*TyDIE, DIDerivedType(Ty));
583 addToContextOwner(TyDIE, Ty.getContext());
587 /// addType - Add a new type attribute to the specified entity.
588 void CompileUnit::addType(DIE *Entity, DIType Ty) {
592 // Check for pre-existence.
593 DIEEntry *Entry = getDIEEntry(Ty);
594 // If it exists then use the existing value.
596 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
601 DIE *Buffer = getOrCreateTypeDIE(Ty);
604 Entry = createDIEEntry(Buffer);
605 insertDIEEntry(Ty, Entry);
607 Entity->addValue(dwarf::DW_AT_type, dwarf::DW_FORM_ref4, Entry);
610 /// constructTypeDIE - Construct basic type die from DIBasicType.
611 void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) {
612 // Get core information.
613 StringRef Name = BTy.getName();
614 Buffer.setTag(dwarf::DW_TAG_base_type);
615 addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
618 // Add name if not anonymous or intermediate type.
620 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
621 uint64_t Size = BTy.getSizeInBits() >> 3;
622 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
625 /// constructTypeDIE - Construct derived type die from DIDerivedType.
626 void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) {
627 // Get core information.
628 StringRef Name = DTy.getName();
629 uint64_t Size = DTy.getSizeInBits() >> 3;
630 unsigned Tag = DTy.getTag();
632 // FIXME - Workaround for templates.
633 if (Tag == dwarf::DW_TAG_inheritance) Tag = dwarf::DW_TAG_reference_type;
637 // Map to main type, void will not have a type.
638 DIType FromTy = DTy.getTypeDerivedFrom();
639 addType(&Buffer, FromTy);
641 // Add name if not anonymous or intermediate type.
643 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
645 // Add size if non-zero (derived types might be zero-sized.)
647 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
649 // Add source line info if available and TyDesc is not a forward declaration.
650 if (!DTy.isForwardDecl())
651 addSourceLine(&Buffer, DTy);
654 /// constructTypeDIE - Construct type DIE from DICompositeType.
655 void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) {
656 // Get core information.
657 StringRef Name = CTy.getName();
659 uint64_t Size = CTy.getSizeInBits() >> 3;
660 unsigned Tag = CTy.getTag();
664 case dwarf::DW_TAG_vector_type:
665 case dwarf::DW_TAG_array_type:
666 constructArrayTypeDIE(Buffer, &CTy);
668 case dwarf::DW_TAG_enumeration_type: {
669 DIArray Elements = CTy.getTypeArray();
671 // Add enumerators to enumeration type.
672 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
674 DIDescriptor Enum(Elements.getElement(i));
675 if (Enum.isEnumerator()) {
676 ElemDie = constructEnumTypeDIE(DIEnumerator(Enum));
677 Buffer.addChild(ElemDie);
682 case dwarf::DW_TAG_subroutine_type: {
684 DIArray Elements = CTy.getTypeArray();
685 DIDescriptor RTy = Elements.getElement(0);
686 addType(&Buffer, DIType(RTy));
688 bool isPrototyped = true;
690 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
691 DIDescriptor Ty = Elements.getElement(i);
692 if (Ty.isUnspecifiedParameter()) {
693 DIE *Arg = new DIE(dwarf::DW_TAG_unspecified_parameters);
694 Buffer.addChild(Arg);
695 isPrototyped = false;
697 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
698 addType(Arg, DIType(Ty));
699 Buffer.addChild(Arg);
702 // Add prototype flag.
704 addUInt(&Buffer, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag, 1);
707 case dwarf::DW_TAG_structure_type:
708 case dwarf::DW_TAG_union_type:
709 case dwarf::DW_TAG_class_type: {
710 // Add elements to structure type.
711 DIArray Elements = CTy.getTypeArray();
713 // A forward struct declared type may not have elements available.
714 unsigned N = Elements.getNumElements();
718 // Add elements to structure type.
719 for (unsigned i = 0; i < N; ++i) {
720 DIDescriptor Element = Elements.getElement(i);
722 if (Element.isSubprogram()) {
723 DISubprogram SP(Element);
724 ElemDie = DD->createSubprogramDIE(DISubprogram(Element));
725 if (SP.isProtected())
726 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
727 dwarf::DW_ACCESS_protected);
728 else if (SP.isPrivate())
729 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
730 dwarf::DW_ACCESS_private);
732 addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
733 dwarf::DW_ACCESS_public);
735 addUInt(ElemDie, dwarf::DW_AT_explicit, dwarf::DW_FORM_flag, 1);
737 else if (Element.isVariable()) {
738 DIVariable DV(Element);
739 ElemDie = new DIE(dwarf::DW_TAG_variable);
740 addString(ElemDie, dwarf::DW_AT_name, dwarf::DW_FORM_string,
742 addType(ElemDie, DV.getType());
743 addUInt(ElemDie, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
744 addUInt(ElemDie, dwarf::DW_AT_external, dwarf::DW_FORM_flag, 1);
745 addSourceLine(ElemDie, DV);
746 } else if (Element.isDerivedType())
747 ElemDie = createMemberDIE(DIDerivedType(Element));
750 Buffer.addChild(ElemDie);
753 if (CTy.isAppleBlockExtension())
754 addUInt(&Buffer, dwarf::DW_AT_APPLE_block, dwarf::DW_FORM_flag, 1);
756 unsigned RLang = CTy.getRunTimeLang();
758 addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class,
759 dwarf::DW_FORM_data1, RLang);
761 DICompositeType ContainingType = CTy.getContainingType();
762 if (DIDescriptor(ContainingType).isCompositeType())
763 addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, dwarf::DW_FORM_ref4,
764 getOrCreateTypeDIE(DIType(ContainingType)));
766 DIDescriptor Context = CTy.getContext();
767 addToContextOwner(&Buffer, Context);
770 if (Tag == dwarf::DW_TAG_class_type)
771 addTemplateParams(Buffer, CTy.getTemplateParams());
779 // Add name if not anonymous or intermediate type.
781 addString(&Buffer, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
783 if (Tag == dwarf::DW_TAG_enumeration_type || Tag == dwarf::DW_TAG_class_type
784 || Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
786 // Add size if non-zero (derived types might be zero-sized.)
788 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, Size);
790 // Add zero size if it is not a forward declaration.
791 if (CTy.isForwardDecl())
792 addUInt(&Buffer, dwarf::DW_AT_declaration, dwarf::DW_FORM_flag, 1);
794 addUInt(&Buffer, dwarf::DW_AT_byte_size, 0, 0);
797 // Add source line info if available.
798 if (!CTy.isForwardDecl())
799 addSourceLine(&Buffer, CTy);
803 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
804 /// for the given DITemplateTypeParameter.
806 CompileUnit::getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP) {
807 DIE *ParamDIE = getDIE(TP);
811 ParamDIE = new DIE(dwarf::DW_TAG_template_type_parameter);
812 addType(ParamDIE, TP.getType());
813 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TP.getName());
817 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
818 /// for the given DITemplateValueParameter.
820 CompileUnit::getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TPV) {
821 DIE *ParamDIE = getDIE(TPV);
825 ParamDIE = new DIE(dwarf::DW_TAG_template_value_parameter);
826 addType(ParamDIE, TPV.getType());
827 if (!TPV.getName().empty())
828 addString(ParamDIE, dwarf::DW_AT_name, dwarf::DW_FORM_string, TPV.getName());
829 addUInt(ParamDIE, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata,
834 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
835 void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy){
836 DIE *DW_Subrange = new DIE(dwarf::DW_TAG_subrange_type);
837 addDIEEntry(DW_Subrange, dwarf::DW_AT_type, dwarf::DW_FORM_ref4, IndexTy);
838 int64_t L = SR.getLo();
839 int64_t H = SR.getHi();
841 // The L value defines the lower bounds which is typically zero for C/C++. The
842 // H value is the upper bounds. Values are 64 bit. H - L + 1 is the size
843 // of the array. If L > H then do not emit DW_AT_lower_bound and
844 // DW_AT_upper_bound attributes. If L is zero and H is also zero then the
845 // array has one element and in such case do not emit lower bound.
848 Buffer.addChild(DW_Subrange);
852 addSInt(DW_Subrange, dwarf::DW_AT_lower_bound, 0, L);
853 addSInt(DW_Subrange, dwarf::DW_AT_upper_bound, 0, H);
854 Buffer.addChild(DW_Subrange);
857 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
858 void CompileUnit::constructArrayTypeDIE(DIE &Buffer,
859 DICompositeType *CTy) {
860 Buffer.setTag(dwarf::DW_TAG_array_type);
861 if (CTy->getTag() == dwarf::DW_TAG_vector_type)
862 addUInt(&Buffer, dwarf::DW_AT_GNU_vector, dwarf::DW_FORM_flag, 1);
864 // Emit derived type.
865 addType(&Buffer, CTy->getTypeDerivedFrom());
866 DIArray Elements = CTy->getTypeArray();
868 // Get an anonymous type for index type.
869 DIE *IdxTy = getIndexTyDie();
871 // Construct an anonymous type for index type.
872 IdxTy = new DIE(dwarf::DW_TAG_base_type);
873 addUInt(IdxTy, dwarf::DW_AT_byte_size, 0, sizeof(int32_t));
874 addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
875 dwarf::DW_ATE_signed);
877 setIndexTyDie(IdxTy);
880 // Add subranges to array type.
881 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
882 DIDescriptor Element = Elements.getElement(i);
883 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
884 constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy);
888 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
889 DIE *CompileUnit::constructEnumTypeDIE(DIEnumerator ETy) {
890 DIE *Enumerator = new DIE(dwarf::DW_TAG_enumerator);
891 StringRef Name = ETy.getName();
892 addString(Enumerator, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
893 int64_t Value = ETy.getEnumValue();
894 addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value);
898 /// createMemberDIE - Create new member DIE.
899 DIE *CompileUnit::createMemberDIE(DIDerivedType DT) {
900 DIE *MemberDie = new DIE(DT.getTag());
901 StringRef Name = DT.getName();
903 addString(MemberDie, dwarf::DW_AT_name, dwarf::DW_FORM_string, Name);
905 addType(MemberDie, DT.getTypeDerivedFrom());
907 addSourceLine(MemberDie, DT);
909 DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock();
910 addUInt(MemLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
912 uint64_t Size = DT.getSizeInBits();
913 uint64_t FieldSize = DT.getOriginalTypeSize();
915 if (Size != FieldSize) {
917 addUInt(MemberDie, dwarf::DW_AT_byte_size, 0, DT.getOriginalTypeSize()>>3);
918 addUInt(MemberDie, dwarf::DW_AT_bit_size, 0, DT.getSizeInBits());
920 uint64_t Offset = DT.getOffsetInBits();
921 uint64_t AlignMask = ~(DT.getAlignInBits() - 1);
922 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
923 uint64_t FieldOffset = (HiMark - FieldSize);
924 Offset -= FieldOffset;
926 // Maybe we need to work from the other end.
927 if (Asm->getTargetData().isLittleEndian())
928 Offset = FieldSize - (Offset + Size);
929 addUInt(MemberDie, dwarf::DW_AT_bit_offset, 0, Offset);
931 // Here WD_AT_data_member_location points to the anonymous
932 // field that includes this bit field.
933 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, FieldOffset >> 3);
936 // This is not a bitfield.
937 addUInt(MemLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits() >> 3);
939 if (DT.getTag() == dwarf::DW_TAG_inheritance
942 // For C++, virtual base classes are not at fixed offset. Use following
943 // expression to extract appropriate offset from vtable.
944 // BaseAddr = ObAddr + *((*ObAddr) - Offset)
946 DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock();
947 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
948 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
949 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
950 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_udata, DT.getOffsetInBits());
951 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
952 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
953 addUInt(VBaseLocationDie, 0, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
955 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0,
958 addBlock(MemberDie, dwarf::DW_AT_data_member_location, 0, MemLocationDie);
960 if (DT.isProtected())
961 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
962 dwarf::DW_ACCESS_protected);
963 else if (DT.isPrivate())
964 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
965 dwarf::DW_ACCESS_private);
966 // Otherwise C++ member and base classes are considered public.
967 else if (DT.getCompileUnit().getLanguage() == dwarf::DW_LANG_C_plus_plus)
968 addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_flag,
969 dwarf::DW_ACCESS_public);
971 addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_flag,
972 dwarf::DW_VIRTUALITY_virtual);
974 // Objective-C properties.
975 StringRef PropertyName = DT.getObjCPropertyName();
976 if (!PropertyName.empty()) {
977 addString(MemberDie, dwarf::DW_AT_APPLE_property_name, dwarf::DW_FORM_string,
979 StringRef GetterName = DT.getObjCPropertyGetterName();
980 if (!GetterName.empty())
981 addString(MemberDie, dwarf::DW_AT_APPLE_property_getter,
982 dwarf::DW_FORM_string, GetterName);
983 StringRef SetterName = DT.getObjCPropertySetterName();
984 if (!SetterName.empty())
985 addString(MemberDie, dwarf::DW_AT_APPLE_property_setter,
986 dwarf::DW_FORM_string, SetterName);
987 unsigned PropertyAttributes = 0;
988 if (DT.isReadOnlyObjCProperty())
989 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly;
990 if (DT.isReadWriteObjCProperty())
991 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite;
992 if (DT.isAssignObjCProperty())
993 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign;
994 if (DT.isRetainObjCProperty())
995 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain;
996 if (DT.isCopyObjCProperty())
997 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy;
998 if (DT.isNonAtomicObjCProperty())
999 PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic;
1000 if (PropertyAttributes)
1001 addUInt(MemberDie, dwarf::DW_AT_APPLE_property_attribute, 0,
1002 PropertyAttributes);