DwarfDebug.h revision aa219559b8feecd575d706a3c7ef6308f08b68af
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 "DwarfPrinter.h"
19#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/CodeGen/MachineLocation.h"
21#include "llvm/Analysis/DebugInfo.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/Support/Mangler.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/FoldingSet.h"
26#include "llvm/ADT/SmallSet.h"
27#include "llvm/ADT/StringMap.h"
28#include "llvm/ADT/UniqueVector.h"
29#include <string>
30
31namespace llvm {
32
33class CompileUnit;
34class DbgVariable;
35class DbgScope;
36class DbgConcreteScope;
37class MachineFrameInfo;
38class MachineModuleInfo;
39class TargetAsmInfo;
40class Timer;
41
42//===----------------------------------------------------------------------===//
43/// SrcLineInfo - This class is used to record source line correspondence.
44///
45class VISIBILITY_HIDDEN SrcLineInfo {
46  unsigned Line;                     // Source line number.
47  unsigned Column;                   // Source column.
48  unsigned SourceID;                 // Source ID number.
49  unsigned LabelID;                  // Label in code ID number.
50public:
51  SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
52    : Line(L), Column(C), SourceID(S), LabelID(I) {}
53
54  // Accessors
55  unsigned getLine() const { return Line; }
56  unsigned getColumn() const { return Column; }
57  unsigned getSourceID() const { return SourceID; }
58  unsigned getLabelID() const { return LabelID; }
59};
60
61class VISIBILITY_HIDDEN DwarfDebug : public Dwarf {
62  //===--------------------------------------------------------------------===//
63  // Attributes used to construct specific Dwarf sections.
64  //
65
66  /// CompileUnitMap - A map of global variables representing compile units to
67  /// compile units.
68  DenseMap<Value *, CompileUnit *> CompileUnitMap;
69
70  /// CompileUnits - All the compile units in this module.
71  ///
72  SmallVector<CompileUnit *, 8> CompileUnits;
73
74  /// ModuleCU - All DIEs are inserted in ModuleCU.
75  CompileUnit *ModuleCU;
76
77  /// AbbreviationsSet - Used to uniquely define abbreviations.
78  ///
79  FoldingSet<DIEAbbrev> AbbreviationsSet;
80
81  /// Abbreviations - A list of all the unique abbreviations in use.
82  ///
83  std::vector<DIEAbbrev *> Abbreviations;
84
85  /// DirectoryIdMap - Directory name to directory id map.
86  ///
87  StringMap<unsigned> DirectoryIdMap;
88
89  /// DirectoryNames - A list of directory names.
90  SmallVector<std::string, 8> DirectoryNames;
91
92  /// SourceFileIdMap - Source file name to source file id map.
93  ///
94  StringMap<unsigned> SourceFileIdMap;
95
96  /// SourceFileNames - A list of source file names.
97  SmallVector<std::string, 8> SourceFileNames;
98
99  /// SourceIdMap - Source id map, i.e. pair of directory id and source file
100  /// id mapped to a unique id.
101  DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
102
103  /// SourceIds - Reverse map from source id to directory id + file id pair.
104  ///
105  SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
106
107  /// Lines - List of of source line correspondence.
108  std::vector<SrcLineInfo> Lines;
109
110  /// ValuesSet - Used to uniquely define values.
111  ///
112  FoldingSet<DIEValue> ValuesSet;
113
114  /// Values - A list of all the unique values in use.
115  ///
116  std::vector<DIEValue *> Values;
117
118  /// StringPool - A UniqueVector of strings used by indirect references.
119  ///
120  UniqueVector<std::string> StringPool;
121
122  /// SectionMap - Provides a unique id per text section.
123  ///
124  UniqueVector<const Section*> SectionMap;
125
126  /// SectionSourceLines - Tracks line numbers per text section.
127  ///
128  std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
129
130  /// didInitial - Flag to indicate if initial emission has been done.
131  ///
132  bool didInitial;
133
134  /// shouldEmit - Flag to indicate if debug information should be emitted.
135  ///
136  bool shouldEmit;
137
138  // FunctionDbgScope - Top level scope for the current function.
139  //
140  DbgScope *FunctionDbgScope;
141
142  /// DbgScopeMap - Tracks the scopes in the current function.
143  DenseMap<GlobalVariable *, DbgScope *> DbgScopeMap;
144
145  /// DbgAbstractScopeMap - Tracks abstract instance scopes in the current
146  /// function.
147  DenseMap<GlobalVariable *, DbgScope *> DbgAbstractScopeMap;
148
149  /// DbgConcreteScopeMap - Tracks concrete instance scopes in the current
150  /// function.
151  DenseMap<GlobalVariable *,
152           SmallVector<DbgScope *, 8> > DbgConcreteScopeMap;
153
154  /// InlineInfo - Keep track of inlined functions and their location.  This
155  /// information is used to populate debug_inlined section.
156  DenseMap<GlobalVariable *, SmallVector<unsigned, 4> > InlineInfo;
157
158  /// InlinedVariableScopes - Scopes information for the inlined subroutine
159  /// variables.
160  DenseMap<const MachineInstr *, DbgScope *> InlinedVariableScopes;
161
162  /// AbstractInstanceRootMap - Map of abstract instance roots of inlined
163  /// functions. These are subroutine entries that contain a DW_AT_inline
164  /// attribute.
165  DenseMap<const GlobalVariable *, DbgScope *> AbstractInstanceRootMap;
166
167  /// AbstractInstanceRootList - List of abstract instance roots of inlined
168  /// functions. These are subroutine entries that contain a DW_AT_inline
169  /// attribute.
170  SmallVector<DbgScope *, 32> AbstractInstanceRootList;
171
172  /// LexicalScopeStack - A stack of lexical scopes. The top one is the current
173  /// scope.
174  SmallVector<DbgScope *, 16> LexicalScopeStack;
175
176  /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
177  /// used when calculating the "origin" of a concrete instance of an inlined
178  /// function.
179  DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
180
181  /// DebugTimer - Timer for the Dwarf debug writer.
182  Timer *DebugTimer;
183
184  Mangler *LLVMMangler;
185
186  struct FunctionDebugFrameInfo {
187    unsigned Number;
188    std::vector<MachineMove> Moves;
189
190    FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
191      : Number(Num), Moves(M) {}
192  };
193
194  std::vector<FunctionDebugFrameInfo> DebugFrames;
195
196  /// getSourceDirectoryAndFileIds - Return the directory and file ids that
197  /// maps to the source id. Source id starts at 1.
198  std::pair<unsigned, unsigned>
199  getSourceDirectoryAndFileIds(unsigned SId) const {
200    return SourceIds[SId-1];
201  }
202
203  /// getNumSourceDirectories - Return the number of source directories in the
204  /// debug info.
205  unsigned getNumSourceDirectories() const {
206    return DirectoryNames.size();
207  }
208
209  /// getSourceDirectoryName - Return the name of the directory corresponding
210  /// to the id.
211  const std::string &getSourceDirectoryName(unsigned Id) const {
212    return DirectoryNames[Id - 1];
213  }
214
215  /// getSourceFileName - Return the name of the source file corresponding
216  /// to the id.
217  const std::string &getSourceFileName(unsigned Id) const {
218    return SourceFileNames[Id - 1];
219  }
220
221  /// getNumSourceIds - Return the number of unique source ids.
222  unsigned getNumSourceIds() const {
223    return SourceIds.size();
224  }
225
226  /// AssignAbbrevNumber - Define a unique number for the abbreviation.
227  ///
228  void AssignAbbrevNumber(DIEAbbrev &Abbrev);
229
230  /// CreateDIEEntry - Creates a new DIEEntry to be a proxy for a debug
231  /// information entry.
232  DIEEntry *CreateDIEEntry(DIE *Entry = NULL);
233
234  /// SetDIEEntry - Set a DIEEntry once the debug information entry is defined.
235  ///
236  void SetDIEEntry(DIEEntry *Value, DIE *Entry);
237
238  /// AddUInt - Add an unsigned integer attribute data and value.
239  ///
240  void AddUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
241
242  /// AddSInt - Add an signed integer attribute data and value.
243  ///
244  void AddSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
245
246  /// AddString - Add a string attribute data and value.
247  ///
248  void AddString(DIE *Die, unsigned Attribute, unsigned Form,
249                 const std::string &String);
250
251  /// AddLabel - Add a Dwarf label attribute data and value.
252  ///
253  void AddLabel(DIE *Die, unsigned Attribute, unsigned Form,
254                const DWLabel &Label);
255
256  /// AddObjectLabel - Add an non-Dwarf label attribute data and value.
257  ///
258  void AddObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
259                      const std::string &Label);
260
261  /// AddSectionOffset - Add a section offset label attribute data and value.
262  ///
263  void AddSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
264                        const DWLabel &Label, const DWLabel &Section,
265                        bool isEH = false, bool useSet = true);
266
267  /// AddDelta - Add a label delta attribute data and value.
268  ///
269  void AddDelta(DIE *Die, unsigned Attribute, unsigned Form,
270                const DWLabel &Hi, const DWLabel &Lo);
271
272  /// AddDIEEntry - Add a DIE attribute data and value.
273  ///
274  void AddDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
275    Die->AddValue(Attribute, Form, CreateDIEEntry(Entry));
276  }
277
278  /// AddBlock - Add block data.
279  ///
280  void AddBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
281
282  /// AddSourceLine - Add location information to specified debug information
283  /// entry.
284  void AddSourceLine(DIE *Die, const DIVariable *V);
285
286  /// AddSourceLine - Add location information to specified debug information
287  /// entry.
288  void AddSourceLine(DIE *Die, const DIGlobal *G);
289
290  void AddSourceLine(DIE *Die, const DIType *Ty);
291
292  /// AddAddress - Add an address attribute to a die based on the location
293  /// provided.
294  void AddAddress(DIE *Die, unsigned Attribute,
295                  const MachineLocation &Location);
296
297  /// AddType - Add a new type attribute to the specified entity.
298  void AddType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
299
300  /// ConstructTypeDIE - Construct basic type die from DIBasicType.
301  void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
302                        DIBasicType BTy);
303
304  /// ConstructTypeDIE - Construct derived type die from DIDerivedType.
305  void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
306                        DIDerivedType DTy);
307
308  /// ConstructTypeDIE - Construct type DIE from DICompositeType.
309  void ConstructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
310                        DICompositeType CTy);
311
312  /// ConstructSubrangeDIE - Construct subrange DIE from DISubrange.
313  void ConstructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
314
315  /// ConstructArrayTypeDIE - Construct array type DIE from DICompositeType.
316  void ConstructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
317                             DICompositeType *CTy);
318
319  /// ConstructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
320  DIE *ConstructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
321
322  /// CreateGlobalVariableDIE - Create new DIE using GV.
323  DIE *CreateGlobalVariableDIE(CompileUnit *DW_Unit,
324                               const DIGlobalVariable &GV);
325
326  /// CreateMemberDIE - Create new member DIE.
327  DIE *CreateMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
328
329  /// CreateSubprogramDIE - Create new DIE using SP.
330  DIE *CreateSubprogramDIE(CompileUnit *DW_Unit,
331                           const DISubprogram &SP,
332                           bool IsConstructor = false,
333                           bool IsInlined = false);
334
335  /// FindCompileUnit - Get the compile unit for the given descriptor.
336  ///
337  CompileUnit &FindCompileUnit(DICompileUnit Unit) const;
338
339  /// CreateDbgScopeVariable - Create a new scope variable.
340  ///
341  DIE *CreateDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
342
343  /// getOrCreateScope - Returns the scope associated with the given descriptor.
344  ///
345  DbgScope *getOrCreateScope(GlobalVariable *V);
346
347  /// ConstructDbgScope - Construct the components of a scope.
348  ///
349  void ConstructDbgScope(DbgScope *ParentScope,
350                         unsigned ParentStartID, unsigned ParentEndID,
351                         DIE *ParentDie, CompileUnit *Unit);
352
353  /// ConstructFunctionDbgScope - Construct the scope for the subprogram.
354  ///
355  void ConstructFunctionDbgScope(DbgScope *RootScope,
356                                 bool AbstractScope = false);
357
358  /// ConstructDefaultDbgScope - Construct a default scope for the subprogram.
359  ///
360  void ConstructDefaultDbgScope(MachineFunction *MF);
361
362  /// EmitInitial - Emit initial Dwarf declarations.  This is necessary for cc
363  /// tools to recognize the object file contains Dwarf information.
364  void EmitInitial();
365
366  /// EmitDIE - Recusively Emits a debug information entry.
367  ///
368  void EmitDIE(DIE *Die);
369
370  /// SizeAndOffsetDie - Compute the size and offset of a DIE.
371  ///
372  unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
373
374  /// SizeAndOffsets - Compute the size and offset of all the DIEs.
375  ///
376  void SizeAndOffsets();
377
378  /// EmitDebugInfo / EmitDebugInfoPerCU - Emit the debug info section.
379  ///
380  void EmitDebugInfoPerCU(CompileUnit *Unit);
381
382  void EmitDebugInfo();
383
384  /// EmitAbbreviations - Emit the abbreviation section.
385  ///
386  void EmitAbbreviations() const;
387
388  /// EmitEndOfLineMatrix - Emit the last address of the section and the end of
389  /// the line matrix.
390  ///
391  void EmitEndOfLineMatrix(unsigned SectionEnd);
392
393  /// EmitDebugLines - Emit source line information.
394  ///
395  void EmitDebugLines();
396
397  /// EmitCommonDebugFrame - Emit common frame info into a debug frame section.
398  ///
399  void EmitCommonDebugFrame();
400
401  /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
402  /// section.
403  void EmitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
404
405  void EmitDebugPubNamesPerCU(CompileUnit *Unit);
406
407  /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
408  ///
409  void EmitDebugPubNames();
410
411  /// EmitDebugStr - Emit visible names into a debug str section.
412  ///
413  void EmitDebugStr();
414
415  /// EmitDebugLoc - Emit visible names into a debug loc section.
416  ///
417  void EmitDebugLoc();
418
419  /// EmitDebugARanges - Emit visible names into a debug aranges section.
420  ///
421  void EmitDebugARanges();
422
423  /// EmitDebugRanges - Emit visible names into a debug ranges section.
424  ///
425  void EmitDebugRanges();
426
427  /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
428  ///
429  void EmitDebugMacInfo();
430
431  /// EmitDebugInlineInfo - Emit inline info using following format.
432  /// Section Header:
433  /// 1. length of section
434  /// 2. Dwarf version number
435  /// 3. address size.
436  ///
437  /// Entries (one "entry" for each function that was inlined):
438  ///
439  /// 1. offset into __debug_str section for MIPS linkage name, if exists;
440  ///   otherwise offset into __debug_str for regular function name.
441  /// 2. offset into __debug_str section for regular function name.
442  /// 3. an unsigned LEB128 number indicating the number of distinct inlining
443  /// instances for the function.
444  ///
445  /// The rest of the entry consists of a {die_offset, low_pc}  pair for each
446  /// inlined instance; the die_offset points to the inlined_subroutine die in
447  /// the __debug_info section, and the low_pc is the starting address  for the
448  ///  inlining instance.
449  void EmitDebugInlineInfo();
450
451  /// GetOrCreateSourceID - Look up the source id with the given directory and
452  /// source file names. If none currently exists, create a new id and insert it
453  /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
454  /// as well.
455  unsigned GetOrCreateSourceID(const std::string &DirName,
456                               const std::string &FileName);
457
458  void ConstructCompileUnit(GlobalVariable *GV);
459
460  void ConstructGlobalVariableDIE(GlobalVariable *GV);
461
462  void ConstructSubprogram(GlobalVariable *GV);
463
464public:
465  //===--------------------------------------------------------------------===//
466  // Main entry points.
467  //
468  DwarfDebug(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T);
469  virtual ~DwarfDebug();
470
471  /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
472  /// be emitted.
473  bool ShouldEmitDwarfDebug() const { return shouldEmit; }
474
475  /// BeginModule - Emit all Dwarf sections that should come prior to the
476  /// content.
477  void BeginModule(Module *M, MachineModuleInfo *MMI);
478
479  /// EndModule - Emit all Dwarf sections that should come after the content.
480  ///
481  void EndModule();
482
483  /// BeginFunction - Gather pre-function debug information.  Assumes being
484  /// emitted immediately after the function entry point.
485  void BeginFunction(MachineFunction *MF);
486
487  /// EndFunction - Gather and emit post-function debug information.
488  ///
489  void EndFunction(MachineFunction *MF);
490
491  /// RecordSourceLine - Records location information and associates it with a
492  /// label. Returns a unique label ID used to generate a label and provide
493  /// correspondence to the source line list.
494  unsigned RecordSourceLine(Value *V, unsigned Line, unsigned Col);
495
496  /// RecordSourceLine - Records location information and associates it with a
497  /// label. Returns a unique label ID used to generate a label and provide
498  /// correspondence to the source line list.
499  unsigned RecordSourceLine(unsigned Line, unsigned Col, DICompileUnit CU);
500
501  /// getRecordSourceLineCount - Return the number of source lines in the debug
502  /// info.
503  unsigned getRecordSourceLineCount() const {
504    return Lines.size();
505  }
506
507  /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
508  /// timed. Look up the source id with the given directory and source file
509  /// names. If none currently exists, create a new id and insert it in the
510  /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
511  /// well.
512  unsigned getOrCreateSourceID(const std::string &DirName,
513                               const std::string &FileName);
514
515  /// RecordRegionStart - Indicate the start of a region.
516  unsigned RecordRegionStart(GlobalVariable *V);
517
518  /// RecordRegionEnd - Indicate the end of a region.
519  unsigned RecordRegionEnd(GlobalVariable *V);
520
521  /// RecordVariable - Indicate the declaration of  a local variable.
522  void RecordVariable(GlobalVariable *GV, unsigned FrameIndex,
523                      const MachineInstr *MI);
524
525  //// RecordInlinedFnStart - Indicate the start of inlined subroutine.
526  unsigned RecordInlinedFnStart(DISubprogram &SP, DICompileUnit CU,
527                                unsigned Line, unsigned Col);
528
529  /// RecordInlinedFnEnd - Indicate the end of inlined subroutine.
530  unsigned RecordInlinedFnEnd(DISubprogram &SP);
531
532  /// RecordVariableScope - Record scope for the variable declared by
533  /// DeclareMI. DeclareMI must describe TargetInstrInfo::DECLARE. Record scopes
534  /// for only inlined subroutine variables. Other variables's scopes are
535  /// determined during RecordVariable().
536  void RecordVariableScope(DIVariable &DV, const MachineInstr *DeclareMI);
537};
538
539} // End of namespace llvm
540
541#endif
542