com.oracle.truffle.api.CallTarget Java Examples

The following examples show how to use com.oracle.truffle.api.CallTarget. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HashemParseInContextTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
    return Truffle.getRuntime().createCallTarget(new RootNode(this) {

        @CompilationFinal private ContextReference<Env> reference;

        @Override
        public Object execute(VirtualFrame frame) {
            return parseAndEval();
        }

        @TruffleBoundary
        private Object parseAndEval() {
            if (reference == null) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                this.reference = lookupContextReference(EvalLang.class);
            }
            Source aPlusB = Source.newBuilder("hashemi", "a + b", "plus.hashem").build();
            return reference.get().parsePublic(aPlusB, "a", "b").call(30, 12);
        }
    });
}
 
Example #2
Source File: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Specialization(limit = "3")
Object doMap(Object source, Type elementType, CUDARuntime runtime,
                @CachedLibrary("source") InteropLibrary interop,
                @CachedContext(GrCUDALanguage.class) @SuppressWarnings("unused") GrCUDAContext context,
                @Cached(value = "createLoop(source)", uncached = "createUncachedLoop(source, context)") CallTarget loop) {

    if (source instanceof DeviceArray && ((DeviceArray) source).getElementType() == elementType) {
        return source;
    }

    if (!interop.hasArrayElements(source)) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("cannot map from non-array to DeviceArray");
    }

    long size;
    try {
        size = interop.getArraySize(source);
    } catch (UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("cannot read array size");
    }
    DeviceArray result = new DeviceArray(runtime, size, elementType);
    loop.call(size, source, result);
    return result;
}
 
Example #3
Source File: HashemStackTraceBuiltin.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@TruffleBoundary
private static String createStackTrace() {
    final StringBuilder str = new StringBuilder();

    Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Integer>() {
        private int skip = 1; // skip stack trace builtin

        @Override
        public Integer visitFrame(FrameInstance frameInstance) {
            if (skip > 0) {
                skip--;
                return null;
            }
            CallTarget callTarget = frameInstance.getCallTarget();
            Frame frame = frameInstance.getFrame(FrameAccess.READ_ONLY);
            RootNode rn = ((RootCallTarget) callTarget).getRootNode();
            // ignore internal or interop stack frames
            if (rn.isInternal() || rn.getLanguageInfo() == null) {
                return 1;
            }
            if (str.length() > 0) {
                str.append(System.getProperty("line.separator"));
            }
            str.append("Frame: ").append(rn.toString());
            FrameDescriptor frameDescriptor = frame.getFrameDescriptor();
            for (FrameSlot s : frameDescriptor.getSlots()) {
                str.append(", ").append(s.getIdentifier()).append("=").append(frame.getValue(s));
            }
            return null;
        }
    });
    return str.toString();
}
 
Example #4
Source File: DirectDispatchNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Object executeDispatch(VirtualFrame frame, CallTarget callTarget,
        Object[] arguments) {
    if (this.cachedCallTarget == callTarget) {
        return this.callCachedTargetNode.call(arguments);
    }
    return this.nextNode.executeDispatch(frame, callTarget, arguments);
}
 
Example #5
Source File: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected CallTarget createUncachedLoop(Object source, GrCUDAContext context) {
    ConcurrentHashMap<Class<?>, CallTarget> uncachedCallTargets = context.getMapCallTargets();
    DynamicDispatchLibrary dispatch = DynamicDispatchLibrary.getFactory().getUncached(source);
    Class<?> clazz = dispatch.dispatch(source);
    if (clazz == null) {
        clazz = source.getClass();
    }
    return uncachedCallTargets.computeIfAbsent(clazz, c -> Truffle.getRuntime().createCallTarget(new LoopRootNode(source, c)));
}
 
Example #6
Source File: DebuggerVisualizer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static String getDisplayName(CallTarget ct) {
    if (ct instanceof RootCallTarget) {
        RootNode rn = ((RootCallTarget) ct).getRootNode();
        return getMethodName(rn);
    } else {
        //System.err.println("Unexpected CallTarget: "+ct.getClass());
        return ct.toString();
    }
}
 
Example #7
Source File: SqueakLanguage.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Override
protected CallTarget parse(final ParsingRequest request) throws Exception {
    final SqueakImageContext image = getContext();
    final Source source = request.getSource();
    if (source.hasBytes()) {
        image.setImagePath(source.getPath());
        return image.getSqueakImage().asCallTarget();
    } else {
        image.ensureLoaded();
        if (source.isInternal()) {
            image.printToStdOut(MiscUtils.format("Evaluating '%s'...", source.getCharacters().toString()));
        }
        return Truffle.getRuntime().createCallTarget(image.getDoItContextNode(source));
    }
}
 
Example #8
Source File: MumblerLanguage.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected CallTarget parse(ParsingRequest parsingRequest) throws IOException {
    MumblerContext context = new MumblerContext(this);
    ListSyntax sexp = Reader.read(parsingRequest.getSource());
    Converter converter = new Converter(this, TAIL_CALL_OPTIMIZATION_ENABLED);
    MumblerNode[] nodes = converter.convertSexp(context, sexp);
    MumblerFunction function = MumblerFunction.create(this, new FrameSlot[] {},
            nodes, context.getGlobalFrame().getFrameDescriptor());
    return function.callTarget;
}
 
Example #9
Source File: TCOInvokeNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Object call(VirtualFrame virtualFrame, CallTarget callTarget,
        Object[] arguments) {
    if (this.isTail()) {
        throw new TailCallException(callTarget, arguments);
    }
    while (true) {
        try {
            return super.call(virtualFrame, callTarget, arguments);
        } catch (TailCallException e) {
            callTarget = e.callTarget;
            arguments = e.arguments;
        }
    }
}
 
Example #10
Source File: UninitializedDispatchNode.java    From mumbler with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Object executeDispatch(VirtualFrame virtualFrame,
        CallTarget callTarget, Object[] arguments) {
    CompilerDirectives.transferToInterpreterAndInvalidate();

    Node cur = this;
    int size = 0;
    while (cur.getParent() instanceof DispatchNode) {
        cur = cur.getParent();
        size++;
    }
    InvokeNode invokeNode = (InvokeNode) cur.getParent();

    DispatchNode replacement;
    if (size < INLINE_CACHE_SIZE) {
        // There's still room in the cache. Add a new DirectDispatchNode.
        DispatchNode next = new UninitializedDispatchNode();
        replacement = new DirectDispatchNode(next, callTarget);
        this.replace(replacement);
    } else {
        replacement = new GenericDispatchNode();
        invokeNode.dispatchNode.replace(replacement);
    }

    // Call function with newly created dispatch node.
    return replacement.executeDispatch(virtualFrame, callTarget, arguments);
}
 
Example #11
Source File: GenericDispatchNode.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Object executeDispatch(VirtualFrame virtualFrame,
        CallTarget callTarget, Object[] argumentValues) {
    return this.callNode.call(callTarget, argumentValues);
}
 
Example #12
Source File: HashemLanguage.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
@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 = HashemLanguageParser.parseHashemiLang(this, source);
    } else {
        Source requestedSource = request.getSource();
        StringBuilder sb = new StringBuilder();
        sb.append("bebin azinja(");
        String sep = "";
        for (String argumentName : request.getArgumentNames()) {
            sb.append(sep);
            sb.append(argumentName);
            sep = ",";
        }
        sb.append(") { return ");
        sb.append(request.getSource().getCharacters());
        sb.append(";}");
        String language = requestedSource.getLanguage() == null ? ID : requestedSource.getLanguage();
        Source decoratedSource = Source.newBuilder(language, sb.toString(), request.getSource().getName()).build();
        functions = HashemLanguageParser.parseHashemiLang(this, decoratedSource);
    }

    RootCallTarget main = functions.get("azinja");
    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 HashemEvalRootNode(this, main, functions);
    } else {
        /*
         * Even without a main function, "evaluating" the parsed source needs to register the
         * functions into the SLContext.
         */
        evalMain = new HashemEvalRootNode(this, null, functions);
    }
    return Truffle.getRuntime().createCallTarget(evalMain);
}
 
Example #13
Source File: DirectDispatchNode.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public DirectDispatchNode(DispatchNode next, CallTarget callTarget) {
    this.cachedCallTarget = callTarget;
    this.callCachedTargetNode = Truffle.getRuntime().createDirectCallNode(
            this.cachedCallTarget);
    this.nextNode = next;
}
 
Example #14
Source File: InvokeNode.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
protected Object call(VirtualFrame virtualFrame, CallTarget callTarget,
        Object[] arguments) {
    return this.dispatchNode.executeDispatch(virtualFrame,
            callTarget, arguments);
}
 
Example #15
Source File: TailCallException.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
public TailCallException(CallTarget callTarget, Object[] arguments) {
    this.callTarget = callTarget;
    this.arguments = arguments;
}
 
Example #16
Source File: DispatchNode.java    From mumbler with GNU General Public License v3.0 4 votes vote down vote up
protected abstract Object executeDispatch(VirtualFrame virtualFrame,
CallTarget callTarget, Object[] argumentValues);
 
Example #17
Source File: ContextObject.java    From trufflesqueak with MIT License 4 votes vote down vote up
public CallTarget getCallTarget() {
    return getBlockOrMethod().getResumptionCallTarget(this);
}
 
Example #18
Source File: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected CallTarget createLoop(Object source) {
    return Truffle.getRuntime().createCallTarget(new LoopRootNode(source, null));
}
 
Example #19
Source File: GrCUDALanguage.java    From grcuda with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected CallTarget parse(ParsingRequest request) {
    ExpressionNode expression = new ParserAntlr().parse(request.getSource());
    GrCUDARootNode newParserRoot = new GrCUDARootNode(this, expression);
    return Truffle.getRuntime().createCallTarget(newParserRoot);
}
 
Example #20
Source File: GrCUDAContext.java    From grcuda with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ConcurrentHashMap<Class<?>, CallTarget> getMapCallTargets() {
    return uncachedMapCallTargets;
}
 
Example #21
Source File: HashemEvalBuiltin.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
protected CallTarget parse(String id, String code, HashemContext context) {
    final Source source = Source.newBuilder(id, code, "(eval)").build();
    return context.parse(source);
}