summaryrefslogtreecommitdiff
path: root/libguile/control.c
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2012-03-03 17:01:16 +0100
committerAndy Wingo <wingo@pobox.com>2012-03-03 17:06:02 +0100
commit9ede013f68361df731cadc62844be11d1bfea7e5 (patch)
tree5f507a75e7aa6f932a44f1ddf98045137cd6913d /libguile/control.c
parent05b4d9106d301af055d675cd0ffdd3699642a0ee (diff)
downloadguile-9ede013f68361df731cadc62844be11d1bfea7e5.tar.gz
the dynamic stack is really a stack now, instead of a list
* libguile/dynstack.h: * libguile/dynstack.c: New files, implementing the dynamic stack as a true stack instead of a linked list. This lowers the cost of dynwinds: frames, winders, prompts, with-fluids, and dynamic-wind. For the most part, we allocate these items directly on the stack. * libguile/dynwinds.h: * libguile/dynwinds.c: Adapt all manipulators of the wind stack to use interfaces from dynstack.c. Remove heap-allocated winder and frame object types. (scm_dowinds, scm_i_dowinds): Remove these. The first was exported, but it was not a public interface. * libguile/continuations.c: * libguile/continuations.h (scm_t_contregs): Continuation objects reference scm_t_dynstack* values now. Adapt to the new interfaces. * libguile/control.c: * libguile/control.h: There is no longer a scm_tc7_prompt kind of object that can be allocated on the heap. Instead, the prompt flags, key, and registers are pushed on the dynwind stack. (The registers are still on the heap.) Also, since the vm_cont will reference the dynwinds, make the partial continuation stub take just one extra arg, instead of storing the intwinds separately in the object table. * libguile/fluids.c: * libguile/fluids.h: No more with-fluids objects; instead, the fluids go on the dynstack. The values still have to be on the heap, though. (scm_prepare_fluids, scm_swap_fluids): New internal functions, replacing scm_i_make_with_fluids and scm_i_swap_with_fluids. * libguile/print.c: Remove prompt and with-fluids printers. * libguile/tags.h: Revert prompt and with-fluids tc7 values to what they were before they were allocated. * libguile/vm-i-system.c (partial_cont_call): Just pop the vmcont, the intwinds will not be passed as a second arg. Rewind the dynamic stack from within the VM, so that any rewinder sees valid prompt entries. (call_cc, tail_call_cc): Adapt to pass the dynstack to scm_i_vm_capture_stack. (prompt, wind, unwind, wind_fluids, unwind_fluids): Adapt to the new interfaces. * libguile/vm.h (scm_i_capture_current_stack): Rename from scm_i_vm_capture_continuation. (scm_i_vm_capture_stack): Take a dynstack as an argument. * libguile/vm.c (vm_reinstate_partial_continuation): Don't wind here, as that could result in winders seeing invalid prompts. * libguile/eval.c: * libguile/root.c: * libguile/stacks.c: * libguile/threads.c: * libguile/threads.h: * libguile/throw.c: Adapt other users of dynwinds to use the dynstack.
Diffstat (limited to 'libguile/control.c')
-rw-r--r--libguile/control.c119
1 files changed, 48 insertions, 71 deletions
diff --git a/libguile/control.c b/libguile/control.c
index ff6bfd89a..613ffbe4c 100644
--- a/libguile/control.c
+++ b/libguile/control.c
@@ -30,18 +30,18 @@
+#define PROMPT_ESCAPE_P(p) \
+ (SCM_DYNSTACK_TAG_FLAGS (SCM_DYNSTACK_TAG (p)) \
+ & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
-SCM
-scm_c_make_prompt (SCM k, SCM *fp, SCM *sp, scm_t_uint8 *abort_ip,
- scm_t_uint8 escape_only_p, scm_t_int64 vm_cookie,
- SCM winds)
-{
- scm_t_bits tag;
- struct scm_prompt_registers *regs;
+
- tag = scm_tc7_prompt;
- if (escape_only_p)
- tag |= (SCM_F_PROMPT_ESCAPE<<8);
+
+scm_t_prompt_registers*
+scm_c_make_prompt_registers (SCM *fp, SCM *sp, scm_t_uint8 *abort_ip,
+ scm_t_int64 vm_cookie)
+{
+ scm_t_prompt_registers *regs;
regs = scm_gc_malloc_pointerless (sizeof (*regs), "prompt registers");
regs->fp = fp;
@@ -49,11 +49,10 @@ scm_c_make_prompt (SCM k, SCM *fp, SCM *sp, scm_t_uint8 *abort_ip,
regs->ip = abort_ip;
regs->cookie = vm_cookie;
- return scm_double_cell (tag, SCM_UNPACK (k), (scm_t_bits)regs,
- SCM_UNPACK (winds));
+ return regs;
}
-/* Only to be called if the SCM_PROMPT_SETJMP returns 1 */
+/* Only to be called if the SCM_I_SETJMP returns 1 */
SCM
scm_i_prompt_pop_abort_args_x (SCM vm)
{
@@ -115,9 +114,9 @@ SCM_STATIC_OBJCODE (cont_objcode) = {
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_partial_cont_call, /* and go! */
- /* 5 */ scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
+ /* 2 */ scm_op_partial_cont_call, /* and go! */
+ /* 3 */ scm_op_nop,
+ /* 4 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop, /* pad to 8 bytes */
/* 8 */
/* We could put some meta-info to say that this proc is a continuation. Not sure
@@ -125,7 +124,7 @@ SCM_STATIC_OBJCODE (cont_objcode) = {
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, 5, /* arity: from ip 0 to ip 7 */
+ /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 3, /* arity: from ip 0 to ip 3 */
/* 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 */
@@ -138,45 +137,35 @@ SCM_STATIC_OBJCODE (cont_objcode) = {
static SCM
-reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
+reify_partial_continuation (SCM vm, scm_t_prompt_registers *regs,
+ scm_t_dynstack *dynstack,
scm_t_int64 cookie)
{
- SCM vm_cont, dynwinds, intwinds = SCM_EOL, ret;
+ SCM vm_cont, 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)
+ if (cookie >= 0 && regs->cookie == cookie)
flags |= SCM_F_VM_CONT_REWINDABLE;
/* Since non-escape continuations should begin with a thunk application, the
first bit of the stack should be a frame, with the saved fp equal to the fp
that was current when the prompt was made. */
- if ((SCM*)SCM_UNPACK (SCM_PROMPT_REGISTERS (prompt)->sp[1])
- != SCM_PROMPT_REGISTERS (prompt)->fp)
+ if ((SCM*)SCM_UNPACK (regs->sp[1]) != regs->fp)
abort ();
/* Capture from the top of the thunk application frame up to the end. Set an
MVRA only, as the post-abort code is in an MV context. */
- vm_cont = scm_i_vm_capture_stack (SCM_PROMPT_REGISTERS (prompt)->sp + 4,
+ vm_cont = scm_i_vm_capture_stack (regs->sp + 4,
SCM_VM_DATA (vm)->fp,
SCM_VM_DATA (vm)->sp,
NULL,
SCM_VM_DATA (vm)->ip,
+ dynstack,
flags);
ret = scm_make_program (cont_objcode,
- scm_vector (scm_list_2 (vm_cont, intwinds)),
+ scm_c_make_vector (1, vm_cont),
SCM_BOOL_F);
SCM_SET_CELL_WORD_0 (ret,
SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_PARTIAL_CONTINUATION);
@@ -186,46 +175,42 @@ reify_partial_continuation (SCM vm, SCM prompt, SCM extwinds,
void
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;
+ SCM cont;
+ scm_t_dynstack *dynstack = &SCM_I_CURRENT_THREAD->dynstack;
+ scm_t_bits *prompt;
+ scm_t_prompt_registers *regs;
+ scm_t_dynstack_prompt_flags flags;
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, raise an error. */
- if (scm_is_false (prompt))
+ prompt = scm_dynstack_find_prompt (dynstack, tag, &regs, &flags);
+
+ if (!prompt)
scm_misc_error ("abort", "Abort to unknown prompt", scm_list_1 (tag));
- cont = reify_partial_continuation (vm, prompt, winds, cookie);
+ /* Only reify if the continuation referenced in the handler. */
+ if (flags & SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY)
+ cont = SCM_BOOL_F;
+ else
+ {
+ scm_t_dynstack *captured;
- /* Unwind once more, beyond the prompt. */
- winds = SCM_CDR (winds), delta++;
+ captured = scm_dynstack_capture (dynstack, SCM_DYNSTACK_NEXT (prompt));
+ cont = reify_partial_continuation (vm, regs, captured, cookie);
+ }
- /* Unwind */
- scm_dowinds (winds, delta);
+ /* Unwind. */
+ scm_dynstack_unwind (dynstack, prompt);
/* Unwinding may have changed the current thread's VM, so use the
new one. */
vm = scm_the_vm ();
/* 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;
+ SCM_VM_DATA (vm)->fp = regs->fp;
+ SCM_VM_DATA (vm)->sp = regs->sp;
+ SCM_VM_DATA (vm)->ip = regs->ip;
- /* Since we're jumping down, we should always have enough space */
+ /* 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 ();
@@ -236,7 +221,7 @@ scm_c_abort (SCM vm, SCM tag, size_t n, SCM *argv, scm_t_int64 cookie)
*(++(SCM_VM_DATA (vm)->sp)) = scm_from_size_t (n+1); /* +1 for continuation */
/* Jump! */
- SCM_I_LONGJMP (SCM_PROMPT_REGISTERS (prompt)->regs, 1);
+ SCM_I_LONGJMP (regs->regs, 1);
/* Shouldn't get here */
abort ();
@@ -266,14 +251,6 @@ SCM_DEFINE (scm_at_abort, "@abort", 2, 0, 0, (SCM tag, SCM args),
#undef FUNC_NAME
void
-scm_i_prompt_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
-{
- scm_puts_unlocked ("#<prompt ", port);
- scm_intprint (SCM_UNPACK (exp), 16, port);
- scm_putc_unlocked ('>', port);
-}
-
-void
scm_init_control (void)
{
#include "libguile/control.x"