[llvm-commits] [llvm] r71815 - in /llvm/trunk: docs/TableGenFundamentals.html test/TableGen/if.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
Thu May 14 16:26:51 PDT 2009
Author: greened
Date: Thu May 14 18:26:46 2009
New Revision: 71815
URL: http://llvm.org/viewvc/llvm-project?rev=71815&view=rev
Log:
Implement !if, analogous to $(if) in GNU make.
Added:
llvm/trunk/test/TableGen/if.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=71815&r1=71814&r2=71815&view=diff
==============================================================================
--- llvm/trunk/docs/TableGenFundamentals.html (original)
+++ llvm/trunk/docs/TableGenFundamentals.html Thu May 14 18:26:46 2009
@@ -417,6 +417,8 @@
<dd>The 2nd-N elements of list 'a.'</dd>
<dt><tt>!null(a)</tt></dt>
<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>
</dl>
<p>Note that all of the values have rules specifying how they convert to values
Added: llvm/trunk/test/TableGen/if.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/TableGen/if.td?rev=71815&view=auto
==============================================================================
--- llvm/trunk/test/TableGen/if.td (added)
+++ llvm/trunk/test/TableGen/if.td Thu May 14 18:26:46 2009
@@ -0,0 +1,20 @@
+// RUN: tblgen %s | grep {1, 2, 3} | count 4
+// RUN: tblgen %s | grep {4, 5, 6} | count 2
+
+class A<list<list<int>> vals> {
+ list<int> first = vals[0];
+ list<int> rest = !if(!null(!cdr(vals)), vals[0], vals[1]);
+}
+
+def One : A<[[1,2,3]]>;
+def Two : A<[[1,2,3],[4,5,6]]>;
+
+class B<list<int> v> {
+ list<int> vals = v;
+}
+
+class BB<list<list<int>> vals> : B<!if(!null(!cdr(vals)), vals[0], vals[1])>;
+class BBB<list<list<int>> vals> : BB<vals>;
+
+def OneB : BBB<[[1,2,3]]>;
+def TwoB : BBB<[[1,2,3],[4,5,6]]>;
Modified: llvm/trunk/utils/TableGen/Record.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.cpp?rev=71815&r1=71814&r2=71815&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/Record.cpp (original)
+++ llvm/trunk/utils/TableGen/Record.cpp Thu May 14 18:26:46 2009
@@ -912,6 +912,19 @@
}
break;
}
+
+ case IF: {
+ IntInit *LHSi = dynamic_cast<IntInit*>(LHS);
+ if (LHSi) {
+ if (LHSi->getValue()) {
+ return MHS;
+ }
+ else {
+ return RHS;
+ }
+ }
+ break;
+ }
}
return this;
@@ -932,6 +945,7 @@
switch (Opc) {
case SUBST: Result = "!subst"; break;
case FOREACH: Result = "!foreach"; break;
+ case IF: Result = "!if"; break;
}
return Result + "(" + LHS->getAsString() + ", " + MHS->getAsString() + ", "
+ RHS->getAsString() + ")";
Modified: llvm/trunk/utils/TableGen/Record.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.h?rev=71815&r1=71814&r2=71815&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/Record.h (original)
+++ llvm/trunk/utils/TableGen/Record.h Thu May 14 18:26:46 2009
@@ -843,7 +843,7 @@
///
class TernOpInit : public OpInit {
public:
- enum TernaryOp { SUBST, FOREACH };
+ enum TernaryOp { SUBST, FOREACH, IF };
private:
TernaryOp Opc;
Init *LHS, *MHS, *RHS;
Modified: llvm/trunk/utils/TableGen/TGLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGLexer.cpp?rev=71815&r1=71814&r2=71815&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/TGLexer.cpp (original)
+++ llvm/trunk/utils/TableGen/TGLexer.cpp Thu May 14 18:26:46 2009
@@ -453,6 +453,7 @@
if (Len == 3 && !memcmp(Start, "car", 3)) return tgtok::XCar;
if (Len == 3 && !memcmp(Start, "cdr", 3)) return tgtok::XCdr;
if (Len == 4 && !memcmp(Start, "null", 4)) return tgtok::XNull;
+ if (Len == 2 && !memcmp(Start, "if", 2)) return tgtok::XIf;
return ReturnError(Start-1, "Unknown operator");
}
Modified: llvm/trunk/utils/TableGen/TGLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGLexer.h?rev=71815&r1=71814&r2=71815&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/TGLexer.h (original)
+++ llvm/trunk/utils/TableGen/TGLexer.h Thu May 14 18:26:46 2009
@@ -46,7 +46,7 @@
// !keywords.
XConcat, XSRA, XSRL, XSHL, XStrConcat, XNameConcat, XCast, XSubst,
- XForEach, XCar, XCdr, XNull,
+ XForEach, XCar, XCdr, XNull, XIf,
// Integer value.
IntVal,
Modified: llvm/trunk/utils/TableGen/TGParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/TGParser.cpp?rev=71815&r1=71814&r2=71815&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/TGParser.cpp (original)
+++ llvm/trunk/utils/TableGen/TGParser.cpp Thu May 14 18:26:46 2009
@@ -862,6 +862,7 @@
return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
}
+ case tgtok::XIf:
case tgtok::XForEach:
case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
TernOpInit::TernaryOp Code;
@@ -872,6 +873,9 @@
Lex.Lex(); // eat the operation
switch (LexCode) {
default: assert(0 && "Unhandled code!");
+ case tgtok::XIf:
+ Code = TernOpInit::IF;
+ break;
case tgtok::XForEach:
Code = TernOpInit::FOREACH;
break;
@@ -914,6 +918,25 @@
switch (LexCode) {
default: assert(0 && "Unhandled code!");
+ case tgtok::XIf: {
+ TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
+ TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
+ if (MHSt == 0 || RHSt == 0) {
+ TokError("could not get type for !if");
+ return 0;
+ }
+ if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
+ Type = RHSt->getType();
+ }
+ else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
+ Type = MHSt->getType();
+ }
+ else {
+ TokError("inconsistent types for !if");
+ return 0;
+ }
+ break;
+ }
case tgtok::XForEach: {
TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
if (MHSt == 0) {
@@ -1152,6 +1175,7 @@
case tgtok::XSHL:
case tgtok::XStrConcat:
case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
+ case tgtok::XIf:
case tgtok::XForEach:
case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
return ParseOperation(CurRec);
More information about the llvm-commits
mailing list