Support standard DWARF TLS opcode; Darwin and PS4 use it.
[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 "DwarfCompileUnit.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/CodeGen/DIE.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/Debug.h"
25
26 using namespace llvm;
27
28 // The length of the header data is always going to be 4 + 4 + 4*NumAtoms.
29 DwarfAccelTable::DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom> atomList)
30     : Header(8 + (atomList.size() * 4)), HeaderData(atomList),
31       Entries(Allocator) {}
32
33 void DwarfAccelTable::AddName(StringRef Name, MCSymbol *StrSym, const DIE *die,
34                               char Flags) {
35   assert(Data.empty() && "Already finalized!");
36   // If the string is in the list already then add this die to the list
37   // otherwise add a new one.
38   DataArray &DIEs = Entries[Name];
39   assert(!DIEs.StrSym || DIEs.StrSym == StrSym);
40   DIEs.StrSym = StrSym;
41   DIEs.Values.push_back(new (Allocator) HashDataContents(die, Flags));
42 }
43
44 void DwarfAccelTable::ComputeBucketCount(void) {
45   // First get the number of unique hashes.
46   std::vector<uint32_t> uniques(Data.size());
47   for (size_t i = 0, e = Data.size(); i < e; ++i)
48     uniques[i] = Data[i]->HashValue;
49   array_pod_sort(uniques.begin(), uniques.end());
50   std::vector<uint32_t>::iterator p =
51       std::unique(uniques.begin(), uniques.end());
52   uint32_t num = std::distance(uniques.begin(), p);
53
54   // Then compute the bucket size, minimum of 1 bucket.
55   if (num > 1024)
56     Header.bucket_count = num / 4;
57   if (num > 16)
58     Header.bucket_count = num / 2;
59   else
60     Header.bucket_count = num > 0 ? num : 1;
61
62   Header.hashes_count = num;
63 }
64
65 // compareDIEs - comparison predicate that sorts DIEs by their offset.
66 static bool compareDIEs(const DwarfAccelTable::HashDataContents *A,
67                         const DwarfAccelTable::HashDataContents *B) {
68   return A->Die->getOffset() < B->Die->getOffset();
69 }
70
71 void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, StringRef Prefix) {
72   // Create the individual hash data outputs.
73   Data.reserve(Entries.size());
74   for (StringMap<DataArray>::iterator EI = Entries.begin(), EE = Entries.end();
75        EI != EE; ++EI) {
76
77     // Unique the entries.
78     std::stable_sort(EI->second.Values.begin(), EI->second.Values.end(), compareDIEs);
79     EI->second.Values.erase(
80         std::unique(EI->second.Values.begin(), EI->second.Values.end()),
81         EI->second.Values.end());
82
83     HashData *Entry = new (Allocator) HashData(EI->getKey(), EI->second);
84     Data.push_back(Entry);
85   }
86
87   // Figure out how many buckets we need, then compute the bucket
88   // contents and the final ordering. We'll emit the hashes and offsets
89   // by doing a walk during the emission phase. We add temporary
90   // symbols to the data so that we can reference them during the offset
91   // later, we'll emit them when we emit the data.
92   ComputeBucketCount();
93
94   // Compute bucket contents and final ordering.
95   Buckets.resize(Header.bucket_count);
96   for (size_t i = 0, e = Data.size(); i < e; ++i) {
97     uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
98     Buckets[bucket].push_back(Data[i]);
99     Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
100   }
101 }
102
103 // Emits the header for the table via the AsmPrinter.
104 void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
105   Asm->OutStreamer.AddComment("Header Magic");
106   Asm->EmitInt32(Header.magic);
107   Asm->OutStreamer.AddComment("Header Version");
108   Asm->EmitInt16(Header.version);
109   Asm->OutStreamer.AddComment("Header Hash Function");
110   Asm->EmitInt16(Header.hash_function);
111   Asm->OutStreamer.AddComment("Header Bucket Count");
112   Asm->EmitInt32(Header.bucket_count);
113   Asm->OutStreamer.AddComment("Header Hash Count");
114   Asm->EmitInt32(Header.hashes_count);
115   Asm->OutStreamer.AddComment("Header Data Length");
116   Asm->EmitInt32(Header.header_data_len);
117   Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
118   Asm->EmitInt32(HeaderData.die_offset_base);
119   Asm->OutStreamer.AddComment("HeaderData Atom Count");
120   Asm->EmitInt32(HeaderData.Atoms.size());
121   for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
122     Atom A = HeaderData.Atoms[i];
123     Asm->OutStreamer.AddComment(dwarf::AtomTypeString(A.type));
124     Asm->EmitInt16(A.type);
125     Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
126     Asm->EmitInt16(A.form);
127   }
128 }
129
130 // Walk through and emit the buckets for the table. Each index is
131 // an offset into the list of hashes.
132 void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
133   unsigned index = 0;
134   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
135     Asm->OutStreamer.AddComment("Bucket " + Twine(i));
136     if (Buckets[i].size() != 0)
137       Asm->EmitInt32(index);
138     else
139       Asm->EmitInt32(UINT32_MAX);
140     index += Buckets[i].size();
141   }
142 }
143
144 // Walk through the buckets and emit the individual hashes for each
145 // bucket.
146 void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
147   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
148     for (HashList::const_iterator HI = Buckets[i].begin(),
149                                   HE = Buckets[i].end();
150          HI != HE; ++HI) {
151       Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
152       Asm->EmitInt32((*HI)->HashValue);
153     }
154   }
155 }
156
157 // Walk through the buckets and emit the individual offsets for each
158 // element in each bucket. This is done via a symbol subtraction from the
159 // beginning of the section. The non-section symbol will be output later
160 // when we emit the actual data.
161 void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
162   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
163     for (HashList::const_iterator HI = Buckets[i].begin(),
164                                   HE = Buckets[i].end();
165          HI != HE; ++HI) {
166       Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
167       MCContext &Context = Asm->OutStreamer.getContext();
168       const MCExpr *Sub = MCBinaryExpr::CreateSub(
169           MCSymbolRefExpr::Create((*HI)->Sym, Context),
170           MCSymbolRefExpr::Create(SecBegin, Context), Context);
171       Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t));
172     }
173   }
174 }
175
176 // Walk through the buckets and emit the full data for each element in
177 // the bucket. For the string case emit the dies and the various offsets.
178 // Terminate each HashData bucket with 0.
179 void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D,
180                                MCSymbol *StrSym) {
181   uint64_t PrevHash = UINT64_MAX;
182   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
183     for (HashList::const_iterator HI = Buckets[i].begin(),
184                                   HE = Buckets[i].end();
185          HI != HE; ++HI) {
186       // Remember to emit the label for our offset.
187       Asm->OutStreamer.EmitLabel((*HI)->Sym);
188       Asm->OutStreamer.AddComment((*HI)->Str);
189       Asm->EmitSectionOffset((*HI)->Data.StrSym, StrSym);
190       Asm->OutStreamer.AddComment("Num DIEs");
191       Asm->EmitInt32((*HI)->Data.Values.size());
192       for (HashDataContents *HD : (*HI)->Data.Values) {
193         // Emit the DIE offset
194         DwarfCompileUnit *CU = D->lookupUnit(HD->Die->getUnit());
195         assert(CU && "Accelerated DIE should belong to a CU.");
196         Asm->EmitInt32(HD->Die->getOffset() + CU->getDebugInfoOffset());
197         // If we have multiple Atoms emit that info too.
198         // FIXME: A bit of a hack, we either emit only one atom or all info.
199         if (HeaderData.Atoms.size() > 1) {
200           Asm->EmitInt16(HD->Die->getTag());
201           Asm->EmitInt8(HD->Flags);
202         }
203       }
204       // Emit a 0 to terminate the data unless we have a hash collision.
205       if (PrevHash != (*HI)->HashValue)
206         Asm->EmitInt32(0);
207       PrevHash = (*HI)->HashValue;
208     }
209   }
210 }
211
212 // Emit the entire data structure to the output file.
213 void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, DwarfDebug *D,
214                            MCSymbol *StrSym) {
215   // Emit the header.
216   EmitHeader(Asm);
217
218   // Emit the buckets.
219   EmitBuckets(Asm);
220
221   // Emit the hashes.
222   EmitHashes(Asm);
223
224   // Emit the offsets.
225   EmitOffsets(Asm, SecBegin);
226
227   // Emit the hash data.
228   EmitData(Asm, D, StrSym);
229 }
230
231 #ifndef NDEBUG
232 void DwarfAccelTable::print(raw_ostream &O) {
233
234   Header.print(O);
235   HeaderData.print(O);
236
237   O << "Entries: \n";
238   for (StringMap<DataArray>::const_iterator EI = Entries.begin(),
239                                             EE = Entries.end();
240        EI != EE; ++EI) {
241     O << "Name: " << EI->getKeyData() << "\n";
242     for (HashDataContents *HD : EI->second.Values)
243       HD->print(O);
244   }
245
246   O << "Buckets and Hashes: \n";
247   for (size_t i = 0, e = Buckets.size(); i < e; ++i)
248     for (HashList::const_iterator HI = Buckets[i].begin(),
249                                   HE = Buckets[i].end();
250          HI != HE; ++HI)
251       (*HI)->print(O);
252
253   O << "Data: \n";
254   for (std::vector<HashData *>::const_iterator DI = Data.begin(),
255                                                DE = Data.end();
256        DI != DE; ++DI)
257     (*DI)->print(O);
258 }
259 #endif