Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ShortCircuitTest.java @ 13527:25ecb47a6d0e
Truffle-DSL: Added support for references to child arrays in @ShortCircuit;
Introduced new layer NodeExecutionData to the implementation model which is in between NodeChildData and the actual parameters..
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 07 Jan 2014 12:22:47 +0100 |
parents | |
children | 64dcb92ee75a |
comparison
equal
deleted
inserted
replaced
13483:37ec2cabf397 | 13527:25ecb47a6d0e |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, 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 org.junit.Assert.*; | |
26 | |
27 import org.junit.*; | |
28 | |
29 import com.oracle.truffle.api.*; | |
30 import com.oracle.truffle.api.dsl.*; | |
31 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.DoubleChildNodeFactory; | |
32 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.SingleChildNodeFactory; | |
33 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.VarArgsNodeFactory; | |
34 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ArgumentNode; | |
35 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestArguments; | |
36 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
37 | |
38 public class ShortCircuitTest { | |
39 | |
40 @Test | |
41 public void testSingleChild1() { | |
42 ArgumentNode arg0 = new ArgumentNode(0); | |
43 CallTarget callTarget = TestHelper.createCallTarget(SingleChildNodeFactory.create(arg0)); | |
44 SingleChildNode.needsChild = true; | |
45 assertEquals(42, callTarget.call(new TestArguments(42))); | |
46 assertEquals(1, arg0.getInvocationCount()); | |
47 } | |
48 | |
49 @Test | |
50 public void testSingleChild2() { | |
51 ArgumentNode arg0 = new ArgumentNode(0); | |
52 CallTarget callTarget = TestHelper.createCallTarget(SingleChildNodeFactory.create(arg0)); | |
53 SingleChildNode.needsChild = false; | |
54 assertEquals(0, callTarget.call(new TestArguments(42))); | |
55 assertEquals(0, arg0.getInvocationCount()); | |
56 } | |
57 | |
58 @NodeChild("child0") | |
59 abstract static class SingleChildNode extends ValueNode { | |
60 | |
61 static boolean needsChild; | |
62 | |
63 @ShortCircuit("child0") | |
64 boolean needsChild0() { | |
65 return needsChild; | |
66 } | |
67 | |
68 @Specialization | |
69 int doIt(boolean hasChild0, int child0) { | |
70 assert hasChild0 == needsChild0(); | |
71 return child0; | |
72 } | |
73 | |
74 } | |
75 | |
76 @Test | |
77 public void testDoubleChild1() { | |
78 ArgumentNode arg0 = new ArgumentNode(0); | |
79 ArgumentNode arg1 = new ArgumentNode(1); | |
80 CallTarget callTarget = TestHelper.createCallTarget(DoubleChildNodeFactory.create(arg0, arg1)); | |
81 assertEquals(42, callTarget.call(new TestArguments(41, 42))); | |
82 assertEquals(1, arg1.getInvocationCount()); | |
83 } | |
84 | |
85 @Test | |
86 public void testDoubleChild2() { | |
87 ArgumentNode arg0 = new ArgumentNode(0); | |
88 ArgumentNode arg1 = new ArgumentNode(1); | |
89 CallTarget callTarget = TestHelper.createCallTarget(DoubleChildNodeFactory.create(arg0, arg1)); | |
90 assertEquals(0, callTarget.call(new TestArguments(42, 42))); | |
91 assertEquals(0, arg1.getInvocationCount()); | |
92 } | |
93 | |
94 @NodeChildren({@NodeChild("child0"), @NodeChild("child1")}) | |
95 @SuppressWarnings("unused") | |
96 abstract static class DoubleChildNode extends ValueNode { | |
97 | |
98 @ShortCircuit("child1") | |
99 boolean needsChild1(Object leftValue) { | |
100 return leftValue.equals(41); | |
101 } | |
102 | |
103 @Specialization | |
104 int doIt(int child0, boolean hasChild1, int child1) { | |
105 return child1; | |
106 } | |
107 | |
108 } | |
109 | |
110 @Test | |
111 public void testVarArgs1() { | |
112 ArgumentNode arg0 = new ArgumentNode(0); | |
113 ArgumentNode arg1 = new ArgumentNode(1); | |
114 CallTarget callTarget = TestHelper.createCallTarget(VarArgsNodeFactory.create(new ValueNode[]{arg0, arg1})); | |
115 assertEquals(42, callTarget.call(new TestArguments(41, 42))); | |
116 assertEquals(1, arg1.getInvocationCount()); | |
117 } | |
118 | |
119 @Test | |
120 public void testVarArgs2() { | |
121 ArgumentNode arg0 = new ArgumentNode(0); | |
122 ArgumentNode arg1 = new ArgumentNode(1); | |
123 CallTarget callTarget = TestHelper.createCallTarget(VarArgsNodeFactory.create(new ValueNode[]{arg0, arg1})); | |
124 assertEquals(0, callTarget.call(new TestArguments(42, 42))); | |
125 assertEquals(0, arg1.getInvocationCount()); | |
126 } | |
127 | |
128 @NodeChild(value = "children", type = ValueNode[].class) | |
129 abstract static class VarArgsNode extends ValueNode { | |
130 | |
131 @ShortCircuit("children[1]") | |
132 boolean needsChild1(Object leftValue) { | |
133 return leftValue.equals(41); | |
134 } | |
135 | |
136 @Specialization | |
137 @SuppressWarnings("unused") | |
138 int doIt(int child0, boolean hasChild1, int child1) { | |
139 return child1; | |
140 } | |
141 | |
142 } | |
143 | |
144 } |