DwarfAccelTable.h 8.2 KB

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