Releases: vsanna/lovejvm
Releases · vsanna/lovejvm
Step2.1: fibonacchi
input
class Recursive2 {
static public void main() {
fibonacchi(10);
}
static int fibonacchi(int n) {
if(n <= 1) return n;
return fibonacchi(n-1) + fibonacchi(n-2);
}
}
output
stack#=0, pc = 259, inst = 10, frame=LFrame{locals=[], operandStack=[Word(0,0,0,0: 0)]}
stack#=0, pc = 261, inst = b8, frame=LFrame{locals=[], operandStack=[Word(0,0,0,a: 10), Word(0,0,0,0: 0)]}
stack#=1, pc = 308, inst = 1a, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,1,8: 264)]}
stack#=1, pc = 309, inst = 4, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,a: 10), Word(0,0,1,8: 264)]}
stack#=1, pc = 310, inst = a3, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,a: 10), Word(0,0,1,8: 264)]}
stack#=1, pc = 315, inst = 1a, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,1,8: 264)]}
stack#=1, pc = 316, inst = 4, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,a: 10), Word(0,0,1,8: 264)]}
stack#=1, pc = 317, inst = 64, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,a: 10), Word(0,0,1,8: 264)]}
stack#=1, pc = 318, inst = b8, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,9: 9), Word(0,0,1,8: 264)]}
stack#=2, pc = 308, inst = 1a, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,1,41: 321)]}
stack#=2, pc = 309, inst = 4, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,0,9: 9), Word(0,0,1,41: 321)]}
stack#=2, pc = 310, inst = a3, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,9: 9), Word(0,0,1,41: 321)]}
stack#=2, pc = 315, inst = 1a, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,1,41: 321)]}
stack#=2, pc = 316, inst = 4, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,0,9: 9), Word(0,0,1,41: 321)]}
stack#=2, pc = 317, inst = 64, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,9: 9), Word(0,0,1,41: 321)]}
stack#=2, pc = 318, inst = b8, frame=LFrame{locals=[Word(0,0,0,9: 9)], operandStack=[Word(0,0,0,8: 8), Word(0,0,1,41: 321)]}
stack#=3, pc = 308, inst = 1a, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,1,41: 321)]}
stack#=3, pc = 309, inst = 4, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,8: 8), Word(0,0,1,41: 321)]}
stack#=3, pc = 310, inst = a3, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,8: 8), Word(0,0,1,41: 321)]}
stack#=3, pc = 315, inst = 1a, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,1,41: 321)]}
stack#=3, pc = 316, inst = 4, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,8: 8), Word(0,0,1,41: 321)]}
stack#=3, pc = 317, inst = 64, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,8: 8), Word(0,0,1,41: 321)]}
stack#=3, pc = 318, inst = b8, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,7: 7), Word(0,0,1,41: 321)]}
...
...
stack#=3, pc = 327, inst = 60, frame=LFrame{locals=[Word(0,0,0,6: 6)], operandStack=[Word(0,0,0,3: 3), Word(0,0,0,5: 5), Word(0,0,1,47: 327)]}
stack#=3, pc = 328, inst = ac, frame=LFrame{locals=[Word(0,0,0,6: 6)], operandStack=[Word(0,0,0,8: 8), Word(0,0,1,47: 327)]}
stack#=2, pc = 327, inst = 60, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,8: 8), Word(0,0,0,d: 13), Word(0,0,1,47: 327)]}
stack#=2, pc = 328, inst = ac, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,15: 21), Word(0,0,1,47: 327)]}
stack#=1, pc = 327, inst = 60, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,15: 21), Word(0,0,0,22: 34), Word(0,0,1,8: 264)]}
stack#=1, pc = 328, inst = ac, frame=LFrame{locals=[Word(0,0,0,a: 10)], operandStack=[Word(0,0,0,37: 55), Word(0,0,1,8: 264)]}
stack#=0, pc = 264, inst = 57, frame=LFrame{locals=[], operandStack=[Word(0,0,0,37: 55), Word(0,0,0,0: 0)]}
stack#=0, pc = 265, inst = b1, frame=LFrame{locals=[], operandStack=[Word(0,0,0,0: 0)]}
Step2: Recursive
what is attemted
- Introduced Thread/Frame so that jvm can call other methods(invokeXXX)
- introduced classparser so that we can manage methods
- introduced MethodArea so that we can manage classes/methods
- introduced invokestatic!!
- make more instructions available
- add Recursive.java as guest code
output
class Recursive {
static public void recursiveMain() {
recursive(10);
}
static int recursive(int a) {
if(a < 1000) {
return recursive(a + a);
} else {
return a;
}
}
}
stack#=0, pc = 265, inst = 4, frame=LFrame{locals=[], operandStack=[]}
stack#=0, pc = 266, inst = b8, frame=LFrame{locals=[], operandStack=[Word(0,0,0,1: 1)]}
stack#=1, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[]}
stack#=1, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[Word(0,0,0,1: 1)]}
stack#=1, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,1: 1)]}
stack#=1, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[]}
stack#=1, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[Word(0,0,0,1: 1)]}
stack#=1, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[Word(0,0,0,1: 1), Word(0,0,0,1: 1)]}
stack#=1, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,1: 1)], operandStack=[Word(0,0,0,2: 2)]}
stack#=2, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[]}
stack#=2, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[Word(0,0,0,2: 2)]}
stack#=2, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,2: 2)]}
stack#=2, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[]}
stack#=2, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[Word(0,0,0,2: 2)]}
stack#=2, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[Word(0,0,0,2: 2), Word(0,0,0,2: 2)]}
stack#=2, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,2: 2)], operandStack=[Word(0,0,0,4: 4)]}
stack#=3, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[]}
stack#=3, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[Word(0,0,0,4: 4)]}
stack#=3, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,4: 4)]}
stack#=3, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[]}
stack#=3, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[Word(0,0,0,4: 4)]}
stack#=3, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[Word(0,0,0,4: 4), Word(0,0,0,4: 4)]}
stack#=3, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,4: 4)], operandStack=[Word(0,0,0,8: 8)]}
stack#=4, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[]}
stack#=4, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,8: 8)]}
stack#=4, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,8: 8)]}
stack#=4, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[]}
stack#=4, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,8: 8)]}
stack#=4, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,8: 8), Word(0,0,0,8: 8)]}
stack#=4, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,8: 8)], operandStack=[Word(0,0,0,10: 16)]}
stack#=5, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[]}
stack#=5, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[Word(0,0,0,10: 16)]}
stack#=5, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,10: 16)]}
stack#=5, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[]}
stack#=5, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[Word(0,0,0,10: 16)]}
stack#=5, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[Word(0,0,0,10: 16), Word(0,0,0,10: 16)]}
stack#=5, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,10: 16)], operandStack=[Word(0,0,0,20: 32)]}
stack#=6, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[]}
stack#=6, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[Word(0,0,0,20: 32)]}
stack#=6, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,20: 32)]}
stack#=6, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[]}
stack#=6, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[Word(0,0,0,20: 32)]}
stack#=6, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[Word(0,0,0,20: 32), Word(0,0,0,20: 32)]}
stack#=6, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,20: 32)], operandStack=[Word(0,0,0,40: 64)]}
stack#=7, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[]}
stack#=7, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[Word(0,0,0,40: 64)]}
stack#=7, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,40: 64)]}
stack#=7, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[]}
stack#=7, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[Word(0,0,0,40: 64)]}
stack#=7, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[Word(0,0,0,40: 64), Word(0,0,0,40: 64)]}
stack#=7, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,40: 64)], operandStack=[Word(0,0,0,80: 128)]}
stack#=8, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[]}
stack#=8, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[Word(0,0,0,80: 128)]}
stack#=8, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,0,80: 128)]}
stack#=8, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[]}
stack#=8, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[Word(0,0,0,80: 128)]}
stack#=8, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[Word(0,0,0,80: 128), Word(0,0,0,80: 128)]}
stack#=8, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,0,80: 128)], operandStack=[Word(0,0,1,0: 256)]}
stack#=9, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[]}
stack#=9, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[Word(0,0,1,0: 256)]}
stack#=9, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,1,0: 256)]}
stack#=9, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[]}
stack#=9, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[Word(0,0,1,0: 256)]}
stack#=9, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[Word(0,0,1,0: 256), Word(0,0,1,0: 256)]}
stack#=9, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,1,0: 256)], operandStack=[Word(0,0,2,0: 512)]}
stack#=10, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[]}
stack#=10, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[Word(0,0,2,0: 512)]}
stack#=10, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,2,0: 512)]}
stack#=10, pc = 320, inst = 1a, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[]}
stack#=10, pc = 321, inst = 1a, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[Word(0,0,2,0: 512)]}
stack#=10, pc = 322, inst = 60, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[Word(0,0,2,0: 512), Word(0,0,2,0: 512)]}
stack#=10, pc = 323, inst = b8, frame=LFrame{locals=[Word(0,0,2,0: 512)], operandStack=[Word(0,0,4,0: 1024)]}
stack#=11, pc = 313, inst = 1a, frame=LFrame{locals=[Word(0,0,4,0: 1024)], operandStack=[]}
stack#=11, pc = 314, inst = 11, frame=LFrame{locals=[Word(0,0,4,0: 1024)], operandStack=[Word(0,0,4,0: 1024)]}
stack#=11, pc = 317, inst = a2, frame=LFrame{locals=[Word(0,0,4,0: 1024)], operandStack=[Word(0,0,3,e8: 1000), Word(0,0,4,0: 1024)]}
stack#=11, pc = 327, inst = 1a, frame=LFrame{locals=[Word(0,0,4,0: 1024)], operandStack=[]}
stack#=11, pc = 328, inst = ac, frame=LFrame{locals=[Word(0,0,4,0: 1024)], operandStack=[Word(0,0,4,0: 1024)]}
Step1: ForLoop
what is attempted
- modify locals/operandStack so that they accept word(=32bit) instead of byte
- make more instructions available
- add ForLoop.java as a guest sample code
output
...
...
pc = 30, inst = ac, locals = [null, Word(2,fa,dc,f8: 49995000), Word(0,0,27,10: 10000)], operandStack = [Word(2,fa,dc,f8: 49995000)]
Step0: very primitive JVM
what to try
- interpret Code section extracted from a classfile
- a few instructions
- no thread, no frame.
- no method area
- no heap
- no classloader
output
> Task :LoveJVM.main()
pc = 8, inst = 10, locals = [0, 0, 0] , operandStack = []
pc = 10, inst = 3b, locals = [0, 0, 0] , operandStack = [10]
pc = 11, inst = 7, locals = [10, 0, 0], operandStack = []
pc = 12, inst = 3c, locals = [10, 0, 0], operandStack = [4]
pc = 13, inst = 1a, locals = [10, 4, 0], operandStack = []
pc = 14, inst = 1b, locals = [10, 4, 0], operandStack = [10]
pc = 15, inst = 60, locals = [10, 4, 0], operandStack = [4, 10]
pc = 16, inst = 3d, locals = [10, 4, 0], operandStack = [14]
pc = 17, inst = 1c, locals = [10, 4, 14], operandStack = []
pc = 18, inst = 5, locals = [10, 4, 14], operandStack = [14]
pc = 19, inst = 6c, locals = [10, 4, 14], operandStack = [2, 14]
pc = 20, inst = 3d, locals = [10, 4, 14], operandStack = [7]
pc = 21, inst = b1, locals = [10, 4, 7], operandStack = [] <--- locals[2](=c) has 7