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