Search in sources :

Example 91 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project sulong by graalvm.

the class LLVMDispatchNode method doNative.

@Specialization(replaces = "doCachedNative", guards = "descriptor.isNativeFunction()")
protected Object doNative(LLVMFunctionDescriptor descriptor, Object[] arguments, @Cached("createToNativeNodes()") LLVMNativeConvertNode[] toNative, @Cached("createFromNativeNode()") LLVMNativeConvertNode fromNative, @Cached("createNativeCallNode()") Node nativeCall, @Cached("getBindNode()") Node bindNode, @Cached("getContextReference()") ContextReference<LLVMContext> context, @Cached("nativeCallStatisticsEnabled(context)") boolean statistics) {
    Object[] nativeArgs = prepareNativeArguments(arguments, toNative);
    TruffleObject boundSymbol = LLVMNativeCallUtils.bindNativeSymbol(bindNode, descriptor.getNativeFunction(), getSignature());
    Object returnValue;
    try (StackPointer save = ((StackPointer) arguments[0]).newFrame()) {
        returnValue = LLVMNativeCallUtils.callNativeFunction(statistics, context, nativeCall, boundSymbol, nativeArgs, descriptor);
    }
    return fromNative.executeConvert(returnValue);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StackPointer(com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 92 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project sulong by graalvm.

the class LLVMDispatchNode method doCachedNative.

/*
     * Function is not defined in the user program (not available as LLVM IR). No intrinsic
     * available. We do a native call.
     */
@Specialization(limit = "10", guards = { "descriptor == cachedDescriptor", "descriptor.isNativeFunction()" })
protected Object doCachedNative(@SuppressWarnings("unused") LLVMFunctionDescriptor descriptor, Object[] arguments, @Cached("descriptor") LLVMFunctionDescriptor cachedDescriptor, @Cached("createToNativeNodes()") LLVMNativeConvertNode[] toNative, @Cached("createFromNativeNode()") LLVMNativeConvertNode fromNative, @Cached("createNativeCallNode()") Node nativeCall, @Cached("bindSymbol(cachedDescriptor)") TruffleObject cachedBoundFunction, @Cached("getContextReference()") ContextReference<LLVMContext> context, @Cached("nativeCallStatisticsEnabled(context)") boolean statistics) {
    Object[] nativeArgs = prepareNativeArguments(arguments, toNative);
    Object returnValue;
    try (StackPointer save = ((StackPointer) arguments[0]).newFrame()) {
        returnValue = LLVMNativeCallUtils.callNativeFunction(statistics, context, nativeCall, cachedBoundFunction, nativeArgs, cachedDescriptor);
    }
    return fromNative.executeConvert(returnValue);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StackPointer(com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 93 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project sulong by graalvm.

the class LLVMGlobalRootNode method execute.

@Override
@ExplodeLoop
public Object execute(VirtualFrame frame) {
    try (StackPointer basePointer = getContext().getThreadingStack().getStack().newFrame()) {
        try {
            TruffleObject appPath = (TruffleObject) ctxRef.get().getEnv().asGuestValue(applicationPath.getBytes());
            LLVMTruffleObject applicationPathObj = new LLVMTruffleObject(LLVMTypedForeignObject.createUnknown(appPath));
            Object[] realArgs = new Object[] { basePointer, mainFunctionType, applicationPathObj };
            Object result = startFunction.call(realArgs);
            getContext().awaitThreadTermination();
            return result;
        } catch (LLVMExitException e) {
            LLVMContext context = getContext();
            // if any variant of exit or abort was called, we know that all the necessary
            // cleanup was already done
            context.setCleanupNecessary(false);
            context.awaitThreadTermination();
            return e.getReturnCode();
        } catch (SulongRuntimeException e) {
            CompilerDirectives.transferToInterpreter();
            throw e;
        } catch (GuestLanguageRuntimeException e) {
            CompilerDirectives.transferToInterpreter();
            return e.handleExit();
        } finally {
            // if not done already, we want at least call a shutdown command
            getContext().shutdownThreads();
        }
    }
}
Also used : LLVMContext(com.oracle.truffle.llvm.runtime.LLVMContext) SulongRuntimeException(com.oracle.truffle.llvm.runtime.SulongRuntimeException) GuestLanguageRuntimeException(com.oracle.truffle.llvm.runtime.GuestLanguageRuntimeException) LLVMExitException(com.oracle.truffle.llvm.runtime.LLVMExitException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTypedForeignObject(com.oracle.truffle.llvm.runtime.interop.LLVMTypedForeignObject) StackPointer(com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) ExplodeLoop(com.oracle.truffle.api.nodes.ExplodeLoop)

Example 94 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project sulong by graalvm.

the class LLVMToFunctionNode method doTruffleObject.

@Specialization
protected Object doTruffleObject(LLVMTruffleObject from, @Cached("create()") LLVMAsForeignNode asForeign) {
    TruffleObject foreign = asForeign.execute(from);
    if (ForeignAccess.sendIsNull(isNull, foreign)) {
        return LLVMAddress.fromLong(0);
    } else if (ForeignAccess.sendIsExecutable(isExecutable, foreign)) {
        return from;
    }
    CompilerDirectives.transferToInterpreter();
    throw new IllegalStateException("Not a function");
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 95 with TruffleObject

use of com.oracle.truffle.api.interop.TruffleObject in project sulong by graalvm.

the class LLVMNativeDispatchNode method doGeneric.

@Specialization
protected Object doGeneric(LLVMAddress function, Object[] arguments, @Cached("getContextReference()") ContextReference<LLVMContext> context, @Cached("identityFunction()") TruffleObject identity, @Cached("createToNativeNodes()") LLVMNativeConvertNode[] toNative, @Cached("createFromNativeNode()") LLVMNativeConvertNode fromNative, @Cached("nativeCallStatisticsEnabled(context)") boolean statistics) {
    Object[] nativeArgs = prepareNativeArguments(arguments, toNative);
    Object returnValue;
    try (StackPointer save = ((StackPointer) arguments[0]).newFrame()) {
        returnValue = LLVMNativeCallUtils.callNativeFunction(statistics, context, nativeCallNode, dispatchIdentity(identity, function.getVal()), nativeArgs, null);
    }
    return fromNative.executeConvert(returnValue);
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) StackPointer(com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer) Specialization(com.oracle.truffle.api.dsl.Specialization)

Aggregations

TruffleObject (com.oracle.truffle.api.interop.TruffleObject)201 Test (org.junit.Test)135 ValueHostInteropTest (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest)34 InteropException (com.oracle.truffle.api.interop.InteropException)18 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)17 Specialization (com.oracle.truffle.api.dsl.Specialization)16 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)14 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)12 ArrayTruffleObject (com.oracle.truffle.api.test.polyglot.ValueHostInteropTest.ArrayTruffleObject)10 Node (com.oracle.truffle.api.nodes.Node)9 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 CallTarget (com.oracle.truffle.api.CallTarget)7 StackPointer (com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer)7 TestCallback (com.oracle.truffle.nfi.test.interop.TestCallback)7 LinkedHashMap (java.util.LinkedHashMap)7 Source (com.oracle.truffle.api.source.Source)6 Method (java.lang.reflect.Method)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5