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