1 //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf 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/DerivedTypes.h"
22 #include "llvm/Constants.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/Analysis/DebugInfo.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/DataTypes.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/System/Path.h"
35 #include "llvm/Target/TargetAsmInfo.h"
36 #include "llvm/Target/TargetRegisterInfo.h"
37 #include "llvm/Target/TargetData.h"
38 #include "llvm/Target/TargetFrameInfo.h"
39 #include "llvm/Target/TargetInstrInfo.h"
40 #include "llvm/Target/TargetMachine.h"
41 #include "llvm/Target/TargetOptions.h"
45 using namespace llvm::dwarf;
47 static RegisterPass<DwarfWriter>
48 X("dwarfwriter", "DWARF Information Writer");
49 char DwarfWriter::ID = 0;
53 //===----------------------------------------------------------------------===//
55 /// Configuration values for initial hash set sizes (log2).
57 static const unsigned InitDiesSetSize = 9; // 512
58 static const unsigned InitAbbreviationsSetSize = 9; // 512
59 static const unsigned InitValuesSetSize = 9; // 512
61 //===----------------------------------------------------------------------===//
62 /// Forward declarations.
67 //===----------------------------------------------------------------------===//
70 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
71 /// specified value in their initializer somewhere.
73 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
74 // Scan though value users.
75 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
77 // If the user is a GlobalVariable then add to result.
79 } else if (Constant *C = dyn_cast<Constant>(*I)) {
80 // If the user is a constant variable then scan its users.
81 getGlobalVariablesUsing(C, Result);
86 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
87 /// named GlobalVariable.
89 getGlobalVariablesUsing(Module &M, const std::string &RootName,
90 std::vector<GlobalVariable*> &Result) {
91 std::vector<const Type*> FieldTypes;
92 FieldTypes.push_back(Type::Int32Ty);
93 FieldTypes.push_back(Type::Int32Ty);
95 // Get the GlobalVariable root.
96 GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
97 StructType::get(FieldTypes));
99 // If present and linkonce then scan for users.
100 if (UseRoot && UseRoot->hasLinkOnceLinkage())
101 getGlobalVariablesUsing(UseRoot, Result);
104 /// getGlobalVariable - Return either a direct or cast Global value.
106 static GlobalVariable *getGlobalVariable(Value *V) {
107 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
109 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
110 if (CE->getOpcode() == Instruction::BitCast) {
111 return dyn_cast<GlobalVariable>(CE->getOperand(0));
112 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
113 for (unsigned int i=1; i<CE->getNumOperands(); i++) {
114 if (!CE->getOperand(i)->isNullValue())
117 return dyn_cast<GlobalVariable>(CE->getOperand(0));
123 //===----------------------------------------------------------------------===//
124 /// DWLabel - Labels are used to track locations in the assembler file.
125 /// Labels appear in the form @verbatim <prefix><Tag><Number> @endverbatim,
126 /// where the tag is a category of label (Ex. location) and number is a value
127 /// unique in that category.
130 /// Tag - Label category tag. Should always be a staticly declared C string.
134 /// Number - Value to make label unique.
138 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
140 void Profile(FoldingSetNodeID &ID) const {
141 ID.AddString(std::string(Tag));
142 ID.AddInteger(Number);
146 void print(std::ostream *O) const {
149 void print(std::ostream &O) const {
151 if (Number) O << Number;
156 //===----------------------------------------------------------------------===//
157 /// DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
158 /// Dwarf abbreviation.
159 class DIEAbbrevData {
161 /// Attribute - Dwarf attribute code.
165 /// Form - Dwarf form code.
170 DIEAbbrevData(unsigned A, unsigned F)
176 unsigned getAttribute() const { return Attribute; }
177 unsigned getForm() const { return Form; }
179 /// Profile - Used to gather unique data for the abbreviation folding set.
181 void Profile(FoldingSetNodeID &ID)const {
182 ID.AddInteger(Attribute);
187 //===----------------------------------------------------------------------===//
188 /// DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
189 /// information object.
190 class DIEAbbrev : public FoldingSetNode {
192 /// Tag - Dwarf tag code.
196 /// Unique number for node.
200 /// ChildrenFlag - Dwarf children flag.
202 unsigned ChildrenFlag;
204 /// Data - Raw data bytes for abbreviation.
206 SmallVector<DIEAbbrevData, 8> Data;
210 DIEAbbrev(unsigned T, unsigned C)
218 unsigned getTag() const { return Tag; }
219 unsigned getNumber() const { return Number; }
220 unsigned getChildrenFlag() const { return ChildrenFlag; }
221 const SmallVector<DIEAbbrevData, 8> &getData() const { return Data; }
222 void setTag(unsigned T) { Tag = T; }
223 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
224 void setNumber(unsigned N) { Number = N; }
226 /// AddAttribute - Adds another set of attribute information to the
228 void AddAttribute(unsigned Attribute, unsigned Form) {
229 Data.push_back(DIEAbbrevData(Attribute, Form));
232 /// AddFirstAttribute - Adds a set of attribute information to the front
233 /// of the abbreviation.
234 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
235 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
238 /// Profile - Used to gather unique data for the abbreviation folding set.
240 void Profile(FoldingSetNodeID &ID) {
242 ID.AddInteger(ChildrenFlag);
244 // For each attribute description.
245 for (unsigned i = 0, N = Data.size(); i < N; ++i)
249 /// Emit - Print the abbreviation using the specified Dwarf writer.
251 void Emit(const DwarfDebug &DD) const;
254 void print(std::ostream *O) {
257 void print(std::ostream &O);
262 //===----------------------------------------------------------------------===//
263 /// DIE - A structured debug information entry. Has an abbreviation which
264 /// describes it's organization.
265 class DIE : public FoldingSetNode {
267 /// Abbrev - Buffer for constructing abbreviation.
271 /// Offset - Offset in debug info section.
275 /// Size - Size of instance + children.
281 std::vector<DIE *> Children;
283 /// Attributes values.
285 SmallVector<DIEValue*, 32> Values;
288 explicit DIE(unsigned Tag)
289 : Abbrev(Tag, DW_CHILDREN_no)
298 DIEAbbrev &getAbbrev() { return Abbrev; }
299 unsigned getAbbrevNumber() const {
300 return Abbrev.getNumber();
302 unsigned getTag() const { return Abbrev.getTag(); }
303 unsigned getOffset() const { return Offset; }
304 unsigned getSize() const { return Size; }
305 const std::vector<DIE *> &getChildren() const { return Children; }
306 SmallVector<DIEValue*, 32> &getValues() { return Values; }
307 void setTag(unsigned Tag) { Abbrev.setTag(Tag); }
308 void setOffset(unsigned O) { Offset = O; }
309 void setSize(unsigned S) { Size = S; }
311 /// AddValue - Add a value and attributes to a DIE.
313 void AddValue(unsigned Attribute, unsigned Form, DIEValue *Value) {
314 Abbrev.AddAttribute(Attribute, Form);
315 Values.push_back(Value);
318 /// SiblingOffset - Return the offset of the debug information entry's
320 unsigned SiblingOffset() const { return Offset + Size; }
322 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
324 void AddSiblingOffset();
326 /// AddChild - Add a child to the DIE.
328 void AddChild(DIE *Child) {
329 Abbrev.setChildrenFlag(DW_CHILDREN_yes);
330 Children.push_back(Child);
333 /// Detach - Detaches objects connected to it after copying.
339 /// Profile - Used to gather unique data for the value folding set.
341 void Profile(FoldingSetNodeID &ID) ;
344 void print(std::ostream *O, unsigned IncIndent = 0) {
345 if (O) print(*O, IncIndent);
347 void print(std::ostream &O, unsigned IncIndent = 0);
352 //===----------------------------------------------------------------------===//
353 /// DIEValue - A debug information entry value.
355 class DIEValue : public FoldingSetNode {
368 /// Type - Type of data stored in the value.
372 explicit DIEValue(unsigned T)
375 virtual ~DIEValue() {}
378 unsigned getType() const { return Type; }
380 // Implement isa/cast/dyncast.
381 static bool classof(const DIEValue *) { return true; }
383 /// EmitValue - Emit value via the Dwarf writer.
385 virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
387 /// SizeOf - Return the size of a value in bytes.
389 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const = 0;
391 /// Profile - Used to gather unique data for the value folding set.
393 virtual void Profile(FoldingSetNodeID &ID) = 0;
396 void print(std::ostream *O) {
399 virtual void print(std::ostream &O) = 0;
404 //===----------------------------------------------------------------------===//
405 /// DWInteger - An integer value DIE.
407 class DIEInteger : public DIEValue {
412 explicit DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
414 // Implement isa/cast/dyncast.
415 static bool classof(const DIEInteger *) { return true; }
416 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
418 /// BestForm - Choose the best form for integer.
420 static unsigned BestForm(bool IsSigned, uint64_t Integer) {
422 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
423 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
424 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
426 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
427 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
428 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
430 return DW_FORM_data8;
433 /// EmitValue - Emit integer of appropriate size.
435 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
437 /// SizeOf - Determine size of integer value in bytes.
439 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
441 /// Profile - Used to gather unique data for the value folding set.
443 static void Profile(FoldingSetNodeID &ID, unsigned Integer) {
444 ID.AddInteger(isInteger);
445 ID.AddInteger(Integer);
447 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Integer); }
450 virtual void print(std::ostream &O) {
451 O << "Int: " << (int64_t)Integer
452 << " 0x" << std::hex << Integer << std::dec;
457 //===----------------------------------------------------------------------===//
458 /// DIEString - A string value DIE.
460 class DIEString : public DIEValue {
462 const std::string String;
464 explicit DIEString(const std::string &S) : DIEValue(isString), String(S) {}
466 // Implement isa/cast/dyncast.
467 static bool classof(const DIEString *) { return true; }
468 static bool classof(const DIEValue *S) { return S->Type == isString; }
470 /// EmitValue - Emit string value.
472 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
474 /// SizeOf - Determine size of string value in bytes.
476 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
477 return String.size() + sizeof(char); // sizeof('\0');
480 /// Profile - Used to gather unique data for the value folding set.
482 static void Profile(FoldingSetNodeID &ID, const std::string &String) {
483 ID.AddInteger(isString);
484 ID.AddString(String);
486 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, String); }
489 virtual void print(std::ostream &O) {
490 O << "Str: \"" << String << "\"";
495 //===----------------------------------------------------------------------===//
496 /// DIEDwarfLabel - A Dwarf internal label expression DIE.
498 class DIEDwarfLabel : public DIEValue {
503 explicit DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
505 // Implement isa/cast/dyncast.
506 static bool classof(const DIEDwarfLabel *) { return true; }
507 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
509 /// EmitValue - Emit label value.
511 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
513 /// SizeOf - Determine size of label value in bytes.
515 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
517 /// Profile - Used to gather unique data for the value folding set.
519 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label) {
520 ID.AddInteger(isLabel);
523 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
526 virtual void print(std::ostream &O) {
534 //===----------------------------------------------------------------------===//
535 /// DIEObjectLabel - A label to an object in code or data.
537 class DIEObjectLabel : public DIEValue {
539 const std::string Label;
541 explicit DIEObjectLabel(const std::string &L)
542 : DIEValue(isAsIsLabel), Label(L) {}
544 // Implement isa/cast/dyncast.
545 static bool classof(const DIEObjectLabel *) { return true; }
546 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
548 /// EmitValue - Emit label value.
550 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
552 /// SizeOf - Determine size of label value in bytes.
554 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
556 /// Profile - Used to gather unique data for the value folding set.
558 static void Profile(FoldingSetNodeID &ID, const std::string &Label) {
559 ID.AddInteger(isAsIsLabel);
562 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label); }
565 virtual void print(std::ostream &O) {
566 O << "Obj: " << Label;
571 //===----------------------------------------------------------------------===//
572 /// DIESectionOffset - A section offset DIE.
574 class DIESectionOffset : public DIEValue {
577 const DWLabel Section;
581 DIESectionOffset(const DWLabel &Lab, const DWLabel &Sec,
582 bool isEH = false, bool useSet = true)
583 : DIEValue(isSectionOffset), Label(Lab), Section(Sec),
584 IsEH(isEH), UseSet(useSet) {}
586 // Implement isa/cast/dyncast.
587 static bool classof(const DIESectionOffset *) { return true; }
588 static bool classof(const DIEValue *D) { return D->Type == isSectionOffset; }
590 /// EmitValue - Emit section offset.
592 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
594 /// SizeOf - Determine size of section offset value in bytes.
596 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
598 /// Profile - Used to gather unique data for the value folding set.
600 static void Profile(FoldingSetNodeID &ID, const DWLabel &Label,
601 const DWLabel &Section) {
602 ID.AddInteger(isSectionOffset);
605 // IsEH and UseSet are specific to the Label/Section that we will emit
606 // the offset for; so Label/Section are enough for uniqueness.
608 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, Label, Section); }
611 virtual void print(std::ostream &O) {
616 O << "-" << IsEH << "-" << UseSet;
621 //===----------------------------------------------------------------------===//
622 /// DIEDelta - A simple label difference DIE.
624 class DIEDelta : public DIEValue {
626 const DWLabel LabelHi;
627 const DWLabel LabelLo;
629 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
630 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
632 // Implement isa/cast/dyncast.
633 static bool classof(const DIEDelta *) { return true; }
634 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
636 /// EmitValue - Emit delta value.
638 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
640 /// SizeOf - Determine size of delta value in bytes.
642 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
644 /// Profile - Used to gather unique data for the value folding set.
646 static void Profile(FoldingSetNodeID &ID, const DWLabel &LabelHi,
647 const DWLabel &LabelLo) {
648 ID.AddInteger(isDelta);
652 virtual void Profile(FoldingSetNodeID &ID) { Profile(ID, LabelHi, LabelLo); }
655 virtual void print(std::ostream &O) {
664 //===----------------------------------------------------------------------===//
665 /// DIEntry - A pointer to another debug information entry. An instance of this
666 /// class can also be used as a proxy for a debug information entry not yet
667 /// defined (ie. types.)
668 class DIEntry : public DIEValue {
672 explicit DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
674 // Implement isa/cast/dyncast.
675 static bool classof(const DIEntry *) { return true; }
676 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
678 /// EmitValue - Emit debug information entry offset.
680 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
682 /// SizeOf - Determine size of debug information entry in bytes.
684 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const {
685 return sizeof(int32_t);
688 /// Profile - Used to gather unique data for the value folding set.
690 static void Profile(FoldingSetNodeID &ID, DIE *Entry) {
691 ID.AddInteger(isEntry);
692 ID.AddPointer(Entry);
694 virtual void Profile(FoldingSetNodeID &ID) {
695 ID.AddInteger(isEntry);
698 ID.AddPointer(Entry);
705 virtual void print(std::ostream &O) {
706 O << "Die: 0x" << std::hex << (intptr_t)Entry << std::dec;
711 //===----------------------------------------------------------------------===//
712 /// DIEBlock - A block of values. Primarily used for location expressions.
714 class DIEBlock : public DIEValue, public DIE {
716 unsigned Size; // Size in bytes excluding size header.
726 // Implement isa/cast/dyncast.
727 static bool classof(const DIEBlock *) { return true; }
728 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
730 /// ComputeSize - calculate the size of the block.
732 unsigned ComputeSize(DwarfDebug &DD);
734 /// BestForm - Choose the best form for data.
736 unsigned BestForm() const {
737 if ((unsigned char)Size == Size) return DW_FORM_block1;
738 if ((unsigned short)Size == Size) return DW_FORM_block2;
739 if ((unsigned int)Size == Size) return DW_FORM_block4;
740 return DW_FORM_block;
743 /// EmitValue - Emit block data.
745 virtual void EmitValue(DwarfDebug &DD, unsigned Form);
747 /// SizeOf - Determine size of block data in bytes.
749 virtual unsigned SizeOf(const DwarfDebug &DD, unsigned Form) const;
752 /// Profile - Used to gather unique data for the value folding set.
754 virtual void Profile(FoldingSetNodeID &ID) {
755 ID.AddInteger(isBlock);
760 virtual void print(std::ostream &O) {
767 //===----------------------------------------------------------------------===//
768 /// CompileUnit - This dwarf writer support class manages information associate
769 /// with a source file.
772 /// ID - File identifier for source.
776 /// Die - Compile unit debug information entry.
780 /// GVToDieMap - Tracks the mapping of unit level debug informaton
781 /// variables to debug information entries.
782 std::map<GlobalVariable *, DIE *> GVToDieMap;
784 /// GVToDIEntryMap - Tracks the mapping of unit level debug informaton
785 /// descriptors to debug information entries using a DIEntry proxy.
786 std::map<GlobalVariable *, DIEntry *> GVToDIEntryMap;
788 /// Globals - A map of globally visible named entities for this unit.
790 std::map<std::string, DIE *> Globals;
792 /// DiesSet - Used to uniquely define dies within the compile unit.
794 FoldingSet<DIE> DiesSet;
797 CompileUnit(unsigned I, DIE *D)
798 : ID(I), Die(D), GVToDieMap(),
799 GVToDIEntryMap(), Globals(), DiesSet(InitDiesSetSize)
807 unsigned getID() const { return ID; }
808 DIE* getDie() const { return Die; }
809 std::map<std::string, DIE *> &getGlobals() { return Globals; }
811 /// hasContent - Return true if this compile unit has something to write out.
813 bool hasContent() const {
814 return !Die->getChildren().empty();
817 /// AddGlobal - Add a new global entity to the compile unit.
819 void AddGlobal(const std::string &Name, DIE *Die) {
823 /// getDieMapSlotFor - Returns the debug information entry map slot for the
824 /// specified debug variable.
825 DIE *&getDieMapSlotFor(GlobalVariable *GV) {
826 return GVToDieMap[GV];
829 /// getDIEntrySlotFor - Returns the debug information entry proxy slot for the
830 /// specified debug variable.
831 DIEntry *&getDIEntrySlotFor(GlobalVariable *GV) {
832 return GVToDIEntryMap[GV];
835 /// AddDie - Adds or interns the DIE to the compile unit.
837 DIE *AddDie(DIE &Buffer) {
841 DIE *Die = DiesSet.FindNodeOrInsertPos(ID, Where);
844 Die = new DIE(Buffer);
845 DiesSet.InsertNode(Die, Where);
846 this->Die->AddChild(Die);
854 //===----------------------------------------------------------------------===//
855 /// Dwarf - Emits general Dwarf directives.
861 //===--------------------------------------------------------------------===//
862 // Core attributes used by the Dwarf writer.
866 /// O - Stream to .s file.
870 /// Asm - Target of Dwarf emission.
874 /// TAI - Target asm information.
875 const TargetAsmInfo *TAI;
877 /// TD - Target data.
878 const TargetData *TD;
880 /// RI - Register Information.
881 const TargetRegisterInfo *RI;
883 /// M - Current module.
887 /// MF - Current machine function.
891 /// MMI - Collected machine module information.
893 MachineModuleInfo *MMI;
895 /// SubprogramCount - The running count of functions being compiled.
897 unsigned SubprogramCount;
899 /// Flavor - A unique string indicating what dwarf producer this is, used to
901 const char * const Flavor;
904 Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T,
909 , TD(Asm->TM.getTargetData())
910 , RI(Asm->TM.getRegisterInfo())
922 //===--------------------------------------------------------------------===//
925 AsmPrinter *getAsm() const { return Asm; }
926 MachineModuleInfo *getMMI() const { return MMI; }
927 const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
928 const TargetData *getTargetData() const { return TD; }
930 void PrintRelDirective(bool Force32Bit = false, bool isInSection = false)
932 if (isInSection && TAI->getDwarfSectionOffsetDirective())
933 O << TAI->getDwarfSectionOffsetDirective();
934 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
935 O << TAI->getData32bitsDirective();
937 O << TAI->getData64bitsDirective();
940 /// PrintLabelName - Print label name in form used by Dwarf writer.
942 void PrintLabelName(DWLabel Label) const {
943 PrintLabelName(Label.Tag, Label.Number);
945 void PrintLabelName(const char *Tag, unsigned Number) const {
946 O << TAI->getPrivateGlobalPrefix() << Tag;
947 if (Number) O << Number;
950 void PrintLabelName(const char *Tag, unsigned Number,
951 const char *Suffix) const {
952 O << TAI->getPrivateGlobalPrefix() << Tag;
953 if (Number) O << Number;
957 /// EmitLabel - Emit location label for internal use by Dwarf.
959 void EmitLabel(DWLabel Label) const {
960 EmitLabel(Label.Tag, Label.Number);
962 void EmitLabel(const char *Tag, unsigned Number) const {
963 PrintLabelName(Tag, Number);
967 /// EmitReference - Emit a reference to a label.
969 void EmitReference(DWLabel Label, bool IsPCRelative = false,
970 bool Force32Bit = false) const {
971 EmitReference(Label.Tag, Label.Number, IsPCRelative, Force32Bit);
973 void EmitReference(const char *Tag, unsigned Number,
974 bool IsPCRelative = false, bool Force32Bit = false) const {
975 PrintRelDirective(Force32Bit);
976 PrintLabelName(Tag, Number);
978 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
980 void EmitReference(const std::string &Name, bool IsPCRelative = false,
981 bool Force32Bit = false) const {
982 PrintRelDirective(Force32Bit);
986 if (IsPCRelative) O << "-" << TAI->getPCSymbol();
989 /// EmitDifference - Emit the difference between two labels. Some
990 /// assemblers do not behave with absolute expressions with data directives,
991 /// so there is an option (needsSet) to use an intermediary set expression.
992 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
993 bool IsSmall = false) {
994 EmitDifference(LabelHi.Tag, LabelHi.Number,
995 LabelLo.Tag, LabelLo.Number,
998 void EmitDifference(const char *TagHi, unsigned NumberHi,
999 const char *TagLo, unsigned NumberLo,
1000 bool IsSmall = false) {
1001 if (TAI->needsSet()) {
1003 PrintLabelName("set", SetCounter, Flavor);
1005 PrintLabelName(TagHi, NumberHi);
1007 PrintLabelName(TagLo, NumberLo);
1010 PrintRelDirective(IsSmall);
1011 PrintLabelName("set", SetCounter, Flavor);
1014 PrintRelDirective(IsSmall);
1016 PrintLabelName(TagHi, NumberHi);
1018 PrintLabelName(TagLo, NumberLo);
1022 void EmitSectionOffset(const char* Label, const char* Section,
1023 unsigned LabelNumber, unsigned SectionNumber,
1024 bool IsSmall = false, bool isEH = false,
1025 bool useSet = true) {
1026 bool printAbsolute = false;
1028 printAbsolute = TAI->isAbsoluteEHSectionOffsets();
1030 printAbsolute = TAI->isAbsoluteDebugSectionOffsets();
1032 if (TAI->needsSet() && useSet) {
1034 PrintLabelName("set", SetCounter, Flavor);
1036 PrintLabelName(Label, LabelNumber);
1038 if (!printAbsolute) {
1040 PrintLabelName(Section, SectionNumber);
1044 PrintRelDirective(IsSmall);
1046 PrintLabelName("set", SetCounter, Flavor);
1049 PrintRelDirective(IsSmall, true);
1051 PrintLabelName(Label, LabelNumber);
1053 if (!printAbsolute) {
1055 PrintLabelName(Section, SectionNumber);
1060 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1062 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1063 const std::vector<MachineMove> &Moves, bool isEH) {
1065 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1066 TargetFrameInfo::StackGrowsUp ?
1067 TD->getPointerSize() : -TD->getPointerSize();
1068 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
1070 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1071 const MachineMove &Move = Moves[i];
1072 unsigned LabelID = Move.getLabelID();
1075 LabelID = MMI->MappedLabel(LabelID);
1077 // Throw out move if the label is invalid.
1078 if (!LabelID) continue;
1081 const MachineLocation &Dst = Move.getDestination();
1082 const MachineLocation &Src = Move.getSource();
1084 // Advance row if new location.
1085 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
1086 Asm->EmitInt8(DW_CFA_advance_loc4);
1087 Asm->EOL("DW_CFA_advance_loc4");
1088 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
1091 BaseLabelID = LabelID;
1092 BaseLabel = "label";
1096 // If advancing cfa.
1097 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
1099 if (Src.getReg() == MachineLocation::VirtualFP) {
1100 Asm->EmitInt8(DW_CFA_def_cfa_offset);
1101 Asm->EOL("DW_CFA_def_cfa_offset");
1103 Asm->EmitInt8(DW_CFA_def_cfa);
1104 Asm->EOL("DW_CFA_def_cfa");
1105 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
1106 Asm->EOL("Register");
1109 int Offset = -Src.getOffset();
1111 Asm->EmitULEB128Bytes(Offset);
1114 assert(0 && "Machine move no supported yet.");
1116 } else if (Src.isReg() &&
1117 Src.getReg() == MachineLocation::VirtualFP) {
1119 Asm->EmitInt8(DW_CFA_def_cfa_register);
1120 Asm->EOL("DW_CFA_def_cfa_register");
1121 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
1122 Asm->EOL("Register");
1124 assert(0 && "Machine move no supported yet.");
1127 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
1128 int Offset = Dst.getOffset() / stackGrowth;
1131 Asm->EmitInt8(DW_CFA_offset_extended_sf);
1132 Asm->EOL("DW_CFA_offset_extended_sf");
1133 Asm->EmitULEB128Bytes(Reg);
1135 Asm->EmitSLEB128Bytes(Offset);
1137 } else if (Reg < 64) {
1138 Asm->EmitInt8(DW_CFA_offset + Reg);
1140 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
1143 Asm->EmitULEB128Bytes(Offset);
1146 Asm->EmitInt8(DW_CFA_offset_extended);
1147 Asm->EOL("DW_CFA_offset_extended");
1148 Asm->EmitULEB128Bytes(Reg);
1150 Asm->EmitULEB128Bytes(Offset);
1159 //===----------------------------------------------------------------------===//
1160 /// SrcLineInfo - This class is used to record source line correspondence.
1163 unsigned Line; // Source line number.
1164 unsigned Column; // Source column.
1165 unsigned SourceID; // Source ID number.
1166 unsigned LabelID; // Label in code ID number.
1168 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
1169 : Line(L), Column(C), SourceID(S), LabelID(I) {}
1172 unsigned getLine() const { return Line; }
1173 unsigned getColumn() const { return Column; }
1174 unsigned getSourceID() const { return SourceID; }
1175 unsigned getLabelID() const { return LabelID; }
1179 //===----------------------------------------------------------------------===//
1180 /// SrcFileInfo - This class is used to track source information.
1183 unsigned DirectoryID; // Directory ID number.
1184 std::string Name; // File name (not including directory.)
1186 SrcFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
1189 unsigned getDirectoryID() const { return DirectoryID; }
1190 const std::string &getName() const { return Name; }
1192 /// operator== - Used by UniqueVector to locate entry.
1194 bool operator==(const SrcFileInfo &SI) const {
1195 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
1198 /// operator< - Used by UniqueVector to locate entry.
1200 bool operator<(const SrcFileInfo &SI) const {
1201 return getDirectoryID() < SI.getDirectoryID() ||
1202 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
1206 //===----------------------------------------------------------------------===//
1207 /// DbgVariable - This class is used to track local variable information.
1211 DIVariable Var; // Variable Descriptor.
1212 unsigned FrameIndex; // Variable frame index.
1215 DbgVariable(DIVariable V, unsigned I) : Var(V), FrameIndex(I) {}
1218 DIVariable getVariable() const { return Var; }
1219 unsigned getFrameIndex() const { return FrameIndex; }
1222 //===----------------------------------------------------------------------===//
1223 /// DbgScope - This class is used to track scope information.
1227 DbgScope *Parent; // Parent to this scope.
1228 DIDescriptor Desc; // Debug info descriptor for scope.
1229 // Either subprogram or block.
1230 unsigned StartLabelID; // Label ID of the beginning of scope.
1231 unsigned EndLabelID; // Label ID of the end of scope.
1232 SmallVector<DbgScope *, 4> Scopes; // Scopes defined in scope.
1233 SmallVector<DbgVariable *, 8> Variables;// Variables declared in scope.
1236 DbgScope(DbgScope *P, DIDescriptor D)
1237 : Parent(P), Desc(D), StartLabelID(0), EndLabelID(0), Scopes(), Variables()
1240 for (unsigned i = 0, N = Scopes.size(); i < N; ++i) delete Scopes[i];
1241 for (unsigned j = 0, M = Variables.size(); j < M; ++j) delete Variables[j];
1245 DbgScope *getParent() const { return Parent; }
1246 DIDescriptor getDesc() const { return Desc; }
1247 unsigned getStartLabelID() const { return StartLabelID; }
1248 unsigned getEndLabelID() const { return EndLabelID; }
1249 SmallVector<DbgScope *, 4> &getScopes() { return Scopes; }
1250 SmallVector<DbgVariable *, 8> &getVariables() { return Variables; }
1251 void setStartLabelID(unsigned S) { StartLabelID = S; }
1252 void setEndLabelID(unsigned E) { EndLabelID = E; }
1254 /// AddScope - Add a scope to the scope.
1256 void AddScope(DbgScope *S) { Scopes.push_back(S); }
1258 /// AddVariable - Add a variable to the scope.
1260 void AddVariable(DbgVariable *V) { Variables.push_back(V); }
1263 //===----------------------------------------------------------------------===//
1264 /// DwarfDebug - Emits Dwarf debug directives.
1266 class DwarfDebug : public Dwarf {
1269 //===--------------------------------------------------------------------===//
1270 // Attributes used to construct specific Dwarf sections.
1273 /// DW_CUs - All the compile units involved in this build. The index
1274 /// of each entry in this vector corresponds to the sources in MMI.
1275 DenseMap<Value *, CompileUnit *> DW_CUs;
1277 /// AbbreviationsSet - Used to uniquely define abbreviations.
1279 FoldingSet<DIEAbbrev> AbbreviationsSet;
1281 /// Abbreviations - A list of all the unique abbreviations in use.
1283 std::vector<DIEAbbrev *> Abbreviations;
1285 /// Directories - Uniquing vector for directories.
1286 UniqueVector<std::string> Directories;
1288 /// SourceFiles - Uniquing vector for source files.
1289 UniqueVector<SrcFileInfo> SrcFiles;
1291 /// Lines - List of of source line correspondence.
1292 std::vector<SrcLineInfo> Lines;
1294 /// ValuesSet - Used to uniquely define values.
1296 FoldingSet<DIEValue> ValuesSet;
1298 /// Values - A list of all the unique values in use.
1300 std::vector<DIEValue *> Values;
1302 /// StringPool - A UniqueVector of strings used by indirect references.
1304 UniqueVector<std::string> StringPool;
1306 /// SectionMap - Provides a unique id per text section.
1308 UniqueVector<const Section*> SectionMap;
1310 /// SectionSourceLines - Tracks line numbers per text section.
1312 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
1314 /// didInitial - Flag to indicate if initial emission has been done.
1318 /// shouldEmit - Flag to indicate if debug information should be emitted.
1322 // RootDbgScope - Top level scope for the current function.
1324 DbgScope *RootDbgScope;
1326 // DbgScopeMap - Tracks the scopes in the current function.
1327 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
1329 struct FunctionDebugFrameInfo {
1331 std::vector<MachineMove> Moves;
1333 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M):
1334 Number(Num), Moves(M) { }
1337 std::vector<FunctionDebugFrameInfo> DebugFrames;
1341 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
1343 bool ShouldEmitDwarf() const { return shouldEmit; }
1345 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
1347 void AssignAbbrevNumber(DIEAbbrev &Abbrev) {
1348 // Profile the node so that we can make it unique.
1349 FoldingSetNodeID ID;
1352 // Check the set for priors.
1353 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
1355 // If it's newly added.
1356 if (InSet == &Abbrev) {
1357 // Add to abbreviation list.
1358 Abbreviations.push_back(&Abbrev);
1359 // Assign the vector position + 1 as its number.
1360 Abbrev.setNumber(Abbreviations.size());
1362 // Assign existing abbreviation number.
1363 Abbrev.setNumber(InSet->getNumber());
1367 /// NewString - Add a string to the constant pool and returns a label.
1369 DWLabel NewString(const std::string &String) {
1370 unsigned StringID = StringPool.insert(String);
1371 return DWLabel("string", StringID);
1374 /// NewDIEntry - Creates a new DIEntry to be a proxy for a debug information
1376 DIEntry *NewDIEntry(DIE *Entry = NULL) {
1380 FoldingSetNodeID ID;
1381 DIEntry::Profile(ID, Entry);
1383 Value = static_cast<DIEntry *>(ValuesSet.FindNodeOrInsertPos(ID, Where));
1385 if (Value) return Value;
1387 Value = new DIEntry(Entry);
1388 ValuesSet.InsertNode(Value, Where);
1390 Value = new DIEntry(Entry);
1393 Values.push_back(Value);
1397 /// SetDIEntry - Set a DIEntry once the debug information entry is defined.
1399 void SetDIEntry(DIEntry *Value, DIE *Entry) {
1400 Value->Entry = Entry;
1401 // Add to values set if not already there. If it is, we merely have a
1402 // duplicate in the values list (no harm.)
1403 ValuesSet.GetOrInsertNode(Value);
1406 /// AddUInt - Add an unsigned integer attribute data and value.
1408 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer) {
1409 if (!Form) Form = DIEInteger::BestForm(false, Integer);
1411 FoldingSetNodeID ID;
1412 DIEInteger::Profile(ID, Integer);
1414 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1416 Value = new DIEInteger(Integer);
1417 ValuesSet.InsertNode(Value, Where);
1418 Values.push_back(Value);
1421 Die->AddValue(Attribute, Form, Value);
1424 /// AddSInt - Add an signed integer attribute data and value.
1426 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer) {
1427 if (!Form) Form = DIEInteger::BestForm(true, Integer);
1429 FoldingSetNodeID ID;
1430 DIEInteger::Profile(ID, (uint64_t)Integer);
1432 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1434 Value = new DIEInteger(Integer);
1435 ValuesSet.InsertNode(Value, Where);
1436 Values.push_back(Value);
1439 Die->AddValue(Attribute, Form, Value);
1442 /// AddString - Add a std::string attribute data and value.
1444 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
1445 const std::string &String) {
1446 FoldingSetNodeID ID;
1447 DIEString::Profile(ID, String);
1449 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1451 Value = new DIEString(String);
1452 ValuesSet.InsertNode(Value, Where);
1453 Values.push_back(Value);
1456 Die->AddValue(Attribute, Form, Value);
1459 /// AddLabel - Add a Dwarf label attribute data and value.
1461 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
1462 const DWLabel &Label) {
1463 FoldingSetNodeID ID;
1464 DIEDwarfLabel::Profile(ID, Label);
1466 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1468 Value = new DIEDwarfLabel(Label);
1469 ValuesSet.InsertNode(Value, Where);
1470 Values.push_back(Value);
1473 Die->AddValue(Attribute, Form, Value);
1476 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
1478 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
1479 const std::string &Label) {
1480 FoldingSetNodeID ID;
1481 DIEObjectLabel::Profile(ID, Label);
1483 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1485 Value = new DIEObjectLabel(Label);
1486 ValuesSet.InsertNode(Value, Where);
1487 Values.push_back(Value);
1490 Die->AddValue(Attribute, Form, Value);
1493 /// AddSectionOffset - Add a section offset label attribute data and value.
1495 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
1496 const DWLabel &Label, const DWLabel &Section,
1497 bool isEH = false, bool useSet = true) {
1498 FoldingSetNodeID ID;
1499 DIESectionOffset::Profile(ID, Label, Section);
1501 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1503 Value = new DIESectionOffset(Label, Section, isEH, useSet);
1504 ValuesSet.InsertNode(Value, Where);
1505 Values.push_back(Value);
1508 Die->AddValue(Attribute, Form, Value);
1511 /// AddDelta - Add a label delta attribute data and value.
1513 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
1514 const DWLabel &Hi, const DWLabel &Lo) {
1515 FoldingSetNodeID ID;
1516 DIEDelta::Profile(ID, Hi, Lo);
1518 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1520 Value = new DIEDelta(Hi, Lo);
1521 ValuesSet.InsertNode(Value, Where);
1522 Values.push_back(Value);
1525 Die->AddValue(Attribute, Form, Value);
1528 /// AddDIEntry - Add a DIE attribute data and value.
1530 void AddDIEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
1531 Die->AddValue(Attribute, Form, NewDIEntry(Entry));
1534 /// AddBlock - Add block data.
1536 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block) {
1537 Block->ComputeSize(*this);
1538 FoldingSetNodeID ID;
1541 DIEValue *Value = ValuesSet.FindNodeOrInsertPos(ID, Where);
1544 ValuesSet.InsertNode(Value, Where);
1545 Values.push_back(Value);
1547 // Already exists, reuse the previous one.
1549 Block = cast<DIEBlock>(Value);
1552 Die->AddValue(Attribute, Block->BestForm(), Value);
1557 /// AddSourceLine - Add location information to specified debug information
1559 void AddSourceLine(DIE *Die, const DIVariable *V) {
1560 unsigned FileID = 0;
1561 unsigned Line = V->getLineNumber();
1562 if (V->getVersion() <= LLVMDebugVersion6) {
1563 // Version6 or earlier. Use compile unit info to get file id.
1564 CompileUnit *Unit = FindCompileUnit(V->getCompileUnit());
1565 FileID = Unit->getID();
1567 // Version7 or newer, use filename and directory info from DIVariable
1569 unsigned DID = Directories.idFor(V->getDirectory());
1570 FileID = SrcFiles.idFor(SrcFileInfo(DID, V->getFilename()));
1572 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1573 AddUInt(Die, DW_AT_decl_line, 0, Line);
1576 /// AddSourceLine - Add location information to specified debug information
1578 void AddSourceLine(DIE *Die, const DIGlobal *G) {
1579 unsigned FileID = 0;
1580 unsigned Line = G->getLineNumber();
1581 if (G->getVersion() < LLVMDebugVersion6) {
1582 // Version6 or earlier. Use compile unit info to get file id.
1583 CompileUnit *Unit = FindCompileUnit(G->getCompileUnit());
1584 FileID = Unit->getID();
1586 // Version7 or newer, use filename and directory info from DIGlobal
1588 unsigned DID = Directories.idFor(G->getDirectory());
1589 FileID = SrcFiles.idFor(SrcFileInfo(DID, G->getFilename()));
1591 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1592 AddUInt(Die, DW_AT_decl_line, 0, Line);
1595 void AddSourceLine(DIE *Die, const DIType *Ty) {
1596 unsigned FileID = 0;
1597 unsigned Line = Ty->getLineNumber();
1598 if (Ty->getVersion() <= LLVMDebugVersion6) {
1599 // Version6 or earlier. Use compile unit info to get file id.
1600 CompileUnit *Unit = FindCompileUnit(Ty->getCompileUnit());
1601 FileID = Unit->getID();
1603 // Version7 or newer, use filename and directory info from DIType
1605 unsigned DID = Directories.idFor(Ty->getDirectory());
1606 FileID = SrcFiles.idFor(SrcFileInfo(DID, Ty->getFilename()));
1608 AddUInt(Die, DW_AT_decl_file, 0, FileID);
1609 AddUInt(Die, DW_AT_decl_line, 0, Line);
1612 /// AddAddress - Add an address attribute to a die based on the location
1614 void AddAddress(DIE *Die, unsigned Attribute,
1615 const MachineLocation &Location) {
1616 unsigned Reg = RI->getDwarfRegNum(Location.getReg(), false);
1617 DIEBlock *Block = new DIEBlock();
1619 if (Location.isReg()) {
1621 AddUInt(Block, 0, DW_FORM_data1, DW_OP_reg0 + Reg);
1623 AddUInt(Block, 0, DW_FORM_data1, DW_OP_regx);
1624 AddUInt(Block, 0, DW_FORM_udata, Reg);
1628 AddUInt(Block, 0, DW_FORM_data1, DW_OP_breg0 + Reg);
1630 AddUInt(Block, 0, DW_FORM_data1, DW_OP_bregx);
1631 AddUInt(Block, 0, DW_FORM_udata, Reg);
1633 AddUInt(Block, 0, DW_FORM_sdata, Location.getOffset());
1636 AddBlock(Die, Attribute, 0, Block);
1639 /// AddBasicType - Add a new basic type attribute to the specified entity.
1641 void AddBasicType(DIE *Entity, CompileUnit *Unit,
1642 const std::string &Name,
1643 unsigned Encoding, unsigned Size) {
1645 DIE Buffer(DW_TAG_base_type);
1646 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1647 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, Encoding);
1648 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1649 DIE *BasicTypeDie = Unit->AddDie(Buffer);
1650 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, BasicTypeDie);
1653 /// AddPointerType - Add a new pointer type attribute to the specified entity.
1655 void AddPointerType(DIE *Entity, CompileUnit *Unit, const std::string &Name) {
1656 DIE Buffer(DW_TAG_pointer_type);
1657 AddUInt(&Buffer, DW_AT_byte_size, 0, TD->getPointerSize());
1658 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1659 DIE *PointerTypeDie = Unit->AddDie(Buffer);
1660 AddDIEntry(Entity, DW_AT_type, DW_FORM_ref4, PointerTypeDie);
1663 /// AddType - Add a new type attribute to the specified entity.
1664 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty) {
1666 AddBasicType(Entity, DW_Unit, "", DW_ATE_signed, sizeof(int32_t));
1670 // Check for pre-existence.
1671 DIEntry *&Slot = DW_Unit->getDIEntrySlotFor(Ty.getGV());
1672 // If it exists then use the existing value.
1674 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1679 Slot = NewDIEntry();
1682 DIE Buffer(DW_TAG_base_type);
1683 if (Ty.isBasicType(Ty.getTag()))
1684 ConstructTypeDIE(DW_Unit, Buffer, DIBasicType(Ty.getGV()));
1685 else if (Ty.isDerivedType(Ty.getTag()))
1686 ConstructTypeDIE(DW_Unit, Buffer, DIDerivedType(Ty.getGV()));
1688 assert (Ty.isCompositeType(Ty.getTag()) && "Unknown kind of DIType");
1689 ConstructTypeDIE(DW_Unit, Buffer, DICompositeType(Ty.getGV()));
1692 // Add debug information entry to entity and unit.
1693 DIE *Die = DW_Unit->AddDie(Buffer);
1694 SetDIEntry(Slot, Die);
1695 Entity->AddValue(DW_AT_type, DW_FORM_ref4, Slot);
1698 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
1699 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1702 // Get core information.
1703 const std::string &Name = BTy.getName();
1704 Buffer.setTag(DW_TAG_base_type);
1705 AddUInt(&Buffer, DW_AT_encoding, DW_FORM_data1, BTy.getEncoding());
1706 // Add name if not anonymous or intermediate type.
1708 AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1709 uint64_t Size = BTy.getSizeInBits() >> 3;
1710 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1713 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
1714 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1715 DIDerivedType DTy) {
1717 // Get core information.
1718 const std::string &Name = DTy.getName();
1719 uint64_t Size = DTy.getSizeInBits() >> 3;
1720 unsigned Tag = DTy.getTag();
1721 // FIXME - Workaround for templates.
1722 if (Tag == DW_TAG_inheritance) Tag = DW_TAG_reference_type;
1725 // Map to main type, void will not have a type.
1726 DIType FromTy = DTy.getTypeDerivedFrom();
1727 AddType(DW_Unit, &Buffer, FromTy);
1729 // Add name if not anonymous or intermediate type.
1730 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1732 // Add size if non-zero (derived types might be zero-sized.)
1734 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1736 // Add source line info if available and TyDesc is not a forward
1738 // FIXME - Enable this. if (!DTy.isForwardDecl())
1739 // FIXME - Enable this. AddSourceLine(&Buffer, *DTy);
1742 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
1743 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1744 DICompositeType CTy) {
1746 // Get core information.
1747 const std::string &Name = CTy.getName();
1748 uint64_t Size = CTy.getSizeInBits() >> 3;
1749 unsigned Tag = CTy.getTag();
1751 case DW_TAG_vector_type:
1752 case DW_TAG_array_type:
1753 ConstructArrayTypeDIE(DW_Unit, Buffer, &CTy);
1755 case DW_TAG_enumeration_type:
1757 DIArray Elements = CTy.getTypeArray();
1758 // Add enumerators to enumeration type.
1759 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1760 DIE *ElemDie = NULL;
1761 DIEnumerator Enum(Elements.getElement(i).getGV());
1762 ElemDie = ConstructEnumTypeDIE(DW_Unit, &Enum);
1763 Buffer.AddChild(ElemDie);
1767 case DW_TAG_subroutine_type:
1769 // Add prototype flag.
1770 AddUInt(&Buffer, DW_AT_prototyped, DW_FORM_flag, 1);
1771 DIArray Elements = CTy.getTypeArray();
1773 DIDescriptor RTy = Elements.getElement(0);
1774 AddType(DW_Unit, &Buffer, DIType(RTy.getGV()));
1777 for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) {
1778 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1779 DIDescriptor Ty = Elements.getElement(i);
1780 AddType(DW_Unit, Arg, DIType(Ty.getGV()));
1781 Buffer.AddChild(Arg);
1785 case DW_TAG_structure_type:
1786 case DW_TAG_union_type:
1788 // Add elements to structure type.
1789 DIArray Elements = CTy.getTypeArray();
1791 // A forward struct declared type may not have elements available.
1792 if (Elements.isNull())
1795 // Add elements to structure type.
1796 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1797 DIDescriptor Element = Elements.getElement(i);
1798 DIE *ElemDie = NULL;
1799 if (Element.getTag() == dwarf::DW_TAG_subprogram)
1800 ElemDie = CreateSubprogramDIE(DW_Unit,
1801 DISubprogram(Element.getGV()));
1802 else if (Element.getTag() == dwarf::DW_TAG_variable) // ???
1803 ElemDie = CreateGlobalVariableDIE(DW_Unit,
1804 DIGlobalVariable(Element.getGV()));
1806 ElemDie = CreateMemberDIE(DW_Unit,
1807 DIDerivedType(Element.getGV()));
1808 Buffer.AddChild(ElemDie);
1816 // Add name if not anonymous or intermediate type.
1817 if (!Name.empty()) AddString(&Buffer, DW_AT_name, DW_FORM_string, Name);
1819 // Add size if non-zero (derived types might be zero-sized.)
1821 AddUInt(&Buffer, DW_AT_byte_size, 0, Size);
1823 // Add zero size if it is not a forward declaration.
1824 if (CTy.isForwardDecl())
1825 AddUInt(&Buffer, DW_AT_declaration, DW_FORM_flag, 1);
1827 AddUInt(&Buffer, DW_AT_byte_size, 0, 0);
1830 // Add source line info if available.
1831 if (!CTy.isForwardDecl())
1832 AddSourceLine(&Buffer, &CTy);
1835 // ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
1836 void ConstructSubrangeDIE (DIE &Buffer, DISubrange SR, DIE *IndexTy) {
1837 int64_t L = SR.getLo();
1838 int64_t H = SR.getHi();
1839 DIE *DW_Subrange = new DIE(DW_TAG_subrange_type);
1841 AddDIEntry(DW_Subrange, DW_AT_type, DW_FORM_ref4, IndexTy);
1843 AddSInt(DW_Subrange, DW_AT_lower_bound, 0, L);
1844 AddSInt(DW_Subrange, DW_AT_upper_bound, 0, H);
1846 Buffer.AddChild(DW_Subrange);
1849 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
1850 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
1851 DICompositeType *CTy) {
1852 Buffer.setTag(DW_TAG_array_type);
1853 if (CTy->getTag() == DW_TAG_vector_type)
1854 AddUInt(&Buffer, DW_AT_GNU_vector, DW_FORM_flag, 1);
1856 DIArray Elements = CTy->getTypeArray();
1857 AddType(DW_Unit, &Buffer, CTy->getTypeDerivedFrom());
1859 // Construct an anonymous type for index type.
1860 DIE IdxBuffer(DW_TAG_base_type);
1861 AddUInt(&IdxBuffer, DW_AT_byte_size, 0, sizeof(int32_t));
1862 AddUInt(&IdxBuffer, DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1863 DIE *IndexTy = DW_Unit->AddDie(IdxBuffer);
1865 // Add subranges to array type.
1866 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
1867 DIDescriptor Element = Elements.getElement(i);
1868 if (Element.getTag() == dwarf::DW_TAG_subrange_type)
1869 ConstructSubrangeDIE(Buffer, DISubrange(Element.getGV()), IndexTy);
1873 /// ConstructEnumTypeDIE - Construct enum type DIE from
1875 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy) {
1877 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1878 AddString(Enumerator, DW_AT_name, DW_FORM_string, ETy->getName());
1879 int64_t Value = ETy->getEnumValue();
1880 AddSInt(Enumerator, DW_AT_const_value, DW_FORM_sdata, Value);
1884 /// CreateGlobalVariableDIE - Create new DIE using GV.
1885 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit, const DIGlobalVariable &GV)
1887 DIE *GVDie = new DIE(DW_TAG_variable);
1888 AddString(GVDie, DW_AT_name, DW_FORM_string, GV.getName());
1889 const std::string &LinkageName = GV.getLinkageName();
1890 if (!LinkageName.empty())
1891 AddString(GVDie, DW_AT_MIPS_linkage_name, DW_FORM_string, LinkageName);
1892 AddType(DW_Unit, GVDie, GV.getType());
1893 if (!GV.isLocalToUnit())
1894 AddUInt(GVDie, DW_AT_external, DW_FORM_flag, 1);
1895 AddSourceLine(GVDie, &GV);
1899 /// CreateMemberDIE - Create new member DIE.
1900 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT) {
1901 DIE *MemberDie = new DIE(DT.getTag());
1902 std::string Name = DT.getName();
1904 AddString(MemberDie, DW_AT_name, DW_FORM_string, Name);
1906 AddType(DW_Unit, MemberDie, DT.getTypeDerivedFrom());
1908 AddSourceLine(MemberDie, &DT);
1910 AddUInt(MemberDie, DW_AT_bit_size, 0, DT.getSizeInBits());
1911 DIEBlock *Block = new DIEBlock();
1912 AddUInt(Block, 0, DW_FORM_data1, DW_OP_plus_uconst);
1913 AddUInt(Block, 0, DW_FORM_udata, DT.getOffsetInBits() >> 3);
1914 AddBlock(MemberDie, DW_AT_data_member_location, 0, Block);
1916 // FIXME - Handle DW_AT_accessibility
1920 /// CreateSubprogramDIE - Create new DIE using SP.
1921 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
1922 const DISubprogram &SP,
1923 bool IsConstructor = false) {
1924 DIE *SPDie = new DIE(DW_TAG_subprogram);
1925 AddString(SPDie, DW_AT_name, DW_FORM_string, SP.getName());
1926 const std::string &LinkageName = SP.getLinkageName();
1927 if (!LinkageName.empty())
1928 AddString(SPDie, DW_AT_MIPS_linkage_name, DW_FORM_string,
1930 AddSourceLine(SPDie, &SP);
1932 DICompositeType SPTy = SP.getType();
1933 DIArray Args = SPTy.getTypeArray();
1937 AddType(DW_Unit, SPDie, DIType(Args.getElement(0).getGV()));
1941 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
1942 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1943 AddType(DW_Unit, Arg, DIType(Args.getElement(i).getGV()));
1944 AddUInt(Arg, DW_AT_artificial, DW_FORM_flag, 1); // ???
1945 SPDie->AddChild(Arg);
1948 if (!SP.isLocalToUnit())
1949 AddUInt(SPDie, DW_AT_external, DW_FORM_flag, 1);
1953 /// FindCompileUnit - Get the compile unit for the given descriptor.
1955 CompileUnit *FindCompileUnit(DICompileUnit Unit) {
1956 CompileUnit *DW_Unit = DW_CUs[Unit.getGV()];
1957 assert(DW_Unit && "Missing compile unit.");
1961 /// NewDbgScopeVariable - Create a new scope variable.
1963 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit) {
1964 // Get the descriptor.
1965 const DIVariable &VD = DV->getVariable();
1967 // Translate tag to proper Dwarf tag. The result variable is dropped for
1970 switch (VD.getTag()) {
1971 case DW_TAG_return_variable: return NULL;
1972 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1973 case DW_TAG_auto_variable: // fall thru
1974 default: Tag = DW_TAG_variable; break;
1977 // Define variable debug information entry.
1978 DIE *VariableDie = new DIE(Tag);
1979 AddString(VariableDie, DW_AT_name, DW_FORM_string, VD.getName());
1981 // Add source line info if available.
1982 AddSourceLine(VariableDie, &VD);
1984 // Add variable type.
1985 AddType(Unit, VariableDie, VD.getType());
1987 // Add variable address.
1988 MachineLocation Location;
1989 Location.set(RI->getFrameRegister(*MF),
1990 RI->getFrameIndexOffset(*MF, DV->getFrameIndex()));
1991 AddAddress(VariableDie, DW_AT_location, Location);
1996 /// getOrCreateScope - Returns the scope associated with the given descriptor.
1998 DbgScope *getOrCreateScope(GlobalVariable *V) {
1999 DbgScope *&Slot = DbgScopeMap[V];
2001 // FIXME - breaks down when the context is an inlined function.
2002 DIDescriptor ParentDesc;
2003 DIDescriptor Desc(V);
2004 if (Desc.getTag() == dwarf::DW_TAG_lexical_block) {
2006 ParentDesc = Block.getContext();
2008 DbgScope *Parent = ParentDesc.isNull() ?
2009 NULL : getOrCreateScope(ParentDesc.getGV());
2010 Slot = new DbgScope(Parent, Desc);
2012 Parent->AddScope(Slot);
2013 } else if (RootDbgScope) {
2014 // FIXME - Add inlined function scopes to the root so we can delete
2015 // them later. Long term, handle inlined functions properly.
2016 RootDbgScope->AddScope(Slot);
2018 // First function is top level function.
2019 RootDbgScope = Slot;
2025 /// ConstructDbgScope - Construct the components of a scope.
2027 void ConstructDbgScope(DbgScope *ParentScope,
2028 unsigned ParentStartID, unsigned ParentEndID,
2029 DIE *ParentDie, CompileUnit *Unit) {
2030 // Add variables to scope.
2031 SmallVector<DbgVariable *, 8> &Variables = ParentScope->getVariables();
2032 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
2033 DIE *VariableDie = NewDbgScopeVariable(Variables[i], Unit);
2034 if (VariableDie) ParentDie->AddChild(VariableDie);
2037 // Add nested scopes.
2038 SmallVector<DbgScope *, 4> &Scopes = ParentScope->getScopes();
2039 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
2040 // Define the Scope debug information entry.
2041 DbgScope *Scope = Scopes[j];
2042 // FIXME - Ignore inlined functions for the time being.
2043 if (!Scope->getParent()) continue;
2045 unsigned StartID = MMI->MappedLabel(Scope->getStartLabelID());
2046 unsigned EndID = MMI->MappedLabel(Scope->getEndLabelID());
2048 // Ignore empty scopes.
2049 if (StartID == EndID && StartID != 0) continue;
2050 if (Scope->getScopes().empty() && Scope->getVariables().empty()) continue;
2052 if (StartID == ParentStartID && EndID == ParentEndID) {
2053 // Just add stuff to the parent scope.
2054 ConstructDbgScope(Scope, ParentStartID, ParentEndID, ParentDie, Unit);
2056 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
2058 // Add the scope bounds.
2060 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2061 DWLabel("label", StartID));
2063 AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
2064 DWLabel("func_begin", SubprogramCount));
2067 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2068 DWLabel("label", EndID));
2070 AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
2071 DWLabel("func_end", SubprogramCount));
2074 // Add the scope contents.
2075 ConstructDbgScope(Scope, StartID, EndID, ScopeDie, Unit);
2076 ParentDie->AddChild(ScopeDie);
2081 /// ConstructRootDbgScope - Construct the scope for the subprogram.
2083 void ConstructRootDbgScope(DbgScope *RootScope) {
2084 // Exit if there is no root scope.
2085 if (!RootScope) return;
2086 DIDescriptor Desc = RootScope->getDesc();
2090 // Get the subprogram debug information entry.
2091 DISubprogram SPD(Desc.getGV());
2093 // Get the compile unit context.
2094 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
2096 // Get the subprogram die.
2097 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
2098 assert(SPDie && "Missing subprogram descriptor");
2100 // Add the function bounds.
2101 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2102 DWLabel("func_begin", SubprogramCount));
2103 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2104 DWLabel("func_end", SubprogramCount));
2105 MachineLocation Location(RI->getFrameRegister(*MF));
2106 AddAddress(SPDie, DW_AT_frame_base, Location);
2108 ConstructDbgScope(RootScope, 0, 0, SPDie, Unit);
2111 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
2113 void ConstructDefaultDbgScope(MachineFunction *MF) {
2114 // Find the correct subprogram descriptor.
2115 std::string SPName = "llvm.dbg.subprograms";
2116 std::vector<GlobalVariable*> Result;
2117 getGlobalVariablesUsing(*M, SPName, Result);
2118 for (std::vector<GlobalVariable *>::iterator I = Result.begin(),
2119 E = Result.end(); I != E; ++I) {
2121 DISubprogram SPD(*I);
2123 if (SPD.getName() == MF->getFunction()->getName()) {
2124 // Get the compile unit context.
2125 CompileUnit *Unit = FindCompileUnit(SPD.getCompileUnit());
2127 // Get the subprogram die.
2128 DIE *SPDie = Unit->getDieMapSlotFor(SPD.getGV());
2129 assert(SPDie && "Missing subprogram descriptor");
2131 // Add the function bounds.
2132 AddLabel(SPDie, DW_AT_low_pc, DW_FORM_addr,
2133 DWLabel("func_begin", SubprogramCount));
2134 AddLabel(SPDie, DW_AT_high_pc, DW_FORM_addr,
2135 DWLabel("func_end", SubprogramCount));
2137 MachineLocation Location(RI->getFrameRegister(*MF));
2138 AddAddress(SPDie, DW_AT_frame_base, Location);
2143 // FIXME: This is causing an abort because C++ mangled names are compared
2144 // with their unmangled counterparts. See PR2885. Don't do this assert.
2145 assert(0 && "Couldn't find DIE for machine function!");
2149 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
2150 /// tools to recognize the object file contains Dwarf information.
2151 void EmitInitial() {
2152 // Check to see if we already emitted intial headers.
2153 if (didInitial) return;
2156 // Dwarf sections base addresses.
2157 if (TAI->doesDwarfRequireFrameSection()) {
2158 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2159 EmitLabel("section_debug_frame", 0);
2161 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2162 EmitLabel("section_info", 0);
2163 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2164 EmitLabel("section_abbrev", 0);
2165 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2166 EmitLabel("section_aranges", 0);
2167 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2168 EmitLabel("section_macinfo", 0);
2169 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2170 EmitLabel("section_line", 0);
2171 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2172 EmitLabel("section_loc", 0);
2173 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2174 EmitLabel("section_pubnames", 0);
2175 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2176 EmitLabel("section_str", 0);
2177 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2178 EmitLabel("section_ranges", 0);
2180 Asm->SwitchToSection(TAI->getTextSection());
2181 EmitLabel("text_begin", 0);
2182 Asm->SwitchToSection(TAI->getDataSection());
2183 EmitLabel("data_begin", 0);
2186 /// EmitDIE - Recusively Emits a debug information entry.
2188 void EmitDIE(DIE *Die) {
2189 // Get the abbreviation for this DIE.
2190 unsigned AbbrevNumber = Die->getAbbrevNumber();
2191 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2195 // Emit the code (index) for the abbreviation.
2196 Asm->EmitULEB128Bytes(AbbrevNumber);
2199 Asm->EOL(std::string("Abbrev [" +
2200 utostr(AbbrevNumber) +
2201 "] 0x" + utohexstr(Die->getOffset()) +
2202 ":0x" + utohexstr(Die->getSize()) + " " +
2203 TagString(Abbrev->getTag())));
2207 SmallVector<DIEValue*, 32> &Values = Die->getValues();
2208 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2210 // Emit the DIE attribute values.
2211 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2212 unsigned Attr = AbbrevData[i].getAttribute();
2213 unsigned Form = AbbrevData[i].getForm();
2214 assert(Form && "Too many attributes for DIE (check abbreviation)");
2217 case DW_AT_sibling: {
2218 Asm->EmitInt32(Die->SiblingOffset());
2222 // Emit an attribute using the defined form.
2223 Values[i]->EmitValue(*this, Form);
2228 Asm->EOL(AttributeString(Attr));
2231 // Emit the DIE children if any.
2232 if (Abbrev->getChildrenFlag() == DW_CHILDREN_yes) {
2233 const std::vector<DIE *> &Children = Die->getChildren();
2235 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2236 EmitDIE(Children[j]);
2239 Asm->EmitInt8(0); Asm->EOL("End Of Children Mark");
2243 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
2245 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
2246 // Get the children.
2247 const std::vector<DIE *> &Children = Die->getChildren();
2249 // If not last sibling and has children then add sibling offset attribute.
2250 if (!Last && !Children.empty()) Die->AddSiblingOffset();
2252 // Record the abbreviation.
2253 AssignAbbrevNumber(Die->getAbbrev());
2255 // Get the abbreviation for this DIE.
2256 unsigned AbbrevNumber = Die->getAbbrevNumber();
2257 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
2260 Die->setOffset(Offset);
2262 // Start the size with the size of abbreviation code.
2263 Offset += TargetAsmInfo::getULEB128Size(AbbrevNumber);
2265 const SmallVector<DIEValue*, 32> &Values = Die->getValues();
2266 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev->getData();
2268 // Size the DIE attribute values.
2269 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2270 // Size attribute value.
2271 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
2274 // Size the DIE children if any.
2275 if (!Children.empty()) {
2276 assert(Abbrev->getChildrenFlag() == DW_CHILDREN_yes &&
2277 "Children flag not set");
2279 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
2280 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
2283 // End of children marker.
2284 Offset += sizeof(int8_t);
2287 Die->setSize(Offset - Die->getOffset());
2291 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
2293 void SizeAndOffsets() {
2294 // Process base compile unit.
2295 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2296 CE = DW_CUs.end(); CI != CE; ++CI) {
2297 CompileUnit *Unit = CI->second;
2298 // Compute size of compile unit header
2299 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
2300 sizeof(int16_t) + // DWARF version number
2301 sizeof(int32_t) + // Offset Into Abbrev. Section
2302 sizeof(int8_t); // Pointer Size (in bytes)
2303 SizeAndOffsetDie(Unit->getDie(), Offset, true);
2307 /// EmitDebugInfo - Emit the debug info section.
2309 void EmitDebugInfo() {
2310 // Start debug info section.
2311 Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
2313 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2314 CE = DW_CUs.end(); CI != CE; ++CI) {
2315 CompileUnit *Unit = CI->second;
2316 DIE *Die = Unit->getDie();
2317 // Emit the compile units header.
2318 EmitLabel("info_begin", Unit->getID());
2319 // Emit size of content not including length itself
2320 unsigned ContentSize = Die->getSize() +
2321 sizeof(int16_t) + // DWARF version number
2322 sizeof(int32_t) + // Offset Into Abbrev. Section
2323 sizeof(int8_t) + // Pointer Size (in bytes)
2324 sizeof(int32_t); // FIXME - extra pad for gdb bug.
2326 Asm->EmitInt32(ContentSize); Asm->EOL("Length of Compilation Unit Info");
2327 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2328 EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true, false);
2329 Asm->EOL("Offset Into Abbrev. Section");
2330 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Address Size (in bytes)");
2333 // FIXME - extra padding for gdb bug.
2334 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2335 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2336 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2337 Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
2338 EmitLabel("info_end", Unit->getID());
2344 /// EmitAbbreviations - Emit the abbreviation section.
2346 void EmitAbbreviations() const {
2347 // Check to see if it is worth the effort.
2348 if (!Abbreviations.empty()) {
2349 // Start the debug abbrev section.
2350 Asm->SwitchToDataSection(TAI->getDwarfAbbrevSection());
2352 EmitLabel("abbrev_begin", 0);
2354 // For each abbrevation.
2355 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
2356 // Get abbreviation data
2357 const DIEAbbrev *Abbrev = Abbreviations[i];
2359 // Emit the abbrevations code (base 1 index.)
2360 Asm->EmitULEB128Bytes(Abbrev->getNumber());
2361 Asm->EOL("Abbreviation Code");
2363 // Emit the abbreviations data.
2364 Abbrev->Emit(*this);
2369 // Mark end of abbreviations.
2370 Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
2372 EmitLabel("abbrev_end", 0);
2378 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
2379 /// the line matrix.
2381 void EmitEndOfLineMatrix(unsigned SectionEnd) {
2382 // Define last address of section.
2383 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2384 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2385 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2386 EmitReference("section_end", SectionEnd); Asm->EOL("Section end label");
2388 // Mark end of matrix.
2389 Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
2390 Asm->EmitULEB128Bytes(1); Asm->EOL();
2391 Asm->EmitInt8(1); Asm->EOL();
2394 /// EmitDebugLines - Emit source line information.
2396 void EmitDebugLines() {
2397 // If the target is using .loc/.file, the assembler will be emitting the
2398 // .debug_line table automatically.
2399 if (TAI->hasDotLocAndDotFile())
2402 // Minimum line delta, thus ranging from -10..(255-10).
2403 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2404 // Maximum line delta, thus ranging from -10..(255-10).
2405 const int MaxLineDelta = 255 + MinLineDelta;
2407 // Start the dwarf line section.
2408 Asm->SwitchToDataSection(TAI->getDwarfLineSection());
2410 // Construct the section header.
2412 EmitDifference("line_end", 0, "line_begin", 0, true);
2413 Asm->EOL("Length of Source Line Info");
2414 EmitLabel("line_begin", 0);
2416 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
2418 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0, true);
2419 Asm->EOL("Prolog Length");
2420 EmitLabel("line_prolog_begin", 0);
2422 Asm->EmitInt8(1); Asm->EOL("Minimum Instruction Length");
2424 Asm->EmitInt8(1); Asm->EOL("Default is_stmt_start flag");
2426 Asm->EmitInt8(MinLineDelta); Asm->EOL("Line Base Value (Special Opcodes)");
2428 Asm->EmitInt8(MaxLineDelta); Asm->EOL("Line Range Value (Special Opcodes)");
2430 Asm->EmitInt8(-MinLineDelta); Asm->EOL("Special Opcode Base");
2432 // Line number standard opcode encodings argument count
2433 Asm->EmitInt8(0); Asm->EOL("DW_LNS_copy arg count");
2434 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_pc arg count");
2435 Asm->EmitInt8(1); Asm->EOL("DW_LNS_advance_line arg count");
2436 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_file arg count");
2437 Asm->EmitInt8(1); Asm->EOL("DW_LNS_set_column arg count");
2438 Asm->EmitInt8(0); Asm->EOL("DW_LNS_negate_stmt arg count");
2439 Asm->EmitInt8(0); Asm->EOL("DW_LNS_set_basic_block arg count");
2440 Asm->EmitInt8(0); Asm->EOL("DW_LNS_const_add_pc arg count");
2441 Asm->EmitInt8(1); Asm->EOL("DW_LNS_fixed_advance_pc arg count");
2443 // Emit directories.
2444 for (unsigned DirectoryID = 1, NDID = Directories.size();
2445 DirectoryID <= NDID; ++DirectoryID) {
2446 Asm->EmitString(Directories[DirectoryID]); Asm->EOL("Directory");
2448 Asm->EmitInt8(0); Asm->EOL("End of directories");
2451 for (unsigned SourceID = 1, NSID = SrcFiles.size();
2452 SourceID <= NSID; ++SourceID) {
2453 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
2454 Asm->EmitString(SourceFile.getName());
2456 Asm->EmitULEB128Bytes(SourceFile.getDirectoryID());
2457 Asm->EOL("Directory #");
2458 Asm->EmitULEB128Bytes(0);
2459 Asm->EOL("Mod date");
2460 Asm->EmitULEB128Bytes(0);
2461 Asm->EOL("File size");
2463 Asm->EmitInt8(0); Asm->EOL("End of files");
2465 EmitLabel("line_prolog_end", 0);
2467 // A sequence for each text section.
2468 unsigned SecSrcLinesSize = SectionSourceLines.size();
2470 for (unsigned j = 0; j < SecSrcLinesSize; ++j) {
2471 // Isolate current sections line info.
2472 const std::vector<SrcLineInfo> &LineInfos = SectionSourceLines[j];
2475 const Section* S = SectionMap[j + 1];
2476 Asm->EOL(std::string("Section ") + S->getName());
2480 // Dwarf assumes we start with first line of first source file.
2481 unsigned Source = 1;
2484 // Construct rows of the address, source, line, column matrix.
2485 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2486 const SrcLineInfo &LineInfo = LineInfos[i];
2487 unsigned LabelID = MMI->MappedLabel(LineInfo.getLabelID());
2488 if (!LabelID) continue;
2490 unsigned SourceID = LineInfo.getSourceID();
2491 const SrcFileInfo &SourceFile = SrcFiles[SourceID];
2492 unsigned DirectoryID = SourceFile.getDirectoryID();
2494 Asm->EOL(Directories[DirectoryID]
2495 + SourceFile.getName()
2497 + utostr_32(LineInfo.getLine()));
2501 // Define the line address.
2502 Asm->EmitInt8(0); Asm->EOL("Extended Op");
2503 Asm->EmitInt8(TD->getPointerSize() + 1); Asm->EOL("Op size");
2504 Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
2505 EmitReference("label", LabelID); Asm->EOL("Location label");
2507 // If change of source, then switch to the new source.
2508 if (Source != LineInfo.getSourceID()) {
2509 Source = LineInfo.getSourceID();
2510 Asm->EmitInt8(DW_LNS_set_file); Asm->EOL("DW_LNS_set_file");
2511 Asm->EmitULEB128Bytes(Source); Asm->EOL("New Source");
2514 // If change of line.
2515 if (Line != LineInfo.getLine()) {
2516 // Determine offset.
2517 int Offset = LineInfo.getLine() - Line;
2518 int Delta = Offset - MinLineDelta;
2521 Line = LineInfo.getLine();
2523 // If delta is small enough and in range...
2524 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2525 // ... then use fast opcode.
2526 Asm->EmitInt8(Delta - MinLineDelta); Asm->EOL("Line Delta");
2528 // ... otherwise use long hand.
2529 Asm->EmitInt8(DW_LNS_advance_line); Asm->EOL("DW_LNS_advance_line");
2530 Asm->EmitSLEB128Bytes(Offset); Asm->EOL("Line Offset");
2531 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2534 // Copy the previous row (different address or source)
2535 Asm->EmitInt8(DW_LNS_copy); Asm->EOL("DW_LNS_copy");
2539 EmitEndOfLineMatrix(j + 1);
2542 if (SecSrcLinesSize == 0)
2543 // Because we're emitting a debug_line section, we still need a line
2544 // table. The linker and friends expect it to exist. If there's nothing to
2545 // put into it, emit an empty table.
2546 EmitEndOfLineMatrix(1);
2548 EmitLabel("line_end", 0);
2553 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
2555 void EmitCommonDebugFrame() {
2556 if (!TAI->doesDwarfRequireFrameSection())
2560 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2561 TargetFrameInfo::StackGrowsUp ?
2562 TD->getPointerSize() : -TD->getPointerSize();
2564 // Start the dwarf frame section.
2565 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2567 EmitLabel("debug_frame_common", 0);
2568 EmitDifference("debug_frame_common_end", 0,
2569 "debug_frame_common_begin", 0, true);
2570 Asm->EOL("Length of Common Information Entry");
2572 EmitLabel("debug_frame_common_begin", 0);
2573 Asm->EmitInt32((int)DW_CIE_ID);
2574 Asm->EOL("CIE Identifier Tag");
2575 Asm->EmitInt8(DW_CIE_VERSION);
2576 Asm->EOL("CIE Version");
2577 Asm->EmitString("");
2578 Asm->EOL("CIE Augmentation");
2579 Asm->EmitULEB128Bytes(1);
2580 Asm->EOL("CIE Code Alignment Factor");
2581 Asm->EmitSLEB128Bytes(stackGrowth);
2582 Asm->EOL("CIE Data Alignment Factor");
2583 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), false));
2584 Asm->EOL("CIE RA Column");
2586 std::vector<MachineMove> Moves;
2587 RI->getInitialFrameState(Moves);
2589 EmitFrameMoves(NULL, 0, Moves, false);
2591 Asm->EmitAlignment(2, 0, 0, false);
2592 EmitLabel("debug_frame_common_end", 0);
2597 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2599 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo) {
2600 if (!TAI->doesDwarfRequireFrameSection())
2603 // Start the dwarf frame section.
2604 Asm->SwitchToDataSection(TAI->getDwarfFrameSection());
2606 EmitDifference("debug_frame_end", DebugFrameInfo.Number,
2607 "debug_frame_begin", DebugFrameInfo.Number, true);
2608 Asm->EOL("Length of Frame Information Entry");
2610 EmitLabel("debug_frame_begin", DebugFrameInfo.Number);
2612 EmitSectionOffset("debug_frame_common", "section_debug_frame",
2614 Asm->EOL("FDE CIE offset");
2616 EmitReference("func_begin", DebugFrameInfo.Number);
2617 Asm->EOL("FDE initial location");
2618 EmitDifference("func_end", DebugFrameInfo.Number,
2619 "func_begin", DebugFrameInfo.Number);
2620 Asm->EOL("FDE address range");
2622 EmitFrameMoves("func_begin", DebugFrameInfo.Number, DebugFrameInfo.Moves,
2625 Asm->EmitAlignment(2, 0, 0, false);
2626 EmitLabel("debug_frame_end", DebugFrameInfo.Number);
2631 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2633 void EmitDebugPubNames() {
2634 // Start the dwarf pubnames section.
2635 Asm->SwitchToDataSection(TAI->getDwarfPubNamesSection());
2637 for (DenseMap<Value *, CompileUnit *>::iterator CI = DW_CUs.begin(),
2638 CE = DW_CUs.end(); CI != CE; ++CI) {
2639 CompileUnit *Unit = CI->second;
2641 EmitDifference("pubnames_end", Unit->getID(),
2642 "pubnames_begin", Unit->getID(), true);
2643 Asm->EOL("Length of Public Names Info");
2645 EmitLabel("pubnames_begin", Unit->getID());
2647 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
2649 EmitSectionOffset("info_begin", "section_info",
2650 Unit->getID(), 0, true, false);
2651 Asm->EOL("Offset of Compilation Unit Info");
2653 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),
2655 Asm->EOL("Compilation Unit Length");
2657 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2659 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2662 const std::string &Name = GI->first;
2663 DIE * Entity = GI->second;
2665 Asm->EmitInt32(Entity->getOffset()); Asm->EOL("DIE offset");
2666 Asm->EmitString(Name); Asm->EOL("External Name");
2669 Asm->EmitInt32(0); Asm->EOL("End Mark");
2670 EmitLabel("pubnames_end", Unit->getID());
2676 /// EmitDebugStr - Emit visible names into a debug str section.
2678 void EmitDebugStr() {
2679 // Check to see if it is worth the effort.
2680 if (!StringPool.empty()) {
2681 // Start the dwarf str section.
2682 Asm->SwitchToDataSection(TAI->getDwarfStrSection());
2684 // For each of strings in the string pool.
2685 for (unsigned StringID = 1, N = StringPool.size();
2686 StringID <= N; ++StringID) {
2687 // Emit a label for reference from debug information entries.
2688 EmitLabel("string", StringID);
2689 // Emit the string itself.
2690 const std::string &String = StringPool[StringID];
2691 Asm->EmitString(String); Asm->EOL();
2698 /// EmitDebugLoc - Emit visible names into a debug loc section.
2700 void EmitDebugLoc() {
2701 // Start the dwarf loc section.
2702 Asm->SwitchToDataSection(TAI->getDwarfLocSection());
2707 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2709 void EmitDebugARanges() {
2710 // Start the dwarf aranges section.
2711 Asm->SwitchToDataSection(TAI->getDwarfARangesSection());
2715 CompileUnit *Unit = GetBaseCompileUnit();
2717 // Don't include size of length
2718 Asm->EmitInt32(0x1c); Asm->EOL("Length of Address Ranges Info");
2720 Asm->EmitInt16(DWARF_VERSION); Asm->EOL("Dwarf Version");
2722 EmitReference("info_begin", Unit->getID());
2723 Asm->EOL("Offset of Compilation Unit Info");
2725 Asm->EmitInt8(TD->getPointerSize()); Asm->EOL("Size of Address");
2727 Asm->EmitInt8(0); Asm->EOL("Size of Segment Descriptor");
2729 Asm->EmitInt16(0); Asm->EOL("Pad (1)");
2730 Asm->EmitInt16(0); Asm->EOL("Pad (2)");
2733 EmitReference("text_begin", 0); Asm->EOL("Address");
2734 EmitDifference("text_end", 0, "text_begin", 0, true); Asm->EOL("Length");
2736 Asm->EmitInt32(0); Asm->EOL("EOM (1)");
2737 Asm->EmitInt32(0); Asm->EOL("EOM (2)");
2743 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2745 void EmitDebugRanges() {
2746 // Start the dwarf ranges section.
2747 Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
2752 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2754 void EmitDebugMacInfo() {
2755 // Start the dwarf macinfo section.
2756 Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
2761 /// ConstructCompileUnits - Create a compile unit DIEs.
2762 void ConstructCompileUnits() {
2763 std::string CUName = "llvm.dbg.compile_units";
2764 std::vector<GlobalVariable*> Result;
2765 getGlobalVariablesUsing(*M, CUName, Result);
2766 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2767 RE = Result.end(); RI != RE; ++RI) {
2768 DICompileUnit DIUnit(*RI);
2769 unsigned ID = RecordSource(DIUnit.getDirectory(),
2770 DIUnit.getFilename());
2772 DIE *Die = new DIE(DW_TAG_compile_unit);
2773 AddSectionOffset(Die, DW_AT_stmt_list, DW_FORM_data4,
2774 DWLabel("section_line", 0), DWLabel("section_line", 0),
2776 AddString(Die, DW_AT_producer, DW_FORM_string, DIUnit.getProducer());
2777 AddUInt(Die, DW_AT_language, DW_FORM_data1, DIUnit.getLanguage());
2778 AddString(Die, DW_AT_name, DW_FORM_string, DIUnit.getFilename());
2779 if (!DIUnit.getDirectory().empty())
2780 AddString(Die, DW_AT_comp_dir, DW_FORM_string, DIUnit.getDirectory());
2782 CompileUnit *Unit = new CompileUnit(ID, Die);
2783 DW_CUs[DIUnit.getGV()] = Unit;
2787 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
2788 /// visible global variables.
2789 void ConstructGlobalVariableDIEs() {
2790 std::string GVName = "llvm.dbg.global_variables";
2791 std::vector<GlobalVariable*> Result;
2792 getGlobalVariablesUsing(*M, GVName, Result);
2793 for (std::vector<GlobalVariable *>::iterator GVI = Result.begin(),
2794 GVE = Result.end(); GVI != GVE; ++GVI) {
2795 DIGlobalVariable DI_GV(*GVI);
2796 CompileUnit *DW_Unit = FindCompileUnit(DI_GV.getCompileUnit());
2798 // Check for pre-existence.
2799 DIE *&Slot = DW_Unit->getDieMapSlotFor(DI_GV.getGV());
2802 DIE *VariableDie = CreateGlobalVariableDIE(DW_Unit, DI_GV);
2805 DIEBlock *Block = new DIEBlock();
2806 AddUInt(Block, 0, DW_FORM_data1, DW_OP_addr);
2807 AddObjectLabel(Block, 0, DW_FORM_udata,
2808 Asm->getGlobalLinkName(DI_GV.getGlobal()));
2809 AddBlock(VariableDie, DW_AT_location, 0, Block);
2814 //Add to context owner.
2815 DW_Unit->getDie()->AddChild(VariableDie);
2817 //Expose as global. FIXME - need to check external flag.
2818 DW_Unit->AddGlobal(DI_GV.getName(), VariableDie);
2822 /// ConstructSubprograms - Create DIEs for each of the externally visible
2824 void ConstructSubprograms() {
2826 std::string SPName = "llvm.dbg.subprograms";
2827 std::vector<GlobalVariable*> Result;
2828 getGlobalVariablesUsing(*M, SPName, Result);
2829 for (std::vector<GlobalVariable *>::iterator RI = Result.begin(),
2830 RE = Result.end(); RI != RE; ++RI) {
2832 DISubprogram SP(*RI);
2833 CompileUnit *Unit = FindCompileUnit(SP.getCompileUnit());
2835 // Check for pre-existence.
2836 DIE *&Slot = Unit->getDieMapSlotFor(SP.getGV());
2839 DIE *SubprogramDie = CreateSubprogramDIE(Unit, SP);
2842 Slot = SubprogramDie;
2843 //Add to context owner.
2844 Unit->getDie()->AddChild(SubprogramDie);
2846 Unit->AddGlobal(SP.getName(), SubprogramDie);
2851 //===--------------------------------------------------------------------===//
2852 // Main entry points.
2854 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
2855 : Dwarf(OS, A, T, "dbg")
2856 , AbbreviationsSet(InitAbbreviationsSetSize)
2858 , ValuesSet(InitValuesSetSize)
2862 , SectionSourceLines()
2865 , RootDbgScope(NULL)
2868 virtual ~DwarfDebug() {
2869 for (unsigned j = 0, M = Values.size(); j < M; ++j)
2873 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
2874 /// This is inovked by the target AsmPrinter.
2875 void SetDebugInfo(MachineModuleInfo *mmi) {
2877 // Create all the compile unit DIEs.
2878 ConstructCompileUnits();
2885 MMI->setDebugInfoAvailability(true);
2887 // Create DIEs for each of the externally visible global variables.
2888 ConstructGlobalVariableDIEs();
2890 // Create DIEs for each of the externally visible subprograms.
2891 ConstructSubprograms();
2893 // Prime section data.
2894 SectionMap.insert(TAI->getTextSection());
2896 // Print out .file directives to specify files for .loc directives. These
2897 // are printed out early so that they precede any .loc directives.
2898 if (TAI->hasDotLocAndDotFile()) {
2899 for (unsigned i = 1, e = SrcFiles.size(); i <= e; ++i) {
2900 sys::Path FullPath(Directories[SrcFiles[i].getDirectoryID()]);
2901 bool AppendOk = FullPath.appendComponent(SrcFiles[i].getName());
2902 assert(AppendOk && "Could not append filename to directory!");
2904 Asm->EmitFile(i, FullPath.toString());
2909 // Emit initial sections
2913 /// BeginModule - Emit all Dwarf sections that should come prior to the
2915 void BeginModule(Module *M) {
2919 /// EndModule - Emit all Dwarf sections that should come after the content.
2922 if (!ShouldEmitDwarf()) return;
2924 // Standard sections final addresses.
2925 Asm->SwitchToSection(TAI->getTextSection());
2926 EmitLabel("text_end", 0);
2927 Asm->SwitchToSection(TAI->getDataSection());
2928 EmitLabel("data_end", 0);
2930 // End text sections.
2931 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2932 Asm->SwitchToSection(SectionMap[i]);
2933 EmitLabel("section_end", i);
2936 // Emit common frame information.
2937 EmitCommonDebugFrame();
2939 // Emit function debug frame information
2940 for (std::vector<FunctionDebugFrameInfo>::iterator I = DebugFrames.begin(),
2941 E = DebugFrames.end(); I != E; ++I)
2942 EmitFunctionDebugFrame(*I);
2944 // Compute DIE offsets and sizes.
2947 // Emit all the DIEs into a debug info section
2950 // Corresponding abbreviations into a abbrev section.
2951 EmitAbbreviations();
2953 // Emit source line correspondence into a debug line section.
2956 // Emit info into a debug pubnames section.
2957 EmitDebugPubNames();
2959 // Emit info into a debug str section.
2962 // Emit info into a debug loc section.
2965 // Emit info into a debug aranges section.
2968 // Emit info into a debug ranges section.
2971 // Emit info into a debug macinfo section.
2975 /// BeginFunction - Gather pre-function debug information. Assumes being
2976 /// emitted immediately after the function entry point.
2977 void BeginFunction(MachineFunction *MF) {
2980 if (!ShouldEmitDwarf()) return;
2982 // Begin accumulating function debug information.
2983 MMI->BeginFunction(MF);
2985 // Assumes in correct section after the entry point.
2986 EmitLabel("func_begin", ++SubprogramCount);
2988 // Emit label for the implicitly defined dbg.stoppoint at the start of
2990 if (!Lines.empty()) {
2991 const SrcLineInfo &LineInfo = Lines[0];
2992 Asm->printLabel(LineInfo.getLabelID());
2996 /// EndFunction - Gather and emit post-function debug information.
2998 void EndFunction(MachineFunction *MF) {
2999 if (!ShouldEmitDwarf()) return;
3001 // Define end label for subprogram.
3002 EmitLabel("func_end", SubprogramCount);
3004 // Get function line info.
3005 if (!Lines.empty()) {
3006 // Get section line info.
3007 unsigned ID = SectionMap.insert(Asm->CurrentSection_);
3008 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
3009 std::vector<SrcLineInfo> &SectionLineInfos = SectionSourceLines[ID-1];
3010 // Append the function info to section info.
3011 SectionLineInfos.insert(SectionLineInfos.end(),
3012 Lines.begin(), Lines.end());
3015 // Construct scopes for subprogram.
3017 ConstructRootDbgScope(RootDbgScope);
3019 // FIXME: This is wrong. We are essentially getting past a problem with
3020 // debug information not being able to handle unreachable blocks that have
3021 // debug information in them. In particular, those unreachable blocks that
3022 // have "region end" info in them. That situation results in the "root
3023 // scope" not being created. If that's the case, then emit a "default"
3024 // scope, i.e., one that encompasses the whole function. This isn't
3025 // desirable. And a better way of handling this (and all of the debugging
3026 // information) needs to be explored.
3027 ConstructDefaultDbgScope(MF);
3029 DebugFrames.push_back(FunctionDebugFrameInfo(SubprogramCount,
3030 MMI->getFrameMoves()));
3034 delete RootDbgScope;
3035 DbgScopeMap.clear();
3036 RootDbgScope = NULL;
3043 /// ValidDebugInfo - Return true if V represents valid debug info value.
3044 bool ValidDebugInfo(Value *V) {
3052 GlobalVariable *GV = getGlobalVariable(V);
3056 if (GV->getLinkage() != GlobalValue::InternalLinkage
3057 && GV->getLinkage() != GlobalValue::LinkOnceLinkage)
3060 DIDescriptor DI(GV);
3061 // Check current version. Allow Version6 for now.
3062 unsigned Version = DI.getVersion();
3063 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
3066 unsigned Tag = DI.getTag();
3068 case DW_TAG_variable:
3069 assert (DIVariable(GV).Verify() && "Invalid DebugInfo value");
3071 case DW_TAG_compile_unit:
3072 assert (DICompileUnit(GV).Verify() && "Invalid DebugInfo value");
3074 case DW_TAG_subprogram:
3075 assert (DISubprogram(GV).Verify() && "Invalid DebugInfo value");
3084 /// RecordSourceLine - Records location information and associates it with a
3085 /// label. Returns a unique label ID used to generate a label and provide
3086 /// correspondence to the source line list.
3087 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col) {
3088 CompileUnit *Unit = DW_CUs[V];
3089 assert (Unit && "Unable to find CompileUnit");
3090 unsigned ID = MMI->NextLabelID();
3091 Lines.push_back(SrcLineInfo(Line, Col, Unit->getID(), ID));
3095 /// RecordSourceLine - Records location information and associates it with a
3096 /// label. Returns a unique label ID used to generate a label and provide
3097 /// correspondence to the source line list.
3098 unsigned RecordSourceLine(unsigned Line, unsigned Col, unsigned Src) {
3099 unsigned ID = MMI->NextLabelID();
3100 Lines.push_back(SrcLineInfo(Line, Col, Src, ID));
3104 unsigned getRecordSourceLineCount() {
3105 return Lines.size();
3108 /// RecordSource - Register a source file with debug info. Returns an source
3110 unsigned RecordSource(const std::string &Directory,
3111 const std::string &File) {
3112 unsigned DID = Directories.insert(Directory);
3113 return SrcFiles.insert(SrcFileInfo(DID,File));
3116 /// RecordRegionStart - Indicate the start of a region.
3118 unsigned RecordRegionStart(GlobalVariable *V) {
3119 DbgScope *Scope = getOrCreateScope(V);
3120 unsigned ID = MMI->NextLabelID();
3121 if (!Scope->getStartLabelID()) Scope->setStartLabelID(ID);
3125 /// RecordRegionEnd - Indicate the end of a region.
3127 unsigned RecordRegionEnd(GlobalVariable *V) {
3128 DbgScope *Scope = getOrCreateScope(V);
3129 unsigned ID = MMI->NextLabelID();
3130 Scope->setEndLabelID(ID);
3134 /// RecordVariable - Indicate the declaration of a local variable.
3136 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
3137 DIDescriptor Desc(GV);
3138 DbgScope *Scope = NULL;
3139 if (Desc.getTag() == DW_TAG_variable) {
3140 // GV is a global variable.
3141 DIGlobalVariable DG(GV);
3142 Scope = getOrCreateScope(DG.getContext().getGV());
3144 // or GV is a local variable.
3146 Scope = getOrCreateScope(DV.getContext().getGV());
3148 assert (Scope && "Unable to find variable' scope");
3149 DbgVariable *DV = new DbgVariable(DIVariable(GV), FrameIndex);
3150 Scope->AddVariable(DV);
3154 //===----------------------------------------------------------------------===//
3155 /// DwarfException - Emits Dwarf exception handling directives.
3157 class DwarfException : public Dwarf {
3160 struct FunctionEHFrameInfo {
3163 unsigned PersonalityIndex;
3165 bool hasLandingPads;
3166 std::vector<MachineMove> Moves;
3167 const Function * function;
3169 FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
3171 const std::vector<MachineMove> &M,
3173 FnName(FN), Number(Num), PersonalityIndex(P),
3174 hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
3177 std::vector<FunctionEHFrameInfo> EHFrames;
3179 /// shouldEmitTable - Per-function flag to indicate if EH tables should
3181 bool shouldEmitTable;
3183 /// shouldEmitMoves - Per-function flag to indicate if frame moves info
3184 /// should be emitted.
3185 bool shouldEmitMoves;
3187 /// shouldEmitTableModule - Per-module flag to indicate if EH tables
3188 /// should be emitted.
3189 bool shouldEmitTableModule;
3191 /// shouldEmitFrameModule - Per-module flag to indicate if frame moves
3192 /// should be emitted.
3193 bool shouldEmitMovesModule;
3195 /// EmitCommonEHFrame - Emit the common eh unwind frame.
3197 void EmitCommonEHFrame(const Function *Personality, unsigned Index) {
3198 // Size and sign of stack growth.
3200 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
3201 TargetFrameInfo::StackGrowsUp ?
3202 TD->getPointerSize() : -TD->getPointerSize();
3204 // Begin eh frame section.
3205 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3207 if (!TAI->doesRequireNonLocalEHFrameLabel())
3208 O << TAI->getEHGlobalPrefix();
3209 O << "EH_frame" << Index << ":\n";
3210 EmitLabel("section_eh_frame", Index);
3212 // Define base labels.
3213 EmitLabel("eh_frame_common", Index);
3215 // Define the eh frame length.
3216 EmitDifference("eh_frame_common_end", Index,
3217 "eh_frame_common_begin", Index, true);
3218 Asm->EOL("Length of Common Information Entry");
3221 EmitLabel("eh_frame_common_begin", Index);
3222 Asm->EmitInt32((int)0);
3223 Asm->EOL("CIE Identifier Tag");
3224 Asm->EmitInt8(DW_CIE_VERSION);
3225 Asm->EOL("CIE Version");
3227 // The personality presence indicates that language specific information
3228 // will show up in the eh frame.
3229 Asm->EmitString(Personality ? "zPLR" : "zR");
3230 Asm->EOL("CIE Augmentation");
3232 // Round out reader.
3233 Asm->EmitULEB128Bytes(1);
3234 Asm->EOL("CIE Code Alignment Factor");
3235 Asm->EmitSLEB128Bytes(stackGrowth);
3236 Asm->EOL("CIE Data Alignment Factor");
3237 Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
3238 Asm->EOL("CIE Return Address Column");
3240 // If there is a personality, we need to indicate the functions location.
3242 Asm->EmitULEB128Bytes(7);
3243 Asm->EOL("Augmentation Size");
3245 if (TAI->getNeedsIndirectEncoding()) {
3246 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4 | DW_EH_PE_indirect);
3247 Asm->EOL("Personality (pcrel sdata4 indirect)");
3249 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3250 Asm->EOL("Personality (pcrel sdata4)");
3253 PrintRelDirective(true);
3254 O << TAI->getPersonalityPrefix();
3255 Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
3256 O << TAI->getPersonalitySuffix();
3257 if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
3258 O << "-" << TAI->getPCSymbol();
3259 Asm->EOL("Personality");
3261 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3262 Asm->EOL("LSDA Encoding (pcrel sdata4)");
3264 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3265 Asm->EOL("FDE Encoding (pcrel sdata4)");
3267 Asm->EmitULEB128Bytes(1);
3268 Asm->EOL("Augmentation Size");
3270 Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
3271 Asm->EOL("FDE Encoding (pcrel sdata4)");
3274 // Indicate locations of general callee saved registers in frame.
3275 std::vector<MachineMove> Moves;
3276 RI->getInitialFrameState(Moves);
3277 EmitFrameMoves(NULL, 0, Moves, true);
3279 // On Darwin the linker honors the alignment of eh_frame, which means it
3280 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3281 // you get holes which confuse readers of eh_frame.
3282 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
3284 EmitLabel("eh_frame_common_end", Index);
3289 /// EmitEHFrame - Emit function exception frame information.
3291 void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
3292 Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
3294 Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
3296 // Externally visible entry into the functions eh frame info.
3297 // If the corresponding function is static, this should not be
3298 // externally visible.
3299 if (linkage != Function::InternalLinkage &&
3300 linkage != Function::PrivateLinkage) {
3301 if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
3302 O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
3305 // If corresponding function is weak definition, this should be too.
3306 if ((linkage == Function::WeakLinkage ||
3307 linkage == Function::LinkOnceLinkage) &&
3308 TAI->getWeakDefDirective())
3309 O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
3311 // If there are no calls then you can't unwind. This may mean we can
3312 // omit the EH Frame, but some environments do not handle weak absolute
3314 // If UnwindTablesMandatory is set we cannot do this optimization; the
3315 // unwind info is to be available for non-EH uses.
3316 if (!EHFrameInfo.hasCalls &&
3317 !UnwindTablesMandatory &&
3318 ((linkage != Function::WeakLinkage &&
3319 linkage != Function::LinkOnceLinkage) ||
3320 !TAI->getWeakDefDirective() ||
3321 TAI->getSupportsWeakOmittedEHFrame()))
3323 O << EHFrameInfo.FnName << " = 0\n";
3324 // This name has no connection to the function, so it might get
3325 // dead-stripped when the function is not, erroneously. Prohibit
3326 // dead-stripping unconditionally.
3327 if (const char *UsedDirective = TAI->getUsedDirective())
3328 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3330 O << EHFrameInfo.FnName << ":\n";
3333 EmitDifference("eh_frame_end", EHFrameInfo.Number,
3334 "eh_frame_begin", EHFrameInfo.Number, true);
3335 Asm->EOL("Length of Frame Information Entry");
3337 EmitLabel("eh_frame_begin", EHFrameInfo.Number);
3339 if (TAI->doesRequireNonLocalEHFrameLabel()) {
3340 PrintRelDirective(true, true);
3341 PrintLabelName("eh_frame_begin", EHFrameInfo.Number);
3343 if (!TAI->isAbsoluteEHSectionOffsets())
3344 O << "-EH_frame" << EHFrameInfo.PersonalityIndex;
3346 EmitSectionOffset("eh_frame_begin", "eh_frame_common",
3347 EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
3351 Asm->EOL("FDE CIE offset");
3353 EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
3354 Asm->EOL("FDE initial location");
3355 EmitDifference("eh_func_end", EHFrameInfo.Number,
3356 "eh_func_begin", EHFrameInfo.Number, true);
3357 Asm->EOL("FDE address range");
3359 // If there is a personality and landing pads then point to the language
3360 // specific data area in the exception table.
3361 if (EHFrameInfo.PersonalityIndex) {
3362 Asm->EmitULEB128Bytes(4);
3363 Asm->EOL("Augmentation size");
3365 if (EHFrameInfo.hasLandingPads)
3366 EmitReference("exception", EHFrameInfo.Number, true, true);
3368 Asm->EmitInt32((int)0);
3369 Asm->EOL("Language Specific Data Area");
3371 Asm->EmitULEB128Bytes(0);
3372 Asm->EOL("Augmentation size");
3375 // Indicate locations of function specific callee saved registers in
3377 EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
3380 // On Darwin the linker honors the alignment of eh_frame, which means it
3381 // must be 8-byte on 64-bit targets to match what gcc does. Otherwise
3382 // you get holes which confuse readers of eh_frame.
3383 Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
3385 EmitLabel("eh_frame_end", EHFrameInfo.Number);
3387 // If the function is marked used, this table should be also. We cannot
3388 // make the mark unconditional in this case, since retaining the table
3389 // also retains the function in this case, and there is code around
3390 // that depends on unused functions (calling undefined externals) being
3391 // dead-stripped to link correctly. Yes, there really is.
3392 if (MMI->getUsedFunctions().count(EHFrameInfo.function))
3393 if (const char *UsedDirective = TAI->getUsedDirective())
3394 O << UsedDirective << EHFrameInfo.FnName << "\n\n";
3398 /// EmitExceptionTable - Emit landing pads and actions.
3400 /// The general organization of the table is complex, but the basic concepts
3401 /// are easy. First there is a header which describes the location and
3402 /// organization of the three components that follow.
3403 /// 1. The landing pad site information describes the range of code covered
3404 /// by the try. In our case it's an accumulation of the ranges covered
3405 /// by the invokes in the try. There is also a reference to the landing
3406 /// pad that handles the exception once processed. Finally an index into
3407 /// the actions table.
3408 /// 2. The action table, in our case, is composed of pairs of type ids
3409 /// and next action offset. Starting with the action index from the
3410 /// landing pad site, each type Id is checked for a match to the current
3411 /// exception. If it matches then the exception and type id are passed
3412 /// on to the landing pad. Otherwise the next action is looked up. This
3413 /// chain is terminated with a next action of zero. If no type id is
3414 /// found the the frame is unwound and handling continues.
3415 /// 3. Type id table contains references to all the C++ typeinfo for all
3416 /// catches in the function. This tables is reversed indexed base 1.
3418 /// SharedTypeIds - How many leading type ids two landing pads have in common.
3419 static unsigned SharedTypeIds(const LandingPadInfo *L,
3420 const LandingPadInfo *R) {
3421 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3422 unsigned LSize = LIds.size(), RSize = RIds.size();
3423 unsigned MinSize = LSize < RSize ? LSize : RSize;
3426 for (; Count != MinSize; ++Count)
3427 if (LIds[Count] != RIds[Count])
3433 /// PadLT - Order landing pads lexicographically by type id.
3434 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
3435 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
3436 unsigned LSize = LIds.size(), RSize = RIds.size();
3437 unsigned MinSize = LSize < RSize ? LSize : RSize;
3439 for (unsigned i = 0; i != MinSize; ++i)
3440 if (LIds[i] != RIds[i])
3441 return LIds[i] < RIds[i];
3443 return LSize < RSize;
3447 static inline unsigned getEmptyKey() { return -1U; }
3448 static inline unsigned getTombstoneKey() { return -2U; }
3449 static unsigned getHashValue(const unsigned &Key) { return Key; }
3450 static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
3451 static bool isPod() { return true; }
3454 /// ActionEntry - Structure describing an entry in the actions table.
3455 struct ActionEntry {
3456 int ValueForTypeID; // The value to write - may not be equal to the type id.
3458 struct ActionEntry *Previous;
3461 /// PadRange - Structure holding a try-range and the associated landing pad.
3463 // The index of the landing pad.
3465 // The index of the begin and end labels in the landing pad's label lists.
3466 unsigned RangeIndex;
3469 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
3471 /// CallSiteEntry - Structure describing an entry in the call-site table.
3472 struct CallSiteEntry {
3473 // The 'try-range' is BeginLabel .. EndLabel.
3474 unsigned BeginLabel; // zero indicates the start of the function.
3475 unsigned EndLabel; // zero indicates the end of the function.
3476 // The landing pad starts at PadLabel.
3477 unsigned PadLabel; // zero indicates that there is no landing pad.
3481 void EmitExceptionTable() {
3482 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
3483 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
3484 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
3485 if (PadInfos.empty()) return;
3487 // Sort the landing pads in order of their type ids. This is used to fold
3488 // duplicate actions.
3489 SmallVector<const LandingPadInfo *, 64> LandingPads;
3490 LandingPads.reserve(PadInfos.size());
3491 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
3492 LandingPads.push_back(&PadInfos[i]);
3493 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
3495 // Negative type ids index into FilterIds, positive type ids index into
3496 // TypeInfos. The value written for a positive type id is just the type
3497 // id itself. For a negative type id, however, the value written is the
3498 // (negative) byte offset of the corresponding FilterIds entry. The byte
3499 // offset is usually equal to the type id, because the FilterIds entries
3500 // are written using a variable width encoding which outputs one byte per
3501 // entry as long as the value written is not too large, but can differ.
3502 // This kind of complication does not occur for positive type ids because
3503 // type infos are output using a fixed width encoding.
3504 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
3505 SmallVector<int, 16> FilterOffsets;
3506 FilterOffsets.reserve(FilterIds.size());
3508 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
3509 E = FilterIds.end(); I != E; ++I) {
3510 FilterOffsets.push_back(Offset);
3511 Offset -= TargetAsmInfo::getULEB128Size(*I);
3514 // Compute the actions table and gather the first action index for each
3515 // landing pad site.
3516 SmallVector<ActionEntry, 32> Actions;
3517 SmallVector<unsigned, 64> FirstActions;
3518 FirstActions.reserve(LandingPads.size());
3520 int FirstAction = 0;
3521 unsigned SizeActions = 0;
3522 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3523 const LandingPadInfo *LP = LandingPads[i];
3524 const std::vector<int> &TypeIds = LP->TypeIds;
3525 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
3526 unsigned SizeSiteActions = 0;
3528 if (NumShared < TypeIds.size()) {
3529 unsigned SizeAction = 0;
3530 ActionEntry *PrevAction = 0;
3533 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
3534 assert(Actions.size());
3535 PrevAction = &Actions.back();
3536 SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
3537 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
3538 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
3540 TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
3541 SizeAction += -PrevAction->NextAction;
3542 PrevAction = PrevAction->Previous;
3546 // Compute the actions.
3547 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
3548 int TypeID = TypeIds[I];
3549 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
3550 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
3551 unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
3553 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
3554 SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
3555 SizeSiteActions += SizeAction;
3557 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
3558 Actions.push_back(Action);
3560 PrevAction = &Actions.back();
3563 // Record the first action of the landing pad site.
3564 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
3565 } // else identical - re-use previous FirstAction
3567 FirstActions.push_back(FirstAction);
3569 // Compute this sites contribution to size.
3570 SizeActions += SizeSiteActions;
3573 // Compute the call-site table. The entry for an invoke has a try-range
3574 // containing the call, a non-zero landing pad and an appropriate action.
3575 // The entry for an ordinary call has a try-range containing the call and
3576 // zero for the landing pad and the action. Calls marked 'nounwind' have
3577 // no entry and must not be contained in the try-range of any entry - they
3578 // form gaps in the table. Entries must be ordered by try-range address.
3579 SmallVector<CallSiteEntry, 64> CallSites;
3581 RangeMapType PadMap;
3582 // Invokes and nounwind calls have entries in PadMap (due to being bracketed
3583 // by try-range labels when lowered). Ordinary calls do not, so appropriate
3584 // try-ranges for them need be deduced.
3585 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
3586 const LandingPadInfo *LandingPad = LandingPads[i];
3587 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
3588 unsigned BeginLabel = LandingPad->BeginLabels[j];
3589 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
3590 PadRange P = { i, j };
3591 PadMap[BeginLabel] = P;
3595 // The end label of the previous invoke or nounwind try-range.
3596 unsigned LastLabel = 0;
3598 // Whether there is a potentially throwing instruction (currently this means
3599 // an ordinary call) between the end of the previous try-range and now.
3600 bool SawPotentiallyThrowing = false;
3602 // Whether the last callsite entry was for an invoke.
3603 bool PreviousIsInvoke = false;
3605 // Visit all instructions in order of address.
3606 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
3608 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
3610 if (!MI->isLabel()) {
3611 SawPotentiallyThrowing |= MI->getDesc().isCall();
3615 unsigned BeginLabel = MI->getOperand(0).getImm();
3616 assert(BeginLabel && "Invalid label!");
3618 // End of the previous try-range?
3619 if (BeginLabel == LastLabel)
3620 SawPotentiallyThrowing = false;
3622 // Beginning of a new try-range?
3623 RangeMapType::iterator L = PadMap.find(BeginLabel);
3624 if (L == PadMap.end())
3625 // Nope, it was just some random label.
3628 PadRange P = L->second;
3629 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
3631 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
3632 "Inconsistent landing pad map!");
3634 // If some instruction between the previous try-range and this one may
3635 // throw, create a call-site entry with no landing pad for the region
3636 // between the try-ranges.
3637 if (SawPotentiallyThrowing) {
3638 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
3639 CallSites.push_back(Site);
3640 PreviousIsInvoke = false;
3643 LastLabel = LandingPad->EndLabels[P.RangeIndex];
3644 assert(BeginLabel && LastLabel && "Invalid landing pad!");
3646 if (LandingPad->LandingPadLabel) {
3647 // This try-range is for an invoke.
3648 CallSiteEntry Site = {BeginLabel, LastLabel,
3649 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
3651 // Try to merge with the previous call-site.
3652 if (PreviousIsInvoke) {
3653 CallSiteEntry &Prev = CallSites.back();
3654 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
3655 // Extend the range of the previous entry.
3656 Prev.EndLabel = Site.EndLabel;
3661 // Otherwise, create a new call-site.
3662 CallSites.push_back(Site);
3663 PreviousIsInvoke = true;
3666 PreviousIsInvoke = false;
3670 // If some instruction between the previous try-range and the end of the
3671 // function may throw, create a call-site entry with no landing pad for the
3672 // region following the try-range.
3673 if (SawPotentiallyThrowing) {
3674 CallSiteEntry Site = {LastLabel, 0, 0, 0};
3675 CallSites.push_back(Site);
3681 const unsigned SiteStartSize = sizeof(int32_t); // DW_EH_PE_udata4
3682 const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
3683 const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
3684 unsigned SizeSites = CallSites.size() * (SiteStartSize +
3687 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
3688 SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
3691 const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
3692 unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
3694 unsigned TypeOffset = sizeof(int8_t) + // Call site format
3695 TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
3696 SizeSites + SizeActions + SizeTypes;
3698 unsigned TotalSize = sizeof(int8_t) + // LPStart format
3699 sizeof(int8_t) + // TType format
3700 TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
3703 unsigned SizeAlign = (4 - TotalSize) & 3;
3705 // Begin the exception table.
3706 Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
3707 Asm->EmitAlignment(2, 0, 0, false);
3708 O << "GCC_except_table" << SubprogramCount << ":\n";
3709 for (unsigned i = 0; i != SizeAlign; ++i) {
3711 Asm->EOL("Padding");
3713 EmitLabel("exception", SubprogramCount);
3716 Asm->EmitInt8(DW_EH_PE_omit);
3717 Asm->EOL("LPStart format (DW_EH_PE_omit)");
3718 Asm->EmitInt8(DW_EH_PE_absptr);
3719 Asm->EOL("TType format (DW_EH_PE_absptr)");
3720 Asm->EmitULEB128Bytes(TypeOffset);
3721 Asm->EOL("TType base offset");
3722 Asm->EmitInt8(DW_EH_PE_udata4);
3723 Asm->EOL("Call site format (DW_EH_PE_udata4)");
3724 Asm->EmitULEB128Bytes(SizeSites);
3725 Asm->EOL("Call-site table length");
3727 // Emit the landing pad site information.
3728 for (unsigned i = 0; i < CallSites.size(); ++i) {
3729 CallSiteEntry &S = CallSites[i];
3730 const char *BeginTag;
3731 unsigned BeginNumber;
3733 if (!S.BeginLabel) {
3734 BeginTag = "eh_func_begin";
3735 BeginNumber = SubprogramCount;
3738 BeginNumber = S.BeginLabel;
3741 EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
3743 Asm->EOL("Region start");
3746 EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
3749 EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
3751 Asm->EOL("Region length");
3756 EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
3758 Asm->EOL("Landing pad");
3760 Asm->EmitULEB128Bytes(S.Action);
3764 // Emit the actions.
3765 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
3766 ActionEntry &Action = Actions[I];
3768 Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
3769 Asm->EOL("TypeInfo index");
3770 Asm->EmitSLEB128Bytes(Action.NextAction);
3771 Asm->EOL("Next action");
3774 // Emit the type ids.
3775 for (unsigned M = TypeInfos.size(); M; --M) {
3776 GlobalVariable *GV = TypeInfos[M - 1];
3778 PrintRelDirective();
3781 O << Asm->getGlobalLinkName(GV);
3785 Asm->EOL("TypeInfo");
3788 // Emit the filter typeids.
3789 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
3790 unsigned TypeID = FilterIds[j];
3791 Asm->EmitULEB128Bytes(TypeID);
3792 Asm->EOL("Filter TypeInfo index");
3795 Asm->EmitAlignment(2, 0, 0, false);
3799 //===--------------------------------------------------------------------===//
3800 // Main entry points.
3802 DwarfException(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
3803 : Dwarf(OS, A, T, "eh")
3804 , shouldEmitTable(false)
3805 , shouldEmitMoves(false)
3806 , shouldEmitTableModule(false)
3807 , shouldEmitMovesModule(false)
3810 virtual ~DwarfException() {}
3812 /// SetModuleInfo - Set machine module information when it's known that pass
3813 /// manager has created it. Set by the target AsmPrinter.
3814 void SetModuleInfo(MachineModuleInfo *mmi) {
3818 /// BeginModule - Emit all exception information that should come prior to the
3820 void BeginModule(Module *M) {
3824 /// EndModule - Emit all exception information that should come after the
3827 if (shouldEmitMovesModule || shouldEmitTableModule) {
3828 const std::vector<Function *> Personalities = MMI->getPersonalities();
3829 for (unsigned i =0; i < Personalities.size(); ++i)
3830 EmitCommonEHFrame(Personalities[i], i);
3832 for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
3833 E = EHFrames.end(); I != E; ++I)
3838 /// BeginFunction - Gather pre-function exception information. Assumes being
3839 /// emitted immediately after the function entry point.
3840 void BeginFunction(MachineFunction *MF) {
3842 shouldEmitTable = shouldEmitMoves = false;
3843 if (MMI && TAI->doesSupportExceptionHandling()) {
3845 // Map all labels and get rid of any dead landing pads.
3846 MMI->TidyLandingPads();
3847 // If any landing pads survive, we need an EH table.
3848 if (MMI->getLandingPads().size())
3849 shouldEmitTable = true;
3851 // See if we need frame move info.
3852 if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
3853 shouldEmitMoves = true;
3855 if (shouldEmitMoves || shouldEmitTable)
3856 // Assumes in correct section after the entry point.
3857 EmitLabel("eh_func_begin", ++SubprogramCount);
3859 shouldEmitTableModule |= shouldEmitTable;
3860 shouldEmitMovesModule |= shouldEmitMoves;
3863 /// EndFunction - Gather and emit post-function exception information.
3865 void EndFunction() {
3866 if (shouldEmitMoves || shouldEmitTable) {
3867 EmitLabel("eh_func_end", SubprogramCount);
3868 EmitExceptionTable();
3870 // Save EH frame information
3872 push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
3874 MMI->getPersonalityIndex(),
3875 MF->getFrameInfo()->hasCalls(),
3876 !MMI->getLandingPads().empty(),
3877 MMI->getFrameMoves(),
3878 MF->getFunction()));
3883 } // End of namespace llvm
3885 //===----------------------------------------------------------------------===//
3887 /// Emit - Print the abbreviation using the specified Dwarf writer.
3889 void DIEAbbrev::Emit(const DwarfDebug &DD) const {
3890 // Emit its Dwarf tag type.
3891 DD.getAsm()->EmitULEB128Bytes(Tag);
3892 DD.getAsm()->EOL(TagString(Tag));
3894 // Emit whether it has children DIEs.
3895 DD.getAsm()->EmitULEB128Bytes(ChildrenFlag);
3896 DD.getAsm()->EOL(ChildrenString(ChildrenFlag));
3898 // For each attribute description.
3899 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3900 const DIEAbbrevData &AttrData = Data[i];
3902 // Emit attribute type.
3903 DD.getAsm()->EmitULEB128Bytes(AttrData.getAttribute());
3904 DD.getAsm()->EOL(AttributeString(AttrData.getAttribute()));
3907 DD.getAsm()->EmitULEB128Bytes(AttrData.getForm());
3908 DD.getAsm()->EOL(FormEncodingString(AttrData.getForm()));
3911 // Mark end of abbreviation.
3912 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(1)");
3913 DD.getAsm()->EmitULEB128Bytes(0); DD.getAsm()->EOL("EOM(2)");
3917 void DIEAbbrev::print(std::ostream &O) {
3918 O << "Abbreviation @"
3919 << std::hex << (intptr_t)this << std::dec
3923 << ChildrenString(ChildrenFlag)
3926 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
3928 << AttributeString(Data[i].getAttribute())
3930 << FormEncodingString(Data[i].getForm())
3934 void DIEAbbrev::dump() { print(cerr); }
3937 //===----------------------------------------------------------------------===//
3940 void DIEValue::dump() {
3945 //===----------------------------------------------------------------------===//
3947 /// EmitValue - Emit integer of appropriate size.
3949 void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
3951 case DW_FORM_flag: // Fall thru
3952 case DW_FORM_ref1: // Fall thru
3953 case DW_FORM_data1: DD.getAsm()->EmitInt8(Integer); break;
3954 case DW_FORM_ref2: // Fall thru
3955 case DW_FORM_data2: DD.getAsm()->EmitInt16(Integer); break;
3956 case DW_FORM_ref4: // Fall thru
3957 case DW_FORM_data4: DD.getAsm()->EmitInt32(Integer); break;
3958 case DW_FORM_ref8: // Fall thru
3959 case DW_FORM_data8: DD.getAsm()->EmitInt64(Integer); break;
3960 case DW_FORM_udata: DD.getAsm()->EmitULEB128Bytes(Integer); break;
3961 case DW_FORM_sdata: DD.getAsm()->EmitSLEB128Bytes(Integer); break;
3962 default: assert(0 && "DIE Value form not supported yet"); break;
3966 /// SizeOf - Determine size of integer value in bytes.
3968 unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
3970 case DW_FORM_flag: // Fall thru
3971 case DW_FORM_ref1: // Fall thru
3972 case DW_FORM_data1: return sizeof(int8_t);
3973 case DW_FORM_ref2: // Fall thru
3974 case DW_FORM_data2: return sizeof(int16_t);
3975 case DW_FORM_ref4: // Fall thru
3976 case DW_FORM_data4: return sizeof(int32_t);
3977 case DW_FORM_ref8: // Fall thru
3978 case DW_FORM_data8: return sizeof(int64_t);
3979 case DW_FORM_udata: return TargetAsmInfo::getULEB128Size(Integer);
3980 case DW_FORM_sdata: return TargetAsmInfo::getSLEB128Size(Integer);
3981 default: assert(0 && "DIE Value form not supported yet"); break;
3986 //===----------------------------------------------------------------------===//
3988 /// EmitValue - Emit string value.
3990 void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
3991 DD.getAsm()->EmitString(String);
3994 //===----------------------------------------------------------------------===//
3996 /// EmitValue - Emit label value.
3998 void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
3999 bool IsSmall = Form == DW_FORM_data4;
4000 DD.EmitReference(Label, false, IsSmall);
4003 /// SizeOf - Determine size of label value in bytes.
4005 unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4006 if (Form == DW_FORM_data4) return 4;
4007 return DD.getTargetData()->getPointerSize();
4010 //===----------------------------------------------------------------------===//
4012 /// EmitValue - Emit label value.
4014 void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
4015 bool IsSmall = Form == DW_FORM_data4;
4016 DD.EmitReference(Label, false, IsSmall);
4019 /// SizeOf - Determine size of label value in bytes.
4021 unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4022 if (Form == DW_FORM_data4) return 4;
4023 return DD.getTargetData()->getPointerSize();
4026 //===----------------------------------------------------------------------===//
4028 /// EmitValue - Emit delta value.
4030 void DIESectionOffset::EmitValue(DwarfDebug &DD, unsigned Form) {
4031 bool IsSmall = Form == DW_FORM_data4;
4032 DD.EmitSectionOffset(Label.Tag, Section.Tag,
4033 Label.Number, Section.Number, IsSmall, IsEH, UseSet);
4036 /// SizeOf - Determine size of delta value in bytes.
4038 unsigned DIESectionOffset::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4039 if (Form == DW_FORM_data4) return 4;
4040 return DD.getTargetData()->getPointerSize();
4043 //===----------------------------------------------------------------------===//
4045 /// EmitValue - Emit delta value.
4047 void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
4048 bool IsSmall = Form == DW_FORM_data4;
4049 DD.EmitDifference(LabelHi, LabelLo, IsSmall);
4052 /// SizeOf - Determine size of delta value in bytes.
4054 unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4055 if (Form == DW_FORM_data4) return 4;
4056 return DD.getTargetData()->getPointerSize();
4059 //===----------------------------------------------------------------------===//
4061 /// EmitValue - Emit debug information entry offset.
4063 void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
4064 DD.getAsm()->EmitInt32(Entry->getOffset());
4067 //===----------------------------------------------------------------------===//
4069 /// ComputeSize - calculate the size of the block.
4071 unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
4073 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
4075 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4076 Size += Values[i]->SizeOf(DD, AbbrevData[i].getForm());
4082 /// EmitValue - Emit block data.
4084 void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
4086 case DW_FORM_block1: DD.getAsm()->EmitInt8(Size); break;
4087 case DW_FORM_block2: DD.getAsm()->EmitInt16(Size); break;
4088 case DW_FORM_block4: DD.getAsm()->EmitInt32(Size); break;
4089 case DW_FORM_block: DD.getAsm()->EmitULEB128Bytes(Size); break;
4090 default: assert(0 && "Improper form for block"); break;
4093 const SmallVector<DIEAbbrevData, 8> &AbbrevData = Abbrev.getData();
4095 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
4097 Values[i]->EmitValue(DD, AbbrevData[i].getForm());
4101 /// SizeOf - Determine size of block data in bytes.
4103 unsigned DIEBlock::SizeOf(const DwarfDebug &DD, unsigned Form) const {
4105 case DW_FORM_block1: return Size + sizeof(int8_t);
4106 case DW_FORM_block2: return Size + sizeof(int16_t);
4107 case DW_FORM_block4: return Size + sizeof(int32_t);
4108 case DW_FORM_block: return Size + TargetAsmInfo::getULEB128Size(Size);
4109 default: assert(0 && "Improper form for block"); break;
4114 //===----------------------------------------------------------------------===//
4115 /// DIE Implementation
4118 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4122 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
4124 void DIE::AddSiblingOffset() {
4125 DIEInteger *DI = new DIEInteger(0);
4126 Values.insert(Values.begin(), DI);
4127 Abbrev.AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
4130 /// Profile - Used to gather unique data for the value folding set.
4132 void DIE::Profile(FoldingSetNodeID &ID) {
4135 for (unsigned i = 0, N = Children.size(); i < N; ++i)
4136 ID.AddPointer(Children[i]);
4138 for (unsigned j = 0, M = Values.size(); j < M; ++j)
4139 ID.AddPointer(Values[j]);
4143 void DIE::print(std::ostream &O, unsigned IncIndent) {
4144 static unsigned IndentCount = 0;
4145 IndentCount += IncIndent;
4146 const std::string Indent(IndentCount, ' ');
4147 bool isBlock = Abbrev.getTag() == 0;
4152 << "0x" << std::hex << (intptr_t)this << std::dec
4153 << ", Offset: " << Offset
4154 << ", Size: " << Size
4158 << TagString(Abbrev.getTag())
4160 << ChildrenString(Abbrev.getChildrenFlag());
4162 O << "Size: " << Size;
4166 const SmallVector<DIEAbbrevData, 8> &Data = Abbrev.getData();
4169 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
4173 O << AttributeString(Data[i].getAttribute());
4175 O << "Blk[" << i << "]";
4178 << FormEncodingString(Data[i].getForm())
4180 Values[i]->print(O);
4185 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
4186 Children[j]->print(O, 4);
4189 if (!isBlock) O << "\n";
4190 IndentCount -= IncIndent;
4198 //===----------------------------------------------------------------------===//
4199 /// DwarfWriter Implementation
4202 DwarfWriter::DwarfWriter() : ImmutablePass(&ID), DD(NULL), DE(NULL) {
4205 DwarfWriter::~DwarfWriter() {
4210 /// BeginModule - Emit all Dwarf sections that should come prior to the
4212 void DwarfWriter::BeginModule(Module *M,
4213 MachineModuleInfo *MMI,
4214 raw_ostream &OS, AsmPrinter *A,
4215 const TargetAsmInfo *T) {
4216 DE = new DwarfException(OS, A, T);
4217 DD = new DwarfDebug(OS, A, T);
4220 DD->SetDebugInfo(MMI);
4221 DE->SetModuleInfo(MMI);
4224 /// EndModule - Emit all Dwarf sections that should come after the content.
4226 void DwarfWriter::EndModule() {
4231 /// BeginFunction - Gather pre-function debug information. Assumes being
4232 /// emitted immediately after the function entry point.
4233 void DwarfWriter::BeginFunction(MachineFunction *MF) {
4234 DE->BeginFunction(MF);
4235 DD->BeginFunction(MF);
4238 /// EndFunction - Gather and emit post-function debug information.
4240 void DwarfWriter::EndFunction(MachineFunction *MF) {
4241 DD->EndFunction(MF);
4244 if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI())
4245 // Clear function debug information.
4249 /// ValidDebugInfo - Return true if V represents valid debug info value.
4250 bool DwarfWriter::ValidDebugInfo(Value *V) {
4251 return DD && DD->ValidDebugInfo(V);
4254 /// RecordSourceLine - Records location information and associates it with a
4255 /// label. Returns a unique label ID used to generate a label and provide
4256 /// correspondence to the source line list.
4257 unsigned DwarfWriter::RecordSourceLine(unsigned Line, unsigned Col,
4259 return DD->RecordSourceLine(Line, Col, Src);
4262 /// RecordSource - Register a source file with debug info. Returns an source
4264 unsigned DwarfWriter::RecordSource(const std::string &Dir,
4265 const std::string &File) {
4266 return DD->RecordSource(Dir, File);
4269 /// RecordRegionStart - Indicate the start of a region.
4270 unsigned DwarfWriter::RecordRegionStart(GlobalVariable *V) {
4271 return DD->RecordRegionStart(V);
4274 /// RecordRegionEnd - Indicate the end of a region.
4275 unsigned DwarfWriter::RecordRegionEnd(GlobalVariable *V) {
4276 return DD->RecordRegionEnd(V);
4279 /// getRecordSourceLineCount - Count source lines.
4280 unsigned DwarfWriter::getRecordSourceLineCount() {
4281 return DD->getRecordSourceLineCount();
4284 /// RecordVariable - Indicate the declaration of a local variable.
4286 void DwarfWriter::RecordVariable(GlobalVariable *GV, unsigned FrameIndex) {
4287 DD->RecordVariable(GV, FrameIndex);