package com.oracle.truffle.llvm.runtime.nodes.func;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.CachedContext;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.llvm.runtime.ContextExtension;
import com.oracle.truffle.llvm.runtime.LLVMContext;
import com.oracle.truffle.llvm.runtime.LLVMLanguage;
import com.oracle.truffle.llvm.runtime.NativeContextExtension;
import com.oracle.truffle.llvm.runtime.except.LLVMNativePointerException;
import com.oracle.truffle.llvm.runtime.interop.nfi.LLVMNativeConvertNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMNode;
import com.oracle.truffle.llvm.runtime.pointer.LLVMNativePointer;
import com.oracle.truffle.llvm.runtime.types.FunctionType;
public abstract class LLVMNativeDispatchNode extends LLVMNode {
private final FunctionType type;
private final Source signatureSource;
@CompilationFinal private ContextExtension.Key<NativeContextExtension> nativeCtxExtKey;
protected LLVMNativeDispatchNode(FunctionType type, Source signatureSource) {
this.type = type;
this.signatureSource = signatureSource;
}
public abstract Object executeDispatch(Object function, Object[] arguments);
NativeContextExtension getNativeCtxExt(ContextReference<LLVMContext> ctxRef) {
if (nativeCtxExtKey == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
nativeCtxExtKey = ctxRef.get().getLanguage().lookupContextExtension(NativeContextExtension.class);
}
return nativeCtxExtKey.get(ctxRef.get());
}
@TruffleBoundary
protected Object bindSignature(NativeContextExtension ctxExt, long pointer) {
return ctxExt.bindSignature(pointer, signatureSource);
}
@ExplodeLoop
protected LLVMNativeConvertNode[] createToNativeNodes() {
LLVMNativeConvertNode[] ret = new LLVMNativeConvertNode[type.getNumberOfArguments() - LLVMCallNode.USER_ARGUMENT_OFFSET];
for (int i = LLVMCallNode.USER_ARGUMENT_OFFSET; i < type.getNumberOfArguments(); i++) {
ret[i - LLVMCallNode.USER_ARGUMENT_OFFSET] = LLVMNativeConvertNode.createToNative(type.getArgumentType(i));
}
return ret;
}
protected LLVMNativeConvertNode createFromNativeNode() {
CompilerAsserts.neverPartOfCompilation();
return LLVMNativeConvertNode.createFromNative(type.getReturnType());
}
@ExplodeLoop
private static Object[] prepareNativeArguments(Object[] arguments, LLVMNativeConvertNode[] toNative) {
Object[] nativeArgs = new Object[arguments.length - LLVMCallNode.USER_ARGUMENT_OFFSET];
for (int i = LLVMCallNode.USER_ARGUMENT_OFFSET; i < arguments.length; i++) {
nativeArgs[i - LLVMCallNode.USER_ARGUMENT_OFFSET] = toNative[i - LLVMCallNode.USER_ARGUMENT_OFFSET].executeConvert(arguments[i]);
}
return nativeArgs;
}
@Specialization(guards = {"function.asNative() == cachedFunction.asNative()", "!cachedFunction.isNull()"}, assumptions = "singleContextAssumption()")
protected Object doCached(LLVMNativePointer function, Object[] arguments,
@CachedContext(LLVMLanguage.class) ContextReference<LLVMContext> context,
@Cached("function") @SuppressWarnings("unused") LLVMNativePointer cachedFunction,
@Cached("bindSignature(getNativeCtxExt(context), cachedFunction.asNative())") Object nativeFunctionHandle,
@CachedLibrary("nativeFunctionHandle") InteropLibrary nativeCall,
@Cached("createToNativeNodes()") LLVMNativeConvertNode[] toNative,
@Cached("createFromNativeNode()") LLVMNativeConvertNode fromNative,
@Cached("nativeCallStatisticsEnabled(context)") boolean statistics) {
Object[] nativeArgs = prepareNativeArguments(arguments, toNative);
Object returnValue;
returnValue = LLVMNativeCallUtils.callNativeFunction(statistics, context, nativeCall, nativeFunctionHandle, nativeArgs, null);
return fromNative.executeConvert(returnValue);
}
@Specialization(replaces = "doCached", guards = "!function.isNull()")
protected Object doGeneric(LLVMNativePointer function, Object[] arguments,
@CachedContext(LLVMLanguage.class) ContextReference<LLVMContext> context,
@Cached("createToNativeNodes()") LLVMNativeConvertNode[] toNative,
@Cached("createFromNativeNode()") LLVMNativeConvertNode fromNative,
@CachedLibrary(limit = "5") InteropLibrary nativeCall,
@Cached("nativeCallStatisticsEnabled(context)") boolean statistics) {
Object[] nativeArgs = prepareNativeArguments(arguments, toNative);
Object returnValue;
Object bound = bindSignature(getNativeCtxExt(context), function.asNative());
returnValue = LLVMNativeCallUtils.callNativeFunction(statistics, context, nativeCall, bound, nativeArgs, null);
return fromNative.executeConvert(returnValue);
}
@Specialization(guards = "function.isNull()")
protected Object doNull(@SuppressWarnings("unused") LLVMNativePointer function, @SuppressWarnings("unused") Object[] arguments) {
throw new LLVMNativePointerException(this, "Invalid native function pointer", null);
}
}