rt/emul/compact/src/main/java/java/lang/invoke/MethodTypeForm.java
branchjdk8
changeset 1675 cd50c1894ce5
parent 1674 eca8e9c3ec3e
child 1678 35daab73e225
     1.1 --- a/rt/emul/compact/src/main/java/java/lang/invoke/MethodTypeForm.java	Sun Aug 17 20:09:05 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,388 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.  Oracle designates this
    1.11 - * particular file as subject to the "Classpath" exception as provided
    1.12 - * by Oracle in the LICENSE file that accompanied this code.
    1.13 - *
    1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 - * version 2 for more details (a copy is included in the LICENSE file that
    1.18 - * accompanied this code).
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License version
    1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 - *
    1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 - * or visit www.oracle.com if you need additional information or have any
    1.26 - * questions.
    1.27 - */
    1.28 -
    1.29 -package java.lang.invoke;
    1.30 -
    1.31 -import sun.invoke.util.Wrapper;
    1.32 -import static java.lang.invoke.MethodHandleStatics.*;
    1.33 -import static java.lang.invoke.MethodHandleNatives.Constants.*;
    1.34 - import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
    1.35 -
    1.36 -/**
    1.37 - * Shared information for a group of method types, which differ
    1.38 - * only by reference types, and therefore share a common erasure
    1.39 - * and wrapping.
    1.40 - * <p>
    1.41 - * For an empirical discussion of the structure of method types,
    1.42 - * see <a href="http://groups.google.com/group/jvm-languages/browse_thread/thread/ac9308ae74da9b7e/">
    1.43 - * the thread "Avoiding Boxing" on jvm-languages</a>.
    1.44 - * There are approximately 2000 distinct erased method types in the JDK.
    1.45 - * There are a little over 10 times that number of unerased types.
    1.46 - * No more than half of these are likely to be loaded at once.
    1.47 - * @author John Rose
    1.48 - */
    1.49 -final class MethodTypeForm {
    1.50 -    final int[] argToSlotTable, slotToArgTable;
    1.51 -    final long argCounts;               // packed slot & value counts
    1.52 -    final long primCounts;              // packed prim & double counts
    1.53 -    final int vmslots;                  // total number of parameter slots
    1.54 -    final MethodType erasedType;        // the canonical erasure
    1.55 -    final MethodType basicType;         // the canonical erasure, with primitives simplified
    1.56 -
    1.57 -    // Cached adapter information:
    1.58 -    @Stable String typeString;           // argument type signature characters
    1.59 -    @Stable MethodHandle genericInvoker; // JVM hook for inexact invoke
    1.60 -    @Stable MethodHandle basicInvoker;   // cached instance of MH.invokeBasic
    1.61 -    @Stable MethodHandle namedFunctionInvoker; // cached helper for LF.NamedFunction
    1.62 -
    1.63 -    // Cached lambda form information, for basic types only:
    1.64 -    final @Stable LambdaForm[] lambdaForms;
    1.65 -    // Indexes into lambdaForms:
    1.66 -    static final int
    1.67 -            LF_INVVIRTUAL     =  0,  // DMH invokeVirtual
    1.68 -            LF_INVSTATIC      =  1,
    1.69 -            LF_INVSPECIAL     =  2,
    1.70 -            LF_NEWINVSPECIAL  =  3,
    1.71 -            LF_INVINTERFACE   =  4,
    1.72 -            LF_INVSTATIC_INIT =  5,  // DMH invokeStatic with <clinit> barrier
    1.73 -            LF_INTERPRET      =  6,  // LF interpreter
    1.74 -            LF_COUNTER        =  7,  // CMH wrapper
    1.75 -            LF_REINVOKE       =  8,  // other wrapper
    1.76 -            LF_EX_LINKER      =  9,  // invokeExact_MT
    1.77 -            LF_EX_INVOKER     = 10,  // invokeExact MH
    1.78 -            LF_GEN_LINKER     = 11,
    1.79 -            LF_GEN_INVOKER    = 12,
    1.80 -            LF_CS_LINKER      = 13,  // linkToCallSite_CS
    1.81 -            LF_MH_LINKER      = 14,  // linkToCallSite_MH
    1.82 -            LF_LIMIT          = 15;
    1.83 -
    1.84 -    public MethodType erasedType() {
    1.85 -        return erasedType;
    1.86 -    }
    1.87 -
    1.88 -    public MethodType basicType() {
    1.89 -        return basicType;
    1.90 -    }
    1.91 -
    1.92 -    public LambdaForm cachedLambdaForm(int which) {
    1.93 -        return lambdaForms[which];
    1.94 -    }
    1.95 -
    1.96 -    public LambdaForm setCachedLambdaForm(int which, LambdaForm form) {
    1.97 -        // Should we perform some sort of CAS, to avoid racy duplication?
    1.98 -        return lambdaForms[which] = form;
    1.99 -    }
   1.100 -
   1.101 -    public MethodHandle basicInvoker() {
   1.102 -        assert(erasedType == basicType) : "erasedType: " + erasedType + " != basicType: " + basicType;  // primitives must be flattened also
   1.103 -        MethodHandle invoker = basicInvoker;
   1.104 -        if (invoker != null)  return invoker;
   1.105 -        invoker = DirectMethodHandle.make(invokeBasicMethod(basicType));
   1.106 -        basicInvoker = invoker;
   1.107 -        return invoker;
   1.108 -    }
   1.109 -
   1.110 -    // This next one is called from LambdaForm.NamedFunction.<init>.
   1.111 -    /*non-public*/ static MemberName invokeBasicMethod(MethodType basicType) {
   1.112 -        assert(basicType == basicType.basicType());
   1.113 -        try {
   1.114 -            // Do approximately the same as this public API call:
   1.115 -            //   Lookup.findVirtual(MethodHandle.class, name, type);
   1.116 -            // But bypass access and corner case checks, since we know exactly what we need.
   1.117 -            return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType);
   1.118 -         } catch (ReflectiveOperationException ex) {
   1.119 -            throw newInternalError("JVM cannot find invoker for "+basicType, ex);
   1.120 -        }
   1.121 -    }
   1.122 -
   1.123 -    /**
   1.124 -     * Build an MTF for a given type, which must have all references erased to Object.
   1.125 -     * This MTF will stand for that type and all un-erased variations.
   1.126 -     * Eagerly compute some basic properties of the type, common to all variations.
   1.127 -     */
   1.128 -    protected MethodTypeForm(MethodType erasedType) {
   1.129 -        this.erasedType = erasedType;
   1.130 -
   1.131 -        Class<?>[] ptypes = erasedType.ptypes();
   1.132 -        int ptypeCount = ptypes.length;
   1.133 -        int pslotCount = ptypeCount;            // temp. estimate
   1.134 -        int rtypeCount = 1;                     // temp. estimate
   1.135 -        int rslotCount = 1;                     // temp. estimate
   1.136 -
   1.137 -        int[] argToSlotTab = null, slotToArgTab = null;
   1.138 -
   1.139 -        // Walk the argument types, looking for primitives.
   1.140 -        int pac = 0, lac = 0, prc = 0, lrc = 0;
   1.141 -        Class<?>[] epts = ptypes;
   1.142 -        Class<?>[] bpts = epts;
   1.143 -        for (int i = 0; i < epts.length; i++) {
   1.144 -            Class<?> pt = epts[i];
   1.145 -            if (pt != Object.class) {
   1.146 -                ++pac;
   1.147 -                Wrapper w = Wrapper.forPrimitiveType(pt);
   1.148 -                if (w.isDoubleWord())  ++lac;
   1.149 -                if (w.isSubwordOrInt() && pt != int.class) {
   1.150 -                    if (bpts == epts)
   1.151 -                        bpts = bpts.clone();
   1.152 -                    bpts[i] = int.class;
   1.153 -                }
   1.154 -            }
   1.155 -        }
   1.156 -        pslotCount += lac;                  // #slots = #args + #longs
   1.157 -        Class<?> rt = erasedType.returnType();
   1.158 -        Class<?> bt = rt;
   1.159 -        if (rt != Object.class) {
   1.160 -            ++prc;          // even void.class counts as a prim here
   1.161 -            Wrapper w = Wrapper.forPrimitiveType(rt);
   1.162 -            if (w.isDoubleWord())  ++lrc;
   1.163 -            if (w.isSubwordOrInt() && rt != int.class)
   1.164 -                bt = int.class;
   1.165 -            // adjust #slots, #args
   1.166 -            if (rt == void.class)
   1.167 -                rtypeCount = rslotCount = 0;
   1.168 -            else
   1.169 -                rslotCount += lrc;
   1.170 -        }
   1.171 -        if (epts == bpts && bt == rt) {
   1.172 -            this.basicType = erasedType;
   1.173 -        } else {
   1.174 -            this.basicType = MethodType.makeImpl(bt, bpts, true);
   1.175 -        }
   1.176 -        if (lac != 0) {
   1.177 -            int slot = ptypeCount + lac;
   1.178 -            slotToArgTab = new int[slot+1];
   1.179 -            argToSlotTab = new int[1+ptypeCount];
   1.180 -            argToSlotTab[0] = slot;  // argument "-1" is past end of slots
   1.181 -            for (int i = 0; i < epts.length; i++) {
   1.182 -                Class<?> pt = epts[i];
   1.183 -                Wrapper w = Wrapper.forBasicType(pt);
   1.184 -                if (w.isDoubleWord())  --slot;
   1.185 -                --slot;
   1.186 -                slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
   1.187 -                argToSlotTab[1+i]  = slot;
   1.188 -            }
   1.189 -            assert(slot == 0);  // filled the table
   1.190 -        }
   1.191 -        this.primCounts = pack(lrc, prc, lac, pac);
   1.192 -        this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount);
   1.193 -        if (slotToArgTab == null) {
   1.194 -            int slot = ptypeCount; // first arg is deepest in stack
   1.195 -            slotToArgTab = new int[slot+1];
   1.196 -            argToSlotTab = new int[1+ptypeCount];
   1.197 -            argToSlotTab[0] = slot;  // argument "-1" is past end of slots
   1.198 -            for (int i = 0; i < ptypeCount; i++) {
   1.199 -                --slot;
   1.200 -                slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
   1.201 -                argToSlotTab[1+i]  = slot;
   1.202 -            }
   1.203 -        }
   1.204 -        this.argToSlotTable = argToSlotTab;
   1.205 -        this.slotToArgTable = slotToArgTab;
   1.206 -
   1.207 -        if (pslotCount >= 256)  throw newIllegalArgumentException("too many arguments");
   1.208 -
   1.209 -        // send a few bits down to the JVM:
   1.210 -        this.vmslots = parameterSlotCount();
   1.211 -
   1.212 -        if (basicType == erasedType) {
   1.213 -            lambdaForms = new LambdaForm[LF_LIMIT];
   1.214 -        } else {
   1.215 -            lambdaForms = null;  // could be basicType.form().lambdaForms;
   1.216 -        }
   1.217 -    }
   1.218 -
   1.219 -    private static long pack(int a, int b, int c, int d) {
   1.220 -        assert(((a|b|c|d) & ~0xFFFF) == 0);
   1.221 -        long hw = ((a << 16) | b), lw = ((c << 16) | d);
   1.222 -        return (hw << 32) | lw;
   1.223 -    }
   1.224 -    private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
   1.225 -        assert(word <= 3);
   1.226 -        return (char)(packed >> ((3-word) * 16));
   1.227 -    }
   1.228 -
   1.229 -    public int parameterCount() {                      // # outgoing values
   1.230 -        return unpack(argCounts, 3);
   1.231 -    }
   1.232 -    public int parameterSlotCount() {                  // # outgoing interpreter slots
   1.233 -        return unpack(argCounts, 2);
   1.234 -    }
   1.235 -    public int returnCount() {                         // = 0 (V), or 1
   1.236 -        return unpack(argCounts, 1);
   1.237 -    }
   1.238 -    public int returnSlotCount() {                     // = 0 (V), 2 (J/D), or 1
   1.239 -        return unpack(argCounts, 0);
   1.240 -    }
   1.241 -    public int primitiveParameterCount() {
   1.242 -        return unpack(primCounts, 3);
   1.243 -    }
   1.244 -    public int longPrimitiveParameterCount() {
   1.245 -        return unpack(primCounts, 2);
   1.246 -    }
   1.247 -    public int primitiveReturnCount() {                // = 0 (obj), or 1
   1.248 -        return unpack(primCounts, 1);
   1.249 -    }
   1.250 -    public int longPrimitiveReturnCount() {            // = 1 (J/D), or 0
   1.251 -        return unpack(primCounts, 0);
   1.252 -    }
   1.253 -    public boolean hasPrimitives() {
   1.254 -        return primCounts != 0;
   1.255 -    }
   1.256 -    public boolean hasNonVoidPrimitives() {
   1.257 -        if (primCounts == 0)  return false;
   1.258 -        if (primitiveParameterCount() != 0)  return true;
   1.259 -        return (primitiveReturnCount() != 0 && returnCount() != 0);
   1.260 -    }
   1.261 -    public boolean hasLongPrimitives() {
   1.262 -        return (longPrimitiveParameterCount() | longPrimitiveReturnCount()) != 0;
   1.263 -    }
   1.264 -    public int parameterToArgSlot(int i) {
   1.265 -        return argToSlotTable[1+i];
   1.266 -    }
   1.267 -    public int argSlotToParameter(int argSlot) {
   1.268 -        // Note:  Empty slots are represented by zero in this table.
   1.269 -        // Valid arguments slots contain incremented entries, so as to be non-zero.
   1.270 -        // We return -1 the caller to mean an empty slot.
   1.271 -        return slotToArgTable[argSlot] - 1;
   1.272 -    }
   1.273 -
   1.274 -    static MethodTypeForm findForm(MethodType mt) {
   1.275 -        MethodType erased = canonicalize(mt, ERASE, ERASE);
   1.276 -        if (erased == null) {
   1.277 -            // It is already erased.  Make a new MethodTypeForm.
   1.278 -            return new MethodTypeForm(mt);
   1.279 -        } else {
   1.280 -            // Share the MethodTypeForm with the erased version.
   1.281 -            return erased.form();
   1.282 -        }
   1.283 -    }
   1.284 -
   1.285 -    /** Codes for {@link #canonicalize(java.lang.Class, int)}.
   1.286 -     * ERASE means change every reference to {@code Object}.
   1.287 -     * WRAP means convert primitives (including {@code void} to their
   1.288 -     * corresponding wrapper types.  UNWRAP means the reverse of WRAP.
   1.289 -     * INTS means convert all non-void primitive types to int or long,
   1.290 -     * according to size.  LONGS means convert all non-void primitives
   1.291 -     * to long, regardless of size.  RAW_RETURN means convert a type
   1.292 -     * (assumed to be a return type) to int if it is smaller than an int,
   1.293 -     * or if it is void.
   1.294 -     */
   1.295 -    public static final int NO_CHANGE = 0, ERASE = 1, WRAP = 2, UNWRAP = 3, INTS = 4, LONGS = 5, RAW_RETURN = 6;
   1.296 -
   1.297 -    /** Canonicalize the types in the given method type.
   1.298 -     * If any types change, intern the new type, and return it.
   1.299 -     * Otherwise return null.
   1.300 -     */
   1.301 -    public static MethodType canonicalize(MethodType mt, int howRet, int howArgs) {
   1.302 -        Class<?>[] ptypes = mt.ptypes();
   1.303 -        Class<?>[] ptc = MethodTypeForm.canonicalizes(ptypes, howArgs);
   1.304 -        Class<?> rtype = mt.returnType();
   1.305 -        Class<?> rtc = MethodTypeForm.canonicalize(rtype, howRet);
   1.306 -        if (ptc == null && rtc == null) {
   1.307 -            // It is already canonical.
   1.308 -            return null;
   1.309 -        }
   1.310 -        // Find the erased version of the method type:
   1.311 -        if (rtc == null)  rtc = rtype;
   1.312 -        if (ptc == null)  ptc = ptypes;
   1.313 -        return MethodType.makeImpl(rtc, ptc, true);
   1.314 -    }
   1.315 -
   1.316 -    /** Canonicalize the given return or param type.
   1.317 -     *  Return null if the type is already canonicalized.
   1.318 -     */
   1.319 -    static Class<?> canonicalize(Class<?> t, int how) {
   1.320 -        Class<?> ct;
   1.321 -        if (t == Object.class) {
   1.322 -            // no change, ever
   1.323 -        } else if (!t.isPrimitive()) {
   1.324 -            switch (how) {
   1.325 -                case UNWRAP:
   1.326 -                    ct = Wrapper.asPrimitiveType(t);
   1.327 -                    if (ct != t)  return ct;
   1.328 -                    break;
   1.329 -                case RAW_RETURN:
   1.330 -                case ERASE:
   1.331 -                    return Object.class;
   1.332 -            }
   1.333 -        } else if (t == void.class) {
   1.334 -            // no change, usually
   1.335 -            switch (how) {
   1.336 -                case RAW_RETURN:
   1.337 -                    return int.class;
   1.338 -                case WRAP:
   1.339 -                    return Void.class;
   1.340 -            }
   1.341 -        } else {
   1.342 -            // non-void primitive
   1.343 -            switch (how) {
   1.344 -                case WRAP:
   1.345 -                    return Wrapper.asWrapperType(t);
   1.346 -                case INTS:
   1.347 -                    if (t == int.class || t == long.class)
   1.348 -                        return null;  // no change
   1.349 -                    if (t == double.class)
   1.350 -                        return long.class;
   1.351 -                    return int.class;
   1.352 -                case LONGS:
   1.353 -                    if (t == long.class)
   1.354 -                        return null;  // no change
   1.355 -                    return long.class;
   1.356 -                case RAW_RETURN:
   1.357 -                    if (t == int.class || t == long.class ||
   1.358 -                        t == float.class || t == double.class)
   1.359 -                        return null;  // no change
   1.360 -                    // everything else returns as an int
   1.361 -                    return int.class;
   1.362 -            }
   1.363 -        }
   1.364 -        // no change; return null to signify
   1.365 -        return null;
   1.366 -    }
   1.367 -
   1.368 -    /** Canonicalize each param type in the given array.
   1.369 -     *  Return null if all types are already canonicalized.
   1.370 -     */
   1.371 -    static Class<?>[] canonicalizes(Class<?>[] ts, int how) {
   1.372 -        Class<?>[] cs = null;
   1.373 -        for (int imax = ts.length, i = 0; i < imax; i++) {
   1.374 -            Class<?> c = canonicalize(ts[i], how);
   1.375 -            if (c == void.class)
   1.376 -                c = null;  // a Void parameter was unwrapped to void; ignore
   1.377 -            if (c != null) {
   1.378 -                if (cs == null)
   1.379 -                    cs = ts.clone();
   1.380 -                cs[i] = c;
   1.381 -            }
   1.382 -        }
   1.383 -        return cs;
   1.384 -    }
   1.385 -
   1.386 -    @Override
   1.387 -    public String toString() {
   1.388 -        return "Form"+erasedType;
   1.389 -    }
   1.390 -
   1.391 -}