1 //===-- llvm/CodeGen/DwarfWriter.h - Dwarf Framework ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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 ,
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.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CODEGEN_DWARFWRITER_H
21 #define LLVM_CODEGEN_DWARFWRITER_H
23 #include "llvm/ADT/UniqueVector.h"
24 #include "llvm/Support/DataTypes.h"
32 // Forward declarations.
36 class CompileUnitDesc;
40 class GlobalVariableDesc;
41 class MachineDebugInfo;
42 class MachineFunction;
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
55 const char *Tag; // Label category tag. Should always be
56 // a staticly declared C string.
57 unsigned Number; // Unique number.
59 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
62 //===----------------------------------------------------------------------===//
63 // DwarfWriter - Emits Dwarf debug and exception handling directives.
68 //===--------------------------------------------------------------------===//
69 // Core attributes used by the Dwarf writer.
73 /// O - Stream to .s file.
77 /// Asm - Target of Dwarf emission.
81 /// DebugInfo - Collected debug information.
83 MachineDebugInfo *DebugInfo;
85 /// didInitial - Flag to indicate if initial emission has been done.
89 //===--------------------------------------------------------------------===//
90 // Attributes used to construct specific Dwarf sections.
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;
97 /// Abbreviations - A UniqueVector of TAG structure abbreviations.
99 UniqueVector<DIEAbbrev> Abbreviations;
101 /// StringPool - A UniqueVector of strings used by indirect references.
102 /// UnitMap - Map debug information descriptor to compile unit.
104 UniqueVector<std::string> StringPool;
106 /// UnitMap - Map debug information descriptor to compile unit.
108 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
110 /// DescToDieMap - Tracks the mapping of debug informaton descriptors to
112 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
114 /// TypeToDieMap - Type to DIEType map.
116 // FIXME - Should not be needed.
117 std::map<Type *, DIE *> TypeToDieMap;
119 //===--------------------------------------------------------------------===//
120 // Properties to be set by the derived class ctor, used to configure the
124 /// AddressSize - Size of addresses used in file.
126 unsigned AddressSize;
128 /// hasLEB128 - True if target asm supports leb128 directives.
130 bool hasLEB128; /// Defaults to false.
132 /// hasDotLoc - True if target asm supports .loc directives.
134 bool hasDotLoc; /// Defaults to false.
136 /// hasDotFile - True if target asm supports .file directives.
138 bool hasDotFile; /// Defaults to false.
140 /// needsSet - True if target asm can't compute addresses on data
142 bool needsSet; /// Defaults to false.
144 /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
146 const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
148 /// DwarfInfoSection - Section directive for Dwarf info.
150 const char *DwarfInfoSection; /// Defaults to ".debug_info".
152 /// DwarfLineSection - Section directive for Dwarf info.
154 const char *DwarfLineSection; /// Defaults to ".debug_line".
156 /// DwarfFrameSection - Section directive for Dwarf info.
158 const char *DwarfFrameSection; /// Defaults to ".debug_frame".
160 /// DwarfPubNamesSection - Section directive for Dwarf info.
162 const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
164 /// DwarfPubTypesSection - Section directive for Dwarf info.
166 const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
168 /// DwarfStrSection - Section directive for Dwarf info.
170 const char *DwarfStrSection; /// Defaults to ".debug_str".
172 /// DwarfLocSection - Section directive for Dwarf info.
174 const char *DwarfLocSection; /// Defaults to ".debug_loc".
176 /// DwarfARangesSection - Section directive for Dwarf info.
178 const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
180 /// DwarfRangesSection - Section directive for Dwarf info.
182 const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
184 /// DwarfMacInfoSection - Section directive for Dwarf info.
186 const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
188 /// TextSection - Section directive for standard text.
190 const char *TextSection; /// Defaults to ".text".
192 /// DataSection - Section directive for standard data.
194 const char *DataSection; /// Defaults to ".data".
196 //===--------------------------------------------------------------------===//
197 // Emission and print routines
201 /// getAddressSize - Return the size of a target address in bytes.
203 unsigned getAddressSize() const { return AddressSize; }
205 /// PrintHex - Print a value as a hexidecimal value.
207 void PrintHex(int Value) const;
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;
213 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
214 /// unsigned leb128 value.
215 void EmitULEB128Bytes(unsigned Value) const;
217 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
218 /// signed leb128 value.
219 void EmitSLEB128Bytes(int Value) const;
221 /// PrintULEB128 - Print a series of hexidecimal values (separated by
222 /// commas) representing an unsigned leb128 value.
223 void PrintULEB128(unsigned Value) const;
225 /// SizeULEB128 - Compute the number of bytes required for an unsigned
227 static unsigned SizeULEB128(unsigned Value);
229 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
230 /// commas) representing a signed leb128 value.
231 void PrintSLEB128(int Value) const;
233 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
235 static unsigned SizeSLEB128(int Value);
237 /// EmitInt8 - Emit a byte directive and value.
239 void EmitInt8(int Value) const;
241 /// EmitInt16 - Emit a short directive and value.
243 void EmitInt16(int Value) const;
245 /// EmitInt32 - Emit a long directive and value.
247 void EmitInt32(int Value) const;
249 /// EmitInt64 - Emit a long long directive and value.
251 void EmitInt64(uint64_t Value) const;
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;
257 /// PrintLabelName - Print label name in form used by Dwarf writer.
259 void PrintLabelName(DWLabel Label) const {
260 PrintLabelName(Label.Tag, Label.Number);
262 void PrintLabelName(const char *Tag, unsigned Number) const;
264 /// EmitLabel - Emit location label for internal use by Dwarf.
266 void EmitLabel(DWLabel Label) const {
267 EmitLabel(Label.Tag, Label.Number);
269 void EmitLabel(const char *Tag, unsigned Number) const;
271 /// EmitReference - Emit a reference to a label.
273 void EmitReference(DWLabel Label) const {
274 EmitReference(Label.Tag, Label.Number);
276 void EmitReference(const char *Tag, unsigned Number) const;
277 void EmitReference(const std::string &Name) const;
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);
285 void EmitDifference(const char *TagHi, unsigned NumberHi,
286 const char *TagLo, unsigned NumberLo) const;
288 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
290 unsigned NewAbbreviation(DIEAbbrev *Abbrev);
292 /// NewString - Add a string to the constant pool and returns a label.
294 DWLabel NewString(const std::string &String);
296 /// NewBasicType - Creates a new basic type if necessary, then adds to the
298 /// FIXME - Should never be needed.
299 DIE *NewBasicType(DIE *Context, Type *Ty);
303 /// NewType - Create a new type DIE.
305 DIE *NewType(DIE *Context, TypeDesc *TyDesc);
307 /// NewCompileUnit - Create new compile unit and it's die.
309 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
311 /// FindCompileUnit - Get the compile unit for the given descriptor.
313 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
315 /// NewGlobalVariable - Make a new global variable DIE.
317 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
319 /// NewSubprogram - Add a new subprogram DIE.
321 DIE *NewSubprogram(SubprogramDesc *SPD);
323 /// EmitInitial - Emit initial Dwarf declarations.
325 void EmitInitial() const;
327 /// EmitDIE - Recusively Emits a debug information entry.
329 void EmitDIE(DIE *Die) const;
331 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
333 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset);
335 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
337 void SizeAndOffsets();
339 /// EmitDebugInfo - Emit the debug info section.
341 void EmitDebugInfo() const;
343 /// EmitAbbreviations - Emit the abbreviation section.
345 void EmitAbbreviations() const;
347 /// EmitDebugLines - Emit source line information.
349 void EmitDebugLines() const;
351 /// EmitDebugFrame - Emit info into a debug frame section.
353 void EmitDebugFrame();
355 /// EmitDebugPubNames - Emit info into a debug pubnames section.
357 void EmitDebugPubNames();
359 /// EmitDebugStr - Emit info into a debug str section.
363 /// EmitDebugLoc - Emit info into a debug loc section.
367 /// EmitDebugARanges - Emit info into a debug aranges section.
369 void EmitDebugARanges();
371 /// EmitDebugRanges - Emit info into a debug ranges section.
373 void EmitDebugRanges();
375 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
377 void EmitDebugMacInfo();
379 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
381 void ConstructCompileUnitDIEs();
383 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
384 /// global variables.
385 void ConstructGlobalDIEs(Module &M);
387 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
389 void ConstructSubprogramDIEs(Module &M);
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();
398 DwarfWriter(std::ostream &OS, AsmPrinter *A);
399 virtual ~DwarfWriter();
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; }
405 //===--------------------------------------------------------------------===//
406 // Main entry points.
409 /// BeginModule - Emit all Dwarf sections that should come prior to the
411 void BeginModule(Module &M);
413 /// EndModule - Emit all Dwarf sections that should come after the content.
415 void EndModule(Module &M);
417 /// BeginFunction - Gather pre-function debug information.
419 void BeginFunction(MachineFunction &MF);
421 /// EndFunction - Gather and emit post-function debug information.
423 void EndFunction(MachineFunction &MF);
426 } // end llvm namespace