[PATCH] D68453: TableGen: Allow 'a+b' in TableGen language

Javed Absar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 03:16:58 PDT 2019


javed.absar created this revision.
javed.absar added reviewers: lebedev.ri, arsenm, RKSimon.
Herald added subscribers: hiraditya, wdng.
Herald added a project: LLVM.

!add(a,b) is often used in td files but its bit cumbersome.
 This patch allows one to write 'a+b'. !add(a,b) will still work.


https://reviews.llvm.org/D68453

Files:
  llvm/lib/TableGen/TGParser.cpp
  llvm/test/TableGen/infix-add.td


Index: llvm/test/TableGen/infix-add.td
===================================================================
--- /dev/null
+++ llvm/test/TableGen/infix-add.td
@@ -0,0 +1,28 @@
+// RUN: llvm-tblgen %s | FileCheck %s
+// XFAIL: vg_leak
+
+class Int<int v> {
+  int Val = v;
+}
+
+// CHECK: def AA1 {
+// CHECK-NEXT: int Val = 1;
+// CHECK: def AB1 {
+// CHECK-NEXT: int Val = 11;
+foreach Index = 1-1 in {
+  def AA#Index  : Int<Index>;
+  def AB#Index  : Int<Index + 10>;
+}
+
+
+// CHECK: def I20 {
+// CHECK-NEXT : int  Val = 20;
+def I20 : Int<20>;
+
+// CHECK: def I21 {
+// CHECK-NEXT : int  Val = 21;
+def I21 : Int<!if(!eq(I20.Val,20), I20.Val + 1, 22)>;
+
+// CHECK: def I300 {
+// CHECK-NEXT: int Val = 300;
+def I300 : Int<155 + 145>;
Index: llvm/lib/TableGen/TGParser.cpp
===================================================================
--- llvm/lib/TableGen/TGParser.cpp
+++ llvm/lib/TableGen/TGParser.cpp
@@ -2173,6 +2173,20 @@
       break;
     }
 
+    case tgtok::plus: {
+      SMLoc PlusLoc = Lex.getLoc();
+      TypedInit *LHS = dyn_cast<TypedInit>(Result);
+      if (!LHS) {
+        Error(PlusLoc, "LHS of '+' is not typed!");
+        return nullptr;
+      }
+      Lex.Lex(); // Eat the '+'
+      Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
+      Result = BinOpInit::get(BinOpInit::ADD, LHS, RHSResult,
+                                    IntRecTy::get())->Fold(CurRec);
+      break;
+    }
+
     case tgtok::paste:
       SMLoc PasteLoc = Lex.getLoc();
       TypedInit *LHS = dyn_cast<TypedInit>(Result);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68453.223180.patch
Type: text/x-patch
Size: 1558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191004/7c694d84/attachment.bin>


More information about the llvm-commits mailing list