Basic array support.
[oota-llvm.git] / lib / CodeGen / DwarfWriter.cpp
1 //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf debug info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/DwarfWriter.h"
15
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"
25
26 #include <iostream>
27
28 using namespace llvm;
29 using namespace llvm::dwarf;
30
31 static cl::opt<bool>
32 DwarfVerbose("dwarf-verbose", cl::Hidden,
33                                 cl::desc("Add comments to Dwarf directives."));
34
35 namespace llvm {
36
37 //===----------------------------------------------------------------------===//
38 // Forward declarations.
39 //
40 class CompileUnit;
41 class DIE;
42
43 //===----------------------------------------------------------------------===//
44 class CompileUnit {
45 private:
46   CompileUnitDesc *Desc;                // Compile unit debug descriptor.
47   unsigned ID;                          // File ID for source.
48   DIE *Die;                             // Compile unit die.
49   std::map<std::string, DIE *> Globals; // A map of globally visible named
50                                         // entities for this unit.
51
52 public:
53   CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
54   : Desc(CUD)
55   , ID(I)
56   , Die(D)
57   , Globals()
58   {}
59   
60   ~CompileUnit();
61   
62   // Accessors.
63   CompileUnitDesc *getDesc() const { return Desc; }
64   unsigned getID()           const { return ID; }
65   DIE* getDie()              const { return Die; }
66   std::map<std::string, DIE *> &getGlobals() { return Globals; }
67   
68   /// hasContent - Return true if this compile unit has something to write out.
69   ///
70   bool hasContent() const;
71   
72   /// AddGlobal - Add a new global entity to the compile unit.
73   ///
74   void AddGlobal(const std::string &Name, DIE *Die);
75   
76 };
77
78 //===----------------------------------------------------------------------===//
79 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
80 // Dwarf abbreviation.
81 class DIEAbbrevData {
82 private:
83   unsigned Attribute;                 // Dwarf attribute code.
84   unsigned Form;                      // Dwarf form code.
85   
86 public:
87   DIEAbbrevData(unsigned A, unsigned F)
88   : Attribute(A)
89   , Form(F)
90   {}
91   
92   // Accessors.
93   unsigned getAttribute() const { return Attribute; }
94   unsigned getForm()      const { return Form; }
95   
96   /// operator== - Used by DIEAbbrev to locate entry.
97   ///
98   bool operator==(const DIEAbbrevData &DAD) const {
99     return Attribute == DAD.Attribute && Form == DAD.Form;
100   }
101
102   /// operator!= - Used by DIEAbbrev to locate entry.
103   ///
104   bool operator!=(const DIEAbbrevData &DAD) const {
105     return Attribute != DAD.Attribute || Form != DAD.Form;
106   }
107   
108   /// operator< - Used by DIEAbbrev to locate entry.
109   ///
110   bool operator<(const DIEAbbrevData &DAD) const {
111     return Attribute < DAD.Attribute ||
112           (Attribute == DAD.Attribute && Form < DAD.Form);
113   }
114 };
115
116 //===----------------------------------------------------------------------===//
117 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
118 // information object.
119 class DIEAbbrev {
120 private:
121   unsigned Tag;                       // Dwarf tag code.
122   unsigned ChildrenFlag;              // Dwarf children flag.
123   std::vector<DIEAbbrevData> Data;    // Raw data bytes for abbreviation.
124
125 public:
126
127   DIEAbbrev(unsigned T, unsigned C)
128   : Tag(T)
129   , ChildrenFlag(C)
130   , Data()
131   {}
132   ~DIEAbbrev() {}
133   
134   // Accessors.
135   unsigned getTag()                           const { return Tag; }
136   unsigned getChildrenFlag()                  const { return ChildrenFlag; }
137   const std::vector<DIEAbbrevData> &getData() const { return Data; }
138   void setChildrenFlag(unsigned CF)                 { ChildrenFlag = CF; }
139
140   /// operator== - Used by UniqueVector to locate entry.
141   ///
142   bool operator==(const DIEAbbrev &DA) const;
143
144   /// operator< - Used by UniqueVector to locate entry.
145   ///
146   bool operator<(const DIEAbbrev &DA) const;
147
148   /// AddAttribute - Adds another set of attribute information to the
149   /// abbreviation.
150   void AddAttribute(unsigned Attribute, unsigned Form) {
151     Data.push_back(DIEAbbrevData(Attribute, Form));
152   }
153   
154   /// Emit - Print the abbreviation using the specified Dwarf writer.
155   ///
156   void Emit(const DwarfWriter &DW) const; 
157       
158 #ifndef NDEBUG
159   void print(std::ostream &O);
160   void dump();
161 #endif
162 };
163
164 //===----------------------------------------------------------------------===//
165 // DIEValue - A debug information entry value.
166 //
167 class DIEValue {
168 public:
169   enum {
170     isInteger,
171     isString,
172     isLabel,
173     isAsIsLabel,
174     isDelta,
175     isEntry
176   };
177   
178   unsigned Type;                      // Type of the value
179   
180   DIEValue(unsigned T) : Type(T) {}
181   virtual ~DIEValue() {}
182   
183   // Implement isa/cast/dyncast.
184   static bool classof(const DIEValue *) { return true; }
185   
186   /// EmitValue - Emit value via the Dwarf writer.
187   ///
188   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
189   
190   /// SizeOf - Return the size of a value in bytes.
191   ///
192   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
193 };
194
195 //===----------------------------------------------------------------------===//
196 // DWInteger - An integer value DIE.
197 // 
198 class DIEInteger : public DIEValue {
199 private:
200   uint64_t Integer;
201   
202 public:
203   DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
204
205   // Implement isa/cast/dyncast.
206   static bool classof(const DIEInteger *) { return true; }
207   static bool classof(const DIEValue *I)  { return I->Type == isInteger; }
208   
209   /// EmitValue - Emit integer of appropriate size.
210   ///
211   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
212   
213   /// SizeOf - Determine size of integer value in bytes.
214   ///
215   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
216 };
217
218 //===----------------------------------------------------------------------===//
219 // DIEString - A string value DIE.
220 // 
221 struct DIEString : public DIEValue {
222   const std::string String;
223   
224   DIEString(const std::string &S) : DIEValue(isString), String(S) {}
225
226   // Implement isa/cast/dyncast.
227   static bool classof(const DIEString *) { return true; }
228   static bool classof(const DIEValue *S) { return S->Type == isString; }
229   
230   /// EmitValue - Emit string value.
231   ///
232   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
233   
234   /// SizeOf - Determine size of string value in bytes.
235   ///
236   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
237 };
238
239 //===----------------------------------------------------------------------===//
240 // DIEDwarfLabel - A Dwarf internal label expression DIE.
241 //
242 struct DIEDwarfLabel : public DIEValue {
243   const DWLabel Label;
244   
245   DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
246
247   // Implement isa/cast/dyncast.
248   static bool classof(const DIEDwarfLabel *)  { return true; }
249   static bool classof(const DIEValue *L) { return L->Type == isLabel; }
250   
251   /// EmitValue - Emit label value.
252   ///
253   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
254   
255   /// SizeOf - Determine size of label value in bytes.
256   ///
257   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
258 };
259
260
261 //===----------------------------------------------------------------------===//
262 // DIEObjectLabel - A label to an object in code or data.
263 //
264 struct DIEObjectLabel : public DIEValue {
265   const std::string Label;
266   
267   DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
268
269   // Implement isa/cast/dyncast.
270   static bool classof(const DIEObjectLabel *) { return true; }
271   static bool classof(const DIEValue *L)    { return L->Type == isAsIsLabel; }
272   
273   /// EmitValue - Emit label value.
274   ///
275   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
276   
277   /// SizeOf - Determine size of label value in bytes.
278   ///
279   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
280 };
281
282 //===----------------------------------------------------------------------===//
283 // DIEDelta - A simple label difference DIE.
284 // 
285 struct DIEDelta : public DIEValue {
286   const DWLabel LabelHi;
287   const DWLabel LabelLo;
288   
289   DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
290   : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
291
292   // Implement isa/cast/dyncast.
293   static bool classof(const DIEDelta *)  { return true; }
294   static bool classof(const DIEValue *D) { return D->Type == isDelta; }
295   
296   /// EmitValue - Emit delta value.
297   ///
298   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
299   
300   /// SizeOf - Determine size of delta value in bytes.
301   ///
302   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
303 };
304
305 //===----------------------------------------------------------------------===//
306 // DIEntry - A pointer to a debug information entry.
307 // 
308 struct DIEntry : public DIEValue {
309   DIE *Entry;
310   
311   DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
312
313   // Implement isa/cast/dyncast.
314   static bool classof(const DIEntry *)   { return true; }
315   static bool classof(const DIEValue *E) { return E->Type == isEntry; }
316   
317   /// EmitValue - Emit delta value.
318   ///
319   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
320   
321   /// SizeOf - Determine size of delta value in bytes.
322   ///
323   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
324 };
325
326 //===----------------------------------------------------------------------===//
327 // DIE - A structured debug information entry.  Has an abbreviation which
328 // describes it's organization.
329 class DIE {
330 private:
331   DIEAbbrev *Abbrev;                    // Temporary buffer for abbreviation.
332   unsigned AbbrevID;                    // Decribing abbreviation ID.
333   unsigned Offset;                      // Offset in debug info section.
334   unsigned Size;                        // Size of instance + children.
335   std::vector<DIE *> Children;          // Children DIEs.
336   std::vector<DIEValue *> Values;       // Attributes values.
337   
338 public:
339   DIE(unsigned Tag);
340   ~DIE();
341   
342   // Accessors.
343   unsigned   getAbbrevID()                   const { return AbbrevID; }
344   unsigned   getOffset()                     const { return Offset; }
345   unsigned   getSize()                       const { return Size; }
346   const std::vector<DIE *> &getChildren()    const { return Children; }
347   const std::vector<DIEValue *> &getValues() const { return Values; }
348   void setOffset(unsigned O)                 { Offset = O; }
349   void setSize(unsigned S)                   { Size = S; }
350   
351   /// SiblingOffset - Return the offset of the debug information entry's
352   /// sibling.
353   unsigned SiblingOffset() const { return Offset + Size; }
354
355   /// AddUInt - Add an unsigned integer attribute data and value.
356   ///
357   void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
358
359   /// AddSInt - Add an signed integer attribute data and value.
360   ///
361   void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
362       
363   /// AddString - Add a std::string attribute data and value.
364   ///
365   void AddString(unsigned Attribute, unsigned Form,
366                  const std::string &String);
367       
368   /// AddLabel - Add a Dwarf label attribute data and value.
369   ///
370   void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
371       
372   /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
373   ///
374   void AddObjectLabel(unsigned Attribute, unsigned Form,
375                       const std::string &Label);
376       
377   /// AddDelta - Add a label delta attribute data and value.
378   ///
379   void AddDelta(unsigned Attribute, unsigned Form,
380                 const DWLabel &Hi, const DWLabel &Lo);
381       
382   ///  AddDIEntry - Add a DIE attribute data and value.
383   ///
384   void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
385
386   /// Complete - Indicate that all attributes have been added and
387   /// ready to get an abbreviation ID.
388   ///
389   void Complete(DwarfWriter &DW);
390   
391   /// AddChild - Add a child to the DIE.
392   void AddChild(DIE *Child);
393 };
394
395 } // End of namespace llvm
396
397 //===----------------------------------------------------------------------===//
398
399 CompileUnit::~CompileUnit() {
400   delete Die;
401 }
402
403 /// hasContent - Return true if this compile unit has something to write out.
404 ///
405 bool CompileUnit::hasContent() const {
406   return !Die->getChildren().empty();
407 }
408
409 /// AddGlobal - Add a new global entity to the compile unit.
410 ///
411 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
412   Globals[Name] = Die;
413 }
414
415 //===----------------------------------------------------------------------===//
416
417 /// operator== - Used by UniqueVector to locate entry.
418 ///
419 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
420   if (Tag != DA.Tag) return false;
421   if (ChildrenFlag != DA.ChildrenFlag) return false;
422   if (Data.size() != DA.Data.size()) return false;
423   
424   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
425     if (Data[i] != DA.Data[i]) return false;
426   }
427   
428   return true;
429 }
430
431 /// operator< - Used by UniqueVector to locate entry.
432 ///
433 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
434   if (Tag != DA.Tag) return Tag < DA.Tag;
435   if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
436   if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
437   
438   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
439     if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
440   }
441   
442   return false;
443 }
444     
445 /// Emit - Print the abbreviation using the specified Dwarf writer.
446 ///
447 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
448   // Emit its Dwarf tag type.
449   DW.EmitULEB128Bytes(Tag);
450   DW.EOL(TagString(Tag));
451   
452   // Emit whether it has children DIEs.
453   DW.EmitULEB128Bytes(ChildrenFlag);
454   DW.EOL(ChildrenString(ChildrenFlag));
455   
456   // For each attribute description.
457   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
458     const DIEAbbrevData &AttrData = Data[i];
459     
460     // Emit attribute type.
461     DW.EmitULEB128Bytes(AttrData.getAttribute());
462     DW.EOL(AttributeString(AttrData.getAttribute()));
463     
464     // Emit form type.
465     DW.EmitULEB128Bytes(AttrData.getForm());
466     DW.EOL(FormEncodingString(AttrData.getForm()));
467   }
468
469   // Mark end of abbreviation.
470   DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
471   DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
472 }
473
474 #ifndef NDEBUG
475   void DIEAbbrev::print(std::ostream &O) {
476     O << "Abbreviation @"
477       << std::hex << (intptr_t)this << std::dec
478       << "  "
479       << TagString(Tag)
480       << " "
481       << ChildrenString(ChildrenFlag)
482       << "\n";
483     
484     for (unsigned i = 0, N = Data.size(); i < N; ++i) {
485       O << "  "
486         << AttributeString(Data[i].getAttribute())
487         << "  "
488         << FormEncodingString(Data[i].getForm())
489         << "\n";
490     }
491   }
492   void DIEAbbrev::dump() { print(std::cerr); }
493 #endif
494
495 //===----------------------------------------------------------------------===//
496
497 /// EmitValue - Emit integer of appropriate size.
498 ///
499 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
500   switch (Form) {
501   case DW_FORM_flag:  // Fall thru
502   case DW_FORM_data1: DW.EmitInt8(Integer);         break;
503   case DW_FORM_data2: DW.EmitInt16(Integer);        break;
504   case DW_FORM_data4: DW.EmitInt32(Integer);        break;
505   case DW_FORM_data8: DW.EmitInt64(Integer);        break;
506   case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
507   case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
508   default: assert(0 && "DIE Value form not supported yet"); break;
509   }
510 }
511
512 /// SizeOf - Determine size of integer value in bytes.
513 ///
514 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
515   switch (Form) {
516   case DW_FORM_flag:  // Fall thru
517   case DW_FORM_data1: return sizeof(int8_t);
518   case DW_FORM_data2: return sizeof(int16_t);
519   case DW_FORM_data4: return sizeof(int32_t);
520   case DW_FORM_data8: return sizeof(int64_t);
521   case DW_FORM_udata: return DW.SizeULEB128(Integer);
522   case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
523   default: assert(0 && "DIE Value form not supported yet"); break;
524   }
525   return 0;
526 }
527
528 //===----------------------------------------------------------------------===//
529
530 /// EmitValue - Emit string value.
531 ///
532 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
533   DW.EmitString(String);
534 }
535
536 /// SizeOf - Determine size of string value in bytes.
537 ///
538 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
539   return String.size() + sizeof(char); // sizeof('\0');
540 }
541
542 //===----------------------------------------------------------------------===//
543
544 /// EmitValue - Emit label value.
545 ///
546 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
547   DW.EmitReference(Label);
548 }
549
550 /// SizeOf - Determine size of label value in bytes.
551 ///
552 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
553   return DW.getAddressSize();
554 }
555     
556 //===----------------------------------------------------------------------===//
557
558 /// EmitValue - Emit label value.
559 ///
560 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
561   DW.EmitInt8(sizeof(int8_t) + DW.getAddressSize());
562   DW.EOL("DW_FORM_block1 length");
563   
564   DW.EmitInt8(DW_OP_addr);
565   DW.EOL("DW_OP_addr");
566   
567   DW.EmitReference(Label);
568 }
569
570 /// SizeOf - Determine size of label value in bytes.
571 ///
572 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
573   return sizeof(int8_t) + sizeof(int8_t) + DW.getAddressSize();
574 }
575     
576 //===----------------------------------------------------------------------===//
577
578 /// EmitValue - Emit delta value.
579 ///
580 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
581   DW.EmitDifference(LabelHi, LabelLo);
582 }
583
584 /// SizeOf - Determine size of delta value in bytes.
585 ///
586 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
587   return DW.getAddressSize();
588 }
589
590 //===----------------------------------------------------------------------===//
591 /// EmitValue - Emit extry offset.
592 ///
593 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
594   DW.EmitInt32(Entry->getOffset());
595 }
596
597 /// SizeOf - Determine size of label value in bytes.
598 ///
599 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
600   return sizeof(int32_t);
601 }
602     
603 //===----------------------------------------------------------------------===//
604
605 DIE::DIE(unsigned Tag)
606 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
607 , AbbrevID(0)
608 , Offset(0)
609 , Size(0)
610 , Children()
611 , Values()
612 {}
613
614 DIE::~DIE() {
615   if (Abbrev) delete Abbrev;
616   
617   for (unsigned i = 0, N = Children.size(); i < N; ++i) {
618     delete Children[i];
619   }
620
621   for (unsigned j = 0, M = Values.size(); j < M; ++j) {
622     delete Values[j];
623   }
624 }
625     
626 /// AddUInt - Add an unsigned integer attribute data and value.
627 ///
628 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
629   if (Form == 0) {
630       if ((unsigned char)Integer == Integer)       Form = DW_FORM_data1;
631       else if ((unsigned short)Integer == Integer) Form = DW_FORM_data2;
632       else if ((unsigned int)Integer == Integer)   Form = DW_FORM_data4;
633       else                                         Form = DW_FORM_data8;
634   }
635   Abbrev->AddAttribute(Attribute, Form);
636   Values.push_back(new DIEInteger(Integer));
637 }
638     
639 /// AddSInt - Add an signed integer attribute data and value.
640 ///
641 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
642   if (Form == 0) {
643       if ((char)Integer == Integer)       Form = DW_FORM_data1;
644       else if ((short)Integer == Integer) Form = DW_FORM_data2;
645       else if ((int)Integer == Integer)   Form = DW_FORM_data4;
646       else                                Form = DW_FORM_data8;
647   }
648   Abbrev->AddAttribute(Attribute, Form);
649   Values.push_back(new DIEInteger(Integer));
650 }
651     
652 /// AddString - Add a std::string attribute data and value.
653 ///
654 void DIE::AddString(unsigned Attribute, unsigned Form,
655                     const std::string &String) {
656   Abbrev->AddAttribute(Attribute, Form);
657   Values.push_back(new DIEString(String));
658 }
659     
660 /// AddLabel - Add a Dwarf label attribute data and value.
661 ///
662 void DIE::AddLabel(unsigned Attribute, unsigned Form,
663                    const DWLabel &Label) {
664   Abbrev->AddAttribute(Attribute, Form);
665   Values.push_back(new DIEDwarfLabel(Label));
666 }
667     
668 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
669 ///
670 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
671                          const std::string &Label) {
672   Abbrev->AddAttribute(Attribute, Form);
673   Values.push_back(new DIEObjectLabel(Label));
674 }
675     
676 /// AddDelta - Add a label delta attribute data and value.
677 ///
678 void DIE::AddDelta(unsigned Attribute, unsigned Form,
679                    const DWLabel &Hi, const DWLabel &Lo) {
680   Abbrev->AddAttribute(Attribute, Form);
681   Values.push_back(new DIEDelta(Hi, Lo));
682 }
683     
684 /// AddDIEntry - Add a DIE attribute data and value.
685 ///
686 void DIE::AddDIEntry(unsigned Attribute,
687                      unsigned Form, DIE *Entry) {
688   Abbrev->AddAttribute(Attribute, Form);
689   Values.push_back(new DIEntry(Entry));
690 }
691
692 /// Complete - Indicate that all attributes have been added and ready to get an
693 /// abbreviation ID.
694 void DIE::Complete(DwarfWriter &DW) {
695   AbbrevID = DW.NewAbbreviation(Abbrev);
696   delete Abbrev;
697   Abbrev = NULL;
698 }
699
700 /// AddChild - Add a child to the DIE.
701 ///
702 void DIE::AddChild(DIE *Child) {
703   assert(Abbrev && "Adding children without an abbreviation");
704   Abbrev->setChildrenFlag(DW_CHILDREN_yes);
705   Children.push_back(Child);
706 }
707
708 //===----------------------------------------------------------------------===//
709
710 /// DWContext
711
712 //===----------------------------------------------------------------------===//
713
714 /// PrintHex - Print a value as a hexidecimal value.
715 ///
716 void DwarfWriter::PrintHex(int Value) const { 
717   O << "0x" << std::hex << Value << std::dec;
718 }
719
720 /// EOL - Print a newline character to asm stream.  If a comment is present
721 /// then it will be printed first.  Comments should not contain '\n'.
722 void DwarfWriter::EOL(const std::string &Comment) const {
723   if (DwarfVerbose) {
724     O << "\t"
725       << Asm->CommentString
726       << " "
727       << Comment;
728   }
729   O << "\n";
730 }
731
732 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
733 /// unsigned leb128 value.
734 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
735   if (hasLEB128) {
736     O << "\t.uleb128\t"
737       << Value;
738   } else {
739     O << Asm->Data8bitsDirective;
740     PrintULEB128(Value);
741   }
742 }
743
744 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
745 /// signed leb128 value.
746 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
747   if (hasLEB128) {
748     O << "\t.sleb128\t"
749       << Value;
750   } else {
751     O << Asm->Data8bitsDirective;
752     PrintSLEB128(Value);
753   }
754 }
755
756 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
757 /// representing an unsigned leb128 value.
758 void DwarfWriter::PrintULEB128(unsigned Value) const {
759   do {
760     unsigned Byte = Value & 0x7f;
761     Value >>= 7;
762     if (Value) Byte |= 0x80;
763     PrintHex(Byte);
764     if (Value) O << ", ";
765   } while (Value);
766 }
767
768 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
769 /// value.
770 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
771   unsigned Size = 0;
772   do {
773     Value >>= 7;
774     Size += sizeof(int8_t);
775   } while (Value);
776   return Size;
777 }
778
779 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
780 /// representing a signed leb128 value.
781 void DwarfWriter::PrintSLEB128(int Value) const {
782   int Sign = Value >> (8 * sizeof(Value) - 1);
783   bool IsMore;
784   
785   do {
786     unsigned Byte = Value & 0x7f;
787     Value >>= 7;
788     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
789     if (IsMore) Byte |= 0x80;
790     PrintHex(Byte);
791     if (IsMore) O << ", ";
792   } while (IsMore);
793 }
794
795 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
796 /// value.
797 unsigned DwarfWriter::SizeSLEB128(int Value) {
798   unsigned Size = 0;
799   int Sign = Value >> (8 * sizeof(Value) - 1);
800   bool IsMore;
801   
802   do {
803     unsigned Byte = Value & 0x7f;
804     Value >>= 7;
805     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
806     Size += sizeof(int8_t);
807   } while (IsMore);
808   return Size;
809 }
810
811 /// EmitInt8 - Emit a byte directive and value.
812 ///
813 void DwarfWriter::EmitInt8(int Value) const {
814   O << Asm->Data8bitsDirective;
815   PrintHex(Value & 0xFF);
816 }
817
818 /// EmitInt16 - Emit a short directive and value.
819 ///
820 void DwarfWriter::EmitInt16(int Value) const {
821   O << Asm->Data16bitsDirective;
822   PrintHex(Value & 0xFFFF);
823 }
824
825 /// EmitInt32 - Emit a long directive and value.
826 ///
827 void DwarfWriter::EmitInt32(int Value) const {
828   O << Asm->Data32bitsDirective;
829   PrintHex(Value);
830 }
831
832 /// EmitInt64 - Emit a long long directive and value.
833 ///
834 void DwarfWriter::EmitInt64(uint64_t Value) const {
835   if (Asm->Data64bitsDirective) {
836     O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
837   } else {
838     const TargetData &TD = Asm->TM.getTargetData();
839     
840     if (TD.isBigEndian()) {
841       EmitInt32(unsigned(Value >> 32)); O << "\n";
842       EmitInt32(unsigned(Value));
843     } else {
844       EmitInt32(unsigned(Value)); O << "\n";
845       EmitInt32(unsigned(Value >> 32));
846     }
847   }
848 }
849
850 /// EmitString - Emit a string with quotes and a null terminator.
851 /// Special characters are emitted properly. (Eg. '\t')
852 void DwarfWriter::EmitString(const std::string &String) const {
853   O << Asm->AsciiDirective
854     << "\"";
855   for (unsigned i = 0, N = String.size(); i < N; ++i) {
856     unsigned char C = String[i];
857     
858     if (!isascii(C) || iscntrl(C)) {
859       switch(C) {
860       case '\b': O << "\\b"; break;
861       case '\f': O << "\\f"; break;
862       case '\n': O << "\\n"; break;
863       case '\r': O << "\\r"; break;
864       case '\t': O << "\\t"; break;
865       default:
866         O << '\\';
867         O << char('0' + (C >> 6));
868         O << char('0' + (C >> 3));
869         O << char('0' + (C >> 0));
870         break;
871       }
872     } else if (C == '\"') {
873       O << "\\\"";
874     } else if (C == '\'') {
875       O << "\\\'";
876     } else {
877      O << C;
878     }
879   }
880   O << "\\0\"";
881 }
882
883 /// PrintLabelName - Print label name in form used by Dwarf writer.
884 ///
885 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
886   O << Asm->PrivateGlobalPrefix
887     << "debug_"
888     << Tag;
889   if (Number) O << Number;
890 }
891
892 /// EmitLabel - Emit location label for internal use by Dwarf.
893 ///
894 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
895   PrintLabelName(Tag, Number);
896   O << ":\n";
897 }
898
899 /// EmitReference - Emit a reference to a label.
900 ///
901 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
902   if (AddressSize == 4)
903     O << Asm->Data32bitsDirective;
904   else
905     O << Asm->Data64bitsDirective;
906     
907   PrintLabelName(Tag, Number);
908 }
909 void DwarfWriter::EmitReference(const std::string &Name) const {
910   if (AddressSize == 4)
911     O << Asm->Data32bitsDirective;
912   else
913     O << Asm->Data64bitsDirective;
914     
915   O << Name;
916 }
917
918 /// EmitDifference - Emit an label difference as sizeof(pointer) value.  Some
919 /// assemblers do not accept absolute expressions with data directives, so there 
920 /// is an option (needsSet) to use an intermediary 'set' expression.
921 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
922                                  const char *TagLo, unsigned NumberLo) const {
923   if (needsSet) {
924     static unsigned SetCounter = 0;
925     
926     O << "\t.set\t";
927     PrintLabelName("set", SetCounter);
928     O << ",";
929     PrintLabelName(TagHi, NumberHi);
930     O << "-";
931     PrintLabelName(TagLo, NumberLo);
932     O << "\n";
933     
934     if (AddressSize == sizeof(int32_t))
935       O << Asm->Data32bitsDirective;
936     else
937       O << Asm->Data64bitsDirective;
938       
939     PrintLabelName("set", SetCounter);
940     
941     ++SetCounter;
942   } else {
943     if (AddressSize == sizeof(int32_t))
944       O << Asm->Data32bitsDirective;
945     else
946       O << Asm->Data64bitsDirective;
947       
948     PrintLabelName(TagHi, NumberHi);
949     O << "-";
950     PrintLabelName(TagLo, NumberLo);
951   }
952 }
953
954 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
955 ///  
956 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
957   return Abbreviations.insert(*Abbrev);
958 }
959
960 /// NewString - Add a string to the constant pool and returns a label.
961 ///
962 DWLabel DwarfWriter::NewString(const std::string &String) {
963   unsigned StringID = StringPool.insert(String);
964   return DWLabel("string", StringID);
965 }
966
967 /// NewBasicType - Creates a new basic type if necessary, then adds to the
968 /// owner.
969 /// FIXME - Should never be needed.
970 DIE *DwarfWriter::NewBasicType(CompileUnit *Unit, Type *Ty) {
971   DIE *&Slot = TypeToDieMap[Ty];
972   if (Slot) return Slot;
973   
974   const char *Name;
975   unsigned Size;
976   unsigned Encoding = 0;
977   
978   switch (Ty->getTypeID()) {
979   case Type::UByteTyID:
980     Name = "unsigned char";
981     Size = 1;
982     Encoding = DW_ATE_unsigned_char;
983     break;
984   case Type::SByteTyID:
985     Name = "char";
986     Size = 1;
987     Encoding = DW_ATE_signed_char;
988     break;
989   case Type::UShortTyID:
990     Name = "unsigned short";
991     Size = 2;
992     Encoding = DW_ATE_unsigned;
993     break;
994   case Type::ShortTyID:
995     Name = "short";
996     Size = 2;
997     Encoding = DW_ATE_signed;
998     break;
999   case Type::UIntTyID:
1000     Name = "unsigned int";
1001     Size = 4;
1002     Encoding = DW_ATE_unsigned;
1003     break;
1004   case Type::IntTyID:
1005     Name = "int";
1006     Size = 4;
1007     Encoding = DW_ATE_signed;
1008     break;
1009   case Type::ULongTyID:
1010     Name = "unsigned long long";
1011     Size = 7;
1012     Encoding = DW_ATE_unsigned;
1013     break;
1014   case Type::LongTyID:
1015     Name = "long long";
1016     Size = 7;
1017     Encoding = DW_ATE_signed;
1018     break;
1019   case Type::FloatTyID:
1020     Name = "float";
1021     Size = 4;
1022     Encoding = DW_ATE_float;
1023     break;
1024   case Type::DoubleTyID:
1025     Name = "double";
1026     Size = 8;
1027     Encoding = DW_ATE_float;
1028     break;
1029   default: 
1030     // FIXME - handle more complex types.
1031     Name = "unknown";
1032     Size = 1;
1033     Encoding = DW_ATE_address;
1034     break;
1035   }
1036   
1037   // construct the type DIE.
1038   Slot = new DIE(DW_TAG_base_type);
1039   Slot->AddString(DW_AT_name,      DW_FORM_string, Name);
1040   Slot->AddUInt  (DW_AT_byte_size, 0,              Size);
1041   Slot->AddUInt  (DW_AT_encoding,  DW_FORM_data1,  Encoding);
1042   
1043   // Add to context owner.
1044   Unit->getDie()->AddChild(Slot);
1045   
1046   return Slot;
1047 }
1048
1049 /// NewType - Create a new type DIE.
1050 ///
1051 DIE *DwarfWriter::NewType(CompileUnit *Unit, TypeDesc *TyDesc) {
1052   // FIXME - hack to get around NULL types short term.
1053   if (!TyDesc)  return NewBasicType(Unit, Type::IntTy);
1054
1055   // Check for pre-existence.
1056   DIE *&Slot = DescToDieMap[TyDesc];
1057   if (Slot) return Slot;
1058
1059   // Get core information.
1060   const std::string &Name = TyDesc->getName();
1061   // FIXME - handle larger sizes.
1062   unsigned Size = TyDesc->getSize() >> 3;
1063   
1064   DIE *Ty = NULL;
1065   
1066   if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
1067     // Fundamental types like int, float, bool
1068     Slot = Ty = new DIE(DW_TAG_base_type);
1069     unsigned Encoding = BasicTy->getEncoding();
1070     Ty->AddUInt  (DW_AT_encoding,  DW_FORM_data1, Encoding);
1071   } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
1072     // Determine which derived type.
1073     unsigned T = 0;
1074     switch (DerivedTy->getTag()) {
1075     case DI_TAG_typedef:   T = DW_TAG_typedef;         break;
1076     case DI_TAG_pointer:   T = DW_TAG_pointer_type;    break;
1077     case DI_TAG_reference: T = DW_TAG_reference_type;  break;
1078     case DI_TAG_const:     T = DW_TAG_const_type;      break;
1079     case DI_TAG_volatile:  T = DW_TAG_volatile_type;   break;
1080     case DI_TAG_restrict:  T = DW_TAG_restrict_type;   break;
1081     default: assert( 0 && "Unknown tag on derived type");
1082     }
1083     
1084     // Create specific DIE.
1085     Slot = Ty = new DIE(T);
1086     
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(Unit, FromTy));
1090     }
1091   } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
1092     // Determine which composite type.
1093     unsigned T = 0;
1094     switch (CompTy->getTag()) {
1095     case DI_TAG_array:     T = DW_TAG_array_type;       break;
1096     case DI_TAG_struct:    T = DW_TAG_structure_type;   break;
1097     case DI_TAG_union:     T = DW_TAG_union_type;       break;
1098     case DI_TAG_enum:      T = DW_TAG_enumeration_type; break;
1099     default: assert( 0 && "Unknown tag on composite type");
1100     }
1101     
1102     // Create specific DIE.
1103     Slot = Ty = new DIE(T);
1104     std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
1105     
1106     switch (CompTy->getTag()) {
1107     case DI_TAG_array: {
1108       // Add element type.
1109       if (TypeDesc *FromTy = CompTy->getFromType()) {
1110          Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4, NewType(Unit, FromTy));
1111       }
1112       // Don't emit size attribute.
1113       Size = 0;
1114       
1115       // Construct an anonymous type for index type.
1116       DIE *IndexTy = new DIE(DW_TAG_base_type);
1117       IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
1118       IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
1119       // Add to context.
1120       Unit->getDie()->AddChild(IndexTy);
1121     
1122       // Add subranges to array type.
1123       for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
1124         SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
1125         int64_t Lo = SRD->getLo();
1126         int64_t Hi = SRD->getHi();
1127         DIE *Subrange = new DIE(DW_TAG_subrange_type);
1128         
1129         // If a range is available.
1130         if (Lo != Hi) {
1131           Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
1132           // Only add low if non-zero.
1133           if (Lo) Subrange->AddUInt(DW_AT_lower_bound, 0, Lo);
1134           Subrange->AddUInt(DW_AT_upper_bound, 0, Hi);
1135         }
1136         Ty->AddChild(Subrange);
1137       }
1138       
1139       break;
1140     }
1141     case DI_TAG_struct: {
1142       break;
1143     }
1144     case DI_TAG_union: {
1145       break;
1146     }
1147     case DI_TAG_enum: {
1148       break;
1149     }
1150     default: break;
1151     }
1152   }
1153   
1154   assert(Ty && "Type not supported yet");
1155  
1156   // Add size if non-zero (derived types don't have a size.)
1157   if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
1158   // Add name if not anonymous or intermediate type.
1159   if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
1160   // Add source line info if present.
1161   if (CompileUnitDesc *File = TyDesc->getFile()) {
1162     CompileUnit *FileUnit = FindCompileUnit(File);
1163     unsigned FileID = FileUnit->getID();
1164     int Line = TyDesc->getLine();
1165     Ty->AddUInt(DW_AT_decl_file, 0, FileID);
1166     Ty->AddUInt(DW_AT_decl_line, 0, Line);
1167   }
1168
1169   // Add to context owner.
1170   Unit->getDie()->AddChild(Ty);
1171   
1172   return Slot;
1173 }
1174
1175 /// NewCompileUnit - Create new compile unit and it's die.
1176 ///
1177 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1178                                          unsigned ID) {
1179   // Construct debug information entry.
1180   DIE *Die = new DIE(DW_TAG_compile_unit);
1181   Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4,  DWLabel("line", 0));
1182   Die->AddLabel (DW_AT_high_pc,   DW_FORM_addr,   DWLabel("text_end", 0));
1183   Die->AddLabel (DW_AT_low_pc,    DW_FORM_addr,   DWLabel("text_begin", 0));
1184   Die->AddString(DW_AT_producer,  DW_FORM_string, UnitDesc->getProducer());
1185   Die->AddUInt  (DW_AT_language,  DW_FORM_data1,  UnitDesc->getLanguage());
1186   Die->AddString(DW_AT_name,      DW_FORM_string, UnitDesc->getFileName());
1187   Die->AddString(DW_AT_comp_dir,  DW_FORM_string, UnitDesc->getDirectory());
1188   
1189   // Add die to descriptor map.
1190   DescToDieMap[UnitDesc] = Die;
1191   
1192   // Construct compile unit.
1193   CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1194   
1195   // Add Unit to compile unit map.
1196   DescToUnitMap[UnitDesc] = Unit;
1197   
1198   return Unit;
1199 }
1200
1201 /// FindCompileUnit - Get the compile unit for the given descriptor.
1202 ///
1203 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1204   CompileUnit *Unit = DescToUnitMap[UnitDesc];
1205   assert(Unit && "Missing compile unit.");
1206   return Unit;
1207 }
1208
1209 /// NewGlobalVariable - Add a new global variable DIE.
1210 ///
1211 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1212   // Check for pre-existence.
1213   DIE *&Slot = DescToDieMap[GVD];
1214   if (Slot) return Slot;
1215   
1216   // Get the compile unit context.
1217   CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
1218   CompileUnit *Unit = FindCompileUnit(UnitDesc);
1219   // Get the global variable itself.
1220   GlobalVariable *GV = GVD->getGlobalVariable();
1221   // Generate the mangled name.
1222   std::string MangledName = Asm->Mang->getValueName(GV);
1223
1224   // Gather the details (simplify add attribute code.)
1225   const std::string &Name = GVD->getName();
1226   unsigned FileID = Unit->getID();
1227   unsigned Line = GVD->getLine();
1228   
1229   // Get the global's type.
1230   DIE *Type = NewType(Unit, GVD->getTypeDesc()); 
1231
1232   // Create the globale variable DIE.
1233   DIE *VariableDie = new DIE(DW_TAG_variable);
1234   VariableDie->AddString     (DW_AT_name,      DW_FORM_string, Name);
1235   VariableDie->AddUInt       (DW_AT_decl_file, 0,              FileID);
1236   VariableDie->AddUInt       (DW_AT_decl_line, 0,              Line);
1237   VariableDie->AddDIEntry    (DW_AT_type,      DW_FORM_ref4,   Type);
1238   VariableDie->AddUInt       (DW_AT_external,  DW_FORM_flag,   1);
1239   // FIXME - needs to be a proper expression.
1240   VariableDie->AddObjectLabel(DW_AT_location,  DW_FORM_block1, MangledName);
1241   
1242   // Add to map.
1243   Slot = VariableDie;
1244  
1245   // Add to context owner.
1246   Unit->getDie()->AddChild(VariableDie);
1247   
1248   // Expose as global.
1249   // FIXME - need to check external flag.
1250   Unit->AddGlobal(Name, VariableDie);
1251   
1252   return VariableDie;
1253 }
1254
1255 /// NewSubprogram - Add a new subprogram DIE.
1256 ///
1257 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1258   // Check for pre-existence.
1259   DIE *&Slot = DescToDieMap[SPD];
1260   if (Slot) return Slot;
1261   
1262   // Get the compile unit context.
1263   CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1264   CompileUnit *Unit = FindCompileUnit(UnitDesc);
1265
1266   // Gather the details (simplify add attribute code.)
1267   const std::string &Name = SPD->getName();
1268   unsigned FileID = Unit->getID();
1269   // FIXME - faking the line for the time being.
1270   unsigned Line = 1;
1271   
1272   // FIXME - faking the type for the time being.
1273   DIE *Type = NewBasicType(Unit, Type::IntTy); 
1274                                     
1275   DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
1276   SubprogramDie->AddString     (DW_AT_name,      DW_FORM_string, Name);
1277   SubprogramDie->AddUInt       (DW_AT_decl_file, 0,              FileID);
1278   SubprogramDie->AddUInt       (DW_AT_decl_line, 0,              Line);
1279   SubprogramDie->AddDIEntry    (DW_AT_type,      DW_FORM_ref4,   Type);
1280   SubprogramDie->AddUInt       (DW_AT_external,  DW_FORM_flag,   1);
1281   
1282   // Add to map.
1283   Slot = SubprogramDie;
1284  
1285   // Add to context owner.
1286   Unit->getDie()->AddChild(SubprogramDie);
1287   
1288   // Expose as global.
1289   Unit->AddGlobal(Name, SubprogramDie);
1290   
1291   return SubprogramDie;
1292 }
1293
1294 /// EmitInitial - Emit initial Dwarf declarations.  This is necessary for cc
1295 /// tools to recognize the object file contains Dwarf information.
1296 ///
1297 void DwarfWriter::EmitInitial() const {
1298   // Dwarf sections base addresses.
1299   Asm->SwitchSection(DwarfFrameSection, 0);
1300   EmitLabel("section_frame", 0);
1301   Asm->SwitchSection(DwarfInfoSection, 0);
1302   EmitLabel("section_info", 0);
1303   EmitLabel("info", 0);
1304   Asm->SwitchSection(DwarfAbbrevSection, 0);
1305   EmitLabel("section_abbrev", 0);
1306   EmitLabel("abbrev", 0);
1307   Asm->SwitchSection(DwarfARangesSection, 0);
1308   EmitLabel("section_aranges", 0);
1309   Asm->SwitchSection(DwarfMacInfoSection, 0);
1310   EmitLabel("section_macinfo", 0);
1311   Asm->SwitchSection(DwarfLineSection, 0);
1312   EmitLabel("section_line", 0);
1313   EmitLabel("line", 0);
1314   Asm->SwitchSection(DwarfLocSection, 0);
1315   EmitLabel("section_loc", 0);
1316   Asm->SwitchSection(DwarfPubNamesSection, 0);
1317   EmitLabel("section_pubnames", 0);
1318   Asm->SwitchSection(DwarfStrSection, 0);
1319   EmitLabel("section_str", 0);
1320   Asm->SwitchSection(DwarfRangesSection, 0);
1321   EmitLabel("section_ranges", 0);
1322
1323   Asm->SwitchSection(TextSection, 0);
1324   EmitLabel("text_begin", 0);
1325   Asm->SwitchSection(DataSection, 0);
1326   EmitLabel("data_begin", 0);
1327 }
1328
1329 /// EmitDIE - Recusively Emits a debug information entry.
1330 ///
1331 void DwarfWriter::EmitDIE(DIE *Die) const {
1332   // Get the abbreviation for this DIE.
1333   unsigned AbbrevID = Die->getAbbrevID();
1334   const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1335   
1336   O << "\n";
1337
1338   // Emit the code (index) for the abbreviation.
1339   EmitULEB128Bytes(AbbrevID);
1340   EOL(std::string("Abbrev [" +
1341       utostr(AbbrevID) +
1342       "] 0x" + utohexstr(Die->getOffset()) +
1343       ":0x" + utohexstr(Die->getSize()) + " " +
1344       TagString(Abbrev.getTag())));
1345   
1346   const std::vector<DIEValue *> &Values = Die->getValues();
1347   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1348   
1349   // Emit the DIE attribute values.
1350   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1351     unsigned Attr = AbbrevData[i].getAttribute();
1352     unsigned Form = AbbrevData[i].getForm();
1353     assert(Form && "Too many attributes for DIE (check abbreviation)");
1354     
1355     switch (Attr) {
1356     case DW_AT_sibling: {
1357       EmitInt32(Die->SiblingOffset());
1358       break;
1359     }
1360     default: {
1361       // Emit an attribute using the defined form.
1362       Values[i]->EmitValue(*this, Form);
1363       break;
1364     }
1365     }
1366     
1367     EOL(AttributeString(Attr));
1368   }
1369   
1370   // Emit the DIE children if any.
1371   if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1372     const std::vector<DIE *> &Children = Die->getChildren();
1373     
1374     for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1375       // FIXME - handle sibling offsets.
1376       // FIXME - handle all DIE types.
1377       EmitDIE(Children[j]);
1378     }
1379     
1380     EmitInt8(0); EOL("End Of Children Mark");
1381   }
1382 }
1383
1384 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1385 ///
1386 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1387   // Record the abbreviation.
1388   Die->Complete(*this);
1389   
1390   // Get the abbreviation for this DIE.
1391   unsigned AbbrevID = Die->getAbbrevID();
1392   const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1393
1394   // Set DIE offset
1395   Die->setOffset(Offset);
1396   
1397   // Start the size with the size of abbreviation code.
1398   Offset += SizeULEB128(AbbrevID);
1399   
1400   const std::vector<DIEValue *> &Values = Die->getValues();
1401   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1402
1403   // Emit the DIE attribute values.
1404   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
1405     // Size attribute value.
1406     Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
1407   }
1408   
1409   // Emit the DIE children if any.
1410   if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1411     const std::vector<DIE *> &Children = Die->getChildren();
1412     
1413     for (unsigned j = 0, M = Children.size(); j < M; ++j) {
1414       // FIXME - handle sibling offsets.
1415       // FIXME - handle all DIE types.
1416       Offset = SizeAndOffsetDie(Children[j], Offset);
1417     }
1418     
1419     // End of children marker.
1420     Offset += sizeof(int8_t);
1421   }
1422
1423   Die->setSize(Offset - Die->getOffset());
1424   return Offset;
1425 }
1426
1427 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1428 ///
1429 void DwarfWriter::SizeAndOffsets() {
1430   
1431   // Process each compile unit.
1432   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1433     CompileUnit *Unit = CompileUnits[i];
1434     if (Unit->hasContent()) {
1435       // Compute size of compile unit header
1436       unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
1437                         sizeof(int16_t) + // DWARF version number
1438                         sizeof(int32_t) + // Offset Into Abbrev. Section
1439                         sizeof(int8_t);   // Pointer Size (in bytes)
1440     
1441       SizeAndOffsetDie(Unit->getDie(), Offset);
1442     }
1443   }
1444 }
1445
1446 /// EmitDebugInfo - Emit the debug info section.
1447 ///
1448 void DwarfWriter::EmitDebugInfo() const {
1449   // Start debug info section.
1450   Asm->SwitchSection(DwarfInfoSection, 0);
1451   
1452   // Process each compile unit.
1453   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1454     CompileUnit *Unit = CompileUnits[i];
1455     
1456     if (Unit->hasContent()) {
1457       DIE *Die = Unit->getDie();
1458       // Emit the compile units header.
1459       EmitLabel("info_begin", Unit->getID());
1460       // Emit size of content not including length itself
1461       unsigned ContentSize = Die->getSize() +
1462                              sizeof(int16_t) + // DWARF version number
1463                              sizeof(int32_t) + // Offset Into Abbrev. Section
1464                              sizeof(int8_t);   // Pointer Size (in bytes)
1465                              
1466       EmitInt32(ContentSize);  EOL("Length of Compilation Unit Info");
1467       EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1468       EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
1469       EmitInt8(AddressSize); EOL("Address Size (in bytes)");
1470     
1471       EmitDIE(Die);
1472       EmitLabel("info_end", Unit->getID());
1473     }
1474     
1475     O << "\n";
1476   }
1477 }
1478
1479 /// EmitAbbreviations - Emit the abbreviation section.
1480 ///
1481 void DwarfWriter::EmitAbbreviations() const {
1482   // Check to see if it is worth the effort.
1483   if (!Abbreviations.empty()) {
1484     // Start the debug abbrev section.
1485     Asm->SwitchSection(DwarfAbbrevSection, 0);
1486     
1487     EmitLabel("abbrev_begin", 0);
1488     
1489     // For each abbrevation.
1490     for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
1491                   AbbrevID <= NAID; ++AbbrevID) {
1492       // Get abbreviation data
1493       const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1494       
1495       // Emit the abbrevations code (base 1 index.)
1496       EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1497       
1498       // Emit the abbreviations data.
1499       Abbrev.Emit(*this);
1500   
1501       O << "\n";
1502     }
1503     
1504     EmitLabel("abbrev_end", 0);
1505   
1506     O << "\n";
1507   }
1508 }
1509
1510 /// EmitDebugLines - Emit source line information.
1511 ///
1512 void DwarfWriter::EmitDebugLines() const {
1513   // Minimum line delta, thus ranging from -10..(255-10).
1514   const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
1515   // Maximum line delta, thus ranging from -10..(255-10).
1516   const int MaxLineDelta = 255 + MinLineDelta;
1517
1518   // Start the dwarf line section.
1519   Asm->SwitchSection(DwarfLineSection, 0);
1520   
1521   // Construct the section header.
1522   
1523   EmitDifference("line_end", 0, "line_begin", 0);
1524   EOL("Length of Source Line Info");
1525   EmitLabel("line_begin", 0);
1526   
1527   EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1528   
1529   EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1530   EOL("Prolog Length");
1531   EmitLabel("line_prolog_begin", 0);
1532   
1533   EmitInt8(1); EOL("Minimum Instruction Length");
1534
1535   EmitInt8(1); EOL("Default is_stmt_start flag");
1536
1537   EmitInt8(MinLineDelta);  EOL("Line Base Value (Special Opcodes)");
1538   
1539   EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1540
1541   EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1542   
1543   // Line number standard opcode encodings argument count
1544   EmitInt8(0); EOL("DW_LNS_copy arg count");
1545   EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
1546   EmitInt8(1); EOL("DW_LNS_advance_line arg count");
1547   EmitInt8(1); EOL("DW_LNS_set_file arg count");
1548   EmitInt8(1); EOL("DW_LNS_set_column arg count");
1549   EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
1550   EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
1551   EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
1552   EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
1553
1554   const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1555   const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1556
1557   // Emit directories.
1558   for (unsigned DirectoryID = 1, NDID = Directories.size();
1559                 DirectoryID <= NDID; ++DirectoryID) {
1560     EmitString(Directories[DirectoryID]); EOL("Directory");
1561   }
1562   EmitInt8(0); EOL("End of directories");
1563   
1564   // Emit files.
1565   for (unsigned SourceID = 1, NSID = SourceFiles.size();
1566                SourceID <= NSID; ++SourceID) {
1567     const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1568     EmitString(SourceFile.getName()); EOL("Source");
1569     EmitULEB128Bytes(SourceFile.getDirectoryID());  EOL("Directory #");
1570     EmitULEB128Bytes(0);  EOL("Mod date");
1571     EmitULEB128Bytes(0);  EOL("File size");
1572   }
1573   EmitInt8(0); EOL("End of files");
1574   
1575   EmitLabel("line_prolog_end", 0);
1576   
1577   // Emit line information
1578   const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1579   
1580   // Dwarf assumes we start with first line of first source file.
1581   unsigned Source = 1;
1582   unsigned Line = 1;
1583   
1584   // Construct rows of the address, source, line, column matrix.
1585   for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
1586     SourceLineInfo *LineInfo = LineInfos[i];
1587     
1588     if (DwarfVerbose) {
1589       unsigned SourceID = LineInfo->getSourceID();
1590       const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1591       unsigned DirectoryID = SourceFile.getDirectoryID();
1592       O << "\t"
1593         << Asm->CommentString << " "
1594         << Directories[DirectoryID]
1595         << SourceFile.getName() << ":"
1596         << LineInfo->getLine() << "\n"; 
1597     }
1598
1599     // Define the line address.
1600     EmitInt8(0); EOL("Extended Op");
1601     EmitInt8(4 + 1); EOL("Op size");
1602     EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1603     EmitReference("loc", i + 1); EOL("Location label");
1604     
1605     // If change of source, then switch to the new source.
1606     if (Source != LineInfo->getSourceID()) {
1607       Source = LineInfo->getSourceID();
1608       EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
1609       EmitULEB128Bytes(Source); EOL("New Source");
1610     }
1611     
1612     // If change of line.
1613     if (Line != LineInfo->getLine()) {
1614       // Determine offset.
1615       int Offset = LineInfo->getLine() - Line;
1616       int Delta = Offset - MinLineDelta;
1617       
1618       // Update line.
1619       Line = LineInfo->getLine();
1620       
1621       // If delta is small enough and in range...
1622       if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
1623         // ... then use fast opcode.
1624         EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
1625       } else {
1626         // ... otherwise use long hand.
1627         EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
1628         EmitSLEB128Bytes(Offset); EOL("Line Offset");
1629         EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1630       }
1631     } else {
1632       // Copy the previous row (different address or source)
1633       EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1634     }
1635   }
1636
1637   // Define last address.
1638   EmitInt8(0); EOL("Extended Op");
1639   EmitInt8(4 + 1); EOL("Op size");
1640   EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
1641   EmitReference("text_end", 0); EOL("Location label");
1642
1643   // Mark end of matrix.
1644   EmitInt8(0); EOL("DW_LNE_end_sequence");
1645   EmitULEB128Bytes(1);  O << "\n";
1646   EmitInt8(1); O << "\n";
1647   
1648   EmitLabel("line_end", 0);
1649   
1650   O << "\n";
1651 }
1652   
1653 /// EmitDebugFrame - Emit visible names into a debug frame section.
1654 ///
1655 void DwarfWriter::EmitDebugFrame() {
1656   // FIXME - Should be per frame
1657 }
1658
1659 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1660 ///
1661 void DwarfWriter::EmitDebugPubNames() {
1662   // Start the dwarf pubnames section.
1663   Asm->SwitchSection(DwarfPubNamesSection, 0);
1664     
1665   // Process each compile unit.
1666   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1667     CompileUnit *Unit = CompileUnits[i];
1668     
1669     if (Unit->hasContent()) {
1670       EmitDifference("pubnames_end", Unit->getID(),
1671                      "pubnames_begin", Unit->getID());
1672       EOL("Length of Public Names Info");
1673       
1674       EmitLabel("pubnames_begin", Unit->getID());
1675       
1676       EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1677       
1678       EmitReference("info_begin", Unit->getID());
1679       EOL("Offset of Compilation Unit Info");
1680
1681       EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
1682       EOL("Compilation Unit Length");
1683       
1684       std::map<std::string, DIE *> &Globals = Unit->getGlobals();
1685       
1686       for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
1687                                                   GE = Globals.end();
1688            GI != GE; ++GI) {
1689         const std::string &Name = GI->first;
1690         DIE * Entity = GI->second;
1691         
1692         EmitInt32(Entity->getOffset()); EOL("DIE offset");
1693         EmitString(Name); EOL("External Name");
1694       }
1695     
1696       EmitInt32(0); EOL("End Mark");
1697       EmitLabel("pubnames_end", Unit->getID());
1698     
1699       O << "\n";
1700     }
1701   }
1702 }
1703
1704 /// EmitDebugStr - Emit visible names into a debug str section.
1705 ///
1706 void DwarfWriter::EmitDebugStr() {
1707   // Check to see if it is worth the effort.
1708   if (!StringPool.empty()) {
1709     // Start the dwarf str section.
1710     Asm->SwitchSection(DwarfStrSection, 0);
1711     
1712     // For each of strings in teh string pool.
1713     for (unsigned StringID = 1, N = StringPool.size();
1714          StringID <= N; ++StringID) {
1715       // Emit a label for reference from debug information entries.
1716       EmitLabel("string", StringID);
1717       // Emit the string itself.
1718       const std::string &String = StringPool[StringID];
1719       EmitString(String); O << "\n";
1720     }
1721   
1722     O << "\n";
1723   }
1724 }
1725
1726 /// EmitDebugLoc - Emit visible names into a debug loc section.
1727 ///
1728 void DwarfWriter::EmitDebugLoc() {
1729   // Start the dwarf loc section.
1730   Asm->SwitchSection(DwarfLocSection, 0);
1731   
1732   O << "\n";
1733 }
1734
1735 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1736 ///
1737 void DwarfWriter::EmitDebugARanges() {
1738   // Start the dwarf aranges section.
1739   Asm->SwitchSection(DwarfARangesSection, 0);
1740   
1741   // FIXME - Mock up
1742 #if 0
1743   // Process each compile unit.
1744   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1745     CompileUnit *Unit = CompileUnits[i];
1746     
1747     if (Unit->hasContent()) {
1748       // Don't include size of length
1749       EmitInt32(0x1c); EOL("Length of Address Ranges Info");
1750       
1751       EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
1752       
1753       EmitReference("info_begin", Unit->getID());
1754       EOL("Offset of Compilation Unit Info");
1755
1756       EmitInt8(AddressSize); EOL("Size of Address");
1757
1758       EmitInt8(0); EOL("Size of Segment Descriptor");
1759
1760       EmitInt16(0);  EOL("Pad (1)");
1761       EmitInt16(0);  EOL("Pad (2)");
1762
1763       // Range 1
1764       EmitReference("text_begin", 0); EOL("Address");
1765       EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
1766
1767       EmitInt32(0); EOL("EOM (1)");
1768       EmitInt32(0); EOL("EOM (2)");
1769       
1770       O << "\n";
1771     }
1772   }
1773 #endif
1774 }
1775
1776 /// EmitDebugRanges - Emit visible names into a debug ranges section.
1777 ///
1778 void DwarfWriter::EmitDebugRanges() {
1779   // Start the dwarf ranges section.
1780   Asm->SwitchSection(DwarfRangesSection, 0);
1781   
1782   O << "\n";
1783 }
1784
1785 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1786 ///
1787 void DwarfWriter::EmitDebugMacInfo() {
1788   // Start the dwarf macinfo section.
1789   Asm->SwitchSection(DwarfMacInfoSection, 0);
1790   
1791   O << "\n";
1792 }
1793
1794 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1795 /// header file.
1796 void DwarfWriter::ConstructCompileUnitDIEs() {
1797   const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
1798   
1799   for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
1800     CompileUnit *Unit = NewCompileUnit(CUW[i], i);
1801     CompileUnits.push_back(Unit);
1802   }
1803 }
1804
1805 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1806 /// variables.
1807 void DwarfWriter::ConstructGlobalDIEs(Module &M) {
1808   std::vector<GlobalVariableDesc *> GlobalVariables =
1809                        DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
1810   
1811   for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
1812     GlobalVariableDesc *GVD = GlobalVariables[i];
1813     NewGlobalVariable(GVD);
1814   }
1815 }
1816
1817 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
1818 /// subprograms.
1819 void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
1820   std::vector<SubprogramDesc *> Subprograms =
1821                            DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
1822   
1823   for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
1824     SubprogramDesc *SPD = Subprograms[i];
1825     NewSubprogram(SPD);
1826   }
1827 }
1828
1829 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
1830 ///
1831 bool DwarfWriter::ShouldEmitDwarf() {
1832   // Check if debug info is present.
1833   if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1834   
1835   // Make sure initial declarations are made.
1836   if (!didInitial) {
1837     EmitInitial();
1838     didInitial = true;
1839   }
1840   
1841   // Okay to emit.
1842   return true;
1843 }
1844
1845 //===----------------------------------------------------------------------===//
1846 // Main entry points.
1847 //
1848   
1849 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
1850 : O(OS)
1851 , Asm(A)
1852 , DebugInfo(NULL)
1853 , didInitial(false)
1854 , CompileUnits()
1855 , Abbreviations()
1856 , StringPool()
1857 , DescToUnitMap()
1858 , DescToDieMap()
1859 , TypeToDieMap()
1860 , AddressSize(sizeof(int32_t))
1861 , hasLEB128(false)
1862 , hasDotLoc(false)
1863 , hasDotFile(false)
1864 , needsSet(false)
1865 , DwarfAbbrevSection(".debug_abbrev")
1866 , DwarfInfoSection(".debug_info")
1867 , DwarfLineSection(".debug_line")
1868 , DwarfFrameSection(".debug_frame")
1869 , DwarfPubNamesSection(".debug_pubnames")
1870 , DwarfPubTypesSection(".debug_pubtypes")
1871 , DwarfStrSection(".debug_str")
1872 , DwarfLocSection(".debug_loc")
1873 , DwarfARangesSection(".debug_aranges")
1874 , DwarfRangesSection(".debug_ranges")
1875 , DwarfMacInfoSection(".debug_macinfo")
1876 , TextSection(".text")
1877 , DataSection(".data")
1878 {}
1879 DwarfWriter::~DwarfWriter() {
1880   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1881     delete CompileUnits[i];
1882   }
1883 }
1884
1885 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
1886 ///
1887 void DwarfWriter::BeginModule(Module &M) {
1888   if (!ShouldEmitDwarf()) return;
1889   EOL("Dwarf Begin Module");
1890 }
1891
1892 /// EndModule - Emit all Dwarf sections that should come after the content.
1893 ///
1894 void DwarfWriter::EndModule(Module &M) {
1895   if (!ShouldEmitDwarf()) return;
1896   EOL("Dwarf End Module");
1897   
1898   // Standard sections final addresses.
1899   Asm->SwitchSection(TextSection, 0);
1900   EmitLabel("text_end", 0);
1901   Asm->SwitchSection(DataSection, 0);
1902   EmitLabel("data_end", 0);
1903   
1904   // Create all the compile unit DIEs.
1905   ConstructCompileUnitDIEs();
1906   
1907   // Create DIEs for each of the externally visible global variables.
1908   ConstructGlobalDIEs(M);
1909
1910   // Create DIEs for each of the externally visible subprograms.
1911   ConstructSubprogramDIEs(M);
1912   
1913   // Compute DIE offsets and sizes.
1914   SizeAndOffsets();
1915   
1916   // Emit all the DIEs into a debug info section
1917   EmitDebugInfo();
1918   
1919   // Corresponding abbreviations into a abbrev section.
1920   EmitAbbreviations();
1921   
1922   // Emit source line correspondence into a debug line section.
1923   EmitDebugLines();
1924   
1925   // Emit info into a debug frame section.
1926   // EmitDebugFrame();
1927   
1928   // Emit info into a debug pubnames section.
1929   EmitDebugPubNames();
1930   
1931   // Emit info into a debug str section.
1932   EmitDebugStr();
1933   
1934   // Emit info into a debug loc section.
1935   EmitDebugLoc();
1936   
1937   // Emit info into a debug aranges section.
1938   EmitDebugARanges();
1939   
1940   // Emit info into a debug ranges section.
1941   EmitDebugRanges();
1942   
1943   // Emit info into a debug macinfo section.
1944   EmitDebugMacInfo();
1945 }
1946
1947 /// BeginFunction - Gather pre-function debug information.
1948 ///
1949 void DwarfWriter::BeginFunction(MachineFunction &MF) {
1950   if (!ShouldEmitDwarf()) return;
1951   EOL("Dwarf Begin Function");
1952 }
1953
1954 /// EndFunction - Gather and emit post-function debug information.
1955 ///
1956 void DwarfWriter::EndFunction(MachineFunction &MF) {
1957   if (!ShouldEmitDwarf()) return;
1958   EOL("Dwarf End Function");
1959 }