1. Support standard dwarf format (was bootstrapping in Apple format.)
[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 coorespondence - 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 #include "llvm/User.h"
39
40 #include <string>
41 #include <set>
42
43 namespace llvm {
44
45 //===----------------------------------------------------------------------===//
46 // Forward declarations.
47 class Constant;
48 class DebugInfoDesc;
49 class GlobalVariable;
50 class MachineFunction;
51 class MachineMove;
52 class Module;
53 class PointerType;
54 class StructType;
55
56 //===----------------------------------------------------------------------===//
57 // Debug info constants.
58
59 enum {
60   LLVMDebugVersion = 4                  // Current version of debug information.
61 };
62
63 //===----------------------------------------------------------------------===//
64 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
65 /// the supplied DebugInfoDesc.
66 class DIVisitor {
67 public:
68   DIVisitor() {}
69   virtual ~DIVisitor() {}
70
71   /// ApplyToFields - Target the visitor to each field of the debug information
72   /// descriptor.
73   void ApplyToFields(DebugInfoDesc *DD);
74   
75   /// Apply - Subclasses override each of these methods to perform the
76   /// appropriate action for the type of field.
77   virtual void Apply(int &Field) = 0;
78   virtual void Apply(unsigned &Field) = 0;
79   virtual void Apply(int64_t &Field) = 0;
80   virtual void Apply(uint64_t &Field) = 0;
81   virtual void Apply(bool &Field) = 0;
82   virtual void Apply(std::string &Field) = 0;
83   virtual void Apply(DebugInfoDesc *&Field) = 0;
84   virtual void Apply(GlobalVariable *&Field) = 0;
85   virtual void Apply(std::vector<DebugInfoDesc *> &Field) = 0;
86 };
87
88 //===----------------------------------------------------------------------===//
89 /// DebugInfoDesc - This class is the base class for debug info descriptors.
90 ///
91 class DebugInfoDesc {
92 private:
93   enum {
94     tag_mask = 0x0000ffff,
95     version_shift = 16
96   };
97
98
99   unsigned Tag;                         // Content indicator.  Dwarf values are
100                                         // used but that does not limit use to
101                                         // Dwarf writers.
102   
103 protected:
104   DebugInfoDesc(unsigned T) : Tag(T | (LLVMDebugVersion << version_shift)) {}
105   
106 public:
107   virtual ~DebugInfoDesc() {}
108
109   // Accessors
110   unsigned getTag()          const { return Tag & tag_mask; }
111   unsigned getVersion()      const { return Tag >> version_shift; }
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   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   AnchorDesc *Anchor;                   // Anchor for all descriptors of the
203                                         // same type.
204
205 protected:
206
207   AnchoredDesc(unsigned T);
208
209 public:  
210   // Accessors.
211   AnchorDesc *getAnchor() const { return Anchor; }
212   void setAnchor(AnchorDesc *A) { Anchor = 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 *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   DebugInfoDesc *Context;               // Context debug descriptor.
284   std::string Name;                     // Type name (may be empty.)
285   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
286   unsigned Line;                        // Defined line# (may be zero.)
287   uint64_t Size;                        // Type bit size (may be zero.)
288   uint64_t Align;                       // Type bit alignment (may be zero.)
289   uint64_t Offset;                      // Type bit offset (may be zero.)
290
291 public:
292   TypeDesc(unsigned T);
293
294   // Accessors
295   DebugInfoDesc *getContext()                const { return Context; }
296   const std::string &getName()               const { return Name; }
297   CompileUnitDesc *getFile()                 const { return File; }
298   unsigned getLine()                         const { return Line; }
299   uint64_t getSize()                         const { return Size; }
300   uint64_t getAlign()                        const { return Align; }
301   uint64_t getOffset()                       const { return Offset; }
302   void setContext(DebugInfoDesc *C)                { Context = C; }
303   void setName(const std::string &N)               { Name = N; }
304   void setFile(CompileUnitDesc *U)                 { File = U; }
305   void setLine(unsigned L)                         { Line = L; }
306   void setSize(uint64_t S)                         { Size = S; }
307   void setAlign(uint64_t A)                        { Align = A; }
308   void setOffset(uint64_t O)                       { Offset = O; }
309   
310   /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
311   ///
312   virtual void ApplyToFields(DIVisitor *Visitor);
313
314   /// getDescString - Return a string used to compose global names and labels.
315   ///
316   virtual const char *getDescString() const;
317
318   /// getTypeString - Return a string used to label this descriptor's type.
319   ///
320   virtual const char *getTypeString() const;
321   
322 #ifndef NDEBUG
323   virtual void dump();
324 #endif
325 };
326
327 //===----------------------------------------------------------------------===//
328 /// BasicTypeDesc - This class packages debug information associated with a
329 /// basic type (eg. int, bool, double.)
330 class BasicTypeDesc : public TypeDesc {
331 private:
332   unsigned Encoding;                    // Type encoding.
333   
334 public:
335   BasicTypeDesc();
336   
337   // Accessors
338   unsigned getEncoding()                     const { return Encoding; }
339   void setEncoding(unsigned E)                     { Encoding = E; }
340
341   // Implement isa/cast/dyncast.
342   static bool classof(const BasicTypeDesc *) { return true; }
343   static bool classof(const DebugInfoDesc *D);
344   
345   /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
346   ///
347   virtual void ApplyToFields(DIVisitor *Visitor);
348
349   /// getDescString - Return a string used to compose global names and labels.
350   ///
351   virtual const char *getDescString() const;
352
353   /// getTypeString - Return a string used to label this descriptor's type.
354   ///
355   virtual const char *getTypeString() const;
356
357 #ifndef NDEBUG
358   virtual void dump();
359 #endif
360 };
361
362
363 //===----------------------------------------------------------------------===//
364 /// DerivedTypeDesc - This class packages debug information associated with a
365 /// derived types (eg., typedef, pointer, reference.)
366 class DerivedTypeDesc : public TypeDesc {
367 private:
368   TypeDesc *FromType;                   // Type derived from.
369
370 public:
371   DerivedTypeDesc(unsigned T);
372   
373   // Accessors
374   TypeDesc *getFromType()                    const { return FromType; }
375   void setFromType(TypeDesc *F)                    { FromType = F; }
376
377   // Implement isa/cast/dyncast.
378   static bool classof(const DerivedTypeDesc *) { return true; }
379   static bool classof(const DebugInfoDesc *D);
380   
381   /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
382   ///
383   virtual void ApplyToFields(DIVisitor *Visitor);
384
385   /// getDescString - Return a string used to compose global names and labels.
386   ///
387   virtual const char *getDescString() const;
388
389   /// getTypeString - Return a string used to label this descriptor's type.
390   ///
391   virtual const char *getTypeString() const;
392
393 #ifndef NDEBUG
394   virtual void dump();
395 #endif
396 };
397
398 //===----------------------------------------------------------------------===//
399 /// CompositeTypeDesc - This class packages debug information associated with a
400 /// array/struct types (eg., arrays, struct, union, enums.)
401 class CompositeTypeDesc : public DerivedTypeDesc {
402 private:
403   bool IsVector;                        // packed/vector array
404   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
405
406 public:
407   CompositeTypeDesc(unsigned T);
408   
409   // Accessors
410   bool isVector() const { return IsVector; }
411   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
412   void setIsVector() { IsVector = true; }
413
414   // Implement isa/cast/dyncast.
415   static bool classof(const CompositeTypeDesc *) { return true; }
416   static bool classof(const DebugInfoDesc *D);
417   
418   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
419   ///
420   virtual void ApplyToFields(DIVisitor *Visitor);
421
422   /// getDescString - Return a string used to compose global names and labels.
423   ///
424   virtual const char *getDescString() const;
425
426   /// getTypeString - Return a string used to label this descriptor's type.
427   ///
428   virtual const char *getTypeString() const;
429
430 #ifndef NDEBUG
431   virtual void dump();
432 #endif
433 };
434
435 //===----------------------------------------------------------------------===//
436 /// SubrangeDesc - This class packages debug information associated with integer
437 /// value ranges.
438 class SubrangeDesc : public DebugInfoDesc {
439 private:
440   int64_t Lo;                           // Low value of range.
441   int64_t Hi;                           // High value of range.
442
443 public:
444   SubrangeDesc();
445   
446   // Accessors
447   int64_t getLo()                            const { return Lo; }
448   int64_t getHi()                            const { return Hi; }
449   void setLo(int64_t L)                            { Lo = L; }
450   void setHi(int64_t H)                            { Hi = H; }
451
452   // Implement isa/cast/dyncast.
453   static bool classof(const SubrangeDesc *) { return true; }
454   static bool classof(const DebugInfoDesc *D);
455   
456   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
457   ///
458   virtual void ApplyToFields(DIVisitor *Visitor);
459
460   /// getDescString - Return a string used to compose global names and labels.
461   ///
462   virtual const char *getDescString() const;
463     
464   /// getTypeString - Return a string used to label this descriptor's type.
465   ///
466   virtual const char *getTypeString() const;
467
468 #ifndef NDEBUG
469   virtual void dump();
470 #endif
471 };
472
473 //===----------------------------------------------------------------------===//
474 /// EnumeratorDesc - This class packages debug information associated with
475 /// named integer constants.
476 class EnumeratorDesc : public DebugInfoDesc {
477 private:
478   std::string Name;                     // Enumerator name.
479   int64_t Value;                        // Enumerator value.
480
481 public:
482   EnumeratorDesc();
483   
484   // Accessors
485   const std::string &getName()               const { return Name; }
486   int64_t getValue()                         const { return Value; }
487   void setName(const std::string &N)               { Name = N; }
488   void setValue(int64_t V)                         { Value = V; }
489
490   // Implement isa/cast/dyncast.
491   static bool classof(const EnumeratorDesc *) { return true; }
492   static bool classof(const DebugInfoDesc *D);
493   
494   /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
495   ///
496   virtual void ApplyToFields(DIVisitor *Visitor);
497
498   /// getDescString - Return a string used to compose global names and labels.
499   ///
500   virtual const char *getDescString() const;
501     
502   /// getTypeString - Return a string used to label this descriptor's type.
503   ///
504   virtual const char *getTypeString() const;
505
506 #ifndef NDEBUG
507   virtual void dump();
508 #endif
509 };
510
511 //===----------------------------------------------------------------------===//
512 /// VariableDesc - This class packages debug information associated with a
513 /// subprogram variable.
514 ///
515 class VariableDesc : public DebugInfoDesc {
516 private:
517   DebugInfoDesc *Context;               // Context debug descriptor.
518   std::string Name;                     // Type name (may be empty.)
519   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
520   unsigned Line;                        // Defined line# (may be zero.)
521   TypeDesc *TyDesc;                     // Type of variable.
522
523 public:
524   VariableDesc(unsigned T);
525
526   // Accessors
527   DebugInfoDesc *getContext()                const { return Context; }
528   const std::string &getName()               const { return Name; }
529   CompileUnitDesc *getFile()                 const { return File; }
530   unsigned getLine()                         const { return Line; }
531   TypeDesc *getType()                        const { return TyDesc; }
532   void setContext(DebugInfoDesc *C)                { Context = C; }
533   void setName(const std::string &N)               { Name = N; }
534   void setFile(CompileUnitDesc *U)                 { File = U; }
535   void setLine(unsigned L)                         { Line = L; }
536   void setType(TypeDesc *T)                        { TyDesc = T; }
537   
538   // Implement isa/cast/dyncast.
539   static bool classof(const VariableDesc *) { return true; }
540   static bool classof(const DebugInfoDesc *D);
541   
542   /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
543   ///
544   virtual void ApplyToFields(DIVisitor *Visitor);
545
546   /// getDescString - Return a string used to compose global names and labels.
547   ///
548   virtual const char *getDescString() const;
549
550   /// getTypeString - Return a string used to label this descriptor's type.
551   ///
552   virtual const char *getTypeString() const;
553   
554 #ifndef NDEBUG
555   virtual void dump();
556 #endif
557 };
558
559 //===----------------------------------------------------------------------===//
560 /// GlobalDesc - This class is the base descriptor for global functions and
561 /// variables.
562 class GlobalDesc : public AnchoredDesc {
563 private:
564   DebugInfoDesc *Context;               // Context debug descriptor.
565   std::string Name;                     // Global name.
566   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
567   unsigned Line;                        // Defined line# (may be zero.)
568   TypeDesc *TyDesc;                     // Type debug descriptor.
569   bool IsStatic;                        // Is the global a static.
570   bool IsDefinition;                    // Is the global defined in context.
571   
572 protected:
573   GlobalDesc(unsigned T);
574
575 public:
576   // Accessors
577   DebugInfoDesc *getContext()                const { return Context; }
578   const std::string &getName()               const { return Name; }
579   CompileUnitDesc *getFile()                 const { return File; }
580   unsigned getLine()                         const { return Line; }
581   TypeDesc *getType()                        const { return TyDesc; }
582   bool isStatic()                            const { return IsStatic; }
583   bool isDefinition()                        const { return IsDefinition; }
584   void setContext(DebugInfoDesc *C)                { Context = C; }
585   void setName(const std::string &N)               { Name = N; }
586   void setFile(CompileUnitDesc *U)                 { File = U; }
587   void setLine(unsigned L)                         { Line = L; }
588   void setType(TypeDesc *T)                        { TyDesc = T; }
589   void setIsStatic(bool IS)                        { IsStatic = IS; }
590   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
591
592   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
593   ///
594   virtual void ApplyToFields(DIVisitor *Visitor);
595 };
596
597 //===----------------------------------------------------------------------===//
598 /// GlobalVariableDesc - This class packages debug information associated with a
599 /// GlobalVariable.
600 class GlobalVariableDesc : public GlobalDesc {
601 private:
602   GlobalVariable *Global;               // llvm global.
603   
604 public:
605   GlobalVariableDesc();
606
607   // Accessors.
608   GlobalVariable *getGlobalVariable()        const { return Global; }
609   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
610  
611   // Implement isa/cast/dyncast.
612   static bool classof(const GlobalVariableDesc *) { return true; }
613   static bool classof(const DebugInfoDesc *D);
614   
615   /// ApplyToFields - Target the visitor to the fields of the
616   /// GlobalVariableDesc.
617   virtual void ApplyToFields(DIVisitor *Visitor);
618
619   /// getDescString - Return a string used to compose global names and labels.
620   ///
621   virtual const char *getDescString() const;
622
623   /// getTypeString - Return a string used to label this descriptor's type.
624   ///
625   virtual const char *getTypeString() const;
626   
627   /// getAnchorString - Return a string used to label this descriptor's anchor.
628   ///
629   static const char *AnchorString;
630   virtual const char *getAnchorString() const;
631     
632 #ifndef NDEBUG
633   virtual void dump();
634 #endif
635 };
636
637 //===----------------------------------------------------------------------===//
638 /// SubprogramDesc - This class packages debug information associated with a
639 /// subprogram/function.
640 class SubprogramDesc : public GlobalDesc {
641 private:
642   
643 public:
644   SubprogramDesc();
645   
646   // Accessors
647   
648   // Implement isa/cast/dyncast.
649   static bool classof(const SubprogramDesc *) { return true; }
650   static bool classof(const DebugInfoDesc *D);
651   
652   /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
653   ///
654   virtual void ApplyToFields(DIVisitor *Visitor);
655
656   /// getDescString - Return a string used to compose global names and labels.
657   ///
658   virtual const char *getDescString() const;
659
660   /// getTypeString - Return a string used to label this descriptor's type.
661   ///
662   virtual const char *getTypeString() const;
663   
664   /// getAnchorString - Return a string used to label this descriptor's anchor.
665   ///
666   static const char *AnchorString;
667   virtual const char *getAnchorString() const;
668     
669 #ifndef NDEBUG
670   virtual void dump();
671 #endif
672 };
673
674 //===----------------------------------------------------------------------===//
675 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
676 ///
677 class BlockDesc : public DebugInfoDesc {
678 private:
679   DebugInfoDesc *Context;               // Context debug descriptor.
680
681 public:
682   BlockDesc();
683   
684   // Accessors
685   DebugInfoDesc *getContext()                const { return Context; }
686   void setContext(DebugInfoDesc *C)                { Context = C; }
687   
688   // Implement isa/cast/dyncast.
689   static bool classof(const BlockDesc *) { return true; }
690   static bool classof(const DebugInfoDesc *D);
691   
692   /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
693   ///
694   virtual void ApplyToFields(DIVisitor *Visitor);
695
696   /// getDescString - Return a string used to compose global names and labels.
697   ///
698   virtual const char *getDescString() const;
699
700   /// getTypeString - Return a string used to label this descriptor's type.
701   ///
702   virtual const char *getTypeString() const;
703     
704 #ifndef NDEBUG
705   virtual void dump();
706 #endif
707 };
708
709 //===----------------------------------------------------------------------===//
710 /// DIDeserializer - This class is responsible for casting GlobalVariables
711 /// into DebugInfoDesc objects.
712 class DIDeserializer {
713 private:
714   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
715                                         // Previously defined gloabls.
716   
717 public:
718   DIDeserializer() {}
719   ~DIDeserializer() {}
720   
721   /// Deserialize - Reconstitute a GlobalVariable into it's component
722   /// DebugInfoDesc objects.
723   DebugInfoDesc *Deserialize(Value *V);
724   DebugInfoDesc *Deserialize(GlobalVariable *GV);
725 };
726
727 //===----------------------------------------------------------------------===//
728 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
729 /// into GlobalVariables.
730 class DISerializer {
731 private:
732   Module *M;                            // Definition space module.
733   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
734   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
735   std::map<unsigned, StructType *> TagTypes;
736                                         // Types per Tag.  Created lazily.
737   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
738                                         // Previously defined descriptors.
739   std::map<const std::string, Constant *> StringCache;
740                                         // Previously defined strings.
741                                           
742 public:
743   DISerializer()
744   : M(NULL)
745   , StrPtrTy(NULL)
746   , EmptyStructPtrTy(NULL)
747   , TagTypes()
748   , DescGlobals()
749   , StringCache()
750   {}
751   ~DISerializer() {}
752   
753   // Accessors
754   Module *getModule()        const { return M; };
755   void setModule(Module *module)  { M = module; }
756
757   /// getStrPtrType - Return a "sbyte *" type.
758   ///
759   const PointerType *getStrPtrType();
760   
761   /// getEmptyStructPtrType - Return a "{ }*" type.
762   ///
763   const PointerType *getEmptyStructPtrType();
764   
765   /// getTagType - Return the type describing the specified descriptor (via
766   /// tag.)
767   const StructType *getTagType(DebugInfoDesc *DD);
768   
769   /// getString - Construct the string as constant string global.
770   ///
771   Constant *getString(const std::string &String);
772   
773   /// Serialize - Recursively cast the specified descriptor into a
774   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
775   GlobalVariable *Serialize(DebugInfoDesc *DD);
776 };
777
778 //===----------------------------------------------------------------------===//
779 /// DIVerifier - This class is responsible for verifying the given network of
780 /// GlobalVariables are valid as DebugInfoDesc objects.
781 class DIVerifier {
782 private:
783   enum {
784     Unknown = 0,
785     Invalid,
786     Valid
787   };
788   std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
789   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
790   
791 public:
792   DIVerifier()
793   : Validity()
794   , Counts()
795   {}
796   ~DIVerifier() {}
797   
798   /// Verify - Return true if the GlobalVariable appears to be a valid
799   /// serialization of a DebugInfoDesc.
800   bool Verify(Value *V);
801   bool Verify(GlobalVariable *GV);
802 };
803
804 //===----------------------------------------------------------------------===//
805 /// SourceLineInfo - This class is used to record source line correspondence.
806 ///
807 class SourceLineInfo {
808 private:
809   unsigned Line;                        // Source line number.
810   unsigned Column;                      // Source column.
811   unsigned SourceID;                    // Source ID number.
812   unsigned LabelID;                     // Label in code ID number.
813
814 public:
815   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
816   : Line(L), Column(C), SourceID(S), LabelID(I) {}
817   
818   // Accessors
819   unsigned getLine()     const { return Line; }
820   unsigned getColumn()   const { return Column; }
821   unsigned getSourceID() const { return SourceID; }
822   unsigned getLabelID()  const { return LabelID; }
823 };
824
825 //===----------------------------------------------------------------------===//
826 /// SourceFileInfo - This class is used to track source information.
827 ///
828 class SourceFileInfo {
829 private:
830   unsigned DirectoryID;                 // Directory ID number.
831   std::string Name;                     // File name (not including directory.)
832   
833 public:
834   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
835             
836   // Accessors
837   unsigned getDirectoryID()    const { return DirectoryID; }
838   const std::string &getName() const { return Name; }
839
840   /// operator== - Used by UniqueVector to locate entry.
841   ///
842   bool operator==(const SourceFileInfo &SI) const {
843     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
844   }
845
846   /// operator< - Used by UniqueVector to locate entry.
847   ///
848   bool operator<(const SourceFileInfo &SI) const {
849     return getDirectoryID() < SI.getDirectoryID() ||
850           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
851   }
852 };
853
854 //===----------------------------------------------------------------------===//
855 /// DebugVariable - This class is used to track local variable information.
856 ///
857 class DebugVariable {
858 private:
859   VariableDesc *Desc;                   // Variable Descriptor.
860   unsigned FrameIndex;                  // Variable frame index.
861
862 public:
863   DebugVariable(VariableDesc *D, unsigned I)
864   : Desc(D)
865   , FrameIndex(I)
866   {}
867   
868   // Accessors.
869   VariableDesc *getDesc()  const { return Desc; }
870   unsigned getFrameIndex() const { return FrameIndex; }
871 };
872
873 //===----------------------------------------------------------------------===//
874 /// DebugScope - This class is used to track scope information.
875 ///
876 class DebugScope {
877 private:
878   DebugScope *Parent;                   // Parent to this scope.
879   DebugInfoDesc *Desc;                  // Debug info descriptor for scope.
880                                         // Either subprogram or block.
881   unsigned StartLabelID;                // Label ID of the beginning of scope.
882   unsigned EndLabelID;                  // Label ID of the end of scope.
883   std::vector<DebugScope *> Scopes;     // Scopes defined in scope.
884   std::vector<DebugVariable *> Variables;// Variables declared in scope.
885   
886 public:
887   DebugScope(DebugScope *P, DebugInfoDesc *D)
888   : Parent(P)
889   , Desc(D)
890   , StartLabelID(0)
891   , EndLabelID(0)
892   , Scopes()
893   , Variables()
894   {}
895   ~DebugScope();
896   
897   // Accessors.
898   DebugScope *getParent()        const { return Parent; }
899   DebugInfoDesc *getDesc()       const { return Desc; }
900   unsigned getStartLabelID()     const { return StartLabelID; }
901   unsigned getEndLabelID()       const { return EndLabelID; }
902   std::vector<DebugScope *> &getScopes() { return Scopes; }
903   std::vector<DebugVariable *> &getVariables() { return Variables; }
904   void setStartLabelID(unsigned S) { StartLabelID = S; }
905   void setEndLabelID(unsigned E)   { EndLabelID = E; }
906   
907   /// AddScope - Add a scope to the scope.
908   ///
909   void AddScope(DebugScope *S) { Scopes.push_back(S); }
910   
911   /// AddVariable - Add a variable to the scope.
912   ///
913   void AddVariable(DebugVariable *V) { Variables.push_back(V); }
914 };
915
916 //===----------------------------------------------------------------------===//
917 /// MachineDebugInfo - This class contains debug information specific to a
918 /// module.  Queries can be made by different debugging schemes and reformated
919 /// for specific use.
920 ///
921 class MachineDebugInfo : public ImmutablePass {
922 private:
923   // Use the same deserializer/verifier for the module.
924   DIDeserializer DR;
925   DIVerifier VR;
926
927   // CompileUnits - Uniquing vector for compile units.
928   UniqueVector<CompileUnitDesc *> CompileUnits;
929   
930   // Directories - Uniquing vector for directories.
931   UniqueVector<std::string> Directories;
932                                          
933   // SourceFiles - Uniquing vector for source files.
934   UniqueVector<SourceFileInfo> SourceFiles;
935
936   // Lines - List of of source line correspondence.
937   std::vector<SourceLineInfo *> Lines;
938   
939   // LabelID - Current number assigned to unique label numbers.
940   unsigned LabelID;
941   
942   // ScopeMap - Tracks the scopes in the current function.
943   std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
944   
945   // RootScope - Top level scope for the current function.
946   //
947   DebugScope *RootScope;
948   
949   // FrameMoves - List of moves done by a function's prolog.  Used to construct
950   // frame maps by debug consumers.
951   std::vector<MachineMove *> FrameMoves;
952
953 public:
954   MachineDebugInfo();
955   ~MachineDebugInfo();
956   
957   /// doInitialization - Initialize the debug state for a new module.
958   ///
959   bool doInitialization();
960   
961   /// doFinalization - Tear down the debug state after completion of a module.
962   ///
963   bool doFinalization();
964   
965   /// BeginFunction - Begin gathering function debug information.
966   ///
967   void BeginFunction(MachineFunction *MF);
968   
969   /// EndFunction - Discard function debug information.
970   ///
971   void EndFunction();
972
973   /// getDescFor - Convert a Value to a debug information descriptor.
974   ///
975   // FIXME - use new Value type when available.
976   DebugInfoDesc *getDescFor(Value *V);
977   
978   /// Verify - Verify that a Value is debug information descriptor.
979   ///
980   bool Verify(Value *V);
981   
982   /// AnalyzeModule - Scan the module for global debug information.
983   ///
984   void AnalyzeModule(Module &M);
985   
986   /// hasInfo - Returns true if valid debug info is present.
987   ///
988   bool hasInfo() const { return !CompileUnits.empty(); }
989   
990   /// NextLabelID - Return the next unique label id.
991   ///
992   unsigned NextLabelID() { return ++LabelID; }
993   
994   /// RecordLabel - Records location information and associates it with a
995   /// debug label.  Returns a unique label ID used to generate a label and 
996   /// provide correspondence to the source line list.
997   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source);
998   
999   /// RecordSource - Register a source file with debug info. Returns an source
1000   /// ID.
1001   unsigned RecordSource(const std::string &Directory,
1002                         const std::string &Source);
1003   unsigned RecordSource(const CompileUnitDesc *CompileUnit);
1004   
1005   /// getDirectories - Return the UniqueVector of std::string representing
1006   /// directories.
1007   const UniqueVector<std::string> &getDirectories() const {
1008     return Directories;
1009   }
1010   
1011   /// getSourceFiles - Return the UniqueVector of source files. 
1012   ///
1013   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
1014     return SourceFiles;
1015   }
1016   
1017   /// getSourceLines - Return a vector of source lines.  Vector index + 1
1018   /// equals label ID.
1019   const std::vector<SourceLineInfo *> &getSourceLines() const {
1020     return Lines;
1021   }
1022   
1023   /// SetupCompileUnits - Set up the unique vector of compile units.
1024   ///
1025   void SetupCompileUnits(Module &M);
1026
1027   /// getCompileUnits - Return a vector of debug compile units.
1028   ///
1029   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
1030   
1031   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1032   /// named GlobalVariable.
1033   std::vector<GlobalVariable*>
1034   getGlobalVariablesUsing(Module &M, const std::string &RootName);
1035
1036   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
1037   ///
1038   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
1039     T Desc;
1040     std::vector<GlobalVariable *> Globals =
1041                              getGlobalVariablesUsing(M, Desc.getAnchorString());
1042     std::vector<T *> AnchoredDescs;
1043     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
1044       GlobalVariable *GV = Globals[i];
1045       
1046       // FIXME - In the short term, changes are too drastic to continue.
1047       if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() &&
1048           DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) {
1049         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
1050       }
1051     }
1052
1053     return AnchoredDescs;
1054   }
1055   
1056   /// RecordRegionStart - Indicate the start of a region.
1057   ///
1058   unsigned RecordRegionStart(Value *V);
1059
1060   /// RecordRegionEnd - Indicate the end of a region.
1061   ///
1062   unsigned RecordRegionEnd(Value *V);
1063
1064   /// RecordVariable - Indicate the declaration of  a local variable.
1065   ///
1066   void RecordVariable(Value *V, unsigned FrameIndex);
1067   
1068   /// getRootScope - Return current functions root scope.
1069   ///
1070   DebugScope *getRootScope() { return RootScope; }
1071   
1072   /// getOrCreateScope - Returns the scope associated with the given descriptor.
1073   ///
1074   DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
1075   
1076   /// getFrameMoves - Returns a reference to a list of moves done in the current
1077   /// function's prologue.  Used to construct frame maps for debug comsumers.
1078   std::vector<MachineMove *> &getFrameMoves() { return FrameMoves; }
1079
1080 }; // End class MachineDebugInfo
1081
1082 } // End llvm namespace
1083
1084 #endif