allow specifying an indentation level for the string matcher.
[oota-llvm.git] / utils / TableGen / StringMatcher.cpp
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
14 #include "StringMatcher.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <map>
17 using 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.
22 static unsigned 
23 FindFirstNonCommonLetter(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.
43 bool StringMatcher::
44 EmitStringMatcherForChar(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 {
81       // Do the comparison with if (Str.substr(1,3) != "foo").    
82       // FIXME: Need to escape general strings.
83       OS << Indent << "if (" << StrVariableName << ".substr(" << CharNo << ","
84       << 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 // "
101        << LI->second.size() << " string";
102     if (LI->second.size() != 1) OS << 's';
103     OS << " to match.\n";
104     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 ///
115 void StringMatcher::Emit(unsigned Indent) const {
116   // If nothing to match, just fall through.
117   if (Matches.empty()) return;
118   
119   // 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.
127   OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
128   OS.indent(Indent*2+2) << "default: break;\n";
129   
130   for (std::map<unsigned, std::vector<const StringPair*> >::iterator LI =
131        MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) {
132     OS.indent(Indent*2+2) << "case " << LI->first << ":\t // "
133        << LI->second.size()
134        << " string" << (LI->second.size() == 1 ? "" : "s") << " to match.\n";
135     if (EmitStringMatcherForChar(LI->second, 0, Indent))
136       OS.indent(Indent*2+4) << "break;\n";
137   }
138   
139   OS.indent(Indent*2+2) << "}\n";
140 }