1 /*
2 * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package impl.java.dyn;
27
28 import impl.java.dyn.util.VerifyType;
29 import impl.java.dyn.util.Wrappers;
30 import java.dyn.*;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import static impl.java.dyn.MethodHandleNatives.Constants.*;
35 import static impl.java.dyn.MethodHandleImpl.newIllegalArgumentException;
36
37 /**
38 * This method handle performs simple conversion or checking of a single argument.
39 * @author jrose
40 */
41 public class AdapterMethodHandle extends BoundMethodHandle {
42 //MethodHandle vmtarget; // next AMH or BMH in chain or final DMH
43 //Object argument; // parameter to the conversion if needed
44 //int vmargslot; // which argument slot is affected
45 private final int conversion; // the type of conversion: RETYPE_ONLY, etc.
46
47 // Constructors in this class *must* be package scoped or private.
48 private AdapterMethodHandle(MethodHandle target, MethodType newType,
49 long conv, Object convArg) {
50 super(newType, convArg, newType.parameterSlot(convArgPos(conv)));
51 this.conversion = convCode(conv);
52 if (MethodHandleNatives.JVM_SUPPORT) {
53 // JVM might update VM-specific bits of conversion (ignore)
54 MethodHandleNatives.init(this, target, convArgPos(conv));
55 }
56 }
57 private AdapterMethodHandle(MethodHandle target, MethodType newType,
58 long conv) {
59 this(target, newType, conv, null);
60 }
61
62 private static final Access IMPL_TOKEN = Access.getToken();
63
64 // TO DO: When adapting another MH with a null conversion, clone
65 // the target and change its type, instead of adding another layer.
66
67 /**
68 * Create a JVM-level adapter method handle to conform the given method
69 * handle to the similar newType, using only pairwise argument conversions.
70 * For each argument, convert incoming argument to the exact type needed.
71 * Only null conversions are allowed on the return value (until
72 * the JVM supports ricochet adapters).
73 * The argument conversions allowed are casting, unboxing,
74 * integral widening or narrowing, and floating point widening or narrowing.
75 * @param token access check
76 * @param newType required call type
77 * @param target original method handle
78 * @return an adapter to the original handle with the desired new type,
79 * or the original target if the types are already identical
80 * @throws IllegalArgumentException if the adaptation cannot be made
81 * directly by a JVM-level adapter, without help from Java code
82 */
83 public static MethodHandle makePairwiseConversion(Access token,
84 MethodType newType, MethodHandle target) {
85 Access.check(token);
86 int len = newType.parameterCount();
87 MethodType oldType = target.type();
88 if (newType == oldType) return target;
89 if (len != oldType.parameterCount())
90 throw newIllegalArgumentException("wrong number of arguments in "+newType);
91
92 // Check return type. (Not much can be done with it.)
93 Class<?> exp = newType.returnType();
94 Class<?> ret = oldType.returnType();
95 if (!VerifyType.isNullConversion(ret, exp))
96 throw newIllegalArgumentException("bad return conversion for "+newType);
97
98 // Find last non-trivial conversion.
99 int lastConv = len-1;
100 while (lastConv >= 0) {
101 Class<?> src = newType.parameterType(lastConv); // source type
102 Class<?> dst = oldType.parameterType(lastConv); // destination type
103 if (src == dst || VerifyType.isNullConversion(src, dst)) {
104 --lastConv;
105 } else {
106 break;
107 }
108 }
109 // Now build a chain of one or more adapters.
110 MethodHandle adapter = target;
111 MethodType midType = oldType.changeReturnType(exp);
112 for (int i = 0; i <= lastConv; i++) {
113 Class<?> src = newType.parameterType(i); // source type
114 Class<?> dst = midType.parameterType(i); // destination type
115 if (src == dst || VerifyType.isNullConversion(src, dst)) {
116 // do nothing: difference is trivial
117 continue;
118 }
119 // Work the current type backward toward the desired caller type:
120 if (i != lastConv) {
121 midType = midType.changeParameterType(i, src);
122 } else {
123 // When doing the last (or only) real conversion,
124 // force all remaining null conversions to happen also.
125 assert(VerifyType.isNullConversion(newType, midType.changeParameterType(i, src)));
126 midType = newType;
127 }
128 // Tricky case analysis follows.
129 boolean srcPrim = src.isPrimitive();
130 boolean dstPrim = dst.isPrimitive();
131 if (!srcPrim && !dstPrim && canCheckCast(src, dst)) {
132 // Simple reference conversion.
133 // Note: Do not check for a class hierarchy relation
134 // between src and dst. In all cases a 'null' argument
135 // will pass the cast conversion.
136 adapter = makeCheckCast(token, midType, adapter, i, dst);
137 } else if (srcPrim && dstPrim && canPrimCast(src, dst)) {
138 // Convert a primitive to a primitive, if the JVM supports it.
139 adapter = makePrimCast(token, midType, adapter, i, dst);
140 } else if (!srcPrim && dstPrim && canUnboxArgument(src, dst)) {
141 // Caller has boxed a primitive. Unbox it for the target.
142 // The box type must correspond exactly to the primitive type.
143 // This is simpler than the powerful set of widening
144 // conversions supported by reflect.Method.invoke.
145 // Those conversions require a big nest of if/then/else logic,
146 // which we prefer to make a user responsibility.
147 adapter = makeUnboxArgument(token, midType, adapter, i, dst);
148 } else {
149 throw newIllegalArgumentException("bad argument #"+i+" conversion in "+newType);
150 }
151 assert(adapter.type() == midType);
152 }
153 if (adapter.type() != newType) {
154 // Only trivial conversions remain.
155 adapter = makeRetypeOnly(IMPL_TOKEN, newType, adapter);
156 // Actually, that's because there were no non-trivial ones:
157 assert(lastConv == -1);
158 }
159 assert(adapter.type() == newType);
160 return adapter;
161 }
162
163 /**
164 * Create a JVM-level adapter method handle to permute the arguments
165 * of the given method.
166 * @param token access check
167 * @param newType required call type
168 * @param target original method handle
169 * @param argumentMap for each target argument, position of its source in newType
170 * @return an adapter to the original handle with the desired new type,
171 * or the original target if the types are already identical
172 * and the permutation is null
173 * @throws IllegalArgumentException if the adaptation cannot be made
174 * directly by a JVM-level adapter, without help from Java code
175 */
176 public static MethodHandle makePermutation(Access token,
177 MethodType newType, MethodHandle target,
178 int[] argumentMap) {
179 MethodType oldType = target.type();
180 boolean nullPermutation = true;
181 for (int i = 0; i < argumentMap.length; i++) {
182 int pos = argumentMap[i];
183 if (pos != i)
184 nullPermutation = false;
185 if (pos < 0 || pos >= newType.parameterCount()) {
186 argumentMap = new int[0]; break;
187 }
188 }
189 if (argumentMap.length != oldType.parameterCount())
190 throw newIllegalArgumentException("bad permutation: "+Arrays.toString(argumentMap));
191 if (nullPermutation)
192 return makePairwiseConversion(token, newType, target); // well, that was easy
193
194 // Check return type. (Not much can be done with it.)
195 Class<?> exp = newType.returnType();
196 Class<?> ret = oldType.returnType();
197 if (!VerifyType.isNullConversion(ret, exp))
198 throw newIllegalArgumentException("bad return conversion for "+newType);
199
200 // See if the argument types match up.
201 for (int i = 0; i < argumentMap.length; i++) {
202 int j = argumentMap[i];
203 Class<?> src = newType.parameterType(j);
204 Class<?> dst = oldType.parameterType(i);
205 if (!VerifyType.isNullConversion(src, dst))
206 throw newIllegalArgumentException("bad argument #"+j+" conversion for "+newType);
207 }
208
209 // Now figure out a nice mix of SWAP, ROT, DUP, and DROP adapters.
210 // A workable greedy algorithm is as follows:
211 // Drop unused outgoing arguments (right to left: shallowest first).
212 // Duplicate doubly-used outgoing arguments (left to right: deepest first).
213 // Then the remaining problem is a true argument permutation.
214 // Marshal the outgoing arguments as required from left to right.
215 // That is, find the deepest outgoing stack position that does not yet
216 // have the correct argument value, and correct at least that position
217 // by swapping or rotating in the misplaced value (from a shallower place).
218 // If the misplaced value is followed by one or more consecutive values
219 // (also misplaced) issue a rotation which brings as many as possible
220 // into position. Otherwise make progress with either a swap or a
221 // rotation. Prefer the swap as cheaper, but do not use it if it
222 // breaks a slot pair. Prefer the rotation over the swap if it would
223 // preserve more consecutive values shallower than the target position.
224 // When more than one rotation will work (because the required value
225 // is already adjacent to the target position), then use a rotation
226 // which moves the old value in the target position adjacent to
227 // one of its consecutive values. Also, prefer shorter rotation
228 // spans, since they use fewer memory cycles for shuffling.
229
230 throw new UnsupportedOperationException("NYI");
231 }
232
233 private static byte basicType(Class<?> type) {
234 if (type == null) return T_VOID;
235 char c = Wrappers.basicTypeChar(type);
236 switch (c) {
237 case 'Z': return T_BOOLEAN;
238 case 'C': return T_CHAR;
239 case 'F': return T_FLOAT;
240 case 'D': return T_DOUBLE;
241 case 'B': return T_BYTE;
242 case 'S': return T_SHORT;
243 case 'I': return T_INT;
244 case 'J': return T_LONG;
245 case 'L': return T_OBJECT;
246 }
247 return 99; // T_ILLEGAL or some such
248 }
249
250 /** Number of stack slots for the given type.
251 * Two for T_DOUBLE and T_FLOAT, one for the rest.
252 */
253 private static int type2size(int type) {
254 assert(type >= T_BOOLEAN && type <= T_OBJECT);
255 return (type == T_FLOAT || type == T_DOUBLE) ? 2 : 1;
256 }
257
258 /** Construct an adapter conversion descriptor for a single-argument conversion. */
259 private static long makeConv(int convOp, int argnum, int src, int dest) {
260 assert(src == (src & 0xF));
261 assert(dest == (dest & 0xF));
262 assert(convOp == (convOp & CONV_OP_MASK));
263 assert(convOp >= CHECK_CAST && convOp <= PRIM_TO_REF);
264 long stackMove = type2size(dest) - type2size(src);
265 return ((long) argnum << 32 |
266 (long) convOp |
267 (int) src << CONV_SRC_TYPE_SHIFT |
268 (int) dest << CONV_DEST_TYPE_SHIFT |
269 stackMove << CONV_STACK_MOVE_SHIFT
270 );
271 }
272 private static long makeConv(int convOp, int argnum, int stackMove) {
273 assert(convOp == (convOp & CONV_OP_MASK));
274 assert(convOp >= SWAP_ARGS && convOp <= SPREAD_ARGS);
275 byte src = 0, dest = 0;
276 if (convOp >= COLLECT_ARGS && convOp <= SPREAD_ARGS)
277 src = dest = T_OBJECT;
278 return ((long) argnum << 32 |
279 (long) convOp |
280 (int) src << CONV_SRC_TYPE_SHIFT |
281 (int) dest << CONV_DEST_TYPE_SHIFT |
282 stackMove << CONV_STACK_MOVE_SHIFT
283 );
284 }
285 private static long makeConv(int convOp) {
286 assert(convOp == (convOp & CONV_OP_MASK));
287 assert(convOp == RETYPE_ONLY);
288 return (long) convOp; // stackMove, src, dst, argnum all zero
289 }
290 private static int convCode(long conv) {
291 return (int)conv;
292 }
293 private static int convArgPos(long conv) {
294 return (int)(conv >>> 32);
295 }
296
297 @Override
298 public String toString() {
299 MethodType adaptedType = ((MethodHandle)this).type();
300 Object namh = nonAdapter((MethodHandle)vmtarget);
301 if (namh == null) namh = "unknown";
302 return "Adapted[" + adaptedType + "," + namh + "]";
303 }
304
305 private static MethodHandle nonAdapter(MethodHandle mh) {
306 return (MethodHandle)
307 MethodHandleNatives.getTarget(mh, ETF_DIRECT_HANDLE);
308 }
309
310 /* Return one plus the position of the first non-trivial difference
311 * between the given types. This is not a symmetric operation;
312 * we are considering adapting the targetType to adapterType.
313 * Trivial differences are those which could be ignored by the JVM
314 * without subverting the verifier. Otherwise, adaptable differences
315 * are ones for which we could create an adapter to make the type change.
316 * Return zero if there are no differences (other than trivial ones).
317 * Return 1+N if N is the only adaptable argument difference.
318 * Return the -2-N where N is the first of several adaptable
319 * argument differences.
320 * Return -1 if there there are differences which are not adaptable.
321 */
322 private static int diffTypes(MethodType adapterType,
323 MethodType targetType) {
324 Class<?> src = targetType.returnType();
325 Class<?> dst = adapterType.returnType();
326 if (VerifyType.canPassUnchecked(src, dst) <= 0)
327 return -1;
328 int nargs = adapterType.parameterCount();
329 if (nargs != targetType.parameterCount())
330 return -1;
331 int diff = diffTypes(adapterType, 0, targetType, 0, nargs);
332 System.out.println("diff "+adapterType);
333 System.out.println(" "+diff+" "+targetType);
334 return diff;
335 }
336 private static int diffTypes(MethodType adapterType, int tstart,
337 MethodType targetType, int astart,
338 int nargs) {
339 int res = 0;
340 for (int i = 0; i < nargs; i++) {
341 Class<?> src = adapterType.parameterType(tstart+i);
342 Class<?> dest = targetType.parameterType(astart+i);
343 if (VerifyType.canPassUnchecked(src, dest) <= 0) {
344 // found a difference; is it the only one so far?
345 if (res != 0)
346 return -1-res; // return -2-i for prev. i
347 res = 1+i;
348 }
349 }
350 return res;
351 }
352
353 /** Can a retyping adapter (alone) validly convert the target to newType? */
354 public static boolean canRetypeOnly(MethodType newType, MethodType targetType) {
355 int diff = diffTypes(newType, targetType);
356 // %%% This assert is too strong. Factor diff into VerifyType and reconcile.
357 assert((diff == 0) == VerifyType.isNullConversion(newType, targetType));
358 return diff == 0;
359 }
360
361 /** Factory method: Performs no conversions; simply retypes the adapter. */
362 public static MethodHandle makeRetypeOnly(Access token,
363 MethodType newType, MethodHandle target) {
364 Access.check(token);
365 assert(canRetypeOnly(newType, target.type()));
366 // %%% TO DO: If adapter is already an adapter, fold in the retyping.
367 return new AdapterMethodHandle(target, newType, makeConv(RETYPE_ONLY));
368 }
369
370 /** Can a checkcast adapter validly convert the target to newType?
371 * The JVM supports all kind of reference casts, even silly ones.
372 */
373 public static boolean canCheckCast(MethodType newType, MethodType targetType,
374 int arg, Class<?> castType) {
375 Class<?> src = newType.parameterType(arg);
376 Class<?> dst = targetType.parameterType(arg);
377 if (!canCheckCast(src, castType)
378 || !VerifyType.isNullConversion(castType, dst))
379 return false;
380 int diff = diffTypes(newType, targetType);
381 return (diff == arg+1); // arg is sole non-trivial diff
382 }
383 /** Can an primitive conversion adapter validly convert src to dst? */
384 public static boolean canCheckCast(Class<?> src, Class<?> dst) {
385 return (!src.isPrimitive() && !dst.isPrimitive());
386 }
387
388 /** Factory method: Forces a cast at the given argument.
389 * The castType is the target of the cast, and can be any type
390 * with a null conversion to the corresponding target parameter.
391 */
392 public static MethodHandle makeCheckCast(Access token,
393 MethodType newType, MethodHandle target,
394 int arg, Class<?> castType) {
395 Access.check(token);
396 assert(canCheckCast(newType, target.type(), arg, castType));
397 long conv = makeConv(CHECK_CAST, arg, 0);
398 return new AdapterMethodHandle(target, newType, conv, castType);
399 }
400
401 /** Can an primitive conversion adapter validly convert the target to newType?
402 * The JVM currently supports all conversions except those between
403 * floating and integral types.
404 */
405 public static boolean canPrimCast(MethodType newType, MethodType targetType,
406 int arg, Class<?> convType) {
407 Class<?> src = newType.parameterType(arg);
408 Class<?> dst = targetType.parameterType(arg);
409 if (!canPrimCast(src, convType)
410 || !VerifyType.isNullConversion(convType, dst))
411 return false;
412 int diff = diffTypes(newType, targetType);
413 return (diff == arg+1); // arg is sole non-trivial diff
414 }
415 /** Can an primitive conversion adapter validly convert src to dst? */
416 public static boolean canPrimCast(Class<?> src, Class<?> dst) {
417 if (src == dst || !src.isPrimitive() || !dst.isPrimitive()) {
418 return false;
419 } else if (Wrappers.isFloating(dst)) {
420 // both must be floating types
421 return Wrappers.isFloating(src);
422 } else {
423 // both are integral, and all combinations work fine
424 assert(Wrappers.isIntegral(src) && Wrappers.isIntegral(dst));
425 return true;
426 }
427 }
428
429 /** Factory method: Truncate the given argument with zero or sign extension,
430 * and/or convert between single and doubleword versions of integer or float.
431 * The convType is the target of the conversion, and can be any type
432 * with a null conversion to the corresponding target parameter.
433 */
434 public static MethodHandle makePrimCast(Access token,
435 MethodType newType, MethodHandle target,
436 int arg, Class<?> convType) {
437 Access.check(token);
438 MethodType oldType = target.type();
439 Class<?> src = newType.parameterType(arg);
440 Class<?> dst = oldType.parameterType(arg);
441 assert(canPrimCast(newType, oldType, arg, convType));
442 long conv = makeConv(PRIM_TO_PRIM, arg, basicType(src), basicType(convType));
443 return new AdapterMethodHandle(target, newType, conv);
444 }
445
446 /** Can an unboxing conversion validly convert src to dst?
447 * The JVM currently supports all kinds of casting and unboxing.
448 * The convType is the unboxed type; it can be either a primitive or wrapper.
449 */
450 public static boolean canUnboxArgument(MethodType newType, MethodType targetType,
451 int arg, Class<?> convType) {
452 Class<?> src = newType.parameterType(arg);
453 Class<?> dst = targetType.parameterType(arg);
454 Class<?> boxType = Wrappers.asWrapperType(convType);
455 convType = Wrappers.asPrimitiveType(convType);
456 if (!canCheckCast(src, boxType)
457 || boxType == convType
458 || !VerifyType.isNullConversion(convType, dst))
459 return false;
460 int diff = diffTypes(newType, targetType);
461 return (diff == arg+1); // arg is sole non-trivial diff
462 }
463 /** Can an primitive unboxing adapter validly convert src to dst? */
464 public static boolean canUnboxArgument(Class<?> src, Class<?> dst) {
465 return (!src.isPrimitive() && Wrappers.asPrimitiveType(dst).isPrimitive());
466 }
467
468 /** Factory method: Unbox the given argument. */
469 public static MethodHandle makeUnboxArgument(Access token,
470 MethodType newType, MethodHandle target,
471 int arg, Class<?> convType) {
472 MethodType oldType = target.type();
473 Class<?> src = newType.parameterType(arg);
474 Class<?> dst = oldType.parameterType(arg);
475 Class<?> boxType = Wrappers.asWrapperType(convType);
476 Class<?> primType = Wrappers.asPrimitiveType(convType);
477 assert(canUnboxArgument(newType, oldType, arg, convType));
478 MethodType castDone = newType;
479 if (!VerifyType.isNullConversion(src, boxType))
480 castDone = newType.changeParameterType(arg, boxType);
481 long conv = makeConv(REF_TO_PRIM, arg, T_OBJECT, basicType(primType));
482 MethodHandle adapter = new AdapterMethodHandle(target, castDone, conv, boxType);
483 if (castDone == newType)
484 return adapter;
485 return makeCheckCast(token, newType, adapter, arg, boxType);
486 }
487
488 // TO DO: makeBoxArgument, makeSwapArguments, makeRotateArguments, makeDuplicateArguments
489
490 /** Can an adapter simply drop arguments to convert the target to newType? */
491 public static boolean canDropArguments(MethodType newType, MethodType targetType,
492 int dropArgPos, int dropArgCount) {
493 List<Class<?>> ptypes = targetType.parameterList();
494 int nptypes = ptypes.size();
495 if ((dropArgPos | dropArgCount) < 0)
496 return false;
497 if (dropArgPos == 0)
498 ptypes = ptypes.subList(dropArgCount, nptypes);
499 else if (dropArgPos + dropArgCount == nptypes)
500 ptypes = ptypes.subList(0, nptypes - dropArgCount);
501 else {
502 if (dropArgPos > nptypes ||
503 dropArgPos + dropArgCount > nptypes)
504 return false;
505 ptypes = new ArrayList<Class<?>>(ptypes);
506 ptypes.subList(dropArgPos, dropArgPos + dropArgCount).clear();
507 }
508 MethodType midType = MethodType.make(targetType.returnType(), ptypes);
509 return diffTypes(newType, midType) == 0;
510 }
511
512 /** Factory method: Drop selected initial or final arguments. */
513 public static MethodHandle makeDropArguments(Access token,
514 MethodType newType, MethodHandle target,
515 int dropArgPos, int dropArgCount) {
516 Access.check(token);
517 assert(canDropArguments(newType, target.type(), dropArgPos, dropArgCount));
518 MethodType mt = target.type();
519 int argCount = mt.parameterCount();
520 int dropSlotCount, dropSlotPos;
521 if (dropArgCount <= 0) {
522 return makeRetypeOnly(IMPL_TOKEN, newType, target);
523 } else if (dropArgCount >= argCount) {
524 assert(dropArgPos == argCount-1);
525 dropSlotPos = 0;
526 dropSlotCount = mt.parameterSlotCount();
527 } else {
528 // arglist: [0: keep... | dpos: drop... | dpos+dcount: keep... ]
529 int lastDroppedArg = dropArgPos + dropArgCount - 1;
530 int lastKeptArg = dropArgPos - 1; // might be -1, which is OK
531 dropSlotPos = mt.parameterSlot(lastDroppedArg);
532 int lastKeptSlot = mt.parameterSlot(lastKeptArg);
533 dropSlotCount = lastKeptSlot - dropSlotPos;
534 assert(dropSlotCount >= dropArgCount);
535 }
536 long conv = makeConv(DROP_ARGS, dropArgPos, +dropSlotCount);
537 return new AdapterMethodHandle(target, newType, dropSlotCount, conv);
538 }
539
540 // TO DO: makeCollectArguments, makeSpreadArguments, makeFlyby, makeRicochet
541 }