Move some methods out of MachineInstr into MachineOperand
[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 MachineFunction;
51 class MachineMove;
52 class Module;
53 class PointerType;
54 class StructType;
55
56 //===----------------------------------------------------------------------===//
57 // Debug info constants.
58
59 enum {
60   LLVMDebugVersion = 3                  // Current version of debug information.
61 };
62
63 //===----------------------------------------------------------------------===//
64 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
65 /// the supplied DebugInfoDesc.
66 class DIVisitor {
67 public:
68   DIVisitor() {}
69   virtual ~DIVisitor() {}
70
71   /// ApplyToFields - Target the visitor to each field of the debug information
72   /// descriptor.
73   void ApplyToFields(DebugInfoDesc *DD);
74   
75   /// Apply - Subclasses override each of these methods to perform the
76   /// appropriate action for the type of field.
77   virtual void Apply(int &Field) = 0;
78   virtual void Apply(unsigned &Field) = 0;
79   virtual void Apply(int64_t &Field) = 0;
80   virtual void Apply(uint64_t &Field) = 0;
81   virtual void Apply(bool &Field) = 0;
82   virtual void Apply(std::string &Field) = 0;
83   virtual void Apply(DebugInfoDesc *&Field) = 0;
84   virtual void Apply(GlobalVariable *&Field) = 0;
85   virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
86 };
87
88 //===----------------------------------------------------------------------===//
89 /// DebugInfoDesc - This class is the base class for debug info descriptors.
90 ///
91 class DebugInfoDesc {
92 private:
93   unsigned Tag;                         // Content indicator.  Dwarf values are
94                                         // used but that does not limit use to
95                                         // Dwarf writers.
96   
97 protected:
98   DebugInfoDesc(unsigned T) : Tag(T) {}
99   
100 public:
101   virtual ~DebugInfoDesc() {}
102
103   // Accessors
104   unsigned getTag()          const { return Tag; }
105   
106   /// TagFromGlobal - Returns the Tag number from a debug info descriptor
107   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
108   static unsigned TagFromGlobal(GlobalVariable *GV);
109
110   /// DescFactory - Create an instance of debug info descriptor based on Tag.
111   /// Return NULL if not a recognized Tag.
112   static DebugInfoDesc *DescFactory(unsigned Tag);
113   
114   /// getLinkage - get linkage appropriate for this type of descriptor.
115   ///
116   virtual GlobalValue::LinkageTypes getLinkage() const;
117     
118   //===--------------------------------------------------------------------===//
119   // Subclasses should supply the following static methods.
120   
121   // Implement isa/cast/dyncast.
122   static bool classof(const DebugInfoDesc *) { return true; }
123   
124   //===--------------------------------------------------------------------===//
125   // Subclasses should supply the following virtual methods.
126   
127   /// ApplyToFields - Target the vistor to the fields of the descriptor.
128   ///
129   virtual void ApplyToFields(DIVisitor *Visitor);
130
131   /// getDescString - Return a string used to compose global names and labels.
132   ///
133   virtual const char *getDescString() const = 0;
134   
135   /// getTypeString - Return a string used to label this descriptor's type.
136   ///
137   virtual const char *getTypeString() const = 0;
138   
139 #ifndef NDEBUG
140   virtual void dump() = 0;
141 #endif
142 };
143
144 //===----------------------------------------------------------------------===//
145 /// AnchorDesc - Descriptors of this class act as markers for identifying
146 /// descriptors of certain groups.
147 class AnchoredDesc;
148 class AnchorDesc : public DebugInfoDesc {
149 private:
150   unsigned AnchorTag;                   // Tag number of descriptors anchored
151                                         // by this object.
152   
153 public:
154   AnchorDesc();
155   AnchorDesc(AnchoredDesc *D);
156   
157   // Accessors
158   unsigned getAnchorTag() const { return AnchorTag; }
159
160   // Implement isa/cast/dyncast.
161   static bool classof(const AnchorDesc *) { return true; }
162   static bool classof(const DebugInfoDesc *D);
163
164   /// getLinkage - get linkage appropriate for this type of descriptor.
165   ///
166   virtual GlobalValue::LinkageTypes getLinkage() const;
167
168   /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
169   ///
170   virtual void ApplyToFields(DIVisitor *Visitor);
171
172   /// getDescString - Return a string used to compose global names and labels.
173   ///
174   virtual const char *getDescString() const;
175     
176   /// getTypeString - Return a string used to label this descriptor's type.
177   ///
178   virtual const char *getTypeString() const;
179     
180 #ifndef NDEBUG
181   virtual void dump();
182 #endif
183 };
184
185 //===----------------------------------------------------------------------===//
186 /// AnchoredDesc - This class manages anchors for a variety of top level
187 /// descriptors.
188 class AnchoredDesc : public DebugInfoDesc {
189 private:  
190   AnchorDesc *Anchor;                   // Anchor for all descriptors of the
191                                         // same type.
192
193 protected:
194
195   AnchoredDesc(unsigned T);
196
197 public:  
198   // Accessors.
199   AnchorDesc *getAnchor() const { return Anchor; }
200   void setAnchor(AnchorDesc *A) { Anchor = A; }
201
202   //===--------------------------------------------------------------------===//
203   // Subclasses should supply the following virtual methods.
204   
205   /// getAnchorString - Return a string used to label descriptor's anchor.
206   ///
207   virtual const char *getAnchorString() const = 0;
208     
209   /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
210   ///
211   virtual void ApplyToFields(DIVisitor *Visitor);
212 };
213
214 //===----------------------------------------------------------------------===//
215 /// CompileUnitDesc - This class packages debug information associated with a 
216 /// source/header file.
217 class CompileUnitDesc : public AnchoredDesc {
218 private:  
219   unsigned DebugVersion;                // LLVM debug version when produced.
220   unsigned Language;                    // Language number (ex. DW_LANG_C89.)
221   std::string FileName;                 // Source file name.
222   std::string Directory;                // Source file directory.
223   std::string Producer;                 // Compiler string.
224   
225 public:
226   CompileUnitDesc();
227   
228   
229   // Accessors
230   unsigned getDebugVersion()              const { return DebugVersion; }
231   unsigned getLanguage()                  const { return Language; }
232   const std::string &getFileName()        const { return FileName; }
233   const std::string &getDirectory()       const { return Directory; }
234   const std::string &getProducer()        const { return Producer; }
235   void setLanguage(unsigned L)                  { Language = L; }
236   void setFileName(const std::string &FN)       { FileName = FN; }
237   void setDirectory(const std::string &D)       { Directory = D; }
238   void setProducer(const std::string &P)        { Producer = P; }
239   
240   // FIXME - Need translation unit getter/setter.
241
242   // Implement isa/cast/dyncast.
243   static bool classof(const CompileUnitDesc *) { return true; }
244   static bool classof(const DebugInfoDesc *D);
245   
246   /// DebugVersionFromGlobal - Returns the version number from a compile unit
247   /// GlobalVariable.  Return DIIValid if operand is not an unsigned int.
248   static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
249   
250   /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
251   ///
252   virtual void ApplyToFields(DIVisitor *Visitor);
253
254   /// getDescString - Return a string used to compose global names and labels.
255   ///
256   virtual const char *getDescString() const;
257     
258   /// getTypeString - Return a string used to label this descriptor's type.
259   ///
260   virtual const char *getTypeString() const;
261   
262   /// getAnchorString - Return a string used to label this descriptor's anchor.
263   ///
264   static const char *AnchorString;
265   virtual const char *getAnchorString() const;
266     
267 #ifndef NDEBUG
268   virtual void dump();
269 #endif
270 };
271
272 //===----------------------------------------------------------------------===//
273 /// TypeDesc - This class packages debug information associated with a type.
274 ///
275 class TypeDesc : public DebugInfoDesc {
276 private:
277   DebugInfoDesc *Context;               // Context debug descriptor.
278   std::string Name;                     // Type name (may be empty.)
279   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
280   unsigned Line;                        // Defined line# (may be zero.)
281   uint64_t Size;                        // Type bit size (may be zero.)
282   uint64_t Align;                       // Type bit alignment (may be zero.)
283   uint64_t Offset;                      // Type bit offset (may be zero.)
284
285 public:
286   TypeDesc(unsigned T);
287
288   // Accessors
289   DebugInfoDesc *getContext()                const { return Context; }
290   const std::string &getName()               const { return Name; }
291   CompileUnitDesc *getFile()                 const { return File; }
292   unsigned getLine()                         const { return Line; }
293   uint64_t getSize()                         const { return Size; }
294   uint64_t getAlign()                        const { return Align; }
295   uint64_t getOffset()                       const { return Offset; }
296   void setContext(DebugInfoDesc *C)                { Context = C; }
297   void setName(const std::string &N)               { Name = N; }
298   void setFile(CompileUnitDesc *U)                 { File = U; }
299   void setLine(unsigned L)                         { Line = L; }
300   void setSize(uint64_t S)                         { Size = S; }
301   void setAlign(uint64_t A)                        { Align = A; }
302   void setOffset(uint64_t O)                       { Offset = O; }
303   
304   /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
305   ///
306   virtual void ApplyToFields(DIVisitor *Visitor);
307
308   /// getDescString - Return a string used to compose global names and labels.
309   ///
310   virtual const char *getDescString() const;
311
312   /// getTypeString - Return a string used to label this descriptor's type.
313   ///
314   virtual const char *getTypeString() const;
315   
316 #ifndef NDEBUG
317   virtual void dump();
318 #endif
319 };
320
321 //===----------------------------------------------------------------------===//
322 /// BasicTypeDesc - This class packages debug information associated with a
323 /// basic type (eg. int, bool, double.)
324 class BasicTypeDesc : public TypeDesc {
325 private:
326   unsigned Encoding;                    // Type encoding.
327   
328 public:
329   BasicTypeDesc();
330   
331   // Accessors
332   unsigned getEncoding()                     const { return Encoding; }
333   void setEncoding(unsigned E)                     { Encoding = E; }
334
335   // Implement isa/cast/dyncast.
336   static bool classof(const BasicTypeDesc *) { return true; }
337   static bool classof(const DebugInfoDesc *D);
338   
339   /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
340   ///
341   virtual void ApplyToFields(DIVisitor *Visitor);
342
343   /// getDescString - Return a string used to compose global names and labels.
344   ///
345   virtual const char *getDescString() const;
346
347   /// getTypeString - Return a string used to label this descriptor's type.
348   ///
349   virtual const char *getTypeString() const;
350
351 #ifndef NDEBUG
352   virtual void dump();
353 #endif
354 };
355
356
357 //===----------------------------------------------------------------------===//
358 /// DerivedTypeDesc - This class packages debug information associated with a
359 /// derived types (eg., typedef, pointer, reference.)
360 class DerivedTypeDesc : public TypeDesc {
361 private:
362   TypeDesc *FromType;                   // Type derived from.
363
364 public:
365   DerivedTypeDesc(unsigned T);
366   
367   // Accessors
368   TypeDesc *getFromType()                    const { return FromType; }
369   void setFromType(TypeDesc *F)                    { FromType = F; }
370
371   // Implement isa/cast/dyncast.
372   static bool classof(const DerivedTypeDesc *) { return true; }
373   static bool classof(const DebugInfoDesc *D);
374   
375   /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
376   ///
377   virtual void ApplyToFields(DIVisitor *Visitor);
378
379   /// getDescString - Return a string used to compose global names and labels.
380   ///
381   virtual const char *getDescString() const;
382
383   /// getTypeString - Return a string used to label this descriptor's type.
384   ///
385   virtual const char *getTypeString() const;
386
387 #ifndef NDEBUG
388   virtual void dump();
389 #endif
390 };
391
392 //===----------------------------------------------------------------------===//
393 /// CompositeTypeDesc - This class packages debug information associated with a
394 /// array/struct types (eg., arrays, struct, union, enums.)
395 class CompositeTypeDesc : public DerivedTypeDesc {
396 private:
397   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
398
399 public:
400   CompositeTypeDesc(unsigned T);
401   
402   // Accessors
403   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
404
405   // Implement isa/cast/dyncast.
406   static bool classof(const CompositeTypeDesc *) { return true; }
407   static bool classof(const DebugInfoDesc *D);
408   
409   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
410   ///
411   virtual void ApplyToFields(DIVisitor *Visitor);
412
413   /// getDescString - Return a string used to compose global names and labels.
414   ///
415   virtual const char *getDescString() const;
416
417   /// getTypeString - Return a string used to label this descriptor's type.
418   ///
419   virtual const char *getTypeString() const;
420
421 #ifndef NDEBUG
422   virtual void dump();
423 #endif
424 };
425
426 //===----------------------------------------------------------------------===//
427 /// SubrangeDesc - This class packages debug information associated with integer
428 /// value ranges.
429 class SubrangeDesc : public DebugInfoDesc {
430 private:
431   int64_t Lo;                           // Low value of range.
432   int64_t Hi;                           // High value of range.
433
434 public:
435   SubrangeDesc();
436   
437   // Accessors
438   int64_t getLo()                            const { return Lo; }
439   int64_t getHi()                            const { return Hi; }
440   void setLo(int64_t L)                            { Lo = L; }
441   void setHi(int64_t H)                            { Hi = H; }
442
443   // Implement isa/cast/dyncast.
444   static bool classof(const SubrangeDesc *) { return true; }
445   static bool classof(const DebugInfoDesc *D);
446   
447   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
448   ///
449   virtual void ApplyToFields(DIVisitor *Visitor);
450
451   /// getDescString - Return a string used to compose global names and labels.
452   ///
453   virtual const char *getDescString() const;
454     
455   /// getTypeString - Return a string used to label this descriptor's type.
456   ///
457   virtual const char *getTypeString() const;
458
459 #ifndef NDEBUG
460   virtual void dump();
461 #endif
462 };
463
464 //===----------------------------------------------------------------------===//
465 /// EnumeratorDesc - This class packages debug information associated with
466 /// named integer constants.
467 class EnumeratorDesc : public DebugInfoDesc {
468 private:
469   std::string Name;                     // Enumerator name.
470   int64_t Value;                        // Enumerator value.
471
472 public:
473   EnumeratorDesc();
474   
475   // Accessors
476   const std::string &getName()               const { return Name; }
477   int64_t getValue()                         const { return Value; }
478   void setName(const std::string &N)               { Name = N; }
479   void setValue(int64_t V)                         { Value = V; }
480
481   // Implement isa/cast/dyncast.
482   static bool classof(const EnumeratorDesc *) { return true; }
483   static bool classof(const DebugInfoDesc *D);
484   
485   /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
486   ///
487   virtual void ApplyToFields(DIVisitor *Visitor);
488
489   /// getDescString - Return a string used to compose global names and labels.
490   ///
491   virtual const char *getDescString() const;
492     
493   /// getTypeString - Return a string used to label this descriptor's type.
494   ///
495   virtual const char *getTypeString() const;
496
497 #ifndef NDEBUG
498   virtual void dump();
499 #endif
500 };
501
502 //===----------------------------------------------------------------------===//
503 /// VariableDesc - This class packages debug information associated with a
504 /// subprogram variable.
505 ///
506 class VariableDesc : public DebugInfoDesc {
507 private:
508   DebugInfoDesc *Context;               // Context debug descriptor.
509   std::string Name;                     // Type name (may be empty.)
510   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
511   unsigned Line;                        // Defined line# (may be zero.)
512   TypeDesc *TyDesc;                     // Type of variable.
513
514 public:
515   VariableDesc(unsigned T);
516
517   // Accessors
518   DebugInfoDesc *getContext()                const { return Context; }
519   const std::string &getName()               const { return Name; }
520   CompileUnitDesc *getFile()                 const { return File; }
521   unsigned getLine()                         const { return Line; }
522   TypeDesc *getType()                        const { return TyDesc; }
523   void setContext(DebugInfoDesc *C)                { Context = C; }
524   void setName(const std::string &N)               { Name = N; }
525   void setFile(CompileUnitDesc *U)                 { File = U; }
526   void setLine(unsigned L)                         { Line = L; }
527   void setType(TypeDesc *T)                        { TyDesc = T; }
528   
529   // Implement isa/cast/dyncast.
530   static bool classof(const VariableDesc *) { return true; }
531   static bool classof(const DebugInfoDesc *D);
532   
533   /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
534   ///
535   virtual void ApplyToFields(DIVisitor *Visitor);
536
537   /// getDescString - Return a string used to compose global names and labels.
538   ///
539   virtual const char *getDescString() const;
540
541   /// getTypeString - Return a string used to label this descriptor's type.
542   ///
543   virtual const char *getTypeString() const;
544   
545 #ifndef NDEBUG
546   virtual void dump();
547 #endif
548 };
549
550 //===----------------------------------------------------------------------===//
551 /// GlobalDesc - This class is the base descriptor for global functions and
552 /// variables.
553 class GlobalDesc : public AnchoredDesc {
554 private:
555   DebugInfoDesc *Context;               // Context debug descriptor.
556   std::string Name;                     // Global name.
557   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
558   unsigned Line;                        // Defined line# (may be zero.)
559   TypeDesc *TyDesc;                     // Type debug descriptor.
560   bool IsStatic;                        // Is the global a static.
561   bool IsDefinition;                    // Is the global defined in context.
562   
563 protected:
564   GlobalDesc(unsigned T);
565
566 public:
567   // Accessors
568   DebugInfoDesc *getContext()                const { return Context; }
569   const std::string &getName()               const { return Name; }
570   CompileUnitDesc *getFile()                 const { return File; }
571   unsigned getLine()                         const { return Line; }
572   TypeDesc *getType()                        const { return TyDesc; }
573   bool isStatic()                            const { return IsStatic; }
574   bool isDefinition()                        const { return IsDefinition; }
575   void setContext(DebugInfoDesc *C)                { Context = C; }
576   void setName(const std::string &N)               { Name = N; }
577   void setFile(CompileUnitDesc *U)                 { File = U; }
578   void setLine(unsigned L)                         { Line = L; }
579   void setType(TypeDesc *T)                        { TyDesc = T; }
580   void setIsStatic(bool IS)                        { IsStatic = IS; }
581   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
582
583   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
584   ///
585   virtual void ApplyToFields(DIVisitor *Visitor);
586 };
587
588 //===----------------------------------------------------------------------===//
589 /// GlobalVariableDesc - This class packages debug information associated with a
590 /// GlobalVariable.
591 class GlobalVariableDesc : public GlobalDesc {
592 private:
593   GlobalVariable *Global;               // llvm global.
594   
595 public:
596   GlobalVariableDesc();
597
598   // Accessors.
599   GlobalVariable *getGlobalVariable()        const { return Global; }
600   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
601  
602   // Implement isa/cast/dyncast.
603   static bool classof(const GlobalVariableDesc *) { return true; }
604   static bool classof(const DebugInfoDesc *D);
605   
606   /// ApplyToFields - Target the visitor to the fields of the
607   /// GlobalVariableDesc.
608   virtual void ApplyToFields(DIVisitor *Visitor);
609
610   /// getDescString - Return a string used to compose global names and labels.
611   ///
612   virtual const char *getDescString() const;
613
614   /// getTypeString - Return a string used to label this descriptor's type.
615   ///
616   virtual const char *getTypeString() const;
617   
618   /// getAnchorString - Return a string used to label this descriptor's anchor.
619   ///
620   static const char *AnchorString;
621   virtual const char *getAnchorString() const;
622     
623 #ifndef NDEBUG
624   virtual void dump();
625 #endif
626 };
627
628 //===----------------------------------------------------------------------===//
629 /// SubprogramDesc - This class packages debug information associated with a
630 /// subprogram/function.
631 class SubprogramDesc : public GlobalDesc {
632 private:
633   
634 public:
635   SubprogramDesc();
636   
637   // Accessors
638   
639   // Implement isa/cast/dyncast.
640   static bool classof(const SubprogramDesc *) { return true; }
641   static bool classof(const DebugInfoDesc *D);
642   
643   /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
644   ///
645   virtual void ApplyToFields(DIVisitor *Visitor);
646
647   /// getDescString - Return a string used to compose global names and labels.
648   ///
649   virtual const char *getDescString() const;
650
651   /// getTypeString - Return a string used to label this descriptor's type.
652   ///
653   virtual const char *getTypeString() const;
654   
655   /// getAnchorString - Return a string used to label this descriptor's anchor.
656   ///
657   static const char *AnchorString;
658   virtual const char *getAnchorString() const;
659     
660 #ifndef NDEBUG
661   virtual void dump();
662 #endif
663 };
664
665 //===----------------------------------------------------------------------===//
666 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
667 ///
668 class BlockDesc : public DebugInfoDesc {
669 private:
670   DebugInfoDesc *Context;               // Context debug descriptor.
671
672 public:
673   BlockDesc();
674   
675   // Accessors
676   DebugInfoDesc *getContext()                const { return Context; }
677   void setContext(DebugInfoDesc *C)                { Context = C; }
678   
679   // Implement isa/cast/dyncast.
680   static bool classof(const BlockDesc *) { return true; }
681   static bool classof(const DebugInfoDesc *D);
682   
683   /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
684   ///
685   virtual void ApplyToFields(DIVisitor *Visitor);
686
687   /// getDescString - Return a string used to compose global names and labels.
688   ///
689   virtual const char *getDescString() const;
690
691   /// getTypeString - Return a string used to label this descriptor's type.
692   ///
693   virtual const char *getTypeString() const;
694     
695 #ifndef NDEBUG
696   virtual void dump();
697 #endif
698 };
699
700 //===----------------------------------------------------------------------===//
701 /// DIDeserializer - This class is responsible for casting GlobalVariables
702 /// into DebugInfoDesc objects.
703 class DIDeserializer {
704 private:
705   unsigned DebugVersion;                // Version of debug information in use.
706   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
707                                         // Previously defined gloabls.
708   
709 public:
710   DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
711   ~DIDeserializer() {}
712   
713   // Accessors
714   unsigned getDebugVersion() const { return DebugVersion; }
715   
716   /// Deserialize - Reconstitute a GlobalVariable into it's component
717   /// DebugInfoDesc objects.
718   DebugInfoDesc *Deserialize(Value *V);
719   DebugInfoDesc *Deserialize(GlobalVariable *GV);
720 };
721
722 //===----------------------------------------------------------------------===//
723 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
724 /// into GlobalVariables.
725 class DISerializer {
726 private:
727   Module *M;                            // Definition space module.
728   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
729   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
730   std::map<unsigned, StructType *> TagTypes;
731                                         // Types per Tag.  Created lazily.
732   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
733                                         // Previously defined descriptors.
734   std::map<const std::string, Constant *> StringCache;
735                                         // Previously defined strings.
736                                           
737 public:
738   DISerializer()
739   : M(NULL)
740   , StrPtrTy(NULL)
741   , EmptyStructPtrTy(NULL)
742   , TagTypes()
743   , DescGlobals()
744   , StringCache()
745   {}
746   ~DISerializer() {}
747   
748   // Accessors
749   Module *getModule()        const { return M; };
750   void setModule(Module *module)  { M = module; }
751
752   /// getStrPtrType - Return a "sbyte *" type.
753   ///
754   const PointerType *getStrPtrType();
755   
756   /// getEmptyStructPtrType - Return a "{ }*" type.
757   ///
758   const PointerType *getEmptyStructPtrType();
759   
760   /// getTagType - Return the type describing the specified descriptor (via
761   /// tag.)
762   const StructType *getTagType(DebugInfoDesc *DD);
763   
764   /// getString - Construct the string as constant string global.
765   ///
766   Constant *getString(const std::string &String);
767   
768   /// Serialize - Recursively cast the specified descriptor into a
769   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
770   GlobalVariable *Serialize(DebugInfoDesc *DD);
771 };
772
773 //===----------------------------------------------------------------------===//
774 /// DIVerifier - This class is responsible for verifying the given network of
775 /// GlobalVariables are valid as DebugInfoDesc objects.
776 class DIVerifier {
777 private:
778   enum {
779     Unknown = 0,
780     Invalid,
781     Valid
782   };
783   unsigned DebugVersion;                // Version of debug information in use.
784   std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
785   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
786   
787 public:
788   DIVerifier()
789   : DebugVersion(LLVMDebugVersion)
790   , Validity()
791   , Counts()
792   {}
793   ~DIVerifier() {}
794   
795   /// Verify - Return true if the GlobalVariable appears to be a valid
796   /// serialization of a DebugInfoDesc.
797   bool Verify(Value *V);
798   bool Verify(GlobalVariable *GV);
799 };
800
801 //===----------------------------------------------------------------------===//
802 /// SourceLineInfo - This class is used to record source line correspondence.
803 ///
804 class SourceLineInfo {
805 private:
806   unsigned Line;                        // Source line number.
807   unsigned Column;                      // Source column.
808   unsigned SourceID;                    // Source ID number.
809   unsigned LabelID;                     // Label in code ID number.
810
811 public:
812   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
813   : Line(L), Column(C), SourceID(S), LabelID(I) {}
814   
815   // Accessors
816   unsigned getLine()     const { return Line; }
817   unsigned getColumn()   const { return Column; }
818   unsigned getSourceID() const { return SourceID; }
819   unsigned getLabelID()  const { return LabelID; }
820 };
821
822 //===----------------------------------------------------------------------===//
823 /// SourceFileInfo - This class is used to track source information.
824 ///
825 class SourceFileInfo {
826 private:
827   unsigned DirectoryID;                 // Directory ID number.
828   std::string Name;                     // File name (not including directory.)
829   
830 public:
831   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
832             
833   // Accessors
834   unsigned getDirectoryID()    const { return DirectoryID; }
835   const std::string &getName() const { return Name; }
836
837   /// operator== - Used by UniqueVector to locate entry.
838   ///
839   bool operator==(const SourceFileInfo &SI) const {
840     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
841   }
842
843   /// operator< - Used by UniqueVector to locate entry.
844   ///
845   bool operator<(const SourceFileInfo &SI) const {
846     return getDirectoryID() < SI.getDirectoryID() ||
847           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
848   }
849 };
850
851 //===----------------------------------------------------------------------===//
852 /// DebugVariable - This class is used to track local variable information.
853 ///
854 class DebugVariable {
855 private:
856   VariableDesc *Desc;                   // Variable Descriptor.
857   unsigned FrameIndex;                  // Variable frame index.
858
859 public:
860   DebugVariable(VariableDesc *D, unsigned I)
861   : Desc(D)
862   , FrameIndex(I)
863   {}
864   
865   // Accessors.
866   VariableDesc *getDesc()  const { return Desc; }
867   unsigned getFrameIndex() const { return FrameIndex; }
868 };
869
870 //===----------------------------------------------------------------------===//
871 /// DebugScope - This class is used to track scope information.
872 ///
873 class DebugScope {
874 private:
875   DebugScope *Parent;                   // Parent to this scope.
876   DebugInfoDesc *Desc;                  // Debug info descriptor for scope.
877                                         // Either subprogram or block.
878   unsigned StartLabelID;                // Label ID of the beginning of scope.
879   unsigned EndLabelID;                  // Label ID of the end of scope.
880   std::vector<DebugScope *> Scopes;     // Scopes defined in scope.
881   std::vector<DebugVariable *> Variables;// Variables declared in scope.
882   
883 public:
884   DebugScope(DebugScope *P, DebugInfoDesc *D)
885   : Parent(P)
886   , Desc(D)
887   , StartLabelID(0)
888   , EndLabelID(0)
889   , Scopes()
890   , Variables()
891   {}
892   ~DebugScope();
893   
894   // Accessors.
895   DebugScope *getParent()        const { return Parent; }
896   DebugInfoDesc *getDesc()       const { return Desc; }
897   unsigned getStartLabelID()     const { return StartLabelID; }
898   unsigned getEndLabelID()       const { return EndLabelID; }
899   std::vector<DebugScope *> &getScopes() { return Scopes; }
900   std::vector<DebugVariable *> &getVariables() { return Variables; }
901   void setStartLabelID(unsigned S) { StartLabelID = S; }
902   void setEndLabelID(unsigned E)   { EndLabelID = E; }
903   
904   /// AddScope - Add a scope to the scope.
905   ///
906   void AddScope(DebugScope *S) { Scopes.push_back(S); }
907   
908   /// AddVariable - Add a variable to the scope.
909   ///
910   void AddVariable(DebugVariable *V) { Variables.push_back(V); }
911 };
912
913 //===----------------------------------------------------------------------===//
914 /// MachineDebugInfo - This class contains debug information specific to a
915 /// module.  Queries can be made by different debugging schemes and reformated
916 /// for specific use.
917 ///
918 class MachineDebugInfo : public ImmutablePass {
919 private:
920   // Use the same deserializer/verifier for the module.
921   DIDeserializer DR;
922   DIVerifier VR;
923
924   // CompileUnits - Uniquing vector for compile units.
925   UniqueVector<CompileUnitDesc *> CompileUnits;
926   
927   // Directories - Uniquing vector for directories.
928   UniqueVector<std::string> Directories;
929                                          
930   // SourceFiles - Uniquing vector for source files.
931   UniqueVector<SourceFileInfo> SourceFiles;
932
933   // Lines - List of of source line correspondence.
934   std::vector<SourceLineInfo *> Lines;
935   
936   // LabelID - Current number assigned to unique label numbers.
937   unsigned LabelID;
938   
939   // ScopeMap - Tracks the scopes in the current function.
940   std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
941   
942   // RootScope - Top level scope for the current function.
943   //
944   DebugScope *RootScope;
945   
946   // FrameMoves - List of moves done by a function's prolog.  Used to construct
947   // frame maps by debug consumers.
948   std::vector<MachineMove *> FrameMoves;
949
950 public:
951   MachineDebugInfo();
952   ~MachineDebugInfo();
953   
954   /// doInitialization - Initialize the debug state for a new module.
955   ///
956   bool doInitialization();
957   
958   /// doFinalization - Tear down the debug state after completion of a module.
959   ///
960   bool doFinalization();
961   
962   /// BeginFunction - Begin gathering function debug information.
963   ///
964   void BeginFunction(MachineFunction *MF);
965   
966   /// EndFunction - Discard function debug information.
967   ///
968   void EndFunction();
969
970   /// getDescFor - Convert a Value to a debug information descriptor.
971   ///
972   // FIXME - use new Value type when available.
973   DebugInfoDesc *getDescFor(Value *V);
974   
975   /// Verify - Verify that a Value is debug information descriptor.
976   ///
977   bool Verify(Value *V);
978   
979   /// AnalyzeModule - Scan the module for global debug information.
980   ///
981   void AnalyzeModule(Module &M);
982   
983   /// hasInfo - Returns true if valid debug info is present.
984   ///
985   bool hasInfo() const { return !CompileUnits.empty(); }
986   
987   /// NextLabelID - Return the next unique label id.
988   ///
989   unsigned NextLabelID() { return ++LabelID; }
990   
991   /// RecordLabel - Records location information and associates it with a
992   /// debug label.  Returns a unique label ID used to generate a label and 
993   /// provide correspondence to the source line list.
994   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source);
995   
996   /// RecordSource - Register a source file with debug info. Returns an source
997   /// ID.
998   unsigned RecordSource(const std::string &Directory,
999                         const std::string &Source);
1000   unsigned RecordSource(const CompileUnitDesc *CompileUnit);
1001   
1002   /// getDirectories - Return the UniqueVector of std::string representing
1003   /// directories.
1004   const UniqueVector<std::string> &getDirectories() const {
1005     return Directories;
1006   }
1007   
1008   /// getSourceFiles - Return the UniqueVector of source files. 
1009   ///
1010   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
1011     return SourceFiles;
1012   }
1013   
1014   /// getSourceLines - Return a vector of source lines.  Vector index + 1
1015   /// equals label ID.
1016   const std::vector<SourceLineInfo *> &getSourceLines() const {
1017     return Lines;
1018   }
1019   
1020   /// SetupCompileUnits - Set up the unique vector of compile units.
1021   ///
1022   void SetupCompileUnits(Module &M);
1023
1024   /// getCompileUnits - Return a vector of debug compile units.
1025   ///
1026   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
1027   
1028   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1029   /// named GlobalVariable.
1030   std::vector<GlobalVariable*>
1031   getGlobalVariablesUsing(Module &M, const std::string &RootName);
1032
1033   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
1034   ///
1035   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
1036     T Desc;
1037     std::vector<GlobalVariable *> Globals =
1038                              getGlobalVariablesUsing(M, Desc.getAnchorString());
1039     std::vector<T *> AnchoredDescs;
1040     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
1041       GlobalVariable *GV = Globals[i];
1042       unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1043       
1044       if (isa<CompileUnitDesc>(&Desc)) {
1045         unsigned DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1046         // FIXME - In the short term, changes are too drastic to continue.
1047         if (DebugVersion != LLVMDebugVersion) break;
1048       }
1049       
1050       if (Tag == Desc.getTag()) {
1051         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
1052       }
1053     }
1054
1055     return AnchoredDescs;
1056   }
1057   
1058   /// RecordRegionStart - Indicate the start of a region.
1059   ///
1060   unsigned RecordRegionStart(Value *V);
1061
1062   /// RecordRegionEnd - Indicate the end of a region.
1063   ///
1064   unsigned RecordRegionEnd(Value *V);
1065
1066   /// RecordVariable - Indicate the declaration of  a local variable.
1067   ///
1068   void RecordVariable(Value *V, unsigned FrameIndex);
1069   
1070   /// getRootScope - Return current functions root scope.
1071   ///
1072   DebugScope *getRootScope() { return RootScope; }
1073   
1074   /// getOrCreateScope - Returns the scope associated with the given descriptor.
1075   ///
1076   DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
1077   
1078   /// getFrameMoves - Returns a reference to a list of moves done in the current
1079   /// function's prologue.  Used to construct frame maps for debug comsumers.
1080   std::vector<MachineMove *> &getFrameMoves() { return FrameMoves; }
1081
1082 }; // End class MachineDebugInfo
1083
1084 } // End llvm namespace
1085
1086 #endif