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