1 //===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
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 debug info into asm files.
12 //===----------------------------------------------------------------------===//
13 #define DEBUG_TYPE "dwarfdebug"
14 #include "DwarfDebug.h"
15 #include "llvm/Module.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/Target/TargetFrameInfo.h"
23 #include "llvm/Target/TargetLoweringObjectFile.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Support/Timer.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/System/Path.h"
31 static TimerGroup
&getDwarfTimerGroup() {
32 static TimerGroup
DwarfTimerGroup("Dwarf Debugging");
33 return DwarfTimerGroup
;
36 //===----------------------------------------------------------------------===//
38 /// Configuration values for initial hash set sizes (log2).
40 static const unsigned InitDiesSetSize
= 9; // log2(512)
41 static const unsigned InitAbbreviationsSetSize
= 9; // log2(512)
42 static const unsigned InitValuesSetSize
= 9; // log2(512)
46 //===----------------------------------------------------------------------===//
47 /// CompileUnit - This dwarf writer support class manages information associate
48 /// with a source file.
49 class VISIBILITY_HIDDEN CompileUnit
{
50 /// ID - File identifier for source.
54 /// Die - Compile unit debug information entry.
58 /// GVToDieMap - Tracks the mapping of unit level debug informaton
59 /// variables to debug information entries.
60 /// FIXME : Rename GVToDieMap -> NodeToDieMap
61 std::map
<MDNode
*, DIE
*> GVToDieMap
;
63 /// GVToDIEEntryMap - Tracks the mapping of unit level debug informaton
64 /// descriptors to debug information entries using a DIEEntry proxy.
66 std::map
<MDNode
*, DIEEntry
*> GVToDIEEntryMap
;
68 /// Globals - A map of globally visible named entities for this unit.
70 StringMap
<DIE
*> Globals
;
72 /// DiesSet - Used to uniquely define dies within the compile unit.
74 FoldingSet
<DIE
> DiesSet
;
76 CompileUnit(unsigned I
, DIE
*D
)
77 : ID(I
), Die(D
), DiesSet(InitDiesSetSize
) {}
78 ~CompileUnit() { delete Die
; }
81 unsigned getID() const { return ID
; }
82 DIE
* getDie() const { return Die
; }
83 StringMap
<DIE
*> &getGlobals() { return Globals
; }
85 /// hasContent - Return true if this compile unit has something to write out.
87 bool hasContent() const { return !Die
->getChildren().empty(); }
89 /// AddGlobal - Add a new global entity to the compile unit.
91 void AddGlobal(const std::string
&Name
, DIE
*Die
) { Globals
[Name
] = Die
; }
93 /// getDieMapSlotFor - Returns the debug information entry map slot for the
94 /// specified debug variable.
95 DIE
*&getDieMapSlotFor(MDNode
*N
) { return GVToDieMap
[N
]; }
97 /// getDIEEntrySlotFor - Returns the debug information entry proxy slot for
98 /// the specified debug variable.
99 DIEEntry
*&getDIEEntrySlotFor(MDNode
*N
) {
100 return GVToDIEEntryMap
[N
];
103 /// AddDie - Adds or interns the DIE to the compile unit.
105 DIE
*AddDie(DIE
&Buffer
) {
109 DIE
*Die
= DiesSet
.FindNodeOrInsertPos(ID
, Where
);
112 Die
= new DIE(Buffer
);
113 DiesSet
.InsertNode(Die
, Where
);
114 this->Die
->AddChild(Die
);
122 //===----------------------------------------------------------------------===//
123 /// DbgVariable - This class is used to track local variable information.
125 class VISIBILITY_HIDDEN DbgVariable
{
126 DIVariable Var
; // Variable Descriptor.
127 unsigned FrameIndex
; // Variable frame index.
128 bool InlinedFnVar
; // Variable for an inlined function.
130 DbgVariable(DIVariable V
, unsigned I
, bool IFV
)
131 : Var(V
), FrameIndex(I
), InlinedFnVar(IFV
) {}
134 DIVariable
getVariable() const { return Var
; }
135 unsigned getFrameIndex() const { return FrameIndex
; }
136 bool isInlinedFnVar() const { return InlinedFnVar
; }
139 //===----------------------------------------------------------------------===//
140 /// DbgScope - This class is used to track scope information.
142 class DbgConcreteScope
;
143 class VISIBILITY_HIDDEN DbgScope
{
144 DbgScope
*Parent
; // Parent to this scope.
145 DIDescriptor Desc
; // Debug info descriptor for scope.
146 // Either subprogram or block.
147 unsigned StartLabelID
; // Label ID of the beginning of scope.
148 unsigned EndLabelID
; // Label ID of the end of scope.
149 SmallVector
<DbgScope
*, 4> Scopes
; // Scopes defined in scope.
150 SmallVector
<DbgVariable
*, 8> Variables
;// Variables declared in scope.
151 SmallVector
<DbgConcreteScope
*, 8> ConcreteInsts
;// Concrete insts of funcs.
153 // Private state for dump()
154 mutable unsigned IndentLevel
;
156 DbgScope(DbgScope
*P
, DIDescriptor D
)
157 : Parent(P
), Desc(D
), StartLabelID(0), EndLabelID(0), IndentLevel(0) {}
161 DbgScope
*getParent() const { return Parent
; }
162 DIDescriptor
getDesc() const { return Desc
; }
163 unsigned getStartLabelID() const { return StartLabelID
; }
164 unsigned getEndLabelID() const { return EndLabelID
; }
165 SmallVector
<DbgScope
*, 4> &getScopes() { return Scopes
; }
166 SmallVector
<DbgVariable
*, 8> &getVariables() { return Variables
; }
167 SmallVector
<DbgConcreteScope
*,8> &getConcreteInsts() { return ConcreteInsts
; }
168 void setStartLabelID(unsigned S
) { StartLabelID
= S
; }
169 void setEndLabelID(unsigned E
) { EndLabelID
= E
; }
171 /// AddScope - Add a scope to the scope.
173 void AddScope(DbgScope
*S
) { Scopes
.push_back(S
); }
175 /// AddVariable - Add a variable to the scope.
177 void AddVariable(DbgVariable
*V
) { Variables
.push_back(V
); }
179 /// AddConcreteInst - Add a concrete instance to the scope.
181 void AddConcreteInst(DbgConcreteScope
*C
) { ConcreteInsts
.push_back(C
); }
189 void DbgScope::dump() const {
190 raw_ostream
&err
= errs();
191 err
.indent(IndentLevel
);
193 err
<< " [" << StartLabelID
<< ", " << EndLabelID
<< "]\n";
197 for (unsigned i
= 0, e
= Scopes
.size(); i
!= e
; ++i
)
198 if (Scopes
[i
] != this)
205 //===----------------------------------------------------------------------===//
206 /// DbgConcreteScope - This class is used to track a scope that holds concrete
207 /// instance information.
209 class VISIBILITY_HIDDEN DbgConcreteScope
: public DbgScope
{
211 DIE
*Die
; // Debug info for this concrete scope.
213 DbgConcreteScope(DIDescriptor D
) : DbgScope(NULL
, D
) {}
216 DIE
*getDie() const { return Die
; }
217 void setDie(DIE
*D
) { Die
= D
; }
220 DbgScope::~DbgScope() {
221 for (unsigned i
= 0, N
= Scopes
.size(); i
< N
; ++i
)
223 for (unsigned j
= 0, M
= Variables
.size(); j
< M
; ++j
)
225 for (unsigned k
= 0, O
= ConcreteInsts
.size(); k
< O
; ++k
)
226 delete ConcreteInsts
[k
];
229 } // end llvm namespace
231 DwarfDebug::DwarfDebug(raw_ostream
&OS
, AsmPrinter
*A
, const MCAsmInfo
*T
)
232 : Dwarf(OS
, A
, T
, "dbg"), ModuleCU(0),
233 AbbreviationsSet(InitAbbreviationsSetSize
), Abbreviations(),
234 ValuesSet(InitValuesSetSize
), Values(), StringPool(),
235 SectionSourceLines(), didInitial(false), shouldEmit(false),
236 FunctionDbgScope(0), DebugTimer(0) {
237 if (TimePassesIsEnabled
)
238 DebugTimer
= new Timer("Dwarf Debug Writer",
239 getDwarfTimerGroup());
241 DwarfDebug::~DwarfDebug() {
242 for (unsigned j
= 0, M
= Values
.size(); j
< M
; ++j
)
245 for (DenseMap
<const MDNode
*, DbgScope
*>::iterator
246 I
= AbstractInstanceRootMap
.begin(),
247 E
= AbstractInstanceRootMap
.end(); I
!= E
;++I
)
253 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
255 void DwarfDebug::AssignAbbrevNumber(DIEAbbrev
&Abbrev
) {
256 // Profile the node so that we can make it unique.
260 // Check the set for priors.
261 DIEAbbrev
*InSet
= AbbreviationsSet
.GetOrInsertNode(&Abbrev
);
263 // If it's newly added.
264 if (InSet
== &Abbrev
) {
265 // Add to abbreviation list.
266 Abbreviations
.push_back(&Abbrev
);
268 // Assign the vector position + 1 as its number.
269 Abbrev
.setNumber(Abbreviations
.size());
271 // Assign existing abbreviation number.
272 Abbrev
.setNumber(InSet
->getNumber());
276 /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
277 /// information entry.
278 DIEEntry
*DwarfDebug::CreateDIEEntry(DIE
*Entry
) {
283 DIEEntry::Profile(ID
, Entry
);
285 Value
= static_cast<DIEEntry
*>(ValuesSet
.FindNodeOrInsertPos(ID
, Where
));
287 if (Value
) return Value
;
289 Value
= new DIEEntry(Entry
);
290 ValuesSet
.InsertNode(Value
, Where
);
292 Value
= new DIEEntry(Entry
);
295 Values
.push_back(Value
);
299 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
301 void DwarfDebug::SetDIEEntry(DIEEntry
*Value
, DIE
*Entry
) {
302 Value
->setEntry(Entry
);
304 // Add to values set if not already there. If it is, we merely have a
305 // duplicate in the values list (no harm.)
306 ValuesSet
.GetOrInsertNode(Value
);
309 /// AddUInt - Add an unsigned integer attribute data and value.
311 void DwarfDebug::AddUInt(DIE
*Die
, unsigned Attribute
,
312 unsigned Form
, uint64_t Integer
) {
313 if (!Form
) Form
= DIEInteger::BestForm(false, Integer
);
316 DIEInteger::Profile(ID
, Integer
);
318 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
321 Value
= new DIEInteger(Integer
);
322 ValuesSet
.InsertNode(Value
, Where
);
323 Values
.push_back(Value
);
326 Die
->AddValue(Attribute
, Form
, Value
);
329 /// AddSInt - Add an signed integer attribute data and value.
331 void DwarfDebug::AddSInt(DIE
*Die
, unsigned Attribute
,
332 unsigned Form
, int64_t Integer
) {
333 if (!Form
) Form
= DIEInteger::BestForm(true, Integer
);
336 DIEInteger::Profile(ID
, (uint64_t)Integer
);
338 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
341 Value
= new DIEInteger(Integer
);
342 ValuesSet
.InsertNode(Value
, Where
);
343 Values
.push_back(Value
);
346 Die
->AddValue(Attribute
, Form
, Value
);
349 /// AddString - Add a string attribute data and value.
351 void DwarfDebug::AddString(DIE
*Die
, unsigned Attribute
, unsigned Form
,
352 const std::string
&String
) {
354 DIEString::Profile(ID
, String
);
356 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
359 Value
= new DIEString(String
);
360 ValuesSet
.InsertNode(Value
, Where
);
361 Values
.push_back(Value
);
364 Die
->AddValue(Attribute
, Form
, Value
);
367 /// AddLabel - Add a Dwarf label attribute data and value.
369 void DwarfDebug::AddLabel(DIE
*Die
, unsigned Attribute
, unsigned Form
,
370 const DWLabel
&Label
) {
372 DIEDwarfLabel::Profile(ID
, Label
);
374 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
377 Value
= new DIEDwarfLabel(Label
);
378 ValuesSet
.InsertNode(Value
, Where
);
379 Values
.push_back(Value
);
382 Die
->AddValue(Attribute
, Form
, Value
);
385 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
387 void DwarfDebug::AddObjectLabel(DIE
*Die
, unsigned Attribute
, unsigned Form
,
388 const std::string
&Label
) {
390 DIEObjectLabel::Profile(ID
, Label
);
392 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
395 Value
= new DIEObjectLabel(Label
);
396 ValuesSet
.InsertNode(Value
, Where
);
397 Values
.push_back(Value
);
400 Die
->AddValue(Attribute
, Form
, Value
);
403 /// AddSectionOffset - Add a section offset label attribute data and value.
405 void DwarfDebug::AddSectionOffset(DIE
*Die
, unsigned Attribute
, unsigned Form
,
406 const DWLabel
&Label
, const DWLabel
&Section
,
407 bool isEH
, bool useSet
) {
409 DIESectionOffset::Profile(ID
, Label
, Section
);
411 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
414 Value
= new DIESectionOffset(Label
, Section
, isEH
, useSet
);
415 ValuesSet
.InsertNode(Value
, Where
);
416 Values
.push_back(Value
);
419 Die
->AddValue(Attribute
, Form
, Value
);
422 /// AddDelta - Add a label delta attribute data and value.
424 void DwarfDebug::AddDelta(DIE
*Die
, unsigned Attribute
, unsigned Form
,
425 const DWLabel
&Hi
, const DWLabel
&Lo
) {
427 DIEDelta::Profile(ID
, Hi
, Lo
);
429 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
432 Value
= new DIEDelta(Hi
, Lo
);
433 ValuesSet
.InsertNode(Value
, Where
);
434 Values
.push_back(Value
);
437 Die
->AddValue(Attribute
, Form
, Value
);
440 /// AddBlock - Add block data.
442 void DwarfDebug::AddBlock(DIE
*Die
, unsigned Attribute
, unsigned Form
,
444 Block
->ComputeSize(TD
);
448 DIEValue
*Value
= ValuesSet
.FindNodeOrInsertPos(ID
, Where
);
452 ValuesSet
.InsertNode(Value
, Where
);
453 Values
.push_back(Value
);
455 // Already exists, reuse the previous one.
457 Block
= cast
<DIEBlock
>(Value
);
460 Die
->AddValue(Attribute
, Block
->BestForm(), Value
);
463 /// AddSourceLine - Add location information to specified debug information
465 void DwarfDebug::AddSourceLine(DIE
*Die
, const DIVariable
*V
) {
466 // If there is no compile unit specified, don't add a line #.
467 if (V
->getCompileUnit().isNull())
470 unsigned Line
= V
->getLineNumber();
471 unsigned FileID
= FindCompileUnit(V
->getCompileUnit()).getID();
472 assert(FileID
&& "Invalid file id");
473 AddUInt(Die
, dwarf::DW_AT_decl_file
, 0, FileID
);
474 AddUInt(Die
, dwarf::DW_AT_decl_line
, 0, Line
);
477 /// AddSourceLine - Add location information to specified debug information
479 void DwarfDebug::AddSourceLine(DIE
*Die
, const DIGlobal
*G
) {
480 // If there is no compile unit specified, don't add a line #.
481 if (G
->getCompileUnit().isNull())
484 unsigned Line
= G
->getLineNumber();
485 unsigned FileID
= FindCompileUnit(G
->getCompileUnit()).getID();
486 assert(FileID
&& "Invalid file id");
487 AddUInt(Die
, dwarf::DW_AT_decl_file
, 0, FileID
);
488 AddUInt(Die
, dwarf::DW_AT_decl_line
, 0, Line
);
491 /// AddSourceLine - Add location information to specified debug information
493 void DwarfDebug::AddSourceLine(DIE
*Die
, const DISubprogram
*SP
) {
494 // If there is no compile unit specified, don't add a line #.
495 if (SP
->getCompileUnit().isNull())
498 unsigned Line
= SP
->getLineNumber();
499 unsigned FileID
= FindCompileUnit(SP
->getCompileUnit()).getID();
500 assert(FileID
&& "Invalid file id");
501 AddUInt(Die
, dwarf::DW_AT_decl_file
, 0, FileID
);
502 AddUInt(Die
, dwarf::DW_AT_decl_line
, 0, Line
);
505 /// AddSourceLine - Add location information to specified debug information
507 void DwarfDebug::AddSourceLine(DIE
*Die
, const DIType
*Ty
) {
508 // If there is no compile unit specified, don't add a line #.
509 DICompileUnit CU
= Ty
->getCompileUnit();
513 unsigned Line
= Ty
->getLineNumber();
514 unsigned FileID
= FindCompileUnit(CU
).getID();
515 assert(FileID
&& "Invalid file id");
516 AddUInt(Die
, dwarf::DW_AT_decl_file
, 0, FileID
);
517 AddUInt(Die
, dwarf::DW_AT_decl_line
, 0, Line
);
520 /* Byref variables, in Blocks, are declared by the programmer as
521 "SomeType VarName;", but the compiler creates a
522 __Block_byref_x_VarName struct, and gives the variable VarName
523 either the struct, or a pointer to the struct, as its type. This
524 is necessary for various behind-the-scenes things the compiler
525 needs to do with by-reference variables in blocks.
527 However, as far as the original *programmer* is concerned, the
528 variable should still have type 'SomeType', as originally declared.
530 The following function dives into the __Block_byref_x_VarName
531 struct to find the original type of the variable. This will be
532 passed back to the code generating the type for the Debug
533 Information Entry for the variable 'VarName'. 'VarName' will then
534 have the original type 'SomeType' in its debug information.
536 The original type 'SomeType' will be the type of the field named
537 'VarName' inside the __Block_byref_x_VarName struct.
539 NOTE: In order for this to not completely fail on the debugger
540 side, the Debug Information Entry for the variable VarName needs to
541 have a DW_AT_location that tells the debugger how to unwind through
542 the pointers and __Block_byref_x_VarName struct to find the actual
543 value of the variable. The function AddBlockByrefType does this. */
545 /// Find the type the programmer originally declared the variable to be
546 /// and return that type.
548 DIType
DwarfDebug::GetBlockByrefType(DIType Ty
, std::string Name
) {
551 unsigned tag
= Ty
.getTag();
553 if (tag
== dwarf::DW_TAG_pointer_type
) {
554 DIDerivedType DTy
= DIDerivedType (Ty
.getNode());
555 subType
= DTy
.getTypeDerivedFrom();
558 DICompositeType blockStruct
= DICompositeType(subType
.getNode());
560 DIArray Elements
= blockStruct
.getTypeArray();
562 if (Elements
.isNull())
565 for (unsigned i
= 0, N
= Elements
.getNumElements(); i
< N
; ++i
) {
566 DIDescriptor Element
= Elements
.getElement(i
);
567 DIDerivedType DT
= DIDerivedType(Element
.getNode());
571 return (DT
.getTypeDerivedFrom());
577 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
578 VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
579 gives the variable VarName either the struct, or a pointer to the struct, as
580 its type. This is necessary for various behind-the-scenes things the
581 compiler needs to do with by-reference variables in Blocks.
583 However, as far as the original *programmer* is concerned, the variable
584 should still have type 'SomeType', as originally declared.
586 The function GetBlockByrefType dives into the __Block_byref_x_VarName
587 struct to find the original type of the variable, which is then assigned to
588 the variable's Debug Information Entry as its real type. So far, so good.
589 However now the debugger will expect the variable VarName to have the type
590 SomeType. So we need the location attribute for the variable to be an
591 expression that explains to the debugger how to navigate through the
592 pointers and struct to find the actual variable of type SomeType.
594 The following function does just that. We start by getting
595 the "normal" location for the variable. This will be the location
596 of either the struct __Block_byref_x_VarName or the pointer to the
597 struct __Block_byref_x_VarName.
599 The struct will look something like:
601 struct __Block_byref_x_VarName {
603 struct __Block_byref_x_VarName *forwarding;
604 ... <various other fields>
606 ... <maybe more fields>
609 If we are given the struct directly (as our starting point) we
610 need to tell the debugger to:
612 1). Add the offset of the forwarding field.
614 2). Follow that pointer to get the the real __Block_byref_x_VarName
615 struct to use (the real one may have been copied onto the heap).
617 3). Add the offset for the field VarName, to find the actual variable.
619 If we started with a pointer to the struct, then we need to
620 dereference that pointer first, before the other steps.
621 Translating this into DWARF ops, we will need to append the following
622 to the current location description for the variable:
624 DW_OP_deref -- optional, if we start with a pointer
625 DW_OP_plus_uconst <forward_fld_offset>
627 DW_OP_plus_uconst <varName_fld_offset>
629 That is what this function does. */
631 /// AddBlockByrefAddress - Start with the address based on the location
632 /// provided, and generate the DWARF information necessary to find the
633 /// actual Block variable (navigating the Block struct) based on the
634 /// starting location. Add the DWARF information to the die. For
635 /// more information, read large comment just above here.
637 void DwarfDebug::AddBlockByrefAddress(DbgVariable
*&DV
, DIE
*Die
,
639 const MachineLocation
&Location
) {
640 const DIVariable
&VD
= DV
->getVariable();
641 DIType Ty
= VD
.getType();
643 unsigned Tag
= Ty
.getTag();
644 bool isPointer
= false;
649 if (Tag
== dwarf::DW_TAG_pointer_type
) {
650 DIDerivedType DTy
= DIDerivedType (Ty
.getNode());
651 TmpTy
= DTy
.getTypeDerivedFrom();
655 DICompositeType blockStruct
= DICompositeType(TmpTy
.getNode());
657 std::string typeName
;
658 blockStruct
.getName(typeName
);
660 assert(typeName
.find ("__Block_byref_") == 0
661 && "Attempting to get Block location of non-Block variable!");
663 // Find the __forwarding field and the variable field in the __Block_byref
666 DIArray Fields
= blockStruct
.getTypeArray();
667 DIDescriptor varField
= DIDescriptor();
668 DIDescriptor forwardingField
= DIDescriptor();
671 for (unsigned i
= 0, N
= Fields
.getNumElements(); i
< N
; ++i
) {
672 DIDescriptor Element
= Fields
.getElement(i
);
673 DIDerivedType DT
= DIDerivedType(Element
.getNode());
674 std::string fieldName
;
675 DT
.getName(fieldName
);
676 if (fieldName
== "__forwarding")
677 forwardingField
= Element
;
678 else if (fieldName
== varName
)
682 assert (!varField
.isNull() && "Can't find byref variable in Block struct");
683 assert (!forwardingField
.isNull()
684 && "Can't find forwarding field in Block struct");
686 // Get the offsets for the forwarding field and the variable field.
688 unsigned int forwardingFieldOffset
=
689 DIDerivedType(forwardingField
.getNode()).getOffsetInBits() >> 3;
690 unsigned int varFieldOffset
=
691 DIDerivedType(varField
.getNode()).getOffsetInBits() >> 3;
693 // Decode the original location, and use that as the start of the
694 // byref variable's location.
696 unsigned Reg
= RI
->getDwarfRegNum(Location
.getReg(), false);
697 DIEBlock
*Block
= new DIEBlock();
699 if (Location
.isReg()) {
701 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_reg0
+ Reg
);
703 Reg
= Reg
- dwarf::DW_OP_reg0
;
704 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_breg0
+ Reg
);
705 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, Reg
);
709 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_breg0
+ Reg
);
711 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_bregx
);
712 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, Reg
);
715 AddUInt(Block
, 0, dwarf::DW_FORM_sdata
, Location
.getOffset());
718 // If we started with a pointer to the__Block_byref... struct, then
719 // the first thing we need to do is dereference the pointer (DW_OP_deref).
722 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_deref
);
724 // Next add the offset for the '__forwarding' field:
725 // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in
726 // adding the offset if it's 0.
728 if (forwardingFieldOffset
> 0) {
729 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus_uconst
);
730 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, forwardingFieldOffset
);
733 // Now dereference the __forwarding field to get to the real __Block_byref
734 // struct: DW_OP_deref.
736 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_deref
);
738 // Now that we've got the real __Block_byref... struct, add the offset
739 // for the variable's field to get to the location of the actual variable:
740 // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0.
742 if (varFieldOffset
> 0) {
743 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus_uconst
);
744 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, varFieldOffset
);
747 // Now attach the location information to the DIE.
749 AddBlock(Die
, Attribute
, 0, Block
);
752 /// AddAddress - Add an address attribute to a die based on the location
754 void DwarfDebug::AddAddress(DIE
*Die
, unsigned Attribute
,
755 const MachineLocation
&Location
) {
756 unsigned Reg
= RI
->getDwarfRegNum(Location
.getReg(), false);
757 DIEBlock
*Block
= new DIEBlock();
759 if (Location
.isReg()) {
761 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_reg0
+ Reg
);
763 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_regx
);
764 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, Reg
);
768 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_breg0
+ Reg
);
770 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_bregx
);
771 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, Reg
);
774 AddUInt(Block
, 0, dwarf::DW_FORM_sdata
, Location
.getOffset());
777 AddBlock(Die
, Attribute
, 0, Block
);
780 /// AddType - Add a new type attribute to the specified entity.
781 void DwarfDebug::AddType(CompileUnit
*DW_Unit
, DIE
*Entity
, DIType Ty
) {
785 // Check for pre-existence.
786 DIEEntry
*&Slot
= DW_Unit
->getDIEEntrySlotFor(Ty
.getNode());
788 // If it exists then use the existing value.
790 Entity
->AddValue(dwarf::DW_AT_type
, dwarf::DW_FORM_ref4
, Slot
);
795 Slot
= CreateDIEEntry();
798 DIE
Buffer(dwarf::DW_TAG_base_type
);
799 if (Ty
.isBasicType())
800 ConstructTypeDIE(DW_Unit
, Buffer
, DIBasicType(Ty
.getNode()));
801 else if (Ty
.isCompositeType())
802 ConstructTypeDIE(DW_Unit
, Buffer
, DICompositeType(Ty
.getNode()));
804 assert(Ty
.isDerivedType() && "Unknown kind of DIType");
805 ConstructTypeDIE(DW_Unit
, Buffer
, DIDerivedType(Ty
.getNode()));
809 // Add debug information entry to entity and appropriate context.
811 DIDescriptor Context
= Ty
.getContext();
812 if (!Context
.isNull())
813 Die
= DW_Unit
->getDieMapSlotFor(Context
.getNode());
816 DIE
*Child
= new DIE(Buffer
);
817 Die
->AddChild(Child
);
819 SetDIEEntry(Slot
, Child
);
821 Die
= DW_Unit
->AddDie(Buffer
);
822 SetDIEEntry(Slot
, Die
);
825 Entity
->AddValue(dwarf::DW_AT_type
, dwarf::DW_FORM_ref4
, Slot
);
828 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
829 void DwarfDebug::ConstructTypeDIE(CompileUnit
*DW_Unit
, DIE
&Buffer
,
831 // Get core information.
834 Buffer
.setTag(dwarf::DW_TAG_base_type
);
835 AddUInt(&Buffer
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
838 // Add name if not anonymous or intermediate type.
840 AddString(&Buffer
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
841 uint64_t Size
= BTy
.getSizeInBits() >> 3;
842 AddUInt(&Buffer
, dwarf::DW_AT_byte_size
, 0, Size
);
845 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
846 void DwarfDebug::ConstructTypeDIE(CompileUnit
*DW_Unit
, DIE
&Buffer
,
848 // Get core information.
851 uint64_t Size
= DTy
.getSizeInBits() >> 3;
852 unsigned Tag
= DTy
.getTag();
854 // FIXME - Workaround for templates.
855 if (Tag
== dwarf::DW_TAG_inheritance
) Tag
= dwarf::DW_TAG_reference_type
;
859 // Map to main type, void will not have a type.
860 DIType FromTy
= DTy
.getTypeDerivedFrom();
861 AddType(DW_Unit
, &Buffer
, FromTy
);
863 // Add name if not anonymous or intermediate type.
865 AddString(&Buffer
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
867 // Add size if non-zero (derived types might be zero-sized.)
869 AddUInt(&Buffer
, dwarf::DW_AT_byte_size
, 0, Size
);
871 // Add source line info if available and TyDesc is not a forward declaration.
872 if (!DTy
.isForwardDecl())
873 AddSourceLine(&Buffer
, &DTy
);
876 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
877 void DwarfDebug::ConstructTypeDIE(CompileUnit
*DW_Unit
, DIE
&Buffer
,
878 DICompositeType CTy
) {
879 // Get core information.
883 uint64_t Size
= CTy
.getSizeInBits() >> 3;
884 unsigned Tag
= CTy
.getTag();
888 case dwarf::DW_TAG_vector_type
:
889 case dwarf::DW_TAG_array_type
:
890 ConstructArrayTypeDIE(DW_Unit
, Buffer
, &CTy
);
892 case dwarf::DW_TAG_enumeration_type
: {
893 DIArray Elements
= CTy
.getTypeArray();
895 // Add enumerators to enumeration type.
896 for (unsigned i
= 0, N
= Elements
.getNumElements(); i
< N
; ++i
) {
898 DIEnumerator
Enum(Elements
.getElement(i
).getNode());
899 ElemDie
= ConstructEnumTypeDIE(DW_Unit
, &Enum
);
900 Buffer
.AddChild(ElemDie
);
904 case dwarf::DW_TAG_subroutine_type
: {
906 DIArray Elements
= CTy
.getTypeArray();
907 DIDescriptor RTy
= Elements
.getElement(0);
908 AddType(DW_Unit
, &Buffer
, DIType(RTy
.getNode()));
910 // Add prototype flag.
911 AddUInt(&Buffer
, dwarf::DW_AT_prototyped
, dwarf::DW_FORM_flag
, 1);
914 for (unsigned i
= 1, N
= Elements
.getNumElements(); i
< N
; ++i
) {
915 DIE
*Arg
= new DIE(dwarf::DW_TAG_formal_parameter
);
916 DIDescriptor Ty
= Elements
.getElement(i
);
917 AddType(DW_Unit
, Arg
, DIType(Ty
.getNode()));
918 Buffer
.AddChild(Arg
);
922 case dwarf::DW_TAG_structure_type
:
923 case dwarf::DW_TAG_union_type
:
924 case dwarf::DW_TAG_class_type
: {
925 // Add elements to structure type.
926 DIArray Elements
= CTy
.getTypeArray();
928 // A forward struct declared type may not have elements available.
929 if (Elements
.isNull())
932 // Add elements to structure type.
933 for (unsigned i
= 0, N
= Elements
.getNumElements(); i
< N
; ++i
) {
934 DIDescriptor Element
= Elements
.getElement(i
);
935 if (Element
.isNull())
938 if (Element
.getTag() == dwarf::DW_TAG_subprogram
)
939 ElemDie
= CreateSubprogramDIE(DW_Unit
,
940 DISubprogram(Element
.getNode()));
942 ElemDie
= CreateMemberDIE(DW_Unit
,
943 DIDerivedType(Element
.getNode()));
944 Buffer
.AddChild(ElemDie
);
947 if (CTy
.isAppleBlockExtension())
948 AddUInt(&Buffer
, dwarf::DW_AT_APPLE_block
, dwarf::DW_FORM_flag
, 1);
950 unsigned RLang
= CTy
.getRunTimeLang();
952 AddUInt(&Buffer
, dwarf::DW_AT_APPLE_runtime_class
,
953 dwarf::DW_FORM_data1
, RLang
);
960 // Add name if not anonymous or intermediate type.
962 AddString(&Buffer
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
964 if (Tag
== dwarf::DW_TAG_enumeration_type
||
965 Tag
== dwarf::DW_TAG_structure_type
|| Tag
== dwarf::DW_TAG_union_type
) {
966 // Add size if non-zero (derived types might be zero-sized.)
968 AddUInt(&Buffer
, dwarf::DW_AT_byte_size
, 0, Size
);
970 // Add zero size if it is not a forward declaration.
971 if (CTy
.isForwardDecl())
972 AddUInt(&Buffer
, dwarf::DW_AT_declaration
, dwarf::DW_FORM_flag
, 1);
974 AddUInt(&Buffer
, dwarf::DW_AT_byte_size
, 0, 0);
977 // Add source line info if available.
978 if (!CTy
.isForwardDecl())
979 AddSourceLine(&Buffer
, &CTy
);
983 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
984 void DwarfDebug::ConstructSubrangeDIE(DIE
&Buffer
, DISubrange SR
, DIE
*IndexTy
){
985 int64_t L
= SR
.getLo();
986 int64_t H
= SR
.getHi();
987 DIE
*DW_Subrange
= new DIE(dwarf::DW_TAG_subrange_type
);
989 AddDIEEntry(DW_Subrange
, dwarf::DW_AT_type
, dwarf::DW_FORM_ref4
, IndexTy
);
991 AddSInt(DW_Subrange
, dwarf::DW_AT_lower_bound
, 0, L
);
993 AddSInt(DW_Subrange
, dwarf::DW_AT_upper_bound
, 0, H
);
995 Buffer
.AddChild(DW_Subrange
);
998 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
999 void DwarfDebug::ConstructArrayTypeDIE(CompileUnit
*DW_Unit
, DIE
&Buffer
,
1000 DICompositeType
*CTy
) {
1001 Buffer
.setTag(dwarf::DW_TAG_array_type
);
1002 if (CTy
->getTag() == dwarf::DW_TAG_vector_type
)
1003 AddUInt(&Buffer
, dwarf::DW_AT_GNU_vector
, dwarf::DW_FORM_flag
, 1);
1005 // Emit derived type.
1006 AddType(DW_Unit
, &Buffer
, CTy
->getTypeDerivedFrom());
1007 DIArray Elements
= CTy
->getTypeArray();
1009 // Construct an anonymous type for index type.
1010 DIE
IdxBuffer(dwarf::DW_TAG_base_type
);
1011 AddUInt(&IdxBuffer
, dwarf::DW_AT_byte_size
, 0, sizeof(int32_t));
1012 AddUInt(&IdxBuffer
, dwarf::DW_AT_encoding
, dwarf::DW_FORM_data1
,
1013 dwarf::DW_ATE_signed
);
1014 DIE
*IndexTy
= DW_Unit
->AddDie(IdxBuffer
);
1016 // Add subranges to array type.
1017 for (unsigned i
= 0, N
= Elements
.getNumElements(); i
< N
; ++i
) {
1018 DIDescriptor Element
= Elements
.getElement(i
);
1019 if (Element
.getTag() == dwarf::DW_TAG_subrange_type
)
1020 ConstructSubrangeDIE(Buffer
, DISubrange(Element
.getNode()), IndexTy
);
1024 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
1025 DIE
*DwarfDebug::ConstructEnumTypeDIE(CompileUnit
*DW_Unit
, DIEnumerator
*ETy
) {
1026 DIE
*Enumerator
= new DIE(dwarf::DW_TAG_enumerator
);
1029 AddString(Enumerator
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
1030 int64_t Value
= ETy
->getEnumValue();
1031 AddSInt(Enumerator
, dwarf::DW_AT_const_value
, dwarf::DW_FORM_sdata
, Value
);
1035 /// CreateGlobalVariableDIE - Create new DIE using GV.
1036 DIE
*DwarfDebug::CreateGlobalVariableDIE(CompileUnit
*DW_Unit
,
1037 const DIGlobalVariable
&GV
) {
1038 DIE
*GVDie
= new DIE(dwarf::DW_TAG_variable
);
1040 GV
.getDisplayName(Name
);
1041 AddString(GVDie
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
1042 std::string LinkageName
;
1043 GV
.getLinkageName(LinkageName
);
1044 if (!LinkageName
.empty()) {
1045 // Skip special LLVM prefix that is used to inform the asm printer to not
1046 // emit usual symbol prefix before the symbol name. This happens for
1047 // Objective-C symbol names and symbol whose name is replaced using GCC's
1048 // __asm__ attribute.
1049 if (LinkageName
[0] == 1)
1050 LinkageName
= &LinkageName
[1];
1051 AddString(GVDie
, dwarf::DW_AT_MIPS_linkage_name
, dwarf::DW_FORM_string
,
1054 AddType(DW_Unit
, GVDie
, GV
.getType());
1055 if (!GV
.isLocalToUnit())
1056 AddUInt(GVDie
, dwarf::DW_AT_external
, dwarf::DW_FORM_flag
, 1);
1057 AddSourceLine(GVDie
, &GV
);
1061 /// CreateMemberDIE - Create new member DIE.
1062 DIE
*DwarfDebug::CreateMemberDIE(CompileUnit
*DW_Unit
, const DIDerivedType
&DT
){
1063 DIE
*MemberDie
= new DIE(DT
.getTag());
1067 AddString(MemberDie
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
1069 AddType(DW_Unit
, MemberDie
, DT
.getTypeDerivedFrom());
1071 AddSourceLine(MemberDie
, &DT
);
1073 uint64_t Size
= DT
.getSizeInBits();
1074 uint64_t FieldSize
= DT
.getOriginalTypeSize();
1076 if (Size
!= FieldSize
) {
1078 AddUInt(MemberDie
, dwarf::DW_AT_byte_size
, 0, DT
.getOriginalTypeSize()>>3);
1079 AddUInt(MemberDie
, dwarf::DW_AT_bit_size
, 0, DT
.getSizeInBits());
1081 uint64_t Offset
= DT
.getOffsetInBits();
1082 uint64_t FieldOffset
= Offset
;
1083 uint64_t AlignMask
= ~(DT
.getAlignInBits() - 1);
1084 uint64_t HiMark
= (Offset
+ FieldSize
) & AlignMask
;
1085 FieldOffset
= (HiMark
- FieldSize
);
1086 Offset
-= FieldOffset
;
1088 // Maybe we need to work from the other end.
1089 if (TD
->isLittleEndian()) Offset
= FieldSize
- (Offset
+ Size
);
1090 AddUInt(MemberDie
, dwarf::DW_AT_bit_offset
, 0, Offset
);
1093 DIEBlock
*Block
= new DIEBlock();
1094 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_plus_uconst
);
1095 AddUInt(Block
, 0, dwarf::DW_FORM_udata
, DT
.getOffsetInBits() >> 3);
1096 AddBlock(MemberDie
, dwarf::DW_AT_data_member_location
, 0, Block
);
1098 if (DT
.isProtected())
1099 AddUInt(MemberDie
, dwarf::DW_AT_accessibility
, 0,
1100 dwarf::DW_ACCESS_protected
);
1101 else if (DT
.isPrivate())
1102 AddUInt(MemberDie
, dwarf::DW_AT_accessibility
, 0,
1103 dwarf::DW_ACCESS_private
);
1108 /// CreateSubprogramDIE - Create new DIE using SP.
1109 DIE
*DwarfDebug::CreateSubprogramDIE(CompileUnit
*DW_Unit
,
1110 const DISubprogram
&SP
,
1113 DIE
*SPDie
= new DIE(dwarf::DW_TAG_subprogram
);
1117 AddString(SPDie
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
1119 std::string LinkageName
;
1120 SP
.getLinkageName(LinkageName
);
1121 if (!LinkageName
.empty()) {
1122 // Skip special LLVM prefix that is used to inform the asm printer to not emit
1123 // usual symbol prefix before the symbol name. This happens for Objective-C
1124 // symbol names and symbol whose name is replaced using GCC's __asm__ attribute.
1125 if (LinkageName
[0] == 1)
1126 LinkageName
= &LinkageName
[1];
1127 AddString(SPDie
, dwarf::DW_AT_MIPS_linkage_name
, dwarf::DW_FORM_string
,
1130 AddSourceLine(SPDie
, &SP
);
1132 DICompositeType SPTy
= SP
.getType();
1133 DIArray Args
= SPTy
.getTypeArray();
1135 // Add prototyped tag, if C or ObjC.
1136 unsigned Lang
= SP
.getCompileUnit().getLanguage();
1137 if (Lang
== dwarf::DW_LANG_C99
|| Lang
== dwarf::DW_LANG_C89
||
1138 Lang
== dwarf::DW_LANG_ObjC
)
1139 AddUInt(SPDie
, dwarf::DW_AT_prototyped
, dwarf::DW_FORM_flag
, 1);
1142 unsigned SPTag
= SPTy
.getTag();
1143 if (!IsConstructor
) {
1144 if (Args
.isNull() || SPTag
!= dwarf::DW_TAG_subroutine_type
)
1145 AddType(DW_Unit
, SPDie
, SPTy
);
1147 AddType(DW_Unit
, SPDie
, DIType(Args
.getElement(0).getNode()));
1150 if (!SP
.isDefinition()) {
1151 AddUInt(SPDie
, dwarf::DW_AT_declaration
, dwarf::DW_FORM_flag
, 1);
1153 // Add arguments. Do not add arguments for subprogram definition. They will
1154 // be handled through RecordVariable.
1155 if (SPTag
== dwarf::DW_TAG_subroutine_type
)
1156 for (unsigned i
= 1, N
= Args
.getNumElements(); i
< N
; ++i
) {
1157 DIE
*Arg
= new DIE(dwarf::DW_TAG_formal_parameter
);
1158 AddType(DW_Unit
, Arg
, DIType(Args
.getElement(i
).getNode()));
1159 AddUInt(Arg
, dwarf::DW_AT_artificial
, dwarf::DW_FORM_flag
, 1); // ??
1160 SPDie
->AddChild(Arg
);
1164 if (!SP
.isLocalToUnit() && !IsInlined
)
1165 AddUInt(SPDie
, dwarf::DW_AT_external
, dwarf::DW_FORM_flag
, 1);
1167 // DW_TAG_inlined_subroutine may refer to this DIE.
1168 DIE
*&Slot
= DW_Unit
->getDieMapSlotFor(SP
.getNode());
1173 /// FindCompileUnit - Get the compile unit for the given descriptor.
1175 CompileUnit
&DwarfDebug::FindCompileUnit(DICompileUnit Unit
) const {
1176 DenseMap
<Value
*, CompileUnit
*>::const_iterator I
=
1177 CompileUnitMap
.find(Unit
.getNode());
1178 assert(I
!= CompileUnitMap
.end() && "Missing compile unit.");
1182 /// CreateDbgScopeVariable - Create a new scope variable.
1184 DIE
*DwarfDebug::CreateDbgScopeVariable(DbgVariable
*DV
, CompileUnit
*Unit
) {
1185 // Get the descriptor.
1186 const DIVariable
&VD
= DV
->getVariable();
1188 // Translate tag to proper Dwarf tag. The result variable is dropped for
1191 switch (VD
.getTag()) {
1192 case dwarf::DW_TAG_return_variable
:
1194 case dwarf::DW_TAG_arg_variable
:
1195 Tag
= dwarf::DW_TAG_formal_parameter
;
1197 case dwarf::DW_TAG_auto_variable
: // fall thru
1199 Tag
= dwarf::DW_TAG_variable
;
1203 // Define variable debug information entry.
1204 DIE
*VariableDie
= new DIE(Tag
);
1207 AddString(VariableDie
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, Name
);
1209 // Add source line info if available.
1210 AddSourceLine(VariableDie
, &VD
);
1212 // Add variable type.
1213 if (VD
.isBlockByrefVariable())
1214 AddType(Unit
, VariableDie
, GetBlockByrefType(VD
.getType(), Name
));
1216 AddType(Unit
, VariableDie
, VD
.getType());
1218 // Add variable address.
1219 if (!DV
->isInlinedFnVar()) {
1220 // Variables for abstract instances of inlined functions don't get a
1222 MachineLocation Location
;
1223 Location
.set(RI
->getFrameRegister(*MF
),
1224 RI
->getFrameIndexOffset(*MF
, DV
->getFrameIndex()));
1226 if (VD
.isBlockByrefVariable())
1227 AddBlockByrefAddress (DV
, VariableDie
, dwarf::DW_AT_location
, Location
);
1229 AddAddress(VariableDie
, dwarf::DW_AT_location
, Location
);
1235 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1237 DbgScope
*DwarfDebug::getOrCreateScope(MDNode
*N
) {
1238 DbgScope
*&Slot
= DbgScopeMap
[N
];
1239 if (Slot
) return Slot
;
1241 DbgScope
*Parent
= NULL
;
1242 DILexicalBlock
Block(N
);
1244 // Don't create a new scope if we already created one for an inlined function.
1245 DenseMap
<const MDNode
*, DbgScope
*>::iterator
1246 II
= AbstractInstanceRootMap
.find(N
);
1247 if (II
!= AbstractInstanceRootMap
.end())
1248 return LexicalScopeStack
.back();
1250 if (!Block
.isNull()) {
1251 DIDescriptor ParentDesc
= Block
.getContext();
1253 ParentDesc
.isNull() ? NULL
: getOrCreateScope(ParentDesc
.getNode());
1256 Slot
= new DbgScope(Parent
, DIDescriptor(N
));
1259 Parent
->AddScope(Slot
);
1261 // First function is top level function.
1262 FunctionDbgScope
= Slot
;
1267 /// ConstructDbgScope - Construct the components of a scope.
1269 void DwarfDebug::ConstructDbgScope(DbgScope
*ParentScope
,
1270 unsigned ParentStartID
,
1271 unsigned ParentEndID
,
1272 DIE
*ParentDie
, CompileUnit
*Unit
) {
1273 // Add variables to scope.
1274 SmallVector
<DbgVariable
*, 8> &Variables
= ParentScope
->getVariables();
1275 for (unsigned i
= 0, N
= Variables
.size(); i
< N
; ++i
) {
1276 DIE
*VariableDie
= CreateDbgScopeVariable(Variables
[i
], Unit
);
1277 if (VariableDie
) ParentDie
->AddChild(VariableDie
);
1280 // Add concrete instances to scope.
1281 SmallVector
<DbgConcreteScope
*, 8> &ConcreteInsts
=
1282 ParentScope
->getConcreteInsts();
1283 for (unsigned i
= 0, N
= ConcreteInsts
.size(); i
< N
; ++i
) {
1284 DbgConcreteScope
*ConcreteInst
= ConcreteInsts
[i
];
1285 DIE
*Die
= ConcreteInst
->getDie();
1287 unsigned StartID
= ConcreteInst
->getStartLabelID();
1288 unsigned EndID
= ConcreteInst
->getEndLabelID();
1290 // Add the scope bounds.
1292 AddLabel(Die
, dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
,
1293 DWLabel("label", StartID
));
1295 AddLabel(Die
, dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
,
1296 DWLabel("func_begin", SubprogramCount
));
1299 AddLabel(Die
, dwarf::DW_AT_high_pc
, dwarf::DW_FORM_addr
,
1300 DWLabel("label", EndID
));
1302 AddLabel(Die
, dwarf::DW_AT_high_pc
, dwarf::DW_FORM_addr
,
1303 DWLabel("func_end", SubprogramCount
));
1305 ParentDie
->AddChild(Die
);
1308 // Add nested scopes.
1309 SmallVector
<DbgScope
*, 4> &Scopes
= ParentScope
->getScopes();
1310 for (unsigned j
= 0, M
= Scopes
.size(); j
< M
; ++j
) {
1311 // Define the Scope debug information entry.
1312 DbgScope
*Scope
= Scopes
[j
];
1314 unsigned StartID
= MMI
->MappedLabel(Scope
->getStartLabelID());
1315 unsigned EndID
= MMI
->MappedLabel(Scope
->getEndLabelID());
1317 // Ignore empty scopes.
1318 if (StartID
== EndID
&& StartID
!= 0) continue;
1320 // Do not ignore inlined scopes even if they don't have any variables or
1322 if (Scope
->getScopes().empty() && Scope
->getVariables().empty() &&
1323 Scope
->getConcreteInsts().empty())
1326 if (StartID
== ParentStartID
&& EndID
== ParentEndID
) {
1327 // Just add stuff to the parent scope.
1328 ConstructDbgScope(Scope
, ParentStartID
, ParentEndID
, ParentDie
, Unit
);
1330 DIE
*ScopeDie
= new DIE(dwarf::DW_TAG_lexical_block
);
1332 // Add the scope bounds.
1334 AddLabel(ScopeDie
, dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
,
1335 DWLabel("label", StartID
));
1337 AddLabel(ScopeDie
, dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
,
1338 DWLabel("func_begin", SubprogramCount
));
1341 AddLabel(ScopeDie
, dwarf::DW_AT_high_pc
, dwarf::DW_FORM_addr
,
1342 DWLabel("label", EndID
));
1344 AddLabel(ScopeDie
, dwarf::DW_AT_high_pc
, dwarf::DW_FORM_addr
,
1345 DWLabel("func_end", SubprogramCount
));
1347 // Add the scope's contents.
1348 ConstructDbgScope(Scope
, StartID
, EndID
, ScopeDie
, Unit
);
1349 ParentDie
->AddChild(ScopeDie
);
1354 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
1356 void DwarfDebug::ConstructFunctionDbgScope(DbgScope
*RootScope
,
1357 bool AbstractScope
) {
1358 // Exit if there is no root scope.
1359 if (!RootScope
) return;
1360 DIDescriptor Desc
= RootScope
->getDesc();
1364 // Get the subprogram debug information entry.
1365 DISubprogram
SPD(Desc
.getNode());
1367 // Get the subprogram die.
1368 DIE
*SPDie
= ModuleCU
->getDieMapSlotFor(SPD
.getNode());
1369 assert(SPDie
&& "Missing subprogram descriptor");
1371 if (!AbstractScope
) {
1372 // Add the function bounds.
1373 AddLabel(SPDie
, dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
,
1374 DWLabel("func_begin", SubprogramCount
));
1375 AddLabel(SPDie
, dwarf::DW_AT_high_pc
, dwarf::DW_FORM_addr
,
1376 DWLabel("func_end", SubprogramCount
));
1377 MachineLocation
Location(RI
->getFrameRegister(*MF
));
1378 AddAddress(SPDie
, dwarf::DW_AT_frame_base
, Location
);
1381 ConstructDbgScope(RootScope
, 0, 0, SPDie
, ModuleCU
);
1384 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
1386 void DwarfDebug::ConstructDefaultDbgScope(MachineFunction
*MF
) {
1387 StringMap
<DIE
*> &Globals
= ModuleCU
->getGlobals();
1388 StringMap
<DIE
*>::iterator GI
= Globals
.find(MF
->getFunction()->getName());
1389 if (GI
!= Globals
.end()) {
1390 DIE
*SPDie
= GI
->second
;
1392 // Add the function bounds.
1393 AddLabel(SPDie
, dwarf::DW_AT_low_pc
, dwarf::DW_FORM_addr
,
1394 DWLabel("func_begin", SubprogramCount
));
1395 AddLabel(SPDie
, dwarf::DW_AT_high_pc
, dwarf::DW_FORM_addr
,
1396 DWLabel("func_end", SubprogramCount
));
1398 MachineLocation
Location(RI
->getFrameRegister(*MF
));
1399 AddAddress(SPDie
, dwarf::DW_AT_frame_base
, Location
);
1403 /// GetOrCreateSourceID - Look up the source id with the given directory and
1404 /// source file names. If none currently exists, create a new id and insert it
1405 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
1407 unsigned DwarfDebug::GetOrCreateSourceID(const std::string
&DirName
,
1408 const std::string
&FileName
) {
1410 StringMap
<unsigned>::iterator DI
= DirectoryIdMap
.find(DirName
);
1411 if (DI
!= DirectoryIdMap
.end()) {
1412 DId
= DI
->getValue();
1414 DId
= DirectoryNames
.size() + 1;
1415 DirectoryIdMap
[DirName
] = DId
;
1416 DirectoryNames
.push_back(DirName
);
1420 StringMap
<unsigned>::iterator FI
= SourceFileIdMap
.find(FileName
);
1421 if (FI
!= SourceFileIdMap
.end()) {
1422 FId
= FI
->getValue();
1424 FId
= SourceFileNames
.size() + 1;
1425 SourceFileIdMap
[FileName
] = FId
;
1426 SourceFileNames
.push_back(FileName
);
1429 DenseMap
<std::pair
<unsigned, unsigned>, unsigned>::iterator SI
=
1430 SourceIdMap
.find(std::make_pair(DId
, FId
));
1431 if (SI
!= SourceIdMap
.end())
1434 unsigned SrcId
= SourceIds
.size() + 1; // DW_AT_decl_file cannot be 0.
1435 SourceIdMap
[std::make_pair(DId
, FId
)] = SrcId
;
1436 SourceIds
.push_back(std::make_pair(DId
, FId
));
1441 void DwarfDebug::ConstructCompileUnit(MDNode
*N
) {
1442 DICompileUnit
DIUnit(N
);
1443 std::string Dir
, FN
, Prod
;
1444 unsigned ID
= GetOrCreateSourceID(DIUnit
.getDirectory(Dir
),
1445 DIUnit
.getFilename(FN
));
1447 DIE
*Die
= new DIE(dwarf::DW_TAG_compile_unit
);
1448 AddSectionOffset(Die
, dwarf::DW_AT_stmt_list
, dwarf::DW_FORM_data4
,
1449 DWLabel("section_line", 0), DWLabel("section_line", 0),
1451 AddString(Die
, dwarf::DW_AT_producer
, dwarf::DW_FORM_string
,
1452 DIUnit
.getProducer(Prod
));
1453 AddUInt(Die
, dwarf::DW_AT_language
, dwarf::DW_FORM_data1
,
1454 DIUnit
.getLanguage());
1455 AddString(Die
, dwarf::DW_AT_name
, dwarf::DW_FORM_string
, FN
);
1458 AddString(Die
, dwarf::DW_AT_comp_dir
, dwarf::DW_FORM_string
, Dir
);
1459 if (DIUnit
.isOptimized())
1460 AddUInt(Die
, dwarf::DW_AT_APPLE_optimized
, dwarf::DW_FORM_flag
, 1);
1463 DIUnit
.getFlags(Flags
);
1465 AddString(Die
, dwarf::DW_AT_APPLE_flags
, dwarf::DW_FORM_string
, Flags
);
1467 unsigned RVer
= DIUnit
.getRunTimeVersion();
1469 AddUInt(Die
, dwarf::DW_AT_APPLE_major_runtime_vers
,
1470 dwarf::DW_FORM_data1
, RVer
);
1472 CompileUnit
*Unit
= new CompileUnit(ID
, Die
);
1473 if (!ModuleCU
&& DIUnit
.isMain()) {
1474 // Use first compile unit marked as isMain as the compile unit
1479 CompileUnitMap
[DIUnit
.getNode()] = Unit
;
1480 CompileUnits
.push_back(Unit
);
1483 void DwarfDebug::ConstructGlobalVariableDIE(MDNode
*N
) {
1484 DIGlobalVariable
DI_GV(N
);
1486 // If debug information is malformed then ignore it.
1487 if (DI_GV
.Verify() == false)
1490 // Check for pre-existence.
1491 DIE
*&Slot
= ModuleCU
->getDieMapSlotFor(DI_GV
.getNode());
1495 DIE
*VariableDie
= CreateGlobalVariableDIE(ModuleCU
, DI_GV
);
1498 DIEBlock
*Block
= new DIEBlock();
1499 AddUInt(Block
, 0, dwarf::DW_FORM_data1
, dwarf::DW_OP_addr
);
1501 AddObjectLabel(Block
, 0, dwarf::DW_FORM_udata
,
1502 Asm
->getGlobalLinkName(DI_GV
.getGlobal(), GLN
));
1503 AddBlock(VariableDie
, dwarf::DW_AT_location
, 0, Block
);
1508 // Add to context owner.
1509 ModuleCU
->getDie()->AddChild(VariableDie
);
1511 // Expose as global. FIXME - need to check external flag.
1513 ModuleCU
->AddGlobal(DI_GV
.getName(Name
), VariableDie
);
1517 void DwarfDebug::ConstructSubprogram(MDNode
*N
) {
1520 // Check for pre-existence.
1521 DIE
*&Slot
= ModuleCU
->getDieMapSlotFor(N
);
1525 if (!SP
.isDefinition())
1526 // This is a method declaration which will be handled while constructing
1530 DIE
*SubprogramDie
= CreateSubprogramDIE(ModuleCU
, SP
);
1533 Slot
= SubprogramDie
;
1535 // Add to context owner.
1536 ModuleCU
->getDie()->AddChild(SubprogramDie
);
1538 // Expose as global.
1540 ModuleCU
->AddGlobal(SP
.getName(Name
), SubprogramDie
);
1544 /// BeginModule - Emit all Dwarf sections that should come prior to the
1545 /// content. Create global DIEs and emit initial debug info sections.
1546 /// This is inovked by the target AsmPrinter.
1547 void DwarfDebug::BeginModule(Module
*M
, MachineModuleInfo
*mmi
) {
1550 if (TimePassesIsEnabled
)
1551 DebugTimer
->startTimer();
1553 DebugInfoFinder DbgFinder
;
1554 DbgFinder
.processModule(*M
);
1556 // Create all the compile unit DIEs.
1557 for (DebugInfoFinder::iterator I
= DbgFinder
.compile_unit_begin(),
1558 E
= DbgFinder
.compile_unit_end(); I
!= E
; ++I
)
1559 ConstructCompileUnit(*I
);
1561 if (CompileUnits
.empty()) {
1562 if (TimePassesIsEnabled
)
1563 DebugTimer
->stopTimer();
1568 // If main compile unit for this module is not seen than randomly
1569 // select first compile unit.
1571 ModuleCU
= CompileUnits
[0];
1573 // If there is not any debug info available for any global variables and any
1574 // subprograms then there is not any debug info to emit.
1575 if (DbgFinder
.global_variable_count() == 0
1576 && DbgFinder
.subprogram_count() == 0) {
1577 if (TimePassesIsEnabled
)
1578 DebugTimer
->stopTimer();
1582 // Create DIEs for each of the externally visible global variables.
1583 for (DebugInfoFinder::iterator I
= DbgFinder
.global_variable_begin(),
1584 E
= DbgFinder
.global_variable_end(); I
!= E
; ++I
)
1585 ConstructGlobalVariableDIE(*I
);
1587 // Create DIEs for each of the externally visible subprograms.
1588 for (DebugInfoFinder::iterator I
= DbgFinder
.subprogram_begin(),
1589 E
= DbgFinder
.subprogram_end(); I
!= E
; ++I
)
1590 ConstructSubprogram(*I
);
1594 MMI
->setDebugInfoAvailability(true);
1596 // Prime section data.
1597 SectionMap
.insert(Asm
->getObjFileLowering().getTextSection());
1599 // Print out .file directives to specify files for .loc directives. These are
1600 // printed out early so that they precede any .loc directives.
1601 if (MAI
->hasDotLocAndDotFile()) {
1602 for (unsigned i
= 1, e
= getNumSourceIds()+1; i
!= e
; ++i
) {
1603 // Remember source id starts at 1.
1604 std::pair
<unsigned, unsigned> Id
= getSourceDirectoryAndFileIds(i
);
1605 sys::Path
FullPath(getSourceDirectoryName(Id
.first
));
1607 FullPath
.appendComponent(getSourceFileName(Id
.second
));
1608 assert(AppendOk
&& "Could not append filename to directory!");
1610 Asm
->EmitFile(i
, FullPath
.str());
1615 // Emit initial sections
1618 if (TimePassesIsEnabled
)
1619 DebugTimer
->stopTimer();
1622 /// EndModule - Emit all Dwarf sections that should come after the content.
1624 void DwarfDebug::EndModule() {
1625 if (!ShouldEmitDwarfDebug())
1628 if (TimePassesIsEnabled
)
1629 DebugTimer
->startTimer();
1631 // Standard sections final addresses.
1632 Asm
->OutStreamer
.SwitchSection(Asm
->getObjFileLowering().getTextSection());
1633 EmitLabel("text_end", 0);
1634 Asm
->OutStreamer
.SwitchSection(Asm
->getObjFileLowering().getDataSection());
1635 EmitLabel("data_end", 0);
1637 // End text sections.
1638 for (unsigned i
= 1, N
= SectionMap
.size(); i
<= N
; ++i
) {
1639 Asm
->OutStreamer
.SwitchSection(SectionMap
[i
]);
1640 EmitLabel("section_end", i
);
1643 // Emit common frame information.
1644 EmitCommonDebugFrame();
1646 // Emit function debug frame information
1647 for (std::vector
<FunctionDebugFrameInfo
>::iterator I
= DebugFrames
.begin(),
1648 E
= DebugFrames
.end(); I
!= E
; ++I
)
1649 EmitFunctionDebugFrame(*I
);
1651 // Compute DIE offsets and sizes.
1654 // Emit all the DIEs into a debug info section
1657 // Corresponding abbreviations into a abbrev section.
1658 EmitAbbreviations();
1660 // Emit source line correspondence into a debug line section.
1663 // Emit info into a debug pubnames section.
1664 EmitDebugPubNames();
1666 // Emit info into a debug str section.
1669 // Emit info into a debug loc section.
1672 // Emit info into a debug aranges section.
1675 // Emit info into a debug ranges section.
1678 // Emit info into a debug macinfo section.
1681 // Emit inline info.
1682 EmitDebugInlineInfo();
1684 if (TimePassesIsEnabled
)
1685 DebugTimer
->stopTimer();
1688 /// BeginFunction - Gather pre-function debug information. Assumes being
1689 /// emitted immediately after the function entry point.
1690 void DwarfDebug::BeginFunction(MachineFunction
*MF
) {
1693 if (!ShouldEmitDwarfDebug()) return;
1695 if (TimePassesIsEnabled
)
1696 DebugTimer
->startTimer();
1698 // Begin accumulating function debug information.
1699 MMI
->BeginFunction(MF
);
1701 // Assumes in correct section after the entry point.
1702 EmitLabel("func_begin", ++SubprogramCount
);
1704 // Emit label for the implicitly defined dbg.stoppoint at the start of the
1706 DebugLoc FDL
= MF
->getDefaultDebugLoc();
1707 if (!FDL
.isUnknown()) {
1708 DebugLocTuple DLT
= MF
->getDebugLocTuple(FDL
);
1709 unsigned LabelID
= RecordSourceLine(DLT
.Line
, DLT
.Col
,
1710 DICompileUnit(DLT
.CompileUnit
));
1711 Asm
->printLabel(LabelID
);
1715 if (TimePassesIsEnabled
)
1716 DebugTimer
->stopTimer();
1719 /// EndFunction - Gather and emit post-function debug information.
1721 void DwarfDebug::EndFunction(MachineFunction
*MF
) {
1722 if (!ShouldEmitDwarfDebug()) return;
1724 if (TimePassesIsEnabled
)
1725 DebugTimer
->startTimer();
1727 // Define end label for subprogram.
1728 EmitLabel("func_end", SubprogramCount
);
1730 // Get function line info.
1731 if (!Lines
.empty()) {
1732 // Get section line info.
1733 unsigned ID
= SectionMap
.insert(Asm
->getCurrentSection());
1734 if (SectionSourceLines
.size() < ID
) SectionSourceLines
.resize(ID
);
1735 std::vector
<SrcLineInfo
> &SectionLineInfos
= SectionSourceLines
[ID
-1];
1736 // Append the function info to section info.
1737 SectionLineInfos
.insert(SectionLineInfos
.end(),
1738 Lines
.begin(), Lines
.end());
1741 // Construct the DbgScope for abstract instances.
1742 for (SmallVector
<DbgScope
*, 32>::iterator
1743 I
= AbstractInstanceRootList
.begin(),
1744 E
= AbstractInstanceRootList
.end(); I
!= E
; ++I
)
1745 ConstructFunctionDbgScope(*I
);
1747 // Construct scopes for subprogram.
1748 if (FunctionDbgScope
)
1749 ConstructFunctionDbgScope(FunctionDbgScope
);
1751 // FIXME: This is wrong. We are essentially getting past a problem with
1752 // debug information not being able to handle unreachable blocks that have
1753 // debug information in them. In particular, those unreachable blocks that
1754 // have "region end" info in them. That situation results in the "root
1755 // scope" not being created. If that's the case, then emit a "default"
1756 // scope, i.e., one that encompasses the whole function. This isn't
1757 // desirable. And a better way of handling this (and all of the debugging
1758 // information) needs to be explored.
1759 ConstructDefaultDbgScope(MF
);
1761 DebugFrames
.push_back(FunctionDebugFrameInfo(SubprogramCount
,
1762 MMI
->getFrameMoves()));
1765 if (FunctionDbgScope
) {
1766 delete FunctionDbgScope
;
1767 DbgScopeMap
.clear();
1768 DbgAbstractScopeMap
.clear();
1769 DbgConcreteScopeMap
.clear();
1770 FunctionDbgScope
= NULL
;
1771 LexicalScopeStack
.clear();
1772 AbstractInstanceRootList
.clear();
1773 AbstractInstanceRootMap
.clear();
1778 if (TimePassesIsEnabled
)
1779 DebugTimer
->stopTimer();
1782 /// RecordSourceLine - Records location information and associates it with a
1783 /// label. Returns a unique label ID used to generate a label and provide
1784 /// correspondence to the source line list.
1785 unsigned DwarfDebug::RecordSourceLine(Value
*V
, unsigned Line
, unsigned Col
) {
1786 if (TimePassesIsEnabled
)
1787 DebugTimer
->startTimer();
1789 CompileUnit
*Unit
= CompileUnitMap
[V
];
1790 assert(Unit
&& "Unable to find CompileUnit");
1791 unsigned ID
= MMI
->NextLabelID();
1792 Lines
.push_back(SrcLineInfo(Line
, Col
, Unit
->getID(), ID
));
1794 if (TimePassesIsEnabled
)
1795 DebugTimer
->stopTimer();
1800 /// RecordSourceLine - Records location information and associates it with a
1801 /// label. Returns a unique label ID used to generate a label and provide
1802 /// correspondence to the source line list.
1803 unsigned DwarfDebug::RecordSourceLine(unsigned Line
, unsigned Col
,
1808 if (TimePassesIsEnabled
)
1809 DebugTimer
->startTimer();
1811 std::string Dir
, Fn
;
1812 unsigned Src
= GetOrCreateSourceID(CU
.getDirectory(Dir
),
1813 CU
.getFilename(Fn
));
1814 unsigned ID
= MMI
->NextLabelID();
1815 Lines
.push_back(SrcLineInfo(Line
, Col
, Src
, ID
));
1817 if (TimePassesIsEnabled
)
1818 DebugTimer
->stopTimer();
1823 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
1824 /// timed. Look up the source id with the given directory and source file
1825 /// names. If none currently exists, create a new id and insert it in the
1826 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
1828 unsigned DwarfDebug::getOrCreateSourceID(const std::string
&DirName
,
1829 const std::string
&FileName
) {
1830 if (TimePassesIsEnabled
)
1831 DebugTimer
->startTimer();
1833 unsigned SrcId
= GetOrCreateSourceID(DirName
, FileName
);
1835 if (TimePassesIsEnabled
)
1836 DebugTimer
->stopTimer();
1841 /// RecordRegionStart - Indicate the start of a region.
1842 unsigned DwarfDebug::RecordRegionStart(MDNode
*N
) {
1843 if (TimePassesIsEnabled
)
1844 DebugTimer
->startTimer();
1846 DbgScope
*Scope
= getOrCreateScope(N
);
1847 unsigned ID
= MMI
->NextLabelID();
1848 if (!Scope
->getStartLabelID()) Scope
->setStartLabelID(ID
);
1849 LexicalScopeStack
.push_back(Scope
);
1851 if (TimePassesIsEnabled
)
1852 DebugTimer
->stopTimer();
1857 /// RecordRegionEnd - Indicate the end of a region.
1858 unsigned DwarfDebug::RecordRegionEnd(MDNode
*N
) {
1859 if (TimePassesIsEnabled
)
1860 DebugTimer
->startTimer();
1862 DbgScope
*Scope
= getOrCreateScope(N
);
1863 unsigned ID
= MMI
->NextLabelID();
1864 Scope
->setEndLabelID(ID
);
1865 // FIXME : region.end() may not be in the last basic block.
1866 // For now, do not pop last lexical scope because next basic
1867 // block may start new inlined function's body.
1868 unsigned LSSize
= LexicalScopeStack
.size();
1869 if (LSSize
!= 0 && LSSize
!= 1)
1870 LexicalScopeStack
.pop_back();
1872 if (TimePassesIsEnabled
)
1873 DebugTimer
->stopTimer();
1878 /// RecordVariable - Indicate the declaration of a local variable.
1879 void DwarfDebug::RecordVariable(MDNode
*N
, unsigned FrameIndex
) {
1880 if (TimePassesIsEnabled
)
1881 DebugTimer
->startTimer();
1883 DIDescriptor
Desc(N
);
1884 DbgScope
*Scope
= NULL
;
1885 bool InlinedFnVar
= false;
1887 if (Desc
.getTag() == dwarf::DW_TAG_variable
)
1888 Scope
= getOrCreateScope(DIGlobalVariable(N
).getContext().getNode());
1890 bool InlinedVar
= false;
1891 MDNode
*Context
= DIVariable(N
).getContext().getNode();
1892 DISubprogram
SP(Context
);
1894 // SP is inserted into DbgAbstractScopeMap when inlined function
1895 // start was recorded by RecordInlineFnStart.
1896 DenseMap
<MDNode
*, DbgScope
*>::iterator
1897 I
= DbgAbstractScopeMap
.find(SP
.getNode());
1898 if (I
!= DbgAbstractScopeMap
.end()) {
1904 Scope
= getOrCreateScope(Context
);
1907 assert(Scope
&& "Unable to find the variable's scope");
1908 DbgVariable
*DV
= new DbgVariable(DIVariable(N
), FrameIndex
, InlinedFnVar
);
1909 Scope
->AddVariable(DV
);
1911 if (TimePassesIsEnabled
)
1912 DebugTimer
->stopTimer();
1915 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
1916 unsigned DwarfDebug::RecordInlinedFnStart(DISubprogram
&SP
, DICompileUnit CU
,
1917 unsigned Line
, unsigned Col
) {
1918 unsigned LabelID
= MMI
->NextLabelID();
1920 if (!MAI
->doesDwarfUsesInlineInfoSection())
1923 if (TimePassesIsEnabled
)
1924 DebugTimer
->startTimer();
1926 MDNode
*Node
= SP
.getNode();
1927 DenseMap
<const MDNode
*, DbgScope
*>::iterator
1928 II
= AbstractInstanceRootMap
.find(Node
);
1930 if (II
== AbstractInstanceRootMap
.end()) {
1931 // Create an abstract instance entry for this inlined function if it doesn't
1933 DbgScope
*Scope
= new DbgScope(NULL
, DIDescriptor(Node
));
1935 // Get the compile unit context.
1936 DIE
*SPDie
= ModuleCU
->getDieMapSlotFor(Node
);
1938 SPDie
= CreateSubprogramDIE(ModuleCU
, SP
, false, true);
1940 // Mark as being inlined. This makes this subprogram entry an abstract
1942 // FIXME: Our debugger doesn't care about the value of DW_AT_inline, only
1943 // that it's defined. That probably won't change in the future. However,
1944 // this could be more elegant.
1945 AddUInt(SPDie
, dwarf::DW_AT_inline
, 0, dwarf::DW_INL_declared_not_inlined
);
1947 // Keep track of the abstract scope for this function.
1948 DbgAbstractScopeMap
[Node
] = Scope
;
1950 AbstractInstanceRootMap
[Node
] = Scope
;
1951 AbstractInstanceRootList
.push_back(Scope
);
1954 // Create a concrete inlined instance for this inlined function.
1955 DbgConcreteScope
*ConcreteScope
= new DbgConcreteScope(DIDescriptor(Node
));
1956 DIE
*ScopeDie
= new DIE(dwarf::DW_TAG_inlined_subroutine
);
1957 ScopeDie
->setAbstractCompileUnit(ModuleCU
);
1959 DIE
*Origin
= ModuleCU
->getDieMapSlotFor(Node
);
1960 AddDIEEntry(ScopeDie
, dwarf::DW_AT_abstract_origin
,
1961 dwarf::DW_FORM_ref4
, Origin
);
1962 AddUInt(ScopeDie
, dwarf::DW_AT_call_file
, 0, ModuleCU
->getID());
1963 AddUInt(ScopeDie
, dwarf::DW_AT_call_line
, 0, Line
);
1964 AddUInt(ScopeDie
, dwarf::DW_AT_call_column
, 0, Col
);
1966 ConcreteScope
->setDie(ScopeDie
);
1967 ConcreteScope
->setStartLabelID(LabelID
);
1968 MMI
->RecordUsedDbgLabel(LabelID
);
1970 LexicalScopeStack
.back()->AddConcreteInst(ConcreteScope
);
1972 // Keep track of the concrete scope that's inlined into this function.
1973 DenseMap
<MDNode
*, SmallVector
<DbgScope
*, 8> >::iterator
1974 SI
= DbgConcreteScopeMap
.find(Node
);
1976 if (SI
== DbgConcreteScopeMap
.end())
1977 DbgConcreteScopeMap
[Node
].push_back(ConcreteScope
);
1979 SI
->second
.push_back(ConcreteScope
);
1981 // Track the start label for this inlined function.
1982 DenseMap
<MDNode
*, SmallVector
<unsigned, 4> >::iterator
1983 I
= InlineInfo
.find(Node
);
1985 if (I
== InlineInfo
.end())
1986 InlineInfo
[Node
].push_back(LabelID
);
1988 I
->second
.push_back(LabelID
);
1990 if (TimePassesIsEnabled
)
1991 DebugTimer
->stopTimer();
1996 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
1997 unsigned DwarfDebug::RecordInlinedFnEnd(DISubprogram
&SP
) {
1998 if (!MAI
->doesDwarfUsesInlineInfoSection())
2001 if (TimePassesIsEnabled
)
2002 DebugTimer
->startTimer();
2004 MDNode
*Node
= SP
.getNode();
2005 DenseMap
<MDNode
*, SmallVector
<DbgScope
*, 8> >::iterator
2006 I
= DbgConcreteScopeMap
.find(Node
);
2008 if (I
== DbgConcreteScopeMap
.end()) {
2009 // FIXME: Can this situation actually happen? And if so, should it?
2010 if (TimePassesIsEnabled
)
2011 DebugTimer
->stopTimer();
2016 SmallVector
<DbgScope
*, 8> &Scopes
= I
->second
;
2017 if (Scopes
.empty()) {
2018 // Returned ID is 0 if this is unbalanced "end of inlined
2019 // scope". This could happen if optimizer eats dbg intrinsics
2020 // or "beginning of inlined scope" is not recoginized due to
2021 // missing location info. In such cases, ignore this region.end.
2025 DbgScope
*Scope
= Scopes
.back(); Scopes
.pop_back();
2026 unsigned ID
= MMI
->NextLabelID();
2027 MMI
->RecordUsedDbgLabel(ID
);
2028 Scope
->setEndLabelID(ID
);
2030 if (TimePassesIsEnabled
)
2031 DebugTimer
->stopTimer();
2036 //===----------------------------------------------------------------------===//
2038 //===----------------------------------------------------------------------===//
2040 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2042 unsigned DwarfDebug::SizeAndOffsetDie(DIE
*Die
, unsigned Offset
, bool Last
) {
2043 // Get the children.
2044 const std::vector
<DIE
*> &Children
= Die
->getChildren();
2046 // If not last sibling and has children then add sibling offset attribute.
2047 if (!Last
&& !Children
.empty()) Die
->AddSiblingOffset();
2049 // Record the abbreviation.
2050 AssignAbbrevNumber(Die
->getAbbrev());
2052 // Get the abbreviation for this DIE.
2053 unsigned AbbrevNumber
= Die
->getAbbrevNumber();
2054 const DIEAbbrev
*Abbrev
= Abbreviations
[AbbrevNumber
- 1];
2057 Die
->setOffset(Offset
);
2059 // Start the size with the size of abbreviation code.
2060 Offset
+= MCAsmInfo::getULEB128Size(AbbrevNumber
);
2062 const SmallVector
<DIEValue
*, 32> &Values
= Die
->getValues();
2063 const SmallVector
<DIEAbbrevData
, 8> &AbbrevData
= Abbrev
->getData();
2065 // Size the DIE attribute values.
2066 for (unsigned i
= 0, N
= Values
.size(); i
< N
; ++i
)
2067 // Size attribute value.
2068 Offset
+= Values
[i
]->SizeOf(TD
, AbbrevData
[i
].getForm());
2070 // Size the DIE children if any.
2071 if (!Children
.empty()) {
2072 assert(Abbrev
->getChildrenFlag() == dwarf::DW_CHILDREN_yes
&&
2073 "Children flag not set");
2075 for (unsigned j
= 0, M
= Children
.size(); j
< M
; ++j
)
2076 Offset
= SizeAndOffsetDie(Children
[j
], Offset
, (j
+ 1) == M
);
2078 // End of children marker.
2079 Offset
+= sizeof(int8_t);
2082 Die
->setSize(Offset
- Die
->getOffset());
2086 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2088 void DwarfDebug::SizeAndOffsets() {
2089 // Compute size of compile unit header.
2090 static unsigned Offset
=
2091 sizeof(int32_t) + // Length of Compilation Unit Info
2092 sizeof(int16_t) + // DWARF version number
2093 sizeof(int32_t) + // Offset Into Abbrev. Section
2094 sizeof(int8_t); // Pointer Size (in bytes)
2096 SizeAndOffsetDie(ModuleCU
->getDie(), Offset
, true);
2097 CompileUnitOffsets
[ModuleCU
] = 0;
2100 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2101 /// tools to recognize the object file contains Dwarf information.
2102 void DwarfDebug::EmitInitial() {
2103 // Check to see if we already emitted intial headers.
2104 if (didInitial
) return;
2107 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
2109 // Dwarf sections base addresses.
2110 if (MAI
->doesDwarfRequireFrameSection()) {
2111 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfFrameSection());
2112 EmitLabel("section_debug_frame", 0);
2115 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfInfoSection());
2116 EmitLabel("section_info", 0);
2117 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfAbbrevSection());
2118 EmitLabel("section_abbrev", 0);
2119 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfARangesSection());
2120 EmitLabel("section_aranges", 0);
2122 if (const MCSection
*LineInfoDirective
= TLOF
.getDwarfMacroInfoSection()) {
2123 Asm
->OutStreamer
.SwitchSection(LineInfoDirective
);
2124 EmitLabel("section_macinfo", 0);
2127 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfLineSection());
2128 EmitLabel("section_line", 0);
2129 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfLocSection());
2130 EmitLabel("section_loc", 0);
2131 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfPubNamesSection());
2132 EmitLabel("section_pubnames", 0);
2133 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfStrSection());
2134 EmitLabel("section_str", 0);
2135 Asm
->OutStreamer
.SwitchSection(TLOF
.getDwarfRangesSection());
2136 EmitLabel("section_ranges", 0);
2138 Asm
->OutStreamer
.SwitchSection(TLOF
.getTextSection());
2139 EmitLabel("text_begin", 0);
2140 Asm
->OutStreamer
.SwitchSection(TLOF
.getDataSection());
2141 EmitLabel("data_begin", 0);
2144 /// EmitDIE - Recusively Emits a debug information entry.
2146 void DwarfDebug::EmitDIE(DIE
*Die
) {
2147 // Get the abbreviation for this DIE.
2148 unsigned AbbrevNumber
= Die
->getAbbrevNumber();
2149 const DIEAbbrev
*Abbrev
= Abbreviations
[AbbrevNumber
- 1];
2153 // Emit the code (index) for the abbreviation.
2154 Asm
->EmitULEB128Bytes(AbbrevNumber
);
2156 if (Asm
->isVerbose())
2157 Asm
->EOL(std::string("Abbrev [" +
2158 utostr(AbbrevNumber
) +
2159 "] 0x" + utohexstr(Die
->getOffset()) +
2160 ":0x" + utohexstr(Die
->getSize()) + " " +
2161 dwarf::TagString(Abbrev
->getTag())));
2165 SmallVector
<DIEValue
*, 32> &Values
= Die
->getValues();
2166 const SmallVector
<DIEAbbrevData
, 8> &AbbrevData
= Abbrev
->getData();
2168 // Emit the DIE attribute values.
2169 for (unsigned i
= 0, N
= Values
.size(); i
< N
; ++i
) {
2170 unsigned Attr
= AbbrevData
[i
].getAttribute();
2171 unsigned Form
= AbbrevData
[i
].getForm();
2172 assert(Form
&& "Too many attributes for DIE (check abbreviation)");
2175 case dwarf::DW_AT_sibling
:
2176 Asm
->EmitInt32(Die
->SiblingOffset());
2178 case dwarf::DW_AT_abstract_origin
: {
2179 DIEEntry
*E
= cast
<DIEEntry
>(Values
[i
]);
2180 DIE
*Origin
= E
->getEntry();
2182 CompileUnitOffsets
[Die
->getAbstractCompileUnit()] +
2183 Origin
->getOffset();
2185 Asm
->EmitInt32(Addr
);
2189 // Emit an attribute using the defined form.
2190 Values
[i
]->EmitValue(this, Form
);
2194 Asm
->EOL(dwarf::AttributeString(Attr
));
2197 // Emit the DIE children if any.
2198 if (Abbrev
->getChildrenFlag() == dwarf::DW_CHILDREN_yes
) {
2199 const std::vector
<DIE
*> &Children
= Die
->getChildren();
2201 for (unsigned j
= 0, M
= Children
.size(); j
< M
; ++j
)
2202 EmitDIE(Children
[j
]);
2204 Asm
->EmitInt8(0); Asm
->EOL("End Of Children Mark");
2208 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
2210 void DwarfDebug::EmitDebugInfoPerCU(CompileUnit
*Unit
) {
2211 DIE
*Die
= Unit
->getDie();
2213 // Emit the compile units header.
2214 EmitLabel("info_begin", Unit
->getID());
2216 // Emit size of content not including length itself
2217 unsigned ContentSize
= Die
->getSize() +
2218 sizeof(int16_t) + // DWARF version number
2219 sizeof(int32_t) + // Offset Into Abbrev. Section
2220 sizeof(int8_t) + // Pointer Size (in bytes)
2221 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2223 Asm
->EmitInt32(ContentSize
); Asm
->EOL("Length of Compilation Unit Info");
2224 Asm
->EmitInt16(dwarf::DWARF_VERSION
); Asm
->EOL("DWARF version number");
2225 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2226 Asm
->EOL("Offset Into Abbrev. Section");
2227 Asm
->EmitInt8(TD
->getPointerSize()); Asm
->EOL("Address Size (in bytes)");
2230 // FIXME - extra padding for gdb bug.
2231 Asm
->EmitInt8(0); Asm
->EOL("Extra Pad For GDB");
2232 Asm
->EmitInt8(0); Asm
->EOL("Extra Pad For GDB");
2233 Asm
->EmitInt8(0); Asm
->EOL("Extra Pad For GDB");
2234 Asm
->EmitInt8(0); Asm
->EOL("Extra Pad For GDB");
2235 EmitLabel("info_end", Unit
->getID());
2240 void DwarfDebug::EmitDebugInfo() {
2241 // Start debug info section.
2242 Asm
->OutStreamer
.SwitchSection(
2243 Asm
->getObjFileLowering().getDwarfInfoSection());
2245 EmitDebugInfoPerCU(ModuleCU
);
2248 /// EmitAbbreviations - Emit the abbreviation section.
2250 void DwarfDebug::EmitAbbreviations() const {
2251 // Check to see if it is worth the effort.
2252 if (!Abbreviations
.empty()) {
2253 // Start the debug abbrev section.
2254 Asm
->OutStreamer
.SwitchSection(
2255 Asm
->getObjFileLowering().getDwarfAbbrevSection());
2257 EmitLabel("abbrev_begin", 0);
2259 // For each abbrevation.
2260 for (unsigned i
= 0, N
= Abbreviations
.size(); i
< N
; ++i
) {
2261 // Get abbreviation data
2262 const DIEAbbrev
*Abbrev
= Abbreviations
[i
];
2264 // Emit the abbrevations code (base 1 index.)
2265 Asm
->EmitULEB128Bytes(Abbrev
->getNumber());
2266 Asm
->EOL("Abbreviation Code");
2268 // Emit the abbreviations data.
2274 // Mark end of abbreviations.
2275 Asm
->EmitULEB128Bytes(0); Asm
->EOL("EOM(3)");
2277 EmitLabel("abbrev_end", 0);
2282 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2283 /// the line matrix.
2285 void DwarfDebug::EmitEndOfLineMatrix(unsigned SectionEnd
) {
2286 // Define last address of section.
2287 Asm
->EmitInt8(0); Asm
->EOL("Extended Op");
2288 Asm
->EmitInt8(TD
->getPointerSize() + 1); Asm
->EOL("Op size");
2289 Asm
->EmitInt8(dwarf::DW_LNE_set_address
); Asm
->EOL("DW_LNE_set_address");
2290 EmitReference("section_end", SectionEnd
); Asm
->EOL("Section end label");
2292 // Mark end of matrix.
2293 Asm
->EmitInt8(0); Asm
->EOL("DW_LNE_end_sequence");
2294 Asm
->EmitULEB128Bytes(1); Asm
->EOL();
2295 Asm
->EmitInt8(1); Asm
->EOL();
2298 /// EmitDebugLines - Emit source line information.
2300 void DwarfDebug::EmitDebugLines() {
2301 // If the target is using .loc/.file, the assembler will be emitting the
2302 // .debug_line table automatically.
2303 if (MAI
->hasDotLocAndDotFile())
2306 // Minimum line delta, thus ranging from -10..(255-10).
2307 const int MinLineDelta
= -(dwarf::DW_LNS_fixed_advance_pc
+ 1);
2308 // Maximum line delta, thus ranging from -10..(255-10).
2309 const int MaxLineDelta
= 255 + MinLineDelta
;
2311 // Start the dwarf line section.
2312 Asm
->OutStreamer
.SwitchSection(
2313 Asm
->getObjFileLowering().getDwarfLineSection());
2315 // Construct the section header.
2316 EmitDifference("line_end", 0, "line_begin", 0, true);
2317 Asm
->EOL("Length of Source Line Info");
2318 EmitLabel("line_begin", 0);
2320 Asm
->EmitInt16(dwarf::DWARF_VERSION
); Asm
->EOL("DWARF version number");
2322 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2323 Asm
->EOL("Prolog Length");
2324 EmitLabel("line_prolog_begin", 0);
2326 Asm
->EmitInt8(1); Asm
->EOL("Minimum Instruction Length");
2328 Asm
->EmitInt8(1); Asm
->EOL("Default is_stmt_start flag");
2330 Asm
->EmitInt8(MinLineDelta
); Asm
->EOL("Line Base Value (Special Opcodes)");
2332 Asm
->EmitInt8(MaxLineDelta
); Asm
->EOL("Line Range Value (Special Opcodes)");
2334 Asm
->EmitInt8(-MinLineDelta
); Asm
->EOL("Special Opcode Base");
2336 // Line number standard opcode encodings argument count
2337 Asm
->EmitInt8(0); Asm
->EOL("DW_LNS_copy arg count");
2338 Asm
->EmitInt8(1); Asm
->EOL("DW_LNS_advance_pc arg count");
2339 Asm
->EmitInt8(1); Asm
->EOL("DW_LNS_advance_line arg count");
2340 Asm
->EmitInt8(1); Asm
->EOL("DW_LNS_set_file arg count");
2341 Asm
->EmitInt8(1); Asm
->EOL("DW_LNS_set_column arg count");
2342 Asm
->EmitInt8(0); Asm
->EOL("DW_LNS_negate_stmt arg count");
2343 Asm
->EmitInt8(0); Asm
->EOL("DW_LNS_set_basic_block arg count");
2344 Asm
->EmitInt8(0); Asm
->EOL("DW_LNS_const_add_pc arg count");
2345 Asm
->EmitInt8(1); Asm
->EOL("DW_LNS_fixed_advance_pc arg count");
2347 // Emit directories.
2348 for (unsigned DI
= 1, DE
= getNumSourceDirectories()+1; DI
!= DE
; ++DI
) {
2349 Asm
->EmitString(getSourceDirectoryName(DI
));
2350 Asm
->EOL("Directory");
2353 Asm
->EmitInt8(0); Asm
->EOL("End of directories");
2356 for (unsigned SI
= 1, SE
= getNumSourceIds()+1; SI
!= SE
; ++SI
) {
2357 // Remember source id starts at 1.
2358 std::pair
<unsigned, unsigned> Id
= getSourceDirectoryAndFileIds(SI
);
2359 Asm
->EmitString(getSourceFileName(Id
.second
));
2361 Asm
->EmitULEB128Bytes(Id
.first
);
2362 Asm
->EOL("Directory #");
2363 Asm
->EmitULEB128Bytes(0);
2364 Asm
->EOL("Mod date");
2365 Asm
->EmitULEB128Bytes(0);
2366 Asm
->EOL("File size");
2369 Asm
->EmitInt8(0); Asm
->EOL("End of files");
2371 EmitLabel("line_prolog_end", 0);
2373 // A sequence for each text section.
2374 unsigned SecSrcLinesSize
= SectionSourceLines
.size();
2376 for (unsigned j
= 0; j
< SecSrcLinesSize
; ++j
) {
2377 // Isolate current sections line info.
2378 const std::vector
<SrcLineInfo
> &LineInfos
= SectionSourceLines
[j
];
2380 /*if (Asm->isVerbose()) {
2381 const MCSection *S = SectionMap[j + 1];
2382 O << '\t' << MAI->getCommentString() << " Section"
2383 << S->getName() << '\n';
2387 // Dwarf assumes we start with first line of first source file.
2388 unsigned Source
= 1;
2391 // Construct rows of the address, source, line, column matrix.
2392 for (unsigned i
= 0, N
= LineInfos
.size(); i
< N
; ++i
) {
2393 const SrcLineInfo
&LineInfo
= LineInfos
[i
];
2394 unsigned LabelID
= MMI
->MappedLabel(LineInfo
.getLabelID());
2395 if (!LabelID
) continue;
2397 if (!Asm
->isVerbose())
2400 std::pair
<unsigned, unsigned> SourceID
=
2401 getSourceDirectoryAndFileIds(LineInfo
.getSourceID());
2402 O
<< '\t' << MAI
->getCommentString() << ' '
2403 << getSourceDirectoryName(SourceID
.first
) << ' '
2404 << getSourceFileName(SourceID
.second
)
2405 <<" :" << utostr_32(LineInfo
.getLine()) << '\n';
2408 // Define the line address.
2409 Asm
->EmitInt8(0); Asm
->EOL("Extended Op");
2410 Asm
->EmitInt8(TD
->getPointerSize() + 1); Asm
->EOL("Op size");
2411 Asm
->EmitInt8(dwarf::DW_LNE_set_address
); Asm
->EOL("DW_LNE_set_address");
2412 EmitReference("label", LabelID
); Asm
->EOL("Location label");
2414 // If change of source, then switch to the new source.
2415 if (Source
!= LineInfo
.getSourceID()) {
2416 Source
= LineInfo
.getSourceID();
2417 Asm
->EmitInt8(dwarf::DW_LNS_set_file
); Asm
->EOL("DW_LNS_set_file");
2418 Asm
->EmitULEB128Bytes(Source
); Asm
->EOL("New Source");
2421 // If change of line.
2422 if (Line
!= LineInfo
.getLine()) {
2423 // Determine offset.
2424 int Offset
= LineInfo
.getLine() - Line
;
2425 int Delta
= Offset
- MinLineDelta
;
2428 Line
= LineInfo
.getLine();
2430 // If delta is small enough and in range...
2431 if (Delta
>= 0 && Delta
< (MaxLineDelta
- 1)) {
2432 // ... then use fast opcode.
2433 Asm
->EmitInt8(Delta
- MinLineDelta
); Asm
->EOL("Line Delta");
2435 // ... otherwise use long hand.
2436 Asm
->EmitInt8(dwarf::DW_LNS_advance_line
);
2437 Asm
->EOL("DW_LNS_advance_line");
2438 Asm
->EmitSLEB128Bytes(Offset
); Asm
->EOL("Line Offset");
2439 Asm
->EmitInt8(dwarf::DW_LNS_copy
); Asm
->EOL("DW_LNS_copy");
2442 // Copy the previous row (different address or source)
2443 Asm
->EmitInt8(dwarf::DW_LNS_copy
); Asm
->EOL("DW_LNS_copy");
2447 EmitEndOfLineMatrix(j
+ 1);
2450 if (SecSrcLinesSize
== 0)
2451 // Because we're emitting a debug_line section, we still need a line
2452 // table. The linker and friends expect it to exist. If there's nothing to
2453 // put into it, emit an empty table.
2454 EmitEndOfLineMatrix(1);
2456 EmitLabel("line_end", 0);
2460 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2462 void DwarfDebug::EmitCommonDebugFrame() {
2463 if (!MAI
->doesDwarfRequireFrameSection())
2467 Asm
->TM
.getFrameInfo()->getStackGrowthDirection() ==
2468 TargetFrameInfo::StackGrowsUp
?
2469 TD
->getPointerSize() : -TD
->getPointerSize();
2471 // Start the dwarf frame section.
2472 Asm
->OutStreamer
.SwitchSection(
2473 Asm
->getObjFileLowering().getDwarfFrameSection());
2475 EmitLabel("debug_frame_common", 0);
2476 EmitDifference("debug_frame_common_end", 0,
2477 "debug_frame_common_begin", 0, true);
2478 Asm
->EOL("Length of Common Information Entry");
2480 EmitLabel("debug_frame_common_begin", 0);
2481 Asm
->EmitInt32((int)dwarf::DW_CIE_ID
);
2482 Asm
->EOL("CIE Identifier Tag");
2483 Asm
->EmitInt8(dwarf::DW_CIE_VERSION
);
2484 Asm
->EOL("CIE Version");
2485 Asm
->EmitString("");
2486 Asm
->EOL("CIE Augmentation");
2487 Asm
->EmitULEB128Bytes(1);
2488 Asm
->EOL("CIE Code Alignment Factor");
2489 Asm
->EmitSLEB128Bytes(stackGrowth
);
2490 Asm
->EOL("CIE Data Alignment Factor");
2491 Asm
->EmitInt8(RI
->getDwarfRegNum(RI
->getRARegister(), false));
2492 Asm
->EOL("CIE RA Column");
2494 std::vector
<MachineMove
> Moves
;
2495 RI
->getInitialFrameState(Moves
);
2497 EmitFrameMoves(NULL
, 0, Moves
, false);
2499 Asm
->EmitAlignment(2, 0, 0, false);
2500 EmitLabel("debug_frame_common_end", 0);
2505 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2508 DwarfDebug::EmitFunctionDebugFrame(const FunctionDebugFrameInfo
&DebugFrameInfo
){
2509 if (!MAI
->doesDwarfRequireFrameSection())
2512 // Start the dwarf frame section.
2513 Asm
->OutStreamer
.SwitchSection(
2514 Asm
->getObjFileLowering().getDwarfFrameSection());
2516 EmitDifference("debug_frame_end", DebugFrameInfo
.Number
,
2517 "debug_frame_begin", DebugFrameInfo
.Number
, true);
2518 Asm
->EOL("Length of Frame Information Entry");
2520 EmitLabel("debug_frame_begin", DebugFrameInfo
.Number
);
2522 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2524 Asm
->EOL("FDE CIE offset");
2526 EmitReference("func_begin", DebugFrameInfo
.Number
);
2527 Asm
->EOL("FDE initial location");
2528 EmitDifference("func_end", DebugFrameInfo
.Number
,
2529 "func_begin", DebugFrameInfo
.Number
);
2530 Asm
->EOL("FDE address range");
2532 EmitFrameMoves("func_begin", DebugFrameInfo
.Number
, DebugFrameInfo
.Moves
,
2535 Asm
->EmitAlignment(2, 0, 0, false);
2536 EmitLabel("debug_frame_end", DebugFrameInfo
.Number
);
2541 void DwarfDebug::EmitDebugPubNamesPerCU(CompileUnit
*Unit
) {
2542 EmitDifference("pubnames_end", Unit
->getID(),
2543 "pubnames_begin", Unit
->getID(), true);
2544 Asm
->EOL("Length of Public Names Info");
2546 EmitLabel("pubnames_begin", Unit
->getID());
2548 Asm
->EmitInt16(dwarf::DWARF_VERSION
); Asm
->EOL("DWARF Version");
2550 EmitSectionOffset("info_begin", "section_info",
2551 Unit
->getID(), 0, true, false);
2552 Asm
->EOL("Offset of Compilation Unit Info");
2554 EmitDifference("info_end", Unit
->getID(), "info_begin", Unit
->getID(),
2556 Asm
->EOL("Compilation Unit Length");
2558 StringMap
<DIE
*> &Globals
= Unit
->getGlobals();
2559 for (StringMap
<DIE
*>::const_iterator
2560 GI
= Globals
.begin(), GE
= Globals
.end(); GI
!= GE
; ++GI
) {
2561 const char *Name
= GI
->getKeyData();
2562 DIE
* Entity
= GI
->second
;
2564 Asm
->EmitInt32(Entity
->getOffset()); Asm
->EOL("DIE offset");
2565 Asm
->EmitString(Name
, strlen(Name
)); Asm
->EOL("External Name");
2568 Asm
->EmitInt32(0); Asm
->EOL("End Mark");
2569 EmitLabel("pubnames_end", Unit
->getID());
2574 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2576 void DwarfDebug::EmitDebugPubNames() {
2577 // Start the dwarf pubnames section.
2578 Asm
->OutStreamer
.SwitchSection(
2579 Asm
->getObjFileLowering().getDwarfPubNamesSection());
2581 EmitDebugPubNamesPerCU(ModuleCU
);
2584 /// EmitDebugStr - Emit visible names into a debug str section.
2586 void DwarfDebug::EmitDebugStr() {
2587 // Check to see if it is worth the effort.
2588 if (!StringPool
.empty()) {
2589 // Start the dwarf str section.
2590 Asm
->OutStreamer
.SwitchSection(
2591 Asm
->getObjFileLowering().getDwarfStrSection());
2593 // For each of strings in the string pool.
2594 for (unsigned StringID
= 1, N
= StringPool
.size();
2595 StringID
<= N
; ++StringID
) {
2596 // Emit a label for reference from debug information entries.
2597 EmitLabel("string", StringID
);
2599 // Emit the string itself.
2600 const std::string
&String
= StringPool
[StringID
];
2601 Asm
->EmitString(String
); Asm
->EOL();
2608 /// EmitDebugLoc - Emit visible names into a debug loc section.
2610 void DwarfDebug::EmitDebugLoc() {
2611 // Start the dwarf loc section.
2612 Asm
->OutStreamer
.SwitchSection(
2613 Asm
->getObjFileLowering().getDwarfLocSection());
2617 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2619 void DwarfDebug::EmitDebugARanges() {
2620 // Start the dwarf aranges section.
2621 Asm
->OutStreamer
.SwitchSection(
2622 Asm
->getObjFileLowering().getDwarfARangesSection());
2626 CompileUnit
*Unit
= GetBaseCompileUnit();
2628 // Don't include size of length
2629 Asm
->EmitInt32(0x1c); Asm
->EOL("Length of Address Ranges Info");
2631 Asm
->EmitInt16(dwarf::DWARF_VERSION
); Asm
->EOL("Dwarf Version");
2633 EmitReference("info_begin", Unit
->getID());
2634 Asm
->EOL("Offset of Compilation Unit Info");
2636 Asm
->EmitInt8(TD
->getPointerSize()); Asm
->EOL("Size of Address");
2638 Asm
->EmitInt8(0); Asm
->EOL("Size of Segment Descriptor");
2640 Asm
->EmitInt16(0); Asm
->EOL("Pad (1)");
2641 Asm
->EmitInt16(0); Asm
->EOL("Pad (2)");
2644 EmitReference("text_begin", 0); Asm
->EOL("Address");
2645 EmitDifference("text_end", 0, "text_begin", 0, true); Asm
->EOL("Length");
2647 Asm
->EmitInt32(0); Asm
->EOL("EOM (1)");
2648 Asm
->EmitInt32(0); Asm
->EOL("EOM (2)");
2654 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2656 void DwarfDebug::EmitDebugRanges() {
2657 // Start the dwarf ranges section.
2658 Asm
->OutStreamer
.SwitchSection(
2659 Asm
->getObjFileLowering().getDwarfRangesSection());
2663 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2665 void DwarfDebug::EmitDebugMacInfo() {
2666 if (const MCSection
*LineInfo
=
2667 Asm
->getObjFileLowering().getDwarfMacroInfoSection()) {
2668 // Start the dwarf macinfo section.
2669 Asm
->OutStreamer
.SwitchSection(LineInfo
);
2674 /// EmitDebugInlineInfo - Emit inline info using following format.
2676 /// 1. length of section
2677 /// 2. Dwarf version number
2678 /// 3. address size.
2680 /// Entries (one "entry" for each function that was inlined):
2682 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
2683 /// otherwise offset into __debug_str for regular function name.
2684 /// 2. offset into __debug_str section for regular function name.
2685 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
2686 /// instances for the function.
2688 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
2689 /// inlined instance; the die_offset points to the inlined_subroutine die in the
2690 /// __debug_info section, and the low_pc is the starting address for the
2691 /// inlining instance.
2692 void DwarfDebug::EmitDebugInlineInfo() {
2693 if (!MAI
->doesDwarfUsesInlineInfoSection())
2699 Asm
->OutStreamer
.SwitchSection(
2700 Asm
->getObjFileLowering().getDwarfDebugInlineSection());
2702 EmitDifference("debug_inlined_end", 1,
2703 "debug_inlined_begin", 1, true);
2704 Asm
->EOL("Length of Debug Inlined Information Entry");
2706 EmitLabel("debug_inlined_begin", 1);
2708 Asm
->EmitInt16(dwarf::DWARF_VERSION
); Asm
->EOL("Dwarf Version");
2709 Asm
->EmitInt8(TD
->getPointerSize()); Asm
->EOL("Address Size (in bytes)");
2711 for (DenseMap
<MDNode
*, SmallVector
<unsigned, 4> >::iterator
2712 I
= InlineInfo
.begin(), E
= InlineInfo
.end(); I
!= E
; ++I
) {
2713 MDNode
*Node
= I
->first
;
2714 SmallVector
<unsigned, 4> &Labels
= I
->second
;
2715 DISubprogram
SP(Node
);
2719 SP
.getLinkageName(LName
);
2723 Asm
->EmitString(Name
);
2725 // Skip special LLVM prefix that is used to inform the asm printer to not
2726 // emit usual symbol prefix before the symbol name. This happens for
2727 // Objective-C symbol names and symbol whose name is replaced using GCC's
2728 // __asm__ attribute.
2731 Asm
->EmitString(LName
);
2733 Asm
->EOL("MIPS linkage name");
2735 Asm
->EmitString(Name
); Asm
->EOL("Function name");
2737 Asm
->EmitULEB128Bytes(Labels
.size()); Asm
->EOL("Inline count");
2739 for (SmallVector
<unsigned, 4>::iterator LI
= Labels
.begin(),
2740 LE
= Labels
.end(); LI
!= LE
; ++LI
) {
2741 DIE
*SP
= ModuleCU
->getDieMapSlotFor(Node
);
2742 Asm
->EmitInt32(SP
->getOffset()); Asm
->EOL("DIE offset");
2744 if (TD
->getPointerSize() == sizeof(int32_t))
2745 O
<< MAI
->getData32bitsDirective();
2747 O
<< MAI
->getData64bitsDirective();
2749 PrintLabelName("label", *LI
); Asm
->EOL("low_pc");
2753 EmitLabel("debug_inlined_end", 1);