blob: 4a6085b46fc583b35b58811831b5916920357be2 [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
15#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
Eric Christopher6e472042011-11-07 09:18:42 +000016
Benjamin Kramer330970d2012-04-13 20:06:17 +000017#include "llvm/ADT/ArrayRef.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000018#include "llvm/ADT/StringMap.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000019#include "llvm/CodeGen/DIE.h"
Eric Christopher6e472042011-11-07 09:18:42 +000020#include "llvm/MC/MCSymbol.h"
David Blaikie18d33752014-04-24 01:23:49 +000021#include "llvm/Support/Compiler.h"
Eric Christopher6e472042011-11-07 09:18:42 +000022#include "llvm/Support/DataTypes.h"
23#include "llvm/Support/Debug.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000024#include "llvm/Support/Dwarf.h"
Eric Christopher6e472042011-11-07 09:18:42 +000025#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/Format.h"
27#include "llvm/Support/FormattedStream.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000028#include <vector>
Eric Christopher6e472042011-11-07 09:18:42 +000029
Eric Christopher4996c702011-11-07 09:24:32 +000030// The dwarf accelerator tables are an indirect hash table optimized
Eric Christopher6e472042011-11-07 09:18:42 +000031// 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.
Eric Christopher48fef592012-12-20 21:58:40 +000054//
Eric Christopher6e472042011-11-07 09:18:42 +000055// 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
62namespace llvm {
63
64class AsmPrinter;
Frederic Riss3a6b3542014-11-12 23:48:14 +000065class DwarfDebug;
Eric Christopher48fef592012-12-20 21:58:40 +000066
Benjamin Kramer079b96e2013-09-11 18:05:11 +000067class DwarfAccelTable {
Eric Christopher6e472042011-11-07 09:18:42 +000068
Eric Christopherb4e2cc42013-09-05 16:46:43 +000069 static uint32_t HashDJB(StringRef Str) {
Eric Christopher6e472042011-11-07 09:18:42 +000070 uint32_t h = 5381;
Eric Christopherff2edf12011-11-07 21:49:35 +000071 for (unsigned i = 0, e = Str.size(); i != e; ++i)
72 h = ((h << 5) + h) + Str[i];
Eric Christopher6e472042011-11-07 09:18:42 +000073 return h;
74 }
75
76 // Helper function to compute the number of buckets needed based on
77 // the number of unique hashes.
Eric Christopherb4e2cc42013-09-05 16:46:43 +000078 void ComputeBucketCount(void);
Eric Christopher48fef592012-12-20 21:58:40 +000079
Eric Christopher6e472042011-11-07 09:18:42 +000080 struct TableHeader {
Eric Christopherb4e2cc42013-09-05 16:46:43 +000081 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.
Eric Christopher6e472042011-11-07 09:18:42 +000089 // Also written to disk is the implementation specific header data.
90
91 static const uint32_t MagicHash = 0x48415348;
Eric Christopher48fef592012-12-20 21:58:40 +000092
Eric Christopherb4e2cc42013-09-05 16:46:43 +000093 TableHeader(uint32_t data_len)
Eric Christophercf7289f2013-09-05 18:20:16 +000094 : magic(MagicHash), version(1),
95 hash_function(dwarf::DW_hash_function_djb), bucket_count(0),
96 hashes_count(0), header_data_len(data_len) {}
Eric Christopher6e472042011-11-07 09:18:42 +000097
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
110public:
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
Eric Christopher48fef592012-12-20 21:58:40 +0000121 // atom_count Atoms
Eric Christopher48fef592012-12-20 21:58:40 +0000122
Eric Christopher6e472042011-11-07 09:18:42 +0000123 // Make these public so that they can be used as a general interface to
124 // the class.
125 struct Atom {
Eric Christophercf7289f2013-09-05 18:20:16 +0000126 uint16_t type; // enum AtomType
Eric Christopher6e472042011-11-07 09:18:42 +0000127 uint16_t form; // DWARF DW_FORM_ defines
128
David Blaikie18d33752014-04-24 01:23:49 +0000129 LLVM_CONSTEXPR Atom(uint16_t type, uint16_t form)
130 : type(type), form(form) {}
Eric Christopher6e472042011-11-07 09:18:42 +0000131#ifndef NDEBUG
132 void print(raw_ostream &O) {
Eric Christophercf7289f2013-09-05 18:20:16 +0000133 O << "Type: " << dwarf::AtomTypeString(type) << "\n"
Eric Christopher6e472042011-11-07 09:18:42 +0000134 << "Form: " << dwarf::FormEncodingString(form) << "\n";
135 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000136 void dump() { print(dbgs()); }
Eric Christopher6e472042011-11-07 09:18:42 +0000137#endif
138 };
139
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000140private:
Eric Christopher6e472042011-11-07 09:18:42 +0000141 struct TableHeaderData {
Eric Christopher6e472042011-11-07 09:18:42 +0000142 uint32_t die_offset_base;
Hans Wennborgb8cff692014-08-11 02:18:15 +0000143 SmallVector<Atom, 3> Atoms;
Eric Christopher6e472042011-11-07 09:18:42 +0000144
Benjamin Kramer330970d2012-04-13 20:06:17 +0000145 TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0)
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000146 : die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {}
Benjamin Kramer330970d2012-04-13 20:06:17 +0000147
Eric Christopher6e472042011-11-07 09:18:42 +0000148#ifndef NDEBUG
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000149 void print(raw_ostream &O) {
Eric Christopher6e472042011-11-07 09:18:42 +0000150 O << "die_offset_base: " << die_offset_base << "\n";
151 for (size_t i = 0; i < Atoms.size(); i++)
152 Atoms[i].print(O);
153 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000154 void dump() { print(dbgs()); }
Eric Christopher6e472042011-11-07 09:18:42 +0000155#endif
156 };
157
Eric Christopher4996c702011-11-07 09:24:32 +0000158 // The data itself consists of a str_offset, a count of the DIEs in the
Eric Christopher6e472042011-11-07 09:18:42 +0000159 // hash and the offsets to the DIEs themselves.
160 // On disk each data section is ended with a 0 KeyType as the end of the
161 // hash chain.
162 // On output this looks like:
163 // uint32_t str_offset
164 // uint32_t hash_data_count
165 // HashData[hash_data_count]
Eric Christopher21bde872012-01-06 04:35:23 +0000166public:
167 struct HashDataContents {
David Blaikie2ea848b2013-11-19 22:51:04 +0000168 const DIE *Die; // Offsets
Eric Christopher21bde872012-01-06 04:35:23 +0000169 char Flags; // Specific flags to output
170
David Blaikie2ea848b2013-11-19 22:51:04 +0000171 HashDataContents(const DIE *D, char Flags) : Die(D), Flags(Flags) {}
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000172#ifndef NDEBUG
Eric Christopher21bde872012-01-06 04:35:23 +0000173 void print(raw_ostream &O) const {
174 O << " Offset: " << Die->getOffset() << "\n";
175 O << " Tag: " << dwarf::TagString(Die->getTag()) << "\n";
176 O << " Flags: " << Flags << "\n";
177 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000178#endif
Eric Christopher21bde872012-01-06 04:35:23 +0000179 };
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000180
Eric Christopher21bde872012-01-06 04:35:23 +0000181private:
David Blaikie772ab8a2014-04-25 22:21:35 +0000182 // String Data
183 struct DataArray {
184 MCSymbol *StrSym;
185 std::vector<HashDataContents *> Values;
David Blaikie6afb2672014-04-27 14:47:23 +0000186 DataArray() : StrSym(nullptr) {}
David Blaikie772ab8a2014-04-25 22:21:35 +0000187 };
188 friend struct HashData;
Eric Christopher6e472042011-11-07 09:18:42 +0000189 struct HashData {
190 StringRef Str;
191 uint32_t HashValue;
192 MCSymbol *Sym;
David Blaikie772ab8a2014-04-25 22:21:35 +0000193 DwarfAccelTable::DataArray &Data; // offsets
194 HashData(StringRef S, DwarfAccelTable::DataArray &Data)
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000195 : Str(S), Data(Data) {
Eric Christopherff2edf12011-11-07 21:49:35 +0000196 HashValue = DwarfAccelTable::HashDJB(S);
Eric Christopher6e472042011-11-07 09:18:42 +0000197 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000198#ifndef NDEBUG
Eric Christopher6e472042011-11-07 09:18:42 +0000199 void print(raw_ostream &O) {
200 O << "Name: " << Str << "\n";
201 O << " Hash Value: " << format("0x%x", HashValue) << "\n";
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000202 O << " Symbol: ";
203 if (Sym)
204 Sym->print(O);
205 else
206 O << "<none>";
Eric Christopher6e472042011-11-07 09:18:42 +0000207 O << "\n";
David Blaikie772ab8a2014-04-25 22:21:35 +0000208 for (HashDataContents *C : Data.Values) {
209 O << " Offset: " << C->Die->getOffset() << "\n";
210 O << " Tag: " << dwarf::TagString(C->Die->getTag()) << "\n";
211 O << " Flags: " << C->Flags << "\n";
Eric Christopher21bde872012-01-06 04:35:23 +0000212 }
Eric Christopher6e472042011-11-07 09:18:42 +0000213 }
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000214 void dump() { print(dbgs()); }
215#endif
Eric Christopher6e472042011-11-07 09:18:42 +0000216 };
217
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000218 DwarfAccelTable(const DwarfAccelTable &) LLVM_DELETED_FUNCTION;
219 void operator=(const DwarfAccelTable &) LLVM_DELETED_FUNCTION;
Eric Christopher6e472042011-11-07 09:18:42 +0000220
221 // Internal Functions
222 void EmitHeader(AsmPrinter *);
223 void EmitBuckets(AsmPrinter *);
224 void EmitHashes(AsmPrinter *);
225 void EmitOffsets(AsmPrinter *, MCSymbol *);
Frederic Riss3a6b3542014-11-12 23:48:14 +0000226 void EmitData(AsmPrinter *, DwarfDebug *D, MCSymbol *StrSym);
Benjamin Kramer330970d2012-04-13 20:06:17 +0000227
228 // Allocator for HashData and HashDataContents.
229 BumpPtrAllocator Allocator;
230
Eric Christopher6e472042011-11-07 09:18:42 +0000231 // Output Variables
232 TableHeader Header;
233 TableHeaderData HeaderData;
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000234 std::vector<HashData *> Data;
Eric Christopher6e472042011-11-07 09:18:42 +0000235
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000236 typedef StringMap<DataArray, BumpPtrAllocator &> StringEntries;
Eric Christopher6e472042011-11-07 09:18:42 +0000237 StringEntries Entries;
238
239 // Buckets/Hashes/Offsets
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000240 typedef std::vector<HashData *> HashList;
Eric Christopher6e472042011-11-07 09:18:42 +0000241 typedef std::vector<HashList> BucketList;
242 BucketList Buckets;
243 HashList Hashes;
Eric Christopher48fef592012-12-20 21:58:40 +0000244
Eric Christopher6e472042011-11-07 09:18:42 +0000245 // Public Implementation
Eric Christopherb4e2cc42013-09-05 16:46:43 +0000246public:
Benjamin Kramer330970d2012-04-13 20:06:17 +0000247 DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>);
David Blaikie772ab8a2014-04-25 22:21:35 +0000248 void AddName(StringRef Name, MCSymbol *StrSym, const DIE *Die,
249 char Flags = 0);
Benjamin Kramer63e39eb2013-05-11 18:24:28 +0000250 void FinalizeTable(AsmPrinter *, StringRef);
Frederic Riss3a6b3542014-11-12 23:48:14 +0000251 void Emit(AsmPrinter *, MCSymbol *, DwarfDebug *, MCSymbol *StrSym);
Eric Christopher6e472042011-11-07 09:18:42 +0000252#ifndef NDEBUG
253 void print(raw_ostream &O);
254 void dump() { print(dbgs()); }
255#endif
256};
Eric Christopher6e472042011-11-07 09:18:42 +0000257}
258#endif