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