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