All is one-op instructions (save push). Code in memory (this will allow compilation)

This commit is contained in:
2025-06-05 14:22:12 -05:00
parent 11eae06995
commit 679b53d76e
3 changed files with 204 additions and 137 deletions

140
tests.scm
View File

@@ -8,93 +8,101 @@
;;; Data
(define adder-program-asm
'((push 1)
'((variable result 0)
(push 1)
(push 2)
(+)
(store #x01)))
(push result)
(!)
(bye)))
(define fib-program-asm
'( (load 1)
(call fib)
(jmp cleanup)
'( (variable result 0)
(push result)
(@)
(push fib)
(call)
(push cleanup)
(jmp)
fib
(dup)
(push 0)
(=)
(if not0)
(pop)
(pop)
(push 0)
(return)
not0
(pop)
(dup)
(over)
(push 1)
(=)
(if not1)
(pop)
(pop)
(push 1)
(or)
(push recur)
(branch)
(return)
not1
(pop)
(push 1)
(-)
recur
(dup)
(call fib)
(swap)
(push 1)
(-)
(call fib)
(push fib)
(call)
(over)
(push 2)
(-)
(push fib)
(call)
(+)
(nip)
(return)
cleanup
(store #x1)))
(push result)
(!)
(bye)))
(define adder-program-bytecode
#vu8(#x01 1 0 0 0 ; Push value "1"
#vu8(0 0 0 0 ; Memory address of the result
#x01 1 0 0 0 ; Push value "1"
#x01 2 0 0 0 ; Push value "2"
#x05 ; Perform "+"
#x03 1 0 0 0 ; Store the value to memory address 1
#x04 ; Perform "+"
#x01 1 0 0 0 ; Push the address of the result
#x02 ; Store the value
#xff ; Exit the program
))
(define fib-program-bytecode
#vu8(#x04 1 0 0 0 ; 0 load "n" from memory address 0x01
#x0d 15 0 0 0 ; 5 call fib procedure
#x16 83 0 0 0 ; 10 jump to cleanup
;; "fib" procedure
#x14 ; 15 duplicate n
#x01 0 0 0 0 ; 16 push 0
#x11 ; 21 test equality
#x0c 35 0 0 0 ; 22 if
#x02 ; 27 pop tested value
#x02 ; 28 pop n
#x01 0 0 0 0 ; 29 push '0'
#x0e ; 31 return
#x02 ; 35 pop tested value
#x14 ; 36 duplicate n
#x01 1 0 0 0 ; 37 push 1
#x11 ; 42 test equality
#x0c 56 0 0 0 ; 43 if
#x02 ; 48 pop tested value
#x02 ; 49 pop n
#x01 1 0 0 0 ; 50 push '1'
#x0e ; 55 return
#x02 ; 56 pop tested value
#vu8(0 0 0 0 ; Memory address of the input, will also store the result
#x1 1 0 0 0 ; Push address of the input
#x03 ; Fetch "n"
#x1 23 0 0 0 ; Push address of "fib"
#x10 ; Call
#x01 74 0 0 0 ; Push address of "cleanup"
#x0e ; Jump
;; "fib" procedure ( n -- fib(n) )
#x16 ; Duplicate n
#x01 0 0 0 0 ; Push 0
#x0b ; Test equality
#x19 ; Over
#x01 1 0 0 0 ; Push 1
#x0b ; Test equality
#x07 ; OR the values of the last two tests
#x01 45 0 0 0 ; Push address of "recur"
#x0f ; Branch
#x11 ; Return
;; "recur" label
;; recursively calculate fib (n - 1)
#x01 1 0 0 0 ; 57 push 1
#x06 ; 62 (n - 1)
#x14 ; 63 duplicate (n - 1) as an arg
#x0d 15 0 0 0 ; 64 call fib
#x16 ; Dupe "n"
#x01 1 0 0 0 ; Push 1
#x05 ; (n - 1)
#x01 23 0 0 0 ; Push address of "fib"
#x10 ; Call
;; recursively calculate fib (n - 2)
#x15 ; 69 swap n - 1 back atop stack
#x01 1 0 0 0 ; 70 push 1
#x06 ; 75 (n - 2)
#x0d 15 0 0 0 ; 76 call fib
#x05 ; 81 (fib(n - 1) + fib (n - 2))
#x0e ; 82 return
#x19 ; Dupe n over the result
#x01 2 0 0 0 ; Push 2
#x05 ; (n - 2)
#x01 23 0 0 0 ; Push address of fib
#x10 ; Call
#x04 ; (fib(n - 1) + fib (n - 2))
#x15 ; Nip the dupe of "n"
#x11 ; Return
;; cleanup
#x03 1 0 0 0 ; 83 store fib(n) to memory address 0x01
#x01 1 0 0 0 ; Push memory address of result
#x02 ; Store fib(n)
#xff ; Exit program
))
@@ -112,12 +120,14 @@
(define-test-suite "vm"
(define-test "adder"
(define my-vm (make-vm))
(define my-program (open-bytevector-input-port adder-program-bytecode))
(run-vm my-vm my-program)
(vm-load-program! my-vm adder-program-bytecode)
(vm-pc-set! my-vm 5)
((my-vm 'vm-run))
(assert-equal 3 (vm-memory-ref my-vm 1)))
(define-test "fib"
(define my-vm (make-vm))
(vm-load-program! my-vm fib-program-bytecode)
(vm-memory-set! my-vm 1 10)
(define my-program (open-bytevector-input-port fib-program-bytecode))
(run-vm my-vm my-program)
(vm-pc-set! my-vm 5)
((my-vm 'vm-run))
(assert-equal 55 (vm-memory-ref my-vm 1))))