From 555519b85ded4a8fefd9c217e6acfb529ac67432 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 22 Jul 2022 12:02:25 -0700 Subject: Handle global variables. Global variables will be stored in an array which is kept in register 0. The linker will later translate each library-ref into an integer, which is used as an index into the globals table. This design makes implementing eval quite straightforward, the eval bytecode will accept a globals table and a bytevector of bytecode, save the current set of registers, set register 0 to the new globals table, and begin executing the given bytecode. When eval is finished, it will restore the saved registers and return to the previous instruction pointer. In this way, calling eval can create a call stack. We could think about implementing function calls with eval, and it's cool that it would work, but the CPS transformation mostly makes this irrelevant. It's interesting to think about implementing an interpreter, where a function call would simply eval the function's bytecode, and the function call stack would be handled automatically. --- csc/cps.csc | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'csc/cps.csc') diff --git a/csc/cps.csc b/csc/cps.csc index 1f58287..230a1c4 100644 --- a/csc/cps.csc +++ b/csc/cps.csc @@ -23,6 +23,7 @@ %lexical-ref %lexical-set %library-define + %library-ref %sequence call? constant? @@ -54,6 +55,8 @@ %branch %fix %primitive + *globals* + branch-atom closure-arguments closure-body closure-name @@ -205,9 +208,14 @@ (define (to-cps expr continuation) (match expr (_ when (or (constant? expr) - (lexical-ref? expr) - (library-ref? expr)) + (lexical-ref? expr)) (continuation expr)) + ((% %library-ref . _) + (unless (library-ref? expr) + (error "wtf")) + (define temp (new-ref)) + (make-primitive 'peek (list *globals* expr) (list temp) + (continuation temp))) ((% %lexical-set ref arg) (to-cps arg @@ -343,10 +351,9 @@ (define (box-conversion expr) (define boxed-refs (get-boxed expr)) (define (boxed? ref) - (or (library-ref? ref) ; globals are always boxed - (and (lexical-ref? ref) - (guard (e ((key-not-found-error? e) #f)) - (lookup boxed-refs ref))))) + (and (lexical-ref? ref) + (guard (e ((key-not-found-error? e) #f)) + (lookup boxed-refs ref)))) (define (convert-arg-list args) (define boxed-args (loop for arg in args if (boxed? arg) @@ -363,7 +370,10 @@ (values new-args boxed-args vars)) (let convert ((expr expr)) (match expr - ((% %update ref atom continuation) + ((% %update ref atom continuation) when (library-ref? ref) + (make-primitive 'poke (list atom *globals* ref) '() + (convert continuation))) + ((% %update ref atom continuation) when (lexical-ref? ref) (make-primitive 'poke (list atom ref (make-constant 0)) '() (convert continuation))) ((% %primitive op args res continuation) ; Note that no reference in res can be boxed. @@ -374,12 +384,16 @@ do (set! new-expr (make-primitive 'peek (list arg (make-constant 0)) (list var) new-expr)) finally (return new-expr))) + ((% %branch atom true false) when (boxed? atom) + (define temp (new-ref)) + (make-primitive 'peek (list atom (make-constant 0)) (list temp) + (make-branch temp + (convert true) + (convert false)))) ((% %branch atom true false) - (if (boxed? atom) - (let ((var (new-ref))) - (make-primitive 'peek (list atom (make-constant 0)) (list var) - (make-branch var (convert true) (convert false)))) - (make-branch atom (convert true) (convert false)))) + (make-branch atom + (convert true) + (convert false))) ((% %apply proc args) (define-values (new-params boxed-params vars) (convert-arg-list (cons proc args))) (define new-expr (make-apply (car new-params) (cdr new-params))) -- cgit v1.3.1