Search in sources :

Example 41 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class Context method parse.

@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
    Source code = request.getSource();
    SourceSection outer = code.createSection(0, code.getLength());
    BaseNode node;
    try {
        node = parse(code);
    } catch (LanguageError e) {
        throw new IOException(e);
    }
    RootCallTarget afterTarget = getContextReference().get().afterTarget;
    return Truffle.getRuntime().createCallTarget(new InstrumentationTestRootNode(this, "", outer, afterTarget, node));
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection) IOException(java.io.IOException) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Source(com.oracle.truffle.api.source.Source)

Example 42 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class StatementProfilerExampleTest method assertLine.

private static void assertLine(Map<SourceSection, Counter> counters, int line, int count) {
    boolean found = false;
    for (SourceSection section : counters.keySet()) {
        if (section.getStartLine() == line) {
            Assert.assertFalse(found);
            found = true;
            Assert.assertEquals(count, counters.get(section).getCount());
        }
    }
    Assert.assertTrue(found);
}
Also used : SourceSection(com.oracle.truffle.api.source.SourceSection)

Example 43 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class StatementProfilerExampleTest method testStatements.

@Test
public void testStatements() throws IOException {
    Source source = lines(// 1
    "ROOT(", // 2
    "STATEMENT(EXPRESSION", // 3
    ", STATEMENT),", // 4
    "STATEMENT)");
    Map<SourceSection, Counter> counters = profiler.getCounters();
    for (int i = 0; i < 1000; i++) {
        if (i == 0) {
            Assert.assertTrue(counters.isEmpty());
        } else {
            assertLine(counters, 2, i);
            assertLine(counters, 3, i);
            assertLine(counters, 4, i);
        }
        run(source);
    }
}
Also used : Counter(com.oracle.truffle.api.instrumentation.test.examples.StatementProfilerExample.Counter) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test) AbstractInstrumentationTest(com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)

Example 44 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class StatementProfilerExampleTest method testLoopZero.

@Test
public void testLoopZero() throws IOException {
    Source source = lines(// 1
    "LOOP(0", // 2
    ",STATEMENT(", // 3
    "STATEMENT),", /**/
    "STATEMENT)");
    // 4
    Map<SourceSection, Counter> counters = profiler.getCounters();
    for (int i = 0; i < 10; i++) {
        run(source);
        // never actually executed
        Assert.assertTrue(counters.isEmpty());
    }
}
Also used : Counter(com.oracle.truffle.api.instrumentation.test.examples.StatementProfilerExample.Counter) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(org.graalvm.polyglot.Source) Test(org.junit.Test) AbstractInstrumentationTest(com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)

Example 45 with SourceSection

use of com.oracle.truffle.api.source.SourceSection in project graal by oracle.

the class DefaultNearestNodeSearch method findChildTaggedNode.

/**
 * Finds the nearest tagged {@link Node node}. See the algorithm description at
 * {@link InstrumentableNode#findNearestNodeAt(int, Set)}.
 */
private static Node findChildTaggedNode(Node node, int offset, Set<Class<? extends Tag>> tags, boolean haveOuterCandidate, boolean preferFirst) {
    Node[] highestLowerNode = new Node[] { null };
    Node[] highestLowerTaggedNode = new Node[] { null };
    Node[] lowestHigherNode = new Node[] { null };
    Node[] lowestHigherTaggedNode = new Node[] { null };
    final Node[] foundNode = new Node[] { null };
    NodeUtil.forEachChild(node, new NodeVisitor() {

        int highestLowerNodeIndex = 0;

        int highestLowerTaggedNodeIndex = 0;

        int lowestHigherNodeIndex = 0;

        int lowestHigherTaggedNodeIndex = 0;

        @Override
        public boolean visit(Node childNode) {
            Node ch = childNode;
            if (ch instanceof WrapperNode) {
                ch = ((WrapperNode) ch).getDelegateNode();
            }
            SourceSection ss;
            if (ch instanceof InstrumentableNode && ((InstrumentableNode) ch).isInstrumentable()) {
                ch = (Node) ((InstrumentableNode) ch).materializeInstrumentableNodes(tags);
                ss = ch.getSourceSection();
                if (ss == null) {
                    return true;
                }
            } else {
                // An unknown node, process its children on the same level.
                NodeUtil.forEachChild(ch, this);
                return foundNode[0] == null;
            }
            boolean isTagged = isTaggedWith((InstrumentableNode) ch, tags);
            int i1 = ss.getCharIndex();
            int i2 = getCharEndIndex(ss);
            if (isTagged && offset == i1) {
                // We're at it
                foundNode[0] = ch;
                return false;
            }
            if (i1 <= offset && offset <= i2) {
                // In an encapsulating source section
                Node taggedNode = findChildTaggedNode(ch, offset, tags, isTagged || haveOuterCandidate, preferFirst);
                if (taggedNode != null) {
                    foundNode[0] = taggedNode;
                    return false;
                }
                if (isTagged) {
                    // If nothing in and is tagged, return it
                    foundNode[0] = ch;
                    return false;
                }
            }
            if (offset < i1) {
                // We're after the offset
                if (lowestHigherNode[0] == null || lowestHigherNodeIndex > i1) {
                    lowestHigherNode[0] = ch;
                    lowestHigherNodeIndex = i1;
                }
                if (isTagged) {
                    if (lowestHigherTaggedNode[0] == null || lowestHigherTaggedNodeIndex > i1) {
                        lowestHigherTaggedNode[0] = ch;
                        lowestHigherTaggedNodeIndex = i1;
                    }
                }
            }
            if (i2 < offset) {
                // We're before the offset
                if (highestLowerNode[0] == null || (preferFirst ? i1 < highestLowerNodeIndex : highestLowerNodeIndex < i1)) {
                    highestLowerNode[0] = ch;
                    highestLowerNodeIndex = i1;
                }
                if (isTagged) {
                    if (highestLowerTaggedNode[0] == null || (preferFirst ? i1 < highestLowerTaggedNodeIndex : highestLowerTaggedNodeIndex < i1)) {
                        highestLowerTaggedNode[0] = ch;
                        highestLowerTaggedNodeIndex = i1;
                    }
                }
            }
            return true;
        }
    });
    if (foundNode[0] != null) {
        return foundNode[0];
    }
    Node primaryNode;
    Node primaryTaggedNode;
    Node secondaryNode;
    Node secondaryTaggedNode;
    if (preferFirst) {
        // Prefer node before the offset:
        primaryNode = highestLowerNode[0];
        primaryTaggedNode = highestLowerTaggedNode[0];
        secondaryNode = lowestHigherNode[0];
        secondaryTaggedNode = lowestHigherTaggedNode[0];
    } else {
        // Prefer node after the offset:
        primaryNode = lowestHigherNode[0];
        primaryTaggedNode = lowestHigherTaggedNode[0];
        secondaryNode = highestLowerNode[0];
        secondaryTaggedNode = highestLowerTaggedNode[0];
    }
    if (isTaggedWith(primaryNode, tags)) {
        return primaryNode;
    }
    if (isTaggedWith(secondaryNode, tags)) {
        return secondaryNode;
    }
    // Try to go in the preferred node:
    Node taggedNode = null;
    if (!haveOuterCandidate) {
        if (primaryNode != null) {
            taggedNode = findChildTaggedNode(primaryNode, offset, tags, haveOuterCandidate, true);
        }
    }
    if (taggedNode == null && primaryTaggedNode != null) {
        return primaryTaggedNode;
    }
    // Try to go in a node before:
    if (!haveOuterCandidate) {
        if (taggedNode == null && secondaryNode != null) {
            taggedNode = findChildTaggedNode(secondaryNode, offset, tags, haveOuterCandidate, true);
        }
    }
    if (taggedNode == null && secondaryTaggedNode != null) {
        return secondaryTaggedNode;
    }
    return taggedNode;
}
Also used : WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) Node(com.oracle.truffle.api.nodes.Node) WrapperNode(com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode) SourceSection(com.oracle.truffle.api.source.SourceSection) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor)

Aggregations

SourceSection (com.oracle.truffle.api.source.SourceSection)68 Test (org.junit.Test)23 Source (com.oracle.truffle.api.source.Source)15 Source (org.graalvm.polyglot.Source)12 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)11 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)8 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)8 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)6 Node (com.oracle.truffle.api.nodes.Node)6 RootNode (com.oracle.truffle.api.nodes.RootNode)6 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)5 DebugValue (com.oracle.truffle.api.debug.DebugValue)5 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)3 DebugScope (com.oracle.truffle.api.debug.DebugScope)3 Counter (com.oracle.truffle.api.instrumentation.test.examples.StatementProfilerExample.Counter)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 RootCallTarget (com.oracle.truffle.api.RootCallTarget)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 WrapperNode (com.oracle.truffle.api.instrumentation.InstrumentableNode.WrapperNode)2 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)2