xref: /aosp_15_r20/external/llvm/lib/TableGen/StringMatcher.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- StringMatcher.cpp - Generate a matcher for input strings -----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the StringMatcher class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/TableGen/StringMatcher.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
16*9880d681SAndroid Build Coastguard Worker #include <map>
17*9880d681SAndroid Build Coastguard Worker using namespace llvm;
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker /// FindFirstNonCommonLetter - Find the first character in the keys of the
20*9880d681SAndroid Build Coastguard Worker /// string pairs that is not shared across the whole set of strings.  All
21*9880d681SAndroid Build Coastguard Worker /// strings are assumed to have the same length.
22*9880d681SAndroid Build Coastguard Worker static unsigned
FindFirstNonCommonLetter(const std::vector<const StringMatcher::StringPair * > & Matches)23*9880d681SAndroid Build Coastguard Worker FindFirstNonCommonLetter(const std::vector<const
24*9880d681SAndroid Build Coastguard Worker                               StringMatcher::StringPair*> &Matches) {
25*9880d681SAndroid Build Coastguard Worker   assert(!Matches.empty());
26*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
27*9880d681SAndroid Build Coastguard Worker     // Check to see if letter i is the same across the set.
28*9880d681SAndroid Build Coastguard Worker     char Letter = Matches[0]->first[i];
29*9880d681SAndroid Build Coastguard Worker 
30*9880d681SAndroid Build Coastguard Worker     for (unsigned str = 0, e = Matches.size(); str != e; ++str)
31*9880d681SAndroid Build Coastguard Worker       if (Matches[str]->first[i] != Letter)
32*9880d681SAndroid Build Coastguard Worker         return i;
33*9880d681SAndroid Build Coastguard Worker   }
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker   return Matches[0]->first.size();
36*9880d681SAndroid Build Coastguard Worker }
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker /// EmitStringMatcherForChar - Given a set of strings that are known to be the
39*9880d681SAndroid Build Coastguard Worker /// same length and whose characters leading up to CharNo are the same, emit
40*9880d681SAndroid Build Coastguard Worker /// code to verify that CharNo and later are the same.
41*9880d681SAndroid Build Coastguard Worker ///
42*9880d681SAndroid Build Coastguard Worker /// \return - True if control can leave the emitted code fragment.
43*9880d681SAndroid Build Coastguard Worker bool StringMatcher::
EmitStringMatcherForChar(const std::vector<const StringPair * > & Matches,unsigned CharNo,unsigned IndentCount) const44*9880d681SAndroid Build Coastguard Worker EmitStringMatcherForChar(const std::vector<const StringPair*> &Matches,
45*9880d681SAndroid Build Coastguard Worker                          unsigned CharNo, unsigned IndentCount) const {
46*9880d681SAndroid Build Coastguard Worker   assert(!Matches.empty() && "Must have at least one string to match!");
47*9880d681SAndroid Build Coastguard Worker   std::string Indent(IndentCount*2+4, ' ');
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker   // If we have verified that the entire string matches, we're done: output the
50*9880d681SAndroid Build Coastguard Worker   // matching code.
51*9880d681SAndroid Build Coastguard Worker   if (CharNo == Matches[0]->first.size()) {
52*9880d681SAndroid Build Coastguard Worker     assert(Matches.size() == 1 && "Had duplicate keys to match on");
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker     // If the to-execute code has \n's in it, indent each subsequent line.
55*9880d681SAndroid Build Coastguard Worker     StringRef Code = Matches[0]->second;
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker     std::pair<StringRef, StringRef> Split = Code.split('\n');
58*9880d681SAndroid Build Coastguard Worker     OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n";
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker     Code = Split.second;
61*9880d681SAndroid Build Coastguard Worker     while (!Code.empty()) {
62*9880d681SAndroid Build Coastguard Worker       Split = Code.split('\n');
63*9880d681SAndroid Build Coastguard Worker       OS << Indent << Split.first << "\n";
64*9880d681SAndroid Build Coastguard Worker       Code = Split.second;
65*9880d681SAndroid Build Coastguard Worker     }
66*9880d681SAndroid Build Coastguard Worker     return false;
67*9880d681SAndroid Build Coastguard Worker   }
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker   // Bucket the matches by the character we are comparing.
70*9880d681SAndroid Build Coastguard Worker   std::map<char, std::vector<const StringPair*> > MatchesByLetter;
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Matches.size(); i != e; ++i)
73*9880d681SAndroid Build Coastguard Worker     MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]);
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker   // If we have exactly one bucket to match, see how many characters are common
77*9880d681SAndroid Build Coastguard Worker   // across the whole set and match all of them at once.
78*9880d681SAndroid Build Coastguard Worker   if (MatchesByLetter.size() == 1) {
79*9880d681SAndroid Build Coastguard Worker     unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
80*9880d681SAndroid Build Coastguard Worker     unsigned NumChars = FirstNonCommonLetter-CharNo;
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker     // Emit code to break out if the prefix doesn't match.
83*9880d681SAndroid Build Coastguard Worker     if (NumChars == 1) {
84*9880d681SAndroid Build Coastguard Worker       // Do the comparison with if (Str[1] != 'f')
85*9880d681SAndroid Build Coastguard Worker       // FIXME: Need to escape general characters.
86*9880d681SAndroid Build Coastguard Worker       OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"
87*9880d681SAndroid Build Coastguard Worker       << Matches[0]->first[CharNo] << "')\n";
88*9880d681SAndroid Build Coastguard Worker       OS << Indent << "  break;\n";
89*9880d681SAndroid Build Coastguard Worker     } else {
90*9880d681SAndroid Build Coastguard Worker       // Do the comparison with if memcmp(Str.data()+1, "foo", 3).
91*9880d681SAndroid Build Coastguard Worker       // FIXME: Need to escape general strings.
92*9880d681SAndroid Build Coastguard Worker       OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo
93*9880d681SAndroid Build Coastguard Worker          << ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", "
94*9880d681SAndroid Build Coastguard Worker          << NumChars << "))\n";
95*9880d681SAndroid Build Coastguard Worker       OS << Indent << "  break;\n";
96*9880d681SAndroid Build Coastguard Worker     }
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker     return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount);
99*9880d681SAndroid Build Coastguard Worker   }
100*9880d681SAndroid Build Coastguard Worker 
101*9880d681SAndroid Build Coastguard Worker   // Otherwise, we have multiple possible things, emit a switch on the
102*9880d681SAndroid Build Coastguard Worker   // character.
103*9880d681SAndroid Build Coastguard Worker   OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
104*9880d681SAndroid Build Coastguard Worker   OS << Indent << "default: break;\n";
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   for (std::map<char, std::vector<const StringPair*> >::iterator LI =
107*9880d681SAndroid Build Coastguard Worker        MatchesByLetter.begin(), E = MatchesByLetter.end(); LI != E; ++LI) {
108*9880d681SAndroid Build Coastguard Worker     // TODO: escape hard stuff (like \n) if we ever care about it.
109*9880d681SAndroid Build Coastguard Worker     OS << Indent << "case '" << LI->first << "':\t // "
110*9880d681SAndroid Build Coastguard Worker        << LI->second.size() << " string";
111*9880d681SAndroid Build Coastguard Worker     if (LI->second.size() != 1) OS << 's';
112*9880d681SAndroid Build Coastguard Worker     OS << " to match.\n";
113*9880d681SAndroid Build Coastguard Worker     if (EmitStringMatcherForChar(LI->second, CharNo+1, IndentCount+1))
114*9880d681SAndroid Build Coastguard Worker       OS << Indent << "  break;\n";
115*9880d681SAndroid Build Coastguard Worker   }
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker   OS << Indent << "}\n";
118*9880d681SAndroid Build Coastguard Worker   return true;
119*9880d681SAndroid Build Coastguard Worker }
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker /// Emit - Top level entry point.
123*9880d681SAndroid Build Coastguard Worker ///
Emit(unsigned Indent) const124*9880d681SAndroid Build Coastguard Worker void StringMatcher::Emit(unsigned Indent) const {
125*9880d681SAndroid Build Coastguard Worker   // If nothing to match, just fall through.
126*9880d681SAndroid Build Coastguard Worker   if (Matches.empty()) return;
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker   // First level categorization: group strings by length.
129*9880d681SAndroid Build Coastguard Worker   std::map<unsigned, std::vector<const StringPair*> > MatchesByLength;
130*9880d681SAndroid Build Coastguard Worker 
131*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Matches.size(); i != e; ++i)
132*9880d681SAndroid Build Coastguard Worker     MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]);
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker   // Output a switch statement on length and categorize the elements within each
135*9880d681SAndroid Build Coastguard Worker   // bin.
136*9880d681SAndroid Build Coastguard Worker   OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
137*9880d681SAndroid Build Coastguard Worker   OS.indent(Indent*2+2) << "default: break;\n";
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker   for (std::map<unsigned, std::vector<const StringPair*> >::iterator LI =
140*9880d681SAndroid Build Coastguard Worker        MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) {
141*9880d681SAndroid Build Coastguard Worker     OS.indent(Indent*2+2) << "case " << LI->first << ":\t // "
142*9880d681SAndroid Build Coastguard Worker        << LI->second.size()
143*9880d681SAndroid Build Coastguard Worker        << " string" << (LI->second.size() == 1 ? "" : "s") << " to match.\n";
144*9880d681SAndroid Build Coastguard Worker     if (EmitStringMatcherForChar(LI->second, 0, Indent))
145*9880d681SAndroid Build Coastguard Worker       OS.indent(Indent*2+4) << "break;\n";
146*9880d681SAndroid Build Coastguard Worker   }
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker   OS.indent(Indent*2+2) << "}\n";
149*9880d681SAndroid Build Coastguard Worker }
150