changeset 22250:0fb3522e5b72

Truffle/Instrumentation: The ProbeListener start/stop methods now pass the RootNode instead of the Source; a comment on the RootNode constructor encourages implementations to provide *some* SourceSection instance with every RootNode, which may be one created by SourceSection.createUnavailable(String kind, String name) which makes tracing/debugging much more useful.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Thu, 24 Sep 2015 13:15:45 -0700
parents bda4b68f2e07
children 8dddde8b20d4
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/TruffleRuntimeTest.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/AdvancedInstrumentTest.java truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/InstrumentationTest.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeListener.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultProbeListener.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java
diffstat 7 files changed, 50 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/TruffleRuntimeTest.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/TruffleRuntimeTest.java	Thu Sep 24 13:15:45 2015 -0700
@@ -22,6 +22,21 @@
  */
 package com.oracle.truffle.api.test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
 import com.oracle.truffle.api.RootCallTarget;
 import com.oracle.truffle.api.Truffle;
 import com.oracle.truffle.api.TruffleRuntime;
@@ -32,21 +47,6 @@
 import com.oracle.truffle.api.source.SourceSection;
 import com.oracle.truffle.api.test.utilities.InstrumentationTestMode;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
 /**
  * <h3>Accessing the Truffle Runtime</h3>
  *
@@ -78,8 +78,8 @@
         InstrumentationTestMode.set(false);
     }
 
-    private static RootNode createTestRootNode() {
-        return new RootNode(TestingLanguage.class, null, null) {
+    private static RootNode createTestRootNode(SourceSection sourceSection) {
+        return new RootNode(TestingLanguage.class, sourceSection, null) {
             @Override
             public Object execute(VirtualFrame frame) {
                 return 42;
@@ -104,7 +104,7 @@
 
     @Test
     public void testCreateCallTarget() {
-        RootNode rootNode = createTestRootNode();
+        RootNode rootNode = createTestRootNode(null);
         RootCallTarget target = runtime.createCallTarget(rootNode);
         assertNotNull(target);
         assertEquals(target.call(), 42);
@@ -113,14 +113,14 @@
 
     @Test
     public void testGetCallTargets1() {
-        RootNode rootNode = createTestRootNode();
+        RootNode rootNode = createTestRootNode(null);
         RootCallTarget target = runtime.createCallTarget(rootNode);
         assertTrue(runtime.getCallTargets().contains(target));
     }
 
     @Test
     public void testGetCallTargets2() {
-        RootNode rootNode = createTestRootNode();
+        RootNode rootNode = createTestRootNode(null);
         RootCallTarget target1 = runtime.createCallTarget(rootNode);
         RootCallTarget target2 = runtime.createCallTarget(rootNode);
         assertTrue(runtime.getCallTargets().contains(target1));
@@ -138,10 +138,8 @@
         SourceSection sourceSection1 = source1.createSection("foo", 1);
         SourceSection sourceSection2 = source1.createSection("bar", 2);
 
-        RootNode rootNode1 = createTestRootNode();
-        rootNode1.assignSourceSection(sourceSection1);
-        RootNode rootNode2 = createTestRootNode();
-        rootNode2.assignSourceSection(sourceSection2);
+        RootNode rootNode1 = createTestRootNode(sourceSection1);
+        RootNode rootNode2 = createTestRootNode(sourceSection2);
         RootNode rootNode2Copy = NodeUtil.cloneNode(rootNode2);
 
         assertSame(rootNode2.getSourceSection(), rootNode2Copy.getSourceSection());
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/AdvancedInstrumentTest.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/AdvancedInstrumentTest.java	Thu Sep 24 13:15:45 2015 -0700
@@ -35,8 +35,8 @@
 import com.oracle.truffle.api.instrument.AdvancedInstrumentRootFactory;
 import com.oracle.truffle.api.instrument.Instrumenter;
 import com.oracle.truffle.api.instrument.Probe;
-import com.oracle.truffle.api.instrument.ProbeListener;
 import com.oracle.truffle.api.instrument.SyntaxTag;
+import com.oracle.truffle.api.instrument.impl.DefaultProbeListener;
 import com.oracle.truffle.api.nodes.Node;
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestAdvancedInstrumentCounterRoot;
@@ -59,24 +59,15 @@
         final Source source = Source.fromText("testAdvancedInstrumentListener text", "testAdvancedInstrumentListener").withMimeType("text/x-instTest");
 
         final Probe[] addNodeProbe = new Probe[1];
-        instrumenter.addProbeListener(new ProbeListener() {
+        instrumenter.addProbeListener(new DefaultProbeListener() {
 
-            public void startASTProbing(Source s) {
-            }
-
-            public void newProbeInserted(Probe p) {
-            }
-
+            @Override
             public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) {
                 if (tag == InstrumentTestTag.ADD_TAG) {
                     assertNull("only one add node", addNodeProbe[0]);
                     addNodeProbe[0] = probe;
                 }
             }
-
-            public void endASTProbing(Source s) {
-            }
-
         });
         assertEquals(vm.eval(source).get(), 13);
         assertNotNull("Add node should be probed", addNodeProbe[0]);
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/InstrumentationTest.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/InstrumentationTest.java	Thu Sep 24 13:15:45 2015 -0700
@@ -36,11 +36,10 @@
 import com.oracle.truffle.api.instrument.Instrument;
 import com.oracle.truffle.api.instrument.Instrumenter;
 import com.oracle.truffle.api.instrument.Probe;
-import com.oracle.truffle.api.instrument.ProbeListener;
-import com.oracle.truffle.api.instrument.WrapperNode;
 import com.oracle.truffle.api.instrument.SimpleInstrumentListener;
 import com.oracle.truffle.api.instrument.StandardInstrumentListener;
 import com.oracle.truffle.api.instrument.SyntaxTag;
+import com.oracle.truffle.api.instrument.WrapperNode;
 import com.oracle.truffle.api.instrument.impl.DefaultProbeListener;
 import com.oracle.truffle.api.instrument.impl.DefaultSimpleInstrumentListener;
 import com.oracle.truffle.api.instrument.impl.DefaultStandardInstrumentListener;
@@ -71,14 +70,9 @@
         final Source source = Source.fromText("testProbing text", "testProbing").withMimeType("text/x-instTest");
 
         final Probe[] probes = new Probe[3];
-        instrumenter.addProbeListener(new ProbeListener() {
+        instrumenter.addProbeListener(new DefaultProbeListener() {
 
-            public void startASTProbing(Source s) {
-            }
-
-            public void newProbeInserted(Probe probe) {
-            }
-
+            @Override
             public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) {
                 if (tag == InstrumentTestTag.ADD_TAG) {
                     assertEquals(probes[0], null);
@@ -93,10 +87,6 @@
                     }
                 }
             }
-
-            public void endASTProbing(Source s) {
-            }
-
         });
         assertEquals(vm.eval(source).get(), 13);
         assertNotNull("Add node should be probed", probes[0]);
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/Instrumenter.java	Thu Sep 24 13:15:45 2015 -0700
@@ -38,7 +38,6 @@
 import com.oracle.truffle.api.TruffleLanguage;
 import com.oracle.truffle.api.impl.Accessor;
 import com.oracle.truffle.api.nodes.Node;
-import com.oracle.truffle.api.nodes.NodeVisitor;
 import com.oracle.truffle.api.nodes.RootNode;
 import com.oracle.truffle.api.source.Source;
 import com.oracle.truffle.api.source.SourceSection;
@@ -58,16 +57,6 @@
         }
     }
 
-    /**
-     * Walks an AST, looking for the first node with an assigned {@link SourceSection} and returning
-     * the {@link Source}.
-     */
-    private static Source findSource(Node node) {
-        final FindSourceVisitor visitor = new FindSourceVisitor();
-        node.accept(visitor);
-        return visitor.source;
-    }
-
     private enum ToolState {
 
         /** Not yet installed, inert. */
@@ -253,20 +242,6 @@
      */
     @CompilationFinal private SyntaxTagTrap afterTagTrap = null;
 
-    private static final class FindSourceVisitor implements NodeVisitor {
-
-        Source source = null;
-
-        public boolean visit(Node node) {
-            final SourceSection sourceSection = node.getSourceSection();
-            if (sourceSection != null) {
-                source = sourceSection.getSource();
-                return false;
-            }
-            return true;
-        }
-    }
-
     Instrumenter(Object vm) {
         this.vm = vm;
     }
@@ -556,24 +531,24 @@
         if (!astProbers.isEmpty()) {
 
             String name = "<?>";
-            final Source source = findSource(rootNode);
-            if (source != null) {
-                name = source.getShortName();
-            } else {
-                final SourceSection sourceSection = rootNode.getEncapsulatingSourceSection();
-                if (sourceSection != null) {
+            final SourceSection sourceSection = rootNode.getSourceSection();
+            if (sourceSection != null) {
+                final Source source = sourceSection.getSource();
+                if (source != null) {
+                    name = source.getShortName();
+                } else {
                     name = sourceSection.getShortDescription();
                 }
             }
             trace("START %s", name);
             for (ProbeListener listener : probeListeners) {
-                listener.startASTProbing(source);
+                listener.startASTProbing(rootNode);
             }
             for (ASTProber prober : astProbers) {
                 prober.probeAST(this, rootNode);  // TODO (mlvdv)
             }
             for (ProbeListener listener : probeListeners) {
-                listener.endASTProbing(source);
+                listener.endASTProbing(rootNode);
             }
             trace("FINISHED %s", name);
         }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeListener.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeListener.java	Thu Sep 24 13:15:45 2015 -0700
@@ -25,7 +25,7 @@
 package com.oracle.truffle.api.instrument;
 
 import com.oracle.truffle.api.nodes.Node;
-import com.oracle.truffle.api.source.Source;
+import com.oracle.truffle.api.nodes.RootNode;
 
 /**
  * An observer of events related to {@link Probe}s: creating and tagging.
@@ -36,9 +36,9 @@
      * Notifies that all registered {@link ASTProber}s are about to be applied to a newly
      * constructed AST.
      *
-     * @param source source code from which the AST was constructed
+     * @param rootNode parent of the newly created AST
      */
-    void startASTProbing(Source source);
+    void startASTProbing(RootNode rootNode);
 
     /**
      * Notifies that a {@link Probe} has been newly attached to an AST via
@@ -74,8 +74,8 @@
      * Notifies that the application of all registered {@link ASTProber}s to a newly constructed AST
      * has completed.
      *
-     * @param source source code from which the AST was constructed
+     * @param rootNode parent of the newly created AST
      */
-    void endASTProbing(Source source);
+    void endASTProbing(RootNode rootNode);
 
 }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultProbeListener.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultProbeListener.java	Thu Sep 24 13:15:45 2015 -0700
@@ -27,11 +27,11 @@
 import com.oracle.truffle.api.instrument.Probe;
 import com.oracle.truffle.api.instrument.ProbeListener;
 import com.oracle.truffle.api.instrument.SyntaxTag;
-import com.oracle.truffle.api.source.Source;
+import com.oracle.truffle.api.nodes.RootNode;
 
 public abstract class DefaultProbeListener implements ProbeListener {
 
-    public void startASTProbing(Source source) {
+    public void startASTProbing(RootNode rootNode) {
     }
 
     public void newProbeInserted(Probe probe) {
@@ -40,7 +40,7 @@
     public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) {
     }
 
-    public void endASTProbing(Source source) {
+    public void endASTProbing(RootNode rootNode) {
     }
 
 }
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java	Thu Sep 24 13:11:19 2015 -0700
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java	Thu Sep 24 13:15:45 2015 -0700
@@ -78,6 +78,11 @@
     /**
      * Creates new root node. Each {@link RootNode} is associated with a particular language - if
      * the root node represents a method it is assumed the method is written in such language.
+     * <p>
+     * <strong>Note:</strong> Although the {@link SourceSction} <em>can</em> be {@code null}, this
+     * is strongly discouraged for the purposes of testing/tracing/tooling. Please use
+     * {@link SourceSection#createUnavailable(String, String)} to create a descriptive instance with
+     * a language-specific <em>kind</em> such as "SL Builtin" and a <em>name</em> if possible.
      *
      * @param language the language of the node, <b>cannot be</b> <code>null</code>
      * @param sourceSection a part of source associated with this node, can be <code>null</code>
@@ -160,7 +165,7 @@
      * stack) without prior knowledge of the language it has come from.
      *
      * Used for instance to determine the language of a <code>RootNode<code>:
-     *
+     * 
      * <pre>
      * <code>
      * rootNode.getExecutionContext().getLanguageShortName();