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 MachineFunction;
45 class MachineLocation;
54 //===----------------------------------------------------------------------===//
55 // DWLabel - Labels are used to track locations in the assembler file.
56 // Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
57 // category of label (Ex. location) and number is a value unique in that
61 const char *Tag; // Label category tag. Should always be
62 // a staticly declared C string.
63 unsigned Number; // Unique number.
65 DWLabel(const char *T, unsigned N) : Tag(T), Number(N) {}
68 //===----------------------------------------------------------------------===//
69 // DwarfWriter - Emits Dwarf debug and exception handling directives.
74 //===--------------------------------------------------------------------===//
75 // Core attributes used by the Dwarf writer.
79 /// O - Stream to .s file.
83 /// Asm - Target of Dwarf emission.
90 /// RI - Register Information.
91 const MRegisterInfo *RI;
93 /// M - Current module.
97 /// MF - Current machine function.
101 /// DebugInfo - Collected debug information.
103 MachineDebugInfo *DebugInfo;
105 /// didInitial - Flag to indicate if initial emission has been done.
109 /// SubprogramCount - The running count of functions being compiled.
111 unsigned SubprogramCount;
113 //===--------------------------------------------------------------------===//
114 // Attributes used to construct specific Dwarf sections.
117 /// CompileUnits - All the compile units involved in this build. The index
118 /// of each entry in this vector corresponds to the sources in DebugInfo.
119 std::vector<CompileUnit *> CompileUnits;
121 /// Abbreviations - A UniqueVector of TAG structure abbreviations.
123 UniqueVector<DIEAbbrev> Abbreviations;
125 /// StringPool - A UniqueVector of strings used by indirect references.
126 /// UnitMap - Map debug information descriptor to compile unit.
128 UniqueVector<std::string> StringPool;
130 /// UnitMap - Map debug information descriptor to compile unit.
132 std::map<DebugInfoDesc *, CompileUnit *> DescToUnitMap;
134 /// DescToDieMap - Tracks the mapping of top level debug informaton
135 /// descriptors to debug information entries.
136 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
138 /// TypeToDieMap - Type to DIEType map.
140 // FIXME - Should not be needed.
141 std::map<Type *, DIE *> TypeToDieMap;
143 //===--------------------------------------------------------------------===//
144 // Properties to be set by the derived class ctor, used to configure the
148 /// AddressSize - Size of addresses used in file.
150 unsigned AddressSize;
152 /// hasLEB128 - True if target asm supports leb128 directives.
154 bool hasLEB128; /// Defaults to false.
156 /// hasDotLoc - True if target asm supports .loc directives.
158 bool hasDotLoc; /// Defaults to false.
160 /// hasDotFile - True if target asm supports .file directives.
162 bool hasDotFile; /// Defaults to false.
164 /// needsSet - True if target asm can't compute addresses on data
166 bool needsSet; /// Defaults to false.
168 /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
170 const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
172 /// DwarfInfoSection - Section directive for Dwarf info.
174 const char *DwarfInfoSection; /// Defaults to ".debug_info".
176 /// DwarfLineSection - Section directive for Dwarf info.
178 const char *DwarfLineSection; /// Defaults to ".debug_line".
180 /// DwarfFrameSection - Section directive for Dwarf info.
182 const char *DwarfFrameSection; /// Defaults to ".debug_frame".
184 /// DwarfPubNamesSection - Section directive for Dwarf info.
186 const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
188 /// DwarfPubTypesSection - Section directive for Dwarf info.
190 const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
192 /// DwarfStrSection - Section directive for Dwarf info.
194 const char *DwarfStrSection; /// Defaults to ".debug_str".
196 /// DwarfLocSection - Section directive for Dwarf info.
198 const char *DwarfLocSection; /// Defaults to ".debug_loc".
200 /// DwarfARangesSection - Section directive for Dwarf info.
202 const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
204 /// DwarfRangesSection - Section directive for Dwarf info.
206 const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
208 /// DwarfMacInfoSection - Section directive for Dwarf info.
210 const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
212 /// TextSection - Section directive for standard text.
214 const char *TextSection; /// Defaults to ".text".
216 /// DataSection - Section directive for standard data.
218 const char *DataSection; /// Defaults to ".data".
220 //===--------------------------------------------------------------------===//
221 // Emission and print routines
225 /// getAddressSize - Return the size of a target address in bytes.
227 unsigned getAddressSize() const { return AddressSize; }
229 /// PrintHex - Print a value as a hexidecimal value.
231 void PrintHex(int Value) const;
233 /// EOL - Print a newline character to asm stream. If a comment is present
234 /// then it will be printed first. Comments should not contain '\n'.
235 void EOL(const std::string &Comment) const;
237 /// EmitAlign - Print a align directive.
239 void EmitAlign(unsigned Alignment) const;
241 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
242 /// unsigned leb128 value.
243 void EmitULEB128Bytes(unsigned Value) const;
245 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
246 /// signed leb128 value.
247 void EmitSLEB128Bytes(int Value) const;
249 /// PrintULEB128 - Print a series of hexidecimal values (separated by
250 /// commas) representing an unsigned leb128 value.
251 void PrintULEB128(unsigned Value) const;
253 /// SizeULEB128 - Compute the number of bytes required for an unsigned
255 static unsigned SizeULEB128(unsigned Value);
257 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
258 /// commas) representing a signed leb128 value.
259 void PrintSLEB128(int Value) const;
261 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
263 static unsigned SizeSLEB128(int Value);
265 /// EmitInt8 - Emit a byte directive and value.
267 void EmitInt8(int Value) const;
269 /// EmitInt16 - Emit a short directive and value.
271 void EmitInt16(int Value) const;
273 /// EmitInt32 - Emit a long directive and value.
275 void EmitInt32(int Value) const;
277 /// EmitInt64 - Emit a long long directive and value.
279 void EmitInt64(uint64_t Value) const;
281 /// EmitString - Emit a string with quotes and a null terminator.
282 /// Special characters are emitted properly.
283 /// \literal (Eg. '\t') \endliteral
284 void EmitString(const std::string &String) const;
286 /// PrintLabelName - Print label name in form used by Dwarf writer.
288 void PrintLabelName(DWLabel Label) const {
289 PrintLabelName(Label.Tag, Label.Number);
291 void PrintLabelName(const char *Tag, unsigned Number) const;
293 /// EmitLabel - Emit location label for internal use by Dwarf.
295 void EmitLabel(DWLabel Label) const {
296 EmitLabel(Label.Tag, Label.Number);
298 void EmitLabel(const char *Tag, unsigned Number) const;
300 /// EmitReference - Emit a reference to a label.
302 void EmitReference(DWLabel Label) const {
303 EmitReference(Label.Tag, Label.Number);
305 void EmitReference(const char *Tag, unsigned Number) const;
306 void EmitReference(const std::string &Name) const;
308 /// EmitDifference - Emit the difference between two labels. Some
309 /// assemblers do not behave with absolute expressions with data directives,
310 /// so there is an option (needsSet) to use an intermediary set expression.
311 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
312 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
314 void EmitDifference(const char *TagHi, unsigned NumberHi,
315 const char *TagLo, unsigned NumberLo) const;
317 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
319 unsigned NewAbbreviation(DIEAbbrev *Abbrev);
321 /// NewString - Add a string to the constant pool and returns a label.
323 DWLabel NewString(const std::string &String);
325 /// getDieMapSlotFor - Returns the debug information entry map slot for the
326 /// specified debug descriptor.
327 DIE *&getDieMapSlotFor(DebugInfoDesc *DD);
331 /// AddSourceLine - Add location information to specified debug information
333 void AddSourceLine(DIE *Die, CompileUnitDesc *File, unsigned Line);
335 /// AddAddress - Add an address attribute to a die based on the location
337 void AddAddress(DIE *Die, unsigned Attribute,
338 const MachineLocation &Location);
340 /// NewType - Create a new type DIE.
342 DIE *NewType(DIE *Context, TypeDesc *TyDesc, CompileUnit *Unit);
344 /// NewCompileUnit - Create new compile unit and it's die.
346 CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID);
348 /// FindCompileUnit - Get the compile unit for the given descriptor.
350 CompileUnit *FindCompileUnit(CompileUnitDesc *UnitDesc);
352 /// NewGlobalVariable - Make a new global variable DIE.
354 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
356 /// NewSubprogram - Add a new subprogram DIE.
358 DIE *NewSubprogram(SubprogramDesc *SPD);
360 /// NewScopeVariable - Create a new scope variable.
362 DIE *NewScopeVariable(DebugVariable *DV, CompileUnit *Unit);
364 /// ConstructScope - Construct the components of a scope.
366 void ConstructScope(DebugScope *ParentScope, DIE *ParentDie,
369 /// ConstructRootScope - Construct the scope for the subprogram.
371 void ConstructRootScope(DebugScope *RootScope);
373 /// EmitInitial - Emit initial Dwarf declarations.
375 void EmitInitial() const;
377 /// EmitDIE - Recusively Emits a debug information entry.
379 void EmitDIE(DIE *Die) const;
381 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
383 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset, bool Last);
385 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
387 void SizeAndOffsets();
389 /// EmitFrameMoves - Emit frame instructions to describe the layout of the
391 void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
392 std::vector<MachineMove *> &Moves);
394 /// EmitDebugInfo - Emit the debug info section.
396 void EmitDebugInfo() const;
398 /// EmitAbbreviations - Emit the abbreviation section.
400 void EmitAbbreviations() const;
402 /// EmitDebugLines - Emit source line information.
404 void EmitDebugLines() const;
406 /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
408 void EmitInitialDebugFrame();
410 /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
412 void EmitFunctionDebugFrame();
414 /// EmitDebugPubNames - Emit info into a debug pubnames section.
416 void EmitDebugPubNames();
418 /// EmitDebugStr - Emit info into a debug str section.
422 /// EmitDebugLoc - Emit info into a debug loc section.
426 /// EmitDebugARanges - Emit info into a debug aranges section.
428 void EmitDebugARanges();
430 /// EmitDebugRanges - Emit info into a debug ranges section.
432 void EmitDebugRanges();
434 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
436 void EmitDebugMacInfo();
438 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
440 void ConstructCompileUnitDIEs();
442 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
443 /// global variables.
444 void ConstructGlobalDIEs();
446 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
448 void ConstructSubprogramDIEs();
450 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
451 /// When called it also checks to see if debug info is newly available. if
452 /// so the initial Dwarf headers are emitted.
453 bool ShouldEmitDwarf();
457 DwarfWriter(std::ostream &OS, AsmPrinter *A);
458 virtual ~DwarfWriter();
460 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
461 /// created it. Set by the target AsmPrinter.
462 void SetDebugInfo(MachineDebugInfo *DI);
464 //===--------------------------------------------------------------------===//
465 // Main entry points.
468 /// BeginModule - Emit all Dwarf sections that should come prior to the
470 void BeginModule(Module *M);
472 /// EndModule - Emit all Dwarf sections that should come after the content.
476 /// BeginFunction - Gather pre-function debug information. Assumes being
477 /// emitted immediately after the function entry point.
478 void BeginFunction(MachineFunction *MF);
480 /// EndFunction - Gather and emit post-function debug information.
485 } // end llvm namespace