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