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