com.oracle.truffle.api.interop.InteropLibrary Java Examples

The following examples show how to use com.oracle.truffle.api.interop.InteropLibrary. 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: 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 #2
Source File: CallNode.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Specialization
Object doDefault(VirtualFrame frame,
                @CachedLibrary(limit = "2") InteropLibrary interop,
                @CachedContext(GrCUDALanguage.class) GrCUDAContext context) {
    String[] functionName = identifier.getIdentifierName();
    Namespace namespace = context.getRootNamespace();
    Optional<Object> maybeFunction = namespace.lookup(functionName);
    if (!maybeFunction.isPresent() || !(maybeFunction.get() instanceof Function)) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException("function '" + GrCUDAException.format(functionName) + "' not found", this);
    }
    Function function = (Function) maybeFunction.get();
    Object[] argumentValues = new Object[argumentNodes.length];
    for (int i = 0; i < argumentNodes.length; i++) {
        argumentValues[i] = argumentNodes[i].execute(frame);
    }
    try {
        return interop.execute(function, argumentValues);
    } catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw new GrCUDAException(e.getMessage(), this);
    }
}
 
Example #3
Source File: Kernel.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary(limit = "3") InteropLibrary gridSizeAccess,
                @CachedLibrary(limit = "3") InteropLibrary gridSizeElementAccess,
                @CachedLibrary(limit = "3") InteropLibrary blockSizeAccess,
                @CachedLibrary(limit = "3") InteropLibrary blockSizeElementAccess,
                @CachedLibrary(limit = "3") InteropLibrary sharedMemoryAccess) throws UnsupportedTypeException, ArityException {
    int dynamicSharedMemoryBytes;
    if (arguments.length == 2) {
        dynamicSharedMemoryBytes = 0;
    } else if (arguments.length == 3) {
        // dynamic shared memory specified
        dynamicSharedMemoryBytes = extractNumber(arguments[2], "dynamicSharedMemory", sharedMemoryAccess);
    } else {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(2, arguments.length);
    }

    Dim3 gridSize = extractDim3(arguments[0], "gridSize", gridSizeAccess, gridSizeElementAccess);
    Dim3 blockSize = extractDim3(arguments[1], "blockSize", blockSizeAccess, blockSizeElementAccess);
    KernelConfig config = new KernelConfig(gridSize, blockSize, dynamicSharedMemoryBytes);

    return new ConfiguredKernel(this, config);
}
 
Example #4
Source File: ShreddedObject.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
@TruffleBoundary
public Object readMember(String member,
                @CachedLibrary("this.delegate") InteropLibrary memberInterop) {
    if (memberInterop.isMemberReadable(delegate, member)) {
        try {
            return memberInterop.readMember(delegate, member);
        } catch (UnsupportedMessageException | UnknownIdentifierException e) {
            CompilerDirectives.transferToInterpreter();
            throw new MapException("cannot read 'readable' member: " + e.getMessage());
        }
    } else {
        if (memberInterop.hasArrayElements(delegate)) {
            return new ShreddedObjectMember(delegate, member);
        } else {
            CompilerDirectives.transferToInterpreter();
            throw new MapException("cannot shred object without size and member");
        }
    }
}
 
Example #5
Source File: HashemUnboxNode.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Specialization(limit = "LIMIT")
public static Object fromForeign(Object value, @CachedLibrary("value") InteropLibrary interop) {
    try {
        if (interop.fitsInLong(value)) {
            return interop.asLong(value);
        } else if (interop.fitsInFloat(value)) {
            return interop.asFloat(value);
        } else if (interop.isString(value)) {
            return interop.asString(value);
        } else if (interop.isBoolean(value)) {
            return interop.asBoolean(value);
        } else {
            return value;
        }
    } catch (UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw new AssertionError();
    }
}
 
Example #6
Source File: DeviceArrayCopyFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary(limit = "3") InteropLibrary pointerAccess,
                @CachedLibrary(limit = "3") InteropLibrary numElementsAccess) throws UnsupportedTypeException, ArityException, IndexOutOfBoundsException {
    long numElements;
    if (arguments.length == 1) {
        numElements = deviceArray.getArraySize();
    } else if (arguments.length == 2) {
        numElements = extractNumber(arguments[1], "numElements", numElementsAccess);
    } else {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(1, arguments.length);
    }
    long pointer = extractPointer(arguments[0], "fromPointer", pointerAccess);
    if (direction == CopyDirection.FROM_POINTER) {
        deviceArray.copyFrom(pointer, numElements);
    }
    if (direction == CopyDirection.TO_POINTER) {
        deviceArray.copyTo(pointer, numElements);
    }
    return deviceArray;
}
 
Example #7
Source File: MappedFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary("this.parent") InteropLibrary parentInterop,
                @CachedLibrary(limit = "2") InteropLibrary memberInterop) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
    if (arguments.length != 3) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(3, arguments.length);
    }
    Object value = parentInterop.execute(parent, arguments);
    try {
        return memberInterop.readMember(value, name);
    } catch (UnsupportedMessageException | UnknownIdentifierException e) {
        CompilerDirectives.transferToInterpreter();
        throw new MapException("cannot read member '" + name + "' from argument " + parent);
    }
}
 
Example #8
Source File: MappedFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary("this.parent") InteropLibrary parentInterop,
                @CachedLibrary(limit = "2") InteropLibrary elementInterop) throws ArityException, UnsupportedTypeException, UnsupportedMessageException {
    if (arguments.length != 3) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(3, arguments.length);
    }
    Object value = parentInterop.execute(parent, arguments);
    try {
        return elementInterop.readArrayElement(value, index);
    } catch (UnsupportedMessageException | InvalidArrayIndexException e) {
        CompilerDirectives.transferToInterpreter();
        throw new MapException("cannot read element '" + index + "' from argument " + parent);
    }
}
 
Example #9
Source File: MappedFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
Object execute(Object[] arguments,
                @CachedLibrary("this.parent") InteropLibrary parentInterop,
                @CachedLibrary("this.function") InteropLibrary mapInterop) throws UnsupportedTypeException, ArityException, UnsupportedMessageException {
    if (arguments.length != 3) {
        CompilerDirectives.transferToInterpreter();
        throw ArityException.create(3, arguments.length);
    }
    Object value = parentInterop.execute(parent, arguments);
    try {
        return mapInterop.execute(function, value);
    } catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
        CompilerDirectives.transferToInterpreter();
        throw new MapException("cannot map argument " + parent);
    }
}
 
Example #10
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 #11
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 #12
Source File: AbstractFactory.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
protected static Object readCBProperty(DynamicObject cb, String name) {
    if (cb == null) {
        return null;
    }
    try {
        Object val = InteropLibrary.getFactory().getUncached(cb).readMember(cb, name);
        return val == null ? null : val;
    } catch (UnknownIdentifierException | UnsupportedMessageException e) {
        return null;
    }
}
 
Example #13
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected boolean fitsInInt(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    if (isNumber()) {
        return lib.fitsInInt(wrappedObject);
    } else {
        return false;
    }
}
 
Example #14
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected boolean fitsInLong(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    if (isNumber()) {
        return lib.fitsInLong(wrappedObject);
    } else {
        return false;
    }
}
 
Example #15
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 #16
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;
    }
}
 
Example #17
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 #18
Source File: PolyglotPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = "lib.isInstant(object)")
protected static final Object doAsInstant(@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.asInstant(object));
    } catch (final UnsupportedMessageException e) {
        throw primitiveFailedInInterpreterCapturing(e);
    }
}
 
Example #19
Source File: ConvertToSqueakNode.java    From trufflesqueak with MIT License 5 votes vote down vote up
@Specialization(guards = {"lib.fitsInDouble(value)", "!lib.fitsInLong(value)"}, limit = "1")
protected static final double doDouble(final Object value,
                @CachedLibrary("value") final InteropLibrary lib) {
    try {
        return lib.asDouble(value);
    } catch (final UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        e.printStackTrace();
        return 0D;
    }
}
 
Example #20
Source File: BaseEventHandlerNode.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
/**
 *
 * get the node-specific attribute, in case of missing such attributes, return null
 *
 * @param key of the current InstrumentableNode
 * @return the value of this key
 */
public Object getAttributeNoReport(String key) {
    Object result = null;
    try {
        result = InteropLibrary.getFactory().getUncached().readMember(((InstrumentableNode) context.getInstrumentedNode()).getNodeObject(), key);
    } catch (Exception e) {
        return null;
    }
    return result;
}
 
Example #21
Source File: InitialRootFactory.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@Override
public BaseEventHandlerNode create(EventContext context) {
    return new FunctionRootEventHandler(context) {
        @Node.Child private InteropLibrary postDispatch = (post == null) ? null : createDispatchNode();

        @Override
        public int getPriority() {
            return -1;
        }

        @Override
        public BaseEventHandlerNode wantsToUpdateHandler() {
            // remove after initial execution
            return null;
        }

        @Override
        public void executePre(VirtualFrame frame, Object[] inputs) throws InteropException {
            checkForSymbolicLocation(context.getInstrumentedNode(), getArguments(frame));

            if (post == null) {
                return;
            }

            Source source = getSource();
            if (source == null) {
                return;
            }

            if (isNewSource(source)) {
                wrappedDispatchExecution(this, postDispatch, post,
                                SourceMapping.getJSObjectForSource(source), // arg 1: source
                                                                            // object
                                source.getCharacters().toString()); // arg 2: source code
            }
        }
    };
}
 
Example #22
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 5 votes vote down vote up
@ExportMessage
protected int asInt(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) throws UnsupportedMessageException {
    if (isNumber()) {
        return lib.asInt(wrappedObject);
    } else {
        throw UnsupportedMessageException.create();
    }
}
 
Example #23
Source File: NodeProfJalangi.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
@TruffleBoundary
private static Object getProperty(TruffleObject cb, String prop) {
    Object result = null;
    try {
        result = InteropLibrary.getFactory().getUncached().readMember(cb, prop);
    } catch (UnknownIdentifierException | UnsupportedMessageException e) {
        // undefined property is expected
    }
    if (Undefined.instance == result || Null.instance == result) {
        result = null;
    }
    return result;
}
 
Example #24
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 #25
Source File: CUBLASRegistry.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void checkCUBLASReturnCode(Object result, String... function) {
    CompilerAsserts.neverPartOfCompilation();
    int returnCode;
    try {
        returnCode = InteropLibrary.getFactory().getUncached().asInt(result);
    } catch (UnsupportedMessageException e) {
        throw new GrCUDAInternalException("expected return code as Integer object in " + function + ", got " + result.getClass().getName());
    }
    if (returnCode != 0) {
        throw new GrCUDAException(returnCode, cublasReturnCodeToString(returnCode), function);
    }
}
 
Example #26
Source File: CUBLASRegistry.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void cuBLASShutdown() {
    CompilerAsserts.neverPartOfCompilation();
    if (cublasHandle != null) {
        try {
            Object result = InteropLibrary.getFactory().getUncached().execute(cublasDestroyFunction, cublasHandle);
            checkCUBLASReturnCode(result, CUBLAS_CUBLASDESTROY.getName());
            cublasHandle = null;
        } catch (InteropException e) {
            throw new GrCUDAInternalException(e);
        }
    }
}
 
Example #27
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 #28
Source File: DeviceArrayCopyFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static long extractPointer(Object valueObj, String argumentName, InteropLibrary access) throws UnsupportedTypeException {
    try {
        if (access.isPointer(valueObj)) {
            return access.asPointer(valueObj);
        }
        return access.asLong(valueObj);
    } catch (UnsupportedMessageException e) {
        CompilerDirectives.transferToInterpreter();
        throw UnsupportedTypeException.create(new Object[]{valueObj}, "integer expected for " + argumentName);
    }
}
 
Example #29
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 #30
Source File: AbstractOSProcessPlugin.java    From trufflesqueak with MIT License 5 votes vote down vote up
protected final long getValue(final InteropLibrary lib, final long id) {
    try {
        return (int) lib.execute(sysCallObject, (int) id);
    } catch (final UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
        throw PrimitiveFailed.andTransferToInterpreterWithError(e);
    }
}