< prev index next >

jdk/src/java.base/share/classes/jdk/experimental/value/ValueType.java

Print this page
rev 14083 : vwithfield class/field access

*** 39,58 **** import java.util.stream.Stream; import jdk.experimental.value.ValueType.ValueHandleKind.ValueHandleKey; import jdk.experimental.bytecode.MacroCodeBuilder.CondKind; import jdk.experimental.bytecode.TypeTag; - import jdk.internal.misc.Unsafe; import sun.invoke.util.BytecodeDescriptor; import sun.invoke.util.Wrapper; import valhalla.shady.MinimalValueTypes_1_0; // Rough place holder just now... public class ValueType<T> { - static final Unsafe UNSAFE = Unsafe.getUnsafe(); - enum ValueHandleKind { BOX, UNBOX, DEFAULT, EQ, --- 39,55 ----
*** 195,205 **** ValueHandleKey key = ValueHandleKind.DEFAULT.key(); MethodHandle result = handleMap.get(key); if (result == null) { result = MethodHandleBuilder.loadCode(lookup, "default" + sourceClass().getName(), MethodType.methodType(valueClass()), C -> { ! C.new_(boxClass()).vunbox(valueClass()).vreturn(); }); handleMap.put(key, result); } return result; } --- 192,202 ---- ValueHandleKey key = ValueHandleKind.DEFAULT.key(); MethodHandle result = handleMap.get(key); if (result == null) { result = MethodHandleBuilder.loadCode(lookup, "default" + sourceClass().getName(), MethodType.methodType(valueClass()), C -> { ! C.vdefault(valueClass()).vreturn(); }); handleMap.put(key, result); } return result; }
*** 255,297 **** handleMap.put(key, result); } return result; } - //Todo: when 'vwithfield' is ready, this handle could be greatly simplified public MethodHandle findWither(String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { ValueHandleKey key = ValueHandleKind.WITHER.key(name); MethodHandle result = handleMap.get(key); if (result == null) { ! Field field = boxClass().getDeclaredField(name); ! if (field == null || !field.getType().equals(type) || ! (field.getModifiers() & Modifier.STATIC) != 0) { ! throw new NoSuchFieldException(name); ! } ! Class<?> erasedType = type.isPrimitive() ? ! type : Object.class; ! Method unsafeMethod = Stream.of(UNSAFE.getClass().getDeclaredMethods()) ! .filter(m -> m.getName().startsWith("put") && ! Arrays.asList(m.getParameterTypes()).equals(Arrays.asList(Object.class, long.class, erasedType))) ! .findFirst().get(); ! long fieldOffset = UNSAFE.objectFieldOffset(field); ! result = MethodHandleBuilder.loadCode(lookup, "wither" + sourceClass().getName() + ":" + name, MethodType.methodType(valueClass(), UNSAFE.getClass(), valueClass(), type), ! C -> { ! C.withLocal("boxedVal", BytecodeDescriptor.unparse(boxClass())) ! .load(1) ! .vbox(boxClass()) ! .store("boxedVal") ! .load(0) ! .load("boxedVal") ! .const_(fieldOffset) ! .load(2); ! MethodType unsafeMT = MethodType.methodType(unsafeMethod.getReturnType(), unsafeMethod.getParameterTypes()); ! C.invokevirtual(UNSAFE.getClass(), unsafeMethod.getName(), BytecodeDescriptor.unparse(unsafeMT), false) ! .load("boxedVal") ! .vunbox(valueClass()) ! .vreturn(); ! }).bindTo(UNSAFE); handleMap.put(key, result); } return result; } --- 252,268 ---- handleMap.put(key, result); } return result; } public MethodHandle findWither(String name, Class<?> type) throws NoSuchFieldException, IllegalAccessException { ValueHandleKey key = ValueHandleKind.WITHER.key(name); MethodHandle result = handleMap.get(key); if (result == null) { ! result = MethodHandleBuilder.loadCode(lookup, "wither" + sourceClass().getName() + ":" + name, ! MethodType.methodType(valueClass(), valueClass(), type), ! C -> { C.load(0).load(1).vwithfield(valueClass(), name, BytecodeDescriptor.unparse(type)).vreturn(); }); handleMap.put(key, result); } return result; }
< prev index next >