Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ImplicitCastTest.java @ 12392:4e26955b6da2
Truffle-DSL: new implicit cast code generation layout supports now executeWith.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Thu, 03 Oct 2013 18:07:59 +0200 |
parents | 7aa2a8c69ba3 |
children | 7cce548b0b60 |
comparison
equal
deleted
inserted
replaced
12391:7aa2a8c69ba3 | 12392:4e26955b6da2 |
---|---|
24 | 24 |
25 import org.junit.*; | 25 import org.junit.*; |
26 | 26 |
27 import com.oracle.truffle.api.dsl.*; | 27 import com.oracle.truffle.api.dsl.*; |
28 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast0NodeFactory; | 28 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast0NodeFactory; |
29 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast1NodeFactory; | |
30 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast2NodeFactory; | |
29 import com.oracle.truffle.api.dsl.test.NodeContainerTest.Str; | 31 import com.oracle.truffle.api.dsl.test.NodeContainerTest.Str; |
30 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | 32 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; |
31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | 33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; |
32 import com.oracle.truffle.api.frame.*; | 34 import com.oracle.truffle.api.frame.*; |
33 | 35 |
52 @NodeChild(value = "operand", type = ImplicitCast0Node.class) | 54 @NodeChild(value = "operand", type = ImplicitCast0Node.class) |
53 abstract static class ImplicitCast0Node extends ValueNode { | 55 abstract static class ImplicitCast0Node extends ValueNode { |
54 | 56 |
55 public abstract Object executeEvaluated(VirtualFrame frame, Object value2); | 57 public abstract Object executeEvaluated(VirtualFrame frame, Object value2); |
56 | 58 |
57 @Specialization | 59 @Specialization(order = 1) |
58 public String op1(String value) { | 60 public String op1(String value) { |
59 return value; | 61 return value; |
60 } | 62 } |
61 | 63 |
62 @Specialization | 64 @Specialization(order = 2) |
63 public boolean op1(boolean value) { | 65 public boolean op1(boolean value) { |
64 return value; | 66 return value; |
65 } | 67 } |
66 | 68 |
67 } | 69 } |
75 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); | 77 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); |
76 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | 78 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); |
77 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); | 79 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); |
78 } | 80 } |
79 | 81 |
80 // @TypeSystemReference(ImplicitCast0Types.class) | 82 @TypeSystemReference(ImplicitCast0Types.class) |
81 // @NodeChild(value = "operand", type = ImplicitCast0Node.class) | 83 @NodeChild(value = "operand", type = ImplicitCast1Node.class) |
82 // abstract static class ImplicitCast1Node extends ValueNode { | 84 // TODO temporary workaround |
83 // | 85 @PolymorphicLimit(1) |
84 // @Specialization | 86 abstract static class ImplicitCast1Node extends ValueNode { |
85 // public String op0(String value) { | |
86 // return value; | |
87 // } | |
88 // | |
89 // @Specialization(order = 1, rewriteOn = RuntimeException.class) | |
90 // public boolean op1(@SuppressWarnings("unused") boolean value) throws RuntimeException { | |
91 // throw new RuntimeException(); | |
92 // } | |
93 // | |
94 // @Specialization(order = 2) | |
95 // public boolean op2(boolean value) { | |
96 // return value; | |
97 // } | |
98 // | |
99 // } | |
100 // | |
101 // @Test | |
102 // public void testImplicitCast1() { | |
103 // ImplicitCast0Node node = ImplicitCast0NodeFactory.create(null); | |
104 // TestRootNode<ImplicitCast0Node> root = new TestRootNode<>(node); | |
105 // Assert.assertEquals("2", root.getNode().executeEvaluated(null, "2")); | |
106 // Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
107 // Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); | |
108 // Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
109 // Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); | |
110 // } | |
111 | 87 |
112 // TODO assert implicit casts only in one direction | 88 public abstract Object executeEvaluated(VirtualFrame frame, Object operand); |
113 | 89 |
114 // test example that covers the most cases | 90 @Specialization(order = 0) |
91 public String op0(String value) { | |
92 return value; | |
93 } | |
94 | |
95 @Specialization(order = 1, rewriteOn = RuntimeException.class) | |
96 public boolean op1(@SuppressWarnings("unused") boolean value) throws RuntimeException { | |
97 throw new RuntimeException(); | |
98 } | |
99 | |
100 @Specialization(order = 2) | |
101 public boolean op2(boolean value) { | |
102 return value; | |
103 } | |
104 | |
105 } | |
106 | |
107 @Test | |
108 public void testImplicitCast1() { | |
109 ImplicitCast1Node node = ImplicitCast1NodeFactory.create(null); | |
110 TestRootNode<ImplicitCast1Node> root = new TestRootNode<>(node); | |
111 Assert.assertEquals("2", root.getNode().executeEvaluated(null, "2")); | |
112 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
113 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); | |
114 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
115 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); | |
116 } | |
117 | |
118 @TypeSystemReference(ImplicitCast0Types.class) | |
119 @NodeChildren({@NodeChild(value = "operand0", type = ImplicitCast2Node.class), @NodeChild(value = "operand1", type = ImplicitCast2Node.class, executeWith = "operand0")}) | |
120 // TODO temporary workaround | |
121 @PolymorphicLimit(1) | |
122 abstract static class ImplicitCast2Node extends ValueNode { | |
123 | |
124 @Specialization(order = 0) | |
125 public String op0(String v0, String v1) { | |
126 return v0 + v1; | |
127 } | |
128 | |
129 @SuppressWarnings("unused") | |
130 @Specialization(order = 1, rewriteOn = RuntimeException.class) | |
131 public boolean op1(boolean v0, boolean v1) throws RuntimeException { | |
132 throw new RuntimeException(); | |
133 } | |
134 | |
135 @Specialization(order = 2) | |
136 public boolean op2(boolean v0, boolean v1) { | |
137 return v0 && v1; | |
138 } | |
139 | |
140 public abstract Object executeEvaluated(VirtualFrame frame, Object v1); | |
141 | |
142 public abstract Object executeEvaluated(VirtualFrame frame, Object v1, Object v2); | |
143 | |
144 public abstract Object executeEvaluated(VirtualFrame frame, boolean v1, boolean v2); | |
145 | |
146 } | |
147 | |
148 @Test | |
149 public void testImplicitCast2() { | |
150 ImplicitCast2Node node = ImplicitCast2NodeFactory.create(null, null); | |
151 TestRootNode<ImplicitCast2Node> root = new TestRootNode<>(node); | |
152 Assert.assertEquals("2", root.getNode().executeEvaluated(null, "2")); | |
153 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
154 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); | |
155 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
156 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); | |
157 } | |
115 | 158 |
116 } | 159 } |