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