jdk/src/java.base/share/classes/sun/invoke/util/ValueConversions.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 26475 acf0c98309d3
child 40256 c5e03eaf7ba2
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20533
diff changeset
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    26
package sun.invoke.util;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    28
import java.lang.invoke.MethodHandle;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    29
import java.lang.invoke.MethodHandles;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    30
import java.lang.invoke.MethodHandles.Lookup;
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 8821
diff changeset
    31
import java.lang.invoke.MethodType;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    32
import java.util.EnumMap;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    33
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    34
public class ValueConversions {
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    35
    private static final Class<?> THIS_CLASS = ValueConversions.class;
8821
2836ee97ee27 6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents: 8347
diff changeset
    36
    private static final Lookup IMPL_LOOKUP = MethodHandles.lookup();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    37
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    38
    /** Thread-safe canonicalized mapping from Wrapper to MethodHandle
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    39
     * with unsynchronized reads and synchronized writes.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    40
     * It's safe to publish MethodHandles by data race because they are immutable. */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    41
    private static class WrapperCache {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    42
        /** EnumMap uses preconstructed array internally, which is constant during it's lifetime. */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    43
        private final EnumMap<Wrapper, MethodHandle> map = new EnumMap<>(Wrapper.class);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    44
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    45
        public MethodHandle get(Wrapper w) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    46
            return map.get(w);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    47
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    48
        public synchronized MethodHandle put(final Wrapper w, final MethodHandle mh) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    49
            // Simulate CAS to avoid racy duplication
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    50
            MethodHandle prev = map.putIfAbsent(w, mh);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    51
            if (prev != null)  return prev;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    52
            return mh;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    53
        }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    54
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    55
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    56
    private static WrapperCache[] newWrapperCaches(int n) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    57
        WrapperCache[] caches = new WrapperCache[n];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    58
        for (int i = 0; i < n; i++)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    59
            caches[i] = new WrapperCache();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    60
        return caches;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    61
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    62
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    63
    /// Converting references to values.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    64
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    65
    // There are several levels of this unboxing conversions:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    66
    //   no conversions:  exactly Integer.valueOf, etc.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    67
    //   implicit conversions sanctioned by JLS 5.1.2, etc.
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    68
    //   explicit conversions as allowed by explicitCastArguments
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    69
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    70
    static int unboxInteger(Integer x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    71
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    72
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    73
    static int unboxInteger(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    74
        if (x instanceof Integer)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    75
            return (Integer) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    76
        return primitiveConversion(Wrapper.INT, x, cast).intValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    77
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    78
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    79
    static byte unboxByte(Byte x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    80
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    81
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    82
    static byte unboxByte(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    83
        if (x instanceof Byte)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    84
            return (Byte) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    85
        return primitiveConversion(Wrapper.BYTE, x, cast).byteValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    86
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    87
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    88
    static short unboxShort(Short x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    89
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    90
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    91
    static short unboxShort(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    92
        if (x instanceof Short)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    93
            return (Short) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
    94
        return primitiveConversion(Wrapper.SHORT, x, cast).shortValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    95
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    96
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    97
    static boolean unboxBoolean(Boolean x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    98
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
    99
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   100
    static boolean unboxBoolean(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   101
        if (x instanceof Boolean)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   102
            return (Boolean) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   103
        return (primitiveConversion(Wrapper.BOOLEAN, x, cast).intValue() & 1) != 0;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   104
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   105
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   106
    static char unboxCharacter(Character x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   107
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   108
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   109
    static char unboxCharacter(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   110
        if (x instanceof Character)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   111
            return (Character) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   112
        return (char) primitiveConversion(Wrapper.CHAR, x, cast).intValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   113
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   114
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   115
    static long unboxLong(Long x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   116
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   117
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   118
    static long unboxLong(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   119
        if (x instanceof Long)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   120
            return (Long) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   121
        return primitiveConversion(Wrapper.LONG, x, cast).longValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   122
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   123
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   124
    static float unboxFloat(Float x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   125
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   126
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   127
    static float unboxFloat(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   128
        if (x instanceof Float)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   129
            return (Float) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   130
        return primitiveConversion(Wrapper.FLOAT, x, cast).floatValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   131
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   132
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   133
    static double unboxDouble(Double x) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   134
        return x;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   135
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   136
    static double unboxDouble(Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   137
        if (x instanceof Double)
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   138
            return (Double) x;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   139
        return primitiveConversion(Wrapper.DOUBLE, x, cast).doubleValue();
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   140
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   141
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   142
    private static MethodType unboxType(Wrapper wrap, int kind) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   143
        if (kind == 0)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   144
            return MethodType.methodType(wrap.primitiveType(), wrap.wrapperType());
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   145
        return MethodType.methodType(wrap.primitiveType(), Object.class, boolean.class);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   146
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   147
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   148
    private static final WrapperCache[] UNBOX_CONVERSIONS = newWrapperCaches(4);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   149
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   150
    private static MethodHandle unbox(Wrapper wrap, int kind) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   151
        // kind 0 -> strongly typed with NPE
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   152
        // kind 1 -> strongly typed but zero for null,
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   153
        // kind 2 -> asType rules: accept multiple box types but only widening conversions with NPE
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   154
        // kind 3 -> explicitCastArguments rules: allow narrowing conversions, zero for null
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   155
        WrapperCache cache = UNBOX_CONVERSIONS[kind];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   156
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   157
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   158
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   159
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   160
        // slow path
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   161
        switch (wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   162
            case OBJECT:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   163
            case VOID:
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   164
                throw new IllegalArgumentException("unbox "+wrap);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   165
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   166
        // look up the method
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   167
        String name = "unbox" + wrap.wrapperSimpleName();
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   168
        MethodType type = unboxType(wrap, kind);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   169
        try {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   170
            mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   171
        } catch (ReflectiveOperationException ex) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   172
            mh = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   173
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   174
        if (mh != null) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   175
            if (kind > 0) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   176
                boolean cast = (kind != 2);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   177
                mh = MethodHandles.insertArguments(mh, 1, cast);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   178
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   179
            if (kind == 1) {  // casting but exact (null -> zero)
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   180
                mh = mh.asType(unboxType(wrap, 0));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   181
            }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   182
            return cache.put(wrap, mh);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   183
        }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   184
        throw new IllegalArgumentException("cannot find unbox adapter for " + wrap
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   185
                + (kind <= 1 ? " (exact)" : kind == 3 ? " (cast)" : ""));
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   186
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   187
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   188
    /** Return an exact unboxer for the given primitive type. */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   189
    public static MethodHandle unboxExact(Wrapper type) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   190
        return unbox(type, 0);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   191
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   192
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   193
    /** Return an exact unboxer for the given primitive type, with optional null-to-zero conversion.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   194
     *  The boolean says whether to throw an NPE on a null value (false means unbox a zero).
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   195
     *  The type of the unboxer is of a form like (Integer)int.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   196
     */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   197
    public static MethodHandle unboxExact(Wrapper type, boolean throwNPE) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   198
        return unbox(type, throwNPE ? 0 : 1);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   199
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   200
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   201
    /** Return a widening unboxer for the given primitive type.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   202
     *  Widen narrower primitive boxes to the given type.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   203
     *  Do not narrow any primitive values or convert null to zero.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   204
     *  The type of the unboxer is of a form like (Object)int.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   205
     */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   206
    public static MethodHandle unboxWiden(Wrapper type) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   207
        return unbox(type, 2);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   208
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   209
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   210
    /** Return a casting unboxer for the given primitive type.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   211
     *  Widen or narrow primitive values to the given type, or convert null to zero, as needed.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   212
     *  The type of the unboxer is of a form like (Object)int.
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   213
     */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   214
    public static MethodHandle unboxCast(Wrapper type) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   215
        return unbox(type, 3);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   216
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   217
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 26475
diff changeset
   218
    private static final Integer ZERO_INT = 0, ONE_INT = 1;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   219
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   220
    /// Primitive conversions
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   221
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   222
     * Produce a Number which represents the given value {@code x}
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   223
     * according to the primitive type of the given wrapper {@code wrap}.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   224
     * Caller must invoke intValue, byteValue, longValue (etc.) on the result
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   225
     * to retrieve the desired primitive value.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   226
     */
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   227
    public static Number primitiveConversion(Wrapper wrap, Object x, boolean cast) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   228
        // Maybe merge this code with Wrapper.convert/cast.
13610
28122b96858e 7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents: 13423
diff changeset
   229
        Number res;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   230
        if (x == null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   231
            if (!cast)  return null;
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   232
            return ZERO_INT;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   233
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   234
        if (x instanceof Number) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   235
            res = (Number) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   236
        } else if (x instanceof Boolean) {
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   237
            res = ((boolean)x ? ONE_INT : ZERO_INT);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   238
        } else if (x instanceof Character) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   239
            res = (int)(char)x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   240
        } else {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   241
            // this will fail with the required ClassCastException:
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   242
            res = (Number) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   243
        }
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   244
        Wrapper xwrap = Wrapper.findWrapperType(x.getClass());
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   245
        if (xwrap == null || !cast && !wrap.isConvertibleFrom(xwrap))
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   246
            // this will fail with the required ClassCastException:
9752
88ab34b6da6d 7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents: 9646
diff changeset
   247
            return (Number) wrap.wrapperType().cast(x);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   248
        return res;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   249
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   250
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   251
    /**
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   252
     * The JVM verifier allows boolean, byte, short, or char to widen to int.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   253
     * Support exactly this conversion, from a boxed value type Boolean,
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   254
     * Byte, Short, Character, or Integer.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   255
     */
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   256
    public static int widenSubword(Object x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   257
        if (x instanceof Integer)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   258
            return (int) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   259
        else if (x instanceof Boolean)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   260
            return fromBoolean((boolean) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   261
        else if (x instanceof Character)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   262
            return (char) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   263
        else if (x instanceof Short)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   264
            return (short) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   265
        else if (x instanceof Byte)
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   266
            return (byte) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   267
        else
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   268
            // Fail with a ClassCastException.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   269
            return (int) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   270
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   271
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   272
    /// Converting primitives to references
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   273
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   274
    static Integer boxInteger(int x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   275
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   276
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   277
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   278
    static Byte boxByte(byte x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   279
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   280
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   281
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   282
    static Short boxShort(short x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   283
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   284
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   285
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   286
    static Boolean boxBoolean(boolean x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   287
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   288
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   289
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   290
    static Character boxCharacter(char x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   291
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   292
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   293
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   294
    static Long boxLong(long x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   295
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   296
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   297
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   298
    static Float boxFloat(float x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   299
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   300
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   301
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   302
    static Double boxDouble(double x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   303
        return x;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   304
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   305
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   306
    private static MethodType boxType(Wrapper wrap) {
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   307
        // be exact, since return casts are hard to compose
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   308
        Class<?> boxType = wrap.wrapperType();
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   309
        return MethodType.methodType(boxType, wrap.primitiveType());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   310
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   311
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   312
    private static final WrapperCache[] BOX_CONVERSIONS = newWrapperCaches(1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   313
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   314
    public static MethodHandle boxExact(Wrapper wrap) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   315
        WrapperCache cache = BOX_CONVERSIONS[0];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   316
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   317
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   318
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   319
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   320
        // look up the method
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   321
        String name = "box" + wrap.wrapperSimpleName();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   322
        MethodType type = boxType(wrap);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   323
        try {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   324
            mh = IMPL_LOOKUP.findStatic(THIS_CLASS, name, type);
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   325
        } catch (ReflectiveOperationException ex) {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   326
            mh = null;
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   327
        }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   328
        if (mh != null) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   329
            return cache.put(wrap, mh);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   330
        }
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   331
        throw new IllegalArgumentException("cannot find box adapter for " + wrap);
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   332
    }
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   333
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   334
    /// Constant functions
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   335
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   336
    static void ignore(Object x) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   337
        // no value to return; this is an unbox of null
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   338
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   339
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   340
    static void empty() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   341
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   342
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   343
    static Object zeroObject() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   344
        return null;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   345
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   346
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   347
    static int zeroInteger() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   348
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   349
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   350
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   351
    static long zeroLong() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   352
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   353
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   354
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   355
    static float zeroFloat() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   356
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   357
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   358
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   359
    static double zeroDouble() {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   360
        return 0;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   361
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   362
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   363
    private static final WrapperCache[] CONSTANT_FUNCTIONS = newWrapperCaches(2);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   364
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   365
    public static MethodHandle zeroConstantFunction(Wrapper wrap) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   366
        WrapperCache cache = CONSTANT_FUNCTIONS[0];
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   367
        MethodHandle mh = cache.get(wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   368
        if (mh != null) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   369
            return mh;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   370
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   371
        // slow path
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   372
        MethodType type = MethodType.methodType(wrap.primitiveType());
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   373
        switch (wrap) {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   374
            case VOID:
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   375
                mh = EMPTY;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   376
                break;
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   377
            case OBJECT:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   378
            case INT: case LONG: case FLOAT: case DOUBLE:
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   379
                try {
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   380
                    mh = IMPL_LOOKUP.findStatic(THIS_CLASS, "zero"+wrap.wrapperSimpleName(), type);
8347
e5daa5772ffd 7013730: JSR 292 reflective operations should report errors with standard exception types
jrose
parents: 7668
diff changeset
   381
                } catch (ReflectiveOperationException ex) {
7052
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   382
                    mh = null;
963a5baf2ba3 6980096: JSR 292 reflective lookup should throw checked exceptions
jrose
parents: 5725
diff changeset
   383
                }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   384
                break;
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   385
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   386
        if (mh != null) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   387
            return cache.put(wrap, mh);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   388
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   389
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   390
        // use zeroInt and cast the result
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   391
        if (wrap.isSubwordOrInt() && wrap != Wrapper.INT) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   392
            mh = MethodHandles.explicitCastArguments(zeroConstantFunction(Wrapper.INT), type);
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   393
            return cache.put(wrap, mh);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   394
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   395
        throw new IllegalArgumentException("cannot find zero constant for " + wrap);
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   396
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   397
26475
acf0c98309d3 8050884: Intrinsify ValueConversions.identity() functions
vlivanov
parents: 26474
diff changeset
   398
    private static final MethodHandle CAST_REFERENCE, IGNORE, EMPTY;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   399
    static {
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   400
        try {
4537
7c3c7f8d5195 6914665: update jdk code for JSR 292 (post 6858164)
jrose
parents: 2707
diff changeset
   401
            MethodType idType = MethodType.genericMethodType(1);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   402
            MethodType ignoreType = idType.changeReturnType(void.class);
23039
6ee70bc18809 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke
vlivanov
parents: 23010
diff changeset
   403
            CAST_REFERENCE = IMPL_LOOKUP.findVirtual(Class.class, "cast", idType);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   404
            IGNORE = IMPL_LOOKUP.findStatic(THIS_CLASS, "ignore", ignoreType);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   405
            EMPTY = IMPL_LOOKUP.findStatic(THIS_CLASS, "empty", ignoreType.dropParameterTypes(0, 1));
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   406
        } catch (NoSuchMethodException | IllegalAccessException ex) {
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   407
            throw newInternalError("uncaught exception", ex);
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   408
        }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   409
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   410
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   411
    public static MethodHandle ignore() {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   412
        return IGNORE;
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   413
    }
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   414
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   415
    /** Return a method that casts its second argument (an Object) to the given type (a Class). */
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   416
    public static MethodHandle cast() {
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   417
        return CAST_REFERENCE;
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   418
    }
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   419
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   420
    /// Primitive conversions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   421
    // These are supported directly by the JVM, usually by a single instruction.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   422
    // In the case of narrowing to a subword, there may be a pair of instructions.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   423
    // In the case of booleans, there may be a helper routine to manage a 1-bit value.
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   424
    // This is the full 8x8 matrix (minus the diagonal).
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   425
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   426
    // narrow double to all other types:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   427
    static float doubleToFloat(double x) {  // bytecode: d2f
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   428
        return (float) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   429
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   430
    static long doubleToLong(double x) {  // bytecode: d2l
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   431
        return (long) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   432
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   433
    static int doubleToInt(double x) {  // bytecode: d2i
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   434
        return (int) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   435
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   436
    static short doubleToShort(double x) {  // bytecodes: d2i, i2s
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   437
        return (short) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   438
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   439
    static char doubleToChar(double x) {  // bytecodes: d2i, i2c
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   440
        return (char) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   441
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   442
    static byte doubleToByte(double x) {  // bytecodes: d2i, i2b
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   443
        return (byte) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   444
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   445
    static boolean doubleToBoolean(double x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   446
        return toBoolean((byte) x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   447
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   448
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   449
    // widen float:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   450
    static double floatToDouble(float x) {  // bytecode: f2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   451
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   452
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   453
    // narrow float:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   454
    static long floatToLong(float x) {  // bytecode: f2l
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   455
        return (long) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   456
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   457
    static int floatToInt(float x) {  // bytecode: f2i
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   458
        return (int) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   459
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   460
    static short floatToShort(float x) {  // bytecodes: f2i, i2s
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   461
        return (short) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   462
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   463
    static char floatToChar(float x) {  // bytecodes: f2i, i2c
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   464
        return (char) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   465
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   466
    static byte floatToByte(float x) {  // bytecodes: f2i, i2b
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   467
        return (byte) x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   468
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   469
    static boolean floatToBoolean(float x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   470
        return toBoolean((byte) x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   471
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   472
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   473
    // widen long:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   474
    static double longToDouble(long x) {  // bytecode: l2d
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   475
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   476
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   477
    static float longToFloat(long x) {  // bytecode: l2f
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   478
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   479
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   480
    // narrow long:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   481
    static int longToInt(long x) {  // bytecode: l2i
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   482
        return (int) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   483
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   484
    static short longToShort(long x) {  // bytecodes: f2i, i2s
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   485
        return (short) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   486
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   487
    static char longToChar(long x) {  // bytecodes: f2i, i2c
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   488
        return (char) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   489
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   490
    static byte longToByte(long x) {  // bytecodes: f2i, i2b
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   491
        return (byte) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   492
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   493
    static boolean longToBoolean(long x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   494
        return toBoolean((byte) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   495
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   496
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   497
    // widen int:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   498
    static double intToDouble(int x) {  // bytecode: i2d
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   499
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   500
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   501
    static float intToFloat(int x) {  // bytecode: i2f
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   502
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   503
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   504
    static long intToLong(int x) {  // bytecode: i2l
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   505
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   506
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   507
    // narrow int:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   508
    static short intToShort(int x) {  // bytecode: i2s
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   509
        return (short) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   510
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   511
    static char intToChar(int x) {  // bytecode: i2c
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   512
        return (char) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   513
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   514
    static byte intToByte(int x) {  // bytecode: i2b
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   515
        return (byte) x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   516
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   517
    static boolean intToBoolean(int x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   518
        return toBoolean((byte) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   519
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   520
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   521
    // widen short:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   522
    static double shortToDouble(short x) {  // bytecode: i2d (implicit 's2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   523
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   524
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   525
    static float shortToFloat(short x) {  // bytecode: i2f (implicit 's2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   526
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   527
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   528
    static long shortToLong(short x) {  // bytecode: i2l (implicit 's2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   529
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   530
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   531
    static int shortToInt(short x) {  // (implicit 's2i')
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   532
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   533
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   534
    // narrow short:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   535
    static char shortToChar(short x) {  // bytecode: i2c (implicit 's2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   536
        return (char)x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   537
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   538
    static byte shortToByte(short x) {  // bytecode: i2b (implicit 's2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   539
        return (byte)x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   540
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   541
    static boolean shortToBoolean(short x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   542
        return toBoolean((byte) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   543
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   544
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   545
    // widen char:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   546
    static double charToDouble(char x) {  // bytecode: i2d (implicit 'c2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   547
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   548
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   549
    static float charToFloat(char x) {  // bytecode: i2f (implicit 'c2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   550
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   551
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   552
    static long charToLong(char x) {  // bytecode: i2l (implicit 'c2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   553
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   554
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   555
    static int charToInt(char x) {  // (implicit 'c2i')
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   556
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   557
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   558
    // narrow char:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   559
    static short charToShort(char x) {  // bytecode: i2s (implicit 'c2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   560
        return (short)x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   561
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   562
    static byte charToByte(char x) {  // bytecode: i2b (implicit 'c2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   563
        return (byte)x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   564
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   565
    static boolean charToBoolean(char x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   566
        return toBoolean((byte) x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   567
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   568
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   569
    // widen byte:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   570
    static double byteToDouble(byte x) {  // bytecode: i2d (implicit 'b2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   571
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   572
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   573
    static float byteToFloat(byte x) {  // bytecode: i2f (implicit 'b2i')
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   574
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   575
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   576
    static long byteToLong(byte x) {  // bytecode: i2l (implicit 'b2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   577
        return x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   578
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   579
    static int byteToInt(byte x) {  // (implicit 'b2i')
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   580
        return x;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   581
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   582
    static short byteToShort(byte x) {  // bytecode: i2s (implicit 'b2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   583
        return (short)x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   584
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   585
    static char byteToChar(byte x) {  // bytecode: i2b (implicit 'b2i')
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   586
        return (char)x;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   587
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   588
    // narrow byte to boolean:
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   589
    static boolean byteToBoolean(byte x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   590
        return toBoolean(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   591
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   592
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   593
    // widen boolean to all types:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   594
    static double booleanToDouble(boolean x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   595
        return fromBoolean(x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   596
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   597
    static float booleanToFloat(boolean x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   598
        return fromBoolean(x);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   599
    }
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   600
    static long booleanToLong(boolean x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   601
        return fromBoolean(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   602
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   603
    static int booleanToInt(boolean x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   604
        return fromBoolean(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   605
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   606
    static short booleanToShort(boolean x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   607
        return fromBoolean(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   608
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   609
    static char booleanToChar(boolean x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   610
        return (char)fromBoolean(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   611
    }
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   612
    static byte booleanToByte(boolean x) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   613
        return fromBoolean(x);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   614
    }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   615
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   616
    // helpers to force boolean into the conversion scheme:
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   617
    static boolean toBoolean(byte x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   618
        // see javadoc for MethodHandles.explicitCastArguments
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   619
        return ((x & 1) != 0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   620
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   621
    static byte fromBoolean(boolean x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   622
        // see javadoc for MethodHandles.explicitCastArguments
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   623
        return (x ? (byte)1 : (byte)0);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   624
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   625
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   626
    private static final WrapperCache[] CONVERT_PRIMITIVE_FUNCTIONS = newWrapperCaches(Wrapper.values().length);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   627
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   628
    public static MethodHandle convertPrimitive(Wrapper wsrc, Wrapper wdst) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   629
        WrapperCache cache = CONVERT_PRIMITIVE_FUNCTIONS[wsrc.ordinal()];
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   630
        MethodHandle mh = cache.get(wdst);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   631
        if (mh != null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   632
            return mh;
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   633
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   634
        // slow path
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   635
        Class<?> src = wsrc.primitiveType();
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   636
        Class<?> dst = wdst.primitiveType();
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   637
        MethodType type = MethodType.methodType(dst, src);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   638
        if (wsrc == wdst) {
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   639
            mh = MethodHandles.identity(src);
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   640
        } else {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   641
            assert(src.isPrimitive() && dst.isPrimitive());
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   642
            try {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   643
                mh = IMPL_LOOKUP.findStatic(THIS_CLASS, src.getSimpleName()+"To"+capitalize(dst.getSimpleName()), type);
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   644
            } catch (ReflectiveOperationException ex) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   645
                mh = null;
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   646
            }
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   647
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   648
        if (mh != null) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   649
            assert(mh.type() == type) : mh;
26474
655d08549e43 8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents: 26466
diff changeset
   650
            return cache.put(wdst, mh);
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   651
        }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   652
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   653
        throw new IllegalArgumentException("cannot find primitive conversion function for " +
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   654
                                           src.getSimpleName()+" -> "+dst.getSimpleName());
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   655
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   656
13423
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   657
    public static MethodHandle convertPrimitive(Class<?> src, Class<?> dst) {
17843fff200d 7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents: 11534
diff changeset
   658
        return convertPrimitive(Wrapper.forPrimitiveType(src), Wrapper.forPrimitiveType(dst));
9646
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   659
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   660
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   661
    private static String capitalize(String x) {
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   662
        return Character.toUpperCase(x.charAt(0))+x.substring(1);
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   663
    }
5ebbe5ab084f 6939861: JVM should handle more conversion operations
jrose
parents: 8822
diff changeset
   664
14089
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   665
    // handy shared exception makers (they simplify the common case code)
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   666
    private static InternalError newInternalError(String message, Throwable cause) {
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   667
        return new InternalError(message, cause);
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   668
    }
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   669
    private static InternalError newInternalError(Throwable cause) {
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   670
        return new InternalError(cause);
0a41b980d62a 8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents: 13610
diff changeset
   671
    }
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   672
}