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/ValueMap.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 // CurrentFnDbgScope - Top level scope for the current function.
139 DbgScope *CurrentFnDbgScope;
141 /// DbgScopeMap - Tracks the scopes in the current function.
143 ValueMap<MDNode *, DbgScope *> DbgScopeMap;
145 /// ConcreteScopes - Tracks the concrete scopees in the current function.
146 /// These scopes are also included in DbgScopeMap.
147 ValueMap<MDNode *, DbgScope *> ConcreteScopes;
149 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
150 /// not included DbgScopeMap.
151 ValueMap<MDNode *, DbgScope *> AbstractScopes;
152 SmallVector<DbgScope *, 4>AbstractScopesList;
154 /// AbstractVariables - Collection on abstract variables.
155 ValueMap<MDNode *, DbgVariable *> AbstractVariables;
157 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
158 /// (at the end of the module) as DW_AT_inline.
159 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
161 /// AbstractSubprogramDIEs - Collection of abstruct subprogram DIEs.
162 SmallPtrSet<DIE *, 4> AbstractSubprogramDIEs;
164 /// ScopedGVs - Tracks global variables that are not at file scope.
165 /// For example void f() { static int b = 42; }
166 SmallVector<WeakVH, 4> ScopedGVs;
168 typedef SmallVector<DbgScope *, 2> ScopeVector;
169 typedef DenseMap<const MachineInstr *, ScopeVector>
172 /// DbgScopeBeginMap - Maps instruction with a list of DbgScopes it starts.
173 InsnToDbgScopeMapTy DbgScopeBeginMap;
175 /// DbgScopeEndMap - Maps instruction with a list DbgScopes it ends.
176 InsnToDbgScopeMapTy DbgScopeEndMap;
178 /// InlineInfo - Keep track of inlined functions and their location. This
179 /// information is used to populate debug_inlined section.
180 typedef std::pair<unsigned, DIE *> InlineInfoLabels;
181 ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
182 SmallVector<MDNode *, 4> InlinedSPNodes;
184 /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
185 /// used when calculating the "origin" of a concrete instance of an inlined
187 DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
189 /// DebugTimer - Timer for the Dwarf debug writer.
192 struct FunctionDebugFrameInfo {
194 std::vector<MachineMove> Moves;
196 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
197 : Number(Num), Moves(M) {}
200 std::vector<FunctionDebugFrameInfo> DebugFrames;
202 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
203 /// maps to the source id. Source id starts at 1.
204 std::pair<unsigned, unsigned>
205 getSourceDirectoryAndFileIds(unsigned SId) const {
206 return SourceIds[SId-1];
209 /// getNumSourceDirectories - Return the number of source directories in the
211 unsigned getNumSourceDirectories() const {
212 return DirectoryNames.size();
215 /// getSourceDirectoryName - Return the name of the directory corresponding
217 const std::string &getSourceDirectoryName(unsigned Id) const {
218 return DirectoryNames[Id - 1];
221 /// getSourceFileName - Return the name of the source file corresponding
223 const std::string &getSourceFileName(unsigned Id) const {
224 return SourceFileNames[Id - 1];
227 /// getNumSourceIds - Return the number of unique source ids.
228 unsigned getNumSourceIds() const {
229 return SourceIds.size();
232 /// AssignAbbrevNumber - Define a unique number for the abbreviation.
234 void AssignAbbrevNumber(DIEAbbrev &Abbrev);
236 /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
237 /// information entry.
238 DIEEntry *CreateDIEEntry(DIE *Entry = NULL);
240 /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
242 void SetDIEEntry(DIEEntry *Value, DIE *Entry);
244 /// AddUInt - Add an unsigned integer attribute data and value.
246 void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
248 /// AddSInt - Add an signed integer attribute data and value.
250 void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
252 /// AddString - Add a string attribute data and value.
254 void AddString(DIE *Die, unsigned Attribute, unsigned Form,
255 const std::string &String);
257 /// AddLabel - Add a Dwarf label attribute data and value.
259 void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
260 const DWLabel &Label);
262 /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
264 void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
265 const std::string &Label);
267 /// AddSectionOffset - Add a section offset label attribute data and value.
269 void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
270 const DWLabel &Label, const DWLabel &Section,
271 bool isEH = false, bool useSet = true);
273 /// AddDelta - Add a label delta attribute data and value.
275 void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
276 const DWLabel &Hi, const DWLabel &Lo);
278 /// AddDIEEntry - Add a DIE attribute data and value.
280 void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
281 Die->AddValue(Attribute, Form, CreateDIEEntry(Entry));
284 /// AddBlock - Add block data.
286 void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
288 /// AddSourceLine - Add location information to specified debug information
290 void AddSourceLine(DIE *Die, const DIVariable *V);
291 void AddSourceLine(DIE *Die, const DIGlobal *G);
292 void AddSourceLine(DIE *Die, const DISubprogram *SP);
293 void AddSourceLine(DIE *Die, const DIType *Ty);
295 /// AddAddress - Add an address attribute to a die based on the location
297 void AddAddress(DIE *Die, unsigned Attribute,
298 const MachineLocation &Location);
300 /// AddComplexAddress - Start with the address based on the location provided,
301 /// and generate the DWARF information necessary to find the actual variable
302 /// (navigating the extra location information encoded in the type) based on
303 /// the starting location. Add the DWARF information to the die.
305 void AddComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
306 const MachineLocation &Location);
308 // FIXME: Should be reformulated in terms of AddComplexAddress.
309 /// AddBlockByrefAddress - Start with the address based on the location
310 /// provided, and generate the DWARF information necessary to find the
311 /// actual Block variable (navigating the Block struct) based on the
312 /// starting location. Add the DWARF information to the die. Obsolete,
313 /// please use AddComplexAddress instead.
315 void AddBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
316 const MachineLocation &Location);
318 /// AddType - Add a new type attribute to the specified entity.
319 void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
321 /// ConstructTypeDIE - Construct basic type die from DIBasicType.
322 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
325 /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
326 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
329 /// ConstructTypeDIE - Construct type DIE from DICompositeType.
330 void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
331 DICompositeType CTy);
333 /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
334 void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
336 /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
337 void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
338 DICompositeType *CTy);
340 /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
341 DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
343 /// CreateGlobalVariableDIE - Create new DIE using GV.
344 DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit,
345 const DIGlobalVariable &GV);
347 /// CreateMemberDIE - Create new member DIE.
348 DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
350 /// CreateSubprogramDIE - Create new DIE using SP.
351 DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
352 const DISubprogram &SP,
353 bool IsConstructor = false,
354 bool IsInlined = false);
356 /// FindCompileUnit - Get the compile unit for the given descriptor.
358 CompileUnit &FindCompileUnit(DICompileUnit Unit) const;
360 /// CreateDbgScopeVariable - Create a new scope variable.
362 DIE *CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
364 /// getUpdatedDbgScope - Find or create DbgScope assicated with
365 /// the instruction. Initialize scope and update scope hierarchy.
366 DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI, MDNode *InlinedAt);
368 /// createDbgScope - Create DbgScope for the scope.
369 void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
371 DbgScope *getOrCreateAbstractScope(MDNode *N);
373 /// findAbstractVariable - Find abstract variable associated with Var.
374 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
377 DIE *UpdateSubprogramScopeDIE(MDNode *SPNode);
378 DIE *ConstructLexicalScopeDIE(DbgScope *Scope);
379 DIE *ConstructScopeDIE(DbgScope *Scope);
380 DIE *ConstructInlinedScopeDIE(DbgScope *Scope);
381 DIE *ConstructVariableDIE(DbgVariable *DV, DbgScope *S, CompileUnit *Unit);
383 /// ConstructDbgScope - Construct the components of a scope.
385 void ConstructDbgScope(DbgScope *ParentScope,
386 unsigned ParentStartID, unsigned ParentEndID,
387 DIE *ParentDie, CompileUnit *Unit);
389 /// EmitInitial - Emit initial Dwarf declarations. This is necessary for cc
390 /// tools to recognize the object file contains Dwarf information.
393 /// EmitDIE - Recusively Emits a debug information entry.
395 void EmitDIE(DIE *Die);
397 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
399 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
401 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
403 void SizeAndOffsets();
405 /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
407 void EmitDebugInfoPerCU(CompileUnit *Unit);
409 void EmitDebugInfo();
411 /// EmitAbbreviations - Emit the abbreviation section.
413 void EmitAbbreviations() const;
415 /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
418 void EmitEndOfLineMatrix(unsigned SectionEnd);
420 /// EmitDebugLines - Emit source line information.
422 void EmitDebugLines();
424 /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
426 void EmitCommonDebugFrame();
428 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
430 void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
432 void EmitDebugPubNamesPerCU(CompileUnit *Unit);
434 /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
436 void EmitDebugPubNames();
438 /// EmitDebugStr - Emit visible names into a debug str section.
442 /// EmitDebugLoc - Emit visible names into a debug loc section.
446 /// EmitDebugARanges - Emit visible names into a debug aranges section.
448 void EmitDebugARanges();
450 /// EmitDebugRanges - Emit visible names into a debug ranges section.
452 void EmitDebugRanges();
454 /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
456 void EmitDebugMacInfo();
458 /// EmitDebugInlineInfo - Emit inline info using following format.
460 /// 1. length of section
461 /// 2. Dwarf version number
464 /// Entries (one "entry" for each function that was inlined):
466 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
467 /// otherwise offset into __debug_str for regular function name.
468 /// 2. offset into __debug_str section for regular function name.
469 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
470 /// instances for the function.
472 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
473 /// inlined instance; the die_offset points to the inlined_subroutine die in
474 /// the __debug_info section, and the low_pc is the starting address for the
475 /// inlining instance.
476 void EmitDebugInlineInfo();
478 /// GetOrCreateSourceID - Look up the source id with the given directory and
479 /// source file names. If none currently exists, create a new id and insert it
480 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
482 unsigned GetOrCreateSourceID(const char *DirName,
483 const char *FileName);
485 void ConstructCompileUnit(MDNode *N);
487 void ConstructGlobalVariableDIE(MDNode *N);
489 void ConstructSubprogram(MDNode *N);
491 // FIXME: This should go away in favor of complex addresses.
492 /// Find the type the programmer originally declared the variable to be
493 /// and return that type. Obsolete, use GetComplexAddrType instead.
495 DIType GetBlockByrefType(DIType Ty, std::string Name);
498 //===--------------------------------------------------------------------===//
499 // Main entry points.
501 DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T);
502 virtual ~DwarfDebug();
504 /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
506 bool ShouldEmitDwarfDebug() const { return shouldEmit; }
508 /// BeginModule - Emit all Dwarf sections that should come prior to the
510 void BeginModule(Module *M, MachineModuleInfo *MMI);
512 /// EndModule - Emit all Dwarf sections that should come after the content.
516 /// BeginFunction - Gather pre-function debug information. Assumes being
517 /// emitted immediately after the function entry point.
518 void BeginFunction(MachineFunction *MF);
520 /// EndFunction - Gather and emit post-function debug information.
522 void EndFunction(MachineFunction *MF);
524 /// RecordSourceLine - Records location information and associates it with a
525 /// label. Returns a unique label ID used to generate a label and provide
526 /// correspondence to the source line list.
527 unsigned RecordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
529 /// getRecordSourceLineCount - Return the number of source lines in the debug
531 unsigned getRecordSourceLineCount() const {
535 /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
536 /// timed. Look up the source id with the given directory and source file
537 /// names. If none currently exists, create a new id and insert it in the
538 /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
540 unsigned getOrCreateSourceID(const std::string &DirName,
541 const std::string &FileName);
543 /// ExtractScopeInformation - Scan machine instructions in this function
544 /// and collect DbgScopes. Return true, if atleast one scope was found.
545 bool ExtractScopeInformation(MachineFunction *MF);
547 /// CollectVariableInfo - Populate DbgScope entries with variables' info.
548 void CollectVariableInfo();
550 /// SetDbgScopeEndLabels - Update DbgScope end labels for the scopes that
551 /// end with this machine instruction.
552 void SetDbgScopeEndLabels(const MachineInstr *MI, unsigned Label);
554 /// BeginScope - Process beginning of a scope starting at Label.
555 void BeginScope(const MachineInstr *MI, unsigned Label);
557 /// EndScope - Prcess end of a scope.
558 void EndScope(const MachineInstr *MI);
560 } // End of namespace llvm