Search in sources :

Example 16 with Method

use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.

the class JniEnv method CallStaticCharMethodVarargs.

@JniImpl
public char CallStaticCharMethodVarargs(@JavaType(Class.class) StaticObject clazz, @Handle(Method.class) long methodId, @Pointer TruffleObject varargsPtr) {
    Method method = methodIds.getObject(methodId);
    assert method.isStatic();
    assert (clazz.getMirrorKlass()) == method.getDeclaringKlass();
    Object result = method.invokeDirect(null, popVarArgs(varargsPtr, method.getParsedSignature()));
    return getMeta().asChar(result, true);
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Method(com.oracle.truffle.espresso.impl.Method)

Example 17 with Method

use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.

the class LivenessAnalysis method analyze.

@SuppressWarnings("try")
public static LivenessAnalysis analyze(Method.MethodVersion methodVersion) {
    EspressoContext context = methodVersion.getMethod().getContext();
    if (!enableLivenessAnalysis(context, methodVersion)) {
        return NO_ANALYSIS;
    }
    Method method = methodVersion.getMethod();
    TimerCollection scope = method.getContext().getTimers();
    try (DebugCloseable liveness = LIVENESS_TIMER.scope(scope)) {
        Graph<? extends LinkedBlock> graph;
        try (DebugCloseable builder = BUILDER_TIMER.scope(scope)) {
            graph = GraphBuilder.build(method);
        }
        // Transform the graph into a more manageable graph consisting of only the history of
        // load/stores.
        LoadStoreFinder loadStoreClosure;
        try (DebugCloseable loadStore = LOADSTORE_TIMER.scope(scope)) {
            loadStoreClosure = new LoadStoreFinder(graph, method);
            loadStoreClosure.analyze();
        }
        // Computes the entry/end live sets for each variable for each block.
        BlockBoundaryFinder blockBoundaryFinder;
        try (DebugCloseable boundary = STATE_TIMER.scope(scope)) {
            blockBoundaryFinder = new BlockBoundaryFinder(methodVersion, loadStoreClosure.result());
            DepthFirstBlockIterator.analyze(method, graph, blockBoundaryFinder);
        }
        try (DebugCloseable propagation = PROPAGATE_TIMER.scope(scope)) {
            // Forces loop ends to inherit the loop entry state, and propagates the changes.
            LoopPropagatorClosure loopPropagation = new LoopPropagatorClosure(graph, blockBoundaryFinder.result());
            while (loopPropagation.process(graph)) {
            /*
                     * This loop should iterate at MOST exactly the maximum number of nested loops
                     * in the method.
                     *
                     * The reasoning is the following:
                     *
                     * - The only reason a new iteration is required is when a loop entry's state
                     * gets modified by the previous iteration.
                     *
                     * - This can happen only if a new live variable gets propagated from an outer
                     * loop.
                     *
                     * - Which means that we do not need to re-propagate the state of the outermost
                     * loop.
                     */
            }
        }
        // frees as early as possible each dead local.
        try (DebugCloseable actionFinder = ACTION_TIMER.scope(scope)) {
            Builder builder = new Builder(graph, methodVersion, blockBoundaryFinder.result());
            builder.build();
            return new LivenessAnalysis(builder.actions, builder.edge, builder.onStart);
        }
    }
}
Also used : TimerCollection(com.oracle.truffle.espresso.perf.TimerCollection) GraphBuilder(com.oracle.truffle.espresso.analysis.GraphBuilder) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Method(com.oracle.truffle.espresso.impl.Method) DebugCloseable(com.oracle.truffle.espresso.perf.DebugCloseable)

Example 18 with Method

use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.

the class VM method JVM_DoPrivileged.

@VmImpl(isJni = true)
@SuppressWarnings("unused")
@JavaType(Object.class)
public StaticObject JVM_DoPrivileged(@JavaType(Class.class) StaticObject cls, /* PrivilegedAction or PrivilegedActionException */
@JavaType(Object.class) StaticObject action, @JavaType(AccessControlContext.class) StaticObject context, boolean wrapException, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
    if (StaticObject.isNull(action)) {
        profiler.profile(0);
        throw meta.throwNullPointerException();
    }
    FrameInstance callerFrame = getCallerFrame(1, false, meta);
    assert callerFrame != null : "No caller ?";
    Klass caller = getMethodFromFrame(callerFrame).getDeclaringKlass();
    StaticObject acc = context;
    if (!StaticObject.isNull(context)) {
        if (!isAuthorized(context, caller)) {
            acc = createDummyACC();
        }
    }
    Method run = action.getKlass().lookupMethod(Name.run, Signature.Object);
    if (run == null || !run.isPublic() || run.isStatic()) {
        profiler.profile(1);
        throw meta.throwException(meta.java_lang_InternalError);
    }
    // Prepare the privileged stack
    PrivilegedStack stack = getPrivilegedStack();
    stack.push(callerFrame, acc, caller);
    // Execute the action.
    StaticObject result;
    try {
        result = (StaticObject) run.invokeDirect(action);
    } catch (EspressoException e) {
        profiler.profile(2);
        if (meta.java_lang_Exception.isAssignableFrom(e.getGuestException().getKlass()) && !meta.java_lang_RuntimeException.isAssignableFrom(e.getGuestException().getKlass())) {
            profiler.profile(3);
            StaticObject wrapper = meta.java_security_PrivilegedActionException.allocateInstance();
            getMeta().java_security_PrivilegedActionException_init_Exception.invokeDirect(wrapper, e.getGuestException());
            throw meta.throwException(wrapper);
        }
        profiler.profile(4);
        throw e;
    } finally {
        stack.pop();
    }
    return result;
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 19 with Method

use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.

the class VM method JVM_InitProperties.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Properties.class)
public StaticObject JVM_InitProperties(@JavaType(Properties.class) StaticObject properties) {
    Map<String, String> props = buildPropertiesMap();
    Method setProperty = properties.getKlass().lookupMethod(Name.setProperty, Signature.Object_String_String);
    for (Map.Entry<String, String> entry : props.entrySet()) {
        setProperty.invokeWithConversions(properties, entry.getKey(), entry.getValue());
    }
    return properties;
}
Also used : Method(com.oracle.truffle.espresso.impl.Method) Map(java.util.Map) EconomicMap(org.graalvm.collections.EconomicMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 20 with Method

use of com.oracle.truffle.espresso.impl.Method in project graal by oracle.

the class VM method DetachCurrentThread.

@VmImpl
@TruffleBoundary
public int DetachCurrentThread(@Inject EspressoContext context) {
    StaticObject currentThread = context.getCurrentThread();
    if (currentThread == null) {
        return JNI_OK;
    }
    getLogger().fine(() -> {
        String guestName = getThreadAccess().getThreadName(currentThread);
        return "DetachCurrentThread: " + guestName;
    });
    // HotSpot will wait forever if the current VM this thread was attached to has exited
    // Should we reproduce this behaviour?
    Method lastJavaMethod = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Method>() {

        @Override
        public Method visitFrame(FrameInstance frameInstance) {
            Method method = getMethodFromFrame(frameInstance);
            if (method != null && method.getContext() == context) {
                return method;
            }
            return null;
        }
    });
    if (lastJavaMethod != null) {
        // this thread is executing
        getLogger().warning(() -> {
            String guestName = getThreadAccess().getThreadName(currentThread);
            return "DetachCurrentThread called while thread is still executing Java code (" + guestName + ")";
        });
        return JNI_ERR;
    }
    StaticObject pendingException = jniEnv.getPendingException();
    jniEnv.clearPendingException();
    Meta meta = context.getMeta();
    try {
        if (pendingException != null) {
            meta.java_lang_Thread_dispatchUncaughtException.invokeDirect(currentThread, pendingException);
        }
        getThreadAccess().terminate(currentThread);
    } catch (EspressoException e) {
        try {
            StaticObject ex = e.getGuestException();
            String exception = ex.getKlass().getExternalName();
            String threadName = getThreadAccess().getThreadName(currentThread);
            context.getLogger().warning(String.format("Exception: %s thrown while terminating thread \"%s\"", exception, threadName));
            Method printStackTrace = ex.getKlass().lookupMethod(Name.printStackTrace, Signature._void);
            printStackTrace.invokeDirect(ex);
        } catch (EspressoException ee) {
            String exception = ee.getGuestException().getKlass().getExternalName();
            context.getLogger().warning(String.format("Exception: %s thrown while trying to print stack trace", exception));
        } catch (EspressoExitException ee) {
        // ignore
        }
    } catch (EspressoExitException e) {
    // ignore
    } catch (Throwable t) {
        context.getLogger().severe("Host exception thrown while trying to terminate thread");
        t.printStackTrace();
    }
    return JNI_OK;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) EspressoExitException(com.oracle.truffle.espresso.runtime.EspressoExitException) Method(com.oracle.truffle.espresso.impl.Method) FrameInstance(com.oracle.truffle.api.frame.FrameInstance) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

Method (com.oracle.truffle.espresso.impl.Method)91 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)57 Meta (com.oracle.truffle.espresso.meta.Meta)27 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)22 Klass (com.oracle.truffle.espresso.impl.Klass)19 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)19 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ExportMessage (com.oracle.truffle.api.library.ExportMessage)12 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)9 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)8 ArityException (com.oracle.truffle.api.interop.ArityException)8 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)8 ArrayList (java.util.ArrayList)8 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)7 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)5 Field (com.oracle.truffle.espresso.impl.Field)4 MethodParametersAttribute (com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute)3 Signature (com.oracle.truffle.espresso.descriptors.Symbol.Signature)3