From c92a672e01cdcb0ce4c976c32fee5acf69c7de05 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 17 Oct 2013 23:29:18 +0900 Subject: [PATCH] change the stack layout: to hold proc object on the stack when procedure call is invoked --- include/picrin.h | 1 - src/gc.c | 6 +----- src/vm.c | 27 ++++++++++++++------------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 06d08720..7a718c83 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -10,7 +10,6 @@ struct pic_code; typedef struct pic_callinfo { - struct pic_proc *proc; int argc; struct pic_code *pc; pic_value *sp; diff --git a/src/gc.c b/src/gc.c index 436fbcc6..30fdf2f3 100644 --- a/src/gc.c +++ b/src/gc.c @@ -177,11 +177,7 @@ gc_mark_phase(pic_state *pic) for (stack = pic->stbase; stack != pic->sp; ++stack) { gc_mark(pic, *stack); } - - /* callinfo */ - for (ci = pic->cibase; ci != pic->ci; ++ci) { - gc_mark_object(pic, (struct pic_object *)ci->proc); - } + gc_mark(pic, *stack); /* arena */ for (i = 0; i < pic->arena_idx; ++i) { diff --git a/src/vm.c b/src/vm.c index 910c2b73..0af07182 100644 --- a/src/vm.c +++ b/src/vm.c @@ -406,7 +406,7 @@ pic_gen_call(pic_state *pic, struct pic_irep *irep, pic_value obj, struct pic_en ++i; } irep->code[irep->clen].insn = OP_CALL; - irep->code[irep->clen].u.i = i - 1; + irep->code[irep->clen].u.i = i; irep->clen++; } @@ -476,17 +476,16 @@ pic_codegen(pic_state *pic, pic_value obj, struct pic_env *env) # define VM_LOOP_END } } #endif -#define PUSH(v) (*pic->sp++ = (v)) -#define POP() (*--pic->sp) +#define PUSH(v) (*++pic->sp = (v)) +#define POP() (*pic->sp--) -#define PUSHCI() (pic->ci++) -#define POPCI() (--pic->ci) +#define PUSHCI() (++pic->ci) +#define POPCI() (pic->ci--) pic_value pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) { struct pic_code *pc; - pic_callinfo *ci; pic_value val; int ai = pic_gc_arena_preserve(pic); @@ -501,9 +500,11 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) pc = proc->u.irep->code; - ci = PUSHCI(); - ci->proc = proc; - ci->argc = 0; + /* adjust call frame */ + pic->sp[0] = pic_obj_value(proc); + pic->ci->argc = 0; + pic->ci->pc = NULL; + pic->ci->sp = NULL; VM_LOOP { CASE(OP_PUSHNIL) { @@ -531,7 +532,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) NEXT; } CASE(OP_LREF) { - PUSH(pic->ci[-1].sp[pc->u.i]); + PUSH(pic->ci->sp[pc->u.i]); NEXT; } CASE(OP_JMP) { @@ -550,12 +551,12 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) } CASE(OP_CALL) { pic_value c, v; + pic_callinfo *ci; struct pic_proc *proc; - pic_gc_protect(pic, c = POP()); + c = pic->sp[0]; proc = pic_proc_ptr(c); ci = PUSHCI(); - ci->proc = proc; ci->argc = pc->u.i; ci->pc = pc; ci->sp = pic->sp; @@ -575,6 +576,7 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) } CASE(OP_RET) { pic_value v; + pic_callinfo *ci; v = POP(); pic->sp -= ci->argc; @@ -636,7 +638,6 @@ pic_run(pic_state *pic, struct pic_proc *proc, pic_value args) } VM_LOOP_END; STOP: - POPCI(); val = POP(); #if GC_DEBUG