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"
16 #include "DwarfDebug.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/Twine.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 // The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
28 DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
29 : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
32 DwarfAccelTable::~DwarfAccelTable() {}
34 void DwarfAccelTable::AddName(StringRef Name, const DIE *die, char Flags) {
35 assert(Data.empty() && "Already finalized!");
36 // If the string is in the list already then add this die to the list
37 // otherwise add a new one.
38 DataArray &DIEs = Entries[Name];
39 DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
42 void DwarfAccelTable::ComputeBucketCount(void) {
43 // First get the number of unique hashes.
44 std::vector<uint32_t> uniques(Data.size());
45 for (size_t i = 0, e = Data.size(); i < e; ++i)
46 uniques[i] = Data[i]->HashValue;
47 array_pod_sort(uniques.begin(), uniques.end());
48 std::vector<uint32_t>::iterator p =
49 std::unique(uniques.begin(), uniques.end());
50 uint32_t num = std::distance(uniques.begin(), p);
52 // Then compute the bucket size, minimum of 1 bucket.
54 Header.bucket_count = num / 4;
56 Header.bucket_count = num / 2;
58 Header.bucket_count = num > 0 ? num : 1;
60 Header.hashes_count = num;
63 // compareDIEs - comparison predicate that sorts DIEs by their offset.
64 static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
65 const DwarfAccelTable::HashDataContents *B) {
66 return A->Die->getOffset() < B->Die->getOffset();
69 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
70 // Create the individual hash data outputs.
71 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
74 // Unique the entries.
75 std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
76 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
79 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
80 Data.push_back(Entry);
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.
90 // Compute bucket contents and final ordering.
91 Buckets.resize(Header.bucket_count);
92 for (size_t i = 0, e = Data.size(); i < e; ++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);
99 // Emits the header for the table via the AsmPrinter.
100 void 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(dwarf::AtomTypeString(A.type));
120 Asm->EmitInt16(A.type);
121 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
122 Asm->EmitInt16(A.form);
126 // Walk through and emit the buckets for the table. Each index is
127 // an offset into the list of hashes.
128 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
130 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
131 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
132 if (Buckets[i].size() != 0)
133 Asm->EmitInt32(index);
135 Asm->EmitInt32(UINT32_MAX);
136 index += Buckets[i].size();
140 // Walk through the buckets and emit the individual hashes for each
142 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
143 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
144 for (HashList::const_iterator HI = Buckets[i].begin(),
145 HE = Buckets[i].end();
147 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
148 Asm->EmitInt32((*HI)->HashValue);
153 // Walk through the buckets and emit the individual offsets for each
154 // element in each bucket. This is done via a symbol subtraction from the
155 // beginning of the section. The non-section symbol will be output later
156 // when we emit the actual data.
157 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
158 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
159 for (HashList::const_iterator HI = Buckets[i].begin(),
160 HE = Buckets[i].end();
162 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
163 MCContext &Context = Asm->OutStreamer.getContext();
164 const MCExpr *Sub = MCBinaryExpr::CreateSub(
165 MCSymbolRefExpr::Create((*HI)->Sym, Context),
166 MCSymbolRefExpr::Create(SecBegin, Context), Context);
167 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
172 // Walk through the buckets and emit the full data for each element in
173 // the bucket. For the string case emit the dies and the various offsets.
174 // Terminate each HashData bucket with 0.
175 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfFile *D) {
176 uint64_t PrevHash = UINT64_MAX;
177 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
178 for (HashList::const_iterator HI = Buckets[i].begin(),
179 HE = Buckets[i].end();
181 // Remember to emit the label for our offset.
182 Asm->OutStreamer.EmitLabel((*HI)->Sym);
183 Asm->OutStreamer.AddComment((*HI)->Str);
184 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
185 D->getStringPoolSym());
186 Asm->OutStreamer.AddComment("Num DIEs");
187 Asm->EmitInt32((*HI)->Data.size());
188 for (ArrayRef<HashDataContents *>::const_iterator
189 DI = (*HI)->Data.begin(),
190 DE = (*HI)->Data.end();
192 // Emit the DIE offset
193 Asm->EmitInt32((*DI)->Die->getOffset());
194 // If we have multiple Atoms emit that info too.
195 // FIXME: A bit of a hack, we either emit only one atom or all info.
196 if (HeaderData.Atoms.size() > 1) {
197 Asm->EmitInt16((*DI)->Die->getTag());
198 Asm->EmitInt8((*DI)->Flags);
201 // Emit a 0 to terminate the data unless we have a hash collision.
202 if (PrevHash != (*HI)->HashValue)
204 PrevHash = (*HI)->HashValue;
209 // Emit the entire data structure to the output file.
210 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfFile *D) {
221 EmitOffsets(Asm, SecBegin);
223 // Emit the hash data.
228 void DwarfAccelTable::print(raw_ostream &O) {
234 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
237 O << "Name: " << EI->getKeyData() << "\n";
238 for (DataArray::const_iterator DI = EI->second.begin(),
239 DE = EI->second.end();
244 O << "Buckets and Hashes: \n";
245 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
246 for (HashList::const_iterator HI = Buckets[i].begin(),
247 HE = Buckets[i].end();
252 for (std::vector<HashData *>::const_iterator DI = Data.begin(),