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