scm_c_make_frame takes struct scm_frame as arg
[bpt/guile.git] / libguile / continuations.c
CommitLineData
c2247b78 1/* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
0f2d19dd 24
bbd43f03
RB
25#include "libguile/_scm.h"
26
9ede013f 27#include <assert.h>
13070bd3 28#include <string.h>
8b7f0bb3 29#include <stdio.h>
13070bd3 30
8b7f0bb3 31#include "libguile/async.h"
d0f6ceb8 32#include "libguile/debug.h"
a0599745
MD
33#include "libguile/root.h"
34#include "libguile/stackchk.h"
5f144b10
GH
35#include "libguile/smob.h"
36#include "libguile/ports.h"
9ede013f 37#include "libguile/dynstack.h"
9de87eea 38#include "libguile/eval.h"
bfffd258 39#include "libguile/vm.h"
1d1cae0e 40#include "libguile/instructions.h"
5f144b10 41
db4b4ca6 42#include "libguile/validate.h"
a0599745 43#include "libguile/continuations.h"
01c8a3dd 44
0f2d19dd
JB
45\f
46
1d1cae0e
AW
47static scm_t_bits tc16_continuation;
48#define SCM_CONTREGSP(x) SCM_TYP16_PREDICATE (tc16_continuation, x)
49
50#define SCM_CONTREGS(x) ((scm_t_contregs *) SCM_SMOB_DATA_1 (x))
51
52#define SCM_CONTINUATION_LENGTH(x) (SCM_CONTREGS (x)->num_stack_items)
53#define SCM_SET_CONTINUATION_LENGTH(x, n)\
54 (SCM_CONTREGS (x)->num_stack_items = (n))
55#define SCM_JMPBUF(x) ((SCM_CONTREGS (x))->jmpbuf)
1d1cae0e
AW
56#define SCM_CONTINUATION_ROOT(x) ((SCM_CONTREGS (x))->root)
57#define SCM_DFRAME(x) ((SCM_CONTREGS (x))->dframe)
58
59\f
60
d691ac20
AW
61/* scm_i_make_continuation will return a procedure whose code will
62 reinstate the continuation. Here, as in gsubr.c, we define the form
63 of that trampoline function.
0f2d19dd
JB
64 */
65
d691ac20
AW
66static const scm_t_uint32 continuation_stub_code[] =
67 {
095100bb 68 SCM_PACK_OP_24 (continuation_call, 0)
d691ac20 69 };
1d1cae0e 70
1d1cae0e
AW
71static SCM
72make_continuation_trampoline (SCM contregs)
73{
d691ac20
AW
74 SCM ret;
75 scm_t_bits nfree = 1;
76 scm_t_bits flags = SCM_F_PROGRAM_IS_CONTINUATION;
77
e0755cd1 78 ret = scm_words (scm_tc7_program | (nfree << 16) | flags, nfree + 2);
d691ac20 79 SCM_SET_CELL_WORD_1 (ret, continuation_stub_code);
d798a895 80 SCM_PROGRAM_FREE_VARIABLE_SET (ret, 0, contregs);
1d1cae0e
AW
81
82 return ret;
83}
84
85
86/* {Continuations}
87 */
0f2d19dd 88
01c8a3dd 89
e841c3e0 90static int
e81d98ec 91continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
5f144b10 92{
92c2555f 93 scm_t_contregs *continuation = SCM_CONTREGS (obj);
5f144b10 94
0607ebbf 95 scm_puts_unlocked ("#<continuation ", port);
5f144b10 96 scm_intprint (continuation->num_stack_items, 10, port);
0607ebbf 97 scm_puts_unlocked (" @ ", port);
0236bc68 98 scm_uintprint (SCM_SMOB_DATA_1 (obj), 16, port);
0607ebbf 99 scm_putc_unlocked ('>', port);
5f144b10
GH
100 return 1;
101}
1cc91f1b 102
2acdd822
AW
103/* James Clark came up with this neat one instruction fix for
104 * continuations on the SPARC. It flushes the register windows so
105 * that all the state of the process is contained in the stack.
106 */
107
108#if defined (sparc) || defined (__sparc__) || defined (__sparc)
109# define SCM_FLUSH_REGISTER_WINDOWS asm("ta 3")
110#else
111# define SCM_FLUSH_REGISTER_WINDOWS /* empty */
112#endif
113
5f144b10 114/* this may return more than once: the first time with the escape
d8873dfe
AW
115 procedure, then subsequently with SCM_UNDEFINED (the vals already having been
116 placed on the VM stack). */
997659f8 117#define FUNC_NAME "scm_i_make_continuation"
0f2d19dd 118SCM
796e54a7 119scm_i_make_continuation (int *first, struct scm_vm *vp, SCM vm_cont)
0f2d19dd 120{
9de87eea
MV
121 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
122 SCM cont;
92c2555f 123 scm_t_contregs *continuation;
c014a02e 124 long stack_size;
01c8a3dd 125 SCM_STACKITEM * src;
0f2d19dd 126
0f2d19dd 127 SCM_FLUSH_REGISTER_WINDOWS;
9de87eea 128 stack_size = scm_stack_size (thread->continuation_base);
4c9419ac
MV
129 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
130 + (stack_size - 1) * sizeof (SCM_STACKITEM),
131 "continuation");
5f144b10 132 continuation->num_stack_items = stack_size;
9de87eea 133 continuation->root = thread->continuation_root;
9de87eea 134 src = thread->continuation_base;
4ccb2cd2 135#if ! SCM_STACK_GROWS_UP
5f144b10
GH
136 src -= stack_size;
137#endif
5c5c27dc 138 continuation->offset = continuation->stack - src;
5f144b10 139 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
796e54a7 140 continuation->vp = vp;
269479e3 141 continuation->vm_cont = vm_cont;
5f144b10 142
1d1cae0e 143 SCM_NEWSMOB (cont, tc16_continuation, continuation);
79824460 144
a4dbe1ac 145 *first = !SCM_I_SETJMP (continuation->jmpbuf);
346e4402 146 if (*first)
193297d8 147 {
346e4402 148#ifdef __ia64__
9a5fa6e9 149 continuation->backing_store_size =
346e4402 150 (char *) scm_ia64_ar_bsp(&continuation->jmpbuf.ctx)
9a5fa6e9 151 -
346e4402 152 (char *) thread->register_backing_store_base;
193297d8
RB
153 continuation->backing_store = NULL;
154 continuation->backing_store =
4c9419ac
MV
155 scm_gc_malloc (continuation->backing_store_size,
156 "continuation backing store");
193297d8 157 memcpy (continuation->backing_store,
346e4402 158 (void *) thread->register_backing_store_base,
193297d8 159 continuation->backing_store_size);
346e4402 160#endif /* __ia64__ */
1d1cae0e 161 return make_continuation_trampoline (cont);
193297d8
RB
162 }
163 else
c2247b78
AW
164 {
165 scm_gc_after_nonlocal_exit ();
166 return SCM_UNDEFINED;
167 }
0f2d19dd 168}
5f144b10 169#undef FUNC_NAME
0f2d19dd 170
1d1cae0e
AW
171SCM
172scm_i_continuation_to_frame (SCM continuation)
173{
174 SCM contregs;
175 scm_t_contregs *cont;
176
d798a895 177 contregs = SCM_PROGRAM_FREE_VARIABLE_REF (continuation, 0);
1d1cae0e
AW
178 cont = SCM_CONTREGS (contregs);
179
269479e3 180 if (scm_is_true (cont->vm_cont))
1d1cae0e 181 {
8de051da 182 struct scm_frame frame;
269479e3 183 struct scm_vm_cont *data = SCM_VM_CONT_DATA (cont->vm_cont);
8de051da
AW
184
185 frame.stack_holder = data;
186 frame.fp_offset = (data->fp + data->reloc) - data->stack_base;
187 frame.sp_offset = (data->sp + data->reloc) - data->stack_base;
188 frame.ip = data->ra;
189
190 return scm_c_make_frame (SCM_VM_FRAME_KIND_CONT, &frame);
1d1cae0e
AW
191 }
192 else
193 return SCM_BOOL_F;
194}
195
796e54a7
AW
196struct scm_vm *
197scm_i_contregs_vp (SCM contregs)
d8873dfe 198{
796e54a7 199 return SCM_CONTREGS (contregs)->vp;
d8873dfe
AW
200}
201
202SCM
203scm_i_contregs_vm_cont (SCM contregs)
204{
205 return SCM_CONTREGS (contregs)->vm_cont;
206}
207
1d1cae0e
AW
208
209/* {Apply}
210 */
d3c6aef9
MV
211
212/* Invoking a continuation proceeds as follows:
213 *
214 * - the stack is made large enough for the called continuation
215 * - the old windchain is unwound down to the branching point
216 * - the continuation stack is copied into place
217 * - the windchain is rewound up to the continuation's context
218 * - the continuation is invoked via longjmp (or setcontext)
219 *
220 * This order is important so that unwind and rewind handlers are run
221 * with their correct stack.
222 */
223
d8873dfe 224static void scm_dynthrow (SCM);
01c8a3dd
DH
225
226/* Grow the stack by a fixed amount to provide space to copy in the
227 * continuation. Possibly this function has to be called several times
228 * recursively before enough space is available. Make sure the compiler does
229 * not optimize the growth array away by storing it's address into a global
230 * variable.
231 */
232
8c93b597 233static scm_t_bits scm_i_dummy;
1cc91f1b 234
0f2d19dd 235static void
d8873dfe 236grow_stack (SCM cont)
01c8a3dd 237{
92c2555f 238 scm_t_bits growth[100];
01c8a3dd 239
92c2555f 240 scm_i_dummy = (scm_t_bits) growth;
d8873dfe 241 scm_dynthrow (cont);
0f2d19dd 242}
0f2d19dd 243
1cc91f1b 244
01c8a3dd
DH
245/* Copy the continuation stack into the current stack. Calling functions from
246 * within this function is safe, since only stack frames below this function's
247 * own frame are overwritten. Thus, memcpy can be used for best performance.
248 */
d3c6aef9 249
d3c6aef9 250static void
d8873dfe 251copy_stack_and_call (scm_t_contregs *continuation,
5f144b10 252 SCM_STACKITEM * dst)
0f2d19dd 253{
9ede013f
AW
254 scm_t_dynstack *dynstack;
255 scm_t_bits *joint;
256 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
257
258 dynstack = SCM_VM_CONT_DATA (continuation->vm_cont)->dynstack;
259
260 joint = scm_dynstack_unwind_fork (&thread->dynstack, dynstack);
261
262 memcpy (dst, continuation->stack,
263 sizeof (SCM_STACKITEM) * continuation->num_stack_items);
264#ifdef __ia64__
265 thread->pending_rbs_continuation = continuation;
266#endif
d3c6aef9 267
9ede013f 268 scm_dynstack_wind (&thread->dynstack, joint);
01c8a3dd 269
a4dbe1ac 270 SCM_I_LONGJMP (continuation->jmpbuf, 1);
01c8a3dd
DH
271}
272
346e4402
NJ
273#ifdef __ia64__
274void
a4dbe1ac 275scm_ia64_longjmp (scm_i_jmp_buf *JB, int VAL)
346e4402
NJ
276{
277 scm_i_thread *t = SCM_I_CURRENT_THREAD;
278
279 if (t->pending_rbs_continuation)
280 {
281 memcpy (t->register_backing_store_base,
282 t->pending_rbs_continuation->backing_store,
283 t->pending_rbs_continuation->backing_store_size);
284 t->pending_rbs_continuation = NULL;
285 }
286 setcontext (&JB->ctx);
287}
288#endif
289
01c8a3dd
DH
290/* Call grow_stack until the stack space is large enough, then, as the current
291 * stack frame might get overwritten, let copy_stack_and_call perform the
292 * actual copying and continuation calling.
293 */
294static void
d8873dfe 295scm_dynthrow (SCM cont)
01c8a3dd 296{
9de87eea 297 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
92c2555f 298 scm_t_contregs *continuation = SCM_CONTREGS (cont);
9de87eea 299 SCM_STACKITEM *dst = thread->continuation_base;
01c8a3dd
DH
300 SCM_STACKITEM stack_top_element;
301
87f30eda 302 if (thread->critical_section_level)
8b7f0bb3
MV
303 {
304 fprintf (stderr, "continuation invoked from within critical section.\n");
305 abort ();
306 }
307
4ccb2cd2 308#if SCM_STACK_GROWS_UP
5afcf08b 309 if (dst + continuation->num_stack_items >= &stack_top_element)
d8873dfe 310 grow_stack (cont);
0f2d19dd 311#else
5f144b10 312 dst -= continuation->num_stack_items;
c8a1bdc4 313 if (dst <= &stack_top_element)
d8873dfe 314 grow_stack (cont);
0f2d19dd 315#endif /* def SCM_STACK_GROWS_UP */
01c8a3dd 316
5f144b10 317 SCM_FLUSH_REGISTER_WINDOWS;
d8873dfe 318 copy_stack_and_call (continuation, dst);
0f2d19dd
JB
319}
320
db4b4ca6 321
1d1cae0e 322void
d8873dfe 323scm_i_check_continuation (SCM cont)
0f2d19dd 324{
9de87eea 325 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
92c2555f 326 scm_t_contregs *continuation = SCM_CONTREGS (cont);
5f144b10 327
d223c3fc 328 if (!scm_is_eq (continuation->root, thread->continuation_root))
1d1cae0e
AW
329 scm_misc_error
330 ("%continuation-call",
331 "invoking continuation would cross continuation barrier: ~A",
332 scm_list_1 (cont));
d8873dfe
AW
333}
334
335void
336scm_i_reinstate_continuation (SCM cont)
337{
338 scm_dynthrow (cont);
0f2d19dd 339}
0f2d19dd 340
9de87eea
MV
341SCM
342scm_i_with_continuation_barrier (scm_t_catch_body body,
343 void *body_data,
344 scm_t_catch_handler handler,
43e01b1e
NJ
345 void *handler_data,
346 scm_t_catch_handler pre_unwind_handler,
347 void *pre_unwind_handler_data)
9de87eea
MV
348{
349 SCM_STACKITEM stack_item;
350 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
351 SCM old_controot;
352 SCM_STACKITEM *old_contbase;
9de87eea
MV
353 SCM result;
354
355 /* Establish a fresh continuation root.
356 */
357 old_controot = thread->continuation_root;
358 old_contbase = thread->continuation_base;
9de87eea
MV
359 thread->continuation_root = scm_cons (thread->handle, old_controot);
360 thread->continuation_base = &stack_item;
9de87eea
MV
361
362 /* Call FUNC inside a catch all. This is now guaranteed to return
363 directly and exactly once.
364 */
43e01b1e
NJ
365 result = scm_c_catch (SCM_BOOL_T,
366 body, body_data,
367 handler, handler_data,
368 pre_unwind_handler, pre_unwind_handler_data);
9de87eea
MV
369
370 /* Return to old continuation root.
371 */
9de87eea
MV
372 thread->continuation_base = old_contbase;
373 thread->continuation_root = old_controot;
374
375 return result;
376}
377
e309f3bf
AW
378\f
379
380static int
381should_print_backtrace (SCM tag, SCM stack)
382{
383 return SCM_BACKTRACE_P
384 && scm_is_true (stack)
385 && scm_initialized_p
386 /* It's generally not useful to print backtraces for errors reading
387 or expanding code in these fallback catch statements. */
388 && !scm_is_eq (tag, scm_from_latin1_symbol ("read-error"))
389 && !scm_is_eq (tag, scm_from_latin1_symbol ("syntax-error"));
390}
391
392static void
393print_exception_and_backtrace (SCM port, SCM tag, SCM args)
394{
395 SCM stack, frame;
396
397 /* We get here via a throw to a catch-all. In that case there is the
398 throw frame active, and this catch closure, so narrow by two
399 frames. */
400 stack = scm_make_stack (SCM_BOOL_T, scm_list_1 (scm_from_int (2)));
401 frame = scm_is_true (stack) ? scm_stack_ref (stack, SCM_INUM0) : SCM_BOOL_F;
402
403 if (should_print_backtrace (tag, stack))
404 {
0607ebbf 405 scm_puts_unlocked ("Backtrace:\n", port);
e309f3bf
AW
406 scm_display_backtrace_with_highlights (stack, port,
407 SCM_BOOL_F, SCM_BOOL_F,
408 SCM_EOL);
409 scm_newline (port);
410 }
411
412 scm_print_exception (port, frame, tag, args);
413}
414
415\f
416
9de87eea
MV
417struct c_data {
418 void *(*func) (void *);
419 void *data;
420 void *result;
421};
422
423static SCM
424c_body (void *d)
425{
426 struct c_data *data = (struct c_data *)d;
427 data->result = data->func (data->data);
428 return SCM_UNSPECIFIED;
429}
430
431static SCM
432c_handler (void *d, SCM tag, SCM args)
433{
e309f3bf
AW
434 struct c_data *data;
435
436 /* If TAG is `quit', exit() the process. */
437 if (scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
438 exit (scm_exit_status (args));
439
440 data = (struct c_data *)d;
9de87eea
MV
441 data->result = NULL;
442 return SCM_UNSPECIFIED;
443}
444
e309f3bf
AW
445static SCM
446pre_unwind_handler (void *error_port, SCM tag, SCM args)
447{
448 /* Print the exception unless TAG is `quit'. */
449 if (!scm_is_eq (tag, scm_from_latin1_symbol ("quit")))
21041372 450 print_exception_and_backtrace (SCM_PACK_POINTER (error_port), tag, args);
e309f3bf
AW
451
452 return SCM_UNSPECIFIED;
453}
454
9de87eea
MV
455void *
456scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
457{
458 struct c_data c_data;
459 c_data.func = func;
460 c_data.data = data;
461 scm_i_with_continuation_barrier (c_body, &c_data,
43e01b1e 462 c_handler, &c_data,
e309f3bf 463 pre_unwind_handler,
21041372 464 SCM_UNPACK_POINTER (scm_current_error_port ()));
9de87eea
MV
465 return c_data.result;
466}
467
468struct scm_data {
469 SCM proc;
470};
471
472static SCM
473scm_body (void *d)
474{
475 struct scm_data *data = (struct scm_data *)d;
476 return scm_call_0 (data->proc);
477}
478
479static SCM
480scm_handler (void *d, SCM tag, SCM args)
481{
e309f3bf
AW
482 /* Print a message. Note that if TAG is `quit', this will exit() the
483 process. */
484 scm_handle_by_message_noexit (NULL, tag, args);
485
9de87eea
MV
486 return SCM_BOOL_F;
487}
488
489SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
490 (SCM proc),
69d2000d
MV
491"Call @var{proc} and return its result. Do not allow the invocation of\n"
492"continuations that would leave or enter the dynamic extent of the call\n"
493"to @code{with-continuation-barrier}. Such an attempt causes an error\n"
494"to be signaled.\n"
495"\n"
496"Throws (such as errors) that are not caught from within @var{proc} are\n"
497"caught by @code{with-continuation-barrier}. In that case, a short\n"
498"message is printed to the current error port and @code{#f} is returned.\n"
499"\n"
500"Thus, @code{with-continuation-barrier} returns exactly once.\n")
9de87eea
MV
501#define FUNC_NAME s_scm_with_continuation_barrier
502{
503 struct scm_data scm_data;
504 scm_data.proc = proc;
505 return scm_i_with_continuation_barrier (scm_body, &scm_data,
43e01b1e 506 scm_handler, &scm_data,
e309f3bf 507 pre_unwind_handler,
21041372 508 SCM_UNPACK_POINTER (scm_current_error_port ()));
9de87eea
MV
509}
510#undef FUNC_NAME
db4b4ca6 511
0f2d19dd
JB
512void
513scm_init_continuations ()
0f2d19dd 514{
1d1cae0e
AW
515 tc16_continuation = scm_make_smob_type ("continuation", 0);
516 scm_set_smob_print (tc16_continuation, continuation_print);
a0599745 517#include "libguile/continuations.x"
0f2d19dd
JB
518}
519
89e00824
ML
520/*
521 Local Variables:
522 c-file-style: "gnu"
523 End:
524*/