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"
18 #include "llvm/CodeGen/MachineLocation.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/UniqueVector.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/DebugLoc.h"
31 class DbgConcreteScope;
34 class MachineFrameInfo;
35 class MachineModuleInfo;
47 class DIGlobalVariable;
54 class DICompositeType;
56 //===----------------------------------------------------------------------===//
57 /// SrcLineInfo - This class is used to record source line correspondence.
60 unsigned Line; // Source line number.
61 unsigned Column; // Source column.
62 unsigned SourceID; // Source ID number.
63 MCSymbol *Label; // Label in code ID number.
65 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
66 : Line(L), Column(C), SourceID(S), Label(label) {}
69 unsigned getLine() const { return Line; }
70 unsigned getColumn() const { return Column; }
71 unsigned getSourceID() const { return SourceID; }
72 MCSymbol *getLabel() const { return Label; }
76 /// Asm - Target of Dwarf emission.
79 /// MMI - Collected machine module information.
80 MachineModuleInfo *MMI;
82 //===--------------------------------------------------------------------===//
83 // Attributes used to construct specific Dwarf sections.
87 DenseMap <const MDNode *, CompileUnit *> CUMap;
89 /// AbbreviationsSet - Used to uniquely define abbreviations.
91 FoldingSet<DIEAbbrev> AbbreviationsSet;
93 /// Abbreviations - A list of all the unique abbreviations in use.
95 std::vector<DIEAbbrev *> Abbreviations;
97 /// DirectoryIdMap - Directory name to directory id map.
99 StringMap<unsigned> DirectoryIdMap;
101 /// DirectoryNames - A list of directory names.
102 SmallVector<std::string, 8> DirectoryNames;
104 /// SourceFileIdMap - Source file name to source file id map.
106 StringMap<unsigned> SourceFileIdMap;
108 /// SourceFileNames - A list of source file names.
109 SmallVector<std::string, 8> SourceFileNames;
111 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
112 /// id mapped to a unique id.
113 DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
115 /// SourceIds - Reverse map from source id to directory id + file id pair.
117 SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
119 /// Lines - List of source line correspondence.
120 std::vector<SrcLineInfo> Lines;
122 /// DIEBlocks - A list of all the DIEBlocks in use.
123 std::vector<DIEBlock *> DIEBlocks;
125 // DIEValueAllocator - All DIEValues are allocated through this allocator.
126 BumpPtrAllocator DIEValueAllocator;
128 /// StringPool - A String->Symbol mapping of strings used by indirect
130 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
131 unsigned NextStringPoolNumber;
133 MCSymbol *getStringPoolEntry(StringRef Str);
135 /// SectionMap - Provides a unique id per text section.
137 UniqueVector<const MCSection*> SectionMap;
139 /// SectionSourceLines - Tracks line numbers per text section.
141 std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
143 // CurrentFnDbgScope - Top level scope for the current function.
145 DbgScope *CurrentFnDbgScope;
147 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
148 /// contained DbgScope*s.
150 DenseMap<const MDNode *, DbgScope *> DbgScopeMap;
152 /// ConcreteScopes - Tracks the concrete scopees in the current function.
153 /// These scopes are also included in DbgScopeMap.
154 DenseMap<const MDNode *, DbgScope *> ConcreteScopes;
156 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
157 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
158 DenseMap<const MDNode *, DbgScope *> AbstractScopes;
160 /// AbstractSPDies - Collection of abstract subprogram DIEs.
161 DenseMap<const MDNode *, DIE *> AbstractSPDies;
163 /// AbstractScopesList - Tracks abstract scopes constructed while processing
164 /// a function. This list is cleared during endFunction().
165 SmallVector<DbgScope *, 4>AbstractScopesList;
167 /// AbstractVariables - Collection on abstract variables. Owned by the
168 /// DbgScopes in AbstractScopes.
169 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
171 /// DbgVariableToFrameIndexMap - Tracks frame index used to find
172 /// variable's value.
173 DenseMap<const DbgVariable *, int> DbgVariableToFrameIndexMap;
175 /// DbgVariableToDbgInstMap - Maps DbgVariable to corresponding DBG_VALUE
176 /// machine instruction.
177 DenseMap<const DbgVariable *, const MachineInstr *> DbgVariableToDbgInstMap;
179 /// DotDebugLocEntry - This struct describes location entries emitted in
180 /// .debug_loc section.
181 typedef struct DotDebugLocEntry {
182 const MCSymbol *Begin;
185 DotDebugLocEntry() : Begin(0), End(0) {}
186 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
187 MachineLocation &L) : Begin(B), End(E), Loc(L) {}
188 /// Empty entries are also used as a trigger to emit temp label. Such
189 /// labels are referenced is used to find debug_loc offset for a given DIE.
190 bool isEmpty() { return Begin == 0 && End == 0; }
193 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
194 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
196 /// UseDotDebugLocEntry - DW_AT_location attributes for the DIEs in this set
197 /// idetifies corresponding .debug_loc entry offset.
198 SmallPtrSet<const DIE *, 4> UseDotDebugLocEntry;
200 /// VarToAbstractVarMap - Maps DbgVariable with corresponding Abstract
201 /// DbgVariable, if any.
202 DenseMap<const DbgVariable *, const DbgVariable *> VarToAbstractVarMap;
204 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
205 /// (at the end of the module) as DW_AT_inline.
206 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
208 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
209 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
210 /// corresponds to the MDNode mapped with the subprogram DIE.
211 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
213 typedef SmallVector<DbgScope *, 2> ScopeVector;
215 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
217 /// InlineInfo - Keep track of inlined functions and their location. This
218 /// information is used to populate debug_inlined section.
219 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
220 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
221 SmallVector<const MDNode *, 4> InlinedSPNodes;
223 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
224 // are processed to create DIEs.
225 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
227 /// LabelsBeforeInsn - Maps instruction with label emitted before
229 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
231 /// LabelsAfterInsn - Maps instruction with label emitted after
233 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
235 /// insnNeedsLabel - Collection of instructions that need a label to mark
236 /// a debuggging information entity.
237 SmallPtrSet<const MachineInstr *, 8> InsnNeedsLabel;
239 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
241 /// Previous instruction's location information. This is used to determine
242 /// label location to indicate scope boundries in dwarf debug info.
243 DebugLoc PrevInstLoc;
246 struct FunctionDebugFrameInfo {
248 std::vector<MachineMove> Moves;
250 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
251 : Number(Num), Moves(M) {}
254 std::vector<FunctionDebugFrameInfo> DebugFrames;
256 // Section Symbols: these are assembler temporary labels that are emitted at
257 // the beginning of each supported dwarf section. These are used to form
258 // section offsets and are created by EmitSectionLabels.
259 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
260 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
261 MCSymbol *DwarfDebugLocSectionSym;
262 MCSymbol *FunctionBeginSym, *FunctionEndSym;
264 DIEInteger *DIEIntegerOne;
267 /// getSourceDirectoryAndFileIds - Return the directory and file ids that
268 /// maps to the source id. Source id starts at 1.
269 std::pair<unsigned, unsigned>
270 getSourceDirectoryAndFileIds(unsigned SId) const {
271 return SourceIds[SId-1];
274 /// getNumSourceDirectories - Return the number of source directories in the
276 unsigned getNumSourceDirectories() const {
277 return DirectoryNames.size();
280 /// getSourceDirectoryName - Return the name of the directory corresponding
282 const std::string &getSourceDirectoryName(unsigned Id) const {
283 return DirectoryNames[Id - 1];
286 /// getSourceFileName - Return the name of the source file corresponding
288 const std::string &getSourceFileName(unsigned Id) const {
289 return SourceFileNames[Id - 1];
292 /// getNumSourceIds - Return the number of unique source ids.
293 unsigned getNumSourceIds() const {
294 return SourceIds.size();
297 /// assignAbbrevNumber - Define a unique number for the abbreviation.
299 void assignAbbrevNumber(DIEAbbrev &Abbrev);
301 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
302 /// information entry.
303 DIEEntry *createDIEEntry(DIE *Entry);
305 /// addUInt - Add an unsigned integer attribute data and value.
307 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
309 /// addSInt - Add an signed integer attribute data and value.
311 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
313 /// addString - Add a string attribute data and value.
315 void addString(DIE *Die, unsigned Attribute, unsigned Form,
316 const StringRef Str);
318 /// addLabel - Add a Dwarf label attribute data and value.
320 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
321 const MCSymbol *Label);
323 /// addDelta - Add a label delta attribute data and value.
325 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
326 const MCSymbol *Hi, const MCSymbol *Lo);
328 /// addDIEEntry - Add a DIE attribute data and value.
330 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
332 /// addBlock - Add block data.
334 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
336 /// addSourceLine - Add location information to specified debug information
338 void addSourceLine(DIE *Die, DIVariable V);
339 void addSourceLine(DIE *Die, DIGlobalVariable G);
340 void addSourceLine(DIE *Die, DISubprogram SP);
341 void addSourceLine(DIE *Die, DIType Ty);
342 void addSourceLine(DIE *Die, DINameSpace NS);
344 /// addAddress - Add an address attribute to a die based on the location
346 void addAddress(DIE *Die, unsigned Attribute,
347 const MachineLocation &Location);
349 /// addRegisterAddress - Add register location entry in variable DIE.
350 bool addRegisterAddress(DIE *Die, const MachineOperand &MO);
352 /// addConstantValue - Add constant value entry in variable DIE.
353 bool addConstantValue(DIE *Die, const MachineOperand &MO);
355 /// addConstantFPValue - Add constant value entry in variable DIE.
356 bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
358 /// addComplexAddress - Start with the address based on the location provided,
359 /// and generate the DWARF information necessary to find the actual variable
360 /// (navigating the extra location information encoded in the type) based on
361 /// the starting location. Add the DWARF information to the die.
363 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
364 const MachineLocation &Location);
366 // FIXME: Should be reformulated in terms of addComplexAddress.
367 /// addBlockByrefAddress - Start with the address based on the location
368 /// provided, and generate the DWARF information necessary to find the
369 /// actual Block variable (navigating the Block struct) based on the
370 /// starting location. Add the DWARF information to the die. Obsolete,
371 /// please use addComplexAddress instead.
373 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
374 const MachineLocation &Location);
376 /// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
377 /// on provided frame index.
378 void addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI);
380 /// addToContextOwner - Add Die into the list of its context owner's children.
381 void addToContextOwner(DIE *Die, DIDescriptor Context);
383 /// addType - Add a new type attribute to the specified entity.
384 void addType(DIE *Entity, DIType Ty);
387 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
388 DIE *getOrCreateNameSpace(DINameSpace NS);
390 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
392 DIE *getOrCreateTypeDIE(DIType Ty);
394 void addPubTypes(DISubprogram SP);
396 /// constructTypeDIE - Construct basic type die from DIBasicType.
397 void constructTypeDIE(DIE &Buffer,
400 /// constructTypeDIE - Construct derived type die from DIDerivedType.
401 void constructTypeDIE(DIE &Buffer,
404 /// constructTypeDIE - Construct type DIE from DICompositeType.
405 void constructTypeDIE(DIE &Buffer,
406 DICompositeType CTy);
408 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
409 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
411 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
412 void constructArrayTypeDIE(DIE &Buffer,
413 DICompositeType *CTy);
415 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
416 DIE *constructEnumTypeDIE(DIEnumerator ETy);
418 /// createMemberDIE - Create new member DIE.
419 DIE *createMemberDIE(DIDerivedType DT);
421 /// createSubprogramDIE - Create new DIE using SP.
422 DIE *createSubprogramDIE(DISubprogram SP);
424 /// getOrCreateDbgScope - Create DbgScope for the scope.
425 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
427 DbgScope *getOrCreateAbstractScope(const MDNode *N);
429 /// findAbstractVariable - Find abstract variable associated with Var.
430 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
432 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
433 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
434 /// If there are global variables in this scope then create and insert
435 /// DIEs for these variables.
436 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
438 /// constructLexicalScope - Construct new DW_TAG_lexical_block
439 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
440 DIE *constructLexicalScopeDIE(DbgScope *Scope);
442 /// constructInlinedScopeDIE - This scope represents inlined body of
443 /// a function. Construct DIE to represent this concrete inlined copy
445 DIE *constructInlinedScopeDIE(DbgScope *Scope);
447 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
448 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
450 /// constructScopeDIE - Construct a DIE for this scope.
451 DIE *constructScopeDIE(DbgScope *Scope);
453 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
454 /// the start of each one.
455 void EmitSectionLabels();
457 /// emitDIE - Recusively Emits a debug information entry.
459 void emitDIE(DIE *Die);
461 /// computeSizeAndOffset - Compute the size and offset of a DIE.
463 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
465 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
467 void computeSizeAndOffsets();
469 /// EmitDebugInfo - Emit the debug info section.
471 void emitDebugInfo();
473 /// emitAbbreviations - Emit the abbreviation section.
475 void emitAbbreviations() const;
477 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
480 void emitEndOfLineMatrix(unsigned SectionEnd);
482 /// emitDebugLines - Emit source line information.
484 void emitDebugLines();
486 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
488 void emitCommonDebugFrame();
490 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
492 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
494 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
496 void emitDebugPubNames();
498 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
500 void emitDebugPubTypes();
502 /// emitDebugStr - Emit visible names into a debug str section.
506 /// emitDebugLoc - Emit visible names into a debug loc section.
510 /// EmitDebugARanges - Emit visible names into a debug aranges section.
512 void EmitDebugARanges();
514 /// emitDebugRanges - Emit visible names into a debug ranges section.
516 void emitDebugRanges();
518 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
520 void emitDebugMacInfo();
522 /// emitDebugInlineInfo - Emit inline info using following format.
524 /// 1. length of section
525 /// 2. Dwarf version number
528 /// Entries (one "entry" for each function that was inlined):
530 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
531 /// otherwise offset into __debug_str for regular function name.
532 /// 2. offset into __debug_str section for regular function name.
533 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
534 /// instances for the function.
536 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
537 /// inlined instance; the die_offset points to the inlined_subroutine die in
538 /// the __debug_info section, and the low_pc is the starting address for the
539 /// inlining instance.
540 void emitDebugInlineInfo();
542 /// GetOrCreateSourceID - Look up the source id with the given directory and
543 /// source file names. If none currently exists, create a new id and insert it
544 /// in the SourceIds map. This can update DirectoryNames and SourceFileNames
546 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
548 /// constructCompileUnit - Create new CompileUnit for the given
549 /// metadata node with tag DW_TAG_compile_unit.
550 void constructCompileUnit(const MDNode *N);
552 /// getCompielUnit - Get CompileUnit DIE.
553 CompileUnit *getCompileUnit(const MDNode *N) const;
555 /// constructGlobalVariableDIE - Construct global variable DIE.
556 void constructGlobalVariableDIE(const MDNode *N);
558 /// construct SubprogramDIE - Construct subprogram DIE.
559 void constructSubprogramDIE(const MDNode *N);
561 /// recordSourceLine - Register a source line with debug info. Returns the
562 /// unique label that was emitted and which provides correspondence to
563 /// the source line list.
564 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
566 /// getSourceLineCount - Return the number of source lines in the debug
568 unsigned getSourceLineCount() const {
572 /// recordVariableFrameIndex - Record a variable's index.
573 void recordVariableFrameIndex(const DbgVariable *V, int Index);
575 /// findVariableFrameIndex - Return true if frame index for the variable
576 /// is found. Update FI to hold value of the index.
577 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
579 /// findDbgScope - Find DbgScope for the debug loc attached with an
581 DbgScope *findDbgScope(const MachineInstr *MI);
583 /// identifyScopeMarkers() - Indentify instructions that are marking
584 /// beginning of or end of a scope.
585 void identifyScopeMarkers();
587 /// extractScopeInformation - Scan machine instructions in this function
588 /// and collect DbgScopes. Return true, if atleast one scope was found.
589 bool extractScopeInformation();
591 /// collectVariableInfo - Populate DbgScope entries with variables' info.
592 void collectVariableInfo(const MachineFunction *,
593 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
595 /// collectVariableInfoFromMMITable - Collect variable information from
596 /// side table maintained by MMI.
597 void collectVariableInfoFromMMITable(const MachineFunction * MF,
598 SmallPtrSet<const MDNode *, 16> &P);
600 //===--------------------------------------------------------------------===//
601 // Main entry points.
603 DwarfDebug(AsmPrinter *A, Module *M);
606 /// beginModule - Emit all Dwarf sections that should come prior to the
608 void beginModule(Module *M);
610 /// endModule - Emit all Dwarf sections that should come after the content.
614 /// beginFunction - Gather pre-function debug information. Assumes being
615 /// emitted immediately after the function entry point.
616 void beginFunction(const MachineFunction *MF);
618 /// endFunction - Gather and emit post-function debug information.
620 void endFunction(const MachineFunction *MF);
622 /// getLabelBeforeInsn - Return Label preceding the instruction.
623 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
625 /// getLabelAfterInsn - Return Label immediately following the instruction.
626 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
628 /// beginInstruction - Process beginning of an instruction.
629 void beginInstruction(const MachineInstr *MI);
631 /// endInstruction - Prcess end of an instruction.
632 void endInstruction(const MachineInstr *MI);
634 } // End of namespace llvm