Commit | Line | Data |
---|---|---|
f2c9fcb0 | 1 | /* Copyright (C) 1995,1996,1998, 2000 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 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
a0599745 MD |
48 | #include "libguile/_scm.h" |
49 | #include "libguile/root.h" | |
50 | #include "libguile/stackchk.h" | |
5f144b10 GH |
51 | #include "libguile/smob.h" |
52 | #include "libguile/ports.h" | |
53 | #include "libguile/dynwind.h" | |
ce212434 | 54 | #include "libguile/values.h" |
5f144b10 | 55 | |
311df4f0 | 56 | #ifdef DEBUG_EXTENSIONS |
a0599745 | 57 | #include "libguile/debug.h" |
311df4f0 | 58 | #endif |
0f2d19dd | 59 | |
a0599745 | 60 | #include "libguile/continuations.h" |
01c8a3dd | 61 | |
0f2d19dd JB |
62 | \f |
63 | ||
64 | /* {Continuations} | |
65 | */ | |
66 | ||
5f144b10 | 67 | scm_bits_t scm_tc16_continuation; |
0f2d19dd | 68 | |
5f144b10 GH |
69 | static SCM continuation_mark (SCM obj) |
70 | { | |
71 | scm_contregs *continuation = SCM_CONTREGS (obj); | |
01c8a3dd | 72 | |
5f144b10 GH |
73 | scm_gc_mark (continuation->throw_value); |
74 | scm_mark_locations (continuation->stack, continuation->num_stack_items); | |
75 | return continuation->dynenv; | |
76 | } | |
01c8a3dd | 77 | |
5f144b10 GH |
78 | static scm_sizet continuation_free (SCM obj) |
79 | { | |
80 | scm_contregs *continuation = SCM_CONTREGS (obj); | |
81 | /* stack array size is 1 if num_stack_items is 0 (rootcont). */ | |
82 | scm_sizet extra_items = (continuation->num_stack_items > 0) | |
83 | ? (continuation->num_stack_items - 1) | |
84 | : 0; | |
85 | scm_sizet bytes_free = sizeof (scm_contregs) | |
86 | + extra_items * sizeof (SCM_STACKITEM); | |
87 | ||
88 | scm_must_free (continuation); | |
89 | return bytes_free; | |
90 | } | |
01c8a3dd | 91 | |
5f144b10 GH |
92 | static int continuation_print (SCM obj, SCM port, scm_print_state *state) |
93 | { | |
94 | scm_contregs *continuation = SCM_CONTREGS (obj); | |
95 | ||
96 | scm_puts ("#<continuation ", port); | |
97 | scm_intprint (continuation->num_stack_items, 10, port); | |
98 | scm_puts (" @ ", port); | |
99 | scm_intprint (SCM_CELL_WORD_1 (obj), 16, port); | |
100 | scm_putc ('>', port); | |
101 | return 1; | |
102 | } | |
1cc91f1b | 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 | { |
0f2d19dd | 111 | SCM cont; |
5f144b10 GH |
112 | scm_contregs *continuation; |
113 | scm_contregs *rootcont = SCM_CONTREGS (scm_rootcont); | |
114 | long stack_size; | |
01c8a3dd | 115 | SCM_STACKITEM * src; |
0f2d19dd | 116 | |
f83e2737 | 117 | SCM_ENTER_A_SECTION; |
0f2d19dd | 118 | SCM_FLUSH_REGISTER_WINDOWS; |
5f144b10 GH |
119 | stack_size = scm_stack_size (rootcont->base); |
120 | continuation = scm_must_malloc (sizeof (scm_contregs) | |
121 | + (stack_size - 1) * sizeof (SCM_STACKITEM), | |
122 | FUNC_NAME); | |
123 | continuation->num_stack_items = stack_size; | |
124 | continuation->dynenv = scm_dynwinds; | |
125 | continuation->throw_value = SCM_EOL; | |
126 | continuation->base = src = rootcont->base; | |
127 | continuation->seq = rootcont->seq; | |
0f2d19dd | 128 | #ifdef DEBUG_EXTENSIONS |
5f144b10 | 129 | continuation->dframe = scm_last_debug_frame; |
0f2d19dd | 130 | #endif |
5f144b10 GH |
131 | SCM_NEWSMOB (cont, scm_tc16_continuation, continuation); |
132 | SCM_EXIT_A_SECTION; | |
01c8a3dd | 133 | |
5f144b10 GH |
134 | #ifndef SCM_STACK_GROWS_UP |
135 | src -= stack_size; | |
136 | #endif | |
137 | memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size); | |
138 | ||
139 | if (setjmp (continuation->jmpbuf)) | |
140 | { | |
141 | *first = 0; | |
142 | return continuation->throw_value; | |
143 | } | |
144 | else | |
145 | { | |
146 | *first = 1; | |
147 | return cont; | |
148 | } | |
0f2d19dd | 149 | } |
5f144b10 | 150 | #undef FUNC_NAME |
0f2d19dd | 151 | |
5f144b10 | 152 | static void scm_dynthrow (SCM, SCM); |
01c8a3dd DH |
153 | |
154 | /* Grow the stack by a fixed amount to provide space to copy in the | |
155 | * continuation. Possibly this function has to be called several times | |
156 | * recursively before enough space is available. Make sure the compiler does | |
157 | * not optimize the growth array away by storing it's address into a global | |
158 | * variable. | |
159 | */ | |
160 | ||
161 | scm_bits_t scm_i_dummy; | |
1cc91f1b | 162 | |
0f2d19dd | 163 | static void |
01c8a3dd DH |
164 | grow_stack (SCM cont, SCM val) |
165 | { | |
166 | scm_bits_t growth[100]; | |
167 | ||
168 | scm_i_dummy = (scm_bits_t) growth; | |
169 | scm_dynthrow (cont, val); | |
0f2d19dd | 170 | } |
0f2d19dd | 171 | |
1cc91f1b | 172 | |
01c8a3dd DH |
173 | /* Copy the continuation stack into the current stack. Calling functions from |
174 | * within this function is safe, since only stack frames below this function's | |
175 | * own frame are overwritten. Thus, memcpy can be used for best performance. | |
176 | */ | |
177 | static void | |
5f144b10 GH |
178 | copy_stack_and_call (scm_contregs *continuation, SCM val, |
179 | SCM_STACKITEM * dst) | |
0f2d19dd | 180 | { |
5f144b10 GH |
181 | memcpy (dst, continuation->stack, |
182 | sizeof (SCM_STACKITEM) * continuation->num_stack_items); | |
01c8a3dd DH |
183 | |
184 | #ifdef DEBUG_EXTENSIONS | |
5f144b10 | 185 | scm_last_debug_frame = continuation->dframe; |
0f2d19dd | 186 | #endif |
01c8a3dd | 187 | |
5f144b10 GH |
188 | continuation->throw_value = val; |
189 | longjmp (continuation->jmpbuf, 1); | |
01c8a3dd DH |
190 | } |
191 | ||
192 | ||
193 | /* Call grow_stack until the stack space is large enough, then, as the current | |
194 | * stack frame might get overwritten, let copy_stack_and_call perform the | |
195 | * actual copying and continuation calling. | |
196 | */ | |
197 | static void | |
198 | scm_dynthrow (SCM cont, SCM val) | |
199 | { | |
5f144b10 | 200 | scm_contregs *continuation = SCM_CONTREGS (cont); |
01c8a3dd DH |
201 | SCM_STACKITEM * dst = SCM_BASE (scm_rootcont); |
202 | SCM_STACKITEM stack_top_element; | |
203 | ||
0f2d19dd | 204 | #ifdef SCM_STACK_GROWS_UP |
5f144b10 | 205 | if (SCM_PTR_GE (dst + continuation->num_stack_items, &stack_top_element)) |
01c8a3dd | 206 | grow_stack (cont, val); |
0f2d19dd | 207 | #else |
5f144b10 GH |
208 | dst -= continuation->num_stack_items; |
209 | if (SCM_PTR_LE (dst, &stack_top_element)) | |
01c8a3dd | 210 | grow_stack (cont, val); |
0f2d19dd | 211 | #endif /* def SCM_STACK_GROWS_UP */ |
01c8a3dd | 212 | |
5f144b10 GH |
213 | SCM_FLUSH_REGISTER_WINDOWS; |
214 | copy_stack_and_call (continuation, val, dst); | |
0f2d19dd JB |
215 | } |
216 | ||
5f144b10 GH |
217 | #define FUNC_NAME "continuation_apply" |
218 | static SCM continuation_apply (SCM cont, SCM args) | |
0f2d19dd | 219 | { |
5f144b10 GH |
220 | scm_contregs *continuation = SCM_CONTREGS (cont); |
221 | scm_contregs *rootcont = SCM_CONTREGS (scm_rootcont); | |
222 | ||
5f144b10 GH |
223 | if (continuation->seq != rootcont->seq |
224 | /* this base comparison isn't needed */ | |
225 | || continuation->base != rootcont->base) | |
226 | { | |
227 | scm_wta (cont, "continuation from wrong top level", FUNC_NAME); | |
228 | } | |
0f2d19dd | 229 | |
5f144b10 GH |
230 | scm_dowinds (continuation->dynenv, |
231 | scm_ilength (scm_dynwinds) - continuation->dynenv); | |
0f2d19dd | 232 | |
ce212434 | 233 | scm_dynthrow (cont, scm_values (args)); |
0f2d19dd JB |
234 | return SCM_UNSPECIFIED; /* not reached */ |
235 | } | |
5f144b10 | 236 | #undef FUNC_NAME |
0f2d19dd | 237 | |
0f2d19dd JB |
238 | void |
239 | scm_init_continuations () | |
0f2d19dd | 240 | { |
5f144b10 GH |
241 | scm_tc16_continuation = scm_make_smob_type ("continuation", 0); |
242 | scm_set_smob_mark (scm_tc16_continuation, continuation_mark); | |
243 | scm_set_smob_free (scm_tc16_continuation, continuation_free); | |
244 | scm_set_smob_print (scm_tc16_continuation, continuation_print); | |
245 | scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1); | |
246 | ||
8dc9439f | 247 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 248 | #include "libguile/continuations.x" |
8dc9439f | 249 | #endif |
0f2d19dd JB |
250 | } |
251 | ||
89e00824 ML |
252 | /* |
253 | Local Variables: | |
254 | c-file-style: "gnu" | |
255 | End: | |
256 | */ |