reg -> weak

This commit is contained in:
Yuichi Nishiwaki 2016-02-10 21:50:39 +09:00
parent 7b86c6d34e
commit b577b2d453
11 changed files with 130 additions and 130 deletions

View File

@ -146,13 +146,13 @@ define_var(pic_state *pic, analyze_scope *scope, pic_sym *sym)
int ret;
if (search_scope(pic, scope, sym)) {
if (scope->depth > 0 || pic_reg_has(pic, pic->globals, sym)) {
if (scope->depth > 0 || pic_weak_has(pic, pic->globals, sym)) {
pic_warnf(pic, "redefining variable: ~s", pic_obj_value(sym));
}
return;
}
pic_reg_set(pic, pic->globals, sym, pic_invalid_value());
pic_weak_set(pic, pic->globals, sym, pic_invalid_value());
kh_put(a, &scope->locals, sym, &ret);
}

View File

@ -25,7 +25,7 @@ struct pic_object {
struct pic_pair pair;
struct pic_vector vec;
struct pic_dict dict;
struct pic_reg reg;
struct pic_weak weak;
struct pic_data data;
struct pic_record rec;
struct pic_id id;
@ -42,7 +42,7 @@ struct pic_object {
struct pic_heap {
union header base, *freep;
struct heap_page *pages;
struct pic_reg *regs; /* weak map chain */
struct pic_weak *weaks; /* weak map chain */
};
struct pic_heap *
@ -58,7 +58,7 @@ pic_heap_open(pic_state *pic)
heap->freep = &heap->base;
heap->pages = NULL;
heap->regs = NULL;
heap->weaks = NULL;
return heap;
}
@ -387,11 +387,11 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
LOOP(obj->u.sym.str);
break;
}
case PIC_TT_REG: {
struct pic_reg *reg = (struct pic_reg *)obj;
case PIC_TT_WEAK: {
struct pic_weak *weak = (struct pic_weak *)obj;
reg->prev = pic->heap->regs;
pic->heap->regs = reg;
weak->prev = pic->heap->weaks;
pic->heap->weaks = weak;
break;
}
case PIC_TT_CP: {
@ -429,7 +429,7 @@ gc_mark_phase(pic_state *pic)
struct pic_list *list;
size_t j;
assert(pic->heap->regs == NULL);
assert(pic->heap->weaks == NULL);
/* checkpoint */
if (pic->cp) {
@ -497,19 +497,19 @@ gc_mark_phase(pic_state *pic)
/* parameter table */
gc_mark(pic, pic->ptable);
/* registries */
/* weak maps */
do {
struct pic_object *key;
pic_value val;
khiter_t it;
khash_t(reg) *h;
struct pic_reg *reg;
khash_t(weak) *h;
struct pic_weak *weak;
j = 0;
reg = pic->heap->regs;
weak = pic->heap->weaks;
while (reg != NULL) {
h = &reg->hash;
while (weak != NULL) {
h = &weak->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
@ -522,7 +522,7 @@ gc_mark_phase(pic_state *pic)
}
}
}
reg = reg->prev;
weak = weak->prev;
}
} while (j > 0);
}
@ -563,8 +563,8 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
/* TODO: remove this symbol's entry from pic->syms immediately */
break;
}
case PIC_TT_REG: {
kh_destroy(reg, &obj->u.reg.hash);
case PIC_TT_WEAK: {
kh_destroy(weak, &obj->u.weak.hash);
break;
}
case PIC_TT_PROC: {
@ -643,24 +643,24 @@ gc_sweep_phase(pic_state *pic)
{
struct heap_page *page;
khiter_t it;
khash_t(reg) *h;
khash_t(weak) *h;
khash_t(s) *s = &pic->oblist;
pic_sym *sym;
struct pic_object *obj;
size_t total = 0, inuse = 0;
/* registries */
while (pic->heap->regs != NULL) {
h = &pic->heap->regs->hash;
/* weak maps */
while (pic->heap->weaks != NULL) {
h = &pic->heap->weaks->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
obj = kh_key(h, it);
if (obj->u.basic.gc_mark == PIC_GC_UNMARK) {
kh_del(reg, h, it);
kh_del(weak, h, it);
}
}
pic->heap->regs = pic->heap->regs->prev;
pic->heap->weaks = pic->heap->weaks->prev;
}
/* symbol table */

View File

@ -106,8 +106,8 @@ struct pic_state {
khash_t(s) oblist; /* string to symbol */
int ucnt;
struct pic_reg *globals;
struct pic_reg *macros;
struct pic_weak *globals;
struct pic_weak *macros;
pic_value libs;
struct pic_list ireps; /* chain */
@ -246,7 +246,7 @@ pic_value pic_fdisplay(pic_state *, pic_value, xFILE *);
#include "picrin/string.h"
#include "picrin/symbol.h"
#include "picrin/vector.h"
#include "picrin/reg.h"
#include "picrin/weak.h"
#if defined(__cplusplus)
}

View File

@ -1,34 +0,0 @@
/**
* See Copyright Notice in picrin.h
*/
#ifndef PICRIN_REG_H
#define PICRIN_REG_H
#if defined(__cplusplus)
extern "C" {
#endif
KHASH_DECLARE(reg, void *, pic_value)
struct pic_reg {
PIC_OBJECT_HEADER
khash_t(reg) hash;
struct pic_reg *prev; /* for GC */
};
#define pic_reg_p(v) (pic_type(v) == PIC_TT_REG)
#define pic_reg_ptr(v) ((struct pic_reg *)pic_ptr(v))
struct pic_reg *pic_make_reg(pic_state *);
pic_value pic_reg_ref(pic_state *, struct pic_reg *, void *);
void pic_reg_set(pic_state *, struct pic_reg *, void *, pic_value);
void pic_reg_del(pic_state *, struct pic_reg *, void *);
bool pic_reg_has(pic_state *, struct pic_reg *, void *);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -154,7 +154,7 @@ enum pic_tt {
PIC_TT_LIB,
PIC_TT_DATA,
PIC_TT_DICT,
PIC_TT_REG,
PIC_TT_WEAK,
PIC_TT_RECORD,
PIC_TT_CXT,
PIC_TT_CP
@ -308,8 +308,8 @@ pic_type_repr(enum pic_tt tt)
return "data";
case PIC_TT_DICT:
return "dict";
case PIC_TT_REG:
return "reg";
case PIC_TT_WEAK:
return "weak";
case PIC_TT_RECORD:
return "record";
case PIC_TT_CP:

View File

@ -0,0 +1,34 @@
/**
* See Copyright Notice in picrin.h
*/
#ifndef PICRIN_WEAK_H
#define PICRIN_WEAK_H
#if defined(__cplusplus)
extern "C" {
#endif
KHASH_DECLARE(weak, void *, pic_value)
struct pic_weak {
PIC_OBJECT_HEADER
khash_t(weak) hash;
struct pic_weak *prev; /* for GC */
};
#define pic_weak_p(v) (pic_type(v) == PIC_TT_WEAK)
#define pic_weak_ptr(v) ((struct pic_weak *)pic_ptr(v))
struct pic_weak *pic_make_weak(pic_state *);
pic_value pic_weak_ref(pic_state *, struct pic_weak *, void *);
void pic_weak_set(pic_state *, struct pic_weak *, void *, pic_value);
void pic_weak_del(pic_state *, struct pic_weak *, void *);
bool pic_weak_has(pic_state *, struct pic_weak *, void *);
#if defined(__cplusplus)
}
#endif
#endif

View File

@ -120,26 +120,26 @@ pic_lookup_identifier(pic_state *pic, pic_id *id, struct pic_env *env)
static void
define_macro(pic_state *pic, pic_sym *uid, struct pic_proc *mac)
{
if (pic_reg_has(pic, pic->macros, uid)) {
if (pic_weak_has(pic, pic->macros, uid)) {
pic_warnf(pic, "redefining syntax variable: ~s", pic_obj_value(uid));
}
pic_reg_set(pic, pic->macros, uid, pic_obj_value(mac));
pic_weak_set(pic, pic->macros, uid, pic_obj_value(mac));
}
static struct pic_proc *
find_macro(pic_state *pic, pic_sym *uid)
{
if (! pic_reg_has(pic, pic->macros, uid)) {
if (! pic_weak_has(pic, pic->macros, uid)) {
return NULL;
}
return pic_proc_ptr(pic_reg_ref(pic, pic->macros, uid));
return pic_proc_ptr(pic_weak_ref(pic, pic->macros, uid));
}
static void
shadow_macro(pic_state *pic, pic_sym *uid)
{
if (pic_reg_has(pic, pic->macros, uid)) {
pic_reg_del(pic, pic->macros, uid);
if (pic_weak_has(pic, pic->macros, uid)) {
pic_weak_del(pic, pic->macros, uid);
}
}

View File

@ -178,21 +178,21 @@ pic_get_args(pic_state *pic, const char *format, ...)
static pic_value
vm_gref(pic_state *pic, pic_sym *uid)
{
if (! pic_reg_has(pic, pic->globals, uid)) {
pic_reg_set(pic, pic->globals, uid, pic_invalid_value());
if (! pic_weak_has(pic, pic->globals, uid)) {
pic_weak_set(pic, pic->globals, uid, pic_invalid_value());
pic_errorf(pic, "uninitialized global variable: %s", pic_symbol_name(pic, uid));
return pic_invalid_value();
}
return pic_reg_ref(pic, pic->globals, uid);
return pic_weak_ref(pic, pic->globals, uid);
}
static void
vm_gset(pic_state *pic, pic_sym *uid, pic_value value)
{
pic_reg_set(pic, pic->globals, uid, value);
pic_weak_set(pic, pic->globals, uid, value);
}
static void
@ -909,7 +909,7 @@ pic_define_(pic_state *pic, const char *name, pic_value val)
if ((uid = pic_find_identifier(pic, (pic_id *)sym, pic->lib->env)) == NULL) {
uid = pic_add_identifier(pic, (pic_id *)sym, pic->lib->env);
} else {
if (pic_reg_has(pic, pic->globals, uid)) {
if (pic_weak_has(pic, pic->globals, uid)) {
pic_warnf(pic, "redefining variable: ~s", pic_obj_value(uid));
}
}

View File

@ -108,7 +108,7 @@ void pic_init_dict(pic_state *);
void pic_init_record(pic_state *);
void pic_init_eval(pic_state *);
void pic_init_lib(pic_state *);
void pic_init_reg(pic_state *);
void pic_init_weak(pic_state *);
extern const char pic_boot[][80];
@ -170,7 +170,7 @@ pic_init_core(pic_state *pic)
pic_init_record(pic); DONE;
pic_init_eval(pic); DONE;
pic_init_lib(pic); DONE;
pic_init_reg(pic); DONE;
pic_init_weak(pic); DONE;
pic_defun(pic, "features", pic_features);
@ -329,8 +329,8 @@ pic_open(pic_allocf allocf, void *userdata)
pic_gc_arena_restore(pic, ai);
/* root tables */
pic->globals = pic_make_reg(pic);
pic->macros = pic_make_reg(pic);
pic->globals = pic_make_weak(pic);
pic->macros = pic_make_weak(pic);
/* root block */
pic->cp = (pic_checkpoint *)pic_obj_alloc(pic, sizeof(pic_checkpoint), PIC_TT_CP);
@ -342,7 +342,7 @@ pic_open(pic_allocf allocf, void *userdata)
pic_reader_init(pic);
/* parameter table */
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_reg(pic)), pic->ptable);
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable);
/* standard libraries */
pic->PICRIN_BASE = pic_make_library(pic, pic_read_cstr(pic, "(picrin base)"));

View File

@ -17,12 +17,12 @@ static pic_value
var_get(pic_state *pic, struct pic_proc *var)
{
pic_value elem, it;
struct pic_reg *reg;
struct pic_weak *weak;
pic_for_each (elem, pic->ptable, it) {
reg = pic_reg_ptr(elem);
if (pic_reg_has(pic, reg, var)) {
return pic_reg_ref(pic, reg, var);
weak = pic_weak_ptr(elem);
if (pic_weak_has(pic, weak, var)) {
return pic_weak_ref(pic, weak, var);
}
}
pic_panic(pic, "logic flaw");
@ -31,11 +31,11 @@ var_get(pic_state *pic, struct pic_proc *var)
static pic_value
var_set(pic_state *pic, struct pic_proc *var, pic_value val)
{
struct pic_reg *reg;
struct pic_weak *weak;
reg = pic_reg_ptr(pic_car(pic, pic->ptable));
weak = pic_weak_ptr(pic_car(pic, pic->ptable));
pic_reg_set(pic, reg, var, val);
pic_weak_set(pic, weak, var, val);
return pic_undef_value();
}
@ -91,7 +91,7 @@ pic_var_with_parameter(pic_state *pic)
pic_get_args(pic, "l", &body);
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_reg(pic)), pic->ptable);
pic->ptable = pic_cons(pic, pic_obj_value(pic_make_weak(pic)), pic->ptable);
val = pic_apply0(pic, body);

View File

@ -4,27 +4,27 @@
#include "picrin.h"
KHASH_DEFINE(reg, void *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
KHASH_DEFINE(weak, void *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
struct pic_reg *
pic_make_reg(pic_state *pic)
struct pic_weak *
pic_make_weak(pic_state *pic)
{
struct pic_reg *reg;
struct pic_weak *weak;
reg = (struct pic_reg *)pic_obj_alloc(pic, sizeof(struct pic_reg), PIC_TT_REG);
reg->prev = NULL;
kh_init(reg, &reg->hash);
weak = (struct pic_weak *)pic_obj_alloc(pic, sizeof(struct pic_weak), PIC_TT_WEAK);
weak->prev = NULL;
kh_init(weak, &weak->hash);
return reg;
return weak;
}
pic_value
pic_reg_ref(pic_state *pic, struct pic_reg *reg, void *key)
pic_weak_ref(pic_state *pic, struct pic_weak *weak, void *key)
{
khash_t(reg) *h = &reg->hash;
khash_t(weak) *h = &weak->hash;
khiter_t it;
it = kh_get(reg, h, key);
it = kh_get(weak, h, key);
if (it == kh_end(h)) {
pic_errorf(pic, "element not found for a key: ~s", pic_obj_value(key));
}
@ -32,9 +32,9 @@ pic_reg_ref(pic_state *pic, struct pic_reg *reg, void *key)
}
void *
pic_reg_rev_ref(pic_state *pic, struct pic_reg *reg, pic_value val)
pic_weak_rev_ref(pic_state *pic, struct pic_weak *weak, pic_value val)
{
khash_t(reg) *h = &reg->hash;
khash_t(weak) *h = &weak->hash;
if (h->n_buckets) {
khint_t i = 0;
@ -48,64 +48,64 @@ pic_reg_rev_ref(pic_state *pic, struct pic_reg *reg, pic_value val)
}
void
pic_reg_set(pic_state PIC_UNUSED(*pic), struct pic_reg *reg, void *key, pic_value val)
pic_weak_set(pic_state PIC_UNUSED(*pic), struct pic_weak *weak, void *key, pic_value val)
{
khash_t(reg) *h = &reg->hash;
khash_t(weak) *h = &weak->hash;
int ret;
khiter_t it;
it = kh_put(reg, h, key, &ret);
it = kh_put(weak, h, key, &ret);
kh_val(h, it) = val;
}
bool
pic_reg_has(pic_state PIC_UNUSED(*pic), struct pic_reg *reg, void *key)
pic_weak_has(pic_state PIC_UNUSED(*pic), struct pic_weak *weak, void *key)
{
return kh_get(reg, &reg->hash, key) != kh_end(&reg->hash);
return kh_get(weak, &weak->hash, key) != kh_end(&weak->hash);
}
void
pic_reg_del(pic_state *pic, struct pic_reg *reg, void *key)
pic_weak_del(pic_state *pic, struct pic_weak *weak, void *key)
{
khash_t(reg) *h = &reg->hash;
khash_t(weak) *h = &weak->hash;
khiter_t it;
it = kh_get(reg, h, key);
it = kh_get(weak, h, key);
if (it == kh_end(h)) {
pic_errorf(pic, "no slot named ~s found in register", pic_obj_value(key));
}
kh_del(reg, h, it);
kh_del(weak, h, it);
}
static pic_value
reg_get(pic_state *pic, struct pic_reg *reg, void *key)
weak_get(pic_state *pic, struct pic_weak *weak, void *key)
{
if (! pic_reg_has(pic, reg, key)) {
if (! pic_weak_has(pic, weak, key)) {
return pic_false_value();
}
return pic_cons(pic, pic_obj_value(key), pic_reg_ref(pic, reg, key));
return pic_cons(pic, pic_obj_value(key), pic_weak_ref(pic, weak, key));
}
static pic_value
reg_set(pic_state *pic, struct pic_reg *reg, void *key, pic_value val)
weak_set(pic_state *pic, struct pic_weak *weak, void *key, pic_value val)
{
if (pic_undef_p(val)) {
if (pic_reg_has(pic, reg, key)) {
pic_reg_del(pic, reg, key);
if (pic_weak_has(pic, weak, key)) {
pic_weak_del(pic, weak, key);
}
} else {
pic_reg_set(pic, reg, key, val);
pic_weak_set(pic, weak, key, val);
}
return pic_undef_value();
}
static pic_value
reg_call(pic_state *pic)
weak_call(pic_state *pic)
{
struct pic_proc *self;
struct pic_reg *reg;
struct pic_weak *weak;
pic_value key, val;
int n;
@ -115,34 +115,34 @@ reg_call(pic_state *pic)
pic_errorf(pic, "attempted to set a non-object key '~s' in a register", key);
}
reg = pic_reg_ptr(pic_proc_env_ref(pic, self, "reg"));
weak = pic_weak_ptr(pic_proc_env_ref(pic, self, "weak"));
if (n == 1) {
return reg_get(pic, reg, pic_obj_ptr(key));
return weak_get(pic, weak, pic_obj_ptr(key));
} else {
return reg_set(pic, reg, pic_obj_ptr(key), val);
return weak_set(pic, weak, pic_obj_ptr(key), val);
}
}
static pic_value
pic_reg_make_register(pic_state *pic)
pic_weak_make_register(pic_state *pic)
{
struct pic_reg *reg;
struct pic_weak *weak;
struct pic_proc *proc;
pic_get_args(pic, "");
reg = pic_make_reg(pic);
weak = pic_make_weak(pic);
proc = pic_make_proc(pic, reg_call);
proc = pic_make_proc(pic, weak_call);
pic_proc_env_set(pic, proc, "reg", pic_obj_value(reg));
pic_proc_env_set(pic, proc, "weak", pic_obj_value(weak));
return pic_obj_value(proc);
}
void
pic_init_reg(pic_state *pic)
pic_init_weak(pic_state *pic)
{
pic_defun(pic, "make-register", pic_reg_make_register);
pic_defun(pic, "make-register", pic_weak_make_register);
}