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.
45 //===----------------------------------------------------------------------===//
46 // CompileUnit - This dwarf writer support class manages information associate
47 // with a source file.
50 CompileUnitDesc *Desc; // Compile unit debug descriptor.
51 unsigned ID; // File ID for source.
52 DIE *Die; // Compile unit debug information entry.
53 std::map<std::string, DIE *> Globals; // A map of globally visible named
54 // entities for this unit.
55 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
56 // Tracks the mapping of unit level
57 // debug informaton descriptors to debug
58 // information entries.
61 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
72 CompileUnitDesc *getDesc() const { return Desc; }
73 unsigned getID() const { return ID; }
74 DIE* getDie() const { return Die; }
75 std::map<std::string, DIE *> &getGlobals() { return Globals; }
77 /// hasContent - Return true if this compile unit has something to write out.
79 bool hasContent() const;
81 /// AddGlobal - Add a new global entity to the compile unit.
83 void AddGlobal(const std::string &Name, DIE *Die);
85 /// getDieMapSlotFor - Returns the debug information entry map slot for the
86 /// specified debug descriptor.
87 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
88 return DescToDieMap[DD];
92 //===----------------------------------------------------------------------===//
93 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
94 // Dwarf abbreviation.
97 unsigned Attribute; // Dwarf attribute code.
98 unsigned Form; // Dwarf form code.
101 DIEAbbrevData(unsigned A, unsigned F)
107 unsigned getAttribute() const { return Attribute; }
108 unsigned getForm() const { return Form; }
110 /// operator== - Used by DIEAbbrev to locate entry.
112 bool operator==(const DIEAbbrevData &DAD) const {
113 return Attribute == DAD.Attribute && Form == DAD.Form;
116 /// operator!= - Used by DIEAbbrev to locate entry.
118 bool operator!=(const DIEAbbrevData &DAD) const {
119 return Attribute != DAD.Attribute || Form != DAD.Form;
122 /// operator< - Used by DIEAbbrev to locate entry.
124 bool operator<(const DIEAbbrevData &DAD) const {
125 return Attribute < DAD.Attribute ||
126 (Attribute == DAD.Attribute && Form < DAD.Form);
130 //===----------------------------------------------------------------------===//
131 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
132 // information object.
135 unsigned Tag; // Dwarf tag code.
136 unsigned ChildrenFlag; // Dwarf children flag.
137 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
141 DIEAbbrev(unsigned T, unsigned C)
149 unsigned getTag() const { return Tag; }
150 unsigned getChildrenFlag() const { return ChildrenFlag; }
151 const std::vector<DIEAbbrevData> &getData() const { return Data; }
152 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
154 /// operator== - Used by UniqueVector to locate entry.
156 bool operator==(const DIEAbbrev &DA) const;
158 /// operator< - Used by UniqueVector to locate entry.
160 bool operator<(const DIEAbbrev &DA) const;
162 /// AddAttribute - Adds another set of attribute information to the
164 void AddAttribute(unsigned Attribute, unsigned Form) {
165 Data.push_back(DIEAbbrevData(Attribute, Form));
168 /// AddFirstAttribute - Adds a set of attribute information to the front
169 /// of the abbreviation.
170 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
171 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
174 /// Emit - Print the abbreviation using the specified Dwarf writer.
176 void Emit(const DwarfWriter &DW) const;
179 void print(std::ostream &O);
184 //===----------------------------------------------------------------------===//
185 // DIEValue - A debug information entry value.
199 unsigned Type; // Type of the value
201 DIEValue(unsigned T) : Type(T) {}
202 virtual ~DIEValue() {}
204 // Implement isa/cast/dyncast.
205 static bool classof(const DIEValue *) { return true; }
207 /// EmitValue - Emit value via the Dwarf writer.
209 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
211 /// SizeOf - Return the size of a value in bytes.
213 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
216 //===----------------------------------------------------------------------===//
217 // DWInteger - An integer value DIE.
219 class DIEInteger : public DIEValue {
224 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
226 // Implement isa/cast/dyncast.
227 static bool classof(const DIEInteger *) { return true; }
228 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
230 /// BestForm - Choose the best form for integer.
232 unsigned BestForm(bool IsSigned);
234 /// EmitValue - Emit integer of appropriate size.
236 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
238 /// SizeOf - Determine size of integer value in bytes.
240 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
243 //===----------------------------------------------------------------------===//
244 // DIEString - A string value DIE.
246 struct DIEString : public DIEValue {
247 const std::string String;
249 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
251 // Implement isa/cast/dyncast.
252 static bool classof(const DIEString *) { return true; }
253 static bool classof(const DIEValue *S) { return S->Type == isString; }
255 /// EmitValue - Emit string value.
257 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
259 /// SizeOf - Determine size of string value in bytes.
261 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
264 //===----------------------------------------------------------------------===//
265 // DIEDwarfLabel - A Dwarf internal label expression DIE.
267 struct DIEDwarfLabel : public DIEValue {
270 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
272 // Implement isa/cast/dyncast.
273 static bool classof(const DIEDwarfLabel *) { return true; }
274 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
276 /// EmitValue - Emit label value.
278 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
280 /// SizeOf - Determine size of label value in bytes.
282 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
286 //===----------------------------------------------------------------------===//
287 // DIEObjectLabel - A label to an object in code or data.
289 struct DIEObjectLabel : public DIEValue {
290 const std::string Label;
292 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
294 // Implement isa/cast/dyncast.
295 static bool classof(const DIEObjectLabel *) { return true; }
296 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
298 /// EmitValue - Emit label value.
300 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
302 /// SizeOf - Determine size of label value in bytes.
304 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
307 //===----------------------------------------------------------------------===//
308 // DIEDelta - A simple label difference DIE.
310 struct DIEDelta : public DIEValue {
311 const DWLabel LabelHi;
312 const DWLabel LabelLo;
314 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
315 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
317 // Implement isa/cast/dyncast.
318 static bool classof(const DIEDelta *) { return true; }
319 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
321 /// EmitValue - Emit delta value.
323 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
325 /// SizeOf - Determine size of delta value in bytes.
327 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
330 //===----------------------------------------------------------------------===//
331 // DIEntry - A pointer to a debug information entry.
333 struct DIEntry : public DIEValue {
336 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
338 // Implement isa/cast/dyncast.
339 static bool classof(const DIEntry *) { return true; }
340 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
342 /// EmitValue - Emit debug information entry offset.
344 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
346 /// SizeOf - Determine size of debug information entry in bytes.
348 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
351 //===----------------------------------------------------------------------===//
352 // DIEBlock - A block of values. Primarily used for location expressions.
354 struct DIEBlock : public DIEValue {
355 unsigned Size; // Size in bytes excluding size header.
356 std::vector<unsigned> Forms; // Data forms.
357 std::vector<DIEValue *> Values; // Block values.
367 // Implement isa/cast/dyncast.
368 static bool classof(const DIEBlock *) { return true; }
369 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
371 /// ComputeSize - calculate the size of the block.
373 unsigned ComputeSize(DwarfWriter &DW);
375 /// BestForm - Choose the best form for data.
379 /// EmitValue - Emit block data.
381 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
383 /// SizeOf - Determine size of block data in bytes.
385 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
387 /// AddUInt - Add an unsigned integer value.
389 void AddUInt(unsigned Form, uint64_t Integer);
391 /// AddSInt - Add an signed integer value.
393 void AddSInt(unsigned Form, int64_t Integer);
395 /// AddString - Add a std::string value.
397 void AddString(unsigned Form, const std::string &String);
399 /// AddLabel - Add a Dwarf label value.
401 void AddLabel(unsigned Form, const DWLabel &Label);
403 /// AddObjectLabel - Add a non-Dwarf label value.
405 void AddObjectLabel(unsigned Form, const std::string &Label);
407 /// AddDelta - Add a label delta value.
409 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
411 /// AddDIEntry - Add a DIE value.
413 void AddDIEntry(unsigned Form, DIE *Entry);
417 //===----------------------------------------------------------------------===//
418 // DIE - A structured debug information entry. Has an abbreviation which
419 // describes it's organization.
422 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
423 unsigned AbbrevID; // Decribing abbreviation ID.
424 unsigned Offset; // Offset in debug info section.
425 unsigned Size; // Size of instance + children.
426 std::vector<DIE *> Children; // Children DIEs.
427 std::vector<DIEValue *> Values; // Attributes values.
434 unsigned getAbbrevID() const { return AbbrevID; }
435 unsigned getOffset() const { return Offset; }
436 unsigned getSize() const { return Size; }
437 const std::vector<DIE *> &getChildren() const { return Children; }
438 const std::vector<DIEValue *> &getValues() const { return Values; }
439 void setOffset(unsigned O) { Offset = O; }
440 void setSize(unsigned S) { Size = S; }
442 /// SiblingOffset - Return the offset of the debug information entry's
444 unsigned SiblingOffset() const { return Offset + Size; }
446 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
448 void AddSiblingOffset();
450 /// AddUInt - Add an unsigned integer attribute data and value.
452 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
454 /// AddSInt - Add an signed integer attribute data and value.
456 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
458 /// AddString - Add a std::string attribute data and value.
460 void AddString(unsigned Attribute, unsigned Form,
461 const std::string &String);
463 /// AddLabel - Add a Dwarf label attribute data and value.
465 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
467 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
469 void AddObjectLabel(unsigned Attribute, unsigned Form,
470 const std::string &Label);
472 /// AddDelta - Add a label delta attribute data and value.
474 void AddDelta(unsigned Attribute, unsigned Form,
475 const DWLabel &Hi, const DWLabel &Lo);
477 /// AddDIEntry - Add a DIE attribute data and value.
479 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
481 /// AddBlock - Add block data.
483 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
485 /// Complete - Indicate that all attributes have been added and
486 /// ready to get an abbreviation ID.
488 void Complete(DwarfWriter &DW);
490 /// AddChild - Add a child to the DIE.
491 void AddChild(DIE *Child);
494 } // End of namespace llvm
496 //===----------------------------------------------------------------------===//
498 CompileUnit::~CompileUnit() {
502 /// hasContent - Return true if this compile unit has something to write out.
504 bool CompileUnit::hasContent() const {
505 return !Die->getChildren().empty();
508 /// AddGlobal - Add a new global entity to the compile unit.
510 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
514 //===----------------------------------------------------------------------===//
516 /// operator== - Used by UniqueVector to locate entry.
518 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
519 if (Tag != DA.Tag) return false;
520 if (ChildrenFlag != DA.ChildrenFlag) return false;
521 if (Data.size() != DA.Data.size()) return false;
523 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
524 if (Data[i] != DA.Data[i]) return false;
530 /// operator< - Used by UniqueVector to locate entry.
532 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
533 if (Tag != DA.Tag) return Tag < DA.Tag;
534 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
535 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
537 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
538 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
544 /// Emit - Print the abbreviation using the specified Dwarf writer.
546 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
547 // Emit its Dwarf tag type.
548 DW.EmitULEB128Bytes(Tag);
549 DW.EOL(TagString(Tag));
551 // Emit whether it has children DIEs.
552 DW.EmitULEB128Bytes(ChildrenFlag);
553 DW.EOL(ChildrenString(ChildrenFlag));
555 // For each attribute description.
556 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
557 const DIEAbbrevData &AttrData = Data[i];
559 // Emit attribute type.
560 DW.EmitULEB128Bytes(AttrData.getAttribute());
561 DW.EOL(AttributeString(AttrData.getAttribute()));
564 DW.EmitULEB128Bytes(AttrData.getForm());
565 DW.EOL(FormEncodingString(AttrData.getForm()));
568 // Mark end of abbreviation.
569 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
570 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
574 void DIEAbbrev::print(std::ostream &O) {
575 O << "Abbreviation @"
576 << std::hex << (intptr_t)this << std::dec
580 << ChildrenString(ChildrenFlag)
583 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
585 << AttributeString(Data[i].getAttribute())
587 << FormEncodingString(Data[i].getForm())
591 void DIEAbbrev::dump() { print(std::cerr); }
594 //===----------------------------------------------------------------------===//
596 /// BestForm - Choose the best form for integer.
598 unsigned DIEInteger::BestForm(bool IsSigned) {
600 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
601 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
602 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
604 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
605 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
606 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
608 return DW_FORM_data8;
611 /// EmitValue - Emit integer of appropriate size.
613 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
615 case DW_FORM_flag: // Fall thru
616 case DW_FORM_ref1: // Fall thru
617 case DW_FORM_data1: DW.EmitInt8(Integer); break;
618 case DW_FORM_ref2: // Fall thru
619 case DW_FORM_data2: DW.EmitInt16(Integer); break;
620 case DW_FORM_ref4: // Fall thru
621 case DW_FORM_data4: DW.EmitInt32(Integer); break;
622 case DW_FORM_ref8: // Fall thru
623 case DW_FORM_data8: DW.EmitInt64(Integer); break;
624 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
625 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
626 default: assert(0 && "DIE Value form not supported yet"); break;
630 /// SizeOf - Determine size of integer value in bytes.
632 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
634 case DW_FORM_flag: // Fall thru
635 case DW_FORM_ref1: // Fall thru
636 case DW_FORM_data1: return sizeof(int8_t);
637 case DW_FORM_ref2: // Fall thru
638 case DW_FORM_data2: return sizeof(int16_t);
639 case DW_FORM_ref4: // Fall thru
640 case DW_FORM_data4: return sizeof(int32_t);
641 case DW_FORM_ref8: // Fall thru
642 case DW_FORM_data8: return sizeof(int64_t);
643 case DW_FORM_udata: return DW.SizeULEB128(Integer);
644 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
645 default: assert(0 && "DIE Value form not supported yet"); break;
650 //===----------------------------------------------------------------------===//
652 /// EmitValue - Emit string value.
654 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
655 DW.EmitString(String);
658 /// SizeOf - Determine size of string value in bytes.
660 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
661 return String.size() + sizeof(char); // sizeof('\0');
664 //===----------------------------------------------------------------------===//
666 /// EmitValue - Emit label value.
668 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
669 DW.EmitReference(Label);
672 /// SizeOf - Determine size of label value in bytes.
674 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
675 return DW.getAddressSize();
678 //===----------------------------------------------------------------------===//
680 /// EmitValue - Emit label value.
682 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
683 DW.EmitReference(Label);
686 /// SizeOf - Determine size of label value in bytes.
688 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
689 return DW.getAddressSize();
692 //===----------------------------------------------------------------------===//
694 /// EmitValue - Emit delta value.
696 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
697 DW.EmitDifference(LabelHi, LabelLo);
700 /// SizeOf - Determine size of delta value in bytes.
702 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
703 return DW.getAddressSize();
706 //===----------------------------------------------------------------------===//
707 /// EmitValue - Emit debug information entry offset.
709 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
710 DW.EmitInt32(Entry->getOffset());
713 /// SizeOf - Determine size of debug information entry value in bytes.
715 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
716 return sizeof(int32_t);
719 //===----------------------------------------------------------------------===//
721 DIEBlock::~DIEBlock() {
722 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
727 /// ComputeSize - calculate the size of the block.
729 unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
731 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
732 Size += Values[i]->SizeOf(DW, Forms[i]);
737 /// BestForm - Choose the best form for data.
739 unsigned DIEBlock::BestForm() {
740 if ((unsigned char)Size == Size) return DW_FORM_block1;
741 if ((unsigned short)Size == Size) return DW_FORM_block2;
742 if ((unsigned int)Size == Size) return DW_FORM_block4;
743 return DW_FORM_block;
746 /// EmitValue - Emit block data.
748 void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
750 case DW_FORM_block1: DW.EmitInt8(Size); break;
751 case DW_FORM_block2: DW.EmitInt16(Size); break;
752 case DW_FORM_block4: DW.EmitInt32(Size); break;
753 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
754 default: assert(0 && "Improper form for block"); break;
756 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
758 Values[i]->EmitValue(DW, Forms[i]);
762 /// SizeOf - Determine size of block data in bytes.
764 unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
766 case DW_FORM_block1: return Size + sizeof(int8_t);
767 case DW_FORM_block2: return Size + sizeof(int16_t);
768 case DW_FORM_block4: return Size + sizeof(int32_t);
769 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
770 default: assert(0 && "Improper form for block"); break;
775 /// AddUInt - Add an unsigned integer value.
777 void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
778 DIEInteger *DI = new DIEInteger(Integer);
779 Values.push_back(DI);
780 if (Form == 0) Form = DI->BestForm(false);
781 Forms.push_back(Form);
784 /// AddSInt - Add an signed integer value.
786 void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
787 DIEInteger *DI = new DIEInteger(Integer);
788 Values.push_back(DI);
789 if (Form == 0) Form = DI->BestForm(true);
790 Forms.push_back(Form);
793 /// AddString - Add a std::string value.
795 void DIEBlock::AddString(unsigned Form, const std::string &String) {
796 Values.push_back(new DIEString(String));
797 Forms.push_back(Form);
800 /// AddLabel - Add a Dwarf label value.
802 void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
803 Values.push_back(new DIEDwarfLabel(Label));
804 Forms.push_back(Form);
807 /// AddObjectLabel - Add a non-Dwarf label value.
809 void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
810 Values.push_back(new DIEObjectLabel(Label));
811 Forms.push_back(Form);
814 /// AddDelta - Add a label delta value.
816 void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
817 Values.push_back(new DIEDelta(Hi, Lo));
818 Forms.push_back(Form);
821 /// AddDIEntry - Add a DIE value.
823 void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
824 Values.push_back(new DIEntry(Entry));
825 Forms.push_back(Form);
828 //===----------------------------------------------------------------------===//
830 DIE::DIE(unsigned Tag)
831 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
840 if (Abbrev) delete Abbrev;
842 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
846 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
851 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
853 void DIE::AddSiblingOffset() {
854 DIEInteger *DI = new DIEInteger(0);
855 Values.insert(Values.begin(), DI);
856 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
859 /// AddUInt - Add an unsigned integer attribute data and value.
861 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
862 DIEInteger *DI = new DIEInteger(Integer);
863 Values.push_back(DI);
864 if (!Form) Form = DI->BestForm(false);
865 Abbrev->AddAttribute(Attribute, Form);
868 /// AddSInt - Add an signed integer attribute data and value.
870 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
871 DIEInteger *DI = new DIEInteger(Integer);
872 Values.push_back(DI);
873 if (!Form) Form = DI->BestForm(true);
874 Abbrev->AddAttribute(Attribute, Form);
877 /// AddString - Add a std::string attribute data and value.
879 void DIE::AddString(unsigned Attribute, unsigned Form,
880 const std::string &String) {
881 Values.push_back(new DIEString(String));
882 Abbrev->AddAttribute(Attribute, Form);
885 /// AddLabel - Add a Dwarf label attribute data and value.
887 void DIE::AddLabel(unsigned Attribute, unsigned Form,
888 const DWLabel &Label) {
889 Values.push_back(new DIEDwarfLabel(Label));
890 Abbrev->AddAttribute(Attribute, Form);
893 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
895 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
896 const std::string &Label) {
897 Values.push_back(new DIEObjectLabel(Label));
898 Abbrev->AddAttribute(Attribute, Form);
901 /// AddDelta - Add a label delta attribute data and value.
903 void DIE::AddDelta(unsigned Attribute, unsigned Form,
904 const DWLabel &Hi, const DWLabel &Lo) {
905 Values.push_back(new DIEDelta(Hi, Lo));
906 Abbrev->AddAttribute(Attribute, Form);
909 /// AddDIEntry - Add a DIE attribute data and value.
911 void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
912 Values.push_back(new DIEntry(Entry));
913 Abbrev->AddAttribute(Attribute, Form);
916 /// AddBlock - Add block data.
918 void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
919 assert(Block->Size && "Block size has not been computed");
920 Values.push_back(Block);
921 if (!Form) Form = Block->BestForm();
922 Abbrev->AddAttribute(Attribute, Form);
925 /// Complete - Indicate that all attributes have been added and ready to get an
927 void DIE::Complete(DwarfWriter &DW) {
928 AbbrevID = DW.NewAbbreviation(Abbrev);
933 /// AddChild - Add a child to the DIE.
935 void DIE::AddChild(DIE *Child) {
936 assert(Abbrev && "Adding children without an abbreviation");
937 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
938 Children.push_back(Child);
941 //===----------------------------------------------------------------------===//
945 //===----------------------------------------------------------------------===//
947 /// PrintHex - Print a value as a hexidecimal value.
949 void DwarfWriter::PrintHex(int Value) const {
950 O << "0x" << std::hex << Value << std::dec;
953 /// EOL - Print a newline character to asm stream. If a comment is present
954 /// then it will be printed first. Comments should not contain '\n'.
955 void DwarfWriter::EOL(const std::string &Comment) const {
956 if (DwarfVerbose && !Comment.empty()) {
958 << Asm->CommentString
965 /// EmitAlign - Print a align directive.
967 void DwarfWriter::EmitAlign(unsigned Alignment) const {
968 O << Asm->AlignDirective << Alignment << "\n";
971 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
972 /// unsigned leb128 value.
973 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
978 O << Asm->Data8bitsDirective;
983 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
984 /// signed leb128 value.
985 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
990 O << Asm->Data8bitsDirective;
995 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
996 /// representing an unsigned leb128 value.
997 void DwarfWriter::PrintULEB128(unsigned Value) const {
999 unsigned Byte = Value & 0x7f;
1001 if (Value) Byte |= 0x80;
1003 if (Value) O << ", ";
1007 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1009 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1013 Size += sizeof(int8_t);
1018 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
1019 /// representing a signed leb128 value.
1020 void DwarfWriter::PrintSLEB128(int Value) const {
1021 int Sign = Value >> (8 * sizeof(Value) - 1);
1025 unsigned Byte = Value & 0x7f;
1027 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1028 if (IsMore) Byte |= 0x80;
1030 if (IsMore) O << ", ";
1034 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1036 unsigned DwarfWriter::SizeSLEB128(int Value) {
1038 int Sign = Value >> (8 * sizeof(Value) - 1);
1042 unsigned Byte = Value & 0x7f;
1044 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1045 Size += sizeof(int8_t);
1050 /// EmitInt8 - Emit a byte directive and value.
1052 void DwarfWriter::EmitInt8(int Value) const {
1053 O << Asm->Data8bitsDirective;
1054 PrintHex(Value & 0xFF);
1057 /// EmitInt16 - Emit a short directive and value.
1059 void DwarfWriter::EmitInt16(int Value) const {
1060 O << Asm->Data16bitsDirective;
1061 PrintHex(Value & 0xFFFF);
1064 /// EmitInt32 - Emit a long directive and value.
1066 void DwarfWriter::EmitInt32(int Value) const {
1067 O << Asm->Data32bitsDirective;
1071 /// EmitInt64 - Emit a long long directive and value.
1073 void DwarfWriter::EmitInt64(uint64_t Value) const {
1074 if (Asm->Data64bitsDirective) {
1075 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1077 const TargetData &TD = Asm->TM.getTargetData();
1079 if (TD.isBigEndian()) {
1080 EmitInt32(unsigned(Value >> 32)); O << "\n";
1081 EmitInt32(unsigned(Value));
1083 EmitInt32(unsigned(Value)); O << "\n";
1084 EmitInt32(unsigned(Value >> 32));
1089 /// EmitString - Emit a string with quotes and a null terminator.
1090 /// Special characters are emitted properly. (Eg. '\t')
1091 void DwarfWriter::EmitString(const std::string &String) const {
1092 O << Asm->AsciiDirective
1094 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1095 unsigned char C = String[i];
1097 if (!isascii(C) || iscntrl(C)) {
1099 case '\b': O << "\\b"; break;
1100 case '\f': O << "\\f"; break;
1101 case '\n': O << "\\n"; break;
1102 case '\r': O << "\\r"; break;
1103 case '\t': O << "\\t"; break;
1106 O << char('0' + (C >> 6));
1107 O << char('0' + (C >> 3));
1108 O << char('0' + (C >> 0));
1111 } else if (C == '\"') {
1113 } else if (C == '\'') {
1122 /// PrintLabelName - Print label name in form used by Dwarf writer.
1124 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
1125 O << Asm->PrivateGlobalPrefix
1128 if (Number) O << Number;
1131 /// EmitLabel - Emit location label for internal use by Dwarf.
1133 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1134 PrintLabelName(Tag, Number);
1138 /// EmitReference - Emit a reference to a label.
1140 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
1141 if (AddressSize == 4)
1142 O << Asm->Data32bitsDirective;
1144 O << Asm->Data64bitsDirective;
1146 PrintLabelName(Tag, Number);
1148 void DwarfWriter::EmitReference(const std::string &Name) const {
1149 if (AddressSize == 4)
1150 O << Asm->Data32bitsDirective;
1152 O << Asm->Data64bitsDirective;
1157 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1158 /// assemblers do not accept absolute expressions with data directives, so there
1159 /// is an option (needsSet) to use an intermediary 'set' expression.
1160 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1161 const char *TagLo, unsigned NumberLo) const {
1163 static unsigned SetCounter = 0;
1166 PrintLabelName("set", SetCounter);
1168 PrintLabelName(TagHi, NumberHi);
1170 PrintLabelName(TagLo, NumberLo);
1173 if (AddressSize == sizeof(int32_t))
1174 O << Asm->Data32bitsDirective;
1176 O << Asm->Data64bitsDirective;
1178 PrintLabelName("set", SetCounter);
1182 if (AddressSize == sizeof(int32_t))
1183 O << Asm->Data32bitsDirective;
1185 O << Asm->Data64bitsDirective;
1187 PrintLabelName(TagHi, NumberHi);
1189 PrintLabelName(TagLo, NumberLo);
1193 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
1195 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1196 return Abbreviations.insert(*Abbrev);
1199 /// NewString - Add a string to the constant pool and returns a label.
1201 DWLabel DwarfWriter::NewString(const std::string &String) {
1202 unsigned StringID = StringPool.insert(String);
1203 return DWLabel("string", StringID);
1206 /// AddSourceLine - Add location information to specified debug information
1208 void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
1210 CompileUnit *FileUnit = FindCompileUnit(File);
1211 unsigned FileID = FileUnit->getID();
1212 Die->AddUInt(DW_AT_decl_file, 0, FileID);
1213 Die->AddUInt(DW_AT_decl_line, 0, Line);
1217 /// getDieMapSlotFor - Returns the debug information entry map slot for the
1218 /// specified debug descriptor.
1219 DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1220 return DescToDieMap[DD];
1223 /// NewType - Create a new type DIE.
1225 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1227 // FIXME - Hack for missing types
1228 DIE *Die = new DIE(DW_TAG_base_type);
1229 Die->AddUInt(DW_AT_byte_size, 0, 4);
1230 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1231 Unit->getDie()->AddChild(Die);
1235 // FIXME - Should handle other contexts that compile units.
1237 // Check for pre-existence.
1238 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
1239 if (Slot) return Slot;
1241 // Get core information.
1242 const std::string &Name = TyDesc->getName();
1243 uint64_t Size = TyDesc->getSize() >> 3;
1247 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1248 // Fundamental types like int, float, bool
1249 Slot = Ty = new DIE(DW_TAG_base_type);
1250 unsigned Encoding = BasicTy->getEncoding();
1251 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1252 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1253 // Create specific DIE.
1254 Slot = Ty = new DIE(DerivedTy->getTag());
1256 // Map to main type, void will not have a type.
1257 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1258 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1259 NewType(Context, FromTy, Unit));
1261 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1262 // Create specific DIE.
1263 Slot = Ty = new DIE(CompTy->getTag());
1264 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1266 switch (CompTy->getTag()) {
1267 case DW_TAG_array_type: {
1268 // Add element type.
1269 if (TypeDesc *FromTy = CompTy->getFromType()) {
1270 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1271 NewType(Context, FromTy, Unit));
1273 // Don't emit size attribute.
1276 // Construct an anonymous type for index type.
1277 DIE *IndexTy = new DIE(DW_TAG_base_type);
1278 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1279 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1281 Context->AddChild(IndexTy);
1283 // Add subranges to array type.
1284 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1285 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1286 int64_t Lo = SRD->getLo();
1287 int64_t Hi = SRD->getHi();
1288 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1290 // If a range is available.
1292 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1293 // Only add low if non-zero.
1294 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1295 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
1297 Ty->AddChild(Subrange);
1302 case DW_TAG_structure_type:
1303 case DW_TAG_union_type: {
1304 // FIXME - this is just the basics.
1305 // Add elements to structure type.
1306 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1307 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
1309 // Extract the basic information.
1310 const std::string &Name = MemberDesc->getName();
1311 TypeDesc *MemTy = MemberDesc->getFromType();
1312 uint64_t Size = MemberDesc->getSize();
1313 uint64_t Align = MemberDesc->getAlign();
1314 uint64_t Offset = MemberDesc->getOffset();
1316 // Construct member debug information entry.
1317 DIE *Member = new DIE(DW_TAG_member);
1319 // Add name if not "".
1320 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
1321 // Add location if available.
1322 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1324 // Most of the time the field info is the same as the members.
1325 uint64_t FieldSize = Size;
1326 uint64_t FieldAlign = Align;
1327 uint64_t FieldOffset = Offset;
1329 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1330 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1331 NewType(Context, FromTy, Unit));
1332 FieldSize = FromTy->getSize();
1333 FieldAlign = FromTy->getSize();
1336 // Unless we have a bit field.
1337 if (FieldSize != Size) {
1338 // Construct the alignment mask.
1339 uint64_t AlignMask = ~(FieldAlign - 1);
1340 // Determine the high bit + 1 of the declared size.
1341 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1342 // Work backwards to determine the base offset of the field.
1343 FieldOffset = HiMark - FieldSize;
1344 // Now normalize offset to the field.
1345 Offset -= FieldOffset;
1347 // Maybe we need to work from the other.
1348 const TargetData &TD = Asm->TM.getTargetData();
1349 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1351 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1352 Member->AddUInt(DW_AT_bit_size, 0, Size);
1353 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1356 // Add computation for offset.
1357 DIEBlock *Block = new DIEBlock();
1358 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1359 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1360 Block->ComputeSize(*this);
1361 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1363 Ty->AddChild(Member);
1367 case DW_TAG_enumeration_type: {
1368 // Add enumerators to enumeration type.
1369 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1370 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1371 const std::string &Name = ED->getName();
1372 int64_t Value = ED->getValue();
1373 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1374 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1375 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1376 Ty->AddChild(Enumerator);
1385 assert(Ty && "Type not supported yet");
1387 // Add size if non-zero (derived types don't have a size.)
1388 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1389 // Add name if not anonymous or intermediate type.
1390 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1391 // Add source line info if available.
1392 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
1394 // Add to context owner.
1395 Context->AddChild(Ty);
1400 /// NewCompileUnit - Create new compile unit and it's debug information entry.
1402 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1404 // Construct debug information entry.
1405 DIE *Die = new DIE(DW_TAG_compile_unit);
1406 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1407 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1408 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1409 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1410 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1411 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1412 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1414 // Add debug information entry to descriptor map.
1415 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1418 // Construct compile unit.
1419 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1421 // Add Unit to compile unit map.
1422 DescToUnitMap[UnitDesc] = Unit;
1427 /// FindCompileUnit - Get the compile unit for the given descriptor.
1429 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1430 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1431 assert(Unit && "Missing compile unit.");
1435 /// NewGlobalVariable - Add a new global variable DIE.
1437 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1438 // Get the compile unit context.
1439 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1440 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1442 // Check for pre-existence.
1443 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1444 if (Slot) return Slot;
1446 // Get the global variable itself.
1447 GlobalVariable *GV = GVD->getGlobalVariable();
1448 // Generate the mangled name.
1449 std::string MangledName = Asm->Mang->getValueName(GV);
1451 // Gather the details (simplify add attribute code.)
1452 const std::string &Name = GVD->getName();
1454 // Get the global's type.
1455 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
1457 // Create the globale variable DIE.
1458 DIE *VariableDie = new DIE(DW_TAG_variable);
1459 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1460 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1461 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1463 // Add source line info if available.
1464 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1467 DIEBlock *Block = new DIEBlock();
1468 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1469 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1470 Block->ComputeSize(*this);
1471 VariableDie->AddBlock(DW_AT_location, 0, Block);
1476 // Add to context owner.
1477 Unit->getDie()->AddChild(VariableDie);
1479 // Expose as global.
1480 // FIXME - need to check external flag.
1481 Unit->AddGlobal(Name, VariableDie);
1486 /// NewSubprogram - Add a new subprogram DIE.
1488 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1489 // Get the compile unit context.
1490 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1491 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1493 // Check for pre-existence.
1494 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1495 if (Slot) return Slot;
1497 // Gather the details (simplify add attribute code.)
1498 const std::string &Name = SPD->getName();
1499 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
1500 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1502 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1503 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1504 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1505 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1507 // Add source line info if available.
1508 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1511 Slot = SubprogramDie;
1513 // Add to context owner.
1514 Unit->getDie()->AddChild(SubprogramDie);
1516 // Expose as global.
1517 Unit->AddGlobal(Name, SubprogramDie);
1519 return SubprogramDie;
1523 /// NewScopeVariable - Create a new scope variable.
1525 DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1526 // Get the descriptor.
1527 VariableDesc *VD = DV->getDesc();
1529 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1531 switch (VD->getTag()) {
1532 case DW_TAG_return_variable: return NULL;
1533 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1534 case DW_TAG_auto_variable: // fall thru
1535 default: Tag = DW_TAG_variable; break;
1538 // Define variable debug information entry.
1539 DIE *VariableDie = new DIE(Tag);
1540 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1542 // Add source line info if available.
1543 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1545 // Add variable type.
1546 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
1547 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1549 // Get variable address.
1550 MachineLocation Location;
1551 Asm->TM.getRegisterInfo()->getLocation(*MF, DV->getFrameIndex(), Location);
1553 // Add computation for variable.
1554 DIEBlock *Block = new DIEBlock();
1555 if (Location.isRegister()) {
1556 // FIXME - This is a real hack.
1557 Block->AddUInt(DW_FORM_data1, DW_OP_reg0 + Location.getRegister());
1559 // FIXME - This is a real hack.
1560 Block->AddUInt(DW_FORM_data1, DW_OP_breg0 + Location.getRegister());
1561 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1563 Block->ComputeSize(*this);
1564 VariableDie->AddBlock(DW_AT_location, 0, Block);
1569 /// ConstructScope - Construct the components of a scope.
1571 void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1572 DIE *ParentDie, CompileUnit *Unit) {
1573 // Add variables to scope.
1574 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1575 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1576 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1577 if (VariableDie) ParentDie->AddChild(VariableDie);
1580 // Add nested scopes.
1581 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1582 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1583 // Define the Scope debug information entry.
1584 DebugScope *Scope = Scopes[j];
1585 // FIXME - Ignore inlined functions for the time being.
1586 if (Scope->getParent()) continue;
1588 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1590 // Add the scope bounds.
1591 if (unsigned StartID = Scope->getStartLabelID()) {
1592 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1593 DWLabel("loc", StartID));
1595 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1596 DWLabel("func_begin", SubprogramCount));
1598 if (unsigned EndID = Scope->getEndLabelID()) {
1599 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1600 DWLabel("loc", EndID));
1602 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1603 DWLabel("func_end", SubprogramCount));
1606 // Add the scope contents.
1607 ConstructScope(Scope, ScopeDie, Unit);
1608 ParentDie->AddChild(ScopeDie);
1612 /// ConstructRootScope - Construct the scope for the subprogram.
1614 void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1615 // Exit if there is no root scope.
1616 if (!RootScope) return;
1618 // Get the subprogram debug information entry.
1619 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1621 // Get the compile unit context.
1622 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1623 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1625 // Get the subprogram die.
1626 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1627 assert(SPDie && "Missing subprogram descriptor");
1629 // Add the function bounds.
1630 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1631 DWLabel("func_begin", SubprogramCount));
1632 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1633 DWLabel("func_end", SubprogramCount));
1635 ConstructScope(RootScope, SPDie, Unit);
1638 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1639 /// tools to recognize the object file contains Dwarf information.
1641 void DwarfWriter::EmitInitial() const {
1642 // Dwarf sections base addresses.
1643 Asm->SwitchSection(DwarfFrameSection, 0);
1644 EmitLabel("section_frame", 0);
1645 Asm->SwitchSection(DwarfInfoSection, 0);
1646 EmitLabel("section_info", 0);
1647 EmitLabel("info", 0);
1648 Asm->SwitchSection(DwarfAbbrevSection, 0);
1649 EmitLabel("section_abbrev", 0);
1650 EmitLabel("abbrev", 0);
1651 Asm->SwitchSection(DwarfARangesSection, 0);
1652 EmitLabel("section_aranges", 0);
1653 Asm->SwitchSection(DwarfMacInfoSection, 0);
1654 EmitLabel("section_macinfo", 0);
1655 Asm->SwitchSection(DwarfLineSection, 0);
1656 EmitLabel("section_line", 0);
1657 EmitLabel("line", 0);
1658 Asm->SwitchSection(DwarfLocSection, 0);
1659 EmitLabel("section_loc", 0);
1660 Asm->SwitchSection(DwarfPubNamesSection, 0);
1661 EmitLabel("section_pubnames", 0);
1662 Asm->SwitchSection(DwarfStrSection, 0);
1663 EmitLabel("section_str", 0);
1664 Asm->SwitchSection(DwarfRangesSection, 0);
1665 EmitLabel("section_ranges", 0);
1667 Asm->SwitchSection(TextSection, 0);
1668 EmitLabel("text_begin", 0);
1669 Asm->SwitchSection(DataSection, 0);
1670 EmitLabel("data_begin", 0);
1673 /// EmitDIE - Recusively Emits a debug information entry.
1675 void DwarfWriter::EmitDIE(DIE *Die) const {
1676 // Get the abbreviation for this DIE.
1677 unsigned AbbrevID = Die->getAbbrevID();
1678 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1682 // Emit the code (index) for the abbreviation.
1683 EmitULEB128Bytes(AbbrevID);
1684 EOL(std::string("Abbrev [" +
1686 "] 0x" + utohexstr(Die->getOffset()) +
1687 ":0x" + utohexstr(Die->getSize()) + " " +
1688 TagString(Abbrev.getTag())));
1690 const std::vector<DIEValue *> &Values = Die->getValues();
1691 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1693 // Emit the DIE attribute values.
1694 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1695 unsigned Attr = AbbrevData[i].getAttribute();
1696 unsigned Form = AbbrevData[i].getForm();
1697 assert(Form && "Too many attributes for DIE (check abbreviation)");
1700 case DW_AT_sibling: {
1701 EmitInt32(Die->SiblingOffset());
1705 // Emit an attribute using the defined form.
1706 Values[i]->EmitValue(*this, Form);
1711 EOL(AttributeString(Attr));
1714 // Emit the DIE children if any.
1715 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1716 const std::vector<DIE *> &Children = Die->getChildren();
1718 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1719 EmitDIE(Children[j]);
1722 EmitInt8(0); EOL("End Of Children Mark");
1726 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1728 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1729 // Get the children.
1730 const std::vector<DIE *> &Children = Die->getChildren();
1732 // If not last sibling and has children then add sibling offset attribute.
1733 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1735 // Record the abbreviation.
1736 Die->Complete(*this);
1738 // Get the abbreviation for this DIE.
1739 unsigned AbbrevID = Die->getAbbrevID();
1740 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1743 Die->setOffset(Offset);
1745 // Start the size with the size of abbreviation code.
1746 Offset += SizeULEB128(AbbrevID);
1748 const std::vector<DIEValue *> &Values = Die->getValues();
1749 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1751 // Emit the DIE attribute values.
1752 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1753 // Size attribute value.
1754 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1757 // Emit the DIE children if any.
1758 if (!Children.empty()) {
1759 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1760 "Children flag not set");
1762 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1763 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1766 // End of children marker.
1767 Offset += sizeof(int8_t);
1770 Die->setSize(Offset - Die->getOffset());
1774 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1776 void DwarfWriter::SizeAndOffsets() {
1778 // Process each compile unit.
1779 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1780 CompileUnit *Unit = CompileUnits[i];
1781 if (Unit->hasContent()) {
1782 // Compute size of compile unit header
1783 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1784 sizeof(int16_t) + // DWARF version number
1785 sizeof(int32_t) + // Offset Into Abbrev. Section
1786 sizeof(int8_t); // Pointer Size (in bytes)
1787 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
1792 /// EmitDebugInfo - Emit the debug info section.
1794 void DwarfWriter::EmitDebugInfo() const {
1795 // Start debug info section.
1796 Asm->SwitchSection(DwarfInfoSection, 0);
1798 // Process each compile unit.
1799 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1800 CompileUnit *Unit = CompileUnits[i];
1802 if (Unit->hasContent()) {
1803 DIE *Die = Unit->getDie();
1804 // Emit the compile units header.
1805 EmitLabel("info_begin", Unit->getID());
1806 // Emit size of content not including length itself
1807 unsigned ContentSize = Die->getSize() +
1808 sizeof(int16_t) + // DWARF version number
1809 sizeof(int32_t) + // Offset Into Abbrev. Section
1810 sizeof(int8_t); // Pointer Size (in bytes)
1812 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1813 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1814 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1815 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1818 EmitLabel("info_end", Unit->getID());
1825 /// EmitAbbreviations - Emit the abbreviation section.
1827 void DwarfWriter::EmitAbbreviations() const {
1828 // Check to see if it is worth the effort.
1829 if (!Abbreviations.empty()) {
1830 // Start the debug abbrev section.
1831 Asm->SwitchSection(DwarfAbbrevSection, 0);
1833 EmitLabel("abbrev_begin", 0);
1835 // For each abbrevation.
1836 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1837 AbbrevID <= NAID; ++AbbrevID) {
1838 // Get abbreviation data
1839 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1841 // Emit the abbrevations code (base 1 index.)
1842 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1844 // Emit the abbreviations data.
1850 EmitLabel("abbrev_end", 0);
1856 /// EmitDebugLines - Emit source line information.
1858 void DwarfWriter::EmitDebugLines() const {
1859 // Minimum line delta, thus ranging from -10..(255-10).
1860 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1861 // Maximum line delta, thus ranging from -10..(255-10).
1862 const int MaxLineDelta = 255 + MinLineDelta;
1864 // Start the dwarf line section.
1865 Asm->SwitchSection(DwarfLineSection, 0);
1867 // Construct the section header.
1869 EmitDifference("line_end", 0, "line_begin", 0);
1870 EOL("Length of Source Line Info");
1871 EmitLabel("line_begin", 0);
1873 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1875 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1876 EOL("Prolog Length");
1877 EmitLabel("line_prolog_begin", 0);
1879 EmitInt8(1); EOL("Minimum Instruction Length");
1881 EmitInt8(1); EOL("Default is_stmt_start flag");
1883 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1885 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1887 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1889 // Line number standard opcode encodings argument count
1890 EmitInt8(0); EOL("DW_LNS_copy arg count");
1891 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1892 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1893 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1894 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1895 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1896 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1897 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1898 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1900 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1901 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1903 // Emit directories.
1904 for (unsigned DirectoryID = 1, NDID = Directories.size();
1905 DirectoryID <= NDID; ++DirectoryID) {
1906 EmitString(Directories[DirectoryID]); EOL("Directory");
1908 EmitInt8(0); EOL("End of directories");
1911 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1912 SourceID <= NSID; ++SourceID) {
1913 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1914 EmitString(SourceFile.getName()); EOL("Source");
1915 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1916 EmitULEB128Bytes(0); EOL("Mod date");
1917 EmitULEB128Bytes(0); EOL("File size");
1919 EmitInt8(0); EOL("End of files");
1921 EmitLabel("line_prolog_end", 0);
1923 // Emit line information
1924 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1926 // Dwarf assumes we start with first line of first source file.
1927 unsigned Source = 1;
1930 // Construct rows of the address, source, line, column matrix.
1931 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1932 SourceLineInfo *LineInfo = LineInfos[i];
1935 unsigned SourceID = LineInfo->getSourceID();
1936 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1937 unsigned DirectoryID = SourceFile.getDirectoryID();
1939 << Asm->CommentString << " "
1940 << Directories[DirectoryID]
1941 << SourceFile.getName() << ":"
1942 << LineInfo->getLine() << "\n";
1945 // Define the line address.
1946 EmitInt8(0); EOL("Extended Op");
1947 EmitInt8(4 + 1); EOL("Op size");
1948 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1949 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
1951 // If change of source, then switch to the new source.
1952 if (Source != LineInfo->getSourceID()) {
1953 Source = LineInfo->getSourceID();
1954 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1955 EmitULEB128Bytes(Source); EOL("New Source");
1958 // If change of line.
1959 if (Line != LineInfo->getLine()) {
1960 // Determine offset.
1961 int Offset = LineInfo->getLine() - Line;
1962 int Delta = Offset - MinLineDelta;
1965 Line = LineInfo->getLine();
1967 // If delta is small enough and in range...
1968 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1969 // ... then use fast opcode.
1970 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1972 // ... otherwise use long hand.
1973 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1974 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1975 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1978 // Copy the previous row (different address or source)
1979 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1983 // Define last address.
1984 EmitInt8(0); EOL("Extended Op");
1985 EmitInt8(4 + 1); EOL("Op size");
1986 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1987 EmitReference("text_end", 0); EOL("Location label");
1989 // Mark end of matrix.
1990 EmitInt8(0); EOL("DW_LNE_end_sequence");
1991 EmitULEB128Bytes(1); O << "\n";
1992 EmitInt8(1); O << "\n";
1994 EmitLabel("line_end", 0);
1999 /// EmitDebugFrame - Emit visible names into a debug frame section.
2001 void DwarfWriter::EmitDebugFrame() {
2002 // Start the dwarf pubnames section.
2003 Asm->SwitchSection(DwarfFrameSection, 0);
2005 EmitDifference("frame_common_end", 0,
2006 "frame_common_begin", 0);
2007 EOL("Length of Common Information Entry");
2009 EmitLabel("frame_common_begin", 0);
2010 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2011 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2012 EmitString(""); EOL("CIE Augmentation");
2013 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2014 // FIXME - needs to change based on stack direction.
2015 EmitSLEB128Bytes(-sizeof(int32_t)); EOL("CIE Data Alignment Factor");
2016 // FIXME - hard coded for PPC (LR).
2017 EmitInt8(0x41); EOL("CIE RA Column Hardcoded (PPC LR)");
2018 // FIXME - hard coded for PPC 0(SP).
2019 EmitULEB128Bytes(DW_CFA_def_cfa); EOL("DW_CFA_def_cfa");
2020 EmitULEB128Bytes(1); EOL("PPC Register SP");
2021 EmitULEB128Bytes(0); EOL("PPC offset 0 as in 0(SP)");
2023 EmitLabel("frame_common_end", 0);
2028 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2030 void DwarfWriter::EmitDebugPubNames() {
2031 // Start the dwarf pubnames section.
2032 Asm->SwitchSection(DwarfPubNamesSection, 0);
2034 // Process each compile unit.
2035 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2036 CompileUnit *Unit = CompileUnits[i];
2038 if (Unit->hasContent()) {
2039 EmitDifference("pubnames_end", Unit->getID(),
2040 "pubnames_begin", Unit->getID());
2041 EOL("Length of Public Names Info");
2043 EmitLabel("pubnames_begin", Unit->getID());
2045 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2047 EmitReference("info_begin", Unit->getID());
2048 EOL("Offset of Compilation Unit Info");
2050 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2051 EOL("Compilation Unit Length");
2053 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2055 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2058 const std::string &Name = GI->first;
2059 DIE * Entity = GI->second;
2061 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2062 EmitString(Name); EOL("External Name");
2065 EmitInt32(0); EOL("End Mark");
2066 EmitLabel("pubnames_end", Unit->getID());
2073 /// EmitDebugStr - Emit visible names into a debug str section.
2075 void DwarfWriter::EmitDebugStr() {
2076 // Check to see if it is worth the effort.
2077 if (!StringPool.empty()) {
2078 // Start the dwarf str section.
2079 Asm->SwitchSection(DwarfStrSection, 0);
2081 // For each of strings in the string pool.
2082 for (unsigned StringID = 1, N = StringPool.size();
2083 StringID <= N; ++StringID) {
2084 // Emit a label for reference from debug information entries.
2085 EmitLabel("string", StringID);
2086 // Emit the string itself.
2087 const std::string &String = StringPool[StringID];
2088 EmitString(String); O << "\n";
2095 /// EmitDebugLoc - Emit visible names into a debug loc section.
2097 void DwarfWriter::EmitDebugLoc() {
2098 // Start the dwarf loc section.
2099 Asm->SwitchSection(DwarfLocSection, 0);
2104 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2106 void DwarfWriter::EmitDebugARanges() {
2107 // Start the dwarf aranges section.
2108 Asm->SwitchSection(DwarfARangesSection, 0);
2112 // Process each compile unit.
2113 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2114 CompileUnit *Unit = CompileUnits[i];
2116 if (Unit->hasContent()) {
2117 // Don't include size of length
2118 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2120 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2122 EmitReference("info_begin", Unit->getID());
2123 EOL("Offset of Compilation Unit Info");
2125 EmitInt8(AddressSize); EOL("Size of Address");
2127 EmitInt8(0); EOL("Size of Segment Descriptor");
2129 EmitInt16(0); EOL("Pad (1)");
2130 EmitInt16(0); EOL("Pad (2)");
2133 EmitReference("text_begin", 0); EOL("Address");
2134 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2136 EmitInt32(0); EOL("EOM (1)");
2137 EmitInt32(0); EOL("EOM (2)");
2145 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2147 void DwarfWriter::EmitDebugRanges() {
2148 // Start the dwarf ranges section.
2149 Asm->SwitchSection(DwarfRangesSection, 0);
2154 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2156 void DwarfWriter::EmitDebugMacInfo() {
2157 // Start the dwarf macinfo section.
2158 Asm->SwitchSection(DwarfMacInfoSection, 0);
2163 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2165 void DwarfWriter::ConstructCompileUnitDIEs() {
2166 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2168 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2169 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
2170 CompileUnits.push_back(Unit);
2174 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2176 void DwarfWriter::ConstructGlobalDIEs() {
2177 std::vector<GlobalVariableDesc *> GlobalVariables =
2178 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2180 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2181 GlobalVariableDesc *GVD = GlobalVariables[i];
2182 NewGlobalVariable(GVD);
2186 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2188 void DwarfWriter::ConstructSubprogramDIEs() {
2189 std::vector<SubprogramDesc *> Subprograms =
2190 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2192 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2193 SubprogramDesc *SPD = Subprograms[i];
2198 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
2200 bool DwarfWriter::ShouldEmitDwarf() {
2201 // Check if debug info is present.
2202 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2204 // Make sure initial declarations are made.
2208 // Create all the compile unit DIEs.
2209 ConstructCompileUnitDIEs();
2211 // Create DIEs for each of the externally visible global variables.
2212 ConstructGlobalDIEs();
2214 // Create DIEs for each of the externally visible subprograms.
2215 ConstructSubprogramDIEs();
2224 //===----------------------------------------------------------------------===//
2225 // Main entry points.
2228 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2235 , SubprogramCount(0)
2242 , AddressSize(sizeof(int32_t))
2247 , DwarfAbbrevSection(".debug_abbrev")
2248 , DwarfInfoSection(".debug_info")
2249 , DwarfLineSection(".debug_line")
2250 , DwarfFrameSection(".debug_frame")
2251 , DwarfPubNamesSection(".debug_pubnames")
2252 , DwarfPubTypesSection(".debug_pubtypes")
2253 , DwarfStrSection(".debug_str")
2254 , DwarfLocSection(".debug_loc")
2255 , DwarfARangesSection(".debug_aranges")
2256 , DwarfRangesSection(".debug_ranges")
2257 , DwarfMacInfoSection(".debug_macinfo")
2258 , TextSection(".text")
2259 , DataSection(".data")
2261 DwarfWriter::~DwarfWriter() {
2262 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2263 delete CompileUnits[i];
2267 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
2269 void DwarfWriter::BeginModule(Module *M) {
2272 if (!ShouldEmitDwarf()) return;
2273 EOL("Dwarf Begin Module");
2276 /// EndModule - Emit all Dwarf sections that should come after the content.
2278 void DwarfWriter::EndModule() {
2279 if (!ShouldEmitDwarf()) return;
2280 EOL("Dwarf End Module");
2282 // Standard sections final addresses.
2283 Asm->SwitchSection(TextSection, 0);
2284 EmitLabel("text_end", 0);
2285 Asm->SwitchSection(DataSection, 0);
2286 EmitLabel("data_end", 0);
2288 // Compute DIE offsets and sizes.
2291 // Emit all the DIEs into a debug info section
2294 // Corresponding abbreviations into a abbrev section.
2295 EmitAbbreviations();
2297 // Emit source line correspondence into a debug line section.
2300 // Emit info into a debug frame section.
2303 // Emit info into a debug pubnames section.
2304 EmitDebugPubNames();
2306 // Emit info into a debug str section.
2309 // Emit info into a debug loc section.
2312 // Emit info into a debug aranges section.
2315 // Emit info into a debug ranges section.
2318 // Emit info into a debug macinfo section.
2322 /// BeginFunction - Gather pre-function debug information.
2324 void DwarfWriter::BeginFunction(MachineFunction *MF) {
2327 if (!ShouldEmitDwarf()) return;
2328 EOL("Dwarf Begin Function");
2330 // Define begin label for subprogram.
2331 Asm->SwitchSection(TextSection, 0);
2332 EmitLabel("func_begin", ++SubprogramCount);
2336 /// EndFunction - Gather and emit post-function debug information.
2338 void DwarfWriter::EndFunction() {
2339 if (!ShouldEmitDwarf()) return;
2340 EOL("Dwarf End Function");
2342 // Define end label for subprogram.
2343 Asm->SwitchSection(TextSection, 0);
2344 EmitLabel("func_end", SubprogramCount);
2346 // Construct scopes for subprogram.
2347 ConstructRootScope(DebugInfo->getRootScope());
2348 DebugInfo->ClearScopes();