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__
17 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/UniqueVector.h"
24 #include "llvm/Support/Allocator.h"
29 class DbgConcreteScope;
32 class MachineFrameInfo;
33 class MachineLocation;
34 class MachineModuleInfo;
45 class DIGlobalVariable;
52 class DICompositeType;
54 //===----------------------------------------------------------------------===//
55 /// SrcLineInfo - This class is used to record source line correspondence.
58 unsigned Line; // Source line number.
59 unsigned Column; // Source column.
60 unsigned SourceID; // Source ID number.
61 MCSymbol *Label; // Label in code ID number.
63 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
64 : Line(L), Column(C), SourceID(S), Label(label) {}
67 unsigned getLine() const { return Line; }
68 unsigned getColumn() const { return Column; }
69 unsigned getSourceID() const { return SourceID; }
70 MCSymbol *getLabel() const { return Label; }
74 /// Asm - Target of Dwarf emission.
77 /// MMI - Collected machine module information.
78 MachineModuleInfo *MMI;
80 //===--------------------------------------------------------------------===//
81 // Attributes used to construct specific Dwarf sections.
84 /// ModuleCU - All DIEs are inserted in ModuleCU.
85 CompileUnit *ModuleCU;
87 /// AbbreviationsSet - Used to uniquely define abbreviations.
89 FoldingSet<DIEAbbrev> AbbreviationsSet;
91 /// Abbreviations - A list of all the unique abbreviations in use.
93 std::vector<DIEAbbrev *> Abbreviations;
95 /// DirectoryIdMap - Directory name to directory id map.
97 StringMap<unsigned> DirectoryIdMap;
99 /// DirectoryNames - A list of directory names.
100 SmallVector<std::string, 8> DirectoryNames;
102 /// SourceFileIdMap - Source file name to source file id map.
104 StringMap<unsigned> SourceFileIdMap;
106 /// SourceFileNames - A list of source file names.
107 SmallVector<std::string, 8> SourceFileNames;
109 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
110 /// id mapped to a unique id.
111 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
113 /// SourceIds - Reverse map from source id to directory id + file id pair.
115 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
117 /// Lines - List of source line correspondence.
118 std::vector<SrcLineInfo> Lines;
120 /// DIEBlocks - A list of all the DIEBlocks in use.
121 std::vector<DIEBlock *> DIEBlocks;
123 // DIEValueAllocator - All DIEValues are allocated through this allocator.
124 BumpPtrAllocator DIEValueAllocator;
126 /// StringPool - A String->Symbol mapping of strings used by indirect
128 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
129 unsigned NextStringPoolNumber;
131 MCSymbol *getStringPoolEntry(StringRef Str);
133 /// SectionMap - Provides a unique id per text section.
135 UniqueVector<const MCSection*> SectionMap;
137 /// SectionSourceLines - Tracks line numbers per text section.
139 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
141 // CurrentFnDbgScope - Top level scope for the current function.
143 DbgScope *CurrentFnDbgScope;
145 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
146 /// contained DbgScope*s.
148 DenseMap<MDNode *, DbgScope *> DbgScopeMap;
150 /// ConcreteScopes - Tracks the concrete scopees in the current function.
151 /// These scopes are also included in DbgScopeMap.
152 DenseMap<MDNode *, DbgScope *> ConcreteScopes;
154 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
155 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
156 DenseMap<MDNode *, DbgScope *> AbstractScopes;
158 /// AbstractScopesList - Tracks abstract scopes constructed while processing
159 /// a function. This list is cleared during endFunction().
160 SmallVector<DbgScope *, 4>AbstractScopesList;
162 /// AbstractVariables - Collection on abstract variables. Owned by the
163 /// DbgScopes in AbstractScopes.
164 DenseMap<MDNode *, DbgVariable *> AbstractVariables;
166 /// DbgValueStartMap - Tracks starting scope of variable DIEs.
167 /// If the scope of an object begins sometime after the low pc value for the
168 /// scope most closely enclosing the object, the object entry may have a
169 /// DW_AT_start_scope attribute.
170 DenseMap<const MachineInstr *, DbgVariable *> DbgValueStartMap;
172 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
173 /// (at the end of the module) as DW_AT_inline.
174 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
176 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
177 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
178 /// corresponds to the MDNode mapped with the subprogram DIE.
179 DenseMap<DIE *, MDNode *> ContainingTypeMap;
181 typedef SmallVector<DbgScope *, 2> ScopeVector;
182 SmallPtrSet<const MachineInstr *, 8> InsnsBeginScopeSet;
183 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
185 /// InlineInfo - Keep track of inlined functions and their location. This
186 /// information is used to populate debug_inlined section.
187 typedef std::pair<MCSymbol*, DIE *> InlineInfoLabels;
188 DenseMap<MDNode*, SmallVector<InlineInfoLabels, 4> > InlineInfo;
189 SmallVector<MDNode *, 4> InlinedSPNodes;
191 /// LabelsBeforeInsn - Maps instruction with label emitted before
193 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
195 /// LabelsAfterInsn - Maps instruction with label emitted after
197 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
199 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
201 /// Previous instruction's location information. This is used to determine
202 /// label location to indicate scope boundries in dwarf debug info.
203 DebugLoc PrevInstLoc;
206 struct FunctionDebugFrameInfo {
208 std::vector<MachineMove> Moves;
210 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
211 : Number(Num), Moves(M) {}
214 std::vector<FunctionDebugFrameInfo> DebugFrames;
216 // Section Symbols: these are assembler temporary labels that are emitted at
217 // the beginning of each supported dwarf section. These are used to form
218 // section offsets and are created by EmitSectionLabels.
219 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
220 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
222 MCSymbol *FunctionBeginSym;
225 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
226 /// maps to the source id. Source id starts at 1.
227 std::pair<unsigned, unsigned>
228 getSourceDirectoryAndFileIds(unsigned SId) const {
229 return SourceIds[SId-1];
232 /// getNumSourceDirectories - Return the number of source directories in the
234 unsigned getNumSourceDirectories() const {
235 return DirectoryNames.size();
238 /// getSourceDirectoryName - Return the name of the directory corresponding
240 const std::string &getSourceDirectoryName(unsigned Id) const {
241 return DirectoryNames[Id - 1];
244 /// getSourceFileName - Return the name of the source file corresponding
246 const std::string &getSourceFileName(unsigned Id) const {
247 return SourceFileNames[Id - 1];
250 /// getNumSourceIds - Return the number of unique source ids.
251 unsigned getNumSourceIds() const {
252 return SourceIds.size();
255 /// assignAbbrevNumber - Define a unique number for the abbreviation.
257 void assignAbbrevNumber(DIEAbbrev &Abbrev);
259 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
260 /// information entry.
261 DIEEntry *createDIEEntry(DIE *Entry);
263 /// addUInt - Add an unsigned integer attribute data and value.
265 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
267 /// addSInt - Add an signed integer attribute data and value.
269 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
271 /// addString - Add a string attribute data and value.
273 void addString(DIE *Die, unsigned Attribute, unsigned Form,
274 const StringRef Str);
276 /// addLabel - Add a Dwarf label attribute data and value.
278 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
279 const MCSymbol *Label);
281 /// addDelta - Add a label delta attribute data and value.
283 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
284 const MCSymbol *Hi, const MCSymbol *Lo);
286 /// addDIEEntry - Add a DIE attribute data and value.
288 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
290 /// addBlock - Add block data.
292 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
294 /// addSourceLine - Add location information to specified debug information
296 void addSourceLine(DIE *Die, const DIVariable *V);
297 void addSourceLine(DIE *Die, const DIGlobal *G);
298 void addSourceLine(DIE *Die, const DISubprogram *SP);
299 void addSourceLine(DIE *Die, const DIType *Ty);
300 void addSourceLine(DIE *Die, const DINameSpace *NS);
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 /// addComplexAddress - Start with the address based on the location provided,
308 /// and generate the DWARF information necessary to find the actual variable
309 /// (navigating the extra location information encoded in the type) based on
310 /// the starting location. Add the DWARF information to the die.
312 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
313 const MachineLocation &Location);
315 // FIXME: Should be reformulated in terms of addComplexAddress.
316 /// addBlockByrefAddress - Start with the address based on the location
317 /// provided, and generate the DWARF information necessary to find the
318 /// actual Block variable (navigating the Block struct) based on the
319 /// starting location. Add the DWARF information to the die. Obsolete,
320 /// please use addComplexAddress instead.
322 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
323 const MachineLocation &Location);
325 /// addToContextOwner - Add Die into the list of its context owner's children.
326 void addToContextOwner(DIE *Die, DIDescriptor Context);
328 /// addType - Add a new type attribute to the specified entity.
329 void addType(DIE *Entity, DIType Ty);
332 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
333 DIE *getOrCreateNameSpace(DINameSpace NS);
335 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
337 DIE *getOrCreateTypeDIE(DIType Ty);
339 void addPubTypes(DISubprogram SP);
341 /// constructTypeDIE - Construct basic type die from DIBasicType.
342 void constructTypeDIE(DIE &Buffer,
345 /// constructTypeDIE - Construct derived type die from DIDerivedType.
346 void constructTypeDIE(DIE &Buffer,
349 /// constructTypeDIE - Construct type DIE from DICompositeType.
350 void constructTypeDIE(DIE &Buffer,
351 DICompositeType CTy);
353 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
354 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
356 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
357 void constructArrayTypeDIE(DIE &Buffer,
358 DICompositeType *CTy);
360 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
361 DIE *constructEnumTypeDIE(DIEnumerator ETy);
363 /// createGlobalVariableDIE - Create new DIE using GV.
364 DIE *createGlobalVariableDIE(const DIGlobalVariable &GV);
366 /// createMemberDIE - Create new member DIE.
367 DIE *createMemberDIE(const DIDerivedType &DT);
369 /// createSubprogramDIE - Create new DIE using SP.
370 DIE *createSubprogramDIE(const DISubprogram &SP, bool MakeDecl = false);
372 /// getOrCreateDbgScope - Create DbgScope for the scope.
373 DbgScope *getOrCreateDbgScope(MDNode *Scope, MDNode *InlinedAt);
375 DbgScope *getOrCreateAbstractScope(MDNode *N);
377 /// findAbstractVariable - Find abstract variable associated with Var.
378 DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx,
380 DbgVariable *findAbstractVariable(DIVariable &Var, const MachineInstr *MI,
383 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
384 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
385 /// If there are global variables in this scope then create and insert
386 /// DIEs for these variables.
387 DIE *updateSubprogramScopeDIE(MDNode *SPNode);
389 /// constructLexicalScope - Construct new DW_TAG_lexical_block
390 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
391 DIE *constructLexicalScopeDIE(DbgScope *Scope);
393 /// constructInlinedScopeDIE - This scope represents inlined body of
394 /// a function. Construct DIE to represent this concrete inlined copy
396 DIE *constructInlinedScopeDIE(DbgScope *Scope);
398 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
399 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
401 /// constructScopeDIE - Construct a DIE for this scope.
402 DIE *constructScopeDIE(DbgScope *Scope);
404 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
405 /// the start of each one.
406 void EmitSectionLabels();
408 /// emitDIE - Recusively Emits a debug information entry.
410 void emitDIE(DIE *Die);
412 /// computeSizeAndOffset - Compute the size and offset of a DIE.
414 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
416 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
418 void computeSizeAndOffsets();
420 /// EmitDebugInfo - Emit the debug info section.
422 void emitDebugInfo();
424 /// emitAbbreviations - Emit the abbreviation section.
426 void emitAbbreviations() const;
428 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
431 void emitEndOfLineMatrix(unsigned SectionEnd);
433 /// emitDebugLines - Emit source line information.
435 void emitDebugLines();
437 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
439 void emitCommonDebugFrame();
441 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
443 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
445 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
447 void emitDebugPubNames();
449 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
451 void emitDebugPubTypes();
453 /// emitDebugStr - Emit visible names into a debug str section.
457 /// emitDebugLoc - Emit visible names into a debug loc section.
461 /// EmitDebugARanges - Emit visible names into a debug aranges section.
463 void EmitDebugARanges();
465 /// emitDebugRanges - Emit visible names into a debug ranges section.
467 void emitDebugRanges();
469 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
471 void emitDebugMacInfo();
473 /// emitDebugInlineInfo - Emit inline info using following format.
475 /// 1. length of section
476 /// 2. Dwarf version number
479 /// Entries (one "entry" for each function that was inlined):
481 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
482 /// otherwise offset into __debug_str for regular function name.
483 /// 2. offset into __debug_str section for regular function name.
484 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
485 /// instances for the function.
487 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
488 /// inlined instance; the die_offset points to the inlined_subroutine die in
489 /// the __debug_info section, and the low_pc is the starting address for the
490 /// inlining instance.
491 void emitDebugInlineInfo();
493 /// GetOrCreateSourceID - Look up the source id with the given directory and
494 /// source file names. If none currently exists, create a new id and insert it
495 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
497 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
499 void constructCompileUnit(MDNode *N);
501 void constructGlobalVariableDIE(MDNode *N);
503 void constructSubprogramDIE(MDNode *N);
505 // FIXME: This should go away in favor of complex addresses.
506 /// Find the type the programmer originally declared the variable to be
507 /// and return that type. Obsolete, use GetComplexAddrType instead.
509 DIType getBlockByrefType(DIType Ty, std::string Name);
511 /// recordSourceLine - Register a source line with debug info. Returns the
512 /// unique label that was emitted and which provides correspondence to
513 /// the source line list.
514 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
516 /// getSourceLineCount - Return the number of source lines in the debug
518 unsigned getSourceLineCount() const {
522 /// identifyScopeMarkers() - Indentify instructions that are marking
523 /// beginning of or end of a scope.
524 void identifyScopeMarkers();
526 /// extractScopeInformation - Scan machine instructions in this function
527 /// and collect DbgScopes. Return true, if atleast one scope was found.
528 bool extractScopeInformation();
530 /// collectVariableInfo - Populate DbgScope entries with variables' info.
531 void collectVariableInfo();
534 //===--------------------------------------------------------------------===//
535 // Main entry points.
537 DwarfDebug(AsmPrinter *A, Module *M);
540 /// beginModule - Emit all Dwarf sections that should come prior to the
542 void beginModule(Module *M);
544 /// endModule - Emit all Dwarf sections that should come after the content.
548 /// beginFunction - Gather pre-function debug information. Assumes being
549 /// emitted immediately after the function entry point.
550 void beginFunction(const MachineFunction *MF);
552 /// endFunction - Gather and emit post-function debug information.
554 void endFunction(const MachineFunction *MF);
556 /// beginScope - Process beginning of a scope.
557 void beginScope(const MachineInstr *MI);
559 /// endScope - Prcess end of a scope.
560 void endScope(const MachineInstr *MI);
562 } // End of namespace llvm