StringMatcher.cpp 5.6 KB

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