[DebugInfo] Add DwarfDebug& to DwarfFile.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfFile.cpp
1 //===-- llvm/CodeGen/DwarfFile.cpp - Dwarf Debug Framework ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "DwarfFile.h"
11
12 #include "DwarfDebug.h"
13 #include "DwarfUnit.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/Support/LEB128.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Target/TargetLoweringObjectFile.h"
19
20 namespace llvm {
21 DwarfFile::DwarfFile(AsmPrinter *AP, DwarfDebug &DD, StringRef Pref,
22                      BumpPtrAllocator &DA)
23     : Asm(AP), DD(DD), StrPool(DA, *Asm, Pref) {
24   (void)this->DD;
25 }
26
27 DwarfFile::~DwarfFile() {}
28
29 // Define a unique number for the abbreviation.
30 //
31 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
32   // Check the set for priors.
33   DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
34
35   // If it's newly added.
36   if (InSet == &Abbrev) {
37     // Add to abbreviation list.
38     Abbreviations.push_back(&Abbrev);
39
40     // Assign the vector position + 1 as its number.
41     Abbrev.setNumber(Abbreviations.size());
42   } else {
43     // Assign existing abbreviation number.
44     Abbrev.setNumber(InSet->getNumber());
45   }
46 }
47
48 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
49   CUs.push_back(std::move(U));
50 }
51
52 // Emit the various dwarf units to the unit section USection with
53 // the abbreviations going into ASection.
54 void DwarfFile::emitUnits(const MCSymbol *ASectionSym) {
55   for (const auto &TheU : CUs) {
56     DIE &Die = TheU->getUnitDie();
57     const MCSection *USection = TheU->getSection();
58     Asm->OutStreamer.SwitchSection(USection);
59
60     // Emit the compile units header.
61     Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
62
63     // Emit size of content not including length itself
64     Asm->OutStreamer.AddComment("Length of Unit");
65     Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
66
67     TheU->emitHeader(ASectionSym);
68
69     DD.emitDIE(Die);
70     Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
71   }
72 }
73 // Compute the size and offset for each DIE.
74 void DwarfFile::computeSizeAndOffsets() {
75   // Offset from the first CU in the debug info section is 0 initially.
76   unsigned SecOffset = 0;
77
78   // Iterate over each compile unit and set the size and offsets for each
79   // DIE within each compile unit. All offsets are CU relative.
80   for (const auto &TheU : CUs) {
81     TheU->setDebugInfoOffset(SecOffset);
82
83     // CU-relative offset is reset to 0 here.
84     unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
85                       TheU->getHeaderSize(); // Unit-specific headers
86
87     // EndOffset here is CU-relative, after laying out
88     // all of the CU DIE.
89     unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
90     SecOffset += EndOffset;
91   }
92 }
93 // Compute the size and offset of a DIE. The offset is relative to start of the
94 // CU. It returns the offset after laying out the DIE.
95 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
96   // Record the abbreviation.
97   assignAbbrevNumber(Die.getAbbrev());
98
99   // Get the abbreviation for this DIE.
100   const DIEAbbrev &Abbrev = Die.getAbbrev();
101
102   // Set DIE offset
103   Die.setOffset(Offset);
104
105   // Start the size with the size of abbreviation code.
106   Offset += getULEB128Size(Die.getAbbrevNumber());
107
108   const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
109   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
110
111   // Size the DIE attribute values.
112   for (unsigned i = 0, N = Values.size(); i < N; ++i)
113     // Size attribute value.
114     Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
115
116   // Get the children.
117   const auto &Children = Die.getChildren();
118
119   // Size the DIE children if any.
120   if (!Children.empty()) {
121     assert(Abbrev.hasChildren() && "Children flag not set");
122
123     for (auto &Child : Children)
124       Offset = computeSizeAndOffset(*Child, Offset);
125
126     // End of children marker.
127     Offset += sizeof(int8_t);
128   }
129
130   Die.setSize(Offset - Die.getOffset());
131   return Offset;
132 }
133 void DwarfFile::emitAbbrevs(const MCSection *Section) {
134   // Check to see if it is worth the effort.
135   if (!Abbreviations.empty()) {
136     // Start the debug abbrev section.
137     Asm->OutStreamer.SwitchSection(Section);
138
139     // For each abbrevation.
140     for (const DIEAbbrev *Abbrev : Abbreviations) {
141       // Emit the abbrevations code (base 1 index.)
142       Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
143
144       // Emit the abbreviations data.
145       Abbrev->Emit(Asm);
146     }
147
148     // Mark end of abbreviations.
149     Asm->EmitULEB128(0, "EOM(3)");
150   }
151 }
152
153 // Emit strings into a string section.
154 void DwarfFile::emitStrings(const MCSection *StrSection,
155                             const MCSection *OffsetSection) {
156   StrPool.emit(*Asm, StrSection, OffsetSection);
157 }
158 }