8a7160caca245b004254f430bb353351a2a7b083
[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 = 3                  // 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   std::vector<DebugInfoDesc *> Elements;// Information used to compose type.
404
405 public:
406   CompositeTypeDesc(unsigned T);
407   
408   // Accessors
409   std::vector<DebugInfoDesc *> &getElements() { return Elements; }
410
411   // Implement isa/cast/dyncast.
412   static bool classof(const CompositeTypeDesc *) { return true; }
413   static bool classof(const DebugInfoDesc *D);
414   
415   /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
416   ///
417   virtual void ApplyToFields(DIVisitor *Visitor);
418
419   /// getDescString - Return a string used to compose global names and labels.
420   ///
421   virtual const char *getDescString() const;
422
423   /// getTypeString - Return a string used to label this descriptor's type.
424   ///
425   virtual const char *getTypeString() const;
426
427 #ifndef NDEBUG
428   virtual void dump();
429 #endif
430 };
431
432 //===----------------------------------------------------------------------===//
433 /// SubrangeDesc - This class packages debug information associated with integer
434 /// value ranges.
435 class SubrangeDesc : public DebugInfoDesc {
436 private:
437   int64_t Lo;                           // Low value of range.
438   int64_t Hi;                           // High value of range.
439
440 public:
441   SubrangeDesc();
442   
443   // Accessors
444   int64_t getLo()                            const { return Lo; }
445   int64_t getHi()                            const { return Hi; }
446   void setLo(int64_t L)                            { Lo = L; }
447   void setHi(int64_t H)                            { Hi = H; }
448
449   // Implement isa/cast/dyncast.
450   static bool classof(const SubrangeDesc *) { return true; }
451   static bool classof(const DebugInfoDesc *D);
452   
453   /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
454   ///
455   virtual void ApplyToFields(DIVisitor *Visitor);
456
457   /// getDescString - Return a string used to compose global names and labels.
458   ///
459   virtual const char *getDescString() const;
460     
461   /// getTypeString - Return a string used to label this descriptor's type.
462   ///
463   virtual const char *getTypeString() const;
464
465 #ifndef NDEBUG
466   virtual void dump();
467 #endif
468 };
469
470 //===----------------------------------------------------------------------===//
471 /// EnumeratorDesc - This class packages debug information associated with
472 /// named integer constants.
473 class EnumeratorDesc : public DebugInfoDesc {
474 private:
475   std::string Name;                     // Enumerator name.
476   int64_t Value;                        // Enumerator value.
477
478 public:
479   EnumeratorDesc();
480   
481   // Accessors
482   const std::string &getName()               const { return Name; }
483   int64_t getValue()                         const { return Value; }
484   void setName(const std::string &N)               { Name = N; }
485   void setValue(int64_t V)                         { Value = V; }
486
487   // Implement isa/cast/dyncast.
488   static bool classof(const EnumeratorDesc *) { return true; }
489   static bool classof(const DebugInfoDesc *D);
490   
491   /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
492   ///
493   virtual void ApplyToFields(DIVisitor *Visitor);
494
495   /// getDescString - Return a string used to compose global names and labels.
496   ///
497   virtual const char *getDescString() const;
498     
499   /// getTypeString - Return a string used to label this descriptor's type.
500   ///
501   virtual const char *getTypeString() const;
502
503 #ifndef NDEBUG
504   virtual void dump();
505 #endif
506 };
507
508 //===----------------------------------------------------------------------===//
509 /// VariableDesc - This class packages debug information associated with a
510 /// subprogram variable.
511 ///
512 class VariableDesc : public DebugInfoDesc {
513 private:
514   DebugInfoDesc *Context;               // Context debug descriptor.
515   std::string Name;                     // Type name (may be empty.)
516   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
517   unsigned Line;                        // Defined line# (may be zero.)
518   TypeDesc *TyDesc;                     // Type of variable.
519
520 public:
521   VariableDesc(unsigned T);
522
523   // Accessors
524   DebugInfoDesc *getContext()                const { return Context; }
525   const std::string &getName()               const { return Name; }
526   CompileUnitDesc *getFile()                 const { return File; }
527   unsigned getLine()                         const { return Line; }
528   TypeDesc *getType()                        const { return TyDesc; }
529   void setContext(DebugInfoDesc *C)                { Context = C; }
530   void setName(const std::string &N)               { Name = N; }
531   void setFile(CompileUnitDesc *U)                 { File = U; }
532   void setLine(unsigned L)                         { Line = L; }
533   void setType(TypeDesc *T)                        { TyDesc = T; }
534   
535   // Implement isa/cast/dyncast.
536   static bool classof(const VariableDesc *) { return true; }
537   static bool classof(const DebugInfoDesc *D);
538   
539   /// ApplyToFields - Target the visitor to the fields of the VariableDesc.
540   ///
541   virtual void ApplyToFields(DIVisitor *Visitor);
542
543   /// getDescString - Return a string used to compose global names and labels.
544   ///
545   virtual const char *getDescString() const;
546
547   /// getTypeString - Return a string used to label this descriptor's type.
548   ///
549   virtual const char *getTypeString() const;
550   
551 #ifndef NDEBUG
552   virtual void dump();
553 #endif
554 };
555
556 //===----------------------------------------------------------------------===//
557 /// GlobalDesc - This class is the base descriptor for global functions and
558 /// variables.
559 class GlobalDesc : public AnchoredDesc {
560 private:
561   DebugInfoDesc *Context;               // Context debug descriptor.
562   std::string Name;                     // Global name.
563   CompileUnitDesc *File;                // Defined compile unit (may be NULL.)
564   unsigned Line;                        // Defined line# (may be zero.)
565   TypeDesc *TyDesc;                     // Type debug descriptor.
566   bool IsStatic;                        // Is the global a static.
567   bool IsDefinition;                    // Is the global defined in context.
568   
569 protected:
570   GlobalDesc(unsigned T);
571
572 public:
573   // Accessors
574   DebugInfoDesc *getContext()                const { return Context; }
575   const std::string &getName()               const { return Name; }
576   CompileUnitDesc *getFile()                 const { return File; }
577   unsigned getLine()                         const { return Line; }
578   TypeDesc *getType()                        const { return TyDesc; }
579   bool isStatic()                            const { return IsStatic; }
580   bool isDefinition()                        const { return IsDefinition; }
581   void setContext(DebugInfoDesc *C)                { Context = C; }
582   void setName(const std::string &N)               { Name = N; }
583   void setFile(CompileUnitDesc *U)                 { File = U; }
584   void setLine(unsigned L)                         { Line = L; }
585   void setType(TypeDesc *T)                        { TyDesc = T; }
586   void setIsStatic(bool IS)                        { IsStatic = IS; }
587   void setIsDefinition(bool ID)                    { IsDefinition = ID; }
588
589   /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
590   ///
591   virtual void ApplyToFields(DIVisitor *Visitor);
592 };
593
594 //===----------------------------------------------------------------------===//
595 /// GlobalVariableDesc - This class packages debug information associated with a
596 /// GlobalVariable.
597 class GlobalVariableDesc : public GlobalDesc {
598 private:
599   GlobalVariable *Global;               // llvm global.
600   
601 public:
602   GlobalVariableDesc();
603
604   // Accessors.
605   GlobalVariable *getGlobalVariable()        const { return Global; }
606   void setGlobalVariable(GlobalVariable *GV)       { Global = GV; }
607  
608   // Implement isa/cast/dyncast.
609   static bool classof(const GlobalVariableDesc *) { return true; }
610   static bool classof(const DebugInfoDesc *D);
611   
612   /// ApplyToFields - Target the visitor to the fields of the
613   /// GlobalVariableDesc.
614   virtual void ApplyToFields(DIVisitor *Visitor);
615
616   /// getDescString - Return a string used to compose global names and labels.
617   ///
618   virtual const char *getDescString() const;
619
620   /// getTypeString - Return a string used to label this descriptor's type.
621   ///
622   virtual const char *getTypeString() const;
623   
624   /// getAnchorString - Return a string used to label this descriptor's anchor.
625   ///
626   static const char *AnchorString;
627   virtual const char *getAnchorString() const;
628     
629 #ifndef NDEBUG
630   virtual void dump();
631 #endif
632 };
633
634 //===----------------------------------------------------------------------===//
635 /// SubprogramDesc - This class packages debug information associated with a
636 /// subprogram/function.
637 class SubprogramDesc : public GlobalDesc {
638 private:
639   
640 public:
641   SubprogramDesc();
642   
643   // Accessors
644   
645   // Implement isa/cast/dyncast.
646   static bool classof(const SubprogramDesc *) { return true; }
647   static bool classof(const DebugInfoDesc *D);
648   
649   /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
650   ///
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 /// BlockDesc - This descriptor groups variables and blocks nested in a block.
673 ///
674 class BlockDesc : public DebugInfoDesc {
675 private:
676   DebugInfoDesc *Context;               // Context debug descriptor.
677
678 public:
679   BlockDesc();
680   
681   // Accessors
682   DebugInfoDesc *getContext()                const { return Context; }
683   void setContext(DebugInfoDesc *C)                { Context = C; }
684   
685   // Implement isa/cast/dyncast.
686   static bool classof(const BlockDesc *) { return true; }
687   static bool classof(const DebugInfoDesc *D);
688   
689   /// ApplyToFields - Target the visitor to the fields of the BlockDesc.
690   ///
691   virtual void ApplyToFields(DIVisitor *Visitor);
692
693   /// getDescString - Return a string used to compose global names and labels.
694   ///
695   virtual const char *getDescString() const;
696
697   /// getTypeString - Return a string used to label this descriptor's type.
698   ///
699   virtual const char *getTypeString() const;
700     
701 #ifndef NDEBUG
702   virtual void dump();
703 #endif
704 };
705
706 //===----------------------------------------------------------------------===//
707 /// DIDeserializer - This class is responsible for casting GlobalVariables
708 /// into DebugInfoDesc objects.
709 class DIDeserializer {
710 private:
711   std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
712                                         // Previously defined gloabls.
713   
714 public:
715   DIDeserializer() {}
716   ~DIDeserializer() {}
717   
718   /// Deserialize - Reconstitute a GlobalVariable into it's component
719   /// DebugInfoDesc objects.
720   DebugInfoDesc *Deserialize(Value *V);
721   DebugInfoDesc *Deserialize(GlobalVariable *GV);
722 };
723
724 //===----------------------------------------------------------------------===//
725 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
726 /// into GlobalVariables.
727 class DISerializer {
728 private:
729   Module *M;                            // Definition space module.
730   PointerType *StrPtrTy;                // A "sbyte *" type.  Created lazily.
731   PointerType *EmptyStructPtrTy;        // A "{ }*" type.  Created lazily.
732   std::map<unsigned, StructType *> TagTypes;
733                                         // Types per Tag.  Created lazily.
734   std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
735                                         // Previously defined descriptors.
736   std::map<const std::string, Constant *> StringCache;
737                                         // Previously defined strings.
738                                           
739 public:
740   DISerializer()
741   : M(NULL)
742   , StrPtrTy(NULL)
743   , EmptyStructPtrTy(NULL)
744   , TagTypes()
745   , DescGlobals()
746   , StringCache()
747   {}
748   ~DISerializer() {}
749   
750   // Accessors
751   Module *getModule()        const { return M; };
752   void setModule(Module *module)  { M = module; }
753
754   /// getStrPtrType - Return a "sbyte *" type.
755   ///
756   const PointerType *getStrPtrType();
757   
758   /// getEmptyStructPtrType - Return a "{ }*" type.
759   ///
760   const PointerType *getEmptyStructPtrType();
761   
762   /// getTagType - Return the type describing the specified descriptor (via
763   /// tag.)
764   const StructType *getTagType(DebugInfoDesc *DD);
765   
766   /// getString - Construct the string as constant string global.
767   ///
768   Constant *getString(const std::string &String);
769   
770   /// Serialize - Recursively cast the specified descriptor into a
771   /// GlobalVariable so that it can be serialized to a .bc or .ll file.
772   GlobalVariable *Serialize(DebugInfoDesc *DD);
773 };
774
775 //===----------------------------------------------------------------------===//
776 /// DIVerifier - This class is responsible for verifying the given network of
777 /// GlobalVariables are valid as DebugInfoDesc objects.
778 class DIVerifier {
779 private:
780   enum {
781     Unknown = 0,
782     Invalid,
783     Valid
784   };
785   std::map<GlobalVariable *, unsigned> Validity;// Tracks prior results.
786   std::map<unsigned, unsigned> Counts;  // Count of fields per Tag type.
787   
788 public:
789   DIVerifier()
790   : Validity()
791   , Counts()
792   {}
793   ~DIVerifier() {}
794   
795   /// Verify - Return true if the GlobalVariable appears to be a valid
796   /// serialization of a DebugInfoDesc.
797   bool Verify(Value *V);
798   bool Verify(GlobalVariable *GV);
799 };
800
801 //===----------------------------------------------------------------------===//
802 /// SourceLineInfo - This class is used to record source line correspondence.
803 ///
804 class SourceLineInfo {
805 private:
806   unsigned Line;                        // Source line number.
807   unsigned Column;                      // Source column.
808   unsigned SourceID;                    // Source ID number.
809   unsigned LabelID;                     // Label in code ID number.
810
811 public:
812   SourceLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
813   : Line(L), Column(C), SourceID(S), LabelID(I) {}
814   
815   // Accessors
816   unsigned getLine()     const { return Line; }
817   unsigned getColumn()   const { return Column; }
818   unsigned getSourceID() const { return SourceID; }
819   unsigned getLabelID()  const { return LabelID; }
820 };
821
822 //===----------------------------------------------------------------------===//
823 /// SourceFileInfo - This class is used to track source information.
824 ///
825 class SourceFileInfo {
826 private:
827   unsigned DirectoryID;                 // Directory ID number.
828   std::string Name;                     // File name (not including directory.)
829   
830 public:
831   SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
832             
833   // Accessors
834   unsigned getDirectoryID()    const { return DirectoryID; }
835   const std::string &getName() const { return Name; }
836
837   /// operator== - Used by UniqueVector to locate entry.
838   ///
839   bool operator==(const SourceFileInfo &SI) const {
840     return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
841   }
842
843   /// operator< - Used by UniqueVector to locate entry.
844   ///
845   bool operator<(const SourceFileInfo &SI) const {
846     return getDirectoryID() < SI.getDirectoryID() ||
847           (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
848   }
849 };
850
851 //===----------------------------------------------------------------------===//
852 /// DebugVariable - This class is used to track local variable information.
853 ///
854 class DebugVariable {
855 private:
856   VariableDesc *Desc;                   // Variable Descriptor.
857   unsigned FrameIndex;                  // Variable frame index.
858
859 public:
860   DebugVariable(VariableDesc *D, unsigned I)
861   : Desc(D)
862   , FrameIndex(I)
863   {}
864   
865   // Accessors.
866   VariableDesc *getDesc()  const { return Desc; }
867   unsigned getFrameIndex() const { return FrameIndex; }
868 };
869
870 //===----------------------------------------------------------------------===//
871 /// DebugScope - This class is used to track scope information.
872 ///
873 class DebugScope {
874 private:
875   DebugScope *Parent;                   // Parent to this scope.
876   DebugInfoDesc *Desc;                  // Debug info descriptor for scope.
877                                         // Either subprogram or block.
878   unsigned StartLabelID;                // Label ID of the beginning of scope.
879   unsigned EndLabelID;                  // Label ID of the end of scope.
880   std::vector<DebugScope *> Scopes;     // Scopes defined in scope.
881   std::vector<DebugVariable *> Variables;// Variables declared in scope.
882   
883 public:
884   DebugScope(DebugScope *P, DebugInfoDesc *D)
885   : Parent(P)
886   , Desc(D)
887   , StartLabelID(0)
888   , EndLabelID(0)
889   , Scopes()
890   , Variables()
891   {}
892   ~DebugScope();
893   
894   // Accessors.
895   DebugScope *getParent()        const { return Parent; }
896   DebugInfoDesc *getDesc()       const { return Desc; }
897   unsigned getStartLabelID()     const { return StartLabelID; }
898   unsigned getEndLabelID()       const { return EndLabelID; }
899   std::vector<DebugScope *> &getScopes() { return Scopes; }
900   std::vector<DebugVariable *> &getVariables() { return Variables; }
901   void setStartLabelID(unsigned S) { StartLabelID = S; }
902   void setEndLabelID(unsigned E)   { EndLabelID = E; }
903   
904   /// AddScope - Add a scope to the scope.
905   ///
906   void AddScope(DebugScope *S) { Scopes.push_back(S); }
907   
908   /// AddVariable - Add a variable to the scope.
909   ///
910   void AddVariable(DebugVariable *V) { Variables.push_back(V); }
911 };
912
913 //===----------------------------------------------------------------------===//
914 /// MachineDebugInfo - This class contains debug information specific to a
915 /// module.  Queries can be made by different debugging schemes and reformated
916 /// for specific use.
917 ///
918 class MachineDebugInfo : public ImmutablePass {
919 private:
920   // Use the same deserializer/verifier for the module.
921   DIDeserializer DR;
922   DIVerifier VR;
923
924   // CompileUnits - Uniquing vector for compile units.
925   UniqueVector<CompileUnitDesc *> CompileUnits;
926   
927   // Directories - Uniquing vector for directories.
928   UniqueVector<std::string> Directories;
929                                          
930   // SourceFiles - Uniquing vector for source files.
931   UniqueVector<SourceFileInfo> SourceFiles;
932
933   // Lines - List of of source line correspondence.
934   std::vector<SourceLineInfo *> Lines;
935   
936   // LabelID - Current number assigned to unique label numbers.
937   unsigned LabelID;
938   
939   // ScopeMap - Tracks the scopes in the current function.
940   std::map<DebugInfoDesc *, DebugScope *> ScopeMap;
941   
942   // RootScope - Top level scope for the current function.
943   //
944   DebugScope *RootScope;
945   
946   // FrameMoves - List of moves done by a function's prolog.  Used to construct
947   // frame maps by debug consumers.
948   std::vector<MachineMove *> FrameMoves;
949
950 public:
951   MachineDebugInfo();
952   ~MachineDebugInfo();
953   
954   /// doInitialization - Initialize the debug state for a new module.
955   ///
956   bool doInitialization();
957   
958   /// doFinalization - Tear down the debug state after completion of a module.
959   ///
960   bool doFinalization();
961   
962   /// BeginFunction - Begin gathering function debug information.
963   ///
964   void BeginFunction(MachineFunction *MF);
965   
966   /// EndFunction - Discard function debug information.
967   ///
968   void EndFunction();
969
970   /// getDescFor - Convert a Value to a debug information descriptor.
971   ///
972   // FIXME - use new Value type when available.
973   DebugInfoDesc *getDescFor(Value *V);
974   
975   /// Verify - Verify that a Value is debug information descriptor.
976   ///
977   bool Verify(Value *V);
978   
979   /// AnalyzeModule - Scan the module for global debug information.
980   ///
981   void AnalyzeModule(Module &M);
982   
983   /// hasInfo - Returns true if valid debug info is present.
984   ///
985   bool hasInfo() const { return !CompileUnits.empty(); }
986   
987   /// NextLabelID - Return the next unique label id.
988   ///
989   unsigned NextLabelID() { return ++LabelID; }
990   
991   /// RecordLabel - Records location information and associates it with a
992   /// debug label.  Returns a unique label ID used to generate a label and 
993   /// provide correspondence to the source line list.
994   unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source);
995   
996   /// RecordSource - Register a source file with debug info. Returns an source
997   /// ID.
998   unsigned RecordSource(const std::string &Directory,
999                         const std::string &Source);
1000   unsigned RecordSource(const CompileUnitDesc *CompileUnit);
1001   
1002   /// getDirectories - Return the UniqueVector of std::string representing
1003   /// directories.
1004   const UniqueVector<std::string> &getDirectories() const {
1005     return Directories;
1006   }
1007   
1008   /// getSourceFiles - Return the UniqueVector of source files. 
1009   ///
1010   const UniqueVector<SourceFileInfo> &getSourceFiles() const {
1011     return SourceFiles;
1012   }
1013   
1014   /// getSourceLines - Return a vector of source lines.  Vector index + 1
1015   /// equals label ID.
1016   const std::vector<SourceLineInfo *> &getSourceLines() const {
1017     return Lines;
1018   }
1019   
1020   /// SetupCompileUnits - Set up the unique vector of compile units.
1021   ///
1022   void SetupCompileUnits(Module &M);
1023
1024   /// getCompileUnits - Return a vector of debug compile units.
1025   ///
1026   const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
1027   
1028   /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1029   /// named GlobalVariable.
1030   std::vector<GlobalVariable*>
1031   getGlobalVariablesUsing(Module &M, const std::string &RootName);
1032
1033   /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
1034   ///
1035   template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
1036     T Desc;
1037     std::vector<GlobalVariable *> Globals =
1038                              getGlobalVariablesUsing(M, Desc.getAnchorString());
1039     std::vector<T *> AnchoredDescs;
1040     for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
1041       GlobalVariable *GV = Globals[i];
1042       
1043       // FIXME - In the short term, changes are too drastic to continue.
1044       if (DebugInfoDesc::TagFromGlobal(GV) == Desc.getTag() &&
1045           DebugInfoDesc::VersionFromGlobal(GV) == LLVMDebugVersion) {
1046         AnchoredDescs.push_back(cast<T>(DR.Deserialize(GV)));
1047       }
1048     }
1049
1050     return AnchoredDescs;
1051   }
1052   
1053   /// RecordRegionStart - Indicate the start of a region.
1054   ///
1055   unsigned RecordRegionStart(Value *V);
1056
1057   /// RecordRegionEnd - Indicate the end of a region.
1058   ///
1059   unsigned RecordRegionEnd(Value *V);
1060
1061   /// RecordVariable - Indicate the declaration of  a local variable.
1062   ///
1063   void RecordVariable(Value *V, unsigned FrameIndex);
1064   
1065   /// getRootScope - Return current functions root scope.
1066   ///
1067   DebugScope *getRootScope() { return RootScope; }
1068   
1069   /// getOrCreateScope - Returns the scope associated with the given descriptor.
1070   ///
1071   DebugScope *getOrCreateScope(DebugInfoDesc *ScopeDesc);
1072   
1073   /// getFrameMoves - Returns a reference to a list of moves done in the current
1074   /// function's prologue.  Used to construct frame maps for debug comsumers.
1075   std::vector<MachineMove *> &getFrameMoves() { return FrameMoves; }
1076
1077 }; // End class MachineDebugInfo
1078
1079 } // End llvm namespace
1080
1081 #endif