Search in sources :

Example 36 with CEntryPoint

use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.

the class CEntryPointNativeFunctions method createIsolate.

@Uninterruptible(reason = UNINTERRUPTIBLE_REASON)
@CEntryPoint(name = "create_isolate", documentation = { "Create a new isolate, considering the passed parameters (which may be NULL).", "Returns 0 on success, or a non-zero value on failure.", "On success, the current thread is attached to the created isolate, and the", "address of the isolate structure is written to the passed pointer." })
@CEntryPointOptions(prologue = NoPrologue.class, epilogue = NoEpilogue.class, nameTransformation = NameTransformation.class)
public static int createIsolate(CEntryPointCreateIsolateParameters params, IsolatePointer isolate) {
    int result = CEntryPointActions.enterCreateIsolate(params);
    if (result == 0) {
        isolate.write(CEntryPointContext.getCurrentIsolate());
        result = CEntryPointActions.leave();
    }
    return result;
}
Also used : CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 37 with CEntryPoint

use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.

the class JNIFunctionTablesFeature method beforeAnalysis.

@Override
public void beforeAnalysis(BeforeAnalysisAccess arg) {
    BeforeAnalysisAccessImpl access = (BeforeAnalysisAccessImpl) arg;
    AnalysisMetaAccess metaAccess = access.getMetaAccess();
    JNIFunctionTables.create();
    NativeLibraries nativeLibraries = access.getNativeLibraries();
    AnalysisType invokeInterface = metaAccess.lookupJavaType(JNIInvokeInterface.class);
    invokeInterfaceMetadata = (StructInfo) nativeLibraries.findElementInfo(invokeInterface);
    AnalysisType functionTable = metaAccess.lookupJavaType(JNINativeInterface.class);
    functionTableMetadata = (StructInfo) nativeLibraries.findElementInfo(functionTable);
    // Manually add functions as entry points so this is only done when JNI features are enabled
    AnalysisType invokes = metaAccess.lookupJavaType(JNIInvocationInterface.class);
    AnalysisType exports = metaAccess.lookupJavaType(JNIInvocationInterface.Exports.class);
    AnalysisType functions = metaAccess.lookupJavaType(JNIFunctions.class);
    Stream<AnalysisMethod> analysisMethods = Stream.of(invokes, functions, exports).flatMap(t -> Stream.of(t.getDeclaredMethods()));
    Stream<AnalysisMethod> unimplementedMethods = Stream.of((AnalysisMethod) getSingleMethod(metaAccess, UnimplementedWithJNIEnvArgument.class), (AnalysisMethod) getSingleMethod(metaAccess, UnimplementedWithJavaVMArgument.class));
    Stream.concat(analysisMethods, unimplementedMethods).forEach(method -> {
        CEntryPoint annotation = method.getAnnotation(CEntryPoint.class);
        assert annotation != null : "only entry points allowed in class";
        CEntryPointCallStubSupport.singleton().registerStubForMethod(method, () -> CEntryPointData.create(method));
    });
    ArrayList<ResolvedJavaMethod> generated = new ArrayList<>();
    MetaAccessProvider wrappedMetaAccess = metaAccess.getWrapped();
    ResolvedJavaType generatedMethodClass = wrappedMetaAccess.lookupJavaType(JNIFunctions.class);
    ConstantPool constantPool = generatedMethodClass.getDeclaredMethods()[0].getConstantPool();
    // Generate JNI field accessors
    EnumSet<JavaKind> fldKinds = jniKinds.clone();
    fldKinds.remove(JavaKind.Void);
    for (JavaKind kind : fldKinds) {
        boolean[] trueFalse = { true, false };
        for (boolean isSetter : trueFalse) {
            for (boolean isStatic : trueFalse) {
                JNIFieldAccessorMethod method = new JNIFieldAccessorMethod(kind, isSetter, isStatic, generatedMethodClass, constantPool, wrappedMetaAccess);
                AnalysisMethod analysisMethod = access.getUniverse().lookup(method);
                access.getBigBang().addRootMethod(analysisMethod).registerAsEntryPoint(method.createEntryPointData());
                generated.add(method);
            }
        }
    }
    // Generate JNI primitive array operations
    EnumSet<JavaKind> primitiveArrayKinds = jniKinds.clone();
    primitiveArrayKinds.remove(JavaKind.Void);
    primitiveArrayKinds.remove(JavaKind.Object);
    for (JavaKind kind : primitiveArrayKinds) {
        for (Operation op : Operation.values()) {
            JNIPrimitiveArrayOperationMethod method = new JNIPrimitiveArrayOperationMethod(kind, op, generatedMethodClass, constantPool, wrappedMetaAccess);
            AnalysisMethod analysisMethod = access.getUniverse().lookup(method);
            access.getBigBang().addRootMethod(analysisMethod).registerAsEntryPoint(method.createEntryPointData());
            generated.add(method);
        }
    }
    generatedMethods = generated.toArray(new ResolvedJavaMethod[0]);
}
Also used : BeforeAnalysisAccessImpl(com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl) AnalysisType(com.oracle.graal.pointsto.meta.AnalysisType) NativeLibraries(com.oracle.svm.hosted.c.NativeLibraries) ArrayList(java.util.ArrayList) AnalysisMetaAccess(com.oracle.graal.pointsto.meta.AnalysisMetaAccess) Operation(com.oracle.svm.jni.hosted.JNIPrimitiveArrayOperationMethod.Operation) JNIFieldAccessorMethod(com.oracle.svm.jni.hosted.JNIFieldAccessorMethod) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) AnalysisMethod(com.oracle.graal.pointsto.meta.AnalysisMethod) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) ConstantPool(jdk.vm.ci.meta.ConstantPool) JNIPrimitiveArrayOperationMethod(com.oracle.svm.jni.hosted.JNIPrimitiveArrayOperationMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 38 with CEntryPoint

use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.

the class JNIFunctions method NewGlobalRef.

/*
     * jobject NewGlobalRef(JNIEnv *env, jobject obj);
     */
@CEntryPoint
@CEntryPointOptions(prologue = JNIEnvironmentEnterPrologue.class, exceptionHandler = JNIExceptionHandlerReturnNullHandle.class, publishAs = Publish.NotPublished, include = CEntryPointOptions.NotIncludedAutomatically.class)
static JNIObjectHandle NewGlobalRef(JNIEnvironment env, JNIObjectHandle handle) {
    JNIObjectHandle result = JNIObjectHandles.nullHandle();
    Object obj = JNIObjectHandles.getObject(handle);
    if (obj != null) {
        result = (JNIObjectHandle) JNIGlobalHandles.singleton().create(obj);
    }
    return result;
}
Also used : JNIObjectHandle(com.oracle.svm.jni.nativeapi.JNIObjectHandle) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPointOptions(com.oracle.svm.core.c.function.CEntryPointOptions)

Example 39 with CEntryPoint

use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.

the class JNIFunctions method NewString.

/*
     * jstring NewString(JNIEnv *env, const jchar *unicodeChars, jsize len);
     */
@CEntryPoint
@CEntryPointOptions(prologue = JNIEnvironmentEnterPrologue.class, exceptionHandler = JNIExceptionHandlerReturnNullHandle.class, publishAs = Publish.NotPublished, include = CEntryPointOptions.NotIncludedAutomatically.class)
static JNIObjectHandle NewString(JNIEnvironment env, CShortPointer unicode, int len) {
    String str;
    char[] chars = new char[len];
    for (int i = 0; i < chars.length; i++) {
        int value = Short.toUnsignedInt(unicode.read(i));
        chars[i] = (char) value;
    }
    str = new String(chars);
    return JNIThreadLocalHandles.get().create(str);
}
Also used : CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPointOptions(com.oracle.svm.core.c.function.CEntryPointOptions)

Example 40 with CEntryPoint

use of org.graalvm.nativeimage.c.function.CEntryPoint in project graal by oracle.

the class JNIFunctions method FromReflectedMethod.

/*
     * jmethodID FromReflectedMethod(JNIEnv *env, jobject method);
     */
@CEntryPoint
@CEntryPointOptions(prologue = JNIEnvironmentEnterPrologue.class, exceptionHandler = JNIExceptionHandlerReturnNullWord.class, publishAs = Publish.NotPublished, include = CEntryPointOptions.NotIncludedAutomatically.class)
static JNIMethodId FromReflectedMethod(JNIEnvironment env, JNIObjectHandle methodHandle) {
    JNIMethodId methodId = Word.nullPointer();
    if (JNIAccessFeature.singleton().haveJavaRuntimeReflectionSupport()) {
        Executable method = JNIObjectHandles.getObject(methodHandle);
        if (method != null) {
            boolean isStatic = Modifier.isStatic(method.getModifiers());
            JNIAccessibleMethodDescriptor descriptor = JNIAccessibleMethodDescriptor.of(method);
            methodId = JNIReflectionDictionary.singleton().getMethodID(method.getDeclaringClass(), descriptor, isStatic);
        }
    }
    return methodId;
}
Also used : JNIAccessibleMethodDescriptor(com.oracle.svm.jni.access.JNIAccessibleMethodDescriptor) Executable(java.lang.reflect.Executable) JNIMethodId(com.oracle.svm.jni.nativeapi.JNIMethodId) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPointOptions(com.oracle.svm.core.c.function.CEntryPointOptions)

Aggregations

CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)81 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)26 Value (org.graalvm.polyglot.Value)23 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 Context (org.graalvm.polyglot.Context)19 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)7 ObjectHandle (org.graalvm.nativeimage.ObjectHandle)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)4 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)4 JNIObjectHandle (com.oracle.svm.jni.nativeapi.JNIObjectHandle)3 PinnedObject (org.graalvm.nativeimage.PinnedObject)3 Log (com.oracle.svm.core.log.Log)2 JNIAccessibleMethodDescriptor (com.oracle.svm.jni.access.JNIAccessibleMethodDescriptor)2 JNINativeLinkage (com.oracle.svm.jni.access.JNINativeLinkage)2 JNIMethodId (com.oracle.svm.jni.nativeapi.JNIMethodId)2 JNINativeMethod (com.oracle.svm.jni.nativeapi.JNINativeMethod)2 Executable (java.lang.reflect.Executable)2 Method (java.lang.reflect.Method)2 ByteBuffer (java.nio.ByteBuffer)2