package com.oracle.svm.jni.hosted;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
import org.graalvm.compiler.nodes.FixedWithNextNode;
import org.graalvm.compiler.nodes.InvokeWithExceptionNode;
import org.graalvm.compiler.nodes.ValueNode;
import org.graalvm.compiler.nodes.java.ExceptionObjectNode;
import com.oracle.graal.pointsto.meta.HostedProviders;
import com.oracle.svm.hosted.phases.HostedGraphKit;
import com.oracle.svm.jni.JNIGeneratedMethodSupport;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.ResolvedJavaMethod;
class JNIGraphKit extends HostedGraphKit {
JNIGraphKit(DebugContext debug, HostedProviders providers, ResolvedJavaMethod method) {
super(debug, providers, method);
}
private InvokeWithExceptionNode createStaticInvoke(String name, ValueNode... args) {
return createInvokeWithExceptionAndUnwind(findMethod(JNIGeneratedMethodSupport.class, name, true), InvokeKind.Static, getFrameState(), bci(), args);
}
private FixedWithNextNode createStaticInvokeRetainException(String name, ValueNode... args) {
ResolvedJavaMethod method = findMethod(JNIGeneratedMethodSupport.class, name, true);
int invokeBci = bci();
startInvokeWithException(method, InvokeKind.Static, getFrameState(), invokeBci, args);
exceptionPart();
ExceptionObjectNode exception = exceptionObject();
setPendingException(exception);
endInvokeWithException();
return lastFixedNode;
}
public InvokeWithExceptionNode nativeCallAddress(ValueNode linkage) {
ResolvedJavaMethod method = findMethod(JNIGeneratedMethodSupport.class, "nativeCallAddress", true);
int invokeBci = bci();
return createInvokeWithExceptionAndUnwind(method, InvokeKind.Static, getFrameState(), invokeBci, linkage);
}
public InvokeWithExceptionNode nativeCallPrologue() {
return createStaticInvoke("nativeCallPrologue");
}
public InvokeWithExceptionNode nativeCallEpilogue(ValueNode handleFrame) {
return createStaticInvoke("nativeCallEpilogue", handleFrame);
}
public InvokeWithExceptionNode environment() {
return createStaticInvoke("environment");
}
public InvokeWithExceptionNode boxObjectInLocalHandle(ValueNode obj) {
return createStaticInvoke("boxObjectInLocalHandle", obj);
}
public InvokeWithExceptionNode unboxHandle(ValueNode handle) {
return createStaticInvoke("unboxHandle", handle);
}
public InvokeWithExceptionNode getFieldOffsetFromId(ValueNode fieldId) {
return createStaticInvoke("getFieldOffsetFromId", fieldId);
}
public InvokeWithExceptionNode getStaticPrimitiveFieldsArray() {
return createStaticInvoke("getStaticPrimitiveFieldsArray");
}
public InvokeWithExceptionNode getStaticObjectFieldsArray() {
return createStaticInvoke("getStaticObjectFieldsArray");
}
public InvokeWithExceptionNode setPendingException(ValueNode obj) {
return createStaticInvoke("setPendingException", obj);
}
public InvokeWithExceptionNode getAndClearPendingException() {
return createStaticInvoke("getAndClearPendingException");
}
public InvokeWithExceptionNode rethrowPendingException() {
ResolvedJavaMethod method = findMethod(JNIGeneratedMethodSupport.class, "rethrowPendingException", true);
int invokeBci = bci();
return createInvokeWithExceptionAndUnwind(method, InvokeKind.Static, getFrameState(), invokeBci);
}
public InvokeWithExceptionNode pinArrayAndGetAddress(ValueNode array, ValueNode isCopy) {
return createStaticInvoke("pinArrayAndGetAddress", array, isCopy);
}
public InvokeWithExceptionNode unpinArrayByAddress(ValueNode address) {
return createStaticInvoke("unpinArrayByAddress", address);
}
public FixedWithNextNode getPrimitiveArrayRegionRetainException(JavaKind elementKind, ValueNode array, ValueNode start, ValueNode count, ValueNode buffer) {
assert elementKind.isPrimitive();
return createStaticInvokeRetainException("getPrimitiveArrayRegion", createObject(elementKind), array, start, count, buffer);
}
public FixedWithNextNode setPrimitiveArrayRegionRetainException(JavaKind elementKind, ValueNode array, ValueNode start, ValueNode count, ValueNode buffer) {
assert elementKind.isPrimitive();
return createStaticInvokeRetainException("setPrimitiveArrayRegion", createObject(elementKind), array, start, count, buffer);
}
}