Search in sources :

Example 46 with Node

use of com.oracle.truffle.api.nodes.Node in project graal by oracle.

the class SourceSectionFilterTest method testIndexIn.

@Test
public void testIndexIn() {
    Source sampleSource = Source.newBuilder("line1\nline2\nline3\nline4").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
    Node root = createNode(sampleSource.createSection(0, 23));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(0, 0).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(0, 1).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(0, 4), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(4, 5), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(4, 6), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(10, 1), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(9, 1), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(9, 5), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createUnavailableSection())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(IndexRange.between(0, 5)).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(IndexRange.between(0, 5), IndexRange.between(5, 6)).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(IndexRange.between(0, 5), IndexRange.between(11, 12)).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
    Assert.assertNotNull(SourceSectionFilter.newBuilder().indexIn(5, 5).build().toString());
}
Also used : Node(com.oracle.truffle.api.nodes.Node) ProbeNode(com.oracle.truffle.api.instrumentation.ProbeNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 47 with Node

use of com.oracle.truffle.api.nodes.Node in project graal by oracle.

the class SourceSectionFilterTest method testSourceSectionEquals.

@Test
public void testSourceSectionEquals() {
    Source sampleSource1 = Source.newBuilder("line1\nline2\nline3\nline4").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
    Source sampleSource2 = Source.newBuilder("line1\nline2\nline3\nline4").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
    Node root1 = createNode(sampleSource1.createSection(0, 23));
    Node root2 = createNode(sampleSource2.createSection(0, 23));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 6)).build(), root1, createNode(sampleSource1.createSection(1, 6), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 6)).build(), root2, createNode(sampleSource2.createSection(1, 6), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 7)).build(), root1, createNode(sampleSource1.createSection(1, 6), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6)).build(), root1, createNode(sampleSource1.createSection(1, 6), tags())));
    Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6), sampleSource1.createSection(2, 7)).build(), root1, createNode(sampleSource1.createSection(2, 7), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6), sampleSource1.createSection(2, 7)).build(), root1, createNode(sampleSource1.createSection(2, 8), tags())));
    Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6), sampleSource1.createSection(2, 7)).build(), null, createNode(sampleSource1.createUnavailableSection())));
    Assert.assertNotNull(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 6)).build().toString());
}
Also used : Node(com.oracle.truffle.api.nodes.Node) ProbeNode(com.oracle.truffle.api.instrumentation.ProbeNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 48 with Node

use of com.oracle.truffle.api.nodes.Node in project graal by oracle.

the class InputFilterTest method assertCleanedUp.

private void assertCleanedUp(String code) {
    // first we capture all root nodes used by the code.
    Set<RootNode> rootNodes = new HashSet<>();
    EventBinding<?> binding = instrumenter.attachExecutionEventListener(SourceSectionFilter.ANY, new ExecutionEventListener() {

        public void onEnter(EventContext c, VirtualFrame frame) {
            addRoot(c);
        }

        @TruffleBoundary
        private void addRoot(EventContext c) {
            rootNodes.add(c.getInstrumentedNode().getRootNode());
        }

        public void onReturnValue(EventContext c, VirtualFrame frame, Object result) {
        }

        public void onReturnExceptional(EventContext c, VirtualFrame frame, Throwable exception) {
        }
    });
    execute(code);
    binding.dispose();
    // we execute again to let the instrumentation wrappers be cleaned up
    execute(code);
    for (RootNode root : rootNodes) {
        // all frame slots got removed
        assertEquals(new HashSet<>(), root.getFrameDescriptor().getIdentifiers());
        // no wrappers left
        root.accept(new NodeVisitor() {

            public boolean visit(Node node) {
                if (node instanceof WrapperNode) {
                    throw new AssertionError();
                }
                return true;
            }
        });
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor) EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) HashSet(java.util.HashSet)

Example 49 with Node

use of com.oracle.truffle.api.nodes.Node in project graal by oracle.

the class InstrumentableNodeTest method assertProperties.

private static void assertProperties(Object receiver, Object... properties) {
    try {
        assertTrue(receiver instanceof TruffleObject);
        TruffleObject obj = (TruffleObject) receiver;
        Node hasKeysNode = Message.HAS_KEYS.createNode();
        Node keysNode = Message.KEYS.createNode();
        assertTrue(ForeignAccess.sendHasKeys(hasKeysNode, obj));
        TruffleObject keys = ForeignAccess.sendKeys(keysNode, obj);
        for (int i = 0; i < properties.length; i = i + 2) {
            String expectedKey = (String) properties[i];
            Object expectedValue = properties[i + 1];
            Node readNode = Message.READ.createNode();
            Object key = ForeignAccess.sendRead(readNode, keys, i / 2);
            assertEquals(expectedKey, key);
            assertEquals(expectedValue, ForeignAccess.sendRead(readNode, obj, key));
        }
    } catch (InteropException e) {
        throw e.raise();
    }
}
Also used : Node(com.oracle.truffle.api.nodes.Node) ExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.ExpressionNode) MaterializeChildExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializeChildExpressionNode) MaterializedChildExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializedChildExpressionNode) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Example 50 with Node

use of com.oracle.truffle.api.nodes.Node in project graal by oracle.

the class DefaultNearestNodeSearch method findNearestNodeAt.

/**
 * We have a source offset (the character index) and a context node given. The offset is in
 * node's source. The context node is an instrumentable node, but may not have any particular
 * tags. We're searching for an instrumentable node tagged with at least one of the given tags,
 * which is nearest to the given offset according to the guest language execution flow. We find
 * the nearest tagged {@link Node node} in following steps:
 * <ul>
 * <li>When the offset is inside the context node, we search for the first tagged parent node
 * and call {@link #findChildTaggedNode(Node, int, Set, boolean)} to find a tagged child. If we
 * find it, we return it, if not, we return the tagged parent.</li>
 * <li>When the offset is behind the context node, we return the last tagged child of the
 * context node.</li>
 * <li>When the offset is before the context node, we return the first tagged child of the
 * context node.</li>
 * </ul>
 */
static Node findNearestNodeAt(int offset, Node contextNode, Set<Class<? extends Tag>> tags) {
    Node node = (Node) ((InstrumentableNode) contextNode).materializeInstrumentableNodes(tags);
    SourceSection section = node.getSourceSection();
    int startIndex = section.getCharIndex();
    int endIndex = getCharEndIndex(section);
    if (startIndex <= offset && offset <= endIndex) {
        Node parent = findParentTaggedNode(node, tags);
        Node ch = findChildTaggedNode(node, offset, tags, parent != null, false);
        if (ch == null) {
            return parent;
        }
        return ch;
    } else if (endIndex < offset) {
        return findLastNode(node, tags);
    } else {
        // offset < o1
        return findFirstNode(node, tags);
    }
}
Also used : WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) Node(com.oracle.truffle.api.nodes.Node) SourceSection(com.oracle.truffle.api.source.SourceSection)

Aggregations

Node (com.oracle.truffle.api.nodes.Node)101 RootNode (com.oracle.truffle.api.nodes.RootNode)65 Test (org.junit.Test)46 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)21 ProbeNode (com.oracle.truffle.api.instrumentation.ProbeNode)21 Source (com.oracle.truffle.api.source.Source)16 NodeVisitor (com.oracle.truffle.api.nodes.NodeVisitor)11 CallTarget (com.oracle.truffle.api.CallTarget)9 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)9 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)8 WrapperNode (com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode)7 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)6 SourceSection (com.oracle.truffle.api.source.SourceSection)6 TestHelper.createNode (com.oracle.truffle.api.dsl.test.TestHelper.createNode)5 ValueNode (com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode)5 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)5 SLStatementNode (com.oracle.truffle.sl.nodes.SLStatementNode)5 SLBlockNode (com.oracle.truffle.sl.nodes.controlflow.SLBlockNode)5 LinkedHashMap (java.util.LinkedHashMap)5 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)4