xref: /openbsd-src/gnu/llvm/llvm/lib/TableGen/StringMatcher.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===- StringMatcher.cpp - Generate a matcher for input strings -----------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file implements the StringMatcher class.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #include "llvm/TableGen/StringMatcher.h"
1409467b48Spatrick #include "llvm/ADT/StringRef.h"
15*d415bd75Srobert #include "llvm/Support/ErrorHandling.h"
1609467b48Spatrick #include "llvm/Support/raw_ostream.h"
1709467b48Spatrick #include <cassert>
1809467b48Spatrick #include <map>
1909467b48Spatrick #include <string>
2009467b48Spatrick #include <utility>
2109467b48Spatrick #include <vector>
2209467b48Spatrick 
2309467b48Spatrick using namespace llvm;
2409467b48Spatrick 
2509467b48Spatrick /// FindFirstNonCommonLetter - Find the first character in the keys of the
2609467b48Spatrick /// string pairs that is not shared across the whole set of strings.  All
2709467b48Spatrick /// strings are assumed to have the same length.
2809467b48Spatrick static unsigned
FindFirstNonCommonLetter(const std::vector<const StringMatcher::StringPair * > & Matches)2909467b48Spatrick FindFirstNonCommonLetter(const std::vector<const
3009467b48Spatrick                               StringMatcher::StringPair*> &Matches) {
3109467b48Spatrick   assert(!Matches.empty());
3209467b48Spatrick   for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
3309467b48Spatrick     // Check to see if letter i is the same across the set.
3409467b48Spatrick     char Letter = Matches[0]->first[i];
3509467b48Spatrick 
36*d415bd75Srobert     for (const StringMatcher::StringPair *Match : Matches)
37*d415bd75Srobert       if (Match->first[i] != Letter)
3809467b48Spatrick         return i;
3909467b48Spatrick   }
4009467b48Spatrick 
4109467b48Spatrick   return Matches[0]->first.size();
4209467b48Spatrick }
4309467b48Spatrick 
4409467b48Spatrick /// EmitStringMatcherForChar - Given a set of strings that are known to be the
4509467b48Spatrick /// same length and whose characters leading up to CharNo are the same, emit
4609467b48Spatrick /// code to verify that CharNo and later are the same.
4709467b48Spatrick ///
4809467b48Spatrick /// \return - True if control can leave the emitted code fragment.
EmitStringMatcherForChar(const std::vector<const StringPair * > & Matches,unsigned CharNo,unsigned IndentCount,bool IgnoreDuplicates) const4909467b48Spatrick bool StringMatcher::EmitStringMatcherForChar(
5009467b48Spatrick     const std::vector<const StringPair *> &Matches, unsigned CharNo,
5109467b48Spatrick     unsigned IndentCount, bool IgnoreDuplicates) const {
5209467b48Spatrick   assert(!Matches.empty() && "Must have at least one string to match!");
5309467b48Spatrick   std::string Indent(IndentCount * 2 + 4, ' ');
5409467b48Spatrick 
5509467b48Spatrick   // If we have verified that the entire string matches, we're done: output the
5609467b48Spatrick   // matching code.
5709467b48Spatrick   if (CharNo == Matches[0]->first.size()) {
5809467b48Spatrick     if (Matches.size() > 1 && !IgnoreDuplicates)
5909467b48Spatrick       report_fatal_error("Had duplicate keys to match on");
6009467b48Spatrick 
6109467b48Spatrick     // If the to-execute code has \n's in it, indent each subsequent line.
6209467b48Spatrick     StringRef Code = Matches[0]->second;
6309467b48Spatrick 
6409467b48Spatrick     std::pair<StringRef, StringRef> Split = Code.split('\n');
6509467b48Spatrick     OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n";
6609467b48Spatrick 
6709467b48Spatrick     Code = Split.second;
6809467b48Spatrick     while (!Code.empty()) {
6909467b48Spatrick       Split = Code.split('\n');
7009467b48Spatrick       OS << Indent << Split.first << "\n";
7109467b48Spatrick       Code = Split.second;
7209467b48Spatrick     }
7309467b48Spatrick     return false;
7409467b48Spatrick   }
7509467b48Spatrick 
7609467b48Spatrick   // Bucket the matches by the character we are comparing.
7709467b48Spatrick   std::map<char, std::vector<const StringPair*>> MatchesByLetter;
7809467b48Spatrick 
79*d415bd75Srobert   for (const StringPair *Match : Matches)
80*d415bd75Srobert     MatchesByLetter[Match->first[CharNo]].push_back(Match);
8109467b48Spatrick 
8209467b48Spatrick   // If we have exactly one bucket to match, see how many characters are common
8309467b48Spatrick   // across the whole set and match all of them at once.
8409467b48Spatrick   if (MatchesByLetter.size() == 1) {
8509467b48Spatrick     unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
8609467b48Spatrick     unsigned NumChars = FirstNonCommonLetter-CharNo;
8709467b48Spatrick 
8809467b48Spatrick     // Emit code to break out if the prefix doesn't match.
8909467b48Spatrick     if (NumChars == 1) {
9009467b48Spatrick       // Do the comparison with if (Str[1] != 'f')
9109467b48Spatrick       // FIXME: Need to escape general characters.
9209467b48Spatrick       OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"
9309467b48Spatrick       << Matches[0]->first[CharNo] << "')\n";
9409467b48Spatrick       OS << Indent << "  break;\n";
9509467b48Spatrick     } else {
9609467b48Spatrick       // Do the comparison with if memcmp(Str.data()+1, "foo", 3).
9709467b48Spatrick       // FIXME: Need to escape general strings.
9809467b48Spatrick       OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo
9909467b48Spatrick          << ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", "
10009467b48Spatrick          << NumChars << ") != 0)\n";
10109467b48Spatrick       OS << Indent << "  break;\n";
10209467b48Spatrick     }
10309467b48Spatrick 
10409467b48Spatrick     return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount,
10509467b48Spatrick                                     IgnoreDuplicates);
10609467b48Spatrick   }
10709467b48Spatrick 
10809467b48Spatrick   // Otherwise, we have multiple possible things, emit a switch on the
10909467b48Spatrick   // character.
11009467b48Spatrick   OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
11109467b48Spatrick   OS << Indent << "default: break;\n";
11209467b48Spatrick 
11373471bf0Spatrick   for (const auto &LI : MatchesByLetter) {
11409467b48Spatrick     // TODO: escape hard stuff (like \n) if we ever care about it.
11573471bf0Spatrick     OS << Indent << "case '" << LI.first << "':\t // " << LI.second.size()
11673471bf0Spatrick        << " string";
11773471bf0Spatrick     if (LI.second.size() != 1)
11873471bf0Spatrick       OS << 's';
11909467b48Spatrick     OS << " to match.\n";
12073471bf0Spatrick     if (EmitStringMatcherForChar(LI.second, CharNo + 1, IndentCount + 1,
12109467b48Spatrick                                  IgnoreDuplicates))
12209467b48Spatrick       OS << Indent << "  break;\n";
12309467b48Spatrick   }
12409467b48Spatrick 
12509467b48Spatrick   OS << Indent << "}\n";
12609467b48Spatrick   return true;
12709467b48Spatrick }
12809467b48Spatrick 
12909467b48Spatrick /// Emit - Top level entry point.
13009467b48Spatrick ///
Emit(unsigned Indent,bool IgnoreDuplicates) const13109467b48Spatrick void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const {
13209467b48Spatrick   // If nothing to match, just fall through.
13309467b48Spatrick   if (Matches.empty()) return;
13409467b48Spatrick 
13509467b48Spatrick   // First level categorization: group strings by length.
13609467b48Spatrick   std::map<unsigned, std::vector<const StringPair*>> MatchesByLength;
13709467b48Spatrick 
138*d415bd75Srobert   for (const StringPair &Match : Matches)
139*d415bd75Srobert     MatchesByLength[Match.first.size()].push_back(&Match);
14009467b48Spatrick 
14109467b48Spatrick   // Output a switch statement on length and categorize the elements within each
14209467b48Spatrick   // bin.
14309467b48Spatrick   OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
14409467b48Spatrick   OS.indent(Indent*2+2) << "default: break;\n";
14509467b48Spatrick 
14673471bf0Spatrick   for (const auto &LI : MatchesByLength) {
14773471bf0Spatrick     OS.indent(Indent * 2 + 2)
14873471bf0Spatrick         << "case " << LI.first << ":\t // " << LI.second.size() << " string"
14973471bf0Spatrick         << (LI.second.size() == 1 ? "" : "s") << " to match.\n";
15073471bf0Spatrick     if (EmitStringMatcherForChar(LI.second, 0, Indent, IgnoreDuplicates))
15109467b48Spatrick       OS.indent(Indent*2+4) << "break;\n";
15209467b48Spatrick   }
15309467b48Spatrick 
15409467b48Spatrick   OS.indent(Indent*2+2) << "}\n";
15509467b48Spatrick }
156