1 /* 2 * Copyright (c) 2015, 2016, 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 /* @test 27 * @bug 8139885 28 * @bug 8150635 29 * @run testng/othervm -ea -esa test.java.lang.invoke.LoopCombinatorTest 30 */ 31 32 package test.java.lang.invoke; 33 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.lang.invoke.MethodHandles.Lookup; 37 import java.lang.invoke.MethodType; 38 import java.util.*; 39 40 import static java.lang.invoke.MethodType.methodType; 41 42 import static org.testng.AssertJUnit.*; 43 44 import org.testng.annotations.*; 45 46 /** 47 * Tests for the loop combinators introduced in JEP 274. 48 */ 49 public class LoopCombinatorTest { 50 51 static final Lookup LOOKUP = MethodHandles.lookup(); 52 53 @Test 54 public static void testLoopFac() throws Throwable { 55 MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc}; 56 MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}; 57 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause); 58 assertEquals(Fac.MT_fac, loop.type()); 59 assertEquals(120, loop.invoke(5)); 60 } 61 62 @Test 63 public static void testLoopFacNullInit() throws Throwable { 64 // null initializer for counter, should initialize to 0 65 MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc}; 66 MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}; 67 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause); 68 assertEquals(Fac.MT_fac, loop.type()); 69 assertEquals(120, loop.invoke(5)); 70 } 71 72 @Test 73 public static void testLoopNullInit() throws Throwable { 74 // null initializer for counter, should initialize to 0, one-clause loop 75 MethodHandle[] counterClause = new MethodHandle[]{null, Loop.MH_inc, Loop.MH_pred, Loop.MH_fin}; 76 MethodHandle loop = MethodHandles.loop(counterClause); 77 assertEquals(Loop.MT_loop, loop.type()); 78 assertEquals(10, loop.invoke(10)); 79 } 80 81 @Test 82 public static void testLoopVoid1() throws Throwable { 83 // construct a post-checked loop that only does one iteration and has a void body and void local state 84 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null}); 85 assertEquals(MethodType.methodType(void.class), loop.type()); 86 loop.invoke(); 87 } 88 89 @Test 90 public static void testLoopVoid2() throws Throwable { 91 // construct a post-checked loop that only does one iteration and has a void body and void local state, 92 // initialized implicitly from the step type 93 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, null}); 94 assertEquals(MethodType.methodType(void.class), loop.type()); 95 loop.invoke(); 96 } 97 98 @Test 99 public static void testLoopVoid3() throws Throwable { 100 // construct a post-checked loop that only does one iteration and has a void body and void local state, 101 // and that has a void finalizer 102 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_f}); 103 assertEquals(MethodType.methodType(void.class), loop.type()); 104 loop.invoke(); 105 } 106 107 @Test 108 public static void testLoopFacWithVoidState() throws Throwable { 109 // like testLoopFac, but with additional void state that outputs a dot 110 MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc}; 111 MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}; 112 MethodHandle[] dotClause = new MethodHandle[]{null, Fac.MH_dot}; 113 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause, dotClause); 114 assertEquals(Fac.MT_fac, loop.type()); 115 assertEquals(120, loop.invoke(5)); 116 } 117 118 @Test 119 public static void testLoopVoidInt() throws Throwable { 120 // construct a post-checked loop that only does one iteration and has a void body and void local state, 121 // and that returns a constant 122 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_c}); 123 assertEquals(MethodType.methodType(int.class), loop.type()); 124 assertEquals(23, loop.invoke()); 125 } 126 127 @Test 128 public static void testLoopWithVirtuals() throws Throwable { 129 // construct a loop (to calculate factorial) that uses a mix of static and virtual methods 130 MethodHandle[] counterClause = new MethodHandle[]{null, LoopWithVirtuals.permute(LoopWithVirtuals.MH_inc)}; 131 MethodHandle[] accumulatorClause = new MethodHandle[]{ 132 // init function must indicate the loop arguments (there is no other means to determine them) 133 MethodHandles.dropArguments(LoopWithVirtuals.MH_one, 0, LoopWithVirtuals.class), 134 LoopWithVirtuals.permute(LoopWithVirtuals.MH_mult), 135 LoopWithVirtuals.permute(LoopWithVirtuals.MH_pred), 136 LoopWithVirtuals.permute(LoopWithVirtuals.MH_fin) 137 }; 138 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause); 139 assertEquals(LoopWithVirtuals.MT_loop, loop.type()); 140 assertEquals(120, loop.invoke(new LoopWithVirtuals(), 5)); 141 } 142 143 @DataProvider 144 static Object[][] negativeTestData() { 145 MethodHandle i0 = MethodHandles.constant(int.class, 0); 146 MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class); 147 MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class); 148 MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class); 149 List<MethodHandle> inits = Arrays.asList(ii, id, i3); 150 List<Class<?>> ints = Arrays.asList(int.class, int.class, int.class); 151 List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step); 152 List<MethodHandle> preds1 = Arrays.asList(null, null, null); 153 List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null); 154 MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class); 155 List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot); 156 List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null); 157 List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null); 158 List<MethodHandle> lvsteps = Arrays.asList(LoopWithVirtuals.MH_inc, LoopWithVirtuals.MH_mult); 159 List<MethodHandle> lvpreds = Arrays.asList(null, LoopWithVirtuals.MH_pred); 160 List<MethodHandle> lvfinis = Arrays.asList(null, LoopWithVirtuals.MH_fin); 161 return new Object[][] { 162 {null, "null or no clauses passed"}, 163 {new MethodHandle[][]{}, "null or no clauses passed"}, 164 {new MethodHandle[][]{{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}}, 165 "All loop clauses must be represented as MethodHandle arrays with at most 4 elements."}, 166 {new MethodHandle[][]{{null, Fac.MH_inc}, null}, "null clauses are not allowed"}, 167 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_dot}}, 168 "clause 0: init and step return types must match: int != void"}, 169 {new MethodHandle[][]{{ii}, {id}, {i3}}, 170 "found non-effectively identical init parameter type lists: " + inits + 171 " (common suffix: " + ints + ")"}, 172 {new MethodHandle[][]{{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc}, 173 {null, Counted.MH_start, null, Counted.MH_step}}, 174 "found non-identical finalizer return types: " + finis + " (return type: int)"}, 175 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin}, 176 {null, Fac.MH_dot}}, "no predicate found: " + preds1}, 177 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin}, 178 {null, Fac.MH_dot}}, "predicates must have boolean return type: " + preds2}, 179 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin}, 180 {null, Fac.MH_dot}}, 181 "found non-effectively identical parameter type lists:\nstep: " + nesteps + 182 "\npred: " + nepreds + "\nfini: " + nefinis + " (common parameter sequence: " + ints + ")"}, 183 {new MethodHandle[][]{{null, LoopWithVirtuals.MH_inc}, 184 {LoopWithVirtuals.MH_one, LoopWithVirtuals.MH_mult, LoopWithVirtuals.MH_pred, LoopWithVirtuals.MH_fin}}, 185 "found non-effectively identical parameter type lists:\nstep: " + lvsteps + 186 "\npred: " + lvpreds + "\nfini: " + lvfinis + " (common parameter sequence: " + ints + ")"} 187 }; 188 } 189 190 static final MethodHandle MH_loop; 191 192 static { 193 try { 194 MH_loop = LOOKUP.findStatic(MethodHandles.class, "loop", methodType(MethodHandle.class, MethodHandle[][].class)); 195 } catch (NoSuchMethodException | IllegalAccessException e) { 196 throw new ExceptionInInitializerError(e); 197 } 198 } 199 200 @Test(dataProvider = "negativeTestData") 201 public static void testLoopNegative(MethodHandle[][] clauses, String expectedMessage) throws Throwable { 202 boolean caught = false; 203 try { 204 MH_loop.invokeWithArguments(clauses); 205 } catch (IllegalArgumentException iae) { 206 assertEquals(expectedMessage, iae.getMessage()); 207 caught = true; 208 } 209 assertTrue(caught); 210 } 211 212 @Test 213 public static void testWhileLoop() throws Throwable { 214 // int i = 0; while (i < limit) { ++i; } return i; => limit 215 MethodHandle loop = MethodHandles.whileLoop(While.MH_zero, While.MH_pred, While.MH_step); 216 assertEquals(While.MT_while, loop.type()); 217 assertEquals(23, loop.invoke(23)); 218 } 219 220 @Test 221 public static void testWhileLoopNoIteration() throws Throwable { 222 // a while loop that never executes its body because the predicate evaluates to false immediately 223 MethodHandle loop = MethodHandles.whileLoop(While.MH_initString, While.MH_predString, While.MH_stepString); 224 assertEquals(While.MT_string, loop.type()); 225 assertEquals("a", loop.invoke()); 226 } 227 228 @Test 229 public static void testDoWhileLoop() throws Throwable { 230 // int i = 0; do { ++i; } while (i < limit); return i; => limit 231 MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zero, While.MH_step, While.MH_pred); 232 assertEquals(While.MT_while, loop.type()); 233 assertEquals(23, loop.invoke(23)); 234 } 235 236 @Test 237 public static void testDoWhileNullInit() throws Throwable { 238 While w = new While(); 239 MethodHandle loop = MethodHandles.doWhileLoop(null, While.MH_voidBody.bindTo(w), While.MH_voidPred.bindTo(w)); 240 assertEquals(While.MT_void, loop.type()); 241 loop.invoke(5); 242 } 243 244 @Test 245 public static void testWhileZip() throws Throwable { 246 MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred); 247 assertEquals(While.MT_zip, loop.type()); 248 List<String> a = Arrays.asList("a", "b", "c", "d"); 249 List<String> b = Arrays.asList("e", "f", "g", "h"); 250 List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h"); 251 assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator())); 252 } 253 254 @Test 255 public static void testWhileNullInit() throws Throwable { 256 While w = new While(); 257 MethodHandle loop = MethodHandles.whileLoop(null, While.MH_voidPred.bindTo(w), While.MH_voidBody.bindTo(w)); 258 assertEquals(While.MT_void, loop.type()); 259 loop.invoke(5); 260 } 261 262 @Test 263 public static void testCountedLoop() throws Throwable { 264 // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme 265 MethodHandle fit13 = MethodHandles.constant(int.class, 13); 266 MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step); 267 assertEquals(Counted.MT_counted, loop.type()); 268 assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!")); 269 } 270 271 @Test 272 public static void testCountedArrayLoop() throws Throwable { 273 // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13 274 MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class); 275 MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray); 276 assertEquals(Counted.MT_arrayCounted, loop.type()); 277 int[] a = new int[]{0}; 278 loop.invoke(a); 279 assertEquals(13, a[0]); 280 } 281 282 @Test 283 public static void testCountedPrintingLoop() throws Throwable { 284 MethodHandle fit5 = MethodHandles.constant(int.class, 5); 285 MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello); 286 assertEquals(Counted.MT_countedPrinting, loop.type()); 287 loop.invoke(); 288 } 289 290 @Test 291 public static void testCountedRangeLoop() throws Throwable { 292 // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme 293 MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class); 294 MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class); 295 MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step); 296 assertEquals(Counted.MT_counted, loop.type()); 297 assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!")); 298 } 299 300 @Test 301 public static void testIterateSum() throws Throwable { 302 // Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21 303 MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep); 304 assertEquals(Iterate.MT_sum, loop.type()); 305 assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6})); 306 } 307 308 @Test 309 public static void testIterateReverse() throws Throwable { 310 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_reverseInit, Iterate.MH_reverseStep); 311 assertEquals(Iterate.MT_reverse, loop.type()); 312 List<String> list = Arrays.asList("a", "b", "c", "d", "e"); 313 List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a"); 314 assertEquals(reversedList, (List<String>) loop.invoke(list)); 315 } 316 317 @Test 318 public static void testIterateLength() throws Throwable { 319 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_lengthInit, Iterate.MH_lengthStep); 320 assertEquals(Iterate.MT_length, loop.type()); 321 List<Double> list = Arrays.asList(23.0, 148.0, 42.0); 322 assertEquals(list.size(), (int) loop.invoke(list)); 323 } 324 325 @Test 326 public static void testIterateMap() throws Throwable { 327 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_mapInit, Iterate.MH_mapStep); 328 assertEquals(Iterate.MT_map, loop.type()); 329 List<String> list = Arrays.asList("Hello", "world", "!"); 330 List<String> upList = Arrays.asList("HELLO", "WORLD", "!"); 331 assertEquals(upList, (List<String>) loop.invoke(list)); 332 } 333 334 @Test 335 public static void testIteratePrint() throws Throwable { 336 MethodHandle loop = MethodHandles.iteratedLoop(null, null, Iterate.MH_printStep); 337 assertEquals(Iterate.MT_print, loop.type()); 338 loop.invoke(Arrays.asList("hello", "world")); 339 } 340 341 @Test 342 public static void testIterateNullBody() { 343 boolean caught = false; 344 try { 345 MethodHandles.iteratedLoop(MethodHandles.identity(int.class), MethodHandles.identity(int.class), null); 346 } catch (IllegalArgumentException iae) { 347 assertEquals("iterated loop body must not be null", iae.getMessage()); 348 caught = true; 349 } 350 assertTrue(caught); 351 } 352 353 static class Empty { 354 355 static void f() { } 356 357 static boolean pred() { 358 return false; 359 } 360 361 static int c() { 362 return 23; 363 } 364 365 static final Class<Empty> EMPTY = Empty.class; 366 367 static final MethodType MT_f = methodType(void.class); 368 static final MethodType MT_pred = methodType(boolean.class); 369 static final MethodType MT_c = methodType(int.class); 370 371 static final MethodHandle MH_f; 372 static final MethodHandle MH_pred; 373 static final MethodHandle MH_c; 374 375 static { 376 try { 377 MH_f = LOOKUP.findStatic(EMPTY, "f", MT_f); 378 MH_pred = LOOKUP.findStatic(EMPTY, "pred", MT_pred); 379 MH_c = LOOKUP.findStatic(EMPTY, "c", MT_c); 380 } catch (Exception e) { 381 throw new ExceptionInInitializerError(e); 382 } 383 } 384 } 385 386 static class Fac { 387 388 static int zero(int k) { 389 return 0; 390 } 391 392 static int one(int k) { 393 return 1; 394 } 395 396 static boolean pred(int i, int acc, int k) { 397 return i < k; 398 } 399 400 static int inc(int i, int acc, int k) { 401 return i + 1; 402 } 403 404 static int mult(int i, int acc, int k) { 405 return i * acc; 406 } 407 408 static void dot(int i, int acc, int k) { 409 System.out.print('.'); 410 } 411 412 static int fin(int i, int acc, int k) { 413 return acc; 414 } 415 416 static final Class<Fac> FAC = Fac.class; 417 418 static final MethodType MT_init = methodType(int.class, int.class); 419 static final MethodType MT_fn = methodType(int.class, int.class, int.class, int.class); 420 static final MethodType MT_dot = methodType(void.class, int.class, int.class, int.class); 421 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class); 422 423 static final MethodHandle MH_zero; 424 static final MethodHandle MH_one; 425 static final MethodHandle MH_pred; 426 static final MethodHandle MH_inc; 427 static final MethodHandle MH_mult; 428 static final MethodHandle MH_dot; 429 static final MethodHandle MH_fin; 430 431 static final MethodType MT_fac = methodType(int.class, int.class); 432 433 static { 434 try { 435 MH_zero = LOOKUP.findStatic(FAC, "zero", MT_init); 436 MH_one = LOOKUP.findStatic(FAC, "one", MT_init); 437 MH_pred = LOOKUP.findStatic(FAC, "pred", MT_pred); 438 MH_inc = LOOKUP.findStatic(FAC, "inc", MT_fn); 439 MH_mult = LOOKUP.findStatic(FAC, "mult", MT_fn); 440 MH_dot = LOOKUP.findStatic(FAC, "dot", MT_dot); 441 MH_fin = LOOKUP.findStatic(FAC, "fin", MT_fn); 442 } catch (Exception e) { 443 throw new ExceptionInInitializerError(e); 444 } 445 } 446 447 } 448 449 static class Loop { 450 451 static int inc(int i, int k) { 452 return i + 1; 453 } 454 455 static boolean pred(int i, int k) { 456 return i < k; 457 } 458 459 static int fin(int i, int k) { 460 return k; 461 } 462 463 static final Class<Loop> LOOP = Loop.class; 464 465 static final MethodType MT_inc = methodType(int.class, int.class, int.class); 466 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class); 467 static final MethodType MT_fin = methodType(int.class, int.class, int.class); 468 469 static final MethodHandle MH_inc; 470 static final MethodHandle MH_pred; 471 static final MethodHandle MH_fin; 472 473 static final MethodType MT_loop = methodType(int.class, int.class); 474 475 static { 476 try { 477 MH_inc = LOOKUP.findStatic(LOOP, "inc", MT_inc); 478 MH_pred = LOOKUP.findStatic(LOOP, "pred", MT_pred); 479 MH_fin = LOOKUP.findStatic(LOOP, "fin", MT_fin); 480 } catch (Exception e) { 481 throw new ExceptionInInitializerError(e); 482 } 483 } 484 485 } 486 487 static class LoopWithVirtuals { 488 489 static int one(int k) { 490 return 1; 491 } 492 493 int inc(int i, int acc, int k) { 494 return i + 1; 495 } 496 497 int mult(int i, int acc, int k) { 498 return i * acc; 499 } 500 501 boolean pred(int i, int acc, int k) { 502 return i < k; 503 } 504 505 int fin(int i, int acc, int k) { 506 return acc; 507 } 508 509 static final Class<LoopWithVirtuals> LOOP_WITH_VIRTUALS = LoopWithVirtuals.class; 510 511 static final MethodType MT_one = methodType(int.class, int.class); 512 static final MethodType MT_inc = methodType(int.class, int.class, int.class, int.class); 513 static final MethodType MT_mult = methodType(int.class, int.class, int.class, int.class); 514 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class); 515 static final MethodType MT_fin = methodType(int.class, int.class, int.class, int.class); 516 517 static final MethodHandle MH_one; 518 static final MethodHandle MH_inc; 519 static final MethodHandle MH_mult; 520 static final MethodHandle MH_pred; 521 static final MethodHandle MH_fin; 522 523 static final MethodType MT_loop = methodType(int.class, LOOP_WITH_VIRTUALS, int.class); 524 525 static { 526 try { 527 MH_one = LOOKUP.findStatic(LOOP_WITH_VIRTUALS, "one", MT_one); 528 MH_inc = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "inc", MT_inc); 529 MH_mult = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "mult", MT_mult); 530 MH_pred = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "pred", MT_pred); 531 MH_fin = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "fin", MT_fin); 532 } catch (Exception e) { 533 throw new ExceptionInInitializerError(e); 534 } 535 } 536 537 static MethodHandle permute(MethodHandle h) { 538 // The handles representing virtual methods need to be rearranged to match the required order of arguments 539 // (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is 540 // a loop argument, it must be moved to the appropriate position in the signature. 541 return MethodHandles.permuteArguments(h, 542 methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3); 543 } 544 545 } 546 547 static class While { 548 549 static int zero(int limit) { 550 return 0; 551 } 552 553 static boolean pred(int i, int limit) { 554 return i < limit; 555 } 556 557 static int step(int i, int limit) { 558 return i + 1; 559 } 560 561 static String initString() { 562 return "a"; 563 } 564 565 static boolean predString(String s) { 566 return s.length() != 1; 567 } 568 569 static String stepString(String s) { 570 return s + "a"; 571 } 572 573 static List<String> zipInitZip(Iterator<String> a, Iterator<String> b) { 574 return new ArrayList<>(); 575 } 576 577 static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) { 578 return a.hasNext() && b.hasNext(); 579 } 580 581 static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) { 582 zip.add(a.next()); 583 zip.add(b.next()); 584 return zip; 585 } 586 587 private int i = 0; 588 589 void voidBody(int k) { 590 ++i; 591 } 592 593 boolean voidPred(int k) { 594 return i < k; 595 } 596 597 static final Class<While> WHILE = While.class; 598 599 static final MethodType MT_zero = methodType(int.class, int.class); 600 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class); 601 static final MethodType MT_fn = methodType(int.class, int.class, int.class); 602 static final MethodType MT_initString = methodType(String.class); 603 static final MethodType MT_predString = methodType(boolean.class, String.class); 604 static final MethodType MT_stepString = methodType(String.class, String.class); 605 static final MethodType MT_zipInitZip = methodType(List.class, Iterator.class, Iterator.class); 606 static final MethodType MT_zipPred = methodType(boolean.class, List.class, Iterator.class, Iterator.class); 607 static final MethodType MT_zipStep = methodType(List.class, List.class, Iterator.class, Iterator.class); 608 static final MethodType MT_voidBody = methodType(void.class, int.class); 609 static final MethodType MT_voidPred = methodType(boolean.class, int.class); 610 611 static final MethodHandle MH_zero; 612 static final MethodHandle MH_pred; 613 static final MethodHandle MH_step; 614 static final MethodHandle MH_initString; 615 static final MethodHandle MH_predString; 616 static final MethodHandle MH_stepString; 617 static final MethodHandle MH_zipInitZip; 618 static final MethodHandle MH_zipPred; 619 static final MethodHandle MH_zipStep; 620 static final MethodHandle MH_voidBody; 621 static final MethodHandle MH_voidPred; 622 623 static final MethodType MT_while = methodType(int.class, int.class); 624 static final MethodType MT_string = methodType(String.class); 625 static final MethodType MT_zip = methodType(List.class, Iterator.class, Iterator.class); 626 static final MethodType MT_void = methodType(void.class, int.class); 627 628 static { 629 try { 630 MH_zero = LOOKUP.findStatic(WHILE, "zero", MT_zero); 631 MH_pred = LOOKUP.findStatic(WHILE, "pred", MT_pred); 632 MH_step = LOOKUP.findStatic(WHILE, "step", MT_fn); 633 MH_initString = LOOKUP.findStatic(WHILE, "initString", MT_initString); 634 MH_predString = LOOKUP.findStatic(WHILE, "predString", MT_predString); 635 MH_stepString = LOOKUP.findStatic(WHILE, "stepString", MT_stepString); 636 MH_zipInitZip = LOOKUP.findStatic(WHILE, "zipInitZip", MT_zipInitZip); 637 MH_zipPred = LOOKUP.findStatic(WHILE, "zipPred", MT_zipPred); 638 MH_zipStep = LOOKUP.findStatic(WHILE, "zipStep", MT_zipStep); 639 MH_voidBody = LOOKUP.findVirtual(WHILE, "voidBody", MT_voidBody); 640 MH_voidPred = LOOKUP.findVirtual(WHILE, "voidPred", MT_voidPred); 641 } catch (Exception e) { 642 throw new ExceptionInInitializerError(e); 643 } 644 } 645 646 } 647 648 static class Counted { 649 650 static String start(String arg) { 651 return arg; 652 } 653 654 static String step(int counter, String v, String arg) { 655 return "na " + v; 656 } 657 658 static void stepUpdateArray(int counter, int[] a) { 659 ++a[0]; 660 } 661 662 static void printHello(int counter) { 663 System.out.print("hello"); 664 } 665 666 static final Class<Counted> COUNTED = Counted.class; 667 668 static final MethodType MT_start = methodType(String.class, String.class); 669 static final MethodType MT_step = methodType(String.class, int.class, String.class, String.class); 670 static final MethodType MT_stepUpdateArray = methodType(void.class, int.class, int[].class); 671 static final MethodType MT_printHello = methodType(void.class, int.class); 672 673 static final MethodHandle MH_13; 674 static final MethodHandle MH_m5; 675 static final MethodHandle MH_8; 676 static final MethodHandle MH_start; 677 static final MethodHandle MH_step; 678 static final MethodHandle MH_stepUpdateArray; 679 static final MethodHandle MH_printHello; 680 681 static final MethodType MT_counted = methodType(String.class, String.class); 682 static final MethodType MT_arrayCounted = methodType(void.class, int[].class); 683 static final MethodType MT_countedPrinting = methodType(void.class); 684 685 static { 686 try { 687 MH_13 = MethodHandles.constant(int.class, 13); 688 MH_m5 = MethodHandles.constant(int.class, -5); 689 MH_8 = MethodHandles.constant(int.class, 8); 690 MH_start = LOOKUP.findStatic(COUNTED, "start", MT_start); 691 MH_step = LOOKUP.findStatic(COUNTED, "step", MT_step); 692 MH_stepUpdateArray = LOOKUP.findStatic(COUNTED, "stepUpdateArray", MT_stepUpdateArray); 693 MH_printHello = LOOKUP.findStatic(COUNTED, "printHello", MT_printHello); 694 } catch (Exception e) { 695 throw new ExceptionInInitializerError(e); 696 } 697 } 698 699 } 700 701 static class Iterate { 702 703 static Iterator<Integer> sumIterator(Integer[] a) { 704 return Arrays.asList(a).iterator(); 705 } 706 707 static int sumInit(Integer[] a) { 708 return 0; 709 } 710 711 static int sumStep(int s, int e, Integer[] a) { 712 return s + e; 713 } 714 715 static List<String> reverseInit(List<String> l) { 716 return new ArrayList<>(); 717 } 718 719 static List<String> reverseStep(String e, List<String> r, List<String> l) { 720 r.add(0, e); 721 return r; 722 } 723 724 static int lengthInit(List<Double> l) { 725 return 0; 726 } 727 728 static int lengthStep(Object o, int len, List<Double> l) { 729 return len + 1; 730 } 731 732 static List<String> mapInit(List<String> l) { 733 return new ArrayList<>(); 734 } 735 736 static List<String> mapStep(String e, List<String> r, List<String> l) { 737 r.add(e.toUpperCase()); 738 return r; 739 } 740 741 static void printStep(String s, List<String> l) { 742 System.out.print(s); 743 } 744 745 static final Class<Iterate> ITERATE = Iterate.class; 746 747 static final MethodType MT_sumIterator = methodType(Iterator.class, Integer[].class); 748 749 static final MethodType MT_sumInit = methodType(int.class, Integer[].class); 750 static final MethodType MT_reverseInit = methodType(List.class, List.class); 751 static final MethodType MT_lenghInit = methodType(int.class, List.class); 752 static final MethodType MT_mapInit = methodType(List.class, List.class); 753 754 static final MethodType MT_sumStep = methodType(int.class, int.class, int.class, Integer[].class); 755 static final MethodType MT_reverseStep = methodType(List.class, String.class, List.class, List.class); 756 static final MethodType MT_lengthStep = methodType(int.class, Object.class, int.class, List.class); 757 static final MethodType MT_mapStep = methodType(List.class, String.class, List.class, List.class); 758 static final MethodType MT_printStep = methodType(void.class, String.class, List.class); 759 760 static final MethodHandle MH_sumIterator; 761 static final MethodHandle MH_sumInit; 762 static final MethodHandle MH_sumStep; 763 static final MethodHandle MH_printStep; 764 765 static final MethodHandle MH_reverseInit; 766 static final MethodHandle MH_reverseStep; 767 768 static final MethodHandle MH_lengthInit; 769 static final MethodHandle MH_lengthStep; 770 771 static final MethodHandle MH_mapInit; 772 static final MethodHandle MH_mapStep; 773 774 static final MethodType MT_sum = methodType(int.class, Integer[].class); 775 static final MethodType MT_reverse = methodType(List.class, List.class); 776 static final MethodType MT_length = methodType(int.class, List.class); 777 static final MethodType MT_map = methodType(List.class, List.class); 778 static final MethodType MT_print = methodType(void.class, List.class); 779 780 static { 781 try { 782 MH_sumIterator = LOOKUP.findStatic(ITERATE, "sumIterator", MT_sumIterator); 783 MH_sumInit = LOOKUP.findStatic(ITERATE, "sumInit", MT_sumInit); 784 MH_sumStep = LOOKUP.findStatic(ITERATE, "sumStep", MT_sumStep); 785 MH_reverseInit = LOOKUP.findStatic(ITERATE, "reverseInit", MT_reverseInit); 786 MH_reverseStep = LOOKUP.findStatic(ITERATE, "reverseStep", MT_reverseStep); 787 MH_lengthInit = LOOKUP.findStatic(ITERATE, "lengthInit", MT_lenghInit); 788 MH_lengthStep = LOOKUP.findStatic(ITERATE, "lengthStep", MT_lengthStep); 789 MH_mapInit = LOOKUP.findStatic(ITERATE, "mapInit", MT_mapInit); 790 MH_mapStep = LOOKUP.findStatic(ITERATE, "mapStep", MT_mapStep); 791 MH_printStep = LOOKUP.findStatic(ITERATE, "printStep", MT_printStep); 792 } catch (Exception e) { 793 throw new ExceptionInInitializerError(e); 794 } 795 } 796 797 } 798 799 }