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 /// ModuleCU - All DIEs are inserted in ModuleCU.
74 CompileUnit *ModuleCU;
76 /// AbbreviationsSet - Used to uniquely define abbreviations.
78 FoldingSet<DIEAbbrev> AbbreviationsSet;
80 /// Abbreviations - A list of all the unique abbreviations in use.
82 std::vector<DIEAbbrev *> Abbreviations;
84 /// DirectoryIdMap - Directory name to directory id map.
86 StringMap<unsigned> DirectoryIdMap;
88 /// DirectoryNames - A list of directory names.
89 SmallVector<std::string, 8> DirectoryNames;
91 /// SourceFileIdMap - Source file name to source file id map.
93 StringMap<unsigned> SourceFileIdMap;
95 /// SourceFileNames - A list of source file names.
96 SmallVector<std::string, 8> SourceFileNames;
98 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
99 /// id mapped to a unique id.
100 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
102 /// SourceIds - Reverse map from source id to directory id + file id pair.
104 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
106 /// Lines - List of of source line correspondence.
107 std::vector<SrcLineInfo> Lines;
109 /// ValuesSet - Used to uniquely define values.
111 FoldingSet<DIEValue> ValuesSet;
113 /// Values - A list of all the unique values in use.
115 std::vector<DIEValue *> Values;
117 /// StringPool - A UniqueVector of strings used by indirect references.
119 UniqueVector<std::string> StringPool;
121 /// SectionMap - Provides a unique id per text section.
123 UniqueVector<const MCSection*> SectionMap;
125 /// SectionSourceLines - Tracks line numbers per text section.
127 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
129 /// didInitial - Flag to indicate if initial emission has been done.
133 /// shouldEmit - Flag to indicate if debug information should be emitted.
137 // FunctionDbgScope - Top level scope for the current function.
139 DbgScope *FunctionDbgScope;
141 /// DbgScopeMap - Tracks the scopes in the current function.
142 DenseMap<MDNode *, DbgScope *> DbgScopeMap;
144 /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
146 DenseMap<MDNode *, DbgScope *> DbgAbstractScopeMap;
148 /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
151 SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
153 /// InlineInfo - Keep track of inlined functions and their location. This
154 /// information is used to populate debug_inlined section.
155 DenseMap<MDNode *, SmallVector<unsigned, 4> > InlineInfo;
157 /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
158 /// functions. These are subroutine entries that contain a DW_AT_inline
160 DenseMap<const MDNode *, DbgScope *> AbstractInstanceRootMap;
162 /// AbstractInstanceRootList - List of abstract instance roots of inlined
163 /// functions. These are subroutine entries that contain a DW_AT_inline
165 SmallVector<DbgScope *, 32> AbstractInstanceRootList;
167 /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
169 SmallVector<DbgScope *, 16> LexicalScopeStack;
171 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
172 /// used when calculating the "origin" of a concrete instance of an inlined
174 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
176 /// DebugTimer - Timer for the Dwarf debug writer.
179 struct FunctionDebugFrameInfo {
181 std::vector<MachineMove> Moves;
183 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
184 : Number(Num), Moves(M) {}
187 std::vector<FunctionDebugFrameInfo> DebugFrames;
189 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
190 /// maps to the source id. Source id starts at 1.
191 std::pair<unsigned, unsigned>
192 getSourceDirectoryAndFileIds(unsigned SId) const {
193 return SourceIds[SId-1];
196 /// getNumSourceDirectories - Return the number of source directories in the
198 unsigned getNumSourceDirectories() const {
199 return DirectoryNames.size();
202 /// getSourceDirectoryName - Return the name of the directory corresponding
204 const std::string &getSourceDirectoryName(unsigned Id) const {
205 return DirectoryNames[Id - 1];
208 /// getSourceFileName - Return the name of the source file corresponding
210 const std::string &getSourceFileName(unsigned Id) const {
211 return SourceFileNames[Id - 1];
214 /// getNumSourceIds - Return the number of unique source ids.
215 unsigned getNumSourceIds() const {
216 return SourceIds.size();
219 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
221 void AssignAbbrevNumber(DIEAbbrev &Abbrev);
223 /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
224 /// information entry.
225 DIEEntry *CreateDIEEntry(DIE *Entry = NULL);
227 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
229 void SetDIEEntry(DIEEntry *Value, DIE *Entry);
231 /// AddUInt - Add an unsigned integer attribute data and value.
233 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
235 /// AddSInt - Add an signed integer attribute data and value.
237 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
239 /// AddString - Add a string attribute data and value.
241 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
242 const std::string &String);
244 /// AddLabel - Add a Dwarf label attribute data and value.
246 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
247 const DWLabel &Label);
249 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
251 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
252 const std::string &Label);
254 /// AddSectionOffset - Add a section offset label attribute data and value.
256 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
257 const DWLabel &Label, const DWLabel &Section,
258 bool isEH = false, bool useSet = true);
260 /// AddDelta - Add a label delta attribute data and value.
262 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
263 const DWLabel &Hi, const DWLabel &Lo);
265 /// AddDIEEntry - Add a DIE attribute data and value.
267 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
268 Die->AddValue(Attribute, Form, CreateDIEEntry(Entry));
271 /// AddBlock - Add block data.
273 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
275 /// AddSourceLine - Add location information to specified debug information
277 void AddSourceLine(DIE *Die, const DIVariable *V);
278 void AddSourceLine(DIE *Die, const DIGlobal *G);
279 void AddSourceLine(DIE *Die, const DISubprogram *SP);
280 void AddSourceLine(DIE *Die, const DIType *Ty);
282 /// AddAddress - Add an address attribute to a die based on the location
284 void AddAddress(DIE *Die, unsigned Attribute,
285 const MachineLocation &Location);
287 /// AddBlockByrefAddress - Start with the address based on the location
288 /// provided, and generate the DWARF information necessary to find the
289 /// actual Block variable (navigating the Block struct) based on the
290 /// starting location. Add the DWARF information to the die.
292 void AddBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
293 const MachineLocation &Location);
295 /// AddType - Add a new type attribute to the specified entity.
296 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
298 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
299 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
302 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
303 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
306 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
307 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
308 DICompositeType CTy);
310 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
311 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
313 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
314 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
315 DICompositeType *CTy);
317 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
318 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
320 /// CreateGlobalVariableDIE - Create new DIE using GV.
321 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit,
322 const DIGlobalVariable &GV);
324 /// CreateMemberDIE - Create new member DIE.
325 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
327 /// CreateSubprogramDIE - Create new DIE using SP.
328 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
329 const DISubprogram &SP,
330 bool IsConstructor = false,
331 bool IsInlined = false);
333 /// FindCompileUnit - Get the compile unit for the given descriptor.
335 CompileUnit &FindCompileUnit(DICompileUnit Unit) const;
337 /// CreateDbgScopeVariable - Create a new scope variable.
339 DIE *CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
341 /// getOrCreateScope - Returns the scope associated with the given descriptor.
343 DbgScope *getOrCreateScope(MDNode *N);
345 /// ConstructDbgScope - Construct the components of a scope.
347 void ConstructDbgScope(DbgScope *ParentScope,
348 unsigned ParentStartID, unsigned ParentEndID,
349 DIE *ParentDie, CompileUnit *Unit);
351 /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
353 void ConstructFunctionDbgScope(DbgScope *RootScope,
354 bool AbstractScope = false);
356 /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
358 void ConstructDefaultDbgScope(MachineFunction *MF);
360 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
361 /// tools to recognize the object file contains Dwarf information.
364 /// EmitDIE - Recusively Emits a debug information entry.
366 void EmitDIE(DIE *Die);
368 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
370 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
372 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
374 void SizeAndOffsets();
376 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
378 void EmitDebugInfoPerCU(CompileUnit *Unit);
380 void EmitDebugInfo();
382 /// EmitAbbreviations - Emit the abbreviation section.
384 void EmitAbbreviations() const;
386 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
389 void EmitEndOfLineMatrix(unsigned SectionEnd);
391 /// EmitDebugLines - Emit source line information.
393 void EmitDebugLines();
395 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
397 void EmitCommonDebugFrame();
399 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
401 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
403 void EmitDebugPubNamesPerCU(CompileUnit *Unit);
405 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
407 void EmitDebugPubNames();
409 /// EmitDebugStr - Emit visible names into a debug str section.
413 /// EmitDebugLoc - Emit visible names into a debug loc section.
417 /// EmitDebugARanges - Emit visible names into a debug aranges section.
419 void EmitDebugARanges();
421 /// EmitDebugRanges - Emit visible names into a debug ranges section.
423 void EmitDebugRanges();
425 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
427 void EmitDebugMacInfo();
429 /// EmitDebugInlineInfo - Emit inline info using following format.
431 /// 1. length of section
432 /// 2. Dwarf version number
435 /// Entries (one "entry" for each function that was inlined):
437 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
438 /// otherwise offset into __debug_str for regular function name.
439 /// 2. offset into __debug_str section for regular function name.
440 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
441 /// instances for the function.
443 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
444 /// inlined instance; the die_offset points to the inlined_subroutine die in
445 /// the __debug_info section, and the low_pc is the starting address for the
446 /// inlining instance.
447 void EmitDebugInlineInfo();
449 /// GetOrCreateSourceID - Look up the source id with the given directory and
450 /// source file names. If none currently exists, create a new id and insert it
451 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
453 unsigned GetOrCreateSourceID(const char *DirName,
454 const char *FileName);
456 void ConstructCompileUnit(MDNode *N);
458 void ConstructGlobalVariableDIE(MDNode *N);
460 void ConstructSubprogram(MDNode *N);
462 /// Find the type the programmer originally declared the variable to be
463 /// and return that type.
465 DIType GetBlockByrefType(DIType Ty, std::string Name);
468 //===--------------------------------------------------------------------===//
469 // Main entry points.
471 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T);
472 virtual ~DwarfDebug();
474 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
476 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
478 /// BeginModule - Emit all Dwarf sections that should come prior to the
480 void BeginModule(Module *M, MachineModuleInfo *MMI);
482 /// EndModule - Emit all Dwarf sections that should come after the content.
486 /// BeginFunction - Gather pre-function debug information. Assumes being
487 /// emitted immediately after the function entry point.
488 void BeginFunction(MachineFunction *MF);
490 /// EndFunction - Gather and emit post-function debug information.
492 void EndFunction(MachineFunction *MF);
494 /// RecordSourceLine - Records location information and associates it with a
495 /// label. Returns a unique label ID used to generate a label and provide
496 /// correspondence to the source line list.
497 unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col);
499 /// RecordSourceLine - Records location information and associates it with a
500 /// label. Returns a unique label ID used to generate a label and provide
501 /// correspondence to the source line list.
502 unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU);
504 /// getRecordSourceLineCount - Return the number of source lines in the debug
506 unsigned getRecordSourceLineCount() const {
510 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
511 /// timed. Look up the source id with the given directory and source file
512 /// names. If none currently exists, create a new id and insert it in the
513 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
515 unsigned getOrCreateSourceID(const std::string &DirName,
516 const std::string &FileName);
518 /// RecordRegionStart - Indicate the start of a region.
519 unsigned RecordRegionStart(MDNode *N);
521 /// RecordRegionEnd - Indicate the end of a region.
522 unsigned RecordRegionEnd(MDNode *N);
524 /// RecordVariable - Indicate the declaration of a local variable.
525 void RecordVariable(MDNode *N, unsigned FrameIndex);
527 //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
528 unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
529 unsigned Line, unsigned Col);
531 /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
532 unsigned RecordInlinedFnEnd(DISubprogram &SP);
536 } // End of namespace llvm