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;
42 class GlobalVariableDesc;
43 class MachineDebugInfo;
44 class MachineLocation;
45 class MachineFunction;
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
58 const char *Tag; // Label category tag. Should always be
59 // a staticly declared C string.
60 unsigned Number; // Unique number.
62 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
65 //===----------------------------------------------------------------------===//
66 // DwarfWriter - Emits Dwarf debug and exception handling directives.
71 //===--------------------------------------------------------------------===//
72 // Core attributes used by the Dwarf writer.
76 /// O - Stream to .s file.
80 /// Asm - Target of Dwarf emission.
84 /// M - Current module.
88 /// MF - Current machine function.
92 /// DebugInfo - Collected debug information.
94 MachineDebugInfo *DebugInfo;
96 /// didInitial - Flag to indicate if initial emission has been done.
100 /// SubprogramCount - The running count of functions being compiled.
102 unsigned SubprogramCount;
104 //===--------------------------------------------------------------------===//
105 // Attributes used to construct specific Dwarf sections.
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;
112 /// Abbreviations - A UniqueVector of TAG structure abbreviations.
114 UniqueVector<DIEAbbrev> Abbreviations;
116 /// StringPool - A UniqueVector of strings used by indirect references.
117 /// UnitMap - Map debug information descriptor to compile unit.
119 UniqueVector<std::string> StringPool;
121 /// UnitMap - Map debug information descriptor to compile unit.
123 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
125 /// DescToDieMap - Tracks the mapping of top level debug informaton
126 /// descriptors to debug information entries.
127 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
129 /// TypeToDieMap - Type to DIEType map.
131 // FIXME - Should not be needed.
132 std::map<Type *, DIE *> TypeToDieMap;
134 //===--------------------------------------------------------------------===//
135 // Properties to be set by the derived class ctor, used to configure the
139 /// AddressSize - Size of addresses used in file.
141 unsigned AddressSize;
143 /// hasLEB128 - True if target asm supports leb128 directives.
145 bool hasLEB128; /// Defaults to false.
147 /// hasDotLoc - True if target asm supports .loc directives.
149 bool hasDotLoc; /// Defaults to false.
151 /// hasDotFile - True if target asm supports .file directives.
153 bool hasDotFile; /// Defaults to false.
155 /// needsSet - True if target asm can't compute addresses on data
157 bool needsSet; /// Defaults to false.
159 /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
161 const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
163 /// DwarfInfoSection - Section directive for Dwarf info.
165 const char *DwarfInfoSection; /// Defaults to ".debug_info".
167 /// DwarfLineSection - Section directive for Dwarf info.
169 const char *DwarfLineSection; /// Defaults to ".debug_line".
171 /// DwarfFrameSection - Section directive for Dwarf info.
173 const char *DwarfFrameSection; /// Defaults to ".debug_frame".
175 /// DwarfPubNamesSection - Section directive for Dwarf info.
177 const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
179 /// DwarfPubTypesSection - Section directive for Dwarf info.
181 const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
183 /// DwarfStrSection - Section directive for Dwarf info.
185 const char *DwarfStrSection; /// Defaults to ".debug_str".
187 /// DwarfLocSection - Section directive for Dwarf info.
189 const char *DwarfLocSection; /// Defaults to ".debug_loc".
191 /// DwarfARangesSection - Section directive for Dwarf info.
193 const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
195 /// DwarfRangesSection - Section directive for Dwarf info.
197 const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
199 /// DwarfMacInfoSection - Section directive for Dwarf info.
201 const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
203 /// TextSection - Section directive for standard text.
205 const char *TextSection; /// Defaults to ".text".
207 /// DataSection - Section directive for standard data.
209 const char *DataSection; /// Defaults to ".data".
211 //===--------------------------------------------------------------------===//
212 // Emission and print routines
216 /// getAddressSize - Return the size of a target address in bytes.
218 unsigned getAddressSize() const { return AddressSize; }
220 /// PrintHex - Print a value as a hexidecimal value.
222 void PrintHex(int Value) const;
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;
228 /// EmitAlign - Print a align directive.
230 void EmitAlign(unsigned Alignment) const;
232 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
233 /// unsigned leb128 value.
234 void EmitULEB128Bytes(unsigned Value) const;
236 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
237 /// signed leb128 value.
238 void EmitSLEB128Bytes(int Value) const;
240 /// PrintULEB128 - Print a series of hexidecimal values (separated by
241 /// commas) representing an unsigned leb128 value.
242 void PrintULEB128(unsigned Value) const;
244 /// SizeULEB128 - Compute the number of bytes required for an unsigned
246 static unsigned SizeULEB128(unsigned Value);
248 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
249 /// commas) representing a signed leb128 value.
250 void PrintSLEB128(int Value) const;
252 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
254 static unsigned SizeSLEB128(int Value);
256 /// EmitInt8 - Emit a byte directive and value.
258 void EmitInt8(int Value) const;
260 /// EmitInt16 - Emit a short directive and value.
262 void EmitInt16(int Value) const;
264 /// EmitInt32 - Emit a long directive and value.
266 void EmitInt32(int Value) const;
268 /// EmitInt64 - Emit a long long directive and value.
270 void EmitInt64(uint64_t Value) const;
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;
276 /// PrintLabelName - Print label name in form used by Dwarf writer.
278 void PrintLabelName(DWLabel Label) const {
279 PrintLabelName(Label.Tag, Label.Number);
281 void PrintLabelName(const char *Tag, unsigned Number) const;
283 /// EmitLabel - Emit location label for internal use by Dwarf.
285 void EmitLabel(DWLabel Label) const {
286 EmitLabel(Label.Tag, Label.Number);
288 void EmitLabel(const char *Tag, unsigned Number) const;
290 /// EmitReference - Emit a reference to a label.
292 void EmitReference(DWLabel Label) const {
293 EmitReference(Label.Tag, Label.Number);
295 void EmitReference(const char *Tag, unsigned Number) const;
296 void EmitReference(const std::string &Name) const;
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);
304 void EmitDifference(const char *TagHi, unsigned NumberHi,
305 const char *TagLo, unsigned NumberLo) const;
307 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
309 unsigned NewAbbreviation(DIEAbbrev *Abbrev);
311 /// NewString - Add a string to the constant pool and returns a label.
313 DWLabel NewString(const std::string &String);
315 /// getDieMapSlotFor - Returns the debug information entry map slot for the
316 /// specified debug descriptor.
317 DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
321 /// AddSourceLine - Add location information to specified debug information
323 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
325 /// AddAddress - Add an address attribute to a die based on the location
327 void AddAddress(DIE *Die, unsigned Attribute, MachineLocation &Location);
329 /// NewType - Create a new type DIE.
331 DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
333 /// NewCompileUnit - Create new compile unit and it's die.
335 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
337 /// FindCompileUnit - Get the compile unit for the given descriptor.
339 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
341 /// NewGlobalVariable - Make a new global variable DIE.
343 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
345 /// NewSubprogram - Add a new subprogram DIE.
347 DIE *NewSubprogram(SubprogramDesc *SPD);
349 /// NewScopeVariable - Create a new scope variable.
351 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
353 /// ConstructScope - Construct the components of a scope.
355 void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
358 /// ConstructRootScope - Construct the scope for the subprogram.
360 void ConstructRootScope(DebugScope *RootScope);
362 /// EmitInitial - Emit initial Dwarf declarations.
364 void EmitInitial() const;
366 /// EmitDIE - Recusively Emits a debug information entry.
368 void EmitDIE(DIE *Die) const;
370 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
372 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
374 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
376 void SizeAndOffsets();
378 /// EmitDebugInfo - Emit the debug info section.
380 void EmitDebugInfo() const;
382 /// EmitAbbreviations - Emit the abbreviation section.
384 void EmitAbbreviations() const;
386 /// EmitDebugLines - Emit source line information.
388 void EmitDebugLines() const;
390 /// EmitDebugFrame - Emit info into a debug frame section.
392 void EmitDebugFrame();
394 /// EmitDebugPubNames - Emit info into a debug pubnames section.
396 void EmitDebugPubNames();
398 /// EmitDebugStr - Emit info into a debug str section.
402 /// EmitDebugLoc - Emit info into a debug loc section.
406 /// EmitDebugARanges - Emit info into a debug aranges section.
408 void EmitDebugARanges();
410 /// EmitDebugRanges - Emit info into a debug ranges section.
412 void EmitDebugRanges();
414 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
416 void EmitDebugMacInfo();
418 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
420 void ConstructCompileUnitDIEs();
422 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
423 /// global variables.
424 void ConstructGlobalDIEs();
426 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
428 void ConstructSubprogramDIEs();
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();
437 DwarfWriter(std::ostream &OS, AsmPrinter *A);
438 virtual ~DwarfWriter();
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; }
444 //===--------------------------------------------------------------------===//
445 // Main entry points.
448 /// BeginModule - Emit all Dwarf sections that should come prior to the
450 void BeginModule(Module *M);
452 /// EndModule - Emit all Dwarf sections that should come after the content.
456 /// BeginFunction - Gather pre-function debug information.
458 void BeginFunction(MachineFunction *MF);
460 /// EndFunction - Gather and emit post-function debug information.
465 } // end llvm namespace