src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/RewriteException.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     1
/*
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     4
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    10
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    15
 * accompanied this code).
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    16
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    20
 *
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    23
 * questions.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    24
 */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    25
package jdk.nashorn.internal.runtime;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    26
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    27
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    28
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    29
import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCallNoLookup;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    30
24776
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
    31
import java.io.NotSerializableException;
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
    32
import java.io.ObjectInputStream;
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
    33
import java.io.ObjectOutputStream;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    34
import java.lang.invoke.CallSite;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    35
import java.lang.invoke.ConstantCallSite;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    36
import java.lang.invoke.MethodHandle;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    37
import java.lang.invoke.MethodHandles;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    38
import java.lang.invoke.MethodHandles.Lookup;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    39
import java.lang.invoke.MethodType;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    40
import java.lang.reflect.Array;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    41
import java.util.Arrays;
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    42
import jdk.nashorn.internal.codegen.CompilerConstants;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    43
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    44
import jdk.nashorn.internal.codegen.types.Type;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    45
import jdk.nashorn.internal.lookup.MethodHandleFactory;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    46
import jdk.nashorn.internal.lookup.MethodHandleFunctionality;
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
    47
import jdk.nashorn.internal.objects.Global;
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
    48
import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    49
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    50
/**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    51
 * Used to signal to the linker to relink the callee
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    52
 */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    53
@SuppressWarnings("serial")
24776
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
    54
public final class RewriteException extends Exception {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    55
    private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    56
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
    57
    // Runtime scope in effect at the time of the compilation. Used to evaluate types of expressions and prevent overly
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
    58
    // optimistic assumptions (which will lead to unnecessary deoptimizing recompilations).
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
    59
    private ScriptObject runtimeScope;
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
    60
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    61
    // Contents of bytecode slots
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    62
    private Object[] byteCodeSlots;
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
    63
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    64
    private final int[] previousContinuationEntryPoints;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    65
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    66
    /** Call for getting the contents of the bytecode slots in the exception */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    67
    public static final Call GET_BYTECODE_SLOTS       = virtualCallNoLookup(RewriteException.class, "getByteCodeSlots", Object[].class);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    68
    /** Call for getting the program point in the exception */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    69
    public static final Call GET_PROGRAM_POINT        = virtualCallNoLookup(RewriteException.class, "getProgramPoint", int.class);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    70
    /** Call for getting the return value for the exception */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    71
    public static final Call GET_RETURN_VALUE         = virtualCallNoLookup(RewriteException.class, "getReturnValueDestructive", Object.class);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    72
    /** Call for the populate array bootstrap */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    73
    public static final Call BOOTSTRAP                = staticCallNoLookup(RewriteException.class, "populateArrayBootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    74
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    75
    /** Call for populating an array with local variable state */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    76
    private static final Call POPULATE_ARRAY           = staticCall(MethodHandles.lookup(), RewriteException.class, "populateArray", Object[].class, Object[].class, int.class, Object[].class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
    77
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    78
    /** Call for converting an array to a long array. */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    79
    public static final Call TO_LONG_ARRAY   = staticCallNoLookup(RewriteException.class, "toLongArray",   long[].class, Object.class, RewriteException.class);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    80
    /** Call for converting an array to a double array. */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    81
    public static final Call TO_DOUBLE_ARRAY = staticCallNoLookup(RewriteException.class, "toDoubleArray", double[].class, Object.class, RewriteException.class);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    82
    /** Call for converting an array to an object array. */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    83
    public static final Call TO_OBJECT_ARRAY = staticCallNoLookup(RewriteException.class, "toObjectArray", Object[].class, Object.class, RewriteException.class);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    84
    /** Call for converting an object to null if it can't be represented as an instance of a class. */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    85
    public static final Call INSTANCE_OR_NULL = staticCallNoLookup(RewriteException.class, "instanceOrNull", Object.class, Object.class, Class.class);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    86
    /** Call for asserting the length of an array. */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    87
    public static final Call ASSERT_ARRAY_LENGTH = staticCallNoLookup(RewriteException.class, "assertArrayLength", void.class, Object[].class, int.class);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
    88
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    89
    private RewriteException(
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    90
            final UnwarrantedOptimismException e,
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    91
            final Object[] byteCodeSlots,
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    92
            final String[] byteCodeSymbolNames,
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    93
            final int[] previousContinuationEntryPoints) {
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    94
        super("", e, false, Context.DEBUG);
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    95
        this.byteCodeSlots = byteCodeSlots;
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    96
        this.runtimeScope = mergeSlotsWithScope(byteCodeSlots, byteCodeSymbolNames);
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    97
        this.previousContinuationEntryPoints = previousContinuationEntryPoints;
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    98
    }
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
    99
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   100
    /**
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
   101
     * Constructor for a rewrite exception thrown from an optimistic function.
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
   102
     * @param e the {@link UnwarrantedOptimismException} that triggered this exception.
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
   103
     * @param byteCodeSlots contents of local variable slots at the time of rewrite at the program point
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   104
     * @param byteCodeSymbolNames the names of the variables in the {@code byteCodeSlots} parameter. The array might
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   105
     * have less elements, and some elements might be unnamed (the name can be null). The information is provided in an
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   106
     * effort to assist evaluation of expressions for their types by the compiler doing the deoptimizing recompilation,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   107
     * and can thus be incomplete - the more complete it is, the more expressions can be evaluated by the compiler, and
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   108
     * the more unnecessary deoptimizing compilations can be avoided.
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
   109
     * @return a new rewrite exception
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
   110
     */
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
   111
    public static RewriteException create(final UnwarrantedOptimismException e,
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
   112
            final Object[] byteCodeSlots,
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   113
            final String[] byteCodeSymbolNames) {
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
   114
        return create(e, byteCodeSlots, byteCodeSymbolNames, null);
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
   115
    }
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
   116
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
   117
    /**
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
   118
     * Constructor for a rewrite exception thrown from a rest-of method.
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
   119
     * @param e the {@link UnwarrantedOptimismException} that triggered this exception.
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
   120
     * @param byteCodeSlots contents of local variable slots at the time of rewrite at the program point
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   121
     * @param byteCodeSymbolNames the names of the variables in the {@code byteCodeSlots} parameter. The array might
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   122
     * have less elements, and some elements might be unnamed (the name can be null). The information is provided in an
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   123
     * effort to assist evaluation of expressions for their types by the compiler doing the deoptimizing recompilation,
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   124
     * and can thus be incomplete - the more complete it is, the more expressions can be evaluated by the compiler, and
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   125
     * the more unnecessary deoptimizing compilations can be avoided.
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
   126
     * @param previousContinuationEntryPoints an array of continuation entry points that were already executed during
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
   127
     * one logical invocation of the function (a rest-of triggering a rest-of triggering a...)
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
   128
     * @return a new rewrite exception
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
   129
     */
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
   130
    public static RewriteException create(final UnwarrantedOptimismException e,
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
   131
            final Object[] byteCodeSlots,
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
   132
            final String[] byteCodeSymbolNames,
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
   133
            final int[] previousContinuationEntryPoints) {
24773
34c21f8464c5 8044171: Make optimistic exception handlers smaller
attila
parents: 24759
diff changeset
   134
        return new RewriteException(e, byteCodeSlots, byteCodeSymbolNames, previousContinuationEntryPoints);
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
   135
    }
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
   136
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
   137
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   138
     * Bootstrap method for populate array
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   139
     * @param lookup     lookup
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   140
     * @param name       name (ignored)
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   141
     * @param type       method type for signature
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   142
     * @param startIndex start index to start writing to
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   143
     * @return callsite to array populator (constant)
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   144
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   145
    public static CallSite populateArrayBootstrap(final MethodHandles.Lookup lookup, final String name, final MethodType type, final int startIndex) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   146
        MethodHandle mh = POPULATE_ARRAY.methodHandle();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   147
        mh = MH.insertArguments(mh, 1, startIndex);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   148
        mh = MH.asCollector(mh, Object[].class, type.parameterCount() - 1);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   149
        mh = MH.asType(mh, type);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   150
        return new ConstantCallSite(mh);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   151
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   152
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   153
    private static ScriptObject mergeSlotsWithScope(final Object[] byteCodeSlots, final String[] byteCodeSymbolNames) {
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   154
        final ScriptObject locals = Global.newEmptyInstance();
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   155
        final int l = Math.min(byteCodeSlots.length, byteCodeSymbolNames.length);
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   156
        ScriptObject runtimeScope = null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   157
        final String scopeName = CompilerConstants.SCOPE.symbolName();
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   158
        for(int i = 0; i < l; ++i) {
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   159
            final String name = byteCodeSymbolNames[i];
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   160
            final Object value = byteCodeSlots[i];
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   161
            if(scopeName.equals(name)) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   162
                assert runtimeScope == null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   163
                runtimeScope = (ScriptObject)value;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   164
            } else if(name != null) {
26765
97501edd2979 8047764: Indexed or polymorphic set on global affects Object.prototype
hannesw
parents: 25865
diff changeset
   165
                locals.set(name, value, NashornCallSiteDescriptor.CALLSITE_STRICT);
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   166
            }
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   167
        }
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   168
        locals.setProto(runtimeScope);
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   169
        return locals;
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   170
    }
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   171
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   172
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   173
     * Array populator used for saving the local variable state into the array contained in the
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   174
     * RewriteException
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   175
     * @param arrayToBePopluated array to be populated
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   176
     * @param startIndex start index to write to
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   177
     * @param items items with which to populate the array
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   178
     * @return the populated array - same array object
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   179
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   180
    public static Object[] populateArray(final Object[] arrayToBePopluated, final int startIndex, final Object[] items) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   181
        System.arraycopy(items, 0, arrayToBePopluated, startIndex, items.length);
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   182
        return arrayToBePopluated;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   183
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   184
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   185
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   186
     * Continuation handler calls this method when a local variable carried over into the continuation is expected to be
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   187
     * a long array in the continued method. Normally, it will also be a long array in the original (interrupted by
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   188
     * deoptimization) method, but it can actually be an int array that underwent widening in the new code version.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   189
     * @param obj the object that has to be converted into a long array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   190
     * @param e the exception being processed
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   191
     * @return a long array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   192
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   193
    public static long[] toLongArray(final Object obj, final RewriteException e) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   194
        if(obj instanceof long[]) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   195
            return (long[])obj;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   196
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   197
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   198
        assert obj instanceof int[];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   199
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   200
        final int[] in = (int[])obj;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   201
        final long[] out = new long[in.length];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   202
        for(int i = 0; i < in.length; ++i) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   203
            out[i] = in[i];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   204
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   205
        return e.replaceByteCodeValue(in, out);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   206
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   207
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   208
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   209
     * Continuation handler calls this method when a local variable carried over into the continuation is expected to be
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   210
     * a double array in the continued method. Normally, it will also be a double array in the original (interrupted by
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   211
     * deoptimization) method, but it can actually be an int or long array that underwent widening in the new code version.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   212
     * @param obj the object that has to be converted into a double array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   213
     * @param e the exception being processed
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   214
     * @return a double array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   215
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   216
    public static double[] toDoubleArray(final Object obj, final RewriteException e) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   217
        if(obj instanceof double[]) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   218
            return (double[])obj;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   219
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   220
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   221
        assert obj instanceof int[] || obj instanceof long[];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   222
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   223
        final int l = Array.getLength(obj);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   224
        final double[] out = new double[l];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   225
        for(int i = 0; i < l; ++i) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   226
            out[i] = Array.getDouble(obj, i);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   227
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   228
        return e.replaceByteCodeValue(obj, out);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   229
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   230
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   231
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   232
     * Continuation handler calls this method when a local variable carried over into the continuation is expected to be
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   233
     * an Object array in the continued method. Normally, it will also be an Object array in the original (interrupted by
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   234
     * deoptimization) method, but it can actually be an int, long, or double array that underwent widening in the new
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   235
     * code version.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   236
     * @param obj the object that has to be converted into an Object array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   237
     * @param e the exception being processed
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   238
     * @return an Object array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   239
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   240
    public static Object[] toObjectArray(final Object obj, final RewriteException e) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   241
        if(obj instanceof Object[]) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   242
            return (Object[])obj;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   243
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   244
24759
31aed7d9c02a 8034206: Make parts of code pipeline reusable in order to facilitate faster warmup and faster lazy compilation.
lagergren
parents: 24751
diff changeset
   245
        assert obj instanceof int[] || obj instanceof long[] || obj instanceof double[] : obj + " is " + obj.getClass().getName();
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   246
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   247
        final int l = Array.getLength(obj);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   248
        final Object[] out = new Object[l];
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   249
        for(int i = 0; i < l; ++i) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   250
            out[i] = Array.get(obj, i);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   251
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   252
        return e.replaceByteCodeValue(obj, out);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   253
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   254
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   255
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   256
     * Continuation handler calls this method when a local variable carried over into the continuation is expected to
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   257
     * have a certain type, but the value can have a different type coming from the deoptimized method as it was a dead
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   258
     * store. If we had precise liveness analysis, we wouldn't need this.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   259
     * @param obj the object inspected for being of a particular type
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   260
     * @param clazz the type the object must belong to
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   261
     * @return the object if it belongs to the type, or null otherwise
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   262
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   263
    public static Object instanceOrNull(final Object obj, final Class<?> clazz) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   264
        return clazz.isInstance(obj) ? obj : null;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   265
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   266
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   267
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   268
     * Asserts the length of an array. Invoked from continuation handler only when running with assertions enabled.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   269
     * The array can, in fact, have more elements than asserted, but they must all have Undefined as their value. The
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   270
     * method does not test for the array having less elements than asserted, as those would already have caused an
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   271
     * {@code ArrayIndexOutOfBoundsException} to be thrown as the continuation handler attempts to access the missing
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   272
     * elements.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   273
     * @param arr the array
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   274
     * @param length the asserted length
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   275
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   276
    public static void assertArrayLength(final Object[] arr, final int length) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   277
        for(int i = arr.length; i-- > length;) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   278
            if(arr[i] != ScriptRuntime.UNDEFINED) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   279
                throw new AssertionError(String.format("Expected array length %d, but it is %d", length, i + 1));
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   280
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   281
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   282
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   283
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   284
    private <T> T replaceByteCodeValue(final Object in, final T out) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   285
        for(int i = 0; i < byteCodeSlots.length; ++i) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   286
            if(byteCodeSlots[i] == in) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   287
                byteCodeSlots[i] = out;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   288
            }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   289
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   290
        return out;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   291
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   292
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   293
    private UnwarrantedOptimismException getUOE() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   294
        return (UnwarrantedOptimismException)getCause();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   295
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   296
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   297
     * Get return value. This method is destructive, after it is invoked subsequent invocation of either
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   298
     * {@link #getByteCodeSlots()} or this method will return null. This method is invoked from the generated
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   299
     * continuation code as the last step before continuing the execution, and we need to make sure we don't hang on to
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   300
     * either the entry bytecode slot values or the return value and prevent them from being garbage collected.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   301
     * @return return value
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   302
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   303
    public Object getReturnValueDestructive() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   304
        assert byteCodeSlots != null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   305
        byteCodeSlots = null;
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   306
        runtimeScope = null;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   307
        return getUOE().getReturnValueDestructive();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   308
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   309
24742
a9afb384e654 8040655: When processing a RewriteException debug object, the return value has already been reset to null. We need to catch this value before that.
lagergren
parents: 24733
diff changeset
   310
    Object getReturnValueNonDestructive() {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   311
        return getUOE().getReturnValueNonDestructive();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   312
    }
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
   313
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   314
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   315
     * Get return type
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   316
     * @return return type
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   317
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   318
    public Type getReturnType() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   319
        return getUOE().getReturnType();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   320
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   321
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   322
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   323
     * Get the program point.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   324
     * @return program point.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   325
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   326
    public int getProgramPoint() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   327
        return getUOE().getProgramPoint();
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   328
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   329
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   330
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   331
     * Get the bytecode slot contents.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   332
     * @return bytecode slot contents.
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   333
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   334
    public Object[] getByteCodeSlots() {
25254
a56c2713f1a8 8049223: RewriteException class exposes public mutable arrays
attila
parents: 24776
diff changeset
   335
        return byteCodeSlots == null ? null : byteCodeSlots.clone();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   336
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   337
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   338
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   339
     * @return an array of continuation entry points that were already executed during one logical invocation of the
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   340
     * function (a rest-of triggering a rest-of triggering a...)
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   341
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   342
    public int[] getPreviousContinuationEntryPoints() {
25254
a56c2713f1a8 8049223: RewriteException class exposes public mutable arrays
attila
parents: 24776
diff changeset
   343
        return previousContinuationEntryPoints == null ? null : previousContinuationEntryPoints.clone();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   344
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   345
24729
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   346
    /**
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   347
     * Returns the runtime scope that was in effect when the exception was thrown.
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   348
     * @return the runtime scope.
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   349
     */
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   350
    public ScriptObject getRuntimeScope() {
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   351
        return runtimeScope;
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   352
    }
2b13051f2122 8037534: Use scope types to determine optimistic types
attila
parents: 24719
diff changeset
   353
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   354
    private static String stringify(final Object returnValue) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   355
        if (returnValue == null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   356
            return "null";
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   357
        }
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
   358
        String str = returnValue.toString();
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
   359
        if (returnValue instanceof String) {
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
   360
            str = '\'' + str + '\'';
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24742
diff changeset
   361
        } else if (returnValue instanceof Double) {
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 26765
diff changeset
   362
            str += 'd';
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
   363
        } else if (returnValue instanceof Long) {
31549
b627094c5649 8130734: Apply transformations found by netbeans Refactor->Inspect and transform menu
sundar
parents: 26765
diff changeset
   364
            str += 'l';
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
   365
        }
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
   366
        return str;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   367
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   368
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   369
    @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   370
    public String getMessage() {
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
   371
        return getMessage(false);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   372
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   373
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   374
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   375
     * Short toString function for message
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   376
     * @return short message
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   377
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   378
    public String getMessageShort() {
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
   379
        return getMessage(true);
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
   380
    }
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
   381
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
   382
    private String getMessage(final boolean isShort) {
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
   383
        final StringBuilder sb = new StringBuilder();
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
   384
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
   385
        //program point
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
   386
        sb.append("[pp=").
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
   387
            append(getProgramPoint()).
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
   388
            append(", ");
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
   389
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
   390
        //slot contents
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
   391
        if (!isShort) {
25254
a56c2713f1a8 8049223: RewriteException class exposes public mutable arrays
attila
parents: 24776
diff changeset
   392
            final Object[] slots = byteCodeSlots;
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
   393
            if (slots != null) {
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
   394
                sb.append("slots=").
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
   395
                    append(Arrays.asList(slots)).
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
   396
                    append(", ");
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
   397
            }
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
   398
        }
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
   399
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
   400
        //return type
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
   401
        sb.append("type=").
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
   402
            append(getReturnType()).
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
   403
            append(", ");
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
   404
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
   405
        //return value
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
   406
        sb.append("value=").
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
   407
            append(stringify(getReturnValueNonDestructive())).
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
   408
            append(")]");
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
   409
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
   410
        return sb.toString();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   411
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   412
24776
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   413
    private void writeObject(final ObjectOutputStream out) throws NotSerializableException {
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   414
        throw new NotSerializableException(getClass().getName());
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   415
    }
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   416
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   417
    private void readObject(final ObjectInputStream in) throws NotSerializableException {
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   418
        throw new NotSerializableException(getClass().getName());
966226aeee50 8044518: Ensure exceptions related to optimistic recompilation are not serializable
attila
parents: 24773
diff changeset
   419
    }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents:
diff changeset
   420
}