# HG changeset patch # User Andreas Woess # Date 1383742465 -3600 # Node ID ffd4b6b4ae687fb252e23fd5bacdf22b79cc08be # Parent 426786412db6030f566a1d1925b97802900295ce Truffle Node class refactoring. diff -r 426786412db6 -r ffd4b6b4ae68 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 Nov 06 13:38:09 2013 +0100 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Wed Nov 06 13:54:25 2013 +0100 @@ -24,6 +24,7 @@ */ package com.oracle.truffle.api.nodes; +import java.io.*; import java.lang.annotation.*; import java.util.*; @@ -184,6 +185,7 @@ if (!NodeUtil.replaceChild(this.parent, this, newNode)) { fixupTree(); } + reportReplace(); return newNode; } @@ -193,7 +195,7 @@ * This is a rather expensive operation but rare to occur. */ private void fixupTree() { - Node rootNode = NodeUtil.findParent(this, RootNode.class); + Node rootNode = getRootNode(); if (rootNode == null) { throw new UnsupportedOperationException("Tree does not have a root node."); } @@ -243,6 +245,15 @@ return false; } + private void reportReplace() { + RootNode rootNode = getRootNode(); + if (rootNode != null) { + if (rootNode.getCallTarget() instanceof ReplaceObserver) { + ((ReplaceObserver) rootNode.getCallTarget()).nodeReplaced(); + } + } + } + /** * Intended to be implemented by subclasses of {@link Node} to receive a notification when the * node is rewritten. This method is invoked before the actual replace has happened. @@ -251,37 +262,34 @@ * @param reason the reason the replace supplied */ protected void onReplace(Node newNode, String reason) { - RootNode rootNode = NodeUtil.findParent(this, RootNode.class); - if (rootNode != null) { - if (rootNode.getCallTarget() instanceof ReplaceObserver) { - ((ReplaceObserver) rootNode.getCallTarget()).nodeReplaced(); + if (TruffleOptions.TraceRewrites) { + traceRewrite(newNode, reason); + } + } + + private void traceRewrite(Node newNode, String reason) { + Class from = getClass(); + Class to = newNode.getClass(); + + if (TruffleOptions.TraceRewritesFilterFromKind != null) { + if (filterByKind(from, TruffleOptions.TraceRewritesFilterFromKind)) { + return; } } - if (TruffleOptions.TraceRewrites) { - Class from = getClass(); - Class to = newNode.getClass(); - if (TruffleOptions.TraceRewritesFilterFromKind != null) { - if (filterByKind(from, TruffleOptions.TraceRewritesFilterFromKind)) { - return; - } - } - - if (TruffleOptions.TraceRewritesFilterToKind != null) { - if (filterByKind(to, TruffleOptions.TraceRewritesFilterToKind)) { - return; - } - } - - String filter = TruffleOptions.TraceRewritesFilterClass; - if (filter != null && (filterByContainsClassName(from, filter) || filterByContainsClassName(to, filter))) { + if (TruffleOptions.TraceRewritesFilterToKind != null) { + if (filterByKind(to, TruffleOptions.TraceRewritesFilterToKind)) { return; } + } - // CheckStyle: stop system..print check - System.out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s.%n", this.toString(), formatNodeInfo(from), formatNodeInfo(to), reason); - // CheckStyle: resume system..print check + String filter = TruffleOptions.TraceRewritesFilterClass; + if (filter != null && (filterByContainsClassName(from, filter) || filterByContainsClassName(to, filter))) { + return; } + + PrintStream out = System.out; + out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s.%n", this.toString(), formatNodeInfo(from), formatNodeInfo(to), reason); } private static String formatNodeInfo(Class clazz) { @@ -383,6 +391,23 @@ } /** + * Get the root node of the tree a node belongs to. + * + * @return the {@link RootNode} or {@code null} if there is none. + */ + protected final RootNode getRootNode() { + Node rootNode = this; + while (rootNode.getParent() != null) { + assert !(rootNode instanceof RootNode) : "root node must not have a parent"; + rootNode = rootNode.getParent(); + } + if (rootNode instanceof RootNode) { + return (RootNode) rootNode; + } + return null; + } + + /** * Converts this node to a textual representation useful for debugging. */ @Override