Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/RicochetTest.java
47216 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary unit tests for recursive method handles25* @run junit/othervm/timeout=3600 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -DRicochetTest.MAX_ARITY=10 test.java.lang.invoke.RicochetTest26*/27/*28* @ignore The following test creates an unreasonable number of adapters in -Xcomp mode (7049122)29* @run junit/othervm -DRicochetTest.MAX_ARITY=255 test.java.lang.invoke.RicochetTest30*/3132package test.java.lang.invoke;3334import java.lang.invoke.*;35import java.util.*;36import org.junit.*;37import static java.lang.invoke.MethodType.*;38import static java.lang.invoke.MethodHandles.*;39import static org.junit.Assert.*;404142/**43*44* @author jrose45*/46public class RicochetTest {47private static final Class<?> CLASS = RicochetTest.class;48private static final int MAX_ARITY = Integer.getInteger(CLASS.getSimpleName()+".MAX_ARITY", 40);4950public static void main(String... av) throws Throwable {51RicochetTest test = new RicochetTest();52if (av.length > 0) test.testOnly = Arrays.asList(av).toString();53if (REPEAT == 1 || test.testOnly != null) {54test.testAll();55if (test.testOnlyTests == null) throw new RuntimeException("no matching test: "+test.testOnly);56} else if (REPEAT == 0) {57org.junit.runner.JUnitCore.runClasses(RicochetTest.class);58} else {59verbose(1, "REPEAT="+REPEAT);60for (int i = 0; i < REPEAT; i++) {61test.testRepetition = (i+1);62verbose(0, "[#"+test.testRepetition+"]");63test.testAll();64}65}66}67int testRepetition;6869public void testAll() throws Throwable {70testNull();71testBoxInteger();72testFilterReturnValue();73testFilterObject();74testBoxLong();75testFilterInteger();76testIntSpreads();77testByteSpreads();78testLongSpreads();79testIntCollects();80testReturns();81testRecursion();82}8384@Test85public void testNull() throws Throwable {86if (testRepetition > (1+REPEAT/100)) return; // trivial test87if (!startTest("testNull")) return;88assertEquals(opI(37), opI.invokeWithArguments(37));89assertEqualFunction(opI, opI);90}9192@Test93public void testBoxInteger() throws Throwable {94if (!startTest("testBoxInteger")) return;95assertEqualFunction(opI, opI.asType(opL_I.type()).asType(opI.type()));96}9798@Test99public void testFilterReturnValue() throws Throwable {100if (!startTest("testFilterReturnValue")) return;101int[] ints = { 12, 23, 34, 45, 56, 67, 78, 89 };102Object res = list8ints.invokeExact(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7]);103assertEquals(Arrays.toString(ints), res.toString());104MethodHandle idreturn = filterReturnValue(list8ints, identity(Object.class));105res = idreturn.invokeExact(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], ints[7]);106assertEquals(Arrays.toString(ints), res.toString());107MethodHandle add0 = addL.bindTo(0);108assertEqualFunction(filterReturnValue(opL2, add0), opL2);109}110111@Test112public void testFilterObject() throws Throwable {113if (!startTest("testFilterObject")) return;114MethodHandle add0 = addL.bindTo(0);115assertEqualFunction(sequence(opL2, add0), opL2);116int bump13 = -13; // value near 20 works as long as test values are near [-80..80]117MethodHandle add13 = addL.bindTo(bump13);118MethodHandle add13_0 = addL.bindTo(opI2(bump13, 0));119MethodHandle add13_1 = addL.bindTo(opI2(0, bump13));120assertEqualFunction(sequence(opL2, add13_0),121filterArguments(opL2, 0, add13));122assertEqualFunction(sequence(opL2, add13_1),123filterArguments(opL2, 1, add13));124System.out.println("[testFilterObject done]");125}126127@Test128public void testBoxLong() throws Throwable {129if (!startTest("testBoxLong")) return;130assertEqualFunction(opJ, opJ.asType(opL_J.type()).asType(opJ.type()));131}132133@Test134public void testFilterInteger() throws Throwable {135if (!startTest("testFilterInteger")) return;136assertEqualFunction(opI, sequence(convI_L, opL_I));137}138139@Test140public void testIntSpreads() throws Throwable {141if (!startTest("testIntSpreads")) return;142MethodHandle id = identity(int[].class);143final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added144for (int nargs = 0; nargs <= MAX; nargs++) {145if (nargs > 30 && nargs < MAX-20) nargs += 10;146int[] args = new int[nargs];147for (int j = 0; j < args.length; j++) args[j] = j + 11;148//System.out.println("testIntSpreads "+Arrays.toString(args));149int[] args1 = (int[]) id.invokeExact(args);150assertArrayEquals(args, args1);151MethodHandle coll = id.asCollector(int[].class, nargs);152int[] args2 = args;153switch (nargs) {154case 0: args2 = (int[]) coll.invokeExact(); break;155case 1: args2 = (int[]) coll.invokeExact(args[0]); break;156case 2: args2 = (int[]) coll.invokeExact(args[0], args[1]); break;157case 3: args2 = (int[]) coll.invokeExact(args[0], args[1], args[2]); break;158case 4: args2 = (int[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;159case 5: args2 = (int[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;160}161assertArrayEquals(args, args2);162MethodHandle mh = coll.asSpreader(int[].class, nargs);163int[] args3 = (int[]) mh.invokeExact(args);164assertArrayEquals(args, args3);165}166}167168@Test169public void testByteSpreads() throws Throwable {170if (!startTest("testByteSpreads")) return;171MethodHandle id = identity(byte[].class);172final int MAX = MAX_ARITY-2; // 253+1 would cause parameter overflow with 'this' added173for (int nargs = 0; nargs <= MAX; nargs++) {174if (nargs > 30 && nargs < MAX-20) nargs += 10;175byte[] args = new byte[nargs];176for (int j = 0; j < args.length; j++) args[j] = (byte)(j + 11);177//System.out.println("testByteSpreads "+Arrays.toString(args));178byte[] args1 = (byte[]) id.invokeExact(args);179assertArrayEquals(args, args1);180MethodHandle coll = id.asCollector(byte[].class, nargs);181byte[] args2 = args;182switch (nargs) {183case 0: args2 = (byte[]) coll.invokeExact(); break;184case 1: args2 = (byte[]) coll.invokeExact(args[0]); break;185case 2: args2 = (byte[]) coll.invokeExact(args[0], args[1]); break;186case 3: args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2]); break;187case 4: args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;188case 5: args2 = (byte[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;189}190assertArrayEquals(args, args2);191MethodHandle mh = coll.asSpreader(byte[].class, nargs);192byte[] args3 = (byte[]) mh.invokeExact(args);193assertArrayEquals(args, args3);194}195}196197@Test198public void testLongSpreads() throws Throwable {199if (!startTest("testLongSpreads")) return;200MethodHandle id = identity(long[].class);201final int MAX = (MAX_ARITY - 2) / 2; // 253/2+1 would cause parameter overflow with 'this' added202for (int nargs = 0; nargs <= MAX; nargs++) {203if (nargs > 30 && nargs < MAX-20) nargs += 10;204long[] args = new long[nargs];205for (int j = 0; j < args.length; j++) args[j] = (long)(j + 11);206//System.out.println("testLongSpreads "+Arrays.toString(args));207long[] args1 = (long[]) id.invokeExact(args);208assertArrayEquals(args, args1);209MethodHandle coll = id.asCollector(long[].class, nargs);210long[] args2 = args;211switch (nargs) {212case 0: args2 = (long[]) coll.invokeExact(); break;213case 1: args2 = (long[]) coll.invokeExact(args[0]); break;214case 2: args2 = (long[]) coll.invokeExact(args[0], args[1]); break;215case 3: args2 = (long[]) coll.invokeExact(args[0], args[1], args[2]); break;216case 4: args2 = (long[]) coll.invokeExact(args[0], args[1], args[2], args[3]); break;217case 5: args2 = (long[]) coll.invokeExact(args[0], args[1], args[2], args[3], args[4]); break;218}219assertArrayEquals(args, args2);220MethodHandle mh = coll.asSpreader(long[].class, nargs);221long[] args3 = (long[]) mh.invokeExact(args);222assertArrayEquals(args, args3);223}224}225226@Test227public void testIntCollects() throws Throwable {228if (!startTest("testIntCollects")) return;229for (MethodHandle lister : INT_LISTERS) {230int outputs = lister.type().parameterCount();231for (int collects = 0; collects <= Math.min(outputs, INT_COLLECTORS.length-1); collects++) {232int inputs = outputs - 1 + collects;233if (inputs < 0) continue;234for (int pos = 0; pos + collects <= inputs; pos++) {235MethodHandle collector = INT_COLLECTORS[collects];236int[] args = new int[inputs];237int ap = 0, arg = 31;238for (int i = 0; i < pos; i++)239args[ap++] = arg++ + 0;240for (int i = 0; i < collects; i++)241args[ap++] = arg++ + 10;242while (ap < args.length)243args[ap++] = arg++ + 20;244// calculate piecemeal:245//System.out.println("testIntCollects "+Arrays.asList(lister, pos, collector)+" on "+Arrays.toString(args));246int[] collargs = Arrays.copyOfRange(args, pos, pos+collects);247int coll = (int) collector.asSpreader(int[].class, collargs.length).invokeExact(collargs);248int[] listargs = Arrays.copyOfRange(args, 0, outputs);249System.arraycopy(args, pos+collects, listargs, pos+1, outputs - (pos+1));250listargs[pos] = coll;251//System.out.println(" coll="+coll+" listargs="+Arrays.toString(listargs));252Object expect = lister.asSpreader(int[].class, listargs.length).invokeExact(listargs);253//System.out.println(" expect="+expect);254255// now use the combined MH, and test the output:256MethodHandle mh = collectArguments(lister, pos, int[].class, INT_COLLECTORS[collects]);257if (mh == null) continue; // no infix collection, yet258assert(mh.type().parameterCount() == inputs);259Object observe = mh.asSpreader(int[].class, args.length).invokeExact(args);260assertEquals(expect, observe);261}262}263}264}265266@Test267public void testByteCollects() throws Throwable {268if (!startTest("testByteCollects")) return;269for (MethodHandle lister : BYTE_LISTERS) {270int outputs = lister.type().parameterCount();271for (int collects = 0; collects <= Math.min(outputs, BYTE_COLLECTORS.length-1); collects++) {272int inputs = outputs - 1 + collects;273if (inputs < 0) continue;274for (int pos = 0; pos + collects <= inputs; pos++) {275MethodHandle collector = BYTE_COLLECTORS[collects];276byte[] args = new byte[inputs];277int ap = 0, arg = 31;278for (int i = 0; i < pos; i++)279args[ap++] = (byte)(arg++ + 0);280for (int i = 0; i < collects; i++)281args[ap++] = (byte)(arg++ + 10);282while (ap < args.length)283args[ap++] = (byte)(arg++ + 20);284// calculate piecemeal:285//System.out.println("testIntCollects "+Arrays.asList(lister, pos, collector)+" on "+Arrays.toString(args));286byte[] collargs = Arrays.copyOfRange(args, pos, pos+collects);287byte coll = (byte) collector.asSpreader(byte[].class, collargs.length).invokeExact(collargs);288byte[] listargs = Arrays.copyOfRange(args, 0, outputs);289System.arraycopy(args, pos+collects, listargs, pos+1, outputs - (pos+1));290listargs[pos] = coll;291//System.out.println(" coll="+coll+" listargs="+Arrays.toString(listargs));292Object expect = lister.asSpreader(byte[].class, listargs.length).invokeExact(listargs);293//System.out.println(" expect="+expect);294295// now use the combined MH, and test the output:296MethodHandle mh = collectArguments(lister, pos, byte[].class, BYTE_COLLECTORS[collects]);297if (mh == null) continue; // no infix collection, yet298assert(mh.type().parameterCount() == inputs);299Object observe = mh.asSpreader(byte[].class, args.length).invokeExact(args);300assertEquals(expect, observe);301}302}303}304}305306private static MethodHandle collectArguments(MethodHandle lister, int pos, Class<?> array, MethodHandle collector) {307int collects = collector.type().parameterCount();308int outputs = lister.type().parameterCount();309if (pos == outputs - 1)310return MethodHandles.filterArguments(lister, pos,311collector.asSpreader(array, collects))312.asCollector(array, collects);313//return MethodHandles.collectArguments(lister, pos, collector); //no such animal314return null;315}316317private static final Class<?>[] RETURN_TYPES = {318Object.class, String.class, Integer.class,319int.class, long.class,320boolean.class, byte.class, char.class, short.class,321float.class, double.class,322void.class,323};324325@Test326public void testReturns() throws Throwable {327if (!startTest("testReturns")) return;328// fault injection:329int faultCount = 0; // total of 1296 tests330faultCount = Integer.getInteger("testReturns.faultCount", 0);331for (Class<?> ret : RETURN_TYPES) {332// make a complicated identity function and pass something through it333System.out.println(ret.getSimpleName());334Class<?> vret = (ret == void.class) ? Void.class : ret;335MethodHandle id = // (vret)->ret336identity(vret).asType(methodType(ret, vret));337final int LENGTH = 4;338int[] index = {0};339Object vals = java.lang.reflect.Array.newInstance(vret, LENGTH);340MethodHandle indexGetter = //()->int341insertArguments(arrayElementGetter(index.getClass()), 0, index, 0);342MethodHandle valSelector = // (int)->vret343arrayElementGetter(vals.getClass()).bindTo(vals);344MethodHandle valGetter = // ()->vret345foldArguments(valSelector, indexGetter);346if (ret != void.class) {347for (int i = 0; i < LENGTH; i++) {348Object val = (i + 50);349if (ret == boolean.class) val = (i % 3 == 0);350if (ret == String.class) val = "#"+i;351if (ret == char.class) val = (char)('a'+i);352if (ret == byte.class) val = (byte)~i;353if (ret == short.class) val = (short)(1<<i);354java.lang.reflect.Array.set(vals, i, val);355}356}357for (int i = 0; i < LENGTH; i++) {358Object val = java.lang.reflect.Array.get(vals, i);359System.out.println(i+" => "+val);360index[0] = i;361if (--faultCount == 0) index[0] ^= 1;362Object x = valGetter.invokeWithArguments();363assertEquals(val, x);364// make a return-filter call: x = id(valGetter())365if (--faultCount == 0) index[0] ^= 1;366x = filterReturnValue(valGetter, id).invokeWithArguments();367assertEquals(val, x);368// make a filter call: x = id(*,valGetter(),*)369for (int len = 1; len <= 4; len++) {370for (int pos = 0; pos < len; pos++) {371MethodHandle proj = id; // lambda(..., vret x,...){x}372for (int j = 0; j < len; j++) {373if (j == pos) continue;374proj = dropArguments(proj, j, Object.class);375}376assert(proj.type().parameterCount() == len);377// proj: (Object*, pos: vret, Object*)->ret378assertEquals(vret, proj.type().parameterType(pos));379MethodHandle vgFilter = dropArguments(valGetter, 0, Object.class);380if (--faultCount == 0) index[0] ^= 1;381x = filterArguments(proj, pos, vgFilter).invokeWithArguments(new Object[len]);382assertEquals(val, x);383}384}385// make a fold call:386for (int len = 0; len <= 4; len++) {387for (int fold = 0; fold <= len; fold++) {388MethodHandle proj = id; // lambda(ret x, ...){x}389if (ret == void.class) proj = constant(Object.class, null);390int arg0 = (ret == void.class ? 0 : 1);391for (int j = 0; j < len; j++) {392proj = dropArguments(proj, arg0, Object.class);393}394assert(proj.type().parameterCount() == arg0 + len);395// proj: (Object*, pos: vret, Object*)->ret396if (arg0 != 0) assertEquals(vret, proj.type().parameterType(0));397MethodHandle vgFilter = valGetter.asType(methodType(ret));398for (int j = 0; j < fold; j++) {399vgFilter = dropArguments(vgFilter, j, Object.class);400}401x = foldArguments(proj, vgFilter).invokeWithArguments(new Object[len]);402if (--faultCount == 0) index[0] ^= 1;403assertEquals(val, x);404}405}406}407}408//System.out.println("faultCount="+faultCount);409}410411@Test412public void testRecursion() throws Throwable {413if (!startTest("testRecursion")) return;414final int LIMIT = 10;415for (int i = 0; i < LIMIT; i++) {416RFCB rfcb = new RFCB(i);417Object x = "x", y = "y";418Object result = rfcb.recursiveFunction(x, y);419verbose(1, result);420}421}422/** Recursive Function Control Block */423private static class RFCB {424java.util.Random random;425final MethodHandle[] fns;426int depth;427@SuppressWarnings("LeakingThisInConstructor")428RFCB(int seed) throws Throwable {429this.random = new java.util.Random(seed);430this.fns = new MethodHandle[Math.max(29, (1 << MAX_DEPTH-2)/3)];431java.util.Arrays.fill(fns, lookup().bind(this, "recursiveFunction", genericMethodType(2)));432for (int i = 5; i < fns.length; i++) {433switch (i % 4) {434case 0: fns[i] = filterArguments(fns[i - 5], 0, insertArguments(fns[i - 4], 1, ".")); break;435case 1: fns[i] = filterArguments(fns[i - 5], 1, insertArguments(fns[i - 3], 1, ".")); break;436case 2: fns[i] = filterReturnValue(fns[i - 5], insertArguments(fns[i - 2], 1, ".")); break;437}438}439}440Object recursiveFunction(Object x, Object y) throws Throwable {441depth++;442try {443final int ACTION_COUNT = 11;444switch (random.nextInt(ACTION_COUNT)) {445case 1:446Throwable ex = new RuntimeException();447ex.fillInStackTrace();448if (VERBOSITY >= 2) ex.printStackTrace(System.out);449x = "ST; " + x;450break;451case 2:452System.gc();453x = "GC; " + x;454break;455}456boolean isLeaf = (depth >= MAX_DEPTH);457if (isLeaf) {458return Arrays.asList(x, y).toString();459}460return fns[random.nextInt(fns.length)].invokeExact(x, y);461} finally {462depth--;463}464}465}466467private static MethodHandle sequence(MethodHandle mh1, MethodHandle... mhs) {468MethodHandle res = mh1;469for (MethodHandle mh2 : mhs)470res = filterReturnValue(res, mh2);471return res;472}473private static void assertEqualFunction(MethodHandle x, MethodHandle y) throws Throwable {474assertEquals(x.type(), y.type()); //??475MethodType t = x.type();476if (t.parameterCount() == 0) {477assertEqualFunctionAt(null, x, y);478return;479}480Class<?> ptype = t.parameterType(0);481if (ptype == long.class || ptype == Long.class) {482for (long i = -10; i <= 10; i++) {483assertEqualFunctionAt(i, x, y);484}485} else {486for (int i = -10; i <= 10; i++) {487assertEqualFunctionAt(i, x, y);488}489}490}491private static void assertEqualFunctionAt(Object v, MethodHandle x, MethodHandle y) throws Throwable {492Object[] args = new Object[x.type().parameterCount()];493Arrays.fill(args, v);494Object xval = invokeWithCatch(x, args);495Object yval = invokeWithCatch(y, args);496String msg = "ok";497if (!Objects.equals(xval, yval)) {498msg = ("applying "+x+" & "+y+" to "+v);499}500assertEquals(msg, xval, yval);501}502private static Object invokeWithCatch(MethodHandle mh, Object... args) throws Throwable {503try {504return mh.invokeWithArguments(args);505} catch (Throwable ex) {506System.out.println("threw: "+mh+Arrays.asList(args));507ex.printStackTrace(System.out);508return ex;509}510}511512private static final Lookup LOOKUP = lookup();513private static MethodHandle findStatic(String name,514Class<?> rtype,515Class<?>... ptypes) {516try {517return LOOKUP.findStatic(LOOKUP.lookupClass(), name, methodType(rtype, ptypes));518} catch (ReflectiveOperationException ex) {519throw new RuntimeException(ex);520}521}522private static MethodHandle findStatic(String name,523Class<?> rtype,524List<?> ptypes) {525return findStatic(name, rtype, ptypes.toArray(new Class<?>[ptypes.size()]));526}527static int getProperty(String name, int dflt) {528String qual = LOOKUP.lookupClass().getName();529String prop = System.getProperty(qual+"."+name);530if (prop == null) prop = System.getProperty(name);531if (prop == null) return dflt;532return Integer.parseInt(prop);533}534535private static int opI(int... xs) {536stress();537int base = 100;538int z = 0;539for (int x : xs) {540z = (z * base) + (x % base);541}542verbose("opI", xs.length, xs, z);543return z;544}545private static int opI2(int x, int y) { return opI(x, y); } // x*100 + y%100546private static int opI3(int x, int y, int z) { return opI(x, y, z); }547private static int opI4(int w, int x, int y, int z) { return opI(w, x, y, z); }548private static int opI(int x) { return opI2(x, 37); }549private static Object opI_L(int x) { return (Object) opI(x); }550private static long opJ3(long x, long y, long z) { return (long) opI3((int)x, (int)y, (int)z); }551private static long opJ2(long x, long y) { return (long) opI2((int)x, (int)y); }552private static long opJ(long x) { return (long) opI((int)x); }553private static Object opL2(Object x, Object y) { return (Object) opI2((int)x, (int)y); }554private static Object opL(Object x) { return (Object) opI((int)x); }555private static int opL2_I(Object x, Object y) { return opI2((int)x, (int)y); }556private static int opL_I(Object x) { return opI((int)x); }557private static long opL_J(Object x) { return (long) opI((int)x); }558private static final MethodHandle opI, opI2, opI3, opI4, opI_L, opJ, opJ2, opJ3, opL2, opL, opL2_I, opL_I, opL_J;559static {560opI4 = findStatic("opI4", int.class, int.class, int.class, int.class, int.class);561opI3 = findStatic("opI3", int.class, int.class, int.class, int.class);562opI2 = findStatic("opI2", int.class, int.class, int.class);563opI = findStatic("opI", int.class, int.class);564opI_L = findStatic("opI_L", Object.class, int.class);565opJ = findStatic("opJ", long.class, long.class);566opJ2 = findStatic("opJ2", long.class, long.class, long.class);567opJ3 = findStatic("opJ3", long.class, long.class, long.class, long.class);568opL2 = findStatic("opL2", Object.class, Object.class, Object.class);569opL = findStatic("opL", Object.class, Object.class);570opL2_I = findStatic("opL2_I", int.class, Object.class, Object.class);571opL_I = findStatic("opL_I", int.class, Object.class);572opL_J = findStatic("opL_J", long.class, Object.class);573}574private static final MethodHandle[] INT_COLLECTORS = {575constant(int.class, 42), opI, opI2, opI3, opI4576};577private static final MethodHandle[] BYTE_COLLECTORS = {578constant(byte.class, (byte)42), i2b(opI), i2b(opI2), i2b(opI3), i2b(opI4)579};580private static final MethodHandle[] LONG_COLLECTORS = {581constant(long.class, 42), opJ, opJ2, opJ3582};583584private static int addI(int x, int y) { stress(); return x+y; }585private static Object addL(Object x, Object y) { return addI((int)x, (int)y); }586private static final MethodHandle addI, addL;587static {588addI = findStatic("addI", int.class, int.class, int.class);589addL = findStatic("addL", Object.class, Object.class, Object.class);590}591592private static Object list8ints(int a, int b, int c, int d, int e, int f, int g, int h) {593return Arrays.asList(a, b, c, d, e, f, g, h);594}595private static Object list8longs(long a, long b, long c, long d, long e, long f, long g, long h) {596return Arrays.asList(a, b, c, d, e, f, g, h);597}598private static final MethodHandle list8ints = findStatic("list8ints", Object.class,599Collections.nCopies(8, int.class));600private static final MethodHandle list8longs = findStatic("list8longs", Object.class,601Collections.nCopies(8, long.class));602private static final MethodHandle[] INT_LISTERS, LONG_LISTERS, BYTE_LISTERS;603static {604int listerCount = list8ints.type().parameterCount() + 1;605INT_LISTERS = new MethodHandle[listerCount];606LONG_LISTERS = new MethodHandle[listerCount];607BYTE_LISTERS = new MethodHandle[listerCount];608MethodHandle lister = list8ints;609MethodHandle llister = list8longs;610for (int i = listerCount - 1; ; i--) {611INT_LISTERS[i] = lister;612LONG_LISTERS[i] = llister;613BYTE_LISTERS[i] = i2b(lister);614if (i == 0) break;615lister = insertArguments(lister, i-1, 0);616llister = insertArguments(llister, i-1, 0L);617}618}619private static MethodHandle i2b(MethodHandle mh) {620return MethodHandles.explicitCastArguments(mh, subst(mh.type(), int.class, byte.class));621}622private static MethodType subst(MethodType mt, Class<?> from, Class<?> to) {623for (int i = 0; i < mt.parameterCount(); i++) {624if (mt.parameterType(i) == from)625mt = mt.changeParameterType(i, to);626}627if (mt.returnType() == from)628mt = mt.changeReturnType(to);629return mt;630}631632633private static Object convI_L(int x) { stress(); return (Object) x; }634private static int convL_I(Object x) { stress(); return (int) x; }635private static Object convJ_L(long x) { stress(); return (Object) x; }636private static long convL_J(Object x) { stress(); return (long) x; }637private static int convJ_I(long x) { stress(); return (int) x; }638private static long convI_J(int x) { stress(); return (long) x; }639private static final MethodHandle convI_L, convL_I, convJ_L, convL_J, convJ_I, convI_J;640static {641convI_L = findStatic("convI_L", Object.class, int.class);642convL_I = findStatic("convL_I", int.class, Object.class);643convJ_L = findStatic("convJ_L", Object.class, long.class);644convL_J = findStatic("convL_J", long.class, Object.class);645convJ_I = findStatic("convJ_I", int.class, long.class);646convI_J = findStatic("convI_J", long.class, int.class);647}648649// stress modes:650private static final int MAX_DEPTH = getProperty("MAX_DEPTH", 5);651private static final int REPEAT = getProperty("REPEAT", 0);652private static final int STRESS = getProperty("STRESS", 0);653private static /*v*/ int STRESS_COUNT;654private static final Object[] SINK = new Object[4];655private static void stress() {656if (STRESS <= 0) return;657int count = STRESS + (STRESS_COUNT++ & 0x1); // non-constant value658for (int i = 0; i < count; i++) {659SINK[i % SINK.length] = new Object[STRESS + i % (SINK.length + 1)];660}661}662663// verbosity:664private static final int VERBOSITY = getProperty("VERBOSITY", 0) + (REPEAT == 0 ? 0 : -1);665private static void verbose(Object a, Object b, Object c, Object d) {666if (VERBOSITY <= 0) return;667verbose(1, a, b, c, d);668}669private static void verbose(Object a, Object b, Object c) {670if (VERBOSITY <= 0) return;671verbose(1, a, b, c);672}673private static void verbose(int level, Object a, Object... bcd) {674if (level > VERBOSITY) return;675String m = a.toString();676if (bcd != null && bcd.length > 0) {677List<Object> l = new ArrayList<>(bcd.length);678for (Object x : bcd) {679if (x instanceof Object[]) x = Arrays.asList((Object[])x);680if (x instanceof int[]) x = Arrays.toString((int[])x);681if (x instanceof long[]) x = Arrays.toString((long[])x);682l.add(x);683}684m = m+Arrays.asList(bcd);685}686System.out.println(m);687}688String testOnly;689String testOnlyTests;690private boolean startTest(String name) {691if (testOnly != null && !testOnly.contains(name))692return false;693verbose(0, "["+name+"]");694testOnlyTests = (testOnlyTests == null) ? name : testOnlyTests+" "+name;695return true;696}697698}699700701