#include "libguile/_scm.h"
#include "libguile/control.h"
+#include "libguile/objcodes.h"
+#include "libguile/instructions.h"
#include "libguile/vm.h"
\f
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;
abort ();
}
+ cont = reify_partial_continuation (vm, prompt, winds, cookie);
+
/* Unwind once more, beyond the prompt. */
winds = SCM_CDR (winds), delta++;
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 */
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...
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;
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)
{
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)
{
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 ();
}
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;
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);
}
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
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;
/* 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 ();
}
\f