blob: 454a923c13e06864a16bc6a626fb9640f1a85175 [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
Eric Christopherc36145f2012-01-06 04:35:23 +000039// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
Benjamin Kramer36c38b82012-04-13 20:06:17 +000040DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList) :
Eric Christopherc36145f2012-01-06 04:35:23 +000041 Header(8 + (atomList.size() * 4)),
Benjamin Kramer36c38b82012-04-13 20:06:17 +000042 HeaderData(atomList),
43 Entries(Allocator) { }
Eric Christopherc36145f2012-01-06 04:35:23 +000044
Benjamin Kramer36c38b82012-04-13 20:06:17 +000045DwarfAccelTable::~DwarfAccelTable() { }
Eric Christophere77546c2011-11-07 21:49:28 +000046
Eric Christopherc36145f2012-01-06 04:35:23 +000047void DwarfAccelTable::AddName(StringRef Name, DIE* die, char Flags) {
Benjamin Kramer36c38b82012-04-13 20:06:17 +000048 assert(Data.empty() && "Already finalized!");
Eric Christopherbcbd3a42011-11-07 09:18:42 +000049 // If the string is in the list already then add this die to the list
50 // otherwise add a new one.
Eric Christopherc36145f2012-01-06 04:35:23 +000051 DataArray &DIEs = Entries[Name];
Benjamin Kramer36c38b82012-04-13 20:06:17 +000052 DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
Eric Christopherbcbd3a42011-11-07 09:18:42 +000053}
54
55void DwarfAccelTable::ComputeBucketCount(void) {
56 // First get the number of unique hashes.
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000057 std::vector<uint32_t> uniques(Data.size());
Eric Christopher30b4d8b2011-11-08 18:38:40 +000058 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +000059 uniques[i] = Data[i]->HashValue;
Benjamin Kramerbe3f0512012-03-26 14:17:26 +000060 array_pod_sort(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000061 std::vector<uint32_t>::iterator p =
62 std::unique(uniques.begin(), uniques.end());
63 uint32_t num = std::distance(uniques.begin(), p);
64
65 // Then compute the bucket size, minimum of 1 bucket.
66 if (num > 1024) Header.bucket_count = num/4;
67 if (num > 16) Header.bucket_count = num/2;
68 else Header.bucket_count = num > 0 ? num : 1;
69
70 Header.hashes_count = num;
71}
72
Benjamin Kramer36c38b82012-04-13 20:06:17 +000073// compareDIEs - comparison predicate that sorts DIEs by their offset.
74static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
75 const DwarfAccelTable::HashDataContents *B) {
76 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher8368f742011-11-15 23:37:17 +000077}
78
Eric Christopherbcbd3a42011-11-07 09:18:42 +000079void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
80 // Create the individual hash data outputs.
Eric Christopherc36145f2012-01-06 04:35:23 +000081 for (StringMap<DataArray>::iterator
Eric Christopherbcbd3a42011-11-07 09:18:42 +000082 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
Eric Christopher0ffe2b42011-11-10 19:25:34 +000083
84 // Unique the entries.
Benjamin Kramer36c38b82012-04-13 20:06:17 +000085 std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
Eric Christopher547abbb2012-01-06 23:03:27 +000086 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
87 EI->second.end());
Eric Christopher0ffe2b42011-11-10 19:25:34 +000088
Benjamin Kramer36c38b82012-04-13 20:06:17 +000089 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
Eric Christopherbcbd3a42011-11-07 09:18:42 +000090 Data.push_back(Entry);
91 }
92
93 // Figure out how many buckets we need, then compute the bucket
94 // contents and the final ordering. We'll emit the hashes and offsets
95 // by doing a walk during the emission phase. We add temporary
96 // symbols to the data so that we can reference them during the offset
97 // later, we'll emit them when we emit the data.
98 ComputeBucketCount();
99
100 // Compute bucket contents and final ordering.
101 Buckets.resize(Header.bucket_count);
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000102 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000103 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
104 Buckets[bucket].push_back(Data[i]);
105 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
106 }
107}
108
109// Emits the header for the table via the AsmPrinter.
110void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
111 Asm->OutStreamer.AddComment("Header Magic");
112 Asm->EmitInt32(Header.magic);
113 Asm->OutStreamer.AddComment("Header Version");
114 Asm->EmitInt16(Header.version);
115 Asm->OutStreamer.AddComment("Header Hash Function");
116 Asm->EmitInt16(Header.hash_function);
117 Asm->OutStreamer.AddComment("Header Bucket Count");
118 Asm->EmitInt32(Header.bucket_count);
119 Asm->OutStreamer.AddComment("Header Hash Count");
120 Asm->EmitInt32(Header.hashes_count);
121 Asm->OutStreamer.AddComment("Header Data Length");
122 Asm->EmitInt32(Header.header_data_len);
123 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
124 Asm->EmitInt32(HeaderData.die_offset_base);
125 Asm->OutStreamer.AddComment("HeaderData Atom Count");
126 Asm->EmitInt32(HeaderData.Atoms.size());
127 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
128 Atom A = HeaderData.Atoms[i];
129 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
130 Asm->EmitInt16(A.type);
131 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
132 Asm->EmitInt16(A.form);
133 }
134}
135
136// Walk through and emit the buckets for the table. This will look
137// like a list of numbers of how many elements are in each bucket.
138void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
139 unsigned index = 0;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000140 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherc5453222011-11-07 18:34:47 +0000141 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000142 if (Buckets[i].size() != 0)
143 Asm->EmitInt32(index);
144 else
145 Asm->EmitInt32(UINT32_MAX);
146 index += Buckets[i].size();
147 }
148}
149
150// Walk through the buckets and emit the individual hashes for each
151// bucket.
152void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000153 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000154 for (HashList::const_iterator HI = Buckets[i].begin(),
155 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000156 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000157 Asm->EmitInt32((*HI)->HashValue);
158 }
159 }
160}
161
162// Walk through the buckets and emit the individual offsets for each
163// element in each bucket. This is done via a symbol subtraction from the
164// beginning of the section. The non-section symbol will be output later
165// when we emit the actual data.
166void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000167 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000168 for (HashList::const_iterator HI = Buckets[i].begin(),
169 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000170 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000171 MCContext &Context = Asm->OutStreamer.getContext();
172 const MCExpr *Sub =
173 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
174 MCSymbolRefExpr::Create(SecBegin, Context),
175 Context);
176 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
177 }
178 }
179}
180
181// Walk through the buckets and emit the full data for each element in
182// the bucket. For the string case emit the dies and the various offsets.
183// Terminate each HashData bucket with 0.
184void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
185 uint64_t PrevHash = UINT64_MAX;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000186 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000187 for (HashList::const_iterator HI = Buckets[i].begin(),
188 HE = Buckets[i].end(); HI != HE; ++HI) {
189 // Remember to emit the label for our offset.
190 Asm->OutStreamer.EmitLabel((*HI)->Sym);
191 Asm->OutStreamer.AddComment((*HI)->Str);
192 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
Eric Christopher76a4e1a2011-11-07 09:38:42 +0000193 D->getStringPool());
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000194 Asm->OutStreamer.AddComment("Num DIEs");
Eric Christopherc36145f2012-01-06 04:35:23 +0000195 Asm->EmitInt32((*HI)->Data.size());
Benjamin Kramer36c38b82012-04-13 20:06:17 +0000196 for (ArrayRef<HashDataContents*>::const_iterator
Eric Christopherc36145f2012-01-06 04:35:23 +0000197 DI = (*HI)->Data.begin(), DE = (*HI)->Data.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000198 DI != DE; ++DI) {
Eric Christopherc36145f2012-01-06 04:35:23 +0000199 // Emit the DIE offset
200 Asm->EmitInt32((*DI)->Die->getOffset());
201 // If we have multiple Atoms emit that info too.
202 // FIXME: A bit of a hack, we either emit only one atom or all info.
203 if (HeaderData.Atoms.size() > 1) {
204 Asm->EmitInt16((*DI)->Die->getTag());
205 Asm->EmitInt8((*DI)->Flags);
206 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000207 }
208 // Emit a 0 to terminate the data unless we have a hash collision.
209 if (PrevHash != (*HI)->HashValue)
210 Asm->EmitInt32(0);
211 PrevHash = (*HI)->HashValue;
212 }
213 }
214}
215
216// Emit the entire data structure to the output file.
217void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
218 DwarfDebug *D) {
219 // Emit the header.
220 EmitHeader(Asm);
221
222 // Emit the buckets.
223 EmitBuckets(Asm);
224
225 // Emit the hashes.
226 EmitHashes(Asm);
227
228 // Emit the offsets.
229 EmitOffsets(Asm, SecBegin);
230
231 // Emit the hash data.
232 EmitData(Asm, D);
233}
234
235#ifndef NDEBUG
236void DwarfAccelTable::print(raw_ostream &O) {
237
238 Header.print(O);
239 HeaderData.print(O);
240
241 O << "Entries: \n";
Eric Christopherc36145f2012-01-06 04:35:23 +0000242 for (StringMap<DataArray>::const_iterator
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000243 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
Eric Christopher547abbb2012-01-06 23:03:27 +0000244 O << "Name: " << EI->getKeyData() << "\n";
245 for (DataArray::const_iterator DI = EI->second.begin(),
246 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000247 DI != DE; ++DI)
248 (*DI)->print(O);
249 }
250
251 O << "Buckets and Hashes: \n";
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000252 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000253 for (HashList::const_iterator HI = Buckets[i].begin(),
254 HE = Buckets[i].end(); HI != HE; ++HI)
255 (*HI)->print(O);
256
257 O << "Data: \n";
258 for (std::vector<HashData*>::const_iterator
259 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)
260 (*DI)->print(O);
261
262
263}
264#endif