17ae0e2c9SDimitry Andric //===- StringMatcher.cpp - Generate a matcher for input strings -----------===//
27ae0e2c9SDimitry Andric //
37ae0e2c9SDimitry Andric //                     The LLVM Compiler Infrastructure
47ae0e2c9SDimitry Andric //
57ae0e2c9SDimitry Andric // This file is distributed under the University of Illinois Open Source
67ae0e2c9SDimitry Andric // License. See LICENSE.TXT for details.
77ae0e2c9SDimitry Andric //
87ae0e2c9SDimitry Andric //===----------------------------------------------------------------------===//
97ae0e2c9SDimitry Andric //
107ae0e2c9SDimitry Andric // This file implements the StringMatcher class.
117ae0e2c9SDimitry Andric //
127ae0e2c9SDimitry Andric //===----------------------------------------------------------------------===//
137ae0e2c9SDimitry Andric 
14db17bf38SDimitry Andric #include "llvm/TableGen/StringMatcher.h"
158e0f8b8cSDimitry Andric #include "llvm/ADT/StringRef.h"
167ae0e2c9SDimitry Andric #include "llvm/Support/raw_ostream.h"
178e0f8b8cSDimitry Andric #include <cassert>
187ae0e2c9SDimitry Andric #include <map>
198e0f8b8cSDimitry Andric #include <string>
208e0f8b8cSDimitry Andric #include <utility>
218e0f8b8cSDimitry Andric #include <vector>
228e0f8b8cSDimitry Andric 
237ae0e2c9SDimitry Andric using namespace llvm;
247ae0e2c9SDimitry Andric 
257ae0e2c9SDimitry Andric /// FindFirstNonCommonLetter - Find the first character in the keys of the
267ae0e2c9SDimitry Andric /// string pairs that is not shared across the whole set of strings.  All
277ae0e2c9SDimitry Andric /// strings are assumed to have the same length.
287ae0e2c9SDimitry Andric static unsigned
FindFirstNonCommonLetter(const std::vector<const StringMatcher::StringPair * > & Matches)297ae0e2c9SDimitry Andric FindFirstNonCommonLetter(const std::vector<const
307ae0e2c9SDimitry Andric                               StringMatcher::StringPair*> &Matches) {
317ae0e2c9SDimitry Andric   assert(!Matches.empty());
327ae0e2c9SDimitry Andric   for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
337ae0e2c9SDimitry Andric     // Check to see if letter i is the same across the set.
347ae0e2c9SDimitry Andric     char Letter = Matches[0]->first[i];
357ae0e2c9SDimitry Andric 
367ae0e2c9SDimitry Andric     for (unsigned str = 0, e = Matches.size(); str != e; ++str)
377ae0e2c9SDimitry Andric       if (Matches[str]->first[i] != Letter)
387ae0e2c9SDimitry Andric         return i;
397ae0e2c9SDimitry Andric   }
407ae0e2c9SDimitry Andric 
417ae0e2c9SDimitry Andric   return Matches[0]->first.size();
427ae0e2c9SDimitry Andric }
437ae0e2c9SDimitry Andric 
447ae0e2c9SDimitry Andric /// EmitStringMatcherForChar - Given a set of strings that are known to be the
457ae0e2c9SDimitry Andric /// same length and whose characters leading up to CharNo are the same, emit
467ae0e2c9SDimitry Andric /// code to verify that CharNo and later are the same.
477ae0e2c9SDimitry Andric ///
487ae0e2c9SDimitry Andric /// \return - True if control can leave the emitted code fragment.
EmitStringMatcherForChar(const std::vector<const StringPair * > & Matches,unsigned CharNo,unsigned IndentCount,bool IgnoreDuplicates) const492cab237bSDimitry Andric bool StringMatcher::EmitStringMatcherForChar(
502cab237bSDimitry Andric     const std::vector<const StringPair *> &Matches, unsigned CharNo,
512cab237bSDimitry Andric     unsigned IndentCount, bool IgnoreDuplicates) const {
527ae0e2c9SDimitry Andric   assert(!Matches.empty() && "Must have at least one string to match!");
537ae0e2c9SDimitry Andric   std::string Indent(IndentCount * 2 + 4, ' ');
547ae0e2c9SDimitry Andric 
557ae0e2c9SDimitry Andric   // If we have verified that the entire string matches, we're done: output the
567ae0e2c9SDimitry Andric   // matching code.
577ae0e2c9SDimitry Andric   if (CharNo == Matches[0]->first.size()) {
582cab237bSDimitry Andric     if (Matches.size() > 1 && !IgnoreDuplicates)
592cab237bSDimitry Andric       report_fatal_error("Had duplicate keys to match on");
607ae0e2c9SDimitry Andric 
617ae0e2c9SDimitry Andric     // If the to-execute code has \n's in it, indent each subsequent line.
627ae0e2c9SDimitry Andric     StringRef Code = Matches[0]->second;
637ae0e2c9SDimitry Andric 
647ae0e2c9SDimitry Andric     std::pair<StringRef, StringRef> Split = Code.split('\n');
657ae0e2c9SDimitry Andric     OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n";
667ae0e2c9SDimitry Andric 
677ae0e2c9SDimitry Andric     Code = Split.second;
687ae0e2c9SDimitry Andric     while (!Code.empty()) {
697ae0e2c9SDimitry Andric       Split = Code.split('\n');
707ae0e2c9SDimitry Andric       OS << Indent << Split.first << "\n";
717ae0e2c9SDimitry Andric       Code = Split.second;
727ae0e2c9SDimitry Andric     }
737ae0e2c9SDimitry Andric     return false;
747ae0e2c9SDimitry Andric   }
757ae0e2c9SDimitry Andric 
767ae0e2c9SDimitry Andric   // Bucket the matches by the character we are comparing.
777ae0e2c9SDimitry Andric   std::map<char, std::vector<const StringPair*>> MatchesByLetter;
787ae0e2c9SDimitry Andric 
797ae0e2c9SDimitry Andric   for (unsigned i = 0, e = Matches.size(); i != e; ++i)
807ae0e2c9SDimitry Andric     MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]);
817ae0e2c9SDimitry Andric 
827ae0e2c9SDimitry Andric 
837ae0e2c9SDimitry Andric   // If we have exactly one bucket to match, see how many characters are common
847ae0e2c9SDimitry Andric   // across the whole set and match all of them at once.
857ae0e2c9SDimitry Andric   if (MatchesByLetter.size() == 1) {
867ae0e2c9SDimitry Andric     unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
877ae0e2c9SDimitry Andric     unsigned NumChars = FirstNonCommonLetter-CharNo;
887ae0e2c9SDimitry Andric 
897ae0e2c9SDimitry Andric     // Emit code to break out if the prefix doesn't match.
907ae0e2c9SDimitry Andric     if (NumChars == 1) {
917ae0e2c9SDimitry Andric       // Do the comparison with if (Str[1] != 'f')
927ae0e2c9SDimitry Andric       // FIXME: Need to escape general characters.
937ae0e2c9SDimitry Andric       OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"
947ae0e2c9SDimitry Andric       << Matches[0]->first[CharNo] << "')\n";
957ae0e2c9SDimitry Andric       OS << Indent << "  break;\n";
967ae0e2c9SDimitry Andric     } else {
977ae0e2c9SDimitry Andric       // Do the comparison with if memcmp(Str.data()+1, "foo", 3).
987ae0e2c9SDimitry Andric       // FIXME: Need to escape general strings.
997ae0e2c9SDimitry Andric       OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo
1007ae0e2c9SDimitry Andric          << ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", "
1018e0f8b8cSDimitry Andric          << NumChars << ") != 0)\n";
1027ae0e2c9SDimitry Andric       OS << Indent << "  break;\n";
1037ae0e2c9SDimitry Andric     }
1047ae0e2c9SDimitry Andric 
1052cab237bSDimitry Andric     return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount,
1062cab237bSDimitry Andric                                     IgnoreDuplicates);
1077ae0e2c9SDimitry Andric   }
1087ae0e2c9SDimitry Andric 
1097ae0e2c9SDimitry Andric   // Otherwise, we have multiple possible things, emit a switch on the
1107ae0e2c9SDimitry Andric   // character.
1117ae0e2c9SDimitry Andric   OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
1127ae0e2c9SDimitry Andric   OS << Indent << "default: break;\n";
1137ae0e2c9SDimitry Andric 
1147ae0e2c9SDimitry Andric   for (std::map<char, std::vector<const StringPair*>>::iterator LI =
1157ae0e2c9SDimitry Andric        MatchesByLetter.begin(), E = MatchesByLetter.end(); LI != E; ++LI) {
1167ae0e2c9SDimitry Andric     // TODO: escape hard stuff (like \n) if we ever care about it.
1177ae0e2c9SDimitry Andric     OS << Indent << "case '" << LI->first << "':\t // "
1187ae0e2c9SDimitry Andric        << LI->second.size() << " string";
1197ae0e2c9SDimitry Andric     if (LI->second.size() != 1) OS << 's';
1207ae0e2c9SDimitry Andric     OS << " to match.\n";
1212cab237bSDimitry Andric     if (EmitStringMatcherForChar(LI->second, CharNo + 1, IndentCount + 1,
1222cab237bSDimitry Andric                                  IgnoreDuplicates))
1237ae0e2c9SDimitry Andric       OS << Indent << "  break;\n";
1247ae0e2c9SDimitry Andric   }
1257ae0e2c9SDimitry Andric 
1267ae0e2c9SDimitry Andric   OS << Indent << "}\n";
1277ae0e2c9SDimitry Andric   return true;
1287ae0e2c9SDimitry Andric }
1297ae0e2c9SDimitry Andric 
1307ae0e2c9SDimitry Andric /// Emit - Top level entry point.
1317ae0e2c9SDimitry Andric ///
Emit(unsigned Indent,bool IgnoreDuplicates) const1322cab237bSDimitry Andric void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const {
1337ae0e2c9SDimitry Andric   // If nothing to match, just fall through.
1347ae0e2c9SDimitry Andric   if (Matches.empty()) return;
1357ae0e2c9SDimitry Andric 
1367ae0e2c9SDimitry Andric   // First level categorization: group strings by length.
1377ae0e2c9SDimitry Andric   std::map<unsigned, std::vector<const StringPair*>> MatchesByLength;
1387ae0e2c9SDimitry Andric 
1397ae0e2c9SDimitry Andric   for (unsigned i = 0, e = Matches.size(); i != e; ++i)
1407ae0e2c9SDimitry Andric     MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]);
1417ae0e2c9SDimitry Andric 
1427ae0e2c9SDimitry Andric   // Output a switch statement on length and categorize the elements within each
1437ae0e2c9SDimitry Andric   // bin.
1447ae0e2c9SDimitry Andric   OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
1457ae0e2c9SDimitry Andric   OS.indent(Indent*2+2) << "default: break;\n";
1467ae0e2c9SDimitry Andric 
1477ae0e2c9SDimitry Andric   for (std::map<unsigned, std::vector<const StringPair*>>::iterator LI =
1487ae0e2c9SDimitry Andric        MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) {
1497ae0e2c9SDimitry Andric     OS.indent(Indent*2+2) << "case " << LI->first << ":\t // "
1507ae0e2c9SDimitry Andric        << LI->second.size()
1517ae0e2c9SDimitry Andric        << " string" << (LI->second.size() == 1 ? "" : "s") << " to match.\n";
1522cab237bSDimitry Andric     if (EmitStringMatcherForChar(LI->second, 0, Indent, IgnoreDuplicates))
1537ae0e2c9SDimitry Andric       OS.indent(Indent*2+4) << "break;\n";
1547ae0e2c9SDimitry Andric   }
1557ae0e2c9SDimitry Andric 
1567ae0e2c9SDimitry Andric   OS.indent(Indent*2+2) << "}\n";
1577ae0e2c9SDimitry Andric }
158