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