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.
61 // FIXME - Change over with gcc4.
64 DI_TAG_compile_unit = DW_TAG_compile_unit,
65 DI_TAG_global_variable = DW_TAG_variable,
66 DI_TAG_subprogram = DW_TAG_subprogram
69 DI_TAG_global_variable,
74 //===----------------------------------------------------------------------===//
75 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
76 /// the supplied DebugInfoDesc.
80 virtual ~DIVisitor() {}
83 /// ApplyToFields - Target the visitor to each field of the debug information
85 void ApplyToFields(DebugInfoDesc *DD);
87 /// Apply - Subclasses override each of these methods to perform the
88 /// appropriate action for the type of field.
89 virtual void Apply(int &Field) = 0;
90 virtual void Apply(unsigned &Field) = 0;
91 virtual void Apply(bool &Field) = 0;
92 virtual void Apply(std::string &Field) = 0;
93 virtual void Apply(DebugInfoDesc *&Field) = 0;
94 virtual void Apply(GlobalVariable *&Field) = 0;
97 //===----------------------------------------------------------------------===//
98 /// DebugInfoDesc - This class is the base class for debug info descriptors.
100 class DebugInfoDesc {
102 unsigned Tag; // Content indicator. Dwarf values are
103 // used but that does not limit use to
107 DebugInfoDesc(unsigned T) : Tag(T) {}
110 virtual ~DebugInfoDesc() {}
113 unsigned getTag() const { return Tag; }
115 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
116 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
117 static unsigned TagFromGlobal(GlobalVariable *GV);
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);
123 /// getLinkage - get linkage appropriate for this type of descriptor.
125 virtual GlobalValue::LinkageTypes getLinkage() const;
127 //===--------------------------------------------------------------------===//
128 // Subclasses should supply the following static methods.
130 // Implement isa/cast/dyncast.
131 static bool classof(const DebugInfoDesc *) { return true; }
133 //===--------------------------------------------------------------------===//
134 // Subclasses should supply the following virtual methods.
136 /// ApplyToFields - Target the vistor to the fields of the descriptor.
138 virtual void ApplyToFields(DIVisitor *Visitor);
140 /// getDescString - Return a string used to compose global names and labels.
142 virtual const char *getDescString() const = 0;
144 /// getTypeString - Return a string used to label this descriptor's type.
146 virtual const char *getTypeString() const = 0;
149 virtual void dump() = 0;
154 //===----------------------------------------------------------------------===//
155 /// AnchorDesc - Descriptors of this class act as markers for identifying
156 /// descriptors of certain groups.
157 class AnchorDesc : public DebugInfoDesc {
159 std::string Name; // Anchor type string.
163 : DebugInfoDesc(DI_TAG_anchor)
166 AnchorDesc(const std::string &N)
167 : DebugInfoDesc(DI_TAG_anchor)
172 const std::string &getName() const { return Name; }
174 // Implement isa/cast/dyncast.
175 static bool classof(const AnchorDesc *) { return true; }
176 static bool classof(const DebugInfoDesc *D) {
177 return D->getTag() == DI_TAG_anchor;
180 /// getLinkage - get linkage appropriate for this type of descriptor.
182 virtual GlobalValue::LinkageTypes getLinkage() const;
184 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
186 virtual void ApplyToFields(DIVisitor *Visitor);
188 /// getDescString - Return a string used to compose global names and labels.
190 virtual const char *getDescString() const;
192 /// getTypeString - Return a string used to label this descriptor's type.
194 virtual const char *getTypeString() const;
201 //===----------------------------------------------------------------------===//
202 /// AnchoredDesc - This class manages anchors for a variety of top level
204 class AnchoredDesc : public DebugInfoDesc {
206 AnchorDesc *Anchor; // Anchor for all descriptors of the
211 AnchoredDesc(unsigned T);
215 AnchorDesc *getAnchor() const { return Anchor; }
216 void setAnchor(AnchorDesc *A) { Anchor = A; }
218 //===--------------------------------------------------------------------===//
219 // Subclasses should supply the following virtual methods.
221 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
223 virtual void ApplyToFields(DIVisitor *Visitor);
226 //===----------------------------------------------------------------------===//
227 /// CompileUnitDesc - This class packages debug information associated with a
228 /// source/header file.
229 class CompileUnitDesc : public AnchoredDesc {
231 unsigned DebugVersion; // LLVM debug version when produced.
232 unsigned Language; // Language number (ex. DW_LANG_C89.)
233 std::string FileName; // Source file name.
234 std::string Directory; // Source file directory.
235 std::string Producer; // Compiler string.
241 unsigned getDebugVersion() const { return DebugVersion; }
242 unsigned getLanguage() const { return Language; }
243 const std::string &getFileName() const { return FileName; }
244 const std::string &getDirectory() const { return Directory; }
245 const std::string &getProducer() const { return Producer; }
246 void setLanguage(unsigned L) { Language = L; }
247 void setFileName(const std::string &FN) { FileName = FN; }
248 void setDirectory(const std::string &D) { Directory = D; }
249 void setProducer(const std::string &P) { Producer = P; }
251 // FIXME - Need translation unit getter/setter.
253 // Implement isa/cast/dyncast.
254 static bool classof(const CompileUnitDesc *) { return true; }
255 static bool classof(const DebugInfoDesc *D) {
256 return D->getTag() == DI_TAG_compile_unit;
259 /// DebugVersionFromGlobal - Returns the version number from a compile unit
260 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
261 static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
263 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
265 virtual void ApplyToFields(DIVisitor *Visitor);
267 /// getDescString - Return a string used to compose global names and labels.
269 virtual const char *getDescString() const;
271 /// getTypeString - Return a string used to label this descriptor's type.
273 virtual const char *getTypeString() const;
275 /// getAnchorString - Return a string used to label this descriptor's anchor.
277 virtual const char *getAnchorString() const;
284 //===----------------------------------------------------------------------===//
285 /// GlobalDesc - This class is the base descriptor for global functions and
287 class GlobalDesc : public AnchoredDesc {
289 DebugInfoDesc *Context; // Context debug descriptor.
290 std::string Name; // Global name.
291 // FIXME - Use a descriptor.
292 GlobalVariable *TyDesc; // Type debug descriptor.
293 bool IsStatic; // Is the global a static.
294 bool IsDefinition; // Is the global defined in context.
297 GlobalDesc(unsigned T);
301 DebugInfoDesc *getContext() const { return Context; }
302 const std::string &getName() const { return Name; }
303 bool isStatic() const { return IsStatic; }
304 bool isDefinition() const { return IsDefinition; }
305 void setContext(DebugInfoDesc *C) { Context = C; }
306 void setName(const std::string &N) { Name = N; }
307 void setIsStatic(bool IS) { IsStatic = IS; }
308 void setIsDefinition(bool ID) { IsDefinition = ID; }
310 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
312 virtual void ApplyToFields(DIVisitor *Visitor);
315 //===----------------------------------------------------------------------===//
316 /// GlobalVariableDesc - This class packages debug information associated with a
318 class GlobalVariableDesc : public GlobalDesc {
320 GlobalVariable *Global; // llvm global.
323 GlobalVariableDesc();
326 GlobalVariable *getGlobalVariable() const { return Global; }
327 void setGlobalVariable(GlobalVariable *GV) { Global = GV; }
329 // Implement isa/cast/dyncast.
330 static bool classof(const GlobalVariableDesc *) { return true; }
331 static bool classof(const DebugInfoDesc *D) {
332 return D->getTag() == DI_TAG_global_variable;
335 /// ApplyToFields - Target the visitor to the fields of the
336 /// GlobalVariableDesc.
337 virtual void ApplyToFields(DIVisitor *Visitor);
339 /// getDescString - Return a string used to compose global names and labels.
341 virtual const char *getDescString() const;
343 /// getTypeString - Return a string used to label this descriptor's type.
345 virtual const char *getTypeString() const;
347 /// getAnchorString - Return a string used to label this descriptor's anchor.
349 virtual const char *getAnchorString() const;
356 //===----------------------------------------------------------------------===//
357 /// SubprogramDesc - This class packages debug information associated with a
358 /// subprogram/function.
359 class SubprogramDesc : public GlobalDesc {
361 DebugInfoDesc *Context; // Context debug descriptor.
362 std::string Name; // Subprogram name.
363 // FIXME - Use a descriptor.
364 GlobalVariable *TyDesc; // Type debug descriptor.
365 bool IsStatic; // Is the subprogram a static.
366 bool IsDefinition; // Is the subprogram defined in context.
372 DebugInfoDesc *getContext() const { return Context; }
373 const std::string &getName() const { return Name; }
374 bool isStatic() const { return IsStatic; }
375 bool isDefinition() const { return IsDefinition; }
376 void setContext(DebugInfoDesc *C) { Context = C; }
377 void setName(const std::string &N) { Name = N; }
378 void setIsStatic(bool IS) { IsStatic = IS; }
379 void setIsDefinition(bool ID) { IsDefinition = ID; }
380 // FIXME - Other getters/setters.
382 // Implement isa/cast/dyncast.
383 static bool classof(const SubprogramDesc *) { return true; }
384 static bool classof(const DebugInfoDesc *D) {
385 return D->getTag() == DI_TAG_subprogram;
388 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
390 virtual void ApplyToFields(DIVisitor *Visitor);
392 /// getDescString - Return a string used to compose global names and labels.
394 virtual const char *getDescString() const;
396 /// getTypeString - Return a string used to label this descriptor's type.
398 virtual const char *getTypeString() const;
400 /// getAnchorString - Return a string used to label this descriptor's anchor.
402 virtual const char *getAnchorString() const;
409 //===----------------------------------------------------------------------===//
410 /// DIDeserializer - This class is responsible for casting GlobalVariables
411 /// into DebugInfoDesc objects.
412 class DIDeserializer {
414 unsigned DebugVersion; // Version of debug information in use.
415 std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
416 // Previously defined gloabls.
419 DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
423 unsigned getDebugVersion() const { return DebugVersion; }
425 /// Deserialize - Reconstitute a GlobalVariable into it's component
426 /// DebugInfoDesc objects.
427 DebugInfoDesc *Deserialize(Value *V);
428 DebugInfoDesc *Deserialize(GlobalVariable *GV);
431 //===----------------------------------------------------------------------===//
432 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
433 /// into GlobalVariables.
436 Module *M; // Definition space module.
437 PointerType *StrPtrTy; // A "sbyte *" type. Created lazily.
438 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
439 std::map<unsigned, StructType *> TagTypes;
440 // Types per Tag. Created lazily.
441 std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
442 // Previously defined descriptors.
443 std::map<const std::string, Constant *> StringCache;
444 // Previously defined strings.
450 , EmptyStructPtrTy(NULL)
458 Module *getModule() const { return M; };
459 void setModule(Module *module) { M = module; }
461 /// getStrPtrType - Return a "sbyte *" type.
463 const PointerType *getStrPtrType();
465 /// getEmptyStructPtrType - Return a "{ }*" type.
467 const PointerType *getEmptyStructPtrType();
469 /// getTagType - Return the type describing the specified descriptor (via
471 const StructType *getTagType(DebugInfoDesc *DD);
473 /// getString - Construct the string as constant string global.
475 Constant *getString(const std::string &String);
477 /// Serialize - Recursively cast the specified descriptor into a
478 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
479 GlobalVariable *Serialize(DebugInfoDesc *DD);
482 //===----------------------------------------------------------------------===//
483 /// DIVerifier - This class is responsible for verifying the given network of
484 /// GlobalVariables are valid as DebugInfoDesc objects.
487 unsigned DebugVersion; // Version of debug information in use.
488 std::set<GlobalVariable *> Visited; // Tracks visits during recursion.
489 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
491 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
492 /// Mark markVisited otherwise.
493 bool markVisited(GlobalVariable *GV);
496 DIVerifier() : DebugVersion(LLVMDebugVersion) {}
499 /// Verify - Return true if the GlobalVariable appears to be a valid
500 /// serialization of a DebugInfoDesc.
501 bool Verify(Value *V);
502 bool Verify(GlobalVariable *GV);
505 //===----------------------------------------------------------------------===//
506 /// SourceLineInfo - This class is used to record source line correspondence.
508 class SourceLineInfo {
510 unsigned Line; // Source line number.
511 unsigned Column; // Source column.
512 unsigned SourceID; // Source ID number.
515 SourceLineInfo(unsigned L, unsigned C, unsigned S)
516 : Line(L), Column(C), SourceID(S) {}
519 unsigned getLine() const { return Line; }
520 unsigned getColumn() const { return Column; }
521 unsigned getSourceID() const { return SourceID; }
524 //===----------------------------------------------------------------------===//
525 /// SourceFileInfo - This class is used to track source information.
527 class SourceFileInfo {
529 unsigned DirectoryID; // Directory ID number.
530 std::string Name; // File name (not including directory.)
533 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
536 unsigned getDirectoryID() const { return DirectoryID; }
537 const std::string &getName() const { return Name; }
539 /// operator== - Used by UniqueVector to locate entry.
541 bool operator==(const SourceFileInfo &SI) const {
542 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
545 /// operator< - Used by UniqueVector to locate entry.
547 bool operator<(const SourceFileInfo &SI) const {
548 return getDirectoryID() < SI.getDirectoryID() ||
549 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
553 //===----------------------------------------------------------------------===//
554 /// MachineDebugInfo - This class contains debug information specific to a
555 /// module. Queries can be made by different debugging schemes and reformated
556 /// for specific use.
558 class MachineDebugInfo : public ImmutablePass {
560 // Use the same serializer/deserializer/verifier for the module.
563 // CompileUnits - Uniquing vector for compile units.
564 UniqueVector<CompileUnitDesc *> CompileUnits;
566 // Directories - Uniquing vector for directories.
567 UniqueVector<std::string> Directories;
569 // SourceFiles - Uniquing vector for source files.
570 UniqueVector<SourceFileInfo> SourceFiles;
572 // Lines - List of of source line correspondence.
573 std::vector<SourceLineInfo *> Lines;
579 /// doInitialization - Initialize the debug state for a new module.
581 bool doInitialization();
583 /// doFinalization - Tear down the debug state after completion of a module.
585 bool doFinalization();
587 /// Deserialize - Convert a Value to a debug information descriptor.
589 DebugInfoDesc *Deserialize(Value *V);
591 /// Verify - Verify that a Value is debug information descriptor.
593 bool Verify(Value *V);
595 /// AnalyzeModule - Scan the module for global debug information.
597 void AnalyzeModule(Module &M);
599 /// hasInfo - Returns true if valid debug info is present.
601 bool hasInfo() const { return !CompileUnits.empty(); }
603 /// RecordLabel - Records location information and associates it with a
604 /// debug label. Returns a unique label ID used to generate a label and
605 /// provide correspondence to the source line list.
606 unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
607 Lines.push_back(new SourceLineInfo(Line, Column, Source));
611 /// RecordSource - Register a source file with debug info. Returns an source
613 unsigned RecordSource(const std::string &Directory,
614 const std::string &Source) {
615 unsigned DirectoryID = Directories.insert(Directory);
616 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
619 /// getDirectories - Return the UniqueVector of std::string representing
621 const UniqueVector<std::string> &getDirectories() const {
625 /// getSourceFiles - Return the UniqueVector of source files.
627 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
631 /// getSourceLines - Return a vector of source lines. Vector index + 1
633 const std::vector<SourceLineInfo *> &getSourceLines() const {
637 /// SetupCompileUnits - Set up the unique vector of compile units.
639 void SetupCompileUnits(Module &M);
641 /// getCompileUnits - Return a vector of debug compile units.
643 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
645 /// getGlobalVariables - Return a vector of debug GlobalVariables.
647 std::vector<GlobalVariableDesc *> getGlobalVariables(Module &M);
649 }; // End class MachineDebugInfo
651 } // End llvm namespace