Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ExecuteGroupingTest.java @ 20940:476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 14 Apr 2015 15:12:48 +0200 |
parents | |
children | 6361fa2e3321 |
comparison
equal
deleted
inserted
replaced
20939:f83fd99b2962 | 20940:476374f3fe9a |
---|---|
1 /* | |
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.dsl.test; | |
24 | |
25 import org.junit.*; | |
26 import org.junit.experimental.theories.*; | |
27 import org.junit.runner.*; | |
28 import org.junit.runners.Parameterized.Parameters; | |
29 | |
30 import static org.junit.Assert.*; | |
31 | |
32 import com.oracle.truffle.api.dsl.*; | |
33 import com.oracle.truffle.api.dsl.test.ExecuteGroupingTestFactory.ExecuteGrouping1NodeGen; | |
34 import com.oracle.truffle.api.frame.*; | |
35 import com.oracle.truffle.api.nodes.*; | |
36 | |
37 /* | |
38 * This test aims to test the reuse of execute methods with evaluated parameters as much as possible. | |
39 */ | |
40 @SuppressWarnings("unused") | |
41 @RunWith(Theories.class) | |
42 public class ExecuteGroupingTest { | |
43 | |
44 @DataPoints public static final Object[] parameters = new Object[]{1, 2}; | |
45 | |
46 static final class ExecuteGroupingChild extends Node { | |
47 | |
48 int invocationCount = 0; | |
49 | |
50 private final Object returnValue; | |
51 | |
52 public ExecuteGroupingChild(Object returnValue) { | |
53 this.returnValue = returnValue; | |
54 } | |
55 | |
56 Object execute() { | |
57 invocationCount++; | |
58 return returnValue; | |
59 } | |
60 | |
61 } | |
62 | |
63 @Theory | |
64 public void testExecuteGrouping1Node(Object a, Object b, Object c) throws UnexpectedResultException { | |
65 ExecuteGroupingChild child0 = new ExecuteGroupingChild(a); | |
66 ExecuteGroupingChild child1 = new ExecuteGroupingChild(b); | |
67 ExecuteGroupingChild child2 = new ExecuteGroupingChild(c); | |
68 | |
69 int result = ((int) a) + ((int) b) + ((int) c); | |
70 | |
71 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(child0, child1, child2)).execute()); | |
72 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(child0, child1, child2)).execute((VirtualFrame) null)); | |
73 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, child1, child2)).execute(a)); | |
74 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, child1, child2)).executeInt(a)); | |
75 | |
76 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute(a, b)); | |
77 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute((int) a, b)); | |
78 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute(a, (int) b)); | |
79 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute((int) a, (int) b)); | |
80 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).executeInt((int) a, (int) b)); | |
81 | |
82 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, null)).execute(a, b, c)); | |
83 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, null)).execute((int) a, (int) b, c)); | |
84 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, null)).execute((int) a, (int) b, (int) c)); | |
85 | |
86 } | |
87 | |
88 @NodeChildren({@NodeChild(type = ExecuteGroupingChild.class), @NodeChild(type = ExecuteGroupingChild.class), @NodeChild(type = ExecuteGroupingChild.class)}) | |
89 abstract static class ExecuteGrouping1Node extends Node { | |
90 | |
91 abstract Object execute(); | |
92 | |
93 int executeInt() throws UnexpectedResultException { | |
94 Object value = execute(); | |
95 if (value instanceof Integer) { | |
96 return (int) value; | |
97 } | |
98 throw new UnexpectedResultException(value); | |
99 } | |
100 | |
101 abstract double executeDouble() throws UnexpectedResultException; | |
102 | |
103 abstract Object execute(VirtualFrame frame); | |
104 | |
105 abstract Object execute(Object o1); | |
106 | |
107 abstract int executeInt(Object o1) throws UnexpectedResultException; | |
108 | |
109 abstract Object execute(Object o1, Object o2); | |
110 | |
111 abstract Object execute(int o1, int o2); | |
112 | |
113 abstract Object execute(int o1, int o2, Object o3); | |
114 | |
115 abstract int executeInt(int o1, int o2) throws UnexpectedResultException; | |
116 | |
117 abstract Object execute(Object o1, int o2); | |
118 | |
119 abstract Object execute(int o1, Object o2); | |
120 | |
121 abstract Object execute(Object o1, Object o2, Object o3); | |
122 | |
123 abstract Object execute(int o1, int o2, int o3); | |
124 | |
125 @Specialization | |
126 int s1(int a, int b, int c) { | |
127 return a + b + c; | |
128 } | |
129 | |
130 @Specialization | |
131 int s2(Object a, Object b, Object c) { | |
132 return ((int) a) + ((int) b) + ((int) c); | |
133 } | |
134 | |
135 } | |
136 | |
137 abstract static class StrangeReturnCase extends Node { | |
138 | |
139 // we don't know how to implement executeDouble | |
140 public abstract double executeDouble(); | |
141 | |
142 public int executeInt() { | |
143 return 42; | |
144 } | |
145 | |
146 @Specialization(rewriteOn = RuntimeException.class) | |
147 int s1() { | |
148 return 42; | |
149 } | |
150 | |
151 @Specialization | |
152 int s2() { | |
153 return 42; | |
154 } | |
155 | |
156 } | |
157 | |
158 @ExpectError("Incompatible abstract execute methods found [executeDouble(), executeInt()].%") | |
159 abstract static class IncompatibleAbstract1 extends Node { | |
160 | |
161 // we don't know how to implement executeDouble | |
162 abstract double executeDouble(); | |
163 | |
164 abstract int executeInt(); | |
165 | |
166 @Specialization | |
167 int s1() { | |
168 return 42; | |
169 } | |
170 | |
171 } | |
172 | |
173 abstract static class IncompatibleAbstract2 extends Node { | |
174 | |
175 abstract double executeDouble(); | |
176 | |
177 // we can resolve duplicate path errors by making an execute method final | |
178 @SuppressWarnings("static-method") | |
179 public final int executeInt() { | |
180 return 42; | |
181 } | |
182 | |
183 @ExpectError("The provided return type \"int\" does not match expected return type \"double\".%") | |
184 @Specialization(rewriteOn = RuntimeException.class) | |
185 int s1() { | |
186 return 42; | |
187 } | |
188 | |
189 @Specialization | |
190 double s2() { | |
191 return 42; | |
192 } | |
193 | |
194 } | |
195 | |
196 } |