DwarfAccelTable.h revision 234285
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"
18234285Sdim#include "llvm/MC/MCSymbol.h"
19234285Sdim#include "llvm/Support/Dwarf.h"
20234285Sdim#include "llvm/Support/DataTypes.h"
21234285Sdim#include "llvm/Support/Debug.h"
22234285Sdim#include "llvm/Support/ErrorHandling.h"
23234285Sdim#include "llvm/Support/Format.h"
24234285Sdim#include "llvm/Support/FormattedStream.h"
25234285Sdim#include "DIE.h"
26234285Sdim#include <vector>
27234285Sdim#include <map>
28234285Sdim
29234285Sdim// The dwarf accelerator tables are an indirect hash table optimized
30234285Sdim// for null lookup rather than access to known data. They are output into
31234285Sdim// an on-disk format that looks like this:
32234285Sdim//
33234285Sdim// .-------------.
34234285Sdim// |  HEADER     |
35234285Sdim// |-------------|
36234285Sdim// |  BUCKETS    |
37234285Sdim// |-------------|
38234285Sdim// |  HASHES     |
39234285Sdim// |-------------|
40234285Sdim// |  OFFSETS    |
41234285Sdim// |-------------|
42234285Sdim// |  DATA       |
43234285Sdim// `-------------'
44234285Sdim//
45234285Sdim// where the header contains a magic number, version, type of hash function,
46234285Sdim// the number of buckets, total number of hashes, and room for a special
47234285Sdim// struct of data and the length of that struct.
48234285Sdim//
49234285Sdim// The buckets contain an index (e.g. 6) into the hashes array. The hashes
50234285Sdim// section contains all of the 32-bit hash values in contiguous memory, and
51234285Sdim// the offsets contain the offset into the data area for the particular
52234285Sdim// hash.
53234285Sdim//
54234285Sdim// For a lookup example, we could hash a function name and take it modulo the
55234285Sdim// number of buckets giving us our bucket. From there we take the bucket value
56234285Sdim// as an index into the hashes table and look at each successive hash as long
57234285Sdim// as the hash value is still the same modulo result (bucket value) as earlier.
58234285Sdim// If we have a match we look at that same entry in the offsets table and
59234285Sdim// grab the offset in the data for our final match.
60234285Sdim
61234285Sdimnamespace llvm {
62234285Sdim
63234285Sdimclass AsmPrinter;
64234285Sdimclass DIE;
65234285Sdimclass DwarfDebug;
66234285Sdim
67234285Sdimclass DwarfAccelTable {
68234285Sdim
69234285Sdim  enum HashFunctionType {
70234285Sdim    eHashFunctionDJB = 0u
71234285Sdim  };
72234285Sdim
73234285Sdim  static uint32_t HashDJB (StringRef Str) {
74234285Sdim    uint32_t h = 5381;
75234285Sdim    for (unsigned i = 0, e = Str.size(); i != e; ++i)
76234285Sdim      h = ((h << 5) + h) + Str[i];
77234285Sdim    return h;
78234285Sdim  }
79234285Sdim
80234285Sdim  // Helper function to compute the number of buckets needed based on
81234285Sdim  // the number of unique hashes.
82234285Sdim  void ComputeBucketCount (void);
83234285Sdim
84234285Sdim  struct TableHeader {
85234285Sdim    uint32_t   magic;           // 'HASH' magic value to allow endian detection
86234285Sdim    uint16_t   version;         // Version number.
87234285Sdim    uint16_t   hash_function;   // The hash function enumeration that was used.
88234285Sdim    uint32_t   bucket_count;    // The number of buckets in this hash table.
89234285Sdim    uint32_t   hashes_count;    // The total number of unique hash values
90234285Sdim                                // and hash data offsets in this table.
91234285Sdim    uint32_t   header_data_len; // The bytes to skip to get to the hash
92234285Sdim                                // indexes (buckets) for correct alignment.
93234285Sdim    // Also written to disk is the implementation specific header data.
94234285Sdim
95234285Sdim    static const uint32_t MagicHash = 0x48415348;
96234285Sdim
97234285Sdim    TableHeader (uint32_t data_len) :
98234285Sdim      magic (MagicHash), version (1), hash_function (eHashFunctionDJB),
99234285Sdim      bucket_count (0), hashes_count (0), header_data_len (data_len)
100234285Sdim    {}
101234285Sdim
102234285Sdim#ifndef NDEBUG
103234285Sdim    void print(raw_ostream &O) {
104234285Sdim      O << "Magic: " << format("0x%x", magic) << "\n"
105234285Sdim        << "Version: " << version << "\n"
106234285Sdim        << "Hash Function: " << hash_function << "\n"
107234285Sdim        << "Bucket Count: " << bucket_count << "\n"
108234285Sdim        << "Header Data Length: " << header_data_len << "\n";
109234285Sdim    }
110234285Sdim    void dump() { print(dbgs()); }
111234285Sdim#endif
112234285Sdim  };
113234285Sdim
114234285Sdimpublic:
115234285Sdim  // The HeaderData describes the form of each set of data. In general this
116234285Sdim  // is as a list of atoms (atom_count) where each atom contains a type
117234285Sdim  // (AtomType type) of data, and an encoding form (form). In the case of
118234285Sdim  // data that is referenced via DW_FORM_ref_* the die_offset_base is
119234285Sdim  // used to describe the offset for all forms in the list of atoms.
120234285Sdim  // This also serves as a public interface of sorts.
121234285Sdim  // When written to disk this will have the form:
122234285Sdim  //
123234285Sdim  // uint32_t die_offset_base
124234285Sdim  // uint32_t atom_count
125234285Sdim  // atom_count Atoms
126234285Sdim  enum AtomType {
127234285Sdim    eAtomTypeNULL       = 0u,
128234285Sdim    eAtomTypeDIEOffset  = 1u,   // DIE offset, check form for encoding
129234285Sdim    eAtomTypeCUOffset   = 2u,   // DIE offset of the compiler unit header that
130234285Sdim                                // contains the item in question
131234285Sdim    eAtomTypeTag        = 3u,   // DW_TAG_xxx value, should be encoded as
132234285Sdim                                // DW_FORM_data1 (if no tags exceed 255) or
133234285Sdim                                // DW_FORM_data2.
134234285Sdim    eAtomTypeNameFlags  = 4u,   // Flags from enum NameFlags
135234285Sdim    eAtomTypeTypeFlags  = 5u    // Flags from enum TypeFlags
136234285Sdim  };
137234285Sdim
138234285Sdim  enum TypeFlags {
139234285Sdim    eTypeFlagClassMask = 0x0000000fu,
140234285Sdim
141234285Sdim    // Always set for C++, only set for ObjC if this is the
142234285Sdim    // @implementation for a class.
143234285Sdim    eTypeFlagClassIsImplementation  = ( 1u << 1 )
144234285Sdim  };
145234285Sdim
146234285Sdim  // Make these public so that they can be used as a general interface to
147234285Sdim  // the class.
148234285Sdim  struct Atom {
149234285Sdim    AtomType type; // enum AtomType
150234285Sdim    uint16_t form; // DWARF DW_FORM_ defines
151234285Sdim
152234285Sdim    Atom(AtomType type, uint16_t form) : type(type), form(form) {}
153234285Sdim    static const char * AtomTypeString(enum AtomType);
154234285Sdim#ifndef NDEBUG
155234285Sdim    void print(raw_ostream &O) {
156234285Sdim      O << "Type: " << AtomTypeString(type) << "\n"
157234285Sdim        << "Form: " << dwarf::FormEncodingString(form) << "\n";
158234285Sdim    }
159234285Sdim    void dump() {
160234285Sdim      print(dbgs());
161234285Sdim    }
162234285Sdim#endif
163234285Sdim  };
164234285Sdim
165234285Sdim private:
166234285Sdim  struct TableHeaderData {
167234285Sdim
168234285Sdim    uint32_t die_offset_base;
169234285Sdim    std::vector<Atom> Atoms;
170234285Sdim
171234285Sdim    TableHeaderData(std::vector<DwarfAccelTable::Atom> &AtomList,
172234285Sdim                    uint32_t offset = 0) :
173234285Sdim      die_offset_base(offset) {
174234285Sdim      for (size_t i = 0, e = AtomList.size(); i != e; ++i)
175234285Sdim        Atoms.push_back(AtomList[i]);
176234285Sdim    }
177234285Sdim
178234285Sdim    TableHeaderData(DwarfAccelTable::Atom Atom, uint32_t offset = 0)
179234285Sdim    : die_offset_base(offset) {
180234285Sdim      Atoms.push_back(Atom);
181234285Sdim    }
182234285Sdim
183234285Sdim#ifndef NDEBUG
184234285Sdim    void print (raw_ostream &O) {
185234285Sdim      O << "die_offset_base: " << die_offset_base << "\n";
186234285Sdim      for (size_t i = 0; i < Atoms.size(); i++)
187234285Sdim        Atoms[i].print(O);
188234285Sdim    }
189234285Sdim    void dump() {
190234285Sdim      print(dbgs());
191234285Sdim    }
192234285Sdim#endif
193234285Sdim  };
194234285Sdim
195234285Sdim  // The data itself consists of a str_offset, a count of the DIEs in the
196234285Sdim  // hash and the offsets to the DIEs themselves.
197234285Sdim  // On disk each data section is ended with a 0 KeyType as the end of the
198234285Sdim  // hash chain.
199234285Sdim  // On output this looks like:
200234285Sdim  // uint32_t str_offset
201234285Sdim  // uint32_t hash_data_count
202234285Sdim  // HashData[hash_data_count]
203234285Sdimpublic:
204234285Sdim  struct HashDataContents {
205234285Sdim    DIE *Die; // Offsets
206234285Sdim    char Flags; // Specific flags to output
207234285Sdim
208234285Sdim    HashDataContents(DIE *D, char Flags) :
209234285Sdim      Die(D),
210234285Sdim      Flags(Flags) { }
211234285Sdim    #ifndef NDEBUG
212234285Sdim    void print(raw_ostream &O) const {
213234285Sdim      O << "  Offset: " << Die->getOffset() << "\n";
214234285Sdim      O << "  Tag: " << dwarf::TagString(Die->getTag()) << "\n";
215234285Sdim      O << "  Flags: " << Flags << "\n";
216234285Sdim    }
217234285Sdim    #endif
218234285Sdim  };
219234285Sdimprivate:
220234285Sdim  struct HashData {
221234285Sdim    StringRef Str;
222234285Sdim    uint32_t HashValue;
223234285Sdim    MCSymbol *Sym;
224234285Sdim    std::vector<struct HashDataContents*> Data; // offsets
225234285Sdim    HashData(StringRef S) : Str(S) {
226234285Sdim      HashValue = DwarfAccelTable::HashDJB(S);
227234285Sdim    }
228234285Sdim    void addData(struct HashDataContents *Datum) { Data.push_back(Datum); }
229234285Sdim    #ifndef NDEBUG
230234285Sdim    void print(raw_ostream &O) {
231234285Sdim      O << "Name: " << Str << "\n";
232234285Sdim      O << "  Hash Value: " << format("0x%x", HashValue) << "\n";
233234285Sdim      O << "  Symbol: " ;
234234285Sdim      if (Sym) Sym->print(O);
235234285Sdim      else O << "<none>";
236234285Sdim      O << "\n";
237234285Sdim      for (size_t i = 0; i < Data.size(); i++) {
238234285Sdim        O << "  Offset: " << Data[i]->Die->getOffset() << "\n";
239234285Sdim        O << "  Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n";
240234285Sdim        O << "  Flags: " << Data[i]->Flags << "\n";
241234285Sdim      }
242234285Sdim    }
243234285Sdim    void dump() {
244234285Sdim      print(dbgs());
245234285Sdim    }
246234285Sdim    #endif
247234285Sdim  };
248234285Sdim
249234285Sdim  DwarfAccelTable(const DwarfAccelTable&); // DO NOT IMPLEMENT
250234285Sdim  void operator=(const DwarfAccelTable&);  // DO NOT IMPLEMENT
251234285Sdim
252234285Sdim  // Internal Functions
253234285Sdim  void EmitHeader(AsmPrinter *);
254234285Sdim  void EmitBuckets(AsmPrinter *);
255234285Sdim  void EmitHashes(AsmPrinter *);
256234285Sdim  void EmitOffsets(AsmPrinter *, MCSymbol *);
257234285Sdim  void EmitData(AsmPrinter *, DwarfDebug *D);
258234285Sdim
259234285Sdim  // Output Variables
260234285Sdim  TableHeader Header;
261234285Sdim  TableHeaderData HeaderData;
262234285Sdim  std::vector<HashData*> Data;
263234285Sdim
264234285Sdim  // String Data
265234285Sdim  typedef std::vector<struct HashDataContents*> DataArray;
266234285Sdim  typedef StringMap<DataArray> StringEntries;
267234285Sdim  StringEntries Entries;
268234285Sdim
269234285Sdim  // Buckets/Hashes/Offsets
270234285Sdim  typedef std::vector<HashData*> HashList;
271234285Sdim  typedef std::vector<HashList> BucketList;
272234285Sdim  BucketList Buckets;
273234285Sdim  HashList Hashes;
274234285Sdim
275234285Sdim  // Public Implementation
276234285Sdim public:
277234285Sdim  DwarfAccelTable(DwarfAccelTable::Atom);
278234285Sdim  DwarfAccelTable(std::vector<DwarfAccelTable::Atom> &);
279234285Sdim  ~DwarfAccelTable();
280234285Sdim  void AddName(StringRef, DIE*, char = 0);
281234285Sdim  void FinalizeTable(AsmPrinter *, const char *);
282234285Sdim  void Emit(AsmPrinter *, MCSymbol *, DwarfDebug *);
283234285Sdim#ifndef NDEBUG
284234285Sdim  void print(raw_ostream &O);
285234285Sdim  void dump() { print(dbgs()); }
286234285Sdim#endif
287234285Sdim};
288234285Sdim
289234285Sdim}
290234285Sdim#endif
291