Return DIE by reference instead of pointer from DwarfUnit::getUnitDie
[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, const char *Pref, BumpPtrAllocator &DA)
22     : Asm(AP), StringPool(DA), NextStringPoolNumber(0), StringPref(Pref) {}
23
24 DwarfFile::~DwarfFile() {}
25
26 MCSymbol *DwarfFile::getStringPoolSym() {
27   return Asm->GetTempSymbol(StringPref);
28 }
29
30 MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
31   std::pair<MCSymbol *, unsigned> &Entry =
32       StringPool.GetOrCreateValue(Str).getValue();
33   if (Entry.first)
34     return Entry.first;
35
36   Entry.second = NextStringPoolNumber++;
37   return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
38 }
39
40 unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
41   std::pair<MCSymbol *, unsigned> &Entry =
42       StringPool.GetOrCreateValue(Str).getValue();
43   if (Entry.first)
44     return Entry.second;
45
46   Entry.second = NextStringPoolNumber++;
47   Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
48   return Entry.second;
49 }
50
51 // Define a unique number for the abbreviation.
52 //
53 void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
54   // Check the set for priors.
55   DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
56
57   // If it's newly added.
58   if (InSet == &Abbrev) {
59     // Add to abbreviation list.
60     Abbreviations.push_back(&Abbrev);
61
62     // Assign the vector position + 1 as its number.
63     Abbrev.setNumber(Abbreviations.size());
64   } else {
65     // Assign existing abbreviation number.
66     Abbrev.setNumber(InSet->getNumber());
67   }
68 }
69
70 void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
71   CUs.push_back(std::move(U));
72 }
73
74 // Emit the various dwarf units to the unit section USection with
75 // the abbreviations going into ASection.
76 void DwarfFile::emitUnits(DwarfDebug *DD, const MCSymbol *ASectionSym) {
77   for (const auto &TheU : CUs) {
78     DIE &Die = TheU->getUnitDie();
79     const MCSection *USection = TheU->getSection();
80     Asm->OutStreamer.SwitchSection(USection);
81
82     // Emit the compile units header.
83     Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
84
85     // Emit size of content not including length itself
86     Asm->OutStreamer.AddComment("Length of Unit");
87     Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
88
89     TheU->emitHeader(ASectionSym);
90
91     DD->emitDIE(Die);
92     Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
93   }
94 }
95 // Compute the size and offset for each DIE.
96 void DwarfFile::computeSizeAndOffsets() {
97   // Offset from the first CU in the debug info section is 0 initially.
98   unsigned SecOffset = 0;
99
100   // Iterate over each compile unit and set the size and offsets for each
101   // DIE within each compile unit. All offsets are CU relative.
102   for (const auto &TheU : CUs) {
103     TheU->setDebugInfoOffset(SecOffset);
104
105     // CU-relative offset is reset to 0 here.
106     unsigned Offset = sizeof(int32_t) +      // Length of Unit Info
107                       TheU->getHeaderSize(); // Unit-specific headers
108
109     // EndOffset here is CU-relative, after laying out
110     // all of the CU DIE.
111     unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
112     SecOffset += EndOffset;
113   }
114 }
115 // Compute the size and offset of a DIE. The offset is relative to start of the
116 // CU. It returns the offset after laying out the DIE.
117 unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
118   // Record the abbreviation.
119   assignAbbrevNumber(Die.getAbbrev());
120
121   // Get the abbreviation for this DIE.
122   const DIEAbbrev &Abbrev = Die.getAbbrev();
123
124   // Set DIE offset
125   Die.setOffset(Offset);
126
127   // Start the size with the size of abbreviation code.
128   Offset += getULEB128Size(Die.getAbbrevNumber());
129
130   const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
131   const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
132
133   // Size the DIE attribute values.
134   for (unsigned i = 0, N = Values.size(); i < N; ++i)
135     // Size attribute value.
136     Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
137
138   // Get the children.
139   const auto &Children = Die.getChildren();
140
141   // Size the DIE children if any.
142   if (!Children.empty()) {
143     assert(Abbrev.hasChildren() && "Children flag not set");
144
145     for (auto &Child : Children)
146       Offset = computeSizeAndOffset(*Child, Offset);
147
148     // End of children marker.
149     Offset += sizeof(int8_t);
150   }
151
152   Die.setSize(Offset - Die.getOffset());
153   return Offset;
154 }
155 void DwarfFile::emitAbbrevs(const MCSection *Section) {
156   // Check to see if it is worth the effort.
157   if (!Abbreviations.empty()) {
158     // Start the debug abbrev section.
159     Asm->OutStreamer.SwitchSection(Section);
160
161     // For each abbrevation.
162     for (const DIEAbbrev *Abbrev : Abbreviations) {
163       // Emit the abbrevations code (base 1 index.)
164       Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
165
166       // Emit the abbreviations data.
167       Abbrev->Emit(Asm);
168     }
169
170     // Mark end of abbreviations.
171     Asm->EmitULEB128(0, "EOM(3)");
172   }
173 }
174
175 // Emit strings into a string section.
176 void DwarfFile::emitStrings(const MCSection *StrSection,
177                             const MCSection *OffsetSection,
178                             const MCSymbol *StrSecSym) {
179
180   if (StringPool.empty())
181     return;
182
183   // Start the dwarf str section.
184   Asm->OutStreamer.SwitchSection(StrSection);
185
186   // Get all of the string pool entries and put them in an array by their ID so
187   // we can sort them.
188   SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64> Entries;
189
190   for (const auto &I : StringPool)
191     Entries.push_back(std::make_pair(I.second.second, &I));
192
193   array_pod_sort(Entries.begin(), Entries.end());
194
195   for (const auto &Entry : Entries) {
196     // Emit a label for reference from debug information entries.
197     Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
198
199     // Emit the string itself with a terminating null byte.
200     Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
201                                          Entry.second->getKeyLength() + 1));
202   }
203
204   // If we've got an offset section go ahead and emit that now as well.
205   if (OffsetSection) {
206     Asm->OutStreamer.SwitchSection(OffsetSection);
207     unsigned offset = 0;
208     unsigned size = 4; // FIXME: DWARF64 is 8.
209     for (const auto &Entry : Entries) {
210       Asm->OutStreamer.EmitIntValue(offset, size);
211       offset += Entry.second->getKeyLength() + 1;
212     }
213   }
214 }
215 }