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