# HG changeset patch
# User Doug Simon
# Date 1355849972 -3600
# Node ID 19f5c9b96fa7577b092f2ecb663a339ffeaab446
# Parent 7f6bf286629c9f1d8a480f85a3f2b63f65d87e7f# Parent 17e12b5a3386f684e9b4923a65d53649faa13a3f
Merge.
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/SwitchNode.java
--- a/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/SwitchNode.java Tue Dec 18 17:59:03 2012 +0100
+++ b/graal/com.oracle.graal.nodes/src/com/oracle/graal/nodes/extended/SwitchNode.java Tue Dec 18 17:59:32 2012 +0100
@@ -22,6 +22,8 @@
*/
package com.oracle.graal.nodes.extended;
+import java.util.*;
+
import com.oracle.graal.api.meta.*;
import com.oracle.graal.graph.*;
import com.oracle.graal.nodes.*;
@@ -34,8 +36,8 @@
@Successor protected final NodeSuccessorList successors;
protected double[] successorProbabilities;
@Input private ValueNode value;
- private final double[] keyProbabilities;
- private final int[] keySuccessors;
+ private double[] keyProbabilities;
+ private int[] keySuccessors;
public ValueNode value() {
return value;
@@ -139,4 +141,13 @@
}
return probability;
}
+
+ @Override
+ public SwitchNode clone(Graph into) {
+ SwitchNode newSwitch = (SwitchNode) super.clone(into);
+ newSwitch.successorProbabilities = Arrays.copyOf(successorProbabilities, successorProbabilities.length);
+ newSwitch.keyProbabilities = Arrays.copyOf(keyProbabilities, keyProbabilities.length);
+ newSwitch.keySuccessors = Arrays.copyOf(keySuccessors, keySuccessors.length);
+ return newSwitch;
+ }
}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ArgumentsTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ArgumentsTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+
+/**
+ *
Passing Arguments
+ *
+ *
+ * A guest language can pass its own custom arguments when invoking a Truffle method by creating a subclass of
+ * {@link Arguments}. When invoking a call target with {@link CallTarget#call(Arguments)}, the arguments can be passed.
+ * A Truffle node can access the arguments passed into the Truffle method by using {@link VirtualFrame#getArguments()}.
+ *
+ *
+ *
+ * The arguments class should only contain fields that are declared as final. This allows the Truffle runtime to improve
+ * optimizations around guest language method calls. Also, the arguments object must never be stored into a field. It
+ * should be created immediately before invoking {@link CallTarget#call(Arguments)} and no longer be accessed
+ * afterwards.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.FrameTest}.
+ *
+ */
+public class ArgumentsTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ TestRootNode rootNode = new TestRootNode(new TestArgumentNode[]{new TestArgumentNode(0), new TestArgumentNode(1)});
+ CallTarget target = runtime.createCallTarget(rootNode);
+ Object result = target.call(new TestArguments(20, 22));
+ Assert.assertEquals(42, result);
+ }
+
+ class TestArguments extends Arguments {
+ final int[] values;
+ TestArguments(int... values) {
+ this.values = values;
+ }
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Children private TestArgumentNode[] children;
+
+ TestRootNode(TestArgumentNode[] children) {
+ this.children = adoptChildren(children);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ int sum = 0;
+ for (int i = 0; i < children.length; ++i) {
+ sum += children[i].execute(frame);
+ }
+ return sum;
+ }
+ }
+
+ class TestArgumentNode extends Node {
+ private final int index;
+
+ TestArgumentNode(int index) {
+ this.index = index;
+ }
+
+ int execute(VirtualFrame frame) {
+ return ((TestArguments) frame.getArguments()).values[index];
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/CallTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/CallTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+
+/**
+ *
Calling Another Tree
+ *
+ *
+ * A guest language implementation can create multiple call targets using the {@link TruffleRuntime#createCallTarget(RootNode)} method.
+ * Those call targets can be passed around as normal Java objects and used for calling guest language methods.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ArgumentsTest}.
+ *
+ */
+public class CallTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ CallTarget foo = runtime.createCallTarget(new ConstantRootNode(20));
+ CallTarget bar = runtime.createCallTarget(new ConstantRootNode(22));
+ CallTarget main = runtime.createCallTarget(new DualCallNode(foo, bar));
+ Object result = main.call();
+ Assert.assertEquals(42, result);
+ }
+
+ class DualCallNode extends RootNode {
+
+ private final CallTarget firstTarget;
+ private final CallTarget secondTarget;
+
+ DualCallNode(CallTarget firstTarget, CallTarget secondTarget) {
+ this.firstTarget = firstTarget;
+ this.secondTarget = secondTarget;
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ return ((Integer) firstTarget.call()) + ((Integer) secondTarget.call());
+ }
+ }
+
+ class ConstantRootNode extends RootNode {
+ private final int value;
+
+ public ConstantRootNode(int value) {
+ this.value = value;
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ return value;
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ChildNodeTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ChildNodeTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import java.util.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+import com.oracle.truffle.api.nodes.Node.Child;
+
+/**
+ *
Creating a Child Node
+ *
+ *
+ * Child nodes are stored in the class of the parent node in fields that are marked with the {@link Child} annotation.
+ * Before such a field is assigned, {@link Node#adoptChild} must be called. This method automatically establishes a link
+ * from the child to the parent. The {@link Node#getParent()} method allows access to this field. Every node also
+ * provides the ability to iterate over its children using {@link Node#getChildren()}.
+ *
+ *
A child node field must be declared private and non-final. It may only be assigned in the constructor of the parent
+ * node. For changing the structure of the tree at run time, the method {@link Node#replace(Node)} must be used (see
+ * {@link ReplaceTest}).
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ChildrenNodesTest}.
+ *
+ */
+public class ChildNodeTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ TestChildNode leftChild = new TestChildNode();
+ TestChildNode rightChild = new TestChildNode();
+ TestRootNode rootNode = new TestRootNode(leftChild, rightChild);
+ Assert.assertEquals(rootNode, leftChild.getParent());
+ Assert.assertEquals(rootNode, rightChild.getParent());
+ Iterator iterator = rootNode.getChildren().iterator();
+ Assert.assertEquals(leftChild, iterator.next());
+ Assert.assertEquals(rightChild, iterator.next());
+ Assert.assertFalse(iterator.hasNext());
+ CallTarget target = runtime.createCallTarget(rootNode);
+ Object result = target.call();
+ Assert.assertEquals(42, result);
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Child private TestChildNode left;
+ @Child private TestChildNode right;
+
+ public TestRootNode(TestChildNode left, TestChildNode right) {
+ this.left = adoptChild(left);
+ this.right = adoptChild(right);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ return left.execute() + right.execute();
+ }
+ }
+
+ class TestChildNode extends Node {
+
+ public int execute() {
+ return 21;
+ }
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ChildrenNodesTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ChildrenNodesTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import java.util.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ *
Creating an Array of Children Nodes
+ *
+ *
+ * An array of children nodes can be used as a field in a parent node. The field has to be annotated with
+ * {@link com.oracle.truffle.api.nodes.Node.Children} and must be declared private and final. Before assigning the field
+ * in the parent node constructor, {@link Node#adoptChildren} must be called in order to update the parent pointers in
+ * the child nodes. After filling the array with its first values, it must never be changed. It is only possible to call
+ * {@link Node#replace} on a child node.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.FinalFieldTest}.
+ *
+ */
+public class ChildrenNodesTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ TestChildNode firstChild = new TestChildNode();
+ TestChildNode secondChild = new TestChildNode();
+ TestRootNode rootNode = new TestRootNode(new TestChildNode[]{firstChild, secondChild});
+ Assert.assertEquals(rootNode, firstChild.getParent());
+ Assert.assertEquals(rootNode, secondChild.getParent());
+ Iterator iterator = rootNode.getChildren().iterator();
+ Assert.assertEquals(firstChild, iterator.next());
+ Assert.assertEquals(secondChild, iterator.next());
+ Assert.assertFalse(iterator.hasNext());
+ CallTarget target = runtime.createCallTarget(rootNode);
+ Object result = target.call();
+ Assert.assertEquals(42, result);
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Children private final TestChildNode[] children;
+
+ public TestRootNode(TestChildNode[] children) {
+ this.children = adoptChildren(children);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ int sum = 0;
+ for (int i = 0; i < children.length; ++i) {
+ sum += children[i].execute();
+ }
+ return sum;
+ }
+ }
+
+ class TestChildNode extends Node {
+ public int execute() {
+ return 21;
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FinalFieldTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FinalFieldTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ *
Using Final Fields in Node Classes
+ *
+ *
+ * The usage of final fields in node classes is highly encouraged. It is beneficial for performance to declare every
+ * field that is not pointing to a child node as final. This gives the Truffle runtime an increased opportunity to
+ * optimize this node.
+ *
+ *
+ *
+ * If a node has a value which may change at run time, but will rarely do so, it is recommended to speculate on the
+ * field being final. This involves starting executing with a node where this field is final and only if this
+ * turns out to be no longer the case, the node is replaced with an alternative implementation of the operation (see
+ * {@link ReplaceTest}).
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ReplaceTest}.
+ *
+ */
+public class FinalFieldTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ TestRootNode rootNode = new TestRootNode(new TestChildNode[]{new TestChildNode(20), new TestChildNode(22)});
+ CallTarget target = runtime.createCallTarget(rootNode);
+ Object result = target.call();
+ Assert.assertEquals(42, result);
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Children TestChildNode[] children;
+
+ public TestRootNode(TestChildNode[] children) {
+ this.children = adoptChildren(children);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ int sum = 0;
+ for (int i = 0; i < children.length; ++i) {
+ sum += children[i].execute();
+ }
+ return sum;
+ }
+ }
+
+ class TestChildNode extends Node {
+ private final int value;
+
+ public TestChildNode(int value) {
+ this.value = value;
+ }
+
+ public int execute() {
+ return value;
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameSlotTypeSpecializationTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameSlotTypeSpecializationTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+
+/**
+ *
Specializing Frame Slot Types
+ *
+ *
+ * Dynamically typed languages can speculate on the type of a frame slot and only fall back at run time to a more
+ * generic type if necessary. The new type of a frame slot can be set using the {@link FrameSlot#setType(Class)} method.
+ * It is the responsibility of the language implementor to update the content of currently active frames (using
+ * {@link Frame#updateToLatestVersion()}). Also, nodes that depend a specific type of a frame slot must be replaced.
+ * Such node can register a listener that implements {@link FrameSlotTypeListener} using
+ * {@link FrameSlot#registerOneShotTypeListener(FrameSlotTypeListener)}. The event of a type change on the frame slot
+ * will fire only once for the next upcoming change.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ReturnTypeSpecializationTest}.
+ *
+ */
+public class FrameSlotTypeSpecializationTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ FrameDescriptor frameDescriptor = new FrameDescriptor();
+ FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class);
+ TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot));
+ CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor);
+ Assert.assertEquals(Integer.class, slot.getType());
+ Object result = target.call();
+ Assert.assertEquals("42", result);
+ Assert.assertEquals(Object.class, slot.getType());
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Child TestChildNode left;
+ @Child TestChildNode right;
+
+ public TestRootNode(TestChildNode left, TestChildNode right) {
+ this.left = adoptChild(left);
+ this.right = adoptChild(right);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ left.execute(frame);
+ return right.execute(frame);
+ }
+ }
+
+ abstract class TestChildNode extends Node {
+ abstract Object execute(VirtualFrame frame);
+ }
+
+ abstract class FrameSlotNode extends TestChildNode {
+ protected final FrameSlot slot;
+
+ public FrameSlotNode(FrameSlot slot) {
+ this.slot = slot;
+ }
+ }
+
+ class StringTestChildNode extends TestChildNode {
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ return "42";
+ }
+
+ }
+
+ class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener {
+ @Child private TestChildNode value;
+
+ IntAssignLocal(FrameSlot slot, TestChildNode value) {
+ super(slot);
+ this.value = adoptChild(value);
+ slot.registerOneShotTypeListener(this);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ Object o = value.execute(frame);
+ if (o instanceof Integer) {
+ frame.setInt(slot, (Integer) o);
+ } else {
+ slot.setType(Object.class);
+ frame.updateToLatestVersion();
+ frame.setObject(slot, o);
+ }
+ return null;
+ }
+
+ @Override
+ public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
+ if (changedSlot.getType() == Object.class) {
+ this.replace(new ObjectAssignLocal(changedSlot, value));
+ }
+ }
+ }
+
+ class ObjectAssignLocal extends FrameSlotNode {
+ @Child private TestChildNode value;
+
+ ObjectAssignLocal(FrameSlot slot, TestChildNode value) {
+ super(slot);
+ this.value = adoptChild(value);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ Object o = value.execute(frame);
+ frame.setObject(slot, o);
+ return null;
+ }
+ }
+
+ class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener {
+ IntReadLocal(FrameSlot slot) {
+ super(slot);
+ slot.registerOneShotTypeListener(this);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ return frame.getInt(slot);
+ }
+
+ @Override
+ public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
+ if (changedSlot.getType() == Object.class) {
+ this.replace(new ObjectReadLocal(changedSlot));
+ }
+ }
+ }
+
+ class ObjectReadLocal extends FrameSlotNode {
+ ObjectReadLocal(FrameSlot slot) {
+ super(slot);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ return frame.getObject(slot);
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/FrameTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,122 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ *
Storing Values in Frame Slots
+ *
+ *
+ * The frame is the preferred data structure for passing values between nodes. It can in particular be used for storing
+ * the values of local variables of the guest language. The {@link FrameDescriptor} represents the current structure of
+ * the frame. The method {@link FrameDescriptor#addFrameSlot(String, Class)} can be used to create predefined frame
+ * slots. The setter and getter methods in the {@link Frame} class can be used to access the current value of a
+ * particular frame slot.
+ *
+ *
+ *
+ * There are five primitive types for slots available: {@link java.lang.Boolean}, @{link java.lang.Integer},
+ * {@link java.lang.Long}, {@link java.lang.Float}, and {@link java.lang.Double}. It is encouraged to use those types
+ * whenever possible. Dynamically typed languages can speculate on the type of a value fitting into a primitive (see
+ * {@link FrameSlotTypeSpecializationTest}). When a frame slot is of one of those particular primitive types, its value
+ * may only be accessed with the repectively typed getter method ({@link Frame#getBoolean}, {@link Frame#getInt},
+ * {@link Frame#getLong}, {@link Frame#getFloat}, or {@link Frame#getDouble}) or setter method ({@link Frame#setBoolean},
+ * {@link Frame#setInt}, {@link Frame#setLong}, {@link Frame#setFloat}, or {@link Frame#setDouble}) in the
+ * {@link Frame} class.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at
+ * {@link com.oracle.truffle.api.test.FrameSlotTypeSpecializationTest}.
+ *
+ */
+public class FrameTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ FrameDescriptor frameDescriptor = new FrameDescriptor();
+ FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class);
+ TestRootNode rootNode = new TestRootNode(new AssignLocal(slot), new ReadLocal(slot));
+ CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor);
+ Object result = target.call();
+ Assert.assertEquals(42, result);
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Child TestChildNode left;
+ @Child TestChildNode right;
+
+ public TestRootNode(TestChildNode left, TestChildNode right) {
+ this.left = adoptChild(left);
+ this.right = adoptChild(right);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ return left.execute(frame) + right.execute(frame);
+ }
+ }
+
+ abstract class TestChildNode extends Node {
+ abstract int execute(VirtualFrame frame);
+ }
+
+ abstract class FrameSlotNode extends TestChildNode {
+ protected final FrameSlot slot;
+
+ public FrameSlotNode(FrameSlot slot) {
+ this.slot = slot;
+ }
+ }
+
+ class AssignLocal extends FrameSlotNode {
+ AssignLocal(FrameSlot slot) {
+ super(slot);
+ }
+
+ @Override
+ int execute(VirtualFrame frame) {
+ frame.setInt(slot, 42);
+ return 0;
+ }
+ }
+
+ class ReadLocal extends FrameSlotNode {
+ ReadLocal(FrameSlot slot) {
+ super(slot);
+ }
+
+ @Override
+ int execute(VirtualFrame frame) {
+ return frame.getInt(slot);
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReplaceTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReplaceTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+
+/**
+ *
Replacing Nodes at Run Time
+ *
+ *
+ * The structure of the Truffle tree can be changed at run time by replacing nodes using the {@link Node#replace(Node)}
+ * method. This method will automatically change the child pointer in the parent of the node and replace it with a
+ * pointer to the new node.
+ *
+ *
+ *
+ * Replacing nodes is a costly operation, so it should not happen too often. The convention is that the implementation
+ * of the Truffle nodes should ensure that there are maximal a small (and constant) number of node replacements per
+ * Truffle node.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.CallTest}.
+ *
+ */
+public class ReplaceTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ UnresolvedNode leftChild = new UnresolvedNode("20");
+ UnresolvedNode rightChild = new UnresolvedNode("22");
+ TestRootNode rootNode = new TestRootNode(new ValueNode[]{leftChild, rightChild});
+ assertEquals(rootNode, leftChild.getParent());
+ assertEquals(rootNode, rightChild.getParent());
+ Iterator iterator = rootNode.getChildren().iterator();
+ Assert.assertEquals(leftChild, iterator.next());
+ Assert.assertEquals(rightChild, iterator.next());
+ Assert.assertFalse(iterator.hasNext());
+ CallTarget target = runtime.createCallTarget(rootNode);
+ Object result = target.call();
+ assertEquals(42, result);
+ assertEquals(42, target.call());
+ iterator = rootNode.getChildren().iterator();
+ Assert.assertEquals(ResolvedNode.class, iterator.next().getClass());
+ Assert.assertEquals(ResolvedNode.class, iterator.next().getClass());
+ Assert.assertFalse(iterator.hasNext());
+ iterator = rootNode.getChildren().iterator();
+ Assert.assertEquals(rootNode, iterator.next().getParent());
+ Assert.assertEquals(rootNode, iterator.next().getParent());
+ Assert.assertFalse(iterator.hasNext());
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Children private ValueNode[] children;
+
+ public TestRootNode(ValueNode[] children) {
+ this.children = adoptChildren(children);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ int sum = 0;
+ for (int i = 0; i < children.length; ++i) {
+ sum += children[i].execute();
+ }
+ return sum;
+ }
+ }
+
+ abstract class ValueNode extends Node {
+ abstract int execute();
+ }
+
+ class UnresolvedNode extends ValueNode {
+ private final String value;
+
+ public UnresolvedNode(String value) {
+ this.value = value;
+ }
+
+ @Override
+ int execute() {
+ int intValue = Integer.parseInt(value);
+ ResolvedNode newNode = this.replace(new ResolvedNode(intValue));
+ return newNode.execute();
+ }
+ }
+
+ class ResolvedNode extends ValueNode {
+ private final int value;
+
+ ResolvedNode(int value) {
+ this.value = value;
+ }
+
+ @Override
+ int execute() {
+ return value;
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReturnTypeSpecializationTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ReturnTypeSpecializationTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,184 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ *
Specializing Return Types
+ *
+ *
+ * In order to avoid boxing and/or type casts on the return value of a node, the return value the method for executing a
+ * node can have a specific type and need not be of type {@link java.lang.Object}. For dynamically typed languages, this
+ * return type is something that should be speculated on. When the speculation fails and the child node cannot return
+ * the appropriate type of value, it can use an {@link UnexpectedResultException} to still pass the result to the
+ * caller. In such a case, the caller must rewrite itself to a more general version in oder to avoid future failures of
+ * this kind.
+ *
+ */
+public class ReturnTypeSpecializationTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ FrameDescriptor frameDescriptor = new FrameDescriptor();
+ FrameSlot slot = frameDescriptor.addFrameSlot("localVar", Integer.class);
+ TestRootNode rootNode = new TestRootNode(new IntAssignLocal(slot, new StringTestChildNode()), new IntReadLocal(slot));
+ CallTarget target = runtime.createCallTarget(rootNode, frameDescriptor);
+ Assert.assertEquals(Integer.class, slot.getType());
+ Object result = target.call();
+ Assert.assertEquals("42", result);
+ Assert.assertEquals(Object.class, slot.getType());
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Child TestChildNode left;
+ @Child TestChildNode right;
+
+ public TestRootNode(TestChildNode left, TestChildNode right) {
+ this.left = adoptChild(left);
+ this.right = adoptChild(right);
+ }
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ left.execute(frame);
+ return right.execute(frame);
+ }
+ }
+
+ abstract class TestChildNode extends Node {
+ abstract Object execute(VirtualFrame frame);
+
+ int executeInt(VirtualFrame frame) throws UnexpectedResultException {
+ Object result = execute(frame);
+ if (result instanceof Integer) {
+ return (Integer) result;
+ }
+ throw new UnexpectedResultException(result);
+ }
+ }
+
+ abstract class FrameSlotNode extends TestChildNode {
+ protected final FrameSlot slot;
+
+ public FrameSlotNode(FrameSlot slot) {
+ this.slot = slot;
+ }
+ }
+
+ class StringTestChildNode extends TestChildNode {
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ return "42";
+ }
+
+ }
+
+ class IntAssignLocal extends FrameSlotNode implements FrameSlotTypeListener {
+ @Child private TestChildNode value;
+
+ IntAssignLocal(FrameSlot slot, TestChildNode value) {
+ super(slot);
+ this.value = adoptChild(value);
+ slot.registerOneShotTypeListener(this);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ try {
+ frame.setInt(slot, value.executeInt(frame));
+ } catch (UnexpectedResultException e) {
+ slot.setType(Object.class);
+ frame.updateToLatestVersion();
+ frame.setObject(slot, e.getResult());
+ }
+ return null;
+ }
+
+ @Override
+ public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
+ if (changedSlot.getType() == Object.class) {
+ this.replace(new ObjectAssignLocal(changedSlot, value));
+ }
+ }
+ }
+
+ class ObjectAssignLocal extends FrameSlotNode {
+ @Child private TestChildNode value;
+
+ ObjectAssignLocal(FrameSlot slot, TestChildNode value) {
+ super(slot);
+ this.value = adoptChild(value);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ Object o = value.execute(frame);
+ frame.setObject(slot, o);
+ return null;
+ }
+ }
+
+ class IntReadLocal extends FrameSlotNode implements FrameSlotTypeListener {
+ IntReadLocal(FrameSlot slot) {
+ super(slot);
+ slot.registerOneShotTypeListener(this);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ return executeInt(frame);
+ }
+
+ @Override
+ int executeInt(VirtualFrame frame) {
+ return frame.getInt(slot);
+ }
+
+ @Override
+ public void typeChanged(FrameSlot changedSlot, Class< ? > oldType) {
+ if (changedSlot.getType() == Object.class) {
+ this.replace(new ObjectReadLocal(changedSlot));
+ }
+ }
+ }
+
+ class ObjectReadLocal extends FrameSlotNode {
+ ObjectReadLocal(FrameSlot slot) {
+ super(slot);
+ }
+
+ @Override
+ Object execute(VirtualFrame frame) {
+ return frame.getObject(slot);
+ }
+ }
+}
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/RootNodeTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/RootNodeTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ *
Creating a Root Node
+ *
+ *
+ * A Truffle root node is the entry point into a Truffle tree that represents a guest language method. It contains a
+ * {@link RootNode#execute(VirtualFrame)} method that can return a {@link java.lang.Object} value as the result of the
+ * guest language method invocation. This method must however never be called directly. Instead, the Truffle runtime
+ * must be used to create a {@link CallTarget} object from a root node using the
+ * {@link TruffleRuntime#createCallTarget(RootNode)} method. This call target object can then be executed using the
+ * {@link CallTarget#call()} method or one of its overloads.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.ChildNodeTest}.
+ *
+ */
+public class RootNodeTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ TestRootNode rootNode = new TestRootNode();
+ CallTarget target = runtime.createCallTarget(rootNode);
+ Object result = target.call();
+ Assert.assertEquals(42, result);
+ }
+
+ class TestRootNode extends RootNode {
+
+ @Override
+ public Object execute(VirtualFrame frame) {
+ return 42;
+ }
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/TruffleRuntimeTest.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/TruffleRuntimeTest.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test;
+
+import org.junit.*;
+
+import com.oracle.truffle.api.*;
+
+/**
+ *
Accessing the Truffle Runtime
+ *
+ *
+ * The Truffle runtime can be accessed at any point in time globally using the static method
+ * {@link Truffle#getRuntime()}. This method is guaranteed to return a non-null Truffle runtime object with an
+ * identifying name. A Java Virtual Machine implementation can chose to replace the default implementation of the
+ * {@link TruffleRuntime} interface with its own implementation for providing improved performance.
+ *
+ *
+ *
+ * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.RootNodeTest}.
+ *
+ */
+public class TruffleRuntimeTest {
+
+ @Test
+ public void test() {
+ TruffleRuntime runtime = Truffle.getRuntime();
+ Assert.assertNotNull(runtime);
+ Assert.assertNotNull(runtime.getName());
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/package-info.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/package-info.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/**
+ *
This package contains basic tests of the Truffle API and serves at the same
+ * time as an introduction to the Truffle API for language implementors. Every test gives an example on how to use the construct explained in the class description.
+ *
+ *
+ * Truffle is a language implementation framework. A guest language method is represented as a tree of executable nodes.
+ * The framework provides mechanisms for those trees to call each other. Additionally it contains dedicated data structures for storing data local to a tree invocation.
+ *
+ *
+ *
+ * This introduction to Truffle contains items in the following recommended order:
+ *
+ *
+ *
How to get access to the Truffle runtime? {@link com.oracle.truffle.api.test.TruffleRuntimeTest}
+ *
How to create a root node? {@link com.oracle.truffle.api.test.RootNodeTest}
+ *
How to create a child node and link it with its parent? {@link com.oracle.truffle.api.test.ChildNodeTest}
+ *
How to create an array of child nodes? {@link com.oracle.truffle.api.test.ChildrenNodesTest}
+ *
Why are final fields in node classes important? {@link com.oracle.truffle.api.test.FinalFieldTest}
+ *
How to replace one node with another node and what for? {@link com.oracle.truffle.api.test.ReplaceTest}
+ *
How to let one Truffle tree invoke another one? {@link com.oracle.truffle.api.test.CallTest}
+ *
How to pass arguments when executing a tree? {@link com.oracle.truffle.api.test.ArgumentsTest}
+ *
How to use frames and frame slots to store values local to an activation? {@link com.oracle.truffle.api.test.FrameTest}
+ *
How to use type specialization and speculation for frame slots? {@link com.oracle.truffle.api.test.FrameSlotTypeSpecializationTest}
+ *
How to use type specialization and speculation for node return values? {@link com.oracle.truffle.api.test.ReturnTypeSpecializationTest}
+ *
+ *
+ *
+ *
+ */
+package com.oracle.truffle.api.test;
+
+
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/Arguments.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/Arguments.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api;
+
+/**
+ * Base class for arguments passed to guest language methods via the
+ * {@link CallTarget#call(com.oracle.truffle.api.frame.PackedFrame, Arguments)} method. A guest language create a
+ * subclass with immutable fields representing the arguments passed to a guest language method. The {@link Arguments}
+ * object must be created immediately before a method call and it must not be stored in a field or cast to
+ * {@link java.lang.Object}.
+ */
+public class Arguments {
+
+ /**
+ * Constant that can be used as an argument to
+ * {@link CallTarget#call(com.oracle.truffle.api.frame.PackedFrame, Arguments)} in case no arguments should be
+ * supplied.
+ */
+ public static final Arguments EMPTY_ARGUMENTS = new Arguments();
+
+ /**
+ * Constructs an empty {@link Arguments} instance. Guest languages should create a subclass to specify their own arguments.
+ */
+ protected Arguments() {
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CallTarget.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CallTarget.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api;
+
+import com.oracle.truffle.api.frame.*;
+
+/**
+ * Represents the target of a call. Instances of this interface can be created using the
+ * {@link TruffleRuntime#createCallTarget(com.oracle.truffle.api.nodes.RootNode, FrameDescriptor)} method.
+ */
+public abstract class CallTarget {
+
+ /**
+ * Calls this target as a root method and without arguments.
+ * @return the return result of the call
+ */
+ public final Object call() {
+ return call(null, Arguments.EMPTY_ARGUMENTS);
+ }
+
+ /**
+ * Calls this target with a caller frame and no arguments.
+ * @param caller the caller frame
+ * @return the return result of the call
+ */
+ public final Object call(PackedFrame caller) {
+ return call(caller, Arguments.EMPTY_ARGUMENTS);
+ }
+
+ /**
+ * Calls this target as a root method passing arguments.
+ * @param arguments the arguments that should be passed to the callee
+ * @return the return result of the call
+ */
+ public final Object call(Arguments arguments) {
+ return call(null, arguments);
+ }
+
+ /**
+ * Calls this target passing a caller frame and arguments.
+ * @param caller the caller frame
+ * @param arguments the arguments that should be passed to the callee
+ * @return the return result of the call
+ */
+ public abstract Object call(PackedFrame caller, Arguments arguments);
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/Truffle.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/Truffle.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api;
+
+import com.oracle.truffle.api.impl.*;
+
+/**
+ * Class for obtaining the Truffle runtime singleton object of this virtual machine.
+ */
+public class Truffle {
+
+ private static TruffleRuntime runtime;
+
+ private static native TruffleRuntime initializeRuntime();
+
+ public static TruffleRuntime getRuntime() {
+ return runtime;
+ }
+
+ static {
+ try {
+ runtime = initializeRuntime();
+ } catch (UnsatisfiedLinkError e) {
+ runtime = new DefaultTruffleRuntime();
+ }
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleRuntime.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/TruffleRuntime.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api;
+
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ * Interface representing a Truffle runtime object. The runtime is responsible for creating call targets and performing optimizations for them.
+ */
+public interface TruffleRuntime {
+ /**
+ * Name describing this runtime implementation for debugging purposes.
+ * @return the name as a String
+ */
+ String getName();
+
+ /**
+ * Creates a new call target for a given root node.
+ * @param rootNode the root node whose {@link RootNode#execute(com.oracle.truffle.api.frame.VirtualFrame)} method represents the entry point
+ * @return the new call target object
+ */
+ CallTarget createCallTarget(RootNode rootNode);
+
+ /**
+ * Creates a new call target for a given root node and a given frame descriptor.
+ * @param rootNode the root node whose {@link RootNode#execute(com.oracle.truffle.api.frame.VirtualFrame)} method represents the entry point
+ * @param frameDescriptor the descriptor used for creating a new frame at each invocation
+ * @return the new call target object
+ */
+ CallTarget createCallTarget(RootNode rootNode, FrameDescriptor frameDescriptor);
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/DefaultTypeConversion.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/DefaultTypeConversion.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * Default type conversion semantics where a conversion is without changing any data.
+ */
+public final class DefaultTypeConversion implements TypeConversion {
+
+ private static DefaultTypeConversion instance = new DefaultTypeConversion();
+
+ public static TypeConversion getInstance() {
+ return instance;
+ }
+
+ private DefaultTypeConversion() {
+
+ }
+
+ @Override
+ public Class< ? > getTopType() {
+ return Object.class;
+ }
+
+ @Override
+ public Object convertTo(Class< ? > targetType, Object value) {
+ return value;
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/Frame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/Frame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+import com.oracle.truffle.api.*;
+
+/**
+ * Represents a frame containing values of local variables of the guest language. Instances of this type must not be
+ * stored in a field or cast to {@link java.lang.Object}.
+ */
+public interface Frame {
+
+ /**
+ * @return the arguments used when calling this method
+ */
+ Arguments getArguments();
+
+ /**
+ * Read access to a local variable of type {@link Object}.
+ *
+ * @param slot the slot of the local variable
+ * @return the current value of the local variable
+ */
+ Object getObject(FrameSlot slot);
+
+ /**
+ * Write access to a local variable of type {@link Object}.
+ *
+ * @param slot the slot of the local variable
+ * @param value the new value of the local variable
+ */
+ void setObject(FrameSlot slot, Object value);
+
+ /**
+ * Read access to a local variable of type boolean.
+ *
+ * @param slot the slot of the local variable
+ * @return the current value of the local variable
+ */
+ boolean getBoolean(FrameSlot slot);
+
+ /**
+ * Write access to a local variable of type boolean.
+ *
+ * @param slot the slot of the local variable
+ * @param value the new value of the local variable
+ */
+ void setBoolean(FrameSlot slot, boolean value);
+
+ /**
+ * Read access to a local variable of type int.
+ *
+ * @param slot the slot of the local variable
+ * @return the current value of the local variable
+ */
+ int getInt(FrameSlot slot);
+
+ /**
+ * Write access to a local variable of type int.
+ *
+ * @param slot the slot of the local variable
+ * @param value the new value of the local variable
+ */
+ void setInt(FrameSlot slot, int value);
+
+ /**
+ * Read access to a local variable of type long.
+ *
+ * @param slot the slot of the local variable
+ * @return the current value of the local variable
+ */
+ long getLong(FrameSlot slot);
+
+ /**
+ * Write access to a local variable of type long.
+ *
+ * @param slot the slot of the local variable
+ * @param value the new value of the local variable
+ */
+ void setLong(FrameSlot slot, long value);
+
+ /**
+ * Read access to a local variable of type float.
+ *
+ * @param slot the slot of the local variable
+ * @return the current value of the local variable
+ */
+ float getFloat(FrameSlot slot);
+
+ /**
+ * Write access to a local variable of type float.
+ *
+ * @param slot the slot of the local variable
+ * @param value the new value of the local variable
+ */
+ void setFloat(FrameSlot slot, float value);
+
+ /**
+ * Read access to a local variable of type double.
+ *
+ * @param slot the slot of the local variable
+ * @return the current value of the local variable
+ */
+ double getDouble(FrameSlot slot);
+
+ /**
+ * Write access to a local variable of type double.
+ *
+ * @param slot the slot of the local variable
+ * @param value the new value of the local variable
+ */
+ void setDouble(FrameSlot slot, double value);
+
+ void updateToLatestVersion();
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameDescriptor.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameDescriptor.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,201 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+import java.util.*;
+
+/**
+ * Descriptor of the slots of frame objects. Multiple frame instances are associated with one such descriptor.
+ */
+public final class FrameDescriptor {
+
+ protected final TypeConversion typeConversion;
+ private final ArrayList slots;
+ private FrameVersionImpl lastVersion;
+ private final HashMap nameToSlotMap;
+
+ public FrameDescriptor() {
+ this(DefaultTypeConversion.getInstance());
+ }
+
+ public FrameDescriptor(TypeConversion typeConversion) {
+ this.typeConversion = typeConversion;
+ slots = new ArrayList<>();
+ nameToSlotMap = new HashMap<>();
+ lastVersion = new FrameVersionImpl();
+ }
+
+ public FrameSlot addFrameSlot(String name) {
+ return addFrameSlot(name, typeConversion.getTopType());
+ }
+
+ public FrameSlot addFrameSlot(String name, Class> type) {
+ assert !nameToSlotMap.containsKey(name);
+ FrameSlotImpl slot = new FrameSlotImpl(this, name, slots.size(), type);
+ slots.add(slot);
+ nameToSlotMap.put(name, slot);
+ return slot;
+ }
+
+ public FrameSlot findFrameSlot(String name) {
+ return nameToSlotMap.get(name);
+ }
+
+ public FrameSlot findOrAddFrameSlot(String name) {
+ FrameSlot result = findFrameSlot(name);
+ if (result != null) {
+ return result;
+ }
+ return addFrameSlot(name);
+ }
+
+ public FrameVersion getCurrentVersion() {
+ return lastVersion;
+ }
+
+ public int getSize() {
+ return slots.size();
+ }
+
+ public List< ? extends FrameSlot> getSlots() {
+ return Collections.unmodifiableList(slots);
+ }
+
+ protected void appendVersion(FrameVersionImpl newVersion) {
+ lastVersion.next = newVersion;
+ lastVersion = newVersion;
+ }
+}
+
+class FrameVersionImpl implements FrameVersion {
+
+ protected FrameVersionImpl next;
+
+ @Override
+ public final FrameVersion getNext() {
+ return next;
+ }
+}
+
+class TypeChangeFrameVersionImpl extends FrameVersionImpl implements FrameVersion.TypeChange {
+
+ private final FrameSlotImpl slot;
+ private final Class< ? > oldType;
+ private final Class< ? > newType;
+
+ protected TypeChangeFrameVersionImpl(FrameSlotImpl slot, Class< ? > oldType, Class< ? > newType) {
+ this.slot = slot;
+ this.oldType = oldType;
+ this.newType = newType;
+ }
+
+ @Override
+ public final void applyTransformation(Frame frame) {
+ Object value = slot.getValue(oldType, frame);
+ slot.setValue(newType, frame, value);
+ }
+}
+
+class FrameSlotImpl implements FrameSlot {
+
+ private final FrameDescriptor descriptor;
+ private final String name;
+ private final int index;
+ private Class< ? > type;
+ private ArrayList listeners;
+
+ protected FrameSlotImpl(FrameDescriptor descriptor, String name, int index, Class< ? > type) {
+ this.descriptor = descriptor;
+ this.name = name;
+ this.index = index;
+ this.type = type;
+ assert type != null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public Class< ? > getType() {
+ return type;
+ }
+
+ protected Object getValue(Class< ? > accessType, Frame frame) {
+ if (accessType == Integer.class) {
+ return frame.getInt(this);
+ } else if (accessType == Long.class) {
+ return frame.getLong(this);
+ } else if (accessType == Float.class) {
+ return frame.getFloat(this);
+ } else if (accessType == Double.class) {
+ return frame.getDouble(this);
+ } else {
+ return frame.getObject(this);
+ }
+ }
+
+ protected void setValue(Class< ? > accessType, Frame frame, Object value) {
+ Object newValue = descriptor.typeConversion.convertTo(accessType, value);
+ if (accessType == Integer.class) {
+ frame.setInt(this, (Integer) newValue);
+ } else if (accessType == Long.class) {
+ frame.setLong(this, (Long) newValue);
+ } else if (accessType == Float.class) {
+ frame.setFloat(this, (Float) newValue);
+ } else if (accessType == Double.class) {
+ frame.setDouble(this, (Double) newValue);
+ } else {
+ frame.setObject(this, newValue);
+ }
+ }
+
+ public void setType(final Class< ? > type) {
+ final Class< ? > oldType = this.type;
+ this.type = type;
+ ArrayList oldListeners = this.listeners;
+ this.listeners = null;
+ if (oldListeners != null) {
+ for (FrameSlotTypeListener listener : oldListeners) {
+ listener.typeChanged(this, oldType);
+ }
+ }
+ descriptor.appendVersion(new TypeChangeFrameVersionImpl(this, oldType, type));
+ }
+
+ @Override
+ public String toString() {
+ return "[" + index + "," + name + "]";
+ }
+
+ @Override
+ public void registerOneShotTypeListener(FrameSlotTypeListener listener) {
+ if (listeners == null) {
+ listeners = new ArrayList<>();
+ }
+ listeners.add(listener);
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlot.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlot.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * A slot in a frame that can store a value of a given type.
+ */
+public interface FrameSlot {
+ String getName();
+ int getIndex();
+ Class< ? > getType();
+ void setType(Class< ? > type);
+ void registerOneShotTypeListener(FrameSlotTypeListener listener);
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlotTypeListener.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameSlotTypeListener.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * Listener for the event of a type change of a frame slot.
+ */
+public interface FrameSlotTypeListener {
+ void typeChanged(FrameSlot slot, Class< ? > oldType);
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameVersion.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/FrameVersion.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * Represents a specific version of a frame.
+ */
+public interface FrameVersion {
+
+ FrameVersion getNext();
+
+ public interface Resize {
+
+ int getNewSize();
+ }
+
+ public interface TypeChange {
+
+ void applyTransformation(Frame frame);
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/MaterializedFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/MaterializedFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * Represents a materialized frame containing values of local variables of the guest language. It can be created using
+ * the {@link VirtualFrame#materialize()} method. Instances of this type are the only frame instances that may be stored
+ * in fields or cast to {@link java.lang.Object}. In contrast to a {@link VirtualFrame}, a {@link MaterializedFrame} can
+ * no longer be packed and it also does not provide access to the caller frame.
+ */
+public interface MaterializedFrame extends Frame {
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/NativeFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/NativeFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+import com.oracle.truffle.api.*;
+
+/**
+ * Represents a native frame without any local variables. Instances of this type must not be stored in a field or cast
+ * to {@link java.lang.Object}.
+ */
+public class NativeFrame implements VirtualFrame, PackedFrame {
+
+ private PackedFrame caller;
+ private Arguments arguments;
+
+ public NativeFrame(PackedFrame caller, Arguments arguments) {
+ this.caller = caller;
+ this.arguments = arguments;
+ }
+
+ @Override
+ public Arguments getArguments() {
+ return arguments;
+ }
+
+ @Override
+ public Object getObject(FrameSlot slot) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public void setObject(FrameSlot slot, Object value) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public boolean getBoolean(FrameSlot slot) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public void setBoolean(FrameSlot slot, boolean value) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public int getInt(FrameSlot slot) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public void setInt(FrameSlot slot, int value) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public long getLong(FrameSlot slot) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public void setLong(FrameSlot slot, long value) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public float getFloat(FrameSlot slot) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public void setFloat(FrameSlot slot, float value) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public double getDouble(FrameSlot slot) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public void setDouble(FrameSlot slot, double value) {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public PackedFrame pack() {
+ return this;
+ }
+
+ @Override
+ public PackedFrame getCaller() {
+ return caller;
+ }
+
+ @Override
+ public MaterializedFrame materialize() {
+ throw new UnsupportedOperationException("native frame");
+ }
+
+ @Override
+ public VirtualFrame unpack() {
+ return this;
+ }
+
+ @Override
+ public void updateToLatestVersion() {
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/PackedFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/PackedFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * Represents a packed frame that represents a virtual frame. A packed frame instance can be retrieved with the
+ * {@link VirtualFrame#pack()} method. It can be converted back into a virtual frame using {@link PackedFrame#unpack()}.
+ * Instances of this type must not be stored in a field or cast to {@link java.lang.Object}.
+ */
+public interface PackedFrame {
+
+ /**
+ * Unpacks this frame and converts it back to a virtual frame.
+ * @return the virtual frame that was the content of this packed frame
+ */
+ VirtualFrame unpack();
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/TypeConversion.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/TypeConversion.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+/**
+ * Interface for defining type conversions for frame slot values.
+ */
+public interface TypeConversion {
+ Class> getTopType();
+
+ Object convertTo(Class> targetType, Object value);
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/VirtualFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/frame/VirtualFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.frame;
+
+import com.oracle.truffle.api.*;
+
+/**
+ * Represents a frame containing values of local variables of the guest language. Instances of this type must not be
+ * stored in a field or cast to {@link java.lang.Object}. If this is necessary, the frame must be explicitly converted
+ * into a materialized frame using the {@link VirtualFrame#materialize()} method. Whenever fast access to the local
+ * variables of a frame is no longer necessary, a virtual frame should be converted into a packed frame using the
+ * {@link VirtualFrame#pack()} method.
+ */
+public interface VirtualFrame extends Frame {
+
+ /**
+ * Converts this virtual frame into a packed frame that has no longer direct access to the local variables. This
+ * packing is an important hint to the Truffle optimizer and therefore passing around a {@link PackedFrame} should
+ * be preferred over passing around a {@link VirtualFrame} when the probability that an unpacking will occur is low.
+ *
+ * @return the packed frame
+ */
+ PackedFrame pack();
+
+ /**
+ * Accesses the caller frame passed in via {@link CallTarget#call}. To get full access, it must be first unpacked
+ * using {@link PackedFrame#unpack()}.
+ *
+ * @return the caller frame or null if this was a root method call
+ */
+ PackedFrame getCaller();
+
+ /**
+ * Materializes this frame, which allows it to be stored in a field or cast to {@link java.lang.Object}. The frame
+ * however looses the ability to be packed or to access the caller frame.
+ *
+ * @return the new materialized frame
+ */
+ MaterializedFrame materialize();
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultCallTarget.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultCallTarget.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.impl;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+final class DefaultCallTarget extends CallTarget {
+
+ protected final RootNode rootNode;
+ protected final FrameDescriptor frameDescriptor;
+
+ DefaultCallTarget(RootNode function, FrameDescriptor frameDescriptor) {
+ this.rootNode = function;
+ this.frameDescriptor = frameDescriptor;
+ }
+
+ @Override
+ public String toString() {
+ return "DefaultCallTarget " + rootNode;
+ }
+
+ @Override
+ public Object call(PackedFrame caller, Arguments args) {
+ VirtualFrame frame = new DefaultVirtualFrame(frameDescriptor, caller, args);
+ return rootNode.execute(frame);
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultMaterializedFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultMaterializedFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.impl;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+
+final class DefaultMaterializedFrame implements MaterializedFrame {
+ private final DefaultVirtualFrame wrapped;
+
+ protected DefaultMaterializedFrame(DefaultVirtualFrame wrapped) {
+ this.wrapped = wrapped;
+ }
+
+ @Override
+ public Arguments getArguments() {
+ return wrapped.getArguments();
+ }
+
+ @Override
+ public Object getObject(FrameSlot slot) {
+ return wrapped.getObject(slot);
+ }
+
+ @Override
+ public void setObject(FrameSlot slot, Object value) {
+ wrapped.setObject(slot, value);
+ }
+
+ @Override
+ public boolean getBoolean(FrameSlot slot) {
+ return wrapped.getBoolean(slot);
+ }
+
+ @Override
+ public void setBoolean(FrameSlot slot, boolean value) {
+ wrapped.setBoolean(slot, value);
+ }
+
+ @Override
+ public int getInt(FrameSlot slot) {
+ return wrapped.getInt(slot);
+ }
+
+ @Override
+ public void setInt(FrameSlot slot, int value) {
+ wrapped.setInt(slot, value);
+ }
+
+ @Override
+ public long getLong(FrameSlot slot) {
+ return wrapped.getLong(slot);
+ }
+
+ @Override
+ public void setLong(FrameSlot slot, long value) {
+ wrapped.setLong(slot, value);
+ }
+
+ @Override
+ public float getFloat(FrameSlot slot) {
+ return wrapped.getFloat(slot);
+ }
+
+ @Override
+ public void setFloat(FrameSlot slot, float value) {
+ wrapped.setFloat(slot, value);
+ }
+
+ @Override
+ public double getDouble(FrameSlot slot) {
+ return wrapped.getDouble(slot);
+ }
+
+ @Override
+ public void setDouble(FrameSlot slot, double value) {
+ wrapped.setDouble(slot, value);
+ }
+
+ @Override
+ public void updateToLatestVersion() {
+ wrapped.updateToLatestVersion();
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultPackedFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultPackedFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.impl;
+
+import com.oracle.truffle.api.frame.*;
+
+final class DefaultPackedFrame implements PackedFrame {
+ private final DefaultVirtualFrame wrapped;
+
+ protected DefaultPackedFrame(DefaultVirtualFrame wrapped) {
+ this.wrapped = wrapped;
+ }
+
+ @Override
+ public VirtualFrame unpack() {
+ return wrapped;
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultTruffleRuntime.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultTruffleRuntime.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.impl;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+import com.oracle.truffle.api.nodes.*;
+
+/**
+ * Default implementation of the Truffle runtime if the virtual machine does not provide a better performing alternative.
+ */
+public final class DefaultTruffleRuntime implements TruffleRuntime {
+
+ @Override
+ public String getName() {
+ return "Default Truffle Runtime";
+ }
+
+ @Override
+ public CallTarget createCallTarget(RootNode rootNode, FrameDescriptor frameDescriptor) {
+ return new DefaultCallTarget(rootNode, frameDescriptor);
+ }
+
+ @Override
+ public CallTarget createCallTarget(RootNode rootNode) {
+ return createCallTarget(rootNode, new FrameDescriptor());
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultVirtualFrame.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/DefaultVirtualFrame.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.impl;
+
+import java.util.*;
+
+import com.oracle.truffle.api.*;
+import com.oracle.truffle.api.frame.*;
+
+final class DefaultVirtualFrame implements VirtualFrame {
+
+ private static final Object UNDEFINED_OBJECT = null;
+ private static final Boolean UNDEFINED_BOOLEAN = false;
+ private static final Integer UNDEFINED_INTEGER = 0;
+ private static final Float UNDEFINED_FLOAT = 0.0f;
+ private static final Long UNDEFINED_LONG = 0L;
+ private static final Double UNDEFINED_DOUBLE = 0.0d;
+
+ private final FrameDescriptor descriptor;
+ private final PackedFrame caller;
+ private final Arguments arguments;
+ private FrameVersion currentVersion;
+ protected Object[] locals;
+ protected Class[] tags;
+
+ DefaultVirtualFrame(FrameDescriptor descriptor, PackedFrame caller, Arguments arguments) {
+ this.descriptor = descriptor;
+ this.caller = caller;
+ this.arguments = arguments;
+ this.currentVersion = descriptor.getCurrentVersion();
+ this.locals = new Object[descriptor.getSize()];
+ // The tags are only needed for assertion checking, so initialize the field only when assertions are enabled
+ assert (this.tags = new Class[descriptor.getSize()]) != null;
+ }
+
+ @Override
+ public Arguments getArguments() {
+ return arguments;
+ }
+
+ @Override
+ public PackedFrame getCaller() {
+ return caller;
+ }
+
+ @Override
+ public PackedFrame pack() {
+ return new DefaultPackedFrame(this);
+ }
+
+ @Override
+ public MaterializedFrame materialize() {
+ return new DefaultMaterializedFrame(this);
+ }
+
+ @Override
+ public Object getObject(FrameSlot slot) {
+ return get(slot, Object.class, UNDEFINED_OBJECT);
+ }
+
+ @Override
+ public void setObject(FrameSlot slot, Object value) {
+ set(slot, Object.class, value);
+ }
+
+ @Override
+ public boolean getBoolean(FrameSlot slot) {
+ return (Boolean) get(slot, Float.class, UNDEFINED_BOOLEAN);
+ }
+
+ @Override
+ public void setBoolean(FrameSlot slot, boolean value) {
+ set(slot, Float.class, value);
+ }
+
+ @Override
+ public int getInt(FrameSlot slot) {
+ return (Integer) get(slot, Integer.class, UNDEFINED_INTEGER);
+ }
+
+ @Override
+ public void setInt(FrameSlot slot, int value) {
+ set(slot, Integer.class, value);
+ }
+
+ @Override
+ public long getLong(FrameSlot slot) {
+ return (Long) get(slot, Long.class, UNDEFINED_LONG);
+ }
+
+ @Override
+ public void setLong(FrameSlot slot, long value) {
+ set(slot, Long.class, value);
+ }
+
+ @Override
+ public float getFloat(FrameSlot slot) {
+ return (Float) get(slot, Float.class, UNDEFINED_FLOAT);
+ }
+
+ @Override
+ public void setFloat(FrameSlot slot, float value) {
+ set(slot, Float.class, value);
+ }
+
+ @Override
+ public double getDouble(FrameSlot slot) {
+ return (Double) get(slot, Double.class, UNDEFINED_DOUBLE);
+ }
+
+ @Override
+ public void setDouble(FrameSlot slot, double value) {
+ set(slot, Double.class, value);
+ }
+
+ private Object get(FrameSlot slot, Class< ? > accessType, Object defaultValue) {
+ Object value = locals[slot.getIndex()];
+ assert verifyGet(slot, accessType, value);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ return value;
+ }
+ }
+
+ private boolean verifyGet(FrameSlot slot, Class< ? > accessType, Object value) {
+ assert descriptor.getSlots().get(slot.getIndex()) == slot;
+ Class< ? > tag = tags[slot.getIndex()];
+ if (value == null) {
+ assert tag == null || tag == Object.class;
+ } else {
+ assert tag == accessType : "Local variable " + slot + " was written with set" + tag.getSimpleName() + ", but is read with get" + accessType.getSimpleName();
+ }
+ return true;
+ }
+
+ private void set(FrameSlot slot, Class< ? > accessType, Object value) {
+ assert verifySet(slot, accessType, value);
+ locals[slot.getIndex()] = value;
+ }
+
+ private boolean verifySet(FrameSlot slot, Class< ? > accessType, Object value) {
+ assert descriptor.getSlots().get(slot.getIndex()) == slot;
+ tags[slot.getIndex()] = accessType;
+ assert accessType.isAssignableFrom(slot.getType()) : "Local variable " + slot + ": " + accessType + " is not assignable from " + slot.getType();
+ if (value == null) {
+ assert accessType == Object.class;
+ } else {
+ assert slot.getType().isAssignableFrom(value.getClass()) : "Local variable " + slot + ": " + slot.getType() + " is not assignable from " + value.getClass();
+ }
+ return true;
+ }
+
+ @Override
+ public void updateToLatestVersion() {
+ if (currentVersion.getNext() != null) {
+ doUpdateToLatestVersion();
+ }
+ }
+
+ private void doUpdateToLatestVersion() {
+ FrameVersion version = currentVersion;
+ while (version.getNext() != null) {
+ version = version.getNext();
+ if (version instanceof FrameVersion.TypeChange) {
+ ((FrameVersion.TypeChange) version).applyTransformation(this);
+ } else if (version instanceof FrameVersion.Resize) {
+ int newSize = ((FrameVersion.Resize) version).getNewSize();
+ locals = Arrays.copyOf(locals, newSize);
+ }
+ }
+ currentVersion = version;
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/intrinsics/ExactMath.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/intrinsics/ExactMath.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.intrinsics;
+
+/**
+ * This class contains methods that will be part of java.lang.Math starting with JDK 8. Until JDK 8 is release, we
+ * duplicate them here because they are generally useful for dynamic language implementations.
+ */
+public class ExactMath {
+
+ public static int addExact(int x, int y) {
+ int r = x + y;
+ // HD 2-12 Overflow iff both arguments have the opposite sign of the result
+ if (((x ^ r) & (y ^ r)) < 0) {
+ throw new ArithmeticException("integer overflow");
+ }
+ return r;
+ }
+
+ public static long addExact(long x, long y) {
+ long r = x + y;
+ // HD 2-12 Overflow iff both arguments have the opposite sign of the result
+ if (((x ^ r) & (y ^ r)) < 0) {
+ throw new ArithmeticException("long overflow");
+ }
+ return r;
+ }
+
+ public static int subtractExact(int x, int y) {
+ int r = x - y;
+ // HD 2-12 Overflow iff the arguments have different signs and
+ // the sign of the result is different than the sign of x
+ if (((x ^ y) & (x ^ r)) < 0) {
+ throw new ArithmeticException("integer overflow");
+ }
+ return r;
+ }
+
+ public static long subtractExact(long x, long y) {
+ long r = x - y;
+ // HD 2-12 Overflow iff the arguments have different signs and
+ // the sign of the result is different than the sign of x
+ if (((x ^ y) & (x ^ r)) < 0) {
+ throw new ArithmeticException("long overflow");
+ }
+ return r;
+ }
+
+ public static int multiplyExact(int x, int y) {
+ long r = (long) x * (long) y;
+ if ((int) r != r) {
+ throw new ArithmeticException("long overflow");
+ }
+ return (int) r;
+ }
+
+ public static long multiplyExact(long x, long y) {
+ long r = x * y;
+ long ax = Math.abs(x);
+ long ay = Math.abs(y);
+ if (((ax | ay) >>> 31 != 0)) {
+ // Some bits greater than 2^31 that might cause overflow
+ // Check the result using the divide operator
+ // and check for the special case of Long.MIN_VALUE * -1
+ if (((y != 0) && (r / y != x)) || (x == Long.MIN_VALUE && y == -1)) {
+ throw new ArithmeticException("long overflow");
+ }
+ }
+ return r;
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/intrinsics/TruffleIntrinsics.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/intrinsics/TruffleIntrinsics.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.intrinsics;
+
+/**
+ * Predefined Truffle intrinsics that allow direct influence of the generated machine code.
+ */
+public final class TruffleIntrinsics {
+
+ /**
+ * Specifies that the compiler should put a deoptimization point at this position that will continue execution in the interpreter.
+ * Should be used to cut off cold paths that should not be part of the compiled machine code.
+ */
+ public static void deoptimize() {
+ }
+
+ /**
+ * Checks whether the Thread has been interrupted in the interpreter in order to avoid endless loops. The compiled code may choose a more efficient implementation.
+ */
+ public static void checkThreadInterrupted() {
+ if (Thread.currentThread().isInterrupted()) {
+ throw new RuntimeException("Timeout");
+ }
+ }
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/ExplodeLoop.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/ExplodeLoop.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes;
+
+import java.lang.annotation.*;
+
+/**
+ * Specifies for a method that the loops with constant number of invocations should be fully unrolled.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface ExplodeLoop {
+}
diff -r 7f6bf286629c -r 19f5c9b96fa7 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java Tue Dec 18 17:59:32 2012 +0100
@@ -0,0 +1,385 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes;
+
+import java.io.*;
+import java.lang.annotation.*;
+import java.lang.reflect.*;
+import java.net.*;
+import java.util.*;
+
+import javax.xml.parsers.*;
+import javax.xml.transform.*;
+import javax.xml.transform.dom.*;
+import javax.xml.transform.stream.*;
+
+import org.w3c.dom.*;
+
+/**
+ * Utility class for creating output for the ideal graph visualizer.
+ */
+public class GraphPrintVisitor {
+
+ public static final String GraphVisualizerAddress = "127.0.0.1";
+ public static final int GraphVisualizerPort = 4444;
+
+ private Document dom;
+ private Map