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 17249423Sdim#include "DIE.h" 18249423Sdim#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" 23249423Sdim#include "llvm/Support/Dwarf.h" 24234285Sdim#include "llvm/Support/ErrorHandling.h" 25234285Sdim#include "llvm/Support/Format.h" 26234285Sdim#include "llvm/Support/FormattedStream.h" 27249423Sdim#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. 54249423Sdim// 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; 66249423Sdimclass DwarfUnits; 67249423Sdim 68234285Sdimclass DwarfAccelTable { 69234285Sdim 70263508Sdim 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. 79263508Sdim void ComputeBucketCount(void); 80249423Sdim 81234285Sdim struct TableHeader { 82263508Sdim uint32_t magic; // 'HASH' magic value to allow endian detection 83263508Sdim uint16_t version; // Version number. 84263508Sdim uint16_t hash_function; // The hash function enumeration that was used. 85263508Sdim uint32_t bucket_count; // The number of buckets in this hash table. 86263508Sdim uint32_t hashes_count; // The total number of unique hash values 87263508Sdim // and hash data offsets in this table. 88263508Sdim uint32_t header_data_len; // The bytes to skip to get to the hash 89263508Sdim // indexes (buckets) for correct alignment. 90234285Sdim // Also written to disk is the implementation specific header data. 91234285Sdim 92234285Sdim static const uint32_t MagicHash = 0x48415348; 93249423Sdim 94263508Sdim TableHeader(uint32_t data_len) 95263508Sdim : magic(MagicHash), version(1), 96263508Sdim hash_function(dwarf::DW_hash_function_djb), bucket_count(0), 97263508Sdim 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 122249423Sdim // 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 { 127263508Sdim uint16_t type; // enum AtomType 128234285Sdim uint16_t form; // DWARF DW_FORM_ defines 129234285Sdim 130263508Sdim Atom(uint16_t type, uint16_t form) : type(type), form(form) {} 131234285Sdim#ifndef NDEBUG 132234285Sdim void print(raw_ostream &O) { 133263508Sdim O << "Type: " << dwarf::AtomTypeString(type) << "\n" 134234285Sdim << "Form: " << dwarf::FormEncodingString(form) << "\n"; 135234285Sdim } 136263508Sdim void dump() { print(dbgs()); } 137234285Sdim#endif 138234285Sdim }; 139234285Sdim 140263508Sdimprivate: 141234285Sdim struct TableHeaderData { 142234285Sdim uint32_t die_offset_base; 143234982Sdim SmallVector<Atom, 1> Atoms; 144234285Sdim 145234982Sdim TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0) 146263508Sdim : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {} 147234982Sdim 148234285Sdim#ifndef NDEBUG 149263508Sdim 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 } 154263508Sdim 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 { 168263508Sdim DIE *Die; // Offsets 169234285Sdim char Flags; // Specific flags to output 170234285Sdim 171263508Sdim HashDataContents(DIE *D, char Flags) : Die(D), Flags(Flags) {} 172263508Sdim#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 } 178263508Sdim#endif 179234285Sdim }; 180263508Sdim 181234285Sdimprivate: 182234285Sdim struct HashData { 183234285Sdim StringRef Str; 184234285Sdim uint32_t HashValue; 185234285Sdim MCSymbol *Sym; 186263508Sdim ArrayRef<HashDataContents *> Data; // offsets 187263508Sdim HashData(StringRef S, ArrayRef<HashDataContents *> Data) 188263508Sdim : Str(S), Data(Data) { 189234285Sdim HashValue = DwarfAccelTable::HashDJB(S); 190234285Sdim } 191263508Sdim#ifndef NDEBUG 192234285Sdim void print(raw_ostream &O) { 193234285Sdim O << "Name: " << Str << "\n"; 194234285Sdim O << " Hash Value: " << format("0x%x", HashValue) << "\n"; 195263508Sdim O << " Symbol: "; 196263508Sdim if (Sym) 197263508Sdim Sym->print(O); 198263508Sdim else 199263508Sdim 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 } 207263508Sdim void dump() { print(dbgs()); } 208263508Sdim#endif 209234285Sdim }; 210234285Sdim 211263508Sdim DwarfAccelTable(const DwarfAccelTable &) LLVM_DELETED_FUNCTION; 212263508Sdim 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 *); 219249423Sdim void EmitData(AsmPrinter *, DwarfUnits *D); 220234982Sdim 221234982Sdim // Allocator for HashData and HashDataContents. 222234982Sdim BumpPtrAllocator Allocator; 223234982Sdim 224234285Sdim // Output Variables 225234285Sdim TableHeader Header; 226234285Sdim TableHeaderData HeaderData; 227263508Sdim std::vector<HashData *> Data; 228234285Sdim 229234285Sdim // String Data 230263508Sdim typedef std::vector<HashDataContents *> DataArray; 231263508Sdim typedef StringMap<DataArray, BumpPtrAllocator &> StringEntries; 232234285Sdim StringEntries Entries; 233234285Sdim 234234285Sdim // Buckets/Hashes/Offsets 235263508Sdim typedef std::vector<HashData *> HashList; 236234285Sdim typedef std::vector<HashList> BucketList; 237234285Sdim BucketList Buckets; 238234285Sdim HashList Hashes; 239249423Sdim 240234285Sdim // Public Implementation 241263508Sdimpublic: 242234982Sdim DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>); 243234285Sdim ~DwarfAccelTable(); 244263508Sdim void AddName(StringRef, DIE *, char = 0); 245263508Sdim void FinalizeTable(AsmPrinter *, StringRef); 246249423Sdim 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