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 void DwarfAccelTable::AddName(StringRef Name, const DIE *die, char Flags) {
33 assert(Data.empty() && "Already finalized!");
34 // If the string is in the list already then add this die to the list
35 // otherwise add a new one.
36 DataArray &DIEs = Entries[Name];
37 DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
40 void DwarfAccelTable::ComputeBucketCount(void) {
41 // First get the number of unique hashes.
42 std::vector<uint32_t> uniques(Data.size());
43 for (size_t i = 0, e = Data.size(); i < e; ++i)
44 uniques[i] = Data[i]->HashValue;
45 array_pod_sort(uniques.begin(), uniques.end());
46 std::vector<uint32_t>::iterator p =
47 std::unique(uniques.begin(), uniques.end());
48 uint32_t num = std::distance(uniques.begin(), p);
50 // Then compute the bucket size, minimum of 1 bucket.
52 Header.bucket_count = num / 4;
54 Header.bucket_count = num / 2;
56 Header.bucket_count = num > 0 ? num : 1;
58 Header.hashes_count = num;
61 // compareDIEs - comparison predicate that sorts DIEs by their offset.
62 static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
63 const DwarfAccelTable::HashDataContents *B) {
64 return A->Die->getOffset() < B->Die->getOffset();
67 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
68 // Create the individual hash data outputs.
69 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
72 // Unique the entries.
73 std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
74 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
77 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
78 Data.push_back(Entry);
81 // Figure out how many buckets we need, then compute the bucket
82 // contents and the final ordering. We'll emit the hashes and offsets
83 // by doing a walk during the emission phase. We add temporary
84 // symbols to the data so that we can reference them during the offset
85 // later, we'll emit them when we emit the data.
88 // Compute bucket contents and final ordering.
89 Buckets.resize(Header.bucket_count);
90 for (size_t i = 0, e = Data.size(); i < e; ++i) {
91 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
92 Buckets[bucket].push_back(Data[i]);
93 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
97 // Emits the header for the table via the AsmPrinter.
98 void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
99 Asm->OutStreamer.AddComment("Header Magic");
100 Asm->EmitInt32(Header.magic);
101 Asm->OutStreamer.AddComment("Header Version");
102 Asm->EmitInt16(Header.version);
103 Asm->OutStreamer.AddComment("Header Hash Function");
104 Asm->EmitInt16(Header.hash_function);
105 Asm->OutStreamer.AddComment("Header Bucket Count");
106 Asm->EmitInt32(Header.bucket_count);
107 Asm->OutStreamer.AddComment("Header Hash Count");
108 Asm->EmitInt32(Header.hashes_count);
109 Asm->OutStreamer.AddComment("Header Data Length");
110 Asm->EmitInt32(Header.header_data_len);
111 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
112 Asm->EmitInt32(HeaderData.die_offset_base);
113 Asm->OutStreamer.AddComment("HeaderData Atom Count");
114 Asm->EmitInt32(HeaderData.Atoms.size());
115 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
116 Atom A = HeaderData.Atoms[i];
117 Asm->OutStreamer.AddComment(dwarf::AtomTypeString(A.type));
118 Asm->EmitInt16(A.type);
119 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
120 Asm->EmitInt16(A.form);
124 // Walk through and emit the buckets for the table. Each index is
125 // an offset into the list of hashes.
126 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
128 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
129 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
130 if (Buckets[i].size() != 0)
131 Asm->EmitInt32(index);
133 Asm->EmitInt32(UINT32_MAX);
134 index += Buckets[i].size();
138 // Walk through the buckets and emit the individual hashes for each
140 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
141 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
142 for (HashList::const_iterator HI = Buckets[i].begin(),
143 HE = Buckets[i].end();
145 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
146 Asm->EmitInt32((*HI)->HashValue);
151 // Walk through the buckets and emit the individual offsets for each
152 // element in each bucket. This is done via a symbol subtraction from the
153 // beginning of the section. The non-section symbol will be output later
154 // when we emit the actual data.
155 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
156 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
157 for (HashList::const_iterator HI = Buckets[i].begin(),
158 HE = Buckets[i].end();
160 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
161 MCContext &Context = Asm->OutStreamer.getContext();
162 const MCExpr *Sub = MCBinaryExpr::CreateSub(
163 MCSymbolRefExpr::Create((*HI)->Sym, Context),
164 MCSymbolRefExpr::Create(SecBegin, Context), Context);
165 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
170 // Walk through the buckets and emit the full data for each element in
171 // the bucket. For the string case emit the dies and the various offsets.
172 // Terminate each HashData bucket with 0.
173 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfFile *D) {
174 uint64_t PrevHash = UINT64_MAX;
175 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
176 for (HashList::const_iterator HI = Buckets[i].begin(),
177 HE = Buckets[i].end();
179 // Remember to emit the label for our offset.
180 Asm->OutStreamer.EmitLabel((*HI)->Sym);
181 Asm->OutStreamer.AddComment((*HI)->Str);
182 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
183 D->getStringPoolSym());
184 Asm->OutStreamer.AddComment("Num DIEs");
185 Asm->EmitInt32((*HI)->Data.size());
186 for (ArrayRef<HashDataContents *>::const_iterator
187 DI = (*HI)->Data.begin(),
188 DE = (*HI)->Data.end();
190 // Emit the DIE offset
191 Asm->EmitInt32((*DI)->Die->getOffset());
192 // If we have multiple Atoms emit that info too.
193 // FIXME: A bit of a hack, we either emit only one atom or all info.
194 if (HeaderData.Atoms.size() > 1) {
195 Asm->EmitInt16((*DI)->Die->getTag());
196 Asm->EmitInt8((*DI)->Flags);
199 // Emit a 0 to terminate the data unless we have a hash collision.
200 if (PrevHash != (*HI)->HashValue)
202 PrevHash = (*HI)->HashValue;
207 // Emit the entire data structure to the output file.
208 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfFile *D) {
219 EmitOffsets(Asm, SecBegin);
221 // Emit the hash data.
226 void DwarfAccelTable::print(raw_ostream &O) {
232 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
235 O << "Name: " << EI->getKeyData() << "\n";
236 for (DataArray::const_iterator DI = EI->second.begin(),
237 DE = EI->second.end();
242 O << "Buckets and Hashes: \n";
243 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
244 for (HashList::const_iterator HI = Buckets[i].begin(),
245 HE = Buckets[i].end();
250 for (std::vector<HashData *>::const_iterator DI = Data.begin(),