AccelTable.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. uint16_t Version = 5;
  165. uint16_t Padding = 0;
  166. uint32_t CompUnitCount;
  167. uint32_t LocalTypeUnitCount = 0;
  168. uint32_t ForeignTypeUnitCount = 0;
  169. uint32_t BucketCount;
  170. uint32_t NameCount;
  171. uint32_t AbbrevTableSize = 0;
  172. uint32_t AugmentationStringSize = sizeof(AugmentationString);
  173. char AugmentationString[8] = {'L', 'L', 'V', 'M', '0', '7', '0', '0'};
  174. Header(uint32_t CompUnitCount, uint32_t BucketCount, uint32_t NameCount)
  175. : CompUnitCount(CompUnitCount), BucketCount(BucketCount),
  176. NameCount(NameCount) {}
  177. void emit(Dwarf5AccelTableWriter &Ctx);
  178. };
  179. struct AttributeEncoding {
  180. dwarf::Index Index;
  181. dwarf::Form Form;
  182. };
  183. Header Header;
  184. DenseMap<uint32_t, SmallVector<AttributeEncoding, 2>> Abbreviations;
  185. ArrayRef<MCSymbol *> CompUnits;
  186. llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry;
  187. MCSymbol *ContributionEnd = nullptr;
  188. MCSymbol *AbbrevStart = Asm->createTempSymbol("names_abbrev_start");
  189. MCSymbol *AbbrevEnd = Asm->createTempSymbol("names_abbrev_end");
  190. MCSymbol *EntryPool = Asm->createTempSymbol("names_entries");
  191. DenseSet<uint32_t> getUniqueTags() const;
  192. // Right now, we emit uniform attributes for all tags.
  193. SmallVector<AttributeEncoding, 2> getUniformAttributes() const;
  194. void emitCUList() const;
  195. void emitBuckets() const;
  196. void emitStringOffsets() const;
  197. void emitAbbrevs() const;
  198. void emitEntry(const DataT &Entry) const;
  199. void emitData() const;
  200. public:
  201. Dwarf5AccelTableWriter(
  202. AsmPrinter *Asm, const AccelTableBase &Contents,
  203. ArrayRef<MCSymbol *> CompUnits,
  204. llvm::function_ref<unsigned(const DataT &)> GetCUIndexForEntry);
  205. void emit();
  206. };
  207. } // namespace
  208. void AccelTableWriter::emitHashes() const {
  209. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  210. unsigned BucketIdx = 0;
  211. for (auto &Bucket : Contents.getBuckets()) {
  212. for (auto &Hash : Bucket) {
  213. uint32_t HashValue = Hash->HashValue;
  214. if (SkipIdenticalHashes && PrevHash == HashValue)
  215. continue;
  216. Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(BucketIdx));
  217. Asm->emitInt32(HashValue);
  218. PrevHash = HashValue;
  219. }
  220. BucketIdx++;
  221. }
  222. }
  223. void AccelTableWriter::emitOffsets(const MCSymbol *Base) const {
  224. const auto &Buckets = Contents.getBuckets();
  225. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  226. for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
  227. for (auto *Hash : Buckets[i]) {
  228. uint32_t HashValue = Hash->HashValue;
  229. if (SkipIdenticalHashes && PrevHash == HashValue)
  230. continue;
  231. PrevHash = HashValue;
  232. Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i));
  233. Asm->emitLabelDifference(Hash->Sym, Base, Asm->getDwarfOffsetByteSize());
  234. }
  235. }
  236. }
  237. void AppleAccelTableWriter::Header::emit(AsmPrinter *Asm) const {
  238. Asm->OutStreamer->AddComment("Header Magic");
  239. Asm->emitInt32(Magic);
  240. Asm->OutStreamer->AddComment("Header Version");
  241. Asm->emitInt16(Version);
  242. Asm->OutStreamer->AddComment("Header Hash Function");
  243. Asm->emitInt16(HashFunction);
  244. Asm->OutStreamer->AddComment("Header Bucket Count");
  245. Asm->emitInt32(BucketCount);
  246. Asm->OutStreamer->AddComment("Header Hash Count");
  247. Asm->emitInt32(HashCount);
  248. Asm->OutStreamer->AddComment("Header Data Length");
  249. Asm->emitInt32(HeaderDataLength);
  250. }
  251. void AppleAccelTableWriter::HeaderData::emit(AsmPrinter *Asm) const {
  252. Asm->OutStreamer->AddComment("HeaderData Die Offset Base");
  253. Asm->emitInt32(DieOffsetBase);
  254. Asm->OutStreamer->AddComment("HeaderData Atom Count");
  255. Asm->emitInt32(Atoms.size());
  256. for (const Atom &A : Atoms) {
  257. Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.Type));
  258. Asm->emitInt16(A.Type);
  259. Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.Form));
  260. Asm->emitInt16(A.Form);
  261. }
  262. }
  263. void AppleAccelTableWriter::emitBuckets() const {
  264. const auto &Buckets = Contents.getBuckets();
  265. unsigned index = 0;
  266. for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
  267. Asm->OutStreamer->AddComment("Bucket " + Twine(i));
  268. if (!Buckets[i].empty())
  269. Asm->emitInt32(index);
  270. else
  271. Asm->emitInt32(std::numeric_limits<uint32_t>::max());
  272. // Buckets point in the list of hashes, not to the data. Do not increment
  273. // the index multiple times in case of hash collisions.
  274. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  275. for (auto *HD : Buckets[i]) {
  276. uint32_t HashValue = HD->HashValue;
  277. if (PrevHash != HashValue)
  278. ++index;
  279. PrevHash = HashValue;
  280. }
  281. }
  282. }
  283. void AppleAccelTableWriter::emitData() const {
  284. const auto &Buckets = Contents.getBuckets();
  285. for (const AccelTableBase::HashList &Bucket : Buckets) {
  286. uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
  287. for (auto &Hash : Bucket) {
  288. // Terminate the previous entry if there is no hash collision with the
  289. // current one.
  290. if (PrevHash != std::numeric_limits<uint64_t>::max() &&
  291. PrevHash != Hash->HashValue)
  292. Asm->emitInt32(0);
  293. // Remember to emit the label for our offset.
  294. Asm->OutStreamer->emitLabel(Hash->Sym);
  295. Asm->OutStreamer->AddComment(Hash->Name.getString());
  296. Asm->emitDwarfStringOffset(Hash->Name);
  297. Asm->OutStreamer->AddComment("Num DIEs");
  298. Asm->emitInt32(Hash->Values.size());
  299. for (const auto *V : Hash->Values)
  300. static_cast<const AppleAccelTableData *>(V)->emit(Asm);
  301. PrevHash = Hash->HashValue;
  302. }
  303. // Emit the final end marker for the bucket.
  304. if (!Bucket.empty())
  305. Asm->emitInt32(0);
  306. }
  307. }
  308. void AppleAccelTableWriter::emit() const {
  309. Header.emit(Asm);
  310. HeaderData.emit(Asm);
  311. emitBuckets();
  312. emitHashes();
  313. emitOffsets(SecBegin);
  314. emitData();
  315. }
  316. template <typename DataT>
  317. void Dwarf5AccelTableWriter<DataT>::Header::emit(Dwarf5AccelTableWriter &Ctx) {
  318. assert(CompUnitCount > 0 && "Index must have at least one CU.");
  319. AsmPrinter *Asm = Ctx.Asm;
  320. Ctx.ContributionEnd =
  321. Asm->emitDwarfUnitLength("names", "Header: unit length");
  322. Asm->OutStreamer->AddComment("Header: version");
  323. Asm->emitInt16(Version);
  324. Asm->OutStreamer->AddComment("Header: padding");
  325. Asm->emitInt16(Padding);
  326. Asm->OutStreamer->AddComment("Header: compilation unit count");
  327. Asm->emitInt32(CompUnitCount);
  328. Asm->OutStreamer->AddComment("Header: local type unit count");
  329. Asm->emitInt32(LocalTypeUnitCount);
  330. Asm->OutStreamer->AddComment("Header: foreign type unit count");
  331. Asm->emitInt32(ForeignTypeUnitCount);
  332. Asm->OutStreamer->AddComment("Header: bucket count");
  333. Asm->emitInt32(BucketCount);
  334. Asm->OutStreamer->AddComment("Header: name count");
  335. Asm->emitInt32(NameCount);
  336. Asm->OutStreamer->AddComment("Header: abbreviation table size");
  337. Asm->emitLabelDifference(Ctx.AbbrevEnd, Ctx.AbbrevStart, sizeof(uint32_t));
  338. Asm->OutStreamer->AddComment("Header: augmentation string size");
  339. assert(AugmentationStringSize % 4 == 0);
  340. Asm->emitInt32(AugmentationStringSize);
  341. Asm->OutStreamer->AddComment("Header: augmentation string");
  342. Asm->OutStreamer->emitBytes({AugmentationString, AugmentationStringSize});
  343. }
  344. template <typename DataT>
  345. DenseSet<uint32_t> Dwarf5AccelTableWriter<DataT>::getUniqueTags() const {
  346. DenseSet<uint32_t> UniqueTags;
  347. for (auto &Bucket : Contents.getBuckets()) {
  348. for (auto *Hash : Bucket) {
  349. for (auto *Value : Hash->Values) {
  350. unsigned Tag = static_cast<const DataT *>(Value)->getDieTag();
  351. UniqueTags.insert(Tag);
  352. }
  353. }
  354. }
  355. return UniqueTags;
  356. }
  357. template <typename DataT>
  358. SmallVector<typename Dwarf5AccelTableWriter<DataT>::AttributeEncoding, 2>
  359. Dwarf5AccelTableWriter<DataT>::getUniformAttributes() const {
  360. SmallVector<AttributeEncoding, 2> UA;
  361. if (CompUnits.size() > 1) {
  362. size_t LargestCUIndex = CompUnits.size() - 1;
  363. dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, LargestCUIndex);
  364. UA.push_back({dwarf::DW_IDX_compile_unit, Form});
  365. }
  366. UA.push_back({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4});
  367. return UA;
  368. }
  369. template <typename DataT>
  370. void Dwarf5AccelTableWriter<DataT>::emitCUList() const {
  371. for (const auto &CU : enumerate(CompUnits)) {
  372. Asm->OutStreamer->AddComment("Compilation unit " + Twine(CU.index()));
  373. Asm->emitDwarfSymbolReference(CU.value());
  374. }
  375. }
  376. template <typename DataT>
  377. void Dwarf5AccelTableWriter<DataT>::emitBuckets() const {
  378. uint32_t Index = 1;
  379. for (const auto &Bucket : enumerate(Contents.getBuckets())) {
  380. Asm->OutStreamer->AddComment("Bucket " + Twine(Bucket.index()));
  381. Asm->emitInt32(Bucket.value().empty() ? 0 : Index);
  382. Index += Bucket.value().size();
  383. }
  384. }
  385. template <typename DataT>
  386. void Dwarf5AccelTableWriter<DataT>::emitStringOffsets() const {
  387. for (const auto &Bucket : enumerate(Contents.getBuckets())) {
  388. for (auto *Hash : Bucket.value()) {
  389. DwarfStringPoolEntryRef String = Hash->Name;
  390. Asm->OutStreamer->AddComment("String in Bucket " + Twine(Bucket.index()) +
  391. ": " + String.getString());
  392. Asm->emitDwarfStringOffset(String);
  393. }
  394. }
  395. }
  396. template <typename DataT>
  397. void Dwarf5AccelTableWriter<DataT>::emitAbbrevs() const {
  398. Asm->OutStreamer->emitLabel(AbbrevStart);
  399. for (const auto &Abbrev : Abbreviations) {
  400. Asm->OutStreamer->AddComment("Abbrev code");
  401. assert(Abbrev.first != 0);
  402. Asm->emitULEB128(Abbrev.first);
  403. Asm->OutStreamer->AddComment(dwarf::TagString(Abbrev.first));
  404. Asm->emitULEB128(Abbrev.first);
  405. for (const auto &AttrEnc : Abbrev.second) {
  406. Asm->emitULEB128(AttrEnc.Index, dwarf::IndexString(AttrEnc.Index).data());
  407. Asm->emitULEB128(AttrEnc.Form,
  408. dwarf::FormEncodingString(AttrEnc.Form).data());
  409. }
  410. Asm->emitULEB128(0, "End of abbrev");
  411. Asm->emitULEB128(0, "End of abbrev");
  412. }
  413. Asm->emitULEB128(0, "End of abbrev list");
  414. Asm->OutStreamer->emitLabel(AbbrevEnd);
  415. }
  416. template <typename DataT>
  417. void Dwarf5AccelTableWriter<DataT>::emitEntry(const DataT &Entry) const {
  418. auto AbbrevIt = Abbreviations.find(Entry.getDieTag());
  419. assert(AbbrevIt != Abbreviations.end() &&
  420. "Why wasn't this abbrev generated?");
  421. Asm->emitULEB128(AbbrevIt->first, "Abbreviation code");
  422. for (const auto &AttrEnc : AbbrevIt->second) {
  423. Asm->OutStreamer->AddComment(dwarf::IndexString(AttrEnc.Index));
  424. switch (AttrEnc.Index) {
  425. case dwarf::DW_IDX_compile_unit: {
  426. DIEInteger ID(getCUIndexForEntry(Entry));
  427. ID.emitValue(Asm, AttrEnc.Form);
  428. break;
  429. }
  430. case dwarf::DW_IDX_die_offset:
  431. assert(AttrEnc.Form == dwarf::DW_FORM_ref4);
  432. Asm->emitInt32(Entry.getDieOffset());
  433. break;
  434. default:
  435. llvm_unreachable("Unexpected index attribute!");
  436. }
  437. }
  438. }
  439. template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emitData() const {
  440. Asm->OutStreamer->emitLabel(EntryPool);
  441. for (auto &Bucket : Contents.getBuckets()) {
  442. for (auto *Hash : Bucket) {
  443. // Remember to emit the label for our offset.
  444. Asm->OutStreamer->emitLabel(Hash->Sym);
  445. for (const auto *Value : Hash->Values)
  446. emitEntry(*static_cast<const DataT *>(Value));
  447. Asm->OutStreamer->AddComment("End of list: " + Hash->Name.getString());
  448. Asm->emitInt8(0);
  449. }
  450. }
  451. }
  452. template <typename DataT>
  453. Dwarf5AccelTableWriter<DataT>::Dwarf5AccelTableWriter(
  454. AsmPrinter *Asm, const AccelTableBase &Contents,
  455. ArrayRef<MCSymbol *> CompUnits,
  456. llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry)
  457. : AccelTableWriter(Asm, Contents, false),
  458. Header(CompUnits.size(), Contents.getBucketCount(),
  459. Contents.getUniqueNameCount()),
  460. CompUnits(CompUnits), getCUIndexForEntry(std::move(getCUIndexForEntry)) {
  461. DenseSet<uint32_t> UniqueTags = getUniqueTags();
  462. SmallVector<AttributeEncoding, 2> UniformAttributes = getUniformAttributes();
  463. Abbreviations.reserve(UniqueTags.size());
  464. for (uint32_t Tag : UniqueTags)
  465. Abbreviations.try_emplace(Tag, UniformAttributes);
  466. }
  467. template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() {
  468. Header.emit(*this);
  469. emitCUList();
  470. emitBuckets();
  471. emitHashes();
  472. emitStringOffsets();
  473. emitOffsets(EntryPool);
  474. emitAbbrevs();
  475. emitData();
  476. Asm->OutStreamer->emitValueToAlignment(4, 0);
  477. Asm->OutStreamer->emitLabel(ContributionEnd);
  478. }
  479. void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
  480. StringRef Prefix, const MCSymbol *SecBegin,
  481. ArrayRef<AppleAccelTableData::Atom> Atoms) {
  482. Contents.finalize(Asm, Prefix);
  483. AppleAccelTableWriter(Asm, Contents, Atoms, SecBegin).emit();
  484. }
  485. void llvm::emitDWARF5AccelTable(
  486. AsmPrinter *Asm, AccelTable<DWARF5AccelTableData> &Contents,
  487. const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
  488. std::vector<MCSymbol *> CompUnits;
  489. SmallVector<unsigned, 1> CUIndex(CUs.size());
  490. int Count = 0;
  491. for (const auto &CU : enumerate(CUs)) {
  492. if (CU.value()->getCUNode()->getNameTableKind() !=
  493. DICompileUnit::DebugNameTableKind::Default)
  494. continue;
  495. CUIndex[CU.index()] = Count++;
  496. assert(CU.index() == CU.value()->getUniqueID());
  497. const DwarfCompileUnit *MainCU =
  498. DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get();
  499. CompUnits.push_back(MainCU->getLabelBegin());
  500. }
  501. if (CompUnits.empty())
  502. return;
  503. Asm->OutStreamer->SwitchSection(
  504. Asm->getObjFileLowering().getDwarfDebugNamesSection());
  505. Contents.finalize(Asm, "names");
  506. Dwarf5AccelTableWriter<DWARF5AccelTableData>(
  507. Asm, Contents, CompUnits,
  508. [&](const DWARF5AccelTableData &Entry) {
  509. const DIE *CUDie = Entry.getDie().getUnitDie();
  510. return CUIndex[DD.lookupCU(CUDie)->getUniqueID()];
  511. })
  512. .emit();
  513. }
  514. void llvm::emitDWARF5AccelTable(
  515. AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
  516. ArrayRef<MCSymbol *> CUs,
  517. llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
  518. getCUIndexForEntry) {
  519. Contents.finalize(Asm, "names");
  520. Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs,
  521. getCUIndexForEntry)
  522. .emit();
  523. }
  524. void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const {
  525. assert(Die.getDebugSectionOffset() <= UINT32_MAX &&
  526. "The section offset exceeds the limit.");
  527. Asm->emitInt32(Die.getDebugSectionOffset());
  528. }
  529. void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const {
  530. assert(Die.getDebugSectionOffset() <= UINT32_MAX &&
  531. "The section offset exceeds the limit.");
  532. Asm->emitInt32(Die.getDebugSectionOffset());
  533. Asm->emitInt16(Die.getTag());
  534. Asm->emitInt8(0);
  535. }
  536. void AppleAccelTableStaticOffsetData::emit(AsmPrinter *Asm) const {
  537. Asm->emitInt32(Offset);
  538. }
  539. void AppleAccelTableStaticTypeData::emit(AsmPrinter *Asm) const {
  540. Asm->emitInt32(Offset);
  541. Asm->emitInt16(Tag);
  542. Asm->emitInt8(ObjCClassIsImplementation ? dwarf::DW_FLAG_type_implementation
  543. : 0);
  544. Asm->emitInt32(QualifiedNameHash);
  545. }
  546. constexpr AppleAccelTableData::Atom AppleAccelTableTypeData::Atoms[];
  547. constexpr AppleAccelTableData::Atom AppleAccelTableOffsetData::Atoms[];
  548. constexpr AppleAccelTableData::Atom AppleAccelTableStaticOffsetData::Atoms[];
  549. constexpr AppleAccelTableData::Atom AppleAccelTableStaticTypeData::Atoms[];
  550. #ifndef NDEBUG
  551. void AppleAccelTableWriter::Header::print(raw_ostream &OS) const {
  552. OS << "Magic: " << format("0x%x", Magic) << "\n"
  553. << "Version: " << Version << "\n"
  554. << "Hash Function: " << HashFunction << "\n"
  555. << "Bucket Count: " << BucketCount << "\n"
  556. << "Header Data Length: " << HeaderDataLength << "\n";
  557. }
  558. void AppleAccelTableData::Atom::print(raw_ostream &OS) const {
  559. OS << "Type: " << dwarf::AtomTypeString(Type) << "\n"
  560. << "Form: " << dwarf::FormEncodingString(Form) << "\n";
  561. }
  562. void AppleAccelTableWriter::HeaderData::print(raw_ostream &OS) const {
  563. OS << "DIE Offset Base: " << DieOffsetBase << "\n";
  564. for (auto Atom : Atoms)
  565. Atom.print(OS);
  566. }
  567. void AppleAccelTableWriter::print(raw_ostream &OS) const {
  568. Header.print(OS);
  569. HeaderData.print(OS);
  570. Contents.print(OS);
  571. SecBegin->print(OS, nullptr);
  572. }
  573. void AccelTableBase::HashData::print(raw_ostream &OS) const {
  574. OS << "Name: " << Name.getString() << "\n";
  575. OS << " Hash Value: " << format("0x%x", HashValue) << "\n";
  576. OS << " Symbol: ";
  577. if (Sym)
  578. OS << *Sym;
  579. else
  580. OS << "<none>";
  581. OS << "\n";
  582. for (auto *Value : Values)
  583. Value->print(OS);
  584. }
  585. void AccelTableBase::print(raw_ostream &OS) const {
  586. // Print Content.
  587. OS << "Entries: \n";
  588. for (const auto &Entry : Entries) {
  589. OS << "Name: " << Entry.first() << "\n";
  590. for (auto *V : Entry.second.Values)
  591. V->print(OS);
  592. }
  593. OS << "Buckets and Hashes: \n";
  594. for (auto &Bucket : Buckets)
  595. for (auto &Hash : Bucket)
  596. Hash->print(OS);
  597. OS << "Data: \n";
  598. for (auto &E : Entries)
  599. E.second.print(OS);
  600. }
  601. void DWARF5AccelTableData::print(raw_ostream &OS) const {
  602. OS << " Offset: " << getDieOffset() << "\n";
  603. OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
  604. }
  605. void DWARF5AccelTableStaticData::print(raw_ostream &OS) const {
  606. OS << " Offset: " << getDieOffset() << "\n";
  607. OS << " Tag: " << dwarf::TagString(getDieTag()) << "\n";
  608. }
  609. void AppleAccelTableOffsetData::print(raw_ostream &OS) const {
  610. OS << " Offset: " << Die.getOffset() << "\n";
  611. }
  612. void AppleAccelTableTypeData::print(raw_ostream &OS) const {
  613. OS << " Offset: " << Die.getOffset() << "\n";
  614. OS << " Tag: " << dwarf::TagString(Die.getTag()) << "\n";
  615. }
  616. void AppleAccelTableStaticOffsetData::print(raw_ostream &OS) const {
  617. OS << " Static Offset: " << Offset << "\n";
  618. }
  619. void AppleAccelTableStaticTypeData::print(raw_ostream &OS) const {
  620. OS << " Static Offset: " << Offset << "\n";
  621. OS << " QualifiedNameHash: " << format("%x\n", QualifiedNameHash) << "\n";
  622. OS << " Tag: " << dwarf::TagString(Tag) << "\n";
  623. OS << " ObjCClassIsImplementation: "
  624. << (ObjCClassIsImplementation ? "true" : "false");
  625. OS << "\n";
  626. }
  627. #endif