Make sure only one copy of a filter is placed in the
[oota-llvm.git] / include / llvm / CodeGen / MachineModuleInfo.h
1 //===-- llvm/CodeGen/MachineModuleInfo.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 meta information for a module.  This information should be in a
11 // neutral form that can be used by different debugging and exception handling
12 // schemes.
13 //
14 // The organization of information is primarily clustered around the source
15 // compile units.  The main exception is source line correspondence where
16 // inlining may interleave code from various compile units.
17 //
18 // The following information can be retrieved from the MachineModuleInfo.
19 //
20 //  -- Source directories - Directories are uniqued based on their canonical
21 //     string and assigned a sequential numeric ID (base 1.)
22 //  -- Source files - Files are also uniqued based on their name and directory
23 //     ID.  A file ID is sequential number (base 1.)
24 //  -- Source line correspondence - A vector of file ID, line#, column# triples.
25 //     A DEBUG_LOCATION instruction is generated  by the DAG Legalizer
26 //     corresponding to each entry in the source line list.  This allows a debug
27 //     emitter to generate labels referenced by debug information tables.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H
32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H
33
34 #include "llvm/Support/Dwarf.h"
35 #include "llvm/Support/DataTypes.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/UniqueVector.h"
38 #include "llvm/GlobalValue.h"
39 #include "llvm/Pass.h"
40
41 namespace llvm {
42
43 //===----------------------------------------------------------------------===//
44 // Forward declarations.
45 class Constant;
46 class DebugInfoDesc;
47 class GlobalVariable;
48 class MachineBasicBlock;
49 class MachineFunction;
50 class MachineMove;
51 class Module;
52 class PointerType;
53 class StructType;
54
55 //===----------------------------------------------------------------------===//
56 // Debug info constants.
57
58 enum {
59   LLVMDebugVersion = (6 << 16),         // Current version of debug information.
60   LLVMDebugVersion5 = (5 << 16),        // Constant for version 5.
61   LLVMDebugVersion4 = (4 << 16),        // Constant for version 4.
62   LLVMDebugVersionMask = 0xffff0000     // Mask for version number.
63 };
64
65 //===----------------------------------------------------------------------===//
66 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
67 /// the supplied DebugInfoDesc.
68 class DIVisitor {
69 public:
70   DIVisitor() {}
71   virtual ~DIVisitor() {}
72
73   /// ApplyToFields - Target the visitor to each field of the debug information
74   /// descriptor.
75   void ApplyToFields(DebugInfoDesc *DD);
76   
77   /// Apply - Subclasses override each of these methods to perform the
78   /// appropriate action for the type of field.
79   virtual void Apply(int &Field) = 0;
80   virtual void Apply(unsigned &Field) = 0;
81   virtual void Apply(int64_t &Field) = 0;
82   virtual void Apply(uint64_t &Field) = 0;
83   virtual void Apply(bool &Field) = 0;
84   virtual void Apply(std::string &Field) = 0;
85   virtual void Apply(DebugInfoDesc *&Field) = 0;
86   virtual void Apply(GlobalVariable *&Field) = 0;
87   virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
88 };
89
90 //===----------------------------------------------------------------------===//
91 /// DebugInfoDesc - This class is the base class for debug info descriptors.
92 ///
93 class DebugInfoDesc {
94 private:
95   unsigned Tag;                         // Content indicator.  Dwarf values are
96                                         // used but that does not limit use to
97                                         // Dwarf writers.
98   
99 protected:
100   DebugInfoDesc(unsigned T) : Tag(T | LLVMDebugVersion) {}
101   
102 public:
103   virtual ~DebugInfoDesc() {}
104
105   // Accessors
106   unsigned getTag()          const { return Tag & ~LLVMDebugVersionMask; }
107   unsigned getVersion()      const { return Tag &  LLVMDebugVersionMask; }
108   void setTag(unsigned T)          { Tag = T | LLVMDebugVersion; }
109   
110   /// TagFromGlobal - Returns the tag number from a debug info descriptor
111   /// GlobalVariable.   Return DIIValid if operand is not an unsigned int. 
112   static unsigned TagFromGlobal(GlobalVariable *GV);
113
114   /// VersionFromGlobal - Returns the version number from a debug info
115   /// descriptor GlobalVariable.  Return DIIValid if operand is not an unsigned
116   /// int.
117   static unsigned VersionFromGlobal(GlobalVariable *GV);
118
119   /// DescFactory - Create an instance of debug info descriptor based on Tag.
120   /// Return NULL if not a recognized Tag.
121   static DebugInfoDesc *DescFactory(unsigned Tag);
122   
123   /// getLinkage - get linkage appropriate for this type of descriptor.
124   ///
125   virtual GlobalValue::LinkageTypes getLinkage() const;
126     
127   //===--------------------------------------------------------------------===//
128   // Subclasses should supply the following static methods.
129   
130   // Implement isa/cast/dyncast.
131   static bool classof(const DebugInfoDesc *) { return true; }
132   
133   //===--------------------------------------------------------------------===//
134   // Subclasses should supply the following virtual methods.
135   
136   /// ApplyToFields - Target the vistor to the fields of the descriptor.
137   ///
138   virtual void ApplyToFields(DIVisitor *Visitor);
139
140   /// getDescString - Return a string used to compose global names and labels.
141   ///
142   virtual const char *getDescString() const = 0;
143   
144   /// getTypeString - Return a string used to label this descriptor's type.
145   ///
146   virtual const char *getTypeString() const = 0;
147   
148 #ifndef NDEBUG
149   virtual void dump() = 0;
150 #endif
151 };
152
153 //===----------------------------------------------------------------------===//
154 /// AnchorDesc - Descriptors of this class act as markers for identifying
155 /// descriptors of certain groups.
156 class AnchoredDesc;
157 class AnchorDesc : public DebugInfoDesc {
158 private:
159   unsigned AnchorTag;                   // Tag number of descriptors anchored
160                                         // by this object.
161   
162 public:
163   AnchorDesc();
164   AnchorDesc(AnchoredDesc *D);
165   
166   // Accessors
167   unsigned getAnchorTag() const { return AnchorTag; }
168
169   // Implement isa/cast/dyncast.
170   static bool classof(const AnchorDesc *) { return true; }
171   static bool classof(const DebugInfoDesc *D);
172
173   /// getLinkage - get linkage appropriate for this type of descriptor.
174   ///
175   virtual GlobalValue::LinkageTypes getLinkage() const;
176
177   /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
178   ///
179   virtual void ApplyToFields(DIVisitor *Visitor);
180
181   /// getDescString - Return a string used to compose global names and labels.
182   ///
183   virtual const char *getDescString() const;
184     
185   /// getTypeString - Return a string used to label this descriptor's type.
186   ///
187   virtual const char *getTypeString() const;
188     
189 #ifndef NDEBUG
190   virtual void dump();
191 #endif
192 };
193
194 //===----------------------------------------------------------------------===//
195 /// AnchoredDesc - This class manages anchors for a variety of top level
196 /// descriptors.
197 class AnchoredDesc : public DebugInfoDesc {
198 private:  
199   DebugInfoDesc *Anchor;                // Anchor for all descriptors of the
200                                         // same type.
201
202 protected:
203
204   AnchoredDesc(unsigned T);
205
206 public:  
207   // Accessors.
208   AnchorDesc *getAnchor() const { return static_cast<AnchorDesc *>(Anchor); }
209   void setAnchor(AnchorDesc *A) { Anchor = static_cast<DebugInfoDesc *>(A); }
210
211   //===--------------------------------------------------------------------===//
212   // Subclasses should supply the following virtual methods.
213   
214   /// getAnchorString - Return a string used to label descriptor's anchor.
215   ///
216   virtual const char *getAnchorString() const = 0;
217     
218   /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
219   ///
220   virtual void ApplyToFields(DIVisitor *Visitor);
221 };
222
223 //===----------------------------------------------------------------------===//
224 /// CompileUnitDesc - This class packages debug information associated with a 
225 /// source/header file.
226 class CompileUnitDesc : public AnchoredDesc {
227 private:  
228   unsigned Language;                    // Language number (ex. DW_LANG_C89.)
229   std::string FileName;                 // Source file name.
230   std::string Directory;                // Source file directory.
231   std::string Producer;                 // Compiler string.
232   
233 public:
234   CompileUnitDesc();
235   
236   
237   // Accessors
238   unsigned getLanguage()                  const { return Language; }
239   const std::string &getFileName()        const { return FileName; }
240   const std::string &getDirectory()       const { return Directory; }
241   const std::string &getProducer()        const { return Producer; }
242   void setLanguage(unsigned L)                  { Language = L; }
243   void setFileName(const std::string &FN)       { FileName = FN; }
244   void setDirectory(const std::string &D)       { Directory = D; }
245   void setProducer(const std::string &P)        { Producer = P; }
246   
247   // FIXME - Need translation unit getter/setter.
248
249   // Implement isa/cast/dyncast.
250   static bool classof(const CompileUnitDesc *) { return true; }
251   static bool classof(const DebugInfoDesc *D);
252   
253   /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
254   ///
255   virtual void ApplyToFields(DIVisitor *Visitor);
256
257   /// getDescString - Return a string used to compose global names and labels.
258   ///
259   virtual const char *getDescString() const;
260     
261   /// getTypeString - Return a string used to label this descriptor's type.
262   ///
263   virtual const char *getTypeString() const;
264   
265   /// getAnchorString - Return a string used to label this descriptor's anchor.
266   ///
267   static const char *AnchorString;
268   virtual const char *getAnchorString() const;
269     
270 #ifndef NDEBUG
271   virtual void dump();
272 #endif
273 };
274
275 //===----------------------------------------------------------------------===//
276 /// TypeDesc - This class packages debug information associated with a type.
277 ///
278 class TypeDesc : public DebugInfoDesc {
279 private:
280   enum {
281     FlagPrivate    = 1 << 0,
282     FlagProtected  = 1 << 1
283   };
284   DebugInfoDesc *Context;               // Context debug descriptor.
285   std::string Name;                     // Type name (may be empty.)
286   DebugInfoDesc *File;                  // Defined compile unit (may be NULL.)
287   unsigned Line;                        // Defined line# (may be zero.)
288   uint64_t Size;                        // Type bit size (may be zero.)
289   uint64_t Align;                       // Type bit alignment (may be zero.)
290   uint64_t Offset;                      // Type bit offset (may be zero.)
291   unsigned Flags;                       // Miscellaneous flags.
292
293 public:
294   TypeDesc(unsigned T);
295
296   // Accessors
297   DebugInfoDesc *getContext()                const { return Context; }
298   const std::string &getName()               const { return Name; }
299   CompileUnitDesc *getFile() const {
300     return static_cast<CompileUnitDesc *>(File);
301   }
302   unsigned getLine()                         const { return Line; }
303   uint64_t getSize()                         const { return Size; }
304   uint64_t getAlign()                        const { return Align; }
305   uint64_t getOffset()                       const { return Offset; }
306   bool isPrivate() const {
307     return (Flags & FlagPrivate) != 0;
308   }
309   bool isProtected() const {
310     return (Flags & FlagProtected) != 0;
311   }
312   void setContext(DebugInfoDesc *C)                { Context = C; }
313   void setName(const std::string &N)               { Name = N; }
314   void setFile(CompileUnitDesc *U) {
315     File = static_cast<DebugInfoDesc *>(U);
316   }
317   void setLine(unsigned L)                         { Line = L; }
318   void setSize(uint64_t S)                         { Size = S; }
319   void setAlign(uint64_t A)                        { Align = A; }
320   void setOffset(uint64_t O)                       { Offset = O; }
321   void setIsPrivate()                              { Flags |= FlagPrivate; }
322   void setIsProtected()                            { Flags |= FlagProtected; }
323   
324   /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
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 /// BasicTypeDesc - This class packages debug information associated with a
343 /// basic type (eg. int, bool, double.)
344 class BasicTypeDesc : public TypeDesc {
345 private:
346   unsigned Encoding;                    // Type encoding.
347   
348 public:
349   BasicTypeDesc();
350   
351   // Accessors
352   unsigned getEncoding()                     const { return Encoding; }
353   void setEncoding(unsigned E)                     { Encoding = E; }
354
355   // Implement isa/cast/dyncast.
356   static bool classof(const BasicTypeDesc *) { return true; }
357   static bool classof(const DebugInfoDesc *D);
358   
359   /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
360   ///
361   virtual void ApplyToFields(DIVisitor *Visitor);
362
363   /// getDescString - Return a string used to compose global names and labels.
364   ///
365   virtual const char *getDescString() const;
366
367   /// getTypeString - Return a string used to label this descriptor's type.
368   ///
369   virtual const char *getTypeString() const;
370
371 #ifndef NDEBUG
372   virtual void dump();
373 #endif
374 };
375
376
377 //===----------------------------------------------------------------------===//
378 /// DerivedTypeDesc - This class packages debug information associated with a
379 /// derived types (eg., typedef, pointer, reference.)
380 class DerivedTypeDesc : public TypeDesc {
381 private:
382   DebugInfoDesc *FromType;              // Type derived from.
383
384 public:
385   DerivedTypeDesc(unsigned T);
386   
387   // Accessors
388   TypeDesc *getFromType() const {
389     return static_cast<TypeDesc *>(FromType);
390   }
391   void setFromType(TypeDesc *F) {
392     FromType = static_cast<DebugInfoDesc *>(F);
393   }
394
395   // Implement isa/cast/dyncast.
396   static bool classof(const DerivedTypeDesc *) { return true; }
397   static bool classof(const DebugInfoDesc *D);
398   
399   /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
400   ///
401   virtual void ApplyToFields(DIVisitor *Visitor);
402
403   /// getDescString - Return a string used to compose global names and labels.
404   ///
405   virtual const char *getDescString() const;
406
407   /// getTypeString - Return a string used to label this descriptor's type.
408   ///
409   virtual const char *getTypeString() const;
410
411 #ifndef NDEBUG
412   virtual void dump();
413 #endif
414 };
415
416 //===----------------------------------------------------------------------===//
417 /// CompositeTypeDesc - This class packages debug information associated with a
418 /// array/struct types (eg., arrays, struct, union, enums.)
419 class CompositeTypeDesc : public DerivedTypeDesc {
420 private:
421   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
422
423 public:
424   CompositeTypeDesc(unsigned T);
425   
426   // Accessors
427   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
428
429   // Implement isa/cast/dyncast.
430   static bool classof(const CompositeTypeDesc *) { return true; }
431   static bool classof(const DebugInfoDesc *D);
432   
433   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
434   ///
435   virtual void ApplyToFields(DIVisitor *Visitor);
436
437   /// getDescString - Return a string used to compose global names and labels.
438   ///
439   virtual const char *getDescString() const;
440
441   /// getTypeString - Return a string used to label this descriptor's type.
442   ///
443   virtual const char *getTypeString() const;
444
445 #ifndef NDEBUG
446   virtual void dump();
447 #endif
448 };
449
450 //===----------------------------------------------------------------------===//
451 /// SubrangeDesc - This class packages debug information associated with integer
452 /// value ranges.
453 class SubrangeDesc : public DebugInfoDesc {
454 private:
455   int64_t Lo;                           // Low value of range.
456   int64_t Hi;                           // High value of range.
457
458 public:
459   SubrangeDesc();
460   
461   // Accessors
462   int64_t getLo()                            const { return Lo; }
463   int64_t getHi()                            const { return Hi; }
464   void setLo(int64_t L)                            { Lo = L; }
465   void setHi(int64_t H)                            { Hi = H; }
466
467   // Implement isa/cast/dyncast.
468   static bool classof(const SubrangeDesc *) { return true; }
469   static bool classof(const DebugInfoDesc *D);
470   
471   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
472   ///
473   virtual void ApplyToFields(DIVisitor *Visitor);
474
475   /// getDescString - Return a string used to compose global names and labels.
476   ///
477   virtual const char *getDescString() const;
478     
479   /// getTypeString - Return a string used to label this descriptor's type.
480   ///
481   virtual const char *getTypeString() const;
482
483 #ifndef NDEBUG
484   virtual void dump();
485 #endif
486 };
487
488 //===----------------------------------------------------------------------===//
489 /// EnumeratorDesc - This class packages debug information associated with
490 /// named integer constants.
491 class EnumeratorDesc : public DebugInfoDesc {
492 private:
493   std::string Name;                     // Enumerator name.
494   int64_t Value;                        // Enumerator value.
495
496 public:
497   EnumeratorDesc();
498   
499   // Accessors
500   const std::string &getName()               const { return Name; }
501   int64_t getValue()                         const { return Value; }
502   void setName(const std::string &N)               { Name = N; }
503   void setValue(int64_t V)                         { Value = V; }
504
505   // Implement isa/cast/dyncast.
506   static bool classof(const EnumeratorDesc *) { return true; }
507   static bool classof(const DebugInfoDesc *D);
508   
509   /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
510   ///
511   virtual void ApplyToFields(DIVisitor *Visitor);
512
513   /// getDescString - Return a string used to compose global names and labels.
514   ///
515   virtual const char *getDescString() const;
516     
517   /// getTypeString - Return a string used to label this descriptor's type.
518   ///
519   virtual const char *getTypeString() const;
520
521 #ifndef NDEBUG
522   virtual void dump();
523 #endif
524 };
525
526 //===----------------------------------------------------------------------===//
527 /// VariableDesc - This class packages debug information associated with a
528 /// subprogram variable.
529 ///
530 class VariableDesc : public DebugInfoDesc {
531 private:
532   DebugInfoDesc *Context;               // Context debug descriptor.
533   std::string Name;                     // Type name (may be empty.)
534   DebugInfoDesc *File;                  // Defined compile unit (may be NULL.)
535   unsigned Line;                        // Defined line# (may be zero.)
536   DebugInfoDesc *TyDesc;                // Type of variable.
537
538 public:
539   VariableDesc(unsigned T);
540
541   // Accessors
542   DebugInfoDesc *getContext()                const { return Context; }
543   const std::string &getName()               const { return Name; }
544   CompileUnitDesc *getFile() const {
545     return static_cast<CompileUnitDesc *>(File);
546   }
547   unsigned getLine()                         const { return Line; }
548   TypeDesc *getType() const {
549     return static_cast<TypeDesc *>(TyDesc);
550   }
551   void setContext(DebugInfoDesc *C)                { Context = C; }
552   void setName(const std::string &N)               { Name = N; }
553   void setFile(CompileUnitDesc *U) {
554     File = static_cast<DebugInfoDesc *>(U);
555   }
556   void setLine(unsigned L)                         { Line = L; }
557   void setType(TypeDesc *T) {
558     TyDesc = static_cast<DebugInfoDesc *>(T);
559   }
560   
561   // Implement isa/cast/dyncast.
562   static bool classof(const VariableDesc *) { return true; }
563   static bool classof(const DebugInfoDesc *D);
564   
565   /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
566   ///
567   virtual void ApplyToFields(DIVisitor *Visitor);
568
569   /// getDescString - Return a string used to compose global names and labels.
570   ///
571   virtual const char *getDescString() const;
572
573   /// getTypeString - Return a string used to label this descriptor's type.
574   ///
575   virtual const char *getTypeString() const;
576   
577 #ifndef NDEBUG
578   virtual void dump();
579 #endif
580 };
581
582 //===----------------------------------------------------------------------===//
583 /// GlobalDesc - This class is the base descriptor for global functions and
584 /// variables.
585 class GlobalDesc : public AnchoredDesc {
586 private:
587   DebugInfoDesc *Context;               // Context debug descriptor.
588   std::string Name;                     // Global name.
589   std::string FullName;                 // Fully qualified name.
590   std::string LinkageName;              // Name for binding to MIPS linkage.
591   DebugInfoDesc *File;                  // Defined compile unit (may be NULL.)
592   unsigned Line;                        // Defined line# (may be zero.)
593   DebugInfoDesc *TyDesc;                // Type debug descriptor.
594   bool IsStatic;                        // Is the global a static.
595   bool IsDefinition;                    // Is the global defined in context.
596   
597 protected:
598   GlobalDesc(unsigned T);
599
600 public:
601   // Accessors
602   DebugInfoDesc *getContext()                const { return Context; }
603   const std::string &getName()               const { return Name; }
604   const std::string &getFullName()           const { return FullName; }
605   const std::string &getLinkageName()        const { return LinkageName; }
606   CompileUnitDesc *getFile() const {
607     return static_cast<CompileUnitDesc *>(File);
608   }
609   unsigned getLine()                         const { return Line; }
610   TypeDesc *getType() const {
611     return static_cast<TypeDesc *>(TyDesc);
612   }
613   bool isStatic()                            const { return IsStatic; }
614   bool isDefinition()                        const { return IsDefinition; }
615   void setContext(DebugInfoDesc *C)                { Context = C; }
616   void setName(const std::string &N)               { Name = N; }
617   void setFullName(const std::string &N)           { FullName = N; }
618   void setLinkageName(const std::string &N)        { LinkageName = N; }
619   void setFile(CompileUnitDesc *U) {
620     File = static_cast<DebugInfoDesc *>(U);
621   }
622   void setLine(unsigned L)                         { Line = L; }
623   void setType(TypeDesc *T) {
624     TyDesc = static_cast<DebugInfoDesc *>(T);
625   }
626   void setIsStatic(bool IS)                        { IsStatic = IS; }
627   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
628
629   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
630   ///
631   virtual void ApplyToFields(DIVisitor *Visitor);
632 };
633
634 //===----------------------------------------------------------------------===//
635 /// GlobalVariableDesc - This class packages debug information associated with a
636 /// GlobalVariable.
637 class GlobalVariableDesc : public GlobalDesc {
638 private:
639   GlobalVariable *Global;               // llvm global.
640   
641 public:
642   GlobalVariableDesc();
643
644   // Accessors.
645   GlobalVariable *getGlobalVariable()        const { return Global; }
646   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
647  
648   // Implement isa/cast/dyncast.
649   static bool classof(const GlobalVariableDesc *) { return true; }
650   static bool classof(const DebugInfoDesc *D);
651   
652   /// ApplyToFields - Target the visitor to the fields of the
653   /// GlobalVariableDesc.
654   virtual void ApplyToFields(DIVisitor *Visitor);
655
656   /// getDescString - Return a string used to compose global names and labels.
657   ///
658   virtual const char *getDescString() const;
659
660   /// getTypeString - Return a string used to label this descriptor's type.
661   ///
662   virtual const char *getTypeString() const;
663   
664   /// getAnchorString - Return a string used to label this descriptor's anchor.
665   ///
666   static const char *AnchorString;
667   virtual const char *getAnchorString() const;
668     
669 #ifndef NDEBUG
670   virtual void dump();
671 #endif
672 };
673
674 //===----------------------------------------------------------------------===//
675 /// SubprogramDesc - This class packages debug information associated with a
676 /// subprogram/function.
677 class SubprogramDesc : public GlobalDesc {
678 private:
679   
680 public:
681   SubprogramDesc();
682   
683   // Accessors
684   
685   // Implement isa/cast/dyncast.
686   static bool classof(const SubprogramDesc *) { return true; }
687   static bool classof(const DebugInfoDesc *D);
688   
689   /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
690   ///
691   virtual void ApplyToFields(DIVisitor *Visitor);
692
693   /// getDescString - Return a string used to compose global names and labels.
694   ///
695   virtual const char *getDescString() const;
696
697   /// getTypeString - Return a string used to label this descriptor's type.
698   ///
699   virtual const char *getTypeString() const;
700   
701   /// getAnchorString - Return a string used to label this descriptor's anchor.
702   ///
703   static const char *AnchorString;
704   virtual const char *getAnchorString() const;
705     
706 #ifndef NDEBUG
707   virtual void dump();
708 #endif
709 };
710
711 //===----------------------------------------------------------------------===//
712 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
713 ///
714 class BlockDesc : public DebugInfoDesc {
715 private:
716   DebugInfoDesc *Context;               // Context debug descriptor.
717
718 public:
719   BlockDesc();
720   
721   // Accessors
722   DebugInfoDesc *getContext()                const { return Context; }
723   void setContext(DebugInfoDesc *C)                { Context = C; }
724   
725   // Implement isa/cast/dyncast.
726   static bool classof(const BlockDesc *) { return true; }
727   static bool classof(const DebugInfoDesc *D);
728   
729   /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
730   ///
731   virtual void ApplyToFields(DIVisitor *Visitor);
732
733   /// getDescString - Return a string used to compose global names and labels.
734   ///
735   virtual const char *getDescString() const;
736
737   /// getTypeString - Return a string used to label this descriptor's type.
738   ///
739   virtual const char *getTypeString() const;
740     
741 #ifndef NDEBUG
742   virtual void dump();
743 #endif
744 };
745
746 //===----------------------------------------------------------------------===//
747 /// DIDeserializer - This class is responsible for casting GlobalVariables
748 /// into DebugInfoDesc objects.
749 class DIDeserializer {
750 private:
751   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
752                                         // Previously defined gloabls.
753   
754 public:
755   DIDeserializer() {}
756   ~DIDeserializer() {}
757   
758   /// Deserialize - Reconstitute a GlobalVariable into it's component
759   /// DebugInfoDesc objects.
760   DebugInfoDesc *Deserialize(Value *V);
761   DebugInfoDesc *Deserialize(GlobalVariable *GV);
762 };
763
764 //===----------------------------------------------------------------------===//
765 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
766 /// into GlobalVariables.
767 class DISerializer {
768 private:
769   Module *M;                            // Definition space module.
770   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
771   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
772   std::map<unsigned, StructType *> TagTypes;
773                                         // Types per Tag.  Created lazily.
774   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
775                                         // Previously defined descriptors.
776   std::map<const std::string, Constant *> StringCache;
777                                         // Previously defined strings.
778                                           
779 public:
780   DISerializer()
781   : M(NULL)
782   , StrPtrTy(NULL)
783   , EmptyStructPtrTy(NULL)
784   , TagTypes()
785   , DescGlobals()
786   , StringCache()
787   {}
788   ~DISerializer() {}
789   
790   // Accessors
791   Module *getModule()        const { return M; };
792   void setModule(Module *module)  { M = module; }
793
794   /// getStrPtrType - Return a "sbyte *" type.
795   ///
796   const PointerType *getStrPtrType();
797   
798   /// getEmptyStructPtrType - Return a "{ }*" type.
799   ///
800   const PointerType *getEmptyStructPtrType();
801   
802   /// getTagType - Return the type describing the specified descriptor (via
803   /// tag.)
804   const StructType *getTagType(DebugInfoDesc *DD);
805   
806   /// getString - Construct the string as constant string global.
807   ///
808   Constant *getString(const std::string &String);
809   
810   /// Serialize - Recursively cast the specified descriptor into a
811   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
812   GlobalVariable *Serialize(DebugInfoDesc *DD);
813 };
814
815 //===----------------------------------------------------------------------===//
816 /// DIVerifier - This class is responsible for verifying the given network of
817 /// GlobalVariables are valid as DebugInfoDesc objects.
818 class DIVerifier {
819 private:
820   enum {
821     Unknown = 0,
822     Invalid,
823     Valid
824   };
825   std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
826   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
827   
828 public:
829   DIVerifier()
830   : Validity()
831   , Counts()
832   {}
833   ~DIVerifier() {}
834   
835   /// Verify - Return true if the GlobalVariable appears to be a valid
836   /// serialization of a DebugInfoDesc.
837   bool Verify(Value *V);
838   bool Verify(GlobalVariable *GV);
839 };
840
841 //===----------------------------------------------------------------------===//
842 /// SourceLineInfo - This class is used to record source line correspondence.
843 ///
844 class SourceLineInfo {
845 private:
846   unsigned Line;                        // Source line number.
847   unsigned Column;                      // Source column.
848   unsigned SourceID;                    // Source ID number.
849   unsigned LabelID;                     // Label in code ID number.
850
851 public:
852   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
853   : Line(L), Column(C), SourceID(S), LabelID(I) {}
854   
855   // Accessors
856   unsigned getLine()     const { return Line; }
857   unsigned getColumn()   const { return Column; }
858   unsigned getSourceID() const { return SourceID; }
859   unsigned getLabelID()  const { return LabelID; }
860 };
861
862 //===----------------------------------------------------------------------===//
863 /// SourceFileInfo - This class is used to track source information.
864 ///
865 class SourceFileInfo {
866 private:
867   unsigned DirectoryID;                 // Directory ID number.
868   std::string Name;                     // File name (not including directory.)
869   
870 public:
871   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
872             
873   // Accessors
874   unsigned getDirectoryID()    const { return DirectoryID; }
875   const std::string &getName() const { return Name; }
876
877   /// operator== - Used by UniqueVector to locate entry.
878   ///
879   bool operator==(const SourceFileInfo &SI) const {
880     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
881   }
882
883   /// operator< - Used by UniqueVector to locate entry.
884   ///
885   bool operator<(const SourceFileInfo &SI) const {
886     return getDirectoryID() < SI.getDirectoryID() ||
887           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
888   }
889 };
890
891 //===----------------------------------------------------------------------===//
892 /// DebugVariable - This class is used to track local variable information.
893 ///
894 class DebugVariable {
895 private:
896   VariableDesc *Desc;                   // Variable Descriptor.
897   unsigned FrameIndex;                  // Variable frame index.
898
899 public:
900   DebugVariable(VariableDesc *D, unsigned I)
901   : Desc(D)
902   , FrameIndex(I)
903   {}
904   
905   // Accessors.
906   VariableDesc *getDesc()  const { return Desc; }
907   unsigned getFrameIndex() const { return FrameIndex; }
908 };
909
910 //===----------------------------------------------------------------------===//
911 /// DebugScope - This class is used to track scope information.
912 ///
913 class DebugScope {
914 private:
915   DebugScope *Parent;                   // Parent to this scope.
916   DebugInfoDesc *Desc;                  // Debug info descriptor for scope.
917                                         // Either subprogram or block.
918   unsigned StartLabelID;                // Label ID of the beginning of scope.
919   unsigned EndLabelID;                  // Label ID of the end of scope.
920   std::vector<DebugScope *> Scopes;     // Scopes defined in scope.
921   std::vector<DebugVariable *> Variables;// Variables declared in scope.
922   
923 public:
924   DebugScope(DebugScope *P, DebugInfoDesc *D)
925   : Parent(P)
926   , Desc(D)
927   , StartLabelID(0)
928   , EndLabelID(0)
929   , Scopes()
930   , Variables()
931   {}
932   ~DebugScope();
933   
934   // Accessors.
935   DebugScope *getParent()        const { return Parent; }
936   DebugInfoDesc *getDesc()       const { return Desc; }
937   unsigned getStartLabelID()     const { return StartLabelID; }
938   unsigned getEndLabelID()       const { return EndLabelID; }
939   std::vector<DebugScope *> &getScopes() { return Scopes; }
940   std::vector<DebugVariable *> &getVariables() { return Variables; }
941   void setStartLabelID(unsigned S) { StartLabelID = S; }
942   void setEndLabelID(unsigned E)   { EndLabelID = E; }
943   
944   /// AddScope - Add a scope to the scope.
945   ///
946   void AddScope(DebugScope *S) { Scopes.push_back(S); }
947   
948   /// AddVariable - Add a variable to the scope.
949   ///
950   void AddVariable(DebugVariable *V) { Variables.push_back(V); }
951 };
952
953 //===----------------------------------------------------------------------===//
954 /// LandingPadInfo - This structure is used to retain landing pad info for
955 /// the current function.
956 ///
957 struct LandingPadInfo {
958   MachineBasicBlock *LandingPadBlock;   // Landing pad block.
959   SmallVector<unsigned, 1> BeginLabels; // Labels prior to invoke.
960   SmallVector<unsigned, 1> EndLabels;   // Labels after invoke.
961   unsigned LandingPadLabel;             // Label at beginning of landing pad.
962   Function *Personality;                // Personality function.
963   std::vector<int> TypeIds;             // List of type ids (filters negative)
964
965   LandingPadInfo(MachineBasicBlock *MBB)
966   : LandingPadBlock(MBB)
967   , LandingPadLabel(0)
968   , Personality(NULL)  
969   , TypeIds(1, 0) // Always have cleanups
970   {}
971 };
972
973 //===----------------------------------------------------------------------===//
974 /// MachineModuleInfo - This class contains meta information specific to a
975 /// module.  Queries can be made by different debugging and exception handling 
976 /// schemes and reformated for specific use.
977 ///
978 class MachineModuleInfo : public ImmutablePass {
979 private:
980   // Use the same deserializer/verifier for the module.
981   DIDeserializer DR;
982   DIVerifier VR;
983
984   // CompileUnits - Uniquing vector for compile units.
985   UniqueVector<CompileUnitDesc *> CompileUnits;
986   
987   // Directories - Uniquing vector for directories.
988   UniqueVector<std::string> Directories;
989                                          
990   // SourceFiles - Uniquing vector for source files.
991   UniqueVector<SourceFileInfo> SourceFiles;
992
993   // Lines - List of of source line correspondence.
994   std::vector<SourceLineInfo> Lines;
995   
996   // LabelIDList - One entry per assigned label.  Normally the entry is equal to
997   // the list index(+1).  If the entry is zero then the label has been deleted.
998   // Any other value indicates the label has been deleted by is mapped to
999   // another label.
1000   std::vector<unsigned> LabelIDList;
1001   
1002   // ScopeMap - Tracks the scopes in the current function.
1003   std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
1004   
1005   // RootScope - Top level scope for the current function.
1006   //
1007   DebugScope *RootScope;
1008   
1009   // FrameMoves - List of moves done by a function's prolog.  Used to construct
1010   // frame maps by debug and exception handling consumers.
1011   std::vector<MachineMove> FrameMoves;
1012   
1013   // LandingPads - List of LandingPadInfo describing the landing pad information
1014   // in the current function.
1015   std::vector<LandingPadInfo> LandingPads;
1016   
1017   // TypeInfos - List of C++ TypeInfo used in the current function.
1018   //
1019   std::vector<GlobalVariable *> TypeInfos;
1020
1021   // FilterIds - List of typeids encoding filters used in the current function.
1022   //
1023   std::vector<unsigned> FilterIds;
1024
1025   // FilterEnds - List of the indices in FilterIds corresponding to filter
1026   // terminators.
1027   //
1028   std::vector<unsigned> FilterEnds;
1029
1030   // Personalities - Vector of all personality functions ever seen. Used to emit
1031   // common EH frames.
1032   std::vector<Function *> Personalities;
1033 public:
1034   static char ID; // Pass identification, replacement for typeid
1035
1036   MachineModuleInfo();
1037   ~MachineModuleInfo();
1038   
1039   /// doInitialization - Initialize the state for a new module.
1040   ///
1041   bool doInitialization();
1042   
1043   /// doFinalization - Tear down the state after completion of a module.
1044   ///
1045   bool doFinalization();
1046   
1047   /// BeginFunction - Begin gathering function meta information.
1048   ///
1049   void BeginFunction(MachineFunction *MF);
1050   
1051   /// EndFunction - Discard function meta information.
1052   ///
1053   void EndFunction();
1054
1055   /// getDescFor - Convert a Value to a debug information descriptor.
1056   ///
1057   // FIXME - use new Value type when available.
1058   DebugInfoDesc *getDescFor(Value *V);
1059   
1060   /// Verify - Verify that a Value is debug information descriptor.
1061   ///
1062   bool Verify(Value *V);
1063   
1064   /// AnalyzeModule - Scan the module for global debug information.
1065   ///
1066   void AnalyzeModule(Module &M);
1067   
1068   /// hasDebugInfo - Returns true if valid debug info is present.
1069   ///
1070   bool hasDebugInfo() const { return !CompileUnits.empty(); }
1071   
1072   /// needsFrameInfo - Returns true if we need to gather callee-saved register
1073   /// move info for the frame.
1074   bool needsFrameInfo() const;
1075   
1076   /// NextLabelID - Return the next unique label id.
1077   ///
1078   unsigned NextLabelID() {
1079     unsigned ID = LabelIDList.size() + 1;
1080     LabelIDList.push_back(ID);
1081     return ID;
1082   }
1083   
1084   /// RecordLabel - Records location information and associates it with a
1085   /// label.  Returns a unique label ID used to generate a label and 
1086   /// provide correspondence to the source line list.
1087   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source);
1088   
1089   /// InvalidateLabel - Inhibit use of the specified label # from
1090   /// MachineModuleInfo, for example because the code was deleted.
1091   void InvalidateLabel(unsigned LabelID) {
1092     // Remap to zero to indicate deletion.
1093     RemapLabel(LabelID, 0);
1094   }
1095
1096   /// RemapLabel - Indicate that a label has been merged into another.
1097   ///
1098   void RemapLabel(unsigned OldLabelID, unsigned NewLabelID) {
1099     assert(0 < OldLabelID && OldLabelID <= LabelIDList.size() &&
1100           "Old label ID out of range.");
1101     assert(NewLabelID <= LabelIDList.size() &&
1102           "New label ID out of range.");
1103     LabelIDList[OldLabelID - 1] = NewLabelID;
1104   }
1105   
1106   /// MappedLabel - Find out the label's final ID.  Zero indicates deletion.
1107   /// ID != Mapped ID indicates that the label was folded into another label.
1108   unsigned MappedLabel(unsigned LabelID) const {
1109     assert(LabelID <= LabelIDList.size() && "Debug label ID out of range.");
1110     return LabelID ? LabelIDList[LabelID - 1] : 0;
1111   }
1112
1113   /// RecordSource - Register a source file with debug info. Returns an source
1114   /// ID.
1115   unsigned RecordSource(const std::string &Directory,
1116                         const std::string &Source);
1117   unsigned RecordSource(const CompileUnitDesc *CompileUnit);
1118   
1119   /// getDirectories - Return the UniqueVector of std::string representing
1120   /// directories.
1121   const UniqueVector<std::string> &getDirectories() const {
1122     return Directories;
1123   }
1124   
1125   /// getSourceFiles - Return the UniqueVector of source files. 
1126   ///
1127   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
1128     return SourceFiles;
1129   }
1130   
1131   /// getSourceLines - Return a vector of source lines.
1132   ///
1133   const std::vector<SourceLineInfo> &getSourceLines() const {
1134     return Lines;
1135   }
1136   
1137   /// SetupCompileUnits - Set up the unique vector of compile units.
1138   ///
1139   void SetupCompileUnits(Module &M);
1140
1141   /// getCompileUnits - Return a vector of debug compile units.
1142   ///
1143   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
1144   
1145   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1146   /// named GlobalVariable.
1147   std::vector<GlobalVariable*>
1148   getGlobalVariablesUsing(Module &M, const std::string &RootName);
1149
1150   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
1151   ///
1152   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
1153     T Desc;
1154     std::vector<GlobalVariable *> Globals =
1155                              getGlobalVariablesUsing(M, Desc.getAnchorString());
1156     std::vector<T *> AnchoredDescs;
1157     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
1158       GlobalVariable *GV = Globals[i];
1159       
1160       // FIXME - In the short term, changes are too drastic to continue.
1161       if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() &&
1162           DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) {
1163         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
1164       }
1165     }
1166
1167     return AnchoredDescs;
1168   }
1169   
1170   /// RecordRegionStart - Indicate the start of a region.
1171   ///
1172   unsigned RecordRegionStart(Value *V);
1173
1174   /// RecordRegionEnd - Indicate the end of a region.
1175   ///
1176   unsigned RecordRegionEnd(Value *V);
1177
1178   /// RecordVariable - Indicate the declaration of  a local variable.
1179   ///
1180   void RecordVariable(Value *V, unsigned FrameIndex);
1181   
1182   /// getRootScope - Return current functions root scope.
1183   ///
1184   DebugScope *getRootScope() { return RootScope; }
1185   
1186   /// getOrCreateScope - Returns the scope associated with the given descriptor.
1187   ///
1188   DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
1189   
1190   /// getFrameMoves - Returns a reference to a list of moves done in the current
1191   /// function's prologue.  Used to construct frame maps for debug and exception
1192   /// handling comsumers.
1193   std::vector<MachineMove> &getFrameMoves() { return FrameMoves; }
1194   
1195   //===-EH-----------------------------------------------------------------===//
1196
1197   /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
1198   /// specified MachineBasicBlock.
1199   LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
1200
1201   /// addInvoke - Provide the begin and end labels of an invoke style call and
1202   /// associate it with a try landing pad block.
1203   void addInvoke(MachineBasicBlock *LandingPad, unsigned BeginLabel,
1204                                                 unsigned EndLabel);
1205   
1206   /// addLandingPad - Add a new panding pad.  Returns the label ID for the 
1207   /// landing pad entry.
1208   unsigned addLandingPad(MachineBasicBlock *LandingPad);
1209   
1210   /// addPersonality - Provide the personality function for the exception
1211   /// information.
1212   void addPersonality(MachineBasicBlock *LandingPad, Function *Personality);
1213
1214   /// getPersonalityIndex - Get index of the current personality function inside
1215   /// Personalitites array
1216   unsigned getPersonalityIndex() const;
1217
1218   /// getPersonalities - Return array of personality functions ever seen.
1219   const std::vector<Function *>& getPersonalities() const {
1220     return Personalities;
1221   }
1222
1223   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
1224   ///
1225   void addCatchTypeInfo(MachineBasicBlock *LandingPad,
1226                         std::vector<GlobalVariable *> &TyInfo);
1227
1228   /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
1229   ///
1230   void addFilterTypeInfo(MachineBasicBlock *LandingPad,
1231                          std::vector<GlobalVariable *> &TyInfo);
1232
1233   /// getTypeIDFor - Return the type id for the specified typeinfo.  This is 
1234   /// function wide.
1235   unsigned getTypeIDFor(GlobalVariable *TI);
1236
1237   /// getFilterIDFor - Return the id of the filter encoded by TyIds.  This is
1238   /// function wide.
1239   int getFilterIDFor(std::vector<unsigned> &TyIds);
1240
1241   /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
1242   /// pads.
1243   void TidyLandingPads();
1244                         
1245   /// getLandingPads - Return a reference to the landing pad info for the
1246   /// current function.
1247   const std::vector<LandingPadInfo> &getLandingPads() const {
1248     return LandingPads;
1249   }
1250   
1251   /// getTypeInfos - Return a reference to the C++ typeinfo for the current
1252   /// function.
1253   const std::vector<GlobalVariable *> &getTypeInfos() const {
1254     return TypeInfos;
1255   }
1256
1257   /// getFilterIds - Return a reference to the typeids encoding filters used in
1258   /// the current function.
1259   const std::vector<unsigned> &getFilterIds() const {
1260     return FilterIds;
1261   }
1262
1263   /// getPersonality - Return a personality function if available.  The presence
1264   /// of one is required to emit exception handling info.
1265   Function *getPersonality() const;
1266
1267 }; // End class MachineModuleInfo
1268
1269 } // End llvm namespace
1270
1271 #endif