1 //===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf debug info into asm files.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15 #define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
18 #include "DwarfPrinter.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineLocation.h"
21 #include "llvm/Analysis/DebugInfo.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/ADT/UniqueVector.h"
35 class DbgConcreteScope;
36 class MachineFrameInfo;
37 class MachineModuleInfo;
41 //===----------------------------------------------------------------------===//
42 /// SrcLineInfo - This class is used to record source line correspondence.
44 class VISIBILITY_HIDDEN SrcLineInfo {
45 unsigned Line; // Source line number.
46 unsigned Column; // Source column.
47 unsigned SourceID; // Source ID number.
48 unsigned LabelID; // Label in code ID number.
50 SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
51 : Line(L), Column(C), SourceID(S), LabelID(I) {}
54 unsigned getLine() const { return Line; }
55 unsigned getColumn() const { return Column; }
56 unsigned getSourceID() const { return SourceID; }
57 unsigned getLabelID() const { return LabelID; }
60 class VISIBILITY_HIDDEN DwarfDebug : public Dwarf {
61 //===--------------------------------------------------------------------===//
62 // Attributes used to construct specific Dwarf sections.
65 /// CompileUnitMap - A map of global variables representing compile units to
67 DenseMap<Value *, CompileUnit *> CompileUnitMap;
69 /// CompileUnits - All the compile units in this module.
71 SmallVector<CompileUnit *, 8> CompileUnits;
73 /// MainCU - Some platform prefers one compile unit per .o file. In such
74 /// cases, all dies are inserted in MainCU.
77 /// AbbreviationsSet - Used to uniquely define abbreviations.
79 FoldingSet<DIEAbbrev> AbbreviationsSet;
81 /// Abbreviations - A list of all the unique abbreviations in use.
83 std::vector<DIEAbbrev *> Abbreviations;
85 /// DirectoryIdMap - Directory name to directory id map.
87 StringMap<unsigned> DirectoryIdMap;
89 /// DirectoryNames - A list of directory names.
90 SmallVector<std::string, 8> DirectoryNames;
92 /// SourceFileIdMap - Source file name to source file id map.
94 StringMap<unsigned> SourceFileIdMap;
96 /// SourceFileNames - A list of source file names.
97 SmallVector<std::string, 8> SourceFileNames;
99 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
100 /// id mapped to a unique id.
101 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
103 /// SourceIds - Reverse map from source id to directory id + file id pair.
105 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
107 /// Lines - List of of source line correspondence.
108 std::vector<SrcLineInfo> Lines;
110 /// ValuesSet - Used to uniquely define values.
112 FoldingSet<DIEValue> ValuesSet;
114 /// Values - A list of all the unique values in use.
116 std::vector<DIEValue *> Values;
118 /// StringPool - A UniqueVector of strings used by indirect references.
120 UniqueVector<std::string> StringPool;
122 /// SectionMap - Provides a unique id per text section.
124 UniqueVector<const Section*> SectionMap;
126 /// SectionSourceLines - Tracks line numbers per text section.
128 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
130 /// didInitial - Flag to indicate if initial emission has been done.
134 /// shouldEmit - Flag to indicate if debug information should be emitted.
138 // FunctionDbgScope - Top level scope for the current function.
140 DbgScope *FunctionDbgScope;
142 /// DbgScopeMap - Tracks the scopes in the current function.
143 DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
145 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
147 DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
149 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
151 DenseMap<GlobalVariable *,
152 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
154 /// InlineInfo - Keep track of inlined functions and their location. This
155 /// information is used to populate debug_inlined section.
156 DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
158 /// InlinedVariableScopes - Scopes information for the inlined subroutine
160 DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
162 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
163 /// functions. These are subroutine entries that contain a DW_AT_inline
165 DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
167 /// InlinedParamMap - A map keeping track of which parameters are assigned to
168 /// which abstract instance.
169 DenseMap<const GlobalVariable *,
170 SmallSet<const GlobalVariable *, 32> > InlinedParamMap;
172 /// AbstractInstanceRootList - List of abstract instance roots of inlined
173 /// functions. These are subroutine entries that contain a DW_AT_inline
175 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
177 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
179 SmallVector<DbgScope *, 16> LexicalScopeStack;
181 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
182 /// used when calculating the "origin" of a concrete instance of an inlined
184 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
186 /// DebugTimer - Timer for the Dwarf debug writer.
189 struct FunctionDebugFrameInfo {
191 std::vector<MachineMove> Moves;
193 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
194 : Number(Num), Moves(M) {}
197 std::vector<FunctionDebugFrameInfo> DebugFrames;
199 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
200 /// maps to the source id. Source id starts at 1.
201 std::pair<unsigned, unsigned>
202 getSourceDirectoryAndFileIds(unsigned SId) const {
203 return SourceIds[SId-1];
206 /// getNumSourceDirectories - Return the number of source directories in the
208 unsigned getNumSourceDirectories() const {
209 return DirectoryNames.size();
212 /// getSourceDirectoryName - Return the name of the directory corresponding
214 const std::string &getSourceDirectoryName(unsigned Id) const {
215 return DirectoryNames[Id - 1];
218 /// getSourceFileName - Return the name of the source file corresponding
220 const std::string &getSourceFileName(unsigned Id) const {
221 return SourceFileNames[Id - 1];
224 /// getNumSourceIds - Return the number of unique source ids.
225 unsigned getNumSourceIds() const {
226 return SourceIds.size();
229 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
231 void AssignAbbrevNumber(DIEAbbrev &Abbrev);
233 /// NewString - Add a string to the constant pool and returns a label.
235 DWLabel NewString(const std::string &String) {
236 unsigned StringID = StringPool.insert(String);
237 return DWLabel("string", StringID);
240 /// NewDIEEntry - Creates a new DIEEntry to be a proxy for a debug information
242 DIEEntry *NewDIEEntry(DIE *Entry = NULL);
244 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
246 void SetDIEEntry(DIEEntry *Value, DIE *Entry);
248 /// AddUInt - Add an unsigned integer attribute data and value.
250 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
252 /// AddSInt - Add an signed integer attribute data and value.
254 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
256 /// AddString - Add a string attribute data and value.
258 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
259 const std::string &String);
261 /// AddLabel - Add a Dwarf label attribute data and value.
263 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
264 const DWLabel &Label);
266 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
268 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
269 const std::string &Label);
271 /// AddSectionOffset - Add a section offset label attribute data and value.
273 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
274 const DWLabel &Label, const DWLabel &Section,
275 bool isEH = false, bool useSet = true);
277 /// AddDelta - Add a label delta attribute data and value.
279 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
280 const DWLabel &Hi, const DWLabel &Lo);
282 /// AddDIEEntry - Add a DIE attribute data and value.
284 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
285 Die->AddValue(Attribute, Form, NewDIEEntry(Entry));
288 /// AddBlock - Add block data.
290 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
292 /// AddSourceLine - Add location information to specified debug information
294 void AddSourceLine(DIE *Die, const DIVariable *V);
296 /// AddSourceLine - Add location information to specified debug information
298 void AddSourceLine(DIE *Die, const DIGlobal *G);
300 void AddSourceLine(DIE *Die, const DIType *Ty);
302 /// AddAddress - Add an address attribute to a die based on the location
304 void AddAddress(DIE *Die, unsigned Attribute,
305 const MachineLocation &Location);
307 /// AddType - Add a new type attribute to the specified entity.
308 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
310 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
311 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
314 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
315 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
318 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
319 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
320 DICompositeType CTy);
322 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
323 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
325 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
326 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
327 DICompositeType *CTy);
329 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
330 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
332 /// CreateGlobalVariableDIE - Create new DIE using GV.
333 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit,
334 const DIGlobalVariable &GV);
336 /// CreateMemberDIE - Create new member DIE.
337 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
339 /// CreateSubprogramDIE - Create new DIE using SP.
340 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
341 const DISubprogram &SP,
342 bool IsConstructor = false,
343 bool IsInlined = false);
345 /// FindCompileUnit - Get the compile unit for the given descriptor.
347 CompileUnit &FindCompileUnit(DICompileUnit Unit) const;
349 /// NewDbgScopeVariable - Create a new scope variable.
351 DIE *NewDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
353 /// getOrCreateScope - Returns the scope associated with the given descriptor.
355 DbgScope *getOrCreateScope(GlobalVariable *V);
357 /// ConstructDbgScope - Construct the components of a scope.
359 void ConstructDbgScope(DbgScope *ParentScope,
360 unsigned ParentStartID, unsigned ParentEndID,
361 DIE *ParentDie, CompileUnit *Unit);
363 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
365 void ConstructFunctionDbgScope(DbgScope *RootScope);
367 /// ConstructFunctionDbgScope - Construct the scope for the abstract debug
370 void ConstructAbstractDbgScope(DbgScope *AbsScope);
372 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
374 void ConstructDefaultDbgScope(MachineFunction *MF);
376 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
377 /// tools to recognize the object file contains Dwarf information.
380 /// EmitDIE - Recusively Emits a debug information entry.
382 void EmitDIE(DIE *Die);
384 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
386 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
388 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
390 void SizeAndOffsets();
392 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
394 void EmitDebugInfoPerCU(CompileUnit *Unit);
396 void EmitDebugInfo();
398 /// EmitAbbreviations - Emit the abbreviation section.
400 void EmitAbbreviations() const;
402 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
405 void EmitEndOfLineMatrix(unsigned SectionEnd);
407 /// EmitDebugLines - Emit source line information.
409 void EmitDebugLines();
411 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
413 void EmitCommonDebugFrame();
415 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
417 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
419 void EmitDebugPubNamesPerCU(CompileUnit *Unit);
421 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
423 void EmitDebugPubNames();
425 /// EmitDebugStr - Emit visible names into a debug str section.
429 /// EmitDebugLoc - Emit visible names into a debug loc section.
433 /// EmitDebugARanges - Emit visible names into a debug aranges section.
435 void EmitDebugARanges();
437 /// EmitDebugRanges - Emit visible names into a debug ranges section.
439 void EmitDebugRanges();
441 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
443 void EmitDebugMacInfo();
445 /// EmitDebugInlineInfo - Emit inline info using following format.
447 /// 1. length of section
448 /// 2. Dwarf version number
451 /// Entries (one "entry" for each function that was inlined):
453 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
454 /// otherwise offset into __debug_str for regular function name.
455 /// 2. offset into __debug_str section for regular function name.
456 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
457 /// instances for the function.
459 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
460 /// inlined instance; the die_offset points to the inlined_subroutine die in
461 /// the __debug_info section, and the low_pc is the starting address for the
462 /// inlining instance.
463 void EmitDebugInlineInfo();
465 /// GetOrCreateSourceID - Look up the source id with the given directory and
466 /// source file names. If none currently exists, create a new id and insert it
467 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
469 unsigned GetOrCreateSourceID(const std::string &DirName,
470 const std::string &FileName);
472 void ConstructCompileUnit(GlobalVariable *GV);
474 /// ConstructCompileUnits - Create a compile unit DIEs.
475 void ConstructCompileUnits();
477 bool ConstructGlobalVariableDIE(GlobalVariable *GV);
479 /// ConstructGlobalVariableDIEs - Create DIEs for each of the externally
480 /// visible global variables. Return true if at least one global DIE is
482 bool ConstructGlobalVariableDIEs();
484 bool ConstructSubprogram(GlobalVariable *GV);
486 /// ConstructSubprograms - Create DIEs for each of the externally visible
487 /// subprograms. Return true if at least one subprogram DIE is created.
488 bool ConstructSubprograms();
490 //===--------------------------------------------------------------------===//
491 // Main entry points.
493 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
494 virtual ~DwarfDebug();
496 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
498 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
500 /// SetDebugInfo - Create global DIEs and emit initial debug info sections.
501 /// This is inovked by the target AsmPrinter.
502 void SetDebugInfo(MachineModuleInfo *mmi);
504 /// BeginModule - Emit all Dwarf sections that should come prior to the
506 void BeginModule(Module *M) {
510 /// EndModule - Emit all Dwarf sections that should come after the content.
514 /// BeginFunction - Gather pre-function debug information. Assumes being
515 /// emitted immediately after the function entry point.
516 void BeginFunction(MachineFunction *MF);
518 /// EndFunction - Gather and emit post-function debug information.
520 void EndFunction(MachineFunction *MF);
522 /// RecordSourceLine - Records location information and associates it with a
523 /// label. Returns a unique label ID used to generate a label and provide
524 /// correspondence to the source line list.
525 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col);
527 /// RecordSourceLine - Records location information and associates it with a
528 /// label. Returns a unique label ID used to generate a label and provide
529 /// correspondence to the source line list.
530 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU);
532 /// getRecordSourceLineCount - Return the number of source lines in the debug
534 unsigned getRecordSourceLineCount() const {
538 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
539 /// timed. Look up the source id with the given directory and source file
540 /// names. If none currently exists, create a new id and insert it in the
541 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
543 unsigned getOrCreateSourceID(const std::string &DirName,
544 const std::string &FileName);
546 /// RecordRegionStart - Indicate the start of a region.
547 unsigned RecordRegionStart(GlobalVariable *V);
549 /// RecordRegionEnd - Indicate the end of a region.
550 unsigned RecordRegionEnd(GlobalVariable *V);
552 /// RecordVariable - Indicate the declaration of a local variable.
553 void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
554 const MachineInstr *MI);
556 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
557 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
558 unsigned Line, unsigned Col);
560 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
561 unsigned RecordInlinedFnEnd(DISubprogram &SP);
563 /// RecordVariableScope - Record scope for the variable declared by
564 /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
565 /// for only inlined subroutine variables. Other variables's scopes are
566 /// determined during RecordVariable().
567 void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI);
570 } // End of namespace llvm