1 //==-- llvm/CodeGen/DwarfAccelTable.h - Dwarf Accelerator Tables -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf accelerator tables.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
15 #define CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormattedStream.h"
28 // The dwarf accelerator tables are an indirect hash table optimized
29 // for null lookup rather than access to known data. They are output into
30 // an on-disk format that looks like this:
44 // where the header contains a magic number, version, type of hash function,
45 // the number of buckets, total number of hashes, and room for a special
46 // struct of data and the length of that struct.
48 // The buckets contain an index (e.g. 6) into the hashes array. The hashes
49 // section contains all of the 32-bit hash values in contiguous memory, and
50 // the offsets contain the offset into the data area for the particular
53 // For a lookup example, we could hash a function name and take it modulo the
54 // number of buckets giving us our bucket. From there we take the bucket value
55 // as an index into the hashes table and look at each successive hash as long
56 // as the hash value is still the same modulo result (bucket value) as earlier.
57 // If we have a match we look at that same entry in the offsets table and
58 // grab the offset in the data for our final match.
66 class DwarfAccelTable {
68 enum HashFunctionType {
72 static uint32_t HashDJB (StringRef Str) {
74 for (unsigned i = 0, e = Str.size(); i != e; ++i)
75 h = ((h << 5) + h) + Str[i];
79 // Helper function to compute the number of buckets needed based on
80 // the number of unique hashes.
81 void ComputeBucketCount (void);
84 uint32_t magic; // 'HASH' magic value to allow endian detection
85 uint16_t version; // Version number.
86 uint16_t hash_function; // The hash function enumeration that was used.
87 uint32_t bucket_count; // The number of buckets in this hash table.
88 uint32_t hashes_count; // The total number of unique hash values
89 // and hash data offsets in this table.
90 uint32_t header_data_len; // The bytes to skip to get to the hash
91 // indexes (buckets) for correct alignment.
92 // Also written to disk is the implementation specific header data.
94 static const uint32_t MagicHash = 0x48415348;
96 TableHeader (uint32_t data_len) :
97 magic (MagicHash), version (1), hash_function (eHashFunctionDJB),
98 bucket_count (0), hashes_count (0), header_data_len (data_len)
102 void print(raw_ostream &O) {
103 O << "Magic: " << format("0x%x", magic) << "\n"
104 << "Version: " << version << "\n"
105 << "Hash Function: " << hash_function << "\n"
106 << "Bucket Count: " << bucket_count << "\n"
107 << "Header Data Length: " << header_data_len << "\n";
109 void dump() { print(dbgs()); }
114 // The HeaderData describes the form of each set of data. In general this
115 // is as a list of atoms (atom_count) where each atom contains a type
116 // (AtomType type) of data, and an encoding form (form). In the case of
117 // data that is referenced via DW_FORM_ref_* the die_offset_base is
118 // used to describe the offset for all forms in the list of atoms.
119 // This also serves as a public interface of sorts.
120 // When written to disk this will have the form:
122 // uint32_t die_offset_base
123 // uint32_t atom_count
127 eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
128 eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that
129 // contains the item in question
130 eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as
131 // DW_FORM_data1 (if no tags exceed 255) or
133 eAtomTypeNameFlags = 4u, // Flags from enum NameFlags
134 eAtomTypeTypeFlags = 5u // Flags from enum TypeFlags
137 // Make these public so that they can be used as a general interface to
140 AtomType type; // enum AtomType
141 uint16_t form; // DWARF DW_FORM_ defines
143 Atom(AtomType type, uint16_t form) : type(type), form(form) {}
144 static const char * AtomTypeString(enum AtomType);
146 void print(raw_ostream &O) {
147 O << "Type: " << dwarf::TagString(type) << "\n"
148 << "Form: " << dwarf::FormEncodingString(form) << "\n";
157 struct TableHeaderData {
159 uint32_t die_offset_base;
160 std::vector<Atom> Atoms;
162 TableHeaderData(DwarfAccelTable::Atom Atom, uint32_t offset = 0)
163 : die_offset_base(offset) {
164 Atoms.push_back(Atom);
168 void print (raw_ostream &O) {
169 O << "die_offset_base: " << die_offset_base << "\n";
170 for (size_t i = 0; i < Atoms.size(); i++)
179 // The data itself consists of a str_offset, a count of the DIEs in the
180 // hash and the offsets to the DIEs themselves.
181 // On disk each data section is ended with a 0 KeyType as the end of the
183 // On output this looks like:
184 // uint32_t str_offset
185 // uint32_t hash_data_count
186 // HashData[hash_data_count]
191 std::vector<uint32_t> DIEOffsets; // offsets
192 HashData(StringRef S) : Str(S) {
193 HashValue = DwarfAccelTable::HashDJB(S);
195 void addOffset(uint32_t off) { DIEOffsets.push_back(off); }
197 void print(raw_ostream &O) {
198 O << "Name: " << Str << "\n";
199 O << " Hash Value: " << format("0x%x", HashValue) << "\n";
201 if (Sym) Sym->print(O);
204 for (size_t i = 0; i < DIEOffsets.size(); i++)
205 O << " Offset: " << DIEOffsets[i] << "\n";
213 DwarfAccelTable(const DwarfAccelTable&); // DO NOT IMPLEMENT
214 void operator=(const DwarfAccelTable&); // DO NOT IMPLEMENT
216 // Internal Functions
217 void EmitHeader(AsmPrinter *);
218 void EmitBuckets(AsmPrinter *);
219 void EmitHashes(AsmPrinter *);
220 void EmitOffsets(AsmPrinter *, MCSymbol *);
221 void EmitData(AsmPrinter *, DwarfDebug *D);
225 TableHeaderData HeaderData;
226 std::vector<HashData*> Data;
229 typedef std::vector<DIE*> DIEArray;
230 typedef StringMap<DIEArray> StringEntries;
231 StringEntries Entries;
233 // Buckets/Hashes/Offsets
234 typedef std::vector<HashData*> HashList;
235 typedef std::vector<HashList> BucketList;
239 // Public Implementation
241 DwarfAccelTable(DwarfAccelTable::Atom Atom);
243 void AddName(StringRef, DIE*);
244 void FinalizeTable(AsmPrinter *, const char *);
245 void Emit(AsmPrinter *, MCSymbol *, DwarfDebug *);
247 void print(raw_ostream &O);
248 void dump() { print(dbgs()); }