6c8a28d68319b10e132802b78435d8f5b4c89571
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfDebug.h
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/ADT/ValueMap.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/ADT/UniqueVector.h"
28 #include <string>
29
30 namespace llvm {
31
32 class CompileUnit;
33 class DbgConcreteScope;
34 class DbgScope;
35 class DbgVariable;
36 class MachineFrameInfo;
37 class MachineModuleInfo;
38 class MCAsmInfo;
39 class Timer;
40
41 //===----------------------------------------------------------------------===//
42 /// SrcLineInfo - This class is used to record source line correspondence.
43 ///
44 class SrcLineInfo {
45   unsigned Line;                     // Source line number.
46   unsigned Column;                   // Source column.
47   unsigned SourceID;                 // Source ID number.
48   unsigned LabelID;                  // Label in code ID number.
49 public:
50   SrcLineInfo(unsigned L, unsigned C, unsigned S, unsigned I)
51     : Line(L), Column(C), SourceID(S), LabelID(I) {}
52
53   // Accessors
54   unsigned getLine() const { return Line; }
55   unsigned getColumn() const { return Column; }
56   unsigned getSourceID() const { return SourceID; }
57   unsigned getLabelID() const { return LabelID; }
58 };
59
60 class DwarfDebug : public Dwarf {
61   //===--------------------------------------------------------------------===//
62   // Attributes used to construct specific Dwarf sections.
63   //
64
65   /// CompileUnitMap - A map of global variables representing compile units to
66   /// compile units.
67   DenseMap<Value *, CompileUnit *> CompileUnitMap;
68
69   /// CompileUnits - All the compile units in this module.
70   ///
71   SmallVector<CompileUnit *, 8> CompileUnits;
72
73   /// ModuleCU - All DIEs are inserted in ModuleCU.
74   CompileUnit *ModuleCU;
75
76   /// AbbreviationsSet - Used to uniquely define abbreviations.
77   ///
78   FoldingSet<DIEAbbrev> AbbreviationsSet;
79
80   /// Abbreviations - A list of all the unique abbreviations in use.
81   ///
82   std::vector<DIEAbbrev *> Abbreviations;
83
84   /// DirectoryIdMap - Directory name to directory id map.
85   ///
86   StringMap<unsigned> DirectoryIdMap;
87
88   /// DirectoryNames - A list of directory names.
89   SmallVector<std::string, 8> DirectoryNames;
90
91   /// SourceFileIdMap - Source file name to source file id map.
92   ///
93   StringMap<unsigned> SourceFileIdMap;
94
95   /// SourceFileNames - A list of source file names.
96   SmallVector<std::string, 8> SourceFileNames;
97
98   /// SourceIdMap - Source id map, i.e. pair of directory id and source file
99   /// id mapped to a unique id.
100   DenseMap<std::pair<unsigned, unsigned>, unsigned> SourceIdMap;
101
102   /// SourceIds - Reverse map from source id to directory id + file id pair.
103   ///
104   SmallVector<std::pair<unsigned, unsigned>, 8> SourceIds;
105
106   /// Lines - List of of source line correspondence.
107   std::vector<SrcLineInfo> Lines;
108
109   /// DIEValues - A list of all the unique values in use.
110   ///
111   std::vector<DIEValue *> DIEValues;
112
113   /// StringPool - A UniqueVector of strings used by indirect references.
114   ///
115   UniqueVector<std::string> StringPool;
116
117   /// SectionMap - Provides a unique id per text section.
118   ///
119   UniqueVector<const MCSection*> SectionMap;
120
121   /// SectionSourceLines - Tracks line numbers per text section.
122   ///
123   std::vector<std::vector<SrcLineInfo> > SectionSourceLines;
124
125   /// didInitial - Flag to indicate if initial emission has been done.
126   ///
127   bool didInitial;
128
129   /// shouldEmit - Flag to indicate if debug information should be emitted.
130   ///
131   bool shouldEmit;
132
133   // CurrentFnDbgScope - Top level scope for the current function.
134   //
135   DbgScope *CurrentFnDbgScope;
136   
137   /// DbgScopeMap - Tracks the scopes in the current function.
138   ///
139   ValueMap<MDNode *, DbgScope *> DbgScopeMap;
140
141   /// ConcreteScopes - Tracks the concrete scopees in the current function.
142   /// These scopes are also included in DbgScopeMap.
143   ValueMap<MDNode *, DbgScope *> ConcreteScopes;
144
145   /// AbstractScopes - Tracks the abstract scopes a module. These scopes are
146   /// not included DbgScopeMap.
147   ValueMap<MDNode *, DbgScope *> AbstractScopes;
148   SmallVector<DbgScope *, 4>AbstractScopesList;
149
150   /// AbstractVariables - Collection on abstract variables.
151   ValueMap<MDNode *, DbgVariable *> AbstractVariables;
152
153   /// InliendSubprogramDIEs - Collection of subprgram DIEs that are marked
154   /// (at the end of the module) as DW_AT_inline.
155   SmallPtrSet<DIE *, 4> InlinedSubprogramDIEs;
156
157   DenseMap<DIE *, WeakVH> ContainingTypeMap;
158
159   /// AbstractSubprogramDIEs - Collection of abstruct subprogram DIEs.
160   SmallPtrSet<DIE *, 4> AbstractSubprogramDIEs;
161
162   /// TopLevelDIEs - Collection of top level DIEs. 
163   SmallPtrSet<DIE *, 4> TopLevelDIEs;
164   SmallVector<DIE *, 4> TopLevelDIEsVector;
165
166   /// ScopedGVs - Tracks global variables that are not at file scope.
167   /// For example void f() { static int b = 42; }
168   SmallVector<WeakVH, 4> ScopedGVs;
169
170   typedef SmallVector<DbgScope *, 2> ScopeVector;
171   typedef DenseMap<const MachineInstr *, ScopeVector>
172     InsnToDbgScopeMapTy;
173
174   /// DbgScopeBeginMap - Maps instruction with a list of DbgScopes it starts.
175   InsnToDbgScopeMapTy DbgScopeBeginMap;
176
177   /// DbgScopeEndMap - Maps instruction with a list DbgScopes it ends.
178   InsnToDbgScopeMapTy DbgScopeEndMap;
179
180   /// InlineInfo - Keep track of inlined functions and their location.  This
181   /// information is used to populate debug_inlined section.
182   typedef std::pair<unsigned, DIE *> InlineInfoLabels;
183   ValueMap<MDNode *, SmallVector<InlineInfoLabels, 4> > InlineInfo;
184   SmallVector<MDNode *, 4> InlinedSPNodes;
185
186   /// CompileUnitOffsets - A vector of the offsets of the compile units. This is
187   /// used when calculating the "origin" of a concrete instance of an inlined
188   /// function.
189   DenseMap<CompileUnit *, unsigned> CompileUnitOffsets;
190
191   /// DebugTimer - Timer for the Dwarf debug writer.
192   Timer *DebugTimer;
193   
194   struct FunctionDebugFrameInfo {
195     unsigned Number;
196     std::vector<MachineMove> Moves;
197
198     FunctionDebugFrameInfo(unsigned Num, const std::vector<MachineMove> &M)
199       : Number(Num), Moves(M) {}
200   };
201
202   std::vector<FunctionDebugFrameInfo> DebugFrames;
203
204   /// getSourceDirectoryAndFileIds - Return the directory and file ids that
205   /// maps to the source id. Source id starts at 1.
206   std::pair<unsigned, unsigned>
207   getSourceDirectoryAndFileIds(unsigned SId) const {
208     return SourceIds[SId-1];
209   }
210
211   /// getNumSourceDirectories - Return the number of source directories in the
212   /// debug info.
213   unsigned getNumSourceDirectories() const {
214     return DirectoryNames.size();
215   }
216
217   /// getSourceDirectoryName - Return the name of the directory corresponding
218   /// to the id.
219   const std::string &getSourceDirectoryName(unsigned Id) const {
220     return DirectoryNames[Id - 1];
221   }
222
223   /// getSourceFileName - Return the name of the source file corresponding
224   /// to the id.
225   const std::string &getSourceFileName(unsigned Id) const {
226     return SourceFileNames[Id - 1];
227   }
228
229   /// getNumSourceIds - Return the number of unique source ids.
230   unsigned getNumSourceIds() const {
231     return SourceIds.size();
232   }
233
234   /// assignAbbrevNumber - Define a unique number for the abbreviation.
235   ///
236   void assignAbbrevNumber(DIEAbbrev &Abbrev);
237
238   /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug
239   /// information entry.
240   DIEEntry *createDIEEntry(DIE *Entry = NULL);
241
242   /// addUInt - Add an unsigned integer attribute data and value.
243   ///
244   void addUInt(DIE *Die, unsigned Attribute, unsigned Form, uint64_t Integer);
245
246   /// addSInt - Add an signed integer attribute data and value.
247   ///
248   void addSInt(DIE *Die, unsigned Attribute, unsigned Form, int64_t Integer);
249
250   /// addString - Add a string attribute data and value.
251   ///
252   void addString(DIE *Die, unsigned Attribute, unsigned Form,
253                  const StringRef Str);
254
255   /// addLabel - Add a Dwarf label attribute data and value.
256   ///
257   void addLabel(DIE *Die, unsigned Attribute, unsigned Form,
258                 const DWLabel &Label);
259
260   /// addObjectLabel - Add an non-Dwarf label attribute data and value.
261   ///
262   void addObjectLabel(DIE *Die, unsigned Attribute, unsigned Form,
263                       const std::string &Label);
264
265   /// addSectionOffset - Add a section offset label attribute data and value.
266   ///
267   void addSectionOffset(DIE *Die, unsigned Attribute, unsigned Form,
268                         const DWLabel &Label, const DWLabel &Section,
269                         bool isEH = false, bool useSet = true);
270
271   /// addDelta - Add a label delta attribute data and value.
272   ///
273   void addDelta(DIE *Die, unsigned Attribute, unsigned Form,
274                 const DWLabel &Hi, const DWLabel &Lo);
275
276   /// addDIEEntry - Add a DIE attribute data and value.
277   ///
278   void addDIEEntry(DIE *Die, unsigned Attribute, unsigned Form, DIE *Entry) {
279     Die->addValue(Attribute, Form, createDIEEntry(Entry));
280   }
281
282   /// addBlock - Add block data.
283   ///
284   void addBlock(DIE *Die, unsigned Attribute, unsigned Form, DIEBlock *Block);
285
286   /// addSourceLine - Add location information to specified debug information
287   /// entry.
288   void addSourceLine(DIE *Die, const DIVariable *V);
289   void addSourceLine(DIE *Die, const DIGlobal *G);
290   void addSourceLine(DIE *Die, const DISubprogram *SP);
291   void addSourceLine(DIE *Die, const DIType *Ty);
292
293   /// addAddress - Add an address attribute to a die based on the location
294   /// provided.
295   void addAddress(DIE *Die, unsigned Attribute,
296                   const MachineLocation &Location);
297
298   /// addComplexAddress - Start with the address based on the location provided,
299   /// and generate the DWARF information necessary to find the actual variable
300   /// (navigating the extra location information encoded in the type) based on
301   /// the starting location.  Add the DWARF information to the die.
302   ///
303   void addComplexAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
304                          const MachineLocation &Location);
305
306   // FIXME: Should be reformulated in terms of addComplexAddress.
307   /// addBlockByrefAddress - Start with the address based on the location
308   /// provided, and generate the DWARF information necessary to find the
309   /// actual Block variable (navigating the Block struct) based on the
310   /// starting location.  Add the DWARF information to the die.  Obsolete,
311   /// please use addComplexAddress instead.
312   ///
313   void addBlockByrefAddress(DbgVariable *&DV, DIE *Die, unsigned Attribute,
314                             const MachineLocation &Location);
315
316   /// addType - Add a new type attribute to the specified entity.
317   void addType(CompileUnit *DW_Unit, DIE *Entity, DIType Ty);
318
319   void addPubTypes(DISubprogram SP);
320
321   /// constructTypeDIE - Construct basic type die from DIBasicType.
322   void constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
323                         DIBasicType BTy);
324
325   /// constructTypeDIE - Construct derived type die from DIDerivedType.
326   void constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
327                         DIDerivedType DTy);
328
329   /// constructTypeDIE - Construct type DIE from DICompositeType.
330   void constructTypeDIE(CompileUnit *DW_Unit, DIE &Buffer,
331                         DICompositeType CTy);
332
333   /// constructSubrangeDIE - Construct subrange DIE from DISubrange.
334   void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy);
335
336   /// constructArrayTypeDIE - Construct array type DIE from DICompositeType.
337   void constructArrayTypeDIE(CompileUnit *DW_Unit, DIE &Buffer, 
338                              DICompositeType *CTy);
339
340   /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator.
341   DIE *constructEnumTypeDIE(CompileUnit *DW_Unit, DIEnumerator *ETy);
342
343   /// createGlobalVariableDIE - Create new DIE using GV.
344   DIE *createGlobalVariableDIE(CompileUnit *DW_Unit,
345                                const DIGlobalVariable &GV);
346
347   /// createMemberDIE - Create new member DIE.
348   DIE *createMemberDIE(CompileUnit *DW_Unit, const DIDerivedType &DT);
349
350   /// createSubprogramDIE - Create new DIE using SP.
351   DIE *createSubprogramDIE(CompileUnit *DW_Unit, const DISubprogram &SP);
352
353   /// createMemberSubprogramDIE - Create new member DIE using SP. This
354   /// routine always returns a die with DW_AT_declaration attribute.
355
356   DIE *createMemberSubprogramDIE(CompileUnit *DW_Unit, const DISubprogram &SP);
357
358   /// createRawSubprogramDIE - Create new partially incomplete DIE. This is
359   /// a helper routine used by createMemberSubprogramDIE and 
360   /// createSubprogramDIE.
361   DIE *createRawSubprogramDIE(CompileUnit *DW_Unit, const DISubprogram &SP);
362
363   /// findCompileUnit - Get the compile unit for the given descriptor. 
364   ///
365   CompileUnit &findCompileUnit(DICompileUnit Unit) const;
366
367   /// createDbgScopeVariable - Create a new scope variable.
368   ///
369   DIE *createDbgScopeVariable(DbgVariable *DV, CompileUnit *Unit);
370
371   /// getUpdatedDbgScope - Find or create DbgScope assicated with 
372   /// the instruction. Initialize scope and update scope hierarchy.
373   DbgScope *getUpdatedDbgScope(MDNode *N, const MachineInstr *MI, MDNode *InlinedAt);
374
375   /// createDbgScope - Create DbgScope for the scope.
376   void createDbgScope(MDNode *Scope, MDNode *InlinedAt);
377
378   DbgScope *getOrCreateAbstractScope(MDNode *N);
379
380   /// findAbstractVariable - Find abstract variable associated with Var.
381   DbgVariable *findAbstractVariable(DIVariable &Var, unsigned FrameIdx, 
382                                     DILocation &Loc);
383
384   /// updateSubprogramScopeDIE - Find DIE for the given subprogram and 
385   /// attach appropriate DW_AT_low_pc and DW_AT_high_pc attributes.
386   /// If there are global variables in this scope then create and insert
387   /// DIEs for these variables.
388   DIE *updateSubprogramScopeDIE(MDNode *SPNode);
389
390   /// constructLexicalScope - Construct new DW_TAG_lexical_block 
391   /// for this scope and attach DW_AT_low_pc/DW_AT_high_pc labels.
392   DIE *constructLexicalScopeDIE(DbgScope *Scope);
393
394   /// constructInlinedScopeDIE - This scope represents inlined body of
395   /// a function. Construct DIE to represent this concrete inlined copy
396   /// of the function.
397   DIE *constructInlinedScopeDIE(DbgScope *Scope);
398
399   /// constructVariableDIE - Construct a DIE for the given DbgVariable.
400   DIE *constructVariableDIE(DbgVariable *DV, DbgScope *S, CompileUnit *Unit);
401
402   /// constructScopeDIE - Construct a DIE for this scope.
403   DIE *constructScopeDIE(DbgScope *Scope);
404
405   /// emitInitial - Emit initial Dwarf declarations.  This is necessary for cc
406   /// tools to recognize the object file contains Dwarf information.
407   void emitInitial();
408
409   /// emitDIE - Recusively Emits a debug information entry.
410   ///
411   void emitDIE(DIE *Die);
412
413   /// computeSizeAndOffset - Compute the size and offset of a DIE.
414   ///
415   unsigned computeSizeAndOffset(DIE *Die, unsigned Offset, bool Last);
416
417   /// computeSizeAndOffsets - Compute the size and offset of all the DIEs.
418   ///
419   void computeSizeAndOffsets();
420
421   /// EmitDebugInfo / emitDebugInfoPerCU - Emit the debug info section.
422   ///
423   void emitDebugInfoPerCU(CompileUnit *Unit);
424
425   void emitDebugInfo();
426
427   /// emitAbbreviations - Emit the abbreviation section.
428   ///
429   void emitAbbreviations() const;
430
431   /// emitEndOfLineMatrix - Emit the last address of the section and the end of
432   /// the line matrix.
433   ///
434   void emitEndOfLineMatrix(unsigned SectionEnd);
435
436   /// emitDebugLines - Emit source line information.
437   ///
438   void emitDebugLines();
439
440   /// emitCommonDebugFrame - Emit common frame info into a debug frame section.
441   ///
442   void emitCommonDebugFrame();
443
444   /// emitFunctionDebugFrame - Emit per function frame info into a debug frame
445   /// section.
446   void emitFunctionDebugFrame(const FunctionDebugFrameInfo &DebugFrameInfo);
447
448   void emitDebugPubNamesPerCU(CompileUnit *Unit);
449
450   /// emitDebugPubNames - Emit visible names into a debug pubnames section.
451   ///
452   void emitDebugPubNames();
453
454   /// emitDebugPubTypes - Emit visible types into a debug pubtypes section.
455   ///
456   void emitDebugPubTypes();
457
458   /// emitDebugStr - Emit visible names into a debug str section.
459   ///
460   void emitDebugStr();
461
462   /// emitDebugLoc - Emit visible names into a debug loc section.
463   ///
464   void emitDebugLoc();
465
466   /// EmitDebugARanges - Emit visible names into a debug aranges section.
467   ///
468   void EmitDebugARanges();
469
470   /// emitDebugRanges - Emit visible names into a debug ranges section.
471   ///
472   void emitDebugRanges();
473
474   /// emitDebugMacInfo - Emit visible names into a debug macinfo section.
475   ///
476   void emitDebugMacInfo();
477
478   /// emitDebugInlineInfo - Emit inline info using following format.
479   /// Section Header:
480   /// 1. length of section
481   /// 2. Dwarf version number
482   /// 3. address size.
483   ///
484   /// Entries (one "entry" for each function that was inlined):
485   ///
486   /// 1. offset into __debug_str section for MIPS linkage name, if exists; 
487   ///   otherwise offset into __debug_str for regular function name.
488   /// 2. offset into __debug_str section for regular function name.
489   /// 3. an unsigned LEB128 number indicating the number of distinct inlining 
490   /// instances for the function.
491   /// 
492   /// The rest of the entry consists of a {die_offset, low_pc}  pair for each 
493   /// inlined instance; the die_offset points to the inlined_subroutine die in
494   /// the __debug_info section, and the low_pc is the starting address  for the
495   ///  inlining instance.
496   void emitDebugInlineInfo();
497
498   /// GetOrCreateSourceID - Look up the source id with the given directory and
499   /// source file names. If none currently exists, create a new id and insert it
500   /// in the SourceIds map. This can update DirectoryNames and SourceFileNames maps
501   /// as well.
502   unsigned GetOrCreateSourceID(StringRef DirName, StringRef FileName);
503
504   void constructCompileUnit(MDNode *N);
505
506   void constructGlobalVariableDIE(MDNode *N);
507
508   void constructSubprogramDIE(MDNode *N);
509
510   // FIXME: This should go away in favor of complex addresses.
511   /// Find the type the programmer originally declared the variable to be
512   /// and return that type.  Obsolete, use GetComplexAddrType instead.
513   ///
514   DIType getBlockByrefType(DIType Ty, std::string Name);
515
516 public:
517   //===--------------------------------------------------------------------===//
518   // Main entry points.
519   //
520   DwarfDebug(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T);
521   virtual ~DwarfDebug();
522
523   /// ShouldEmitDwarfDebug - Returns true if Dwarf debugging declarations should
524   /// be emitted.
525   bool ShouldEmitDwarfDebug() const { return shouldEmit; }
526
527   /// beginModule - Emit all Dwarf sections that should come prior to the
528   /// content.
529   void beginModule(Module *M, MachineModuleInfo *MMI);
530
531   /// endModule - Emit all Dwarf sections that should come after the content.
532   ///
533   void endModule();
534
535   /// beginFunction - Gather pre-function debug information.  Assumes being
536   /// emitted immediately after the function entry point.
537   void beginFunction(MachineFunction *MF);
538
539   /// endFunction - Gather and emit post-function debug information.
540   ///
541   void endFunction(MachineFunction *MF);
542
543   /// recordSourceLine - Records location information and associates it with a 
544   /// label. Returns a unique label ID used to generate a label and provide
545   /// correspondence to the source line list.
546   unsigned recordSourceLine(unsigned Line, unsigned Col, MDNode *Scope);
547
548   /// getSourceLineCount - Return the number of source lines in the debug
549   /// info.
550   unsigned getSourceLineCount() const {
551     return Lines.size();
552   }
553                             
554   /// getOrCreateSourceID - Public version of GetOrCreateSourceID. This can be
555   /// timed. Look up the source id with the given directory and source file
556   /// names. If none currently exists, create a new id and insert it in the
557   /// SourceIds map. This can update DirectoryNames and SourceFileNames maps as
558   /// well.
559   unsigned getOrCreateSourceID(const std::string &DirName,
560                                const std::string &FileName);
561
562   /// extractScopeInformation - Scan machine instructions in this function
563   /// and collect DbgScopes. Return true, if atleast one scope was found.
564   bool extractScopeInformation(MachineFunction *MF);
565
566   /// collectVariableInfo - Populate DbgScope entries with variables' info.
567   void collectVariableInfo();
568
569   /// beginScope - Process beginning of a scope starting at Label.
570   void beginScope(const MachineInstr *MI, unsigned Label);
571
572   /// endScope - Prcess end of a scope.
573   void endScope(const MachineInstr *MI);
574 };
575 } // End of namespace llvm
576
577 #endif