From 711b53eb72540a6219afa65f38b4b085251d56dd Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 23 Jun 2014 00:52:36 +0900 Subject: [PATCH 1/3] fix #140 --- src/vm.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/vm.c b/src/vm.c index c2d0b1e0..8b7c51f1 100644 --- a/src/vm.c +++ b/src/vm.c @@ -747,7 +747,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) /* invoke! */ pic->sp[0] = proc->u.func.f(pic); - pic->sp += ci->retc; + pic->sp += pic->ci->retc; pic_gc_arena_restore(pic, ai); goto L_RET; @@ -1002,21 +1002,23 @@ static pic_code trampoline_iseq[] = { pic_value pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) { - pic_value v, call_list, *fp = pic->ci->fp; + pic_value v, *sp; pic_callinfo *ci; - call_list = pic_cons(pic, pic_obj_value(proc), args); + *pic->sp++ = pic_obj_value(proc); - pic_for_each (v, call_list) { - *fp++ = v; + sp = pic->sp; + pic_for_each (v, args) { + *sp++ = v; } - trampoline_iseq[1].u.i = pic_length(pic, call_list); + trampoline_iseq[1].u.i = -1; ci = PUSHCI(); ci->ip = trampoline_iseq; - ci->fp = fp - 1; /* the last argument is pushed by the VM */ - return v; + ci->fp = pic->sp; + ci->retc = pic_length(pic, args); + return pic_obj_value(proc); } pic_value From fa179dc1528b1d599004012f10bdd3bd44ff2dbb Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 23 Jun 2014 00:54:11 +0900 Subject: [PATCH 2/3] trampoline_iseq is no longer mutable --- src/vm.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/vm.c b/src/vm.c index 8b7c51f1..1fe153a4 100644 --- a/src/vm.c +++ b/src/vm.c @@ -994,14 +994,11 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) } VM_LOOP_END; } -static pic_code trampoline_iseq[] = { - { OP_NOP, {0} }, - { OP_TAILCALL, {0} }, -}; - pic_value pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) { + static const pic_code iseq[] = { { OP_NOP, {0} }, { OP_TAILCALL, { .i = -1 } } }; + pic_value v, *sp; pic_callinfo *ci; @@ -1012,10 +1009,8 @@ pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) *sp++ = v; } - trampoline_iseq[1].u.i = -1; - ci = PUSHCI(); - ci->ip = trampoline_iseq; + ci->ip = (pic_code *)iseq; ci->fp = pic->sp; ci->retc = pic_length(pic, args); return pic_obj_value(proc); From 4c78e0694a940ccb187fc529577bbd28610538a9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 23 Jun 2014 00:56:43 +0900 Subject: [PATCH 3/3] shrink trampoline iseq --- src/vm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vm.c b/src/vm.c index 1fe153a4..9e4509f4 100644 --- a/src/vm.c +++ b/src/vm.c @@ -997,7 +997,7 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) pic_value pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) { - static const pic_code iseq[] = { { OP_NOP, {0} }, { OP_TAILCALL, { .i = -1 } } }; + static const pic_code iseq = { OP_TAILCALL, { .i = -1 } }; pic_value v, *sp; pic_callinfo *ci; @@ -1010,7 +1010,7 @@ pic_apply_trampoline(pic_state *pic, struct pic_proc *proc, pic_value args) } ci = PUSHCI(); - ci->ip = (pic_code *)iseq; + ci->ip = (pic_code *)&iseq - 1; ci->fp = pic->sp; ci->retc = pic_length(pic, args); return pic_obj_value(proc);