Search in sources :

Example 1 with CFunctionPointer

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

the class JNIFunctionTablesFeature method buildInvokesInitializer.

private JNIStructFunctionsInitializer<JNIInvokeInterface> buildInvokesInitializer(CompilationAccessImpl access, CFunctionPointer unimplemented) {
    HostedType invokes = access.getMetaAccess().lookupJavaType(JNIInvocationInterface.class);
    HostedMethod[] methods = invokes.getDeclaredMethods();
    int index = 0;
    int[] offsets = new int[methods.length];
    CFunctionPointer[] pointers = new CFunctionPointer[offsets.length];
    for (HostedMethod method : methods) {
        StructFieldInfo field = findFieldFor(invokeInterfaceMetadata, method.getName());
        offsets[index] = field.getOffsetInfo().getProperty();
        pointers[index] = getStubFunctionPointer(access, method);
        index++;
    }
    VMError.guarantee(index == offsets.length && index == pointers.length);
    return new JNIStructFunctionsInitializer<>(JNIInvokeInterface.class, offsets, pointers, unimplemented);
}
Also used : HostedType(com.oracle.svm.hosted.meta.HostedType) HostedMethod(com.oracle.svm.hosted.meta.HostedMethod) StructFieldInfo(com.oracle.svm.hosted.c.info.StructFieldInfo) CFunctionPointer(org.graalvm.nativeimage.c.function.CFunctionPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 2 with CFunctionPointer

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

the class JNIFunctionTablesFeature method buildFunctionsInitializer.

private JNIStructFunctionsInitializer<JNINativeInterface> buildFunctionsInitializer(CompilationAccessImpl access, CFunctionPointer unimplemented) {
    Class<JNIFunctions> clazz = JNIFunctions.class;
    HostedType functions = access.getMetaAccess().lookupJavaType(clazz);
    HostedMethod[] methods = functions.getDeclaredMethods();
    int index = 0;
    int count = methods.length + generatedMethods.length;
    // Call, CallStatic, CallNonvirtual: for each return value kind: array, va_list, varargs
    // NewObject: array, va_list, varargs
    count += (jniKinds.size() * 3 + 1) * 3;
    int[] offsets = new int[count];
    CFunctionPointer[] pointers = new CFunctionPointer[offsets.length];
    for (HostedMethod method : methods) {
        StructFieldInfo field = findFieldFor(functionTableMetadata, method.getName());
        offsets[index] = field.getOffsetInfo().getProperty();
        pointers[index] = getStubFunctionPointer(access, method);
        index++;
    }
    for (ResolvedJavaMethod accessor : generatedMethods) {
        StructFieldInfo field = findFieldFor(functionTableMetadata, accessor.getName());
        AnalysisUniverse analysisUniverse = access.getUniverse().getBigBang().getUniverse();
        AnalysisMethod analysisMethod = analysisUniverse.lookup(accessor);
        HostedMethod hostedMethod = access.getUniverse().lookup(analysisMethod);
        offsets[index] = field.getOffsetInfo().getProperty();
        pointers[index] = MethodPointer.factory(hostedMethod);
        index++;
    }
    for (CallVariant variant : CallVariant.values()) {
        CFunctionPointer trampoline = prepareCallTrampoline(access, variant, false);
        String suffix = (variant == CallVariant.ARRAY) ? "A" : ((variant == CallVariant.VA_LIST) ? "V" : "");
        CFunctionPointer nonvirtualTrampoline = prepareCallTrampoline(access, variant, true);
        for (JavaKind kind : jniKinds) {
            String[] prefixes = { "Call", "CallStatic" };
            for (String prefix : prefixes) {
                StructFieldInfo field = findFieldFor(functionTableMetadata, prefix + kind.name() + "Method" + suffix);
                offsets[index] = field.getOffsetInfo().getProperty();
                pointers[index] = trampoline;
                index++;
            }
            StructFieldInfo field = findFieldFor(functionTableMetadata, "CallNonvirtual" + kind.name() + "Method" + suffix);
            offsets[index] = field.getOffsetInfo().getProperty();
            pointers[index] = nonvirtualTrampoline;
            index++;
        }
        StructFieldInfo field = findFieldFor(functionTableMetadata, "NewObject" + suffix);
        offsets[index] = field.getOffsetInfo().getProperty();
        pointers[index] = trampoline;
        index++;
    }
    VMError.guarantee(index == offsets.length && index == pointers.length);
    return new JNIStructFunctionsInitializer<>(JNINativeInterface.class, offsets, pointers, unimplemented);
}
Also used : CallVariant(com.oracle.svm.jni.hosted.JNIJavaCallWrapperMethod.CallVariant) StructFieldInfo(com.oracle.svm.hosted.c.info.StructFieldInfo) CFunctionPointer(org.graalvm.nativeimage.c.function.CFunctionPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) HostedType(com.oracle.svm.hosted.meta.HostedType) AnalysisMethod(com.oracle.graal.pointsto.meta.AnalysisMethod) HostedMethod(com.oracle.svm.hosted.meta.HostedMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) AnalysisUniverse(com.oracle.graal.pointsto.meta.AnalysisUniverse) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 3 with CFunctionPointer

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

the class JNIFunctions method RegisterNatives.

/*
     * jint RegisterNatives(JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint
     * nMethods);
     */
@CEntryPoint
@CEntryPointOptions(prologue = JNIEnvironmentEnterPrologue.class, exceptionHandler = JNIExceptionHandlerReturnJniErr.class, publishAs = Publish.NotPublished, include = CEntryPointOptions.NotIncludedAutomatically.class)
static int RegisterNatives(JNIEnvironment env, JNIObjectHandle hclazz, JNINativeMethod methods, int nmethods) {
    Class<?> clazz = JNIObjectHandles.getObject(hclazz);
    Pointer p = (Pointer) methods;
    for (int i = 0; i < nmethods; i++) {
        JNINativeMethod entry = (JNINativeMethod) p;
        String name = CTypeConversion.toJavaString(entry.name());
        String signature = CTypeConversion.toJavaString(entry.signature());
        CFunctionPointer fnPtr = entry.fnPtr();
        String declaringClass = MetaUtil.toInternalName(clazz.getName());
        JNINativeLinkage linkage = JNIReflectionDictionary.singleton().getLinkage(declaringClass, name, signature);
        if (linkage != null) {
            linkage.setEntryPoint(fnPtr);
        } else {
            return JNIErrors.JNI_ERR();
        }
        p = p.add(SizeOf.get(JNINativeMethod.class));
    }
    return JNIErrors.JNI_OK();
}
Also used : JNINativeMethod(com.oracle.svm.jni.nativeapi.JNINativeMethod) InvokeCFunctionPointer(org.graalvm.nativeimage.c.function.InvokeCFunctionPointer) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) CFunctionPointer(org.graalvm.nativeimage.c.function.CFunctionPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CShortPointer(org.graalvm.nativeimage.c.type.CShortPointer) JNIJavaVMPointer(com.oracle.svm.jni.nativeapi.JNIJavaVMPointer) Pointer(org.graalvm.word.Pointer) InvokeCFunctionPointer(org.graalvm.nativeimage.c.function.InvokeCFunctionPointer) CFunctionPointer(org.graalvm.nativeimage.c.function.CFunctionPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) JNINativeLinkage(com.oracle.svm.jni.access.JNINativeLinkage) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CEntryPointOptions(com.oracle.svm.core.c.function.CEntryPointOptions)

Example 4 with CFunctionPointer

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

the class NativeImageHeap method addNonDataRelocation.

/**
 * Adds a relocation for a code pointer or other non-data pointers.
 */
private void addNonDataRelocation(RelocatableBuffer buffer, int index, RelocatedPointer pointer) {
    mustBeAligned(index);
    assert pointer instanceof CFunctionPointer : "unknown relocated pointer " + pointer;
    assert pointer instanceof MethodPointer : "cannot create relocation for unknown FunctionPointer " + pointer;
    HostedMethod method = ((MethodPointer) pointer).getMethod();
    if (method.isCodeAddressOffsetValid()) {
        // Only compiled methods inserted in vtables require relocation.
        buffer.addDirectRelocationWithoutAddend(index, objectSize(), pointer);
    }
}
Also used : MethodPointer(com.oracle.svm.hosted.meta.MethodPointer) HostedMethod(com.oracle.svm.hosted.meta.HostedMethod) CFunctionPointer(org.graalvm.nativeimage.c.function.CFunctionPointer)

Example 5 with CFunctionPointer

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

the class JNIFunctionTablesFeature method beforeCompilation.

@Override
public void beforeCompilation(BeforeCompilationAccess a) {
    BeforeCompilationAccessImpl access = (BeforeCompilationAccessImpl) a;
    HostedMetaAccess metaAccess = access.getMetaAccess();
    CFunctionPointer unimplementedWithJavaVMArgument = getStubFunctionPointer(access, (HostedMethod) getSingleMethod(metaAccess, UnimplementedWithJavaVMArgument.class));
    JNIStructFunctionsInitializer<JNIInvokeInterface> invokesInitializer = buildInvokesInitializer(access, unimplementedWithJavaVMArgument);
    CFunctionPointer unimplementedWithJNIEnvArgument = getStubFunctionPointer(access, (HostedMethod) getSingleMethod(metaAccess, UnimplementedWithJNIEnvArgument.class));
    JNIStructFunctionsInitializer<JNINativeInterface> functionsInitializer = buildFunctionsInitializer(access, unimplementedWithJNIEnvArgument);
    JNIFunctionTables.singleton().initialize(invokesInitializer, functionsInitializer);
}
Also used : JNIInvokeInterface(com.oracle.svm.jni.nativeapi.JNIInvokeInterface) CFunctionPointer(org.graalvm.nativeimage.c.function.CFunctionPointer) JNINativeInterface(com.oracle.svm.jni.nativeapi.JNINativeInterface) HostedMetaAccess(com.oracle.svm.hosted.meta.HostedMetaAccess) BeforeCompilationAccessImpl(com.oracle.svm.hosted.FeatureImpl.BeforeCompilationAccessImpl)

Aggregations

CFunctionPointer (org.graalvm.nativeimage.c.function.CFunctionPointer)8 HostedMethod (com.oracle.svm.hosted.meta.HostedMethod)5 MethodPointer (com.oracle.svm.hosted.meta.MethodPointer)3 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)3 StructFieldInfo (com.oracle.svm.hosted.c.info.StructFieldInfo)2 HostedType (com.oracle.svm.hosted.meta.HostedType)2 JavaKind (jdk.vm.ci.meta.JavaKind)2 AnalysisMethod (com.oracle.graal.pointsto.meta.AnalysisMethod)1 AnalysisUniverse (com.oracle.graal.pointsto.meta.AnalysisUniverse)1 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)1 ObjectLayout (com.oracle.svm.core.config.ObjectLayout)1 ReferenceMapEncoder (com.oracle.svm.core.heap.ReferenceMapEncoder)1 DynamicHub (com.oracle.svm.core.hub.DynamicHub)1 DynamicHubSupport (com.oracle.svm.core.hub.DynamicHubSupport)1 BeforeCompilationAccessImpl (com.oracle.svm.hosted.FeatureImpl.BeforeCompilationAccessImpl)1 HybridLayout (com.oracle.svm.hosted.config.HybridLayout)1 HostedMetaAccess (com.oracle.svm.hosted.meta.HostedMetaAccess)1 JNINativeLinkage (com.oracle.svm.jni.access.JNINativeLinkage)1 CallVariant (com.oracle.svm.jni.hosted.JNIJavaCallWrapperMethod.CallVariant)1 JNIInvokeInterface (com.oracle.svm.jni.nativeapi.JNIInvokeInterface)1