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