Search in sources :

Example 26 with Klass

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

the class StaticObject method getMirrorKlass.

/**
 * Same as {@link #getMirrorKlass()}, but passing a {@code meta} argument allows some constant
 * folding, even if {@code this} is not constant.
 */
public final Klass getMirrorKlass(Meta meta) {
    assert getKlass().getType() == Type.java_lang_Class;
    checkNotForeign();
    Klass result = (Klass) meta.HIDDEN_MIRROR_KLASS.getHiddenObject(this);
    assert result != null : "Uninitialized mirror class";
    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)

Example 27 with Klass

use of com.oracle.truffle.espresso.impl.Klass 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 28 with Klass

use of com.oracle.truffle.espresso.impl.Klass 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 29 with Klass

use of com.oracle.truffle.espresso.impl.Klass 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 30 with Klass

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

the class EspressoInterop method fitsInByte.

@ExportMessage
static boolean fitsInByte(StaticObject receiver) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return false;
    }
    Klass klass = receiver.getKlass();
    if (isAtMostByte(klass)) {
        return true;
    }
    Meta meta = klass.getMeta();
    if (klass == meta.java_lang_Short) {
        short content = meta.java_lang_Short_value.getShort(receiver);
        return (byte) content == content;
    }
    if (klass == meta.java_lang_Integer) {
        int content = meta.java_lang_Integer_value.getInt(receiver);
        return (byte) content == content;
    }
    if (klass == meta.java_lang_Long) {
        long content = meta.java_lang_Long_value.getLong(receiver);
        return (byte) content == content;
    }
    if (klass == meta.java_lang_Float) {
        float content = meta.java_lang_Float_value.getFloat(receiver);
        return (byte) content == content && !isNegativeZero(content);
    }
    if (klass == meta.java_lang_Double) {
        double content = meta.java_lang_Double_value.getDouble(receiver);
        return (byte) content == content && !isNegativeZero(content);
    }
    return false;
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Aggregations

Klass (com.oracle.truffle.espresso.impl.Klass)71 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)54 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)49 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)33 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)24 Meta (com.oracle.truffle.espresso.meta.Meta)21 Method (com.oracle.truffle.espresso.impl.Method)19 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)10 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)10 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)10 ExportMessage (com.oracle.truffle.api.library.ExportMessage)8 Field (com.oracle.truffle.espresso.impl.Field)7 ArrayList (java.util.ArrayList)7 RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)6 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)6 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)5 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)5 NativeType (com.oracle.truffle.espresso.ffi.NativeType)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)3