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