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__
18 #include "llvm/DebugInfo.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/LexicalScopes.h"
21 #include "llvm/MC/MachineLocation.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/DebugLoc.h"
36 class MachineFrameInfo;
37 class MachineModuleInfo;
45 //===----------------------------------------------------------------------===//
46 /// SrcLineInfo - This class is used to record source line correspondence.
49 unsigned Line; // Source line number.
50 unsigned Column; // Source column.
51 unsigned SourceID; // Source ID number.
52 MCSymbol *Label; // Label in code ID number.
54 SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
55 : Line(L), Column(C), SourceID(S), Label(label) {}
58 unsigned getLine() const { return Line; }
59 unsigned getColumn() const { return Column; }
60 unsigned getSourceID() const { return SourceID; }
61 MCSymbol *getLabel() const { return Label; }
64 /// DotDebugLocEntry - This struct describes location entries emitted in
65 /// .debug_loc section.
66 typedef struct DotDebugLocEntry {
67 const MCSymbol *Begin;
70 const MDNode *Variable;
79 enum EntryType EntryKind;
83 const ConstantFP *CFP;
84 const ConstantInt *CIP;
87 : Begin(0), End(0), Variable(0), Merged(false),
88 Constant(false) { Constants.Int = 0;}
89 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, MachineLocation &L,
91 : Begin(B), End(E), Loc(L), Variable(V), Merged(false),
92 Constant(false) { Constants.Int = 0; EntryKind = E_Location; }
93 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, int64_t i)
94 : Begin(B), End(E), Variable(0), Merged(false),
95 Constant(true) { Constants.Int = i; EntryKind = E_Integer; }
96 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E, const ConstantFP *FPtr)
97 : Begin(B), End(E), Variable(0), Merged(false),
98 Constant(true) { Constants.CFP = FPtr; EntryKind = E_ConstantFP; }
99 DotDebugLocEntry(const MCSymbol *B, const MCSymbol *E,
100 const ConstantInt *IPtr)
101 : Begin(B), End(E), Variable(0), Merged(false),
102 Constant(true) { Constants.CIP = IPtr; EntryKind = E_ConstantInt; }
104 /// Empty entries are also used as a trigger to emit temp label. Such
105 /// labels are referenced is used to find debug_loc offset for a given DIE.
106 bool isEmpty() { return Begin == 0 && End == 0; }
107 bool isMerged() { return Merged; }
108 void Merge(DotDebugLocEntry *Next) {
109 if (!(Begin && Loc == Next->Loc && End == Next->Begin))
114 bool isLocation() const { return EntryKind == E_Location; }
115 bool isInt() const { return EntryKind == E_Integer; }
116 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
117 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
118 int64_t getInt() { return Constants.Int; }
119 const ConstantFP *getConstantFP() { return Constants.CFP; }
120 const ConstantInt *getConstantInt() { return Constants.CIP; }
123 //===----------------------------------------------------------------------===//
124 /// DbgVariable - This class is used to track local variable information.
127 DIVariable Var; // Variable Descriptor.
128 DIE *TheDIE; // Variable DIE.
129 unsigned DotDebugLocOffset; // Offset in DotDebugLocEntries.
130 DbgVariable *AbsVar; // Corresponding Abstract variable, if any.
131 const MachineInstr *MInsn; // DBG_VALUE instruction of the variable.
134 // AbsVar may be NULL.
135 DbgVariable(DIVariable V, DbgVariable *AV)
136 : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
140 DIVariable getVariable() const { return Var; }
141 void setDIE(DIE *D) { TheDIE = D; }
142 DIE *getDIE() const { return TheDIE; }
143 void setDotDebugLocOffset(unsigned O) { DotDebugLocOffset = O; }
144 unsigned getDotDebugLocOffset() const { return DotDebugLocOffset; }
145 StringRef getName() const { return Var.getName(); }
146 DbgVariable *getAbstractVariable() const { return AbsVar; }
147 const MachineInstr *getMInsn() const { return MInsn; }
148 void setMInsn(const MachineInstr *M) { MInsn = M; }
149 int getFrameIndex() const { return FrameIndex; }
150 void setFrameIndex(int FI) { FrameIndex = FI; }
151 // Translate tag to proper Dwarf tag.
152 unsigned getTag() const {
153 if (Var.getTag() == dwarf::DW_TAG_arg_variable)
154 return dwarf::DW_TAG_formal_parameter;
156 return dwarf::DW_TAG_variable;
158 /// isArtificial - Return true if DbgVariable is artificial.
159 bool isArtificial() const {
160 if (Var.isArtificial())
162 if (getType().isArtificial())
167 bool isObjectPointer() const {
168 if (Var.isObjectPointer())
170 if (getType().isObjectPointer())
175 bool variableHasComplexAddress() const {
176 assert(Var.Verify() && "Invalid complex DbgVariable!");
177 return Var.hasComplexAddress();
179 bool isBlockByrefVariable() const {
180 assert(Var.Verify() && "Invalid complex DbgVariable!");
181 return Var.isBlockByrefVariable();
183 unsigned getNumAddrElements() const {
184 assert(Var.Verify() && "Invalid complex DbgVariable!");
185 return Var.getNumAddrElements();
187 uint64_t getAddrElement(unsigned i) const {
188 return Var.getAddrElement(i);
190 DIType getType() const;
194 /// Asm - Target of Dwarf emission.
197 /// MMI - Collected machine module information.
198 MachineModuleInfo *MMI;
200 /// DIEValueAllocator - All DIEValues are allocated through this allocator.
201 BumpPtrAllocator DIEValueAllocator;
203 //===--------------------------------------------------------------------===//
204 // Attributes used to construct specific Dwarf sections.
207 CompileUnit *FirstCU;
209 /// Maps MDNode with its corresponding CompileUnit.
210 DenseMap <const MDNode *, CompileUnit *> CUMap;
212 /// Maps subprogram MDNode with its corresponding CompileUnit.
213 DenseMap <const MDNode *, CompileUnit *> SPMap;
215 /// AbbreviationsSet - Used to uniquely define abbreviations.
217 FoldingSet<DIEAbbrev> AbbreviationsSet;
219 /// Abbreviations - A list of all the unique abbreviations in use.
221 std::vector<DIEAbbrev *> Abbreviations;
223 /// SourceIdMap - Source id map, i.e. pair of source filename and directory,
224 /// separated by a zero byte, mapped to a unique id.
225 StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
227 /// StringPool - A String->Symbol mapping of strings used by indirect
229 StringMap<std::pair<MCSymbol*, unsigned>, BumpPtrAllocator&> StringPool;
230 unsigned NextStringPoolNumber;
232 /// SectionMap - Provides a unique id per text section.
234 SetVector<const MCSection*> SectionMap;
236 /// CurrentFnArguments - List of Arguments (DbgValues) for current function.
237 SmallVector<DbgVariable *, 8> CurrentFnArguments;
239 LexicalScopes LScopes;
241 /// AbstractSPDies - Collection of abstract subprogram DIEs.
242 DenseMap<const MDNode *, DIE *> AbstractSPDies;
244 /// ScopeVariables - Collection of dbg variables of a scope.
245 DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> > ScopeVariables;
247 /// AbstractVariables - Collection of abstract variables.
248 DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
250 /// DotDebugLocEntries - Collection of DotDebugLocEntry.
251 SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
253 /// InlinedSubprogramDIEs - Collection of subprogram DIEs that are marked
254 /// (at the end of the module) as DW_AT_inline.
255 SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
257 /// InlineInfo - Keep track of inlined functions and their location. This
258 /// information is used to populate the debug_inlined section.
259 typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
260 DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
261 SmallVector<const MDNode *, 4> InlinedSPNodes;
263 // ProcessedSPNodes - This is a collection of subprogram MDNodes that
264 // are processed to create DIEs.
265 SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
267 /// LabelsBeforeInsn - Maps instruction with label emitted before
269 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
271 /// LabelsAfterInsn - Maps instruction with label emitted after
273 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
275 /// UserVariables - Every user variable mentioned by a DBG_VALUE instruction
276 /// in order of appearance.
277 SmallVector<const MDNode*, 8> UserVariables;
279 /// DbgValues - For each user variable, keep a list of DBG_VALUE
280 /// instructions in order. The list can also contain normal instructions that
281 /// clobber the previous DBG_VALUE.
282 typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
284 DbgValueHistoryMap DbgValues;
286 SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
288 /// Previous instruction's location information. This is used to determine
289 /// label location to indicate scope boundries in dwarf debug info.
290 DebugLoc PrevInstLoc;
293 /// PrologEndLoc - This location indicates end of function prologue and
294 /// beginning of function body.
295 DebugLoc PrologEndLoc;
297 struct FunctionDebugFrameInfo {
299 std::vector<MachineMove> Moves;
301 FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
302 : Number(Num), Moves(M) {}
305 std::vector<FunctionDebugFrameInfo> DebugFrames;
307 // Section Symbols: these are assembler temporary labels that are emitted at
308 // the beginning of each supported dwarf section. These are used to form
309 // section offsets and are created by EmitSectionLabels.
310 MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
311 MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
312 MCSymbol *DwarfDebugLocSectionSym;
313 MCSymbol *FunctionBeginSym, *FunctionEndSym;
315 // As an optimization, there is no need to emit an entry in the directory
316 // table for the same directory as DW_at_comp_dir.
317 StringRef CompilationDir;
319 // A holder for the DarwinGDBCompat flag so that the compile unit can use it.
320 bool isDarwinGDBCompat;
321 bool hasDwarfAccelTables;
322 bool hasDwarfFission;
325 /// assignAbbrevNumber - Define a unique number for the abbreviation.
327 void assignAbbrevNumber(DIEAbbrev &Abbrev);
329 void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
331 /// findAbstractVariable - Find abstract variable associated with Var.
332 DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
334 /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
335 /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
336 /// If there are global variables in this scope then create and insert
337 /// DIEs for these variables.
338 DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
340 /// constructLexicalScope - Construct new DW_TAG_lexical_block
341 /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
342 DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
344 /// constructInlinedScopeDIE - This scope represents inlined body of
345 /// a function. Construct DIE to represent this concrete inlined copy
347 DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
349 /// constructScopeDIE - Construct a DIE for this scope.
350 DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
352 /// EmitSectionLabels - Emit initial Dwarf sections with a label at
353 /// the start of each one.
354 void EmitSectionLabels();
356 /// emitDIE - Recursively Emits a debug information entry.
358 void emitDIE(DIE *Die);
360 /// computeSizeAndOffset - Compute the size and offset of a DIE.
362 unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
364 /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
366 void computeSizeAndOffsets();
368 /// EmitDebugInfo - Emit the debug info section.
370 void emitDebugInfo();
372 /// emitAbbreviations - Emit the abbreviation section.
374 void emitAbbreviations() const;
376 /// emitEndOfLineMatrix - Emit the last address of the section and the end of
379 void emitEndOfLineMatrix(unsigned SectionEnd);
381 /// emitAccelNames - Emit visible names into a hashed accelerator table
383 void emitAccelNames();
385 /// emitAccelObjC - Emit objective C classes and categories into a hashed
386 /// accelerator table section.
387 void emitAccelObjC();
389 /// emitAccelNamespace - Emit namespace dies into a hashed accelerator
391 void emitAccelNamespaces();
393 /// emitAccelTypes() - Emit type dies into a hashed accelerator table.
395 void emitAccelTypes();
397 /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
399 void emitDebugPubTypes();
401 /// emitDebugStr - Emit visible names into a debug str section.
405 /// emitDebugLoc - Emit visible names into a debug loc section.
409 /// EmitDebugARanges - Emit visible names into a debug aranges section.
411 void EmitDebugARanges();
413 /// emitDebugRanges - Emit visible names into a debug ranges section.
415 void emitDebugRanges();
417 /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
419 void emitDebugMacInfo();
421 /// emitDebugInlineInfo - Emit inline info using following format.
423 /// 1. length of section
424 /// 2. Dwarf version number
427 /// Entries (one "entry" for each function that was inlined):
429 /// 1. offset into __debug_str section for MIPS linkage name, if exists;
430 /// otherwise offset into __debug_str for regular function name.
431 /// 2. offset into __debug_str section for regular function name.
432 /// 3. an unsigned LEB128 number indicating the number of distinct inlining
433 /// instances for the function.
435 /// The rest of the entry consists of a {die_offset, low_pc} pair for each
436 /// inlined instance; the die_offset points to the inlined_subroutine die in
437 /// the __debug_info section, and the low_pc is the starting address for the
438 /// inlining instance.
439 void emitDebugInlineInfo();
441 /// constructCompileUnit - Create new CompileUnit for the given
442 /// metadata node with tag DW_TAG_compile_unit.
443 CompileUnit *constructCompileUnit(const MDNode *N);
445 /// construct SubprogramDIE - Construct subprogram DIE.
446 void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
448 /// recordSourceLine - Register a source line with debug info. Returns the
449 /// unique label that was emitted and which provides correspondence to
450 /// the source line list.
451 void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
454 /// identifyScopeMarkers() - Indentify instructions that are marking the
455 /// beginning of or ending of a scope.
456 void identifyScopeMarkers();
458 /// addCurrentFnArgument - If Var is an current function argument that add
459 /// it in CurrentFnArguments list.
460 bool addCurrentFnArgument(const MachineFunction *MF,
461 DbgVariable *Var, LexicalScope *Scope);
463 /// collectVariableInfo - Populate LexicalScope entries with variables' info.
464 void collectVariableInfo(const MachineFunction *,
465 SmallPtrSet<const MDNode *, 16> &ProcessedVars);
467 /// collectVariableInfoFromMMITable - Collect variable information from
468 /// side table maintained by MMI.
469 void collectVariableInfoFromMMITable(const MachineFunction * MF,
470 SmallPtrSet<const MDNode *, 16> &P);
472 /// requestLabelBeforeInsn - Ensure that a label will be emitted before MI.
473 void requestLabelBeforeInsn(const MachineInstr *MI) {
474 LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
477 /// getLabelBeforeInsn - Return Label preceding the instruction.
478 const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
480 /// requestLabelAfterInsn - Ensure that a label will be emitted after MI.
481 void requestLabelAfterInsn(const MachineInstr *MI) {
482 LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
485 /// getLabelAfterInsn - Return Label immediately following the instruction.
486 const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
489 //===--------------------------------------------------------------------===//
490 // Main entry points.
492 DwarfDebug(AsmPrinter *A, Module *M);
495 /// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
496 /// as llvm.dbg.enum and llvm.dbg.ty
497 void collectInfoFromNamedMDNodes(const Module *M);
499 /// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
500 /// FIXME - Remove this when DragonEgg switches to DIBuilder.
501 bool collectLegacyDebugInfo(const Module *M);
503 /// beginModule - Emit all Dwarf sections that should come prior to the
507 /// endModule - Emit all Dwarf sections that should come after the content.
511 /// beginFunction - Gather pre-function debug information. Assumes being
512 /// emitted immediately after the function entry point.
513 void beginFunction(const MachineFunction *MF);
515 /// endFunction - Gather and emit post-function debug information.
517 void endFunction(const MachineFunction *MF);
519 /// beginInstruction - Process beginning of an instruction.
520 void beginInstruction(const MachineInstr *MI);
522 /// endInstruction - Prcess end of an instruction.
523 void endInstruction(const MachineInstr *MI);
525 /// GetOrCreateSourceID - Look up the source id with the given directory and
526 /// source file names. If none currently exists, create a new id and insert it
527 /// in the SourceIds map.
528 unsigned GetOrCreateSourceID(StringRef DirName, StringRef FullName);
530 /// getStringPool - returns the entry into the start of the pool.
531 MCSymbol *getStringPool();
533 /// getStringPoolEntry - returns an entry into the string pool with the given
535 MCSymbol *getStringPoolEntry(StringRef Str);
537 /// useDarwinGDBCompat - returns whether or not to limit some of our debug
538 /// output to the limitations of darwin gdb.
539 bool useDarwinGDBCompat() { return isDarwinGDBCompat; }
540 bool useDwarfAccelTables() { return hasDwarfAccelTables; }
541 bool useDwarfFission() { return hasDwarfFission; }
543 } // End of namespace llvm