*** empty log message ***
[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. */
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>
48#include "_scm.h"
ba11fd4c 49#include "root.h"
311df4f0
JB
50#include "stackchk.h"
51#ifdef DEBUG_EXTENSIONS
52#include "debug.h"
53#endif
54#include "dynwind.h"
0f2d19dd 55
311df4f0 56#include "continuations.h"
0f2d19dd
JB
57\f
58
59/* {Continuations}
60 */
61
62static char s_cont[] = "continuation";
63
1cc91f1b 64
0f2d19dd 65SCM
6e8d25a6 66scm_make_cont (SCM *answer)
0f2d19dd
JB
67{
68 long j;
69 SCM cont;
70
71#ifdef CHEAP_CONTINUATIONS
72 SCM_NEWCELL (cont);
73 *answer = cont;
f83e2737 74 SCM_ENTER_A_SECTION;
8b3bda20 75 SCM_SET_CONTREGS (cont, scm_must_malloc (sizeof (scm_contregs), s_cont));
f08df670 76 SCM_DYNENV (cont) = scm_dynwinds;
0f2d19dd
JB
77 SCM_THROW_VALUE = SCM_EOL;
78 SCM_BASE (cont) = SCM_BASE (rootcont);
79 SCM_SEQ (cont) = SCM_SEQ (rootcont);
f83e2737
MD
80 SCM_SETCAR (cont, scm_tc7_contin);
81 SCM_EXIT_A_SECTION;
0f2d19dd
JB
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;
f83e2737 101 SCM_ENTER_A_SECTION;
0f2d19dd
JB
102 SCM_FLUSH_REGISTER_WINDOWS;
103 j = scm_stack_size (SCM_BASE (scm_rootcont));
8b3bda20
DH
104 SCM_SET_CONTREGS (cont,
105 scm_must_malloc (sizeof (scm_contregs)
106 + j * sizeof (SCM_STACKITEM),
107 s_cont));
0f2d19dd
JB
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);
f83e2737
MD
112 SCM_SETLENGTH (cont, j, scm_tc7_contin);
113 SCM_EXIT_A_SECTION;
0f2d19dd
JB
114#ifndef SCM_STACK_GROWS_UP
115 src -= SCM_LENGTH (cont);
116#endif /* ndef SCM_STACK_GROWS_UP */
0db18cf4 117 dst = (SCM_STACKITEM *) (SCM_CHARS (cont) + sizeof (scm_contregs));
0f2d19dd
JB
118 for (j = SCM_LENGTH (cont); 0 <= --j;)
119 *dst++ = *src++;
120#endif /* def CHEAP_CONTINUATIONS */
121#ifdef DEBUG_EXTENSIONS
1646d37b 122 SCM_DFRAME (cont) = scm_last_debug_frame;
0f2d19dd
JB
123#endif
124 return cont;
125}
126
0f2d19dd
JB
127/* Grow the stack so that there is room */
128/* to copy in the continuation. Then */
129#ifndef CHEAP_CONTINUATIONS
1cc91f1b 130
0f2d19dd 131static void
1bbd0b84 132grow_throw (SCM *a)
0f2d19dd
JB
133{ /* retry the throw. */
134 SCM growth[100];
135 growth[0] = a[0];
136 growth[1] = a[1];
137 growth[2] = a[2] + 1;
138 growth[3] = (SCM) a;
139 scm_dynthrow (growth);
140}
141#endif /* ndef CHEAP_CONTINUATIONS */
142
1cc91f1b 143
0f2d19dd 144void
6e8d25a6 145scm_dynthrow (SCM *a)
0f2d19dd
JB
146{
147 SCM cont = a[0], val = a[1];
148#ifndef CHEAP_CONTINUATIONS
149 register long j;
150 register SCM_STACKITEM *src, *dst = SCM_BASE (scm_rootcont);
151#ifdef SCM_STACK_GROWS_UP
152 if (a[2] && (a - ((SCM *) a[3]) < 100))
153#else
154 if (a[2] && (((SCM *) a[3]) - a < 100))
155#endif
156 fputs ("grow_throw: check if SCM growth[100]; being optimized out\n",
157 stderr);
158 /* if (a[2]) fprintf(stderr, " ct = %ld, dist = %ld\n",
159 a[2], (((SCM *)a[3]) - a)); */
160#ifdef SCM_STACK_GROWS_UP
161 if (SCM_PTR_GE (dst + SCM_LENGTH (cont), (SCM_STACKITEM *) & a))
162 grow_throw (a);
163#else
164 dst -= SCM_LENGTH (cont);
165 if (SCM_PTR_LE (dst, (SCM_STACKITEM *) & a))
166 grow_throw (a);
167#endif /* def SCM_STACK_GROWS_UP */
168 SCM_FLUSH_REGISTER_WINDOWS;
0db18cf4 169 src = (SCM_STACKITEM *) (SCM_CHARS (cont) + sizeof (scm_contregs));
0f2d19dd
JB
170 for (j = SCM_LENGTH (cont); 0 <= --j;)
171 *dst++ = *src++;
172#ifdef sparc /* clear out stack up to this stackframe */
173 /* maybe this would help, maybe not */
174/* bzero((void *)&a, sizeof(SCM_STACKITEM) * (((SCM_STACKITEM *)&a) -
175 (dst - SCM_LENGTH(cont)))) */
176#endif
177#endif /* ndef CHEAP_CONTINUATIONS */
178#ifdef DEBUG_EXTENSIONS
1646d37b 179 scm_last_debug_frame = SCM_DFRAME (cont);
0f2d19dd
JB
180#endif
181 SCM_THROW_VALUE(cont) = val;
182 longjmp (SCM_JMPBUF (cont), 1);
183}
184
1cc91f1b 185
0f2d19dd 186SCM
6e8d25a6 187scm_call_continuation (SCM cont, SCM val)
0f2d19dd
JB
188{
189 SCM a[3];
190 a[0] = cont;
191 a[1] = val;
192 a[2] = 0;
193 if ( (SCM_SEQ (cont) != SCM_SEQ (scm_rootcont))
194 || (SCM_BASE (cont) != SCM_BASE (scm_rootcont))) /* base compare not needed */
195 scm_wta (cont, "continuation from wrong top level", s_cont);
196
197 scm_dowinds (SCM_DYNENV (cont),
198 scm_ilength (scm_dynwinds) - scm_ilength (SCM_DYNENV (cont)));
199
200 scm_dynthrow (a);
201 return SCM_UNSPECIFIED; /* not reached */
202}
203
204
1cc91f1b 205
0f2d19dd
JB
206void
207scm_init_continuations ()
0f2d19dd
JB
208{
209#include "continuations.x"
210}
211
89e00824
ML
212
213/*
214 Local Variables:
215 c-file-style: "gnu"
216 End:
217*/