DwarfAccelTable.cpp revision 30b4d8b83b7b3995ac1b53f35d3110d48676b187
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::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 76void DwarfAccelTable::FinalizeTable(AsmPrinter *Asm, const char *Prefix) { 77 // Create the individual hash data outputs. 78 for (StringMap<DIEArray>::const_iterator 79 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) { 80 struct HashData *Entry = new HashData((*EI).getKeyData()); 81 for (DIEArray::const_iterator DI = (*EI).second.begin(), 82 DE = (*EI).second.end(); 83 DI != DE; ++DI) 84 Entry->addOffset((*DI)->getOffset()); 85 Data.push_back(Entry); 86 } 87 88 // Figure out how many buckets we need, then compute the bucket 89 // contents and the final ordering. We'll emit the hashes and offsets 90 // by doing a walk during the emission phase. We add temporary 91 // symbols to the data so that we can reference them during the offset 92 // later, we'll emit them when we emit the data. 93 ComputeBucketCount(); 94 95 // Compute bucket contents and final ordering. 96 Buckets.resize(Header.bucket_count); 97 for (size_t i = 0, e = Data.size(); i < e; ++i) { 98 uint32_t bucket = Data[i]->HashValue % Header.bucket_count; 99 Buckets[bucket].push_back(Data[i]); 100 Data[i]->Sym = Asm->GetTempSymbol(Prefix, i); 101 } 102} 103 104// Emits the header for the table via the AsmPrinter. 105void DwarfAccelTable::EmitHeader(AsmPrinter *Asm) { 106 Asm->OutStreamer.AddComment("Header Magic"); 107 Asm->EmitInt32(Header.magic); 108 Asm->OutStreamer.AddComment("Header Version"); 109 Asm->EmitInt16(Header.version); 110 Asm->OutStreamer.AddComment("Header Hash Function"); 111 Asm->EmitInt16(Header.hash_function); 112 Asm->OutStreamer.AddComment("Header Bucket Count"); 113 Asm->EmitInt32(Header.bucket_count); 114 Asm->OutStreamer.AddComment("Header Hash Count"); 115 Asm->EmitInt32(Header.hashes_count); 116 Asm->OutStreamer.AddComment("Header Data Length"); 117 Asm->EmitInt32(Header.header_data_len); 118 Asm->OutStreamer.AddComment("HeaderData Die Offset Base"); 119 Asm->EmitInt32(HeaderData.die_offset_base); 120 Asm->OutStreamer.AddComment("HeaderData Atom Count"); 121 Asm->EmitInt32(HeaderData.Atoms.size()); 122 for (size_t i = 0; i < HeaderData.Atoms.size(); i++) { 123 Atom A = HeaderData.Atoms[i]; 124 Asm->OutStreamer.AddComment(Atom::AtomTypeString(A.type)); 125 Asm->EmitInt16(A.type); 126 Asm->OutStreamer.AddComment(dwarf::FormEncodingString(A.form)); 127 Asm->EmitInt16(A.form); 128 } 129} 130 131// Walk through and emit the buckets for the table. This will look 132// like a list of numbers of how many elements are in each bucket. 133void DwarfAccelTable::EmitBuckets(AsmPrinter *Asm) { 134 unsigned index = 0; 135 for (size_t i = 0, e = Buckets.size(); i < e; ++i) { 136 Asm->OutStreamer.AddComment("Bucket " + Twine(i)); 137 if (Buckets[i].size() != 0) 138 Asm->EmitInt32(index); 139 else 140 Asm->EmitInt32(UINT32_MAX); 141 index += Buckets[i].size(); 142 } 143} 144 145// Walk through the buckets and emit the individual hashes for each 146// bucket. 147void DwarfAccelTable::EmitHashes(AsmPrinter *Asm) { 148 for (size_t i = 0, e = Buckets.size(); i < e; ++i) { 149 for (HashList::const_iterator HI = Buckets[i].begin(), 150 HE = Buckets[i].end(); 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. 161void 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(); HI != HE; ++HI) { 165 Asm->OutStreamer.AddComment("Offset in Bucket " + Twine(i)); 166 MCContext &Context = Asm->OutStreamer.getContext(); 167 const MCExpr *Sub = 168 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create((*HI)->Sym, Context), 169 MCSymbolRefExpr::Create(SecBegin, Context), 170 Context); 171 Asm->OutStreamer.EmitValue(Sub, sizeof(uint32_t), 0); 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. 179void DwarfAccelTable::EmitData(AsmPrinter *Asm, DwarfDebug *D) { 180 uint64_t PrevHash = UINT64_MAX; 181 for (size_t i = 0, e = Buckets.size(); i < e; ++i) { 182 for (HashList::const_iterator HI = Buckets[i].begin(), 183 HE = Buckets[i].end(); HI != HE; ++HI) { 184 // Remember to emit the label for our offset. 185 Asm->OutStreamer.EmitLabel((*HI)->Sym); 186 Asm->OutStreamer.AddComment((*HI)->Str); 187 Asm->EmitSectionOffset(D->getStringPoolEntry((*HI)->Str), 188 D->getStringPool()); 189 Asm->OutStreamer.AddComment("Num DIEs"); 190 Asm->EmitInt32((*HI)->DIEOffsets.size()); 191 for (std::vector<uint32_t>::const_iterator 192 DI = (*HI)->DIEOffsets.begin(), DE = (*HI)->DIEOffsets.end(); 193 DI != DE; ++DI) { 194 Asm->EmitInt32((*DI)); 195 } 196 // Emit a 0 to terminate the data unless we have a hash collision. 197 if (PrevHash != (*HI)->HashValue) 198 Asm->EmitInt32(0); 199 PrevHash = (*HI)->HashValue; 200 } 201 } 202} 203 204// Emit the entire data structure to the output file. 205void DwarfAccelTable::Emit(AsmPrinter *Asm, MCSymbol *SecBegin, 206 DwarfDebug *D) { 207 // Emit the header. 208 EmitHeader(Asm); 209 210 // Emit the buckets. 211 EmitBuckets(Asm); 212 213 // Emit the hashes. 214 EmitHashes(Asm); 215 216 // Emit the offsets. 217 EmitOffsets(Asm, SecBegin); 218 219 // Emit the hash data. 220 EmitData(Asm, D); 221} 222 223#ifndef NDEBUG 224void DwarfAccelTable::print(raw_ostream &O) { 225 226 Header.print(O); 227 HeaderData.print(O); 228 229 O << "Entries: \n"; 230 for (StringMap<DIEArray>::const_iterator 231 EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) { 232 O << "Name: " << (*EI).getKeyData() << "\n"; 233 for (DIEArray::const_iterator DI = (*EI).second.begin(), 234 DE = (*EI).second.end(); 235 DI != DE; ++DI) 236 (*DI)->print(O); 237 } 238 239 O << "Buckets and Hashes: \n"; 240 for (size_t i = 0, e = Buckets.size(); i < e; ++i) 241 for (HashList::const_iterator HI = Buckets[i].begin(), 242 HE = Buckets[i].end(); HI != HE; ++HI) 243 (*HI)->print(O); 244 245 O << "Data: \n"; 246 for (std::vector<HashData*>::const_iterator 247 DI = Data.begin(), DE = Data.end(); DI != DE; ++DI) 248 (*DI)->print(O); 249 250 251} 252#endif 253