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 dwarf;
32 DwarfVerbose("dwarf-verbose", cl::Hidden,
33 cl::desc("Add comments to Dwarf directives."));
37 //===----------------------------------------------------------------------===//
38 // Forward declarations.
43 //===----------------------------------------------------------------------===//
44 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
45 // Dwarf abbreviation.
48 unsigned Attribute; // Dwarf attribute code.
49 unsigned Form; // Dwarf form code.
52 DIEAbbrevData(unsigned A, unsigned F)
58 unsigned getAttribute() const { return Attribute; }
59 unsigned getForm() const { return Form; }
61 /// operator== - Used by DIEAbbrev to locate entry.
63 bool operator==(const DIEAbbrevData &DAD) const {
64 return Attribute == DAD.Attribute && Form == DAD.Form;
67 /// operator!= - Used by DIEAbbrev to locate entry.
69 bool operator!=(const DIEAbbrevData &DAD) const {
70 return Attribute != DAD.Attribute || Form != DAD.Form;
73 /// operator< - Used by DIEAbbrev to locate entry.
75 bool operator<(const DIEAbbrevData &DAD) const {
76 return Attribute < DAD.Attribute ||
77 (Attribute == DAD.Attribute && Form < DAD.Form);
81 //===----------------------------------------------------------------------===//
82 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
83 // information object.
86 unsigned Tag; // Dwarf tag code.
87 unsigned ChildrenFlag; // Dwarf children flag.
88 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
92 DIEAbbrev(unsigned T, unsigned C)
100 unsigned getTag() const { return Tag; }
101 unsigned getChildrenFlag() const { return ChildrenFlag; }
102 const std::vector<DIEAbbrevData> &getData() const { return Data; }
103 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
105 /// operator== - Used by UniqueVector to locate entry.
107 bool operator==(const DIEAbbrev &DA) const;
109 /// operator< - Used by UniqueVector to locate entry.
111 bool operator<(const DIEAbbrev &DA) const;
113 /// AddAttribute - Adds another set of attribute information to the
115 void AddAttribute(unsigned Attribute, unsigned Form) {
116 Data.push_back(DIEAbbrevData(Attribute, Form));
119 /// Emit - Print the abbreviation using the specified Dwarf writer.
121 void Emit(const DwarfWriter &DW) const;
124 void print(std::ostream &O);
129 //===----------------------------------------------------------------------===//
130 // DIEValue - A debug information entry value.
143 unsigned Type; // Type of the value
145 DIEValue(unsigned T) : Type(T) {}
146 virtual ~DIEValue() {}
148 // Implement isa/cast/dyncast.
149 static bool classof(const DIEValue *) { return true; }
151 /// EmitValue - Emit value via the Dwarf writer.
153 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
155 /// SizeOf - Return the size of a value in bytes.
157 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
160 //===----------------------------------------------------------------------===//
161 // DWInteger - An integer value DIE.
163 class DIEInteger : public DIEValue {
168 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
170 // Implement isa/cast/dyncast.
171 static bool classof(const DIEInteger *) { return true; }
172 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
174 /// EmitValue - Emit integer of appropriate size.
176 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
178 /// SizeOf - Determine size of integer value in bytes.
180 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
183 //===----------------------------------------------------------------------===//
184 // DIEString - A string value DIE.
186 struct DIEString : public DIEValue {
187 const std::string String;
189 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
191 // Implement isa/cast/dyncast.
192 static bool classof(const DIEString *) { return true; }
193 static bool classof(const DIEValue *S) { return S->Type == isString; }
195 /// EmitValue - Emit string value.
197 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
199 /// SizeOf - Determine size of string value in bytes.
201 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
204 //===----------------------------------------------------------------------===//
205 // DIEDwarfLabel - A Dwarf internal label expression DIE.
207 struct DIEDwarfLabel : public DIEValue {
210 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
212 // Implement isa/cast/dyncast.
213 static bool classof(const DIEDwarfLabel *) { return true; }
214 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
216 /// EmitValue - Emit label value.
218 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
220 /// SizeOf - Determine size of label value in bytes.
222 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
226 //===----------------------------------------------------------------------===//
227 // DIEObjectLabel - A label to an object in code or data.
229 struct DIEObjectLabel : public DIEValue {
230 const std::string Label;
232 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
234 // Implement isa/cast/dyncast.
235 static bool classof(const DIEObjectLabel *) { return true; }
236 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
238 /// EmitValue - Emit label value.
240 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
242 /// SizeOf - Determine size of label value in bytes.
244 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
247 //===----------------------------------------------------------------------===//
248 // DIEDelta - A simple label difference DIE.
250 struct DIEDelta : public DIEValue {
251 const DWLabel LabelHi;
252 const DWLabel LabelLo;
254 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
255 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
257 // Implement isa/cast/dyncast.
258 static bool classof(const DIEDelta *) { return true; }
259 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
261 /// EmitValue - Emit delta value.
263 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
265 /// SizeOf - Determine size of delta value in bytes.
267 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
270 //===----------------------------------------------------------------------===//
271 // DIEntry - A pointer to a debug information entry.
273 struct DIEntry : public DIEValue {
276 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
278 // Implement isa/cast/dyncast.
279 static bool classof(const DIEntry *) { return true; }
280 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
282 /// EmitValue - Emit delta value.
284 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
286 /// SizeOf - Determine size of delta value in bytes.
288 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
291 //===----------------------------------------------------------------------===//
292 // DIE - A structured debug information entry. Has an abbreviation which
293 // describes it's organization.
296 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
297 unsigned AbbrevID; // Decribing abbreviation ID.
298 unsigned Offset; // Offset in debug info section.
299 unsigned Size; // Size of instance + children.
300 std::vector<DIE *> Children; // Children DIEs.
301 std::vector<DIEValue *> Values; // Attributes values.
308 unsigned getAbbrevID() const { return AbbrevID; }
309 unsigned getOffset() const { return Offset; }
310 unsigned getSize() const { return Size; }
311 const std::vector<DIE *> &getChildren() const { return Children; }
312 const std::vector<DIEValue *> &getValues() const { return Values; }
313 void setOffset(unsigned O) { Offset = O; }
314 void setSize(unsigned S) { Size = S; }
316 /// SiblingOffset - Return the offset of the debug information entry's
318 unsigned SiblingOffset() const { return Offset + Size; }
320 /// AddUInt - Add an unsigned integer attribute data and value.
322 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
324 /// AddSInt - Add an signed integer attribute data and value.
326 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
328 /// AddString - Add a std::string attribute data and value.
330 void AddString(unsigned Attribute, unsigned Form,
331 const std::string &String);
333 /// AddLabel - Add a Dwarf label attribute data and value.
335 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
337 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
339 void AddObjectLabel(unsigned Attribute, unsigned Form,
340 const std::string &Label);
342 /// AddDelta - Add a label delta attribute data and value.
344 void AddDelta(unsigned Attribute, unsigned Form,
345 const DWLabel &Hi, const DWLabel &Lo);
347 /// AddDIEntry - Add a DIE attribute data and value.
349 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
351 /// Complete - Indicate that all attributes have been added and
352 /// ready to get an abbreviation ID.
354 void Complete(DwarfWriter &DW);
356 /// AddChild - Add a child to the DIE.
357 void AddChild(DIE *Child);
360 } // End of namespace llvm
362 //===----------------------------------------------------------------------===//
364 /// operator== - Used by UniqueVector to locate entry.
366 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
367 if (Tag != DA.Tag) return false;
368 if (ChildrenFlag != DA.ChildrenFlag) return false;
369 if (Data.size() != DA.Data.size()) return false;
371 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
372 if (Data[i] != DA.Data[i]) return false;
378 /// operator< - Used by UniqueVector to locate entry.
380 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
381 if (Tag != DA.Tag) return Tag < DA.Tag;
382 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
383 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
385 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
386 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
392 /// Emit - Print the abbreviation using the specified Dwarf writer.
394 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
395 // Emit its Dwarf tag type.
396 DW.EmitULEB128Bytes(Tag);
397 DW.EOL(TagString(Tag));
399 // Emit whether it has children DIEs.
400 DW.EmitULEB128Bytes(ChildrenFlag);
401 DW.EOL(ChildrenString(ChildrenFlag));
403 // For each attribute description.
404 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
405 const DIEAbbrevData &AttrData = Data[i];
407 // Emit attribute type.
408 DW.EmitULEB128Bytes(AttrData.getAttribute());
409 DW.EOL(AttributeString(AttrData.getAttribute()));
412 DW.EmitULEB128Bytes(AttrData.getForm());
413 DW.EOL(FormEncodingString(AttrData.getForm()));
416 // Mark end of abbreviation.
417 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
418 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
422 void DIEAbbrev::print(std::ostream &O) {
423 O << "Abbreviation @"
424 << std::hex << (intptr_t)this << std::dec
428 << ChildrenString(ChildrenFlag)
431 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
433 << AttributeString(Data[i].getAttribute())
435 << FormEncodingString(Data[i].getForm())
439 void DIEAbbrev::dump() { print(std::cerr); }
442 //===----------------------------------------------------------------------===//
444 /// EmitValue - Emit integer of appropriate size.
446 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
448 case DW_FORM_flag: // Fall thru
449 case DW_FORM_data1: DW.EmitInt8(Integer); break;
450 case DW_FORM_data2: DW.EmitInt16(Integer); break;
451 case DW_FORM_data4: DW.EmitInt32(Integer); break;
452 case DW_FORM_data8: DW.EmitInt64(Integer); break;
453 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
454 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
455 default: assert(0 && "DIE Value form not supported yet"); break;
459 /// SizeOf - Determine size of integer value in bytes.
461 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
463 case DW_FORM_flag: // Fall thru
464 case DW_FORM_data1: return sizeof(int8_t);
465 case DW_FORM_data2: return sizeof(int16_t);
466 case DW_FORM_data4: return sizeof(int32_t);
467 case DW_FORM_data8: return sizeof(int64_t);
468 case DW_FORM_udata: return DW.SizeULEB128(Integer);
469 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
470 default: assert(0 && "DIE Value form not supported yet"); break;
475 //===----------------------------------------------------------------------===//
477 /// EmitValue - Emit string value.
479 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
480 DW.EmitString(String);
483 /// SizeOf - Determine size of string value in bytes.
485 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
486 return String.size() + sizeof(char); // sizeof('\0');
489 //===----------------------------------------------------------------------===//
491 /// EmitValue - Emit label value.
493 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
494 DW.EmitReference(Label);
497 /// SizeOf - Determine size of label value in bytes.
499 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
500 return DW.getAddressSize();
503 //===----------------------------------------------------------------------===//
505 /// EmitValue - Emit label value.
507 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
508 DW.EmitInt8(sizeof(int8_t) + DW.getAddressSize());
509 DW.EOL("DW_FORM_block1 length");
511 DW.EmitInt8(DW_OP_addr);
512 DW.EOL("DW_OP_addr");
514 DW.EmitReference(Label);
517 /// SizeOf - Determine size of label value in bytes.
519 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
520 return sizeof(int8_t) + sizeof(int8_t) + DW.getAddressSize();
523 //===----------------------------------------------------------------------===//
525 /// EmitValue - Emit delta value.
527 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
528 DW.EmitDifference(LabelHi, LabelLo);
531 /// SizeOf - Determine size of delta value in bytes.
533 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
534 return DW.getAddressSize();
537 //===----------------------------------------------------------------------===//
538 /// EmitValue - Emit extry offset.
540 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
541 DW.EmitInt32(Entry->getOffset());
544 /// SizeOf - Determine size of label value in bytes.
546 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
547 return sizeof(int32_t);
550 //===----------------------------------------------------------------------===//
552 DIE::DIE(unsigned Tag)
553 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
562 if (Abbrev) delete Abbrev;
564 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
568 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
573 /// AddUInt - Add an unsigned integer attribute data and value.
575 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
577 if ((unsigned char)Integer == Integer) Form = DW_FORM_data1;
578 else if ((unsigned short)Integer == Integer) Form = DW_FORM_data2;
579 else if ((unsigned int)Integer == Integer) Form = DW_FORM_data4;
580 else Form = DW_FORM_data8;
582 Abbrev->AddAttribute(Attribute, Form);
583 Values.push_back(new DIEInteger(Integer));
586 /// AddSInt - Add an signed integer attribute data and value.
588 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
590 if ((char)Integer == Integer) Form = DW_FORM_data1;
591 else if ((short)Integer == Integer) Form = DW_FORM_data2;
592 else if ((int)Integer == Integer) Form = DW_FORM_data4;
593 else Form = DW_FORM_data8;
595 Abbrev->AddAttribute(Attribute, Form);
596 Values.push_back(new DIEInteger(Integer));
599 /// AddString - Add a std::string attribute data and value.
601 void DIE::AddString(unsigned Attribute, unsigned Form,
602 const std::string &String) {
603 Abbrev->AddAttribute(Attribute, Form);
604 Values.push_back(new DIEString(String));
607 /// AddLabel - Add a Dwarf label attribute data and value.
609 void DIE::AddLabel(unsigned Attribute, unsigned Form,
610 const DWLabel &Label) {
611 Abbrev->AddAttribute(Attribute, Form);
612 Values.push_back(new DIEDwarfLabel(Label));
615 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
617 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
618 const std::string &Label) {
619 Abbrev->AddAttribute(Attribute, Form);
620 Values.push_back(new DIEObjectLabel(Label));
623 /// AddDelta - Add a label delta attribute data and value.
625 void DIE::AddDelta(unsigned Attribute, unsigned Form,
626 const DWLabel &Hi, const DWLabel &Lo) {
627 Abbrev->AddAttribute(Attribute, Form);
628 Values.push_back(new DIEDelta(Hi, Lo));
631 /// AddDIEntry - Add a DIE attribute data and value.
633 void DIE::AddDIEntry(unsigned Attribute,
634 unsigned Form, DIE *Entry) {
635 Abbrev->AddAttribute(Attribute, Form);
636 Values.push_back(new DIEntry(Entry));
639 /// Complete - Indicate that all attributes have been added and ready to get an
641 void DIE::Complete(DwarfWriter &DW) {
642 AbbrevID = DW.NewAbbreviation(Abbrev);
647 /// AddChild - Add a child to the DIE.
649 void DIE::AddChild(DIE *Child) {
650 assert(Abbrev && "Adding children without an abbreviation");
651 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
652 Children.push_back(Child);
655 //===----------------------------------------------------------------------===//
659 //===----------------------------------------------------------------------===//
661 /// PrintHex - Print a value as a hexidecimal value.
663 void DwarfWriter::PrintHex(int Value) const {
664 O << "0x" << std::hex << Value << std::dec;
667 /// EOL - Print a newline character to asm stream. If a comment is present
668 /// then it will be printed first. Comments should not contain '\n'.
669 void DwarfWriter::EOL(const std::string &Comment) const {
672 << Asm->CommentString
679 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
680 /// unsigned leb128 value.
681 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
686 O << Asm->Data8bitsDirective;
691 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
692 /// signed leb128 value.
693 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
698 O << Asm->Data8bitsDirective;
703 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
704 /// representing an unsigned leb128 value.
705 void DwarfWriter::PrintULEB128(unsigned Value) const {
707 unsigned Byte = Value & 0x7f;
709 if (Value) Byte |= 0x80;
711 if (Value) O << ", ";
715 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
717 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
721 Size += sizeof(int8_t);
726 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
727 /// representing a signed leb128 value.
728 void DwarfWriter::PrintSLEB128(int Value) const {
729 int Sign = Value >> (8 * sizeof(Value) - 1);
733 unsigned Byte = Value & 0x7f;
735 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
736 if (IsMore) Byte |= 0x80;
738 if (IsMore) O << ", ";
742 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
744 unsigned DwarfWriter::SizeSLEB128(int Value) {
746 int Sign = Value >> (8 * sizeof(Value) - 1);
750 unsigned Byte = Value & 0x7f;
752 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
753 Size += sizeof(int8_t);
758 /// EmitInt8 - Emit a byte directive and value.
760 void DwarfWriter::EmitInt8(int Value) const {
761 O << Asm->Data8bitsDirective;
762 PrintHex(Value & 0xFF);
765 /// EmitInt16 - Emit a short directive and value.
767 void DwarfWriter::EmitInt16(int Value) const {
768 O << Asm->Data16bitsDirective;
769 PrintHex(Value & 0xFFFF);
772 /// EmitInt32 - Emit a long directive and value.
774 void DwarfWriter::EmitInt32(int Value) const {
775 O << Asm->Data32bitsDirective;
779 /// EmitInt64 - Emit a long long directive and value.
781 void DwarfWriter::EmitInt64(uint64_t Value) const {
782 if (Asm->Data64bitsDirective) {
783 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
785 const TargetData &TD = Asm->TM.getTargetData();
787 if (TD.isBigEndian()) {
788 EmitInt32(unsigned(Value >> 32)); O << "\n";
789 EmitInt32(unsigned(Value));
791 EmitInt32(unsigned(Value)); O << "\n";
792 EmitInt32(unsigned(Value >> 32));
797 /// EmitString - Emit a string with quotes and a null terminator.
798 /// Special characters are emitted properly. (Eg. '\t')
799 void DwarfWriter::EmitString(const std::string &String) const {
800 O << Asm->AsciiDirective
802 for (unsigned i = 0, N = String.size(); i < N; ++i) {
803 unsigned char C = String[i];
805 if (!isascii(C) || iscntrl(C)) {
807 case '\b': O << "\\b"; break;
808 case '\f': O << "\\f"; break;
809 case '\n': O << "\\n"; break;
810 case '\r': O << "\\r"; break;
811 case '\t': O << "\\t"; break;
814 O << char('0' + (C >> 6));
815 O << char('0' + (C >> 3));
816 O << char('0' + (C >> 0));
819 } else if (C == '\"') {
821 } else if (C == '\'') {
830 /// PrintLabelName - Print label name in form used by Dwarf writer.
832 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
833 O << Asm->PrivateGlobalPrefix
836 if (Number) O << Number;
839 /// EmitLabel - Emit location label for internal use by Dwarf.
841 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
842 PrintLabelName(Tag, Number);
846 /// EmitReference - Emit a reference to a label.
848 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
849 if (AddressSize == 4)
850 O << Asm->Data32bitsDirective;
852 O << Asm->Data64bitsDirective;
854 PrintLabelName(Tag, Number);
856 void DwarfWriter::EmitReference(const std::string &Name) const {
857 if (AddressSize == 4)
858 O << Asm->Data32bitsDirective;
860 O << Asm->Data64bitsDirective;
865 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
866 /// assemblers do not accept absolute expressions with data directives, so there
867 /// is an option (needsSet) to use an intermediary 'set' expression.
868 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
869 const char *TagLo, unsigned NumberLo) const {
871 static unsigned SetCounter = 0;
874 PrintLabelName("set", SetCounter);
876 PrintLabelName(TagHi, NumberHi);
878 PrintLabelName(TagLo, NumberLo);
881 if (AddressSize == sizeof(int32_t))
882 O << Asm->Data32bitsDirective;
884 O << Asm->Data64bitsDirective;
886 PrintLabelName("set", SetCounter);
890 if (AddressSize == sizeof(int32_t))
891 O << Asm->Data32bitsDirective;
893 O << Asm->Data64bitsDirective;
895 PrintLabelName(TagHi, NumberHi);
897 PrintLabelName(TagLo, NumberLo);
901 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
903 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
904 return Abbreviations.insert(*Abbrev);
907 /// NewString - Add a string to the constant pool and returns a label.
909 DWLabel DwarfWriter::NewString(const std::string &String) {
910 unsigned StringID = StringPool.insert(String);
911 return DWLabel("string", StringID);
914 /// NewBasicType - Creates a new basic type if necessary, then adds to the
916 /// FIXME - Should never be needed.
917 DIE *DwarfWriter::NewBasicType(DIE *Owner, Type *Ty) {
918 DIE *&Slot = TypeToDieMap[Ty];
919 if (Slot) return Slot;
923 unsigned Encoding = 0;
925 switch (Ty->getTypeID()) {
926 case Type::UByteTyID:
927 Name = "unsigned char";
929 Encoding = DW_ATE_unsigned_char;
931 case Type::SByteTyID:
934 Encoding = DW_ATE_signed_char;
936 case Type::UShortTyID:
937 Name = "unsigned short";
939 Encoding = DW_ATE_unsigned;
941 case Type::ShortTyID:
944 Encoding = DW_ATE_signed;
947 Name = "unsigned int";
949 Encoding = DW_ATE_unsigned;
954 Encoding = DW_ATE_signed;
956 case Type::ULongTyID:
957 Name = "unsigned long long";
959 Encoding = DW_ATE_unsigned;
964 Encoding = DW_ATE_signed;
966 case Type::FloatTyID:
969 Encoding = DW_ATE_float;
971 case Type::DoubleTyID:
974 Encoding = DW_ATE_float;
977 // FIXME - handle more complex types.
980 Encoding = DW_ATE_address;
984 // construct the type DIE.
985 Slot = new DIE(DW_TAG_base_type);
986 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
987 Slot->AddUInt (DW_AT_byte_size, 0, Size);
988 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
990 // Add to context owner.
991 Owner->AddChild(Slot);
996 /// NewGlobalType - Make the type visible globally using the given name.
998 void DwarfWriter::NewGlobalType(const std::string &Name, DIE *Type) {
999 assert(!GlobalTypes[Name] && "Duplicate global type");
1000 GlobalTypes[Name] = Type;
1003 /// NewGlobalEntity - Make the entity visible globally using the given name.
1005 void DwarfWriter::NewGlobalEntity(const std::string &Name, DIE *Entity) {
1006 assert(!GlobalEntities[Name] && "Duplicate global variable or function");
1007 GlobalEntities[Name] = Entity;
1010 /// NewType - Create a new type DIE.
1012 DIE *DwarfWriter::NewType(DIE *Unit, TypeDesc *TyDesc) {
1013 // FIXME - hack to get around NULL types short term.
1014 if (!TyDesc) return NewBasicType(Unit, Type::IntTy);
1016 // Check for pre-existence.
1017 DIE *&Slot = DescToDieMap[TyDesc];
1018 if (Slot) return Slot;
1020 // Get core information.
1021 const std::string &Name = TyDesc->getName();
1022 // FIXME - handle larger sizes.
1023 unsigned Size = TyDesc->getSize() >> 3;
1027 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1028 // Fundamental types like int, float, bool
1029 Slot = Ty = new DIE(DW_TAG_base_type);
1030 unsigned Encoding = BasicTy->getEncoding();
1031 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1032 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1033 // Determine which derived type.
1035 switch (DerivedTy->getTag()) {
1036 case DI_TAG_typedef: T = DW_TAG_typedef; break;
1037 case DI_TAG_pointer: T = DW_TAG_pointer_type; break;
1038 case DI_TAG_reference: T = DW_TAG_reference_type; break;
1039 default: assert( 0 && "Unknown tag on derived type");
1042 // Create specific DIE.
1043 Slot = Ty = new DIE(T);
1045 // Map to main type, void will not have a type.
1046 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1047 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Unit, FromTy));
1051 assert(Ty && "Type not supported yet");
1053 // Add size if non-zero (derived types don't have a size.)
1054 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1055 // Add name if not anonymous or intermediate type.
1056 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1057 // Add source line info if present.
1058 if (CompileUnitDesc *File = TyDesc->getFile()) {
1059 unsigned FileID = DebugInfo->RecordSource(File);
1060 int Line = TyDesc->getLine();
1061 Ty->AddUInt(DW_AT_decl_file, 0, FileID);
1062 Ty->AddUInt(DW_AT_decl_line, 0, Line);
1065 // Add to context owner.
1071 /// NewCompileUnit - Create new compile unit DIE.
1073 DIE *DwarfWriter::NewCompileUnit(CompileUnitDesc *CompileUnit) {
1074 // Check for pre-existence.
1075 DIE *&Slot = DescToDieMap[CompileUnit];
1076 if (Slot) return Slot;
1078 DIE *Unit = new DIE(DW_TAG_compile_unit);
1079 // FIXME - use the correct line set.
1080 Unit->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0));
1081 Unit->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1082 Unit->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1083 Unit->AddString(DW_AT_producer, DW_FORM_string, CompileUnit->getProducer());
1084 Unit->AddUInt (DW_AT_language, DW_FORM_data1, CompileUnit->getLanguage());
1085 Unit->AddString(DW_AT_name, DW_FORM_string, CompileUnit->getFileName());
1086 Unit->AddString(DW_AT_comp_dir, DW_FORM_string, CompileUnit->getDirectory());
1093 /// NewGlobalVariable - Add a new global variable DIE.
1095 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1096 // Check for pre-existence.
1097 DIE *&Slot = DescToDieMap[GVD];
1098 if (Slot) return Slot;
1100 // Get the compile unit context.
1101 CompileUnitDesc *CompileUnit =
1102 static_cast<CompileUnitDesc *>(GVD->getContext());
1103 DIE *Unit = NewCompileUnit(CompileUnit);
1104 // Get the global variable itself.
1105 GlobalVariable *GV = GVD->getGlobalVariable();
1106 // Generate the mangled name.
1107 std::string MangledName = Asm->Mang->getValueName(GV);
1109 // Gather the details (simplify add attribute code.)
1110 const std::string &Name = GVD->getName();
1111 unsigned FileID = DebugInfo->RecordSource(CompileUnit);
1112 unsigned Line = GVD->getLine();
1114 // Get the global's type.
1115 DIE *Type = NewType(Unit, GVD->getTypeDesc());
1117 // Create the globale variable DIE.
1118 DIE *VariableDie = new DIE(DW_TAG_variable);
1119 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1120 VariableDie->AddUInt (DW_AT_decl_file, 0, FileID);
1121 VariableDie->AddUInt (DW_AT_decl_line, 0, Line);
1122 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1123 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1124 // FIXME - needs to be a proper expression.
1125 VariableDie->AddObjectLabel(DW_AT_location, DW_FORM_block1, MangledName);
1130 // Add to context owner.
1131 Unit->AddChild(VariableDie);
1133 // Expose as global.
1134 NewGlobalEntity(Name, VariableDie);
1139 /// NewSubprogram - Add a new subprogram DIE.
1141 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1142 // Check for pre-existence.
1143 DIE *&Slot = DescToDieMap[SPD];
1144 if (Slot) return Slot;
1146 // Get the compile unit context.
1147 CompileUnitDesc *CompileUnit =
1148 static_cast<CompileUnitDesc *>(SPD->getContext());
1149 DIE *Unit = NewCompileUnit(CompileUnit);
1151 // Gather the details (simplify add attribute code.)
1152 const std::string &Name = SPD->getName();
1153 unsigned FileID = DebugInfo->RecordSource(CompileUnit);
1154 // FIXME - faking the line for the time being.
1157 // FIXME - faking the type for the time being.
1158 DIE *Type = NewBasicType(Unit, Type::IntTy);
1160 DIE *SubprogramDie = new DIE(DW_TAG_variable);
1161 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1162 SubprogramDie->AddUInt (DW_AT_decl_file, 0, FileID);
1163 SubprogramDie->AddUInt (DW_AT_decl_line, 0, Line);
1164 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1165 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1168 Slot = SubprogramDie;
1170 // Add to context owner.
1171 Unit->AddChild(SubprogramDie);
1173 // Expose as global.
1174 NewGlobalEntity(Name, SubprogramDie);
1176 return SubprogramDie;
1179 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1180 /// tools to recognize the object file contains Dwarf information.
1182 void DwarfWriter::EmitInitial() const {
1183 // Dwarf sections base addresses.
1184 Asm->SwitchSection(DwarfFrameSection, 0);
1185 EmitLabel("section_frame", 0);
1186 Asm->SwitchSection(DwarfInfoSection, 0);
1187 EmitLabel("section_info", 0);
1188 EmitLabel("info", 0);
1189 Asm->SwitchSection(DwarfAbbrevSection, 0);
1190 EmitLabel("section_abbrev", 0);
1191 EmitLabel("abbrev", 0);
1192 Asm->SwitchSection(DwarfARangesSection, 0);
1193 EmitLabel("section_aranges", 0);
1194 Asm->SwitchSection(DwarfMacInfoSection, 0);
1195 EmitLabel("section_macinfo", 0);
1196 Asm->SwitchSection(DwarfLineSection, 0);
1197 EmitLabel("section_line", 0);
1198 EmitLabel("line", 0);
1199 Asm->SwitchSection(DwarfLocSection, 0);
1200 EmitLabel("section_loc", 0);
1201 Asm->SwitchSection(DwarfPubNamesSection, 0);
1202 EmitLabel("section_pubnames", 0);
1203 Asm->SwitchSection(DwarfStrSection, 0);
1204 EmitLabel("section_str", 0);
1205 Asm->SwitchSection(DwarfRangesSection, 0);
1206 EmitLabel("section_ranges", 0);
1208 Asm->SwitchSection(TextSection, 0);
1209 EmitLabel("text_begin", 0);
1210 Asm->SwitchSection(DataSection, 0);
1211 EmitLabel("data_begin", 0);
1214 /// EmitDIE - Recusively Emits a debug information entry.
1216 void DwarfWriter::EmitDIE(DIE *Die) const {
1217 // Get the abbreviation for this DIE.
1218 unsigned AbbrevID = Die->getAbbrevID();
1219 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1223 // Emit the code (index) for the abbreviation.
1224 EmitULEB128Bytes(AbbrevID);
1225 EOL(std::string("Abbrev [" +
1227 "] 0x" + utohexstr(Die->getOffset()) +
1228 ":0x" + utohexstr(Die->getSize()) + " " +
1229 TagString(Abbrev.getTag())));
1231 const std::vector<DIEValue *> &Values = Die->getValues();
1232 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1234 // Emit the DIE attribute values.
1235 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1236 unsigned Attr = AbbrevData[i].getAttribute();
1237 unsigned Form = AbbrevData[i].getForm();
1238 assert(Form && "Too many attributes for DIE (check abbreviation)");
1241 case DW_AT_sibling: {
1242 EmitInt32(Die->SiblingOffset());
1246 // Emit an attribute using the defined form.
1247 Values[i]->EmitValue(*this, Form);
1252 EOL(AttributeString(Attr));
1255 // Emit the DIE children if any.
1256 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1257 const std::vector<DIE *> &Children = Die->getChildren();
1259 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1260 // FIXME - handle sibling offsets.
1261 // FIXME - handle all DIE types.
1262 EmitDIE(Children[j]);
1265 EmitInt8(0); EOL("End Of Children Mark");
1269 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1271 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1272 // Record the abbreviation.
1273 Die->Complete(*this);
1275 // Get the abbreviation for this DIE.
1276 unsigned AbbrevID = Die->getAbbrevID();
1277 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1280 Die->setOffset(Offset);
1282 // Start the size with the size of abbreviation code.
1283 Offset += SizeULEB128(AbbrevID);
1285 const std::vector<DIEValue *> &Values = Die->getValues();
1286 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1288 // Emit the DIE attribute values.
1289 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1290 // Size attribute value.
1291 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1294 // Emit the DIE children if any.
1295 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1296 const std::vector<DIE *> &Children = Die->getChildren();
1298 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1299 // FIXME - handle sibling offsets.
1300 // FIXME - handle all DIE types.
1301 Offset = SizeAndOffsetDie(Children[j], Offset);
1304 // End of children marker.
1305 Offset += sizeof(int8_t);
1308 Die->setSize(Offset - Die->getOffset());
1312 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1314 void DwarfWriter::SizeAndOffsets() {
1315 unsigned Offset = 0;
1317 // Process each compile unit.
1318 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1319 // Compute size of compile unit header
1320 Offset += sizeof(int32_t) + // Length of Compilation Unit Info
1321 sizeof(int16_t) + // DWARF version number
1322 sizeof(int32_t) + // Offset Into Abbrev. Section
1323 sizeof(int8_t); // Pointer Size (in bytes)
1325 Offset = SizeAndOffsetDie(CompileUnits[i], Offset);
1329 /// EmitDebugInfo - Emit the debug info section.
1331 void DwarfWriter::EmitDebugInfo() const {
1332 // Start debug info section.
1333 Asm->SwitchSection(DwarfInfoSection, 0);
1335 // Get the number of compile units.
1336 unsigned N = CompileUnits.size();
1338 // If there are any compile units.
1340 EmitLabel("info_begin", 0);
1342 // Process each compile unit.
1343 for (unsigned i = 0; i < N; ++i) {
1344 // Emit the compile units header.
1346 // Emit size of content not including length itself
1347 unsigned ContentSize = CompileUnits[i]->getSize() +
1348 sizeof(int16_t) + // DWARF version number
1349 sizeof(int32_t) + // Offset Into Abbrev. Section
1350 sizeof(int8_t); // Pointer Size (in bytes)
1352 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1353 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1354 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1355 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1357 EmitDIE(CompileUnits[i]);
1360 EmitLabel("info_end", 0);
1366 /// EmitAbbreviations - Emit the abbreviation section.
1368 void DwarfWriter::EmitAbbreviations() const {
1369 // Check to see if it is worth the effort.
1370 if (!Abbreviations.empty()) {
1371 // Start the debug abbrev section.
1372 Asm->SwitchSection(DwarfAbbrevSection, 0);
1374 EmitLabel("abbrev_begin", 0);
1376 // For each abbrevation.
1377 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1378 AbbrevID <= NAID; ++AbbrevID) {
1379 // Get abbreviation data
1380 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1382 // Emit the abbrevations code (base 1 index.)
1383 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1385 // Emit the abbreviations data.
1391 EmitLabel("abbrev_end", 0);
1397 /// EmitDebugLines - Emit source line information.
1399 void DwarfWriter::EmitDebugLines() const {
1400 // Minimum line delta, thus ranging from -10..(255-10).
1401 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1402 // Maximum line delta, thus ranging from -10..(255-10).
1403 const int MaxLineDelta = 255 + MinLineDelta;
1405 // Start the dwarf line section.
1406 Asm->SwitchSection(DwarfLineSection, 0);
1408 // Construct the section header.
1410 EmitDifference("line_end", 0, "line_begin", 0);
1411 EOL("Length of Source Line Info");
1412 EmitLabel("line_begin", 0);
1414 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1416 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1417 EOL("Prolog Length");
1418 EmitLabel("line_prolog_begin", 0);
1420 EmitInt8(1); EOL("Minimum Instruction Length");
1422 EmitInt8(1); EOL("Default is_stmt_start flag");
1424 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1426 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1428 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1430 // Line number standard opcode encodings argument count
1431 EmitInt8(0); EOL("DW_LNS_copy arg count");
1432 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1433 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1434 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1435 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1436 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1437 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1438 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1439 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1441 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1442 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1444 // Emit directories.
1445 for (unsigned DirectoryID = 1, NDID = Directories.size();
1446 DirectoryID <= NDID; ++DirectoryID) {
1447 EmitString(Directories[DirectoryID]); EOL("Directory");
1449 EmitInt8(0); EOL("End of directories");
1452 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1453 SourceID <= NSID; ++SourceID) {
1454 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1455 EmitString(SourceFile.getName()); EOL("Source");
1456 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1457 EmitULEB128Bytes(0); EOL("Mod date");
1458 EmitULEB128Bytes(0); EOL("File size");
1460 EmitInt8(0); EOL("End of files");
1462 EmitLabel("line_prolog_end", 0);
1464 // Emit line information
1465 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1467 // Dwarf assumes we start with first line of first source file.
1468 unsigned Source = 1;
1471 // Construct rows of the address, source, line, column matrix.
1472 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1473 SourceLineInfo *LineInfo = LineInfos[i];
1476 unsigned SourceID = LineInfo->getSourceID();
1477 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1478 unsigned DirectoryID = SourceFile.getDirectoryID();
1480 << Asm->CommentString << " "
1481 << Directories[DirectoryID]
1482 << SourceFile.getName() << ":"
1483 << LineInfo->getLine() << "\n";
1486 // Define the line address.
1487 EmitInt8(0); EOL("Extended Op");
1488 EmitInt8(4 + 1); EOL("Op size");
1489 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1490 EmitReference("loc", i + 1); EOL("Location label");
1492 // If change of source, then switch to the new source.
1493 if (Source != LineInfo->getSourceID()) {
1494 Source = LineInfo->getSourceID();
1495 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1496 EmitULEB128Bytes(0); EOL("New Source");
1499 // If change of line.
1500 if (Line != LineInfo->getLine()) {
1501 // Determine offset.
1502 int Offset = LineInfo->getLine() - Line;
1503 int Delta = Offset - MinLineDelta;
1506 Line = LineInfo->getLine();
1508 // If delta is small enough and in range...
1509 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1510 // ... then use fast opcode.
1511 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1513 // ... otherwise use long hand.
1514 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1515 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1516 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1519 // Copy the previous row (different address or source)
1520 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1524 // Define last address.
1525 EmitInt8(0); EOL("Extended Op");
1526 EmitInt8(4 + 1); EOL("Op size");
1527 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1528 EmitReference("text_end", 0); EOL("Location label");
1530 // Mark end of matrix.
1531 EmitInt8(0); EOL("DW_LNE_end_sequence");
1532 EmitULEB128Bytes(1); O << "\n";
1533 EmitInt8(1); O << "\n";
1535 EmitLabel("line_end", 0);
1540 /// EmitDebugFrame - Emit visible names into a debug frame section.
1542 void DwarfWriter::EmitDebugFrame() {
1543 // FIXME - Should be per frame
1546 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1548 void DwarfWriter::EmitDebugPubNames() {
1549 // Check to see if it is worth the effort.
1550 if (!GlobalEntities.empty()) {
1551 // Start the dwarf pubnames section.
1552 Asm->SwitchSection(DwarfPubNamesSection, 0);
1554 EmitDifference("pubnames_end", 0, "pubnames_begin", 0);
1555 EOL("Length of Public Names Info");
1557 EmitLabel("pubnames_begin", 0);
1559 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1561 EmitReference("info_begin", 0); EOL("Offset of Compilation Unit Info");
1563 EmitDifference("info_end", 0, "info_begin", 0);
1564 EOL("Compilation Unit Length");
1566 for (std::map<std::string, DIE *>::iterator GI = GlobalEntities.begin(),
1567 GE = GlobalEntities.end();
1569 const std::string &Name = GI->first;
1570 DIE * Entity = GI->second;
1572 EmitInt32(Entity->getOffset()); EOL("DIE offset");
1573 EmitString(Name); EOL("External Name");
1577 EmitInt32(0); EOL("End Mark");
1578 EmitLabel("pubnames_end", 0);
1584 /// EmitDebugPubTypes - Emit visible names into a debug pubtypes section.
1586 void DwarfWriter::EmitDebugPubTypes() {
1587 // Check to see if it is worth the effort.
1588 if (!GlobalTypes.empty()) {
1589 // Start the dwarf pubtypes section.
1590 Asm->SwitchSection(DwarfPubTypesSection, 0);
1596 /// EmitDebugStr - Emit visible names into a debug str section.
1598 void DwarfWriter::EmitDebugStr() {
1599 // Check to see if it is worth the effort.
1600 if (!StringPool.empty()) {
1601 // Start the dwarf str section.
1602 Asm->SwitchSection(DwarfStrSection, 0);
1604 // For each of strings in teh string pool.
1605 for (unsigned StringID = 1, N = StringPool.size();
1606 StringID <= N; ++StringID) {
1607 // Emit a label for reference from debug information entries.
1608 EmitLabel("string", StringID);
1609 // Emit the string itself.
1610 const std::string &String = StringPool[StringID];
1611 EmitString(String); O << "\n";
1618 /// EmitDebugLoc - Emit visible names into a debug loc section.
1620 void DwarfWriter::EmitDebugLoc() {
1621 // Start the dwarf loc section.
1622 Asm->SwitchSection(DwarfLocSection, 0);
1627 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1629 void DwarfWriter::EmitDebugARanges() {
1630 // Start the dwarf aranges section.
1631 Asm->SwitchSection(DwarfARangesSection, 0);
1635 // Don't include size of length
1636 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
1638 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
1640 EmitReference("info_begin", 0); EOL("Offset of Compilation Unit Info");
1642 EmitInt8(AddressSize); EOL("Size of Address");
1644 EmitInt8(0); EOL("Size of Segment Descriptor");
1646 EmitInt16(0); EOL("Pad (1)");
1647 EmitInt16(0); EOL("Pad (2)");
1650 EmitReference("text_begin", 0); EOL("Address");
1651 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
1653 EmitInt32(0); EOL("EOM (1)");
1654 EmitInt32(0); EOL("EOM (2)");
1659 /// EmitDebugRanges - Emit visible names into a debug ranges section.
1661 void DwarfWriter::EmitDebugRanges() {
1662 // Start the dwarf ranges section.
1663 Asm->SwitchSection(DwarfRangesSection, 0);
1668 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1670 void DwarfWriter::EmitDebugMacInfo() {
1671 // Start the dwarf macinfo section.
1672 Asm->SwitchSection(DwarfMacInfoSection, 0);
1677 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1679 void DwarfWriter::ConstructCompileUnitDIEs() {
1680 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
1682 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
1683 DIE *Unit = NewCompileUnit(CUW[i]);
1684 CompileUnits.push_back(Unit);
1688 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1690 void DwarfWriter::ConstructGlobalDIEs(Module &M) {
1691 std::vector<GlobalVariableDesc *> GlobalVariables =
1692 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
1694 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
1695 GlobalVariableDesc *GVD = GlobalVariables[i];
1696 NewGlobalVariable(GVD);
1700 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
1702 void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
1703 std::vector<SubprogramDesc *> Subprograms =
1704 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
1706 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
1707 SubprogramDesc *SPD = Subprograms[i];
1712 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
1714 bool DwarfWriter::ShouldEmitDwarf() {
1715 // Check if debug info is present.
1716 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1718 // Make sure initial declarations are made.
1728 //===----------------------------------------------------------------------===//
1729 // Main entry points.
1732 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
1744 , AddressSize(sizeof(int32_t))
1749 , DwarfAbbrevSection(".debug_abbrev")
1750 , DwarfInfoSection(".debug_info")
1751 , DwarfLineSection(".debug_line")
1752 , DwarfFrameSection(".debug_frame")
1753 , DwarfPubNamesSection(".debug_pubnames")
1754 , DwarfPubTypesSection(".debug_pubtypes")
1755 , DwarfStrSection(".debug_str")
1756 , DwarfLocSection(".debug_loc")
1757 , DwarfARangesSection(".debug_aranges")
1758 , DwarfRangesSection(".debug_ranges")
1759 , DwarfMacInfoSection(".debug_macinfo")
1760 , TextSection(".text")
1761 , DataSection(".data")
1763 DwarfWriter::~DwarfWriter() {
1764 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1765 delete CompileUnits[i];
1769 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
1771 void DwarfWriter::BeginModule(Module &M) {
1772 if (!ShouldEmitDwarf()) return;
1773 EOL("Dwarf Begin Module");
1776 /// EndModule - Emit all Dwarf sections that should come after the content.
1778 void DwarfWriter::EndModule(Module &M) {
1779 if (!ShouldEmitDwarf()) return;
1780 EOL("Dwarf End Module");
1782 // Standard sections final addresses.
1783 Asm->SwitchSection(TextSection, 0);
1784 EmitLabel("text_end", 0);
1785 Asm->SwitchSection(DataSection, 0);
1786 EmitLabel("data_end", 0);
1788 // Create all the compile unit DIEs.
1789 ConstructCompileUnitDIEs();
1791 // Create DIEs for each of the externally visible global variables.
1792 ConstructGlobalDIEs(M);
1794 // Create DIEs for each of the externally visible subprograms.
1795 ConstructSubprogramDIEs(M);
1797 // Compute DIE offsets and sizes.
1800 // Emit all the DIEs into a debug info section
1803 // Corresponding abbreviations into a abbrev section.
1804 EmitAbbreviations();
1806 // Emit source line correspondence into a debug line section.
1809 // Emit info into a debug frame section.
1810 // EmitDebugFrame();
1812 // Emit info into a debug pubnames section.
1813 EmitDebugPubNames();
1815 // Emit info into a debug pubtypes section.
1816 // EmitDebugPubTypes();
1818 // Emit info into a debug str section.
1821 // Emit info into a debug loc section.
1824 // Emit info into a debug aranges section.
1827 // Emit info into a debug ranges section.
1830 // Emit info into a debug macinfo section.
1834 /// BeginFunction - Gather pre-function debug information.
1836 void DwarfWriter::BeginFunction(MachineFunction &MF) {
1837 if (!ShouldEmitDwarf()) return;
1838 EOL("Dwarf Begin Function");
1841 /// EndFunction - Gather and emit post-function debug information.
1843 void DwarfWriter::EndFunction(MachineFunction &MF) {
1844 if (!ShouldEmitDwarf()) return;
1845 EOL("Dwarf End Function");