1 //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DwarfWriter.h"
16 #include "llvm/ADT/FoldingSet.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/UniqueVector.h"
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineDebugInfo.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineLocation.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/DataTypes.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/Target/TargetAsmInfo.h"
30 #include "llvm/Target/MRegisterInfo.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetFrameInfo.h"
39 using namespace llvm::dwarf;
42 DwarfVerbose("dwarf-verbose", cl::Hidden,
43 cl::desc("Add comments to Dwarf directives."));
47 //===----------------------------------------------------------------------===//
48 /// DWLabel - Labels are used to track locations in the assembler file.
49 /// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
50 /// category of label (Ex. location) and number is a value unique in that
54 const char *Tag; // Label category tag. Should always be
55 // a staticly declared C string.
56 unsigned Number; // Unique number.
58 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
61 //===----------------------------------------------------------------------===//
62 /// Forward declarations.
66 //===----------------------------------------------------------------------===//
67 /// CompileUnit - This dwarf writer support class manages information associate
68 /// with a source file.
71 CompileUnitDesc *Desc; // Compile unit debug descriptor.
72 unsigned ID; // File ID for source.
73 DIE *Die; // Compile unit debug information entry.
74 std::map<std::string, DIE *> Globals; // A map of globally visible named
75 // entities for this unit.
76 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
77 // Tracks the mapping of unit level
78 // debug informaton descriptors to debug
79 // information entries.
82 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
93 CompileUnitDesc *getDesc() const { return Desc; }
94 unsigned getID() const { return ID; }
95 DIE* getDie() const { return Die; }
96 std::map<std::string, DIE *> &getGlobals() { return Globals; }
98 /// hasContent - Return true if this compile unit has something to write out.
100 bool hasContent() const;
102 /// AddGlobal - Add a new global entity to the compile unit.
104 void AddGlobal(const std::string &Name, DIE *Die);
106 /// getDieMapSlotFor - Returns the debug information entry map slot for the
107 /// specified debug descriptor.
108 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
109 return DescToDieMap[DD];
113 //===----------------------------------------------------------------------===//
114 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
115 /// Dwarf abbreviation.
116 class DIEAbbrevData {
118 unsigned Attribute; // Dwarf attribute code.
119 unsigned Form; // Dwarf form code.
122 DIEAbbrevData(unsigned A, unsigned F)
128 unsigned getAttribute() const { return Attribute; }
129 unsigned getForm() const { return Form; }
131 /// Profile - Used to gather unique data for the abbreviation folding set.
133 void Profile(FoldingSetNodeID &ID) {
134 ID.AddInteger(Attribute);
139 //===----------------------------------------------------------------------===//
140 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
141 /// information object.
142 class DIEAbbrev : public FoldingSetNode {
144 unsigned Number; // Unique number for abbreviation.
145 unsigned Tag; // Dwarf tag code.
146 unsigned ChildrenFlag; // Dwarf children flag.
147 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
151 DIEAbbrev(unsigned T, unsigned C)
160 unsigned getNumber() const { return Number; }
161 unsigned getTag() const { return Tag; }
162 unsigned getChildrenFlag() const { return ChildrenFlag; }
163 const std::vector<DIEAbbrevData> &getData() const { return Data; }
164 void setNumber(unsigned N) { Number = N; }
165 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
167 /// AddAttribute - Adds another set of attribute information to the
169 void AddAttribute(unsigned Attribute, unsigned Form) {
170 Data.push_back(DIEAbbrevData(Attribute, Form));
173 /// AddFirstAttribute - Adds a set of attribute information to the front
174 /// of the abbreviation.
175 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
176 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
179 /// Profile - Used to gather unique data for the abbreviation folding set.
181 void Profile(FoldingSetNodeID &ID) {
183 ID.AddInteger(ChildrenFlag);
185 // For each attribute description.
186 for (unsigned i = 0, N = Data.size(); i < N; ++i)
190 /// Emit - Print the abbreviation using the specified Dwarf writer.
192 void Emit(const Dwarf &DW) const;
195 void print(std::ostream &O);
200 //===----------------------------------------------------------------------===//
201 /// DIEValue - A debug information entry value.
215 unsigned Type; // Type of the value
217 DIEValue(unsigned T) : Type(T) {}
218 virtual ~DIEValue() {}
220 // Implement isa/cast/dyncast.
221 static bool classof(const DIEValue *) { return true; }
223 /// EmitValue - Emit value via the Dwarf writer.
225 virtual void EmitValue(const Dwarf &DW, unsigned Form) const = 0;
227 /// SizeOf - Return the size of a value in bytes.
229 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const = 0;
232 //===----------------------------------------------------------------------===//
233 /// DWInteger - An integer value DIE.
235 class DIEInteger : public DIEValue {
240 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
242 // Implement isa/cast/dyncast.
243 static bool classof(const DIEInteger *) { return true; }
244 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
246 /// BestForm - Choose the best form for integer.
248 unsigned BestForm(bool IsSigned);
250 /// EmitValue - Emit integer of appropriate size.
252 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
254 /// SizeOf - Determine size of integer value in bytes.
256 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
259 //===----------------------------------------------------------------------===//
260 /// DIEString - A string value DIE.
262 struct DIEString : public DIEValue {
263 const std::string String;
265 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
267 // Implement isa/cast/dyncast.
268 static bool classof(const DIEString *) { return true; }
269 static bool classof(const DIEValue *S) { return S->Type == isString; }
271 /// EmitValue - Emit string value.
273 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
275 /// SizeOf - Determine size of string value in bytes.
277 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
280 //===----------------------------------------------------------------------===//
281 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
283 struct DIEDwarfLabel : public DIEValue {
286 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
288 // Implement isa/cast/dyncast.
289 static bool classof(const DIEDwarfLabel *) { return true; }
290 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
292 /// EmitValue - Emit label value.
294 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
296 /// SizeOf - Determine size of label value in bytes.
298 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
302 //===----------------------------------------------------------------------===//
303 /// DIEObjectLabel - A label to an object in code or data.
305 struct DIEObjectLabel : public DIEValue {
306 const std::string Label;
308 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
310 // Implement isa/cast/dyncast.
311 static bool classof(const DIEObjectLabel *) { return true; }
312 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
314 /// EmitValue - Emit label value.
316 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
318 /// SizeOf - Determine size of label value in bytes.
320 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
323 //===----------------------------------------------------------------------===//
324 /// DIEDelta - A simple label difference DIE.
326 struct DIEDelta : public DIEValue {
327 const DWLabel LabelHi;
328 const DWLabel LabelLo;
330 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
331 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
333 // Implement isa/cast/dyncast.
334 static bool classof(const DIEDelta *) { return true; }
335 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
337 /// EmitValue - Emit delta value.
339 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
341 /// SizeOf - Determine size of delta value in bytes.
343 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
346 //===----------------------------------------------------------------------===//
347 /// DIEntry - A pointer to a debug information entry.
349 struct DIEntry : public DIEValue {
352 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
354 // Implement isa/cast/dyncast.
355 static bool classof(const DIEntry *) { return true; }
356 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
358 /// EmitValue - Emit debug information entry offset.
360 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
362 /// SizeOf - Determine size of debug information entry in bytes.
364 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
367 //===----------------------------------------------------------------------===//
368 /// DIEBlock - A block of values. Primarily used for location expressions.
370 struct DIEBlock : public DIEValue {
371 unsigned Size; // Size in bytes excluding size header.
372 std::vector<unsigned> Forms; // Data forms.
373 std::vector<DIEValue *> Values; // Block values.
383 // Implement isa/cast/dyncast.
384 static bool classof(const DIEBlock *) { return true; }
385 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
387 /// ComputeSize - calculate the size of the block.
389 unsigned ComputeSize(Dwarf &DW);
391 /// BestForm - Choose the best form for data.
395 /// EmitValue - Emit block data.
397 virtual void EmitValue(const Dwarf &DW, unsigned Form) const;
399 /// SizeOf - Determine size of block data in bytes.
401 virtual unsigned SizeOf(const Dwarf &DW, unsigned Form) const;
403 /// AddUInt - Add an unsigned integer value.
405 void AddUInt(unsigned Form, uint64_t Integer);
407 /// AddSInt - Add an signed integer value.
409 void AddSInt(unsigned Form, int64_t Integer);
411 /// AddString - Add a std::string value.
413 void AddString(unsigned Form, const std::string &String);
415 /// AddLabel - Add a Dwarf label value.
417 void AddLabel(unsigned Form, const DWLabel &Label);
419 /// AddObjectLabel - Add a non-Dwarf label value.
421 void AddObjectLabel(unsigned Form, const std::string &Label);
423 /// AddDelta - Add a label delta value.
425 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
427 /// AddDIEntry - Add a DIE value.
429 void AddDIEntry(unsigned Form, DIE *Entry);
433 //===----------------------------------------------------------------------===//
434 /// DIE - A structured debug information entry. Has an abbreviation which
435 /// describes it's organization.
438 DIEAbbrev Abbrev; // Buffer for constructing abbreviation.
439 unsigned Offset; // Offset in debug info section.
440 unsigned Size; // Size of instance + children.
441 std::vector<DIE *> Children; // Children DIEs.
442 std::vector<DIEValue *> Values; // Attributes values.
449 unsigned getAbbrevNumber() const {
450 return Abbrev.getNumber();
452 unsigned getOffset() const { return Offset; }
453 unsigned getSize() const { return Size; }
454 const std::vector<DIE *> &getChildren() const { return Children; }
455 const std::vector<DIEValue *> &getValues() const { return Values; }
456 void setOffset(unsigned O) { Offset = O; }
457 void setSize(unsigned S) { Size = S; }
459 /// SiblingOffset - Return the offset of the debug information entry's
461 unsigned SiblingOffset() const { return Offset + Size; }
463 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
465 void AddSiblingOffset();
467 /// AddUInt - Add an unsigned integer attribute data and value.
469 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
471 /// AddSInt - Add an signed integer attribute data and value.
473 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
475 /// AddString - Add a std::string attribute data and value.
477 void AddString(unsigned Attribute, unsigned Form,
478 const std::string &String);
480 /// AddLabel - Add a Dwarf label attribute data and value.
482 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
484 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
486 void AddObjectLabel(unsigned Attribute, unsigned Form,
487 const std::string &Label);
489 /// AddDelta - Add a label delta attribute data and value.
491 void AddDelta(unsigned Attribute, unsigned Form,
492 const DWLabel &Hi, const DWLabel &Lo);
494 /// AddDIEntry - Add a DIE attribute data and value.
496 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
498 /// AddBlock - Add block data.
500 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
502 /// Complete - Indicate that all attributes have been added and
503 /// ready to get an abbreviation ID.
505 void Complete(Dwarf &DW);
507 /// AddChild - Add a child to the DIE.
508 void AddChild(DIE *Child);
511 //===----------------------------------------------------------------------===//
512 /// Dwarf - Emits Dwarf debug and exception handling directives.
518 //===--------------------------------------------------------------------===//
519 // Core attributes used by the Dwarf writer.
523 /// O - Stream to .s file.
527 /// Asm - Target of Dwarf emission.
531 /// TAI - Target Asm Printer.
532 const TargetAsmInfo *TAI;
534 /// TD - Target data.
535 const TargetData *TD;
537 /// RI - Register Information.
538 const MRegisterInfo *RI;
540 /// M - Current module.
544 /// MF - Current machine function.
548 /// DebugInfo - Collected debug information.
550 MachineDebugInfo *DebugInfo;
552 /// didInitial - Flag to indicate if initial emission has been done.
556 /// shouldEmit - Flag to indicate if debug information should be emitted.
560 /// SubprogramCount - The running count of functions being compiled.
562 unsigned SubprogramCount;
564 //===--------------------------------------------------------------------===//
565 // Attributes used to construct specific Dwarf sections.
568 /// CompileUnits - All the compile units involved in this build. The index
569 /// of each entry in this vector corresponds to the sources in DebugInfo.
570 std::vector<CompileUnit *> CompileUnits;
572 /// AbbreviationsSet - Used to uniquely define the abbreviations.
574 FoldingSet<DIEAbbrev> AbbreviationsSet;
576 /// Abbreviations - A list of all the unique abbreviations in use.
578 std::vector<DIEAbbrev *> Abbreviations;
580 /// StringPool - A UniqueVector of strings used by indirect references.
581 /// UnitMap - Map debug information descriptor to compile unit.
583 UniqueVector<std::string> StringPool;
585 /// UnitMap - Map debug information descriptor to compile unit.
587 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
589 /// DescToDieMap - Tracks the mapping of top level debug informaton
590 /// descriptors to debug information entries.
591 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
593 /// SectionMap - Provides a unique id per text section.
595 UniqueVector<std::string> SectionMap;
597 /// SectionSourceLines - Tracks line numbers per text section.
599 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
604 //===--------------------------------------------------------------------===//
605 // Emission and print routines
608 /// PrintHex - Print a value as a hexidecimal value.
610 void PrintHex(int Value) const;
612 /// EOL - Print a newline character to asm stream. If a comment is present
613 /// then it will be printed first. Comments should not contain '\n'.
614 void EOL(const std::string &Comment) const;
616 /// EmitAlign - Print a align directive.
618 void EmitAlign(unsigned Alignment) const;
620 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
621 /// unsigned leb128 value.
622 void EmitULEB128Bytes(unsigned Value) const;
624 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
625 /// signed leb128 value.
626 void EmitSLEB128Bytes(int Value) const;
628 /// PrintULEB128 - Print a series of hexidecimal values (separated by
629 /// commas) representing an unsigned leb128 value.
630 void PrintULEB128(unsigned Value) const;
632 /// SizeULEB128 - Compute the number of bytes required for an unsigned
634 static unsigned SizeULEB128(unsigned Value);
636 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
637 /// commas) representing a signed leb128 value.
638 void PrintSLEB128(int Value) const;
640 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
642 static unsigned SizeSLEB128(int Value);
644 /// EmitInt8 - Emit a byte directive and value.
646 void EmitInt8(int Value) const;
648 /// EmitInt16 - Emit a short directive and value.
650 void EmitInt16(int Value) const;
652 /// EmitInt32 - Emit a long directive and value.
654 void EmitInt32(int Value) const;
656 /// EmitInt64 - Emit a long long directive and value.
658 void EmitInt64(uint64_t Value) const;
660 /// EmitString - Emit a string with quotes and a null terminator.
661 /// Special characters are emitted properly.
662 /// \literal (Eg. '\t') \endliteral
663 void EmitString(const std::string &String) const;
665 /// PrintLabelName - Print label name in form used by Dwarf writer.
667 void PrintLabelName(DWLabel Label) const {
668 PrintLabelName(Label.Tag, Label.Number);
670 void PrintLabelName(const char *Tag, unsigned Number) const;
672 /// EmitLabel - Emit location label for internal use by Dwarf.
674 void EmitLabel(DWLabel Label) const {
675 EmitLabel(Label.Tag, Label.Number);
677 void EmitLabel(const char *Tag, unsigned Number) const;
679 /// EmitReference - Emit a reference to a label.
681 void EmitReference(DWLabel Label) const {
682 EmitReference(Label.Tag, Label.Number);
684 void EmitReference(const char *Tag, unsigned Number) const;
685 void EmitReference(const std::string &Name) const;
687 /// EmitDifference - Emit the difference between two labels. Some
688 /// assemblers do not behave with absolute expressions with data directives,
689 /// so there is an option (needsSet) to use an intermediary set expression.
690 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
691 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
693 void EmitDifference(const char *TagHi, unsigned NumberHi,
694 const char *TagLo, unsigned NumberLo) const;
696 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
698 void AssignAbbrevNumber(DIEAbbrev *Abbrev);
700 /// NewString - Add a string to the constant pool and returns a label.
702 DWLabel NewString(const std::string &String);
704 /// getDieMapSlotFor - Returns the debug information entry map slot for the
705 /// specified debug descriptor.
706 DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
710 /// AddSourceLine - Add location information to specified debug information
712 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
714 /// AddAddress - Add an address attribute to a die based on the location
716 void AddAddress(DIE *Die, unsigned Attribute,
717 const MachineLocation &Location);
719 /// NewType - Create a new type DIE.
721 DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
723 /// NewCompileUnit - Create new compile unit and it's die.
725 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
727 /// FindCompileUnit - Get the compile unit for the given descriptor.
729 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
731 /// NewGlobalVariable - Make a new global variable DIE.
733 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
735 /// NewSubprogram - Add a new subprogram DIE.
737 DIE *NewSubprogram(SubprogramDesc *SPD);
739 /// NewScopeVariable - Create a new scope variable.
741 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
743 /// ConstructScope - Construct the components of a scope.
745 void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
748 /// ConstructRootScope - Construct the scope for the subprogram.
750 void ConstructRootScope(DebugScope *RootScope);
752 /// EmitInitial - Emit initial Dwarf declarations.
756 /// EmitDIE - Recusively Emits a debug information entry.
758 void EmitDIE(DIE *Die) const;
760 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
762 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
764 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
766 void SizeAndOffsets();
768 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
770 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
771 std::vector<MachineMove *> &Moves);
773 /// EmitDebugInfo - Emit the debug info section.
775 void EmitDebugInfo() const;
777 /// EmitAbbreviations - Emit the abbreviation section.
779 void EmitAbbreviations() const;
781 /// EmitDebugLines - Emit source line information.
783 void EmitDebugLines() const;
785 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
787 void EmitInitialDebugFrame();
789 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
791 void EmitFunctionDebugFrame();
793 /// EmitDebugPubNames - Emit info into a debug pubnames section.
795 void EmitDebugPubNames();
797 /// EmitDebugStr - Emit info into a debug str section.
801 /// EmitDebugLoc - Emit info into a debug loc section.
805 /// EmitDebugARanges - Emit info into a debug aranges section.
807 void EmitDebugARanges();
809 /// EmitDebugRanges - Emit info into a debug ranges section.
811 void EmitDebugRanges();
813 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
815 void EmitDebugMacInfo();
817 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
819 void ConstructCompileUnitDIEs();
821 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
822 /// global variables.
823 void ConstructGlobalDIEs();
825 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
827 void ConstructSubprogramDIEs();
829 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
831 bool ShouldEmitDwarf() const { return shouldEmit; }
835 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
840 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
842 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
843 /// created it. Set by the target AsmPrinter.
844 void SetDebugInfo(MachineDebugInfo *DI);
846 //===--------------------------------------------------------------------===//
847 // Main entry points.
850 /// BeginModule - Emit all Dwarf sections that should come prior to the
852 void BeginModule(Module *M);
854 /// EndModule - Emit all Dwarf sections that should come after the content.
858 /// BeginFunction - Gather pre-function debug information. Assumes being
859 /// emitted immediately after the function entry point.
860 void BeginFunction(MachineFunction *MF);
862 /// EndFunction - Gather and emit post-function debug information.
867 } // End of namespace llvm
869 //===----------------------------------------------------------------------===//
871 CompileUnit::~CompileUnit() {
875 /// hasContent - Return true if this compile unit has something to write out.
877 bool CompileUnit::hasContent() const {
878 return !Die->getChildren().empty();
881 /// AddGlobal - Add a new global entity to the compile unit.
883 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
887 //===----------------------------------------------------------------------===//
889 /// Emit - Print the abbreviation using the specified Dwarf writer.
891 void DIEAbbrev::Emit(const Dwarf &DW) const {
892 // Emit its Dwarf tag type.
893 DW.EmitULEB128Bytes(Tag);
894 DW.EOL(TagString(Tag));
896 // Emit whether it has children DIEs.
897 DW.EmitULEB128Bytes(ChildrenFlag);
898 DW.EOL(ChildrenString(ChildrenFlag));
900 // For each attribute description.
901 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
902 const DIEAbbrevData &AttrData = Data[i];
904 // Emit attribute type.
905 DW.EmitULEB128Bytes(AttrData.getAttribute());
906 DW.EOL(AttributeString(AttrData.getAttribute()));
909 DW.EmitULEB128Bytes(AttrData.getForm());
910 DW.EOL(FormEncodingString(AttrData.getForm()));
913 // Mark end of abbreviation.
914 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
915 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
919 void DIEAbbrev::print(std::ostream &O) {
920 O << "Abbreviation @"
921 << std::hex << (intptr_t)this << std::dec
925 << ChildrenString(ChildrenFlag)
928 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
930 << AttributeString(Data[i].getAttribute())
932 << FormEncodingString(Data[i].getForm())
936 void DIEAbbrev::dump() { print(std::cerr); }
939 //===----------------------------------------------------------------------===//
941 /// BestForm - Choose the best form for integer.
943 unsigned DIEInteger::BestForm(bool IsSigned) {
945 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
946 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
947 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
949 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
950 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
951 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
953 return DW_FORM_data8;
956 /// EmitValue - Emit integer of appropriate size.
958 void DIEInteger::EmitValue(const Dwarf &DW, unsigned Form) const {
960 case DW_FORM_flag: // Fall thru
961 case DW_FORM_ref1: // Fall thru
962 case DW_FORM_data1: DW.EmitInt8(Integer); break;
963 case DW_FORM_ref2: // Fall thru
964 case DW_FORM_data2: DW.EmitInt16(Integer); break;
965 case DW_FORM_ref4: // Fall thru
966 case DW_FORM_data4: DW.EmitInt32(Integer); break;
967 case DW_FORM_ref8: // Fall thru
968 case DW_FORM_data8: DW.EmitInt64(Integer); break;
969 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
970 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
971 default: assert(0 && "DIE Value form not supported yet"); break;
975 /// SizeOf - Determine size of integer value in bytes.
977 unsigned DIEInteger::SizeOf(const Dwarf &DW, unsigned Form) const {
979 case DW_FORM_flag: // Fall thru
980 case DW_FORM_ref1: // Fall thru
981 case DW_FORM_data1: return sizeof(int8_t);
982 case DW_FORM_ref2: // Fall thru
983 case DW_FORM_data2: return sizeof(int16_t);
984 case DW_FORM_ref4: // Fall thru
985 case DW_FORM_data4: return sizeof(int32_t);
986 case DW_FORM_ref8: // Fall thru
987 case DW_FORM_data8: return sizeof(int64_t);
988 case DW_FORM_udata: return DW.SizeULEB128(Integer);
989 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
990 default: assert(0 && "DIE Value form not supported yet"); break;
995 //===----------------------------------------------------------------------===//
997 /// EmitValue - Emit string value.
999 void DIEString::EmitValue(const Dwarf &DW, unsigned Form) const {
1000 DW.EmitString(String);
1003 /// SizeOf - Determine size of string value in bytes.
1005 unsigned DIEString::SizeOf(const Dwarf &DW, unsigned Form) const {
1006 return String.size() + sizeof(char); // sizeof('\0');
1009 //===----------------------------------------------------------------------===//
1011 /// EmitValue - Emit label value.
1013 void DIEDwarfLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
1014 DW.EmitReference(Label);
1017 /// SizeOf - Determine size of label value in bytes.
1019 unsigned DIEDwarfLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
1020 return DW.getTargetAsmInfo()->getAddressSize();
1023 //===----------------------------------------------------------------------===//
1025 /// EmitValue - Emit label value.
1027 void DIEObjectLabel::EmitValue(const Dwarf &DW, unsigned Form) const {
1028 DW.EmitReference(Label);
1031 /// SizeOf - Determine size of label value in bytes.
1033 unsigned DIEObjectLabel::SizeOf(const Dwarf &DW, unsigned Form) const {
1034 return DW.getTargetAsmInfo()->getAddressSize();
1037 //===----------------------------------------------------------------------===//
1039 /// EmitValue - Emit delta value.
1041 void DIEDelta::EmitValue(const Dwarf &DW, unsigned Form) const {
1042 DW.EmitDifference(LabelHi, LabelLo);
1045 /// SizeOf - Determine size of delta value in bytes.
1047 unsigned DIEDelta::SizeOf(const Dwarf &DW, unsigned Form) const {
1048 return DW.getTargetAsmInfo()->getAddressSize();
1051 //===----------------------------------------------------------------------===//
1052 /// EmitValue - Emit debug information entry offset.
1054 void DIEntry::EmitValue(const Dwarf &DW, unsigned Form) const {
1055 DW.EmitInt32(Entry->getOffset());
1058 /// SizeOf - Determine size of debug information entry value in bytes.
1060 unsigned DIEntry::SizeOf(const Dwarf &DW, unsigned Form) const {
1061 return sizeof(int32_t);
1064 //===----------------------------------------------------------------------===//
1066 DIEBlock::~DIEBlock() {
1067 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1072 /// ComputeSize - calculate the size of the block.
1074 unsigned DIEBlock::ComputeSize(Dwarf &DW) {
1076 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1077 Size += Values[i]->SizeOf(DW, Forms[i]);
1082 /// BestForm - Choose the best form for data.
1084 unsigned DIEBlock::BestForm() {
1085 if ((unsigned char)Size == Size) return DW_FORM_block1;
1086 if ((unsigned short)Size == Size) return DW_FORM_block2;
1087 if ((unsigned int)Size == Size) return DW_FORM_block4;
1088 return DW_FORM_block;
1091 /// EmitValue - Emit block data.
1093 void DIEBlock::EmitValue(const Dwarf &DW, unsigned Form) const {
1095 case DW_FORM_block1: DW.EmitInt8(Size); break;
1096 case DW_FORM_block2: DW.EmitInt16(Size); break;
1097 case DW_FORM_block4: DW.EmitInt32(Size); break;
1098 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
1099 default: assert(0 && "Improper form for block"); break;
1101 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1103 Values[i]->EmitValue(DW, Forms[i]);
1107 /// SizeOf - Determine size of block data in bytes.
1109 unsigned DIEBlock::SizeOf(const Dwarf &DW, unsigned Form) const {
1111 case DW_FORM_block1: return Size + sizeof(int8_t);
1112 case DW_FORM_block2: return Size + sizeof(int16_t);
1113 case DW_FORM_block4: return Size + sizeof(int32_t);
1114 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
1115 default: assert(0 && "Improper form for block"); break;
1120 /// AddUInt - Add an unsigned integer value.
1122 void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
1123 DIEInteger *DI = new DIEInteger(Integer);
1124 Values.push_back(DI);
1125 if (Form == 0) Form = DI->BestForm(false);
1126 Forms.push_back(Form);
1129 /// AddSInt - Add an signed integer value.
1131 void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
1132 DIEInteger *DI = new DIEInteger(Integer);
1133 Values.push_back(DI);
1134 if (Form == 0) Form = DI->BestForm(true);
1135 Forms.push_back(Form);
1138 /// AddString - Add a std::string value.
1140 void DIEBlock::AddString(unsigned Form, const std::string &String) {
1141 Values.push_back(new DIEString(String));
1142 Forms.push_back(Form);
1145 /// AddLabel - Add a Dwarf label value.
1147 void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
1148 Values.push_back(new DIEDwarfLabel(Label));
1149 Forms.push_back(Form);
1152 /// AddObjectLabel - Add a non-Dwarf label value.
1154 void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
1155 Values.push_back(new DIEObjectLabel(Label));
1156 Forms.push_back(Form);
1159 /// AddDelta - Add a label delta value.
1161 void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
1162 Values.push_back(new DIEDelta(Hi, Lo));
1163 Forms.push_back(Form);
1166 /// AddDIEntry - Add a DIE value.
1168 void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
1169 Values.push_back(new DIEntry(Entry));
1170 Forms.push_back(Form);
1173 //===----------------------------------------------------------------------===//
1175 DIE::DIE(unsigned Tag)
1176 : Abbrev(Tag, DW_CHILDREN_no)
1184 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
1188 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
1193 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
1195 void DIE::AddSiblingOffset() {
1196 DIEInteger *DI = new DIEInteger(0);
1197 Values.insert(Values.begin(), DI);
1198 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
1201 /// AddUInt - Add an unsigned integer attribute data and value.
1203 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
1204 DIEInteger *DI = new DIEInteger(Integer);
1205 Values.push_back(DI);
1206 if (!Form) Form = DI->BestForm(false);
1207 Abbrev.AddAttribute(Attribute, Form);
1210 /// AddSInt - Add an signed integer attribute data and value.
1212 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
1213 DIEInteger *DI = new DIEInteger(Integer);
1214 Values.push_back(DI);
1215 if (!Form) Form = DI->BestForm(true);
1216 Abbrev.AddAttribute(Attribute, Form);
1219 /// AddString - Add a std::string attribute data and value.
1221 void DIE::AddString(unsigned Attribute, unsigned Form,
1222 const std::string &String) {
1223 Values.push_back(new DIEString(String));
1224 Abbrev.AddAttribute(Attribute, Form);
1227 /// AddLabel - Add a Dwarf label attribute data and value.
1229 void DIE::AddLabel(unsigned Attribute, unsigned Form,
1230 const DWLabel &Label) {
1231 Values.push_back(new DIEDwarfLabel(Label));
1232 Abbrev.AddAttribute(Attribute, Form);
1235 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1237 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
1238 const std::string &Label) {
1239 Values.push_back(new DIEObjectLabel(Label));
1240 Abbrev.AddAttribute(Attribute, Form);
1243 /// AddDelta - Add a label delta attribute data and value.
1245 void DIE::AddDelta(unsigned Attribute, unsigned Form,
1246 const DWLabel &Hi, const DWLabel &Lo) {
1247 Values.push_back(new DIEDelta(Hi, Lo));
1248 Abbrev.AddAttribute(Attribute, Form);
1251 /// AddDIEntry - Add a DIE attribute data and value.
1253 void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
1254 Values.push_back(new DIEntry(Entry));
1255 Abbrev.AddAttribute(Attribute, Form);
1258 /// AddBlock - Add block data.
1260 void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
1261 assert(Block->Size && "Block size has not been computed");
1262 Values.push_back(Block);
1263 if (!Form) Form = Block->BestForm();
1264 Abbrev.AddAttribute(Attribute, Form);
1267 /// Complete - Indicate that all attributes have been added and ready to get an
1268 /// abbreviation ID.
1269 void DIE::Complete(Dwarf &DW) {
1270 DW.AssignAbbrevNumber(&Abbrev);
1273 /// AddChild - Add a child to the DIE.
1275 void DIE::AddChild(DIE *Child) {
1276 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
1277 Children.push_back(Child);
1280 //===----------------------------------------------------------------------===//
1284 //===----------------------------------------------------------------------===//
1286 /// PrintHex - Print a value as a hexidecimal value.
1288 void Dwarf::PrintHex(int Value) const {
1289 O << "0x" << std::hex << Value << std::dec;
1292 /// EOL - Print a newline character to asm stream. If a comment is present
1293 /// then it will be printed first. Comments should not contain '\n'.
1294 void Dwarf::EOL(const std::string &Comment) const {
1295 if (DwarfVerbose && !Comment.empty()) {
1297 << TAI->getCommentString()
1304 /// EmitAlign - Print a align directive.
1306 void Dwarf::EmitAlign(unsigned Alignment) const {
1307 O << TAI->getAlignDirective() << Alignment << "\n";
1310 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
1311 /// unsigned leb128 value.
1312 void Dwarf::EmitULEB128Bytes(unsigned Value) const {
1313 if (TAI->hasLEB128()) {
1317 O << TAI->getData8bitsDirective();
1318 PrintULEB128(Value);
1322 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
1323 /// signed leb128 value.
1324 void Dwarf::EmitSLEB128Bytes(int Value) const {
1325 if (TAI->hasLEB128()) {
1329 O << TAI->getData8bitsDirective();
1330 PrintSLEB128(Value);
1334 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
1335 /// representing an unsigned leb128 value.
1336 void Dwarf::PrintULEB128(unsigned Value) const {
1338 unsigned Byte = Value & 0x7f;
1340 if (Value) Byte |= 0x80;
1342 if (Value) O << ", ";
1346 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1348 unsigned Dwarf::SizeULEB128(unsigned Value) {
1352 Size += sizeof(int8_t);
1357 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
1358 /// representing a signed leb128 value.
1359 void Dwarf::PrintSLEB128(int Value) const {
1360 int Sign = Value >> (8 * sizeof(Value) - 1);
1364 unsigned Byte = Value & 0x7f;
1366 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1367 if (IsMore) Byte |= 0x80;
1369 if (IsMore) O << ", ";
1373 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1375 unsigned Dwarf::SizeSLEB128(int Value) {
1377 int Sign = Value >> (8 * sizeof(Value) - 1);
1381 unsigned Byte = Value & 0x7f;
1383 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1384 Size += sizeof(int8_t);
1389 /// EmitInt8 - Emit a byte directive and value.
1391 void Dwarf::EmitInt8(int Value) const {
1392 O << TAI->getData8bitsDirective();
1393 PrintHex(Value & 0xFF);
1396 /// EmitInt16 - Emit a short directive and value.
1398 void Dwarf::EmitInt16(int Value) const {
1399 O << TAI->getData16bitsDirective();
1400 PrintHex(Value & 0xFFFF);
1403 /// EmitInt32 - Emit a long directive and value.
1405 void Dwarf::EmitInt32(int Value) const {
1406 O << TAI->getData32bitsDirective();
1410 /// EmitInt64 - Emit a long long directive and value.
1412 void Dwarf::EmitInt64(uint64_t Value) const {
1413 if (TAI->getData64bitsDirective()) {
1414 O << TAI->getData64bitsDirective() << "0x" << std::hex << Value << std::dec;
1416 if (TD->isBigEndian()) {
1417 EmitInt32(unsigned(Value >> 32)); O << "\n";
1418 EmitInt32(unsigned(Value));
1420 EmitInt32(unsigned(Value)); O << "\n";
1421 EmitInt32(unsigned(Value >> 32));
1426 /// EmitString - Emit a string with quotes and a null terminator.
1427 /// Special characters are emitted properly. (Eg. '\t')
1428 void Dwarf::EmitString(const std::string &String) const {
1429 O << TAI->getAsciiDirective()
1431 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1432 unsigned char C = String[i];
1434 if (!isascii(C) || iscntrl(C)) {
1436 case '\b': O << "\\b"; break;
1437 case '\f': O << "\\f"; break;
1438 case '\n': O << "\\n"; break;
1439 case '\r': O << "\\r"; break;
1440 case '\t': O << "\\t"; break;
1443 O << char('0' + ((C >> 6) & 7));
1444 O << char('0' + ((C >> 3) & 7));
1445 O << char('0' + ((C >> 0) & 7));
1448 } else if (C == '\"') {
1450 } else if (C == '\'') {
1459 /// PrintLabelName - Print label name in form used by Dwarf writer.
1461 void Dwarf::PrintLabelName(const char *Tag, unsigned Number) const {
1462 O << TAI->getPrivateGlobalPrefix()
1465 if (Number) O << Number;
1468 /// EmitLabel - Emit location label for internal use by Dwarf.
1470 void Dwarf::EmitLabel(const char *Tag, unsigned Number) const {
1471 PrintLabelName(Tag, Number);
1475 /// EmitReference - Emit a reference to a label.
1477 void Dwarf::EmitReference(const char *Tag, unsigned Number) const {
1478 if (TAI->getAddressSize() == 4)
1479 O << TAI->getData32bitsDirective();
1481 O << TAI->getData64bitsDirective();
1483 PrintLabelName(Tag, Number);
1485 void Dwarf::EmitReference(const std::string &Name) const {
1486 if (TAI->getAddressSize() == 4)
1487 O << TAI->getData32bitsDirective();
1489 O << TAI->getData64bitsDirective();
1494 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1495 /// assemblers do not accept absolute expressions with data directives, so there
1496 /// is an option (needsSet) to use an intermediary 'set' expression.
1497 void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi,
1498 const char *TagLo, unsigned NumberLo) const {
1499 if (TAI->needsSet()) {
1500 static unsigned SetCounter = 0;
1503 PrintLabelName("set", SetCounter);
1505 PrintLabelName(TagHi, NumberHi);
1507 PrintLabelName(TagLo, NumberLo);
1510 if (TAI->getAddressSize() == sizeof(int32_t))
1511 O << TAI->getData32bitsDirective();
1513 O << TAI->getData64bitsDirective();
1515 PrintLabelName("set", SetCounter);
1519 if (TAI->getAddressSize() == sizeof(int32_t))
1520 O << TAI->getData32bitsDirective();
1522 O << TAI->getData64bitsDirective();
1524 PrintLabelName(TagHi, NumberHi);
1526 PrintLabelName(TagLo, NumberLo);
1530 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
1532 void Dwarf::AssignAbbrevNumber(DIEAbbrev *Abbrev) {
1533 // Profile the node so that we can make it unique.
1534 FoldingSetNodeID ID;
1535 Abbrev->Profile(ID);
1537 // Check the set for priors.
1538 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(Abbrev);
1540 // If it's newly added.
1541 if (InSet == Abbrev) {
1542 // Add to abbreviation list.
1543 Abbreviations.push_back(Abbrev);
1544 // Assign the vector position + 1 as its number.
1545 Abbrev->setNumber(Abbreviations.size());
1547 // Assign existing abbreviation number.
1548 Abbrev->setNumber(InSet->getNumber());
1552 /// NewString - Add a string to the constant pool and returns a label.
1554 DWLabel Dwarf::NewString(const std::string &String) {
1555 unsigned StringID = StringPool.insert(String);
1556 return DWLabel("string", StringID);
1559 /// AddSourceLine - Add location information to specified debug information
1561 void Dwarf::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line){
1563 CompileUnit *FileUnit = FindCompileUnit(File);
1564 unsigned FileID = FileUnit->getID();
1565 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1566 Die->AddUInt(DW_AT_decl_line, 0, Line);
1570 /// AddAddress - Add an address attribute to a die based on the location
1572 void Dwarf::AddAddress(DIE *Die, unsigned Attribute,
1573 const MachineLocation &Location) {
1574 DIEBlock *Block = new DIEBlock();
1575 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1577 if (Location.isRegister()) {
1579 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Reg);
1581 Block->AddUInt(DW_FORM_data1, DW_OP_regx);
1582 Block->AddUInt(DW_FORM_udata, Reg);
1586 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Reg);
1588 Block->AddUInt(DW_FORM_data1, DW_OP_bregx);
1589 Block->AddUInt(DW_FORM_udata, Reg);
1591 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1593 Block->ComputeSize(*this);
1594 Die->AddBlock(Attribute, 0, Block);
1597 /// getDieMapSlotFor - Returns the debug information entry map slot for the
1598 /// specified debug descriptor.
1599 DIE *&Dwarf::getDieMapSlotFor(DebugInfoDesc *DD) {
1600 return DescToDieMap[DD];
1603 /// NewType - Create a new type DIE.
1605 DIE *Dwarf::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1607 // FIXME - Hack for missing types
1608 DIE *Die = new DIE(DW_TAG_base_type);
1609 Die->AddUInt(DW_AT_byte_size, 0, 4);
1610 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1611 Unit->getDie()->AddChild(Die);
1615 // Check for pre-existence.
1616 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
1617 if (Slot) return Slot;
1622 // FIXME - Not sure why programs and variables are coming through here.
1623 // Short cut for handling subprogram types (not really a TyDesc.)
1624 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1625 Slot = Ty = new DIE(DW_TAG_pointer_type);
1626 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1627 Ty->AddString(DW_AT_name, DW_FORM_string, SubprogramTy->getName());
1628 Context->AddChild(Ty);
1631 // Short cut for handling global variable types (not really a TyDesc.)
1632 if (GlobalVariableDesc *GlobalVariableTy =
1633 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1634 Slot = Ty = new DIE(DW_TAG_pointer_type);
1635 Ty->AddUInt(DW_AT_byte_size, 0, TAI->getAddressSize());
1636 Ty->AddString(DW_AT_name, DW_FORM_string, GlobalVariableTy->getName());
1637 Context->AddChild(Ty);
1641 // Get core information.
1642 const std::string &Name = TyDesc->getName();
1643 uint64_t Size = TyDesc->getSize() >> 3;
1645 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1646 // Fundamental types like int, float, bool
1647 Slot = Ty = new DIE(DW_TAG_base_type);
1648 unsigned Encoding = BasicTy->getEncoding();
1649 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
1650 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1651 // Create specific DIE.
1652 Slot = Ty = new DIE(DerivedTy->getTag());
1654 // Map to main type, void will not have a type.
1655 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1656 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1657 NewType(Context, FromTy, Unit));
1659 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1661 unsigned Tag = CompTy->getTag();
1663 // Create specific DIE.
1664 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1667 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1670 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1672 case DW_TAG_array_type: {
1673 // Add element type.
1674 if (TypeDesc *FromTy = CompTy->getFromType()) {
1675 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1676 NewType(Context, FromTy, Unit));
1679 // Don't emit size attribute.
1682 // Construct an anonymous type for index type.
1683 DIE *IndexTy = new DIE(DW_TAG_base_type);
1684 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1685 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1687 Context->AddChild(IndexTy);
1689 // Add subranges to array type.
1690 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1691 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1692 int64_t Lo = SRD->getLo();
1693 int64_t Hi = SRD->getHi();
1694 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1696 // If a range is available.
1698 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1699 // Only add low if non-zero.
1700 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1701 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
1703 Ty->AddChild(Subrange);
1708 case DW_TAG_structure_type:
1709 case DW_TAG_union_type: {
1710 // Add elements to structure type.
1711 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1712 DebugInfoDesc *Element = Elements[i];
1714 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1715 // Add field or base class.
1717 unsigned Tag = MemberDesc->getTag();
1719 // Extract the basic information.
1720 const std::string &Name = MemberDesc->getName();
1721 TypeDesc *MemTy = MemberDesc->getFromType();
1722 uint64_t Size = MemberDesc->getSize();
1723 uint64_t Align = MemberDesc->getAlign();
1724 uint64_t Offset = MemberDesc->getOffset();
1726 // Construct member debug information entry.
1727 DIE *Member = new DIE(Tag);
1729 // Add name if not "".
1730 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1731 // Add location if available.
1732 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1734 // Most of the time the field info is the same as the members.
1735 uint64_t FieldSize = Size;
1736 uint64_t FieldAlign = Align;
1737 uint64_t FieldOffset = Offset;
1739 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1740 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1741 NewType(Context, FromTy, Unit));
1742 FieldSize = FromTy->getSize();
1743 FieldAlign = FromTy->getSize();
1746 // Unless we have a bit field.
1747 if (Tag == DW_TAG_member && FieldSize != Size) {
1748 // Construct the alignment mask.
1749 uint64_t AlignMask = ~(FieldAlign - 1);
1750 // Determine the high bit + 1 of the declared size.
1751 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1752 // Work backwards to determine the base offset of the field.
1753 FieldOffset = HiMark - FieldSize;
1754 // Now normalize offset to the field.
1755 Offset -= FieldOffset;
1757 // Maybe we need to work from the other end.
1758 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1760 // Add size and offset.
1761 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1762 Member->AddUInt(DW_AT_bit_size, 0, Size);
1763 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1766 // Add computation for offset.
1767 DIEBlock *Block = new DIEBlock();
1768 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1769 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1770 Block->ComputeSize(*this);
1771 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1773 // Add accessibility (public default unless is base class.
1774 if (MemberDesc->isProtected()) {
1775 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1776 } else if (MemberDesc->isPrivate()) {
1777 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1778 } else if (Tag == DW_TAG_inheritance) {
1779 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1782 Ty->AddChild(Member);
1783 } else if (GlobalVariableDesc *StaticDesc =
1784 dyn_cast<GlobalVariableDesc>(Element)) {
1785 // Add static member.
1787 // Construct member debug information entry.
1788 DIE *Static = new DIE(DW_TAG_variable);
1790 // Add name and mangled name.
1791 const std::string &Name = StaticDesc->getDisplayName();
1792 const std::string &MangledName = StaticDesc->getName();
1793 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1794 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1798 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1801 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1802 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1803 NewType(Context, StaticTy, Unit));
1807 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1808 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1810 Ty->AddChild(Static);
1811 } else if (SubprogramDesc *MethodDesc =
1812 dyn_cast<SubprogramDesc>(Element)) {
1813 // Add member function.
1815 // Construct member debug information entry.
1816 DIE *Method = new DIE(DW_TAG_subprogram);
1818 // Add name and mangled name.
1819 const std::string &Name = MethodDesc->getDisplayName();
1820 const std::string &MangledName = MethodDesc->getName();
1821 bool IsCTor = false;
1824 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1825 IsCTor = TyDesc->getName() == MangledName;
1827 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1828 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1833 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1836 if (CompositeTypeDesc *MethodTy =
1837 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1838 // Get argument information.
1839 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1844 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1845 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1850 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1851 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1852 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1853 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1854 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1855 Method->AddChild(Arg);
1860 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1861 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1863 Ty->AddChild(Method);
1868 case DW_TAG_enumeration_type: {
1869 // Add enumerators to enumeration type.
1870 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1871 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1872 const std::string &Name = ED->getName();
1873 int64_t Value = ED->getValue();
1874 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1875 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1876 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1877 Ty->AddChild(Enumerator);
1882 case DW_TAG_subroutine_type: {
1883 // Add prototype flag.
1884 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1886 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1887 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
1890 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1891 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1892 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1893 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1903 assert(Ty && "Type not supported yet");
1905 // Add size if non-zero (derived types don't have a size.)
1906 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1907 // Add name if not anonymous or intermediate type.
1908 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1909 // Add source line info if available.
1910 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
1912 // Add to context owner.
1913 Context->AddChild(Ty);
1918 /// NewCompileUnit - Create new compile unit and it's debug information entry.
1920 CompileUnit *Dwarf::NewCompileUnit(CompileUnitDesc *UnitDesc,
1922 // Construct debug information entry.
1923 DIE *Die = new DIE(DW_TAG_compile_unit);
1924 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1925 DWLabel("section_line", 0));
1926 // Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1927 // Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1928 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1929 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1930 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1931 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1933 // Add debug information entry to descriptor map.
1934 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1937 // Construct compile unit.
1938 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1940 // Add Unit to compile unit map.
1941 DescToUnitMap[UnitDesc] = Unit;
1946 /// FindCompileUnit - Get the compile unit for the given descriptor.
1948 CompileUnit *Dwarf::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1949 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1950 assert(Unit && "Missing compile unit.");
1954 /// NewGlobalVariable - Add a new global variable DIE.
1956 DIE *Dwarf::NewGlobalVariable(GlobalVariableDesc *GVD) {
1957 // Get the compile unit context.
1958 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1959 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1961 // Check for pre-existence.
1962 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1963 if (Slot) return Slot;
1965 // Get the global variable itself.
1966 GlobalVariable *GV = GVD->getGlobalVariable();
1968 const std::string &Name = GVD->hasMangledName() ? GVD->getDisplayName()
1970 const std::string &MangledName = GVD->hasMangledName() ? GVD->getName()
1972 // Get the global's type.
1973 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
1975 // Create the globale variable DIE.
1976 DIE *VariableDie = new DIE(DW_TAG_variable);
1977 VariableDie->AddString(DW_AT_name, DW_FORM_string, Name);
1978 if (!MangledName.empty()) {
1979 VariableDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1982 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1983 VariableDie->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1985 // Add source line info if available.
1986 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1988 // Work up linkage name.
1989 const std::string LinkageName = Asm->getGlobalLinkName(GV);
1992 DIEBlock *Block = new DIEBlock();
1993 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1994 Block->AddObjectLabel(DW_FORM_udata, LinkageName);
1995 Block->ComputeSize(*this);
1996 VariableDie->AddBlock(DW_AT_location, 0, Block);
2001 // Add to context owner.
2002 Unit->getDie()->AddChild(VariableDie);
2004 // Expose as global.
2005 // FIXME - need to check external flag.
2006 Unit->AddGlobal(Name, VariableDie);
2011 /// NewSubprogram - Add a new subprogram DIE.
2013 DIE *Dwarf::NewSubprogram(SubprogramDesc *SPD) {
2014 // Get the compile unit context.
2015 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
2016 CompileUnit *Unit = FindCompileUnit(UnitDesc);
2018 // Check for pre-existence.
2019 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
2020 if (Slot) return Slot;
2022 // Gather the details (simplify add attribute code.)
2023 const std::string &Name = SPD->hasMangledName() ? SPD->getDisplayName()
2025 const std::string &MangledName = SPD->hasMangledName() ? SPD->getName()
2027 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
2028 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
2030 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
2031 SubprogramDie->AddString(DW_AT_name, DW_FORM_string, Name);
2032 if (!MangledName.empty()) {
2033 SubprogramDie->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
2037 SubprogramDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2039 SubprogramDie->AddUInt(DW_AT_external, DW_FORM_flag, IsExternal);
2040 SubprogramDie->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
2042 // Add source line info if available.
2043 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
2046 Slot = SubprogramDie;
2048 // Add to context owner.
2049 Unit->getDie()->AddChild(SubprogramDie);
2051 // Expose as global.
2052 Unit->AddGlobal(Name, SubprogramDie);
2054 return SubprogramDie;
2057 /// NewScopeVariable - Create a new scope variable.
2059 DIE *Dwarf::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
2060 // Get the descriptor.
2061 VariableDesc *VD = DV->getDesc();
2063 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
2065 switch (VD->getTag()) {
2066 case DW_TAG_return_variable: return NULL;
2067 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
2068 case DW_TAG_auto_variable: // fall thru
2069 default: Tag = DW_TAG_variable; break;
2072 // Define variable debug information entry.
2073 DIE *VariableDie = new DIE(Tag);
2074 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
2076 // Add source line info if available.
2077 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
2079 // Add variable type.
2080 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
2081 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
2083 // Add variable address.
2084 MachineLocation Location;
2085 RI->getLocation(*MF, DV->getFrameIndex(), Location);
2086 AddAddress(VariableDie, DW_AT_location, Location);
2091 /// ConstructScope - Construct the components of a scope.
2093 void Dwarf::ConstructScope(DebugScope *ParentScope,
2094 DIE *ParentDie, CompileUnit *Unit) {
2095 // Add variables to scope.
2096 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
2097 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2098 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
2099 if (VariableDie) ParentDie->AddChild(VariableDie);
2102 // Add nested scopes.
2103 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
2104 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2105 // Define the Scope debug information entry.
2106 DebugScope *Scope = Scopes[j];
2107 // FIXME - Ignore inlined functions for the time being.
2108 if (!Scope->getParent()) continue;
2110 unsigned StartID = Scope->getStartLabelID();
2111 unsigned EndID = Scope->getEndLabelID();
2113 // Throw out scope if block is discarded.
2114 if (StartID && !DebugInfo->isLabelValid(StartID)) continue;
2115 if (EndID && !DebugInfo->isLabelValid(EndID)) continue;
2117 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2119 // Add the scope bounds.
2121 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2122 DWLabel("loc", StartID));
2124 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2125 DWLabel("func_begin", SubprogramCount));
2128 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2129 DWLabel("loc", EndID));
2131 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2132 DWLabel("func_end", SubprogramCount));
2135 // Add the scope contents.
2136 ConstructScope(Scope, ScopeDie, Unit);
2137 ParentDie->AddChild(ScopeDie);
2141 /// ConstructRootScope - Construct the scope for the subprogram.
2143 void Dwarf::ConstructRootScope(DebugScope *RootScope) {
2144 // Exit if there is no root scope.
2145 if (!RootScope) return;
2147 // Get the subprogram debug information entry.
2148 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
2150 // Get the compile unit context.
2151 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
2152 CompileUnit *Unit = FindCompileUnit(UnitDesc);
2154 // Get the subprogram die.
2155 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
2156 assert(SPDie && "Missing subprogram descriptor");
2158 // Add the function bounds.
2159 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
2160 DWLabel("func_begin", SubprogramCount));
2161 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
2162 DWLabel("func_end", SubprogramCount));
2163 MachineLocation Location(RI->getFrameRegister(*MF));
2164 AddAddress(SPDie, DW_AT_frame_base, Location);
2166 ConstructScope(RootScope, SPDie, Unit);
2169 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2170 /// tools to recognize the object file contains Dwarf information.
2172 void Dwarf::EmitInitial() {
2173 // Check to see if we already emitted intial headers.
2174 if (didInitial) return;
2177 // Dwarf sections base addresses.
2178 if (TAI->getDwarfRequiresFrameSection()) {
2179 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2180 EmitLabel("section_frame", 0);
2182 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2183 EmitLabel("section_info", 0);
2184 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2185 EmitLabel("section_abbrev", 0);
2186 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2187 EmitLabel("section_aranges", 0);
2188 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2189 EmitLabel("section_macinfo", 0);
2190 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2191 EmitLabel("section_line", 0);
2192 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2193 EmitLabel("section_loc", 0);
2194 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2195 EmitLabel("section_pubnames", 0);
2196 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2197 EmitLabel("section_str", 0);
2198 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2199 EmitLabel("section_ranges", 0);
2200 Asm->SwitchToTextSection(TAI->getTextSection());
2201 EmitLabel("text_begin", 0);
2202 Asm->SwitchToDataSection(TAI->getDataSection());
2203 EmitLabel("data_begin", 0);
2205 // Emit common frame information.
2206 EmitInitialDebugFrame();
2209 /// EmitDIE - Recusively Emits a debug information entry.
2211 void Dwarf::EmitDIE(DIE *Die) const {
2212 // Get the abbreviation for this DIE.
2213 unsigned AbbrevNumber = Die->getAbbrevNumber();
2214 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2218 // Emit the code (index) for the abbreviation.
2219 EmitULEB128Bytes(AbbrevNumber);
2220 EOL(std::string("Abbrev [" +
2221 utostr(AbbrevNumber) +
2222 "] 0x" + utohexstr(Die->getOffset()) +
2223 ":0x" + utohexstr(Die->getSize()) + " " +
2224 TagString(Abbrev->getTag())));
2226 const std::vector<DIEValue *> &Values = Die->getValues();
2227 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2229 // Emit the DIE attribute values.
2230 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2231 unsigned Attr = AbbrevData[i].getAttribute();
2232 unsigned Form = AbbrevData[i].getForm();
2233 assert(Form && "Too many attributes for DIE (check abbreviation)");
2236 case DW_AT_sibling: {
2237 EmitInt32(Die->SiblingOffset());
2241 // Emit an attribute using the defined form.
2242 Values[i]->EmitValue(*this, Form);
2247 EOL(AttributeString(Attr));
2250 // Emit the DIE children if any.
2251 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2252 const std::vector<DIE *> &Children = Die->getChildren();
2254 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2255 EmitDIE(Children[j]);
2258 EmitInt8(0); EOL("End Of Children Mark");
2262 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2264 unsigned Dwarf::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2265 // Get the children.
2266 const std::vector<DIE *> &Children = Die->getChildren();
2268 // If not last sibling and has children then add sibling offset attribute.
2269 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2271 // Record the abbreviation.
2272 Die->Complete(*this);
2274 // Get the abbreviation for this DIE.
2275 unsigned AbbrevNumber = Die->getAbbrevNumber();
2276 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2279 Die->setOffset(Offset);
2281 // Start the size with the size of abbreviation code.
2282 Offset += SizeULEB128(AbbrevNumber);
2284 const std::vector<DIEValue *> &Values = Die->getValues();
2285 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2287 // Emit the DIE attribute values.
2288 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2289 // Size attribute value.
2290 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2293 // Emit the DIE children if any.
2294 if (!Children.empty()) {
2295 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2296 "Children flag not set");
2298 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2299 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2302 // End of children marker.
2303 Offset += sizeof(int8_t);
2306 Die->setSize(Offset - Die->getOffset());
2310 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2312 void Dwarf::SizeAndOffsets() {
2314 // Process each compile unit.
2315 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2316 CompileUnit *Unit = CompileUnits[i];
2317 if (Unit->hasContent()) {
2318 // Compute size of compile unit header
2319 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2320 sizeof(int16_t) + // DWARF version number
2321 sizeof(int32_t) + // Offset Into Abbrev. Section
2322 sizeof(int8_t); // Pointer Size (in bytes)
2323 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
2328 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
2330 void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
2331 std::vector<MachineMove *> &Moves) {
2332 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
2333 MachineMove *Move = Moves[i];
2334 unsigned LabelID = Move->getLabelID();
2336 // Throw out move if the label is invalid.
2337 if (LabelID && !DebugInfo->isLabelValid(LabelID)) continue;
2339 const MachineLocation &Dst = Move->getDestination();
2340 const MachineLocation &Src = Move->getSource();
2342 // Advance row if new location.
2343 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
2344 EmitInt8(DW_CFA_advance_loc4);
2345 EOL("DW_CFA_advance_loc4");
2346 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
2349 BaseLabelID = LabelID;
2354 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2355 TargetFrameInfo::StackGrowsUp ?
2356 TAI->getAddressSize() : -TAI->getAddressSize();
2358 // If advancing cfa.
2359 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
2360 if (!Src.isRegister()) {
2361 if (Src.getRegister() == MachineLocation::VirtualFP) {
2362 EmitInt8(DW_CFA_def_cfa_offset);
2363 EOL("DW_CFA_def_cfa_offset");
2365 EmitInt8(DW_CFA_def_cfa);
2366 EOL("DW_CFA_def_cfa");
2368 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
2372 int Offset = Src.getOffset() / stackGrowth;
2374 EmitULEB128Bytes(Offset);
2377 assert(0 && "Machine move no supported yet.");
2380 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
2381 int Offset = Dst.getOffset() / stackGrowth;
2384 EmitInt8(DW_CFA_offset_extended_sf);
2385 EOL("DW_CFA_offset_extended_sf");
2386 EmitULEB128Bytes(Reg);
2388 EmitSLEB128Bytes(Offset);
2390 } else if (Reg < 64) {
2391 EmitInt8(DW_CFA_offset + Reg);
2392 EOL("DW_CFA_offset + Reg");
2393 EmitULEB128Bytes(Offset);
2396 EmitInt8(DW_CFA_offset_extended);
2397 EOL("DW_CFA_offset_extended");
2398 EmitULEB128Bytes(Reg);
2400 EmitULEB128Bytes(Offset);
2407 /// EmitDebugInfo - Emit the debug info section.
2409 void Dwarf::EmitDebugInfo() const {
2410 // Start debug info section.
2411 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2413 // Process each compile unit.
2414 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2415 CompileUnit *Unit = CompileUnits[i];
2417 if (Unit->hasContent()) {
2418 DIE *Die = Unit->getDie();
2419 // Emit the compile units header.
2420 EmitLabel("info_begin", Unit->getID());
2421 // Emit size of content not including length itself
2422 unsigned ContentSize = Die->getSize() +
2423 sizeof(int16_t) + // DWARF version number
2424 sizeof(int32_t) + // Offset Into Abbrev. Section
2425 sizeof(int8_t); // Pointer Size (in bytes)
2427 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
2428 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2429 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
2430 EOL("Offset Into Abbrev. Section");
2431 EmitInt8(TAI->getAddressSize()); EOL("Address Size (in bytes)");
2434 EmitLabel("info_end", Unit->getID());
2441 /// EmitAbbreviations - Emit the abbreviation section.
2443 void Dwarf::EmitAbbreviations() const {
2444 // Check to see if it is worth the effort.
2445 if (!Abbreviations.empty()) {
2446 // Start the debug abbrev section.
2447 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2449 EmitLabel("abbrev_begin", 0);
2451 // For each abbrevation.
2452 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2453 // Get abbreviation data
2454 const DIEAbbrev *Abbrev = Abbreviations[i];
2456 // Emit the abbrevations code (base 1 index.)
2457 EmitULEB128Bytes(Abbrev->getNumber()); EOL("Abbreviation Code");
2459 // Emit the abbreviations data.
2460 Abbrev->Emit(*this);
2465 EmitLabel("abbrev_end", 0);
2471 /// EmitDebugLines - Emit source line information.
2473 void Dwarf::EmitDebugLines() const {
2474 // Minimum line delta, thus ranging from -10..(255-10).
2475 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2476 // Maximum line delta, thus ranging from -10..(255-10).
2477 const int MaxLineDelta = 255 + MinLineDelta;
2479 // Start the dwarf line section.
2480 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2482 // Construct the section header.
2484 EmitDifference("line_end", 0, "line_begin", 0);
2485 EOL("Length of Source Line Info");
2486 EmitLabel("line_begin", 0);
2488 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2490 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2491 EOL("Prolog Length");
2492 EmitLabel("line_prolog_begin", 0);
2494 EmitInt8(1); EOL("Minimum Instruction Length");
2496 EmitInt8(1); EOL("Default is_stmt_start flag");
2498 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2500 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2502 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2504 // Line number standard opcode encodings argument count
2505 EmitInt8(0); EOL("DW_LNS_copy arg count");
2506 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2507 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2508 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2509 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2510 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2511 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2512 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2513 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2515 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2516 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2518 // Emit directories.
2519 for (unsigned DirectoryID = 1, NDID = Directories.size();
2520 DirectoryID <= NDID; ++DirectoryID) {
2521 EmitString(Directories[DirectoryID]); EOL("Directory");
2523 EmitInt8(0); EOL("End of directories");
2526 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2527 SourceID <= NSID; ++SourceID) {
2528 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2529 EmitString(SourceFile.getName()); EOL("Source");
2530 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2531 EmitULEB128Bytes(0); EOL("Mod date");
2532 EmitULEB128Bytes(0); EOL("File size");
2534 EmitInt8(0); EOL("End of files");
2536 EmitLabel("line_prolog_end", 0);
2538 // A sequence for each text section.
2539 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2540 // Isolate current sections line info.
2541 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2545 << TAI->getCommentString() << " "
2547 << SectionMap[j + 1].c_str() << "\n";
2550 // Dwarf assumes we start with first line of first source file.
2551 unsigned Source = 1;
2554 // Construct rows of the address, source, line, column matrix.
2555 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2556 const SourceLineInfo &LineInfo = LineInfos[i];
2557 unsigned LabelID = LineInfo.getLabelID();
2559 // Source line labels are validated at the MachineDebugInfo level.
2562 unsigned SourceID = LineInfo.getSourceID();
2563 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2564 unsigned DirectoryID = SourceFile.getDirectoryID();
2566 << TAI->getCommentString() << " "
2567 << Directories[DirectoryID]
2568 << SourceFile.getName() << ":"
2569 << LineInfo.getLine() << "\n";
2572 // Define the line address.
2573 EmitInt8(0); EOL("Extended Op");
2574 EmitInt8(4 + 1); EOL("Op size");
2575 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2576 EmitReference("loc", LabelID); EOL("Location label");
2578 // If change of source, then switch to the new source.
2579 if (Source != LineInfo.getSourceID()) {
2580 Source = LineInfo.getSourceID();
2581 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2582 EmitULEB128Bytes(Source); EOL("New Source");
2585 // If change of line.
2586 if (Line != LineInfo.getLine()) {
2587 // Determine offset.
2588 int Offset = LineInfo.getLine() - Line;
2589 int Delta = Offset - MinLineDelta;
2592 Line = LineInfo.getLine();
2594 // If delta is small enough and in range...
2595 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2596 // ... then use fast opcode.
2597 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2599 // ... otherwise use long hand.
2600 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2601 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2602 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2605 // Copy the previous row (different address or source)
2606 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2610 // Define last address of section.
2611 EmitInt8(0); EOL("Extended Op");
2612 EmitInt8(4 + 1); EOL("Op size");
2613 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2614 EmitReference("section_end", j + 1); EOL("Section end label");
2616 // Mark end of matrix.
2617 EmitInt8(0); EOL("DW_LNE_end_sequence");
2618 EmitULEB128Bytes(1); O << "\n";
2619 EmitInt8(1); O << "\n";
2622 EmitLabel("line_end", 0);
2627 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2629 void Dwarf::EmitInitialDebugFrame() {
2630 if (!TAI->getDwarfRequiresFrameSection())
2634 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2635 TargetFrameInfo::StackGrowsUp ?
2636 TAI->getAddressSize() : -TAI->getAddressSize();
2638 // Start the dwarf frame section.
2639 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2641 EmitLabel("frame_common", 0);
2642 EmitDifference("frame_common_end", 0,
2643 "frame_common_begin", 0);
2644 EOL("Length of Common Information Entry");
2646 EmitLabel("frame_common_begin", 0);
2647 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2648 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2649 EmitString(""); EOL("CIE Augmentation");
2650 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2651 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2652 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2654 std::vector<MachineMove *> Moves;
2655 RI->getInitialFrameState(Moves);
2656 EmitFrameMoves(NULL, 0, Moves);
2657 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2660 EmitLabel("frame_common_end", 0);
2665 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2667 void Dwarf::EmitFunctionDebugFrame() {
2668 if (!TAI->getDwarfRequiresFrameSection())
2671 // Start the dwarf frame section.
2672 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2674 EmitDifference("frame_end", SubprogramCount,
2675 "frame_begin", SubprogramCount);
2676 EOL("Length of Frame Information Entry");
2678 EmitLabel("frame_begin", SubprogramCount);
2680 EmitDifference("frame_common", 0, "section_frame", 0);
2681 EOL("FDE CIE offset");
2683 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2684 EmitDifference("func_end", SubprogramCount,
2685 "func_begin", SubprogramCount);
2686 EOL("FDE address range");
2688 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2690 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2693 EmitLabel("frame_end", SubprogramCount);
2698 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2700 void Dwarf::EmitDebugPubNames() {
2701 // Start the dwarf pubnames section.
2702 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2704 // Process each compile unit.
2705 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2706 CompileUnit *Unit = CompileUnits[i];
2708 if (Unit->hasContent()) {
2709 EmitDifference("pubnames_end", Unit->getID(),
2710 "pubnames_begin", Unit->getID());
2711 EOL("Length of Public Names Info");
2713 EmitLabel("pubnames_begin", Unit->getID());
2715 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2717 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2718 EOL("Offset of Compilation Unit Info");
2720 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2721 EOL("Compilation Unit Length");
2723 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2725 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2728 const std::string &Name = GI->first;
2729 DIE * Entity = GI->second;
2731 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2732 EmitString(Name); EOL("External Name");
2735 EmitInt32(0); EOL("End Mark");
2736 EmitLabel("pubnames_end", Unit->getID());
2743 /// EmitDebugStr - Emit visible names into a debug str section.
2745 void Dwarf::EmitDebugStr() {
2746 // Check to see if it is worth the effort.
2747 if (!StringPool.empty()) {
2748 // Start the dwarf str section.
2749 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2751 // For each of strings in the string pool.
2752 for (unsigned StringID = 1, N = StringPool.size();
2753 StringID <= N; ++StringID) {
2754 // Emit a label for reference from debug information entries.
2755 EmitLabel("string", StringID);
2756 // Emit the string itself.
2757 const std::string &String = StringPool[StringID];
2758 EmitString(String); O << "\n";
2765 /// EmitDebugLoc - Emit visible names into a debug loc section.
2767 void Dwarf::EmitDebugLoc() {
2768 // Start the dwarf loc section.
2769 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2774 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2776 void Dwarf::EmitDebugARanges() {
2777 // Start the dwarf aranges section.
2778 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2782 // Process each compile unit.
2783 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2784 CompileUnit *Unit = CompileUnits[i];
2786 if (Unit->hasContent()) {
2787 // Don't include size of length
2788 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2790 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2792 EmitReference("info_begin", Unit->getID());
2793 EOL("Offset of Compilation Unit Info");
2795 EmitInt8(TAI->getAddressSize()); EOL("Size of Address");
2797 EmitInt8(0); EOL("Size of Segment Descriptor");
2799 EmitInt16(0); EOL("Pad (1)");
2800 EmitInt16(0); EOL("Pad (2)");
2803 EmitReference("text_begin", 0); EOL("Address");
2804 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2806 EmitInt32(0); EOL("EOM (1)");
2807 EmitInt32(0); EOL("EOM (2)");
2815 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2817 void Dwarf::EmitDebugRanges() {
2818 // Start the dwarf ranges section.
2819 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2824 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2826 void Dwarf::EmitDebugMacInfo() {
2827 // Start the dwarf macinfo section.
2828 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2833 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2835 void Dwarf::ConstructCompileUnitDIEs() {
2836 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2838 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2839 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
2840 CompileUnits.push_back(Unit);
2844 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2846 void Dwarf::ConstructGlobalDIEs() {
2847 std::vector<GlobalVariableDesc *> GlobalVariables =
2848 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2850 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2851 GlobalVariableDesc *GVD = GlobalVariables[i];
2852 NewGlobalVariable(GVD);
2856 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2858 void Dwarf::ConstructSubprogramDIEs() {
2859 std::vector<SubprogramDesc *> Subprograms =
2860 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2862 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2863 SubprogramDesc *SPD = Subprograms[i];
2868 //===----------------------------------------------------------------------===//
2869 /// Dwarf implemenation.
2872 Dwarf::Dwarf(std::ostream &OS, AsmPrinter *A,
2873 const TargetAsmInfo *T)
2877 , TD(Asm->TM.getTargetData())
2878 , RI(Asm->TM.getRegisterInfo())
2884 , SubprogramCount(0)
2886 , AbbreviationsSet()
2892 , SectionSourceLines()
2896 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2897 delete CompileUnits[i];
2901 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2902 /// created it. Set by the target AsmPrinter.
2903 void Dwarf::SetDebugInfo(MachineDebugInfo *DI) {
2904 // Make sure initial declarations are made.
2905 if (!DebugInfo && DI->hasInfo()) {
2909 // Emit initial sections
2912 // Create all the compile unit DIEs.
2913 ConstructCompileUnitDIEs();
2915 // Create DIEs for each of the externally visible global variables.
2916 ConstructGlobalDIEs();
2918 // Create DIEs for each of the externally visible subprograms.
2919 ConstructSubprogramDIEs();
2921 // Prime section data.
2922 SectionMap.insert(std::string("\t") + TAI->getTextSection());
2926 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
2928 void Dwarf::BeginModule(Module *M) {
2931 if (!ShouldEmitDwarf()) return;
2932 EOL("Dwarf Begin Module");
2935 /// EndModule - Emit all Dwarf sections that should come after the content.
2937 void Dwarf::EndModule() {
2938 if (!ShouldEmitDwarf()) return;
2939 EOL("Dwarf End Module");
2941 // Standard sections final addresses.
2942 Asm->SwitchToTextSection(TAI->getTextSection());
2943 EmitLabel("text_end", 0);
2944 Asm->SwitchToDataSection(TAI->getDataSection());
2945 EmitLabel("data_end", 0);
2947 // End text sections.
2948 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2949 Asm->SwitchToTextSection(SectionMap[i].c_str());
2950 EmitLabel("section_end", i);
2953 // Compute DIE offsets and sizes.
2956 // Emit all the DIEs into a debug info section
2959 // Corresponding abbreviations into a abbrev section.
2960 EmitAbbreviations();
2962 // Emit source line correspondence into a debug line section.
2965 // Emit info into a debug pubnames section.
2966 EmitDebugPubNames();
2968 // Emit info into a debug str section.
2971 // Emit info into a debug loc section.
2974 // Emit info into a debug aranges section.
2977 // Emit info into a debug ranges section.
2980 // Emit info into a debug macinfo section.
2984 /// BeginFunction - Gather pre-function debug information. Assumes being
2985 /// emitted immediately after the function entry point.
2986 void Dwarf::BeginFunction(MachineFunction *MF) {
2989 if (!ShouldEmitDwarf()) return;
2990 EOL("Dwarf Begin Function");
2992 // Begin accumulating function debug information.
2993 DebugInfo->BeginFunction(MF);
2995 // Assumes in correct section after the entry point.
2996 EmitLabel("func_begin", ++SubprogramCount);
2999 /// EndFunction - Gather and emit post-function debug information.
3001 void Dwarf::EndFunction() {
3002 if (!ShouldEmitDwarf()) return;
3003 EOL("Dwarf End Function");
3005 // Define end label for subprogram.
3006 EmitLabel("func_end", SubprogramCount);
3008 // Get function line info.
3009 const std::vector<SourceLineInfo> &LineInfos = DebugInfo->getSourceLines();
3011 if (!LineInfos.empty()) {
3012 // Get section line info.
3013 unsigned ID = SectionMap.insert(Asm->CurrentSection);
3014 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3015 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3016 // Append the function info to section info.
3017 SectionLineInfos.insert(SectionLineInfos.end(),
3018 LineInfos.begin(), LineInfos.end());
3021 // Construct scopes for subprogram.
3022 ConstructRootScope(DebugInfo->getRootScope());
3024 // Emit function frame information.
3025 EmitFunctionDebugFrame();
3027 // Reset the line numbers for the next function.
3028 DebugInfo->ClearLineInfo();
3030 // Clear function debug information.
3031 DebugInfo->EndFunction();
3035 //===----------------------------------------------------------------------===//
3036 /// DwarfWriter Implementation
3038 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3039 const TargetAsmInfo *T) {
3040 DW = new Dwarf(OS, A, T);
3043 DwarfWriter::~DwarfWriter() {
3047 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
3048 /// created it. Set by the target AsmPrinter.
3049 void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
3050 DW->SetDebugInfo(DI);
3053 /// BeginModule - Emit all Dwarf sections that should come prior to the
3055 void DwarfWriter::BeginModule(Module *M) {
3059 /// EndModule - Emit all Dwarf sections that should come after the content.
3061 void DwarfWriter::EndModule() {
3065 /// BeginFunction - Gather pre-function debug information. Assumes being
3066 /// emitted immediately after the function entry point.
3067 void DwarfWriter::BeginFunction(MachineFunction *MF) {
3068 DW->BeginFunction(MF);
3071 /// EndFunction - Gather and emit post-function debug information.
3073 void DwarfWriter::EndFunction() {