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