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 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
98 /// id mapped to a unique id.
99 StringMap<unsigned> SourceIdMap;
101 /// DIEBlocks - A list of all the DIEBlocks in use.
102 std::vector<DIEBlock *> DIEBlocks;
104 // DIEValueAllocator - All DIEValues are allocated through this allocator.
105 BumpPtrAllocator DIEValueAllocator;
107 /// StringPool - A String->Symbol mapping of strings used by indirect
109 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
110 unsigned NextStringPoolNumber;
112 MCSymbol *getStringPoolEntry(StringRef Str);
114 /// SectionMap - Provides a unique id per text section.
116 UniqueVector<const MCSection*> SectionMap;
118 // CurrentFnDbgScope - Top level scope for the current function.
120 DbgScope *CurrentFnDbgScope;
122 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
123 /// contained DbgScope*s.
125 DenseMap<const MDNode *, DbgScope *> DbgScopeMap;
127 /// ConcreteScopes - Tracks the concrete scopees in the current function.
128 /// These scopes are also included in DbgScopeMap.
129 DenseMap<const MDNode *, DbgScope *> ConcreteScopes;
131 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
132 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
133 DenseMap<const MDNode *, DbgScope *> AbstractScopes;
135 /// AbstractSPDies - Collection of abstract subprogram DIEs.
136 DenseMap<const MDNode *, DIE *> AbstractSPDies;
138 /// AbstractScopesList - Tracks abstract scopes constructed while processing
139 /// a function. This list is cleared during endFunction().
140 SmallVector<DbgScope *, 4>AbstractScopesList;
142 /// AbstractVariables - Collection on abstract variables. Owned by the
143 /// DbgScopes in AbstractScopes.
144 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
146 /// DbgVariableToFrameIndexMap - Tracks frame index used to find
147 /// variable's value.
148 DenseMap<const DbgVariable *, int> DbgVariableToFrameIndexMap;
150 /// DbgVariableToDbgInstMap - Maps DbgVariable to corresponding DBG_VALUE
151 /// machine instruction.
152 DenseMap<const DbgVariable *, const MachineInstr *> DbgVariableToDbgInstMap;
154 /// DotDebugLocEntry - This struct describes location entries emitted in
155 /// .debug_loc section.
156 typedef struct DotDebugLocEntry {
157 const MCSymbol *Begin;
160 DotDebugLocEntry() : Begin(0), End(0) {}
161 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
162 MachineLocation &L) : Begin(B), End(E), Loc(L) {}
163 /// Empty entries are also used as a trigger to emit temp label. Such
164 /// labels are referenced is used to find debug_loc offset for a given DIE.
165 bool isEmpty() { return Begin == 0 && End == 0; }
168 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
169 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
171 /// UseDotDebugLocEntry - DW_AT_location attributes for the DIEs in this set
172 /// idetifies corresponding .debug_loc entry offset.
173 SmallPtrSet<const DIE *, 4> UseDotDebugLocEntry;
175 /// VarToAbstractVarMap - Maps DbgVariable with corresponding Abstract
176 /// DbgVariable, if any.
177 DenseMap<const DbgVariable *, const DbgVariable *> VarToAbstractVarMap;
179 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
180 /// (at the end of the module) as DW_AT_inline.
181 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
183 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
184 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
185 /// corresponds to the MDNode mapped with the subprogram DIE.
186 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
188 typedef SmallVector<DbgScope *, 2> ScopeVector;
190 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
192 /// InlineInfo - Keep track of inlined functions and their location. This
193 /// information is used to populate debug_inlined section.
194 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
195 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
196 SmallVector<const MDNode *, 4> InlinedSPNodes;
198 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
199 // are processed to create DIEs.
200 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
202 /// LabelsBeforeInsn - Maps instruction with label emitted before
204 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
206 /// LabelsAfterInsn - Maps instruction with label emitted after
208 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
210 /// insnNeedsLabel - Collection of instructions that need a label to mark
211 /// a debuggging information entity.
212 SmallPtrSet<const MachineInstr *, 8> InsnNeedsLabel;
214 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
216 /// Previous instruction's location information. This is used to determine
217 /// label location to indicate scope boundries in dwarf debug info.
218 DebugLoc PrevInstLoc;
221 struct FunctionDebugFrameInfo {
223 std::vector<MachineMove> Moves;
225 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
226 : Number(Num), Moves(M) {}
229 std::vector<FunctionDebugFrameInfo> DebugFrames;
231 // Section Symbols: these are assembler temporary labels that are emitted at
232 // the beginning of each supported dwarf section. These are used to form
233 // section offsets and are created by EmitSectionLabels.
234 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
235 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
236 MCSymbol *DwarfDebugLocSectionSym;
237 MCSymbol *FunctionBeginSym, *FunctionEndSym;
239 DIEInteger *DIEIntegerOne;
242 /// getNumSourceIds - Return the number of unique source ids.
243 unsigned getNumSourceIds() const {
244 return SourceIdMap.size();
247 /// assignAbbrevNumber - Define a unique number for the abbreviation.
249 void assignAbbrevNumber(DIEAbbrev &Abbrev);
251 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
252 /// information entry.
253 DIEEntry *createDIEEntry(DIE *Entry);
255 /// addUInt - Add an unsigned integer attribute data and value.
257 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
259 /// addSInt - Add an signed integer attribute data and value.
261 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
263 /// addString - Add a string attribute data and value.
265 void addString(DIE *Die, unsigned Attribute, unsigned Form,
266 const StringRef Str);
268 /// addLabel - Add a Dwarf label attribute data and value.
270 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
271 const MCSymbol *Label);
273 /// addDelta - Add a label delta attribute data and value.
275 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
276 const MCSymbol *Hi, const MCSymbol *Lo);
278 /// addDIEEntry - Add a DIE attribute data and value.
280 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
282 /// addBlock - Add block data.
284 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
286 /// addSourceLine - Add location information to specified debug information
288 void addSourceLine(DIE *Die, DIVariable V);
289 void addSourceLine(DIE *Die, DIGlobalVariable G);
290 void addSourceLine(DIE *Die, DISubprogram SP);
291 void addSourceLine(DIE *Die, DIType Ty);
292 void addSourceLine(DIE *Die, DINameSpace NS);
294 /// addAddress - Add an address attribute to a die based on the location
296 void addAddress(DIE *Die, unsigned Attribute,
297 const MachineLocation &Location);
299 /// addRegisterAddress - Add register location entry in variable DIE.
300 bool addRegisterAddress(DIE *Die, const MachineOperand &MO);
302 /// addConstantValue - Add constant value entry in variable DIE.
303 bool addConstantValue(DIE *Die, const MachineOperand &MO);
304 bool addConstantValue(DIE *Die, ConstantInt *CI, bool Unsigned);
306 /// addConstantFPValue - Add constant value entry in variable DIE.
307 bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
309 /// addComplexAddress - Start with the address based on the location provided,
310 /// and generate the DWARF information necessary to find the actual variable
311 /// (navigating the extra location information encoded in the type) based on
312 /// the starting location. Add the DWARF information to the die.
314 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
315 const MachineLocation &Location);
317 // FIXME: Should be reformulated in terms of addComplexAddress.
318 /// addBlockByrefAddress - Start with the address based on the location
319 /// provided, and generate the DWARF information necessary to find the
320 /// actual Block variable (navigating the Block struct) based on the
321 /// starting location. Add the DWARF information to the die. Obsolete,
322 /// please use addComplexAddress instead.
324 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
325 const MachineLocation &Location);
327 /// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
328 /// on provided frame index.
329 void addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI);
331 /// addToContextOwner - Add Die into the list of its context owner's children.
332 void addToContextOwner(DIE *Die, DIDescriptor Context);
334 /// addType - Add a new type attribute to the specified entity.
335 void addType(DIE *Entity, DIType Ty);
338 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
339 DIE *getOrCreateNameSpace(DINameSpace NS);
341 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
343 DIE *getOrCreateTypeDIE(DIType Ty);
345 void addPubTypes(DISubprogram SP);
347 /// constructTypeDIE - Construct basic type die from DIBasicType.
348 void constructTypeDIE(DIE &Buffer,
351 /// constructTypeDIE - Construct derived type die from DIDerivedType.
352 void constructTypeDIE(DIE &Buffer,
355 /// constructTypeDIE - Construct type DIE from DICompositeType.
356 void constructTypeDIE(DIE &Buffer,
357 DICompositeType CTy);
359 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
360 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
362 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
363 void constructArrayTypeDIE(DIE &Buffer,
364 DICompositeType *CTy);
366 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
367 DIE *constructEnumTypeDIE(DIEnumerator ETy);
369 /// createMemberDIE - Create new member DIE.
370 DIE *createMemberDIE(DIDerivedType DT);
372 /// createSubprogramDIE - Create new DIE using SP.
373 DIE *createSubprogramDIE(DISubprogram SP);
375 /// getOrCreateDbgScope - Create DbgScope for the scope.
376 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
378 DbgScope *getOrCreateAbstractScope(const MDNode *N);
380 /// findAbstractVariable - Find abstract variable associated with Var.
381 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
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(const 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 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
435 void emitCommonDebugFrame();
437 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
439 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
441 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
443 void emitDebugPubNames();
445 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
447 void emitDebugPubTypes();
449 /// emitDebugStr - Emit visible names into a debug str section.
453 /// emitDebugLoc - Emit visible names into a debug loc section.
457 /// EmitDebugARanges - Emit visible names into a debug aranges section.
459 void EmitDebugARanges();
461 /// emitDebugRanges - Emit visible names into a debug ranges section.
463 void emitDebugRanges();
465 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
467 void emitDebugMacInfo();
469 /// emitDebugInlineInfo - Emit inline info using following format.
471 /// 1. length of section
472 /// 2. Dwarf version number
475 /// Entries (one "entry" for each function that was inlined):
477 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
478 /// otherwise offset into __debug_str for regular function name.
479 /// 2. offset into __debug_str section for regular function name.
480 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
481 /// instances for the function.
483 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
484 /// inlined instance; the die_offset points to the inlined_subroutine die in
485 /// the __debug_info section, and the low_pc is the starting address for the
486 /// inlining instance.
487 void emitDebugInlineInfo();
489 /// GetOrCreateSourceID - Look up the source id with the given directory and
490 /// source file names. If none currently exists, create a new id and insert it
491 /// in the SourceIds map.
492 unsigned GetOrCreateSourceID(StringRef FullName);
494 /// constructCompileUnit - Create new CompileUnit for the given
495 /// metadata node with tag DW_TAG_compile_unit.
496 void constructCompileUnit(const MDNode *N);
498 /// getCompielUnit - Get CompileUnit DIE.
499 CompileUnit *getCompileUnit(const MDNode *N) const;
501 /// constructGlobalVariableDIE - Construct global variable DIE.
502 void constructGlobalVariableDIE(const MDNode *N);
504 /// construct SubprogramDIE - Construct subprogram DIE.
505 void constructSubprogramDIE(const MDNode *N);
507 /// recordSourceLine - Register a source line with debug info. Returns the
508 /// unique label that was emitted and which provides correspondence to
509 /// the source line list.
510 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
512 /// recordVariableFrameIndex - Record a variable's index.
513 void recordVariableFrameIndex(const DbgVariable *V, int Index);
515 /// findVariableFrameIndex - Return true if frame index for the variable
516 /// is found. Update FI to hold value of the index.
517 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
519 /// findDbgScope - Find DbgScope for the debug loc attached with an
521 DbgScope *findDbgScope(const MachineInstr *MI);
523 /// identifyScopeMarkers() - Indentify instructions that are marking
524 /// beginning of or end of a scope.
525 void identifyScopeMarkers();
527 /// extractScopeInformation - Scan machine instructions in this function
528 /// and collect DbgScopes. Return true, if atleast one scope was found.
529 bool extractScopeInformation();
531 /// collectVariableInfo - Populate DbgScope entries with variables' info.
532 void collectVariableInfo(const MachineFunction *,
533 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
535 /// collectVariableInfoFromMMITable - Collect variable information from
536 /// side table maintained by MMI.
537 void collectVariableInfoFromMMITable(const MachineFunction * MF,
538 SmallPtrSet<const MDNode *, 16> &P);
540 //===--------------------------------------------------------------------===//
541 // Main entry points.
543 DwarfDebug(AsmPrinter *A, Module *M);
546 /// beginModule - Emit all Dwarf sections that should come prior to the
548 void beginModule(Module *M);
550 /// endModule - Emit all Dwarf sections that should come after the content.
554 /// beginFunction - Gather pre-function debug information. Assumes being
555 /// emitted immediately after the function entry point.
556 void beginFunction(const MachineFunction *MF);
558 /// endFunction - Gather and emit post-function debug information.
560 void endFunction(const MachineFunction *MF);
562 /// getLabelBeforeInsn - Return Label preceding the instruction.
563 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
565 /// getLabelAfterInsn - Return Label immediately following the instruction.
566 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
568 /// beginInstruction - Process beginning of an instruction.
569 void beginInstruction(const MachineInstr *MI);
571 /// endInstruction - Prcess end of an instruction.
572 void endInstruction(const MachineInstr *MI);
574 } // End of namespace llvm