Split this out of Target.td
[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/Support/DataTypes.h"
35 #include "llvm/ADT/UniqueVector.h"
36 #include "llvm/GlobalValue.h"
37 #include "llvm/Pass.h"
38 #include "llvm/User.h"
39
40 #include <string>
41 #include <set>
42
43 namespace llvm {
44
45 //===----------------------------------------------------------------------===//
46 // Forward declarations.
47 class Constant;
48 class DebugInfoDesc;
49 class GlobalVariable;
50 class Module;
51 class PointerType;
52 class StructType;
53
54 //===----------------------------------------------------------------------===//
55 // Debug info constants.
56
57 enum {
58   LLVMDebugVersion = 1                  // Current version of debug information.
59 };
60
61 //===----------------------------------------------------------------------===//
62 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
63 /// the supplied DebugInfoDesc.
64 class DIVisitor {
65 public:
66   DIVisitor() {}
67   virtual ~DIVisitor() {}
68
69   /// ApplyToFields - Target the visitor to each field of the debug information
70   /// descriptor.
71   void ApplyToFields(DebugInfoDesc *DD);
72   
73   /// Apply - Subclasses override each of these methods to perform the
74   /// appropriate action for the type of field.
75   virtual void Apply(int &Field) = 0;
76   virtual void Apply(unsigned &Field) = 0;
77   virtual void Apply(int64_t &Field) = 0;
78   virtual void Apply(uint64_t &Field) = 0;
79   virtual void Apply(bool &Field) = 0;
80   virtual void Apply(std::string &Field) = 0;
81   virtual void Apply(DebugInfoDesc *&Field) = 0;
82   virtual void Apply(GlobalVariable *&Field) = 0;
83   virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
84 };
85
86 //===----------------------------------------------------------------------===//
87 /// DebugInfoDesc - This class is the base class for debug info descriptors.
88 ///
89 class DebugInfoDesc {
90 private:
91   unsigned Tag;                         // Content indicator.  Dwarf values are
92                                         // used but that does not limit use to
93                                         // Dwarf writers.
94   
95 protected:
96   DebugInfoDesc(unsigned T) : Tag(T) {}
97   
98 public:
99   virtual ~DebugInfoDesc() {}
100
101   // Accessors
102   unsigned getTag()          const { return Tag; }
103   
104   /// TagFromGlobal - Returns the Tag number from a debug info descriptor
105   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
106   static unsigned TagFromGlobal(GlobalVariable *GV);
107
108   /// DescFactory - Create an instance of debug info descriptor based on Tag.
109   /// Return NULL if not a recognized Tag.
110   static DebugInfoDesc *DescFactory(unsigned Tag);
111   
112   /// getLinkage - get linkage appropriate for this type of descriptor.
113   ///
114   virtual GlobalValue::LinkageTypes getLinkage() const;
115     
116   //===--------------------------------------------------------------------===//
117   // Subclasses should supply the following static methods.
118   
119   // Implement isa/cast/dyncast.
120   static bool classof(const DebugInfoDesc *) { return true; }
121   
122   //===--------------------------------------------------------------------===//
123   // Subclasses should supply the following virtual methods.
124   
125   /// ApplyToFields - Target the vistor to the fields of the descriptor.
126   ///
127   virtual void ApplyToFields(DIVisitor *Visitor);
128
129   /// getDescString - Return a string used to compose global names and labels.
130   ///
131   virtual const char *getDescString() const = 0;
132   
133   /// getTypeString - Return a string used to label this descriptor's type.
134   ///
135   virtual const char *getTypeString() const = 0;
136   
137 #ifndef NDEBUG
138   virtual void dump() = 0;
139 #endif
140 };
141
142 //===----------------------------------------------------------------------===//
143 /// AnchorDesc - Descriptors of this class act as markers for identifying
144 /// descriptors of certain groups.
145 class AnchorDesc : public DebugInfoDesc {
146 private:  
147   std::string Name;                     // Anchor type string.
148   
149 public:
150   AnchorDesc();
151   AnchorDesc(const std::string &N);
152   
153   // Accessors
154   const std::string &getName() const { return Name; }
155
156   // Implement isa/cast/dyncast.
157   static bool classof(const AnchorDesc *) { return true; }
158   static bool classof(const DebugInfoDesc *D);
159
160   /// getLinkage - get linkage appropriate for this type of descriptor.
161   ///
162   virtual GlobalValue::LinkageTypes getLinkage() const;
163
164   /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
165   ///
166   virtual void ApplyToFields(DIVisitor *Visitor);
167
168   /// getDescString - Return a string used to compose global names and labels.
169   ///
170   virtual const char *getDescString() const;
171     
172   /// getTypeString - Return a string used to label this descriptor's type.
173   ///
174   virtual const char *getTypeString() const;
175     
176 #ifndef NDEBUG
177   virtual void dump();
178 #endif
179 };
180
181 //===----------------------------------------------------------------------===//
182 /// AnchoredDesc - This class manages anchors for a variety of top level
183 /// descriptors.
184 class AnchoredDesc : public DebugInfoDesc {
185 private:  
186   AnchorDesc *Anchor;                   // Anchor for all descriptors of the
187                                         // same type.
188
189 protected:
190
191   AnchoredDesc(unsigned T);
192
193 public:  
194   // Accessors.
195   AnchorDesc *getAnchor() const { return Anchor; }
196   void setAnchor(AnchorDesc *A) { Anchor = A; }
197
198   //===--------------------------------------------------------------------===//
199   // Subclasses should supply the following virtual methods.
200   
201   /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
202   ///
203   virtual void ApplyToFields(DIVisitor *Visitor);
204 };
205
206 //===----------------------------------------------------------------------===//
207 /// CompileUnitDesc - This class packages debug information associated with a 
208 /// source/header file.
209 class CompileUnitDesc : public AnchoredDesc {
210 private:  
211   unsigned DebugVersion;                // LLVM debug version when produced.
212   unsigned Language;                    // Language number (ex. DW_LANG_C89.)
213   std::string FileName;                 // Source file name.
214   std::string Directory;                // Source file directory.
215   std::string Producer;                 // Compiler string.
216   
217 public:
218   CompileUnitDesc();
219   
220   // Accessors
221   unsigned getDebugVersion()              const { return DebugVersion; }
222   unsigned getLanguage()                  const { return Language; }
223   const std::string &getFileName()        const { return FileName; }
224   const std::string &getDirectory()       const { return Directory; }
225   const std::string &getProducer()        const { return Producer; }
226   void setLanguage(unsigned L)                  { Language = L; }
227   void setFileName(const std::string &FN)       { FileName = FN; }
228   void setDirectory(const std::string &D)       { Directory = D; }
229   void setProducer(const std::string &P)        { Producer = P; }
230   
231   // FIXME - Need translation unit getter/setter.
232
233   // Implement isa/cast/dyncast.
234   static bool classof(const CompileUnitDesc *) { return true; }
235   static bool classof(const DebugInfoDesc *D);
236   
237   /// DebugVersionFromGlobal - Returns the version number from a compile unit
238   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
239   static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
240   
241   /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
242   ///
243   virtual void ApplyToFields(DIVisitor *Visitor);
244
245   /// getDescString - Return a string used to compose global names and labels.
246   ///
247   virtual const char *getDescString() const;
248     
249   /// getTypeString - Return a string used to label this descriptor's type.
250   ///
251   virtual const char *getTypeString() const;
252   
253   /// getAnchorString - Return a string used to label this descriptor's anchor.
254   ///
255   virtual const char *getAnchorString() const;
256     
257 #ifndef NDEBUG
258   virtual void dump();
259 #endif
260 };
261
262 //===----------------------------------------------------------------------===//
263 /// TypeDesc - This class packages debug information associated with a type.
264 ///
265 class TypeDesc : public DebugInfoDesc {
266 private:
267   DebugInfoDesc *Context;               // Context debug descriptor.
268   std::string Name;                     // Type name (may be empty.)
269   CompileUnitDesc *File;                // Declared compile unit (may be NULL.)
270   int Line;                             // Declared line# (may be zero.)
271   uint64_t Size;                        // Type size (may be zero.)
272
273 protected:
274   TypeDesc(unsigned T);
275
276 public:
277   // Accessors
278   DebugInfoDesc *getContext()                const { return Context; }
279   const std::string &getName()               const { return Name; }
280   CompileUnitDesc *getFile()                 const { return File; }
281   int getLine()                              const { return Line; }
282   uint64_t getSize()                         const { return Size; }
283   void setContext(DebugInfoDesc *C)                { Context = C; }
284   void setName(const std::string &N)               { Name = N; }
285   void setFile(CompileUnitDesc *U)                 { File = U; }
286   void setLine(int L)                              { Line = L; }
287   void setSize(uint64_t S)                         { Size = S; }
288   
289   /// ApplyToFields - Target the visitor to the fields of the  TypeDesc.
290   ///
291   virtual void ApplyToFields(DIVisitor *Visitor);
292
293   /// getDescString - Return a string used to compose global names and labels.
294   ///
295   virtual const char *getDescString() const;
296
297   /// getTypeString - Return a string used to label this descriptor's type.
298   ///
299   virtual const char *getTypeString() const;
300   
301 #ifndef NDEBUG
302   virtual void dump();
303 #endif
304 };
305
306 //===----------------------------------------------------------------------===//
307 /// BasicTypeDesc - This class packages debug information associated with a
308 /// basic type (eg. int, bool, double.)
309 class BasicTypeDesc : public TypeDesc {
310 private:
311   unsigned Encoding;                    // Type encoding.
312   
313 public:
314   BasicTypeDesc();
315   
316   // Accessors
317   unsigned getEncoding()                     const { return Encoding; }
318   void setEncoding(unsigned E)                     { Encoding = E; }
319
320   // Implement isa/cast/dyncast.
321   static bool classof(const BasicTypeDesc *) { return true; }
322   static bool classof(const DebugInfoDesc *D);
323   
324   /// ApplyToFields - Target the visitor to the fields of the  BasicTypeDesc.
325   ///
326   virtual void ApplyToFields(DIVisitor *Visitor);
327
328   /// getDescString - Return a string used to compose global names and labels.
329   ///
330   virtual const char *getDescString() const;
331
332   /// getTypeString - Return a string used to label this descriptor's type.
333   ///
334   virtual const char *getTypeString() const;
335
336 #ifndef NDEBUG
337   virtual void dump();
338 #endif
339 };
340
341
342 //===----------------------------------------------------------------------===//
343 /// DerivedTypeDesc - This class packages debug information associated with a
344 /// derived types (eg., typedef, pointer, reference.)
345 class DerivedTypeDesc : public TypeDesc {
346 private:
347   TypeDesc *FromType;                   // Type derived from.
348
349 public:
350   DerivedTypeDesc(unsigned T);
351   
352   // Accessors
353   TypeDesc *getFromType()                    const { return FromType; }
354   void setFromType(TypeDesc *F)                    { FromType = F; }
355
356   // Implement isa/cast/dyncast.
357   static bool classof(const DerivedTypeDesc *) { return true; }
358   static bool classof(const DebugInfoDesc *D);
359   
360   /// ApplyToFields - Target the visitor to the fields of the  DerivedTypeDesc.
361   ///
362   virtual void ApplyToFields(DIVisitor *Visitor);
363
364   /// getDescString - Return a string used to compose global names and labels.
365   ///
366   virtual const char *getDescString() const;
367
368   /// getTypeString - Return a string used to label this descriptor's type.
369   ///
370   virtual const char *getTypeString() const;
371
372 #ifndef NDEBUG
373   virtual void dump();
374 #endif
375 };
376
377 //===----------------------------------------------------------------------===//
378 /// CompositeTypeDesc - This class packages debug information associated with a
379 /// array/struct types (eg., arrays, struct, union, enums.)
380 class CompositeTypeDesc : public DerivedTypeDesc {
381 private:
382   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
383
384 public:
385   CompositeTypeDesc(unsigned T);
386   
387   // Accessors
388   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
389
390   // Implement isa/cast/dyncast.
391   static bool classof(const CompositeTypeDesc *) { return true; }
392   static bool classof(const DebugInfoDesc *D);
393   
394   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
395   ///
396   virtual void ApplyToFields(DIVisitor *Visitor);
397
398   /// getDescString - Return a string used to compose global names and labels.
399   ///
400   virtual const char *getDescString() const;
401
402   /// getTypeString - Return a string used to label this descriptor's type.
403   ///
404   virtual const char *getTypeString() const;
405
406 #ifndef NDEBUG
407   virtual void dump();
408 #endif
409 };
410
411 //===----------------------------------------------------------------------===//
412 /// SubrangeDesc - This class packages debug information associated with integer
413 /// value ranges.
414 class SubrangeDesc : public DebugInfoDesc {
415 private:
416   int64_t Lo;                           // Low value of range
417   int64_t Hi;                           // High value of range
418
419 public:
420   SubrangeDesc();
421   
422   // Accessors
423   int64_t getLo()                            const { return Lo; }
424   int64_t getHi()                            const { return Hi; }
425   void setLo(int64_t L)                            { Lo = L; }
426   void setHi(int64_t H)                            { Hi = H; }
427
428   // Implement isa/cast/dyncast.
429   static bool classof(const SubrangeDesc *) { return true; }
430   static bool classof(const DebugInfoDesc *D);
431   
432   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
433   ///
434   virtual void ApplyToFields(DIVisitor *Visitor);
435
436   /// getDescString - Return a string used to compose global names and labels.
437   ///
438   virtual const char *getDescString() const;
439     
440   /// getTypeString - Return a string used to label this descriptor's type.
441   ///
442   virtual const char *getTypeString() const;
443
444 #ifndef NDEBUG
445   virtual void dump();
446 #endif
447 };
448
449 //===----------------------------------------------------------------------===//
450 /// EnumeratorDesc - This class packages debug information associated with
451 /// named integer constants.
452 class EnumeratorDesc : public DebugInfoDesc {
453 private:
454   std::string Name;                     // Enumerator name.
455   int64_t Value;                        // Enumerator value.
456
457 public:
458   EnumeratorDesc();
459   
460   // Accessors
461   const std::string &getName()               const { return Name; }
462   int64_t getValue()                         const { return Value; }
463   void setName(const std::string &N)               { Name = N; }
464   void setValue(int64_t V)                         { Value = V; }
465
466   // Implement isa/cast/dyncast.
467   static bool classof(const EnumeratorDesc *) { return true; }
468   static bool classof(const DebugInfoDesc *D);
469   
470   /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
471   ///
472   virtual void ApplyToFields(DIVisitor *Visitor);
473
474   /// getDescString - Return a string used to compose global names and labels.
475   ///
476   virtual const char *getDescString() const;
477     
478   /// getTypeString - Return a string used to label this descriptor's type.
479   ///
480   virtual const char *getTypeString() const;
481
482 #ifndef NDEBUG
483   virtual void dump();
484 #endif
485 };
486
487 //===----------------------------------------------------------------------===//
488 /// GlobalDesc - This class is the base descriptor for global functions and
489 /// variables.
490 class GlobalDesc : public AnchoredDesc {
491 private:
492   DebugInfoDesc *Context;               // Context debug descriptor.
493   std::string Name;                     // Global name.
494   TypeDesc *TyDesc;                     // Type debug descriptor.
495   bool IsStatic;                        // Is the global a static.
496   bool IsDefinition;                    // Is the global defined in context.
497   
498 protected:
499   GlobalDesc(unsigned T);
500
501 public:
502   // Accessors
503   DebugInfoDesc *getContext()                const { return Context; }
504   const std::string &getName()               const { return Name; }
505   TypeDesc *getTypeDesc()                    const { return TyDesc; }
506   bool isStatic()                            const { return IsStatic; }
507   bool isDefinition()                        const { return IsDefinition; }
508   void setContext(DebugInfoDesc *C)                { Context = C; }
509   void setName(const std::string &N)               { Name = N; }
510   void setTypeDesc(TypeDesc *T)                    { TyDesc = T; }
511   void setIsStatic(bool IS)                        { IsStatic = IS; }
512   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
513
514   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
515   ///
516   virtual void ApplyToFields(DIVisitor *Visitor);
517 };
518
519 //===----------------------------------------------------------------------===//
520 /// GlobalVariableDesc - This class packages debug information associated with a
521 /// GlobalVariable.
522 class GlobalVariableDesc : public GlobalDesc {
523 private:
524   GlobalVariable *Global;               // llvm global.
525   unsigned Line;                        // Source line number.
526   
527 public:
528   GlobalVariableDesc();
529
530   // Accessors.
531   GlobalVariable *getGlobalVariable()        const { return Global; }
532   unsigned getLine()                         const { return Line; }
533   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
534   void setLine(unsigned L)                         { Line = L; }
535  
536   // Implement isa/cast/dyncast.
537   static bool classof(const GlobalVariableDesc *) { return true; }
538   static bool classof(const DebugInfoDesc *D);
539   
540   /// ApplyToFields - Target the visitor to the fields of the
541   /// GlobalVariableDesc.
542   virtual void ApplyToFields(DIVisitor *Visitor);
543
544   /// getDescString - Return a string used to compose global names and labels.
545   ///
546   virtual const char *getDescString() const;
547
548   /// getTypeString - Return a string used to label this descriptor's type.
549   ///
550   virtual const char *getTypeString() const;
551   
552   /// getAnchorString - Return a string used to label this descriptor's anchor.
553   ///
554   virtual const char *getAnchorString() const;
555     
556 #ifndef NDEBUG
557   virtual void dump();
558 #endif
559 };
560
561 //===----------------------------------------------------------------------===//
562 /// SubprogramDesc - This class packages debug information associated with a
563 /// subprogram/function.
564 class SubprogramDesc : public GlobalDesc {
565 private:
566   // FIXME - Other attributes
567   
568 public:
569   SubprogramDesc();
570   
571   // Accessors
572   // FIXME - Other getters/setters.
573   
574   // Implement isa/cast/dyncast.
575   static bool classof(const SubprogramDesc *) { return true; }
576   static bool classof(const DebugInfoDesc *D);
577   
578   /// ApplyToFields - Target the visitor to the fields of the  SubprogramDesc.
579   ///
580   virtual void ApplyToFields(DIVisitor *Visitor);
581
582   /// getDescString - Return a string used to compose global names and labels.
583   ///
584   virtual const char *getDescString() const;
585
586   /// getTypeString - Return a string used to label this descriptor's type.
587   ///
588   virtual const char *getTypeString() const;
589   
590   /// getAnchorString - Return a string used to label this descriptor's anchor.
591   ///
592   virtual const char *getAnchorString() const;
593     
594 #ifndef NDEBUG
595   virtual void dump();
596 #endif
597 };
598
599 //===----------------------------------------------------------------------===//
600 /// DIDeserializer - This class is responsible for casting GlobalVariables
601 /// into DebugInfoDesc objects.
602 class DIDeserializer {
603 private:
604   unsigned DebugVersion;                // Version of debug information in use.
605   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
606                                         // Previously defined gloabls.
607   
608 public:
609   DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
610   ~DIDeserializer() {}
611   
612   // Accessors
613   unsigned getDebugVersion() const { return DebugVersion; }
614   
615   /// Deserialize - Reconstitute a GlobalVariable into it's component
616   /// DebugInfoDesc objects.
617   DebugInfoDesc *Deserialize(Value *V);
618   DebugInfoDesc *Deserialize(GlobalVariable *GV);
619 };
620
621 //===----------------------------------------------------------------------===//
622 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
623 /// into GlobalVariables.
624 class DISerializer {
625 private:
626   Module *M;                            // Definition space module.
627   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
628   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
629   std::map<unsigned, StructType *> TagTypes;
630                                         // Types per Tag.  Created lazily.
631   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
632                                         // Previously defined descriptors.
633   std::map<const std::string, Constant *> StringCache;
634                                         // Previously defined strings.
635                                           
636 public:
637   DISerializer()
638   : M(NULL)
639   , StrPtrTy(NULL)
640   , EmptyStructPtrTy(NULL)
641   , TagTypes()
642   , DescGlobals()
643   , StringCache()
644   {}
645   ~DISerializer() {}
646   
647   // Accessors
648   Module *getModule()        const { return M; };
649   void setModule(Module *module)  { M = module; }
650
651   /// getStrPtrType - Return a "sbyte *" type.
652   ///
653   const PointerType *getStrPtrType();
654   
655   /// getEmptyStructPtrType - Return a "{ }*" type.
656   ///
657   const PointerType *getEmptyStructPtrType();
658   
659   /// getTagType - Return the type describing the specified descriptor (via
660   /// tag.)
661   const StructType *getTagType(DebugInfoDesc *DD);
662   
663   /// getString - Construct the string as constant string global.
664   ///
665   Constant *getString(const std::string &String);
666   
667   /// Serialize - Recursively cast the specified descriptor into a
668   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
669   GlobalVariable *Serialize(DebugInfoDesc *DD);
670 };
671
672 //===----------------------------------------------------------------------===//
673 /// DIVerifier - This class is responsible for verifying the given network of
674 /// GlobalVariables are valid as DebugInfoDesc objects.
675 class DIVerifier {
676 private:
677   unsigned DebugVersion;                // Version of debug information in use.
678   std::set<GlobalVariable *> Visited;   // Tracks visits during recursion.
679   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
680
681   /// markVisited - Return true if the GlobalVariable hase been "seen" before.
682   /// Mark markVisited otherwise.
683   bool markVisited(GlobalVariable *GV);
684   
685 public:
686   DIVerifier() : DebugVersion(LLVMDebugVersion) {}
687   ~DIVerifier() {}
688   
689   /// Verify - Return true if the GlobalVariable appears to be a valid
690   /// serialization of a DebugInfoDesc.
691   bool Verify(Value *V);
692   bool Verify(GlobalVariable *GV);
693 };
694
695 //===----------------------------------------------------------------------===//
696 /// SourceLineInfo - This class is used to record source line correspondence.
697 ///
698 class SourceLineInfo {
699 private:
700   unsigned Line;                        // Source line number.
701   unsigned Column;                      // Source column.
702   unsigned SourceID;                    // Source ID number.
703
704 public:
705   SourceLineInfo(unsigned L, unsigned C, unsigned S)
706   : Line(L), Column(C), SourceID(S) {}
707   
708   // Accessors
709   unsigned getLine()     const { return Line; }
710   unsigned getColumn()   const { return Column; }
711   unsigned getSourceID() const { return SourceID; }
712 };
713
714 //===----------------------------------------------------------------------===//
715 /// SourceFileInfo - This class is used to track source information.
716 ///
717 class SourceFileInfo {
718 private:
719   unsigned DirectoryID;                 // Directory ID number.
720   std::string Name;                     // File name (not including directory.)
721   
722 public:
723   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
724             
725   // Accessors
726   unsigned getDirectoryID()    const { return DirectoryID; }
727   const std::string &getName() const { return Name; }
728
729   /// operator== - Used by UniqueVector to locate entry.
730   ///
731   bool operator==(const SourceFileInfo &SI) const {
732     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
733   }
734
735   /// operator< - Used by UniqueVector to locate entry.
736   ///
737   bool operator<(const SourceFileInfo &SI) const {
738     return getDirectoryID() < SI.getDirectoryID() ||
739           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
740   }
741 };
742
743 //===----------------------------------------------------------------------===//
744 /// MachineDebugInfo - This class contains debug information specific to a
745 /// module.  Queries can be made by different debugging schemes and reformated
746 /// for specific use.
747 ///
748 class MachineDebugInfo : public ImmutablePass {
749 private:
750   // Use the same serializer/deserializer/verifier for the module.
751   DIDeserializer DR;
752
753   // CompileUnits - Uniquing vector for compile units.
754   UniqueVector<CompileUnitDesc *> CompileUnits;
755   
756   // Directories - Uniquing vector for directories.
757   UniqueVector<std::string> Directories;
758                                          
759   // SourceFiles - Uniquing vector for source files.
760   UniqueVector<SourceFileInfo> SourceFiles;
761
762   // Lines - List of of source line correspondence.
763   std::vector<SourceLineInfo *> Lines;
764
765 public:
766   MachineDebugInfo();
767   ~MachineDebugInfo();
768   
769   /// doInitialization - Initialize the debug state for a new module.
770   ///
771   bool doInitialization();
772   
773   /// doFinalization - Tear down the debug state after completion of a module.
774   ///
775   bool doFinalization();
776   
777   /// getDescFor - Convert a Value to a debug information descriptor.
778   ///
779   // FIXME - use new Value type when available.
780   DebugInfoDesc *getDescFor(Value *V);
781   
782   /// Verify - Verify that a Value is debug information descriptor.
783   ///
784   bool Verify(Value *V);
785   
786   /// AnalyzeModule - Scan the module for global debug information.
787   ///
788   void AnalyzeModule(Module &M);
789   
790   /// hasInfo - Returns true if valid debug info is present.
791   ///
792   bool hasInfo() const { return !CompileUnits.empty(); }
793   
794   /// RecordLabel - Records location information and associates it with a
795   /// debug label.  Returns a unique label ID used to generate a label and 
796   /// provide correspondence to the source line list.
797   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
798     Lines.push_back(new SourceLineInfo(Line, Column, Source));
799     return Lines.size();
800   }
801   
802   /// RecordSource - Register a source file with debug info. Returns an source
803   /// ID.
804   unsigned RecordSource(const std::string &Directory,
805                         const std::string &Source) {
806     unsigned DirectoryID = Directories.insert(Directory);
807     return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
808   }
809   unsigned RecordSource(const CompileUnitDesc *CompileUnit) {
810     return RecordSource(CompileUnit->getDirectory(),
811                         CompileUnit->getFileName());
812   }
813   
814   /// getDirectories - Return the UniqueVector of std::string representing
815   /// directories.
816   const UniqueVector<std::string> &getDirectories() const {
817     return Directories;
818   }
819   
820   /// getSourceFiles - Return the UniqueVector of source files. 
821   ///
822   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
823     return SourceFiles;
824   }
825   
826   /// getSourceLines - Return a vector of source lines.  Vector index + 1
827   /// equals label ID.
828   const std::vector<SourceLineInfo *> &getSourceLines() const {
829     return Lines;
830   }
831   
832   /// SetupCompileUnits - Set up the unique vector of compile units.
833   ///
834   void SetupCompileUnits(Module &M);
835
836   /// getCompileUnits - Return a vector of debug compile units.
837   ///
838   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
839   
840   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
841   /// named GlobalVariable.
842   std::vector<GlobalVariable*>
843   getGlobalVariablesUsing(Module &M, const std::string &RootName);
844
845   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
846   ///
847   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
848     T Desc;
849     std::vector<GlobalVariable *> Globals =
850                              getGlobalVariablesUsing(M, Desc.getAnchorString());
851     std::vector<T *> AnchoredDescs;
852     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
853       GlobalVariable *GV = Globals[i];
854       // FIXME - Tag check only necessary for bring up (changed tag values.)
855       unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
856       if (Tag == Desc.getTag()) {
857         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
858       }
859     }
860
861     return AnchoredDescs;
862   }
863
864 }; // End class MachineDebugInfo
865
866 } // End llvm namespace
867
868 #endif