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