Search in sources :

Example 31 with JavaType

use of jdk.vm.ci.meta.JavaType in project graal by oracle.

the class CFunctionCallStubMethod method adaptReturnValue.

private static ValueNode adaptReturnValue(ResolvedJavaMethod method, HostedProviders providers, NativeLibraries nativeLibraries, HostedGraphKit kit, ValueNode invokeValue) {
    ValueNode returnValue = invokeValue;
    JavaType declaredReturnType = method.getSignature().getReturnType(null);
    if (isPrimitiveOrWord(providers, declaredReturnType)) {
        return returnValue;
    }
    ElementInfo typeInfo = nativeLibraries.findElementInfo(declaredReturnType);
    if (typeInfo instanceof EnumInfo) {
        UserError.guarantee(typeInfo.getChildren().stream().anyMatch(EnumValueInfo.class::isInstance), "Enum class " + declaredReturnType.toJavaName() + " needs a method that is annotated with @" + CEnumLookup.class + " because it is used as the return type of a method annotated with @" + CFunction.class.getSimpleName() + ": " + method.format("%H.%n(%p)"));
        // We take a word return type because checks expect word type replacements, but it is
        // narrowed to cEnumKind here.
        CInterfaceEnumTool tool = new CInterfaceEnumTool(providers.getMetaAccess(), providers.getSnippetReflection());
        returnValue = tool.createEnumLookupInvoke(kit, (ResolvedJavaType) declaredReturnType, (EnumInfo) typeInfo, cEnumKind, returnValue);
    } else {
        throw UserError.abort("Return types of methods annotated with @" + CFunction.class.getSimpleName() + " are restricted to primitive types, word types and enumerations (@" + CEnum.class.getSimpleName() + "): " + method.format("%H.%n(%p)"));
    }
    return returnValue;
}
Also used : CEnumLookup(org.graalvm.nativeimage.c.constant.CEnumLookup) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) ElementInfo(com.oracle.svm.hosted.c.info.ElementInfo) EnumInfo(com.oracle.svm.hosted.c.info.EnumInfo) ValueNode(org.graalvm.compiler.nodes.ValueNode) CInterfaceEnumTool(com.oracle.svm.hosted.phases.CInterfaceEnumTool) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Example 32 with JavaType

use of jdk.vm.ci.meta.JavaType 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)

Example 33 with JavaType

use of jdk.vm.ci.meta.JavaType in project graal by oracle.

the class BytecodeParser method genNewObjectArray.

private void genNewObjectArray(int cpi) {
    JavaType type = lookupType(cpi, ANEWARRAY);
    if (!(type instanceof ResolvedJavaType)) {
        ValueNode length = frameState.pop(JavaKind.Int);
        handleUnresolvedNewObjectArray(type, length);
        return;
    }
    ResolvedJavaType resolvedType = (ResolvedJavaType) type;
    ClassInitializationPlugin classInitializationPlugin = this.graphBuilderConfig.getPlugins().getClassInitializationPlugin();
    if (classInitializationPlugin != null && classInitializationPlugin.shouldApply(this, resolvedType.getArrayClass())) {
        FrameState stateBefore = frameState.create(bci(), getNonIntrinsicAncestor(), false, null, null);
        classInitializationPlugin.apply(this, resolvedType.getArrayClass(), stateBefore);
    }
    ValueNode length = frameState.pop(JavaKind.Int);
    for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
        if (plugin.handleNewArray(this, resolvedType, length)) {
            return;
        }
    }
    frameState.push(JavaKind.Object, append(createNewArray(resolvedType, length, true)));
}
Also used : ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) NodePlugin(org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin) ValueNode(org.graalvm.compiler.nodes.ValueNode) FrameState(org.graalvm.compiler.nodes.FrameState) ClassInitializationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.ClassInitializationPlugin) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Example 34 with JavaType

use of jdk.vm.ci.meta.JavaType in project graal by oracle.

the class BytecodeParser method genInstanceOf.

private void genInstanceOf() {
    int cpi = getStream().readCPI();
    JavaType type = lookupType(cpi, INSTANCEOF);
    ValueNode object = frameState.pop(JavaKind.Object);
    if (!(type instanceof ResolvedJavaType)) {
        handleUnresolvedInstanceOf(type, object);
        return;
    }
    TypeReference resolvedType = TypeReference.createTrusted(graph.getAssumptions(), (ResolvedJavaType) type);
    JavaTypeProfile profile = getProfileForTypeCheck(resolvedType);
    for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
        if (plugin.handleInstanceOf(this, object, resolvedType.getType(), profile)) {
            return;
        }
    }
    LogicNode instanceOfNode = null;
    if (profile != null) {
        if (profile.getNullSeen().isFalse()) {
            object = nullCheckedValue(object);
            ResolvedJavaType singleType = profile.asSingleType();
            if (singleType != null) {
                LogicNode typeCheck = append(createInstanceOf(TypeReference.createExactTrusted(singleType), object, profile));
                if (!typeCheck.isTautology()) {
                    append(new FixedGuardNode(typeCheck, DeoptimizationReason.TypeCheckedInliningViolated, DeoptimizationAction.InvalidateReprofile));
                }
                instanceOfNode = LogicConstantNode.forBoolean(resolvedType.getType().isAssignableFrom(singleType));
            }
        }
    }
    if (instanceOfNode == null) {
        instanceOfNode = createInstanceOf(resolvedType, object, null);
    }
    LogicNode logicNode = genUnique(instanceOfNode);
    int next = getStream().nextBCI();
    int value = getStream().readUByte(next);
    if (next <= currentBlock.endBci && (value == Bytecodes.IFEQ || value == Bytecodes.IFNE)) {
        getStream().next();
        BciBlock firstSucc = currentBlock.getSuccessor(0);
        BciBlock secondSucc = currentBlock.getSuccessor(1);
        if (firstSucc != secondSucc) {
            boolean negate = value != Bytecodes.IFNE;
            if (negate) {
                BciBlock tmp = firstSucc;
                firstSucc = secondSucc;
                secondSucc = tmp;
            }
            genIf(instanceOfNode, firstSucc, secondSucc, getProfileProbability(negate));
        } else {
            appendGoto(firstSucc);
        }
    } else {
        // Most frequent for value is IRETURN, followed by ISTORE.
        frameState.push(JavaKind.Int, append(genConditional(logicNode)));
    }
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) NodePlugin(org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin) JavaTypeProfile(jdk.vm.ci.meta.JavaTypeProfile) ValueNode(org.graalvm.compiler.nodes.ValueNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) TypeReference(org.graalvm.compiler.core.common.type.TypeReference) BciBlock(org.graalvm.compiler.java.BciBlockMapping.BciBlock) RuntimeConstraint(jdk.vm.ci.meta.DeoptimizationReason.RuntimeConstraint) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Example 35 with JavaType

use of jdk.vm.ci.meta.JavaType in project graal by oracle.

the class BytecodeParser method genCheckCast.

private void genCheckCast() {
    int cpi = getStream().readCPI();
    JavaType type = lookupType(cpi, CHECKCAST);
    ValueNode object = frameState.pop(JavaKind.Object);
    if (!(type instanceof ResolvedJavaType)) {
        handleUnresolvedCheckCast(type, object);
        return;
    }
    TypeReference checkedType = TypeReference.createTrusted(graph.getAssumptions(), (ResolvedJavaType) type);
    JavaTypeProfile profile = getProfileForTypeCheck(checkedType);
    for (NodePlugin plugin : graphBuilderConfig.getPlugins().getNodePlugins()) {
        if (plugin.handleCheckCast(this, object, checkedType.getType(), profile)) {
            return;
        }
    }
    ValueNode castNode = null;
    if (profile != null) {
        if (profile.getNullSeen().isFalse()) {
            object = nullCheckedValue(object);
            ResolvedJavaType singleType = profile.asSingleType();
            if (singleType != null && checkedType.getType().isAssignableFrom(singleType)) {
                LogicNode typeCheck = append(createInstanceOf(TypeReference.createExactTrusted(singleType), object, profile));
                if (typeCheck.isTautology()) {
                    castNode = object;
                } else {
                    FixedGuardNode fixedGuard = append(new FixedGuardNode(typeCheck, DeoptimizationReason.TypeCheckedInliningViolated, DeoptimizationAction.InvalidateReprofile, false));
                    castNode = append(PiNode.create(object, StampFactory.objectNonNull(TypeReference.createExactTrusted(singleType)), fixedGuard));
                }
            }
        }
    }
    boolean nonNull = ((ObjectStamp) object.stamp(NodeView.DEFAULT)).nonNull();
    if (castNode == null) {
        LogicNode condition = genUnique(createInstanceOfAllowNull(checkedType, object, null));
        if (condition.isTautology()) {
            castNode = object;
        } else {
            FixedGuardNode fixedGuard = append(new FixedGuardNode(condition, DeoptimizationReason.ClassCastException, DeoptimizationAction.InvalidateReprofile, false));
            castNode = append(PiNode.create(object, StampFactory.object(checkedType, nonNull), fixedGuard));
        }
    }
    frameState.push(JavaKind.Object, castNode);
}
Also used : FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) NodePlugin(org.graalvm.compiler.nodes.graphbuilderconf.NodePlugin) JavaTypeProfile(jdk.vm.ci.meta.JavaTypeProfile) ValueNode(org.graalvm.compiler.nodes.ValueNode) LogicNode(org.graalvm.compiler.nodes.LogicNode) TypeReference(org.graalvm.compiler.core.common.type.TypeReference) RuntimeConstraint(jdk.vm.ci.meta.DeoptimizationReason.RuntimeConstraint) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType)

Aggregations

JavaType (jdk.vm.ci.meta.JavaType)45 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)35 ValueNode (org.graalvm.compiler.nodes.ValueNode)22 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)15 JavaKind (jdk.vm.ci.meta.JavaKind)14 Signature (jdk.vm.ci.meta.Signature)11 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)8 InvokeNode (org.graalvm.compiler.nodes.InvokeNode)8 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)8 InvokeKind (org.graalvm.compiler.nodes.CallTargetNode.InvokeKind)7 EnumInfo (com.oracle.svm.hosted.c.info.EnumInfo)6 ArrayList (java.util.ArrayList)6 InvokeWithExceptionNode (org.graalvm.compiler.nodes.InvokeWithExceptionNode)6 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)6 NativeLibraries (com.oracle.svm.hosted.c.NativeLibraries)5 StampPair (org.graalvm.compiler.core.common.type.StampPair)5 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)4 ElementInfo (com.oracle.svm.hosted.c.info.ElementInfo)4 HostedGraphKit (com.oracle.svm.hosted.phases.HostedGraphKit)4 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)4