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/ir2.csc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'csc/ir2.csc') diff --git a/csc/ir2.csc b/csc/ir2.csc index 331aa4c..30fb5a3 100644 --- a/csc/ir2.csc +++ b/csc/ir2.csc @@ -4,8 +4,10 @@ %branch %closure %fix + %globals %primitive %variable + *globals* apply-arguments apply-procedure apply? @@ -27,6 +29,7 @@ fix-body fix-functions fix? + globals? kargs-expression kargs-refs kargs? @@ -101,10 +104,10 @@ ; Atoms consist of ; - constant, ; - lexical-ref, - ; - or library-ref + ; - library-ref, + ; - or globals. ; After closure conversion, there are no more lexical refs. - ; Each lexical ref will be converted to one of the following - ; data types. + ; Each lexical ref will be converted to a . ; A function argument or local variable. @@ -115,6 +118,18 @@ (gensym variable-gensym)) + ; The globals array. This will eventually be stored in register 0. + (define-match-record-type + (make-globals) + globals? + %globals) + + + ; A global instance of . + ; Considered equal to calling (make-globals). + (define *globals* (make-globals)) + + ; CPS expressions: ; CPS expressions are similar to IR1 expressions, ; but constrained not to have any subexpressions except atoms. -- cgit v1.3.1