1eaa69a04cf85cd76bd209bd6d61205a69529cc9
[oota-llvm.git] / include / llvm / Analysis / DebugInfo.h
1 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_DEBUGINFO_H
16 #define LLVM_SUPPORT_DEBUGINFO_H
17
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
21
22 namespace llvm {
23   class BasicBlock;
24   class Constant;
25   class Function;
26   class GlobalVariable;
27   class Module;
28   class Type;
29   class Value;
30   class DbgStopPointInst;
31   class DbgDeclareInst;
32   class Instruction;
33   
34   class DIDescriptor {
35   public:
36     enum {
37       Version7    = 7 << 16,     // Current version of debug information.
38       Version6    = 6 << 16,     // Constant for version 6.
39       Version5    = 5 << 16,     // Constant for version 5.
40       Version4    = 4 << 16,     // Constant for version 4.
41       VersionMask = 0xffff0000   // Mask for version number.
42     };
43     
44   protected:    
45     GlobalVariable *GV;
46     
47     /// DIDescriptor constructor.  If the specified GV is non-null, this checks
48     /// to make sure that the tag in the descriptor matches 'RequiredTag'.  If
49     /// not, the debug info is corrupt and we ignore it.
50     DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
51     
52     std::string getStringField(unsigned Elt) const;
53     unsigned getUnsignedField(unsigned Elt) const {
54       return (unsigned)getUInt64Field(Elt);
55     }
56     uint64_t getUInt64Field(unsigned Elt) const;
57     DIDescriptor getDescriptorField(unsigned Elt) const;
58     
59     template <typename DescTy>
60     DescTy getFieldAs(unsigned Elt) const {
61       return DescTy(getDescriptorField(Elt).getGV());
62     }
63   
64     GlobalVariable *getGlobalVariableField(unsigned Elt) const;
65     
66   public:
67     explicit DIDescriptor() : GV(0) {}
68     explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
69
70     bool isNull() const { return GV == 0; }
71
72     GlobalVariable *getGV() const { return GV; }
73
74     unsigned getVersion() const {
75       return getUnsignedField(0) & VersionMask;
76     }
77     
78     unsigned getTag() const {
79       return getUnsignedField(0) & ~VersionMask;
80     }
81     
82   };
83   
84   /// DIAnchor - A wrapper for various anchor descriptors.
85   class DIAnchor : public DIDescriptor {
86   public:
87     explicit DIAnchor(GlobalVariable *GV = 0);
88     
89     unsigned getAnchorTag() const { return getUnsignedField(1); }
90   };
91
92   /// DISubrange - This is used to represent ranges, for array bounds.
93   class DISubrange : public DIDescriptor {
94   public:
95     explicit DISubrange(GlobalVariable *GV = 0);
96     
97     int64_t getLo() const { return (int64_t)getUInt64Field(1); }
98     int64_t getHi() const { return (int64_t)getUInt64Field(2); }
99   };
100   
101   /// DIArray - This descriptor holds an array of descriptors.
102   class DIArray : public DIDescriptor {
103   public:
104     explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
105     
106     unsigned getNumElements() const;
107     DIDescriptor getElement(unsigned Idx) const {
108       return getDescriptorField(Idx);
109     }
110   };
111   
112   /// DICompileUnit - A wrapper for a compile unit.
113   class DICompileUnit : public DIDescriptor {
114   public:
115     explicit DICompileUnit(GlobalVariable *GV = 0);
116     
117     unsigned getLanguage() const     { return getUnsignedField(2); }
118     std::string getFilename() const  { return getStringField(3); }
119     std::string getDirectory() const { return getStringField(4); }
120     std::string getProducer() const  { return getStringField(5); }
121     /// Verify - Verify that a compile unit is well formed.
122     bool Verify() const;
123   };
124
125   /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
126   /// FIXME: it seems strange that this doesn't have either a reference to the
127   /// type/precision or a file/line pair for location info.
128   class DIEnumerator : public DIDescriptor {
129   public:
130     explicit DIEnumerator(GlobalVariable *GV = 0);
131     
132     std::string getName() const  { return getStringField(1); }
133     uint64_t getEnumValue() const { return getUInt64Field(2); }
134   };
135   
136   /// DIType - This is a wrapper for a type.
137   /// FIXME: Types should be factored much better so that CV qualifiers and
138   /// others do not require a huge and empty descriptor full of zeros.
139   class DIType : public DIDescriptor {
140   protected:
141     DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
142     // This ctor is used when the Tag has already been validated by a derived
143     // ctor.
144     DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
145
146   public:
147     /// isDerivedType - Return true if the specified tag is legal for
148     /// DIDerivedType.
149     static bool isDerivedType(unsigned TAG);
150
151     /// isCompositeType - Return true if the specified tag is legal for
152     /// DICompositeType.
153     static bool isCompositeType(unsigned TAG);
154
155     /// isBasicType - Return true if the specified tag is legal for
156     /// DIBasicType.
157     static bool isBasicType(unsigned TAG) {
158       return TAG == dwarf::DW_TAG_base_type;
159     }
160
161     /// Verify - Verify that a type descriptor is well formed.
162     bool Verify() const;
163   public:
164     explicit DIType(GlobalVariable *GV);
165     explicit DIType() {}
166     virtual ~DIType() {}
167
168     DIDescriptor getContext() const     { return getDescriptorField(1); }
169     std::string getName() const         { return getStringField(2); }
170     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
171     unsigned getLineNumber() const      { return getUnsignedField(4); }
172     uint64_t getSizeInBits() const      { return getUInt64Field(5); }
173     uint64_t getAlignInBits() const     { return getUInt64Field(6); }
174     // FIXME: Offset is only used for DW_TAG_member nodes.  Making every type
175     // carry this is just plain insane.
176     uint64_t getOffsetInBits() const    { return getUInt64Field(7); }
177     unsigned getFlags() const           { return getUnsignedField(8); }
178
179     virtual std::string getFilename() const { 
180       assert (0 && "Invalid DIDescriptor");
181       return "";
182     }
183
184     virtual std::string getDirectory() const { 
185       assert (0 && "Invalid DIDescriptor");
186       return "";
187     }
188   };
189   
190   /// DIBasicType - A basic type, like 'int' or 'float'.
191   class DIBasicType : public DIType {
192   public:
193     explicit DIBasicType(GlobalVariable *GV);
194     
195     unsigned getEncoding() const { return getUnsignedField(9); }
196     std::string getFilename() const { return getStringField(10); }
197     std::string getDirectory() const { return getStringField(11); }
198   };
199   
200   /// DIDerivedType - A simple derived type, like a const qualified type,
201   /// a typedef, a pointer or reference, etc.
202   class DIDerivedType : public DIType {
203   protected:
204     explicit DIDerivedType(GlobalVariable *GV, bool, bool)
205       : DIType(GV, true, true) {}
206   public:
207     explicit DIDerivedType(GlobalVariable *GV);
208     
209     DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
210     std::string getFilename() const { return getStringField(10); }
211     std::string getDirectory() const { return getStringField(11); }
212   };
213
214   
215   /// DICompositeType - This descriptor holds a type that can refer to multiple
216   /// other types, like a function or struct.
217   /// FIXME: Why is this a DIDerivedType??
218   class DICompositeType : public DIDerivedType {
219   public:
220     explicit DICompositeType(GlobalVariable *GV);
221     
222     DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
223     std::string getFilename() const { return getStringField(11); }
224     std::string getDirectory() const { return getStringField(12); }
225
226     /// Verify - Verify that a composite type descriptor is well formed.
227     bool Verify() const;
228   };
229   
230   /// DIGlobal - This is a common class for global variables and subprograms.
231   class DIGlobal : public DIDescriptor {
232   protected:
233     explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
234       : DIDescriptor(GV, RequiredTag) {}
235
236     /// isSubprogram - Return true if the specified tag is legal for
237     /// DISubprogram.
238     static bool isSubprogram(unsigned TAG) {
239       return TAG == dwarf::DW_TAG_subprogram;
240     }
241
242     /// isGlobalVariable - Return true if the specified tag is legal for
243     /// DIGlobalVariable.
244     static bool isGlobalVariable(unsigned TAG) {
245       return TAG == dwarf::DW_TAG_variable;
246     }
247
248   public:
249     virtual ~DIGlobal() {}
250
251     DIDescriptor getContext() const     { return getDescriptorField(2); }
252     std::string getName() const         { return getStringField(3); }
253     std::string getDisplayName() const  { return getStringField(4); }
254     std::string getLinkageName() const  { return getStringField(5); }
255     
256     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
257     unsigned getLineNumber() const      { return getUnsignedField(7); }
258     DIType getType() const              { return getFieldAs<DIType>(8); }
259     
260     /// isLocalToUnit - Return true if this subprogram is local to the current
261     /// compile unit, like 'static' in C.
262     unsigned isLocalToUnit() const      { return getUnsignedField(9); }
263     unsigned isDefinition() const       { return getUnsignedField(10); }
264
265     virtual std::string getFilename() const { 
266       assert (0 && "Invalid DIDescriptor");
267       return "";
268     }
269
270     virtual std::string getDirectory() const { 
271       assert (0 && "Invalid DIDescriptor");
272       return "";
273     }
274   };
275   
276   
277   /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
278   class DISubprogram : public DIGlobal {
279   public:
280     explicit DISubprogram(GlobalVariable *GV = 0);
281     std::string getFilename() const { return getStringField(11); }
282     std::string getDirectory() const { return getStringField(12); }
283     DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
284
285     /// Verify - Verify that a subprogram descriptor is well formed.
286     bool Verify() const;
287   };
288   
289   /// DIGlobalVariable - This is a wrapper for a global variable.
290   class DIGlobalVariable : public DIGlobal {
291   public:
292     explicit DIGlobalVariable(GlobalVariable *GV = 0);
293     
294     GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
295     std::string getFilename() const { return getStringField(12); }
296     std::string getDirectory() const { return getStringField(13); }
297
298     /// Verify - Verify that a global variable descriptor is well formed.
299     bool Verify() const;
300   };
301   
302   
303   /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
304   /// global etc).
305   class DIVariable : public DIDescriptor {
306   public:
307     explicit DIVariable(GlobalVariable *GV = 0);
308     
309     DIDescriptor getContext() const { return getDescriptorField(1); }
310     std::string getName() const { return getStringField(2); }
311     
312     DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
313     unsigned getLineNumber() const      { return getUnsignedField(4); }
314     DIType getType() const              { return getFieldAs<DIType>(5); }
315     std::string getFilename() const { return getStringField(6); }
316     std::string getDirectory() const { return getStringField(7); }
317     
318     /// isVariable - Return true if the specified tag is legal for DIVariable.
319     static bool isVariable(unsigned Tag);
320
321     /// Verify - Verify that a variable descriptor is well formed.
322     bool Verify() const;
323   };
324   
325   
326   /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
327   class DIBlock : public DIDescriptor {
328   public:
329     explicit DIBlock(GlobalVariable *GV = 0);
330     
331     DIDescriptor getContext() const { return getDescriptorField(1); }
332   };
333   
334   /// DIFactory - This object assists with the construction of the various
335   /// descriptors.
336   class DIFactory {
337     Module &M;
338     // Cached values for uniquing and faster lookups.
339     DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
340     const Type *EmptyStructPtr; // "{}*".
341     Function *StopPointFn;   // llvm.dbg.stoppoint
342     Function *FuncStartFn;   // llvm.dbg.func.start
343     Function *RegionStartFn; // llvm.dbg.region.start
344     Function *RegionEndFn;   // llvm.dbg.region.end
345     Function *DeclareFn;     // llvm.dbg.declare
346     StringMap<Constant*> StringCache;
347     DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
348     
349     DIFactory(const DIFactory &);     // DO NOT IMPLEMENT
350     void operator=(const DIFactory&); // DO NOT IMPLEMENT
351   public:
352     explicit DIFactory(Module &m);
353     
354     /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
355     /// creating a new one if there isn't already one in the module.
356     DIAnchor GetOrCreateCompileUnitAnchor();
357
358     /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
359     /// creating a new one if there isn't already one in the module.
360     DIAnchor GetOrCreateSubprogramAnchor();
361
362     /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
363     /// creating a new one if there isn't already one in the module.
364     DIAnchor GetOrCreateGlobalVariableAnchor();
365
366     /// GetOrCreateArray - Create an descriptor for an array of descriptors. 
367     /// This implicitly uniques the arrays created.
368     DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
369
370     /// GetOrCreateSubrange - Create a descriptor for a value range.  This
371     /// implicitly uniques the values returned.
372     DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
373     
374     
375     /// CreateCompileUnit - Create a new descriptor for the specified compile
376     /// unit.
377     DICompileUnit CreateCompileUnit(unsigned LangID,
378                                     const std::string &Filename,
379                                     const std::string &Directory,
380                                     const std::string &Producer);
381
382     /// CreateEnumerator - Create a single enumerator value.
383     DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
384     
385     /// CreateBasicType - Create a basic type like int, float, etc.
386     DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
387                                 DICompileUnit CompileUnit, unsigned LineNumber,
388                                 uint64_t SizeInBits, uint64_t AlignInBits,
389                                 uint64_t OffsetInBits, unsigned Flags,
390                                 unsigned Encoding,
391                                 const std::string *FileName = 0,
392                                 const std::string *Directory = 0);
393  
394     /// CreateDerivedType - Create a derived type like const qualified type,
395     /// pointer, typedef, etc.
396     DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
397                                     const std::string &Name,
398                                     DICompileUnit CompileUnit,
399                                     unsigned LineNumber,
400                                     uint64_t SizeInBits, uint64_t AlignInBits,
401                                     uint64_t OffsetInBits, unsigned Flags,
402                                     DIType DerivedFrom,
403                                     const std::string *FileName = 0,
404                                     const std::string *Directory = 0);
405
406     /// CreateCompositeType - Create a composite type like array, struct, etc.
407     DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
408                                         const std::string &Name,
409                                         DICompileUnit CompileUnit,
410                                         unsigned LineNumber,
411                                         uint64_t SizeInBits,
412                                         uint64_t AlignInBits,
413                                         uint64_t OffsetInBits, unsigned Flags,
414                                         DIType DerivedFrom,
415                                         DIArray Elements,
416                                         const std::string *FileName = 0,
417                                         const std::string *Directory = 0);
418     
419     /// CreateSubprogram - Create a new descriptor for the specified subprogram.
420     /// See comments in DISubprogram for descriptions of these fields.
421     DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
422                                   const std::string &DisplayName,
423                                   const std::string &LinkageName,
424                                   DICompileUnit CompileUnit, unsigned LineNo,
425                                   DIType Type, bool isLocalToUnit,
426                                   bool isDefinition,
427                                   const std::string *FileName = 0,
428                                   const std::string *Directory = 0);
429
430     /// CreateGlobalVariable - Create a new descriptor for the specified global.
431     DIGlobalVariable
432     CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
433                          const std::string &DisplayName,
434                          const std::string &LinkageName, 
435                          DICompileUnit CompileUnit,
436                          unsigned LineNo, DIType Type, bool isLocalToUnit,
437                          bool isDefinition, llvm::GlobalVariable *GV,
438                          const std::string *FileName = 0,
439                          const std::string *Directory = 0);
440     
441     
442     /// CreateVariable - Create a new descriptor for the specified variable.
443     DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
444                               const std::string &Name,
445                               DICompileUnit CompileUnit, unsigned LineNo,
446                               DIType Type,
447                               const std::string *FileName = 0,
448                               const std::string *Directory = 0);
449     
450     /// CreateBlock - This creates a descriptor for a lexical block with the
451     /// specified parent context.
452     DIBlock CreateBlock(DIDescriptor Context);
453     
454     /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
455     /// inserting it at the end of the specified basic block.
456     void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
457                          BasicBlock *BB);
458     
459     /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
460     /// mark the start of the specified subprogram.
461     void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
462
463     /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
464     /// mark the start of a region for the specified scoping descriptor.
465     void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
466     
467     /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
468     /// mark the end of a region for the specified scoping descriptor.
469     void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
470     
471     /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
472     void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
473     
474   private:
475     Constant *GetTagConstant(unsigned TAG);
476     Constant *GetStringConstant(const std::string &String);
477     DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
478     
479     /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
480     Constant *getCastToEmpty(DIDescriptor D);
481   };
482   
483   /// Finds the stoppoint coressponding to this instruction, that is the
484   /// stoppoint that dominates this instruction 
485   const DbgStopPointInst *findStopPoint(const Instruction *Inst);
486
487   /// Finds the stoppoint corresponding to first real (non-debug intrinsic) 
488   /// instruction in this Basic Block, and returns the stoppoint for it.
489   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
490
491   /// Finds the dbg.declare intrinsic corresponding to this value if any.
492   /// It looks through pointer casts too.
493   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
494 } // end namespace llvm
495
496 #endif