Encapsulate the DWARF string pool in a separate type.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfAccelTable.cpp
1 //=-- llvm/CodeGen/DwarfAccelTable.cpp - Dwarf Accelerator Tables -*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf accelerator tables.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DwarfAccelTable.h"
15 #include "DIE.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"
24
25 using namespace llvm;
26
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),
30       Entries(Allocator) {}
31
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));
38 }
39
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);
49
50   // Then compute the bucket size, minimum of 1 bucket.
51   if (num > 1024)
52     Header.bucket_count = num / 4;
53   if (num > 16)
54     Header.bucket_count = num / 2;
55   else
56     Header.bucket_count = num > 0 ? num : 1;
57
58   Header.hashes_count = num;
59 }
60
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();
65 }
66
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();
70        EI != EE; ++EI) {
71
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()),
75                      EI->second.end());
76
77     HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
78     Data.push_back(Entry);
79   }
80
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.
86   ComputeBucketCount();
87
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);
94   }
95 }
96
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);
121   }
122 }
123
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) {
127   unsigned index = 0;
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);
132     else
133       Asm->EmitInt32(UINT32_MAX);
134     index += Buckets[i].size();
135   }
136 }
137
138 // Walk through the buckets and emit the individual hashes for each
139 // bucket.
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();
144          HI != HE; ++HI) {
145       Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
146       Asm->EmitInt32((*HI)->HashValue);
147     }
148   }
149 }
150
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();
159          HI != HE; ++HI) {
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));
166     }
167   }
168 }
169
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();
178          HI != HE; ++HI) {
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->getStringPool().getSymbol(*Asm, (*HI)->Str),
183                              D->getStringPool().getSectionSymbol());
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();
189            DI != DE; ++DI) {
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);
197         }
198       }
199       // Emit a 0 to terminate the data unless we have a hash collision.
200       if (PrevHash != (*HI)->HashValue)
201         Asm->EmitInt32(0);
202       PrevHash = (*HI)->HashValue;
203     }
204   }
205 }
206
207 // Emit the entire data structure to the output file.
208 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfFile *D) {
209   // Emit the header.
210   EmitHeader(Asm);
211
212   // Emit the buckets.
213   EmitBuckets(Asm);
214
215   // Emit the hashes.
216   EmitHashes(Asm);
217
218   // Emit the offsets.
219   EmitOffsets(Asm, SecBegin);
220
221   // Emit the hash data.
222   EmitData(Asm, D);
223 }
224
225 #ifndef NDEBUG
226 void DwarfAccelTable::print(raw_ostream &O) {
227
228   Header.print(O);
229   HeaderData.print(O);
230
231   O << "Entries: \n";
232   for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
233                                             EE = Entries.end();
234        EI != EE; ++EI) {
235     O << "Name: " << EI->getKeyData() << "\n";
236     for (DataArray::const_iterator DI = EI->second.begin(),
237                                    DE = EI->second.end();
238          DI != DE; ++DI)
239       (*DI)->print(O);
240   }
241
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();
246          HI != HE; ++HI)
247       (*HI)->print(O);
248
249   O << "Data: \n";
250   for (std::vector<HashData *>::const_iterator DI = Data.begin(),
251                                                DE = Data.end();
252        DI != DE; ++DI)
253     (*DI)->print(O);
254 }
255 #endif