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