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,
68 //===----------------------------------------------------------------------===//
69 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
70 /// the supplied DebugInfoDesc.
74 virtual ~DIVisitor() {}
76 /// ApplyToFields - Target the visitor to each field of the debug information
78 void ApplyToFields(DebugInfoDesc *DD);
80 /// Apply - Subclasses override each of these methods to perform the
81 /// appropriate action for the type of field.
82 virtual void Apply(int &Field) = 0;
83 virtual void Apply(unsigned &Field) = 0;
84 virtual void Apply(uint64_t &Field) = 0;
85 virtual void Apply(bool &Field) = 0;
86 virtual void Apply(std::string &Field) = 0;
87 virtual void Apply(DebugInfoDesc *&Field) = 0;
88 virtual void Apply(GlobalVariable *&Field) = 0;
91 //===----------------------------------------------------------------------===//
92 /// DebugInfoDesc - This class is the base class for debug info descriptors.
96 unsigned Tag; // Content indicator. Dwarf values are
97 // used but that does not limit use to
101 DebugInfoDesc(unsigned T) : Tag(T) {}
104 virtual ~DebugInfoDesc() {}
107 unsigned getTag() const { return Tag; }
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);
113 /// DescFactory - Create an instance of debug info descriptor based on Tag.
114 /// Return NULL if not a recognized Tag.
115 static DebugInfoDesc *DescFactory(unsigned Tag);
117 /// getLinkage - get linkage appropriate for this type of descriptor.
119 virtual GlobalValue::LinkageTypes getLinkage() const;
121 //===--------------------------------------------------------------------===//
122 // Subclasses should supply the following static methods.
124 // Implement isa/cast/dyncast.
125 static bool classof(const DebugInfoDesc *) { return true; }
127 //===--------------------------------------------------------------------===//
128 // Subclasses should supply the following virtual methods.
130 /// ApplyToFields - Target the vistor to the fields of the descriptor.
132 virtual void ApplyToFields(DIVisitor *Visitor);
134 /// getDescString - Return a string used to compose global names and labels.
136 virtual const char *getDescString() const = 0;
138 /// getTypeString - Return a string used to label this descriptor's type.
140 virtual const char *getTypeString() const = 0;
143 virtual void dump() = 0;
148 //===----------------------------------------------------------------------===//
149 /// AnchorDesc - Descriptors of this class act as markers for identifying
150 /// descriptors of certain groups.
151 class AnchorDesc : public DebugInfoDesc {
153 std::string Name; // Anchor type string.
157 : DebugInfoDesc(DI_TAG_anchor)
160 AnchorDesc(const std::string &N)
161 : DebugInfoDesc(DI_TAG_anchor)
166 const std::string &getName() const { return Name; }
168 // Implement isa/cast/dyncast.
169 static bool classof(const AnchorDesc *) { return true; }
170 static bool classof(const DebugInfoDesc *D) {
171 return D->getTag() == DI_TAG_anchor;
174 /// getLinkage - get linkage appropriate for this type of descriptor.
176 virtual GlobalValue::LinkageTypes getLinkage() const;
178 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
180 virtual void ApplyToFields(DIVisitor *Visitor);
182 /// getDescString - Return a string used to compose global names and labels.
184 virtual const char *getDescString() const;
186 /// getTypeString - Return a string used to label this descriptor's type.
188 virtual const char *getTypeString() const;
195 //===----------------------------------------------------------------------===//
196 /// AnchoredDesc - This class manages anchors for a variety of top level
198 class AnchoredDesc : public DebugInfoDesc {
200 AnchorDesc *Anchor; // Anchor for all descriptors of the
205 AnchoredDesc(unsigned T);
209 AnchorDesc *getAnchor() const { return Anchor; }
210 void setAnchor(AnchorDesc *A) { Anchor = A; }
212 //===--------------------------------------------------------------------===//
213 // Subclasses should supply the following virtual methods.
215 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
217 virtual void ApplyToFields(DIVisitor *Visitor);
220 //===----------------------------------------------------------------------===//
221 /// CompileUnitDesc - This class packages debug information associated with a
222 /// source/header file.
223 class CompileUnitDesc : public AnchoredDesc {
225 unsigned DebugVersion; // LLVM debug version when produced.
226 unsigned Language; // Language number (ex. DW_LANG_C89.)
227 std::string FileName; // Source file name.
228 std::string Directory; // Source file directory.
229 std::string Producer; // Compiler string.
235 unsigned getDebugVersion() const { return DebugVersion; }
236 unsigned getLanguage() const { return Language; }
237 const std::string &getFileName() const { return FileName; }
238 const std::string &getDirectory() const { return Directory; }
239 const std::string &getProducer() const { return Producer; }
240 void setLanguage(unsigned L) { Language = L; }
241 void setFileName(const std::string &FN) { FileName = FN; }
242 void setDirectory(const std::string &D) { Directory = D; }
243 void setProducer(const std::string &P) { Producer = P; }
245 // FIXME - Need translation unit getter/setter.
247 // Implement isa/cast/dyncast.
248 static bool classof(const CompileUnitDesc *) { return true; }
249 static bool classof(const DebugInfoDesc *D) {
250 return D->getTag() == DI_TAG_compile_unit;
253 /// DebugVersionFromGlobal - Returns the version number from a compile unit
254 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
255 static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
257 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
259 virtual void ApplyToFields(DIVisitor *Visitor);
261 /// getDescString - Return a string used to compose global names and labels.
263 virtual const char *getDescString() const;
265 /// getTypeString - Return a string used to label this descriptor's type.
267 virtual const char *getTypeString() const;
269 /// getAnchorString - Return a string used to label this descriptor's anchor.
271 virtual const char *getAnchorString() const;
278 //===----------------------------------------------------------------------===//
279 /// TypeDesc - This class packages debug information associated with a type.
281 class TypeDesc : public DebugInfoDesc {
283 DebugInfoDesc *Context; // Context debug descriptor.
284 std::string Name; // Type name.
285 uint64_t Size; // Type size.
288 TypeDesc(unsigned T);
292 DebugInfoDesc *getContext() const { return Context; }
293 const std::string &getName() const { return Name; }
294 uint64_t getSize() const { return Size; }
295 void setContext(DebugInfoDesc *C) { Context = C; }
296 void setName(const std::string &N) { Name = N; }
297 void setSize(uint64_t S) { Size = S; }
299 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
301 virtual void ApplyToFields(DIVisitor *Visitor);
303 /// getDescString - Return a string used to compose global names and labels.
305 virtual const char *getDescString() const;
307 /// getTypeString - Return a string used to label this descriptor's type.
309 virtual const char *getTypeString() const;
316 //===----------------------------------------------------------------------===//
317 /// BasicTypeDesc - This class packages debug information associated with a
318 /// basic type (eg. int, bool, double.)
319 class BasicTypeDesc : public TypeDesc {
321 unsigned Encoding; // Type encoding.
327 unsigned getEncoding() const { return Encoding; }
328 void setEncoding(unsigned E) { Encoding = E; }
330 // Implement isa/cast/dyncast.
331 static bool classof(const BasicTypeDesc *) { return true; }
332 static bool classof(const DebugInfoDesc *D) {
333 return D->getTag() == DI_TAG_basictype;
336 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
338 virtual void ApplyToFields(DIVisitor *Visitor);
345 //===----------------------------------------------------------------------===//
346 /// GlobalDesc - This class is the base descriptor for global functions and
348 class GlobalDesc : public AnchoredDesc {
350 DebugInfoDesc *Context; // Context debug descriptor.
351 std::string Name; // Global name.
352 TypeDesc *TyDesc; // Type debug descriptor.
353 bool IsStatic; // Is the global a static.
354 bool IsDefinition; // Is the global defined in context.
357 GlobalDesc(unsigned T);
361 DebugInfoDesc *getContext() const { return Context; }
362 const std::string &getName() const { return Name; }
363 TypeDesc *getTypeDesc() const { return TyDesc; }
364 bool isStatic() const { return IsStatic; }
365 bool isDefinition() const { return IsDefinition; }
366 void setContext(DebugInfoDesc *C) { Context = C; }
367 void setName(const std::string &N) { Name = N; }
368 void setTypeDesc(TypeDesc *T) { TyDesc = T; }
369 void setIsStatic(bool IS) { IsStatic = IS; }
370 void setIsDefinition(bool ID) { IsDefinition = ID; }
372 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
374 virtual void ApplyToFields(DIVisitor *Visitor);
377 //===----------------------------------------------------------------------===//
378 /// GlobalVariableDesc - This class packages debug information associated with a
380 class GlobalVariableDesc : public GlobalDesc {
382 GlobalVariable *Global; // llvm global.
383 unsigned Line; // Source line number.
386 GlobalVariableDesc();
389 GlobalVariable *getGlobalVariable() const { return Global; }
390 unsigned getLine() const { return Line; }
391 void setGlobalVariable(GlobalVariable *GV) { Global = GV; }
392 void setLine(unsigned L) { Line = L; }
394 // Implement isa/cast/dyncast.
395 static bool classof(const GlobalVariableDesc *) { return true; }
396 static bool classof(const DebugInfoDesc *D) {
397 return D->getTag() == DI_TAG_global_variable;
400 /// ApplyToFields - Target the visitor to the fields of the
401 /// GlobalVariableDesc.
402 virtual void ApplyToFields(DIVisitor *Visitor);
404 /// getDescString - Return a string used to compose global names and labels.
406 virtual const char *getDescString() const;
408 /// getTypeString - Return a string used to label this descriptor's type.
410 virtual const char *getTypeString() const;
412 /// getAnchorString - Return a string used to label this descriptor's anchor.
414 virtual const char *getAnchorString() const;
421 //===----------------------------------------------------------------------===//
422 /// SubprogramDesc - This class packages debug information associated with a
423 /// subprogram/function.
424 class SubprogramDesc : public GlobalDesc {
426 // FIXME - Other attributes
432 // FIXME - Other getters/setters.
434 // Implement isa/cast/dyncast.
435 static bool classof(const SubprogramDesc *) { return true; }
436 static bool classof(const DebugInfoDesc *D) {
437 return D->getTag() == DI_TAG_subprogram;
440 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
442 virtual void ApplyToFields(DIVisitor *Visitor);
444 /// getDescString - Return a string used to compose global names and labels.
446 virtual const char *getDescString() const;
448 /// getTypeString - Return a string used to label this descriptor's type.
450 virtual const char *getTypeString() const;
452 /// getAnchorString - Return a string used to label this descriptor's anchor.
454 virtual const char *getAnchorString() const;
461 //===----------------------------------------------------------------------===//
462 /// DIDeserializer - This class is responsible for casting GlobalVariables
463 /// into DebugInfoDesc objects.
464 class DIDeserializer {
466 unsigned DebugVersion; // Version of debug information in use.
467 std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
468 // Previously defined gloabls.
471 DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
475 unsigned getDebugVersion() const { return DebugVersion; }
477 /// Deserialize - Reconstitute a GlobalVariable into it's component
478 /// DebugInfoDesc objects.
479 DebugInfoDesc *Deserialize(Value *V);
480 DebugInfoDesc *Deserialize(GlobalVariable *GV);
483 //===----------------------------------------------------------------------===//
484 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
485 /// into GlobalVariables.
488 Module *M; // Definition space module.
489 PointerType *StrPtrTy; // A "sbyte *" type. Created lazily.
490 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
491 std::map<unsigned, StructType *> TagTypes;
492 // Types per Tag. Created lazily.
493 std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
494 // Previously defined descriptors.
495 std::map<const std::string, Constant *> StringCache;
496 // Previously defined strings.
502 , EmptyStructPtrTy(NULL)
510 Module *getModule() const { return M; };
511 void setModule(Module *module) { M = module; }
513 /// getStrPtrType - Return a "sbyte *" type.
515 const PointerType *getStrPtrType();
517 /// getEmptyStructPtrType - Return a "{ }*" type.
519 const PointerType *getEmptyStructPtrType();
521 /// getTagType - Return the type describing the specified descriptor (via
523 const StructType *getTagType(DebugInfoDesc *DD);
525 /// getString - Construct the string as constant string global.
527 Constant *getString(const std::string &String);
529 /// Serialize - Recursively cast the specified descriptor into a
530 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
531 GlobalVariable *Serialize(DebugInfoDesc *DD);
534 //===----------------------------------------------------------------------===//
535 /// DIVerifier - This class is responsible for verifying the given network of
536 /// GlobalVariables are valid as DebugInfoDesc objects.
539 unsigned DebugVersion; // Version of debug information in use.
540 std::set<GlobalVariable *> Visited; // Tracks visits during recursion.
541 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
543 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
544 /// Mark markVisited otherwise.
545 bool markVisited(GlobalVariable *GV);
548 DIVerifier() : DebugVersion(LLVMDebugVersion) {}
551 /// Verify - Return true if the GlobalVariable appears to be a valid
552 /// serialization of a DebugInfoDesc.
553 bool Verify(Value *V);
554 bool Verify(GlobalVariable *GV);
557 //===----------------------------------------------------------------------===//
558 /// SourceLineInfo - This class is used to record source line correspondence.
560 class SourceLineInfo {
562 unsigned Line; // Source line number.
563 unsigned Column; // Source column.
564 unsigned SourceID; // Source ID number.
567 SourceLineInfo(unsigned L, unsigned C, unsigned S)
568 : Line(L), Column(C), SourceID(S) {}
571 unsigned getLine() const { return Line; }
572 unsigned getColumn() const { return Column; }
573 unsigned getSourceID() const { return SourceID; }
576 //===----------------------------------------------------------------------===//
577 /// SourceFileInfo - This class is used to track source information.
579 class SourceFileInfo {
581 unsigned DirectoryID; // Directory ID number.
582 std::string Name; // File name (not including directory.)
585 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
588 unsigned getDirectoryID() const { return DirectoryID; }
589 const std::string &getName() const { return Name; }
591 /// operator== - Used by UniqueVector to locate entry.
593 bool operator==(const SourceFileInfo &SI) const {
594 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
597 /// operator< - Used by UniqueVector to locate entry.
599 bool operator<(const SourceFileInfo &SI) const {
600 return getDirectoryID() < SI.getDirectoryID() ||
601 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
605 //===----------------------------------------------------------------------===//
606 /// MachineDebugInfo - This class contains debug information specific to a
607 /// module. Queries can be made by different debugging schemes and reformated
608 /// for specific use.
610 class MachineDebugInfo : public ImmutablePass {
612 // Use the same serializer/deserializer/verifier for the module.
615 // CompileUnits - Uniquing vector for compile units.
616 UniqueVector<CompileUnitDesc *> CompileUnits;
618 // Directories - Uniquing vector for directories.
619 UniqueVector<std::string> Directories;
621 // SourceFiles - Uniquing vector for source files.
622 UniqueVector<SourceFileInfo> SourceFiles;
624 // Lines - List of of source line correspondence.
625 std::vector<SourceLineInfo *> Lines;
631 /// doInitialization - Initialize the debug state for a new module.
633 bool doInitialization();
635 /// doFinalization - Tear down the debug state after completion of a module.
637 bool doFinalization();
639 /// getDescFor - Convert a Value to a debug information descriptor.
641 // FIXME - use new Value type when available.
642 DebugInfoDesc *getDescFor(Value *V);
644 /// Verify - Verify that a Value is debug information descriptor.
646 bool Verify(Value *V);
648 /// AnalyzeModule - Scan the module for global debug information.
650 void AnalyzeModule(Module &M);
652 /// hasInfo - Returns true if valid debug info is present.
654 bool hasInfo() const { return !CompileUnits.empty(); }
656 /// RecordLabel - Records location information and associates it with a
657 /// debug label. Returns a unique label ID used to generate a label and
658 /// provide correspondence to the source line list.
659 unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
660 Lines.push_back(new SourceLineInfo(Line, Column, Source));
664 /// RecordSource - Register a source file with debug info. Returns an source
666 unsigned RecordSource(const std::string &Directory,
667 const std::string &Source) {
668 unsigned DirectoryID = Directories.insert(Directory);
669 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
671 unsigned RecordSource(const CompileUnitDesc *CompileUnit) {
672 return RecordSource(CompileUnit->getDirectory(),
673 CompileUnit->getFileName());
676 /// getDirectories - Return the UniqueVector of std::string representing
678 const UniqueVector<std::string> &getDirectories() const {
682 /// getSourceFiles - Return the UniqueVector of source files.
684 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
688 /// getSourceLines - Return a vector of source lines. Vector index + 1
690 const std::vector<SourceLineInfo *> &getSourceLines() const {
694 /// SetupCompileUnits - Set up the unique vector of compile units.
696 void SetupCompileUnits(Module &M);
698 /// getCompileUnits - Return a vector of debug compile units.
700 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
702 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
703 /// named GlobalVariable.
704 std::vector<GlobalVariable*>
705 getGlobalVariablesUsing(Module &M, const std::string &RootName);
707 /// getAnchoredDescriptors - Return a vector of anchored debug descriptors.
709 template <class T>std::vector<T *> getAnchoredDescriptors(Module &M) {
711 std::vector<GlobalVariable *> Globals =
712 getGlobalVariablesUsing(M, Desc.getAnchorString());
713 std::vector<T *> AnchoredDescs;
714 for (unsigned i = 0, N = Globals.size(); i < N; ++i) {
715 AnchoredDescs.push_back(static_cast<T *>(DR.Deserialize(Globals[i])));
718 return AnchoredDescs;
721 }; // End class MachineDebugInfo
723 } // End llvm namespace