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