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.
35 class CompileUnitDesc;
39 class GlobalVariableDesc;
40 class MachineDebugInfo;
41 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<DIE *> CompileUnits;
97 /// Abbreviations - A UniqueVector of TAG structure abbreviations.
99 UniqueVector<DIEAbbrev> Abbreviations;
101 /// GlobalTypes - A map of globally visible named types.
103 std::map<std::string, DIE *> GlobalTypes;
105 /// GlobalEntities - A map of globally visible named entities.
107 std::map<std::string, DIE *> GlobalEntities;
109 /// StringPool - A UniqueVector of strings used by indirect references.
111 UniqueVector<std::string> StringPool;
113 /// DescToDieMap - Tracks the mapping of debug informaton descriptors to
115 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
117 /// TypeToDieMap - Type to DIEType map.
119 // FIXME - Should not be needed.
120 std::map<Type *, DIE *> TypeToDieMap;
122 //===--------------------------------------------------------------------===//
123 // Properties to be set by the derived class ctor, used to configure the
127 /// AddressSize - Size of addresses used in file.
129 unsigned AddressSize;
131 /// hasLEB128 - True if target asm supports leb128 directives.
133 bool hasLEB128; /// Defaults to false.
135 /// hasDotLoc - True if target asm supports .loc directives.
137 bool hasDotLoc; /// Defaults to false.
139 /// hasDotFile - True if target asm supports .file directives.
141 bool hasDotFile; /// Defaults to false.
143 /// needsSet - True if target asm can't compute addresses on data
145 bool needsSet; /// Defaults to false.
147 /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
149 const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
151 /// DwarfInfoSection - Section directive for Dwarf info.
153 const char *DwarfInfoSection; /// Defaults to ".debug_info".
155 /// DwarfLineSection - Section directive for Dwarf info.
157 const char *DwarfLineSection; /// Defaults to ".debug_line".
159 /// DwarfFrameSection - Section directive for Dwarf info.
161 const char *DwarfFrameSection; /// Defaults to ".debug_frame".
163 /// DwarfPubNamesSection - Section directive for Dwarf info.
165 const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
167 /// DwarfPubTypesSection - Section directive for Dwarf info.
169 const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
171 /// DwarfStrSection - Section directive for Dwarf info.
173 const char *DwarfStrSection; /// Defaults to ".debug_str".
175 /// DwarfLocSection - Section directive for Dwarf info.
177 const char *DwarfLocSection; /// Defaults to ".debug_loc".
179 /// DwarfARangesSection - Section directive for Dwarf info.
181 const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
183 /// DwarfRangesSection - Section directive for Dwarf info.
185 const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
187 /// DwarfMacInfoSection - Section directive for Dwarf info.
189 const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
191 /// TextSection - Section directive for standard text.
193 const char *TextSection; /// Defaults to ".text".
195 /// DataSection - Section directive for standard data.
197 const char *DataSection; /// Defaults to ".data".
199 //===--------------------------------------------------------------------===//
200 // Emission and print routines
204 /// getAddressSize - Return the size of a target address in bytes.
206 unsigned getAddressSize() const { return AddressSize; }
208 /// PrintHex - Print a value as a hexidecimal value.
210 void PrintHex(int Value) const;
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;
216 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
217 /// unsigned leb128 value.
218 void EmitULEB128Bytes(unsigned Value) const;
220 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
221 /// signed leb128 value.
222 void EmitSLEB128Bytes(int Value) const;
224 /// PrintULEB128 - Print a series of hexidecimal values (separated by
225 /// commas) representing an unsigned leb128 value.
226 void PrintULEB128(unsigned Value) const;
228 /// SizeULEB128 - Compute the number of bytes required for an unsigned
230 static unsigned SizeULEB128(unsigned Value);
232 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
233 /// commas) representing a signed leb128 value.
234 void PrintSLEB128(int Value) const;
236 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
238 static unsigned SizeSLEB128(int Value);
240 /// EmitInt8 - Emit a byte directive and value.
242 void EmitInt8(int Value) const;
244 /// EmitInt16 - Emit a short directive and value.
246 void EmitInt16(int Value) const;
248 /// EmitInt32 - Emit a long directive and value.
250 void EmitInt32(int Value) const;
252 /// EmitInt64 - Emit a long long directive and value.
254 void EmitInt64(uint64_t Value) const;
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;
260 /// PrintLabelName - Print label name in form used by Dwarf writer.
262 void PrintLabelName(DWLabel Label) const {
263 PrintLabelName(Label.Tag, Label.Number);
265 void PrintLabelName(const char *Tag, unsigned Number) const;
267 /// EmitLabel - Emit location label for internal use by Dwarf.
269 void EmitLabel(DWLabel Label) const {
270 EmitLabel(Label.Tag, Label.Number);
272 void EmitLabel(const char *Tag, unsigned Number) const;
274 /// EmitReference - Emit a reference to a label.
276 void EmitReference(DWLabel Label) const {
277 EmitReference(Label.Tag, Label.Number);
279 void EmitReference(const char *Tag, unsigned Number) const;
280 void EmitReference(const std::string &Name) const;
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);
288 void EmitDifference(const char *TagHi, unsigned NumberHi,
289 const char *TagLo, unsigned NumberLo) const;
291 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
293 unsigned NewAbbreviation(DIEAbbrev *Abbrev);
295 /// NewString - Add a string to the constant pool and returns a label.
297 DWLabel NewString(const std::string &String);
299 /// NewBasicType - Creates a new basic type if necessary, then adds to the
301 /// FIXME - Should never be needed.
302 DIE *NewBasicType(DIE *Owner, Type *Ty);
304 /// NewGlobalType - Make the type visible globally using the given name.
306 void NewGlobalType(const std::string &Name, DIE *Type);
308 /// NewGlobalEntity - Make the entity visible globally using the given name.
310 void NewGlobalEntity(const std::string &Name, DIE *Entity);
314 /// NewType - Create a new type DIE.
316 DIE *NewType(DIE *Unit, TypeDesc *TyDesc);
318 /// NewCompileUnit - Create new compile unit DIE.
320 DIE *NewCompileUnit(CompileUnitDesc *CompileUnit);
322 /// NewGlobalVariable - Make a new global variable DIE.
324 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
326 /// NewSubprogram - Add a new subprogram DIE.
328 DIE *NewSubprogram(SubprogramDesc *SPD);
330 /// EmitInitial - Emit initial Dwarf declarations.
332 void EmitInitial() const;
334 /// EmitDIE - Recusively Emits a debug information entry.
336 void EmitDIE(DIE *Die) const;
338 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
340 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset);
342 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
344 void SizeAndOffsets();
346 /// EmitDebugInfo - Emit the debug info section.
348 void EmitDebugInfo() const;
350 /// EmitAbbreviations - Emit the abbreviation section.
352 void EmitAbbreviations() const;
354 /// EmitDebugLines - Emit source line information.
356 void EmitDebugLines() const;
358 /// EmitDebugFrame - Emit info into a debug frame section.
360 void EmitDebugFrame();
362 /// EmitDebugPubNames - Emit info into a debug pubnames section.
364 void EmitDebugPubNames();
366 /// EmitDebugPubTypes - Emit info into a debug pubtypes section.
368 void EmitDebugPubTypes();
370 /// EmitDebugStr - Emit info into a debug str section.
374 /// EmitDebugLoc - Emit info into a debug loc section.
378 /// EmitDebugARanges - Emit info into a debug aranges section.
380 void EmitDebugARanges();
382 /// EmitDebugRanges - Emit info into a debug ranges section.
384 void EmitDebugRanges();
386 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
388 void EmitDebugMacInfo();
390 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
392 void ConstructCompileUnitDIEs();
394 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
395 /// global variables.
396 void ConstructGlobalDIEs(Module &M);
398 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
400 void ConstructSubprogramDIEs(Module &M);
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();
409 DwarfWriter(std::ostream &OS, AsmPrinter *A);
410 virtual ~DwarfWriter();
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; }
416 //===--------------------------------------------------------------------===//
417 // Main entry points.
420 /// BeginModule - Emit all Dwarf sections that should come prior to the
422 void BeginModule(Module &M);
424 /// EndModule - Emit all Dwarf sections that should come after the content.
426 void EndModule(Module &M);
428 /// BeginFunction - Gather pre-function debug information.
430 void BeginFunction(MachineFunction &MF);
432 /// EndFunction - Gather and emit post-function debug information.
434 void EndFunction(MachineFunction &MF);
437 } // end llvm namespace