AccelTable.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. //===- llvm/CodeGen/AsmPrinter/AccelTable.cpp - Accelerator Tables --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains support for writing accelerator tables.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/AccelTable.h"
  13. #include "DwarfCompileUnit.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/BinaryFormat/Dwarf.h"
  18. #include "llvm/CodeGen/AsmPrinter.h"
  19. #include "llvm/CodeGen/DIE.h"
  20. #include "llvm/MC/MCStreamer.h"
  21. #include "llvm/MC/MCSymbol.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include "llvm/Target/TargetLoweringObjectFile.h"
  24. #include <algorithm>
  25. #include <cstddef>
  26. #include <cstdint>
  27. #include <limits>
  28. #include <vector>
  29. using namespace llvm;
  30. void AccelTableBase::computeBucketCount() {
  31. // First get the number of unique hashes.
  32. std::vector<uint32_t> Uniques;
  33. Uniques.reserve(Entries.size());
  34. for (const auto &E : Entries)
  35. Uniques.push_back(E.second.HashValue);
  36. array_pod_sort(Uniques.begin(), Uniques.end());
  37. std::vector<uint32_t>::iterator P =
  38. std::unique(Uniques.begin(), Uniques.end());
  39. UniqueHashCount = std::distance(Uniques.begin(), P);
  40. if (UniqueHashCount > 1024)
  41. BucketCount = UniqueHashCount / 4;
  42. else if (UniqueHashCount > 16)
  43. BucketCount = UniqueHashCount / 2;
  44. else
  45. BucketCount = std::max<uint32_t>(UniqueHashCount, 1);
  46. }
  47. void AccelTableBase::finalize(AsmPrinter *Asm, StringRef Prefix) {
  48. // Create the individual hash data outputs.
  49. for (auto &E : Entries) {
  50. // Unique the entries.
  51. llvm::stable_sort(E.second.Values,
  52. [](const AccelTableData *A, const AccelTableData *B) {
  53. return *A < *B;
  54. });
  55. E.second.Values.erase(
  56. std::unique(E.second.Values.begin(), E.second.Values.end()),
  57. E.second.Values.end());
  58. }
  59. // Figure out how many buckets we need, then compute the bucket contents and
  60. // the final ordering. The hashes and offsets can be emitted by walking these
  61. // data structures. We add temporary symbols to the data so they can be
  62. // referenced when emitting the offsets.
  63. computeBucketCount();
  64. // Compute bucket contents and final ordering.
  65. Buckets.resize(BucketCount);
  66. for (auto &E : Entries) {
  67. uint32_t Bucket = E.second.HashValue % BucketCount;
  68. Buckets[Bucket].push_back(&E.second);
  69. E.second.Sym = Asm->createTempSymbol(Prefix);
  70. }
  71. // Sort the contents of the buckets by hash value so that hash collisions end
  72. // up together. Stable sort makes testing easier and doesn't cost much more.
  73. for (auto &Bucket : Buckets)
  74. llvm::stable_sort(Bucket, [](HashData *LHS, HashData *RHS) {
  75. return LHS->HashValue < RHS->HashValue;
  76. });
  77. }
  78. namespace {
  79. /// Base class for writing out Accelerator tables. It holds the common
  80. /// functionality for the two Accelerator table types.
  81. class AccelTableWriter {
  82. protected:
  83. AsmPrinter *const Asm; ///< Destination.
  84. const AccelTableBase &Contents; ///< Data to emit.
  85. /// Controls whether to emit duplicate hash and offset table entries for names
  86. /// with identical hashes. Apple tables don't emit duplicate entries, DWARF v5
  87. /// tables do.
  88. const bool SkipIdenticalHashes;
  89. void emitHashes() const;
  90. /// Emit offsets to lists of entries with identical names. The offsets are
  91. /// relative to the Base argument.
  92. void emitOffsets(const MCSymbol *Base) const;
  93. public:
  94. AccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents,
  95. bool SkipIdenticalHashes)
  96. : Asm(Asm), Contents(Contents), SkipIdenticalHashes(SkipIdenticalHashes) {
  97. }
  98. };
  99. class AppleAccelTableWriter : public AccelTableWriter {
  100. using Atom = AppleAccelTableData::Atom;
  101. /// The fixed header of an Apple Accelerator Table.
  102. struct Header {
  103. uint32_t Magic = MagicHash;
  104. uint16_t Version = 1;
  105. uint16_t HashFunction = dwarf::DW_hash_function_djb;
  106. uint32_t BucketCount;
  107. uint32_t HashCount;
  108. uint32_t HeaderDataLength;
  109. /// 'HASH' magic value to detect endianness.
  110. static const uint32_t MagicHash = 0x48415348;
  111. Header(uint32_t BucketCount, uint32_t UniqueHashCount, uint32_t DataLength)
  112. : BucketCount(BucketCount), HashCount(UniqueHashCount),
  113. HeaderDataLength(DataLength) {}
  114. void emit(AsmPrinter *Asm) const;
  115. #ifndef NDEBUG
  116. void print(raw_ostream &OS) const;
  117. void dump() const { print(dbgs()); }
  118. #endif
  119. };
  120. /// The HeaderData describes the structure of an Apple accelerator table
  121. /// through a list of Atoms.
  122. struct HeaderData {
  123. /// In the case of data that is referenced via DW_FORM_ref_* the offset
  124. /// base is used to describe the offset for all forms in the list of atoms.
  125. uint32_t DieOffsetBase;
  126. const SmallVector<Atom, 4> Atoms;
  127. HeaderData(ArrayRef<Atom> AtomList, uint32_t Offset = 0)
  128. : DieOffsetBase(Offset), Atoms(AtomList.begin(), AtomList.end()) {}
  129. void emit(AsmPrinter *Asm) const;
  130. #ifndef NDEBUG
  131. void print(raw_ostream &OS) const;
  132. void dump() const { print(dbgs()); }
  133. #endif
  134. };
  135. Header Header;
  136. HeaderData HeaderData;
  137. const MCSymbol *SecBegin;
  138. void emitBuckets() const;
  139. void emitData() const;
  140. public:
  141. AppleAccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents,
  142. ArrayRef<Atom> Atoms, const MCSymbol *SecBegin)
  143. : AccelTableWriter(Asm, Contents, true),
  144. Header(Contents.getBucketCount(), Contents.getUniqueHashCount(),
  145. 8 + (Atoms.size() * 4)),
  146. HeaderData(Atoms), SecBegin(SecBegin) {}
  147. void emit() const;
  148. #ifndef NDEBUG
  149. void print(raw_ostream &OS) const;
  150. void dump() const { print(dbgs()); }
  151. #endif
  152. };
  153. /// Class responsible for emitting a DWARF v5 Accelerator Table. The only
  154. /// public function is emit(), which performs the actual emission.
  155. ///
  156. /// The class is templated in its data type. This allows us to emit both dyamic
  157. /// and static data entries. A callback abstract the logic to provide a CU
  158. /// index for a given entry, which is different per data type, but identical
  159. /// for every entry in the same table.
  160. template <typename DataT>
  161. class Dwarf5AccelTableWriter : public AccelTableWriter {
  162. struct Header {
  163. uint16_t Version = 5;
  164. uint16_t Padding = 0;
  165. uint32_t CompUnitCount;
  166. uint32_t LocalTypeUnitCount = 0;
  167. uint32_t ForeignTypeUnitCount = 0;
  168. uint32_t BucketCount;
  169. uint32_t NameCount;
  170. uint32_t AbbrevTableSize = 0;
  171. uint32_t AugmentationStringSize = sizeof(AugmentationString);
  172. char AugmentationString[8] = {'L', 'L', 'V', 'M', '0', '7', '0', '0'};
  173. Header(uint32_t CompUnitCount, uint32_t BucketCount, uint32_t NameCount)
  174. : CompUnitCount(CompUnitCount), BucketCount(BucketCount),
  175. NameCount(NameCount) {}
  176. void emit(Dwarf5AccelTableWriter &Ctx);
  177. };
  178. struct AttributeEncoding {
  179. dwarf::Index Index;
  180. dwarf::Form Form;
  181. };
  182. Header Header;
  183. DenseMap<uint32_t, SmallVector<AttributeEncoding, 2>> Abbreviations;
  184. ArrayRef<MCSymbol *> CompUnits;
  185. llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry;
  186. MCSymbol *ContributionEnd = nullptr;
  187. MCSymbol *AbbrevStart = Asm->createTempSymbol("names_abbrev_start");
  188. MCSymbol *AbbrevEnd = Asm->createTempSymbol("names_abbrev_end");
  189. MCSymbol *EntryPool = Asm->createTempSymbol("names_entries");
  190. DenseSet<uint32_t> getUniqueTags() const;
  191. // Right now, we emit uniform attributes for all tags.
  192. SmallVector<AttributeEncoding, 2> getUniformAttributes() const;
  193. void emitCUList() const;
  194. void emitBuckets() const;
  195. void emitStringOffsets() const;
  196. void emitAbbrevs() const;
  197. void emitEntry(const DataT &Entry) const;
  198. void emitData() const;
  199. public:
  200. Dwarf5AccelTableWriter(
  201. AsmPrinter *Asm, const AccelTableBase &Contents,
  202. ArrayRef<MCSymbol *> CompUnits,
  203. llvm::function_ref<unsigned(const DataT &)> GetCUIndexForEntry);
  204. void emit();
  205. };
  206. } // namespace
  207. void AccelTableWriter::emitHashes() const {
  208. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  209. unsigned BucketIdx = 0;
  210. for (const auto &Bucket : Contents.getBuckets()) {
  211. for (const auto &Hash : Bucket) {
  212. uint32_t HashValue = Hash->HashValue;
  213. if (SkipIdenticalHashes && PrevHash == HashValue)
  214. continue;
  215. Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(BucketIdx));
  216. Asm->emitInt32(HashValue);
  217. PrevHash = HashValue;
  218. }
  219. BucketIdx++;
  220. }
  221. }
  222. void AccelTableWriter::emitOffsets(const MCSymbol *Base) const {
  223. const auto &Buckets = Contents.getBuckets();
  224. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  225. for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
  226. for (auto *Hash : Buckets[i]) {
  227. uint32_t HashValue = Hash->HashValue;
  228. if (SkipIdenticalHashes && PrevHash == HashValue)
  229. continue;
  230. PrevHash = HashValue;
  231. Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i));
  232. Asm->emitLabelDifference(Hash->Sym, Base, Asm->getDwarfOffsetByteSize());
  233. }
  234. }
  235. }
  236. void AppleAccelTableWriter::Header::emit(AsmPrinter *Asm) const {
  237. Asm->OutStreamer->AddComment("Header Magic");
  238. Asm->emitInt32(Magic);
  239. Asm->OutStreamer->AddComment("Header Version");
  240. Asm->emitInt16(Version);
  241. Asm->OutStreamer->AddComment("Header Hash Function");
  242. Asm->emitInt16(HashFunction);
  243. Asm->OutStreamer->AddComment("Header Bucket Count");
  244. Asm->emitInt32(BucketCount);
  245. Asm->OutStreamer->AddComment("Header Hash Count");
  246. Asm->emitInt32(HashCount);
  247. Asm->OutStreamer->AddComment("Header Data Length");
  248. Asm->emitInt32(HeaderDataLength);
  249. }
  250. void AppleAccelTableWriter::HeaderData::emit(AsmPrinter *Asm) const {
  251. Asm->OutStreamer->AddComment("HeaderData Die Offset Base");
  252. Asm->emitInt32(DieOffsetBase);
  253. Asm->OutStreamer->AddComment("HeaderData Atom Count");
  254. Asm->emitInt32(Atoms.size());
  255. for (const Atom &A : Atoms) {
  256. Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.Type));
  257. Asm->emitInt16(A.Type);
  258. Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.Form));
  259. Asm->emitInt16(A.Form);
  260. }
  261. }
  262. void AppleAccelTableWriter::emitBuckets() const {
  263. const auto &Buckets = Contents.getBuckets();
  264. unsigned index = 0;
  265. for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
  266. Asm->OutStreamer->AddComment("Bucket " + Twine(i));
  267. if (!Buckets[i].empty())
  268. Asm->emitInt32(index);
  269. else
  270. Asm->emitInt32(std::numeric_limits<uint32_t>::max());
  271. // Buckets point in the list of hashes, not to the data. Do not increment
  272. // the index multiple times in case of hash collisions.
  273. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  274. for (auto *HD : Buckets[i]) {
  275. uint32_t HashValue = HD->HashValue;
  276. if (PrevHash != HashValue)
  277. ++index;
  278. PrevHash = HashValue;
  279. }
  280. }
  281. }
  282. void AppleAccelTableWriter::emitData() const {
  283. const auto &Buckets = Contents.getBuckets();
  284. for (const AccelTableBase::HashList &Bucket : Buckets) {
  285. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  286. for (const auto &Hash : Bucket) {
  287. // Terminate the previous entry if there is no hash collision with the
  288. // current one.
  289. if (PrevHash != std::numeric_limits<uint64_t>::max() &&
  290. PrevHash != Hash->HashValue)
  291. Asm->emitInt32(0);
  292. // Remember to emit the label for our offset.
  293. Asm->OutStreamer->emitLabel(Hash->Sym);
  294. Asm->OutStreamer->AddComment(Hash->Name.getString());
  295. Asm->emitDwarfStringOffset(Hash->Name);
  296. Asm->OutStreamer->AddComment("Num DIEs");
  297. Asm->emitInt32(Hash->Values.size());
  298. for (const auto *V : Hash->Values)
  299. static_cast<const AppleAccelTableData *>(V)->emit(Asm);
  300. PrevHash = Hash->HashValue;
  301. }
  302. // Emit the final end marker for the bucket.
  303. if (!Bucket.empty())
  304. Asm->emitInt32(0);
  305. }
  306. }
  307. void AppleAccelTableWriter::emit() const {
  308. Header.emit(Asm);
  309. HeaderData.emit(Asm);
  310. emitBuckets();
  311. emitHashes();
  312. emitOffsets(SecBegin);
  313. emitData();
  314. }
  315. template <typename DataT>
  316. void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) {
  317. assert(CompUnitCount > 0 && "Index must have at least one CU.");
  318. AsmPrinter *Asm = Ctx.Asm;
  319. Ctx.ContributionEnd =
  320. Asm->emitDwarfUnitLength("names", "Header: unit length");
  321. Asm->OutStreamer->AddComment("Header: version");
  322. Asm->emitInt16(Version);
  323. Asm->OutStreamer->AddComment("Header: padding");
  324. Asm->emitInt16(Padding);
  325. Asm->OutStreamer->AddComment("Header: compilation unit count");
  326. Asm->emitInt32(CompUnitCount);
  327. Asm->OutStreamer->AddComment("Header: local type unit count");
  328. Asm->emitInt32(LocalTypeUnitCount);
  329. Asm->OutStreamer->AddComment("Header: foreign type unit count");
  330. Asm->emitInt32(ForeignTypeUnitCount);
  331. Asm->OutStreamer->AddComment("Header: bucket count");
  332. Asm->emitInt32(BucketCount);
  333. Asm->OutStreamer->AddComment("Header: name count");
  334. Asm->emitInt32(NameCount);
  335. Asm->OutStreamer->AddComment("Header: abbreviation table size");
  336. Asm->emitLabelDifference(Ctx.AbbrevEnd, Ctx.AbbrevStart, sizeof(uint32_t));
  337. Asm->OutStreamer->AddComment("Header: augmentation string size");
  338. assert(AugmentationStringSize % 4 == 0);
  339. Asm->emitInt32(AugmentationStringSize);
  340. Asm->OutStreamer->AddComment("Header: augmentation string");
  341. Asm->OutStreamer->emitBytes({AugmentationString, AugmentationStringSize});
  342. }
  343. template <typename DataT>
  344. DenseSet<uint32_t> Dwarf5AccelTableWriter<DataT>::getUniqueTags() const {
  345. DenseSet<uint32_t> UniqueTags;
  346. for (auto &Bucket : Contents.getBuckets()) {
  347. for (auto *Hash : Bucket) {
  348. for (auto *Value : Hash->Values) {
  349. unsigned Tag = static_cast<const DataT *>(Value)->getDieTag();
  350. UniqueTags.insert(Tag);
  351. }
  352. }
  353. }
  354. return UniqueTags;
  355. }
  356. template <typename DataT>
  357. SmallVector<typename Dwarf5AccelTableWriter<DataT>::AttributeEncoding, 2>
  358. Dwarf5AccelTableWriter<DataT>::getUniformAttributes() const {
  359. SmallVector<AttributeEncoding, 2> UA;
  360. if (CompUnits.size() > 1) {
  361. size_t LargestCUIndex = CompUnits.size() - 1;
  362. dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, LargestCUIndex);
  363. UA.push_back({dwarf::DW_IDX_compile_unit, Form});
  364. }
  365. UA.push_back({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4});
  366. return UA;
  367. }
  368. template <typename DataT>
  369. void Dwarf5AccelTableWriter<DataT>::emitCUList() const {
  370. for (const auto &CU : enumerate(CompUnits)) {
  371. Asm->OutStreamer->AddComment("Compilation unit " + Twine(CU.index()));
  372. Asm->emitDwarfSymbolReference(CU.value());
  373. }
  374. }
  375. template <typename DataT>
  376. void Dwarf5AccelTableWriter<DataT>::emitBuckets() const {
  377. uint32_t Index = 1;
  378. for (const auto &Bucket : enumerate(Contents.getBuckets())) {
  379. Asm->OutStreamer->AddComment("Bucket " + Twine(Bucket.index()));
  380. Asm->emitInt32(Bucket.value().empty() ? 0 : Index);
  381. Index += Bucket.value().size();
  382. }
  383. }
  384. template <typename DataT>
  385. void Dwarf5AccelTableWriter<DataT>::emitStringOffsets() const {
  386. for (const auto &Bucket : enumerate(Contents.getBuckets())) {
  387. for (auto *Hash : Bucket.value()) {
  388. DwarfStringPoolEntryRef String = Hash->Name;
  389. Asm->OutStreamer->AddComment("String in Bucket " + Twine(Bucket.index()) +
  390. ": " + String.getString());
  391. Asm->emitDwarfStringOffset(String);
  392. }
  393. }
  394. }
  395. template <typename DataT>
  396. void Dwarf5AccelTableWriter<DataT>::emitAbbrevs() const {
  397. Asm->OutStreamer->emitLabel(AbbrevStart);
  398. for (const auto &Abbrev : Abbreviations) {
  399. Asm->OutStreamer->AddComment("Abbrev code");
  400. assert(Abbrev.first != 0);
  401. Asm->emitULEB128(Abbrev.first);
  402. Asm->OutStreamer->AddComment(dwarf::TagString(Abbrev.first));
  403. Asm->emitULEB128(Abbrev.first);
  404. for (const auto &AttrEnc : Abbrev.second) {
  405. Asm->emitULEB128(AttrEnc.Index, dwarf::IndexString(AttrEnc.Index).data());
  406. Asm->emitULEB128(AttrEnc.Form,
  407. dwarf::FormEncodingString(AttrEnc.Form).data());
  408. }
  409. Asm->emitULEB128(0, "End of abbrev");
  410. Asm->emitULEB128(0, "End of abbrev");
  411. }
  412. Asm->emitULEB128(0, "End of abbrev list");
  413. Asm->OutStreamer->emitLabel(AbbrevEnd);
  414. }
  415. template <typename DataT>
  416. void Dwarf5AccelTableWriter<DataT>::emitEntry(const DataT &Entry) const {
  417. auto AbbrevIt = Abbreviations.find(Entry.getDieTag());
  418. assert(AbbrevIt != Abbreviations.end() &&
  419. "Why wasn't this abbrev generated?");
  420. Asm->emitULEB128(AbbrevIt->first, "Abbreviation code");
  421. for (const auto &AttrEnc : AbbrevIt->second) {
  422. Asm->OutStreamer->AddComment(dwarf::IndexString(AttrEnc.Index));
  423. switch (AttrEnc.Index) {
  424. case dwarf::DW_IDX_compile_unit: {
  425. DIEInteger ID(getCUIndexForEntry(Entry));
  426. ID.emitValue(Asm, AttrEnc.Form);
  427. break;
  428. }
  429. case dwarf::DW_IDX_die_offset:
  430. assert(AttrEnc.Form == dwarf::DW_FORM_ref4);
  431. Asm->emitInt32(Entry.getDieOffset());
  432. break;
  433. default:
  434. llvm_unreachable("Unexpected index attribute!");
  435. }
  436. }
  437. }
  438. template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emitData() const {
  439. Asm->OutStreamer->emitLabel(EntryPool);
  440. for (auto &Bucket : Contents.getBuckets()) {
  441. for (auto *Hash : Bucket) {
  442. // Remember to emit the label for our offset.
  443. Asm->OutStreamer->emitLabel(Hash->Sym);
  444. for (const auto *Value : Hash->Values)
  445. emitEntry(*static_cast<const DataT *>(Value));
  446. Asm->OutStreamer->AddComment("End of list: " + Hash->Name.getString());
  447. Asm->emitInt8(0);
  448. }
  449. }
  450. }
  451. template <typename DataT>
  452. Dwarf5AccelTableWriter<DataT>::Dwarf5AccelTableWriter(
  453. AsmPrinter *Asm, const AccelTableBase &Contents,
  454. ArrayRef<MCSymbol *> CompUnits,
  455. llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry)
  456. : AccelTableWriter(Asm, Contents, false),
  457. Header(CompUnits.size(), Contents.getBucketCount(),
  458. Contents.getUniqueNameCount()),
  459. CompUnits(CompUnits), getCUIndexForEntry(std::move(getCUIndexForEntry)) {
  460. DenseSet<uint32_t> UniqueTags = getUniqueTags();
  461. SmallVector<AttributeEncoding, 2> UniformAttributes = getUniformAttributes();
  462. Abbreviations.reserve(UniqueTags.size());
  463. for (uint32_t Tag : UniqueTags)
  464. Abbreviations.try_emplace(Tag, UniformAttributes);
  465. }
  466. template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() {
  467. Header.emit(*this);
  468. emitCUList();
  469. emitBuckets();
  470. emitHashes();
  471. emitStringOffsets();
  472. emitOffsets(EntryPool);
  473. emitAbbrevs();
  474. emitData();
  475. Asm->OutStreamer->emitValueToAlignment(Align(4), 0);
  476. Asm->OutStreamer->emitLabel(ContributionEnd);
  477. }
  478. void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
  479. StringRef Prefix, const MCSymbol *SecBegin,
  480. ArrayRef<AppleAccelTableData::Atom> Atoms) {
  481. Contents.finalize(Asm, Prefix);
  482. AppleAccelTableWriter(Asm, Contents, Atoms, SecBegin).emit();
  483. }
  484. void llvm::emitDWARF5AccelTable(
  485. AsmPrinter *Asm, AccelTable<DWARF5AccelTableData> &Contents,
  486. const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
  487. std::vector<MCSymbol *> CompUnits;
  488. SmallVector<unsigned, 1> CUIndex(CUs.size());
  489. int Count = 0;
  490. for (const auto &CU : enumerate(CUs)) {
  491. if (CU.value()->getCUNode()->getNameTableKind() !=
  492. DICompileUnit::DebugNameTableKind::Default)
  493. continue;
  494. CUIndex[CU.index()] = Count++;
  495. assert(CU.index() == CU.value()->getUniqueID());
  496. const DwarfCompileUnit *MainCU =
  497. DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get();
  498. CompUnits.push_back(MainCU->getLabelBegin());
  499. }
  500. if (CompUnits.empty())
  501. return;
  502. Asm->OutStreamer->switchSection(
  503. Asm->getObjFileLowering().getDwarfDebugNamesSection());
  504. Contents.finalize(Asm, "names");
  505. Dwarf5AccelTableWriter<DWARF5AccelTableData>(
  506. Asm, Contents, CompUnits,
  507. [&](const DWARF5AccelTableData &Entry) {
  508. const DIE *CUDie = Entry.getDie().getUnitDie();
  509. return CUIndex[DD.lookupCU(CUDie)->getUniqueID()];
  510. })
  511. .emit();
  512. }
  513. void llvm::emitDWARF5AccelTable(
  514. AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
  515. ArrayRef<MCSymbol *> CUs,
  516. llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
  517. getCUIndexForEntry) {
  518. Contents.finalize(Asm, "names");
  519. Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs,
  520. getCUIndexForEntry)
  521. .emit();
  522. }
  523. void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const {
  524. assert(Die.getDebugSectionOffset() <= UINT32_MAX &&
  525. "The section offset exceeds the limit.");
  526. Asm->emitInt32(Die.getDebugSectionOffset());
  527. }
  528. void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const {
  529. assert(Die.getDebugSectionOffset() <= UINT32_MAX &&
  530. "The section offset exceeds the limit.");
  531. Asm->emitInt32(Die.getDebugSectionOffset());
  532. Asm->emitInt16(Die.getTag());
  533. Asm->emitInt8(0);
  534. }
  535. void AppleAccelTableStaticOffsetData::emit(AsmPrinter *Asm) const {
  536. Asm->emitInt32(Offset);
  537. }
  538. void AppleAccelTableStaticTypeData::emit(AsmPrinter *Asm) const {
  539. Asm->emitInt32(Offset);
  540. Asm->emitInt16(Tag);
  541. Asm->emitInt8(ObjCClassIsImplementation ? dwarf::DW_FLAG_type_implementation
  542. : 0);
  543. Asm->emitInt32(QualifiedNameHash);
  544. }
  545. constexpr AppleAccelTableData::Atom AppleAccelTableTypeData::Atoms[];
  546. constexpr AppleAccelTableData::Atom AppleAccelTableOffsetData::Atoms[];
  547. constexpr AppleAccelTableData::Atom AppleAccelTableStaticOffsetData::Atoms[];
  548. constexpr AppleAccelTableData::Atom AppleAccelTableStaticTypeData::Atoms[];
  549. #ifndef NDEBUG
  550. void AppleAccelTableWriter::Header::print(raw_ostream &OS) const {
  551. OS << "Magic: " << format("0x%x", Magic) << "\n"
  552. << "Version: " << Version << "\n"
  553. << "Hash Function: " << HashFunction << "\n"
  554. << "Bucket Count: " << BucketCount << "\n"
  555. << "Header Data Length: " << HeaderDataLength << "\n";
  556. }
  557. void AppleAccelTableData::Atom::print(raw_ostream &OS) const {
  558. OS << "Type: " << dwarf::AtomTypeString(Type) << "\n"
  559. << "Form: " << dwarf::FormEncodingString(Form) << "\n";
  560. }
  561. void AppleAccelTableWriter::HeaderData::print(raw_ostream &OS) const {
  562. OS << "DIE Offset Base: " << DieOffsetBase << "\n";
  563. for (auto Atom : Atoms)
  564. Atom.print(OS);
  565. }
  566. void AppleAccelTableWriter::print(raw_ostream &OS) const {
  567. Header.print(OS);
  568. HeaderData.print(OS);
  569. Contents.print(OS);
  570. SecBegin->print(OS, nullptr);
  571. }
  572. void AccelTableBase::HashData::print(raw_ostream &OS) const {
  573. OS << "Name: " << Name.getString() << "\n";
  574. OS << " Hash Value: " << format("0x%x", HashValue) << "\n";
  575. OS << " Symbol: ";
  576. if (Sym)
  577. OS << *Sym;
  578. else
  579. OS << "<none>";
  580. OS << "\n";
  581. for (auto *Value : Values)
  582. Value->print(OS);
  583. }
  584. void AccelTableBase::print(raw_ostream &OS) const {
  585. // Print Content.
  586. OS << "Entries: \n";
  587. for (const auto &Entry : Entries) {
  588. OS << "Name: " << Entry.first() << "\n";
  589. for (auto *V : Entry.second.Values)
  590. V->print(OS);
  591. }
  592. OS << "Buckets and Hashes: \n";
  593. for (const auto &Bucket : Buckets)
  594. for (const auto &Hash : Bucket)
  595. Hash->print(OS);
  596. OS << "Data: \n";
  597. for (const auto &E : Entries)
  598. E.second.print(OS);
  599. }
  600. void DWARF5AccelTableData::print(raw_ostream &OS) const {
  601. OS << " Offset: " << getDieOffset() << "\n";
  602. OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
  603. }
  604. void DWARF5AccelTableStaticData::print(raw_ostream &OS) const {
  605. OS << " Offset: " << getDieOffset() << "\n";
  606. OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
  607. }
  608. void AppleAccelTableOffsetData::print(raw_ostream &OS) const {
  609. OS << " Offset: " << Die.getOffset() << "\n";
  610. }
  611. void AppleAccelTableTypeData::print(raw_ostream &OS) const {
  612. OS << " Offset: " << Die.getOffset() << "\n";
  613. OS << " Tag: " << dwarf::TagString(Die.getTag()) << "\n";
  614. }
  615. void AppleAccelTableStaticOffsetData::print(raw_ostream &OS) const {
  616. OS << " Static Offset: " << Offset << "\n";
  617. }
  618. void AppleAccelTableStaticTypeData::print(raw_ostream &OS) const {
  619. OS << " Static Offset: " << Offset << "\n";
  620. OS << " QualifiedNameHash: " << format("%x\n", QualifiedNameHash) << "\n";
  621. OS << " Tag: " << dwarf::TagString(Tag) << "\n";
  622. OS << " ObjCClassIsImplementation: "
  623. << (ObjCClassIsImplementation ? "true" : "false");
  624. OS << "\n";
  625. }
  626. #endif