blob: 7c93dbf5b9a55faa0379a8c1beba726f4753f593 [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 Christophere77546c2011-11-07 21:49:28 +000045DwarfAccelTable::~DwarfAccelTable() {
Eric Christopherec8ffc22011-11-08 18:22:25 +000046 for (size_t i = 0, e = Data.size() ; i < e; ++i)
Eric Christophere77546c2011-11-07 21:49:28 +000047 delete Data[i];
48}
49
Eric Christopherbcbd3a42011-11-07 09:18:42 +000050void DwarfAccelTable::AddName(StringRef Name, DIE* die) {
51 // If the string is in the list already then add this die to the list
52 // otherwise add a new one.
53 DIEArray &DIEs = Entries[Name];
54 DIEs.push_back(die);
55}
56
57void DwarfAccelTable::ComputeBucketCount(void) {
58 // First get the number of unique hashes.
59 std::vector<uint32_t> uniques;
60 uniques.resize(Data.size());
Eric Christopher30b4d8b2011-11-08 18:38:40 +000061 for (size_t i = 0, e = Data.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +000062 uniques[i] = Data[i]->HashValue;
63 std::sort(uniques.begin(), uniques.end());
64 std::vector<uint32_t>::iterator p =
65 std::unique(uniques.begin(), uniques.end());
66 uint32_t num = std::distance(uniques.begin(), p);
67
68 // Then compute the bucket size, minimum of 1 bucket.
69 if (num > 1024) Header.bucket_count = num/4;
70 if (num > 16) Header.bucket_count = num/2;
71 else Header.bucket_count = num > 0 ? num : 1;
72
73 Header.hashes_count = num;
74}
75
76void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
77 // Create the individual hash data outputs.
78 for (StringMap<DIEArray>::const_iterator
79 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
80 struct HashData *Entry = new HashData((*EI).getKeyData());
81 for (DIEArray::const_iterator DI = (*EI).second.begin(),
82 DE = (*EI).second.end();
83 DI != DE; ++DI)
84 Entry->addOffset((*DI)->getOffset());
85 Data.push_back(Entry);
86 }
87
88 // Figure out how many buckets we need, then compute the bucket
89 // contents and the final ordering. We'll emit the hashes and offsets
90 // by doing a walk during the emission phase. We add temporary
91 // symbols to the data so that we can reference them during the offset
92 // later, we'll emit them when we emit the data.
93 ComputeBucketCount();
94
95 // Compute bucket contents and final ordering.
96 Buckets.resize(Header.bucket_count);
Eric Christopher30b4d8b2011-11-08 18:38:40 +000097 for (size_t i = 0, e = Data.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +000098 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
99 Buckets[bucket].push_back(Data[i]);
100 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
101 }
102}
103
104// Emits the header for the table via the AsmPrinter.
105void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
106 Asm->OutStreamer.AddComment("Header Magic");
107 Asm->EmitInt32(Header.magic);
108 Asm->OutStreamer.AddComment("Header Version");
109 Asm->EmitInt16(Header.version);
110 Asm->OutStreamer.AddComment("Header Hash Function");
111 Asm->EmitInt16(Header.hash_function);
112 Asm->OutStreamer.AddComment("Header Bucket Count");
113 Asm->EmitInt32(Header.bucket_count);
114 Asm->OutStreamer.AddComment("Header Hash Count");
115 Asm->EmitInt32(Header.hashes_count);
116 Asm->OutStreamer.AddComment("Header Data Length");
117 Asm->EmitInt32(Header.header_data_len);
118 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
119 Asm->EmitInt32(HeaderData.die_offset_base);
120 Asm->OutStreamer.AddComment("HeaderData Atom Count");
121 Asm->EmitInt32(HeaderData.Atoms.size());
122 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
123 Atom A = HeaderData.Atoms[i];
124 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
125 Asm->EmitInt16(A.type);
126 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
127 Asm->EmitInt16(A.form);
128 }
129}
130
131// Walk through and emit the buckets for the table. This will look
132// like a list of numbers of how many elements are in each bucket.
133void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
134 unsigned index = 0;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000135 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherc5453222011-11-07 18:34:47 +0000136 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000137 if (Buckets[i].size() != 0)
138 Asm->EmitInt32(index);
139 else
140 Asm->EmitInt32(UINT32_MAX);
141 index += Buckets[i].size();
142 }
143}
144
145// Walk through the buckets and emit the individual hashes for each
146// bucket.
147void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000148 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000149 for (HashList::const_iterator HI = Buckets[i].begin(),
150 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000151 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000152 Asm->EmitInt32((*HI)->HashValue);
153 }
154 }
155}
156
157// Walk through the buckets and emit the individual offsets for each
158// element in each bucket. This is done via a symbol subtraction from the
159// beginning of the section. The non-section symbol will be output later
160// when we emit the actual data.
161void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000162 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000163 for (HashList::const_iterator HI = Buckets[i].begin(),
164 HE = Buckets[i].end(); HI != HE; ++HI) {
Eric Christopherc5453222011-11-07 18:34:47 +0000165 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000166 MCContext &Context = Asm->OutStreamer.getContext();
167 const MCExpr *Sub =
168 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
169 MCSymbolRefExpr::Create(SecBegin, Context),
170 Context);
171 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
172 }
173 }
174}
175
176// Walk through the buckets and emit the full data for each element in
177// the bucket. For the string case emit the dies and the various offsets.
178// Terminate each HashData bucket with 0.
179void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
180 uint64_t PrevHash = UINT64_MAX;
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000181 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000182 for (HashList::const_iterator HI = Buckets[i].begin(),
183 HE = Buckets[i].end(); HI != HE; ++HI) {
184 // Remember to emit the label for our offset.
185 Asm->OutStreamer.EmitLabel((*HI)->Sym);
186 Asm->OutStreamer.AddComment((*HI)->Str);
187 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
Eric Christopher76a4e1a2011-11-07 09:38:42 +0000188 D->getStringPool());
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000189 Asm->OutStreamer.AddComment("Num DIEs");
190 Asm->EmitInt32((*HI)->DIEOffsets.size());
191 for (std::vector<uint32_t>::const_iterator
192 DI = (*HI)->DIEOffsets.begin(), DE = (*HI)->DIEOffsets.end();
193 DI != DE; ++DI) {
194 Asm->EmitInt32((*DI));
195 }
196 // Emit a 0 to terminate the data unless we have a hash collision.
197 if (PrevHash != (*HI)->HashValue)
198 Asm->EmitInt32(0);
199 PrevHash = (*HI)->HashValue;
200 }
201 }
202}
203
204// Emit the entire data structure to the output file.
205void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
206 DwarfDebug *D) {
207 // Emit the header.
208 EmitHeader(Asm);
209
210 // Emit the buckets.
211 EmitBuckets(Asm);
212
213 // Emit the hashes.
214 EmitHashes(Asm);
215
216 // Emit the offsets.
217 EmitOffsets(Asm, SecBegin);
218
219 // Emit the hash data.
220 EmitData(Asm, D);
221}
222
223#ifndef NDEBUG
224void DwarfAccelTable::print(raw_ostream &O) {
225
226 Header.print(O);
227 HeaderData.print(O);
228
229 O << "Entries: \n";
230 for (StringMap<DIEArray>::const_iterator
231 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
232 O << "Name: " << (*EI).getKeyData() << "\n";
233 for (DIEArray::const_iterator DI = (*EI).second.begin(),
234 DE = (*EI).second.end();
235 DI != DE; ++DI)
236 (*DI)->print(O);
237 }
238
239 O << "Buckets and Hashes: \n";
Eric Christopher30b4d8b2011-11-08 18:38:40 +0000240 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
Eric Christopherbcbd3a42011-11-07 09:18:42 +0000241 for (HashList::const_iterator HI = Buckets[i].begin(),
242 HE = Buckets[i].end(); HI != HE; ++HI)
243 (*HI)->print(O);
244
245 O << "Data: \n";
246 for (std::vector<HashData*>::const_iterator
247 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)
248 (*DI)->print(O);
249
250
251}
252#endif