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