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 //===--------------------------------------------------------------------===//
33 // 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 // DIEAbbrevData - Dwarf abbreviation data, describes the one attribute of a
64 // Dwarf abbreviation.
67 unsigned Attribute; // Dwarf attribute code.
68 unsigned Form; // Dwarf form code.
71 DIEAbbrevData(unsigned A, unsigned F)
77 unsigned getAttribute() const { return Attribute; }
78 unsigned getForm() const { return Form; }
80 /// operator== - Used by DIEAbbrev to locate entry.
82 bool operator==(const DIEAbbrevData &DAD) const {
83 return Attribute == DAD.Attribute && Form == DAD.Form;
86 /// operator!= - Used by DIEAbbrev to locate entry.
88 bool operator!=(const DIEAbbrevData &DAD) const {
89 return Attribute != DAD.Attribute || Form != DAD.Form;
92 /// operator< - Used by DIEAbbrev to locate entry.
94 bool operator<(const DIEAbbrevData &DAD) const {
95 return Attribute < DAD.Attribute ||
96 (Attribute == DAD.Attribute && Form < DAD.Form);
100 //===--------------------------------------------------------------------===//
101 // DIEAbbrev - Dwarf abbreviation, describes the organization of a debug
102 // information object.
105 unsigned Tag; // Dwarf tag code.
106 unsigned ChildrenFlag; // Dwarf children flag.
107 std::vector<DIEAbbrevData> Data; // Raw data bytes for abbreviation.
111 DIEAbbrev(unsigned T, unsigned C)
119 unsigned getTag() const { return Tag; }
120 unsigned getChildrenFlag() const { return ChildrenFlag; }
121 const std::vector<DIEAbbrevData> &getData() const { return Data; }
122 void setChildrenFlag(unsigned CF) { ChildrenFlag = CF; }
124 /// operator== - Used by UniqueVector to locate entry.
126 bool operator==(const DIEAbbrev &DA) const;
128 /// operator< - Used by UniqueVector to locate entry.
130 bool operator<(const DIEAbbrev &DA) const;
132 /// AddAttribute - Adds another set of attribute information to the
134 void AddAttribute(unsigned Attribute, unsigned Form) {
135 Data.push_back(DIEAbbrevData(Attribute, Form));
138 /// Emit - Print the abbreviation using the specified Dwarf writer.
140 void Emit(const DwarfWriter &DW) const;
143 void print(std::ostream &O);
148 //===--------------------------------------------------------------------===//
149 // DIEValue - A debug information entry value.
162 unsigned Type; // Type of the value
164 DIEValue(unsigned T) : Type(T) {}
165 virtual ~DIEValue() {}
167 // Implement isa/cast/dyncast.
168 static bool classof(const DIEValue *) { return true; }
170 /// EmitValue - Emit value via the Dwarf writer.
172 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const = 0;
174 /// SizeOf - Return the size of a value in bytes.
176 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const = 0;
179 //===--------------------------------------------------------------------===//
180 // DWInteger - An integer value DIE.
182 class DIEInteger : public DIEValue {
187 DIEInteger(uint64_t I) : DIEValue(isInteger), Integer(I) {}
189 // Implement isa/cast/dyncast.
190 static bool classof(const DIEInteger *) { return true; }
191 static bool classof(const DIEValue *I) { return I->Type == isInteger; }
193 /// EmitValue - Emit integer of appropriate size.
195 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
197 /// SizeOf - Determine size of integer value in bytes.
199 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
202 //===--------------------------------------------------------------------===//
203 // DIEString - A string value DIE.
205 struct DIEString : public DIEValue {
206 const std::string String;
208 DIEString(const std::string &S) : DIEValue(isString), String(S) {}
210 // Implement isa/cast/dyncast.
211 static bool classof(const DIEString *) { return true; }
212 static bool classof(const DIEValue *S) { return S->Type == isString; }
214 /// EmitValue - Emit string value.
216 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
218 /// SizeOf - Determine size of string value in bytes.
220 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
223 //===--------------------------------------------------------------------===//
224 // DIEDwarfLabel - A Dwarf internal label expression DIE.
226 struct DIEDwarfLabel : public DIEValue {
229 DIEDwarfLabel(const DWLabel &L) : DIEValue(isLabel), Label(L) {}
231 // Implement isa/cast/dyncast.
232 static bool classof(const DIEDwarfLabel *) { return true; }
233 static bool classof(const DIEValue *L) { return L->Type == isLabel; }
235 /// EmitValue - Emit label value.
237 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
239 /// SizeOf - Determine size of label value in bytes.
241 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
245 //===--------------------------------------------------------------------===//
246 // DIEObjectLabel - A label to an object in code or data.
248 struct DIEObjectLabel : public DIEValue {
249 const std::string Label;
251 DIEObjectLabel(const std::string &L) : DIEValue(isAsIsLabel), Label(L) {}
253 // Implement isa/cast/dyncast.
254 static bool classof(const DIEObjectLabel *) { return true; }
255 static bool classof(const DIEValue *L) { return L->Type == isAsIsLabel; }
257 /// EmitValue - Emit label value.
259 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
261 /// SizeOf - Determine size of label value in bytes.
263 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
266 //===--------------------------------------------------------------------===//
267 // DIEDelta - A simple label difference DIE.
269 struct DIEDelta : public DIEValue {
270 const DWLabel LabelHi;
271 const DWLabel LabelLo;
273 DIEDelta(const DWLabel &Hi, const DWLabel &Lo)
274 : DIEValue(isDelta), LabelHi(Hi), LabelLo(Lo) {}
276 // Implement isa/cast/dyncast.
277 static bool classof(const DIEDelta *) { return true; }
278 static bool classof(const DIEValue *D) { return D->Type == isDelta; }
280 /// EmitValue - Emit delta value.
282 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
284 /// SizeOf - Determine size of delta value in bytes.
286 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
289 //===--------------------------------------------------------------------===//
290 // DIEntry - A pointer to a debug information entry.
292 struct DIEntry : public DIEValue {
295 DIEntry(DIE *E) : DIEValue(isEntry), Entry(E) {}
297 // Implement isa/cast/dyncast.
298 static bool classof(const DIEntry *) { return true; }
299 static bool classof(const DIEValue *E) { return E->Type == isEntry; }
301 /// EmitValue - Emit delta value.
303 virtual void EmitValue(const DwarfWriter &DW, unsigned Form) const;
305 /// SizeOf - Determine size of delta value in bytes.
307 virtual unsigned SizeOf(const DwarfWriter &DW, unsigned Form) const;
310 //===--------------------------------------------------------------------===//
311 // DIE - A structured debug information entry. Has an abbreviation which
312 // describes it's organization.
315 DIEAbbrev *Abbrev; // Temporary buffer for abbreviation.
316 unsigned AbbrevID; // Decribing abbreviation ID.
317 unsigned Offset; // Offset in debug info section.
318 unsigned Size; // Size of instance + children.
319 std::vector<DIE *> Children; // Children DIEs.
320 std::vector<DIEValue *> Values; // Attributes values.
327 unsigned getAbbrevID() const { return AbbrevID; }
328 unsigned getOffset() const { return Offset; }
329 unsigned getSize() const { return Size; }
330 const std::vector<DIE *> &getChildren() const { return Children; }
331 const std::vector<DIEValue *> &getValues() const { return Values; }
332 void setOffset(unsigned O) { Offset = O; }
333 void setSize(unsigned S) { Size = S; }
335 /// SiblingOffset - Return the offset of the debug information entry's
337 unsigned SiblingOffset() const { return Offset + Size; }
339 /// AddUInt - Add an unsigned integer attribute data and value.
341 void AddUInt(unsigned Attribute, unsigned Form, uint64_t Integer);
343 /// AddSInt - Add an signed integer attribute data and value.
345 void AddSInt(unsigned Attribute, unsigned Form, int64_t Integer);
347 /// AddString - Add a std::string attribute data and value.
349 void AddString(unsigned Attribute, unsigned Form,
350 const std::string &String);
352 /// AddLabel - Add a Dwarf label attribute data and value.
354 void AddLabel(unsigned Attribute, unsigned Form, const DWLabel &Label);
356 /// AddObjectLabel - Add a non-Dwarf label attribute data and value.
358 void AddObjectLabel(unsigned Attribute, unsigned Form,
359 const std::string &Label);
361 /// AddDelta - Add a label delta attribute data and value.
363 void AddDelta(unsigned Attribute, unsigned Form,
364 const DWLabel &Hi, const DWLabel &Lo);
366 /// AddDIEntry - Add a DIE attribute data and value.
368 void AddDIEntry(unsigned Attribute, unsigned Form, DIE *Entry);
370 /// Complete - Indicate that all attributes have been added and
371 /// ready to get an abbreviation ID.
373 void Complete(DwarfWriter &DW);
375 /// AddChild - Add a child to the DIE.
376 void AddChild(DIE *Child);
379 //===--------------------------------------------------------------------===//
380 // DwarfWriter - Emits Dwarf debug and exception handling directives.
385 //===------------------------------------------------------------------===//
386 // Core attributes used by the Dwarf writer.
390 /// O - Stream to .s file.
394 /// Asm - Target of Dwarf emission.
398 /// DebugInfo - Collected debug information.
400 MachineDebugInfo *DebugInfo;
402 /// didInitial - Flag to indicate if initial emission has been done.
406 //===------------------------------------------------------------------===//
407 // Attributes used to construct specific Dwarf sections.
410 /// CompileUnits - All the compile units involved in this build. The index
411 /// of each entry in this vector corresponds to the sources in DebugInfo.
412 std::vector<DIE *> CompileUnits;
414 /// Abbreviations - A UniqueVector of TAG structure abbreviations.
416 UniqueVector<DIEAbbrev> Abbreviations;
418 /// GlobalTypes - A map of globally visible named types.
420 std::map<std::string, DIE *> GlobalTypes;
422 /// GlobalEntities - A map of globally visible named entities.
424 std::map<std::string, DIE *> GlobalEntities;
426 /// StringPool - A UniqueVector of strings used by indirect references.
428 UniqueVector<std::string> StringPool;
430 /// DescToDieMap - Tracks the mapping of debug informaton descriptors to
432 std::map<DebugInfoDesc *, DIE *> DescToDieMap;
434 /// TypeToDieMap - Type to DIEType map.
436 // FIXME - Should not be needed.
437 std::map<Type *, DIE *> TypeToDieMap;
439 //===------------------------------------------------------------------===//
440 // Properties to be set by the derived class ctor, used to configure the
444 /// AddressSize - Size of addresses used in file.
446 unsigned AddressSize;
448 /// hasLEB128 - True if target asm supports leb128 directives.
450 bool hasLEB128; /// Defaults to false.
452 /// hasDotLoc - True if target asm supports .loc directives.
454 bool hasDotLoc; /// Defaults to false.
456 /// hasDotFile - True if target asm supports .file directives.
458 bool hasDotFile; /// Defaults to false.
460 /// needsSet - True if target asm can't compute addresses on data
462 bool needsSet; /// Defaults to false.
464 /// DwarfAbbrevSection - Section directive for Dwarf abbrev.
466 const char *DwarfAbbrevSection; /// Defaults to ".debug_abbrev".
468 /// DwarfInfoSection - Section directive for Dwarf info.
470 const char *DwarfInfoSection; /// Defaults to ".debug_info".
472 /// DwarfLineSection - Section directive for Dwarf info.
474 const char *DwarfLineSection; /// Defaults to ".debug_line".
476 /// DwarfFrameSection - Section directive for Dwarf info.
478 const char *DwarfFrameSection; /// Defaults to ".debug_frame".
480 /// DwarfPubNamesSection - Section directive for Dwarf info.
482 const char *DwarfPubNamesSection; /// Defaults to ".debug_pubnames".
484 /// DwarfPubTypesSection - Section directive for Dwarf info.
486 const char *DwarfPubTypesSection; /// Defaults to ".debug_pubtypes".
488 /// DwarfStrSection - Section directive for Dwarf info.
490 const char *DwarfStrSection; /// Defaults to ".debug_str".
492 /// DwarfLocSection - Section directive for Dwarf info.
494 const char *DwarfLocSection; /// Defaults to ".debug_loc".
496 /// DwarfARangesSection - Section directive for Dwarf info.
498 const char *DwarfARangesSection; /// Defaults to ".debug_aranges".
500 /// DwarfRangesSection - Section directive for Dwarf info.
502 const char *DwarfRangesSection; /// Defaults to ".debug_ranges".
504 /// DwarfMacInfoSection - Section directive for Dwarf info.
506 const char *DwarfMacInfoSection; /// Defaults to ".debug_macinfo".
508 /// TextSection - Section directive for standard text.
510 const char *TextSection; /// Defaults to ".text".
512 /// DataSection - Section directive for standard data.
514 const char *DataSection; /// Defaults to ".data".
516 //===------------------------------------------------------------------===//
517 // Emission and print routines
521 /// getAddressSize - Return the size of a target address in bytes.
523 unsigned getAddressSize() const { return AddressSize; }
525 /// PrintHex - Print a value as a hexidecimal value.
527 void PrintHex(int Value) const;
529 /// EOL - Print a newline character to asm stream. If a comment is present
530 /// then it will be printed first. Comments should not contain '\n'.
531 void EOL(const std::string &Comment) const;
533 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
534 /// unsigned leb128 value.
535 void EmitULEB128Bytes(unsigned Value) const;
537 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a
538 /// signed leb128 value.
539 void EmitSLEB128Bytes(int Value) const;
541 /// PrintULEB128 - Print a series of hexidecimal values (separated by
542 /// commas) representing an unsigned leb128 value.
543 void PrintULEB128(unsigned Value) const;
545 /// SizeULEB128 - Compute the number of bytes required for an unsigned
547 static unsigned SizeULEB128(unsigned Value);
549 /// PrintSLEB128 - Print a series of hexidecimal values (separated by
550 /// commas) representing a signed leb128 value.
551 void PrintSLEB128(int Value) const;
553 /// SizeSLEB128 - Compute the number of bytes required for a signed leb128
555 static unsigned SizeSLEB128(int Value);
557 /// EmitInt8 - Emit a byte directive and value.
559 void EmitInt8(int Value) const;
561 /// EmitInt16 - Emit a short directive and value.
563 void EmitInt16(int Value) const;
565 /// EmitInt32 - Emit a long directive and value.
567 void EmitInt32(int Value) const;
569 /// EmitInt64 - Emit a long long directive and value.
571 void EmitInt64(uint64_t Value) const;
573 /// EmitString - Emit a string with quotes and a null terminator.
574 /// Special characters are emitted properly. (Eg. '\t')
575 void EmitString(const std::string &String) const;
577 /// PrintLabelName - Print label name in form used by Dwarf writer.
579 void PrintLabelName(DWLabel Label) const {
580 PrintLabelName(Label.Tag, Label.Number);
582 void PrintLabelName(const char *Tag, unsigned Number) const;
584 /// EmitLabel - Emit location label for internal use by Dwarf.
586 void EmitLabel(DWLabel Label) const {
587 EmitLabel(Label.Tag, Label.Number);
589 void EmitLabel(const char *Tag, unsigned Number) const;
591 /// EmitReference - Emit a reference to a label.
593 void EmitReference(DWLabel Label) const {
594 EmitReference(Label.Tag, Label.Number);
596 void EmitReference(const char *Tag, unsigned Number) const;
597 void EmitReference(const std::string &Name) const;
599 /// EmitDifference - Emit the difference between two labels. Some
600 /// assemblers do not behave with absolute expressions with data directives,
601 /// so there is an option (needsSet) to use an intermediary set expression.
602 void EmitDifference(DWLabel LabelHi, DWLabel LabelLo) const {
603 EmitDifference(LabelHi.Tag, LabelHi.Number, LabelLo.Tag, LabelLo.Number);
605 void EmitDifference(const char *TagHi, unsigned NumberHi,
606 const char *TagLo, unsigned NumberLo) const;
608 /// NewAbbreviation - Add the abbreviation to the Abbreviation vector.
610 unsigned NewAbbreviation(DIEAbbrev *Abbrev);
612 /// NewString - Add a string to the constant pool and returns a label.
614 DWLabel NewString(const std::string &String);
616 /// NewBasicType - Creates a new basic type if necessary, then adds to the
618 /// FIXME - Should never be needed.
619 DIE *NewBasicType(DIE *Owner, Type *Ty);
621 /// NewGlobalType - Make the type visible globally using the given name.
623 void NewGlobalType(const std::string &Name, DIE *Type);
625 /// NewGlobalEntity - Make the entity visible globally using the given name.
627 void NewGlobalEntity(const std::string &Name, DIE *Entity);
631 /// NewType - Create a new type DIE.
633 DIE *NewType(DIE *Unit, TypeDesc *TyDesc);
635 /// NewCompileUnit - Create new compile unit DIE.
637 DIE *NewCompileUnit(CompileUnitDesc *CompileUnit);
639 /// NewGlobalVariable - Make a new global variable DIE.
641 DIE *NewGlobalVariable(GlobalVariableDesc *GVD);
643 /// NewSubprogram - Add a new subprogram DIE.
645 DIE *NewSubprogram(SubprogramDesc *SPD);
647 /// EmitInitial - Emit initial Dwarf declarations.
649 void EmitInitial() const;
651 /// EmitDIE - Recusively Emits a debug information entry.
653 void EmitDIE(DIE *Die) const;
655 /// SizeAndOffsetDie - Compute the size and offset of a DIE.
657 unsigned SizeAndOffsetDie(DIE *Die, unsigned Offset);
659 /// SizeAndOffsets - Compute the size and offset of all the DIEs.
661 void SizeAndOffsets();
663 /// EmitDebugInfo - Emit the debug info section.
665 void EmitDebugInfo() const;
667 /// EmitAbbreviations - Emit the abbreviation section.
669 void EmitAbbreviations() const;
671 /// EmitDebugLines - Emit source line information.
673 void EmitDebugLines() const;
675 /// EmitDebugFrame - Emit info into a debug frame section.
677 void EmitDebugFrame();
679 /// EmitDebugPubNames - Emit info into a debug pubnames section.
681 void EmitDebugPubNames();
683 /// EmitDebugPubTypes - Emit info into a debug pubtypes section.
685 void EmitDebugPubTypes();
687 /// EmitDebugStr - Emit info into a debug str section.
691 /// EmitDebugLoc - Emit info into a debug loc section.
695 /// EmitDebugARanges - Emit info into a debug aranges section.
697 void EmitDebugARanges();
699 /// EmitDebugRanges - Emit info into a debug ranges section.
701 void EmitDebugRanges();
703 /// EmitDebugMacInfo - Emit info into a debug macinfo section.
705 void EmitDebugMacInfo();
707 /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
709 void ConstructCompileUnitDIEs();
711 /// ConstructGlobalDIEs - Create DIEs for each of the externally visible
712 /// global variables.
713 void ConstructGlobalDIEs(Module &M);
715 /// ConstructSubprogramDIEs - Create DIEs for each of the externally visible
717 void ConstructSubprogramDIEs(Module &M);
719 /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
720 /// When called it also checks to see if debug info is newly available. if
721 /// so the initial Dwarf headers are emitted.
722 bool ShouldEmitDwarf();
726 DwarfWriter(std::ostream &OS, AsmPrinter *A);
727 virtual ~DwarfWriter();
729 /// SetDebugInfo - Set DebugInfo when it's known that pass manager has
730 /// created it. Set by the target AsmPrinter.
731 void SetDebugInfo(MachineDebugInfo *DI) { DebugInfo = DI; }
733 //===------------------------------------------------------------------===//
734 // Main entry points.
737 /// BeginModule - Emit all Dwarf sections that should come prior to the
739 void BeginModule(Module &M);
741 /// EndModule - Emit all Dwarf sections that should come after the content.
743 void EndModule(Module &M);
745 /// BeginFunction - Gather pre-function debug information.
747 void BeginFunction(MachineFunction &MF);
749 /// EndFunction - Gather and emit post-function debug information.
751 void EndFunction(MachineFunction &MF);
754 } // end llvm namespace