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;
57 //===----------------------------------------------------------------------===//
58 /// SrcLineInfo - This class is used to record source line correspondence.
61 unsigned Line; // Source line number.
62 unsigned Column; // Source column.
63 unsigned SourceID; // Source ID number.
64 MCSymbol *Label; // Label in code ID number.
66 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
67 : Line(L), Column(C), SourceID(S), Label(label) {}
70 unsigned getLine() const { return Line; }
71 unsigned getColumn() const { return Column; }
72 unsigned getSourceID() const { return SourceID; }
73 MCSymbol *getLabel() const { return Label; }
77 /// Asm - Target of Dwarf emission.
80 /// MMI - Collected machine module information.
81 MachineModuleInfo *MMI;
83 //===--------------------------------------------------------------------===//
84 // Attributes used to construct specific Dwarf sections.
88 DenseMap <const MDNode *, CompileUnit *> CUMap;
90 /// AbbreviationsSet - Used to uniquely define abbreviations.
92 FoldingSet<DIEAbbrev> AbbreviationsSet;
94 /// Abbreviations - A list of all the unique abbreviations in use.
96 std::vector<DIEAbbrev *> Abbreviations;
98 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
99 /// id mapped to a unique id.
100 StringMap<unsigned> SourceIdMap;
102 /// DIEBlocks - A list of all the DIEBlocks in use.
103 std::vector<DIEBlock *> DIEBlocks;
105 // DIEValueAllocator - All DIEValues are allocated through this allocator.
106 BumpPtrAllocator DIEValueAllocator;
108 /// StringPool - A String->Symbol mapping of strings used by indirect
110 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
111 unsigned NextStringPoolNumber;
113 MCSymbol *getStringPoolEntry(StringRef Str);
115 /// SectionMap - Provides a unique id per text section.
117 UniqueVector<const MCSection*> SectionMap;
119 // CurrentFnDbgScope - Top level scope for the current function.
121 DbgScope *CurrentFnDbgScope;
123 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
124 /// contained DbgScope*s.
126 DenseMap<const MDNode *, DbgScope *> DbgScopeMap;
128 /// ConcreteScopes - Tracks the concrete scopees in the current function.
129 /// These scopes are also included in DbgScopeMap.
130 DenseMap<const MDNode *, DbgScope *> ConcreteScopes;
132 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
133 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
134 DenseMap<const MDNode *, DbgScope *> AbstractScopes;
136 /// AbstractSPDies - Collection of abstract subprogram DIEs.
137 DenseMap<const MDNode *, DIE *> AbstractSPDies;
139 /// AbstractScopesList - Tracks abstract scopes constructed while processing
140 /// a function. This list is cleared during endFunction().
141 SmallVector<DbgScope *, 4>AbstractScopesList;
143 /// AbstractVariables - Collection on abstract variables. Owned by the
144 /// DbgScopes in AbstractScopes.
145 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
147 /// DbgVariableToFrameIndexMap - Tracks frame index used to find
148 /// variable's value.
149 DenseMap<const DbgVariable *, int> DbgVariableToFrameIndexMap;
151 /// DbgVariableToDbgInstMap - Maps DbgVariable to corresponding DBG_VALUE
152 /// machine instruction.
153 DenseMap<const DbgVariable *, const MachineInstr *> DbgVariableToDbgInstMap;
155 /// DotDebugLocEntry - This struct describes location entries emitted in
156 /// .debug_loc section.
157 typedef struct DotDebugLocEntry {
158 const MCSymbol *Begin;
161 DotDebugLocEntry() : Begin(0), End(0) {}
162 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
163 MachineLocation &L) : Begin(B), End(E), Loc(L) {}
164 /// Empty entries are also used as a trigger to emit temp label. Such
165 /// labels are referenced is used to find debug_loc offset for a given DIE.
166 bool isEmpty() { return Begin == 0 && End == 0; }
169 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
170 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
172 /// UseDotDebugLocEntry - DW_AT_location attributes for the DIEs in this set
173 /// idetifies corresponding .debug_loc entry offset.
174 SmallPtrSet<const DIE *, 4> UseDotDebugLocEntry;
176 /// VarToAbstractVarMap - Maps DbgVariable with corresponding Abstract
177 /// DbgVariable, if any.
178 DenseMap<const DbgVariable *, const DbgVariable *> VarToAbstractVarMap;
180 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
181 /// (at the end of the module) as DW_AT_inline.
182 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
184 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
185 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
186 /// corresponds to the MDNode mapped with the subprogram DIE.
187 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
189 typedef SmallVector<DbgScope *, 2> ScopeVector;
191 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
193 /// InlineInfo - Keep track of inlined functions and their location. This
194 /// information is used to populate debug_inlined section.
195 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
196 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
197 SmallVector<const MDNode *, 4> InlinedSPNodes;
199 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
200 // are processed to create DIEs.
201 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
203 /// LabelsBeforeInsn - Maps instruction with label emitted before
205 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
207 /// LabelsAfterInsn - Maps instruction with label emitted after
209 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
211 /// insnNeedsLabel - Collection of instructions that need a label to mark
212 /// a debuggging information entity.
213 SmallPtrSet<const MachineInstr *, 8> InsnNeedsLabel;
215 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
217 /// Previous instruction's location information. This is used to determine
218 /// label location to indicate scope boundries in dwarf debug info.
219 DebugLoc PrevInstLoc;
222 struct FunctionDebugFrameInfo {
224 std::vector<MachineMove> Moves;
226 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
227 : Number(Num), Moves(M) {}
230 std::vector<FunctionDebugFrameInfo> DebugFrames;
232 // Section Symbols: these are assembler temporary labels that are emitted at
233 // the beginning of each supported dwarf section. These are used to form
234 // section offsets and are created by EmitSectionLabels.
235 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
236 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
237 MCSymbol *DwarfDebugLocSectionSym;
238 MCSymbol *FunctionBeginSym, *FunctionEndSym;
240 DIEInteger *DIEIntegerOne;
243 /// getNumSourceIds - Return the number of unique source ids.
244 unsigned getNumSourceIds() const {
245 return SourceIdMap.size();
248 /// assignAbbrevNumber - Define a unique number for the abbreviation.
250 void assignAbbrevNumber(DIEAbbrev &Abbrev);
252 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
253 /// information entry.
254 DIEEntry *createDIEEntry(DIE *Entry);
256 /// addUInt - Add an unsigned integer attribute data and value.
258 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
260 /// addSInt - Add an signed integer attribute data and value.
262 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
264 /// addString - Add a string attribute data and value.
266 void addString(DIE *Die, unsigned Attribute, unsigned Form,
267 const StringRef Str);
269 /// addLabel - Add a Dwarf label attribute data and value.
271 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
272 const MCSymbol *Label);
274 /// addDelta - Add a label delta attribute data and value.
276 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
277 const MCSymbol *Hi, const MCSymbol *Lo);
279 /// addDIEEntry - Add a DIE attribute data and value.
281 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
283 /// addBlock - Add block data.
285 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
287 /// addSourceLine - Add location information to specified debug information
289 void addSourceLine(DIE *Die, DIVariable V);
290 void addSourceLine(DIE *Die, DIGlobalVariable G);
291 void addSourceLine(DIE *Die, DISubprogram SP);
292 void addSourceLine(DIE *Die, DIType Ty);
293 void addSourceLine(DIE *Die, DINameSpace NS);
295 /// addAddress - Add an address attribute to a die based on the location
297 void addAddress(DIE *Die, unsigned Attribute,
298 const MachineLocation &Location);
300 /// addRegisterAddress - Add register location entry in variable DIE.
301 bool addRegisterAddress(DIE *Die, const MachineOperand &MO);
303 /// addConstantValue - Add constant value entry in variable DIE.
304 bool addConstantValue(DIE *Die, const MachineOperand &MO);
305 bool addConstantValue(DIE *Die, ConstantInt *CI, bool Unsigned);
307 /// addConstantFPValue - Add constant value entry in variable DIE.
308 bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
310 /// addComplexAddress - Start with the address based on the location provided,
311 /// and generate the DWARF information necessary to find the actual variable
312 /// (navigating the extra location information encoded in the type) based on
313 /// the starting location. Add the DWARF information to the die.
315 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
316 const MachineLocation &Location);
318 // FIXME: Should be reformulated in terms of addComplexAddress.
319 /// addBlockByrefAddress - Start with the address based on the location
320 /// provided, and generate the DWARF information necessary to find the
321 /// actual Block variable (navigating the Block struct) based on the
322 /// starting location. Add the DWARF information to the die. Obsolete,
323 /// please use addComplexAddress instead.
325 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
326 const MachineLocation &Location);
328 /// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
329 /// on provided frame index.
330 void addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI);
332 /// addToContextOwner - Add Die into the list of its context owner's children.
333 void addToContextOwner(DIE *Die, DIDescriptor Context);
335 /// addType - Add a new type attribute to the specified entity.
336 void addType(DIE *Entity, DIType Ty);
339 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
340 DIE *getOrCreateNameSpace(DINameSpace NS);
342 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
344 DIE *getOrCreateTypeDIE(DIType Ty);
346 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
347 /// for the given DITemplateTypeParameter.
348 DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
350 void addPubTypes(DISubprogram SP);
352 /// constructTypeDIE - Construct basic type die from DIBasicType.
353 void constructTypeDIE(DIE &Buffer,
356 /// constructTypeDIE - Construct derived type die from DIDerivedType.
357 void constructTypeDIE(DIE &Buffer,
360 /// constructTypeDIE - Construct type DIE from DICompositeType.
361 void constructTypeDIE(DIE &Buffer,
362 DICompositeType CTy);
364 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
365 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
367 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
368 void constructArrayTypeDIE(DIE &Buffer,
369 DICompositeType *CTy);
371 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
372 DIE *constructEnumTypeDIE(DIEnumerator ETy);
374 /// createMemberDIE - Create new member DIE.
375 DIE *createMemberDIE(DIDerivedType DT);
377 /// createSubprogramDIE - Create new DIE using SP.
378 DIE *createSubprogramDIE(DISubprogram SP);
380 /// getOrCreateDbgScope - Create DbgScope for the scope.
381 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
383 DbgScope *getOrCreateAbstractScope(const MDNode *N);
385 /// findAbstractVariable - Find abstract variable associated with Var.
386 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
388 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
389 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
390 /// If there are global variables in this scope then create and insert
391 /// DIEs for these variables.
392 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
394 /// constructLexicalScope - Construct new DW_TAG_lexical_block
395 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
396 DIE *constructLexicalScopeDIE(DbgScope *Scope);
398 /// constructInlinedScopeDIE - This scope represents inlined body of
399 /// a function. Construct DIE to represent this concrete inlined copy
401 DIE *constructInlinedScopeDIE(DbgScope *Scope);
403 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
404 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
406 /// constructScopeDIE - Construct a DIE for this scope.
407 DIE *constructScopeDIE(DbgScope *Scope);
409 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
410 /// the start of each one.
411 void EmitSectionLabels();
413 /// emitDIE - Recusively Emits a debug information entry.
415 void emitDIE(DIE *Die);
417 /// computeSizeAndOffset - Compute the size and offset of a DIE.
419 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
421 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
423 void computeSizeAndOffsets();
425 /// EmitDebugInfo - Emit the debug info section.
427 void emitDebugInfo();
429 /// emitAbbreviations - Emit the abbreviation section.
431 void emitAbbreviations() const;
433 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
436 void emitEndOfLineMatrix(unsigned SectionEnd);
438 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
440 void emitCommonDebugFrame();
442 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
444 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
446 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
448 void emitDebugPubNames();
450 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
452 void emitDebugPubTypes();
454 /// emitDebugStr - Emit visible names into a debug str section.
458 /// emitDebugLoc - Emit visible names into a debug loc section.
462 /// EmitDebugARanges - Emit visible names into a debug aranges section.
464 void EmitDebugARanges();
466 /// emitDebugRanges - Emit visible names into a debug ranges section.
468 void emitDebugRanges();
470 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
472 void emitDebugMacInfo();
474 /// emitDebugInlineInfo - Emit inline info using following format.
476 /// 1. length of section
477 /// 2. Dwarf version number
480 /// Entries (one "entry" for each function that was inlined):
482 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
483 /// otherwise offset into __debug_str for regular function name.
484 /// 2. offset into __debug_str section for regular function name.
485 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
486 /// instances for the function.
488 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
489 /// inlined instance; the die_offset points to the inlined_subroutine die in
490 /// the __debug_info section, and the low_pc is the starting address for the
491 /// inlining instance.
492 void emitDebugInlineInfo();
494 /// GetOrCreateSourceID - Look up the source id with the given directory and
495 /// source file names. If none currently exists, create a new id and insert it
496 /// in the SourceIds map.
497 unsigned GetOrCreateSourceID(StringRef FullName);
499 /// constructCompileUnit - Create new CompileUnit for the given
500 /// metadata node with tag DW_TAG_compile_unit.
501 void constructCompileUnit(const MDNode *N);
503 /// getCompielUnit - Get CompileUnit DIE.
504 CompileUnit *getCompileUnit(const MDNode *N) const;
506 /// constructGlobalVariableDIE - Construct global variable DIE.
507 void constructGlobalVariableDIE(const MDNode *N);
509 /// construct SubprogramDIE - Construct subprogram DIE.
510 void constructSubprogramDIE(const MDNode *N);
512 /// recordSourceLine - Register a source line with debug info. Returns the
513 /// unique label that was emitted and which provides correspondence to
514 /// the source line list.
515 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
517 /// recordVariableFrameIndex - Record a variable's index.
518 void recordVariableFrameIndex(const DbgVariable *V, int Index);
520 /// findVariableFrameIndex - Return true if frame index for the variable
521 /// is found. Update FI to hold value of the index.
522 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
524 /// findDbgScope - Find DbgScope for the debug loc attached with an
526 DbgScope *findDbgScope(const MachineInstr *MI);
528 /// identifyScopeMarkers() - Indentify instructions that are marking
529 /// beginning of or end of a scope.
530 void identifyScopeMarkers();
532 /// extractScopeInformation - Scan machine instructions in this function
533 /// and collect DbgScopes. Return true, if atleast one scope was found.
534 bool extractScopeInformation();
536 /// collectVariableInfo - Populate DbgScope entries with variables' info.
537 void collectVariableInfo(const MachineFunction *,
538 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
540 /// collectVariableInfoFromMMITable - Collect variable information from
541 /// side table maintained by MMI.
542 void collectVariableInfoFromMMITable(const MachineFunction * MF,
543 SmallPtrSet<const MDNode *, 16> &P);
545 //===--------------------------------------------------------------------===//
546 // Main entry points.
548 DwarfDebug(AsmPrinter *A, Module *M);
551 /// beginModule - Emit all Dwarf sections that should come prior to the
553 void beginModule(Module *M);
555 /// endModule - Emit all Dwarf sections that should come after the content.
559 /// beginFunction - Gather pre-function debug information. Assumes being
560 /// emitted immediately after the function entry point.
561 void beginFunction(const MachineFunction *MF);
563 /// endFunction - Gather and emit post-function debug information.
565 void endFunction(const MachineFunction *MF);
567 /// getLabelBeforeInsn - Return Label preceding the instruction.
568 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
570 /// getLabelAfterInsn - Return Label immediately following the instruction.
571 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
573 /// beginInstruction - Process beginning of an instruction.
574 void beginInstruction(const MachineInstr *MI);
576 /// endInstruction - Prcess end of an instruction.
577 void endInstruction(const MachineInstr *MI);
579 } // End of namespace llvm