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