Update Gnulib to v0.0-6827-g39c3009; use the `dirfd' module.
[bpt/guile.git] / libguile / continuations.c
1 /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008, 2009, 2010, 2011 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_pointerless (sizeof(sym##__unaligned)); \
86 memcpy (sym, sym##__unaligned, sizeof(sym##__unaligned));) \
87 static type *sym = NULL; \
88 static const type sym##__unaligned[]
89 #endif
90
91 #define STATIC_OBJCODE_TAG \
92 SCM_PACK (SCM_MAKE_OBJCODE_TAG (SCM_OBJCODE_TYPE_STATIC, 0))
93
94 #define SCM_STATIC_OBJCODE(sym) \
95 SCM_DECLARE_STATIC_ALIGNED_ARRAY (scm_t_uint8, sym##__bytecode); \
96 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_cell, sym##__cells) = { \
97 { STATIC_OBJCODE_TAG, SCM_PACK (sym##__bytecode) }, \
98 { SCM_BOOL_F, SCM_PACK (0) } \
99 }; \
100 static const SCM sym = SCM_PACK (sym##__cells); \
101 SCM_STATIC_ALIGNED_ARRAY (8, scm_t_uint8, sym##__bytecode)
102
103
104 SCM_STATIC_OBJCODE (cont_objcode) = {
105 /* This code is the same as in gsubr.c, except we use continuation_call
106 instead of subr_call. */
107 OBJCODE_HEADER (8, 19),
108 /* leave args on the stack */
109 /* 0 */ scm_op_object_ref, 0, /* push scm_t_contregs smob */
110 /* 2 */ scm_op_continuation_call, /* and longjmp (whee) */
111 /* 3 */ scm_op_nop, /* pad to 8 bytes */
112 /* 4 */ scm_op_nop, scm_op_nop, scm_op_nop, scm_op_nop,
113 /* 8 */
114
115 /* We could put some meta-info to say that this proc is a continuation. Not sure
116 how to do that, though. */
117 META_HEADER (19),
118 /* 0 */ scm_op_make_eol, /* bindings */
119 /* 1 */ scm_op_make_eol, /* sources */
120 /* 2 */ scm_op_make_int8, 0, scm_op_make_int8, 3, /* arity: from ip 0 to ip 3 */
121 /* 6 */ scm_op_make_int8_0, /* the arity is 0 required args */
122 /* 7 */ scm_op_make_int8_0, /* 0 optionals */
123 /* 8 */ scm_op_make_true, /* and a rest arg */
124 /* 9 */ scm_op_list, 0, 5, /* make a list of those 5 vals */
125 /* 12 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
126 /* 15 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
127 /* 18 */ scm_op_return /* and return */
128 /* 19 */
129 };
130
131
132 SCM_STATIC_OBJCODE (call_cc_objcode) = {
133 /* Before Scheme's call/cc is compiled, eval.c will use this hand-coded
134 call/cc. */
135 OBJCODE_HEADER (8, 17),
136 /* 0 */ scm_op_assert_nargs_ee, 0, 1, /* assert that nargs==1 */
137 /* 3 */ scm_op_local_ref, 0, /* push the proc */
138 /* 5 */ scm_op_tail_call_cc, /* and call/cc */
139 /* 6 */ scm_op_nop, scm_op_nop, /* pad to 8 bytes */
140 /* 8 */
141
142 META_HEADER (17),
143 /* 0 */ scm_op_make_eol, /* bindings */
144 /* 1 */ scm_op_make_eol, /* sources */
145 /* 2 */ scm_op_make_int8, 3, scm_op_make_int8, 6, /* arity: from ip 0 to ip 6 */
146 /* 6 */ scm_op_make_int8_1, /* the arity is 0 required args */
147 /* 7 */ scm_op_list, 0, 3, /* make a list of those 5 vals */
148 /* 10 */ scm_op_list, 0, 1, /* and the arities will be a list of that one list */
149 /* 13 */ scm_op_list, 0, 3, /* pack bindings, sources, and arities into list */
150 /* 16 */ scm_op_return /* and return */
151 /* 17 */
152 };
153
154
155 static SCM
156 make_continuation_trampoline (SCM contregs)
157 {
158 SCM ret = scm_make_program (cont_objcode,
159 scm_c_make_vector (1, contregs),
160 SCM_BOOL_F);
161 SCM_SET_CELL_WORD_0 (ret,
162 SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_CONTINUATION);
163
164 return ret;
165 }
166
167
168 /* {Continuations}
169 */
170
171
172 static int
173 continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
174 {
175 scm_t_contregs *continuation = SCM_CONTREGS (obj);
176
177 scm_puts ("#<continuation ", port);
178 scm_intprint (continuation->num_stack_items, 10, port);
179 scm_puts (" @ ", port);
180 scm_uintprint (SCM_SMOB_DATA_1 (obj), 16, port);
181 scm_putc ('>', port);
182 return 1;
183 }
184
185 /* this may return more than once: the first time with the escape
186 procedure, then subsequently with SCM_UNDEFINED (the vals already having been
187 placed on the VM stack). */
188 #define FUNC_NAME "scm_i_make_continuation"
189 SCM
190 scm_i_make_continuation (int *first, SCM vm, SCM vm_cont)
191 {
192 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
193 SCM cont;
194 scm_t_contregs *continuation;
195 long stack_size;
196 SCM_STACKITEM * src;
197
198 SCM_FLUSH_REGISTER_WINDOWS;
199 stack_size = scm_stack_size (thread->continuation_base);
200 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
201 + (stack_size - 1) * sizeof (SCM_STACKITEM),
202 "continuation");
203 continuation->num_stack_items = stack_size;
204 continuation->dynenv = scm_i_dynwinds ();
205 continuation->root = thread->continuation_root;
206 src = thread->continuation_base;
207 #if ! SCM_STACK_GROWS_UP
208 src -= stack_size;
209 #endif
210 continuation->offset = continuation->stack - src;
211 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
212 continuation->vm = vm;
213 continuation->vm_cont = vm_cont;
214
215 SCM_NEWSMOB (cont, tc16_continuation, continuation);
216
217 *first = !SCM_I_SETJMP (continuation->jmpbuf);
218 if (*first)
219 {
220 #ifdef __ia64__
221 continuation->backing_store_size =
222 (char *) scm_ia64_ar_bsp(&continuation->jmpbuf.ctx)
223 -
224 (char *) thread->register_backing_store_base;
225 continuation->backing_store = NULL;
226 continuation->backing_store =
227 scm_gc_malloc (continuation->backing_store_size,
228 "continuation backing store");
229 memcpy (continuation->backing_store,
230 (void *) thread->register_backing_store_base,
231 continuation->backing_store_size);
232 #endif /* __ia64__ */
233 return make_continuation_trampoline (cont);
234 }
235 else
236 return SCM_UNDEFINED;
237 }
238 #undef FUNC_NAME
239
240 SCM
241 scm_i_call_with_current_continuation (SCM proc)
242 {
243 static SCM call_cc = SCM_BOOL_F;
244
245 if (scm_is_false (call_cc))
246 call_cc = scm_make_program (call_cc_objcode, SCM_BOOL_F, SCM_BOOL_F);
247
248 return scm_call_1 (call_cc, proc);
249 }
250
251 SCM
252 scm_i_continuation_to_frame (SCM continuation)
253 {
254 SCM contregs;
255 scm_t_contregs *cont;
256
257 contregs = scm_c_vector_ref (scm_program_objects (continuation), 0);
258 cont = SCM_CONTREGS (contregs);
259
260 if (scm_is_true (cont->vm_cont))
261 {
262 struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont->vm_cont);
263 return scm_c_make_frame (cont->vm_cont,
264 data->fp + data->reloc,
265 data->sp + data->reloc,
266 data->ra,
267 data->reloc);
268 }
269 else
270 return SCM_BOOL_F;
271 }
272
273 SCM
274 scm_i_contregs_vm (SCM contregs)
275 {
276 return SCM_CONTREGS (contregs)->vm;
277 }
278
279 SCM
280 scm_i_contregs_vm_cont (SCM contregs)
281 {
282 return SCM_CONTREGS (contregs)->vm_cont;
283 }
284
285
286 /* {Apply}
287 */
288
289 /* Invoking a continuation proceeds as follows:
290 *
291 * - the stack is made large enough for the called continuation
292 * - the old windchain is unwound down to the branching point
293 * - the continuation stack is copied into place
294 * - the windchain is rewound up to the continuation's context
295 * - the continuation is invoked via longjmp (or setcontext)
296 *
297 * This order is important so that unwind and rewind handlers are run
298 * with their correct stack.
299 */
300
301 static void scm_dynthrow (SCM);
302
303 /* Grow the stack by a fixed amount to provide space to copy in the
304 * continuation. Possibly this function has to be called several times
305 * recursively before enough space is available. Make sure the compiler does
306 * not optimize the growth array away by storing it's address into a global
307 * variable.
308 */
309
310 static scm_t_bits scm_i_dummy;
311
312 static void
313 grow_stack (SCM cont)
314 {
315 scm_t_bits growth[100];
316
317 scm_i_dummy = (scm_t_bits) growth;
318 scm_dynthrow (cont);
319 }
320
321
322 /* Copy the continuation stack into the current stack. Calling functions from
323 * within this function is safe, since only stack frames below this function's
324 * own frame are overwritten. Thus, memcpy can be used for best performance.
325 */
326
327 typedef struct {
328 scm_t_contregs *continuation;
329 SCM_STACKITEM *dst;
330 } copy_stack_data;
331
332 static void
333 copy_stack (void *data)
334 {
335 copy_stack_data *d = (copy_stack_data *)data;
336 memcpy (d->dst, d->continuation->stack,
337 sizeof (SCM_STACKITEM) * d->continuation->num_stack_items);
338 #ifdef __ia64__
339 SCM_I_CURRENT_THREAD->pending_rbs_continuation = d->continuation;
340 #endif
341 }
342
343 static void
344 copy_stack_and_call (scm_t_contregs *continuation,
345 SCM_STACKITEM * dst)
346 {
347 long delta;
348 copy_stack_data data;
349
350 delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv);
351 data.continuation = continuation;
352 data.dst = dst;
353 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
354
355 SCM_I_LONGJMP (continuation->jmpbuf, 1);
356 }
357
358 #ifdef __ia64__
359 void
360 scm_ia64_longjmp (scm_i_jmp_buf *JB, int VAL)
361 {
362 scm_i_thread *t = SCM_I_CURRENT_THREAD;
363
364 if (t->pending_rbs_continuation)
365 {
366 memcpy (t->register_backing_store_base,
367 t->pending_rbs_continuation->backing_store,
368 t->pending_rbs_continuation->backing_store_size);
369 t->pending_rbs_continuation = NULL;
370 }
371 setcontext (&JB->ctx);
372 }
373 #endif
374
375 /* Call grow_stack until the stack space is large enough, then, as the current
376 * stack frame might get overwritten, let copy_stack_and_call perform the
377 * actual copying and continuation calling.
378 */
379 static void
380 scm_dynthrow (SCM cont)
381 {
382 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
383 scm_t_contregs *continuation = SCM_CONTREGS (cont);
384 SCM_STACKITEM *dst = thread->continuation_base;
385 SCM_STACKITEM stack_top_element;
386
387 if (thread->critical_section_level)
388 {
389 fprintf (stderr, "continuation invoked from within critical section.\n");
390 abort ();
391 }
392
393 #if SCM_STACK_GROWS_UP
394 if (dst + continuation->num_stack_items >= &stack_top_element)
395 grow_stack (cont);
396 #else
397 dst -= continuation->num_stack_items;
398 if (dst <= &stack_top_element)
399 grow_stack (cont);
400 #endif /* def SCM_STACK_GROWS_UP */
401
402 SCM_FLUSH_REGISTER_WINDOWS;
403 copy_stack_and_call (continuation, dst);
404 }
405
406
407 void
408 scm_i_check_continuation (SCM cont)
409 {
410 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
411 scm_t_contregs *continuation = SCM_CONTREGS (cont);
412
413 if (!scm_is_eq (continuation->root, thread->continuation_root))
414 scm_misc_error
415 ("%continuation-call",
416 "invoking continuation would cross continuation barrier: ~A",
417 scm_list_1 (cont));
418 }
419
420 void
421 scm_i_reinstate_continuation (SCM cont)
422 {
423 scm_dynthrow (cont);
424 }
425
426 SCM
427 scm_i_with_continuation_barrier (scm_t_catch_body body,
428 void *body_data,
429 scm_t_catch_handler handler,
430 void *handler_data,
431 scm_t_catch_handler pre_unwind_handler,
432 void *pre_unwind_handler_data)
433 {
434 SCM_STACKITEM stack_item;
435 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
436 SCM old_controot;
437 SCM_STACKITEM *old_contbase;
438 SCM result;
439
440 /* Establish a fresh continuation root.
441 */
442 old_controot = thread->continuation_root;
443 old_contbase = thread->continuation_base;
444 thread->continuation_root = scm_cons (thread->handle, old_controot);
445 thread->continuation_base = &stack_item;
446
447 /* Call FUNC inside a catch all. This is now guaranteed to return
448 directly and exactly once.
449 */
450 result = scm_c_catch (SCM_BOOL_T,
451 body, body_data,
452 handler, handler_data,
453 pre_unwind_handler, pre_unwind_handler_data);
454
455 /* Return to old continuation root.
456 */
457 thread->continuation_base = old_contbase;
458 thread->continuation_root = old_controot;
459
460 return result;
461 }
462
463 \f
464
465 static int
466 should_print_backtrace (SCM tag, SCM stack)
467 {
468 return SCM_BACKTRACE_P
469 && scm_is_true (stack)
470 && scm_initialized_p
471 /* It's generally not useful to print backtraces for errors reading
472 or expanding code in these fallback catch statements. */
473 && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
474 && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
475 }
476
477 static void
478 print_exception_and_backtrace (SCM port, SCM tag, SCM args)
479 {
480 SCM stack, frame;
481
482 /* We get here via a throw to a catch-all. In that case there is the
483 throw frame active, and this catch closure, so narrow by two
484 frames. */
485 stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
486 frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
487
488 if (should_print_backtrace (tag, stack))
489 {
490 scm_puts ("Backtrace:\n", port);
491 scm_display_backtrace_with_highlights (stack, port,
492 SCM_BOOL_F, SCM_BOOL_F,
493 SCM_EOL);
494 scm_newline (port);
495 }
496
497 scm_print_exception (port, frame, tag, args);
498 }
499
500 \f
501
502 struct c_data {
503 void *(*func) (void *);
504 void *data;
505 void *result;
506 };
507
508 static SCM
509 c_body (void *d)
510 {
511 struct c_data *data = (struct c_data *)d;
512 data->result = data->func (data->data);
513 return SCM_UNSPECIFIED;
514 }
515
516 static SCM
517 c_handler (void *d, SCM tag, SCM args)
518 {
519 struct c_data *data;
520
521 /* If TAG is `quit', exit() the process. */
522 if (scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
523 exit (scm_exit_status (args));
524
525 data = (struct c_data *)d;
526 data->result = NULL;
527 return SCM_UNSPECIFIED;
528 }
529
530 static SCM
531 pre_unwind_handler (void *error_port, SCM tag, SCM args)
532 {
533 /* Print the exception unless TAG is `quit'. */
534 if (!scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
535 print_exception_and_backtrace (PTR2SCM (error_port), tag, args);
536
537 return SCM_UNSPECIFIED;
538 }
539
540 void *
541 scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
542 {
543 struct c_data c_data;
544 c_data.func = func;
545 c_data.data = data;
546 scm_i_with_continuation_barrier (c_body, &c_data,
547 c_handler, &c_data,
548 pre_unwind_handler,
549 SCM2PTR (scm_current_error_port ()));
550 return c_data.result;
551 }
552
553 struct scm_data {
554 SCM proc;
555 };
556
557 static SCM
558 scm_body (void *d)
559 {
560 struct scm_data *data = (struct scm_data *)d;
561 return scm_call_0 (data->proc);
562 }
563
564 static SCM
565 scm_handler (void *d, SCM tag, SCM args)
566 {
567 /* Print a message. Note that if TAG is `quit', this will exit() the
568 process. */
569 scm_handle_by_message_noexit (NULL, tag, args);
570
571 return SCM_BOOL_F;
572 }
573
574 SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
575 (SCM proc),
576 "Call @var{proc} and return its result. Do not allow the invocation of\n"
577 "continuations that would leave or enter the dynamic extent of the call\n"
578 "to @code{with-continuation-barrier}. Such an attempt causes an error\n"
579 "to be signaled.\n"
580 "\n"
581 "Throws (such as errors) that are not caught from within @var{proc} are\n"
582 "caught by @code{with-continuation-barrier}. In that case, a short\n"
583 "message is printed to the current error port and @code{#f} is returned.\n"
584 "\n"
585 "Thus, @code{with-continuation-barrier} returns exactly once.\n")
586 #define FUNC_NAME s_scm_with_continuation_barrier
587 {
588 struct scm_data scm_data;
589 scm_data.proc = proc;
590 return scm_i_with_continuation_barrier (scm_body, &scm_data,
591 scm_handler, &scm_data,
592 pre_unwind_handler,
593 SCM2PTR (scm_current_error_port ()));
594 }
595 #undef FUNC_NAME
596
597 void
598 scm_init_continuations ()
599 {
600 tc16_continuation = scm_make_smob_type ("continuation", 0);
601 scm_set_smob_print (tc16_continuation, continuation_print);
602 #include "libguile/continuations.x"
603 }
604
605 /*
606 Local Variables:
607 c-file-style: "gnu"
608 End:
609 */