1 //=-- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf accelerator tables.
12 //===----------------------------------------------------------------------===//
14 #include "DwarfAccelTable.h"
15 #include "DwarfDebug.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/ADT/STLExtras.h"
19 #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"
27 const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
29 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";
36 llvm_unreachable("invalid AtomType!");
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.
42 DwarfAccelTable::DwarfAccelTable(DwarfAccelTable::Atom atom) :
47 // The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
48 DwarfAccelTable::DwarfAccelTable(std::vector<DwarfAccelTable::Atom> &atomList) :
49 Header(8 + (atomList.size() * 4)),
50 HeaderData(atomList) {
53 DwarfAccelTable::~DwarfAccelTable() {
54 for (size_t i = 0, e = Data.size(); i < e; ++i)
56 for (StringMap<DataArray>::iterator
57 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI)
58 for (DataArray::iterator DI = EI->second.begin(),
59 DE = EI->second.end(); DI != DE; ++DI)
63 void DwarfAccelTable::AddName(StringRef Name, DIE* die, char Flags) {
64 // If the string is in the list already then add this die to the list
65 // otherwise add a new one.
66 DataArray &DIEs = Entries[Name];
67 DIEs.push_back(new HashDataContents(die, Flags));
70 void DwarfAccelTable::ComputeBucketCount(void) {
71 // First get the number of unique hashes.
72 std::vector<uint32_t> uniques(Data.size());
73 for (size_t i = 0, e = Data.size(); i < e; ++i)
74 uniques[i] = Data[i]->HashValue;
75 array_pod_sort(uniques.begin(), uniques.end());
76 std::vector<uint32_t>::iterator p =
77 std::unique(uniques.begin(), uniques.end());
78 uint32_t num = std::distance(uniques.begin(), p);
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;
85 Header.hashes_count = num;
89 // DIESorter - comparison predicate that sorts DIEs by their offset.
91 bool operator()(const struct DwarfAccelTable::HashDataContents *A,
92 const struct DwarfAccelTable::HashDataContents *B) const {
93 return A->Die->getOffset() < B->Die->getOffset();
98 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
99 // Create the individual hash data outputs.
100 for (StringMap<DataArray>::iterator
101 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
102 struct HashData *Entry = new HashData((*EI).getKeyData());
104 // Unique the entries.
105 std::stable_sort(EI->second.begin(), EI->second.end(), DIESorter());
106 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
109 for (DataArray::const_iterator DI = EI->second.begin(),
110 DE = EI->second.end();
112 Entry->addData((*DI));
113 Data.push_back(Entry);
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();
123 // Compute bucket contents and final ordering.
124 Buckets.resize(Header.bucket_count);
125 for (size_t i = 0, e = Data.size(); i < e; ++i) {
126 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
127 Buckets[bucket].push_back(Data[i]);
128 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
132 // Emits the header for the table via the AsmPrinter.
133 void 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);
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.
161 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
163 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
164 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
165 if (Buckets[i].size() != 0)
166 Asm->EmitInt32(index);
168 Asm->EmitInt32(UINT32_MAX);
169 index += Buckets[i].size();
173 // Walk through the buckets and emit the individual hashes for each
175 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
176 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
177 for (HashList::const_iterator HI = Buckets[i].begin(),
178 HE = Buckets[i].end(); HI != HE; ++HI) {
179 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
180 Asm->EmitInt32((*HI)->HashValue);
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.
189 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
190 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
191 for (HashList::const_iterator HI = Buckets[i].begin(),
192 HE = Buckets[i].end(); HI != HE; ++HI) {
193 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
194 MCContext &Context = Asm->OutStreamer.getContext();
196 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
197 MCSymbolRefExpr::Create(SecBegin, Context),
199 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
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.
207 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
208 uint64_t PrevHash = UINT64_MAX;
209 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
210 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),
217 Asm->OutStreamer.AddComment("Num DIEs");
218 Asm->EmitInt32((*HI)->Data.size());
219 for (std::vector<struct HashDataContents*>::const_iterator
220 DI = (*HI)->Data.begin(), DE = (*HI)->Data.end();
222 // 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);
231 // Emit a 0 to terminate the data unless we have a hash collision.
232 if (PrevHash != (*HI)->HashValue)
234 PrevHash = (*HI)->HashValue;
239 // Emit the entire data structure to the output file.
240 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
252 EmitOffsets(Asm, SecBegin);
254 // Emit the hash data.
259 void DwarfAccelTable::print(raw_ostream &O) {
265 for (StringMap<DataArray>::const_iterator
266 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
267 O << "Name: " << EI->getKeyData() << "\n";
268 for (DataArray::const_iterator DI = EI->second.begin(),
269 DE = EI->second.end();
274 O << "Buckets and Hashes: \n";
275 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
276 for (HashList::const_iterator HI = Buckets[i].begin(),
277 HE = Buckets[i].end(); HI != HE; ++HI)
281 for (std::vector<HashData*>::const_iterator
282 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)