summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2010-02-22 23:00:19 +0100
committerAndy Wingo <wingo@pobox.com>2010-02-22 23:00:19 +0100
commitcee1d22c3c10b1892c82a5758ef69cd6fc9aba31 (patch)
tree03628430d2426ab2148587d19ee386f82a9f09aa
parent76e3816281cf6c406ef6f01907ce29401c8ff455 (diff)
downloadguile-cee1d22c3c10b1892c82a5758ef69cd6fc9aba31.tar.gz
actually capture partial continuations
* libguile/control.c (cont_objcode): Along with a bunch of boilerplate that certainly needs to go in some central place, define this continuation-calling trampoline. (reify_partial_continuation): New function, returns a procedure that when called will reinstate a partial continuation. (scm_c_abort): Take an extra arg, the cookie. Actually reify a continuation. (scm_at_abort): Adapt to scm_c_abort change. * libguile/control.h: Declare scm_c_abort change. * libguile/vm-i-system.c (partial_cont_call): New instruction. (call/cc, tail-call/cc): Adapt to scm_i_vm_capture_stack change. (abort): Pass vm_cookie to abort. * libguile/vm.h (SCM_F_VM_CONT_PARTIAL, SCM_F_VM_CONT_REWINDABLE): New flags. (struct scm_vm_cont): Add flags field. (SCM_VM_CONT_PARTIAL_P, SCM_VM_CONT_REWINDABLE_P): New predicates. * libguile/vm.c (scm_i_vm_capture_stack): Rename from vm_capture_continuation, and make internal instead of static. Take a flags argument. (scm_i_vm_capture_continuation): Adapt to scm_i_vm_capture_stack change. (vm_abort): Plumb cookie to scm_c_abort. (vm_reinstate_partial_continuation): New stub.
-rw-r--r--libguile/control.c117
-rw-r--r--libguile/control.h3
-rw-r--r--libguile/vm-i-system.c26
-rw-r--r--libguile/vm.c22
-rw-r--r--libguile/vm.h9
5 files changed, 158 insertions, 19 deletions
diff --git a/libguile/control.c b/libguile/control.c
index 371ec2d20..25c95047b 100644
--- a/libguile/control.c
+++ b/libguile/control.c
@@ -22,6 +22,8 @@
#include "libguile/_scm.h"
#include "libguile/control.h"
+#include "libguile/objcodes.h"
+#include "libguile/instructions.h"
#include "libguile/vm.h"
@@ -52,10 +54,115 @@ scm_c_make_prompt (SCM vm, SCM k, scm_t_uint8 escape_only_p,
return ret;
}
+#ifdef WORDS_BIGENDIAN
+#define OBJCODE_HEADER(main,meta) 0, 0, 0, main, 0, 0, 0, meta+8
+#define META_HEADER(meta) 0, 0, 0, meta, 0, 0, 0, 0
+#else
+#define OBJCODE_HEADER(main,meta) main, 0, 0, 0, meta+8, 0, 0, 0
+#define META_HEADER(meta) meta, 0, 0, 0, 0, 0, 0, 0
+#endif
+
+#define ROUND_UP(len,align) (((len-1)|(align-1))+1)
+#define ALIGN_PTR(type,p,align) (type*)(ROUND_UP (((scm_t_bits)p), align))
+
+#ifdef SCM_ALIGNED
+#define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym)\
+static const type sym[]
+#define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym)\
+static SCM_ALIGNED (alignment) const type sym[]
+#else
+#define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym)\
+static type *sym
+#define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym) \
+SCM_SNARF_INIT(sym = scm_malloc (sizeof(sym##__unaligned) + alignment - 1); \
+ sym = ALIGN_PTR (type, sym, alignment); \
+ memcpy (sym, sym##__unaligned, sizeof(sym##__unaligned));) \
+static type *sym = NULL; \
+static const type sym##__unaligned[]
+#endif
+
+#define STATIC_OBJCODE_TAG \
+ SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
+
+#define SCM_STATIC_OBJCODE(sym) \
+ SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
+ SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
+ { STATIC_OBJCODE_TAG, SCM_PACK (sym##__bytecode) }, \
+ { SCM_BOOL_F, SCM_PACK (0) } \
+ }; \
+ static const SCM sym = SCM_PACK (sym##__cells); \
+ SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
+
+
+SCM_STATIC_OBJCODE (cont_objcode) = {
+ /* Like in continuations.c, but with partial-cont-call. */
+ OBJCODE_HEADER (8, 19),
+ /* leave args on the stack */
+ /* 0 */ scm_op_object_ref, 0, /* push scm_vm_cont object */
+ /* 2 */ scm_op_object_ref, 1, /* push internal winds */
+ /* 4 */ scm_op_object_ref, 2, /* push external winds */
+ /* 6 */ scm_op_partial_cont_call, /* and go! */
+ /* 7 */ scm_op_nop, /* pad to 8 bytes */
+ /* 8 */
+
+ /* We could put some meta-info to say that this proc is a continuation. Not sure
+ how to do that, though. */
+ META_HEADER (19),
+ /* 0 */ scm_op_make_eol, /* bindings */
+ /* 1 */ scm_op_make_eol, /* sources */
+ /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 7, /* arity: from ip 0 to ip 7 */
+ /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
+ /* 7 */ scm_op_make_int8_0, /* 0 optionals */
+ /* 8 */ scm_op_make_true, /* and a rest arg */
+ /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
+ /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
+ /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
+ /* 18 */ scm_op_return /* and return */
+ /* 19 */
+};
+
+
+static SCM
+reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
+ scm_t_int64 cookie)
+{
+ SCM vm_cont, dynwinds, intwinds = SCM_EOL, ret;
+ scm_t_uint32 flags;
+
+ /* No need to reify if the continuation is never referenced in the handler. */
+ if (SCM_PROMPT_ESCAPE_P (prompt))
+ return SCM_BOOL_F;
+
+ dynwinds = scm_i_dynwinds ();
+ while (!scm_is_eq (dynwinds, extwinds))
+ {
+ intwinds = scm_cons (scm_car (dynwinds), intwinds);
+ dynwinds = scm_cdr (dynwinds);
+ }
+
+ flags = SCM_F_VM_CONT_PARTIAL;
+ if (cookie >= 0 && SCM_PROMPT_REGISTERS (prompt)->cookie == cookie)
+ flags |= SCM_F_VM_CONT_REWINDABLE;
+
+ /* NULL RA and MVRA, as those get set when the cont is reinstated */
+ vm_cont = scm_i_vm_capture_stack (SCM_PROMPT_REGISTERS (prompt)->sp,
+ SCM_VM_DATA (vm)->fp,
+ SCM_VM_DATA (vm)->sp,
+ NULL, NULL,
+ flags);
+
+ ret = scm_make_program (cont_objcode,
+ scm_vector (scm_list_3 (vm_cont, intwinds, extwinds)),
+ SCM_BOOL_F);
+ SCM_SET_CELL_WORD_0 (ret,
+ SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_CONTINUATION);
+ return ret;
+}
+
SCM
-scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv)
+scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv, scm_t_int64 cookie)
{
- SCM winds, prompt = SCM_BOOL_F;
+ SCM cont, winds, prompt = SCM_BOOL_F;
long delta;
size_t i;
@@ -83,6 +190,8 @@ scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv)
abort ();
}
+ cont = reify_partial_continuation (vm, prompt, winds, cookie);
+
/* Unwind once more, beyond the prompt. */
winds = SCM_CDR (winds), delta++;
@@ -99,7 +208,7 @@ scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv)
abort ();
/* Push vals */
- *(++(SCM_VM_DATA (vm)->sp)) = SCM_BOOL_F; /* the continuation */
+ *(++(SCM_VM_DATA (vm)->sp)) = cont;
for (i = 0; i < n; i++)
*(++(SCM_VM_DATA (vm)->sp)) = argv[i];
*(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
@@ -123,7 +232,7 @@ SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
for (i = 0; i < n; i++, args = scm_cdr (args))
argv[i] = scm_car (args);
- scm_c_abort (scm_the_vm (), tag, n, argv);
+ scm_c_abort (scm_the_vm (), tag, n, argv, -1);
/* Oh, what, you're still here? The abort must have been reinstated. Actually,
that's quite impossible, given that we're already in C-land here, so...
diff --git a/libguile/control.h b/libguile/control.h
index 6144a38c9..160728d8f 100644
--- a/libguile/control.h
+++ b/libguile/control.h
@@ -44,7 +44,8 @@ struct scm_prompt_registers
SCM_INTERNAL SCM scm_c_make_prompt (SCM vm, SCM k, scm_t_uint8 escape_only_p,
scm_t_int64 cookie);
-SCM_INTERNAL SCM scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv) SCM_NORETURN;
+SCM_INTERNAL SCM scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv,
+ scm_t_int64 cookie) SCM_NORETURN;
SCM_INTERNAL SCM scm_at_abort (SCM tag, SCM args) SCM_NORETURN;
diff --git a/libguile/vm-i-system.c b/libguile/vm-i-system.c
index faae6ab3e..09293bef8 100644
--- a/libguile/vm-i-system.c
+++ b/libguile/vm-i-system.c
@@ -993,6 +993,17 @@ VM_DEFINE_INSTRUCTION (89, continuation_call, "continuation-call", 0, -1, 0)
abort ();
}
+VM_DEFINE_INSTRUCTION (94, partial_cont_call, "partial-cont-call", 0, -1, 0)
+{
+ SCM vmcont, intwinds, extwinds;
+ POP (extwinds);
+ POP (intwinds);
+ POP (vmcont);
+
+ vm_reinstate_partial_continuation (vm, vmcont, intwinds, extwinds);
+ NEXT;
+}
+
VM_DEFINE_INSTRUCTION (59, tail_call_nargs, "tail-call/nargs", 0, 0, 1)
{
SCM x;
@@ -1099,7 +1110,7 @@ VM_DEFINE_INSTRUCTION (64, call_cc, "call/cc", 0, 1, 1)
SCM proc, vm_cont, cont;
POP (proc);
SYNC_ALL ();
- vm_cont = vm_capture_continuation (vp->stack_base, fp, sp, ip, NULL);
+ vm_cont = scm_i_vm_capture_stack (vp->stack_base, fp, sp, ip, NULL, 0);
cont = scm_i_make_continuation (&first, vm, vm_cont);
if (first)
{
@@ -1131,11 +1142,12 @@ VM_DEFINE_INSTRUCTION (65, tail_call_cc, "tail-call/cc", 0, 1, 1)
SYNC_ALL ();
/* In contrast to call/cc, tail-call/cc captures the continuation without the
stack frame. */
- vm_cont = vm_capture_continuation (vp->stack_base,
- SCM_FRAME_DYNAMIC_LINK (fp),
- SCM_FRAME_LOWER_ADDRESS (fp) - 1,
- SCM_FRAME_RETURN_ADDRESS (fp),
- SCM_FRAME_MV_RETURN_ADDRESS (fp));
+ vm_cont = scm_i_vm_capture_stack (vp->stack_base,
+ SCM_FRAME_DYNAMIC_LINK (fp),
+ SCM_FRAME_LOWER_ADDRESS (fp) - 1,
+ SCM_FRAME_RETURN_ADDRESS (fp),
+ SCM_FRAME_MV_RETURN_ADDRESS (fp),
+ 0);
cont = scm_i_make_continuation (&first, vm, vm_cont);
if (first)
{
@@ -1511,7 +1523,7 @@ VM_DEFINE_INSTRUCTION (86, abort, "abort", 1, -1, -1)
SYNC_REGISTER ();
if (sp - n - 2 <= SCM_FRAME_UPPER_ADDRESS (fp))
goto vm_error_stack_underflow;
- vm_abort (vm, n);
+ vm_abort (vm, n, vm_cookie);
/* vm_abort should not return */
abort ();
}
diff --git a/libguile/vm.c b/libguile/vm.c
index 831200fdf..572a7100a 100644
--- a/libguile/vm.c
+++ b/libguile/vm.c
@@ -91,9 +91,9 @@ scm_i_vm_cont_print (SCM x, SCM port, scm_print_state *pstate)
continuation root is inside VM code, and call/cc was invoked within that same
call to vm_run; but that's currently not implemented.
*/
-static SCM
-vm_capture_continuation (SCM *stack_base,
- SCM *fp, SCM *sp, scm_t_uint8 *ra, scm_t_uint8 *mvra)
+SCM
+scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp, scm_t_uint8 *ra,
+ scm_t_uint8 *mvra, scm_t_uint32 flags)
{
struct scm_vm_cont *p;
@@ -116,6 +116,7 @@ vm_capture_continuation (SCM *stack_base,
p->fp = fp;
memcpy (p->stack_base, stack_base, (sp + 1 - stack_base) * sizeof (SCM));
p->reloc = p->stack_base - stack_base;
+ p->flags = flags;
return scm_cell (scm_tc7_vm_cont, (scm_t_bits)p);
}
@@ -178,7 +179,7 @@ SCM
scm_i_vm_capture_continuation (SCM vm)
{
struct scm_vm *vp = SCM_VM_DATA (vm);
- return vm_capture_continuation (vp->stack_base, vp->fp, vp->sp, vp->ip, NULL);
+ return scm_i_vm_capture_stack (vp->stack_base, vp->fp, vp->sp, vp->ip, NULL, 0);
}
static void
@@ -201,9 +202,9 @@ vm_dispatch_hook (SCM vm, int hook_num)
vp->trace_level++;
}
-static void vm_abort (SCM vm, size_t n) SCM_NORETURN;
+static void vm_abort (SCM vm, size_t n, scm_t_int64 cookie) SCM_NORETURN;
static void
-vm_abort (SCM vm, size_t n)
+vm_abort (SCM vm, size_t n, scm_t_int64 vm_cookie)
{
size_t i;
ssize_t tail_len;
@@ -224,7 +225,14 @@ vm_abort (SCM vm, size_t n)
/* NULLSTACK (n + 1) */
SCM_VM_DATA (vm)->sp -= n + 1;
- scm_c_abort (vm, tag, n + tail_len, argv);
+ scm_c_abort (vm, tag, n + tail_len, argv, vm_cookie);
+}
+
+static void
+vm_reinstate_partial_continuation (SCM vm, SCM vm_cont, SCM intwinds,
+ SCM extwinds)
+{
+ abort ();
}
diff --git a/libguile/vm.h b/libguile/vm.h
index 6d76f12e8..48e0bb627 100644
--- a/libguile/vm.h
+++ b/libguile/vm.h
@@ -87,6 +87,9 @@ SCM_API SCM scm_set_vm_option_x (SCM vm, SCM key, SCM val);
SCM_API SCM scm_vm_trace_level (SCM vm);
SCM_API SCM scm_set_vm_trace_level_x (SCM vm, SCM level);
+#define SCM_F_VM_CONT_PARTIAL 0x1
+#define SCM_F_VM_CONT_REWINDABLE 0x2
+
struct scm_vm_cont {
SCM *sp;
SCM *fp;
@@ -94,16 +97,22 @@ struct scm_vm_cont {
scm_t_ptrdiff stack_size;
SCM *stack_base;
scm_t_ptrdiff reloc;
+ scm_t_uint32 flags;
};
#define SCM_VM_CONT_P(OBJ) (SCM_NIMP (OBJ) && SCM_TYP7 (OBJ) == scm_tc7_vm_cont)
#define SCM_VM_CONT_DATA(CONT) ((struct scm_vm_cont *) SCM_CELL_WORD_1 (CONT))
+#define SCM_VM_CONT_PARTIAL_P(CONT) (SCM_VM_CONT_DATA (CONT) & SCM_F_VM_CONT_PARTIAL)
+#define SCM_VM_CONT_REWINDABLE_P(CONT) (SCM_VM_CONT_DATA (CONT) & SCM_F_VM_CONT_REWINDABLE)
SCM_API SCM scm_load_compiled_with_vm (SCM file);
SCM_INTERNAL void scm_i_vm_print (SCM x, SCM port,
scm_print_state *pstate);
SCM_INTERNAL SCM scm_i_vm_capture_continuation (SCM vm);
+SCM_INTERNAL SCM scm_i_vm_capture_stack (SCM *stack_base, SCM *fp, SCM *sp,
+ scm_t_uint8 *ra, scm_t_uint8 *mvra,
+ scm_t_uint32 flags);
SCM_INTERNAL void scm_i_vm_cont_print (SCM x, SCM port,
scm_print_state *pstate);
SCM_INTERNAL void scm_bootstrap_vm (void);