1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import java.util.Arrays;
  29 import static java.lang.invoke.LambdaForm.*;
  30 import static java.lang.invoke.LambdaForm.BasicType.*;
  31 import static java.lang.invoke.MethodHandleImpl.Intrinsic;
  32 import java.util.Collections;
  33 import java.util.concurrent.ConcurrentHashMap;
  34 
  35 import sun.invoke.util.Wrapper;
  36 
  37 /** Transforms on LFs.
  38  *  A lambda-form editor can derive new LFs from its base LF.
  39  *  The editor can cache derived LFs, which simplifies the reuse of their underlying bytecodes.
  40  *  To support this caching, a LF has an optional pointer to its editor.
  41  */
  42 class LambdaFormEditor {
  43     final LambdaForm lambdaForm;
  44 
  45     private LambdaFormEditor(LambdaForm lambdaForm) {
  46         this.lambdaForm = lambdaForm;
  47     }
  48 
  49     // Factory method.
  50     static LambdaFormEditor lambdaFormEditor(LambdaForm lambdaForm) {
  51         // TO DO:  Consider placing intern logic here, to cut down on duplication.
  52         // lambdaForm = findPreexistingEquivalent(lambdaForm)
  53         return new LambdaFormEditor(lambdaForm);
  54     }
  55 
  56     /** A description of a cached transform, possibly associated with the result of the transform.
  57      *  The logical content is a sequence of byte values, starting with a Kind.ordinal value.
  58      *  The sequence is unterminated, ending with an indefinite number of zero bytes.
  59      *  Sequences that are simple (short enough and with small enough values) pack into a 64-bit long.
  60      */
  61     private static final class Transform {
  62         final long packedBytes;
  63         final byte[] fullBytes;
  64         final LambdaForm result;  // result of transform, or null, if there is none available
  65 
  66         private enum Kind {
  67             NO_KIND,  // necessary because ordinal must be greater than zero
  68             BIND_ARG, ADD_ARG, DUP_ARG,
  69             SPREAD_ARGS,
  70             FILTER_ARG, FILTER_RETURN, FILTER_RETURN_TO_ZERO,
  71             COLLECT_ARGS, COLLECT_ARGS_TO_VOID, COLLECT_ARGS_TO_ARRAY,
  72             FOLD_ARGS, FOLD_ARGS_TO_VOID,
  73             PERMUTE_ARGS
  74             //maybe add more for guard with test, catch exception, pointwise type conversions
  75         }
  76 
  77         private static final boolean STRESS_TEST = false; // turn on to disable most packing
  78         private static final int
  79                 PACKED_BYTE_SIZE = (STRESS_TEST ? 2 : 4),
  80                 PACKED_BYTE_MASK = (1 << PACKED_BYTE_SIZE) - 1,
  81                 PACKED_BYTE_MAX_LENGTH = (STRESS_TEST ? 3 : 64 / PACKED_BYTE_SIZE);
  82 
  83         private static long packedBytes(byte[] bytes) {
  84             if (bytes.length > PACKED_BYTE_MAX_LENGTH)  return 0;
  85             long pb = 0;
  86             int bitset = 0;
  87             for (int i = 0; i < bytes.length; i++) {
  88                 int b = bytes[i] & 0xFF;
  89                 bitset |= b;
  90                 pb |= (long)b << (i * PACKED_BYTE_SIZE);
  91             }
  92             if (!inRange(bitset))
  93                 return 0;
  94             return pb;
  95         }
  96         private static long packedBytes(int b0, int b1) {
  97             assert(inRange(b0 | b1));
  98             return (  (b0 << 0*PACKED_BYTE_SIZE)
  99                     | (b1 << 1*PACKED_BYTE_SIZE));
 100         }
 101         private static long packedBytes(int b0, int b1, int b2) {
 102             assert(inRange(b0 | b1 | b2));
 103             return (  (b0 << 0*PACKED_BYTE_SIZE)
 104                     | (b1 << 1*PACKED_BYTE_SIZE)
 105                     | (b2 << 2*PACKED_BYTE_SIZE));
 106         }
 107         private static long packedBytes(int b0, int b1, int b2, int b3) {
 108             assert(inRange(b0 | b1 | b2 | b3));
 109             return (  (b0 << 0*PACKED_BYTE_SIZE)
 110                     | (b1 << 1*PACKED_BYTE_SIZE)
 111                     | (b2 << 2*PACKED_BYTE_SIZE)
 112                     | (b3 << 3*PACKED_BYTE_SIZE));
 113         }
 114         private static boolean inRange(int bitset) {
 115             assert((bitset & 0xFF) == bitset);  // incoming values must fit in *unsigned* byte
 116             return ((bitset & ~PACKED_BYTE_MASK) == 0);
 117         }
 118         private static byte[] fullBytes(int... byteValues) {
 119             byte[] bytes = new byte[byteValues.length];
 120             int i = 0;
 121             for (int bv : byteValues) {
 122                 bytes[i++] = bval(bv);
 123             }
 124             assert(packedBytes(bytes) == 0);
 125             return bytes;
 126         }
 127 
 128         private byte byteAt(int i) {
 129             long pb = packedBytes;
 130             if (pb == 0) {
 131                 if (i >= fullBytes.length)  return 0;
 132                 return fullBytes[i];
 133             }
 134             assert(fullBytes == null);
 135             if (i > PACKED_BYTE_MAX_LENGTH)  return 0;
 136             int pos = (i * PACKED_BYTE_SIZE);
 137             return (byte)((pb >>> pos) & PACKED_BYTE_MASK);
 138         }
 139 
 140         Kind kind() { return Kind.values()[byteAt(0)]; }
 141 
 142         private Transform(long packedBytes, byte[] fullBytes, LambdaForm result) {
 143             this.packedBytes = packedBytes;
 144             this.fullBytes = fullBytes;
 145             this.result = result;
 146         }
 147         private Transform(long packedBytes) {
 148             this(packedBytes, null, null);
 149             assert(packedBytes != 0);
 150         }
 151         private Transform(byte[] fullBytes) {
 152             this(0, fullBytes, null);
 153         }
 154 
 155         private static byte bval(int b) {
 156             assert((b & 0xFF) == b);  // incoming value must fit in *unsigned* byte
 157             return (byte)b;
 158         }
 159         private static byte bval(Kind k) {
 160             return bval(k.ordinal());
 161         }
 162         static Transform of(Kind k, int b1) {
 163             byte b0 = bval(k);
 164             if (inRange(b0 | b1))
 165                 return new Transform(packedBytes(b0, b1));
 166             else
 167                 return new Transform(fullBytes(b0, b1));
 168         }
 169         static Transform of(Kind k, int b1, int b2) {
 170             byte b0 = (byte) k.ordinal();
 171             if (inRange(b0 | b1 | b2))
 172                 return new Transform(packedBytes(b0, b1, b2));
 173             else
 174                 return new Transform(fullBytes(b0, b1, b2));
 175         }
 176         static Transform of(Kind k, int b1, int b2, int b3) {
 177             byte b0 = (byte) k.ordinal();
 178             if (inRange(b0 | b1 | b2 | b3))
 179                 return new Transform(packedBytes(b0, b1, b2, b3));
 180             else
 181                 return new Transform(fullBytes(b0, b1, b2, b3));
 182         }
 183         private static final byte[] NO_BYTES = {};
 184         static Transform of(Kind k, int... b123) {
 185             return ofBothArrays(k, b123, NO_BYTES);
 186         }
 187         static Transform of(Kind k, int b1, byte[] b234) {
 188             return ofBothArrays(k, new int[]{ b1 }, b234);
 189         }
 190         static Transform of(Kind k, int b1, int b2, byte[] b345) {
 191             return ofBothArrays(k, new int[]{ b1, b2 }, b345);
 192         }
 193         private static Transform ofBothArrays(Kind k, int[] b123, byte[] b456) {
 194             byte[] fullBytes = new byte[1 + b123.length + b456.length];
 195             int i = 0;
 196             fullBytes[i++] = bval(k);
 197             for (int bv : b123) {
 198                 fullBytes[i++] = bval(bv);
 199             }
 200             for (byte bv : b456) {
 201                 fullBytes[i++] = bv;
 202             }
 203             long packedBytes = packedBytes(fullBytes);
 204             if (packedBytes != 0)
 205                 return new Transform(packedBytes);
 206             else
 207                 return new Transform(fullBytes);
 208         }
 209 
 210         Transform withResult(LambdaForm result) {
 211             return new Transform(this.packedBytes, this.fullBytes, result);
 212         }
 213 
 214         @Override
 215         public boolean equals(Object obj) {
 216             return obj instanceof Transform && equals((Transform)obj);
 217         }
 218         public boolean equals(Transform that) {
 219             return this.packedBytes == that.packedBytes && Arrays.equals(this.fullBytes, that.fullBytes);
 220         }
 221         @Override
 222         public int hashCode() {
 223             if (packedBytes != 0) {
 224                 assert(fullBytes == null);
 225                 return Long.hashCode(packedBytes);
 226             }
 227             return Arrays.hashCode(fullBytes);
 228         }
 229         @Override
 230         public String toString() {
 231             StringBuilder buf = new StringBuilder();
 232             long bits = packedBytes;
 233             if (bits != 0) {
 234                 buf.append("(");
 235                 while (bits != 0) {
 236                     buf.append(bits & PACKED_BYTE_MASK);
 237                     bits >>>= PACKED_BYTE_SIZE;
 238                     if (bits != 0)  buf.append(",");
 239                 }
 240                 buf.append(")");
 241             }
 242             if (fullBytes != null) {
 243                 buf.append("unpacked");
 244                 buf.append(Arrays.toString(fullBytes));
 245             }
 246             if (result != null) {
 247                 buf.append(" result=");
 248                 buf.append(result);
 249             }
 250             return buf.toString();
 251         }
 252     }
 253 
 254     /** Find a previously cached transform equivalent to the given one, and return its result. */
 255     private LambdaForm getInCache(Transform key) {
 256         assert(key.result == null);
 257         // The transformCache is one of null, Transform, Transform[], or ConcurrentHashMap.
 258         Object c = lambdaForm.transformCache;
 259         Transform k = null;
 260         if (c instanceof ConcurrentHashMap) {
 261             @SuppressWarnings("unchecked")
 262             ConcurrentHashMap<Transform,Transform> m = (ConcurrentHashMap<Transform,Transform>) c;
 263             k = m.get(key);
 264         } else if (c == null) {
 265             return null;
 266         } else if (c instanceof Transform) {
 267             // one-element cache avoids overhead of an array
 268             Transform t = (Transform)c;
 269             if (t.equals(key))  k = t;
 270         } else {
 271             Transform[] ta = (Transform[])c;
 272             for (int i = 0; i < ta.length; i++) {
 273                 Transform t = ta[i];
 274                 if (t == null)  break;
 275                 if (t.equals(key)) { k = t; break; }
 276             }
 277         }
 278         assert(k == null || key.equals(k));
 279         return k == null ? null : k.result;
 280     }
 281 
 282     /** Arbitrary but reasonable limits on Transform[] size for cache. */
 283     private static final int MIN_CACHE_ARRAY_SIZE = 4, MAX_CACHE_ARRAY_SIZE = 16;
 284 
 285     /** Cache a transform with its result, and return that result.
 286      *  But if an equivalent transform has already been cached, return its result instead.
 287      */
 288     private LambdaForm putInCache(Transform key, LambdaForm form) {
 289         key = key.withResult(form);
 290         for (int pass = 0; ; pass++) {
 291             Object c = lambdaForm.transformCache;
 292             if (c instanceof ConcurrentHashMap) {
 293                 @SuppressWarnings("unchecked")
 294                 ConcurrentHashMap<Transform,Transform> m = (ConcurrentHashMap<Transform,Transform>) c;
 295                 Transform k = m.putIfAbsent(key, key);
 296                 return k != null ? k.result : form;
 297             }
 298             assert(pass == 0);
 299             synchronized (lambdaForm) {
 300                 c = lambdaForm.transformCache;
 301                 if (c instanceof ConcurrentHashMap)
 302                     continue;
 303                 if (c == null) {
 304                     lambdaForm.transformCache = key;
 305                     return form;
 306                 }
 307                 Transform[] ta;
 308                 if (c instanceof Transform) {
 309                     Transform k = (Transform)c;
 310                     if (k.equals(key)) {
 311                         return k.result;
 312                     }
 313                     // expand one-element cache to small array
 314                     ta = new Transform[MIN_CACHE_ARRAY_SIZE];
 315                     ta[0] = k;
 316                     lambdaForm.transformCache = c = ta;
 317                 } else {
 318                     // it is already expanded
 319                     ta = (Transform[])c;
 320                 }
 321                 int len = ta.length;
 322                 int i;
 323                 for (i = 0; i < len; i++) {
 324                     Transform k = ta[i];
 325                     if (k == null) {
 326                         break;
 327                     }
 328                     if (k.equals(key)) {
 329                         return k.result;
 330                     }
 331                 }
 332                 if (i < len) {
 333                     // just fall through to cache update
 334                 } else if (len < MAX_CACHE_ARRAY_SIZE) {
 335                     len = Math.min(len * 2, MAX_CACHE_ARRAY_SIZE);
 336                     ta = Arrays.copyOf(ta, len);
 337                     lambdaForm.transformCache = ta;
 338                 } else {
 339                     ConcurrentHashMap<Transform, Transform> m = new ConcurrentHashMap<>(MAX_CACHE_ARRAY_SIZE * 2);
 340                     for (Transform k : ta) {
 341                         m.put(k, k);
 342                     }
 343                     lambdaForm.transformCache = m;
 344                     // The second iteration will update for this query, concurrently.
 345                     continue;
 346                 }
 347                 ta[i] = key;
 348                 return form;
 349             }
 350         }
 351     }
 352 
 353     private LambdaFormBuffer buffer() {
 354         return new LambdaFormBuffer(lambdaForm);
 355     }
 356 
 357     /// Editing methods for method handles.  These need to have fast paths.
 358 
 359     private BoundMethodHandle.SpeciesData oldSpeciesData() {
 360         return BoundMethodHandle.speciesData(lambdaForm);
 361     }
 362     private BoundMethodHandle.SpeciesData newSpeciesData(BasicType type) {
 363         return oldSpeciesData().extendWith(type);
 364     }
 365 
 366     BoundMethodHandle bindArgumentL(BoundMethodHandle mh, int pos, Object value) {
 367         assert(mh.speciesData() == oldSpeciesData());
 368         BasicType bt = L_TYPE;
 369         MethodType type2 = bindArgumentType(mh, pos, bt);
 370         LambdaForm form2 = bindArgumentForm(1+pos);
 371         return mh.copyWithExtendL(type2, form2, value);
 372     }
 373     BoundMethodHandle bindArgumentI(BoundMethodHandle mh, int pos, int value) {
 374         assert(mh.speciesData() == oldSpeciesData());
 375         BasicType bt = I_TYPE;
 376         MethodType type2 = bindArgumentType(mh, pos, bt);
 377         LambdaForm form2 = bindArgumentForm(1+pos);
 378         return mh.copyWithExtendI(type2, form2, value);
 379     }
 380 
 381     BoundMethodHandle bindArgumentJ(BoundMethodHandle mh, int pos, long value) {
 382         assert(mh.speciesData() == oldSpeciesData());
 383         BasicType bt = J_TYPE;
 384         MethodType type2 = bindArgumentType(mh, pos, bt);
 385         LambdaForm form2 = bindArgumentForm(1+pos);
 386         return mh.copyWithExtendJ(type2, form2, value);
 387     }
 388 
 389     BoundMethodHandle bindArgumentF(BoundMethodHandle mh, int pos, float value) {
 390         assert(mh.speciesData() == oldSpeciesData());
 391         BasicType bt = F_TYPE;
 392         MethodType type2 = bindArgumentType(mh, pos, bt);
 393         LambdaForm form2 = bindArgumentForm(1+pos);
 394         return mh.copyWithExtendF(type2, form2, value);
 395     }
 396 
 397     BoundMethodHandle bindArgumentD(BoundMethodHandle mh, int pos, double value) {
 398         assert(mh.speciesData() == oldSpeciesData());
 399         BasicType bt = D_TYPE;
 400         MethodType type2 = bindArgumentType(mh, pos, bt);
 401         LambdaForm form2 = bindArgumentForm(1+pos);
 402         return mh.copyWithExtendD(type2, form2, value);
 403     }
 404 
 405     private MethodType bindArgumentType(BoundMethodHandle mh, int pos, BasicType bt) {
 406         assert(mh.form == lambdaForm);
 407         assert(mh.form.names[1+pos].type == bt);
 408         assert(BasicType.basicType(mh.type().parameterType(pos)) == bt);
 409         return mh.type().dropParameterTypes(pos, pos+1);
 410     }
 411 
 412     /// Editing methods for lambda forms.
 413     // Each editing method can (potentially) cache the edited LF so that it can be reused later.
 414 
 415     LambdaForm bindArgumentForm(int pos) {
 416         Transform key = Transform.of(Transform.Kind.BIND_ARG, pos);
 417         LambdaForm form = getInCache(key);
 418         if (form != null) {
 419             assert(form.parameterConstraint(0) == newSpeciesData(lambdaForm.parameterType(pos)));
 420             return form;
 421         }
 422         LambdaFormBuffer buf = buffer();
 423         buf.startEdit();
 424 
 425         BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
 426         BoundMethodHandle.SpeciesData newData = newSpeciesData(lambdaForm.parameterType(pos));
 427         Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
 428         Name newBaseAddress;
 429         NamedFunction getter = newData.getterFunction(oldData.fieldCount());
 430 
 431         if (pos != 0) {
 432             // The newly created LF will run with a different BMH.
 433             // Switch over any pre-existing BMH field references to the new BMH class.
 434             buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
 435             newBaseAddress = oldBaseAddress.withConstraint(newData);
 436             buf.renameParameter(0, newBaseAddress);
 437             buf.replaceParameterByNewExpression(pos, new Name(getter, newBaseAddress));
 438         } else {
 439             // cannot bind the MH arg itself, unless oldData is empty
 440             assert(oldData == BoundMethodHandle.SpeciesData.EMPTY);
 441             newBaseAddress = new Name(L_TYPE).withConstraint(newData);
 442             buf.replaceParameterByNewExpression(0, new Name(getter, newBaseAddress));
 443             buf.insertParameter(0, newBaseAddress);
 444         }
 445 
 446         form = buf.endEdit();
 447         return putInCache(key, form);
 448     }
 449 
 450     LambdaForm addArgumentForm(int pos, BasicType type) {
 451         Transform key = Transform.of(Transform.Kind.ADD_ARG, pos, type.ordinal());
 452         LambdaForm form = getInCache(key);
 453         if (form != null) {
 454             assert(form.arity == lambdaForm.arity+1);
 455             assert(form.parameterType(pos) == type);
 456             return form;
 457         }
 458         LambdaFormBuffer buf = buffer();
 459         buf.startEdit();
 460 
 461         buf.insertParameter(pos, new Name(type));
 462 
 463         form = buf.endEdit();
 464         return putInCache(key, form);
 465     }
 466 
 467     LambdaForm dupArgumentForm(int srcPos, int dstPos) {
 468         Transform key = Transform.of(Transform.Kind.DUP_ARG, srcPos, dstPos);
 469         LambdaForm form = getInCache(key);
 470         if (form != null) {
 471             assert(form.arity == lambdaForm.arity-1);
 472             return form;
 473         }
 474         LambdaFormBuffer buf = buffer();
 475         buf.startEdit();
 476 
 477         assert(lambdaForm.parameter(srcPos).constraint == null);
 478         assert(lambdaForm.parameter(dstPos).constraint == null);
 479         buf.replaceParameterByCopy(dstPos, srcPos);
 480 
 481         form = buf.endEdit();
 482         return putInCache(key, form);
 483     }
 484 
 485     LambdaForm spreadArgumentsForm(int pos, Class<?> arrayType, int arrayLength) {
 486         Class<?> elementType = arrayType.getComponentType();
 487         Class<?> erasedArrayType = arrayType;
 488         if (!elementType.isPrimitive())
 489             erasedArrayType = Object[].class;
 490         BasicType bt = basicType(elementType);
 491         int elementTypeKey = bt.ordinal();
 492         if (bt.basicTypeClass() != elementType) {
 493             if (elementType.isPrimitive()) {
 494                 elementTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal();
 495             }
 496         }
 497         Transform key = Transform.of(Transform.Kind.SPREAD_ARGS, pos, elementTypeKey, arrayLength);
 498         LambdaForm form = getInCache(key);
 499         if (form != null) {
 500             assert(form.arity == lambdaForm.arity - arrayLength + 1);
 501             return form;
 502         }
 503         LambdaFormBuffer buf = buffer();
 504         buf.startEdit();
 505 
 506         assert(pos <= MethodType.MAX_JVM_ARITY);
 507         assert(pos + arrayLength <= lambdaForm.arity);
 508         assert(pos > 0);  // cannot spread the MH arg itself
 509 
 510         Name spreadParam = new Name(L_TYPE);
 511         Name checkSpread = new Name(MethodHandleImpl.Lazy.NF_checkSpreadArgument, spreadParam, arrayLength);
 512 
 513         // insert the new expressions
 514         int exprPos = lambdaForm.arity();
 515         buf.insertExpression(exprPos++, checkSpread);
 516         // adjust the arguments
 517         MethodHandle aload = MethodHandles.arrayElementGetter(erasedArrayType);
 518         for (int i = 0; i < arrayLength; i++) {
 519             Name loadArgument = new Name(aload, spreadParam, i);
 520             buf.insertExpression(exprPos + i, loadArgument);
 521             buf.replaceParameterByCopy(pos + i, exprPos + i);
 522         }
 523         buf.insertParameter(pos, spreadParam);
 524 
 525         form = buf.endEdit();
 526         return putInCache(key, form);
 527     }
 528 
 529     LambdaForm collectArgumentsForm(int pos, MethodType collectorType) {
 530         int collectorArity = collectorType.parameterCount();
 531         boolean dropResult = (collectorType.returnType() == void.class);
 532         if (collectorArity == 1 && !dropResult) {
 533             return filterArgumentForm(pos, basicType(collectorType.parameterType(0)));
 534         }
 535         BasicType[] newTypes = BasicType.basicTypes(collectorType.parameterList());
 536         Transform.Kind kind = (dropResult
 537                 ? Transform.Kind.COLLECT_ARGS_TO_VOID
 538                 : Transform.Kind.COLLECT_ARGS);
 539         if (dropResult && collectorArity == 0)  pos = 1;  // pure side effect
 540         Transform key = Transform.of(kind, pos, collectorArity, BasicType.basicTypesOrd(newTypes));
 541         LambdaForm form = getInCache(key);
 542         if (form != null) {
 543             assert(form.arity == lambdaForm.arity - (dropResult ? 0 : 1) + collectorArity);
 544             return form;
 545         }
 546         form = makeArgumentCombinationForm(pos, collectorType, false, dropResult);
 547         return putInCache(key, form);
 548     }
 549 
 550     LambdaForm collectArgumentArrayForm(int pos, MethodHandle arrayCollector) {
 551         MethodType collectorType = arrayCollector.type();
 552         int collectorArity = collectorType.parameterCount();
 553         assert(arrayCollector.intrinsicName() == Intrinsic.NEW_ARRAY);
 554         Class<?> arrayType = collectorType.returnType();
 555         Class<?> elementType = arrayType.getComponentType();
 556         BasicType argType = basicType(elementType);
 557         int argTypeKey = argType.ordinal();
 558         if (argType.basicTypeClass() != elementType) {
 559             // return null if it requires more metadata (like String[].class)
 560             if (!elementType.isPrimitive())
 561                 return null;
 562             argTypeKey = TYPE_LIMIT + Wrapper.forPrimitiveType(elementType).ordinal();
 563         }
 564         assert(collectorType.parameterList().equals(Collections.nCopies(collectorArity, elementType)));
 565         Transform.Kind kind = Transform.Kind.COLLECT_ARGS_TO_ARRAY;
 566         Transform key = Transform.of(kind, pos, collectorArity, argTypeKey);
 567         LambdaForm form = getInCache(key);
 568         if (form != null) {
 569             assert(form.arity == lambdaForm.arity - 1 + collectorArity);
 570             return form;
 571         }
 572         LambdaFormBuffer buf = buffer();
 573         buf.startEdit();
 574 
 575         assert(pos + 1 <= lambdaForm.arity);
 576         assert(pos > 0);  // cannot filter the MH arg itself
 577 
 578         Name[] newParams = new Name[collectorArity];
 579         for (int i = 0; i < collectorArity; i++) {
 580             newParams[i] = new Name(pos + i, argType);
 581         }
 582         Name callCombiner = new Name(arrayCollector, (Object[]) /*...*/ newParams);
 583 
 584         // insert the new expression
 585         int exprPos = lambdaForm.arity();
 586         buf.insertExpression(exprPos, callCombiner);
 587 
 588         // insert new arguments
 589         int argPos = pos + 1;  // skip result parameter
 590         for (Name newParam : newParams) {
 591             buf.insertParameter(argPos++, newParam);
 592         }
 593         assert(buf.lastIndexOf(callCombiner) == exprPos+newParams.length);
 594         buf.replaceParameterByCopy(pos, exprPos+newParams.length);
 595 
 596         form = buf.endEdit();
 597         return putInCache(key, form);
 598     }
 599 
 600     LambdaForm filterArgumentForm(int pos, BasicType newType) {
 601         Transform key = Transform.of(Transform.Kind.FILTER_ARG, pos, newType.ordinal());
 602         LambdaForm form = getInCache(key);
 603         if (form != null) {
 604             assert(form.arity == lambdaForm.arity);
 605             assert(form.parameterType(pos) == newType);
 606             return form;
 607         }
 608 
 609         BasicType oldType = lambdaForm.parameterType(pos);
 610         MethodType filterType = MethodType.methodType(oldType.basicTypeClass(),
 611                 newType.basicTypeClass());
 612         form = makeArgumentCombinationForm(pos, filterType, false, false);
 613         return putInCache(key, form);
 614     }
 615 
 616     private LambdaForm makeArgumentCombinationForm(int pos,
 617                                                    MethodType combinerType,
 618                                                    boolean keepArguments, boolean dropResult) {
 619         LambdaFormBuffer buf = buffer();
 620         buf.startEdit();
 621         int combinerArity = combinerType.parameterCount();
 622         int resultArity = (dropResult ? 0 : 1);
 623 
 624         assert(pos <= MethodType.MAX_JVM_ARITY);
 625         assert(pos + resultArity + (keepArguments ? combinerArity : 0) <= lambdaForm.arity);
 626         assert(pos > 0);  // cannot filter the MH arg itself
 627         assert(combinerType == combinerType.basicType());
 628         assert(combinerType.returnType() != void.class || dropResult);
 629 
 630         BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
 631         BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE);
 632 
 633         // The newly created LF will run with a different BMH.
 634         // Switch over any pre-existing BMH field references to the new BMH class.
 635         Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
 636         buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
 637         Name newBaseAddress = oldBaseAddress.withConstraint(newData);
 638         buf.renameParameter(0, newBaseAddress);
 639 
 640         Name getCombiner = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress);
 641         Object[] combinerArgs = new Object[1 + combinerArity];
 642         combinerArgs[0] = getCombiner;
 643         Name[] newParams;
 644         if (keepArguments) {
 645             newParams = new Name[0];
 646             System.arraycopy(lambdaForm.names, pos + resultArity,
 647                              combinerArgs, 1, combinerArity);
 648         } else {
 649             newParams = new Name[combinerArity];
 650             BasicType[] newTypes = basicTypes(combinerType.parameterList());
 651             for (int i = 0; i < newTypes.length; i++) {
 652                 newParams[i] = new Name(pos + i, newTypes[i]);
 653             }
 654             System.arraycopy(newParams, 0,
 655                              combinerArgs, 1, combinerArity);
 656         }
 657         Name callCombiner = new Name(combinerType, combinerArgs);
 658 
 659         // insert the two new expressions
 660         int exprPos = lambdaForm.arity();
 661         buf.insertExpression(exprPos+0, getCombiner);
 662         buf.insertExpression(exprPos+1, callCombiner);
 663 
 664         // insert new arguments, if needed
 665         int argPos = pos + resultArity;  // skip result parameter
 666         for (Name newParam : newParams) {
 667             buf.insertParameter(argPos++, newParam);
 668         }
 669         assert(buf.lastIndexOf(callCombiner) == exprPos+1+newParams.length);
 670         if (!dropResult) {
 671             buf.replaceParameterByCopy(pos, exprPos+1+newParams.length);
 672         }
 673 
 674         return buf.endEdit();
 675     }
 676 
 677     LambdaForm filterReturnForm(BasicType newType, boolean constantZero) {
 678         Transform.Kind kind = (constantZero ? Transform.Kind.FILTER_RETURN_TO_ZERO : Transform.Kind.FILTER_RETURN);
 679         Transform key = Transform.of(kind, newType.ordinal());
 680         LambdaForm form = getInCache(key);
 681         if (form != null) {
 682             assert(form.arity == lambdaForm.arity);
 683             assert(form.returnType() == newType);
 684             return form;
 685         }
 686         LambdaFormBuffer buf = buffer();
 687         buf.startEdit();
 688 
 689         int insPos = lambdaForm.names.length;
 690         Name callFilter;
 691         if (constantZero) {
 692             // Synthesize a constant zero value for the given type.
 693             if (newType == V_TYPE)
 694                 callFilter = null;
 695             else
 696                 callFilter = new Name(constantZero(newType));
 697         } else {
 698             BoundMethodHandle.SpeciesData oldData = oldSpeciesData();
 699             BoundMethodHandle.SpeciesData newData = newSpeciesData(L_TYPE);
 700 
 701             // The newly created LF will run with a different BMH.
 702             // Switch over any pre-existing BMH field references to the new BMH class.
 703             Name oldBaseAddress = lambdaForm.parameter(0);  // BMH holding the values
 704             buf.replaceFunctions(oldData.getterFunctions(), newData.getterFunctions(), oldBaseAddress);
 705             Name newBaseAddress = oldBaseAddress.withConstraint(newData);
 706             buf.renameParameter(0, newBaseAddress);
 707 
 708             Name getFilter = new Name(newData.getterFunction(oldData.fieldCount()), newBaseAddress);
 709             buf.insertExpression(insPos++, getFilter);
 710             BasicType oldType = lambdaForm.returnType();
 711             if (oldType == V_TYPE) {
 712                 MethodType filterType = MethodType.methodType(newType.basicTypeClass());
 713                 callFilter = new Name(filterType, getFilter);
 714             } else {
 715                 MethodType filterType = MethodType.methodType(newType.basicTypeClass(), oldType.basicTypeClass());
 716                 callFilter = new Name(filterType, getFilter, lambdaForm.names[lambdaForm.result]);
 717             }
 718         }
 719 
 720         if (callFilter != null)
 721             buf.insertExpression(insPos++, callFilter);
 722         buf.setResult(callFilter);
 723 
 724         form = buf.endEdit();
 725         return putInCache(key, form);
 726     }
 727 
 728     LambdaForm foldArgumentsForm(int foldPos, boolean dropResult, MethodType combinerType) {
 729         int combinerArity = combinerType.parameterCount();
 730         Transform.Kind kind = (dropResult ? Transform.Kind.FOLD_ARGS_TO_VOID : Transform.Kind.FOLD_ARGS);
 731         Transform key = Transform.of(kind, foldPos, combinerArity);
 732         LambdaForm form = getInCache(key);
 733         if (form != null) {
 734             assert(form.arity == lambdaForm.arity - (kind == Transform.Kind.FOLD_ARGS ? 1 : 0));
 735             return form;
 736         }
 737         form = makeArgumentCombinationForm(foldPos, combinerType, true, dropResult);
 738         return putInCache(key, form);
 739     }
 740 
 741     LambdaForm permuteArgumentsForm(int skip, int[] reorder) {
 742         assert(skip == 1);  // skip only the leading MH argument, names[0]
 743         int length = lambdaForm.names.length;
 744         int outArgs = reorder.length;
 745         int inTypes = 0;
 746         boolean nullPerm = true;
 747         for (int i = 0; i < reorder.length; i++) {
 748             int inArg = reorder[i];
 749             if (inArg != i)  nullPerm = false;
 750             inTypes = Math.max(inTypes, inArg+1);
 751         }
 752         assert(skip + reorder.length == lambdaForm.arity);
 753         if (nullPerm)  return lambdaForm;  // do not bother to cache
 754         Transform key = Transform.of(Transform.Kind.PERMUTE_ARGS, reorder);
 755         LambdaForm form = getInCache(key);
 756         if (form != null) {
 757             assert(form.arity == skip+inTypes) : form;
 758             return form;
 759         }
 760 
 761         BasicType[] types = new BasicType[inTypes];
 762         for (int i = 0; i < outArgs; i++) {
 763             int inArg = reorder[i];
 764             types[inArg] = lambdaForm.names[skip + i].type;
 765         }
 766         assert (skip + outArgs == lambdaForm.arity);
 767         assert (permutedTypesMatch(reorder, types, lambdaForm.names, skip));
 768         int pos = 0;
 769         while (pos < outArgs && reorder[pos] == pos) {
 770             pos += 1;
 771         }
 772         Name[] names2 = new Name[length - outArgs + inTypes];
 773         System.arraycopy(lambdaForm.names, 0, names2, 0, skip + pos);
 774         int bodyLength = length - lambdaForm.arity;
 775         System.arraycopy(lambdaForm.names, skip + outArgs, names2, skip + inTypes, bodyLength);
 776         int arity2 = names2.length - bodyLength;
 777         int result2 = lambdaForm.result;
 778         if (result2 >= 0) {
 779             if (result2 < skip + outArgs) {
 780                 result2 = reorder[result2 - skip];
 781             } else {
 782                 result2 = result2 - outArgs + inTypes;
 783             }
 784         }
 785         for (int j = pos; j < outArgs; j++) {
 786             Name n = lambdaForm.names[skip + j];
 787             int i = reorder[j];
 788             Name n2 = names2[skip + i];
 789             if (n2 == null) {
 790                 names2[skip + i] = n2 = new Name(types[i]);
 791             } else {
 792                 assert (n2.type == types[i]);
 793             }
 794             for (int k = arity2; k < names2.length; k++) {
 795                 names2[k] = names2[k].replaceName(n, n2);
 796             }
 797         }
 798         for (int i = skip + pos; i < arity2; i++) {
 799             if (names2[i] == null) {
 800                 names2[i] = argument(i, types[i - skip]);
 801             }
 802         }
 803         for (int j = lambdaForm.arity; j < lambdaForm.names.length; j++) {
 804             int i = j - lambdaForm.arity + arity2;
 805             Name n = lambdaForm.names[j];
 806             Name n2 = names2[i];
 807             if (n != n2) {
 808                 for (int k = i + 1; k < names2.length; k++) {
 809                     names2[k] = names2[k].replaceName(n, n2);
 810                 }
 811             }
 812         }
 813 
 814         form = new LambdaForm(lambdaForm.debugName, arity2, names2, result2);
 815         return putInCache(key, form);
 816     }
 817 
 818     static boolean permutedTypesMatch(int[] reorder, BasicType[] types, Name[] names, int skip) {
 819         for (int i = 0; i < reorder.length; i++) {
 820             assert (names[skip + i].isParam());
 821             assert (names[skip + i].type == types[reorder[i]]);
 822         }
 823         return true;
 824     }
 825 }