Search in sources :

Example 66 with ResolvedJavaType

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

the class InjectedAccessorsPlugin method handleField.

private static boolean handleField(GraphBuilderContext b, ResolvedJavaField field, boolean isStatic, ValueNode receiver, boolean isGet, ValueNode value) {
    InjectAccessors injectAccesors = field.getAnnotation(InjectAccessors.class);
    if (injectAccesors == null) {
        return false;
    }
    Class<?> accessorsClass = injectAccesors.value();
    ResolvedJavaType accessorsType = b.getMetaAccess().lookupJavaType(accessorsClass);
    String shortName = isGet ? "get" : "set";
    String fieldName = field.getName();
    String longName = shortName + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
    ResolvedJavaMethod foundMethod = null;
    for (ResolvedJavaMethod method : accessorsType.getDeclaredMethods()) {
        if (method.getName().equals(shortName) || method.getName().equals(longName)) {
            if (foundMethod != null) {
                error(field, accessorsType, null, "found two methods " + foundMethod.format("%n(%p)") + " and " + method.format("%n(%p)"));
            }
            foundMethod = method;
        }
    }
    if (foundMethod == null) {
        error(field, accessorsType, null, "found no method named " + shortName + " or " + longName);
    }
    if (!foundMethod.isStatic()) {
        error(field, accessorsType, foundMethod, "method is not static");
    }
    int paramIdx = 0;
    if (!isStatic) {
        if (foundMethod.getSignature().getParameterCount(false) < paramIdx + 1) {
            error(field, accessorsType, foundMethod, "not enough parameters");
        }
        JavaType actualReceiver = foundMethod.getSignature().getParameterType(paramIdx, null);
        ResolvedJavaType expectedReceiver = field.getDeclaringClass();
        if (!actualReceiver.equals(expectedReceiver)) {
            error(field, accessorsType, foundMethod, "wrong receiver type: expected " + expectedReceiver.toJavaName(true) + ", found " + actualReceiver.toJavaName(true));
        }
        paramIdx++;
    }
    JavaType expectedValue = field.getType();
    if (isGet) {
        JavaType actualValue = foundMethod.getSignature().getReturnType(null);
        if (!actualValue.equals(expectedValue)) {
            error(field, accessorsType, foundMethod, "wrong return type: expected " + expectedValue.toJavaName(true) + ", found " + actualValue.toJavaName(true));
        }
    } else {
        if (foundMethod.getSignature().getParameterCount(false) < paramIdx + 1) {
            error(field, accessorsType, foundMethod, "not enough parameters");
        }
        JavaType actualValue = foundMethod.getSignature().getParameterType(paramIdx, null);
        if (!actualValue.equals(expectedValue)) {
            error(field, accessorsType, foundMethod, "wrong value type: expected " + expectedValue.toJavaName(true) + ", found " + actualValue.toJavaName(true));
        }
        paramIdx++;
    }
    if (foundMethod.getSignature().getParameterCount(false) != paramIdx) {
        error(field, accessorsType, foundMethod, "Wrong number of parameters: expected " + paramIdx + ", found " + foundMethod.getSignature().getParameterCount(false));
    }
    List<ValueNode> args = new ArrayList<>();
    if (!isStatic) {
        args.add(receiver);
    }
    if (!isGet) {
        args.add(value);
    }
    b.handleReplacedInvoke(InvokeKind.Static, foundMethod, args.toArray(new ValueNode[args.size()]), false);
    return true;
}
Also used : JavaType(jdk.vm.ci.meta.JavaType) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) InjectAccessors(com.oracle.svm.core.annotate.InjectAccessors) ValueNode(org.graalvm.compiler.nodes.ValueNode) ArrayList(java.util.ArrayList) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 67 with ResolvedJavaType

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

the class StrengthenStampsPhase method strengthenStamp.

private Stamp strengthenStamp(ValueNode node, JavaTypeProfile typeProfile) {
    ObjectStamp oldStamp = (ObjectStamp) node.stamp(NodeView.DEFAULT);
    HostedType oldType = toHosted(oldStamp.type());
    if (oldStamp.alwaysNull()) {
        /* We cannot make that more precise. */
        return oldStamp;
    }
    boolean nonNull = oldStamp.nonNull() || typeProfile.getNullSeen() == TriState.FALSE;
    ProfiledType[] exactTypes = typeProfile.getTypes();
    if (exactTypes.length == 1) {
        ResolvedJavaType exactType = exactTypes[0].getType();
        assert oldType == null || oldType.isAssignableFrom(exactType);
        if (!oldStamp.isExactType() || !exactType.equals(oldType) || nonNull != oldStamp.nonNull()) {
            TypeReference typeRef = TypeReference.createExactTrusted(toTarget(exactType));
            return nonNull ? StampFactory.objectNonNull(typeRef) : StampFactory.object(typeRef);
        } else {
            return oldStamp;
        }
    }
    if (exactTypes.length == 0) {
        if (!nonNull) {
            return StampFactory.alwaysNull();
        } else {
            /*
                 * The code after the node is unreachable. We just insert a always-failing guard
                 * after the node and let dead code elimination remove everything after the node.
                 */
            StructuredGraph graph = node.graph();
            FixedWithNextNode insertionPoint;
            if (node instanceof ParameterNode) {
                /* The whole method is unreachable. */
                insertionPoint = graph.start();
            } else if (node instanceof InvokeWithExceptionNode) {
                /* The invoked method never returns normally (but can throw an exception). */
                insertionPoint = ((InvokeWithExceptionNode) node).next();
            } else {
                insertionPoint = (FixedWithNextNode) node;
            }
            graph.addAfterFixed(insertionPoint, graph.add(new FixedGuardNode(LogicConstantNode.forBoolean(true, graph), DeoptimizationReason.UnreachedCode, DeoptimizationAction.None, true)));
            return oldStamp;
        }
    }
    ResolvedJavaType baseType;
    if (oldStamp.isExactType()) {
        /* Base type cannot be more precise. */
        baseType = oldType;
    } else {
        assert exactTypes.length > 1;
        assert oldType == null || oldType.isAssignableFrom(exactTypes[0].getType());
        baseType = exactTypes[0].getType();
        for (int i = 1; i < exactTypes.length; i++) {
            assert oldType == null || oldType.isAssignableFrom(exactTypes[i].getType());
            baseType = baseType.findLeastCommonAncestor(exactTypes[i].getType());
        }
        if (oldType != null && !oldType.isAssignableFrom(baseType)) {
            /*
                 * When the original stamp is an interface type, we do not want to weaken that type
                 * with the common base class of all implementation types (which could even be
                 * java.lang.Object).
                 */
            baseType = oldType;
        }
    }
    if (!baseType.equals(oldType) || nonNull != oldStamp.nonNull()) {
        TypeReference typeRef = TypeReference.createTrustedWithoutAssumptions(toTarget(baseType));
        return nonNull ? StampFactory.objectNonNull(typeRef) : StampFactory.object(typeRef);
    }
    return oldStamp;
}
Also used : FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) ProfiledType(jdk.vm.ci.meta.JavaTypeProfile.ProfiledType) ObjectStamp(org.graalvm.compiler.core.common.type.ObjectStamp) AbstractObjectStamp(org.graalvm.compiler.core.common.type.AbstractObjectStamp) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) HostedType(com.oracle.svm.hosted.meta.HostedType) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ParameterNode(org.graalvm.compiler.nodes.ParameterNode) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) TypeReference(org.graalvm.compiler.core.common.type.TypeReference)

Example 68 with ResolvedJavaType

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

the class AssertTypeStateNode method alwaysHolds.

@Override
protected boolean alwaysHolds(boolean reportError) {
    if (getInput().isConstant()) {
        if (getInput().asJavaConstant().isNull()) {
            if (typeState.getNullSeen() == TriState.FALSE) {
                if (reportError) {
                    throw shouldNotReachHere("Null constant not compatible with type state: " + this + " : " + getTypeState());
                }
            } else {
                return true;
            }
        } else {
            Set<ResolvedJavaType> ourTypes = new HashSet<>();
            addAllTypes(ourTypes, typeState);
            if (!ourTypes.contains(StampTool.typeOrNull(getInput()))) {
                if (reportError) {
                    throw shouldNotReachHere("Constant object not compatible with type state: " + this + " : " + getTypeState() + ", " + StampTool.typeOrNull(getInput()));
                }
            } else {
                return true;
            }
        }
    }
    if (getInput() instanceof AssertTypeStateNode) {
        JavaTypeProfile inputTypeState = ((AssertTypeStateNode) getInput()).getTypeState();
        Set<ResolvedJavaType> inputTypes = new HashSet<>();
        addAllTypes(inputTypes, inputTypeState);
        Set<ResolvedJavaType> ourTypes = new HashSet<>();
        addAllTypes(ourTypes, typeState);
        if (ourTypes.containsAll(inputTypes)) {
            /* Another node is already checking the same or a stronger type state. */
            return true;
        }
    }
    return false;
}
Also used : JavaTypeProfile(jdk.vm.ci.meta.JavaTypeProfile) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) HashSet(java.util.HashSet)

Example 69 with ResolvedJavaType

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

the class PEReadEliminationClosure method processUnsafeStore.

private boolean processUnsafeStore(RawStoreNode store, PEReadEliminationBlockState state, GraphEffectList effects) {
    ResolvedJavaType type = StampTool.typeOrNull(store.object());
    if (type != null && type.isArray()) {
        JavaKind accessKind = store.accessKind();
        JavaKind componentKind = type.getComponentType().getJavaKind();
        LocationIdentity location = NamedLocationIdentity.getArrayLocation(componentKind);
        if (store.offset().isConstant()) {
            long offset = store.offset().asJavaConstant().asLong();
            boolean overflowAccess = isOverflowAccess(accessKind, componentKind);
            int index = overflowAccess ? -1 : VirtualArrayNode.entryIndexForOffset(tool.getArrayOffsetProvider(), offset, accessKind, type.getComponentType(), Integer.MAX_VALUE);
            return processStore(store, store.object(), location, index, accessKind, overflowAccess, store.value(), state, effects);
        } else {
            processIdentity(state, location);
        }
    } else {
        state.killReadCache();
    }
    return false;
}
Also used : LocationIdentity(org.graalvm.word.LocationIdentity) FieldLocationIdentity(org.graalvm.compiler.nodes.FieldLocationIdentity) NamedLocationIdentity(org.graalvm.compiler.nodes.NamedLocationIdentity) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) MemoryCheckpoint(org.graalvm.compiler.nodes.memory.MemoryCheckpoint) JavaKind(jdk.vm.ci.meta.JavaKind)

Example 70 with ResolvedJavaType

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

the class SubstrateGraphBuilderPlugins method registerArraysPlugins.

private static void registerArraysPlugins(InvocationPlugins plugins, boolean analysis) {
    Registration r = new Registration(plugins, Arrays.class).setAllowOverwrite(true);
    r.register2("copyOf", Object[].class, int.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode original, ValueNode newLength) {
            if (analysis) {
                b.addPush(JavaKind.Object, new AnalysisArraysCopyOfNode(b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp(), original, newLength));
            } else {
                /* Get the class from the original node. */
                GetClassNode originalArrayType = b.add(new GetClassNode(original.stamp(NodeView.DEFAULT), b.nullCheckedValue(original)));
                ValueNode originalLength = b.add(ArrayLengthNode.create(original, b.getConstantReflection()));
                Stamp stamp = b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp().join(original.stamp(NodeView.DEFAULT));
                b.addPush(JavaKind.Object, new SubstrateArraysCopyOfNode(stamp, original, originalLength, newLength, originalArrayType));
            }
            return true;
        }
    });
    r.register3("copyOf", Object[].class, int.class, Class.class, new InvocationPlugin() {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode original, ValueNode newLength, ValueNode newArrayType) {
            if (analysis) {
                /*
                     * If the new array type comes from a GetClassNode or is a constant we can infer
                     * the concrete type of the new array, otherwise we conservatively assume that
                     * the new array can have any of the instantiated array types.
                     */
                b.addPush(JavaKind.Object, new AnalysisArraysCopyOfNode(b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp(), original, newLength, newArrayType));
            } else {
                Stamp stamp;
                if (newArrayType.isConstant()) {
                    ResolvedJavaType newType = b.getConstantReflection().asJavaType(newArrayType.asConstant());
                    stamp = StampFactory.objectNonNull(TypeReference.createExactTrusted(newType));
                } else {
                    stamp = b.getInvokeReturnStamp(b.getAssumptions()).getTrustedStamp();
                }
                ValueNode originalLength = b.add(ArrayLengthNode.create(original, b.getConstantReflection()));
                b.addPush(JavaKind.Object, new SubstrateArraysCopyOfNode(stamp, original, originalLength, newLength, newArrayType));
            }
            return true;
        }
    });
}
Also used : Stamp(org.graalvm.compiler.core.common.type.Stamp) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) GetClassNode(org.graalvm.compiler.nodes.extended.GetClassNode) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) SubstrateArraysCopyOfNode(com.oracle.svm.core.graal.jdk.SubstrateArraysCopyOfNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) StackValueNode(com.oracle.svm.core.graal.stackvalue.StackValueNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ConvertUnknownValueNode(com.oracle.graal.pointsto.nodes.ConvertUnknownValueNode) AnalysisArraysCopyOfNode(com.oracle.graal.pointsto.nodes.AnalysisArraysCopyOfNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) Arrays(java.util.Arrays) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)161 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)63 ValueNode (org.graalvm.compiler.nodes.ValueNode)60 JavaKind (jdk.vm.ci.meta.JavaKind)25 JavaType (jdk.vm.ci.meta.JavaType)25 MethodCallTargetNode (org.graalvm.compiler.nodes.java.MethodCallTargetNode)19 Stamp (org.graalvm.compiler.core.common.type.Stamp)17 ConstantNode (org.graalvm.compiler.nodes.ConstantNode)17 MetaAccessProvider (jdk.vm.ci.meta.MetaAccessProvider)16 ObjectStamp (org.graalvm.compiler.core.common.type.ObjectStamp)13 TypeReference (org.graalvm.compiler.core.common.type.TypeReference)13 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)13 Signature (jdk.vm.ci.meta.Signature)11 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)11 LogicNode (org.graalvm.compiler.nodes.LogicNode)11 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)11 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)11 ArrayList (java.util.ArrayList)10 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)10 ResolvedJavaField (jdk.vm.ci.meta.ResolvedJavaField)9