SequenceToOffsetTable.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // Helper function for SequenceToOffsetTable<string>.
  26. static inline void printStrLitEscChar(raw_ostream &OS, char C) {
  27. const char *Escapes[] = {
  28. "\\000", "\\001", "\\002", "\\003", "\\004", "\\005", "\\006", "\\007",
  29. "\\010", "\\t", "\\n", "\\013", "\\014", "\\r", "\\016", "\\017",
  30. "\\020", "\\021", "\\022", "\\023", "\\024", "\\025", "\\026", "\\027",
  31. "\\030", "\\031", "\\032", "\\033", "\\034", "\\035", "\\036", "\\037",
  32. " ", "!", "\\\"", "#", "$", "%", "&", "'",
  33. "(", ")", "*", "+", ",", "-", ".", "/",
  34. "0", "1", "2", "3", "4", "5", "6", "7",
  35. "8", "9", ":", ";", "<", "=", ">", "?",
  36. "@", "A", "B", "C", "D", "E", "F", "G",
  37. "H", "I", "J", "K", "L", "M", "N", "O",
  38. "P", "Q", "R", "S", "T", "U", "V", "W",
  39. "X", "Y", "Z", "[", "\\\\", "]", "^", "_",
  40. "`", "a", "b", "c", "d", "e", "f", "g",
  41. "h", "i", "j", "k", "l", "m", "n", "o",
  42. "p", "q", "r", "s", "t", "u", "v", "w",
  43. "x", "y", "z", "{", "|", "}", "~", "\\177",
  44. "\\200", "\\201", "\\202", "\\203", "\\204", "\\205", "\\206", "\\207",
  45. "\\210", "\\211", "\\212", "\\213", "\\214", "\\215", "\\216", "\\217",
  46. "\\220", "\\221", "\\222", "\\223", "\\224", "\\225", "\\226", "\\227",
  47. "\\230", "\\231", "\\232", "\\233", "\\234", "\\235", "\\236", "\\237",
  48. "\\240", "\\241", "\\242", "\\243", "\\244", "\\245", "\\246", "\\247",
  49. "\\250", "\\251", "\\252", "\\253", "\\254", "\\255", "\\256", "\\257",
  50. "\\260", "\\261", "\\262", "\\263", "\\264", "\\265", "\\266", "\\267",
  51. "\\270", "\\271", "\\272", "\\273", "\\274", "\\275", "\\276", "\\277",
  52. "\\300", "\\301", "\\302", "\\303", "\\304", "\\305", "\\306", "\\307",
  53. "\\310", "\\311", "\\312", "\\313", "\\314", "\\315", "\\316", "\\317",
  54. "\\320", "\\321", "\\322", "\\323", "\\324", "\\325", "\\326", "\\327",
  55. "\\330", "\\331", "\\332", "\\333", "\\334", "\\335", "\\336", "\\337",
  56. "\\340", "\\341", "\\342", "\\343", "\\344", "\\345", "\\346", "\\347",
  57. "\\350", "\\351", "\\352", "\\353", "\\354", "\\355", "\\356", "\\357",
  58. "\\360", "\\361", "\\362", "\\363", "\\364", "\\365", "\\366", "\\367",
  59. "\\370", "\\371", "\\372", "\\373", "\\374", "\\375", "\\376", "\\377"};
  60. static_assert(sizeof Escapes / sizeof Escapes[0] ==
  61. std::numeric_limits<unsigned char>::max() + 1,
  62. "unsupported character type");
  63. OS << Escapes[static_cast<unsigned char>(C)];
  64. }
  65. static inline void printChar(raw_ostream &OS, char C) {
  66. unsigned char UC(C);
  67. if (isalnum(UC) || ispunct(UC)) {
  68. OS << '\'';
  69. if (C == '\\' || C == '\'')
  70. OS << '\\';
  71. OS << C << '\'';
  72. } else {
  73. OS << unsigned(UC);
  74. }
  75. }
  76. /// SequenceToOffsetTable - Collect a number of terminated sequences of T.
  77. /// Compute the layout of a table that contains all the sequences, possibly by
  78. /// reusing entries.
  79. ///
  80. /// @tparam SeqT The sequence container. (vector or string).
  81. /// @tparam Less A stable comparator for SeqT elements.
  82. template<typename SeqT, typename Less = std::less<typename SeqT::value_type> >
  83. class SequenceToOffsetTable {
  84. typedef typename SeqT::value_type ElemT;
  85. // Define a comparator for SeqT that sorts a suffix immediately before a
  86. // sequence with that suffix.
  87. struct SeqLess {
  88. Less L;
  89. bool operator()(const SeqT &A, const SeqT &B) const {
  90. return std::lexicographical_compare(A.rbegin(), A.rend(),
  91. B.rbegin(), B.rend(), L);
  92. }
  93. };
  94. // Keep sequences ordered according to SeqLess so suffixes are easy to find.
  95. // Map each sequence to its offset in the table.
  96. typedef std::map<SeqT, unsigned, SeqLess> SeqMap;
  97. // Sequences added so far, with suffixes removed.
  98. SeqMap Seqs;
  99. // Entries in the final table, or 0 before layout was called.
  100. unsigned Entries;
  101. // isSuffix - Returns true if A is a suffix of B.
  102. static bool isSuffix(const SeqT &A, const SeqT &B) {
  103. return A.size() <= B.size() && std::equal(A.rbegin(), A.rend(), B.rbegin());
  104. }
  105. public:
  106. SequenceToOffsetTable() : Entries(0) {}
  107. /// add - Add a sequence to the table.
  108. /// This must be called before layout().
  109. void add(const SeqT &Seq) {
  110. assert(Entries == 0 && "Cannot call add() after layout()");
  111. typename SeqMap::iterator I = Seqs.lower_bound(Seq);
  112. // If SeqMap contains a sequence that has Seq as a suffix, I will be
  113. // pointing to it.
  114. if (I != Seqs.end() && isSuffix(Seq, I->first))
  115. return;
  116. I = Seqs.insert(I, std::make_pair(Seq, 0u));
  117. // The entry before I may be a suffix of Seq that can now be erased.
  118. if (I != Seqs.begin() && isSuffix((--I)->first, Seq))
  119. Seqs.erase(I);
  120. }
  121. bool empty() const { return Seqs.empty(); }
  122. unsigned size() const {
  123. assert((empty() || Entries) && "Call layout() before size()");
  124. return Entries;
  125. }
  126. /// layout - Computes the final table layout.
  127. void layout() {
  128. assert(Entries == 0 && "Can only call layout() once");
  129. // Lay out the table in Seqs iteration order.
  130. for (typename SeqMap::iterator I = Seqs.begin(), E = Seqs.end(); I != E;
  131. ++I) {
  132. I->second = Entries;
  133. // Include space for a terminator.
  134. Entries += I->first.size() + 1;
  135. }
  136. }
  137. /// get - Returns the offset of Seq in the final table.
  138. unsigned get(const SeqT &Seq) const {
  139. assert(Entries && "Call layout() before get()");
  140. typename SeqMap::const_iterator I = Seqs.lower_bound(Seq);
  141. assert(I != Seqs.end() && isSuffix(Seq, I->first) &&
  142. "get() called with sequence that wasn't added first");
  143. return I->second + (I->first.size() - Seq.size());
  144. }
  145. /// `emitStringLiteralDef` - Print out the table as the body of an array
  146. /// initializer, where each element is a C string literal terminated by
  147. /// `\0`. Falls back to emitting a comma-separated integer list if
  148. /// `EmitLongStrLiterals` is false
  149. void emitStringLiteralDef(raw_ostream &OS, const llvm::Twine &Decl) const {
  150. assert(Entries && "Call layout() before emitStringLiteralDef()");
  151. if (EmitLongStrLiterals) {
  152. OS << "\n#ifdef __GNUC__\n"
  153. << "#pragma GCC diagnostic push\n"
  154. << "#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n"
  155. << "#endif\n"
  156. << Decl << " = {\n";
  157. } else {
  158. OS << Decl << " = {\n";
  159. emit(OS, printChar, "0");
  160. OS << "\n};\n\n";
  161. return;
  162. }
  163. for (auto I : Seqs) {
  164. OS << " /* " << I.second << " */ \"";
  165. for (auto C : I.first) {
  166. printStrLitEscChar(OS, C);
  167. }
  168. OS << "\\0\"\n";
  169. }
  170. OS << "};\n"
  171. << "#ifdef __GNUC__\n"
  172. << "#pragma GCC diagnostic pop\n"
  173. << "#endif\n\n";
  174. }
  175. /// emit - Print out the table as the body of an array initializer.
  176. /// Use the Print function to print elements.
  177. void emit(raw_ostream &OS,
  178. void (*Print)(raw_ostream&, ElemT),
  179. const char *Term = "0") const {
  180. assert((empty() || Entries) && "Call layout() before emit()");
  181. for (typename SeqMap::const_iterator I = Seqs.begin(), E = Seqs.end();
  182. I != E; ++I) {
  183. OS << " /* " << I->second << " */ ";
  184. for (typename SeqT::const_iterator SI = I->first.begin(),
  185. SE = I->first.end(); SI != SE; ++SI) {
  186. Print(OS, *SI);
  187. OS << ", ";
  188. }
  189. OS << Term << ",\n";
  190. }
  191. }
  192. };
  193. } // end namespace llvm
  194. #endif