com.oracle.truffle.nfi.api.SignatureLibraryGen Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truffle-nfi Show documentation
Show all versions of truffle-nfi Show documentation
Native function interface for the Truffle framework.
// CheckStyle: start generated
package com.oracle.truffle.nfi.api;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.GenerateAOT;
import com.oracle.truffle.api.dsl.GeneratedBy;
import com.oracle.truffle.api.interop.ArityException;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.interop.UnsupportedTypeException;
import com.oracle.truffle.api.library.DynamicDispatchLibrary;
import com.oracle.truffle.api.library.Library;
import com.oracle.truffle.api.library.LibraryExport;
import com.oracle.truffle.api.library.LibraryFactory;
import com.oracle.truffle.api.library.Message;
import com.oracle.truffle.api.library.ReflectionLibrary;
import com.oracle.truffle.api.nodes.DenyReplace;
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.nodes.UnadoptableNode;
import com.oracle.truffle.api.utilities.FinalBitSet;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.util.BitSet;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@GeneratedBy(SignatureLibrary.class)
final class SignatureLibraryGen extends LibraryFactory {
private static final Class LIBRARY_CLASS = SignatureLibraryGen.lazyLibraryClass();
private static final Message CALL = new MessageImpl("call", 0, false, Object.class, Object.class, Object.class, Object[].class);
private static final Message BIND = new MessageImpl("bind", 1, false, Object.class, Object.class, Object.class);
private static final Message CREATE_CLOSURE = new MessageImpl("createClosure", 2, false, Object.class, Object.class, Object.class);
private static final SignatureLibraryGen INSTANCE = new SignatureLibraryGen();
private static final LibraryFactory DYNAMIC_DISPATCH_LIBRARY_ = LibraryFactory.resolve(DynamicDispatchLibrary.class);
static {
LibraryExport.register(SignatureLibraryGen.LIBRARY_CLASS, new Default());
LibraryFactory.register(SignatureLibraryGen.LIBRARY_CLASS, INSTANCE);
}
private SignatureLibraryGen() {
super(SignatureLibraryGen.LIBRARY_CLASS, List.of(SignatureLibraryGen.CALL, SignatureLibraryGen.BIND, SignatureLibraryGen.CREATE_CLOSURE));
}
@Override
protected Class> getDefaultClass(Object receiver) {
return SignatureLibrary.class;
}
@Override
protected Lookup getLookup() {
return MethodHandles.lookup();
}
@Override
protected SignatureLibrary createProxy(ReflectionLibrary library) {
return new Proxy(library);
}
@Override
protected FinalBitSet createMessageBitSet(Message... messages) {
BitSet bitSet = new BitSet(2);
for (Message message : messages) {
bitSet.set(message.getId());
}
return FinalBitSet.valueOf(bitSet);
}
@Override
protected SignatureLibrary createDelegate(SignatureLibrary delegateLibrary) {
return new Delegate(delegateLibrary);
}
@Override
protected Object genericDispatch(Library originalLib, Object receiver, Message message, Object[] args, int offset) throws Exception {
SignatureLibrary lib = (SignatureLibrary) originalLib;
if (message.getParameterCount() - 1 != args.length - offset) {
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new IllegalArgumentException("Invalid number of arguments.");
}
switch (message.getId()) {
case 0 :
return lib.call(receiver, args[offset], (Object[]) args[offset + 1]);
case 1 :
return lib.bind(receiver, args[offset]);
case 2 :
return lib.createClosure(receiver, args[offset]);
}
CompilerDirectives.transferToInterpreterAndInvalidate();
throw new AbstractMethodError(message.toString());
}
@Override
protected SignatureLibrary createDispatchImpl(int limit) {
return new CachedDispatchFirst(null, null, limit);
}
@Override
protected SignatureLibrary createUncachedDispatch() {
return new UncachedDispatch();
}
@SuppressWarnings("unchecked")
private static Class lazyLibraryClass() {
try {
return (Class) Class.forName("com.oracle.truffle.nfi.api.SignatureLibrary", false, SignatureLibraryGen.class.getClassLoader());
} catch (ClassNotFoundException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
}
@GeneratedBy(SignatureLibrary.class)
private static final class Default extends LibraryExport {
private Default() {
super(SignatureLibrary.class, Object.class, false, false, 0);
}
@Override
protected SignatureLibrary createUncached(Object receiver) {
SignatureLibrary uncached = new Uncached(receiver);
return uncached;
}
@Override
protected SignatureLibrary createCached(Object receiver) {
return new Cached(receiver);
}
@GeneratedBy(SignatureLibrary.class)
private static final class Cached extends SignatureLibrary {
@Child private DynamicDispatchLibrary dynamicDispatch_;
private final Class> dynamicDispatchTarget_;
protected Cached(Object receiver) {
this.dynamicDispatch_ = insert(DYNAMIC_DISPATCH_LIBRARY_.create(receiver));
this.dynamicDispatchTarget_ = DYNAMIC_DISPATCH_LIBRARY_.getUncached(receiver).dispatch(receiver);
}
@Override
public boolean accepts(Object receiver) {
return dynamicDispatch_.accepts(receiver) && dynamicDispatch_.dispatch(receiver) == dynamicDispatchTarget_;
}
@TruffleBoundary
@Override
public Object call(Object receiver, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
assert this.accepts(receiver) : "Invalid library usage. Library does not accept given receiver.";
throw new AbstractMethodError();
}
@TruffleBoundary
@Override
public Object bind(Object receiver, Object functionPointer) {
assert this.accepts(receiver) : "Invalid library usage. Library does not accept given receiver.";
throw new AbstractMethodError();
}
@TruffleBoundary
@Override
public Object createClosure(Object receiver, Object executable) {
assert this.accepts(receiver) : "Invalid library usage. Library does not accept given receiver.";
throw new AbstractMethodError();
}
}
@GeneratedBy(SignatureLibrary.class)
@DenyReplace
private static final class Uncached extends SignatureLibrary implements UnadoptableNode {
@Child private DynamicDispatchLibrary dynamicDispatch_;
private final Class> dynamicDispatchTarget_;
protected Uncached(Object receiver) {
this.dynamicDispatch_ = DYNAMIC_DISPATCH_LIBRARY_.getUncached(receiver);
this.dynamicDispatchTarget_ = dynamicDispatch_.dispatch(receiver);
}
@Override
@TruffleBoundary
public boolean accepts(Object receiver) {
return dynamicDispatch_.accepts(receiver) && dynamicDispatch_.dispatch(receiver) == dynamicDispatchTarget_;
}
@TruffleBoundary
@Override
public Object call(Object receiver, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
// declared: true
assert this.accepts(receiver) : "Invalid library usage. Library does not accept given receiver.";
throw new AbstractMethodError();
}
@TruffleBoundary
@Override
public Object bind(Object receiver, Object functionPointer) {
// declared: true
assert this.accepts(receiver) : "Invalid library usage. Library does not accept given receiver.";
throw new AbstractMethodError();
}
@TruffleBoundary
@Override
public Object createClosure(Object receiver, Object executable) {
// declared: true
assert this.accepts(receiver) : "Invalid library usage. Library does not accept given receiver.";
throw new AbstractMethodError();
}
}
}
@GeneratedBy(SignatureLibrary.class)
private static class MessageImpl extends Message {
MessageImpl(String name, int index, boolean deprecated, Class> returnType, Class>... parameters) {
super(SignatureLibraryGen.LIBRARY_CLASS, name, index, deprecated, returnType, parameters);
}
}
@GeneratedBy(SignatureLibrary.class)
private static final class Proxy extends SignatureLibrary {
@Child private ReflectionLibrary lib;
Proxy(ReflectionLibrary lib) {
this.lib = lib;
}
@Override
public Object call(Object receiver_, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
try {
return lib.send(receiver_, SignatureLibraryGen.CALL, functionPointer, args);
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException | RuntimeException e_) {
throw e_;
} catch (Exception e_) {
throw CompilerDirectives.shouldNotReachHere(e_);
}
}
@Override
public Object bind(Object receiver_, Object functionPointer) {
try {
return lib.send(receiver_, SignatureLibraryGen.BIND, functionPointer);
} catch (RuntimeException e_) {
throw e_;
} catch (Exception e_) {
throw CompilerDirectives.shouldNotReachHere(e_);
}
}
@Override
public Object createClosure(Object receiver_, Object executable) {
try {
return lib.send(receiver_, SignatureLibraryGen.CREATE_CLOSURE, executable);
} catch (RuntimeException e_) {
throw e_;
} catch (Exception e_) {
throw CompilerDirectives.shouldNotReachHere(e_);
}
}
@Override
public boolean accepts(Object receiver_) {
return lib.accepts(receiver_);
}
}
@GeneratedBy(SignatureLibrary.class)
private static final class Delegate extends SignatureLibrary {
@Child private SignatureLibrary delegateLibrary;
Delegate(SignatureLibrary delegateLibrary) {
this.delegateLibrary = delegateLibrary;
}
@Override
public Object call(Object receiver_, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
if (LibraryFactory.isDelegated(delegateLibrary, 0)) {
Object delegate = LibraryFactory.readDelegate(this.delegateLibrary, receiver_);
return LibraryFactory.getDelegateLibrary(this.delegateLibrary, delegate).call(delegate, functionPointer, args);
} else {
return this.delegateLibrary.call(receiver_, functionPointer, args);
}
}
@Override
public Object bind(Object receiver_, Object functionPointer) {
if (LibraryFactory.isDelegated(delegateLibrary, 1)) {
Object delegate = LibraryFactory.readDelegate(this.delegateLibrary, receiver_);
return LibraryFactory.getDelegateLibrary(this.delegateLibrary, delegate).bind(delegate, functionPointer);
} else {
return this.delegateLibrary.bind(receiver_, functionPointer);
}
}
@Override
public Object createClosure(Object receiver_, Object executable) {
if (LibraryFactory.isDelegated(delegateLibrary, 2)) {
Object delegate = LibraryFactory.readDelegate(this.delegateLibrary, receiver_);
return LibraryFactory.getDelegateLibrary(this.delegateLibrary, delegate).createClosure(delegate, executable);
} else {
return this.delegateLibrary.createClosure(receiver_, executable);
}
}
@Override
public boolean accepts(Object receiver_) {
return delegateLibrary.accepts(receiver_);
}
@Override
public boolean isAdoptable() {
return this.delegateLibrary.isAdoptable();
}
}
@GeneratedBy(SignatureLibrary.class)
private static final class CachedToUncachedDispatch extends SignatureLibrary {
@TruffleBoundary
@Override
public Object call(Object receiver_, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
assert assertAdopted(this);
EncapsulatingNodeReference encapsulating_ = EncapsulatingNodeReference.getCurrent();
Node prev_ = encapsulating_.set(getParent());
try {
return INSTANCE.getUncached(receiver_).call(receiver_, functionPointer, args);
} finally {
encapsulating_.set(prev_);
}
}
@TruffleBoundary
@Override
public Object bind(Object receiver_, Object functionPointer) {
assert assertAdopted(this);
EncapsulatingNodeReference encapsulating_ = EncapsulatingNodeReference.getCurrent();
Node prev_ = encapsulating_.set(getParent());
try {
return INSTANCE.getUncached(receiver_).bind(receiver_, functionPointer);
} finally {
encapsulating_.set(prev_);
}
}
@TruffleBoundary
@Override
public Object createClosure(Object receiver_, Object executable) {
assert assertAdopted(this);
EncapsulatingNodeReference encapsulating_ = EncapsulatingNodeReference.getCurrent();
Node prev_ = encapsulating_.set(getParent());
try {
return INSTANCE.getUncached(receiver_).createClosure(receiver_, executable);
} finally {
encapsulating_.set(prev_);
}
}
@Override
public boolean accepts(Object receiver_) {
return true;
}
}
@GeneratedBy(SignatureLibrary.class)
@DenyReplace
private static final class UncachedDispatch extends SignatureLibrary implements UnadoptableNode {
@TruffleBoundary
@Override
public Object call(Object receiver_, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
return INSTANCE.getUncached(receiver_).call(receiver_, functionPointer, args);
}
@TruffleBoundary
@Override
public Object bind(Object receiver_, Object functionPointer) {
return INSTANCE.getUncached(receiver_).bind(receiver_, functionPointer);
}
@TruffleBoundary
@Override
public Object createClosure(Object receiver_, Object executable) {
return INSTANCE.getUncached(receiver_).createClosure(receiver_, executable);
}
@TruffleBoundary
@Override
public boolean accepts(Object receiver_) {
return true;
}
}
@GeneratedBy(SignatureLibrary.class)
private static final class CachedDispatchNext extends CachedDispatch {
CachedDispatchNext(SignatureLibrary library, CachedDispatch next) {
super(library, next);
}
@Override
int getLimit() {
throw CompilerDirectives.shouldNotReachHere();
}
@Override
void setAOT(boolean value) {
throw CompilerDirectives.shouldNotReachHere();
}
@Override
boolean isAOT() {
throw CompilerDirectives.shouldNotReachHere();
}
}
@GeneratedBy(SignatureLibrary.class)
private static final class CachedDispatchFirst extends CachedDispatch implements GenerateAOT.Provider {
private final int limit_;
private volatile boolean aot_;
CachedDispatchFirst(SignatureLibrary library, CachedDispatch next, int limit_) {
super(library, next);
this.limit_ = limit_;
}
@Override
void setAOT(boolean value) {
this.aot_ = value;
}
@Override
boolean isAOT() {
return this.aot_;
}
@Override
int getLimit() {
return this.limit_;
}
@Override
public void prepareForAOT(TruffleLanguage> language, RootNode root) {
assert ((ReentrantLock) getLock()).isHeldByCurrentThread() : "During prepare AST lock must be held.";
if (this.aot_) {
return;
}
for (LibraryExport export : INSTANCE.getAOTExports()) {
SignatureLibrary exportLibrary = INSTANCE.createAOT(export);
if (this.library == null) {
this.library = insert(exportLibrary);
} else {
this.next = insert(new CachedDispatchNext(exportLibrary, this.next));
}
}
this.aot_ = true;
}
}
@GeneratedBy(SignatureLibrary.class)
private abstract static class CachedDispatch extends SignatureLibrary {
@Child SignatureLibrary library;
@Child CachedDispatch next;
CachedDispatch(SignatureLibrary library, CachedDispatch next) {
this.library = library;
this.next = next;
}
abstract int getLimit();
abstract boolean isAOT();
abstract void setAOT(boolean value);
@ExplodeLoop
@Override
public Object call(Object receiver_, Object functionPointer, Object... args) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
if (CompilerDirectives.inInterpreter() && isAOT()) {
CompilerDirectives.transferToInterpreterAndInvalidate();
specialize(receiver_);
}
do {
CachedDispatch current = this;
do {
SignatureLibrary thisLibrary = current.library;
if (thisLibrary != null && thisLibrary.accepts(receiver_)) {
return thisLibrary.call(receiver_, functionPointer, args);
}
current = current.next;
} while (current != null);
CompilerDirectives.transferToInterpreterAndInvalidate();
specialize(receiver_);
} while (true);
}
@ExplodeLoop
@Override
public Object bind(Object receiver_, Object functionPointer) {
if (CompilerDirectives.inInterpreter() && isAOT()) {
CompilerDirectives.transferToInterpreterAndInvalidate();
specialize(receiver_);
}
do {
CachedDispatch current = this;
do {
SignatureLibrary thisLibrary = current.library;
if (thisLibrary != null && thisLibrary.accepts(receiver_)) {
return thisLibrary.bind(receiver_, functionPointer);
}
current = current.next;
} while (current != null);
CompilerDirectives.transferToInterpreterAndInvalidate();
specialize(receiver_);
} while (true);
}
@ExplodeLoop
@Override
public Object createClosure(Object receiver_, Object executable) {
if (CompilerDirectives.inInterpreter() && isAOT()) {
CompilerDirectives.transferToInterpreterAndInvalidate();
specialize(receiver_);
}
do {
CachedDispatch current = this;
do {
SignatureLibrary thisLibrary = current.library;
if (thisLibrary != null && thisLibrary.accepts(receiver_)) {
return thisLibrary.createClosure(receiver_, executable);
}
current = current.next;
} while (current != null);
CompilerDirectives.transferToInterpreterAndInvalidate();
specialize(receiver_);
} while (true);
}
@Override
public boolean accepts(Object receiver_) {
return true;
}
private void specialize(Object receiver_) {
Lock lock = getLock();
lock.lock();
try {
if (isAOT()) {
this.library = null;
this.next = null;
setAOT(false);
}
CachedDispatch current = this;
SignatureLibrary thisLibrary = current.library;
if (thisLibrary == null) {
this.library = insert(INSTANCE.create(receiver_));
} else {
int count = 0;
do {
SignatureLibrary currentLibrary = current.library;
if (currentLibrary != null && currentLibrary.accepts(receiver_)) {
return;
}
count++;
current = current.next;
} while (current != null);
if (count >= getLimit()) {
this.library = insert(new CachedToUncachedDispatch());
this.next = null;
} else {
this.next = insert(new CachedDispatchNext(INSTANCE.create(receiver_), next));
}
}
} finally {
lock.unlock();
}
}
}
}