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