package com.oracle.truffle.llvm.nativemode.runtime;
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.ArityException;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.llvm.nativemode.runtime.NFIContextExtension.WellKnownFunction;
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.NativeContextExtension.WellKnownNativeFunctionNode;
abstract class WellKnownNFIFunctionNode extends WellKnownNativeFunctionNode {
private final WellKnownFunction function;
private final ContextExtension.Key<NativeContextExtension> ctxExtKey;
WellKnownNFIFunctionNode(WellKnownFunction function) {
this.function = function;
this.ctxExtKey = LLVMLanguage.getLanguage().lookupContextExtension(NativeContextExtension.class);
}
Object getFunction(ContextReference<LLVMContext> ctx) {
NFIContextExtension ctxExt = (NFIContextExtension) ctxExtKey.get(ctx.get());
return ctxExt.getCachedWellKnownFunction(function);
}
@Specialization(assumptions = "singleContextAssumption()")
Object doCached(Object[] args,
@SuppressWarnings("unused") @CachedContext(LLVMLanguage.class) ContextReference<LLVMContext> ctx,
@Cached("getFunction(ctx)") Object cachedFunction,
@CachedLibrary("cachedFunction") InteropLibrary interop) throws ArityException, UnsupportedMessageException, UnsupportedTypeException {
return interop.execute(cachedFunction, args);
}
@Specialization(replaces = "doCached")
Object doGeneric(Object[] args,
@CachedContext(LLVMLanguage.class) ContextReference<LLVMContext> ctx,
@CachedLibrary(limit = "3") InteropLibrary interop) throws ArityException, UnsupportedMessageException, UnsupportedTypeException {
Object fn = getFunction(ctx);
return interop.execute(fn, args);
}
}