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/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineLocation.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/Target/MRegisterInfo.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetFrameInfo.h"
34 using namespace llvm::dwarf;
37 DwarfVerbose("dwarf-verbose", cl::Hidden,
38 cl::desc("Add comments to Dwarf directives."));
42 //===----------------------------------------------------------------------===//
43 // Forward declarations.
47 //===----------------------------------------------------------------------===//
48 // CompileUnit - This dwarf writer support class manages information associate
49 // with a source file.
52 CompileUnitDesc *Desc; // Compile unit debug descriptor.
53 unsigned ID; // File ID for source.
54 DIE *Die; // Compile unit debug information entry.
55 std::map<std::string, DIE *> Globals; // A map of globally visible named
56 // entities for this unit.
57 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
58 // Tracks the mapping of unit level
59 // debug informaton descriptors to debug
60 // information entries.
63 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
74 CompileUnitDesc *getDesc() const { return Desc; }
75 unsigned getID() const { return ID; }
76 DIE* getDie() const { return Die; }
77 std::map<std::string, DIE *> &getGlobals() { return Globals; }
79 /// hasContent - Return true if this compile unit has something to write out.
81 bool hasContent() const;
83 /// AddGlobal - Add a new global entity to the compile unit.
85 void AddGlobal(const std::string &Name, DIE *Die);
87 /// getDieMapSlotFor - Returns the debug information entry map slot for the
88 /// specified debug descriptor.
89 DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
90 return DescToDieMap[DD];
94 //===----------------------------------------------------------------------===//
95 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
96 // Dwarf abbreviation.
99 unsigned Attribute; // Dwarf attribute code.
100 unsigned Form; // Dwarf form code.
103 DIEAbbrevData(unsigned A, unsigned F)
109 unsigned getAttribute() const { return Attribute; }
110 unsigned getForm() const { return Form; }
112 /// operator== - Used by DIEAbbrev to locate entry.
114 bool operator==(const DIEAbbrevData &DAD) const {
115 return Attribute == DAD.Attribute && Form == DAD.Form;
118 /// operator!= - Used by DIEAbbrev to locate entry.
120 bool operator!=(const DIEAbbrevData &DAD) const {
121 return Attribute != DAD.Attribute || Form != DAD.Form;
124 /// operator< - Used by DIEAbbrev to locate entry.
126 bool operator<(const DIEAbbrevData &DAD) const {
127 return Attribute < DAD.Attribute ||
128 (Attribute == DAD.Attribute && Form < DAD.Form);
132 //===----------------------------------------------------------------------===//
133 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
134 // information object.
137 unsigned Tag; // Dwarf tag code.
138 unsigned ChildrenFlag; // Dwarf children flag.
139 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
143 DIEAbbrev(unsigned T, unsigned C)
151 unsigned getTag() const { return Tag; }
152 unsigned getChildrenFlag() const { return ChildrenFlag; }
153 const std::vector<DIEAbbrevData> &getData() const { return Data; }
154 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
156 /// operator== - Used by UniqueVector to locate entry.
158 bool operator==(const DIEAbbrev &DA) const;
160 /// operator< - Used by UniqueVector to locate entry.
162 bool operator<(const DIEAbbrev &DA) const;
164 /// AddAttribute - Adds another set of attribute information to the
166 void AddAttribute(unsigned Attribute, unsigned Form) {
167 Data.push_back(DIEAbbrevData(Attribute, Form));
170 /// AddFirstAttribute - Adds a set of attribute information to the front
171 /// of the abbreviation.
172 void AddFirstAttribute(unsigned Attribute, unsigned Form) {
173 Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
176 /// Emit - Print the abbreviation using the specified Dwarf writer.
178 void Emit(const DwarfWriter &DW) const;
181 void print(std::ostream &O);
186 //===----------------------------------------------------------------------===//
187 // DIEValue - A debug information entry value.
201 unsigned Type; // Type of the value
203 DIEValue(unsigned T) : Type(T) {}
204 virtual ~DIEValue() {}
206 // Implement isa/cast/dyncast.
207 static bool classof(const DIEValue *) { return true; }
209 /// EmitValue - Emit value via the Dwarf writer.
211 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
213 /// SizeOf - Return the size of a value in bytes.
215 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
218 //===----------------------------------------------------------------------===//
219 // DWInteger - An integer value DIE.
221 class DIEInteger : public DIEValue {
226 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
228 // Implement isa/cast/dyncast.
229 static bool classof(const DIEInteger *) { return true; }
230 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
232 /// BestForm - Choose the best form for integer.
234 unsigned BestForm(bool IsSigned);
236 /// EmitValue - Emit integer of appropriate size.
238 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
240 /// SizeOf - Determine size of integer value in bytes.
242 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
245 //===----------------------------------------------------------------------===//
246 // DIEString - A string value DIE.
248 struct DIEString : public DIEValue {
249 const std::string String;
251 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
253 // Implement isa/cast/dyncast.
254 static bool classof(const DIEString *) { return true; }
255 static bool classof(const DIEValue *S) { return S->Type == isString; }
257 /// EmitValue - Emit string value.
259 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
261 /// SizeOf - Determine size of string value in bytes.
263 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
266 //===----------------------------------------------------------------------===//
267 // DIEDwarfLabel - A Dwarf internal label expression DIE.
269 struct DIEDwarfLabel : public DIEValue {
272 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
274 // Implement isa/cast/dyncast.
275 static bool classof(const DIEDwarfLabel *) { return true; }
276 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
278 /// EmitValue - Emit label value.
280 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
282 /// SizeOf - Determine size of label value in bytes.
284 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
288 //===----------------------------------------------------------------------===//
289 // DIEObjectLabel - A label to an object in code or data.
291 struct DIEObjectLabel : public DIEValue {
292 const std::string Label;
294 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
296 // Implement isa/cast/dyncast.
297 static bool classof(const DIEObjectLabel *) { return true; }
298 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
300 /// EmitValue - Emit label value.
302 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
304 /// SizeOf - Determine size of label value in bytes.
306 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
309 //===----------------------------------------------------------------------===//
310 // DIEDelta - A simple label difference DIE.
312 struct DIEDelta : public DIEValue {
313 const DWLabel LabelHi;
314 const DWLabel LabelLo;
316 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
317 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
319 // Implement isa/cast/dyncast.
320 static bool classof(const DIEDelta *) { return true; }
321 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
323 /// EmitValue - Emit delta value.
325 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
327 /// SizeOf - Determine size of delta value in bytes.
329 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
332 //===----------------------------------------------------------------------===//
333 // DIEntry - A pointer to a debug information entry.
335 struct DIEntry : public DIEValue {
338 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
340 // Implement isa/cast/dyncast.
341 static bool classof(const DIEntry *) { return true; }
342 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
344 /// EmitValue - Emit debug information entry offset.
346 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
348 /// SizeOf - Determine size of debug information entry in bytes.
350 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
353 //===----------------------------------------------------------------------===//
354 // DIEBlock - A block of values. Primarily used for location expressions.
356 struct DIEBlock : public DIEValue {
357 unsigned Size; // Size in bytes excluding size header.
358 std::vector<unsigned> Forms; // Data forms.
359 std::vector<DIEValue *> Values; // Block values.
369 // Implement isa/cast/dyncast.
370 static bool classof(const DIEBlock *) { return true; }
371 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
373 /// ComputeSize - calculate the size of the block.
375 unsigned ComputeSize(DwarfWriter &DW);
377 /// BestForm - Choose the best form for data.
381 /// EmitValue - Emit block data.
383 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
385 /// SizeOf - Determine size of block data in bytes.
387 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
389 /// AddUInt - Add an unsigned integer value.
391 void AddUInt(unsigned Form, uint64_t Integer);
393 /// AddSInt - Add an signed integer value.
395 void AddSInt(unsigned Form, int64_t Integer);
397 /// AddString - Add a std::string value.
399 void AddString(unsigned Form, const std::string &String);
401 /// AddLabel - Add a Dwarf label value.
403 void AddLabel(unsigned Form, const DWLabel &Label);
405 /// AddObjectLabel - Add a non-Dwarf label value.
407 void AddObjectLabel(unsigned Form, const std::string &Label);
409 /// AddDelta - Add a label delta value.
411 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
413 /// AddDIEntry - Add a DIE value.
415 void AddDIEntry(unsigned Form, DIE *Entry);
419 //===----------------------------------------------------------------------===//
420 // DIE - A structured debug information entry. Has an abbreviation which
421 // describes it's organization.
424 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
425 unsigned AbbrevID; // Decribing abbreviation ID.
426 unsigned Offset; // Offset in debug info section.
427 unsigned Size; // Size of instance + children.
428 std::vector<DIE *> Children; // Children DIEs.
429 std::vector<DIEValue *> Values; // Attributes values.
436 unsigned getAbbrevID() const { return AbbrevID; }
437 unsigned getOffset() const { return Offset; }
438 unsigned getSize() const { return Size; }
439 const std::vector<DIE *> &getChildren() const { return Children; }
440 const std::vector<DIEValue *> &getValues() const { return Values; }
441 void setOffset(unsigned O) { Offset = O; }
442 void setSize(unsigned S) { Size = S; }
444 /// SiblingOffset - Return the offset of the debug information entry's
446 unsigned SiblingOffset() const { return Offset + Size; }
448 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
450 void AddSiblingOffset();
452 /// AddUInt - Add an unsigned integer attribute data and value.
454 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
456 /// AddSInt - Add an signed integer attribute data and value.
458 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
460 /// AddString - Add a std::string attribute data and value.
462 void AddString(unsigned Attribute, unsigned Form,
463 const std::string &String);
465 /// AddLabel - Add a Dwarf label attribute data and value.
467 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
469 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
471 void AddObjectLabel(unsigned Attribute, unsigned Form,
472 const std::string &Label);
474 /// AddDelta - Add a label delta attribute data and value.
476 void AddDelta(unsigned Attribute, unsigned Form,
477 const DWLabel &Hi, const DWLabel &Lo);
479 /// AddDIEntry - Add a DIE attribute data and value.
481 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
483 /// AddBlock - Add block data.
485 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
487 /// Complete - Indicate that all attributes have been added and
488 /// ready to get an abbreviation ID.
490 void Complete(DwarfWriter &DW);
492 /// AddChild - Add a child to the DIE.
493 void AddChild(DIE *Child);
496 } // End of namespace llvm
498 //===----------------------------------------------------------------------===//
500 CompileUnit::~CompileUnit() {
504 /// hasContent - Return true if this compile unit has something to write out.
506 bool CompileUnit::hasContent() const {
507 return !Die->getChildren().empty();
510 /// AddGlobal - Add a new global entity to the compile unit.
512 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
516 //===----------------------------------------------------------------------===//
518 /// operator== - Used by UniqueVector to locate entry.
520 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
521 if (Tag != DA.Tag) return false;
522 if (ChildrenFlag != DA.ChildrenFlag) return false;
523 if (Data.size() != DA.Data.size()) return false;
525 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
526 if (Data[i] != DA.Data[i]) return false;
532 /// operator< - Used by UniqueVector to locate entry.
534 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
535 if (Tag != DA.Tag) return Tag < DA.Tag;
536 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
537 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
539 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
540 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
546 /// Emit - Print the abbreviation using the specified Dwarf writer.
548 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
549 // Emit its Dwarf tag type.
550 DW.EmitULEB128Bytes(Tag);
551 DW.EOL(TagString(Tag));
553 // Emit whether it has children DIEs.
554 DW.EmitULEB128Bytes(ChildrenFlag);
555 DW.EOL(ChildrenString(ChildrenFlag));
557 // For each attribute description.
558 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
559 const DIEAbbrevData &AttrData = Data[i];
561 // Emit attribute type.
562 DW.EmitULEB128Bytes(AttrData.getAttribute());
563 DW.EOL(AttributeString(AttrData.getAttribute()));
566 DW.EmitULEB128Bytes(AttrData.getForm());
567 DW.EOL(FormEncodingString(AttrData.getForm()));
570 // Mark end of abbreviation.
571 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
572 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
576 void DIEAbbrev::print(std::ostream &O) {
577 O << "Abbreviation @"
578 << std::hex << (intptr_t)this << std::dec
582 << ChildrenString(ChildrenFlag)
585 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
587 << AttributeString(Data[i].getAttribute())
589 << FormEncodingString(Data[i].getForm())
593 void DIEAbbrev::dump() { print(std::cerr); }
596 //===----------------------------------------------------------------------===//
598 /// BestForm - Choose the best form for integer.
600 unsigned DIEInteger::BestForm(bool IsSigned) {
602 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
603 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
604 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
606 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
607 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
608 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
610 return DW_FORM_data8;
613 /// EmitValue - Emit integer of appropriate size.
615 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
617 case DW_FORM_flag: // Fall thru
618 case DW_FORM_ref1: // Fall thru
619 case DW_FORM_data1: DW.EmitInt8(Integer); break;
620 case DW_FORM_ref2: // Fall thru
621 case DW_FORM_data2: DW.EmitInt16(Integer); break;
622 case DW_FORM_ref4: // Fall thru
623 case DW_FORM_data4: DW.EmitInt32(Integer); break;
624 case DW_FORM_ref8: // Fall thru
625 case DW_FORM_data8: DW.EmitInt64(Integer); break;
626 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
627 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
628 default: assert(0 && "DIE Value form not supported yet"); break;
632 /// SizeOf - Determine size of integer value in bytes.
634 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
636 case DW_FORM_flag: // Fall thru
637 case DW_FORM_ref1: // Fall thru
638 case DW_FORM_data1: return sizeof(int8_t);
639 case DW_FORM_ref2: // Fall thru
640 case DW_FORM_data2: return sizeof(int16_t);
641 case DW_FORM_ref4: // Fall thru
642 case DW_FORM_data4: return sizeof(int32_t);
643 case DW_FORM_ref8: // Fall thru
644 case DW_FORM_data8: return sizeof(int64_t);
645 case DW_FORM_udata: return DW.SizeULEB128(Integer);
646 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
647 default: assert(0 && "DIE Value form not supported yet"); break;
652 //===----------------------------------------------------------------------===//
654 /// EmitValue - Emit string value.
656 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
657 DW.EmitString(String);
660 /// SizeOf - Determine size of string value in bytes.
662 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
663 return String.size() + sizeof(char); // sizeof('\0');
666 //===----------------------------------------------------------------------===//
668 /// EmitValue - Emit label value.
670 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
671 DW.EmitReference(Label);
674 /// SizeOf - Determine size of label value in bytes.
676 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
677 return DW.getAddressSize();
680 //===----------------------------------------------------------------------===//
682 /// EmitValue - Emit label value.
684 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
685 DW.EmitReference(Label);
688 /// SizeOf - Determine size of label value in bytes.
690 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
691 return DW.getAddressSize();
694 //===----------------------------------------------------------------------===//
696 /// EmitValue - Emit delta value.
698 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
699 DW.EmitDifference(LabelHi, LabelLo);
702 /// SizeOf - Determine size of delta value in bytes.
704 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
705 return DW.getAddressSize();
708 //===----------------------------------------------------------------------===//
709 /// EmitValue - Emit debug information entry offset.
711 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
712 DW.EmitInt32(Entry->getOffset());
715 /// SizeOf - Determine size of debug information entry value in bytes.
717 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
718 return sizeof(int32_t);
721 //===----------------------------------------------------------------------===//
723 DIEBlock::~DIEBlock() {
724 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
729 /// ComputeSize - calculate the size of the block.
731 unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
733 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
734 Size += Values[i]->SizeOf(DW, Forms[i]);
739 /// BestForm - Choose the best form for data.
741 unsigned DIEBlock::BestForm() {
742 if ((unsigned char)Size == Size) return DW_FORM_block1;
743 if ((unsigned short)Size == Size) return DW_FORM_block2;
744 if ((unsigned int)Size == Size) return DW_FORM_block4;
745 return DW_FORM_block;
748 /// EmitValue - Emit block data.
750 void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
752 case DW_FORM_block1: DW.EmitInt8(Size); break;
753 case DW_FORM_block2: DW.EmitInt16(Size); break;
754 case DW_FORM_block4: DW.EmitInt32(Size); break;
755 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
756 default: assert(0 && "Improper form for block"); break;
758 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
760 Values[i]->EmitValue(DW, Forms[i]);
764 /// SizeOf - Determine size of block data in bytes.
766 unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
768 case DW_FORM_block1: return Size + sizeof(int8_t);
769 case DW_FORM_block2: return Size + sizeof(int16_t);
770 case DW_FORM_block4: return Size + sizeof(int32_t);
771 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
772 default: assert(0 && "Improper form for block"); break;
777 /// AddUInt - Add an unsigned integer value.
779 void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
780 DIEInteger *DI = new DIEInteger(Integer);
781 Values.push_back(DI);
782 if (Form == 0) Form = DI->BestForm(false);
783 Forms.push_back(Form);
786 /// AddSInt - Add an signed integer value.
788 void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
789 DIEInteger *DI = new DIEInteger(Integer);
790 Values.push_back(DI);
791 if (Form == 0) Form = DI->BestForm(true);
792 Forms.push_back(Form);
795 /// AddString - Add a std::string value.
797 void DIEBlock::AddString(unsigned Form, const std::string &String) {
798 Values.push_back(new DIEString(String));
799 Forms.push_back(Form);
802 /// AddLabel - Add a Dwarf label value.
804 void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
805 Values.push_back(new DIEDwarfLabel(Label));
806 Forms.push_back(Form);
809 /// AddObjectLabel - Add a non-Dwarf label value.
811 void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
812 Values.push_back(new DIEObjectLabel(Label));
813 Forms.push_back(Form);
816 /// AddDelta - Add a label delta value.
818 void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
819 Values.push_back(new DIEDelta(Hi, Lo));
820 Forms.push_back(Form);
823 /// AddDIEntry - Add a DIE value.
825 void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
826 Values.push_back(new DIEntry(Entry));
827 Forms.push_back(Form);
830 //===----------------------------------------------------------------------===//
832 DIE::DIE(unsigned Tag)
833 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
842 if (Abbrev) delete Abbrev;
844 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
848 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
853 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
855 void DIE::AddSiblingOffset() {
856 DIEInteger *DI = new DIEInteger(0);
857 Values.insert(Values.begin(), DI);
858 Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
861 /// AddUInt - Add an unsigned integer attribute data and value.
863 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
864 DIEInteger *DI = new DIEInteger(Integer);
865 Values.push_back(DI);
866 if (!Form) Form = DI->BestForm(false);
867 Abbrev->AddAttribute(Attribute, Form);
870 /// AddSInt - Add an signed integer attribute data and value.
872 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
873 DIEInteger *DI = new DIEInteger(Integer);
874 Values.push_back(DI);
875 if (!Form) Form = DI->BestForm(true);
876 Abbrev->AddAttribute(Attribute, Form);
879 /// AddString - Add a std::string attribute data and value.
881 void DIE::AddString(unsigned Attribute, unsigned Form,
882 const std::string &String) {
883 Values.push_back(new DIEString(String));
884 Abbrev->AddAttribute(Attribute, Form);
887 /// AddLabel - Add a Dwarf label attribute data and value.
889 void DIE::AddLabel(unsigned Attribute, unsigned Form,
890 const DWLabel &Label) {
891 Values.push_back(new DIEDwarfLabel(Label));
892 Abbrev->AddAttribute(Attribute, Form);
895 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
897 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
898 const std::string &Label) {
899 Values.push_back(new DIEObjectLabel(Label));
900 Abbrev->AddAttribute(Attribute, Form);
903 /// AddDelta - Add a label delta attribute data and value.
905 void DIE::AddDelta(unsigned Attribute, unsigned Form,
906 const DWLabel &Hi, const DWLabel &Lo) {
907 Values.push_back(new DIEDelta(Hi, Lo));
908 Abbrev->AddAttribute(Attribute, Form);
911 /// AddDIEntry - Add a DIE attribute data and value.
913 void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
914 Values.push_back(new DIEntry(Entry));
915 Abbrev->AddAttribute(Attribute, Form);
918 /// AddBlock - Add block data.
920 void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
921 assert(Block->Size && "Block size has not been computed");
922 Values.push_back(Block);
923 if (!Form) Form = Block->BestForm();
924 Abbrev->AddAttribute(Attribute, Form);
927 /// Complete - Indicate that all attributes have been added and ready to get an
929 void DIE::Complete(DwarfWriter &DW) {
930 AbbrevID = DW.NewAbbreviation(Abbrev);
935 /// AddChild - Add a child to the DIE.
937 void DIE::AddChild(DIE *Child) {
938 assert(Abbrev && "Adding children without an abbreviation");
939 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
940 Children.push_back(Child);
943 //===----------------------------------------------------------------------===//
947 //===----------------------------------------------------------------------===//
949 /// PrintHex - Print a value as a hexidecimal value.
951 void DwarfWriter::PrintHex(int Value) const {
952 O << "0x" << std::hex << Value << std::dec;
955 /// EOL - Print a newline character to asm stream. If a comment is present
956 /// then it will be printed first. Comments should not contain '\n'.
957 void DwarfWriter::EOL(const std::string &Comment) const {
958 if (DwarfVerbose && !Comment.empty()) {
960 << Asm->CommentString
967 /// EmitAlign - Print a align directive.
969 void DwarfWriter::EmitAlign(unsigned Alignment) const {
970 O << Asm->AlignDirective << Alignment << "\n";
973 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
974 /// unsigned leb128 value.
975 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
980 O << Asm->Data8bitsDirective;
985 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
986 /// signed leb128 value.
987 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
992 O << Asm->Data8bitsDirective;
997 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
998 /// representing an unsigned leb128 value.
999 void DwarfWriter::PrintULEB128(unsigned Value) const {
1001 unsigned Byte = Value & 0x7f;
1003 if (Value) Byte |= 0x80;
1005 if (Value) O << ", ";
1009 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
1011 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
1015 Size += sizeof(int8_t);
1020 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
1021 /// representing a signed leb128 value.
1022 void DwarfWriter::PrintSLEB128(int Value) const {
1023 int Sign = Value >> (8 * sizeof(Value) - 1);
1027 unsigned Byte = Value & 0x7f;
1029 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1030 if (IsMore) Byte |= 0x80;
1032 if (IsMore) O << ", ";
1036 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
1038 unsigned DwarfWriter::SizeSLEB128(int Value) {
1040 int Sign = Value >> (8 * sizeof(Value) - 1);
1044 unsigned Byte = Value & 0x7f;
1046 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1047 Size += sizeof(int8_t);
1052 /// EmitInt8 - Emit a byte directive and value.
1054 void DwarfWriter::EmitInt8(int Value) const {
1055 O << Asm->Data8bitsDirective;
1056 PrintHex(Value & 0xFF);
1059 /// EmitInt16 - Emit a short directive and value.
1061 void DwarfWriter::EmitInt16(int Value) const {
1062 O << Asm->Data16bitsDirective;
1063 PrintHex(Value & 0xFFFF);
1066 /// EmitInt32 - Emit a long directive and value.
1068 void DwarfWriter::EmitInt32(int Value) const {
1069 O << Asm->Data32bitsDirective;
1073 /// EmitInt64 - Emit a long long directive and value.
1075 void DwarfWriter::EmitInt64(uint64_t Value) const {
1076 if (Asm->Data64bitsDirective) {
1077 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
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 /// AddAddress - Add an address attribute to a die based on the location
1219 void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
1220 const MachineLocation &Location) {
1221 DIEBlock *Block = new DIEBlock();
1222 if (Location.isRegister()) {
1223 Block->AddUInt(DW_FORM_data1,
1224 DW_OP_reg0 + RI->getDwarfRegNum(Location.getRegister()));
1226 Block->AddUInt(DW_FORM_data1,
1227 DW_OP_breg0 + RI->getDwarfRegNum(Location.getRegister()));
1228 Block->AddUInt(DW_FORM_sdata, Location.getOffset());
1230 Block->ComputeSize(*this);
1231 Die->AddBlock(Attribute, 0, Block);
1234 /// getDieMapSlotFor - Returns the debug information entry map slot for the
1235 /// specified debug descriptor.
1236 DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
1237 return DescToDieMap[DD];
1240 /// NewType - Create a new type DIE.
1242 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
1244 // FIXME - Hack for missing types
1245 DIE *Die = new DIE(DW_TAG_base_type);
1246 Die->AddUInt(DW_AT_byte_size, 0, 4);
1247 Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1248 Unit->getDie()->AddChild(Die);
1252 // Check for pre-existence.
1253 DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
1254 if (Slot) return Slot;
1256 // Get core information.
1257 const std::string &Name = TyDesc->getName();
1258 uint64_t Size = TyDesc->getSize() >> 3;
1262 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1263 // Fundamental types like int, float, bool
1264 Slot = Ty = new DIE(DW_TAG_base_type);
1265 unsigned Encoding = BasicTy->getEncoding();
1266 Ty->AddUInt(DW_AT_encoding, DW_FORM_data1, Encoding);
1267 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1268 // Create specific DIE.
1269 Slot = Ty = new DIE(DerivedTy->getTag());
1271 // Map to main type, void will not have a type.
1272 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1273 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1274 NewType(Context, FromTy, Unit));
1276 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1278 unsigned Tag = CompTy->getTag();
1280 // Create specific DIE.
1281 Slot = Ty = Tag == DW_TAG_vector_type ? new DIE(DW_TAG_array_type) :
1284 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1287 case DW_TAG_vector_type: Ty->AddUInt(DW_AT_GNU_vector, DW_FORM_flag, 1);
1289 case DW_TAG_array_type: {
1290 // Add element type.
1291 if (TypeDesc *FromTy = CompTy->getFromType()) {
1292 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1293 NewType(Context, FromTy, Unit));
1296 // Don't emit size attribute.
1299 // Construct an anonymous type for index type.
1300 DIE *IndexTy = new DIE(DW_TAG_base_type);
1301 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1302 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1304 Context->AddChild(IndexTy);
1306 // Add subranges to array type.
1307 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1308 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1309 int64_t Lo = SRD->getLo();
1310 int64_t Hi = SRD->getHi();
1311 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1313 // If a range is available.
1315 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1316 // Only add low if non-zero.
1317 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1318 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
1320 Ty->AddChild(Subrange);
1325 case DW_TAG_structure_type:
1326 case DW_TAG_union_type: {
1327 // Add elements to structure type.
1328 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1329 DebugInfoDesc *Element = Elements[i];
1331 if (DerivedTypeDesc *MemberDesc = dyn_cast<DerivedTypeDesc>(Element)) {
1332 // Add field or base class.
1334 unsigned Tag = MemberDesc->getTag();
1336 // Extract the basic information.
1337 const std::string &Name = MemberDesc->getName();
1338 TypeDesc *MemTy = MemberDesc->getFromType();
1339 uint64_t Size = MemberDesc->getSize();
1340 uint64_t Align = MemberDesc->getAlign();
1341 uint64_t Offset = MemberDesc->getOffset();
1343 // Construct member debug information entry.
1344 DIE *Member = new DIE(Tag);
1346 // Add name if not "".
1347 if (!Name.empty())Member->AddString(DW_AT_name, DW_FORM_string, Name);
1348 // Add location if available.
1349 AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
1351 // Most of the time the field info is the same as the members.
1352 uint64_t FieldSize = Size;
1353 uint64_t FieldAlign = Align;
1354 uint64_t FieldOffset = Offset;
1356 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1357 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1358 NewType(Context, FromTy, Unit));
1359 FieldSize = FromTy->getSize();
1360 FieldAlign = FromTy->getSize();
1363 // Unless we have a bit field.
1364 if (Tag == DW_TAG_member && FieldSize != Size) {
1365 // Construct the alignment mask.
1366 uint64_t AlignMask = ~(FieldAlign - 1);
1367 // Determine the high bit + 1 of the declared size.
1368 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1369 // Work backwards to determine the base offset of the field.
1370 FieldOffset = HiMark - FieldSize;
1371 // Now normalize offset to the field.
1372 Offset -= FieldOffset;
1374 // Maybe we need to work from the other end.
1375 if (TD->isLittleEndian()) Offset = FieldSize - (Offset + Size);
1377 // Add size and offset.
1378 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1379 Member->AddUInt(DW_AT_bit_size, 0, Size);
1380 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1383 // Add computation for offset.
1384 DIEBlock *Block = new DIEBlock();
1385 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1386 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1387 Block->ComputeSize(*this);
1388 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1390 // Add accessibility (public default unless is base class.
1391 if (MemberDesc->isProtected()) {
1392 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_protected);
1393 } else if (MemberDesc->isPrivate()) {
1394 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_private);
1395 } else if (Tag == DW_TAG_inheritance) {
1396 Member->AddUInt(DW_AT_accessibility, 0, DW_ACCESS_public);
1399 Ty->AddChild(Member);
1400 } else if (GlobalVariableDesc *StaticDesc =
1401 dyn_cast<GlobalVariableDesc>(Element)) {
1402 // Add static member.
1404 // Construct member debug information entry.
1405 DIE *Static = new DIE(DW_TAG_variable);
1407 // Add name and mangled name.
1408 const std::string &Name = StaticDesc->getDisplayName();
1409 const std::string &MangledName = StaticDesc->getName();
1410 Static->AddString(DW_AT_name, DW_FORM_string, Name);
1411 Static->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1415 AddSourceLine(Static, StaticDesc->getFile(), StaticDesc->getLine());
1418 if (TypeDesc *StaticTy = StaticDesc->getType()) {
1419 Static->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1420 NewType(Context, StaticTy, Unit));
1424 Static->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1425 Static->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1427 Ty->AddChild(Static);
1428 } else if (SubprogramDesc *MethodDesc =
1429 dyn_cast<SubprogramDesc>(Element)) {
1430 // Add member function.
1432 // Construct member debug information entry.
1433 DIE *Method = new DIE(DW_TAG_subprogram);
1435 // Add name and mangled name.
1436 const std::string &Name = MethodDesc->getDisplayName();
1437 const std::string &MangledName = MethodDesc->getName();
1438 bool IsCTor = false;
1441 Method->AddString(DW_AT_name, DW_FORM_string, MangledName);
1442 IsCTor = TyDesc->getName() == MangledName;
1444 Method->AddString(DW_AT_name, DW_FORM_string, Name);
1445 Method->AddString(DW_AT_MIPS_linkage_name, DW_FORM_string,
1450 AddSourceLine(Method, MethodDesc->getFile(), MethodDesc->getLine());
1453 if (CompositeTypeDesc *MethodTy =
1454 dyn_cast_or_null<CompositeTypeDesc>(MethodDesc->getType())) {
1455 // Get argument information.
1456 std::vector<DebugInfoDesc *> &Args = MethodTy->getElements();
1461 Method->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1462 NewType(Context, dyn_cast<TypeDesc>(Args[0]),
1467 for(unsigned i = 1, N = Args.size(); i < N; ++i) {
1468 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1469 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1470 NewType(Context, cast<TypeDesc>(Args[i]), Unit));
1471 Arg->AddUInt(DW_AT_artificial, DW_FORM_flag, 1);
1472 Method->AddChild(Arg);
1477 Method->AddUInt(DW_AT_external, DW_FORM_flag, 1);
1478 Method->AddUInt(DW_AT_declaration, DW_FORM_flag, 1);
1480 Ty->AddChild(Method);
1485 case DW_TAG_enumeration_type: {
1486 // Add enumerators to enumeration type.
1487 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1488 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1489 const std::string &Name = ED->getName();
1490 int64_t Value = ED->getValue();
1491 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1492 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1493 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1494 Ty->AddChild(Enumerator);
1499 case DW_TAG_subroutine_type: {
1500 // Add prototype flag.
1501 Ty->AddUInt(DW_AT_prototyped, DW_FORM_flag, 1);
1503 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1504 NewType(Context, dyn_cast<TypeDesc>(Elements[0]), Unit));
1507 for(unsigned i = 1, N = Elements.size(); i < N; ++i) {
1508 DIE *Arg = new DIE(DW_TAG_formal_parameter);
1509 Arg->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1510 NewType(Context, cast<TypeDesc>(Elements[i]), Unit));
1520 assert(Ty && "Type not supported yet");
1522 // Add size if non-zero (derived types don't have a size.)
1523 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1524 // Add name if not anonymous or intermediate type.
1525 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1526 // Add source line info if available.
1527 AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
1529 // Add to context owner.
1530 Context->AddChild(Ty);
1535 /// NewCompileUnit - Create new compile unit and it's debug information entry.
1537 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1539 // Construct debug information entry.
1540 DIE *Die = new DIE(DW_TAG_compile_unit);
1541 Die->AddDelta (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0),
1542 DWLabel("section_line", 0));
1543 // Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1544 // Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1545 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1546 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1547 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1548 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1550 // Add debug information entry to descriptor map.
1551 DIE *&Slot = getDieMapSlotFor(UnitDesc);
1554 // Construct compile unit.
1555 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1557 // Add Unit to compile unit map.
1558 DescToUnitMap[UnitDesc] = Unit;
1563 /// FindCompileUnit - Get the compile unit for the given descriptor.
1565 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1566 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1567 assert(Unit && "Missing compile unit.");
1571 /// NewGlobalVariable - Add a new global variable DIE.
1573 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1574 // Get the compile unit context.
1575 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1576 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1578 // Check for pre-existence.
1579 DIE *&Slot = Unit->getDieMapSlotFor(GVD);
1580 if (Slot) return Slot;
1582 // Get the global variable itself.
1583 GlobalVariable *GV = GVD->getGlobalVariable();
1584 // Generate the mangled name.
1585 std::string MangledName = Asm->Mang->getValueName(GV);
1587 // Gather the details (simplify add attribute code.)
1588 const std::string &Name = GVD->getName();
1590 // Get the global's type.
1591 DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit);
1593 // Create the globale variable DIE.
1594 DIE *VariableDie = new DIE(DW_TAG_variable);
1595 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1596 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1597 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1599 // Add source line info if available.
1600 AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
1603 DIEBlock *Block = new DIEBlock();
1604 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1605 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1606 Block->ComputeSize(*this);
1607 VariableDie->AddBlock(DW_AT_location, 0, Block);
1612 // Add to context owner.
1613 Unit->getDie()->AddChild(VariableDie);
1615 // Expose as global.
1616 // FIXME - need to check external flag.
1617 Unit->AddGlobal(Name, VariableDie);
1622 /// NewSubprogram - Add a new subprogram DIE.
1624 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1625 // Get the compile unit context.
1626 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1627 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1629 // Check for pre-existence.
1630 DIE *&Slot = Unit->getDieMapSlotFor(SPD);
1631 if (Slot) return Slot;
1633 // Gather the details (simplify add attribute code.)
1634 const std::string &Name = SPD->getName();
1635 DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit);
1636 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1638 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1639 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1641 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1643 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1644 SubprogramDie->AddUInt (DW_AT_prototyped, DW_FORM_flag, 1);
1646 // Add source line info if available.
1647 AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
1650 Slot = SubprogramDie;
1652 // Add to context owner.
1653 Unit->getDie()->AddChild(SubprogramDie);
1655 // Expose as global.
1656 Unit->AddGlobal(Name, SubprogramDie);
1658 return SubprogramDie;
1661 /// NewScopeVariable - Create a new scope variable.
1663 DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
1664 // Get the descriptor.
1665 VariableDesc *VD = DV->getDesc();
1667 // Translate tag to proper Dwarf tag. The result variable is dropped for now.
1669 switch (VD->getTag()) {
1670 case DW_TAG_return_variable: return NULL;
1671 case DW_TAG_arg_variable: Tag = DW_TAG_formal_parameter; break;
1672 case DW_TAG_auto_variable: // fall thru
1673 default: Tag = DW_TAG_variable; break;
1676 // Define variable debug information entry.
1677 DIE *VariableDie = new DIE(Tag);
1678 VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
1680 // Add source line info if available.
1681 AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
1683 // Add variable type.
1684 DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit);
1685 VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
1687 // Add variable address.
1688 MachineLocation Location;
1689 RI->getLocation(*MF, DV->getFrameIndex(), Location);
1690 AddAddress(VariableDie, DW_AT_location, Location);
1695 /// ConstructScope - Construct the components of a scope.
1697 void DwarfWriter::ConstructScope(DebugScope *ParentScope,
1698 DIE *ParentDie, CompileUnit *Unit) {
1699 // Add variables to scope.
1700 std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
1701 for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
1702 DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
1703 if (VariableDie) ParentDie->AddChild(VariableDie);
1706 // Add nested scopes.
1707 std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
1708 for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
1709 // Define the Scope debug information entry.
1710 DebugScope *Scope = Scopes[j];
1711 // FIXME - Ignore inlined functions for the time being.
1712 if (!Scope->getParent()) continue;
1714 DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
1716 // Add the scope bounds.
1717 if (unsigned StartID = Scope->getStartLabelID()) {
1718 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1719 DWLabel("loc", StartID));
1721 ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1722 DWLabel("func_begin", SubprogramCount));
1724 if (unsigned EndID = Scope->getEndLabelID()) {
1725 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1726 DWLabel("loc", EndID));
1728 ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1729 DWLabel("func_end", SubprogramCount));
1732 // Add the scope contents.
1733 ConstructScope(Scope, ScopeDie, Unit);
1734 ParentDie->AddChild(ScopeDie);
1738 /// ConstructRootScope - Construct the scope for the subprogram.
1740 void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
1741 // Exit if there is no root scope.
1742 if (!RootScope) return;
1744 // Get the subprogram debug information entry.
1745 SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
1747 // Get the compile unit context.
1748 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1749 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1751 // Get the subprogram die.
1752 DIE *SPDie = Unit->getDieMapSlotFor(SPD);
1753 assert(SPDie && "Missing subprogram descriptor");
1755 // Add the function bounds.
1756 SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
1757 DWLabel("func_begin", SubprogramCount));
1758 SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
1759 DWLabel("func_end", SubprogramCount));
1760 MachineLocation Location(RI->getFrameRegister(*MF));
1761 AddAddress(SPDie, DW_AT_frame_base, Location);
1763 ConstructScope(RootScope, SPDie, Unit);
1766 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1767 /// tools to recognize the object file contains Dwarf information.
1769 void DwarfWriter::EmitInitial() {
1770 // Check to see if we already emitted intial headers.
1771 if (didInitial) return;
1774 // Dwarf sections base addresses.
1775 Asm->SwitchToDataSection(DwarfFrameSection, 0);
1776 EmitLabel("section_frame", 0);
1777 Asm->SwitchToDataSection(DwarfInfoSection, 0);
1778 EmitLabel("section_info", 0);
1779 EmitLabel("info", 0);
1780 Asm->SwitchToDataSection(DwarfAbbrevSection, 0);
1781 EmitLabel("section_abbrev", 0);
1782 EmitLabel("abbrev", 0);
1783 Asm->SwitchToDataSection(DwarfARangesSection, 0);
1784 EmitLabel("section_aranges", 0);
1785 Asm->SwitchToDataSection(DwarfMacInfoSection, 0);
1786 EmitLabel("section_macinfo", 0);
1787 Asm->SwitchToDataSection(DwarfLineSection, 0);
1788 EmitLabel("section_line", 0);
1789 EmitLabel("line", 0);
1790 Asm->SwitchToDataSection(DwarfLocSection, 0);
1791 EmitLabel("section_loc", 0);
1792 Asm->SwitchToDataSection(DwarfPubNamesSection, 0);
1793 EmitLabel("section_pubnames", 0);
1794 Asm->SwitchToDataSection(DwarfStrSection, 0);
1795 EmitLabel("section_str", 0);
1796 Asm->SwitchToDataSection(DwarfRangesSection, 0);
1797 EmitLabel("section_ranges", 0);
1799 Asm->SwitchToTextSection(TextSection, 0);
1800 EmitLabel("text_begin", 0);
1801 Asm->SwitchToDataSection(DataSection, 0);
1802 EmitLabel("data_begin", 0);
1804 // Emit common frame information.
1805 EmitInitialDebugFrame();
1808 /// EmitDIE - Recusively Emits a debug information entry.
1810 void DwarfWriter::EmitDIE(DIE *Die) const {
1811 // Get the abbreviation for this DIE.
1812 unsigned AbbrevID = Die->getAbbrevID();
1813 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1817 // Emit the code (index) for the abbreviation.
1818 EmitULEB128Bytes(AbbrevID);
1819 EOL(std::string("Abbrev [" +
1821 "] 0x" + utohexstr(Die->getOffset()) +
1822 ":0x" + utohexstr(Die->getSize()) + " " +
1823 TagString(Abbrev.getTag())));
1825 const std::vector<DIEValue *> &Values = Die->getValues();
1826 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1828 // Emit the DIE attribute values.
1829 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1830 unsigned Attr = AbbrevData[i].getAttribute();
1831 unsigned Form = AbbrevData[i].getForm();
1832 assert(Form && "Too many attributes for DIE (check abbreviation)");
1835 case DW_AT_sibling: {
1836 EmitInt32(Die->SiblingOffset());
1840 // Emit an attribute using the defined form.
1841 Values[i]->EmitValue(*this, Form);
1846 EOL(AttributeString(Attr));
1849 // Emit the DIE children if any.
1850 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1851 const std::vector<DIE *> &Children = Die->getChildren();
1853 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1854 EmitDIE(Children[j]);
1857 EmitInt8(0); EOL("End Of Children Mark");
1861 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1863 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
1864 // Get the children.
1865 const std::vector<DIE *> &Children = Die->getChildren();
1867 // If not last sibling and has children then add sibling offset attribute.
1868 if (!Last && !Children.empty()) Die->AddSiblingOffset();
1870 // Record the abbreviation.
1871 Die->Complete(*this);
1873 // Get the abbreviation for this DIE.
1874 unsigned AbbrevID = Die->getAbbrevID();
1875 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1878 Die->setOffset(Offset);
1880 // Start the size with the size of abbreviation code.
1881 Offset += SizeULEB128(AbbrevID);
1883 const std::vector<DIEValue *> &Values = Die->getValues();
1884 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1886 // Emit the DIE attribute values.
1887 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1888 // Size attribute value.
1889 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1892 // Emit the DIE children if any.
1893 if (!Children.empty()) {
1894 assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
1895 "Children flag not set");
1897 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1898 Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
1901 // End of children marker.
1902 Offset += sizeof(int8_t);
1905 Die->setSize(Offset - Die->getOffset());
1909 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1911 void DwarfWriter::SizeAndOffsets() {
1913 // Process each compile unit.
1914 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1915 CompileUnit *Unit = CompileUnits[i];
1916 if (Unit->hasContent()) {
1917 // Compute size of compile unit header
1918 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1919 sizeof(int16_t) + // DWARF version number
1920 sizeof(int32_t) + // Offset Into Abbrev. Section
1921 sizeof(int8_t); // Pointer Size (in bytes)
1922 SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
1927 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
1929 void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
1930 std::vector<MachineMove *> &Moves) {
1931 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
1932 MachineMove *Move = Moves[i];
1933 unsigned LabelID = Move->getLabelID();
1934 const MachineLocation &Dst = Move->getDestination();
1935 const MachineLocation &Src = Move->getSource();
1937 // Advance row if new location.
1938 if (BaseLabel && LabelID && BaseLabelID != LabelID) {
1939 EmitULEB128Bytes(DW_CFA_advance_loc4);
1940 EOL("DW_CFA_advance_loc4");
1941 EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
1944 BaseLabelID = LabelID;
1948 // If advancing cfa.
1949 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
1950 if (!Src.isRegister()) {
1951 if (Src.getRegister() == MachineLocation::VirtualFP) {
1952 EmitULEB128Bytes(DW_CFA_def_cfa_offset);
1953 EOL("DW_CFA_def_cfa_offset");
1955 EmitULEB128Bytes(DW_CFA_def_cfa);
1956 EOL("DW_CFA_def_cfa");
1958 EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
1963 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
1964 TargetFrameInfo::StackGrowsUp ?
1965 AddressSize : -AddressSize;
1967 EmitULEB128Bytes(Src.getOffset() / stackGrowth);
1976 /// EmitDebugInfo - Emit the debug info section.
1978 void DwarfWriter::EmitDebugInfo() const {
1979 // Start debug info section.
1980 Asm->SwitchToDataSection(DwarfInfoSection, 0);
1982 // Process each compile unit.
1983 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1984 CompileUnit *Unit = CompileUnits[i];
1986 if (Unit->hasContent()) {
1987 DIE *Die = Unit->getDie();
1988 // Emit the compile units header.
1989 EmitLabel("info_begin", Unit->getID());
1990 // Emit size of content not including length itself
1991 unsigned ContentSize = Die->getSize() +
1992 sizeof(int16_t) + // DWARF version number
1993 sizeof(int32_t) + // Offset Into Abbrev. Section
1994 sizeof(int8_t); // Pointer Size (in bytes)
1996 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1997 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1998 EmitDifference("abbrev_begin", 0, "section_abbrev", 0);
1999 EOL("Offset Into Abbrev. Section");
2000 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
2003 EmitLabel("info_end", Unit->getID());
2010 /// EmitAbbreviations - Emit the abbreviation section.
2012 void DwarfWriter::EmitAbbreviations() const {
2013 // Check to see if it is worth the effort.
2014 if (!Abbreviations.empty()) {
2015 // Start the debug abbrev section.
2016 Asm->SwitchToDataSection(DwarfAbbrevSection, 0);
2018 EmitLabel("abbrev_begin", 0);
2020 // For each abbrevation.
2021 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
2022 AbbrevID <= NAID; ++AbbrevID) {
2023 // Get abbreviation data
2024 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
2026 // Emit the abbrevations code (base 1 index.)
2027 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
2029 // Emit the abbreviations data.
2035 EmitLabel("abbrev_end", 0);
2041 /// EmitDebugLines - Emit source line information.
2043 void DwarfWriter::EmitDebugLines() const {
2044 // Minimum line delta, thus ranging from -10..(255-10).
2045 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
2046 // Maximum line delta, thus ranging from -10..(255-10).
2047 const int MaxLineDelta = 255 + MinLineDelta;
2049 // Start the dwarf line section.
2050 Asm->SwitchToDataSection(DwarfLineSection, 0);
2052 // Construct the section header.
2054 EmitDifference("line_end", 0, "line_begin", 0);
2055 EOL("Length of Source Line Info");
2056 EmitLabel("line_begin", 0);
2058 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
2060 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
2061 EOL("Prolog Length");
2062 EmitLabel("line_prolog_begin", 0);
2064 EmitInt8(1); EOL("Minimum Instruction Length");
2066 EmitInt8(1); EOL("Default is_stmt_start flag");
2068 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
2070 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
2072 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
2074 // Line number standard opcode encodings argument count
2075 EmitInt8(0); EOL("DW_LNS_copy arg count");
2076 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
2077 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
2078 EmitInt8(1); EOL("DW_LNS_set_file arg count");
2079 EmitInt8(1); EOL("DW_LNS_set_column arg count");
2080 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
2081 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
2082 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
2083 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
2085 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
2086 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
2088 // Emit directories.
2089 for (unsigned DirectoryID = 1, NDID = Directories.size();
2090 DirectoryID <= NDID; ++DirectoryID) {
2091 EmitString(Directories[DirectoryID]); EOL("Directory");
2093 EmitInt8(0); EOL("End of directories");
2096 for (unsigned SourceID = 1, NSID = SourceFiles.size();
2097 SourceID <= NSID; ++SourceID) {
2098 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2099 EmitString(SourceFile.getName()); EOL("Source");
2100 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
2101 EmitULEB128Bytes(0); EOL("Mod date");
2102 EmitULEB128Bytes(0); EOL("File size");
2104 EmitInt8(0); EOL("End of files");
2106 EmitLabel("line_prolog_end", 0);
2108 // A sequence for each text section.
2109 for (unsigned j = 0, M = SectionSourceLines.size(); j < M; ++j) {
2110 // Isolate current sections line info.
2111 const std::vector<SourceLineInfo *> &LineInfos = SectionSourceLines[j];
2115 << Asm->CommentString << " "
2117 << SectionMap[j + 1].c_str() << "\n";
2120 // Dwarf assumes we start with first line of first source file.
2121 unsigned Source = 1;
2124 // Construct rows of the address, source, line, column matrix.
2125 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
2126 SourceLineInfo *LineInfo = LineInfos[i];
2129 unsigned SourceID = LineInfo->getSourceID();
2130 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
2131 unsigned DirectoryID = SourceFile.getDirectoryID();
2133 << Asm->CommentString << " "
2134 << Directories[DirectoryID]
2135 << SourceFile.getName() << ":"
2136 << LineInfo->getLine() << "\n";
2139 // Define the line address.
2140 EmitInt8(0); EOL("Extended Op");
2141 EmitInt8(4 + 1); EOL("Op size");
2142 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2143 EmitReference("loc", LineInfo->getLabelID()); EOL("Location label");
2145 // If change of source, then switch to the new source.
2146 if (Source != LineInfo->getSourceID()) {
2147 Source = LineInfo->getSourceID();
2148 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
2149 EmitULEB128Bytes(Source); EOL("New Source");
2152 // If change of line.
2153 if (Line != LineInfo->getLine()) {
2154 // Determine offset.
2155 int Offset = LineInfo->getLine() - Line;
2156 int Delta = Offset - MinLineDelta;
2159 Line = LineInfo->getLine();
2161 // If delta is small enough and in range...
2162 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
2163 // ... then use fast opcode.
2164 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
2166 // ... otherwise use long hand.
2167 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
2168 EmitSLEB128Bytes(Offset); EOL("Line Offset");
2169 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2172 // Copy the previous row (different address or source)
2173 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
2177 // Define last address of section.
2178 EmitInt8(0); EOL("Extended Op");
2179 EmitInt8(4 + 1); EOL("Op size");
2180 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
2181 EmitReference("section_end", j + 1); EOL("Section end label");
2183 // Mark end of matrix.
2184 EmitInt8(0); EOL("DW_LNE_end_sequence");
2185 EmitULEB128Bytes(1); O << "\n";
2186 EmitInt8(1); O << "\n";
2189 EmitLabel("line_end", 0);
2194 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
2196 void DwarfWriter::EmitInitialDebugFrame() {
2198 Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
2199 TargetFrameInfo::StackGrowsUp ?
2200 AddressSize : -AddressSize;
2202 // Start the dwarf frame section.
2203 Asm->SwitchToDataSection(DwarfFrameSection, 0);
2205 EmitLabel("frame_common", 0);
2206 EmitDifference("frame_common_end", 0,
2207 "frame_common_begin", 0);
2208 EOL("Length of Common Information Entry");
2210 EmitLabel("frame_common_begin", 0);
2211 EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
2212 EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
2213 EmitString(""); EOL("CIE Augmentation");
2214 EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
2215 EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");
2216 EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
2218 std::vector<MachineMove *> Moves;
2219 RI->getInitialFrameState(Moves);
2220 EmitFrameMoves(NULL, 0, Moves);
2221 for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
2224 EmitLabel("frame_common_end", 0);
2229 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
2231 void DwarfWriter::EmitFunctionDebugFrame() {
2232 // Start the dwarf frame section.
2233 Asm->SwitchToDataSection(DwarfFrameSection, 0);
2235 EmitDifference("frame_end", SubprogramCount,
2236 "frame_begin", SubprogramCount);
2237 EOL("Length of Frame Information Entry");
2239 EmitLabel("frame_begin", SubprogramCount);
2241 EmitDifference("frame_common", 0, "section_frame", 0);
2242 EOL("FDE CIE offset");
2244 EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
2245 EmitDifference("func_end", SubprogramCount,
2246 "func_begin", SubprogramCount);
2247 EOL("FDE address range");
2249 std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
2251 EmitFrameMoves("func_begin", SubprogramCount, Moves);
2254 EmitLabel("frame_end", SubprogramCount);
2259 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
2261 void DwarfWriter::EmitDebugPubNames() {
2262 // Start the dwarf pubnames section.
2263 Asm->SwitchToDataSection(DwarfPubNamesSection, 0);
2265 // Process each compile unit.
2266 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2267 CompileUnit *Unit = CompileUnits[i];
2269 if (Unit->hasContent()) {
2270 EmitDifference("pubnames_end", Unit->getID(),
2271 "pubnames_begin", Unit->getID());
2272 EOL("Length of Public Names Info");
2274 EmitLabel("pubnames_begin", Unit->getID());
2276 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
2278 EmitDifference("info_begin", Unit->getID(), "section_info", 0);
2279 EOL("Offset of Compilation Unit Info");
2281 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
2282 EOL("Compilation Unit Length");
2284 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
2286 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
2289 const std::string &Name = GI->first;
2290 DIE * Entity = GI->second;
2292 EmitInt32(Entity->getOffset()); EOL("DIE offset");
2293 EmitString(Name); EOL("External Name");
2296 EmitInt32(0); EOL("End Mark");
2297 EmitLabel("pubnames_end", Unit->getID());
2304 /// EmitDebugStr - Emit visible names into a debug str section.
2306 void DwarfWriter::EmitDebugStr() {
2307 // Check to see if it is worth the effort.
2308 if (!StringPool.empty()) {
2309 // Start the dwarf str section.
2310 Asm->SwitchToDataSection(DwarfStrSection, 0);
2312 // For each of strings in the string pool.
2313 for (unsigned StringID = 1, N = StringPool.size();
2314 StringID <= N; ++StringID) {
2315 // Emit a label for reference from debug information entries.
2316 EmitLabel("string", StringID);
2317 // Emit the string itself.
2318 const std::string &String = StringPool[StringID];
2319 EmitString(String); O << "\n";
2326 /// EmitDebugLoc - Emit visible names into a debug loc section.
2328 void DwarfWriter::EmitDebugLoc() {
2329 // Start the dwarf loc section.
2330 Asm->SwitchToDataSection(DwarfLocSection, 0);
2335 /// EmitDebugARanges - Emit visible names into a debug aranges section.
2337 void DwarfWriter::EmitDebugARanges() {
2338 // Start the dwarf aranges section.
2339 Asm->SwitchToDataSection(DwarfARangesSection, 0);
2343 // Process each compile unit.
2344 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2345 CompileUnit *Unit = CompileUnits[i];
2347 if (Unit->hasContent()) {
2348 // Don't include size of length
2349 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2351 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2353 EmitReference("info_begin", Unit->getID());
2354 EOL("Offset of Compilation Unit Info");
2356 EmitInt8(AddressSize); EOL("Size of Address");
2358 EmitInt8(0); EOL("Size of Segment Descriptor");
2360 EmitInt16(0); EOL("Pad (1)");
2361 EmitInt16(0); EOL("Pad (2)");
2364 EmitReference("text_begin", 0); EOL("Address");
2365 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2367 EmitInt32(0); EOL("EOM (1)");
2368 EmitInt32(0); EOL("EOM (2)");
2376 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2378 void DwarfWriter::EmitDebugRanges() {
2379 // Start the dwarf ranges section.
2380 Asm->SwitchToDataSection(DwarfRangesSection, 0);
2385 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2387 void DwarfWriter::EmitDebugMacInfo() {
2388 // Start the dwarf macinfo section.
2389 Asm->SwitchToDataSection(DwarfMacInfoSection, 0);
2394 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2396 void DwarfWriter::ConstructCompileUnitDIEs() {
2397 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2399 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2400 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
2401 CompileUnits.push_back(Unit);
2405 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2407 void DwarfWriter::ConstructGlobalDIEs() {
2408 std::vector<GlobalVariableDesc *> GlobalVariables =
2409 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
2411 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2412 GlobalVariableDesc *GVD = GlobalVariables[i];
2413 NewGlobalVariable(GVD);
2417 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2419 void DwarfWriter::ConstructSubprogramDIEs() {
2420 std::vector<SubprogramDesc *> Subprograms =
2421 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
2423 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2424 SubprogramDesc *SPD = Subprograms[i];
2429 //===----------------------------------------------------------------------===//
2430 // Main entry points.
2433 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2436 , TD(Asm->TM.getTargetData())
2437 , RI(Asm->TM.getRegisterInfo())
2443 , SubprogramCount(0)
2450 , SectionSourceLines()
2451 , AddressSize(sizeof(int32_t))
2456 , DwarfAbbrevSection(".debug_abbrev")
2457 , DwarfInfoSection(".debug_info")
2458 , DwarfLineSection(".debug_line")
2459 , DwarfFrameSection(".debug_frame")
2460 , DwarfPubNamesSection(".debug_pubnames")
2461 , DwarfPubTypesSection(".debug_pubtypes")
2462 , DwarfStrSection(".debug_str")
2463 , DwarfLocSection(".debug_loc")
2464 , DwarfARangesSection(".debug_aranges")
2465 , DwarfRangesSection(".debug_ranges")
2466 , DwarfMacInfoSection(".debug_macinfo")
2467 , TextSection(".text")
2468 , DataSection(".data")
2470 DwarfWriter::~DwarfWriter() {
2471 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2472 delete CompileUnits[i];
2476 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
2477 /// created it. Set by the target AsmPrinter.
2478 void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
2479 // Make sure initial declarations are made.
2480 if (!DebugInfo && DI->hasInfo()) {
2484 // Emit initial sections
2487 // Create all the compile unit DIEs.
2488 ConstructCompileUnitDIEs();
2490 // Create DIEs for each of the externally visible global variables.
2491 ConstructGlobalDIEs();
2493 // Create DIEs for each of the externally visible subprograms.
2494 ConstructSubprogramDIEs();
2496 // Prime section data.
2497 SectionMap.insert(std::string("\t") + TextSection);
2501 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
2503 void DwarfWriter::BeginModule(Module *M) {
2506 if (!ShouldEmitDwarf()) return;
2507 EOL("Dwarf Begin Module");
2510 /// EndModule - Emit all Dwarf sections that should come after the content.
2512 void DwarfWriter::EndModule() {
2513 if (!ShouldEmitDwarf()) return;
2514 EOL("Dwarf End Module");
2516 // Standard sections final addresses.
2517 Asm->SwitchToTextSection(TextSection, 0);
2518 EmitLabel("text_end", 0);
2519 Asm->SwitchToDataSection(DataSection, 0);
2520 EmitLabel("data_end", 0);
2522 // End text sections.
2523 for (unsigned i = 1, N = SectionMap.size(); i <= N; ++i) {
2524 Asm->SwitchToTextSection(SectionMap[i].c_str(), 0);
2525 EmitLabel("section_end", i);
2528 // Compute DIE offsets and sizes.
2531 // Emit all the DIEs into a debug info section
2534 // Corresponding abbreviations into a abbrev section.
2535 EmitAbbreviations();
2537 // Emit source line correspondence into a debug line section.
2540 // Emit info into a debug pubnames section.
2541 EmitDebugPubNames();
2543 // Emit info into a debug str section.
2546 // Emit info into a debug loc section.
2549 // Emit info into a debug aranges section.
2552 // Emit info into a debug ranges section.
2555 // Emit info into a debug macinfo section.
2559 /// BeginFunction - Gather pre-function debug information. Assumes being
2560 /// emitted immediately after the function entry point.
2561 void DwarfWriter::BeginFunction(MachineFunction *MF) {
2564 if (!ShouldEmitDwarf()) return;
2565 EOL("Dwarf Begin Function");
2567 // Begin accumulating function debug information.
2568 DebugInfo->BeginFunction(MF);
2570 // Assumes in correct section after the entry point.
2571 EmitLabel("func_begin", ++SubprogramCount);
2574 /// EndFunction - Gather and emit post-function debug information.
2576 void DwarfWriter::EndFunction() {
2577 if (!ShouldEmitDwarf()) return;
2578 EOL("Dwarf End Function");
2580 // Define end label for subprogram.
2581 EmitLabel("func_end", SubprogramCount);
2583 // Get function line info.
2584 std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
2586 if (!LineInfos.empty()) {
2587 // Get section line info.
2588 unsigned ID = SectionMap.insert(Asm->CurrentSection);
2589 if (SectionSourceLines.size() < ID) SectionSourceLines.resize(ID);
2590 std::vector<SourceLineInfo *> &SectionLineInfos =SectionSourceLines[ID-1];
2591 // Append the function info to section info.
2592 SectionLineInfos.insert(SectionLineInfos.end(),
2593 LineInfos.begin(), LineInfos.end());
2596 // Construct scopes for subprogram.
2597 ConstructRootScope(DebugInfo->getRootScope());
2599 // Emit function frame information.
2600 EmitFunctionDebugFrame();
2602 // Reset the line numbers for the next function.
2605 // Clear function debug information.
2606 DebugInfo->EndFunction();