1 /* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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)
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.
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.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
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.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
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.
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.
46 #ifdef DEBUG_EXTENSIONS
51 #include "continuations.h"
57 static char s_cont
[] = "continuation";
61 scm_make_cont (answer
)
67 #ifdef CHEAP_CONTINUATIONS
71 SCM_SETJMPBUF (cont
, scm_must_malloc ((long) sizeof (scm_contregs
), s_cont
));
72 SCM_CAR (cont
) = scm_tc7_contin
;
73 SCM_DYNENV (cont
) = scm_dynwinds
;
74 SCM_THROW_VALUE
= SCM_EOL
;
75 SCM_BASE (cont
) = SCM_BASE (rootcont
);
76 SCM_SEQ (cont
) = SCM_SEQ (rootcont
);
79 register SCM_STACKITEM
*src
, *dst
;
85 for (winds
= scm_dynwinds
; winds
!= SCM_EOL
; winds
= SCM_CDR (winds
))
87 if (SCM_INUMP (SCM_CAR (winds
)))
89 scm_relocate_chunk_to_heap (SCM_CAR (winds
));
98 SCM_FLUSH_REGISTER_WINDOWS
;
99 j
= scm_stack_size (SCM_BASE (scm_rootcont
));
101 scm_must_malloc ((long) (sizeof (scm_contregs
) + j
* sizeof (SCM_STACKITEM
)),
103 SCM_SETLENGTH (cont
, j
, scm_tc7_contin
);
104 SCM_DYNENV (cont
) = scm_dynwinds
;
105 SCM_THROW_VALUE (cont
) = SCM_EOL
;
106 src
= SCM_BASE (cont
) = SCM_BASE (scm_rootcont
);
107 SCM_SEQ (cont
) = SCM_SEQ (scm_rootcont
);
109 #ifndef SCM_STACK_GROWS_UP
110 src
-= SCM_LENGTH (cont
);
111 #endif /* ndef SCM_STACK_GROWS_UP */
112 dst
= (SCM_STACKITEM
*) (SCM_CHARS (cont
) + sizeof (scm_contregs
));
113 for (j
= SCM_LENGTH (cont
); 0 <= --j
;)
115 #endif /* def CHEAP_CONTINUATIONS */
116 #ifdef DEBUG_EXTENSIONS
117 SCM_DFRAME (cont
) = scm_last_debug_frame
;
122 /* Grow the stack so that there is room */
123 /* to copy in the continuation. Then */
124 #ifndef CHEAP_CONTINUATIONS
126 static void grow_throw
SCM_P ((SCM
*a
));
131 { /* retry the throw. */
135 growth
[2] = a
[2] + 1;
137 scm_dynthrow (growth
);
139 #endif /* ndef CHEAP_CONTINUATIONS */
146 SCM cont
= a
[0], val
= a
[1];
147 #ifndef CHEAP_CONTINUATIONS
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))
153 if (a
[2] && (((SCM
*) a
[3]) - a
< 100))
155 fputs ("grow_throw: check if SCM growth[100]; being optimized out\n",
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
))
163 dst
-= SCM_LENGTH (cont
);
164 if (SCM_PTR_LE (dst
, (SCM_STACKITEM
*) & a
))
166 #endif /* def SCM_STACK_GROWS_UP */
167 SCM_FLUSH_REGISTER_WINDOWS
;
168 src
= (SCM_STACKITEM
*) (SCM_CHARS (cont
) + sizeof (scm_contregs
));
169 for (j
= SCM_LENGTH (cont
); 0 <= --j
;)
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)))) */
176 #endif /* ndef CHEAP_CONTINUATIONS */
177 #ifdef DEBUG_EXTENSIONS
178 scm_last_debug_frame
= SCM_DFRAME (cont
);
180 SCM_THROW_VALUE(cont
) = val
;
181 longjmp (SCM_JMPBUF (cont
), 1);
186 scm_call_continuation (cont
, val
)
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
);
198 scm_dowinds (SCM_DYNENV (cont
),
199 scm_ilength (scm_dynwinds
) - scm_ilength (SCM_DYNENV (cont
)));
202 return SCM_UNSPECIFIED
; /* not reached */
208 scm_init_continuations ()
210 #include "continuations.x"