1234285Sdim//==-- llvm/CodeGen/DwarfAccelTable.h - Dwarf Accelerator Tables -*- C++ -*-==// 2234285Sdim// 3234285Sdim// The LLVM Compiler Infrastructure 4234285Sdim// 5234285Sdim// This file is distributed under the University of Illinois Open Source 6234285Sdim// License. See LICENSE.TXT for details. 7234285Sdim// 8234285Sdim//===----------------------------------------------------------------------===// 9234285Sdim// 10234285Sdim// This file contains support for writing dwarf accelerator tables. 11234285Sdim// 12234285Sdim//===----------------------------------------------------------------------===// 13234285Sdim 14234285Sdim#ifndef CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__ 15234285Sdim#define CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__ 16234285Sdim 17252723Sdim#include "DIE.h" 18252723Sdim#include "llvm/ADT/ArrayRef.h" 19234285Sdim#include "llvm/ADT/StringMap.h" 20234285Sdim#include "llvm/MC/MCSymbol.h" 21234285Sdim#include "llvm/Support/DataTypes.h" 22234285Sdim#include "llvm/Support/Debug.h" 23252723Sdim#include "llvm/Support/Dwarf.h" 24234285Sdim#include "llvm/Support/ErrorHandling.h" 25234285Sdim#include "llvm/Support/Format.h" 26234285Sdim#include "llvm/Support/FormattedStream.h" 27252723Sdim#include <map> 28234285Sdim#include <vector> 29234285Sdim 30234285Sdim// The dwarf accelerator tables are an indirect hash table optimized 31234285Sdim// for null lookup rather than access to known data. They are output into 32234285Sdim// an on-disk format that looks like this: 33234285Sdim// 34234285Sdim// .-------------. 35234285Sdim// | HEADER | 36234285Sdim// |-------------| 37234285Sdim// | BUCKETS | 38234285Sdim// |-------------| 39234285Sdim// | HASHES | 40234285Sdim// |-------------| 41234285Sdim// | OFFSETS | 42234285Sdim// |-------------| 43234285Sdim// | DATA | 44234285Sdim// `-------------' 45234285Sdim// 46234285Sdim// where the header contains a magic number, version, type of hash function, 47234285Sdim// the number of buckets, total number of hashes, and room for a special 48234285Sdim// struct of data and the length of that struct. 49234285Sdim// 50234285Sdim// The buckets contain an index (e.g. 6) into the hashes array. The hashes 51234285Sdim// section contains all of the 32-bit hash values in contiguous memory, and 52234285Sdim// the offsets contain the offset into the data area for the particular 53234285Sdim// hash. 54252723Sdim// 55234285Sdim// For a lookup example, we could hash a function name and take it modulo the 56234285Sdim// number of buckets giving us our bucket. From there we take the bucket value 57234285Sdim// as an index into the hashes table and look at each successive hash as long 58234285Sdim// as the hash value is still the same modulo result (bucket value) as earlier. 59234285Sdim// If we have a match we look at that same entry in the offsets table and 60234285Sdim// grab the offset in the data for our final match. 61234285Sdim 62234285Sdimnamespace llvm { 63234285Sdim 64234285Sdimclass AsmPrinter; 65234285Sdimclass DIE; 66252723Sdimclass DwarfUnits; 67252723Sdim 68234285Sdimclass DwarfAccelTable { 69234285Sdim 70263509Sdim static uint32_t HashDJB(StringRef Str) { 71234285Sdim uint32_t h = 5381; 72234285Sdim for (unsigned i = 0, e = Str.size(); i != e; ++i) 73234285Sdim h = ((h << 5) + h) + Str[i]; 74234285Sdim return h; 75234285Sdim } 76234285Sdim 77234285Sdim // Helper function to compute the number of buckets needed based on 78234285Sdim // the number of unique hashes. 79263509Sdim void ComputeBucketCount(void); 80252723Sdim 81234285Sdim struct TableHeader { 82263509Sdim uint32_t magic; // 'HASH' magic value to allow endian detection 83263509Sdim uint16_t version; // Version number. 84263509Sdim uint16_t hash_function; // The hash function enumeration that was used. 85263509Sdim uint32_t bucket_count; // The number of buckets in this hash table. 86263509Sdim uint32_t hashes_count; // The total number of unique hash values 87263509Sdim // and hash data offsets in this table. 88263509Sdim uint32_t header_data_len; // The bytes to skip to get to the hash 89263509Sdim // indexes (buckets) for correct alignment. 90234285Sdim // Also written to disk is the implementation specific header data. 91234285Sdim 92234285Sdim static const uint32_t MagicHash = 0x48415348; 93252723Sdim 94263509Sdim TableHeader(uint32_t data_len) 95263509Sdim : magic(MagicHash), version(1), 96263509Sdim hash_function(dwarf::DW_hash_function_djb), bucket_count(0), 97263509Sdim hashes_count(0), header_data_len(data_len) {} 98234285Sdim 99234285Sdim#ifndef NDEBUG 100234285Sdim void print(raw_ostream &O) { 101234285Sdim O << "Magic: " << format("0x%x", magic) << "\n" 102234285Sdim << "Version: " << version << "\n" 103234285Sdim << "Hash Function: " << hash_function << "\n" 104234285Sdim << "Bucket Count: " << bucket_count << "\n" 105234285Sdim << "Header Data Length: " << header_data_len << "\n"; 106234285Sdim } 107234285Sdim void dump() { print(dbgs()); } 108234285Sdim#endif 109234285Sdim }; 110234285Sdim 111234285Sdimpublic: 112234285Sdim // The HeaderData describes the form of each set of data. In general this 113234285Sdim // is as a list of atoms (atom_count) where each atom contains a type 114234285Sdim // (AtomType type) of data, and an encoding form (form). In the case of 115234285Sdim // data that is referenced via DW_FORM_ref_* the die_offset_base is 116234285Sdim // used to describe the offset for all forms in the list of atoms. 117234285Sdim // This also serves as a public interface of sorts. 118234285Sdim // When written to disk this will have the form: 119234285Sdim // 120234285Sdim // uint32_t die_offset_base 121234285Sdim // uint32_t atom_count 122252723Sdim // atom_count Atoms 123234285Sdim 124234285Sdim // Make these public so that they can be used as a general interface to 125234285Sdim // the class. 126234285Sdim struct Atom { 127263509Sdim uint16_t type; // enum AtomType 128234285Sdim uint16_t form; // DWARF DW_FORM_ defines 129234285Sdim 130263509Sdim Atom(uint16_t type, uint16_t form) : type(type), form(form) {} 131234285Sdim#ifndef NDEBUG 132234285Sdim void print(raw_ostream &O) { 133263509Sdim O << "Type: " << dwarf::AtomTypeString(type) << "\n" 134234285Sdim << "Form: " << dwarf::FormEncodingString(form) << "\n"; 135234285Sdim } 136263509Sdim void dump() { print(dbgs()); } 137234285Sdim#endif 138234285Sdim }; 139234285Sdim 140263509Sdimprivate: 141234285Sdim struct TableHeaderData { 142234285Sdim uint32_t die_offset_base; 143235633Sdim SmallVector<Atom, 1> Atoms; 144234285Sdim 145235633Sdim TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0) 146263509Sdim : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {} 147235633Sdim 148234285Sdim#ifndef NDEBUG 149263509Sdim void print(raw_ostream &O) { 150234285Sdim O << "die_offset_base: " << die_offset_base << "\n"; 151234285Sdim for (size_t i = 0; i < Atoms.size(); i++) 152234285Sdim Atoms[i].print(O); 153234285Sdim } 154263509Sdim void dump() { print(dbgs()); } 155234285Sdim#endif 156234285Sdim }; 157234285Sdim 158234285Sdim // The data itself consists of a str_offset, a count of the DIEs in the 159234285Sdim // hash and the offsets to the DIEs themselves. 160234285Sdim // On disk each data section is ended with a 0 KeyType as the end of the 161234285Sdim // hash chain. 162234285Sdim // On output this looks like: 163234285Sdim // uint32_t str_offset 164234285Sdim // uint32_t hash_data_count 165234285Sdim // HashData[hash_data_count] 166234285Sdimpublic: 167234285Sdim struct HashDataContents { 168263509Sdim DIE *Die; // Offsets 169234285Sdim char Flags; // Specific flags to output 170234285Sdim 171263509Sdim HashDataContents(DIE *D, char Flags) : Die(D), Flags(Flags) {} 172263509Sdim#ifndef NDEBUG 173234285Sdim void print(raw_ostream &O) const { 174234285Sdim O << " Offset: " << Die->getOffset() << "\n"; 175234285Sdim O << " Tag: " << dwarf::TagString(Die->getTag()) << "\n"; 176234285Sdim O << " Flags: " << Flags << "\n"; 177234285Sdim } 178263509Sdim#endif 179234285Sdim }; 180263509Sdim 181234285Sdimprivate: 182234285Sdim struct HashData { 183234285Sdim StringRef Str; 184234285Sdim uint32_t HashValue; 185234285Sdim MCSymbol *Sym; 186263509Sdim ArrayRef<HashDataContents *> Data; // offsets 187263509Sdim HashData(StringRef S, ArrayRef<HashDataContents *> Data) 188263509Sdim : Str(S), Data(Data) { 189234285Sdim HashValue = DwarfAccelTable::HashDJB(S); 190234285Sdim } 191263509Sdim#ifndef NDEBUG 192234285Sdim void print(raw_ostream &O) { 193234285Sdim O << "Name: " << Str << "\n"; 194234285Sdim O << " Hash Value: " << format("0x%x", HashValue) << "\n"; 195263509Sdim O << " Symbol: "; 196263509Sdim if (Sym) 197263509Sdim Sym->print(O); 198263509Sdim else 199263509Sdim O << "<none>"; 200234285Sdim O << "\n"; 201234285Sdim for (size_t i = 0; i < Data.size(); i++) { 202234285Sdim O << " Offset: " << Data[i]->Die->getOffset() << "\n"; 203234285Sdim O << " Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n"; 204234285Sdim O << " Flags: " << Data[i]->Flags << "\n"; 205234285Sdim } 206234285Sdim } 207263509Sdim void dump() { print(dbgs()); } 208263509Sdim#endif 209234285Sdim }; 210234285Sdim 211263509Sdim DwarfAccelTable(const DwarfAccelTable &) LLVM_DELETED_FUNCTION; 212263509Sdim void operator=(const DwarfAccelTable &) LLVM_DELETED_FUNCTION; 213234285Sdim 214234285Sdim // Internal Functions 215234285Sdim void EmitHeader(AsmPrinter *); 216234285Sdim void EmitBuckets(AsmPrinter *); 217234285Sdim void EmitHashes(AsmPrinter *); 218234285Sdim void EmitOffsets(AsmPrinter *, MCSymbol *); 219252723Sdim void EmitData(AsmPrinter *, DwarfUnits *D); 220235633Sdim 221235633Sdim // Allocator for HashData and HashDataContents. 222235633Sdim BumpPtrAllocator Allocator; 223235633Sdim 224234285Sdim // Output Variables 225234285Sdim TableHeader Header; 226234285Sdim TableHeaderData HeaderData; 227263509Sdim std::vector<HashData *> Data; 228234285Sdim 229234285Sdim // String Data 230263509Sdim typedef std::vector<HashDataContents *> DataArray; 231263509Sdim typedef StringMap<DataArray, BumpPtrAllocator &> StringEntries; 232234285Sdim StringEntries Entries; 233234285Sdim 234234285Sdim // Buckets/Hashes/Offsets 235263509Sdim typedef std::vector<HashData *> HashList; 236234285Sdim typedef std::vector<HashList> BucketList; 237234285Sdim BucketList Buckets; 238234285Sdim HashList Hashes; 239252723Sdim 240234285Sdim // Public Implementation 241263509Sdimpublic: 242235633Sdim DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>); 243234285Sdim ~DwarfAccelTable(); 244263509Sdim void AddName(StringRef, DIE *, char = 0); 245263509Sdim void FinalizeTable(AsmPrinter *, StringRef); 246252723Sdim void Emit(AsmPrinter *, MCSymbol *, DwarfUnits *); 247234285Sdim#ifndef NDEBUG 248234285Sdim void print(raw_ostream &O); 249234285Sdim void dump() { print(dbgs()); } 250234285Sdim#endif 251234285Sdim}; 252234285Sdim} 253234285Sdim#endif 254