AccelTable.cpp 25 KB

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