1 //===-- SequenceToOffsetTable.h - Compress similar sequences ----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_UTILS_TABLEGEN_BASIC_SEQUENCETOOFFSETTABLE_H
16 #define LLVM_UTILS_TABLEGEN_BASIC_SEQUENCETOOFFSETTABLE_H
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/raw_ostream.h"
27 extern cl::opt
<bool> EmitLongStrLiterals
;
29 inline void printChar(raw_ostream
&OS
, char C
) {
31 if (isAlnum(UC
) || isPunct(UC
)) {
33 if (C
== '\\' || C
== '\'')
41 /// SequenceToOffsetTable - Collect a number of terminated sequences of T.
42 /// Compute the layout of a table that contains all the sequences, possibly by
45 /// @tparam SeqT The sequence container. (vector or string).
46 /// @tparam Less A stable comparator for SeqT elements.
47 template <typename SeqT
, typename Less
= std::less
<typename
SeqT::value_type
>>
48 class SequenceToOffsetTable
{
49 typedef typename
SeqT::value_type ElemT
;
51 // Define a comparator for SeqT that sorts a suffix immediately before a
52 // sequence with that suffix.
55 bool operator()(const SeqT
&A
, const SeqT
&B
) const {
56 return std::lexicographical_compare(A
.rbegin(), A
.rend(), B
.rbegin(),
61 // Keep sequences ordered according to SeqLess so suffixes are easy to find.
62 // Map each sequence to its offset in the table.
63 typedef std::map
<SeqT
, unsigned, SeqLess
> SeqMap
;
65 // Sequences added so far, with suffixes removed.
68 // Entries in the final table, or 0 before layout was called.
71 // isSuffix - Returns true if A is a suffix of B.
72 static bool isSuffix(const SeqT
&A
, const SeqT
&B
) {
73 return A
.size() <= B
.size() && std::equal(A
.rbegin(), A
.rend(), B
.rbegin());
77 SequenceToOffsetTable() : Entries(0) {}
79 /// add - Add a sequence to the table.
80 /// This must be called before layout().
81 void add(const SeqT
&Seq
) {
82 assert(Entries
== 0 && "Cannot call add() after layout()");
83 typename
SeqMap::iterator I
= Seqs
.lower_bound(Seq
);
85 // If SeqMap contains a sequence that has Seq as a suffix, I will be
87 if (I
!= Seqs
.end() && isSuffix(Seq
, I
->first
))
90 I
= Seqs
.insert(I
, std::pair(Seq
, 0u));
92 // The entry before I may be a suffix of Seq that can now be erased.
93 if (I
!= Seqs
.begin() && isSuffix((--I
)->first
, Seq
))
97 bool empty() const { return Seqs
.empty(); }
99 unsigned size() const {
100 assert((empty() || Entries
) && "Call layout() before size()");
104 /// layout - Computes the final table layout.
106 assert(Entries
== 0 && "Can only call layout() once");
107 // Lay out the table in Seqs iteration order.
108 for (typename
SeqMap::iterator I
= Seqs
.begin(), E
= Seqs
.end(); I
!= E
;
111 // Include space for a terminator.
112 Entries
+= I
->first
.size() + 1;
116 /// get - Returns the offset of Seq in the final table.
117 unsigned get(const SeqT
&Seq
) const {
118 assert(Entries
&& "Call layout() before get()");
119 typename
SeqMap::const_iterator I
= Seqs
.lower_bound(Seq
);
120 assert(I
!= Seqs
.end() && isSuffix(Seq
, I
->first
) &&
121 "get() called with sequence that wasn't added first");
122 return I
->second
+ (I
->first
.size() - Seq
.size());
125 /// `emitStringLiteralDef` - Print out the table as the body of an array
126 /// initializer, where each element is a C string literal terminated by
127 /// `\0`. Falls back to emitting a comma-separated integer list if
128 /// `EmitLongStrLiterals` is false
129 void emitStringLiteralDef(raw_ostream
&OS
, const Twine
&Decl
) const {
130 assert(Entries
&& "Call layout() before emitStringLiteralDef()");
131 if (!EmitLongStrLiterals
) {
132 OS
<< Decl
<< " = {\n";
133 emit(OS
, printChar
, "0");
138 OS
<< "\n#ifdef __GNUC__\n"
139 << "#pragma GCC diagnostic push\n"
140 << "#pragma GCC diagnostic ignored \"-Woverlength-strings\"\n"
143 for (const auto &[Seq
, Offset
] : Seqs
) {
144 OS
<< " /* " << Offset
<< " */ \"";
145 OS
.write_escaped(Seq
);
149 << "#ifdef __GNUC__\n"
150 << "#pragma GCC diagnostic pop\n"
154 /// emit - Print out the table as the body of an array initializer.
155 /// Use the Print function to print elements.
156 void emit(raw_ostream
&OS
, void (*Print
)(raw_ostream
&, ElemT
),
157 const char *Term
= "0") const {
158 assert((empty() || Entries
) && "Call layout() before emit()");
159 for (const auto &[Seq
, Offset
] : Seqs
) {
160 OS
<< " /* " << Offset
<< " */ ";
161 for (const ElemT
&Element
: Seq
) {
170 } // end namespace llvm
172 #endif // LLVM_UTILS_TABLEGEN_BASIC_SEQUENCETOOFFSETTABLE_H