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