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 //===----------------------------------------------------------------------===//
46 CompileUnitDesc *Desc; // Compile unit debug descriptor.
47 unsigned ID; // File ID for source.
48 DIE *Die; // Compile unit die.
49 std::map<std::string, DIE *> Globals; // A map of globally visible named
50 // entities for this unit.
53 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
63 CompileUnitDesc *getDesc() const { return Desc; }
64 unsigned getID() const { return ID; }
65 DIE* getDie() const { return Die; }
66 std::map<std::string, DIE *> &getGlobals() { return Globals; }
68 /// hasContent - Return true if this compile unit has something to write out.
70 bool hasContent() const;
72 /// AddGlobal - Add a new global entity to the compile unit.
74 void AddGlobal(const std::string &Name, DIE *Die);
78 //===----------------------------------------------------------------------===//
79 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
80 // Dwarf abbreviation.
83 unsigned Attribute; // Dwarf attribute code.
84 unsigned Form; // Dwarf form code.
87 DIEAbbrevData(unsigned A, unsigned F)
93 unsigned getAttribute() const { return Attribute; }
94 unsigned getForm() const { return Form; }
96 /// operator== - Used by DIEAbbrev to locate entry.
98 bool operator==(const DIEAbbrevData &DAD) const {
99 return Attribute == DAD.Attribute && Form == DAD.Form;
102 /// operator!= - Used by DIEAbbrev to locate entry.
104 bool operator!=(const DIEAbbrevData &DAD) const {
105 return Attribute != DAD.Attribute || Form != DAD.Form;
108 /// operator< - Used by DIEAbbrev to locate entry.
110 bool operator<(const DIEAbbrevData &DAD) const {
111 return Attribute < DAD.Attribute ||
112 (Attribute == DAD.Attribute && Form < DAD.Form);
116 //===----------------------------------------------------------------------===//
117 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
118 // information object.
121 unsigned Tag; // Dwarf tag code.
122 unsigned ChildrenFlag; // Dwarf children flag.
123 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
127 DIEAbbrev(unsigned T, unsigned C)
135 unsigned getTag() const { return Tag; }
136 unsigned getChildrenFlag() const { return ChildrenFlag; }
137 const std::vector<DIEAbbrevData> &getData() const { return Data; }
138 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
140 /// operator== - Used by UniqueVector to locate entry.
142 bool operator==(const DIEAbbrev &DA) const;
144 /// operator< - Used by UniqueVector to locate entry.
146 bool operator<(const DIEAbbrev &DA) const;
148 /// AddAttribute - Adds another set of attribute information to the
150 void AddAttribute(unsigned Attribute, unsigned Form) {
151 Data.push_back(DIEAbbrevData(Attribute, Form));
154 /// Emit - Print the abbreviation using the specified Dwarf writer.
156 void Emit(const DwarfWriter &DW) const;
159 void print(std::ostream &O);
164 //===----------------------------------------------------------------------===//
165 // DIEValue - A debug information entry value.
178 unsigned Type; // Type of the value
180 DIEValue(unsigned T) : Type(T) {}
181 virtual ~DIEValue() {}
183 // Implement isa/cast/dyncast.
184 static bool classof(const DIEValue *) { return true; }
186 /// EmitValue - Emit value via the Dwarf writer.
188 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
190 /// SizeOf - Return the size of a value in bytes.
192 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
195 //===----------------------------------------------------------------------===//
196 // DWInteger - An integer value DIE.
198 class DIEInteger : public DIEValue {
203 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
205 // Implement isa/cast/dyncast.
206 static bool classof(const DIEInteger *) { return true; }
207 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
209 /// EmitValue - Emit integer of appropriate size.
211 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
213 /// SizeOf - Determine size of integer value in bytes.
215 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
218 //===----------------------------------------------------------------------===//
219 // DIEString - A string value DIE.
221 struct DIEString : public DIEValue {
222 const std::string String;
224 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
226 // Implement isa/cast/dyncast.
227 static bool classof(const DIEString *) { return true; }
228 static bool classof(const DIEValue *S) { return S->Type == isString; }
230 /// EmitValue - Emit string value.
232 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
234 /// SizeOf - Determine size of string value in bytes.
236 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
239 //===----------------------------------------------------------------------===//
240 // DIEDwarfLabel - A Dwarf internal label expression DIE.
242 struct DIEDwarfLabel : public DIEValue {
245 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
247 // Implement isa/cast/dyncast.
248 static bool classof(const DIEDwarfLabel *) { return true; }
249 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
251 /// EmitValue - Emit label value.
253 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
255 /// SizeOf - Determine size of label value in bytes.
257 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
261 //===----------------------------------------------------------------------===//
262 // DIEObjectLabel - A label to an object in code or data.
264 struct DIEObjectLabel : public DIEValue {
265 const std::string Label;
267 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
269 // Implement isa/cast/dyncast.
270 static bool classof(const DIEObjectLabel *) { return true; }
271 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
273 /// EmitValue - Emit label value.
275 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
277 /// SizeOf - Determine size of label value in bytes.
279 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
282 //===----------------------------------------------------------------------===//
283 // DIEDelta - A simple label difference DIE.
285 struct DIEDelta : public DIEValue {
286 const DWLabel LabelHi;
287 const DWLabel LabelLo;
289 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
290 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
292 // Implement isa/cast/dyncast.
293 static bool classof(const DIEDelta *) { return true; }
294 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
296 /// EmitValue - Emit delta value.
298 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
300 /// SizeOf - Determine size of delta value in bytes.
302 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
305 //===----------------------------------------------------------------------===//
306 // DIEntry - A pointer to a debug information entry.
308 struct DIEntry : public DIEValue {
311 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
313 // Implement isa/cast/dyncast.
314 static bool classof(const DIEntry *) { return true; }
315 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
317 /// EmitValue - Emit delta value.
319 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
321 /// SizeOf - Determine size of delta value in bytes.
323 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
326 //===----------------------------------------------------------------------===//
327 // DIE - A structured debug information entry. Has an abbreviation which
328 // describes it's organization.
331 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
332 unsigned AbbrevID; // Decribing abbreviation ID.
333 unsigned Offset; // Offset in debug info section.
334 unsigned Size; // Size of instance + children.
335 std::vector<DIE *> Children; // Children DIEs.
336 std::vector<DIEValue *> Values; // Attributes values.
343 unsigned getAbbrevID() const { return AbbrevID; }
344 unsigned getOffset() const { return Offset; }
345 unsigned getSize() const { return Size; }
346 const std::vector<DIE *> &getChildren() const { return Children; }
347 const std::vector<DIEValue *> &getValues() const { return Values; }
348 void setOffset(unsigned O) { Offset = O; }
349 void setSize(unsigned S) { Size = S; }
351 /// SiblingOffset - Return the offset of the debug information entry's
353 unsigned SiblingOffset() const { return Offset + Size; }
355 /// AddUInt - Add an unsigned integer attribute data and value.
357 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
359 /// AddSInt - Add an signed integer attribute data and value.
361 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
363 /// AddString - Add a std::string attribute data and value.
365 void AddString(unsigned Attribute, unsigned Form,
366 const std::string &String);
368 /// AddLabel - Add a Dwarf label attribute data and value.
370 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
372 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
374 void AddObjectLabel(unsigned Attribute, unsigned Form,
375 const std::string &Label);
377 /// AddDelta - Add a label delta attribute data and value.
379 void AddDelta(unsigned Attribute, unsigned Form,
380 const DWLabel &Hi, const DWLabel &Lo);
382 /// AddDIEntry - Add a DIE attribute data and value.
384 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
386 /// Complete - Indicate that all attributes have been added and
387 /// ready to get an abbreviation ID.
389 void Complete(DwarfWriter &DW);
391 /// AddChild - Add a child to the DIE.
392 void AddChild(DIE *Child);
395 } // End of namespace llvm
397 //===----------------------------------------------------------------------===//
399 CompileUnit::~CompileUnit() {
403 /// hasContent - Return true if this compile unit has something to write out.
405 bool CompileUnit::hasContent() const {
406 return !Die->getChildren().empty();
409 /// AddGlobal - Add a new global entity to the compile unit.
411 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
415 //===----------------------------------------------------------------------===//
417 /// operator== - Used by UniqueVector to locate entry.
419 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
420 if (Tag != DA.Tag) return false;
421 if (ChildrenFlag != DA.ChildrenFlag) return false;
422 if (Data.size() != DA.Data.size()) return false;
424 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
425 if (Data[i] != DA.Data[i]) return false;
431 /// operator< - Used by UniqueVector to locate entry.
433 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
434 if (Tag != DA.Tag) return Tag < DA.Tag;
435 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
436 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
438 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
439 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
445 /// Emit - Print the abbreviation using the specified Dwarf writer.
447 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
448 // Emit its Dwarf tag type.
449 DW.EmitULEB128Bytes(Tag);
450 DW.EOL(TagString(Tag));
452 // Emit whether it has children DIEs.
453 DW.EmitULEB128Bytes(ChildrenFlag);
454 DW.EOL(ChildrenString(ChildrenFlag));
456 // For each attribute description.
457 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
458 const DIEAbbrevData &AttrData = Data[i];
460 // Emit attribute type.
461 DW.EmitULEB128Bytes(AttrData.getAttribute());
462 DW.EOL(AttributeString(AttrData.getAttribute()));
465 DW.EmitULEB128Bytes(AttrData.getForm());
466 DW.EOL(FormEncodingString(AttrData.getForm()));
469 // Mark end of abbreviation.
470 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
471 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
475 void DIEAbbrev::print(std::ostream &O) {
476 O << "Abbreviation @"
477 << std::hex << (intptr_t)this << std::dec
481 << ChildrenString(ChildrenFlag)
484 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
486 << AttributeString(Data[i].getAttribute())
488 << FormEncodingString(Data[i].getForm())
492 void DIEAbbrev::dump() { print(std::cerr); }
495 //===----------------------------------------------------------------------===//
497 /// EmitValue - Emit integer of appropriate size.
499 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
501 case DW_FORM_flag: // Fall thru
502 case DW_FORM_data1: DW.EmitInt8(Integer); break;
503 case DW_FORM_data2: DW.EmitInt16(Integer); break;
504 case DW_FORM_data4: DW.EmitInt32(Integer); break;
505 case DW_FORM_data8: DW.EmitInt64(Integer); break;
506 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
507 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
508 default: assert(0 && "DIE Value form not supported yet"); break;
512 /// SizeOf - Determine size of integer value in bytes.
514 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
516 case DW_FORM_flag: // Fall thru
517 case DW_FORM_data1: return sizeof(int8_t);
518 case DW_FORM_data2: return sizeof(int16_t);
519 case DW_FORM_data4: return sizeof(int32_t);
520 case DW_FORM_data8: return sizeof(int64_t);
521 case DW_FORM_udata: return DW.SizeULEB128(Integer);
522 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
523 default: assert(0 && "DIE Value form not supported yet"); break;
528 //===----------------------------------------------------------------------===//
530 /// EmitValue - Emit string value.
532 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
533 DW.EmitString(String);
536 /// SizeOf - Determine size of string value in bytes.
538 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
539 return String.size() + sizeof(char); // sizeof('\0');
542 //===----------------------------------------------------------------------===//
544 /// EmitValue - Emit label value.
546 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
547 DW.EmitReference(Label);
550 /// SizeOf - Determine size of label value in bytes.
552 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
553 return DW.getAddressSize();
556 //===----------------------------------------------------------------------===//
558 /// EmitValue - Emit label value.
560 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
561 DW.EmitInt8(sizeof(int8_t) + DW.getAddressSize());
562 DW.EOL("DW_FORM_block1 length");
564 DW.EmitInt8(DW_OP_addr);
565 DW.EOL("DW_OP_addr");
567 DW.EmitReference(Label);
570 /// SizeOf - Determine size of label value in bytes.
572 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
573 return sizeof(int8_t) + sizeof(int8_t) + DW.getAddressSize();
576 //===----------------------------------------------------------------------===//
578 /// EmitValue - Emit delta value.
580 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
581 DW.EmitDifference(LabelHi, LabelLo);
584 /// SizeOf - Determine size of delta value in bytes.
586 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
587 return DW.getAddressSize();
590 //===----------------------------------------------------------------------===//
591 /// EmitValue - Emit extry offset.
593 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
594 DW.EmitInt32(Entry->getOffset());
597 /// SizeOf - Determine size of label value in bytes.
599 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
600 return sizeof(int32_t);
603 //===----------------------------------------------------------------------===//
605 DIE::DIE(unsigned Tag)
606 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
615 if (Abbrev) delete Abbrev;
617 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
621 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
626 /// AddUInt - Add an unsigned integer attribute data and value.
628 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
630 if ((unsigned char)Integer == Integer) Form = DW_FORM_data1;
631 else if ((unsigned short)Integer == Integer) Form = DW_FORM_data2;
632 else if ((unsigned int)Integer == Integer) Form = DW_FORM_data4;
633 else Form = DW_FORM_data8;
635 Abbrev->AddAttribute(Attribute, Form);
636 Values.push_back(new DIEInteger(Integer));
639 /// AddSInt - Add an signed integer attribute data and value.
641 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
643 if ((char)Integer == Integer) Form = DW_FORM_data1;
644 else if ((short)Integer == Integer) Form = DW_FORM_data2;
645 else if ((int)Integer == Integer) Form = DW_FORM_data4;
646 else Form = DW_FORM_data8;
648 Abbrev->AddAttribute(Attribute, Form);
649 Values.push_back(new DIEInteger(Integer));
652 /// AddString - Add a std::string attribute data and value.
654 void DIE::AddString(unsigned Attribute, unsigned Form,
655 const std::string &String) {
656 Abbrev->AddAttribute(Attribute, Form);
657 Values.push_back(new DIEString(String));
660 /// AddLabel - Add a Dwarf label attribute data and value.
662 void DIE::AddLabel(unsigned Attribute, unsigned Form,
663 const DWLabel &Label) {
664 Abbrev->AddAttribute(Attribute, Form);
665 Values.push_back(new DIEDwarfLabel(Label));
668 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
670 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
671 const std::string &Label) {
672 Abbrev->AddAttribute(Attribute, Form);
673 Values.push_back(new DIEObjectLabel(Label));
676 /// AddDelta - Add a label delta attribute data and value.
678 void DIE::AddDelta(unsigned Attribute, unsigned Form,
679 const DWLabel &Hi, const DWLabel &Lo) {
680 Abbrev->AddAttribute(Attribute, Form);
681 Values.push_back(new DIEDelta(Hi, Lo));
684 /// AddDIEntry - Add a DIE attribute data and value.
686 void DIE::AddDIEntry(unsigned Attribute,
687 unsigned Form, DIE *Entry) {
688 Abbrev->AddAttribute(Attribute, Form);
689 Values.push_back(new DIEntry(Entry));
692 /// Complete - Indicate that all attributes have been added and ready to get an
694 void DIE::Complete(DwarfWriter &DW) {
695 AbbrevID = DW.NewAbbreviation(Abbrev);
700 /// AddChild - Add a child to the DIE.
702 void DIE::AddChild(DIE *Child) {
703 assert(Abbrev && "Adding children without an abbreviation");
704 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
705 Children.push_back(Child);
708 //===----------------------------------------------------------------------===//
712 //===----------------------------------------------------------------------===//
714 /// PrintHex - Print a value as a hexidecimal value.
716 void DwarfWriter::PrintHex(int Value) const {
717 O << "0x" << std::hex << Value << std::dec;
720 /// EOL - Print a newline character to asm stream. If a comment is present
721 /// then it will be printed first. Comments should not contain '\n'.
722 void DwarfWriter::EOL(const std::string &Comment) const {
725 << Asm->CommentString
732 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
733 /// unsigned leb128 value.
734 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
739 O << Asm->Data8bitsDirective;
744 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
745 /// signed leb128 value.
746 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
751 O << Asm->Data8bitsDirective;
756 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
757 /// representing an unsigned leb128 value.
758 void DwarfWriter::PrintULEB128(unsigned Value) const {
760 unsigned Byte = Value & 0x7f;
762 if (Value) Byte |= 0x80;
764 if (Value) O << ", ";
768 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
770 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
774 Size += sizeof(int8_t);
779 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
780 /// representing a signed leb128 value.
781 void DwarfWriter::PrintSLEB128(int Value) const {
782 int Sign = Value >> (8 * sizeof(Value) - 1);
786 unsigned Byte = Value & 0x7f;
788 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
789 if (IsMore) Byte |= 0x80;
791 if (IsMore) O << ", ";
795 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
797 unsigned DwarfWriter::SizeSLEB128(int Value) {
799 int Sign = Value >> (8 * sizeof(Value) - 1);
803 unsigned Byte = Value & 0x7f;
805 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
806 Size += sizeof(int8_t);
811 /// EmitInt8 - Emit a byte directive and value.
813 void DwarfWriter::EmitInt8(int Value) const {
814 O << Asm->Data8bitsDirective;
815 PrintHex(Value & 0xFF);
818 /// EmitInt16 - Emit a short directive and value.
820 void DwarfWriter::EmitInt16(int Value) const {
821 O << Asm->Data16bitsDirective;
822 PrintHex(Value & 0xFFFF);
825 /// EmitInt32 - Emit a long directive and value.
827 void DwarfWriter::EmitInt32(int Value) const {
828 O << Asm->Data32bitsDirective;
832 /// EmitInt64 - Emit a long long directive and value.
834 void DwarfWriter::EmitInt64(uint64_t Value) const {
835 if (Asm->Data64bitsDirective) {
836 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
838 const TargetData &TD = Asm->TM.getTargetData();
840 if (TD.isBigEndian()) {
841 EmitInt32(unsigned(Value >> 32)); O << "\n";
842 EmitInt32(unsigned(Value));
844 EmitInt32(unsigned(Value)); O << "\n";
845 EmitInt32(unsigned(Value >> 32));
850 /// EmitString - Emit a string with quotes and a null terminator.
851 /// Special characters are emitted properly. (Eg. '\t')
852 void DwarfWriter::EmitString(const std::string &String) const {
853 O << Asm->AsciiDirective
855 for (unsigned i = 0, N = String.size(); i < N; ++i) {
856 unsigned char C = String[i];
858 if (!isascii(C) || iscntrl(C)) {
860 case '\b': O << "\\b"; break;
861 case '\f': O << "\\f"; break;
862 case '\n': O << "\\n"; break;
863 case '\r': O << "\\r"; break;
864 case '\t': O << "\\t"; break;
867 O << char('0' + (C >> 6));
868 O << char('0' + (C >> 3));
869 O << char('0' + (C >> 0));
872 } else if (C == '\"') {
874 } else if (C == '\'') {
883 /// PrintLabelName - Print label name in form used by Dwarf writer.
885 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
886 O << Asm->PrivateGlobalPrefix
889 if (Number) O << Number;
892 /// EmitLabel - Emit location label for internal use by Dwarf.
894 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
895 PrintLabelName(Tag, Number);
899 /// EmitReference - Emit a reference to a label.
901 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
902 if (AddressSize == 4)
903 O << Asm->Data32bitsDirective;
905 O << Asm->Data64bitsDirective;
907 PrintLabelName(Tag, Number);
909 void DwarfWriter::EmitReference(const std::string &Name) const {
910 if (AddressSize == 4)
911 O << Asm->Data32bitsDirective;
913 O << Asm->Data64bitsDirective;
918 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
919 /// assemblers do not accept absolute expressions with data directives, so there
920 /// is an option (needsSet) to use an intermediary 'set' expression.
921 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
922 const char *TagLo, unsigned NumberLo) const {
924 static unsigned SetCounter = 0;
927 PrintLabelName("set", SetCounter);
929 PrintLabelName(TagHi, NumberHi);
931 PrintLabelName(TagLo, NumberLo);
934 if (AddressSize == sizeof(int32_t))
935 O << Asm->Data32bitsDirective;
937 O << Asm->Data64bitsDirective;
939 PrintLabelName("set", SetCounter);
943 if (AddressSize == sizeof(int32_t))
944 O << Asm->Data32bitsDirective;
946 O << Asm->Data64bitsDirective;
948 PrintLabelName(TagHi, NumberHi);
950 PrintLabelName(TagLo, NumberLo);
954 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
956 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
957 return Abbreviations.insert(*Abbrev);
960 /// NewString - Add a string to the constant pool and returns a label.
962 DWLabel DwarfWriter::NewString(const std::string &String) {
963 unsigned StringID = StringPool.insert(String);
964 return DWLabel("string", StringID);
967 /// NewBasicType - Creates a new basic type if necessary, then adds to the
969 /// FIXME - Should never be needed.
970 DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) {
971 DIE *&Slot = TypeToDieMap[Ty];
972 if (Slot) return Slot;
976 unsigned Encoding = 0;
978 switch (Ty->getTypeID()) {
979 case Type::UByteTyID:
980 Name = "unsigned char";
982 Encoding = DW_ATE_unsigned_char;
984 case Type::SByteTyID:
987 Encoding = DW_ATE_signed_char;
989 case Type::UShortTyID:
990 Name = "unsigned short";
992 Encoding = DW_ATE_unsigned;
994 case Type::ShortTyID:
997 Encoding = DW_ATE_signed;
1000 Name = "unsigned int";
1002 Encoding = DW_ATE_unsigned;
1007 Encoding = DW_ATE_signed;
1009 case Type::ULongTyID:
1010 Name = "unsigned long long";
1012 Encoding = DW_ATE_unsigned;
1014 case Type::LongTyID:
1017 Encoding = DW_ATE_signed;
1019 case Type::FloatTyID:
1022 Encoding = DW_ATE_float;
1024 case Type::DoubleTyID:
1027 Encoding = DW_ATE_float;
1030 // FIXME - handle more complex types.
1033 Encoding = DW_ATE_address;
1037 // construct the type DIE.
1038 Slot = new DIE(DW_TAG_base_type);
1039 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
1040 Slot->AddUInt (DW_AT_byte_size, 0, Size);
1041 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1044 Context->AddChild(Slot);
1049 /// NewType - Create a new type DIE.
1051 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc) {
1052 // FIXME - hack to get around NULL types short term.
1053 if (!TyDesc) return NewBasicType(Context, Type::IntTy);
1055 // FIXME - Should handle other contexts that compile units.
1057 // Check for pre-existence.
1058 DIE *&Slot = DescToDieMap[TyDesc];
1059 if (Slot) return Slot;
1061 // Get core information.
1062 const std::string &Name = TyDesc->getName();
1063 uint64_t Size = TyDesc->getSize() >> 3;
1067 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1068 // Fundamental types like int, float, bool
1069 Slot = Ty = new DIE(DW_TAG_base_type);
1070 unsigned Encoding = BasicTy->getEncoding();
1071 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1072 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1073 // Create specific DIE.
1074 Slot = Ty = new DIE(DerivedTy->getTag());
1076 // Map to main type, void will not have a type.
1077 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1078 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
1080 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1081 // Create specific DIE.
1082 Slot = Ty = new DIE(CompTy->getTag());
1083 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1085 switch (CompTy->getTag()) {
1086 case DW_TAG_array_type: {
1087 // Add element type.
1088 if (TypeDesc *FromTy = CompTy->getFromType()) {
1089 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
1091 // Don't emit size attribute.
1094 // Construct an anonymous type for index type.
1095 DIE *IndexTy = new DIE(DW_TAG_base_type);
1096 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1097 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1099 Context->AddChild(IndexTy);
1101 // Add subranges to array type.
1102 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1103 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1104 int64_t Lo = SRD->getLo();
1105 int64_t Hi = SRD->getHi();
1106 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1108 // If a range is available.
1110 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1111 // Only add low if non-zero.
1112 if (Lo) Subrange->AddUInt(DW_AT_lower_bound, 0, Lo);
1113 Subrange->AddUInt(DW_AT_upper_bound, 0, Hi);
1115 Ty->AddChild(Subrange);
1120 case DW_TAG_structure_type: {
1123 case DW_TAG_union_type: {
1126 case DW_TAG_enumeration_type: {
1133 assert(Ty && "Type not supported yet");
1135 // Add size if non-zero (derived types don't have a size.)
1136 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1137 // Add name if not anonymous or intermediate type.
1138 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1139 // Add source line info if present.
1140 if (CompileUnitDesc *File = TyDesc->getFile()) {
1141 CompileUnit *FileUnit = FindCompileUnit(File);
1142 unsigned FileID = FileUnit->getID();
1143 int Line = TyDesc->getLine();
1144 Ty->AddUInt(DW_AT_decl_file, 0, FileID);
1145 Ty->AddUInt(DW_AT_decl_line, 0, Line);
1148 // Add to context owner.
1149 Context->AddChild(Ty);
1154 /// NewCompileUnit - Create new compile unit and it's die.
1156 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1158 // Construct debug information entry.
1159 DIE *Die = new DIE(DW_TAG_compile_unit);
1160 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1161 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1162 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1163 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1164 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1165 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1166 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1168 // Add die to descriptor map.
1169 DescToDieMap[UnitDesc] = Die;
1171 // Construct compile unit.
1172 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1174 // Add Unit to compile unit map.
1175 DescToUnitMap[UnitDesc] = Unit;
1180 /// FindCompileUnit - Get the compile unit for the given descriptor.
1182 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1183 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1184 assert(Unit && "Missing compile unit.");
1188 /// NewGlobalVariable - Add a new global variable DIE.
1190 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1191 // Check for pre-existence.
1192 DIE *&Slot = DescToDieMap[GVD];
1193 if (Slot) return Slot;
1195 // Get the compile unit context.
1196 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1197 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1198 // Get the global variable itself.
1199 GlobalVariable *GV = GVD->getGlobalVariable();
1200 // Generate the mangled name.
1201 std::string MangledName = Asm->Mang->getValueName(GV);
1203 // Gather the details (simplify add attribute code.)
1204 const std::string &Name = GVD->getName();
1205 unsigned FileID = Unit->getID();
1206 unsigned Line = GVD->getLine();
1208 // Get the global's type.
1209 DIE *Type = NewType(Unit->getDie(), GVD->getTypeDesc());
1211 // Create the globale variable DIE.
1212 DIE *VariableDie = new DIE(DW_TAG_variable);
1213 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1214 VariableDie->AddUInt (DW_AT_decl_file, 0, FileID);
1215 VariableDie->AddUInt (DW_AT_decl_line, 0, Line);
1216 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1217 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1218 // FIXME - needs to be a proper expression.
1219 VariableDie->AddObjectLabel(DW_AT_location, DW_FORM_block1, MangledName);
1224 // Add to context owner.
1225 Unit->getDie()->AddChild(VariableDie);
1227 // Expose as global.
1228 // FIXME - need to check external flag.
1229 Unit->AddGlobal(Name, VariableDie);
1234 /// NewSubprogram - Add a new subprogram DIE.
1236 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1237 // Check for pre-existence.
1238 DIE *&Slot = DescToDieMap[SPD];
1239 if (Slot) return Slot;
1241 // Get the compile unit context.
1242 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1243 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1245 // Gather the details (simplify add attribute code.)
1246 const std::string &Name = SPD->getName();
1247 unsigned FileID = Unit->getID();
1248 // FIXME - faking the line for the time being.
1251 // FIXME - faking the type for the time being.
1252 DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy);
1254 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1255 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1256 SubprogramDie->AddUInt (DW_AT_decl_file, 0, FileID);
1257 SubprogramDie->AddUInt (DW_AT_decl_line, 0, Line);
1258 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1259 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1262 Slot = SubprogramDie;
1264 // Add to context owner.
1265 Unit->getDie()->AddChild(SubprogramDie);
1267 // Expose as global.
1268 Unit->AddGlobal(Name, SubprogramDie);
1270 return SubprogramDie;
1273 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1274 /// tools to recognize the object file contains Dwarf information.
1276 void DwarfWriter::EmitInitial() const {
1277 // Dwarf sections base addresses.
1278 Asm->SwitchSection(DwarfFrameSection, 0);
1279 EmitLabel("section_frame", 0);
1280 Asm->SwitchSection(DwarfInfoSection, 0);
1281 EmitLabel("section_info", 0);
1282 EmitLabel("info", 0);
1283 Asm->SwitchSection(DwarfAbbrevSection, 0);
1284 EmitLabel("section_abbrev", 0);
1285 EmitLabel("abbrev", 0);
1286 Asm->SwitchSection(DwarfARangesSection, 0);
1287 EmitLabel("section_aranges", 0);
1288 Asm->SwitchSection(DwarfMacInfoSection, 0);
1289 EmitLabel("section_macinfo", 0);
1290 Asm->SwitchSection(DwarfLineSection, 0);
1291 EmitLabel("section_line", 0);
1292 EmitLabel("line", 0);
1293 Asm->SwitchSection(DwarfLocSection, 0);
1294 EmitLabel("section_loc", 0);
1295 Asm->SwitchSection(DwarfPubNamesSection, 0);
1296 EmitLabel("section_pubnames", 0);
1297 Asm->SwitchSection(DwarfStrSection, 0);
1298 EmitLabel("section_str", 0);
1299 Asm->SwitchSection(DwarfRangesSection, 0);
1300 EmitLabel("section_ranges", 0);
1302 Asm->SwitchSection(TextSection, 0);
1303 EmitLabel("text_begin", 0);
1304 Asm->SwitchSection(DataSection, 0);
1305 EmitLabel("data_begin", 0);
1308 /// EmitDIE - Recusively Emits a debug information entry.
1310 void DwarfWriter::EmitDIE(DIE *Die) const {
1311 // Get the abbreviation for this DIE.
1312 unsigned AbbrevID = Die->getAbbrevID();
1313 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1317 // Emit the code (index) for the abbreviation.
1318 EmitULEB128Bytes(AbbrevID);
1319 EOL(std::string("Abbrev [" +
1321 "] 0x" + utohexstr(Die->getOffset()) +
1322 ":0x" + utohexstr(Die->getSize()) + " " +
1323 TagString(Abbrev.getTag())));
1325 const std::vector<DIEValue *> &Values = Die->getValues();
1326 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1328 // Emit the DIE attribute values.
1329 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1330 unsigned Attr = AbbrevData[i].getAttribute();
1331 unsigned Form = AbbrevData[i].getForm();
1332 assert(Form && "Too many attributes for DIE (check abbreviation)");
1335 case DW_AT_sibling: {
1336 EmitInt32(Die->SiblingOffset());
1340 // Emit an attribute using the defined form.
1341 Values[i]->EmitValue(*this, Form);
1346 EOL(AttributeString(Attr));
1349 // Emit the DIE children if any.
1350 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1351 const std::vector<DIE *> &Children = Die->getChildren();
1353 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1354 // FIXME - handle sibling offsets.
1355 // FIXME - handle all DIE types.
1356 EmitDIE(Children[j]);
1359 EmitInt8(0); EOL("End Of Children Mark");
1363 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1365 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1366 // Record the abbreviation.
1367 Die->Complete(*this);
1369 // Get the abbreviation for this DIE.
1370 unsigned AbbrevID = Die->getAbbrevID();
1371 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1374 Die->setOffset(Offset);
1376 // Start the size with the size of abbreviation code.
1377 Offset += SizeULEB128(AbbrevID);
1379 const std::vector<DIEValue *> &Values = Die->getValues();
1380 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1382 // Emit the DIE attribute values.
1383 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1384 // Size attribute value.
1385 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1388 // Emit the DIE children if any.
1389 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1390 const std::vector<DIE *> &Children = Die->getChildren();
1392 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1393 // FIXME - handle sibling offsets.
1394 // FIXME - handle all DIE types.
1395 Offset = SizeAndOffsetDie(Children[j], Offset);
1398 // End of children marker.
1399 Offset += sizeof(int8_t);
1402 Die->setSize(Offset - Die->getOffset());
1406 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1408 void DwarfWriter::SizeAndOffsets() {
1410 // Process each compile unit.
1411 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1412 CompileUnit *Unit = CompileUnits[i];
1413 if (Unit->hasContent()) {
1414 // Compute size of compile unit header
1415 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1416 sizeof(int16_t) + // DWARF version number
1417 sizeof(int32_t) + // Offset Into Abbrev. Section
1418 sizeof(int8_t); // Pointer Size (in bytes)
1420 SizeAndOffsetDie(Unit->getDie(), Offset);
1425 /// EmitDebugInfo - Emit the debug info section.
1427 void DwarfWriter::EmitDebugInfo() const {
1428 // Start debug info section.
1429 Asm->SwitchSection(DwarfInfoSection, 0);
1431 // Process each compile unit.
1432 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1433 CompileUnit *Unit = CompileUnits[i];
1435 if (Unit->hasContent()) {
1436 DIE *Die = Unit->getDie();
1437 // Emit the compile units header.
1438 EmitLabel("info_begin", Unit->getID());
1439 // Emit size of content not including length itself
1440 unsigned ContentSize = Die->getSize() +
1441 sizeof(int16_t) + // DWARF version number
1442 sizeof(int32_t) + // Offset Into Abbrev. Section
1443 sizeof(int8_t); // Pointer Size (in bytes)
1445 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1446 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1447 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1448 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1451 EmitLabel("info_end", Unit->getID());
1458 /// EmitAbbreviations - Emit the abbreviation section.
1460 void DwarfWriter::EmitAbbreviations() const {
1461 // Check to see if it is worth the effort.
1462 if (!Abbreviations.empty()) {
1463 // Start the debug abbrev section.
1464 Asm->SwitchSection(DwarfAbbrevSection, 0);
1466 EmitLabel("abbrev_begin", 0);
1468 // For each abbrevation.
1469 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1470 AbbrevID <= NAID; ++AbbrevID) {
1471 // Get abbreviation data
1472 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1474 // Emit the abbrevations code (base 1 index.)
1475 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1477 // Emit the abbreviations data.
1483 EmitLabel("abbrev_end", 0);
1489 /// EmitDebugLines - Emit source line information.
1491 void DwarfWriter::EmitDebugLines() const {
1492 // Minimum line delta, thus ranging from -10..(255-10).
1493 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1494 // Maximum line delta, thus ranging from -10..(255-10).
1495 const int MaxLineDelta = 255 + MinLineDelta;
1497 // Start the dwarf line section.
1498 Asm->SwitchSection(DwarfLineSection, 0);
1500 // Construct the section header.
1502 EmitDifference("line_end", 0, "line_begin", 0);
1503 EOL("Length of Source Line Info");
1504 EmitLabel("line_begin", 0);
1506 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1508 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1509 EOL("Prolog Length");
1510 EmitLabel("line_prolog_begin", 0);
1512 EmitInt8(1); EOL("Minimum Instruction Length");
1514 EmitInt8(1); EOL("Default is_stmt_start flag");
1516 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1518 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1520 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1522 // Line number standard opcode encodings argument count
1523 EmitInt8(0); EOL("DW_LNS_copy arg count");
1524 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1525 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1526 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1527 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1528 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1529 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1530 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1531 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1533 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1534 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1536 // Emit directories.
1537 for (unsigned DirectoryID = 1, NDID = Directories.size();
1538 DirectoryID <= NDID; ++DirectoryID) {
1539 EmitString(Directories[DirectoryID]); EOL("Directory");
1541 EmitInt8(0); EOL("End of directories");
1544 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1545 SourceID <= NSID; ++SourceID) {
1546 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1547 EmitString(SourceFile.getName()); EOL("Source");
1548 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1549 EmitULEB128Bytes(0); EOL("Mod date");
1550 EmitULEB128Bytes(0); EOL("File size");
1552 EmitInt8(0); EOL("End of files");
1554 EmitLabel("line_prolog_end", 0);
1556 // Emit line information
1557 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1559 // Dwarf assumes we start with first line of first source file.
1560 unsigned Source = 1;
1563 // Construct rows of the address, source, line, column matrix.
1564 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1565 SourceLineInfo *LineInfo = LineInfos[i];
1568 unsigned SourceID = LineInfo->getSourceID();
1569 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1570 unsigned DirectoryID = SourceFile.getDirectoryID();
1572 << Asm->CommentString << " "
1573 << Directories[DirectoryID]
1574 << SourceFile.getName() << ":"
1575 << LineInfo->getLine() << "\n";
1578 // Define the line address.
1579 EmitInt8(0); EOL("Extended Op");
1580 EmitInt8(4 + 1); EOL("Op size");
1581 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1582 EmitReference("loc", i + 1); EOL("Location label");
1584 // If change of source, then switch to the new source.
1585 if (Source != LineInfo->getSourceID()) {
1586 Source = LineInfo->getSourceID();
1587 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1588 EmitULEB128Bytes(Source); EOL("New Source");
1591 // If change of line.
1592 if (Line != LineInfo->getLine()) {
1593 // Determine offset.
1594 int Offset = LineInfo->getLine() - Line;
1595 int Delta = Offset - MinLineDelta;
1598 Line = LineInfo->getLine();
1600 // If delta is small enough and in range...
1601 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1602 // ... then use fast opcode.
1603 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1605 // ... otherwise use long hand.
1606 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1607 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1608 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1611 // Copy the previous row (different address or source)
1612 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1616 // Define last address.
1617 EmitInt8(0); EOL("Extended Op");
1618 EmitInt8(4 + 1); EOL("Op size");
1619 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1620 EmitReference("text_end", 0); EOL("Location label");
1622 // Mark end of matrix.
1623 EmitInt8(0); EOL("DW_LNE_end_sequence");
1624 EmitULEB128Bytes(1); O << "\n";
1625 EmitInt8(1); O << "\n";
1627 EmitLabel("line_end", 0);
1632 /// EmitDebugFrame - Emit visible names into a debug frame section.
1634 void DwarfWriter::EmitDebugFrame() {
1635 // FIXME - Should be per frame
1638 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1640 void DwarfWriter::EmitDebugPubNames() {
1641 // Start the dwarf pubnames section.
1642 Asm->SwitchSection(DwarfPubNamesSection, 0);
1644 // Process each compile unit.
1645 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1646 CompileUnit *Unit = CompileUnits[i];
1648 if (Unit->hasContent()) {
1649 EmitDifference("pubnames_end", Unit->getID(),
1650 "pubnames_begin", Unit->getID());
1651 EOL("Length of Public Names Info");
1653 EmitLabel("pubnames_begin", Unit->getID());
1655 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1657 EmitReference("info_begin", Unit->getID());
1658 EOL("Offset of Compilation Unit Info");
1660 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
1661 EOL("Compilation Unit Length");
1663 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
1665 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
1668 const std::string &Name = GI->first;
1669 DIE * Entity = GI->second;
1671 EmitInt32(Entity->getOffset()); EOL("DIE offset");
1672 EmitString(Name); EOL("External Name");
1675 EmitInt32(0); EOL("End Mark");
1676 EmitLabel("pubnames_end", Unit->getID());
1683 /// EmitDebugStr - Emit visible names into a debug str section.
1685 void DwarfWriter::EmitDebugStr() {
1686 // Check to see if it is worth the effort.
1687 if (!StringPool.empty()) {
1688 // Start the dwarf str section.
1689 Asm->SwitchSection(DwarfStrSection, 0);
1691 // For each of strings in teh string pool.
1692 for (unsigned StringID = 1, N = StringPool.size();
1693 StringID <= N; ++StringID) {
1694 // Emit a label for reference from debug information entries.
1695 EmitLabel("string", StringID);
1696 // Emit the string itself.
1697 const std::string &String = StringPool[StringID];
1698 EmitString(String); O << "\n";
1705 /// EmitDebugLoc - Emit visible names into a debug loc section.
1707 void DwarfWriter::EmitDebugLoc() {
1708 // Start the dwarf loc section.
1709 Asm->SwitchSection(DwarfLocSection, 0);
1714 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1716 void DwarfWriter::EmitDebugARanges() {
1717 // Start the dwarf aranges section.
1718 Asm->SwitchSection(DwarfARangesSection, 0);
1722 // Process each compile unit.
1723 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1724 CompileUnit *Unit = CompileUnits[i];
1726 if (Unit->hasContent()) {
1727 // Don't include size of length
1728 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
1730 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
1732 EmitReference("info_begin", Unit->getID());
1733 EOL("Offset of Compilation Unit Info");
1735 EmitInt8(AddressSize); EOL("Size of Address");
1737 EmitInt8(0); EOL("Size of Segment Descriptor");
1739 EmitInt16(0); EOL("Pad (1)");
1740 EmitInt16(0); EOL("Pad (2)");
1743 EmitReference("text_begin", 0); EOL("Address");
1744 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
1746 EmitInt32(0); EOL("EOM (1)");
1747 EmitInt32(0); EOL("EOM (2)");
1755 /// EmitDebugRanges - Emit visible names into a debug ranges section.
1757 void DwarfWriter::EmitDebugRanges() {
1758 // Start the dwarf ranges section.
1759 Asm->SwitchSection(DwarfRangesSection, 0);
1764 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1766 void DwarfWriter::EmitDebugMacInfo() {
1767 // Start the dwarf macinfo section.
1768 Asm->SwitchSection(DwarfMacInfoSection, 0);
1773 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1775 void DwarfWriter::ConstructCompileUnitDIEs() {
1776 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
1778 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
1779 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
1780 CompileUnits.push_back(Unit);
1784 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1786 void DwarfWriter::ConstructGlobalDIEs(Module &M) {
1787 std::vector<GlobalVariableDesc *> GlobalVariables =
1788 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
1790 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
1791 GlobalVariableDesc *GVD = GlobalVariables[i];
1792 NewGlobalVariable(GVD);
1796 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
1798 void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
1799 std::vector<SubprogramDesc *> Subprograms =
1800 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
1802 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
1803 SubprogramDesc *SPD = Subprograms[i];
1808 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
1810 bool DwarfWriter::ShouldEmitDwarf() {
1811 // Check if debug info is present.
1812 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1814 // Make sure initial declarations are made.
1824 //===----------------------------------------------------------------------===//
1825 // Main entry points.
1828 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
1839 , AddressSize(sizeof(int32_t))
1844 , DwarfAbbrevSection(".debug_abbrev")
1845 , DwarfInfoSection(".debug_info")
1846 , DwarfLineSection(".debug_line")
1847 , DwarfFrameSection(".debug_frame")
1848 , DwarfPubNamesSection(".debug_pubnames")
1849 , DwarfPubTypesSection(".debug_pubtypes")
1850 , DwarfStrSection(".debug_str")
1851 , DwarfLocSection(".debug_loc")
1852 , DwarfARangesSection(".debug_aranges")
1853 , DwarfRangesSection(".debug_ranges")
1854 , DwarfMacInfoSection(".debug_macinfo")
1855 , TextSection(".text")
1856 , DataSection(".data")
1858 DwarfWriter::~DwarfWriter() {
1859 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1860 delete CompileUnits[i];
1864 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
1866 void DwarfWriter::BeginModule(Module &M) {
1867 if (!ShouldEmitDwarf()) return;
1868 EOL("Dwarf Begin Module");
1871 /// EndModule - Emit all Dwarf sections that should come after the content.
1873 void DwarfWriter::EndModule(Module &M) {
1874 if (!ShouldEmitDwarf()) return;
1875 EOL("Dwarf End Module");
1877 // Standard sections final addresses.
1878 Asm->SwitchSection(TextSection, 0);
1879 EmitLabel("text_end", 0);
1880 Asm->SwitchSection(DataSection, 0);
1881 EmitLabel("data_end", 0);
1883 // Create all the compile unit DIEs.
1884 ConstructCompileUnitDIEs();
1886 // Create DIEs for each of the externally visible global variables.
1887 ConstructGlobalDIEs(M);
1889 // Create DIEs for each of the externally visible subprograms.
1890 ConstructSubprogramDIEs(M);
1892 // Compute DIE offsets and sizes.
1895 // Emit all the DIEs into a debug info section
1898 // Corresponding abbreviations into a abbrev section.
1899 EmitAbbreviations();
1901 // Emit source line correspondence into a debug line section.
1904 // Emit info into a debug frame section.
1905 // EmitDebugFrame();
1907 // Emit info into a debug pubnames section.
1908 EmitDebugPubNames();
1910 // Emit info into a debug str section.
1913 // Emit info into a debug loc section.
1916 // Emit info into a debug aranges section.
1919 // Emit info into a debug ranges section.
1922 // Emit info into a debug macinfo section.
1926 /// BeginFunction - Gather pre-function debug information.
1928 void DwarfWriter::BeginFunction(MachineFunction &MF) {
1929 if (!ShouldEmitDwarf()) return;
1930 EOL("Dwarf Begin Function");
1933 /// EndFunction - Gather and emit post-function debug information.
1935 void DwarfWriter::EndFunction(MachineFunction &MF) {
1936 if (!ShouldEmitDwarf()) return;
1937 EOL("Dwarf End Function");