src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java
author darcy
Fri, 27 Sep 2019 08:57:25 -0700
changeset 58379 8511c662083b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231557: Suppress warnings on non-serializable instance fields in jdk.scripting.nashorn module Reviewed-by: jlaskey
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
/*
58379
8511c662083b 8231557: Suppress warnings on non-serializable instance fields in jdk.scripting.nashorn module
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
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
     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
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
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
    27
17233
72ccf78a8216 8010701: Immutable nodes - final iteration
lagergren
parents: 16523
diff changeset
    28
import static jdk.nashorn.internal.lookup.Lookup.MH;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    29
24769
attila
parents: 24759 23767
diff changeset
    30
import java.io.IOException;
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
    31
import java.io.ObjectOutputStream;
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
    32
import java.io.Serializable;
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
    33
import java.lang.invoke.MethodHandle;
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.MethodHandles;
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
    35
import java.lang.invoke.MethodType;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    36
import java.lang.ref.Reference;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    37
import java.lang.ref.SoftReference;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
    38
import java.util.Collection;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    39
import java.util.Collections;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    40
import java.util.HashSet;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    41
import java.util.IdentityHashMap;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
    42
import java.util.Map;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    43
import java.util.Set;
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
    44
import java.util.TreeMap;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    45
import java.util.concurrent.ExecutorService;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    46
import java.util.concurrent.LinkedBlockingDeque;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    47
import java.util.concurrent.ThreadPoolExecutor;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    48
import java.util.concurrent.TimeUnit;
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
    49
import jdk.nashorn.internal.codegen.Compiler;
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
    50
import jdk.nashorn.internal.codegen.Compiler.CompilationPhases;
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.CompilerConstants;
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
    52
import jdk.nashorn.internal.codegen.FunctionSignature;
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
    53
import jdk.nashorn.internal.codegen.Namespace;
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
    54
import jdk.nashorn.internal.codegen.OptimisticTypesPersistence;
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
    55
import jdk.nashorn.internal.codegen.TypeMap;
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: 17233
diff changeset
    56
import jdk.nashorn.internal.codegen.types.Type;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    57
import jdk.nashorn.internal.ir.Block;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    58
import jdk.nashorn.internal.ir.ForNode;
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
    59
import jdk.nashorn.internal.ir.FunctionNode;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    60
import jdk.nashorn.internal.ir.IdentNode;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    61
import jdk.nashorn.internal.ir.LexicalContext;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    62
import jdk.nashorn.internal.ir.Node;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    63
import jdk.nashorn.internal.ir.SwitchNode;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    64
import jdk.nashorn.internal.ir.Symbol;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    65
import jdk.nashorn.internal.ir.TryNode;
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 32696
diff changeset
    66
import jdk.nashorn.internal.ir.visitor.SimpleNodeVisitor;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
    67
import jdk.nashorn.internal.objects.Global;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    68
import jdk.nashorn.internal.parser.Parser;
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
    69
import jdk.nashorn.internal.parser.Token;
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
    70
import jdk.nashorn.internal.parser.TokenType;
33533
43400f0f2b47 8141144: Move NameCodec to jdk.nashorn.internal space
attila
parents: 32888
diff changeset
    71
import jdk.nashorn.internal.runtime.linker.NameCodec;
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
    72
import jdk.nashorn.internal.runtime.logging.DebugLogger;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
    73
import jdk.nashorn.internal.runtime.logging.Loggable;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
    74
import jdk.nashorn.internal.runtime.logging.Logger;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    75
import jdk.nashorn.internal.runtime.options.Options;
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
    76
/**
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
    77
 * This is a subclass that represents a script function that may be regenerated,
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
    78
 * for example with specialization based on call site types, or lazily generated.
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
    79
 * The common denominator is that it can get new invokers during its lifespan,
19472
9476460521b3 8023017: SUB missing for widest op == number for BinaryNode
lagergren
parents: 19456
diff changeset
    80
 * unlike {@code FinalScriptFunctionData}
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
    81
 */
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
    82
@Logger(name="recompile")
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
    83
public final class RecompilableScriptFunctionData extends ScriptFunctionData implements Loggable {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    84
    /** Prefix used for all recompiled script classes */
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
    85
    public static final String RECOMPILATION_PREFIX = "Recompilation$";
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
    86
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    87
    private static final ExecutorService astSerializerExecutorService = createAstSerializerExecutorService();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
    88
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    89
    /** Unique function node id for this function node */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    90
    private final int functionNodeId;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    91
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    92
    private final String functionName;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    93
24769
attila
parents: 24759 23767
diff changeset
    94
    /** The line number where this function begins. */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
    95
    private final int lineNumber;
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 17756
diff changeset
    96
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 17756
diff changeset
    97
    /** Source from which FunctionNode was parsed. */
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
    98
    private transient Source source;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
    99
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   100
    /**
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   101
     * Cached form of the AST. Either a {@code SerializedAst} object used by split functions as they can't be
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   102
     * reparsed from source, or a soft reference to a {@code FunctionNode} for other functions (it is safe
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   103
     * to be cleared as they can be reparsed).
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   104
     */
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   105
    private volatile transient Object cachedAst;
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   106
19456
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 17756
diff changeset
   107
    /** Token of this function within the source. */
8cc345d620c8 8022524: Memory leaks in nashorn sources and tests found by jhat analysis
sundar
parents: 17756
diff changeset
   108
    private final long token;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   109
26510
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   110
    /**
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   111
     * Represents the allocation strategy (property map, script object class, and method handle) for when
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   112
     * this function is used as a constructor. Note that majority of functions (those not setting any this.*
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   113
     * properties) will share a single canonical "default strategy" instance.
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   114
     */
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   115
    private final AllocationStrategy allocationStrategy;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   116
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   117
    /**
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   118
     * Opaque object representing parser state at the end of the function. Used when reparsing outer function
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   119
     * to help with skipping parsing inner functions.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   120
     */
58379
8511c662083b 8231557: Suppress warnings on non-serializable instance fields in jdk.scripting.nashorn module
darcy
parents: 47216
diff changeset
   121
    @SuppressWarnings("serial") // Not statically typed as Serializable
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   122
    private final Object endParserState;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   123
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   124
    /** Code installer used for all further recompilation/specialization of this ScriptFunction */
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   125
    private transient CodeInstaller installer;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   126
58379
8511c662083b 8231557: Suppress warnings on non-serializable instance fields in jdk.scripting.nashorn module
darcy
parents: 47216
diff changeset
   127
    @SuppressWarnings("serial") // Not statically typed as Serializable
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: 24720
diff changeset
   128
    private final Map<Integer, RecompilableScriptFunctionData> nestedFunctions;
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: 24720
diff changeset
   129
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: 24720
diff changeset
   130
    /** Id to parent function if one exists */
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: 24720
diff changeset
   131
    private RecompilableScriptFunctionData parent;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   132
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   133
    /** Copy of the {@link FunctionNode} flags. */
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   134
    private final int functionFlags;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   135
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
   136
    private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
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
   137
24769
attila
parents: 24759 23767
diff changeset
   138
    private transient DebugLogger log;
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
   139
58379
8511c662083b 8231557: Suppress warnings on non-serializable instance fields in jdk.scripting.nashorn module
darcy
parents: 47216
diff changeset
   140
    @SuppressWarnings("serial") // Not statically typed as Serializable
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: 24720
diff changeset
   141
    private final Map<String, Integer> externalScopeDepths;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   142
58379
8511c662083b 8231557: Suppress warnings on non-serializable instance fields in jdk.scripting.nashorn module
darcy
parents: 47216
diff changeset
   143
    @SuppressWarnings("serial") // Not statically typed as Serializable
24735
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   144
    private final Set<String> internalSymbols;
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   145
24769
attila
parents: 24759 23767
diff changeset
   146
    private static final int GET_SET_PREFIX_LENGTH = "*et ".length();
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   147
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   148
    private static final long serialVersionUID = 4914839316174633726L;
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   149
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   150
    /**
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
   151
     * Constructor - public as scripts use 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
   152
     *
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: 24720
diff changeset
   153
     * @param functionNode        functionNode that represents this function code
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: 24720
diff changeset
   154
     * @param installer           installer for code regeneration versions of this function
29406
d25bb4d24a81 8074556: Functions should not share allocator maps
hannesw
parents: 27529
diff changeset
   155
     * @param allocationStrategy  strategy for the allocation behavior when this function is used as a constructor
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: 24720
diff changeset
   156
     * @param nestedFunctions     nested function map
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: 24720
diff changeset
   157
     * @param externalScopeDepths external scope depths
24735
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   158
     * @param internalSymbols     internal symbols to method, defined in its scope
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
   159
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   160
    public RecompilableScriptFunctionData(
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   161
        final FunctionNode functionNode,
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   162
        final CodeInstaller installer,
29406
d25bb4d24a81 8074556: Functions should not share allocator maps
hannesw
parents: 27529
diff changeset
   163
        final AllocationStrategy allocationStrategy,
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   164
        final Map<Integer, RecompilableScriptFunctionData> nestedFunctions,
24735
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   165
        final Map<String, Integer> externalScopeDepths,
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   166
        final Set<String> internalSymbols) {
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: 24720
diff changeset
   167
20934
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   168
        super(functionName(functionNode),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   169
              Math.min(functionNode.getParameters().size(), MAX_ARITY),
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   170
              getDataFlags(functionNode));
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
   171
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: 24720
diff changeset
   172
        this.functionName        = functionNode.getName();
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: 24720
diff changeset
   173
        this.lineNumber          = functionNode.getLineNumber();
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   174
        this.functionFlags       = functionNode.getFlags() | (functionNode.needsCallee() ? FunctionNode.NEEDS_CALLEE : 0);
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: 24720
diff changeset
   175
        this.functionNodeId      = functionNode.getId();
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: 24720
diff changeset
   176
        this.source              = functionNode.getSource();
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   177
        this.endParserState      = functionNode.getEndParserState();
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: 24720
diff changeset
   178
        this.token               = tokenFor(functionNode);
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: 24720
diff changeset
   179
        this.installer           = installer;
29406
d25bb4d24a81 8074556: Functions should not share allocator maps
hannesw
parents: 27529
diff changeset
   180
        this.allocationStrategy  = allocationStrategy;
26510
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   181
        this.nestedFunctions     = smallMap(nestedFunctions);
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   182
        this.externalScopeDepths = smallMap(externalScopeDepths);
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   183
        this.internalSymbols     = smallSet(new HashSet<>(internalSymbols));
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: 24720
diff changeset
   184
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: 24720
diff changeset
   185
        for (final RecompilableScriptFunctionData nfn : nestedFunctions.values()) {
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: 24720
diff changeset
   186
            assert nfn.getParent() == null;
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: 24720
diff changeset
   187
            nfn.setParent(this);
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   188
        }
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   189
24769
attila
parents: 24759 23767
diff changeset
   190
        createLogger();
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   191
    }
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   192
26510
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   193
    private static <K, V> Map<K, V> smallMap(final Map<K, V> map) {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   194
        if (map == null || map.isEmpty()) {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   195
            return Collections.emptyMap();
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   196
        } else if (map.size() == 1) {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   197
            final Map.Entry<K, V> entry = map.entrySet().iterator().next();
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   198
            return Collections.singletonMap(entry.getKey(), entry.getValue());
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   199
        } else {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   200
            return map;
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   201
        }
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   202
    }
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   203
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   204
    private static <T> Set<T> smallSet(final Set<T> set) {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   205
        if (set == null || set.isEmpty()) {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   206
            return Collections.emptySet();
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   207
        } else if (set.size() == 1) {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   208
            return Collections.singleton(set.iterator().next());
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   209
        } else {
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   210
            return set;
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   211
        }
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   212
    }
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   213
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   214
    @Override
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   215
    public DebugLogger getLogger() {
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   216
        return log;
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   217
    }
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   218
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   219
    @Override
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   220
    public DebugLogger initLogger(final Context ctxt) {
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   221
        return ctxt.getLogger(this.getClass());
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: 24720
diff changeset
   222
    }
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: 24720
diff changeset
   223
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: 24720
diff changeset
   224
    /**
24735
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   225
     * Check if a symbol is internally defined in a function. For example
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   226
     * if "undefined" is internally defined in the outermost program function,
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   227
     * it has not been reassigned or overridden and can be optimized
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   228
     *
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   229
     * @param symbolName symbol name
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   230
     * @return true if symbol is internal to this ScriptFunction
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   231
     */
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   232
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   233
    public boolean hasInternalSymbol(final String symbolName) {
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   234
        return internalSymbols.contains(symbolName);
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   235
    }
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   236
9833d3ceed5b 8038945: Simplify strict undefined checks
lagergren
parents: 24733
diff changeset
   237
    /**
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: 24720
diff changeset
   238
     * Return the external symbol table
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: 24720
diff changeset
   239
     * @param symbolName symbol name
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: 24720
diff changeset
   240
     * @return the external symbol table with proto depths
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: 24720
diff changeset
   241
     */
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: 24720
diff changeset
   242
    public int getExternalSymbolDepth(final String symbolName) {
26510
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   243
        final Integer depth = externalScopeDepths.get(symbolName);
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   244
        return depth == null ? -1 : depth;
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: 24720
diff changeset
   245
    }
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: 24720
diff changeset
   246
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: 24720
diff changeset
   247
    /**
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   248
     * Returns the names of all external symbols this function uses.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   249
     * @return the names of all external symbols this function uses.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   250
     */
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   251
    public Set<String> getExternalSymbolNames() {
26510
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   252
        return Collections.unmodifiableSet(externalScopeDepths.keySet());
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   253
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   254
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   255
    /**
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   256
     * Returns the opaque object representing the parser state at the end of this function's body, used to
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   257
     * skip parsing this function when reparsing its containing outer function.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   258
     * @return the object representing the end parser state
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   259
     */
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   260
    public Object getEndParserState() {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   261
        return endParserState;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   262
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   263
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   264
    /**
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: 24720
diff changeset
   265
     * Get the parent of this RecompilableScriptFunctionData. If we are
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: 24720
diff changeset
   266
     * a nested function, we have a parent. Note that "null" return value
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: 24720
diff changeset
   267
     * can also mean that we have a parent but it is unknown, so this can
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: 24720
diff changeset
   268
     * only be used for conservative assumptions.
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: 24720
diff changeset
   269
     * @return parent data, or null if non exists and also null IF UNKNOWN.
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: 24720
diff changeset
   270
     */
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: 24720
diff changeset
   271
    public RecompilableScriptFunctionData getParent() {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   272
        return parent;
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: 24720
diff changeset
   273
    }
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: 24720
diff changeset
   274
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: 24720
diff changeset
   275
    void setParent(final RecompilableScriptFunctionData parent) {
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: 24720
diff changeset
   276
        this.parent = parent;
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
   277
    }
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
   278
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
   279
    @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
   280
    String toSource() {
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
   281
        if (source != null && token != 0) {
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
   282
            return source.getString(Token.descPosition(token), Token.descLength(token));
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
   283
        }
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
   284
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
   285
        return "function " + (name == null ? "" : name) + "() { [native 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
   286
    }
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
   287
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24778
diff changeset
   288
    /**
25243
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25236
diff changeset
   289
     * Initialize transient fields on deserialized instances
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24778
diff changeset
   290
     *
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25243
diff changeset
   291
     * @param src source
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25243
diff changeset
   292
     * @param inst code installer
24993
b707d46bae40 8046898: Make sure that lazy compilation is the default, remove redundant "enable lazy compilation" flags, added warning message if compile logging is enabled and lazy is switched off. Verified existing test suite code coverage equivalence between lazy and eager.
lagergren
parents: 24778
diff changeset
   293
     */
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   294
    public void initTransients(final Source src, final CodeInstaller inst) {
25243
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25236
diff changeset
   295
        if (this.source == null && this.installer == null) {
25821
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25243
diff changeset
   296
            this.source    = src;
fbb51e67d2a7 8048869: Reduce compile time by about 5% by removing the Class.casts from the AST nodes
lagergren
parents: 25243
diff changeset
   297
            this.installer = inst;
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   298
            for (final RecompilableScriptFunctionData nested : nestedFunctions.values()) {
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   299
                nested.initTransients(src, inst);
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   300
            }
26891
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   301
        } else if (this.source != src || !this.installer.isCompatibleWith(inst)) {
25243
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25236
diff changeset
   302
            // Existing values must be same as those passed as parameters
7a1edca6ce94 8048079: Persistent code store is broken after optimistic types merge
hannesw
parents: 25236
diff changeset
   303
            throw new IllegalArgumentException();
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   304
        }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   305
    }
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   306
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
   307
    @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
   308
    public String toString() {
24733
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   309
        return super.toString() + '@' + functionNodeId;
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   310
    }
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   311
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   312
    @Override
1e825be55fd1 8027043: Turn global accesses into MethodHandle.constant, with one chance of reassignment, e.g. x = value occuring once in the global scope is ok, twice is not.
lagergren
parents: 24731
diff changeset
   313
    public String toStringVerbose() {
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
   314
        final StringBuilder sb = new StringBuilder();
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
   315
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   316
        sb.append("fnId=").append(functionNodeId).append(' ');
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: 24720
diff changeset
   317
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
   318
        if (source != 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
   319
            sb.append(source.getName())
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
                .append(':')
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   321
                .append(lineNumber)
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
   322
                .append(' ');
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
   323
        }
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
   324
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
   325
        return sb.toString() + super.toString();
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
   326
    }
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
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   328
    @Override
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   329
    public String getFunctionName() {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   330
        return functionName;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   331
    }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   332
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24747
diff changeset
   333
    @Override
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24747
diff changeset
   334
    public boolean inDynamicContext() {
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   335
        return getFunctionFlag(FunctionNode.IN_DYNAMIC_CONTEXT);
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24747
diff changeset
   336
    }
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24747
diff changeset
   337
20934
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   338
    private static String functionName(final FunctionNode fn) {
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   339
        if (fn.isAnonymous()) {
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   340
            return "";
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   341
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   342
        final FunctionNode.Kind kind = fn.getKind();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   343
        if (kind == FunctionNode.Kind.GETTER || kind == FunctionNode.Kind.SETTER) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   344
            final String name = NameCodec.decode(fn.getIdent().getName());
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: 24720
diff changeset
   345
            return name.substring(GET_SET_PREFIX_LENGTH);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   346
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   347
        return fn.getIdent().getName();
20934
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   348
    }
9cbbf2065cd1 8026264: Getter, setter function name mangling issues
sundar
parents: 20559
diff changeset
   349
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
   350
    private static long tokenFor(final FunctionNode fn) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   351
        final int  position  = Token.descPosition(fn.getFirstToken());
24994
92a19723a5ac 8047035: (function() "hello")() crashes in Lexer with jdk9
sundar
parents: 24993
diff changeset
   352
        final long lastToken = Token.withDelimiter(fn.getLastToken());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   353
        // EOL uses length field to store the line number
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   354
        final int  length    = Token.descPosition(lastToken) - position + (Token.descType(lastToken) == TokenType.EOL ? 0 : Token.descLength(lastToken));
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
   355
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
   356
        return Token.toDesc(TokenType.FUNCTION, position, length);
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
   357
    }
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
   358
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   359
    private static int getDataFlags(final FunctionNode functionNode) {
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   360
        int flags = IS_CONSTRUCTOR;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   361
        if (functionNode.isStrict()) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   362
            flags |= IS_STRICT;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   363
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   364
        if (functionNode.needsCallee()) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   365
            flags |= NEEDS_CALLEE;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   366
        }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   367
        if (functionNode.usesThis() || functionNode.hasEval()) {
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   368
            flags |= USES_THIS;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   369
        }
24727
attila
parents: 24725 23372
diff changeset
   370
        if (functionNode.isVarArg()) {
attila
parents: 24725 23372
diff changeset
   371
            flags |= IS_VARIABLE_ARITY;
attila
parents: 24725 23372
diff changeset
   372
        }
30393
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 30389
diff changeset
   373
        if (functionNode.getKind() == FunctionNode.Kind.GETTER || functionNode.getKind() == FunctionNode.Kind.SETTER) {
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 30389
diff changeset
   374
            flags |= IS_PROPERTY_ACCESSOR;
0e005bd1de13 8066237: Fuzzing bug: Parser error on optimistic recompilation
hannesw
parents: 30389
diff changeset
   375
        }
39077
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34736
diff changeset
   376
        if (functionNode.isMethod() || functionNode.isClassConstructor()) {
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34736
diff changeset
   377
            flags |= IS_ES6_METHOD;
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34736
diff changeset
   378
        }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   379
        return flags;
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   380
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   381
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
   382
    @Override
32695
9b708b92c695 8134609: Allow constructors with same prototoype map to share the allocator map
hannesw
parents: 32530
diff changeset
   383
    PropertyMap getAllocatorMap(final ScriptObject prototype) {
9b708b92c695 8134609: Allow constructors with same prototoype map to share the allocator map
hannesw
parents: 32530
diff changeset
   384
        return allocationStrategy.getAllocatorMap(prototype);
24727
attila
parents: 24725 23372
diff changeset
   385
    }
attila
parents: 24725 23372
diff changeset
   386
attila
parents: 24725 23372
diff changeset
   387
    @Override
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20946
diff changeset
   388
    ScriptObject allocate(final PropertyMap map) {
26510
a6f3dd80da4c 8058100: Reduce the RecompilableScriptFunctionData footprint
attila
parents: 26503
diff changeset
   389
        return allocationStrategy.allocate(map);
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
   390
    }
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
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   392
    FunctionNode reparse() {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   393
        final FunctionNode cachedFunction = getCachedAst();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   394
        if (cachedFunction != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   395
            assert cachedFunction.isCached();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   396
            return cachedFunction;
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   397
        }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   398
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   399
        final int descPosition = Token.descPosition(token);
24769
attila
parents: 24759 23767
diff changeset
   400
        final Context context = Context.getContextTrusted();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   401
        final Parser parser = new Parser(
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   402
            context.getEnv(),
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   403
            source,
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   404
            new Context.ThrowErrorManager(),
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   405
            isStrict(),
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   406
            // source starts at line 0, so even though lineNumber is the correct declaration line, back off
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   407
            // one to make it exclusive
24745
3a6e1477362b 8041434: Add synchronization to the common global constants structure
lagergren
parents: 24744
diff changeset
   408
            lineNumber - 1,
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   409
            context.getLogger(Parser.class));
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
   410
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   411
        if (getFunctionFlag(FunctionNode.IS_ANONYMOUS)) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   412
            parser.setFunctionName(functionName);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   413
        }
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   414
        parser.setReparsedFunction(this);
24749
1549c85f8200 8041625: AccessorProperty currentType must only by Object.class when non-primitive, and scoping followup problem for lazily generated with bodies
lagergren
parents: 24747
diff changeset
   415
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   416
        final FunctionNode program = parser.parse(CompilerConstants.PROGRAM.symbolName(), descPosition,
39077
c549268fe94c 8156614: Lazy parsing of ES6 shorthand method syntax is broken
hannesw
parents: 34736
diff changeset
   417
                Token.descLength(token), flags);
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   418
        // Parser generates a program AST even if we're recompiling a single function, so when we are only
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   419
        // recompiling a single function, extract it from the program.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   420
        return (isProgram() ? program : extractFunctionFromScript(program)).setName(null, functionName);
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   421
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   422
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   423
    private FunctionNode getCachedAst() {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   424
        final Object lCachedAst = cachedAst;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   425
        // Are we softly caching the AST?
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   426
        if (lCachedAst instanceof Reference<?>) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   427
            final FunctionNode fn = (FunctionNode)((Reference<?>)lCachedAst).get();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   428
            if (fn != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   429
                // Yes we are - this is fast
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   430
                return cloneSymbols(fn);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   431
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   432
        // Are we strongly caching a serialized AST (for split functions only)?
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   433
        } else if (lCachedAst instanceof SerializedAst) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   434
            final SerializedAst serializedAst = (SerializedAst)lCachedAst;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   435
            // Even so, are we also softly caching the AST?
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   436
            final FunctionNode cachedFn = serializedAst.cachedAst == null ? null : serializedAst.cachedAst.get();
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   437
            if (cachedFn != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   438
                // Yes we are - this is fast
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   439
                return cloneSymbols(cachedFn);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   440
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   441
            final FunctionNode deserializedFn = deserialize(serializedAst.serializedAst);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   442
            // Softly cache after deserialization, maybe next time we won't need to deserialize
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   443
            serializedAst.cachedAst = new SoftReference<>(deserializedFn);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   444
            return deserializedFn;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   445
        }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   446
        // No cached representation; return null for reparsing
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   447
        return null;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   448
    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   449
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   450
    /**
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   451
     * Sets the AST to cache in this function
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   452
     * @param astToCache the new AST to cache
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   453
     */
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   454
    public void setCachedAst(final FunctionNode astToCache) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   455
        assert astToCache.getId() == functionNodeId; // same function
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   456
        assert !(cachedAst instanceof SerializedAst); // Can't overwrite serialized AST
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   457
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   458
        final boolean isSplit = astToCache.isSplit();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   459
        // If we're caching a split function, we're doing it in the eager pass, hence there can be no other
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   460
        // cached representation already. In other words, isSplit implies cachedAst == null.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   461
        assert !isSplit || cachedAst == null; //
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   462
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   463
        final FunctionNode symbolClonedAst = cloneSymbols(astToCache);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   464
        final Reference<FunctionNode> ref = new SoftReference<>(symbolClonedAst);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   465
        cachedAst = ref;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   466
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   467
        // Asynchronously serialize split functions.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   468
        if (isSplit) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   469
            astSerializerExecutorService.execute(() -> {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   470
                cachedAst = new SerializedAst(symbolClonedAst, ref);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   471
            });
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   472
        }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   473
    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   474
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   475
    /**
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   476
     * Creates the AST serializer executor service used for in-memory serialization of split functions' ASTs.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   477
     * It is created with an unbounded queue (so it can queue any number of pending tasks). Its core and max
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   478
     * threads is the same, but they are all allowed to time out so when there's no work, they can all go
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   479
     * away. The threads will be daemons, and they will time out if idle for a minute. Their priority is also
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   480
     * slightly lower than normal priority as we'd prefer the CPU to keep running the program; serializing
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   481
     * split function is a memory conservation measure (it allows us to release the AST), it can wait a bit.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   482
     * @return an executor service with above described characteristics.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   483
     */
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   484
    private static ExecutorService createAstSerializerExecutorService() {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   485
        final int threads = Math.max(1, Options.getIntProperty("nashorn.serialize.threads", Runtime.getRuntime().availableProcessors() / 2));
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   486
        final ThreadPoolExecutor service = new ThreadPoolExecutor(threads, threads, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>(),
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   487
            (r) -> {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   488
                final Thread t = new Thread(r, "Nashorn AST Serializer");
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   489
                t.setDaemon(true);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   490
                t.setPriority(Thread.NORM_PRIORITY - 1);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   491
                return t;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   492
            });
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   493
        service.allowCoreThreadTimeOut(true);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   494
        return service;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   495
    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   496
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   497
    /**
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   498
     * A tuple of a serialized AST and a soft reference to a deserialized AST. This is used to cache split
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   499
     * functions. Since split functions are altered from their source form, they can't be reparsed from
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   500
     * source. While we could just use the {@code byte[]} representation in {@link RecompilableScriptFunctionData#cachedAst}
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   501
     * we're using this tuple instead to also keep a deserialized AST around in memory to cut down on
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   502
     * deserialization costs.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   503
     */
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   504
    private static class SerializedAst implements Serializable {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   505
        private final byte[] serializedAst;
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   506
        private volatile transient Reference<FunctionNode> cachedAst;
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   507
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
   508
        private static final long serialVersionUID = 1L;
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   509
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   510
        SerializedAst(final FunctionNode fn, final Reference<FunctionNode> cachedAst) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   511
            this.serializedAst = AstSerializer.serialize(fn);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   512
            this.cachedAst = cachedAst;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   513
        }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   514
    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   515
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   516
    private FunctionNode deserialize(final byte[] serializedAst) {
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   517
        final ScriptEnvironment env = installer.getContext().getEnv();
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   518
        final Timing timing = env._timing;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   519
        final long t1 = System.nanoTime();
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   520
        try {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   521
            return AstDeserializer.deserialize(serializedAst).initializeDeserialized(source, new Namespace(env.getNamespace()));
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   522
        } finally {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   523
            timing.accumulateTime("'Deserialize'", System.nanoTime() - t1);
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   524
        }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   525
    }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   526
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   527
    private FunctionNode cloneSymbols(final FunctionNode fn) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   528
        final IdentityHashMap<Symbol, Symbol> symbolReplacements = new IdentityHashMap<>();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   529
        final boolean cached = fn.isCached();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   530
        // blockDefinedSymbols is used to re-mark symbols defined outside the function as global. We only
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   531
        // need to do this when we cache an eagerly parsed function (which currently means a split one, as we
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   532
        // don't cache non-split functions from the eager pass); those already cached, or those not split
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   533
        // don't need this step.
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   534
        final Set<Symbol> blockDefinedSymbols = fn.isSplit() && !cached ? Collections.newSetFromMap(new IdentityHashMap<>()) : null;
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 32696
diff changeset
   535
        FunctionNode newFn = (FunctionNode)fn.accept(new SimpleNodeVisitor() {
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   536
            private Symbol getReplacement(final Symbol original) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   537
                if (original == null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   538
                    return null;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   539
                }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   540
                final Symbol existingReplacement = symbolReplacements.get(original);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   541
                if (existingReplacement != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   542
                    return existingReplacement;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   543
                }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   544
                final Symbol newReplacement = original.clone();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   545
                symbolReplacements.put(original, newReplacement);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   546
                return newReplacement;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   547
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   548
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   549
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   550
            public Node leaveIdentNode(final IdentNode identNode) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   551
                final Symbol oldSymbol = identNode.getSymbol();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   552
                if (oldSymbol != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   553
                    final Symbol replacement = getReplacement(oldSymbol);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   554
                    return identNode.setSymbol(replacement);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   555
                }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   556
                return identNode;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   557
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   558
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   559
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   560
            public Node leaveForNode(final ForNode forNode) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   561
                return ensureUniqueLabels(forNode.setIterator(lc, getReplacement(forNode.getIterator())));
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   562
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   563
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   564
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   565
            public Node leaveSwitchNode(final SwitchNode switchNode) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   566
                return ensureUniqueLabels(switchNode.setTag(lc, getReplacement(switchNode.getTag())));
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   567
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   568
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   569
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   570
            public Node leaveTryNode(final TryNode tryNode) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   571
                return ensureUniqueLabels(tryNode.setException(lc, getReplacement(tryNode.getException())));
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   572
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   573
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   574
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   575
            public boolean enterBlock(final Block block) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   576
                for(final Symbol symbol: block.getSymbols()) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   577
                    final Symbol replacement = getReplacement(symbol);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   578
                    if (blockDefinedSymbols != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   579
                        blockDefinedSymbols.add(replacement);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   580
                    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   581
                }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   582
                return true;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   583
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   584
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   585
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   586
            public Node leaveBlock(final Block block) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   587
                return ensureUniqueLabels(block.replaceSymbols(lc, symbolReplacements));
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   588
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   589
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   590
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   591
            public Node leaveFunctionNode(final FunctionNode functionNode) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   592
                return functionNode.setParameters(lc, functionNode.visitParameters(this));
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   593
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   594
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   595
            @Override
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   596
            protected Node leaveDefault(final Node node) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   597
                return ensureUniqueLabels(node);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   598
            };
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   599
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   600
            private Node ensureUniqueLabels(final Node node) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   601
                // If we're returning a cached AST, we must also ensure unique labels
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   602
                return cached ? node.ensureUniqueLabels(lc) : node;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   603
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   604
        });
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   605
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   606
        if (blockDefinedSymbols != null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   607
            // Mark all symbols not defined in blocks as globals
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   608
            Block newBody = null;
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   609
            for(final Symbol symbol: symbolReplacements.values()) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   610
                if(!blockDefinedSymbols.contains(symbol)) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   611
                    assert symbol.isScope(); // must be scope
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   612
                    assert externalScopeDepths.containsKey(symbol.getName()); // must be known to us as an external
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   613
                    // Register it in the function body symbol table as a new global symbol
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   614
                    symbol.setFlags((symbol.getFlags() & ~Symbol.KINDMASK) | Symbol.IS_GLOBAL);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   615
                    if (newBody == null) {
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   616
                        newBody = newFn.getBody().copyWithNewSymbols();
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   617
                        newFn = newFn.setBody(null, newBody);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   618
                    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   619
                    assert newBody.getExistingSymbol(symbol.getName()) == null; // must not be defined in the body already
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   620
                    newBody.putSymbol(symbol);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   621
                }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   622
            }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   623
        }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   624
        return newFn.setCached(null);
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   625
    }
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   626
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   627
    private boolean getFunctionFlag(final int flag) {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   628
        return (functionFlags & flag) != 0;
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   629
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   630
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   631
    private boolean isProgram() {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   632
        return getFunctionFlag(FunctionNode.IS_PROGRAM);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   633
    }
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
   634
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   635
    TypeMap typeMap(final MethodType fnCallSiteType) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   636
        if (fnCallSiteType == null) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   637
            return null;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   638
        }
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   639
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   640
        if (CompiledFunction.isVarArgsType(fnCallSiteType)) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   641
            return null;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   642
        }
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   643
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   644
        return new TypeMap(functionNodeId, explicitParams(fnCallSiteType), needsCallee());
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: 17233
diff changeset
   645
    }
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: 17233
diff changeset
   646
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   647
    private static ScriptObject newLocals(final ScriptObject runtimeScope) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   648
        final ScriptObject locals = Global.newEmptyInstance();
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   649
        locals.setProto(runtimeScope);
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   650
        return locals;
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   651
    }
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   652
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   653
    private Compiler getCompiler(final FunctionNode fn, final MethodType actualCallSiteType, final ScriptObject runtimeScope) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   654
        return getCompiler(fn, actualCallSiteType, newLocals(runtimeScope), null, null);
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   655
    }
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   656
26891
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   657
    /**
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   658
     * Returns a code installer for installing new code. If we're using either optimistic typing or loader-per-compile,
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   659
     * then asks for a code installer with a new class loader; otherwise just uses the current installer. We use
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   660
     * a new class loader with optimistic typing so that deoptimized code can get reclaimed by GC.
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   661
     * @return a code installer for installing new code.
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   662
     */
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   663
    private CodeInstaller getInstallerForNewCode() {
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   664
        final ScriptEnvironment env = installer.getContext().getEnv();
32696
05c6d9c5eb07 8135251: Use Unsafe.defineAnonymousClass for loading Nashorn script code
attila
parents: 32695
diff changeset
   665
        return env._optimistic_types || env._loader_per_compile ? installer.getOnDemandCompilationInstaller() : installer;
26891
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   666
    }
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   667
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   668
    Compiler getCompiler(final FunctionNode functionNode, final MethodType actualCallSiteType,
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   669
            final ScriptObject runtimeScope, final Map<Integer, Type> invalidatedProgramPoints,
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   670
            final int[] continuationEntryPoints) {
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   671
        final TypeMap typeMap = typeMap(actualCallSiteType);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   672
        final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   673
        final Object typeInformationFile = OptimisticTypesPersistence.getLocationDescriptor(source, functionNodeId, paramTypes);
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   674
        return Compiler.forOnDemandCompilation(
26891
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   675
                getInstallerForNewCode(),
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   676
                functionNode.getSource(),  // source
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   677
                isStrict() | functionNode.isStrict(), // is strict
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   678
                this,       // compiledFunction, i.e. this RecompilableScriptFunctionData
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   679
                typeMap,    // type map
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   680
                getEffectiveInvalidatedProgramPoints(invalidatedProgramPoints, typeInformationFile), // invalidated program points
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   681
                typeInformationFile,
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   682
                continuationEntryPoints, // continuation entry points
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   683
                runtimeScope); // runtime scope
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   684
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   685
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   686
    /**
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   687
     * If the function being compiled already has its own invalidated program points map, use it. Otherwise, attempt to
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   688
     * load invalidated program points map from the persistent type info cache.
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   689
     * @param invalidatedProgramPoints the function's current invalidated program points map. Null if the function
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   690
     * doesn't have it.
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   691
     * @param typeInformationFile the object describing the location of the persisted type information.
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   692
     * @return either the existing map, or a loaded map from the persistent type info cache, or a new empty map if
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   693
     * neither an existing map or a persistent cached type info is available.
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   694
     */
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27206
diff changeset
   695
    @SuppressWarnings("unused")
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   696
    private static Map<Integer, Type> getEffectiveInvalidatedProgramPoints(
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   697
            final Map<Integer, Type> invalidatedProgramPoints, final Object typeInformationFile) {
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   698
        if(invalidatedProgramPoints != null) {
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   699
            return invalidatedProgramPoints;
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   700
        }
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   701
        final Map<Integer, Type> loadedProgramPoints = OptimisticTypesPersistence.load(typeInformationFile);
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   702
        return loadedProgramPoints != null ? loadedProgramPoints : new TreeMap<Integer, Type>();
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   703
    }
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   704
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   705
    private FunctionInitializer compileTypeSpecialization(final MethodType actualCallSiteType, final ScriptObject runtimeScope, final boolean persist) {
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   706
        // We're creating an empty script object for holding local variables. AssignSymbols will populate it with
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   707
        // explicit Undefined values for undefined local variables (see AssignSymbols#defineSymbol() and
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
   708
        // CompilationEnvironment#declareLocalSymbol()).
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
   709
24744
5290da85fc3d 8038426: Move all loggers from process wide scope into Global scope
lagergren
parents: 24740
diff changeset
   710
        if (log.isEnabled()) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   711
            log.info("Parameter type specialization of '", functionName, "' signature: ", actualCallSiteType);
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
   712
        }
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   713
30389
35e1a33f3d12 8078612: Persistent code cache should support more configurations
hannesw
parents: 30056
diff changeset
   714
        final boolean persistentCache = persist && usePersistentCodeCache();
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   715
        String cacheKey = null;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   716
        if (persistentCache) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   717
            final TypeMap typeMap = typeMap(actualCallSiteType);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   718
            final Type[] paramTypes = typeMap == null ? null : typeMap.getParameterTypes(functionNodeId);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   719
            cacheKey = CodeStore.getCacheKey(functionNodeId, paramTypes);
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   720
            final CodeInstaller newInstaller = getInstallerForNewCode();
26891
aee38d04254a 8059346: Single class loader is used to load compiled bytecode
attila
parents: 26887
diff changeset
   721
            final StoredScript script = newInstaller.loadScript(source, cacheKey);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   722
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   723
            if (script != null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   724
                Compiler.updateCompilationId(script.getCompilationId());
30056
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   725
                return script.installFunction(this, newInstaller);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   726
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   727
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   728
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   729
        final FunctionNode fn = reparse();
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   730
        final Compiler compiler = getCompiler(fn, actualCallSiteType, runtimeScope);
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
   731
        final FunctionNode compiledFn = compiler.compile(fn,
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   732
                fn.isCached() ? CompilationPhases.COMPILE_ALL_CACHED : CompilationPhases.COMPILE_ALL);
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   733
32435
cfd619ef23a6 8133300: Ensure symbol table immutability in Nashorn AST
attila
parents: 31827
diff changeset
   734
        if (persist && !compiledFn.hasApplyToCallSpecialization()) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   735
            compiler.persistClassInfo(cacheKey, compiledFn);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   736
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   737
        return new FunctionInitializer(compiledFn, compiler.getInvalidatedProgramPoints());
25236
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   738
    }
fac419f1e889 8046921: Deoptimization type information peristence
attila
parents: 24994
diff changeset
   739
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   740
    boolean usePersistentCodeCache() {
32530
20aa15248117 8135262: Sanitize CodeInstaller API
attila
parents: 32435
diff changeset
   741
        return installer != null && installer.getContext().getEnv()._persistent_cache;
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20946
diff changeset
   742
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20946
diff changeset
   743
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   744
    private MethodType explicitParams(final MethodType callSiteType) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   745
        if (CompiledFunction.isVarArgsType(callSiteType)) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   746
            return null;
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   747
        }
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   748
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   749
        final MethodType noCalleeThisType = callSiteType.dropParameterTypes(0, 2); // (callee, this) is always in call site type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   750
        final int callSiteParamCount = noCalleeThisType.parameterCount();
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   751
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   752
        // Widen parameters of reference types to Object as we currently don't care for specialization among reference
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   753
        // types. E.g. call site saying (ScriptFunction, Object, String) should still link to (ScriptFunction, Object, Object)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   754
        final Class<?>[] paramTypes = noCalleeThisType.parameterArray();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   755
        boolean changed = false;
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
        for (int i = 0; i < paramTypes.length; ++i) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   757
            final Class<?> paramType = paramTypes[i];
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   758
            if (!(paramType.isPrimitive() || paramType == Object.class)) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   759
                paramTypes[i] = Object.class;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   760
                changed = true;
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: 17233
diff changeset
   761
            }
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: 17233
diff changeset
   762
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   763
        final MethodType generalized = changed ? MethodType.methodType(noCalleeThisType.returnType(), paramTypes) : noCalleeThisType;
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: 17233
diff changeset
   764
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   765
        if (callSiteParamCount < getArity()) {
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   766
            return generalized.appendParameterTypes(Collections.<Class<?>>nCopies(getArity() - callSiteParamCount, Object.class));
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   767
        }
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   768
        return generalized;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   769
    }
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   770
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   771
    private FunctionNode extractFunctionFromScript(final FunctionNode script) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   772
        final Set<FunctionNode> fns = new HashSet<>();
32888
24f99be3d5ab 8134502: introduce abstraction for basic NodeVisitor usage
attila
parents: 32696
diff changeset
   773
        script.getBody().accept(new SimpleNodeVisitor() {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   774
            @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   775
            public boolean enterFunctionNode(final FunctionNode fn) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   776
                fns.add(fn);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   777
                return false;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   778
            }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   779
        });
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   780
        assert fns.size() == 1 : "got back more than one method in recompilation";
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   781
        final FunctionNode f = fns.iterator().next();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   782
        assert f.getId() == functionNodeId;
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   783
        if (!getFunctionFlag(FunctionNode.IS_DECLARED) && f.isDeclared()) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   784
            return f.clearFlag(null, FunctionNode.IS_DECLARED);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   785
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   786
        return f;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   787
    }
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: 17233
diff changeset
   788
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   789
    private void logLookup(final boolean shouldLog, final MethodType targetType) {
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   790
        if (shouldLog && log.isEnabled()) {
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   791
            log.info("Looking up ", DebugLogger.quote(functionName), " type=", targetType);
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   792
        }
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   793
    }
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   794
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   795
    private MethodHandle lookup(final FunctionInitializer fnInit, final boolean shouldLog) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   796
        final MethodType type = fnInit.getMethodType();
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   797
        logLookup(shouldLog, type);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   798
        return lookupCodeMethod(fnInit.getCode(), type);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   799
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   800
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   801
    MethodHandle lookup(final FunctionNode fn) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   802
        final MethodType type = new FunctionSignature(fn).getMethodType();
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   803
        logLookup(true, type);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   804
        return lookupCodeMethod(fn.getCompileUnit().getCode(), 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
   805
    }
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
   806
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   807
    MethodHandle lookupCodeMethod(final Class<?> codeClass, final MethodType targetType) {
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26768
diff changeset
   808
        return MH.findStatic(LOOKUP, codeClass, functionName, targetType);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   809
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   810
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   811
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   812
     * Initializes this function data with the eagerly generated version of the code. This method can only be invoked
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   813
     * by the compiler internals in Nashorn and is public for implementation reasons only. Attempting to invoke it
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   814
     * externally will result in an exception.
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   815
     *
30056
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   816
     * @param functionNode FunctionNode for this data
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   817
     */
30056
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   818
    public void initializeCode(final FunctionNode functionNode) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   819
        // Since the method is public, we double-check that we aren't invoked with an inappropriate compile unit.
30056
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   820
        if (!code.isEmpty() || functionNode.getId() != functionNodeId || !functionNode.getCompileUnit().isInitializing(this, functionNode)) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   821
            throw new IllegalStateException(name);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   822
        }
30056
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   823
        addCode(lookup(functionNode), null, null, functionNode.getFlags());
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   824
    }
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   825
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   826
    /**
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   827
     * Initializes this function with the given function code initializer.
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   828
     * @param initializer function code initializer
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   829
     */
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 29406
diff changeset
   830
    void initializeCode(final FunctionInitializer initializer) {
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   831
        addCode(lookup(initializer, true), null, null, initializer.getFlags());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   832
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   833
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   834
    private CompiledFunction addCode(final MethodHandle target, final Map<Integer, Type> invalidatedProgramPoints,
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   835
                                     final MethodType callSiteType, final int fnFlags) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   836
        final CompiledFunction cfn = new CompiledFunction(target, this, invalidatedProgramPoints, callSiteType, fnFlags);
31827
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   837
        assert noDuplicateCode(cfn) : "duplicate code";
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   838
        code.add(cfn);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   839
        return cfn;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   840
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   841
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   842
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   843
     * Add code with specific call site type. It will adapt the type of the looked up method handle to fit the call site
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   844
     * type. This is necessary because even if we request a specialization that takes an "int" parameter, we might end
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   845
     * up getting one that takes a "double" etc. because of internal function logic causes widening (e.g. assignment of
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   846
     * a wider value to the parameter variable). However, we use the method handle type for matching subsequent lookups
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   847
     * for the same specialization, so we must adapt the handle to the expected type.
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   848
     * @param fnInit the function
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   849
     * @param callSiteType the call site type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   850
     * @return the compiled function object, with its type matching that of the call site type.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   851
     */
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   852
    private CompiledFunction addCode(final FunctionInitializer fnInit, final MethodType callSiteType) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   853
        if (isVariableArity()) {
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   854
            return addCode(lookup(fnInit, true), fnInit.getInvalidatedProgramPoints(), callSiteType, fnInit.getFlags());
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: 17233
diff changeset
   855
        }
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: 17233
diff changeset
   856
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   857
        final MethodHandle handle = lookup(fnInit, true);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   858
        final MethodType fromType = handle.type();
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: 24720
diff changeset
   859
        MethodType toType = needsCallee(fromType) ? callSiteType.changeParameterType(0, ScriptFunction.class) : callSiteType.dropParameterTypes(0, 1);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   860
        toType = toType.changeReturnType(fromType.returnType());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   861
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   862
        final int toCount = toType.parameterCount();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   863
        final int fromCount = fromType.parameterCount();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   864
        final int minCount = Math.min(fromCount, toCount);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   865
        for(int i = 0; i < minCount; ++i) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   866
            final Class<?> fromParam = fromType.parameterType(i);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   867
            final Class<?>   toParam =   toType.parameterType(i);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   868
            // If method has an Object parameter, but call site had String, preserve it as Object. No need to narrow it
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   869
            // artificially. Note that this is related to how CompiledFunction.matchesCallSite() works, specifically
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   870
            // the fact that various reference types compare to equal (see "fnType.isEquivalentTo(csType)" there).
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   871
            if (fromParam != toParam && !fromParam.isPrimitive() && !toParam.isPrimitive()) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   872
                assert fromParam.isAssignableFrom(toParam);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   873
                toType = toType.changeParameterType(i, fromParam);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   874
            }
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: 17233
diff changeset
   875
        }
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   876
        if (fromCount > toCount) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   877
            toType = toType.appendParameterTypes(fromType.parameterList().subList(toCount, fromCount));
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   878
        } else if (fromCount < toCount) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   879
            toType = toType.dropParameterTypes(fromCount, toCount);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   880
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   881
27529
eafb7de4648e 8063036: Various pretty printing issues with --log=recompile
lagergren
parents: 27365
diff changeset
   882
        return addCode(lookup(fnInit, false).asType(toType), fnInit.getInvalidatedProgramPoints(), callSiteType, fnInit.getFlags());
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20946
diff changeset
   883
    }
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20946
diff changeset
   884
27105
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   885
    /**
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   886
     * Returns the return type of a function specialization for particular parameter types.<br>
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   887
     * <b>Be aware that the way this is implemented, it forces full materialization (compilation and installation) of
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   888
     * code for that specialization.</b>
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   889
     * @param callSiteType the parameter types at the call site. It must include the mandatory {@code callee} and
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   890
     * {@code this} parameters, so it needs to start with at least {@code ScriptFunction.class} and
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   891
     * {@code Object.class} class. Since the return type of the function is calculated from the code itself, it is
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   892
     * irrelevant and should be set to {@code Object.class}.
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   893
     * @param runtimeScope a current runtime scope. Can be null but when it's present it will be used as a source of
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   894
     * current runtime values that can improve the compiler's type speculations (and thus reduce the need for later
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   895
     * recompilations) if the specialization is not already present and thus needs to be freshly compiled.
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   896
     * @return the return type of the function specialization.
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   897
     */
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   898
    public Class<?> getReturnType(final MethodType callSiteType, final ScriptObject runtimeScope) {
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   899
        return getBest(callSiteType, runtimeScope, CompiledFunction.NO_FUNCTIONS).type().returnType();
ef61cca021aa 8060241: Immediately invoked function expressions cause lot of deoptimization
attila
parents: 26891
diff changeset
   900
    }
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   901
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   902
    @Override
34736
52e9e61d937f 8145550: Megamorphic invoke should use CompiledFunction variants without any LinkLogic
sundar
parents: 33533
diff changeset
   903
    synchronized CompiledFunction getBest(final MethodType callSiteType, final ScriptObject runtimeScope, final Collection<CompiledFunction> forbidden, final boolean linkLogicOkay) {
31827
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   904
        assert isValidCallSite(callSiteType) : callSiteType;
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   905
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   906
        CompiledFunction existingBest = pickFunction(callSiteType, false);
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   907
        if (existingBest == null) {
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   908
            existingBest = pickFunction(callSiteType, true); // try vararg last
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   909
        }
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   910
        if (existingBest == null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   911
            existingBest = addCode(compileTypeSpecialization(callSiteType, runtimeScope, true), callSiteType);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   912
        }
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   913
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   914
        assert existingBest != null;
23767
7c0614b75e23 8038638: Persistent store for compiled scripts
hannesw
parents: 23372
diff changeset
   915
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   916
        //if the best one is an apply to call, it has to match the callsite exactly
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   917
        //or we need to regenerate
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   918
        if (existingBest.isApplyToCall()) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   919
            final CompiledFunction best = lookupExactApplyToCall(callSiteType);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   920
            if (best != null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   921
                return best;
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   922
            }
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   923
27365
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
   924
            // special case: we had an apply to call, but we failed to make it fit.
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
   925
            // Try to generate a specialized one for this callsite. It may
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
   926
            // be another apply to call specialization, or it may not, but whatever
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
   927
            // it is, it is a specialization that is guaranteed to fit
31827
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
   928
            existingBest = addCode(compileTypeSpecialization(callSiteType, runtimeScope, false), callSiteType);
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   929
        }
24740
26791be09688 8040089: Apply to call transform was incomplete. Now passes all tests and performance is back
lagergren
parents: 24738
diff changeset
   930
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   931
        return existingBest;
23372
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   932
    }
09707b3e5fb0 8021350: Share script classes between threads/globals within context
hannesw
parents: 23084
diff changeset
   933
23084
6c5c02d1023a 8035948: Redesign property listeners for shared classes
hannesw
parents: 20946
diff changeset
   934
    @Override
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   935
    public boolean needsCallee() {
26503
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   936
        return getFunctionFlag(FunctionNode.NEEDS_CALLEE);
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   937
    }
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   938
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   939
    /**
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   940
     * Returns the {@link FunctionNode} flags associated with this function data.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   941
     * @return the {@link FunctionNode} flags associated with this function data.
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   942
     */
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   943
    public int getFunctionFlags() {
3ed48a01100c 8057148: Skip nested functions on reparse
attila
parents: 26377
diff changeset
   944
        return functionFlags;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   945
    }
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: 17233
diff changeset
   946
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
   947
    @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   948
    MethodType getGenericType() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   949
        // 2 is for (callee, this)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   950
        if (isVariableArity()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   951
            return MethodType.genericMethodType(2, true);
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   952
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   953
        return MethodType.genericMethodType(2 + getArity());
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   954
    }
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   955
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   956
    /**
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   957
     * Return the function node id.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   958
     * @return the function node id
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   959
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   960
    public int getFunctionNodeId() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   961
        return functionNodeId;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   962
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   963
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   964
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   965
     * Get the source for the script
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   966
     * @return source
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents: 26764
diff changeset
   967
     */
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   968
    public Source getSource() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   969
        return source;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   970
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   971
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25821
diff changeset
   972
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   973
     * Return a script function data based on a function id, either this function if
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: 24720
diff changeset
   974
     * the id matches or a nested function based on functionId. This goes down into
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: 24720
diff changeset
   975
     * nested functions until all leaves are exhausted.
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: 24720
diff changeset
   976
     *
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   977
     * @param functionId function id
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   978
     * @return script function data or null if invalid id
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   979
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   980
    public RecompilableScriptFunctionData getScriptFunctionData(final int functionId) {
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: 24720
diff changeset
   981
        if (functionId == functionNodeId) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 20946
diff changeset
   982
            return this;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   983
        }
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: 24720
diff changeset
   984
        RecompilableScriptFunctionData data;
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: 17233
diff changeset
   985
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: 24720
diff changeset
   986
        data = nestedFunctions == null ? null : nestedFunctions.get(functionId);
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: 24720
diff changeset
   987
        if (data != null) {
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: 24720
diff changeset
   988
            return data;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   989
        }
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: 24720
diff changeset
   990
        for (final RecompilableScriptFunctionData ndata : nestedFunctions.values()) {
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: 24720
diff changeset
   991
            data = ndata.getScriptFunctionData(functionId);
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: 24720
diff changeset
   992
            if (data != null) {
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: 24720
diff changeset
   993
                return data;
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: 17233
diff changeset
   994
            }
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   995
        }
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: 24720
diff changeset
   996
        return null;
17756
daaa1e643f71 8006069: Range analysis first iteration, runtime specializations
lagergren
parents: 17523
diff changeset
   997
    }
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: 17233
diff changeset
   998
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
   999
    /**
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1000
     * Check whether a certain name is a global symbol, i.e. only exists as defined
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1001
     * in outermost scope and not shadowed by being parameter or assignment in inner
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1002
     * scopes
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1003
     *
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1004
     * @param functionNode function node to check
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1005
     * @param symbolName symbol name
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1006
     * @return true if global symbol
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1007
     */
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1008
    public boolean isGlobalSymbol(final FunctionNode functionNode, final String symbolName) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1009
        RecompilableScriptFunctionData data = getScriptFunctionData(functionNode.getId());
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1010
        assert data != null;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1011
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
  1012
        do {
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1013
            if (data.hasInternalSymbol(symbolName)) {
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1014
                return false;
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1015
            }
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1016
            data = data.getParent();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24749
diff changeset
  1017
        } while(data != null);
24738
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1018
be2026c9717c 8039746: Transform applies to calls wherever possible, for ScriptFunctions and JSObjects.
lagergren
parents: 24735
diff changeset
  1019
        return 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
  1020
    }
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
  1021
27206
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1022
    /**
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1023
     * Restores the {@link #getFunctionFlags()} flags to a function node. During on-demand compilation, we might need
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1024
     * to restore flags to a function node that was otherwise not subjected to a full compile pipeline (e.g. its parse
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1025
     * was skipped, or it's a nested function of a deserialized function.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1026
     * @param lc current lexical context
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1027
     * @param fn the function node to restore flags onto
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1028
     * @return the transformed function node
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1029
     */
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1030
    public FunctionNode restoreFlags(final LexicalContext lc, final FunctionNode fn) {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1031
        assert fn.getId() == functionNodeId;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1032
        FunctionNode newFn = fn.setFlags(lc, functionFlags);
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1033
        // This compensates for missing markEval() in case the function contains an inner function
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1034
        // that contains eval(), that now we didn't discover since we skipped the inner function.
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1035
        if (newFn.hasNestedEval()) {
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1036
            assert newFn.hasScopeBlock();
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1037
            newFn = newFn.setBody(lc, newFn.getBody().setNeedsScope(null));
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1038
        }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1039
        return newFn;
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1040
    }
d4a707c9db5a 8059844: Implement optimistic splitter
attila
parents: 27105
diff changeset
  1041
31827
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1042
    // Make sure code does not contain a compiled function with the same signature as compiledFunction
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1043
    private boolean noDuplicateCode(final CompiledFunction compiledFunction) {
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1044
        for (final CompiledFunction cf : code) {
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1045
            if (cf.type().equals(compiledFunction.type())) {
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1046
                return false;
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1047
            }
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1048
        }
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1049
        return true;
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1050
    }
c9984afe4a29 8131340: Varargs function is recompiled each time it is linked
hannesw
parents: 30393
diff changeset
  1051
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1052
    private void writeObject(final ObjectOutputStream out) throws IOException {
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1053
        final Object localCachedAst = cachedAst;
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1054
        out.defaultWriteObject();
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1055
        // We need to persist SerializedAst for split functions as they can't reparse the source code.
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1056
        if (localCachedAst instanceof SerializedAst) {
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1057
            out.writeObject(localCachedAst);
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1058
        } else {
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1059
            out.writeObject(null);
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1060
        }
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1061
    }
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1062
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24769
diff changeset
  1063
    private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
24769
attila
parents: 24759 23767
diff changeset
  1064
        in.defaultReadObject();
43117
2027548da50f 8166187: Regression: NPE during reparse when using persistent code cache and optimistic types
hannesw
parents: 39077
diff changeset
  1065
        cachedAst = in.readObject();
24769
attila
parents: 24759 23767
diff changeset
  1066
        createLogger();
attila
parents: 24759 23767
diff changeset
  1067
    }
attila
parents: 24759 23767
diff changeset
  1068
attila
parents: 24759 23767
diff changeset
  1069
    private void createLogger() {
attila
parents: 24759 23767
diff changeset
  1070
        log = initLogger(Context.getContextTrusted());
attila
parents: 24759 23767
diff changeset
  1071
    }
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
  1072
}