[llvm-commits] [llvm] r117886 - in /llvm/trunk/utils/TableGen: AsmMatcherEmitter.cpp AsmWriterInst.cpp CodeGenInstruction.cpp CodeGenInstruction.h

Chris Lattner sabre at nondot.org
Sun Oct 31 18:07:14 PDT 2010


Author: lattner
Date: Sun Oct 31 20:07:14 2010
New Revision: 117886

URL: http://llvm.org/viewvc/llvm-project?rev=117886&view=rev
Log:
move FlattenVariants out of AsmMatcherEmitter into a shared
CodeGenInstruction::FlattenAsmStringVariants method.  Use it
to simplify the code in AsmWriterInst, which now no longer 
needs to worry about variants.

Modified:
    llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp
    llvm/trunk/utils/TableGen/AsmWriterInst.cpp
    llvm/trunk/utils/TableGen/CodeGenInstruction.cpp
    llvm/trunk/utils/TableGen/CodeGenInstruction.h

Modified: llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp?rev=117886&r1=117885&r2=117886&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/AsmMatcherEmitter.cpp Sun Oct 31 20:07:14 2010
@@ -92,54 +92,6 @@
 MatchPrefix("match-prefix", cl::init(""),
             cl::desc("Only match instructions with the given prefix"));
 
-/// FlattenVariants - Flatten an .td file assembly string by selecting the
-/// variant at index \arg N.
-static std::string FlattenVariants(const std::string &AsmString,
-                                   unsigned N) {
-  StringRef Cur = AsmString;
-  std::string Res = "";
-
-  for (;;) {
-    // Find the start of the next variant string.
-    size_t VariantsStart = 0;
-    for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
-      if (Cur[VariantsStart] == '{' &&
-          (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
-                                  Cur[VariantsStart-1] != '\\')))
-        break;
-
-    // Add the prefix to the result.
-    Res += Cur.slice(0, VariantsStart);
-    if (VariantsStart == Cur.size())
-      break;
-
-    ++VariantsStart; // Skip the '{'.
-
-    // Scan to the end of the variants string.
-    size_t VariantsEnd = VariantsStart;
-    unsigned NestedBraces = 1;
-    for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
-      if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
-        if (--NestedBraces == 0)
-          break;
-      } else if (Cur[VariantsEnd] == '{')
-        ++NestedBraces;
-    }
-
-    // Select the Nth variant (or empty).
-    StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
-    for (unsigned i = 0; i != N; ++i)
-      Selection = Selection.split('|').second;
-    Res += Selection.split('|').first;
-
-    assert(VariantsEnd != Cur.size() &&
-           "Unterminated variants in assembly string!");
-    Cur = Cur.substr(VariantsEnd + 1);
-  }
-
-  return Res;
-}
-
 /// TokenizeAsmString - Tokenize a simplified assembly string.
 static void TokenizeAsmString(StringRef AsmString,
                               SmallVectorImpl<StringRef> &Tokens) {
@@ -516,7 +468,6 @@
     return !(HasLT ^ HasGT);
   }
 
-public:
   void dump();
 };
 
@@ -932,7 +883,8 @@
 
     II->InstrName = CGI.TheDef->getName();
     II->Instr = &CGI;
-    II->AsmString = FlattenVariants(CGI.AsmString, 0);
+    // TODO: Eventually support asmparser for Variant != 0.
+    II->AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, 0);
 
     // Remove comments from the asm string.  We know that the asmstring only
     // has one line.
@@ -944,7 +896,8 @@
 
     TokenizeAsmString(II->AsmString, II->Tokens);
 
-    // Ignore instructions which shouldn't be matched.
+    // Ignore instructions which shouldn't be matched and diagnose invalid
+    // instruction definitions with an error.
     if (!IsAssemblerInstruction(CGI.TheDef->getName(), CGI, II->Tokens))
       continue;
     

Modified: llvm/trunk/utils/TableGen/AsmWriterInst.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/AsmWriterInst.cpp?rev=117886&r1=117885&r2=117886&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/AsmWriterInst.cpp (original)
+++ llvm/trunk/utils/TableGen/AsmWriterInst.cpp Sun Oct 31 20:07:14 2010
@@ -53,8 +53,6 @@
                              int OperandSpacing) {
   this->CGI = &CGI;
   
-  unsigned CurVariant = ~0U;  // ~0 if we are outside a {.|.|.} region, other #.
-  
   // This is the number of tabs we've seen if we're doing columnar layout.
   unsigned CurColumn = 0;
   
@@ -62,54 +60,48 @@
   // NOTE: Any extensions to this code need to be mirrored in the 
   // AsmPrinter::printInlineAsm code that executes as compile time (assuming
   // that inline asm strings should also get the new feature)!
-  const std::string &AsmString = CGI.AsmString;
+  std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant);
   std::string::size_type LastEmitted = 0;
   while (LastEmitted != AsmString.size()) {
     std::string::size_type DollarPos =
-    AsmString.find_first_of("${|}\\", LastEmitted);
+      AsmString.find_first_of("$\\", LastEmitted);
     if (DollarPos == std::string::npos) DollarPos = AsmString.size();
     
     // Emit a constant string fragment.
-    
     if (DollarPos != LastEmitted) {
-      if (CurVariant == Variant || CurVariant == ~0U) {
-        for (; LastEmitted != DollarPos; ++LastEmitted)
-          switch (AsmString[LastEmitted]) {
-            case '\n':
-              AddLiteralString("\\n");
-              break;
-            case '\t':
-              // If the asm writer is not using a columnar layout, \t is not
-              // magic.
-              if (FirstOperandColumn == -1 || OperandSpacing == -1) {
-                AddLiteralString("\\t");
-              } else {
-                // We recognize a tab as an operand delimeter.
-                unsigned DestColumn = FirstOperandColumn + 
-                CurColumn++ * OperandSpacing;
-                Operands.push_back(
-                  AsmWriterOperand(
-                    "O.PadToColumn(" +
-                    utostr(DestColumn) + ");\n",
-                    AsmWriterOperand::isLiteralStatementOperand));
-              }
-              break;
-            case '"':
-              AddLiteralString("\\\"");
-              break;
-            case '\\':
-              AddLiteralString("\\\\");
-              break;
-            default:
-              AddLiteralString(std::string(1, AsmString[LastEmitted]));
-              break;
-          }
-      } else {
-        LastEmitted = DollarPos;
-      }
+      for (; LastEmitted != DollarPos; ++LastEmitted)
+        switch (AsmString[LastEmitted]) {
+          case '\n':
+            AddLiteralString("\\n");
+            break;
+          case '\t':
+            // If the asm writer is not using a columnar layout, \t is not
+            // magic.
+            if (FirstOperandColumn == -1 || OperandSpacing == -1) {
+              AddLiteralString("\\t");
+            } else {
+              // We recognize a tab as an operand delimeter.
+              unsigned DestColumn = FirstOperandColumn + 
+              CurColumn++ * OperandSpacing;
+              Operands.push_back(
+                AsmWriterOperand(
+                  "O.PadToColumn(" +
+                  utostr(DestColumn) + ");\n",
+                  AsmWriterOperand::isLiteralStatementOperand));
+            }
+            break;
+          case '"':
+            AddLiteralString("\\\"");
+            break;
+          case '\\':
+            AddLiteralString("\\\\");
+            break;
+          default:
+            AddLiteralString(std::string(1, AsmString[LastEmitted]));
+            break;
+        }
     } else if (AsmString[DollarPos] == '\\') {
-      if (DollarPos+1 != AsmString.size() &&
-          (CurVariant == Variant || CurVariant == ~0U)) {
+      if (DollarPos+1 != AsmString.size()) {
         if (AsmString[DollarPos+1] == 'n') {
           AddLiteralString("\\n");
         } else if (AsmString[DollarPos+1] == 't') {
@@ -137,29 +129,9 @@
         LastEmitted = DollarPos+2;
         continue;
       }
-    } else if (AsmString[DollarPos] == '{') {
-      if (CurVariant != ~0U)
-        throw "Nested variants found for instruction '" +
-        CGI.TheDef->getName() + "'!";
-      LastEmitted = DollarPos+1;
-      CurVariant = 0;   // We are now inside of the variant!
-    } else if (AsmString[DollarPos] == '|') {
-      if (CurVariant == ~0U)
-        throw "'|' character found outside of a variant in instruction '"
-        + CGI.TheDef->getName() + "'!";
-      ++CurVariant;
-      ++LastEmitted;
-    } else if (AsmString[DollarPos] == '}') {
-      if (CurVariant == ~0U)
-        throw "'}' character found outside of a variant in instruction '"
-        + CGI.TheDef->getName() + "'!";
-      ++LastEmitted;
-      CurVariant = ~0U;
     } else if (DollarPos+1 != AsmString.size() &&
                AsmString[DollarPos+1] == '$') {
-      if (CurVariant == Variant || CurVariant == ~0U) {
-        AddLiteralString("$");  // "$$" -> $
-      }
+      AddLiteralString("$");  // "$$" -> $
       LastEmitted = DollarPos+2;
     } else {
       // Get the name of the variable.
@@ -229,13 +201,9 @@
         unsigned OpNo = CGI.getOperandNamed(VarName);
         CodeGenInstruction::OperandInfo OpInfo = CGI.OperandList[OpNo];
         
-        if (CurVariant == Variant || CurVariant == ~0U) {
-          unsigned MIOp = OpInfo.MIOperandNo;
-          Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, 
-                                              OpNo,
-                                              MIOp,
-                                              Modifier));
-        }
+        unsigned MIOp = OpInfo.MIOperandNo;
+        Operands.push_back(AsmWriterOperand(OpInfo.PrinterMethodName, 
+                                            OpNo, MIOp, Modifier));
       }
       LastEmitted = VarEnd;
     }

Modified: llvm/trunk/utils/TableGen/CodeGenInstruction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/CodeGenInstruction.cpp?rev=117886&r1=117885&r2=117886&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/CodeGenInstruction.cpp (original)
+++ llvm/trunk/utils/TableGen/CodeGenInstruction.cpp Sun Oct 31 20:07:14 2010
@@ -321,3 +321,52 @@
   return MVT::Other;
 }
 
+
+/// FlattenAsmStringVariants - Flatten the specified AsmString to only
+/// include text from the specified variant, returning the new string.
+std::string CodeGenInstruction::
+FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
+  std::string Res = "";
+  
+  for (;;) {
+    // Find the start of the next variant string.
+    size_t VariantsStart = 0;
+    for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
+      if (Cur[VariantsStart] == '{' &&
+          (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
+                                  Cur[VariantsStart-1] != '\\')))
+        break;
+    
+    // Add the prefix to the result.
+    Res += Cur.slice(0, VariantsStart);
+    if (VariantsStart == Cur.size())
+      break;
+    
+    ++VariantsStart; // Skip the '{'.
+    
+    // Scan to the end of the variants string.
+    size_t VariantsEnd = VariantsStart;
+    unsigned NestedBraces = 1;
+    for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
+      if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
+        if (--NestedBraces == 0)
+          break;
+      } else if (Cur[VariantsEnd] == '{')
+        ++NestedBraces;
+    }
+    
+    // Select the Nth variant (or empty).
+    StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
+    for (unsigned i = 0; i != Variant; ++i)
+      Selection = Selection.split('|').second;
+    Res += Selection.split('|').first;
+    
+    assert(VariantsEnd != Cur.size() &&
+           "Unterminated variants in assembly string!");
+    Cur = Cur.substr(VariantsEnd + 1);
+  }
+  
+  return Res;
+}
+
+

Modified: llvm/trunk/utils/TableGen/CodeGenInstruction.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/CodeGenInstruction.h?rev=117886&r1=117885&r2=117886&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/CodeGenInstruction.h (original)
+++ llvm/trunk/utils/TableGen/CodeGenInstruction.h Sun Oct 31 20:07:14 2010
@@ -23,6 +23,7 @@
   class Record;
   class DagInit;
   class CodeGenTarget;
+  class StringRef;
 
   class CodeGenInstruction {
   public:
@@ -201,6 +202,12 @@
     /// MVT::Other.
     MVT::SimpleValueType
       HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const;
+    
+    
+    /// FlattenAsmStringVariants - Flatten the specified AsmString to only
+    /// include text from the specified variant, returning the new string.
+    static std::string FlattenAsmStringVariants(StringRef AsmString,
+                                                unsigned Variant);
   };
 }
 





More information about the llvm-commits mailing list