Search in sources :

Example 41 with RootNode

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

the class InstrumentationHandler method lazyInitializeSourcesExecutedList.

/**
 * Initializes sourcesExecuted and sourcesExecutedList by populating them from executedRoots.
 */
private void lazyInitializeSourcesExecutedList() {
    assert Thread.holdsLock(sourcesExecuted);
    if (sourcesExecutedListRef.get() == null) {
        // build the sourcesExecutedList, we need it now
        Collection<Source> sourcesExecutedList = new WeakAsyncList<>(16);
        sourcesExecutedListRef.set(sourcesExecutedList);
        for (RootNode root : executedRoots) {
            int rootBits = RootNodeBits.get(root);
            if (RootNodeBits.isNoSourceSection(rootBits)) {
                continue;
            } else {
                SourceSection sourceSection = root.getSourceSection();
                if (RootNodeBits.isSameSource(rootBits) && sourceSection != null) {
                    Source source = sourceSection.getSource();
                    if (!sourcesExecuted.containsKey(source)) {
                        sourcesExecuted.put(source, null);
                        sourcesExecutedList.add(source);
                    }
                } else {
                    if (sourceSection != null) {
                        findSourcesExecutedVisitor.adoptSource(sourceSection.getSource());
                    }
                    visitRoot(root, root, findSourcesExecutedVisitor, false);
                    for (Source source : findSourcesExecutedVisitor.rootSources) {
                        if (!sourcesExecuted.containsKey(source)) {
                            sourcesExecuted.put(source, null);
                            sourcesExecutedList.add(source);
                        }
                    }
                    findSourcesExecutedVisitor.rootSources.clear();
                }
            }
        }
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(com.oracle.truffle.api.source.Source)

Example 42 with RootNode

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

the class ProbeNode method findParentChain.

@SuppressWarnings("deprecation")
private EventChainNode findParentChain(VirtualFrame frame, EventBinding<?> binding) {
    Node node = getParent().getParent();
    while (node != null) {
        // TODO we should avoid materializing the source section here
        if (node instanceof com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode) {
            ProbeNode probe = ((com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode) node).getProbeNode();
            EventChainNode c = probe.lazyUpdate(frame);
            if (c != null) {
                c = c.find(binding);
            }
            if (c != null) {
                return c;
            }
        } else if (node instanceof RootNode) {
            break;
        }
        node = node.getParent();
    }
    if (node == null) {
        throw new IllegalStateException("The AST node is not yet adopted. ");
    }
    return null;
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node)

Example 43 with RootNode

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

the class SLLanguage method parse.

@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
    Source source = request.getSource();
    Map<String, RootCallTarget> functions;
    /*
         * Parse the provided source. At this point, we do not have a SLContext yet. Registration of
         * the functions with the SLContext happens lazily in SLEvalRootNode.
         */
    if (request.getArgumentNames().isEmpty()) {
        functions = Parser.parseSL(this, source);
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("function main(");
        String sep = "";
        for (String argumentName : request.getArgumentNames()) {
            sb.append(sep);
            sb.append(argumentName);
            sep = ",";
        }
        sb.append(") { return ");
        sb.append(request.getSource().getCharacters());
        sb.append(";}");
        Source decoratedSource = Source.newBuilder(sb.toString()).mimeType(request.getSource().getMimeType()).name(request.getSource().getName()).build();
        functions = Parser.parseSL(this, decoratedSource);
    }
    RootCallTarget main = functions.get("main");
    RootNode evalMain;
    if (main != null) {
        /*
             * We have a main function, so "evaluating" the parsed source means invoking that main
             * function. However, we need to lazily register functions into the SLContext first, so
             * we cannot use the original SLRootNode for the main function. Instead, we create a new
             * SLEvalRootNode that does everything we need.
             */
        evalMain = new SLEvalRootNode(this, main, functions);
    } else {
        /*
             * Even without a main function, "evaluating" the parsed source needs to register the
             * functions into the SLContext.
             */
        evalMain = new SLEvalRootNode(this, null, functions);
    }
    return Truffle.getRuntime().createCallTarget(evalMain);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) SLEvalRootNode(com.oracle.truffle.sl.nodes.SLEvalRootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Source(com.oracle.truffle.api.source.Source)

Example 44 with RootNode

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

the class SLStatementNode method getSourceSection.

/*
     * The creation of source section can be implemented lazily by looking up the root node source
     * and then creating the source section object using the indices stored in the node. This avoids
     * the eager creation of source section objects during parsing and creates them only when they
     * are needed. Alternatively, if the language uses source sections to implement language
     * semantics, then it might be more efficient to eagerly create source sections and store it in
     * the AST.
     *
     * For more details see {@link InstrumentableNode}.
     */
@Override
@TruffleBoundary
public final SourceSection getSourceSection() {
    if (sourceCharIndex == NO_SOURCE) {
        // AST node without source
        return null;
    }
    RootNode rootNode = getRootNode();
    if (rootNode == null) {
        // not yet adopted yet
        return null;
    }
    SourceSection rootSourceSection = rootNode.getSourceSection();
    if (rootSourceSection == null) {
        return null;
    }
    Source source = rootSourceSection.getSource();
    if (sourceCharIndex == UNAVAILABLE_SOURCE) {
        return source.createUnavailableSection();
    } else {
        return source.createSection(sourceCharIndex, sourceLength);
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) SourceSection(com.oracle.truffle.api.source.SourceSection) Source(com.oracle.truffle.api.source.Source) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 45 with RootNode

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

the class SimplePartialEvaluationTest method intrinsicHashCode.

@Test
public void intrinsicHashCode() {
    /*
         * The intrinsic for Object.hashCode() is inlined late during Truffle partial evaluation,
         * because we call hashCode() on a value whose exact type Object is only known during
         * partial evaluation.
         */
    FrameDescriptor fd = new FrameDescriptor();
    Object testObject = new Object();
    AbstractTestNode result = new ObjectHashCodeNode(testObject);
    RootNode rootNode = new RootTestNode(fd, "intrinsicHashCode", result);
    OptimizedCallTarget compilable = compileHelper("intrinsicHashCode", rootNode, new Object[0]);
    int actual = (Integer) compilable.call(new Object[0]);
    int expected = testObject.hashCode();
    Assert.assertEquals(expected, actual);
}
Also used : FrameDescriptor(com.oracle.truffle.api.frame.FrameDescriptor) RootNode(com.oracle.truffle.api.nodes.RootNode) AbstractTestNode(org.graalvm.compiler.truffle.test.nodes.AbstractTestNode) ObjectHashCodeNode(org.graalvm.compiler.truffle.test.nodes.ObjectHashCodeNode) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) RootTestNode(org.graalvm.compiler.truffle.test.nodes.RootTestNode) Test(org.junit.Test)

Aggregations

RootNode (com.oracle.truffle.api.nodes.RootNode)86 Test (org.junit.Test)36 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)24 Node (com.oracle.truffle.api.nodes.Node)23 CallTarget (com.oracle.truffle.api.CallTarget)16 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)16 RootCallTarget (com.oracle.truffle.api.RootCallTarget)12 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)12 RootTestNode (org.graalvm.compiler.truffle.test.nodes.RootTestNode)9 Source (com.oracle.truffle.api.source.Source)8 AbstractTestNode (org.graalvm.compiler.truffle.test.nodes.AbstractTestNode)8 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 SourceSection (com.oracle.truffle.api.source.SourceSection)6 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)5 ArrayList (java.util.ArrayList)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 TruffleException (com.oracle.truffle.api.TruffleException)3 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)3