LLVM API Documentation

DebugInfo.h

Go to the documentation of this file.
00001 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a bunch of datatypes that are useful for creating and
00011 // walking debug info in LLVM IR form.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_ANALYSIS_DEBUGINFO_H
00016 #define LLVM_ANALYSIS_DEBUGINFO_H
00017 
00018 #include "llvm/ADT/StringMap.h"
00019 #include "llvm/ADT/DenseMap.h"
00020 #include "llvm/Support/Dwarf.h"
00021 
00022 namespace llvm {
00023   class BasicBlock;
00024   class Constant;
00025   class Function;
00026   class GlobalVariable;
00027   class Module;
00028   class Type;
00029   class Value;
00030   class DbgStopPointInst;
00031   class DbgDeclareInst;
00032   class Instruction;
00033 
00034   class DIDescriptor {
00035   protected:    
00036     GlobalVariable *GV;
00037 
00038     /// DIDescriptor constructor.  If the specified GV is non-null, this checks
00039     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
00040     /// not, the debug info is corrupt and we ignore it.
00041     DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
00042 
00043     std::string getStringField(unsigned Elt) const;
00044     unsigned getUnsignedField(unsigned Elt) const {
00045       return (unsigned)getUInt64Field(Elt);
00046     }
00047     uint64_t getUInt64Field(unsigned Elt) const;
00048     DIDescriptor getDescriptorField(unsigned Elt) const;
00049 
00050     template <typename DescTy>
00051     DescTy getFieldAs(unsigned Elt) const {
00052       return DescTy(getDescriptorField(Elt).getGV());
00053     }
00054 
00055     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
00056 
00057   public:
00058     explicit DIDescriptor() : GV(0) {}
00059     explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
00060 
00061     bool isNull() const { return GV == 0; }
00062 
00063     GlobalVariable *getGV() const { return GV; }
00064 
00065     unsigned getVersion() const {
00066       return getUnsignedField(0) & LLVMDebugVersionMask;
00067     }
00068 
00069     unsigned getTag() const {
00070       return getUnsignedField(0) & ~LLVMDebugVersionMask;
00071     }
00072 
00073   };
00074 
00075   /// DIAnchor - A wrapper for various anchor descriptors.
00076   class DIAnchor : public DIDescriptor {
00077   public:
00078     explicit DIAnchor(GlobalVariable *GV = 0);
00079 
00080     unsigned getAnchorTag() const { return getUnsignedField(1); }
00081   };
00082 
00083   /// DISubrange - This is used to represent ranges, for array bounds.
00084   class DISubrange : public DIDescriptor {
00085   public:
00086     explicit DISubrange(GlobalVariable *GV = 0);
00087 
00088     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
00089     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
00090   };
00091 
00092   /// DIArray - This descriptor holds an array of descriptors.
00093   class DIArray : public DIDescriptor {
00094   public:
00095     explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
00096 
00097     unsigned getNumElements() const;
00098     DIDescriptor getElement(unsigned Idx) const {
00099       return getDescriptorField(Idx);
00100     }
00101   };
00102 
00103   /// DICompileUnit - A wrapper for a compile unit.
00104   class DICompileUnit : public DIDescriptor {
00105   public:
00106     explicit DICompileUnit(GlobalVariable *GV = 0);
00107 
00108     unsigned getLanguage() const     { return getUnsignedField(2); }
00109     std::string getFilename() const  { return getStringField(3); }
00110     std::string getDirectory() const { return getStringField(4); }
00111     std::string getProducer() const  { return getStringField(5); }
00112     
00113     /// isMain - Each input file is encoded as a separate compile unit in LLVM
00114     /// debugging information output. However, many target specific tool chains
00115     /// prefer to encode only one compile unit in an object file. In this 
00116     /// situation, the LLVM code generator will include  debugging information
00117     /// entities in the compile unit that is marked as main compile unit. The 
00118     /// code generator accepts maximum one main compile unit per module. If a
00119     /// module does not contain any main compile unit then the code generator 
00120     /// will emit multiple compile units in the output object file.
00121     bool isMain() const             { return getUnsignedField(6); }
00122     bool isOptimized() const         { return getUnsignedField(7); }
00123     std::string getFlags() const     { return getStringField(8); }
00124 
00125     /// Verify - Verify that a compile unit is well formed.
00126     bool Verify() const;
00127 
00128     /// dump - print compile unit.
00129     void dump() const;
00130   };
00131 
00132   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
00133   /// FIXME: it seems strange that this doesn't have either a reference to the
00134   /// type/precision or a file/line pair for location info.
00135   class DIEnumerator : public DIDescriptor {
00136   public:
00137     explicit DIEnumerator(GlobalVariable *GV = 0);
00138 
00139     std::string getName() const  { return getStringField(1); }
00140     uint64_t getEnumValue() const { return getUInt64Field(2); }
00141   };
00142 
00143   /// DIType - This is a wrapper for a type.
00144   /// FIXME: Types should be factored much better so that CV qualifiers and
00145   /// others do not require a huge and empty descriptor full of zeros.
00146   class DIType : public DIDescriptor {
00147   public:
00148     enum {
00149       FlagPrivate   = 1 << 0,
00150       FlagProtected = 1 << 1,
00151       FlagFwdDecl   = 1 << 2
00152     };
00153 
00154   protected:
00155     DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
00156     // This ctor is used when the Tag has already been validated by a derived
00157     // ctor.
00158     DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
00159 
00160   public:
00161     /// isDerivedType - Return true if the specified tag is legal for
00162     /// DIDerivedType.
00163     static bool isDerivedType(unsigned TAG);
00164 
00165     /// isCompositeType - Return true if the specified tag is legal for
00166     /// DICompositeType.
00167     static bool isCompositeType(unsigned TAG);
00168 
00169     /// isBasicType - Return true if the specified tag is legal for
00170     /// DIBasicType.
00171     static bool isBasicType(unsigned TAG) {
00172       return TAG == dwarf::DW_TAG_base_type;
00173     }
00174 
00175     /// Verify - Verify that a type descriptor is well formed.
00176     bool Verify() const;
00177   public:
00178     explicit DIType(GlobalVariable *GV);
00179     explicit DIType() {}
00180     virtual ~DIType() {}
00181 
00182     DIDescriptor getContext() const     { return getDescriptorField(1); }
00183     std::string getName() const         { return getStringField(2); }
00184     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
00185     unsigned getLineNumber() const      { return getUnsignedField(4); }
00186     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
00187     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
00188     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
00189     // carry this is just plain insane.
00190     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
00191     unsigned getFlags() const           { return getUnsignedField(8); }
00192     bool isPrivate() const              { return (getFlags() & FlagPrivate) != 0; }
00193     bool isProtected() const            { return (getFlags() & FlagProtected) != 0; }
00194     bool isForwardDecl() const          { return (getFlags() & FlagFwdDecl) != 0; }
00195 
00196     /// dump - print type.
00197     void dump() const;
00198   };
00199 
00200   /// DIBasicType - A basic type, like 'int' or 'float'.
00201   class DIBasicType : public DIType {
00202   public:
00203     explicit DIBasicType(GlobalVariable *GV);
00204     unsigned getEncoding() const { return getUnsignedField(9); }
00205 
00206     /// dump - print basic type.
00207     void dump() const;
00208   };
00209 
00210   /// DIDerivedType - A simple derived type, like a const qualified type,
00211   /// a typedef, a pointer or reference, etc.
00212   class DIDerivedType : public DIType {
00213   protected:
00214     explicit DIDerivedType(GlobalVariable *GV, bool, bool)
00215       : DIType(GV, true, true) {}
00216   public:
00217     explicit DIDerivedType(GlobalVariable *GV);
00218     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
00219 
00220     /// dump - print derived type.
00221     void dump() const;
00222   };
00223 
00224   /// DICompositeType - This descriptor holds a type that can refer to multiple
00225   /// other types, like a function or struct.
00226   /// FIXME: Why is this a DIDerivedType??
00227   class DICompositeType : public DIDerivedType {
00228   public:
00229     explicit DICompositeType(GlobalVariable *GV);
00230     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
00231 
00232     /// Verify - Verify that a composite type descriptor is well formed.
00233     bool Verify() const;
00234 
00235     /// dump - print composite type.
00236     void dump() const;
00237   };
00238 
00239   /// DIGlobal - This is a common class for global variables and subprograms.
00240   class DIGlobal : public DIDescriptor {
00241   protected:
00242     explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
00243       : DIDescriptor(GV, RequiredTag) {}
00244 
00245     /// isSubprogram - Return true if the specified tag is legal for
00246     /// DISubprogram.
00247     static bool isSubprogram(unsigned TAG) {
00248       return TAG == dwarf::DW_TAG_subprogram;
00249     }
00250 
00251     /// isGlobalVariable - Return true if the specified tag is legal for
00252     /// DIGlobalVariable.
00253     static bool isGlobalVariable(unsigned TAG) {
00254       return TAG == dwarf::DW_TAG_variable;
00255     }
00256 
00257   public:
00258     virtual ~DIGlobal() {}
00259 
00260     DIDescriptor getContext() const     { return getDescriptorField(2); }
00261     std::string getName() const         { return getStringField(3); }
00262     std::string getDisplayName() const  { return getStringField(4); }
00263     std::string getLinkageName() const  { return getStringField(5); }
00264     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
00265     unsigned getLineNumber() const      { return getUnsignedField(7); }
00266     DIType getType() const              { return getFieldAs<DIType>(8); }
00267 
00268     /// isLocalToUnit - Return true if this subprogram is local to the current
00269     /// compile unit, like 'static' in C.
00270     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
00271     unsigned isDefinition() const       { return getUnsignedField(10); }
00272 
00273     /// dump - print global.
00274     void dump() const;
00275   };
00276 
00277   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
00278   class DISubprogram : public DIGlobal {
00279   public:
00280     explicit DISubprogram(GlobalVariable *GV = 0);
00281     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
00282 
00283     /// Verify - Verify that a subprogram descriptor is well formed.
00284     bool Verify() const;
00285 
00286     /// dump - print subprogram.
00287     void dump() const;
00288   };
00289 
00290   /// DIGlobalVariable - This is a wrapper for a global variable.
00291   class DIGlobalVariable : public DIGlobal {
00292   public:
00293     explicit DIGlobalVariable(GlobalVariable *GV = 0);
00294     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
00295 
00296     /// Verify - Verify that a global variable descriptor is well formed.
00297     bool Verify() const;
00298 
00299     /// dump - print global variable.
00300     void dump() const;
00301   };
00302 
00303   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
00304   /// global etc).
00305   class DIVariable : public DIDescriptor {
00306   public:
00307     explicit DIVariable(GlobalVariable *GV = 0);
00308 
00309     DIDescriptor getContext() const { return getDescriptorField(1); }
00310     std::string getName() const { return getStringField(2); }
00311     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
00312     unsigned getLineNumber() const      { return getUnsignedField(4); }
00313     DIType getType() const              { return getFieldAs<DIType>(5); }
00314 
00315     /// isVariable - Return true if the specified tag is legal for DIVariable.
00316     static bool isVariable(unsigned Tag);
00317 
00318     /// Verify - Verify that a variable descriptor is well formed.
00319     bool Verify() const;
00320 
00321     /// dump - print variable.
00322     void dump() const;
00323   };
00324 
00325   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
00326   class DIBlock : public DIDescriptor {
00327   public:
00328     explicit DIBlock(GlobalVariable *GV = 0);
00329     
00330     DIDescriptor getContext() const { return getDescriptorField(1); }
00331   };
00332 
00333   /// DIFactory - This object assists with the construction of the various
00334   /// descriptors.
00335   class DIFactory {
00336     Module &M;
00337     // Cached values for uniquing and faster lookups.
00338     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
00339     const Type *EmptyStructPtr; // "{}*".
00340     Function *StopPointFn;   // llvm.dbg.stoppoint
00341     Function *FuncStartFn;   // llvm.dbg.func.start
00342     Function *RegionStartFn; // llvm.dbg.region.start
00343     Function *RegionEndFn;   // llvm.dbg.region.end
00344     Function *DeclareFn;     // llvm.dbg.declare
00345     StringMap<Constant*> StringCache;
00346     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
00347 
00348     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
00349     void operator=(const DIFactory&); // DO NOT IMPLEMENT
00350   public:
00351     explicit DIFactory(Module &m);
00352 
00353     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
00354     /// creating a new one if there isn't already one in the module.
00355     DIAnchor GetOrCreateCompileUnitAnchor();
00356 
00357     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
00358     /// creating a new one if there isn't already one in the module.
00359     DIAnchor GetOrCreateSubprogramAnchor();
00360 
00361     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
00362     /// creating a new one if there isn't already one in the module.
00363     DIAnchor GetOrCreateGlobalVariableAnchor();
00364 
00365     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
00366     /// This implicitly uniques the arrays created.
00367     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
00368 
00369     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
00370     /// implicitly uniques the values returned.
00371     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
00372 
00373     /// CreateCompileUnit - Create a new descriptor for the specified compile
00374     /// unit.
00375     DICompileUnit CreateCompileUnit(unsigned LangID,
00376                                     const std::string &Filename,
00377                                     const std::string &Directory,
00378                                     const std::string &Producer,
00379                                     bool isMain = false,
00380                                     bool isOptimized = false,
00381                                     const char *Flags = "");
00382 
00383     /// CreateEnumerator - Create a single enumerator value.
00384     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
00385 
00386     /// CreateBasicType - Create a basic type like int, float, etc.
00387     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
00388                                 DICompileUnit CompileUnit, unsigned LineNumber,
00389                                 uint64_t SizeInBits, uint64_t AlignInBits,
00390                                 uint64_t OffsetInBits, unsigned Flags,
00391                                 unsigned Encoding);
00392 
00393     /// CreateDerivedType - Create a derived type like const qualified type,
00394     /// pointer, typedef, etc.
00395     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
00396                                     const std::string &Name,
00397                                     DICompileUnit CompileUnit,
00398                                     unsigned LineNumber,
00399                                     uint64_t SizeInBits, uint64_t AlignInBits,
00400                                     uint64_t OffsetInBits, unsigned Flags,
00401                                     DIType DerivedFrom);
00402 
00403     /// CreateCompositeType - Create a composite type like array, struct, etc.
00404     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
00405                                         const std::string &Name,
00406                                         DICompileUnit CompileUnit,
00407                                         unsigned LineNumber,
00408                                         uint64_t SizeInBits,
00409                                         uint64_t AlignInBits,
00410                                         uint64_t OffsetInBits, unsigned Flags,
00411                                         DIType DerivedFrom,
00412                                         DIArray Elements);
00413 
00414     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
00415     /// See comments in DISubprogram for descriptions of these fields.
00416     DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
00417                                   const std::string &DisplayName,
00418                                   const std::string &LinkageName,
00419                                   DICompileUnit CompileUnit, unsigned LineNo,
00420                                   DIType Type, bool isLocalToUnit,
00421                                   bool isDefinition);
00422 
00423     /// CreateGlobalVariable - Create a new descriptor for the specified global.
00424     DIGlobalVariable
00425     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
00426                          const std::string &DisplayName,
00427                          const std::string &LinkageName, 
00428                          DICompileUnit CompileUnit,
00429                          unsigned LineNo, DIType Type, bool isLocalToUnit,
00430                          bool isDefinition, llvm::GlobalVariable *GV);
00431 
00432     /// CreateVariable - Create a new descriptor for the specified variable.
00433     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
00434                               const std::string &Name,
00435                               DICompileUnit CompileUnit, unsigned LineNo,
00436                               DIType Type);
00437 
00438     /// CreateBlock - This creates a descriptor for a lexical block with the
00439     /// specified parent context.
00440     DIBlock CreateBlock(DIDescriptor Context);
00441 
00442     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
00443     /// inserting it at the end of the specified basic block.
00444     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
00445                          BasicBlock *BB);
00446 
00447     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
00448     /// mark the start of the specified subprogram.
00449     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
00450 
00451     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
00452     /// mark the start of a region for the specified scoping descriptor.
00453     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
00454 
00455     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
00456     /// mark the end of a region for the specified scoping descriptor.
00457     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
00458 
00459     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
00460     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
00461 
00462   private:
00463     Constant *GetTagConstant(unsigned TAG);
00464     Constant *GetStringConstant(const std::string &String);
00465     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
00466 
00467     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
00468     Constant *getCastToEmpty(DIDescriptor D);
00469   };
00470 
00471   /// Finds the stoppoint coressponding to this instruction, that is the
00472   /// stoppoint that dominates this instruction 
00473   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
00474 
00475   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
00476   /// instruction in this Basic Block, and returns the stoppoint for it.
00477   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
00478 
00479   /// Finds the dbg.declare intrinsic corresponding to this value if any.
00480   /// It looks through pointer casts too.
00481   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
00482 } // end namespace llvm
00483 
00484 #endif