summaryrefslogtreecommitdiff
path: root/libguile/vm-i-system.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/vm-i-system.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/vm-i-system.c')
-rw-r--r--libguile/vm-i-system.c86
1 files changed, 57 insertions, 29 deletions
diff --git a/libguile/vm-i-system.c b/libguile/vm-i-system.c
index 8981042f8..f30ed9d02 100644
--- a/libguile/vm-i-system.c
+++ b/libguile/vm-i-system.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2001,2008,2009,2010,2011 Free Software Foundation, Inc.
+/* Copyright (C) 2001,2008,2009,2010,2011,2012 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
@@ -1032,25 +1032,49 @@ VM_DEFINE_INSTRUCTION (58, continuation_call, "continuation-call", 0, -1, 0)
VM_DEFINE_INSTRUCTION (59, partial_cont_call, "partial-cont-call", 0, -1, 0)
{
- SCM vmcont, intwinds, prevwinds;
- POP2 (intwinds, vmcont);
+ SCM vmcont;
+ scm_t_ptrdiff reloc;
+ POP (vmcont);
SYNC_REGISTER ();
if (SCM_UNLIKELY (!SCM_VM_CONT_REWINDABLE_P (vmcont)))
{ finish_args = vmcont;
goto vm_error_continuation_not_rewindable;
}
- prevwinds = scm_i_dynwinds ();
- vm_reinstate_partial_continuation (vm, vmcont, intwinds, sp + 1 - fp, fp,
- vm_cookie);
+ reloc = vm_reinstate_partial_continuation (vm, vmcont, sp + 1 - fp, fp,
+ vm_cookie);
- /* Rewind prompt jmpbuffers, if any. */
+ /* The prompt captured a slice of the dynamic stack. Here we wind
+ those entries onto the current thread's stack.
+
+ Unhappily, this code must be here, in vm_engine, so that the setjmp
+ captures the stack in this function, and so that subsequently wound
+ stack entries don't see stale prompts. */
{
- SCM winds = scm_i_dynwinds ();
- for (; !scm_is_eq (winds, prevwinds); winds = scm_cdr (winds))
- if (SCM_PROMPT_P (scm_car (winds)) && SCM_PROMPT_SETJMP (scm_car (winds)))
- break;
+ scm_t_bits *walk;
+
+ for (walk = SCM_DYNSTACK_FIRST (SCM_VM_CONT_DATA (vmcont)->dynstack);
+ SCM_DYNSTACK_TAG (walk);
+ walk = SCM_DYNSTACK_NEXT (walk))
+ {
+ scm_t_bits tag = SCM_DYNSTACK_TAG (walk);
+
+ scm_dynstack_wind_1 (&current_thread->dynstack, walk);
+
+ if (SCM_DYNSTACK_TAG_TYPE (tag) == SCM_DYNSTACK_TYPE_PROMPT)
+ {
+ scm_t_prompt_registers *rewound;
+
+ rewound = scm_dynstack_relocate_prompt (&current_thread->dynstack,
+ reloc, vm_cookie);
+
+ /* Reset the jmpbuf. */
+ if (SCM_I_SETJMP (rewound->regs))
+ /* Non-local exit to this newly rewound prompt. */
+ break;
+ }
+ }
}
-
+
CACHE_REGISTER ();
program = SCM_FRAME_PROGRAM (fp);
CACHE_PROGRAM ();
@@ -1176,9 +1200,12 @@ VM_DEFINE_INSTRUCTION (65, call_cc, "call/cc", 0, 1, 1)
{
int first;
SCM proc, vm_cont, cont;
+ scm_t_dynstack *dynstack;
POP (proc);
SYNC_ALL ();
- vm_cont = scm_i_vm_capture_stack (vp->stack_base, fp, sp, ip, NULL, 0);
+ dynstack = scm_dynstack_capture_all (&current_thread->dynstack);
+ vm_cont = scm_i_vm_capture_stack (vp->stack_base, fp, sp, ip, NULL,
+ dynstack, 0);
cont = scm_i_make_continuation (&first, vm, vm_cont);
if (first)
{
@@ -1211,15 +1238,18 @@ VM_DEFINE_INSTRUCTION (66, tail_call_cc, "tail-call/cc", 0, 1, 1)
{
int first;
SCM proc, vm_cont, cont;
+ scm_t_dynstack *dynstack;
POP (proc);
SYNC_ALL ();
/* In contrast to call/cc, tail-call/cc captures the continuation without the
stack frame. */
+ dynstack = scm_dynstack_capture_all (&current_thread->dynstack);
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),
+ dynstack,
0);
cont = scm_i_make_continuation (&first, vm, vm_cont);
if (first)
@@ -1543,7 +1573,9 @@ VM_DEFINE_INSTRUCTION (85, prompt, "prompt", 4, 2, 0)
{
scm_t_int32 offset;
scm_t_uint8 escape_only_p;
- SCM k, prompt;
+ SCM k;
+ scm_t_dynstack_prompt_flags flags;
+ scm_t_prompt_registers *regs;
escape_only_p = FETCH ();
FETCH_OFFSET (offset);
@@ -1551,10 +1583,10 @@ VM_DEFINE_INSTRUCTION (85, prompt, "prompt", 4, 2, 0)
SYNC_REGISTER ();
/* Push the prompt onto the dynamic stack. */
- prompt = scm_c_make_prompt (k, fp, sp, ip + offset, escape_only_p, vm_cookie,
- scm_i_dynwinds ());
- scm_i_set_dynwinds (scm_cons (prompt, SCM_PROMPT_DYNWINDS (prompt)));
- if (SCM_PROMPT_SETJMP (prompt))
+ regs = scm_c_make_prompt_registers (fp, sp, ip + offset, vm_cookie);
+ flags = escape_only_p ? SCM_F_DYNSTACK_PROMPT_ESCAPE_ONLY : 0;
+ scm_dynstack_push_prompt (&current_thread->dynstack, flags, k, regs);
+ if (SCM_I_SETJMP (regs->regs))
{
/* The prompt exited nonlocally. Cache the regs back from the vp, and go
to the handler.
@@ -1595,7 +1627,7 @@ VM_DEFINE_INSTRUCTION (86, wind, "wind", 0, 2, 0)
finish_args = unwind;
goto vm_error_not_a_thunk;
}
- scm_i_set_dynwinds (scm_cons (scm_cons (wind, unwind), scm_i_dynwinds ()));
+ scm_dynstack_push_dynwind (&current_thread->dynstack, wind, unwind);
NEXT;
}
@@ -1614,32 +1646,28 @@ VM_DEFINE_INSTRUCTION (88, unwind, "unwind", 0, 0, 0)
{
/* A normal exit from the dynamic extent of an expression. Pop the top entry
off of the dynamic stack. */
- scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ()));
+ scm_dynstack_pop (&current_thread->dynstack);
NEXT;
}
VM_DEFINE_INSTRUCTION (89, wind_fluids, "wind-fluids", 1, -1, 0)
{
unsigned n = FETCH ();
- SCM wf;
SYNC_REGISTER ();
sp -= 2 * n;
CHECK_UNDERFLOW ();
- wf = scm_i_make_with_fluids (n, sp + 1, sp + 1 + n);
+ scm_dynstack_push_fluids (&current_thread->dynstack, n, sp + 1, sp + 1 + n,
+ current_thread->dynamic_state);
NULLSTACK (2 * n);
-
- scm_i_swap_with_fluids (wf, current_thread->dynamic_state);
- scm_i_set_dynwinds (scm_cons (wf, scm_i_dynwinds ()));
NEXT;
}
VM_DEFINE_INSTRUCTION (90, unwind_fluids, "unwind-fluids", 0, 0, 0)
{
- SCM wf;
- wf = scm_car (scm_i_dynwinds ());
- scm_i_set_dynwinds (scm_cdr (scm_i_dynwinds ()));
- scm_i_swap_with_fluids (wf, current_thread->dynamic_state);
+ /* This function must not allocate. */
+ scm_dynstack_unwind_fluids (&current_thread->dynstack,
+ current_thread->dynamic_state);
NEXT;
}