nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Specialization.java
author hannesw
Fri, 25 Nov 2016 14:20:24 +0100
changeset 42376 8604f1a50c30
parent 26768 751b0f427090
permissions -rw-r--r--
8170322: Specialized functions convert booleans to numbers Reviewed-by: jlaskey, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     1
/*
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     4
 *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    10
 *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    15
 * accompanied this code).
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    16
 *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    20
 *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    23
 * questions.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    24
 */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    25
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    27
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    28
import java.lang.invoke.MethodHandle;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    29
import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    30
import jdk.nashorn.internal.objects.annotations.SpecializedFunction.LinkLogic;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    31
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    32
/**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    33
 * Specialization info for a {@link SpecializedFunction}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    34
 */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    35
public final class Specialization {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    36
    private final MethodHandle mh;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    37
    private final Class<? extends LinkLogic> linkLogicClass;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    38
    private final boolean isOptimistic;
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    39
    private final boolean convertsNumericArgs;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    40
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    41
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    42
     * Constructor
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    43
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    44
     * @param mh  invoker method handler
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    45
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    46
    public Specialization(final MethodHandle mh) {
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    47
        this(mh, false, true);
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    48
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    49
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    50
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    51
     * Constructor
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    52
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    53
     * @param mh  invoker method handler
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    54
     * @param isOptimistic is this an optimistic native method, i.e. can it throw {@link UnwarrantedOptimismException}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    55
     *   which would have to lead to a relink and return value processing
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    56
     * @param convertsNumericArgs true if it is safe to convert arguments to numbers
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    57
     */
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    58
    public Specialization(final MethodHandle mh, final boolean isOptimistic, final boolean convertsNumericArgs) {
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    59
        this(mh, null, isOptimistic, convertsNumericArgs);
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    60
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    61
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    62
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    63
     * Constructor
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    64
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    65
     * @param mh  invoker method handler
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    66
     * @param linkLogicClass extra link logic needed for this function. Instances of this class also contains logic for checking
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    67
     *  if this can be linked on its first encounter, which is needed as per our standard linker semantics
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    68
     * @param isOptimistic is this an optimistic native method, i.e. can it throw {@link UnwarrantedOptimismException}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    69
     *   which would have to lead to a relink and return value processing
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    70
     * @param convertsNumericArgs true if it is safe to convert arguments to numbers
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    71
     */
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    72
    public Specialization(final MethodHandle mh, final Class<? extends LinkLogic> linkLogicClass,
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    73
                          final boolean isOptimistic, final boolean convertsNumericArgs) {
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    74
        this.mh             = mh;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    75
        this.isOptimistic   = isOptimistic;
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
    76
        this.convertsNumericArgs = convertsNumericArgs;
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    77
        if (linkLogicClass != null) {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    78
            //null out the "empty" link logic class for optimization purposes
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    79
            //we only use the empty instance because we can't default class annotations
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    80
            //to null
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    81
            this.linkLogicClass = LinkLogic.isEmpty(linkLogicClass) ? null : linkLogicClass;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    82
        } else {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    83
            this.linkLogicClass = null;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    84
        }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    85
     }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    86
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    87
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    88
     * Get the method handle for the invoker of this ScriptFunction
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    89
     * @return the method handle
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    90
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    91
    public MethodHandle getMethodHandle() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    92
        return mh;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    93
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    94
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    95
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    96
     * Get the link logic class for this ScriptFunction
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    97
     * @return link logic class info, i.e. one whose instance contains stuff like
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    98
     *  "do we need exception check for every call", and logic to check if we may link
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
    99
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   100
    public Class<? extends LinkLogic> getLinkLogicClass() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   101
        return linkLogicClass;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   102
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   103
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   104
    /**
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   105
     * An optimistic specialization is one that can throw UnwarrantedOptimismException.
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   106
     * This is allowed for native methods, as long as they are functional, i.e. don't change
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   107
     * any state between entering and throwing the UOE. Then we can re-execute a wider version
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   108
     * of the method in the continuation. Rest-of method generation for optimistic builtins is
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   109
     * of course not possible, but this approach works and fits into the same relinking
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   110
     * framework
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   111
     *
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   112
     * @return true if optimistic
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   113
     */
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   114
    public boolean isOptimistic() {
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   115
        return isOptimistic;
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   116
    }
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   117
42376
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   118
    /**
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   119
     * Check if this function converts arguments for numeric parameters to numbers
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   120
     * so it's safe to pass booleans as 0 and 1
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   121
     *
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   122
     * @return true if it is safe to convert arguments to numbers
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   123
     */
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   124
    public boolean convertsNumericArgs() {
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   125
        return convertsNumericArgs;
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   126
    }
8604f1a50c30 8170322: Specialized functions convert booleans to numbers
hannesw
parents: 26768
diff changeset
   127
26768
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   128
}
751b0f427090 8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
diff changeset
   129