Search in sources :

Example 51 with Specialization

use of com.oracle.truffle.api.dsl.Specialization in project graal by oracle.

the class ExecuteMethodNode method doVarArgs.

@SuppressWarnings("unused")
@Specialization(guards = { "method.isVarArgs()", "method == cachedMethod" }, limit = "LIMIT")
Object doVarArgs(SingleMethodDesc method, Object obj, Object[] args, Object languageContext, @Cached("method") SingleMethodDesc cachedMethod, @Cached("create()") ToJavaNode toJavaNode, @Cached("createClassProfile()") ValueProfile receiverProfile) {
    int parameterCount = cachedMethod.getParameterCount();
    int minArity = parameterCount - 1;
    if (args.length < minArity) {
        throw ArityException.raise(minArity, args.length);
    }
    Class<?>[] types = cachedMethod.getParameterTypes();
    Type[] genericTypes = cachedMethod.getGenericParameterTypes();
    Object[] convertedArguments = new Object[args.length];
    for (int i = 0; i < minArity; i++) {
        convertedArguments[i] = toJavaNode.execute(args[i], types[i], genericTypes[i], languageContext);
    }
    if (asVarArgs(args, cachedMethod)) {
        for (int i = minArity; i < args.length; i++) {
            Class<?> expectedType = types[minArity].getComponentType();
            Type expectedGenericType = getGenericComponentType(genericTypes[minArity]);
            convertedArguments[i] = toJavaNode.execute(args[i], expectedType, expectedGenericType, languageContext);
        }
        convertedArguments = createVarArgsArray(cachedMethod, convertedArguments, parameterCount);
    } else {
        convertedArguments[minArity] = toJavaNode.execute(args[minArity], types[minArity], genericTypes[minArity], languageContext);
    }
    return doInvoke(cachedMethod, receiverProfile.profile(obj), convertedArguments, languageContext);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 52 with Specialization

use of com.oracle.truffle.api.dsl.Specialization in project graal by oracle.

the class ExecuteMethodNode method doOverloadedCached.

// Note: checkArgTypes must be evaluated after selectOverload.
@SuppressWarnings("unused")
@ExplodeLoop
@Specialization(guards = { "method == cachedMethod", "checkArgTypes(args, cachedArgTypes, toJavaNode, asVarArgs)" }, limit = "LIMIT")
Object doOverloadedCached(OverloadedMethodDesc method, Object obj, Object[] args, Object languageContext, @Cached("method") OverloadedMethodDesc cachedMethod, @Cached("create()") ToJavaNode toJavaNode, @Cached(value = "createArgTypesArray(args)", dimensions = 1) Type[] cachedArgTypes, @Cached("selectOverload(method, args, languageContext, toJavaNode, cachedArgTypes)") SingleMethodDesc overload, @Cached("asVarArgs(args, overload)") boolean asVarArgs, @Cached("createClassProfile()") ValueProfile receiverProfile) {
    assert overload == selectOverload(method, args, languageContext, toJavaNode);
    Class<?>[] types = overload.getParameterTypes();
    Type[] genericTypes = overload.getGenericParameterTypes();
    Object[] convertedArguments = new Object[cachedArgTypes.length];
    if (asVarArgs) {
        assert overload.isVarArgs();
        int parameterCount = overload.getParameterCount();
        for (int i = 0; i < cachedArgTypes.length; i++) {
            Class<?> expectedType = i < parameterCount - 1 ? types[i] : types[parameterCount - 1].getComponentType();
            Type expectedGenericType = i < parameterCount - 1 ? genericTypes[i] : getGenericComponentType(genericTypes[parameterCount - 1]);
            convertedArguments[i] = toJavaNode.execute(args[i], expectedType, expectedGenericType, languageContext);
        }
        convertedArguments = createVarArgsArray(overload, convertedArguments, parameterCount);
    } else {
        for (int i = 0; i < cachedArgTypes.length; i++) {
            convertedArguments[i] = toJavaNode.execute(args[i], types[i], genericTypes[i], languageContext);
        }
    }
    return doInvoke(overload, receiverProfile.profile(obj), convertedArguments, languageContext);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) Specialization(com.oracle.truffle.api.dsl.Specialization) ExplodeLoop(com.oracle.truffle.api.nodes.ExplodeLoop)

Example 53 with Specialization

use of com.oracle.truffle.api.dsl.Specialization in project sulong by graalvm.

the class LLVMTruffleReadNBytes method interop.

@Specialization
protected Object interop(LLVMTruffleObject objectWithOffset, int n, @Cached("createForeignReadNode()") Node foreignRead, @Cached("createToByteNode()") ForeignToLLVM toLLVM, @Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
    long offset = objectWithOffset.getOffset();
    TruffleObject object = objectWithOffset.getObject();
    byte[] chars = new byte[n];
    for (int i = 0; i < n; i++) {
        Object rawValue;
        try {
            rawValue = ForeignAccess.sendRead(foreignRead, object, offset + i);
        } catch (UnknownIdentifierException | UnsupportedMessageException e) {
            CompilerDirectives.transferToInterpreter();
            throw new IllegalStateException(e);
        }
        chars[i] = (byte) toLLVM.executeWithTarget(rawValue);
    }
    TruffleObject ret = (TruffleObject) ctxRef.get().getEnv().asGuestValue(chars);
    return new LLVMTruffleObject(LLVMTypedForeignObject.createUnknown(ret));
}
Also used : UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTypedForeignObject(com.oracle.truffle.llvm.runtime.interop.LLVMTypedForeignObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 54 with Specialization

use of com.oracle.truffle.api.dsl.Specialization in project sulong by graalvm.

the class LLVMTruffleReadBytes method doIntrinsic.

@Specialization
protected Object doIntrinsic(LLVMAddress value, @Cached("getLLVMMemory()") LLVMMemory memory, @Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
    byte c;
    long ptr = value.getVal();
    int count = 0;
    while ((c = memory.getI8(ptr)) != 0) {
        count++;
        ptr += Byte.BYTES;
    }
    byte[] bytes = new byte[count];
    count = 0;
    ptr = value.getVal();
    while ((c = memory.getI8(ptr)) != 0) {
        bytes[count++] = c;
        ptr += Byte.BYTES;
    }
    TruffleObject ret = (TruffleObject) ctxRef.get().getEnv().asGuestValue(bytes);
    return new LLVMTruffleObject(LLVMTypedForeignObject.createUnknown(ret));
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) LLVMTruffleObject(com.oracle.truffle.llvm.runtime.LLVMTruffleObject) Specialization(com.oracle.truffle.api.dsl.Specialization)

Example 55 with Specialization

use of com.oracle.truffle.api.dsl.Specialization in project sulong by graalvm.

the class LLVMNativeDispatchNode method doCached.

@Specialization(guards = "function.getVal() == cachedFunction.getVal()")
@SuppressWarnings("unused")
protected Object doCached(LLVMAddress function, Object[] arguments, @Cached("getContextReference()") ContextReference<LLVMContext> context, @Cached("function") LLVMAddress cachedFunction, @Cached("identityFunction()") TruffleObject identity, @Cached("dispatchIdentity(identity, cachedFunction.getVal())") TruffleObject nativeFunctionHandle, @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, nativeFunctionHandle, 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

Specialization (com.oracle.truffle.api.dsl.Specialization)73 LLVMTruffleObject (com.oracle.truffle.llvm.runtime.LLVMTruffleObject)35 ExplodeLoop (com.oracle.truffle.api.nodes.ExplodeLoop)27 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)16 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)6 UnexpectedResultException (com.oracle.truffle.api.nodes.UnexpectedResultException)6 LLVMAddress (com.oracle.truffle.llvm.runtime.LLVMAddress)6 StackPointer (com.oracle.truffle.llvm.runtime.memory.LLVMStack.StackPointer)6 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)5 FileValue (cz.cuni.mff.d3s.trupple.language.runtime.customvalues.FileValue)3 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)3 Frame (com.oracle.truffle.api.frame.Frame)2 InteropException (com.oracle.truffle.api.interop.InteropException)2 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)2 LLVMFunctionDescriptor (com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor)2 LLVM80BitFloat (com.oracle.truffle.llvm.runtime.floating.LLVM80BitFloat)2 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 Type (java.lang.reflect.Type)2 Assumption (com.oracle.truffle.api.Assumption)1