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"
29 // The dwarf accelerator tables are an indirect hash table optimized
30 // for null lookup rather than access to known data. They are output into
31 // an on-disk format that looks like this:
45 // where the header contains a magic number, version, type of hash function,
46 // the number of buckets, total number of hashes, and room for a special
47 // struct of data and the length of that struct.
49 // The buckets contain an index (e.g. 6) into the hashes array. The hashes
50 // section contains all of the 32-bit hash values in contiguous memory, and
51 // the offsets contain the offset into the data area for the particular
54 // For a lookup example, we could hash a function name and take it modulo the
55 // number of buckets giving us our bucket. From there we take the bucket value
56 // as an index into the hashes table and look at each successive hash as long
57 // as the hash value is still the same modulo result (bucket value) as earlier.
58 // If we have a match we look at that same entry in the offsets table and
59 // grab the offset in the data for our final match.
67 class DwarfAccelTable {
69 enum HashFunctionType {
73 static uint32_t HashDJB (StringRef Str) {
75 for (unsigned i = 0, e = Str.size(); i != e; ++i)
76 h = ((h << 5) + h) + Str[i];
80 // Helper function to compute the number of buckets needed based on
81 // the number of unique hashes.
82 void ComputeBucketCount (void);
85 uint32_t magic; // 'HASH' magic value to allow endian detection
86 uint16_t version; // Version number.
87 uint16_t hash_function; // The hash function enumeration that was used.
88 uint32_t bucket_count; // The number of buckets in this hash table.
89 uint32_t hashes_count; // The total number of unique hash values
90 // and hash data offsets in this table.
91 uint32_t header_data_len; // The bytes to skip to get to the hash
92 // indexes (buckets) for correct alignment.
93 // Also written to disk is the implementation specific header data.
95 static const uint32_t MagicHash = 0x48415348;
97 TableHeader (uint32_t data_len) :
98 magic (MagicHash), version (1), hash_function (eHashFunctionDJB),
99 bucket_count (0), hashes_count (0), header_data_len (data_len)
103 void print(raw_ostream &O) {
104 O << "Magic: " << format("0x%x", magic) << "\n"
105 << "Version: " << version << "\n"
106 << "Hash Function: " << hash_function << "\n"
107 << "Bucket Count: " << bucket_count << "\n"
108 << "Header Data Length: " << header_data_len << "\n";
110 void dump() { print(dbgs()); }
115 // The HeaderData describes the form of each set of data. In general this
116 // is as a list of atoms (atom_count) where each atom contains a type
117 // (AtomType type) of data, and an encoding form (form). In the case of
118 // data that is referenced via DW_FORM_ref_* the die_offset_base is
119 // used to describe the offset for all forms in the list of atoms.
120 // This also serves as a public interface of sorts.
121 // When written to disk this will have the form:
123 // uint32_t die_offset_base
124 // uint32_t atom_count
128 eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
129 eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that
130 // contains the item in question
131 eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as
132 // DW_FORM_data1 (if no tags exceed 255) or
134 eAtomTypeNameFlags = 4u, // Flags from enum NameFlags
135 eAtomTypeTypeFlags = 5u // Flags from enum TypeFlags
139 eTypeFlagClassMask = 0x0000000fu,
141 // Always set for C++, only set for ObjC if this is the
142 // @implementation for a class.
143 eTypeFlagClassIsImplementation = ( 1u << 1 )
146 // Make these public so that they can be used as a general interface to
149 AtomType type; // enum AtomType
150 uint16_t form; // DWARF DW_FORM_ defines
152 Atom(AtomType type, uint16_t form) : type(type), form(form) {}
153 static const char * AtomTypeString(enum AtomType);
155 void print(raw_ostream &O) {
156 O << "Type: " << AtomTypeString(type) << "\n"
157 << "Form: " << dwarf::FormEncodingString(form) << "\n";
166 struct TableHeaderData {
168 uint32_t die_offset_base;
169 std::vector<Atom> Atoms;
171 TableHeaderData(std::vector<DwarfAccelTable::Atom> &AtomList,
172 uint32_t offset = 0) :
173 die_offset_base(offset) {
174 for (size_t i = 0, e = AtomList.size(); i != e; ++i)
175 Atoms.push_back(AtomList[i]);
178 TableHeaderData(DwarfAccelTable::Atom Atom, uint32_t offset = 0)
179 : die_offset_base(offset) {
180 Atoms.push_back(Atom);
184 void print (raw_ostream &O) {
185 O << "die_offset_base: " << die_offset_base << "\n";
186 for (size_t i = 0; i < Atoms.size(); i++)
195 // The data itself consists of a str_offset, a count of the DIEs in the
196 // hash and the offsets to the DIEs themselves.
197 // On disk each data section is ended with a 0 KeyType as the end of the
199 // On output this looks like:
200 // uint32_t str_offset
201 // uint32_t hash_data_count
202 // HashData[hash_data_count]
204 struct HashDataContents {
206 char Flags; // Specific flags to output
208 HashDataContents(DIE *D, char Flags) :
212 void print(raw_ostream &O) const {
213 O << " Offset: " << Die->getOffset() << "\n";
214 O << " Tag: " << dwarf::TagString(Die->getTag()) << "\n";
215 O << " Flags: " << Flags << "\n";
224 std::vector<struct HashDataContents*> Data; // offsets
225 HashData(StringRef S) : Str(S) {
226 HashValue = DwarfAccelTable::HashDJB(S);
228 void addData(struct HashDataContents *Datum) { Data.push_back(Datum); }
230 void print(raw_ostream &O) {
231 O << "Name: " << Str << "\n";
232 O << " Hash Value: " << format("0x%x", HashValue) << "\n";
234 if (Sym) Sym->print(O);
237 for (size_t i = 0; i < Data.size(); i++) {
238 O << " Offset: " << Data[i]->Die->getOffset() << "\n";
239 O << " Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n";
240 O << " Flags: " << Data[i]->Flags << "\n";
249 DwarfAccelTable(const DwarfAccelTable&); // DO NOT IMPLEMENT
250 void operator=(const DwarfAccelTable&); // DO NOT IMPLEMENT
252 // Internal Functions
253 void EmitHeader(AsmPrinter *);
254 void EmitBuckets(AsmPrinter *);
255 void EmitHashes(AsmPrinter *);
256 void EmitOffsets(AsmPrinter *, MCSymbol *);
257 void EmitData(AsmPrinter *, DwarfDebug *D);
261 TableHeaderData HeaderData;
262 std::vector<HashData*> Data;
265 typedef std::vector<struct HashDataContents*> DataArray;
266 typedef StringMap<DataArray> StringEntries;
267 StringEntries Entries;
269 // Buckets/Hashes/Offsets
270 typedef std::vector<HashData*> HashList;
271 typedef std::vector<HashList> BucketList;
275 // Public Implementation
277 DwarfAccelTable(DwarfAccelTable::Atom);
278 DwarfAccelTable(std::vector<DwarfAccelTable::Atom> &);
280 void AddName(StringRef, DIE*, char = 0);
281 void FinalizeTable(AsmPrinter *, const char *);
282 void Emit(AsmPrinter *, MCSymbol *, DwarfDebug *);
284 void print(raw_ostream &O);
285 void dump() { print(dbgs()); }