[llvm-commits] [llvm] r73074 - in /llvm/trunk: docs/TableGenFundamentals.html test/TableGen/regmatch.td utils/TableGen/Record.cpp utils/TableGen/Record.h utils/TableGen/TGLexer.cpp utils/TableGen/TGLexer.h utils/TableGen/TGParser.cpp

David Greene greened at obbligato.org
Mon Jun 8 10:00:34 PDT 2009


Author: greened
Date: Mon Jun  8 12:00:34 2009
New Revision: 73074

URL: http://llvm.org/viewvc/llvm-project?rev=73074&view=rev
Log:

Add a !regmatch operator to do pattern matching in TableGen.

Added:
    llvm/trunk/test/TableGen/regmatch.td
Modified:
    llvm/trunk/docs/TableGenFundamentals.html
    llvm/trunk/utils/TableGen/Record.cpp
    llvm/trunk/utils/TableGen/Record.h
    llvm/trunk/utils/TableGen/TGLexer.cpp
    llvm/trunk/utils/TableGen/TGLexer.h
    llvm/trunk/utils/TableGen/TGParser.cpp

Modified: llvm/trunk/docs/TableGenFundamentals.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/TableGenFundamentals.html?rev=73074&r1=73073&r2=73074&view=diff

==============================================================================
--- llvm/trunk/docs/TableGenFundamentals.html (original)
+++ llvm/trunk/docs/TableGenFundamentals.html Mon Jun  8 12:00:34 2009
@@ -414,6 +414,9 @@
 <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>!regmatch(a, b)</tt></dt>
+  <dd>An integer {0,1} indicating whether string 'b' matched regular expression
+'a.'</dd>
 <dt><tt>!foreach(a, b, c)</tt></dt>
   <dd>For each member 'b' of dag or list 'a' apply operator 'c.'  'b' is a 
 dummy variable that should be declared as a member variable of an instantiated 

Added: llvm/trunk/test/TableGen/regmatch.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/regmatch.td?rev=73074&view=auto

==============================================================================
--- llvm/trunk/test/TableGen/regmatch.td (added)
+++ llvm/trunk/test/TableGen/regmatch.td Mon Jun  8 12:00:34 2009
@@ -0,0 +1,11 @@
+// RUN: tblgen %s | grep {Match1 = 1} | count 1
+// RUN: tblgen %s | grep {Match2 = 1} | count 1
+
+class Foo<string v> {
+      string Value = v;
+      int Match1 = !regmatch(".*ps$", v);
+      int Match2 = !regmatch(".*pd$", v);
+}
+
+def Bar : Foo<"addps">;
+def Baz : Foo<"addpd">;

Modified: llvm/trunk/utils/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.cpp?rev=73074&r1=73073&r2=73074&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/Record.cpp (original)
+++ llvm/trunk/utils/TableGen/Record.cpp Mon Jun  8 12:00:34 2009
@@ -16,6 +16,8 @@
 #include "llvm/Support/Streams.h"
 #include "llvm/ADT/StringExtras.h"
 #include <ios>
+#include <sys/types.h>
+#include <regex.h>
 
 using namespace llvm;
 
@@ -671,6 +673,36 @@
     }
     break;
   }
+  case REGMATCH: {
+    StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+    StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
+    if (LHSs && 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;
+      }
+      int result = regexec(&compiled, RHSs->getValue().c_str(), 0, NULL, 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);
+      return new IntInit(result == 0);
+    }    
+    break;
+  }
   case SHL:
   case SRA:
   case SRL: {
@@ -710,6 +742,7 @@
   case SRA: Result = "!sra"; break;
   case SRL: Result = "!srl"; break;
   case STRCONCAT: Result = "!strconcat"; break;
+  case REGMATCH: Result = "!regmatch"; break;
   case NAMECONCAT: 
     Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
   }

Modified: llvm/trunk/utils/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.h?rev=73074&r1=73073&r2=73074&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/Record.h (original)
+++ llvm/trunk/utils/TableGen/Record.h Mon Jun  8 12:00:34 2009
@@ -802,7 +802,7 @@
 ///
 class BinOpInit : public OpInit {
 public:
-  enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
+  enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT, REGMATCH };
 private:
   BinaryOp Opc;
   Init *LHS, *RHS;

Modified: llvm/trunk/utils/TableGen/TGLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGLexer.cpp?rev=73074&r1=73073&r2=73074&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGLexer.cpp (original)
+++ llvm/trunk/utils/TableGen/TGLexer.cpp Mon Jun  8 12:00:34 2009
@@ -447,6 +447,7 @@
   if (Len == 3  && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
   if (Len == 9  && !memcmp(Start, "strconcat", 9))   return tgtok::XStrConcat;
   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 == 7 && !memcmp(Start, "foreach", 7)) return tgtok::XForEach;
   if (Len == 4 && !memcmp(Start, "cast", 4)) return tgtok::XCast;

Modified: llvm/trunk/utils/TableGen/TGLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGLexer.h?rev=73074&r1=73073&r2=73074&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGLexer.h (original)
+++ llvm/trunk/utils/TableGen/TGLexer.h Mon Jun  8 12:00:34 2009
@@ -46,7 +46,7 @@
     
     // !keywords.
     XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
-    XForEach, XCar, XCdr, XNull, XIf,
+    XForEach, XCar, XCdr, XNull, XIf, XRegMatch,
 
     // Integer value.
     IntVal,

Modified: llvm/trunk/utils/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGParser.cpp?rev=73074&r1=73073&r2=73074&view=diff

==============================================================================
--- llvm/trunk/utils/TableGen/TGParser.cpp (original)
+++ llvm/trunk/utils/TableGen/TGParser.cpp Mon Jun  8 12:00:34 2009
@@ -791,6 +791,7 @@
   case tgtok::XSRL:
   case tgtok::XSHL:
   case tgtok::XStrConcat:
+  case tgtok::XRegMatch:
   case tgtok::XNameConcat: {  // Value ::= !binop '(' Value ',' Value ')'
     BinOpInit::BinaryOp Code;
     RecTy *Type = 0;
@@ -823,6 +824,11 @@
       Code = BinOpInit::STRCONCAT;
       Type = new StringRecTy();
       break;
+    case tgtok::XRegMatch:  
+      Lex.Lex();  // eat the operation
+      Code = BinOpInit::REGMATCH;
+      Type = new IntRecTy();
+      break;
     case tgtok::XNameConcat: 
       Lex.Lex();  // eat the operation
       Code = BinOpInit::NAMECONCAT;
@@ -1174,6 +1180,7 @@
   case tgtok::XSRL:
   case tgtok::XSHL:
   case tgtok::XStrConcat:
+  case tgtok::XRegMatch:
   case tgtok::XNameConcat:  // Value ::= !binop '(' Value ',' Value ')'
   case tgtok::XIf:
   case tgtok::XForEach:





More information about the llvm-commits mailing list