DwarfAccelTable.h revision 234982
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
17234285Sdim#include "llvm/ADT/StringMap.h"
18234982Sdim#include "llvm/ADT/ArrayRef.h"
19234285Sdim#include "llvm/MC/MCSymbol.h"
20234285Sdim#include "llvm/Support/Dwarf.h"
21234285Sdim#include "llvm/Support/DataTypes.h"
22234285Sdim#include "llvm/Support/Debug.h"
23234285Sdim#include "llvm/Support/ErrorHandling.h"
24234285Sdim#include "llvm/Support/Format.h"
25234285Sdim#include "llvm/Support/FormattedStream.h"
26234285Sdim#include "DIE.h"
27234285Sdim#include <vector>
28234285Sdim#include <map>
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.
54234285Sdim//
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;
66234285Sdimclass DwarfDebug;
67234285Sdim
68234285Sdimclass DwarfAccelTable {
69234285Sdim
70234285Sdim  enum HashFunctionType {
71234285Sdim    eHashFunctionDJB = 0u
72234285Sdim  };
73234285Sdim
74234285Sdim  static uint32_t HashDJB (StringRef Str) {
75234285Sdim    uint32_t h = 5381;
76234285Sdim    for (unsigned i = 0, e = Str.size(); i != e; ++i)
77234285Sdim      h = ((h << 5) + h) + Str[i];
78234285Sdim    return h;
79234285Sdim  }
80234285Sdim
81234285Sdim  // Helper function to compute the number of buckets needed based on
82234285Sdim  // the number of unique hashes.
83234285Sdim  void ComputeBucketCount (void);
84234285Sdim
85234285Sdim  struct TableHeader {
86234285Sdim    uint32_t   magic;           // 'HASH' magic value to allow endian detection
87234285Sdim    uint16_t   version;         // Version number.
88234285Sdim    uint16_t   hash_function;   // The hash function enumeration that was used.
89234285Sdim    uint32_t   bucket_count;    // The number of buckets in this hash table.
90234285Sdim    uint32_t   hashes_count;    // The total number of unique hash values
91234285Sdim                                // and hash data offsets in this table.
92234285Sdim    uint32_t   header_data_len; // The bytes to skip to get to the hash
93234285Sdim                                // indexes (buckets) for correct alignment.
94234285Sdim    // Also written to disk is the implementation specific header data.
95234285Sdim
96234285Sdim    static const uint32_t MagicHash = 0x48415348;
97234285Sdim
98234285Sdim    TableHeader (uint32_t data_len) :
99234285Sdim      magic (MagicHash), version (1), hash_function (eHashFunctionDJB),
100234285Sdim      bucket_count (0), hashes_count (0), header_data_len (data_len)
101234285Sdim    {}
102234285Sdim
103234285Sdim#ifndef NDEBUG
104234285Sdim    void print(raw_ostream &O) {
105234285Sdim      O << "Magic: " << format("0x%x", magic) << "\n"
106234285Sdim        << "Version: " << version << "\n"
107234285Sdim        << "Hash Function: " << hash_function << "\n"
108234285Sdim        << "Bucket Count: " << bucket_count << "\n"
109234285Sdim        << "Header Data Length: " << header_data_len << "\n";
110234285Sdim    }
111234285Sdim    void dump() { print(dbgs()); }
112234285Sdim#endif
113234285Sdim  };
114234285Sdim
115234285Sdimpublic:
116234285Sdim  // The HeaderData describes the form of each set of data. In general this
117234285Sdim  // is as a list of atoms (atom_count) where each atom contains a type
118234285Sdim  // (AtomType type) of data, and an encoding form (form). In the case of
119234285Sdim  // data that is referenced via DW_FORM_ref_* the die_offset_base is
120234285Sdim  // used to describe the offset for all forms in the list of atoms.
121234285Sdim  // This also serves as a public interface of sorts.
122234285Sdim  // When written to disk this will have the form:
123234285Sdim  //
124234285Sdim  // uint32_t die_offset_base
125234285Sdim  // uint32_t atom_count
126234285Sdim  // atom_count Atoms
127234285Sdim  enum AtomType {
128234285Sdim    eAtomTypeNULL       = 0u,
129234285Sdim    eAtomTypeDIEOffset  = 1u,   // DIE offset, check form for encoding
130234285Sdim    eAtomTypeCUOffset   = 2u,   // DIE offset of the compiler unit header that
131234285Sdim                                // contains the item in question
132234285Sdim    eAtomTypeTag        = 3u,   // DW_TAG_xxx value, should be encoded as
133234285Sdim                                // DW_FORM_data1 (if no tags exceed 255) or
134234285Sdim                                // DW_FORM_data2.
135234285Sdim    eAtomTypeNameFlags  = 4u,   // Flags from enum NameFlags
136234285Sdim    eAtomTypeTypeFlags  = 5u    // Flags from enum TypeFlags
137234285Sdim  };
138234285Sdim
139234285Sdim  enum TypeFlags {
140234285Sdim    eTypeFlagClassMask = 0x0000000fu,
141234285Sdim
142234285Sdim    // Always set for C++, only set for ObjC if this is the
143234285Sdim    // @implementation for a class.
144234285Sdim    eTypeFlagClassIsImplementation  = ( 1u << 1 )
145234285Sdim  };
146234285Sdim
147234285Sdim  // Make these public so that they can be used as a general interface to
148234285Sdim  // the class.
149234285Sdim  struct Atom {
150234285Sdim    AtomType type; // enum AtomType
151234285Sdim    uint16_t form; // DWARF DW_FORM_ defines
152234285Sdim
153234285Sdim    Atom(AtomType type, uint16_t form) : type(type), form(form) {}
154234285Sdim    static const char * AtomTypeString(enum AtomType);
155234285Sdim#ifndef NDEBUG
156234285Sdim    void print(raw_ostream &O) {
157234285Sdim      O << "Type: " << AtomTypeString(type) << "\n"
158234285Sdim        << "Form: " << dwarf::FormEncodingString(form) << "\n";
159234285Sdim    }
160234285Sdim    void dump() {
161234285Sdim      print(dbgs());
162234285Sdim    }
163234285Sdim#endif
164234285Sdim  };
165234285Sdim
166234285Sdim private:
167234285Sdim  struct TableHeaderData {
168234285Sdim    uint32_t die_offset_base;
169234982Sdim    SmallVector<Atom, 1> Atoms;
170234285Sdim
171234982Sdim    TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0)
172234982Sdim      : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) { }
173234982Sdim
174234285Sdim#ifndef NDEBUG
175234285Sdim    void print (raw_ostream &O) {
176234285Sdim      O << "die_offset_base: " << die_offset_base << "\n";
177234285Sdim      for (size_t i = 0; i < Atoms.size(); i++)
178234285Sdim        Atoms[i].print(O);
179234285Sdim    }
180234285Sdim    void dump() {
181234285Sdim      print(dbgs());
182234285Sdim    }
183234285Sdim#endif
184234285Sdim  };
185234285Sdim
186234285Sdim  // The data itself consists of a str_offset, a count of the DIEs in the
187234285Sdim  // hash and the offsets to the DIEs themselves.
188234285Sdim  // On disk each data section is ended with a 0 KeyType as the end of the
189234285Sdim  // hash chain.
190234285Sdim  // On output this looks like:
191234285Sdim  // uint32_t str_offset
192234285Sdim  // uint32_t hash_data_count
193234285Sdim  // HashData[hash_data_count]
194234285Sdimpublic:
195234285Sdim  struct HashDataContents {
196234285Sdim    DIE *Die; // Offsets
197234285Sdim    char Flags; // Specific flags to output
198234285Sdim
199234285Sdim    HashDataContents(DIE *D, char Flags) :
200234285Sdim      Die(D),
201234285Sdim      Flags(Flags) { }
202234285Sdim    #ifndef NDEBUG
203234285Sdim    void print(raw_ostream &O) const {
204234285Sdim      O << "  Offset: " << Die->getOffset() << "\n";
205234285Sdim      O << "  Tag: " << dwarf::TagString(Die->getTag()) << "\n";
206234285Sdim      O << "  Flags: " << Flags << "\n";
207234285Sdim    }
208234285Sdim    #endif
209234285Sdim  };
210234285Sdimprivate:
211234285Sdim  struct HashData {
212234285Sdim    StringRef Str;
213234285Sdim    uint32_t HashValue;
214234285Sdim    MCSymbol *Sym;
215234982Sdim    ArrayRef<HashDataContents*> Data; // offsets
216234982Sdim    HashData(StringRef S, ArrayRef<HashDataContents*> Data)
217234982Sdim      : Str(S), Data(Data) {
218234285Sdim      HashValue = DwarfAccelTable::HashDJB(S);
219234285Sdim    }
220234285Sdim    #ifndef NDEBUG
221234285Sdim    void print(raw_ostream &O) {
222234285Sdim      O << "Name: " << Str << "\n";
223234285Sdim      O << "  Hash Value: " << format("0x%x", HashValue) << "\n";
224234285Sdim      O << "  Symbol: " ;
225234285Sdim      if (Sym) Sym->print(O);
226234285Sdim      else O << "<none>";
227234285Sdim      O << "\n";
228234285Sdim      for (size_t i = 0; i < Data.size(); i++) {
229234285Sdim        O << "  Offset: " << Data[i]->Die->getOffset() << "\n";
230234285Sdim        O << "  Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n";
231234285Sdim        O << "  Flags: " << Data[i]->Flags << "\n";
232234285Sdim      }
233234285Sdim    }
234234285Sdim    void dump() {
235234285Sdim      print(dbgs());
236234285Sdim    }
237234285Sdim    #endif
238234285Sdim  };
239234285Sdim
240234285Sdim  DwarfAccelTable(const DwarfAccelTable&); // DO NOT IMPLEMENT
241234285Sdim  void operator=(const DwarfAccelTable&);  // DO NOT IMPLEMENT
242234285Sdim
243234285Sdim  // Internal Functions
244234285Sdim  void EmitHeader(AsmPrinter *);
245234285Sdim  void EmitBuckets(AsmPrinter *);
246234285Sdim  void EmitHashes(AsmPrinter *);
247234285Sdim  void EmitOffsets(AsmPrinter *, MCSymbol *);
248234285Sdim  void EmitData(AsmPrinter *, DwarfDebug *D);
249234982Sdim
250234982Sdim  // Allocator for HashData and HashDataContents.
251234982Sdim  BumpPtrAllocator Allocator;
252234982Sdim
253234285Sdim  // Output Variables
254234285Sdim  TableHeader Header;
255234285Sdim  TableHeaderData HeaderData;
256234285Sdim  std::vector<HashData*> Data;
257234285Sdim
258234285Sdim  // String Data
259234982Sdim  typedef std::vector<HashDataContents*> DataArray;
260234982Sdim  typedef StringMap<DataArray, BumpPtrAllocator&> StringEntries;
261234285Sdim  StringEntries Entries;
262234285Sdim
263234285Sdim  // Buckets/Hashes/Offsets
264234285Sdim  typedef std::vector<HashData*> HashList;
265234285Sdim  typedef std::vector<HashList> BucketList;
266234285Sdim  BucketList Buckets;
267234285Sdim  HashList Hashes;
268234285Sdim
269234285Sdim  // Public Implementation
270234285Sdim public:
271234982Sdim  DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>);
272234285Sdim  ~DwarfAccelTable();
273234285Sdim  void AddName(StringRef, DIE*, char = 0);
274234285Sdim  void FinalizeTable(AsmPrinter *, const char *);
275234285Sdim  void Emit(AsmPrinter *, MCSymbol *, DwarfDebug *);
276234285Sdim#ifndef NDEBUG
277234285Sdim  void print(raw_ostream &O);
278234285Sdim  void dump() { print(dbgs()); }
279234285Sdim#endif
280234285Sdim};
281234285Sdim
282234285Sdim}
283234285Sdim#endif
284