From 85a766a24250a4a757e096843cc79e3370cb3ca1 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 20 Feb 2014 17:41:59 +0900 Subject: [PATCH] adjust stack pointer to the next to the initial position after calling function --- src/cont.c | 18 +++++++++--------- src/vm.c | 11 ++++++----- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/cont.c b/src/cont.c index 6e7dc00b..f89c9728 100644 --- a/src/cont.c +++ b/src/cont.c @@ -181,13 +181,13 @@ pic_values(pic_state *pic, size_t c, ...) va_start(ap, c); for (i = 0; i < c; ++i) { - *pic->sp++ = va_arg(ap, pic_value); + pic->sp[i] = va_arg(ap, pic_value); } pic->ci->retc = c; va_end(ap); - return c == 0 ? pic_none_value() : pic->sp[-c]; + return c == 0 ? pic_none_value() : pic->sp[0]; } pic_value @@ -196,26 +196,26 @@ pic_values_by_array(pic_state *pic, size_t argc, pic_value *argv) size_t i; for (i = 0; i < argc; ++i) { - *pic->sp++ = argv[i]; + pic->sp[i] = argv[i]; } pic->ci->retc = argc; - return argc == 0 ? pic_none_value() : pic->sp[-argc]; + return argc == 0 ? pic_none_value() : pic->sp[0]; } pic_value pic_values_by_list(pic_state *pic, pic_value list) { pic_value v; - size_t c; + size_t i; - c = pic_length(pic, list); + i = 0; pic_for_each (v, list) { - *pic->sp++ = v; + pic->sp[i++] = v; } - pic->ci->retc = c; + pic->ci->retc = i; - return pic_nil_p(list) ? pic_none_value() : pic->sp[-c]; + return pic_nil_p(list) ? pic_none_value() : pic->sp[0]; } size_t diff --git a/src/vm.c b/src/vm.c index 48823f23..b97061a3 100644 --- a/src/vm.c +++ b/src/vm.c @@ -572,10 +572,8 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) if (pic_proc_cfunc_p(x)) { /* invoke! */ - v = proc->u.cfunc(pic); - if (ci->retc == 1 && pic->sp == ci->fp + c.u.i) { - PUSH(v); - } + pic->sp[0] = proc->u.cfunc(pic); + pic->sp += ci->retc; pic_gc_arena_restore(pic, ai); goto L_RET; @@ -656,11 +654,14 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) L_RET: retc = pic->ci->retc; retv = pic->sp - retc; + if (retc == 0) { + pic->ci->fp[0] = retv[0]; /* copy at least once */ + } for (i = 0; i < retc; ++i) { pic->ci->fp[i] = retv[i]; } ci = POPCI(); - pic->sp = ci->fp + retc; + pic->sp = ci->fp + 1; /* advance only one! */ pic->ip = ci->ip; NEXT;