Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | /* Copyright (C) 1995,1996 Free Software Foundation, Inc. |
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 | |
15 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. | |
40 | */ | |
41 | \f | |
42 | ||
43 | #include <stdio.h> | |
44 | #include "_scm.h" | |
311df4f0 JB |
45 | #include "stackchk.h" |
46 | #ifdef DEBUG_EXTENSIONS | |
47 | #include "debug.h" | |
48 | #endif | |
49 | #include "dynwind.h" | |
0f2d19dd | 50 | |
311df4f0 | 51 | #include "continuations.h" |
0f2d19dd JB |
52 | \f |
53 | ||
54 | /* {Continuations} | |
55 | */ | |
56 | ||
57 | static char s_cont[] = "continuation"; | |
58 | ||
59 | #ifdef __STDC__ | |
60 | SCM | |
61 | scm_make_cont (SCM * answer) | |
62 | #else | |
63 | SCM | |
64 | scm_make_cont (answer) | |
65 | SCM * answer; | |
66 | #endif | |
67 | { | |
68 | long j; | |
69 | SCM cont; | |
70 | ||
71 | #ifdef CHEAP_CONTINUATIONS | |
72 | SCM_NEWCELL (cont); | |
73 | *answer = cont; | |
74 | SCM_DEFER_INTS; | |
75 | SCM_SETJMPBUF (cont, scm_must_malloc ((long) sizeof (regs), s_cont)); | |
76 | SCM_CAR (cont) = scm_tc7_contin; | |
f08df670 | 77 | SCM_DYNENV (cont) = scm_dynwinds; |
0f2d19dd JB |
78 | SCM_THROW_VALUE = SCM_EOL; |
79 | SCM_BASE (cont) = SCM_BASE (rootcont); | |
80 | SCM_SEQ (cont) = SCM_SEQ (rootcont); | |
81 | SCM_ALLOW_INTS; | |
82 | #else | |
83 | register SCM_STACKITEM *src, *dst; | |
84 | ||
f08df670 | 85 | #if 0 |
0f2d19dd JB |
86 | { |
87 | SCM winds; | |
88 | ||
89 | for (winds = scm_dynwinds; winds != SCM_EOL; winds = SCM_CDR (winds)) | |
90 | { | |
0f2d19dd JB |
91 | if (SCM_INUMP (SCM_CAR (winds))) |
92 | { | |
93 | scm_relocate_chunk_to_heap (SCM_CAR (winds)); | |
94 | } | |
0f2d19dd JB |
95 | } |
96 | } | |
f08df670 | 97 | #endif |
0f2d19dd JB |
98 | |
99 | SCM_NEWCELL (cont); | |
100 | *answer = cont; | |
101 | SCM_DEFER_INTS; | |
102 | SCM_FLUSH_REGISTER_WINDOWS; | |
103 | j = scm_stack_size (SCM_BASE (scm_rootcont)); | |
104 | SCM_SETJMPBUF (cont, | |
105 | scm_must_malloc ((long) (sizeof (regs) + j * sizeof (SCM_STACKITEM)), | |
106 | s_cont)); | |
107 | SCM_SETLENGTH (cont, j, scm_tc7_contin); | |
108 | SCM_DYNENV (cont) = scm_dynwinds; | |
109 | SCM_THROW_VALUE (cont) = SCM_EOL; | |
110 | src = SCM_BASE (cont) = SCM_BASE (scm_rootcont); | |
111 | SCM_SEQ (cont) = SCM_SEQ (scm_rootcont); | |
112 | SCM_ALLOW_INTS; | |
113 | #ifndef SCM_STACK_GROWS_UP | |
114 | src -= SCM_LENGTH (cont); | |
115 | #endif /* ndef SCM_STACK_GROWS_UP */ | |
116 | dst = (SCM_STACKITEM *) (SCM_CHARS (cont) + sizeof (regs)); | |
117 | for (j = SCM_LENGTH (cont); 0 <= --j;) | |
118 | *dst++ = *src++; | |
119 | #endif /* def CHEAP_CONTINUATIONS */ | |
120 | #ifdef DEBUG_EXTENSIONS | |
121 | SCM_DFRAME (cont) = last_debug_info_frame; | |
122 | #endif | |
123 | return cont; | |
124 | } | |
125 | ||
0f2d19dd JB |
126 | /* Grow the stack so that there is room */ |
127 | /* to copy in the continuation. Then */ | |
128 | #ifndef CHEAP_CONTINUATIONS | |
129 | #ifdef __STDC__ | |
130 | static void | |
131 | grow_throw (SCM *a) | |
132 | #else | |
133 | static void | |
134 | grow_throw (a) | |
135 | SCM *a; | |
136 | #endif | |
137 | { /* retry the throw. */ | |
138 | SCM growth[100]; | |
139 | growth[0] = a[0]; | |
140 | growth[1] = a[1]; | |
141 | growth[2] = a[2] + 1; | |
142 | growth[3] = (SCM) a; | |
143 | scm_dynthrow (growth); | |
144 | } | |
145 | #endif /* ndef CHEAP_CONTINUATIONS */ | |
146 | ||
147 | #ifdef __STDC__ | |
148 | void | |
149 | scm_dynthrow (SCM *a) | |
150 | #else | |
151 | void | |
152 | scm_dynthrow (a) | |
153 | SCM *a; | |
154 | #endif | |
155 | { | |
156 | SCM cont = a[0], val = a[1]; | |
157 | #ifndef CHEAP_CONTINUATIONS | |
158 | register long j; | |
159 | register SCM_STACKITEM *src, *dst = SCM_BASE (scm_rootcont); | |
160 | #ifdef SCM_STACK_GROWS_UP | |
161 | if (a[2] && (a - ((SCM *) a[3]) < 100)) | |
162 | #else | |
163 | if (a[2] && (((SCM *) a[3]) - a < 100)) | |
164 | #endif | |
165 | fputs ("grow_throw: check if SCM growth[100]; being optimized out\n", | |
166 | stderr); | |
167 | /* if (a[2]) fprintf(stderr, " ct = %ld, dist = %ld\n", | |
168 | a[2], (((SCM *)a[3]) - a)); */ | |
169 | #ifdef SCM_STACK_GROWS_UP | |
170 | if (SCM_PTR_GE (dst + SCM_LENGTH (cont), (SCM_STACKITEM *) & a)) | |
171 | grow_throw (a); | |
172 | #else | |
173 | dst -= SCM_LENGTH (cont); | |
174 | if (SCM_PTR_LE (dst, (SCM_STACKITEM *) & a)) | |
175 | grow_throw (a); | |
176 | #endif /* def SCM_STACK_GROWS_UP */ | |
177 | SCM_FLUSH_REGISTER_WINDOWS; | |
178 | src = (SCM_STACKITEM *) (SCM_CHARS (cont) + sizeof (regs)); | |
179 | for (j = SCM_LENGTH (cont); 0 <= --j;) | |
180 | *dst++ = *src++; | |
181 | #ifdef sparc /* clear out stack up to this stackframe */ | |
182 | /* maybe this would help, maybe not */ | |
183 | /* bzero((void *)&a, sizeof(SCM_STACKITEM) * (((SCM_STACKITEM *)&a) - | |
184 | (dst - SCM_LENGTH(cont)))) */ | |
185 | #endif | |
186 | #endif /* ndef CHEAP_CONTINUATIONS */ | |
187 | #ifdef DEBUG_EXTENSIONS | |
188 | last_debug_info_frame = SCM_DFRAME (cont); | |
189 | #endif | |
190 | SCM_THROW_VALUE(cont) = val; | |
191 | longjmp (SCM_JMPBUF (cont), 1); | |
192 | } | |
193 | ||
194 | #ifdef __STDC__ | |
195 | SCM | |
196 | scm_call_continuation (SCM cont, SCM val) | |
197 | #else | |
198 | SCM | |
199 | scm_call_continuation (cont, val) | |
200 | SCM cont; | |
201 | SCM val; | |
202 | #endif | |
203 | { | |
204 | SCM a[3]; | |
205 | a[0] = cont; | |
206 | a[1] = val; | |
207 | a[2] = 0; | |
208 | if ( (SCM_SEQ (cont) != SCM_SEQ (scm_rootcont)) | |
209 | || (SCM_BASE (cont) != SCM_BASE (scm_rootcont))) /* base compare not needed */ | |
210 | scm_wta (cont, "continuation from wrong top level", s_cont); | |
211 | ||
212 | scm_dowinds (SCM_DYNENV (cont), | |
213 | scm_ilength (scm_dynwinds) - scm_ilength (SCM_DYNENV (cont))); | |
214 | ||
215 | scm_dynthrow (a); | |
216 | return SCM_UNSPECIFIED; /* not reached */ | |
217 | } | |
218 | ||
219 | ||
220 | #ifdef __STDC__ | |
221 | void | |
222 | scm_init_continuations (void) | |
223 | #else | |
224 | void | |
225 | scm_init_continuations () | |
226 | #endif | |
227 | { | |
228 | #include "continuations.x" | |
229 | } | |
230 |