diff options
author | Andy Wingo <wingo@pobox.com> | 2010-02-22 23:00:19 +0100 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2010-02-22 23:00:19 +0100 |
commit | cee1d22c3c10b1892c82a5758ef69cd6fc9aba31 (patch) | |
tree | 03628430d2426ab2148587d19ee386f82a9f09aa /libguile/control.c | |
parent | 76e3816281cf6c406ef6f01907ce29401c8ff455 (diff) | |
download | guile-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.
Diffstat (limited to 'libguile/control.c')
-rw-r--r-- | libguile/control.c | 117 |
1 files changed, 113 insertions, 4 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... |