[llvm-commits] [llvm] r78513 - /llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp

Daniel Dunbar daniel at zuster.org
Sat Aug 8 15:57:25 PDT 2009


Author: ddunbar
Date: Sat Aug  8 17:57:25 2009
New Revision: 78513

URL: http://llvm.org/viewvc/llvm-project?rev=78513&view=rev
Log:
llvm-mc/AsmMatcher: Tweak string matcher.
 - Track whether we need to insert an explicit 'break'.

 - Invert conditional when matching a single prefix to reduce
   nesting/bracing/breaking.

 - wc -l of X86GenAsmMatcher.inc decreased by 10%. :)

Modified:
    llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp

Modified: llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp?rev=78513&r1=78512&r2=78513&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp Sat Aug  8 17:57:25 2009
@@ -734,7 +734,9 @@
 /// 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.
-static void EmitStringMatcherForChar(const std::string &StrVariableName,
+///
+/// \return - True if control can leave the emitted code fragment.
+static bool EmitStringMatcherForChar(const std::string &StrVariableName,
                                   const std::vector<const StringPair*> &Matches,
                                      unsigned CharNo, unsigned IndentCount,
                                      raw_ostream &OS) {
@@ -749,7 +751,7 @@
     // FIXME: If Matches[0].first has embeded \n, this will be bad.
     OS << Indent << Matches[0]->second << "\t // \"" << Matches[0]->first
        << "\"\n";
-    return;
+    return false;
   }
   
   // Bucket the matches by the character we are comparing.
@@ -766,24 +768,24 @@
     unsigned FirstNonCommonLetter = FindFirstNonCommonLetter(Matches);
     unsigned NumChars = FirstNonCommonLetter-CharNo;
     
+    // Emit code to break out if the prefix doesn't match.
     if (NumChars == 1) {
-      // Do the comparison with if (Str[1] == 'f')
+      // 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 << "if (" << StrVariableName << "[" << CharNo << "] != '"
+         << Matches[0]->first[CharNo] << "')\n";
+      OS << Indent << "  break;\n";
     } else {
-      // Do the comparison with if (Str.substr(1,3) == "foo").
-      OS << Indent << "if (" << StrVariableName << ".substr(" << CharNo << ","
-         << NumChars << ") == \"";
-    
+      // Do the comparison with if (Str.substr(1,3) != "foo").    
       // FIXME: Need to escape general strings.
-      OS << Matches[0]->first.substr(CharNo, NumChars) << "\") {\n";
+      OS << Indent << "if (" << StrVariableName << ".substr(" << CharNo << ","
+         << NumChars << ") != \"";
+      OS << Matches[0]->first.substr(CharNo, NumChars) << "\")\n";
+      OS << Indent << "  break;";
     }
     
-    EmitStringMatcherForChar(StrVariableName, Matches, FirstNonCommonLetter,
-                             IndentCount+1, OS);
-    OS << Indent << "}\n";
-    return;
+    return EmitStringMatcherForChar(StrVariableName, Matches, 
+                                    FirstNonCommonLetter, IndentCount, OS);
   }
   
   // Otherwise, we have multiple possible things, emit a switch on the
@@ -796,21 +798,23 @@
     // TODO: escape hard stuff (like \n) if we ever care about it.
     OS << Indent << "case '" << LI->first << "':\t // "
        << LI->second.size() << " strings to match.\n";
-    EmitStringMatcherForChar(StrVariableName, LI->second, CharNo+1,
-                             IndentCount+1, OS);
-    OS << Indent << "  break;\n";
+    if (EmitStringMatcherForChar(StrVariableName, LI->second, CharNo+1,
+                                 IndentCount+1, OS))
+      OS << Indent << "  break;\n";
   }
   
   OS << Indent << "}\n";
-  
+  return true;
 }
 
 
 /// EmitStringMatcher - Given a list of strings and code to execute when they
-/// match, output a simple switch tree to classify the input string.  If a
-/// match is found, the code in Vals[i].second is executed.  This code should do
-/// a return to avoid falling through.  If nothing matches, execution falls
-/// through.  StrVariableName is the name of teh variable to test.
+/// match, output a simple switch tree to classify the input string.
+/// 
+/// If a match is found, the code in Vals[i].second is executed; control must
+/// not exit this code fragment.  If nothing matches, execution falls through.
+///
+/// \param StrVariableName - The name of the variable to test.
 static void EmitStringMatcher(const std::string &StrVariableName,
                               const std::vector<StringPair> &Matches,
                               raw_ostream &OS) {
@@ -825,16 +829,14 @@
   OS << "  switch (" << StrVariableName << ".size()) {\n";
   OS << "  default: break;\n";
   
-  
   for (std::map<unsigned, std::vector<const StringPair*> >::iterator LI =
        MatchesByLength.begin(), E = MatchesByLength.end(); LI != E; ++LI) {
     OS << "  case " << LI->first << ":\t // " << LI->second.size()
        << " strings to match.\n";
-    EmitStringMatcherForChar(StrVariableName, LI->second, 0, 0, OS);
-    OS << "    break;\n";
+    if (EmitStringMatcherForChar(StrVariableName, LI->second, 0, 0, OS))
+      OS << "    break;\n";
   }
   
-  
   OS << "  }\n";
 }
 





More information about the llvm-commits mailing list