public static CallSite bootstrap(MethodHandles.Lookup callerLookup, String name, MethodType type, long bindingId) { ClassLoader classLoader = callerLookup.lookupClass().getClassLoader(); checkArgument(classLoader instanceof DynamicClassLoader, "Expected %s's classloader to be of type %s", callerLookup.lookupClass().getName(), DynamicClassLoader.class.getName()); DynamicClassLoader dynamicClassLoader = (DynamicClassLoader) classLoader; MethodHandle target = dynamicClassLoader.getCallSiteBindings().get(bindingId); checkArgument(target != null, "Binding %s for function %s%s not found", bindingId, name, type.parameterList()); return new ConstantCallSite(target); } }
private void setupInvokeDynamic(Class<?> generatedClass) throws NoSuchMethodException, IllegalAccessException{ MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodType funType = MethodType.methodType(Object.class, Frame.class); MethodType rtType = MethodType.methodType(Object.class, RVMonJVM.class, Frame.class); for (Map.Entry<String, Integer> e : functionMap.entrySet()) { String fname = e.getKey(); Integer findex = e.getValue(); Function func = functionStore[findex]; String methodName = BytecodeGenerator.rvm2jvmName(fname); MethodHandle mh = lookup.findVirtual(generatedClass, methodName, funType); func.handle = new ConstantCallSite(mh.asType(rtType)).dynamicInvoker(); } }
@GenerateMicroBenchmark public long testInvokeDynamic() throws Throwable { Container container = new Container(0,0,(short)0); mhsa.dynamicInvoker().invoke(container, 100L); mhsb.dynamicInvoker().invoke(container, 200); mhsc.dynamicInvoker().invoke(container, (short)300L); return (Long)mhga.dynamicInvoker().invoke(container) + (Integer)mhgb.dynamicInvoker().invoke(container) + (Short)mhgc.dynamicInvoker().invoke(container); }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> long reduceBy( final long[] array, T object ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object ); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object); MethodHandle methodHandle = callSite.dynamicInvoker(); try { long sum = 0; for ( long v : array ) { sum = (long) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object); } }
@SuppressWarnings("UnusedDeclaration") public static CallSite bootstrapIntrinsic(MethodHandles.Lookup caller, String name, MethodType type, String callee) throws IllegalAccessException { MethodHandle mh = getMethodHandle(callee, name, type); if (mh == null) { throw new IllegalArgumentException("Could not find intrinsic for " + callee + ":" + name); } return new ConstantCallSite(mh.asType(type)); }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> double reduceBy( final double[] array, T object, String methodName ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object, methodName); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object, methodName); MethodHandle methodHandle = callSite.dynamicInvoker(); try { double sum = 0; for ( double v : array ) { sum = (double) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object, methodName); } }
/** * @since 2.5.0 */ public static CallSite staticArrayAccess(MethodHandles.Lookup lookup, String name, MethodType type) { if (type.parameterCount()==2) { return new ConstantCallSite(IndyArrayAccess.arrayGet(type)); } else { return new ConstantCallSite(IndyArrayAccess.arraySet(type)); } } }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> long reduceBy( final long[] array, T object, String methodName ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object, methodName); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object, methodName); MethodHandle methodHandle = callSite.dynamicInvoker(); try { long sum = 0; for ( long v : array ) { sum = (long) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object, methodName); } }
@Override public CallSite invokeStatic(Lookup caller, String methodName, MethodType signature, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findStatic(owner, methodName, signature)); } catch (ReflectiveOperationException e) { return null; } }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> long reduceBy( final long[] array, T object, String methodName ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object, methodName); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object, methodName); MethodHandle methodHandle = callSite.dynamicInvoker(); try { long sum = 0; for ( long v : array ) { sum = (long) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object, methodName); } }
@Override public CallSite getField(Lookup caller, String fieldName, Class<?> fieldType, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findGetter(owner, fieldName, fieldType)); } catch (ReflectiveOperationException e) { return null; } }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> double reduceBy( final double[] array, T object ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object ); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object); MethodHandle methodHandle = callSite.dynamicInvoker(); try { double sum = 0; for ( double v : array ) { sum = (double) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object); } }
@Override public CallSite getStatic(Lookup caller, String fieldName, Class<?> fieldType, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findStaticGetter(owner, fieldName, fieldType)); } catch (ReflectiveOperationException e) { return null; } }
/** * Reduce by functional support for int arrays. * @param array array of items to reduce by * @param object object that contains the reduce by function * @param <T> the type of object * @return the final reduction */ public static <T> double reduceBy( final float[] array, T object ) { if (object.getClass().isAnonymousClass()) { return reduceByR(array, object ); } try { ConstantCallSite callSite = Invoker.invokeReducerLongIntReturnLongMethodHandle(object); MethodHandle methodHandle = callSite.dynamicInvoker(); try { double sum = 0; for ( float v : array ) { sum = (double) methodHandle.invokeExact( sum, v ); } return sum; } catch (Throwable throwable) { return handle(Long.class, throwable, "Unable to perform reduceBy"); } } catch (Exception ex) { return reduceByR(array, object); } }
@Override public CallSite invokeVirtual(Lookup caller, String methodName, MethodType signature, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findVirtual(owner, methodName, signature)); } catch (ReflectiveOperationException e) { return null; } }
@Override public CallSite invokeConstructor(Lookup caller, MethodType signature, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findConstructor(owner, signature)); } catch (ReflectiveOperationException e) { return null; } }
@Override public CallSite invokeInterface(Lookup caller, String methodName, MethodType signature, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findVirtual(owner, methodName, signature)); } catch (ReflectiveOperationException e) { return null; } }
@Override public CallSite putField(Lookup caller, String fieldName, Class<?> fieldType, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findSetter(owner, fieldName, fieldType)); } catch (ReflectiveOperationException e) { return null; } }
@Override public CallSite putStatic(Lookup caller, String fieldName, Class<?> fieldType, Class<?> owner) throws ReflectiveOperationException { try { return new ConstantCallSite(caller.findStaticSetter(owner, fieldName, fieldType)); } catch (ReflectiveOperationException e) { return null; } } }
public static CallSite bootstrap(MethodHandles.Lookup caller, String name, MethodType type) { assert "methodInfo".equals(name); // Empty object as a placeholder. return new ConstantCallSite(MethodHandles.constant(Object.class, new Object())); }