comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReplaceTest.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 static org.junit.Assert.*;
26
27 import java.util.*;
28
29 import org.junit.*;
30
31 import com.oracle.truffle.api.*;
32 import com.oracle.truffle.api.frame.*;
33 import com.oracle.truffle.api.nodes.*;
34
35
36 /**
37 * <h3>Replacing Nodes at Run Time</h3>
38 *
39 * <p>
40 * The structure of the Truffle tree can be changed at run time by replacing nodes using the {@link Node#replace(Node)}
41 * method. This method will automatically change the child pointer in the parent of the node and replace it with a
42 * pointer to the new node.
43 * </p>
44 *
45 * <p>
46 * Replacing nodes is a costly operation, so it should not happen too often. The convention is that the implementation
47 * of the Truffle nodes should ensure that there are maximal a small (and constant) number of node replacements per
48 * Truffle node.
49 * </p>
50 *
51 * <p>
52 * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.CallTest}.
53 * </p>
54 */
55 public class ReplaceTest {
56
57 @Test
58 public void test() {
59 TruffleRuntime runtime = Truffle.getRuntime();
60 UnresolvedNode leftChild = new UnresolvedNode("20");
61 UnresolvedNode rightChild = new UnresolvedNode("22");
62 TestRootNode rootNode = new TestRootNode(new ValueNode[]{leftChild, rightChild});
63 assertEquals(rootNode, leftChild.getParent());
64 assertEquals(rootNode, rightChild.getParent());
65 Iterator<Node> iterator = rootNode.getChildren().iterator();
66 Assert.assertEquals(leftChild, iterator.next());
67 Assert.assertEquals(rightChild, iterator.next());
68 Assert.assertFalse(iterator.hasNext());
69 CallTarget target = runtime.createCallTarget(rootNode);
70 Object result = target.call();
71 assertEquals(42, result);
72 assertEquals(42, target.call());
73 iterator = rootNode.getChildren().iterator();
74 Assert.assertEquals(ResolvedNode.class, iterator.next().getClass());
75 Assert.assertEquals(ResolvedNode.class, iterator.next().getClass());
76 Assert.assertFalse(iterator.hasNext());
77 iterator = rootNode.getChildren().iterator();
78 Assert.assertEquals(rootNode, iterator.next().getParent());
79 Assert.assertEquals(rootNode, iterator.next().getParent());
80 Assert.assertFalse(iterator.hasNext());
81 }
82
83 class TestRootNode extends RootNode {
84
85 @Children private ValueNode[] children;
86
87 public TestRootNode(ValueNode[] children) {
88 this.children = adoptChildren(children);
89 }
90
91 @Override
92 public Object execute(VirtualFrame frame) {
93 int sum = 0;
94 for (int i = 0; i < children.length; ++i) {
95 sum += children[i].execute();
96 }
97 return sum;
98 }
99 }
100
101 abstract class ValueNode extends Node {
102 abstract int execute();
103 }
104
105 class UnresolvedNode extends ValueNode {
106 private final String value;
107
108 public UnresolvedNode(String value) {
109 this.value = value;
110 }
111
112 @Override
113 int execute() {
114 int intValue = Integer.parseInt(value);
115 ResolvedNode newNode = this.replace(new ResolvedNode(intValue));
116 return newNode.execute();
117 }
118 }
119
120 class ResolvedNode extends ValueNode {
121 private final int value;
122
123 ResolvedNode(int value) {
124 this.value = value;
125 }
126
127 @Override
128 int execute() {
129 return value;
130 }
131 }
132 }
133