1 //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DwarfWriter.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Module.h"
18 #include "llvm/Type.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineDebugInfo.h"
21 #include "llvm/CodeGen/MachineLocation.h"
22 #include "llvm/Support/Dwarf.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Mangler.h"
25 #include "llvm/Target/MRegisterInfo.h"
26 #include "llvm/Target/TargetMachine.h"
31 using namespace llvm::dwarf;
34 DwarfVerbose("dwarf-verbose", cl::Hidden,
35 cl::desc("Add comments to Dwarf directives."));
39 //===----------------------------------------------------------------------===//
40 // Forward declarations.
44 //===----------------------------------------------------------------------===//
45 // CompileUnit - This dwarf writer support class manages information associate
46 // with a source file.
49 CompileUnitDesc *Desc; // Compile unit debug descriptor.
50 unsigned ID; // File ID for source.
51 DIE *Die; // Compile unit debug information entry.
52 std::map<std::string, DIE *> Globals; // A map of globally visible named
53 // entities for this unit.
54 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
55 // Tracks the mapping of unit level
56 // debug informaton descriptors to debug
57 // information entries.
60 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
71 CompileUnitDesc *getDesc() const { return Desc; }
72 unsigned getID() const { return ID; }
73 DIE* getDie() const { return Die; }
74 std::map<std::string, DIE *> &getGlobals() { return Globals; }
76 /// hasContent - Return true if this compile unit has something to write out.
78 bool hasContent() const;
80 /// AddGlobal - Add a new global entity to the compile unit.
82 void AddGlobal(const std::string &Name, DIE *Die);
84 /// getDieMapSlotFor - Returns the debug information entry map slot for the
85 /// specified debug descriptor.
86 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
87 return DescToDieMap[DD];
91 //===----------------------------------------------------------------------===//
92 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
93 // Dwarf abbreviation.
96 unsigned Attribute; // Dwarf attribute code.
97 unsigned Form; // Dwarf form code.
100 DIEAbbrevData(unsigned A, unsigned F)
106 unsigned getAttribute() const { return Attribute; }
107 unsigned getForm() const { return Form; }
109 /// operator== - Used by DIEAbbrev to locate entry.
111 bool operator==(const DIEAbbrevData &DAD) const {
112 return Attribute == DAD.Attribute && Form == DAD.Form;
115 /// operator!= - Used by DIEAbbrev to locate entry.
117 bool operator!=(const DIEAbbrevData &DAD) const {
118 return Attribute != DAD.Attribute || Form != DAD.Form;
121 /// operator< - Used by DIEAbbrev to locate entry.
123 bool operator<(const DIEAbbrevData &DAD) const {
124 return Attribute < DAD.Attribute ||
125 (Attribute == DAD.Attribute && Form < DAD.Form);
129 //===----------------------------------------------------------------------===//
130 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
131 // information object.
134 unsigned Tag; // Dwarf tag code.
135 unsigned ChildrenFlag; // Dwarf children flag.
136 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
140 DIEAbbrev(unsigned T, unsigned C)
148 unsigned getTag() const { return Tag; }
149 unsigned getChildrenFlag() const { return ChildrenFlag; }
150 const std::vector<DIEAbbrevData> &getData() const { return Data; }
151 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
153 /// operator== - Used by UniqueVector to locate entry.
155 bool operator==(const DIEAbbrev &DA) const;
157 /// operator< - Used by UniqueVector to locate entry.
159 bool operator<(const DIEAbbrev &DA) const;
161 /// AddAttribute - Adds another set of attribute information to the
163 void AddAttribute(unsigned Attribute, unsigned Form) {
164 Data.push_back(DIEAbbrevData(Attribute, Form));
167 /// AddFirstAttribute - Adds a set of attribute information to the front
168 /// of the abbreviation.
169 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
170 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
173 /// Emit - Print the abbreviation using the specified Dwarf writer.
175 void Emit(const DwarfWriter &DW) const;
178 void print(std::ostream &O);
183 //===----------------------------------------------------------------------===//
184 // DIEValue - A debug information entry value.
198 unsigned Type; // Type of the value
200 DIEValue(unsigned T) : Type(T) {}
201 virtual ~DIEValue() {}
203 // Implement isa/cast/dyncast.
204 static bool classof(const DIEValue *) { return true; }
206 /// EmitValue - Emit value via the Dwarf writer.
208 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
210 /// SizeOf - Return the size of a value in bytes.
212 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
215 //===----------------------------------------------------------------------===//
216 // DWInteger - An integer value DIE.
218 class DIEInteger : public DIEValue {
223 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
225 // Implement isa/cast/dyncast.
226 static bool classof(const DIEInteger *) { return true; }
227 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
229 /// BestForm - Choose the best form for integer.
231 unsigned BestForm(bool IsSigned);
233 /// EmitValue - Emit integer of appropriate size.
235 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
237 /// SizeOf - Determine size of integer value in bytes.
239 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
242 //===----------------------------------------------------------------------===//
243 // DIEString - A string value DIE.
245 struct DIEString : public DIEValue {
246 const std::string String;
248 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
250 // Implement isa/cast/dyncast.
251 static bool classof(const DIEString *) { return true; }
252 static bool classof(const DIEValue *S) { return S->Type == isString; }
254 /// EmitValue - Emit string value.
256 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
258 /// SizeOf - Determine size of string value in bytes.
260 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
263 //===----------------------------------------------------------------------===//
264 // DIEDwarfLabel - A Dwarf internal label expression DIE.
266 struct DIEDwarfLabel : public DIEValue {
269 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
271 // Implement isa/cast/dyncast.
272 static bool classof(const DIEDwarfLabel *) { return true; }
273 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
275 /// EmitValue - Emit label value.
277 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
279 /// SizeOf - Determine size of label value in bytes.
281 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
285 //===----------------------------------------------------------------------===//
286 // DIEObjectLabel - A label to an object in code or data.
288 struct DIEObjectLabel : public DIEValue {
289 const std::string Label;
291 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
293 // Implement isa/cast/dyncast.
294 static bool classof(const DIEObjectLabel *) { return true; }
295 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
297 /// EmitValue - Emit label value.
299 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
301 /// SizeOf - Determine size of label value in bytes.
303 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
306 //===----------------------------------------------------------------------===//
307 // DIEDelta - A simple label difference DIE.
309 struct DIEDelta : public DIEValue {
310 const DWLabel LabelHi;
311 const DWLabel LabelLo;
313 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
314 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
316 // Implement isa/cast/dyncast.
317 static bool classof(const DIEDelta *) { return true; }
318 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
320 /// EmitValue - Emit delta value.
322 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
324 /// SizeOf - Determine size of delta value in bytes.
326 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
329 //===----------------------------------------------------------------------===//
330 // DIEntry - A pointer to a debug information entry.
332 struct DIEntry : public DIEValue {
335 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
337 // Implement isa/cast/dyncast.
338 static bool classof(const DIEntry *) { return true; }
339 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
341 /// EmitValue - Emit debug information entry offset.
343 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
345 /// SizeOf - Determine size of debug information entry in bytes.
347 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
350 //===----------------------------------------------------------------------===//
351 // DIEBlock - A block of values. Primarily used for location expressions.
353 struct DIEBlock : public DIEValue {
354 unsigned Size; // Size in bytes excluding size header.
355 std::vector<unsigned> Forms; // Data forms.
356 std::vector<DIEValue *> Values; // Block values.
366 // Implement isa/cast/dyncast.
367 static bool classof(const DIEBlock *) { return true; }
368 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
370 /// ComputeSize - calculate the size of the block.
372 unsigned ComputeSize(DwarfWriter &DW);
374 /// BestForm - Choose the best form for data.
378 /// EmitValue - Emit block data.
380 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
382 /// SizeOf - Determine size of block data in bytes.
384 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
386 /// AddUInt - Add an unsigned integer value.
388 void AddUInt(unsigned Form, uint64_t Integer);
390 /// AddSInt - Add an signed integer value.
392 void AddSInt(unsigned Form, int64_t Integer);
394 /// AddString - Add a std::string value.
396 void AddString(unsigned Form, const std::string &String);
398 /// AddLabel - Add a Dwarf label value.
400 void AddLabel(unsigned Form, const DWLabel &Label);
402 /// AddObjectLabel - Add a non-Dwarf label value.
404 void AddObjectLabel(unsigned Form, const std::string &Label);
406 /// AddDelta - Add a label delta value.
408 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
410 /// AddDIEntry - Add a DIE value.
412 void AddDIEntry(unsigned Form, DIE *Entry);
416 //===----------------------------------------------------------------------===//
417 // DIE - A structured debug information entry. Has an abbreviation which
418 // describes it's organization.
421 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
422 unsigned AbbrevID; // Decribing abbreviation ID.
423 unsigned Offset; // Offset in debug info section.
424 unsigned Size; // Size of instance + children.
425 std::vector<DIE *> Children; // Children DIEs.
426 std::vector<DIEValue *> Values; // Attributes values.
433 unsigned getAbbrevID() const { return AbbrevID; }
434 unsigned getOffset() const { return Offset; }
435 unsigned getSize() const { return Size; }
436 const std::vector<DIE *> &getChildren() const { return Children; }
437 const std::vector<DIEValue *> &getValues() const { return Values; }
438 void setOffset(unsigned O) { Offset = O; }
439 void setSize(unsigned S) { Size = S; }
441 /// SiblingOffset - Return the offset of the debug information entry's
443 unsigned SiblingOffset() const { return Offset + Size; }
445 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
447 void AddSiblingOffset();
449 /// AddUInt - Add an unsigned integer attribute data and value.
451 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
453 /// AddSInt - Add an signed integer attribute data and value.
455 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
457 /// AddString - Add a std::string attribute data and value.
459 void AddString(unsigned Attribute, unsigned Form,
460 const std::string &String);
462 /// AddLabel - Add a Dwarf label attribute data and value.
464 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
466 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
468 void AddObjectLabel(unsigned Attribute, unsigned Form,
469 const std::string &Label);
471 /// AddDelta - Add a label delta attribute data and value.
473 void AddDelta(unsigned Attribute, unsigned Form,
474 const DWLabel &Hi, const DWLabel &Lo);
476 /// AddDIEntry - Add a DIE attribute data and value.
478 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
480 /// AddBlock - Add block data.
482 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
484 /// Complete - Indicate that all attributes have been added and
485 /// ready to get an abbreviation ID.
487 void Complete(DwarfWriter &DW);
489 /// AddChild - Add a child to the DIE.
490 void AddChild(DIE *Child);
493 } // End of namespace llvm
495 //===----------------------------------------------------------------------===//
497 CompileUnit::~CompileUnit() {
501 /// hasContent - Return true if this compile unit has something to write out.
503 bool CompileUnit::hasContent() const {
504 return !Die->getChildren().empty();
507 /// AddGlobal - Add a new global entity to the compile unit.
509 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
513 //===----------------------------------------------------------------------===//
515 /// operator== - Used by UniqueVector to locate entry.
517 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
518 if (Tag != DA.Tag) return false;
519 if (ChildrenFlag != DA.ChildrenFlag) return false;
520 if (Data.size() != DA.Data.size()) return false;
522 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
523 if (Data[i] != DA.Data[i]) return false;
529 /// operator< - Used by UniqueVector to locate entry.
531 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
532 if (Tag != DA.Tag) return Tag < DA.Tag;
533 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
534 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
536 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
537 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
543 /// Emit - Print the abbreviation using the specified Dwarf writer.
545 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
546 // Emit its Dwarf tag type.
547 DW.EmitULEB128Bytes(Tag);
548 DW.EOL(TagString(Tag));
550 // Emit whether it has children DIEs.
551 DW.EmitULEB128Bytes(ChildrenFlag);
552 DW.EOL(ChildrenString(ChildrenFlag));
554 // For each attribute description.
555 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
556 const DIEAbbrevData &AttrData = Data[i];
558 // Emit attribute type.
559 DW.EmitULEB128Bytes(AttrData.getAttribute());
560 DW.EOL(AttributeString(AttrData.getAttribute()));
563 DW.EmitULEB128Bytes(AttrData.getForm());
564 DW.EOL(FormEncodingString(AttrData.getForm()));
567 // Mark end of abbreviation.
568 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
569 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
573 void DIEAbbrev::print(std::ostream &O) {
574 O << "Abbreviation @"
575 << std::hex << (intptr_t)this << std::dec
579 << ChildrenString(ChildrenFlag)
582 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
584 << AttributeString(Data[i].getAttribute())
586 << FormEncodingString(Data[i].getForm())
590 void DIEAbbrev::dump() { print(std::cerr); }
593 //===----------------------------------------------------------------------===//
595 /// BestForm - Choose the best form for integer.
597 unsigned DIEInteger::BestForm(bool IsSigned) {
599 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
600 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
601 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
603 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
604 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
605 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
607 return DW_FORM_data8;
610 /// EmitValue - Emit integer of appropriate size.
612 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
614 case DW_FORM_flag: // Fall thru
615 case DW_FORM_ref1: // Fall thru
616 case DW_FORM_data1: DW.EmitInt8(Integer); break;
617 case DW_FORM_ref2: // Fall thru
618 case DW_FORM_data2: DW.EmitInt16(Integer); break;
619 case DW_FORM_ref4: // Fall thru
620 case DW_FORM_data4: DW.EmitInt32(Integer); break;
621 case DW_FORM_ref8: // Fall thru
622 case DW_FORM_data8: DW.EmitInt64(Integer); break;
623 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
624 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
625 default: assert(0 && "DIE Value form not supported yet"); break;
629 /// SizeOf - Determine size of integer value in bytes.
631 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
633 case DW_FORM_flag: // Fall thru
634 case DW_FORM_ref1: // Fall thru
635 case DW_FORM_data1: return sizeof(int8_t);
636 case DW_FORM_ref2: // Fall thru
637 case DW_FORM_data2: return sizeof(int16_t);
638 case DW_FORM_ref4: // Fall thru
639 case DW_FORM_data4: return sizeof(int32_t);
640 case DW_FORM_ref8: // Fall thru
641 case DW_FORM_data8: return sizeof(int64_t);
642 case DW_FORM_udata: return DW.SizeULEB128(Integer);
643 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
644 default: assert(0 && "DIE Value form not supported yet"); break;
649 //===----------------------------------------------------------------------===//
651 /// EmitValue - Emit string value.
653 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
654 DW.EmitString(String);
657 /// SizeOf - Determine size of string value in bytes.
659 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
660 return String.size() + sizeof(char); // sizeof('\0');
663 //===----------------------------------------------------------------------===//
665 /// EmitValue - Emit label value.
667 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
668 DW.EmitReference(Label);
671 /// SizeOf - Determine size of label value in bytes.
673 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
674 return DW.getAddressSize();
677 //===----------------------------------------------------------------------===//
679 /// EmitValue - Emit label value.
681 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
682 DW.EmitReference(Label);
685 /// SizeOf - Determine size of label value in bytes.
687 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
688 return DW.getAddressSize();
691 //===----------------------------------------------------------------------===//
693 /// EmitValue - Emit delta value.
695 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
696 DW.EmitDifference(LabelHi, LabelLo);
699 /// SizeOf - Determine size of delta value in bytes.
701 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
702 return DW.getAddressSize();
705 //===----------------------------------------------------------------------===//
706 /// EmitValue - Emit debug information entry offset.
708 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
709 DW.EmitInt32(Entry->getOffset());
712 /// SizeOf - Determine size of debug information entry value in bytes.
714 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
715 return sizeof(int32_t);
718 //===----------------------------------------------------------------------===//
720 DIEBlock::~DIEBlock() {
721 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
726 /// ComputeSize - calculate the size of the block.
728 unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
730 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
731 Size += Values[i]->SizeOf(DW, Forms[i]);
736 /// BestForm - Choose the best form for data.
738 unsigned DIEBlock::BestForm() {
739 if ((unsigned char)Size == Size) return DW_FORM_block1;
740 if ((unsigned short)Size == Size) return DW_FORM_block2;
741 if ((unsigned int)Size == Size) return DW_FORM_block4;
742 return DW_FORM_block;
745 /// EmitValue - Emit block data.
747 void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
749 case DW_FORM_block1: DW.EmitInt8(Size); break;
750 case DW_FORM_block2: DW.EmitInt16(Size); break;
751 case DW_FORM_block4: DW.EmitInt32(Size); break;
752 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
753 default: assert(0 && "Improper form for block"); break;
755 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
757 Values[i]->EmitValue(DW, Forms[i]);
761 /// SizeOf - Determine size of block data in bytes.
763 unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
765 case DW_FORM_block1: return Size + sizeof(int8_t);
766 case DW_FORM_block2: return Size + sizeof(int16_t);
767 case DW_FORM_block4: return Size + sizeof(int32_t);
768 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
769 default: assert(0 && "Improper form for block"); break;
774 /// AddUInt - Add an unsigned integer value.
776 void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
777 DIEInteger *DI = new DIEInteger(Integer);
778 Values.push_back(DI);
779 if (Form == 0) Form = DI->BestForm(false);
780 Forms.push_back(Form);
783 /// AddSInt - Add an signed integer value.
785 void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
786 DIEInteger *DI = new DIEInteger(Integer);
787 Values.push_back(DI);
788 if (Form == 0) Form = DI->BestForm(true);
789 Forms.push_back(Form);
792 /// AddString - Add a std::string value.
794 void DIEBlock::AddString(unsigned Form, const std::string &String) {
795 Values.push_back(new DIEString(String));
796 Forms.push_back(Form);
799 /// AddLabel - Add a Dwarf label value.
801 void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
802 Values.push_back(new DIEDwarfLabel(Label));
803 Forms.push_back(Form);
806 /// AddObjectLabel - Add a non-Dwarf label value.
808 void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
809 Values.push_back(new DIEObjectLabel(Label));
810 Forms.push_back(Form);
813 /// AddDelta - Add a label delta value.
815 void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
816 Values.push_back(new DIEDelta(Hi, Lo));
817 Forms.push_back(Form);
820 /// AddDIEntry - Add a DIE value.
822 void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
823 Values.push_back(new DIEntry(Entry));
824 Forms.push_back(Form);
827 //===----------------------------------------------------------------------===//
829 DIE::DIE(unsigned Tag)
830 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
839 if (Abbrev) delete Abbrev;
841 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
845 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
850 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
852 void DIE::AddSiblingOffset() {
853 DIEInteger *DI = new DIEInteger(0);
854 Values.insert(Values.begin(), DI);
855 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
858 /// AddUInt - Add an unsigned integer attribute data and value.
860 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
861 DIEInteger *DI = new DIEInteger(Integer);
862 Values.push_back(DI);
863 if (!Form) Form = DI->BestForm(false);
864 Abbrev->AddAttribute(Attribute, Form);
867 /// AddSInt - Add an signed integer attribute data and value.
869 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
870 DIEInteger *DI = new DIEInteger(Integer);
871 Values.push_back(DI);
872 if (!Form) Form = DI->BestForm(true);
873 Abbrev->AddAttribute(Attribute, Form);
876 /// AddString - Add a std::string attribute data and value.
878 void DIE::AddString(unsigned Attribute, unsigned Form,
879 const std::string &String) {
880 Values.push_back(new DIEString(String));
881 Abbrev->AddAttribute(Attribute, Form);
884 /// AddLabel - Add a Dwarf label attribute data and value.
886 void DIE::AddLabel(unsigned Attribute, unsigned Form,
887 const DWLabel &Label) {
888 Values.push_back(new DIEDwarfLabel(Label));
889 Abbrev->AddAttribute(Attribute, Form);
892 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
894 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
895 const std::string &Label) {
896 Values.push_back(new DIEObjectLabel(Label));
897 Abbrev->AddAttribute(Attribute, Form);
900 /// AddDelta - Add a label delta attribute data and value.
902 void DIE::AddDelta(unsigned Attribute, unsigned Form,
903 const DWLabel &Hi, const DWLabel &Lo) {
904 Values.push_back(new DIEDelta(Hi, Lo));
905 Abbrev->AddAttribute(Attribute, Form);
908 /// AddDIEntry - Add a DIE attribute data and value.
910 void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
911 Values.push_back(new DIEntry(Entry));
912 Abbrev->AddAttribute(Attribute, Form);
915 /// AddBlock - Add block data.
917 void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
918 assert(Block->Size && "Block size has not been computed");
919 Values.push_back(Block);
920 if (!Form) Form = Block->BestForm();
921 Abbrev->AddAttribute(Attribute, Form);
924 /// Complete - Indicate that all attributes have been added and ready to get an
926 void DIE::Complete(DwarfWriter &DW) {
927 AbbrevID = DW.NewAbbreviation(Abbrev);
932 /// AddChild - Add a child to the DIE.
934 void DIE::AddChild(DIE *Child) {
935 assert(Abbrev && "Adding children without an abbreviation");
936 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
937 Children.push_back(Child);
940 //===----------------------------------------------------------------------===//
944 //===----------------------------------------------------------------------===//
946 /// PrintHex - Print a value as a hexidecimal value.
948 void DwarfWriter::PrintHex(int Value) const {
949 O << "0x" << std::hex << Value << std::dec;
952 /// EOL - Print a newline character to asm stream. If a comment is present
953 /// then it will be printed first. Comments should not contain '\n'.
954 void DwarfWriter::EOL(const std::string &Comment) const {
955 if (DwarfVerbose && !Comment.empty()) {
957 << Asm->CommentString
964 /// EmitAlign - Print a align directive.
966 void DwarfWriter::EmitAlign(unsigned Alignment) const {
967 O << Asm->AlignDirective << Alignment << "\n";
970 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
971 /// unsigned leb128 value.
972 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
977 O << Asm->Data8bitsDirective;
982 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
983 /// signed leb128 value.
984 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
989 O << Asm->Data8bitsDirective;
994 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
995 /// representing an unsigned leb128 value.
996 void DwarfWriter::PrintULEB128(unsigned Value) const {
998 unsigned Byte = Value & 0x7f;
1000 if (Value) Byte |= 0x80;
1002 if (Value) O << ", ";
1006 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1008 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1012 Size += sizeof(int8_t);
1017 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
1018 /// representing a signed leb128 value.
1019 void DwarfWriter::PrintSLEB128(int Value) const {
1020 int Sign = Value >> (8 * sizeof(Value) - 1);
1024 unsigned Byte = Value & 0x7f;
1026 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1027 if (IsMore) Byte |= 0x80;
1029 if (IsMore) O << ", ";
1033 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1035 unsigned DwarfWriter::SizeSLEB128(int Value) {
1037 int Sign = Value >> (8 * sizeof(Value) - 1);
1041 unsigned Byte = Value & 0x7f;
1043 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1044 Size += sizeof(int8_t);
1049 /// EmitInt8 - Emit a byte directive and value.
1051 void DwarfWriter::EmitInt8(int Value) const {
1052 O << Asm->Data8bitsDirective;
1053 PrintHex(Value & 0xFF);
1056 /// EmitInt16 - Emit a short directive and value.
1058 void DwarfWriter::EmitInt16(int Value) const {
1059 O << Asm->Data16bitsDirective;
1060 PrintHex(Value & 0xFFFF);
1063 /// EmitInt32 - Emit a long directive and value.
1065 void DwarfWriter::EmitInt32(int Value) const {
1066 O << Asm->Data32bitsDirective;
1070 /// EmitInt64 - Emit a long long directive and value.
1072 void DwarfWriter::EmitInt64(uint64_t Value) const {
1073 if (Asm->Data64bitsDirective) {
1074 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1076 const TargetData &TD = Asm->TM.getTargetData();
1078 if (TD.isBigEndian()) {
1079 EmitInt32(unsigned(Value >> 32)); O << "\n";
1080 EmitInt32(unsigned(Value));
1082 EmitInt32(unsigned(Value)); O << "\n";
1083 EmitInt32(unsigned(Value >> 32));
1088 /// EmitString - Emit a string with quotes and a null terminator.
1089 /// Special characters are emitted properly. (Eg. '\t')
1090 void DwarfWriter::EmitString(const std::string &String) const {
1091 O << Asm->AsciiDirective
1093 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1094 unsigned char C = String[i];
1096 if (!isascii(C) || iscntrl(C)) {
1098 case '\b': O << "\\b"; break;
1099 case '\f': O << "\\f"; break;
1100 case '\n': O << "\\n"; break;
1101 case '\r': O << "\\r"; break;
1102 case '\t': O << "\\t"; break;
1105 O << char('0' + (C >> 6));
1106 O << char('0' + (C >> 3));
1107 O << char('0' + (C >> 0));
1110 } else if (C == '\"') {
1112 } else if (C == '\'') {
1121 /// PrintLabelName - Print label name in form used by Dwarf writer.
1123 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
1124 O << Asm->PrivateGlobalPrefix
1127 if (Number) O << Number;
1130 /// EmitLabel - Emit location label for internal use by Dwarf.
1132 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1133 PrintLabelName(Tag, Number);
1137 /// EmitReference - Emit a reference to a label.
1139 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
1140 if (AddressSize == 4)
1141 O << Asm->Data32bitsDirective;
1143 O << Asm->Data64bitsDirective;
1145 PrintLabelName(Tag, Number);
1147 void DwarfWriter::EmitReference(const std::string &Name) const {
1148 if (AddressSize == 4)
1149 O << Asm->Data32bitsDirective;
1151 O << Asm->Data64bitsDirective;
1156 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1157 /// assemblers do not accept absolute expressions with data directives, so there
1158 /// is an option (needsSet) to use an intermediary 'set' expression.
1159 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1160 const char *TagLo, unsigned NumberLo) const {
1162 static unsigned SetCounter = 0;
1165 PrintLabelName("set", SetCounter);
1167 PrintLabelName(TagHi, NumberHi);
1169 PrintLabelName(TagLo, NumberLo);
1172 if (AddressSize == sizeof(int32_t))
1173 O << Asm->Data32bitsDirective;
1175 O << Asm->Data64bitsDirective;
1177 PrintLabelName("set", SetCounter);
1181 if (AddressSize == sizeof(int32_t))
1182 O << Asm->Data32bitsDirective;
1184 O << Asm->Data64bitsDirective;
1186 PrintLabelName(TagHi, NumberHi);
1188 PrintLabelName(TagLo, NumberLo);
1192 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
1194 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1195 return Abbreviations.insert(*Abbrev);
1198 /// NewString - Add a string to the constant pool and returns a label.
1200 DWLabel DwarfWriter::NewString(const std::string &String) {
1201 unsigned StringID = StringPool.insert(String);
1202 return DWLabel("string", StringID);
1205 /// AddSourceLine - Add location information to specified debug information
1207 void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1209 CompileUnit *FileUnit = FindCompileUnit(File);
1210 unsigned FileID = FileUnit->getID();
1211 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1212 Die->AddUInt(DW_AT_decl_line, 0, Line);
1216 /// AddAddress - Add an address attribute to a die based on the location
1218 void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
1219 MachineLocation &Location) {
1220 DIEBlock *Block = new DIEBlock();
1221 if (Location.isRegister()) {
1222 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Location.getRegister());
1224 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Location.getRegister());
1225 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1227 Block->ComputeSize(*this);
1228 Die->AddBlock(Attribute, 0, Block);
1231 /// getDieMapSlotFor - Returns the debug information entry map slot for the
1232 /// specified debug descriptor.
1233 DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1234 return DescToDieMap[DD];
1237 /// NewType - Create a new type DIE.
1239 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1241 // FIXME - Hack for missing types
1242 DIE *Die = new DIE(DW_TAG_base_type);
1243 Die->AddUInt(DW_AT_byte_size, 0, 4);
1244 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1245 Unit->getDie()->AddChild(Die);
1249 // FIXME - Should handle other contexts that compile units.
1251 // Check for pre-existence.
1252 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
1253 if (Slot) return Slot;
1255 // Get core information.
1256 const std::string &Name = TyDesc->getName();
1257 uint64_t Size = TyDesc->getSize() >> 3;
1261 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1262 // Fundamental types like int, float, bool
1263 Slot = Ty = new DIE(DW_TAG_base_type);
1264 unsigned Encoding = BasicTy->getEncoding();
1265 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1266 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1267 // Create specific DIE.
1268 Slot = Ty = new DIE(DerivedTy->getTag());
1270 // Map to main type, void will not have a type.
1271 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1272 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1273 NewType(Context, FromTy, Unit));
1275 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1276 // Create specific DIE.
1277 Slot = Ty = new DIE(CompTy->getTag());
1278 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1280 switch (CompTy->getTag()) {
1281 case DW_TAG_array_type: {
1282 // Add element type.
1283 if (TypeDesc *FromTy = CompTy->getFromType()) {
1284 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1285 NewType(Context, FromTy, Unit));
1287 // Don't emit size attribute.
1290 // Construct an anonymous type for index type.
1291 DIE *IndexTy = new DIE(DW_TAG_base_type);
1292 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1293 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1295 Context->AddChild(IndexTy);
1297 // Add subranges to array type.
1298 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1299 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1300 int64_t Lo = SRD->getLo();
1301 int64_t Hi = SRD->getHi();
1302 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1304 // If a range is available.
1306 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1307 // Only add low if non-zero.
1308 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1309 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
1311 Ty->AddChild(Subrange);
1316 case DW_TAG_structure_type:
1317 case DW_TAG_union_type: {
1318 // FIXME - this is just the basics.
1319 // Add elements to structure type.
1320 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1321 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
1323 // Extract the basic information.
1324 const std::string &Name = MemberDesc->getName();
1325 TypeDesc *MemTy = MemberDesc->getFromType();
1326 uint64_t Size = MemberDesc->getSize();
1327 uint64_t Align = MemberDesc->getAlign();
1328 uint64_t Offset = MemberDesc->getOffset();
1330 // Construct member debug information entry.
1331 DIE *Member = new DIE(DW_TAG_member);
1333 // Add name if not "".
1334 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
1335 // Add location if available.
1336 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1338 // Most of the time the field info is the same as the members.
1339 uint64_t FieldSize = Size;
1340 uint64_t FieldAlign = Align;
1341 uint64_t FieldOffset = Offset;
1343 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1344 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1345 NewType(Context, FromTy, Unit));
1346 FieldSize = FromTy->getSize();
1347 FieldAlign = FromTy->getSize();
1350 // Unless we have a bit field.
1351 if (FieldSize != Size) {
1352 // Construct the alignment mask.
1353 uint64_t AlignMask = ~(FieldAlign - 1);
1354 // Determine the high bit + 1 of the declared size.
1355 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1356 // Work backwards to determine the base offset of the field.
1357 FieldOffset = HiMark - FieldSize;
1358 // Now normalize offset to the field.
1359 Offset -= FieldOffset;
1361 // Maybe we need to work from the other.
1362 const TargetData &TD = Asm->TM.getTargetData();
1363 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1365 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1366 Member->AddUInt(DW_AT_bit_size, 0, Size);
1367 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1370 // Add computation for offset.
1371 DIEBlock *Block = new DIEBlock();
1372 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1373 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1374 Block->ComputeSize(*this);
1375 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1377 Ty->AddChild(Member);
1381 case DW_TAG_enumeration_type: {
1382 // Add enumerators to enumeration type.
1383 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1384 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1385 const std::string &Name = ED->getName();
1386 int64_t Value = ED->getValue();
1387 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1388 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1389 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1390 Ty->AddChild(Enumerator);
1399 assert(Ty && "Type not supported yet");
1401 // Add size if non-zero (derived types don't have a size.)
1402 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1403 // Add name if not anonymous or intermediate type.
1404 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1405 // Add source line info if available.
1406 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
1408 // Add to context owner.
1409 Context->AddChild(Ty);
1414 /// NewCompileUnit - Create new compile unit and it's debug information entry.
1416 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1418 // Construct debug information entry.
1419 DIE *Die = new DIE(DW_TAG_compile_unit);
1420 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1421 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1422 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1423 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1424 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1425 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1426 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1428 // Add debug information entry to descriptor map.
1429 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1432 // Construct compile unit.
1433 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1435 // Add Unit to compile unit map.
1436 DescToUnitMap[UnitDesc] = Unit;
1441 /// FindCompileUnit - Get the compile unit for the given descriptor.
1443 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1444 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1445 assert(Unit && "Missing compile unit.");
1449 /// NewGlobalVariable - Add a new global variable DIE.
1451 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1452 // Get the compile unit context.
1453 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1454 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1456 // Check for pre-existence.
1457 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1458 if (Slot) return Slot;
1460 // Get the global variable itself.
1461 GlobalVariable *GV = GVD->getGlobalVariable();
1462 // Generate the mangled name.
1463 std::string MangledName = Asm->Mang->getValueName(GV);
1465 // Gather the details (simplify add attribute code.)
1466 const std::string &Name = GVD->getName();
1468 // Get the global's type.
1469 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
1471 // Create the globale variable DIE.
1472 DIE *VariableDie = new DIE(DW_TAG_variable);
1473 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1474 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1475 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1477 // Add source line info if available.
1478 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1481 DIEBlock *Block = new DIEBlock();
1482 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1483 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1484 Block->ComputeSize(*this);
1485 VariableDie->AddBlock(DW_AT_location, 0, Block);
1490 // Add to context owner.
1491 Unit->getDie()->AddChild(VariableDie);
1493 // Expose as global.
1494 // FIXME - need to check external flag.
1495 Unit->AddGlobal(Name, VariableDie);
1500 /// NewSubprogram - Add a new subprogram DIE.
1502 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1503 // Get the compile unit context.
1504 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1505 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1507 // Check for pre-existence.
1508 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1509 if (Slot) return Slot;
1511 // Gather the details (simplify add attribute code.)
1512 const std::string &Name = SPD->getName();
1513 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
1514 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1516 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1517 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1518 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1519 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1521 // Add source line info if available.
1522 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1525 Slot = SubprogramDie;
1527 // Add to context owner.
1528 Unit->getDie()->AddChild(SubprogramDie);
1530 // Expose as global.
1531 Unit->AddGlobal(Name, SubprogramDie);
1533 return SubprogramDie;
1536 /// NewScopeVariable - Create a new scope variable.
1538 DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1539 // Get the descriptor.
1540 VariableDesc *VD = DV->getDesc();
1542 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1544 switch (VD->getTag()) {
1545 case DW_TAG_return_variable: return NULL;
1546 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1547 case DW_TAG_auto_variable: // fall thru
1548 default: Tag = DW_TAG_variable; break;
1551 // Define variable debug information entry.
1552 DIE *VariableDie = new DIE(Tag);
1553 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1555 // Add source line info if available.
1556 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1558 // Add variable type.
1559 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
1560 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1562 // Add variable address.
1563 MachineLocation Location;
1564 Asm->TM.getRegisterInfo()->getLocation(*MF, DV->getFrameIndex(), Location);
1565 AddAddress(VariableDie, DW_AT_location, Location);
1570 /// ConstructScope - Construct the components of a scope.
1572 void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1573 DIE *ParentDie, CompileUnit *Unit) {
1574 // Add variables to scope.
1575 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1576 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1577 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1578 if (VariableDie) ParentDie->AddChild(VariableDie);
1581 // Add nested scopes.
1582 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1583 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1584 // Define the Scope debug information entry.
1585 DebugScope *Scope = Scopes[j];
1586 // FIXME - Ignore inlined functions for the time being.
1587 if (Scope->getParent()) continue;
1589 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1591 // Add the scope bounds.
1592 if (unsigned StartID = Scope->getStartLabelID()) {
1593 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1594 DWLabel("loc", StartID));
1596 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1597 DWLabel("func_begin", SubprogramCount));
1599 if (unsigned EndID = Scope->getEndLabelID()) {
1600 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1601 DWLabel("loc", EndID));
1603 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1604 DWLabel("func_end", SubprogramCount));
1607 // Add the scope contents.
1608 ConstructScope(Scope, ScopeDie, Unit);
1609 ParentDie->AddChild(ScopeDie);
1613 /// ConstructRootScope - Construct the scope for the subprogram.
1615 void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1616 // Exit if there is no root scope.
1617 if (!RootScope) return;
1619 // Get the subprogram debug information entry.
1620 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1622 // Get the compile unit context.
1623 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1624 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1626 // Get the subprogram die.
1627 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1628 assert(SPDie && "Missing subprogram descriptor");
1630 // Add the function bounds.
1631 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1632 DWLabel("func_begin", SubprogramCount));
1633 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1634 DWLabel("func_end", SubprogramCount));
1635 MachineLocation Location(Asm->TM.getRegisterInfo()->getFrameRegister(*MF));
1636 AddAddress(SPDie, DW_AT_frame_base, Location);
1638 ConstructScope(RootScope, SPDie, Unit);
1641 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1642 /// tools to recognize the object file contains Dwarf information.
1644 void DwarfWriter::EmitInitial() const {
1645 // Dwarf sections base addresses.
1646 Asm->SwitchSection(DwarfFrameSection, 0);
1647 EmitLabel("section_frame", 0);
1648 Asm->SwitchSection(DwarfInfoSection, 0);
1649 EmitLabel("section_info", 0);
1650 EmitLabel("info", 0);
1651 Asm->SwitchSection(DwarfAbbrevSection, 0);
1652 EmitLabel("section_abbrev", 0);
1653 EmitLabel("abbrev", 0);
1654 Asm->SwitchSection(DwarfARangesSection, 0);
1655 EmitLabel("section_aranges", 0);
1656 Asm->SwitchSection(DwarfMacInfoSection, 0);
1657 EmitLabel("section_macinfo", 0);
1658 Asm->SwitchSection(DwarfLineSection, 0);
1659 EmitLabel("section_line", 0);
1660 EmitLabel("line", 0);
1661 Asm->SwitchSection(DwarfLocSection, 0);
1662 EmitLabel("section_loc", 0);
1663 Asm->SwitchSection(DwarfPubNamesSection, 0);
1664 EmitLabel("section_pubnames", 0);
1665 Asm->SwitchSection(DwarfStrSection, 0);
1666 EmitLabel("section_str", 0);
1667 Asm->SwitchSection(DwarfRangesSection, 0);
1668 EmitLabel("section_ranges", 0);
1670 Asm->SwitchSection(TextSection, 0);
1671 EmitLabel("text_begin", 0);
1672 Asm->SwitchSection(DataSection, 0);
1673 EmitLabel("data_begin", 0);
1676 /// EmitDIE - Recusively Emits a debug information entry.
1678 void DwarfWriter::EmitDIE(DIE *Die) const {
1679 // Get the abbreviation for this DIE.
1680 unsigned AbbrevID = Die->getAbbrevID();
1681 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1685 // Emit the code (index) for the abbreviation.
1686 EmitULEB128Bytes(AbbrevID);
1687 EOL(std::string("Abbrev [" +
1689 "] 0x" + utohexstr(Die->getOffset()) +
1690 ":0x" + utohexstr(Die->getSize()) + " " +
1691 TagString(Abbrev.getTag())));
1693 const std::vector<DIEValue *> &Values = Die->getValues();
1694 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1696 // Emit the DIE attribute values.
1697 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1698 unsigned Attr = AbbrevData[i].getAttribute();
1699 unsigned Form = AbbrevData[i].getForm();
1700 assert(Form && "Too many attributes for DIE (check abbreviation)");
1703 case DW_AT_sibling: {
1704 EmitInt32(Die->SiblingOffset());
1708 // Emit an attribute using the defined form.
1709 Values[i]->EmitValue(*this, Form);
1714 EOL(AttributeString(Attr));
1717 // Emit the DIE children if any.
1718 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1719 const std::vector<DIE *> &Children = Die->getChildren();
1721 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1722 EmitDIE(Children[j]);
1725 EmitInt8(0); EOL("End Of Children Mark");
1729 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1731 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1732 // Get the children.
1733 const std::vector<DIE *> &Children = Die->getChildren();
1735 // If not last sibling and has children then add sibling offset attribute.
1736 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1738 // Record the abbreviation.
1739 Die->Complete(*this);
1741 // Get the abbreviation for this DIE.
1742 unsigned AbbrevID = Die->getAbbrevID();
1743 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1746 Die->setOffset(Offset);
1748 // Start the size with the size of abbreviation code.
1749 Offset += SizeULEB128(AbbrevID);
1751 const std::vector<DIEValue *> &Values = Die->getValues();
1752 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1754 // Emit the DIE attribute values.
1755 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1756 // Size attribute value.
1757 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1760 // Emit the DIE children if any.
1761 if (!Children.empty()) {
1762 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1763 "Children flag not set");
1765 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1766 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1769 // End of children marker.
1770 Offset += sizeof(int8_t);
1773 Die->setSize(Offset - Die->getOffset());
1777 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1779 void DwarfWriter::SizeAndOffsets() {
1781 // Process each compile unit.
1782 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1783 CompileUnit *Unit = CompileUnits[i];
1784 if (Unit->hasContent()) {
1785 // Compute size of compile unit header
1786 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1787 sizeof(int16_t) + // DWARF version number
1788 sizeof(int32_t) + // Offset Into Abbrev. Section
1789 sizeof(int8_t); // Pointer Size (in bytes)
1790 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
1795 /// EmitDebugInfo - Emit the debug info section.
1797 void DwarfWriter::EmitDebugInfo() const {
1798 // Start debug info section.
1799 Asm->SwitchSection(DwarfInfoSection, 0);
1801 // Process each compile unit.
1802 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1803 CompileUnit *Unit = CompileUnits[i];
1805 if (Unit->hasContent()) {
1806 DIE *Die = Unit->getDie();
1807 // Emit the compile units header.
1808 EmitLabel("info_begin", Unit->getID());
1809 // Emit size of content not including length itself
1810 unsigned ContentSize = Die->getSize() +
1811 sizeof(int16_t) + // DWARF version number
1812 sizeof(int32_t) + // Offset Into Abbrev. Section
1813 sizeof(int8_t); // Pointer Size (in bytes)
1815 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1816 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1817 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1818 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1821 EmitLabel("info_end", Unit->getID());
1828 /// EmitAbbreviations - Emit the abbreviation section.
1830 void DwarfWriter::EmitAbbreviations() const {
1831 // Check to see if it is worth the effort.
1832 if (!Abbreviations.empty()) {
1833 // Start the debug abbrev section.
1834 Asm->SwitchSection(DwarfAbbrevSection, 0);
1836 EmitLabel("abbrev_begin", 0);
1838 // For each abbrevation.
1839 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1840 AbbrevID <= NAID; ++AbbrevID) {
1841 // Get abbreviation data
1842 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1844 // Emit the abbrevations code (base 1 index.)
1845 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1847 // Emit the abbreviations data.
1853 EmitLabel("abbrev_end", 0);
1859 /// EmitDebugLines - Emit source line information.
1861 void DwarfWriter::EmitDebugLines() const {
1862 // Minimum line delta, thus ranging from -10..(255-10).
1863 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1864 // Maximum line delta, thus ranging from -10..(255-10).
1865 const int MaxLineDelta = 255 + MinLineDelta;
1867 // Start the dwarf line section.
1868 Asm->SwitchSection(DwarfLineSection, 0);
1870 // Construct the section header.
1872 EmitDifference("line_end", 0, "line_begin", 0);
1873 EOL("Length of Source Line Info");
1874 EmitLabel("line_begin", 0);
1876 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1878 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1879 EOL("Prolog Length");
1880 EmitLabel("line_prolog_begin", 0);
1882 EmitInt8(1); EOL("Minimum Instruction Length");
1884 EmitInt8(1); EOL("Default is_stmt_start flag");
1886 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1888 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1890 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1892 // Line number standard opcode encodings argument count
1893 EmitInt8(0); EOL("DW_LNS_copy arg count");
1894 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1895 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1896 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1897 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1898 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1899 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1900 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1901 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1903 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1904 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1906 // Emit directories.
1907 for (unsigned DirectoryID = 1, NDID = Directories.size();
1908 DirectoryID <= NDID; ++DirectoryID) {
1909 EmitString(Directories[DirectoryID]); EOL("Directory");
1911 EmitInt8(0); EOL("End of directories");
1914 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1915 SourceID <= NSID; ++SourceID) {
1916 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1917 EmitString(SourceFile.getName()); EOL("Source");
1918 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1919 EmitULEB128Bytes(0); EOL("Mod date");
1920 EmitULEB128Bytes(0); EOL("File size");
1922 EmitInt8(0); EOL("End of files");
1924 EmitLabel("line_prolog_end", 0);
1926 // Emit line information
1927 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1929 // Dwarf assumes we start with first line of first source file.
1930 unsigned Source = 1;
1933 // Construct rows of the address, source, line, column matrix.
1934 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1935 SourceLineInfo *LineInfo = LineInfos[i];
1938 unsigned SourceID = LineInfo->getSourceID();
1939 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1940 unsigned DirectoryID = SourceFile.getDirectoryID();
1942 << Asm->CommentString << " "
1943 << Directories[DirectoryID]
1944 << SourceFile.getName() << ":"
1945 << LineInfo->getLine() << "\n";
1948 // Define the line address.
1949 EmitInt8(0); EOL("Extended Op");
1950 EmitInt8(4 + 1); EOL("Op size");
1951 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1952 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
1954 // If change of source, then switch to the new source.
1955 if (Source != LineInfo->getSourceID()) {
1956 Source = LineInfo->getSourceID();
1957 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1958 EmitULEB128Bytes(Source); EOL("New Source");
1961 // If change of line.
1962 if (Line != LineInfo->getLine()) {
1963 // Determine offset.
1964 int Offset = LineInfo->getLine() - Line;
1965 int Delta = Offset - MinLineDelta;
1968 Line = LineInfo->getLine();
1970 // If delta is small enough and in range...
1971 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1972 // ... then use fast opcode.
1973 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1975 // ... otherwise use long hand.
1976 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1977 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1978 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1981 // Copy the previous row (different address or source)
1982 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1986 // Define last address.
1987 EmitInt8(0); EOL("Extended Op");
1988 EmitInt8(4 + 1); EOL("Op size");
1989 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1990 EmitReference("text_end", 0); EOL("Location label");
1992 // Mark end of matrix.
1993 EmitInt8(0); EOL("DW_LNE_end_sequence");
1994 EmitULEB128Bytes(1); O << "\n";
1995 EmitInt8(1); O << "\n";
1997 EmitLabel("line_end", 0);
2002 /// EmitDebugFrame - Emit visible names into a debug frame section.
2004 void DwarfWriter::EmitDebugFrame() {
2005 // Start the dwarf pubnames section.
2006 Asm->SwitchSection(DwarfFrameSection, 0);
2008 EmitDifference("frame_common_end", 0,
2009 "frame_common_begin", 0);
2010 EOL("Length of Common Information Entry");
2012 EmitLabel("frame_common_begin", 0);
2013 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2014 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2015 EmitString(""); EOL("CIE Augmentation");
2016 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2017 // FIXME - needs to change based on stack direction.
2018 EmitSLEB128Bytes(-sizeof(int32_t)); EOL("CIE Data Alignment Factor");
2019 // FIXME - hard coded for PPC (LR).
2020 EmitInt8(0x41); EOL("CIE RA Column Hardcoded (PPC LR)");
2021 // FIXME - hard coded for PPC 0(SP).
2022 EmitULEB128Bytes(DW_CFA_def_cfa); EOL("DW_CFA_def_cfa");
2023 EmitULEB128Bytes(1); EOL("PPC Register SP");
2024 EmitULEB128Bytes(0); EOL("PPC offset 0 as in 0(SP)");
2026 EmitLabel("frame_common_end", 0);
2031 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2033 void DwarfWriter::EmitDebugPubNames() {
2034 // Start the dwarf pubnames section.
2035 Asm->SwitchSection(DwarfPubNamesSection, 0);
2037 // Process each compile unit.
2038 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2039 CompileUnit *Unit = CompileUnits[i];
2041 if (Unit->hasContent()) {
2042 EmitDifference("pubnames_end", Unit->getID(),
2043 "pubnames_begin", Unit->getID());
2044 EOL("Length of Public Names Info");
2046 EmitLabel("pubnames_begin", Unit->getID());
2048 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2050 EmitReference("info_begin", Unit->getID());
2051 EOL("Offset of Compilation Unit Info");
2053 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2054 EOL("Compilation Unit Length");
2056 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2058 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2061 const std::string &Name = GI->first;
2062 DIE * Entity = GI->second;
2064 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2065 EmitString(Name); EOL("External Name");
2068 EmitInt32(0); EOL("End Mark");
2069 EmitLabel("pubnames_end", Unit->getID());
2076 /// EmitDebugStr - Emit visible names into a debug str section.
2078 void DwarfWriter::EmitDebugStr() {
2079 // Check to see if it is worth the effort.
2080 if (!StringPool.empty()) {
2081 // Start the dwarf str section.
2082 Asm->SwitchSection(DwarfStrSection, 0);
2084 // For each of strings in the string pool.
2085 for (unsigned StringID = 1, N = StringPool.size();
2086 StringID <= N; ++StringID) {
2087 // Emit a label for reference from debug information entries.
2088 EmitLabel("string", StringID);
2089 // Emit the string itself.
2090 const std::string &String = StringPool[StringID];
2091 EmitString(String); O << "\n";
2098 /// EmitDebugLoc - Emit visible names into a debug loc section.
2100 void DwarfWriter::EmitDebugLoc() {
2101 // Start the dwarf loc section.
2102 Asm->SwitchSection(DwarfLocSection, 0);
2107 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2109 void DwarfWriter::EmitDebugARanges() {
2110 // Start the dwarf aranges section.
2111 Asm->SwitchSection(DwarfARangesSection, 0);
2115 // Process each compile unit.
2116 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2117 CompileUnit *Unit = CompileUnits[i];
2119 if (Unit->hasContent()) {
2120 // Don't include size of length
2121 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2123 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2125 EmitReference("info_begin", Unit->getID());
2126 EOL("Offset of Compilation Unit Info");
2128 EmitInt8(AddressSize); EOL("Size of Address");
2130 EmitInt8(0); EOL("Size of Segment Descriptor");
2132 EmitInt16(0); EOL("Pad (1)");
2133 EmitInt16(0); EOL("Pad (2)");
2136 EmitReference("text_begin", 0); EOL("Address");
2137 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2139 EmitInt32(0); EOL("EOM (1)");
2140 EmitInt32(0); EOL("EOM (2)");
2148 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2150 void DwarfWriter::EmitDebugRanges() {
2151 // Start the dwarf ranges section.
2152 Asm->SwitchSection(DwarfRangesSection, 0);
2157 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2159 void DwarfWriter::EmitDebugMacInfo() {
2160 // Start the dwarf macinfo section.
2161 Asm->SwitchSection(DwarfMacInfoSection, 0);
2166 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2168 void DwarfWriter::ConstructCompileUnitDIEs() {
2169 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2171 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2172 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
2173 CompileUnits.push_back(Unit);
2177 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2179 void DwarfWriter::ConstructGlobalDIEs() {
2180 std::vector<GlobalVariableDesc *> GlobalVariables =
2181 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2183 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2184 GlobalVariableDesc *GVD = GlobalVariables[i];
2185 NewGlobalVariable(GVD);
2189 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2191 void DwarfWriter::ConstructSubprogramDIEs() {
2192 std::vector<SubprogramDesc *> Subprograms =
2193 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2195 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2196 SubprogramDesc *SPD = Subprograms[i];
2201 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
2203 bool DwarfWriter::ShouldEmitDwarf() {
2204 // Check if debug info is present.
2205 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2207 // Make sure initial declarations are made.
2211 // Create all the compile unit DIEs.
2212 ConstructCompileUnitDIEs();
2214 // Create DIEs for each of the externally visible global variables.
2215 ConstructGlobalDIEs();
2217 // Create DIEs for each of the externally visible subprograms.
2218 ConstructSubprogramDIEs();
2227 //===----------------------------------------------------------------------===//
2228 // Main entry points.
2231 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2238 , SubprogramCount(0)
2245 , AddressSize(sizeof(int32_t))
2250 , DwarfAbbrevSection(".debug_abbrev")
2251 , DwarfInfoSection(".debug_info")
2252 , DwarfLineSection(".debug_line")
2253 , DwarfFrameSection(".debug_frame")
2254 , DwarfPubNamesSection(".debug_pubnames")
2255 , DwarfPubTypesSection(".debug_pubtypes")
2256 , DwarfStrSection(".debug_str")
2257 , DwarfLocSection(".debug_loc")
2258 , DwarfARangesSection(".debug_aranges")
2259 , DwarfRangesSection(".debug_ranges")
2260 , DwarfMacInfoSection(".debug_macinfo")
2261 , TextSection(".text")
2262 , DataSection(".data")
2264 DwarfWriter::~DwarfWriter() {
2265 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2266 delete CompileUnits[i];
2270 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
2272 void DwarfWriter::BeginModule(Module *M) {
2275 if (!ShouldEmitDwarf()) return;
2276 EOL("Dwarf Begin Module");
2279 /// EndModule - Emit all Dwarf sections that should come after the content.
2281 void DwarfWriter::EndModule() {
2282 if (!ShouldEmitDwarf()) return;
2283 EOL("Dwarf End Module");
2285 // Standard sections final addresses.
2286 Asm->SwitchSection(TextSection, 0);
2287 EmitLabel("text_end", 0);
2288 Asm->SwitchSection(DataSection, 0);
2289 EmitLabel("data_end", 0);
2291 // Compute DIE offsets and sizes.
2294 // Emit all the DIEs into a debug info section
2297 // Corresponding abbreviations into a abbrev section.
2298 EmitAbbreviations();
2300 // Emit source line correspondence into a debug line section.
2303 // Emit info into a debug frame section.
2306 // Emit info into a debug pubnames section.
2307 EmitDebugPubNames();
2309 // Emit info into a debug str section.
2312 // Emit info into a debug loc section.
2315 // Emit info into a debug aranges section.
2318 // Emit info into a debug ranges section.
2321 // Emit info into a debug macinfo section.
2325 /// BeginFunction - Gather pre-function debug information.
2327 void DwarfWriter::BeginFunction(MachineFunction *MF) {
2330 if (!ShouldEmitDwarf()) return;
2331 EOL("Dwarf Begin Function");
2333 // Define begin label for subprogram.
2334 Asm->SwitchSection(TextSection, 0);
2335 EmitLabel("func_begin", ++SubprogramCount);
2339 /// EndFunction - Gather and emit post-function debug information.
2341 void DwarfWriter::EndFunction() {
2342 if (!ShouldEmitDwarf()) return;
2343 EOL("Dwarf End Function");
2345 // Define end label for subprogram.
2346 Asm->SwitchSection(TextSection, 0);
2347 EmitLabel("func_end", SubprogramCount);
2349 // Construct scopes for subprogram.
2350 ConstructRootScope(DebugInfo->getRootScope());
2351 DebugInfo->ClearScopes();