env preserves global symbols and their bindings in Lisp world from GC.
+--------+
| |
next V |
+----------+ | +---------+
| Env *env |---+ | |
+----------+ cdr V |
| +-----------+ |
+-------------->| TCELL:Obj |---+
vars +-----------+
|
car V (var)
+-----------+
+---------| TCELL:Obj |-------+
| +-----------+ |
| |
car V (name) cdr V (val)
+-------------+ +---------+
| TSYMBOL:Obj | | *:Obj |
+-------------+ +---------+
Like (defvar *env* (list (cons 'foo 1) (cons 'bar #\2) (cons 'baz "3")))
in
Lisp.
root explicitly represents the stack-frame to preserve unbound intermediate objects in C world from GC.
-
at the top of
main()
+--------------------+ | main() | +--------------------+ | [0] NULL | | [1] &main | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **sexp ] | [3] (Obj *) 0 |<----[ Obj **expanded ] |- - - - - - - - - - | | [4] (Obj *) -1 | +--------------------+
-
at the top of
add_primitive()
called fromdefine_primitives()
+--------------------+ +---->| main() | | +--------------------+ | | [0] NULL | | | [1] &main | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **sexp ] | | [3] (Obj *) 0 |<----[ Obj **expanded ] | |- - - - - - - - - - | | | [4] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_primitive() | | +--------------------+ +-----| [0] (Obj **) ? | | [1] &add_primitive | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **prim ] | [3] (Obj *) 0 |--+ | [4] (Obj *) 0 | | useless? | [5] (Obj *) 0 |--+ |- - - - - - - - - - | | [6] (Obj *) -1 | +--------------------+
-
in
add_var()
called fromadd_primitive()
+--------------------+ +---->| main() | | +--------------------+ | | [0] NULL | | | [1] &main | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **sexp ] | | [3] (Obj *) 0 |<----[ Obj **expanded ] | |- - - - - - - - - - | | | [4] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_primitive() |<--------------------------+ | +--------------------+ | +-----| [0] (Obj **) ? | | | [1] &add_primitive | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **prim ] | | [3] (Obj *) 0 |--+ | | [4] (Obj *) 0 | | useless? | | [5] (Obj *) 0 |--+ | |- - - - - - - - - - | | | [6] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_var() | | +--------------------+ | | [0] (Obj **) ? |---------------------------+ | [1] &add_var | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **sym ] | [3] (Obj *) 0 |<----[ Obj **cell ] | [4] (Obj *) 0 |<----[ Obj **tmp ] useless? |- - - - - - - - - - | | [5] (Obj *) -1 | +--------------------+
-
in
add_var_int()
called fromadd_var()
+--------------------+ +---->| main() | | +--------------------+ | | [0] NULL | | | [1] &main | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **sexp ] | | [3] (Obj *) 0 |<----[ Obj **expanded ] | |- - - - - - - - - - | | | [4] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_primitive() |<--------------------------+ | +--------------------+ | +-----| [0] (Obj **) ? | | | [1] &add_primitive | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **prim ] | | [3] (Obj *) 0 |--+ | | [4] (Obj *) 0 | | useless? | | [5] (Obj *) 0 |--+ | |- - - - - - - - - - | | | [6] (Obj *) -1 | | +--------------------+ | | +--------------------+ | +---->| add_var() | | | +--------------------+ | | | [0] (Obj **) ? |---------------------------+ | | [1] &add_var | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **sym ] | | [3] (Obj *) 0 |<----[ Obj **cell ] | | [4] (Obj *) 0 |<----[ Obj **tmp ] useless? | |- - - - - - - - - - | | | [5] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_var_int() | | +--------------------+ +-----| [0] (Obj **) ? | | [1] &add_var_init | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **cell ] | [3] (Obj *) 0 |<----[ Obj **tmp ] |- - - - - - - - - - | | [4] (Obj *) -1 | +--------------------+
-
in
add_var()
after returning fromadd_var_int()
call+--------------------+ +---->| main() | | +--------------------+ | | [0] NULL | | | [1] &main | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **sexp ] | | [3] (Obj *) 0 |<----[ Obj **expanded ] | |- - - - - - - - - - | | | [4] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_primitive() |<--------------------------+ | +--------------------+ | +-----| [0] (Obj **) ? | | | [1] &add_primitive | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **prim ] | | [3] (Obj *) 0 |--+ | | [4] (Obj *) 0 | | useless? | | [5] (Obj *) 0 |--+ | |- - - - - - - - - - | | | [6] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_var() | | +--------------------+ | | [0] (Obj **) ? |---------------------------+ | [1] &add_var | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **sym ] | [3] (Obj *) 0 |<----[ Obj **cell ] | [4] (Obj *) 0 |<----[ Obj **tmp ] useless? |- - - - - - - - - - | | [5] (Obj *) -1 | +--------------------+
-
in
add_primitive()
after returning fromadd_var()
call+--------------------+ +---->| main() | | +--------------------+ | | [0] NULL | | | [1] &main | | |- - - - - - - - - - | | | [2] (Obj *) 0 |<----[ Obj **sexp ] | | [3] (Obj *) 0 |<----[ Obj **expanded ] | |- - - - - - - - - - | | | [4] (Obj *) -1 | | +--------------------+ | | +--------------------+ | | add_primitive() | | +--------------------+ +-----| [0] (Obj **) ? | | [1] &add_primitive | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **prim ] | [3] (Obj *) 0 |--+ | [4] (Obj *) 0 | | useless? | [5] (Obj *) 0 |--+ |- - - - - - - - - - | | [6] (Obj *) -1 | +--------------------+
-
in
define_primitives()
after returning from eachadd_primitive()
call+--------------------+ | main() | +--------------------+ | [0] NULL | | [1] &main | |- - - - - - - - - - | | [2] (Obj *) 0 |<----[ Obj **sexp ] | [3] (Obj *) 0 |<----[ Obj **expanded ] |- - - - - - - - - - | | [4] (Obj *) -1 | +--------------------+