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