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