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;
55 class DITemplateTypeParameter;
56 class DITemplateValueParameter;
58 //===----------------------------------------------------------------------===//
59 /// SrcLineInfo - This class is used to record source line correspondence.
62 unsigned Line; // Source line number.
63 unsigned Column; // Source column.
64 unsigned SourceID; // Source ID number.
65 MCSymbol *Label; // Label in code ID number.
67 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
68 : Line(L), Column(C), SourceID(S), Label(label) {}
71 unsigned getLine() const { return Line; }
72 unsigned getColumn() const { return Column; }
73 unsigned getSourceID() const { return SourceID; }
74 MCSymbol *getLabel() const { return Label; }
78 /// Asm - Target of Dwarf emission.
81 /// MMI - Collected machine module information.
82 MachineModuleInfo *MMI;
84 //===--------------------------------------------------------------------===//
85 // Attributes used to construct specific Dwarf sections.
89 DenseMap <const MDNode *, CompileUnit *> CUMap;
91 /// AbbreviationsSet - Used to uniquely define abbreviations.
93 FoldingSet<DIEAbbrev> AbbreviationsSet;
95 /// Abbreviations - A list of all the unique abbreviations in use.
97 std::vector<DIEAbbrev *> Abbreviations;
99 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
100 /// id mapped to a unique id.
101 StringMap<unsigned> SourceIdMap;
103 /// DIEBlocks - A list of all the DIEBlocks in use.
104 std::vector<DIEBlock *> DIEBlocks;
106 // DIEValueAllocator - All DIEValues are allocated through this allocator.
107 BumpPtrAllocator DIEValueAllocator;
109 /// StringPool - A String->Symbol mapping of strings used by indirect
111 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
112 unsigned NextStringPoolNumber;
114 MCSymbol *getStringPoolEntry(StringRef Str);
116 /// SectionMap - Provides a unique id per text section.
118 UniqueVector<const MCSection*> SectionMap;
120 // CurrentFnDbgScope - Top level scope for the current function.
122 DbgScope *CurrentFnDbgScope;
124 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
125 /// contained DbgScope*s.
127 DenseMap<const MDNode *, DbgScope *> DbgScopeMap;
129 /// ConcreteScopes - Tracks the concrete scopees in the current function.
130 /// These scopes are also included in DbgScopeMap.
131 DenseMap<const MDNode *, DbgScope *> ConcreteScopes;
133 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
134 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
135 DenseMap<const MDNode *, DbgScope *> AbstractScopes;
137 /// AbstractSPDies - Collection of abstract subprogram DIEs.
138 DenseMap<const MDNode *, DIE *> AbstractSPDies;
140 /// AbstractScopesList - Tracks abstract scopes constructed while processing
141 /// a function. This list is cleared during endFunction().
142 SmallVector<DbgScope *, 4>AbstractScopesList;
144 /// AbstractVariables - Collection on abstract variables. Owned by the
145 /// DbgScopes in AbstractScopes.
146 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
148 /// DbgVariableToFrameIndexMap - Tracks frame index used to find
149 /// variable's value.
150 DenseMap<const DbgVariable *, int> DbgVariableToFrameIndexMap;
152 /// DbgVariableToDbgInstMap - Maps DbgVariable to corresponding DBG_VALUE
153 /// machine instruction.
154 DenseMap<const DbgVariable *, const MachineInstr *> DbgVariableToDbgInstMap;
156 /// DotDebugLocEntry - This struct describes location entries emitted in
157 /// .debug_loc section.
158 typedef struct DotDebugLocEntry {
159 const MCSymbol *Begin;
162 DotDebugLocEntry() : Begin(0), End(0) {}
163 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
164 MachineLocation &L) : Begin(B), End(E), Loc(L) {}
165 /// Empty entries are also used as a trigger to emit temp label. Such
166 /// labels are referenced is used to find debug_loc offset for a given DIE.
167 bool isEmpty() { return Begin == 0 && End == 0; }
170 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
171 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
173 /// UseDotDebugLocEntry - DW_AT_location attributes for the DIEs in this set
174 /// idetifies corresponding .debug_loc entry offset.
175 SmallPtrSet<const DIE *, 4> UseDotDebugLocEntry;
177 /// VarToAbstractVarMap - Maps DbgVariable with corresponding Abstract
178 /// DbgVariable, if any.
179 DenseMap<const DbgVariable *, const DbgVariable *> VarToAbstractVarMap;
181 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
182 /// (at the end of the module) as DW_AT_inline.
183 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
185 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
186 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
187 /// corresponds to the MDNode mapped with the subprogram DIE.
188 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
190 typedef SmallVector<DbgScope *, 2> ScopeVector;
192 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
194 /// InlineInfo - Keep track of inlined functions and their location. This
195 /// information is used to populate debug_inlined section.
196 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
197 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
198 SmallVector<const MDNode *, 4> InlinedSPNodes;
200 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
201 // are processed to create DIEs.
202 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
204 /// LabelsBeforeInsn - Maps instruction with label emitted before
206 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
208 /// LabelsAfterInsn - Maps instruction with label emitted after
210 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
212 /// insnNeedsLabel - Collection of instructions that need a label to mark
213 /// a debuggging information entity.
214 SmallPtrSet<const MachineInstr *, 8> InsnNeedsLabel;
216 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
218 /// Previous instruction's location information. This is used to determine
219 /// label location to indicate scope boundries in dwarf debug info.
220 DebugLoc PrevInstLoc;
223 struct FunctionDebugFrameInfo {
225 std::vector<MachineMove> Moves;
227 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
228 : Number(Num), Moves(M) {}
231 std::vector<FunctionDebugFrameInfo> DebugFrames;
233 // Section Symbols: these are assembler temporary labels that are emitted at
234 // the beginning of each supported dwarf section. These are used to form
235 // section offsets and are created by EmitSectionLabels.
236 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
237 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
238 MCSymbol *DwarfDebugLocSectionSym;
239 MCSymbol *FunctionBeginSym, *FunctionEndSym;
241 DIEInteger *DIEIntegerOne;
244 /// getNumSourceIds - Return the number of unique source ids.
245 unsigned getNumSourceIds() const {
246 return SourceIdMap.size();
249 /// assignAbbrevNumber - Define a unique number for the abbreviation.
251 void assignAbbrevNumber(DIEAbbrev &Abbrev);
253 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
254 /// information entry.
255 DIEEntry *createDIEEntry(DIE *Entry);
257 /// addUInt - Add an unsigned integer attribute data and value.
259 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
261 /// addSInt - Add an signed integer attribute data and value.
263 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
265 /// addString - Add a string attribute data and value.
267 void addString(DIE *Die, unsigned Attribute, unsigned Form,
268 const StringRef Str);
270 /// addLabel - Add a Dwarf label attribute data and value.
272 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
273 const MCSymbol *Label);
275 /// addDelta - Add a label delta attribute data and value.
277 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
278 const MCSymbol *Hi, const MCSymbol *Lo);
280 /// addDIEEntry - Add a DIE attribute data and value.
282 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *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, DIVariable V);
291 void addSourceLine(DIE *Die, DIGlobalVariable G);
292 void addSourceLine(DIE *Die, DISubprogram SP);
293 void addSourceLine(DIE *Die, DIType Ty);
294 void addSourceLine(DIE *Die, DINameSpace NS);
296 /// addAddress - Add an address attribute to a die based on the location
298 void addAddress(DIE *Die, unsigned Attribute,
299 const MachineLocation &Location);
301 /// addRegisterAddress - Add register location entry in variable DIE.
302 bool addRegisterAddress(DIE *Die, const MachineOperand &MO);
304 /// addConstantValue - Add constant value entry in variable DIE.
305 bool addConstantValue(DIE *Die, const MachineOperand &MO);
306 bool addConstantValue(DIE *Die, ConstantInt *CI, bool Unsigned);
308 /// addConstantFPValue - Add constant value entry in variable DIE.
309 bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
311 /// addComplexAddress - Start with the address based on the location provided,
312 /// and generate the DWARF information necessary to find the actual variable
313 /// (navigating the extra location information encoded in the type) based on
314 /// the starting location. Add the DWARF information to the die.
316 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
317 const MachineLocation &Location);
319 // FIXME: Should be reformulated in terms of addComplexAddress.
320 /// addBlockByrefAddress - Start with the address based on the location
321 /// provided, and generate the DWARF information necessary to find the
322 /// actual Block variable (navigating the Block struct) based on the
323 /// starting location. Add the DWARF information to the die. Obsolete,
324 /// please use addComplexAddress instead.
326 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
327 const MachineLocation &Location);
329 /// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
330 /// on provided frame index.
331 void addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI);
333 /// addToContextOwner - Add Die into the list of its context owner's children.
334 void addToContextOwner(DIE *Die, DIDescriptor Context);
336 /// addType - Add a new type attribute to the specified entity.
337 void addType(DIE *Entity, DIType Ty);
340 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
341 DIE *getOrCreateNameSpace(DINameSpace NS);
343 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
345 DIE *getOrCreateTypeDIE(DIType Ty);
347 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
348 /// for the given DITemplateTypeParameter.
349 DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
351 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
352 /// for the given DITemplateValueParameter.
353 DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
355 void addPubTypes(DISubprogram SP);
357 /// constructTypeDIE - Construct basic type die from DIBasicType.
358 void constructTypeDIE(DIE &Buffer,
361 /// constructTypeDIE - Construct derived type die from DIDerivedType.
362 void constructTypeDIE(DIE &Buffer,
365 /// constructTypeDIE - Construct type DIE from DICompositeType.
366 void constructTypeDIE(DIE &Buffer,
367 DICompositeType CTy);
369 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
370 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
372 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
373 void constructArrayTypeDIE(DIE &Buffer,
374 DICompositeType *CTy);
376 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
377 DIE *constructEnumTypeDIE(DIEnumerator ETy);
379 /// createMemberDIE - Create new member DIE.
380 DIE *createMemberDIE(DIDerivedType DT);
382 /// createSubprogramDIE - Create new DIE using SP.
383 DIE *createSubprogramDIE(DISubprogram SP);
385 /// getOrCreateDbgScope - Create DbgScope for the scope.
386 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
388 DbgScope *getOrCreateAbstractScope(const MDNode *N);
390 /// findAbstractVariable - Find abstract variable associated with Var.
391 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
393 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
394 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
395 /// If there are global variables in this scope then create and insert
396 /// DIEs for these variables.
397 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
399 /// constructLexicalScope - Construct new DW_TAG_lexical_block
400 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
401 DIE *constructLexicalScopeDIE(DbgScope *Scope);
403 /// constructInlinedScopeDIE - This scope represents inlined body of
404 /// a function. Construct DIE to represent this concrete inlined copy
406 DIE *constructInlinedScopeDIE(DbgScope *Scope);
408 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
409 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
411 /// constructScopeDIE - Construct a DIE for this scope.
412 DIE *constructScopeDIE(DbgScope *Scope);
414 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
415 /// the start of each one.
416 void EmitSectionLabels();
418 /// emitDIE - Recusively Emits a debug information entry.
420 void emitDIE(DIE *Die);
422 /// computeSizeAndOffset - Compute the size and offset of a DIE.
424 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
426 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
428 void computeSizeAndOffsets();
430 /// EmitDebugInfo - Emit the debug info section.
432 void emitDebugInfo();
434 /// emitAbbreviations - Emit the abbreviation section.
436 void emitAbbreviations() const;
438 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
441 void emitEndOfLineMatrix(unsigned SectionEnd);
443 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
445 void emitCommonDebugFrame();
447 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
449 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
451 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
453 void emitDebugPubNames();
455 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
457 void emitDebugPubTypes();
459 /// emitDebugStr - Emit visible names into a debug str section.
463 /// emitDebugLoc - Emit visible names into a debug loc section.
467 /// EmitDebugARanges - Emit visible names into a debug aranges section.
469 void EmitDebugARanges();
471 /// emitDebugRanges - Emit visible names into a debug ranges section.
473 void emitDebugRanges();
475 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
477 void emitDebugMacInfo();
479 /// emitDebugInlineInfo - Emit inline info using following format.
481 /// 1. length of section
482 /// 2. Dwarf version number
485 /// Entries (one "entry" for each function that was inlined):
487 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
488 /// otherwise offset into __debug_str for regular function name.
489 /// 2. offset into __debug_str section for regular function name.
490 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
491 /// instances for the function.
493 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
494 /// inlined instance; the die_offset points to the inlined_subroutine die in
495 /// the __debug_info section, and the low_pc is the starting address for the
496 /// inlining instance.
497 void emitDebugInlineInfo();
499 /// GetOrCreateSourceID - Look up the source id with the given directory and
500 /// source file names. If none currently exists, create a new id and insert it
501 /// in the SourceIds map.
502 unsigned GetOrCreateSourceID(StringRef FullName);
504 /// constructCompileUnit - Create new CompileUnit for the given
505 /// metadata node with tag DW_TAG_compile_unit.
506 void constructCompileUnit(const MDNode *N);
508 /// getCompielUnit - Get CompileUnit DIE.
509 CompileUnit *getCompileUnit(const MDNode *N) const;
511 /// constructGlobalVariableDIE - Construct global variable DIE.
512 void constructGlobalVariableDIE(const MDNode *N);
514 /// construct SubprogramDIE - Construct subprogram DIE.
515 void constructSubprogramDIE(const MDNode *N);
517 /// recordSourceLine - Register a source line with debug info. Returns the
518 /// unique label that was emitted and which provides correspondence to
519 /// the source line list.
520 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
522 /// recordVariableFrameIndex - Record a variable's index.
523 void recordVariableFrameIndex(const DbgVariable *V, int Index);
525 /// findVariableFrameIndex - Return true if frame index for the variable
526 /// is found. Update FI to hold value of the index.
527 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
529 /// findDbgScope - Find DbgScope for the debug loc attached with an
531 DbgScope *findDbgScope(const MachineInstr *MI);
533 /// identifyScopeMarkers() - Indentify instructions that are marking
534 /// beginning of or end of a scope.
535 void identifyScopeMarkers();
537 /// extractScopeInformation - Scan machine instructions in this function
538 /// and collect DbgScopes. Return true, if atleast one scope was found.
539 bool extractScopeInformation();
541 /// collectVariableInfo - Populate DbgScope entries with variables' info.
542 void collectVariableInfo(const MachineFunction *,
543 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
545 /// collectVariableInfoFromMMITable - Collect variable information from
546 /// side table maintained by MMI.
547 void collectVariableInfoFromMMITable(const MachineFunction * MF,
548 SmallPtrSet<const MDNode *, 16> &P);
550 //===--------------------------------------------------------------------===//
551 // Main entry points.
553 DwarfDebug(AsmPrinter *A, Module *M);
556 /// beginModule - Emit all Dwarf sections that should come prior to the
558 void beginModule(Module *M);
560 /// endModule - Emit all Dwarf sections that should come after the content.
564 /// beginFunction - Gather pre-function debug information. Assumes being
565 /// emitted immediately after the function entry point.
566 void beginFunction(const MachineFunction *MF);
568 /// endFunction - Gather and emit post-function debug information.
570 void endFunction(const MachineFunction *MF);
572 /// getLabelBeforeInsn - Return Label preceding the instruction.
573 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
575 /// getLabelAfterInsn - Return Label immediately following the instruction.
576 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
578 /// beginInstruction - Process beginning of an instruction.
579 void beginInstruction(const MachineInstr *MI);
581 /// endInstruction - Prcess end of an instruction.
582 void endInstruction(const MachineInstr *MI);
584 } // End of namespace llvm