LLVM API Documentation

DwarfWriter.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/DwarfWriter.h - 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.  For
00011 // Details on the Dwarf 3 specfication see DWARF Debugging Information Format
00012 // V.3 reference manual http://dwarf.freestandards.org ,
00013 //
00014 // The role of the Dwarf Writer class is to extract debug information from the
00015 // MachineDebugInfo object, organize it in Dwarf form and then emit it into asm
00016 // the current asm file using data and high level Dwarf directives.
00017 // 
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_CODEGEN_DWARFWRITER_H
00021 #define LLVM_CODEGEN_DWARFWRITER_H
00022 
00023 #include "llvm/ADT/UniqueVector.h"
00024 #include "llvm/Support/DataTypes.h"
00025 
00026 #include <iosfwd>
00027 #include <string>
00028 
00029 
00030 namespace llvm {
00031 
00032 // Forward declarations.
00033 
00034 class AsmPrinter;
00035 class CompileUnit;
00036 class CompileUnitDesc;
00037 class DebugInfoDesc;
00038 class DebugVariable;
00039 class DebugScope;
00040 class DIE;
00041 class DIEAbbrev;
00042 class GlobalVariableDesc;
00043 class MachineDebugInfo;
00044 class MachineFunction;
00045 class MachineLocation;
00046 class MachineMove;
00047 class Module;
00048 class MRegisterInfo;
00049 class SubprogramDesc;
00050 class TargetData;
00051 class Type;
00052 class TypeDesc;
00053   
00054 //===----------------------------------------------------------------------===//
00055 // DWLabel - Labels are used to track locations in the assembler file.
00056 // Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
00057 // category of label (Ex. location) and number is a value unique in that
00058 // category.
00059 class DWLabel {
00060 public:
00061   const char *Tag;                    // Label category tag. Should always be
00062                                       // a staticly declared C string.
00063   unsigned    Number;                 // Unique number.
00064 
00065   DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
00066 };
00067 
00068 //===----------------------------------------------------------------------===//
00069 // DwarfWriter - Emits Dwarf debug and exception handling directives.
00070 //
00071 class DwarfWriter {
00072 protected:
00073 
00074   //===--------------------------------------------------------------------===//
00075   // Core attributes used by the Dwarf  writer.
00076   //
00077   
00078   //
00079   /// O - Stream to .s file.
00080   ///
00081   std::ostream &O;
00082 
00083   /// Asm - Target of Dwarf emission.
00084   ///
00085   AsmPrinter *Asm;
00086   
00087   /// TD - Target data.
00088   const TargetData &TD;
00089   
00090   /// RI - Register Information.
00091   const MRegisterInfo *RI;
00092   
00093   /// M - Current module.
00094   ///
00095   Module *M;
00096   
00097   /// MF - Current machine function.
00098   ///
00099   MachineFunction *MF;
00100   
00101   /// DebugInfo - Collected debug information.
00102   ///
00103   MachineDebugInfo *DebugInfo;
00104   
00105   /// didInitial - Flag to indicate if initial emission has been done.
00106   ///
00107   bool didInitial;
00108   
00109   /// SubprogramCount - The running count of functions being compiled.
00110   ///
00111   unsigned SubprogramCount;
00112   
00113   //===--------------------------------------------------------------------===//
00114   // Attributes used to construct specific Dwarf sections.
00115   //
00116   
00117   /// CompileUnits - All the compile units involved in this build.  The index
00118   /// of each entry in this vector corresponds to the sources in DebugInfo.
00119   std::vector<CompileUnit *> CompileUnits;
00120 
00121   /// Abbreviations - A UniqueVector of TAG structure abbreviations.
00122   ///
00123   UniqueVector<DIEAbbrev> Abbreviations;
00124   
00125   /// StringPool - A UniqueVector of strings used by indirect references.
00126   /// UnitMap - Map debug information descriptor to compile unit.
00127    ///
00128   UniqueVector<std::string> StringPool;
00129 
00130   /// UnitMap - Map debug information descriptor to compile unit.
00131   ///
00132   std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
00133   
00134   /// DescToDieMap - Tracks the mapping of top level debug informaton
00135   /// descriptors to debug information entries.
00136   std::map<DebugInfoDesc *, DIE *> DescToDieMap;
00137   
00138   /// TypeToDieMap - Type to DIEType map.
00139   ///
00140   // FIXME - Should not be needed.
00141   std::map<Type *, DIE *> TypeToDieMap;
00142   
00143   //===--------------------------------------------------------------------===//
00144   // Properties to be set by the derived class ctor, used to configure the
00145   // Dwarf writer.
00146   //
00147   
00148   /// AddressSize - Size of addresses used in file.
00149   ///
00150   unsigned AddressSize;
00151 
00152   /// hasLEB128 - True if target asm supports leb128 directives.
00153   ///
00154   bool hasLEB128; /// Defaults to false.
00155   
00156   /// hasDotLoc - True if target asm supports .loc directives.
00157   ///
00158   bool hasDotLoc; /// Defaults to false.
00159   
00160   /// hasDotFile - True if target asm supports .file directives.
00161   ///
00162   bool hasDotFile; /// Defaults to false.
00163   
00164   /// needsSet - True if target asm can't compute addresses on data
00165   /// directives.
00166   bool needsSet; /// Defaults to false.
00167   
00168   /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
00169   ///
00170   const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
00171 
00172   /// DwarfInfoSection - Section directive for Dwarf info.
00173   ///
00174   const char *DwarfInfoSection; /// Defaults to ".debug_info".
00175 
00176   /// DwarfLineSection - Section directive for Dwarf info.
00177   ///
00178   const char *DwarfLineSection; /// Defaults to ".debug_line".
00179   
00180   /// DwarfFrameSection - Section directive for Dwarf info.
00181   ///
00182   const char *DwarfFrameSection; /// Defaults to ".debug_frame".
00183   
00184   /// DwarfPubNamesSection - Section directive for Dwarf info.
00185   ///
00186   const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
00187   
00188   /// DwarfPubTypesSection - Section directive for Dwarf info.
00189   ///
00190   const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
00191   
00192   /// DwarfStrSection - Section directive for Dwarf info.
00193   ///
00194   const char *DwarfStrSection; /// Defaults to ".debug_str".
00195 
00196   /// DwarfLocSection - Section directive for Dwarf info.
00197   ///
00198   const char *DwarfLocSection; /// Defaults to ".debug_loc".
00199 
00200   /// DwarfARangesSection - Section directive for Dwarf info.
00201   ///
00202   const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
00203 
00204   /// DwarfRangesSection - Section directive for Dwarf info.
00205   ///
00206   const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
00207 
00208   /// DwarfMacInfoSection - Section directive for Dwarf info.
00209   ///
00210   const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
00211 
00212   /// TextSection - Section directive for standard text.
00213   ///
00214   const char *TextSection; /// Defaults to ".text".
00215   
00216   /// DataSection - Section directive for standard data.
00217   ///
00218   const char *DataSection; /// Defaults to ".data".
00219 
00220   //===--------------------------------------------------------------------===//
00221   // Emission and print routines
00222   //
00223 
00224 public:
00225   /// getAddressSize - Return the size of a target address in bytes.
00226   ///
00227   unsigned getAddressSize() const { return AddressSize; }
00228 
00229   /// PrintHex - Print a value as a hexidecimal value.
00230   ///
00231   void PrintHex(int Value) const;
00232 
00233   /// EOL - Print a newline character to asm stream.  If a comment is present
00234   /// then it will be printed first.  Comments should not contain '\n'.
00235   void EOL(const std::string &Comment) const;
00236   
00237   /// EmitAlign - Print a align directive.
00238   ///
00239   void EmitAlign(unsigned Alignment) const;
00240                                         
00241   /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
00242   /// unsigned leb128 value.
00243   void EmitULEB128Bytes(unsigned Value) const;
00244   
00245   /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
00246   /// signed leb128 value.
00247   void EmitSLEB128Bytes(int Value) const;
00248   
00249   /// PrintULEB128 - Print a series of hexidecimal values (separated by
00250   /// commas) representing an unsigned leb128 value.
00251   void PrintULEB128(unsigned Value) const;
00252 
00253   /// SizeULEB128 - Compute the number of bytes required for an unsigned
00254   /// leb128 value.
00255   static unsigned SizeULEB128(unsigned Value);
00256   
00257   /// PrintSLEB128 - Print a series of hexidecimal values (separated by
00258   /// commas) representing a signed leb128 value.
00259   void PrintSLEB128(int Value) const;
00260   
00261   /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
00262   /// value.
00263   static unsigned SizeSLEB128(int Value);
00264   
00265   /// EmitInt8 - Emit a byte directive and value.
00266   ///
00267   void EmitInt8(int Value) const;
00268 
00269   /// EmitInt16 - Emit a short directive and value.
00270   ///
00271   void EmitInt16(int Value) const;
00272 
00273   /// EmitInt32 - Emit a long directive and value.
00274   ///
00275   void EmitInt32(int Value) const;
00276   
00277   /// EmitInt64 - Emit a long long directive and value.
00278   ///
00279   void EmitInt64(uint64_t Value) const;
00280   
00281   /// EmitString - Emit a string with quotes and a null terminator.
00282   /// Special characters are emitted properly. (Eg. '\t')
00283   void EmitString(const std::string &String) const;
00284 
00285   /// PrintLabelName - Print label name in form used by Dwarf writer.
00286   ///
00287   void PrintLabelName(DWLabel Label) const {
00288     PrintLabelName(Label.Tag, Label.Number);
00289   }
00290   void PrintLabelName(const char *Tag, unsigned Number) const;
00291   
00292   /// EmitLabel - Emit location label for internal use by Dwarf.
00293   ///
00294   void EmitLabel(DWLabel Label) const {
00295     EmitLabel(Label.Tag, Label.Number);
00296   }
00297   void EmitLabel(const char *Tag, unsigned Number) const;
00298   
00299   /// EmitReference - Emit a reference to a label.
00300   ///
00301   void EmitReference(DWLabel Label) const {
00302     EmitReference(Label.Tag, Label.Number);
00303   }
00304   void EmitReference(const char *Tag, unsigned Number) const;
00305   void EmitReference(const std::string &Name) const;
00306 
00307   /// EmitDifference - Emit the difference between two labels.  Some
00308   /// assemblers do not behave with absolute expressions with data directives,
00309   /// so there is an option (needsSet) to use an intermediary set expression.
00310   void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
00311     EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
00312   }
00313   void EmitDifference(const char *TagHi, unsigned NumberHi,
00314                       const char *TagLo, unsigned NumberLo) const;
00315                       
00316   /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
00317   ///  
00318   unsigned NewAbbreviation(DIEAbbrev *Abbrev);
00319   
00320   /// NewString - Add a string to the constant pool and returns a label.
00321   ///
00322   DWLabel NewString(const std::string &String);
00323   
00324   /// getDieMapSlotFor - Returns the debug information entry map slot for the
00325   /// specified debug descriptor.
00326   DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
00327                                  
00328 private:
00329 
00330   /// AddSourceLine - Add location information to specified debug information
00331   /// entry. 
00332   void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
00333 
00334   /// AddAddress - Add an address attribute to a die based on the location
00335   /// provided.
00336   void AddAddress(DIE *Die, unsigned Attribute,
00337                   const MachineLocation &Location);
00338 
00339   /// NewType - Create a new type DIE.
00340   ///
00341   DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
00342   
00343   /// NewCompileUnit - Create new compile unit and it's die.
00344   ///
00345   CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
00346   
00347   /// FindCompileUnit - Get the compile unit for the given descriptor.
00348   ///
00349   CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
00350   
00351   /// NewGlobalVariable - Make a new global variable DIE.
00352   ///
00353   DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
00354 
00355   /// NewSubprogram - Add a new subprogram DIE.
00356   ///
00357   DIE *NewSubprogram(SubprogramDesc *SPD);
00358 
00359   /// NewScopeVariable - Create a new scope variable.
00360   ///
00361   DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
00362 
00363   /// ConstructScope - Construct the components of a scope.
00364   ///
00365   void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
00366                       CompileUnit *Unit);
00367 
00368   /// ConstructRootScope - Construct the scope for the subprogram.
00369   ///
00370   void ConstructRootScope(DebugScope *RootScope);
00371 
00372   /// EmitInitial - Emit initial Dwarf declarations.
00373   ///
00374   void EmitInitial() const;
00375   
00376   /// EmitDIE - Recusively Emits a debug information entry.
00377   ///
00378   void EmitDIE(DIE *Die) const;
00379   
00380   /// SizeAndOffsetDie - Compute the size and offset of a DIE.
00381   ///
00382   unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
00383 
00384   /// SizeAndOffsets - Compute the size and offset of all the DIEs.
00385   ///
00386   void SizeAndOffsets();
00387   
00388   /// EmitFrameMoves - Emit frame instructions to describe the layout of the
00389   /// frame.
00390   void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
00391                       std::vector<MachineMove *> &Moves);
00392 
00393   /// EmitDebugInfo - Emit the debug info section.
00394   ///
00395   void EmitDebugInfo() const;
00396   
00397   /// EmitAbbreviations - Emit the abbreviation section.
00398   ///
00399   void EmitAbbreviations() const;
00400   
00401   /// EmitDebugLines - Emit source line information.
00402   ///
00403   void EmitDebugLines() const;
00404 
00405   /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
00406   ///
00407   void EmitInitialDebugFrame();
00408     
00409   /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
00410   /// section.
00411   void EmitFunctionDebugFrame();
00412 
00413   /// EmitDebugPubNames - Emit info into a debug pubnames section.
00414   ///
00415   void EmitDebugPubNames();
00416   
00417   /// EmitDebugStr - Emit info into a debug str section.
00418   ///
00419   void EmitDebugStr();
00420   
00421   /// EmitDebugLoc - Emit info into a debug loc section.
00422   ///
00423   void EmitDebugLoc();
00424   
00425   /// EmitDebugARanges - Emit info into a debug aranges section.
00426   ///
00427   void EmitDebugARanges();
00428   
00429   /// EmitDebugRanges - Emit info into a debug ranges section.
00430   ///
00431   void EmitDebugRanges();
00432   
00433   /// EmitDebugMacInfo - Emit info into a debug macinfo section.
00434   ///
00435   void EmitDebugMacInfo();
00436   
00437   /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
00438   /// header file.
00439   void ConstructCompileUnitDIEs();
00440   
00441   /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
00442   /// global variables.
00443   void ConstructGlobalDIEs();
00444 
00445   /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
00446   /// subprograms.
00447   void ConstructSubprogramDIEs();
00448 
00449   /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
00450   /// When called it also checks to see if debug info is newly available.  if
00451   /// so the initial Dwarf headers are emitted.
00452   bool ShouldEmitDwarf();
00453 
00454 public:
00455   
00456   DwarfWriter(std::ostream &OS, AsmPrinter *A);
00457   virtual ~DwarfWriter();
00458   
00459   /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
00460   /// created it.  Set by the target AsmPrinter.
00461   void SetDebugInfo(MachineDebugInfo *DI);
00462 
00463   //===--------------------------------------------------------------------===//
00464   // Main entry points.
00465   //
00466   
00467   /// BeginModule - Emit all Dwarf sections that should come prior to the
00468   /// content.
00469   void BeginModule(Module *M);
00470   
00471   /// EndModule - Emit all Dwarf sections that should come after the content.
00472   ///
00473   void EndModule();
00474   
00475   /// BeginFunction - Gather pre-function debug information.  Assumes being 
00476   /// emitted immediately after the function entry point.
00477   void BeginFunction(MachineFunction *MF);
00478   
00479   /// EndFunction - Gather and emit post-function debug information.
00480   ///
00481   void EndFunction();
00482 };
00483 
00484 } // end llvm namespace
00485 
00486 #endif