Commit | Line | Data |
---|---|---|
e81d98ec | 1 | /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
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. | |
0f2d19dd | 7 | * |
73be1d9e MV |
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. | |
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 | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd JB |
19 | \f |
20 | ||
bbd43f03 RB |
21 | #include "libguile/_scm.h" |
22 | ||
13070bd3 DH |
23 | #include <string.h> |
24 | ||
d0f6ceb8 | 25 | #include "libguile/debug.h" |
a0599745 MD |
26 | #include "libguile/root.h" |
27 | #include "libguile/stackchk.h" | |
5f144b10 GH |
28 | #include "libguile/smob.h" |
29 | #include "libguile/ports.h" | |
30 | #include "libguile/dynwind.h" | |
ce212434 | 31 | #include "libguile/values.h" |
5f144b10 | 32 | |
db4b4ca6 | 33 | #include "libguile/validate.h" |
a0599745 | 34 | #include "libguile/continuations.h" |
01c8a3dd | 35 | |
0f2d19dd JB |
36 | \f |
37 | ||
38 | /* {Continuations} | |
39 | */ | |
40 | ||
92c2555f | 41 | scm_t_bits scm_tc16_continuation; |
0f2d19dd | 42 | |
e841c3e0 KN |
43 | static SCM |
44 | continuation_mark (SCM obj) | |
5f144b10 | 45 | { |
92c2555f | 46 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
01c8a3dd | 47 | |
5f144b10 GH |
48 | scm_gc_mark (continuation->throw_value); |
49 | scm_mark_locations (continuation->stack, continuation->num_stack_items); | |
193297d8 RB |
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__ */ | |
5f144b10 GH |
56 | return continuation->dynenv; |
57 | } | |
01c8a3dd | 58 | |
1be6b49c | 59 | static size_t |
e841c3e0 | 60 | continuation_free (SCM obj) |
5f144b10 | 61 | { |
92c2555f | 62 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
5f144b10 | 63 | /* stack array size is 1 if num_stack_items is 0 (rootcont). */ |
1be6b49c | 64 | size_t extra_items = (continuation->num_stack_items > 0) |
5f144b10 GH |
65 | ? (continuation->num_stack_items - 1) |
66 | : 0; | |
92c2555f | 67 | size_t bytes_free = sizeof (scm_t_contregs) |
5f144b10 | 68 | + extra_items * sizeof (SCM_STACKITEM); |
193297d8 RB |
69 | |
70 | #ifdef __ia64__ | |
4c9419ac MV |
71 | scm_gc_free (continuation->backing_store, continuation->backing_store_size, |
72 | "continuation backing store"); | |
193297d8 | 73 | #endif /* __ia64__ */ |
4c9419ac MV |
74 | scm_gc_free (continuation, bytes_free, "continuation"); |
75 | return 0; | |
5f144b10 | 76 | } |
01c8a3dd | 77 | |
e841c3e0 | 78 | static int |
e81d98ec | 79 | continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED) |
5f144b10 | 80 | { |
92c2555f | 81 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
5f144b10 GH |
82 | |
83 | scm_puts ("#<continuation ", port); | |
84 | scm_intprint (continuation->num_stack_items, 10, port); | |
85 | scm_puts (" @ ", port); | |
86 | scm_intprint (SCM_CELL_WORD_1 (obj), 16, port); | |
87 | scm_putc ('>', port); | |
88 | return 1; | |
89 | } | |
1cc91f1b | 90 | |
193297d8 | 91 | #ifdef __ia64__ |
87855fa2 MV |
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(). */ | |
193297d8 RB |
95 | struct rv |
96 | { | |
97 | long retval; | |
98 | long first_return; | |
99 | }; | |
100 | extern struct rv getcontext (ucontext_t *); | |
101 | extern int setcontext (ucontext_t *); | |
102 | #endif /* __ia64__ */ | |
103 | ||
5f144b10 GH |
104 | /* this may return more than once: the first time with the escape |
105 | procedure, then subsequently with the value to be passed to the | |
106 | continuation. */ | |
107 | #define FUNC_NAME "scm_make_continuation" | |
0f2d19dd | 108 | SCM |
5f144b10 | 109 | scm_make_continuation (int *first) |
0f2d19dd | 110 | { |
fcba9b58 | 111 | volatile SCM cont; |
92c2555f MV |
112 | scm_t_contregs *continuation; |
113 | scm_t_contregs *rootcont = SCM_CONTREGS (scm_rootcont); | |
c014a02e | 114 | long stack_size; |
01c8a3dd | 115 | SCM_STACKITEM * src; |
193297d8 RB |
116 | #ifdef __ia64__ |
117 | struct rv rv; | |
87855fa2 | 118 | #endif /* __ia64__ */ |
0f2d19dd | 119 | |
f83e2737 | 120 | SCM_ENTER_A_SECTION; |
0f2d19dd | 121 | SCM_FLUSH_REGISTER_WINDOWS; |
5f144b10 | 122 | stack_size = scm_stack_size (rootcont->base); |
4c9419ac MV |
123 | continuation = scm_gc_malloc (sizeof (scm_t_contregs) |
124 | + (stack_size - 1) * sizeof (SCM_STACKITEM), | |
125 | "continuation"); | |
5f144b10 GH |
126 | continuation->num_stack_items = stack_size; |
127 | continuation->dynenv = scm_dynwinds; | |
128 | continuation->throw_value = SCM_EOL; | |
129 | continuation->base = src = rootcont->base; | |
130 | continuation->seq = rootcont->seq; | |
5f144b10 | 131 | continuation->dframe = scm_last_debug_frame; |
5f144b10 GH |
132 | SCM_NEWSMOB (cont, scm_tc16_continuation, continuation); |
133 | SCM_EXIT_A_SECTION; | |
01c8a3dd | 134 | |
4ccb2cd2 | 135 | #if ! SCM_STACK_GROWS_UP |
5f144b10 GH |
136 | src -= stack_size; |
137 | #endif | |
138 | memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size); | |
139 | ||
193297d8 RB |
140 | #ifdef __ia64__ |
141 | rv = getcontext (&continuation->ctx); | |
142 | if (rv.first_return) | |
143 | { | |
144 | continuation->backing_store_size = | |
145 | continuation->ctx.uc_mcontext.sc_ar_bsp - | |
87855fa2 | 146 | (unsigned long) __libc_ia64_register_backing_store_base; |
193297d8 RB |
147 | continuation->backing_store = NULL; |
148 | continuation->backing_store = | |
4c9419ac MV |
149 | scm_gc_malloc (continuation->backing_store_size, |
150 | "continuation backing store"); | |
193297d8 RB |
151 | memcpy (continuation->backing_store, |
152 | (void *) __libc_ia64_register_backing_store_base, | |
153 | continuation->backing_store_size); | |
154 | *first = 1; | |
155 | return cont; | |
156 | } | |
157 | else | |
158 | { | |
3c468478 | 159 | SCM ret = continuation->throw_value; |
193297d8 | 160 | *first = 0; |
3c468478 MV |
161 | continuation->throw_value = SCM_BOOL_F; |
162 | return ret; | |
193297d8 RB |
163 | } |
164 | #else /* !__ia64__ */ | |
5f144b10 GH |
165 | if (setjmp (continuation->jmpbuf)) |
166 | { | |
3c468478 | 167 | SCM ret = continuation->throw_value; |
5f144b10 | 168 | *first = 0; |
3c468478 MV |
169 | continuation->throw_value = SCM_BOOL_F; |
170 | return ret; | |
5f144b10 GH |
171 | } |
172 | else | |
173 | { | |
174 | *first = 1; | |
175 | return cont; | |
176 | } | |
193297d8 | 177 | #endif /* !__ia64__ */ |
0f2d19dd | 178 | } |
5f144b10 | 179 | #undef FUNC_NAME |
0f2d19dd | 180 | |
5f144b10 | 181 | static void scm_dynthrow (SCM, SCM); |
01c8a3dd DH |
182 | |
183 | /* Grow the stack by a fixed amount to provide space to copy in the | |
184 | * continuation. Possibly this function has to be called several times | |
185 | * recursively before enough space is available. Make sure the compiler does | |
186 | * not optimize the growth array away by storing it's address into a global | |
187 | * variable. | |
188 | */ | |
189 | ||
92c2555f | 190 | scm_t_bits scm_i_dummy; |
1cc91f1b | 191 | |
0f2d19dd | 192 | static void |
01c8a3dd DH |
193 | grow_stack (SCM cont, SCM val) |
194 | { | |
92c2555f | 195 | scm_t_bits growth[100]; |
01c8a3dd | 196 | |
92c2555f | 197 | scm_i_dummy = (scm_t_bits) growth; |
01c8a3dd | 198 | scm_dynthrow (cont, val); |
0f2d19dd | 199 | } |
0f2d19dd | 200 | |
1cc91f1b | 201 | |
01c8a3dd DH |
202 | /* Copy the continuation stack into the current stack. Calling functions from |
203 | * within this function is safe, since only stack frames below this function's | |
204 | * own frame are overwritten. Thus, memcpy can be used for best performance. | |
205 | */ | |
206 | static void | |
92c2555f | 207 | copy_stack_and_call (scm_t_contregs *continuation, SCM val, |
5f144b10 | 208 | SCM_STACKITEM * dst) |
0f2d19dd | 209 | { |
5f144b10 GH |
210 | memcpy (dst, continuation->stack, |
211 | sizeof (SCM_STACKITEM) * continuation->num_stack_items); | |
01c8a3dd | 212 | |
5f144b10 | 213 | scm_last_debug_frame = continuation->dframe; |
01c8a3dd | 214 | |
5f144b10 | 215 | continuation->throw_value = val; |
193297d8 RB |
216 | #ifdef __ia64__ |
217 | memcpy ((void *) __libc_ia64_register_backing_store_base, | |
218 | continuation->backing_store, | |
219 | continuation->backing_store_size); | |
220 | setcontext (&continuation->ctx); | |
221 | #else | |
5f144b10 | 222 | longjmp (continuation->jmpbuf, 1); |
193297d8 | 223 | #endif |
01c8a3dd DH |
224 | } |
225 | ||
226 | ||
227 | /* Call grow_stack until the stack space is large enough, then, as the current | |
228 | * stack frame might get overwritten, let copy_stack_and_call perform the | |
229 | * actual copying and continuation calling. | |
230 | */ | |
231 | static void | |
232 | scm_dynthrow (SCM cont, SCM val) | |
233 | { | |
92c2555f | 234 | scm_t_contregs *continuation = SCM_CONTREGS (cont); |
01c8a3dd DH |
235 | SCM_STACKITEM * dst = SCM_BASE (scm_rootcont); |
236 | SCM_STACKITEM stack_top_element; | |
237 | ||
4ccb2cd2 | 238 | #if SCM_STACK_GROWS_UP |
5f144b10 | 239 | if (SCM_PTR_GE (dst + continuation->num_stack_items, &stack_top_element)) |
01c8a3dd | 240 | grow_stack (cont, val); |
0f2d19dd | 241 | #else |
5f144b10 | 242 | dst -= continuation->num_stack_items; |
c8a1bdc4 | 243 | if (dst <= &stack_top_element) |
01c8a3dd | 244 | grow_stack (cont, val); |
0f2d19dd | 245 | #endif /* def SCM_STACK_GROWS_UP */ |
01c8a3dd | 246 | |
5f144b10 GH |
247 | SCM_FLUSH_REGISTER_WINDOWS; |
248 | copy_stack_and_call (continuation, val, dst); | |
0f2d19dd JB |
249 | } |
250 | ||
db4b4ca6 DH |
251 | |
252 | static SCM | |
253 | continuation_apply (SCM cont, SCM args) | |
5f144b10 | 254 | #define FUNC_NAME "continuation_apply" |
0f2d19dd | 255 | { |
92c2555f MV |
256 | scm_t_contregs *continuation = SCM_CONTREGS (cont); |
257 | scm_t_contregs *rootcont = SCM_CONTREGS (scm_rootcont); | |
5f144b10 | 258 | |
5f144b10 GH |
259 | if (continuation->seq != rootcont->seq |
260 | /* this base comparison isn't needed */ | |
261 | || continuation->base != rootcont->base) | |
262 | { | |
db4b4ca6 | 263 | SCM_MISC_ERROR ("continuation from wrong top level: ~S", |
1afff620 | 264 | scm_list_1 (cont)); |
5f144b10 | 265 | } |
0f2d19dd | 266 | |
5f144b10 | 267 | scm_dowinds (continuation->dynenv, |
5bd44fc9 GH |
268 | scm_ilength (scm_dynwinds) |
269 | - scm_ilength (continuation->dynenv)); | |
0f2d19dd | 270 | |
ce212434 | 271 | scm_dynthrow (cont, scm_values (args)); |
0f2d19dd JB |
272 | return SCM_UNSPECIFIED; /* not reached */ |
273 | } | |
5f144b10 | 274 | #undef FUNC_NAME |
0f2d19dd | 275 | |
db4b4ca6 | 276 | |
0f2d19dd JB |
277 | void |
278 | scm_init_continuations () | |
0f2d19dd | 279 | { |
5f144b10 GH |
280 | scm_tc16_continuation = scm_make_smob_type ("continuation", 0); |
281 | scm_set_smob_mark (scm_tc16_continuation, continuation_mark); | |
282 | scm_set_smob_free (scm_tc16_continuation, continuation_free); | |
283 | scm_set_smob_print (scm_tc16_continuation, continuation_print); | |
284 | scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1); | |
a0599745 | 285 | #include "libguile/continuations.x" |
0f2d19dd JB |
286 | } |
287 | ||
89e00824 ML |
288 | /* |
289 | Local Variables: | |
290 | c-file-style: "gnu" | |
291 | End: | |
292 | */ |