Remove a bunch more SparcV9 specific stuff
[oota-llvm.git] / include / llvm / CodeGen / DwarfWriter.h
1 //===-- llvm/CodeGen/DwarfWriter.h - Dwarf Framework ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing Dwarf debug info into asm files.  For
11 // Details on the Dwarf 3 specfication see DWARF Debugging Information Format
12 // V.3 reference manual http://dwarf.freestandards.org ,
13 //
14 // The role of the Dwarf Writer class is to extract debug information from the
15 // MachineDebugInfo object, organize it in Dwarf form and then emit it into asm
16 // the current asm file using data and high level Dwarf directives.
17 // 
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CODEGEN_DWARFWRITER_H
21 #define LLVM_CODEGEN_DWARFWRITER_H
22
23 #include "llvm/ADT/UniqueVector.h"
24 #include "llvm/Support/DataTypes.h"
25
26 #include <iosfwd>
27 #include <string>
28
29
30 namespace llvm {
31
32 // Forward declarations.
33
34 class AsmPrinter;
35 class CompileUnit;
36 class CompileUnitDesc;
37 class DebugInfoDesc;
38 class DebugVariable;
39 class DebugScope;
40 class DIE;
41 class DIEAbbrev;
42 class GlobalVariableDesc;
43 class MachineDebugInfo;
44 class MachineFunction;
45 class MachineLocation;
46 class MachineMove;
47 class Module;
48 class MRegisterInfo;
49 class SubprogramDesc;
50 class TargetData;
51 class Type;
52 class TypeDesc;
53   
54 //===----------------------------------------------------------------------===//
55 // DWLabel - Labels are used to track locations in the assembler file.
56 // Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
57 // category of label (Ex. location) and number is a value unique in that
58 // category.
59 class DWLabel {
60 public:
61   const char *Tag;                    // Label category tag. Should always be
62                                       // a staticly declared C string.
63   unsigned    Number;                 // Unique number.
64
65   DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
66 };
67
68 //===----------------------------------------------------------------------===//
69 // DwarfWriter - Emits Dwarf debug and exception handling directives.
70 //
71 class DwarfWriter {
72 protected:
73
74   //===--------------------------------------------------------------------===//
75   // Core attributes used by the Dwarf  writer.
76   //
77   
78   //
79   /// O - Stream to .s file.
80   ///
81   std::ostream &O;
82
83   /// Asm - Target of Dwarf emission.
84   ///
85   AsmPrinter *Asm;
86   
87   /// TD - Target data.
88   const TargetData *TD;
89   
90   /// RI - Register Information.
91   const MRegisterInfo *RI;
92   
93   /// M - Current module.
94   ///
95   Module *M;
96   
97   /// MF - Current machine function.
98   ///
99   MachineFunction *MF;
100   
101   /// DebugInfo - Collected debug information.
102   ///
103   MachineDebugInfo *DebugInfo;
104   
105   /// didInitial - Flag to indicate if initial emission has been done.
106   ///
107   bool didInitial;
108   
109   /// SubprogramCount - The running count of functions being compiled.
110   ///
111   unsigned SubprogramCount;
112   
113   //===--------------------------------------------------------------------===//
114   // Attributes used to construct specific Dwarf sections.
115   //
116   
117   /// CompileUnits - All the compile units involved in this build.  The index
118   /// of each entry in this vector corresponds to the sources in DebugInfo.
119   std::vector<CompileUnit *> CompileUnits;
120
121   /// Abbreviations - A UniqueVector of TAG structure abbreviations.
122   ///
123   UniqueVector<DIEAbbrev> Abbreviations;
124   
125   /// StringPool - A UniqueVector of strings used by indirect references.
126   /// UnitMap - Map debug information descriptor to compile unit.
127    ///
128   UniqueVector<std::string> StringPool;
129
130   /// UnitMap - Map debug information descriptor to compile unit.
131   ///
132   std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
133   
134   /// DescToDieMap - Tracks the mapping of top level debug informaton
135   /// descriptors to debug information entries.
136   std::map<DebugInfoDesc *, DIE *> DescToDieMap;
137   
138   /// TypeToDieMap - Type to DIEType map.
139   ///
140   // FIXME - Should not be needed.
141   std::map<Type *, DIE *> TypeToDieMap;
142   
143   //===--------------------------------------------------------------------===//
144   // Properties to be set by the derived class ctor, used to configure the
145   // Dwarf writer.
146   //
147   
148   /// AddressSize - Size of addresses used in file.
149   ///
150   unsigned AddressSize;
151
152   /// hasLEB128 - True if target asm supports leb128 directives.
153   ///
154   bool hasLEB128; /// Defaults to false.
155   
156   /// hasDotLoc - True if target asm supports .loc directives.
157   ///
158   bool hasDotLoc; /// Defaults to false.
159   
160   /// hasDotFile - True if target asm supports .file directives.
161   ///
162   bool hasDotFile; /// Defaults to false.
163   
164   /// needsSet - True if target asm can't compute addresses on data
165   /// directives.
166   bool needsSet; /// Defaults to false.
167   
168   /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
169   ///
170   const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
171
172   /// DwarfInfoSection - Section directive for Dwarf info.
173   ///
174   const char *DwarfInfoSection; /// Defaults to ".debug_info".
175
176   /// DwarfLineSection - Section directive for Dwarf info.
177   ///
178   const char *DwarfLineSection; /// Defaults to ".debug_line".
179   
180   /// DwarfFrameSection - Section directive for Dwarf info.
181   ///
182   const char *DwarfFrameSection; /// Defaults to ".debug_frame".
183   
184   /// DwarfPubNamesSection - Section directive for Dwarf info.
185   ///
186   const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
187   
188   /// DwarfPubTypesSection - Section directive for Dwarf info.
189   ///
190   const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
191   
192   /// DwarfStrSection - Section directive for Dwarf info.
193   ///
194   const char *DwarfStrSection; /// Defaults to ".debug_str".
195
196   /// DwarfLocSection - Section directive for Dwarf info.
197   ///
198   const char *DwarfLocSection; /// Defaults to ".debug_loc".
199
200   /// DwarfARangesSection - Section directive for Dwarf info.
201   ///
202   const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
203
204   /// DwarfRangesSection - Section directive for Dwarf info.
205   ///
206   const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
207
208   /// DwarfMacInfoSection - Section directive for Dwarf info.
209   ///
210   const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
211
212   /// TextSection - Section directive for standard text.
213   ///
214   const char *TextSection; /// Defaults to ".text".
215   
216   /// DataSection - Section directive for standard data.
217   ///
218   const char *DataSection; /// Defaults to ".data".
219
220   //===--------------------------------------------------------------------===//
221   // Emission and print routines
222   //
223
224 public:
225   /// getAddressSize - Return the size of a target address in bytes.
226   ///
227   unsigned getAddressSize() const { return AddressSize; }
228
229   /// PrintHex - Print a value as a hexidecimal value.
230   ///
231   void PrintHex(int Value) const;
232
233   /// EOL - Print a newline character to asm stream.  If a comment is present
234   /// then it will be printed first.  Comments should not contain '\n'.
235   void EOL(const std::string &Comment) const;
236   
237   /// EmitAlign - Print a align directive.
238   ///
239   void EmitAlign(unsigned Alignment) const;
240                                         
241   /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
242   /// unsigned leb128 value.
243   void EmitULEB128Bytes(unsigned Value) const;
244   
245   /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
246   /// signed leb128 value.
247   void EmitSLEB128Bytes(int Value) const;
248   
249   /// PrintULEB128 - Print a series of hexidecimal values (separated by
250   /// commas) representing an unsigned leb128 value.
251   void PrintULEB128(unsigned Value) const;
252
253   /// SizeULEB128 - Compute the number of bytes required for an unsigned
254   /// leb128 value.
255   static unsigned SizeULEB128(unsigned Value);
256   
257   /// PrintSLEB128 - Print a series of hexidecimal values (separated by
258   /// commas) representing a signed leb128 value.
259   void PrintSLEB128(int Value) const;
260   
261   /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
262   /// value.
263   static unsigned SizeSLEB128(int Value);
264   
265   /// EmitInt8 - Emit a byte directive and value.
266   ///
267   void EmitInt8(int Value) const;
268
269   /// EmitInt16 - Emit a short directive and value.
270   ///
271   void EmitInt16(int Value) const;
272
273   /// EmitInt32 - Emit a long directive and value.
274   ///
275   void EmitInt32(int Value) const;
276   
277   /// EmitInt64 - Emit a long long directive and value.
278   ///
279   void EmitInt64(uint64_t Value) const;
280   
281   /// EmitString - Emit a string with quotes and a null terminator.
282   /// Special characters are emitted properly. (Eg. '\t')
283   void EmitString(const std::string &String) const;
284
285   /// PrintLabelName - Print label name in form used by Dwarf writer.
286   ///
287   void PrintLabelName(DWLabel Label) const {
288     PrintLabelName(Label.Tag, Label.Number);
289   }
290   void PrintLabelName(const char *Tag, unsigned Number) const;
291   
292   /// EmitLabel - Emit location label for internal use by Dwarf.
293   ///
294   void EmitLabel(DWLabel Label) const {
295     EmitLabel(Label.Tag, Label.Number);
296   }
297   void EmitLabel(const char *Tag, unsigned Number) const;
298   
299   /// EmitReference - Emit a reference to a label.
300   ///
301   void EmitReference(DWLabel Label) const {
302     EmitReference(Label.Tag, Label.Number);
303   }
304   void EmitReference(const char *Tag, unsigned Number) const;
305   void EmitReference(const std::string &Name) const;
306
307   /// EmitDifference - Emit the difference between two labels.  Some
308   /// assemblers do not behave with absolute expressions with data directives,
309   /// so there is an option (needsSet) to use an intermediary set expression.
310   void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
311     EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
312   }
313   void EmitDifference(const char *TagHi, unsigned NumberHi,
314                       const char *TagLo, unsigned NumberLo) const;
315                       
316   /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
317   ///  
318   unsigned NewAbbreviation(DIEAbbrev *Abbrev);
319   
320   /// NewString - Add a string to the constant pool and returns a label.
321   ///
322   DWLabel NewString(const std::string &String);
323   
324   /// getDieMapSlotFor - Returns the debug information entry map slot for the
325   /// specified debug descriptor.
326   DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
327                                  
328 private:
329
330   /// AddSourceLine - Add location information to specified debug information
331   /// entry. 
332   void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
333
334   /// AddAddress - Add an address attribute to a die based on the location
335   /// provided.
336   void AddAddress(DIE *Die, unsigned Attribute,
337                   const MachineLocation &Location);
338
339   /// NewType - Create a new type DIE.
340   ///
341   DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
342   
343   /// NewCompileUnit - Create new compile unit and it's die.
344   ///
345   CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
346   
347   /// FindCompileUnit - Get the compile unit for the given descriptor.
348   ///
349   CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
350   
351   /// NewGlobalVariable - Make a new global variable DIE.
352   ///
353   DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
354
355   /// NewSubprogram - Add a new subprogram DIE.
356   ///
357   DIE *NewSubprogram(SubprogramDesc *SPD);
358
359   /// NewScopeVariable - Create a new scope variable.
360   ///
361   DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
362
363   /// ConstructScope - Construct the components of a scope.
364   ///
365   void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
366                       CompileUnit *Unit);
367
368   /// ConstructRootScope - Construct the scope for the subprogram.
369   ///
370   void ConstructRootScope(DebugScope *RootScope);
371
372   /// EmitInitial - Emit initial Dwarf declarations.
373   ///
374   void EmitInitial() const;
375   
376   /// EmitDIE - Recusively Emits a debug information entry.
377   ///
378   void EmitDIE(DIE *Die) const;
379   
380   /// SizeAndOffsetDie - Compute the size and offset of a DIE.
381   ///
382   unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
383
384   /// SizeAndOffsets - Compute the size and offset of all the DIEs.
385   ///
386   void SizeAndOffsets();
387   
388   /// EmitFrameMoves - Emit frame instructions to describe the layout of the
389   /// frame.
390   void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
391                       std::vector<MachineMove *> &Moves);
392
393   /// EmitDebugInfo - Emit the debug info section.
394   ///
395   void EmitDebugInfo() const;
396   
397   /// EmitAbbreviations - Emit the abbreviation section.
398   ///
399   void EmitAbbreviations() const;
400   
401   /// EmitDebugLines - Emit source line information.
402   ///
403   void EmitDebugLines() const;
404
405   /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
406   ///
407   void EmitInitialDebugFrame();
408     
409   /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
410   /// section.
411   void EmitFunctionDebugFrame();
412
413   /// EmitDebugPubNames - Emit info into a debug pubnames section.
414   ///
415   void EmitDebugPubNames();
416   
417   /// EmitDebugStr - Emit info into a debug str section.
418   ///
419   void EmitDebugStr();
420   
421   /// EmitDebugLoc - Emit info into a debug loc section.
422   ///
423   void EmitDebugLoc();
424   
425   /// EmitDebugARanges - Emit info into a debug aranges section.
426   ///
427   void EmitDebugARanges();
428   
429   /// EmitDebugRanges - Emit info into a debug ranges section.
430   ///
431   void EmitDebugRanges();
432   
433   /// EmitDebugMacInfo - Emit info into a debug macinfo section.
434   ///
435   void EmitDebugMacInfo();
436   
437   /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
438   /// header file.
439   void ConstructCompileUnitDIEs();
440   
441   /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
442   /// global variables.
443   void ConstructGlobalDIEs();
444
445   /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
446   /// subprograms.
447   void ConstructSubprogramDIEs();
448
449   /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
450   /// When called it also checks to see if debug info is newly available.  if
451   /// so the initial Dwarf headers are emitted.
452   bool ShouldEmitDwarf();
453
454 public:
455   
456   DwarfWriter(std::ostream &OS, AsmPrinter *A);
457   virtual ~DwarfWriter();
458   
459   /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
460   /// created it.  Set by the target AsmPrinter.
461   void SetDebugInfo(MachineDebugInfo *DI);
462
463   //===--------------------------------------------------------------------===//
464   // Main entry points.
465   //
466   
467   /// BeginModule - Emit all Dwarf sections that should come prior to the
468   /// content.
469   void BeginModule(Module *M);
470   
471   /// EndModule - Emit all Dwarf sections that should come after the content.
472   ///
473   void EndModule();
474   
475   /// BeginFunction - Gather pre-function debug information.  Assumes being 
476   /// emitted immediately after the function entry point.
477   void BeginFunction(MachineFunction *MF);
478   
479   /// EndFunction - Gather and emit post-function debug information.
480   ///
481   void EndFunction();
482 };
483
484 } // end llvm namespace
485
486 #endif