1 //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/DwarfWriter.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Module.h"
18 #include "llvm/Type.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineDebugInfo.h"
21 #include "llvm/Support/Dwarf.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/Target/TargetMachine.h"
29 using namespace llvm::dwarf;
32 DwarfVerbose("dwarf-verbose", cl::Hidden,
33 cl::desc("Add comments to Dwarf directives."));
37 //===----------------------------------------------------------------------===//
38 // Forward declarations.
43 //===----------------------------------------------------------------------===//
44 // CompileUnit - This dwarf writer support class manages information associate
45 // with a source file.
48 CompileUnitDesc *Desc; // Compile unit debug descriptor.
49 unsigned ID; // File ID for source.
50 DIE *Die; // Compile unit die.
51 std::map<std::string, DIE *> Globals; // A map of globally visible named
52 // entities for this unit.
55 CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
65 CompileUnitDesc *getDesc() const { return Desc; }
66 unsigned getID() const { return ID; }
67 DIE* getDie() const { return Die; }
68 std::map<std::string, DIE *> &getGlobals() { return Globals; }
70 /// hasContent - Return true if this compile unit has something to write out.
72 bool hasContent() const;
74 /// AddGlobal - Add a new global entity to the compile unit.
76 void AddGlobal(const std::string &Name, DIE *Die);
80 //===----------------------------------------------------------------------===//
81 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
82 // Dwarf abbreviation.
85 unsigned Attribute; // Dwarf attribute code.
86 unsigned Form; // Dwarf form code.
89 DIEAbbrevData(unsigned A, unsigned F)
95 unsigned getAttribute() const { return Attribute; }
96 unsigned getForm() const { return Form; }
98 /// operator== - Used by DIEAbbrev to locate entry.
100 bool operator==(const DIEAbbrevData &DAD) const {
101 return Attribute == DAD.Attribute && Form == DAD.Form;
104 /// operator!= - Used by DIEAbbrev to locate entry.
106 bool operator!=(const DIEAbbrevData &DAD) const {
107 return Attribute != DAD.Attribute || Form != DAD.Form;
110 /// operator< - Used by DIEAbbrev to locate entry.
112 bool operator<(const DIEAbbrevData &DAD) const {
113 return Attribute < DAD.Attribute ||
114 (Attribute == DAD.Attribute && Form < DAD.Form);
118 //===----------------------------------------------------------------------===//
119 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
120 // information object.
123 unsigned Tag; // Dwarf tag code.
124 unsigned ChildrenFlag; // Dwarf children flag.
125 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
129 DIEAbbrev(unsigned T, unsigned C)
137 unsigned getTag() const { return Tag; }
138 unsigned getChildrenFlag() const { return ChildrenFlag; }
139 const std::vector<DIEAbbrevData> &getData() const { return Data; }
140 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
142 /// operator== - Used by UniqueVector to locate entry.
144 bool operator==(const DIEAbbrev &DA) const;
146 /// operator< - Used by UniqueVector to locate entry.
148 bool operator<(const DIEAbbrev &DA) const;
150 /// AddAttribute - Adds another set of attribute information to the
152 void AddAttribute(unsigned Attribute, unsigned Form) {
153 Data.push_back(DIEAbbrevData(Attribute, Form));
156 /// Emit - Print the abbreviation using the specified Dwarf writer.
158 void Emit(const DwarfWriter &DW) const;
161 void print(std::ostream &O);
166 //===----------------------------------------------------------------------===//
167 // DIEValue - A debug information entry value.
180 unsigned Type; // Type of the value
182 DIEValue(unsigned T) : Type(T) {}
183 virtual ~DIEValue() {}
185 // Implement isa/cast/dyncast.
186 static bool classof(const DIEValue *) { return true; }
188 /// EmitValue - Emit value via the Dwarf writer.
190 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
192 /// SizeOf - Return the size of a value in bytes.
194 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
197 //===----------------------------------------------------------------------===//
198 // DWInteger - An integer value DIE.
200 class DIEInteger : public DIEValue {
205 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
207 // Implement isa/cast/dyncast.
208 static bool classof(const DIEInteger *) { return true; }
209 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
211 /// EmitValue - Emit integer of appropriate size.
213 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
215 /// SizeOf - Determine size of integer value in bytes.
217 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
220 //===----------------------------------------------------------------------===//
221 // DIEString - A string value DIE.
223 struct DIEString : public DIEValue {
224 const std::string String;
226 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
228 // Implement isa/cast/dyncast.
229 static bool classof(const DIEString *) { return true; }
230 static bool classof(const DIEValue *S) { return S->Type == isString; }
232 /// EmitValue - Emit string value.
234 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
236 /// SizeOf - Determine size of string value in bytes.
238 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
241 //===----------------------------------------------------------------------===//
242 // DIEDwarfLabel - A Dwarf internal label expression DIE.
244 struct DIEDwarfLabel : public DIEValue {
247 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
249 // Implement isa/cast/dyncast.
250 static bool classof(const DIEDwarfLabel *) { return true; }
251 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
253 /// EmitValue - Emit label value.
255 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
257 /// SizeOf - Determine size of label value in bytes.
259 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
263 //===----------------------------------------------------------------------===//
264 // DIEObjectLabel - A label to an object in code or data.
266 struct DIEObjectLabel : public DIEValue {
267 const std::string Label;
269 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
271 // Implement isa/cast/dyncast.
272 static bool classof(const DIEObjectLabel *) { return true; }
273 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
275 /// EmitValue - Emit label value.
277 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
279 /// SizeOf - Determine size of label value in bytes.
281 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
284 //===----------------------------------------------------------------------===//
285 // DIEDelta - A simple label difference DIE.
287 struct DIEDelta : public DIEValue {
288 const DWLabel LabelHi;
289 const DWLabel LabelLo;
291 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
292 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
294 // Implement isa/cast/dyncast.
295 static bool classof(const DIEDelta *) { return true; }
296 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
298 /// EmitValue - Emit delta value.
300 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
302 /// SizeOf - Determine size of delta value in bytes.
304 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
307 //===----------------------------------------------------------------------===//
308 // DIEntry - A pointer to a debug information entry.
310 struct DIEntry : public DIEValue {
313 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
315 // Implement isa/cast/dyncast.
316 static bool classof(const DIEntry *) { return true; }
317 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
319 /// EmitValue - Emit delta value.
321 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
323 /// SizeOf - Determine size of delta value in bytes.
325 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
328 //===----------------------------------------------------------------------===//
329 // DIE - A structured debug information entry. Has an abbreviation which
330 // describes it's organization.
333 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
334 unsigned AbbrevID; // Decribing abbreviation ID.
335 unsigned Offset; // Offset in debug info section.
336 unsigned Size; // Size of instance + children.
337 std::vector<DIE *> Children; // Children DIEs.
338 std::vector<DIEValue *> Values; // Attributes values.
345 unsigned getAbbrevID() const { return AbbrevID; }
346 unsigned getOffset() const { return Offset; }
347 unsigned getSize() const { return Size; }
348 const std::vector<DIE *> &getChildren() const { return Children; }
349 const std::vector<DIEValue *> &getValues() const { return Values; }
350 void setOffset(unsigned O) { Offset = O; }
351 void setSize(unsigned S) { Size = S; }
353 /// SiblingOffset - Return the offset of the debug information entry's
355 unsigned SiblingOffset() const { return Offset + Size; }
357 /// AddUInt - Add an unsigned integer attribute data and value.
359 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
361 /// AddSInt - Add an signed integer attribute data and value.
363 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
365 /// AddString - Add a std::string attribute data and value.
367 void AddString(unsigned Attribute, unsigned Form,
368 const std::string &String);
370 /// AddLabel - Add a Dwarf label attribute data and value.
372 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
374 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
376 void AddObjectLabel(unsigned Attribute, unsigned Form,
377 const std::string &Label);
379 /// AddDelta - Add a label delta attribute data and value.
381 void AddDelta(unsigned Attribute, unsigned Form,
382 const DWLabel &Hi, const DWLabel &Lo);
384 /// AddDIEntry - Add a DIE attribute data and value.
386 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
388 /// Complete - Indicate that all attributes have been added and
389 /// ready to get an abbreviation ID.
391 void Complete(DwarfWriter &DW);
393 /// AddChild - Add a child to the DIE.
394 void AddChild(DIE *Child);
397 } // End of namespace llvm
399 //===----------------------------------------------------------------------===//
401 CompileUnit::~CompileUnit() {
405 /// hasContent - Return true if this compile unit has something to write out.
407 bool CompileUnit::hasContent() const {
408 return !Die->getChildren().empty();
411 /// AddGlobal - Add a new global entity to the compile unit.
413 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
417 //===----------------------------------------------------------------------===//
419 /// operator== - Used by UniqueVector to locate entry.
421 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
422 if (Tag != DA.Tag) return false;
423 if (ChildrenFlag != DA.ChildrenFlag) return false;
424 if (Data.size() != DA.Data.size()) return false;
426 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
427 if (Data[i] != DA.Data[i]) return false;
433 /// operator< - Used by UniqueVector to locate entry.
435 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
436 if (Tag != DA.Tag) return Tag < DA.Tag;
437 if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
438 if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
440 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
441 if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
447 /// Emit - Print the abbreviation using the specified Dwarf writer.
449 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
450 // Emit its Dwarf tag type.
451 DW.EmitULEB128Bytes(Tag);
452 DW.EOL(TagString(Tag));
454 // Emit whether it has children DIEs.
455 DW.EmitULEB128Bytes(ChildrenFlag);
456 DW.EOL(ChildrenString(ChildrenFlag));
458 // For each attribute description.
459 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
460 const DIEAbbrevData &AttrData = Data[i];
462 // Emit attribute type.
463 DW.EmitULEB128Bytes(AttrData.getAttribute());
464 DW.EOL(AttributeString(AttrData.getAttribute()));
467 DW.EmitULEB128Bytes(AttrData.getForm());
468 DW.EOL(FormEncodingString(AttrData.getForm()));
471 // Mark end of abbreviation.
472 DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
473 DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
477 void DIEAbbrev::print(std::ostream &O) {
478 O << "Abbreviation @"
479 << std::hex << (intptr_t)this << std::dec
483 << ChildrenString(ChildrenFlag)
486 for (unsigned i = 0, N = Data.size(); i < N; ++i) {
488 << AttributeString(Data[i].getAttribute())
490 << FormEncodingString(Data[i].getForm())
494 void DIEAbbrev::dump() { print(std::cerr); }
497 //===----------------------------------------------------------------------===//
499 /// EmitValue - Emit integer of appropriate size.
501 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
503 case DW_FORM_flag: // Fall thru
504 case DW_FORM_data1: DW.EmitInt8(Integer); break;
505 case DW_FORM_data2: DW.EmitInt16(Integer); break;
506 case DW_FORM_data4: DW.EmitInt32(Integer); break;
507 case DW_FORM_data8: DW.EmitInt64(Integer); break;
508 case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
509 case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
510 // FIXME - Punting on field offsets.
511 case DW_FORM_block1: {
512 DW.EmitInt8(1 + DW.SizeULEB128(Integer)); DW.EOL("Form1 Size");
513 DW.EmitInt8(DW_OP_plus_uconst); DW.EOL("DW_OP_plus_uconst");
514 DW.EmitULEB128Bytes(Integer);
517 default: assert(0 && "DIE Value form not supported yet"); break;
521 /// SizeOf - Determine size of integer value in bytes.
523 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
525 case DW_FORM_flag: // Fall thru
526 case DW_FORM_data1: return sizeof(int8_t);
527 case DW_FORM_data2: return sizeof(int16_t);
528 case DW_FORM_data4: return sizeof(int32_t);
529 case DW_FORM_data8: return sizeof(int64_t);
530 case DW_FORM_udata: return DW.SizeULEB128(Integer);
531 case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
532 // FIXME - Punting on field offsets.
533 case DW_FORM_block1: return 2 + DW.SizeULEB128(Integer);
534 default: assert(0 && "DIE Value form not supported yet"); break;
539 //===----------------------------------------------------------------------===//
541 /// EmitValue - Emit string value.
543 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
544 DW.EmitString(String);
547 /// SizeOf - Determine size of string value in bytes.
549 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
550 return String.size() + sizeof(char); // sizeof('\0');
553 //===----------------------------------------------------------------------===//
555 /// EmitValue - Emit label value.
557 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
558 DW.EmitReference(Label);
561 /// SizeOf - Determine size of label value in bytes.
563 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
564 return DW.getAddressSize();
567 //===----------------------------------------------------------------------===//
569 /// EmitValue - Emit label value.
571 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
572 DW.EmitInt8(sizeof(int8_t) + DW.getAddressSize());
573 DW.EOL("DW_FORM_block1 length");
575 DW.EmitInt8(DW_OP_addr);
576 DW.EOL("DW_OP_addr");
578 DW.EmitReference(Label);
581 /// SizeOf - Determine size of label value in bytes.
583 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
584 return sizeof(int8_t) + sizeof(int8_t) + DW.getAddressSize();
587 //===----------------------------------------------------------------------===//
589 /// EmitValue - Emit delta value.
591 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
592 DW.EmitDifference(LabelHi, LabelLo);
595 /// SizeOf - Determine size of delta value in bytes.
597 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
598 return DW.getAddressSize();
601 //===----------------------------------------------------------------------===//
602 /// EmitValue - Emit extry offset.
604 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
605 DW.EmitInt32(Entry->getOffset());
608 /// SizeOf - Determine size of label value in bytes.
610 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
611 return sizeof(int32_t);
614 //===----------------------------------------------------------------------===//
616 DIE::DIE(unsigned Tag)
617 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
626 if (Abbrev) delete Abbrev;
628 for (unsigned i = 0, N = Children.size(); i < N; ++i) {
632 for (unsigned j = 0, M = Values.size(); j < M; ++j) {
637 /// AddUInt - Add an unsigned integer attribute data and value.
639 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
641 if ((unsigned char)Integer == Integer) Form = DW_FORM_data1;
642 else if ((unsigned short)Integer == Integer) Form = DW_FORM_data2;
643 else if ((unsigned int)Integer == Integer) Form = DW_FORM_data4;
644 else Form = DW_FORM_data8;
646 Abbrev->AddAttribute(Attribute, Form);
647 Values.push_back(new DIEInteger(Integer));
650 /// AddSInt - Add an signed integer attribute data and value.
652 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
654 if ((char)Integer == Integer) Form = DW_FORM_data1;
655 else if ((short)Integer == Integer) Form = DW_FORM_data2;
656 else if ((int)Integer == Integer) Form = DW_FORM_data4;
657 else Form = DW_FORM_data8;
659 Abbrev->AddAttribute(Attribute, Form);
660 Values.push_back(new DIEInteger(Integer));
663 /// AddString - Add a std::string attribute data and value.
665 void DIE::AddString(unsigned Attribute, unsigned Form,
666 const std::string &String) {
667 Abbrev->AddAttribute(Attribute, Form);
668 Values.push_back(new DIEString(String));
671 /// AddLabel - Add a Dwarf label attribute data and value.
673 void DIE::AddLabel(unsigned Attribute, unsigned Form,
674 const DWLabel &Label) {
675 Abbrev->AddAttribute(Attribute, Form);
676 Values.push_back(new DIEDwarfLabel(Label));
679 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
681 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
682 const std::string &Label) {
683 Abbrev->AddAttribute(Attribute, Form);
684 Values.push_back(new DIEObjectLabel(Label));
687 /// AddDelta - Add a label delta attribute data and value.
689 void DIE::AddDelta(unsigned Attribute, unsigned Form,
690 const DWLabel &Hi, const DWLabel &Lo) {
691 Abbrev->AddAttribute(Attribute, Form);
692 Values.push_back(new DIEDelta(Hi, Lo));
695 /// AddDIEntry - Add a DIE attribute data and value.
697 void DIE::AddDIEntry(unsigned Attribute,
698 unsigned Form, DIE *Entry) {
699 Abbrev->AddAttribute(Attribute, Form);
700 Values.push_back(new DIEntry(Entry));
703 /// Complete - Indicate that all attributes have been added and ready to get an
705 void DIE::Complete(DwarfWriter &DW) {
706 AbbrevID = DW.NewAbbreviation(Abbrev);
711 /// AddChild - Add a child to the DIE.
713 void DIE::AddChild(DIE *Child) {
714 assert(Abbrev && "Adding children without an abbreviation");
715 Abbrev->setChildrenFlag(DW_CHILDREN_yes);
716 Children.push_back(Child);
719 //===----------------------------------------------------------------------===//
723 //===----------------------------------------------------------------------===//
725 /// PrintHex - Print a value as a hexidecimal value.
727 void DwarfWriter::PrintHex(int Value) const {
728 O << "0x" << std::hex << Value << std::dec;
731 /// EOL - Print a newline character to asm stream. If a comment is present
732 /// then it will be printed first. Comments should not contain '\n'.
733 void DwarfWriter::EOL(const std::string &Comment) const {
736 << Asm->CommentString
743 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
744 /// unsigned leb128 value.
745 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
750 O << Asm->Data8bitsDirective;
755 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
756 /// signed leb128 value.
757 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
762 O << Asm->Data8bitsDirective;
767 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
768 /// representing an unsigned leb128 value.
769 void DwarfWriter::PrintULEB128(unsigned Value) const {
771 unsigned Byte = Value & 0x7f;
773 if (Value) Byte |= 0x80;
775 if (Value) O << ", ";
779 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
781 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
785 Size += sizeof(int8_t);
790 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
791 /// representing a signed leb128 value.
792 void DwarfWriter::PrintSLEB128(int Value) const {
793 int Sign = Value >> (8 * sizeof(Value) - 1);
797 unsigned Byte = Value & 0x7f;
799 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
800 if (IsMore) Byte |= 0x80;
802 if (IsMore) O << ", ";
806 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
808 unsigned DwarfWriter::SizeSLEB128(int Value) {
810 int Sign = Value >> (8 * sizeof(Value) - 1);
814 unsigned Byte = Value & 0x7f;
816 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
817 Size += sizeof(int8_t);
822 /// EmitInt8 - Emit a byte directive and value.
824 void DwarfWriter::EmitInt8(int Value) const {
825 O << Asm->Data8bitsDirective;
826 PrintHex(Value & 0xFF);
829 /// EmitInt16 - Emit a short directive and value.
831 void DwarfWriter::EmitInt16(int Value) const {
832 O << Asm->Data16bitsDirective;
833 PrintHex(Value & 0xFFFF);
836 /// EmitInt32 - Emit a long directive and value.
838 void DwarfWriter::EmitInt32(int Value) const {
839 O << Asm->Data32bitsDirective;
843 /// EmitInt64 - Emit a long long directive and value.
845 void DwarfWriter::EmitInt64(uint64_t Value) const {
846 if (Asm->Data64bitsDirective) {
847 O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
849 const TargetData &TD = Asm->TM.getTargetData();
851 if (TD.isBigEndian()) {
852 EmitInt32(unsigned(Value >> 32)); O << "\n";
853 EmitInt32(unsigned(Value));
855 EmitInt32(unsigned(Value)); O << "\n";
856 EmitInt32(unsigned(Value >> 32));
861 /// EmitString - Emit a string with quotes and a null terminator.
862 /// Special characters are emitted properly. (Eg. '\t')
863 void DwarfWriter::EmitString(const std::string &String) const {
864 O << Asm->AsciiDirective
866 for (unsigned i = 0, N = String.size(); i < N; ++i) {
867 unsigned char C = String[i];
869 if (!isascii(C) || iscntrl(C)) {
871 case '\b': O << "\\b"; break;
872 case '\f': O << "\\f"; break;
873 case '\n': O << "\\n"; break;
874 case '\r': O << "\\r"; break;
875 case '\t': O << "\\t"; break;
878 O << char('0' + (C >> 6));
879 O << char('0' + (C >> 3));
880 O << char('0' + (C >> 0));
883 } else if (C == '\"') {
885 } else if (C == '\'') {
894 /// PrintLabelName - Print label name in form used by Dwarf writer.
896 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
897 O << Asm->PrivateGlobalPrefix
900 if (Number) O << Number;
903 /// EmitLabel - Emit location label for internal use by Dwarf.
905 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
906 PrintLabelName(Tag, Number);
910 /// EmitReference - Emit a reference to a label.
912 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
913 if (AddressSize == 4)
914 O << Asm->Data32bitsDirective;
916 O << Asm->Data64bitsDirective;
918 PrintLabelName(Tag, Number);
920 void DwarfWriter::EmitReference(const std::string &Name) const {
921 if (AddressSize == 4)
922 O << Asm->Data32bitsDirective;
924 O << Asm->Data64bitsDirective;
929 /// EmitDifference - Emit an label difference as sizeof(pointer) value. Some
930 /// assemblers do not accept absolute expressions with data directives, so there
931 /// is an option (needsSet) to use an intermediary 'set' expression.
932 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
933 const char *TagLo, unsigned NumberLo) const {
935 static unsigned SetCounter = 0;
938 PrintLabelName("set", SetCounter);
940 PrintLabelName(TagHi, NumberHi);
942 PrintLabelName(TagLo, NumberLo);
945 if (AddressSize == sizeof(int32_t))
946 O << Asm->Data32bitsDirective;
948 O << Asm->Data64bitsDirective;
950 PrintLabelName("set", SetCounter);
954 if (AddressSize == sizeof(int32_t))
955 O << Asm->Data32bitsDirective;
957 O << Asm->Data64bitsDirective;
959 PrintLabelName(TagHi, NumberHi);
961 PrintLabelName(TagLo, NumberLo);
965 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
967 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
968 return Abbreviations.insert(*Abbrev);
971 /// NewString - Add a string to the constant pool and returns a label.
973 DWLabel DwarfWriter::NewString(const std::string &String) {
974 unsigned StringID = StringPool.insert(String);
975 return DWLabel("string", StringID);
978 /// NewBasicType - Creates a new basic type if necessary, then adds to the
980 /// FIXME - Should never be needed.
981 DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) {
982 DIE *&Slot = TypeToDieMap[Ty];
983 if (Slot) return Slot;
987 unsigned Encoding = 0;
989 switch (Ty->getTypeID()) {
990 case Type::UByteTyID:
991 Name = "unsigned char";
993 Encoding = DW_ATE_unsigned_char;
995 case Type::SByteTyID:
998 Encoding = DW_ATE_signed_char;
1000 case Type::UShortTyID:
1001 Name = "unsigned short";
1003 Encoding = DW_ATE_unsigned;
1005 case Type::ShortTyID:
1008 Encoding = DW_ATE_signed;
1010 case Type::UIntTyID:
1011 Name = "unsigned int";
1013 Encoding = DW_ATE_unsigned;
1018 Encoding = DW_ATE_signed;
1020 case Type::ULongTyID:
1021 Name = "unsigned long long";
1023 Encoding = DW_ATE_unsigned;
1025 case Type::LongTyID:
1028 Encoding = DW_ATE_signed;
1030 case Type::FloatTyID:
1033 Encoding = DW_ATE_float;
1035 case Type::DoubleTyID:
1038 Encoding = DW_ATE_float;
1041 // FIXME - handle more complex types.
1044 Encoding = DW_ATE_address;
1048 // construct the type DIE.
1049 Slot = new DIE(DW_TAG_base_type);
1050 Slot->AddString(DW_AT_name, DW_FORM_string, Name);
1051 Slot->AddUInt (DW_AT_byte_size, 0, Size);
1052 Slot->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1055 Context->AddChild(Slot);
1060 /// NewType - Create a new type DIE.
1062 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc) {
1063 // FIXME - hack to get around NULL types short term.
1064 if (!TyDesc) return NewBasicType(Context, Type::IntTy);
1066 // FIXME - Should handle other contexts that compile units.
1068 // Check for pre-existence.
1069 DIE *&Slot = DescToDieMap[TyDesc];
1070 if (Slot) return Slot;
1072 // Get core information.
1073 const std::string &Name = TyDesc->getName();
1074 uint64_t Size = TyDesc->getSize() >> 3;
1078 if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1079 // Fundamental types like int, float, bool
1080 Slot = Ty = new DIE(DW_TAG_base_type);
1081 unsigned Encoding = BasicTy->getEncoding();
1082 Ty->AddUInt (DW_AT_encoding, DW_FORM_data1, Encoding);
1083 } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1084 // Create specific DIE.
1085 Slot = Ty = new DIE(DerivedTy->getTag());
1087 // Map to main type, void will not have a type.
1088 if (TypeDesc *FromTy = DerivedTy->getFromType()) {
1089 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
1091 } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1092 // Create specific DIE.
1093 Slot = Ty = new DIE(CompTy->getTag());
1094 std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1096 switch (CompTy->getTag()) {
1097 case DW_TAG_array_type: {
1098 // Add element type.
1099 if (TypeDesc *FromTy = CompTy->getFromType()) {
1100 Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Context, FromTy));
1102 // Don't emit size attribute.
1105 // Construct an anonymous type for index type.
1106 DIE *IndexTy = new DIE(DW_TAG_base_type);
1107 IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1108 IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1110 Context->AddChild(IndexTy);
1112 // Add subranges to array type.
1113 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1114 SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1115 int64_t Lo = SRD->getLo();
1116 int64_t Hi = SRD->getHi();
1117 DIE *Subrange = new DIE(DW_TAG_subrange_type);
1119 // If a range is available.
1121 Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1122 // Only add low if non-zero.
1123 if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
1124 Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
1126 Ty->AddChild(Subrange);
1131 case DW_TAG_structure_type:
1132 case DW_TAG_union_type: {
1133 // FIXME - this is just the basics.
1134 // Add elements to structure type.
1135 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1136 DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
1137 const std::string &Name = MemberDesc->getName();
1138 unsigned Line = MemberDesc->getLine();
1139 TypeDesc *MemTy = MemberDesc->getFromType();
1140 uint64_t Size = MemberDesc->getSize();
1141 uint64_t Offset = MemberDesc->getOffset();
1143 DIE *Member = new DIE(DW_TAG_member);
1144 if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
1145 if (CompileUnitDesc *File = MemberDesc->getFile()) {
1146 CompileUnit *FileUnit = FindCompileUnit(File);
1147 unsigned FileID = FileUnit->getID();
1148 int Line = TyDesc->getLine();
1149 Member->AddUInt(DW_AT_decl_file, 0, FileID);
1150 Member->AddUInt(DW_AT_decl_line, 0, Line);
1152 if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1153 Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1154 NewType(Context, FromTy));
1156 // FIXME - Punt on the Address.
1157 Member->AddUInt(DW_AT_data_member_location, DW_FORM_block1,
1159 Ty->AddChild(Member);
1163 case DW_TAG_enumeration_type: {
1164 // Add enumerators to enumeration type.
1165 for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1166 EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
1167 const std::string &Name = ED->getName();
1168 int64_t Value = ED->getValue();
1169 DIE *Enumerator = new DIE(DW_TAG_enumerator);
1170 Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
1171 Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
1172 Ty->AddChild(Enumerator);
1181 assert(Ty && "Type not supported yet");
1183 // Add size if non-zero (derived types don't have a size.)
1184 if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1185 // Add name if not anonymous or intermediate type.
1186 if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1187 // Add source line info if present.
1188 if (CompileUnitDesc *File = TyDesc->getFile()) {
1189 CompileUnit *FileUnit = FindCompileUnit(File);
1190 unsigned FileID = FileUnit->getID();
1191 int Line = TyDesc->getLine();
1192 Ty->AddUInt(DW_AT_decl_file, 0, FileID);
1193 Ty->AddUInt(DW_AT_decl_line, 0, Line);
1196 // Add to context owner.
1197 Context->AddChild(Ty);
1202 /// NewCompileUnit - Create new compile unit and it's die.
1204 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1206 // Construct debug information entry.
1207 DIE *Die = new DIE(DW_TAG_compile_unit);
1208 Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4, DWLabel("line", 0));
1209 Die->AddLabel (DW_AT_high_pc, DW_FORM_addr, DWLabel("text_end", 0));
1210 Die->AddLabel (DW_AT_low_pc, DW_FORM_addr, DWLabel("text_begin", 0));
1211 Die->AddString(DW_AT_producer, DW_FORM_string, UnitDesc->getProducer());
1212 Die->AddUInt (DW_AT_language, DW_FORM_data1, UnitDesc->getLanguage());
1213 Die->AddString(DW_AT_name, DW_FORM_string, UnitDesc->getFileName());
1214 Die->AddString(DW_AT_comp_dir, DW_FORM_string, UnitDesc->getDirectory());
1216 // Add die to descriptor map.
1217 DescToDieMap[UnitDesc] = Die;
1219 // Construct compile unit.
1220 CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1222 // Add Unit to compile unit map.
1223 DescToUnitMap[UnitDesc] = Unit;
1228 /// FindCompileUnit - Get the compile unit for the given descriptor.
1230 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1231 CompileUnit *Unit = DescToUnitMap[UnitDesc];
1232 assert(Unit && "Missing compile unit.");
1236 /// NewGlobalVariable - Add a new global variable DIE.
1238 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1239 // Check for pre-existence.
1240 DIE *&Slot = DescToDieMap[GVD];
1241 if (Slot) return Slot;
1243 // Get the compile unit context.
1244 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1245 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1246 // Get the global variable itself.
1247 GlobalVariable *GV = GVD->getGlobalVariable();
1248 // Generate the mangled name.
1249 std::string MangledName = Asm->Mang->getValueName(GV);
1251 // Gather the details (simplify add attribute code.)
1252 const std::string &Name = GVD->getName();
1253 unsigned FileID = Unit->getID();
1254 unsigned Line = GVD->getLine();
1256 // Get the global's type.
1257 DIE *Type = NewType(Unit->getDie(), GVD->getTypeDesc());
1259 // Create the globale variable DIE.
1260 DIE *VariableDie = new DIE(DW_TAG_variable);
1261 VariableDie->AddString (DW_AT_name, DW_FORM_string, Name);
1262 VariableDie->AddUInt (DW_AT_decl_file, 0, FileID);
1263 VariableDie->AddUInt (DW_AT_decl_line, 0, Line);
1264 VariableDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1265 VariableDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1266 // FIXME - needs to be a proper expression.
1267 VariableDie->AddObjectLabel(DW_AT_location, DW_FORM_block1, MangledName);
1272 // Add to context owner.
1273 Unit->getDie()->AddChild(VariableDie);
1275 // Expose as global.
1276 // FIXME - need to check external flag.
1277 Unit->AddGlobal(Name, VariableDie);
1282 /// NewSubprogram - Add a new subprogram DIE.
1284 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1285 // Check for pre-existence.
1286 DIE *&Slot = DescToDieMap[SPD];
1287 if (Slot) return Slot;
1289 // Get the compile unit context.
1290 CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1291 CompileUnit *Unit = FindCompileUnit(UnitDesc);
1293 // Gather the details (simplify add attribute code.)
1294 const std::string &Name = SPD->getName();
1295 unsigned FileID = Unit->getID();
1296 // FIXME - faking the line for the time being.
1299 // FIXME - faking the type for the time being.
1300 DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy);
1302 DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1303 SubprogramDie->AddString (DW_AT_name, DW_FORM_string, Name);
1304 SubprogramDie->AddUInt (DW_AT_decl_file, 0, FileID);
1305 SubprogramDie->AddUInt (DW_AT_decl_line, 0, Line);
1306 SubprogramDie->AddDIEntry (DW_AT_type, DW_FORM_ref4, Type);
1307 SubprogramDie->AddUInt (DW_AT_external, DW_FORM_flag, 1);
1310 Slot = SubprogramDie;
1312 // Add to context owner.
1313 Unit->getDie()->AddChild(SubprogramDie);
1315 // Expose as global.
1316 Unit->AddGlobal(Name, SubprogramDie);
1318 return SubprogramDie;
1321 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
1322 /// tools to recognize the object file contains Dwarf information.
1324 void DwarfWriter::EmitInitial() const {
1325 // Dwarf sections base addresses.
1326 Asm->SwitchSection(DwarfFrameSection, 0);
1327 EmitLabel("section_frame", 0);
1328 Asm->SwitchSection(DwarfInfoSection, 0);
1329 EmitLabel("section_info", 0);
1330 EmitLabel("info", 0);
1331 Asm->SwitchSection(DwarfAbbrevSection, 0);
1332 EmitLabel("section_abbrev", 0);
1333 EmitLabel("abbrev", 0);
1334 Asm->SwitchSection(DwarfARangesSection, 0);
1335 EmitLabel("section_aranges", 0);
1336 Asm->SwitchSection(DwarfMacInfoSection, 0);
1337 EmitLabel("section_macinfo", 0);
1338 Asm->SwitchSection(DwarfLineSection, 0);
1339 EmitLabel("section_line", 0);
1340 EmitLabel("line", 0);
1341 Asm->SwitchSection(DwarfLocSection, 0);
1342 EmitLabel("section_loc", 0);
1343 Asm->SwitchSection(DwarfPubNamesSection, 0);
1344 EmitLabel("section_pubnames", 0);
1345 Asm->SwitchSection(DwarfStrSection, 0);
1346 EmitLabel("section_str", 0);
1347 Asm->SwitchSection(DwarfRangesSection, 0);
1348 EmitLabel("section_ranges", 0);
1350 Asm->SwitchSection(TextSection, 0);
1351 EmitLabel("text_begin", 0);
1352 Asm->SwitchSection(DataSection, 0);
1353 EmitLabel("data_begin", 0);
1356 /// EmitDIE - Recusively Emits a debug information entry.
1358 void DwarfWriter::EmitDIE(DIE *Die) const {
1359 // Get the abbreviation for this DIE.
1360 unsigned AbbrevID = Die->getAbbrevID();
1361 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1365 // Emit the code (index) for the abbreviation.
1366 EmitULEB128Bytes(AbbrevID);
1367 EOL(std::string("Abbrev [" +
1369 "] 0x" + utohexstr(Die->getOffset()) +
1370 ":0x" + utohexstr(Die->getSize()) + " " +
1371 TagString(Abbrev.getTag())));
1373 const std::vector<DIEValue *> &Values = Die->getValues();
1374 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1376 // Emit the DIE attribute values.
1377 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1378 unsigned Attr = AbbrevData[i].getAttribute();
1379 unsigned Form = AbbrevData[i].getForm();
1380 assert(Form && "Too many attributes for DIE (check abbreviation)");
1383 case DW_AT_sibling: {
1384 EmitInt32(Die->SiblingOffset());
1388 // Emit an attribute using the defined form.
1389 Values[i]->EmitValue(*this, Form);
1394 EOL(AttributeString(Attr));
1397 // Emit the DIE children if any.
1398 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1399 const std::vector<DIE *> &Children = Die->getChildren();
1401 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1402 // FIXME - handle sibling offsets.
1403 // FIXME - handle all DIE types.
1404 EmitDIE(Children[j]);
1407 EmitInt8(0); EOL("End Of Children Mark");
1411 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1413 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1414 // Record the abbreviation.
1415 Die->Complete(*this);
1417 // Get the abbreviation for this DIE.
1418 unsigned AbbrevID = Die->getAbbrevID();
1419 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1422 Die->setOffset(Offset);
1424 // Start the size with the size of abbreviation code.
1425 Offset += SizeULEB128(AbbrevID);
1427 const std::vector<DIEValue *> &Values = Die->getValues();
1428 const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1430 // Emit the DIE attribute values.
1431 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1432 // Size attribute value.
1433 Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1436 // Emit the DIE children if any.
1437 if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1438 const std::vector<DIE *> &Children = Die->getChildren();
1440 for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1441 // FIXME - handle sibling offsets.
1442 // FIXME - handle all DIE types.
1443 Offset = SizeAndOffsetDie(Children[j], Offset);
1446 // End of children marker.
1447 Offset += sizeof(int8_t);
1450 Die->setSize(Offset - Die->getOffset());
1454 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1456 void DwarfWriter::SizeAndOffsets() {
1458 // Process each compile unit.
1459 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1460 CompileUnit *Unit = CompileUnits[i];
1461 if (Unit->hasContent()) {
1462 // Compute size of compile unit header
1463 unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1464 sizeof(int16_t) + // DWARF version number
1465 sizeof(int32_t) + // Offset Into Abbrev. Section
1466 sizeof(int8_t); // Pointer Size (in bytes)
1468 SizeAndOffsetDie(Unit->getDie(), Offset);
1473 /// EmitDebugInfo - Emit the debug info section.
1475 void DwarfWriter::EmitDebugInfo() const {
1476 // Start debug info section.
1477 Asm->SwitchSection(DwarfInfoSection, 0);
1479 // Process each compile unit.
1480 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1481 CompileUnit *Unit = CompileUnits[i];
1483 if (Unit->hasContent()) {
1484 DIE *Die = Unit->getDie();
1485 // Emit the compile units header.
1486 EmitLabel("info_begin", Unit->getID());
1487 // Emit size of content not including length itself
1488 unsigned ContentSize = Die->getSize() +
1489 sizeof(int16_t) + // DWARF version number
1490 sizeof(int32_t) + // Offset Into Abbrev. Section
1491 sizeof(int8_t); // Pointer Size (in bytes)
1493 EmitInt32(ContentSize); EOL("Length of Compilation Unit Info");
1494 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1495 EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1496 EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1499 EmitLabel("info_end", Unit->getID());
1506 /// EmitAbbreviations - Emit the abbreviation section.
1508 void DwarfWriter::EmitAbbreviations() const {
1509 // Check to see if it is worth the effort.
1510 if (!Abbreviations.empty()) {
1511 // Start the debug abbrev section.
1512 Asm->SwitchSection(DwarfAbbrevSection, 0);
1514 EmitLabel("abbrev_begin", 0);
1516 // For each abbrevation.
1517 for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1518 AbbrevID <= NAID; ++AbbrevID) {
1519 // Get abbreviation data
1520 const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1522 // Emit the abbrevations code (base 1 index.)
1523 EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1525 // Emit the abbreviations data.
1531 EmitLabel("abbrev_end", 0);
1537 /// EmitDebugLines - Emit source line information.
1539 void DwarfWriter::EmitDebugLines() const {
1540 // Minimum line delta, thus ranging from -10..(255-10).
1541 const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1542 // Maximum line delta, thus ranging from -10..(255-10).
1543 const int MaxLineDelta = 255 + MinLineDelta;
1545 // Start the dwarf line section.
1546 Asm->SwitchSection(DwarfLineSection, 0);
1548 // Construct the section header.
1550 EmitDifference("line_end", 0, "line_begin", 0);
1551 EOL("Length of Source Line Info");
1552 EmitLabel("line_begin", 0);
1554 EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1556 EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1557 EOL("Prolog Length");
1558 EmitLabel("line_prolog_begin", 0);
1560 EmitInt8(1); EOL("Minimum Instruction Length");
1562 EmitInt8(1); EOL("Default is_stmt_start flag");
1564 EmitInt8(MinLineDelta); EOL("Line Base Value (Special Opcodes)");
1566 EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1568 EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1570 // Line number standard opcode encodings argument count
1571 EmitInt8(0); EOL("DW_LNS_copy arg count");
1572 EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1573 EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1574 EmitInt8(1); EOL("DW_LNS_set_file arg count");
1575 EmitInt8(1); EOL("DW_LNS_set_column arg count");
1576 EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1577 EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1578 EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1579 EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1581 const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1582 const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1584 // Emit directories.
1585 for (unsigned DirectoryID = 1, NDID = Directories.size();
1586 DirectoryID <= NDID; ++DirectoryID) {
1587 EmitString(Directories[DirectoryID]); EOL("Directory");
1589 EmitInt8(0); EOL("End of directories");
1592 for (unsigned SourceID = 1, NSID = SourceFiles.size();
1593 SourceID <= NSID; ++SourceID) {
1594 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1595 EmitString(SourceFile.getName()); EOL("Source");
1596 EmitULEB128Bytes(SourceFile.getDirectoryID()); EOL("Directory #");
1597 EmitULEB128Bytes(0); EOL("Mod date");
1598 EmitULEB128Bytes(0); EOL("File size");
1600 EmitInt8(0); EOL("End of files");
1602 EmitLabel("line_prolog_end", 0);
1604 // Emit line information
1605 const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1607 // Dwarf assumes we start with first line of first source file.
1608 unsigned Source = 1;
1611 // Construct rows of the address, source, line, column matrix.
1612 for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1613 SourceLineInfo *LineInfo = LineInfos[i];
1616 unsigned SourceID = LineInfo->getSourceID();
1617 const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1618 unsigned DirectoryID = SourceFile.getDirectoryID();
1620 << Asm->CommentString << " "
1621 << Directories[DirectoryID]
1622 << SourceFile.getName() << ":"
1623 << LineInfo->getLine() << "\n";
1626 // Define the line address.
1627 EmitInt8(0); EOL("Extended Op");
1628 EmitInt8(4 + 1); EOL("Op size");
1629 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1630 EmitReference("loc", i + 1); EOL("Location label");
1632 // If change of source, then switch to the new source.
1633 if (Source != LineInfo->getSourceID()) {
1634 Source = LineInfo->getSourceID();
1635 EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1636 EmitULEB128Bytes(Source); EOL("New Source");
1639 // If change of line.
1640 if (Line != LineInfo->getLine()) {
1641 // Determine offset.
1642 int Offset = LineInfo->getLine() - Line;
1643 int Delta = Offset - MinLineDelta;
1646 Line = LineInfo->getLine();
1648 // If delta is small enough and in range...
1649 if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1650 // ... then use fast opcode.
1651 EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1653 // ... otherwise use long hand.
1654 EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1655 EmitSLEB128Bytes(Offset); EOL("Line Offset");
1656 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1659 // Copy the previous row (different address or source)
1660 EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1664 // Define last address.
1665 EmitInt8(0); EOL("Extended Op");
1666 EmitInt8(4 + 1); EOL("Op size");
1667 EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1668 EmitReference("text_end", 0); EOL("Location label");
1670 // Mark end of matrix.
1671 EmitInt8(0); EOL("DW_LNE_end_sequence");
1672 EmitULEB128Bytes(1); O << "\n";
1673 EmitInt8(1); O << "\n";
1675 EmitLabel("line_end", 0);
1680 /// EmitDebugFrame - Emit visible names into a debug frame section.
1682 void DwarfWriter::EmitDebugFrame() {
1683 // FIXME - Should be per frame
1686 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1688 void DwarfWriter::EmitDebugPubNames() {
1689 // Start the dwarf pubnames section.
1690 Asm->SwitchSection(DwarfPubNamesSection, 0);
1692 // Process each compile unit.
1693 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1694 CompileUnit *Unit = CompileUnits[i];
1696 if (Unit->hasContent()) {
1697 EmitDifference("pubnames_end", Unit->getID(),
1698 "pubnames_begin", Unit->getID());
1699 EOL("Length of Public Names Info");
1701 EmitLabel("pubnames_begin", Unit->getID());
1703 EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1705 EmitReference("info_begin", Unit->getID());
1706 EOL("Offset of Compilation Unit Info");
1708 EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
1709 EOL("Compilation Unit Length");
1711 std::map<std::string, DIE *> &Globals = Unit->getGlobals();
1713 for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
1716 const std::string &Name = GI->first;
1717 DIE * Entity = GI->second;
1719 EmitInt32(Entity->getOffset()); EOL("DIE offset");
1720 EmitString(Name); EOL("External Name");
1723 EmitInt32(0); EOL("End Mark");
1724 EmitLabel("pubnames_end", Unit->getID());
1731 /// EmitDebugStr - Emit visible names into a debug str section.
1733 void DwarfWriter::EmitDebugStr() {
1734 // Check to see if it is worth the effort.
1735 if (!StringPool.empty()) {
1736 // Start the dwarf str section.
1737 Asm->SwitchSection(DwarfStrSection, 0);
1739 // For each of strings in teh string pool.
1740 for (unsigned StringID = 1, N = StringPool.size();
1741 StringID <= N; ++StringID) {
1742 // Emit a label for reference from debug information entries.
1743 EmitLabel("string", StringID);
1744 // Emit the string itself.
1745 const std::string &String = StringPool[StringID];
1746 EmitString(String); O << "\n";
1753 /// EmitDebugLoc - Emit visible names into a debug loc section.
1755 void DwarfWriter::EmitDebugLoc() {
1756 // Start the dwarf loc section.
1757 Asm->SwitchSection(DwarfLocSection, 0);
1762 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1764 void DwarfWriter::EmitDebugARanges() {
1765 // Start the dwarf aranges section.
1766 Asm->SwitchSection(DwarfARangesSection, 0);
1770 // Process each compile unit.
1771 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1772 CompileUnit *Unit = CompileUnits[i];
1774 if (Unit->hasContent()) {
1775 // Don't include size of length
1776 EmitInt32(0x1c); EOL("Length of Address Ranges Info");
1778 EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
1780 EmitReference("info_begin", Unit->getID());
1781 EOL("Offset of Compilation Unit Info");
1783 EmitInt8(AddressSize); EOL("Size of Address");
1785 EmitInt8(0); EOL("Size of Segment Descriptor");
1787 EmitInt16(0); EOL("Pad (1)");
1788 EmitInt16(0); EOL("Pad (2)");
1791 EmitReference("text_begin", 0); EOL("Address");
1792 EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
1794 EmitInt32(0); EOL("EOM (1)");
1795 EmitInt32(0); EOL("EOM (2)");
1803 /// EmitDebugRanges - Emit visible names into a debug ranges section.
1805 void DwarfWriter::EmitDebugRanges() {
1806 // Start the dwarf ranges section.
1807 Asm->SwitchSection(DwarfRangesSection, 0);
1812 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1814 void DwarfWriter::EmitDebugMacInfo() {
1815 // Start the dwarf macinfo section.
1816 Asm->SwitchSection(DwarfMacInfoSection, 0);
1821 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1823 void DwarfWriter::ConstructCompileUnitDIEs() {
1824 const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
1826 for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
1827 CompileUnit *Unit = NewCompileUnit(CUW[i], i);
1828 CompileUnits.push_back(Unit);
1832 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1834 void DwarfWriter::ConstructGlobalDIEs(Module &M) {
1835 std::vector<GlobalVariableDesc *> GlobalVariables =
1836 DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
1838 for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
1839 GlobalVariableDesc *GVD = GlobalVariables[i];
1840 NewGlobalVariable(GVD);
1844 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
1846 void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
1847 std::vector<SubprogramDesc *> Subprograms =
1848 DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
1850 for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
1851 SubprogramDesc *SPD = Subprograms[i];
1856 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
1858 bool DwarfWriter::ShouldEmitDwarf() {
1859 // Check if debug info is present.
1860 if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1862 // Make sure initial declarations are made.
1872 //===----------------------------------------------------------------------===//
1873 // Main entry points.
1876 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
1887 , AddressSize(sizeof(int32_t))
1892 , DwarfAbbrevSection(".debug_abbrev")
1893 , DwarfInfoSection(".debug_info")
1894 , DwarfLineSection(".debug_line")
1895 , DwarfFrameSection(".debug_frame")
1896 , DwarfPubNamesSection(".debug_pubnames")
1897 , DwarfPubTypesSection(".debug_pubtypes")
1898 , DwarfStrSection(".debug_str")
1899 , DwarfLocSection(".debug_loc")
1900 , DwarfARangesSection(".debug_aranges")
1901 , DwarfRangesSection(".debug_ranges")
1902 , DwarfMacInfoSection(".debug_macinfo")
1903 , TextSection(".text")
1904 , DataSection(".data")
1906 DwarfWriter::~DwarfWriter() {
1907 for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1908 delete CompileUnits[i];
1912 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
1914 void DwarfWriter::BeginModule(Module &M) {
1915 if (!ShouldEmitDwarf()) return;
1916 EOL("Dwarf Begin Module");
1919 /// EndModule - Emit all Dwarf sections that should come after the content.
1921 void DwarfWriter::EndModule(Module &M) {
1922 if (!ShouldEmitDwarf()) return;
1923 EOL("Dwarf End Module");
1925 // Standard sections final addresses.
1926 Asm->SwitchSection(TextSection, 0);
1927 EmitLabel("text_end", 0);
1928 Asm->SwitchSection(DataSection, 0);
1929 EmitLabel("data_end", 0);
1931 // Create all the compile unit DIEs.
1932 ConstructCompileUnitDIEs();
1934 // Create DIEs for each of the externally visible global variables.
1935 ConstructGlobalDIEs(M);
1937 // Create DIEs for each of the externally visible subprograms.
1938 ConstructSubprogramDIEs(M);
1940 // Compute DIE offsets and sizes.
1943 // Emit all the DIEs into a debug info section
1946 // Corresponding abbreviations into a abbrev section.
1947 EmitAbbreviations();
1949 // Emit source line correspondence into a debug line section.
1952 // Emit info into a debug frame section.
1953 // EmitDebugFrame();
1955 // Emit info into a debug pubnames section.
1956 EmitDebugPubNames();
1958 // Emit info into a debug str section.
1961 // Emit info into a debug loc section.
1964 // Emit info into a debug aranges section.
1967 // Emit info into a debug ranges section.
1970 // Emit info into a debug macinfo section.
1974 /// BeginFunction - Gather pre-function debug information.
1976 void DwarfWriter::BeginFunction(MachineFunction &MF) {
1977 if (!ShouldEmitDwarf()) return;
1978 EOL("Dwarf Begin Function");
1981 /// EndFunction - Gather and emit post-function debug information.
1983 void DwarfWriter::EndFunction(MachineFunction &MF) {
1984 if (!ShouldEmitDwarf()) return;
1985 EOL("Dwarf End Function");