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 "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"
25 const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType 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";
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.
40 DwarfAccelTable::DwarfAccelTable(DwarfAccelTable::Atom atom) :
45 DwarfAccelTable::~DwarfAccelTable() {
46 for (size_t i = 0, e = Data.size() ; i < e; ++i)
50 void 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];
57 void DwarfAccelTable::ComputeBucketCount(void) {
58 // First get the number of unique hashes.
59 std::vector<uint32_t> uniques;
60 uniques.resize(Data.size());
61 for (size_t i = 0, e = Data.size(); i < e; ++i)
62 uniques[i] = Data[i]->HashValue;
63 std::stable_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);
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;
73 Header.hashes_count = num;
77 // DIESorter - comparison predicate that sorts DIEs by their offset.
79 bool operator()(DIE *A, DIE *B) const {
80 return A->getOffset() < B->getOffset();
85 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
86 // Create the individual hash data outputs.
87 for (StringMap<DIEArray>::iterator
88 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
89 struct HashData *Entry = new HashData((*EI).getKeyData());
91 // Unique the entries.
92 std::stable_sort((*EI).second.begin(), (*EI).second.end(), DIESorter());
93 (*EI).second.erase(std::unique((*EI).second.begin(), (*EI).second.end()),
96 for (DIEArray::const_iterator DI = (*EI).second.begin(),
97 DE = (*EI).second.end();
99 Entry->addOffset((*DI)->getOffset());
100 Data.push_back(Entry);
103 // Figure out how many buckets we need, then compute the bucket
104 // contents and the final ordering. We'll emit the hashes and offsets
105 // by doing a walk during the emission phase. We add temporary
106 // symbols to the data so that we can reference them during the offset
107 // later, we'll emit them when we emit the data.
108 ComputeBucketCount();
110 // Compute bucket contents and final ordering.
111 Buckets.resize(Header.bucket_count);
112 for (size_t i = 0, e = Data.size(); i < e; ++i) {
113 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
114 Buckets[bucket].push_back(Data[i]);
115 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
119 // Emits the header for the table via the AsmPrinter.
120 void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
121 Asm->OutStreamer.AddComment("Header Magic");
122 Asm->EmitInt32(Header.magic);
123 Asm->OutStreamer.AddComment("Header Version");
124 Asm->EmitInt16(Header.version);
125 Asm->OutStreamer.AddComment("Header Hash Function");
126 Asm->EmitInt16(Header.hash_function);
127 Asm->OutStreamer.AddComment("Header Bucket Count");
128 Asm->EmitInt32(Header.bucket_count);
129 Asm->OutStreamer.AddComment("Header Hash Count");
130 Asm->EmitInt32(Header.hashes_count);
131 Asm->OutStreamer.AddComment("Header Data Length");
132 Asm->EmitInt32(Header.header_data_len);
133 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
134 Asm->EmitInt32(HeaderData.die_offset_base);
135 Asm->OutStreamer.AddComment("HeaderData Atom Count");
136 Asm->EmitInt32(HeaderData.Atoms.size());
137 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
138 Atom A = HeaderData.Atoms[i];
139 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
140 Asm->EmitInt16(A.type);
141 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
142 Asm->EmitInt16(A.form);
146 // Walk through and emit the buckets for the table. This will look
147 // like a list of numbers of how many elements are in each bucket.
148 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
150 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
151 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
152 if (Buckets[i].size() != 0)
153 Asm->EmitInt32(index);
155 Asm->EmitInt32(UINT32_MAX);
156 index += Buckets[i].size();
160 // Walk through the buckets and emit the individual hashes for each
162 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
163 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
164 for (HashList::const_iterator HI = Buckets[i].begin(),
165 HE = Buckets[i].end(); HI != HE; ++HI) {
166 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
167 Asm->EmitInt32((*HI)->HashValue);
172 // Walk through the buckets and emit the individual offsets for each
173 // element in each bucket. This is done via a symbol subtraction from the
174 // beginning of the section. The non-section symbol will be output later
175 // when we emit the actual data.
176 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
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(); HI != HE; ++HI) {
180 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
181 MCContext &Context = Asm->OutStreamer.getContext();
183 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
184 MCSymbolRefExpr::Create(SecBegin, Context),
186 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
191 // Walk through the buckets and emit the full data for each element in
192 // the bucket. For the string case emit the dies and the various offsets.
193 // Terminate each HashData bucket with 0.
194 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
195 uint64_t PrevHash = UINT64_MAX;
196 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
197 for (HashList::const_iterator HI = Buckets[i].begin(),
198 HE = Buckets[i].end(); HI != HE; ++HI) {
199 // Remember to emit the label for our offset.
200 Asm->OutStreamer.EmitLabel((*HI)->Sym);
201 Asm->OutStreamer.AddComment((*HI)->Str);
202 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str),
204 Asm->OutStreamer.AddComment("Num DIEs");
205 Asm->EmitInt32((*HI)->DIEOffsets.size());
206 for (std::vector<uint32_t>::const_iterator
207 DI = (*HI)->DIEOffsets.begin(), DE = (*HI)->DIEOffsets.end();
209 Asm->EmitInt32((*DI));
211 // Emit a 0 to terminate the data unless we have a hash collision.
212 if (PrevHash != (*HI)->HashValue)
214 PrevHash = (*HI)->HashValue;
219 // Emit the entire data structure to the output file.
220 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
232 EmitOffsets(Asm, SecBegin);
234 // Emit the hash data.
239 void DwarfAccelTable::print(raw_ostream &O) {
245 for (StringMap<DIEArray>::const_iterator
246 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
247 O << "Name: " << (*EI).getKeyData() << "\n";
248 for (DIEArray::const_iterator DI = (*EI).second.begin(),
249 DE = (*EI).second.end();
254 O << "Buckets and Hashes: \n";
255 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
256 for (HashList::const_iterator HI = Buckets[i].begin(),
257 HE = Buckets[i].end(); HI != HE; ++HI)
261 for (std::vector<HashData*>::const_iterator
262 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)