]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/AsmPrinter/DwarfAccelTable.h
Merge ^/head r321239 through r321306.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / CodeGen / AsmPrinter / DwarfAccelTable.h
1 //==-- llvm/CodeGen/DwarfAccelTable.h - Dwarf Accelerator Tables -*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf accelerator tables.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/BinaryFormat/Dwarf.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include <vector>
29
30 // The dwarf accelerator tables are an indirect hash table optimized
31 // for null lookup rather than access to known data. They are output into
32 // an on-disk format that looks like this:
33 //
34 // .-------------.
35 // |  HEADER     |
36 // |-------------|
37 // |  BUCKETS    |
38 // |-------------|
39 // |  HASHES     |
40 // |-------------|
41 // |  OFFSETS    |
42 // |-------------|
43 // |  DATA       |
44 // `-------------'
45 //
46 // where the header contains a magic number, version, type of hash function,
47 // the number of buckets, total number of hashes, and room for a special
48 // struct of data and the length of that struct.
49 //
50 // The buckets contain an index (e.g. 6) into the hashes array. The hashes
51 // section contains all of the 32-bit hash values in contiguous memory, and
52 // the offsets contain the offset into the data area for the particular
53 // hash.
54 //
55 // For a lookup example, we could hash a function name and take it modulo the
56 // number of buckets giving us our bucket. From there we take the bucket value
57 // as an index into the hashes table and look at each successive hash as long
58 // as the hash value is still the same modulo result (bucket value) as earlier.
59 // If we have a match we look at that same entry in the offsets table and
60 // grab the offset in the data for our final match.
61
62 namespace llvm {
63
64 class AsmPrinter;
65 class DwarfDebug;
66
67 class DwarfAccelTable {
68
69   static uint32_t HashDJB(StringRef Str) {
70     uint32_t h = 5381;
71     for (unsigned i = 0, e = Str.size(); i != e; ++i)
72       h = ((h << 5) + h) + Str[i];
73     return h;
74   }
75
76   // Helper function to compute the number of buckets needed based on
77   // the number of unique hashes.
78   void ComputeBucketCount(void);
79
80   struct TableHeader {
81     uint32_t magic;           // 'HASH' magic value to allow endian detection
82     uint16_t version;         // Version number.
83     uint16_t hash_function;   // The hash function enumeration that was used.
84     uint32_t bucket_count;    // The number of buckets in this hash table.
85     uint32_t hashes_count;    // The total number of unique hash values
86                               // and hash data offsets in this table.
87     uint32_t header_data_len; // The bytes to skip to get to the hash
88                               // indexes (buckets) for correct alignment.
89     // Also written to disk is the implementation specific header data.
90
91     static const uint32_t MagicHash = 0x48415348;
92
93     TableHeader(uint32_t data_len)
94         : magic(MagicHash), version(1),
95           hash_function(dwarf::DW_hash_function_djb), bucket_count(0),
96           hashes_count(0), header_data_len(data_len) {}
97
98 #ifndef NDEBUG
99     void print(raw_ostream &O) {
100       O << "Magic: " << format("0x%x", magic) << "\n"
101         << "Version: " << version << "\n"
102         << "Hash Function: " << hash_function << "\n"
103         << "Bucket Count: " << bucket_count << "\n"
104         << "Header Data Length: " << header_data_len << "\n";
105     }
106     void dump() { print(dbgs()); }
107 #endif
108   };
109
110 public:
111   // The HeaderData describes the form of each set of data. In general this
112   // is as a list of atoms (atom_count) where each atom contains a type
113   // (AtomType type) of data, and an encoding form (form). In the case of
114   // data that is referenced via DW_FORM_ref_* the die_offset_base is
115   // used to describe the offset for all forms in the list of atoms.
116   // This also serves as a public interface of sorts.
117   // When written to disk this will have the form:
118   //
119   // uint32_t die_offset_base
120   // uint32_t atom_count
121   // atom_count Atoms
122
123   // Make these public so that they can be used as a general interface to
124   // the class.
125   struct Atom {
126     uint16_t type; // enum AtomType
127     uint16_t form; // DWARF DW_FORM_ defines
128
129     constexpr Atom(uint16_t type, uint16_t form) : type(type), form(form) {}
130 #ifndef NDEBUG
131     void print(raw_ostream &O) {
132       O << "Type: " << dwarf::AtomTypeString(type) << "\n"
133         << "Form: " << dwarf::FormEncodingString(form) << "\n";
134     }
135     void dump() { print(dbgs()); }
136 #endif
137   };
138
139 private:
140   struct TableHeaderData {
141     uint32_t die_offset_base;
142     SmallVector<Atom, 3> Atoms;
143
144     TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0)
145         : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {}
146
147 #ifndef NDEBUG
148     void print(raw_ostream &O) {
149       O << "die_offset_base: " << die_offset_base << "\n";
150       for (size_t i = 0; i < Atoms.size(); i++)
151         Atoms[i].print(O);
152     }
153     void dump() { print(dbgs()); }
154 #endif
155   };
156
157   // The data itself consists of a str_offset, a count of the DIEs in the
158   // hash and the offsets to the DIEs themselves.
159   // On disk each data section is ended with a 0 KeyType as the end of the
160   // hash chain.
161   // On output this looks like:
162   // uint32_t str_offset
163   // uint32_t hash_data_count
164   // HashData[hash_data_count]
165 public:
166   struct HashDataContents {
167     const DIE *Die;   // Offsets
168     char Flags; // Specific flags to output
169
170     HashDataContents(const DIE *D, char Flags) : Die(D), Flags(Flags) {}
171 #ifndef NDEBUG
172     void print(raw_ostream &O) const {
173       O << "  Offset: " << Die->getOffset() << "\n";
174       O << "  Tag: " << dwarf::TagString(Die->getTag()) << "\n";
175       O << "  Flags: " << Flags << "\n";
176     }
177 #endif
178   };
179
180 private:
181   // String Data
182   struct DataArray {
183     DwarfStringPoolEntryRef Name;
184     std::vector<HashDataContents *> Values;
185   };
186   friend struct HashData;
187   struct HashData {
188     StringRef Str;
189     uint32_t HashValue;
190     MCSymbol *Sym;
191     DwarfAccelTable::DataArray &Data; // offsets
192     HashData(StringRef S, DwarfAccelTable::DataArray &Data)
193         : Str(S), Data(Data) {
194       HashValue = DwarfAccelTable::HashDJB(S);
195     }
196 #ifndef NDEBUG
197     void print(raw_ostream &O) {
198       O << "Name: " << Str << "\n";
199       O << "  Hash Value: " << format("0x%x", HashValue) << "\n";
200       O << "  Symbol: ";
201       if (Sym)
202         O << *Sym;
203       else
204         O << "<none>";
205       O << "\n";
206       for (HashDataContents *C : Data.Values) {
207         O << "  Offset: " << C->Die->getOffset() << "\n";
208         O << "  Tag: " << dwarf::TagString(C->Die->getTag()) << "\n";
209         O << "  Flags: " << C->Flags << "\n";
210       }
211     }
212     void dump() { print(dbgs()); }
213 #endif
214   };
215
216   DwarfAccelTable(const DwarfAccelTable &) = delete;
217   void operator=(const DwarfAccelTable &) = delete;
218
219   // Internal Functions
220   void EmitHeader(AsmPrinter *);
221   void EmitBuckets(AsmPrinter *);
222   void EmitHashes(AsmPrinter *);
223   void emitOffsets(AsmPrinter *, const MCSymbol *);
224   void EmitData(AsmPrinter *, DwarfDebug *D);
225
226   // Allocator for HashData and HashDataContents.
227   BumpPtrAllocator Allocator;
228
229   // Output Variables
230   TableHeader Header;
231   TableHeaderData HeaderData;
232   std::vector<HashData *> Data;
233
234   typedef StringMap<DataArray, BumpPtrAllocator &> StringEntries;
235   StringEntries Entries;
236
237   // Buckets/Hashes/Offsets
238   typedef std::vector<HashData *> HashList;
239   typedef std::vector<HashList> BucketList;
240   BucketList Buckets;
241   HashList Hashes;
242
243   // Public Implementation
244 public:
245   DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>);
246   void AddName(DwarfStringPoolEntryRef Name, const DIE *Die, char Flags = 0);
247   void FinalizeTable(AsmPrinter *, StringRef);
248   void emit(AsmPrinter *, const MCSymbol *, DwarfDebug *);
249 #ifndef NDEBUG
250   void print(raw_ostream &O);
251   void dump() { print(dbgs()); }
252 #endif
253 };
254 }
255 #endif