14ba319b5SDimitry Andric //===- llvm/CodeGen/AsmPrinter/AccelTable.cpp - Accelerator Tables --------===//
24ba319b5SDimitry Andric //
34ba319b5SDimitry Andric //                     The LLVM Compiler Infrastructure
44ba319b5SDimitry Andric //
54ba319b5SDimitry Andric // This file is distributed under the University of Illinois Open Source
64ba319b5SDimitry Andric // License. See LICENSE.TXT for details.
74ba319b5SDimitry Andric //
84ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
94ba319b5SDimitry Andric //
104ba319b5SDimitry Andric // This file contains support for writing accelerator tables.
114ba319b5SDimitry Andric //
124ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
134ba319b5SDimitry Andric 
144ba319b5SDimitry Andric #include "llvm/CodeGen/AccelTable.h"
154ba319b5SDimitry Andric #include "DwarfCompileUnit.h"
164ba319b5SDimitry Andric #include "llvm/ADT/STLExtras.h"
174ba319b5SDimitry Andric #include "llvm/ADT/StringMap.h"
184ba319b5SDimitry Andric #include "llvm/ADT/Twine.h"
194ba319b5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
204ba319b5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
214ba319b5SDimitry Andric #include "llvm/CodeGen/DIE.h"
224ba319b5SDimitry Andric #include "llvm/MC/MCExpr.h"
234ba319b5SDimitry Andric #include "llvm/MC/MCStreamer.h"
244ba319b5SDimitry Andric #include "llvm/MC/MCSymbol.h"
254ba319b5SDimitry Andric #include "llvm/Support/raw_ostream.h"
26*b5893f02SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
274ba319b5SDimitry Andric #include <algorithm>
284ba319b5SDimitry Andric #include <cstddef>
294ba319b5SDimitry Andric #include <cstdint>
304ba319b5SDimitry Andric #include <limits>
314ba319b5SDimitry Andric #include <vector>
324ba319b5SDimitry Andric 
334ba319b5SDimitry Andric using namespace llvm;
344ba319b5SDimitry Andric 
computeBucketCount()354ba319b5SDimitry Andric void AccelTableBase::computeBucketCount() {
364ba319b5SDimitry Andric   // First get the number of unique hashes.
374ba319b5SDimitry Andric   std::vector<uint32_t> Uniques;
384ba319b5SDimitry Andric   Uniques.reserve(Entries.size());
394ba319b5SDimitry Andric   for (const auto &E : Entries)
404ba319b5SDimitry Andric     Uniques.push_back(E.second.HashValue);
414ba319b5SDimitry Andric   array_pod_sort(Uniques.begin(), Uniques.end());
424ba319b5SDimitry Andric   std::vector<uint32_t>::iterator P =
434ba319b5SDimitry Andric       std::unique(Uniques.begin(), Uniques.end());
444ba319b5SDimitry Andric 
454ba319b5SDimitry Andric   UniqueHashCount = std::distance(Uniques.begin(), P);
464ba319b5SDimitry Andric 
474ba319b5SDimitry Andric   if (UniqueHashCount > 1024)
484ba319b5SDimitry Andric     BucketCount = UniqueHashCount / 4;
494ba319b5SDimitry Andric   else if (UniqueHashCount > 16)
504ba319b5SDimitry Andric     BucketCount = UniqueHashCount / 2;
514ba319b5SDimitry Andric   else
524ba319b5SDimitry Andric     BucketCount = std::max<uint32_t>(UniqueHashCount, 1);
534ba319b5SDimitry Andric }
544ba319b5SDimitry Andric 
finalize(AsmPrinter * Asm,StringRef Prefix)554ba319b5SDimitry Andric void AccelTableBase::finalize(AsmPrinter *Asm, StringRef Prefix) {
564ba319b5SDimitry Andric   // Create the individual hash data outputs.
574ba319b5SDimitry Andric   for (auto &E : Entries) {
584ba319b5SDimitry Andric     // Unique the entries.
594ba319b5SDimitry Andric     std::stable_sort(E.second.Values.begin(), E.second.Values.end(),
604ba319b5SDimitry Andric                      [](const AccelTableData *A, const AccelTableData *B) {
614ba319b5SDimitry Andric                        return *A < *B;
624ba319b5SDimitry Andric                      });
634ba319b5SDimitry Andric     E.second.Values.erase(
644ba319b5SDimitry Andric         std::unique(E.second.Values.begin(), E.second.Values.end()),
654ba319b5SDimitry Andric         E.second.Values.end());
664ba319b5SDimitry Andric   }
674ba319b5SDimitry Andric 
684ba319b5SDimitry Andric   // Figure out how many buckets we need, then compute the bucket contents and
694ba319b5SDimitry Andric   // the final ordering. The hashes and offsets can be emitted by walking these
704ba319b5SDimitry Andric   // data structures. We add temporary symbols to the data so they can be
714ba319b5SDimitry Andric   // referenced when emitting the offsets.
724ba319b5SDimitry Andric   computeBucketCount();
734ba319b5SDimitry Andric 
744ba319b5SDimitry Andric   // Compute bucket contents and final ordering.
754ba319b5SDimitry Andric   Buckets.resize(BucketCount);
764ba319b5SDimitry Andric   for (auto &E : Entries) {
774ba319b5SDimitry Andric     uint32_t Bucket = E.second.HashValue % BucketCount;
784ba319b5SDimitry Andric     Buckets[Bucket].push_back(&E.second);
794ba319b5SDimitry Andric     E.second.Sym = Asm->createTempSymbol(Prefix);
804ba319b5SDimitry Andric   }
814ba319b5SDimitry Andric 
824ba319b5SDimitry Andric   // Sort the contents of the buckets by hash value so that hash collisions end
834ba319b5SDimitry Andric   // up together. Stable sort makes testing easier and doesn't cost much more.
844ba319b5SDimitry Andric   for (auto &Bucket : Buckets)
854ba319b5SDimitry Andric     std::stable_sort(Bucket.begin(), Bucket.end(),
864ba319b5SDimitry Andric                      [](HashData *LHS, HashData *RHS) {
874ba319b5SDimitry Andric                        return LHS->HashValue < RHS->HashValue;
884ba319b5SDimitry Andric                      });
894ba319b5SDimitry Andric }
904ba319b5SDimitry Andric 
914ba319b5SDimitry Andric namespace {
924ba319b5SDimitry Andric /// Base class for writing out Accelerator tables. It holds the common
934ba319b5SDimitry Andric /// functionality for the two Accelerator table types.
944ba319b5SDimitry Andric class AccelTableWriter {
954ba319b5SDimitry Andric protected:
964ba319b5SDimitry Andric   AsmPrinter *const Asm;          ///< Destination.
974ba319b5SDimitry Andric   const AccelTableBase &Contents; ///< Data to emit.
984ba319b5SDimitry Andric 
994ba319b5SDimitry Andric   /// Controls whether to emit duplicate hash and offset table entries for names
1004ba319b5SDimitry Andric   /// with identical hashes. Apple tables don't emit duplicate entries, DWARF v5
1014ba319b5SDimitry Andric   /// tables do.
1024ba319b5SDimitry Andric   const bool SkipIdenticalHashes;
1034ba319b5SDimitry Andric 
1044ba319b5SDimitry Andric   void emitHashes() const;
1054ba319b5SDimitry Andric 
1064ba319b5SDimitry Andric   /// Emit offsets to lists of entries with identical names. The offsets are
1074ba319b5SDimitry Andric   /// relative to the Base argument.
1084ba319b5SDimitry Andric   void emitOffsets(const MCSymbol *Base) const;
1094ba319b5SDimitry Andric 
1104ba319b5SDimitry Andric public:
AccelTableWriter(AsmPrinter * Asm,const AccelTableBase & Contents,bool SkipIdenticalHashes)1114ba319b5SDimitry Andric   AccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents,
1124ba319b5SDimitry Andric                    bool SkipIdenticalHashes)
1134ba319b5SDimitry Andric       : Asm(Asm), Contents(Contents), SkipIdenticalHashes(SkipIdenticalHashes) {
1144ba319b5SDimitry Andric   }
1154ba319b5SDimitry Andric };
1164ba319b5SDimitry Andric 
1174ba319b5SDimitry Andric class AppleAccelTableWriter : public AccelTableWriter {
1184ba319b5SDimitry Andric   using Atom = AppleAccelTableData::Atom;
1194ba319b5SDimitry Andric 
1204ba319b5SDimitry Andric   /// The fixed header of an Apple Accelerator Table.
1214ba319b5SDimitry Andric   struct Header {
1224ba319b5SDimitry Andric     uint32_t Magic = MagicHash;
1234ba319b5SDimitry Andric     uint16_t Version = 1;
1244ba319b5SDimitry Andric     uint16_t HashFunction = dwarf::DW_hash_function_djb;
1254ba319b5SDimitry Andric     uint32_t BucketCount;
1264ba319b5SDimitry Andric     uint32_t HashCount;
1274ba319b5SDimitry Andric     uint32_t HeaderDataLength;
1284ba319b5SDimitry Andric 
1294ba319b5SDimitry Andric     /// 'HASH' magic value to detect endianness.
1304ba319b5SDimitry Andric     static const uint32_t MagicHash = 0x48415348;
1314ba319b5SDimitry Andric 
Header__anon1b4e4a6d0311::AppleAccelTableWriter::Header1324ba319b5SDimitry Andric     Header(uint32_t BucketCount, uint32_t UniqueHashCount, uint32_t DataLength)
1334ba319b5SDimitry Andric         : BucketCount(BucketCount), HashCount(UniqueHashCount),
1344ba319b5SDimitry Andric           HeaderDataLength(DataLength) {}
1354ba319b5SDimitry Andric 
1364ba319b5SDimitry Andric     void emit(AsmPrinter *Asm) const;
1374ba319b5SDimitry Andric #ifndef NDEBUG
1384ba319b5SDimitry Andric     void print(raw_ostream &OS) const;
dump__anon1b4e4a6d0311::AppleAccelTableWriter::Header1394ba319b5SDimitry Andric     void dump() const { print(dbgs()); }
1404ba319b5SDimitry Andric #endif
1414ba319b5SDimitry Andric   };
1424ba319b5SDimitry Andric 
1434ba319b5SDimitry Andric   /// The HeaderData describes the structure of an Apple accelerator table
1444ba319b5SDimitry Andric   /// through a list of Atoms.
1454ba319b5SDimitry Andric   struct HeaderData {
1464ba319b5SDimitry Andric     /// In the case of data that is referenced via DW_FORM_ref_* the offset
1474ba319b5SDimitry Andric     /// base is used to describe the offset for all forms in the list of atoms.
1484ba319b5SDimitry Andric     uint32_t DieOffsetBase;
1494ba319b5SDimitry Andric 
1504ba319b5SDimitry Andric     const SmallVector<Atom, 4> Atoms;
1514ba319b5SDimitry Andric 
HeaderData__anon1b4e4a6d0311::AppleAccelTableWriter::HeaderData1524ba319b5SDimitry Andric     HeaderData(ArrayRef<Atom> AtomList, uint32_t Offset = 0)
1534ba319b5SDimitry Andric         : DieOffsetBase(Offset), Atoms(AtomList.begin(), AtomList.end()) {}
1544ba319b5SDimitry Andric 
1554ba319b5SDimitry Andric     void emit(AsmPrinter *Asm) const;
1564ba319b5SDimitry Andric #ifndef NDEBUG
1574ba319b5SDimitry Andric     void print(raw_ostream &OS) const;
dump__anon1b4e4a6d0311::AppleAccelTableWriter::HeaderData1584ba319b5SDimitry Andric     void dump() const { print(dbgs()); }
1594ba319b5SDimitry Andric #endif
1604ba319b5SDimitry Andric   };
1614ba319b5SDimitry Andric 
1624ba319b5SDimitry Andric   Header Header;
1634ba319b5SDimitry Andric   HeaderData HeaderData;
1644ba319b5SDimitry Andric   const MCSymbol *SecBegin;
1654ba319b5SDimitry Andric 
1664ba319b5SDimitry Andric   void emitBuckets() const;
1674ba319b5SDimitry Andric   void emitData() const;
1684ba319b5SDimitry Andric 
1694ba319b5SDimitry Andric public:
AppleAccelTableWriter(AsmPrinter * Asm,const AccelTableBase & Contents,ArrayRef<Atom> Atoms,const MCSymbol * SecBegin)1704ba319b5SDimitry Andric   AppleAccelTableWriter(AsmPrinter *Asm, const AccelTableBase &Contents,
1714ba319b5SDimitry Andric                         ArrayRef<Atom> Atoms, const MCSymbol *SecBegin)
1724ba319b5SDimitry Andric       : AccelTableWriter(Asm, Contents, true),
1734ba319b5SDimitry Andric         Header(Contents.getBucketCount(), Contents.getUniqueHashCount(),
1744ba319b5SDimitry Andric                8 + (Atoms.size() * 4)),
1754ba319b5SDimitry Andric         HeaderData(Atoms), SecBegin(SecBegin) {}
1764ba319b5SDimitry Andric 
1774ba319b5SDimitry Andric   void emit() const;
1784ba319b5SDimitry Andric 
1794ba319b5SDimitry Andric #ifndef NDEBUG
1804ba319b5SDimitry Andric   void print(raw_ostream &OS) const;
dump() const1814ba319b5SDimitry Andric   void dump() const { print(dbgs()); }
1824ba319b5SDimitry Andric #endif
1834ba319b5SDimitry Andric };
1844ba319b5SDimitry Andric 
1854ba319b5SDimitry Andric /// Class responsible for emitting a DWARF v5 Accelerator Table. The only
1864ba319b5SDimitry Andric /// public function is emit(), which performs the actual emission.
1874ba319b5SDimitry Andric ///
1884ba319b5SDimitry Andric /// The class is templated in its data type. This allows us to emit both dyamic
1894ba319b5SDimitry Andric /// and static data entries. A callback abstract the logic to provide a CU
1904ba319b5SDimitry Andric /// index for a given entry, which is different per data type, but identical
1914ba319b5SDimitry Andric /// for every entry in the same table.
1924ba319b5SDimitry Andric template <typename DataT>
1934ba319b5SDimitry Andric class Dwarf5AccelTableWriter : public AccelTableWriter {
1944ba319b5SDimitry Andric   struct Header {
1954ba319b5SDimitry Andric     uint32_t UnitLength = 0;
1964ba319b5SDimitry Andric     uint16_t Version = 5;
1974ba319b5SDimitry Andric     uint16_t Padding = 0;
1984ba319b5SDimitry Andric     uint32_t CompUnitCount;
1994ba319b5SDimitry Andric     uint32_t LocalTypeUnitCount = 0;
2004ba319b5SDimitry Andric     uint32_t ForeignTypeUnitCount = 0;
2014ba319b5SDimitry Andric     uint32_t BucketCount;
2024ba319b5SDimitry Andric     uint32_t NameCount;
2034ba319b5SDimitry Andric     uint32_t AbbrevTableSize = 0;
2044ba319b5SDimitry Andric     uint32_t AugmentationStringSize = sizeof(AugmentationString);
2054ba319b5SDimitry Andric     char AugmentationString[8] = {'L', 'L', 'V', 'M', '0', '7', '0', '0'};
2064ba319b5SDimitry Andric 
Header__anon1b4e4a6d0311::Dwarf5AccelTableWriter::Header2074ba319b5SDimitry Andric     Header(uint32_t CompUnitCount, uint32_t BucketCount, uint32_t NameCount)
2084ba319b5SDimitry Andric         : CompUnitCount(CompUnitCount), BucketCount(BucketCount),
2094ba319b5SDimitry Andric           NameCount(NameCount) {}
2104ba319b5SDimitry Andric 
2114ba319b5SDimitry Andric     void emit(const Dwarf5AccelTableWriter &Ctx) const;
2124ba319b5SDimitry Andric   };
2134ba319b5SDimitry Andric   struct AttributeEncoding {
2144ba319b5SDimitry Andric     dwarf::Index Index;
2154ba319b5SDimitry Andric     dwarf::Form Form;
2164ba319b5SDimitry Andric   };
2174ba319b5SDimitry Andric 
2184ba319b5SDimitry Andric   Header Header;
2194ba319b5SDimitry Andric   DenseMap<uint32_t, SmallVector<AttributeEncoding, 2>> Abbreviations;
2204ba319b5SDimitry Andric   ArrayRef<MCSymbol *> CompUnits;
2214ba319b5SDimitry Andric   llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry;
2224ba319b5SDimitry Andric   MCSymbol *ContributionStart = Asm->createTempSymbol("names_start");
2234ba319b5SDimitry Andric   MCSymbol *ContributionEnd = Asm->createTempSymbol("names_end");
2244ba319b5SDimitry Andric   MCSymbol *AbbrevStart = Asm->createTempSymbol("names_abbrev_start");
2254ba319b5SDimitry Andric   MCSymbol *AbbrevEnd = Asm->createTempSymbol("names_abbrev_end");
2264ba319b5SDimitry Andric   MCSymbol *EntryPool = Asm->createTempSymbol("names_entries");
2274ba319b5SDimitry Andric 
2284ba319b5SDimitry Andric   DenseSet<uint32_t> getUniqueTags() const;
2294ba319b5SDimitry Andric 
2304ba319b5SDimitry Andric   // Right now, we emit uniform attributes for all tags.
2314ba319b5SDimitry Andric   SmallVector<AttributeEncoding, 2> getUniformAttributes() const;
2324ba319b5SDimitry Andric 
2334ba319b5SDimitry Andric   void emitCUList() const;
2344ba319b5SDimitry Andric   void emitBuckets() const;
2354ba319b5SDimitry Andric   void emitStringOffsets() const;
2364ba319b5SDimitry Andric   void emitAbbrevs() const;
2374ba319b5SDimitry Andric   void emitEntry(const DataT &Entry) const;
2384ba319b5SDimitry Andric   void emitData() const;
2394ba319b5SDimitry Andric 
2404ba319b5SDimitry Andric public:
2414ba319b5SDimitry Andric   Dwarf5AccelTableWriter(
2424ba319b5SDimitry Andric       AsmPrinter *Asm, const AccelTableBase &Contents,
2434ba319b5SDimitry Andric       ArrayRef<MCSymbol *> CompUnits,
2444ba319b5SDimitry Andric       llvm::function_ref<unsigned(const DataT &)> GetCUIndexForEntry);
2454ba319b5SDimitry Andric 
2464ba319b5SDimitry Andric   void emit() const;
2474ba319b5SDimitry Andric };
2484ba319b5SDimitry Andric } // namespace
2494ba319b5SDimitry Andric 
emitHashes() const2504ba319b5SDimitry Andric void AccelTableWriter::emitHashes() const {
2514ba319b5SDimitry Andric   uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
2524ba319b5SDimitry Andric   unsigned BucketIdx = 0;
2534ba319b5SDimitry Andric   for (auto &Bucket : Contents.getBuckets()) {
2544ba319b5SDimitry Andric     for (auto &Hash : Bucket) {
2554ba319b5SDimitry Andric       uint32_t HashValue = Hash->HashValue;
2564ba319b5SDimitry Andric       if (SkipIdenticalHashes && PrevHash == HashValue)
2574ba319b5SDimitry Andric         continue;
2584ba319b5SDimitry Andric       Asm->OutStreamer->AddComment("Hash in Bucket " + Twine(BucketIdx));
2594ba319b5SDimitry Andric       Asm->emitInt32(HashValue);
2604ba319b5SDimitry Andric       PrevHash = HashValue;
2614ba319b5SDimitry Andric     }
2624ba319b5SDimitry Andric     BucketIdx++;
2634ba319b5SDimitry Andric   }
2644ba319b5SDimitry Andric }
2654ba319b5SDimitry Andric 
emitOffsets(const MCSymbol * Base) const2664ba319b5SDimitry Andric void AccelTableWriter::emitOffsets(const MCSymbol *Base) const {
2674ba319b5SDimitry Andric   const auto &Buckets = Contents.getBuckets();
2684ba319b5SDimitry Andric   uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
2694ba319b5SDimitry Andric   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
2704ba319b5SDimitry Andric     for (auto *Hash : Buckets[i]) {
2714ba319b5SDimitry Andric       uint32_t HashValue = Hash->HashValue;
2724ba319b5SDimitry Andric       if (SkipIdenticalHashes && PrevHash == HashValue)
2734ba319b5SDimitry Andric         continue;
2744ba319b5SDimitry Andric       PrevHash = HashValue;
2754ba319b5SDimitry Andric       Asm->OutStreamer->AddComment("Offset in Bucket " + Twine(i));
2764ba319b5SDimitry Andric       Asm->EmitLabelDifference(Hash->Sym, Base, sizeof(uint32_t));
2774ba319b5SDimitry Andric     }
2784ba319b5SDimitry Andric   }
2794ba319b5SDimitry Andric }
2804ba319b5SDimitry Andric 
emit(AsmPrinter * Asm) const2814ba319b5SDimitry Andric void AppleAccelTableWriter::Header::emit(AsmPrinter *Asm) const {
2824ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header Magic");
2834ba319b5SDimitry Andric   Asm->emitInt32(Magic);
2844ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header Version");
2854ba319b5SDimitry Andric   Asm->emitInt16(Version);
2864ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header Hash Function");
2874ba319b5SDimitry Andric   Asm->emitInt16(HashFunction);
2884ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header Bucket Count");
2894ba319b5SDimitry Andric   Asm->emitInt32(BucketCount);
2904ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header Hash Count");
2914ba319b5SDimitry Andric   Asm->emitInt32(HashCount);
2924ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header Data Length");
2934ba319b5SDimitry Andric   Asm->emitInt32(HeaderDataLength);
2944ba319b5SDimitry Andric }
2954ba319b5SDimitry Andric 
emit(AsmPrinter * Asm) const2964ba319b5SDimitry Andric void AppleAccelTableWriter::HeaderData::emit(AsmPrinter *Asm) const {
2974ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("HeaderData Die Offset Base");
2984ba319b5SDimitry Andric   Asm->emitInt32(DieOffsetBase);
2994ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("HeaderData Atom Count");
3004ba319b5SDimitry Andric   Asm->emitInt32(Atoms.size());
3014ba319b5SDimitry Andric 
3024ba319b5SDimitry Andric   for (const Atom &A : Atoms) {
3034ba319b5SDimitry Andric     Asm->OutStreamer->AddComment(dwarf::AtomTypeString(A.Type));
3044ba319b5SDimitry Andric     Asm->emitInt16(A.Type);
3054ba319b5SDimitry Andric     Asm->OutStreamer->AddComment(dwarf::FormEncodingString(A.Form));
3064ba319b5SDimitry Andric     Asm->emitInt16(A.Form);
3074ba319b5SDimitry Andric   }
3084ba319b5SDimitry Andric }
3094ba319b5SDimitry Andric 
emitBuckets() const3104ba319b5SDimitry Andric void AppleAccelTableWriter::emitBuckets() const {
3114ba319b5SDimitry Andric   const auto &Buckets = Contents.getBuckets();
3124ba319b5SDimitry Andric   unsigned index = 0;
3134ba319b5SDimitry Andric   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
3144ba319b5SDimitry Andric     Asm->OutStreamer->AddComment("Bucket " + Twine(i));
3154ba319b5SDimitry Andric     if (!Buckets[i].empty())
3164ba319b5SDimitry Andric       Asm->emitInt32(index);
3174ba319b5SDimitry Andric     else
3184ba319b5SDimitry Andric       Asm->emitInt32(std::numeric_limits<uint32_t>::max());
3194ba319b5SDimitry Andric     // Buckets point in the list of hashes, not to the data. Do not increment
3204ba319b5SDimitry Andric     // the index multiple times in case of hash collisions.
3214ba319b5SDimitry Andric     uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
3224ba319b5SDimitry Andric     for (auto *HD : Buckets[i]) {
3234ba319b5SDimitry Andric       uint32_t HashValue = HD->HashValue;
3244ba319b5SDimitry Andric       if (PrevHash != HashValue)
3254ba319b5SDimitry Andric         ++index;
3264ba319b5SDimitry Andric       PrevHash = HashValue;
3274ba319b5SDimitry Andric     }
3284ba319b5SDimitry Andric   }
3294ba319b5SDimitry Andric }
3304ba319b5SDimitry Andric 
emitData() const3314ba319b5SDimitry Andric void AppleAccelTableWriter::emitData() const {
3324ba319b5SDimitry Andric   const auto &Buckets = Contents.getBuckets();
3334ba319b5SDimitry Andric   for (size_t i = 0, e = Buckets.size(); i < e; ++i) {
3344ba319b5SDimitry Andric     uint64_t PrevHash = std::numeric_limits<uint64_t>::max();
3354ba319b5SDimitry Andric     for (auto &Hash : Buckets[i]) {
3364ba319b5SDimitry Andric       // Terminate the previous entry if there is no hash collision with the
3374ba319b5SDimitry Andric       // current one.
3384ba319b5SDimitry Andric       if (PrevHash != std::numeric_limits<uint64_t>::max() &&
3394ba319b5SDimitry Andric           PrevHash != Hash->HashValue)
3404ba319b5SDimitry Andric         Asm->emitInt32(0);
3414ba319b5SDimitry Andric       // Remember to emit the label for our offset.
3424ba319b5SDimitry Andric       Asm->OutStreamer->EmitLabel(Hash->Sym);
3434ba319b5SDimitry Andric       Asm->OutStreamer->AddComment(Hash->Name.getString());
3444ba319b5SDimitry Andric       Asm->emitDwarfStringOffset(Hash->Name);
3454ba319b5SDimitry Andric       Asm->OutStreamer->AddComment("Num DIEs");
3464ba319b5SDimitry Andric       Asm->emitInt32(Hash->Values.size());
3474ba319b5SDimitry Andric       for (const auto *V : Hash->Values)
3484ba319b5SDimitry Andric         static_cast<const AppleAccelTableData *>(V)->emit(Asm);
3494ba319b5SDimitry Andric       PrevHash = Hash->HashValue;
3504ba319b5SDimitry Andric     }
3514ba319b5SDimitry Andric     // Emit the final end marker for the bucket.
3524ba319b5SDimitry Andric     if (!Buckets[i].empty())
3534ba319b5SDimitry Andric       Asm->emitInt32(0);
3544ba319b5SDimitry Andric   }
3554ba319b5SDimitry Andric }
3564ba319b5SDimitry Andric 
emit() const3574ba319b5SDimitry Andric void AppleAccelTableWriter::emit() const {
3584ba319b5SDimitry Andric   Header.emit(Asm);
3594ba319b5SDimitry Andric   HeaderData.emit(Asm);
3604ba319b5SDimitry Andric   emitBuckets();
3614ba319b5SDimitry Andric   emitHashes();
3624ba319b5SDimitry Andric   emitOffsets(SecBegin);
3634ba319b5SDimitry Andric   emitData();
3644ba319b5SDimitry Andric }
3654ba319b5SDimitry Andric 
3664ba319b5SDimitry Andric template <typename DataT>
emit(const Dwarf5AccelTableWriter & Ctx) const3674ba319b5SDimitry Andric void Dwarf5AccelTableWriter<DataT>::Header::emit(
3684ba319b5SDimitry Andric     const Dwarf5AccelTableWriter &Ctx) const {
3694ba319b5SDimitry Andric   assert(CompUnitCount > 0 && "Index must have at least one CU.");
3704ba319b5SDimitry Andric 
3714ba319b5SDimitry Andric   AsmPrinter *Asm = Ctx.Asm;
3724ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: unit length");
3734ba319b5SDimitry Andric   Asm->EmitLabelDifference(Ctx.ContributionEnd, Ctx.ContributionStart,
3744ba319b5SDimitry Andric                            sizeof(uint32_t));
3754ba319b5SDimitry Andric   Asm->OutStreamer->EmitLabel(Ctx.ContributionStart);
3764ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: version");
3774ba319b5SDimitry Andric   Asm->emitInt16(Version);
3784ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: padding");
3794ba319b5SDimitry Andric   Asm->emitInt16(Padding);
3804ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: compilation unit count");
3814ba319b5SDimitry Andric   Asm->emitInt32(CompUnitCount);
3824ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: local type unit count");
3834ba319b5SDimitry Andric   Asm->emitInt32(LocalTypeUnitCount);
3844ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: foreign type unit count");
3854ba319b5SDimitry Andric   Asm->emitInt32(ForeignTypeUnitCount);
3864ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: bucket count");
3874ba319b5SDimitry Andric   Asm->emitInt32(BucketCount);
3884ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: name count");
3894ba319b5SDimitry Andric   Asm->emitInt32(NameCount);
3904ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: abbreviation table size");
3914ba319b5SDimitry Andric   Asm->EmitLabelDifference(Ctx.AbbrevEnd, Ctx.AbbrevStart, sizeof(uint32_t));
3924ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: augmentation string size");
3934ba319b5SDimitry Andric   assert(AugmentationStringSize % 4 == 0);
3944ba319b5SDimitry Andric   Asm->emitInt32(AugmentationStringSize);
3954ba319b5SDimitry Andric   Asm->OutStreamer->AddComment("Header: augmentation string");
3964ba319b5SDimitry Andric   Asm->OutStreamer->EmitBytes({AugmentationString, AugmentationStringSize});
3974ba319b5SDimitry Andric }
3984ba319b5SDimitry Andric 
3994ba319b5SDimitry Andric template <typename DataT>
getUniqueTags() const4004ba319b5SDimitry Andric DenseSet<uint32_t> Dwarf5AccelTableWriter<DataT>::getUniqueTags() const {
4014ba319b5SDimitry Andric   DenseSet<uint32_t> UniqueTags;
4024ba319b5SDimitry Andric   for (auto &Bucket : Contents.getBuckets()) {
4034ba319b5SDimitry Andric     for (auto *Hash : Bucket) {
4044ba319b5SDimitry Andric       for (auto *Value : Hash->Values) {
4054ba319b5SDimitry Andric         unsigned Tag = static_cast<const DataT *>(Value)->getDieTag();
4064ba319b5SDimitry Andric         UniqueTags.insert(Tag);
4074ba319b5SDimitry Andric       }
4084ba319b5SDimitry Andric     }
4094ba319b5SDimitry Andric   }
4104ba319b5SDimitry Andric   return UniqueTags;
4114ba319b5SDimitry Andric }
4124ba319b5SDimitry Andric 
4134ba319b5SDimitry Andric template <typename DataT>
4144ba319b5SDimitry Andric SmallVector<typename Dwarf5AccelTableWriter<DataT>::AttributeEncoding, 2>
getUniformAttributes() const4154ba319b5SDimitry Andric Dwarf5AccelTableWriter<DataT>::getUniformAttributes() const {
4164ba319b5SDimitry Andric   SmallVector<AttributeEncoding, 2> UA;
4174ba319b5SDimitry Andric   if (CompUnits.size() > 1) {
4184ba319b5SDimitry Andric     size_t LargestCUIndex = CompUnits.size() - 1;
4194ba319b5SDimitry Andric     dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, LargestCUIndex);
4204ba319b5SDimitry Andric     UA.push_back({dwarf::DW_IDX_compile_unit, Form});
4214ba319b5SDimitry Andric   }
4224ba319b5SDimitry Andric   UA.push_back({dwarf::DW_IDX_die_offset, dwarf::DW_FORM_ref4});
4234ba319b5SDimitry Andric   return UA;
4244ba319b5SDimitry Andric }
4254ba319b5SDimitry Andric 
4264ba319b5SDimitry Andric template <typename DataT>
emitCUList() const4274ba319b5SDimitry Andric void Dwarf5AccelTableWriter<DataT>::emitCUList() const {
4284ba319b5SDimitry Andric   for (const auto &CU : enumerate(CompUnits)) {
4294ba319b5SDimitry Andric     Asm->OutStreamer->AddComment("Compilation unit " + Twine(CU.index()));
4304ba319b5SDimitry Andric     Asm->emitDwarfSymbolReference(CU.value());
4314ba319b5SDimitry Andric   }
4324ba319b5SDimitry Andric }
4334ba319b5SDimitry Andric 
4344ba319b5SDimitry Andric template <typename DataT>
emitBuckets() const4354ba319b5SDimitry Andric void Dwarf5AccelTableWriter<DataT>::emitBuckets() const {
4364ba319b5SDimitry Andric   uint32_t Index = 1;
4374ba319b5SDimitry Andric   for (const auto &Bucket : enumerate(Contents.getBuckets())) {
4384ba319b5SDimitry Andric     Asm->OutStreamer->AddComment("Bucket " + Twine(Bucket.index()));
4394ba319b5SDimitry Andric     Asm->emitInt32(Bucket.value().empty() ? 0 : Index);
4404ba319b5SDimitry Andric     Index += Bucket.value().size();
4414ba319b5SDimitry Andric   }
4424ba319b5SDimitry Andric }
4434ba319b5SDimitry Andric 
4444ba319b5SDimitry Andric template <typename DataT>
emitStringOffsets() const4454ba319b5SDimitry Andric void Dwarf5AccelTableWriter<DataT>::emitStringOffsets() const {
4464ba319b5SDimitry Andric   for (const auto &Bucket : enumerate(Contents.getBuckets())) {
4474ba319b5SDimitry Andric     for (auto *Hash : Bucket.value()) {
4484ba319b5SDimitry Andric       DwarfStringPoolEntryRef String = Hash->Name;
4494ba319b5SDimitry Andric       Asm->OutStreamer->AddComment("String in Bucket " + Twine(Bucket.index()) +
4504ba319b5SDimitry Andric                                    ": " + String.getString());
4514ba319b5SDimitry Andric       Asm->emitDwarfStringOffset(String);
4524ba319b5SDimitry Andric     }
4534ba319b5SDimitry Andric   }
4544ba319b5SDimitry Andric }
4554ba319b5SDimitry Andric 
4564ba319b5SDimitry Andric template <typename DataT>
emitAbbrevs() const4574ba319b5SDimitry Andric void Dwarf5AccelTableWriter<DataT>::emitAbbrevs() const {
4584ba319b5SDimitry Andric   Asm->OutStreamer->EmitLabel(AbbrevStart);
4594ba319b5SDimitry Andric   for (const auto &Abbrev : Abbreviations) {
4604ba319b5SDimitry Andric     Asm->OutStreamer->AddComment("Abbrev code");
4614ba319b5SDimitry Andric     assert(Abbrev.first != 0);
4624ba319b5SDimitry Andric     Asm->EmitULEB128(Abbrev.first);
4634ba319b5SDimitry Andric     Asm->OutStreamer->AddComment(dwarf::TagString(Abbrev.first));
4644ba319b5SDimitry Andric     Asm->EmitULEB128(Abbrev.first);
4654ba319b5SDimitry Andric     for (const auto &AttrEnc : Abbrev.second) {
4664ba319b5SDimitry Andric       Asm->EmitULEB128(AttrEnc.Index, dwarf::IndexString(AttrEnc.Index).data());
4674ba319b5SDimitry Andric       Asm->EmitULEB128(AttrEnc.Form,
4684ba319b5SDimitry Andric                        dwarf::FormEncodingString(AttrEnc.Form).data());
4694ba319b5SDimitry Andric     }
4704ba319b5SDimitry Andric     Asm->EmitULEB128(0, "End of abbrev");
4714ba319b5SDimitry Andric     Asm->EmitULEB128(0, "End of abbrev");
4724ba319b5SDimitry Andric   }
4734ba319b5SDimitry Andric   Asm->EmitULEB128(0, "End of abbrev list");
4744ba319b5SDimitry Andric   Asm->OutStreamer->EmitLabel(AbbrevEnd);
4754ba319b5SDimitry Andric }
4764ba319b5SDimitry Andric 
4774ba319b5SDimitry Andric template <typename DataT>
emitEntry(const DataT & Entry) const4784ba319b5SDimitry Andric void Dwarf5AccelTableWriter<DataT>::emitEntry(const DataT &Entry) const {
4794ba319b5SDimitry Andric   auto AbbrevIt = Abbreviations.find(Entry.getDieTag());
4804ba319b5SDimitry Andric   assert(AbbrevIt != Abbreviations.end() &&
4814ba319b5SDimitry Andric          "Why wasn't this abbrev generated?");
4824ba319b5SDimitry Andric 
4834ba319b5SDimitry Andric   Asm->EmitULEB128(AbbrevIt->first, "Abbreviation code");
4844ba319b5SDimitry Andric   for (const auto &AttrEnc : AbbrevIt->second) {
4854ba319b5SDimitry Andric     Asm->OutStreamer->AddComment(dwarf::IndexString(AttrEnc.Index));
4864ba319b5SDimitry Andric     switch (AttrEnc.Index) {
4874ba319b5SDimitry Andric     case dwarf::DW_IDX_compile_unit: {
4884ba319b5SDimitry Andric       DIEInteger ID(getCUIndexForEntry(Entry));
4894ba319b5SDimitry Andric       ID.EmitValue(Asm, AttrEnc.Form);
4904ba319b5SDimitry Andric       break;
4914ba319b5SDimitry Andric     }
4924ba319b5SDimitry Andric     case dwarf::DW_IDX_die_offset:
4934ba319b5SDimitry Andric       assert(AttrEnc.Form == dwarf::DW_FORM_ref4);
4944ba319b5SDimitry Andric       Asm->emitInt32(Entry.getDieOffset());
4954ba319b5SDimitry Andric       break;
4964ba319b5SDimitry Andric     default:
4974ba319b5SDimitry Andric       llvm_unreachable("Unexpected index attribute!");
4984ba319b5SDimitry Andric     }
4994ba319b5SDimitry Andric   }
5004ba319b5SDimitry Andric }
5014ba319b5SDimitry Andric 
emitData() const5024ba319b5SDimitry Andric template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emitData() const {
5034ba319b5SDimitry Andric   Asm->OutStreamer->EmitLabel(EntryPool);
5044ba319b5SDimitry Andric   for (auto &Bucket : Contents.getBuckets()) {
5054ba319b5SDimitry Andric     for (auto *Hash : Bucket) {
5064ba319b5SDimitry Andric       // Remember to emit the label for our offset.
5074ba319b5SDimitry Andric       Asm->OutStreamer->EmitLabel(Hash->Sym);
5084ba319b5SDimitry Andric       for (const auto *Value : Hash->Values)
5094ba319b5SDimitry Andric         emitEntry(*static_cast<const DataT *>(Value));
5104ba319b5SDimitry Andric       Asm->OutStreamer->AddComment("End of list: " + Hash->Name.getString());
5114ba319b5SDimitry Andric       Asm->emitInt32(0);
5124ba319b5SDimitry Andric     }
5134ba319b5SDimitry Andric   }
5144ba319b5SDimitry Andric }
5154ba319b5SDimitry Andric 
5164ba319b5SDimitry Andric template <typename DataT>
Dwarf5AccelTableWriter(AsmPrinter * Asm,const AccelTableBase & Contents,ArrayRef<MCSymbol * > CompUnits,llvm::function_ref<unsigned (const DataT &)> getCUIndexForEntry)5174ba319b5SDimitry Andric Dwarf5AccelTableWriter<DataT>::Dwarf5AccelTableWriter(
5184ba319b5SDimitry Andric     AsmPrinter *Asm, const AccelTableBase &Contents,
5194ba319b5SDimitry Andric     ArrayRef<MCSymbol *> CompUnits,
5204ba319b5SDimitry Andric     llvm::function_ref<unsigned(const DataT &)> getCUIndexForEntry)
5214ba319b5SDimitry Andric     : AccelTableWriter(Asm, Contents, false),
5224ba319b5SDimitry Andric       Header(CompUnits.size(), Contents.getBucketCount(),
5234ba319b5SDimitry Andric              Contents.getUniqueNameCount()),
5244ba319b5SDimitry Andric       CompUnits(CompUnits), getCUIndexForEntry(std::move(getCUIndexForEntry)) {
5254ba319b5SDimitry Andric   DenseSet<uint32_t> UniqueTags = getUniqueTags();
5264ba319b5SDimitry Andric   SmallVector<AttributeEncoding, 2> UniformAttributes = getUniformAttributes();
5274ba319b5SDimitry Andric 
5284ba319b5SDimitry Andric   Abbreviations.reserve(UniqueTags.size());
5294ba319b5SDimitry Andric   for (uint32_t Tag : UniqueTags)
5304ba319b5SDimitry Andric     Abbreviations.try_emplace(Tag, UniformAttributes);
5314ba319b5SDimitry Andric }
5324ba319b5SDimitry Andric 
emit() const5334ba319b5SDimitry Andric template <typename DataT> void Dwarf5AccelTableWriter<DataT>::emit() const {
5344ba319b5SDimitry Andric   Header.emit(*this);
5354ba319b5SDimitry Andric   emitCUList();
5364ba319b5SDimitry Andric   emitBuckets();
5374ba319b5SDimitry Andric   emitHashes();
5384ba319b5SDimitry Andric   emitStringOffsets();
5394ba319b5SDimitry Andric   emitOffsets(EntryPool);
5404ba319b5SDimitry Andric   emitAbbrevs();
5414ba319b5SDimitry Andric   emitData();
5424ba319b5SDimitry Andric   Asm->OutStreamer->EmitValueToAlignment(4, 0);
5434ba319b5SDimitry Andric   Asm->OutStreamer->EmitLabel(ContributionEnd);
5444ba319b5SDimitry Andric }
5454ba319b5SDimitry Andric 
emitAppleAccelTableImpl(AsmPrinter * Asm,AccelTableBase & Contents,StringRef Prefix,const MCSymbol * SecBegin,ArrayRef<AppleAccelTableData::Atom> Atoms)5464ba319b5SDimitry Andric void llvm::emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
5474ba319b5SDimitry Andric                                    StringRef Prefix, const MCSymbol *SecBegin,
5484ba319b5SDimitry Andric                                    ArrayRef<AppleAccelTableData::Atom> Atoms) {
5494ba319b5SDimitry Andric   Contents.finalize(Asm, Prefix);
5504ba319b5SDimitry Andric   AppleAccelTableWriter(Asm, Contents, Atoms, SecBegin).emit();
5514ba319b5SDimitry Andric }
5524ba319b5SDimitry Andric 
emitDWARF5AccelTable(AsmPrinter * Asm,AccelTable<DWARF5AccelTableData> & Contents,const DwarfDebug & DD,ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs)5534ba319b5SDimitry Andric void llvm::emitDWARF5AccelTable(
5544ba319b5SDimitry Andric     AsmPrinter *Asm, AccelTable<DWARF5AccelTableData> &Contents,
5554ba319b5SDimitry Andric     const DwarfDebug &DD, ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs) {
5564ba319b5SDimitry Andric   std::vector<MCSymbol *> CompUnits;
557*b5893f02SDimitry Andric   SmallVector<unsigned, 1> CUIndex(CUs.size());
558*b5893f02SDimitry Andric   int Count = 0;
5594ba319b5SDimitry Andric   for (const auto &CU : enumerate(CUs)) {
560*b5893f02SDimitry Andric     if (CU.value()->getCUNode()->getNameTableKind() ==
561*b5893f02SDimitry Andric         DICompileUnit::DebugNameTableKind::None)
562*b5893f02SDimitry Andric       continue;
563*b5893f02SDimitry Andric     CUIndex[CU.index()] = Count++;
5644ba319b5SDimitry Andric     assert(CU.index() == CU.value()->getUniqueID());
5654ba319b5SDimitry Andric     const DwarfCompileUnit *MainCU =
5664ba319b5SDimitry Andric         DD.useSplitDwarf() ? CU.value()->getSkeleton() : CU.value().get();
5674ba319b5SDimitry Andric     CompUnits.push_back(MainCU->getLabelBegin());
5684ba319b5SDimitry Andric   }
5694ba319b5SDimitry Andric 
570*b5893f02SDimitry Andric   if (CompUnits.empty())
571*b5893f02SDimitry Andric     return;
572*b5893f02SDimitry Andric 
573*b5893f02SDimitry Andric   Asm->OutStreamer->SwitchSection(
574*b5893f02SDimitry Andric       Asm->getObjFileLowering().getDwarfDebugNamesSection());
575*b5893f02SDimitry Andric 
5764ba319b5SDimitry Andric   Contents.finalize(Asm, "names");
5774ba319b5SDimitry Andric   Dwarf5AccelTableWriter<DWARF5AccelTableData>(
5784ba319b5SDimitry Andric       Asm, Contents, CompUnits,
579*b5893f02SDimitry Andric       [&](const DWARF5AccelTableData &Entry) {
5804ba319b5SDimitry Andric         const DIE *CUDie = Entry.getDie().getUnitDie();
581*b5893f02SDimitry Andric         return CUIndex[DD.lookupCU(CUDie)->getUniqueID()];
5824ba319b5SDimitry Andric       })
5834ba319b5SDimitry Andric       .emit();
5844ba319b5SDimitry Andric }
5854ba319b5SDimitry Andric 
emitDWARF5AccelTable(AsmPrinter * Asm,AccelTable<DWARF5AccelTableStaticData> & Contents,ArrayRef<MCSymbol * > CUs,llvm::function_ref<unsigned (const DWARF5AccelTableStaticData &)> getCUIndexForEntry)5864ba319b5SDimitry Andric void llvm::emitDWARF5AccelTable(
5874ba319b5SDimitry Andric     AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
5884ba319b5SDimitry Andric     ArrayRef<MCSymbol *> CUs,
5894ba319b5SDimitry Andric     llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
5904ba319b5SDimitry Andric         getCUIndexForEntry) {
5914ba319b5SDimitry Andric   Contents.finalize(Asm, "names");
5924ba319b5SDimitry Andric   Dwarf5AccelTableWriter<DWARF5AccelTableStaticData>(Asm, Contents, CUs,
5934ba319b5SDimitry Andric                                                      getCUIndexForEntry)
5944ba319b5SDimitry Andric       .emit();
5954ba319b5SDimitry Andric }
5964ba319b5SDimitry Andric 
emit(AsmPrinter * Asm) const5974ba319b5SDimitry Andric void AppleAccelTableOffsetData::emit(AsmPrinter *Asm) const {
5984ba319b5SDimitry Andric   Asm->emitInt32(Die.getDebugSectionOffset());
5994ba319b5SDimitry Andric }
6004ba319b5SDimitry Andric 
emit(AsmPrinter * Asm) const6014ba319b5SDimitry Andric void AppleAccelTableTypeData::emit(AsmPrinter *Asm) const {
6024ba319b5SDimitry Andric   Asm->emitInt32(Die.getDebugSectionOffset());
6034ba319b5SDimitry Andric   Asm->emitInt16(Die.getTag());
6044ba319b5SDimitry Andric   Asm->emitInt8(0);
6054ba319b5SDimitry Andric }
6064ba319b5SDimitry Andric 
emit(AsmPrinter * Asm) const6074ba319b5SDimitry Andric void AppleAccelTableStaticOffsetData::emit(AsmPrinter *Asm) const {
6084ba319b5SDimitry Andric   Asm->emitInt32(Offset);
6094ba319b5SDimitry Andric }
6104ba319b5SDimitry Andric 
emit(AsmPrinter * Asm) const6114ba319b5SDimitry Andric void AppleAccelTableStaticTypeData::emit(AsmPrinter *Asm) const {
6124ba319b5SDimitry Andric   Asm->emitInt32(Offset);
6134ba319b5SDimitry Andric   Asm->emitInt16(Tag);
6144ba319b5SDimitry Andric   Asm->emitInt8(ObjCClassIsImplementation ? dwarf::DW_FLAG_type_implementation
6154ba319b5SDimitry Andric                                           : 0);
6164ba319b5SDimitry Andric   Asm->emitInt32(QualifiedNameHash);
6174ba319b5SDimitry Andric }
6184ba319b5SDimitry Andric 
6194ba319b5SDimitry Andric #ifndef _MSC_VER
6204ba319b5SDimitry Andric // The lines below are rejected by older versions (TBD) of MSVC.
6214ba319b5SDimitry Andric constexpr AppleAccelTableData::Atom AppleAccelTableTypeData::Atoms[];
6224ba319b5SDimitry Andric constexpr AppleAccelTableData::Atom AppleAccelTableOffsetData::Atoms[];
6234ba319b5SDimitry Andric constexpr AppleAccelTableData::Atom AppleAccelTableStaticOffsetData::Atoms[];
6244ba319b5SDimitry Andric constexpr AppleAccelTableData::Atom AppleAccelTableStaticTypeData::Atoms[];
6254ba319b5SDimitry Andric #else
6264ba319b5SDimitry Andric // FIXME: Erase this path once the minimum MSCV version has been bumped.
6274ba319b5SDimitry Andric const SmallVector<AppleAccelTableData::Atom, 4>
6284ba319b5SDimitry Andric     AppleAccelTableOffsetData::Atoms = {
6294ba319b5SDimitry Andric         Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4)};
6304ba319b5SDimitry Andric const SmallVector<AppleAccelTableData::Atom, 4> AppleAccelTableTypeData::Atoms =
6314ba319b5SDimitry Andric     {Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
6324ba319b5SDimitry Andric      Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
6334ba319b5SDimitry Andric      Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
6344ba319b5SDimitry Andric const SmallVector<AppleAccelTableData::Atom, 4>
6354ba319b5SDimitry Andric     AppleAccelTableStaticOffsetData::Atoms = {
6364ba319b5SDimitry Andric         Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4)};
6374ba319b5SDimitry Andric const SmallVector<AppleAccelTableData::Atom, 4>
6384ba319b5SDimitry Andric     AppleAccelTableStaticTypeData::Atoms = {
6394ba319b5SDimitry Andric         Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
6404ba319b5SDimitry Andric         Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
6414ba319b5SDimitry Andric         Atom(5, dwarf::DW_FORM_data1), Atom(6, dwarf::DW_FORM_data4)};
6424ba319b5SDimitry Andric #endif
6434ba319b5SDimitry Andric 
6444ba319b5SDimitry Andric #ifndef NDEBUG
print(raw_ostream & OS) const6454ba319b5SDimitry Andric void AppleAccelTableWriter::Header::print(raw_ostream &OS) const {
6464ba319b5SDimitry Andric   OS << "Magic: " << format("0x%x", Magic) << "\n"
6474ba319b5SDimitry Andric      << "Version: " << Version << "\n"
6484ba319b5SDimitry Andric      << "Hash Function: " << HashFunction << "\n"
6494ba319b5SDimitry Andric      << "Bucket Count: " << BucketCount << "\n"
6504ba319b5SDimitry Andric      << "Header Data Length: " << HeaderDataLength << "\n";
6514ba319b5SDimitry Andric }
6524ba319b5SDimitry Andric 
print(raw_ostream & OS) const6534ba319b5SDimitry Andric void AppleAccelTableData::Atom::print(raw_ostream &OS) const {
6544ba319b5SDimitry Andric   OS << "Type: " << dwarf::AtomTypeString(Type) << "\n"
6554ba319b5SDimitry Andric      << "Form: " << dwarf::FormEncodingString(Form) << "\n";
6564ba319b5SDimitry Andric }
6574ba319b5SDimitry Andric 
print(raw_ostream & OS) const6584ba319b5SDimitry Andric void AppleAccelTableWriter::HeaderData::print(raw_ostream &OS) const {
6594ba319b5SDimitry Andric   OS << "DIE Offset Base: " << DieOffsetBase << "\n";
6604ba319b5SDimitry Andric   for (auto Atom : Atoms)
6614ba319b5SDimitry Andric     Atom.print(OS);
6624ba319b5SDimitry Andric }
6634ba319b5SDimitry Andric 
print(raw_ostream & OS) const6644ba319b5SDimitry Andric void AppleAccelTableWriter::print(raw_ostream &OS) const {
6654ba319b5SDimitry Andric   Header.print(OS);
6664ba319b5SDimitry Andric   HeaderData.print(OS);
6674ba319b5SDimitry Andric   Contents.print(OS);
6684ba319b5SDimitry Andric   SecBegin->print(OS, nullptr);
6694ba319b5SDimitry Andric }
6704ba319b5SDimitry Andric 
print(raw_ostream & OS) const6714ba319b5SDimitry Andric void AccelTableBase::HashData::print(raw_ostream &OS) const {
6724ba319b5SDimitry Andric   OS << "Name: " << Name.getString() << "\n";
6734ba319b5SDimitry Andric   OS << "  Hash Value: " << format("0x%x", HashValue) << "\n";
6744ba319b5SDimitry Andric   OS << "  Symbol: ";
6754ba319b5SDimitry Andric   if (Sym)
6764ba319b5SDimitry Andric     OS << *Sym;
6774ba319b5SDimitry Andric   else
6784ba319b5SDimitry Andric     OS << "<none>";
6794ba319b5SDimitry Andric   OS << "\n";
6804ba319b5SDimitry Andric   for (auto *Value : Values)
6814ba319b5SDimitry Andric     Value->print(OS);
6824ba319b5SDimitry Andric }
6834ba319b5SDimitry Andric 
print(raw_ostream & OS) const6844ba319b5SDimitry Andric void AccelTableBase::print(raw_ostream &OS) const {
6854ba319b5SDimitry Andric   // Print Content.
6864ba319b5SDimitry Andric   OS << "Entries: \n";
6874ba319b5SDimitry Andric   for (const auto &Entry : Entries) {
6884ba319b5SDimitry Andric     OS << "Name: " << Entry.first() << "\n";
6894ba319b5SDimitry Andric     for (auto *V : Entry.second.Values)
6904ba319b5SDimitry Andric       V->print(OS);
6914ba319b5SDimitry Andric   }
6924ba319b5SDimitry Andric 
6934ba319b5SDimitry Andric   OS << "Buckets and Hashes: \n";
6944ba319b5SDimitry Andric   for (auto &Bucket : Buckets)
6954ba319b5SDimitry Andric     for (auto &Hash : Bucket)
6964ba319b5SDimitry Andric       Hash->print(OS);
6974ba319b5SDimitry Andric 
6984ba319b5SDimitry Andric   OS << "Data: \n";
6994ba319b5SDimitry Andric   for (auto &E : Entries)
7004ba319b5SDimitry Andric     E.second.print(OS);
7014ba319b5SDimitry Andric }
7024ba319b5SDimitry Andric 
print(raw_ostream & OS) const7034ba319b5SDimitry Andric void DWARF5AccelTableData::print(raw_ostream &OS) const {
7044ba319b5SDimitry Andric   OS << "  Offset: " << getDieOffset() << "\n";
7054ba319b5SDimitry Andric   OS << "  Tag: " << dwarf::TagString(getDieTag()) << "\n";
7064ba319b5SDimitry Andric }
7074ba319b5SDimitry Andric 
print(raw_ostream & OS) const7084ba319b5SDimitry Andric void DWARF5AccelTableStaticData::print(raw_ostream &OS) const {
7094ba319b5SDimitry Andric   OS << "  Offset: " << getDieOffset() << "\n";
7104ba319b5SDimitry Andric   OS << "  Tag: " << dwarf::TagString(getDieTag()) << "\n";
7114ba319b5SDimitry Andric }
7124ba319b5SDimitry Andric 
print(raw_ostream & OS) const7134ba319b5SDimitry Andric void AppleAccelTableOffsetData::print(raw_ostream &OS) const {
7144ba319b5SDimitry Andric   OS << "  Offset: " << Die.getOffset() << "\n";
7154ba319b5SDimitry Andric }
7164ba319b5SDimitry Andric 
print(raw_ostream & OS) const7174ba319b5SDimitry Andric void AppleAccelTableTypeData::print(raw_ostream &OS) const {
7184ba319b5SDimitry Andric   OS << "  Offset: " << Die.getOffset() << "\n";
7194ba319b5SDimitry Andric   OS << "  Tag: " << dwarf::TagString(Die.getTag()) << "\n";
7204ba319b5SDimitry Andric }
7214ba319b5SDimitry Andric 
print(raw_ostream & OS) const7224ba319b5SDimitry Andric void AppleAccelTableStaticOffsetData::print(raw_ostream &OS) const {
7234ba319b5SDimitry Andric   OS << "  Static Offset: " << Offset << "\n";
7244ba319b5SDimitry Andric }
7254ba319b5SDimitry Andric 
print(raw_ostream & OS) const7264ba319b5SDimitry Andric void AppleAccelTableStaticTypeData::print(raw_ostream &OS) const {
7274ba319b5SDimitry Andric   OS << "  Static Offset: " << Offset << "\n";
7284ba319b5SDimitry Andric   OS << "  QualifiedNameHash: " << format("%x\n", QualifiedNameHash) << "\n";
7294ba319b5SDimitry Andric   OS << "  Tag: " << dwarf::TagString(Tag) << "\n";
7304ba319b5SDimitry Andric   OS << "  ObjCClassIsImplementation: "
7314ba319b5SDimitry Andric      << (ObjCClassIsImplementation ? "true" : "false");
7324ba319b5SDimitry Andric   OS << "\n";
7334ba319b5SDimitry Andric }
7344ba319b5SDimitry Andric #endif
735