1 //===-- llvm/CodeGen/MachineDebugInfo.h -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Collect debug information for a module. This information should be in a
11 // neutral form that can be used by different debugging schemes.
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.
17 // The following information can be retrieved from the MachineDebugInfo.
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.
28 //===----------------------------------------------------------------------===//
30 #ifndef LLVM_CODEGEN_MACHINEDEBUGINFO_H
31 #define LLVM_CODEGEN_MACHINEDEBUGINFO_H
33 #include "llvm/Support/Dwarf.h"
34 #include "llvm/ADT/UniqueVector.h"
35 #include "llvm/GlobalValue.h"
36 #include "llvm/Pass.h"
37 #include "llvm/User.h"
44 //===----------------------------------------------------------------------===//
45 // Forward declarations.
53 //===----------------------------------------------------------------------===//
54 // Debug info constants.
57 LLVMDebugVersion = 1, // Current version of debug information.
58 DIInvalid = ~0U, // Invalid result indicator.
60 // DebugInfoDesc type identifying tags.
63 DI_TAG_global_variable,
69 //===----------------------------------------------------------------------===//
70 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
71 /// the supplied DebugInfoDesc.
75 virtual ~DIVisitor() {}
77 /// ApplyToFields - Target the visitor to each field of the debug information
79 void ApplyToFields(DebugInfoDesc *DD);
81 /// Apply - Subclasses override each of these methods to perform the
82 /// appropriate action for the type of field.
83 virtual void Apply(int &Field) = 0;
84 virtual void Apply(unsigned &Field) = 0;
85 virtual void Apply(uint64_t &Field) = 0;
86 virtual void Apply(bool &Field) = 0;
87 virtual void Apply(std::string &Field) = 0;
88 virtual void Apply(DebugInfoDesc *&Field) = 0;
89 virtual void Apply(GlobalVariable *&Field) = 0;
92 //===----------------------------------------------------------------------===//
93 /// DebugInfoDesc - This class is the base class for debug info descriptors.
97 unsigned Tag; // Content indicator. Dwarf values are
98 // used but that does not limit use to
102 DebugInfoDesc(unsigned T) : Tag(T) {}
105 virtual ~DebugInfoDesc() {}
108 unsigned getTag() const { return Tag; }
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);
114 /// DescFactory - Create an instance of debug info descriptor based on Tag.
115 /// Return NULL if not a recognized Tag.
116 static DebugInfoDesc *DescFactory(unsigned Tag);
118 /// getLinkage - get linkage appropriate for this type of descriptor.
120 virtual GlobalValue::LinkageTypes getLinkage() const;
122 //===--------------------------------------------------------------------===//
123 // Subclasses should supply the following static methods.
125 // Implement isa/cast/dyncast.
126 static bool classof(const DebugInfoDesc *) { return true; }
128 //===--------------------------------------------------------------------===//
129 // Subclasses should supply the following virtual methods.
131 /// ApplyToFields - Target the vistor to the fields of the descriptor.
133 virtual void ApplyToFields(DIVisitor *Visitor);
135 /// getDescString - Return a string used to compose global names and labels.
137 virtual const char *getDescString() const = 0;
139 /// getTypeString - Return a string used to label this descriptor's type.
141 virtual const char *getTypeString() const = 0;
144 virtual void dump() = 0;
149 //===----------------------------------------------------------------------===//
150 /// AnchorDesc - Descriptors of this class act as markers for identifying
151 /// descriptors of certain groups.
152 class AnchorDesc : public DebugInfoDesc {
154 std::string Name; // Anchor type string.
158 : DebugInfoDesc(DI_TAG_anchor)
161 AnchorDesc(const std::string &N)
162 : DebugInfoDesc(DI_TAG_anchor)
167 const std::string &getName() const { return Name; }
169 // Implement isa/cast/dyncast.
170 static bool classof(const AnchorDesc *) { return true; }
171 static bool classof(const DebugInfoDesc *D) {
172 return D->getTag() == DI_TAG_anchor;
175 /// getLinkage - get linkage appropriate for this type of descriptor.
177 virtual GlobalValue::LinkageTypes getLinkage() const;
179 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
181 virtual void ApplyToFields(DIVisitor *Visitor);
183 /// getDescString - Return a string used to compose global names and labels.
185 virtual const char *getDescString() const;
187 /// getTypeString - Return a string used to label this descriptor's type.
189 virtual const char *getTypeString() const;
196 //===----------------------------------------------------------------------===//
197 /// AnchoredDesc - This class manages anchors for a variety of top level
199 class AnchoredDesc : public DebugInfoDesc {
201 AnchorDesc *Anchor; // Anchor for all descriptors of the
206 AnchoredDesc(unsigned T);
210 AnchorDesc *getAnchor() const { return Anchor; }
211 void setAnchor(AnchorDesc *A) { Anchor = A; }
213 //===--------------------------------------------------------------------===//
214 // Subclasses should supply the following virtual methods.
216 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
218 virtual void ApplyToFields(DIVisitor *Visitor);
221 //===----------------------------------------------------------------------===//
222 /// CompileUnitDesc - This class packages debug information associated with a
223 /// source/header file.
224 class CompileUnitDesc : public AnchoredDesc {
226 unsigned DebugVersion; // LLVM debug version when produced.
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.
236 unsigned getDebugVersion() const { return DebugVersion; }
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; }
246 // FIXME - Need translation unit getter/setter.
248 // Implement isa/cast/dyncast.
249 static bool classof(const CompileUnitDesc *) { return true; }
250 static bool classof(const DebugInfoDesc *D) {
251 return D->getTag() == DI_TAG_compile_unit;
254 /// DebugVersionFromGlobal - Returns the version number from a compile unit
255 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
256 static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
258 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
260 virtual void ApplyToFields(DIVisitor *Visitor);
262 /// getDescString - Return a string used to compose global names and labels.
264 virtual const char *getDescString() const;
266 /// getTypeString - Return a string used to label this descriptor's type.
268 virtual const char *getTypeString() const;
270 /// getAnchorString - Return a string used to label this descriptor's anchor.
272 virtual const char *getAnchorString() const;
279 //===----------------------------------------------------------------------===//
280 /// TypeDesc - This class packages debug information associated with a type.
282 class TypeDesc : public DebugInfoDesc {
284 DebugInfoDesc *Context; // Context debug descriptor.
285 std::string Name; // Type name.
286 uint64_t Size; // Type size.
289 TypeDesc(unsigned T);
293 DebugInfoDesc *getContext() const { return Context; }
294 const std::string &getName() const { return Name; }
295 uint64_t getSize() const { return Size; }
296 void setContext(DebugInfoDesc *C) { Context = C; }
297 void setName(const std::string &N) { Name = N; }
298 void setSize(uint64_t S) { Size = S; }
300 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
302 virtual void ApplyToFields(DIVisitor *Visitor);
304 /// getDescString - Return a string used to compose global names and labels.
306 virtual const char *getDescString() const;
308 /// getTypeString - Return a string used to label this descriptor's type.
310 virtual const char *getTypeString() const;
317 //===----------------------------------------------------------------------===//
318 /// BasicTypeDesc - This class packages debug information associated with a
319 /// basic type (eg. int, bool, double.)
320 class BasicTypeDesc : public TypeDesc {
322 unsigned Encoding; // Type encoding.
328 unsigned getEncoding() const { return Encoding; }
329 void setEncoding(unsigned E) { Encoding = E; }
331 // Implement isa/cast/dyncast.
332 static bool classof(const BasicTypeDesc *) { return true; }
333 static bool classof(const DebugInfoDesc *D) {
334 return D->getTag() == DI_TAG_basictype;
337 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
339 virtual void ApplyToFields(DIVisitor *Visitor);
347 //===----------------------------------------------------------------------===//
348 /// TypedefDesc - This class packages debug information associated with a
350 class TypedefDesc : public TypeDesc {
352 TypeDesc *FromType; // Type derived from.
353 CompileUnitDesc *File; // Declared compile unit.
354 int Line; // Declared line#.
360 TypeDesc *getFromType() const { return FromType; }
361 CompileUnitDesc *getFile() const { return File; }
362 int getLine() const { return Line; }
363 void setFromType(TypeDesc *F) { FromType = F; }
364 void setFile(CompileUnitDesc *U) { File = U; }
365 void setLine(int L) { Line = L; }
367 // Implement isa/cast/dyncast.
368 static bool classof(const TypedefDesc *) { return true; }
369 static bool classof(const DebugInfoDesc *D) {
370 return D->getTag() == DI_TAG_typedef;
373 /// ApplyToFields - Target the visitor to the fields of the TypedefDesc.
375 virtual void ApplyToFields(DIVisitor *Visitor);
382 //===----------------------------------------------------------------------===//
383 /// GlobalDesc - This class is the base descriptor for global functions and
385 class GlobalDesc : public AnchoredDesc {
387 DebugInfoDesc *Context; // Context debug descriptor.
388 std::string Name; // Global name.
389 TypeDesc *TyDesc; // Type debug descriptor.
390 bool IsStatic; // Is the global a static.
391 bool IsDefinition; // Is the global defined in context.
394 GlobalDesc(unsigned T);
398 DebugInfoDesc *getContext() const { return Context; }
399 const std::string &getName() const { return Name; }
400 TypeDesc *getTypeDesc() const { return TyDesc; }
401 bool isStatic() const { return IsStatic; }
402 bool isDefinition() const { return IsDefinition; }
403 void setContext(DebugInfoDesc *C) { Context = C; }
404 void setName(const std::string &N) { Name = N; }
405 void setTypeDesc(TypeDesc *T) { TyDesc = T; }
406 void setIsStatic(bool IS) { IsStatic = IS; }
407 void setIsDefinition(bool ID) { IsDefinition = ID; }
409 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
411 virtual void ApplyToFields(DIVisitor *Visitor);
414 //===----------------------------------------------------------------------===//
415 /// GlobalVariableDesc - This class packages debug information associated with a
417 class GlobalVariableDesc : public GlobalDesc {
419 GlobalVariable *Global; // llvm global.
420 unsigned Line; // Source line number.
423 GlobalVariableDesc();
426 GlobalVariable *getGlobalVariable() const { return Global; }
427 unsigned getLine() const { return Line; }
428 void setGlobalVariable(GlobalVariable *GV) { Global = GV; }
429 void setLine(unsigned L) { Line = L; }
431 // Implement isa/cast/dyncast.
432 static bool classof(const GlobalVariableDesc *) { return true; }
433 static bool classof(const DebugInfoDesc *D) {
434 return D->getTag() == DI_TAG_global_variable;
437 /// ApplyToFields - Target the visitor to the fields of the
438 /// GlobalVariableDesc.
439 virtual void ApplyToFields(DIVisitor *Visitor);
441 /// getDescString - Return a string used to compose global names and labels.
443 virtual const char *getDescString() const;
445 /// getTypeString - Return a string used to label this descriptor's type.
447 virtual const char *getTypeString() const;
449 /// getAnchorString - Return a string used to label this descriptor's anchor.
451 virtual const char *getAnchorString() const;
458 //===----------------------------------------------------------------------===//
459 /// SubprogramDesc - This class packages debug information associated with a
460 /// subprogram/function.
461 class SubprogramDesc : public GlobalDesc {
463 // FIXME - Other attributes
469 // FIXME - Other getters/setters.
471 // Implement isa/cast/dyncast.
472 static bool classof(const SubprogramDesc *) { return true; }
473 static bool classof(const DebugInfoDesc *D) {
474 return D->getTag() == DI_TAG_subprogram;
477 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
479 virtual void ApplyToFields(DIVisitor *Visitor);
481 /// getDescString - Return a string used to compose global names and labels.
483 virtual const char *getDescString() const;
485 /// getTypeString - Return a string used to label this descriptor's type.
487 virtual const char *getTypeString() const;
489 /// getAnchorString - Return a string used to label this descriptor's anchor.
491 virtual const char *getAnchorString() const;
498 //===----------------------------------------------------------------------===//
499 /// DIDeserializer - This class is responsible for casting GlobalVariables
500 /// into DebugInfoDesc objects.
501 class DIDeserializer {
503 unsigned DebugVersion; // Version of debug information in use.
504 std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
505 // Previously defined gloabls.
508 DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
512 unsigned getDebugVersion() const { return DebugVersion; }
514 /// Deserialize - Reconstitute a GlobalVariable into it's component
515 /// DebugInfoDesc objects.
516 DebugInfoDesc *Deserialize(Value *V);
517 DebugInfoDesc *Deserialize(GlobalVariable *GV);
520 //===----------------------------------------------------------------------===//
521 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
522 /// into GlobalVariables.
525 Module *M; // Definition space module.
526 PointerType *StrPtrTy; // A "sbyte *" type. Created lazily.
527 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
528 std::map<unsigned, StructType *> TagTypes;
529 // Types per Tag. Created lazily.
530 std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
531 // Previously defined descriptors.
532 std::map<const std::string, Constant *> StringCache;
533 // Previously defined strings.
539 , EmptyStructPtrTy(NULL)
547 Module *getModule() const { return M; };
548 void setModule(Module *module) { M = module; }
550 /// getStrPtrType - Return a "sbyte *" type.
552 const PointerType *getStrPtrType();
554 /// getEmptyStructPtrType - Return a "{ }*" type.
556 const PointerType *getEmptyStructPtrType();
558 /// getTagType - Return the type describing the specified descriptor (via
560 const StructType *getTagType(DebugInfoDesc *DD);
562 /// getString - Construct the string as constant string global.
564 Constant *getString(const std::string &String);
566 /// Serialize - Recursively cast the specified descriptor into a
567 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
568 GlobalVariable *Serialize(DebugInfoDesc *DD);
571 //===----------------------------------------------------------------------===//
572 /// DIVerifier - This class is responsible for verifying the given network of
573 /// GlobalVariables are valid as DebugInfoDesc objects.
576 unsigned DebugVersion; // Version of debug information in use.
577 std::set<GlobalVariable *> Visited; // Tracks visits during recursion.
578 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
580 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
581 /// Mark markVisited otherwise.
582 bool markVisited(GlobalVariable *GV);
585 DIVerifier() : DebugVersion(LLVMDebugVersion) {}
588 /// Verify - Return true if the GlobalVariable appears to be a valid
589 /// serialization of a DebugInfoDesc.
590 bool Verify(Value *V);
591 bool Verify(GlobalVariable *GV);
594 //===----------------------------------------------------------------------===//
595 /// SourceLineInfo - This class is used to record source line correspondence.
597 class SourceLineInfo {
599 unsigned Line; // Source line number.
600 unsigned Column; // Source column.
601 unsigned SourceID; // Source ID number.
604 SourceLineInfo(unsigned L, unsigned C, unsigned S)
605 : Line(L), Column(C), SourceID(S) {}
608 unsigned getLine() const { return Line; }
609 unsigned getColumn() const { return Column; }
610 unsigned getSourceID() const { return SourceID; }
613 //===----------------------------------------------------------------------===//
614 /// SourceFileInfo - This class is used to track source information.
616 class SourceFileInfo {
618 unsigned DirectoryID; // Directory ID number.
619 std::string Name; // File name (not including directory.)
622 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
625 unsigned getDirectoryID() const { return DirectoryID; }
626 const std::string &getName() const { return Name; }
628 /// operator== - Used by UniqueVector to locate entry.
630 bool operator==(const SourceFileInfo &SI) const {
631 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
634 /// operator< - Used by UniqueVector to locate entry.
636 bool operator<(const SourceFileInfo &SI) const {
637 return getDirectoryID() < SI.getDirectoryID() ||
638 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
642 //===----------------------------------------------------------------------===//
643 /// MachineDebugInfo - This class contains debug information specific to a
644 /// module. Queries can be made by different debugging schemes and reformated
645 /// for specific use.
647 class MachineDebugInfo : public ImmutablePass {
649 // Use the same serializer/deserializer/verifier for the module.
652 // CompileUnits - Uniquing vector for compile units.
653 UniqueVector<CompileUnitDesc *> CompileUnits;
655 // Directories - Uniquing vector for directories.
656 UniqueVector<std::string> Directories;
658 // SourceFiles - Uniquing vector for source files.
659 UniqueVector<SourceFileInfo> SourceFiles;
661 // Lines - List of of source line correspondence.
662 std::vector<SourceLineInfo *> Lines;
668 /// doInitialization - Initialize the debug state for a new module.
670 bool doInitialization();
672 /// doFinalization - Tear down the debug state after completion of a module.
674 bool doFinalization();
676 /// getDescFor - Convert a Value to a debug information descriptor.
678 // FIXME - use new Value type when available.
679 DebugInfoDesc *getDescFor(Value *V);
681 /// Verify - Verify that a Value is debug information descriptor.
683 bool Verify(Value *V);
685 /// AnalyzeModule - Scan the module for global debug information.
687 void AnalyzeModule(Module &M);
689 /// hasInfo - Returns true if valid debug info is present.
691 bool hasInfo() const { return !CompileUnits.empty(); }
693 /// RecordLabel - Records location information and associates it with a
694 /// debug label. Returns a unique label ID used to generate a label and
695 /// provide correspondence to the source line list.
696 unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
697 Lines.push_back(new SourceLineInfo(Line, Column, Source));
701 /// RecordSource - Register a source file with debug info. Returns an source
703 unsigned RecordSource(const std::string &Directory,
704 const std::string &Source) {
705 unsigned DirectoryID = Directories.insert(Directory);
706 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
708 unsigned RecordSource(const CompileUnitDesc *CompileUnit) {
709 return RecordSource(CompileUnit->getDirectory(),
710 CompileUnit->getFileName());
713 /// getDirectories - Return the UniqueVector of std::string representing
715 const UniqueVector<std::string> &getDirectories() const {
719 /// getSourceFiles - Return the UniqueVector of source files.
721 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
725 /// getSourceLines - Return a vector of source lines. Vector index + 1
727 const std::vector<SourceLineInfo *> &getSourceLines() const {
731 /// SetupCompileUnits - Set up the unique vector of compile units.
733 void SetupCompileUnits(Module &M);
735 /// getCompileUnits - Return a vector of debug compile units.
737 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
739 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
740 /// named GlobalVariable.
741 std::vector<GlobalVariable*>
742 getGlobalVariablesUsing(Module &M, const std::string &RootName);
744 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
746 template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
748 std::vector<GlobalVariable *> Globals =
749 getGlobalVariablesUsing(M, Desc.getAnchorString());
750 std::vector<T *> AnchoredDescs;
751 for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
752 AnchoredDescs.push_back(cast<T>(DR.Deserialize(Globals[i])));
755 return AnchoredDescs;
758 }; // End class MachineDebugInfo
760 } // End llvm namespace