picrin/src/cont.c

319 lines
7.3 KiB
C
Raw Normal View History

2014-01-17 06:58:31 -05:00
/**
* See Copyright Notice in picrin.h
*/
2013-11-09 00:14:25 -05:00
#include <setjmp.h>
#include <string.h>
#include <stdarg.h>
2013-11-09 00:14:25 -05:00
#include "picrin.h"
#include "picrin/proc.h"
#include "picrin/cont.h"
#include "picrin/pair.h"
2013-11-09 00:14:25 -05:00
static void save_cont(pic_state *, struct pic_cont **);
2013-11-09 00:14:25 -05:00
static void restore_cont(pic_state *, struct pic_cont *);
2013-11-09 01:29:58 -05:00
static size_t
native_stack_length(pic_state *pic, pic_value **pos)
{
pic_value t;
*pos = (pic->native_stack_start > &t)
? &t
: pic->native_stack_start;
return (pic->native_stack_start > &t)
? (size_t)(pic->native_stack_start - &t)
: (size_t)(&t - pic->native_stack_start + 1);
}
static void
save_cont(pic_state *pic, struct pic_cont **c)
2013-11-09 01:29:58 -05:00
{
struct pic_cont *cont;
pic_value *pos;
cont = *c = (struct pic_cont *)pic_obj_alloc(pic, sizeof(struct pic_cont), PIC_TT_CONT);
2013-11-09 01:29:58 -05:00
2013-11-11 04:04:21 -05:00
cont->blk = pic->blk;
PIC_BLK_INCREF(pic, cont->blk);
2013-11-11 04:04:21 -05:00
2013-11-09 01:29:58 -05:00
cont->stk_len = native_stack_length(pic, &pos);
cont->stk_pos = pos;
cont->stk_ptr = pic_alloc(pic, sizeof(pic_value) * cont->stk_len);
memcpy(cont->stk_ptr, cont->stk_pos, sizeof(pic_value) * cont->stk_len);
cont->sp_offset = pic->sp - pic->stbase;
2013-11-10 20:14:42 -05:00
cont->st_len = pic->stend - pic->stbase;
cont->st_ptr = (pic_value *)pic_alloc(pic, sizeof(pic_value) * cont->st_len);
memcpy(cont->st_ptr, pic->stbase, sizeof(pic_value) * cont->st_len);
2013-11-09 01:29:58 -05:00
cont->ci_offset = pic->ci - pic->cibase;
2013-11-10 20:14:42 -05:00
cont->ci_len = pic->ciend - pic->cibase;
cont->ci_ptr = (pic_callinfo *)pic_alloc(pic, sizeof(pic_callinfo) * cont->ci_len);
memcpy(cont->ci_ptr, pic->cibase, sizeof(pic_callinfo) * cont->ci_len);
2013-11-09 01:29:58 -05:00
2013-11-17 02:02:58 -05:00
cont->ridx = pic->ridx;
cont->rlen = pic->rlen;
cont->rescue = (struct pic_proc **)pic_alloc(pic, sizeof(struct pic_proc *) * cont->rlen);
memcpy(cont->rescue, pic->rescue, sizeof(struct pic_proc *) * cont->rlen);
2013-11-09 01:29:58 -05:00
cont->arena_idx = pic->arena_idx;
memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * PIC_ARENA_SIZE);
cont->results = pic_undef_value();
2013-11-09 01:29:58 -05:00
}
2013-11-09 00:14:25 -05:00
static void
native_stack_extend(pic_state *pic, struct pic_cont *cont)
{
volatile pic_value v[1024];
((void)v);
restore_cont(pic, cont);
}
2014-01-30 00:08:36 -05:00
NORETURN static void
2013-11-09 00:14:25 -05:00
restore_cont(pic_state *pic, struct pic_cont *cont)
{
pic_value v;
struct pic_cont *tmp = cont;
if (&v < pic->native_stack_start) {
if (&v > cont->stk_pos) native_stack_extend(pic, cont);
}
else {
if (&v > cont->stk_pos + cont->stk_len) native_stack_extend(pic, cont);
}
PIC_BLK_DECREF(pic, pic->blk);
PIC_BLK_INCREF(pic, cont->blk);
2013-11-11 04:04:21 -05:00
pic->blk = cont->blk;
pic->stbase = (pic_value *)pic_realloc(pic, pic->stbase, sizeof(pic_value) * cont->st_len);
2013-11-10 20:14:42 -05:00
memcpy(pic->stbase, cont->st_ptr, sizeof(pic_value) * cont->st_len);
pic->sp = pic->stbase + cont->sp_offset;
pic->stend = pic->stbase + cont->st_len;
2013-11-10 17:59:52 -05:00
pic->cibase = (pic_callinfo *)pic_realloc(pic, pic->cibase, sizeof(pic_callinfo) * cont->ci_len);
2013-11-10 20:14:42 -05:00
memcpy(pic->cibase, cont->ci_ptr, sizeof(pic_callinfo) * cont->ci_len);
pic->ci = pic->cibase + cont->ci_offset;
pic->ciend = pic->cibase + cont->ci_len;
2013-11-10 17:59:52 -05:00
2013-11-17 02:02:58 -05:00
pic->rescue = (struct pic_proc **)pic_realloc(pic, pic->rescue, sizeof(struct pic_proc *) * cont->rlen);
memcpy(pic->rescue, cont->rescue, sizeof(struct pic_object *) * cont->rlen);
pic->ridx = cont->ridx;
pic->rlen = cont->rlen;
2013-11-09 00:14:25 -05:00
memcpy(pic->arena, cont->arena, sizeof(struct pic_object *) * PIC_ARENA_SIZE);
pic->arena_idx = cont->arena_idx;
memcpy(cont->stk_pos, cont->stk_ptr, sizeof(pic_value) * cont->stk_len);
longjmp(tmp->jmp, 1);
}
2013-11-11 04:04:21 -05:00
static void
walk_to_block(pic_state *pic, struct pic_block *here, struct pic_block *there)
{
if (here == there)
return;
if (here->depth < there->depth) {
walk_to_block(pic, here, there->prev);
pic_apply_argv(pic, there->in, 0);
}
else {
pic_apply_argv(pic, there->out, 0);
walk_to_block(pic, here->prev, there);
}
}
2014-01-30 00:08:36 -05:00
NORETURN static pic_value
2013-11-09 01:25:19 -05:00
cont_call(pic_state *pic)
2013-11-09 00:14:25 -05:00
{
struct pic_proc *proc;
size_t argc;
pic_value *argv;
2013-11-09 00:14:25 -05:00
struct pic_cont *cont;
proc = pic_get_proc(pic);
pic_get_args(pic, "*", &argc, &argv);
2013-11-09 00:14:25 -05:00
cont = (struct pic_cont *)pic_ptr(proc->env->values[0]);
cont->results = pic_list_by_array(pic, argc, argv);
2013-11-09 00:14:25 -05:00
/* execute guard handlers */
2013-11-11 04:04:21 -05:00
walk_to_block(pic, pic->blk, cont->blk);
2013-11-09 00:14:25 -05:00
restore_cont(pic, cont);
}
2014-01-18 02:08:07 -05:00
pic_value
pic_callcc(pic_state *pic, struct pic_proc *proc)
2013-11-09 00:14:25 -05:00
{
struct pic_cont *cont;
save_cont(pic, &cont);
2013-11-09 00:14:25 -05:00
if (setjmp(cont->jmp)) {
return pic_values_by_list(pic, cont->results);
2013-11-09 00:14:25 -05:00
}
else {
struct pic_proc *c;
2014-01-08 08:44:53 -05:00
c = pic_proc_new(pic, cont_call);
2013-11-09 00:14:25 -05:00
/* save the continuation object in proc */
pic_proc_cv_init(pic, c, 1);
pic_proc_cv_set(pic, c, 0, pic_obj_value(cont));
2013-11-09 00:14:25 -05:00
2014-01-18 02:08:07 -05:00
return pic_apply_argv(pic, proc, 1, pic_obj_value(c));
2013-11-09 00:14:25 -05:00
}
}
2014-02-01 22:10:31 -05:00
pic_value
pic_values(pic_state *pic, size_t c, ...)
{
va_list ap;
size_t i;
va_start(ap, c);
for (i = 0; i < c; ++i) {
pic->ci->fp[i] = va_arg(ap, pic_value);
}
pic->ci->fp[i] = pic_undef_value();
va_end(ap);
2014-02-01 22:27:24 -05:00
return c == 0 ? pic_none_value() : pic->ci->fp[0];
2014-02-01 22:10:31 -05:00
}
pic_value
2014-02-01 22:25:34 -05:00
pic_values_by_array(pic_state *pic, size_t argc, pic_value *argv)
2014-02-01 22:10:31 -05:00
{
size_t i;
for (i = 0; i < argc; ++i) {
pic->ci->fp[i] = argv[i];
}
pic->ci->fp[i] = pic_undef_value();
2014-02-01 22:27:24 -05:00
return argc == 0 ? pic_none_value() : pic->ci->fp[0];
}
pic_value
pic_values_by_list(pic_state *pic, pic_value list)
{
pic_value v;
size_t i;
i = 0;
pic_for_each (v, list) {
pic->ci->fp[i++] = v;
}
pic->ci->fp[i] = pic_undef_value();
return pic_nil_p(list) ? pic_none_value() : pic->ci->fp[0];
2014-02-01 22:10:31 -05:00
}
2014-02-01 22:38:46 -05:00
size_t
pic_receive(pic_state *pic, size_t n, pic_value *argv)
{
2014-02-02 00:23:58 -05:00
pic_callinfo *ci;
2014-02-01 22:38:46 -05:00
size_t i;
2014-02-02 00:23:58 -05:00
/* take info from already discarded frame */
ci = pic->ci + 1;
2014-02-01 22:38:46 -05:00
for (i = 0; ; ++i) {
2014-02-02 00:23:58 -05:00
if (pic_undef_p(ci->fp[i]))
2014-02-01 22:38:46 -05:00
break;
if (i < n) {
2014-02-02 00:23:58 -05:00
argv[i] = ci->fp[i];
2014-02-01 22:38:46 -05:00
}
}
return i;
}
2014-01-18 02:08:07 -05:00
static pic_value
pic_cont_callcc(pic_state *pic)
{
struct pic_proc *cb;
pic_get_args(pic, "l", &cb);
return pic_callcc(pic, cb);
}
2013-11-11 04:04:21 -05:00
static pic_value
pic_cont_dynamic_wind(pic_state *pic)
{
struct pic_proc *in, *thunk, *out;
2014-01-08 06:53:28 -05:00
pic_value v;
2013-11-11 04:04:21 -05:00
2014-01-08 06:53:28 -05:00
pic_get_args(pic, "lll", &in, &thunk, &out);
2013-11-11 04:04:21 -05:00
/* enter */
pic_apply_argv(pic, in, 0);
{
struct pic_block *here;
here = pic->blk;
pic->blk = (struct pic_block *)pic_alloc(pic, sizeof(struct pic_block));
pic->blk->prev = here;
pic->blk->depth = here->depth + 1;
pic->blk->in = in;
pic->blk->out = out;
pic->blk->refcnt = 1;
PIC_BLK_INCREF(pic, here);
2013-11-11 04:04:21 -05:00
v = pic_apply_argv(pic, thunk, 0);
PIC_BLK_DECREF(pic, pic->blk);
2013-11-11 04:04:21 -05:00
pic->blk = here;
}
/* exit */
pic_apply_argv(pic, out, 0);
return v;
}
static pic_value
pic_cont_values(pic_state *pic)
{
size_t argc;
pic_value *argv;
pic_get_args(pic, "*", &argc, &argv);
return pic_values_by_array(pic, argc, argv);
}
static pic_value
pic_cont_call_with_values(pic_state *pic)
{
struct pic_proc *producer, *consumer;
size_t argc;
pic_value args[256];
pic_get_args(pic, "ll", &producer, &consumer);
pic_apply(pic, producer, pic_nil_value());
argc = pic_receive(pic, 256, args);
return pic_apply(pic, consumer, pic_list_by_array(pic, argc, args));
}
2013-11-09 00:14:25 -05:00
void
pic_init_cont(pic_state *pic)
{
2013-11-09 01:25:19 -05:00
pic_defun(pic, "call-with-current-continuation", pic_cont_callcc);
pic_defun(pic, "call/cc", pic_cont_callcc);
2013-11-11 04:04:21 -05:00
pic_defun(pic, "dynamic-wind", pic_cont_dynamic_wind);
pic_defun(pic, "values", pic_cont_values);
pic_defun(pic, "call-with-values", pic_cont_call_with_values);
2013-11-09 00:14:25 -05:00
}