* __scm.h, alist.c, async.c, async.h, backtrace.h, chars.c,
[bpt/guile.git] / libguile / continuations.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1998 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. */
0f2d19dd
JB
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
57static char s_cont[] = "continuation";
58
1cc91f1b 59
0f2d19dd
JB
60SCM
61scm_make_cont (answer)
62 SCM * answer;
0f2d19dd
JB
63{
64 long j;
65 SCM cont;
66
67#ifdef CHEAP_CONTINUATIONS
68 SCM_NEWCELL (cont);
69 *answer = cont;
f83e2737 70 SCM_ENTER_A_SECTION;
0db18cf4 71 SCM_SETJMPBUF (cont, scm_must_malloc ((long) sizeof (scm_contregs), s_cont));
f08df670 72 SCM_DYNENV (cont) = scm_dynwinds;
0f2d19dd
JB
73 SCM_THROW_VALUE = SCM_EOL;
74 SCM_BASE (cont) = SCM_BASE (rootcont);
75 SCM_SEQ (cont) = SCM_SEQ (rootcont);
f83e2737
MD
76 SCM_SETCAR (cont, scm_tc7_contin);
77 SCM_EXIT_A_SECTION;
0f2d19dd
JB
78#else
79 register SCM_STACKITEM *src, *dst;
80
f08df670 81#if 0
0f2d19dd
JB
82 {
83 SCM winds;
84
85 for (winds = scm_dynwinds; winds != SCM_EOL; winds = SCM_CDR (winds))
86 {
0f2d19dd
JB
87 if (SCM_INUMP (SCM_CAR (winds)))
88 {
89 scm_relocate_chunk_to_heap (SCM_CAR (winds));
90 }
0f2d19dd
JB
91 }
92 }
f08df670 93#endif
0f2d19dd
JB
94
95 SCM_NEWCELL (cont);
96 *answer = cont;
f83e2737 97 SCM_ENTER_A_SECTION;
0f2d19dd
JB
98 SCM_FLUSH_REGISTER_WINDOWS;
99 j = scm_stack_size (SCM_BASE (scm_rootcont));
100 SCM_SETJMPBUF (cont,
0db18cf4 101 scm_must_malloc ((long) (sizeof (scm_contregs) + j * sizeof (SCM_STACKITEM)),
0f2d19dd 102 s_cont));
0f2d19dd
JB
103 SCM_DYNENV (cont) = scm_dynwinds;
104 SCM_THROW_VALUE (cont) = SCM_EOL;
105 src = SCM_BASE (cont) = SCM_BASE (scm_rootcont);
106 SCM_SEQ (cont) = SCM_SEQ (scm_rootcont);
f83e2737
MD
107 SCM_SETLENGTH (cont, j, scm_tc7_contin);
108 SCM_EXIT_A_SECTION;
0f2d19dd
JB
109#ifndef SCM_STACK_GROWS_UP
110 src -= SCM_LENGTH (cont);
111#endif /* ndef SCM_STACK_GROWS_UP */
0db18cf4 112 dst = (SCM_STACKITEM *) (SCM_CHARS (cont) + sizeof (scm_contregs));
0f2d19dd
JB
113 for (j = SCM_LENGTH (cont); 0 <= --j;)
114 *dst++ = *src++;
115#endif /* def CHEAP_CONTINUATIONS */
116#ifdef DEBUG_EXTENSIONS
1646d37b 117 SCM_DFRAME (cont) = scm_last_debug_frame;
0f2d19dd
JB
118#endif
119 return cont;
120}
121
0f2d19dd
JB
122/* Grow the stack so that there is room */
123/* to copy in the continuation. Then */
124#ifndef CHEAP_CONTINUATIONS
1cc91f1b
JB
125
126static void grow_throw SCM_P ((SCM *a));
127
0f2d19dd
JB
128static void
129grow_throw (a)
130 SCM *a;
0f2d19dd
JB
131{ /* retry the throw. */
132 SCM growth[100];
133 growth[0] = a[0];
134 growth[1] = a[1];
135 growth[2] = a[2] + 1;
136 growth[3] = (SCM) a;
137 scm_dynthrow (growth);
138}
139#endif /* ndef CHEAP_CONTINUATIONS */
140
1cc91f1b 141
0f2d19dd
JB
142void
143scm_dynthrow (a)
144 SCM *a;
0f2d19dd
JB
145{
146 SCM cont = a[0], val = a[1];
147#ifndef CHEAP_CONTINUATIONS
148 register long j;
149 register SCM_STACKITEM *src, *dst = SCM_BASE (scm_rootcont);
150#ifdef SCM_STACK_GROWS_UP
151 if (a[2] && (a - ((SCM *) a[3]) < 100))
152#else
153 if (a[2] && (((SCM *) a[3]) - a < 100))
154#endif
155 fputs ("grow_throw: check if SCM growth[100]; being optimized out\n",
156 stderr);
157 /* if (a[2]) fprintf(stderr, " ct = %ld, dist = %ld\n",
158 a[2], (((SCM *)a[3]) - a)); */
159#ifdef SCM_STACK_GROWS_UP
160 if (SCM_PTR_GE (dst + SCM_LENGTH (cont), (SCM_STACKITEM *) & a))
161 grow_throw (a);
162#else
163 dst -= SCM_LENGTH (cont);
164 if (SCM_PTR_LE (dst, (SCM_STACKITEM *) & a))
165 grow_throw (a);
166#endif /* def SCM_STACK_GROWS_UP */
167 SCM_FLUSH_REGISTER_WINDOWS;
0db18cf4 168 src = (SCM_STACKITEM *) (SCM_CHARS (cont) + sizeof (scm_contregs));
0f2d19dd
JB
169 for (j = SCM_LENGTH (cont); 0 <= --j;)
170 *dst++ = *src++;
171#ifdef sparc /* clear out stack up to this stackframe */
172 /* maybe this would help, maybe not */
173/* bzero((void *)&a, sizeof(SCM_STACKITEM) * (((SCM_STACKITEM *)&a) -
174 (dst - SCM_LENGTH(cont)))) */
175#endif
176#endif /* ndef CHEAP_CONTINUATIONS */
177#ifdef DEBUG_EXTENSIONS
1646d37b 178 scm_last_debug_frame = SCM_DFRAME (cont);
0f2d19dd
JB
179#endif
180 SCM_THROW_VALUE(cont) = val;
181 longjmp (SCM_JMPBUF (cont), 1);
182}
183
1cc91f1b 184
0f2d19dd
JB
185SCM
186scm_call_continuation (cont, val)
187 SCM cont;
188 SCM val;
0f2d19dd
JB
189{
190 SCM a[3];
191 a[0] = cont;
192 a[1] = val;
193 a[2] = 0;
194 if ( (SCM_SEQ (cont) != SCM_SEQ (scm_rootcont))
195 || (SCM_BASE (cont) != SCM_BASE (scm_rootcont))) /* base compare not needed */
196 scm_wta (cont, "continuation from wrong top level", s_cont);
197
198 scm_dowinds (SCM_DYNENV (cont),
199 scm_ilength (scm_dynwinds) - scm_ilength (SCM_DYNENV (cont)));
200
201 scm_dynthrow (a);
202 return SCM_UNSPECIFIED; /* not reached */
203}
204
205
1cc91f1b 206
0f2d19dd
JB
207void
208scm_init_continuations ()
0f2d19dd
JB
209{
210#include "continuations.x"
211}
212