--- /dev/null	2009-01-20 02:47:16.000000000 -0800
+++ new/src/share/projects/meth/src/impl/java/dyn/AdapterMethodHandle.java	2009-01-20 02:47:16.000000000 -0800
@@ -0,0 +1,541 @@
+/*
+ * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package impl.java.dyn;
+
+import impl.java.dyn.util.VerifyType;
+import impl.java.dyn.util.Wrappers;
+import java.dyn.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import static impl.java.dyn.MethodHandleNatives.Constants.*;
+import static impl.java.dyn.MethodHandleImpl.newIllegalArgumentException;
+
+/**
+ * This method handle performs simple conversion or checking of a single argument.
+ * @author jrose
+ */
+public class AdapterMethodHandle extends BoundMethodHandle {
+    //MethodHandle vmtarget;   // next AMH or BMH in chain or final DMH
+    //Object       argument;   // parameter to the conversion if needed
+    //int          vmargslot;  // which argument slot is affected
+    private final int conversion;  // the type of conversion: RETYPE_ONLY, etc.
+
+    // Constructors in this class *must* be package scoped or private.
+    private AdapterMethodHandle(MethodHandle target, MethodType newType,
+                long conv, Object convArg) {
+        super(newType, convArg, newType.parameterSlot(convArgPos(conv)));
+        this.conversion = convCode(conv);
+        if (MethodHandleNatives.JVM_SUPPORT) {
+            // JVM might update VM-specific bits of conversion (ignore)
+            MethodHandleNatives.init(this, target, convArgPos(conv));
+        }
+    }
+    private AdapterMethodHandle(MethodHandle target, MethodType newType,
+                long conv) {
+        this(target, newType, conv, null);
+    }
+
+    private static final Access IMPL_TOKEN = Access.getToken();
+
+    // TO DO:  When adapting another MH with a null conversion, clone
+    // the target and change its type, instead of adding another layer.
+
+    /**
+     * Create a JVM-level adapter method handle to conform the given method
+     * handle to the similar newType, using only pairwise argument conversions.
+     * For each argument, convert incoming argument to the exact type needed.
+     * Only null conversions are allowed on the return value (until
+     * the JVM supports ricochet adapters).
+     * The argument conversions allowed are casting, unboxing,
+     * integral widening or narrowing, and floating point widening or narrowing.
+     * @param token access check
+     * @param newType required call type
+     * @param target original method handle
+     * @return an adapter to the original handle with the desired new type,
+     *          or the original target if the types are already identical
+     * @throws IllegalArgumentException if the adaptation cannot be made
+     *          directly by a JVM-level adapter, without help from Java code
+     */
+    public static MethodHandle makePairwiseConversion(Access token,
+                MethodType newType, MethodHandle target) {
+        Access.check(token);
+        int len = newType.parameterCount();
+        MethodType oldType = target.type();
+        if (newType == oldType)  return target;
+        if (len != oldType.parameterCount())
+            throw newIllegalArgumentException("wrong number of arguments in "+newType);
+
+        // Check return type.  (Not much can be done with it.)
+        Class<?> exp = newType.returnType();
+        Class<?> ret = oldType.returnType();
+        if (!VerifyType.isNullConversion(ret, exp))
+            throw newIllegalArgumentException("bad return conversion for "+newType);
+
+        // Find last non-trivial conversion.
+        int lastConv = len-1;
+        while (lastConv >= 0) {
+            Class<?> src = newType.parameterType(lastConv); // source type
+            Class<?> dst = oldType.parameterType(lastConv); // destination type
+            if (src == dst || VerifyType.isNullConversion(src, dst)) {
+                --lastConv;
+            } else {
+                break;
+            }
+        }
+        // Now build a chain of one or more adapters.
+        MethodHandle adapter = target;
+        MethodType midType = oldType.changeReturnType(exp);
+        for (int i = 0; i <= lastConv; i++) {
+            Class<?> src = newType.parameterType(i); // source type
+            Class<?> dst = midType.parameterType(i); // destination type
+            if (src == dst || VerifyType.isNullConversion(src, dst)) {
+                // do nothing: difference is trivial
+                continue;
+            }
+            // Work the current type backward toward the desired caller type:
+            if (i != lastConv) {
+                midType = midType.changeParameterType(i, src);
+            } else {
+                // When doing the last (or only) real conversion,
+                // force all remaining null conversions to happen also.
+                assert(VerifyType.isNullConversion(newType, midType.changeParameterType(i, src)));
+                midType = newType;
+            }
+            // Tricky case analysis follows.
+            boolean srcPrim = src.isPrimitive();
+            boolean dstPrim = dst.isPrimitive();
+            if (!srcPrim && !dstPrim && canCheckCast(src, dst)) {
+                // Simple reference conversion.
+                // Note:  Do not check for a class hierarchy relation
+                // between src and dst.  In all cases a 'null' argument
+                // will pass the cast conversion.
+                adapter = makeCheckCast(token, midType, adapter, i, dst);
+            } else if (srcPrim && dstPrim && canPrimCast(src, dst)) {
+                // Convert a primitive to a primitive, if the JVM supports it.
+                adapter = makePrimCast(token, midType, adapter, i, dst);
+            } else if (!srcPrim && dstPrim && canUnboxArgument(src, dst)) {
+                // Caller has boxed a primitive.  Unbox it for the target.
+                // The box type must correspond exactly to the primitive type.
+                // This is simpler than the powerful set of widening
+                // conversions supported by reflect.Method.invoke.
+                // Those conversions require a big nest of if/then/else logic,
+                // which we prefer to make a user responsibility.
+                adapter = makeUnboxArgument(token, midType, adapter, i, dst);
+            } else {
+                throw newIllegalArgumentException("bad argument #"+i+" conversion in "+newType);
+            }
+            assert(adapter.type() == midType);
+        }
+        if (adapter.type() != newType) {
+            // Only trivial conversions remain.
+            adapter = makeRetypeOnly(IMPL_TOKEN, newType, adapter);
+            // Actually, that's because there were no non-trivial ones:
+            assert(lastConv == -1);
+        }
+        assert(adapter.type() == newType);
+        return adapter;
+    }
+
+    /**
+     * Create a JVM-level adapter method handle to permute the arguments
+     * of the given method.
+     * @param token access check
+     * @param newType required call type
+     * @param target original method handle
+     * @param argumentMap for each target argument, position of its source in newType
+     * @return an adapter to the original handle with the desired new type,
+     *          or the original target if the types are already identical
+     *          and the permutation is null
+     * @throws IllegalArgumentException if the adaptation cannot be made
+     *          directly by a JVM-level adapter, without help from Java code
+     */
+    public static MethodHandle makePermutation(Access token,
+                MethodType newType, MethodHandle target,
+                int[] argumentMap) {
+        MethodType oldType = target.type();
+        boolean nullPermutation = true;
+        for (int i = 0; i < argumentMap.length; i++) {
+            int pos = argumentMap[i];
+            if (pos != i)
+                nullPermutation = false;
+            if (pos < 0 || pos >= newType.parameterCount()) {
+                argumentMap = new int[0]; break;
+            }
+        }
+        if (argumentMap.length != oldType.parameterCount())
+            throw newIllegalArgumentException("bad permutation: "+Arrays.toString(argumentMap));
+        if (nullPermutation)
+            return makePairwiseConversion(token, newType, target);   // well, that was easy
+
+        // Check return type.  (Not much can be done with it.)
+        Class<?> exp = newType.returnType();
+        Class<?> ret = oldType.returnType();
+        if (!VerifyType.isNullConversion(ret, exp))
+            throw newIllegalArgumentException("bad return conversion for "+newType);
+
+        // See if the argument types match up.
+        for (int i = 0; i < argumentMap.length; i++) {
+            int j = argumentMap[i];
+            Class<?> src = newType.parameterType(j);
+            Class<?> dst = oldType.parameterType(i);
+            if (!VerifyType.isNullConversion(src, dst))
+                throw newIllegalArgumentException("bad argument #"+j+" conversion for "+newType);
+        }
+
+        // Now figure out a nice mix of SWAP, ROT, DUP, and DROP adapters.
+        // A workable greedy algorithm is as follows:
+        // Drop unused outgoing arguments (right to left: shallowest first).
+        // Duplicate doubly-used outgoing arguments (left to right: deepest first).
+        // Then the remaining problem is a true argument permutation.
+        // Marshal the outgoing arguments as required from left to right.
+        // That is, find the deepest outgoing stack position that does not yet
+        // have the correct argument value, and correct at least that position
+        // by swapping or rotating in the misplaced value (from a shallower place).
+        // If the misplaced value is followed by one or more consecutive values
+        // (also misplaced)  issue a rotation which brings as many as possible
+        // into position.  Otherwise make progress with either a swap or a
+        // rotation.  Prefer the swap as cheaper, but do not use it if it
+        // breaks a slot pair.  Prefer the rotation over the swap if it would
+        // preserve more consecutive values shallower than the target position.
+        // When more than one rotation will work (because the required value
+        // is already adjacent to the target position), then use a rotation
+        // which moves the old value in the target position adjacent to
+        // one of its consecutive values.  Also, prefer shorter rotation
+        // spans, since they use fewer memory cycles for shuffling.
+
+        throw new UnsupportedOperationException("NYI");
+    }
+
+    private static byte basicType(Class<?> type) {
+        if (type == null)  return T_VOID;
+        char c = Wrappers.basicTypeChar(type);
+        switch (c) {
+            case 'Z': return T_BOOLEAN;
+            case 'C': return T_CHAR;
+            case 'F': return T_FLOAT;
+            case 'D': return T_DOUBLE;
+            case 'B': return T_BYTE;
+            case 'S': return T_SHORT;
+            case 'I': return T_INT;
+            case 'J': return T_LONG;
+            case 'L': return T_OBJECT;
+        }
+        return 99; // T_ILLEGAL or some such
+    }
+
+    /** Number of stack slots for the given type.
+     *  Two for T_DOUBLE and T_FLOAT, one for the rest.
+     */
+    private static int type2size(int type) {
+        assert(type >= T_BOOLEAN && type <= T_OBJECT);
+        return (type == T_FLOAT || type == T_DOUBLE) ? 2 : 1;
+    }
+
+    /** Construct an adapter conversion descriptor for a single-argument conversion. */
+    private static long makeConv(int convOp, int argnum, int src, int dest) {
+        assert(src  == (src  & 0xF));
+        assert(dest == (dest & 0xF));
+        assert(convOp == (convOp & CONV_OP_MASK));
+        assert(convOp >= CHECK_CAST && convOp <= PRIM_TO_REF);
+        long stackMove = type2size(dest) - type2size(src);
+        return ((long) argnum << 32 |
+                (long) convOp |
+                (int)  src    << CONV_SRC_TYPE_SHIFT |
+                (int)  dest   << CONV_DEST_TYPE_SHIFT |
+                stackMove     << CONV_STACK_MOVE_SHIFT
+                );
+    }
+    private static long makeConv(int convOp, int argnum, int stackMove) {
+        assert(convOp == (convOp & CONV_OP_MASK));
+        assert(convOp >= SWAP_ARGS && convOp <= SPREAD_ARGS);
+        byte src = 0, dest = 0;
+        if (convOp >= COLLECT_ARGS && convOp <= SPREAD_ARGS)
+            src = dest = T_OBJECT;
+        return ((long) argnum << 32 |
+                (long) convOp |
+                (int)  src    << CONV_SRC_TYPE_SHIFT |
+                (int)  dest   << CONV_DEST_TYPE_SHIFT |
+                stackMove     << CONV_STACK_MOVE_SHIFT
+                );
+    }
+    private static long makeConv(int convOp) {
+        assert(convOp == (convOp & CONV_OP_MASK));
+        assert(convOp == RETYPE_ONLY);
+        return (long) convOp;   // stackMove, src, dst, argnum all zero
+    }
+    private static int convCode(long conv) {
+        return (int)conv;
+    }
+    private static int convArgPos(long conv) {
+        return (int)(conv >>> 32);
+    }
+
+    @Override
+    public String toString() {
+        MethodType adaptedType = ((MethodHandle)this).type();
+        Object namh = nonAdapter((MethodHandle)vmtarget);
+        if (namh == null)  namh = "unknown";
+        return "Adapted[" + adaptedType + "," + namh + "]";
+    }
+
+    private static MethodHandle nonAdapter(MethodHandle mh) {
+        return (MethodHandle)
+            MethodHandleNatives.getTarget(mh, ETF_DIRECT_HANDLE);
+    }
+
+    /* Return one plus the position of the first non-trivial difference
+     * between the given types.  This is not a symmetric operation;
+     * we are considering adapting the targetType to adapterType.
+     * Trivial differences are those which could be ignored by the JVM
+     * without subverting the verifier.  Otherwise, adaptable differences
+     * are ones for which we could create an adapter to make the type change.
+     * Return zero if there are no differences (other than trivial ones).
+     * Return 1+N if N is the only adaptable argument difference.
+     * Return the -2-N where N is the first of several adaptable
+     * argument differences.
+     * Return -1 if there there are differences which are not adaptable.
+     */
+    private static int diffTypes(MethodType adapterType,
+                                 MethodType targetType) {
+        Class<?> src = targetType.returnType();
+        Class<?> dst = adapterType.returnType();
+        if (VerifyType.canPassUnchecked(src, dst) <= 0)
+            return -1;
+        int nargs = adapterType.parameterCount();
+        if (nargs != targetType.parameterCount())
+            return -1;
+        int diff = diffTypes(adapterType, 0, targetType, 0, nargs);
+        System.out.println("diff "+adapterType);
+        System.out.println("  "+diff+" "+targetType);
+        return diff;
+    }
+    private static int diffTypes(MethodType adapterType, int tstart,
+                                 MethodType targetType, int astart,
+                                 int nargs) {
+        int res = 0;
+        for (int i = 0; i < nargs; i++) {
+            Class<?> src  = adapterType.parameterType(tstart+i);
+            Class<?> dest = targetType.parameterType(astart+i);
+            if (VerifyType.canPassUnchecked(src, dest) <= 0) {
+                // found a difference; is it the only one so far?
+                if (res != 0)
+                    return -1-res; // return -2-i for prev. i
+                res = 1+i;
+            }
+        }
+        return res;
+    }
+
+    /** Can a retyping adapter (alone) validly convert the target to newType? */
+    public static boolean canRetypeOnly(MethodType newType, MethodType targetType) {
+        int diff = diffTypes(newType, targetType);
+        // %%% This assert is too strong.  Factor diff into VerifyType and reconcile.
+        assert((diff == 0) == VerifyType.isNullConversion(newType, targetType));
+        return diff == 0;
+    }
+
+    /** Factory method:  Performs no conversions; simply retypes the adapter. */
+    public static MethodHandle makeRetypeOnly(Access token,
+                MethodType newType, MethodHandle target) {
+        Access.check(token);
+        assert(canRetypeOnly(newType, target.type()));
+        // %%% TO DO: If adapter is already an adapter, fold in the retyping.
+        return new AdapterMethodHandle(target, newType, makeConv(RETYPE_ONLY));
+    }
+
+    /** Can a checkcast adapter validly convert the target to newType?
+     *  The JVM supports all kind of reference casts, even silly ones.
+     */
+    public static boolean canCheckCast(MethodType newType, MethodType targetType,
+                int arg, Class<?> castType) {
+        Class<?> src = newType.parameterType(arg);
+        Class<?> dst = targetType.parameterType(arg);
+        if (!canCheckCast(src, castType)
+                || !VerifyType.isNullConversion(castType, dst))
+            return false;
+        int diff = diffTypes(newType, targetType);
+        return (diff == arg+1);  // arg is sole non-trivial diff
+    }
+    /** Can an primitive conversion adapter validly convert src to dst? */
+    public static boolean canCheckCast(Class<?> src, Class<?> dst) {
+        return (!src.isPrimitive() && !dst.isPrimitive());
+    }
+
+    /** Factory method:  Forces a cast at the given argument.
+     *  The castType is the target of the cast, and can be any type
+     *  with a null conversion to the corresponding target parameter.
+     */
+    public static MethodHandle makeCheckCast(Access token,
+                MethodType newType, MethodHandle target,
+                int arg, Class<?> castType) {
+        Access.check(token);
+        assert(canCheckCast(newType, target.type(), arg, castType));
+        long conv = makeConv(CHECK_CAST, arg, 0);
+        return new AdapterMethodHandle(target, newType, conv, castType);
+    }
+
+    /** Can an primitive conversion adapter validly convert the target to newType?
+     *  The JVM currently supports all conversions except those between
+     *  floating and integral types.
+     */
+    public static boolean canPrimCast(MethodType newType, MethodType targetType,
+                int arg, Class<?> convType) {
+        Class<?> src = newType.parameterType(arg);
+        Class<?> dst = targetType.parameterType(arg);
+        if (!canPrimCast(src, convType)
+                || !VerifyType.isNullConversion(convType, dst))
+            return false;
+        int diff = diffTypes(newType, targetType);
+        return (diff == arg+1);  // arg is sole non-trivial diff
+    }
+    /** Can an primitive conversion adapter validly convert src to dst? */
+    public static boolean canPrimCast(Class<?> src, Class<?> dst) {
+        if (src == dst || !src.isPrimitive() || !dst.isPrimitive()) {
+            return false;
+        } else if (Wrappers.isFloating(dst)) {
+            // both must be floating types
+            return Wrappers.isFloating(src);
+        } else {
+            // both are integral, and all combinations work fine
+            assert(Wrappers.isIntegral(src) && Wrappers.isIntegral(dst));
+            return true;
+        }
+    }
+
+    /** Factory method:  Truncate the given argument with zero or sign extension,
+     *  and/or convert between single and doubleword versions of integer or float.
+     *  The convType is the target of the conversion, and can be any type
+     *  with a null conversion to the corresponding target parameter.
+     */
+    public static MethodHandle makePrimCast(Access token,
+                MethodType newType, MethodHandle target,
+                int arg, Class<?> convType) {
+        Access.check(token);
+        MethodType oldType = target.type();
+        Class<?> src = newType.parameterType(arg);
+        Class<?> dst = oldType.parameterType(arg);
+        assert(canPrimCast(newType, oldType, arg, convType));
+        long conv = makeConv(PRIM_TO_PRIM, arg, basicType(src), basicType(convType));
+        return new AdapterMethodHandle(target, newType, conv);
+    }
+
+    /** Can an unboxing conversion validly convert src to dst?
+     *  The JVM currently supports all kinds of casting and unboxing.
+     *  The convType is the unboxed type; it can be either a primitive or wrapper.
+     */
+    public static boolean canUnboxArgument(MethodType newType, MethodType targetType,
+                int arg, Class<?> convType) {
+        Class<?> src = newType.parameterType(arg);
+        Class<?> dst = targetType.parameterType(arg);
+        Class<?> boxType = Wrappers.asWrapperType(convType);
+        convType = Wrappers.asPrimitiveType(convType);
+        if (!canCheckCast(src, boxType)
+                || boxType == convType
+                || !VerifyType.isNullConversion(convType, dst))
+            return false;
+        int diff = diffTypes(newType, targetType);
+        return (diff == arg+1);  // arg is sole non-trivial diff
+    }
+    /** Can an primitive unboxing adapter validly convert src to dst? */
+    public static boolean canUnboxArgument(Class<?> src, Class<?> dst) {
+        return (!src.isPrimitive() && Wrappers.asPrimitiveType(dst).isPrimitive());
+    }
+
+    /** Factory method:  Unbox the given argument. */
+    public static MethodHandle makeUnboxArgument(Access token,
+                MethodType newType, MethodHandle target,
+                int arg, Class<?> convType) {
+        MethodType oldType = target.type();
+        Class<?> src = newType.parameterType(arg);
+        Class<?> dst = oldType.parameterType(arg);
+        Class<?> boxType = Wrappers.asWrapperType(convType);
+        Class<?> primType = Wrappers.asPrimitiveType(convType);
+        assert(canUnboxArgument(newType, oldType, arg, convType));
+        MethodType castDone = newType;
+        if (!VerifyType.isNullConversion(src, boxType))
+            castDone = newType.changeParameterType(arg, boxType);
+        long conv = makeConv(REF_TO_PRIM, arg, T_OBJECT, basicType(primType));
+        MethodHandle adapter = new AdapterMethodHandle(target, castDone, conv, boxType);
+        if (castDone == newType)
+            return adapter;
+        return makeCheckCast(token, newType, adapter, arg, boxType);
+    }
+
+    // TO DO: makeBoxArgument, makeSwapArguments, makeRotateArguments, makeDuplicateArguments
+
+    /** Can an adapter simply drop arguments to convert the target to newType? */
+    public static boolean canDropArguments(MethodType newType, MethodType targetType,
+                int dropArgPos, int dropArgCount) {
+        List<Class<?>> ptypes = targetType.parameterList();
+        int nptypes = ptypes.size();
+        if ((dropArgPos | dropArgCount) < 0)
+            return false;
+        if (dropArgPos == 0)
+            ptypes = ptypes.subList(dropArgCount, nptypes);
+        else if (dropArgPos + dropArgCount == nptypes)
+            ptypes = ptypes.subList(0, nptypes - dropArgCount);
+        else {
+            if (dropArgPos                > nptypes ||
+                dropArgPos + dropArgCount > nptypes)
+                return false;
+            ptypes = new ArrayList<Class<?>>(ptypes);
+            ptypes.subList(dropArgPos, dropArgPos + dropArgCount).clear();
+        }
+        MethodType midType = MethodType.make(targetType.returnType(), ptypes);
+        return diffTypes(newType, midType) == 0;
+    }
+
+    /** Factory method:  Drop selected initial or final arguments. */
+    public static MethodHandle makeDropArguments(Access token,
+                MethodType newType, MethodHandle target,
+                int dropArgPos, int dropArgCount) {
+        Access.check(token);
+        assert(canDropArguments(newType, target.type(), dropArgPos, dropArgCount));
+        MethodType mt = target.type();
+        int argCount  = mt.parameterCount();
+        int dropSlotCount, dropSlotPos;
+        if (dropArgCount <= 0) {
+            return makeRetypeOnly(IMPL_TOKEN, newType, target);
+        } else if (dropArgCount >= argCount) {
+            assert(dropArgPos == argCount-1);
+            dropSlotPos = 0;
+            dropSlotCount = mt.parameterSlotCount();
+        } else {
+            // arglist: [0: keep... | dpos: drop... | dpos+dcount: keep... ]
+            int lastDroppedArg = dropArgPos + dropArgCount - 1;
+            int lastKeptArg    = dropArgPos - 1;  // might be -1, which is OK
+            dropSlotPos      = mt.parameterSlot(lastDroppedArg);
+            int lastKeptSlot = mt.parameterSlot(lastKeptArg);
+            dropSlotCount = lastKeptSlot - dropSlotPos;
+            assert(dropSlotCount >= dropArgCount);
+        }
+        long conv = makeConv(DROP_ARGS, dropArgPos, +dropSlotCount);
+        return new AdapterMethodHandle(target, newType, dropSlotCount, conv);
+    }
+
+    // TO DO: makeCollectArguments, makeSpreadArguments, makeFlyby, makeRicochet
+}
