com.oracle.truffle.api.library.CachedLibrary Java Examples

The following examples show how to use com.oracle.truffle.api.library.CachedLibrary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 6 votes vote down vote up
@Specialization(guards = {"lib.hasMembers(object)"}, limit = "2")
protected static final ArrayObject doGetMembers(@SuppressWarnings("unused") final Object receiver, final Object object, final boolean includeInternal,
                @CachedLibrary("object") final InteropLibrary lib,
                @CachedLibrary(limit = "2") final InteropLibrary membersLib,
                @CachedLibrary(limit = "2") final InteropLibrary memberNameLib,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image) {
    // TODO: is unpacking really necessary?
    try {
        final Object members = lib.getMembers(object, includeInternal);
        final int size = (int) membersLib.getArraySize(members);
        final Object[] byteStrings = new Object[size];
        for (int i = 0; i < size; i++) {
            final Object memberName = membersLib.readArrayElement(members, i);
            byteStrings[i] = image.asByteString(memberNameLib.asString(memberName));
        }
        return image.asArrayOfObjects(byteStrings);
    } catch (final UnsupportedMessageException | InvalidArrayIndexException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #2
Source File: ConfiguredKernel.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
@TruffleBoundary
Object execute(Object[] arguments,
                @CachedLibrary(limit = "3") InteropLibrary boolAccess,
                @CachedLibrary(limit = "3") InteropLibrary int8Access,
                @CachedLibrary(limit = "3") InteropLibrary int16Access,
                @CachedLibrary(limit = "3") InteropLibrary int32Access,
                @CachedLibrary(limit = "3") InteropLibrary int64Access,
                @CachedLibrary(limit = "3") InteropLibrary doubleAccess) throws UnsupportedTypeException, ArityException {
    kernel.incrementLaunchCount();
    try (KernelArguments args = kernel.createKernelArguments(arguments, boolAccess, int8Access, int16Access,
                    int32Access, int64Access, doubleAccess)) {
        kernel.getCudaRuntime().cuLaunchKernel(kernel, config, args);
    }
    return this;
}
 
Example #3
Source File: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Specialization(limit = "3")
Object doMap(Object source, Type elementType, CUDARuntime runtime,
                @CachedLibrary("source") InteropLibrary interop,
                @CachedContext(GrCUDALanguage.class) @SuppressWarnings("unused") GrCUDAContext context,
                @Cached(value = "createLoop(source)", uncached = "createUncachedLoop(source, context)") CallTarget loop) {

    if (source instanceof DeviceArray && ((DeviceArray) source).getElementType() == elementType) {
        return source;
    }

    if (!interop.hasArrayElements(source)) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("cannot map from non-array to DeviceArray");
    }

    long size;
    try {
        size = interop.getArraySize(source);
    } catch (UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("cannot read array size");
    }
    DeviceArray result = new DeviceArray(runtime, size, elementType);
    loop.call(size, source, result);
    return result;
}
 
Example #4
Source File: DeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object invokeMember(String memberName,
                Object[] arguments,
                @CachedLibrary("this") InteropLibrary interopRead,
                @CachedLibrary(limit = "1") InteropLibrary interopExecute)
                throws UnsupportedTypeException, ArityException, UnsupportedMessageException, UnknownIdentifierException {
    return interopExecute.execute(interopRead.readMember(this, memberName), arguments);
}
 
Example #5
Source File: SqueakFFIPrims.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"getAtomicType(headerWord) == 10", "isPointerType(headerWord)"}, limit = "1")
protected static final String doString(@SuppressWarnings("unused") final int headerWord, final Object value,
                @CachedLibrary("value") final InteropLibrary lib) {
    try {
        return lib.asString(value);
    } catch (final UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        e.printStackTrace();
        return "unknown";
    }
}
 
Example #6
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.fitsInDouble(object)"}, limit = "2")
protected static final double doAsDouble(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        return lib.asDouble(object);
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #7
Source File: MultiDimDeviceArray.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object invokeMember(String memberName,
                Object[] arguments,
                @CachedLibrary("this") InteropLibrary interopRead,
                @CachedLibrary(limit = "1") InteropLibrary interopExecute)
                throws UnsupportedTypeException, ArityException, UnsupportedMessageException, UnknownIdentifierException {
    return interopExecute.execute(interopRead.readMember(this, memberName), arguments);
}
 
Example #8
Source File: DeviceArray.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object invokeMember(String memberName,
                Object[] arguments,
                @CachedLibrary("this") InteropLibrary interopRead,
                @CachedLibrary(limit = "1") InteropLibrary interopExecute)
                throws UnsupportedTypeException, ArityException, UnsupportedMessageException, UnknownIdentifierException {
    return interopExecute.execute(interopRead.readMember(this, memberName), arguments);
}
 
Example #9
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.hasMembers(object)"}, limit = "2")
protected static final Object doGetMembers(@SuppressWarnings("unused") final Object receiver, final Object object, final boolean includeInternal,
                @CachedLibrary("object") final InteropLibrary lib,
                @CachedLibrary(limit = "2") final InteropLibrary sizeLib) {
    try {
        return sizeLib.getArraySize(lib.getMembers(object, includeInternal));
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #10
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.isPointer(object)"}, limit = "2")
protected static final long doAsPointer(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        return lib.asPointer(object);
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #11
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.hasMetaObject(object)")
protected static final Object getMetaObject(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedLibrary(limit = "2") final InteropLibrary lib) {
    try {
        return lib.getMetaObject(object);
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #12
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.isExecutable(object)"}, limit = "2")
protected static final Object doExecute(@SuppressWarnings("unused") final Object receiver, final Object object, final ArrayObject argumentArray,
                @Cached final ArrayObjectToObjectArrayCopyNode getObjectArrayNode,
                @Cached final WrapToSqueakNode wrapNode,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        return wrapNode.executeWrap(lib.execute(object, getObjectArrayNode.execute(argumentArray)));
    } catch (final Exception e) {
        /*
         * Workaround: catch all exceptions raised by other languages to avoid crashes (see
         * https://github.com/oracle/truffleruby/issues/1864).
         */
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #13
Source File: HashemReadPropertyNode.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Specialization(guards = "arrays.hasArrayElements(receiver)", limit = "LIBRARY_LIMIT")
protected Object writeArray(Object receiver, Object index,
                            @CachedLibrary("receiver") InteropLibrary arrays,
                            @CachedLibrary("index") InteropLibrary numbers) {
    try {
        return arrays.readArrayElement(receiver, numbers.asLong(index));
    } catch (UnsupportedMessageException | InvalidArrayIndexException e) {
        // read was not successful. InHashemiwe only have basic support for errors.
        throw HashemUndefinedNameException.undefinedProperty(this, index);
    }
}
 
Example #14
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.isDuration(object)")
protected static final Object doAsDuration(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image,
                @CachedLibrary(limit = "2") final InteropLibrary lib) {
    try {
        return image.env.asGuestValue(lib.asDuration(object));
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #15
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.isArrayElementWritable(object, index)"}, limit = "2")
protected static final Object doWrite(@SuppressWarnings("unused") final Object receiver, final Object object, final long index, final Object value,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        lib.writeArrayElement(object, index - 1, value);
        return value;
    } catch (InvalidArrayIndexException | UnsupportedMessageException | UnsupportedTypeException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #16
Source File: UnixOSProcessPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "supportsNFI")
protected final NativeObject doErrorMessageAt(@SuppressWarnings("unused") final Object receiver, final long index,
                @CachedLibrary("getSysCallObject()") final InteropLibrary lib,
                @CachedLibrary(limit = "1") final InteropLibrary resultLib,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image) {
    try {
        return image.asByteString(resultLib.asString(lib.execute(sysCallObject, (int) index)));
    } catch (final UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
        throw PrimitiveFailed.andTransferToInterpreterWithError(e);
    }
}
 
Example #17
Source File: MappedFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary("this.parent") InteropLibrary parentInterop) throws UnsupportedTypeException, ArityException, UnsupportedMessageException {
    if (arguments.length != 3) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(3, arguments.length);
    }
    return new ShreddedObject(parentInterop.execute(parent, arguments));
}
 
Example #18
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected long asLong(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
    if (isNumber()) {
        return lib.asLong(wrappedObject);
    } else {
        throw UnsupportedMessageException.create();
    }
}
 
Example #19
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected short asShort(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
    if (isNumber()) {
        return lib.asShort(wrappedObject);
    } else {
        throw UnsupportedMessageException.create();
    }
}
 
Example #20
Source File: MapDeviceArrayFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@ExportMessage
public Object execute(Object[] arguments,
                @CachedLibrary(limit = "2") InteropLibrary stringInterop,
                @Cached("createIdentityProfile()") ValueProfile elementTypeStringProfile,
                @Cached("createIdentityProfile()") ValueProfile elementTypeProfile,
                @Cached MapArrayNode mapNode) throws ArityException, UnsupportedTypeException {
    if (arguments.length < 1) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(1, arguments.length);
    }
    String typeName;
    try {
        typeName = elementTypeStringProfile.profile(stringInterop.asString(arguments[0]));
    } catch (UnsupportedMessageException e1) {
        throw UnsupportedTypeException.create(arguments, "first argument of MapDeviceArray must be string (type name)");
    }
    Type elementType;
    try {
        elementType = elementTypeProfile.profile(Type.fromGrCUDATypeString(typeName));
    } catch (TypeException e) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAInternalException(e.getMessage());
    }
    if (arguments.length == 1) {
        return new TypedMapDeviceArrayFunction(runtime, elementType);
    } else {
        if (arguments.length != 2) {
            CompilerDirectives.transferToInterpreter();
            throw ArityException.create(2, arguments.length);
        }
        return mapNode.execute(arguments[1], elementType, runtime);
    }
}
 
Example #21
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.fitsInLong(object)"}, limit = "2")
protected static final long doAsLong(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        return lib.asLong(object);
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #22
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.isArrayElementRemovable(object, to0(index))"}, limit = "2")
protected static final Object doRemoveArrayElement(@SuppressWarnings("unused") final Object receiver, final Object object, final long index,
                @CachedLibrary("object") final InteropLibrary lib) {
    try {
        lib.removeArrayElement(object, index - 1);
        return object;
    } catch (final UnsupportedMessageException | InvalidArrayIndexException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #23
Source File: ConvertToSqueakNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.isString(value)", limit = "1")
protected static final NativeObject doString(final Object value,
                @CachedLibrary("value") final InteropLibrary lib,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image) {
    try {
        return image.asByteString(lib.asString(value));
    } catch (final UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        e.printStackTrace();
        return image.asByteString("");
    }
}
 
Example #24
Source File: ConvertToSqueakNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.fitsInLong(value)", limit = "1")
protected static final long doLong(final Object value,
                @CachedLibrary("value") final InteropLibrary lib) {
    try {
        return lib.asLong(value);
    } catch (final UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        e.printStackTrace();
        return 0L;
    }
}
 
Example #25
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected boolean fitsInByte(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    if (isNumber()) {
        return lib.fitsInByte(wrappedObject);
    } else {
        return false;
    }
}
 
Example #26
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected boolean fitsInShort(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    if (isNumber()) {
        return lib.fitsInShort(wrappedObject);
    } else {
        return false;
    }
}
 
Example #27
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.isMetaObject(object)")
protected static final Object getMetaQualifiedName(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedLibrary(limit = "2") final InteropLibrary lib) {
    try {
        return lib.getMetaQualifiedName(object);
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #28
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.hasLanguage(object)")
protected static final Object getLanguage(@SuppressWarnings("unused") final Object receiver, final Object object,
                @CachedContext(SqueakLanguage.class) final SqueakImageContext image,
                @CachedLibrary(limit = "2") final InteropLibrary lib) {
    try {
        return image.env.asGuestValue(lib.getLanguage(object));
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #29
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected boolean fitsInFloat(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    if (isNumber()) {
        return lib.fitsInFloat(wrappedObject);
    } else {
        return false;
    }
}
 
Example #30
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected boolean fitsInDouble(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    if (isNumber()) {
        return lib.fitsInDouble(wrappedObject);
    } else {
        return false;
    }
}