# HG changeset patch # User Michael Haupt # Date 1398259398 -7200 # Node ID 1cd02b4d90d13e69362e046dac06db8333afc121 # Parent 820c6d3533582e32a1bc1e66722f08a6834fc796 onAdopt callback for ASTs diff -r 820c6d353358 -r 1cd02b4d90d1 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/OnAdoptTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/OnAdoptTest.java Wed Apr 23 15:23:18 2014 +0200 @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2014, 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.*; + +/** + *

Inserting Extra Nodes into the AST Transparently

+ * + *

+ * The {@link Node} class provides a callback that is invoked whenever a node is adopted in an AST + * by insertion or replacement. Node classes can override the {@code onAdopt()} method to run extra + * functionality upon adoption. + *

+ * + *

+ * This test demonstrates how node instances of a specific class can be automatically wrapped in + * extra nodes when they are inserted into the AST. + *

+ */ +public class OnAdoptTest { + + static class Root extends RootNode { + + @Child private Base child1; + @Child private Base child2; + + public Root(Base child1, Base child2) { + super(null); + this.child1 = child1; + this.child2 = child2; + } + + @Override + public Object execute(VirtualFrame frame) { + return child1.executeInt(frame) + child2.executeInt(frame); + } + + } + + abstract static class Base extends Node { + public abstract int executeInt(VirtualFrame frame); + } + + static class Wrapper extends Base { + + @Child private Base wrappee; + + public Wrapper(Base wrappee) { + this.wrappee = wrappee; + } + + @Override + public int executeInt(VirtualFrame frame) { + return 1 + wrappee.executeInt(frame); + } + + } + + abstract static class GenBase extends Base { + + private final int k; + + public GenBase(int k) { + this.k = k; + } + + @Override + public int executeInt(VirtualFrame frame) { + return k; + } + + } + + static class Gen extends GenBase { + public Gen(int k) { + super(k); + } + } + + static class GenWrapped extends GenBase { + + public GenWrapped(int k) { + super(k); + } + + @Override + protected void onAdopt() { + Wrapper w = new Wrapper(this); + this.replace(w); + } + + } + + @Test + public void testOnInsert() { + TruffleRuntime runtime = Truffle.getRuntime(); + Base b1 = new Gen(11); + Base b2 = new GenWrapped(11); + Root r = new Root(b1, b2); + CallTarget ct = runtime.createCallTarget(r); + Object result = ct.call(); + Assert.assertEquals(23, result); + } + +} diff -r 820c6d353358 -r 1cd02b4d90d1 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Wed Apr 23 12:38:05 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Wed Apr 23 15:23:18 2014 +0200 @@ -191,8 +191,12 @@ if (newChild == this) { throw new IllegalStateException("The parent of a node can never be the node itself."); } + boolean isInserted = newChild.parent == null; newChild.parent = this; newChild.adoptHelper(); + if (isInserted) { + newChild.onAdopt(); + } } private void adoptHelper() { @@ -209,8 +213,12 @@ if (newChild == this) { throw new IllegalStateException("The parent of a node can never be the node itself."); } + boolean isInserted = newChild.parent == null; newChild.parent = this; newChild.adoptUnadoptedHelper(); + if (isInserted) { + newChild.onAdopt(); + } } private void adoptUnadoptedHelper() { @@ -356,6 +364,16 @@ out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s.%n", this.toString(), formatNodeInfo(this), formatNodeInfo(newNode), reason); } + /** + * Subclasses of {@link Node} can implement this method to execute extra functionality when a + * node is effectively inserted into the AST. The {@code onAdopt} callback is called after the + * node has been effectively inserted, and it is guaranteed to be called only once for any given + * node. + */ + protected void onAdopt() { + // empty default + } + private static String formatNodeInfo(Node node) { String cost = "?"; switch (node.getCost()) {