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