Search in sources :

Example 1 with Pointer

use of com.oracle.truffle.espresso.ffi.Pointer in project graal by oracle.

the class StructsAccess method initializeStructs.

@SuppressWarnings("try")
private static Structs initializeStructs(EspressoContext context, TruffleObject initializeStructs, TruffleObject lookupMemberOffset) {
    try (DebugCloseable timer = STRUCTS_TIMER.scope(context.getTimers())) {
        Structs[] box = new Structs[1];
        Callback doInitStructs = new Callback(1, new Callback.Function() {

            @Override
            @CompilerDirectives.TruffleBoundary
            public Object call(Object... args) {
                TruffleObject memberInfoPtr = (TruffleObject) args[0];
                box[0] = new Structs(context.getJNI(), memberInfoPtr, lookupMemberOffset);
                return RawPointer.nullInstance();
            }
        });
        /*
             * Go down to native to initialize the data structure storing the offsets of used
             * structs (The memberInfoPtr seen in the callback). This will get back to java code
             * once the data structure is created. Once we get out of the native call, the structure
             * is freed and cannot be used anymore.
             */
        @Pointer TruffleObject closure = context.getNativeAccess().createNativeClosure(doInitStructs, NativeSignature.create(NativeType.VOID, NativeType.POINTER));
        try {
            InteropLibrary.getUncached().execute(initializeStructs, closure);
        } catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
            throw EspressoError.shouldNotReachHere();
        }
        return box[0];
    }
}
Also used : RawPointer(com.oracle.truffle.espresso.ffi.RawPointer) Pointer(com.oracle.truffle.espresso.ffi.Pointer) ArityException(com.oracle.truffle.api.interop.ArityException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Callback(com.oracle.truffle.espresso.jni.Callback) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) DebugCloseable(com.oracle.truffle.espresso.perf.DebugCloseable)

Example 2 with Pointer

use of com.oracle.truffle.espresso.ffi.Pointer in project graal by oracle.

the class JniEnv method GetPrimitiveArrayCritical.

@JniImpl
@Pointer
public TruffleObject GetPrimitiveArrayCritical(@JavaType(Object.class) StaticObject object, @Pointer TruffleObject isCopyPtr) {
    if (!getUncached().isNull(isCopyPtr)) {
        ByteBuffer isCopyBuf = NativeUtils.directByteBuffer(isCopyPtr, 1);
        // Always copy since pinning is not supported.
        isCopyBuf.put((byte) 1);
    }
    StaticObject array = object;
    StaticObject clazz = GetObjectClass(array);
    JavaKind componentKind = ((ArrayKlass) clazz.getMirrorKlass()).getComponentType().getJavaKind();
    assert componentKind.isPrimitive();
    int length = GetArrayLength(array);
    ByteBuffer region = allocateDirect(length, componentKind);
    @Pointer TruffleObject addressPtr = NativeUtils.byteBufferPointer(region);
    // @formatter:off
    switch(componentKind) {
        case Boolean:
            GetBooleanArrayRegion(array, 0, length, addressPtr);
            break;
        case Byte:
            GetByteArrayRegion(array, 0, length, addressPtr);
            break;
        case Short:
            GetShortArrayRegion(array, 0, length, addressPtr);
            break;
        case Char:
            GetCharArrayRegion(array, 0, length, addressPtr);
            break;
        case Int:
            GetIntArrayRegion(array, 0, length, addressPtr);
            break;
        case Float:
            GetFloatArrayRegion(array, 0, length, addressPtr);
            break;
        case Long:
            GetLongArrayRegion(array, 0, length, addressPtr);
            break;
        case Double:
            GetDoubleArrayRegion(array, 0, length, addressPtr);
            break;
        // fall through
        case Object:
        // fall through
        case Void:
        // fall through
        case Illegal:
        default:
            throw EspressoError.shouldNotReachHere();
    }
    return addressPtr;
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer) ByteBuffer(java.nio.ByteBuffer) JavaKind(com.oracle.truffle.espresso.meta.JavaKind) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer)

Example 3 with Pointer

use of com.oracle.truffle.espresso.ffi.Pointer in project graal by oracle.

the class JniEnv method GetLongArrayElements.

@JniImpl
@TruffleBoundary
@Pointer
public TruffleObject GetLongArrayElements(@JavaType(long[].class) StaticObject array, @Pointer TruffleObject isCopyPtr) {
    if (!getUncached().isNull(isCopyPtr)) {
        ByteBuffer isCopyBuf = NativeUtils.directByteBuffer(isCopyPtr, 1);
        // Always copy since pinning is not supported.
        isCopyBuf.put((byte) 1);
    }
    long[] data = array.unwrap();
    ByteBuffer bytes = allocateDirect(data.length, JavaKind.Long);
    LongBuffer elements = bytes.asLongBuffer();
    elements.put(data);
    return NativeUtils.byteBufferPointer(bytes);
}
Also used : LongBuffer(java.nio.LongBuffer) ByteBuffer(java.nio.ByteBuffer) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer)

Example 4 with Pointer

use of com.oracle.truffle.espresso.ffi.Pointer in project graal by oracle.

the class JniEnv method GetIntArrayElements.

@JniImpl
@TruffleBoundary
@Pointer
public TruffleObject GetIntArrayElements(@JavaType(int[].class) StaticObject array, @Pointer TruffleObject isCopyPtr) {
    if (!getUncached().isNull(isCopyPtr)) {
        ByteBuffer isCopyBuf = NativeUtils.directByteBuffer(isCopyPtr, 1);
        // Always copy since pinning is not supported.
        isCopyBuf.put((byte) 1);
    }
    int[] data = array.unwrap();
    ByteBuffer bytes = allocateDirect(data.length, JavaKind.Int);
    IntBuffer elements = bytes.asIntBuffer();
    elements.put(data);
    return NativeUtils.byteBufferPointer(bytes);
}
Also used : IntBuffer(java.nio.IntBuffer) ByteBuffer(java.nio.ByteBuffer) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer)

Example 5 with Pointer

use of com.oracle.truffle.espresso.ffi.Pointer in project graal by oracle.

the class NativeEnv method createNativeClosureForFactory.

@TruffleBoundary
private TruffleObject createNativeClosureForFactory(CallableFromNative.Factory factory, String methodName) {
    // Dummy placeholder for unimplemented/unknown methods.
    if (factory == null) {
        String envName = NativeEnv.this.getClass().getSimpleName();
        getLogger().log(Level.FINER, "Fetching unknown/unimplemented {0} method: {1}", new Object[] { envName, methodName });
        @Pointer TruffleObject errorClosure = getNativeAccess().createNativeClosure(new Callback(0, new Callback.Function() {

            @Override
            public Object call(Object... args) {
                CompilerDirectives.transferToInterpreter();
                getLogger().log(Level.SEVERE, "Calling unimplemented {0} method: {1}", new Object[] { envName, methodName });
                throw EspressoError.unimplemented(envName + " method: " + methodName);
            }
        }), NativeSignature.create(NativeType.VOID));
        nativeClosures.add(errorClosure);
        return errorClosure;
    }
    NativeSignature signature = factory.jniNativeSignature();
    Callback target = intrinsicWrapper(factory);
    @Pointer TruffleObject nativeClosure = getNativeAccess().createNativeClosure(target, signature);
    nativeClosures.add(nativeClosure);
    return nativeClosure;
}
Also used : NativeSignature(com.oracle.truffle.espresso.ffi.NativeSignature) Pointer(com.oracle.truffle.espresso.ffi.Pointer) RawPointer(com.oracle.truffle.espresso.ffi.RawPointer) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

Pointer (com.oracle.truffle.espresso.ffi.Pointer)18 RawPointer (com.oracle.truffle.espresso.ffi.RawPointer)18 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ByteBuffer (java.nio.ByteBuffer)12 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)8 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)3 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 CharBuffer (java.nio.CharBuffer)2 CallTarget (com.oracle.truffle.api.CallTarget)1 AbstractTruffleException (com.oracle.truffle.api.exception.AbstractTruffleException)1 ArityException (com.oracle.truffle.api.interop.ArityException)1 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)1 Source (com.oracle.truffle.api.source.Source)1 NativeSignature (com.oracle.truffle.espresso.ffi.NativeSignature)1 SuppressFBWarnings (com.oracle.truffle.espresso.impl.SuppressFBWarnings)1 Callback (com.oracle.truffle.espresso.jni.Callback)1 JavaKind (com.oracle.truffle.espresso.meta.JavaKind)1 Meta (com.oracle.truffle.espresso.meta.Meta)1 DebugCloseable (com.oracle.truffle.espresso.perf.DebugCloseable)1 DoubleBuffer (java.nio.DoubleBuffer)1