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