Sync up the tag numbers with gcc4.
[oota-llvm.git] / include / llvm / CodeGen / MachineDebugInfo.h
1 //===-- llvm/CodeGen/MachineDebugInfo.h -------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collect debug information for a module.  This information should be in a
11 // neutral form that can be used by different debugging schemes.
12 //
13 // The organization of information is primarily clustered around the source
14 // compile units.  The main exception is source line correspondence where
15 // inlining may interleave code from various compile units.
16 //
17 // The following information can be retrieved from the MachineDebugInfo.
18 //
19 //  -- Source directories - Directories are uniqued based on their canonical
20 //     string and assigned a sequential numeric ID (base 1.)
21 //  -- Source files - Files are also uniqued based on their name and directory
22 //     ID.  A file ID is sequential number (base 1.)
23 //  -- Source line coorespondence - A vector of file ID, line#, column# triples.
24 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
25 //     corresponding to each entry in the source line list.  This allows a debug
26 //     emitter to generate labels referenced by debug information tables.
27 //
28 //===----------------------------------------------------------------------===//
29
30 #ifndef LLVM_CODEGEN_MACHINEDEBUGINFO_H
31 #define LLVM_CODEGEN_MACHINEDEBUGINFO_H
32
33 #include "llvm/Support/Dwarf.h"
34 #include "llvm/ADT/UniqueVector.h"
35 #include "llvm/GlobalValue.h"
36 #include "llvm/Pass.h"
37 #include "llvm/User.h"
38
39 #include <string>
40 #include <set>
41
42 namespace llvm {
43
44 //===----------------------------------------------------------------------===//
45 // Forward declarations.
46 class Constant;
47 class DebugInfoDesc;
48 class GlobalVariable;
49 class Module;
50 class PointerType;
51 class StructType;
52
53 //===----------------------------------------------------------------------===//
54 // Debug info constants.
55
56 enum {
57   LLVMDebugVersion = 1,                 // Current version of debug information.
58   DIInvalid = ~0U,                      // Invalid result indicator.
59   
60   // DebugInfoDesc type identifying tags.
61   DI_TAG_anchor = 0,
62   DI_TAG_compile_unit,
63   DI_TAG_global_variable,
64   DI_TAG_subprogram
65 };
66
67 //===----------------------------------------------------------------------===//
68 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
69 /// the supplied DebugInfoDesc.
70 class DIVisitor {
71 public:
72   DIVisitor() {}
73   virtual ~DIVisitor() {}
74
75   /// ApplyToFields - Target the visitor to each field of the debug information
76   /// descriptor.
77   void ApplyToFields(DebugInfoDesc *DD);
78   
79   /// Apply - Subclasses override each of these methods to perform the
80   /// appropriate action for the type of field.
81   virtual void Apply(int &Field) = 0;
82   virtual void Apply(unsigned &Field) = 0;
83   virtual void Apply(bool &Field) = 0;
84   virtual void Apply(std::string &Field) = 0;
85   virtual void Apply(DebugInfoDesc *&Field) = 0;
86   virtual void Apply(GlobalVariable *&Field) = 0;
87 };
88
89 //===----------------------------------------------------------------------===//
90 /// DebugInfoDesc - This class is the base class for debug info descriptors.
91 ///
92 class DebugInfoDesc {
93 private:
94   unsigned Tag;                         // Content indicator.  Dwarf values are
95                                         // used but that does not limit use to
96                                         // Dwarf writers.
97   
98 protected:
99   DebugInfoDesc(unsigned T) : Tag(T) {}
100   
101 public:
102   virtual ~DebugInfoDesc() {}
103
104   // Accessors
105   unsigned getTag()          const { return Tag; }
106   
107   /// TagFromGlobal - Returns the Tag number from a debug info descriptor
108   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
109   static unsigned TagFromGlobal(GlobalVariable *GV);
110
111   /// DescFactory - Create an instance of debug info descriptor based on Tag.
112   /// Return NULL if not a recognized Tag.
113   static DebugInfoDesc *DescFactory(unsigned Tag);
114   
115   /// getLinkage - get linkage appropriate for this type of descriptor.
116   ///
117   virtual GlobalValue::LinkageTypes getLinkage() const;
118     
119   //===--------------------------------------------------------------------===//
120   // Subclasses should supply the following static methods.
121   
122   // Implement isa/cast/dyncast.
123   static bool classof(const DebugInfoDesc *)  { return true; }
124   
125   //===--------------------------------------------------------------------===//
126   // Subclasses should supply the following virtual methods.
127   
128   /// ApplyToFields - Target the vistor to the fields of the descriptor.
129   ///
130   virtual void ApplyToFields(DIVisitor *Visitor);
131
132   /// getDescString - Return a string used to compose global names and labels.
133   ///
134   virtual const char *getDescString() const = 0;
135   
136   /// getTypeString - Return a string used to label this descriptor's type.
137   ///
138   virtual const char *getTypeString() const = 0;
139   
140 #ifndef NDEBUG
141   virtual void dump() = 0;
142 #endif
143 };
144
145
146 //===----------------------------------------------------------------------===//
147 /// AnchorDesc - Descriptors of this class act as markers for identifying
148 /// descriptors of certain groups.
149 class AnchorDesc : public DebugInfoDesc {
150 private:  
151   std::string Name;                     // Anchor type string.
152   
153 public:
154   AnchorDesc()
155   : DebugInfoDesc(DI_TAG_anchor)
156   , Name("")
157   {}
158   AnchorDesc(const std::string &N)
159   : DebugInfoDesc(DI_TAG_anchor)
160   , Name(N)
161   {}
162   
163   // Accessors
164   const std::string &getName() const { return Name; }
165
166   // Implement isa/cast/dyncast.
167   static bool classof(const AnchorDesc *) { return true; }
168   static bool classof(const DebugInfoDesc *D) {
169     return D->getTag() == DI_TAG_anchor;
170   }
171
172   /// getLinkage - get linkage appropriate for this type of descriptor.
173   ///
174   virtual GlobalValue::LinkageTypes getLinkage() const;
175
176   /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
177   ///
178   virtual void ApplyToFields(DIVisitor *Visitor);
179
180   /// getDescString - Return a string used to compose global names and labels.
181   ///
182   virtual const char *getDescString() const;
183     
184   /// getTypeString - Return a string used to label this descriptor's type.
185   ///
186   virtual const char *getTypeString() const;
187     
188 #ifndef NDEBUG
189   virtual void dump();
190 #endif
191 };
192
193 //===----------------------------------------------------------------------===//
194 /// AnchoredDesc - This class manages anchors for a variety of top level
195 /// descriptors.
196 class AnchoredDesc : public DebugInfoDesc {
197 private:  
198   AnchorDesc *Anchor;                   // Anchor for all descriptors of the
199                                         // same type.
200
201 protected:
202
203   AnchoredDesc(unsigned T);
204
205 public:  
206   // Accessors.
207   AnchorDesc *getAnchor() const { return Anchor; }
208   void setAnchor(AnchorDesc *A) { Anchor = A; }
209
210   //===--------------------------------------------------------------------===//
211   // Subclasses should supply the following virtual methods.
212   
213   /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
214   ///
215   virtual void ApplyToFields(DIVisitor *Visitor);
216 };
217
218 //===----------------------------------------------------------------------===//
219 /// CompileUnitDesc - This class packages debug information associated with a 
220 /// source/header file.
221 class CompileUnitDesc : public AnchoredDesc {
222 private:  
223   unsigned DebugVersion;                // LLVM debug version when produced.
224   unsigned Language;                    // Language number (ex. DW_LANG_C89.)
225   std::string FileName;                 // Source file name.
226   std::string Directory;                // Source file directory.
227   std::string Producer;                 // Compiler string.
228   
229 public:
230   CompileUnitDesc();
231   
232   // Accessors
233   unsigned getDebugVersion()              const { return DebugVersion; }
234   unsigned getLanguage()                  const { return Language; }
235   const std::string &getFileName()        const { return FileName; }
236   const std::string &getDirectory()       const { return Directory; }
237   const std::string &getProducer()        const { return Producer; }
238   void setLanguage(unsigned L)                  { Language = L; }
239   void setFileName(const std::string &FN)       { FileName = FN; }
240   void setDirectory(const std::string &D)       { Directory = D; }
241   void setProducer(const std::string &P)        { Producer = P; }
242   
243   // FIXME - Need translation unit getter/setter.
244
245   // Implement isa/cast/dyncast.
246   static bool classof(const CompileUnitDesc *) { return true; }
247   static bool classof(const DebugInfoDesc *D) {
248     return D->getTag() == DI_TAG_compile_unit;
249   }
250   
251   /// DebugVersionFromGlobal - Returns the version number from a compile unit
252   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
253   static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
254   
255   /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
256   ///
257   virtual void ApplyToFields(DIVisitor *Visitor);
258
259   /// getDescString - Return a string used to compose global names and labels.
260   ///
261   virtual const char *getDescString() const;
262     
263   /// getTypeString - Return a string used to label this descriptor's type.
264   ///
265   virtual const char *getTypeString() const;
266   
267   /// getAnchorString - Return a string used to label this descriptor's anchor.
268   ///
269   virtual const char *getAnchorString() const;
270     
271 #ifndef NDEBUG
272   virtual void dump();
273 #endif
274 };
275
276 //===----------------------------------------------------------------------===//
277 /// GlobalDesc - This class is the base descriptor for global functions and
278 /// variables.
279 class GlobalDesc : public AnchoredDesc {
280 private:
281   DebugInfoDesc *Context;               // Context debug descriptor.
282   std::string Name;                     // Global name.
283   // FIXME - Use a descriptor.
284   GlobalVariable *TyDesc;               // Type debug descriptor.
285   bool IsStatic;                        // Is the global a static.
286   bool IsDefinition;                    // Is the global defined in context.
287   
288 protected:
289   GlobalDesc(unsigned T);
290
291 public:
292   // Accessors
293   DebugInfoDesc *getContext()                const { return Context; }
294   const std::string &getName()               const { return Name; }
295   bool isStatic()                            const { return IsStatic; }
296   bool isDefinition()                        const { return IsDefinition; }
297   void setContext(DebugInfoDesc *C)                { Context = C; }
298   void setName(const std::string &N)               { Name = N; }
299   void setIsStatic(bool IS)                        { IsStatic = IS; }
300   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
301
302   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
303   ///
304   virtual void ApplyToFields(DIVisitor *Visitor);
305 };
306
307 //===----------------------------------------------------------------------===//
308 /// GlobalVariableDesc - This class packages debug information associated with a
309 /// GlobalVariable.
310 class GlobalVariableDesc : public GlobalDesc {
311 private:
312   GlobalVariable *Global;               // llvm global.
313   
314 public:
315   GlobalVariableDesc();
316
317   // Accessors.
318   GlobalVariable *getGlobalVariable()        const { return Global; }
319   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
320   
321   // Implement isa/cast/dyncast.
322   static bool classof(const GlobalVariableDesc *)  { return true; }
323   static bool classof(const DebugInfoDesc *D) {
324     return D->getTag() == DI_TAG_global_variable;
325   }
326   
327   /// ApplyToFields - Target the visitor to the fields of the
328   /// GlobalVariableDesc.
329   virtual void ApplyToFields(DIVisitor *Visitor);
330
331   /// getDescString - Return a string used to compose global names and labels.
332   ///
333   virtual const char *getDescString() const;
334
335   /// getTypeString - Return a string used to label this descriptor's type.
336   ///
337   virtual const char *getTypeString() const;
338   
339   /// getAnchorString - Return a string used to label this descriptor's anchor.
340   ///
341   virtual const char *getAnchorString() const;
342     
343 #ifndef NDEBUG
344   virtual void dump();
345 #endif
346 };
347
348 //===----------------------------------------------------------------------===//
349 /// SubprogramDesc - This class packages debug information associated with a
350 /// subprogram/function.
351 class SubprogramDesc : public GlobalDesc {
352 private:
353   DebugInfoDesc *Context;               // Context debug descriptor.
354   std::string Name;                     // Subprogram name.
355   // FIXME - Use a descriptor.
356   GlobalVariable *TyDesc;               // Type debug descriptor.
357   bool IsStatic;                        // Is the subprogram a static.
358   bool IsDefinition;                    // Is the subprogram defined in context.
359   
360 public:
361   SubprogramDesc();
362   
363   // Accessors
364   DebugInfoDesc *getContext()                const { return Context; }
365   const std::string &getName()               const { return Name; }
366   bool isStatic()                            const { return IsStatic; }
367   bool isDefinition()                        const { return IsDefinition; }
368   void setContext(DebugInfoDesc *C)                { Context = C; }
369   void setName(const std::string &N)               { Name = N; }
370   void setIsStatic(bool IS)                        { IsStatic = IS; }
371   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
372   // FIXME - Other getters/setters.
373   
374   // Implement isa/cast/dyncast.
375   static bool classof(const SubprogramDesc *)  { return true; }
376   static bool classof(const DebugInfoDesc *D) {
377     return D->getTag() == DI_TAG_subprogram;
378   }
379   
380   /// ApplyToFields - Target the visitor to the fields of the  SubprogramDesc.
381   ///
382   virtual void ApplyToFields(DIVisitor *Visitor);
383
384   /// getDescString - Return a string used to compose global names and labels.
385   ///
386   virtual const char *getDescString() const;
387
388   /// getTypeString - Return a string used to label this descriptor's type.
389   ///
390   virtual const char *getTypeString() const;
391   
392   /// getAnchorString - Return a string used to label this descriptor's anchor.
393   ///
394   virtual const char *getAnchorString() const;
395     
396 #ifndef NDEBUG
397   virtual void dump();
398 #endif
399 };
400
401 //===----------------------------------------------------------------------===//
402 /// DIDeserializer - This class is responsible for casting GlobalVariables
403 /// into DebugInfoDesc objects.
404 class DIDeserializer {
405 private:
406   unsigned DebugVersion;                // Version of debug information in use.
407   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
408                                         // Previously defined gloabls.
409   
410 public:
411   DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
412   ~DIDeserializer() {}
413   
414   // Accessors
415   unsigned getDebugVersion() const { return DebugVersion; }
416   
417   /// Deserialize - Reconstitute a GlobalVariable into it's component
418   /// DebugInfoDesc objects.
419   DebugInfoDesc *Deserialize(Value *V);
420   DebugInfoDesc *Deserialize(GlobalVariable *GV);
421 };
422
423 //===----------------------------------------------------------------------===//
424 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
425 /// into GlobalVariables.
426 class DISerializer {
427 private:
428   Module *M;                            // Definition space module.
429   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
430   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
431   std::map<unsigned, StructType *> TagTypes;
432                                         // Types per Tag.  Created lazily.
433   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
434                                         // Previously defined descriptors.
435   std::map<const std::string, Constant *> StringCache;
436                                         // Previously defined strings.
437                                           
438 public:
439   DISerializer()
440   : M(NULL)
441   , StrPtrTy(NULL)
442   , EmptyStructPtrTy(NULL)
443   , TagTypes()
444   , DescGlobals()
445   , StringCache()
446   {}
447   ~DISerializer() {}
448   
449   // Accessors
450   Module *getModule()        const { return M; };
451   void setModule(Module *module)  { M = module; }
452
453   /// getStrPtrType - Return a "sbyte *" type.
454   ///
455   const PointerType *getStrPtrType();
456   
457   /// getEmptyStructPtrType - Return a "{ }*" type.
458   ///
459   const PointerType *getEmptyStructPtrType();
460   
461   /// getTagType - Return the type describing the specified descriptor (via
462   /// tag.)
463   const StructType *getTagType(DebugInfoDesc *DD);
464   
465   /// getString - Construct the string as constant string global.
466   ///
467   Constant *getString(const std::string &String);
468   
469   /// Serialize - Recursively cast the specified descriptor into a
470   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
471   GlobalVariable *Serialize(DebugInfoDesc *DD);
472 };
473
474 //===----------------------------------------------------------------------===//
475 /// DIVerifier - This class is responsible for verifying the given network of
476 /// GlobalVariables are valid as DebugInfoDesc objects.
477 class DIVerifier {
478 private:
479   unsigned DebugVersion;                // Version of debug information in use.
480   std::set<GlobalVariable *> Visited;   // Tracks visits during recursion.
481   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
482
483   /// markVisited - Return true if the GlobalVariable hase been "seen" before.
484   /// Mark markVisited otherwise.
485   bool markVisited(GlobalVariable *GV);
486   
487 public:
488   DIVerifier() : DebugVersion(LLVMDebugVersion) {}
489   ~DIVerifier() {}
490   
491   /// Verify - Return true if the GlobalVariable appears to be a valid
492   /// serialization of a DebugInfoDesc.
493   bool Verify(Value *V);
494   bool Verify(GlobalVariable *GV);
495 };
496
497 //===----------------------------------------------------------------------===//
498 /// SourceLineInfo - This class is used to record source line correspondence.
499 ///
500 class SourceLineInfo {
501 private:
502   unsigned Line;                        // Source line number.
503   unsigned Column;                      // Source column.
504   unsigned SourceID;                    // Source ID number.
505
506 public:
507   SourceLineInfo(unsigned L, unsigned C, unsigned S)
508   : Line(L), Column(C), SourceID(S) {}
509   
510   // Accessors
511   unsigned getLine()     const { return Line; }
512   unsigned getColumn()   const { return Column; }
513   unsigned getSourceID() const { return SourceID; }
514 };
515
516 //===----------------------------------------------------------------------===//
517 /// SourceFileInfo - This class is used to track source information.
518 ///
519 class SourceFileInfo {
520 private:
521   unsigned DirectoryID;                 // Directory ID number.
522   std::string Name;                     // File name (not including directory.)
523   
524 public:
525   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
526             
527   // Accessors
528   unsigned getDirectoryID()    const { return DirectoryID; }
529   const std::string &getName() const { return Name; }
530
531   /// operator== - Used by UniqueVector to locate entry.
532   ///
533   bool operator==(const SourceFileInfo &SI) const {
534     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
535   }
536
537   /// operator< - Used by UniqueVector to locate entry.
538   ///
539   bool operator<(const SourceFileInfo &SI) const {
540     return getDirectoryID() < SI.getDirectoryID() ||
541           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
542   }
543 };
544
545 //===----------------------------------------------------------------------===//
546 /// MachineDebugInfo - This class contains debug information specific to a
547 /// module.  Queries can be made by different debugging schemes and reformated
548 /// for specific use.
549 ///
550 class MachineDebugInfo : public ImmutablePass {
551 private:
552   // Use the same serializer/deserializer/verifier for the module.
553   DIDeserializer DR;
554
555   // CompileUnits - Uniquing vector for compile units.
556   UniqueVector<CompileUnitDesc *> CompileUnits;
557   
558   // Directories - Uniquing vector for directories.
559   UniqueVector<std::string> Directories;
560                                          
561   // SourceFiles - Uniquing vector for source files.
562   UniqueVector<SourceFileInfo> SourceFiles;
563
564   // Lines - List of of source line correspondence.
565   std::vector<SourceLineInfo *> Lines;
566
567 public:
568   MachineDebugInfo();
569   ~MachineDebugInfo();
570   
571   /// doInitialization - Initialize the debug state for a new module.
572   ///
573   bool doInitialization();
574   
575   /// doFinalization - Tear down the debug state after completion of a module.
576   ///
577   bool doFinalization();
578   
579   /// getDescFor - Convert a Value to a debug information descriptor.
580   ///
581   // FIXME - use new Value type when available.
582   DebugInfoDesc *getDescFor(Value *V);
583   
584   /// Verify - Verify that a Value is debug information descriptor.
585   ///
586   bool Verify(Value *V);
587   
588   /// AnalyzeModule - Scan the module for global debug information.
589   ///
590   void AnalyzeModule(Module &M);
591   
592   /// hasInfo - Returns true if valid debug info is present.
593   ///
594   bool hasInfo() const { return !CompileUnits.empty(); }
595   
596   /// RecordLabel - Records location information and associates it with a
597   /// debug label.  Returns a unique label ID used to generate a label and 
598   /// provide correspondence to the source line list.
599   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
600     Lines.push_back(new SourceLineInfo(Line, Column, Source));
601     return Lines.size();
602   }
603   
604   /// RecordSource - Register a source file with debug info. Returns an source
605   /// ID.
606   unsigned RecordSource(const std::string &Directory,
607                                const std::string &Source) {
608     unsigned DirectoryID = Directories.insert(Directory);
609     return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
610   }
611   
612   /// getDirectories - Return the UniqueVector of std::string representing
613   /// directories.
614   const UniqueVector<std::string> &getDirectories() const {
615     return Directories;
616   }
617   
618   /// getSourceFiles - Return the UniqueVector of source files. 
619   ///
620   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
621     return SourceFiles;
622   }
623   
624   /// getSourceLines - Return a vector of source lines.  Vector index + 1
625   /// equals label ID.
626   const std::vector<SourceLineInfo *> &getSourceLines() const {
627     return Lines;
628   }
629   
630   /// SetupCompileUnits - Set up the unique vector of compile units.
631   ///
632   void SetupCompileUnits(Module &M);
633
634   /// getCompileUnits - Return a vector of debug compile units.
635   ///
636   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
637
638   /// getGlobalVariables - Return a vector of debug GlobalVariables.
639   ///
640   std::vector<GlobalVariableDesc *> getGlobalVariables(Module &M);
641
642 }; // End class MachineDebugInfo
643
644 } // End llvm namespace
645
646 #endif