comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameSlotTypeSpecializationTest.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 /**
33 * <h3>Specializing Frame Slot Types</h3>
34 *
35 * <p>
36 * Dynamically typed languages can speculate on the type of a frame slot and only fall back at run time to a more
37 * generic type if necessary. The new type of a frame slot can be set using the {@link FrameSlot#setType(Class)} method.
38 * It is the responsibility of the language implementor to update the content of currently active frames (using
39 * {@link Frame#updateToLatestVersion()}). Also, nodes that depend a specific type of a frame slot must be replaced.
40 * Such node can register a listener that implements {@link FrameSlotTypeListener} using
41 * {@link FrameSlot#registerOneShotTypeListener(FrameSlotTypeListener)}. The event of a type change on the frame slot
42 * will fire only once for the next upcoming change.
43 * </p>
44 *
45 * <p>
46 * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ReturnTypeSpecializationTest}.
47 * </p>
48 */
49 public class FrameSlotTypeSpecializationTest {
50
51 @Test
52 public void test() {
53 TruffleRuntime runtime = Truffle.getRuntime();
54 FrameDescriptor frameDescriptor = new FrameDescriptor();
55 FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class);
56 TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot));
57 CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor);
58 Assert.assertEquals(Integer.class, slot.getType());
59 Object result = target.call();
60 Assert.assertEquals("42", result);
61 Assert.assertEquals(Object.class, slot.getType());
62 }
63
64 class TestRootNode extends RootNode {
65
66 @Child TestChildNode left;
67 @Child TestChildNode right;
68
69 public TestRootNode(TestChildNode left, TestChildNode right) {
70 this.left = adoptChild(left);
71 this.right = adoptChild(right);
72 }
73
74 @Override
75 public Object execute(VirtualFrame frame) {
76 left.execute(frame);
77 return right.execute(frame);
78 }
79 }
80
81 abstract class TestChildNode extends Node {
82 abstract Object execute(VirtualFrame frame);
83 }
84
85 abstract class FrameSlotNode extends TestChildNode {
86 protected final FrameSlot slot;
87
88 public FrameSlotNode(FrameSlot slot) {
89 this.slot = slot;
90 }
91 }
92
93 class StringTestChildNode extends TestChildNode {
94
95 @Override
96 Object execute(VirtualFrame frame) {
97 return "42";
98 }
99
100 }
101
102 class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener {
103 @Child private TestChildNode value;
104
105 IntAssignLocal(FrameSlot slot, TestChildNode value) {
106 super(slot);
107 this.value = adoptChild(value);
108 slot.registerOneShotTypeListener(this);
109 }
110
111 @Override
112 Object execute(VirtualFrame frame) {
113 Object o = value.execute(frame);
114 if (o instanceof Integer) {
115 frame.setInt(slot, (Integer) o);
116 } else {
117 slot.setType(Object.class);
118 frame.updateToLatestVersion();
119 frame.setObject(slot, o);
120 }
121 return null;
122 }
123
124 @Override
125 public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
126 if (changedSlot.getType() == Object.class) {
127 this.replace(new ObjectAssignLocal(changedSlot, value));
128 }
129 }
130 }
131
132 class ObjectAssignLocal extends FrameSlotNode {
133 @Child private TestChildNode value;
134
135 ObjectAssignLocal(FrameSlot slot, TestChildNode value) {
136 super(slot);
137 this.value = adoptChild(value);
138 }
139
140 @Override
141 Object execute(VirtualFrame frame) {
142 Object o = value.execute(frame);
143 frame.setObject(slot, o);
144 return null;
145 }
146 }
147
148 class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener {
149 IntReadLocal(FrameSlot slot) {
150 super(slot);
151 slot.registerOneShotTypeListener(this);
152 }
153
154 @Override
155 Object execute(VirtualFrame frame) {
156 return frame.getInt(slot);
157 }
158
159 @Override
160 public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
161 if (changedSlot.getType() == Object.class) {
162 this.replace(new ObjectReadLocal(changedSlot));
163 }
164 }
165 }
166
167 class ObjectReadLocal extends FrameSlotNode {
168 ObjectReadLocal(FrameSlot slot) {
169 super(slot);
170 }
171
172 @Override
173 Object execute(VirtualFrame frame) {
174 return frame.getObject(slot);
175 }
176 }
177 }
178