LLVM API Documentation

DwarfWriter.cpp

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/DwarfWriter.cpp - Dwarf Framework ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by James M. Laskey and is distributed under the
00006 // University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains support for writing dwarf debug info into asm files.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/DwarfWriter.h"
00015 
00016 #include "llvm/ADT/StringExtras.h"
00017 #include "llvm/Module.h"
00018 #include "llvm/Type.h"
00019 #include "llvm/CodeGen/AsmPrinter.h"
00020 #include "llvm/CodeGen/MachineDebugInfo.h"
00021 #include "llvm/CodeGen/MachineFrameInfo.h"
00022 #include "llvm/CodeGen/MachineLocation.h"
00023 #include "llvm/Support/Dwarf.h"
00024 #include "llvm/Support/CommandLine.h"
00025 #include "llvm/Support/Mangler.h"
00026 #include "llvm/Target/MRegisterInfo.h"
00027 #include "llvm/Target/TargetMachine.h"
00028 #include "llvm/Target/TargetFrameInfo.h"
00029 
00030 #include <iostream>
00031 
00032 using namespace llvm;
00033 using namespace llvm::dwarf;
00034 
00035 static cl::opt<bool>
00036 DwarfVerbose("dwarf-verbose", cl::Hidden,
00037                                 cl::desc("Add comments to Dwarf directives."));
00038 
00039 namespace llvm {
00040 
00041 //===----------------------------------------------------------------------===//
00042 // Forward declarations.
00043 //
00044 class DIE;
00045 
00046 //===----------------------------------------------------------------------===//
00047 // CompileUnit - This dwarf writer support class manages information associate
00048 // with a source file.
00049 class CompileUnit {
00050 private:
00051   CompileUnitDesc *Desc;                // Compile unit debug descriptor.
00052   unsigned ID;                          // File ID for source.
00053   DIE *Die;                             // Compile unit debug information entry.
00054   std::map<std::string, DIE *> Globals; // A map of globally visible named
00055                                         // entities for this unit.
00056   std::map<DebugInfoDesc *, DIE *> DescToDieMap;
00057                                         // Tracks the mapping of unit level
00058                                         // debug informaton descriptors to debug
00059                                         // information entries.
00060 
00061 public:
00062   CompileUnit(CompileUnitDesc *CUD, unsigned I, DIE *D)
00063   : Desc(CUD)
00064   , ID(I)
00065   , Die(D)
00066   , Globals()
00067   , DescToDieMap()
00068   {}
00069   
00070   ~CompileUnit();
00071   
00072   // Accessors.
00073   CompileUnitDesc *getDesc() const { return Desc; }
00074   unsigned getID()           const { return ID; }
00075   DIE* getDie()              const { return Die; }
00076   std::map<std::string, DIE *> &getGlobals() { return Globals; }
00077   
00078   /// hasContent - Return true if this compile unit has something to write out.
00079   ///
00080   bool hasContent() const;
00081   
00082   /// AddGlobal - Add a new global entity to the compile unit.
00083   ///
00084   void AddGlobal(const std::string &Name, DIE *Die);
00085   
00086   /// getDieMapSlotFor - Returns the debug information entry map slot for the
00087   /// specified debug descriptor.
00088   DIE *&getDieMapSlotFor(DebugInfoDesc *DD) {
00089     return DescToDieMap[DD];
00090   }
00091 };
00092 
00093 //===----------------------------------------------------------------------===//
00094 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
00095 // Dwarf abbreviation.
00096 class DIEAbbrevData {
00097 private:
00098   unsigned Attribute;                 // Dwarf attribute code.
00099   unsigned Form;                      // Dwarf form code.
00100   
00101 public:
00102   DIEAbbrevData(unsigned A, unsigned F)
00103   : Attribute(A)
00104   , Form(F)
00105   {}
00106   
00107   // Accessors.
00108   unsigned getAttribute() const { return Attribute; }
00109   unsigned getForm()      const { return Form; }
00110   
00111   /// operator== - Used by DIEAbbrev to locate entry.
00112   ///
00113   bool operator==(const DIEAbbrevData &DAD) const {
00114     return Attribute == DAD.Attribute && Form == DAD.Form;
00115   }
00116 
00117   /// operator!= - Used by DIEAbbrev to locate entry.
00118   ///
00119   bool operator!=(const DIEAbbrevData &DAD) const {
00120     return Attribute != DAD.Attribute || Form != DAD.Form;
00121   }
00122   
00123   /// operator< - Used by DIEAbbrev to locate entry.
00124   ///
00125   bool operator<(const DIEAbbrevData &DAD) const {
00126     return Attribute < DAD.Attribute ||
00127           (Attribute == DAD.Attribute && Form < DAD.Form);
00128   }
00129 };
00130 
00131 //===----------------------------------------------------------------------===//
00132 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
00133 // information object.
00134 class DIEAbbrev {
00135 private:
00136   unsigned Tag;                       // Dwarf tag code.
00137   unsigned ChildrenFlag;              // Dwarf children flag.
00138   std::vector<DIEAbbrevData> Data;    // Raw data bytes for abbreviation.
00139 
00140 public:
00141 
00142   DIEAbbrev(unsigned T, unsigned C)
00143   : Tag(T)
00144   , ChildrenFlag(C)
00145   , Data()
00146   {}
00147   ~DIEAbbrev() {}
00148   
00149   // Accessors.
00150   unsigned getTag()                           const { return Tag; }
00151   unsigned getChildrenFlag()                  const { return ChildrenFlag; }
00152   const std::vector<DIEAbbrevData> &getData() const { return Data; }
00153   void setChildrenFlag(unsigned CF)                 { ChildrenFlag = CF; }
00154 
00155   /// operator== - Used by UniqueVector to locate entry.
00156   ///
00157   bool operator==(const DIEAbbrev &DA) const;
00158 
00159   /// operator< - Used by UniqueVector to locate entry.
00160   ///
00161   bool operator<(const DIEAbbrev &DA) const;
00162 
00163   /// AddAttribute - Adds another set of attribute information to the
00164   /// abbreviation.
00165   void AddAttribute(unsigned Attribute, unsigned Form) {
00166     Data.push_back(DIEAbbrevData(Attribute, Form));
00167   }
00168   
00169   /// AddFirstAttribute - Adds a set of attribute information to the front
00170   /// of the abbreviation.
00171   void AddFirstAttribute(unsigned Attribute, unsigned Form) {
00172     Data.insert(Data.begin(), DIEAbbrevData(Attribute, Form));
00173   }
00174   
00175   /// Emit - Print the abbreviation using the specified Dwarf writer.
00176   ///
00177   void Emit(const DwarfWriter &DW) const; 
00178       
00179 #ifndef NDEBUG
00180   void print(std::ostream &O);
00181   void dump();
00182 #endif
00183 };
00184 
00185 //===----------------------------------------------------------------------===//
00186 // DIEValue - A debug information entry value.
00187 //
00188 class DIEValue {
00189 public:
00190   enum {
00191     isInteger,
00192     isString,
00193     isLabel,
00194     isAsIsLabel,
00195     isDelta,
00196     isEntry,
00197     isBlock
00198   };
00199   
00200   unsigned Type;                      // Type of the value
00201   
00202   DIEValue(unsigned T) : Type(T) {}
00203   virtual ~DIEValue() {}
00204   
00205   // Implement isa/cast/dyncast.
00206   static bool classof(const DIEValue *) { return true; }
00207   
00208   /// EmitValue - Emit value via the Dwarf writer.
00209   ///
00210   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
00211   
00212   /// SizeOf - Return the size of a value in bytes.
00213   ///
00214   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
00215 };
00216 
00217 //===----------------------------------------------------------------------===//
00218 // DWInteger - An integer value DIE.
00219 // 
00220 class DIEInteger : public DIEValue {
00221 private:
00222   uint64_t Integer;
00223   
00224 public:
00225   DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
00226 
00227   // Implement isa/cast/dyncast.
00228   static bool classof(const DIEInteger *) { return true; }
00229   static bool classof(const DIEValue *I)  { return I->Type == isInteger; }
00230   
00231   /// BestForm - Choose the best form for integer.
00232   ///
00233   unsigned BestForm(bool IsSigned);
00234 
00235   /// EmitValue - Emit integer of appropriate size.
00236   ///
00237   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00238   
00239   /// SizeOf - Determine size of integer value in bytes.
00240   ///
00241   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00242 };
00243 
00244 //===----------------------------------------------------------------------===//
00245 // DIEString - A string value DIE.
00246 // 
00247 struct DIEString : public DIEValue {
00248   const std::string String;
00249   
00250   DIEString(const std::string &S) : DIEValue(isString), String(S) {}
00251 
00252   // Implement isa/cast/dyncast.
00253   static bool classof(const DIEString *) { return true; }
00254   static bool classof(const DIEValue *S) { return S->Type == isString; }
00255   
00256   /// EmitValue - Emit string value.
00257   ///
00258   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00259   
00260   /// SizeOf - Determine size of string value in bytes.
00261   ///
00262   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00263 };
00264 
00265 //===----------------------------------------------------------------------===//
00266 // DIEDwarfLabel - A Dwarf internal label expression DIE.
00267 //
00268 struct DIEDwarfLabel : public DIEValue {
00269   const DWLabel Label;
00270   
00271   DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
00272 
00273   // Implement isa/cast/dyncast.
00274   static bool classof(const DIEDwarfLabel *)  { return true; }
00275   static bool classof(const DIEValue *L) { return L->Type == isLabel; }
00276   
00277   /// EmitValue - Emit label value.
00278   ///
00279   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00280   
00281   /// SizeOf - Determine size of label value in bytes.
00282   ///
00283   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00284 };
00285 
00286 
00287 //===----------------------------------------------------------------------===//
00288 // DIEObjectLabel - A label to an object in code or data.
00289 //
00290 struct DIEObjectLabel : public DIEValue {
00291   const std::string Label;
00292   
00293   DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
00294 
00295   // Implement isa/cast/dyncast.
00296   static bool classof(const DIEObjectLabel *) { return true; }
00297   static bool classof(const DIEValue *L)    { return L->Type == isAsIsLabel; }
00298   
00299   /// EmitValue - Emit label value.
00300   ///
00301   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00302   
00303   /// SizeOf - Determine size of label value in bytes.
00304   ///
00305   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00306 };
00307 
00308 //===----------------------------------------------------------------------===//
00309 // DIEDelta - A simple label difference DIE.
00310 // 
00311 struct DIEDelta : public DIEValue {
00312   const DWLabel LabelHi;
00313   const DWLabel LabelLo;
00314   
00315   DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
00316   : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
00317 
00318   // Implement isa/cast/dyncast.
00319   static bool classof(const DIEDelta *)  { return true; }
00320   static bool classof(const DIEValue *D) { return D->Type == isDelta; }
00321   
00322   /// EmitValue - Emit delta value.
00323   ///
00324   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00325   
00326   /// SizeOf - Determine size of delta value in bytes.
00327   ///
00328   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00329 };
00330 
00331 //===----------------------------------------------------------------------===//
00332 // DIEntry - A pointer to a debug information entry.
00333 // 
00334 struct DIEntry : public DIEValue {
00335   DIE *Entry;
00336   
00337   DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
00338 
00339   // Implement isa/cast/dyncast.
00340   static bool classof(const DIEntry *)   { return true; }
00341   static bool classof(const DIEValue *E) { return E->Type == isEntry; }
00342   
00343   /// EmitValue - Emit debug information entry offset.
00344   ///
00345   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00346   
00347   /// SizeOf - Determine size of debug information entry in bytes.
00348   ///
00349   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00350 };
00351 
00352 //===----------------------------------------------------------------------===//
00353 // DIEBlock - A block of values.  Primarily used for location expressions.
00354 //
00355 struct DIEBlock : public DIEValue {
00356   unsigned Size;                        // Size in bytes excluding size header.
00357   std::vector<unsigned> Forms;          // Data forms.
00358   std::vector<DIEValue *> Values;       // Block values.
00359   
00360   DIEBlock()
00361   : DIEValue(isBlock)
00362   , Size(0)
00363   , Forms()
00364   , Values()
00365   {}
00366   ~DIEBlock();
00367 
00368   // Implement isa/cast/dyncast.
00369   static bool classof(const DIEBlock *)  { return true; }
00370   static bool classof(const DIEValue *E) { return E->Type == isBlock; }
00371   
00372   /// ComputeSize - calculate the size of the block.
00373   ///
00374   unsigned ComputeSize(DwarfWriter &DW);
00375   
00376   /// BestForm - Choose the best form for data.
00377   ///
00378   unsigned BestForm();
00379 
00380   /// EmitValue - Emit block data.
00381   ///
00382   virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
00383   
00384   /// SizeOf - Determine size of block data in bytes.
00385   ///
00386   virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
00387 
00388   /// AddUInt - Add an unsigned integer value.
00389   ///
00390   void AddUInt(unsigned Form, uint64_t Integer);
00391 
00392   /// AddSInt - Add an signed integer value.
00393   ///
00394   void AddSInt(unsigned Form, int64_t Integer);
00395       
00396   /// AddString - Add a std::string value.
00397   ///
00398   void AddString(unsigned Form, const std::string &String);
00399       
00400   /// AddLabel - Add a Dwarf label value.
00401   ///
00402   void AddLabel(unsigned Form, const DWLabel &Label);
00403       
00404   /// AddObjectLabel - Add a non-Dwarf label value.
00405   ///
00406   void AddObjectLabel(unsigned Form, const std::string &Label);
00407       
00408   /// AddDelta - Add a label delta value.
00409   ///
00410   void AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo);
00411       
00412   /// AddDIEntry - Add a DIE value.
00413   ///
00414   void AddDIEntry(unsigned Form, DIE *Entry);
00415 
00416 };
00417 
00418 //===----------------------------------------------------------------------===//
00419 // DIE - A structured debug information entry.  Has an abbreviation which
00420 // describes it's organization.
00421 class DIE {
00422 private:
00423   DIEAbbrev *Abbrev;                    // Temporary buffer for abbreviation.
00424   unsigned AbbrevID;                    // Decribing abbreviation ID.
00425   unsigned Offset;                      // Offset in debug info section.
00426   unsigned Size;                        // Size of instance + children.
00427   std::vector<DIE *> Children;          // Children DIEs.
00428   std::vector<DIEValue *> Values;       // Attributes values.
00429   
00430 public:
00431   DIE(unsigned Tag);
00432   ~DIE();
00433   
00434   // Accessors.
00435   unsigned   getAbbrevID()                   const { return AbbrevID; }
00436   unsigned   getOffset()                     const { return Offset; }
00437   unsigned   getSize()                       const { return Size; }
00438   const std::vector<DIE *> &getChildren()    const { return Children; }
00439   const std::vector<DIEValue *> &getValues() const { return Values; }
00440   void setOffset(unsigned O)                 { Offset = O; }
00441   void setSize(unsigned S)                   { Size = S; }
00442   
00443   /// SiblingOffset - Return the offset of the debug information entry's
00444   /// sibling.
00445   unsigned SiblingOffset() const { return Offset + Size; }
00446   
00447   /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
00448   ///
00449   void AddSiblingOffset();
00450 
00451   /// AddUInt - Add an unsigned integer attribute data and value.
00452   ///
00453   void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
00454 
00455   /// AddSInt - Add an signed integer attribute data and value.
00456   ///
00457   void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
00458       
00459   /// AddString - Add a std::string attribute data and value.
00460   ///
00461   void AddString(unsigned Attribute, unsigned Form,
00462                  const std::string &String);
00463       
00464   /// AddLabel - Add a Dwarf label attribute data and value.
00465   ///
00466   void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
00467       
00468   /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
00469   ///
00470   void AddObjectLabel(unsigned Attribute, unsigned Form,
00471                       const std::string &Label);
00472       
00473   /// AddDelta - Add a label delta attribute data and value.
00474   ///
00475   void AddDelta(unsigned Attribute, unsigned Form,
00476                 const DWLabel &Hi, const DWLabel &Lo);
00477       
00478   /// AddDIEntry - Add a DIE attribute data and value.
00479   ///
00480   void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
00481 
00482   /// AddBlock - Add block data.
00483   ///
00484   void AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block);
00485 
00486   /// Complete - Indicate that all attributes have been added and
00487   /// ready to get an abbreviation ID.
00488   ///
00489   void Complete(DwarfWriter &DW);
00490   
00491   /// AddChild - Add a child to the DIE.
00492   void AddChild(DIE *Child);
00493 };
00494 
00495 } // End of namespace llvm
00496 
00497 //===----------------------------------------------------------------------===//
00498 
00499 CompileUnit::~CompileUnit() {
00500   delete Die;
00501 }
00502 
00503 /// hasContent - Return true if this compile unit has something to write out.
00504 ///
00505 bool CompileUnit::hasContent() const {
00506   return !Die->getChildren().empty();
00507 }
00508 
00509 /// AddGlobal - Add a new global entity to the compile unit.
00510 ///
00511 void CompileUnit::AddGlobal(const std::string &Name, DIE *Die) {
00512   Globals[Name] = Die;
00513 }
00514 
00515 //===----------------------------------------------------------------------===//
00516 
00517 /// operator== - Used by UniqueVector to locate entry.
00518 ///
00519 bool DIEAbbrev::operator==(const DIEAbbrev &DA) const {
00520   if (Tag != DA.Tag) return false;
00521   if (ChildrenFlag != DA.ChildrenFlag) return false;
00522   if (Data.size() != DA.Data.size()) return false;
00523   
00524   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00525     if (Data[i] != DA.Data[i]) return false;
00526   }
00527   
00528   return true;
00529 }
00530 
00531 /// operator< - Used by UniqueVector to locate entry.
00532 ///
00533 bool DIEAbbrev::operator<(const DIEAbbrev &DA) const {
00534   if (Tag != DA.Tag) return Tag < DA.Tag;
00535   if (ChildrenFlag != DA.ChildrenFlag) return ChildrenFlag < DA.ChildrenFlag;
00536   if (Data.size() != DA.Data.size()) return Data.size() < DA.Data.size();
00537   
00538   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00539     if (Data[i] != DA.Data[i]) return Data[i] < DA.Data[i];
00540   }
00541   
00542   return false;
00543 }
00544     
00545 /// Emit - Print the abbreviation using the specified Dwarf writer.
00546 ///
00547 void DIEAbbrev::Emit(const DwarfWriter &DW) const {
00548   // Emit its Dwarf tag type.
00549   DW.EmitULEB128Bytes(Tag);
00550   DW.EOL(TagString(Tag));
00551   
00552   // Emit whether it has children DIEs.
00553   DW.EmitULEB128Bytes(ChildrenFlag);
00554   DW.EOL(ChildrenString(ChildrenFlag));
00555   
00556   // For each attribute description.
00557   for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00558     const DIEAbbrevData &AttrData = Data[i];
00559     
00560     // Emit attribute type.
00561     DW.EmitULEB128Bytes(AttrData.getAttribute());
00562     DW.EOL(AttributeString(AttrData.getAttribute()));
00563     
00564     // Emit form type.
00565     DW.EmitULEB128Bytes(AttrData.getForm());
00566     DW.EOL(FormEncodingString(AttrData.getForm()));
00567   }
00568 
00569   // Mark end of abbreviation.
00570   DW.EmitULEB128Bytes(0); DW.EOL("EOM(1)");
00571   DW.EmitULEB128Bytes(0); DW.EOL("EOM(2)");
00572 }
00573 
00574 #ifndef NDEBUG
00575   void DIEAbbrev::print(std::ostream &O) {
00576     O << "Abbreviation @"
00577       << std::hex << (intptr_t)this << std::dec
00578       << "  "
00579       << TagString(Tag)
00580       << " "
00581       << ChildrenString(ChildrenFlag)
00582       << "\n";
00583     
00584     for (unsigned i = 0, N = Data.size(); i < N; ++i) {
00585       O << "  "
00586         << AttributeString(Data[i].getAttribute())
00587         << "  "
00588         << FormEncodingString(Data[i].getForm())
00589         << "\n";
00590     }
00591   }
00592   void DIEAbbrev::dump() { print(std::cerr); }
00593 #endif
00594 
00595 //===----------------------------------------------------------------------===//
00596 
00597 /// BestForm - Choose the best form for integer.
00598 ///
00599 unsigned DIEInteger::BestForm(bool IsSigned) {
00600   if (IsSigned) {
00601     if ((char)Integer == (signed)Integer)   return DW_FORM_data1;
00602     if ((short)Integer == (signed)Integer)  return DW_FORM_data2;
00603     if ((int)Integer == (signed)Integer)    return DW_FORM_data4;
00604   } else {
00605     if ((unsigned char)Integer == Integer)  return DW_FORM_data1;
00606     if ((unsigned short)Integer == Integer) return DW_FORM_data2;
00607     if ((unsigned int)Integer == Integer)   return DW_FORM_data4;
00608   }
00609   return DW_FORM_data8;
00610 }
00611     
00612 /// EmitValue - Emit integer of appropriate size.
00613 ///
00614 void DIEInteger::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00615   switch (Form) {
00616   case DW_FORM_flag:  // Fall thru
00617   case DW_FORM_ref1:  // Fall thru
00618   case DW_FORM_data1: DW.EmitInt8(Integer);         break;
00619   case DW_FORM_ref2:  // Fall thru
00620   case DW_FORM_data2: DW.EmitInt16(Integer);        break;
00621   case DW_FORM_ref4:  // Fall thru
00622   case DW_FORM_data4: DW.EmitInt32(Integer);        break;
00623   case DW_FORM_ref8:  // Fall thru
00624   case DW_FORM_data8: DW.EmitInt64(Integer);        break;
00625   case DW_FORM_udata: DW.EmitULEB128Bytes(Integer); break;
00626   case DW_FORM_sdata: DW.EmitSLEB128Bytes(Integer); break;
00627   default: assert(0 && "DIE Value form not supported yet"); break;
00628   }
00629 }
00630 
00631 /// SizeOf - Determine size of integer value in bytes.
00632 ///
00633 unsigned DIEInteger::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00634   switch (Form) {
00635   case DW_FORM_flag:  // Fall thru
00636   case DW_FORM_ref1:  // Fall thru
00637   case DW_FORM_data1: return sizeof(int8_t);
00638   case DW_FORM_ref2:  // Fall thru
00639   case DW_FORM_data2: return sizeof(int16_t);
00640   case DW_FORM_ref4:  // Fall thru
00641   case DW_FORM_data4: return sizeof(int32_t);
00642   case DW_FORM_ref8:  // Fall thru
00643   case DW_FORM_data8: return sizeof(int64_t);
00644   case DW_FORM_udata: return DW.SizeULEB128(Integer);
00645   case DW_FORM_sdata: return DW.SizeSLEB128(Integer);
00646   default: assert(0 && "DIE Value form not supported yet"); break;
00647   }
00648   return 0;
00649 }
00650 
00651 //===----------------------------------------------------------------------===//
00652 
00653 /// EmitValue - Emit string value.
00654 ///
00655 void DIEString::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00656   DW.EmitString(String);
00657 }
00658 
00659 /// SizeOf - Determine size of string value in bytes.
00660 ///
00661 unsigned DIEString::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00662   return String.size() + sizeof(char); // sizeof('\0');
00663 }
00664 
00665 //===----------------------------------------------------------------------===//
00666 
00667 /// EmitValue - Emit label value.
00668 ///
00669 void DIEDwarfLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00670   DW.EmitReference(Label);
00671 }
00672 
00673 /// SizeOf - Determine size of label value in bytes.
00674 ///
00675 unsigned DIEDwarfLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00676   return DW.getAddressSize();
00677 }
00678     
00679 //===----------------------------------------------------------------------===//
00680 
00681 /// EmitValue - Emit label value.
00682 ///
00683 void DIEObjectLabel::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00684   DW.EmitReference(Label);
00685 }
00686 
00687 /// SizeOf - Determine size of label value in bytes.
00688 ///
00689 unsigned DIEObjectLabel::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00690   return DW.getAddressSize();
00691 }
00692     
00693 //===----------------------------------------------------------------------===//
00694 
00695 /// EmitValue - Emit delta value.
00696 ///
00697 void DIEDelta::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00698   DW.EmitDifference(LabelHi, LabelLo);
00699 }
00700 
00701 /// SizeOf - Determine size of delta value in bytes.
00702 ///
00703 unsigned DIEDelta::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00704   return DW.getAddressSize();
00705 }
00706 
00707 //===----------------------------------------------------------------------===//
00708 /// EmitValue - Emit debug information entry offset.
00709 ///
00710 void DIEntry::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00711   DW.EmitInt32(Entry->getOffset());
00712 }
00713 
00714 /// SizeOf - Determine size of debug information entry value in bytes.
00715 ///
00716 unsigned DIEntry::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00717   return sizeof(int32_t);
00718 }
00719     
00720 //===----------------------------------------------------------------------===//
00721 
00722 DIEBlock::~DIEBlock() {
00723   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
00724     delete Values[i];
00725   }
00726 }
00727 
00728 /// ComputeSize - calculate the size of the block.
00729 ///
00730 unsigned DIEBlock::ComputeSize(DwarfWriter &DW) {
00731   Size = 0;
00732   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
00733     Size += Values[i]->SizeOf(DW, Forms[i]);
00734   }
00735   return Size;
00736 }
00737 
00738 /// BestForm - Choose the best form for data.
00739 ///
00740 unsigned DIEBlock::BestForm() {
00741   if ((unsigned char)Size == Size)  return DW_FORM_block1;
00742   if ((unsigned short)Size == Size) return DW_FORM_block2;
00743   if ((unsigned int)Size == Size)   return DW_FORM_block4;
00744   return DW_FORM_block;
00745 }
00746 
00747 /// EmitValue - Emit block data.
00748 ///
00749 void DIEBlock::EmitValue(const DwarfWriter &DW, unsigned Form) const {
00750   switch (Form) {
00751   case DW_FORM_block1: DW.EmitInt8(Size);         break;
00752   case DW_FORM_block2: DW.EmitInt16(Size);        break;
00753   case DW_FORM_block4: DW.EmitInt32(Size);        break;
00754   case DW_FORM_block:  DW.EmitULEB128Bytes(Size); break;
00755   default: assert(0 && "Improper form for block"); break;
00756   }
00757   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
00758     DW.EOL("");
00759     Values[i]->EmitValue(DW, Forms[i]);
00760   }
00761 }
00762 
00763 /// SizeOf - Determine size of block data in bytes.
00764 ///
00765 unsigned DIEBlock::SizeOf(const DwarfWriter &DW, unsigned Form) const {
00766   switch (Form) {
00767   case DW_FORM_block1: return Size + sizeof(int8_t);
00768   case DW_FORM_block2: return Size + sizeof(int16_t);
00769   case DW_FORM_block4: return Size + sizeof(int32_t);
00770   case DW_FORM_block: return Size + DW.SizeULEB128(Size);
00771   default: assert(0 && "Improper form for block"); break;
00772   }
00773   return 0;
00774 }
00775 
00776 /// AddUInt - Add an unsigned integer value.
00777 ///
00778 void DIEBlock::AddUInt(unsigned Form, uint64_t Integer) {
00779   DIEInteger *DI = new DIEInteger(Integer);
00780   Values.push_back(DI);
00781   if (Form == 0) Form = DI->BestForm(false);
00782   Forms.push_back(Form);
00783 }
00784 
00785 /// AddSInt - Add an signed integer value.
00786 ///
00787 void DIEBlock::AddSInt(unsigned Form, int64_t Integer) {
00788   DIEInteger *DI = new DIEInteger(Integer);
00789   Values.push_back(DI);
00790   if (Form == 0) Form = DI->BestForm(true);
00791   Forms.push_back(Form);
00792 }
00793     
00794 /// AddString - Add a std::string value.
00795 ///
00796 void DIEBlock::AddString(unsigned Form, const std::string &String) {
00797   Values.push_back(new DIEString(String));
00798   Forms.push_back(Form);
00799 }
00800     
00801 /// AddLabel - Add a Dwarf label value.
00802 ///
00803 void DIEBlock::AddLabel(unsigned Form, const DWLabel &Label) {
00804   Values.push_back(new DIEDwarfLabel(Label));
00805   Forms.push_back(Form);
00806 }
00807     
00808 /// AddObjectLabel - Add a non-Dwarf label value.
00809 ///
00810 void DIEBlock::AddObjectLabel(unsigned Form, const std::string &Label) {
00811   Values.push_back(new DIEObjectLabel(Label));
00812   Forms.push_back(Form);
00813 }
00814     
00815 /// AddDelta - Add a label delta value.
00816 ///
00817 void DIEBlock::AddDelta(unsigned Form, const DWLabel &Hi, const DWLabel &Lo) {
00818   Values.push_back(new DIEDelta(Hi, Lo));
00819   Forms.push_back(Form);
00820 }
00821     
00822 /// AddDIEntry - Add a DIE value.
00823 ///
00824 void DIEBlock::AddDIEntry(unsigned Form, DIE *Entry) {
00825   Values.push_back(new DIEntry(Entry));
00826   Forms.push_back(Form);
00827 }
00828 
00829 //===----------------------------------------------------------------------===//
00830 
00831 DIE::DIE(unsigned Tag)
00832 : Abbrev(new DIEAbbrev(Tag, DW_CHILDREN_no))
00833 , AbbrevID(0)
00834 , Offset(0)
00835 , Size(0)
00836 , Children()
00837 , Values()
00838 {}
00839 
00840 DIE::~DIE() {
00841   if (Abbrev) delete Abbrev;
00842   
00843   for (unsigned i = 0, N = Children.size(); i < N; ++i) {
00844     delete Children[i];
00845   }
00846 
00847   for (unsigned j = 0, M = Values.size(); j < M; ++j) {
00848     delete Values[j];
00849   }
00850 }
00851     
00852 /// AddSiblingOffset - Add a sibling offset field to the front of the DIE.
00853 ///
00854 void DIE::AddSiblingOffset() {
00855   DIEInteger *DI = new DIEInteger(0);
00856   Values.insert(Values.begin(), DI);
00857   Abbrev->AddFirstAttribute(DW_AT_sibling, DW_FORM_ref4);
00858 }
00859 
00860 /// AddUInt - Add an unsigned integer attribute data and value.
00861 ///
00862 void DIE::AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer) {
00863   DIEInteger *DI = new DIEInteger(Integer);
00864   Values.push_back(DI);
00865   if (!Form) Form = DI->BestForm(false);
00866   Abbrev->AddAttribute(Attribute, Form);
00867 }
00868     
00869 /// AddSInt - Add an signed integer attribute data and value.
00870 ///
00871 void DIE::AddSInt(unsigned Attribute, unsigned Form, int64_t Integer) {
00872   DIEInteger *DI = new DIEInteger(Integer);
00873   Values.push_back(DI);
00874   if (!Form) Form = DI->BestForm(true);
00875   Abbrev->AddAttribute(Attribute, Form);
00876 }
00877     
00878 /// AddString - Add a std::string attribute data and value.
00879 ///
00880 void DIE::AddString(unsigned Attribute, unsigned Form,
00881                     const std::string &String) {
00882   Values.push_back(new DIEString(String));
00883   Abbrev->AddAttribute(Attribute, Form);
00884 }
00885     
00886 /// AddLabel - Add a Dwarf label attribute data and value.
00887 ///
00888 void DIE::AddLabel(unsigned Attribute, unsigned Form,
00889                    const DWLabel &Label) {
00890   Values.push_back(new DIEDwarfLabel(Label));
00891   Abbrev->AddAttribute(Attribute, Form);
00892 }
00893     
00894 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
00895 ///
00896 void DIE::AddObjectLabel(unsigned Attribute, unsigned Form,
00897                          const std::string &Label) {
00898   Values.push_back(new DIEObjectLabel(Label));
00899   Abbrev->AddAttribute(Attribute, Form);
00900 }
00901     
00902 /// AddDelta - Add a label delta attribute data and value.
00903 ///
00904 void DIE::AddDelta(unsigned Attribute, unsigned Form,
00905                    const DWLabel &Hi, const DWLabel &Lo) {
00906   Values.push_back(new DIEDelta(Hi, Lo));
00907   Abbrev->AddAttribute(Attribute, Form);
00908 }
00909     
00910 /// AddDIEntry - Add a DIE attribute data and value.
00911 ///
00912 void DIE::AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry) {
00913   Values.push_back(new DIEntry(Entry));
00914   Abbrev->AddAttribute(Attribute, Form);
00915 }
00916 
00917 /// AddBlock - Add block data.
00918 ///
00919 void DIE::AddBlock(unsigned Attribute, unsigned Form, DIEBlock *Block) {
00920   assert(Block->Size && "Block size has not been computed");
00921   Values.push_back(Block);
00922   if (!Form) Form = Block->BestForm();
00923   Abbrev->AddAttribute(Attribute, Form);
00924 }
00925 
00926 /// Complete - Indicate that all attributes have been added and ready to get an
00927 /// abbreviation ID.
00928 void DIE::Complete(DwarfWriter &DW) {
00929   AbbrevID = DW.NewAbbreviation(Abbrev);
00930   delete Abbrev;
00931   Abbrev = NULL;
00932 }
00933 
00934 /// AddChild - Add a child to the DIE.
00935 ///
00936 void DIE::AddChild(DIE *Child) {
00937   assert(Abbrev && "Adding children without an abbreviation");
00938   Abbrev->setChildrenFlag(DW_CHILDREN_yes);
00939   Children.push_back(Child);
00940 }
00941 
00942 //===----------------------------------------------------------------------===//
00943 
00944 /// DWContext
00945 
00946 //===----------------------------------------------------------------------===//
00947 
00948 /// PrintHex - Print a value as a hexidecimal value.
00949 ///
00950 void DwarfWriter::PrintHex(int Value) const { 
00951   O << "0x" << std::hex << Value << std::dec;
00952 }
00953 
00954 /// EOL - Print a newline character to asm stream.  If a comment is present
00955 /// then it will be printed first.  Comments should not contain '\n'.
00956 void DwarfWriter::EOL(const std::string &Comment) const {
00957   if (DwarfVerbose && !Comment.empty()) {
00958     O << "\t"
00959       << Asm->CommentString
00960       << " "
00961       << Comment;
00962   }
00963   O << "\n";
00964 }
00965 
00966 /// EmitAlign - Print a align directive.
00967 ///
00968 void DwarfWriter::EmitAlign(unsigned Alignment) const {
00969   O << Asm->AlignDirective << Alignment << "\n";
00970 }
00971 
00972 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
00973 /// unsigned leb128 value.
00974 void DwarfWriter::EmitULEB128Bytes(unsigned Value) const {
00975   if (hasLEB128) {
00976     O << "\t.uleb128\t"
00977       << Value;
00978   } else {
00979     O << Asm->Data8bitsDirective;
00980     PrintULEB128(Value);
00981   }
00982 }
00983 
00984 /// EmitSLEB128Bytes - Emit an assembler byte data directive to compose a
00985 /// signed leb128 value.
00986 void DwarfWriter::EmitSLEB128Bytes(int Value) const {
00987   if (hasLEB128) {
00988     O << "\t.sleb128\t"
00989       << Value;
00990   } else {
00991     O << Asm->Data8bitsDirective;
00992     PrintSLEB128(Value);
00993   }
00994 }
00995 
00996 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
00997 /// representing an unsigned leb128 value.
00998 void DwarfWriter::PrintULEB128(unsigned Value) const {
00999   do {
01000     unsigned Byte = Value & 0x7f;
01001     Value >>= 7;
01002     if (Value) Byte |= 0x80;
01003     PrintHex(Byte);
01004     if (Value) O << ", ";
01005   } while (Value);
01006 }
01007 
01008 /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
01009 /// value.
01010 unsigned DwarfWriter::SizeULEB128(unsigned Value) {
01011   unsigned Size = 0;
01012   do {
01013     Value >>= 7;
01014     Size += sizeof(int8_t);
01015   } while (Value);
01016   return Size;
01017 }
01018 
01019 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
01020 /// representing a signed leb128 value.
01021 void DwarfWriter::PrintSLEB128(int Value) const {
01022   int Sign = Value >> (8 * sizeof(Value) - 1);
01023   bool IsMore;
01024   
01025   do {
01026     unsigned Byte = Value & 0x7f;
01027     Value >>= 7;
01028     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
01029     if (IsMore) Byte |= 0x80;
01030     PrintHex(Byte);
01031     if (IsMore) O << ", ";
01032   } while (IsMore);
01033 }
01034 
01035 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
01036 /// value.
01037 unsigned DwarfWriter::SizeSLEB128(int Value) {
01038   unsigned Size = 0;
01039   int Sign = Value >> (8 * sizeof(Value) - 1);
01040   bool IsMore;
01041   
01042   do {
01043     unsigned Byte = Value & 0x7f;
01044     Value >>= 7;
01045     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
01046     Size += sizeof(int8_t);
01047   } while (IsMore);
01048   return Size;
01049 }
01050 
01051 /// EmitInt8 - Emit a byte directive and value.
01052 ///
01053 void DwarfWriter::EmitInt8(int Value) const {
01054   O << Asm->Data8bitsDirective;
01055   PrintHex(Value & 0xFF);
01056 }
01057 
01058 /// EmitInt16 - Emit a short directive and value.
01059 ///
01060 void DwarfWriter::EmitInt16(int Value) const {
01061   O << Asm->Data16bitsDirective;
01062   PrintHex(Value & 0xFFFF);
01063 }
01064 
01065 /// EmitInt32 - Emit a long directive and value.
01066 ///
01067 void DwarfWriter::EmitInt32(int Value) const {
01068   O << Asm->Data32bitsDirective;
01069   PrintHex(Value);
01070 }
01071 
01072 /// EmitInt64 - Emit a long long directive and value.
01073 ///
01074 void DwarfWriter::EmitInt64(uint64_t Value) const {
01075   if (Asm->Data64bitsDirective) {
01076     O << Asm->Data64bitsDirective << "0x" << std::hex << Value << std::dec;
01077   } else {
01078     if (TD.isBigEndian()) {
01079       EmitInt32(unsigned(Value >> 32)); O << "\n";
01080       EmitInt32(unsigned(Value));
01081     } else {
01082       EmitInt32(unsigned(Value)); O << "\n";
01083       EmitInt32(unsigned(Value >> 32));
01084     }
01085   }
01086 }
01087 
01088 /// EmitString - Emit a string with quotes and a null terminator.
01089 /// Special characters are emitted properly. (Eg. '\t')
01090 void DwarfWriter::EmitString(const std::string &String) const {
01091   O << Asm->AsciiDirective
01092     << "\"";
01093   for (unsigned i = 0, N = String.size(); i < N; ++i) {
01094     unsigned char C = String[i];
01095     
01096     if (!isascii(C) || iscntrl(C)) {
01097       switch(C) {
01098       case '\b': O << "\\b"; break;
01099       case '\f': O << "\\f"; break;
01100       case '\n': O << "\\n"; break;
01101       case '\r': O << "\\r"; break;
01102       case '\t': O << "\\t"; break;
01103       default:
01104         O << '\\';
01105         O << char('0' + (C >> 6));
01106         O << char('0' + (C >> 3));
01107         O << char('0' + (C >> 0));
01108         break;
01109       }
01110     } else if (C == '\"') {
01111       O << "\\\"";
01112     } else if (C == '\'') {
01113       O << "\\\'";
01114     } else {
01115      O << C;
01116     }
01117   }
01118   O << "\\0\"";
01119 }
01120 
01121 /// PrintLabelName - Print label name in form used by Dwarf writer.
01122 ///
01123 void DwarfWriter::PrintLabelName(const char *Tag, unsigned Number) const {
01124   O << Asm->PrivateGlobalPrefix
01125     << "debug_"
01126     << Tag;
01127   if (Number) O << Number;
01128 }
01129 
01130 /// EmitLabel - Emit location label for internal use by Dwarf.
01131 ///
01132 void DwarfWriter::EmitLabel(const char *Tag, unsigned Number) const {
01133   PrintLabelName(Tag, Number);
01134   O << ":\n";
01135 }
01136 
01137 /// EmitReference - Emit a reference to a label.
01138 ///
01139 void DwarfWriter::EmitReference(const char *Tag, unsigned Number) const {
01140   if (AddressSize == 4)
01141     O << Asm->Data32bitsDirective;
01142   else
01143     O << Asm->Data64bitsDirective;
01144     
01145   PrintLabelName(Tag, Number);
01146 }
01147 void DwarfWriter::EmitReference(const std::string &Name) const {
01148   if (AddressSize == 4)
01149     O << Asm->Data32bitsDirective;
01150   else
01151     O << Asm->Data64bitsDirective;
01152     
01153   O << Name;
01154 }
01155 
01156 /// EmitDifference - Emit an label difference as sizeof(pointer) value.  Some
01157 /// assemblers do not accept absolute expressions with data directives, so there 
01158 /// is an option (needsSet) to use an intermediary 'set' expression.
01159 void DwarfWriter::EmitDifference(const char *TagHi, unsigned NumberHi,
01160                                  const char *TagLo, unsigned NumberLo) const {
01161   if (needsSet) {
01162     static unsigned SetCounter = 0;
01163     
01164     O << "\t.set\t";
01165     PrintLabelName("set", SetCounter);
01166     O << ",";
01167     PrintLabelName(TagHi, NumberHi);
01168     O << "-";
01169     PrintLabelName(TagLo, NumberLo);
01170     O << "\n";
01171     
01172     if (AddressSize == sizeof(int32_t))
01173       O << Asm->Data32bitsDirective;
01174     else
01175       O << Asm->Data64bitsDirective;
01176       
01177     PrintLabelName("set", SetCounter);
01178     
01179     ++SetCounter;
01180   } else {
01181     if (AddressSize == sizeof(int32_t))
01182       O << Asm->Data32bitsDirective;
01183     else
01184       O << Asm->Data64bitsDirective;
01185       
01186     PrintLabelName(TagHi, NumberHi);
01187     O << "-";
01188     PrintLabelName(TagLo, NumberLo);
01189   }
01190 }
01191 
01192 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
01193 ///  
01194 unsigned DwarfWriter::NewAbbreviation(DIEAbbrev *Abbrev) {
01195   return Abbreviations.insert(*Abbrev);
01196 }
01197 
01198 /// NewString - Add a string to the constant pool and returns a label.
01199 ///
01200 DWLabel DwarfWriter::NewString(const std::string &String) {
01201   unsigned StringID = StringPool.insert(String);
01202   return DWLabel("string", StringID);
01203 }
01204 
01205 /// AddSourceLine - Add location information to specified debug information
01206 /// entry.
01207 void DwarfWriter::AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line) {
01208   if (File && Line) {
01209     CompileUnit *FileUnit = FindCompileUnit(File);
01210     unsigned FileID = FileUnit->getID();
01211     Die->AddUInt(DW_AT_decl_file, 0, FileID);
01212     Die->AddUInt(DW_AT_decl_line, 0, Line);
01213   }
01214 }
01215 
01216 /// AddAddress - Add an address attribute to a die based on the location
01217 /// provided.
01218 void DwarfWriter::AddAddress(DIE *Die, unsigned Attribute,
01219                              const MachineLocation &Location) {
01220   DIEBlock *Block = new DIEBlock();
01221   if (Location.isRegister()) {
01222     Block->AddUInt(DW_FORM_data1,
01223                    DW_OP_reg0 + RI->getDwarfRegNum(Location.getRegister()));
01224   } else {
01225     Block->AddUInt(DW_FORM_data1,
01226                    DW_OP_breg0 + RI->getDwarfRegNum(Location.getRegister()));
01227     Block->AddUInt(DW_FORM_sdata, Location.getOffset());
01228   }
01229   Block->ComputeSize(*this);
01230   Die->AddBlock(Attribute, 0, Block);
01231 }
01232 
01233 /// getDieMapSlotFor - Returns the debug information entry map slot for the
01234 /// specified debug descriptor.
01235 DIE *&DwarfWriter::getDieMapSlotFor(DebugInfoDesc *DD) {
01236   return DescToDieMap[DD];
01237 }
01238                                  
01239 /// NewType - Create a new type DIE.
01240 ///
01241 DIE *DwarfWriter::NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit) {
01242   if (!TyDesc) {
01243     // FIXME - Hack for missing types
01244     DIE *Die = new DIE(DW_TAG_base_type);
01245     Die->AddUInt(DW_AT_byte_size, 0, 4);
01246     Die->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
01247     Unit->getDie()->AddChild(Die);
01248     return Die;
01249   }
01250   
01251   // FIXME - Should handle other contexts that compile units.
01252 
01253   // Check for pre-existence.
01254   DIE *&Slot = Unit->getDieMapSlotFor(TyDesc);
01255   if (Slot) return Slot;
01256 
01257   // Get core information.
01258   const std::string &Name = TyDesc->getName();
01259   uint64_t Size = TyDesc->getSize() >> 3;
01260   
01261   DIE *Ty = NULL;
01262   
01263   if (BasicTypeDesc *BasicTy = dyn_cast<BasicTypeDesc>(TyDesc)) {
01264     // Fundamental types like int, float, bool
01265     Slot = Ty = new DIE(DW_TAG_base_type);
01266     unsigned Encoding = BasicTy->getEncoding();
01267     Ty->AddUInt  (DW_AT_encoding,  DW_FORM_data1, Encoding);
01268   } else if (DerivedTypeDesc *DerivedTy = dyn_cast<DerivedTypeDesc>(TyDesc)) {
01269     // Create specific DIE.
01270     Slot = Ty = new DIE(DerivedTy->getTag());
01271     
01272     // Map to main type, void will not have a type.
01273     if (TypeDesc *FromTy = DerivedTy->getFromType()) {
01274       Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
01275                      NewType(Context, FromTy, Unit));
01276     }
01277   } else if (CompositeTypeDesc *CompTy = dyn_cast<CompositeTypeDesc>(TyDesc)) {
01278     // Create specific DIE.
01279     Slot = Ty = new DIE(CompTy->getTag());
01280     std::vector<DebugInfoDesc *> &Elements = CompTy->getElements();
01281     
01282     switch (CompTy->getTag()) {
01283     case DW_TAG_array_type: {
01284       // Add element type.
01285       if (TypeDesc *FromTy = CompTy->getFromType()) {
01286         Ty->AddDIEntry(DW_AT_type, DW_FORM_ref4,
01287                        NewType(Context, FromTy, Unit));
01288       }
01289       // Don't emit size attribute.
01290       Size = 0;
01291       
01292       // Construct an anonymous type for index type.
01293       DIE *IndexTy = new DIE(DW_TAG_base_type);
01294       IndexTy->AddUInt(DW_AT_byte_size, 0, 4);
01295       IndexTy->AddUInt(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
01296       // Add to context.
01297       Context->AddChild(IndexTy);
01298     
01299       // Add subranges to array type.
01300       for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
01301         SubrangeDesc *SRD = cast<SubrangeDesc>(Elements[i]);
01302         int64_t Lo = SRD->getLo();
01303         int64_t Hi = SRD->getHi();
01304         DIE *Subrange = new DIE(DW_TAG_subrange_type);
01305         
01306         // If a range is available.
01307         if (Lo != Hi) {
01308           Subrange->AddDIEntry(DW_AT_type, DW_FORM_ref4, IndexTy);
01309           // Only add low if non-zero.
01310           if (Lo) Subrange->AddSInt(DW_AT_lower_bound, 0, Lo);
01311           Subrange->AddSInt(DW_AT_upper_bound, 0, Hi);
01312         }
01313         Ty->AddChild(Subrange);
01314       }
01315       
01316       break;
01317     }
01318     case DW_TAG_structure_type:
01319     case DW_TAG_union_type: {
01320       // FIXME - this is just the basics.
01321       // Add elements to structure type.
01322       for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
01323         DerivedTypeDesc *MemberDesc = cast<DerivedTypeDesc>(Elements[i]);
01324         
01325         // Extract the basic information.
01326         const std::string &Name = MemberDesc->getName();
01327         TypeDesc *MemTy = MemberDesc->getFromType();
01328         uint64_t Size = MemberDesc->getSize();
01329         uint64_t Align = MemberDesc->getAlign();
01330         uint64_t Offset = MemberDesc->getOffset();
01331    
01332         // Construct member debug information entry.
01333         DIE *Member = new DIE(DW_TAG_member);
01334         
01335         // Add name if not "".
01336         if (!Name.empty()) Member->AddString(DW_AT_name, DW_FORM_string, Name);
01337         // Add location if available.
01338         AddSourceLine(Member, MemberDesc->getFile(), MemberDesc->getLine());
01339         
01340         // Most of the time the field info is the same as the members.
01341         uint64_t FieldSize = Size;
01342         uint64_t FieldAlign = Align;
01343         uint64_t FieldOffset = Offset;
01344         
01345         if (TypeDesc *FromTy = MemberDesc->getFromType()) {
01346           Member->AddDIEntry(DW_AT_type, DW_FORM_ref4,
01347                              NewType(Context, FromTy, Unit));
01348           FieldSize = FromTy->getSize();
01349           FieldAlign = FromTy->getSize();
01350         }
01351         
01352         // Unless we have a bit field.
01353         if (FieldSize != Size) {
01354           // Construct the alignment mask.
01355           uint64_t AlignMask = ~(FieldAlign - 1);
01356           // Determine the high bit + 1 of the declared size.
01357           uint64_t HiMark = (Offset + FieldSize) & AlignMask;
01358           // Work backwards to determine the base offset of the field.
01359           FieldOffset = HiMark - FieldSize;
01360           // Now normalize offset to the field.
01361           Offset -= FieldOffset;
01362           
01363           // Maybe we need to work from the other end.
01364           if (TD.isLittleEndian()) Offset = FieldSize - (Offset + Size);
01365           
01366           Member->AddUInt(DW_AT_byte_size, 0, FieldSize >> 3);
01367           Member->AddUInt(DW_AT_bit_size, 0, Size);
01368           Member->AddUInt(DW_AT_bit_offset, 0, Offset);
01369         }
01370         
01371         // Add computation for offset.
01372         DIEBlock *Block = new DIEBlock();
01373         Block->AddUInt(DW_FORM_data1, DW_OP_plus_uconst);
01374         Block->AddUInt(DW_FORM_udata, FieldOffset >> 3);
01375         Block->ComputeSize(*this);
01376         Member->AddBlock(DW_AT_data_member_location, 0, Block);
01377         
01378         Ty->AddChild(Member);
01379       }
01380       break;
01381     }
01382     case DW_TAG_enumeration_type: {
01383       // Add enumerators to enumeration type.
01384       for(unsigned i = 0, N = Elements.size(); i < N; ++i) {
01385         EnumeratorDesc *ED = cast<EnumeratorDesc>(Elements[i]);
01386         const std::string &Name = ED->getName();
01387         int64_t Value = ED->getValue();
01388         DIE *Enumerator = new DIE(DW_TAG_enumerator);
01389         Enumerator->AddString(DW_AT_name, DW_FORM_string, Name);
01390         Enumerator->AddSInt(DW_AT_const_value, DW_FORM_sdata, Value);
01391         Ty->AddChild(Enumerator);
01392       }
01393 
01394       break;
01395     }
01396     default: break;
01397     }
01398   }
01399   
01400   assert(Ty && "Type not supported yet");
01401  
01402   // Add size if non-zero (derived types don't have a size.)
01403   if (Size) Ty->AddUInt(DW_AT_byte_size, 0, Size);
01404   // Add name if not anonymous or intermediate type.
01405   if (!Name.empty()) Ty->AddString(DW_AT_name, DW_FORM_string, Name);
01406   // Add source line info if available.
01407   AddSourceLine(Ty, TyDesc->getFile(), TyDesc->getLine());
01408 
01409   // Add to context owner.
01410   Context->AddChild(Ty);
01411   
01412   return Slot;
01413 }
01414 
01415 /// NewCompileUnit - Create new compile unit and it's debug information entry.
01416 ///
01417 CompileUnit *DwarfWriter::NewCompileUnit(CompileUnitDesc *UnitDesc,
01418                                          unsigned ID) {
01419   // Construct debug information entry.
01420   DIE *Die = new DIE(DW_TAG_compile_unit);
01421   Die->AddLabel (DW_AT_stmt_list, DW_FORM_data4,  DWLabel("line", 0));
01422   Die->AddLabel (DW_AT_high_pc,   DW_FORM_addr,   DWLabel("text_end", 0));
01423   Die->AddLabel (DW_AT_low_pc,    DW_FORM_addr,   DWLabel("text_begin", 0));
01424   Die->AddString(DW_AT_producer,  DW_FORM_string, UnitDesc->getProducer());
01425   Die->AddUInt  (DW_AT_language,  DW_FORM_data1,  UnitDesc->getLanguage());
01426   Die->AddString(DW_AT_name,      DW_FORM_string, UnitDesc->getFileName());
01427   Die->AddString(DW_AT_comp_dir,  DW_FORM_string, UnitDesc->getDirectory());
01428   
01429   // Add debug information entry to descriptor map.
01430   DIE *&Slot = getDieMapSlotFor(UnitDesc);
01431   Slot = Die;
01432   
01433   // Construct compile unit.
01434   CompileUnit *Unit = new CompileUnit(UnitDesc, ID, Die);
01435   
01436   // Add Unit to compile unit map.
01437   DescToUnitMap[UnitDesc] = Unit;
01438   
01439   return Unit;
01440 }
01441 
01442 /// FindCompileUnit - Get the compile unit for the given descriptor.
01443 ///
01444 CompileUnit *DwarfWriter::FindCompileUnit(CompileUnitDesc *UnitDesc) {
01445   CompileUnit *Unit = DescToUnitMap[UnitDesc];
01446   assert(Unit && "Missing compile unit.");
01447   return Unit;
01448 }
01449 
01450 /// NewGlobalVariable - Add a new global variable DIE.
01451 ///
01452 DIE *DwarfWriter::NewGlobalVariable(GlobalVariableDesc *GVD) {
01453   // Get the compile unit context.
01454   CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(GVD->getContext());
01455   CompileUnit *Unit = FindCompileUnit(UnitDesc);
01456 
01457   // Check for pre-existence.
01458   DIE *&Slot = Unit->getDieMapSlotFor(GVD);
01459   if (Slot) return Slot;
01460   
01461   // Get the global variable itself.
01462   GlobalVariable *GV = GVD->getGlobalVariable();
01463   // Generate the mangled name.
01464   std::string MangledName = Asm->Mang->getValueName(GV);
01465 
01466   // Gather the details (simplify add attribute code.)
01467   const std::string &Name = GVD->getName();
01468   
01469   // Get the global's type.
01470   DIE *Type = NewType(Unit->getDie(), GVD->getType(), Unit); 
01471 
01472   // Create the globale variable DIE.
01473   DIE *VariableDie = new DIE(DW_TAG_variable);
01474   VariableDie->AddString     (DW_AT_name,      DW_FORM_string, Name);
01475   VariableDie->AddDIEntry    (DW_AT_type,      DW_FORM_ref4,   Type);
01476   VariableDie->AddUInt       (DW_AT_external,  DW_FORM_flag,   1);
01477   
01478   // Add source line info if available.
01479   AddSourceLine(VariableDie, UnitDesc, GVD->getLine());
01480 
01481   // Add address.
01482   DIEBlock *Block = new DIEBlock();
01483   Block->AddUInt(DW_FORM_data1, DW_OP_addr);
01484   Block->AddObjectLabel(DW_FORM_udata, MangledName);
01485   Block->ComputeSize(*this);
01486   VariableDie->AddBlock(DW_AT_location,  0, Block);
01487   
01488   // Add to map.
01489   Slot = VariableDie;
01490  
01491   // Add to context owner.
01492   Unit->getDie()->AddChild(VariableDie);
01493   
01494   // Expose as global.
01495   // FIXME - need to check external flag.
01496   Unit->AddGlobal(Name, VariableDie);
01497   
01498   return VariableDie;
01499 }
01500 
01501 /// NewSubprogram - Add a new subprogram DIE.
01502 ///
01503 DIE *DwarfWriter::NewSubprogram(SubprogramDesc *SPD) {
01504   // Get the compile unit context.
01505   CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
01506   CompileUnit *Unit = FindCompileUnit(UnitDesc);
01507 
01508   // Check for pre-existence.
01509   DIE *&Slot = Unit->getDieMapSlotFor(SPD);
01510   if (Slot) return Slot;
01511   
01512   // Gather the details (simplify add attribute code.)
01513   const std::string &Name = SPD->getName();
01514   DIE *Type = NewType(Unit->getDie(), SPD->getType(), Unit); 
01515   unsigned IsExternal = SPD->isStatic() ? 0 : 1;
01516                                     
01517   DIE *SubprogramDie = new DIE(DW_TAG_subprogram);
01518   SubprogramDie->AddString     (DW_AT_name,      DW_FORM_string, Name);
01519   if (Type) {
01520     SubprogramDie->AddDIEntry    (DW_AT_type,      DW_FORM_ref4,   Type);
01521   }
01522   SubprogramDie->AddUInt       (DW_AT_external,    DW_FORM_flag,   IsExternal);
01523   SubprogramDie->AddUInt       (DW_AT_prototyped,  DW_FORM_flag,   1);
01524   
01525   // Add source line info if available.
01526   AddSourceLine(SubprogramDie, UnitDesc, SPD->getLine());
01527 
01528   // Add to map.
01529   Slot = SubprogramDie;
01530  
01531   // Add to context owner.
01532   Unit->getDie()->AddChild(SubprogramDie);
01533   
01534   // Expose as global.
01535   Unit->AddGlobal(Name, SubprogramDie);
01536   
01537   return SubprogramDie;
01538 }
01539 
01540 /// NewScopeVariable - Create a new scope variable.
01541 ///
01542 DIE *DwarfWriter::NewScopeVariable(DebugVariable *DV, CompileUnit *Unit) {
01543   // Get the descriptor.
01544   VariableDesc *VD = DV->getDesc();
01545 
01546   // Translate tag to proper Dwarf tag.  The result variable is dropped for now.
01547   unsigned Tag;
01548   switch (VD->getTag()) {
01549   case DW_TAG_return_variable:  return NULL;
01550   case DW_TAG_arg_variable:     Tag = DW_TAG_formal_parameter; break;
01551   case DW_TAG_auto_variable:    // fall thru
01552   default:                      Tag = DW_TAG_variable; break;
01553   }
01554 
01555   // Define variable debug information entry.
01556   DIE *VariableDie = new DIE(Tag);
01557   VariableDie->AddString(DW_AT_name, DW_FORM_string, VD->getName());
01558 
01559   // Add source line info if available.
01560   AddSourceLine(VariableDie, VD->getFile(), VD->getLine());
01561   
01562   // Add variable type.
01563   DIE *Type = NewType(Unit->getDie(), VD->getType(), Unit); 
01564   VariableDie->AddDIEntry(DW_AT_type, DW_FORM_ref4, Type);
01565   
01566   // Add variable address.
01567   MachineLocation Location;
01568   RI->getLocation(*MF, DV->getFrameIndex(), Location);
01569   AddAddress(VariableDie, DW_AT_location, Location);
01570   
01571   return VariableDie;
01572 }
01573 
01574 /// ConstructScope - Construct the components of a scope.
01575 ///
01576 void DwarfWriter::ConstructScope(DebugScope *ParentScope,
01577                                  DIE *ParentDie, CompileUnit *Unit) {
01578   // Add variables to scope.
01579   std::vector<DebugVariable *> &Variables = ParentScope->getVariables();
01580   for (unsigned i = 0, N = Variables.size(); i < N; ++i) {
01581     DIE *VariableDie = NewScopeVariable(Variables[i], Unit);
01582     if (VariableDie) ParentDie->AddChild(VariableDie);
01583   }
01584   
01585   // Add nested scopes.
01586   std::vector<DebugScope *> &Scopes = ParentScope->getScopes();
01587   for (unsigned j = 0, M = Scopes.size(); j < M; ++j) {
01588     // Define the Scope debug information entry.
01589     DebugScope *Scope = Scopes[j];
01590     // FIXME - Ignore inlined functions for the time being.
01591     if (Scope->getParent()) continue;
01592     
01593     DIE *ScopeDie = new DIE(DW_TAG_lexical_block);
01594     
01595     // Add the scope bounds.
01596     if (unsigned StartID = Scope->getStartLabelID()) {
01597       ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
01598                          DWLabel("loc", StartID));
01599     } else {
01600       ScopeDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
01601                          DWLabel("func_begin", SubprogramCount));
01602     }
01603     if (unsigned EndID = Scope->getEndLabelID()) {
01604       ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
01605                          DWLabel("loc", EndID));
01606     } else {
01607       ScopeDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
01608                          DWLabel("func_end", SubprogramCount));
01609     }
01610                        
01611     // Add the scope contents.
01612     ConstructScope(Scope, ScopeDie, Unit);
01613     ParentDie->AddChild(ScopeDie);
01614   }
01615 }
01616 
01617 /// ConstructRootScope - Construct the scope for the subprogram.
01618 ///
01619 void DwarfWriter::ConstructRootScope(DebugScope *RootScope) {
01620   // Exit if there is no root scope.
01621   if (!RootScope) return;
01622   
01623   // Get the subprogram debug information entry. 
01624   SubprogramDesc *SPD = cast<SubprogramDesc>(RootScope->getDesc());
01625   
01626   // Get the compile unit context.
01627   CompileUnitDesc *UnitDesc = static_cast<CompileUnitDesc *>(SPD->getContext());
01628   CompileUnit *Unit = FindCompileUnit(UnitDesc);
01629   
01630   // Get the subprogram die.
01631   DIE *SPDie = Unit->getDieMapSlotFor(SPD);
01632   assert(SPDie && "Missing subprogram descriptor");
01633   
01634   // Add the function bounds.
01635   SPDie->AddLabel(DW_AT_low_pc, DW_FORM_addr,
01636                   DWLabel("func_begin", SubprogramCount));
01637   SPDie->AddLabel(DW_AT_high_pc, DW_FORM_addr,
01638                   DWLabel("func_end", SubprogramCount));
01639   MachineLocation Location(RI->getFrameRegister(*MF));
01640   AddAddress(SPDie, DW_AT_frame_base, Location);
01641                   
01642   ConstructScope(RootScope, SPDie, Unit);
01643 }
01644 
01645 /// EmitInitial - Emit initial Dwarf declarations.  This is necessary for cc
01646 /// tools to recognize the object file contains Dwarf information.
01647 ///
01648 void DwarfWriter::EmitInitial() const {
01649   // Dwarf sections base addresses.
01650   Asm->SwitchSection(DwarfFrameSection, 0);
01651   EmitLabel("section_frame", 0);
01652   Asm->SwitchSection(DwarfInfoSection, 0);
01653   EmitLabel("section_info", 0);
01654   EmitLabel("info", 0);
01655   Asm->SwitchSection(DwarfAbbrevSection, 0);
01656   EmitLabel("section_abbrev", 0);
01657   EmitLabel("abbrev", 0);
01658   Asm->SwitchSection(DwarfARangesSection, 0);
01659   EmitLabel("section_aranges", 0);
01660   Asm->SwitchSection(DwarfMacInfoSection, 0);
01661   EmitLabel("section_macinfo", 0);
01662   Asm->SwitchSection(DwarfLineSection, 0);
01663   EmitLabel("section_line", 0);
01664   EmitLabel("line", 0);
01665   Asm->SwitchSection(DwarfLocSection, 0);
01666   EmitLabel("section_loc", 0);
01667   Asm->SwitchSection(DwarfPubNamesSection, 0);
01668   EmitLabel("section_pubnames", 0);
01669   Asm->SwitchSection(DwarfStrSection, 0);
01670   EmitLabel("section_str", 0);
01671   Asm->SwitchSection(DwarfRangesSection, 0);
01672   EmitLabel("section_ranges", 0);
01673 
01674   Asm->SwitchSection(TextSection, 0);
01675   EmitLabel("text_begin", 0);
01676   Asm->SwitchSection(DataSection, 0);
01677   EmitLabel("data_begin", 0);
01678 }
01679 
01680 /// EmitDIE - Recusively Emits a debug information entry.
01681 ///
01682 void DwarfWriter::EmitDIE(DIE *Die) const {
01683   // Get the abbreviation for this DIE.
01684   unsigned AbbrevID = Die->getAbbrevID();
01685   const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
01686   
01687   O << "\n";
01688 
01689   // Emit the code (index) for the abbreviation.
01690   EmitULEB128Bytes(AbbrevID);
01691   EOL(std::string("Abbrev [" +
01692       utostr(AbbrevID) +
01693       "] 0x" + utohexstr(Die->getOffset()) +
01694       ":0x" + utohexstr(Die->getSize()) + " " +
01695       TagString(Abbrev.getTag())));
01696   
01697   const std::vector<DIEValue *> &Values = Die->getValues();
01698   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
01699   
01700   // Emit the DIE attribute values.
01701   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
01702     unsigned Attr = AbbrevData[i].getAttribute();
01703     unsigned Form = AbbrevData[i].getForm();
01704     assert(Form && "Too many attributes for DIE (check abbreviation)");
01705     
01706     switch (Attr) {
01707     case DW_AT_sibling: {
01708       EmitInt32(Die->SiblingOffset());
01709       break;
01710     }
01711     default: {
01712       // Emit an attribute using the defined form.
01713       Values[i]->EmitValue(*this, Form);
01714       break;
01715     }
01716     }
01717     
01718     EOL(AttributeString(Attr));
01719   }
01720   
01721   // Emit the DIE children if any.
01722   if (Abbrev.getChildrenFlag() == DW_CHILDREN_yes) {
01723     const std::vector<DIE *> &Children = Die->getChildren();
01724     
01725     for (unsigned j = 0, M = Children.size(); j < M; ++j) {
01726       EmitDIE(Children[j]);
01727     }
01728     
01729     EmitInt8(0); EOL("End Of Children Mark");
01730   }
01731 }
01732 
01733 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
01734 ///
01735 unsigned DwarfWriter::SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last) {
01736   // Get the children.
01737   const std::vector<DIE *> &Children = Die->getChildren();
01738   
01739   // If not last sibling and has children then add sibling offset attribute.
01740   if (!Last && !Children.empty()) Die->AddSiblingOffset();
01741 
01742   // Record the abbreviation.
01743   Die->Complete(*this);
01744   
01745   // Get the abbreviation for this DIE.
01746   unsigned AbbrevID = Die->getAbbrevID();
01747   const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
01748 
01749   // Set DIE offset
01750   Die->setOffset(Offset);
01751   
01752   // Start the size with the size of abbreviation code.
01753   Offset += SizeULEB128(AbbrevID);
01754   
01755   const std::vector<DIEValue *> &Values = Die->getValues();
01756   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
01757 
01758   // Emit the DIE attribute values.
01759   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
01760     // Size attribute value.
01761     Offset += Values[i]->SizeOf(*this, AbbrevData[i].getForm());
01762   }
01763   
01764   // Emit the DIE children if any.
01765   if (!Children.empty()) {
01766     assert(Abbrev.getChildrenFlag() == DW_CHILDREN_yes &&
01767            "Children flag not set");
01768     
01769     for (unsigned j = 0, M = Children.size(); j < M; ++j) {
01770       Offset = SizeAndOffsetDie(Children[j], Offset, (j + 1) == M);
01771     }
01772     
01773     // End of children marker.
01774     Offset += sizeof(int8_t);
01775   }
01776 
01777   Die->setSize(Offset - Die->getOffset());
01778   return Offset;
01779 }
01780 
01781 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
01782 ///
01783 void DwarfWriter::SizeAndOffsets() {
01784   
01785   // Process each compile unit.
01786   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
01787     CompileUnit *Unit = CompileUnits[i];
01788     if (Unit->hasContent()) {
01789       // Compute size of compile unit header
01790       unsigned Offset = sizeof(int32_t) + // Length of Compilation Unit Info
01791                         sizeof(int16_t) + // DWARF version number
01792                         sizeof(int32_t) + // Offset Into Abbrev. Section
01793                         sizeof(int8_t);   // Pointer Size (in bytes)
01794       SizeAndOffsetDie(Unit->getDie(), Offset, (i + 1) == N);
01795     }
01796   }
01797 }
01798 
01799 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
01800 /// frame.
01801 void DwarfWriter::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
01802                                  std::vector<MachineMove *> &Moves) {
01803   for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
01804     MachineMove *Move = Moves[i];
01805     unsigned LabelID = Move->getLabelID();
01806     const MachineLocation &Dst = Move->getDestination();
01807     const MachineLocation &Src = Move->getSource();
01808     
01809     // Advance row if new location.
01810     if (BaseLabel && LabelID && BaseLabelID != LabelID) {
01811       EmitULEB128Bytes(DW_CFA_advance_loc4);
01812       EOL("DW_CFA_advance_loc4");
01813       EmitDifference("loc", LabelID, BaseLabel, BaseLabelID);
01814       EOL("");
01815       
01816       BaseLabelID = LabelID;
01817       BaseLabel = "loc";
01818     }
01819     
01820     // If advancing cfa.
01821     if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
01822       if (!Src.isRegister()) {
01823         if (Src.getRegister() == MachineLocation::VirtualFP) {
01824           EmitULEB128Bytes(DW_CFA_def_cfa_offset);
01825           EOL("DW_CFA_def_cfa_offset");
01826         } else {
01827           EmitULEB128Bytes(DW_CFA_def_cfa);
01828           EOL("DW_CFA_def_cfa");
01829           
01830           EmitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister()));
01831           EOL("Register");
01832         }
01833         
01834         int stackGrowth =
01835             Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
01836               TargetFrameInfo::StackGrowsUp ?
01837             AddressSize : -AddressSize;
01838         
01839         EmitULEB128Bytes(Src.getOffset() / stackGrowth);
01840         EOL("Offset");
01841       } else {
01842       }
01843     } else {
01844     }
01845   }
01846 }
01847 
01848 /// EmitDebugInfo - Emit the debug info section.
01849 ///
01850 void DwarfWriter::EmitDebugInfo() const {
01851   // Start debug info section.
01852   Asm->SwitchSection(DwarfInfoSection, 0);
01853   
01854   // Process each compile unit.
01855   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
01856     CompileUnit *Unit = CompileUnits[i];
01857     
01858     if (Unit->hasContent()) {
01859       DIE *Die = Unit->getDie();
01860       // Emit the compile units header.
01861       EmitLabel("info_begin", Unit->getID());
01862       // Emit size of content not including length itself
01863       unsigned ContentSize = Die->getSize() +
01864                              sizeof(int16_t) + // DWARF version number
01865                              sizeof(int32_t) + // Offset Into Abbrev. Section
01866                              sizeof(int8_t);   // Pointer Size (in bytes)
01867                              
01868       EmitInt32(ContentSize);  EOL("Length of Compilation Unit Info");
01869       EmitInt16(DWARF_VERSION); EOL("DWARF version number");
01870       EmitReference("abbrev_begin", 0); EOL("Offset Into Abbrev. Section");
01871       EmitInt8(AddressSize); EOL("Address Size (in bytes)");
01872     
01873       EmitDIE(Die);
01874       EmitLabel("info_end", Unit->getID());
01875     }
01876     
01877     O << "\n";
01878   }
01879 }
01880 
01881 /// EmitAbbreviations - Emit the abbreviation section.
01882 ///
01883 void DwarfWriter::EmitAbbreviations() const {
01884   // Check to see if it is worth the effort.
01885   if (!Abbreviations.empty()) {
01886     // Start the debug abbrev section.
01887     Asm->SwitchSection(DwarfAbbrevSection, 0);
01888     
01889     EmitLabel("abbrev_begin", 0);
01890     
01891     // For each abbrevation.
01892     for (unsigned AbbrevID = 1, NAID = Abbreviations.size();
01893                   AbbrevID <= NAID; ++AbbrevID) {
01894       // Get abbreviation data
01895       const DIEAbbrev &Abbrev = Abbreviations[AbbrevID];
01896       
01897       // Emit the abbrevations code (base 1 index.)
01898       EmitULEB128Bytes(AbbrevID); EOL("Abbreviation Code");
01899       
01900       // Emit the abbreviations data.
01901       Abbrev.Emit(*this);
01902   
01903       O << "\n";
01904     }
01905     
01906     EmitLabel("abbrev_end", 0);
01907   
01908     O << "\n";
01909   }
01910 }
01911 
01912 /// EmitDebugLines - Emit source line information.
01913 ///
01914 void DwarfWriter::EmitDebugLines() const {
01915   // Minimum line delta, thus ranging from -10..(255-10).
01916   const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
01917   // Maximum line delta, thus ranging from -10..(255-10).
01918   const int MaxLineDelta = 255 + MinLineDelta;
01919 
01920   // Start the dwarf line section.
01921   Asm->SwitchSection(DwarfLineSection, 0);
01922   
01923   // Construct the section header.
01924   
01925   EmitDifference("line_end", 0, "line_begin", 0);
01926   EOL("Length of Source Line Info");
01927   EmitLabel("line_begin", 0);
01928   
01929   EmitInt16(DWARF_VERSION); EOL("DWARF version number");
01930   
01931   EmitDifference("line_prolog_end", 0, "line_prolog_begin", 0);
01932   EOL("Prolog Length");
01933   EmitLabel("line_prolog_begin", 0);
01934   
01935   EmitInt8(1); EOL("Minimum Instruction Length");
01936 
01937   EmitInt8(1); EOL("Default is_stmt_start flag");
01938 
01939   EmitInt8(MinLineDelta);  EOL("Line Base Value (Special Opcodes)");
01940   
01941   EmitInt8(MaxLineDelta); EOL("Line Range Value (Special Opcodes)");
01942 
01943   EmitInt8(-MinLineDelta); EOL("Special Opcode Base");
01944   
01945   // Line number standard opcode encodings argument count
01946   EmitInt8(0); EOL("DW_LNS_copy arg count");
01947   EmitInt8(1); EOL("DW_LNS_advance_pc arg count");
01948   EmitInt8(1); EOL("DW_LNS_advance_line arg count");
01949   EmitInt8(1); EOL("DW_LNS_set_file arg count");
01950   EmitInt8(1); EOL("DW_LNS_set_column arg count");
01951   EmitInt8(0); EOL("DW_LNS_negate_stmt arg count");
01952   EmitInt8(0); EOL("DW_LNS_set_basic_block arg count");
01953   EmitInt8(0); EOL("DW_LNS_const_add_pc arg count");
01954   EmitInt8(1); EOL("DW_LNS_fixed_advance_pc arg count");
01955 
01956   const UniqueVector<std::string> &Directories = DebugInfo->getDirectories();
01957   const UniqueVector<SourceFileInfo> &SourceFiles = DebugInfo->getSourceFiles();
01958 
01959   // Emit directories.
01960   for (unsigned DirectoryID = 1, NDID = Directories.size();
01961                 DirectoryID <= NDID; ++DirectoryID) {
01962     EmitString(Directories[DirectoryID]); EOL("Directory");
01963   }
01964   EmitInt8(0); EOL("End of directories");
01965   
01966   // Emit files.
01967   for (unsigned SourceID = 1, NSID = SourceFiles.size();
01968                SourceID <= NSID; ++SourceID) {
01969     const SourceFileInfo &SourceFile = SourceFiles[SourceID];
01970     EmitString(SourceFile.getName()); EOL("Source");
01971     EmitULEB128Bytes(SourceFile.getDirectoryID());  EOL("Directory #");
01972     EmitULEB128Bytes(0);  EOL("Mod date");
01973     EmitULEB128Bytes(0);  EOL("File size");
01974   }
01975   EmitInt8(0); EOL("End of files");
01976   
01977   EmitLabel("line_prolog_end", 0);
01978   
01979   // Emit line information
01980   const std::vector<SourceLineInfo *> &LineInfos = DebugInfo->getSourceLines();
01981   
01982   // Dwarf assumes we start with first line of first source file.
01983   unsigned Source = 1;
01984   unsigned Line = 1;
01985   
01986   // Construct rows of the address, source, line, column matrix.
01987   for (unsigned i = 0, N = LineInfos.size(); i < N; ++i) {
01988     SourceLineInfo *LineInfo = LineInfos[i];
01989     
01990     if (DwarfVerbose) {
01991       unsigned SourceID = LineInfo->getSourceID();
01992       const SourceFileInfo &SourceFile = SourceFiles[SourceID];
01993       unsigned DirectoryID = SourceFile.getDirectoryID();
01994       O << "\t"
01995         << Asm->CommentString << " "
01996         << Directories[DirectoryID]
01997         << SourceFile.getName() << ":"
01998         << LineInfo->getLine() << "\n"; 
01999     }
02000 
02001     // Define the line address.
02002     EmitInt8(0); EOL("Extended Op");
02003     EmitInt8(4 + 1); EOL("Op size");
02004     EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
02005     EmitReference("loc",  LineInfo->getLabelID()); EOL("Location label");
02006     
02007     // If change of source, then switch to the new source.
02008     if (Source != LineInfo->getSourceID()) {
02009       Source = LineInfo->getSourceID();
02010       EmitInt8(DW_LNS_set_file); EOL("DW_LNS_set_file");
02011       EmitULEB128Bytes(Source); EOL("New Source");
02012     }
02013     
02014     // If change of line.
02015     if (Line != LineInfo->getLine()) {
02016       // Determine offset.
02017       int Offset = LineInfo->getLine() - Line;
02018       int Delta = Offset - MinLineDelta;
02019       
02020       // Update line.
02021       Line = LineInfo->getLine();
02022       
02023       // If delta is small enough and in range...
02024       if (Delta >= 0 && Delta < (MaxLineDelta - 1)) {
02025         // ... then use fast opcode.
02026         EmitInt8(Delta - MinLineDelta); EOL("Line Delta");
02027       } else {
02028         // ... otherwise use long hand.
02029         EmitInt8(DW_LNS_advance_line); EOL("DW_LNS_advance_line");
02030         EmitSLEB128Bytes(Offset); EOL("Line Offset");
02031         EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
02032       }
02033     } else {
02034       // Copy the previous row (different address or source)
02035       EmitInt8(DW_LNS_copy); EOL("DW_LNS_copy");
02036     }
02037   }
02038 
02039   // Define last address.
02040   EmitInt8(0); EOL("Extended Op");
02041   EmitInt8(4 + 1); EOL("Op size");
02042   EmitInt8(DW_LNE_set_address); EOL("DW_LNE_set_address");
02043   EmitReference("text_end", 0); EOL("Location label");
02044 
02045   // Mark end of matrix.
02046   EmitInt8(0); EOL("DW_LNE_end_sequence");
02047   EmitULEB128Bytes(1);  O << "\n";
02048   EmitInt8(1); O << "\n";
02049   
02050   EmitLabel("line_end", 0);
02051   
02052   O << "\n";
02053 }
02054   
02055 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
02056 ///
02057 void DwarfWriter::EmitInitialDebugFrame() {
02058   int stackGrowth =
02059       Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
02060         TargetFrameInfo::StackGrowsUp ?
02061       AddressSize : -AddressSize;
02062 
02063   // Start the dwarf frame section.
02064   Asm->SwitchSection(DwarfFrameSection, 0);
02065 
02066   EmitDifference("frame_common_end", 0,
02067                  "frame_common_begin", 0);
02068   EOL("Length of Common Information Entry");
02069 
02070   EmitLabel("frame_common_begin", 0);
02071   EmitInt32(DW_CIE_ID); EOL("CIE Identifier Tag");
02072   EmitInt8(DW_CIE_VERSION); EOL("CIE Version");
02073   EmitString("");  EOL("CIE Augmentation");
02074   EmitULEB128Bytes(1); EOL("CIE Code Alignment Factor");
02075   EmitSLEB128Bytes(stackGrowth); EOL("CIE Data Alignment Factor");   
02076   EmitInt8(RI->getDwarfRegNum(RI->getRARegister())); EOL("CIE RA Column");
02077   
02078   std::vector<MachineMove *> Moves;
02079   RI->getInitialFrameState(Moves);
02080   EmitFrameMoves(NULL, 0, Moves);
02081   for (unsigned i = 0, N = Moves.size(); i < N; ++i) delete Moves[i];
02082 
02083   EmitAlign(2);
02084   EmitLabel("frame_common_end", 0);
02085   
02086   O << "\n";
02087 }
02088 
02089 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
02090 /// section.
02091 void DwarfWriter::EmitFunctionDebugFrame() {
02092   // Start the dwarf frame section.
02093   Asm->SwitchSection(DwarfFrameSection, 0);
02094   
02095   EmitDifference("frame_end", SubprogramCount,
02096                  "frame_begin", SubprogramCount);
02097   EOL("Length of Frame Information Entry");
02098   
02099   EmitLabel("frame_begin", SubprogramCount);
02100   
02101   EmitReference("section_frame", 0); EOL("FDE CIE offset");
02102 
02103   EmitReference("func_begin", SubprogramCount); EOL("FDE initial location");
02104   EmitDifference("func_end", SubprogramCount,
02105                  "func_begin", SubprogramCount);
02106   EOL("FDE address range");
02107   
02108   std::vector<MachineMove *> &Moves = DebugInfo->getFrameMoves();
02109   
02110   EmitFrameMoves("func_begin", SubprogramCount, Moves);
02111   
02112   EmitAlign(2);
02113   EmitLabel("frame_end", SubprogramCount);
02114 
02115   O << "\n";
02116 }
02117 
02118 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
02119 ///
02120 void DwarfWriter::EmitDebugPubNames() {
02121   // Start the dwarf pubnames section.
02122   Asm->SwitchSection(DwarfPubNamesSection, 0);
02123     
02124   // Process each compile unit.
02125   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
02126     CompileUnit *Unit = CompileUnits[i];
02127     
02128     if (Unit->hasContent()) {
02129       EmitDifference("pubnames_end", Unit->getID(),
02130                      "pubnames_begin", Unit->getID());
02131       EOL("Length of Public Names Info");
02132       
02133       EmitLabel("pubnames_begin", Unit->getID());
02134       
02135       EmitInt16(DWARF_VERSION); EOL("DWARF Version");
02136       
02137       EmitReference("info_begin", Unit->getID());
02138       EOL("Offset of Compilation Unit Info");
02139 
02140       EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID());
02141       EOL("Compilation Unit Length");
02142       
02143       std::map<std::string, DIE *> &Globals = Unit->getGlobals();
02144       
02145       for (std::map<std::string, DIE *>::iterator GI = Globals.begin(),
02146                                                   GE = Globals.end();
02147            GI != GE; ++GI) {
02148         const std::string &Name = GI->first;
02149         DIE * Entity = GI->second;
02150         
02151         EmitInt32(Entity->getOffset()); EOL("DIE offset");
02152         EmitString(Name); EOL("External Name");
02153       }
02154     
02155       EmitInt32(0); EOL("End Mark");
02156       EmitLabel("pubnames_end", Unit->getID());
02157     
02158       O << "\n";
02159     }
02160   }
02161 }
02162 
02163 /// EmitDebugStr - Emit visible names into a debug str section.
02164 ///
02165 void DwarfWriter::EmitDebugStr() {
02166   // Check to see if it is worth the effort.
02167   if (!StringPool.empty()) {
02168     // Start the dwarf str section.
02169     Asm->SwitchSection(DwarfStrSection, 0);
02170     
02171     // For each of strings in the string pool.
02172     for (unsigned StringID = 1, N = StringPool.size();
02173          StringID <= N; ++StringID) {
02174       // Emit a label for reference from debug information entries.
02175       EmitLabel("string", StringID);
02176       // Emit the string itself.
02177       const std::string &String = StringPool[StringID];
02178       EmitString(String); O << "\n";
02179     }
02180   
02181     O << "\n";
02182   }
02183 }
02184 
02185 /// EmitDebugLoc - Emit visible names into a debug loc section.
02186 ///
02187 void DwarfWriter::EmitDebugLoc() {
02188   // Start the dwarf loc section.
02189   Asm->SwitchSection(DwarfLocSection, 0);
02190   
02191   O << "\n";
02192 }
02193 
02194 /// EmitDebugARanges - Emit visible names into a debug aranges section.
02195 ///
02196 void DwarfWriter::EmitDebugARanges() {
02197   // Start the dwarf aranges section.
02198   Asm->SwitchSection(DwarfARangesSection, 0);
02199   
02200   // FIXME - Mock up
02201 #if 0
02202   // Process each compile unit.
02203   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
02204     CompileUnit *Unit = CompileUnits[i];
02205     
02206     if (Unit->hasContent()) {
02207       // Don't include size of length
02208       EmitInt32(0x1c); EOL("Length of Address Ranges Info");
02209       
02210       EmitInt16(DWARF_VERSION); EOL("Dwarf Version");
02211       
02212       EmitReference("info_begin", Unit->getID());
02213       EOL("Offset of Compilation Unit Info");
02214 
02215       EmitInt8(AddressSize); EOL("Size of Address");
02216 
02217       EmitInt8(0); EOL("Size of Segment Descriptor");
02218 
02219       EmitInt16(0);  EOL("Pad (1)");
02220       EmitInt16(0);  EOL("Pad (2)");
02221 
02222       // Range 1
02223       EmitReference("text_begin", 0); EOL("Address");
02224       EmitDifference("text_end", 0, "text_begin", 0); EOL("Length");
02225 
02226       EmitInt32(0); EOL("EOM (1)");
02227       EmitInt32(0); EOL("EOM (2)");
02228       
02229       O << "\n";
02230     }
02231   }
02232 #endif
02233 }
02234 
02235 /// EmitDebugRanges - Emit visible names into a debug ranges section.
02236 ///
02237 void DwarfWriter::EmitDebugRanges() {
02238   // Start the dwarf ranges section.
02239   Asm->SwitchSection(DwarfRangesSection, 0);
02240   
02241   O << "\n";
02242 }
02243 
02244 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
02245 ///
02246 void DwarfWriter::EmitDebugMacInfo() {
02247   // Start the dwarf macinfo section.
02248   Asm->SwitchSection(DwarfMacInfoSection, 0);
02249   
02250   O << "\n";
02251 }
02252 
02253 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
02254 /// header file.
02255 void DwarfWriter::ConstructCompileUnitDIEs() {
02256   const UniqueVector<CompileUnitDesc *> CUW = DebugInfo->getCompileUnits();
02257   
02258   for (unsigned i = 1, N = CUW.size(); i <= N; ++i) {
02259     CompileUnit *Unit = NewCompileUnit(CUW[i], i);
02260     CompileUnits.push_back(Unit);
02261   }
02262 }
02263 
02264 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible global
02265 /// variables.
02266 void DwarfWriter::ConstructGlobalDIEs() {
02267   std::vector<GlobalVariableDesc *> GlobalVariables =
02268       DebugInfo->getAnchoredDescriptors<GlobalVariableDesc>(*M);
02269   
02270   for (unsigned i = 0, N = GlobalVariables.size(); i < N; ++i) {
02271     GlobalVariableDesc *GVD = GlobalVariables[i];
02272     NewGlobalVariable(GVD);
02273   }
02274 }
02275 
02276 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
02277 /// subprograms.
02278 void DwarfWriter::ConstructSubprogramDIEs() {
02279   std::vector<SubprogramDesc *> Subprograms =
02280       DebugInfo->getAnchoredDescriptors<SubprogramDesc>(*M);
02281   
02282   for (unsigned i = 0, N = Subprograms.size(); i < N; ++i) {
02283     SubprogramDesc *SPD = Subprograms[i];
02284     NewSubprogram(SPD);
02285   }
02286 }
02287 
02288 /// ShouldEmitDwarf - Determine if Dwarf declarations should be made.
02289 ///
02290 bool DwarfWriter::ShouldEmitDwarf() {
02291   // Check if debug info is present.
02292   if (!DebugInfo || !DebugInfo->hasInfo()) return false;
02293   
02294   // Make sure initial declarations are made.
02295   if (!didInitial) {
02296     EmitInitial();
02297   
02298     // Emit common frame information.
02299     EmitInitialDebugFrame();
02300   
02301     // Create all the compile unit DIEs.
02302     ConstructCompileUnitDIEs();
02303     
02304     // Create DIEs for each of the externally visible global variables.
02305     ConstructGlobalDIEs();
02306 
02307     // Create DIEs for each of the externally visible subprograms.
02308     ConstructSubprogramDIEs();
02309 
02310     didInitial = true;
02311   }
02312   
02313   // Okay to emit.
02314   return true;
02315 }
02316 
02317 //===----------------------------------------------------------------------===//
02318 // Main entry points.
02319 //
02320   
02321 DwarfWriter::DwarfWriter(std::ostream &OS, AsmPrinter *A)
02322 : O(OS)
02323 , Asm(A)
02324 , TD(Asm->TM.getTargetData())
02325 , RI(Asm->TM.getRegisterInfo())
02326 , M(NULL)
02327 , MF(NULL)
02328 , DebugInfo(NULL)
02329 , didInitial(false)
02330 , SubprogramCount(0)
02331 , CompileUnits()
02332 , Abbreviations()
02333 , StringPool()
02334 , DescToUnitMap()
02335 , DescToDieMap()
02336 , TypeToDieMap()
02337 , AddressSize(sizeof(int32_t))
02338 , hasLEB128(false)
02339 , hasDotLoc(false)
02340 , hasDotFile(false)
02341 , needsSet(false)
02342 , DwarfAbbrevSection(".debug_abbrev")
02343 , DwarfInfoSection(".debug_info")
02344 , DwarfLineSection(".debug_line")
02345 , DwarfFrameSection(".debug_frame")
02346 , DwarfPubNamesSection(".debug_pubnames")
02347 , DwarfPubTypesSection(".debug_pubtypes")
02348 , DwarfStrSection(".debug_str")
02349 , DwarfLocSection(".debug_loc")
02350 , DwarfARangesSection(".debug_aranges")
02351 , DwarfRangesSection(".debug_ranges")
02352 , DwarfMacInfoSection(".debug_macinfo")
02353 , TextSection(".text")
02354 , DataSection(".data")
02355 {}
02356 DwarfWriter::~DwarfWriter() {
02357   for (unsigned i = 0, N = CompileUnits.size(); i < N; ++i) {
02358     delete CompileUnits[i];
02359   }
02360 }
02361 
02362 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
02363 /// created it.  Set by the target AsmPrinter.
02364 void DwarfWriter::SetDebugInfo(MachineDebugInfo *DI) {
02365   DebugInfo = DI;
02366 }
02367 
02368 /// BeginModule - Emit all Dwarf sections that should come prior to the content.
02369 ///
02370 void DwarfWriter::BeginModule(Module *M) {
02371   this->M = M;
02372   
02373   if (!ShouldEmitDwarf()) return;
02374   EOL("Dwarf Begin Module");
02375 }
02376 
02377 /// EndModule - Emit all Dwarf sections that should come after the content.
02378 ///
02379 void DwarfWriter::EndModule() {
02380   if (!ShouldEmitDwarf()) return;
02381   EOL("Dwarf End Module");
02382   
02383   // Standard sections final addresses.
02384   Asm->SwitchSection(TextSection, 0);
02385   EmitLabel("text_end", 0);
02386   Asm->SwitchSection(DataSection, 0);
02387   EmitLabel("data_end", 0);
02388   
02389   // Compute DIE offsets and sizes.
02390   SizeAndOffsets();
02391   
02392   // Emit all the DIEs into a debug info section
02393   EmitDebugInfo();
02394   
02395   // Corresponding abbreviations into a abbrev section.
02396   EmitAbbreviations();
02397   
02398   // Emit source line correspondence into a debug line section.
02399   EmitDebugLines();
02400   
02401   // Emit info into a debug pubnames section.
02402   EmitDebugPubNames();
02403   
02404   // Emit info into a debug str section.
02405   EmitDebugStr();
02406   
02407   // Emit info into a debug loc section.
02408   EmitDebugLoc();
02409   
02410   // Emit info into a debug aranges section.
02411   EmitDebugARanges();
02412   
02413   // Emit info into a debug ranges section.
02414   EmitDebugRanges();
02415   
02416   // Emit info into a debug macinfo section.
02417   EmitDebugMacInfo();
02418 }
02419 
02420 /// BeginFunction - Gather pre-function debug information.  Assumes being 
02421 /// emitted immediately after the function entry point.
02422 void DwarfWriter::BeginFunction(MachineFunction *MF) {
02423   this->MF = MF;
02424   
02425   // Begin accumulating function debug information.
02426   DebugInfo->BeginFunction(MF);
02427   
02428   if (!ShouldEmitDwarf()) return;
02429   EOL("Dwarf Begin Function");
02430   
02431   // Assumes in correct section after the entry point.
02432   EmitLabel("func_begin", ++SubprogramCount);
02433 }
02434 
02435 /// EndFunction - Gather and emit post-function debug information.
02436 ///
02437 void DwarfWriter::EndFunction() {
02438   if (!ShouldEmitDwarf()) return;
02439   EOL("Dwarf End Function");
02440   
02441   // Define end label for subprogram.
02442   EmitLabel("func_end", SubprogramCount);
02443   
02444   // Construct scopes for subprogram.
02445   ConstructRootScope(DebugInfo->getRootScope());
02446   
02447   // Emit function frame information.
02448   EmitFunctionDebugFrame();
02449   
02450   // Clear function debug information.
02451   DebugInfo->EndFunction();
02452 }