Search in sources :

Example 46 with Source

use of com.oracle.truffle.api.source.Source 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 47 with Source

use of com.oracle.truffle.api.source.Source 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 48 with Source

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

the class SLDefineFunctionBuiltin method defineFunction.

@TruffleBoundary
@Specialization
public String defineFunction(String code) {
    // @formatter:off
    Source source = Source.newBuilder(code).name("[defineFunction]").mimeType(SLLanguage.MIME_TYPE).build();
    // @formatter:on
    /* The same parsing code as for parsing the initial source. */
    getContext().getFunctionRegistry().register(source);
    return code;
}
Also used : Source(com.oracle.truffle.api.source.Source) Specialization(com.oracle.truffle.api.dsl.Specialization) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 49 with Source

use of com.oracle.truffle.api.source.Source 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 50 with Source

use of com.oracle.truffle.api.source.Source in project sieve by jtulach.

the class Main method main.

public static void main(String[] args) throws Exception {
    System.err.println("Setting up PolyglotEngine");
    PolyglotEngine vm = PolyglotEngine.newBuilder().build();
    vm.eval(Source.fromText("", "warmup.R").withMimeType("text/x-r"));
    URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();
    File local = new File(url.toURI());
    for (; ; ) {
        File sieveInR = new File(local, "sieve.R");
        if (sieveInR.exists()) {
            break;
        }
        local = local.getParentFile();
    }
    System.err.println("engine is ready");
    Source r = Source.fromFileName(new File(local, "sieve.R").getPath());
    vm.eval(r);
}
Also used : File(java.io.File) PolyglotEngine(com.oracle.truffle.api.vm.PolyglotEngine) URL(java.net.URL) Source(com.oracle.truffle.api.source.Source)

Aggregations

Source (com.oracle.truffle.api.source.Source)113 Test (org.junit.Test)65 RootNode (com.oracle.truffle.api.nodes.RootNode)23 File (java.io.File)20 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)16 Node (com.oracle.truffle.api.nodes.Node)16 SourceSection (com.oracle.truffle.api.source.SourceSection)16 ProbeNode (com.oracle.truffle.api.instrumentation.ProbeNode)15 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)11 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 CallTarget (com.oracle.truffle.api.CallTarget)5 FileWriter (java.io.FileWriter)5 RootCallTarget (com.oracle.truffle.api.RootCallTarget)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)3 Script (com.oracle.truffle.tools.chromeinspector.types.Script)3