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 info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DwarfWriter.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/FoldingSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/UniqueVector.h"
20 #include "llvm/Module.h"
21 #include "llvm/Type.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineLocation.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DataTypes.h"
30 #include "llvm/Support/Mangler.h"
31 #include "llvm/System/Path.h"
32 #include "llvm/Target/TargetAsmInfo.h"
33 #include "llvm/Target/MRegisterInfo.h"
34 #include "llvm/Target/TargetData.h"
35 #include "llvm/Target/TargetFrameInfo.h"
36 #include "llvm/Target/TargetInstrInfo.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
42 using namespace llvm::dwarf;
46 //===----------------------------------------------------------------------===//
48 /// Configuration values for initial hash set sizes (log2).
50 static const unsigned InitDiesSetSize = 9; // 512
51 static const unsigned InitAbbreviationsSetSize = 9; // 512
52 static const unsigned InitValuesSetSize = 9; // 512
54 //===----------------------------------------------------------------------===//
55 /// Forward declarations.
60 //===----------------------------------------------------------------------===//
61 /// DWLabel - Labels are used to track locations in the assembler file.
62 /// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
63 /// where the tag is a category of label (Ex. location) and number is a value
64 /// unique in that category.
67 /// Tag - Label category tag. Should always be a staticly declared C string.
71 /// Number - Value to make label unique.
75 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
77 void Profile(FoldingSetNodeID &ID) const {
78 ID.AddString(std::string(Tag));
79 ID.AddInteger(Number);
83 void print(std::ostream *O) const {
86 void print(std::ostream &O) const {
88 if (Number) O << Number;
93 //===----------------------------------------------------------------------===//
94 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
95 /// Dwarf abbreviation.
98 /// Attribute - Dwarf attribute code.
102 /// Form - Dwarf form code.
107 DIEAbbrevData(unsigned A, unsigned F)
113 unsigned getAttribute() const { return Attribute; }
114 unsigned getForm() const { return Form; }
116 /// Profile - Used to gather unique data for the abbreviation folding set.
118 void Profile(FoldingSetNodeID &ID)const {
119 ID.AddInteger(Attribute);
124 //===----------------------------------------------------------------------===//
125 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
126 /// information object.
127 class DIEAbbrev : public FoldingSetNode {
129 /// Tag - Dwarf tag code.
133 /// Unique number for node.
137 /// ChildrenFlag - Dwarf children flag.
139 unsigned ChildrenFlag;
141 /// Data - Raw data bytes for abbreviation.
143 std::vector<DIEAbbrevData> Data;
147 DIEAbbrev(unsigned T, unsigned C)
155 unsigned getTag() const { return Tag; }
156 unsigned getNumber() const { return Number; }
157 unsigned getChildrenFlag() const { return ChildrenFlag; }
158 const std::vector<DIEAbbrevData> &getData() const { return Data; }
159 void setTag(unsigned T) { Tag = T; }
160 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
161 void setNumber(unsigned N) { Number = N; }
163 /// AddAttribute - Adds another set of attribute information to the
165 void AddAttribute(unsigned Attribute, unsigned Form) {
166 Data.push_back(DIEAbbrevData(Attribute, Form));
169 /// AddFirstAttribute - Adds a set of attribute information to the front
170 /// of the abbreviation.
171 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
172 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
175 /// Profile - Used to gather unique data for the abbreviation folding set.
177 void Profile(FoldingSetNodeID &ID) {
179 ID.AddInteger(ChildrenFlag);
181 // For each attribute description.
182 for (unsigned i = 0, N = Data.size(); i < N; ++i)
186 /// Emit - Print the abbreviation using the specified Dwarf writer.
188 void Emit(const DwarfDebug &DD) const;
191 void print(std::ostream *O) {
194 void print(std::ostream &O);
199 //===----------------------------------------------------------------------===//
200 /// DIE - A structured debug information entry. Has an abbreviation which
201 /// describes it's organization.
202 class DIE : public FoldingSetNode {
204 /// Abbrev - Buffer for constructing abbreviation.
208 /// Offset - Offset in debug info section.
212 /// Size - Size of instance + children.
218 std::vector<DIE *> Children;
220 /// Attributes values.
222 std::vector<DIEValue *> Values;
225 explicit DIE(unsigned Tag)
226 : Abbrev(Tag, DW_CHILDREN_no)
235 DIEAbbrev &getAbbrev() { return Abbrev; }
236 unsigned getAbbrevNumber() const {
237 return Abbrev.getNumber();
239 unsigned getTag() const { return Abbrev.getTag(); }
240 unsigned getOffset() const { return Offset; }
241 unsigned getSize() const { return Size; }
242 const std::vector<DIE *> &getChildren() const { return Children; }
243 std::vector<DIEValue *> &getValues() { return Values; }
244 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
245 void setOffset(unsigned O) { Offset = O; }
246 void setSize(unsigned S) { Size = S; }
248 /// AddValue - Add a value and attributes to a DIE.
250 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
251 Abbrev.AddAttribute(Attribute, Form);
252 Values.push_back(Value);
255 /// SiblingOffset - Return the offset of the debug information entry's
257 unsigned SiblingOffset() const { return Offset + Size; }
259 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
261 void AddSiblingOffset();
263 /// AddChild - Add a child to the DIE.
265 void AddChild(DIE *Child) {
266 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
267 Children.push_back(Child);
270 /// Detach - Detaches objects connected to it after copying.
276 /// Profile - Used to gather unique data for the value folding set.
278 void Profile(FoldingSetNodeID &ID) ;
281 void print(std::ostream *O, unsigned IncIndent = 0) {
282 if (O) print(*O, IncIndent);
284 void print(std::ostream &O, unsigned IncIndent = 0);
289 //===----------------------------------------------------------------------===//
290 /// DIEValue - A debug information entry value.
292 class DIEValue : public FoldingSetNode {
304 /// Type - Type of data stored in the value.
308 explicit DIEValue(unsigned T)
311 virtual ~DIEValue() {}
314 unsigned getType() const { return Type; }
316 // Implement isa/cast/dyncast.
317 static bool classof(const DIEValue *) { return true; }
319 /// EmitValue - Emit value via the Dwarf writer.
321 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
323 /// SizeOf - Return the size of a value in bytes.
325 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
327 /// Profile - Used to gather unique data for the value folding set.
329 virtual void Profile(FoldingSetNodeID &ID) = 0;
332 void print(std::ostream *O) {
335 virtual void print(std::ostream &O) = 0;
340 //===----------------------------------------------------------------------===//
341 /// DWInteger - An integer value DIE.
343 class DIEInteger : public DIEValue {
348 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
350 // Implement isa/cast/dyncast.
351 static bool classof(const DIEInteger *) { return true; }
352 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
354 /// BestForm - Choose the best form for integer.
356 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
358 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
359 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
360 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
362 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
363 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
364 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
366 return DW_FORM_data8;
369 /// EmitValue - Emit integer of appropriate size.
371 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
373 /// SizeOf - Determine size of integer value in bytes.
375 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
377 /// Profile - Used to gather unique data for the value folding set.
379 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
380 ID.AddInteger(isInteger);
381 ID.AddInteger(Integer);
383 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
386 virtual void print(std::ostream &O) {
387 O << "Int: " << (int64_t)Integer
388 << " 0x" << std::hex << Integer << std::dec;
393 //===----------------------------------------------------------------------===//
394 /// DIEString - A string value DIE.
396 class DIEString : public DIEValue {
398 const std::string String;
400 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
402 // Implement isa/cast/dyncast.
403 static bool classof(const DIEString *) { return true; }
404 static bool classof(const DIEValue *S) { return S->Type == isString; }
406 /// EmitValue - Emit string value.
408 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
410 /// SizeOf - Determine size of string value in bytes.
412 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
413 return String.size() + sizeof(char); // sizeof('\0');
416 /// Profile - Used to gather unique data for the value folding set.
418 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
419 ID.AddInteger(isString);
420 ID.AddString(String);
422 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
425 virtual void print(std::ostream &O) {
426 O << "Str: \"" << String << "\"";
431 //===----------------------------------------------------------------------===//
432 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
434 class DIEDwarfLabel : public DIEValue {
439 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
441 // Implement isa/cast/dyncast.
442 static bool classof(const DIEDwarfLabel *) { return true; }
443 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
445 /// EmitValue - Emit label value.
447 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
449 /// SizeOf - Determine size of label value in bytes.
451 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
453 /// Profile - Used to gather unique data for the value folding set.
455 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
456 ID.AddInteger(isLabel);
459 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
462 virtual void print(std::ostream &O) {
470 //===----------------------------------------------------------------------===//
471 /// DIEObjectLabel - A label to an object in code or data.
473 class DIEObjectLabel : public DIEValue {
475 const std::string Label;
477 explicit DIEObjectLabel(const std::string &L)
478 : DIEValue(isAsIsLabel), Label(L) {}
480 // Implement isa/cast/dyncast.
481 static bool classof(const DIEObjectLabel *) { return true; }
482 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
484 /// EmitValue - Emit label value.
486 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
488 /// SizeOf - Determine size of label value in bytes.
490 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
492 /// Profile - Used to gather unique data for the value folding set.
494 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
495 ID.AddInteger(isAsIsLabel);
498 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
501 virtual void print(std::ostream &O) {
502 O << "Obj: " << Label;
507 //===----------------------------------------------------------------------===//
508 /// DIEDelta - A simple label difference DIE.
510 class DIEDelta : public DIEValue {
512 const DWLabel LabelHi;
513 const DWLabel LabelLo;
515 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
516 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
518 // Implement isa/cast/dyncast.
519 static bool classof(const DIEDelta *) { return true; }
520 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
522 /// EmitValue - Emit delta value.
524 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
526 /// SizeOf - Determine size of delta value in bytes.
528 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
530 /// Profile - Used to gather unique data for the value folding set.
532 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
533 const DWLabel &LabelLo) {
534 ID.AddInteger(isDelta);
538 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
541 virtual void print(std::ostream &O) {
550 //===----------------------------------------------------------------------===//
551 /// DIEntry - A pointer to another debug information entry. An instance of this
552 /// class can also be used as a proxy for a debug information entry not yet
553 /// defined (ie. types.)
554 class DIEntry : public DIEValue {
558 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
560 // Implement isa/cast/dyncast.
561 static bool classof(const DIEntry *) { return true; }
562 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
564 /// EmitValue - Emit debug information entry offset.
566 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
568 /// SizeOf - Determine size of debug information entry in bytes.
570 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
571 return sizeof(int32_t);
574 /// Profile - Used to gather unique data for the value folding set.
576 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
577 ID.AddInteger(isEntry);
578 ID.AddPointer(Entry);
580 virtual void Profile(FoldingSetNodeID &ID) {
581 ID.AddInteger(isEntry);
584 ID.AddPointer(Entry);
591 virtual void print(std::ostream &O) {
592 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
597 //===----------------------------------------------------------------------===//
598 /// DIEBlock - A block of values. Primarily used for location expressions.
600 class DIEBlock : public DIEValue, public DIE {
602 unsigned Size; // Size in bytes excluding size header.
612 // Implement isa/cast/dyncast.
613 static bool classof(const DIEBlock *) { return true; }
614 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
616 /// ComputeSize - calculate the size of the block.
618 unsigned ComputeSize(DwarfDebug &DD);
620 /// BestForm - Choose the best form for data.
622 unsigned BestForm() const {
623 if ((unsigned char)Size == Size) return DW_FORM_block1;
624 if ((unsigned short)Size == Size) return DW_FORM_block2;
625 if ((unsigned int)Size == Size) return DW_FORM_block4;
626 return DW_FORM_block;
629 /// EmitValue - Emit block data.
631 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
633 /// SizeOf - Determine size of block data in bytes.
635 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
638 /// Profile - Used to gather unique data for the value folding set.
640 virtual void Profile(FoldingSetNodeID &ID) {
641 ID.AddInteger(isBlock);
646 virtual void print(std::ostream &O) {
653 //===----------------------------------------------------------------------===//
654 /// CompileUnit - This dwarf writer support class manages information associate
655 /// with a source file.
658 /// Desc - Compile unit debug descriptor.
660 CompileUnitDesc *Desc;
662 /// ID - File identifier for source.
666 /// Die - Compile unit debug information entry.
670 /// DescToDieMap - Tracks the mapping of unit level debug informaton
671 /// descriptors to debug information entries.
672 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
674 /// DescToDIEntryMap - Tracks the mapping of unit level debug informaton
675 /// descriptors to debug information entries using a DIEntry proxy.
676 std::map<DebugInfoDesc *, DIEntry *> DescToDIEntryMap;
678 /// Globals - A map of globally visible named entities for this unit.
680 std::map<std::string, DIE *> Globals;
682 /// DiesSet - Used to uniquely define dies within the compile unit.
684 FoldingSet<DIE> DiesSet;
686 /// Dies - List of all dies in the compile unit.
688 std::vector<DIE *> Dies;
691 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
698 , DiesSet(InitDiesSetSize)
705 for (unsigned i = 0, N = Dies.size(); i < N; ++i)
710 CompileUnitDesc *getDesc() const { return Desc; }
711 unsigned getID() const { return ID; }
712 DIE* getDie() const { return Die; }
713 std::map<std::string, DIE *> &getGlobals() { return Globals; }
715 /// hasContent - Return true if this compile unit has something to write out.
717 bool hasContent() const {
718 return !Die->getChildren().empty();
721 /// AddGlobal - Add a new global entity to the compile unit.
723 void AddGlobal(const std::string &Name, DIE *Die) {
727 /// getDieMapSlotFor - Returns the debug information entry map slot for the
728 /// specified debug descriptor.
729 DIE *&getDieMapSlotFor(DebugInfoDesc *DID) {
730 return DescToDieMap[DID];
733 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
734 /// specified debug descriptor.
735 DIEntry *&getDIEntrySlotFor(DebugInfoDesc *DID) {
736 return DescToDIEntryMap[DID];
739 /// AddDie - Adds or interns the DIE to the compile unit.
741 DIE *AddDie(DIE &Buffer) {
745 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
748 Die = new DIE(Buffer);
749 DiesSet.InsertNode(Die, Where);
750 this->Die->AddChild(Die);
758 //===----------------------------------------------------------------------===//
759 /// Dwarf - Emits general Dwarf directives.
765 //===--------------------------------------------------------------------===//
766 // Core attributes used by the Dwarf writer.
770 /// O - Stream to .s file.
774 /// Asm - Target of Dwarf emission.
778 /// TAI - Target Asm Printer.
779 const TargetAsmInfo *TAI;
781 /// TD - Target data.
782 const TargetData *TD;
784 /// RI - Register Information.
785 const MRegisterInfo *RI;
787 /// M - Current module.
791 /// MF - Current machine function.
795 /// MMI - Collected machine module information.
797 MachineModuleInfo *MMI;
799 /// SubprogramCount - The running count of functions being compiled.
801 unsigned SubprogramCount;
803 /// Flavor - A unique string indicating what dwarf producer this is, used to
805 const char * const Flavor;
808 Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
813 , TD(Asm->TM.getTargetData())
814 , RI(Asm->TM.getRegisterInfo())
826 //===--------------------------------------------------------------------===//
829 AsmPrinter *getAsm() const { return Asm; }
830 MachineModuleInfo *getMMI() const { return MMI; }
831 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
833 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
835 if (isInSection && TAI->getDwarfSectionOffsetDirective())
836 O << TAI->getDwarfSectionOffsetDirective();
837 else if (Force32Bit || TAI->getAddressSize() == sizeof(int32_t))
838 O << TAI->getData32bitsDirective();
840 O << TAI->getData64bitsDirective();
843 /// PrintLabelName - Print label name in form used by Dwarf writer.
845 void PrintLabelName(DWLabel Label) const {
846 PrintLabelName(Label.Tag, Label.Number);
848 void PrintLabelName(const char *Tag, unsigned Number) const {
849 O << TAI->getPrivateGlobalPrefix() << Tag;
850 if (Number) O << Number;
853 void PrintLabelName(const char *Tag, unsigned Number,
854 const char *Suffix) const {
855 O << TAI->getPrivateGlobalPrefix() << Tag;
856 if (Number) O << Number;
860 /// EmitLabel - Emit location label for internal use by Dwarf.
862 void EmitLabel(DWLabel Label) const {
863 EmitLabel(Label.Tag, Label.Number);
865 void EmitLabel(const char *Tag, unsigned Number) const {
866 PrintLabelName(Tag, Number);
870 /// EmitReference - Emit a reference to a label.
872 void EmitReference(DWLabel Label, bool IsPCRelative = false) const {
873 EmitReference(Label.Tag, Label.Number, IsPCRelative);
875 void EmitReference(const char *Tag, unsigned Number,
876 bool IsPCRelative = false) const {
878 PrintLabelName(Tag, Number);
880 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
882 void EmitReference(const std::string &Name, bool IsPCRelative = false) const {
887 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
890 /// EmitDifference - Emit the difference between two labels. Some
891 /// assemblers do not behave with absolute expressions with data directives,
892 /// so there is an option (needsSet) to use an intermediary set expression.
893 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
894 bool IsSmall = false) {
895 EmitDifference(LabelHi.Tag, LabelHi.Number,
896 LabelLo.Tag, LabelLo.Number,
899 void EmitDifference(const char *TagHi, unsigned NumberHi,
900 const char *TagLo, unsigned NumberLo,
901 bool IsSmall = false) {
902 if (TAI->needsSet()) {
904 PrintLabelName("set", SetCounter, Flavor);
906 PrintLabelName(TagHi, NumberHi);
908 PrintLabelName(TagLo, NumberLo);
911 PrintRelDirective(IsSmall);
912 PrintLabelName("set", SetCounter, Flavor);
915 PrintRelDirective(IsSmall);
917 PrintLabelName(TagHi, NumberHi);
919 PrintLabelName(TagLo, NumberLo);
923 void EmitSectionOffset(const char* Label, const char* Section,
924 unsigned LabelNumber, unsigned SectionNumber,
925 bool IsSmall = false, bool isEH = false) {
926 bool printAbsolute = false;
927 if (TAI->needsSet()) {
929 PrintLabelName("set", SetCounter, Flavor);
931 PrintLabelName(Label, LabelNumber);
934 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
936 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
938 if (!printAbsolute) {
940 PrintLabelName(Section, SectionNumber);
944 PrintRelDirective(IsSmall);
946 PrintLabelName("set", SetCounter, Flavor);
949 PrintRelDirective(IsSmall, true);
951 PrintLabelName(Label, LabelNumber);
954 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
956 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
958 if (!printAbsolute) {
960 PrintLabelName(Section, SectionNumber);
965 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
967 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
968 const std::vector<MachineMove> &Moves) {
970 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
971 TargetFrameInfo::StackGrowsUp ?
972 TAI->getAddressSize() : -TAI->getAddressSize();
973 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
975 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
976 const MachineMove &Move = Moves[i];
977 unsigned LabelID = Move.getLabelID();
980 LabelID = MMI->MappedLabel(LabelID);
982 // Throw out move if the label is invalid.
983 if (!LabelID) continue;
986 const MachineLocation &Dst = Move.getDestination();
987 const MachineLocation &Src = Move.getSource();
989 // Advance row if new location.
990 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
991 Asm->EmitInt8(DW_CFA_advance_loc4);
992 Asm->EOL("DW_CFA_advance_loc4");
993 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
996 BaseLabelID = LabelID;
1001 // If advancing cfa.
1002 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1003 if (!Src.isRegister()) {
1004 if (Src.getRegister() == MachineLocation::VirtualFP) {
1005 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1006 Asm->EOL("DW_CFA_def_cfa_offset");
1008 Asm->EmitInt8(DW_CFA_def_cfa);
1009 Asm->EOL("DW_CFA_def_cfa");
1010 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1011 Asm->EOL("Register");
1014 int Offset = -Src.getOffset();
1016 Asm->EmitULEB128Bytes(Offset);
1019 assert(0 && "Machine move no supported yet.");
1021 } else if (Src.isRegister() &&
1022 Src.getRegister() == MachineLocation::VirtualFP) {
1023 if (Dst.isRegister()) {
1024 Asm->EmitInt8(DW_CFA_def_cfa_register);
1025 Asm->EOL("DW_CFA_def_cfa_register");
1026 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister()));
1027 Asm->EOL("Register");
1029 assert(0 && "Machine move no supported yet.");
1032 unsigned Reg = RI->getDwarfRegNum(Src.getRegister());
1033 int Offset = Dst.getOffset() / stackGrowth;
1036 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1037 Asm->EOL("DW_CFA_offset_extended_sf");
1038 Asm->EmitULEB128Bytes(Reg);
1040 Asm->EmitSLEB128Bytes(Offset);
1042 } else if (Reg < 64) {
1043 Asm->EmitInt8(DW_CFA_offset + Reg);
1044 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1045 Asm->EmitULEB128Bytes(Offset);
1048 Asm->EmitInt8(DW_CFA_offset_extended);
1049 Asm->EOL("DW_CFA_offset_extended");
1050 Asm->EmitULEB128Bytes(Reg);
1052 Asm->EmitULEB128Bytes(Offset);
1061 //===----------------------------------------------------------------------===//
1062 /// DwarfDebug - Emits Dwarf debug directives.
1064 class DwarfDebug : public Dwarf {
1067 //===--------------------------------------------------------------------===//
1068 // Attributes used to construct specific Dwarf sections.
1071 /// CompileUnits - All the compile units involved in this build. The index
1072 /// of each entry in this vector corresponds to the sources in MMI.
1073 std::vector<CompileUnit *> CompileUnits;
1075 /// AbbreviationsSet - Used to uniquely define abbreviations.
1077 FoldingSet<DIEAbbrev> AbbreviationsSet;
1079 /// Abbreviations - A list of all the unique abbreviations in use.
1081 std::vector<DIEAbbrev *> Abbreviations;
1083 /// ValuesSet - Used to uniquely define values.
1085 FoldingSet<DIEValue> ValuesSet;
1087 /// Values - A list of all the unique values in use.
1089 std::vector<DIEValue *> Values;
1091 /// StringPool - A UniqueVector of strings used by indirect references.
1093 UniqueVector<std::string> StringPool;
1095 /// UnitMap - Map debug information descriptor to compile unit.
1097 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
1099 /// SectionMap - Provides a unique id per text section.
1101 UniqueVector<std::string> SectionMap;
1103 /// SectionSourceLines - Tracks line numbers per text section.
1105 std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
1107 /// didInitial - Flag to indicate if initial emission has been done.
1111 /// shouldEmit - Flag to indicate if debug information should be emitted.
1115 struct FunctionDebugFrameInfo {
1117 std::vector<MachineMove> Moves;
1119 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
1120 Number(Num), Moves(M) { }
1123 std::vector<FunctionDebugFrameInfo> DebugFrames;
1127 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1129 bool ShouldEmitDwarf() const { return shouldEmit; }
1131 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
1133 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1134 // Profile the node so that we can make it unique.
1135 FoldingSetNodeID ID;
1138 // Check the set for priors.
1139 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1141 // If it's newly added.
1142 if (InSet == &Abbrev) {
1143 // Add to abbreviation list.
1144 Abbreviations.push_back(&Abbrev);
1145 // Assign the vector position + 1 as its number.
1146 Abbrev.setNumber(Abbreviations.size());
1148 // Assign existing abbreviation number.
1149 Abbrev.setNumber(InSet->getNumber());
1153 /// NewString - Add a string to the constant pool and returns a label.
1155 DWLabel NewString(const std::string &String) {
1156 unsigned StringID = StringPool.insert(String);
1157 return DWLabel("string", StringID);
1160 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1162 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1166 FoldingSetNodeID ID;
1167 DIEntry::Profile(ID, Entry);
1169 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1171 if (Value) return Value;
1173 Value = new DIEntry(Entry);
1174 ValuesSet.InsertNode(Value, Where);
1176 Value = new DIEntry(Entry);
1179 Values.push_back(Value);
1183 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1185 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1186 Value->Entry = Entry;
1187 // Add to values set if not already there. If it is, we merely have a
1188 // duplicate in the values list (no harm.)
1189 ValuesSet.GetOrInsertNode(Value);
1192 /// AddUInt - Add an unsigned integer attribute data and value.
1194 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1195 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1197 FoldingSetNodeID ID;
1198 DIEInteger::Profile(ID, Integer);
1200 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1202 Value = new DIEInteger(Integer);
1203 ValuesSet.InsertNode(Value, Where);
1204 Values.push_back(Value);
1207 Die->AddValue(Attribute, Form, Value);
1210 /// AddSInt - Add an signed integer attribute data and value.
1212 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1213 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1215 FoldingSetNodeID ID;
1216 DIEInteger::Profile(ID, (uint64_t)Integer);
1218 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1220 Value = new DIEInteger(Integer);
1221 ValuesSet.InsertNode(Value, Where);
1222 Values.push_back(Value);
1225 Die->AddValue(Attribute, Form, Value);
1228 /// AddString - Add a std::string attribute data and value.
1230 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1231 const std::string &String) {
1232 FoldingSetNodeID ID;
1233 DIEString::Profile(ID, String);
1235 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1237 Value = new DIEString(String);
1238 ValuesSet.InsertNode(Value, Where);
1239 Values.push_back(Value);
1242 Die->AddValue(Attribute, Form, Value);
1245 /// AddLabel - Add a Dwarf label attribute data and value.
1247 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1248 const DWLabel &Label) {
1249 FoldingSetNodeID ID;
1250 DIEDwarfLabel::Profile(ID, Label);
1252 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1254 Value = new DIEDwarfLabel(Label);
1255 ValuesSet.InsertNode(Value, Where);
1256 Values.push_back(Value);
1259 Die->AddValue(Attribute, Form, Value);
1262 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1264 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1265 const std::string &Label) {
1266 FoldingSetNodeID ID;
1267 DIEObjectLabel::Profile(ID, Label);
1269 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1271 Value = new DIEObjectLabel(Label);
1272 ValuesSet.InsertNode(Value, Where);
1273 Values.push_back(Value);
1276 Die->AddValue(Attribute, Form, Value);
1279 /// AddDelta - Add a label delta attribute data and value.
1281 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1282 const DWLabel &Hi, const DWLabel &Lo) {
1283 FoldingSetNodeID ID;
1284 DIEDelta::Profile(ID, Hi, Lo);
1286 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1288 Value = new DIEDelta(Hi, Lo);
1289 ValuesSet.InsertNode(Value, Where);
1290 Values.push_back(Value);
1293 Die->AddValue(Attribute, Form, Value);
1296 /// AddDIEntry - Add a DIE attribute data and value.
1298 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1299 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1302 /// AddBlock - Add block data.
1304 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1305 Block->ComputeSize(*this);
1306 FoldingSetNodeID ID;
1309 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1312 ValuesSet.InsertNode(Value, Where);
1313 Values.push_back(Value);
1315 // Already exists, reuse the previous one.
1317 Block = cast<DIEBlock>(Value);
1320 Die->AddValue(Attribute, Block->BestForm(), Value);
1325 /// AddSourceLine - Add location information to specified debug information
1327 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1329 CompileUnit *FileUnit = FindCompileUnit(File);
1330 unsigned FileID = FileUnit->getID();
1331 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1332 AddUInt(Die, DW_AT_decl_line, 0, Line);
1336 /// AddAddress - Add an address attribute to a die based on the location
1338 void AddAddress(DIE *Die, unsigned Attribute,
1339 const MachineLocation &Location) {
1340 unsigned Reg = RI->getDwarfRegNum(Location.getRegister());
1341 DIEBlock *Block = new DIEBlock();
1343 if (Location.isRegister()) {
1345 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1347 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1348 AddUInt(Block, 0, DW_FORM_udata, Reg);
1352 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1354 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1355 AddUInt(Block, 0, DW_FORM_udata, Reg);
1357 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1360 AddBlock(Die, Attribute, 0, Block);
1363 /// AddBasicType - Add a new basic type attribute to the specified entity.
1365 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1366 const std::string &Name,
1367 unsigned Encoding, unsigned Size) {
1368 DIE *Die = ConstructBasicType(Unit, Name, Encoding, Size);
1369 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1372 /// ConstructBasicType - Construct a new basic type.
1374 DIE *ConstructBasicType(CompileUnit *Unit,
1375 const std::string &Name,
1376 unsigned Encoding, unsigned Size) {
1377 DIE Buffer(DW_TAG_base_type);
1378 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1379 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1380 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1381 return Unit->AddDie(Buffer);
1384 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1386 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1387 DIE *Die = ConstructPointerType(Unit, Name);
1388 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, Die);
1391 /// ConstructPointerType - Construct a new pointer type.
1393 DIE *ConstructPointerType(CompileUnit *Unit, const std::string &Name) {
1394 DIE Buffer(DW_TAG_pointer_type);
1395 AddUInt(&Buffer, DW_AT_byte_size, 0, TAI->getAddressSize());
1396 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1397 return Unit->AddDie(Buffer);
1400 /// AddType - Add a new type attribute to the specified entity.
1402 void AddType(DIE *Entity, TypeDesc *TyDesc, CompileUnit *Unit) {
1404 AddBasicType(Entity, Unit, "", DW_ATE_signed, sizeof(int32_t));
1406 // Check for pre-existence.
1407 DIEntry *&Slot = Unit->getDIEntrySlotFor(TyDesc);
1409 // If it exists then use the existing value.
1411 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1415 if (SubprogramDesc *SubprogramTy = dyn_cast<SubprogramDesc>(TyDesc)) {
1416 // FIXME - Not sure why programs and variables are coming through here.
1417 // Short cut for handling subprogram types (not really a TyDesc.)
1418 AddPointerType(Entity, Unit, SubprogramTy->getName());
1419 } else if (GlobalVariableDesc *GlobalTy =
1420 dyn_cast<GlobalVariableDesc>(TyDesc)) {
1421 // FIXME - Not sure why programs and variables are coming through here.
1422 // Short cut for handling global variable types (not really a TyDesc.)
1423 AddPointerType(Entity, Unit, GlobalTy->getName());
1426 Slot = NewDIEntry();
1429 DIE Buffer(DW_TAG_base_type);
1430 ConstructType(Buffer, TyDesc, Unit);
1432 // Add debug information entry to entity and unit.
1433 DIE *Die = Unit->AddDie(Buffer);
1434 SetDIEntry(Slot, Die);
1435 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1440 /// ConstructType - Adds all the required attributes to the type.
1442 void ConstructType(DIE &Buffer, TypeDesc *TyDesc, CompileUnit *Unit) {
1443 // Get core information.
1444 const std::string &Name = TyDesc->getName();
1445 uint64_t Size = TyDesc->getSize() >> 3;
1447 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1448 // Fundamental types like int, float, bool
1449 Buffer.setTag(DW_TAG_base_type);
1450 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BasicTy->getEncoding());
1451 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1453 unsigned Tag = DerivedTy->getTag();
1454 // FIXME - Workaround for templates.
1455 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1456 // Pointers, typedefs et al.
1458 // Map to main type, void will not have a type.
1459 if (TypeDesc *FromTy = DerivedTy->getFromType())
1460 AddType(&Buffer, FromTy, Unit);
1461 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)){
1463 unsigned Tag = CompTy->getTag();
1465 // Set tag accordingly.
1466 if (Tag == DW_TAG_vector_type)
1467 Buffer.setTag(DW_TAG_array_type);
1471 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1474 case DW_TAG_vector_type:
1475 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1477 case DW_TAG_array_type: {
1478 // Add element type.
1479 if (TypeDesc *FromTy = CompTy->getFromType())
1480 AddType(&Buffer, FromTy, Unit);
1482 // Don't emit size attribute.
1485 // Construct an anonymous type for index type.
1486 DIE *IndexTy = ConstructBasicType(Unit, "", DW_ATE_signed,
1489 // Add subranges to array type.
1490 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1491 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1492 int64_t Lo = SRD->getLo();
1493 int64_t Hi = SRD->getHi();
1494 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1496 // If a range is available.
1498 AddDIEntry(Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1499 // Only add low if non-zero.
1500 if (Lo) AddSInt(Subrange, DW_AT_lower_bound, 0, Lo);
1501 AddSInt(Subrange, DW_AT_upper_bound, 0, Hi);
1504 Buffer.AddChild(Subrange);
1508 case DW_TAG_structure_type:
1509 case DW_TAG_union_type: {
1510 // Add elements to structure type.
1511 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1512 DebugInfoDesc *Element = Elements[i];
1514 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)){
1515 // Add field or base class.
1517 unsigned Tag = MemberDesc->getTag();
1519 // Extract the basic information.
1520 const std::string &Name = MemberDesc->getName();
1521 uint64_t Size = MemberDesc->getSize();
1522 uint64_t Align = MemberDesc->getAlign();
1523 uint64_t Offset = MemberDesc->getOffset();
1525 // Construct member debug information entry.
1526 DIE *Member = new DIE(Tag);
1528 // Add name if not "".
1530 AddString(Member, DW_AT_name, DW_FORM_string, Name);
1531 // Add location if available.
1532 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1534 // Most of the time the field info is the same as the members.
1535 uint64_t FieldSize = Size;
1536 uint64_t FieldAlign = Align;
1537 uint64_t FieldOffset = Offset;
1539 // Set the member type.
1540 TypeDesc *FromTy = MemberDesc->getFromType();
1541 AddType(Member, FromTy, Unit);
1543 // Walk up typedefs until a real size is found.
1545 if (FromTy->getTag() != DW_TAG_typedef) {
1546 FieldSize = FromTy->getSize();
1547 FieldAlign = FromTy->getSize();
1551 FromTy = cast<DerivedTypeDesc>(FromTy)->getFromType();
1554 // Unless we have a bit field.
1555 if (Tag == DW_TAG_member && FieldSize != Size) {
1556 // Construct the alignment mask.
1557 uint64_t AlignMask = ~(FieldAlign - 1);
1558 // Determine the high bit + 1 of the declared size.
1559 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1560 // Work backwards to determine the base offset of the field.
1561 FieldOffset = HiMark - FieldSize;
1562 // Now normalize offset to the field.
1563 Offset -= FieldOffset;
1565 // Maybe we need to work from the other end.
1566 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1568 // Add size and offset.
1569 AddUInt(Member, DW_AT_byte_size, 0, FieldSize >> 3);
1570 AddUInt(Member, DW_AT_bit_size, 0, Size);
1571 AddUInt(Member, DW_AT_bit_offset, 0, Offset);
1574 // Add computation for offset.
1575 DIEBlock *Block = new DIEBlock();
1576 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1577 AddUInt(Block, 0, DW_FORM_udata, FieldOffset >> 3);
1578 AddBlock(Member, DW_AT_data_member_location, 0, Block);
1580 // Add accessibility (public default unless is base class.
1581 if (MemberDesc->isProtected()) {
1582 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_protected);
1583 } else if (MemberDesc->isPrivate()) {
1584 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_private);
1585 } else if (Tag == DW_TAG_inheritance) {
1586 AddUInt(Member, DW_AT_accessibility, 0, DW_ACCESS_public);
1589 Buffer.AddChild(Member);
1590 } else if (GlobalVariableDesc *StaticDesc =
1591 dyn_cast<GlobalVariableDesc>(Element)) {
1592 // Add static member.
1594 // Construct member debug information entry.
1595 DIE *Static = new DIE(DW_TAG_variable);
1597 // Add name and mangled name.
1598 const std::string &Name = StaticDesc->getName();
1599 const std::string &LinkageName = StaticDesc->getLinkageName();
1600 AddString(Static, DW_AT_name, DW_FORM_string, Name);
1601 if (!LinkageName.empty()) {
1602 AddString(Static, DW_AT_MIPS_linkage_name, DW_FORM_string,
1607 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1610 if (TypeDesc *StaticTy = StaticDesc->getType())
1611 AddType(Static, StaticTy, Unit);
1614 if (!StaticDesc->isStatic())
1615 AddUInt(Static, DW_AT_external, DW_FORM_flag, 1);
1616 AddUInt(Static, DW_AT_declaration, DW_FORM_flag, 1);
1618 Buffer.AddChild(Static);
1619 } else if (SubprogramDesc *MethodDesc =
1620 dyn_cast<SubprogramDesc>(Element)) {
1621 // Add member function.
1623 // Construct member debug information entry.
1624 DIE *Method = new DIE(DW_TAG_subprogram);
1626 // Add name and mangled name.
1627 const std::string &Name = MethodDesc->getName();
1628 const std::string &LinkageName = MethodDesc->getLinkageName();
1630 AddString(Method, DW_AT_name, DW_FORM_string, Name);
1631 bool IsCTor = TyDesc->getName() == Name;
1633 if (!LinkageName.empty()) {
1634 AddString(Method, DW_AT_MIPS_linkage_name, DW_FORM_string,
1639 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1642 if (CompositeTypeDesc *MethodTy =
1643 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1644 // Get argument information.
1645 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1650 AddType(Method, dyn_cast<TypeDesc>(Args[0]), Unit);
1654 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1655 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1656 AddType(Arg, cast<TypeDesc>(Args[i]), Unit);
1657 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1);
1658 Method->AddChild(Arg);
1663 if (!MethodDesc->isStatic())
1664 AddUInt(Method, DW_AT_external, DW_FORM_flag, 1);
1665 AddUInt(Method, DW_AT_declaration, DW_FORM_flag, 1);
1667 Buffer.AddChild(Method);
1672 case DW_TAG_enumeration_type: {
1673 // Add enumerators to enumeration type.
1674 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1675 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1676 const std::string &Name = ED->getName();
1677 int64_t Value = ED->getValue();
1678 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1679 AddString(Enumerator, DW_AT_name, DW_FORM_string, Name);
1680 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1681 Buffer.AddChild(Enumerator);
1686 case DW_TAG_subroutine_type: {
1687 // Add prototype flag.
1688 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1690 AddType(&Buffer, dyn_cast<TypeDesc>(Elements[0]), Unit);
1693 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1694 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1695 AddType(Arg, cast<TypeDesc>(Elements[i]), Unit);
1696 Buffer.AddChild(Arg);
1705 // Add size if non-zero (derived types don't have a size.)
1706 if (Size) AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1707 // Add name if not anonymous or intermediate type.
1708 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1709 // Add source line info if available.
1710 AddSourceLine(&Buffer, TyDesc->getFile(), TyDesc->getLine());
1713 /// NewCompileUnit - Create new compile unit and it's debug information entry.
1715 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
1716 // Construct debug information entry.
1717 DIE *Die = new DIE(DW_TAG_compile_unit);
1718 if (TAI->isAbsoluteDebugSectionOffsets())
1719 AddLabel(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0));
1721 AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
1722 DWLabel("section_line", 0));
1723 AddString(Die, DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1724 AddUInt (Die, DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1725 AddString(Die, DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1726 AddString(Die, DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1728 // Construct compile unit.
1729 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1731 // Add Unit to compile unit map.
1732 DescToUnitMap[UnitDesc] = Unit;
1737 /// GetBaseCompileUnit - Get the main compile unit.
1739 CompileUnit *GetBaseCompileUnit() const {
1740 CompileUnit *Unit = CompileUnits[0];
1741 assert(Unit && "Missing compile unit.");
1745 /// FindCompileUnit - Get the compile unit for the given descriptor.
1747 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc) {
1748 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1749 assert(Unit && "Missing compile unit.");
1753 /// NewGlobalVariable - Add a new global variable DIE.
1755 DIE *NewGlobalVariable(GlobalVariableDesc *GVD) {
1756 // Get the compile unit context.
1757 CompileUnitDesc *UnitDesc =
1758 static_cast<CompileUnitDesc *>(GVD->getContext());
1759 CompileUnit *Unit = GetBaseCompileUnit();
1761 // Check for pre-existence.
1762 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1763 if (Slot) return Slot;
1765 // Get the global variable itself.
1766 GlobalVariable *GV = GVD->getGlobalVariable();
1768 const std::string &Name = GVD->getName();
1769 const std::string &FullName = GVD->getFullName();
1770 const std::string &LinkageName = GVD->getLinkageName();
1771 // Create the global's variable DIE.
1772 DIE *VariableDie = new DIE(DW_TAG_variable);
1773 AddString(VariableDie, DW_AT_name, DW_FORM_string, Name);
1774 if (!LinkageName.empty()) {
1775 AddString(VariableDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1778 AddType(VariableDie, GVD->getType(), Unit);
1779 if (!GVD->isStatic())
1780 AddUInt(VariableDie, DW_AT_external, DW_FORM_flag, 1);
1782 // Add source line info if available.
1783 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1786 DIEBlock *Block = new DIEBlock();
1787 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
1788 AddObjectLabel(Block, 0, DW_FORM_udata, Asm->getGlobalLinkName(GV));
1789 AddBlock(VariableDie, DW_AT_location, 0, Block);
1794 // Add to context owner.
1795 Unit->getDie()->AddChild(VariableDie);
1797 // Expose as global.
1798 // FIXME - need to check external flag.
1799 Unit->AddGlobal(FullName, VariableDie);
1804 /// NewSubprogram - Add a new subprogram DIE.
1806 DIE *NewSubprogram(SubprogramDesc *SPD) {
1807 // Get the compile unit context.
1808 CompileUnitDesc *UnitDesc =
1809 static_cast<CompileUnitDesc *>(SPD->getContext());
1810 CompileUnit *Unit = GetBaseCompileUnit();
1812 // Check for pre-existence.
1813 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1814 if (Slot) return Slot;
1816 // Gather the details (simplify add attribute code.)
1817 const std::string &Name = SPD->getName();
1818 const std::string &FullName = SPD->getFullName();
1819 const std::string &LinkageName = SPD->getLinkageName();
1821 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1822 AddString(SubprogramDie, DW_AT_name, DW_FORM_string, Name);
1823 if (!LinkageName.empty()) {
1824 AddString(SubprogramDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1827 if (SPD->getType()) AddType(SubprogramDie, SPD->getType(), Unit);
1828 if (!SPD->isStatic())
1829 AddUInt(SubprogramDie, DW_AT_external, DW_FORM_flag, 1);
1830 AddUInt(SubprogramDie, DW_AT_prototyped, DW_FORM_flag, 1);
1832 // Add source line info if available.
1833 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1836 Slot = SubprogramDie;
1838 // Add to context owner.
1839 Unit->getDie()->AddChild(SubprogramDie);
1841 // Expose as global.
1842 Unit->AddGlobal(FullName, SubprogramDie);
1844 return SubprogramDie;
1847 /// NewScopeVariable - Create a new scope variable.
1849 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1850 // Get the descriptor.
1851 VariableDesc *VD = DV->getDesc();
1853 // Translate tag to proper Dwarf tag. The result variable is dropped for
1856 switch (VD->getTag()) {
1857 case DW_TAG_return_variable: return NULL;
1858 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1859 case DW_TAG_auto_variable: // fall thru
1860 default: Tag = DW_TAG_variable; break;
1863 // Define variable debug information entry.
1864 DIE *VariableDie = new DIE(Tag);
1865 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD->getName());
1867 // Add source line info if available.
1868 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1870 // Add variable type.
1871 AddType(VariableDie, VD->getType(), Unit);
1873 // Add variable address.
1874 MachineLocation Location;
1875 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1876 AddAddress(VariableDie, DW_AT_location, Location);
1881 /// ConstructScope - Construct the components of a scope.
1883 void ConstructScope(DebugScope *ParentScope,
1884 unsigned ParentStartID, unsigned ParentEndID,
1885 DIE *ParentDie, CompileUnit *Unit) {
1886 // Add variables to scope.
1887 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1888 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1889 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1890 if (VariableDie) ParentDie->AddChild(VariableDie);
1893 // Add nested scopes.
1894 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1895 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1896 // Define the Scope debug information entry.
1897 DebugScope *Scope = Scopes[j];
1898 // FIXME - Ignore inlined functions for the time being.
1899 if (!Scope->getParent()) continue;
1901 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
1902 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
1904 // Ignore empty scopes.
1905 if (StartID == EndID && StartID != 0) continue;
1906 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
1908 if (StartID == ParentStartID && EndID == ParentEndID) {
1909 // Just add stuff to the parent scope.
1910 ConstructScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
1912 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1914 // Add the scope bounds.
1916 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1917 DWLabel("label", StartID));
1919 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
1920 DWLabel("func_begin", SubprogramCount));
1923 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1924 DWLabel("label", EndID));
1926 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
1927 DWLabel("func_end", SubprogramCount));
1930 // Add the scope contents.
1931 ConstructScope(Scope, StartID, EndID, ScopeDie, Unit);
1932 ParentDie->AddChild(ScopeDie);
1937 /// ConstructRootScope - Construct the scope for the subprogram.
1939 void ConstructRootScope(DebugScope *RootScope) {
1940 // Exit if there is no root scope.
1941 if (!RootScope) return;
1943 // Get the subprogram debug information entry.
1944 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1946 // Get the compile unit context.
1947 CompileUnit *Unit = GetBaseCompileUnit();
1949 // Get the subprogram die.
1950 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1951 assert(SPDie && "Missing subprogram descriptor");
1953 // Add the function bounds.
1954 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
1955 DWLabel("func_begin", SubprogramCount));
1956 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
1957 DWLabel("func_end", SubprogramCount));
1958 MachineLocation Location(RI->getFrameRegister(*MF));
1959 AddAddress(SPDie, DW_AT_frame_base, Location);
1961 ConstructScope(RootScope, 0, 0, SPDie, Unit);
1964 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1965 /// tools to recognize the object file contains Dwarf information.
1966 void EmitInitial() {
1967 // Check to see if we already emitted intial headers.
1968 if (didInitial) return;
1971 // Print out .file directives to specify files for .loc directives.
1972 if (TAI->hasDotLocAndDotFile()) {
1973 const UniqueVector<SourceFileInfo> &SourceFiles = MMI->getSourceFiles();
1974 const UniqueVector<std::string> &Directories = MMI->getDirectories();
1975 for (unsigned i = 1, e = SourceFiles.size(); i <= e; ++i) {
1976 sys::Path FullPath(Directories[SourceFiles[i].getDirectoryID()]);
1977 bool AppendOk = FullPath.appendComponent(SourceFiles[i].getName());
1978 assert(AppendOk && "Could not append filename to directory!");
1979 Asm->EmitFile(i, FullPath.toString());
1984 // Dwarf sections base addresses.
1985 if (TAI->doesDwarfRequireFrameSection()) {
1986 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
1987 EmitLabel("section_debug_frame", 0);
1989 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
1990 EmitLabel("section_info", 0);
1991 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
1992 EmitLabel("section_abbrev", 0);
1993 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
1994 EmitLabel("section_aranges", 0);
1995 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
1996 EmitLabel("section_macinfo", 0);
1997 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
1998 EmitLabel("section_line", 0);
1999 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2000 EmitLabel("section_loc", 0);
2001 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2002 EmitLabel("section_pubnames", 0);
2003 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2004 EmitLabel("section_str", 0);
2005 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2006 EmitLabel("section_ranges", 0);
2008 Asm->SwitchToTextSection(TAI->getTextSection());
2009 EmitLabel("text_begin", 0);
2010 Asm->SwitchToDataSection(TAI->getDataSection());
2011 EmitLabel("data_begin", 0);
2014 /// EmitDIE - Recusively Emits a debug information entry.
2016 void EmitDIE(DIE *Die) {
2017 // Get the abbreviation for this DIE.
2018 unsigned AbbrevNumber = Die->getAbbrevNumber();
2019 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2023 // Emit the code (index) for the abbreviation.
2024 Asm->EmitULEB128Bytes(AbbrevNumber);
2025 Asm->EOL(std::string("Abbrev [" +
2026 utostr(AbbrevNumber) +
2027 "] 0x" + utohexstr(Die->getOffset()) +
2028 ":0x" + utohexstr(Die->getSize()) + " " +
2029 TagString(Abbrev->getTag())));
2031 std::vector<DIEValue *> &Values = Die->getValues();
2032 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2034 // Emit the DIE attribute values.
2035 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2036 unsigned Attr = AbbrevData[i].getAttribute();
2037 unsigned Form = AbbrevData[i].getForm();
2038 assert(Form && "Too many attributes for DIE (check abbreviation)");
2041 case DW_AT_sibling: {
2042 Asm->EmitInt32(Die->SiblingOffset());
2046 // Emit an attribute using the defined form.
2047 Values[i]->EmitValue(*this, Form);
2052 Asm->EOL(AttributeString(Attr));
2055 // Emit the DIE children if any.
2056 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2057 const std::vector<DIE *> &Children = Die->getChildren();
2059 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2060 EmitDIE(Children[j]);
2063 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2067 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2069 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2070 // Get the children.
2071 const std::vector<DIE *> &Children = Die->getChildren();
2073 // If not last sibling and has children then add sibling offset attribute.
2074 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2076 // Record the abbreviation.
2077 AssignAbbrevNumber(Die->getAbbrev());
2079 // Get the abbreviation for this DIE.
2080 unsigned AbbrevNumber = Die->getAbbrevNumber();
2081 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2084 Die->setOffset(Offset);
2086 // Start the size with the size of abbreviation code.
2087 Offset += Asm->SizeULEB128(AbbrevNumber);
2089 const std::vector<DIEValue *> &Values = Die->getValues();
2090 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
2092 // Size the DIE attribute values.
2093 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2094 // Size attribute value.
2095 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2098 // Size the DIE children if any.
2099 if (!Children.empty()) {
2100 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2101 "Children flag not set");
2103 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2104 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2107 // End of children marker.
2108 Offset += sizeof(int8_t);
2111 Die->setSize(Offset - Die->getOffset());
2115 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2117 void SizeAndOffsets() {
2118 // Process base compile unit.
2119 CompileUnit *Unit = GetBaseCompileUnit();
2120 // Compute size of compile unit header
2121 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2122 sizeof(int16_t) + // DWARF version number
2123 sizeof(int32_t) + // Offset Into Abbrev. Section
2124 sizeof(int8_t); // Pointer Size (in bytes)
2125 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2128 /// EmitDebugInfo - Emit the debug info section.
2130 void EmitDebugInfo() {
2131 // Start debug info section.
2132 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2134 CompileUnit *Unit = GetBaseCompileUnit();
2135 DIE *Die = Unit->getDie();
2136 // Emit the compile units header.
2137 EmitLabel("info_begin", Unit->getID());
2138 // Emit size of content not including length itself
2139 unsigned ContentSize = Die->getSize() +
2140 sizeof(int16_t) + // DWARF version number
2141 sizeof(int32_t) + // Offset Into Abbrev. Section
2142 sizeof(int8_t) + // Pointer Size (in bytes)
2143 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2145 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2146 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2147 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2148 Asm->EOL("Offset Into Abbrev. Section");
2149 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Address Size (in bytes)");
2152 // FIXME - extra padding for gdb bug.
2153 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2154 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2155 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2156 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2157 EmitLabel("info_end", Unit->getID());
2162 /// EmitAbbreviations - Emit the abbreviation section.
2164 void EmitAbbreviations() const {
2165 // Check to see if it is worth the effort.
2166 if (!Abbreviations.empty()) {
2167 // Start the debug abbrev section.
2168 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2170 EmitLabel("abbrev_begin", 0);
2172 // For each abbrevation.
2173 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2174 // Get abbreviation data
2175 const DIEAbbrev *Abbrev = Abbreviations[i];
2177 // Emit the abbrevations code (base 1 index.)
2178 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2179 Asm->EOL("Abbreviation Code");
2181 // Emit the abbreviations data.
2182 Abbrev->Emit(*this);
2187 // Mark end of abbreviations.
2188 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2190 EmitLabel("abbrev_end", 0);
2196 /// EmitDebugLines - Emit source line information.
2198 void EmitDebugLines() {
2199 // If there are no lines to emit (such as when we're using .loc directives
2200 // to emit .debug_line information) don't emit a .debug_line header.
2201 if (SectionSourceLines.empty())
2204 // Minimum line delta, thus ranging from -10..(255-10).
2205 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2206 // Maximum line delta, thus ranging from -10..(255-10).
2207 const int MaxLineDelta = 255 + MinLineDelta;
2209 // Start the dwarf line section.
2210 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2212 // Construct the section header.
2214 EmitDifference("line_end", 0, "line_begin", 0, true);
2215 Asm->EOL("Length of Source Line Info");
2216 EmitLabel("line_begin", 0);
2218 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2220 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2221 Asm->EOL("Prolog Length");
2222 EmitLabel("line_prolog_begin", 0);
2224 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2226 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2228 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2230 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2232 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2234 // Line number standard opcode encodings argument count
2235 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2236 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2237 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2238 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2239 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2240 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2241 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2242 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2243 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2245 const UniqueVector<std::string> &Directories = MMI->getDirectories();
2246 const UniqueVector<SourceFileInfo>
2247 &SourceFiles = MMI->getSourceFiles();
2249 // Emit directories.
2250 for (unsigned DirectoryID = 1, NDID = Directories.size();
2251 DirectoryID <= NDID; ++DirectoryID) {
2252 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2254 Asm->EmitInt8(0); Asm->EOL("End of directories");
2257 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2258 SourceID <= NSID; ++SourceID) {
2259 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2260 Asm->EmitString(SourceFile.getName());
2262 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2263 Asm->EOL("Directory #");
2264 Asm->EmitULEB128Bytes(0);
2265 Asm->EOL("Mod date");
2266 Asm->EmitULEB128Bytes(0);
2267 Asm->EOL("File size");
2269 Asm->EmitInt8(0); Asm->EOL("End of files");
2271 EmitLabel("line_prolog_end", 0);
2273 // A sequence for each text section.
2274 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2275 // Isolate current sections line info.
2276 const std::vector<SourceLineInfo> &LineInfos = SectionSourceLines[j];
2278 Asm->EOL(std::string("Section ") + SectionMap[j + 1]);
2280 // Dwarf assumes we start with first line of first source file.
2281 unsigned Source = 1;
2284 // Construct rows of the address, source, line, column matrix.
2285 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2286 const SourceLineInfo &LineInfo = LineInfos[i];
2287 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2288 if (!LabelID) continue;
2290 unsigned SourceID = LineInfo.getSourceID();
2291 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2292 unsigned DirectoryID = SourceFile.getDirectoryID();
2293 Asm->EOL(Directories[DirectoryID]
2294 + SourceFile.getName()
2296 + utostr_32(LineInfo.getLine()));
2298 // Define the line address.
2299 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2300 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2301 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2302 EmitReference("label", LabelID); Asm->EOL("Location label");
2304 // If change of source, then switch to the new source.
2305 if (Source != LineInfo.getSourceID()) {
2306 Source = LineInfo.getSourceID();
2307 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2308 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2311 // If change of line.
2312 if (Line != LineInfo.getLine()) {
2313 // Determine offset.
2314 int Offset = LineInfo.getLine() - Line;
2315 int Delta = Offset - MinLineDelta;
2318 Line = LineInfo.getLine();
2320 // If delta is small enough and in range...
2321 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2322 // ... then use fast opcode.
2323 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2325 // ... otherwise use long hand.
2326 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2327 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2328 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2331 // Copy the previous row (different address or source)
2332 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2336 // Define last address of section.
2337 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2338 Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
2339 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2340 EmitReference("section_end", j + 1); Asm->EOL("Section end label");
2342 // Mark end of matrix.
2343 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2344 Asm->EmitULEB128Bytes(1); Asm->EOL();
2345 Asm->EmitInt8(1); Asm->EOL();
2348 EmitLabel("line_end", 0);
2353 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2355 void EmitCommonDebugFrame() {
2356 if (!TAI->doesDwarfRequireFrameSection())
2360 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2361 TargetFrameInfo::StackGrowsUp ?
2362 TAI->getAddressSize() : -TAI->getAddressSize();
2364 // Start the dwarf frame section.
2365 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2367 EmitLabel("debug_frame_common", 0);
2368 EmitDifference("debug_frame_common_end", 0,
2369 "debug_frame_common_begin", 0, true);
2370 Asm->EOL("Length of Common Information Entry");
2372 EmitLabel("debug_frame_common_begin", 0);
2373 Asm->EmitInt32((int)DW_CIE_ID);
2374 Asm->EOL("CIE Identifier Tag");
2375 Asm->EmitInt8(DW_CIE_VERSION);
2376 Asm->EOL("CIE Version");
2377 Asm->EmitString("");
2378 Asm->EOL("CIE Augmentation");
2379 Asm->EmitULEB128Bytes(1);
2380 Asm->EOL("CIE Code Alignment Factor");
2381 Asm->EmitSLEB128Bytes(stackGrowth);
2382 Asm->EOL("CIE Data Alignment Factor");
2383 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2384 Asm->EOL("CIE RA Column");
2386 std::vector<MachineMove> Moves;
2387 RI->getInitialFrameState(Moves);
2389 EmitFrameMoves(NULL, 0, Moves);
2391 Asm->EmitAlignment(2);
2392 EmitLabel("debug_frame_common_end", 0);
2397 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2399 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2400 if (!TAI->doesDwarfRequireFrameSection())
2403 // Start the dwarf frame section.
2404 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2406 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2407 "debug_frame_begin", DebugFrameInfo.Number, true);
2408 Asm->EOL("Length of Frame Information Entry");
2410 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2412 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2414 Asm->EOL("FDE CIE offset");
2416 EmitReference("func_begin", DebugFrameInfo.Number);
2417 Asm->EOL("FDE initial location");
2418 EmitDifference("func_end", DebugFrameInfo.Number,
2419 "func_begin", DebugFrameInfo.Number);
2420 Asm->EOL("FDE address range");
2422 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves);
2424 Asm->EmitAlignment(2);
2425 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2430 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2432 void EmitDebugPubNames() {
2433 // Start the dwarf pubnames section.
2434 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2436 CompileUnit *Unit = GetBaseCompileUnit();
2438 EmitDifference("pubnames_end", Unit->getID(),
2439 "pubnames_begin", Unit->getID(), true);
2440 Asm->EOL("Length of Public Names Info");
2442 EmitLabel("pubnames_begin", Unit->getID());
2444 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2446 EmitSectionOffset("info_begin", "section_info",
2447 Unit->getID(), 0, true, false);
2448 Asm->EOL("Offset of Compilation Unit Info");
2450 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
2451 Asm->EOL("Compilation Unit Length");
2453 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2455 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2458 const std::string &Name = GI->first;
2459 DIE * Entity = GI->second;
2461 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2462 Asm->EmitString(Name); Asm->EOL("External Name");
2465 Asm->EmitInt32(0); Asm->EOL("End Mark");
2466 EmitLabel("pubnames_end", Unit->getID());
2471 /// EmitDebugStr - Emit visible names into a debug str section.
2473 void EmitDebugStr() {
2474 // Check to see if it is worth the effort.
2475 if (!StringPool.empty()) {
2476 // Start the dwarf str section.
2477 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2479 // For each of strings in the string pool.
2480 for (unsigned StringID = 1, N = StringPool.size();
2481 StringID <= N; ++StringID) {
2482 // Emit a label for reference from debug information entries.
2483 EmitLabel("string", StringID);
2484 // Emit the string itself.
2485 const std::string &String = StringPool[StringID];
2486 Asm->EmitString(String); Asm->EOL();
2493 /// EmitDebugLoc - Emit visible names into a debug loc section.
2495 void EmitDebugLoc() {
2496 // Start the dwarf loc section.
2497 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2502 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2504 void EmitDebugARanges() {
2505 // Start the dwarf aranges section.
2506 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2510 CompileUnit *Unit = GetBaseCompileUnit();
2512 // Don't include size of length
2513 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2515 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2517 EmitReference("info_begin", Unit->getID());
2518 Asm->EOL("Offset of Compilation Unit Info");
2520 Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Size of Address");
2522 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2524 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2525 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2528 EmitReference("text_begin", 0); Asm->EOL("Address");
2529 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2531 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2532 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2538 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2540 void EmitDebugRanges() {
2541 // Start the dwarf ranges section.
2542 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2547 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2549 void EmitDebugMacInfo() {
2550 // Start the dwarf macinfo section.
2551 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2556 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2558 void ConstructCompileUnitDIEs() {
2559 const UniqueVector<CompileUnitDesc *> CUW = MMI->getCompileUnits();
2561 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2562 unsigned ID = MMI->RecordSource(CUW[i]);
2563 CompileUnit *Unit = NewCompileUnit(CUW[i], ID);
2564 CompileUnits.push_back(Unit);
2568 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
2569 /// global variables.
2570 void ConstructGlobalDIEs() {
2571 std::vector<GlobalVariableDesc *> GlobalVariables =
2572 MMI->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2574 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2575 GlobalVariableDesc *GVD = GlobalVariables[i];
2576 NewGlobalVariable(GVD);
2580 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2582 void ConstructSubprogramDIEs() {
2583 std::vector<SubprogramDesc *> Subprograms =
2584 MMI->getAnchoredDescriptors<SubprogramDesc>(*M);
2586 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2587 SubprogramDesc *SPD = Subprograms[i];
2593 //===--------------------------------------------------------------------===//
2594 // Main entry points.
2596 DwarfDebug(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2597 : Dwarf(OS, A, T, "dbg")
2599 , AbbreviationsSet(InitAbbreviationsSetSize)
2601 , ValuesSet(InitValuesSetSize)
2606 , SectionSourceLines()
2611 virtual ~DwarfDebug() {
2612 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i)
2613 delete CompileUnits[i];
2614 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2618 /// SetModuleInfo - Set machine module information when it's known that pass
2619 /// manager has created it. Set by the target AsmPrinter.
2620 void SetModuleInfo(MachineModuleInfo *mmi) {
2621 // Make sure initial declarations are made.
2622 if (!MMI && mmi->hasDebugInfo()) {
2626 // Emit initial sections
2629 // Create all the compile unit DIEs.
2630 ConstructCompileUnitDIEs();
2632 // Create DIEs for each of the externally visible global variables.
2633 ConstructGlobalDIEs();
2635 // Create DIEs for each of the externally visible subprograms.
2636 ConstructSubprogramDIEs();
2638 // Prime section data.
2639 SectionMap.insert(TAI->getTextSection());
2643 /// BeginModule - Emit all Dwarf sections that should come prior to the
2645 void BeginModule(Module *M) {
2648 if (!ShouldEmitDwarf()) return;
2651 /// EndModule - Emit all Dwarf sections that should come after the content.
2654 if (!ShouldEmitDwarf()) return;
2656 // Standard sections final addresses.
2657 Asm->SwitchToTextSection(TAI->getTextSection());
2658 EmitLabel("text_end", 0);
2659 Asm->SwitchToDataSection(TAI->getDataSection());
2660 EmitLabel("data_end", 0);
2662 // End text sections.
2663 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2664 Asm->SwitchToTextSection(SectionMap[i].c_str());
2665 EmitLabel("section_end", i);
2668 // Emit common frame information.
2669 EmitCommonDebugFrame();
2671 // Emit function debug frame information
2672 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2673 E = DebugFrames.end(); I != E; ++I)
2674 EmitFunctionDebugFrame(*I);
2676 // Compute DIE offsets and sizes.
2679 // Emit all the DIEs into a debug info section
2682 // Corresponding abbreviations into a abbrev section.
2683 EmitAbbreviations();
2685 // Emit source line correspondence into a debug line section.
2688 // Emit info into a debug pubnames section.
2689 EmitDebugPubNames();
2691 // Emit info into a debug str section.
2694 // Emit info into a debug loc section.
2697 // Emit info into a debug aranges section.
2700 // Emit info into a debug ranges section.
2703 // Emit info into a debug macinfo section.
2707 /// BeginFunction - Gather pre-function debug information. Assumes being
2708 /// emitted immediately after the function entry point.
2709 void BeginFunction(MachineFunction *MF) {
2712 if (!ShouldEmitDwarf()) return;
2714 // Begin accumulating function debug information.
2715 MMI->BeginFunction(MF);
2717 // Assumes in correct section after the entry point.
2718 EmitLabel("func_begin", ++SubprogramCount);
2721 /// EndFunction - Gather and emit post-function debug information.
2723 void EndFunction() {
2724 if (!ShouldEmitDwarf()) return;
2726 // Define end label for subprogram.
2727 EmitLabel("func_end", SubprogramCount);
2729 // Get function line info.
2730 const std::vector<SourceLineInfo> &LineInfos = MMI->getSourceLines();
2732 if (!LineInfos.empty()) {
2733 // Get section line info.
2734 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2735 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2736 std::vector<SourceLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
2737 // Append the function info to section info.
2738 SectionLineInfos.insert(SectionLineInfos.end(),
2739 LineInfos.begin(), LineInfos.end());
2742 // Construct scopes for subprogram.
2743 ConstructRootScope(MMI->getRootScope());
2745 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
2746 MMI->getFrameMoves()));
2750 //===----------------------------------------------------------------------===//
2751 /// DwarfException - Emits Dwarf exception handling directives.
2753 class DwarfException : public Dwarf {
2756 struct FunctionEHFrameInfo {
2759 unsigned PersonalityIndex;
2761 bool hasLandingPads;
2762 std::vector<MachineMove> Moves;
2764 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
2766 const std::vector<MachineMove> &M):
2767 FnName(FN), Number(Num), PersonalityIndex(P),
2768 hasCalls(hC), hasLandingPads(hL), Moves(M) { }
2771 std::vector<FunctionEHFrameInfo> EHFrames;
2773 /// shouldEmit - Flag to indicate if debug information should be emitted.
2777 /// EmitCommonEHFrame - Emit the common eh unwind frame.
2779 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
2780 // Size and sign of stack growth.
2782 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2783 TargetFrameInfo::StackGrowsUp ?
2784 TAI->getAddressSize() : -TAI->getAddressSize();
2786 // Begin eh frame section.
2787 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2788 O << "EH_frame" << Index << ":\n";
2789 EmitLabel("section_eh_frame", Index);
2791 // Define base labels.
2792 EmitLabel("eh_frame_common", Index);
2794 // Define the eh frame length.
2795 EmitDifference("eh_frame_common_end", Index,
2796 "eh_frame_common_begin", Index, true);
2797 Asm->EOL("Length of Common Information Entry");
2800 EmitLabel("eh_frame_common_begin", Index);
2801 Asm->EmitInt32((int)0);
2802 Asm->EOL("CIE Identifier Tag");
2803 Asm->EmitInt8(DW_CIE_VERSION);
2804 Asm->EOL("CIE Version");
2806 // The personality presence indicates that language specific information
2807 // will show up in the eh frame.
2808 Asm->EmitString(Personality ? "zPLR" : "zR");
2809 Asm->EOL("CIE Augmentation");
2811 // Round out reader.
2812 Asm->EmitULEB128Bytes(1);
2813 Asm->EOL("CIE Code Alignment Factor");
2814 Asm->EmitSLEB128Bytes(stackGrowth);
2815 Asm->EOL("CIE Data Alignment Factor");
2816 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
2817 Asm->EOL("CIE RA Column");
2819 // If there is a personality, we need to indicate the functions location.
2821 Asm->EmitULEB128Bytes(7);
2822 Asm->EOL("Augmentation Size");
2824 if (TAI->getNeedsIndirectEncoding())
2825 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
2827 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
2829 Asm->EOL("Personality (pcrel sdata4 indirect)");
2831 PrintRelDirective();
2832 O << TAI->getPersonalityPrefix();
2833 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
2834 O << TAI->getPersonalitySuffix();
2835 O << "-" << TAI->getPCSymbol();
2836 Asm->EOL("Personality");
2838 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2839 Asm->EOL("LSDA Encoding (pcrel)");
2840 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2841 Asm->EOL("FDE Encoding (pcrel)");
2843 Asm->EmitULEB128Bytes(1);
2844 Asm->EOL("Augmentation Size");
2845 Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
2846 Asm->EOL("FDE Encoding (pcrel)");
2849 // Indicate locations of general callee saved registers in frame.
2850 std::vector<MachineMove> Moves;
2851 RI->getInitialFrameState(Moves);
2852 EmitFrameMoves(NULL, 0, Moves);
2854 Asm->EmitAlignment(2);
2855 EmitLabel("eh_frame_common_end", Index);
2860 /// EmitEHFrame - Emit function exception frame information.
2862 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
2863 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
2865 // Externally visible entry into the functions eh frame info.
2866 if (const char *GlobalDirective = TAI->getGlobalDirective())
2867 O << GlobalDirective << EHFrameInfo.FnName << "\n";
2869 // If there are no calls then you can't unwind.
2870 if (!EHFrameInfo.hasCalls) {
2871 O << EHFrameInfo.FnName << " = 0\n";
2873 O << EHFrameInfo.FnName << ":\n";
2876 EmitDifference("eh_frame_end", EHFrameInfo.Number,
2877 "eh_frame_begin", EHFrameInfo.Number, true);
2878 Asm->EOL("Length of Frame Information Entry");
2880 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
2882 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
2883 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
2885 Asm->EOL("FDE CIE offset");
2887 EmitReference("eh_func_begin", EHFrameInfo.Number, true);
2888 Asm->EOL("FDE initial location");
2889 EmitDifference("eh_func_end", EHFrameInfo.Number,
2890 "eh_func_begin", EHFrameInfo.Number);
2891 Asm->EOL("FDE address range");
2893 // If there is a personality and landing pads then point to the language
2894 // specific data area in the exception table.
2895 if (EHFrameInfo.PersonalityIndex) {
2896 Asm->EmitULEB128Bytes(4);
2897 Asm->EOL("Augmentation size");
2899 if (EHFrameInfo.hasLandingPads) {
2900 EmitReference("exception", EHFrameInfo.Number, true);
2901 } else if(TAI->getAddressSize() == 8) {
2902 Asm->EmitInt64((int)0);
2904 Asm->EmitInt32((int)0);
2906 Asm->EOL("Language Specific Data Area");
2908 Asm->EmitULEB128Bytes(0);
2909 Asm->EOL("Augmentation size");
2912 // Indicate locations of function specific callee saved registers in
2914 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves);
2916 Asm->EmitAlignment(2);
2917 EmitLabel("eh_frame_end", EHFrameInfo.Number);
2920 if (const char *UsedDirective = TAI->getUsedDirective())
2921 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
2924 /// EmitExceptionTable - Emit landing pads and actions.
2926 /// The general organization of the table is complex, but the basic concepts
2927 /// are easy. First there is a header which describes the location and
2928 /// organization of the three components that follow.
2929 /// 1. The landing pad site information describes the range of code covered
2930 /// by the try. In our case it's an accumulation of the ranges covered
2931 /// by the invokes in the try. There is also a reference to the landing
2932 /// pad that handles the exception once processed. Finally an index into
2933 /// the actions table.
2934 /// 2. The action table, in our case, is composed of pairs of type ids
2935 /// and next action offset. Starting with the action index from the
2936 /// landing pad site, each type Id is checked for a match to the current
2937 /// exception. If it matches then the exception and type id are passed
2938 /// on to the landing pad. Otherwise the next action is looked up. This
2939 /// chain is terminated with a next action of zero. If no type id is
2940 /// found the the frame is unwound and handling continues.
2941 /// 3. Type id table contains references to all the C++ typeinfo for all
2942 /// catches in the function. This tables is reversed indexed base 1.
2944 /// SharedTypeIds - How many leading type ids two landing pads have in common.
2945 static unsigned SharedTypeIds(const LandingPadInfo *L,
2946 const LandingPadInfo *R) {
2947 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
2948 unsigned LSize = LIds.size(), RSize = RIds.size();
2949 unsigned MinSize = LSize < RSize ? LSize : RSize;
2952 for (; Count != MinSize; ++Count)
2953 if (LIds[Count] != RIds[Count])
2959 /// PadLT - Order landing pads lexicographically by type id.
2960 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
2961 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
2962 unsigned LSize = LIds.size(), RSize = RIds.size();
2963 unsigned MinSize = LSize < RSize ? LSize : RSize;
2965 for (unsigned i = 0; i != MinSize; ++i)
2966 if (LIds[i] != RIds[i])
2967 return LIds[i] < RIds[i];
2969 return LSize < RSize;
2973 static inline unsigned getEmptyKey() { return -1U; }
2974 static inline unsigned getTombstoneKey() { return -2U; }
2975 static unsigned getHashValue(const unsigned &Key) { return Key; }
2976 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
2977 static bool isPod() { return true; }
2980 /// ActionEntry - Structure describing an entry in the actions table.
2981 struct ActionEntry {
2982 int ValueForTypeID; // The value to write - may not be equal to the type id.
2984 struct ActionEntry *Previous;
2987 /// PadRange - Structure holding a try-range and the associated landing pad.
2989 // The index of the landing pad.
2991 // The index of the begin and end labels in the landing pad's label lists.
2992 unsigned RangeIndex;
2995 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
2997 /// CallSiteEntry - Structure describing an entry in the call-site table.
2998 struct CallSiteEntry {
2999 unsigned BeginLabel; // zero indicates the start of the function.
3000 unsigned EndLabel; // zero indicates the end of the function.
3001 unsigned PadLabel; // zero indicates that there is no landing pad.
3005 void EmitExceptionTable() {
3006 // Map all labels and get rid of any dead landing pads.
3007 MMI->TidyLandingPads();
3009 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3010 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3011 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3012 if (PadInfos.empty()) return;
3014 // Sort the landing pads in order of their type ids. This is used to fold
3015 // duplicate actions.
3016 SmallVector<const LandingPadInfo *, 64> LandingPads;
3017 LandingPads.reserve(PadInfos.size());
3018 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3019 LandingPads.push_back(&PadInfos[i]);
3020 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3022 // Negative type ids index into FilterIds, positive type ids index into
3023 // TypeInfos. The value written for a positive type id is just the type
3024 // id itself. For a negative type id, however, the value written is the
3025 // (negative) byte offset of the corresponding FilterIds entry. The byte
3026 // offset is usually equal to the type id, because the FilterIds entries
3027 // are written using a variable width encoding which outputs one byte per
3028 // entry as long as the value written is not too large, but can differ.
3029 // This kind of complication does not occur for positive type ids because
3030 // type infos are output using a fixed width encoding.
3031 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3032 SmallVector<int, 16> FilterOffsets;
3033 FilterOffsets.reserve(FilterIds.size());
3035 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3036 E = FilterIds.end(); I != E; ++I) {
3037 FilterOffsets.push_back(Offset);
3038 Offset -= Asm->SizeULEB128(*I);
3041 // Compute the actions table and gather the first action index for each
3042 // landing pad site.
3043 SmallVector<ActionEntry, 32> Actions;
3044 SmallVector<unsigned, 64> FirstActions;
3045 FirstActions.reserve(LandingPads.size());
3047 int FirstAction = 0;
3048 unsigned SizeActions = 0;
3049 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3050 const LandingPadInfo *LP = LandingPads[i];
3051 const std::vector<int> &TypeIds = LP->TypeIds;
3052 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3053 unsigned SizeSiteActions = 0;
3055 if (NumShared < TypeIds.size()) {
3056 unsigned SizeAction = 0;
3057 ActionEntry *PrevAction = 0;
3060 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3061 assert(Actions.size());
3062 PrevAction = &Actions.back();
3063 SizeAction = Asm->SizeSLEB128(PrevAction->NextAction) +
3064 Asm->SizeSLEB128(PrevAction->ValueForTypeID);
3065 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
3066 SizeAction -= Asm->SizeSLEB128(PrevAction->ValueForTypeID);
3067 SizeAction += -PrevAction->NextAction;
3068 PrevAction = PrevAction->Previous;
3072 // Compute the actions.
3073 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3074 int TypeID = TypeIds[I];
3075 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3076 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
3077 unsigned SizeTypeID = Asm->SizeSLEB128(ValueForTypeID);
3079 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
3080 SizeAction = SizeTypeID + Asm->SizeSLEB128(NextAction);
3081 SizeSiteActions += SizeAction;
3083 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3084 Actions.push_back(Action);
3086 PrevAction = &Actions.back();
3089 // Record the first action of the landing pad site.
3090 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3091 } // else identical - re-use previous FirstAction
3093 FirstActions.push_back(FirstAction);
3095 // Compute this sites contribution to size.
3096 SizeActions += SizeSiteActions;
3099 // Compute the call-site table. Entries must be ordered by address.
3100 SmallVector<CallSiteEntry, 64> CallSites;
3102 RangeMapType PadMap;
3103 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3104 const LandingPadInfo *LandingPad = LandingPads[i];
3105 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
3106 unsigned BeginLabel = LandingPad->BeginLabels[j];
3107 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3108 PadRange P = { i, j };
3109 PadMap[BeginLabel] = P;
3113 bool MayThrow = false;
3114 unsigned LastLabel = 0;
3115 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
3116 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3118 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3120 if (MI->getOpcode() != TargetInstrInfo::LABEL) {
3121 MayThrow |= TII->isCall(MI->getOpcode());
3125 unsigned BeginLabel = MI->getOperand(0).getImmedValue();
3126 assert(BeginLabel && "Invalid label!");
3128 if (BeginLabel == LastLabel)
3131 RangeMapType::iterator L = PadMap.find(BeginLabel);
3133 if (L == PadMap.end())
3136 PadRange P = L->second;
3137 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3139 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3140 "Inconsistent landing pad map!");
3142 // If some instruction between the previous try-range and this one may
3143 // throw, create a call-site entry with no landing pad for the region
3144 // between the try-ranges.
3146 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3147 CallSites.push_back(Site);
3150 LastLabel = LandingPad->EndLabels[P.RangeIndex];
3151 CallSiteEntry Site = {BeginLabel, LastLabel,
3152 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
3154 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
3155 "Invalid landing pad!");
3157 // Try to merge with the previous call-site.
3158 if (CallSites.size()) {
3159 CallSiteEntry &Prev = CallSites[CallSites.size()-1];
3160 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3161 // Extend the range of the previous entry.
3162 Prev.EndLabel = Site.EndLabel;
3167 // Otherwise, create a new call-site.
3168 CallSites.push_back(Site);
3171 // If some instruction between the previous try-range and the end of the
3172 // function may throw, create a call-site entry with no landing pad for the
3173 // region following the try-range.
3175 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3176 CallSites.push_back(Site);
3180 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
3181 sizeof(int32_t) + // Site length.
3182 sizeof(int32_t)); // Landing pad.
3183 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
3184 SizeSites += Asm->SizeULEB128(CallSites[i].Action);
3186 unsigned SizeTypes = TypeInfos.size() * TAI->getAddressSize();
3188 unsigned TypeOffset = sizeof(int8_t) + // Call site format
3189 Asm->SizeULEB128(SizeSites) + // Call-site table length
3190 SizeSites + SizeActions + SizeTypes;
3192 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3193 sizeof(int8_t) + // TType format
3194 Asm->SizeULEB128(TypeOffset) + // TType base offset
3197 unsigned SizeAlign = (4 - TotalSize) & 3;
3199 // Begin the exception table.
3200 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
3201 O << "GCC_except_table" << SubprogramCount << ":\n";
3202 Asm->EmitAlignment(2);
3203 for (unsigned i = 0; i != SizeAlign; ++i) {
3205 Asm->EOL("Padding");
3207 EmitLabel("exception", SubprogramCount);
3210 Asm->EmitInt8(DW_EH_PE_omit);
3211 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3212 Asm->EmitInt8(DW_EH_PE_absptr);
3213 Asm->EOL("TType format (DW_EH_PE_absptr)");
3214 Asm->EmitULEB128Bytes(TypeOffset);
3215 Asm->EOL("TType base offset");
3216 Asm->EmitInt8(DW_EH_PE_udata4);
3217 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3218 Asm->EmitULEB128Bytes(SizeSites);
3219 Asm->EOL("Call-site table length");
3221 // Emit the landing pad site information.
3222 for (unsigned i = 0; i < CallSites.size(); ++i) {
3223 CallSiteEntry &S = CallSites[i];
3224 const char *BeginTag;
3225 unsigned BeginNumber;
3227 if (!S.BeginLabel) {
3228 BeginTag = "eh_func_begin";
3229 BeginNumber = SubprogramCount;
3232 BeginNumber = S.BeginLabel;
3235 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
3237 Asm->EOL("Region start");
3240 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber);
3242 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber);
3244 Asm->EOL("Region length");
3247 if (TAI->getAddressSize() == sizeof(int32_t))
3252 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
3255 Asm->EOL("Landing pad");
3257 Asm->EmitULEB128Bytes(S.Action);
3261 // Emit the actions.
3262 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3263 ActionEntry &Action = Actions[I];
3265 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3266 Asm->EOL("TypeInfo index");
3267 Asm->EmitSLEB128Bytes(Action.NextAction);
3268 Asm->EOL("Next action");
3271 // Emit the type ids.
3272 for (unsigned M = TypeInfos.size(); M; --M) {
3273 GlobalVariable *GV = TypeInfos[M - 1];
3275 PrintRelDirective();
3278 O << Asm->getGlobalLinkName(GV);
3282 Asm->EOL("TypeInfo");
3285 // Emit the filter typeids.
3286 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3287 unsigned TypeID = FilterIds[j];
3288 Asm->EmitULEB128Bytes(TypeID);
3289 Asm->EOL("Filter TypeInfo index");
3292 Asm->EmitAlignment(2);
3296 //===--------------------------------------------------------------------===//
3297 // Main entry points.
3299 DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
3300 : Dwarf(OS, A, T, "eh")
3304 virtual ~DwarfException() {}
3306 /// SetModuleInfo - Set machine module information when it's known that pass
3307 /// manager has created it. Set by the target AsmPrinter.
3308 void SetModuleInfo(MachineModuleInfo *mmi) {
3312 /// BeginModule - Emit all exception information that should come prior to the
3314 void BeginModule(Module *M) {
3318 /// EndModule - Emit all exception information that should come after the
3321 if (!shouldEmit) return;
3323 const std::vector<Function *> Personalities = MMI->getPersonalities();
3324 for (unsigned i =0; i < Personalities.size(); ++i)
3325 EmitCommonEHFrame(Personalities[i], i);
3327 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3328 E = EHFrames.end(); I != E; ++I)
3332 /// BeginFunction - Gather pre-function exception information. Assumes being
3333 /// emitted immediately after the function entry point.
3334 void BeginFunction(MachineFunction *MF) {
3338 ExceptionHandling &&
3339 TAI->doesSupportExceptionHandling()) {
3341 // Assumes in correct section after the entry point.
3342 EmitLabel("eh_func_begin", ++SubprogramCount);
3346 /// EndFunction - Gather and emit post-function exception information.
3348 void EndFunction() {
3349 if (!shouldEmit) return;
3351 EmitLabel("eh_func_end", SubprogramCount);
3352 EmitExceptionTable();
3354 // Save EH frame information
3356 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
3358 MMI->getPersonalityIndex(),
3359 MF->getFrameInfo()->hasCalls(),
3360 !MMI->getLandingPads().empty(),
3361 MMI->getFrameMoves()));
3365 } // End of namespace llvm
3367 //===----------------------------------------------------------------------===//
3369 /// Emit - Print the abbreviation using the specified Dwarf writer.
3371 void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3372 // Emit its Dwarf tag type.
3373 DD.getAsm()->EmitULEB128Bytes(Tag);
3374 DD.getAsm()->EOL(TagString(Tag));
3376 // Emit whether it has children DIEs.
3377 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3378 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
3380 // For each attribute description.
3381 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3382 const DIEAbbrevData &AttrData = Data[i];
3384 // Emit attribute type.
3385 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3386 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
3389 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3390 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3393 // Mark end of abbreviation.
3394 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3395 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3399 void DIEAbbrev::print(std::ostream &O) {
3400 O << "Abbreviation @"
3401 << std::hex << (intptr_t)this << std::dec
3405 << ChildrenString(ChildrenFlag)
3408 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3410 << AttributeString(Data[i].getAttribute())
3412 << FormEncodingString(Data[i].getForm())
3416 void DIEAbbrev::dump() { print(cerr); }
3419 //===----------------------------------------------------------------------===//
3422 void DIEValue::dump() {
3427 //===----------------------------------------------------------------------===//
3429 /// EmitValue - Emit integer of appropriate size.
3431 void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3433 case DW_FORM_flag: // Fall thru
3434 case DW_FORM_ref1: // Fall thru
3435 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3436 case DW_FORM_ref2: // Fall thru
3437 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3438 case DW_FORM_ref4: // Fall thru
3439 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
3440 case DW_FORM_ref8: // Fall thru
3441 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3442 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3443 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
3444 default: assert(0 && "DIE Value form not supported yet"); break;
3448 /// SizeOf - Determine size of integer value in bytes.
3450 unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3452 case DW_FORM_flag: // Fall thru
3453 case DW_FORM_ref1: // Fall thru
3454 case DW_FORM_data1: return sizeof(int8_t);
3455 case DW_FORM_ref2: // Fall thru
3456 case DW_FORM_data2: return sizeof(int16_t);
3457 case DW_FORM_ref4: // Fall thru
3458 case DW_FORM_data4: return sizeof(int32_t);
3459 case DW_FORM_ref8: // Fall thru
3460 case DW_FORM_data8: return sizeof(int64_t);
3461 case DW_FORM_udata: return DD.getAsm()->SizeULEB128(Integer);
3462 case DW_FORM_sdata: return DD.getAsm()->SizeSLEB128(Integer);
3463 default: assert(0 && "DIE Value form not supported yet"); break;
3468 //===----------------------------------------------------------------------===//
3470 /// EmitValue - Emit string value.
3472 void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
3473 DD.getAsm()->EmitString(String);
3476 //===----------------------------------------------------------------------===//
3478 /// EmitValue - Emit label value.
3480 void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
3481 DD.EmitReference(Label);
3484 /// SizeOf - Determine size of label value in bytes.
3486 unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3487 return DD.getTargetAsmInfo()->getAddressSize();
3490 //===----------------------------------------------------------------------===//
3492 /// EmitValue - Emit label value.
3494 void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
3495 DD.EmitReference(Label);
3498 /// SizeOf - Determine size of label value in bytes.
3500 unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3501 return DD.getTargetAsmInfo()->getAddressSize();
3504 //===----------------------------------------------------------------------===//
3506 /// EmitValue - Emit delta value.
3508 void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
3509 bool IsSmall = Form == DW_FORM_data4;
3510 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
3513 /// SizeOf - Determine size of delta value in bytes.
3515 unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3516 if (Form == DW_FORM_data4) return 4;
3517 return DD.getTargetAsmInfo()->getAddressSize();
3520 //===----------------------------------------------------------------------===//
3522 /// EmitValue - Emit debug information entry offset.
3524 void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
3525 DD.getAsm()->EmitInt32(Entry->getOffset());
3528 //===----------------------------------------------------------------------===//
3530 /// ComputeSize - calculate the size of the block.
3532 unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
3534 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3536 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3537 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
3543 /// EmitValue - Emit block data.
3545 void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
3547 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
3548 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
3549 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
3550 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
3551 default: assert(0 && "Improper form for block"); break;
3554 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
3556 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
3558 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
3562 /// SizeOf - Determine size of block data in bytes.
3564 unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3566 case DW_FORM_block1: return Size + sizeof(int8_t);
3567 case DW_FORM_block2: return Size + sizeof(int16_t);
3568 case DW_FORM_block4: return Size + sizeof(int32_t);
3569 case DW_FORM_block: return Size + DD.getAsm()->SizeULEB128(Size);
3570 default: assert(0 && "Improper form for block"); break;
3575 //===----------------------------------------------------------------------===//
3576 /// DIE Implementation
3579 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3583 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
3585 void DIE::AddSiblingOffset() {
3586 DIEInteger *DI = new DIEInteger(0);
3587 Values.insert(Values.begin(), DI);
3588 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
3591 /// Profile - Used to gather unique data for the value folding set.
3593 void DIE::Profile(FoldingSetNodeID &ID) {
3596 for (unsigned i = 0, N = Children.size(); i < N; ++i)
3597 ID.AddPointer(Children[i]);
3599 for (unsigned j = 0, M = Values.size(); j < M; ++j)
3600 ID.AddPointer(Values[j]);
3604 void DIE::print(std::ostream &O, unsigned IncIndent) {
3605 static unsigned IndentCount = 0;
3606 IndentCount += IncIndent;
3607 const std::string Indent(IndentCount, ' ');
3608 bool isBlock = Abbrev.getTag() == 0;
3613 << "0x" << std::hex << (intptr_t)this << std::dec
3614 << ", Offset: " << Offset
3615 << ", Size: " << Size
3619 << TagString(Abbrev.getTag())
3621 << ChildrenString(Abbrev.getChildrenFlag());
3623 O << "Size: " << Size;
3627 const std::vector<DIEAbbrevData> &Data = Abbrev.getData();
3630 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3633 O << AttributeString(Data[i].getAttribute());
3635 O << "Blk[" << i << "]";
3638 << FormEncodingString(Data[i].getForm())
3640 Values[i]->print(O);
3645 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
3646 Children[j]->print(O, 4);
3649 if (!isBlock) O << "\n";
3650 IndentCount -= IncIndent;
3658 //===----------------------------------------------------------------------===//
3659 /// DwarfWriter Implementation
3662 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A,
3663 const TargetAsmInfo *T) {
3664 DE = new DwarfException(OS, A, T);
3665 DD = new DwarfDebug(OS, A, T);
3668 DwarfWriter::~DwarfWriter() {
3673 /// SetModuleInfo - Set machine module info when it's known that pass manager
3674 /// has created it. Set by the target AsmPrinter.
3675 void DwarfWriter::SetModuleInfo(MachineModuleInfo *MMI) {
3676 DD->SetModuleInfo(MMI);
3677 DE->SetModuleInfo(MMI);
3680 /// BeginModule - Emit all Dwarf sections that should come prior to the
3682 void DwarfWriter::BeginModule(Module *M) {
3687 /// EndModule - Emit all Dwarf sections that should come after the content.
3689 void DwarfWriter::EndModule() {
3694 /// BeginFunction - Gather pre-function debug information. Assumes being
3695 /// emitted immediately after the function entry point.
3696 void DwarfWriter::BeginFunction(MachineFunction *MF) {
3697 DE->BeginFunction(MF);
3698 DD->BeginFunction(MF);
3701 /// EndFunction - Gather and emit post-function debug information.
3703 void DwarfWriter::EndFunction() {
3707 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI()) {
3708 // Clear function debug information.