diff --git a/extlib/benz/codegen.c b/extlib/benz/codegen.c index 77f3a97b..51df8c0c 100644 --- a/extlib/benz/codegen.c +++ b/extlib/benz/codegen.c @@ -578,9 +578,6 @@ typedef struct codegen_context { /* constant object pool */ pic_value *pool; size_t plen, pcapa; - /* symbol pool */ - pic_sym **syms; - size_t slen, scapa; struct codegen_context *up; } codegen_context; @@ -609,10 +606,6 @@ codegen_context_init(pic_state *pic, codegen_context *cxt, codegen_context *up, cxt->plen = 0; cxt->pcapa = PIC_POOL_SIZE; - cxt->syms = pic_calloc(pic, PIC_SYMS_SIZE, sizeof(pic_sym *)); - cxt->slen = 0; - cxt->scapa = PIC_SYMS_SIZE; - create_activation(pic, cxt); } @@ -633,8 +626,6 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) irep->ilen = cxt->ilen; irep->pool = pic_realloc(pic, cxt->pool, sizeof(pic_value) * cxt->plen); irep->plen = cxt->plen; - irep->syms = pic_realloc(pic, cxt->syms, sizeof(pic_sym *) * cxt->slen); - irep->slen = cxt->slen; return irep; } @@ -647,7 +638,6 @@ codegen_context_destroy(pic_state *pic, codegen_context *cxt) } while (0) #define check_code_size(pic, cxt) check_size(pic, cxt, c, code, pic_code) -#define check_syms_size(pic, cxt) check_size(pic, cxt, s, syms, pic_sym *) #define check_irep_size(pic, cxt) check_size(pic, cxt, i, irep, struct pic_irep *) #define check_pool_size(pic, cxt) check_size(pic, cxt, p, pool, pic_value) @@ -716,18 +706,19 @@ index_local(codegen_context *cxt, pic_sym *sym) } static int -index_symbol(pic_state *pic, codegen_context *cxt, pic_sym *sym) +index_global(pic_state *pic, codegen_context *cxt, pic_sym *name) { - size_t i; + extern pic_value pic_vm_gref_slot(pic_state *, pic_sym *); + int pidx; + pic_value slot; - for (i = 0; i < cxt->slen; ++i) { - if (cxt->syms[i] == sym) { - return i; - } - } - check_syms_size(pic, cxt); - cxt->syms[cxt->slen++] = sym; - return i; + slot = pic_vm_gref_slot(pic, name); + + check_pool_size(pic, cxt); + pidx = (int)cxt->plen++; + cxt->pool[pidx] = slot; + + return pidx; } static void @@ -758,7 +749,10 @@ codegen_ref(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) sym = pic_sym_ptr(pic_car(pic, obj)); if (sym == pic->sGREF) { - emit_i(pic, cxt, OP_GREF, index_symbol(pic, cxt, pic_sym_ptr(pic_list_ref(pic, obj, 1)))); + pic_sym *name; + + name = pic_sym_ptr(pic_list_ref(pic, obj, 1)); + emit_i(pic, cxt, OP_GREF, index_global(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } else if (sym == pic->sCREF) { @@ -797,7 +791,10 @@ codegen_set(pic_state *pic, codegen_context *cxt, pic_value obj, bool tailpos) var = pic_list_ref(pic, obj, 1); type = pic_sym_ptr(pic_list_ref(pic, var, 0)); if (type == pic->sGREF) { - emit_i(pic, cxt, OP_GSET, index_symbol(pic, cxt, pic_sym_ptr(pic_list_ref(pic, var, 1)))); + pic_sym *name; + + name = pic_sym_ptr(pic_list_ref(pic, var, 1)); + emit_i(pic, cxt, OP_GSET, index_global(pic, cxt, name)); emit_ret(pic, cxt, tailpos); } else if (type == pic->sCREF) { diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 77cad985..878c500e 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -435,9 +435,6 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) for (i = 0; i < irep->plen; ++i) { gc_mark(pic, irep->pool[i]); } - for (i = 0; i < irep->slen; ++i) { - gc_mark_object(pic, (struct pic_object *)irep->syms[i]); - } break; } case PIC_TT_DATA: { @@ -712,7 +709,6 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) pic_free(pic, irep->code); pic_free(pic, irep->irep); pic_free(pic, irep->pool); - pic_free(pic, irep->syms); break; } case PIC_TT_DATA: { diff --git a/extlib/benz/include/picrin/irep.h b/extlib/benz/include/picrin/irep.h index 327fb32c..bcaf4eb6 100644 --- a/extlib/benz/include/picrin/irep.h +++ b/extlib/benz/include/picrin/irep.h @@ -74,7 +74,6 @@ struct pic_irep { bool varg; struct pic_irep **irep; pic_value *pool; - pic_sym **syms; size_t clen, ilen, plen, slen; }; diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index 5e2217c6..10fdaac5 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -389,6 +389,34 @@ pic_get_args(pic_state *pic, const char *format, ...) return argc; } +pic_value +pic_vm_gref_slot(pic_state *pic, pic_sym *uid) +{ + pic_value slot; + + if (pic_dict_has(pic, pic->globals, uid)) { + return pic_dict_ref(pic, pic->globals, uid); + } + slot = pic_cons(pic, pic_obj_value(uid), pic_invalid_value()); + pic_dict_set(pic, pic->globals, uid, slot); + return slot; +} + +static pic_value +vm_gref(pic_state *pic, pic_value slot) +{ + if (pic_invalid_p(pic_cdr(pic, slot))) { + pic_errorf(pic, "uninitialized global variable: ~a", pic_car(pic, slot)); + } + return pic_cdr(pic, slot); +} + +static void +vm_gset(pic_state *pic, pic_value slot, pic_value value) +{ + pic_set_cdr(pic, slot, value); +} + static void vm_push_cxt(pic_state *pic) { @@ -605,23 +633,11 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value args) NEXT; } CASE(OP_GREF) { - pic_sym *sym; - - sym = pic->ci->irep->syms[c.u.i]; - if (! pic_dict_has(pic, pic->globals, sym)) { - pic_errorf(pic, "uninitialized global variable: %s", pic_symbol_name(pic, sym)); - } - PUSH(pic_dict_ref(pic, pic->globals, sym)); + PUSH(vm_gref(pic, pic->ci->irep->pool[c.u.i])); NEXT; } CASE(OP_GSET) { - pic_sym *sym; - pic_value val; - - sym = pic->ci->irep->syms[c.u.i]; - - val = POP(); - pic_dict_set(pic, pic->globals, sym, val); + vm_gset(pic, pic->ci->irep->pool[c.u.i], POP()); PUSH(pic_undef_value()); NEXT; } @@ -840,7 +856,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value args) } #define check_condition(name, n) do { \ - if (! pic_eq_p(pic->p##name, pic_dict_ref(pic, pic->globals, pic->u##name))) \ + if (! pic_eq_p(pic->p##name, vm_gref(pic, pic_vm_gref_slot(pic, pic->u##name)))) \ goto L_CALL; \ if (c.u.i != n + 1) \ goto L_CALL; \ @@ -1106,7 +1122,7 @@ pic_define_(pic_state *pic, const char *name, pic_value val) } } - pic_dict_set(pic, pic->globals, uid, val); + pic_set(pic, pic->lib, name, val); } void @@ -1153,7 +1169,7 @@ pic_ref(pic_state *pic, struct pic_lib *lib, const char *name) pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name); } - return pic_dict_ref(pic, pic->globals, uid); + return vm_gref(pic, pic_vm_gref_slot(pic, uid)); } void @@ -1167,7 +1183,7 @@ pic_set(pic_state *pic, struct pic_lib *lib, const char *name, pic_value val) pic_errorf(pic, "symbol \"%s\" not defined in library ~s", name, lib->name); } - pic_dict_set(pic, pic->globals, uid, val); + vm_gset(pic, pic_vm_gref_slot(pic, uid), val); } pic_value