Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/CachedTest.java @ 19289:62c43fcf5be2
Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 03 Feb 2015 15:07:07 +0100 |
parents | |
children | 4cba24bef2ee |
comparison
equal
deleted
inserted
replaced
19288:3a37116ef37f | 19289:62c43fcf5be2 |
---|---|
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 static com.oracle.truffle.api.dsl.test.TestHelper.*; | |
26 import static org.junit.Assert.*; | |
27 | |
28 import org.junit.*; | |
29 | |
30 import com.oracle.truffle.api.*; | |
31 import com.oracle.truffle.api.dsl.*; | |
32 import com.oracle.truffle.api.dsl.test.CachedTestFactory.BoundCacheFactory; | |
33 import com.oracle.truffle.api.dsl.test.CachedTestFactory.BoundCacheOverflowFactory; | |
34 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestBoundCacheOverflowContainsFactory; | |
35 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheFieldFactory; | |
36 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheMethodFactory; | |
37 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheNodeFieldFactory; | |
38 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestGuardWithCachedAndDynamicParameterFactory; | |
39 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestGuardWithJustCachedParameterFactory; | |
40 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestMultipleCachesFactory; | |
41 import com.oracle.truffle.api.dsl.test.CachedTestFactory.UnboundCacheFactory; | |
42 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
43 | |
44 @SuppressWarnings("unused") | |
45 public class CachedTest { | |
46 | |
47 @Test | |
48 public void testUnboundCache() { | |
49 CallTarget root = createCallTarget(UnboundCacheFactory.getInstance()); | |
50 assertEquals(42, root.call(42)); | |
51 assertEquals(42, root.call(43)); | |
52 assertEquals(42, root.call(44)); | |
53 } | |
54 | |
55 @NodeChild | |
56 static class UnboundCache extends ValueNode { | |
57 @Specialization | |
58 static int do1(int value, @Cached("value") int cachedValue) { | |
59 return cachedValue; | |
60 } | |
61 } | |
62 | |
63 @Test | |
64 public void testBoundCache() { | |
65 CallTarget root = createCallTarget(BoundCacheFactory.getInstance()); | |
66 assertEquals(42, root.call(42)); | |
67 assertEquals(43, root.call(43)); | |
68 assertEquals(44, root.call(44)); | |
69 try { | |
70 root.call(45); | |
71 fail(); | |
72 } catch (UnsupportedSpecializationException e) { | |
73 } | |
74 } | |
75 | |
76 @NodeChild | |
77 static class BoundCache extends ValueNode { | |
78 | |
79 @Specialization(guards = "value == cachedValue", limit = "3") | |
80 static int do1(int value, @Cached("value") int cachedValue) { | |
81 return cachedValue; | |
82 } | |
83 | |
84 } | |
85 | |
86 @Test | |
87 public void testBoundCacheOverflow() { | |
88 CallTarget root = createCallTarget(BoundCacheOverflowFactory.getInstance()); | |
89 assertEquals(42, root.call(42)); | |
90 assertEquals(43, root.call(43)); | |
91 assertEquals(-1, root.call(44)); | |
92 assertEquals(42, root.call(42)); | |
93 assertEquals(43, root.call(43)); | |
94 assertEquals(-1, root.call(44)); | |
95 } | |
96 | |
97 @NodeChild | |
98 static class BoundCacheOverflow extends ValueNode { | |
99 | |
100 @Specialization(guards = "value == cachedValue", limit = "2") | |
101 static int do1(int value, @Cached("value") int cachedValue) { | |
102 return cachedValue; | |
103 } | |
104 | |
105 @Specialization | |
106 static int do2(int value) { | |
107 return -1; | |
108 } | |
109 | |
110 } | |
111 | |
112 @Test | |
113 public void testBoundCacheOverflowContains() { | |
114 CallTarget root = createCallTarget(TestBoundCacheOverflowContainsFactory.getInstance()); | |
115 assertEquals(42, root.call(42)); | |
116 assertEquals(43, root.call(43)); | |
117 assertEquals(-1, root.call(44)); | |
118 assertEquals(-1, root.call(42)); | |
119 assertEquals(-1, root.call(43)); | |
120 assertEquals(-1, root.call(44)); | |
121 } | |
122 | |
123 @NodeChild | |
124 static class TestBoundCacheOverflowContains extends ValueNode { | |
125 | |
126 @Specialization(guards = "value == cachedValue", limit = "2") | |
127 static int do1(int value, @Cached("value") int cachedValue) { | |
128 return cachedValue; | |
129 } | |
130 | |
131 @Specialization(contains = "do1") | |
132 static int do2(int value) { | |
133 return -1; | |
134 } | |
135 | |
136 } | |
137 | |
138 @Test | |
139 public void testCacheField() { | |
140 CallTarget root = createCallTarget(TestCacheFieldFactory.getInstance()); | |
141 assertEquals(3, root.call(42)); | |
142 assertEquals(3, root.call(43)); | |
143 } | |
144 | |
145 @NodeChild | |
146 static class TestCacheField extends ValueNode { | |
147 | |
148 protected int field = 3; | |
149 | |
150 @Specialization() | |
151 static int do1(int value, @Cached("field") int cachedValue) { | |
152 return cachedValue; | |
153 } | |
154 | |
155 } | |
156 | |
157 @Test | |
158 public void testCacheNodeField() { | |
159 CallTarget root = createCallTarget(TestCacheNodeFieldFactory.getInstance(), 21); | |
160 assertEquals(21, root.call(42)); | |
161 assertEquals(21, root.call(43)); | |
162 } | |
163 | |
164 @NodeChild | |
165 @NodeField(name = "field", type = int.class) | |
166 static class TestCacheNodeField extends ValueNode { | |
167 | |
168 @Specialization | |
169 static int do1(int value, @Cached("field") int cachedValue) { | |
170 return cachedValue; | |
171 } | |
172 | |
173 } | |
174 | |
175 @Test | |
176 public void testCacheMethod() { | |
177 TestCacheMethod.invocations = 0; | |
178 CallTarget root = createCallTarget(TestCacheMethodFactory.getInstance()); | |
179 assertEquals(42, root.call(42)); | |
180 assertEquals(42, root.call(43)); | |
181 assertEquals(42, root.call(44)); | |
182 assertEquals(1, TestCacheMethod.invocations); | |
183 } | |
184 | |
185 @NodeChild | |
186 static class TestCacheMethod extends ValueNode { | |
187 | |
188 static int invocations = 0; | |
189 | |
190 @Specialization | |
191 static int do1(int value, @Cached("someMethod(value)") int cachedValue) { | |
192 return cachedValue; | |
193 } | |
194 | |
195 static int someMethod(int value) { | |
196 invocations++; | |
197 return value; | |
198 } | |
199 | |
200 } | |
201 | |
202 @Test | |
203 public void testGuardWithJustCachedParameter() { | |
204 TestGuardWithJustCachedParameter.invocations = 0; | |
205 CallTarget root = createCallTarget(TestGuardWithJustCachedParameterFactory.getInstance()); | |
206 assertEquals(42, root.call(42)); | |
207 assertEquals(42, root.call(43)); | |
208 assertEquals(42, root.call(44)); | |
209 // guards with just cached parameters are just invoked on the slow path | |
210 assertEquals(assertionsEnabled() ? 4 : 1, TestGuardWithJustCachedParameter.invocations); | |
211 } | |
212 | |
213 @NodeChild | |
214 static class TestGuardWithJustCachedParameter extends ValueNode { | |
215 | |
216 static int invocations = 0; | |
217 | |
218 @Specialization(guards = "someMethod(cachedValue)") | |
219 static int do1(int value, @Cached("value") int cachedValue) { | |
220 return cachedValue; | |
221 } | |
222 | |
223 static boolean someMethod(int value) { | |
224 invocations++; | |
225 return true; | |
226 } | |
227 | |
228 } | |
229 | |
230 @Test | |
231 public void testGuardWithCachedAndDynamicParameter() { | |
232 TestGuardWithCachedAndDynamicParameter.cachedMethodInvocations = 0; | |
233 TestGuardWithCachedAndDynamicParameter.dynamicMethodInvocations = 0; | |
234 CallTarget root = createCallTarget(TestGuardWithCachedAndDynamicParameterFactory.getInstance()); | |
235 assertEquals(42, root.call(42)); | |
236 assertEquals(42, root.call(43)); | |
237 assertEquals(42, root.call(44)); | |
238 // guards with just cached parameters are just invoked on the slow path | |
239 assertEquals(assertionsEnabled() ? 4 : 1, TestGuardWithCachedAndDynamicParameter.cachedMethodInvocations); | |
240 assertEquals(4, TestGuardWithCachedAndDynamicParameter.dynamicMethodInvocations); | |
241 } | |
242 | |
243 @NodeChild | |
244 static class TestGuardWithCachedAndDynamicParameter extends ValueNode { | |
245 | |
246 static int cachedMethodInvocations = 0; | |
247 static int dynamicMethodInvocations = 0; | |
248 | |
249 @Specialization(guards = {"dynamicMethod(value)", "cachedMethod(cachedValue)"}) | |
250 static int do1(int value, @Cached("value") int cachedValue) { | |
251 return cachedValue; | |
252 } | |
253 | |
254 static boolean cachedMethod(int value) { | |
255 cachedMethodInvocations++; | |
256 return true; | |
257 } | |
258 | |
259 static boolean dynamicMethod(int value) { | |
260 dynamicMethodInvocations++; | |
261 return true; | |
262 } | |
263 | |
264 } | |
265 | |
266 @NodeChild | |
267 static class TestMultipleCaches extends ValueNode { | |
268 | |
269 @Specialization | |
270 static int do1(int value, @Cached("value") int cachedValue1, @Cached("value") int cachedValue2) { | |
271 return cachedValue1 + cachedValue2; | |
272 } | |
273 | |
274 } | |
275 | |
276 @Test | |
277 public void testMultipleCaches() { | |
278 CallTarget root = createCallTarget(TestMultipleCachesFactory.getInstance()); | |
279 assertEquals(42, root.call(21)); | |
280 assertEquals(42, root.call(22)); | |
281 assertEquals(42, root.call(23)); | |
282 } | |
283 | |
284 @NodeChild | |
285 static class CachedError1 extends ValueNode { | |
286 @Specialization | |
287 static int do1(int value, @ExpectError("Incompatible return type int. The expression type must be equal to the parameter type double.")// | |
288 @Cached("value") double cachedValue) { | |
289 return value; | |
290 } | |
291 } | |
292 | |
293 @NodeChild | |
294 static class CachedError2 extends ValueNode { | |
295 | |
296 // caches are not allowed to make backward references | |
297 | |
298 @Specialization | |
299 static int do1(int value, | |
300 @ExpectError("The initializer expression of parameter 'cachedValue1' binds unitialized parameter 'cachedValue2. Reorder the parameters to resolve the problem.") @Cached("cachedValue2") int cachedValue1, | |
301 @Cached("value") int cachedValue2) { | |
302 return cachedValue1 + cachedValue2; | |
303 } | |
304 | |
305 } | |
306 | |
307 @NodeChild | |
308 static class CachedError3 extends ValueNode { | |
309 | |
310 // cyclic dependency between cached expressions | |
311 @Specialization | |
312 static int do1(int value, | |
313 @ExpectError("The initializer expression of parameter 'cachedValue1' binds unitialized parameter 'cachedValue2. Reorder the parameters to resolve the problem.") @Cached("cachedValue2") int cachedValue1, | |
314 @Cached("cachedValue1") int cachedValue2) { | |
315 return cachedValue1 + cachedValue2; | |
316 } | |
317 | |
318 } | |
319 | |
320 } |