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