nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/types/Type.java
author hannesw
Sat, 19 Sep 2015 16:04:28 +0200
changeset 32781 d8f34ffbbc7a
parent 27365 6dd6e324d1c7
child 33538 82c57d427fa1
permissions -rw-r--r--
8135190: Method code too large in Babel browser.js script Reviewed-by: attila, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.codegen.types;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import static jdk.internal.org.objectweb.asm.Opcodes.DALOAD;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import static jdk.internal.org.objectweb.asm.Opcodes.DASTORE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import static jdk.internal.org.objectweb.asm.Opcodes.DUP;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import static jdk.internal.org.objectweb.asm.Opcodes.DUP2;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import static jdk.internal.org.objectweb.asm.Opcodes.DUP2_X1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import static jdk.internal.org.objectweb.asm.Opcodes.DUP2_X2;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
import static jdk.internal.org.objectweb.asm.Opcodes.DUP_X1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
import static jdk.internal.org.objectweb.asm.Opcodes.DUP_X2;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    36
import static jdk.internal.org.objectweb.asm.Opcodes.H_INVOKESTATIC;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
import static jdk.internal.org.objectweb.asm.Opcodes.IALOAD;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
import static jdk.internal.org.objectweb.asm.Opcodes.IASTORE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESTATIC;
18633
c2138d47b2c6 8017082: Long array literals were slightly broken
lagergren
parents: 17756
diff changeset
    40
import static jdk.internal.org.objectweb.asm.Opcodes.LALOAD;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
import static jdk.internal.org.objectweb.asm.Opcodes.LASTORE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
import static jdk.internal.org.objectweb.asm.Opcodes.NEWARRAY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
import static jdk.internal.org.objectweb.asm.Opcodes.POP;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
import static jdk.internal.org.objectweb.asm.Opcodes.POP2;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
import static jdk.internal.org.objectweb.asm.Opcodes.SWAP;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
import static jdk.internal.org.objectweb.asm.Opcodes.T_DOUBLE;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
import static jdk.internal.org.objectweb.asm.Opcodes.T_INT;
18633
c2138d47b2c6 8017082: Long array literals were slightly broken
lagergren
parents: 17756
diff changeset
    48
import static jdk.internal.org.objectweb.asm.Opcodes.T_LONG;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    49
import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup;
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
    50
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
    51
import java.io.DataInput;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
    52
import java.io.DataOutput;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
    53
import java.io.IOException;
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
    54
import java.io.Serializable;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    55
import java.lang.invoke.CallSite;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
import java.lang.invoke.MethodHandle;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    57
import java.lang.invoke.MethodHandles;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    58
import java.lang.invoke.MethodType;
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
    59
import java.util.Collections;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
    60
import java.util.Map;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
    61
import java.util.TreeMap;
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
    62
import java.util.WeakHashMap;
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
    63
import java.util.concurrent.ConcurrentHashMap;
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
    64
import java.util.concurrent.ConcurrentMap;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    65
import jdk.internal.org.objectweb.asm.Handle;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
import jdk.internal.org.objectweb.asm.MethodVisitor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
import jdk.nashorn.internal.codegen.CompilerConstants.Call;
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
    68
import jdk.nashorn.internal.runtime.Context;
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
    69
import jdk.nashorn.internal.runtime.ScriptObject;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    70
import jdk.nashorn.internal.runtime.Undefined;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
    71
import jdk.nashorn.internal.runtime.linker.Bootstrap;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
/**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
 * This is the representation of a JavaScript type, disassociated from java
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
 * Classes, with the basis for conversion weight, mapping to ASM types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
 * and implementing the ByteCodeOps interface which tells this type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
 * how to generate code for various operations.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
 * Except for ClassEmitter, this is the only class that has to know
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
 * about the underlying byte code generation system.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
 * The different types know how to generate bytecode for the different
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
 * operations, inherited from BytecodeOps, that they support. This avoids
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
 * if/else chains depending on type in several cases and allows for
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
 * more readable and shorter code
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
 * The Type class also contains logic used by the type inference and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
 * for comparing types against each other, as well as the concepts
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
 * of narrower to wider types. The widest type is an object. Ideally we
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
 * would like as narrow types as possible for code to be efficient, e.g
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
 * INTs rather than OBJECTs
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
    94
public abstract class Type implements Comparable<Type>, BytecodeOps, Serializable {
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
    95
    private static final long serialVersionUID = 1L;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
    /** Human readable name for type */
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
    98
    private transient final String name;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
    /** Descriptor for type */
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   101
    private transient final String descriptor;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
    /** The "weight" of the type. Used for picking widest/least specific common type */
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   104
    private transient final int weight;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
    /** How many bytecode slots does this type occupy */
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   107
    private transient final int slots;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
    /** The class for this type */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
    private final Class<?> clazz;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   112
    /**
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   113
     * Cache for internal types - this is a query that requires complex stringbuilding inside
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   114
     * ASM and it saves startup time to cache the type mappings
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   115
     */
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   116
    private static final Map<Class<?>, jdk.internal.org.objectweb.asm.Type> INTERNAL_TYPE_CACHE =
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   117
            Collections.synchronizedMap(new WeakHashMap<Class<?>, jdk.internal.org.objectweb.asm.Type>());
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   118
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   119
    /** Internal ASM type for this Type - computed once at construction */
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   120
    private transient final jdk.internal.org.objectweb.asm.Type internalType;
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   121
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
    /** Weights are used to decide which types are "wider" than other types */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
    protected static final int MIN_WEIGHT = -1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
    /** Set way below Integer.MAX_VALUE to prevent overflow when adding weights. Objects are still heaviest. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
    protected static final int MAX_WEIGHT = 20;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   128
    static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "mathBootstrap", CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class, int.class);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   129
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   130
    static final Handle MATHBOOTSTRAP = new Handle(H_INVOKESTATIC, BOOTSTRAP.className(), "mathBootstrap", BOOTSTRAP.descriptor());
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   131
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
     * Constructor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
     * @param clazz       class for type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
     * @param weight      weight - higher is more generic
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
     * @param slots       how many bytecode slots the type takes up
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
    Type(final String name, final Class<?> clazz, final int weight, final int slots) {
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   140
        this.name         = name;
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   141
        this.clazz        = clazz;
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   142
        this.descriptor   = jdk.internal.org.objectweb.asm.Type.getDescriptor(clazz);
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   143
        this.weight       = weight;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
        assert weight >= MIN_WEIGHT && weight <= MAX_WEIGHT : "illegal type weight: " + weight;
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   145
        this.slots        = slots;
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   146
        this.internalType = getInternalType(clazz);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
     * Get the weight of this type - use this e.g. for sorting method descriptors
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
     * @return the weight
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
    public int getWeight() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
        return weight;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
     * Get the Class representing this type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
     * @return the class for this type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
    public Class<?> getTypeClass() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
        return clazz;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
     * For specialization, return the next, slightly more difficulty, type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
     * to test.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
     * @return the next Type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
    public Type nextWider() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
     * Get the boxed type for this class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
     * @return the boxed version of this type or null if N/A
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
    public Class<?> getBoxedType() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
        assert !getTypeClass().isPrimitive();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   185
     * Returns the character describing the bytecode type for this value on the stack or local variable, identical to
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   186
     * what would be used as the prefix for a bytecode {@code LOAD} or {@code STORE} instruction, therefore it must be
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   187
     * one of {@code A, F, D, I, L}. Also, the special value {@code U} is used for local variable slots that haven't
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   188
     * been initialized yet (it can't appear for a value pushed to the operand stack, those always have known values).
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   189
     * Note that while we allow all JVM internal types, Nashorn doesn't necessarily use them all - currently we don't
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   190
     * have floats, only doubles, but that might change in the future.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   191
     * @return the character describing the bytecode type for this value on the stack.
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   192
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   193
    public abstract char getBytecodeStackType();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   194
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   195
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
     * Generate a method descriptor given a return type and a param array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
     * @param returnType return type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
     * @param types      parameters
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
     * @return a descriptor string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
    public static String getMethodDescriptor(final Type returnType, final Type... types) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
        final jdk.internal.org.objectweb.asm.Type[] itypes = new jdk.internal.org.objectweb.asm.Type[types.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
        for (int i = 0; i < types.length; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
            itypes[i] = types[i].getInternalType();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
        return jdk.internal.org.objectweb.asm.Type.getMethodDescriptor(returnType.getInternalType(), itypes);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
     * Generate a method descriptor given a return type and a param array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
     * @param returnType return type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
     * @param types      parameters
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
     * @return a descriptor string
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
    public static String getMethodDescriptor(final Class<?> returnType, final Class<?>... types) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
        final jdk.internal.org.objectweb.asm.Type[] itypes = new jdk.internal.org.objectweb.asm.Type[types.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
        for (int i = 0; i < types.length; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
            itypes[i] = getInternalType(types[i]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
        return jdk.internal.org.objectweb.asm.Type.getMethodDescriptor(getInternalType(returnType), itypes);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
    /**
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   228
     * Return a character representing {@code type} in a method signature.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   229
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   230
     * @param type parameter type
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   231
     * @return descriptor character
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   232
     */
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   233
    public static char getShortSignatureDescriptor(final Type type) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   234
        // Use 'Z' for boolean parameters as we need to distinguish from int
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   235
        if (type instanceof BooleanType) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   236
            return 'Z';
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   237
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   238
        return type.getBytecodeStackType();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   239
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   240
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   241
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
     * Return the type for an internal type, package private - do not use
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
     * outside code gen
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
     * @param itype internal type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
     * @return Nashorn type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   247
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   248
    @SuppressWarnings("fallthrough")
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   249
    static Type typeFor(final jdk.internal.org.objectweb.asm.Type itype) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   250
        switch (itype.getSort()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   251
        case jdk.internal.org.objectweb.asm.Type.BOOLEAN:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
            return BOOLEAN;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
        case jdk.internal.org.objectweb.asm.Type.INT:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
            return INT;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
        case jdk.internal.org.objectweb.asm.Type.LONG:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
            return LONG;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
        case jdk.internal.org.objectweb.asm.Type.DOUBLE:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
            return NUMBER;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
        case jdk.internal.org.objectweb.asm.Type.OBJECT:
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
   260
            if (Context.isStructureClass(itype.getClassName())) {
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
   261
                return SCRIPT_OBJECT;
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
   262
            }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   263
            try {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   264
                return Type.typeFor(Class.forName(itype.getClassName()));
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   265
            } catch(final ClassNotFoundException e) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   266
                throw new AssertionError(e);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   267
            }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
        case jdk.internal.org.objectweb.asm.Type.VOID:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
        case jdk.internal.org.objectweb.asm.Type.ARRAY:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
            switch (itype.getElementType().getSort()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
            case jdk.internal.org.objectweb.asm.Type.DOUBLE:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   273
                return NUMBER_ARRAY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
            case jdk.internal.org.objectweb.asm.Type.INT:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
                return INT_ARRAY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
            case jdk.internal.org.objectweb.asm.Type.LONG:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
                return LONG_ARRAY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
            default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
                assert false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
            case jdk.internal.org.objectweb.asm.Type.OBJECT:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
                return OBJECT_ARRAY;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
            assert false : "Unknown itype : " + itype + " sort " + itype.getSort();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
     * Get the return type for a method
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   293
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   294
     * @param methodDescriptor method descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
     * @return return type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
    public static Type getMethodReturnType(final String methodDescriptor) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
        return Type.typeFor(jdk.internal.org.objectweb.asm.Type.getReturnType(methodDescriptor));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
     * Get type array representing arguments of a method in order
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
     * @param methodDescriptor method descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   305
     * @return parameter type array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   306
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
    public static Type[] getMethodArguments(final String methodDescriptor) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
        final jdk.internal.org.objectweb.asm.Type itypes[] = jdk.internal.org.objectweb.asm.Type.getArgumentTypes(methodDescriptor);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
        final Type types[] = new Type[itypes.length];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
        for (int i = 0; i < itypes.length; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
            types[i] = Type.typeFor(itypes[i]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
        return types;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   316
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   317
     * Write a map of {@code int} to {@code Type} to an output stream. This is used to store deoptimization state.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   318
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   319
     * @param typeMap the type map
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   320
     * @param output data output
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26374
diff changeset
   321
     * @throws IOException if write cannot be completed
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   322
     */
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
   323
    public static void writeTypeMap(final Map<Integer, Type> typeMap, final DataOutput output) throws IOException {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   324
        if (typeMap == null) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   325
            output.writeInt(0);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   326
        } else {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   327
            output.writeInt(typeMap.size());
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
   328
            for(final Map.Entry<Integer, Type> e: typeMap.entrySet()) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   329
                output.writeInt(e.getKey());
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   330
                final byte typeChar;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   331
                final Type type = e.getValue();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   332
                if(type == Type.OBJECT) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   333
                    typeChar = 'L';
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   334
                } else if (type == Type.NUMBER) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   335
                    typeChar = 'D';
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   336
                } else if (type == Type.LONG) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   337
                    typeChar = 'J';
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   338
                } else {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   339
                    throw new AssertionError();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   340
                }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   341
                output.writeByte(typeChar);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   342
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   343
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   344
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   345
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   346
    /**
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   347
     * Read a map of {@code int} to {@code Type} from an input stream. This is used to store deoptimization state.
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   348
     *
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   349
     * @param input data input
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   350
     * @return type map
26886
18c744ab4df2 8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents: 26374
diff changeset
   351
     * @throws IOException if read cannot be completed
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   352
     */
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 26055
diff changeset
   353
    public static Map<Integer, Type> readTypeMap(final DataInput input) throws IOException {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   354
        final int size = input.readInt();
26374
5bc67576b50e 8056913: Limit the size of type info cache on disk
attila
parents: 26068
diff changeset
   355
        if (size <= 0) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   356
            return null;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   357
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   358
        final Map<Integer, Type> map = new TreeMap<>();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   359
        for(int i = 0; i < size; ++i) {
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   360
            final int pp = input.readInt();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   361
            final int typeChar = input.readByte();
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   362
            final Type type;
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27209
diff changeset
   363
            switch (typeChar) {
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   364
                case 'L': type = Type.OBJECT; break;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   365
                case 'D': type = Type.NUMBER; break;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   366
                case 'J': type = Type.LONG; break;
26374
5bc67576b50e 8056913: Limit the size of type info cache on disk
attila
parents: 26068
diff changeset
   367
                default: continue;
26055
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   368
            }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   369
            map.put(pp, type);
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   370
        }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   371
        return map;
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   372
    }
fe8be844ba50 8043956: Make code caching work with optimistic typing and lazy compilation
hannesw
parents: 25249
diff changeset
   373
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
    static jdk.internal.org.objectweb.asm.Type getInternalType(final String className) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   375
        return jdk.internal.org.objectweb.asm.Type.getType(className);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
    private jdk.internal.org.objectweb.asm.Type getInternalType() {
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   379
        return internalType;
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   380
    }
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   381
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   382
    private static jdk.internal.org.objectweb.asm.Type lookupInternalType(final Class<?> type) {
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27209
diff changeset
   383
        final Map<Class<?>, jdk.internal.org.objectweb.asm.Type> c = INTERNAL_TYPE_CACHE;
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27209
diff changeset
   384
        jdk.internal.org.objectweb.asm.Type itype = c.get(type);
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   385
        if (itype != null) {
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   386
            return itype;
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   387
        }
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   388
        itype = jdk.internal.org.objectweb.asm.Type.getType(type);
27361
86c4ddb4797b 8060204: Fix warnings in Joni and tests
lagergren
parents: 27209
diff changeset
   389
        c.put(type, itype);
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   390
        return itype;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   391
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
    private static jdk.internal.org.objectweb.asm.Type getInternalType(final Class<?> type) {
26887
f7e8b7f8f554 8059321: Decrease warmup time by caching common structures that were reused during parse
lagergren
parents: 26886
diff changeset
   394
        return lookupInternalType(type);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   396
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   397
    static void invokestatic(final MethodVisitor method, final Call call) {
22368
9cb3e7db27c6 8029031: "ant clean jar" on nashorn repo results in number of ASM deprecated warnings
sundar
parents: 21457
diff changeset
   398
        method.visitMethodInsn(INVOKESTATIC, call.className(), call.name(), call.descriptor(), false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   400
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   401
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   402
     * Get the internal JVM name of a type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
     * @return the internal name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   404
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   405
    public String getInternalName() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
        return jdk.internal.org.objectweb.asm.Type.getInternalName(getTypeClass());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   408
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   409
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   410
     * Get the internal JVM name of type type represented by a given Java class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   411
     * @param clazz the class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   412
     * @return the internal name
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   413
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   414
    public static String getInternalName(final Class<?> clazz) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   415
        return jdk.internal.org.objectweb.asm.Type.getInternalName(clazz);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   416
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   417
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   418
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   419
     * Determines whether a type is the UNKNOWN type, i.e. not set yet
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   420
     * Used for type inference.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   421
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
     * @return true if UNKNOWN, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
    public boolean isUnknown() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
        return this.equals(Type.UNKNOWN);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
    /**
21457
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   429
     * Determines whether this type represents an primitive type according to the ECMAScript specification,
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   430
     * which includes Boolean, Number, and String.
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   431
     *
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   432
     * @return true if a JavaScript primitive type, false otherwise.
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   433
     */
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   434
    public boolean isJSPrimitive() {
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   435
        return !isObject() || isString();
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   436
    }
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   437
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   438
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
     * Determines whether a type is the BOOLEAN type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
     * @return true if BOOLEAN, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
    public boolean isBoolean() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
        return this.equals(Type.BOOLEAN);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
     * Determines whether a type is the INT type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
     * @return true if INTEGER, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
    public boolean isInteger() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
        return this.equals(Type.INT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   452
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   453
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   454
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   455
     * Determines whether a type is the LONG type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   456
     * @return true if LONG, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   457
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   458
    public boolean isLong() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   459
        return this.equals(Type.LONG);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   460
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   461
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   462
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   463
     * Determines whether a type is the NUMBER type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   464
     * @return true if NUMBER, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   465
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   466
    public boolean isNumber() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   467
        return this.equals(Type.NUMBER);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   468
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   469
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   470
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   471
     * Determines whether a type is numeric, i.e. NUMBER,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   472
     * INT, LONG.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   473
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   474
     * @return true if numeric, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   475
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
    public boolean isNumeric() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
        return this instanceof NumericType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
     * Determines whether a type is an array type, i.e.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
     * OBJECT_ARRAY or NUMBER_ARRAY (for now)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
     * @return true if an array type, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
    public boolean isArray() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
        return this instanceof ArrayType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
     * Determines if a type takes up two bytecode slots or not
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   492
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
     * @return true if type takes up two bytecode slots rather than one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
    public boolean isCategory2() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   496
        return getSlots() == 2;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   497
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   498
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   500
     * Determines whether a type is an OBJECT type, e.g. OBJECT, STRING,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   501
     * NUMBER_ARRAY etc.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   502
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   503
     * @return true if object type, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
    public boolean isObject() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
        return this instanceof ObjectType;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   508
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   510
     * Is this a primitive type (e.g int, long, double, boolean)
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   511
     * @return true if primitive
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   512
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   513
    public boolean isPrimitive() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   514
        return !isObject();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   515
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   516
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   517
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
     * Determines whether a type is a STRING type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
     * @return true if object type, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
    public boolean isString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
        return this.equals(Type.STRING);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   524
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   525
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
    /**
25249
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   527
     * Determines whether a type is a CHARSEQUENCE type used internally strings
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   528
     *
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   529
     * @return true if CharSequence (internal string) type, false otherwise
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   530
     */
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   531
    public boolean isCharSequence() {
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   532
        return this.equals(Type.CHARSEQUENCE);
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   533
    }
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   534
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   535
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
     * Determine if two types are equivalent, i.e. need no conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
     * @param type the second type to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
     * @return true if types are equivalent, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
    public boolean isEquivalentTo(final Type type) {
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   543
        return this.weight() == type.weight() || isObject() && type.isObject();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
     * Determine if a type can be assigned to from another
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   548
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   549
     * @param type0 the first type to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
     * @param type1 the second type to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
     * @return true if type1 can be written to type2, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   553
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
    public static boolean isAssignableFrom(final Type type0, final Type type1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
        if (type0.isObject() && type1.isObject()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
            return type0.weight() >= type1.weight();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   557
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   558
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   559
        return type0.weight() == type1.weight();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
     * Determine if this type is assignable from another type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
     * @param type the type to check against
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   566
     * @return true if "type" can be written to this type, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   567
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
    public boolean isAssignableFrom(final Type type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   569
        return Type.isAssignableFrom(this, type);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   570
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   571
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   572
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
     * Determines is this type is equivalent to another, i.e. needs no conversion
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
     * to be assigned to it.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
     * @param type0 the first type to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
     * @param type1 the second type to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   578
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
     * @return true if this type is equivalent to type, false otherwise
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   580
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
    public static boolean areEquivalent(final Type type0, final Type type1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
        return type0.isEquivalentTo(type1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   583
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   585
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
     * Determine the number of bytecode slots a type takes up
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   587
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
     * @return the number of slots for this type, 1 or 2.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
    public int getSlots() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
        return slots;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
    }
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   593
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   594
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
     * Returns the widest or most common of two types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
     * @param type0 type one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   598
     * @param type1 type two
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
     * @return the widest type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
    public static Type widest(final Type type0, final Type type1) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
        if (type0.isArray() && type1.isArray()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   604
            return ((ArrayType)type0).getElementType() == ((ArrayType)type1).getElementType() ? type0 : Type.OBJECT;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   605
        } else if (type0.isArray() != type1.isArray()) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 18859
diff changeset
   606
            //array and non array is always object, widest(Object[], int) NEVER returns Object[], which has most weight. that does not make sense
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 18859
diff changeset
   607
            return Type.OBJECT;
21457
381acbd07fe5 8027042: Evaluation order for binary operators can be improved
hannesw
parents: 20928
diff changeset
   608
        } else if (type0.isObject() && type1.isObject() && type0.getTypeClass() != type1.getTypeClass()) {
20928
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 18859
diff changeset
   609
            // Object<type=String> and Object<type=ScriptFunction> will produce Object
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 18859
diff changeset
   610
            // TODO: maybe find most specific common superclass?
3ff39d5c8c08 8026137: Fix Issues with Binary Evaluation Order
lagergren
parents: 18859
diff changeset
   611
            return Type.OBJECT;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
        return type0.weight() > type1.weight() ? type0 : type1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
    /**
27209
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   617
     * Returns the widest or most common of two types, given as classes
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   618
     *
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   619
     * @param type0 type one
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   620
     * @param type1 type two
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   621
     *
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   622
     * @return the widest type
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   623
     */
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   624
    public static Class<?> widest(final Class<?> type0, final Class<?> type1) {
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   625
        return widest(Type.typeFor(type0), Type.typeFor(type1)).getTypeClass();
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   626
    }
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   627
30d8609b9561 8061391: concat as a builtin optimistic form, had to remove NoTypedArrayData and replace it, as we throw away a lot of optimistic link opportunities with NoTypedArrayData not being Continuous
lagergren
parents: 27204
diff changeset
   628
    /**
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   629
     * When doing widening for return types of a function or a ternary operator, it is not valid to widen a boolean to
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   630
     * anything other than object. Note that this wouldn't be necessary if {@code Type.widest} did not allow
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   631
     * boolean-to-number widening. Eventually, we should address it there, but it affects too many other parts of the
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   632
     * system and is sometimes legitimate (e.g. whenever a boolean value would undergo ToNumber conversion anyway).
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   633
     * @param t1 type 1
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   634
     * @param t2 type 2
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   635
     * @return wider of t1 and t2, except if one is boolean and the other is neither boolean nor unknown, in which case
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   636
     * {@code Type.OBJECT} is returned.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   637
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   638
    public static Type widestReturnType(final Type t1, final Type t2) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   639
        if (t1.isUnknown()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   640
            return t2;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   641
        } else if (t2.isUnknown()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   642
            return t1;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   643
        } else if(t1.isBoolean() != t2.isBoolean() || t1.isNumeric() != t2.isNumeric()) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   644
            return Type.OBJECT;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   645
        }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   646
        return Type.widest(t1, t2);
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   647
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   648
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   649
    /**
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   650
     * Returns a generic version of the type. Basically, if the type {@link #isObject()}, returns {@link #OBJECT},
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   651
     * otherwise returns the type unchanged.
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   652
     * @param type the type to generify
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   653
     * @return the generified type
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   654
     */
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   655
    public static Type generic(final Type type) {
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   656
        return type.isObject() ? Type.OBJECT : type;
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   657
    }
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   658
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   659
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   660
     * Returns the narrowest or least common of two types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   661
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   662
     * @param type0 type one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   663
     * @param type1 type two
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   664
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   665
     * @return the widest type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   666
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   667
    public static Type narrowest(final Type type0, final Type type1) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   668
        return type0.narrowerThan(type1) ? type0 : type1;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   669
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   670
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   671
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   672
     * Check whether this type is strictly narrower than another one
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   673
     * @param type type to check against
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   674
     * @return true if this type is strictly narrower
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   675
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   676
    public boolean narrowerThan(final Type type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   677
        return weight() < type.weight();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   678
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   679
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   680
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   681
     * Check whether this type is strictly wider than another one
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   682
     * @param type type to check against
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   683
     * @return true if this type is strictly wider
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   684
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   685
    public boolean widerThan(final Type type) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   686
        return weight() > type.weight();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   687
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   688
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   689
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   690
     * Returns the widest or most common of two types, but no wider than "limit"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   691
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   692
     * @param type0 type one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   693
     * @param type1 type two
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   694
     * @param limit limiting type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   695
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   696
     * @return the widest type, but no wider than limit
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   697
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   698
    public static Type widest(final Type type0, final Type type1, final Type limit) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   699
        final Type type = Type.widest(type0,  type1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   700
        if (type.weight() > limit.weight()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   701
            return limit;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   702
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   703
        return type;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   704
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   705
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   706
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   707
     * Returns the widest or most common of two types, but no narrower than "limit"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   708
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   709
     * @param type0 type one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   710
     * @param type1 type two
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   711
     * @param limit limiting type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   712
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   713
     * @return the widest type, but no wider than limit
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   714
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   715
    public static Type narrowest(final Type type0, final Type type1, final Type limit) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   716
        final Type type = type0.weight() < type1.weight() ? type0 : type1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   717
        if (type.weight() < limit.weight()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   718
            return limit;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   719
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   720
        return type;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   721
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   722
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   723
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   724
     * Returns the narrowest of this type and another
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   725
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   726
     * @param  other type to compare against
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   727
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   728
     * @return the widest type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   729
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   730
    public Type narrowest(final Type other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   731
        return Type.narrowest(this, other);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   732
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   733
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   734
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   735
     * Returns the widest of this type and another
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   736
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   737
     * @param  other type to compare against
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   738
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   739
     * @return the widest type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   740
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   741
    public Type widest(final Type other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   742
        return Type.widest(this, other);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   743
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   744
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   745
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   746
     * Returns the weight of a type, used for type comparison
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   747
     * between wider and narrower types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   748
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   749
     * @return the weight
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   750
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   751
    int weight() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   752
        return weight;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   753
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   754
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   755
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   756
     * Return the descriptor of a type, used for e.g. signature
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   757
     * generation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   758
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   759
     * @return the descriptor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   760
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   761
    public String getDescriptor() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   762
        return descriptor;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   763
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   764
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   765
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   766
     * Return the descriptor of a type, short version
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   767
     * Used mainly for debugging purposes
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   768
     *
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   769
     * @return the short descriptor
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   770
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   771
    public String getShortDescriptor() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   772
        return descriptor;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   773
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   774
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   775
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   776
    public String toString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   777
        return name;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   778
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   779
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   780
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   781
     * Return the (possibly cached) Type object for this class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   782
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   783
     * @param clazz the class to check
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   784
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   785
     * @return the Type representing this class
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   786
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   787
    public static Type typeFor(final Class<?> clazz) {
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   788
        final Type type = cache.get(clazz);
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   789
        if(type != null) {
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   790
            return type;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   791
        }
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   792
        assert !clazz.isPrimitive() || clazz == void.class;
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   793
        final Type newType;
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   794
        if (clazz.isArray()) {
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   795
            newType = new ArrayType(clazz);
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   796
        } else {
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   797
            newType = new ObjectType(clazz);
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   798
        }
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   799
        final Type existingType = cache.putIfAbsent(clazz, newType);
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   800
        return existingType == null ? newType : existingType;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   801
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   802
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   803
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   804
    public int compareTo(final Type o) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   805
        return o.weight() - weight();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   806
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   807
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   808
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   809
     * Common logic for implementing dup for all types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   810
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   811
     * @param method method visitor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   812
     * @param depth dup depth
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   813
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   814
     * @return the type at the top of the stack afterwards
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   815
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   816
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   817
    public Type dup(final MethodVisitor method, final int depth) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   818
        return Type.dup(method, this, depth);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   819
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   820
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   821
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   822
     * Common logic for implementing swap for all types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   823
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   824
     * @param method method visitor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   825
     * @param other  the type to swap with
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   826
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   827
     * @return the type at the top of the stack afterwards, i.e. other
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   828
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   829
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   830
    public Type swap(final MethodVisitor method, final Type other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   831
        Type.swap(method, this, other);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   832
        return other;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   833
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   834
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   835
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   836
     * Common logic for implementing pop for all types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   837
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   838
     * @param method method visitor
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   839
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   840
     * @return the type that was popped
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   841
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   842
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   843
    public Type pop(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   844
        Type.pop(method, this);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   845
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   846
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   847
17259
a8d0cb25d725 8013533: Increase code coverage report for types and logging
lagergren
parents: 16523
diff changeset
   848
    @Override
a8d0cb25d725 8013533: Increase code coverage report for types and logging
lagergren
parents: 16523
diff changeset
   849
    public Type loadEmpty(final MethodVisitor method) {
a8d0cb25d725 8013533: Increase code coverage report for types and logging
lagergren
parents: 16523
diff changeset
   850
        assert false : "unsupported operation";
a8d0cb25d725 8013533: Increase code coverage report for types and logging
lagergren
parents: 16523
diff changeset
   851
        return null;
a8d0cb25d725 8013533: Increase code coverage report for types and logging
lagergren
parents: 16523
diff changeset
   852
    }
a8d0cb25d725 8013533: Increase code coverage report for types and logging
lagergren
parents: 16523
diff changeset
   853
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   854
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   855
     * Superclass logic for pop for all types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   856
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   857
     * @param method method emitter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   858
     * @param type   type to pop
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   859
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   860
    protected static void pop(final MethodVisitor method, final Type type) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   861
        method.visitInsn(type.isCategory2() ? POP2 : POP);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   862
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   863
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   864
    private static Type dup(final MethodVisitor method, final Type type, final int depth) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   865
        final boolean       cat2 = type.isCategory2();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   866
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   867
        switch (depth) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   868
        case 0:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   869
            method.visitInsn(cat2 ? DUP2 : DUP);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   870
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   871
        case 1:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   872
            method.visitInsn(cat2 ? DUP2_X1 : DUP_X1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   873
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   874
        case 2:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   875
            method.visitInsn(cat2 ? DUP2_X2 : DUP_X2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   876
            break;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   877
        default:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   878
            return null; //invalid depth
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   879
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   880
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   881
        return type;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   882
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   883
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   884
    private static void swap(final MethodVisitor method, final Type above, final Type below) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   885
        if (below.isCategory2()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   886
            if (above.isCategory2()) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   887
                method.visitInsn(DUP2_X2);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   888
                method.visitInsn(POP2);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   889
            } else {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   890
                method.visitInsn(DUP_X2);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   891
                method.visitInsn(POP);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   892
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   893
        } else {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   894
            if (above.isCategory2()) {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   895
                method.visitInsn(DUP2_X1);
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   896
                method.visitInsn(POP2);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   897
            } else {
16523
af8b30edebce 8009718: Lazy execution architecture continued - ScriptFunctionData is either final or recompilable. Moved ScriptFunctionData creation logic away from runtime to compile time. Prepared for method generation/specialization. Got rid of ScriptFunctionImplTrampoline whose semantics could be done as part of the relinking anyway. Merge with the lookup package change.
lagergren
parents: 16206
diff changeset
   898
                method.visitInsn(SWAP);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   899
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   900
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   901
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   902
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   903
    /** Mappings between java classes and their Type singletons */
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   904
    private static final ConcurrentMap<Class<?>, Type> cache = new ConcurrentHashMap<>();
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   905
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   906
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   907
     * This is the boolean singleton, used for all boolean types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   908
     */
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   909
    public static final Type BOOLEAN = putInCache(new BooleanType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   910
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   911
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   912
     * This is an integer type, i.e INT, INT32.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   913
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   914
    public static final BitwiseType INT = putInCache(new IntType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   915
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   916
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   917
     * This is the number singleton, used for all number types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   918
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   919
    public static final NumericType NUMBER = putInCache(new NumberType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   920
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   921
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   922
     * This is the long singleton, used for all long types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   923
     */
24751
ccbd9cd3f720 8042118: Separate types from symbols
attila
parents: 24725
diff changeset
   924
    public static final BitwiseType LONG = putInCache(new LongType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   925
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   926
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   927
     * A string singleton
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   928
     */
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   929
    public static final Type STRING = putInCache(new ObjectType(String.class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   930
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   931
    /**
25249
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   932
     * This is the CharSequence singleton used to represent JS strings internally
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   933
     * (either a {@code java.lang.String} or {@code jdk.nashorn.internal.runtime.ConsString}.
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   934
     */
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   935
    public static final Type CHARSEQUENCE = putInCache(new ObjectType(CharSequence.class));
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   936
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   937
919903299b0d 8048586: String concatenation with optimistic types is slow
hannesw
parents: 24778
diff changeset
   938
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   939
     * This is the object singleton, used for all object types
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   940
     */
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
   941
    public static final Type OBJECT = putInCache(new ObjectType());
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   942
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   943
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   944
     * A undefined singleton
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   945
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   946
    public static final Type UNDEFINED = putInCache(new ObjectType(Undefined.class));
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   947
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
   948
    /**
24725
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   949
     * This is the singleton for ScriptObjects
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   950
     */
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   951
    public static final Type SCRIPT_OBJECT = putInCache(new ObjectType(ScriptObject.class));
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   952
7bb1f687a852 8033334: Make sure that scope depth information is maintained in the RecompilableScriptFunctionDatas, to avoid unnecessary slow proto linkage when doing on demand compilation
lagergren
parents: 24719
diff changeset
   953
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   954
     * This is the singleton for integer arrays
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   955
     */
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
   956
    public static final ArrayType INT_ARRAY = putInCache(new ArrayType(int[].class) {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   957
        private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   958
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   959
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   960
        public void astore(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   961
            method.visitInsn(IASTORE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   962
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   963
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   964
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   965
        public Type aload(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   966
            method.visitInsn(IALOAD);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   967
            return INT;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   968
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   969
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   970
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   971
        public Type newarray(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   972
            method.visitIntInsn(NEWARRAY, T_INT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   973
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   974
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   975
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   976
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   977
        public Type getElementType() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   978
            return INT;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   979
        }
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
   980
    });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   981
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   982
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   983
     * This is the singleton for long arrays
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   984
     */
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
   985
    public static final ArrayType LONG_ARRAY = putInCache(new ArrayType(long[].class) {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   986
        private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
   987
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   988
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   989
        public void astore(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   990
            method.visitInsn(LASTORE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   991
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   992
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   993
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   994
        public Type aload(final MethodVisitor method) {
18633
c2138d47b2c6 8017082: Long array literals were slightly broken
lagergren
parents: 17756
diff changeset
   995
            method.visitInsn(LALOAD);
c2138d47b2c6 8017082: Long array literals were slightly broken
lagergren
parents: 17756
diff changeset
   996
            return LONG;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   997
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   998
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   999
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1000
        public Type newarray(final MethodVisitor method) {
18633
c2138d47b2c6 8017082: Long array literals were slightly broken
lagergren
parents: 17756
diff changeset
  1001
            method.visitIntInsn(NEWARRAY, T_LONG);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1002
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1003
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1004
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1005
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1006
        public Type getElementType() {
18633
c2138d47b2c6 8017082: Long array literals were slightly broken
lagergren
parents: 17756
diff changeset
  1007
            return LONG;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1008
        }
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
  1009
    });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1010
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1011
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1012
     * This is the singleton for numeric arrays
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1013
     */
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
  1014
    public static final ArrayType NUMBER_ARRAY = putInCache(new ArrayType(double[].class) {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1015
        private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1016
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1017
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1018
        public void astore(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1019
            method.visitInsn(DASTORE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1020
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1021
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1022
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1023
        public Type aload(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1024
            method.visitInsn(DALOAD);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1025
            return NUMBER;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1026
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1027
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1028
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1029
        public Type newarray(final MethodVisitor method) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1030
            method.visitIntInsn(NEWARRAY, T_DOUBLE);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1031
            return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1032
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1033
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1034
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1035
        public Type getElementType() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1036
            return NUMBER;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1037
        }
32781
d8f34ffbbc7a 8135190: Method code too large in Babel browser.js script
hannesw
parents: 27365
diff changeset
  1038
    });
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1039
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1040
    /** This is the singleton for object arrays */
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
  1041
    public static final ArrayType OBJECT_ARRAY = putInCache(new ArrayType(Object[].class));
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1042
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1043
    /** This type, always an object type, just a toString override */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1044
    public static final Type THIS = new ObjectType() {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1045
        private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1046
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1047
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1048
        public String toString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1049
            return "this";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1050
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1051
    };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1052
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1053
    /** Scope type, always an object type, just a toString override */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1054
    public static final Type SCOPE = new ObjectType() {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1055
        private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1056
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1057
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1058
        public String toString() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1059
            return "scope";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1060
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1061
    };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1062
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1063
    private static interface Unknown {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1064
        // EMPTY - used as a class that is absolutely not compatible with a type to represent "unknown"
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1065
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1066
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1067
    private abstract static class ValueLessType extends Type {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1068
        private static final long serialVersionUID = 1L;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1069
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1070
        ValueLessType(final String name) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1071
            super(name, Unknown.class, MIN_WEIGHT, 1);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1072
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1073
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1074
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1075
        public Type load(final MethodVisitor method, final int slot) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1076
            throw new UnsupportedOperationException("load " + slot);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1077
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1078
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1079
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1080
        public void store(final MethodVisitor method, final int slot) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1081
            throw new UnsupportedOperationException("store " + slot);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1082
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1083
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1084
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1085
        public Type ldc(final MethodVisitor method, final Object c) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1086
            throw new UnsupportedOperationException("ldc " + c);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1087
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1088
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1089
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1090
        public Type loadUndefined(final MethodVisitor method) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1091
            throw new UnsupportedOperationException("load undefined");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1092
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1093
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1094
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1095
        public Type loadForcedInitializer(final MethodVisitor method) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1096
            throw new UnsupportedOperationException("load forced initializer");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1097
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1098
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1099
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1100
        public Type convert(final MethodVisitor method, final Type to) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1101
            throw new UnsupportedOperationException("convert => " + to);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1102
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1103
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1104
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1105
        public void _return(final MethodVisitor method) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1106
            throw new UnsupportedOperationException("return");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1107
       }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1108
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1109
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1110
        public Type add(final MethodVisitor method, final int programPoint) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1111
            throw new UnsupportedOperationException("add");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1112
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1113
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1114
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1115
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1116
     * This is the unknown type which is used as initial type for type
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1117
     * inference. It has the minimum type width
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1118
     */
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1119
    public static final Type UNKNOWN = new ValueLessType("<unknown>") {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1120
        private static final long serialVersionUID = 1L;
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1121
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1122
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1123
        public String getDescriptor() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1124
            return "<unknown>";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1125
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1126
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1127
        @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1128
        public char getBytecodeStackType() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1129
            return 'U';
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1130
        }
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1131
    };
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1132
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1133
    /**
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1134
     * This is the unknown type which is used as initial type for type
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1135
     * inference. It has the minimum type width
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1136
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1137
    public static final Type SLOT_2 = new ValueLessType("<slot_2>") {
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1138
        private static final long serialVersionUID = 1L;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1139
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1140
        @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1141
        public String getDescriptor() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1142
            return "<slot_2>";
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1143
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1144
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1145
        @Override
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1146
        public char getBytecodeStackType() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 22368
diff changeset
  1147
            throw new UnsupportedOperationException("getBytecodeStackType");
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1148
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1149
    };
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1150
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 24751
diff changeset
  1151
    private static <T extends Type> T putInCache(final T type) {
18859
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
  1152
        cache.put(type.getTypeClass(), type);
7c4d0146ccd5 8009758: reactivate the 8006529 test.
attila
parents: 18633
diff changeset
  1153
        return type;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1154
    }
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1155
27365
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
  1156
    /**
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
  1157
     * Read resolve
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
  1158
     * @return resolved type
6dd6e324d1c7 8057825: Bug in apply specialization - if an apply specialization that is available doesn't fit, a new one wouldn't be installed, if the new code generated as a specialization didn't manage to do the apply specialization. Basically changing a conditional to an unconditional.
lagergren
parents: 27361
diff changeset
  1159
     */
27204
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1160
    protected final Object readResolve() {
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1161
        return Type.typeFor(clazz);
06ec78f29a56 8059843: Make AST serializable
attila
parents: 26887
diff changeset
  1162
    }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
  1163
}