picrin/lib/cont.c

228 lines
4.1 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2017-03-28 10:09:40 -04:00
#include "object.h"
#include "state.h"
2014-08-25 00:38:09 -04:00
2016-02-23 08:48:06 -05:00
struct cont {
2016-02-20 09:55:40 -05:00
PIC_JMPBUF *jmp;
ptrdiff_t sp_offset;
ptrdiff_t ci_offset;
size_t arena_idx;
2016-02-29 13:14:18 -05:00
const struct code *ip;
2017-03-31 01:39:01 -04:00
pic_value dyn_env;
2016-02-20 09:55:40 -05:00
int retc;
pic_value *retv;
2016-02-23 08:48:06 -05:00
struct cont *prev;
2016-02-20 09:55:40 -05:00
};
2017-03-30 04:42:02 -04:00
static const pic_data_type cont_type = { "cont", NULL };
2016-02-20 09:55:40 -05:00
2016-02-19 03:38:49 -05:00
void
2016-02-23 08:48:06 -05:00
pic_save_point(pic_state *pic, struct cont *cont, PIC_JMPBUF *jmp)
2016-02-19 03:38:49 -05:00
{
2016-02-20 09:55:40 -05:00
cont->jmp = jmp;
2016-02-19 03:38:49 -05:00
/* save runtime context */
cont->sp_offset = pic->sp - pic->stbase;
cont->ci_offset = pic->ci - pic->cibase;
cont->arena_idx = pic->arena_idx;
2017-03-31 01:39:01 -04:00
cont->dyn_env = pic->dyn_env;
2016-02-19 03:38:49 -05:00
cont->ip = pic->ip;
cont->prev = pic->cc;
2016-02-19 03:58:42 -05:00
cont->retc = 0;
cont->retv = NULL;
2016-02-19 03:38:49 -05:00
pic->cc = cont;
}
void
2016-02-23 08:48:06 -05:00
pic_load_point(pic_state *pic, struct cont *cont)
2016-02-19 03:38:49 -05:00
{
pic_vm_tear_off(pic);
2016-02-19 03:38:49 -05:00
/* load runtime context */
pic->sp = pic->stbase + cont->sp_offset;
pic->ci = pic->cibase + cont->ci_offset;
pic->arena_idx = cont->arena_idx;
2017-03-31 01:39:01 -04:00
pic->dyn_env = cont->dyn_env;
2016-02-19 03:38:49 -05:00
pic->ip = cont->ip;
pic->cc = cont->prev;
}
2016-02-20 05:08:07 -05:00
void
pic_exit_point(pic_state *pic)
{
pic->cc = pic->cc->prev;
}
2015-01-07 16:11:48 -05:00
static pic_value
2015-06-01 17:32:43 -04:00
cont_call(pic_state *pic)
2014-09-19 03:41:49 -04:00
{
2015-08-26 06:04:27 -04:00
int argc;
2014-09-19 03:41:49 -04:00
pic_value *argv;
2016-02-23 08:48:06 -05:00
struct cont *cc, *cont;
2014-09-19 03:41:49 -04:00
pic_get_args(pic, "*", &argc, &argv);
2014-09-19 03:41:49 -04:00
2016-02-20 09:55:40 -05:00
cont = pic_data(pic, pic_closure_ref(pic, 0));
/* check if continuation is alive */
for (cc = pic->cc; cc != NULL; cc = cc->prev) {
2016-02-23 09:28:28 -05:00
if (cc == cont) {
break;
}
}
if (cc == NULL) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "calling dead escape continuation", 0);
}
2016-02-19 03:58:42 -05:00
cont->retc = argc;
cont->retv = argv;
2014-09-19 03:41:49 -04:00
pic_load_point(pic, cont);
2016-02-20 09:55:40 -05:00
PIC_LONGJMP(pic, *cont->jmp, 1);
2015-05-27 11:41:55 -04:00
PIC_UNREACHABLE();
2014-09-19 03:41:49 -04:00
}
2016-02-19 10:03:16 -05:00
pic_value
2016-02-23 08:48:06 -05:00
pic_make_cont(pic_state *pic, struct cont *cont)
2014-09-24 01:57:49 -04:00
{
2016-02-20 09:55:40 -05:00
return pic_lambda(pic, cont_call, 1, pic_data_value(pic, cont, &cont_type));
}
2014-09-24 01:57:49 -04:00
2016-02-23 08:48:06 -05:00
struct cont *
2016-02-20 09:55:40 -05:00
pic_alloca_cont(pic_state *pic)
{
2016-02-23 08:48:06 -05:00
return pic_alloca(pic, sizeof(struct cont));
2014-09-24 01:57:49 -04:00
}
2017-03-30 08:53:49 -04:00
static pic_value
values(pic_state *pic, int argc, pic_value *argv)
{
int i;
for (i = 0; i < argc; ++i) {
pic->sp[i] = argv[i];
}
pic->ci->retc = argc;
return argc == 0 ? pic_undef_value(pic) : pic->sp[0];
}
2016-02-19 03:38:49 -05:00
static pic_value
2016-02-19 10:03:16 -05:00
pic_callcc(pic_state *pic, pic_value proc)
2014-09-19 03:41:49 -04:00
{
2016-02-20 09:55:40 -05:00
PIC_JMPBUF jmp;
volatile struct cont *cont = pic_alloca_cont(pic);
2014-09-19 03:41:49 -04:00
2016-02-20 09:55:40 -05:00
if (PIC_SETJMP(pic, jmp)) {
2017-03-30 08:53:49 -04:00
return values(pic, cont->retc, cont->retv);
2014-09-19 03:41:49 -04:00
}
else {
pic_value val;
pic_save_point(pic, (struct cont *)cont, &jmp);
2016-02-20 09:55:40 -05:00
val = pic_call(pic, proc, 1, pic_make_cont(pic, (struct cont *)cont));
2014-09-19 03:41:49 -04:00
2016-02-20 05:08:07 -05:00
pic_exit_point(pic);
2014-09-19 03:41:49 -04:00
return val;
}
}
2016-02-19 03:38:49 -05:00
pic_value
2017-03-30 08:53:49 -04:00
pic_values(pic_state *pic, int n, ...)
2015-06-28 15:25:47 -04:00
{
va_list ap;
2016-02-19 03:38:49 -05:00
pic_value ret;
2015-06-28 15:25:47 -04:00
va_start(ap, n);
2017-03-30 08:53:49 -04:00
ret = pic_vvalues(pic, n, ap);
2015-06-28 15:25:47 -04:00
va_end(ap);
2016-02-19 03:38:49 -05:00
return ret;
2014-08-25 00:38:09 -04:00
}
pic_value
2017-03-30 08:53:49 -04:00
pic_vvalues(pic_state *pic, int n, va_list ap)
2014-08-25 00:38:09 -04:00
{
2016-02-19 03:38:49 -05:00
pic_value *retv = pic_alloca(pic, sizeof(pic_value) * n);
int i;
2014-08-25 00:38:09 -04:00
2016-02-19 03:38:49 -05:00
for (i = 0; i < n; ++i) {
retv[i] = va_arg(ap, pic_value);
}
2017-03-30 08:53:49 -04:00
return values(pic, n, retv);
2014-08-25 00:38:09 -04:00
}
2015-08-26 06:04:27 -04:00
int
pic_receive(pic_state *pic, int n, pic_value *argv)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct callinfo *ci;
2015-08-26 06:04:27 -04:00
int i, retc;
2014-08-25 00:38:09 -04:00
/* take info from discarded frame */
ci = pic->ci + 1;
2015-08-26 06:04:27 -04:00
retc = ci->retc;
2014-08-25 00:38:09 -04:00
for (i = 0; i < retc && i < n; ++i) {
argv[i] = ci->fp[i];
}
return retc;
}
static pic_value
pic_cont_callcc(pic_state *pic)
{
2016-02-19 10:03:16 -05:00
pic_value f;
2014-08-25 00:38:09 -04:00
2016-02-19 10:03:16 -05:00
pic_get_args(pic, "l", &f);
2014-08-25 00:38:09 -04:00
2016-02-19 10:03:16 -05:00
return pic_callcc(pic, f);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_cont_values(pic_state *pic)
{
2015-08-26 06:04:27 -04:00
int argc;
2014-08-25 00:38:09 -04:00
pic_value *argv;
pic_get_args(pic, "*", &argc, &argv);
2017-03-30 08:53:49 -04:00
return values(pic, argc, argv);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_cont_call_with_values(pic_state *pic)
{
pic_value producer, consumer, retv[256];
2016-02-19 03:58:42 -05:00
int retc;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "ll", &producer, &consumer);
2016-02-13 23:46:55 -05:00
pic_call(pic, producer, 0);
2014-08-25 00:38:09 -04:00
retc = pic_receive(pic, 256, retv);
if (retc > 256) {
pic_error(pic, "call-with-values: too many arguments", 1, pic_int_value(pic, retc));
}
2014-08-25 00:38:09 -04:00
2016-02-19 03:58:42 -05:00
return pic_applyk(pic, consumer, retc, retv);
2014-08-25 00:38:09 -04:00
}
void
pic_init_cont(pic_state *pic)
{
pic_defun(pic, "call-with-current-continuation", pic_cont_callcc);
2014-09-08 06:38:33 -04:00
pic_defun(pic, "call/cc", pic_cont_callcc);
2015-07-01 17:17:27 -04:00
pic_defun(pic, "values", pic_cont_values);
pic_defun(pic, "call-with-values", pic_cont_call_with_values);
2014-08-25 00:38:09 -04:00
}