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);
305 /// addConstantFPValue - Add constant value entry in variable DIE.
306 bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
308 /// addComplexAddress - Start with the address based on the location provided,
309 /// and generate the DWARF information necessary to find the actual variable
310 /// (navigating the extra location information encoded in the type) based on
311 /// the starting location. Add the DWARF information to the die.
313 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
314 const MachineLocation &Location);
316 // FIXME: Should be reformulated in terms of addComplexAddress.
317 /// addBlockByrefAddress - Start with the address based on the location
318 /// provided, and generate the DWARF information necessary to find the
319 /// actual Block variable (navigating the Block struct) based on the
320 /// starting location. Add the DWARF information to the die. Obsolete,
321 /// please use addComplexAddress instead.
323 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
324 const MachineLocation &Location);
326 /// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
327 /// on provided frame index.
328 void addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI);
330 /// addToContextOwner - Add Die into the list of its context owner's children.
331 void addToContextOwner(DIE *Die, DIDescriptor Context);
333 /// addType - Add a new type attribute to the specified entity.
334 void addType(DIE *Entity, DIType Ty);
337 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
338 DIE *getOrCreateNameSpace(DINameSpace NS);
340 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
342 DIE *getOrCreateTypeDIE(DIType Ty);
344 void addPubTypes(DISubprogram SP);
346 /// constructTypeDIE - Construct basic type die from DIBasicType.
347 void constructTypeDIE(DIE &Buffer,
350 /// constructTypeDIE - Construct derived type die from DIDerivedType.
351 void constructTypeDIE(DIE &Buffer,
354 /// constructTypeDIE - Construct type DIE from DICompositeType.
355 void constructTypeDIE(DIE &Buffer,
356 DICompositeType CTy);
358 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
359 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
361 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
362 void constructArrayTypeDIE(DIE &Buffer,
363 DICompositeType *CTy);
365 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
366 DIE *constructEnumTypeDIE(DIEnumerator ETy);
368 /// createMemberDIE - Create new member DIE.
369 DIE *createMemberDIE(DIDerivedType DT);
371 /// createSubprogramDIE - Create new DIE using SP.
372 DIE *createSubprogramDIE(DISubprogram SP);
374 /// getOrCreateDbgScope - Create DbgScope for the scope.
375 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
377 DbgScope *getOrCreateAbstractScope(const MDNode *N);
379 /// findAbstractVariable - Find abstract variable associated with Var.
380 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
382 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
383 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
384 /// If there are global variables in this scope then create and insert
385 /// DIEs for these variables.
386 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
388 /// constructLexicalScope - Construct new DW_TAG_lexical_block
389 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
390 DIE *constructLexicalScopeDIE(DbgScope *Scope);
392 /// constructInlinedScopeDIE - This scope represents inlined body of
393 /// a function. Construct DIE to represent this concrete inlined copy
395 DIE *constructInlinedScopeDIE(DbgScope *Scope);
397 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
398 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
400 /// constructScopeDIE - Construct a DIE for this scope.
401 DIE *constructScopeDIE(DbgScope *Scope);
403 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
404 /// the start of each one.
405 void EmitSectionLabels();
407 /// emitDIE - Recusively Emits a debug information entry.
409 void emitDIE(DIE *Die);
411 /// computeSizeAndOffset - Compute the size and offset of a DIE.
413 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
415 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
417 void computeSizeAndOffsets();
419 /// EmitDebugInfo - Emit the debug info section.
421 void emitDebugInfo();
423 /// emitAbbreviations - Emit the abbreviation section.
425 void emitAbbreviations() const;
427 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
430 void emitEndOfLineMatrix(unsigned SectionEnd);
432 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
434 void emitCommonDebugFrame();
436 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
438 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
440 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
442 void emitDebugPubNames();
444 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
446 void emitDebugPubTypes();
448 /// emitDebugStr - Emit visible names into a debug str section.
452 /// emitDebugLoc - Emit visible names into a debug loc section.
456 /// EmitDebugARanges - Emit visible names into a debug aranges section.
458 void EmitDebugARanges();
460 /// emitDebugRanges - Emit visible names into a debug ranges section.
462 void emitDebugRanges();
464 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
466 void emitDebugMacInfo();
468 /// emitDebugInlineInfo - Emit inline info using following format.
470 /// 1. length of section
471 /// 2. Dwarf version number
474 /// Entries (one "entry" for each function that was inlined):
476 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
477 /// otherwise offset into __debug_str for regular function name.
478 /// 2. offset into __debug_str section for regular function name.
479 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
480 /// instances for the function.
482 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
483 /// inlined instance; the die_offset points to the inlined_subroutine die in
484 /// the __debug_info section, and the low_pc is the starting address for the
485 /// inlining instance.
486 void emitDebugInlineInfo();
488 /// GetOrCreateSourceID - Look up the source id with the given directory and
489 /// source file names. If none currently exists, create a new id and insert it
490 /// in the SourceIds map.
491 unsigned GetOrCreateSourceID(StringRef FullName);
493 /// constructCompileUnit - Create new CompileUnit for the given
494 /// metadata node with tag DW_TAG_compile_unit.
495 void constructCompileUnit(const MDNode *N);
497 /// getCompielUnit - Get CompileUnit DIE.
498 CompileUnit *getCompileUnit(const MDNode *N) const;
500 /// constructGlobalVariableDIE - Construct global variable DIE.
501 void constructGlobalVariableDIE(const MDNode *N);
503 /// construct SubprogramDIE - Construct subprogram DIE.
504 void constructSubprogramDIE(const MDNode *N);
506 /// recordSourceLine - Register a source line with debug info. Returns the
507 /// unique label that was emitted and which provides correspondence to
508 /// the source line list.
509 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
511 /// recordVariableFrameIndex - Record a variable's index.
512 void recordVariableFrameIndex(const DbgVariable *V, int Index);
514 /// findVariableFrameIndex - Return true if frame index for the variable
515 /// is found. Update FI to hold value of the index.
516 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
518 /// findDbgScope - Find DbgScope for the debug loc attached with an
520 DbgScope *findDbgScope(const MachineInstr *MI);
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(const MachineFunction *,
532 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
534 /// collectVariableInfoFromMMITable - Collect variable information from
535 /// side table maintained by MMI.
536 void collectVariableInfoFromMMITable(const MachineFunction * MF,
537 SmallPtrSet<const MDNode *, 16> &P);
539 //===--------------------------------------------------------------------===//
540 // Main entry points.
542 DwarfDebug(AsmPrinter *A, Module *M);
545 /// beginModule - Emit all Dwarf sections that should come prior to the
547 void beginModule(Module *M);
549 /// endModule - Emit all Dwarf sections that should come after the content.
553 /// beginFunction - Gather pre-function debug information. Assumes being
554 /// emitted immediately after the function entry point.
555 void beginFunction(const MachineFunction *MF);
557 /// endFunction - Gather and emit post-function debug information.
559 void endFunction(const MachineFunction *MF);
561 /// getLabelBeforeInsn - Return Label preceding the instruction.
562 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
564 /// getLabelAfterInsn - Return Label immediately following the instruction.
565 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
567 /// beginInstruction - Process beginning of an instruction.
568 void beginInstruction(const MachineInstr *MI);
570 /// endInstruction - Prcess end of an instruction.
571 void endInstruction(const MachineInstr *MI);
573 } // End of namespace llvm