Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 1 | //==-- llvm/CodeGen/DwarfAccelTable.h - 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 | #ifndef CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__ |
| 15 | #define CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__ |
| 16 | |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 17 | #include "DIE.h" |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSymbol.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/DataTypes.h" |
| 22 | #include "llvm/Support/Debug.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Dwarf.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/Format.h" |
| 26 | #include "llvm/Support/FormattedStream.h" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 27 | #include <map> |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 28 | #include <vector> |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 29 | |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 30 | // The dwarf accelerator tables are an indirect hash table optimized |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 31 | // for null lookup rather than access to known data. They are output into |
| 32 | // an on-disk format that looks like this: |
| 33 | // |
| 34 | // .-------------. |
| 35 | // | HEADER | |
| 36 | // |-------------| |
| 37 | // | BUCKETS | |
| 38 | // |-------------| |
| 39 | // | HASHES | |
| 40 | // |-------------| |
| 41 | // | OFFSETS | |
| 42 | // |-------------| |
| 43 | // | DATA | |
| 44 | // `-------------' |
| 45 | // |
| 46 | // where the header contains a magic number, version, type of hash function, |
| 47 | // the number of buckets, total number of hashes, and room for a special |
| 48 | // struct of data and the length of that struct. |
| 49 | // |
| 50 | // The buckets contain an index (e.g. 6) into the hashes array. The hashes |
| 51 | // section contains all of the 32-bit hash values in contiguous memory, and |
| 52 | // the offsets contain the offset into the data area for the particular |
| 53 | // hash. |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 54 | // |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 55 | // For a lookup example, we could hash a function name and take it modulo the |
| 56 | // number of buckets giving us our bucket. From there we take the bucket value |
| 57 | // as an index into the hashes table and look at each successive hash as long |
| 58 | // as the hash value is still the same modulo result (bucket value) as earlier. |
| 59 | // If we have a match we look at that same entry in the offsets table and |
| 60 | // grab the offset in the data for our final match. |
| 61 | |
| 62 | namespace llvm { |
| 63 | |
| 64 | class AsmPrinter; |
| 65 | class DIE; |
Eric Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 66 | class DwarfUnits; |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 67 | |
Benjamin Kramer | 55c06ae | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 68 | class DwarfAccelTable { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 69 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 70 | static uint32_t HashDJB(StringRef Str) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 71 | uint32_t h = 5381; |
Eric Christopher | 2dd5e1e | 2011-11-07 21:49:35 +0000 | [diff] [blame] | 72 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 73 | h = ((h << 5) + h) + Str[i]; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 74 | return h; |
| 75 | } |
| 76 | |
| 77 | // Helper function to compute the number of buckets needed based on |
| 78 | // the number of unique hashes. |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 79 | void ComputeBucketCount(void); |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 80 | |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 81 | struct TableHeader { |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 82 | uint32_t magic; // 'HASH' magic value to allow endian detection |
| 83 | uint16_t version; // Version number. |
| 84 | uint16_t hash_function; // The hash function enumeration that was used. |
| 85 | uint32_t bucket_count; // The number of buckets in this hash table. |
| 86 | uint32_t hashes_count; // The total number of unique hash values |
| 87 | // and hash data offsets in this table. |
| 88 | uint32_t header_data_len; // The bytes to skip to get to the hash |
| 89 | // indexes (buckets) for correct alignment. |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 90 | // Also written to disk is the implementation specific header data. |
| 91 | |
| 92 | static const uint32_t MagicHash = 0x48415348; |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 93 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 94 | TableHeader(uint32_t data_len) |
Eric Christopher | 577056f | 2013-09-05 18:20:16 +0000 | [diff] [blame] | 95 | : magic(MagicHash), version(1), |
| 96 | hash_function(dwarf::DW_hash_function_djb), bucket_count(0), |
| 97 | hashes_count(0), header_data_len(data_len) {} |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 98 | |
| 99 | #ifndef NDEBUG |
| 100 | void print(raw_ostream &O) { |
| 101 | O << "Magic: " << format("0x%x", magic) << "\n" |
| 102 | << "Version: " << version << "\n" |
| 103 | << "Hash Function: " << hash_function << "\n" |
| 104 | << "Bucket Count: " << bucket_count << "\n" |
| 105 | << "Header Data Length: " << header_data_len << "\n"; |
| 106 | } |
| 107 | void dump() { print(dbgs()); } |
| 108 | #endif |
| 109 | }; |
| 110 | |
| 111 | public: |
| 112 | // The HeaderData describes the form of each set of data. In general this |
| 113 | // is as a list of atoms (atom_count) where each atom contains a type |
| 114 | // (AtomType type) of data, and an encoding form (form). In the case of |
| 115 | // data that is referenced via DW_FORM_ref_* the die_offset_base is |
| 116 | // used to describe the offset for all forms in the list of atoms. |
| 117 | // This also serves as a public interface of sorts. |
| 118 | // When written to disk this will have the form: |
| 119 | // |
| 120 | // uint32_t die_offset_base |
| 121 | // uint32_t atom_count |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 122 | // atom_count Atoms |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 123 | |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 124 | // Make these public so that they can be used as a general interface to |
| 125 | // the class. |
| 126 | struct Atom { |
Eric Christopher | 577056f | 2013-09-05 18:20:16 +0000 | [diff] [blame] | 127 | uint16_t type; // enum AtomType |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 128 | uint16_t form; // DWARF DW_FORM_ defines |
| 129 | |
Eric Christopher | 577056f | 2013-09-05 18:20:16 +0000 | [diff] [blame] | 130 | Atom(uint16_t type, uint16_t form) : type(type), form(form) {} |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 131 | #ifndef NDEBUG |
| 132 | void print(raw_ostream &O) { |
Eric Christopher | 577056f | 2013-09-05 18:20:16 +0000 | [diff] [blame] | 133 | O << "Type: " << dwarf::AtomTypeString(type) << "\n" |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 134 | << "Form: " << dwarf::FormEncodingString(form) << "\n"; |
| 135 | } |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 136 | void dump() { print(dbgs()); } |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 137 | #endif |
| 138 | }; |
| 139 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 140 | private: |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 141 | struct TableHeaderData { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 142 | uint32_t die_offset_base; |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 143 | SmallVector<Atom, 1> Atoms; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 144 | |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 145 | TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0) |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 146 | : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {} |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 147 | |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 148 | #ifndef NDEBUG |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 149 | void print(raw_ostream &O) { |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 150 | O << "die_offset_base: " << die_offset_base << "\n"; |
| 151 | for (size_t i = 0; i < Atoms.size(); i++) |
| 152 | Atoms[i].print(O); |
| 153 | } |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 154 | void dump() { print(dbgs()); } |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 155 | #endif |
| 156 | }; |
| 157 | |
Eric Christopher | 09ac3d8 | 2011-11-07 09:24:32 +0000 | [diff] [blame] | 158 | // The data itself consists of a str_offset, a count of the DIEs in the |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 159 | // hash and the offsets to the DIEs themselves. |
| 160 | // On disk each data section is ended with a 0 KeyType as the end of the |
| 161 | // hash chain. |
| 162 | // On output this looks like: |
| 163 | // uint32_t str_offset |
| 164 | // uint32_t hash_data_count |
| 165 | // HashData[hash_data_count] |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 166 | public: |
| 167 | struct HashDataContents { |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 168 | DIE *Die; // Offsets |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 169 | char Flags; // Specific flags to output |
| 170 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 171 | HashDataContents(DIE *D, char Flags) : Die(D), Flags(Flags) {} |
| 172 | #ifndef NDEBUG |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 173 | void print(raw_ostream &O) const { |
| 174 | O << " Offset: " << Die->getOffset() << "\n"; |
| 175 | O << " Tag: " << dwarf::TagString(Die->getTag()) << "\n"; |
| 176 | O << " Flags: " << Flags << "\n"; |
| 177 | } |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 178 | #endif |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 179 | }; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 180 | |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 181 | private: |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 182 | struct HashData { |
| 183 | StringRef Str; |
| 184 | uint32_t HashValue; |
| 185 | MCSymbol *Sym; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 186 | ArrayRef<HashDataContents *> Data; // offsets |
| 187 | HashData(StringRef S, ArrayRef<HashDataContents *> Data) |
| 188 | : Str(S), Data(Data) { |
Eric Christopher | 2dd5e1e | 2011-11-07 21:49:35 +0000 | [diff] [blame] | 189 | HashValue = DwarfAccelTable::HashDJB(S); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 190 | } |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 191 | #ifndef NDEBUG |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 192 | void print(raw_ostream &O) { |
| 193 | O << "Name: " << Str << "\n"; |
| 194 | O << " Hash Value: " << format("0x%x", HashValue) << "\n"; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 195 | O << " Symbol: "; |
| 196 | if (Sym) |
| 197 | Sym->print(O); |
| 198 | else |
| 199 | O << "<none>"; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 200 | O << "\n"; |
Eric Christopher | c36145f | 2012-01-06 04:35:23 +0000 | [diff] [blame] | 201 | for (size_t i = 0; i < Data.size(); i++) { |
| 202 | O << " Offset: " << Data[i]->Die->getOffset() << "\n"; |
| 203 | O << " Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n"; |
| 204 | O << " Flags: " << Data[i]->Flags << "\n"; |
| 205 | } |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 206 | } |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 207 | void dump() { print(dbgs()); } |
| 208 | #endif |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 211 | DwarfAccelTable(const DwarfAccelTable &) LLVM_DELETED_FUNCTION; |
| 212 | void operator=(const DwarfAccelTable &) LLVM_DELETED_FUNCTION; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 213 | |
| 214 | // Internal Functions |
| 215 | void EmitHeader(AsmPrinter *); |
| 216 | void EmitBuckets(AsmPrinter *); |
| 217 | void EmitHashes(AsmPrinter *); |
| 218 | void EmitOffsets(AsmPrinter *, MCSymbol *); |
Eric Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 219 | void EmitData(AsmPrinter *, DwarfUnits *D); |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 220 | |
| 221 | // Allocator for HashData and HashDataContents. |
| 222 | BumpPtrAllocator Allocator; |
| 223 | |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 224 | // Output Variables |
| 225 | TableHeader Header; |
| 226 | TableHeaderData HeaderData; |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 227 | std::vector<HashData *> Data; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 228 | |
| 229 | // String Data |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 230 | typedef std::vector<HashDataContents *> DataArray; |
| 231 | typedef StringMap<DataArray, BumpPtrAllocator &> StringEntries; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 232 | StringEntries Entries; |
| 233 | |
| 234 | // Buckets/Hashes/Offsets |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 235 | typedef std::vector<HashData *> HashList; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 236 | typedef std::vector<HashList> BucketList; |
| 237 | BucketList Buckets; |
| 238 | HashList Hashes; |
Eric Christopher | 72c1655 | 2012-12-20 21:58:40 +0000 | [diff] [blame] | 239 | |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 240 | // Public Implementation |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 241 | public: |
Benjamin Kramer | 36c38b8 | 2012-04-13 20:06:17 +0000 | [diff] [blame] | 242 | DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>); |
Eric Christopher | e77546c | 2011-11-07 21:49:28 +0000 | [diff] [blame] | 243 | ~DwarfAccelTable(); |
Eric Christopher | 5b9544b | 2013-09-05 16:46:43 +0000 | [diff] [blame] | 244 | void AddName(StringRef, DIE *, char = 0); |
Benjamin Kramer | 03406c4 | 2013-05-11 18:24:28 +0000 | [diff] [blame] | 245 | void FinalizeTable(AsmPrinter *, StringRef); |
Eric Christopher | 2e5d870 | 2012-12-20 21:58:36 +0000 | [diff] [blame] | 246 | void Emit(AsmPrinter *, MCSymbol *, DwarfUnits *); |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 247 | #ifndef NDEBUG |
| 248 | void print(raw_ostream &O); |
| 249 | void dump() { print(dbgs()); } |
| 250 | #endif |
| 251 | }; |
Eric Christopher | bcbd3a4 | 2011-11-07 09:18:42 +0000 | [diff] [blame] | 252 | } |
| 253 | #endif |