src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/FunctionInitializer.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 30056 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/FunctionInitializer.java@06d201382b55
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     1
/*
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     4
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    10
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    15
 * accompanied this code).
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    16
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    20
 *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    23
 * questions.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    24
 */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    25
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    27
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    28
import java.io.IOException;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    29
import java.io.ObjectInputStream;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    30
import java.io.ObjectOutputStream;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    31
import java.io.Serializable;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    32
import java.lang.invoke.MethodType;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    33
import java.util.Map;
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
    34
import jdk.nashorn.internal.codegen.CompileUnit;
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
    35
import jdk.nashorn.internal.codegen.FunctionSignature;
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
    36
import jdk.nashorn.internal.codegen.types.Type;
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
    37
import jdk.nashorn.internal.ir.FunctionNode;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    38
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    39
/**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    40
 * Class that contains information allowing us to look up a method handle implementing a JavaScript function
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    41
 * from a generated class. This is used both for code coming from codegen and for persistent serialized code.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    42
 */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    43
public final class FunctionInitializer implements Serializable {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    44
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    45
    private final String className;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    46
    private final MethodType methodType;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    47
    private final int flags;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    48
    private transient Map<Integer, Type> invalidatedProgramPoints;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    49
    private transient Class<?> code;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    50
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    51
    private static final long serialVersionUID = -5420835725902966692L;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    52
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    53
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    54
     * Constructor.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    55
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    56
     * @param functionNode the function node
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    57
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    58
    public FunctionInitializer(final FunctionNode functionNode) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    59
        this(functionNode, null);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    60
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    61
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    62
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    63
     * Constructor.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    64
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    65
     * @param functionNode the function node
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    66
     * @param invalidatedProgramPoints invalidated program points
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    67
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    68
    public FunctionInitializer(final FunctionNode functionNode, final Map<Integer, Type> invalidatedProgramPoints) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    69
        this.className  = functionNode.getCompileUnit().getUnitClassName();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    70
        this.methodType = new FunctionSignature(functionNode).getMethodType();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    71
        this.flags = functionNode.getFlags();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    72
        this.invalidatedProgramPoints = invalidatedProgramPoints;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    73
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    74
        final CompileUnit cu = functionNode.getCompileUnit();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    75
        if (cu != null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    76
            this.code = cu.getCode();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    77
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    78
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    79
        assert className != null;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    80
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    81
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    82
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    83
     * Returns the name of the class implementing the function.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    84
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    85
     * @return the class name
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    86
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    87
    public String getClassName() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    88
        return className;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    89
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    90
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    91
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    92
     * Returns the type of the method implementing the function.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    93
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    94
     * @return the method type
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    95
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    96
    public MethodType getMethodType() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    97
        return methodType;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    98
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
    99
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   100
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   101
     * Returns the function flags.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   102
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   103
     * @return function flags
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   104
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   105
    public int getFlags() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   106
        return flags;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   107
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   108
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   109
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   110
     * Returns the class implementing the function.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   111
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   112
     * @return the class
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   113
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   114
    public Class<?> getCode() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   115
        return code;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   116
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   117
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   118
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   119
     * Set the class implementing the function
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   120
     * @param code the class
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   121
     */
30056
06d201382b55 8053905: Eager code generation fails for earley boyer with split threshold set to 1000
hannesw
parents: 26764
diff changeset
   122
    void setCode(final Class<?> code) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   123
        // Make sure code has not been set and has expected class name
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   124
        if (this.code != null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   125
            throw new IllegalStateException("code already set");
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   126
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   127
        assert className.equals(code.getTypeName().replace('.', '/')) : "unexpected class name";
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   128
        this.code = code;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   129
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   130
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   131
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   132
     * Returns the map of invalidated program points.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   133
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   134
     * @return invalidated program points
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   135
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   136
    public Map<Integer, Type> getInvalidatedProgramPoints() {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   137
        return invalidatedProgramPoints;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   138
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   139
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   140
    private void writeObject(final ObjectOutputStream out) throws IOException {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   141
        out.defaultWriteObject();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   142
        Type.writeTypeMap(invalidatedProgramPoints, out);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   143
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   144
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   145
    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   146
        in.defaultReadObject();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   147
        invalidatedProgramPoints = Type.readTypeMap(in);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   148
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents:
diff changeset
   149
}