Search in sources :

Example 61 with Node

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

the class SLGraalRuntimeBuiltin method findCallsTo.

/**
 * Finds all {@link DirectCallNode} instances calling a certain original {@link CallTarget} in a
 * given {@link RootNode}.
 */
@TruffleBoundary
protected static final Set<DirectCallNode> findCallsTo(RootNode root, OptimizedCallTarget originalCallTarget) {
    final Set<DirectCallNode> allCallNodes = new HashSet<>();
    root.accept(new NodeVisitor() {

        @Override
        public boolean visit(Node node) {
            if (node instanceof DirectCallNode) {
                DirectCallNode callNode = (DirectCallNode) node;
                if (callNode.getCallTarget() == originalCallTarget || callNode.getClonedCallTarget() == originalCallTarget) {
                    allCallNodes.add(callNode);
                }
            }
            return true;
        }
    });
    return allCallNodes;
}
Also used : Node(com.oracle.truffle.api.nodes.Node) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) SLBuiltinNode(com.oracle.truffle.sl.builtins.SLBuiltinNode) RootNode(com.oracle.truffle.api.nodes.RootNode) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) HashSet(java.util.HashSet) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 62 with Node

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

the class SLInstrumentTest method testLexicalScopes.

@Test
public void testLexicalScopes() throws Exception {
    String code = "function test(n) {\n" + // 2
    "  a = 1;\n" + "  if (a > 0) {\n" + "    b = 10;\n" + // 5
    "    println(b);\n" + "  }\n" + "  if (a == 1) {\n" + "    b = 20;\n" + "    a = 0;\n" + // 10
    "    c = 1;\n" + "    if (b > 0) {\n" + "      a = 4;\n" + "      b = 5;\n" + "      c = 6;\n" + // 15
    "      d = 7;\n" + "      println(d);\n" + "    }\n" + "  }\n" + "  println(b);\n" + // 20
    "  println(a);\n" + "}\n" + "function main() {\n" + "  test(\"n_n\");\n" + "}";
    Source source = Source.newBuilder("sl", code, "testing").build();
    List<Throwable> throwables;
    try (Engine engine = Engine.newBuilder().out(new java.io.OutputStream() {

        // null output stream
        @Override
        public void write(int b) throws IOException {
        }
    }).build()) {
        Instrument envInstr = engine.getInstruments().get("testEnvironmentHandlerInstrument");
        TruffleInstrument.Env env = envInstr.lookup(Environment.class).env;
        throwables = new ArrayList<>();
        env.getInstrumenter().attachExecutionEventListener(SourceSectionFilter.newBuilder().lineIn(1, source.getLineCount()).build(), new ExecutionEventListener() {

            @Override
            public void onEnter(EventContext context, VirtualFrame frame) {
                Node node = context.getInstrumentedNode();
                Iterable<Scope> lexicalScopes = env.findLocalScopes(node, null);
                Iterable<Scope> dynamicScopes = env.findLocalScopes(node, frame);
                try {
                    verifyLexicalScopes(lexicalScopes, dynamicScopes, context.getInstrumentedSourceSection().getStartLine(), frame.materialize());
                } catch (ThreadDeath t) {
                    throw t;
                } catch (Throwable t) {
                    CompilerDirectives.transferToInterpreter();
                    PrintStream lsErr = System.err;
                    lsErr.println("Line = " + context.getInstrumentedSourceSection().getStartLine());
                    lsErr.println("Node = " + node + ", class = " + node.getClass().getName());
                    t.printStackTrace(lsErr);
                    throwables.add(t);
                }
            }

            @Override
            public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
            }

            @Override
            public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
            }
        });
        Context.newBuilder().engine(engine).build().eval(source);
    }
    assertTrue(throwables.toString(), throwables.isEmpty());
}
Also used : TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RootNode(com.oracle.truffle.api.nodes.RootNode) ProbeNode(com.oracle.truffle.api.instrumentation.ProbeNode) Node(com.oracle.truffle.api.nodes.Node) Source(org.graalvm.polyglot.Source) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener) EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 63 with Node

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

the class SLLexicalScope method getParentBlock.

private static SLBlockNode getParentBlock(Node node) {
    SLBlockNode block;
    Node parent = node.getParent();
    // Find a nearest block node.
    while (parent != null && !(parent instanceof SLBlockNode)) {
        parent = parent.getParent();
    }
    if (parent != null) {
        block = (SLBlockNode) parent;
    } else {
        block = null;
    }
    return block;
}
Also used : SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode)

Example 64 with Node

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

the class SLLexicalScope method findChildrenBlock.

private static SLBlockNode findChildrenBlock(Node node) {
    SLBlockNode[] blockPtr = new SLBlockNode[1];
    node.accept(new NodeVisitor() {

        @Override
        public boolean visit(Node n) {
            if (n instanceof SLBlockNode) {
                blockPtr[0] = (SLBlockNode) n;
                return false;
            } else {
                return true;
            }
        }
    });
    return blockPtr[0];
}
Also used : SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor)

Example 65 with Node

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

the class SLLexicalScope method collectVars.

private Map<String, FrameSlot> collectVars(Node varsBlock, Node currentNode) {
    // Variables are slot-based.
    // To collect declared variables, traverse the block's AST and find slots associated
    // with SLWriteLocalVariableNode. The traversal stops when we hit the current node.
    Map<String, FrameSlot> slots = new LinkedHashMap<>(4);
    NodeUtil.forEachChild(varsBlock, new NodeVisitor() {

        @Override
        public boolean visit(Node node) {
            if (node == currentNode) {
                return false;
            }
            // Do not enter any nested blocks.
            if (!(node instanceof SLBlockNode)) {
                boolean all = NodeUtil.forEachChild(node, this);
                if (!all) {
                    return false;
                }
            }
            // Write to a variable is a declaration unless it exists already in a parent scope.
            if (node instanceof SLWriteLocalVariableNode) {
                SLWriteLocalVariableNode wn = (SLWriteLocalVariableNode) node;
                String name = Objects.toString(wn.getSlot().getIdentifier());
                if (!hasParentVar(name)) {
                    slots.put(name, wn.getSlot());
                }
            }
            return true;
        }
    });
    return slots;
}
Also used : FrameSlot(com.oracle.truffle.api.frame.FrameSlot) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) SLStatementNode(com.oracle.truffle.sl.nodes.SLStatementNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLBlockNode(com.oracle.truffle.sl.nodes.controlflow.SLBlockNode) LinkedHashMap(java.util.LinkedHashMap) NodeVisitor(com.oracle.truffle.api.nodes.NodeVisitor)

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