comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ExecuteEvaluatedTest.java @ 10597:79041ab43660

Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Jul 2013 20:58:32 +0200
parents
children 5fbd1ba4a5f3
comparison
equal deleted inserted replaced
10596:f43eb2f1bbbc 10597:79041ab43660
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 org.junit.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.dsl.*;
29 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.DoubleEvaluatedNodeFactory;
30 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.EvaluatedNodeFactory;
31 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.UseDoubleEvaluatedNodeFactory;
32 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.UseEvaluatedNodeFactory;
33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ArgumentNode;
34 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestArguments;
35 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
36 import com.oracle.truffle.api.frame.*;
37 import com.oracle.truffle.api.nodes.*;
38
39 public class ExecuteEvaluatedTest {
40
41 @Test
42 public void testSingleEvaluated() {
43 ArgumentNode arg0 = new ArgumentNode(0);
44 CallTarget callTarget = TestHelper.createCallTarget(UseEvaluatedNodeFactory.create(arg0, EvaluatedNodeFactory.create(null)));
45
46 Assert.assertEquals(43, callTarget.call(new TestArguments(42)));
47 Assert.assertEquals(1, arg0.getInvocationCount());
48 }
49
50 @NodeChild("exp")
51 abstract static class EvaluatedNode extends ValueNode {
52
53 @Specialization
54 int doExecuteWith(int exp) {
55 return exp + 1;
56 }
57
58 public abstract Object executeEvaluated(VirtualFrame frame, Object targetValue);
59
60 public abstract int executeIntEvaluated(VirtualFrame frame, Object targetValue) throws UnexpectedResultException;
61 }
62
63 @NodeChildren({@NodeChild("exp0"), @NodeChild(value = "exp1", type = EvaluatedNode.class, executeWith = "exp0")})
64 abstract static class UseEvaluatedNode extends ValueNode {
65
66 @Specialization
67 int call(int exp0, int exp1) {
68 Assert.assertEquals(exp0 + 1, exp1);
69 return exp1;
70 }
71 }
72
73 @Test
74 public void testDoubleEvaluated() {
75 ArgumentNode arg0 = new ArgumentNode(0);
76 ArgumentNode arg1 = new ArgumentNode(1);
77 CallTarget callTarget = TestHelper.createCallTarget(UseDoubleEvaluatedNodeFactory.create(arg0, arg1, DoubleEvaluatedNodeFactory.create(null, null)));
78
79 Assert.assertEquals(85, callTarget.call(new TestArguments(42, 43)));
80 Assert.assertEquals(1, arg0.getInvocationCount());
81 Assert.assertEquals(1, arg1.getInvocationCount());
82 }
83
84 @NodeChildren({@NodeChild("exp0"), @NodeChild("exp1")})
85 abstract static class DoubleEvaluatedNode extends ValueNode {
86
87 @Specialization
88 int doExecuteWith(int exp0, int exp1) {
89 return exp0 + exp1;
90 }
91
92 public abstract Object executeEvaluated(VirtualFrame frame, Object exp0, Object exp1);
93
94 public abstract int executeIntEvaluated(VirtualFrame frame, Object exp0, Object exp1) throws UnexpectedResultException;
95 }
96
97 @NodeChildren({@NodeChild("exp0"), @NodeChild("exp1"), @NodeChild(value = "exp2", type = DoubleEvaluatedNode.class, executeWith = {"exp0", "exp1"})})
98 abstract static class UseDoubleEvaluatedNode extends ValueNode {
99
100 @Specialization
101 int call(int exp0, int exp1, int exp2) {
102 Assert.assertEquals(exp0 + exp1, exp2);
103 return exp2;
104 }
105 }
106
107 }