Merge commit '202271f291971cf14175f5a1a193955f72d43d79' into vm-check
[bpt/guile.git] / libguile / continuations.c
CommitLineData
dbb605f5 1/* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
dbb605f5
LC
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
0f2d19dd 23
bbd43f03
RB
24#include "libguile/_scm.h"
25
13070bd3 26#include <string.h>
8b7f0bb3 27#include <stdio.h>
13070bd3 28
8b7f0bb3 29#include "libguile/async.h"
d0f6ceb8 30#include "libguile/debug.h"
a0599745
MD
31#include "libguile/root.h"
32#include "libguile/stackchk.h"
5f144b10
GH
33#include "libguile/smob.h"
34#include "libguile/ports.h"
35#include "libguile/dynwind.h"
ce212434 36#include "libguile/values.h"
9de87eea 37#include "libguile/eval.h"
bfffd258 38#include "libguile/vm.h"
5f144b10 39
db4b4ca6 40#include "libguile/validate.h"
a0599745 41#include "libguile/continuations.h"
01c8a3dd 42
0f2d19dd
JB
43\f
44
45/* {Continuations}
46 */
47
92c2555f 48scm_t_bits scm_tc16_continuation;
0f2d19dd 49
e841c3e0
KN
50static SCM
51continuation_mark (SCM obj)
5f144b10 52{
92c2555f 53 scm_t_contregs *continuation = SCM_CONTREGS (obj);
01c8a3dd 54
9de87eea 55 scm_gc_mark (continuation->root);
5f144b10 56 scm_gc_mark (continuation->throw_value);
7ff01700 57 scm_gc_mark (continuation->vm_conts);
5f144b10 58 scm_mark_locations (continuation->stack, continuation->num_stack_items);
193297d8
RB
59#ifdef __ia64__
60 if (continuation->backing_store)
61 scm_mark_locations (continuation->backing_store,
62 continuation->backing_store_size /
63 sizeof (SCM_STACKITEM));
64#endif /* __ia64__ */
5f144b10
GH
65 return continuation->dynenv;
66}
01c8a3dd 67
1be6b49c 68static size_t
e841c3e0 69continuation_free (SCM obj)
5f144b10 70{
92c2555f 71 scm_t_contregs *continuation = SCM_CONTREGS (obj);
9de87eea 72 /* stack array size is 1 if num_stack_items is 0. */
1be6b49c 73 size_t extra_items = (continuation->num_stack_items > 0)
5f144b10
GH
74 ? (continuation->num_stack_items - 1)
75 : 0;
92c2555f 76 size_t bytes_free = sizeof (scm_t_contregs)
5f144b10 77 + extra_items * sizeof (SCM_STACKITEM);
193297d8
RB
78
79#ifdef __ia64__
4c9419ac
MV
80 scm_gc_free (continuation->backing_store, continuation->backing_store_size,
81 "continuation backing store");
193297d8 82#endif /* __ia64__ */
4c9419ac
MV
83 scm_gc_free (continuation, bytes_free, "continuation");
84 return 0;
5f144b10 85}
01c8a3dd 86
e841c3e0 87static int
e81d98ec 88continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
5f144b10 89{
92c2555f 90 scm_t_contregs *continuation = SCM_CONTREGS (obj);
5f144b10
GH
91
92 scm_puts ("#<continuation ", port);
93 scm_intprint (continuation->num_stack_items, 10, port);
94 scm_puts (" @ ", port);
0345e278 95 scm_uintprint (SCM_CELL_WORD_1 (obj), 16, port);
5f144b10
GH
96 scm_putc ('>', port);
97 return 1;
98}
1cc91f1b 99
5f144b10
GH
100/* this may return more than once: the first time with the escape
101 procedure, then subsequently with the value to be passed to the
102 continuation. */
103#define FUNC_NAME "scm_make_continuation"
0f2d19dd 104SCM
5f144b10 105scm_make_continuation (int *first)
0f2d19dd 106{
9de87eea
MV
107 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
108 SCM cont;
92c2555f 109 scm_t_contregs *continuation;
c014a02e 110 long stack_size;
01c8a3dd 111 SCM_STACKITEM * src;
0f2d19dd 112
0f2d19dd 113 SCM_FLUSH_REGISTER_WINDOWS;
9de87eea 114 stack_size = scm_stack_size (thread->continuation_base);
4c9419ac
MV
115 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
116 + (stack_size - 1) * sizeof (SCM_STACKITEM),
117 "continuation");
5f144b10 118 continuation->num_stack_items = stack_size;
9de87eea 119 continuation->dynenv = scm_i_dynwinds ();
5f144b10 120 continuation->throw_value = SCM_EOL;
9de87eea
MV
121 continuation->root = thread->continuation_root;
122 continuation->dframe = scm_i_last_debug_frame ();
123 src = thread->continuation_base;
5f144b10 124 SCM_NEWSMOB (cont, scm_tc16_continuation, continuation);
01c8a3dd 125
4ccb2cd2 126#if ! SCM_STACK_GROWS_UP
5f144b10
GH
127 src -= stack_size;
128#endif
5c5c27dc 129 continuation->offset = continuation->stack - src;
5f144b10 130 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
bfffd258 131 continuation->vm_conts = scm_vm_capture_continuations ();
5f144b10 132
346e4402
NJ
133 *first = !setjmp (continuation->jmpbuf);
134 if (*first)
193297d8 135 {
346e4402 136#ifdef __ia64__
9a5fa6e9 137 continuation->backing_store_size =
346e4402 138 (char *) scm_ia64_ar_bsp(&continuation->jmpbuf.ctx)
9a5fa6e9 139 -
346e4402 140 (char *) thread->register_backing_store_base;
193297d8
RB
141 continuation->backing_store = NULL;
142 continuation->backing_store =
4c9419ac
MV
143 scm_gc_malloc (continuation->backing_store_size,
144 "continuation backing store");
193297d8 145 memcpy (continuation->backing_store,
346e4402 146 (void *) thread->register_backing_store_base,
193297d8 147 continuation->backing_store_size);
346e4402 148#endif /* __ia64__ */
193297d8
RB
149 return cont;
150 }
151 else
152 {
3c468478 153 SCM ret = continuation->throw_value;
3c468478
MV
154 continuation->throw_value = SCM_BOOL_F;
155 return ret;
193297d8 156 }
0f2d19dd 157}
5f144b10 158#undef FUNC_NAME
0f2d19dd 159
d3c6aef9
MV
160
161/* Invoking a continuation proceeds as follows:
162 *
163 * - the stack is made large enough for the called continuation
164 * - the old windchain is unwound down to the branching point
165 * - the continuation stack is copied into place
166 * - the windchain is rewound up to the continuation's context
167 * - the continuation is invoked via longjmp (or setcontext)
168 *
169 * This order is important so that unwind and rewind handlers are run
170 * with their correct stack.
171 */
172
5f144b10 173static void scm_dynthrow (SCM, SCM);
01c8a3dd
DH
174
175/* Grow the stack by a fixed amount to provide space to copy in the
176 * continuation. Possibly this function has to be called several times
177 * recursively before enough space is available. Make sure the compiler does
178 * not optimize the growth array away by storing it's address into a global
179 * variable.
180 */
181
92c2555f 182scm_t_bits scm_i_dummy;
1cc91f1b 183
0f2d19dd 184static void
01c8a3dd
DH
185grow_stack (SCM cont, SCM val)
186{
92c2555f 187 scm_t_bits growth[100];
01c8a3dd 188
92c2555f 189 scm_i_dummy = (scm_t_bits) growth;
01c8a3dd 190 scm_dynthrow (cont, val);
0f2d19dd 191}
0f2d19dd 192
1cc91f1b 193
01c8a3dd
DH
194/* Copy the continuation stack into the current stack. Calling functions from
195 * within this function is safe, since only stack frames below this function's
196 * own frame are overwritten. Thus, memcpy can be used for best performance.
197 */
d3c6aef9
MV
198
199typedef struct {
200 scm_t_contregs *continuation;
201 SCM_STACKITEM *dst;
202} copy_stack_data;
203
01c8a3dd 204static void
d3c6aef9
MV
205copy_stack (void *data)
206{
207 copy_stack_data *d = (copy_stack_data *)data;
208 memcpy (d->dst, d->continuation->stack,
209 sizeof (SCM_STACKITEM) * d->continuation->num_stack_items);
bfffd258 210 scm_vm_reinstate_continuations (d->continuation->vm_conts);
346e4402
NJ
211#ifdef __ia64__
212 SCM_I_CURRENT_THREAD->pending_rbs_continuation = d->continuation;
213#endif
d3c6aef9
MV
214}
215
216static void
217copy_stack_and_call (scm_t_contregs *continuation, SCM val,
5f144b10 218 SCM_STACKITEM * dst)
0f2d19dd 219{
d3c6aef9
MV
220 long delta;
221 copy_stack_data data;
222
9de87eea 223 delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv);
d3c6aef9
MV
224 data.continuation = continuation;
225 data.dst = dst;
14578fa4 226 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
01c8a3dd 227
9de87eea 228 scm_i_set_last_debug_frame (continuation->dframe);
01c8a3dd 229
5f144b10
GH
230 continuation->throw_value = val;
231 longjmp (continuation->jmpbuf, 1);
01c8a3dd
DH
232}
233
346e4402
NJ
234#ifdef __ia64__
235void
236scm_ia64_longjmp (jmp_buf *JB, int VAL)
237{
238 scm_i_thread *t = SCM_I_CURRENT_THREAD;
239
240 if (t->pending_rbs_continuation)
241 {
242 memcpy (t->register_backing_store_base,
243 t->pending_rbs_continuation->backing_store,
244 t->pending_rbs_continuation->backing_store_size);
245 t->pending_rbs_continuation = NULL;
246 }
247 setcontext (&JB->ctx);
248}
249#endif
250
01c8a3dd
DH
251/* Call grow_stack until the stack space is large enough, then, as the current
252 * stack frame might get overwritten, let copy_stack_and_call perform the
253 * actual copying and continuation calling.
254 */
255static void
256scm_dynthrow (SCM cont, SCM val)
257{
9de87eea 258 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
92c2555f 259 scm_t_contregs *continuation = SCM_CONTREGS (cont);
9de87eea 260 SCM_STACKITEM *dst = thread->continuation_base;
01c8a3dd
DH
261 SCM_STACKITEM stack_top_element;
262
8b7f0bb3
MV
263 if (scm_i_critical_section_level)
264 {
265 fprintf (stderr, "continuation invoked from within critical section.\n");
266 abort ();
267 }
268
4ccb2cd2 269#if SCM_STACK_GROWS_UP
5afcf08b 270 if (dst + continuation->num_stack_items >= &stack_top_element)
01c8a3dd 271 grow_stack (cont, val);
0f2d19dd 272#else
5f144b10 273 dst -= continuation->num_stack_items;
c8a1bdc4 274 if (dst <= &stack_top_element)
01c8a3dd 275 grow_stack (cont, val);
0f2d19dd 276#endif /* def SCM_STACK_GROWS_UP */
01c8a3dd 277
5f144b10
GH
278 SCM_FLUSH_REGISTER_WINDOWS;
279 copy_stack_and_call (continuation, val, dst);
0f2d19dd
JB
280}
281
db4b4ca6
DH
282
283static SCM
284continuation_apply (SCM cont, SCM args)
5f144b10 285#define FUNC_NAME "continuation_apply"
0f2d19dd 286{
9de87eea 287 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
92c2555f 288 scm_t_contregs *continuation = SCM_CONTREGS (cont);
5f144b10 289
9de87eea 290 if (continuation->root != thread->continuation_root)
5f144b10 291 {
9de87eea
MV
292 SCM_MISC_ERROR
293 ("invoking continuation would cross continuation barrier: ~A",
294 scm_list_1 (cont));
5f144b10 295 }
0f2d19dd 296
ce212434 297 scm_dynthrow (cont, scm_values (args));
0f2d19dd
JB
298 return SCM_UNSPECIFIED; /* not reached */
299}
5f144b10 300#undef FUNC_NAME
0f2d19dd 301
9de87eea
MV
302SCM
303scm_i_with_continuation_barrier (scm_t_catch_body body,
304 void *body_data,
305 scm_t_catch_handler handler,
43e01b1e
NJ
306 void *handler_data,
307 scm_t_catch_handler pre_unwind_handler,
308 void *pre_unwind_handler_data)
9de87eea
MV
309{
310 SCM_STACKITEM stack_item;
311 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
312 SCM old_controot;
313 SCM_STACKITEM *old_contbase;
314 scm_t_debug_frame *old_lastframe;
315 SCM result;
316
317 /* Establish a fresh continuation root.
318 */
319 old_controot = thread->continuation_root;
320 old_contbase = thread->continuation_base;
321 old_lastframe = thread->last_debug_frame;
322 thread->continuation_root = scm_cons (thread->handle, old_controot);
323 thread->continuation_base = &stack_item;
324 thread->last_debug_frame = NULL;
325
326 /* Call FUNC inside a catch all. This is now guaranteed to return
327 directly and exactly once.
328 */
43e01b1e
NJ
329 result = scm_c_catch (SCM_BOOL_T,
330 body, body_data,
331 handler, handler_data,
332 pre_unwind_handler, pre_unwind_handler_data);
9de87eea
MV
333
334 /* Return to old continuation root.
335 */
336 thread->last_debug_frame = old_lastframe;
337 thread->continuation_base = old_contbase;
338 thread->continuation_root = old_controot;
339
340 return result;
341}
342
343struct c_data {
344 void *(*func) (void *);
345 void *data;
346 void *result;
347};
348
349static SCM
350c_body (void *d)
351{
352 struct c_data *data = (struct c_data *)d;
353 data->result = data->func (data->data);
354 return SCM_UNSPECIFIED;
355}
356
357static SCM
358c_handler (void *d, SCM tag, SCM args)
359{
360 struct c_data *data = (struct c_data *)d;
9de87eea
MV
361 data->result = NULL;
362 return SCM_UNSPECIFIED;
363}
364
365void *
366scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
367{
368 struct c_data c_data;
369 c_data.func = func;
370 c_data.data = data;
371 scm_i_with_continuation_barrier (c_body, &c_data,
43e01b1e
NJ
372 c_handler, &c_data,
373 scm_handle_by_message_noexit, NULL);
9de87eea
MV
374 return c_data.result;
375}
376
377struct scm_data {
378 SCM proc;
379};
380
381static SCM
382scm_body (void *d)
383{
384 struct scm_data *data = (struct scm_data *)d;
385 return scm_call_0 (data->proc);
386}
387
388static SCM
389scm_handler (void *d, SCM tag, SCM args)
390{
9de87eea
MV
391 return SCM_BOOL_F;
392}
393
394SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
395 (SCM proc),
69d2000d
MV
396"Call @var{proc} and return its result. Do not allow the invocation of\n"
397"continuations that would leave or enter the dynamic extent of the call\n"
398"to @code{with-continuation-barrier}. Such an attempt causes an error\n"
399"to be signaled.\n"
400"\n"
401"Throws (such as errors) that are not caught from within @var{proc} are\n"
402"caught by @code{with-continuation-barrier}. In that case, a short\n"
403"message is printed to the current error port and @code{#f} is returned.\n"
404"\n"
405"Thus, @code{with-continuation-barrier} returns exactly once.\n")
9de87eea
MV
406#define FUNC_NAME s_scm_with_continuation_barrier
407{
408 struct scm_data scm_data;
409 scm_data.proc = proc;
410 return scm_i_with_continuation_barrier (scm_body, &scm_data,
43e01b1e
NJ
411 scm_handler, &scm_data,
412 scm_handle_by_message_noexit, NULL);
9de87eea
MV
413}
414#undef FUNC_NAME
db4b4ca6 415
0f2d19dd
JB
416void
417scm_init_continuations ()
0f2d19dd 418{
5f144b10
GH
419 scm_tc16_continuation = scm_make_smob_type ("continuation", 0);
420 scm_set_smob_mark (scm_tc16_continuation, continuation_mark);
421 scm_set_smob_free (scm_tc16_continuation, continuation_free);
422 scm_set_smob_print (scm_tc16_continuation, continuation_print);
423 scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1);
a0599745 424#include "libguile/continuations.x"
0f2d19dd
JB
425}
426
89e00824
ML
427/*
428 Local Variables:
429 c-file-style: "gnu"
430 End:
431*/