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