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 const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
30 return "eAtomTypeNULL";
31 case eAtomTypeDIEOffset:
32 return "eAtomTypeDIEOffset";
33 case eAtomTypeCUOffset:
34 return "eAtomTypeCUOffset";
36 return "eAtomTypeTag";
37 case eAtomTypeNameFlags:
38 return "eAtomTypeNameFlags";
39 case eAtomTypeTypeFlags:
40 return "eAtomTypeTypeFlags";
42 llvm_unreachable("invalid AtomType!");
45 // The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
46 DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
47 : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
50 DwarfAccelTable::~DwarfAccelTable() {}
52 void DwarfAccelTable::AddName(StringRef Name, DIE *die, char Flags) {
53 assert(Data.empty() && "Already finalized!");
54 // If the string is in the list already then add this die to the list
55 // otherwise add a new one.
56 DataArray &DIEs = Entries[Name];
57 DIEs.push_back(new (Allocator) HashDataContents(die, Flags));
60 void DwarfAccelTable::ComputeBucketCount(void) {
61 // First get the number of unique hashes.
62 std::vector<uint32_t> uniques(Data.size());
63 for (size_t i = 0, e = Data.size(); i < e; ++i)
64 uniques[i] = Data[i]->HashValue;
65 array_pod_sort(uniques.begin(), uniques.end());
66 std::vector<uint32_t>::iterator p =
67 std::unique(uniques.begin(), uniques.end());
68 uint32_t num = std::distance(uniques.begin(), p);
70 // Then compute the bucket size, minimum of 1 bucket.
72 Header.bucket_count = num / 4;
74 Header.bucket_count = num / 2;
76 Header.bucket_count = num > 0 ? num : 1;
78 Header.hashes_count = num;
81 // compareDIEs - comparison predicate that sorts DIEs by their offset.
82 static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
83 const DwarfAccelTable::HashDataContents *B) {
84 return A->Die->getOffset() < B->Die->getOffset();
87 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
88 // Create the individual hash data outputs.
89 for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
92 // Unique the entries.
93 std::stable_sort(EI->second.begin(), EI->second.end(), compareDIEs);
94 EI->second.erase(std::unique(EI->second.begin(), EI->second.end()),
97 HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
98 Data.push_back(Entry);
101 // Figure out how many buckets we need, then compute the bucket
102 // contents and the final ordering. We'll emit the hashes and offsets
103 // by doing a walk during the emission phase. We add temporary
104 // symbols to the data so that we can reference them during the offset
105 // later, we'll emit them when we emit the data.
106 ComputeBucketCount();
108 // Compute bucket contents and final ordering.
109 Buckets.resize(Header.bucket_count);
110 for (size_t i = 0, e = Data.size(); i < e; ++i) {
111 uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
112 Buckets[bucket].push_back(Data[i]);
113 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
117 // Emits the header for the table via the AsmPrinter.
118 void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
119 Asm->OutStreamer.AddComment("Header Magic");
120 Asm->EmitInt32(Header.magic);
121 Asm->OutStreamer.AddComment("Header Version");
122 Asm->EmitInt16(Header.version);
123 Asm->OutStreamer.AddComment("Header Hash Function");
124 Asm->EmitInt16(Header.hash_function);
125 Asm->OutStreamer.AddComment("Header Bucket Count");
126 Asm->EmitInt32(Header.bucket_count);
127 Asm->OutStreamer.AddComment("Header Hash Count");
128 Asm->EmitInt32(Header.hashes_count);
129 Asm->OutStreamer.AddComment("Header Data Length");
130 Asm->EmitInt32(Header.header_data_len);
131 Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
132 Asm->EmitInt32(HeaderData.die_offset_base);
133 Asm->OutStreamer.AddComment("HeaderData Atom Count");
134 Asm->EmitInt32(HeaderData.Atoms.size());
135 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
136 Atom A = HeaderData.Atoms[i];
137 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
138 Asm->EmitInt16(A.type);
139 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
140 Asm->EmitInt16(A.form);
144 // Walk through and emit the buckets for the table. Each index is
145 // an offset into the list of hashes.
146 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
148 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
149 Asm->OutStreamer.AddComment("Bucket " + Twine(i));
150 if (Buckets[i].size() != 0)
151 Asm->EmitInt32(index);
153 Asm->EmitInt32(UINT32_MAX);
154 index += Buckets[i].size();
158 // Walk through the buckets and emit the individual hashes for each
160 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
161 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
162 for (HashList::const_iterator HI = Buckets[i].begin(),
163 HE = Buckets[i].end();
165 Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
166 Asm->EmitInt32((*HI)->HashValue);
171 // Walk through the buckets and emit the individual offsets for each
172 // element in each bucket. This is done via a symbol subtraction from the
173 // beginning of the section. The non-section symbol will be output later
174 // when we emit the actual data.
175 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
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();
180 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
181 MCContext &Context = Asm->OutStreamer.getContext();
182 const MCExpr *Sub = MCBinaryExpr::CreateSub(
183 MCSymbolRefExpr::Create((*HI)->Sym, Context),
184 MCSymbolRefExpr::Create(SecBegin, Context), Context);
185 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
190 // Walk through the buckets and emit the full data for each element in
191 // the bucket. For the string case emit the dies and the various offsets.
192 // Terminate each HashData bucket with 0.
193 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfUnits *D) {
194 uint64_t PrevHash = UINT64_MAX;
195 for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
196 for (HashList::const_iterator HI = Buckets[i].begin(),
197 HE = Buckets[i].end();
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),
203 D->getStringPoolSym());
204 Asm->OutStreamer.AddComment("Num DIEs");
205 Asm->EmitInt32((*HI)->Data.size());
206 for (ArrayRef<HashDataContents *>::const_iterator
207 DI = (*HI)->Data.begin(),
208 DE = (*HI)->Data.end();
210 // Emit the DIE offset
211 Asm->EmitInt32((*DI)->Die->getOffset());
212 // If we have multiple Atoms emit that info too.
213 // FIXME: A bit of a hack, we either emit only one atom or all info.
214 if (HeaderData.Atoms.size() > 1) {
215 Asm->EmitInt16((*DI)->Die->getTag());
216 Asm->EmitInt8((*DI)->Flags);
219 // Emit a 0 to terminate the data unless we have a hash collision.
220 if (PrevHash != (*HI)->HashValue)
222 PrevHash = (*HI)->HashValue;
227 // Emit the entire data structure to the output file.
228 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfUnits *D) {
239 EmitOffsets(Asm, SecBegin);
241 // Emit the hash data.
246 void DwarfAccelTable::print(raw_ostream &O) {
252 for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
255 O << "Name: " << EI->getKeyData() << "\n";
256 for (DataArray::const_iterator DI = EI->second.begin(),
257 DE = EI->second.end();
262 O << "Buckets and Hashes: \n";
263 for (size_t i = 0, e = Buckets.size(); i < e; ++i)
264 for (HashList::const_iterator HI = Buckets[i].begin(),
265 HE = Buckets[i].end();
270 for (std::vector<HashData *>::const_iterator DI = Data.begin(),