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