Add an !eq() operator to TableGen. It operates on strings only.
authorDavid Greene <greened@obbligato.org>
Tue, 5 Jan 2010 19:11:42 +0000 (19:11 +0000)
committerDavid Greene <greened@obbligato.org>
Tue, 5 Jan 2010 19:11:42 +0000 (19:11 +0000)
Use !cast<string>() to compare other types of objects.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92754 91177308-0d34-0410-b5e6-96231b3b80d8

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

index ade4bf67df5974439b51ce52f32b5aa624d37175..457bb62060dd9ecef2c145226e1ca2243aeee967 100644 (file)
@@ -423,6 +423,10 @@ class.  This operation is analogous to $(foreach) in GNU make.</dd>
   <dd>An integer {0,1} indicating whether list 'a' is empty.</dd>
 <dt><tt>!if(a,b,c)</tt></dt>
   <dd>'b' if the result of integer operator 'a' is nonzero, 'c' otherwise.</dd>
+<dt><tt>!eq(a,b)</tt></dt>
+  <dd>Integer one if string a is equal to string b, zero otherwise.  This
+      only operates on string objects.  Use !cast<string> to compare other
+      types of objects.</dd>
 </dl>
 
 <p>Note that all of the values have rules specifying how they convert to values
diff --git a/test/TableGen/eq.td b/test/TableGen/eq.td
new file mode 100644 (file)
index 0000000..8ba6d7e
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: tblgen %s | FileCheck %s
+// CHECK: Value = 0
+// CHECK: Value = 1
+
+class Base<int V> {
+  int Value = V;
+}
+
+class Derived<string Truth> :
+  Base<!if(!eq(Truth, "true"), 1, 0)>;
+
+def TRUE : Derived<"true">;
+def FALSE : Derived<"false">;
index 542735e88b48aaa7b8dd94154a90ad609de0eaa5..f9e2fe8a397a7056951f0080eeaa1f229d5e098b 100644 (file)
@@ -730,6 +730,15 @@ Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
     }
     break;
   }
+  case EQ: {
+    // Make sure we've resolved
+    StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+    StringInit *RHSs = dynamic_cast<StringInit*>(RHS);
+    if (LHSs && RHSs)
+      return new IntInit(LHSs->getValue() == RHSs->getValue());
+
+    break;
+  }
   case SHL:
   case SRA:
   case SRL: {
@@ -768,6 +777,7 @@ std::string BinOpInit::getAsString() const {
   case SHL: Result = "!shl"; break;
   case SRA: Result = "!sra"; break;
   case SRL: Result = "!srl"; break;
+  case EQ: Result = "!eq"; break;
   case STRCONCAT: Result = "!strconcat"; break;
   case NAMECONCAT:
     Result = "!nameconcat<" + getType()->getAsString() + ">"; break;
index 278c349dd65e5ee5152f2454a7eaf7e194a4cc42..45f3072ff0d21c4d0609f7026c6559a2d2ace6d6 100644 (file)
@@ -842,7 +842,7 @@ public:
 ///
 class BinOpInit : public OpInit {
 public:
-  enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
+  enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT, EQ };
 private:
   BinaryOp Opc;
   Init *LHS, *RHS;
index 4498e305b02879a550799ab4d4243bed8a07146f..2c7becc71824d3c4d58824296b224b393f7f8b80 100644 (file)
@@ -434,6 +434,7 @@ tgtok::TokKind TGLexer::LexExclaim() {
   if (Len == 3  && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
   if (Len == 3  && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
   if (Len == 3  && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
+  if (Len == 2  && !memcmp(Start, "eq", 2)) return tgtok::XEq;
   if (Len == 9  && !memcmp(Start, "strconcat", 9))   return tgtok::XStrConcat;
   if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
   if (Len == 5 && !memcmp(Start, "subst", 5)) return tgtok::XSubst;
index 679020880dd3712b9fd87636382940d5b922e3b4..835f351d3d0dd45dcb74b16347cf9dee48cac24b 100644 (file)
@@ -45,7 +45,7 @@ namespace tgtok {
     
     // !keywords.
     XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
-    XForEach, XCar, XCdr, XNull, XIf,
+    XForEach, XCar, XCdr, XNull, XIf, XEq,
 
     // Integer value.
     IntVal,
index b095a6c30148a2497d7f05090c2a21fac409e700..8c158e0e4ee85b0c3d5cbeb680c90b966ffc1d0b 100644 (file)
@@ -792,6 +792,7 @@ Init *TGParser::ParseOperation(Record *CurRec) {
   case tgtok::XSRA:
   case tgtok::XSRL:
   case tgtok::XSHL:
+  case tgtok::XEq:
   case tgtok::XStrConcat:
   case tgtok::XNameConcat: {  // Value ::= !binop '(' Value ',' Value ')'
     BinOpInit::BinaryOp Code;
@@ -820,6 +821,11 @@ Init *TGParser::ParseOperation(Record *CurRec) {
       Code = BinOpInit::SHL;
       Type = new IntRecTy();
       break;
+    case tgtok::XEq:  
+      Lex.Lex();  // eat the operation
+      Code = BinOpInit::EQ;
+      Type = new IntRecTy();
+      break;
     case tgtok::XStrConcat:
       Lex.Lex();  // eat the operation
       Code = BinOpInit::STRCONCAT;
@@ -1257,6 +1263,7 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
   case tgtok::XSRA:
   case tgtok::XSRL:
   case tgtok::XSHL:
+  case tgtok::XEq:
   case tgtok::XStrConcat:
   case tgtok::XNameConcat:  // Value ::= !binop '(' Value ',' Value ')'
   case tgtok::XIf: