blob: 4a144978b0d8c48710f8a4900ab7ea2bd9e0d3f9 [file] [log] [blame]
Eric Christopher6e472042011-11-07 09:18:42 +00001//==-- 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 CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
15#define CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
16
Chandler Carruth802d7552012-12-04 07:12:27 +000017#include "DIE.h"
Benjamin Kramer330970d2012-04-13 20:06:17 +000018#include "llvm/ADT/ArrayRef.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000019#include "llvm/ADT/StringMap.h"
Eric Christopher6e472042011-11-07 09:18:42 +000020#include "llvm/MC/MCSymbol.h"
Eric Christopher6e472042011-11-07 09:18:42 +000021#include "llvm/Support/DataTypes.h"
22#include "llvm/Support/Debug.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000023#include "llvm/Support/Dwarf.h"
Eric Christopher6e472042011-11-07 09:18:42 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/Format.h"
26#include "llvm/Support/FormattedStream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000027#include <vector>
Eric Christopher6e472042011-11-07 09:18:42 +000028
Eric Christopher4996c702011-11-07 09:24:32 +000029// The dwarf accelerator tables are an indirect hash table optimized
Eric Christopher6e472042011-11-07 09:18:42 +000030// for null lookup rather than access to known data. They are output into
31// an on-disk format that looks like this:
32//
33// .-------------.
34// | HEADER |
35// |-------------|
36// | BUCKETS |
37// |-------------|
38// | HASHES |
39// |-------------|
40// | OFFSETS |
41// |-------------|
42// | DATA |
43// `-------------'
44//
45// where the header contains a magic number, version, type of hash function,
46// the number of buckets, total number of hashes, and room for a special
47// struct of data and the length of that struct.
48//
49// The buckets contain an index (e.g. 6) into the hashes array. The hashes
50// section contains all of the 32-bit hash values in contiguous memory, and
51// the offsets contain the offset into the data area for the particular
52// hash.
Eric Christopher48fef592012-12-20 21:58:40 +000053//
Eric Christopher6e472042011-11-07 09:18:42 +000054// For a lookup example, we could hash a function name and take it modulo the
55// number of buckets giving us our bucket. From there we take the bucket value
56// as an index into the hashes table and look at each successive hash as long
57// as the hash value is still the same modulo result (bucket value) as earlier.
58// If we have a match we look at that same entry in the offsets table and
59// grab the offset in the data for our final match.
60
61namespace llvm {
62
63class AsmPrinter;
Eric Christopherf8194852013-12-05 18:06:10 +000064class DwarfFile;
Eric Christopher48fef592012-12-20 21:58:40 +000065
Benjamin Kramer079b96e2013-09-11 18:05:11 +000066class DwarfAccelTable {
Eric Christopher6e472042011-11-07 09:18:42 +000067
Eric Christopherb4e2cc42013-09-05 16:46:43 +000068 static uint32_t HashDJB(StringRef Str) {
Eric Christopher6e472042011-11-07 09:18:42 +000069 uint32_t h = 5381;
Eric Christopherff2edf12011-11-07 21:49:35 +000070 for (unsigned i = 0, e = Str.size(); i != e; ++i)
71 h = ((h << 5) + h) + Str[i];
Eric Christopher6e472042011-11-07 09:18:42 +000072 return h;
73 }
74
75 // Helper function to compute the number of buckets needed based on
76 // the number of unique hashes.
Eric Christopherb4e2cc42013-09-05 16:46:43 +000077 void ComputeBucketCount(void);
Eric Christopher48fef592012-12-20 21:58:40 +000078
Eric Christopher6e472042011-11-07 09:18:42 +000079 struct TableHeader {
Eric Christopherb4e2cc42013-09-05 16:46:43 +000080 uint32_t magic; // 'HASH' magic value to allow endian detection
81 uint16_t version; // Version number.
82 uint16_t hash_function; // The hash function enumeration that was used.
83 uint32_t bucket_count; // The number of buckets in this hash table.
84 uint32_t hashes_count; // The total number of unique hash values
85 // and hash data offsets in this table.
86 uint32_t header_data_len; // The bytes to skip to get to the hash
87 // indexes (buckets) for correct alignment.
Eric Christopher6e472042011-11-07 09:18:42 +000088 // Also written to disk is the implementation specific header data.
89
90 static const uint32_t MagicHash = 0x48415348;
Eric Christopher48fef592012-12-20 21:58:40 +000091
Eric Christopherb4e2cc42013-09-05 16:46:43 +000092 TableHeader(uint32_t data_len)
Eric Christophercf7289f2013-09-05 18:20:16 +000093 : magic(MagicHash), version(1),
94 hash_function(dwarf::DW_hash_function_djb), bucket_count(0),
95 hashes_count(0), header_data_len(data_len) {}
Eric Christopher6e472042011-11-07 09:18:42 +000096
97#ifndef NDEBUG
98 void print(raw_ostream &O) {
99 O << "Magic: " << format("0x%x", magic) << "\n"
100 << "Version: " << version << "\n"
101 << "Hash Function: " << hash_function << "\n"
102 << "Bucket Count: " << bucket_count << "\n"
103 << "Header Data Length: " << header_data_len << "\n";
104 }
105 void dump() { print(dbgs()); }
106#endif
107 };
108
109public:
110 // The HeaderData describes the form of each set of data. In general this
111 // is as a list of atoms (atom_count) where each atom contains a type
112 // (AtomType type) of data, and an encoding form (form). In the case of
113 // data that is referenced via DW_FORM_ref_* the die_offset_base is
114 // used to describe the offset for all forms in the list of atoms.
115 // This also serves as a public interface of sorts.
116 // When written to disk this will have the form:
117 //
118 // uint32_t die_offset_base
119 // uint32_t atom_count
Eric Christopher48fef592012-12-20 21:58:40 +0000120 // atom_count Atoms
Eric Christopher48fef592012-12-20 21:58:40 +0000121
Eric Christopher6e472042011-11-07 09:18:42 +0000122 // Make these public so that they can be used as a general interface to
123 // the class.
124 struct Atom {
Eric Christophercf7289f2013-09-05 18:20:16 +0000125 uint16_t type; // enum AtomType
Eric Christopher6e472042011-11-07 09:18:42 +0000126 uint16_t form; // DWARF DW_FORM_ defines
127
Eric Christophercf7289f2013-09-05 18:20:16 +0000128 Atom(uint16_t type, uint16_t form) : type(type), form(form) {}
Eric Christopher6e472042011-11-07 09:18:42 +0000129#ifndef NDEBUG
130 void print(raw_ostream &O) {
Eric Christophercf7289f2013-09-05 18:20:16 +0000131 O << "Type: " << dwarf::AtomTypeString(type) << "\n"
Eric Christopher6e472042011-11-07 09:18:42 +0000132 << "Form: " << dwarf::FormEncodingString(form) << "\n";
133 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000134 void dump() { print(dbgs()); }
Eric Christopher6e472042011-11-07 09:18:42 +0000135#endif
136 };
137
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000138private:
Eric Christopher6e472042011-11-07 09:18:42 +0000139 struct TableHeaderData {
Eric Christopher6e472042011-11-07 09:18:42 +0000140 uint32_t die_offset_base;
Benjamin Kramer330970d2012-04-13 20:06:17 +0000141 SmallVector<Atom, 1> Atoms;
Eric Christopher6e472042011-11-07 09:18:42 +0000142
Benjamin Kramer330970d2012-04-13 20:06:17 +0000143 TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0)
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000144 : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {}
Benjamin Kramer330970d2012-04-13 20:06:17 +0000145
Eric Christopher6e472042011-11-07 09:18:42 +0000146#ifndef NDEBUG
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000147 void print(raw_ostream &O) {
Eric Christopher6e472042011-11-07 09:18:42 +0000148 O << "die_offset_base: " << die_offset_base << "\n";
149 for (size_t i = 0; i < Atoms.size(); i++)
150 Atoms[i].print(O);
151 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000152 void dump() { print(dbgs()); }
Eric Christopher6e472042011-11-07 09:18:42 +0000153#endif
154 };
155
Eric Christopher4996c702011-11-07 09:24:32 +0000156 // The data itself consists of a str_offset, a count of the DIEs in the
Eric Christopher6e472042011-11-07 09:18:42 +0000157 // hash and the offsets to the DIEs themselves.
158 // On disk each data section is ended with a 0 KeyType as the end of the
159 // hash chain.
160 // On output this looks like:
161 // uint32_t str_offset
162 // uint32_t hash_data_count
163 // HashData[hash_data_count]
Eric Christopher21bde872012-01-06 04:35:23 +0000164public:
165 struct HashDataContents {
David Blaikie2ea848b2013-11-19 22:51:04 +0000166 const DIE *Die; // Offsets
Eric Christopher21bde872012-01-06 04:35:23 +0000167 char Flags; // Specific flags to output
168
David Blaikie2ea848b2013-11-19 22:51:04 +0000169 HashDataContents(const DIE *D, char Flags) : Die(D), Flags(Flags) {}
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000170#ifndef NDEBUG
Eric Christopher21bde872012-01-06 04:35:23 +0000171 void print(raw_ostream &O) const {
172 O << " Offset: " << Die->getOffset() << "\n";
173 O << " Tag: " << dwarf::TagString(Die->getTag()) << "\n";
174 O << " Flags: " << Flags << "\n";
175 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000176#endif
Eric Christopher21bde872012-01-06 04:35:23 +0000177 };
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000178
Eric Christopher21bde872012-01-06 04:35:23 +0000179private:
Eric Christopher6e472042011-11-07 09:18:42 +0000180 struct HashData {
181 StringRef Str;
182 uint32_t HashValue;
183 MCSymbol *Sym;
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000184 ArrayRef<HashDataContents *> Data; // offsets
185 HashData(StringRef S, ArrayRef<HashDataContents *> Data)
186 : Str(S), Data(Data) {
Eric Christopherff2edf12011-11-07 21:49:35 +0000187 HashValue = DwarfAccelTable::HashDJB(S);
Eric Christopher6e472042011-11-07 09:18:42 +0000188 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000189#ifndef NDEBUG
Eric Christopher6e472042011-11-07 09:18:42 +0000190 void print(raw_ostream &O) {
191 O << "Name: " << Str << "\n";
192 O << " Hash Value: " << format("0x%x", HashValue) << "\n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000193 O << " Symbol: ";
194 if (Sym)
195 Sym->print(O);
196 else
197 O << "<none>";
Eric Christopher6e472042011-11-07 09:18:42 +0000198 O << "\n";
Eric Christopher21bde872012-01-06 04:35:23 +0000199 for (size_t i = 0; i < Data.size(); i++) {
200 O << " Offset: " << Data[i]->Die->getOffset() << "\n";
201 O << " Tag: " << dwarf::TagString(Data[i]->Die->getTag()) << "\n";
202 O << " Flags: " << Data[i]->Flags << "\n";
203 }
Eric Christopher6e472042011-11-07 09:18:42 +0000204 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000205 void dump() { print(dbgs()); }
206#endif
Eric Christopher6e472042011-11-07 09:18:42 +0000207 };
208
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000209 DwarfAccelTable(const DwarfAccelTable &) LLVM_DELETED_FUNCTION;
210 void operator=(const DwarfAccelTable &) LLVM_DELETED_FUNCTION;
Eric Christopher6e472042011-11-07 09:18:42 +0000211
212 // Internal Functions
213 void EmitHeader(AsmPrinter *);
214 void EmitBuckets(AsmPrinter *);
215 void EmitHashes(AsmPrinter *);
216 void EmitOffsets(AsmPrinter *, MCSymbol *);
Eric Christopherf8194852013-12-05 18:06:10 +0000217 void EmitData(AsmPrinter *, DwarfFile *D);
Benjamin Kramer330970d2012-04-13 20:06:17 +0000218
219 // Allocator for HashData and HashDataContents.
220 BumpPtrAllocator Allocator;
221
Eric Christopher6e472042011-11-07 09:18:42 +0000222 // Output Variables
223 TableHeader Header;
224 TableHeaderData HeaderData;
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000225 std::vector<HashData *> Data;
Eric Christopher6e472042011-11-07 09:18:42 +0000226
227 // String Data
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000228 typedef std::vector<HashDataContents *> DataArray;
229 typedef StringMap<DataArray, BumpPtrAllocator &> StringEntries;
Eric Christopher6e472042011-11-07 09:18:42 +0000230 StringEntries Entries;
231
232 // Buckets/Hashes/Offsets
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000233 typedef std::vector<HashData *> HashList;
Eric Christopher6e472042011-11-07 09:18:42 +0000234 typedef std::vector<HashList> BucketList;
235 BucketList Buckets;
236 HashList Hashes;
Eric Christopher48fef592012-12-20 21:58:40 +0000237
Eric Christopher6e472042011-11-07 09:18:42 +0000238 // Public Implementation
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000239public:
Benjamin Kramer330970d2012-04-13 20:06:17 +0000240 DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>);
Eric Christopherb6205d82011-11-07 21:49:28 +0000241 ~DwarfAccelTable();
David Blaikie2ea848b2013-11-19 22:51:04 +0000242 void AddName(StringRef, const DIE *, char = 0);
Benjamin Kramer63e39eb2013-05-11 18:24:28 +0000243 void FinalizeTable(AsmPrinter *, StringRef);
Eric Christopherf8194852013-12-05 18:06:10 +0000244 void Emit(AsmPrinter *, MCSymbol *, DwarfFile *);
Eric Christopher6e472042011-11-07 09:18:42 +0000245#ifndef NDEBUG
246 void print(raw_ostream &O);
247 void dump() { print(dbgs()); }
248#endif
249};
Eric Christopher6e472042011-11-07 09:18:42 +0000250}
251#endif