jaroslav@1646: /*
jaroslav@1646: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1646: *
jaroslav@1646: * This code is free software; you can redistribute it and/or modify it
jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as
jaroslav@1646: * published by the Free Software Foundation. Oracle designates this
jaroslav@1646: * particular file as subject to the "Classpath" exception as provided
jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1646: *
jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1646: * accompanied this code).
jaroslav@1646: *
jaroslav@1646: * You should have received a copy of the GNU General Public License version
jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1646: *
jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1646: * or visit www.oracle.com if you need additional information or have any
jaroslav@1646: * questions.
jaroslav@1646: */
jaroslav@1646:
jaroslav@1646: package java.lang.invoke;
jaroslav@1646:
jaroslav@1646: import sun.invoke.util.Wrapper;
jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*;
jaroslav@1646: import static java.lang.invoke.MethodHandleNatives.Constants.*;
jaroslav@1646: import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
jaroslav@1646:
jaroslav@1646: /**
jaroslav@1646: * Shared information for a group of method types, which differ
jaroslav@1646: * only by reference types, and therefore share a common erasure
jaroslav@1646: * and wrapping.
jaroslav@1646: *
jaroslav@1646: * For an empirical discussion of the structure of method types,
jaroslav@1646: * see
jaroslav@1646: * the thread "Avoiding Boxing" on jvm-languages.
jaroslav@1646: * There are approximately 2000 distinct erased method types in the JDK.
jaroslav@1646: * There are a little over 10 times that number of unerased types.
jaroslav@1646: * No more than half of these are likely to be loaded at once.
jaroslav@1646: * @author John Rose
jaroslav@1646: */
jaroslav@1646: final class MethodTypeForm {
jaroslav@1646: final int[] argToSlotTable, slotToArgTable;
jaroslav@1646: final long argCounts; // packed slot & value counts
jaroslav@1646: final long primCounts; // packed prim & double counts
jaroslav@1646: final int vmslots; // total number of parameter slots
jaroslav@1646: final MethodType erasedType; // the canonical erasure
jaroslav@1646: final MethodType basicType; // the canonical erasure, with primitives simplified
jaroslav@1646:
jaroslav@1646: // Cached adapter information:
jaroslav@1646: @Stable String typeString; // argument type signature characters
jaroslav@1646: @Stable MethodHandle genericInvoker; // JVM hook for inexact invoke
jaroslav@1646: @Stable MethodHandle basicInvoker; // cached instance of MH.invokeBasic
jaroslav@1646: @Stable MethodHandle namedFunctionInvoker; // cached helper for LF.NamedFunction
jaroslav@1646:
jaroslav@1646: // Cached lambda form information, for basic types only:
jaroslav@1646: final @Stable LambdaForm[] lambdaForms;
jaroslav@1646: // Indexes into lambdaForms:
jaroslav@1646: static final int
jaroslav@1646: LF_INVVIRTUAL = 0, // DMH invokeVirtual
jaroslav@1646: LF_INVSTATIC = 1,
jaroslav@1646: LF_INVSPECIAL = 2,
jaroslav@1646: LF_NEWINVSPECIAL = 3,
jaroslav@1646: LF_INVINTERFACE = 4,
jaroslav@1646: LF_INVSTATIC_INIT = 5, // DMH invokeStatic with barrier
jaroslav@1646: LF_INTERPRET = 6, // LF interpreter
jaroslav@1646: LF_COUNTER = 7, // CMH wrapper
jaroslav@1646: LF_REINVOKE = 8, // other wrapper
jaroslav@1646: LF_EX_LINKER = 9, // invokeExact_MT
jaroslav@1646: LF_EX_INVOKER = 10, // invokeExact MH
jaroslav@1646: LF_GEN_LINKER = 11,
jaroslav@1646: LF_GEN_INVOKER = 12,
jaroslav@1646: LF_CS_LINKER = 13, // linkToCallSite_CS
jaroslav@1646: LF_MH_LINKER = 14, // linkToCallSite_MH
jaroslav@1646: LF_LIMIT = 15;
jaroslav@1646:
jaroslav@1646: public MethodType erasedType() {
jaroslav@1646: return erasedType;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: public MethodType basicType() {
jaroslav@1646: return basicType;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: public LambdaForm cachedLambdaForm(int which) {
jaroslav@1646: return lambdaForms[which];
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: public LambdaForm setCachedLambdaForm(int which, LambdaForm form) {
jaroslav@1646: // Should we perform some sort of CAS, to avoid racy duplication?
jaroslav@1646: return lambdaForms[which] = form;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: public MethodHandle basicInvoker() {
jaroslav@1646: assert(erasedType == basicType) : "erasedType: " + erasedType + " != basicType: " + basicType; // primitives must be flattened also
jaroslav@1646: MethodHandle invoker = basicInvoker;
jaroslav@1646: if (invoker != null) return invoker;
jaroslav@1646: invoker = DirectMethodHandle.make(invokeBasicMethod(basicType));
jaroslav@1646: basicInvoker = invoker;
jaroslav@1646: return invoker;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: // This next one is called from LambdaForm.NamedFunction..
jaroslav@1646: /*non-public*/ static MemberName invokeBasicMethod(MethodType basicType) {
jaroslav@1646: assert(basicType == basicType.basicType());
jaroslav@1646: try {
jaroslav@1646: // Do approximately the same as this public API call:
jaroslav@1646: // Lookup.findVirtual(MethodHandle.class, name, type);
jaroslav@1646: // But bypass access and corner case checks, since we know exactly what we need.
jaroslav@1646: return IMPL_LOOKUP.resolveOrFail(REF_invokeVirtual, MethodHandle.class, "invokeBasic", basicType);
jaroslav@1646: } catch (ReflectiveOperationException ex) {
jaroslav@1646: throw newInternalError("JVM cannot find invoker for "+basicType, ex);
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: /**
jaroslav@1646: * Build an MTF for a given type, which must have all references erased to Object.
jaroslav@1646: * This MTF will stand for that type and all un-erased variations.
jaroslav@1646: * Eagerly compute some basic properties of the type, common to all variations.
jaroslav@1646: */
jaroslav@1646: protected MethodTypeForm(MethodType erasedType) {
jaroslav@1646: this.erasedType = erasedType;
jaroslav@1646:
jaroslav@1646: Class>[] ptypes = erasedType.ptypes();
jaroslav@1646: int ptypeCount = ptypes.length;
jaroslav@1646: int pslotCount = ptypeCount; // temp. estimate
jaroslav@1646: int rtypeCount = 1; // temp. estimate
jaroslav@1646: int rslotCount = 1; // temp. estimate
jaroslav@1646:
jaroslav@1646: int[] argToSlotTab = null, slotToArgTab = null;
jaroslav@1646:
jaroslav@1646: // Walk the argument types, looking for primitives.
jaroslav@1646: int pac = 0, lac = 0, prc = 0, lrc = 0;
jaroslav@1646: Class>[] epts = ptypes;
jaroslav@1646: Class>[] bpts = epts;
jaroslav@1646: for (int i = 0; i < epts.length; i++) {
jaroslav@1646: Class> pt = epts[i];
jaroslav@1646: if (pt != Object.class) {
jaroslav@1646: ++pac;
jaroslav@1646: Wrapper w = Wrapper.forPrimitiveType(pt);
jaroslav@1646: if (w.isDoubleWord()) ++lac;
jaroslav@1646: if (w.isSubwordOrInt() && pt != int.class) {
jaroslav@1646: if (bpts == epts)
jaroslav@1646: bpts = bpts.clone();
jaroslav@1646: bpts[i] = int.class;
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646: pslotCount += lac; // #slots = #args + #longs
jaroslav@1646: Class> rt = erasedType.returnType();
jaroslav@1646: Class> bt = rt;
jaroslav@1646: if (rt != Object.class) {
jaroslav@1646: ++prc; // even void.class counts as a prim here
jaroslav@1646: Wrapper w = Wrapper.forPrimitiveType(rt);
jaroslav@1646: if (w.isDoubleWord()) ++lrc;
jaroslav@1646: if (w.isSubwordOrInt() && rt != int.class)
jaroslav@1646: bt = int.class;
jaroslav@1646: // adjust #slots, #args
jaroslav@1646: if (rt == void.class)
jaroslav@1646: rtypeCount = rslotCount = 0;
jaroslav@1646: else
jaroslav@1646: rslotCount += lrc;
jaroslav@1646: }
jaroslav@1646: if (epts == bpts && bt == rt) {
jaroslav@1646: this.basicType = erasedType;
jaroslav@1646: } else {
jaroslav@1646: this.basicType = MethodType.makeImpl(bt, bpts, true);
jaroslav@1646: }
jaroslav@1646: if (lac != 0) {
jaroslav@1646: int slot = ptypeCount + lac;
jaroslav@1646: slotToArgTab = new int[slot+1];
jaroslav@1646: argToSlotTab = new int[1+ptypeCount];
jaroslav@1646: argToSlotTab[0] = slot; // argument "-1" is past end of slots
jaroslav@1646: for (int i = 0; i < epts.length; i++) {
jaroslav@1646: Class> pt = epts[i];
jaroslav@1646: Wrapper w = Wrapper.forBasicType(pt);
jaroslav@1646: if (w.isDoubleWord()) --slot;
jaroslav@1646: --slot;
jaroslav@1646: slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
jaroslav@1646: argToSlotTab[1+i] = slot;
jaroslav@1646: }
jaroslav@1646: assert(slot == 0); // filled the table
jaroslav@1646: }
jaroslav@1646: this.primCounts = pack(lrc, prc, lac, pac);
jaroslav@1646: this.argCounts = pack(rslotCount, rtypeCount, pslotCount, ptypeCount);
jaroslav@1646: if (slotToArgTab == null) {
jaroslav@1646: int slot = ptypeCount; // first arg is deepest in stack
jaroslav@1646: slotToArgTab = new int[slot+1];
jaroslav@1646: argToSlotTab = new int[1+ptypeCount];
jaroslav@1646: argToSlotTab[0] = slot; // argument "-1" is past end of slots
jaroslav@1646: for (int i = 0; i < ptypeCount; i++) {
jaroslav@1646: --slot;
jaroslav@1646: slotToArgTab[slot] = i+1; // "+1" see argSlotToParameter note
jaroslav@1646: argToSlotTab[1+i] = slot;
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646: this.argToSlotTable = argToSlotTab;
jaroslav@1646: this.slotToArgTable = slotToArgTab;
jaroslav@1646:
jaroslav@1646: if (pslotCount >= 256) throw newIllegalArgumentException("too many arguments");
jaroslav@1646:
jaroslav@1646: // send a few bits down to the JVM:
jaroslav@1646: this.vmslots = parameterSlotCount();
jaroslav@1646:
jaroslav@1646: if (basicType == erasedType) {
jaroslav@1646: lambdaForms = new LambdaForm[LF_LIMIT];
jaroslav@1646: } else {
jaroslav@1646: lambdaForms = null; // could be basicType.form().lambdaForms;
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: private static long pack(int a, int b, int c, int d) {
jaroslav@1646: assert(((a|b|c|d) & ~0xFFFF) == 0);
jaroslav@1646: long hw = ((a << 16) | b), lw = ((c << 16) | d);
jaroslav@1646: return (hw << 32) | lw;
jaroslav@1646: }
jaroslav@1646: private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
jaroslav@1646: assert(word <= 3);
jaroslav@1646: return (char)(packed >> ((3-word) * 16));
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: public int parameterCount() { // # outgoing values
jaroslav@1646: return unpack(argCounts, 3);
jaroslav@1646: }
jaroslav@1646: public int parameterSlotCount() { // # outgoing interpreter slots
jaroslav@1646: return unpack(argCounts, 2);
jaroslav@1646: }
jaroslav@1646: public int returnCount() { // = 0 (V), or 1
jaroslav@1646: return unpack(argCounts, 1);
jaroslav@1646: }
jaroslav@1646: public int returnSlotCount() { // = 0 (V), 2 (J/D), or 1
jaroslav@1646: return unpack(argCounts, 0);
jaroslav@1646: }
jaroslav@1646: public int primitiveParameterCount() {
jaroslav@1646: return unpack(primCounts, 3);
jaroslav@1646: }
jaroslav@1646: public int longPrimitiveParameterCount() {
jaroslav@1646: return unpack(primCounts, 2);
jaroslav@1646: }
jaroslav@1646: public int primitiveReturnCount() { // = 0 (obj), or 1
jaroslav@1646: return unpack(primCounts, 1);
jaroslav@1646: }
jaroslav@1646: public int longPrimitiveReturnCount() { // = 1 (J/D), or 0
jaroslav@1646: return unpack(primCounts, 0);
jaroslav@1646: }
jaroslav@1646: public boolean hasPrimitives() {
jaroslav@1646: return primCounts != 0;
jaroslav@1646: }
jaroslav@1646: public boolean hasNonVoidPrimitives() {
jaroslav@1646: if (primCounts == 0) return false;
jaroslav@1646: if (primitiveParameterCount() != 0) return true;
jaroslav@1646: return (primitiveReturnCount() != 0 && returnCount() != 0);
jaroslav@1646: }
jaroslav@1646: public boolean hasLongPrimitives() {
jaroslav@1646: return (longPrimitiveParameterCount() | longPrimitiveReturnCount()) != 0;
jaroslav@1646: }
jaroslav@1646: public int parameterToArgSlot(int i) {
jaroslav@1646: return argToSlotTable[1+i];
jaroslav@1646: }
jaroslav@1646: public int argSlotToParameter(int argSlot) {
jaroslav@1646: // Note: Empty slots are represented by zero in this table.
jaroslav@1646: // Valid arguments slots contain incremented entries, so as to be non-zero.
jaroslav@1646: // We return -1 the caller to mean an empty slot.
jaroslav@1646: return slotToArgTable[argSlot] - 1;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: static MethodTypeForm findForm(MethodType mt) {
jaroslav@1646: MethodType erased = canonicalize(mt, ERASE, ERASE);
jaroslav@1646: if (erased == null) {
jaroslav@1646: // It is already erased. Make a new MethodTypeForm.
jaroslav@1646: return new MethodTypeForm(mt);
jaroslav@1646: } else {
jaroslav@1646: // Share the MethodTypeForm with the erased version.
jaroslav@1646: return erased.form();
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: /** Codes for {@link #canonicalize(java.lang.Class, int)}.
jaroslav@1646: * ERASE means change every reference to {@code Object}.
jaroslav@1646: * WRAP means convert primitives (including {@code void} to their
jaroslav@1646: * corresponding wrapper types. UNWRAP means the reverse of WRAP.
jaroslav@1646: * INTS means convert all non-void primitive types to int or long,
jaroslav@1646: * according to size. LONGS means convert all non-void primitives
jaroslav@1646: * to long, regardless of size. RAW_RETURN means convert a type
jaroslav@1646: * (assumed to be a return type) to int if it is smaller than an int,
jaroslav@1646: * or if it is void.
jaroslav@1646: */
jaroslav@1646: public static final int NO_CHANGE = 0, ERASE = 1, WRAP = 2, UNWRAP = 3, INTS = 4, LONGS = 5, RAW_RETURN = 6;
jaroslav@1646:
jaroslav@1646: /** Canonicalize the types in the given method type.
jaroslav@1646: * If any types change, intern the new type, and return it.
jaroslav@1646: * Otherwise return null.
jaroslav@1646: */
jaroslav@1646: public static MethodType canonicalize(MethodType mt, int howRet, int howArgs) {
jaroslav@1646: Class>[] ptypes = mt.ptypes();
jaroslav@1646: Class>[] ptc = MethodTypeForm.canonicalizes(ptypes, howArgs);
jaroslav@1646: Class> rtype = mt.returnType();
jaroslav@1646: Class> rtc = MethodTypeForm.canonicalize(rtype, howRet);
jaroslav@1646: if (ptc == null && rtc == null) {
jaroslav@1646: // It is already canonical.
jaroslav@1646: return null;
jaroslav@1646: }
jaroslav@1646: // Find the erased version of the method type:
jaroslav@1646: if (rtc == null) rtc = rtype;
jaroslav@1646: if (ptc == null) ptc = ptypes;
jaroslav@1646: return MethodType.makeImpl(rtc, ptc, true);
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: /** Canonicalize the given return or param type.
jaroslav@1646: * Return null if the type is already canonicalized.
jaroslav@1646: */
jaroslav@1646: static Class> canonicalize(Class> t, int how) {
jaroslav@1646: Class> ct;
jaroslav@1646: if (t == Object.class) {
jaroslav@1646: // no change, ever
jaroslav@1646: } else if (!t.isPrimitive()) {
jaroslav@1646: switch (how) {
jaroslav@1646: case UNWRAP:
jaroslav@1646: ct = Wrapper.asPrimitiveType(t);
jaroslav@1646: if (ct != t) return ct;
jaroslav@1646: break;
jaroslav@1646: case RAW_RETURN:
jaroslav@1646: case ERASE:
jaroslav@1646: return Object.class;
jaroslav@1646: }
jaroslav@1646: } else if (t == void.class) {
jaroslav@1646: // no change, usually
jaroslav@1646: switch (how) {
jaroslav@1646: case RAW_RETURN:
jaroslav@1646: return int.class;
jaroslav@1646: case WRAP:
jaroslav@1646: return Void.class;
jaroslav@1646: }
jaroslav@1646: } else {
jaroslav@1646: // non-void primitive
jaroslav@1646: switch (how) {
jaroslav@1646: case WRAP:
jaroslav@1646: return Wrapper.asWrapperType(t);
jaroslav@1646: case INTS:
jaroslav@1646: if (t == int.class || t == long.class)
jaroslav@1646: return null; // no change
jaroslav@1646: if (t == double.class)
jaroslav@1646: return long.class;
jaroslav@1646: return int.class;
jaroslav@1646: case LONGS:
jaroslav@1646: if (t == long.class)
jaroslav@1646: return null; // no change
jaroslav@1646: return long.class;
jaroslav@1646: case RAW_RETURN:
jaroslav@1646: if (t == int.class || t == long.class ||
jaroslav@1646: t == float.class || t == double.class)
jaroslav@1646: return null; // no change
jaroslav@1646: // everything else returns as an int
jaroslav@1646: return int.class;
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646: // no change; return null to signify
jaroslav@1646: return null;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: /** Canonicalize each param type in the given array.
jaroslav@1646: * Return null if all types are already canonicalized.
jaroslav@1646: */
jaroslav@1646: static Class>[] canonicalizes(Class>[] ts, int how) {
jaroslav@1646: Class>[] cs = null;
jaroslav@1646: for (int imax = ts.length, i = 0; i < imax; i++) {
jaroslav@1646: Class> c = canonicalize(ts[i], how);
jaroslav@1646: if (c == void.class)
jaroslav@1646: c = null; // a Void parameter was unwrapped to void; ignore
jaroslav@1646: if (c != null) {
jaroslav@1646: if (cs == null)
jaroslav@1646: cs = ts.clone();
jaroslav@1646: cs[i] = c;
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646: return cs;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: @Override
jaroslav@1646: public String toString() {
jaroslav@1646: return "Form"+erasedType;
jaroslav@1646: }
jaroslav@1646:
jaroslav@1646: }