LLVM API Documentation

MachineDebugInfo.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineDebugInfo.h -------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by James M. Laskey and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Collect debug information for a module.  This information should be in a
00011 // neutral form that can be used by different debugging schemes.
00012 //
00013 // The organization of information is primarily clustered around the source
00014 // compile units.  The main exception is source line correspondence where
00015 // inlining may interleave code from various compile units.
00016 //
00017 // The following information can be retrieved from the MachineDebugInfo.
00018 //
00019 //  -- Source directories - Directories are uniqued based on their canonical
00020 //     string and assigned a sequential numeric ID (base 1.)
00021 //  -- Source files - Files are also uniqued based on their name and directory
00022 //     ID.  A file ID is sequential number (base 1.)
00023 //  -- Source line coorespondence - A vector of file ID, line#, column# triples.
00024 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
00025 //     corresponding to each entry in the source line list.  This allows a debug
00026 //     emitter to generate labels referenced by debug information tables.
00027 //
00028 //===----------------------------------------------------------------------===//
00029 
00030 #ifndef LLVM_CODEGEN_MACHINEDEBUGINFO_H
00031 #define LLVM_CODEGEN_MACHINEDEBUGINFO_H
00032 
00033 #include "llvm/Support/Dwarf.h"
00034 #include "llvm/Support/DataTypes.h"
00035 #include "llvm/ADT/UniqueVector.h"
00036 #include "llvm/GlobalValue.h"
00037 #include "llvm/Pass.h"
00038 #include "llvm/User.h"
00039 
00040 #include <string>
00041 #include <set>
00042 
00043 namespace llvm {
00044 
00045 //===----------------------------------------------------------------------===//
00046 // Forward declarations.
00047 class Constant;
00048 class DebugInfoDesc;
00049 class GlobalVariable;
00050 class MachineFunction;
00051 class MachineMove;
00052 class Module;
00053 class PointerType;
00054 class StructType;
00055 
00056 //===----------------------------------------------------------------------===//
00057 // Debug info constants.
00058 
00059 enum {
00060   LLVMDebugVersion = 3                  // Current version of debug information.
00061 };
00062 
00063 //===----------------------------------------------------------------------===//
00064 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
00065 /// the supplied DebugInfoDesc.
00066 class DIVisitor {
00067 public:
00068   DIVisitor() {}
00069   virtual ~DIVisitor() {}
00070 
00071   /// ApplyToFields - Target the visitor to each field of the debug information
00072   /// descriptor.
00073   void ApplyToFields(DebugInfoDesc *DD);
00074   
00075   /// Apply - Subclasses override each of these methods to perform the
00076   /// appropriate action for the type of field.
00077   virtual void Apply(int &Field) = 0;
00078   virtual void Apply(unsigned &Field) = 0;
00079   virtual void Apply(int64_t &Field) = 0;
00080   virtual void Apply(uint64_t &Field) = 0;
00081   virtual void Apply(bool &Field) = 0;
00082   virtual void Apply(std::string &Field) = 0;
00083   virtual void Apply(DebugInfoDesc *&Field) = 0;
00084   virtual void Apply(GlobalVariable *&Field) = 0;
00085   virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
00086 };
00087 
00088 //===----------------------------------------------------------------------===//
00089 /// DebugInfoDesc - This class is the base class for debug info descriptors.
00090 ///
00091 class DebugInfoDesc {
00092 private:
00093   unsigned Tag;                         // Content indicator.  Dwarf values are
00094                                         // used but that does not limit use to
00095                                         // Dwarf writers.
00096   
00097 protected:
00098   DebugInfoDesc(unsigned T) : Tag(T) {}
00099   
00100 public:
00101   virtual ~DebugInfoDesc() {}
00102 
00103   // Accessors
00104   unsigned getTag()          const { return Tag; }
00105   
00106   /// TagFromGlobal - Returns the Tag number from a debug info descriptor
00107   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
00108   static unsigned TagFromGlobal(GlobalVariable *GV);
00109 
00110   /// DescFactory - Create an instance of debug info descriptor based on Tag.
00111   /// Return NULL if not a recognized Tag.
00112   static DebugInfoDesc *DescFactory(unsigned Tag);
00113   
00114   /// getLinkage - get linkage appropriate for this type of descriptor.
00115   ///
00116   virtual GlobalValue::LinkageTypes getLinkage() const;
00117     
00118   //===--------------------------------------------------------------------===//
00119   // Subclasses should supply the following static methods.
00120   
00121   // Implement isa/cast/dyncast.
00122   static bool classof(const DebugInfoDesc *) { return true; }
00123   
00124   //===--------------------------------------------------------------------===//
00125   // Subclasses should supply the following virtual methods.
00126   
00127   /// ApplyToFields - Target the vistor to the fields of the descriptor.
00128   ///
00129   virtual void ApplyToFields(DIVisitor *Visitor);
00130 
00131   /// getDescString - Return a string used to compose global names and labels.
00132   ///
00133   virtual const char *getDescString() const = 0;
00134   
00135   /// getTypeString - Return a string used to label this descriptor's type.
00136   ///
00137   virtual const char *getTypeString() const = 0;
00138   
00139 #ifndef NDEBUG
00140   virtual void dump() = 0;
00141 #endif
00142 };
00143 
00144 //===----------------------------------------------------------------------===//
00145 /// AnchorDesc - Descriptors of this class act as markers for identifying
00146 /// descriptors of certain groups.
00147 class AnchoredDesc;
00148 class AnchorDesc : public DebugInfoDesc {
00149 private:
00150   unsigned AnchorTag;                   // Tag number of descriptors anchored
00151                                         // by this object.
00152   
00153 public:
00154   AnchorDesc();
00155   AnchorDesc(AnchoredDesc *D);
00156   
00157   // Accessors
00158   unsigned getAnchorTag() const { return AnchorTag; }
00159 
00160   // Implement isa/cast/dyncast.
00161   static bool classof(const AnchorDesc *) { return true; }
00162   static bool classof(const DebugInfoDesc *D);
00163 
00164   /// getLinkage - get linkage appropriate for this type of descriptor.
00165   ///
00166   virtual GlobalValue::LinkageTypes getLinkage() const;
00167 
00168   /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
00169   ///
00170   virtual void ApplyToFields(DIVisitor *Visitor);
00171 
00172   /// getDescString - Return a string used to compose global names and labels.
00173   ///
00174   virtual const char *getDescString() const;
00175     
00176   /// getTypeString - Return a string used to label this descriptor's type.
00177   ///
00178   virtual const char *getTypeString() const;
00179     
00180 #ifndef NDEBUG
00181   virtual void dump();
00182 #endif
00183 };
00184 
00185 //===----------------------------------------------------------------------===//
00186 /// AnchoredDesc - This class manages anchors for a variety of top level
00187 /// descriptors.
00188 class AnchoredDesc : public DebugInfoDesc {
00189 private:  
00190   AnchorDesc *Anchor;                   // Anchor for all descriptors of the
00191                                         // same type.
00192 
00193 protected:
00194 
00195   AnchoredDesc(unsigned T);
00196 
00197 public:  
00198   // Accessors.
00199   AnchorDesc *getAnchor() const { return Anchor; }
00200   void setAnchor(AnchorDesc *A) { Anchor = A; }
00201 
00202   //===--------------------------------------------------------------------===//
00203   // Subclasses should supply the following virtual methods.
00204   
00205   /// getAnchorString - Return a string used to label descriptor's anchor.
00206   ///
00207   virtual const char *getAnchorString() const = 0;
00208     
00209   /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
00210   ///
00211   virtual void ApplyToFields(DIVisitor *Visitor);
00212 };
00213 
00214 //===----------------------------------------------------------------------===//
00215 /// CompileUnitDesc - This class packages debug information associated with a 
00216 /// source/header file.
00217 class CompileUnitDesc : public AnchoredDesc {
00218 private:  
00219   unsigned DebugVersion;                // LLVM debug version when produced.
00220   unsigned Language;                    // Language number (ex. DW_LANG_C89.)
00221   std::string FileName;                 // Source file name.
00222   std::string Directory;                // Source file directory.
00223   std::string Producer;                 // Compiler string.
00224   
00225 public:
00226   CompileUnitDesc();
00227   
00228   
00229   // Accessors
00230   unsigned getDebugVersion()              const { return DebugVersion; }
00231   unsigned getLanguage()                  const { return Language; }
00232   const std::string &getFileName()        const { return FileName; }
00233   const std::string &getDirectory()       const { return Directory; }
00234   const std::string &getProducer()        const { return Producer; }
00235   void setLanguage(unsigned L)                  { Language = L; }
00236   void setFileName(const std::string &FN)       { FileName = FN; }
00237   void setDirectory(const std::string &D)       { Directory = D; }
00238   void setProducer(const std::string &P)        { Producer = P; }
00239   
00240   // FIXME - Need translation unit getter/setter.
00241 
00242   // Implement isa/cast/dyncast.
00243   static bool classof(const CompileUnitDesc *) { return true; }
00244   static bool classof(const DebugInfoDesc *D);
00245   
00246   /// DebugVersionFromGlobal - Returns the version number from a compile unit
00247   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
00248   static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
00249   
00250   /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
00251   ///
00252   virtual void ApplyToFields(DIVisitor *Visitor);
00253 
00254   /// getDescString - Return a string used to compose global names and labels.
00255   ///
00256   virtual const char *getDescString() const;
00257     
00258   /// getTypeString - Return a string used to label this descriptor's type.
00259   ///
00260   virtual const char *getTypeString() const;
00261   
00262   /// getAnchorString - Return a string used to label this descriptor's anchor.
00263   ///
00264   static const char *AnchorString;
00265   virtual const char *getAnchorString() const;
00266     
00267 #ifndef NDEBUG
00268   virtual void dump();
00269 #endif
00270 };
00271 
00272 //===----------------------------------------------------------------------===//
00273 /// TypeDesc - This class packages debug information associated with a type.
00274 ///
00275 class TypeDesc : public DebugInfoDesc {
00276 private:
00277   DebugInfoDesc *Context;               // Context debug descriptor.
00278   std::string Name;                     // Type name (may be empty.)
00279   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
00280   unsigned Line;                        // Defined line# (may be zero.)
00281   uint64_t Size;                        // Type bit size (may be zero.)
00282   uint64_t Align;                       // Type bit alignment (may be zero.)
00283   uint64_t Offset;                      // Type bit offset (may be zero.)
00284 
00285 public:
00286   TypeDesc(unsigned T);
00287 
00288   // Accessors
00289   DebugInfoDesc *getContext()                const { return Context; }
00290   const std::string &getName()               const { return Name; }
00291   CompileUnitDesc *getFile()                 const { return File; }
00292   unsigned getLine()                         const { return Line; }
00293   uint64_t getSize()                         const { return Size; }
00294   uint64_t getAlign()                        const { return Align; }
00295   uint64_t getOffset()                       const { return Offset; }
00296   void setContext(DebugInfoDesc *C)                { Context = C; }
00297   void setName(const std::string &N)               { Name = N; }
00298   void setFile(CompileUnitDesc *U)                 { File = U; }
00299   void setLine(unsigned L)                         { Line = L; }
00300   void setSize(uint64_t S)                         { Size = S; }
00301   void setAlign(uint64_t A)                        { Align = A; }
00302   void setOffset(uint64_t O)                       { Offset = O; }
00303   
00304   /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
00305   ///
00306   virtual void ApplyToFields(DIVisitor *Visitor);
00307 
00308   /// getDescString - Return a string used to compose global names and labels.
00309   ///
00310   virtual const char *getDescString() const;
00311 
00312   /// getTypeString - Return a string used to label this descriptor's type.
00313   ///
00314   virtual const char *getTypeString() const;
00315   
00316 #ifndef NDEBUG
00317   virtual void dump();
00318 #endif
00319 };
00320 
00321 //===----------------------------------------------------------------------===//
00322 /// BasicTypeDesc - This class packages debug information associated with a
00323 /// basic type (eg. int, bool, double.)
00324 class BasicTypeDesc : public TypeDesc {
00325 private:
00326   unsigned Encoding;                    // Type encoding.
00327   
00328 public:
00329   BasicTypeDesc();
00330   
00331   // Accessors
00332   unsigned getEncoding()                     const { return Encoding; }
00333   void setEncoding(unsigned E)                     { Encoding = E; }
00334 
00335   // Implement isa/cast/dyncast.
00336   static bool classof(const BasicTypeDesc *) { return true; }
00337   static bool classof(const DebugInfoDesc *D);
00338   
00339   /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
00340   ///
00341   virtual void ApplyToFields(DIVisitor *Visitor);
00342 
00343   /// getDescString - Return a string used to compose global names and labels.
00344   ///
00345   virtual const char *getDescString() const;
00346 
00347   /// getTypeString - Return a string used to label this descriptor's type.
00348   ///
00349   virtual const char *getTypeString() const;
00350 
00351 #ifndef NDEBUG
00352   virtual void dump();
00353 #endif
00354 };
00355 
00356 
00357 //===----------------------------------------------------------------------===//
00358 /// DerivedTypeDesc - This class packages debug information associated with a
00359 /// derived types (eg., typedef, pointer, reference.)
00360 class DerivedTypeDesc : public TypeDesc {
00361 private:
00362   TypeDesc *FromType;                   // Type derived from.
00363 
00364 public:
00365   DerivedTypeDesc(unsigned T);
00366   
00367   // Accessors
00368   TypeDesc *getFromType()                    const { return FromType; }
00369   void setFromType(TypeDesc *F)                    { FromType = F; }
00370 
00371   // Implement isa/cast/dyncast.
00372   static bool classof(const DerivedTypeDesc *) { return true; }
00373   static bool classof(const DebugInfoDesc *D);
00374   
00375   /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
00376   ///
00377   virtual void ApplyToFields(DIVisitor *Visitor);
00378 
00379   /// getDescString - Return a string used to compose global names and labels.
00380   ///
00381   virtual const char *getDescString() const;
00382 
00383   /// getTypeString - Return a string used to label this descriptor's type.
00384   ///
00385   virtual const char *getTypeString() const;
00386 
00387 #ifndef NDEBUG
00388   virtual void dump();
00389 #endif
00390 };
00391 
00392 //===----------------------------------------------------------------------===//
00393 /// CompositeTypeDesc - This class packages debug information associated with a
00394 /// array/struct types (eg., arrays, struct, union, enums.)
00395 class CompositeTypeDesc : public DerivedTypeDesc {
00396 private:
00397   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
00398 
00399 public:
00400   CompositeTypeDesc(unsigned T);
00401   
00402   // Accessors
00403   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
00404 
00405   // Implement isa/cast/dyncast.
00406   static bool classof(const CompositeTypeDesc *) { return true; }
00407   static bool classof(const DebugInfoDesc *D);
00408   
00409   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
00410   ///
00411   virtual void ApplyToFields(DIVisitor *Visitor);
00412 
00413   /// getDescString - Return a string used to compose global names and labels.
00414   ///
00415   virtual const char *getDescString() const;
00416 
00417   /// getTypeString - Return a string used to label this descriptor's type.
00418   ///
00419   virtual const char *getTypeString() const;
00420 
00421 #ifndef NDEBUG
00422   virtual void dump();
00423 #endif
00424 };
00425 
00426 //===----------------------------------------------------------------------===//
00427 /// SubrangeDesc - This class packages debug information associated with integer
00428 /// value ranges.
00429 class SubrangeDesc : public DebugInfoDesc {
00430 private:
00431   int64_t Lo;                           // Low value of range.
00432   int64_t Hi;                           // High value of range.
00433 
00434 public:
00435   SubrangeDesc();
00436   
00437   // Accessors
00438   int64_t getLo()                            const { return Lo; }
00439   int64_t getHi()                            const { return Hi; }
00440   void setLo(int64_t L)                            { Lo = L; }
00441   void setHi(int64_t H)                            { Hi = H; }
00442 
00443   // Implement isa/cast/dyncast.
00444   static bool classof(const SubrangeDesc *) { return true; }
00445   static bool classof(const DebugInfoDesc *D);
00446   
00447   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
00448   ///
00449   virtual void ApplyToFields(DIVisitor *Visitor);
00450 
00451   /// getDescString - Return a string used to compose global names and labels.
00452   ///
00453   virtual const char *getDescString() const;
00454     
00455   /// getTypeString - Return a string used to label this descriptor's type.
00456   ///
00457   virtual const char *getTypeString() const;
00458 
00459 #ifndef NDEBUG
00460   virtual void dump();
00461 #endif
00462 };
00463 
00464 //===----------------------------------------------------------------------===//
00465 /// EnumeratorDesc - This class packages debug information associated with
00466 /// named integer constants.
00467 class EnumeratorDesc : public DebugInfoDesc {
00468 private:
00469   std::string Name;                     // Enumerator name.
00470   int64_t Value;                        // Enumerator value.
00471 
00472 public:
00473   EnumeratorDesc();
00474   
00475   // Accessors
00476   const std::string &getName()               const { return Name; }
00477   int64_t getValue()                         const { return Value; }
00478   void setName(const std::string &N)               { Name = N; }
00479   void setValue(int64_t V)                         { Value = V; }
00480 
00481   // Implement isa/cast/dyncast.
00482   static bool classof(const EnumeratorDesc *) { return true; }
00483   static bool classof(const DebugInfoDesc *D);
00484   
00485   /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
00486   ///
00487   virtual void ApplyToFields(DIVisitor *Visitor);
00488 
00489   /// getDescString - Return a string used to compose global names and labels.
00490   ///
00491   virtual const char *getDescString() const;
00492     
00493   /// getTypeString - Return a string used to label this descriptor's type.
00494   ///
00495   virtual const char *getTypeString() const;
00496 
00497 #ifndef NDEBUG
00498   virtual void dump();
00499 #endif
00500 };
00501 
00502 //===----------------------------------------------------------------------===//
00503 /// VariableDesc - This class packages debug information associated with a
00504 /// subprogram variable.
00505 ///
00506 class VariableDesc : public DebugInfoDesc {
00507 private:
00508   DebugInfoDesc *Context;               // Context debug descriptor.
00509   std::string Name;                     // Type name (may be empty.)
00510   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
00511   unsigned Line;                        // Defined line# (may be zero.)
00512   TypeDesc *TyDesc;                     // Type of variable.
00513 
00514 public:
00515   VariableDesc(unsigned T);
00516 
00517   // Accessors
00518   DebugInfoDesc *getContext()                const { return Context; }
00519   const std::string &getName()               const { return Name; }
00520   CompileUnitDesc *getFile()                 const { return File; }
00521   unsigned getLine()                         const { return Line; }
00522   TypeDesc *getType()                        const { return TyDesc; }
00523   void setContext(DebugInfoDesc *C)                { Context = C; }
00524   void setName(const std::string &N)               { Name = N; }
00525   void setFile(CompileUnitDesc *U)                 { File = U; }
00526   void setLine(unsigned L)                         { Line = L; }
00527   void setType(TypeDesc *T)                        { TyDesc = T; }
00528   
00529   // Implement isa/cast/dyncast.
00530   static bool classof(const VariableDesc *) { return true; }
00531   static bool classof(const DebugInfoDesc *D);
00532   
00533   /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
00534   ///
00535   virtual void ApplyToFields(DIVisitor *Visitor);
00536 
00537   /// getDescString - Return a string used to compose global names and labels.
00538   ///
00539   virtual const char *getDescString() const;
00540 
00541   /// getTypeString - Return a string used to label this descriptor's type.
00542   ///
00543   virtual const char *getTypeString() const;
00544   
00545 #ifndef NDEBUG
00546   virtual void dump();
00547 #endif
00548 };
00549 
00550 //===----------------------------------------------------------------------===//
00551 /// GlobalDesc - This class is the base descriptor for global functions and
00552 /// variables.
00553 class GlobalDesc : public AnchoredDesc {
00554 private:
00555   DebugInfoDesc *Context;               // Context debug descriptor.
00556   std::string Name;                     // Global name.
00557   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
00558   unsigned Line;                        // Defined line# (may be zero.)
00559   TypeDesc *TyDesc;                     // Type debug descriptor.
00560   bool IsStatic;                        // Is the global a static.
00561   bool IsDefinition;                    // Is the global defined in context.
00562   
00563 protected:
00564   GlobalDesc(unsigned T);
00565 
00566 public:
00567   // Accessors
00568   DebugInfoDesc *getContext()                const { return Context; }
00569   const std::string &getName()               const { return Name; }
00570   CompileUnitDesc *getFile()                 const { return File; }
00571   unsigned getLine()                         const { return Line; }
00572   TypeDesc *getType()                        const { return TyDesc; }
00573   bool isStatic()                            const { return IsStatic; }
00574   bool isDefinition()                        const { return IsDefinition; }
00575   void setContext(DebugInfoDesc *C)                { Context = C; }
00576   void setName(const std::string &N)               { Name = N; }
00577   void setFile(CompileUnitDesc *U)                 { File = U; }
00578   void setLine(unsigned L)                         { Line = L; }
00579   void setType(TypeDesc *T)                        { TyDesc = T; }
00580   void setIsStatic(bool IS)                        { IsStatic = IS; }
00581   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
00582 
00583   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
00584   ///
00585   virtual void ApplyToFields(DIVisitor *Visitor);
00586 };
00587 
00588 //===----------------------------------------------------------------------===//
00589 /// GlobalVariableDesc - This class packages debug information associated with a
00590 /// GlobalVariable.
00591 class GlobalVariableDesc : public GlobalDesc {
00592 private:
00593   GlobalVariable *Global;               // llvm global.
00594   
00595 public:
00596   GlobalVariableDesc();
00597 
00598   // Accessors.
00599   GlobalVariable *getGlobalVariable()        const { return Global; }
00600   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
00601  
00602   // Implement isa/cast/dyncast.
00603   static bool classof(const GlobalVariableDesc *) { return true; }
00604   static bool classof(const DebugInfoDesc *D);
00605   
00606   /// ApplyToFields - Target the visitor to the fields of the
00607   /// GlobalVariableDesc.
00608   virtual void ApplyToFields(DIVisitor *Visitor);
00609 
00610   /// getDescString - Return a string used to compose global names and labels.
00611   ///
00612   virtual const char *getDescString() const;
00613 
00614   /// getTypeString - Return a string used to label this descriptor's type.
00615   ///
00616   virtual const char *getTypeString() const;
00617   
00618   /// getAnchorString - Return a string used to label this descriptor's anchor.
00619   ///
00620   static const char *AnchorString;
00621   virtual const char *getAnchorString() const;
00622     
00623 #ifndef NDEBUG
00624   virtual void dump();
00625 #endif
00626 };
00627 
00628 //===----------------------------------------------------------------------===//
00629 /// SubprogramDesc - This class packages debug information associated with a
00630 /// subprogram/function.
00631 class SubprogramDesc : public GlobalDesc {
00632 private:
00633   
00634 public:
00635   SubprogramDesc();
00636   
00637   // Accessors
00638   
00639   // Implement isa/cast/dyncast.
00640   static bool classof(const SubprogramDesc *) { return true; }
00641   static bool classof(const DebugInfoDesc *D);
00642   
00643   /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
00644   ///
00645   virtual void ApplyToFields(DIVisitor *Visitor);
00646 
00647   /// getDescString - Return a string used to compose global names and labels.
00648   ///
00649   virtual const char *getDescString() const;
00650 
00651   /// getTypeString - Return a string used to label this descriptor's type.
00652   ///
00653   virtual const char *getTypeString() const;
00654   
00655   /// getAnchorString - Return a string used to label this descriptor's anchor.
00656   ///
00657   static const char *AnchorString;
00658   virtual const char *getAnchorString() const;
00659     
00660 #ifndef NDEBUG
00661   virtual void dump();
00662 #endif
00663 };
00664 
00665 //===----------------------------------------------------------------------===//
00666 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
00667 ///
00668 class BlockDesc : public DebugInfoDesc {
00669 private:
00670   DebugInfoDesc *Context;               // Context debug descriptor.
00671 
00672 public:
00673   BlockDesc();
00674   
00675   // Accessors
00676   DebugInfoDesc *getContext()                const { return Context; }
00677   void setContext(DebugInfoDesc *C)                { Context = C; }
00678   
00679   // Implement isa/cast/dyncast.
00680   static bool classof(const BlockDesc *) { return true; }
00681   static bool classof(const DebugInfoDesc *D);
00682   
00683   /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
00684   ///
00685   virtual void ApplyToFields(DIVisitor *Visitor);
00686 
00687   /// getDescString - Return a string used to compose global names and labels.
00688   ///
00689   virtual const char *getDescString() const;
00690 
00691   /// getTypeString - Return a string used to label this descriptor's type.
00692   ///
00693   virtual const char *getTypeString() const;
00694     
00695 #ifndef NDEBUG
00696   virtual void dump();
00697 #endif
00698 };
00699 
00700 //===----------------------------------------------------------------------===//
00701 /// DIDeserializer - This class is responsible for casting GlobalVariables
00702 /// into DebugInfoDesc objects.
00703 class DIDeserializer {
00704 private:
00705   unsigned DebugVersion;                // Version of debug information in use.
00706   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
00707                                         // Previously defined gloabls.
00708   
00709 public:
00710   DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
00711   ~DIDeserializer() {}
00712   
00713   // Accessors
00714   unsigned getDebugVersion() const { return DebugVersion; }
00715   
00716   /// Deserialize - Reconstitute a GlobalVariable into it's component
00717   /// DebugInfoDesc objects.
00718   DebugInfoDesc *Deserialize(Value *V);
00719   DebugInfoDesc *Deserialize(GlobalVariable *GV);
00720 };
00721 
00722 //===----------------------------------------------------------------------===//
00723 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
00724 /// into GlobalVariables.
00725 class DISerializer {
00726 private:
00727   Module *M;                            // Definition space module.
00728   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
00729   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
00730   std::map<unsigned, StructType *> TagTypes;
00731                                         // Types per Tag.  Created lazily.
00732   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
00733                                         // Previously defined descriptors.
00734   std::map<const std::string, Constant *> StringCache;
00735                                         // Previously defined strings.
00736                                           
00737 public:
00738   DISerializer()
00739   : M(NULL)
00740   , StrPtrTy(NULL)
00741   , EmptyStructPtrTy(NULL)
00742   , TagTypes()
00743   , DescGlobals()
00744   , StringCache()
00745   {}
00746   ~DISerializer() {}
00747   
00748   // Accessors
00749   Module *getModule()        const { return M; };
00750   void setModule(Module *module)  { M = module; }
00751 
00752   /// getStrPtrType - Return a "sbyte *" type.
00753   ///
00754   const PointerType *getStrPtrType();
00755   
00756   /// getEmptyStructPtrType - Return a "{ }*" type.
00757   ///
00758   const PointerType *getEmptyStructPtrType();
00759   
00760   /// getTagType - Return the type describing the specified descriptor (via
00761   /// tag.)
00762   const StructType *getTagType(DebugInfoDesc *DD);
00763   
00764   /// getString - Construct the string as constant string global.
00765   ///
00766   Constant *getString(const std::string &String);
00767   
00768   /// Serialize - Recursively cast the specified descriptor into a
00769   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
00770   GlobalVariable *Serialize(DebugInfoDesc *DD);
00771 };
00772 
00773 //===----------------------------------------------------------------------===//
00774 /// DIVerifier - This class is responsible for verifying the given network of
00775 /// GlobalVariables are valid as DebugInfoDesc objects.
00776 class DIVerifier {
00777 private:
00778   enum {
00779     Unknown = 0,
00780     Invalid,
00781     Valid
00782   };
00783   unsigned DebugVersion;                // Version of debug information in use.
00784   std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
00785   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
00786   
00787 public:
00788   DIVerifier()
00789   : DebugVersion(LLVMDebugVersion)
00790   , Validity()
00791   , Counts()
00792   {}
00793   ~DIVerifier() {}
00794   
00795   /// Verify - Return true if the GlobalVariable appears to be a valid
00796   /// serialization of a DebugInfoDesc.
00797   bool Verify(Value *V);
00798   bool Verify(GlobalVariable *GV);
00799 };
00800 
00801 //===----------------------------------------------------------------------===//
00802 /// SourceLineInfo - This class is used to record source line correspondence.
00803 ///
00804 class SourceLineInfo {
00805 private:
00806   unsigned Line;                        // Source line number.
00807   unsigned Column;                      // Source column.
00808   unsigned SourceID;                    // Source ID number.
00809   unsigned LabelID;                     // Label in code ID number.
00810 
00811 public:
00812   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
00813   : Line(L), Column(C), SourceID(S), LabelID(I) {}
00814   
00815   // Accessors
00816   unsigned getLine()     const { return Line; }
00817   unsigned getColumn()   const { return Column; }
00818   unsigned getSourceID() const { return SourceID; }
00819   unsigned getLabelID()  const { return LabelID; }
00820 };
00821 
00822 //===----------------------------------------------------------------------===//
00823 /// SourceFileInfo - This class is used to track source information.
00824 ///
00825 class SourceFileInfo {
00826 private:
00827   unsigned DirectoryID;                 // Directory ID number.
00828   std::string Name;                     // File name (not including directory.)
00829   
00830 public:
00831   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
00832             
00833   // Accessors
00834   unsigned getDirectoryID()    const { return DirectoryID; }
00835   const std::string &getName() const { return Name; }
00836 
00837   /// operator== - Used by UniqueVector to locate entry.
00838   ///
00839   bool operator==(const SourceFileInfo &SI) const {
00840     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
00841   }
00842 
00843   /// operator< - Used by UniqueVector to locate entry.
00844   ///
00845   bool operator<(const SourceFileInfo &SI) const {
00846     return getDirectoryID() < SI.getDirectoryID() ||
00847           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
00848   }
00849 };
00850 
00851 //===----------------------------------------------------------------------===//
00852 /// DebugVariable - This class is used to track local variable information.
00853 ///
00854 class DebugVariable {
00855 private:
00856   VariableDesc *Desc;                   // Variable Descriptor.
00857   unsigned FrameIndex;                  // Variable frame index.
00858 
00859 public:
00860   DebugVariable(VariableDesc *D, unsigned I)
00861   : Desc(D)
00862   , FrameIndex(I)
00863   {}
00864   
00865   // Accessors.
00866   VariableDesc *getDesc()  const { return Desc; }
00867   unsigned getFrameIndex() const { return FrameIndex; }
00868 };
00869 
00870 //===----------------------------------------------------------------------===//
00871 /// DebugScope - This class is used to track scope information.
00872 ///
00873 class DebugScope {
00874 private:
00875   DebugScope *Parent;                   // Parent to this scope.
00876   DebugInfoDesc *Desc;                  // Debug info descriptor for scope.
00877                                         // Either subprogram or block.
00878   unsigned StartLabelID;                // Label ID of the beginning of scope.
00879   unsigned EndLabelID;                  // Label ID of the end of scope.
00880   std::vector<DebugScope *> Scopes;     // Scopes defined in scope.
00881   std::vector<DebugVariable *> Variables;// Variables declared in scope.
00882   
00883 public:
00884   DebugScope(DebugScope *P, DebugInfoDesc *D)
00885   : Parent(P)
00886   , Desc(D)
00887   , StartLabelID(0)
00888   , EndLabelID(0)
00889   , Scopes()
00890   , Variables()
00891   {}
00892   ~DebugScope();
00893   
00894   // Accessors.
00895   DebugScope *getParent()        const { return Parent; }
00896   DebugInfoDesc *getDesc()       const { return Desc; }
00897   unsigned getStartLabelID()     const { return StartLabelID; }
00898   unsigned getEndLabelID()       const { return EndLabelID; }
00899   std::vector<DebugScope *> &getScopes() { return Scopes; }
00900   std::vector<DebugVariable *> &getVariables() { return Variables; }
00901   void setStartLabelID(unsigned S) { StartLabelID = S; }
00902   void setEndLabelID(unsigned E)   { EndLabelID = E; }
00903   
00904   /// AddScope - Add a scope to the scope.
00905   ///
00906   void AddScope(DebugScope *S) { Scopes.push_back(S); }
00907   
00908   /// AddVariable - Add a variable to the scope.
00909   ///
00910   void AddVariable(DebugVariable *V) { Variables.push_back(V); }
00911 };
00912 
00913 //===----------------------------------------------------------------------===//
00914 /// MachineDebugInfo - This class contains debug information specific to a
00915 /// module.  Queries can be made by different debugging schemes and reformated
00916 /// for specific use.
00917 ///
00918 class MachineDebugInfo : public ImmutablePass {
00919 private:
00920   // Use the same deserializer/verifier for the module.
00921   DIDeserializer DR;
00922   DIVerifier VR;
00923 
00924   // CompileUnits - Uniquing vector for compile units.
00925   UniqueVector<CompileUnitDesc *> CompileUnits;
00926   
00927   // Directories - Uniquing vector for directories.
00928   UniqueVector<std::string> Directories;
00929                                          
00930   // SourceFiles - Uniquing vector for source files.
00931   UniqueVector<SourceFileInfo> SourceFiles;
00932 
00933   // Lines - List of of source line correspondence.
00934   std::vector<SourceLineInfo *> Lines;
00935   
00936   // LabelID - Current number assigned to unique label numbers.
00937   unsigned LabelID;
00938   
00939   // ScopeMap - Tracks the scopes in the current function.
00940   std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
00941   
00942   // RootScope - Top level scope for the current function.
00943   //
00944   DebugScope *RootScope;
00945   
00946   // FrameMoves - List of moves done by a function's prolog.  Used to construct
00947   // frame maps by debug consumers.
00948   std::vector<MachineMove *> FrameMoves;
00949 
00950 public:
00951   MachineDebugInfo();
00952   ~MachineDebugInfo();
00953   
00954   /// doInitialization - Initialize the debug state for a new module.
00955   ///
00956   bool doInitialization();
00957   
00958   /// doFinalization - Tear down the debug state after completion of a module.
00959   ///
00960   bool doFinalization();
00961   
00962   /// BeginFunction - Begin gathering function debug information.
00963   ///
00964   void BeginFunction(MachineFunction *MF);
00965   
00966   /// EndFunction - Discard function debug information.
00967   ///
00968   void EndFunction();
00969 
00970   /// getDescFor - Convert a Value to a debug information descriptor.
00971   ///
00972   // FIXME - use new Value type when available.
00973   DebugInfoDesc *getDescFor(Value *V);
00974   
00975   /// Verify - Verify that a Value is debug information descriptor.
00976   ///
00977   bool Verify(Value *V);
00978   
00979   /// AnalyzeModule - Scan the module for global debug information.
00980   ///
00981   void AnalyzeModule(Module &M);
00982   
00983   /// hasInfo - Returns true if valid debug info is present.
00984   ///
00985   bool hasInfo() const { return !CompileUnits.empty(); }
00986   
00987   /// NextLabelID - Return the next unique label id.
00988   ///
00989   unsigned NextLabelID() { return ++LabelID; }
00990   
00991   /// RecordLabel - Records location information and associates it with a
00992   /// debug label.  Returns a unique label ID used to generate a label and 
00993   /// provide correspondence to the source line list.
00994   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source);
00995   
00996   /// RecordSource - Register a source file with debug info. Returns an source
00997   /// ID.
00998   unsigned RecordSource(const std::string &Directory,
00999                         const std::string &Source);
01000   unsigned RecordSource(const CompileUnitDesc *CompileUnit);
01001   
01002   /// getDirectories - Return the UniqueVector of std::string representing
01003   /// directories.
01004   const UniqueVector<std::string> &getDirectories() const {
01005     return Directories;
01006   }
01007   
01008   /// getSourceFiles - Return the UniqueVector of source files. 
01009   ///
01010   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
01011     return SourceFiles;
01012   }
01013   
01014   /// getSourceLines - Return a vector of source lines.  Vector index + 1
01015   /// equals label ID.
01016   const std::vector<SourceLineInfo *> &getSourceLines() const {
01017     return Lines;
01018   }
01019   
01020   /// SetupCompileUnits - Set up the unique vector of compile units.
01021   ///
01022   void SetupCompileUnits(Module &M);
01023 
01024   /// getCompileUnits - Return a vector of debug compile units.
01025   ///
01026   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
01027   
01028   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
01029   /// named GlobalVariable.
01030   std::vector<GlobalVariable*>
01031   getGlobalVariablesUsing(Module &M, const std::string &RootName);
01032 
01033   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
01034   ///
01035   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
01036     T Desc;
01037     std::vector<GlobalVariable *> Globals =
01038                              getGlobalVariablesUsing(M, Desc.getAnchorString());
01039     std::vector<T *> AnchoredDescs;
01040     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
01041       GlobalVariable *GV = Globals[i];
01042       unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
01043       
01044       if (isa<CompileUnitDesc>(&Desc)) {
01045         unsigned DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
01046         // FIXME - In the short term, changes are too drastic to continue.
01047         if (DebugVersion != LLVMDebugVersion) break;
01048       }
01049       
01050       if (Tag == Desc.getTag()) {
01051         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
01052       }
01053     }
01054 
01055     return AnchoredDescs;
01056   }
01057   
01058   /// RecordRegionStart - Indicate the start of a region.
01059   ///
01060   unsigned RecordRegionStart(Value *V);
01061 
01062   /// RecordRegionEnd - Indicate the end of a region.
01063   ///
01064   unsigned RecordRegionEnd(Value *V);
01065 
01066   /// RecordVariable - Indicate the declaration of  a local variable.
01067   ///
01068   void RecordVariable(Value *V, unsigned FrameIndex);
01069   
01070   /// getRootScope - Return current functions root scope.
01071   ///
01072   DebugScope *getRootScope() { return RootScope; }
01073   
01074   /// getOrCreateScope - Returns the scope associated with the given descriptor.
01075   ///
01076   DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
01077   
01078   /// getFrameMoves - Returns a reference to a list of moves done in the current
01079   /// function's prologue.  Used to construct frame maps for debug comsumers.
01080   std::vector<MachineMove *> &getFrameMoves() { return FrameMoves; }
01081 
01082 }; // End class MachineDebugInfo
01083 
01084 } // End llvm namespace
01085 
01086 #endif