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; }
77 /// DotDebugLocEntry - This struct describes location entries emitted in
78 /// .debug_loc section.
79 typedef struct DotDebugLocEntry {
80 const MCSymbol *Begin;
84 DotDebugLocEntry() : Begin(0), End(0), Merged(false) {}
85 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L)
86 : Begin(B), End(E), Loc(L), Merged(false) {}
87 /// Empty entries are also used as a trigger to emit temp label. Such
88 /// labels are referenced is used to find debug_loc offset for a given DIE.
89 bool isEmpty() { return Begin == 0 && End == 0; }
90 bool isMerged() { return Merged; }
91 void Merge(DotDebugLocEntry *Next) {
92 if (!(Begin && Loc == Next->Loc && End == Next->Begin))
100 /// Asm - Target of Dwarf emission.
103 /// MMI - Collected machine module information.
104 MachineModuleInfo *MMI;
106 //===--------------------------------------------------------------------===//
107 // Attributes used to construct specific Dwarf sections.
110 CompileUnit *FirstCU;
111 DenseMap <const MDNode *, CompileUnit *> CUMap;
113 /// AbbreviationsSet - Used to uniquely define abbreviations.
115 FoldingSet<DIEAbbrev> AbbreviationsSet;
117 /// Abbreviations - A list of all the unique abbreviations in use.
119 std::vector<DIEAbbrev *> Abbreviations;
121 /// SourceIdMap - Source id map, i.e. pair of directory id and source file
122 /// id mapped to a unique id.
123 StringMap<unsigned> SourceIdMap;
125 /// DIEBlocks - A list of all the DIEBlocks in use.
126 std::vector<DIEBlock *> DIEBlocks;
128 // DIEValueAllocator - All DIEValues are allocated through this allocator.
129 BumpPtrAllocator DIEValueAllocator;
131 /// StringPool - A String->Symbol mapping of strings used by indirect
133 StringMap<std::pair<MCSymbol*, unsigned> > StringPool;
134 unsigned NextStringPoolNumber;
136 MCSymbol *getStringPoolEntry(StringRef Str);
138 /// SectionMap - Provides a unique id per text section.
140 UniqueVector<const MCSection*> SectionMap;
142 // CurrentFnDbgScope - Top level scope for the current function.
144 DbgScope *CurrentFnDbgScope;
146 /// DbgScopeMap - Tracks the scopes in the current function. Owns the
147 /// contained DbgScope*s.
149 DenseMap<const MDNode *, DbgScope *> DbgScopeMap;
151 /// ConcreteScopes - Tracks the concrete scopees in the current function.
152 /// These scopes are also included in DbgScopeMap.
153 DenseMap<const MDNode *, DbgScope *> ConcreteScopes;
155 /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
156 /// not included DbgScopeMap. AbstractScopes owns its DbgScope*s.
157 DenseMap<const MDNode *, DbgScope *> AbstractScopes;
159 /// AbstractSPDies - Collection of abstract subprogram DIEs.
160 DenseMap<const MDNode *, DIE *> AbstractSPDies;
162 /// AbstractScopesList - Tracks abstract scopes constructed while processing
163 /// a function. This list is cleared during endFunction().
164 SmallVector<DbgScope *, 4>AbstractScopesList;
166 /// AbstractVariables - Collection on abstract variables. Owned by the
167 /// DbgScopes in AbstractScopes.
168 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
170 /// DbgVariableToFrameIndexMap - Tracks frame index used to find
171 /// variable's value.
172 DenseMap<const DbgVariable *, int> DbgVariableToFrameIndexMap;
174 /// DbgVariableToDbgInstMap - Maps DbgVariable to corresponding DBG_VALUE
175 /// machine instruction.
176 DenseMap<const DbgVariable *, const MachineInstr *> DbgVariableToDbgInstMap;
178 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
179 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
181 /// UseDotDebugLocEntry - DW_AT_location attributes for the DIEs in this set
182 /// idetifies corresponding .debug_loc entry offset.
183 SmallPtrSet<const DIE *, 4> UseDotDebugLocEntry;
185 /// VarToAbstractVarMap - Maps DbgVariable with corresponding Abstract
186 /// DbgVariable, if any.
187 DenseMap<const DbgVariable *, const DbgVariable *> VarToAbstractVarMap;
189 /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
190 /// (at the end of the module) as DW_AT_inline.
191 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
193 /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that
194 /// need DW_AT_containing_type attribute. This attribute points to a DIE that
195 /// corresponds to the MDNode mapped with the subprogram DIE.
196 DenseMap<DIE *, const MDNode *> ContainingTypeMap;
198 typedef SmallVector<DbgScope *, 2> ScopeVector;
200 SmallPtrSet<const MachineInstr *, 8> InsnsEndScopeSet;
202 /// InlineInfo - Keep track of inlined functions and their location. This
203 /// information is used to populate debug_inlined section.
204 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
205 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
206 SmallVector<const MDNode *, 4> InlinedSPNodes;
208 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
209 // are processed to create DIEs.
210 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
212 /// LabelsBeforeInsn - Maps instruction with label emitted before
214 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
216 /// LabelsAfterInsn - Maps instruction with label emitted after
218 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
220 /// insnNeedsLabel - Collection of instructions that need a label to mark
221 /// a debuggging information entity.
222 SmallPtrSet<const MachineInstr *, 8> InsnNeedsLabel;
224 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
226 /// Previous instruction's location information. This is used to determine
227 /// label location to indicate scope boundries in dwarf debug info.
228 DebugLoc PrevInstLoc;
231 struct FunctionDebugFrameInfo {
233 std::vector<MachineMove> Moves;
235 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
236 : Number(Num), Moves(M) {}
239 std::vector<FunctionDebugFrameInfo> DebugFrames;
241 // Section Symbols: these are assembler temporary labels that are emitted at
242 // the beginning of each supported dwarf section. These are used to form
243 // section offsets and are created by EmitSectionLabels.
244 MCSymbol *DwarfFrameSectionSym, *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
245 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
246 MCSymbol *DwarfDebugLocSectionSym;
247 MCSymbol *FunctionBeginSym, *FunctionEndSym;
249 DIEInteger *DIEIntegerOne;
252 /// getNumSourceIds - Return the number of unique source ids.
253 unsigned getNumSourceIds() const {
254 return SourceIdMap.size();
257 /// assignAbbrevNumber - Define a unique number for the abbreviation.
259 void assignAbbrevNumber(DIEAbbrev &Abbrev);
261 /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
262 /// information entry.
263 DIEEntry *createDIEEntry(DIE *Entry);
265 /// addUInt - Add an unsigned integer attribute data and value.
267 void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
269 /// addSInt - Add an signed integer attribute data and value.
271 void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
273 /// addString - Add a string attribute data and value.
275 void addString(DIE *Die, unsigned Attribute, unsigned Form,
276 const StringRef Str);
278 /// addLabel - Add a Dwarf label attribute data and value.
280 void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
281 const MCSymbol *Label);
283 /// addDelta - Add a label delta attribute data and value.
285 void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
286 const MCSymbol *Hi, const MCSymbol *Lo);
288 /// addDIEEntry - Add a DIE attribute data and value.
290 void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry);
292 /// addBlock - Add block data.
294 void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
296 /// addSourceLine - Add location information to specified debug information
298 void addSourceLine(DIE *Die, DIVariable V);
299 void addSourceLine(DIE *Die, DIGlobalVariable G);
300 void addSourceLine(DIE *Die, DISubprogram SP);
301 void addSourceLine(DIE *Die, DIType Ty);
302 void addSourceLine(DIE *Die, DINameSpace NS);
304 /// addAddress - Add an address attribute to a die based on the location
306 void addAddress(DIE *Die, unsigned Attribute,
307 const MachineLocation &Location);
309 /// addRegisterAddress - Add register location entry in variable DIE.
310 bool addRegisterAddress(DIE *Die, const MachineOperand &MO);
312 /// addConstantValue - Add constant value entry in variable DIE.
313 bool addConstantValue(DIE *Die, const MachineOperand &MO);
314 bool addConstantValue(DIE *Die, ConstantInt *CI, bool Unsigned);
316 /// addConstantFPValue - Add constant value entry in variable DIE.
317 bool addConstantFPValue(DIE *Die, const MachineOperand &MO);
319 /// addComplexAddress - Start with the address based on the location provided,
320 /// and generate the DWARF information necessary to find the actual variable
321 /// (navigating the extra location information encoded in the type) based on
322 /// the starting location. Add the DWARF information to the die.
324 void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
325 const MachineLocation &Location);
327 // FIXME: Should be reformulated in terms of addComplexAddress.
328 /// addBlockByrefAddress - Start with the address based on the location
329 /// provided, and generate the DWARF information necessary to find the
330 /// actual Block variable (navigating the Block struct) based on the
331 /// starting location. Add the DWARF information to the die. Obsolete,
332 /// please use addComplexAddress instead.
334 void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
335 const MachineLocation &Location);
337 /// addVariableAddress - Add DW_AT_location attribute for a DbgVariable based
338 /// on provided frame index.
339 void addVariableAddress(DbgVariable *&DV, DIE *Die, int64_t FI);
341 /// addToContextOwner - Add Die into the list of its context owner's children.
342 void addToContextOwner(DIE *Die, DIDescriptor Context);
344 /// addType - Add a new type attribute to the specified entity.
345 void addType(DIE *Entity, DIType Ty);
348 /// getOrCreateNameSpace - Create a DIE for DINameSpace.
349 DIE *getOrCreateNameSpace(DINameSpace NS);
351 /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the
353 DIE *getOrCreateTypeDIE(DIType Ty);
355 /// getOrCreateTemplateTypeParameterDIE - Find existing DIE or create new DIE
356 /// for the given DITemplateTypeParameter.
357 DIE *getOrCreateTemplateTypeParameterDIE(DITemplateTypeParameter TP);
359 /// getOrCreateTemplateValueParameterDIE - Find existing DIE or create new DIE
360 /// for the given DITemplateValueParameter.
361 DIE *getOrCreateTemplateValueParameterDIE(DITemplateValueParameter TVP);
363 void addPubTypes(DISubprogram SP);
365 /// constructTypeDIE - Construct basic type die from DIBasicType.
366 void constructTypeDIE(DIE &Buffer,
369 /// constructTypeDIE - Construct derived type die from DIDerivedType.
370 void constructTypeDIE(DIE &Buffer,
373 /// constructTypeDIE - Construct type DIE from DICompositeType.
374 void constructTypeDIE(DIE &Buffer,
375 DICompositeType CTy);
377 /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
378 void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
380 /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
381 void constructArrayTypeDIE(DIE &Buffer,
382 DICompositeType *CTy);
384 /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
385 DIE *constructEnumTypeDIE(DIEnumerator ETy);
387 /// createMemberDIE - Create new member DIE.
388 DIE *createMemberDIE(DIDerivedType DT);
390 /// createSubprogramDIE - Create new DIE using SP.
391 DIE *createSubprogramDIE(DISubprogram SP);
393 /// getOrCreateDbgScope - Create DbgScope for the scope.
394 DbgScope *getOrCreateDbgScope(const MDNode *Scope, const MDNode *InlinedAt);
396 DbgScope *getOrCreateAbstractScope(const MDNode *N);
398 /// findAbstractVariable - Find abstract variable associated with Var.
399 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
401 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
402 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
403 /// If there are global variables in this scope then create and insert
404 /// DIEs for these variables.
405 DIE *updateSubprogramScopeDIE(const MDNode *SPNode);
407 /// constructLexicalScope - Construct new DW_TAG_lexical_block
408 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
409 DIE *constructLexicalScopeDIE(DbgScope *Scope);
411 /// constructInlinedScopeDIE - This scope represents inlined body of
412 /// a function. Construct DIE to represent this concrete inlined copy
414 DIE *constructInlinedScopeDIE(DbgScope *Scope);
416 /// constructVariableDIE - Construct a DIE for the given DbgVariable.
417 DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S);
419 /// constructScopeDIE - Construct a DIE for this scope.
420 DIE *constructScopeDIE(DbgScope *Scope);
422 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
423 /// the start of each one.
424 void EmitSectionLabels();
426 /// emitDIE - Recusively Emits a debug information entry.
428 void emitDIE(DIE *Die);
430 /// computeSizeAndOffset - Compute the size and offset of a DIE.
432 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
434 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
436 void computeSizeAndOffsets();
438 /// EmitDebugInfo - Emit the debug info section.
440 void emitDebugInfo();
442 /// emitAbbreviations - Emit the abbreviation section.
444 void emitAbbreviations() const;
446 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
449 void emitEndOfLineMatrix(unsigned SectionEnd);
451 /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
453 void emitCommonDebugFrame();
455 /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
457 void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
459 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
461 void emitDebugPubNames();
463 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
465 void emitDebugPubTypes();
467 /// emitDebugStr - Emit visible names into a debug str section.
471 /// emitDebugLoc - Emit visible names into a debug loc section.
475 /// EmitDebugARanges - Emit visible names into a debug aranges section.
477 void EmitDebugARanges();
479 /// emitDebugRanges - Emit visible names into a debug ranges section.
481 void emitDebugRanges();
483 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
485 void emitDebugMacInfo();
487 /// emitDebugInlineInfo - Emit inline info using following format.
489 /// 1. length of section
490 /// 2. Dwarf version number
493 /// Entries (one "entry" for each function that was inlined):
495 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
496 /// otherwise offset into __debug_str for regular function name.
497 /// 2. offset into __debug_str section for regular function name.
498 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
499 /// instances for the function.
501 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
502 /// inlined instance; the die_offset points to the inlined_subroutine die in
503 /// the __debug_info section, and the low_pc is the starting address for the
504 /// inlining instance.
505 void emitDebugInlineInfo();
507 /// GetOrCreateSourceID - Look up the source id with the given directory and
508 /// source file names. If none currently exists, create a new id and insert it
509 /// in the SourceIds map.
510 unsigned GetOrCreateSourceID(StringRef FullName);
512 /// constructCompileUnit - Create new CompileUnit for the given
513 /// metadata node with tag DW_TAG_compile_unit.
514 void constructCompileUnit(const MDNode *N);
516 /// getCompielUnit - Get CompileUnit DIE.
517 CompileUnit *getCompileUnit(const MDNode *N) const;
519 /// constructGlobalVariableDIE - Construct global variable DIE.
520 void constructGlobalVariableDIE(const MDNode *N);
522 /// construct SubprogramDIE - Construct subprogram DIE.
523 void constructSubprogramDIE(const MDNode *N);
525 /// recordSourceLine - Register a source line with debug info. Returns the
526 /// unique label that was emitted and which provides correspondence to
527 /// the source line list.
528 MCSymbol *recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope);
530 /// recordVariableFrameIndex - Record a variable's index.
531 void recordVariableFrameIndex(const DbgVariable *V, int Index);
533 /// findVariableFrameIndex - Return true if frame index for the variable
534 /// is found. Update FI to hold value of the index.
535 bool findVariableFrameIndex(const DbgVariable *V, int *FI);
537 /// findDbgScope - Find DbgScope for the debug loc attached with an
539 DbgScope *findDbgScope(const MachineInstr *MI);
541 /// identifyScopeMarkers() - Indentify instructions that are marking
542 /// beginning of or end of a scope.
543 void identifyScopeMarkers();
545 /// extractScopeInformation - Scan machine instructions in this function
546 /// and collect DbgScopes. Return true, if atleast one scope was found.
547 bool extractScopeInformation();
549 /// collectVariableInfo - Populate DbgScope entries with variables' info.
550 void collectVariableInfo(const MachineFunction *,
551 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
553 /// collectVariableInfoFromMMITable - Collect variable information from
554 /// side table maintained by MMI.
555 void collectVariableInfoFromMMITable(const MachineFunction * MF,
556 SmallPtrSet<const MDNode *, 16> &P);
558 //===--------------------------------------------------------------------===//
559 // Main entry points.
561 DwarfDebug(AsmPrinter *A, Module *M);
564 /// beginModule - Emit all Dwarf sections that should come prior to the
566 void beginModule(Module *M);
568 /// endModule - Emit all Dwarf sections that should come after the content.
572 /// beginFunction - Gather pre-function debug information. Assumes being
573 /// emitted immediately after the function entry point.
574 void beginFunction(const MachineFunction *MF);
576 /// endFunction - Gather and emit post-function debug information.
578 void endFunction(const MachineFunction *MF);
580 /// getLabelBeforeInsn - Return Label preceding the instruction.
581 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
583 /// getLabelAfterInsn - Return Label immediately following the instruction.
584 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
586 /// beginInstruction - Process beginning of an instruction.
587 void beginInstruction(const MachineInstr *MI);
589 /// endInstruction - Prcess end of an instruction.
590 void endInstruction(const MachineInstr *MI);
592 } // End of namespace llvm