picrin/contrib/10.callcc/callcc.c

298 lines
7.2 KiB
C
Raw Normal View History

2014-09-20 07:30:36 -04:00
#include "picrin.h"
struct pic_fullcont {
2014-09-20 07:30:36 -04:00
jmp_buf jmp;
2015-06-22 14:02:12 -04:00
struct pic_cont *prev_jmp;
2015-06-08 08:04:04 -04:00
pic_checkpoint *cp;
2014-09-20 07:30:36 -04:00
char *stk_pos, *stk_ptr;
ptrdiff_t stk_len;
pic_value *st_ptr;
size_t sp_offset, st_len;
pic_callinfo *ci_ptr;
size_t ci_offset, ci_len;
struct pic_proc **xp_ptr;
size_t xp_offset, xp_len;
pic_code *ip;
2015-06-08 09:28:17 -04:00
pic_value ptable;
2014-09-20 07:30:36 -04:00
struct pic_object **arena;
size_t arena_size;
int arena_idx;
pic_value results;
};
static void
cont_dtor(pic_state *pic, void *data)
{
struct pic_fullcont *cont = data;
2014-09-20 07:30:36 -04:00
pic_free(pic, cont->stk_ptr);
pic_free(pic, cont->st_ptr);
pic_free(pic, cont->ci_ptr);
pic_free(pic, cont->xp_ptr);
pic_free(pic, cont->arena);
pic_free(pic, cont);
}
static void
cont_mark(pic_state *pic, void *data, void (*mark)(pic_state *, pic_value))
{
struct pic_fullcont *cont = data;
2015-06-08 08:04:04 -04:00
pic_checkpoint *cp;
2014-09-20 07:30:36 -04:00
pic_value *stack;
pic_callinfo *ci;
struct pic_proc **xp;
size_t i;
2015-06-08 08:04:04 -04:00
/* checkpoint */
for (cp = cont->cp; cp != NULL; cp = cp->prev) {
if (cp->in) {
mark(pic, pic_obj_value(cp->in));
2014-09-20 07:30:36 -04:00
}
2015-06-08 08:04:04 -04:00
if (cp->out) {
mark(pic, pic_obj_value(cp->out));
2014-09-20 07:30:36 -04:00
}
}
/* stack */
for (stack = cont->st_ptr; stack != cont->st_ptr + cont->sp_offset; ++stack) {
mark(pic, *stack);
}
/* callinfo */
for (ci = cont->ci_ptr + cont->ci_offset; ci != cont->ci_ptr; --ci) {
2015-05-30 09:34:51 -04:00
if (ci->cxt) {
mark(pic, pic_obj_value(ci->cxt));
2014-09-20 07:30:36 -04:00
}
}
/* exception handlers */
for (xp = cont->xp_ptr; xp != cont->xp_ptr + cont->xp_offset; ++xp) {
mark(pic, pic_obj_value(*xp));
}
/* arena */
for (i = 0; i < (size_t)cont->arena_idx; ++i) {
mark(pic, pic_obj_value(cont->arena[i]));
}
2015-06-08 09:28:17 -04:00
/* parameter table */
mark(pic, cont->ptable);
2014-09-20 07:30:36 -04:00
/* result values */
mark(pic, cont->results);
}
static const pic_data_type cont_type = { "continuation", cont_dtor, cont_mark };
static void save_cont(pic_state *, struct pic_fullcont **);
static void restore_cont(pic_state *, struct pic_fullcont *);
2014-09-20 07:30:36 -04:00
static ptrdiff_t
native_stack_length(pic_state *pic, char **pos)
{
char t;
*pos = (pic->native_stack_start > &t)
? &t
: pic->native_stack_start;
return (pic->native_stack_start > &t)
? pic->native_stack_start - &t
: &t - pic->native_stack_start;
}
static void
save_cont(pic_state *pic, struct pic_fullcont **c)
2014-09-20 07:30:36 -04:00
{
void pic_vm_tear_off(pic_state *);
struct pic_fullcont *cont;
2014-09-20 07:30:36 -04:00
char *pos;
pic_vm_tear_off(pic); /* tear off */
cont = *c = pic_malloc(pic, sizeof(struct pic_fullcont));
2014-09-20 07:30:36 -04:00
2015-06-22 14:02:12 -04:00
cont->prev_jmp = pic->cc;
2015-06-08 08:04:04 -04:00
cont->cp = pic->cp;
2014-09-20 07:30:36 -04:00
cont->stk_len = native_stack_length(pic, &pos);
cont->stk_pos = pos;
assert(cont->stk_len > 0);
2015-05-28 03:42:16 -04:00
cont->stk_ptr = pic_malloc(pic, cont->stk_len);
2014-09-20 07:30:36 -04:00
memcpy(cont->stk_ptr, cont->stk_pos, cont->stk_len);
cont->sp_offset = pic->sp - pic->stbase;
cont->st_len = pic->stend - pic->stbase;
2015-05-28 03:42:16 -04:00
cont->st_ptr = pic_malloc(pic, sizeof(pic_value) * cont->st_len);
2014-09-20 07:30:36 -04:00
memcpy(cont->st_ptr, pic->stbase, sizeof(pic_value) * cont->st_len);
cont->ci_offset = pic->ci - pic->cibase;
cont->ci_len = pic->ciend - pic->cibase;
2015-05-28 03:42:16 -04:00
cont->ci_ptr = pic_malloc(pic, sizeof(pic_callinfo) * cont->ci_len);
2014-09-20 07:30:36 -04:00
memcpy(cont->ci_ptr, pic->cibase, sizeof(pic_callinfo) * cont->ci_len);
cont->xp_offset = pic->xp - pic->xpbase;
cont->xp_len = pic->xpend - pic->xpbase;
2015-05-28 03:42:16 -04:00
cont->xp_ptr = pic_malloc(pic, sizeof(struct pic_proc *) * cont->xp_len);
2014-09-20 07:30:36 -04:00
memcpy(cont->xp_ptr, pic->xpbase, sizeof(struct pic_proc *) * cont->xp_len);
cont->ip = pic->ip;
2015-06-08 09:28:17 -04:00
cont->ptable = pic->ptable;
2014-09-20 07:30:36 -04:00
cont->arena_idx = pic->arena_idx;
cont->arena_size = pic->arena_size;
2015-05-28 03:42:16 -04:00
cont->arena = pic_malloc(pic, sizeof(struct pic_object *) * pic->arena_size);
2014-09-20 07:30:36 -04:00
memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * pic->arena_size);
cont->results = pic_undef_value();
}
static void
native_stack_extend(pic_state *pic, struct pic_fullcont *cont)
2014-09-20 07:30:36 -04:00
{
volatile pic_value v[1024];
((void)v);
restore_cont(pic, cont);
}
2015-01-25 22:22:38 -05:00
PIC_NORETURN static void
restore_cont(pic_state *pic, struct pic_fullcont *cont)
2014-09-20 07:30:36 -04:00
{
char v;
struct pic_fullcont *tmp = cont;
2014-09-20 07:30:36 -04:00
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);
2014-09-20 07:30:36 -04:00
}
2015-06-22 14:02:12 -04:00
pic->cc = cont->prev_jmp;
2015-06-08 08:04:04 -04:00
pic->cp = cont->cp;
2014-09-20 07:30:36 -04:00
pic->stbase = pic_realloc(pic, pic->stbase, sizeof(pic_value) * cont->st_len);
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;
pic->cibase = pic_realloc(pic, pic->cibase, sizeof(pic_callinfo) * cont->ci_len);
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;
pic->xpbase = pic_realloc(pic, pic->xpbase, sizeof(struct pic_proc *) * cont->xp_len);
memcpy(pic->xpbase, cont->xp_ptr, sizeof(struct pic_proc *) * cont->xp_len);
pic->xp = pic->xpbase + cont->xp_offset;
pic->xpend = pic->xpbase + cont->xp_len;
pic->ip = cont->ip;
2015-06-08 09:28:17 -04:00
pic->ptable = cont->ptable;
2014-09-20 07:30:36 -04:00
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct pic_object *) * cont->arena_size);
memcpy(pic->arena, cont->arena, sizeof(struct pic_object *) * cont->arena_size);
pic->arena_size = cont->arena_size;
pic->arena_idx = cont->arena_idx;
memcpy(cont->stk_pos, cont->stk_ptr, cont->stk_len);
longjmp(tmp->jmp, 1);
}
2015-01-25 22:22:38 -05:00
PIC_NORETURN static pic_value
2014-09-20 07:30:36 -04:00
cont_call(pic_state *pic)
{
struct pic_proc *proc;
size_t argc;
pic_value *argv;
struct pic_fullcont *cont;
2014-09-20 07:30:36 -04:00
proc = pic_get_proc(pic);
pic_get_args(pic, "*", &argc, &argv);
2015-06-01 17:57:55 -04:00
cont = pic_data_ptr(pic_proc_env_ref(pic, proc, "cont"))->data;
2014-09-20 07:30:36 -04:00
cont->results = pic_list_by_array(pic, argc, argv);
/* execute guard handlers */
2015-06-08 08:04:04 -04:00
pic_wind(pic, pic->cp, cont->cp);
2014-09-20 07:30:36 -04:00
restore_cont(pic, cont);
}
pic_value
pic_callcc_full(pic_state *pic, struct pic_proc *proc)
2014-09-20 07:30:36 -04:00
{
struct pic_fullcont *cont;
2014-09-20 07:30:36 -04:00
save_cont(pic, &cont);
if (setjmp(cont->jmp)) {
return pic_values_by_list(pic, cont->results);
}
else {
struct pic_proc *c;
struct pic_data *dat;
2015-06-27 06:02:18 -04:00
c = pic_make_proc(pic, cont_call);
2014-09-20 07:30:36 -04:00
dat = pic_data_alloc(pic, &cont_type, cont);
/* save the continuation object in proc */
2015-06-01 17:57:55 -04:00
pic_proc_env_set(pic, c, "cont", pic_obj_value(dat));
2014-09-20 07:30:36 -04:00
return pic_apply1(pic, proc, pic_obj_value(c));
}
}
static pic_value
2015-07-04 05:01:30 -04:00
pic_callcc_callcc(pic_state *pic)
2014-09-20 07:30:36 -04:00
{
2015-07-04 05:01:30 -04:00
struct pic_proc *proc;
struct pic_fullcont *cont;
2014-09-20 07:30:36 -04:00
2015-07-04 05:01:30 -04:00
pic_get_args(pic, "l", &proc);
2014-09-20 07:30:36 -04:00
save_cont(pic, &cont);
if (setjmp(cont->jmp)) {
return pic_values_by_list(pic, cont->results);
}
else {
struct pic_proc *c;
struct pic_data *dat;
2015-06-27 06:02:18 -04:00
c = pic_make_proc(pic, cont_call);
2014-09-20 07:30:36 -04:00
dat = pic_data_alloc(pic, &cont_type, cont);
/* save the continuation object in proc */
2015-06-01 17:57:55 -04:00
pic_proc_env_set(pic, c, "cont", pic_obj_value(dat));
2014-09-20 07:30:36 -04:00
2015-07-04 05:01:30 -04:00
return pic_apply_trampoline_list(pic, proc, pic_list1(pic, pic_obj_value(c)));
2014-09-20 07:30:36 -04:00
}
}
#define pic_redefun(pic, lib, name, func) \
2015-06-27 06:02:18 -04:00
pic_set(pic, lib, name, pic_obj_value(pic_make_proc(pic, func)))
2014-09-20 07:30:36 -04:00
void
pic_init_callcc(pic_state *pic)
{
pic_deflibrary (pic, "(scheme base)") {
pic_redefun(pic, pic->PICRIN_BASE, "call-with-current-continuation", pic_callcc_callcc);
pic_redefun(pic, pic->PICRIN_BASE, "call/cc", pic_callcc_callcc);
}
2014-09-20 07:30:36 -04:00
}