pic_block -> pic_winder

This commit is contained in:
Yuichi Nishiwaki 2014-09-17 15:09:15 +09:00
parent 06971a1144
commit b4c3f4fb73
6 changed files with 49 additions and 57 deletions

26
cont.c
View File

@ -121,7 +121,7 @@ save_cont(pic_state *pic, struct pic_cont **c)
cont = *c = (struct pic_cont *)pic_obj_alloc(pic, sizeof(struct pic_cont), PIC_TT_CONT);
cont->blk = pic->blk;
cont->wind = pic->wind;
cont->stk_len = native_stack_length(pic, &pos);
cont->stk_pos = pos;
@ -168,7 +168,6 @@ restore_cont(pic_state *pic, struct pic_cont *cont)
{
char v;
struct pic_cont *tmp = cont;
struct pic_block *blk;
if (&v < pic->native_stack_start) {
if (&v > cont->stk_pos) native_stack_extend(pic, cont);
@ -177,8 +176,7 @@ restore_cont(pic_state *pic, struct pic_cont *cont)
if (&v > cont->stk_pos + cont->stk_len) native_stack_extend(pic, cont);
}
blk = pic->blk;
pic->blk = cont->blk;
pic->wind = cont->wind;
pic->stbase = (pic_value *)pic_realloc(pic, pic->stbase, sizeof(pic_value) * cont->st_len);
memcpy(pic->stbase, cont->st_ptr, sizeof(pic_value) * cont->st_len);
@ -208,7 +206,7 @@ restore_cont(pic_state *pic, struct pic_cont *cont)
}
static void
walk_to_block(pic_state *pic, struct pic_block *here, struct pic_block *there)
walk_to_block(pic_state *pic, struct pic_winder *here, struct pic_winder *there)
{
if (here == there)
return;
@ -226,23 +224,23 @@ walk_to_block(pic_state *pic, struct pic_block *here, struct pic_block *there)
static pic_value
pic_dynamic_wind(pic_state *pic, struct pic_proc *in, struct pic_proc *thunk, struct pic_proc *out)
{
struct pic_block *here;
struct pic_winder *here;
pic_value val;
if (in != NULL) {
pic_apply0(pic, in); /* enter */
}
here = pic->blk;
pic->blk = (struct pic_block *)pic_obj_alloc(pic, sizeof(struct pic_block), PIC_TT_BLK);
pic->blk->prev = here;
pic->blk->depth = here->depth + 1;
pic->blk->in = in;
pic->blk->out = out;
here = pic->wind;
pic->wind = pic_alloc(pic, sizeof(struct pic_winder));
pic->wind->prev = here;
pic->wind->depth = here->depth + 1;
pic->wind->in = in;
pic->wind->out = out;
val = pic_apply0(pic, thunk);
pic->blk = here;
pic->wind = here;
if (out != NULL) {
pic_apply0(pic, out); /* exit */
@ -266,7 +264,7 @@ cont_call(pic_state *pic)
cont->results = pic_list_by_array(pic, argc, argv);
/* execute guard handlers */
walk_to_block(pic, pic->blk, cont->blk);
walk_to_block(pic, pic->wind, cont->wind);
restore_cont(pic, cont);
}

41
gc.c
View File

@ -333,6 +333,20 @@ gc_unmark(union header *p)
p->s.mark = PIC_GC_UNMARK;
}
static void
gc_mark_winder(pic_state *pic, struct pic_winder *wind)
{
if (wind->prev) {
gc_mark_object(pic, (struct pic_object *)wind->prev);
}
if (wind->in) {
gc_mark_object(pic, (struct pic_object *)wind->in);
}
if (wind->out) {
gc_mark_object(pic, (struct pic_object *)wind->out);
}
}
static void
gc_mark_object(pic_state *pic, struct pic_object *obj)
{
@ -404,8 +418,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
pic_callinfo *ci;
size_t i;
/* block */
gc_mark_object(pic, (struct pic_object *)cont->blk);
/* winder */
gc_mark_winder(pic, cont->wind);
/* stack */
for (stack = cont->st_ptr; stack != cont->st_ptr + cont->sp_offset; ++stack) {
@ -504,20 +518,6 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
}
break;
}
case PIC_TT_BLK: {
struct pic_block *blk = (struct pic_block *)obj;
if (blk->prev) {
gc_mark_object(pic, (struct pic_object *)blk->prev);
}
if (blk->in) {
gc_mark_object(pic, (struct pic_object *)blk->in);
}
if (blk->out) {
gc_mark_object(pic, (struct pic_object *)blk->out);
}
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
@ -565,9 +565,9 @@ gc_mark_phase(pic_state *pic)
size_t i, j;
xh_entry *it;
/* block */
if (pic->blk) {
gc_mark_object(pic, (struct pic_object *)pic->blk);
/* winder */
if (pic->wind) {
gc_mark_winder(pic, pic->wind);
}
/* stack */
@ -709,9 +709,6 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
xh_destroy(&rec->hash);
break;
}
case PIC_TT_BLK: {
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:

View File

@ -46,6 +46,13 @@ extern "C" {
typedef struct pic_code pic_code;
struct pic_winder {
struct pic_proc *in;
struct pic_proc *out;
int depth;
struct pic_winder *prev;
};
typedef struct {
int argc, retc;
pic_code *ip;
@ -60,7 +67,7 @@ typedef struct {
int argc;
char **argv, **envp;
struct pic_block *blk;
struct pic_winder *wind;
pic_value *sp;
pic_value *stbase, *stend;

View File

@ -9,18 +9,11 @@
extern "C" {
#endif
struct pic_block {
PIC_OBJECT_HEADER
struct pic_block *prev;
int depth;
struct pic_proc *in, *out;
};
struct pic_cont {
PIC_OBJECT_HEADER
jmp_buf jmp;
struct pic_block *blk;
struct pic_winder *wind;
char *stk_pos, *stk_ptr;
ptrdiff_t stk_len;

View File

@ -125,7 +125,6 @@ enum pic_tt {
PIC_TT_DATA,
PIC_TT_DICT,
PIC_TT_RECORD,
PIC_TT_BLK,
};
#define PIC_OBJECT_HEADER \
@ -274,8 +273,6 @@ pic_type_repr(enum pic_tt tt)
return "dict";
case PIC_TT_RECORD:
return "record";
case PIC_TT_BLK:
return "block";
}
UNREACHABLE();
}

18
state.c
View File

@ -27,7 +27,7 @@ pic_open(int argc, char *argv[], char **envp)
pic = malloc(sizeof(pic_state));
/* root block */
pic->blk = NULL;
pic->wind = NULL;
/* command line */
pic->argc = argc;
@ -153,10 +153,10 @@ pic_open(int argc, char *argv[], char **envp)
pic_gc_arena_restore(pic, ai);
/* root block */
pic->blk = (struct pic_block *)pic_obj_alloc(pic, sizeof(struct pic_block), PIC_TT_BLK);
pic->blk->prev = NULL;
pic->blk->depth = 0;
pic->blk->in = pic->blk->out = NULL;
pic->wind = pic_alloc(pic, sizeof(struct pic_winder));
pic->wind->prev = NULL;
pic->wind->depth = 0;
pic->wind->in = pic->wind->out = NULL;
/* init readers */
pic_init_reader(pic);
@ -182,11 +182,11 @@ pic_close(pic_state *pic)
xh_entry *it;
/* invoke exit handlers */
while (pic->blk) {
if (pic->blk->out) {
pic_apply0(pic, pic->blk->out);
while (pic->wind) {
if (pic->wind->out) {
pic_apply0(pic, pic->wind->out);
}
pic->blk = pic->blk->prev;
pic->wind = pic->wind->prev;
}
/* clear out root objects */