blob: 660684d1bea53a6a965150e24c2a9851334ed06b [file] [log] [blame]
Eric Christopherbcbd3a42011-11-07 09:18:42 +00001//=-- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables -*- C++ -*-=//
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// This file contains support for writing dwarf accelerator tables.
11//
12//===----------------------------------------------------------------------===//
13
Craig Topperf1d0f772012-03-26 06:58:25 +000014#include "DwarfAccelTable.h"
15#include "DwarfDebug.h"
16#include "DIE.h"
17#include "llvm/ADT/Twine.h"
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000018#include "llvm/ADT/STLExtras.h"
Eric Christopherbcbd3a42011-11-07 09:18:42 +000019#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/MCSymbol.h"
23#include "llvm/Support/Debug.h"
Eric Christopherbcbd3a42011-11-07 09:18:42 +000024
25using namespace llvm;
26
27const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
28 switch (AT) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000029 case eAtomTypeNULL: return "eAtomTypeNULL";
30 case eAtomTypeDIEOffset: return "eAtomTypeDIEOffset";
31 case eAtomTypeCUOffset: return "eAtomTypeCUOffset";
32 case eAtomTypeTag: return "eAtomTypeTag";
33 case eAtomTypeNameFlags: return "eAtomTypeNameFlags";
34 case eAtomTypeTypeFlags: return "eAtomTypeTypeFlags";
35 }
David Blaikie2dd674f2012-01-16 23:24:27 +000036 llvm_unreachable("invalid AtomType!");
Eric Christopherbcbd3a42011-11-07 09:18:42 +000037}
38
39// The general case would need to have a less hard coded size for the
40// length of the HeaderData, however, if we're constructing based on a
41// single Atom then we know it will always be: 4 + 4 + 2 + 2.
42DwarfAccelTable::DwarfAccelTable(DwarfAccelTable::Atom atom) :
43 Header(12),
44 HeaderData(atom) {
45}
46
Eric Christopherc36145f2012-01-06 04:35:23 +000047// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
48DwarfAccelTable::DwarfAccelTable(std::vector<DwarfAccelTable::Atom> &atomList) :
49 Header(8 + (atomList.size() * 4)),
50 HeaderData(atomList) {
51}
52
Eric Christophere77546c2011-11-07 21:49:28 +000053DwarfAccelTable::~DwarfAccelTable() {
Eric Christopherc36145f2012-01-06 04:35:23 +000054 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christophere77546c2011-11-07 21:49:28 +000055 delete Data[i];
Eric Christopher7eabae32012-01-06 19:35:04 +000056 for (StringMap<DataArray>::iterator
57 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI)
Eric Christopher547abbb2012-01-06 23:03:27 +000058 for (DataArray::iterator DI = EI->second.begin(),
59 DE = EI->second.end(); DI != DE; ++DI)
Eric Christopher7eabae32012-01-06 19:35:04 +000060 delete (*DI);
Eric Christophere77546c2011-11-07 21:49:28 +000061}
62
Eric Christopherc36145f2012-01-06 04:35:23 +000063void DwarfAccelTable::AddName(StringRef Name, DIE* die, char Flags) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000064 // If the string is in the list already then add this die to the list
65 // otherwise add a new one.
Eric Christopherc36145f2012-01-06 04:35:23 +000066 DataArray &DIEs = Entries[Name];
67 DIEs.push_back(new HashDataContents(die, Flags));
Eric Christopherbcbd3a42011-11-07 09:18:42 +000068}
69
70void DwarfAccelTable::ComputeBucketCount(void) {
71 // First get the number of unique hashes.
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000072 std::vector<uint32_t> uniques(Data.size());
Eric Christopher30b4d8b2011-11-08 18:38:40 +000073 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +000074 uniques[i] = Data[i]->HashValue;
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000075 array_pod_sort(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000076 std::vector<uint32_t>::iterator p =
77 std::unique(uniques.begin(), uniques.end());
78 uint32_t num = std::distance(uniques.begin(), p);
79
80 // Then compute the bucket size, minimum of 1 bucket.
81 if (num > 1024) Header.bucket_count = num/4;
82 if (num > 16) Header.bucket_count = num/2;
83 else Header.bucket_count = num > 0 ? num : 1;
84
85 Header.hashes_count = num;
86}
87
Eric Christopher8368f742011-11-15 23:37:17 +000088namespace {
89 // DIESorter - comparison predicate that sorts DIEs by their offset.
90 struct DIESorter {
Eric Christopherc36145f2012-01-06 04:35:23 +000091 bool operator()(const struct DwarfAccelTable::HashDataContents *A,
92 const struct DwarfAccelTable::HashDataContents *B) const {
93 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher8368f742011-11-15 23:37:17 +000094 }
95 };
96}
97
Eric Christopherbcbd3a42011-11-07 09:18:42 +000098void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
99 // Create the individual hash data outputs.
Eric Christopherc36145f2012-01-06 04:35:23 +0000100 for (StringMap<DataArray>::iterator
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000101 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
102 struct HashData *Entry = new HashData((*EI).getKeyData());
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000103
104 // Unique the entries.
Eric Christopher547abbb2012-01-06 23:03:27 +0000105 std::stable_sort(EI->second.begin(), EI->second.end(), DIESorter());
106 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
107 EI->second.end());
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000108
Eric Christopher547abbb2012-01-06 23:03:27 +0000109 for (DataArray::const_iterator DI = EI->second.begin(),
110 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000111 DI != DE; ++DI)
Eric Christopherc36145f2012-01-06 04:35:23 +0000112 Entry->addData((*DI));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000113 Data.push_back(Entry);
114 }
115
116 // Figure out how many buckets we need, then compute the bucket
117 // contents and the final ordering. We'll emit the hashes and offsets
118 // by doing a walk during the emission phase. We add temporary
119 // symbols to the data so that we can reference them during the offset
120 // later, we'll emit them when we emit the data.
121 ComputeBucketCount();
122
123 // Compute bucket contents and final ordering.
124 Buckets.resize(Header.bucket_count);
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000125 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000126 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
127 Buckets[bucket].push_back(Data[i]);
128 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
129 }
130}
131
132// Emits the header for the table via the AsmPrinter.
133void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
134 Asm->OutStreamer.AddComment("Header Magic");
135 Asm->EmitInt32(Header.magic);
136 Asm->OutStreamer.AddComment("Header Version");
137 Asm->EmitInt16(Header.version);
138 Asm->OutStreamer.AddComment("Header Hash Function");
139 Asm->EmitInt16(Header.hash_function);
140 Asm->OutStreamer.AddComment("Header Bucket Count");
141 Asm->EmitInt32(Header.bucket_count);
142 Asm->OutStreamer.AddComment("Header Hash Count");
143 Asm->EmitInt32(Header.hashes_count);
144 Asm->OutStreamer.AddComment("Header Data Length");
145 Asm->EmitInt32(Header.header_data_len);
146 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
147 Asm->EmitInt32(HeaderData.die_offset_base);
148 Asm->OutStreamer.AddComment("HeaderData Atom Count");
149 Asm->EmitInt32(HeaderData.Atoms.size());
150 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
151 Atom A = HeaderData.Atoms[i];
152 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
153 Asm->EmitInt16(A.type);
154 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
155 Asm->EmitInt16(A.form);
156 }
157}
158
159// Walk through and emit the buckets for the table. This will look
160// like a list of numbers of how many elements are in each bucket.
161void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
162 unsigned index = 0;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000163 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherc5453222011-11-07 18:34:47 +0000164 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000165 if (Buckets[i].size() != 0)
166 Asm->EmitInt32(index);
167 else
168 Asm->EmitInt32(UINT32_MAX);
169 index += Buckets[i].size();
170 }
171}
172
173// Walk through the buckets and emit the individual hashes for each
174// bucket.
175void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000176 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000177 for (HashList::const_iterator HI = Buckets[i].begin(),
178 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000179 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000180 Asm->EmitInt32((*HI)->HashValue);
181 }
182 }
183}
184
185// Walk through the buckets and emit the individual offsets for each
186// element in each bucket. This is done via a symbol subtraction from the
187// beginning of the section. The non-section symbol will be output later
188// when we emit the actual data.
189void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000190 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000191 for (HashList::const_iterator HI = Buckets[i].begin(),
192 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000193 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000194 MCContext &Context = Asm->OutStreamer.getContext();
195 const MCExpr *Sub =
196 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
197 MCSymbolRefExpr::Create(SecBegin, Context),
198 Context);
199 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
200 }
201 }
202}
203
204// Walk through the buckets and emit the full data for each element in
205// the bucket. For the string case emit the dies and the various offsets.
206// Terminate each HashData bucket with 0.
207void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
208 uint64_t PrevHash = UINT64_MAX;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000209 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000210 for (HashList::const_iterator HI = Buckets[i].begin(),
211 HE = Buckets[i].end(); HI != HE; ++HI) {
212 // Remember to emit the label for our offset.
213 Asm->OutStreamer.EmitLabel((*HI)->Sym);
214 Asm->OutStreamer.AddComment((*HI)->Str);
215 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
Eric Christopher76a4e1a2011-11-07 09:38:42 +0000216 D->getStringPool());
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000217 Asm->OutStreamer.AddComment("Num DIEs");
Eric Christopherc36145f2012-01-06 04:35:23 +0000218 Asm->EmitInt32((*HI)->Data.size());
219 for (std::vector<struct HashDataContents*>::const_iterator
220 DI = (*HI)->Data.begin(), DE = (*HI)->Data.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000221 DI != DE; ++DI) {
Eric Christopherc36145f2012-01-06 04:35:23 +0000222 // Emit the DIE offset
223 Asm->EmitInt32((*DI)->Die->getOffset());
224 // If we have multiple Atoms emit that info too.
225 // FIXME: A bit of a hack, we either emit only one atom or all info.
226 if (HeaderData.Atoms.size() > 1) {
227 Asm->EmitInt16((*DI)->Die->getTag());
228 Asm->EmitInt8((*DI)->Flags);
229 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000230 }
231 // Emit a 0 to terminate the data unless we have a hash collision.
232 if (PrevHash != (*HI)->HashValue)
233 Asm->EmitInt32(0);
234 PrevHash = (*HI)->HashValue;
235 }
236 }
237}
238
239// Emit the entire data structure to the output file.
240void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
241 DwarfDebug *D) {
242 // Emit the header.
243 EmitHeader(Asm);
244
245 // Emit the buckets.
246 EmitBuckets(Asm);
247
248 // Emit the hashes.
249 EmitHashes(Asm);
250
251 // Emit the offsets.
252 EmitOffsets(Asm, SecBegin);
253
254 // Emit the hash data.
255 EmitData(Asm, D);
256}
257
258#ifndef NDEBUG
259void DwarfAccelTable::print(raw_ostream &O) {
260
261 Header.print(O);
262 HeaderData.print(O);
263
264 O << "Entries: \n";
Eric Christopherc36145f2012-01-06 04:35:23 +0000265 for (StringMap<DataArray>::const_iterator
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000266 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
Eric Christopher547abbb2012-01-06 23:03:27 +0000267 O << "Name: " << EI->getKeyData() << "\n";
268 for (DataArray::const_iterator DI = EI->second.begin(),
269 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000270 DI != DE; ++DI)
271 (*DI)->print(O);
272 }
273
274 O << "Buckets and Hashes: \n";
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000275 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000276 for (HashList::const_iterator HI = Buckets[i].begin(),
277 HE = Buckets[i].end(); HI != HE; ++HI)
278 (*HI)->print(O);
279
280 O << "Data: \n";
281 for (std::vector<HashData*>::const_iterator
282 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)
283 (*DI)->print(O);
284
285
286}
287#endif