Search in sources :

Example 6 with EnumInfo

use of com.oracle.svm.hosted.c.info.EnumInfo in project graal by oracle.

the class CFunctionCallStubMethod method adaptSignatureAndConvertArguments.

private static Signature adaptSignatureAndConvertArguments(ResolvedJavaMethod method, HostedProviders providers, NativeLibraries nativeLibraries, HostedGraphKit kit, Signature signature, List<ValueNode> arguments) {
    MetaAccessProvider metaAccess = providers.getMetaAccess();
    JavaType returnType = signature.getReturnType(null);
    JavaType[] parameterTypes = signature.toParameterTypes(null);
    for (int i = 0; i < parameterTypes.length; i++) {
        if (!isPrimitiveOrWord(providers, parameterTypes[i])) {
            ElementInfo typeInfo = nativeLibraries.findElementInfo(parameterTypes[i]);
            if (typeInfo instanceof EnumInfo) {
                UserError.guarantee(typeInfo.getChildren().stream().anyMatch(EnumValueInfo.class::isInstance), "Enum class " + returnType.toJavaName() + " needs a method that is annotated with @" + CEnumValue.class.getSimpleName() + " because it is used as a parameter of a method annotated with @" + CFunction.class.getSimpleName() + ": " + method.format("%H.%n(%p)"));
                ValueNode argumentValue = arguments.get(i);
                IsNullNode isNull = kit.unique(new IsNullNode(argumentValue));
                kit.startIf(isNull, BranchProbabilityNode.VERY_SLOW_PATH_PROBABILITY);
                kit.thenPart();
                ResolvedJavaType enumExceptionType = metaAccess.lookupJavaType(RuntimeException.class);
                NewInstanceNode enumException = kit.append(new NewInstanceNode(enumExceptionType, true));
                Iterator<ResolvedJavaMethod> enumExceptionCtor = Arrays.stream(enumExceptionType.getDeclaredConstructors()).filter(c -> c.getSignature().getParameterCount(false) == 1 && c.getSignature().getParameterType(0, null).equals(metaAccess.lookupJavaType(String.class))).iterator();
                ConstantNode enumExceptionMessage = kit.createConstant(kit.getConstantReflection().forString("null return value cannot be converted to a C enum value"), JavaKind.Object);
                kit.createJavaCallWithExceptionAndUnwind(InvokeKind.Special, enumExceptionCtor.next(), enumException, enumExceptionMessage);
                assert !enumExceptionCtor.hasNext();
                kit.append(new UnwindNode(enumException));
                kit.endIf();
                CInterfaceEnumTool tool = new CInterfaceEnumTool(metaAccess, providers.getSnippetReflection());
                argumentValue = tool.createEnumValueInvoke(kit, (EnumInfo) typeInfo, cEnumKind, argumentValue);
                arguments.set(i, argumentValue);
                parameterTypes[i] = metaAccess.lookupJavaType(cEnumKind.toJavaClass());
            } else {
                throw UserError.abort("@" + CFunction.class.getSimpleName() + " parameter types are restricted to primitive types, word types and enumerations (@" + CEnum.class.getSimpleName() + "): " + method.format("%H.%n(%p)"));
            }
        }
    }
    if (!isPrimitiveOrWord(providers, returnType)) {
        // Assume enum: actual checks and conversion are in adaptReturnValue()
        returnType = providers.getWordTypes().getWordImplType();
    }
    JavaType actualReturnType = returnType;
    return new Signature() {

        @Override
        public int getParameterCount(boolean receiver) {
            return parameterTypes.length;
        }

        @Override
        public JavaType getParameterType(int index, ResolvedJavaType accessingClass) {
            return parameterTypes[index];
        }

        @Override
        public JavaType getReturnType(ResolvedJavaType accessingClass) {
            return actualReturnType;
        }
    };
}
Also used : HostedProviders(com.oracle.graal.pointsto.meta.HostedProviders) Signature(jdk.vm.ci.meta.Signature) Arrays(java.util.Arrays) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) BranchProbabilityNode(org.graalvm.compiler.nodes.extended.BranchProbabilityNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) CGlobalDataLoadAddressNode(com.oracle.svm.core.graal.nodes.CGlobalDataLoadAddressNode) InvokeKind(org.graalvm.compiler.nodes.CallTargetNode.InvokeKind) CInterfaceEnumTool(com.oracle.svm.hosted.phases.CInterfaceEnumTool) NativeLibraries(com.oracle.svm.hosted.c.NativeLibraries) EnumValueInfo(com.oracle.svm.hosted.c.info.EnumValueInfo) JavaKind(jdk.vm.ci.meta.JavaKind) Transition(org.graalvm.nativeimage.c.function.CFunction.Transition) DebugContext(org.graalvm.compiler.debug.DebugContext) FrameStateBuilder(org.graalvm.compiler.java.FrameStateBuilder) UserError(com.oracle.svm.core.util.UserError) EnumInfo(com.oracle.svm.hosted.c.info.EnumInfo) ImageSingletons(org.graalvm.nativeimage.ImageSingletons) Iterator(java.util.Iterator) ElementInfo(com.oracle.svm.hosted.c.info.ElementInfo) CustomSubstitutionMethod(com.oracle.svm.hosted.annotation.CustomSubstitutionMethod) CFunction(org.graalvm.nativeimage.c.function.CFunction) SharedMethod(com.oracle.svm.core.meta.SharedMethod) CGlobalDataInfo(com.oracle.svm.core.graal.code.CGlobalDataInfo) CEnumLookup(org.graalvm.nativeimage.c.constant.CEnumLookup) CEnumValue(org.graalvm.nativeimage.c.constant.CEnumValue) ValueNode(org.graalvm.compiler.nodes.ValueNode) JavaType(jdk.vm.ci.meta.JavaType) List(java.util.List) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) UnwindNode(org.graalvm.compiler.nodes.UnwindNode) IsNullNode(org.graalvm.compiler.nodes.calc.IsNullNode) NewInstanceNode(org.graalvm.compiler.nodes.java.NewInstanceNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) CEnum(org.graalvm.nativeimage.c.constant.CEnum) HostedGraphKit(com.oracle.svm.hosted.phases.HostedGraphKit) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) NewInstanceNode(org.graalvm.compiler.nodes.java.NewInstanceNode) ElementInfo(com.oracle.svm.hosted.c.info.ElementInfo) EnumInfo(com.oracle.svm.hosted.c.info.EnumInfo) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) IsNullNode(org.graalvm.compiler.nodes.calc.IsNullNode) Signature(jdk.vm.ci.meta.Signature) ValueNode(org.graalvm.compiler.nodes.ValueNode) CInterfaceEnumTool(com.oracle.svm.hosted.phases.CInterfaceEnumTool) UnwindNode(org.graalvm.compiler.nodes.UnwindNode) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

EnumInfo (com.oracle.svm.hosted.c.info.EnumInfo)6 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)6 ElementInfo (com.oracle.svm.hosted.c.info.ElementInfo)5 JavaType (jdk.vm.ci.meta.JavaType)5 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)5 ValueNode (org.graalvm.compiler.nodes.ValueNode)5 NativeLibraries (com.oracle.svm.hosted.c.NativeLibraries)4 CInterfaceEnumTool (com.oracle.svm.hosted.phases.CInterfaceEnumTool)4 HostedGraphKit (com.oracle.svm.hosted.phases.HostedGraphKit)4 InvokeKind (org.graalvm.compiler.nodes.CallTargetNode.InvokeKind)4 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)4 UniverseMetaAccess (com.oracle.graal.pointsto.infrastructure.UniverseMetaAccess)3 WrappedJavaMethod (com.oracle.graal.pointsto.infrastructure.WrappedJavaMethod)3 HostedProviders (com.oracle.graal.pointsto.meta.HostedProviders)3 UserError (com.oracle.svm.core.util.UserError)3 EnumValueInfo (com.oracle.svm.hosted.c.info.EnumValueInfo)3 CEnumLookup (org.graalvm.nativeimage.c.constant.CEnumLookup)3 GraphProvider (com.oracle.graal.pointsto.infrastructure.GraphProvider)2 AnalysisMetaAccess (com.oracle.graal.pointsto.meta.AnalysisMetaAccess)2 AnalysisMethod (com.oracle.graal.pointsto.meta.AnalysisMethod)2