Commit | Line | Data |
---|---|---|
e81d98ec | 1 | /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
0f2d19dd JB |
43 | \f |
44 | ||
13070bd3 DH |
45 | #include <string.h> |
46 | ||
a0599745 MD |
47 | #include "libguile/_scm.h" |
48 | #include "libguile/root.h" | |
49 | #include "libguile/stackchk.h" | |
5f144b10 GH |
50 | #include "libguile/smob.h" |
51 | #include "libguile/ports.h" | |
52 | #include "libguile/dynwind.h" | |
ce212434 | 53 | #include "libguile/values.h" |
5f144b10 | 54 | |
311df4f0 | 55 | #ifdef DEBUG_EXTENSIONS |
a0599745 | 56 | #include "libguile/debug.h" |
311df4f0 | 57 | #endif |
0f2d19dd | 58 | |
db4b4ca6 | 59 | #include "libguile/validate.h" |
a0599745 | 60 | #include "libguile/continuations.h" |
01c8a3dd | 61 | |
0f2d19dd JB |
62 | \f |
63 | ||
64 | /* {Continuations} | |
65 | */ | |
66 | ||
92c2555f | 67 | scm_t_bits scm_tc16_continuation; |
0f2d19dd | 68 | |
e841c3e0 KN |
69 | static SCM |
70 | continuation_mark (SCM obj) | |
5f144b10 | 71 | { |
92c2555f | 72 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
01c8a3dd | 73 | |
5f144b10 GH |
74 | scm_gc_mark (continuation->throw_value); |
75 | scm_mark_locations (continuation->stack, continuation->num_stack_items); | |
193297d8 RB |
76 | #ifdef __ia64__ |
77 | if (continuation->backing_store) | |
78 | scm_mark_locations (continuation->backing_store, | |
79 | continuation->backing_store_size / | |
80 | sizeof (SCM_STACKITEM)); | |
81 | #endif /* __ia64__ */ | |
5f144b10 GH |
82 | return continuation->dynenv; |
83 | } | |
01c8a3dd | 84 | |
1be6b49c | 85 | static size_t |
e841c3e0 | 86 | continuation_free (SCM obj) |
5f144b10 | 87 | { |
92c2555f | 88 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
5f144b10 | 89 | /* stack array size is 1 if num_stack_items is 0 (rootcont). */ |
1be6b49c | 90 | size_t extra_items = (continuation->num_stack_items > 0) |
5f144b10 GH |
91 | ? (continuation->num_stack_items - 1) |
92 | : 0; | |
92c2555f | 93 | size_t bytes_free = sizeof (scm_t_contregs) |
5f144b10 | 94 | + extra_items * sizeof (SCM_STACKITEM); |
193297d8 RB |
95 | |
96 | #ifdef __ia64__ | |
97 | bytes_free += continuation->backing_store_size; | |
98 | scm_must_free (continuation->backing_store); | |
99 | #endif /* __ia64__ */ | |
5f144b10 GH |
100 | scm_must_free (continuation); |
101 | return bytes_free; | |
102 | } | |
01c8a3dd | 103 | |
e841c3e0 | 104 | static int |
e81d98ec | 105 | continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED) |
5f144b10 | 106 | { |
92c2555f | 107 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
5f144b10 GH |
108 | |
109 | scm_puts ("#<continuation ", port); | |
110 | scm_intprint (continuation->num_stack_items, 10, port); | |
111 | scm_puts (" @ ", port); | |
112 | scm_intprint (SCM_CELL_WORD_1 (obj), 16, port); | |
113 | scm_putc ('>', port); | |
114 | return 1; | |
115 | } | |
1cc91f1b | 116 | |
193297d8 | 117 | #ifdef __ia64__ |
87855fa2 MV |
118 | /* Extern declaration of getcontext()/setcontext() in order to redefine |
119 | getcontext() since on ia64-linux the second return value indicates whether | |
120 | it returned from getcontext() itself or by running setcontext(). */ | |
193297d8 RB |
121 | struct rv |
122 | { | |
123 | long retval; | |
124 | long first_return; | |
125 | }; | |
126 | extern struct rv getcontext (ucontext_t *); | |
127 | extern int setcontext (ucontext_t *); | |
128 | #endif /* __ia64__ */ | |
129 | ||
5f144b10 GH |
130 | /* this may return more than once: the first time with the escape |
131 | procedure, then subsequently with the value to be passed to the | |
132 | continuation. */ | |
133 | #define FUNC_NAME "scm_make_continuation" | |
0f2d19dd | 134 | SCM |
5f144b10 | 135 | scm_make_continuation (int *first) |
0f2d19dd | 136 | { |
fcba9b58 | 137 | volatile SCM cont; |
92c2555f MV |
138 | scm_t_contregs *continuation; |
139 | scm_t_contregs *rootcont = SCM_CONTREGS (scm_rootcont); | |
c014a02e | 140 | long stack_size; |
01c8a3dd | 141 | SCM_STACKITEM * src; |
193297d8 RB |
142 | #ifdef __ia64__ |
143 | struct rv rv; | |
87855fa2 | 144 | #endif /* __ia64__ */ |
0f2d19dd | 145 | |
f83e2737 | 146 | SCM_ENTER_A_SECTION; |
0f2d19dd | 147 | SCM_FLUSH_REGISTER_WINDOWS; |
5f144b10 | 148 | stack_size = scm_stack_size (rootcont->base); |
92c2555f | 149 | continuation = scm_must_malloc (sizeof (scm_t_contregs) |
5f144b10 GH |
150 | + (stack_size - 1) * sizeof (SCM_STACKITEM), |
151 | FUNC_NAME); | |
152 | continuation->num_stack_items = stack_size; | |
153 | continuation->dynenv = scm_dynwinds; | |
154 | continuation->throw_value = SCM_EOL; | |
155 | continuation->base = src = rootcont->base; | |
156 | continuation->seq = rootcont->seq; | |
0f2d19dd | 157 | #ifdef DEBUG_EXTENSIONS |
5f144b10 | 158 | continuation->dframe = scm_last_debug_frame; |
0f2d19dd | 159 | #endif |
5f144b10 GH |
160 | SCM_NEWSMOB (cont, scm_tc16_continuation, continuation); |
161 | SCM_EXIT_A_SECTION; | |
01c8a3dd | 162 | |
5f144b10 GH |
163 | #ifndef SCM_STACK_GROWS_UP |
164 | src -= stack_size; | |
165 | #endif | |
166 | memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size); | |
167 | ||
193297d8 RB |
168 | #ifdef __ia64__ |
169 | rv = getcontext (&continuation->ctx); | |
170 | if (rv.first_return) | |
171 | { | |
172 | continuation->backing_store_size = | |
173 | continuation->ctx.uc_mcontext.sc_ar_bsp - | |
87855fa2 | 174 | (unsigned long) __libc_ia64_register_backing_store_base; |
193297d8 RB |
175 | continuation->backing_store = NULL; |
176 | continuation->backing_store = | |
177 | scm_must_malloc (continuation->backing_store_size, FUNC_NAME); | |
178 | memcpy (continuation->backing_store, | |
179 | (void *) __libc_ia64_register_backing_store_base, | |
180 | continuation->backing_store_size); | |
181 | *first = 1; | |
182 | return cont; | |
183 | } | |
184 | else | |
185 | { | |
186 | *first = 0; | |
187 | return continuation->throw_value; | |
188 | } | |
189 | #else /* !__ia64__ */ | |
5f144b10 GH |
190 | if (setjmp (continuation->jmpbuf)) |
191 | { | |
192 | *first = 0; | |
193 | return continuation->throw_value; | |
194 | } | |
195 | else | |
196 | { | |
197 | *first = 1; | |
198 | return cont; | |
199 | } | |
193297d8 | 200 | #endif /* !__ia64__ */ |
0f2d19dd | 201 | } |
5f144b10 | 202 | #undef FUNC_NAME |
0f2d19dd | 203 | |
5f144b10 | 204 | static void scm_dynthrow (SCM, SCM); |
01c8a3dd DH |
205 | |
206 | /* Grow the stack by a fixed amount to provide space to copy in the | |
207 | * continuation. Possibly this function has to be called several times | |
208 | * recursively before enough space is available. Make sure the compiler does | |
209 | * not optimize the growth array away by storing it's address into a global | |
210 | * variable. | |
211 | */ | |
212 | ||
92c2555f | 213 | scm_t_bits scm_i_dummy; |
1cc91f1b | 214 | |
0f2d19dd | 215 | static void |
01c8a3dd DH |
216 | grow_stack (SCM cont, SCM val) |
217 | { | |
92c2555f | 218 | scm_t_bits growth[100]; |
01c8a3dd | 219 | |
92c2555f | 220 | scm_i_dummy = (scm_t_bits) growth; |
01c8a3dd | 221 | scm_dynthrow (cont, val); |
0f2d19dd | 222 | } |
0f2d19dd | 223 | |
1cc91f1b | 224 | |
01c8a3dd DH |
225 | /* Copy the continuation stack into the current stack. Calling functions from |
226 | * within this function is safe, since only stack frames below this function's | |
227 | * own frame are overwritten. Thus, memcpy can be used for best performance. | |
228 | */ | |
229 | static void | |
92c2555f | 230 | copy_stack_and_call (scm_t_contregs *continuation, SCM val, |
5f144b10 | 231 | SCM_STACKITEM * dst) |
0f2d19dd | 232 | { |
5f144b10 GH |
233 | memcpy (dst, continuation->stack, |
234 | sizeof (SCM_STACKITEM) * continuation->num_stack_items); | |
01c8a3dd DH |
235 | |
236 | #ifdef DEBUG_EXTENSIONS | |
5f144b10 | 237 | scm_last_debug_frame = continuation->dframe; |
0f2d19dd | 238 | #endif |
01c8a3dd | 239 | |
5f144b10 | 240 | continuation->throw_value = val; |
193297d8 RB |
241 | #ifdef __ia64__ |
242 | memcpy ((void *) __libc_ia64_register_backing_store_base, | |
243 | continuation->backing_store, | |
244 | continuation->backing_store_size); | |
245 | setcontext (&continuation->ctx); | |
246 | #else | |
5f144b10 | 247 | longjmp (continuation->jmpbuf, 1); |
193297d8 | 248 | #endif |
01c8a3dd DH |
249 | } |
250 | ||
251 | ||
252 | /* Call grow_stack until the stack space is large enough, then, as the current | |
253 | * stack frame might get overwritten, let copy_stack_and_call perform the | |
254 | * actual copying and continuation calling. | |
255 | */ | |
256 | static void | |
257 | scm_dynthrow (SCM cont, SCM val) | |
258 | { | |
92c2555f | 259 | scm_t_contregs *continuation = SCM_CONTREGS (cont); |
01c8a3dd DH |
260 | SCM_STACKITEM * dst = SCM_BASE (scm_rootcont); |
261 | SCM_STACKITEM stack_top_element; | |
262 | ||
0f2d19dd | 263 | #ifdef SCM_STACK_GROWS_UP |
5f144b10 | 264 | if (SCM_PTR_GE (dst + continuation->num_stack_items, &stack_top_element)) |
01c8a3dd | 265 | grow_stack (cont, val); |
0f2d19dd | 266 | #else |
5f144b10 GH |
267 | dst -= continuation->num_stack_items; |
268 | if (SCM_PTR_LE (dst, &stack_top_element)) | |
01c8a3dd | 269 | grow_stack (cont, val); |
0f2d19dd | 270 | #endif /* def SCM_STACK_GROWS_UP */ |
01c8a3dd | 271 | |
5f144b10 GH |
272 | SCM_FLUSH_REGISTER_WINDOWS; |
273 | copy_stack_and_call (continuation, val, dst); | |
0f2d19dd JB |
274 | } |
275 | ||
db4b4ca6 DH |
276 | |
277 | static SCM | |
278 | continuation_apply (SCM cont, SCM args) | |
5f144b10 | 279 | #define FUNC_NAME "continuation_apply" |
0f2d19dd | 280 | { |
92c2555f MV |
281 | scm_t_contregs *continuation = SCM_CONTREGS (cont); |
282 | scm_t_contregs *rootcont = SCM_CONTREGS (scm_rootcont); | |
5f144b10 | 283 | |
5f144b10 GH |
284 | if (continuation->seq != rootcont->seq |
285 | /* this base comparison isn't needed */ | |
286 | || continuation->base != rootcont->base) | |
287 | { | |
db4b4ca6 | 288 | SCM_MISC_ERROR ("continuation from wrong top level: ~S", |
1afff620 | 289 | scm_list_1 (cont)); |
5f144b10 | 290 | } |
0f2d19dd | 291 | |
5f144b10 | 292 | scm_dowinds (continuation->dynenv, |
5bd44fc9 GH |
293 | scm_ilength (scm_dynwinds) |
294 | - scm_ilength (continuation->dynenv)); | |
0f2d19dd | 295 | |
ce212434 | 296 | scm_dynthrow (cont, scm_values (args)); |
0f2d19dd JB |
297 | return SCM_UNSPECIFIED; /* not reached */ |
298 | } | |
5f144b10 | 299 | #undef FUNC_NAME |
0f2d19dd | 300 | |
db4b4ca6 | 301 | |
0f2d19dd JB |
302 | void |
303 | scm_init_continuations () | |
0f2d19dd | 304 | { |
5f144b10 GH |
305 | scm_tc16_continuation = scm_make_smob_type ("continuation", 0); |
306 | scm_set_smob_mark (scm_tc16_continuation, continuation_mark); | |
307 | scm_set_smob_free (scm_tc16_continuation, continuation_free); | |
308 | scm_set_smob_print (scm_tc16_continuation, continuation_print); | |
309 | scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1); | |
8dc9439f | 310 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 311 | #include "libguile/continuations.x" |
8dc9439f | 312 | #endif |
0f2d19dd JB |
313 | } |
314 | ||
89e00824 ML |
315 | /* |
316 | Local Variables: | |
317 | c-file-style: "gnu" | |
318 | End: | |
319 | */ |