From 7217baee0a09297899718afd3cbf3764b4aa0add Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 20 Feb 2014 16:33:18 +0900 Subject: [PATCH] add coarity register to the VM --- include/picrin.h | 2 +- src/cont.c | 33 ++++++++++++++++----------------- src/vm.c | 21 +++++++++++++++++---- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index fbdf6602..27c1e210 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -60,7 +60,7 @@ extern "C" { struct pic_code; typedef struct { - int argc; + int argc, retc; struct pic_code *ip; pic_value *fp; struct pic_env *env; diff --git a/src/cont.c b/src/cont.c index 578f83a6..92daf1f0 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->ci->fp[i] = va_arg(ap, pic_value); + *pic->sp++ = va_arg(ap, pic_value); } - pic->ci->fp[i] = pic_undef_value(); + pic->ci->retc = c; va_end(ap); - return c == 0 ? pic_none_value() : pic->ci->fp[0]; + return c == 0 ? pic_none_value() : pic->sp[-c]; } pic_value @@ -196,45 +196,44 @@ pic_values_by_array(pic_state *pic, size_t argc, pic_value *argv) size_t i; for (i = 0; i < argc; ++i) { - pic->ci->fp[i] = argv[i]; + *pic->sp++ = argv[i]; } - pic->ci->fp[i] = pic_undef_value(); + pic->ci->retc = argc; - return argc == 0 ? pic_none_value() : pic->ci->fp[0]; + return argc == 0 ? pic_none_value() : pic->sp[-argc]; } pic_value pic_values_by_list(pic_state *pic, pic_value list) { pic_value v; - size_t i; + size_t c; - i = 0; + c = pic_length(pic, list); pic_for_each (v, list) { - pic->ci->fp[i++] = v; + *pic->sp++ = v; } - pic->ci->fp[i] = pic_undef_value(); + pic->ci->retc = c; - return pic_nil_p(list) ? pic_none_value() : pic->ci->fp[0]; + return pic_nil_p(list) ? pic_none_value() : pic->sp[-c]; } size_t pic_receive(pic_state *pic, size_t n, pic_value *argv) { pic_callinfo *ci; - size_t i; + size_t i, retc; - /* take info from already discarded frame */ + /* take info from discarded frame */ ci = pic->ci + 1; + retc = ci->retc; - for (i = 0; ; ++i) { - if (pic_undef_p(ci->fp[i])) - break; + for (i = 0; i < retc; ++i) { if (i < n) { argv[i] = ci->fp[i]; } } - return i; + return retc; } static pic_value diff --git a/src/vm.c b/src/vm.c index 89b11c42..f60eeeb1 100644 --- a/src/vm.c +++ b/src/vm.c @@ -566,15 +566,26 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) ci = PUSHCI(); ci->argc = c.u.i; + ci->retc = 1; ci->ip = pic->ip; ci->fp = pic->sp - c.u.i; ci->env = NULL; if (pic_proc_cfunc_p(x)) { - v = proc->u.cfunc(pic); - ci = POPCI(); + int i, retc; + pic_value *retv; + + /* invoke! */ + PUSH(proc->u.cfunc(pic)); + + retc = ci->retc; + retv = pic->sp - retc; + for (i = 0; i < retc; ++i) { + pic->ci->fp[i] = retv[i]; + } + ci = POPCI(); + pic->sp = ci->fp + retc; pic->ip = ci->ip; - pic->sp = ci->fp; - PUSH(v); + pic_gc_arena_restore(pic, ai); NEXT; } @@ -649,6 +660,8 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) goto L_STOP; } + pic->ci->retc = c.u.i; + retc = c.u.i; retv = pic->sp - retc; for (i = 0; i < retc; ++i) {