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