java.lang.invoke.MutableCallSite Java Examples

The following examples show how to use java.lang.invoke.MutableCallSite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FSClassRegistry.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private static void loadBuiltins(TypeImpl ti, ClassLoader cl, Map<String, JCasClassInfo> type2jcci, ArrayList<MutableCallSite> callSites_toSync) {
    String typeName = ti.getName();
    
    if (BuiltinTypeKinds.creatableBuiltinJCas.contains(typeName) || typeName.equals(CAS.TYPE_NAME_SOFA)) {
      JCasClassInfo jcci = getOrCreateJCasClassInfo(ti, cl, type2jcci, defaultLookup);
      assert jcci != null;
      // done while beginning to commit the staticTsi (before committed flag is set), for builtins  
      updateOrValidateAllCallSitesForJCasClass(jcci.jcasClass, ti, callSites_toSync);
      jcasClassesInfoForBuiltins[ti.getCode()] = jcci;
      
//      Class<?> builtinClass = maybeLoadJCas(ti, cl);
//      assert (builtinClass != null);  // builtin types must be present
//      // copy down to subtypes, if needed, done later
//      int jcasType = Misc.getStaticIntFieldNoInherit(builtinClass, "typeIndexID");
//      JCasClassInfo jcasClassInfo = createJCasClassInfo(builtinClass, ti, jcasType); 
//      jcasClassesInfoForBuiltins[ti.getCode()] = jcasClassInfo; 
    }
    
    for (TypeImpl subType : ti.getDirectSubtypes()) {
      loadBuiltins(subType, cl, type2jcci, callSites_toSync);
    }
  }
 
Example #2
Source File: Bootstrap.java    From es6draft with MIT License 6 votes vote down vote up
private static MethodHandle setCallSiteTarget(MutableCallSite callsite, MethodHandle target, MethodHandle test,
        MethodHandle generic) {
    MethodHandle callSiteTarget;
    if (target != null) {
        target = target.asType(callsite.type());
        if (test != null) {
            MethodHandle fallback = createFallback(callsite, generic);
            callSiteTarget = MethodHandles.guardWithTest(test, target, fallback);
        } else {
            callSiteTarget = target;
        }
    } else {
        callSiteTarget = target = generic;
    }
    callsite.setTarget(callSiteTarget);
    return target;
}
 
Example #3
Source File: CallSiteDepContextTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void testNonBoundCallSite() throws Throwable {
    mcs = new MutableCallSite(LOOKUP.findStatic(T.class, "f1", TYPE));

    // mcs.context == null
    MethodHandle mh = mcs.dynamicInvoker();
    execute(1, mh);

    // mcs.context == cls1
    Class<?> cls1 = UNSAFE.defineAnonymousClass(CallSiteDepContextTest.class, getClassFile("NonBound_1"), null);
    MethodHandle mh1 = LOOKUP.findStatic(cls1, METHOD_NAME, TYPE);

    execute(1, mh1);

    mcs.setTarget(LOOKUP.findStatic(T.class, "f2", TYPE));

    execute(2, mh, mh1);
}
 
Example #4
Source File: IndyInterface.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Core method for indy method selection using runtime types.
 */
public static Object selectMethod(MutableCallSite callSite, Class<?> sender, String methodName, int callID, Boolean safeNavigation, Boolean thisCall, Boolean spreadCall, Object dummyReceiver, Object[] arguments) throws Throwable {
    final MethodHandleWrapper mhw = fallback(callSite, sender, methodName, callID, safeNavigation, thisCall, spreadCall, dummyReceiver, arguments);

    if (callSite instanceof CacheableCallSite) {
        CacheableCallSite cacheableCallSite = (CacheableCallSite) callSite;

        final MethodHandle defaultTarget = cacheableCallSite.getDefaultTarget();
        final long fallbackCount = cacheableCallSite.incrementFallbackCount();
        if ((fallbackCount > INDY_FALLBACK_THRESHOLD) && (cacheableCallSite.getTarget() != defaultTarget)) {
            cacheableCallSite.setTarget(defaultTarget);
            if (LOG_ENABLED) LOG.info("call site target reset to default, preparing outside invocation");

            cacheableCallSite.resetFallbackCount();
        }

        if (defaultTarget == cacheableCallSite.getTarget()) {
            // correct the stale methodhandle in the inline cache of callsite
            // it is important but impacts the performance somehow when cache misses frequently
            doWithCallSite(callSite, arguments, (cs, receiver) -> cs.put(receiver.getClass().getName(), mhw));
        }
    }

    return mhw.getCachedMethodHandle().invokeExact(arguments);
}
 
Example #5
Source File: Selector.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the Selector
 */
public static Selector getSelector(MutableCallSite callSite, Class<?> sender, String methodName, int callID, boolean safeNavigation, boolean thisCall, boolean spreadCall, Object[] arguments) {
    CallType callType = CALL_TYPE_VALUES[callID];
    switch (callType) {
        case INIT:
            return new InitSelector(callSite, sender, methodName, callType, safeNavigation, thisCall, spreadCall, arguments);
        case METHOD:
            return new MethodSelector(callSite, sender, methodName, callType, safeNavigation, thisCall, spreadCall, arguments);
        case GET:
            return new PropertySelector(callSite, sender, methodName, callType, safeNavigation, thisCall, spreadCall, arguments);
        case SET:
            throw new GroovyBugError("your call tried to do a property set, which is not supported.");
        case CAST:
            return new CastSelector(callSite, arguments);
        default:
            throw new GroovyBugError("unexpected call type");
    }
}
 
Example #6
Source File: BootstrapMethods.java    From bumblebench with Apache License 2.0 6 votes vote down vote up
public static CallSite gwtBootstrap(Lookup ignored, String name, MethodType type) throws Throwable {
	Lookup lookup = lookup();
	MethodHandle double_string = lookup.findStatic(BootstrapMethods.class, "dup", methodType(String.class, String.class));
	MethodHandle double_integer = lookup.findStatic(BootstrapMethods.class, "dup", methodType(String.class, Integer.class));
	MethodHandle double_object = lookup.findStatic(BootstrapMethods.class, "dup", methodType(String.class, Object.class));
	MethodHandle isInteger = lookup.findStatic(BootstrapMethods.class, "isInteger", methodType(boolean.class, Object.class));
	MethodHandle isString = lookup.findStatic(BootstrapMethods.class, "isString", methodType(boolean.class, Object.class));

	MethodHandle handle = guardWithTest(
			isString, 
			double_string.asType(methodType(String.class, Object.class)),
			double_object);
	handle = guardWithTest(
			isInteger,
			double_integer.asType(methodType(String.class, Object.class)),
			handle);
	return new MutableCallSite(handle);
}
 
Example #7
Source File: NativeCalls.java    From es6draft with MIT License 6 votes vote down vote up
@SuppressWarnings("unused")
private static MethodHandle nativeCallSetup(MutableCallSite callsite, String name, ExecutionContext cx) {
    RuntimeContext context = cx.getRuntimeContext();
    MethodHandle target;
    try {
        MethodHandle mh = context.getNativeCallResolver().apply(name, callsite.type());
        if (mh == null) {
            throw new IllegalArgumentException();
        }
        target = adaptNativeMethodHandle(mh);
        target = adaptMethodHandle(name, callsite.type(), target);
    } catch (IllegalArgumentException e) {
        target = invalidCallHandle(name, callsite.type());
    }
    callsite.setTarget(target);
    return target;
}
 
Example #8
Source File: Bootstrap.java    From es6draft with MIT License 6 votes vote down vote up
private static void concatSetup(MutableCallSite callsite, MethodType type) {
    MethodHandle target, test, generic;
    int numberOfStrings = type.parameterCount() - 1; // CharSequence..., ExecutionContext
    if (numberOfStrings <= CONCAT_MAX_SPECIALIZATION) {
        assert numberOfStrings >= CONCAT_MIN_PARAMS;
        int index = numberOfStrings - CONCAT_MIN_PARAMS;
        target = concatMH[index];
        test = testConcatMH[index];
        generic = concatConsMH[index];
    } else {
        final int index = CONCAT_MAX_SPECIALIZATION - CONCAT_MIN_PARAMS + 1;
        target = concatMH[index].asCollector(CharSequence[].class, numberOfStrings);
        test = testConcatMH[index].asCollector(CharSequence[].class, numberOfStrings);
        generic = concatConsMH[index].asCollector(CharSequence[].class, numberOfStrings);
    }
    setCallSiteTarget(callsite, target, test, generic);
}
 
Example #9
Source File: HotSpotGraphBuilderPlugins.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void registerCallSitePlugins(InvocationPlugins plugins) {
    InvocationPlugin plugin = new InvocationPlugin() {
        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver) {
            ValueNode callSite = receiver.get();
            ValueNode folded = CallSiteTargetNode.tryFold(GraphUtil.originalValue(callSite), b.getMetaAccess(), b.getAssumptions());
            if (folded != null) {
                b.addPush(JavaKind.Object, folded);
            } else {
                b.addPush(JavaKind.Object, new CallSiteTargetNode(b.getInvokeKind(), targetMethod, b.bci(), b.getInvokeReturnStamp(b.getAssumptions()), callSite));
            }
            return true;
        }

        @Override
        public boolean inlineOnly() {
            return true;
        }
    };
    plugins.register(plugin, ConstantCallSite.class, "getTarget", Receiver.class);
    plugins.register(plugin, MutableCallSite.class, "getTarget", Receiver.class);
    plugins.register(plugin, VolatileCallSite.class, "getTarget", Receiver.class);
}
 
Example #10
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle bitOrSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = bitOrNumberMH;
    } else if (type == Type.BigInt) {
        target = bitOrBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), bitOrGenericMH);
}
 
Example #11
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle shrSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = shrNumberMH;
    } else if (type == Type.BigInt) {
        target = shrBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), shrGenericMH);
}
 
Example #12
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void updateCallSite(boolean desired_state, MethodHandle tester, MutableCallSite c, MethodHandle mh, MutableCallSite[] cs) {
  try {
    if (((boolean)tester.invokeExact()) != desired_state) {
      c.setTarget(mh);
      MutableCallSite.syncAll(cs);
    }
  } catch (Throwable e) {
    Misc.internalError(e);
  }
}
 
Example #13
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle decSetup(MutableCallSite callsite, Number arg) {
    Type type = getType(arg);
    MethodHandle target;
    if (type == Type.Number) {
        target = decNumberMH;
    } else if (type == Type.BigInt) {
        target = decBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForUnaryNumber(type), decGenericMH);
}
 
Example #14
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle incSetup(MutableCallSite callsite, Number arg) {
    Type type = getType(arg);
    MethodHandle target;
    if (type == Type.Number) {
        target = incNumberMH;
    } else if (type == Type.BigInt) {
        target = incBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForUnaryNumber(type), incGenericMH);
}
 
Example #15
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle negSetup(MutableCallSite callsite, Number arg) {
    Type type = getType(arg);
    MethodHandle target;
    if (type == Type.Number) {
        target = negNumberMH;
    } else if (type == Type.BigInt) {
        target = negBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForUnaryNumber(type), negGenericMH);
}
 
Example #16
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle bitNotSetup(MutableCallSite callsite, Number arg) {
    Type type = getType(arg);
    MethodHandle target;
    if (type == Type.Number) {
        target = bitNotNumberMH;
    } else if (type == Type.BigInt) {
        target = bitNotBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForUnaryNumber(type), bitNotGenericMH);
}
 
Example #17
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle bitXorSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = bitXorNumberMH;
    } else if (type == Type.BigInt) {
        target = bitXorBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), bitXorGenericMH);
}
 
Example #18
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle modSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = modNumberMH;
    } else if (type == Type.BigInt) {
        target = modBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), modGenericMH);
}
 
Example #19
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle shlSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = shlNumberMH;
    } else if (type == Type.BigInt) {
        target = shlBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), shlGenericMH);
}
 
Example #20
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle divSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = divNumberMH;
    } else if (type == Type.BigInt) {
        target = divBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), divGenericMH);
}
 
Example #21
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle expSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = expNumberMH;
    } else if (type == Type.BigInt) {
        target = expBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), expGenericMH);
}
 
Example #22
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle subSetup(MutableCallSite callsite, Number arg1, Number arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.Number) {
        target = subNumberMH;
    } else if (type == Type.BigInt) {
        target = subBigIntMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinaryNumber(type), subGenericMH);
}
 
Example #23
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle strictEqCmpSetup(MutableCallSite callsite, Object arg1, Object arg2) {
    // TODO(BigInt): Implement BigInt specializations.
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = strictEqCmpStringMH;
    } else if (type == Type.Number) {
        target = strictEqCmpNumberMH;
    } else if (type == Type.Boolean) {
        target = strictEqCmpBooleanMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinary(type), strictEqCmpGenericMH);
}
 
Example #24
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle eqCmpSetup(MutableCallSite callsite, Object arg1, Object arg2, ExecutionContext cx) {
    // TODO(BigInt): Implement BigInt specializations.
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = eqCmpStringMH;
    } else if (type == Type.Number) {
        target = eqCmpNumberMH;
    } else if (type == Type.Boolean) {
        target = eqCmpBooleanMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinary(type), eqCmpGenericMH);
}
 
Example #25
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle relCmpSetup(MutableCallSite callsite, RelationalOperator op, Object arg1, Object arg2,
        ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = filterReturnValue(relCmpStringMH, op);
    } else if (type == Type.Number) {
        target = filterReturnValue(relCmpNumberMH, op);
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinary(type),
            filterReturnValue(MethodHandles.insertArguments(relCmpGenericMH, 2, op), op));
}
 
Example #26
Source File: Bootstrap.java    From es6draft with MIT License 5 votes vote down vote up
private static MethodHandle addSetup(MutableCallSite callsite, Object arg1, Object arg2, ExecutionContext cx) {
    Type type = getType(arg1, arg2);
    MethodHandle target;
    if (type == Type.String) {
        target = addStringMH;
    } else if (type == Type.Number) {
        target = addNumberMH;
    } else {
        target = null;
    }
    return setCallSiteTarget(callsite, target, getTestForBinary(type), addGenericMH);
}
 
Example #27
Source File: IndyInterface.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static <T> T doWithCallSite(MutableCallSite callSite, Object[] arguments, BiFunction<? super CacheableCallSite, ? super Object, ? extends T> f) {
    if (callSite instanceof CacheableCallSite) {
        CacheableCallSite cacheableCallSite = (CacheableCallSite) callSite;
        Object receiver = arguments[0];

        if (null == receiver) receiver = NullObject.getNullObject();

        return f.apply(cacheableCallSite, receiver);
    }

    throw new GroovyBugError("CacheableCallSite is expected, but the actual callsite is: " + callSite);
}
 
Example #28
Source File: IndyInterface.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static MethodHandleWrapper fallback(MutableCallSite callSite, Class<?> sender, String methodName, int callID, Boolean safeNavigation, Boolean thisCall, Boolean spreadCall, Object dummyReceiver, Object[] arguments) {
    Selector selector = Selector.getSelector(callSite, sender, methodName, callID, safeNavigation, thisCall, spreadCall, arguments);
    selector.setCallSiteTarget();

    return new MethodHandleWrapper(
            selector.handle.asSpreader(Object[].class, arguments.length).asType(MethodType.methodType(Object.class, Object[].class)),
            selector.handle,
            selector.cache
    );
}
 
Example #29
Source File: IndyInterface.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Get the cached methodhandle. if the related methodhandle is not found in the inline cache, cache and return it.
 */
public static Object fromCache(MutableCallSite callSite, Class<?> sender, String methodName, int callID, Boolean safeNavigation, Boolean thisCall, Boolean spreadCall, Object dummyReceiver, Object[] arguments) throws Throwable {
    FallbackSupplier fallbackSupplier = new FallbackSupplier(callSite, sender, methodName, callID, safeNavigation, thisCall, spreadCall, dummyReceiver, arguments);

    MethodHandleWrapper mhw =
            doWithCallSite(
                    callSite, arguments,
                    (cs, receiver) ->
                            cs.getAndPut(
                                    receiver.getClass().getName(),
                                    c -> {
                                        MethodHandleWrapper fbMhw = fallbackSupplier.get();
                                        return fbMhw.isCanSetTarget() ? fbMhw : NULL_METHOD_HANDLE_WRAPPER;
                                    }
                            )
            );

    if (NULL_METHOD_HANDLE_WRAPPER == mhw) {
        mhw = fallbackSupplier.get();
    }

    if (mhw.isCanSetTarget() && (callSite.getTarget() != mhw.getTargetMethodHandle()) && (mhw.getLatestHitCount() > INDY_OPTIMIZE_THRESHOLD)) {
        callSite.setTarget(mhw.getTargetMethodHandle());
        if (LOG_ENABLED) LOG.info("call site target set, preparing outside invocation");

        mhw.resetLatestHitCount();
    }

    return mhw.getCachedMethodHandle().invokeExact(arguments);
}
 
Example #30
Source File: IndyInterface.java    From groovy with Apache License 2.0 5 votes vote down vote up
FallbackSupplier(MutableCallSite callSite, Class<?> sender, String methodName, int callID, Boolean safeNavigation, Boolean thisCall, Boolean spreadCall, Object dummyReceiver, Object[] arguments) {
    this.callSite = callSite;
    this.sender = sender;
    this.methodName = methodName;
    this.callID = callID;
    this.safeNavigation = safeNavigation;
    this.thisCall = thisCall;
    this.spreadCall = spreadCall;
    this.dummyReceiver = dummyReceiver;
    this.arguments = arguments;
}