SequenceToOffsetTable.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //===-- SequenceToOffsetTable.h - Compress similar sequences ----*- C++ -*-===//
  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. // SequenceToOffsetTable can be used to emit a number of null-terminated
  10. // sequences as one big array. Use the same memory when a sequence is a suffix
  11. // of another.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_UTILS_TABLEGEN_SEQUENCETOOFFSETTABLE_H
  15. #define LLVM_UTILS_TABLEGEN_SEQUENCETOOFFSETTABLE_H
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <algorithm>
  19. #include <cassert>
  20. #include <cctype>
  21. #include <functional>
  22. #include <map>
  23. namespace llvm {
  24. extern llvm::cl::opt<bool> EmitLongStrLiterals;
  25. static inline void printChar(raw_ostream &OS, char C) {
  26. unsigned char UC(C);
  27. if (isalnum(UC) || ispunct(UC)) {
  28. OS << '\'';
  29. if (C == '\\' || C == '\'')
  30. OS << '\\';
  31. OS << C << '\'';
  32. } else {
  33. OS << unsigned(UC);
  34. }
  35. }
  36. /// SequenceToOffsetTable - Collect a number of terminated sequences of T.
  37. /// Compute the layout of a table that contains all the sequences, possibly by
  38. /// reusing entries.
  39. ///
  40. /// @tparam SeqT The sequence container. (vector or string).
  41. /// @tparam Less A stable comparator for SeqT elements.
  42. template<typename SeqT, typename Less = std::less<typename SeqT::value_type> >
  43. class SequenceToOffsetTable {
  44. typedef typename SeqT::value_type ElemT;
  45. // Define a comparator for SeqT that sorts a suffix immediately before a
  46. // sequence with that suffix.
  47. struct SeqLess {
  48. Less L;
  49. bool operator()(const SeqT &A, const SeqT &B) const {
  50. return std::lexicographical_compare(A.rbegin(), A.rend(),
  51. B.rbegin(), B.rend(), L);
  52. }
  53. };
  54. // Keep sequences ordered according to SeqLess so suffixes are easy to find.
  55. // Map each sequence to its offset in the table.
  56. typedef std::map<SeqT, unsigned, SeqLess> SeqMap;
  57. // Sequences added so far, with suffixes removed.
  58. SeqMap Seqs;
  59. // Entries in the final table, or 0 before layout was called.
  60. unsigned Entries;
  61. // isSuffix - Returns true if A is a suffix of B.
  62. static bool isSuffix(const SeqT &A, const SeqT &B) {
  63. return A.size() <= B.size() && std::equal(A.rbegin(), A.rend(), B.rbegin());
  64. }
  65. public:
  66. SequenceToOffsetTable() : Entries(0) {}
  67. /// add - Add a sequence to the table.
  68. /// This must be called before layout().
  69. void add(const SeqT &Seq) {
  70. assert(Entries == 0 && "Cannot call add() after layout()");
  71. typename SeqMap::iterator I = Seqs.lower_bound(Seq);
  72. // If SeqMap contains a sequence that has Seq as a suffix, I will be
  73. // pointing to it.
  74. if (I != Seqs.end() && isSuffix(Seq, I->first))
  75. return;
  76. I = Seqs.insert(I, std::make_pair(Seq, 0u));
  77. // The entry before I may be a suffix of Seq that can now be erased.
  78. if (I != Seqs.begin() && isSuffix((--I)->first, Seq))
  79. Seqs.erase(I);
  80. }
  81. bool empty() const { return Seqs.empty(); }
  82. unsigned size() const {
  83. assert((empty() || Entries) && "Call layout() before size()");
  84. return Entries;
  85. }
  86. /// layout - Computes the final table layout.
  87. void layout() {
  88. assert(Entries == 0 && "Can only call layout() once");
  89. // Lay out the table in Seqs iteration order.
  90. for (typename SeqMap::iterator I = Seqs.begin(), E = Seqs.end(); I != E;
  91. ++I) {
  92. I->second = Entries;
  93. // Include space for a terminator.
  94. Entries += I->first.size() + 1;
  95. }
  96. }
  97. /// get - Returns the offset of Seq in the final table.
  98. unsigned get(const SeqT &Seq) const {
  99. assert(Entries && "Call layout() before get()");
  100. typename SeqMap::const_iterator I = Seqs.lower_bound(Seq);
  101. assert(I != Seqs.end() && isSuffix(Seq, I->first) &&
  102. "get() called with sequence that wasn't added first");
  103. return I->second + (I->first.size() - Seq.size());
  104. }
  105. /// `emitStringLiteralDef` - Print out the table as the body of an array
  106. /// initializer, where each element is a C string literal terminated by
  107. /// `\0`. Falls back to emitting a comma-separated integer list if
  108. /// `EmitLongStrLiterals` is false
  109. void emitStringLiteralDef(raw_ostream &OS, const llvm::Twine &Decl) const {
  110. assert(Entries && "Call layout() before emitStringLiteralDef()");
  111. if (!EmitLongStrLiterals) {
  112. OS << Decl << " = {\n";
  113. emit(OS, printChar, "0");
  114. OS << " 0\n};\n\n";
  115. return;
  116. }
  117. OS << "\n#ifdef __GNUC__\n"
  118. << "#pragma GCC diagnostic push\n"
  119. << "#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n"
  120. << "#endif\n"
  121. << Decl << " = {\n";
  122. for (auto I : Seqs) {
  123. OS << " /* " << I.second << " */ \"";
  124. OS.write_escaped(I.first);
  125. OS << "\\0\"\n";
  126. }
  127. OS << "};\n"
  128. << "#ifdef __GNUC__\n"
  129. << "#pragma GCC diagnostic pop\n"
  130. << "#endif\n\n";
  131. }
  132. /// emit - Print out the table as the body of an array initializer.
  133. /// Use the Print function to print elements.
  134. void emit(raw_ostream &OS,
  135. void (*Print)(raw_ostream&, ElemT),
  136. const char *Term = "0") const {
  137. assert((empty() || Entries) && "Call layout() before emit()");
  138. for (typename SeqMap::const_iterator I = Seqs.begin(), E = Seqs.end();
  139. I != E; ++I) {
  140. OS << " /* " << I->second << " */ ";
  141. for (typename SeqT::const_iterator SI = I->first.begin(),
  142. SE = I->first.end(); SI != SE; ++SI) {
  143. Print(OS, *SI);
  144. OS << ", ";
  145. }
  146. OS << Term << ",\n";
  147. }
  148. }
  149. };
  150. } // end namespace llvm
  151. #endif