Adding basic structure 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 // CompileUnit - This dwarf writer support class manages information associate
45 // with a source file.
46 class CompileUnit {
47 private:
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.
53
54 public:
55   CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
56   : Desc(CUD)
57   , ID(I)
58   , Die(D)
59   , Globals()
60   {}
61   
62   ~CompileUnit();
63   
64   // Accessors.
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; }
69   
70   /// hasContent - Return true if this compile unit has something to write out.
71   ///
72   bool hasContent() const;
73   
74   /// AddGlobal - Add a new global entity to the compile unit.
75   ///
76   void AddGlobal(const std::string &Name, DIE *Die);
77   
78 };
79
80 //===----------------------------------------------------------------------===//
81 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
82 // Dwarf abbreviation.
83 class DIEAbbrevData {
84 private:
85   unsigned Attribute;                 // Dwarf attribute code.
86   unsigned Form;                      // Dwarf form code.
87   
88 public:
89   DIEAbbrevData(unsigned A, unsigned F)
90   : Attribute(A)
91   , Form(F)
92   {}
93   
94   // Accessors.
95   unsigned getAttribute() const { return Attribute; }
96   unsigned getForm()      const { return Form; }
97   
98   /// operator== - Used by DIEAbbrev to locate entry.
99   ///
100   bool operator==(const DIEAbbrevData &DAD) const {
101     return Attribute == DAD.Attribute && Form == DAD.Form;
102   }
103
104   /// operator!= - Used by DIEAbbrev to locate entry.
105   ///
106   bool operator!=(const DIEAbbrevData &DAD) const {
107     return Attribute != DAD.Attribute || Form != DAD.Form;
108   }
109   
110   /// operator< - Used by DIEAbbrev to locate entry.
111   ///
112   bool operator<(const DIEAbbrevData &DAD) const {
113     return Attribute < DAD.Attribute ||
114           (Attribute == DAD.Attribute && Form < DAD.Form);
115   }
116 };
117
118 //===----------------------------------------------------------------------===//
119 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
120 // information object.
121 class DIEAbbrev {
122 private:
123   unsigned Tag;                       // Dwarf tag code.
124   unsigned ChildrenFlag;              // Dwarf children flag.
125   std::vector<DIEAbbrevData> Data;    // Raw data bytes for abbreviation.
126
127 public:
128
129   DIEAbbrev(unsigned T, unsigned C)
130   : Tag(T)
131   , ChildrenFlag(C)
132   , Data()
133   {}
134   ~DIEAbbrev() {}
135   
136   // Accessors.
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; }
141
142   /// operator== - Used by UniqueVector to locate entry.
143   ///
144   bool operator==(const DIEAbbrev &DA) const;
145
146   /// operator< - Used by UniqueVector to locate entry.
147   ///
148   bool operator<(const DIEAbbrev &DA) const;
149
150   /// AddAttribute - Adds another set of attribute information to the
151   /// abbreviation.
152   void AddAttribute(unsigned Attribute, unsigned Form) {
153     Data.push_back(DIEAbbrevData(Attribute, Form));
154   }
155   
156   /// Emit - Print the abbreviation using the specified Dwarf writer.
157   ///
158   void Emit(const DwarfWriter &DW) const; 
159       
160 #ifndef NDEBUG
161   void print(std::ostream &O);
162   void dump();
163 #endif
164 };
165
166 //===----------------------------------------------------------------------===//
167 // DIEValue - A debug information entry value.
168 //
169 class DIEValue {
170 public:
171   enum {
172     isInteger,
173     isString,
174     isLabel,
175     isAsIsLabel,
176     isDelta,
177     isEntry
178   };
179   
180   unsigned Type;                      // Type of the value
181   
182   DIEValue(unsigned T) : Type(T) {}
183   virtual ~DIEValue() {}
184   
185   // Implement isa/cast/dyncast.
186   static bool classof(const DIEValue *) { return true; }
187   
188   /// EmitValue - Emit value via the Dwarf writer.
189   ///
190   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
191   
192   /// SizeOf - Return the size of a value in bytes.
193   ///
194   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
195 };
196
197 //===----------------------------------------------------------------------===//
198 // DWInteger - An integer value DIE.
199 // 
200 class DIEInteger : public DIEValue {
201 private:
202   uint64_t Integer;
203   
204 public:
205   DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
206
207   // Implement isa/cast/dyncast.
208   static bool classof(const DIEInteger *) { return true; }
209   static bool classof(const DIEValue *I)  { return I->Type == isInteger; }
210   
211   /// EmitValue - Emit integer of appropriate size.
212   ///
213   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
214   
215   /// SizeOf - Determine size of integer value in bytes.
216   ///
217   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
218 };
219
220 //===----------------------------------------------------------------------===//
221 // DIEString - A string value DIE.
222 // 
223 struct DIEString : public DIEValue {
224   const std::string String;
225   
226   DIEString(const std::string &S) : DIEValue(isString), String(S) {}
227
228   // Implement isa/cast/dyncast.
229   static bool classof(const DIEString *) { return true; }
230   static bool classof(const DIEValue *S) { return S->Type == isString; }
231   
232   /// EmitValue - Emit string value.
233   ///
234   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
235   
236   /// SizeOf - Determine size of string value in bytes.
237   ///
238   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
239 };
240
241 //===----------------------------------------------------------------------===//
242 // DIEDwarfLabel - A Dwarf internal label expression DIE.
243 //
244 struct DIEDwarfLabel : public DIEValue {
245   const DWLabel Label;
246   
247   DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
248
249   // Implement isa/cast/dyncast.
250   static bool classof(const DIEDwarfLabel *)  { return true; }
251   static bool classof(const DIEValue *L) { return L->Type == isLabel; }
252   
253   /// EmitValue - Emit label value.
254   ///
255   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
256   
257   /// SizeOf - Determine size of label value in bytes.
258   ///
259   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
260 };
261
262
263 //===----------------------------------------------------------------------===//
264 // DIEObjectLabel - A label to an object in code or data.
265 //
266 struct DIEObjectLabel : public DIEValue {
267   const std::string Label;
268   
269   DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
270
271   // Implement isa/cast/dyncast.
272   static bool classof(const DIEObjectLabel *) { return true; }
273   static bool classof(const DIEValue *L)    { return L->Type == isAsIsLabel; }
274   
275   /// EmitValue - Emit label value.
276   ///
277   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
278   
279   /// SizeOf - Determine size of label value in bytes.
280   ///
281   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
282 };
283
284 //===----------------------------------------------------------------------===//
285 // DIEDelta - A simple label difference DIE.
286 // 
287 struct DIEDelta : public DIEValue {
288   const DWLabel LabelHi;
289   const DWLabel LabelLo;
290   
291   DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
292   : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
293
294   // Implement isa/cast/dyncast.
295   static bool classof(const DIEDelta *)  { return true; }
296   static bool classof(const DIEValue *D) { return D->Type == isDelta; }
297   
298   /// EmitValue - Emit delta value.
299   ///
300   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
301   
302   /// SizeOf - Determine size of delta value in bytes.
303   ///
304   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
305 };
306
307 //===----------------------------------------------------------------------===//
308 // DIEntry - A pointer to a debug information entry.
309 // 
310 struct DIEntry : public DIEValue {
311   DIE *Entry;
312   
313   DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
314
315   // Implement isa/cast/dyncast.
316   static bool classof(const DIEntry *)   { return true; }
317   static bool classof(const DIEValue *E) { return E->Type == isEntry; }
318   
319   /// EmitValue - Emit delta value.
320   ///
321   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
322   
323   /// SizeOf - Determine size of delta value in bytes.
324   ///
325   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
326 };
327
328 //===----------------------------------------------------------------------===//
329 // DIE - A structured debug information entry.  Has an abbreviation which
330 // describes it's organization.
331 class DIE {
332 private:
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.
339   
340 public:
341   DIE(unsigned Tag);
342   ~DIE();
343   
344   // Accessors.
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; }
352   
353   /// SiblingOffset - Return the offset of the debug information entry's
354   /// sibling.
355   unsigned SiblingOffset() const { return Offset + Size; }
356
357   /// AddUInt - Add an unsigned integer attribute data and value.
358   ///
359   void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
360
361   /// AddSInt - Add an signed integer attribute data and value.
362   ///
363   void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
364       
365   /// AddString - Add a std::string attribute data and value.
366   ///
367   void AddString(unsigned Attribute, unsigned Form,
368                  const std::string &String);
369       
370   /// AddLabel - Add a Dwarf label attribute data and value.
371   ///
372   void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
373       
374   /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
375   ///
376   void AddObjectLabel(unsigned Attribute, unsigned Form,
377                       const std::string &Label);
378       
379   /// AddDelta - Add a label delta attribute data and value.
380   ///
381   void AddDelta(unsigned Attribute, unsigned Form,
382                 const DWLabel &Hi, const DWLabel &Lo);
383       
384   ///  AddDIEntry - Add a DIE attribute data and value.
385   ///
386   void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
387
388   /// Complete - Indicate that all attributes have been added and
389   /// ready to get an abbreviation ID.
390   ///
391   void Complete(DwarfWriter &DW);
392   
393   /// AddChild - Add a child to the DIE.
394   void AddChild(DIE *Child);
395 };
396
397 } // End of namespace llvm
398
399 //===----------------------------------------------------------------------===//
400
401 CompileUnit::~CompileUnit() {
402   delete Die;
403 }
404
405 /// hasContent - Return true if this compile unit has something to write out.
406 ///
407 bool CompileUnit::hasContent() const {
408   return !Die->getChildren().empty();
409 }
410
411 /// AddGlobal - Add a new global entity to the compile unit.
412 ///
413 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
414   Globals[Name] = Die;
415 }
416
417 //===----------------------------------------------------------------------===//
418
419 /// operator== - Used by UniqueVector to locate entry.
420 ///
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;
425   
426   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
427     if (Data[i] != DA.Data[i]) return false;
428   }
429   
430   return true;
431 }
432
433 /// operator< - Used by UniqueVector to locate entry.
434 ///
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();
439   
440   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
441     if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
442   }
443   
444   return false;
445 }
446     
447 /// Emit - Print the abbreviation using the specified Dwarf writer.
448 ///
449 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
450   // Emit its Dwarf tag type.
451   DW.EmitULEB128Bytes(Tag);
452   DW.EOL(TagString(Tag));
453   
454   // Emit whether it has children DIEs.
455   DW.EmitULEB128Bytes(ChildrenFlag);
456   DW.EOL(ChildrenString(ChildrenFlag));
457   
458   // For each attribute description.
459   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
460     const DIEAbbrevData &AttrData = Data[i];
461     
462     // Emit attribute type.
463     DW.EmitULEB128Bytes(AttrData.getAttribute());
464     DW.EOL(AttributeString(AttrData.getAttribute()));
465     
466     // Emit form type.
467     DW.EmitULEB128Bytes(AttrData.getForm());
468     DW.EOL(FormEncodingString(AttrData.getForm()));
469   }
470
471   // Mark end of abbreviation.
472   DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
473   DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
474 }
475
476 #ifndef NDEBUG
477   void DIEAbbrev::print(std::ostream &O) {
478     O << "Abbreviation @"
479       << std::hex << (intptr_t)this << std::dec
480       << "  "
481       << TagString(Tag)
482       << " "
483       << ChildrenString(ChildrenFlag)
484       << "\n";
485     
486     for (unsigned i = 0, N = Data.size(); i < N; ++i) {
487       O << "  "
488         << AttributeString(Data[i].getAttribute())
489         << "  "
490         << FormEncodingString(Data[i].getForm())
491         << "\n";
492     }
493   }
494   void DIEAbbrev::dump() { print(std::cerr); }
495 #endif
496
497 //===----------------------------------------------------------------------===//
498
499 /// EmitValue - Emit integer of appropriate size.
500 ///
501 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
502   switch (Form) {
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);
515     break;
516   }
517   default: assert(0 && "DIE Value form not supported yet"); break;
518   }
519 }
520
521 /// SizeOf - Determine size of integer value in bytes.
522 ///
523 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
524   switch (Form) {
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;
535   }
536   return 0;
537 }
538
539 //===----------------------------------------------------------------------===//
540
541 /// EmitValue - Emit string value.
542 ///
543 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
544   DW.EmitString(String);
545 }
546
547 /// SizeOf - Determine size of string value in bytes.
548 ///
549 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
550   return String.size() + sizeof(char); // sizeof('\0');
551 }
552
553 //===----------------------------------------------------------------------===//
554
555 /// EmitValue - Emit label value.
556 ///
557 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
558   DW.EmitReference(Label);
559 }
560
561 /// SizeOf - Determine size of label value in bytes.
562 ///
563 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
564   return DW.getAddressSize();
565 }
566     
567 //===----------------------------------------------------------------------===//
568
569 /// EmitValue - Emit label value.
570 ///
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");
574   
575   DW.EmitInt8(DW_OP_addr);
576   DW.EOL("DW_OP_addr");
577   
578   DW.EmitReference(Label);
579 }
580
581 /// SizeOf - Determine size of label value in bytes.
582 ///
583 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
584   return sizeof(int8_t) + sizeof(int8_t) + DW.getAddressSize();
585 }
586     
587 //===----------------------------------------------------------------------===//
588
589 /// EmitValue - Emit delta value.
590 ///
591 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
592   DW.EmitDifference(LabelHi, LabelLo);
593 }
594
595 /// SizeOf - Determine size of delta value in bytes.
596 ///
597 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
598   return DW.getAddressSize();
599 }
600
601 //===----------------------------------------------------------------------===//
602 /// EmitValue - Emit extry offset.
603 ///
604 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
605   DW.EmitInt32(Entry->getOffset());
606 }
607
608 /// SizeOf - Determine size of label value in bytes.
609 ///
610 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
611   return sizeof(int32_t);
612 }
613     
614 //===----------------------------------------------------------------------===//
615
616 DIE::DIE(unsigned Tag)
617 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
618 , AbbrevID(0)
619 , Offset(0)
620 , Size(0)
621 , Children()
622 , Values()
623 {}
624
625 DIE::~DIE() {
626   if (Abbrev) delete Abbrev;
627   
628   for (unsigned i = 0, N = Children.size(); i < N; ++i) {
629     delete Children[i];
630   }
631
632   for (unsigned j = 0, M = Values.size(); j < M; ++j) {
633     delete Values[j];
634   }
635 }
636     
637 /// AddUInt - Add an unsigned integer attribute data and value.
638 ///
639 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
640   if (Form == 0) {
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;
645   }
646   Abbrev->AddAttribute(Attribute, Form);
647   Values.push_back(new DIEInteger(Integer));
648 }
649     
650 /// AddSInt - Add an signed integer attribute data and value.
651 ///
652 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
653   if (Form == 0) {
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;
658   }
659   Abbrev->AddAttribute(Attribute, Form);
660   Values.push_back(new DIEInteger(Integer));
661 }
662     
663 /// AddString - Add a std::string attribute data and value.
664 ///
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));
669 }
670     
671 /// AddLabel - Add a Dwarf label attribute data and value.
672 ///
673 void DIE::AddLabel(unsigned Attribute, unsigned Form,
674                    const DWLabel &Label) {
675   Abbrev->AddAttribute(Attribute, Form);
676   Values.push_back(new DIEDwarfLabel(Label));
677 }
678     
679 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
680 ///
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));
685 }
686     
687 /// AddDelta - Add a label delta attribute data and value.
688 ///
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));
693 }
694     
695 /// AddDIEntry - Add a DIE attribute data and value.
696 ///
697 void DIE::AddDIEntry(unsigned Attribute,
698                      unsigned Form, DIE *Entry) {
699   Abbrev->AddAttribute(Attribute, Form);
700   Values.push_back(new DIEntry(Entry));
701 }
702
703 /// Complete - Indicate that all attributes have been added and ready to get an
704 /// abbreviation ID.
705 void DIE::Complete(DwarfWriter &DW) {
706   AbbrevID = DW.NewAbbreviation(Abbrev);
707   delete Abbrev;
708   Abbrev = NULL;
709 }
710
711 /// AddChild - Add a child to the DIE.
712 ///
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);
717 }
718
719 //===----------------------------------------------------------------------===//
720
721 /// DWContext
722
723 //===----------------------------------------------------------------------===//
724
725 /// PrintHex - Print a value as a hexidecimal value.
726 ///
727 void DwarfWriter::PrintHex(int Value) const { 
728   O << "0x" << std::hex << Value << std::dec;
729 }
730
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 {
734   if (DwarfVerbose) {
735     O << "\t"
736       << Asm->CommentString
737       << " "
738       << Comment;
739   }
740   O << "\n";
741 }
742
743 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
744 /// unsigned leb128 value.
745 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
746   if (hasLEB128) {
747     O << "\t.uleb128\t"
748       << Value;
749   } else {
750     O << Asm->Data8bitsDirective;
751     PrintULEB128(Value);
752   }
753 }
754
755 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
756 /// signed leb128 value.
757 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
758   if (hasLEB128) {
759     O << "\t.sleb128\t"
760       << Value;
761   } else {
762     O << Asm->Data8bitsDirective;
763     PrintSLEB128(Value);
764   }
765 }
766
767 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
768 /// representing an unsigned leb128 value.
769 void DwarfWriter::PrintULEB128(unsigned Value) const {
770   do {
771     unsigned Byte = Value & 0x7f;
772     Value >>= 7;
773     if (Value) Byte |= 0x80;
774     PrintHex(Byte);
775     if (Value) O << ", ";
776   } while (Value);
777 }
778
779 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
780 /// value.
781 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
782   unsigned Size = 0;
783   do {
784     Value >>= 7;
785     Size += sizeof(int8_t);
786   } while (Value);
787   return Size;
788 }
789
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);
794   bool IsMore;
795   
796   do {
797     unsigned Byte = Value & 0x7f;
798     Value >>= 7;
799     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
800     if (IsMore) Byte |= 0x80;
801     PrintHex(Byte);
802     if (IsMore) O << ", ";
803   } while (IsMore);
804 }
805
806 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
807 /// value.
808 unsigned DwarfWriter::SizeSLEB128(int Value) {
809   unsigned Size = 0;
810   int Sign = Value >> (8 * sizeof(Value) - 1);
811   bool IsMore;
812   
813   do {
814     unsigned Byte = Value & 0x7f;
815     Value >>= 7;
816     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
817     Size += sizeof(int8_t);
818   } while (IsMore);
819   return Size;
820 }
821
822 /// EmitInt8 - Emit a byte directive and value.
823 ///
824 void DwarfWriter::EmitInt8(int Value) const {
825   O << Asm->Data8bitsDirective;
826   PrintHex(Value & 0xFF);
827 }
828
829 /// EmitInt16 - Emit a short directive and value.
830 ///
831 void DwarfWriter::EmitInt16(int Value) const {
832   O << Asm->Data16bitsDirective;
833   PrintHex(Value & 0xFFFF);
834 }
835
836 /// EmitInt32 - Emit a long directive and value.
837 ///
838 void DwarfWriter::EmitInt32(int Value) const {
839   O << Asm->Data32bitsDirective;
840   PrintHex(Value);
841 }
842
843 /// EmitInt64 - Emit a long long directive and value.
844 ///
845 void DwarfWriter::EmitInt64(uint64_t Value) const {
846   if (Asm->Data64bitsDirective) {
847     O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
848   } else {
849     const TargetData &TD = Asm->TM.getTargetData();
850     
851     if (TD.isBigEndian()) {
852       EmitInt32(unsigned(Value >> 32)); O << "\n";
853       EmitInt32(unsigned(Value));
854     } else {
855       EmitInt32(unsigned(Value)); O << "\n";
856       EmitInt32(unsigned(Value >> 32));
857     }
858   }
859 }
860
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
865     << "\"";
866   for (unsigned i = 0, N = String.size(); i < N; ++i) {
867     unsigned char C = String[i];
868     
869     if (!isascii(C) || iscntrl(C)) {
870       switch(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;
876       default:
877         O << '\\';
878         O << char('0' + (C >> 6));
879         O << char('0' + (C >> 3));
880         O << char('0' + (C >> 0));
881         break;
882       }
883     } else if (C == '\"') {
884       O << "\\\"";
885     } else if (C == '\'') {
886       O << "\\\'";
887     } else {
888      O << C;
889     }
890   }
891   O << "\\0\"";
892 }
893
894 /// PrintLabelName - Print label name in form used by Dwarf writer.
895 ///
896 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
897   O << Asm->PrivateGlobalPrefix
898     << "debug_"
899     << Tag;
900   if (Number) O << Number;
901 }
902
903 /// EmitLabel - Emit location label for internal use by Dwarf.
904 ///
905 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
906   PrintLabelName(Tag, Number);
907   O << ":\n";
908 }
909
910 /// EmitReference - Emit a reference to a label.
911 ///
912 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
913   if (AddressSize == 4)
914     O << Asm->Data32bitsDirective;
915   else
916     O << Asm->Data64bitsDirective;
917     
918   PrintLabelName(Tag, Number);
919 }
920 void DwarfWriter::EmitReference(const std::string &Name) const {
921   if (AddressSize == 4)
922     O << Asm->Data32bitsDirective;
923   else
924     O << Asm->Data64bitsDirective;
925     
926   O << Name;
927 }
928
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 {
934   if (needsSet) {
935     static unsigned SetCounter = 0;
936     
937     O << "\t.set\t";
938     PrintLabelName("set", SetCounter);
939     O << ",";
940     PrintLabelName(TagHi, NumberHi);
941     O << "-";
942     PrintLabelName(TagLo, NumberLo);
943     O << "\n";
944     
945     if (AddressSize == sizeof(int32_t))
946       O << Asm->Data32bitsDirective;
947     else
948       O << Asm->Data64bitsDirective;
949       
950     PrintLabelName("set", SetCounter);
951     
952     ++SetCounter;
953   } else {
954     if (AddressSize == sizeof(int32_t))
955       O << Asm->Data32bitsDirective;
956     else
957       O << Asm->Data64bitsDirective;
958       
959     PrintLabelName(TagHi, NumberHi);
960     O << "-";
961     PrintLabelName(TagLo, NumberLo);
962   }
963 }
964
965 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
966 ///  
967 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
968   return Abbreviations.insert(*Abbrev);
969 }
970
971 /// NewString - Add a string to the constant pool and returns a label.
972 ///
973 DWLabel DwarfWriter::NewString(const std::string &String) {
974   unsigned StringID = StringPool.insert(String);
975   return DWLabel("string", StringID);
976 }
977
978 /// NewBasicType - Creates a new basic type if necessary, then adds to the
979 /// owner.
980 /// FIXME - Should never be needed.
981 DIE *DwarfWriter::NewBasicType(DIE *Context, Type *Ty) {
982   DIE *&Slot = TypeToDieMap[Ty];
983   if (Slot) return Slot;
984   
985   const char *Name;
986   unsigned Size;
987   unsigned Encoding = 0;
988   
989   switch (Ty->getTypeID()) {
990   case Type::UByteTyID:
991     Name = "unsigned char";
992     Size = 1;
993     Encoding = DW_ATE_unsigned_char;
994     break;
995   case Type::SByteTyID:
996     Name = "char";
997     Size = 1;
998     Encoding = DW_ATE_signed_char;
999     break;
1000   case Type::UShortTyID:
1001     Name = "unsigned short";
1002     Size = 2;
1003     Encoding = DW_ATE_unsigned;
1004     break;
1005   case Type::ShortTyID:
1006     Name = "short";
1007     Size = 2;
1008     Encoding = DW_ATE_signed;
1009     break;
1010   case Type::UIntTyID:
1011     Name = "unsigned int";
1012     Size = 4;
1013     Encoding = DW_ATE_unsigned;
1014     break;
1015   case Type::IntTyID:
1016     Name = "int";
1017     Size = 4;
1018     Encoding = DW_ATE_signed;
1019     break;
1020   case Type::ULongTyID:
1021     Name = "unsigned long long";
1022     Size = 7;
1023     Encoding = DW_ATE_unsigned;
1024     break;
1025   case Type::LongTyID:
1026     Name = "long long";
1027     Size = 7;
1028     Encoding = DW_ATE_signed;
1029     break;
1030   case Type::FloatTyID:
1031     Name = "float";
1032     Size = 4;
1033     Encoding = DW_ATE_float;
1034     break;
1035   case Type::DoubleTyID:
1036     Name = "double";
1037     Size = 8;
1038     Encoding = DW_ATE_float;
1039     break;
1040   default: 
1041     // FIXME - handle more complex types.
1042     Name = "unknown";
1043     Size = 1;
1044     Encoding = DW_ATE_address;
1045     break;
1046   }
1047   
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);
1053   
1054   // Add to context.
1055   Context->AddChild(Slot);
1056   
1057   return Slot;
1058 }
1059
1060 /// NewType - Create a new type DIE.
1061 ///
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);
1065   
1066   // FIXME - Should handle other contexts that compile units.
1067
1068   // Check for pre-existence.
1069   DIE *&Slot = DescToDieMap[TyDesc];
1070   if (Slot) return Slot;
1071
1072   // Get core information.
1073   const std::string &Name = TyDesc->getName();
1074   uint64_t Size = TyDesc->getSize() >> 3;
1075   
1076   DIE *Ty = NULL;
1077   
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());
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(Context, FromTy));
1090     }
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();
1095     
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));
1101       }
1102       // Don't emit size attribute.
1103       Size = 0;
1104       
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);
1109       // Add to context.
1110       Context->AddChild(IndexTy);
1111     
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);
1118         
1119         // If a range is available.
1120         if (Lo != Hi) {
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);
1125         }
1126         Ty->AddChild(Subrange);
1127       }
1128       
1129       break;
1130     }
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();
1142    
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);
1151         }
1152         if (TypeDesc *FromTy = MemberDesc->getFromType()) {
1153            Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
1154                               NewType(Context, FromTy));
1155         }
1156         // FIXME - Punt on the Address.
1157         Member->AddUInt(DW_AT_data_member_location, DW_FORM_block1,
1158                                                     Offset >> 3);
1159         Ty->AddChild(Member);
1160       }
1161       break;
1162     }
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);
1173       }
1174
1175       break;
1176     }
1177     default: break;
1178     }
1179   }
1180   
1181   assert(Ty && "Type not supported yet");
1182  
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);
1194   }
1195
1196   // Add to context owner.
1197   Context->AddChild(Ty);
1198   
1199   return Slot;
1200 }
1201
1202 /// NewCompileUnit - Create new compile unit and it's die.
1203 ///
1204 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
1205                                          unsigned ID) {
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());
1215   
1216   // Add die to descriptor map.
1217   DescToDieMap[UnitDesc] = Die;
1218   
1219   // Construct compile unit.
1220   CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
1221   
1222   // Add Unit to compile unit map.
1223   DescToUnitMap[UnitDesc] = Unit;
1224   
1225   return Unit;
1226 }
1227
1228 /// FindCompileUnit - Get the compile unit for the given descriptor.
1229 ///
1230 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
1231   CompileUnit *Unit = DescToUnitMap[UnitDesc];
1232   assert(Unit && "Missing compile unit.");
1233   return Unit;
1234 }
1235
1236 /// NewGlobalVariable - Add a new global variable DIE.
1237 ///
1238 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
1239   // Check for pre-existence.
1240   DIE *&Slot = DescToDieMap[GVD];
1241   if (Slot) return Slot;
1242   
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);
1250
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();
1255   
1256   // Get the global's type.
1257   DIE *Type = NewType(Unit->getDie(), GVD->getTypeDesc()); 
1258
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);
1268   
1269   // Add to map.
1270   Slot = VariableDie;
1271  
1272   // Add to context owner.
1273   Unit->getDie()->AddChild(VariableDie);
1274   
1275   // Expose as global.
1276   // FIXME - need to check external flag.
1277   Unit->AddGlobal(Name, VariableDie);
1278   
1279   return VariableDie;
1280 }
1281
1282 /// NewSubprogram - Add a new subprogram DIE.
1283 ///
1284 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
1285   // Check for pre-existence.
1286   DIE *&Slot = DescToDieMap[SPD];
1287   if (Slot) return Slot;
1288   
1289   // Get the compile unit context.
1290   CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
1291   CompileUnit *Unit = FindCompileUnit(UnitDesc);
1292
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.
1297   unsigned Line = 1;
1298   
1299   // FIXME - faking the type for the time being.
1300   DIE *Type = NewBasicType(Unit->getDie(), Type::IntTy); 
1301                                     
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);
1308   
1309   // Add to map.
1310   Slot = SubprogramDie;
1311  
1312   // Add to context owner.
1313   Unit->getDie()->AddChild(SubprogramDie);
1314   
1315   // Expose as global.
1316   Unit->AddGlobal(Name, SubprogramDie);
1317   
1318   return SubprogramDie;
1319 }
1320
1321 /// EmitInitial - Emit initial Dwarf declarations.  This is necessary for cc
1322 /// tools to recognize the object file contains Dwarf information.
1323 ///
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);
1349
1350   Asm->SwitchSection(TextSection, 0);
1351   EmitLabel("text_begin", 0);
1352   Asm->SwitchSection(DataSection, 0);
1353   EmitLabel("data_begin", 0);
1354 }
1355
1356 /// EmitDIE - Recusively Emits a debug information entry.
1357 ///
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];
1362   
1363   O << "\n";
1364
1365   // Emit the code (index) for the abbreviation.
1366   EmitULEB128Bytes(AbbrevID);
1367   EOL(std::string("Abbrev [" +
1368       utostr(AbbrevID) +
1369       "] 0x" + utohexstr(Die->getOffset()) +
1370       ":0x" + utohexstr(Die->getSize()) + " " +
1371       TagString(Abbrev.getTag())));
1372   
1373   const std::vector<DIEValue *> &Values = Die->getValues();
1374   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1375   
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)");
1381     
1382     switch (Attr) {
1383     case DW_AT_sibling: {
1384       EmitInt32(Die->SiblingOffset());
1385       break;
1386     }
1387     default: {
1388       // Emit an attribute using the defined form.
1389       Values[i]->EmitValue(*this, Form);
1390       break;
1391     }
1392     }
1393     
1394     EOL(AttributeString(Attr));
1395   }
1396   
1397   // Emit the DIE children if any.
1398   if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1399     const std::vector<DIE *> &Children = Die->getChildren();
1400     
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]);
1405     }
1406     
1407     EmitInt8(0); EOL("End Of Children Mark");
1408   }
1409 }
1410
1411 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
1412 ///
1413 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset) {
1414   // Record the abbreviation.
1415   Die->Complete(*this);
1416   
1417   // Get the abbreviation for this DIE.
1418   unsigned AbbrevID = Die->getAbbrevID();
1419   const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
1420
1421   // Set DIE offset
1422   Die->setOffset(Offset);
1423   
1424   // Start the size with the size of abbreviation code.
1425   Offset += SizeULEB128(AbbrevID);
1426   
1427   const std::vector<DIEValue *> &Values = Die->getValues();
1428   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
1429
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());
1434   }
1435   
1436   // Emit the DIE children if any.
1437   if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
1438     const std::vector<DIE *> &Children = Die->getChildren();
1439     
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);
1444     }
1445     
1446     // End of children marker.
1447     Offset += sizeof(int8_t);
1448   }
1449
1450   Die->setSize(Offset - Die->getOffset());
1451   return Offset;
1452 }
1453
1454 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
1455 ///
1456 void DwarfWriter::SizeAndOffsets() {
1457   
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)
1467     
1468       SizeAndOffsetDie(Unit->getDie(), Offset);
1469     }
1470   }
1471 }
1472
1473 /// EmitDebugInfo - Emit the debug info section.
1474 ///
1475 void DwarfWriter::EmitDebugInfo() const {
1476   // Start debug info section.
1477   Asm->SwitchSection(DwarfInfoSection, 0);
1478   
1479   // Process each compile unit.
1480   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1481     CompileUnit *Unit = CompileUnits[i];
1482     
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)
1492                              
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)");
1497     
1498       EmitDIE(Die);
1499       EmitLabel("info_end", Unit->getID());
1500     }
1501     
1502     O << "\n";
1503   }
1504 }
1505
1506 /// EmitAbbreviations - Emit the abbreviation section.
1507 ///
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);
1513     
1514     EmitLabel("abbrev_begin", 0);
1515     
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];
1521       
1522       // Emit the abbrevations code (base 1 index.)
1523       EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
1524       
1525       // Emit the abbreviations data.
1526       Abbrev.Emit(*this);
1527   
1528       O << "\n";
1529     }
1530     
1531     EmitLabel("abbrev_end", 0);
1532   
1533     O << "\n";
1534   }
1535 }
1536
1537 /// EmitDebugLines - Emit source line information.
1538 ///
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;
1544
1545   // Start the dwarf line section.
1546   Asm->SwitchSection(DwarfLineSection, 0);
1547   
1548   // Construct the section header.
1549   
1550   EmitDifference("line_end", 0, "line_begin", 0);
1551   EOL("Length of Source Line Info");
1552   EmitLabel("line_begin", 0);
1553   
1554   EmitInt16(DWARF_VERSION); EOL("DWARF version number");
1555   
1556   EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
1557   EOL("Prolog Length");
1558   EmitLabel("line_prolog_begin", 0);
1559   
1560   EmitInt8(1); EOL("Minimum Instruction Length");
1561
1562   EmitInt8(1); EOL("Default is_stmt_start flag");
1563
1564   EmitInt8(MinLineDelta);  EOL("Line Base Value (Special Opcodes)");
1565   
1566   EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
1567
1568   EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
1569   
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");
1580
1581   const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
1582   const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
1583
1584   // Emit directories.
1585   for (unsigned DirectoryID = 1, NDID = Directories.size();
1586                 DirectoryID <= NDID; ++DirectoryID) {
1587     EmitString(Directories[DirectoryID]); EOL("Directory");
1588   }
1589   EmitInt8(0); EOL("End of directories");
1590   
1591   // Emit files.
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");
1599   }
1600   EmitInt8(0); EOL("End of files");
1601   
1602   EmitLabel("line_prolog_end", 0);
1603   
1604   // Emit line information
1605   const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
1606   
1607   // Dwarf assumes we start with first line of first source file.
1608   unsigned Source = 1;
1609   unsigned Line = 1;
1610   
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];
1614     
1615     if (DwarfVerbose) {
1616       unsigned SourceID = LineInfo->getSourceID();
1617       const SourceFileInfo &SourceFile = SourceFiles[SourceID];
1618       unsigned DirectoryID = SourceFile.getDirectoryID();
1619       O << "\t"
1620         << Asm->CommentString << " "
1621         << Directories[DirectoryID]
1622         << SourceFile.getName() << ":"
1623         << LineInfo->getLine() << "\n"; 
1624     }
1625
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");
1631     
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");
1637     }
1638     
1639     // If change of line.
1640     if (Line != LineInfo->getLine()) {
1641       // Determine offset.
1642       int Offset = LineInfo->getLine() - Line;
1643       int Delta = Offset - MinLineDelta;
1644       
1645       // Update line.
1646       Line = LineInfo->getLine();
1647       
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");
1652       } else {
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");
1657       }
1658     } else {
1659       // Copy the previous row (different address or source)
1660       EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
1661     }
1662   }
1663
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");
1669
1670   // Mark end of matrix.
1671   EmitInt8(0); EOL("DW_LNE_end_sequence");
1672   EmitULEB128Bytes(1);  O << "\n";
1673   EmitInt8(1); O << "\n";
1674   
1675   EmitLabel("line_end", 0);
1676   
1677   O << "\n";
1678 }
1679   
1680 /// EmitDebugFrame - Emit visible names into a debug frame section.
1681 ///
1682 void DwarfWriter::EmitDebugFrame() {
1683   // FIXME - Should be per frame
1684 }
1685
1686 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
1687 ///
1688 void DwarfWriter::EmitDebugPubNames() {
1689   // Start the dwarf pubnames section.
1690   Asm->SwitchSection(DwarfPubNamesSection, 0);
1691     
1692   // Process each compile unit.
1693   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1694     CompileUnit *Unit = CompileUnits[i];
1695     
1696     if (Unit->hasContent()) {
1697       EmitDifference("pubnames_end", Unit->getID(),
1698                      "pubnames_begin", Unit->getID());
1699       EOL("Length of Public Names Info");
1700       
1701       EmitLabel("pubnames_begin", Unit->getID());
1702       
1703       EmitInt16(DWARF_VERSION); EOL("DWARF Version");
1704       
1705       EmitReference("info_begin", Unit->getID());
1706       EOL("Offset of Compilation Unit Info");
1707
1708       EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
1709       EOL("Compilation Unit Length");
1710       
1711       std::map<std::string, DIE *> &Globals = Unit->getGlobals();
1712       
1713       for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
1714                                                   GE = Globals.end();
1715            GI != GE; ++GI) {
1716         const std::string &Name = GI->first;
1717         DIE * Entity = GI->second;
1718         
1719         EmitInt32(Entity->getOffset()); EOL("DIE offset");
1720         EmitString(Name); EOL("External Name");
1721       }
1722     
1723       EmitInt32(0); EOL("End Mark");
1724       EmitLabel("pubnames_end", Unit->getID());
1725     
1726       O << "\n";
1727     }
1728   }
1729 }
1730
1731 /// EmitDebugStr - Emit visible names into a debug str section.
1732 ///
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);
1738     
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";
1747     }
1748   
1749     O << "\n";
1750   }
1751 }
1752
1753 /// EmitDebugLoc - Emit visible names into a debug loc section.
1754 ///
1755 void DwarfWriter::EmitDebugLoc() {
1756   // Start the dwarf loc section.
1757   Asm->SwitchSection(DwarfLocSection, 0);
1758   
1759   O << "\n";
1760 }
1761
1762 /// EmitDebugARanges - Emit visible names into a debug aranges section.
1763 ///
1764 void DwarfWriter::EmitDebugARanges() {
1765   // Start the dwarf aranges section.
1766   Asm->SwitchSection(DwarfARangesSection, 0);
1767   
1768   // FIXME - Mock up
1769 #if 0
1770   // Process each compile unit.
1771   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1772     CompileUnit *Unit = CompileUnits[i];
1773     
1774     if (Unit->hasContent()) {
1775       // Don't include size of length
1776       EmitInt32(0x1c); EOL("Length of Address Ranges Info");
1777       
1778       EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
1779       
1780       EmitReference("info_begin", Unit->getID());
1781       EOL("Offset of Compilation Unit Info");
1782
1783       EmitInt8(AddressSize); EOL("Size of Address");
1784
1785       EmitInt8(0); EOL("Size of Segment Descriptor");
1786
1787       EmitInt16(0);  EOL("Pad (1)");
1788       EmitInt16(0);  EOL("Pad (2)");
1789
1790       // Range 1
1791       EmitReference("text_begin", 0); EOL("Address");
1792       EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
1793
1794       EmitInt32(0); EOL("EOM (1)");
1795       EmitInt32(0); EOL("EOM (2)");
1796       
1797       O << "\n";
1798     }
1799   }
1800 #endif
1801 }
1802
1803 /// EmitDebugRanges - Emit visible names into a debug ranges section.
1804 ///
1805 void DwarfWriter::EmitDebugRanges() {
1806   // Start the dwarf ranges section.
1807   Asm->SwitchSection(DwarfRangesSection, 0);
1808   
1809   O << "\n";
1810 }
1811
1812 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
1813 ///
1814 void DwarfWriter::EmitDebugMacInfo() {
1815   // Start the dwarf macinfo section.
1816   Asm->SwitchSection(DwarfMacInfoSection, 0);
1817   
1818   O << "\n";
1819 }
1820
1821 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
1822 /// header file.
1823 void DwarfWriter::ConstructCompileUnitDIEs() {
1824   const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
1825   
1826   for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
1827     CompileUnit *Unit = NewCompileUnit(CUW[i], i);
1828     CompileUnits.push_back(Unit);
1829   }
1830 }
1831
1832 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
1833 /// variables.
1834 void DwarfWriter::ConstructGlobalDIEs(Module &M) {
1835   std::vector<GlobalVariableDesc *> GlobalVariables =
1836                        DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(M);
1837   
1838   for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
1839     GlobalVariableDesc *GVD = GlobalVariables[i];
1840     NewGlobalVariable(GVD);
1841   }
1842 }
1843
1844 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
1845 /// subprograms.
1846 void DwarfWriter::ConstructSubprogramDIEs(Module &M) {
1847   std::vector<SubprogramDesc *> Subprograms =
1848                            DebugInfo->getAnchoredDescriptors<SubprogramDesc>(M);
1849   
1850   for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
1851     SubprogramDesc *SPD = Subprograms[i];
1852     NewSubprogram(SPD);
1853   }
1854 }
1855
1856 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
1857 ///
1858 bool DwarfWriter::ShouldEmitDwarf() {
1859   // Check if debug info is present.
1860   if (!DebugInfo || !DebugInfo->hasInfo()) return false;
1861   
1862   // Make sure initial declarations are made.
1863   if (!didInitial) {
1864     EmitInitial();
1865     didInitial = true;
1866   }
1867   
1868   // Okay to emit.
1869   return true;
1870 }
1871
1872 //===----------------------------------------------------------------------===//
1873 // Main entry points.
1874 //
1875   
1876 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
1877 : O(OS)
1878 , Asm(A)
1879 , DebugInfo(NULL)
1880 , didInitial(false)
1881 , CompileUnits()
1882 , Abbreviations()
1883 , StringPool()
1884 , DescToUnitMap()
1885 , DescToDieMap()
1886 , TypeToDieMap()
1887 , AddressSize(sizeof(int32_t))
1888 , hasLEB128(false)
1889 , hasDotLoc(false)
1890 , hasDotFile(false)
1891 , needsSet(false)
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")
1905 {}
1906 DwarfWriter::~DwarfWriter() {
1907   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
1908     delete CompileUnits[i];
1909   }
1910 }
1911
1912 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
1913 ///
1914 void DwarfWriter::BeginModule(Module &M) {
1915   if (!ShouldEmitDwarf()) return;
1916   EOL("Dwarf Begin Module");
1917 }
1918
1919 /// EndModule - Emit all Dwarf sections that should come after the content.
1920 ///
1921 void DwarfWriter::EndModule(Module &M) {
1922   if (!ShouldEmitDwarf()) return;
1923   EOL("Dwarf End Module");
1924   
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);
1930   
1931   // Create all the compile unit DIEs.
1932   ConstructCompileUnitDIEs();
1933   
1934   // Create DIEs for each of the externally visible global variables.
1935   ConstructGlobalDIEs(M);
1936
1937   // Create DIEs for each of the externally visible subprograms.
1938   ConstructSubprogramDIEs(M);
1939   
1940   // Compute DIE offsets and sizes.
1941   SizeAndOffsets();
1942   
1943   // Emit all the DIEs into a debug info section
1944   EmitDebugInfo();
1945   
1946   // Corresponding abbreviations into a abbrev section.
1947   EmitAbbreviations();
1948   
1949   // Emit source line correspondence into a debug line section.
1950   EmitDebugLines();
1951   
1952   // Emit info into a debug frame section.
1953   // EmitDebugFrame();
1954   
1955   // Emit info into a debug pubnames section.
1956   EmitDebugPubNames();
1957   
1958   // Emit info into a debug str section.
1959   EmitDebugStr();
1960   
1961   // Emit info into a debug loc section.
1962   EmitDebugLoc();
1963   
1964   // Emit info into a debug aranges section.
1965   EmitDebugARanges();
1966   
1967   // Emit info into a debug ranges section.
1968   EmitDebugRanges();
1969   
1970   // Emit info into a debug macinfo section.
1971   EmitDebugMacInfo();
1972 }
1973
1974 /// BeginFunction - Gather pre-function debug information.
1975 ///
1976 void DwarfWriter::BeginFunction(MachineFunction &MF) {
1977   if (!ShouldEmitDwarf()) return;
1978   EOL("Dwarf Begin Function");
1979 }
1980
1981 /// EndFunction - Gather and emit post-function debug information.
1982 ///
1983 void DwarfWriter::EndFunction(MachineFunction &MF) {
1984   if (!ShouldEmitDwarf()) return;
1985   EOL("Dwarf End Function");
1986 }