1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-22 20:40:29 +02:00
guile/libguile/control.c
Andy Wingo cee1d22c3c 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.
2010-02-22 23:00:19 +01:00

254 lines
8.2 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Copyright (C) 2010 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "libguile/_scm.h"
#include "libguile/control.h"
#include "libguile/objcodes.h"
#include "libguile/instructions.h"
#include "libguile/vm.h"
SCM
scm_c_make_prompt (SCM vm, SCM k, scm_t_uint8 escape_only_p,
scm_t_int64 vm_cookie)
{
scm_t_bits tag;
SCM ret;
struct scm_prompt_registers *regs;
tag = scm_tc7_prompt;
if (escape_only_p)
tag |= SCM_F_PROMPT_ESCAPE;
ret = scm_words (tag, 5);
regs = scm_gc_malloc_pointerless (sizeof (*regs), "prompt registers");
regs->fp = SCM_VM_DATA (vm)->fp;
regs->sp = SCM_VM_DATA (vm)->sp;
regs->ip = SCM_VM_DATA (vm)->ip;
regs->cookie = vm_cookie;
SCM_SET_CELL_OBJECT (ret, 1, k);
SCM_SET_CELL_WORD (ret, 2, (scm_t_bits)regs);
SCM_SET_CELL_OBJECT (ret, 3, scm_i_dynwinds ());
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_t_int64 cookie)
{
SCM cont, winds, prompt = SCM_BOOL_F;
long delta;
size_t i;
/* Search the wind list for an appropriate prompt.
"Waiter, please bring us the wind list." */
for (winds = scm_i_dynwinds (), delta = 0;
scm_is_pair (winds);
winds = SCM_CDR (winds), delta++)
{
SCM elt = SCM_CAR (winds);
if (SCM_PROMPT_P (elt) && scm_is_eq (SCM_PROMPT_TAG (elt), tag))
{
prompt = elt;
break;
}
}
/* If we didn't find anything, print a message and abort the process
right here. If you don't want this, establish a catch-all around
any code that might throw up. */
if (scm_is_false (prompt))
{
/* FIXME: jump to default */
/* scm_handle_by_message (NULL, key, args); */
abort ();
}
cont = reify_partial_continuation (vm, prompt, winds, cookie);
/* Unwind once more, beyond the prompt. */
winds = SCM_CDR (winds), delta++;
/* Unwind */
scm_dowinds (winds, delta);
/* Restore VM regs */
SCM_VM_DATA (vm)->fp = SCM_PROMPT_REGISTERS (prompt)->fp;
SCM_VM_DATA (vm)->sp = SCM_PROMPT_REGISTERS (prompt)->sp;
SCM_VM_DATA (vm)->ip = SCM_PROMPT_REGISTERS (prompt)->ip;
/* Since we're jumping down, we should always have enough space */
if (SCM_VM_DATA (vm)->sp + n + 1 >= SCM_VM_DATA (vm)->stack_limit)
abort ();
/* Push vals */
*(++(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 */
/* Jump! */
SCM_I_LONGJMP (SCM_PROMPT_REGISTERS (prompt)->regs, 1);
/* Shouldn't get here */
abort ();
}
SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
"Abort to the nearest prompt with tag @var{tag}.")
#define FUNC_NAME s_scm_at_abort
{
SCM *argv;
size_t i, n;
SCM_VALIDATE_LIST_COPYLEN (SCM_ARG2, args, n);
argv = alloca (sizeof (SCM)*n);
for (i = 0; i < n; i++, args = scm_cdr (args))
argv[i] = scm_car (args);
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! */
abort ();
}
#undef FUNC_NAME
void scm_init_control (void)
{
#include "control.x"
}
/*
Local Variables:
c-file-style: "gnu"
End:
*/