Skip to content

Commit 13b5f80

Browse files
committed
Demo stack-based bytecode vm with Chipmunk math & calling semantics
1 parent ab6005c commit 13b5f80

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

Lang/src/profiling/java/chipmunk/profiling/ChipmunkProfiler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public static void main(String[] args) throws Throwable {
6363
javaPrograms.put("mathBench", TestPrograms.mathBench());
6464
javaPrograms.put("callJavaMethod", TestPrograms.callJavaMethod());
6565
javaPrograms.put("javaCountBigDecimal", TestPrograms.javaCountBigDecimal());
66+
javaPrograms.put("bytecodeCount", TestPrograms.bytecodeCount());
6667

6768
profiler.programs.put("chipmunk", chipmunkPrograms);
6869
profiler.programs.put("java", javaPrograms);
@@ -74,7 +75,7 @@ public static void main(String[] args) throws Throwable {
7475
long startTime = System.nanoTime();
7576
value = selected.call();
7677
long endTime = System.nanoTime();
77-
Thread.sleep(20);
78+
Thread.sleep(1000);
7879

7980
System.out.println("Value: " + value + ", Time: " + (endTime - startTime) / 1e9 + " seconds");
8081
}

Lang/src/profiling/java/chipmunk/profiling/TestPrograms.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
import chipmunk.runtime.CClass;
2424
import chipmunk.runtime.CMethod;
2525
import chipmunk.runtime.Fiber;
26+
import chipmunk.vm.invoke.CInvoker;
2627
import chipmunk.vm.tree.Node;
2728
import chipmunk.vm.tree.nodes.*;
2829

30+
import java.lang.reflect.Method;
2931
import java.math.BigDecimal;
32+
import java.util.ArrayDeque;
33+
import java.util.Comparator;
3034
import java.util.concurrent.Callable;
3135

3236
public class TestPrograms {
@@ -298,4 +302,95 @@ public static Callable<Object> javaCountBigDecimal(){
298302
};
299303
}
300304

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+
301396
}

0 commit comments

Comments
 (0)