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