Fix parenthesizing of the `ROUND_UP' macro; factorize.
[bpt/guile.git] / libguile / continuations.c
1 /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "libguile/_scm.h"
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #include "libguile/async.h"
31 #include "libguile/debug.h"
32 #include "libguile/root.h"
33 #include "libguile/stackchk.h"
34 #include "libguile/smob.h"
35 #include "libguile/ports.h"
36 #include "libguile/dynwind.h"
37 #include "libguile/eval.h"
38 #include "libguile/vm.h"
39 #include "libguile/instructions.h"
40
41 #include "libguile/validate.h"
42 #include "libguile/continuations.h"
43
44 \f
45
46 static scm_t_bits tc16_continuation;
47 #define SCM_CONTREGSP(x) SCM_TYP16_PREDICATE (tc16_continuation, x)
48
49 #define SCM_CONTREGS(x) ((scm_t_contregs *) SCM_SMOB_DATA_1 (x))
50
51 #define SCM_CONTINUATION_LENGTH(x) (SCM_CONTREGS (x)->num_stack_items)
52 #define SCM_SET_CONTINUATION_LENGTH(x, n)\
53 (SCM_CONTREGS (x)->num_stack_items = (n))
54 #define SCM_JMPBUF(x) ((SCM_CONTREGS (x))->jmpbuf)
55 #define SCM_DYNENV(x) ((SCM_CONTREGS (x))->dynenv)
56 #define SCM_CONTINUATION_ROOT(x) ((SCM_CONTREGS (x))->root)
57 #define SCM_DFRAME(x) ((SCM_CONTREGS (x))->dframe)
58
59 \f
60
61 /* scm_i_make_continuation will return a procedure whose objcode contains an
62 instruction to reinstate the continuation. Here, as in gsubr.c and smob.c, we
63 define the form of that trampoline function.
64 */
65
66 #ifdef WORDS_BIGENDIAN
67 #define OBJCODE_HEADER(main,meta) 0, 0, 0, main, 0, 0, 0, meta+8
68 #define META_HEADER(meta) 0, 0, 0, meta, 0, 0, 0, 0
69 #else
70 #define OBJCODE_HEADER(main,meta) main, 0, 0, 0, meta+8, 0, 0, 0
71 #define META_HEADER(meta) meta, 0, 0, 0, 0, 0, 0, 0
72 #endif
73
74 #define ALIGN_PTR(type,p,align) (type*)(ROUND_UP (((scm_t_bits)p), align))
75
76 #ifdef SCM_ALIGNED
77 #define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym)\
78 static const type sym[]
79 #define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym)\
80 static SCM_ALIGNED (alignment) const type sym[]
81 #else
82 #define SCM_DECLARE_STATIC_ALIGNED_ARRAY(type, sym)\
83 static type *sym
84 #define SCM_STATIC_ALIGNED_ARRAY(alignment, type, sym) \
85 SCM_SNARF_INIT(sym = scm_malloc (sizeof(sym##__unaligned) + alignment - 1); \
86 sym = ALIGN_PTR (type, sym, alignment); \
87 memcpy (sym, sym##__unaligned, sizeof(sym##__unaligned));) \
88 static type *sym = NULL; \
89 static const type sym##__unaligned[]
90 #endif
91
92 #define STATIC_OBJCODE_TAG \
93 SCM_PACK (scm_tc7_objcode | (SCM_F_OBJCODE_IS_STATIC << 8))
94
95 #define SCM_STATIC_OBJCODE(sym) \
96 SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
97 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
98 { STATIC_OBJCODE_TAG, SCM_PACK (sym##__bytecode) }, \
99 { SCM_BOOL_F, SCM_PACK (0) } \
100 }; \
101 static const SCM sym = SCM_PACK (sym##__cells); \
102 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
103
104
105 SCM_STATIC_OBJCODE (cont_objcode) = {
106 /* This code is the same as in gsubr.c, except we use continuation_call
107 instead of subr_call. */
108 OBJCODE_HEADER (8, 19),
109 /* leave args on the stack */
110 /* 0 */ scm_op_object_ref, 0, /* push scm_t_contregs smob */
111 /* 2 */ scm_op_continuation_call, /* and longjmp (whee) */
112 /* 3 */ scm_op_nop, /* pad to 8 bytes */
113 /* 4 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop,
114 /* 8 */
115
116 /* We could put some meta-info to say that this proc is a continuation. Not sure
117 how to do that, though. */
118 META_HEADER (19),
119 /* 0 */ scm_op_make_eol, /* bindings */
120 /* 1 */ scm_op_make_eol, /* sources */
121 /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 3, /* arity: from ip 0 to ip 3 */
122 /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
123 /* 7 */ scm_op_make_int8_0, /* 0 optionals */
124 /* 8 */ scm_op_make_true, /* and a rest arg */
125 /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
126 /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
127 /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
128 /* 18 */ scm_op_return /* and return */
129 /* 19 */
130 };
131
132
133 SCM_STATIC_OBJCODE (call_cc_objcode) = {
134 /* Before Scheme's call/cc is compiled, eval.c will use this hand-coded
135 call/cc. */
136 OBJCODE_HEADER (8, 17),
137 /* 0 */ scm_op_assert_nargs_ee, 0, 1, /* assert that nargs==1 */
138 /* 3 */ scm_op_local_ref, 0, /* push the proc */
139 /* 5 */ scm_op_tail_call_cc, /* and call/cc */
140 /* 6 */ scm_op_nop, scm_op_nop, /* pad to 8 bytes */
141 /* 8 */
142
143 META_HEADER (17),
144 /* 0 */ scm_op_make_eol, /* bindings */
145 /* 1 */ scm_op_make_eol, /* sources */
146 /* 2 */ scm_op_make_int8, 3, scm_op_make_int8, 6, /* arity: from ip 0 to ip 6 */
147 /* 6 */ scm_op_make_int8_1, /* the arity is 0 required args */
148 /* 7 */ scm_op_list, 0, 3, /* make a list of those 5 vals */
149 /* 10 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
150 /* 13 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
151 /* 16 */ scm_op_return /* and return */
152 /* 17 */
153 };
154
155
156 static SCM
157 make_continuation_trampoline (SCM contregs)
158 {
159 SCM ret = scm_make_program (cont_objcode,
160 scm_c_make_vector (1, contregs),
161 SCM_BOOL_F);
162 SCM_SET_CELL_WORD_0 (ret,
163 SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_CONTINUATION);
164
165 return ret;
166 }
167
168
169 /* {Continuations}
170 */
171
172
173 static int
174 continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
175 {
176 scm_t_contregs *continuation = SCM_CONTREGS (obj);
177
178 scm_puts ("#<continuation ", port);
179 scm_intprint (continuation->num_stack_items, 10, port);
180 scm_puts (" @ ", port);
181 scm_uintprint (SCM_SMOB_DATA_1 (obj), 16, port);
182 scm_putc ('>', port);
183 return 1;
184 }
185
186 /* this may return more than once: the first time with the escape
187 procedure, then subsequently with SCM_UNDEFINED (the vals already having been
188 placed on the VM stack). */
189 #define FUNC_NAME "scm_i_make_continuation"
190 SCM
191 scm_i_make_continuation (int *first, SCM vm, SCM vm_cont)
192 {
193 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
194 SCM cont;
195 scm_t_contregs *continuation;
196 long stack_size;
197 SCM_STACKITEM * src;
198
199 SCM_FLUSH_REGISTER_WINDOWS;
200 stack_size = scm_stack_size (thread->continuation_base);
201 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
202 + (stack_size - 1) * sizeof (SCM_STACKITEM),
203 "continuation");
204 continuation->num_stack_items = stack_size;
205 continuation->dynenv = scm_i_dynwinds ();
206 continuation->root = thread->continuation_root;
207 src = thread->continuation_base;
208 #if ! SCM_STACK_GROWS_UP
209 src -= stack_size;
210 #endif
211 continuation->offset = continuation->stack - src;
212 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
213 continuation->vm = vm;
214 continuation->vm_cont = vm_cont;
215
216 SCM_NEWSMOB (cont, tc16_continuation, continuation);
217
218 *first = !SCM_I_SETJMP (continuation->jmpbuf);
219 if (*first)
220 {
221 #ifdef __ia64__
222 continuation->backing_store_size =
223 (char *) scm_ia64_ar_bsp(&continuation->jmpbuf.ctx)
224 -
225 (char *) thread->register_backing_store_base;
226 continuation->backing_store = NULL;
227 continuation->backing_store =
228 scm_gc_malloc (continuation->backing_store_size,
229 "continuation backing store");
230 memcpy (continuation->backing_store,
231 (void *) thread->register_backing_store_base,
232 continuation->backing_store_size);
233 #endif /* __ia64__ */
234 return make_continuation_trampoline (cont);
235 }
236 else
237 return SCM_UNDEFINED;
238 }
239 #undef FUNC_NAME
240
241 SCM
242 scm_i_call_with_current_continuation (SCM proc)
243 {
244 static SCM call_cc = SCM_BOOL_F;
245
246 if (scm_is_false (call_cc))
247 call_cc = scm_make_program (call_cc_objcode, SCM_BOOL_F, SCM_BOOL_F);
248
249 return scm_call_1 (call_cc, proc);
250 }
251
252 SCM
253 scm_i_continuation_to_frame (SCM continuation)
254 {
255 SCM contregs;
256 scm_t_contregs *cont;
257
258 contregs = scm_c_vector_ref (scm_program_objects (continuation), 0);
259 cont = SCM_CONTREGS (contregs);
260
261 if (scm_is_true (cont->vm_cont))
262 {
263 struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont->vm_cont);
264 return scm_c_make_frame (cont->vm_cont,
265 data->fp + data->reloc,
266 data->sp + data->reloc,
267 data->ra,
268 data->reloc);
269 }
270 else
271 return SCM_BOOL_F;
272 }
273
274 SCM
275 scm_i_contregs_vm (SCM contregs)
276 {
277 return SCM_CONTREGS (contregs)->vm;
278 }
279
280 SCM
281 scm_i_contregs_vm_cont (SCM contregs)
282 {
283 return SCM_CONTREGS (contregs)->vm_cont;
284 }
285
286
287 /* {Apply}
288 */
289
290 /* Invoking a continuation proceeds as follows:
291 *
292 * - the stack is made large enough for the called continuation
293 * - the old windchain is unwound down to the branching point
294 * - the continuation stack is copied into place
295 * - the windchain is rewound up to the continuation's context
296 * - the continuation is invoked via longjmp (or setcontext)
297 *
298 * This order is important so that unwind and rewind handlers are run
299 * with their correct stack.
300 */
301
302 static void scm_dynthrow (SCM);
303
304 /* Grow the stack by a fixed amount to provide space to copy in the
305 * continuation. Possibly this function has to be called several times
306 * recursively before enough space is available. Make sure the compiler does
307 * not optimize the growth array away by storing it's address into a global
308 * variable.
309 */
310
311 static scm_t_bits scm_i_dummy;
312
313 static void
314 grow_stack (SCM cont)
315 {
316 scm_t_bits growth[100];
317
318 scm_i_dummy = (scm_t_bits) growth;
319 scm_dynthrow (cont);
320 }
321
322
323 /* Copy the continuation stack into the current stack. Calling functions from
324 * within this function is safe, since only stack frames below this function's
325 * own frame are overwritten. Thus, memcpy can be used for best performance.
326 */
327
328 typedef struct {
329 scm_t_contregs *continuation;
330 SCM_STACKITEM *dst;
331 } copy_stack_data;
332
333 static void
334 copy_stack (void *data)
335 {
336 copy_stack_data *d = (copy_stack_data *)data;
337 memcpy (d->dst, d->continuation->stack,
338 sizeof (SCM_STACKITEM) * d->continuation->num_stack_items);
339 #ifdef __ia64__
340 SCM_I_CURRENT_THREAD->pending_rbs_continuation = d->continuation;
341 #endif
342 }
343
344 static void
345 copy_stack_and_call (scm_t_contregs *continuation,
346 SCM_STACKITEM * dst)
347 {
348 long delta;
349 copy_stack_data data;
350
351 delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv);
352 data.continuation = continuation;
353 data.dst = dst;
354 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
355
356 SCM_I_LONGJMP (continuation->jmpbuf, 1);
357 }
358
359 #ifdef __ia64__
360 void
361 scm_ia64_longjmp (scm_i_jmp_buf *JB, int VAL)
362 {
363 scm_i_thread *t = SCM_I_CURRENT_THREAD;
364
365 if (t->pending_rbs_continuation)
366 {
367 memcpy (t->register_backing_store_base,
368 t->pending_rbs_continuation->backing_store,
369 t->pending_rbs_continuation->backing_store_size);
370 t->pending_rbs_continuation = NULL;
371 }
372 setcontext (&JB->ctx);
373 }
374 #endif
375
376 /* Call grow_stack until the stack space is large enough, then, as the current
377 * stack frame might get overwritten, let copy_stack_and_call perform the
378 * actual copying and continuation calling.
379 */
380 static void
381 scm_dynthrow (SCM cont)
382 {
383 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
384 scm_t_contregs *continuation = SCM_CONTREGS (cont);
385 SCM_STACKITEM *dst = thread->continuation_base;
386 SCM_STACKITEM stack_top_element;
387
388 if (thread->critical_section_level)
389 {
390 fprintf (stderr, "continuation invoked from within critical section.\n");
391 abort ();
392 }
393
394 #if SCM_STACK_GROWS_UP
395 if (dst + continuation->num_stack_items >= &stack_top_element)
396 grow_stack (cont);
397 #else
398 dst -= continuation->num_stack_items;
399 if (dst <= &stack_top_element)
400 grow_stack (cont);
401 #endif /* def SCM_STACK_GROWS_UP */
402
403 SCM_FLUSH_REGISTER_WINDOWS;
404 copy_stack_and_call (continuation, dst);
405 }
406
407
408 void
409 scm_i_check_continuation (SCM cont)
410 {
411 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
412 scm_t_contregs *continuation = SCM_CONTREGS (cont);
413
414 if (continuation->root != thread->continuation_root)
415 scm_misc_error
416 ("%continuation-call",
417 "invoking continuation would cross continuation barrier: ~A",
418 scm_list_1 (cont));
419 }
420
421 void
422 scm_i_reinstate_continuation (SCM cont)
423 {
424 scm_dynthrow (cont);
425 }
426
427 SCM
428 scm_i_with_continuation_barrier (scm_t_catch_body body,
429 void *body_data,
430 scm_t_catch_handler handler,
431 void *handler_data,
432 scm_t_catch_handler pre_unwind_handler,
433 void *pre_unwind_handler_data)
434 {
435 SCM_STACKITEM stack_item;
436 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
437 SCM old_controot;
438 SCM_STACKITEM *old_contbase;
439 SCM result;
440
441 /* Establish a fresh continuation root.
442 */
443 old_controot = thread->continuation_root;
444 old_contbase = thread->continuation_base;
445 thread->continuation_root = scm_cons (thread->handle, old_controot);
446 thread->continuation_base = &stack_item;
447
448 /* Call FUNC inside a catch all. This is now guaranteed to return
449 directly and exactly once.
450 */
451 result = scm_c_catch (SCM_BOOL_T,
452 body, body_data,
453 handler, handler_data,
454 pre_unwind_handler, pre_unwind_handler_data);
455
456 /* Return to old continuation root.
457 */
458 thread->continuation_base = old_contbase;
459 thread->continuation_root = old_controot;
460
461 return result;
462 }
463
464 struct c_data {
465 void *(*func) (void *);
466 void *data;
467 void *result;
468 };
469
470 static SCM
471 c_body (void *d)
472 {
473 struct c_data *data = (struct c_data *)d;
474 data->result = data->func (data->data);
475 return SCM_UNSPECIFIED;
476 }
477
478 static SCM
479 c_handler (void *d, SCM tag, SCM args)
480 {
481 struct c_data *data = (struct c_data *)d;
482 data->result = NULL;
483 return SCM_UNSPECIFIED;
484 }
485
486 void *
487 scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
488 {
489 struct c_data c_data;
490 c_data.func = func;
491 c_data.data = data;
492 scm_i_with_continuation_barrier (c_body, &c_data,
493 c_handler, &c_data,
494 scm_handle_by_message_noexit, NULL);
495 return c_data.result;
496 }
497
498 struct scm_data {
499 SCM proc;
500 };
501
502 static SCM
503 scm_body (void *d)
504 {
505 struct scm_data *data = (struct scm_data *)d;
506 return scm_call_0 (data->proc);
507 }
508
509 static SCM
510 scm_handler (void *d, SCM tag, SCM args)
511 {
512 return SCM_BOOL_F;
513 }
514
515 SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
516 (SCM proc),
517 "Call @var{proc} and return its result. Do not allow the invocation of\n"
518 "continuations that would leave or enter the dynamic extent of the call\n"
519 "to @code{with-continuation-barrier}. Such an attempt causes an error\n"
520 "to be signaled.\n"
521 "\n"
522 "Throws (such as errors) that are not caught from within @var{proc} are\n"
523 "caught by @code{with-continuation-barrier}. In that case, a short\n"
524 "message is printed to the current error port and @code{#f} is returned.\n"
525 "\n"
526 "Thus, @code{with-continuation-barrier} returns exactly once.\n")
527 #define FUNC_NAME s_scm_with_continuation_barrier
528 {
529 struct scm_data scm_data;
530 scm_data.proc = proc;
531 return scm_i_with_continuation_barrier (scm_body, &scm_data,
532 scm_handler, &scm_data,
533 scm_handle_by_message_noexit, NULL);
534 }
535 #undef FUNC_NAME
536
537 void
538 scm_init_continuations ()
539 {
540 tc16_continuation = scm_make_smob_type ("continuation", 0);
541 scm_set_smob_print (tc16_continuation, continuation_print);
542 #include "libguile/continuations.x"
543 }
544
545 /*
546 Local Variables:
547 c-file-style: "gnu"
548 End:
549 */