2010-09-06 10:01:51 +08:00
|
|
|
//===- StringMatcher.cpp - Generate a matcher for input strings -----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-09-06 10:01:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the StringMatcher class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/TableGen/StringMatcher.h"
|
2017-01-04 10:02:05 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2010-09-06 10:01:51 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-04 10:02:05 +08:00
|
|
|
#include <cassert>
|
2010-09-06 10:01:51 +08:00
|
|
|
#include <map>
|
2017-01-04 10:02:05 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// FindFirstNonCommonLetter - Find the first character in the keys of the
|
|
|
|
/// string pairs that is not shared across the whole set of strings. All
|
|
|
|
/// strings are assumed to have the same length.
|
2018-07-31 03:41:25 +08:00
|
|
|
static unsigned
|
2010-09-06 10:01:51 +08:00
|
|
|
FindFirstNonCommonLetter(const std::vector<const
|
|
|
|
StringMatcher::StringPair*> &Matches) {
|
|
|
|
assert(!Matches.empty());
|
|
|
|
for (unsigned i = 0, e = Matches[0]->first.size(); i != e; ++i) {
|
|
|
|
// Check to see if letter i is the same across the set.
|
|
|
|
char Letter = Matches[0]->first[i];
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
for (unsigned str = 0, e = Matches.size(); str != e; ++str)
|
|
|
|
if (Matches[str]->first[i] != Letter)
|
|
|
|
return i;
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
return Matches[0]->first.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitStringMatcherForChar - Given a set of strings that are known to be the
|
|
|
|
/// same length and whose characters leading up to CharNo are the same, emit
|
|
|
|
/// code to verify that CharNo and later are the same.
|
|
|
|
///
|
|
|
|
/// \return - True if control can leave the emitted code fragment.
|
2017-12-07 17:51:55 +08:00
|
|
|
bool StringMatcher::EmitStringMatcherForChar(
|
|
|
|
const std::vector<const StringPair *> &Matches, unsigned CharNo,
|
|
|
|
unsigned IndentCount, bool IgnoreDuplicates) const {
|
2010-09-06 10:01:51 +08:00
|
|
|
assert(!Matches.empty() && "Must have at least one string to match!");
|
2017-12-07 17:51:55 +08:00
|
|
|
std::string Indent(IndentCount * 2 + 4, ' ');
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// If we have verified that the entire string matches, we're done: output the
|
|
|
|
// matching code.
|
|
|
|
if (CharNo == Matches[0]->first.size()) {
|
2017-12-07 17:51:55 +08:00
|
|
|
if (Matches.size() > 1 && !IgnoreDuplicates)
|
|
|
|
report_fatal_error("Had duplicate keys to match on");
|
|
|
|
|
2010-10-31 03:57:17 +08:00
|
|
|
// If the to-execute code has \n's in it, indent each subsequent line.
|
|
|
|
StringRef Code = Matches[0]->second;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-10-31 03:57:17 +08:00
|
|
|
std::pair<StringRef, StringRef> Split = Code.split('\n');
|
|
|
|
OS << Indent << Split.first << "\t // \"" << Matches[0]->first << "\"\n";
|
|
|
|
|
|
|
|
Code = Split.second;
|
|
|
|
while (!Code.empty()) {
|
|
|
|
Split = Code.split('\n');
|
|
|
|
OS << Indent << Split.first << "\n";
|
|
|
|
Code = Split.second;
|
|
|
|
}
|
2010-09-06 10:01:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// Bucket the matches by the character we are comparing.
|
2017-01-04 10:02:05 +08:00
|
|
|
std::map<char, std::vector<const StringPair*>> MatchesByLetter;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
for (unsigned i = 0, e = Matches.size(); i != e; ++i)
|
|
|
|
MatchesByLetter[Matches[i]->first[CharNo]].push_back(Matches[i]);
|
2018-07-31 03:41:25 +08:00
|
|
|
|
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// If we have exactly one bucket to match, see how many characters are common
|
|
|
|
// across the whole set and match all of them at once.
|
|
|
|
if (MatchesByLetter.size() == 1) {
|
|
|
|
unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
|
|
|
|
unsigned NumChars = FirstNonCommonLetter-CharNo;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// Emit code to break out if the prefix doesn't match.
|
|
|
|
if (NumChars == 1) {
|
|
|
|
// Do the comparison with if (Str[1] != 'f')
|
|
|
|
// FIXME: Need to escape general characters.
|
|
|
|
OS << Indent << "if (" << StrVariableName << "[" << CharNo << "] != '"
|
|
|
|
<< Matches[0]->first[CharNo] << "')\n";
|
|
|
|
OS << Indent << " break;\n";
|
|
|
|
} else {
|
2012-05-21 02:10:42 +08:00
|
|
|
// Do the comparison with if memcmp(Str.data()+1, "foo", 3).
|
2010-09-06 10:01:51 +08:00
|
|
|
// FIXME: Need to escape general strings.
|
2012-05-21 02:10:42 +08:00
|
|
|
OS << Indent << "if (memcmp(" << StrVariableName << ".data()+" << CharNo
|
|
|
|
<< ", \"" << Matches[0]->first.substr(CharNo, NumChars) << "\", "
|
2017-01-04 10:02:05 +08:00
|
|
|
<< NumChars << ") != 0)\n";
|
2010-09-06 10:01:51 +08:00
|
|
|
OS << Indent << " break;\n";
|
|
|
|
}
|
2017-12-07 17:51:55 +08:00
|
|
|
|
|
|
|
return EmitStringMatcherForChar(Matches, FirstNonCommonLetter, IndentCount,
|
|
|
|
IgnoreDuplicates);
|
2010-09-06 10:01:51 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// Otherwise, we have multiple possible things, emit a switch on the
|
|
|
|
// character.
|
|
|
|
OS << Indent << "switch (" << StrVariableName << "[" << CharNo << "]) {\n";
|
|
|
|
OS << Indent << "default: break;\n";
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2021-03-07 07:52:55 +08:00
|
|
|
for (const auto &LI : MatchesByLetter) {
|
2010-09-06 10:01:51 +08:00
|
|
|
// TODO: escape hard stuff (like \n) if we ever care about it.
|
2021-03-07 07:52:55 +08:00
|
|
|
OS << Indent << "case '" << LI.first << "':\t // " << LI.second.size()
|
|
|
|
<< " string";
|
|
|
|
if (LI.second.size() != 1)
|
|
|
|
OS << 's';
|
2010-09-06 11:11:10 +08:00
|
|
|
OS << " to match.\n";
|
2021-03-07 07:52:55 +08:00
|
|
|
if (EmitStringMatcherForChar(LI.second, CharNo + 1, IndentCount + 1,
|
2017-12-07 17:51:55 +08:00
|
|
|
IgnoreDuplicates))
|
2010-09-06 10:01:51 +08:00
|
|
|
OS << Indent << " break;\n";
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
OS << Indent << "}\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit - Top level entry point.
|
|
|
|
///
|
2017-12-07 17:51:55 +08:00
|
|
|
void StringMatcher::Emit(unsigned Indent, bool IgnoreDuplicates) const {
|
2010-09-06 11:50:59 +08:00
|
|
|
// If nothing to match, just fall through.
|
|
|
|
if (Matches.empty()) return;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// First level categorization: group strings by length.
|
2017-01-04 10:02:05 +08:00
|
|
|
std::map<unsigned, std::vector<const StringPair*>> MatchesByLength;
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
for (unsigned i = 0, e = Matches.size(); i != e; ++i)
|
|
|
|
MatchesByLength[Matches[i].first.size()].push_back(&Matches[i]);
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 10:01:51 +08:00
|
|
|
// Output a switch statement on length and categorize the elements within each
|
|
|
|
// bin.
|
2010-09-06 11:50:59 +08:00
|
|
|
OS.indent(Indent*2+2) << "switch (" << StrVariableName << ".size()) {\n";
|
|
|
|
OS.indent(Indent*2+2) << "default: break;\n";
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2021-03-07 07:52:55 +08:00
|
|
|
for (const auto &LI : MatchesByLength) {
|
|
|
|
OS.indent(Indent * 2 + 2)
|
|
|
|
<< "case " << LI.first << ":\t // " << LI.second.size() << " string"
|
|
|
|
<< (LI.second.size() == 1 ? "" : "s") << " to match.\n";
|
|
|
|
if (EmitStringMatcherForChar(LI.second, 0, Indent, IgnoreDuplicates))
|
2010-09-06 11:50:59 +08:00
|
|
|
OS.indent(Indent*2+4) << "break;\n";
|
2010-09-06 10:01:51 +08:00
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2010-09-06 11:50:59 +08:00
|
|
|
OS.indent(Indent*2+2) << "}\n";
|
2010-09-06 10:01:51 +08:00
|
|
|
}
|