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 int v = 5; 240 MethodHandle loop = MethodHandles.doWhileLoop(null, While.MH_voidBody.bindTo(w), While.MH_voidPred.bindTo(w)); 241 assertEquals(While.MT_void, loop.type()); 242 loop.invoke(v); 243 assertEquals(v, w.i); 244 } 245 246 @Test 247 public static void testWhileZip() throws Throwable { 248 MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred); 249 assertEquals(While.MT_zip, loop.type()); 250 List<String> a = Arrays.asList("a", "b", "c", "d"); 251 List<String> b = Arrays.asList("e", "f", "g", "h"); 252 List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h"); 253 assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator())); 254 } 255 256 @Test 257 public static void testWhileNullInit() throws Throwable { 258 While w = new While(); 259 int v = 5; 260 MethodHandle loop = MethodHandles.whileLoop(null, While.MH_voidPred.bindTo(w), While.MH_voidBody.bindTo(w)); 261 assertEquals(While.MT_void, loop.type()); 262 loop.invoke(v); 263 assertEquals(v, w.i); 264 } 265 266 @Test 267 public static void testCountedLoop() throws Throwable { 268 // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme 269 MethodHandle fit13 = MethodHandles.constant(int.class, 13); 270 MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step); 271 assertEquals(Counted.MT_counted, loop.type()); 272 assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!")); 273 } 274 275 @Test 276 public static void testCountedArrayLoop() throws Throwable { 277 // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13 278 MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class); 279 MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray); 280 assertEquals(Counted.MT_arrayCounted, loop.type()); 281 int[] a = new int[]{0}; 282 loop.invoke(a); 283 assertEquals(13, a[0]); 284 } 285 286 @Test 287 public static void testCountedPrintingLoop() throws Throwable { 288 MethodHandle fit5 = MethodHandles.constant(int.class, 5); 289 MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello); 290 assertEquals(Counted.MT_countedPrinting, loop.type()); 291 loop.invoke(); 292 } 293 294 @Test 295 public static void testCountedRangeLoop() throws Throwable { 296 // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme 297 MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class); 298 MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class); 299 MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step); 300 assertEquals(Counted.MT_counted, loop.type()); 301 assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!")); 302 } 303 304 @Test 305 public static void testIterateSum() throws Throwable { 306 // Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21 307 MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep); 308 assertEquals(Iterate.MT_sum, loop.type()); 309 assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6})); 310 } 311 312 @Test 313 public static void testIterateReverse() throws Throwable { 314 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_reverseInit, Iterate.MH_reverseStep); 315 assertEquals(Iterate.MT_reverse, loop.type()); 316 List<String> list = Arrays.asList("a", "b", "c", "d", "e"); 317 List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a"); 318 assertEquals(reversedList, (List<String>) loop.invoke(list)); 319 } 320 321 @Test 322 public static void testIterateLength() throws Throwable { 323 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_lengthInit, Iterate.MH_lengthStep); 324 assertEquals(Iterate.MT_length, loop.type()); 325 List<Double> list = Arrays.asList(23.0, 148.0, 42.0); 326 assertEquals(list.size(), (int) loop.invoke(list)); 327 } 328 329 @Test 330 public static void testIterateMap() throws Throwable { 331 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_mapInit, Iterate.MH_mapStep); 332 assertEquals(Iterate.MT_map, loop.type()); 333 List<String> list = Arrays.asList("Hello", "world", "!"); 334 List<String> upList = Arrays.asList("HELLO", "WORLD", "!"); 335 assertEquals(upList, (List<String>) loop.invoke(list)); 336 } 337 338 @Test 339 public static void testIteratePrint() throws Throwable { 340 MethodHandle loop = MethodHandles.iteratedLoop(null, null, Iterate.MH_printStep); 341 assertEquals(Iterate.MT_print, loop.type()); 342 loop.invoke(Arrays.asList("hello", "world")); 343 } 344 345 @Test 346 public static void testIterateNullBody() { 347 boolean caught = false; 348 try { 349 MethodHandles.iteratedLoop(MethodHandles.identity(int.class), MethodHandles.identity(int.class), null); 350 } catch (IllegalArgumentException iae) { 351 assertEquals("iterated loop body must not be null", iae.getMessage()); 352 caught = true; 353 } 354 assertTrue(caught); 355 } 356 357 static class Empty { 358 359 static void f() { } 360 361 static boolean pred() { 362 return false; 363 } 364 365 static int c() { 366 return 23; 367 } 368 369 static final Class<Empty> EMPTY = Empty.class; 370 371 static final MethodType MT_f = methodType(void.class); 372 static final MethodType MT_pred = methodType(boolean.class); 373 static final MethodType MT_c = methodType(int.class); 374 375 static final MethodHandle MH_f; 376 static final MethodHandle MH_pred; 377 static final MethodHandle MH_c; 378 379 static { 380 try { 381 MH_f = LOOKUP.findStatic(EMPTY, "f", MT_f); 382 MH_pred = LOOKUP.findStatic(EMPTY, "pred", MT_pred); 383 MH_c = LOOKUP.findStatic(EMPTY, "c", MT_c); 384 } catch (Exception e) { 385 throw new ExceptionInInitializerError(e); 386 } 387 } 388 } 389 390 static class Fac { 391 392 static int zero(int k) { 393 return 0; 394 } 395 396 static int one(int k) { 397 return 1; 398 } 399 400 static boolean pred(int i, int acc, int k) { 401 return i < k; 402 } 403 404 static int inc(int i, int acc, int k) { 405 return i + 1; 406 } 407 408 static int mult(int i, int acc, int k) { 409 return i * acc; 410 } 411 412 static void dot(int i, int acc, int k) { 413 System.out.print('.'); 414 } 415 416 static int fin(int i, int acc, int k) { 417 return acc; 418 } 419 420 static final Class<Fac> FAC = Fac.class; 421 422 static final MethodType MT_init = methodType(int.class, int.class); 423 static final MethodType MT_fn = methodType(int.class, int.class, int.class, int.class); 424 static final MethodType MT_dot = methodType(void.class, int.class, int.class, int.class); 425 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class); 426 427 static final MethodHandle MH_zero; 428 static final MethodHandle MH_one; 429 static final MethodHandle MH_pred; 430 static final MethodHandle MH_inc; 431 static final MethodHandle MH_mult; 432 static final MethodHandle MH_dot; 433 static final MethodHandle MH_fin; 434 435 static final MethodType MT_fac = methodType(int.class, int.class); 436 437 static { 438 try { 439 MH_zero = LOOKUP.findStatic(FAC, "zero", MT_init); 440 MH_one = LOOKUP.findStatic(FAC, "one", MT_init); 441 MH_pred = LOOKUP.findStatic(FAC, "pred", MT_pred); 442 MH_inc = LOOKUP.findStatic(FAC, "inc", MT_fn); 443 MH_mult = LOOKUP.findStatic(FAC, "mult", MT_fn); 444 MH_dot = LOOKUP.findStatic(FAC, "dot", MT_dot); 445 MH_fin = LOOKUP.findStatic(FAC, "fin", MT_fn); 446 } catch (Exception e) { 447 throw new ExceptionInInitializerError(e); 448 } 449 } 450 451 } 452 453 static class Loop { 454 455 static int inc(int i, int k) { 456 return i + 1; 457 } 458 459 static boolean pred(int i, int k) { 460 return i < k; 461 } 462 463 static int fin(int i, int k) { 464 return k; 465 } 466 467 static final Class<Loop> LOOP = Loop.class; 468 469 static final MethodType MT_inc = methodType(int.class, int.class, int.class); 470 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class); 471 static final MethodType MT_fin = methodType(int.class, int.class, int.class); 472 473 static final MethodHandle MH_inc; 474 static final MethodHandle MH_pred; 475 static final MethodHandle MH_fin; 476 477 static final MethodType MT_loop = methodType(int.class, int.class); 478 479 static { 480 try { 481 MH_inc = LOOKUP.findStatic(LOOP, "inc", MT_inc); 482 MH_pred = LOOKUP.findStatic(LOOP, "pred", MT_pred); 483 MH_fin = LOOKUP.findStatic(LOOP, "fin", MT_fin); 484 } catch (Exception e) { 485 throw new ExceptionInInitializerError(e); 486 } 487 } 488 489 } 490 491 static class LoopWithVirtuals { 492 493 static int one(int k) { 494 return 1; 495 } 496 497 int inc(int i, int acc, int k) { 498 return i + 1; 499 } 500 501 int mult(int i, int acc, int k) { 502 return i * acc; 503 } 504 505 boolean pred(int i, int acc, int k) { 506 return i < k; 507 } 508 509 int fin(int i, int acc, int k) { 510 return acc; 511 } 512 513 static final Class<LoopWithVirtuals> LOOP_WITH_VIRTUALS = LoopWithVirtuals.class; 514 515 static final MethodType MT_one = methodType(int.class, int.class); 516 static final MethodType MT_inc = methodType(int.class, int.class, int.class, int.class); 517 static final MethodType MT_mult = methodType(int.class, int.class, int.class, int.class); 518 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class); 519 static final MethodType MT_fin = methodType(int.class, int.class, int.class, int.class); 520 521 static final MethodHandle MH_one; 522 static final MethodHandle MH_inc; 523 static final MethodHandle MH_mult; 524 static final MethodHandle MH_pred; 525 static final MethodHandle MH_fin; 526 527 static final MethodType MT_loop = methodType(int.class, LOOP_WITH_VIRTUALS, int.class); 528 529 static { 530 try { 531 MH_one = LOOKUP.findStatic(LOOP_WITH_VIRTUALS, "one", MT_one); 532 MH_inc = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "inc", MT_inc); 533 MH_mult = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "mult", MT_mult); 534 MH_pred = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "pred", MT_pred); 535 MH_fin = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "fin", MT_fin); 536 } catch (Exception e) { 537 throw new ExceptionInInitializerError(e); 538 } 539 } 540 541 static MethodHandle permute(MethodHandle h) { 542 // The handles representing virtual methods need to be rearranged to match the required order of arguments 543 // (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is 544 // a loop argument, it must be moved to the appropriate position in the signature. 545 return MethodHandles.permuteArguments(h, 546 methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3); 547 } 548 549 } 550 551 static class While { 552 553 static int zero(int limit) { 554 return 0; 555 } 556 557 static boolean pred(int i, int limit) { 558 return i < limit; 559 } 560 561 static int step(int i, int limit) { 562 return i + 1; 563 } 564 565 static String initString() { 566 return "a"; 567 } 568 569 static boolean predString(String s) { 570 return s.length() != 1; 571 } 572 573 static String stepString(String s) { 574 return s + "a"; 575 } 576 577 static List<String> zipInitZip(Iterator<String> a, Iterator<String> b) { 578 return new ArrayList<>(); 579 } 580 581 static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) { 582 return a.hasNext() && b.hasNext(); 583 } 584 585 static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) { 586 zip.add(a.next()); 587 zip.add(b.next()); 588 return zip; 589 } 590 591 private int i = 0; 592 593 void voidBody(int k) { 594 ++i; 595 } 596 597 boolean voidPred(int k) { 598 return i < k; 599 } 600 601 static final Class<While> WHILE = While.class; 602 603 static final MethodType MT_zero = methodType(int.class, int.class); 604 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class); 605 static final MethodType MT_fn = methodType(int.class, int.class, int.class); 606 static final MethodType MT_initString = methodType(String.class); 607 static final MethodType MT_predString = methodType(boolean.class, String.class); 608 static final MethodType MT_stepString = methodType(String.class, String.class); 609 static final MethodType MT_zipInitZip = methodType(List.class, Iterator.class, Iterator.class); 610 static final MethodType MT_zipPred = methodType(boolean.class, List.class, Iterator.class, Iterator.class); 611 static final MethodType MT_zipStep = methodType(List.class, List.class, Iterator.class, Iterator.class); 612 static final MethodType MT_voidBody = methodType(void.class, int.class); 613 static final MethodType MT_voidPred = methodType(boolean.class, int.class); 614 615 static final MethodHandle MH_zero; 616 static final MethodHandle MH_pred; 617 static final MethodHandle MH_step; 618 static final MethodHandle MH_initString; 619 static final MethodHandle MH_predString; 620 static final MethodHandle MH_stepString; 621 static final MethodHandle MH_zipInitZip; 622 static final MethodHandle MH_zipPred; 623 static final MethodHandle MH_zipStep; 624 static final MethodHandle MH_voidBody; 625 static final MethodHandle MH_voidPred; 626 627 static final MethodType MT_while = methodType(int.class, int.class); 628 static final MethodType MT_string = methodType(String.class); 629 static final MethodType MT_zip = methodType(List.class, Iterator.class, Iterator.class); 630 static final MethodType MT_void = methodType(void.class, int.class); 631 632 static { 633 try { 634 MH_zero = LOOKUP.findStatic(WHILE, "zero", MT_zero); 635 MH_pred = LOOKUP.findStatic(WHILE, "pred", MT_pred); 636 MH_step = LOOKUP.findStatic(WHILE, "step", MT_fn); 637 MH_initString = LOOKUP.findStatic(WHILE, "initString", MT_initString); 638 MH_predString = LOOKUP.findStatic(WHILE, "predString", MT_predString); 639 MH_stepString = LOOKUP.findStatic(WHILE, "stepString", MT_stepString); 640 MH_zipInitZip = LOOKUP.findStatic(WHILE, "zipInitZip", MT_zipInitZip); 641 MH_zipPred = LOOKUP.findStatic(WHILE, "zipPred", MT_zipPred); 642 MH_zipStep = LOOKUP.findStatic(WHILE, "zipStep", MT_zipStep); 643 MH_voidBody = LOOKUP.findVirtual(WHILE, "voidBody", MT_voidBody); 644 MH_voidPred = LOOKUP.findVirtual(WHILE, "voidPred", MT_voidPred); 645 } catch (Exception e) { 646 throw new ExceptionInInitializerError(e); 647 } 648 } 649 650 } 651 652 static class Counted { 653 654 static String start(String arg) { 655 return arg; 656 } 657 658 static String step(int counter, String v, String arg) { 659 return "na " + v; 660 } 661 662 static void stepUpdateArray(int counter, int[] a) { 663 ++a[0]; 664 } 665 666 static void printHello(int counter) { 667 System.out.print("hello"); 668 } 669 670 static final Class<Counted> COUNTED = Counted.class; 671 672 static final MethodType MT_start = methodType(String.class, String.class); 673 static final MethodType MT_step = methodType(String.class, int.class, String.class, String.class); 674 static final MethodType MT_stepUpdateArray = methodType(void.class, int.class, int[].class); 675 static final MethodType MT_printHello = methodType(void.class, int.class); 676 677 static final MethodHandle MH_13; 678 static final MethodHandle MH_m5; 679 static final MethodHandle MH_8; 680 static final MethodHandle MH_start; 681 static final MethodHandle MH_step; 682 static final MethodHandle MH_stepUpdateArray; 683 static final MethodHandle MH_printHello; 684 685 static final MethodType MT_counted = methodType(String.class, String.class); 686 static final MethodType MT_arrayCounted = methodType(void.class, int[].class); 687 static final MethodType MT_countedPrinting = methodType(void.class); 688 689 static { 690 try { 691 MH_13 = MethodHandles.constant(int.class, 13); 692 MH_m5 = MethodHandles.constant(int.class, -5); 693 MH_8 = MethodHandles.constant(int.class, 8); 694 MH_start = LOOKUP.findStatic(COUNTED, "start", MT_start); 695 MH_step = LOOKUP.findStatic(COUNTED, "step", MT_step); 696 MH_stepUpdateArray = LOOKUP.findStatic(COUNTED, "stepUpdateArray", MT_stepUpdateArray); 697 MH_printHello = LOOKUP.findStatic(COUNTED, "printHello", MT_printHello); 698 } catch (Exception e) { 699 throw new ExceptionInInitializerError(e); 700 } 701 } 702 703 } 704 705 static class Iterate { 706 707 static Iterator<Integer> sumIterator(Integer[] a) { 708 return Arrays.asList(a).iterator(); 709 } 710 711 static int sumInit(Integer[] a) { 712 return 0; 713 } 714 715 static int sumStep(int s, int e, Integer[] a) { 716 return s + e; 717 } 718 719 static List<String> reverseInit(List<String> l) { 720 return new ArrayList<>(); 721 } 722 723 static List<String> reverseStep(String e, List<String> r, List<String> l) { 724 r.add(0, e); 725 return r; 726 } 727 728 static int lengthInit(List<Double> l) { 729 return 0; 730 } 731 732 static int lengthStep(Object o, int len, List<Double> l) { 733 return len + 1; 734 } 735 736 static List<String> mapInit(List<String> l) { 737 return new ArrayList<>(); 738 } 739 740 static List<String> mapStep(String e, List<String> r, List<String> l) { 741 r.add(e.toUpperCase()); 742 return r; 743 } 744 745 static void printStep(String s, List<String> l) { 746 System.out.print(s); 747 } 748 749 static final Class<Iterate> ITERATE = Iterate.class; 750 751 static final MethodType MT_sumIterator = methodType(Iterator.class, Integer[].class); 752 753 static final MethodType MT_sumInit = methodType(int.class, Integer[].class); 754 static final MethodType MT_reverseInit = methodType(List.class, List.class); 755 static final MethodType MT_lenghInit = methodType(int.class, List.class); 756 static final MethodType MT_mapInit = methodType(List.class, List.class); 757 758 static final MethodType MT_sumStep = methodType(int.class, int.class, int.class, Integer[].class); 759 static final MethodType MT_reverseStep = methodType(List.class, String.class, List.class, List.class); 760 static final MethodType MT_lengthStep = methodType(int.class, Object.class, int.class, List.class); 761 static final MethodType MT_mapStep = methodType(List.class, String.class, List.class, List.class); 762 static final MethodType MT_printStep = methodType(void.class, String.class, List.class); 763 764 static final MethodHandle MH_sumIterator; 765 static final MethodHandle MH_sumInit; 766 static final MethodHandle MH_sumStep; 767 static final MethodHandle MH_printStep; 768 769 static final MethodHandle MH_reverseInit; 770 static final MethodHandle MH_reverseStep; 771 772 static final MethodHandle MH_lengthInit; 773 static final MethodHandle MH_lengthStep; 774 775 static final MethodHandle MH_mapInit; 776 static final MethodHandle MH_mapStep; 777 778 static final MethodType MT_sum = methodType(int.class, Integer[].class); 779 static final MethodType MT_reverse = methodType(List.class, List.class); 780 static final MethodType MT_length = methodType(int.class, List.class); 781 static final MethodType MT_map = methodType(List.class, List.class); 782 static final MethodType MT_print = methodType(void.class, List.class); 783 784 static { 785 try { 786 MH_sumIterator = LOOKUP.findStatic(ITERATE, "sumIterator", MT_sumIterator); 787 MH_sumInit = LOOKUP.findStatic(ITERATE, "sumInit", MT_sumInit); 788 MH_sumStep = LOOKUP.findStatic(ITERATE, "sumStep", MT_sumStep); 789 MH_reverseInit = LOOKUP.findStatic(ITERATE, "reverseInit", MT_reverseInit); 790 MH_reverseStep = LOOKUP.findStatic(ITERATE, "reverseStep", MT_reverseStep); 791 MH_lengthInit = LOOKUP.findStatic(ITERATE, "lengthInit", MT_lenghInit); 792 MH_lengthStep = LOOKUP.findStatic(ITERATE, "lengthStep", MT_lengthStep); 793 MH_mapInit = LOOKUP.findStatic(ITERATE, "mapInit", MT_mapInit); 794 MH_mapStep = LOOKUP.findStatic(ITERATE, "mapStep", MT_mapStep); 795 MH_printStep = LOOKUP.findStatic(ITERATE, "printStep", MT_printStep); 796 } catch (Exception e) { 797 throw new ExceptionInInitializerError(e); 798 } 799 } 800 801 } 802 803 }