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

The following examples show how to use com.oracle.truffle.api.interop.TruffleObject. 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: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public CUDARuntime(GrCUDAContext context, Env env) {
    this.context = context;
    try {
        TruffleObject libcudart = (TruffleObject) env.parseInternal(
                        Source.newBuilder("nfi", "load " + "lib" + CUDA_RUNTIME_LIBRARY_NAME + ".so", "cudaruntime").build()).call();
        TruffleObject libcuda = (TruffleObject) env.parseInternal(
                        Source.newBuilder("nfi", "load " + "lib" + CUDA_LIBRARY_NAME + ".so", "cuda").build()).call();
        TruffleObject libnvrtc = (TruffleObject) env.parseInternal(
                        Source.newBuilder("nfi", "load " + "lib" + NVRTC_LIBRARY_NAME + ".so", "nvrtc").build()).call();
        loadedLibraries.put(CUDA_RUNTIME_LIBRARY_NAME, libcudart);
        loadedLibraries.put(CUDA_LIBRARY_NAME, libcuda);
        loadedLibraries.put(NVRTC_LIBRARY_NAME, libnvrtc);
    } catch (UnsatisfiedLinkError e) {
        throw new GrCUDAException(e.getMessage());
    }

    nvrtc = new NVRuntimeCompiler(this);
    context.addDisposable(this::shutdown);
}
 
Example #2
Source File: HashemLanguage.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Override
protected boolean isObjectOfLanguage(Object object) {
    if (!(object instanceof TruffleObject)) {
        return false;
    } else if (object instanceof HashemBigNumber || object instanceof HashemBebin || object instanceof HashemPooch) {
        return true;
    } else if (HashemContext.isSLObject(object)) {
        return true;
    } else {
        return false;
    }
}
 
Example #3
Source File: HashemContext.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
public static Object fromForeignValue(Object a) {
    if (a instanceof Long || a instanceof HashemBigNumber || a instanceof String || a instanceof Boolean) {
        return a;
    } else if (a instanceof Character) {
        return String.valueOf(a);
    } else if (a instanceof Number) {
        return fromForeignNumber(a);
    } else if (a instanceof TruffleObject) {
        return a;
    } else if (a instanceof HashemContext) {
        return a;
    }
    CompilerDirectives.transferToInterpreter();
    throw new IllegalStateException(a + " is not a Truffle value");
}
 
Example #4
Source File: HashemInstrumentTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
private static Object read(TruffleObject vars, String key) {
    try {
        return INTEROP.readMember(vars, key);
    } catch (UnknownIdentifierException | UnsupportedMessageException e) {
        throw new AssertionError(e);
    }
}
 
Example #5
Source File: NodeProfJalangi.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
/**
 * called in ChainedAnalysesNoCheck.js when all callbacks are defined
 */
@TruffleBoundary
public void onReady(Object analysis, TruffleObject configObj) {
    if (jalangiAnalyses.containsKey(analysis)) {
        jalangiAnalyses.get(analysis).onReady();
    }
    analysisReady(parseFilterConfig(configObj));
}
 
Example #6
Source File: HashemInstrumentTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
private static int keySize(TruffleObject vars) {
    try {
        return (int) INTEROP.getArraySize(INTEROP.getMembers(vars));
    } catch (UnsupportedMessageException e) {
        throw new AssertionError(e);
    }
}
 
Example #7
Source File: HashemInstrumentTest.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
private static void checkVars(TruffleObject vars, Object... expected) {
    for (int i = 0; i < expected.length; i += 2) {
        String name = (String) expected[i];
        Object value = expected[i + 1];
        assertTrue(name, contains(vars, name));
        if (value != null) {
            assertEquals(name, value, read(vars, name));
        } else {
            assertTrue(isNull((TruffleObject) read(vars, name)));
        }
    }
    assertEquals(expected.length / 2, keySize(vars));
}
 
Example #8
Source File: GrCUDALanguage.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected boolean isObjectOfLanguage(Object object) {
    if (!(object instanceof TruffleObject)) {
        return false;
    }
    TruffleObject truffleObject = (TruffleObject) object;
    return truffleObject instanceof DeviceArray;
}
 
Example #9
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 #10
Source File: NodeProfJalangi.java    From nodeprof.js with Apache License 2.0 5 votes vote down vote up
private static AnalysisFilterBase parseFilterConfig(TruffleObject configObj) {
    AnalysisFilterBase result;

    if (JSFunction.isJSFunction(configObj)) {
        result = new AnalysisFilterJS(configObj);
        Logger.debug("JS filter installed: " + configObj.toString());
    } else {

        Object internal = getProperty(configObj, "internal");
        boolean instrumentInternal = internal == null ? false : JSRuntime.toBoolean(internal);

        boolean excludeFilter = true;
        String filters;
        Object excludes = getProperty(configObj, "excludes");
        if (excludes == null) {
            Object includes = getProperty(configObj, "includes");
            filters = includes == null ? "" : includes.toString();
            excludeFilter = false;
        } else {
            if (getProperty(configObj, "includes") != null) {
                Logger.error("Filter config must not define 'include' and 'exclude' at the same time (config: " + JSObject.safeToString((DynamicObject) configObj, 3) + ")");
                System.exit(-1);
            }
            filters = excludes.toString();
        }
        List<String> filterList = filters == null ? Collections.emptyList() : Arrays.asList(filters.split(","));
        AnalysisFilterSourceList listFilter;
        // return a filter based on excludeFilter, filterList and global excludes
        if (excludeFilter) {
            listFilter = AnalysisFilterSourceList.makeExcludeFilter(filterList, !instrumentInternal);
            listFilter = AnalysisFilterSourceList.addGlobalExcludes(listFilter);
        } else {
            listFilter = AnalysisFilterSourceList.makeIncludeFilter(filterList, "");
        }
        Logger.debug("Custom source filter: " + listFilter.getDescription());
        result = listFilter;
    }
    return result;
}
 
Example #11
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(limit = "1")
protected static final void doTruffleObject(final TruffleObject object, final int index, final Object value, @CachedLibrary("object") final InteropLibrary lib)
                throws UnsupportedMessageException, InvalidArrayIndexException, UnsupportedTypeException {
    lib.writeArrayElement(object, index, value);
}
 
Example #12
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(limit = "1")
protected static final Object doTruffleObject(final TruffleObject object, final int index, @CachedLibrary("object") final InteropLibrary lib)
                throws UnsupportedMessageException, InvalidArrayIndexException {
    return lib.readArrayElement(object, index);
}
 
Example #13
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 4 votes vote down vote up
@Specialization(limit = "1")
protected static final int doTruffleObject(final TruffleObject object, @CachedLibrary("object") final InteropLibrary lib) throws UnsupportedMessageException {
    return (int) lib.getArraySize(object);
}
 
Example #14
Source File: JavaObjectWrapper.java    From trufflesqueak with MIT License 4 votes vote down vote up
@ExportMessage
@TruffleBoundary
protected boolean hasArrayElements(@Shared("lib") @CachedLibrary(limit = "LIMIT") final InteropLibrary lib) {
    return wrappedObject.getClass().isArray() || wrappedObject instanceof TruffleObject && lib.hasArrayElements(wrappedObject);
}
 
Example #15
Source File: AnalysisFilterJS.java    From nodeprof.js with Apache License 2.0 4 votes vote down vote up
public AnalysisFilterJS(TruffleObject jsPredicateFunc) {
    this.jsPredicateFunc = jsPredicateFunc;
    this.includedSources = new HashMap<>();
    this.excludedSources = new HashSet<>();
}
 
Example #16
Source File: HashemInstrumentTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
private static boolean isNull(TruffleObject vars) {
    return INTEROP.isNull(vars);
}
 
Example #17
Source File: HashemInstrumentTest.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
private static boolean contains(TruffleObject vars, String key) {
    return INTEROP.isMemberExisting(vars, key);
}
 
Example #18
Source File: FunctionsObject.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
public static boolean isInstance(TruffleObject obj) {
    return obj instanceof FunctionsObject;
}
 
Example #19
Source File: HashemBebinRegistry.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
public TruffleObject getFunctionsObject() {
    return functionsObject;
}
 
Example #20
Source File: HashemEqualNode.java    From mr-hashemi with Universal Permissive License v1.0 4 votes vote down vote up
/**
 * Specialization for foreign {@link TruffleObject}s.
 */
@Specialization
protected boolean equal(TruffleObject left, TruffleObject right) {
    return left == right;
}
 
Example #21
Source File: HashemContext.java    From mr-hashemi with Universal Permissive License v1.0 2 votes vote down vote up
/**
 * Returns an object that contains bindings that were exported across all used languages. To
 * read or write from this object the {@link TruffleObject interop} API can be used.
 */
public TruffleObject getPolyglotBindings() {
    return (TruffleObject) env.getPolyglotBindings();
}