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