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