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