|
23 | 23 | import chipmunk.runtime.CClass; |
24 | 24 | import chipmunk.runtime.CMethod; |
25 | 25 | import chipmunk.runtime.Fiber; |
| 26 | +import chipmunk.vm.invoke.CInvoker; |
26 | 27 | import chipmunk.vm.tree.Node; |
27 | 28 | import chipmunk.vm.tree.nodes.*; |
28 | 29 |
|
| 30 | +import java.lang.reflect.Method; |
29 | 31 | import java.math.BigDecimal; |
| 32 | +import java.util.ArrayDeque; |
| 33 | +import java.util.Comparator; |
30 | 34 | import java.util.concurrent.Callable; |
31 | 35 |
|
32 | 36 | public class TestPrograms { |
@@ -298,4 +302,95 @@ public static Callable<Object> javaCountBigDecimal(){ |
298 | 302 | }; |
299 | 303 | } |
300 | 304 |
|
| 305 | + public static Callable<Object> bytecodeCount(){ |
| 306 | + |
| 307 | + var fiber = new Fiber(); |
| 308 | + |
| 309 | + var stack = new ArrayDeque<>(); |
| 310 | + Object[] locals = new Object[1]; |
| 311 | + |
| 312 | + // Instructions |
| 313 | + // 0x00 - set x |
| 314 | + // 0x01 - push 1000000 |
| 315 | + // 0x02 - get x |
| 316 | + // 0x03 - less than |
| 317 | + // 0x04 - push 1 |
| 318 | + // 0x05 - add |
| 319 | + // 0x06 - goto test start |
| 320 | + |
| 321 | + var program = new byte[]{ |
| 322 | + 0x04, // 1 |
| 323 | + 0x00, // x = 1 |
| 324 | + 0x01, // 1000000 |
| 325 | + 0x02, // x, 1000000 |
| 326 | + 0x03, // x < 1000000 |
| 327 | + 0x04, // 1 |
| 328 | + 0x02, // x, 1 |
| 329 | + 0x05, // x + 1 |
| 330 | + 0x00, // x = x + 1 |
| 331 | + 0x06 // goto ip 2 |
| 332 | + }; |
| 333 | + |
| 334 | + var cache = new Method[]{ |
| 335 | + resolveMethod(fiber, BigDecimal.class, "plus", new Object[]{new BigDecimal(0), new BigDecimal(0)}) |
| 336 | + }; |
| 337 | + |
| 338 | + return () -> { |
| 339 | + var breakLoop = false; |
| 340 | + var ip = 0; |
| 341 | + while(!breakLoop){ |
| 342 | + switch (program[ip]){ |
| 343 | + case 0x00 -> { |
| 344 | + locals[0] = stack.pop(); |
| 345 | + ip++; |
| 346 | + } |
| 347 | + case 0x01 -> { |
| 348 | + stack.push(new BigDecimal(1000000)); |
| 349 | + ip++; |
| 350 | + } |
| 351 | + case 0x02 -> { |
| 352 | + stack.push(locals[0]); |
| 353 | + ip++; |
| 354 | + } |
| 355 | + case 0x03 -> { |
| 356 | + if(((Comparable) stack.pop()).compareTo(stack.pop()) >= 0){ |
| 357 | + breakLoop = true; |
| 358 | + } |
| 359 | + ip++; |
| 360 | + } |
| 361 | + case 0x04 -> { |
| 362 | + stack.push(new BigDecimal(1)); |
| 363 | + ip++; |
| 364 | + } |
| 365 | + case 0x05 -> { |
| 366 | + stack.push(cache[0].invoke(null, stack.pop(), stack.pop())); |
| 367 | + ip++; |
| 368 | + } |
| 369 | + case 0x06 -> { |
| 370 | + ip = 2; |
| 371 | + } |
| 372 | + } |
| 373 | + if(ip >= program.length){ |
| 374 | + break; |
| 375 | + } |
| 376 | + } |
| 377 | + return locals[0]; |
| 378 | + }; |
| 379 | + |
| 380 | + } |
| 381 | + |
| 382 | + private static Method resolveMethod(Fiber fiber, Class<?> targetType, String targetName, Object[] args){ |
| 383 | + var argTypes = new Class[args.length]; |
| 384 | + for(int i = 0; i < args.length; i++){ |
| 385 | + argTypes[i] = args[i] != null ? args[i].getClass() : null; |
| 386 | + } |
| 387 | + try { |
| 388 | + return fiber.vm.getDefaultLinker().getMethod(targetType, targetName, args, argTypes, true); |
| 389 | + } catch (IllegalAccessException e) { |
| 390 | + throw new RuntimeException(e); |
| 391 | + } catch (NoSuchMethodException e) { |
| 392 | + throw new RuntimeException(e); |
| 393 | + } |
| 394 | + } |
| 395 | + |
301 | 396 | } |
0 commit comments