109467b48Spatrick //===- StringMatcher.cpp - Generate a matcher for input strings -----------===// 209467b48Spatrick // 309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 609467b48Spatrick // 709467b48Spatrick //===----------------------------------------------------------------------===// 809467b48Spatrick // 909467b48Spatrick // This file implements the StringMatcher class. 1009467b48Spatrick // 1109467b48Spatrick //===----------------------------------------------------------------------===// 1209467b48Spatrick 1309467b48Spatrick #include "llvm/TableGen/StringMatcher.h" 1409467b48Spatrick #include "llvm/ADT/StringRef.h" 1509467b48Spatrick #include "llvm/Support/raw_ostream.h" 1609467b48Spatrick #include <cassert> 1709467b48Spatrick #include <map> 1809467b48Spatrick #include <string> 1909467b48Spatrick #include <utility> 2009467b48Spatrick #include <vector> 2109467b48Spatrick 2209467b48Spatrick using namespace llvm; 2309467b48Spatrick 2409467b48Spatrick /// FindFirstNonCommonLetter - Find the first character in the keys of the 2509467b48Spatrick /// string pairs that is not shared across the whole set of strings. All 2609467b48Spatrick /// strings are assumed to have the same length. 2709467b48Spatrick static unsigned 2809467b48Spatrick FindFirstNonCommonLetter(const std::vector<const 2909467b48Spatrick StringMatcher::StringPair*> &Matches) { 3009467b48Spatrick assert(!Matches.empty()); 3109467b48Spatrick for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) { 3209467b48Spatrick // Check to see if letter i is the same across the set. 3309467b48Spatrick char Letter = Matches[0]->first[i]; 3409467b48Spatrick 3509467b48Spatrick for (unsigned str = 0, e = Matches.size(); str != e; ++str) 3609467b48Spatrick if (Matches[str]->first[i] != Letter) 3709467b48Spatrick return i; 3809467b48Spatrick } 3909467b48Spatrick 4009467b48Spatrick return Matches[0]->first.size(); 4109467b48Spatrick } 4209467b48Spatrick 4309467b48Spatrick /// EmitStringMatcherForChar - Given a set of strings that are known to be the 4409467b48Spatrick /// same length and whose characters leading up to CharNo are the same, emit 4509467b48Spatrick /// code to verify that CharNo and later are the same. 4609467b48Spatrick /// 4709467b48Spatrick /// \return - True if control can leave the emitted code fragment. 4809467b48Spatrick bool StringMatcher::EmitStringMatcherForChar( 4909467b48Spatrick const std::vector<const StringPair *> &Matches, unsigned CharNo, 5009467b48Spatrick unsigned IndentCount, bool IgnoreDuplicates) const { 5109467b48Spatrick assert(!Matches.empty() && "Must have at least one string to match!"); 5209467b48Spatrick std::string Indent(IndentCount * 2 + 4, ' '); 5309467b48Spatrick 5409467b48Spatrick // If we have verified that the entire string matches, we're done: output the 5509467b48Spatrick // matching code. 5609467b48Spatrick if (CharNo == Matches[0]->first.size()) { 5709467b48Spatrick if (Matches.size() > 1 && !IgnoreDuplicates) 5809467b48Spatrick report_fatal_error("Had duplicate keys to match on"); 5909467b48Spatrick 6009467b48Spatrick // If the to-execute code has \n's in it, indent each subsequent line. 6109467b48Spatrick StringRef Code = Matches[0]->second; 6209467b48Spatrick 6309467b48Spatrick std::pair<StringRef, StringRef> Split = Code.split('\n'); 6409467b48Spatrick OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n"; 6509467b48Spatrick 6609467b48Spatrick Code = Split.second; 6709467b48Spatrick while (!Code.empty()) { 6809467b48Spatrick Split = Code.split('\n'); 6909467b48Spatrick OS << Indent << Split.first << "\n"; 7009467b48Spatrick Code = Split.second; 7109467b48Spatrick } 7209467b48Spatrick return false; 7309467b48Spatrick } 7409467b48Spatrick 7509467b48Spatrick // Bucket the matches by the character we are comparing. 7609467b48Spatrick std::map<char, std::vector<const StringPair*>> MatchesByLetter; 7709467b48Spatrick 7809467b48Spatrick for (unsigned i = 0, e = Matches.size(); i != e; ++i) 7909467b48Spatrick MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]); 8009467b48Spatrick 8109467b48Spatrick 8209467b48Spatrick // If we have exactly one bucket to match, see how many characters are common 8309467b48Spatrick // across the whole set and match all of them at once. 8409467b48Spatrick if (MatchesByLetter.size() == 1) { 8509467b48Spatrick unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches); 8609467b48Spatrick unsigned NumChars = FirstNonCommonLetter-CharNo; 8709467b48Spatrick 8809467b48Spatrick // Emit code to break out if the prefix doesn't match. 8909467b48Spatrick if (NumChars == 1) { 9009467b48Spatrick // Do the comparison with if (Str[1] != 'f') 9109467b48Spatrick // FIXME: Need to escape general characters. 9209467b48Spatrick OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '" 9309467b48Spatrick << Matches[0]->first[CharNo] << "')\n"; 9409467b48Spatrick OS << Indent << " break;\n"; 9509467b48Spatrick } else { 9609467b48Spatrick // Do the comparison with if memcmp(Str.data()+1, "foo", 3). 9709467b48Spatrick // FIXME: Need to escape general strings. 9809467b48Spatrick OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo 9909467b48Spatrick << ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", " 10009467b48Spatrick << NumChars << ") != 0)\n"; 10109467b48Spatrick OS << Indent << " break;\n"; 10209467b48Spatrick } 10309467b48Spatrick 10409467b48Spatrick return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount, 10509467b48Spatrick IgnoreDuplicates); 10609467b48Spatrick } 10709467b48Spatrick 10809467b48Spatrick // Otherwise, we have multiple possible things, emit a switch on the 10909467b48Spatrick // character. 11009467b48Spatrick OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n"; 11109467b48Spatrick OS << Indent << "default: break;\n"; 11209467b48Spatrick 113*73471bf0Spatrick for (const auto &LI : MatchesByLetter) { 11409467b48Spatrick // TODO: escape hard stuff (like \n) if we ever care about it. 115*73471bf0Spatrick OS << Indent << "case '" << LI.first << "':\t // " << LI.second.size() 116*73471bf0Spatrick << " string"; 117*73471bf0Spatrick if (LI.second.size() != 1) 118*73471bf0Spatrick OS << 's'; 11909467b48Spatrick OS << " to match.\n"; 120*73471bf0Spatrick if (EmitStringMatcherForChar(LI.second, CharNo + 1, IndentCount + 1, 12109467b48Spatrick IgnoreDuplicates)) 12209467b48Spatrick OS << Indent << " break;\n"; 12309467b48Spatrick } 12409467b48Spatrick 12509467b48Spatrick OS << Indent << "}\n"; 12609467b48Spatrick return true; 12709467b48Spatrick } 12809467b48Spatrick 12909467b48Spatrick /// Emit - Top level entry point. 13009467b48Spatrick /// 13109467b48Spatrick void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const { 13209467b48Spatrick // If nothing to match, just fall through. 13309467b48Spatrick if (Matches.empty()) return; 13409467b48Spatrick 13509467b48Spatrick // First level categorization: group strings by length. 13609467b48Spatrick std::map<unsigned, std::vector<const StringPair*>> MatchesByLength; 13709467b48Spatrick 13809467b48Spatrick for (unsigned i = 0, e = Matches.size(); i != e; ++i) 13909467b48Spatrick MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]); 14009467b48Spatrick 14109467b48Spatrick // Output a switch statement on length and categorize the elements within each 14209467b48Spatrick // bin. 14309467b48Spatrick OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n"; 14409467b48Spatrick OS.indent(Indent*2+2) << "default: break;\n"; 14509467b48Spatrick 146*73471bf0Spatrick for (const auto &LI : MatchesByLength) { 147*73471bf0Spatrick OS.indent(Indent * 2 + 2) 148*73471bf0Spatrick << "case " << LI.first << ":\t // " << LI.second.size() << " string" 149*73471bf0Spatrick << (LI.second.size() == 1 ? "" : "s") << " to match.\n"; 150*73471bf0Spatrick if (EmitStringMatcherForChar(LI.second, 0, Indent, IgnoreDuplicates)) 15109467b48Spatrick OS.indent(Indent*2+4) << "break;\n"; 15209467b48Spatrick } 15309467b48Spatrick 15409467b48Spatrick OS.indent(Indent*2+2) << "}\n"; 15509467b48Spatrick } 156