blob: 1c43b6d1c8aadaabcc733a0a3584267231696657 [file] [log] [blame]
Chris Lattner5845e5c2010-09-06 02:01:51 +00001//===- 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
14#include "StringMatcher.h"
15#include "llvm/Support/raw_ostream.h"
16#include <map>
17using namespace llvm;
18
19/// FindFirstNonCommonLetter - Find the first character in the keys of the
20/// string pairs that is not shared across the whole set of strings. All
21/// strings are assumed to have the same length.
22static unsigned
23FindFirstNonCommonLetter(const std::vector<const
24 StringMatcher::StringPair*> &Matches) {
25 assert(!Matches.empty());
26 for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
27 // Check to see if letter i is the same across the set.
28 char Letter = Matches[0]->first[i];
29
30 for (unsigned str = 0, e = Matches.size(); str != e; ++str)
31 if (Matches[str]->first[i] != Letter)
32 return i;
33 }
34
35 return Matches[0]->first.size();
36}
37
38/// EmitStringMatcherForChar - Given a set of strings that are known to be the
39/// same length and whose characters leading up to CharNo are the same, emit
40/// code to verify that CharNo and later are the same.
41///
42/// \return - True if control can leave the emitted code fragment.
43bool StringMatcher::
44EmitStringMatcherForChar(const std::vector<const StringPair*> &Matches,
45 unsigned CharNo, unsigned IndentCount) const {
46 assert(!Matches.empty() && "Must have at least one string to match!");
47 std::string Indent(IndentCount*2+4, ' ');
48
49 // If we have verified that the entire string matches, we're done: output the
50 // matching code.
51 if (CharNo == Matches[0]->first.size()) {
52 assert(Matches.size() == 1 && "Had duplicate keys to match on");
53
54 // FIXME: If Matches[0].first has embeded \n, this will be bad.
55 OS << Indent << Matches[0]->second << "\t // \"" << Matches[0]->first
56 << "\"\n";
57 return false;
58 }
59
60 // Bucket the matches by the character we are comparing.
61 std::map<char, std::vector<const StringPair*> > MatchesByLetter;
62
63 for (unsigned i = 0, e = Matches.size(); i != e; ++i)
64 MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]);
65
66
67 // If we have exactly one bucket to match, see how many characters are common
68 // across the whole set and match all of them at once.
69 if (MatchesByLetter.size() == 1) {
70 unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
71 unsigned NumChars = FirstNonCommonLetter-CharNo;
72
73 // Emit code to break out if the prefix doesn't match.
74 if (NumChars == 1) {
75 // Do the comparison with if (Str[1] != 'f')
76 // FIXME: Need to escape general characters.
77 OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"
78 << Matches[0]->first[CharNo] << "')\n";
79 OS << Indent << " break;\n";
80 } else {
Chris Lattnercc67c752010-09-06 03:58:45 +000081 // Do the comparison with if (Str.substr(1, 3) != "foo").
Chris Lattner5845e5c2010-09-06 02:01:51 +000082 // FIXME: Need to escape general strings.
Chris Lattnercc67c752010-09-06 03:58:45 +000083 OS << Indent << "if (" << StrVariableName << ".substr(" << CharNo << ", "
Chris Lattner5845e5c2010-09-06 02:01:51 +000084 << NumChars << ") != \"";
85 OS << Matches[0]->first.substr(CharNo, NumChars) << "\")\n";
86 OS << Indent << " break;\n";
87 }
88
89 return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount);
90 }
91
92 // Otherwise, we have multiple possible things, emit a switch on the
93 // character.
94 OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
95 OS << Indent << "default: break;\n";
96
97 for (std::map<char, std::vector<const StringPair*> >::iterator LI =
98 MatchesByLetter.begin(), E = MatchesByLetter.end(); LI != E; ++LI) {
99 // TODO: escape hard stuff (like \n) if we ever care about it.
100 OS << Indent << "case '" << LI->first << "':\t // "
Chris Lattner6d7c3072010-09-06 03:11:10 +0000101 << LI->second.size() << " string";
102 if (LI->second.size() != 1) OS << 's';
103 OS << " to match.\n";
Chris Lattner5845e5c2010-09-06 02:01:51 +0000104 if (EmitStringMatcherForChar(LI->second, CharNo+1, IndentCount+1))
105 OS << Indent << " break;\n";
106 }
107
108 OS << Indent << "}\n";
109 return true;
110}
111
112
113/// Emit - Top level entry point.
114///
Chris Lattner902edf22010-09-06 03:50:59 +0000115void StringMatcher::Emit(unsigned Indent) const {
116 // If nothing to match, just fall through.
117 if (Matches.empty()) return;
118
Chris Lattner5845e5c2010-09-06 02:01:51 +0000119 // First level categorization: group strings by length.
120 std::map<unsigned, std::vector<const StringPair*> > MatchesByLength;
121
122 for (unsigned i = 0, e = Matches.size(); i != e; ++i)
123 MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]);
124
125 // Output a switch statement on length and categorize the elements within each
126 // bin.
Chris Lattner902edf22010-09-06 03:50:59 +0000127 OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
128 OS.indent(Indent*2+2) << "default: break;\n";
Chris Lattner5845e5c2010-09-06 02:01:51 +0000129
130 for (std::map<unsigned, std::vector<const StringPair*> >::iterator LI =
131 MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) {
Chris Lattner902edf22010-09-06 03:50:59 +0000132 OS.indent(Indent*2+2) << "case " << LI->first << ":\t // "
133 << LI->second.size()
Chris Lattner8e4fdef2010-09-06 03:12:27 +0000134 << " string" << (LI->second.size() == 1 ? "" : "s") << " to match.\n";
Chris Lattner902edf22010-09-06 03:50:59 +0000135 if (EmitStringMatcherForChar(LI->second, 0, Indent))
136 OS.indent(Indent*2+4) << "break;\n";
Chris Lattner5845e5c2010-09-06 02:01:51 +0000137 }
138
Chris Lattner902edf22010-09-06 03:50:59 +0000139 OS.indent(Indent*2+2) << "}\n";
Chris Lattner5845e5c2010-09-06 02:01:51 +0000140}