DwarfAccelTable.cpp revision c545322c276f933759e4140027e5f84e480d15d6
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 "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"
21#include "DIE.h"
22
23using namespace llvm;
24
25const char *DwarfAccelTable::Atom::AtomTypeString(enum AtomType AT) {
26  switch (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";
34  }
35}
36
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.
40DwarfAccelTable::DwarfAccelTable(DwarfAccelTable::Atom atom) :
41  Header(12),
42  HeaderData(atom) {
43}
44
45void DwarfAccelTable::AddName(StringRef Name, DIE* die) {
46  // If the string is in the list already then add this die to the list
47  // otherwise add a new one.
48  DIEArray &DIEs = Entries[Name];
49  DIEs.push_back(die);
50}
51
52void DwarfAccelTable::ComputeBucketCount(void) {
53  // First get the number of unique hashes.
54  std::vector<uint32_t> uniques;
55  uniques.resize(Data.size());
56  for (size_t i = 0; i < Data.size(); ++i)
57    uniques[i] = Data[i]->HashValue;
58  std::sort(uniques.begin(), uniques.end());
59  std::vector<uint32_t>::iterator p =
60    std::unique(uniques.begin(), uniques.end());
61  uint32_t num = std::distance(uniques.begin(), p);
62
63  // Then compute the bucket size, minimum of 1 bucket.
64  if (num > 1024) Header.bucket_count = num/4;
65  if (num > 16) Header.bucket_count = num/2;
66  else Header.bucket_count = num > 0 ? num : 1;
67
68  Header.hashes_count = num;
69}
70
71void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) {
72  // Create the individual hash data outputs.
73  for (StringMap<DIEArray>::const_iterator
74         EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
75    struct HashData *Entry = new HashData((*EI).getKeyData());
76    for (DIEArray::const_iterator DI = (*EI).second.begin(),
77           DE = (*EI).second.end();
78         DI != DE; ++DI)
79      Entry->addOffset((*DI)->getOffset());
80    Data.push_back(Entry);
81  }
82
83  // Figure out how many buckets we need, then compute the bucket
84  // contents and the final ordering. We'll emit the hashes and offsets
85  // by doing a walk during the emission phase. We add temporary
86  // symbols to the data so that we can reference them during the offset
87  // later, we'll emit them when we emit the data.
88  ComputeBucketCount();
89
90  // Compute bucket contents and final ordering.
91  Buckets.resize(Header.bucket_count);
92  for (size_t i = 0; i < Data.size(); ++i) {
93    uint32_t bucket = Data[i]->HashValue % Header.bucket_count;
94    Buckets[bucket].push_back(Data[i]);
95    Data[i]->Sym = Asm->GetTempSymbol(Prefix, i);
96  }
97}
98
99// Emits the header for the table via the AsmPrinter.
100void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) {
101  Asm->OutStreamer.AddComment("Header Magic");
102  Asm->EmitInt32(Header.magic);
103  Asm->OutStreamer.AddComment("Header Version");
104  Asm->EmitInt16(Header.version);
105  Asm->OutStreamer.AddComment("Header Hash Function");
106  Asm->EmitInt16(Header.hash_function);
107  Asm->OutStreamer.AddComment("Header Bucket Count");
108  Asm->EmitInt32(Header.bucket_count);
109  Asm->OutStreamer.AddComment("Header Hash Count");
110  Asm->EmitInt32(Header.hashes_count);
111  Asm->OutStreamer.AddComment("Header Data Length");
112  Asm->EmitInt32(Header.header_data_len);
113  Asm->OutStreamer.AddComment("HeaderData Die Offset Base");
114  Asm->EmitInt32(HeaderData.die_offset_base);
115  Asm->OutStreamer.AddComment("HeaderData Atom Count");
116  Asm->EmitInt32(HeaderData.Atoms.size());
117  for (size_t i = 0; i < HeaderData.Atoms.size(); i++) {
118    Atom A = HeaderData.Atoms[i];
119    Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type));
120    Asm->EmitInt16(A.type);
121    Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form));
122    Asm->EmitInt16(A.form);
123  }
124}
125
126// Walk through and emit the buckets for the table. This will look
127// like a list of numbers of how many elements are in each bucket.
128void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) {
129  unsigned index = 0;
130  for (size_t i = 0; i < Buckets.size(); ++i) {
131    Asm->OutStreamer.AddComment("Bucket " + Twine(i));
132    if (Buckets[i].size() != 0)
133      Asm->EmitInt32(index);
134    else
135      Asm->EmitInt32(UINT32_MAX);
136    index += Buckets[i].size();
137  }
138}
139
140// Walk through the buckets and emit the individual hashes for each
141// bucket.
142void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) {
143  for (size_t i = 0; i < Buckets.size(); ++i) {
144    for (HashList::const_iterator HI = Buckets[i].begin(),
145           HE = Buckets[i].end(); HI != HE; ++HI) {
146      Asm->OutStreamer.AddComment("Hash in Bucket " + Twine(i));
147      Asm->EmitInt32((*HI)->HashValue);
148    }
149  }
150}
151
152// Walk through the buckets and emit the individual offsets for each
153// element in each bucket. This is done via a symbol subtraction from the
154// beginning of the section. The non-section symbol will be output later
155// when we emit the actual data.
156void DwarfAccelTable::EmitOffsets(AsmPrinter *Asm, MCSymbol *SecBegin) {
157  for (size_t i = 0; i < Buckets.size(); ++i) {
158    for (HashList::const_iterator HI = Buckets[i].begin(),
159           HE = Buckets[i].end(); HI != HE; ++HI) {
160      Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i));
161      MCContext &Context = Asm->OutStreamer.getContext();
162      const MCExpr *Sub =
163        MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context),
164                                MCSymbolRefExpr::Create(SecBegin, Context),
165                                Context);
166      Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0);
167    }
168  }
169}
170
171// Walk through the buckets and emit the full data for each element in
172// the bucket. For the string case emit the dies and the various offsets.
173// Terminate each HashData bucket with 0.
174void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) {
175  uint64_t PrevHash = UINT64_MAX;
176  for (size_t i = 0; i < Buckets.size(); ++i) {
177    for (HashList::const_iterator HI = Buckets[i].begin(),
178           HE = Buckets[i].end(); 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->getStringPoolEntry((*HI)->Str),
183                             D->getStringPool());
184      Asm->OutStreamer.AddComment("Num DIEs");
185      Asm->EmitInt32((*HI)->DIEOffsets.size());
186      for (std::vector<uint32_t>::const_iterator
187             DI = (*HI)->DIEOffsets.begin(), DE = (*HI)->DIEOffsets.end();
188           DI != DE; ++DI) {
189        Asm->EmitInt32((*DI));
190      }
191      // Emit a 0 to terminate the data unless we have a hash collision.
192      if (PrevHash != (*HI)->HashValue)
193        Asm->EmitInt32(0);
194      PrevHash = (*HI)->HashValue;
195    }
196  }
197}
198
199// Emit the entire data structure to the output file.
200void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin,
201                           DwarfDebug *D) {
202  // Emit the header.
203  EmitHeader(Asm);
204
205  // Emit the buckets.
206  EmitBuckets(Asm);
207
208  // Emit the hashes.
209  EmitHashes(Asm);
210
211  // Emit the offsets.
212  EmitOffsets(Asm, SecBegin);
213
214  // Emit the hash data.
215  EmitData(Asm, D);
216}
217
218#ifndef NDEBUG
219void DwarfAccelTable::print(raw_ostream &O) {
220
221  Header.print(O);
222  HeaderData.print(O);
223
224  O << "Entries: \n";
225  for (StringMap<DIEArray>::const_iterator
226         EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) {
227    O << "Name: " << (*EI).getKeyData() << "\n";
228    for (DIEArray::const_iterator DI = (*EI).second.begin(),
229           DE = (*EI).second.end();
230         DI != DE; ++DI)
231      (*DI)->print(O);
232  }
233
234  O << "Buckets and Hashes: \n";
235  for (size_t i = 0; i < Buckets.size(); ++i)
236    for (HashList::const_iterator HI = Buckets[i].begin(),
237           HE = Buckets[i].end(); HI != HE; ++HI)
238      (*HI)->print(O);
239
240  O << "Data: \n";
241    for (std::vector<HashData*>::const_iterator
242           DI = Data.begin(), DE = Data.end(); DI != DE; ++DI)
243      (*DI)->print(O);
244
245
246}
247#endif
248