Add a !patsubst operator. Use on string types.
authorDavid Greene <greened@obbligato.org>
Mon, 8 Jun 2009 23:05:37 +0000 (23:05 +0000)
committerDavid Greene <greened@obbligato.org>
Mon, 8 Jun 2009 23:05:37 +0000 (23:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73099 91177308-0d34-0410-b5e6-96231b3b80d8

docs/TableGenFundamentals.html
test/TableGen/patsubst.td [new file with mode: 0644]
utils/TableGen/CodeGenDAGPatterns.cpp
utils/TableGen/Record.cpp
utils/TableGen/Record.h
utils/TableGen/TGLexer.cpp
utils/TableGen/TGLexer.h
utils/TableGen/TGParser.cpp

index 147b5be3c431cfc26f42bdd2fbbf432162ff6d69..5d64a0895295438d8ec25e53127fd98543b3ddeb 100644 (file)
@@ -417,6 +417,10 @@ aborts with an error. </dd>
 <dt><tt>!subst(a, b, c)</tt></dt>
   <dd>If 'a' and 'b' are of string type or are symbol references, substitute 
 'b' for 'a' in 'c.'  This operation is analogous to $(subst) in GNU make.</dd>
+<dt><tt>!patsubst(a, b, c)</tt></dt>
+  <dd>patch regular expression 'a' against string 'c' and substitute string 'b' on 
+a match.  'b' may contain placeholders of the form $&lt;digit&gt;, where 
+&lt;digit&gt; is a number 1-9.</dd>
 <dt><tt>!regmatch(a, b)</tt></dt>
   <dd>An integer {0,1} indicating whether string 'b' matched regular expression
 'a.'</dd>
diff --git a/test/TableGen/patsubst.td b/test/TableGen/patsubst.td
new file mode 100644 (file)
index 0000000..0a7b3d8
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: tblgen %s | grep {Match1 = "v4f32"} | count 1
+// RUN: tblgen %s | grep {Match2 = "v2f64"} | count 1
+// RUN: tblgen %s | grep {Match3 = "v4f32 add"} | count 1
+// RUN: tblgen %s | grep {Match4 = "v2f64 add"} | count 1
+
+class Foo<string v> {
+      string Value = v;
+      string Match1 = !patsubst(".*ps$", "v4f32", v);
+      string Match2 = !patsubst(".*pd$", "v2f64", v);
+      string Match3 = !patsubst("(.*)ps$", "v4f32 $1", v);
+      string Match4 = !patsubst("(.*)pd$", "v2f64 $1", v);
+}
+
+def Bar : Foo<"addps">;
+def Baz : Foo<"addpd">;
index 05bbc0a7fdb007b689e93e5d7e195f794816b89c..e668468772c644b01926b35791eca56b4c527385 100644 (file)
@@ -2025,7 +2025,7 @@ void CodeGenDAGPatterns::ParsePatterns() {
           }
         }
         else {
-          ListTy - TArg->getType();
+          ListTy = TArg->getType();
         }
       }
       ListInit *LI = new ListInit(Values, new ListRecTy(ListTy));
index fa10799622cd421fdc630e5fdd0d884d29388e87..2f500ae5c4f64756758001bf1b7e32270223a458 100644 (file)
@@ -18,6 +18,7 @@
 #include <ios>
 #include <sys/types.h>
 #include <regex.h>
+#include <sstream>
 
 using namespace llvm;
 
@@ -1034,6 +1035,69 @@ Init *TernOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
     }
     break;
   }
+
+  case PATSUBST: {
+    StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+    StringInit *MHSs = dynamic_cast<StringInit*>(MHS);
+    StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
+
+    if (LHSs && MHSs && RHSs) {
+      regex_t compiled;
+      int err = regcomp (&compiled, LHSs->getValue().c_str(), REG_EXTENDED);
+      if (err != 0) {
+        size_t length = regerror (err, &compiled, NULL, 0);
+        char *buffer = new char[length];
+        (void) regerror (err, &compiled, buffer, length);
+        std::string errmsg = buffer;
+        delete[] buffer;
+        regfree(&compiled);
+        throw errmsg;
+      }
+      regmatch_t matches[10];
+      int result = regexec(&compiled, RHSs->getValue().c_str(), 10, matches, 0);
+      if (result == REG_ESPACE) {
+        size_t length = regerror (err, &compiled, NULL, 0);
+        char *buffer = new char[length];
+        (void) regerror (err, &compiled, buffer, length);
+        std::string errmsg = buffer;
+        delete[] buffer;
+        regfree(&compiled);
+        throw errmsg;
+      }
+      regfree(&compiled);
+      if (result == 0) {
+        // Parse the substitution string looking for $1, $2, etc. and
+        // substitute strings.  If there are no $1, etc. just replace
+        // the whole string.
+        std::string replacement = MHSs->getValue();
+        size_t pos = replacement.find("$");
+        while (pos != std::string::npos && pos+1 < replacement.size()) {
+          if (std::isdigit(replacement[pos+1])) {
+            std::string sidx(&replacement[pos+1], 1);
+            std::istringstream str(sidx);
+            int idx;
+            if (str >> idx) {              
+              replacement.replace(pos, 2, RHSs->getValue(), matches[idx].rm_so,
+                                  matches[idx].rm_eo - matches[idx].rm_so);
+            }
+            else {
+              throw "unexpected failure in patsubst index calculation";
+            }
+          }
+          else if (replacement[pos+1] == '$') {
+            replacement.replace(pos, 2, "$");
+          }
+          pos = replacement.find("$", pos+1);
+        }
+        return new StringInit(replacement);
+      }
+      else {
+        // No match, just pass the string through
+        return RHSs;
+      }
+    }
+    break;
+  }  
   }
 
   return this;
@@ -1069,6 +1133,7 @@ std::string TernOpInit::getAsString() const {
   std::string Result;
   switch (Opc) {
   case SUBST: Result = "!subst"; break;
+  case PATSUBST: Result = "!patsubst"; break;
   case FOREACH: Result = "!foreach"; break; 
   case IF: Result = "!if"; break; 
  }
index 8ef833d554d9f9ad1f52d4784bcf9a10d8468c2a..d8e4fb34b689be59cb2cf04008a664e5032bcaf6 100644 (file)
@@ -885,7 +885,7 @@ public:
 ///
 class TernOpInit : public OpInit {
 public:
-  enum TernaryOp { SUBST, FOREACH, IF };
+  enum TernaryOp { SUBST, FOREACH, IF, PATSUBST };
 private:
   TernaryOp Opc;
   Init *LHS, *MHS, *RHS;
index 378bffb3d2eea0803a5d12d136347ca2f3cab7c9..930d9dba5bd40b02a512be195ff48ecb27759ef2 100644 (file)
@@ -449,6 +449,7 @@ tgtok::TokKind TGLexer::LexExclaim() {
   if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
   if (Len == 8 && !memcmp(Start, "regmatch", 8)) return tgtok::XRegMatch;
   if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
+  if (Len == 8 && !memcmp(Start, "patsubst", 8)) return tgtok::XPatSubst;
   if (Len == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
   if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;
   if (Len == 3 && !memcmp(Start, "car", 3)) return tgtok::XCar;
index 06f6535949ec2022841be6b65941b36b78826d95..fef10f0c455a13734d57f1d2cb159a8210ecbec5 100644 (file)
@@ -46,7 +46,7 @@ namespace tgtok {
     
     // !keywords.
     XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
-    XForEach, XCar, XCdr, XNull, XIf, XRegMatch,
+    XForEach, XCar, XCdr, XNull, XIf, XRegMatch, XPatSubst,
 
     // Integer value.
     IntVal,
index 7d3d1b37a944486b047dcc121ede886650777c80..d2bc6b853dc055a5e937cb2c21ec66b8935b8cf1 100644 (file)
@@ -878,6 +878,7 @@ Init *TGParser::ParseOperation(Record *CurRec) {
 
   case tgtok::XIf:
   case tgtok::XForEach:
+  case tgtok::XPatSubst:
   case tgtok::XSubst: {  // Value ::= !ternop '(' Value ',' Value ',' Value ')'
     TernOpInit::TernaryOp Code;
     RecTy *Type = 0;
@@ -896,6 +897,9 @@ Init *TGParser::ParseOperation(Record *CurRec) {
     case tgtok::XSubst:
       Code = TernOpInit::SUBST;
       break;
+    case tgtok::XPatSubst:
+      Code = TernOpInit::PATSUBST;
+      break;
     }
     if (Lex.getCode() != tgtok::l_paren) {
       TokError("expected '(' after ternary operator");
@@ -969,6 +973,10 @@ Init *TGParser::ParseOperation(Record *CurRec) {
       Type = RHSt->getType();
       break;
     }
+    case tgtok::XPatSubst: {
+      Type = new StringRecTy;
+      break;
+    }      
     }
     return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
   }
@@ -1277,6 +1285,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
   case tgtok::XNameConcat:  // Value ::= !binop '(' Value ',' Value ')'
   case tgtok::XIf:
   case tgtok::XForEach:
+  case tgtok::XPatSubst:
   case tgtok::XSubst: {  // Value ::= !ternop '(' Value ',' Value ',' Value ')'
     return ParseOperation(CurRec);
     break;