blob: 4d567c89a55d7e208e39d274cb3feb048a45ef61 [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
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCStreamer.h"
17#include "llvm/MC/MCSymbol.h"
18#include "llvm/Support/Debug.h"
19#include "DwarfAccelTable.h"
20#include "DwarfDebug.h"
21#include "DIE.h"
22
23using namespace llvm;
24
25const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
26 switch (AT) {
27 default: llvm_unreachable("invalid AtomType!");
28 case eAtomTypeNULL: return "eAtomTypeNULL";
29 case eAtomTypeDIEOffset: return "eAtomTypeDIEOffset";
30 case eAtomTypeCUOffset: return "eAtomTypeCUOffset";
31 case eAtomTypeTag: return "eAtomTypeTag";
32 case eAtomTypeNameFlags: return "eAtomTypeNameFlags";
33 case eAtomTypeTypeFlags: return "eAtomTypeTypeFlags";
34 }
35}
36
37// The general case would need to have a less hard coded size for the
38// length of the HeaderData, however, if we're constructing based on a
39// single Atom then we know it will always be: 4 + 4 + 2 + 2.
40DwarfAccelTable::DwarfAccelTable(DwarfAccelTable::Atom atom) :
41 Header(12),
42 HeaderData(atom) {
43}
44
Eric Christopherc36145f2012-01-06 04:35:23 +000045// The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
46DwarfAccelTable::DwarfAccelTable(std::vector<DwarfAccelTable::Atom> &atomList) :
47 Header(8 + (atomList.size() * 4)),
48 HeaderData(atomList) {
49}
50
Eric Christophere77546c2011-11-07 21:49:28 +000051DwarfAccelTable::~DwarfAccelTable() {
Eric Christopherc36145f2012-01-06 04:35:23 +000052 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christophere77546c2011-11-07 21:49:28 +000053 delete Data[i];
Eric Christopher7eabae32012-01-06 19:35:04 +000054 for (StringMap<DataArray>::iterator
55 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI)
Eric Christopher547abbb2012-01-06 23:03:27 +000056 for (DataArray::iterator DI = EI->second.begin(),
57 DE = EI->second.end(); DI != DE; ++DI)
Eric Christopher7eabae32012-01-06 19:35:04 +000058 delete (*DI);
Eric Christophere77546c2011-11-07 21:49:28 +000059}
60
Eric Christopherc36145f2012-01-06 04:35:23 +000061void DwarfAccelTable::AddName(StringRef Name, DIE* die, char Flags) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000062 // If the string is in the list already then add this die to the list
63 // otherwise add a new one.
Eric Christopherc36145f2012-01-06 04:35:23 +000064 DataArray &DIEs = Entries[Name];
65 DIEs.push_back(new HashDataContents(die, Flags));
Eric Christopherbcbd3a42011-11-07 09:18:42 +000066}
67
68void DwarfAccelTable::ComputeBucketCount(void) {
69 // First get the number of unique hashes.
70 std::vector<uint32_t> uniques;
71 uniques.resize(Data.size());
Eric Christopher30b4d8b2011-11-08 18:38:40 +000072 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +000073 uniques[i] = Data[i]->HashValue;
Eric Christopher8368f742011-11-15 23:37:17 +000074 std::stable_sort(uniques.begin(), uniques.end());
Eric Christopherbcbd3a42011-11-07 09:18:42 +000075 std::vector<uint32_t>::iterator p =
76 std::unique(uniques.begin(), uniques.end());
77 uint32_t num = std::distance(uniques.begin(), p);
78
79 // Then compute the bucket size, minimum of 1 bucket.
80 if (num > 1024) Header.bucket_count = num/4;
81 if (num > 16) Header.bucket_count = num/2;
82 else Header.bucket_count = num > 0 ? num : 1;
83
84 Header.hashes_count = num;
85}
86
Eric Christopher8368f742011-11-15 23:37:17 +000087namespace {
88 // DIESorter - comparison predicate that sorts DIEs by their offset.
89 struct DIESorter {
Eric Christopherc36145f2012-01-06 04:35:23 +000090 bool operator()(const struct DwarfAccelTable::HashDataContents *A,
91 const struct DwarfAccelTable::HashDataContents *B) const {
92 return A->Die->getOffset() < B->Die->getOffset();
Eric Christopher8368f742011-11-15 23:37:17 +000093 }
94 };
95}
96
Eric Christopherbcbd3a42011-11-07 09:18:42 +000097void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
98 // Create the individual hash data outputs.
Eric Christopherc36145f2012-01-06 04:35:23 +000099 for (StringMap<DataArray>::iterator
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000100 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
101 struct HashData *Entry = new HashData((*EI).getKeyData());
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000102
103 // Unique the entries.
Eric Christopher547abbb2012-01-06 23:03:27 +0000104 std::stable_sort(EI->second.begin(), EI->second.end(), DIESorter());
105 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
106 EI->second.end());
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000107
Eric Christopher547abbb2012-01-06 23:03:27 +0000108 for (DataArray::const_iterator DI = EI->second.begin(),
109 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000110 DI != DE; ++DI)
Eric Christopherc36145f2012-01-06 04:35:23 +0000111 Entry->addData((*DI));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000112 Data.push_back(Entry);
113 }
114
115 // Figure out how many buckets we need, then compute the bucket
116 // contents and the final ordering. We'll emit the hashes and offsets
117 // by doing a walk during the emission phase. We add temporary
118 // symbols to the data so that we can reference them during the offset
119 // later, we'll emit them when we emit the data.
120 ComputeBucketCount();
121
122 // Compute bucket contents and final ordering.
123 Buckets.resize(Header.bucket_count);
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000124 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000125 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
126 Buckets[bucket].push_back(Data[i]);
127 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
128 }
129}
130
131// Emits the header for the table via the AsmPrinter.
132void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
133 Asm->OutStreamer.AddComment("Header Magic");
134 Asm->EmitInt32(Header.magic);
135 Asm->OutStreamer.AddComment("Header Version");
136 Asm->EmitInt16(Header.version);
137 Asm->OutStreamer.AddComment("Header Hash Function");
138 Asm->EmitInt16(Header.hash_function);
139 Asm->OutStreamer.AddComment("Header Bucket Count");
140 Asm->EmitInt32(Header.bucket_count);
141 Asm->OutStreamer.AddComment("Header Hash Count");
142 Asm->EmitInt32(Header.hashes_count);
143 Asm->OutStreamer.AddComment("Header Data Length");
144 Asm->EmitInt32(Header.header_data_len);
145 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
146 Asm->EmitInt32(HeaderData.die_offset_base);
147 Asm->OutStreamer.AddComment("HeaderData Atom Count");
148 Asm->EmitInt32(HeaderData.Atoms.size());
149 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
150 Atom A = HeaderData.Atoms[i];
151 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
152 Asm->EmitInt16(A.type);
153 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
154 Asm->EmitInt16(A.form);
155 }
156}
157
158// Walk through and emit the buckets for the table. This will look
159// like a list of numbers of how many elements are in each bucket.
160void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
161 unsigned index = 0;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000162 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherc5453222011-11-07 18:34:47 +0000163 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000164 if (Buckets[i].size() != 0)
165 Asm->EmitInt32(index);
166 else
167 Asm->EmitInt32(UINT32_MAX);
168 index += Buckets[i].size();
169 }
170}
171
172// Walk through the buckets and emit the individual hashes for each
173// bucket.
174void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000175 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000176 for (HashList::const_iterator HI = Buckets[i].begin(),
177 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000178 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000179 Asm->EmitInt32((*HI)->HashValue);
180 }
181 }
182}
183
184// Walk through the buckets and emit the individual offsets for each
185// element in each bucket. This is done via a symbol subtraction from the
186// beginning of the section. The non-section symbol will be output later
187// when we emit the actual data.
188void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000189 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000190 for (HashList::const_iterator HI = Buckets[i].begin(),
191 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000192 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000193 MCContext &Context = Asm->OutStreamer.getContext();
194 const MCExpr *Sub =
195 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
196 MCSymbolRefExpr::Create(SecBegin, Context),
197 Context);
198 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
199 }
200 }
201}
202
203// Walk through the buckets and emit the full data for each element in
204// the bucket. For the string case emit the dies and the various offsets.
205// Terminate each HashData bucket with 0.
206void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
207 uint64_t PrevHash = UINT64_MAX;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000208 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000209 for (HashList::const_iterator HI = Buckets[i].begin(),
210 HE = Buckets[i].end(); HI != HE; ++HI) {
211 // Remember to emit the label for our offset.
212 Asm->OutStreamer.EmitLabel((*HI)->Sym);
213 Asm->OutStreamer.AddComment((*HI)->Str);
214 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
Eric Christopher76a4e1a2011-11-07 09:38:42 +0000215 D->getStringPool());
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000216 Asm->OutStreamer.AddComment("Num DIEs");
Eric Christopherc36145f2012-01-06 04:35:23 +0000217 Asm->EmitInt32((*HI)->Data.size());
218 for (std::vector<struct HashDataContents*>::const_iterator
219 DI = (*HI)->Data.begin(), DE = (*HI)->Data.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000220 DI != DE; ++DI) {
Eric Christopherc36145f2012-01-06 04:35:23 +0000221 // Emit the DIE offset
222 Asm->EmitInt32((*DI)->Die->getOffset());
223 // If we have multiple Atoms emit that info too.
224 // FIXME: A bit of a hack, we either emit only one atom or all info.
225 if (HeaderData.Atoms.size() > 1) {
226 Asm->EmitInt16((*DI)->Die->getTag());
227 Asm->EmitInt8((*DI)->Flags);
228 }
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000229 }
230 // Emit a 0 to terminate the data unless we have a hash collision.
231 if (PrevHash != (*HI)->HashValue)
232 Asm->EmitInt32(0);
233 PrevHash = (*HI)->HashValue;
234 }
235 }
236}
237
238// Emit the entire data structure to the output file.
239void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
240 DwarfDebug *D) {
241 // Emit the header.
242 EmitHeader(Asm);
243
244 // Emit the buckets.
245 EmitBuckets(Asm);
246
247 // Emit the hashes.
248 EmitHashes(Asm);
249
250 // Emit the offsets.
251 EmitOffsets(Asm, SecBegin);
252
253 // Emit the hash data.
254 EmitData(Asm, D);
255}
256
257#ifndef NDEBUG
258void DwarfAccelTable::print(raw_ostream &O) {
259
260 Header.print(O);
261 HeaderData.print(O);
262
263 O << "Entries: \n";
Eric Christopherc36145f2012-01-06 04:35:23 +0000264 for (StringMap<DataArray>::const_iterator
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000265 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
Eric Christopher547abbb2012-01-06 23:03:27 +0000266 O << "Name: " << EI->getKeyData() << "\n";
267 for (DataArray::const_iterator DI = EI->second.begin(),
268 DE = EI->second.end();
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000269 DI != DE; ++DI)
270 (*DI)->print(O);
271 }
272
273 O << "Buckets and Hashes: \n";
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000274 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000275 for (HashList::const_iterator HI = Buckets[i].begin(),
276 HE = Buckets[i].end(); HI != HE; ++HI)
277 (*HI)->print(O);
278
279 O << "Data: \n";
280 for (std::vector<HashData*>::const_iterator
281 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)
282 (*DI)->print(O);
283
284
285}
286#endif