Reformat.
[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 const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
28   switch (AT) {
29   case eAtomTypeNULL:
30     return "eAtomTypeNULL";
31   case eAtomTypeDIEOffset:
32     return "eAtomTypeDIEOffset";
33   case eAtomTypeCUOffset:
34     return "eAtomTypeCUOffset";
35   case eAtomTypeTag:
36     return "eAtomTypeTag";
37   case eAtomTypeNameFlags:
38     return "eAtomTypeNameFlags";
39   case eAtomTypeTypeFlags:
40     return "eAtomTypeTypeFlags";
41   }
42   llvm_unreachable("invalid AtomType!");
43 }
44
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),
48       Entries(Allocator) {}
49
50 DwarfAccelTable::~DwarfAccelTable() {}
51
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));
58 }
59
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);
69
70   // Then compute the bucket size, minimum of 1 bucket.
71   if (num > 1024)
72     Header.bucket_count = num / 4;
73   if (num > 16)
74     Header.bucket_count = num / 2;
75   else
76     Header.bucket_count = num > 0 ? num : 1;
77
78   Header.hashes_count = num;
79 }
80
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();
85 }
86
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();
90        EI != EE; ++EI) {
91
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()),
95                      EI->second.end());
96
97     HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
98     Data.push_back(Entry);
99   }
100
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();
107
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);
114   }
115 }
116
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);
141   }
142 }
143
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) {
147   unsigned index = 0;
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);
152     else
153       Asm->EmitInt32(UINT32_MAX);
154     index += Buckets[i].size();
155   }
156 }
157
158 // Walk through the buckets and emit the individual hashes for each
159 // bucket.
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();
164          HI != HE; ++HI) {
165       Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
166       Asm->EmitInt32((*HI)->HashValue);
167     }
168   }
169 }
170
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();
179          HI != HE; ++HI) {
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));
186     }
187   }
188 }
189
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();
198          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),
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();
209            DI != DE; ++DI) {
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);
217         }
218       }
219       // Emit a 0 to terminate the data unless we have a hash collision.
220       if (PrevHash != (*HI)->HashValue)
221         Asm->EmitInt32(0);
222       PrevHash = (*HI)->HashValue;
223     }
224   }
225 }
226
227 // Emit the entire data structure to the output file.
228 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfUnits *D) {
229   // Emit the header.
230   EmitHeader(Asm);
231
232   // Emit the buckets.
233   EmitBuckets(Asm);
234
235   // Emit the hashes.
236   EmitHashes(Asm);
237
238   // Emit the offsets.
239   EmitOffsets(Asm, SecBegin);
240
241   // Emit the hash data.
242   EmitData(Asm, D);
243 }
244
245 #ifndef NDEBUG
246 void DwarfAccelTable::print(raw_ostream &O) {
247
248   Header.print(O);
249   HeaderData.print(O);
250
251   O << "Entries: \n";
252   for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
253                                             EE = Entries.end();
254        EI != EE; ++EI) {
255     O << "Name: " << EI->getKeyData() << "\n";
256     for (DataArray::const_iterator DI = EI->second.begin(),
257                                    DE = EI->second.end();
258          DI != DE; ++DI)
259       (*DI)->print(O);
260   }
261
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();
266          HI != HE; ++HI)
267       (*HI)->print(O);
268
269   O << "Data: \n";
270   for (std::vector<HashData *>::const_iterator DI = Data.begin(),
271                                                DE = Data.end();
272        DI != DE; ++DI)
273     (*DI)->print(O);
274 }
275 #endif