DwarfDebug.h revision e2ec14090999a45583ed0f6e6f1a1effc510f4f3
1//===-- llvm/CodeGen/DwarfDebug.h - Dwarf Debug Framework ------*- C++ -*--===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CODEGEN_ASMPRINTER_DWARFDEBUG_H__
15#define CODEGEN_ASMPRINTER_DWARFDEBUG_H__
16
17#include "DIE.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/SmallPtrSet.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/UniqueVector.h"
27#include "llvm/Support/Allocator.h"
28#include "llvm/Support/DebugLoc.h"
29
30namespace llvm {
31
32class CompileUnit;
33class ConstantInt;
34class ConstantFP;
35class DbgVariable;
36class MachineFrameInfo;
37class MachineModuleInfo;
38class MachineOperand;
39class MCAsmInfo;
40class DIEAbbrev;
41class DIE;
42class DIEBlock;
43class DIEEntry;
44
45//===----------------------------------------------------------------------===//
46/// SrcLineInfo - This class is used to record source line correspondence.
47///
48class SrcLineInfo {
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.
53public:
54  SrcLineInfo(unsigned L, unsigned C, unsigned S, MCSymbol *label)
55    : Line(L), Column(C), SourceID(S), Label(label) {}
56
57  // Accessors
58  unsigned getLine() const { return Line; }
59  unsigned getColumn() const { return Column; }
60  unsigned getSourceID() const { return SourceID; }
61  MCSymbol *getLabel() const { return Label; }
62};
63
64/// DotDebugLocEntry - This struct describes location entries emitted in
65/// .debug_loc section.
66typedef struct DotDebugLocEntry {
67  const MCSymbol *Begin;
68  const MCSymbol *End;
69  MachineLocation Loc;
70  const MDNode *Variable;
71  bool Merged;
72  bool Constant;
73  enum EntryType {
74    E_Location,
75    E_Integer,
76    E_ConstantFP,
77    E_ConstantInt
78  };
79  enum EntryType EntryKind;
80
81  union {
82    int64_t Int;
83    const ConstantFP *CFP;
84    const ConstantInt *CIP;
85  } Constants;
86  DotDebugLocEntry()
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,
90                   const MDNode *V)
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, const ConstantInt *IPtr)
100    : Begin(B), End(E), Variable(0), Merged(false),
101      Constant(true) { Constants.CIP = IPtr; EntryKind = E_ConstantInt; }
102
103  /// Empty entries are also used as a trigger to emit temp label. Such
104  /// labels are referenced is used to find debug_loc offset for a given DIE.
105  bool isEmpty() { return Begin == 0 && End == 0; }
106  bool isMerged() { return Merged; }
107  void Merge(DotDebugLocEntry *Next) {
108    if (!(Begin && Loc == Next->Loc && End == Next->Begin))
109      return;
110    Next->Begin = Begin;
111    Merged = true;
112  }
113  bool isLocation() const    { return EntryKind == E_Location; }
114  bool isInt() const         { return EntryKind == E_Integer; }
115  bool isConstantFP() const  { return EntryKind == E_ConstantFP; }
116  bool isConstantInt() const { return EntryKind == E_ConstantInt; }
117  int64_t getInt()                    { return Constants.Int; }
118  const ConstantFP *getConstantFP()   { return Constants.CFP; }
119  const ConstantInt *getConstantInt() { return Constants.CIP; }
120} DotDebugLocEntry;
121
122//===----------------------------------------------------------------------===//
123/// DbgVariable - This class is used to track local variable information.
124///
125class DbgVariable {
126  DIVariable Var;                    // Variable Descriptor.
127  DIE *TheDIE;                       // Variable DIE.
128  unsigned DotDebugLocOffset;        // Offset in DotDebugLocEntries.
129  DbgVariable *AbsVar;               // Corresponding Abstract variable, if any.
130  const MachineInstr *MInsn;         // DBG_VALUE instruction of the variable.
131  int FrameIndex;
132public:
133  // AbsVar may be NULL.
134  DbgVariable(DIVariable V, DbgVariable *AV)
135    : Var(V), TheDIE(0), DotDebugLocOffset(~0U), AbsVar(AV), MInsn(0),
136      FrameIndex(~0) {}
137
138  // Accessors.
139  DIVariable getVariable()           const { return Var; }
140  void setDIE(DIE *D)                      { TheDIE = D; }
141  DIE *getDIE()                      const { return TheDIE; }
142  void setDotDebugLocOffset(unsigned O)    { DotDebugLocOffset = O; }
143  unsigned getDotDebugLocOffset()    const { return DotDebugLocOffset; }
144  StringRef getName()                const { return Var.getName(); }
145  DbgVariable *getAbstractVariable() const { return AbsVar; }
146  const MachineInstr *getMInsn()     const { return MInsn; }
147  void setMInsn(const MachineInstr *M)     { MInsn = M; }
148  int getFrameIndex()                const { return FrameIndex; }
149  void setFrameIndex(int FI)               { FrameIndex = FI; }
150  // Translate tag to proper Dwarf tag.
151  unsigned getTag()                  const {
152    if (Var.getTag() == dwarf::DW_TAG_arg_variable)
153      return dwarf::DW_TAG_formal_parameter;
154
155    return dwarf::DW_TAG_variable;
156  }
157  /// isArtificial - Return true if DbgVariable is artificial.
158  bool isArtificial()                const {
159    if (Var.isArtificial())
160      return true;
161    if (Var.getTag() == dwarf::DW_TAG_arg_variable
162        && getType().isArtificial())
163      return true;
164    return false;
165  }
166  bool variableHasComplexAddress()   const {
167    assert(Var.Verify() && "Invalid complex DbgVariable!");
168    return Var.hasComplexAddress();
169  }
170  bool isBlockByrefVariable()        const {
171    assert(Var.Verify() && "Invalid complex DbgVariable!");
172    return Var.isBlockByrefVariable();
173  }
174  unsigned getNumAddrElements()      const {
175    assert(Var.Verify() && "Invalid complex DbgVariable!");
176    return Var.getNumAddrElements();
177  }
178  uint64_t getAddrElement(unsigned i) const {
179    return Var.getAddrElement(i);
180  }
181  DIType getType() const;
182};
183
184class DwarfDebug {
185  /// Asm - Target of Dwarf emission.
186  AsmPrinter *Asm;
187
188  /// MMI - Collected machine module information.
189  MachineModuleInfo *MMI;
190
191  /// DIEValueAllocator - All DIEValues are allocated through this allocator.
192  BumpPtrAllocator DIEValueAllocator;
193
194  //===--------------------------------------------------------------------===//
195  // Attributes used to construct specific Dwarf sections.
196  //
197
198  CompileUnit *FirstCU;
199
200  /// Maps MDNode with its corresponding CompileUnit.
201  DenseMap <const MDNode *, CompileUnit *> CUMap;
202
203  /// Maps subprogram MDNode with its corresponding CompileUnit.
204  DenseMap <const MDNode *, CompileUnit *> SPMap;
205
206  /// AbbreviationsSet - Used to uniquely define abbreviations.
207  ///
208  FoldingSet<DIEAbbrev> AbbreviationsSet;
209
210  /// Abbreviations - A list of all the unique abbreviations in use.
211  ///
212  std::vector<DIEAbbrev *> Abbreviations;
213
214  /// SourceIdMap - Source id map, i.e. pair of source filename and directory,
215  /// separated by a zero byte, mapped to a unique id.
216  StringMap<unsigned, BumpPtrAllocator&> SourceIdMap;
217
218  /// StringPool - A String->Symbol mapping of strings used by indirect
219  /// references.
220  StringMap<std::pair<MCSymbol*, unsigned>, BumpPtrAllocator&> StringPool;
221  unsigned NextStringPoolNumber;
222
223  /// SectionMap - Provides a unique id per text section.
224  ///
225  UniqueVector<const MCSection*> SectionMap;
226
227  /// CurrentFnArguments - List of Arguments (DbgValues) for current function.
228  SmallVector<DbgVariable *, 8> CurrentFnArguments;
229
230  LexicalScopes LScopes;
231
232  /// AbstractSPDies - Collection of abstract subprogram DIEs.
233  DenseMap<const MDNode *, DIE *> AbstractSPDies;
234
235  /// ScopeVariables - Collection of dbg variables of a scope.
236  DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8> > ScopeVariables;
237
238  /// AbstractVariables - Collection of abstract variables.
239  DenseMap<const MDNode *, DbgVariable *> AbstractVariables;
240
241  /// DotDebugLocEntries - Collection of DotDebugLocEntry.
242  SmallVector<DotDebugLocEntry, 4> DotDebugLocEntries;
243
244  /// InlinedSubprogramDIEs - Collection of subprogram DIEs that are marked
245  /// (at the end of the module) as DW_AT_inline.
246  SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
247
248  /// InlineInfo - Keep track of inlined functions and their location.  This
249  /// information is used to populate the debug_inlined section.
250  typedef std::pair<const MCSymbol *, DIE *> InlineInfoLabels;
251  DenseMap<const MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
252  SmallVector<const MDNode *, 4> InlinedSPNodes;
253
254  // ProcessedSPNodes - This is a collection of subprogram MDNodes that
255  // are processed to create DIEs.
256  SmallPtrSet<const MDNode *, 16> ProcessedSPNodes;
257
258  /// LabelsBeforeInsn - Maps instruction with label emitted before
259  /// instruction.
260  DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
261
262  /// LabelsAfterInsn - Maps instruction with label emitted after
263  /// instruction.
264  DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
265
266  /// UserVariables - Every user variable mentioned by a DBG_VALUE instruction
267  /// in order of appearance.
268  SmallVector<const MDNode*, 8> UserVariables;
269
270  /// DbgValues - For each user variable, keep a list of DBG_VALUE
271  /// instructions in order. The list can also contain normal instructions that
272  /// clobber the previous DBG_VALUE.
273  typedef DenseMap<const MDNode*, SmallVector<const MachineInstr*, 4> >
274    DbgValueHistoryMap;
275  DbgValueHistoryMap DbgValues;
276
277  SmallVector<const MCSymbol *, 8> DebugRangeSymbols;
278
279  /// Previous instruction's location information. This is used to determine
280  /// label location to indicate scope boundries in dwarf debug info.
281  DebugLoc PrevInstLoc;
282  MCSymbol *PrevLabel;
283
284  /// PrologEndLoc - This location indicates end of function prologue and
285  /// beginning of function body.
286  DebugLoc PrologEndLoc;
287
288  struct FunctionDebugFrameInfo {
289    unsigned Number;
290    std::vector<MachineMove> Moves;
291
292    FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
293      : Number(Num), Moves(M) {}
294  };
295
296  std::vector<FunctionDebugFrameInfo> DebugFrames;
297
298  // Section Symbols: these are assembler temporary labels that are emitted at
299  // the beginning of each supported dwarf section.  These are used to form
300  // section offsets and are created by EmitSectionLabels.
301  MCSymbol *DwarfInfoSectionSym, *DwarfAbbrevSectionSym;
302  MCSymbol *DwarfStrSectionSym, *TextSectionSym, *DwarfDebugRangeSectionSym;
303  MCSymbol *DwarfDebugLocSectionSym;
304  MCSymbol *FunctionBeginSym, *FunctionEndSym;
305
306  // As an optimization, there is no need to emit an entry in the directory
307  // table for the same directory as DW_at_comp_dir.
308  StringRef CompilationDir;
309
310private:
311
312  /// assignAbbrevNumber - Define a unique number for the abbreviation.
313  ///
314  void assignAbbrevNumber(DIEAbbrev &Abbrev);
315
316  void addScopeVariable(LexicalScope *LS, DbgVariable *Var);
317
318  /// findAbstractVariable - Find abstract variable associated with Var.
319  DbgVariable *findAbstractVariable(DIVariable &Var, DebugLoc Loc);
320
321  /// updateSubprogramScopeDIE - Find DIE for the given subprogram and
322  /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
323  /// If there are global variables in this scope then create and insert
324  /// DIEs for these variables.
325  DIE *updateSubprogramScopeDIE(CompileUnit *SPCU, const MDNode *SPNode);
326
327  /// constructLexicalScope - Construct new DW_TAG_lexical_block
328  /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
329  DIE *constructLexicalScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
330
331  /// constructInlinedScopeDIE - This scope represents inlined body of
332  /// a function. Construct DIE to represent this concrete inlined copy
333  /// of the function.
334  DIE *constructInlinedScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
335
336  /// constructScopeDIE - Construct a DIE for this scope.
337  DIE *constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope);
338
339  /// EmitSectionLabels - Emit initial Dwarf sections with a label at
340  /// the start of each one.
341  void EmitSectionLabels();
342
343  /// emitDIE - Recursively Emits a debug information entry.
344  ///
345  void emitDIE(DIE *Die);
346
347  /// computeSizeAndOffset - Compute the size and offset of a DIE.
348  ///
349  unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
350
351  /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
352  ///
353  void computeSizeAndOffsets();
354
355  /// EmitDebugInfo - Emit the debug info section.
356  ///
357  void emitDebugInfo();
358
359  /// emitAbbreviations - Emit the abbreviation section.
360  ///
361  void emitAbbreviations() const;
362
363  /// emitEndOfLineMatrix - Emit the last address of the section and the end of
364  /// the line matrix.
365  ///
366  void emitEndOfLineMatrix(unsigned SectionEnd);
367
368  /// emitAccelNames - Emit visible names into a hashed accelerator table
369  /// section.
370  void emitAccelNames();
371
372  /// emitAccelObjC - Emit objective C classes and categories into a hashed
373  /// accelerator table section.
374  void emitAccelObjC();
375
376  /// emitAccelNamespace - Emit namespace dies into a hashed accelerator
377  /// table.
378  void emitAccelNamespaces();
379
380  /// emitAccelTypes() - Emit type dies into a hashed accelerator table.
381  ///
382  void emitAccelTypes();
383
384  /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
385  ///
386  void emitDebugPubTypes();
387
388  /// emitDebugStr - Emit visible names into a debug str section.
389  ///
390  void emitDebugStr();
391
392  /// emitDebugLoc - Emit visible names into a debug loc section.
393  ///
394  void emitDebugLoc();
395
396  /// EmitDebugARanges - Emit visible names into a debug aranges section.
397  ///
398  void EmitDebugARanges();
399
400  /// emitDebugRanges - Emit visible names into a debug ranges section.
401  ///
402  void emitDebugRanges();
403
404  /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
405  ///
406  void emitDebugMacInfo();
407
408  /// emitDebugInlineInfo - Emit inline info using following format.
409  /// Section Header:
410  /// 1. length of section
411  /// 2. Dwarf version number
412  /// 3. address size.
413  ///
414  /// Entries (one "entry" for each function that was inlined):
415  ///
416  /// 1. offset into __debug_str section for MIPS linkage name, if exists;
417  ///   otherwise offset into __debug_str for regular function name.
418  /// 2. offset into __debug_str section for regular function name.
419  /// 3. an unsigned LEB128 number indicating the number of distinct inlining
420  /// instances for the function.
421  ///
422  /// The rest of the entry consists of a {die_offset, low_pc} pair for each
423  /// inlined instance; the die_offset points to the inlined_subroutine die in
424  /// the __debug_info section, and the low_pc is the starting address for the
425  /// inlining instance.
426  void emitDebugInlineInfo();
427
428  /// constructCompileUnit - Create new CompileUnit for the given
429  /// metadata node with tag DW_TAG_compile_unit.
430  CompileUnit *constructCompileUnit(const MDNode *N);
431
432  /// construct SubprogramDIE - Construct subprogram DIE.
433  void constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N);
434
435  /// recordSourceLine - Register a source line with debug info. Returns the
436  /// unique label that was emitted and which provides correspondence to
437  /// the source line list.
438  void recordSourceLine(unsigned Line, unsigned Col, const MDNode *Scope,
439                        unsigned Flags);
440
441  /// identifyScopeMarkers() - Indentify instructions that are marking the
442  /// beginning of or ending of a scope.
443  void identifyScopeMarkers();
444
445  /// addCurrentFnArgument - If Var is an current function argument that add
446  /// it in CurrentFnArguments list.
447  bool addCurrentFnArgument(const MachineFunction *MF,
448                            DbgVariable *Var, LexicalScope *Scope);
449
450  /// collectVariableInfo - Populate LexicalScope entries with variables' info.
451  void collectVariableInfo(const MachineFunction *,
452                           SmallPtrSet<const MDNode *, 16> &ProcessedVars);
453
454  /// collectVariableInfoFromMMITable - Collect variable information from
455  /// side table maintained by MMI.
456  void collectVariableInfoFromMMITable(const MachineFunction * MF,
457                                       SmallPtrSet<const MDNode *, 16> &P);
458
459  /// requestLabelBeforeInsn - Ensure that a label will be emitted before MI.
460  void requestLabelBeforeInsn(const MachineInstr *MI) {
461    LabelsBeforeInsn.insert(std::make_pair(MI, (MCSymbol*)0));
462  }
463
464  /// getLabelBeforeInsn - Return Label preceding the instruction.
465  const MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
466
467  /// requestLabelAfterInsn - Ensure that a label will be emitted after MI.
468  void requestLabelAfterInsn(const MachineInstr *MI) {
469    LabelsAfterInsn.insert(std::make_pair(MI, (MCSymbol*)0));
470  }
471
472  /// getLabelAfterInsn - Return Label immediately following the instruction.
473  const MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
474
475public:
476  //===--------------------------------------------------------------------===//
477  // Main entry points.
478  //
479  DwarfDebug(AsmPrinter *A, Module *M);
480  ~DwarfDebug();
481
482  /// collectInfoFromNamedMDNodes - Collect debug info from named mdnodes such
483  /// as llvm.dbg.enum and llvm.dbg.ty
484  void collectInfoFromNamedMDNodes(Module *M);
485
486  /// collectLegacyDebugInfo - Collect debug info using DebugInfoFinder.
487  /// FIXME - Remove this when DragonEgg switches to DIBuilder.
488  bool collectLegacyDebugInfo(Module *M);
489
490  /// beginModule - Emit all Dwarf sections that should come prior to the
491  /// content.
492  void beginModule(Module *M);
493
494  /// endModule - Emit all Dwarf sections that should come after the content.
495  ///
496  void endModule();
497
498  /// beginFunction - Gather pre-function debug information.  Assumes being
499  /// emitted immediately after the function entry point.
500  void beginFunction(const MachineFunction *MF);
501
502  /// endFunction - Gather and emit post-function debug information.
503  ///
504  void endFunction(const MachineFunction *MF);
505
506  /// beginInstruction - Process beginning of an instruction.
507  void beginInstruction(const MachineInstr *MI);
508
509  /// endInstruction - Prcess end of an instruction.
510  void endInstruction(const MachineInstr *MI);
511
512  /// GetOrCreateSourceID - Look up the source id with the given directory and
513  /// source file names. If none currently exists, create a new id and insert it
514  /// in the SourceIds map.
515  unsigned GetOrCreateSourceID(StringRef DirName, StringRef FullName);
516
517  /// getStringPool - returns the entry into the start of the pool.
518  MCSymbol *getStringPool();
519
520  /// getStringPoolEntry - returns an entry into the string pool with the given
521  /// string text.
522  MCSymbol *getStringPoolEntry(StringRef Str);
523};
524} // End of namespace llvm
525
526#endif
527