diff --git a/cont.c b/cont.c index 695e39b0..c542399d 100644 --- a/cont.c +++ b/cont.c @@ -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); } diff --git a/gc.c b/gc.c index 38be3150..15304788 100644 --- a/gc.c +++ b/gc.c @@ -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: diff --git a/include/picrin.h b/include/picrin.h index 3df0c538..5bec9eb0 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -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; diff --git a/include/picrin/cont.h b/include/picrin/cont.h index eeabb798..587a46d1 100644 --- a/include/picrin/cont.h +++ b/include/picrin/cont.h @@ -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; diff --git a/include/picrin/value.h b/include/picrin/value.h index 6a211dc1..453e645a 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -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(); } diff --git a/state.c b/state.c index b88f40a1..df9ba02c 100644 --- a/state.c +++ b/state.c @@ -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 */