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