Search in sources :

Example 51 with ConstantNode

use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.

the class JNINativeCallWrapperMethod method buildGraph.

@Override
public StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose) {
    JNIGraphKit kit = new JNIGraphKit(debug, providers, method);
    StructuredGraph graph = kit.getGraph();
    InvokeWithExceptionNode handleFrame = kit.nativeCallPrologue();
    ValueNode callAddress = kit.nativeCallAddress(kit.createObject(linkage));
    ValueNode environment = kit.environment();
    JavaType javaReturnType = method.getSignature().getReturnType(null);
    JavaType[] javaArgumentTypes = method.toParameterTypes();
    List<ValueNode> javaArguments = kit.loadArguments(javaArgumentTypes);
    List<ValueNode> jniArguments = new ArrayList<>(2 + javaArguments.size());
    List<JavaType> jniArgumentTypes = new ArrayList<>(jniArguments.size());
    JavaType environmentType = providers.getMetaAccess().lookupJavaType(JNIEnvironment.class);
    JavaType objectHandleType = providers.getMetaAccess().lookupJavaType(JNIObjectHandle.class);
    jniArguments.add(environment);
    jniArgumentTypes.add(environmentType);
    if (method.isStatic()) {
        JavaConstant clazz = providers.getConstantReflection().asJavaClass(method.getDeclaringClass());
        ConstantNode clazzNode = ConstantNode.forConstant(clazz, providers.getMetaAccess(), graph);
        ValueNode box = kit.boxObjectInLocalHandle(clazzNode);
        jniArguments.add(box);
        jniArgumentTypes.add(objectHandleType);
    }
    for (int i = 0; i < javaArguments.size(); i++) {
        ValueNode arg = javaArguments.get(i);
        JavaType argType = javaArgumentTypes[i];
        if (javaArgumentTypes[i].getJavaKind().isObject()) {
            ValueNode obj = javaArguments.get(i);
            arg = kit.boxObjectInLocalHandle(obj);
            argType = objectHandleType;
        }
        jniArguments.add(arg);
        jniArgumentTypes.add(argType);
    }
    assert jniArguments.size() == jniArgumentTypes.size();
    JavaType jniReturnType = javaReturnType;
    if (jniReturnType.getJavaKind().isObject()) {
        jniReturnType = objectHandleType;
    }
    if (getOriginal().isSynchronized()) {
        ValueNode monitorObject;
        if (method.isStatic()) {
            Constant hubConstant = providers.getConstantReflection().asObjectHub(method.getDeclaringClass());
            DynamicHub hub = (DynamicHub) SubstrateObjectConstant.asObject(hubConstant);
            monitorObject = ConstantNode.forConstant(SubstrateObjectConstant.forObject(hub), providers.getMetaAccess(), graph);
        } else {
            monitorObject = javaArguments.get(0);
        }
        MonitorIdNode monitorId = graph.add(new MonitorIdNode(kit.getFrameState().lockDepth(false)));
        MonitorEnterNode monitorEnter = kit.append(new MonitorEnterNode(monitorObject, monitorId));
        kit.getFrameState().pushLock(monitorEnter.object(), monitorEnter.getMonitorId());
        monitorEnter.setStateAfter(kit.getFrameState().create(kit.bci(), monitorEnter));
    }
    kit.getFrameState().clearLocals();
    Signature jniSignature = new JNISignature(jniArgumentTypes, jniReturnType);
    ValueNode returnValue = kit.createCFunctionCall(callAddress, method, jniArguments, jniSignature, true, false);
    if (getOriginal().isSynchronized()) {
        MonitorIdNode monitorId = kit.getFrameState().peekMonitorId();
        ValueNode monitorObject = kit.getFrameState().popLock();
        MonitorExitNode monitorExit = kit.append(new MonitorExitNode(monitorObject, monitorId, null));
        monitorExit.setStateAfter(kit.getFrameState().create(kit.bci(), monitorExit));
    }
    if (javaReturnType.getJavaKind().isObject()) {
        // before destroying handles in epilogue
        returnValue = kit.unboxHandle(returnValue);
    }
    kit.nativeCallEpilogue(handleFrame);
    kit.rethrowPendingException();
    if (javaReturnType.getJavaKind().isObject()) {
        // Just before return to always run the epilogue and never suppress a pending exception
        returnValue = castObject(kit, returnValue, (ResolvedJavaType) javaReturnType);
    }
    kit.createReturn(returnValue, javaReturnType.getJavaKind());
    kit.mergeUnwinds();
    assert graph.verify();
    return graph;
}
Also used : MonitorIdNode(org.graalvm.compiler.nodes.java.MonitorIdNode) Constant(jdk.vm.ci.meta.Constant) SubstrateObjectConstant(com.oracle.svm.core.meta.SubstrateObjectConstant) JavaConstant(jdk.vm.ci.meta.JavaConstant) ArrayList(java.util.ArrayList) JavaConstant(jdk.vm.ci.meta.JavaConstant) MonitorExitNode(org.graalvm.compiler.nodes.java.MonitorExitNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JavaType(jdk.vm.ci.meta.JavaType) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) InvokeWithExceptionNode(org.graalvm.compiler.nodes.InvokeWithExceptionNode) MonitorEnterNode(org.graalvm.compiler.nodes.java.MonitorEnterNode) Signature(jdk.vm.ci.meta.Signature) ValueNode(org.graalvm.compiler.nodes.ValueNode) DynamicHub(com.oracle.svm.core.hub.DynamicHub)

Example 52 with ConstantNode

use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.

the class JNIPrimitiveArrayOperationMethod method newArray.

private ValueNode newArray(HostedProviders providers, JNIGraphKit kit, List<ValueNode> arguments) {
    ResolvedJavaType elementType = providers.getMetaAccess().lookupJavaType(elementKind.toJavaClass());
    ValueNode length = arguments.get(1);
    ConstantNode zero = kit.createInt(0);
    kit.startIf(new IntegerLessThanNode(length, zero), BranchProbabilityNode.VERY_SLOW_PATH_PROBABILITY);
    kit.thenPart();
    ValueNode nullHandle = kit.createConstant(JavaConstant.INT_0, providers.getWordTypes().getWordKind());
    kit.elsePart();
    ValueNode array = kit.append(new NewArrayNode(elementType, length, true));
    ValueNode arrayHandle = kit.boxObjectInLocalHandle(array);
    AbstractMergeNode merge = kit.endIf();
    Stamp handleStamp = providers.getWordTypes().getWordStamp(providers.getMetaAccess().lookupJavaType(JNIObjectHandle.class));
    return kit.unique(new ValuePhiNode(handleStamp, merge, new ValueNode[] { nullHandle, arrayHandle }));
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) NewArrayNode(org.graalvm.compiler.nodes.java.NewArrayNode) Stamp(org.graalvm.compiler.core.common.type.Stamp) ValuePhiNode(org.graalvm.compiler.nodes.ValuePhiNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) IntegerLessThanNode(org.graalvm.compiler.nodes.calc.IntegerLessThanNode) AbstractMergeNode(org.graalvm.compiler.nodes.AbstractMergeNode) ResolvedJavaType(jdk.vm.ci.meta.ResolvedJavaType) JNIObjectHandle(com.oracle.svm.jni.nativeapi.JNIObjectHandle)

Example 53 with ConstantNode

use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.

the class NegateNodeCanonicalizationTest method testChar.

@Test
public void testChar() {
    char[] a = new char[] { Character.MIN_VALUE, Character.MIN_VALUE + 1, 0, 1, Character.MAX_VALUE - 1, Character.MAX_VALUE };
    for (char i : a) {
        ConstantNode node = ConstantNode.forChar(i, graph);
        JavaConstant expected = JavaConstant.forInt(-i);
        assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp(NodeView.DEFAULT)).getNeg().foldConstant(node.asConstant()));
    }
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) JavaConstant(jdk.vm.ci.meta.JavaConstant) Test(org.junit.Test)

Example 54 with ConstantNode

use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.

the class NegateNodeCanonicalizationTest method testInt.

@Test
public void testInt() {
    int[] a = new int[] { Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1, 0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE };
    for (int i : a) {
        ConstantNode node = ConstantNode.forInt(i, graph);
        JavaConstant expected = JavaConstant.forInt(-i);
        assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp(NodeView.DEFAULT)).getNeg().foldConstant(node.asConstant()));
    }
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) JavaConstant(jdk.vm.ci.meta.JavaConstant) Test(org.junit.Test)

Example 55 with ConstantNode

use of org.graalvm.compiler.nodes.ConstantNode in project graal by oracle.

the class NegateNodeCanonicalizationTest method testFloat.

@Test
public void testFloat() {
    float[] a = new float[] { Float.MIN_VALUE, Float.MIN_VALUE + 1, -1, 0, 1, Float.MAX_VALUE - 1, Float.MAX_VALUE };
    for (float i : a) {
        ConstantNode node = ConstantNode.forFloat(i, graph);
        JavaConstant expected = JavaConstant.forFloat(-i);
        assertEquals(expected, ArithmeticOpTable.forStamp(node.stamp(NodeView.DEFAULT)).getNeg().foldConstant(node.asConstant()));
    }
}
Also used : ConstantNode(org.graalvm.compiler.nodes.ConstantNode) JavaConstant(jdk.vm.ci.meta.JavaConstant) Test(org.junit.Test)

Aggregations

ConstantNode (org.graalvm.compiler.nodes.ConstantNode)100 ValueNode (org.graalvm.compiler.nodes.ValueNode)46 JavaConstant (jdk.vm.ci.meta.JavaConstant)32 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)28 Stamp (org.graalvm.compiler.core.common.type.Stamp)23 Test (org.junit.Test)15 ResolvedJavaType (jdk.vm.ci.meta.ResolvedJavaType)14 Node (org.graalvm.compiler.graph.Node)14 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)13 ParameterNode (org.graalvm.compiler.nodes.ParameterNode)13 PhiNode (org.graalvm.compiler.nodes.PhiNode)12 LogicNode (org.graalvm.compiler.nodes.LogicNode)11 IntegerStamp (org.graalvm.compiler.core.common.type.IntegerStamp)10 ArrayList (java.util.ArrayList)9 JavaKind (jdk.vm.ci.meta.JavaKind)9 AbstractMergeNode (org.graalvm.compiler.nodes.AbstractMergeNode)9 Constant (jdk.vm.ci.meta.Constant)8 LoopBeginNode (org.graalvm.compiler.nodes.LoopBeginNode)8 FixedGuardNode (org.graalvm.compiler.nodes.FixedGuardNode)7 LogicConstantNode (org.graalvm.compiler.nodes.LogicConstantNode)7