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