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