* variable.c, threads.c, struct.c, stackchk.c, smob.c, root.c,
[bpt/guile.git] / libguile / continuations.c
1 /* Copyright (C) 1995,1996,1998,2000,2001,2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20
21 #include "libguile/_scm.h"
22
23 #include <string.h>
24
25 #include "libguile/debug.h"
26 #include "libguile/root.h"
27 #include "libguile/stackchk.h"
28 #include "libguile/smob.h"
29 #include "libguile/ports.h"
30 #include "libguile/dynwind.h"
31 #include "libguile/values.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/continuations.h"
35
36 \f
37
38 /* {Continuations}
39 */
40
41 scm_t_bits scm_tc16_continuation;
42
43 static SCM
44 continuation_mark (SCM obj)
45 {
46 scm_t_contregs *continuation = SCM_CONTREGS (obj);
47
48 scm_gc_mark (continuation->throw_value);
49 scm_mark_locations (continuation->stack, continuation->num_stack_items);
50 #ifdef __ia64__
51 if (continuation->backing_store)
52 scm_mark_locations (continuation->backing_store,
53 continuation->backing_store_size /
54 sizeof (SCM_STACKITEM));
55 #endif /* __ia64__ */
56 return continuation->dynenv;
57 }
58
59 static size_t
60 continuation_free (SCM obj)
61 {
62 scm_t_contregs *continuation = SCM_CONTREGS (obj);
63 /* stack array size is 1 if num_stack_items is 0 (rootcont). */
64 size_t extra_items = (continuation->num_stack_items > 0)
65 ? (continuation->num_stack_items - 1)
66 : 0;
67 size_t bytes_free = sizeof (scm_t_contregs)
68 + extra_items * sizeof (SCM_STACKITEM);
69
70 #ifdef __ia64__
71 scm_gc_free (continuation->backing_store, continuation->backing_store_size,
72 "continuation backing store");
73 #endif /* __ia64__ */
74 scm_gc_free (continuation, bytes_free, "continuation");
75 return 0;
76 }
77
78 static int
79 continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
80 {
81 scm_t_contregs *continuation = SCM_CONTREGS (obj);
82
83 scm_puts ("#<continuation ", port);
84 scm_intprint (continuation->num_stack_items, 10, port);
85 scm_puts (" @ ", port);
86 scm_uintprint (SCM_CELL_WORD_1 (obj), 16, port);
87 scm_putc ('>', port);
88 return 1;
89 }
90
91 #ifdef __ia64__
92 /* Extern declaration of getcontext()/setcontext() in order to redefine
93 getcontext() since on ia64-linux the second return value indicates whether
94 it returned from getcontext() itself or by running setcontext(). */
95 struct rv
96 {
97 long retval;
98 long first_return;
99 };
100 extern struct rv ia64_getcontext (ucontext_t *) __asm__ ("getcontext");
101 #endif /* __ia64__ */
102
103 /* this may return more than once: the first time with the escape
104 procedure, then subsequently with the value to be passed to the
105 continuation. */
106 #define FUNC_NAME "scm_make_continuation"
107 SCM
108 scm_make_continuation (int *first)
109 {
110 volatile SCM cont;
111 scm_t_contregs *continuation;
112 scm_t_contregs *rootcont = SCM_CONTREGS (scm_rootcont);
113 long stack_size;
114 SCM_STACKITEM * src;
115 #ifdef __ia64__
116 struct rv rv;
117 #endif /* __ia64__ */
118
119 SCM_ENTER_A_SECTION;
120 SCM_FLUSH_REGISTER_WINDOWS;
121 stack_size = scm_stack_size (rootcont->base);
122 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
123 + (stack_size - 1) * sizeof (SCM_STACKITEM),
124 "continuation");
125 continuation->num_stack_items = stack_size;
126 continuation->dynenv = scm_dynwinds;
127 continuation->throw_value = SCM_EOL;
128 continuation->base = src = rootcont->base;
129 continuation->seq = rootcont->seq;
130 continuation->dframe = scm_last_debug_frame;
131 SCM_NEWSMOB (cont, scm_tc16_continuation, continuation);
132 SCM_EXIT_A_SECTION;
133
134 #if ! SCM_STACK_GROWS_UP
135 src -= stack_size;
136 #endif
137 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
138
139 #ifdef __ia64__
140 rv = ia64_getcontext (&continuation->ctx);
141 if (rv.first_return)
142 {
143 continuation->backing_store_size =
144 continuation->ctx.uc_mcontext.sc_ar_bsp -
145 (unsigned long) __libc_ia64_register_backing_store_base;
146 continuation->backing_store = NULL;
147 continuation->backing_store =
148 scm_gc_malloc (continuation->backing_store_size,
149 "continuation backing store");
150 memcpy (continuation->backing_store,
151 (void *) __libc_ia64_register_backing_store_base,
152 continuation->backing_store_size);
153 *first = 1;
154 return cont;
155 }
156 else
157 {
158 SCM ret = continuation->throw_value;
159 *first = 0;
160 continuation->throw_value = SCM_BOOL_F;
161 return ret;
162 }
163 #else /* !__ia64__ */
164 if (setjmp (continuation->jmpbuf))
165 {
166 SCM ret = continuation->throw_value;
167 *first = 0;
168 continuation->throw_value = SCM_BOOL_F;
169 return ret;
170 }
171 else
172 {
173 *first = 1;
174 return cont;
175 }
176 #endif /* !__ia64__ */
177 }
178 #undef FUNC_NAME
179
180
181 /* Invoking a continuation proceeds as follows:
182 *
183 * - the stack is made large enough for the called continuation
184 * - the old windchain is unwound down to the branching point
185 * - the continuation stack is copied into place
186 * - the windchain is rewound up to the continuation's context
187 * - the continuation is invoked via longjmp (or setcontext)
188 *
189 * This order is important so that unwind and rewind handlers are run
190 * with their correct stack.
191 */
192
193 static void scm_dynthrow (SCM, SCM);
194
195 /* Grow the stack by a fixed amount to provide space to copy in the
196 * continuation. Possibly this function has to be called several times
197 * recursively before enough space is available. Make sure the compiler does
198 * not optimize the growth array away by storing it's address into a global
199 * variable.
200 */
201
202 scm_t_bits scm_i_dummy;
203
204 static void
205 grow_stack (SCM cont, SCM val)
206 {
207 scm_t_bits growth[100];
208
209 scm_i_dummy = (scm_t_bits) growth;
210 scm_dynthrow (cont, val);
211 }
212
213
214 /* Copy the continuation stack into the current stack. Calling functions from
215 * within this function is safe, since only stack frames below this function's
216 * own frame are overwritten. Thus, memcpy can be used for best performance.
217 */
218
219 typedef struct {
220 scm_t_contregs *continuation;
221 SCM_STACKITEM *dst;
222 } copy_stack_data;
223
224 static void
225 copy_stack (void *data)
226 {
227 copy_stack_data *d = (copy_stack_data *)data;
228 memcpy (d->dst, d->continuation->stack,
229 sizeof (SCM_STACKITEM) * d->continuation->num_stack_items);
230 }
231
232 static void
233 copy_stack_and_call (scm_t_contregs *continuation, SCM val,
234 SCM_STACKITEM * dst)
235 {
236 long delta;
237 copy_stack_data data;
238
239 delta = scm_ilength (scm_dynwinds) - scm_ilength (continuation->dynenv);
240 data.continuation = continuation;
241 data.dst = dst;
242 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
243
244 scm_last_debug_frame = continuation->dframe;
245
246 continuation->throw_value = val;
247 #ifdef __ia64__
248 memcpy ((void *) __libc_ia64_register_backing_store_base,
249 continuation->backing_store,
250 continuation->backing_store_size);
251 setcontext (&continuation->ctx);
252 #else
253 longjmp (continuation->jmpbuf, 1);
254 #endif
255 }
256
257 /* Call grow_stack until the stack space is large enough, then, as the current
258 * stack frame might get overwritten, let copy_stack_and_call perform the
259 * actual copying and continuation calling.
260 */
261 static void
262 scm_dynthrow (SCM cont, SCM val)
263 {
264 scm_t_contregs *continuation = SCM_CONTREGS (cont);
265 SCM_STACKITEM * dst = SCM_BASE (scm_rootcont);
266 SCM_STACKITEM stack_top_element;
267
268 #if SCM_STACK_GROWS_UP
269 if (dst + continuation->num_stack_items >= &stack_top_element)
270 grow_stack (cont, val);
271 #else
272 dst -= continuation->num_stack_items;
273 if (dst <= &stack_top_element)
274 grow_stack (cont, val);
275 #endif /* def SCM_STACK_GROWS_UP */
276
277 SCM_FLUSH_REGISTER_WINDOWS;
278 copy_stack_and_call (continuation, val, dst);
279 }
280
281
282 static SCM
283 continuation_apply (SCM cont, SCM args)
284 #define FUNC_NAME "continuation_apply"
285 {
286 scm_t_contregs *continuation = SCM_CONTREGS (cont);
287 scm_t_contregs *rootcont = SCM_CONTREGS (scm_rootcont);
288
289 if (continuation->seq != rootcont->seq
290 /* this base comparison isn't needed */
291 || continuation->base != rootcont->base)
292 {
293 SCM_MISC_ERROR ("continuation from wrong top level: ~S",
294 scm_list_1 (cont));
295 }
296
297 scm_dynthrow (cont, scm_values (args));
298 return SCM_UNSPECIFIED; /* not reached */
299 }
300 #undef FUNC_NAME
301
302
303 void
304 scm_init_continuations ()
305 {
306 scm_tc16_continuation = scm_make_smob_type ("continuation", 0);
307 scm_set_smob_mark (scm_tc16_continuation, continuation_mark);
308 scm_set_smob_free (scm_tc16_continuation, continuation_free);
309 scm_set_smob_print (scm_tc16_continuation, continuation_print);
310 scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1);
311 #include "libguile/continuations.x"
312 }
313
314 /*
315 Local Variables:
316 c-file-style: "gnu"
317 End:
318 */