AccelTable.cpp 24 KB

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