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,
67 //===----------------------------------------------------------------------===//
68 /// DIVisitor - Subclasses of this class apply steps to each of the fields in
69 /// the supplied DebugInfoDesc.
73 virtual ~DIVisitor() {}
75 /// ApplyToFields - Target the visitor to each field of the debug information
77 void ApplyToFields(DebugInfoDesc *DD);
79 /// Apply - Subclasses override each of these methods to perform the
80 /// appropriate action for the type of field.
81 virtual void Apply(int &Field) = 0;
82 virtual void Apply(unsigned &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;
89 //===----------------------------------------------------------------------===//
90 /// DebugInfoDesc - This class is the base class for debug info descriptors.
94 unsigned Tag; // Content indicator. Dwarf values are
95 // used but that does not limit use to
99 DebugInfoDesc(unsigned T) : Tag(T) {}
102 virtual ~DebugInfoDesc() {}
105 unsigned getTag() const { return Tag; }
107 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
108 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
109 static unsigned TagFromGlobal(GlobalVariable *GV);
111 /// DescFactory - Create an instance of debug info descriptor based on Tag.
112 /// Return NULL if not a recognized Tag.
113 static DebugInfoDesc *DescFactory(unsigned Tag);
115 /// getLinkage - get linkage appropriate for this type of descriptor.
117 virtual GlobalValue::LinkageTypes getLinkage() const;
119 //===--------------------------------------------------------------------===//
120 // Subclasses should supply the following static methods.
122 // Implement isa/cast/dyncast.
123 static bool classof(const DebugInfoDesc *) { return true; }
125 //===--------------------------------------------------------------------===//
126 // Subclasses should supply the following virtual methods.
128 /// ApplyToFields - Target the vistor to the fields of the descriptor.
130 virtual void ApplyToFields(DIVisitor *Visitor);
132 /// getDescString - Return a string used to compose global names and labels.
134 virtual const char *getDescString() const = 0;
136 /// getTypeString - Return a string used to label this descriptor's type.
138 virtual const char *getTypeString() const = 0;
141 virtual void dump() = 0;
146 //===----------------------------------------------------------------------===//
147 /// AnchorDesc - Descriptors of this class act as markers for identifying
148 /// descriptors of certain groups.
149 class AnchorDesc : public DebugInfoDesc {
151 std::string Name; // Anchor type string.
155 : DebugInfoDesc(DI_TAG_anchor)
158 AnchorDesc(const std::string &N)
159 : DebugInfoDesc(DI_TAG_anchor)
164 const std::string &getName() const { return Name; }
166 // Implement isa/cast/dyncast.
167 static bool classof(const AnchorDesc *) { return true; }
168 static bool classof(const DebugInfoDesc *D) {
169 return D->getTag() == DI_TAG_anchor;
172 /// getLinkage - get linkage appropriate for this type of descriptor.
174 virtual GlobalValue::LinkageTypes getLinkage() const;
176 /// ApplyToFields - Target the visitor to the fields of the AnchorDesc.
178 virtual void ApplyToFields(DIVisitor *Visitor);
180 /// getDescString - Return a string used to compose global names and labels.
182 virtual const char *getDescString() const;
184 /// getTypeString - Return a string used to label this descriptor's type.
186 virtual const char *getTypeString() const;
193 //===----------------------------------------------------------------------===//
194 /// AnchoredDesc - This class manages anchors for a variety of top level
196 class AnchoredDesc : public DebugInfoDesc {
198 AnchorDesc *Anchor; // Anchor for all descriptors of the
203 AnchoredDesc(unsigned T);
207 AnchorDesc *getAnchor() const { return Anchor; }
208 void setAnchor(AnchorDesc *A) { Anchor = A; }
210 //===--------------------------------------------------------------------===//
211 // Subclasses should supply the following virtual methods.
213 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
215 virtual void ApplyToFields(DIVisitor *Visitor);
218 //===----------------------------------------------------------------------===//
219 /// CompileUnitDesc - This class packages debug information associated with a
220 /// source/header file.
221 class CompileUnitDesc : public AnchoredDesc {
223 unsigned DebugVersion; // LLVM debug version when produced.
224 unsigned Language; // Language number (ex. DW_LANG_C89.)
225 std::string FileName; // Source file name.
226 std::string Directory; // Source file directory.
227 std::string Producer; // Compiler string.
233 unsigned getDebugVersion() const { return DebugVersion; }
234 unsigned getLanguage() const { return Language; }
235 const std::string &getFileName() const { return FileName; }
236 const std::string &getDirectory() const { return Directory; }
237 const std::string &getProducer() const { return Producer; }
238 void setLanguage(unsigned L) { Language = L; }
239 void setFileName(const std::string &FN) { FileName = FN; }
240 void setDirectory(const std::string &D) { Directory = D; }
241 void setProducer(const std::string &P) { Producer = P; }
243 // FIXME - Need translation unit getter/setter.
245 // Implement isa/cast/dyncast.
246 static bool classof(const CompileUnitDesc *) { return true; }
247 static bool classof(const DebugInfoDesc *D) {
248 return D->getTag() == DI_TAG_compile_unit;
251 /// DebugVersionFromGlobal - Returns the version number from a compile unit
252 /// GlobalVariable. Return DIIValid if operand is not an unsigned int.
253 static unsigned DebugVersionFromGlobal(GlobalVariable *GV);
255 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
257 virtual void ApplyToFields(DIVisitor *Visitor);
259 /// getDescString - Return a string used to compose global names and labels.
261 virtual const char *getDescString() const;
263 /// getTypeString - Return a string used to label this descriptor's type.
265 virtual const char *getTypeString() const;
267 /// getAnchorString - Return a string used to label this descriptor's anchor.
269 virtual const char *getAnchorString() const;
276 //===----------------------------------------------------------------------===//
277 /// GlobalDesc - This class is the base descriptor for global functions and
279 class GlobalDesc : public AnchoredDesc {
281 DebugInfoDesc *Context; // Context debug descriptor.
282 std::string Name; // Global name.
283 // FIXME - Use a descriptor.
284 GlobalVariable *TyDesc; // Type debug descriptor.
285 bool IsStatic; // Is the global a static.
286 bool IsDefinition; // Is the global defined in context.
289 GlobalDesc(unsigned T);
293 DebugInfoDesc *getContext() const { return Context; }
294 const std::string &getName() const { return Name; }
295 bool isStatic() const { return IsStatic; }
296 bool isDefinition() const { return IsDefinition; }
297 void setContext(DebugInfoDesc *C) { Context = C; }
298 void setName(const std::string &N) { Name = N; }
299 void setIsStatic(bool IS) { IsStatic = IS; }
300 void setIsDefinition(bool ID) { IsDefinition = ID; }
302 /// ApplyToFields - Target the visitor to the fields of the GlobalDesc.
304 virtual void ApplyToFields(DIVisitor *Visitor);
307 //===----------------------------------------------------------------------===//
308 /// GlobalVariableDesc - This class packages debug information associated with a
310 class GlobalVariableDesc : public GlobalDesc {
312 GlobalVariable *Global; // llvm global.
315 GlobalVariableDesc();
318 GlobalVariable *getGlobalVariable() const { return Global; }
319 void setGlobalVariable(GlobalVariable *GV) { Global = GV; }
321 // Implement isa/cast/dyncast.
322 static bool classof(const GlobalVariableDesc *) { return true; }
323 static bool classof(const DebugInfoDesc *D) {
324 return D->getTag() == DI_TAG_global_variable;
327 /// ApplyToFields - Target the visitor to the fields of the
328 /// GlobalVariableDesc.
329 virtual void ApplyToFields(DIVisitor *Visitor);
331 /// getDescString - Return a string used to compose global names and labels.
333 virtual const char *getDescString() const;
335 /// getTypeString - Return a string used to label this descriptor's type.
337 virtual const char *getTypeString() const;
339 /// getAnchorString - Return a string used to label this descriptor's anchor.
341 virtual const char *getAnchorString() const;
348 //===----------------------------------------------------------------------===//
349 /// SubprogramDesc - This class packages debug information associated with a
350 /// subprogram/function.
351 class SubprogramDesc : public GlobalDesc {
353 DebugInfoDesc *Context; // Context debug descriptor.
354 std::string Name; // Subprogram name.
355 // FIXME - Use a descriptor.
356 GlobalVariable *TyDesc; // Type debug descriptor.
357 bool IsStatic; // Is the subprogram a static.
358 bool IsDefinition; // Is the subprogram defined in context.
364 DebugInfoDesc *getContext() const { return Context; }
365 const std::string &getName() const { return Name; }
366 bool isStatic() const { return IsStatic; }
367 bool isDefinition() const { return IsDefinition; }
368 void setContext(DebugInfoDesc *C) { Context = C; }
369 void setName(const std::string &N) { Name = N; }
370 void setIsStatic(bool IS) { IsStatic = IS; }
371 void setIsDefinition(bool ID) { IsDefinition = ID; }
372 // FIXME - Other getters/setters.
374 // Implement isa/cast/dyncast.
375 static bool classof(const SubprogramDesc *) { return true; }
376 static bool classof(const DebugInfoDesc *D) {
377 return D->getTag() == DI_TAG_subprogram;
380 /// ApplyToFields - Target the visitor to the fields of the SubprogramDesc.
382 virtual void ApplyToFields(DIVisitor *Visitor);
384 /// getDescString - Return a string used to compose global names and labels.
386 virtual const char *getDescString() const;
388 /// getTypeString - Return a string used to label this descriptor's type.
390 virtual const char *getTypeString() const;
392 /// getAnchorString - Return a string used to label this descriptor's anchor.
394 virtual const char *getAnchorString() const;
401 //===----------------------------------------------------------------------===//
402 /// DIDeserializer - This class is responsible for casting GlobalVariables
403 /// into DebugInfoDesc objects.
404 class DIDeserializer {
406 unsigned DebugVersion; // Version of debug information in use.
407 std::map<GlobalVariable *, DebugInfoDesc *> GlobalDescs;
408 // Previously defined gloabls.
411 DIDeserializer() : DebugVersion(LLVMDebugVersion) {}
415 unsigned getDebugVersion() const { return DebugVersion; }
417 /// Deserialize - Reconstitute a GlobalVariable into it's component
418 /// DebugInfoDesc objects.
419 DebugInfoDesc *Deserialize(Value *V);
420 DebugInfoDesc *Deserialize(GlobalVariable *GV);
423 //===----------------------------------------------------------------------===//
424 /// DISerializer - This class is responsible for casting DebugInfoDesc objects
425 /// into GlobalVariables.
428 Module *M; // Definition space module.
429 PointerType *StrPtrTy; // A "sbyte *" type. Created lazily.
430 PointerType *EmptyStructPtrTy; // A "{ }*" type. Created lazily.
431 std::map<unsigned, StructType *> TagTypes;
432 // Types per Tag. Created lazily.
433 std::map<DebugInfoDesc *, GlobalVariable *> DescGlobals;
434 // Previously defined descriptors.
435 std::map<const std::string, Constant *> StringCache;
436 // Previously defined strings.
442 , EmptyStructPtrTy(NULL)
450 Module *getModule() const { return M; };
451 void setModule(Module *module) { M = module; }
453 /// getStrPtrType - Return a "sbyte *" type.
455 const PointerType *getStrPtrType();
457 /// getEmptyStructPtrType - Return a "{ }*" type.
459 const PointerType *getEmptyStructPtrType();
461 /// getTagType - Return the type describing the specified descriptor (via
463 const StructType *getTagType(DebugInfoDesc *DD);
465 /// getString - Construct the string as constant string global.
467 Constant *getString(const std::string &String);
469 /// Serialize - Recursively cast the specified descriptor into a
470 /// GlobalVariable so that it can be serialized to a .bc or .ll file.
471 GlobalVariable *Serialize(DebugInfoDesc *DD);
474 //===----------------------------------------------------------------------===//
475 /// DIVerifier - This class is responsible for verifying the given network of
476 /// GlobalVariables are valid as DebugInfoDesc objects.
479 unsigned DebugVersion; // Version of debug information in use.
480 std::set<GlobalVariable *> Visited; // Tracks visits during recursion.
481 std::map<unsigned, unsigned> Counts; // Count of fields per Tag type.
483 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
484 /// Mark markVisited otherwise.
485 bool markVisited(GlobalVariable *GV);
488 DIVerifier() : DebugVersion(LLVMDebugVersion) {}
491 /// Verify - Return true if the GlobalVariable appears to be a valid
492 /// serialization of a DebugInfoDesc.
493 bool Verify(Value *V);
494 bool Verify(GlobalVariable *GV);
497 //===----------------------------------------------------------------------===//
498 /// SourceLineInfo - This class is used to record source line correspondence.
500 class SourceLineInfo {
502 unsigned Line; // Source line number.
503 unsigned Column; // Source column.
504 unsigned SourceID; // Source ID number.
507 SourceLineInfo(unsigned L, unsigned C, unsigned S)
508 : Line(L), Column(C), SourceID(S) {}
511 unsigned getLine() const { return Line; }
512 unsigned getColumn() const { return Column; }
513 unsigned getSourceID() const { return SourceID; }
516 //===----------------------------------------------------------------------===//
517 /// SourceFileInfo - This class is used to track source information.
519 class SourceFileInfo {
521 unsigned DirectoryID; // Directory ID number.
522 std::string Name; // File name (not including directory.)
525 SourceFileInfo(unsigned D, const std::string &N) : DirectoryID(D), Name(N) {}
528 unsigned getDirectoryID() const { return DirectoryID; }
529 const std::string &getName() const { return Name; }
531 /// operator== - Used by UniqueVector to locate entry.
533 bool operator==(const SourceFileInfo &SI) const {
534 return getDirectoryID() == SI.getDirectoryID() && getName() == SI.getName();
537 /// operator< - Used by UniqueVector to locate entry.
539 bool operator<(const SourceFileInfo &SI) const {
540 return getDirectoryID() < SI.getDirectoryID() ||
541 (getDirectoryID() == SI.getDirectoryID() && getName() < SI.getName());
545 //===----------------------------------------------------------------------===//
546 /// MachineDebugInfo - This class contains debug information specific to a
547 /// module. Queries can be made by different debugging schemes and reformated
548 /// for specific use.
550 class MachineDebugInfo : public ImmutablePass {
552 // Use the same serializer/deserializer/verifier for the module.
555 // CompileUnits - Uniquing vector for compile units.
556 UniqueVector<CompileUnitDesc *> CompileUnits;
558 // Directories - Uniquing vector for directories.
559 UniqueVector<std::string> Directories;
561 // SourceFiles - Uniquing vector for source files.
562 UniqueVector<SourceFileInfo> SourceFiles;
564 // Lines - List of of source line correspondence.
565 std::vector<SourceLineInfo *> Lines;
571 /// doInitialization - Initialize the debug state for a new module.
573 bool doInitialization();
575 /// doFinalization - Tear down the debug state after completion of a module.
577 bool doFinalization();
579 /// getDescFor - Convert a Value to a debug information descriptor.
581 // FIXME - use new Value type when available.
582 DebugInfoDesc *getDescFor(Value *V);
584 /// Verify - Verify that a Value is debug information descriptor.
586 bool Verify(Value *V);
588 /// AnalyzeModule - Scan the module for global debug information.
590 void AnalyzeModule(Module &M);
592 /// hasInfo - Returns true if valid debug info is present.
594 bool hasInfo() const { return !CompileUnits.empty(); }
596 /// RecordLabel - Records location information and associates it with a
597 /// debug label. Returns a unique label ID used to generate a label and
598 /// provide correspondence to the source line list.
599 unsigned RecordLabel(unsigned Line, unsigned Column, unsigned Source) {
600 Lines.push_back(new SourceLineInfo(Line, Column, Source));
604 /// RecordSource - Register a source file with debug info. Returns an source
606 unsigned RecordSource(const std::string &Directory,
607 const std::string &Source) {
608 unsigned DirectoryID = Directories.insert(Directory);
609 return SourceFiles.insert(SourceFileInfo(DirectoryID, Source));
612 /// getDirectories - Return the UniqueVector of std::string representing
614 const UniqueVector<std::string> &getDirectories() const {
618 /// getSourceFiles - Return the UniqueVector of source files.
620 const UniqueVector<SourceFileInfo> &getSourceFiles() const {
624 /// getSourceLines - Return a vector of source lines. Vector index + 1
626 const std::vector<SourceLineInfo *> &getSourceLines() const {
630 /// SetupCompileUnits - Set up the unique vector of compile units.
632 void SetupCompileUnits(Module &M);
634 /// getCompileUnits - Return a vector of debug compile units.
636 const UniqueVector<CompileUnitDesc *> getCompileUnits() const;
638 /// getGlobalVariables - Return a vector of debug GlobalVariables.
640 std::vector<GlobalVariableDesc *> getGlobalVariables(Module &M);
642 }; // End class MachineDebugInfo
644 } // End llvm namespace