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/Support/Dwarf.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Target/TargetMachine.h"
29 using namespace llvm::dwarf;
32 DwarfVerbose("dwarf-verbose", cl::Hidden,
33 cl::desc("Add comments to Dwarf directives."));
37 //===----------------------------------------------------------------------===//
38 // Forward declarations.
43 //===----------------------------------------------------------------------===//
44 // CompileUnit - This dwarf writer support class manages information associate
45 // with a source file.
48 CompileUnitDesc *Desc; // Compile unit debug descriptor.
49 unsigned ID; // File ID for source.
50 DIE *Die; // Compile unit die.
51 std::map<std::string, DIE *> Globals; // A map of globally visible named
52 // entities for this unit.
55 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
65 CompileUnitDesc *getDesc() const { return Desc; }
66 unsigned getID() const { return ID; }
67 DIE* getDie() const { return Die; }
68 std::map<std::string, DIE *> &getGlobals() { return Globals; }
70 /// hasContent - Return true if this compile unit has something to write out.
72 bool hasContent() const;
74 /// AddGlobal - Add a new global entity to the compile unit.
76 void AddGlobal(const std::string &Name, DIE *Die);
80 //===----------------------------------------------------------------------===//
81 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
82 // Dwarf abbreviation.
85 unsigned Attribute; // Dwarf attribute code.
86 unsigned Form; // Dwarf form code.
89 DIEAbbrevData(unsigned A, unsigned F)
95 unsigned getAttribute() const { return Attribute; }
96 unsigned getForm() const { return Form; }
98 /// operator== - Used by DIEAbbrev to locate entry.
100 bool operator==(const DIEAbbrevData &DAD) const {
101 return Attribute == DAD.Attribute && Form == DAD.Form;
104 /// operator!= - Used by DIEAbbrev to locate entry.
106 bool operator!=(const DIEAbbrevData &DAD) const {
107 return Attribute != DAD.Attribute || Form != DAD.Form;
110 /// operator< - Used by DIEAbbrev to locate entry.
112 bool operator<(const DIEAbbrevData &DAD) const {
113 return Attribute < DAD.Attribute ||
114 (Attribute == DAD.Attribute && Form < DAD.Form);
118 //===----------------------------------------------------------------------===//
119 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
120 // information object.
123 unsigned Tag; // Dwarf tag code.
124 unsigned ChildrenFlag; // Dwarf children flag.
125 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
129 DIEAbbrev(unsigned T, unsigned C)
137 unsigned getTag() const { return Tag; }
138 unsigned getChildrenFlag() const { return ChildrenFlag; }
139 const std::vector<DIEAbbrevData> &getData() const { return Data; }
140 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
142 /// operator== - Used by UniqueVector to locate entry.
144 bool operator==(const DIEAbbrev &DA) const;
146 /// operator< - Used by UniqueVector to locate entry.
148 bool operator<(const DIEAbbrev &DA) const;
150 /// AddAttribute - Adds another set of attribute information to the
152 void AddAttribute(unsigned Attribute, unsigned Form) {
153 Data.push_back(DIEAbbrevData(Attribute, Form));
156 /// Emit - Print the abbreviation using the specified Dwarf writer.
158 void Emit(const DwarfWriter &DW) const;
161 void print(std::ostream &O);
166 //===----------------------------------------------------------------------===//
167 // DIEValue - A debug information entry value.
181 unsigned Type; // Type of the value
183 DIEValue(unsigned T) : Type(T) {}
184 virtual ~DIEValue() {}
186 // Implement isa/cast/dyncast.
187 static bool classof(const DIEValue *) { return true; }
189 /// EmitValue - Emit value via the Dwarf writer.
191 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
193 /// SizeOf - Return the size of a value in bytes.
195 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
198 //===----------------------------------------------------------------------===//
199 // DWInteger - An integer value DIE.
201 class DIEInteger : public DIEValue {
206 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
208 // Implement isa/cast/dyncast.
209 static bool classof(const DIEInteger *) { return true; }
210 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
212 /// BestForm - Choose the best form for integer.
214 unsigned BestForm(bool IsSigned);
216 /// EmitValue - Emit integer of appropriate size.
218 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
220 /// SizeOf - Determine size of integer value in bytes.
222 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
225 //===----------------------------------------------------------------------===//
226 // DIEString - A string value DIE.
228 struct DIEString : public DIEValue {
229 const std::string String;
231 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
233 // Implement isa/cast/dyncast.
234 static bool classof(const DIEString *) { return true; }
235 static bool classof(const DIEValue *S) { return S->Type == isString; }
237 /// EmitValue - Emit string value.
239 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
241 /// SizeOf - Determine size of string value in bytes.
243 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
246 //===----------------------------------------------------------------------===//
247 // DIEDwarfLabel - A Dwarf internal label expression DIE.
249 struct DIEDwarfLabel : public DIEValue {
252 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
254 // Implement isa/cast/dyncast.
255 static bool classof(const DIEDwarfLabel *) { return true; }
256 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
258 /// EmitValue - Emit label value.
260 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
262 /// SizeOf - Determine size of label value in bytes.
264 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
268 //===----------------------------------------------------------------------===//
269 // DIEObjectLabel - A label to an object in code or data.
271 struct DIEObjectLabel : public DIEValue {
272 const std::string Label;
274 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
276 // Implement isa/cast/dyncast.
277 static bool classof(const DIEObjectLabel *) { return true; }
278 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
280 /// EmitValue - Emit label value.
282 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
284 /// SizeOf - Determine size of label value in bytes.
286 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
289 //===----------------------------------------------------------------------===//
290 // DIEDelta - A simple label difference DIE.
292 struct DIEDelta : public DIEValue {
293 const DWLabel LabelHi;
294 const DWLabel LabelLo;
296 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
297 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
299 // Implement isa/cast/dyncast.
300 static bool classof(const DIEDelta *) { return true; }
301 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
303 /// EmitValue - Emit delta value.
305 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
307 /// SizeOf - Determine size of delta value in bytes.
309 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
312 //===----------------------------------------------------------------------===//
313 // DIEntry - A pointer to a debug information entry.
315 struct DIEntry : public DIEValue {
318 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
320 // Implement isa/cast/dyncast.
321 static bool classof(const DIEntry *) { return true; }
322 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
324 /// EmitValue - Emit die entry offset.
326 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
328 /// SizeOf - Determine size of die entry in bytes.
330 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
333 //===----------------------------------------------------------------------===//
334 // DIEBlock - A block of values. Primarily used for location expressions.
336 struct DIEBlock : public DIEValue {
337 unsigned Size; // Size in bytes excluding size header.
338 std::vector<unsigned> Forms; // Data forms.
339 std::vector<DIEValue *> Values; // Block values.
349 // Implement isa/cast/dyncast.
350 static bool classof(const DIEBlock *) { return true; }
351 static bool classof(const DIEValue *E) { return E->Type == isBlock; }
353 /// ComputeSize - calculate the size of the block.
355 unsigned ComputeSize(DwarfWriter &DW);
357 /// BestForm - Choose the best form for data.
361 /// EmitValue - Emit block data.
363 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
365 /// SizeOf - Determine size of block data in bytes.
367 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
369 /// AddUInt - Add an unsigned integer value.
371 void AddUInt(unsigned Form, uint64_t Integer);
373 /// AddSInt - Add an signed integer value.
375 void AddSInt(unsigned Form, int64_t Integer);
377 /// AddString - Add a std::string value.
379 void AddString(unsigned Form, const std::string &String);
381 /// AddLabel - Add a Dwarf label value.
383 void AddLabel(unsigned Form, const DWLabel &Label);
385 /// AddObjectLabel - Add a non-Dwarf label value.
387 void AddObjectLabel(unsigned Form, const std::string &Label);
389 /// AddDelta - Add a label delta value.
391 void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
393 /// AddDIEntry - Add a DIE value.
395 void AddDIEntry(unsigned Form, DIE *Entry);
399 //===----------------------------------------------------------------------===//
400 // DIE - A structured debug information entry. Has an abbreviation which
401 // describes it's organization.
404 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
405 unsigned AbbrevID; // Decribing abbreviation ID.
406 unsigned Offset; // Offset in debug info section.
407 unsigned Size; // Size of instance + children.
408 std::vector<DIE *> Children; // Children DIEs.
409 std::vector<DIEValue *> Values; // Attributes values.
416 unsigned getAbbrevID() const { return AbbrevID; }
417 unsigned getOffset() const { return Offset; }
418 unsigned getSize() const { return Size; }
419 const std::vector<DIE *> &getChildren() const { return Children; }
420 const std::vector<DIEValue *> &getValues() const { return Values; }
421 void setOffset(unsigned O) { Offset = O; }
422 void setSize(unsigned S) { Size = S; }
424 /// SiblingOffset - Return the offset of the debug information entry's
426 unsigned SiblingOffset() const { return Offset + Size; }
428 /// AddUInt - Add an unsigned integer attribute data and value.
430 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
432 /// AddSInt - Add an signed integer attribute data and value.
434 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
436 /// AddString - Add a std::string attribute data and value.
438 void AddString(unsigned Attribute, unsigned Form,
439 const std::string &String);
441 /// AddLabel - Add a Dwarf label attribute data and value.
443 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
445 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
447 void AddObjectLabel(unsigned Attribute, unsigned Form,
448 const std::string &Label);
450 /// AddDelta - Add a label delta attribute data and value.
452 void AddDelta(unsigned Attribute, unsigned Form,
453 const DWLabel &Hi, const DWLabel &Lo);
455 /// AddDIEntry - Add a DIE attribute data and value.
457 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
459 /// AddBlock - Add block data.
461 void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
463 /// Complete - Indicate that all attributes have been added and
464 /// ready to get an abbreviation ID.
466 void Complete(DwarfWriter &DW);
468 /// AddChild - Add a child to the DIE.
469 void AddChild(DIE *Child);
472 } // End of namespace llvm
474 //===----------------------------------------------------------------------===//
476 CompileUnit::~CompileUnit() {
480 /// hasContent - Return true if this compile unit has something to write out.
482 bool CompileUnit::hasContent() const {
483 return !Die->getChildren().empty();
486 /// AddGlobal - Add a new global entity to the compile unit.
488 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
492 //===----------------------------------------------------------------------===//
494 /// operator== - Used by UniqueVector to locate entry.
496 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
497 if (Tag != DA.Tag) return false;
498 if (ChildrenFlag != DA.ChildrenFlag) return false;
499 if (Data.size() != DA.Data.size()) return false;
501 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
502 if (Data[i] != DA.Data[i]) return false;
508 /// operator< - Used by UniqueVector to locate entry.
510 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
511 if (Tag != DA.Tag) return Tag < DA.Tag;
512 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
513 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
515 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
516 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
522 /// Emit - Print the abbreviation using the specified Dwarf writer.
524 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
525 // Emit its Dwarf tag type.
526 DW.EmitULEB128Bytes(Tag);
527 DW.EOL(TagString(Tag));
529 // Emit whether it has children DIEs.
530 DW.EmitULEB128Bytes(ChildrenFlag);
531 DW.EOL(ChildrenString(ChildrenFlag));
533 // For each attribute description.
534 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
535 const DIEAbbrevData &AttrData = Data[i];
537 // Emit attribute type.
538 DW.EmitULEB128Bytes(AttrData.getAttribute());
539 DW.EOL(AttributeString(AttrData.getAttribute()));
542 DW.EmitULEB128Bytes(AttrData.getForm());
543 DW.EOL(FormEncodingString(AttrData.getForm()));
546 // Mark end of abbreviation.
547 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
548 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
552 void DIEAbbrev::print(std::ostream &O) {
553 O << "Abbreviation @"
554 << std::hex << (intptr_t)this << std::dec
558 << ChildrenString(ChildrenFlag)
561 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
563 << AttributeString(Data[i].getAttribute())
565 << FormEncodingString(Data[i].getForm())
569 void DIEAbbrev::dump() { print(std::cerr); }
572 //===----------------------------------------------------------------------===//
574 /// BestForm - Choose the best form for integer.
576 unsigned DIEInteger::BestForm(bool IsSigned) {
578 if ((char)Integer == (signed)Integer) return DW_FORM_data1;
579 if ((short)Integer == (signed)Integer) return DW_FORM_data2;
580 if ((int)Integer == (signed)Integer) return DW_FORM_data4;
582 if ((unsigned char)Integer == Integer) return DW_FORM_data1;
583 if ((unsigned short)Integer == Integer) return DW_FORM_data2;
584 if ((unsigned int)Integer == Integer) return DW_FORM_data4;
586 return DW_FORM_data8;
589 /// EmitValue - Emit integer of appropriate size.
591 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
593 case DW_FORM_flag: // Fall thru
594 case DW_FORM_data1: DW.EmitInt8(Integer); break;
595 case DW_FORM_data2: DW.EmitInt16(Integer); break;
596 case DW_FORM_data4: DW.EmitInt32(Integer); break;
597 case DW_FORM_data8: DW.EmitInt64(Integer); break;
598 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
599 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
600 default: assert(0 && "DIE Value form not supported yet"); break;
604 /// SizeOf - Determine size of integer value in bytes.
606 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
608 case DW_FORM_flag: // Fall thru
609 case DW_FORM_data1: return sizeof(int8_t);
610 case DW_FORM_data2: return sizeof(int16_t);
611 case DW_FORM_data4: return sizeof(int32_t);
612 case DW_FORM_data8: return sizeof(int64_t);
613 case DW_FORM_udata: return DW.SizeULEB128(Integer);
614 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
615 default: assert(0 && "DIE Value form not supported yet"); break;
620 //===----------------------------------------------------------------------===//
622 /// EmitValue - Emit string value.
624 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
625 DW.EmitString(String);
628 /// SizeOf - Determine size of string value in bytes.
630 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
631 return String.size() + sizeof(char); // sizeof('\0');
634 //===----------------------------------------------------------------------===//
636 /// EmitValue - Emit label value.
638 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
639 DW.EmitReference(Label);
642 /// SizeOf - Determine size of label value in bytes.
644 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
645 return DW.getAddressSize();
648 //===----------------------------------------------------------------------===//
650 /// EmitValue - Emit label value.
652 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
653 DW.EmitReference(Label);
656 /// SizeOf - Determine size of label value in bytes.
658 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
659 return DW.getAddressSize();
662 //===----------------------------------------------------------------------===//
664 /// EmitValue - Emit delta value.
666 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
667 DW.EmitDifference(LabelHi, LabelLo);
670 /// SizeOf - Determine size of delta value in bytes.
672 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
673 return DW.getAddressSize();
676 //===----------------------------------------------------------------------===//
677 /// EmitValue - Emit die entry offset.
679 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
680 DW.EmitInt32(Entry->getOffset());
683 /// SizeOf - Determine size of die value in bytes.
685 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
686 return sizeof(int32_t);
689 //===----------------------------------------------------------------------===//
691 DIEBlock::~DIEBlock() {
692 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
697 /// ComputeSize - calculate the size of the block.
699 unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
701 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
702 Size += Values[i]->SizeOf(DW, Forms[i]);
707 /// BestForm - Choose the best form for data.
709 unsigned DIEBlock::BestForm() {
710 if ((unsigned char)Size == Size) return DW_FORM_block1;
711 if ((unsigned short)Size == Size) return DW_FORM_block2;
712 if ((unsigned int)Size == Size) return DW_FORM_block4;
713 return DW_FORM_block;
716 /// EmitValue - Emit block data.
718 void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
720 case DW_FORM_block1: DW.EmitInt8(Size); break;
721 case DW_FORM_block2: DW.EmitInt16(Size); break;
722 case DW_FORM_block4: DW.EmitInt32(Size); break;
723 case DW_FORM_block: DW.EmitULEB128Bytes(Size); break;
724 default: assert(0 && "Improper form for block"); break;
726 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
728 Values[i]->EmitValue(DW, Forms[i]);
732 /// SizeOf - Determine size of block data in bytes.
734 unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
736 case DW_FORM_block1: return Size + sizeof(int8_t);
737 case DW_FORM_block2: return Size + sizeof(int16_t);
738 case DW_FORM_block4: return Size + sizeof(int32_t);
739 case DW_FORM_block: return Size + DW.SizeULEB128(Size);
740 default: assert(0 && "Improper form for block"); break;
745 /// AddUInt - Add an unsigned integer value.
747 void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
748 DIEInteger *DI = new DIEInteger(Integer);
749 Values.push_back(DI);
750 if (Form == 0) Form = DI->BestForm(false);
751 Forms.push_back(Form);
754 /// AddSInt - Add an signed integer value.
756 void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
757 DIEInteger *DI = new DIEInteger(Integer);
758 Values.push_back(DI);
759 if (Form == 0) Form = DI->BestForm(true);
760 Forms.push_back(Form);
763 /// AddString - Add a std::string value.
765 void DIEBlock::AddString(unsigned Form, const std::string &String) {
766 Values.push_back(new DIEString(String));
767 Forms.push_back(Form);
770 /// AddLabel - Add a Dwarf label value.
772 void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
773 Values.push_back(new DIEDwarfLabel(Label));
774 Forms.push_back(Form);
777 /// AddObjectLabel - Add a non-Dwarf label value.
779 void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
780 Values.push_back(new DIEObjectLabel(Label));
781 Forms.push_back(Form);
784 /// AddDelta - Add a label delta value.
786 void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
787 Values.push_back(new DIEDelta(Hi, Lo));
788 Forms.push_back(Form);
791 /// AddDIEntry - Add a DIE value.
793 void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
794 Values.push_back(new DIEntry(Entry));
795 Forms.push_back(Form);
798 //===----------------------------------------------------------------------===//
800 DIE::DIE(unsigned Tag)
801 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
810 if (Abbrev) delete Abbrev;
812 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
816 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
821 /// AddUInt - Add an unsigned integer attribute data and value.
823 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
824 DIEInteger *DI = new DIEInteger(Integer);
825 Values.push_back(DI);
826 if (!Form) Form = DI->BestForm(false);
827 Abbrev->AddAttribute(Attribute, Form);
830 /// AddSInt - Add an signed integer attribute data and value.
832 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
833 DIEInteger *DI = new DIEInteger(Integer);
834 Values.push_back(DI);
835 if (!Form) Form = DI->BestForm(true);
836 Abbrev->AddAttribute(Attribute, Form);
839 /// AddString - Add a std::string attribute data and value.
841 void DIE::AddString(unsigned Attribute, unsigned Form,
842 const std::string &String) {
843 Values.push_back(new DIEString(String));
844 Abbrev->AddAttribute(Attribute, Form);
847 /// AddLabel - Add a Dwarf label attribute data and value.
849 void DIE::AddLabel(unsigned Attribute, unsigned Form,
850 const DWLabel &Label) {
851 Values.push_back(new DIEDwarfLabel(Label));
852 Abbrev->AddAttribute(Attribute, Form);
855 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
857 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
858 const std::string &Label) {
859 Values.push_back(new DIEObjectLabel(Label));
860 Abbrev->AddAttribute(Attribute, Form);
863 /// AddDelta - Add a label delta attribute data and value.
865 void DIE::AddDelta(unsigned Attribute, unsigned Form,
866 const DWLabel &Hi, const DWLabel &Lo) {
867 Values.push_back(new DIEDelta(Hi, Lo));
868 Abbrev->AddAttribute(Attribute, Form);
871 /// AddDIEntry - Add a DIE attribute data and value.
873 void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
874 Values.push_back(new DIEntry(Entry));
875 Abbrev->AddAttribute(Attribute, Form);
878 /// AddBlock - Add block data.
880 void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
881 assert(Block->Size && "Block size has not been computed");
882 Values.push_back(Block);
883 if (!Form) Form = Block->BestForm();
884 Abbrev->AddAttribute(Attribute, Form);
887 /// Complete - Indicate that all attributes have been added and ready to get an
889 void DIE::Complete(DwarfWriter &DW) {
890 AbbrevID = DW.NewAbbreviation(Abbrev);
895 /// AddChild - Add a child to the DIE.
897 void DIE::AddChild(DIE *Child) {
898 assert(Abbrev && "Adding children without an abbreviation");
899 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
900 Children.push_back(Child);
903 //===----------------------------------------------------------------------===//
907 //===----------------------------------------------------------------------===//
909 /// PrintHex - Print a value as a hexidecimal value.
911 void DwarfWriter::PrintHex(int Value) const {
912 O << "0x" << std::hex << Value << std::dec;
915 /// EOL - Print a newline character to asm stream. If a comment is present
916 /// then it will be printed first. Comments should not contain '\n'.
917 void DwarfWriter::EOL(const std::string &Comment) const {
918 if (DwarfVerbose && !Comment.empty()) {
920 << Asm->CommentString
927 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
928 /// unsigned leb128 value.
929 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
934 O << Asm->Data8bitsDirective;
939 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
940 /// signed leb128 value.
941 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
946 O << Asm->Data8bitsDirective;
951 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
952 /// representing an unsigned leb128 value.
953 void DwarfWriter::PrintULEB128(unsigned Value) const {
955 unsigned Byte = Value & 0x7f;
957 if (Value) Byte |= 0x80;
959 if (Value) O << ", ";
963 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
965 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
969 Size += sizeof(int8_t);
974 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
975 /// representing a signed leb128 value.
976 void DwarfWriter::PrintSLEB128(int Value) const {
977 int Sign = Value >> (8 * sizeof(Value) - 1);
981 unsigned Byte = Value & 0x7f;
983 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
984 if (IsMore) Byte |= 0x80;
986 if (IsMore) O << ", ";
990 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
992 unsigned DwarfWriter::SizeSLEB128(int Value) {
994 int Sign = Value >> (8 * sizeof(Value) - 1);
998 unsigned Byte = Value & 0x7f;
1000 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
1001 Size += sizeof(int8_t);
1006 /// EmitInt8 - Emit a byte directive and value.
1008 void DwarfWriter::EmitInt8(int Value) const {
1009 O << Asm->Data8bitsDirective;
1010 PrintHex(Value & 0xFF);
1013 /// EmitInt16 - Emit a short directive and value.
1015 void DwarfWriter::EmitInt16(int Value) const {
1016 O << Asm->Data16bitsDirective;
1017 PrintHex(Value & 0xFFFF);
1020 /// EmitInt32 - Emit a long directive and value.
1022 void DwarfWriter::EmitInt32(int Value) const {
1023 O << Asm->Data32bitsDirective;
1027 /// EmitInt64 - Emit a long long directive and value.
1029 void DwarfWriter::EmitInt64(uint64_t Value) const {
1030 if (Asm->Data64bitsDirective) {
1031 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
1033 const TargetData &TD = Asm->TM.getTargetData();
1035 if (TD.isBigEndian()) {
1036 EmitInt32(unsigned(Value >> 32)); O << "\n";
1037 EmitInt32(unsigned(Value));
1039 EmitInt32(unsigned(Value)); O << "\n";
1040 EmitInt32(unsigned(Value >> 32));
1045 /// EmitString - Emit a string with quotes and a null terminator.
1046 /// Special characters are emitted properly. (Eg. '\t')
1047 void DwarfWriter::EmitString(const std::string &String) const {
1048 O << Asm->AsciiDirective
1050 for (unsigned i = 0, N = String.size(); i < N; ++i) {
1051 unsigned char C = String[i];
1053 if (!isascii(C) || iscntrl(C)) {
1055 case '\b': O << "\\b"; break;
1056 case '\f': O << "\\f"; break;
1057 case '\n': O << "\\n"; break;
1058 case '\r': O << "\\r"; break;
1059 case '\t': O << "\\t"; break;
1062 O << char('0' + (C >> 6));
1063 O << char('0' + (C >> 3));
1064 O << char('0' + (C >> 0));
1067 } else if (C == '\"') {
1069 } else if (C == '\'') {
1078 /// PrintLabelName - Print label name in form used by Dwarf writer.
1080 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
1081 O << Asm->PrivateGlobalPrefix
1084 if (Number) O << Number;
1087 /// EmitLabel - Emit location label for internal use by Dwarf.
1089 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
1090 PrintLabelName(Tag, Number);
1094 /// EmitReference - Emit a reference to a label.
1096 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
1097 if (AddressSize == 4)
1098 O << Asm->Data32bitsDirective;
1100 O << Asm->Data64bitsDirective;
1102 PrintLabelName(Tag, Number);
1104 void DwarfWriter::EmitReference(const std::string &Name) const {
1105 if (AddressSize == 4)
1106 O << Asm->Data32bitsDirective;
1108 O << Asm->Data64bitsDirective;
1113 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
1114 /// assemblers do not accept absolute expressions with data directives, so there
1115 /// is an option (needsSet) to use an intermediary 'set' expression.
1116 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
1117 const char *TagLo, unsigned NumberLo) const {
1119 static unsigned SetCounter = 0;
1122 PrintLabelName("set", SetCounter);
1124 PrintLabelName(TagHi, NumberHi);
1126 PrintLabelName(TagLo, NumberLo);
1129 if (AddressSize == sizeof(int32_t))
1130 O << Asm->Data32bitsDirective;
1132 O << Asm->Data64bitsDirective;
1134 PrintLabelName("set", SetCounter);
1138 if (AddressSize == sizeof(int32_t))
1139 O << Asm->Data32bitsDirective;
1141 O << Asm->Data64bitsDirective;
1143 PrintLabelName(TagHi, NumberHi);
1145 PrintLabelName(TagLo, NumberLo);
1149 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
1151 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
1152 return Abbreviations.insert(*Abbrev);
1155 /// NewString - Add a string to the constant pool and returns a label.
1157 DWLabel DwarfWriter::NewString(const std::string &String) {
1158 unsigned StringID = StringPool.insert(String);
1159 return DWLabel("string", StringID);
1162 /// NewBasicType - Creates a new basic type if necessary, then adds to the
1164 /// FIXME - Should never be needed.
1165 DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) {
1166 DIE *&Slot = TypeToDieMap[Ty];
1167 if (Slot) return Slot;
1171 unsigned Encoding = 0;
1173 switch (Ty->getTypeID()) {
1174 case Type::UByteTyID:
1175 Name = "unsigned char";
1177 Encoding = DW_ATE_unsigned_char;
1179 case Type::SByteTyID:
1182 Encoding = DW_ATE_signed_char;
1184 case Type::UShortTyID:
1185 Name = "unsigned short";
1187 Encoding = DW_ATE_unsigned;
1189 case Type::ShortTyID:
1192 Encoding = DW_ATE_signed;
1194 case Type::UIntTyID:
1195 Name = "unsigned int";
1197 Encoding = DW_ATE_unsigned;
1202 Encoding = DW_ATE_signed;
1204 case Type::ULongTyID:
1205 Name = "unsigned long long";
1207 Encoding = DW_ATE_unsigned;
1209 case Type::LongTyID:
1212 Encoding = DW_ATE_signed;
1214 case Type::FloatTyID:
1217 Encoding = DW_ATE_float;
1219 case Type::DoubleTyID:
1222 Encoding = DW_ATE_float;
1225 // FIXME - handle more complex types.
1228 Encoding = DW_ATE_address;
1232 // construct the type DIE.
1233 Slot = new DIE(DW_TAG_base_type);
1234 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
1235 Slot->AddUInt (DW_AT_byte_size, 0, Size);
1236 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1239 Context->AddChild(Slot);
1244 /// NewType - Create a new type DIE.
1246 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc) {
1247 if (!TyDesc) return NewBasicType(Context, Type::IntTy);
1249 // FIXME - Should handle other contexts that compile units.
1251 // Check for pre-existence.
1252 DIE *&Slot = DescToDieMap[TyDesc];
1253 if (Slot) return Slot;
1255 // Get core information.
1256 const std::string &Name = TyDesc->getName();
1257 uint64_t Size = TyDesc->getSize() >> 3;
1261 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1262 // Fundamental types like int, float, bool
1263 Slot = Ty = new DIE(DW_TAG_base_type);
1264 unsigned Encoding = BasicTy->getEncoding();
1265 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1266 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1267 // Create specific DIE.
1268 Slot = Ty = new DIE(DerivedTy->getTag());
1270 // Map to main type, void will not have a type.
1271 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1272 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
1274 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1275 // Create specific DIE.
1276 Slot = Ty = new DIE(CompTy->getTag());
1277 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1279 switch (CompTy->getTag()) {
1280 case DW_TAG_array_type: {
1281 // Add element type.
1282 if (TypeDesc *FromTy = CompTy->getFromType()) {
1283 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
1285 // Don't emit size attribute.
1288 // Construct an anonymous type for index type.
1289 DIE *IndexTy = new DIE(DW_TAG_base_type);
1290 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1291 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1293 Context->AddChild(IndexTy);
1295 // Add subranges to array type.
1296 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1297 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1298 int64_t Lo = SRD->getLo();
1299 int64_t Hi = SRD->getHi();
1300 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1302 // If a range is available.
1304 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1305 // Only add low if non-zero.
1306 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1307 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
1309 Ty->AddChild(Subrange);
1314 case DW_TAG_structure_type:
1315 case DW_TAG_union_type: {
1316 // FIXME - this is just the basics.
1317 // Add elements to structure type.
1318 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1319 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
1321 // Extract the basic information.
1322 const std::string &Name = MemberDesc->getName();
1323 unsigned Line = MemberDesc->getLine();
1324 TypeDesc *MemTy = MemberDesc->getFromType();
1325 uint64_t Size = MemberDesc->getSize();
1326 uint64_t Align = MemberDesc->getAlign();
1327 uint64_t Offset = MemberDesc->getOffset();
1329 // Construct member die.
1330 DIE *Member = new DIE(DW_TAG_member);
1333 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
1334 if (CompileUnitDesc *File = MemberDesc->getFile()) {
1335 CompileUnit *FileUnit = FindCompileUnit(File);
1336 unsigned FileID = FileUnit->getID();
1337 int Line = MemberDesc->getLine();
1338 Member->AddUInt(DW_AT_decl_file, 0, FileID);
1339 Member->AddUInt(DW_AT_decl_line, 0, Line);
1342 // Most of the time the field info is the same as the members.
1343 uint64_t FieldSize = Size;
1344 uint64_t FieldAlign = Align;
1345 uint64_t FieldOffset = Offset;
1347 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1348 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1349 NewType(Context, FromTy));
1350 FieldSize = FromTy->getSize();
1351 FieldAlign = FromTy->getSize();
1354 // Unless we have a bit field.
1355 if (FieldSize != Size) {
1356 // Construct the alignment mask.
1357 uint64_t AlignMask = ~(FieldAlign - 1);
1358 // Determine the high bit + 1 of the declared size.
1359 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1360 // Work backwards to determine the base offset of the field.
1361 FieldOffset = HiMark - FieldSize;
1362 // Now normalize offset to the field.
1363 Offset -= FieldOffset;
1365 // Maybe we need to work from the other.
1366 const TargetData &TD = Asm->TM.getTargetData();
1367 if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
1369 Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
1370 Member->AddUInt(DW_AT_bit_size, 0, Size);
1371 Member->AddUInt(DW_AT_bit_offset, 0, Offset);
1374 // Add computation for offset.
1375 DIEBlock *Block = new DIEBlock();
1376 Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
1377 Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
1378 Block->ComputeSize(*this);
1379 Member->AddBlock(DW_AT_data_member_location, 0, Block);
1381 Ty->AddChild(Member);
1385 case DW_TAG_enumeration_type: {
1386 // Add enumerators to enumeration type.
1387 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1388 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1389 const std::string &Name = ED->getName();
1390 int64_t Value = ED->getValue();
1391 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1392 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1393 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1394 Ty->AddChild(Enumerator);
1403 assert(Ty && "Type not supported yet");
1405 // Add size if non-zero (derived types don't have a size.)
1406 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1407 // Add name if not anonymous or intermediate type.
1408 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1409 // Add source line info if present.
1410 if (CompileUnitDesc *File = TyDesc->getFile()) {
1411 CompileUnit *FileUnit = FindCompileUnit(File);
1412 unsigned FileID = FileUnit->getID();
1413 int Line = TyDesc->getLine();
1414 Ty->AddUInt(DW_AT_decl_file, 0, FileID);
1415 Ty->AddUInt(DW_AT_decl_line, 0, Line);
1418 // Add to context owner.
1419 Context->AddChild(Ty);
1424 /// NewCompileUnit - Create new compile unit and it's die.
1426 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1428 // Construct debug information entry.
1429 DIE *Die = new DIE(DW_TAG_compile_unit);
1430 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1431 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1432 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1433 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1434 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1435 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1436 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1438 // Add die to descriptor map.
1439 DescToDieMap[UnitDesc] = Die;
1441 // Construct compile unit.
1442 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1444 // Add Unit to compile unit map.
1445 DescToUnitMap[UnitDesc] = Unit;
1450 /// FindCompileUnit - Get the compile unit for the given descriptor.
1452 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1453 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1454 assert(Unit && "Missing compile unit.");
1458 /// NewGlobalVariable - Add a new global variable DIE.
1460 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1461 // Check for pre-existence.
1462 DIE *&Slot = DescToDieMap[GVD];
1463 if (Slot) return Slot;
1465 // Get the compile unit context.
1466 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1467 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1468 // Get the global variable itself.
1469 GlobalVariable *GV = GVD->getGlobalVariable();
1470 // Generate the mangled name.
1471 std::string MangledName = Asm->Mang->getValueName(GV);
1473 // Gather the details (simplify add attribute code.)
1474 const std::string &Name = GVD->getName();
1475 unsigned FileID = Unit->getID();
1476 unsigned Line = GVD->getLine();
1478 // Get the global's type.
1479 DIE *Type = NewType(Unit->getDie(), GVD->getTypeDesc());
1481 // Create the globale variable DIE.
1482 DIE *VariableDie = new DIE(DW_TAG_variable);
1483 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1484 VariableDie->AddUInt (DW_AT_decl_file, 0, FileID);
1485 VariableDie->AddUInt (DW_AT_decl_line, 0, Line);
1486 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1487 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1489 DIEBlock *Block = new DIEBlock();
1490 Block->AddUInt(DW_FORM_data1, DW_OP_addr);
1491 Block->AddObjectLabel(DW_FORM_udata, MangledName);
1492 Block->ComputeSize(*this);
1493 VariableDie->AddBlock(DW_AT_location, 0, Block);
1498 // Add to context owner.
1499 Unit->getDie()->AddChild(VariableDie);
1501 // Expose as global.
1502 // FIXME - need to check external flag.
1503 Unit->AddGlobal(Name, VariableDie);
1508 /// NewSubprogram - Add a new subprogram DIE.
1510 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1511 // Check for pre-existence.
1512 DIE *&Slot = DescToDieMap[SPD];
1513 if (Slot) return Slot;
1515 // Get the compile unit context.
1516 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1517 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1519 // Gather the details (simplify add attribute code.)
1520 const std::string &Name = SPD->getName();
1521 CompileUnitDesc *FileDesc = static_cast<CompileUnitDesc *>(SPD->getFile());
1522 CompileUnit *File = FindCompileUnit(FileDesc);
1523 unsigned FileID = File->getID();
1524 DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy);
1525 unsigned Line = SPD->getLine();
1526 unsigned IsExternal = SPD->isStatic() ? 0 : 1;
1528 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1529 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1530 SubprogramDie->AddUInt (DW_AT_decl_file, 0, FileID);
1531 SubprogramDie->AddUInt (DW_AT_decl_line, 0, Line);
1532 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1533 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, IsExternal);
1536 Slot = SubprogramDie;
1538 // Add to context owner.
1539 Unit->getDie()->AddChild(SubprogramDie);
1541 // Expose as global.
1542 Unit->AddGlobal(Name, SubprogramDie);
1544 return SubprogramDie;
1547 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1548 /// tools to recognize the object file contains Dwarf information.
1550 void DwarfWriter::EmitInitial() const {
1551 // Dwarf sections base addresses.
1552 Asm->SwitchSection(DwarfFrameSection, 0);
1553 EmitLabel("section_frame", 0);
1554 Asm->SwitchSection(DwarfInfoSection, 0);
1555 EmitLabel("section_info", 0);
1556 EmitLabel("info", 0);
1557 Asm->SwitchSection(DwarfAbbrevSection, 0);
1558 EmitLabel("section_abbrev", 0);
1559 EmitLabel("abbrev", 0);
1560 Asm->SwitchSection(DwarfARangesSection, 0);
1561 EmitLabel("section_aranges", 0);
1562 Asm->SwitchSection(DwarfMacInfoSection, 0);
1563 EmitLabel("section_macinfo", 0);
1564 Asm->SwitchSection(DwarfLineSection, 0);
1565 EmitLabel("section_line", 0);
1566 EmitLabel("line", 0);
1567 Asm->SwitchSection(DwarfLocSection, 0);
1568 EmitLabel("section_loc", 0);
1569 Asm->SwitchSection(DwarfPubNamesSection, 0);
1570 EmitLabel("section_pubnames", 0);
1571 Asm->SwitchSection(DwarfStrSection, 0);
1572 EmitLabel("section_str", 0);
1573 Asm->SwitchSection(DwarfRangesSection, 0);
1574 EmitLabel("section_ranges", 0);
1576 Asm->SwitchSection(TextSection, 0);
1577 EmitLabel("text_begin", 0);
1578 Asm->SwitchSection(DataSection, 0);
1579 EmitLabel("data_begin", 0);
1582 /// EmitDIE - Recusively Emits a debug information entry.
1584 void DwarfWriter::EmitDIE(DIE *Die) const {
1585 // Get the abbreviation for this DIE.
1586 unsigned AbbrevID = Die->getAbbrevID();
1587 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1591 // Emit the code (index) for the abbreviation.
1592 EmitULEB128Bytes(AbbrevID);
1593 EOL(std::string("Abbrev [" +
1595 "] 0x" + utohexstr(Die->getOffset()) +
1596 ":0x" + utohexstr(Die->getSize()) + " " +
1597 TagString(Abbrev.getTag())));
1599 const std::vector<DIEValue *> &Values = Die->getValues();
1600 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1602 // Emit the DIE attribute values.
1603 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1604 unsigned Attr = AbbrevData[i].getAttribute();
1605 unsigned Form = AbbrevData[i].getForm();
1606 assert(Form && "Too many attributes for DIE (check abbreviation)");
1609 case DW_AT_sibling: {
1610 EmitInt32(Die->SiblingOffset());
1614 // Emit an attribute using the defined form.
1615 Values[i]->EmitValue(*this, Form);
1620 EOL(AttributeString(Attr));
1623 // Emit the DIE children if any.
1624 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1625 const std::vector<DIE *> &Children = Die->getChildren();
1627 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1628 // FIXME - handle sibling offsets.
1629 // FIXME - handle all DIE types.
1630 EmitDIE(Children[j]);
1633 EmitInt8(0); EOL("End Of Children Mark");
1637 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1639 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1640 // Record the abbreviation.
1641 Die->Complete(*this);
1643 // Get the abbreviation for this DIE.
1644 unsigned AbbrevID = Die->getAbbrevID();
1645 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1648 Die->setOffset(Offset);
1650 // Start the size with the size of abbreviation code.
1651 Offset += SizeULEB128(AbbrevID);
1653 const std::vector<DIEValue *> &Values = Die->getValues();
1654 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1656 // Emit the DIE attribute values.
1657 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1658 // Size attribute value.
1659 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1662 // Emit the DIE children if any.
1663 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1664 const std::vector<DIE *> &Children = Die->getChildren();
1666 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1667 // FIXME - handle sibling offsets.
1668 // FIXME - handle all DIE types.
1669 Offset = SizeAndOffsetDie(Children[j], Offset);
1672 // End of children marker.
1673 Offset += sizeof(int8_t);
1676 Die->setSize(Offset - Die->getOffset());
1680 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1682 void DwarfWriter::SizeAndOffsets() {
1684 // Process each compile unit.
1685 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1686 CompileUnit *Unit = CompileUnits[i];
1687 if (Unit->hasContent()) {
1688 // Compute size of compile unit header
1689 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1690 sizeof(int16_t) + // DWARF version number
1691 sizeof(int32_t) + // Offset Into Abbrev. Section
1692 sizeof(int8_t); // Pointer Size (in bytes)
1694 SizeAndOffsetDie(Unit->getDie(), Offset);
1699 /// EmitDebugInfo - Emit the debug info section.
1701 void DwarfWriter::EmitDebugInfo() const {
1702 // Start debug info section.
1703 Asm->SwitchSection(DwarfInfoSection, 0);
1705 // Process each compile unit.
1706 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1707 CompileUnit *Unit = CompileUnits[i];
1709 if (Unit->hasContent()) {
1710 DIE *Die = Unit->getDie();
1711 // Emit the compile units header.
1712 EmitLabel("info_begin", Unit->getID());
1713 // Emit size of content not including length itself
1714 unsigned ContentSize = Die->getSize() +
1715 sizeof(int16_t) + // DWARF version number
1716 sizeof(int32_t) + // Offset Into Abbrev. Section
1717 sizeof(int8_t); // Pointer Size (in bytes)
1719 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1720 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1721 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1722 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1725 EmitLabel("info_end", Unit->getID());
1732 /// EmitAbbreviations - Emit the abbreviation section.
1734 void DwarfWriter::EmitAbbreviations() const {
1735 // Check to see if it is worth the effort.
1736 if (!Abbreviations.empty()) {
1737 // Start the debug abbrev section.
1738 Asm->SwitchSection(DwarfAbbrevSection, 0);
1740 EmitLabel("abbrev_begin", 0);
1742 // For each abbrevation.
1743 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1744 AbbrevID <= NAID; ++AbbrevID) {
1745 // Get abbreviation data
1746 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1748 // Emit the abbrevations code (base 1 index.)
1749 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1751 // Emit the abbreviations data.
1757 EmitLabel("abbrev_end", 0);
1763 /// EmitDebugLines - Emit source line information.
1765 void DwarfWriter::EmitDebugLines() const {
1766 // Minimum line delta, thus ranging from -10..(255-10).
1767 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1768 // Maximum line delta, thus ranging from -10..(255-10).
1769 const int MaxLineDelta = 255 + MinLineDelta;
1771 // Start the dwarf line section.
1772 Asm->SwitchSection(DwarfLineSection, 0);
1774 // Construct the section header.
1776 EmitDifference("line_end", 0, "line_begin", 0);
1777 EOL("Length of Source Line Info");
1778 EmitLabel("line_begin", 0);
1780 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1782 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1783 EOL("Prolog Length");
1784 EmitLabel("line_prolog_begin", 0);
1786 EmitInt8(1); EOL("Minimum Instruction Length");
1788 EmitInt8(1); EOL("Default is_stmt_start flag");
1790 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1792 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1794 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1796 // Line number standard opcode encodings argument count
1797 EmitInt8(0); EOL("DW_LNS_copy arg count");
1798 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1799 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1800 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1801 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1802 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1803 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1804 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1805 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1807 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1808 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1810 // Emit directories.
1811 for (unsigned DirectoryID = 1, NDID = Directories.size();
1812 DirectoryID <= NDID; ++DirectoryID) {
1813 EmitString(Directories[DirectoryID]); EOL("Directory");
1815 EmitInt8(0); EOL("End of directories");
1818 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1819 SourceID <= NSID; ++SourceID) {
1820 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1821 EmitString(SourceFile.getName()); EOL("Source");
1822 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1823 EmitULEB128Bytes(0); EOL("Mod date");
1824 EmitULEB128Bytes(0); EOL("File size");
1826 EmitInt8(0); EOL("End of files");
1828 EmitLabel("line_prolog_end", 0);
1830 // Emit line information
1831 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1833 // Dwarf assumes we start with first line of first source file.
1834 unsigned Source = 1;
1837 // Construct rows of the address, source, line, column matrix.
1838 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1839 SourceLineInfo *LineInfo = LineInfos[i];
1842 unsigned SourceID = LineInfo->getSourceID();
1843 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1844 unsigned DirectoryID = SourceFile.getDirectoryID();
1846 << Asm->CommentString << " "
1847 << Directories[DirectoryID]
1848 << SourceFile.getName() << ":"
1849 << LineInfo->getLine() << "\n";
1852 // Define the line address.
1853 EmitInt8(0); EOL("Extended Op");
1854 EmitInt8(4 + 1); EOL("Op size");
1855 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1856 EmitReference("loc", i + 1); EOL("Location label");
1858 // If change of source, then switch to the new source.
1859 if (Source != LineInfo->getSourceID()) {
1860 Source = LineInfo->getSourceID();
1861 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1862 EmitULEB128Bytes(Source); EOL("New Source");
1865 // If change of line.
1866 if (Line != LineInfo->getLine()) {
1867 // Determine offset.
1868 int Offset = LineInfo->getLine() - Line;
1869 int Delta = Offset - MinLineDelta;
1872 Line = LineInfo->getLine();
1874 // If delta is small enough and in range...
1875 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1876 // ... then use fast opcode.
1877 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1879 // ... otherwise use long hand.
1880 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1881 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1882 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1885 // Copy the previous row (different address or source)
1886 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1890 // Define last address.
1891 EmitInt8(0); EOL("Extended Op");
1892 EmitInt8(4 + 1); EOL("Op size");
1893 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1894 EmitReference("text_end", 0); EOL("Location label");
1896 // Mark end of matrix.
1897 EmitInt8(0); EOL("DW_LNE_end_sequence");
1898 EmitULEB128Bytes(1); O << "\n";
1899 EmitInt8(1); O << "\n";
1901 EmitLabel("line_end", 0);
1906 /// EmitDebugFrame - Emit visible names into a debug frame section.
1908 void DwarfWriter::EmitDebugFrame() {
1909 // FIXME - Should be per frame
1912 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1914 void DwarfWriter::EmitDebugPubNames() {
1915 // Start the dwarf pubnames section.
1916 Asm->SwitchSection(DwarfPubNamesSection, 0);
1918 // Process each compile unit.
1919 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1920 CompileUnit *Unit = CompileUnits[i];
1922 if (Unit->hasContent()) {
1923 EmitDifference("pubnames_end", Unit->getID(),
1924 "pubnames_begin", Unit->getID());
1925 EOL("Length of Public Names Info");
1927 EmitLabel("pubnames_begin", Unit->getID());
1929 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1931 EmitReference("info_begin", Unit->getID());
1932 EOL("Offset of Compilation Unit Info");
1934 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
1935 EOL("Compilation Unit Length");
1937 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
1939 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
1942 const std::string &Name = GI->first;
1943 DIE * Entity = GI->second;
1945 EmitInt32(Entity->getOffset()); EOL("DIE offset");
1946 EmitString(Name); EOL("External Name");
1949 EmitInt32(0); EOL("End Mark");
1950 EmitLabel("pubnames_end", Unit->getID());
1957 /// EmitDebugStr - Emit visible names into a debug str section.
1959 void DwarfWriter::EmitDebugStr() {
1960 // Check to see if it is worth the effort.
1961 if (!StringPool.empty()) {
1962 // Start the dwarf str section.
1963 Asm->SwitchSection(DwarfStrSection, 0);
1965 // For each of strings in teh string pool.
1966 for (unsigned StringID = 1, N = StringPool.size();
1967 StringID <= N; ++StringID) {
1968 // Emit a label for reference from debug information entries.
1969 EmitLabel("string", StringID);
1970 // Emit the string itself.
1971 const std::string &String = StringPool[StringID];
1972 EmitString(String); O << "\n";
1979 /// EmitDebugLoc - Emit visible names into a debug loc section.
1981 void DwarfWriter::EmitDebugLoc() {
1982 // Start the dwarf loc section.
1983 Asm->SwitchSection(DwarfLocSection, 0);
1988 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1990 void DwarfWriter::EmitDebugARanges() {
1991 // Start the dwarf aranges section.
1992 Asm->SwitchSection(DwarfARangesSection, 0);
1996 // Process each compile unit.
1997 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1998 CompileUnit *Unit = CompileUnits[i];
2000 if (Unit->hasContent()) {
2001 // Don't include size of length
2002 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
2004 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
2006 EmitReference("info_begin", Unit->getID());
2007 EOL("Offset of Compilation Unit Info");
2009 EmitInt8(AddressSize); EOL("Size of Address");
2011 EmitInt8(0); EOL("Size of Segment Descriptor");
2013 EmitInt16(0); EOL("Pad (1)");
2014 EmitInt16(0); EOL("Pad (2)");
2017 EmitReference("text_begin", 0); EOL("Address");
2018 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
2020 EmitInt32(0); EOL("EOM (1)");
2021 EmitInt32(0); EOL("EOM (2)");
2029 /// EmitDebugRanges - Emit visible names into a debug ranges section.
2031 void DwarfWriter::EmitDebugRanges() {
2032 // Start the dwarf ranges section.
2033 Asm->SwitchSection(DwarfRangesSection, 0);
2038 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
2040 void DwarfWriter::EmitDebugMacInfo() {
2041 // Start the dwarf macinfo section.
2042 Asm->SwitchSection(DwarfMacInfoSection, 0);
2047 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
2049 void DwarfWriter::ConstructCompileUnitDIEs() {
2050 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
2052 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
2053 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
2054 CompileUnits.push_back(Unit);
2058 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
2060 void DwarfWriter::ConstructGlobalDIEs(Module &M) {
2061 std::vector<GlobalVariableDesc *> GlobalVariables =
2062 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
2064 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
2065 GlobalVariableDesc *GVD = GlobalVariables[i];
2066 NewGlobalVariable(GVD);
2070 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
2072 void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
2073 std::vector<SubprogramDesc *> Subprograms =
2074 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
2076 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
2077 SubprogramDesc *SPD = Subprograms[i];
2082 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
2084 bool DwarfWriter::ShouldEmitDwarf() {
2085 // Check if debug info is present.
2086 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
2088 // Make sure initial declarations are made.
2098 //===----------------------------------------------------------------------===//
2099 // Main entry points.
2102 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
2113 , AddressSize(sizeof(int32_t))
2118 , DwarfAbbrevSection(".debug_abbrev")
2119 , DwarfInfoSection(".debug_info")
2120 , DwarfLineSection(".debug_line")
2121 , DwarfFrameSection(".debug_frame")
2122 , DwarfPubNamesSection(".debug_pubnames")
2123 , DwarfPubTypesSection(".debug_pubtypes")
2124 , DwarfStrSection(".debug_str")
2125 , DwarfLocSection(".debug_loc")
2126 , DwarfARangesSection(".debug_aranges")
2127 , DwarfRangesSection(".debug_ranges")
2128 , DwarfMacInfoSection(".debug_macinfo")
2129 , TextSection(".text")
2130 , DataSection(".data")
2132 DwarfWriter::~DwarfWriter() {
2133 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
2134 delete CompileUnits[i];
2138 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
2140 void DwarfWriter::BeginModule(Module &M) {
2141 if (!ShouldEmitDwarf()) return;
2142 EOL("Dwarf Begin Module");
2145 /// EndModule - Emit all Dwarf sections that should come after the content.
2147 void DwarfWriter::EndModule(Module &M) {
2148 if (!ShouldEmitDwarf()) return;
2149 EOL("Dwarf End Module");
2151 // Standard sections final addresses.
2152 Asm->SwitchSection(TextSection, 0);
2153 EmitLabel("text_end", 0);
2154 Asm->SwitchSection(DataSection, 0);
2155 EmitLabel("data_end", 0);
2157 // Create all the compile unit DIEs.
2158 ConstructCompileUnitDIEs();
2160 // Create DIEs for each of the externally visible global variables.
2161 ConstructGlobalDIEs(M);
2163 // Create DIEs for each of the externally visible subprograms.
2164 ConstructSubprogramDIEs(M);
2166 // Compute DIE offsets and sizes.
2169 // Emit all the DIEs into a debug info section
2172 // Corresponding abbreviations into a abbrev section.
2173 EmitAbbreviations();
2175 // Emit source line correspondence into a debug line section.
2178 // Emit info into a debug frame section.
2179 // EmitDebugFrame();
2181 // Emit info into a debug pubnames section.
2182 EmitDebugPubNames();
2184 // Emit info into a debug str section.
2187 // Emit info into a debug loc section.
2190 // Emit info into a debug aranges section.
2193 // Emit info into a debug ranges section.
2196 // Emit info into a debug macinfo section.
2200 /// BeginFunction - Gather pre-function debug information.
2202 void DwarfWriter::BeginFunction(MachineFunction &MF) {
2203 if (!ShouldEmitDwarf()) return;
2204 EOL("Dwarf Begin Function");
2207 /// EndFunction - Gather and emit post-function debug information.
2209 void DwarfWriter::EndFunction(MachineFunction &MF) {
2210 if (!ShouldEmitDwarf()) return;
2211 EOL("Dwarf End Function");