scm-to-vm/asm/runtime.scm

57 lines
915 B
Scheme

;; Note that this is scheme syntax wrapping asm for a stack machine
(variable eom 1024)
;; These need to be initialized with the runtime
(variable scan 0)
(variable free 0)
(variable root 0)
(variable old 0)
(variable new 0)
(push main)
(jmp)
alloc ;; ( -- p)
;; Test if free will go beyond eom
(ref free)
(dup) ; ( -- free free)
(push 8)
(+)
(dup) ; ( -- free free+8 free+8)
(ref eom)
(<) ; ( -- free free+8 (free+8 < eom))
(push alloc-do-gc)
(if)
;; write free+8 to free
(set! free)
;; return the old free, it is memory the program can use
(return)
alloc-do-gc
;; Empty the stack
(drop)
(drop)
;; Run garbage collection
(push gc-start)
(call)
;; Tail-call allocation
(push alloc)
(jmp)
gc-start ;; ( -- )
; Move scan & free to start of new memory
(ref new)
(dup)
(set! free)
(set! scan)
(ref root)
(push relocate-object)
(call)
(push gc-loop)
(call)
(return)
relocate-object ;; (o -- )
;; TODO
main
;; TODO