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