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