Chris Lattner | ca5a355 | 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 | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame^] | 14 | #include "llvm/TableGen/StringMatcher.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 17 | #include <cassert> |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 18 | #include <map> |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 | |
Chris Lattner | ca5a355 | 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. |
| 28 | static unsigned |
| 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]; |
| 35 | |
| 36 | for (unsigned str = 0, e = Matches.size(); str != e; ++str) |
| 37 | if (Matches[str]->first[i] != Letter) |
| 38 | return i; |
| 39 | } |
| 40 | |
| 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. |
| 49 | bool StringMatcher:: |
| 50 | EmitStringMatcherForChar(const std::vector<const StringPair*> &Matches, |
| 51 | unsigned CharNo, unsigned IndentCount) const { |
| 52 | assert(!Matches.empty() && "Must have at least one string to match!"); |
| 53 | std::string Indent(IndentCount*2+4, ' '); |
| 54 | |
| 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()) { |
| 58 | assert(Matches.size() == 1 && "Had duplicate keys to match on"); |
| 59 | |
Chris Lattner | 25896af4 | 2010-10-30 19:57:17 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
| 75 | // Bucket the matches by the character we are comparing. |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 76 | std::map<char, std::vector<const StringPair*>> MatchesByLetter; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 77 | |
| 78 | for (unsigned i = 0, e = Matches.size(); i != e; ++i) |
| 79 | MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]); |
| 80 | |
| 81 | |
| 82 | // If we have exactly one bucket to match, see how many characters are common |
| 83 | // across the whole set and match all of them at once. |
| 84 | if (MatchesByLetter.size() == 1) { |
| 85 | unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches); |
| 86 | unsigned NumChars = FirstNonCommonLetter-CharNo; |
| 87 | |
| 88 | // Emit code to break out if the prefix doesn't match. |
| 89 | if (NumChars == 1) { |
| 90 | // Do the comparison with if (Str[1] != 'f') |
| 91 | // FIXME: Need to escape general characters. |
| 92 | OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '" |
| 93 | << Matches[0]->first[CharNo] << "')\n"; |
| 94 | OS << Indent << " break;\n"; |
| 95 | } else { |
Benjamin Kramer | a405558 | 2012-05-20 18:10:42 +0000 | [diff] [blame] | 96 | // Do the comparison with if memcmp(Str.data()+1, "foo", 3). |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 97 | // FIXME: Need to escape general strings. |
Benjamin Kramer | a405558 | 2012-05-20 18:10:42 +0000 | [diff] [blame] | 98 | OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo |
| 99 | << ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", " |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 100 | << NumChars << ") != 0)\n"; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 101 | OS << Indent << " break;\n"; |
| 102 | } |
| 103 | |
| 104 | return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount); |
| 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 | |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 112 | for (std::map<char, std::vector<const StringPair*>>::iterator LI = |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 113 | MatchesByLetter.begin(), E = MatchesByLetter.end(); LI != E; ++LI) { |
| 114 | // TODO: escape hard stuff (like \n) if we ever care about it. |
| 115 | OS << Indent << "case '" << LI->first << "':\t // " |
Chris Lattner | 9bb3bf1 | 2010-09-06 03:11:10 +0000 | [diff] [blame] | 116 | << LI->second.size() << " string"; |
| 117 | if (LI->second.size() != 1) OS << 's'; |
| 118 | OS << " to match.\n"; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 119 | if (EmitStringMatcherForChar(LI->second, CharNo+1, IndentCount+1)) |
| 120 | OS << Indent << " break;\n"; |
| 121 | } |
| 122 | |
| 123 | OS << Indent << "}\n"; |
| 124 | return true; |
| 125 | } |
| 126 | |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 127 | /// Emit - Top level entry point. |
| 128 | /// |
Chris Lattner | 96fe532 | 2010-09-06 03:50:59 +0000 | [diff] [blame] | 129 | void StringMatcher::Emit(unsigned Indent) const { |
| 130 | // If nothing to match, just fall through. |
| 131 | if (Matches.empty()) return; |
| 132 | |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 133 | // First level categorization: group strings by length. |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 134 | std::map<unsigned, std::vector<const StringPair*>> MatchesByLength; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 135 | |
| 136 | for (unsigned i = 0, e = Matches.size(); i != e; ++i) |
| 137 | MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]); |
| 138 | |
| 139 | // Output a switch statement on length and categorize the elements within each |
| 140 | // bin. |
Chris Lattner | 96fe532 | 2010-09-06 03:50:59 +0000 | [diff] [blame] | 141 | OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n"; |
| 142 | OS.indent(Indent*2+2) << "default: break;\n"; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 143 | |
Eugene Zelenko | b2ca1b3 | 2017-01-04 02:02:05 +0000 | [diff] [blame] | 144 | for (std::map<unsigned, std::vector<const StringPair*>>::iterator LI = |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 145 | MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) { |
Chris Lattner | 96fe532 | 2010-09-06 03:50:59 +0000 | [diff] [blame] | 146 | OS.indent(Indent*2+2) << "case " << LI->first << ":\t // " |
| 147 | << LI->second.size() |
Chris Lattner | 6dcaa42 | 2010-09-06 03:12:27 +0000 | [diff] [blame] | 148 | << " string" << (LI->second.size() == 1 ? "" : "s") << " to match.\n"; |
Chris Lattner | 96fe532 | 2010-09-06 03:50:59 +0000 | [diff] [blame] | 149 | if (EmitStringMatcherForChar(LI->second, 0, Indent)) |
| 150 | OS.indent(Indent*2+4) << "break;\n"; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chris Lattner | 96fe532 | 2010-09-06 03:50:59 +0000 | [diff] [blame] | 153 | OS.indent(Indent*2+2) << "}\n"; |
Chris Lattner | ca5a355 | 2010-09-06 02:01:51 +0000 | [diff] [blame] | 154 | } |