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