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