diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2023-07-16 23:40:26 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2023-07-16 23:40:26 -0700 |
commit | 858aa11f2dbd5db5245bd3a0b1aa5fe9d91c7f65 (patch) | |
tree | ad88dc5233a908ba64539c1067698f2d01cade57 | |
parent | 339d41fabd0f41e2c1008d38f215327cc36af783 (diff) | |
download | txr-858aa11f2dbd5db5245bd3a0b1aa5fe9d91c7f65.tar.gz txr-858aa11f2dbd5db5245bd3a0b1aa5fe9d91c7f65.tar.bz2 txr-858aa11f2dbd5db5245bd3a0b1aa5fe9d91c7f65.zip |
Simplify top-level variable and function environments.
Since their inception, the top_fb and top_fb hashes associated
symbols with bindings cells, causing an extra cons cell to be
allocated for each entry. I don't remember why this is. It
might have been that way so that gethash(top_fb, sym) would
return a cell when the variable exists, or else nil. This was
before we had functions like gethash_e and inhash that return
the hash cell itself. A hash cell is also a cons and can serve
as a binding just fine by itself.Let's make it so.
For now, the macro and symbol macro environments stay the
way they are; I will likely convert them also.
* eval.c (env_fbind, env_vbind, lookup_global_var, lookup_sym_lisp1,
lookup_fun, func_get_name, rt_defv, rt_defun, set_symbol_value,
reg_fun, reg_varl): Update all these functions so they treat the
hash cell from top_vb or top_fb as the binding cell, rather than
putting or expecting the cdr of that cell (i.e the hash value)
to be a binding cell.
* hash.[ch] (gethash_d): New function. Jus gethash_e without the
pesky self argument, that would only be needed for error
reporting if we pass an object that isn't a hash.
* stdlib/place.tl (sys:get-fun-getter-setter, sys:get-vb):
These two functions must be adjusted since they refer to the
top-fb and top-vb variables. sys:get-vb isn't used anywhere;
it continues to exist in order to provide run-time support
to files that were compiled with an buggy version of the
symbol-value place.
-rw-r--r-- | eval.c | 38 | ||||
-rw-r--r-- | hash.c | 5 | ||||
-rw-r--r-- | hash.h | 1 | ||||
-rw-r--r-- | stdlib/place.tl | 6 |
4 files changed, 23 insertions, 27 deletions
@@ -189,11 +189,7 @@ val env_fbind(val env, val sym, val fun) cell = acons_new_c(sym, nulloc, mkloc(env->e.fbindings, env)); return rplacd(cell, fun); } else { - loc pcdr = gethash_l(self, top_fb, sym, nulloc); - val cell = deref(pcdr); - if (cell) - return rplacd(cell, fun); - return set(pcdr, cons(sym, fun)); + return sethash(top_fb, sym, fun); } } @@ -207,11 +203,7 @@ val env_vbind(val env, val sym, val obj) cell = acons_new_c(sym, nulloc, mkloc(env->e.vbindings, env)); return rplacd(cell, obj); } else { - loc pcdr = gethash_l(self, top_vb, sym, nulloc); - val cell = deref(pcdr); - if (cell) - return rplacd(cell, obj); - return set(pcdr, cons(sym, obj)); + return sethash(top_vb, sym, obj); } } @@ -526,8 +518,8 @@ void error_trace(val exsym, val exvals, val out_stream, val prefix) val lookup_global_var(val sym) { uses_or2; - return or2(gethash(top_vb, sym), - if2(autoload_try_var(sym), gethash(top_vb, sym))); + return or2(gethash_d(top_vb, sym), + if2(autoload_try_var(sym), gethash_d(top_vb, sym))); } val lookup_global_fun(val sym) @@ -584,10 +576,10 @@ val lookup_sym_lisp1(val env, val sym) return binding; } - return or3(gethash(top_vb, sym), + return or3(gethash_d(top_vb, sym), if2(autoload_try_fun_var(sym), - gethash(top_vb, sym)), - gethash(top_fb, sym)); + gethash_d(top_vb, sym)), + gethash_d(top_fb, sym)); } loc lookup_var_l(val env, val sym) @@ -648,8 +640,8 @@ val lookup_fun(val env, val sym) } } - return or2(gethash(top_fb, sym), - if2(autoload_try_fun(sym), gethash(top_fb, sym))); + return or2(gethash_d(top_fb, sym), + if2(autoload_try_fun(sym), gethash_d(top_fb, sym))); } val func_get_name(val fun, val env) @@ -677,7 +669,7 @@ val func_get_name(val fun, val env) { val name; - if ((name = hash_revget(top_fb, fun, eq_f, cdr_f))) + if ((name = hash_revget(top_fb, fun, eq_f, nil))) return name; if ((name = hash_revget(top_mb, fun, eq_f, cdr_f))) @@ -2138,7 +2130,7 @@ static val rt_defv(val sym) uw_purge_deferred_warning(cons(var_s, sym)); uw_purge_deferred_warning(cons(sym_s, sym)); remhash(top_smb, sym); - return sys_rplacd(cell, cons(sym, nil)); + return cell; } return nil; @@ -2198,7 +2190,7 @@ void trace_check(val name) static val rt_defun(val name, val function) { autoload_try_fun(name); - sethash(top_fb, name, cons(name, function)); + sethash(top_fb, name, function); uw_purge_deferred_warning(cons(fun_s, name)); uw_purge_deferred_warning(cons(sym_s, name)); return name; @@ -5957,7 +5949,7 @@ static val set_symbol_value(val sym, val value) if (vbind) rplacd(vbind, value); else - sethash(top_vb, sym, cons(sym, value)); + sethash(top_vb, sym, value); return value; } @@ -6625,7 +6617,7 @@ static void reg_op(val sym, opfun_t fun) void reg_fun(val sym, val fun) { assert (sym != 0); - sethash(top_fb, sym, cons(sym, fun)); + sethash(top_fb, sym, fun); sethash(builtin, sym, defun_s); } @@ -6639,7 +6631,7 @@ void reg_mac(val sym, val fun) void reg_varl(val sym, val val) { assert (sym != nil); - sethash(top_vb, sym, cons(sym, val)); + sethash(top_vb, sym, val); } void reg_var(val sym, val val) @@ -1161,6 +1161,11 @@ val gethash_e(val self, val hash, val key) return hash_lookup(h, key, hv); } +val gethash_d(val hash, val key) +{ + return gethash_e(lit("gethash_d"), hash, key); +} + val gethash(val hash, val key) { val self = lit("gethash"); @@ -55,6 +55,7 @@ val make_similar_hash(val existing); val copy_hash(val existing); val gethash_c(val self, val hash, val key, loc new_p); val gethash_e(val self, val hash, val key); +val gethash_d(val hash, val key); val gethash(val hash, val key); val inhash(val hash, val key, val init); val gethash_n(val hash, val key, val notfound_val); diff --git a/stdlib/place.tl b/stdlib/place.tl index fdd4e544..f1bef38a 100644 --- a/stdlib/place.tl +++ b/stdlib/place.tl @@ -823,8 +823,7 @@ (compile-error f "cannot assign to lambda") (compile-error f "invalid function syntax ~s" sym))) (else - (let ((cell (or (gethash sys:top-fb sym) - (sethash sys:top-fb sym (cons else nil))))) + (let ((cell (inhash sys:top-fb else nil))) (cons (op cdr) (op sys:rplacd cell)))))) @@ -857,8 +856,7 @@ ,body))) (defun sys:get-vb (sym) - (or (gethash sys:top-vb sym) - (sethash sys:top-vb sym (cons sym nil)))) + (inhash sys:top-vb sym nil)) (defplace (symbol-value sym-expr) body (getter setter |