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