Search in sources :

Example 96 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class Target_java_lang_System method doArrayCopy.

/*-
     * Order of throws (see JCK api/java_lang/System/index.html#Arraycopy):
     *
     *  A - NullPointerException
     *      if either src or dst is null.
     *  B - ArrayStoreException
     *      if an element in the src array could not be stored into the dest array because of:
     *          1 - The src argument refers to an object that is not an array.
     *          2 - The dst argument refers to an object that is not an array.
     *          3 - The src argument and dst argument refer to arrays whose component types are
     *          different primitive types.
     *          4 - The src argument refers to an array with a primitive component type and the
     *          dst argument refers to an array with a reference component type.
     *          5 - The src argument refers to an array with a reference component type and the
     *          dst argument refers to an array with a primitive component type.
     *  C - IndexOutOfBoundsException
     *      if copying would cause access of data outside array bounds.
     *  D - ArrayStoreException
     *      if an element in the src array could not be stored into the dest array because of a type mismatch
     */
private static void doArrayCopy(@JavaType(Object.class) StaticObject src, int srcPos, @JavaType(Object.class) StaticObject dest, int destPos, int length, Meta meta, SubstitutionProfiler profiler) {
    if (StaticObject.isNull(src) || StaticObject.isNull(dest)) {
        throw throwNullPointerEx(meta, profiler);
    }
    if (src.isForeignObject() || dest.isForeignObject()) {
        // TODO: handle foreign arrays efficiently.
        profiler.profile(FOREIGN_PROFILE);
        handleForeignArray(src.isForeignObject() ? src.rawForeignObject() : src, srcPos, dest.isForeignObject() ? dest.rawForeignObject() : dest, destPos, length, ((ArrayKlass) dest.getKlass()).getComponentType(), meta, profiler);
        return;
    }
    // Mimics hotspot implementation.
    /*
         * First, check that both given objects are arrays. This is done before bypassing checks and
         * bounds checks (see JCK api/java_lang/System/index.html#Arraycopy: System2015)
         */
    profiler.profile(GUEST_PROFILE);
    if (!src.isArray() || !dest.isArray()) {
        throw throwArrayStoreEx(meta, profiler);
    }
    // If both arrays are the same, a lot of checks can be bypassed
    if (src == dest) {
        profiler.profile(SAME_ARRAY_PROFILE);
        /*
             * Let host VM's arrayCopy implementation handle bounds. Guest type checking is useless
             * here due to both array being the same.
             */
        System.arraycopy(src.unwrap(), srcPos, dest.unwrap(), destPos, length);
        return;
    }
    profiler.profile(DIFF_ARRAY_PROFILE);
    Klass destType = ((ArrayKlass) dest.getKlass()).getComponentType();
    Klass srcType = ((ArrayKlass) src.getKlass()).getComponentType();
    if (destType.isPrimitive() || srcType.isPrimitive()) {
        // One of the two arrays is a primitive array.
        profiler.profile(DIFF_PRIMITIVE_ARRAYS_PROFILE);
        if (srcType != destType) {
            throw throwArrayStoreEx(meta, profiler);
        }
        /*
             * Let host VM's arrayCopy implementation handle bounds. Guest type checking is useless
             * here due to one of the two arrays being primitives.
             */
        System.arraycopy(src.unwrap(), srcPos, dest.unwrap(), destPos, length);
        return;
    }
    // Both arrays are reference arrays.
    profiler.profile(DIFF_REFERENCE_ARRAYS_PROFILE);
    /*
         * Perform bounds checks BEFORE checking for length == 0. (see JCK
         * api/java_lang/System/index.html#Arraycopy: System1001)
         */
    boundsCheck(meta, src.length(), srcPos, dest.length(), destPos, length, profiler);
    if (length == 0) {
        profiler.profile(ZERO_LENGTH_PROFILE);
        // All checks have been done, we can take the shortcut.
        return;
    }
    if (destType.isAssignableFrom(srcType)) {
        // We have guarantee we can copy, as all elements in src conform to dest
        // type.
        profiler.profile(SUBTYPE_ARRAYS_PROFILE);
        System.arraycopy(src.unwrap(), srcPos, dest.unwrap(), destPos, length);
        return;
    }
    /*
         * Slow path (manual copy) (/ex: copying an Object[] to a String[]) requires individual type
         * checks. Should rarely happen ( < 1% of cases).
         *
         * Use cases:
         *
         * - System startup.
         *
         * - MethodHandle and CallSite linking.
         */
    profiler.profile(UNRELATED_TYPE_ARRAYS_PROFILE);
    StaticObject[] s = src.unwrap();
    StaticObject[] d = dest.unwrap();
    for (int i = 0; i < length; i++) {
        StaticObject cpy = s[i + srcPos];
        if (!StaticObject.isNull(cpy) && !destType.isAssignableFrom(cpy.getKlass())) {
            throw throwArrayStoreEx(meta, profiler);
        }
        d[destPos + i] = cpy;
    }
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass)

Example 97 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class Target_java_lang_Thread method dumpThreads.

@Substitution
@JavaType(StackTraceElement[][].class)
public static StaticObject dumpThreads(@JavaType(Thread[].class) StaticObject threads, @Inject Meta meta) {
    if (StaticObject.isNull(threads)) {
        throw meta.throwNullPointerException();
    }
    if (threads.length() == 0) {
        throw meta.throwException(meta.java_lang_IllegalArgumentException);
    }
    StaticObject trace = StaticObject.createArray(meta.java_lang_StackTraceElement.array(), StaticObject.EMPTY_ARRAY);
    StaticObject[] toWrap = new StaticObject[threads.length()];
    Arrays.fill(toWrap, trace);
    return StaticObject.createArray(meta.java_lang_StackTraceElement.array().array(), toWrap);
}
Also used : StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 98 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class Target_java_lang_invoke_MethodHandleNatives method getMemberVMInfo.

@Substitution
@JavaType(Object.class)
public static StaticObject getMemberVMInfo(@JavaType(internalName = "Ljava/lang/invoke/MemberName;") StaticObject self, @Inject Meta meta) {
    Object vmtarget = meta.HIDDEN_VMTARGET.getHiddenObject(self);
    Object vmindex = meta.HIDDEN_VMINDEX.getHiddenObject(self);
    StaticObject[] result = new StaticObject[2];
    if (vmindex == null) {
        // vmindex is not used in espresso. Spoof it so java is still happy.
        result[0] = meta.boxLong(-2_000_000);
    } else {
        result[0] = meta.boxLong((long) vmindex);
    }
    if (vmtarget == null) {
        result[1] = StaticObject.NULL;
    } else if (vmtarget instanceof Klass) {
        result[1] = ((Klass) vmtarget).mirror();
    } else {
        result[1] = self;
    }
    return StaticObject.createArray(meta.java_lang_Object_array, result);
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject)

Example 99 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class Target_java_lang_invoke_MethodHandleNatives method getMembers.

// TODO(garcia) verifyConstants
@Substitution
public static int getMembers(@JavaType(Class.class) StaticObject defc, @JavaType(String.class) StaticObject matchName, @JavaType(String.class) StaticObject matchSig, int matchFlags, @JavaType(Class.class) StaticObject originalCaller, int skip, @JavaType(internalName = "[Ljava/lang/invoke/MemberName;") StaticObject resultsArr, @Inject Meta meta) {
    if (StaticObject.isNull(defc) || StaticObject.isNull(resultsArr)) {
        return -1;
    }
    EspressoContext context = meta.getContext();
    StaticObject[] results = resultsArr.unwrap();
    Symbol<Name> name = null;
    if (!StaticObject.isNull(matchName)) {
        name = context.getNames().lookup(meta.toHostString(matchName));
        if (name == null) {
            return 0;
        }
    }
    String sig = meta.toHostString(matchSig);
    if (sig == null) {
        return 0;
    }
    Klass caller = null;
    if (!StaticObject.isNull(originalCaller)) {
        caller = originalCaller.getMirrorKlass();
        if (caller == null) {
            return -1;
        }
    }
    return findMemberNames(defc.getMirrorKlass(), name, sig, matchFlags, caller, skip, results);
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) EspressoContext(com.oracle.truffle.espresso.runtime.EspressoContext) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 100 with StaticObject

use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.

the class EspressoInterop method asInstant.

@ExportMessage
@TruffleBoundary
static Instant asInstant(StaticObject receiver, @CachedLibrary("receiver") InteropLibrary receiverLibrary, @Shared("error") @Cached BranchProfile error) throws UnsupportedMessageException {
    receiver.checkNotForeign();
    if (receiverLibrary.isInstant(receiver)) {
        StaticObject instant;
        Meta meta = receiver.getKlass().getMeta();
        if (instanceOf(receiver, meta.java_time_ZonedDateTime)) {
            instant = (StaticObject) meta.java_time_ZonedDateTime_toInstant.invokeDirect(receiver);
        } else if (instanceOf(receiver, meta.java_util_Date)) {
            int index = meta.java_util_Date_toInstant.getVTableIndex();
            Method virtualToInstant = receiver.getKlass().vtableLookup(index);
            instant = (StaticObject) virtualToInstant.invokeDirect(receiver);
        } else {
            instant = receiver;
        }
        assert instanceOf(instant, meta.java_time_Instant);
        long seconds = (long) meta.java_time_Instant_seconds.get(instant);
        int nanos = (int) meta.java_time_Instant_nanos.get(instant);
        return Instant.ofEpochSecond(seconds, nanos);
    }
    error.enter();
    throw UnsupportedMessageException.create();
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) Method(com.oracle.truffle.espresso.impl.Method) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Aggregations

StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)199 Method (com.oracle.truffle.espresso.impl.Method)57 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)44 Klass (com.oracle.truffle.espresso.impl.Klass)32 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)32 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)30 Meta (com.oracle.truffle.espresso.meta.Meta)29 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)26 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)23 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)17 ArrayList (java.util.ArrayList)13 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)9 Symbol (com.oracle.truffle.espresso.descriptors.Symbol)8 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)7 ExportMessage (com.oracle.truffle.api.library.ExportMessage)7 BytecodeNode (com.oracle.truffle.espresso.nodes.BytecodeNode)7 TruffleSafepoint (com.oracle.truffle.api.TruffleSafepoint)6 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)6 EspressoContext (com.oracle.truffle.espresso.runtime.EspressoContext)6 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)5