1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.internal.runtime;
  26 
  27 import static jdk.nashorn.internal.lookup.Lookup.MH;
  28 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
  29 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.isValid;
  30 import java.lang.invoke.CallSite;
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.MethodHandles;
  33 import java.lang.invoke.MethodType;
  34 import java.lang.invoke.MutableCallSite;
  35 import java.lang.invoke.SwitchPoint;
  36 import java.util.ArrayList;
  37 import java.util.Collection;
  38 import java.util.Collections;
  39 import java.util.Iterator;
  40 import java.util.List;
  41 import java.util.Map;
  42 import java.util.TreeMap;
  43 import java.util.function.Supplier;
  44 import java.util.logging.Level;
  45 import jdk.internal.dynalink.linker.GuardedInvocation;
  46 import jdk.nashorn.internal.codegen.Compiler;
  47 import jdk.nashorn.internal.codegen.Compiler.CompilationPhases;
  48 import jdk.nashorn.internal.codegen.TypeMap;
  49 import jdk.nashorn.internal.codegen.types.ArrayType;
  50 import jdk.nashorn.internal.codegen.types.Type;
  51 import jdk.nashorn.internal.ir.FunctionNode;
  52 import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic;
  53 import jdk.nashorn.internal.runtime.events.RecompilationEvent;
  54 import jdk.nashorn.internal.runtime.linker.Bootstrap;
  55 import jdk.nashorn.internal.runtime.logging.DebugLogger;
  56 
  57 /**
  58  * An version of a JavaScript function, native or JavaScript.
  59  * Supports lazily generating a constructor version of the invocation.
  60  */
  61 final class CompiledFunction {
  62 
  63     private static final MethodHandle NEWFILTER = findOwnMH("newFilter", Object.class, Object.class, Object.class);
  64     private static final MethodHandle RELINK_COMPOSABLE_INVOKER = findOwnMH("relinkComposableInvoker", void.class, CallSite.class, CompiledFunction.class, boolean.class);
  65     private static final MethodHandle HANDLE_REWRITE_EXCEPTION = findOwnMH("handleRewriteException", MethodHandle.class, CompiledFunction.class, OptimismInfo.class, RewriteException.class);
  66     private static final MethodHandle RESTOF_INVOKER = MethodHandles.exactInvoker(MethodType.methodType(Object.class, RewriteException.class));
  67 
  68     private final DebugLogger log;
  69 
  70     static final Collection<CompiledFunction> NO_FUNCTIONS = Collections.emptySet();
  71 
  72     /**
  73      * The method type may be more specific than the invoker, if. e.g.
  74      * the invoker is guarded, and a guard with a generic object only
  75      * fallback, while the target is more specific, we still need the
  76      * more specific type for sorting
  77      */
  78     private MethodHandle invoker;
  79     private MethodHandle constructor;
  80     private OptimismInfo optimismInfo;
  81     private final int flags; // from FunctionNode
  82     private final MethodType callSiteType;
  83 
  84     private final Specialization specialization;
  85 
  86     CompiledFunction(final MethodHandle invoker) {
  87         this(invoker, null, null);
  88     }
  89 
  90     static CompiledFunction createBuiltInConstructor(final MethodHandle invoker, final Specialization specialization) {
  91         return new CompiledFunction(MH.insertArguments(invoker, 0, false), createConstructorFromInvoker(MH.insertArguments(invoker, 0, true)), specialization);
  92     }
  93 
  94     CompiledFunction(final MethodHandle invoker, final MethodHandle constructor, final Specialization specialization) {
  95         this(invoker, constructor, 0, null, specialization, DebugLogger.DISABLED_LOGGER);
  96     }
  97 
  98     CompiledFunction(final MethodHandle invoker, final MethodHandle constructor, final int flags, final MethodType callSiteType, final Specialization specialization, final DebugLogger log) {
  99         this.specialization = specialization;
 100         if (specialization != null && specialization.isOptimistic()) {
 101             /*
 102              * An optimistic builtin with isOptimistic=true works like any optimistic generated function, i.e. it
 103              * can throw unwarranted optimism exceptions. As native functions trivially can't have parts of them
 104              * regenerated as restof methods, this only works if the methods are atomic/functional in their behavior
 105              * and doesn't modify state before an UOE can be thrown. If they aren't, we can reexecute a wider version
 106              * of the same builtin in a recompilation handler for FinalScriptFunctionData. There are several
 107              * candidate methods in Native* that would benefit from this, but I haven't had time to implement any
 108              * of them currently. In order to fit in with the relinking framework, the current thinking is
 109              * that the methods still take a program point to fit in with other optimistic functions, but
 110              * it is set to "first", which is the beginning of the method. The relinker can tell the difference
 111              * between builtin and JavaScript functions. This might change. TODO
 112              */
 113             this.invoker = MH.insertArguments(invoker, invoker.type().parameterCount() - 1, UnwarrantedOptimismException.FIRST_PROGRAM_POINT);
 114             throw new AssertionError("Optimistic (UnwarrantedOptimismException throwing) builtin functions are currently not in use");
 115         }
 116         this.invoker = invoker;
 117         this.constructor = constructor;
 118         this.flags = flags;
 119         this.callSiteType = callSiteType;
 120         this.log = log;
 121     }
 122 
 123     CompiledFunction(final MethodHandle invoker, final RecompilableScriptFunctionData functionData,
 124             final Map<Integer, Type> invalidatedProgramPoints, final MethodType callSiteType, final int flags) {
 125         this(invoker, null, flags, callSiteType, null, functionData.getLogger());
 126         if ((flags & FunctionNode.IS_DEOPTIMIZABLE) != 0) {
 127             optimismInfo = new OptimismInfo(functionData, invalidatedProgramPoints);
 128         } else {
 129             optimismInfo = null;
 130         }
 131     }
 132 
 133     static CompiledFunction createBuiltInConstructor(final MethodHandle invoker) {
 134         return new CompiledFunction(MH.insertArguments(invoker, 0, false), createConstructorFromInvoker(MH.insertArguments(invoker, 0, true)), null);
 135     }
 136 
 137     boolean isSpecialization() {
 138         return specialization != null;
 139     }
 140 
 141     boolean hasLinkLogic() {
 142         return getLinkLogicClass() != null;
 143     }
 144 
 145     Class<? extends LinkLogic> getLinkLogicClass() {
 146         if (isSpecialization()) {
 147             final Class<? extends LinkLogic> linkLogicClass = specialization.getLinkLogicClass();
 148             assert !LinkLogic.isEmpty(linkLogicClass) : "empty link logic classes should have been removed by nasgen";
 149             return linkLogicClass;
 150         }
 151         return null;
 152     }
 153 
 154     int getFlags() {
 155         return flags;
 156     }
 157 
 158     /**
 159      * An optimistic specialization is one that can throw UnwarrantedOptimismException.
 160      * This is allowed for native methods, as long as they are functional, i.e. don't change
 161      * any state between entering and throwing the UOE. Then we can re-execute a wider version
 162      * of the method in the continuation. Rest-of method generation for optimistic builtins is
 163      * of course not possible, but this approach works and fits into the same relinking
 164      * framework
 165      *
 166      * @return true if optimistic builtin
 167      */
 168     boolean isOptimistic() {
 169         return isSpecialization() ? specialization.isOptimistic() : false;
 170     }
 171 
 172     boolean isApplyToCall() {
 173         return (flags & FunctionNode.HAS_APPLY_TO_CALL_SPECIALIZATION) != 0;
 174     }
 175 
 176     boolean isVarArg() {
 177         return isVarArgsType(invoker.type());
 178     }
 179 
 180     @Override
 181     public String toString() {
 182         final StringBuilder sb = new StringBuilder();
 183         final Class<? extends LinkLogic> linkLogicClass = getLinkLogicClass();
 184 
 185         sb.append("[invokerType=").
 186             append(invoker.type()).
 187             append(" ctor=").
 188             append(constructor).
 189             append(" weight=").
 190             append(weight()).
 191             append(" linkLogic=").
 192             append(linkLogicClass != null ? linkLogicClass.getSimpleName() : "none");
 193 
 194         return sb.toString();
 195     }
 196 
 197     boolean needsCallee() {
 198         return ScriptFunctionData.needsCallee(invoker);
 199     }
 200 
 201     /**
 202      * Returns an invoker method handle for this function. Note that the handle is safely composable in
 203      * the sense that you can compose it with other handles using any combinators even if you can't affect call site
 204      * invalidation. If this compiled function is non-optimistic, then it returns the same value as
 205      * {@link #getInvokerOrConstructor(boolean)}. However, if the function is optimistic, then this handle will
 206      * incur an overhead as it will add an intermediate internal call site that can relink itself when the function
 207      * needs to regenerate its code to always point at the latest generated code version.
 208      * @return a guaranteed composable invoker method handle for this function.
 209      */
 210     MethodHandle createComposableInvoker() {
 211         return createComposableInvoker(false);
 212     }
 213 
 214     /**
 215      * Returns an invoker method handle for this function when invoked as a constructor. Note that the handle should be
 216      * considered non-composable in the sense that you can only compose it with other handles using any combinators if
 217      * you can ensure that the composition is guarded by {@link #getOptimisticAssumptionsSwitchPoint()} if it's
 218      * non-null, and that you can relink the call site it is set into as a target if the switch point is invalidated. In
 219      * all other cases, use {@link #createComposableConstructor()}.
 220      * @return a direct constructor method handle for this function.
 221      */
 222     private MethodHandle getConstructor() {
 223         if (constructor == null) {
 224             constructor = createConstructorFromInvoker(createInvokerForPessimisticCaller());
 225         }
 226 
 227         return constructor;
 228     }
 229 
 230     /**
 231      * Creates a version of the invoker intended for a pessimistic caller (return type is Object, no caller optimistic
 232      * program point available).
 233      * @return a version of the invoker intended for a pessimistic caller.
 234      */
 235     private MethodHandle createInvokerForPessimisticCaller() {
 236         return createInvoker(Object.class, INVALID_PROGRAM_POINT);
 237     }
 238 
 239     /**
 240      * Compose a constructor from an invoker.
 241      *
 242      * @param invoker         invoker
 243      * @return the composed constructor
 244      */
 245     private static MethodHandle createConstructorFromInvoker(final MethodHandle invoker) {
 246         final boolean needsCallee = ScriptFunctionData.needsCallee(invoker);
 247         // If it was (callee, this, args...), permute it to (this, callee, args...). We're doing this because having
 248         // "this" in the first argument position is what allows the elegant folded composition of
 249         // (newFilter x constructor x allocator) further down below in the code. Also, ensure the composite constructor
 250         // always returns Object.
 251         final MethodHandle swapped = needsCallee ? swapCalleeAndThis(invoker) : invoker;
 252 
 253         final MethodHandle returnsObject = MH.asType(swapped, swapped.type().changeReturnType(Object.class));
 254 
 255         final MethodType ctorType = returnsObject.type();
 256 
 257         // Construct a dropping type list for NEWFILTER, but don't include constructor "this" into it, so it's actually
 258         // captured as "allocation" parameter of NEWFILTER after we fold the constructor into it.
 259         // (this, [callee, ]args...) => ([callee, ]args...)
 260         final Class<?>[] ctorArgs = ctorType.dropParameterTypes(0, 1).parameterArray();
 261 
 262         // Fold constructor into newFilter that replaces the return value from the constructor with the originally
 263         // allocated value when the originally allocated value is a JS primitive (String, Boolean, Number).
 264         // (result, this, [callee, ]args...) x (this, [callee, ]args...) => (this, [callee, ]args...)
 265         final MethodHandle filtered = MH.foldArguments(MH.dropArguments(NEWFILTER, 2, ctorArgs), returnsObject);
 266 
 267         // allocate() takes a ScriptFunction and returns a newly allocated ScriptObject...
 268         if (needsCallee) {
 269             // ...we either fold it into the previous composition, if we need both the ScriptFunction callee object and
 270             // the newly allocated object in the arguments, so (this, callee, args...) x (callee) => (callee, args...),
 271             // or...
 272             return MH.foldArguments(filtered, ScriptFunction.ALLOCATE);
 273         }
 274 
 275         // ...replace the ScriptFunction argument with the newly allocated object, if it doesn't need the callee
 276         // (this, args...) filter (callee) => (callee, args...)
 277         return MH.filterArguments(filtered, 0, ScriptFunction.ALLOCATE);
 278     }
 279 
 280     /**
 281      * Permutes the parameters in the method handle from {@code (callee, this, ...)} to {@code (this, callee, ...)}.
 282      * Used when creating a constructor handle.
 283      * @param mh a method handle with order of arguments {@code (callee, this, ...)}
 284      * @return a method handle with order of arguments {@code (this, callee, ...)}
 285      */
 286     private static MethodHandle swapCalleeAndThis(final MethodHandle mh) {
 287         final MethodType type = mh.type();
 288         assert type.parameterType(0) == ScriptFunction.class : type;
 289         assert type.parameterType(1) == Object.class : type;
 290         final MethodType newType = type.changeParameterType(0, Object.class).changeParameterType(1, ScriptFunction.class);
 291         final int[] reorder = new int[type.parameterCount()];
 292         reorder[0] = 1;
 293         assert reorder[1] == 0;
 294         for (int i = 2; i < reorder.length; ++i) {
 295             reorder[i] = i;
 296         }
 297         return MethodHandles.permuteArguments(mh, newType, reorder);
 298     }
 299 
 300     /**
 301      * Returns an invoker method handle for this function when invoked as a constructor. Note that the handle is safely
 302      * composable in the sense that you can compose it with other handles using any combinators even if you can't affect
 303      * call site invalidation. If this compiled function is non-optimistic, then it returns the same value as
 304      * {@link #getConstructor()}. However, if the function is optimistic, then this handle will incur an overhead as it
 305      * will add an intermediate internal call site that can relink itself when the function needs to regenerate its code
 306      * to always point at the latest generated code version.
 307      * @return a guaranteed composable constructor method handle for this function.
 308      */
 309     MethodHandle createComposableConstructor() {
 310         return createComposableInvoker(true);
 311     }
 312 
 313     boolean hasConstructor() {
 314         return constructor != null;
 315     }
 316 
 317     MethodType type() {
 318         return invoker.type();
 319     }
 320 
 321     int weight() {
 322         return weight(type());
 323     }
 324 
 325     private static int weight(final MethodType type) {
 326         if (isVarArgsType(type)) {
 327             return Integer.MAX_VALUE; //if there is a varargs it should be the heavist and last fallback
 328         }
 329 
 330         int weight = Type.typeFor(type.returnType()).getWeight();
 331         for (int i = 0 ; i < type.parameterCount() ; i++) {
 332             final Class<?> paramType = type.parameterType(i);
 333             final int pweight = Type.typeFor(paramType).getWeight() * 2; //params are more important than call types as return values are always specialized
 334             weight += pweight;
 335         }
 336 
 337         weight += type.parameterCount(); //more params outweigh few parameters
 338 
 339         return weight;
 340     }
 341 
 342     static boolean isVarArgsType(final MethodType type) {
 343         assert type.parameterCount() >= 1 : type;
 344         return type.parameterType(type.parameterCount() - 1) == Object[].class;
 345     }
 346 
 347     static boolean moreGenericThan(final MethodType mt0, final MethodType mt1) {
 348         return weight(mt0) > weight(mt1);
 349     }
 350 
 351     boolean betterThanFinal(final CompiledFunction other, final MethodType callSiteMethodType) {
 352         // Prefer anything over nothing, as we can't compile new versions.
 353         if (other == null) {
 354             return true;
 355         }
 356         return betterThanFinal(this, other, callSiteMethodType);
 357     }
 358 
 359     private static boolean betterThanFinal(final CompiledFunction cf, final CompiledFunction other, final MethodType callSiteMethodType) {
 360         final MethodType thisMethodType  = cf.type();
 361         final MethodType otherMethodType = other.type();
 362         final int thisParamCount = getParamCount(thisMethodType);
 363         final int otherParamCount = getParamCount(otherMethodType);
 364         final int callSiteRawParamCount = getParamCount(callSiteMethodType);
 365         final boolean csVarArg = callSiteRawParamCount == Integer.MAX_VALUE;
 366         // Subtract 1 for callee for non-vararg call sites
 367         final int callSiteParamCount = csVarArg ? callSiteRawParamCount : callSiteRawParamCount - 1;
 368 
 369         // Prefer the function that discards less parameters
 370         final int thisDiscardsParams = Math.max(callSiteParamCount - thisParamCount, 0);
 371         final int otherDiscardsParams = Math.max(callSiteParamCount - otherParamCount, 0);
 372         if(thisDiscardsParams < otherDiscardsParams) {
 373             return true;
 374         }
 375         if(thisDiscardsParams > otherDiscardsParams) {
 376             return false;
 377         }
 378 
 379         final boolean thisVarArg = thisParamCount == Integer.MAX_VALUE;
 380         final boolean otherVarArg = otherParamCount == Integer.MAX_VALUE;
 381         if(!(thisVarArg && otherVarArg && csVarArg)) {
 382             // At least one of them isn't vararg
 383             final Type[] thisType = toTypeWithoutCallee(thisMethodType, 0); // Never has callee
 384             final Type[] otherType = toTypeWithoutCallee(otherMethodType, 0); // Never has callee
 385             final Type[] callSiteType = toTypeWithoutCallee(callSiteMethodType, 1); // Always has callee
 386 
 387             int narrowWeightDelta = 0;
 388             int widenWeightDelta = 0;
 389             final int minParamsCount = Math.min(Math.min(thisParamCount, otherParamCount), callSiteParamCount);
 390             for(int i = 0; i < minParamsCount; ++i) {
 391                 final int callSiteParamWeight = getParamType(i, callSiteType, csVarArg).getWeight();
 392                 // Delta is negative for narrowing, positive for widening
 393                 final int thisParamWeightDelta = getParamType(i, thisType, thisVarArg).getWeight() - callSiteParamWeight;
 394                 final int otherParamWeightDelta = getParamType(i, otherType, otherVarArg).getWeight() - callSiteParamWeight;
 395                 // Only count absolute values of narrowings
 396                 narrowWeightDelta += Math.max(-thisParamWeightDelta, 0) - Math.max(-otherParamWeightDelta, 0);
 397                 // Only count absolute values of widenings
 398                 widenWeightDelta += Math.max(thisParamWeightDelta, 0) - Math.max(otherParamWeightDelta, 0);
 399             }
 400 
 401             // If both functions accept more arguments than what is passed at the call site, account for ability
 402             // to receive Undefined un-narrowed in the remaining arguments.
 403             if(!thisVarArg) {
 404                 for(int i = callSiteParamCount; i < thisParamCount; ++i) {
 405                     narrowWeightDelta += Math.max(Type.OBJECT.getWeight() - thisType[i].getWeight(), 0);
 406                 }
 407             }
 408             if(!otherVarArg) {
 409                 for(int i = callSiteParamCount; i < otherParamCount; ++i) {
 410                     narrowWeightDelta -= Math.max(Type.OBJECT.getWeight() - otherType[i].getWeight(), 0);
 411                 }
 412             }
 413 
 414             // Prefer function that narrows less
 415             if(narrowWeightDelta < 0) {
 416                 return true;
 417             }
 418             if(narrowWeightDelta > 0) {
 419                 return false;
 420             }
 421 
 422             // Prefer function that widens less
 423             if(widenWeightDelta < 0) {
 424                 return true;
 425             }
 426             if(widenWeightDelta > 0) {
 427                 return false;
 428             }
 429         }
 430 
 431         // Prefer the function that exactly matches the arity of the call site.
 432         if(thisParamCount == callSiteParamCount && otherParamCount != callSiteParamCount) {
 433             return true;
 434         }
 435         if(thisParamCount != callSiteParamCount && otherParamCount == callSiteParamCount) {
 436             return false;
 437         }
 438 
 439         // Otherwise, neither function matches arity exactly. We also know that at this point, they both can receive
 440         // more arguments than call site, otherwise we would've already chosen the one that discards less parameters.
 441         // Note that variable arity methods are preferred, as they actually match the call site arity better, since they
 442         // really have arbitrary arity.
 443         if(thisVarArg) {
 444             if(!otherVarArg) {
 445                 return true; //
 446             }
 447         } else if(otherVarArg) {
 448             return false;
 449         }
 450 
 451         // Neither is variable arity; chose the one that has less extra parameters.
 452         final int fnParamDelta = thisParamCount - otherParamCount;
 453         if(fnParamDelta < 0) {
 454             return true;
 455         }
 456         if(fnParamDelta > 0) {
 457             return false;
 458         }
 459 
 460         final int callSiteRetWeight = Type.typeFor(callSiteMethodType.returnType()).getWeight();
 461         // Delta is negative for narrower return type, positive for wider return type
 462         final int thisRetWeightDelta = Type.typeFor(thisMethodType.returnType()).getWeight() - callSiteRetWeight;
 463         final int otherRetWeightDelta = Type.typeFor(otherMethodType.returnType()).getWeight() - callSiteRetWeight;
 464 
 465         // Prefer function that returns a less wide return type
 466         final int widenRetDelta = Math.max(thisRetWeightDelta, 0) - Math.max(otherRetWeightDelta, 0);
 467         if(widenRetDelta < 0) {
 468             return true;
 469         }
 470         if(widenRetDelta > 0) {
 471             return false;
 472         }
 473 
 474         // Prefer function that returns a less narrow return type
 475         final int narrowRetDelta = Math.max(-thisRetWeightDelta, 0) - Math.max(-otherRetWeightDelta, 0);
 476         if(narrowRetDelta < 0) {
 477             return true;
 478         }
 479         if(narrowRetDelta > 0) {
 480             return false;
 481         }
 482 
 483         //if they are equal, pick the specialized one first
 484         if (cf.isSpecialization() != other.isSpecialization()) {
 485             return cf.isSpecialization(); //always pick the specialized version if we can
 486         }
 487 
 488         if (cf.isSpecialization() && other.isSpecialization()) {
 489             return cf.getLinkLogicClass() != null; //pick link logic specialization above generic specializations
 490         }
 491 
 492         // Signatures are identical
 493         throw new AssertionError(thisMethodType + " identically applicable to " + otherMethodType + " for " + callSiteMethodType);
 494     }
 495 
 496     private static Type[] toTypeWithoutCallee(final MethodType type, final int thisIndex) {
 497         final int paramCount = type.parameterCount();
 498         final Type[] t = new Type[paramCount - thisIndex];
 499         for(int i = thisIndex; i < paramCount; ++i) {
 500             t[i - thisIndex] = Type.typeFor(type.parameterType(i));
 501         }
 502         return t;
 503     }
 504 
 505     private static Type getParamType(final int i, final Type[] paramTypes, final boolean isVarArg) {
 506         final int fixParamCount = paramTypes.length - (isVarArg ? 1 : 0);
 507         if(i < fixParamCount) {
 508             return paramTypes[i];
 509         }
 510         assert isVarArg;
 511         return ((ArrayType)paramTypes[paramTypes.length - 1]).getElementType();
 512     }
 513 
 514     boolean matchesCallSite(final MethodType other, final boolean pickVarArg) {
 515         if (other.equals(this.callSiteType)) {
 516             return true;
 517         }
 518         final MethodType type  = type();
 519         final int fnParamCount = getParamCount(type);
 520         final boolean isVarArg = fnParamCount == Integer.MAX_VALUE;
 521         if (isVarArg) {
 522             return pickVarArg;
 523         }
 524 
 525         final int csParamCount = getParamCount(other);
 526         final boolean csIsVarArg = csParamCount == Integer.MAX_VALUE;
 527         final int thisThisIndex = needsCallee() ? 1 : 0; // Index of "this" parameter in this function's type
 528 
 529         final int fnParamCountNoCallee = fnParamCount - thisThisIndex;
 530         final int minParams = Math.min(csParamCount - 1, fnParamCountNoCallee); // callSiteType always has callee, so subtract 1
 531         // We must match all incoming parameters, including "this". "this" will usually be Object, but there
 532         // are exceptions, e.g. when calling functions with primitive "this" in strict mode or through call/apply.
 533         for(int i = 0; i < minParams; ++i) {
 534             final Type fnType = Type.typeFor(type.parameterType(i + thisThisIndex));
 535             final Type csType = csIsVarArg ? Type.OBJECT : Type.typeFor(other.parameterType(i + 1));
 536             if(!fnType.isEquivalentTo(csType)) {
 537                 return false;
 538             }
 539         }
 540 
 541         // Must match any undefined parameters to Object type.
 542         for(int i = minParams; i < fnParamCountNoCallee; ++i) {
 543             if(!Type.typeFor(type.parameterType(i + thisThisIndex)).isEquivalentTo(Type.OBJECT)) {
 544                 return false;
 545             }
 546         }
 547 
 548         return true;
 549     }
 550 
 551     private static int getParamCount(final MethodType type) {
 552         final int paramCount = type.parameterCount();
 553         return type.parameterType(paramCount - 1).isArray() ? Integer.MAX_VALUE : paramCount;
 554     }
 555 
 556     private boolean canBeDeoptimized() {
 557         return optimismInfo != null;
 558     }
 559 
 560     private MethodHandle createComposableInvoker(final boolean isConstructor) {
 561         final MethodHandle handle = getInvokerOrConstructor(isConstructor);
 562 
 563         // If compiled function is not optimistic, it can't ever change its invoker/constructor, so just return them
 564         // directly.
 565         if(!canBeDeoptimized()) {
 566             return handle;
 567         }
 568 
 569         // Otherwise, we need a new level of indirection; need to introduce a mutable call site that can relink itslef
 570         // to the compiled function's changed target whenever the optimistic assumptions are invalidated.
 571         final CallSite cs = new MutableCallSite(handle.type());
 572         relinkComposableInvoker(cs, this, isConstructor);
 573         return cs.dynamicInvoker();
 574     }
 575 
 576     private static class HandleAndAssumptions {
 577         final MethodHandle handle;
 578         final SwitchPoint assumptions;
 579 
 580         HandleAndAssumptions(final MethodHandle handle, final SwitchPoint assumptions) {
 581             this.handle = handle;
 582             this.assumptions = assumptions;
 583         }
 584 
 585         GuardedInvocation createInvocation() {
 586             return new GuardedInvocation(handle, assumptions);
 587         }
 588     }
 589 
 590     /**
 591      * Returns a pair of an invocation created with a passed-in supplier and a non-invalidated switch point for
 592      * optimistic assumptions (or null for the switch point if the function can not be deoptimized). While the method
 593      * makes a best effort to return a non-invalidated switch point (compensating for possible deoptimizing
 594      * recompilation happening on another thread) it is still possible that by the time this method returns the
 595      * switchpoint has been invalidated by a {@code RewriteException} triggered on another thread for this function.
 596      * This is not a problem, though, as these switch points are always used to produce call sites that fall back to
 597      * relinking when they are invalidated, and in this case the execution will end up here again. What this method
 598      * basically does is minimize such busy-loop relinking while the function is being recompiled on a different thread.
 599      * @param invocationSupplier the supplier that constructs the actual invocation method handle; should use the
 600      * {@code CompiledFunction} method itself in some capacity.
 601      * @return a tuple object containing the method handle as created by the supplier and an optimistic assumptions
 602      * switch point that is guaranteed to not have been invalidated before the call to this method (or null if the
 603      * function can't be further deoptimized).
 604      */
 605     private synchronized HandleAndAssumptions getValidOptimisticInvocation(final Supplier<MethodHandle> invocationSupplier) {
 606         for(;;) {
 607             final MethodHandle handle = invocationSupplier.get();
 608             final SwitchPoint assumptions = canBeDeoptimized() ? optimismInfo.optimisticAssumptions : null;
 609             if(assumptions != null && assumptions.hasBeenInvalidated()) {
 610                 // We can be in a situation where one thread is in the middle of a deoptimizing compilation when we hit
 611                 // this and thus, it has invalidated the old switch point, but hasn't created the new one yet. Note that
 612                 // the behavior of invalidating the old switch point before recompilation, and only creating the new one
 613                 // after recompilation is by design. If we didn't wait here for the recompilation to complete, we would
 614                 // be busy looping through the fallback path of the invalidated switch point, relinking the call site
 615                 // again with the same invalidated switch point, invoking the fallback, etc. stealing CPU cycles from
 616                 // the recompilation task we're dependent on. This can still happen if the switch point gets invalidated
 617                 // after we grabbed it here, in which case we'll indeed do one busy relink immediately.
 618                 try {
 619                     wait();
 620                 } catch (final InterruptedException e) {
 621                     // Intentionally ignored. There's nothing meaningful we can do if we're interrupted
 622                 }
 623             } else {
 624                 return new HandleAndAssumptions(handle, assumptions);
 625             }
 626         }
 627     }
 628 
 629     private static void relinkComposableInvoker(final CallSite cs, final CompiledFunction inv, final boolean constructor) {
 630         final HandleAndAssumptions handleAndAssumptions = inv.getValidOptimisticInvocation(new Supplier<MethodHandle>() {
 631             @Override
 632             public MethodHandle get() {
 633                 return inv.getInvokerOrConstructor(constructor);
 634             }
 635         });
 636         final MethodHandle handle = handleAndAssumptions.handle;
 637         final SwitchPoint assumptions = handleAndAssumptions.assumptions;
 638         final MethodHandle target;
 639         if(assumptions == null) {
 640             target = handle;
 641         } else {
 642             final MethodHandle relink = MethodHandles.insertArguments(RELINK_COMPOSABLE_INVOKER, 0, cs, inv, constructor);
 643             target = assumptions.guardWithTest(handle, MethodHandles.foldArguments(cs.dynamicInvoker(), relink));
 644         }
 645         cs.setTarget(target.asType(cs.type()));
 646     }
 647 
 648     private MethodHandle getInvokerOrConstructor(final boolean selectCtor) {
 649         return selectCtor ? getConstructor() : createInvokerForPessimisticCaller();
 650     }
 651 
 652     /**
 653      * Returns a guarded invocation for this function when not invoked as a constructor. The guarded invocation has no
 654      * guard but it potentially has an optimistic assumptions switch point. As such, it will probably not be used as a
 655      * final guarded invocation, but rather as a holder for an invocation handle and switch point to be decomposed and
 656      * reassembled into a different final invocation by the user of this method. Any recompositions should take care to
 657      * continue to use the switch point. If that is not possible, use {@link #createComposableInvoker()} instead.
 658      * @return a guarded invocation for an ordinary (non-constructor) invocation of this function.
 659      */
 660     GuardedInvocation createFunctionInvocation(final Class<?> callSiteReturnType, final int callerProgramPoint) {
 661         return getValidOptimisticInvocation(new Supplier<MethodHandle>() {
 662             @Override
 663             public MethodHandle get() {
 664                 return createInvoker(callSiteReturnType, callerProgramPoint);
 665             }
 666         }).createInvocation();
 667     }
 668 
 669     /**
 670      * Returns a guarded invocation for this function when invoked as a constructor. The guarded invocation has no guard
 671      * but it potentially has an optimistic assumptions switch point. As such, it will probably not be used as a final
 672      * guarded invocation, but rather as a holder for an invocation handle and switch point to be decomposed and
 673      * reassembled into a different final invocation by the user of this method. Any recompositions should take care to
 674      * continue to use the switch point. If that is not possible, use {@link #createComposableConstructor()} instead.
 675      * @return a guarded invocation for invocation of this function as a constructor.
 676      */
 677     GuardedInvocation createConstructorInvocation() {
 678         return getValidOptimisticInvocation(new Supplier<MethodHandle>() {
 679             @Override
 680             public MethodHandle get() {
 681                 return getConstructor();
 682             }
 683         }).createInvocation();
 684     }
 685 
 686     private MethodHandle createInvoker(final Class<?> callSiteReturnType, final int callerProgramPoint) {
 687         final boolean isOptimistic = canBeDeoptimized();
 688         MethodHandle handleRewriteException = isOptimistic ? createRewriteExceptionHandler() : null;
 689 
 690         MethodHandle inv = invoker;
 691         if(isValid(callerProgramPoint)) {
 692             inv = OptimisticReturnFilters.filterOptimisticReturnValue(inv, callSiteReturnType, callerProgramPoint);
 693             inv = changeReturnType(inv, callSiteReturnType);
 694             if(callSiteReturnType.isPrimitive() && handleRewriteException != null) {
 695                 // because handleRewriteException always returns Object
 696                 handleRewriteException = OptimisticReturnFilters.filterOptimisticReturnValue(handleRewriteException,
 697                         callSiteReturnType, callerProgramPoint);
 698             }
 699         } else if(isOptimistic) {
 700             // Required so that rewrite exception has the same return type. It'd be okay to do it even if we weren't
 701             // optimistic, but it isn't necessary as the linker upstream will eventually convert the return type.
 702             inv = changeReturnType(inv, callSiteReturnType);
 703         }
 704 
 705         if(isOptimistic) {
 706             assert handleRewriteException != null;
 707             final MethodHandle typedHandleRewriteException = changeReturnType(handleRewriteException, inv.type().returnType());
 708             return MH.catchException(inv, RewriteException.class, typedHandleRewriteException);
 709         }
 710         return inv;
 711     }
 712 
 713     private MethodHandle createRewriteExceptionHandler() {
 714         return MH.foldArguments(RESTOF_INVOKER, MH.insertArguments(HANDLE_REWRITE_EXCEPTION, 0, this, optimismInfo));
 715     }
 716 
 717     private static MethodHandle changeReturnType(final MethodHandle mh, final Class<?> newReturnType) {
 718         return Bootstrap.getLinkerServices().asType(mh, mh.type().changeReturnType(newReturnType));
 719     }
 720 
 721     @SuppressWarnings("unused")
 722     private static MethodHandle handleRewriteException(final CompiledFunction function, final OptimismInfo oldOptimismInfo, final RewriteException re) {
 723         return function.handleRewriteException(oldOptimismInfo, re);
 724     }
 725 
 726     /**
 727      * Debug function for printing out all invalidated program points and their
 728      * invalidation mapping to next type
 729      * @param ipp
 730      * @return string describing the ipp map
 731      */
 732     private static List<String> toStringInvalidations(final Map<Integer, Type> ipp) {
 733         if (ipp == null) {
 734             return Collections.emptyList();
 735         }
 736 
 737         final List<String> list = new ArrayList<>();
 738 
 739         for (final Iterator<Map.Entry<Integer, Type>> iter = ipp.entrySet().iterator(); iter.hasNext(); ) {
 740             final Map.Entry<Integer, Type> entry = iter.next();
 741             final char bct = entry.getValue().getBytecodeStackType();
 742             final String type;
 743 
 744             switch (entry.getValue().getBytecodeStackType()) {
 745             case 'A':
 746                 type = "object";
 747                 break;
 748             case 'I':
 749                 type = "int";
 750                 break;
 751             case 'J':
 752                 type = "long";
 753                 break;
 754             case 'D':
 755                 type = "double";
 756                 break;
 757             default:
 758                 type = String.valueOf(bct);
 759                 break;
 760             }
 761 
 762             final StringBuilder sb = new StringBuilder();
 763             sb.append('[').
 764                     append("program point: ").
 765                     append(entry.getKey()).
 766                     append(" -> ").
 767                     append(type).
 768                     append(']');
 769 
 770             list.add(sb.toString());
 771         }
 772 
 773         return list;
 774     }
 775 
 776     private void logRecompile(final String reason, final FunctionNode fn, final MethodType type, final Map<Integer, Type> ipp) {
 777         if (log.isEnabled()) {
 778             log.info(reason, DebugLogger.quote(fn.getName()), " signature: ", type);
 779             log.indent();
 780             for (final String str : toStringInvalidations(ipp)) {
 781                 log.fine(str);
 782             }
 783             log.unindent();
 784         }
 785     }
 786 
 787     /**
 788      * Handles a {@link RewriteException} raised during the execution of this function by recompiling (if needed) the
 789      * function with an optimistic assumption invalidated at the program point indicated by the exception, and then
 790      * executing a rest-of method to complete the execution with the deoptimized version.
 791      * @param oldOptInfo the optimism info of this function. We must store it explicitly as a bound argument in the
 792      * method handle, otherwise it could be null for handling a rewrite exception in an outer invocation of a recursive
 793      * function when recursive invocations of the function have completely deoptimized it.
 794      * @param re the rewrite exception that was raised
 795      * @return the method handle for the rest-of method, for folding composition.
 796      */
 797     private synchronized MethodHandle handleRewriteException(final OptimismInfo oldOptInfo, final RewriteException re) {
 798         if (log.isEnabled()) {
 799             log.info(
 800                     new RecompilationEvent(
 801                         Level.INFO,
 802                         re,
 803                         re.getReturnValueNonDestructive()),
 804                     "caught RewriteException ",
 805                     re.getMessageShort());
 806             log.indent();
 807         }
 808 
 809         final MethodType type = type();
 810 
 811         // Compiler needs a call site type as its input, which always has a callee parameter, so we must add it if
 812         // this function doesn't have a callee parameter.
 813         final MethodType ct = type.parameterType(0) == ScriptFunction.class ?
 814                 type :
 815                 type.insertParameterTypes(0, ScriptFunction.class);
 816         final OptimismInfo currentOptInfo = optimismInfo;
 817         final boolean shouldRecompile = currentOptInfo != null && currentOptInfo.requestRecompile(re);
 818 
 819         // Effective optimism info, for subsequent use. We'll normally try to use the current (latest) one, but if it
 820         // isn't available, we'll use the old one bound into the call site.
 821         final OptimismInfo effectiveOptInfo = currentOptInfo != null ? currentOptInfo : oldOptInfo;
 822         FunctionNode fn = effectiveOptInfo.reparse();
 823         final boolean serialized = effectiveOptInfo.isSerialized();
 824         final Compiler compiler = effectiveOptInfo.getCompiler(fn, ct, re); //set to non rest-of
 825 
 826         if (!shouldRecompile) {
 827             // It didn't necessarily recompile, e.g. for an outer invocation of a recursive function if we already
 828             // recompiled a deoptimized version for an inner invocation.
 829             // We still need to do the rest of from the beginning
 830             logRecompile("Rest-of compilation [STANDALONE] ", fn, ct, effectiveOptInfo.invalidatedProgramPoints);
 831             return restOfHandle(effectiveOptInfo, compiler.compile(fn, serialized ? CompilationPhases.COMPILE_SERIALIZED_RESTOF : CompilationPhases.COMPILE_ALL_RESTOF), currentOptInfo != null);
 832         }
 833 
 834         logRecompile("Deoptimizing recompilation (up to bytecode) ", fn, ct, effectiveOptInfo.invalidatedProgramPoints);
 835         fn = compiler.compile(fn, serialized ? CompilationPhases.RECOMPILE_SERIALIZED_UPTO_BYTECODE : CompilationPhases.COMPILE_UPTO_BYTECODE);
 836         log.fine("Reusable IR generated");
 837 
 838         // compile the rest of the function, and install it
 839         log.info("Generating and installing bytecode from reusable IR...");
 840         logRecompile("Rest-of compilation [CODE PIPELINE REUSE] ", fn, ct, effectiveOptInfo.invalidatedProgramPoints);
 841         final FunctionNode normalFn = compiler.compile(fn, CompilationPhases.GENERATE_BYTECODE_AND_INSTALL);
 842 
 843         if (effectiveOptInfo.data.usePersistentCodeCache()) {
 844             final RecompilableScriptFunctionData data = effectiveOptInfo.data;
 845             final int functionNodeId = data.getFunctionNodeId();
 846             final TypeMap typeMap = data.typeMap(ct);
 847             final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
 848             final String cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
 849             compiler.persistClassInfo(cacheKey, normalFn);
 850         }
 851 
 852         final boolean canBeDeoptimized = normalFn.canBeDeoptimized();
 853 
 854         if (log.isEnabled()) {
 855             log.unindent();
 856             log.info("Done.");
 857 
 858             log.info("Recompiled '", fn.getName(), "' (", Debug.id(this), ") ", canBeDeoptimized ? "can still be deoptimized." : " is completely deoptimized.");
 859             log.finest("Looking up invoker...");
 860         }
 861 
 862         final MethodHandle newInvoker = effectiveOptInfo.data.lookup(fn);
 863         invoker     = newInvoker.asType(type.changeReturnType(newInvoker.type().returnType()));
 864         constructor = null; // Will be regenerated when needed
 865 
 866         log.info("Done: ", invoker);
 867         final MethodHandle restOf = restOfHandle(effectiveOptInfo, compiler.compile(fn, CompilationPhases.GENERATE_BYTECODE_AND_INSTALL_RESTOF), canBeDeoptimized);
 868 
 869         // Note that we only adjust the switch point after we set the invoker/constructor. This is important.
 870         if (canBeDeoptimized) {
 871             effectiveOptInfo.newOptimisticAssumptions(); // Otherwise, set a new switch point.
 872         } else {
 873             optimismInfo = null; // If we got to a point where we no longer have optimistic assumptions, let the optimism info go.
 874         }
 875         notifyAll();
 876 
 877         return restOf;
 878     }
 879 
 880     private MethodHandle restOfHandle(final OptimismInfo info, final FunctionNode restOfFunction, final boolean canBeDeoptimized) {
 881         assert info != null;
 882         assert restOfFunction.getCompileUnit().getUnitClassName().contains("restOf");
 883         final MethodHandle restOf =
 884                 changeReturnType(
 885                         info.data.lookupCodeMethod(
 886                                 restOfFunction.getCompileUnit().getCode(),
 887                                 MH.type(restOfFunction.getReturnType().getTypeClass(),
 888                                         RewriteException.class)),
 889                         Object.class);
 890 
 891         if (!canBeDeoptimized) {
 892             return restOf;
 893         }
 894 
 895         // If rest-of is itself optimistic, we must make sure that we can repeat a deoptimization if it, too hits an exception.
 896         return MH.catchException(restOf, RewriteException.class, createRewriteExceptionHandler());
 897 
 898     }
 899 
 900     private static class OptimismInfo {
 901         // TODO: this is pointing to its owning ScriptFunctionData. Re-evaluate if that's okay.
 902         private final RecompilableScriptFunctionData data;
 903         private final Map<Integer, Type> invalidatedProgramPoints;
 904         private SwitchPoint optimisticAssumptions;
 905         private final DebugLogger log;
 906 
 907         OptimismInfo(final RecompilableScriptFunctionData data, final Map<Integer, Type> invalidatedProgramPoints) {
 908             this.data = data;
 909             this.log  = data.getLogger();
 910             this.invalidatedProgramPoints = invalidatedProgramPoints == null ? new TreeMap<Integer, Type>() : invalidatedProgramPoints;
 911             newOptimisticAssumptions();
 912         }
 913 
 914         private void newOptimisticAssumptions() {
 915             optimisticAssumptions = new SwitchPoint();
 916         }
 917 
 918         boolean requestRecompile(final RewriteException e) {
 919             final Type retType            = e.getReturnType();
 920             final Type previousFailedType = invalidatedProgramPoints.put(e.getProgramPoint(), retType);
 921 
 922             if (previousFailedType != null && !previousFailedType.narrowerThan(retType)) {
 923                 final StackTraceElement[] stack      = e.getStackTrace();
 924                 final String              functionId = stack.length == 0 ?
 925                         data.getName() :
 926                         stack[0].getClassName() + "." + stack[0].getMethodName();
 927 
 928                 log.info("RewriteException for an already invalidated program point ", e.getProgramPoint(), " in ", functionId, ". This is okay for a recursive function invocation, but a bug otherwise.");
 929 
 930                 return false;
 931             }
 932 
 933             SwitchPoint.invalidateAll(new SwitchPoint[] { optimisticAssumptions });
 934 
 935             return true;
 936         }
 937 
 938         Compiler getCompiler(final FunctionNode fn, final MethodType actualCallSiteType, final RewriteException e) {
 939             return data.getCompiler(fn, actualCallSiteType, e.getRuntimeScope(), invalidatedProgramPoints, getEntryPoints(e));
 940         }
 941 
 942         private static int[] getEntryPoints(final RewriteException e) {
 943             final int[] prevEntryPoints = e.getPreviousContinuationEntryPoints();
 944             final int[] entryPoints;
 945             if (prevEntryPoints == null) {
 946                 entryPoints = new int[1];
 947             } else {
 948                 final int l = prevEntryPoints.length;
 949                 entryPoints = new int[l + 1];
 950                 System.arraycopy(prevEntryPoints, 0, entryPoints, 1, l);
 951             }
 952             entryPoints[0] = e.getProgramPoint();
 953             return entryPoints;
 954         }
 955 
 956         FunctionNode reparse() {
 957             return data.reparse();
 958         }
 959 
 960         boolean isSerialized() {
 961             return data.isSerialized();
 962         }
 963     }
 964 
 965     @SuppressWarnings("unused")
 966     private static Object newFilter(final Object result, final Object allocation) {
 967         return (result instanceof ScriptObject || !JSType.isPrimitive(result))? result : allocation;
 968     }
 969 
 970     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 971         return MH.findStatic(MethodHandles.lookup(), CompiledFunction.class, name, MH.type(rtype, types));
 972     }
 973 }
--- EOF ---