adjust stack pointer to the next to the initial position after calling function

This commit is contained in:
Yuichi Nishiwaki 2014-02-20 17:41:59 +09:00
parent 213177eb10
commit 85a766a242
2 changed files with 15 additions and 14 deletions

View File

@ -181,13 +181,13 @@ pic_values(pic_state *pic, size_t c, ...)
va_start(ap, c); va_start(ap, c);
for (i = 0; i < c; ++i) { 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; pic->ci->retc = c;
va_end(ap); va_end(ap);
return c == 0 ? pic_none_value() : pic->sp[-c]; return c == 0 ? pic_none_value() : pic->sp[0];
} }
pic_value pic_value
@ -196,26 +196,26 @@ pic_values_by_array(pic_state *pic, size_t argc, pic_value *argv)
size_t i; size_t i;
for (i = 0; i < argc; ++i) { for (i = 0; i < argc; ++i) {
*pic->sp++ = argv[i]; pic->sp[i] = argv[i];
} }
pic->ci->retc = argc; 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_value
pic_values_by_list(pic_state *pic, pic_value list) pic_values_by_list(pic_state *pic, pic_value list)
{ {
pic_value v; pic_value v;
size_t c; size_t i;
c = pic_length(pic, list); i = 0;
pic_for_each (v, list) { 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 size_t

View File

@ -572,10 +572,8 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv)
if (pic_proc_cfunc_p(x)) { if (pic_proc_cfunc_p(x)) {
/* invoke! */ /* invoke! */
v = proc->u.cfunc(pic); pic->sp[0] = proc->u.cfunc(pic);
if (ci->retc == 1 && pic->sp == ci->fp + c.u.i) { pic->sp += ci->retc;
PUSH(v);
}
pic_gc_arena_restore(pic, ai); pic_gc_arena_restore(pic, ai);
goto L_RET; goto L_RET;
@ -656,11 +654,14 @@ pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv)
L_RET: L_RET:
retc = pic->ci->retc; retc = pic->ci->retc;
retv = pic->sp - retc; retv = pic->sp - retc;
if (retc == 0) {
pic->ci->fp[0] = retv[0]; /* copy at least once */
}
for (i = 0; i < retc; ++i) { for (i = 0; i < retc; ++i) {
pic->ci->fp[i] = retv[i]; pic->ci->fp[i] = retv[i];
} }
ci = POPCI(); ci = POPCI();
pic->sp = ci->fp + retc; pic->sp = ci->fp + 1; /* advance only one! */
pic->ip = ci->ip; pic->ip = ci->ip;
NEXT; NEXT;