comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReturnTypeSpecializationTest.java @ 7267:a4b84ba6dc2e

Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 18 Dec 2012 15:33:55 +0100
parents
children 5e3d1a68664e
comparison
equal deleted inserted replaced
7259:494d99e07614 7267:a4b84ba6dc2e
1 /*
2 * Copyright (c) 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.test;
24
25 import org.junit.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.frame.*;
29 import com.oracle.truffle.api.nodes.*;
30
31 /**
32 * <h3>Specializing Return Types</h3>
33 *
34 * <p>
35 * In order to avoid boxing and/or type casts on the return value of a node, the return value the method for executing a
36 * node can have a specific type and need not be of type {@link java.lang.Object}. For dynamically typed languages, this
37 * return type is something that should be speculated on. When the speculation fails and the child node cannot return
38 * the appropriate type of value, it can use an {@link UnexpectedResultException} to still pass the result to the
39 * caller. In such a case, the caller must rewrite itself to a more general version in oder to avoid future failures of
40 * this kind.
41 * </p>
42 */
43 public class ReturnTypeSpecializationTest {
44
45 @Test
46 public void test() {
47 TruffleRuntime runtime = Truffle.getRuntime();
48 FrameDescriptor frameDescriptor = new FrameDescriptor();
49 FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class);
50 TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot));
51 CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor);
52 Assert.assertEquals(Integer.class, slot.getType());
53 Object result = target.call();
54 Assert.assertEquals("42", result);
55 Assert.assertEquals(Object.class, slot.getType());
56 }
57
58 class TestRootNode extends RootNode {
59
60 @Child TestChildNode left;
61 @Child TestChildNode right;
62
63 public TestRootNode(TestChildNode left, TestChildNode right) {
64 this.left = adoptChild(left);
65 this.right = adoptChild(right);
66 }
67
68 @Override
69 public Object execute(VirtualFrame frame) {
70 left.execute(frame);
71 return right.execute(frame);
72 }
73 }
74
75 abstract class TestChildNode extends Node {
76 abstract Object execute(VirtualFrame frame);
77
78 int executeInt(VirtualFrame frame) throws UnexpectedResultException {
79 Object result = execute(frame);
80 if (result instanceof Integer) {
81 return (Integer) result;
82 }
83 throw new UnexpectedResultException(result);
84 }
85 }
86
87 abstract class FrameSlotNode extends TestChildNode {
88 protected final FrameSlot slot;
89
90 public FrameSlotNode(FrameSlot slot) {
91 this.slot = slot;
92 }
93 }
94
95 class StringTestChildNode extends TestChildNode {
96
97 @Override
98 Object execute(VirtualFrame frame) {
99 return "42";
100 }
101
102 }
103
104 class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener {
105 @Child private TestChildNode value;
106
107 IntAssignLocal(FrameSlot slot, TestChildNode value) {
108 super(slot);
109 this.value = adoptChild(value);
110 slot.registerOneShotTypeListener(this);
111 }
112
113 @Override
114 Object execute(VirtualFrame frame) {
115 try {
116 frame.setInt(slot, value.executeInt(frame));
117 } catch (UnexpectedResultException e) {
118 slot.setType(Object.class);
119 frame.updateToLatestVersion();
120 frame.setObject(slot, e.getResult());
121 }
122 return null;
123 }
124
125 @Override
126 public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
127 if (changedSlot.getType() == Object.class) {
128 this.replace(new ObjectAssignLocal(changedSlot, value));
129 }
130 }
131 }
132
133 class ObjectAssignLocal extends FrameSlotNode {
134 @Child private TestChildNode value;
135
136 ObjectAssignLocal(FrameSlot slot, TestChildNode value) {
137 super(slot);
138 this.value = adoptChild(value);
139 }
140
141 @Override
142 Object execute(VirtualFrame frame) {
143 Object o = value.execute(frame);
144 frame.setObject(slot, o);
145 return null;
146 }
147 }
148
149 class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener {
150 IntReadLocal(FrameSlot slot) {
151 super(slot);
152 slot.registerOneShotTypeListener(this);
153 }
154
155 @Override
156 Object execute(VirtualFrame frame) {
157 return executeInt(frame);
158 }
159
160 @Override
161 int executeInt(VirtualFrame frame) {
162 return frame.getInt(slot);
163 }
164
165 @Override
166 public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
167 if (changedSlot.getType() == Object.class) {
168 this.replace(new ObjectReadLocal(changedSlot));
169 }
170 }
171 }
172
173 class ObjectReadLocal extends FrameSlotNode {
174 ObjectReadLocal(FrameSlot slot) {
175 super(slot);
176 }
177
178 @Override
179 Object execute(VirtualFrame frame) {
180 return frame.getObject(slot);
181 }
182 }
183 }
184